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