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