342
|
1
|
364
|
2 from ..basetarget import Target, Label
|
345
|
3 from ..arm.registers import R0, R1, R2, R3, R4, R5, R6, R7
|
|
4 from ..arm.registers import R8, R9, R10, R11, R12, SP, LR, PC
|
342
|
5 from ..arm.registers import register_range
|
|
6
|
359
|
7 from .instructions import Dcd, Mov, Mov1, Add, Add2, Sub, Orr1, Mul, Mov2, Add1, Mul1
|
356
|
8 from .instructions import Lsr1, Lsl1, And1, Sub1
|
352
|
9 from .instructions import B, Bl, Ble, Bgt, Beq, Blt, Cmp, Cmp2
|
354
|
10 from .instructions import Push, Pop, Str, Ldr, Ldr3, Str1, Ldr1, Adr
|
362
|
11 from .instructions import Mcr, Mrc
|
375
|
12 from .instructions import LdrPseudo
|
346
|
13 from .selector import ArmInstructionSelector
|
|
14 from .frame import ArmFrame
|
342
|
15
|
345
|
16 class ArmTarget(Target):
|
342
|
17 def __init__(self):
|
346
|
18 super().__init__('arm')
|
|
19 self.make_parser()
|
|
20 self.ins_sel = ArmInstructionSelector()
|
|
21 self.FrameClass = ArmFrame
|
342
|
22
|
346
|
23 self.add_lowering(Ldr3, lambda im: Ldr3(im.dst[0], im.others[0]))
|
|
24 self.add_lowering(Str1, lambda im: Str1(im.src[1], im.src[0], im.others[0]))
|
|
25 self.add_lowering(Ldr1, lambda im: Ldr1(im.dst[0], im.src[0], im.others[0]))
|
354
|
26 self.add_lowering(Adr, lambda im: Adr(im.dst[0], im.others[0]))
|
346
|
27 self.add_lowering(Mov2, lambda im: Mov2(im.dst[0], im.src[0]))
|
352
|
28 self.add_lowering(Cmp2, lambda im: Cmp2(im.src[0], im.src[1]))
|
|
29 self.add_lowering(Add1, lambda im: Add1(im.dst[0], im.src[0], im.src[1]))
|
359
|
30 self.add_lowering(Add2, lambda im: Add2(im.dst[0], im.src[0], im.others[0]))
|
356
|
31 self.add_lowering(Sub1, lambda im: Sub1(im.dst[0], im.src[0], im.src[1]))
|
354
|
32 self.add_lowering(Mul1, lambda im: Mul1(im.dst[0], im.src[0], im.src[1]))
|
356
|
33 self.add_lowering(Lsr1, lambda im: Lsr1(im.dst[0], im.src[0], im.src[1]))
|
|
34 self.add_lowering(And1, lambda im: And1(im.dst[0], im.src[0], im.src[1]))
|
357
|
35 self.add_lowering(Mov1, lambda im: Mov1(im.dst[0], im.others[0]))
|
346
|
36
|
364
|
37 def emit_global(self, outs, lname):
|
|
38 outs.emit(Label(lname))
|
|
39 outs.emit(Dcd(0))
|
|
40
|
346
|
41 def make_parser(self):
|
342
|
42 # Assembly grammar:
|
|
43 self.add_keyword('r0')
|
|
44 self.add_keyword('r1')
|
|
45 self.add_keyword('r2')
|
|
46 self.add_keyword('r3')
|
|
47 self.add_keyword('r4')
|
|
48 self.add_keyword('r5')
|
|
49 self.add_keyword('r6')
|
|
50 self.add_keyword('r7')
|
345
|
51 self.add_keyword('r8')
|
|
52 self.add_keyword('r9')
|
|
53 self.add_keyword('r10')
|
|
54 self.add_keyword('r11')
|
|
55 self.add_keyword('r12')
|
346
|
56 self.add_keyword('sp')
|
|
57 self.add_keyword('lr')
|
|
58 self.add_keyword('pc')
|
342
|
59
|
|
60 self.add_rule('reg', ['r0'], lambda rhs: R0)
|
|
61 self.add_rule('reg', ['r1'], lambda rhs: R1)
|
|
62 self.add_rule('reg', ['r2'], lambda rhs: R2)
|
|
63 self.add_rule('reg', ['r3'], lambda rhs: R3)
|
|
64 self.add_rule('reg', ['r4'], lambda rhs: R4)
|
|
65 self.add_rule('reg', ['r5'], lambda rhs: R5)
|
|
66 self.add_rule('reg', ['r6'], lambda rhs: R6)
|
|
67 self.add_rule('reg', ['r7'], lambda rhs: R7)
|
345
|
68 self.add_rule('reg', ['r8'], lambda rhs: R8)
|
|
69 self.add_rule('reg', ['r9'], lambda rhs: R9)
|
|
70 self.add_rule('reg', ['r10'], lambda rhs: R10)
|
|
71 self.add_rule('reg', ['r11'], lambda rhs: R11)
|
|
72 self.add_rule('reg', ['r12'], lambda rhs: R12)
|
346
|
73 self.add_rule('reg', ['sp'], lambda rhs: SP)
|
|
74 self.add_rule('reg', ['lr'], lambda rhs: LR)
|
|
75 self.add_rule('reg', ['pc'], lambda rhs: PC)
|
342
|
76
|
346
|
77 self.add_keyword('dcd')
|
|
78 self.add_instruction(['dcd', 'imm32'],
|
|
79 lambda rhs: Dcd(rhs[1]))
|
342
|
80
|
345
|
81 self.add_keyword('mov')
|
346
|
82 self.add_instruction(['mov', 'reg', ',', 'imm32'],
|
342
|
83 lambda rhs: Mov(rhs[1], rhs[3]))
|
362
|
84 self.add_instruction(['mov', 'reg', ',', 'reg'],
|
|
85 lambda rhs: Mov(rhs[1], rhs[3]))
|
342
|
86
|
352
|
87 self.add_keyword('cmp')
|
|
88 self.add_instruction(['cmp', 'reg', ',', 'imm32'],
|
|
89 lambda rhs: Cmp(rhs[1], rhs[3]))
|
|
90 self.add_instruction(['cmp', 'reg', ',', 'reg'],
|
|
91 lambda rhs: Cmp(rhs[1], rhs[3]))
|
|
92
|
|
93 # Arithmatic:
|
345
|
94 self.add_keyword('add')
|
|
95 self.add_instruction(['add', 'reg', ',', 'reg', ',', 'imm32'],
|
|
96 lambda rhs: Add(rhs[1], rhs[3], rhs[5]))
|
|
97
|
|
98 self.add_instruction(['add', 'reg', ',', 'reg', ',', 'reg'],
|
|
99 lambda rhs: Add(rhs[1], rhs[3], rhs[5]))
|
|
100
|
|
101 self.add_keyword('sub')
|
|
102 self.add_instruction(['sub', 'reg', ',', 'reg', ',', 'imm32'],
|
|
103 lambda rhs: Sub(rhs[1], rhs[3], rhs[5]))
|
|
104
|
|
105 self.add_instruction(['sub', 'reg', ',', 'reg', ',', 'reg'],
|
|
106 lambda rhs: Sub(rhs[1], rhs[3], rhs[5]))
|
|
107
|
346
|
108 self.add_keyword('mul')
|
|
109 self.add_instruction(['mul', 'reg', ',', 'reg', ',', 'reg'],
|
|
110 lambda rhs: Mul(rhs[1], rhs[3], rhs[5]))
|
|
111
|
345
|
112 self.add_keyword('orr')
|
|
113 self.add_instruction(['orr', 'reg', ',', 'reg', ',', 'reg'],
|
|
114 lambda rhs: Orr1(rhs[1], rhs[3], rhs[5]))
|
|
115
|
356
|
116 self.add_keyword('and')
|
|
117 self.add_instruction(['and', 'reg', ',', 'reg', ',', 'reg'],
|
|
118 lambda rhs: And1(rhs[1], rhs[3], rhs[5]))
|
|
119
|
|
120 self.add_keyword('lsr')
|
|
121 self.add_instruction(['lsr', 'reg', ',', 'reg', ',', 'reg'],
|
|
122 lambda rhs: Lsr1(rhs[1], rhs[3], rhs[5]))
|
|
123
|
|
124 self.add_keyword('lsl')
|
|
125 self.add_instruction(['lsl', 'reg', ',', 'reg', ',', 'reg'],
|
|
126 lambda rhs: Lsl1(rhs[1], rhs[3], rhs[5]))
|
|
127
|
345
|
128
|
|
129 # Jumping:
|
|
130 self.add_keyword('b')
|
|
131 self.add_instruction(['b', 'ID'], lambda rhs: B(rhs[1].val))
|
|
132 self.add_keyword('ble')
|
|
133 self.add_instruction(['ble', 'ID'], lambda rhs: Ble(rhs[1].val))
|
|
134 self.add_keyword('bgt')
|
|
135 self.add_instruction(['bgt', 'ID'], lambda rhs: Bgt(rhs[1].val))
|
|
136 self.add_keyword('beq')
|
|
137 self.add_instruction(['beq', 'ID'], lambda rhs: Beq(rhs[1].val))
|
346
|
138 self.add_keyword('blt')
|
|
139 self.add_instruction(['blt', 'ID'], lambda rhs: Blt(rhs[1].val))
|
345
|
140
|
|
141 self.add_keyword('bl')
|
|
142 self.add_instruction(['bl', 'ID'], lambda rhs: Bl(rhs[1].val))
|
346
|
143
|
|
144 # memory:
|
|
145 self.add_keyword('pop')
|
|
146 self.add_instruction(['pop', 'reg_list'], lambda rhs: Pop(rhs[1]))
|
|
147
|
|
148 self.add_keyword('push')
|
|
149 self.add_instruction(['push', 'reg_list'], lambda rhs: Push(rhs[1]))
|
|
150
|
|
151 self.add_keyword('ldr')
|
|
152 self.add_instruction(['ldr', 'reg', ',', '[', 'reg', ',', 'imm8', ']'],
|
|
153 lambda rhs: Ldr(rhs[1], rhs[4], rhs[6]))
|
|
154
|
350
|
155 self.add_instruction(['ldr', 'reg', ',', 'ID'],
|
|
156 lambda rhs: Ldr(rhs[1], rhs[3].val))
|
|
157
|
375
|
158 # This is a pseudo instruction:
|
|
159 self.add_instruction(['ldr', 'reg', ',', '=', 'ID'],
|
|
160 lambda rhs: LdrPseudo(rhs[1], rhs[4].val))
|
|
161
|
346
|
162 self.add_keyword('str')
|
|
163 self.add_instruction(['str', 'reg', ',', '[', 'reg', ',', 'imm8', ']'],
|
|
164 lambda rhs: Str(rhs[1], rhs[4], rhs[6]))
|
|
165
|
|
166 self.add_instruction(['str', 'reg', ',', '[', 'reg', ',', 'reg', ']'],
|
|
167 lambda rhs: Str(rhs[1], rhs[4], rhs[6]))
|
|
168
|
354
|
169 self.add_keyword('adr')
|
|
170 self.add_instruction(['adr', 'reg', ',', 'ID'],
|
|
171 lambda rhs: Adr(rhs[1], rhs[3].val))
|
|
172
|
375
|
173
|
346
|
174 # Register list grammar:
|
|
175 self.add_rule('reg_list', ['{', 'reg_list_inner', '}'],
|
|
176 lambda rhs: rhs[1])
|
|
177 self.add_rule('reg_list_inner', ['reg_or_range'],
|
|
178 lambda rhs: rhs[0])
|
|
179 self.add_rule('reg_list_inner', ['reg_or_range', ',', 'reg_list_inner'],
|
|
180 lambda rhs: rhs[0] | rhs[2])
|
|
181 self.add_rule('reg_or_range', ['reg'], lambda rhs: {rhs[0]})
|
|
182
|
|
183 self.add_rule('reg_or_range', ['reg', '-', 'reg'],
|
|
184 lambda rhs: register_range(rhs[0], rhs[2]))
|
362
|
185
|
|
186 # Add MCR and MRC (co-processor)
|
|
187 for i in range(16):
|
|
188 creg = 'c{}'.format(i)
|
|
189 self.add_keyword(creg)
|
|
190 self.add_rule('coreg', [creg], i)
|
|
191
|
|
192 for i in range(8, 16):
|
|
193 px = 'p{}'.format(i)
|
|
194 self.add_keyword(px)
|
|
195 # Ran into trouble when using i inside lambda function:
|
|
196 # When using inside lambda (as a closure), i is bound to the latest
|
|
197 # value (15)
|
|
198 self.add_rule('coproc', [px], i)
|
|
199
|
|
200 self.add_keyword('mcr')
|
|
201 self.add_instruction(['mcr', 'coproc', ',', 'imm3', ',', 'reg', ',', 'coreg', ',', 'coreg', ',', 'imm3'],
|
|
202 lambda rhs: Mcr(rhs[1], rhs[3], rhs[5], rhs[7], rhs[9], rhs[11]))
|
|
203
|
|
204 self.add_keyword('mrc')
|
|
205 self.add_instruction(['mrc', 'coproc', ',', 'imm3', ',', 'reg', ',', 'coreg', ',', 'coreg', ',', 'imm3'],
|
|
206 lambda rhs: Mrc(rhs[1], rhs[3], rhs[5], rhs[7], rhs[9], rhs[11]))
|