1 # Copyright (c) 2003-2005 The Regents of The University of Michigan
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met: redistributions of source code must retain the above copyright
7 # notice, this list of conditions and the following disclaimer;
8 # redistributions in binary form must reproduce the above copyright
9 # notice, this list of conditions and the following disclaimer in the
10 # documentation and/or other materials provided with the distribution;
11 # neither the name of the copyright holders nor the names of its
12 # contributors may be used to endorse or promote products derived from
13 # this software without specific prior written permission.
15 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 # Prepend the directory where the PLY lex & yacc modules are found
39 sys
.path
[0:0] = [os
.environ
['M5_PLY']]
44 ##########################################################################
46 # Base classes for use outside of the assembler
48 ##########################################################################
50 class Micro_Container(object):
51 def __init__(self
, name
):
55 self
.micro_classes
= {}
58 def add_microop(self
, mnemonic
, microop
):
59 self
.microops
.append(microop
)
62 string
= "%s:\n" % self
.name
63 for microop
in self
.microops
:
64 string
+= " %s\n" % microop
67 class Combinational_Macroop(Micro_Container
):
70 class Rom_Macroop(object):
71 def __init__(self
, name
, target
):
76 return "%s: %s\n" % (self
.name
, self
.target
)
78 class Rom(Micro_Container
):
79 def __init__(self
, name
):
80 super(Rom
, self
).__init
__(name
)
83 ##########################################################################
87 ##########################################################################
98 class Statement(object):
100 self
.is_microop
= False
101 self
.is_directive
= False
104 class Microop(Statement
):
106 super(Microop
, self
).__init
__()
109 self
.is_microop
= True
111 class Directive(Statement
):
113 super(Directive
, self
).__init
__()
115 self
.is_directive
= True
117 ##########################################################################
119 # Functions that handle common tasks
121 ##########################################################################
123 def print_error(message
):
125 print "*** %s" % message
128 def handle_statement(parser
, container
, statement
):
129 if statement
.is_microop
:
130 if statement
.mnemonic
not in parser
.microops
.keys():
131 raise Exception, "Unrecognized mnemonic: %s" % statement
.mnemonic
132 parser
.symbols
["__microopClassFromInsideTheAssembler"] = \
133 parser
.microops
[statement
.mnemonic
]
135 microop
= eval('__microopClassFromInsideTheAssembler(%s)' %
136 statement
.params
, {}, parser
.symbols
)
138 print_error("Error creating microop object with mnemonic %s." % \
142 for label
in statement
.labels
:
143 container
.labels
[label
.text
] = microop
145 container
.externs
[label
.text
] = microop
146 container
.add_microop(statement
.mnemonic
, microop
)
148 print_error("Error adding microop.")
150 elif statement
.is_directive
:
151 if statement
.name
not in container
.directives
.keys():
152 raise Exception, "Unrecognized directive: %s" % statement
.name
153 parser
.symbols
["__directiveFunctionFromInsideTheAssembler"] = \
154 container
.directives
[statement
.name
]
156 eval('__directiveFunctionFromInsideTheAssembler(%s)' %
157 statement
.params
, {}, parser
.symbols
)
159 print_error("Error executing directive.")
160 print container
.directives
163 raise Exception, "Didn't recognize the type of statement", statement
165 ##########################################################################
167 # Lexer specification
169 ##########################################################################
171 # Error handler. Just call exit. Output formatted to work under
172 # Emacs compile-mode. Optional 'print_traceback' arg, if set to True,
173 # prints a Python stack backtrace too (can be handy when trying to
174 # debug the parser itself).
175 def error(lineno
, string
, print_traceback
= False):
176 # Print a Python stack backtrace if requested.
177 if (print_traceback
):
178 traceback
.print_exc()
180 line_str
= "%d:" % lineno
183 sys
.exit("%s %s" % (line_str
, string
))
185 reserved
= ('DEF', 'MACROOP', 'ROM', 'EXTERN')
187 tokens
= reserved
+ (
190 # arguments for microops and directives
195 'COLON', 'SEMI', 'DOT',
199 # New lines are ignored at the top level, but they end statements in the
202 ('asm', 'exclusive'),
203 ('params', 'exclusive'),
208 reserved_map
[r
.lower()] = r
211 def t_ANY_COMMENT(t
):
214 def t_ANY_MULTILINECOMMENT(t
):
215 r
'/\*([^/]|((?<!\*)/))*\*/'
217 # A colon marks the end of a label. It should follow an ID which will
218 # put the lexer in the "params" state. Seeing the colon will put it back
219 # in the "asm" state since it knows it saw a label and not a mnemonic.
220 def t_params_COLON(t
):
225 # Parameters are a string of text which don't contain an unescaped statement
226 # statement terminator, ie a newline or semi colon.
227 def t_params_PARAMS(t
):
228 r
'([^\n;\\]|(\\[\n;\\]))+'
229 t
.lineno
+= t
.value
.count('\n')
230 unescapeParamsRE
= re
.compile(r
'(\\[\n;\\])')
231 def unescapeParams(mo
):
234 t
.value
= unescapeParamsRE
.sub(unescapeParams
, t
.value
)
238 # An "ID" in the micro assembler is either a label, directive, or mnemonic
239 # If it's either a directive or a mnemonic, it will be optionally followed by
240 # parameters. If it's a label, the following colon will make the lexer stop
241 # looking for parameters.
244 t
.type = reserved_map
.get(t
.value
, 'ID')
245 # If the ID is really "extern", we shouldn't start looking for parameters
246 # yet. The real ID, the label itself, is coming up.
247 if t
.type != 'EXTERN':
248 t
.lexer
.begin('params')
251 # If there is a label and you're -not- in the assembler (which would be caught
252 # above), don't start looking for parameters.
255 t
.type = reserved_map
.get(t
.value
, 'ID')
258 # Braces enter and exit micro assembly
259 def t_INITIAL_LBRACE(t
):
266 t
.lexer
.begin('INITIAL')
269 # At the top level, keep track of newlines only for line counting.
270 def t_INITIAL_NEWLINE(t
):
272 t
.lineno
+= t
.value
.count('\n')
274 # In the micro assembler, do line counting but also return a token. The
275 # token is needed by the parser to detect the end of a statement.
276 def t_asm_NEWLINE(t
):
278 t
.lineno
+= t
.value
.count('\n')
281 # A newline or semi colon when looking for params signals that the statement
282 # is over and the lexer should go back to looking for regular assembly.
283 def t_params_NEWLINE(t
):
285 t
.lineno
+= t
.value
.count('\n')
289 def t_params_SEMI(t
):
294 # Basic regular expressions to pick out simple tokens
300 t_ANY_ignore
= ' \t\x0c'
303 error(t
.lineno
, "illegal character '%s'" % t
.value
[0])
306 ##########################################################################
308 # Parser specification
310 ##########################################################################
312 # Start symbol for a file which may have more than one macroop or rom
315 'file : opt_rom_or_macros'
317 def p_opt_rom_or_macros_0(t
):
318 'opt_rom_or_macros : '
320 def p_opt_rom_or_macros_1(t
):
321 'opt_rom_or_macros : rom_or_macros'
323 def p_rom_or_macros_0(t
):
324 'rom_or_macros : rom_or_macro'
326 def p_rom_or_macros_1(t
):
327 'rom_or_macros : rom_or_macros rom_or_macro'
329 def p_rom_or_macro_0(t
):
330 '''rom_or_macro : rom_block
333 # Defines a section of microcode that should go in the current ROM
335 'rom_block : DEF ROM block SEMI'
337 print_error("Rom block found, but no Rom object specified.")
338 raise TypeError, "Rom block found, but no Rom object was specified."
339 for statement
in t
[3].statements
:
340 handle_statement(t
.parser
, t
.parser
.rom
, statement
)
343 # Defines a macroop that jumps to an external label in the ROM
344 def p_macroop_def_0(t
):
345 'macroop_def : DEF MACROOP ID LPAREN ID RPAREN SEMI'
346 if not t
.parser
.rom_macroop_type
:
347 print_error("ROM based macroop found, but no ROM macroop class was specified.")
348 raise TypeError, "ROM based macroop found, but no ROM macroop class was specified."
349 macroop
= t
.parser
.rom_macroop_type(t
[3], t
[5])
350 t
.parser
.macroops
[t
[3]] = macroop
353 # Defines a macroop that is combinationally generated
354 def p_macroop_def_1(t
):
355 'macroop_def : DEF MACROOP ID block SEMI'
357 curop
= t
.parser
.macro_type(t
[3])
359 print_error("Error creating macroop object.")
361 for statement
in t
[4].statements
:
362 handle_statement(t
.parser
, curop
, statement
)
363 t
.parser
.macroops
[t
[3]] = curop
365 # A block of statements
367 'block : LBRACE statements RBRACE'
369 block
.statements
= t
[2]
372 def p_statements_0(t
):
373 'statements : statement'
379 def p_statements_1(t
):
380 'statements : statements statement'
386 'statement : content_of_statement end_of_statement'
389 # A statement can be a microop or an assembler directive
390 def p_content_of_statement_0(t
):
391 '''content_of_statement : microop
395 # Ignore empty statements
396 def p_content_of_statement_1(t
):
397 'content_of_statement : '
400 # Statements are ended by newlines or a semi colon
401 def p_end_of_statement(t
):
402 '''end_of_statement : NEWLINE
406 # Different flavors of microop to avoid shift/reduce errors
408 'microop : labels ID'
410 microop
.labels
= t
[1]
411 microop
.mnemonic
= t
[2]
417 microop
.mnemonic
= t
[1]
421 'microop : labels ID PARAMS'
423 microop
.labels
= t
[1]
424 microop
.mnemonic
= t
[2]
425 microop
.params
= t
[3]
429 'microop : ID PARAMS'
431 microop
.mnemonic
= t
[1]
432 microop
.params
= t
[2]
435 # Labels in the microcode
441 'labels : labels label'
445 # labels on lines by themselves are attached to the following instruction.
447 'labels : labels NEWLINE'
453 label
.is_extern
= False
458 'label : EXTERN ID COLON'
460 label
.is_extern
= True
464 # Directives for the macroop
465 def p_directive_0(t
):
467 directive
= Directive()
468 directive
.name
= t
[2]
471 def p_directive_1(t
):
472 'directive : DOT ID PARAMS'
473 directive
= Directive()
474 directive
.name
= t
[2]
475 directive
.params
= t
[3]
478 # Parse error handler. Note that the argument here is the offending
479 # *token*, not a grammar symbol (hence the need to use t.value)
482 error(t
.lineno
, "syntax error at '%s'" % t
.value
)
484 error(0, "unknown syntax error", True)
486 class MicroAssembler(object):
488 def __init__(self
, macro_type
, microops
,
489 rom
= None, rom_macroop_type
= None):
490 self
.lexer
= lex
.lex()
491 self
.parser
= yacc
.yacc()
492 self
.parser
.macro_type
= macro_type
493 self
.parser
.macroops
= {}
494 self
.parser
.microops
= microops
495 self
.parser
.rom
= rom
496 self
.parser
.rom_macroop_type
= rom_macroop_type
497 self
.parser
.symbols
= {}
498 self
.symbols
= self
.parser
.symbols
500 def assemble(self
, asm
):
501 self
.parser
.parse(asm
, lexer
=self
.lexer
)
502 macroops
= self
.parser
.macroops
503 self
.parser
.macroops
= {}