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

## -------------------------------------
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()
Benoit Perrot's avatar
Benoit Perrot committed
110

111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
  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 get(self):
    return Format(self.params)

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

  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 get(self):
Benoit Perrot's avatar
Benoit Perrot committed
153
    return Syntax(self.params, self.alias, self.level)
154
155
156
157


## -------------------------------------
class Instruction:
Benoit Perrot's avatar
Benoit Perrot committed
158
  def __init__(self, opcode, level, kind, format, syntaxes):
159
160
    self.opcode = opcode
    self.level = level
Benoit Perrot's avatar
Benoit Perrot committed
161
    self.kind = kind
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
    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
177
    self.kind = ""
178
179
180
181
182
183
    self.format = Format("")
    self.syntaxes = []
  def __init__(self):
    self.reset()

  def get(self):
Benoit Perrot's avatar
Benoit Perrot committed
184
185
    return Instruction(self.opcode, self.level, self.kind,
                       self.format, self.syntaxes)
186
187
188
189
190
191
192
193
194
195
196
197
198

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

class InstructionSetHandler(ContentHandler):
  pb = ParamBuilder()
  sb = SyntaxBuilder()
  fb = None
  ib = InstructionBuilder()
  instructions = []
  
  def startElement(self, name, attrs):
    if name == "instruction":
      self.ib.reset()
Benoit Perrot's avatar
Benoit Perrot committed
199
200
201
      self.ib.opcode = attrs["opcode"]
      self.ib.level = attrs["level"]
      self.ib.kind = attrs["kind"]
202
203
204
205
206
    elif name == "format":
      self.fb = FormatBuilder()
    elif name == "syntax":
      self.sb.reset()
      if attrs.has_key("alias"):
Benoit Perrot's avatar
Benoit Perrot committed
207
        self.sb.alias = attrs["alias"]
Benoit Perrot's avatar
Benoit Perrot committed
208
      if attrs.has_key("level"):
Benoit Perrot's avatar
Benoit Perrot committed
209
        self.sb.level = attrs["level"]
Benoit Perrot's avatar
Benoit Perrot committed
210
      else:
Benoit Perrot's avatar
Benoit Perrot committed
211
        self.sb.level = self.ib.level
212
213
    elif name in ["register", "immediate", "label", "address", "hidden"]:
      self.pb.reset()
Benoit Perrot's avatar
Benoit Perrot committed
214
      self.pb.kind = name
215
      if attrs.has_key("name"):
Benoit Perrot's avatar
Benoit Perrot committed
216
        self.pb.name = attrs["name"]
217
      if attrs.has_key("default"):
Benoit Perrot's avatar
Benoit Perrot committed
218
        self.pb.default = attrs["default"]
219
      if attrs.has_key("value"):
Benoit Perrot's avatar
Benoit Perrot committed
220
        self.pb.value = attrs["value"]
221
222
223
224

  def endElement(self, name):
    if name in ["register", "immediate", "label", "address", "hidden"]:
      if self.fb != None:
Benoit Perrot's avatar
Benoit Perrot committed
225
        self.fb.params.append(self.pb.get())
226
      else:
Benoit Perrot's avatar
Benoit Perrot committed
227
        self.sb.params.append(self.pb.get())
228
    elif name == "syntax":
Benoit Perrot's avatar
Benoit Perrot committed
229
      self.ib.syntaxes.append(self.sb.get())
230
    elif name == "format":
Benoit Perrot's avatar
Benoit Perrot committed
231
      self.ib.format = self.fb.get()
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
      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