342
|
1
|
|
2 from ..basetarget import Target
|
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
|
354
|
7 from .instructions import Dcd, Mov, Add, Sub, Orr1, Mul, Mov2, Add1, Mul1
|
352
|
8 from .instructions import B, Bl, Ble, Bgt, Beq, Blt, Cmp, Cmp2
|
354
|
9 from .instructions import Push, Pop, Str, Ldr, Ldr3, Str1, Ldr1, Adr
|
346
|
10 from .selector import ArmInstructionSelector
|
|
11 from .frame import ArmFrame
|
342
|
12
|
345
|
13 class ArmTarget(Target):
|
342
|
14 def __init__(self):
|
346
|
15 super().__init__('arm')
|
|
16 self.make_parser()
|
|
17 self.ins_sel = ArmInstructionSelector()
|
|
18 self.FrameClass = ArmFrame
|
342
|
19
|
346
|
20 self.add_lowering(Ldr3, lambda im: Ldr3(im.dst[0], im.others[0]))
|
|
21 self.add_lowering(Str1, lambda im: Str1(im.src[1], im.src[0], im.others[0]))
|
|
22 self.add_lowering(Ldr1, lambda im: Ldr1(im.dst[0], im.src[0], im.others[0]))
|
354
|
23 self.add_lowering(Adr, lambda im: Adr(im.dst[0], im.others[0]))
|
346
|
24 self.add_lowering(Mov2, lambda im: Mov2(im.dst[0], im.src[0]))
|
352
|
25 self.add_lowering(Cmp2, lambda im: Cmp2(im.src[0], im.src[1]))
|
|
26 self.add_lowering(Add1, lambda im: Add1(im.dst[0], im.src[0], im.src[1]))
|
354
|
27 self.add_lowering(Mul1, lambda im: Mul1(im.dst[0], im.src[0], im.src[1]))
|
346
|
28
|
|
29 def make_parser(self):
|
342
|
30 # Assembly grammar:
|
|
31 self.add_keyword('r0')
|
|
32 self.add_keyword('r1')
|
|
33 self.add_keyword('r2')
|
|
34 self.add_keyword('r3')
|
|
35 self.add_keyword('r4')
|
|
36 self.add_keyword('r5')
|
|
37 self.add_keyword('r6')
|
|
38 self.add_keyword('r7')
|
345
|
39 self.add_keyword('r8')
|
|
40 self.add_keyword('r9')
|
|
41 self.add_keyword('r10')
|
|
42 self.add_keyword('r11')
|
|
43 self.add_keyword('r12')
|
346
|
44 self.add_keyword('sp')
|
|
45 self.add_keyword('lr')
|
|
46 self.add_keyword('pc')
|
342
|
47
|
|
48 self.add_rule('reg', ['r0'], lambda rhs: R0)
|
|
49 self.add_rule('reg', ['r1'], lambda rhs: R1)
|
|
50 self.add_rule('reg', ['r2'], lambda rhs: R2)
|
|
51 self.add_rule('reg', ['r3'], lambda rhs: R3)
|
|
52 self.add_rule('reg', ['r4'], lambda rhs: R4)
|
|
53 self.add_rule('reg', ['r5'], lambda rhs: R5)
|
|
54 self.add_rule('reg', ['r6'], lambda rhs: R6)
|
|
55 self.add_rule('reg', ['r7'], lambda rhs: R7)
|
345
|
56 self.add_rule('reg', ['r8'], lambda rhs: R8)
|
|
57 self.add_rule('reg', ['r9'], lambda rhs: R9)
|
|
58 self.add_rule('reg', ['r10'], lambda rhs: R10)
|
|
59 self.add_rule('reg', ['r11'], lambda rhs: R11)
|
|
60 self.add_rule('reg', ['r12'], lambda rhs: R12)
|
346
|
61 self.add_rule('reg', ['sp'], lambda rhs: SP)
|
|
62 self.add_rule('reg', ['lr'], lambda rhs: LR)
|
|
63 self.add_rule('reg', ['pc'], lambda rhs: PC)
|
342
|
64
|
346
|
65 self.add_keyword('dcd')
|
|
66 self.add_instruction(['dcd', 'imm32'],
|
|
67 lambda rhs: Dcd(rhs[1]))
|
342
|
68
|
345
|
69 self.add_keyword('mov')
|
346
|
70 self.add_instruction(['mov', 'reg', ',', 'imm32'],
|
342
|
71 lambda rhs: Mov(rhs[1], rhs[3]))
|
|
72
|
352
|
73 self.add_keyword('cmp')
|
|
74 self.add_instruction(['cmp', 'reg', ',', 'imm32'],
|
|
75 lambda rhs: Cmp(rhs[1], rhs[3]))
|
|
76 self.add_instruction(['cmp', 'reg', ',', 'reg'],
|
|
77 lambda rhs: Cmp(rhs[1], rhs[3]))
|
|
78
|
|
79 # Arithmatic:
|
345
|
80 self.add_keyword('add')
|
|
81 self.add_instruction(['add', 'reg', ',', 'reg', ',', 'imm32'],
|
|
82 lambda rhs: Add(rhs[1], rhs[3], rhs[5]))
|
|
83
|
|
84 self.add_instruction(['add', 'reg', ',', 'reg', ',', 'reg'],
|
|
85 lambda rhs: Add(rhs[1], rhs[3], rhs[5]))
|
|
86
|
|
87 self.add_keyword('sub')
|
|
88 self.add_instruction(['sub', 'reg', ',', 'reg', ',', 'imm32'],
|
|
89 lambda rhs: Sub(rhs[1], rhs[3], rhs[5]))
|
|
90
|
|
91 self.add_instruction(['sub', 'reg', ',', 'reg', ',', 'reg'],
|
|
92 lambda rhs: Sub(rhs[1], rhs[3], rhs[5]))
|
|
93
|
346
|
94 self.add_keyword('mul')
|
|
95 self.add_instruction(['mul', 'reg', ',', 'reg', ',', 'reg'],
|
|
96 lambda rhs: Mul(rhs[1], rhs[3], rhs[5]))
|
|
97
|
345
|
98 self.add_keyword('orr')
|
|
99 self.add_instruction(['orr', 'reg', ',', 'reg', ',', 'reg'],
|
|
100 lambda rhs: Orr1(rhs[1], rhs[3], rhs[5]))
|
|
101
|
|
102
|
|
103 # Jumping:
|
|
104 self.add_keyword('b')
|
|
105 self.add_instruction(['b', 'ID'], lambda rhs: B(rhs[1].val))
|
|
106 self.add_keyword('ble')
|
|
107 self.add_instruction(['ble', 'ID'], lambda rhs: Ble(rhs[1].val))
|
|
108 self.add_keyword('bgt')
|
|
109 self.add_instruction(['bgt', 'ID'], lambda rhs: Bgt(rhs[1].val))
|
|
110 self.add_keyword('beq')
|
|
111 self.add_instruction(['beq', 'ID'], lambda rhs: Beq(rhs[1].val))
|
346
|
112 self.add_keyword('blt')
|
|
113 self.add_instruction(['blt', 'ID'], lambda rhs: Blt(rhs[1].val))
|
345
|
114
|
|
115 self.add_keyword('bl')
|
|
116 self.add_instruction(['bl', 'ID'], lambda rhs: Bl(rhs[1].val))
|
346
|
117
|
|
118 # memory:
|
|
119 self.add_keyword('pop')
|
|
120 self.add_instruction(['pop', 'reg_list'], lambda rhs: Pop(rhs[1]))
|
|
121
|
|
122 self.add_keyword('push')
|
|
123 self.add_instruction(['push', 'reg_list'], lambda rhs: Push(rhs[1]))
|
|
124
|
|
125 self.add_keyword('ldr')
|
|
126 self.add_instruction(['ldr', 'reg', ',', '[', 'reg', ',', 'imm8', ']'],
|
|
127 lambda rhs: Ldr(rhs[1], rhs[4], rhs[6]))
|
|
128
|
350
|
129 self.add_instruction(['ldr', 'reg', ',', 'ID'],
|
|
130 lambda rhs: Ldr(rhs[1], rhs[3].val))
|
|
131
|
346
|
132 self.add_keyword('str')
|
|
133 self.add_instruction(['str', 'reg', ',', '[', 'reg', ',', 'imm8', ']'],
|
|
134 lambda rhs: Str(rhs[1], rhs[4], rhs[6]))
|
|
135
|
|
136 self.add_instruction(['str', 'reg', ',', '[', 'reg', ',', 'reg', ']'],
|
|
137 lambda rhs: Str(rhs[1], rhs[4], rhs[6]))
|
|
138
|
354
|
139 self.add_keyword('adr')
|
|
140 self.add_instruction(['adr', 'reg', ',', 'ID'],
|
|
141 lambda rhs: Adr(rhs[1], rhs[3].val))
|
|
142
|
346
|
143 # Register list grammar:
|
|
144 self.add_rule('reg_list', ['{', 'reg_list_inner', '}'],
|
|
145 lambda rhs: rhs[1])
|
|
146 self.add_rule('reg_list_inner', ['reg_or_range'],
|
|
147 lambda rhs: rhs[0])
|
|
148 self.add_rule('reg_list_inner', ['reg_or_range', ',', 'reg_list_inner'],
|
|
149 lambda rhs: rhs[0] | rhs[2])
|
|
150 self.add_rule('reg_or_range', ['reg'], lambda rhs: {rhs[0]})
|
|
151
|
|
152 self.add_rule('reg_or_range', ['reg', '-', 'reg'],
|
|
153 lambda rhs: register_range(rhs[0], rhs[2]))
|