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