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