annotate python/target/arminstructionselector.py @ 316:56e6ff84f646

Fixed burn led demo
author Windel Bouwman
date Sat, 21 Dec 2013 13:13:26 +0100
parents 0615b5308710
children e84047f29c78
rev   line source
301
6753763d3bec merge codegen into ppci package
Windel Bouwman
parents: 300
diff changeset
1 from ppci import ir
6753763d3bec merge codegen into ppci package
Windel Bouwman
parents: 300
diff changeset
2 from ppci.irmach import AbstractInstruction as makeIns
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
3 from .basetarget import Label, Comment, Alignment, LabelRef, DebugInfo, Nop
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
4 from .instructionselector import InstructionSelector
305
0615b5308710 Updated docs
Windel Bouwman
parents: 301
diff changeset
5 from .arminstructions import Orr, Lsl, Str2, Ldr2, Ldr3
0615b5308710 Updated docs
Windel Bouwman
parents: 301
diff changeset
6 from .arminstructions import B, Bl, Bgt, Blt, Beq, Bne
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
7 from .arminstructions import Mov2, Mov3
300
Windel Bouwman
parents: 292
diff changeset
8 from .arminstructions import Add, Sub, Cmp, Sub2, Add2, Mul
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
9 from .basetarget import Imm8, Imm7, Imm3
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
10
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
11
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
12 class ArmInstructionSelector(InstructionSelector):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
13 """ Instruction selector for the arm architecture """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
14 def munchExpr(self, e):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
15 if isinstance(e, ir.Alloc):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
16 return 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
17 elif isinstance(e, ir.Binop) and e.operation == '+' and \
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
18 isinstance(e.b, ir.Const) and e.b.value < 8:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
19 a = self.munchExpr(e.a)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
20 d = self.newTmp()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
21 c = Imm3(e.b.value)
300
Windel Bouwman
parents: 292
diff changeset
22 self.emit(Add2, others=[c], dst=[d], src=[a])
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
23 return d
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
24 elif isinstance(e, ir.Binop) and e.operation == '+':
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
25 a = self.munchExpr(e.a)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
26 b = self.munchExpr(e.b)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
27 d = self.newTmp()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
28 self.emit(Add, dst=[d], src=[a, b])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
29 return d
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
30 elif isinstance(e, ir.Binop) and e.operation == '-' and \
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
31 isinstance(e.b, ir.Const) and e.b.value < 8:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
32 a = self.munchExpr(e.a)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
33 d = self.newTmp()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
34 c = Imm3(e.b.value)
300
Windel Bouwman
parents: 292
diff changeset
35 self.emit(Sub2, others=[c], dst=[d], src=[a])
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
36 return d
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
37 elif isinstance(e, ir.Binop) and e.operation == '-':
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
38 a = self.munchExpr(e.a)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
39 b = self.munchExpr(e.b)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
40 d = self.newTmp()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
41 self.emit(Sub, dst=[d], src=[a, b])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
42 return d
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
43 elif isinstance(e, ir.Binop) and e.operation == '|':
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
44 a = self.munchExpr(e.a)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
45 b = self.munchExpr(e.b)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
46 d = self.newTmp()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
47 self.move(d, a)
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 305
diff changeset
48 self.emit(Orr, dst=[], src=[d, b])
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
49 return d
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
50 elif isinstance(e, ir.Binop) and e.operation == '<<':
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
51 a = self.munchExpr(e.a)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
52 b = self.munchExpr(e.b)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
53 d = self.newTmp()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
54 self.move(d, a)
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 305
diff changeset
55 self.emit(Lsl, dst=[], src=[d, b]) # TODO: is d a source variable?
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
56 return d
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
57 elif isinstance(e, ir.Binop) and e.operation == '*':
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
58 a = self.munchExpr(e.a)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
59 b = self.munchExpr(e.b)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
60 d = self.newTmp()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
61 self.move(d, a)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
62 # this mul instruction has operands swapped:
300
Windel Bouwman
parents: 292
diff changeset
63 self.emit(Mul, dst=[d], src=[b, d])
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
64 return d
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
65 elif isinstance(e, ir.Const) and e.value < 256:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
66 d = self.newTmp()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
67 self.emit(Mov3, others=[Imm8(e.value)], dst=[d])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
68 return d
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
69 elif isinstance(e, ir.Const) and e.value < (2**31):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
70 d = self.newTmp()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
71 ln = LabelRef(self.frame.addConstant(e.value))
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
72 self.emit(Ldr3, others=[ln], dst=[d])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
73 return d
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
74 elif isinstance(e, ir.Mem) and isinstance(e.e, ir.Binop) and \
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
75 e.e.operation == '+' and isinstance(e.e.b, ir.Const):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
76 base = self.munchExpr(e.e.a)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
77 d = self.newTmp()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
78 c = e.e.b.value
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
79 self.emit(Ldr2, others=[c], src=[base], dst=[d])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
80 return d
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
81 elif isinstance(e, ir.Mem):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
82 # Load from memory
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
83 base = self.munchExpr(e.e)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
84 d = self.newTmp()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
85 self.emit(Ldr2, others=[0], src=[base], dst=[d])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
86 return d
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
87 elif isinstance(e, ir.Temp):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
88 return e
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
89 elif isinstance(e, ir.Call):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
90 # Move arguments into proper locations:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
91 reguses = []
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
92 for i, a in enumerate(e.arguments):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
93 loc = self.frame.argLoc(i)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
94 m = ir.Move(loc, a)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
95 self.munchStm(m)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
96 if isinstance(loc, ir.Temp):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
97 reguses.append(loc)
305
0615b5308710 Updated docs
Windel Bouwman
parents: 301
diff changeset
98 self.emit(Bl(LabelRef(e.f)), src=reguses, dst=[self.frame.rv])
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
99 d = self.newTmp()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
100 self.move(d, self.frame.rv)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
101 return d
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
102 else:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
103 raise NotImplementedError('Expr --> {}'.format(e))
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
104
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
105 def munchStm(self, s):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
106 if isinstance(s, ir.Terminator):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
107 pass
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
108 elif isinstance(s, ir.Move) and isinstance(s.dst, ir.Mem) and \
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
109 isinstance(s.dst.e, ir.Binop) and s.dst.e.operation == '+' and \
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
110 isinstance(s.dst.e.b, ir.Const):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
111 a = self.munchExpr(s.dst.e.a)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
112 val = self.munchExpr(s.src)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
113 c = s.dst.e.b.value
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
114 self.emit(Str2, others=[c], src=[a, val])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
115 elif isinstance(s, ir.Move) and isinstance(s.dst, ir.Mem):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
116 memloc = self.munchExpr(s.dst.e)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
117 val = self.munchExpr(s.src)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
118 self.emit(Str2, others=[0], src=[memloc, val])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
119 elif isinstance(s, ir.Move) and isinstance(s.dst, ir.Temp):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
120 val = self.munchExpr(s.src)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
121 dreg = s.dst
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
122 self.move(dreg, val)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
123 elif isinstance(s, ir.Exp):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
124 # Generate expression code and discard the result.
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
125 x = self.munchExpr(s.e)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
126 self.emit(Nop(), src=[x])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
127 elif isinstance(s, ir.Jump):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
128 tgt = self.targets[s.target]
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
129 self.emit(B(LabelRef(s.target.name)), jumps=[tgt])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
130 elif isinstance(s, ir.CJump):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
131 a = self.munchExpr(s.a)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
132 b = self.munchExpr(s.b)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
133 self.emit(Cmp, src=[a, b])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
134 ntgt = self.targets[s.lab_no]
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
135 ytgt = self.targets[s.lab_yes]
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
136 jmp_ins = makeIns(B(LabelRef(s.lab_no.name)), jumps=[ntgt])
305
0615b5308710 Updated docs
Windel Bouwman
parents: 301
diff changeset
137 opnames = {'<': Blt, '>':Bgt, '==':Beq, '!=':Bne}
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
138 op = opnames[s.cond](LabelRef(s.lab_yes.name))
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
139 self.emit(op, jumps=[ytgt, jmp_ins]) # Explicitely add fallthrough
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
140 self.emit2(jmp_ins)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
141 else:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
142 raise NotImplementedError('Stmt --> {}'.format(s))
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
143
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
144 def move(self, dst, src):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
145 self.emit(Mov2, src=[src], dst=[dst])