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
|
232
|
42
|
237
|
43 self.emit(Label('reset'))
|
211
|
44 for f in ircode.Functions:
|
240
|
45 self.localVars = []
|
219
|
46 # Add global variable addresses to immediate list:
|
|
47 for gvar in ircode.Variables:
|
|
48 pass #self.imms.append((
|
|
49
|
|
50 self.stack_frame = []
|
234
|
51 self.emit(Label(f.name))
|
218
|
52 # Save some registers:
|
|
53 self.emit(arm.push_ins(arm.RegisterSet({arm.r4, arm.r5, arm.r6,arm.r7,arm.lr})))
|
211
|
54 for bb in f.BasicBlocks:
|
234
|
55 self.emit(Label(bb.name))
|
211
|
56 for ins in bb.Instructions:
|
|
57 self.generateInstruction(ins)
|
|
58
|
235
|
59 self.align()
|
219
|
60 while self.imms:
|
|
61 l, v = self.imms.pop()
|
234
|
62 self.emit(Label(l))
|
235
|
63 self.dcd(v)
|
|
64 self.align()
|
237
|
65 self.outs.backpatch()
|
|
66 self.outs.backpatch()
|
258
|
67 codesize = self.outs.getSection('code').Size
|
|
68 self.logger.info('Generated {} bytes code'.format(codesize))
|
219
|
69
|
235
|
70 def dcd(self, x):
|
|
71 self.emit(arm.dcd_ins(Imm32(x)))
|
|
72
|
|
73 def align(self):
|
|
74 self.outs.emit(Alignment(4))
|
249
|
75
|
225
|
76 # Helper functions:
|
219
|
77 def getStack(self, v):
|
|
78 off = self.stack_frame.index(v)
|
|
79 return off * 4
|
249
|
80
|
219
|
81 def addStack(self, v):
|
|
82 self.stack_frame.append(v)
|
|
83 return self.getStack(v)
|
249
|
84
|
219
|
85 def getGlobal(self, r, g):
|
|
86 _global_address = g.name + '__global'
|
235
|
87 self.emit(arm.ldr_pcrel(r, LabelRef(_global_address)))
|
249
|
88
|
225
|
89 def loadStack(self, reg, val):
|
|
90 self.emit(arm.ldr_sprel(reg, arm.MemSpRel(self.getStack(val))))
|
249
|
91
|
258
|
92 def getreg(self, v):
|
|
93 if not v in self.regmap:
|
|
94 self.regmap[v] = self.available_regs.pop()
|
|
95 return self.regmap[v]
|
|
96
|
|
97 def freereg(self, v, ins):
|
|
98 if v.lastUse(ins):
|
|
99 r = self.regmap.pop(v)
|
|
100 assert r not in self.regmap.values()
|
|
101 self.available_regs.add(r)
|
|
102
|
225
|
103 def comment(self, txt):
|
234
|
104 self.emit(Comment(txt))
|
219
|
105
|
249
|
106 def debugInfo(self, loc):
|
|
107 if loc:
|
|
108 self.emit(DebugInfo(loc))
|
|
109
|
211
|
110 def generateInstruction(self, ins):
|
232
|
111 self.comment(str(ins))
|
249
|
112 if hasattr(ins, 'debugLoc'):
|
|
113 self.debugInfo(ins.debugLoc)
|
211
|
114 if type(ins) is ir.Branch:
|
237
|
115 tgt = LabelRef(ins.target.name)
|
|
116 self.emit(arm.b_ins(tgt))
|
219
|
117 elif type(ins) is ir.ImmLoad:
|
|
118 lname = ins.target.name + '_ivalue'
|
258
|
119 r0 = self.getreg(ins.target)
|
|
120 self.emit(arm.ldr_pcrel(r0, LabelRef(lname)))
|
219
|
121 self.imms.append((lname, ins.value))
|
211
|
122 elif type(ins) is ir.Store:
|
219
|
123 # Load value in r0:
|
258
|
124 r0 = self.getreg(ins.value)
|
219
|
125 # store in memory:
|
232
|
126 # TODO: split globals and locals??
|
|
127 #self.getGlobal(arm.r1, ins.location)
|
240
|
128 # Horrible hack with localVars
|
|
129 if ins.location in self.localVars:
|
243
|
130 # The value was alloc'ed
|
258
|
131 self.emit(arm.str_sprel(r0, arm.MemSpRel(self.getStack(ins.location))))
|
240
|
132 else:
|
258
|
133 r1 = self.getreg(ins.location)
|
|
134 self.emit(arm.storeimm5_ins(r0, arm.MemR8Rel(r1, 0)))
|
|
135 self.freereg(ins.location, ins)
|
|
136 self.freereg(ins.value, ins)
|
211
|
137 elif type(ins) is ir.Load:
|
232
|
138 # TODO: differ global and local??
|
|
139 #self.getGlobal(arm.r0, ins.location)
|
258
|
140 r0 = self.getreg(ins.value)
|
240
|
141 if ins.location in self.localVars:
|
258
|
142 self.emit(arm.ldr_sprel(r0, arm.MemSpRel(self.getStack(ins.location))))
|
240
|
143 else:
|
258
|
144 r2 = self.getreg(ins.location)
|
|
145 self.emit(arm.loadimm5_ins(r0, arm.MemR8Rel(r2, 0)))
|
|
146 self.freereg(ins.location, ins)
|
211
|
147 elif type(ins) is ir.BinaryOperator:
|
219
|
148 # Load operands:
|
258
|
149 r0 = self.getreg(ins.value1)
|
|
150 r1 = self.getreg(ins.value2)
|
|
151 r2 = self.getreg(ins.result)
|
219
|
152 # do operation:
|
|
153 if ins.operation == '+':
|
258
|
154 self.emit(arm.addregs_ins(r2, r0, r1))
|
232
|
155 elif ins.operation == '<<':
|
258
|
156 self.emit(arm.movregreg_ins(r2, r0))
|
|
157 self.emit(arm.lslregs_ins(r2, r1))
|
232
|
158 elif ins.operation == '|':
|
258
|
159 self.emit(arm.movregreg_ins(r2, r0))
|
|
160 self.emit(arm.orrregs_ins(r2, r1))
|
219
|
161 else:
|
237
|
162 raise NotImplementedError('operation {} not implemented'.format(ins.operation))
|
258
|
163 self.freereg(ins.value1, ins)
|
|
164 self.freereg(ins.value2, ins)
|
219
|
165 elif type(ins) is ir.Return:
|
|
166 self.emit(arm.pop_ins(arm.RegisterSet({arm.r4, arm.r5, arm.r6, arm.r7, arm.pc})))
|
212
|
167 elif type(ins) is ir.ConditionalBranch:
|
258
|
168 r0 = self.getreg(ins.a)
|
|
169 r1 = self.getreg(ins.b)
|
|
170 self.emit(arm.cmp_ins(r1, r0))
|
235
|
171 tgt_yes = Label(ins.lab1.name)
|
219
|
172 if ins.cond == '==':
|
|
173 self.emit(arm.beq_ins(tgt_yes))
|
|
174 else:
|
237
|
175 raise NotImplementedError('"{}" not covered'.format(ins.cond))
|
235
|
176 tgt_no = Label(ins.lab2.name)
|
219
|
177 self.emit(arm.jmp_ins(tgt_no))
|
258
|
178 self.freereg(ins.a, ins)
|
|
179 self.freereg(ins.b, ins)
|
222
|
180 elif type(ins) is ir.Alloc:
|
232
|
181 # Local variables are added to stack
|
222
|
182 self.addStack(ins.value)
|
240
|
183 self.localVars.append(ins.value)
|
|
184 # load address into variable:
|
211
|
185 else:
|
237
|
186 raise NotImplementedError('IR "{}" not covered'.format(ins))
|
211
|
187
|
|
188
|