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