annotate python/target/arminstructionselector.py @ 318:e84047f29c78

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