micro_asm.py (4483:1e62824dcc3d) micro_asm.py (4502:766acd3fa962)
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',
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 #'COMMA',
178 'COLON', 'SEMI', 'DOT',
179 'NEWLINE'
180 )
181
182# New lines are ignored at the top level, but they end statements in the
183# assembler
184states = (
185 ('asm', 'exclusive'),
186 ('params', 'exclusive'),
187)
188
189reserved_map = { }
190for r in reserved:
191 reserved_map[r.lower()] = r
192
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
193def t_params_COLON(t):
194 r':'
195 t.lexer.begin('asm')
200def t_params_COLON(t):
201 r':'
202 t.lexer.begin('asm')
203 #print "t_params_COLON %s" % t.value
196 return t
197
198def t_asm_ID(t):
199 r'[A-Za-z_]\w*'
200 t.type = reserved_map.get(t.value, 'ID')
201 t.lexer.begin('params')
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
202 return t
203
204def t_ANY_ID(t):
205 r'[A-Za-z_]\w*'
206 t.type = reserved_map.get(t.value, 'ID')
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
207 return t
208
209def t_params_PARAMS(t):
210 r'([^\n;]|((?<=\\)[\n;]))+'
211 t.lineno += t.value.count('\n')
212 t.lexer.begin('asm')
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
213 return t
214
215def t_INITIAL_LBRACE(t):
216 r'\{'
217 t.lexer.begin('asm')
224 return t
225
226def t_INITIAL_LBRACE(t):
227 r'\{'
228 t.lexer.begin('asm')
229 #print "t_INITIAL_LBRACE %s" % t.value
218 return t
219
220def t_asm_RBRACE(t):
221 r'\}'
222 t.lexer.begin('INITIAL')
230 return t
231
232def t_asm_RBRACE(t):
233 r'\}'
234 t.lexer.begin('INITIAL')
235 #print "t_asm_RBRACE %s" % t.value
223 return t
224
225def t_INITIAL_NEWLINE(t):
226 r'\n+'
227 t.lineno += t.value.count('\n')
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
228
229def t_asm_NEWLINE(t):
230 r'\n+'
231 t.lineno += t.value.count('\n')
242
243def t_asm_NEWLINE(t):
244 r'\n+'
245 t.lineno += t.value.count('\n')
246 #print "t_asm_NEWLINE %s" % t.value
232 return t
233
234def t_params_NEWLINE(t):
235 r'\n+'
236 t.lineno += t.value.count('\n')
237 t.lexer.begin('asm')
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
238 return t
239
240def t_params_SEMI(t):
241 r';'
242 t.lexer.begin('asm')
254 return t
255
256def t_params_SEMI(t):
257 r';'
258 t.lexer.begin('asm')
259 #print "t_params_SEMI %s" % t.value
243 return t
244
245# Basic regular expressions to pick out simple tokens
246t_ANY_LPAREN = r'\('
247t_ANY_RPAREN = r'\)'
260 return t
261
262# Basic regular expressions to pick out simple tokens
263t_ANY_LPAREN = r'\('
264t_ANY_RPAREN = r'\)'
248#t_COMMA = r','
249t_ANY_SEMI = r';'
250t_ANY_DOT = r'\.'
251
252t_ANY_ignore = ' \t\x0c'
253
254def t_ANY_error(t):
255 error(t.lineno, "illegal character '%s'" % t.value[0])
256 t.skip(1)

--- 38 unchanged lines hidden (view full) ---

295def p_rom_block(t):
296 'rom_block : DEF ROM block SEMI'
297 for statement in t[3].statements:
298 handle_statement(t.parser, t.parser.rom, statement)
299 t[0] = t.parser.rom
300
301# Defines a macroop that jumps to an external label in the ROM
302def p_macroop_def_0(t):
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):
303 'macroop_def : DEF MACROOP LPAREN ID RPAREN SEMI'
319 'macroop_def : DEF MACROOP ID LPAREN ID RPAREN SEMI'
304 t[0] = t[4]
305
306# Defines a macroop that is combinationally generated
307def p_macroop_def_1(t):
308 'macroop_def : DEF MACROOP ID block SEMI'
309 try:
310 curop = t.parser.macro_type(t[3])
311 except TypeError:
312 print_error("Error creating macroop object.")
313 raise
314 for statement in t[4].statements:
315 handle_statement(t.parser, curop, statement)
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)
316 t.parser.macroops.append(curop)
332 t.parser.macroops[t[3]] = curop
317
318def p_statements_0(t):
319 'statements : statement'
320 if t[1]:
321 t[0] = [t[1]]
322 else:
323 t[0] = []
324

--- 89 unchanged lines hidden (view full) ---

414 error(0, "unknown syntax error", True)
415
416class MicroAssembler(object):
417
418 def __init__(self, macro_type, microops, rom):
419 self.lexer = lex.lex()
420 self.parser = yacc.yacc()
421 self.parser.macro_type = macro_type
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
422 self.parser.macroops = []
438 self.parser.macroops = {}
423 self.parser.microops = microops
424 self.parser.rom = rom
425
426 def assemble(self, asm):
427 self.parser.parse(asm, lexer=self.lexer)
439 self.parser.microops = microops
440 self.parser.rom = rom
441
442 def assemble(self, asm):
443 self.parser.parse(asm, lexer=self.lexer)
428 for macroop in self.parser.macroops:
444 # Begin debug printing
445 for macroop in self.parser.macroops.values():
429 print macroop
430 print self.parser.rom
446 print macroop
447 print self.parser.rom
448 # End debug printing
431 macroops = self.parser.macroops
449 macroops = self.parser.macroops
432 self.parser.macroops = []
450 self.parser.macroops = {}
433 return macroops
451 return macroops