annotate python/codegenarm.py @ 268:5ec7580976d9

Op naar tree-IR
author Windel Bouwman
date Wed, 14 Aug 2013 20:12:40 +0200
parents ed14e077124c
children 5f8c04a8d26b
rev   line source
255
7416c923a02a Added more logging
Windel Bouwman
parents: 250
diff changeset
1 import logging
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
2 import ir
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
3 from target import Label, Comment, Alignment, LabelRef, Imm32, DebugInfo
218
494828a7adf1 added some sort of cache to assembler
Windel Bouwman
parents: 212
diff changeset
4 import cortexm3 as arm
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
5 from ppci import CompilerError
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
6 import irmach
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
7
262
ed14e077124c Added conditional branch instructions
Windel Bouwman
parents: 261
diff changeset
8
ed14e077124c Added conditional branch instructions
Windel Bouwman
parents: 261
diff changeset
9 class InstructionSelector:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
10 def newTmp(self):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
11 return 't999'
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
12
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
13 def munchProgram(self, p):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
14 assert isinstance(p, ir.Module)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
15 self.result = []
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
16 for f in p.Functions:
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
17 for bb in f.BasicBlocks:
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
18 for i in bb.Instructions:
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
19 self.munchStm(i)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
20 return self.result
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
21
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
22 def emit(self, *args, **kwargs):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
23 """ Abstract instruction emitter """
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
24 i = irmach.AbstractInstruction(*args, **kwargs)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
25 self.result.append(i)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
26
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
27 def munchStm(self, s):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
28 raise NotImplementedError()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
29
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
30 def munchExpr(self, e):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
31 raise NotImplementedError()
262
ed14e077124c Added conditional branch instructions
Windel Bouwman
parents: 261
diff changeset
32
ed14e077124c Added conditional branch instructions
Windel Bouwman
parents: 261
diff changeset
33
ed14e077124c Added conditional branch instructions
Windel Bouwman
parents: 261
diff changeset
34 class RegisterAllocator:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
35 """ Target independent register allocator """
262
ed14e077124c Added conditional branch instructions
Windel Bouwman
parents: 261
diff changeset
36 pass
ed14e077124c Added conditional branch instructions
Windel Bouwman
parents: 261
diff changeset
37
ed14e077124c Added conditional branch instructions
Windel Bouwman
parents: 261
diff changeset
38
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
39 class ArmInstructionSelector(InstructionSelector):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
40 def munchExpr(self, e):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
41 if isinstance(e, ir.Alloc):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
42 return 0
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
43 elif isinstance(e, ir.Binop) and e.operation == '+':
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
44 a = self.munchExpr(e.value1)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
45 b = self.munchExpr(e.value2)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
46 d = self.newTmp()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
47 self.emit('add %d0, %s0, %s1', dst=[d], src=[a, b])
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
48 return d
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
49 elif isinstance(e, ir.Binop) and e.operation == '|':
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
50 a = self.munchExpr(e.value1)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
51 b = self.munchExpr(e.value2)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
52 d = self.newTmp()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
53 self.emit('orrrr %d0, %s0, %s1', dst=[d], src=[a, b])
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
54 return d
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
55 elif isinstance(e, ir.Binop) and e.operation == '<<':
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
56 a = self.munchExpr(e.value1)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
57 b = self.munchExpr(e.value2)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
58 d = self.newTmp()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
59 self.emit('lsl %d0, %s0, %s1', dst=[d], src=[a, b])
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
60 return d
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
61 elif isinstance(e, ir.Binop) and e.operation == '*':
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
62 a = self.munchExpr(e.value1)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
63 b = self.munchExpr(e.value2)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
64 d = self.newTmp()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
65 self.emit('mylll %d0, %s0, %s1', dst=[d], src=[a, b])
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
66 return d
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
67 elif isinstance(e, ir.Const):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
68 d = self.newTmp()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
69 if e.value < 256:
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
70 self.emit('ldr %d0, {}'.format(e.value), dst=[d])
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
71 else:
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
72 self.emit('ldrpcrel TODO')
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
73 return d
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
74 elif isinstance(e, ir.Mem):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
75 # Load from memory
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
76 loc = self.munchExpr(e.e)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
77 d = self.newTmp()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
78 self.emit('ldr %d0, [%s0]', src=[loc], dst=[d])
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
79 return d
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
80 elif isinstance(e, ir.Temp):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
81 return e
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
82 else:
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
83 raise NotImplementedError('--> {}'.format(e))
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
84
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
85 def munchStm(self, s):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
86 if isinstance(s, ir.Move) and isinstance(s.dst, ir.Mem):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
87 memloc = self.munchExpr(s.dst.e)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
88 val = self.munchExpr(s.src)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
89 self.emit('str [%s0], %s1')
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
90 elif isinstance(s, ir.Move) and isinstance(s.dst, ir.Temp):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
91 val = self.munchExpr(s.src)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
92 self.emit('str %d0, %s0', dst=[s.dst], src=[val])
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
93 elif isinstance(s, ir.Return):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
94 self.emit('ret')
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
95 elif isinstance(s, ir.Jump):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
96 self.emit('jmp {}'.format(s))
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
97 elif isinstance(s, ir.CJump):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
98 self.munchExpr(s.a)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
99 self.munchExpr(s.b)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
100 self.emit('jmp {}'.format(s))
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
101 else:
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
102 raise NotImplementedError('--> {}'.format(s))
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
103
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
104
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
105 class ArmCodeGenerator:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
106 def __init__(self, outs):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
107 self.ins_sel = ArmInstructionSelector()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
108 self.outs = outs
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
109 self.outs.getSection('code').address = 0x08000000
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
110 self.outs.getSection('data').address = 0x20000000
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
111
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
112 def generate(self, ircode):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
113 self.ins_sel.munchProgram(ircode)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
114
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
115
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
116 class ArmCodeGenerator_old:
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
117 """
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
118 Simple code generator
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
119 Ad hoc implementation
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
120 """
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
121 def __init__(self, out):
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
122 self.outs = out
258
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
123 self.logger = logging.getLogger('codegenarm')
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
124
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
125 def emit(self, item):
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
126 self.outs.emit(item)
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
127
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
128 def generate(self, ircode):
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
129 assert isinstance(ircode, ir.Module)
255
7416c923a02a Added more logging
Windel Bouwman
parents: 250
diff changeset
130 self.logger.info('Generating arm code for {}'.format(ircode.name))
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
131 self.available_regs = {arm.r3, arm.r4, arm.r5, arm.r6, arm.r7}
258
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
132 self.regmap = {}
250
f5fba5b554d7 Removal of obsolete editor
Windel Bouwman
parents: 249
diff changeset
133 # TODO: get these from linker descriptor?
f5fba5b554d7 Removal of obsolete editor
Windel Bouwman
parents: 249
diff changeset
134 self.outs.getSection('code').address = 0x08000000
f5fba5b554d7 Removal of obsolete editor
Windel Bouwman
parents: 249
diff changeset
135 self.outs.getSection('data').address = 0x20000000
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
136 self.outs.selectSection('data')
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
137
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
138 for gvar in ircode.Variables:
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
139 self.emit(Label(gvar.name))
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
140 # TODO: use initial value:
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
141 self.dcd(0)
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
142
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
143 self.imms = [] # list with immediates relative to PC.
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
144 self.outs.selectSection('code')
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
145
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
146 # Manually inserted startup code:
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
147 self.dcd(0x20000678) # initial stack ptr
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
148 # TODO: use label here:
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
149 #self.emit(arm.dcd_ins(LabelRef('reset'))) # reset vector
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
150 self.dcd(0x08000009) # reset vector, lsb indicates thumb mode
259
ac603eb66b63 Added function call
Windel Bouwman
parents: 258
diff changeset
151 self.emit(arm.bl_ins(LabelRef('main')))
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
152
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
153 self.emit(Label('reset'))
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
154 for f in ircode.Functions:
240
6259856841a0 Remove project
Windel Bouwman
parents: 237
diff changeset
155 self.localVars = []
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
156 # Add global variable addresses to immediate list:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
157 for gvar in ircode.Variables:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
158 pass #self.imms.append((
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
159
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
160 self.stack_frame = []
234
Windel Bouwman
parents: 232
diff changeset
161 self.emit(Label(f.name))
218
494828a7adf1 added some sort of cache to assembler
Windel Bouwman
parents: 212
diff changeset
162 # Save some registers:
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
163 self.emit(arm.push_ins(arm.RegisterSet({arm.r3, arm.r4, arm.r5, arm.r6,arm.r7,arm.lr})))
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
164 for bb in f.BasicBlocks:
234
Windel Bouwman
parents: 232
diff changeset
165 self.emit(Label(bb.name))
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
166 for ins in bb.Instructions:
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
167 self.generateInstruction(ins)
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
168
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
169 self.align()
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
170 while self.imms:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
171 l, v = self.imms.pop()
234
Windel Bouwman
parents: 232
diff changeset
172 self.emit(Label(l))
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
173 self.dcd(v)
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
174 self.align()
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
175 self.outs.backpatch()
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
176 self.outs.backpatch()
258
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
177 codesize = self.outs.getSection('code').Size
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
178 self.logger.info('Generated {} bytes code'.format(codesize))
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
179
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
180 def dcd(self, x):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
181 self.emit(arm.dcd_ins(Imm32(x)))
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
182
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
183 def align(self):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
184 self.outs.emit(Alignment(4))
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
185
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
186 # Helper functions:
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
187 def getStack(self, v):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
188 off = self.stack_frame.index(v)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
189 return off * 4
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
190
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
191 def addStack(self, v):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
192 self.stack_frame.append(v)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
193 return self.getStack(v)
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
194
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
195 def getGlobal(self, r, g):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
196 _global_address = g.name + '__global'
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
197 self.emit(arm.ldr_pcrel(r, LabelRef(_global_address)))
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
198
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
199 def loadStack(self, reg, val):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
200 self.emit(arm.ldr_sprel(reg, arm.MemSpRel(self.getStack(val))))
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
201
258
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
202 def getreg(self, v):
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
203 if not v in self.regmap:
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
204 self.regmap[v] = self.available_regs.pop()
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
205 return self.regmap[v]
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
206
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
207 def freereg(self, v, ins):
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
208 if v.lastUse(ins):
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
209 r = self.regmap.pop(v)
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
210 assert r not in self.regmap.values()
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
211 self.available_regs.add(r)
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
212
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
213 def comment(self, txt):
234
Windel Bouwman
parents: 232
diff changeset
214 self.emit(Comment(txt))
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
215
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
216 def debugInfo(self, loc):
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
217 if loc:
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
218 self.emit(DebugInfo(loc))
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
219
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
220 def generateInstruction(self, ins):
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
221 self.comment(str(ins))
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
222 if hasattr(ins, 'debugLoc'):
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
223 self.debugInfo(ins.debugLoc)
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
224 if type(ins) is ir.Branch:
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
225 tgt = LabelRef(ins.target.name)
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
226 self.emit(arm.b_ins(tgt))
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
227 elif type(ins) is ir.ImmLoad:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
228 lname = ins.target.name + '_ivalue'
258
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
229 r0 = self.getreg(ins.target)
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
230 self.emit(arm.ldr_pcrel(r0, LabelRef(lname)))
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
231 self.imms.append((lname, ins.value))
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
232 elif type(ins) is ir.Store:
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
233 # Load value in r0:
258
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
234 r0 = self.getreg(ins.value)
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
235 # store in memory:
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
236 # TODO: split globals and locals??
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
237 #self.getGlobal(arm.r1, ins.location)
240
6259856841a0 Remove project
Windel Bouwman
parents: 237
diff changeset
238 # Horrible hack with localVars
6259856841a0 Remove project
Windel Bouwman
parents: 237
diff changeset
239 if ins.location in self.localVars:
243
ef683881c64e Remove various files
Windel Bouwman
parents: 240
diff changeset
240 # The value was alloc'ed
258
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
241 self.emit(arm.str_sprel(r0, arm.MemSpRel(self.getStack(ins.location))))
240
6259856841a0 Remove project
Windel Bouwman
parents: 237
diff changeset
242 else:
258
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
243 r1 = self.getreg(ins.location)
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
244 self.emit(arm.storeimm5_ins(r0, arm.MemR8Rel(r1, 0)))
259
ac603eb66b63 Added function call
Windel Bouwman
parents: 258
diff changeset
245 self.freereg(ins.location, ins)
258
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
246 self.freereg(ins.value, ins)
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
247 elif type(ins) is ir.Load:
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
248 # TODO: differ global and local??
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
249 #self.getGlobal(arm.r0, ins.location)
258
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
250 r0 = self.getreg(ins.value)
240
6259856841a0 Remove project
Windel Bouwman
parents: 237
diff changeset
251 if ins.location in self.localVars:
258
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
252 self.emit(arm.ldr_sprel(r0, arm.MemSpRel(self.getStack(ins.location))))
240
6259856841a0 Remove project
Windel Bouwman
parents: 237
diff changeset
253 else:
258
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
254 r2 = self.getreg(ins.location)
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
255 self.emit(arm.loadimm5_ins(r0, arm.MemR8Rel(r2, 0)))
259
ac603eb66b63 Added function call
Windel Bouwman
parents: 258
diff changeset
256 self.freereg(ins.location, ins)
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
257 elif type(ins) is ir.BinaryOperator:
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
258 # Load operands:
258
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
259 r0 = self.getreg(ins.value1)
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
260 r1 = self.getreg(ins.value2)
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
261 r2 = self.getreg(ins.result)
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
262 # do operation:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
263 if ins.operation == '+':
258
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
264 self.emit(arm.addregs_ins(r2, r0, r1))
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
265 elif ins.operation == '<<':
258
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
266 self.emit(arm.movregreg_ins(r2, r0))
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
267 self.emit(arm.lslregs_ins(r2, r1))
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
268 elif ins.operation == '|':
258
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
269 self.emit(arm.movregreg_ins(r2, r0))
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
270 self.emit(arm.orrregs_ins(r2, r1))
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
271 else:
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
272 raise NotImplementedError('operation {} not implemented'.format(ins.operation))
258
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
273 self.freereg(ins.value1, ins)
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
274 self.freereg(ins.value2, ins)
259
ac603eb66b63 Added function call
Windel Bouwman
parents: 258
diff changeset
275 elif type(ins) is ir.Call:
ac603eb66b63 Added function call
Windel Bouwman
parents: 258
diff changeset
276 # TODO: prep parameters:
ac603eb66b63 Added function call
Windel Bouwman
parents: 258
diff changeset
277 self.emit(arm.bl_ins(LabelRef(ins.callee.name)))
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
278 elif type(ins) is ir.Return:
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
279 self.emit(arm.pop_ins(arm.RegisterSet({arm.r3, arm.r4, arm.r5, arm.r6, arm.r7, arm.pc})))
212
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 211
diff changeset
280 elif type(ins) is ir.ConditionalBranch:
258
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
281 r0 = self.getreg(ins.a)
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
282 r1 = self.getreg(ins.b)
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
283 self.emit(arm.cmp_ins(r1, r0))
262
ed14e077124c Added conditional branch instructions
Windel Bouwman
parents: 261
diff changeset
284 tgt_yes = LabelRef(ins.lab1.name)
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
285 if ins.cond == '==':
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
286 self.emit(arm.beq_ins(tgt_yes))
262
ed14e077124c Added conditional branch instructions
Windel Bouwman
parents: 261
diff changeset
287 elif ins.cond == '<':
ed14e077124c Added conditional branch instructions
Windel Bouwman
parents: 261
diff changeset
288 self.emit(arm.blt_ins(tgt_yes))
ed14e077124c Added conditional branch instructions
Windel Bouwman
parents: 261
diff changeset
289 elif ins.cond == '>':
ed14e077124c Added conditional branch instructions
Windel Bouwman
parents: 261
diff changeset
290 self.emit(arm.bgt_ins(tgt_yes))
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
291 else:
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
292 raise NotImplementedError('"{}" not covered'.format(ins.cond))
262
ed14e077124c Added conditional branch instructions
Windel Bouwman
parents: 261
diff changeset
293 tgt_no = LabelRef(ins.lab2.name)
ed14e077124c Added conditional branch instructions
Windel Bouwman
parents: 261
diff changeset
294 self.emit(arm.b_ins(tgt_no))
258
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
295 self.freereg(ins.a, ins)
04c19282a5aa Added register allocator
Windel Bouwman
parents: 256
diff changeset
296 self.freereg(ins.b, ins)
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 219
diff changeset
297 elif type(ins) is ir.Alloc:
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
298 # Local variables are added to stack
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 219
diff changeset
299 self.addStack(ins.value)
240
6259856841a0 Remove project
Windel Bouwman
parents: 237
diff changeset
300 self.localVars.append(ins.value)
6259856841a0 Remove project
Windel Bouwman
parents: 237
diff changeset
301 # load address into variable:
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
302 else:
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
303 raise NotImplementedError('IR "{}" not covered'.format(ins))
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
304
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
305