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