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
|
|
6
|
|
7 class ArmCodeGenerator:
|
219
|
8 """
|
|
9 Simple code generator
|
|
10 Ad hoc implementation
|
|
11 """
|
211
|
12 def __init__(self, out):
|
|
13 self.outs = out
|
258
|
14 self.logger = logging.getLogger('codegenarm')
|
211
|
15
|
|
16 def emit(self, item):
|
|
17 self.outs.emit(item)
|
|
18
|
|
19 def generate(self, ircode):
|
|
20 assert isinstance(ircode, ir.Module)
|
255
|
21 self.logger.info('Generating arm code for {}'.format(ircode.name))
|
258
|
22 self.available_regs = {arm.r2, arm.r3, arm.r4, arm.r5, arm.r6, arm.r7}
|
|
23 self.regmap = {}
|
250
|
24 # TODO: get these from linker descriptor?
|
|
25 self.outs.getSection('code').address = 0x08000000
|
|
26 self.outs.getSection('data').address = 0x20000000
|
211
|
27 self.outs.selectSection('data')
|
|
28
|
|
29 for gvar in ircode.Variables:
|
235
|
30 self.emit(Label(gvar.name))
|
211
|
31 # TODO: use initial value:
|
235
|
32 self.dcd(0)
|
211
|
33
|
219
|
34 self.imms = [] # list with immediates relative to PC.
|
211
|
35 self.outs.selectSection('code')
|
232
|
36
|
|
37 # Manually inserted startup code:
|
235
|
38 self.dcd(0x20000678) # initial stack ptr
|
237
|
39 # TODO: use label here:
|
|
40 #self.emit(arm.dcd_ins(LabelRef('reset'))) # reset vector
|
|
41 self.dcd(0x08000009) # reset vector, lsb indicates thumb mode
|
259
|
42 self.emit(arm.bl_ins(LabelRef('main')))
|
232
|
43
|
237
|
44 self.emit(Label('reset'))
|
211
|
45 for f in ircode.Functions:
|
240
|
46 self.localVars = []
|
219
|
47 # Add global variable addresses to immediate list:
|
|
48 for gvar in ircode.Variables:
|
|
49 pass #self.imms.append((
|
|
50
|
|
51 self.stack_frame = []
|
234
|
52 self.emit(Label(f.name))
|
218
|
53 # Save some registers:
|
|
54 self.emit(arm.push_ins(arm.RegisterSet({arm.r4, arm.r5, arm.r6,arm.r7,arm.lr})))
|
211
|
55 for bb in f.BasicBlocks:
|
234
|
56 self.emit(Label(bb.name))
|
211
|
57 for ins in bb.Instructions:
|
|
58 self.generateInstruction(ins)
|
|
59
|
235
|
60 self.align()
|
219
|
61 while self.imms:
|
|
62 l, v = self.imms.pop()
|
234
|
63 self.emit(Label(l))
|
235
|
64 self.dcd(v)
|
|
65 self.align()
|
237
|
66 self.outs.backpatch()
|
|
67 self.outs.backpatch()
|
258
|
68 codesize = self.outs.getSection('code').Size
|
|
69 self.logger.info('Generated {} bytes code'.format(codesize))
|
219
|
70
|
235
|
71 def dcd(self, x):
|
|
72 self.emit(arm.dcd_ins(Imm32(x)))
|
|
73
|
|
74 def align(self):
|
|
75 self.outs.emit(Alignment(4))
|
249
|
76
|
225
|
77 # Helper functions:
|
219
|
78 def getStack(self, v):
|
|
79 off = self.stack_frame.index(v)
|
|
80 return off * 4
|
249
|
81
|
219
|
82 def addStack(self, v):
|
|
83 self.stack_frame.append(v)
|
|
84 return self.getStack(v)
|
249
|
85
|
219
|
86 def getGlobal(self, r, g):
|
|
87 _global_address = g.name + '__global'
|
235
|
88 self.emit(arm.ldr_pcrel(r, LabelRef(_global_address)))
|
249
|
89
|
225
|
90 def loadStack(self, reg, val):
|
|
91 self.emit(arm.ldr_sprel(reg, arm.MemSpRel(self.getStack(val))))
|
249
|
92
|
258
|
93 def getreg(self, v):
|
|
94 if not v in self.regmap:
|
|
95 self.regmap[v] = self.available_regs.pop()
|
|
96 return self.regmap[v]
|
|
97
|
|
98 def freereg(self, v, ins):
|
|
99 if v.lastUse(ins):
|
|
100 r = self.regmap.pop(v)
|
|
101 assert r not in self.regmap.values()
|
|
102 self.available_regs.add(r)
|
|
103
|
225
|
104 def comment(self, txt):
|
234
|
105 self.emit(Comment(txt))
|
219
|
106
|
249
|
107 def debugInfo(self, loc):
|
|
108 if loc:
|
|
109 self.emit(DebugInfo(loc))
|
|
110
|
211
|
111 def generateInstruction(self, ins):
|
232
|
112 self.comment(str(ins))
|
249
|
113 if hasattr(ins, 'debugLoc'):
|
|
114 self.debugInfo(ins.debugLoc)
|
211
|
115 if type(ins) is ir.Branch:
|
237
|
116 tgt = LabelRef(ins.target.name)
|
|
117 self.emit(arm.b_ins(tgt))
|
219
|
118 elif type(ins) is ir.ImmLoad:
|
|
119 lname = ins.target.name + '_ivalue'
|
258
|
120 r0 = self.getreg(ins.target)
|
|
121 self.emit(arm.ldr_pcrel(r0, LabelRef(lname)))
|
219
|
122 self.imms.append((lname, ins.value))
|
211
|
123 elif type(ins) is ir.Store:
|
219
|
124 # Load value in r0:
|
258
|
125 r0 = self.getreg(ins.value)
|
219
|
126 # store in memory:
|
232
|
127 # TODO: split globals and locals??
|
|
128 #self.getGlobal(arm.r1, ins.location)
|
240
|
129 # Horrible hack with localVars
|
|
130 if ins.location in self.localVars:
|
243
|
131 # The value was alloc'ed
|
258
|
132 self.emit(arm.str_sprel(r0, arm.MemSpRel(self.getStack(ins.location))))
|
240
|
133 else:
|
258
|
134 r1 = self.getreg(ins.location)
|
|
135 self.emit(arm.storeimm5_ins(r0, arm.MemR8Rel(r1, 0)))
|
259
|
136 self.freereg(ins.location, ins)
|
258
|
137 self.freereg(ins.value, ins)
|
211
|
138 elif type(ins) is ir.Load:
|
232
|
139 # TODO: differ global and local??
|
|
140 #self.getGlobal(arm.r0, ins.location)
|
258
|
141 r0 = self.getreg(ins.value)
|
240
|
142 if ins.location in self.localVars:
|
258
|
143 self.emit(arm.ldr_sprel(r0, arm.MemSpRel(self.getStack(ins.location))))
|
240
|
144 else:
|
258
|
145 r2 = self.getreg(ins.location)
|
|
146 self.emit(arm.loadimm5_ins(r0, arm.MemR8Rel(r2, 0)))
|
259
|
147 self.freereg(ins.location, ins)
|
211
|
148 elif type(ins) is ir.BinaryOperator:
|
219
|
149 # Load operands:
|
258
|
150 r0 = self.getreg(ins.value1)
|
|
151 r1 = self.getreg(ins.value2)
|
|
152 r2 = self.getreg(ins.result)
|
219
|
153 # do operation:
|
|
154 if ins.operation == '+':
|
258
|
155 self.emit(arm.addregs_ins(r2, r0, r1))
|
232
|
156 elif ins.operation == '<<':
|
258
|
157 self.emit(arm.movregreg_ins(r2, r0))
|
|
158 self.emit(arm.lslregs_ins(r2, r1))
|
232
|
159 elif ins.operation == '|':
|
258
|
160 self.emit(arm.movregreg_ins(r2, r0))
|
|
161 self.emit(arm.orrregs_ins(r2, r1))
|
219
|
162 else:
|
237
|
163 raise NotImplementedError('operation {} not implemented'.format(ins.operation))
|
258
|
164 self.freereg(ins.value1, ins)
|
|
165 self.freereg(ins.value2, ins)
|
259
|
166 elif type(ins) is ir.Call:
|
|
167 # TODO: prep parameters:
|
|
168 self.emit(arm.bl_ins(LabelRef(ins.callee.name)))
|
219
|
169 elif type(ins) is ir.Return:
|
|
170 self.emit(arm.pop_ins(arm.RegisterSet({arm.r4, arm.r5, arm.r6, arm.r7, arm.pc})))
|
212
|
171 elif type(ins) is ir.ConditionalBranch:
|
258
|
172 r0 = self.getreg(ins.a)
|
|
173 r1 = self.getreg(ins.b)
|
|
174 self.emit(arm.cmp_ins(r1, r0))
|
235
|
175 tgt_yes = Label(ins.lab1.name)
|
219
|
176 if ins.cond == '==':
|
|
177 self.emit(arm.beq_ins(tgt_yes))
|
|
178 else:
|
237
|
179 raise NotImplementedError('"{}" not covered'.format(ins.cond))
|
235
|
180 tgt_no = Label(ins.lab2.name)
|
219
|
181 self.emit(arm.jmp_ins(tgt_no))
|
258
|
182 self.freereg(ins.a, ins)
|
|
183 self.freereg(ins.b, ins)
|
222
|
184 elif type(ins) is ir.Alloc:
|
232
|
185 # Local variables are added to stack
|
222
|
186 self.addStack(ins.value)
|
240
|
187 self.localVars.append(ins.value)
|
|
188 # load address into variable:
|
211
|
189 else:
|
237
|
190 raise NotImplementedError('IR "{}" not covered'.format(ins))
|
211
|
191
|
|
192
|