nolimips.py 11 KB
Newer Older
1
#!/usr/bin/env python
Benoit Perrot's avatar
Benoit Perrot committed
2
##
Benoit Perrot's avatar
Benoit Perrot committed
3
## This file is part of Nolimips, a MIPS simulator with unlimited registers
4
## Copyright (C) 2003, 2004, 2005 Benoit Perrot <benoit@lrde.epita.fr>
Benoit Perrot's avatar
Benoit Perrot committed
5
##
Benoit Perrot's avatar
Benoit Perrot committed
6
## Nolimips is free software; you can redistribute it and/or modify
Benoit Perrot's avatar
Benoit Perrot committed
7
8
9
10
## 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.
## 
Benoit Perrot's avatar
Benoit Perrot committed
11
## Nolimips is distributed in the hope that it will be useful,
Benoit Perrot's avatar
Benoit Perrot committed
12
13
14
15
16
17
18
19
## 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 sys, os.path, filecmp, shutil
23
24
25
26
from xml.sax import ContentHandler, make_parser

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

Benoit Perrot's avatar
Benoit Perrot committed
27
license = """//
Benoit Perrot's avatar
Benoit Perrot committed
28
// This file is part of Nolimips, a MIPS simulator with unlimited registers
29
// Copyright (C) 2004, 2005 Benoit Perrot <benoit@lrde.epita.fr>
Benoit Perrot's avatar
Benoit Perrot committed
30
//
Benoit Perrot's avatar
Benoit Perrot committed
31
// Nolimips is free software; you can redistribute it and/or modify
Benoit Perrot's avatar
Benoit Perrot committed
32
33
34
35
// 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.
// 
Benoit Perrot's avatar
Benoit Perrot committed
36
// Nolimips is distributed in the hope that it will be useful,
Benoit Perrot's avatar
Benoit Perrot committed
37
38
39
40
41
42
43
44
// 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
  if s == "j":
    return "Jmp"
  res = ""
  non_ascii = True
  for i in range(0, len(s)):
Benoit Perrot's avatar
Benoit Perrot committed
78
    if s[i] in string.ascii_letters or s[i] in string.digits:
79
80
81
82
      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

Benoit Perrot's avatar
Benoit Perrot committed
92
93
94
95
96
97
98
## Return a valid C++ variable name
def var_id(s):
  s = string.lower(s)
  if s in ["and", "or", "xor"]:
    s = s + "_";
  return s

99
## -------------------------------------
Benoit Perrot's avatar
Benoit Perrot committed
100
class Attribute:
Benoit Perrot's avatar
Benoit Perrot committed
101
  def __init__(self, type, restriction, name, value):
Benoit Perrot's avatar
Benoit Perrot committed
102
    self.type = type
Benoit Perrot's avatar
Benoit Perrot committed
103
    self.restriction = restriction
104
    self.name = name
Benoit Perrot's avatar
Benoit Perrot committed
105
106
    self.value = value # FIXME: useless
  def __str__(self):
Benoit Perrot's avatar
Benoit Perrot committed
107
108
109
    return "<attribute type=\"" + self.type + \
           "\" restriction=\"" + self.restriction + \
           "\" name=\"" + self.name +"\" />"
Benoit Perrot's avatar
Benoit Perrot committed
110
class AttributeBuilder:
111
  def reset(self):
Benoit Perrot's avatar
Benoit Perrot committed
112
    self.type = ""
Benoit Perrot's avatar
Benoit Perrot committed
113
    self.restriction = ""
114
    self.name = ""
Benoit Perrot's avatar
Benoit Perrot committed
115
    self.value = ""# FIXME: useless
116
117
118
  def __init__(self):
    self.reset()
  def get(self):
Benoit Perrot's avatar
Benoit Perrot committed
119
    assert(self.type != "" and self.name != "")
Benoit Perrot's avatar
Benoit Perrot committed
120
    return Attribute(self.type, self.restriction, self.name, self.value)
121
122

class Format:
123
124
  def __init__(self, type, attributes):
    self.type = type
Benoit Perrot's avatar
Benoit Perrot committed
125
126
127
128
129
130
    self.attributes = attributes
  def __str__(self):
    res = "  <format>"
    for p in self.attributes:
      res = res + "\n    " + str(p)
    return res + "\n  </format>"
131
132
class FormatBuilder:
  def reset(self):
133
    self.type = ""
Benoit Perrot's avatar
Benoit Perrot committed
134
    self.attributes = []
135
136
  def __init__(self):
    self.reset()
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
  def set_to_predefined(self, template):
    self.type = template
    if template == "register":
      self.attributes = [
        Attribute("Register", "", "dest", ""),
        Attribute("Register", "", "src1", ""),
        Attribute("Register", "", "src2", "")]
    elif template == "immediate":
      self.attributes = [
        Attribute("Register", "", "dest", ""),
        Attribute("Register", "", "src", ""),
        Attribute("Exp", "", "imm", "")]
    elif template == "jump":
      self.attributes = [
        Attribute("Register", "", "src1", ""),
        Attribute("Register", "", "src2", ""),
        Attribute("Exp", "", "label", "")]
    else:
      assert(False)
Benoit Perrot's avatar
Benoit Perrot committed
156
  def get(self):
157
    return Format(self.type, self.attributes)
Benoit Perrot's avatar
Benoit Perrot committed
158

159

Benoit Perrot's avatar
Benoit Perrot committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
## -------------------------------------
class Token:
  def __init__(self, kind, name, value, default):
    self.kind = kind
    self.name = name
    self.value = value
    self.default = default
  def __str__(self):
    res = "<token kind=\"" + self.kind + "\" "
    if self.name != "":
      res = res + "name=\"" + self.name + "\" "
    if self.value != "":
      res = res + "value=\"" + self.value + "\" "
    if self.default != "":
      res = res + "default=\"" + self.default + "\" "
    return res + "/>"
class TokenBuilder:
  def reset(self):
    self.kind = ""
    self.name = ""
    self.value = "" 
    self.default = ""
  def __init__(self):
    self.reset()
184
  def get(self):
Benoit Perrot's avatar
Benoit Perrot committed
185
186
    assert(self.kind != "")
    return Token(self.kind, self.name, self.value, self.default)
187
188

class Syntax:
Benoit Perrot's avatar
Benoit Perrot committed
189
190
  def __init__(self, tokens, alias, level):
    self.tokens = tokens
191
    self.alias = alias
Benoit Perrot's avatar
Benoit Perrot committed
192
    self.level = level
Benoit Perrot's avatar
Benoit Perrot committed
193
194
  def __str__(self):
    res = "  <syntax"
195
    if self.alias != "":
Benoit Perrot's avatar
Benoit Perrot committed
196
197
198
199
200
      res = res + " alias=\"" + self.alias + "\""
    res = res + ">"
    for t in self.tokens:
      res = res + "\n    " + str(t)
    return res + "\n  </syntax>"
201
202
class SyntaxBuilder:
  def reset(self):
Benoit Perrot's avatar
Benoit Perrot committed
203
    self.tokens = []
204
    self.alias = ""
205
    self.level = ""
206
207
  def __init__(self):
    self.reset()
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
  def set_to_predefined(self, template, alias, level):
    self.alias = alias
    self.level = level
    if template == "register":
      self.tokens = [
        Token("register", "dest", "", ""),
        Token("register", "", "", "${dest}"),
        Token("register", "", "", "")]
    elif template == "immediate":
      self.tokens = [
        Token("register", "dest", "", ""),
        Token("register", "", "", "${dest}"),
        Token("immediate", "", "", "")]
    elif template == "jump":
      self.tokens = [
        Token("register", "", "", ""),
        Token("register", "", "", ""),
        Token("label", "", "", "")]
226
227
    else:
      assert(False)
228
  def get(self):
Benoit Perrot's avatar
Benoit Perrot committed
229
    return Syntax(self.tokens, self.alias, self.level)
230
231
232
233


## -------------------------------------
class Instruction:
Benoit Perrot's avatar
Benoit Perrot committed
234
  def __init__(self, opcode, level, kind, desc, format, syntaxes):
235
236
    self.opcode = opcode
    self.level = level
Benoit Perrot's avatar
Benoit Perrot committed
237
    self.kind = kind
Benoit Perrot's avatar
Benoit Perrot committed
238
    self.desc = desc
239
240
    self.format = format
    self.syntaxes = syntaxes
Benoit Perrot's avatar
Benoit Perrot committed
241
242
243
244
245
246
  def __str__(self):
    res = "<instruction opcode=\"" + self.opcode + \
          "\" level=\"" + self.level + \
          "\" kind=\"" + self.kind + ">\n" + \
          "  <description>" + self.desc + "</description>\n" + \
          str(self.format) + "\n"
247
    for s in self.syntaxes:
Benoit Perrot's avatar
Benoit Perrot committed
248
249
      res = res + str(s) + "\n"
    return res + "</instruction>"
250
251
252
253
254

class InstructionBuilder:
  def reset(self):
    self.opcode = ""
    self.level = ""
Benoit Perrot's avatar
Benoit Perrot committed
255
    self.kind = ""
Benoit Perrot's avatar
Benoit Perrot committed
256
    self.desc = ""
257
    self.format = Format("", [])
258
259
260
261
    self.syntaxes = []
  def __init__(self):
    self.reset()
  def get(self):
262
263
    assert(self.opcode != "" and self.level != "" and self.kind != "" and
           (self.level != "native" or self.format.type != ""))
Benoit Perrot's avatar
Benoit Perrot committed
264
    return Instruction(self.opcode, self.level, self.kind, self.desc,
Benoit Perrot's avatar
Benoit Perrot committed
265
                       self.format, self.syntaxes)
266
267


Benoit Perrot's avatar
Benoit Perrot committed
268
## -------------------------------------
269
class InstructionSetHandler(ContentHandler):
Benoit Perrot's avatar
Benoit Perrot committed
270
271
272
273
274
275
276
277
278
279
  def reset(self):
    self.attr_b = AttributeBuilder()
    self.format_b = None
    self.token_b = TokenBuilder()
    self.syntax_b = SyntaxBuilder()
    self.inst_b = InstructionBuilder()
    self.instructions = []
    self.content = ""
  def __init__(self):
    self.reset()
280
281
282
  
  def startElement(self, name, attrs):
    if name == "instruction":
Benoit Perrot's avatar
Benoit Perrot committed
283
284
285
286
      self.inst_b.reset()
      self.inst_b.opcode = attrs["opcode"]
      self.inst_b.level = attrs["level"]
      self.inst_b.kind = attrs["kind"]
287
      
Benoit Perrot's avatar
Benoit Perrot committed
288
289
    elif name == "description":
      self.content = ""
290
      
291
    elif name == "format":
Benoit Perrot's avatar
Benoit Perrot committed
292
      self.format_b = FormatBuilder()
293
294
      if attrs.has_key("type"):
        self.format_b.type = attrs["type"]
295
296
297
    elif name == "predefinedAttributes":
      self.format_b = FormatBuilder()
      self.format_b.set_to_predefined(attrs["template"])
Benoit Perrot's avatar
Benoit Perrot committed
298
299
300
    elif name == "attribute":
      self.attr_b.reset()
      self.attr_b.type = attrs["type"]
Benoit Perrot's avatar
Benoit Perrot committed
301
302
      if attrs.has_key("restriction"):
        self.attr_b.restriction = attrs["restriction"]
Benoit Perrot's avatar
Benoit Perrot committed
303
      self.attr_b.name = attrs["name"]
304

305
    elif name == "syntax":
Benoit Perrot's avatar
Benoit Perrot committed
306
      self.syntax_b.reset()
307
      if attrs.has_key("alias"):
Benoit Perrot's avatar
Benoit Perrot committed
308
        self.syntax_b.alias = attrs["alias"]
Benoit Perrot's avatar
Benoit Perrot committed
309
      if attrs.has_key("level"):
Benoit Perrot's avatar
Benoit Perrot committed
310
        self.syntax_b.level = attrs["level"]
Benoit Perrot's avatar
Benoit Perrot committed
311
      else:
Benoit Perrot's avatar
Benoit Perrot committed
312
        self.syntax_b.level = self.inst_b.level
313
314
315
316
317
318
319
320
321
322
    elif name == "predefinedSyntax":
      alias = ""
      if attrs.has_key("alias"):
        alias = attrs["alias"]
      level = ""
      if attrs.has_key("level"):
        level = attrs["level"]
      else:
        level = self.inst_b.level
      self.syntax_b.set_to_predefined(attrs["template"], alias, level)
Benoit Perrot's avatar
Benoit Perrot committed
323
324
325
    elif name == "token":
      self.token_b.reset()
      self.token_b.kind = attrs["kind"]
326
      if attrs.has_key("name"):
Benoit Perrot's avatar
Benoit Perrot committed
327
        self.token_b.name = attrs["name"]
328
      if attrs.has_key("default"):
Benoit Perrot's avatar
Benoit Perrot committed
329
        self.token_b.default = attrs["default"]
330
      if attrs.has_key("value"):
Benoit Perrot's avatar
Benoit Perrot committed
331
        self.token_b.value = attrs["value"]
332
333

  def endElement(self, name):
Benoit Perrot's avatar
Benoit Perrot committed
334
335
    if name == "token":
      self.syntax_b.tokens.append(self.token_b.get())
336
    elif name == "syntax" or name == "predefinedSyntax":
Benoit Perrot's avatar
Benoit Perrot committed
337
      self.inst_b.syntaxes.append(self.syntax_b.get())
338
      
Benoit Perrot's avatar
Benoit Perrot committed
339
340
    if name == "attribute":
      self.format_b.attributes.append(self.attr_b.get())
341
342
    elif name == "predefinedAttributes":
      self.inst_b.format = self.format_b.get()
343
    elif name == "format":
Benoit Perrot's avatar
Benoit Perrot committed
344
      self.inst_b.format = self.format_b.get()
345
      
Benoit Perrot's avatar
Benoit Perrot committed
346
347
    elif name == "description":
      self.inst_b.desc = self.content
348
      
349
    elif name == "instruction":
Benoit Perrot's avatar
Benoit Perrot committed
350
351
352
353
      self.instructions.append(self.inst_b.get())

  def characters (self, content):
    self.content = self.content + content
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369

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