annotate python/codegenarm.py @ 243:ef683881c64e

Remove various files
author Windel Bouwman
date Tue, 23 Jul 2013 16:50:02 +0200
parents 6259856841a0
children e41e4109addd
rev   line source
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
1 import ir
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
2 from target import Label, Comment, Alignment, LabelRef, Imm32
218
494828a7adf1 added some sort of cache to assembler
Windel Bouwman
parents: 212
diff changeset
3 import cortexm3 as arm
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
4 from ppci import CompilerError
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
5
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
6 class ArmCodeGenerator:
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
7 """
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
8 Simple code generator
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
9 Ad hoc implementation
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
10 """
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
11 def __init__(self, out):
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
12 self.outs = out
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
13
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
14 def emit(self, item):
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
15 self.outs.emit(item)
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
16
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
17 def generate(self, ircode):
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
18 assert isinstance(ircode, ir.Module)
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
19 self.outs.selectSection('data')
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
20
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
21 for gvar in ircode.Variables:
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
22 self.emit(Label(gvar.name))
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
23 # TODO: use initial value:
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
24 self.dcd(0)
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
25
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
26 self.imms = [] # list with immediates relative to PC.
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
27 self.outs.selectSection('code')
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
28
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
29 # Manually inserted startup code:
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
30 self.dcd(0x20000678) # initial stack ptr
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
31 # TODO: use label here:
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
32 #self.emit(arm.dcd_ins(LabelRef('reset'))) # reset vector
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
33 self.dcd(0x08000009) # reset vector, lsb indicates thumb mode
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
34
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
35 self.emit(Label('reset'))
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
36 for f in ircode.Functions:
240
6259856841a0 Remove project
Windel Bouwman
parents: 237
diff changeset
37 self.localVars = []
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
38 # Add global variable addresses to immediate list:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
39 for gvar in ircode.Variables:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
40 pass #self.imms.append((
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
41
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
42 self.stack_frame = []
234
Windel Bouwman
parents: 232
diff changeset
43 self.emit(Label(f.name))
218
494828a7adf1 added some sort of cache to assembler
Windel Bouwman
parents: 212
diff changeset
44 # Save some registers:
494828a7adf1 added some sort of cache to assembler
Windel Bouwman
parents: 212
diff changeset
45 self.emit(arm.push_ins(arm.RegisterSet({arm.r4, arm.r5, arm.r6,arm.r7,arm.lr})))
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
46 for bb in f.BasicBlocks:
234
Windel Bouwman
parents: 232
diff changeset
47 self.emit(Label(bb.name))
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
48 for ins in bb.Instructions:
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
49 self.generateInstruction(ins)
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
50
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
51 self.align()
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
52 while self.imms:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
53 l, v = self.imms.pop()
234
Windel Bouwman
parents: 232
diff changeset
54 self.emit(Label(l))
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
55 self.dcd(v)
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
56 self.align()
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
57 self.outs.backpatch()
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
58 self.outs.backpatch()
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
59
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
60 def dcd(self, x):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
61 self.emit(arm.dcd_ins(Imm32(x)))
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
62
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
63 def align(self):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
64 self.outs.emit(Alignment(4))
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
65 # Helper functions:
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
66 def getStack(self, v):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
67 off = self.stack_frame.index(v)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
68 return off * 4
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
69 def addStack(self, v):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
70 self.stack_frame.append(v)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
71 return self.getStack(v)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
72 def getGlobal(self, r, g):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
73 _global_address = g.name + '__global'
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
74 self.emit(arm.ldr_pcrel(r, LabelRef(_global_address)))
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
75 def loadStack(self, reg, val):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
76 self.emit(arm.ldr_sprel(reg, arm.MemSpRel(self.getStack(val))))
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
77 def comment(self, txt):
234
Windel Bouwman
parents: 232
diff changeset
78 self.emit(Comment(txt))
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
79
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
80 def generateInstruction(self, ins):
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
81 self.comment(str(ins))
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
82 if type(ins) is ir.Branch:
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
83 tgt = LabelRef(ins.target.name)
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
84 self.emit(arm.b_ins(tgt))
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
85 elif type(ins) is ir.ImmLoad:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
86 lname = ins.target.name + '_ivalue'
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
87 self.emit(arm.ldr_pcrel(arm.r0, LabelRef(lname)))
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
88 self.imms.append((lname, ins.value))
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
89 self.emit(arm.str_sprel(arm.r0, arm.MemSpRel(self.addStack(ins.target))))
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
90 elif type(ins) is ir.Store:
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
91 # Load value in r0:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
92 self.loadStack(arm.r0, ins.value)
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
93 # store in memory:
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
94 # TODO: split globals and locals??
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
95 #self.getGlobal(arm.r1, ins.location)
240
6259856841a0 Remove project
Windel Bouwman
parents: 237
diff changeset
96 # Horrible hack with localVars
6259856841a0 Remove project
Windel Bouwman
parents: 237
diff changeset
97 if ins.location in self.localVars:
243
ef683881c64e Remove various files
Windel Bouwman
parents: 240
diff changeset
98 # The value was alloc'ed
240
6259856841a0 Remove project
Windel Bouwman
parents: 237
diff changeset
99 self.emit(arm.str_sprel(arm.r0, arm.MemSpRel(self.getStack(ins.location))))
6259856841a0 Remove project
Windel Bouwman
parents: 237
diff changeset
100 else:
6259856841a0 Remove project
Windel Bouwman
parents: 237
diff changeset
101 self.loadStack(arm.r1, ins.location)
6259856841a0 Remove project
Windel Bouwman
parents: 237
diff changeset
102 self.emit(arm.storeimm5_ins(arm.r0, arm.MemR8Rel(arm.r1, 0)))
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
103 elif type(ins) is ir.Load:
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
104 # TODO: differ global and local??
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
105 #self.getGlobal(arm.r0, ins.location)
240
6259856841a0 Remove project
Windel Bouwman
parents: 237
diff changeset
106 if ins.location in self.localVars:
6259856841a0 Remove project
Windel Bouwman
parents: 237
diff changeset
107 self.emit(arm.ldr_sprel(arm.r0, arm.MemSpRel(self.getStack(ins.location))))
6259856841a0 Remove project
Windel Bouwman
parents: 237
diff changeset
108 else:
6259856841a0 Remove project
Windel Bouwman
parents: 237
diff changeset
109 self.loadStack(arm.r0, ins.location)
6259856841a0 Remove project
Windel Bouwman
parents: 237
diff changeset
110 self.emit(arm.loadimm5_ins(arm.r0, arm.MemR8Rel(arm.r0, 0)))
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
111 # Store value on stack:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
112 self.emit(arm.str_sprel(arm.r0, arm.MemSpRel(self.addStack(ins.value))))
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
113 elif type(ins) is ir.BinaryOperator:
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
114 # Load operands:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
115 self.loadStack(arm.r0, ins.value1)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
116 self.loadStack(arm.r1, ins.value2)
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
117 # do operation:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
118 if ins.operation == '+':
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
119 self.emit(arm.addregs_ins(arm.r0, arm.r0, arm.r1))
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
120 elif ins.operation == '<<':
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
121 self.emit(arm.lslregs_ins(arm.r0, arm.r1))
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
122 elif ins.operation == '|':
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
123 self.emit(arm.orrregs_ins(arm.r0, arm.r1))
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
124 else:
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
125 raise NotImplementedError('operation {} not implemented'.format(ins.operation))
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
126 # Store value back:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
127 self.emit(arm.str_sprel(arm.r0, arm.MemSpRel(self.addStack(ins.result))))
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
128 elif type(ins) is ir.Return:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
129 self.emit(arm.pop_ins(arm.RegisterSet({arm.r4, arm.r5, arm.r6, arm.r7, arm.pc})))
212
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 211
diff changeset
130 elif type(ins) is ir.ConditionalBranch:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
131 self.loadStack(arm.r0, ins.a)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
132 self.loadStack(arm.r1, ins.b)
218
494828a7adf1 added some sort of cache to assembler
Windel Bouwman
parents: 212
diff changeset
133 self.emit(arm.cmp_ins(arm.r1, arm.r0))
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
134 tgt_yes = Label(ins.lab1.name)
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
135 if ins.cond == '==':
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
136 self.emit(arm.beq_ins(tgt_yes))
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
137 else:
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
138 raise NotImplementedError('"{}" not covered'.format(ins.cond))
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
139 tgt_no = Label(ins.lab2.name)
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
140 self.emit(arm.jmp_ins(tgt_no))
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 219
diff changeset
141 elif type(ins) is ir.Alloc:
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
142 # Local variables are added to stack
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 219
diff changeset
143 self.addStack(ins.value)
240
6259856841a0 Remove project
Windel Bouwman
parents: 237
diff changeset
144 self.localVars.append(ins.value)
6259856841a0 Remove project
Windel Bouwman
parents: 237
diff changeset
145 # load address into variable:
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
146 else:
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
147 raise NotImplementedError('IR "{}" not covered'.format(ins))
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
148
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
149