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