mipsy.py 7.39 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

import string
Benoit Perrot's avatar
Benoit Perrot committed
22
import os.path, filecmp, shutil
23
24
25
26
from xml.sax import ContentHandler, make_parser

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

Benoit Perrot's avatar
Benoit Perrot committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
//"""
45
46
47
48
49
50
51
52
53
54
55
56
57
58

## ---------------------------------------------------------------------------
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

Benoit Perrot's avatar
Benoit Perrot committed
59
60
61
62
63
64
65
66
## Overwrite file if different
def lazy_overwrite(ref, new):
  if not os.path.isfile(ref) or \
         not filecmp.cmp(ref, new):
    print "> Overwrite: " + ref
    shutil.copy(new, ref)

## Return a conventional macro identifier
67
68
def define_id(s):
  return string.upper(s)
Benoit Perrot's avatar
Benoit Perrot committed
69
70

## Return a conventional C++ class identifier
71
def class_id(s):
Benoit Perrot's avatar
Benoit Perrot committed
72
  # Rename j as jmp for convenience
73
74
75
76
77
78
79
80
81
82
  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:
Benoit Perrot's avatar
Benoit Perrot committed
83
        res = res + s[i]
84
85
86
87
    else:
      non_ascii = True
  return res

Benoit Perrot's avatar
Benoit Perrot committed
88
89
90
## Return a conventional file name
def file_id(s):
  return string.lower(s)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143

## -------------------------------------
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
144
  def __init__(self, params, alias, level):
145
146
    self.params = params
    self.alias = alias
Benoit Perrot's avatar
Benoit Perrot committed
147
    self.level = level
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163

  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
164
165
  def set_level(self, level):
    self.level = level
166
167
168
  def add_param(self, param):
    self.params.append(param)
  def get(self):
Benoit Perrot's avatar
Benoit Perrot committed
169
    return Syntax(self.params, self.alias, self.level)
170
171
172
173


## -------------------------------------
class Instruction:
Benoit Perrot's avatar
Benoit Perrot committed
174
  def __init__(self, opcode, level, kind, format, syntaxes):
175
176
    self.opcode = opcode
    self.level = level
Benoit Perrot's avatar
Benoit Perrot committed
177
    self.kind = kind
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
    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 = ""
Benoit Perrot's avatar
Benoit Perrot committed
193
    self.kind = ""
194
195
196
197
198
199
200
201
202
    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
Benoit Perrot's avatar
Benoit Perrot committed
203
204
  def set_kind(self, kind):
    self.kind = kind
205
206
207
208
209
  def set_format(self, format):
    self.format = format
  def add_syntax(self, syntax):
    self.syntaxes.append(syntax)
  def get(self):
Benoit Perrot's avatar
Benoit Perrot committed
210
211
    return Instruction(self.opcode, self.level, self.kind,
                       self.format, self.syntaxes)
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226

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

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"])
Benoit Perrot's avatar
Benoit Perrot committed
227
      self.ib.set_kind(attrs["kind"])
228
229
230
231
232
233
    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
234
235
236
237
      if attrs.has_key("level"):
        self.sb.set_level(attrs["level"])
      else:
        self.sb.set_level(self.ib.level)
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
    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