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