annotate python/target/armframe.py @ 290:7b38782ed496

File moves
author Windel Bouwman
date Sun, 24 Nov 2013 11:24:15 +0100
parents python/codegenarm.py@02385f62f250
children 534b94b40aa8
rev   line source
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
1 import ir
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
2 from ppci import CompilerError
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
3 from .basetarget import Label, Comment, Alignment, LabelRef, DebugInfo, Nop
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
4 from .basetarget import Imm32, Imm3
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
5 import .armtarget as arm
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
6 from .instructionselector import InstructionSelector
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 270
diff changeset
7 import irmach
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
8 from irmach import AbstractInstruction as makeIns
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
9 import asm
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
10
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
11
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
12 class ArmFrame(irmach.Frame):
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
13 """
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
14 Arm specific frame for functions.
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
15 """
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
16 def __init__(self, name):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
17 # We use r7 as frame pointer.
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
18 super().__init__(name)
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
19 self.regs = [arm.r0, arm.r1, arm.r2, arm.r3, arm.r4, arm.r5, arm.r6]
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
20 self.rv = ir.Temp('special_RV')
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
21 self.p1 = ir.Temp('special_P1')
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
22 self.p2 = ir.Temp('special_P2')
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
23 self.p3 = ir.Temp('special_P3')
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
24 self.p4 = ir.Temp('special_P4')
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
25 self.fp = ir.Temp('special_FP')
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
26 # Pre-colored registers:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
27 self.tempMap = {}
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
28 self.tempMap[self.rv] = arm.r0
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
29 self.tempMap[self.p1] = arm.r1
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
30 self.tempMap[self.p2] = arm.r2
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
31 self.tempMap[self.p3] = arm.r3
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
32 self.tempMap[self.p4] = arm.r4
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
33 self.tempMap[self.fp] = arm.r7
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
34 self.locVars = {}
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
35 self.parMap = {}
276
Windel Bouwman
parents: 275
diff changeset
36 # Literal pool:
Windel Bouwman
parents: 275
diff changeset
37 self.constants = []
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
38
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
39 def argLoc(self, pos):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
40 """
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
41 Gets the function parameter location in IR-code format.
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
42 """
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
43 if pos == 0:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
44 return self.p1
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
45 elif pos == 1:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
46 return self.p2
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
47 elif pos == 2:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
48 return self.p3
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
49 elif pos == 3:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
50 return self.p4
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
51 else:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
52 raise NotImplementedError('No more than 4 parameters implemented')
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
53
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
54 def allocVar(self, lvar):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
55 if lvar not in self.locVars:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
56 self.locVars[lvar] = self.stacksize
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
57 self.stacksize = self.stacksize + 4
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
58 return self.locVars[lvar]
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
59
276
Windel Bouwman
parents: 275
diff changeset
60 def addConstant(self, value):
Windel Bouwman
parents: 275
diff changeset
61 lab_name = '{}_literal_{}'.format(self.name, len(self.constants))
Windel Bouwman
parents: 275
diff changeset
62 self.constants.append((lab_name, value))
Windel Bouwman
parents: 275
diff changeset
63 return lab_name
Windel Bouwman
parents: 275
diff changeset
64
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
65 def EntryExitGlue3(self):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
66 """
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
67 Add code for the prologue and the epilogue. Add a label, the
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
68 return instruction and the stack pointer adjustment for the frame.
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
69 """
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
70 self.instructions.insert(0, makeIns(arm.Label(self.name)))
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
71 self.instructions.insert(1, makeIns(arm.push_ins(arm.RegisterSet({arm.lr, arm.r7}))))
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
72 # Reserve stack space for locals:
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
73 self.instructions.insert(2, makeIns(arm.subspsp_ins(arm.sp, arm.sp, arm.Imm7(self.stacksize))))
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
74 # Setup frame pointer:
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
75 self.instructions.insert(3, makeIns(arm.movregreg_ext_ins(arm.r7, arm.sp)))
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
76 # Stack grows downwards
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
77 self.instructions.append(makeIns(arm.addspsp_ins(arm.sp, arm.sp, arm.Imm7(self.stacksize))))
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
78 self.instructions.append(makeIns(arm.pop_ins(arm.RegisterSet({arm.pc, arm.r7}))))
276
Windel Bouwman
parents: 275
diff changeset
79 # Add constant literals:
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
80 self.instructions.append(makeIns(Alignment(4))) # Align at 4 bytes
276
Windel Bouwman
parents: 275
diff changeset
81 for ln, v in self.constants:
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
82 self.instructions.append(makeIns(arm.Label(ln)))
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
83 self.instructions.append(makeIns(arm.dcd_ins(v)))
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
84
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
85
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
86 class ArmInstructionSelector(InstructionSelector):
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
87 """ Instruction selector for the arm architecture """
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
88 def munchExpr(self, e):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
89 if isinstance(e, ir.Alloc):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
90 return 0
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
91 elif isinstance(e, ir.Binop) and e.operation == '+' and \
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
92 isinstance(e.b, ir.Const) and e.b.value < 8:
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
93 a = self.munchExpr(e.a)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
94 d = self.newTmp()
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
95 c = Imm3(e.b.value)
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
96 self.emit(arm.addregregimm3_ins, others=[c], dst=[d], src=[a])
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
97 return d
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
98 elif isinstance(e, ir.Binop) and e.operation == '+':
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
99 a = self.munchExpr(e.a)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
100 b = self.munchExpr(e.b)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
101 d = self.newTmp()
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
102 self.emit(arm.addregs_ins, dst=[d], src=[a, b])
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
103 return d
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
104 elif isinstance(e, ir.Binop) and e.operation == '-' and \
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
105 isinstance(e.b, ir.Const) and e.b.value < 8:
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
106 a = self.munchExpr(e.a)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
107 d = self.newTmp()
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
108 c = Imm3(e.b.value)
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
109 self.emit(arm.subregregimm3_ins, others=[c], dst=[d], src=[a])
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
110 return d
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
111 elif isinstance(e, ir.Binop) and e.operation == '-':
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
112 a = self.munchExpr(e.a)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
113 b = self.munchExpr(e.b)
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
114 d = self.newTmp()
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
115 self.emit(arm.subregs_ins, dst=[d], src=[a, b])
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
116 return d
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
117 elif isinstance(e, ir.Binop) and e.operation == '|':
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
118 a = self.munchExpr(e.a)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
119 b = self.munchExpr(e.b)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
120 d = self.newTmp()
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
121 self.move(d, a)
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
122 self.emit(arm.orrregs_ins, dst=[], src=[b, d])
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
123 return d
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
124 elif isinstance(e, ir.Binop) and e.operation == '<<':
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
125 a = self.munchExpr(e.a)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
126 b = self.munchExpr(e.b)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
127 d = self.newTmp()
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
128 self.move(d, a)
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
129 self.emit(arm.lslregs_ins, dst=[], src=[b, d]) # TODO: is d a source variable?
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
130 return d
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
131 elif isinstance(e, ir.Binop) and e.operation == '*':
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
132 a = self.munchExpr(e.a)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
133 b = self.munchExpr(e.b)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
134 d = self.newTmp()
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
135 self.move(d, a)
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
136 # this mul instruction has operands swapped:
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
137 self.emit(arm.mulregreg_ins, dst=[d], src=[b, d])
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
138 return d
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
139 elif isinstance(e, ir.Const) and e.value < 256:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
140 d = self.newTmp()
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
141 self.emit(arm.mov_imm8_ins, others=[arm.Imm8(e.value)], dst=[d])
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
142 return d
276
Windel Bouwman
parents: 275
diff changeset
143 elif isinstance(e, ir.Const) and e.value < (2**31):
Windel Bouwman
parents: 275
diff changeset
144 d = self.newTmp()
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
145 ln = LabelRef(self.frame.addConstant(e.value))
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
146 self.emit(arm.ldr_pcrel, others=[ln], dst=[d])
276
Windel Bouwman
parents: 275
diff changeset
147 return d
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
148 elif isinstance(e, ir.Mem) and isinstance(e.e, ir.Binop) and \
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
149 e.e.operation == '+' and isinstance(e.e.b, ir.Const):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
150 base = self.munchExpr(e.e.a)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
151 d = self.newTmp()
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
152 c = e.e.b.value
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
153 self.emit(arm.loadimm5_ins, others=[c], src=[base], dst=[d])
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
154 return d
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
155 elif isinstance(e, ir.Mem):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
156 # Load from memory
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
157 base = self.munchExpr(e.e)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
158 d = self.newTmp()
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
159 self.emit(arm.loadimm5_ins, others=[0], src=[base], dst=[d])
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
160 return d
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
161 elif isinstance(e, ir.Temp):
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
162 return e
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 270
diff changeset
163 elif isinstance(e, ir.Call):
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
164 # Move arguments into proper locations:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
165 reguses = []
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
166 for i, a in enumerate(e.arguments):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
167 loc = self.frame.argLoc(i)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
168 m = ir.Move(loc, a)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
169 self.munchStm(m)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
170 if isinstance(loc, ir.Temp):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
171 reguses.append(loc)
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
172 self.emit(arm.bl_ins(LabelRef(e.f.name)), src=reguses, dst=[self.frame.rv])
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
173 d = self.newTmp()
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
174 self.move(d, self.frame.rv)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
175 return d
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
176 else:
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 270
diff changeset
177 raise NotImplementedError('Expr --> {}'.format(e))
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
178
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
179 def munchStm(self, s):
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
180 if isinstance(s, ir.Terminator):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
181 pass
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
182 elif isinstance(s, ir.Move) and isinstance(s.dst, ir.Mem) and \
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
183 isinstance(s.dst.e, ir.Binop) and s.dst.e.operation == '+' and \
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
184 isinstance(s.dst.e.b, ir.Const):
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
185 a = self.munchExpr(s.dst.e.a)
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
186 val = self.munchExpr(s.src)
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
187 c = s.dst.e.b.value
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
188 self.emit(arm.storeimm5_ins, others=[c], src=[a, val])
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
189 elif isinstance(s, ir.Move) and isinstance(s.dst, ir.Mem):
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
190 memloc = self.munchExpr(s.dst.e)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
191 val = self.munchExpr(s.src)
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
192 self.emit(arm.storeimm5_ins, others=[0], src=[memloc, val])
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
193 elif isinstance(s, ir.Move) and isinstance(s.dst, ir.Temp):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
194 val = self.munchExpr(s.src)
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
195 dreg = s.dst
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
196 self.move(dreg, val)
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
197 elif isinstance(s, ir.Exp):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
198 # Generate expression code and discard the result.
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
199 x = self.munchExpr(s.e)
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
200 self.emit(Nop(), src=[x])
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
201 elif isinstance(s, ir.Jump):
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
202 tgt = self.targets[s.target]
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
203 self.emit(arm.b_ins(LabelRef(s.target.name)), jumps=[tgt])
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
204 elif isinstance(s, ir.CJump):
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
205 a = self.munchExpr(s.a)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
206 b = self.munchExpr(s.b)
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
207 self.emit(arm.cmp_ins, src=[a, b])
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
208 ntgt = self.targets[s.lab_no]
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
209 ytgt = self.targets[s.lab_yes]
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
210 jmp_ins = makeIns(arm.b_ins(LabelRef(s.lab_no.name)), jumps=[ntgt])
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
211 opnames = {'<': arm.blt_ins, '>':arm.bgt_ins, '==':arm.beq_ins}
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
212 op = opnames[s.cond](LabelRef(s.lab_yes.name))
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
213 self.emit(op, jumps=[ytgt, jmp_ins]) # Explicitely add fallthrough
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
214 self.emit2(jmp_ins)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
215 else:
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
216 raise NotImplementedError('Stmt --> {}'.format(s))
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
217
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
218 def move(self, dst, src):
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
219 self.emit(arm.movregreg_ext_ins, src=[src], dst=[dst])