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.
27 from __future__
import print_function
39 ##########################################################################
41 # Base classes for use outside of the assembler
43 ##########################################################################
45 class Micro_Container(object):
46 def __init__(self
, name
):
50 self
.micro_classes
= {}
53 def add_microop(self
, mnemonic
, microop
):
54 self
.microops
.append(microop
)
57 string
= "%s:\n" % self
.name
58 for microop
in self
.microops
:
59 string
+= " %s\n" % microop
62 class Combinational_Macroop(Micro_Container
):
65 class Rom_Macroop(object):
66 def __init__(self
, name
, target
):
71 return "%s: %s\n" % (self
.name
, self
.target
)
73 class Rom(Micro_Container
):
74 def __init__(self
, name
):
75 super(Rom
, self
).__init
__(name
)
78 ##########################################################################
82 ##########################################################################
93 class Statement(object):
95 self
.is_microop
= False
96 self
.is_directive
= False
99 class Microop(Statement
):
101 super(Microop
, self
).__init
__()
104 self
.is_microop
= True
106 class Directive(Statement
):
108 super(Directive
, self
).__init
__()
110 self
.is_directive
= True
112 ##########################################################################
114 # Functions that handle common tasks
116 ##########################################################################
118 def print_error(message
):
120 print("*** %s" % message
)
123 def handle_statement(parser
, container
, statement
):
124 if statement
.is_microop
:
125 if statement
.mnemonic
not in parser
.microops
.keys():
126 raise Exception, "Unrecognized mnemonic: %s" % statement
.mnemonic
127 parser
.symbols
["__microopClassFromInsideTheAssembler"] = \
128 parser
.microops
[statement
.mnemonic
]
130 microop
= eval('__microopClassFromInsideTheAssembler(%s)' %
131 statement
.params
, {}, parser
.symbols
)
133 print_error("Error creating microop object with mnemonic %s." % \
137 for label
in statement
.labels
:
138 container
.labels
[label
.text
] = microop
140 container
.externs
[label
.text
] = microop
141 container
.add_microop(statement
.mnemonic
, microop
)
143 print_error("Error adding microop.")
145 elif statement
.is_directive
:
146 if statement
.name
not in container
.directives
.keys():
147 raise Exception, "Unrecognized directive: %s" % statement
.name
148 parser
.symbols
["__directiveFunctionFromInsideTheAssembler"] = \
149 container
.directives
[statement
.name
]
151 eval('__directiveFunctionFromInsideTheAssembler(%s)' %
152 statement
.params
, {}, parser
.symbols
)
154 print_error("Error executing directive.")
155 print(container
.directives
)
158 raise Exception, "Didn't recognize the type of statement", statement
160 ##########################################################################
162 # Lexer specification
164 ##########################################################################
166 # Error handler. Just call exit. Output formatted to work under
167 # Emacs compile-mode. Optional 'print_traceback' arg, if set to True,
168 # prints a Python stack backtrace too (can be handy when trying to
169 # debug the parser itself).
170 def error(lineno
, string
, print_traceback
= False):
171 # Print a Python stack backtrace if requested.
172 if (print_traceback
):
173 traceback
.print_exc()
175 line_str
= "%d:" % lineno
178 sys
.exit("%s %s" % (line_str
, string
))
180 reserved
= ('DEF', 'MACROOP', 'ROM', 'EXTERN')
182 tokens
= reserved
+ (
185 # arguments for microops and directives
190 'COLON', 'SEMI', 'DOT',
194 # New lines are ignored at the top level, but they end statements in the
197 ('asm', 'exclusive'),
198 ('params', 'exclusive'),
203 reserved_map
[r
.lower()] = r
206 def t_ANY_COMMENT(t
):
209 def t_ANY_MULTILINECOMMENT(t
):
210 r
'/\*([^/]|((?<!\*)/))*\*/'
212 # A colon marks the end of a label. It should follow an ID which will
213 # put the lexer in the "params" state. Seeing the colon will put it back
214 # in the "asm" state since it knows it saw a label and not a mnemonic.
215 def t_params_COLON(t
):
220 # Parameters are a string of text which don't contain an unescaped statement
221 # statement terminator, ie a newline or semi colon.
222 def t_params_PARAMS(t
):
223 r
'([^\n;\\]|(\\[\n;\\]))+'
224 t
.lineno
+= t
.value
.count('\n')
225 unescapeParamsRE
= re
.compile(r
'(\\[\n;\\])')
226 def unescapeParams(mo
):
229 t
.value
= unescapeParamsRE
.sub(unescapeParams
, t
.value
)
233 # An "ID" in the micro assembler is either a label, directive, or mnemonic
234 # If it's either a directive or a mnemonic, it will be optionally followed by
235 # parameters. If it's a label, the following colon will make the lexer stop
236 # looking for parameters.
239 t
.type = reserved_map
.get(t
.value
, 'ID')
240 # If the ID is really "extern", we shouldn't start looking for parameters
241 # yet. The real ID, the label itself, is coming up.
242 if t
.type != 'EXTERN':
243 t
.lexer
.begin('params')
246 # If there is a label and you're -not- in the assembler (which would be caught
247 # above), don't start looking for parameters.
250 t
.type = reserved_map
.get(t
.value
, 'ID')
253 # Braces enter and exit micro assembly
254 def t_INITIAL_LBRACE(t
):
261 t
.lexer
.begin('INITIAL')
264 # At the top level, keep track of newlines only for line counting.
265 def t_INITIAL_NEWLINE(t
):
267 t
.lineno
+= t
.value
.count('\n')
269 # In the micro assembler, do line counting but also return a token. The
270 # token is needed by the parser to detect the end of a statement.
271 def t_asm_NEWLINE(t
):
273 t
.lineno
+= t
.value
.count('\n')
276 # A newline or semi colon when looking for params signals that the statement
277 # is over and the lexer should go back to looking for regular assembly.
278 def t_params_NEWLINE(t
):
280 t
.lineno
+= t
.value
.count('\n')
284 def t_params_SEMI(t
):
289 # Basic regular expressions to pick out simple tokens
295 t_ANY_ignore
= ' \t\x0c'
298 error(t
.lineno
, "illegal character '%s'" % t
.value
[0])
301 ##########################################################################
303 # Parser specification
305 ##########################################################################
307 # Start symbol for a file which may have more than one macroop or rom
310 'file : opt_rom_or_macros'
312 def p_opt_rom_or_macros_0(t
):
313 'opt_rom_or_macros : '
315 def p_opt_rom_or_macros_1(t
):
316 'opt_rom_or_macros : rom_or_macros'
318 def p_rom_or_macros_0(t
):
319 'rom_or_macros : rom_or_macro'
321 def p_rom_or_macros_1(t
):
322 'rom_or_macros : rom_or_macros rom_or_macro'
324 def p_rom_or_macro_0(t
):
325 '''rom_or_macro : rom_block
328 # Defines a section of microcode that should go in the current ROM
330 'rom_block : DEF ROM block SEMI'
332 print_error("Rom block found, but no Rom object specified.")
333 raise TypeError, "Rom block found, but no Rom object was specified."
334 for statement
in t
[3].statements
:
335 handle_statement(t
.parser
, t
.parser
.rom
, statement
)
338 # Defines a macroop that jumps to an external label in the ROM
339 def p_macroop_def_0(t
):
340 'macroop_def : DEF MACROOP ID LPAREN ID RPAREN SEMI'
341 if not t
.parser
.rom_macroop_type
:
342 print_error("ROM based macroop found, but no ROM macroop class was specified.")
343 raise TypeError, "ROM based macroop found, but no ROM macroop class was specified."
344 macroop
= t
.parser
.rom_macroop_type(t
[3], t
[5])
345 t
.parser
.macroops
[t
[3]] = macroop
348 # Defines a macroop that is combinationally generated
349 def p_macroop_def_1(t
):
350 'macroop_def : DEF MACROOP ID block SEMI'
352 curop
= t
.parser
.macro_type(t
[3])
354 print_error("Error creating macroop object.")
356 for statement
in t
[4].statements
:
357 handle_statement(t
.parser
, curop
, statement
)
358 t
.parser
.macroops
[t
[3]] = curop
360 # A block of statements
362 'block : LBRACE statements RBRACE'
364 block
.statements
= t
[2]
367 def p_statements_0(t
):
368 'statements : statement'
374 def p_statements_1(t
):
375 'statements : statements statement'
381 'statement : content_of_statement end_of_statement'
384 # A statement can be a microop or an assembler directive
385 def p_content_of_statement_0(t
):
386 '''content_of_statement : microop
390 # Ignore empty statements
391 def p_content_of_statement_1(t
):
392 'content_of_statement : '
395 # Statements are ended by newlines or a semi colon
396 def p_end_of_statement(t
):
397 '''end_of_statement : NEWLINE
401 # Different flavors of microop to avoid shift/reduce errors
403 'microop : labels ID'
405 microop
.labels
= t
[1]
406 microop
.mnemonic
= t
[2]
412 microop
.mnemonic
= t
[1]
416 'microop : labels ID PARAMS'
418 microop
.labels
= t
[1]
419 microop
.mnemonic
= t
[2]
420 microop
.params
= t
[3]
424 'microop : ID PARAMS'
426 microop
.mnemonic
= t
[1]
427 microop
.params
= t
[2]
430 # Labels in the microcode
436 'labels : labels label'
440 # labels on lines by themselves are attached to the following instruction.
442 'labels : labels NEWLINE'
448 label
.is_extern
= False
453 'label : EXTERN ID COLON'
455 label
.is_extern
= True
459 # Directives for the macroop
460 def p_directive_0(t
):
462 directive
= Directive()
463 directive
.name
= t
[2]
466 def p_directive_1(t
):
467 'directive : DOT ID PARAMS'
468 directive
= Directive()
469 directive
.name
= t
[2]
470 directive
.params
= t
[3]
473 # Parse error handler. Note that the argument here is the offending
474 # *token*, not a grammar symbol (hence the need to use t.value)
477 error(t
.lineno
, "syntax error at '%s'" % t
.value
)
479 error(0, "unknown syntax error", True)
481 class MicroAssembler(object):
483 def __init__(self
, macro_type
, microops
,
484 rom
= None, rom_macroop_type
= None):
485 self
.lexer
= lex
.lex()
486 self
.parser
= yacc
.yacc()
487 self
.parser
.macro_type
= macro_type
488 self
.parser
.macroops
= {}
489 self
.parser
.microops
= microops
490 self
.parser
.rom
= rom
491 self
.parser
.rom_macroop_type
= rom_macroop_type
492 self
.parser
.symbols
= {}
493 self
.symbols
= self
.parser
.symbols
495 def assemble(self
, asm
):
496 self
.parser
.parse(asm
, lexer
=self
.lexer
)
497 macroops
= self
.parser
.macroops
498 self
.parser
.macroops
= {}