comparison python/ppci/assembler.py @ 342:86b02c98a717 devel

Moved target directory
author Windel Bouwman
date Sat, 01 Mar 2014 15:40:31 +0100
parents 4d204f6f7d4e
children b4882ff0ed06
comparison
equal deleted inserted replaced
341:4d204f6f7d4e 342:86b02c98a717
1 1
2 import re 2 import re
3 import pyyacc 3 import pyyacc
4 from . import Token, CompilerError, SourceLocation 4 from . import Token, CompilerError, SourceLocation
5 from target import Target, Label 5 from .target import Target, Label
6 from .asmnodes import ALabel, AInstruction, ABinop, AUnop, ASymbol, ANumber 6 from .asmnodes import ALabel, AInstruction, ABinop, AUnop, ASymbol, ANumber
7 7
8 8
9 def bit_type(value): 9 def bit_type(value):
10 assert value < (2**31) 10 assert value < (2**31)
11 assert value >= 0 11 assert value >= 0
12 t = 'val32' 12 t = 'val32'
13 for n in [8, 5, 3]: 13 for n in [16, 8, 5, 3]:
14 if value < (2**n): 14 if value < (2**n):
15 t = 'val{}'.format(n) 15 t = 'val{}'.format(n)
16 return t 16 return t
17 17
18 def tokenize(s, kws): 18 def tokenize(s, kws):
98 98
99 def __init__(self, kws, instruction_rules, emit): 99 def __init__(self, kws, instruction_rules, emit):
100 # Construct a parser given a grammar: 100 # Construct a parser given a grammar:
101 tokens2 = ['ID', 'NUMBER', ',', '[', ']', ':', '+', '-', '*', 101 tokens2 = ['ID', 'NUMBER', ',', '[', ']', ':', '+', '-', '*',
102 pyyacc.EPS, 'COMMENT', '{', '}', 102 pyyacc.EPS, 'COMMENT', '{', '}',
103 pyyacc.EOF, 'val32', 'val8', 'val5', 'val3'] 103 pyyacc.EOF, 'val32', 'val16', 'val8', 'val5', 'val3']
104 tokens2.extend(kws) 104 tokens2.extend(kws)
105 self.kws = kws 105 self.kws = kws
106 g = pyyacc.Grammar(tokens2) 106 g = pyyacc.Grammar(tokens2)
107 self.g = g 107 self.g = g
108 # Global structure of assembly line: 108 # Global structure of assembly line:
130 g.add_production('factor', ['ID'], lambda name: ASymbol(name.val)) 130 g.add_production('factor', ['ID'], lambda name: ASymbol(name.val))
131 g.add_production('factor', ['NUMBER'], lambda num: ANumber(int(num.val))) 131 g.add_production('factor', ['NUMBER'], lambda num: ANumber(int(num.val)))
132 g.start_symbol = 'asmline' 132 g.start_symbol = 'asmline'
133 self.emit = emit 133 self.emit = emit
134 self.p = g.generate_parser() 134 self.p = g.generate_parser()
135 print('length of table:', len(self.p.action_table)) 135 # print('length of table:', len(self.p.action_table))
136 136
137 # Parser handlers: 137 # Parser handlers:
138 def p_ins_1(self, opc, ops): 138 def p_ins_1(self, opc, ops):
139 ins = AInstruction(opc, ops) 139 ins = AInstruction(opc, ops)
140 self.emit(ins) 140 self.emit(ins)