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