255
|
1 import logging
|
211
|
2 import ir
|
249
|
3 from target import Label, Comment, Alignment, LabelRef, Imm32, DebugInfo
|
218
|
4 import cortexm3 as arm
|
211
|
5 from ppci import CompilerError
|
269
|
6 import registerallocator
|
|
7 from instructionselector import InstructionSelector
|
272
|
8 import irmach
|
275
|
9 from irmach import makeIns
|
|
10 import canon
|
|
11 import asm
|
274
|
12
|
|
13 class ArmFrame(irmach.Frame):
|
|
14 """
|
|
15 Arm specific frame for functions.
|
|
16 """
|
275
|
17 def __init__(self, name):
|
|
18 # We use r7 as frame pointer.
|
|
19 super().__init__(name)
|
|
20 self.regs = ['r0', 'r1', 'r2', 'r3', 'r4', 'r5', 'r6']
|
|
21 self.rv = ir.Temp('special_RV')
|
|
22 self.p1 = ir.Temp('special_P1')
|
|
23 self.p2 = ir.Temp('special_P2')
|
|
24 self.p3 = ir.Temp('special_P3')
|
|
25 self.p4 = ir.Temp('special_P4')
|
|
26 self.fp = ir.Temp('special_FP')
|
|
27 # Pre-colored registers:
|
|
28 self.tempMap = {}
|
|
29 self.tempMap[self.rv] = 'r0'
|
|
30 self.tempMap[self.p1] = 'r1'
|
|
31 self.tempMap[self.p2] = 'r2'
|
|
32 self.tempMap[self.p3] = 'r3'
|
|
33 self.tempMap[self.p4] = 'r4'
|
|
34 self.tempMap[self.fp] = 'r7'
|
|
35 self.locVars = {}
|
|
36 self.parMap = {}
|
276
|
37 # Literal pool:
|
|
38 self.constants = []
|
275
|
39
|
|
40 def argLoc(self, pos):
|
|
41 """
|
|
42 Gets the function parameter location in IR-code format.
|
|
43 """
|
|
44 if pos == 0:
|
|
45 return self.p1
|
|
46 elif pos == 1:
|
|
47 return self.p2
|
|
48 elif pos == 2:
|
|
49 return self.p3
|
|
50 elif pos == 3:
|
|
51 return self.p4
|
|
52 else:
|
|
53 raise NotImplementedError('No more than 4 parameters implemented')
|
|
54
|
|
55 def allocVar(self, lvar):
|
|
56 if lvar not in self.locVars:
|
|
57 self.locVars[lvar] = self.stacksize
|
|
58 self.stacksize = self.stacksize + 4
|
|
59 return self.locVars[lvar]
|
|
60
|
276
|
61 def addConstant(self, value):
|
|
62 lab_name = '{}_literal_{}'.format(self.name, len(self.constants))
|
|
63 self.constants.append((lab_name, value))
|
|
64 return lab_name
|
|
65
|
275
|
66 def EntryExitGlue3(self):
|
|
67 """
|
|
68 Add code for the prologue and the epilogue. Add a label, the
|
|
69 return instruction and the stack pointer adjustment for the frame.
|
|
70 """
|
|
71 self.instructions.insert(0, makeIns('{}:'.format(self.name)))
|
|
72 self.instructions.insert(1, makeIns('push {lr, r7}'))
|
|
73 self.instructions.insert(2, makeIns('mov r7, sp'))
|
|
74 self.instructions.insert(3, makeIns('add sp, sp, {}'.format(self.stacksize)))
|
|
75 self.instructions.append(makeIns('sub sp, sp, {}'.format(self.stacksize)))
|
|
76 self.instructions.append(makeIns('pop {pc,r7}'))
|
276
|
77 # Add constant literals:
|
|
78 for ln, v in self.constants:
|
|
79 self.instructions.append(makeIns('{}:'.format(ln)))
|
|
80 self.instructions.append(makeIns('dcd {}'.format(v)))
|
274
|
81
|
|
82
|
268
|
83 class ArmInstructionSelector(InstructionSelector):
|
276
|
84
|
269
|
85 """ Instruction selector for the arm architecture """
|
268
|
86 def munchExpr(self, e):
|
|
87 if isinstance(e, ir.Alloc):
|
|
88 return 0
|
275
|
89 elif isinstance(e, ir.Binop) and e.operation == '+' and isinstance(e.b, ir.Const) and e.b.value < 8:
|
|
90 a = self.munchExpr(e.a)
|
|
91 d = self.newTmp()
|
|
92 self.emit('add %d0, %s0, {}'.format(e.b.value), dst=[d], src=[a])
|
|
93 return d
|
268
|
94 elif isinstance(e, ir.Binop) and e.operation == '+':
|
275
|
95 a = self.munchExpr(e.a)
|
|
96 b = self.munchExpr(e.b)
|
268
|
97 d = self.newTmp()
|
|
98 self.emit('add %d0, %s0, %s1', dst=[d], src=[a, b])
|
|
99 return d
|
275
|
100 elif isinstance(e, ir.Binop) and e.operation == '-' and isinstance(e.b, ir.Const) and e.b.value < 8:
|
|
101 a = self.munchExpr(e.a)
|
|
102 d = self.newTmp()
|
|
103 self.emit('sub %d0, %s0, {}'.format(e.b.value), dst=[d], src=[a])
|
|
104 return d
|
269
|
105 elif isinstance(e, ir.Binop) and e.operation == '-':
|
275
|
106 a = self.munchExpr(e.a)
|
|
107 b = self.munchExpr(e.b)
|
269
|
108 d = self.newTmp()
|
|
109 self.emit('sub %d0, %s0, %s1', dst=[d], src=[a, b])
|
|
110 return d
|
268
|
111 elif isinstance(e, ir.Binop) and e.operation == '|':
|
275
|
112 a = self.munchExpr(e.a)
|
|
113 b = self.munchExpr(e.b)
|
268
|
114 d = self.newTmp()
|
276
|
115 self.emit('mov %d0, %s0', src=[a], dst=[d])
|
|
116 self.emit('orr %d0, %s0', dst=[d], src=[b, d])
|
268
|
117 return d
|
|
118 elif isinstance(e, ir.Binop) and e.operation == '<<':
|
275
|
119 a = self.munchExpr(e.a)
|
|
120 b = self.munchExpr(e.b)
|
268
|
121 d = self.newTmp()
|
276
|
122 self.emit('mov %d0, %s0', src=[a], dst=[d])
|
|
123 self.emit('lsl %d0, %s0', dst=[d], src=[b, d]) # TODO: is d a source variable?
|
268
|
124 return d
|
|
125 elif isinstance(e, ir.Binop) and e.operation == '*':
|
275
|
126 a = self.munchExpr(e.a)
|
|
127 b = self.munchExpr(e.b)
|
268
|
128 d = self.newTmp()
|
276
|
129 self.emit('mov %d0, %s0', src=[a], dst=[d])
|
|
130 self.emit('mul %d0, %s0', dst=[d], src=[b, d])
|
268
|
131 return d
|
275
|
132 elif isinstance(e, ir.Const) and e.value < 256:
|
268
|
133 d = self.newTmp()
|
275
|
134 self.emit('mov %d0, {}'.format(e.value), dst=[d])
|
|
135 return d
|
276
|
136 elif isinstance(e, ir.Const) and e.value < (2**31):
|
|
137 d = self.newTmp()
|
|
138 ln = self.frame.addConstant(e.value)
|
|
139 self.emit('ldr %d0, {}'.format(ln), dst=[d])
|
|
140 return d
|
275
|
141 elif isinstance(e, ir.Mem) and isinstance(e.e, ir.Binop) and \
|
|
142 e.e.operation == '+' and isinstance(e.e.b, ir.Const):
|
|
143 base = self.munchExpr(e.e.a)
|
|
144 d = self.newTmp()
|
|
145 self.emit('ldr %d0, [%s0 + {}]'.format(e.e.b.value), src=[base], dst=[d])
|
268
|
146 return d
|
|
147 elif isinstance(e, ir.Mem):
|
|
148 # Load from memory
|
275
|
149 base = self.munchExpr(e.e)
|
268
|
150 d = self.newTmp()
|
275
|
151 self.emit('ldr %d0, [%s0]', src=[base], dst=[d])
|
268
|
152 return d
|
|
153 elif isinstance(e, ir.Temp):
|
275
|
154 return e
|
272
|
155 elif isinstance(e, ir.Call):
|
275
|
156 # Move arguments into proper locations:
|
|
157 reguses = []
|
|
158 for i, a in enumerate(e.arguments):
|
|
159 loc = self.frame.argLoc(i)
|
|
160 m = ir.Move(loc, a)
|
|
161 self.munchStm(m)
|
|
162 if isinstance(loc, ir.Temp):
|
|
163 reguses.append(loc)
|
|
164 self.emit('bl {}'.format(e.f.name), src=reguses, dst=[self.frame.rv])
|
|
165 d = self.newTmp()
|
|
166 self.move(d, self.frame.rv)
|
|
167 return d
|
268
|
168 else:
|
272
|
169 raise NotImplementedError('Expr --> {}'.format(e))
|
268
|
170
|
|
171 def munchStm(self, s):
|
275
|
172 if isinstance(s, ir.Terminator):
|
|
173 pass
|
|
174 elif isinstance(s, ir.Move) and isinstance(s.dst, ir.Mem) and isinstance(s.dst.e, ir.Binop) and s.dst.e.operation == '+' and isinstance(s.dst.e.a, ir.Temp) and isinstance(s.dst.e.b, ir.Const):
|
|
175 val = self.munchExpr(s.src)
|
|
176 self.emit('str %s1, [%s0 + {}]'.format(s.dst.e.b.value), src=[s.dst.e.a, val])
|
|
177 elif isinstance(s, ir.Move) and isinstance(s.dst, ir.Mem):
|
268
|
178 memloc = self.munchExpr(s.dst.e)
|
|
179 val = self.munchExpr(s.src)
|
275
|
180 self.emit('str %s1, [%s0]', src=[memloc, val])
|
268
|
181 elif isinstance(s, ir.Move) and isinstance(s.dst, ir.Temp):
|
|
182 val = self.munchExpr(s.src)
|
275
|
183 dreg = s.dst
|
269
|
184 self.emit('mov %d0, %s0', dst=[dreg], src=[val])
|
275
|
185 elif isinstance(s, ir.Exp):
|
|
186 # Generate expression code and discard the result.
|
|
187 x = self.munchExpr(s.e)
|
|
188 self.emit('mov r0, r0', src=[x])
|
268
|
189 elif isinstance(s, ir.Jump):
|
269
|
190 tgt = self.targets[s.target]
|
275
|
191 self.emit('b {}'.format(s.target.name), jumps=[tgt])
|
268
|
192 elif isinstance(s, ir.CJump):
|
269
|
193 a = self.munchExpr(s.a)
|
|
194 b = self.munchExpr(s.b)
|
|
195 self.emit('cmp %s0, %s1', src=[a, b])
|
|
196 ntgt = self.targets[s.lab_no]
|
|
197 ytgt = self.targets[s.lab_yes]
|
276
|
198 jmp_ins = makeIns('b {}'.format(s.lab_no.name), jumps=[ntgt])
|
269
|
199 # Explicitely add fallthrough:
|
276
|
200 self.emit('beq {}'.format(s.lab_yes.name), jumps=[ytgt, jmp_ins])
|
269
|
201 self.emit2(jmp_ins)
|
268
|
202 else:
|
274
|
203 raise NotImplementedError('Stmt --> {}'.format(s))
|
268
|
204
|
|
205
|
274
|
206 # TODO: this class could be target independent:
|
211
|
207 class ArmCodeGenerator:
|
268
|
208 def __init__(self, outs):
|
269
|
209 # TODO: schedule traces in better order.
|
|
210 # This is optional!
|
268
|
211 self.ins_sel = ArmInstructionSelector()
|
277
|
212 self.ra = registerallocator.RegisterAllocator()
|
268
|
213 self.outs = outs
|
|
214 self.outs.getSection('code').address = 0x08000000
|
|
215 self.outs.getSection('data').address = 0x20000000
|
|
216
|
275
|
217 def generateFunc(self, irfunc):
|
|
218 # Create a frame for this function:
|
|
219 frame = ArmFrame(irfunc.name)
|
277
|
220
|
275
|
221 # Canonicalize the intermediate language:
|
|
222 canon.make(irfunc, frame)
|
277
|
223 print('after canonicalize:')
|
|
224 irfunc.dump()
|
275
|
225 self.ins_sel.munchFunction(irfunc, frame)
|
277
|
226 print('Selected instructions:')
|
|
227 for i in frame.instructions:
|
|
228 print(i)
|
275
|
229
|
|
230 # Do register allocation:
|
277
|
231 self.ra.allocFrame(frame)
|
275
|
232 # TODO: Peep-hole here?
|
|
233
|
|
234 # Add label and return and stack adjustment:
|
|
235 frame.EntryExitGlue3()
|
|
236 return frame
|
274
|
237
|
|
238 def generate(self, ircode):
|
|
239 # Munch program into a bunch of frames. One frame per function.
|
|
240 # Each frame has a flat list of abstract instructions.
|
275
|
241 # Generate code for all functions:
|
|
242 self.frames = [self.generateFunc(func) for func in ircode.Functions]
|
274
|
243
|
275
|
244 # Materialize assembly
|
|
245 # Reparse the register allocated instructions into a stream of
|
|
246 # real instructions.
|
|
247 # TODO: this is ugly via string representations. This could be
|
|
248 # another interface?
|
|
249 assembler = asm.Assembler(target=arm.armtarget, stream=self.outs)
|
|
250 self.outs.selectSection('code')
|
|
251 for frame in self.frames:
|
|
252 for i in frame.instructions:
|
|
253 assembler.assemble_line(str(i))
|
268
|
254
|
276
|
255 # TODO: fixup references, do this in another way?
|
|
256 self.outs.backpatch()
|
|
257 self.outs.backpatch()
|
275
|
258 return self.frames
|
268
|
259
|