annotate python/ppci/assembler.py @ 337:b00219172a42

Added cool lm3s811 qemu project
author Windel Bouwman
date Thu, 20 Feb 2014 20:04:52 +0100
parents d1ecc493384e
children c7cc54c0dfdf
rev   line source
334
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
1
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
2 import re
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
3 import pyyacc
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
4 from . import Token, CompilerError, SourceLocation
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
5 from target import Target, Label
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
6 from .asmnodes import ALabel, AInstruction, ABinop, AUnop, ASymbol, ANumber
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
7
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
8 def tokenize(s):
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
9 """
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
10 Tokenizer, generates an iterator that
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
11 returns tokens!
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
12
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
13 This GREAT example was taken from python re doc page!
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
14 """
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
15 tok_spec = [
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
16 ('REAL', r'\d+\.\d+'),
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
17 ('HEXNUMBER', r'0x[\da-fA-F]+'),
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
18 ('NUMBER', r'\d+'),
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
19 ('ID', r'[A-Za-z][A-Za-z\d_]*'),
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
20 ('SKIP', r'[ \t]'),
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
21 ('LEESTEKEN', r':=|[\.,=:\-+*\[\]/\(\)]|>=|<=|<>|>|<|}|{'),
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
22 ('STRING', r"'.*?'"),
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
23 ('COMMENT', r";.*")
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
24 ]
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
25 tok_re = '|'.join('(?P<%s>%s)' % pair for pair in tok_spec)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
26 gettok = re.compile(tok_re).match
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
27 line = 1
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
28 pos = line_start = 0
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
29 mo = gettok(s)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
30 while mo is not None:
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
31 typ = mo.lastgroup
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
32 val = mo.group(typ)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
33 if typ == 'NEWLINE':
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
34 line_start = pos
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
35 line += 1
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
36 elif typ != 'SKIP':
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
37 if typ == 'LEESTEKEN':
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
38 typ = val
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
39 elif typ == 'NUMBER':
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
40 val = int(val)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
41 elif typ == 'HEXNUMBER':
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
42 val = int(val[2:], 16)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
43 typ = 'NUMBER'
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
44 elif typ == 'REAL':
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
45 val = float(val)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
46 elif typ == 'STRING':
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
47 val = val[1:-1]
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
48 col = mo.start() - line_start
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
49 loc = SourceLocation('', line, col, 0) # TODO retrieve length?
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
50 yield Token(typ, val, loc)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
51 pos = mo.end()
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
52 mo = gettok(s, pos)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
53 if pos != len(s):
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
54 col = pos - line_start
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
55 loc = SourceLocation('', line, col, 0)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
56 raise CompilerError('Unexpected character {0}'.format(s[pos]), loc)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
57 yield Token('EOF', pyyacc.EOF)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
58
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
59
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
60 class Lexer:
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
61 def __init__(self, src):
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
62 self.tokens = tokenize(src)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
63 self.curTok = self.tokens.__next__()
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
64
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
65 def next_token(self):
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
66 t = self.curTok
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
67 if t.typ != 'EOF':
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
68 self.curTok = self.tokens.__next__()
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
69 return t
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
70
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
71
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
72 class Parser:
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
73 def __init__(self):
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
74 # Construct a parser given a grammar:
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
75 ident = lambda x: x # Identity helper function
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
76 g = pyyacc.Grammar(['ID', 'NUMBER', ',', '[', ']', ':', '+', '-', '*', pyyacc.EPS, 'COMMENT', '{', '}',
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
77 pyyacc.EOF])
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
78 g.add_production('asmline', ['asmline2'])
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
79 g.add_production('asmline', ['asmline2', 'COMMENT'])
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
80 g.add_production('asmline2', ['label', 'instruction'])
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
81 g.add_production('asmline2', ['instruction'])
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
82 g.add_production('asmline2', ['label'])
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
83 g.add_production('asmline2', [])
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
84 g.add_production('label', ['ID', ':'], self.p_label)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
85 #g.add_production('label', [])
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
86 g.add_production('instruction', ['opcode', 'operands'], self.p_ins_1)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
87 g.add_production('instruction', ['opcode'], self.p_ins_2)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
88 #g.add_production('instruction', [])
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
89 g.add_production('opcode', ['ID'], lambda x: x.val)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
90 g.add_production('operands', ['operand'], self.p_operands_1)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
91 g.add_production('operands', ['operands', ',', 'operand'], self.p_operands_2)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
92 g.add_production('operand', ['expression'], ident)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
93 g.add_production('operand', ['[', 'expression', ']'], self.p_mem_op)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
94 g.add_production('operand', ['{', 'listitems', '}'], self.p_list_op)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
95 g.add_production('listitems', ['expression'], self.p_listitems_1)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
96 g.add_production('listitems', ['listitems', ',', 'expression'], self.p_listitems_2)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
97 g.add_production('expression', ['term'], ident)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
98 g.add_production('expression', ['expression', 'addop', 'term'], self.p_binop)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
99 g.add_production('addop', ['-'], lambda x: x.val)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
100 g.add_production('addop', ['+'], lambda x: x.val)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
101 g.add_production('mulop', ['*'], lambda x: x.val)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
102 g.add_production('term', ['factor'], ident)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
103 g.add_production('term', ['term', 'mulop', 'factor'], self.p_binop)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
104 g.add_production('factor', ['ID'], lambda name: ASymbol(name.val))
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
105 g.add_production('factor', ['NUMBER'], lambda num: ANumber(int(num.val)))
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
106 g.start_symbol = 'asmline'
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
107 self.p = g.genParser()
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
108
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
109 # Parser handlers:
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
110 def p_ins_1(self, opc, ops):
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
111 ins = AInstruction(opc, ops)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
112 self.emit(ins)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
113
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
114 def p_ins_2(self, opc):
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
115 self.p_ins_1(opc, [])
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
116
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
117 def p_operands_1(self, op1):
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
118 return [op1]
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
119
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
120 def p_operands_2(self, ops, comma, op2):
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
121 assert type(ops) is list
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
122 ops.append(op2)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
123 return ops
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
124
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
125 def p_listitems_1(self, li1):
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
126 return [li1]
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
127
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
128 def p_listitems_2(self, lis, comma, li2):
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
129 assert type(lis) is list
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
130 lis.append(li2)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
131 return lis
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
132
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
133 def p_list_op(self, brace_open, lst, brace_close):
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
134 return AUnop('{}', lst)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
135
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
136 def p_mem_op(self, brace_open, exp, brace_close):
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
137 return AUnop('[]', exp)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
138
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
139 def p_label(self, lname, cn):
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
140 lab = ALabel(lname.val)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
141 self.emit(lab)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
142
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
143 def p_binop(self, exp1, op, exp2):
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
144 return ABinop(op, exp1, exp2)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
145
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
146 def parse(self, lexer, emitter):
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
147 self.emit = emitter
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
148 self.p.parse(lexer)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
149
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
150 # Pre construct parser to save time:
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
151 asmParser = Parser()
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
152
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
153 class Assembler:
336
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 334
diff changeset
154 def __init__(self, target, stream):
334
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
155 self.target = target
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 336
diff changeset
156 assert isinstance(target,Target)
334
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
157 self.stream = stream
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
158 self.restart()
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
159 self.p = asmParser
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
160
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
161 # Top level interface:
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
162 def restart(self):
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
163 self.stack = []
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
164
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
165 def emit(self, a):
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
166 """ Emit a parsed instruction """
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
167 self.stack.append(a)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
168
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
169 def parse_line(self, line):
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
170 """ Parse line into asm AST """
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
171 tokens = Lexer(line)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
172 self.p.parse(tokens, self.emit)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
173
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
174 def assemble(self, asmsrc):
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
175 """ Assemble this source snippet """
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
176 if type(asmsrc) is not str:
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
177 asmsrc2 = asmsrc.read()
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
178 asmsrc.close()
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
179 asmsrc = asmsrc2
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
180 for line in asmsrc.split('\n'):
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
181 self.assemble_line(line)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
182
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
183 def assemble_line(self, line):
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
184 """
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
185 Assemble a single source line.
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
186 Do not take newlines into account
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
187 """
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
188 self.parse_line(line)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
189 self.assemble_aast()
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
190
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
191 def assemble_aast(self):
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
192 """ Assemble a parsed asm line """
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
193 while self.stack:
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
194 vi = self.stack.pop(0)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
195 if type(vi) is AInstruction:
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
196 mi = self.target.mapInstruction(vi)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
197 elif type(vi) is ALabel:
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
198 mi = Label(vi.name)
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
199 else:
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
200 raise NotImplementedError('{}'.format(vi))
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
201 if self.stream:
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
202 self.stream.emit(mi)