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