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
, 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 Macroop(Micro_Container
):
70 class Rom(Micro_Container
):
71 def __init__(self
, name
):
72 super(Rom
, self
).__init
__(name
)
75 ##########################################################################
79 ##########################################################################
90 class Statement(object):
92 self
.is_microop
= False
93 self
.is_directive
= False
96 class Microop(Statement
):
98 super(Microop
, self
).__init
__()
101 self
.is_microop
= True
103 class Directive(Statement
):
105 super(Directive
, self
).__init
__()
107 self
.is_directive
= True
109 ##########################################################################
111 # Functions that handle common tasks
113 ##########################################################################
115 def print_error(message
):
117 print "*** %s" % message
120 def handle_statement(parser
, container
, statement
):
121 if statement
.is_microop
:
123 microop
= eval('parser.microops[statement.mnemonic](%s)' %
126 print_error("Error creating microop object.")
129 for label
in statement
.labels
:
130 container
.labels
[label
.name
] = microop
132 container
.externs
[label
.name
] = microop
133 container
.add_microop(microop
)
135 print_error("Error adding microop.")
137 elif statement
.is_directive
:
139 eval('container.directives[statement.name](%s)' % statement
.params
)
141 print_error("Error executing directive.")
142 print container
.directives
145 raise Exception, "Didn't recognize the type of statement", statement
147 ##########################################################################
149 # Lexer specification
151 ##########################################################################
153 # Error handler. Just call exit. Output formatted to work under
154 # Emacs compile-mode. Optional 'print_traceback' arg, if set to True,
155 # prints a Python stack backtrace too (can be handy when trying to
156 # debug the parser itself).
157 def error(lineno
, string
, print_traceback
= False):
158 # Print a Python stack backtrace if requested.
159 if (print_traceback
):
160 traceback
.print_exc()
162 line_str
= "%d:" % lineno
165 sys
.exit("%s %s" % (line_str
, string
))
167 reserved
= ('DEF', 'MACROOP', 'ROM', 'EXTERN')
169 tokens
= reserved
+ (
172 # arguments for microops and directives
177 'COLON', 'SEMI', 'DOT',
181 # New lines are ignored at the top level, but they end statements in the
184 ('asm', 'exclusive'),
185 ('params', 'exclusive'),
190 reserved_map
[r
.lower()] = r
192 def t_ANY_COMMENT(t
):
194 #print "t_ANY_COMMENT %s" % t.value
196 def t_ANY_MULTILINECOMMENT(t
):
197 r
'/\*([^/]|((?<!\*)/))*\*/'
198 #print "t_ANY_MULTILINECOMMENT %s" % t.value
200 def t_params_COLON(t
):
203 #print "t_params_COLON %s" % t.value
208 t
.type = reserved_map
.get(t
.value
, 'ID')
209 t
.lexer
.begin('params')
210 #print "t_asm_ID %s" % t.value
215 t
.type = reserved_map
.get(t
.value
, 'ID')
216 #print "t_ANY_ID %s" % t.value
219 def t_params_PARAMS(t
):
220 r
'([^\n;]|((?<=\\)[\n;]))+'
221 t
.lineno
+= t
.value
.count('\n')
223 #print "t_params_PARAMS %s" % t.value
226 def t_INITIAL_LBRACE(t
):
229 #print "t_INITIAL_LBRACE %s" % t.value
234 t
.lexer
.begin('INITIAL')
235 #print "t_asm_RBRACE %s" % t.value
238 def t_INITIAL_NEWLINE(t
):
240 t
.lineno
+= t
.value
.count('\n')
241 #print "t_INITIAL_NEWLINE %s" % t.value
243 def t_asm_NEWLINE(t
):
245 t
.lineno
+= t
.value
.count('\n')
246 #print "t_asm_NEWLINE %s" % t.value
249 def t_params_NEWLINE(t
):
251 t
.lineno
+= t
.value
.count('\n')
253 #print "t_params_NEWLINE %s" % t.value
256 def t_params_SEMI(t
):
259 #print "t_params_SEMI %s" % t.value
262 # Basic regular expressions to pick out simple tokens
268 t_ANY_ignore
= ' \t\x0c'
271 error(t
.lineno
, "illegal character '%s'" % t
.value
[0])
274 ##########################################################################
276 # Parser specification
278 ##########################################################################
280 # Start symbol for a file which may have more than one macroop or rom
283 'file : opt_rom_or_macros'
285 def p_opt_rom_or_macros_0(t
):
286 'opt_rom_or_macros : '
288 def p_opt_rom_or_macros_1(t
):
289 'opt_rom_or_macros : rom_or_macros'
291 def p_rom_or_macros_0(t
):
292 'rom_or_macros : rom_or_macro'
294 def p_rom_or_macros_1(t
):
295 'rom_or_macros : rom_or_macros rom_or_macro'
297 def p_rom_or_macro_0(t
):
298 '''rom_or_macro : rom_block'''
300 def p_rom_or_macro_1(t
):
301 '''rom_or_macro : macroop_def'''
303 # A block of statements
305 'block : LBRACE statements RBRACE'
307 block
.statements
= t
[2]
310 # Defines a section of microcode that should go in the current ROM
312 'rom_block : DEF ROM block SEMI'
313 for statement
in t
[3].statements
:
314 handle_statement(t
.parser
, t
.parser
.rom
, statement
)
317 # Defines a macroop that jumps to an external label in the ROM
318 def p_macroop_def_0(t
):
319 'macroop_def : DEF MACROOP ID LPAREN ID RPAREN SEMI'
322 # Defines a macroop that is combinationally generated
323 def p_macroop_def_1(t
):
324 'macroop_def : DEF MACROOP ID block SEMI'
326 curop
= t
.parser
.macro_type(t
[3])
328 print_error("Error creating macroop object.")
330 for statement
in t
[4].statements
:
331 handle_statement(t
.parser
, curop
, statement
)
332 t
.parser
.macroops
[t
[3]] = curop
334 def p_statements_0(t
):
335 'statements : statement'
341 def p_statements_1(t
):
342 'statements : statements statement'
348 'statement : content_of_statement end_of_statement'
351 # A statement can be a microop or an assembler directive
352 def p_content_of_statement_0(t
):
353 '''content_of_statement : microop
357 def p_content_of_statement_1(t
):
358 'content_of_statement : '
361 # Statements are ended by newlines or a semi colon
362 def p_end_of_statement(t
):
363 '''end_of_statement : NEWLINE
368 'microop : labels ID'
370 microop
.labels
= t
[1]
371 microop
.mnemonic
= t
[2]
377 microop
.mnemonic
= t
[1]
381 'microop : labels ID PARAMS'
383 microop
.labels
= t
[1]
384 microop
.mnemonic
= t
[2]
385 microop
.params
= t
[3]
389 'microop : ID PARAMS'
391 microop
.mnemonic
= t
[1]
392 microop
.params
= t
[2]
400 'labels : labels label'
407 label
.is_extern
= False
412 'label : EXTERN ID COLON'
414 label
.is_extern
= True
418 def p_directive_0(t
):
420 directive
= Directive()
421 directive
.name
= t
[2]
424 def p_directive_1(t
):
425 'directive : DOT ID PARAMS'
426 directive
= Directive()
427 directive
.name
= t
[2]
428 directive
.params
= t
[3]
431 # Parse error handler. Note that the argument here is the offending
432 # *token*, not a grammar symbol (hence the need to use t.value)
435 error(t
.lineno
, "syntax error at '%s'" % t
.value
)
437 error(0, "unknown syntax error", True)
439 class MicroAssembler(object):
441 def __init__(self
, macro_type
, microops
, rom
):
442 self
.lexer
= lex
.lex()
443 self
.parser
= yacc
.yacc()
444 self
.parser
.macro_type
= macro_type
445 self
.parser
.macroops
= {}
446 self
.parser
.microops
= microops
447 self
.parser
.rom
= rom
449 def assemble(self
, asm
):
450 self
.parser
.parse(asm
, lexer
=self
.lexer
)
451 # Begin debug printing
452 for macroop
in self
.parser
.macroops
.values():
454 print self
.parser
.rom
456 macroops
= self
.parser
.macroops
457 self
.parser
.macroops
= {}