261
|
1 import struct
|
342
|
2 from ..basetarget import Register, Instruction, Target, Label, Alignment
|
346
|
3 from .instructions import Add2, Sub, Sub3, Add3, Cmp, Lsl, Orr, Add, Cmp2, Sub2, Add2, Mul, And
|
342
|
4 from .instructions import Dcd, Pop, Push, Yield, Mov2, Mov3
|
|
5 from .instructions import B, Bl, Bne, Beq, Blt, Bgt
|
346
|
6 from .instructions import Ldr, Str2, Ldr2, Str1, Ldr1, Ldr3
|
202
|
7
|
346
|
8 from .frame import ArmFrame
|
322
|
9 from .arminstructionselector import ArmInstructionSelector
|
342
|
10 from ..arm.registers import R0, R1, R2, R3, R4, R5, R6, R7, SP, LR, PC
|
|
11 from ..arm.registers import register_range
|
341
|
12
|
290
|
13
|
|
14 """ ARM target description. """
|
287
|
15
|
218
|
16 # TODO: encode this in DSL (domain specific language)
|
275
|
17 # TBD: is this required?
|
292
|
18 # TODO: make a difference between armv7 and armv5?
|
219
|
19
|
341
|
20 thumb_assembly_rules = []
|
|
21 def add_rule(rhs, f):
|
|
22 thumb_assembly_rules.append(('instruction', rhs, f))
|
277
|
23
|
341
|
24
|
|
25 class ThumbTarget(Target):
|
292
|
26 def __init__(self):
|
341
|
27 super().__init__('thumb')
|
322
|
28 self.ins_sel = ArmInstructionSelector()
|
|
29 self.FrameClass = ArmFrame
|
341
|
30 self.add_rules()
|
|
31
|
346
|
32 # Add lowering options:
|
|
33 self.add_lowering(Str2, lambda im: Str2(im.src[1], im.src[0], im.others[0]))
|
|
34 self.add_lowering(Ldr2, lambda im: Ldr2(im.dst[0], im.src[0], im.others[0]))
|
|
35 self.add_lowering(Ldr3, lambda im: Ldr3(im.dst[0], im.others[0]))
|
|
36 self.add_lowering(Mov3, lambda im: Mov3(im.dst[0], im.others[0]))
|
|
37 self.add_lowering(Add2, lambda im: Add2(im.dst[0], im.src[0], im.others[0]))
|
|
38 self.add_lowering(Sub2, lambda im: Sub2(im.dst[0], im.src[0], im.others[0]))
|
|
39 self.add_lowering(Mov2, lambda im: Mov2(im.dst[0], im.src[0]))
|
|
40 self.add_lowering(Add3, lambda im: Add3(im.dst[0], im.src[0], im.src[1]))
|
|
41 self.add_lowering(Sub3, lambda im: Sub3(im.dst[0], im.src[0], im.src[1]))
|
|
42 self.add_lowering(Mul, lambda im: Mul(im.src[0], im.dst[0]))
|
|
43 self.add_lowering(And, lambda im: And(im.src[0], im.src[1]))
|
|
44 self.add_lowering(Orr, lambda im: Orr(im.src[0], im.src[1]))
|
|
45 self.add_lowering(Lsl, lambda im: Lsl(im.src[0], im.src[1]))
|
|
46 self.add_lowering(Cmp, lambda im: Cmp(im.src[0], im.src[1]))
|
|
47
|
341
|
48 def add_rules(self):
|
|
49
|
|
50 # Add instructions:
|
|
51 self.add_keyword('dcd')
|
|
52 self.add_instruction(['dcd', 'imm32'], lambda rhs: Dcd(rhs[1]))
|
|
53
|
|
54 self.add_keyword('mov')
|
|
55 self.add_instruction(['mov', 'reg8', ',', 'reg8'],
|
|
56 lambda rhs: Mov2(rhs[1], rhs[3]))
|
|
57
|
|
58 self.add_instruction(['mov', 'reg8', ',', 'imm8'],
|
|
59 lambda rhs: Mov3(rhs[1], rhs[3]))
|
|
60
|
|
61 self.add_keyword('add')
|
|
62 self.add_instruction(['add', 'reg8', ',', 'reg8', ',', 'imm3'],
|
|
63 lambda rhs: Add2(rhs[1], rhs[3], rhs[5]))
|
|
64
|
|
65 self.add_instruction(['add', 'reg8', ',', 'reg8', ',', 'reg8'],
|
|
66 lambda rhs: Add3(rhs[1], rhs[3], rhs[5]))
|
|
67
|
|
68 self.add_keyword('sub')
|
|
69 self.add_instruction(['sub', 'reg8', ',', 'reg8', ',', 'imm3'],
|
|
70 lambda rhs: Sub(rhs[1], rhs[3], rhs[5]))
|
|
71
|
|
72 self.add_instruction(['sub', 'sp', ',', 'sp', ',', 'imm8'],
|
|
73 lambda rhs: Sub(SP, SP, rhs[5]))
|
|
74
|
|
75 self.add_instruction(['add', 'sp', ',', 'sp', ',', 'imm8'],
|
342
|
76 lambda rhs: Add(SP, SP, rhs[5]))
|
341
|
77
|
|
78 self.add_keyword('cmp')
|
|
79 self.add_instruction(['cmp', 'reg8', ',', 'reg8'],
|
|
80 lambda rhs: Cmp(rhs[1], rhs[3]))
|
342
|
81 self.add_instruction(['cmp', 'reg8', ',', 'imm8'],
|
|
82 lambda rhs: Cmp2(rhs[1], rhs[3]))
|
341
|
83
|
|
84 self.add_keyword('lsl')
|
|
85 self.add_instruction(['lsl', 'reg8', ',', 'reg8'],
|
|
86 lambda rhs: Lsl(rhs[1], rhs[3]))
|
|
87
|
|
88 self.add_keyword('str')
|
|
89 self.add_instruction(['str', 'reg8', ',', '[', 'reg8', '+', 'imm5', ']'],
|
|
90 lambda rhs: Str2(rhs[1], rhs[4], rhs[6]))
|
|
91
|
|
92 self.add_keyword('ldr')
|
|
93 self.add_instruction(['ldr', 'reg8', ',', '[', 'reg8', '+', 'imm5', ']'],
|
|
94 lambda rhs: Ldr2(rhs[1], rhs[4], rhs[6]))
|
|
95
|
|
96 self.add_instruction(['str', 'reg8', ',', '[', 'sp', '+', 'imm8', ']'],
|
|
97 lambda rhs: Str1(rhs[1], rhs[6]))
|
|
98
|
|
99 self.add_instruction(['ldr', 'reg8', ',', '[', 'sp', '+', 'imm8', ']'],
|
|
100 lambda rhs: Ldr1(rhs[1], rhs[6]))
|
|
101
|
|
102 self.add_keyword('pop')
|
|
103 self.add_instruction(['pop', 'reg_list'], lambda rhs: Pop(rhs[1]))
|
|
104 self.add_keyword('push')
|
|
105 self.add_instruction(['push', 'reg_list'], lambda rhs: Push(rhs[1]))
|
|
106
|
|
107 self.add_keyword('yield')
|
|
108 self.add_instruction(['yield'], lambda rhs: Yield())
|
|
109
|
|
110 self.add_keyword('b')
|
|
111 self.add_keyword('bl')
|
|
112 self.add_instruction(['b', 'ID'], lambda rhs: B(rhs[1].val))
|
|
113 self.add_instruction(['bl', 'ID'], lambda rhs: Bl(rhs[1].val))
|
|
114 self.add_keyword('beq')
|
|
115 self.add_keyword('bne')
|
|
116 self.add_keyword('blt')
|
|
117 self.add_keyword('bgt')
|
|
118 self.add_instruction(['beq', 'ID'], lambda rhs: Beq(rhs[1].val))
|
|
119 self.add_instruction(['bne', 'ID'], lambda rhs: Bne(rhs[1].val))
|
|
120 self.add_instruction(['blt', 'ID'], lambda rhs: Blt(rhs[1].val))
|
|
121 self.add_instruction(['bgt', 'ID'], lambda rhs: Bgt(rhs[1].val))
|
|
122
|
|
123 self.add_keyword('align')
|
|
124 self.add_instruction(['align', 'imm8'], lambda rhs: Alignment(rhs[1]))
|
|
125
|
|
126 self.add_instruction(['ldr', 'reg8', ',', 'ID'],
|
|
127 lambda rhs: Ldr(rhs[1], rhs[3].val))
|
|
128
|
|
129 # Additional rules:
|
|
130
|
|
131 # Register list grammar:
|
|
132 self.add_rule('reg_list', ['{', 'reg_list_inner', '}'],
|
|
133 lambda rhs: rhs[1])
|
|
134 self.add_rule('reg_list_inner', ['reg_or_range'],
|
|
135 lambda rhs: rhs[0])
|
|
136 self.add_rule('reg_list_inner', ['reg_or_range', ',', 'reg_list_inner'],
|
|
137 lambda rhs: rhs[0] | rhs[2])
|
|
138 self.add_rule('reg_or_range', ['reg8'], lambda rhs: {rhs[0]})
|
|
139 self.add_rule('reg_or_range', ['lr'], lambda rhs: {LR})
|
|
140 self.add_rule('reg_or_range', ['pc'], lambda rhs: {PC})
|
|
141
|
|
142 self.add_rule('reg_or_range', ['reg8', '-', 'reg8'],
|
|
143 lambda rhs: register_range(rhs[0], rhs[2]))
|
|
144
|
|
145 self.add_keyword('r0')
|
|
146 self.add_keyword('r1')
|
|
147 self.add_keyword('r2')
|
|
148 self.add_keyword('r3')
|
|
149 self.add_keyword('r4')
|
|
150 self.add_keyword('r5')
|
|
151 self.add_keyword('r6')
|
|
152 self.add_keyword('r7')
|
|
153 self.add_keyword('sp')
|
|
154 self.add_keyword('lr')
|
|
155 self.add_keyword('pc')
|
|
156 self.add_rule('reg8', ['r0'], lambda rhs: R0)
|
|
157 self.add_rule('reg8', ['r1'], lambda rhs: R1)
|
|
158 self.add_rule('reg8', ['r2'], lambda rhs: R2)
|
|
159 self.add_rule('reg8', ['r3'], lambda rhs: R3)
|
|
160 self.add_rule('reg8', ['r4'], lambda rhs: R4)
|
|
161 self.add_rule('reg8', ['r5'], lambda rhs: R5)
|
|
162 self.add_rule('reg8', ['r6'], lambda rhs: R6)
|
|
163 self.add_rule('reg8', ['r7'], lambda rhs: R7)
|
|
164
|