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