mipsy.py 6.87 KB
Newer Older
1
#!/usr/bin/env python
Benoit Perrot's avatar
Benoit Perrot committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
##
## This file is part of Mipsy, a tiny MIPS simulator
## Copyright (C) 2003 Benoit Perrot <benoit@lrde.epita.fr>
##
## Mipsy is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
## 
## Mipsy is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##
20
21
22
23
24
25

import string
from xml.sax import ContentHandler, make_parser

## ---------------------------------------------------------------------------

Benoit Perrot's avatar
Benoit Perrot committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
license = """//
// This file is part of Mipsy, a tiny MIPS simulator
// Copyright (C) 2003 Benoit Perrot <benoit@lrde.epita.fr>
//
// Mipsy is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// Mipsy is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//"""
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129

## ---------------------------------------------------------------------------
def depth(root):
  if len(root) == 0:
    return []
  res = []
  children = [ [] ]
  if len(root) > 1:
    children = depth(root[1:])
  for e in root[0]:
    for child in children:
      res.append([e] + child)
  return res

def define_id(s):
  return string.upper(s)
def class_id(s):
  # Stupid libc J numeric constant.
  if s == "j":
    return "Jmp"
  res = ""
  non_ascii = True
  for i in range(0, len(s)):
    if s[i] in string.ascii_letters:
      if non_ascii:
        res = res + string.upper(s[i])
        non_ascii = False
      else:
        res = res + string.lower(s[i])
    else:
      non_ascii = True
  return res


## -------------------------------------
class Param:
  def __init__(self, kind, name, value, default):
    self.kind = kind
    self.name = name
    self.value = value
    self.default = default
  def display(self):
    print self.kind + " " + self.name + " = " + self.value

class ParamBuilder:
  def reset(self):
    self.kind = ""
    self.name = ""
    self.value = "" 
    self.default = ""    
  def __init__(self):
    self.reset()
    
  def set_kind(self, kind):
    self.kind = kind
  def set_name(self, name):
    self.name = name
  def set_value(self, value):
    self.value = value
  def set_default(self, default):
    self.default = default
  def get(self):
    return Param(self.kind, self.name, self.value, self.default)

## -------------------------------------
class Format:
  def __init__(self, params):
    self.params = params

  def display(self):
    for p in self.params:
      p.display()

class FormatBuilder:
  def reset(self):
    self.params = []
  def __init__(self):
    self.reset()

  def add_param(self, param):
    self.params.append(param)
  def get(self):
    return Format(self.params)

 ## -------------------------------------
class Syntax:
Benoit Perrot's avatar
Benoit Perrot committed
130
  def __init__(self, params, alias, level):
131
132
    self.params = params
    self.alias = alias
Benoit Perrot's avatar
Benoit Perrot committed
133
    self.level = level
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

  def display(self):
    if self.alias != "":
      print self.alias
    for p in self.params:
      p.display()

class SyntaxBuilder:
  def reset(self):
    self.params = []
    self.alias = ""
  def __init__(self):
    self.reset()

  def set_alias(self, alias):
    self.alias = alias
Benoit Perrot's avatar
Benoit Perrot committed
150
151
  def set_level(self, level):
    self.level = level
152
153
154
  def add_param(self, param):
    self.params.append(param)
  def get(self):
Benoit Perrot's avatar
Benoit Perrot committed
155
    return Syntax(self.params, self.alias, self.level)
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213


## -------------------------------------
class Instruction:
  def __init__(self, opcode, level, format, syntaxes):
    self.opcode = opcode
    self.level = level
    self.format = format
    self.syntaxes = syntaxes

  def display(self):
    print self.opcode + " >>"
    self.format.display()
    for s in self.syntaxes:
      print "<syntax>"
      s.display()
      print "</syntax>"

class InstructionBuilder:
  def reset(self):
    self.opcode = ""
    self.level = ""
    self.format = Format("")
    self.syntaxes = []
  def __init__(self):
    self.reset()

  def set_opcode(self, opcode):
    self.opcode = opcode
  def set_level(self, level):
    self.level = level
  def set_format(self, format):
    self.format = format
  def add_syntax(self, syntax):
    self.syntaxes.append(syntax)
  def get(self):
    return Instruction(self.opcode, self.level, self.format, self.syntaxes)

## -------------------------------------

class InstructionSetHandler(ContentHandler):
  pb = ParamBuilder()
  sb = SyntaxBuilder()
  fb = None
  ib = InstructionBuilder()
  instructions = []
  
  def startElement(self, name, attrs):
    if name == "instruction":
      self.ib.reset()
      self.ib.set_opcode(attrs["opcode"])
      self.ib.set_level(attrs["level"])
    elif name == "format":
      self.fb = FormatBuilder()
    elif name == "syntax":
      self.sb.reset()
      if attrs.has_key("alias"):
        self.sb.set_alias(attrs["alias"])
Benoit Perrot's avatar
Benoit Perrot committed
214
215
216
217
      if attrs.has_key("level"):
        self.sb.set_level(attrs["level"])
      else:
        self.sb.set_level(self.ib.level)
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
    elif name in ["register", "immediate", "label", "address", "hidden"]:
      self.pb.reset()
      self.pb.set_kind(name)
      if attrs.has_key("name"):
        self.pb.set_name(attrs["name"])
      if attrs.has_key("default"):
        self.pb.set_default(attrs["default"])
      if attrs.has_key("value"):
        self.pb.set_value(attrs["value"])

  def endElement(self, name):
    if name in ["register", "immediate", "label", "address", "hidden"]:
      if self.fb != None:
        self.fb.add_param(self.pb.get())
      else:
        self.sb.add_param(self.pb.get())
    elif name == "syntax":
      self.ib.add_syntax(self.sb.get())
    elif name == "format":
      self.ib.set_format(self.fb.get())
      self.fb = None
    elif name == "instruction":
      self.instructions.append(self.ib.get())

class InstructionSetParser:

  def parse(self, file):
    # Create a parser
    parser = make_parser()
    # Create the handler
    h = InstructionSetHandler()
    # Tell the parser to use our handler
    parser.setContentHandler(h)
    
    # Parse the input
    parser.parse(file)

    # Return Instruction set
    return h.instructions