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()
|
256
|
65 code = self.outs.getSection('code').to_bytes()
|
|
66 self.logger.info('Generated {} bytes code'.format(len(code)))
|
219
|
67
|
235
|
68 def dcd(self, x):
|
|
69 self.emit(arm.dcd_ins(Imm32(x)))
|
|
70
|
|
71 def align(self):
|
|
72 self.outs.emit(Alignment(4))
|
249
|
73
|
225
|
74 # Helper functions:
|
219
|
75 def getStack(self, v):
|
|
76 off = self.stack_frame.index(v)
|
|
77 return off * 4
|
249
|
78
|
219
|
79 def addStack(self, v):
|
|
80 self.stack_frame.append(v)
|
|
81 return self.getStack(v)
|
249
|
82
|
219
|
83 def getGlobal(self, r, g):
|
|
84 _global_address = g.name + '__global'
|
235
|
85 self.emit(arm.ldr_pcrel(r, LabelRef(_global_address)))
|
249
|
86
|
225
|
87 def loadStack(self, reg, val):
|
|
88 self.emit(arm.ldr_sprel(reg, arm.MemSpRel(self.getStack(val))))
|
249
|
89
|
225
|
90 def comment(self, txt):
|
234
|
91 self.emit(Comment(txt))
|
219
|
92
|
249
|
93 def debugInfo(self, loc):
|
|
94 if loc:
|
|
95 self.emit(DebugInfo(loc))
|
|
96
|
211
|
97 def generateInstruction(self, ins):
|
232
|
98 self.comment(str(ins))
|
249
|
99 if hasattr(ins, 'debugLoc'):
|
|
100 self.debugInfo(ins.debugLoc)
|
211
|
101 if type(ins) is ir.Branch:
|
237
|
102 tgt = LabelRef(ins.target.name)
|
|
103 self.emit(arm.b_ins(tgt))
|
219
|
104 elif type(ins) is ir.ImmLoad:
|
|
105 lname = ins.target.name + '_ivalue'
|
235
|
106 self.emit(arm.ldr_pcrel(arm.r0, LabelRef(lname)))
|
219
|
107 self.imms.append((lname, ins.value))
|
225
|
108 self.emit(arm.str_sprel(arm.r0, arm.MemSpRel(self.addStack(ins.target))))
|
211
|
109 elif type(ins) is ir.Store:
|
219
|
110 # Load value in r0:
|
225
|
111 self.loadStack(arm.r0, ins.value)
|
219
|
112 # store in memory:
|
232
|
113 # TODO: split globals and locals??
|
|
114 #self.getGlobal(arm.r1, ins.location)
|
240
|
115 # Horrible hack with localVars
|
|
116 if ins.location in self.localVars:
|
243
|
117 # The value was alloc'ed
|
240
|
118 self.emit(arm.str_sprel(arm.r0, arm.MemSpRel(self.getStack(ins.location))))
|
|
119 else:
|
|
120 self.loadStack(arm.r1, ins.location)
|
|
121 self.emit(arm.storeimm5_ins(arm.r0, arm.MemR8Rel(arm.r1, 0)))
|
211
|
122 elif type(ins) is ir.Load:
|
232
|
123 # TODO: differ global and local??
|
|
124 #self.getGlobal(arm.r0, ins.location)
|
240
|
125 if ins.location in self.localVars:
|
|
126 self.emit(arm.ldr_sprel(arm.r0, arm.MemSpRel(self.getStack(ins.location))))
|
|
127 else:
|
|
128 self.loadStack(arm.r0, ins.location)
|
|
129 self.emit(arm.loadimm5_ins(arm.r0, arm.MemR8Rel(arm.r0, 0)))
|
219
|
130 # Store value on stack:
|
225
|
131 self.emit(arm.str_sprel(arm.r0, arm.MemSpRel(self.addStack(ins.value))))
|
211
|
132 elif type(ins) is ir.BinaryOperator:
|
219
|
133 # Load operands:
|
225
|
134 self.loadStack(arm.r0, ins.value1)
|
|
135 self.loadStack(arm.r1, ins.value2)
|
219
|
136 # do operation:
|
|
137 if ins.operation == '+':
|
|
138 self.emit(arm.addregs_ins(arm.r0, arm.r0, arm.r1))
|
232
|
139 elif ins.operation == '<<':
|
|
140 self.emit(arm.lslregs_ins(arm.r0, arm.r1))
|
|
141 elif ins.operation == '|':
|
|
142 self.emit(arm.orrregs_ins(arm.r0, arm.r1))
|
219
|
143 else:
|
237
|
144 raise NotImplementedError('operation {} not implemented'.format(ins.operation))
|
219
|
145 # Store value back:
|
225
|
146 self.emit(arm.str_sprel(arm.r0, arm.MemSpRel(self.addStack(ins.result))))
|
219
|
147 elif type(ins) is ir.Return:
|
|
148 self.emit(arm.pop_ins(arm.RegisterSet({arm.r4, arm.r5, arm.r6, arm.r7, arm.pc})))
|
212
|
149 elif type(ins) is ir.ConditionalBranch:
|
225
|
150 self.loadStack(arm.r0, ins.a)
|
|
151 self.loadStack(arm.r1, ins.b)
|
218
|
152 self.emit(arm.cmp_ins(arm.r1, arm.r0))
|
235
|
153 tgt_yes = Label(ins.lab1.name)
|
219
|
154 if ins.cond == '==':
|
|
155 self.emit(arm.beq_ins(tgt_yes))
|
|
156 else:
|
237
|
157 raise NotImplementedError('"{}" not covered'.format(ins.cond))
|
235
|
158 tgt_no = Label(ins.lab2.name)
|
219
|
159 self.emit(arm.jmp_ins(tgt_no))
|
222
|
160 elif type(ins) is ir.Alloc:
|
232
|
161 # Local variables are added to stack
|
222
|
162 self.addStack(ins.value)
|
240
|
163 self.localVars.append(ins.value)
|
|
164 # load address into variable:
|
211
|
165 else:
|
237
|
166 raise NotImplementedError('IR "{}" not covered'.format(ins))
|
211
|
167
|
|
168
|