annotate python/libasm.py @ 196:ec2b423cdbea

Merge asm and asmlib files
author Windel Bouwman
date Sat, 01 Jun 2013 11:55:49 +0200
parents 37ac6c016e0f
children
rev   line source
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
1 import re
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
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
4 import sys, argparse
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
5
159
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
6
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
7 # Different instruction sets:
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
8 class InstructionSet:
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
9 pass
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
10
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
11 class X86(InstructionSet):
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
12 pass
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
13
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
14 # Generic assembler:
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
15 keywords = ['global', 'db']
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
16
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
17 def tokenize(s):
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
18 """
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
19 Tokenizer, generates an iterator that
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
20 returns tokens!
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
21
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
22 This GREAT example was taken from python re doc page!
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
23 """
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
24 tok_spec = [
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
25 ('REAL', r'\d+\.\d+'),
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
26 ('HEXNUMBER', r'0x[\da-fA-F]+'),
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
27 ('NUMBER', r'\d+'),
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
28 ('ID', r'[A-Za-z][A-Za-z\d_]*'),
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
29 ('SKIP', r'[ \t]'),
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
30 ('LEESTEKEN', r':=|[\.,=:\-+*\[\]/\(\)]|>=|<=|<>|>|<'),
159
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
31 ('STRING', r"'.*?'")
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
32 ]
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
33 tok_re = '|'.join('(?P<%s>%s)' % pair for pair in tok_spec)
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
34 gettok = re.compile(tok_re).match
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
35 line = 1
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
36 pos = line_start = 0
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
37 mo = gettok(s)
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
38 while mo is not None:
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
39 typ = mo.lastgroup
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
40 val = mo.group(typ)
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
41 if typ == 'NEWLINE':
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
42 line_start = pos
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
43 line += 1
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
44 elif typ == 'COMMENTS':
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
45 pass
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
46 elif typ != 'SKIP':
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
47 if typ == 'ID':
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
48 if val in keywords:
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
49 typ = val
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
50 elif typ == 'LEESTEKEN':
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
51 typ = val
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
52 elif typ == 'NUMBER':
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
53 val = int(val)
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
54 elif typ == 'HEXNUMBER':
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
55 val = int(val[2:], 16)
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
56 typ = 'NUMBER'
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
57 elif typ == 'REAL':
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
58 val = float(val)
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
59 elif typ == 'STRING':
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
60 val = val[1:-1]
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
61 col = mo.start() - line_start
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
62 loc = SourceLocation(line, col, 0) # TODO retrieve length?
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
63 yield Token(typ, val, loc)
159
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
64 pos = mo.end()
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
65 mo = gettok(s, pos)
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
66 if pos != len(s):
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
67 col = pos - line_start
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
68 loc = SourceLocation(line, col, 0)
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
69 raise CompilerError('Unexpected character {0}'.format(s[pos]), loc)
159
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
70
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
71 class Lexer:
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
72 def __init__(self, src):
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
73 self.tokens = tokenize(src)
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
74 self.curTok = self.tokens.__next__()
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
75 def eat(self):
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
76 t = self.curTok
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
77 self.curTok = self.tokens.__next__()
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
78 return t
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
79 @property
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
80 def Peak(self):
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
81 return self.curTok
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
82
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
83 class ANode:
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
84 def __eq__(self, other):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
85 return self.__repr__() == other.__repr__()
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
86
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
87 class ALabel(ANode):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
88 def __init__(self, name):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
89 self.name = name
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
90 def __repr__(self):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
91 return '{0}:'.format(self.name)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
92
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
93 class AInstruction(ANode):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
94 def __init__(self, opcode, operands):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
95 self.opcode = opcode
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
96 self.operands = operands
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
97 def __repr__(self):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
98 ops = ', '.join(map(str, self.operands))
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
99 return '{0} {1}'.format(self.opcode, ops)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
100
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
101 class AExpression(ANode):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
102 def __add__(self, other):
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
103 assert isinstance(other, AExpression)
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
104 return ABinop('+', self, other)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
105 def __mul__(self, other):
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
106 assert isinstance(other, AExpression)
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
107 return ABinop('*', self, other)
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
108
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
109 class ABinop(AExpression):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
110 def __init__(self, op, arg1, arg2):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
111 self.op = op
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
112 self.arg1 = arg1
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
113 self.arg2 = arg2
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
114 def __repr__(self):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
115 return '{0} {1} {2}'.format(self.op, self.arg1, self.arg2)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
116
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
117 class AUnop(AExpression):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
118 def __init__(self, op, arg):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
119 self.op = op
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
120 self.arg = arg
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
121 def __repr__(self):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
122 return '{0} {1}'.format(self.op, self.arg)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
123
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
124 class ASymbol(AExpression):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
125 def __init__(self, name):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
126 self.name = name
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
127 def __repr__(self):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
128 return self.name
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
129
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
130 class ANumber(AExpression):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
131 def __init__(self, n):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
132 self.n = n
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
133 def __repr__(self):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
134 return '{0}'.format(self.n)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
135
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
136 class Assembler:
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
137 def __init__(self):
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
138 self.output = []
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
139 # Construct a parser given a grammar:
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
140 ident = lambda x: x # Identity helper function
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
141 g = pyyacc.Grammar(['ID', 'NUMBER', ',', '[', ']', ':', '+', '-', '*', pyyacc.EPS])
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
142 g.add_production('asmline', ['label', 'instruction'])
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
143 g.add_production('asmline', ['instruction'])
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
144 g.add_production('asmline', ['label'])
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
145 g.add_production('asmline', [])
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
146 g.add_production('label', ['ID', ':'], self.p_label)
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
147 g.add_production('instruction', ['opcode', 'operands'], self.p_ins_1)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
148 g.add_production('instruction', ['opcode'], self.p_ins_2)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
149 g.add_production('opcode', ['ID'], ident)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
150 g.add_production('operands', ['operand'], self.p_operands_1)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
151 g.add_production('operands', ['operands', ',', 'operand'], self.p_operands_2)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
152 g.add_production('operand', ['expression'], ident)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
153 g.add_production('operand', ['[', 'expression', ']'], self.p_mem_op)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
154 g.add_production('expression', ['term'], ident)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
155 g.add_production('expression', ['expression', 'addop', 'term'], self.p_binop)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
156 g.add_production('addop', ['-'], ident)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
157 g.add_production('addop', ['+'], ident)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
158 g.add_production('mulop', ['*'], ident)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
159 g.add_production('term', ['factor'], ident)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
160 g.add_production('term', ['term', 'mulop', 'factor'], self.p_binop)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
161 g.add_production('factor', ['ID'], self.p_symbol)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
162 g.add_production('factor', ['NUMBER'], self.p_number)
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
163 g.start_symbol = 'asmline'
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
164 self.p = g.genParser()
159
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
165
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
166 # Parser handlers:
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
167 def p_ins_1(self, opc, ops):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
168 ins = AInstruction(opc, ops)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
169 self.emit(ins)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
170 def p_ins_2(self, opc):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
171 self.p_ins_1(opc, [])
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
172 def p_operands_1(self, op1):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
173 return [op1]
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
174 def p_operands_2(self, ops, comma, op2):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
175 assert type(ops) is list
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
176 ops.append(op2)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
177 return ops
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
178 def p_mem_op(self, brace_open, exp, brace_close):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
179 return AUnop('[]', exp)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
180 def handle_ins(self, id0, operands):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
181 ins = AInstruction(id0)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
182 self.emit(ins)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
183 def p_label(self, lname, cn):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
184 lab = ALabel(lname)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
185 self.emit(lab)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
186 def p_binop(self, exp1, op, exp2):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
187 return ABinop(op, exp1, exp2)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
188 def p_symbol(self, name):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
189 return ASymbol(name)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
190 def p_number(self, n):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
191 n = int(n)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
192 return ANumber(n)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
193
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
194 # Top level interface:
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
195 def emit(self, a):
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
196 """ Emit a parsed instruction """
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
197 self.output.append(a)
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
198 # Determine the bit pattern from a lookup table:
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
199 # TODO
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
200
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
201
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
202 def parse_line(self, line):
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
203 """ Parse line into asm AST """
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
204 tokens = tokenize(line)
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
205 self.p.parse(tokens)
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
206
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
207 def assemble(self, asmsrc):
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
208 """ Assemble this source snippet """
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
209 for line in asmsrc.split('\n'):
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
210 self.assemble_line(line)
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
211 self.back_patch()
159
5e1dd04cb61c Added attempt to assembler
Windel Bouwman
parents:
diff changeset
212
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
213 def assemble_line(self, line):
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
214 """
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
215 Assemble a single source line.
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
216 Do not take newlines into account
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
217 """
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
218 self.parse_line(line)
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
219 self.assemble_aast()
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
220
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
221 def assemble_aast(self, at):
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
222 """ Assemble a parsed asm line """
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
223 pass
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
224
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
225 def back_patch(self):
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
226 """ Fix references to earlier labels """
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
227 pass
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 159
diff changeset
228
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
229
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
230 if __name__ == '__main__':
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
231 # When run as main file, try to grab command line arguments:
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
232 parser = argparse.ArgumentParser(description="Assembler")
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
233 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
234 args = parser.parse_args()
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
235 a = Assembler()
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
236 obj = a.assemble(args.sourcefile.read())
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
237