1# Copyright (c) 2003-2005 The Regents of The University of Michigan 2# All rights reserved. 3# 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 --- 160 unchanged lines hidden (view full) --- 169tokens = reserved + ( 170 # identifier 171 'ID', 172 # arguments for microops and directives 173 'PARAMS', 174 175 'LPAREN', 'RPAREN', 176 'LBRACE', 'RBRACE', |
177 'COLON', 'SEMI', 'DOT', 178 'NEWLINE' 179 ) 180 181# New lines are ignored at the top level, but they end statements in the 182# assembler 183states = ( 184 ('asm', 'exclusive'), 185 ('params', 'exclusive'), 186) 187 188reserved_map = { } 189for r in reserved: 190 reserved_map[r.lower()] = r 191 |
192def t_ANY_COMMENT(t): 193 r'\#[^\n]*(?=\n)' 194 #print "t_ANY_COMMENT %s" % t.value 195 196def t_ANY_MULTILINECOMMENT(t): 197 r'/\*([^/]|((?<!\*)/))*\*/' 198 #print "t_ANY_MULTILINECOMMENT %s" % t.value 199 |
200def t_params_COLON(t): 201 r':' 202 t.lexer.begin('asm') |
203 #print "t_params_COLON %s" % t.value |
204 return t 205 206def t_asm_ID(t): 207 r'[A-Za-z_]\w*' 208 t.type = reserved_map.get(t.value, 'ID') 209 t.lexer.begin('params') |
210 #print "t_asm_ID %s" % t.value |
211 return t 212 213def t_ANY_ID(t): 214 r'[A-Za-z_]\w*' 215 t.type = reserved_map.get(t.value, 'ID') |
216 #print "t_ANY_ID %s" % t.value |
217 return t 218 219def t_params_PARAMS(t): 220 r'([^\n;]|((?<=\\)[\n;]))+' 221 t.lineno += t.value.count('\n') 222 t.lexer.begin('asm') |
223 #print "t_params_PARAMS %s" % t.value |
224 return t 225 226def t_INITIAL_LBRACE(t): 227 r'\{' 228 t.lexer.begin('asm') |
229 #print "t_INITIAL_LBRACE %s" % t.value |
230 return t 231 232def t_asm_RBRACE(t): 233 r'\}' 234 t.lexer.begin('INITIAL') |
235 #print "t_asm_RBRACE %s" % t.value |
236 return t 237 238def t_INITIAL_NEWLINE(t): 239 r'\n+' 240 t.lineno += t.value.count('\n') |
241 #print "t_INITIAL_NEWLINE %s" % t.value |
242 243def t_asm_NEWLINE(t): 244 r'\n+' 245 t.lineno += t.value.count('\n') |
246 #print "t_asm_NEWLINE %s" % t.value |
247 return t 248 249def t_params_NEWLINE(t): 250 r'\n+' 251 t.lineno += t.value.count('\n') 252 t.lexer.begin('asm') |
253 #print "t_params_NEWLINE %s" % t.value |
254 return t 255 256def t_params_SEMI(t): 257 r';' 258 t.lexer.begin('asm') |
259 #print "t_params_SEMI %s" % t.value |
260 return t 261 262# Basic regular expressions to pick out simple tokens 263t_ANY_LPAREN = r'\(' 264t_ANY_RPAREN = r'\)' |
265t_ANY_SEMI = r';' 266t_ANY_DOT = r'\.' 267 268t_ANY_ignore = ' \t\x0c' 269 270def t_ANY_error(t): 271 error(t.lineno, "illegal character '%s'" % t.value[0]) 272 t.skip(1) --- 38 unchanged lines hidden (view full) --- 311def p_rom_block(t): 312 'rom_block : DEF ROM block SEMI' 313 for statement in t[3].statements: 314 handle_statement(t.parser, t.parser.rom, statement) 315 t[0] = t.parser.rom 316 317# Defines a macroop that jumps to an external label in the ROM 318def p_macroop_def_0(t): |
319 'macroop_def : DEF MACROOP ID LPAREN ID RPAREN SEMI' |
320 t[0] = t[4] 321 322# Defines a macroop that is combinationally generated 323def p_macroop_def_1(t): 324 'macroop_def : DEF MACROOP ID block SEMI' 325 try: 326 curop = t.parser.macro_type(t[3]) 327 except TypeError: 328 print_error("Error creating macroop object.") 329 raise 330 for statement in t[4].statements: 331 handle_statement(t.parser, curop, statement) |
332 t.parser.macroops[t[3]] = curop |
333 334def p_statements_0(t): 335 'statements : statement' 336 if t[1]: 337 t[0] = [t[1]] 338 else: 339 t[0] = [] 340 --- 89 unchanged lines hidden (view full) --- 430 error(0, "unknown syntax error", True) 431 432class MicroAssembler(object): 433 434 def __init__(self, macro_type, microops, rom): 435 self.lexer = lex.lex() 436 self.parser = yacc.yacc() 437 self.parser.macro_type = macro_type |
438 self.parser.macroops = {} |
439 self.parser.microops = microops 440 self.parser.rom = rom 441 442 def assemble(self, asm): 443 self.parser.parse(asm, lexer=self.lexer) |
444 # Begin debug printing 445 for macroop in self.parser.macroops.values(): |
446 print macroop 447 print self.parser.rom |
448 # End debug printing |
449 macroops = self.parser.macroops |
450 self.parser.macroops = {} |
451 return macroops |