annotate python/target/arminstructionselector.py @ 301:6753763d3bec

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