annotate python/asm.py @ 318:e84047f29c78

Add burg and yacc initial attempts
author Windel Bouwman
date Tue, 31 Dec 2013 12:38:15 +0100
parents 084cccaa5deb
children 8d07a4254f04
rev   line source
303
be7f60545368 Final fixups
Windel Bouwman
parents: 287
diff changeset
1 #!/usr/bin/env python3
be7f60545368 Final fixups
Windel Bouwman
parents: 287
diff changeset
2
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
3 import re
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
4 import argparse
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
5 import pyyacc
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
6 from ppci import Token, CompilerError, SourceLocation
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
7 from target import Target, Label
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
8 from asmnodes import ALabel, AInstruction, ABinop, AUnop, ASymbol, ANumber
159
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
9
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
10 def tokenize(s):
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
11 """
159
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
12 Tokenizer, generates an iterator that
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
13 returns tokens!
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
14
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
15 This GREAT example was taken from python re doc page!
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
16 """
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
17 tok_spec = [
159
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
18 ('REAL', r'\d+\.\d+'),
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
19 ('HEXNUMBER', r'0x[\da-fA-F]+'),
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
20 ('NUMBER', r'\d+'),
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
21 ('ID', r'[A-Za-z][A-Za-z\d_]*'),
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
22 ('SKIP', r'[ \t]'),
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 203
diff changeset
23 ('LEESTEKEN', r':=|[\.,=:\-+*\[\]/\(\)]|>=|<=|<>|>|<|}|{'),
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 197
diff changeset
24 ('STRING', r"'.*?'"),
33d50727a23c Fixup testscript
Windel Bouwman
parents: 197
diff changeset
25 ('COMMENT', r";.*")
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
26 ]
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
27 tok_re = '|'.join('(?P<%s>%s)' % pair for pair in tok_spec)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
28 gettok = re.compile(tok_re).match
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
29 line = 1
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
30 pos = line_start = 0
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
31 mo = gettok(s)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
32 while mo is not None:
159
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
33 typ = mo.lastgroup
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
34 val = mo.group(typ)
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
35 if typ == 'NEWLINE':
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
36 line_start = pos
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
37 line += 1
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
38 elif typ != 'SKIP':
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
39 if typ == 'LEESTEKEN':
159
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
40 typ = val
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
41 elif typ == 'NUMBER':
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
42 val = int(val)
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
43 elif typ == 'HEXNUMBER':
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
44 val = int(val[2:], 16)
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
45 typ = 'NUMBER'
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
46 elif typ == 'REAL':
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
47 val = float(val)
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
48 elif typ == 'STRING':
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
49 val = val[1:-1]
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
50 col = mo.start() - line_start
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 236
diff changeset
51 loc = SourceLocation('', line, col, 0) # TODO retrieve length?
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
52 yield Token(typ, val, loc)
159
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
53 pos = mo.end()
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
54 mo = gettok(s, pos)
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
55 if pos != len(s):
159
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
56 col = pos - line_start
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 236
diff changeset
57 loc = SourceLocation('', line, col, 0)
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
58 raise CompilerError('Unexpected character {0}'.format(s[pos]), loc)
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
59 yield Token('EOF', pyyacc.EOF)
159
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
60
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 236
diff changeset
61
159
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
62 class Lexer:
315
084cccaa5deb Added console and screen
Windel Bouwman
parents: 303
diff changeset
63 def __init__(self, src):
084cccaa5deb Added console and screen
Windel Bouwman
parents: 303
diff changeset
64 self.tokens = tokenize(src)
084cccaa5deb Added console and screen
Windel Bouwman
parents: 303
diff changeset
65 self.curTok = self.tokens.__next__()
084cccaa5deb Added console and screen
Windel Bouwman
parents: 303
diff changeset
66
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
67 def next_token(self):
315
084cccaa5deb Added console and screen
Windel Bouwman
parents: 303
diff changeset
68 t = self.curTok
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
69 if t.typ != 'EOF':
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
70 self.curTok = self.tokens.__next__()
315
084cccaa5deb Added console and screen
Windel Bouwman
parents: 303
diff changeset
71 return t
084cccaa5deb Added console and screen
Windel Bouwman
parents: 303
diff changeset
72
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 236
diff changeset
73
218
494828a7adf1 added some sort of cache to assembler
Windel Bouwman
parents: 213
diff changeset
74 class Parser:
494828a7adf1 added some sort of cache to assembler
Windel Bouwman
parents: 213
diff changeset
75 def __init__(self):
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
76 # Construct a parser given a grammar:
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
77 ident = lambda x: x # Identity helper function
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
78 g = pyyacc.Grammar(['ID', 'NUMBER', ',', '[', ']', ':', '+', '-', '*', pyyacc.EPS, 'COMMENT', '{', '}',
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
79 pyyacc.EOF])
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 197
diff changeset
80 g.add_production('asmline', ['asmline2'])
33d50727a23c Fixup testscript
Windel Bouwman
parents: 197
diff changeset
81 g.add_production('asmline', ['asmline2', 'COMMENT'])
33d50727a23c Fixup testscript
Windel Bouwman
parents: 197
diff changeset
82 g.add_production('asmline2', ['label', 'instruction'])
33d50727a23c Fixup testscript
Windel Bouwman
parents: 197
diff changeset
83 g.add_production('asmline2', ['instruction'])
33d50727a23c Fixup testscript
Windel Bouwman
parents: 197
diff changeset
84 g.add_production('asmline2', ['label'])
33d50727a23c Fixup testscript
Windel Bouwman
parents: 197
diff changeset
85 g.add_production('asmline2', [])
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
86 g.add_production('label', ['ID', ':'], self.p_label)
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
87 g.add_production('instruction', ['opcode', 'operands'], self.p_ins_1)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
88 g.add_production('instruction', ['opcode'], self.p_ins_2)
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
89 g.add_production('opcode', ['ID'], lambda x: x.val)
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
90 g.add_production('operands', ['operand'], self.p_operands_1)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
91 g.add_production('operands', ['operands', ',', 'operand'], self.p_operands_2)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
92 g.add_production('operand', ['expression'], ident)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
93 g.add_production('operand', ['[', 'expression', ']'], self.p_mem_op)
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 203
diff changeset
94 g.add_production('operand', ['{', 'listitems', '}'], self.p_list_op)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 203
diff changeset
95 g.add_production('listitems', ['expression'], self.p_listitems_1)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 203
diff changeset
96 g.add_production('listitems', ['listitems', ',', 'expression'], self.p_listitems_2)
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
97 g.add_production('expression', ['term'], ident)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
98 g.add_production('expression', ['expression', 'addop', 'term'], self.p_binop)
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
99 g.add_production('addop', ['-'], lambda x: x.val)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
100 g.add_production('addop', ['+'], lambda x: x.val)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
101 g.add_production('mulop', ['*'], lambda x: x.val)
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
102 g.add_production('term', ['factor'], ident)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
103 g.add_production('term', ['term', 'mulop', 'factor'], self.p_binop)
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
104 g.add_production('factor', ['ID'], lambda name: ASymbol(name.val))
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
105 g.add_production('factor', ['NUMBER'], lambda num: ANumber(int(num.val)))
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
106 g.start_symbol = 'asmline'
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
107 self.p = g.genParser()
159
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
108
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
109 # Parser handlers:
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
110 def p_ins_1(self, opc, ops):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
111 ins = AInstruction(opc, ops)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
112 self.emit(ins)
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
113
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
114 def p_ins_2(self, opc):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
115 self.p_ins_1(opc, [])
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
116
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
117 def p_operands_1(self, op1):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
118 return [op1]
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
119
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
120 def p_operands_2(self, ops, comma, op2):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
121 assert type(ops) is list
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
122 ops.append(op2)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
123 return ops
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 203
diff changeset
124
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 203
diff changeset
125 def p_listitems_1(self, li1):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 203
diff changeset
126 return [li1]
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 203
diff changeset
127
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 203
diff changeset
128 def p_listitems_2(self, lis, comma, li2):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 203
diff changeset
129 assert type(lis) is list
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 203
diff changeset
130 lis.append(li2)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 203
diff changeset
131 return lis
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 203
diff changeset
132
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 203
diff changeset
133 def p_list_op(self, brace_open, lst, brace_close):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 203
diff changeset
134 return AUnop('{}', lst)
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
135
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
136 def p_mem_op(self, brace_open, exp, brace_close):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
137 return AUnop('[]', exp)
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
138
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
139 def p_label(self, lname, cn):
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
140 lab = ALabel(lname.val)
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
141 self.emit(lab)
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
142
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
143 def p_binop(self, exp1, op, exp2):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
144 return ABinop(op, exp1, exp2)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
145
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
146 def parse(self, lexer, emitter):
218
494828a7adf1 added some sort of cache to assembler
Windel Bouwman
parents: 213
diff changeset
147 self.emit = emitter
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
148 self.p.parse(lexer)
218
494828a7adf1 added some sort of cache to assembler
Windel Bouwman
parents: 213
diff changeset
149
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
150 # Pre construct parser to save time:
218
494828a7adf1 added some sort of cache to assembler
Windel Bouwman
parents: 213
diff changeset
151 asmParser = Parser()
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
152
218
494828a7adf1 added some sort of cache to assembler
Windel Bouwman
parents: 213
diff changeset
153 class Assembler:
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
154 def __init__(self, target=None, stream=None):
218
494828a7adf1 added some sort of cache to assembler
Windel Bouwman
parents: 213
diff changeset
155 self.target = target
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
156 self.stream = stream
218
494828a7adf1 added some sort of cache to assembler
Windel Bouwman
parents: 213
diff changeset
157 self.restart()
494828a7adf1 added some sort of cache to assembler
Windel Bouwman
parents: 213
diff changeset
158 self.p = asmParser
494828a7adf1 added some sort of cache to assembler
Windel Bouwman
parents: 213
diff changeset
159
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
160 # Top level interface:
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
161 def restart(self):
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
162 self.stack = []
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
163
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
164 def emit(self, a):
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
165 """ Emit a parsed instruction """
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
166 self.stack.append(a)
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
167
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
168 def parse_line(self, line):
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
169 """ Parse line into asm AST """
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
170 tokens = Lexer(line)
218
494828a7adf1 added some sort of cache to assembler
Windel Bouwman
parents: 213
diff changeset
171 self.p.parse(tokens, self.emit)
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
172
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
173 def assemble(self, asmsrc):
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
174 """ Assemble this source snippet """
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
175 for line in asmsrc.split('\n'):
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
176 self.assemble_line(line)
159
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
177
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
178 def assemble_line(self, line):
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
179 """
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
180 Assemble a single source line.
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
181 Do not take newlines into account
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
182 """
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
183 self.parse_line(line)
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
184 self.assemble_aast()
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
185
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 197
diff changeset
186 def assemble_aast(self):
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
187 """ Assemble a parsed asm line """
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
188 if not self.target:
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
189 raise CompilerError('Cannot assemble without target')
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
190 while self.stack:
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
191 vi = self.stack.pop(0)
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 200
diff changeset
192 if type(vi) is AInstruction:
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
193 mi = self.target.mapInstruction(vi)
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
194 elif type(vi) is ALabel:
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
195 mi = Label(vi.name)
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
196 else:
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
197 raise NotImplementedError('{}'.format(vi))
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
198 if self.stream:
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
199 self.stream.emit(mi)
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
200
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
201
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
202 if __name__ == '__main__':
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
203 # When run as main file, try to grab command line arguments:
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
204 parser = argparse.ArgumentParser(description="Assembler")
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
205 parser.add_argument('sourcefile', type=argparse.FileType('r'),
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 315
diff changeset
206 help='the source file to assemble')
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
207 args = parser.parse_args()
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
208 a = Assembler()
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
209 obj = a.assemble(args.sourcefile.read())