annotate python/asm.py @ 203:ca1ea402f6a1

Added some arm instructions
author Windel Bouwman
date Sat, 15 Jun 2013 19:13:05 +0200
parents 5e391d9a3381
children 6c6bf8890d8a
rev   line source
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 197
diff changeset
1 import re, sys, argparse
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
2 import pyyacc
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
3 from ppci import Token, CompilerError, SourceLocation
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
4 from target import Target
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
5 from asmnodes import ALabel, AInstruction, ABinop, AUnop, ASymbol, ANumber
159
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
6
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
7 def tokenize(s):
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
8 """
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
9 Tokenizer, generates an iterator that
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
10 returns tokens!
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
11
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
12 This GREAT example was taken from python re doc page!
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
13 """
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
14 tok_spec = [
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
15 ('REAL', r'\d+\.\d+'),
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
16 ('HEXNUMBER', r'0x[\da-fA-F]+'),
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
17 ('NUMBER', r'\d+'),
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
18 ('ID', r'[A-Za-z][A-Za-z\d_]*'),
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
19 ('SKIP', r'[ \t]'),
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
20 ('LEESTEKEN', r':=|[\.,=:\-+*\[\]/\(\)]|>=|<=|<>|>|<'),
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 197
diff changeset
21 ('STRING', r"'.*?'"),
33d50727a23c Fixup testscript
Windel Bouwman
parents: 197
diff changeset
22 ('COMMENT', r";.*")
159
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
23 ]
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
24 tok_re = '|'.join('(?P<%s>%s)' % pair for pair in tok_spec)
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
25 gettok = re.compile(tok_re).match
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
26 line = 1
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
27 pos = line_start = 0
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
28 mo = gettok(s)
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
29 while mo is not None:
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
30 typ = mo.lastgroup
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
31 val = mo.group(typ)
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
32 if typ == 'NEWLINE':
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
33 line_start = pos
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
34 line += 1
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
35 elif typ != 'SKIP':
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
36 if typ == 'LEESTEKEN':
159
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
37 typ = val
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
38 elif typ == 'NUMBER':
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
39 val = int(val)
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
40 elif typ == 'HEXNUMBER':
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
41 val = int(val[2:], 16)
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
42 typ = 'NUMBER'
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
43 elif typ == 'REAL':
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
44 val = float(val)
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
45 elif typ == 'STRING':
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
46 val = val[1:-1]
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
47 col = mo.start() - line_start
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
48 loc = SourceLocation(line, col, 0) # TODO retrieve length?
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
49 yield Token(typ, val, loc)
159
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
50 pos = mo.end()
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
51 mo = gettok(s, pos)
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
52 if pos != len(s):
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
53 col = pos - line_start
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
54 loc = SourceLocation(line, col, 0)
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
55 raise CompilerError('Unexpected character {0}'.format(s[pos]), loc)
159
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
56
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
57 class Lexer:
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
58 def __init__(self, src):
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
59 self.tokens = tokenize(src)
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
60 self.curTok = self.tokens.__next__()
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
61 def eat(self):
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
62 t = self.curTok
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
63 self.curTok = self.tokens.__next__()
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
64 return t
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
65 @property
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
66 def Peak(self):
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
67 return self.curTok
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
68
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
69
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
70 class Assembler:
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
71 def __init__(self, target=None):
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
72 self.target = target
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
73 self.restart()
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
74 # Construct a parser given a grammar:
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
75 ident = lambda x: x # Identity helper function
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 197
diff changeset
76 g = pyyacc.Grammar(['ID', 'NUMBER', ',', '[', ']', ':', '+', '-', '*', pyyacc.EPS, 'COMMENT'])
33d50727a23c Fixup testscript
Windel Bouwman
parents: 197
diff changeset
77 g.add_production('asmline', ['asmline2'])
33d50727a23c Fixup testscript
Windel Bouwman
parents: 197
diff changeset
78 g.add_production('asmline', ['asmline2', 'COMMENT'])
33d50727a23c Fixup testscript
Windel Bouwman
parents: 197
diff changeset
79 g.add_production('asmline2', ['label', 'instruction'])
33d50727a23c Fixup testscript
Windel Bouwman
parents: 197
diff changeset
80 g.add_production('asmline2', ['instruction'])
33d50727a23c Fixup testscript
Windel Bouwman
parents: 197
diff changeset
81 g.add_production('asmline2', ['label'])
33d50727a23c Fixup testscript
Windel Bouwman
parents: 197
diff changeset
82 g.add_production('asmline2', [])
33d50727a23c Fixup testscript
Windel Bouwman
parents: 197
diff changeset
83 g.add_production('optcomment', [])
33d50727a23c Fixup testscript
Windel Bouwman
parents: 197
diff changeset
84 g.add_production('optcomment', ['COMMENT'])
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
85 g.add_production('label', ['ID', ':'], self.p_label)
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
86 g.add_production('instruction', ['opcode', 'operands'], self.p_ins_1)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
87 g.add_production('instruction', ['opcode'], self.p_ins_2)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
88 g.add_production('opcode', ['ID'], ident)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
89 g.add_production('operands', ['operand'], self.p_operands_1)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
90 g.add_production('operands', ['operands', ',', 'operand'], self.p_operands_2)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
91 g.add_production('operand', ['expression'], ident)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
92 g.add_production('operand', ['[', 'expression', ']'], self.p_mem_op)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
93 g.add_production('expression', ['term'], ident)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
94 g.add_production('expression', ['expression', 'addop', 'term'], self.p_binop)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
95 g.add_production('addop', ['-'], ident)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
96 g.add_production('addop', ['+'], ident)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
97 g.add_production('mulop', ['*'], ident)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
98 g.add_production('term', ['factor'], ident)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
99 g.add_production('term', ['term', 'mulop', 'factor'], self.p_binop)
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
100 g.add_production('factor', ['ID'], lambda name: ASymbol(name))
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
101 g.add_production('factor', ['NUMBER'], lambda num: ANumber(int(num)))
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
102 g.start_symbol = 'asmline'
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
103 self.p = g.genParser()
159
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
104
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
105 # Parser handlers:
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
106 def p_ins_1(self, opc, ops):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
107 ins = AInstruction(opc, ops)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
108 self.emit(ins)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
109 def p_ins_2(self, opc):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
110 self.p_ins_1(opc, [])
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
111 def p_operands_1(self, op1):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
112 return [op1]
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
113 def p_operands_2(self, ops, comma, op2):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
114 assert type(ops) is list
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
115 ops.append(op2)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
116 return ops
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
117 def p_mem_op(self, brace_open, exp, brace_close):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
118 return AUnop('[]', exp)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
119 def p_label(self, lname, cn):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
120 lab = ALabel(lname)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
121 self.emit(lab)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
122 def p_binop(self, exp1, op, exp2):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
123 return ABinop(op, exp1, exp2)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
124
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
125 # Top level interface:
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
126 def restart(self):
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
127 self.output = []
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
128 self.binout = bytearray()
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
129 self.current_section = '.text'
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
130
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
131 def emit(self, a):
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
132 """ Emit a parsed instruction """
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
133 self.output.append(a)
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
134 # Determine the bit pattern from a lookup table:
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
135 # TODO
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
136
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
137 def parse_line(self, line):
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
138 """ Parse line into asm AST """
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
139 tokens = tokenize(line)
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
140 self.p.parse(tokens)
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
141
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
142 def assemble(self, asmsrc):
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
143 """ Assemble this source snippet """
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
144 for line in asmsrc.split('\n'):
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
145 self.assemble_line(line)
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
146 self.back_patch()
159
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
147
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
148 def assemble_line(self, line):
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
149 """
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
150 Assemble a single source line.
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
151 Do not take newlines into account
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
152 """
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
153 self.parse_line(line)
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
154 self.assemble_aast()
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
155
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 197
diff changeset
156 def assemble_aast(self):
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
157 """ Assemble a parsed asm line """
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
158 # TODO
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
159 if not self.target:
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
160 raise CompilerError('Cannot assemble without target')
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
161 while self.output:
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
162 vi = self.output.pop(0)
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 200
diff changeset
163 if type(vi) is AInstruction:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 200
diff changeset
164 ri = self.target.mapInstruction(vi)
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 200
diff changeset
165 b = ri.encode()
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 200
diff changeset
166 assert type(b) is bytes
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 200
diff changeset
167 self.binout.extend(b)
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
168
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
169 def back_patch(self):
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
170 """ Fix references to earlier labels """
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
171 pass
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
172
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
173
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
174 if __name__ == '__main__':
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
175 # When run as main file, try to grab command line arguments:
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
176 parser = argparse.ArgumentParser(description="Assembler")
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
177 parser.add_argument('sourcefile', type=argparse.FileType('r'), help='the source file to assemble')
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
178 args = parser.parse_args()
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
179 a = Assembler()
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
180 obj = a.assemble(args.sourcefile.read())
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
181