205
|
1 import struct, types
|
202
|
2 from target import Register, Instruction, Target
|
|
3 from asmnodes import ASymbol, ANumber
|
|
4 from ppci import CompilerError
|
205
|
5 import ir
|
202
|
6
|
|
7 def u16(h):
|
|
8 return struct.pack('<H', h)
|
|
9
|
205
|
10 def u32(x):
|
|
11 return struct.pack('<I', x)
|
|
12
|
202
|
13 armtarget = Target('arm')
|
|
14
|
|
15 class ArmReg(Register):
|
|
16 def __init__(self, num, name):
|
|
17 super().__init__(name)
|
|
18 self.num = num
|
|
19
|
|
20 class ArmImm:
|
|
21 def __init__(self, i):
|
|
22 self.i = i
|
|
23
|
203
|
24 class RegOp:
|
|
25 def __init__(self, num):
|
|
26 assert num < 8
|
|
27 self.num = num
|
|
28
|
|
29 @classmethod
|
|
30 def Create(cls, vop):
|
|
31 if type(vop) is ASymbol:
|
|
32 name = vop.name
|
|
33 regs = {}
|
|
34 for r in armtarget.registers:
|
|
35 regs[r.name] = r
|
|
36 if name in regs:
|
|
37 r = regs[name]
|
|
38 return cls(r.num)
|
|
39
|
205
|
40 class Label:
|
|
41 def __init__(self, name):
|
|
42 self.name = name
|
|
43
|
|
44 @classmethod
|
|
45 def Create(cls, vop):
|
|
46 if type(vop) is ASymbol:
|
|
47 name = vop.name
|
|
48 return cls(name)
|
|
49
|
203
|
50 class Imm8:
|
|
51 def __init__(self, imm):
|
|
52 assert imm < 256
|
|
53 self.imm = imm
|
|
54
|
|
55 @classmethod
|
|
56 def Create(cls, vop):
|
|
57 if type(vop) is ANumber and vop.number < 256:
|
|
58 return cls(vop.number)
|
|
59
|
|
60 class Imm3:
|
|
61 def __init__(self, imm):
|
|
62 assert imm < 8
|
|
63 assert type(imm) is int
|
|
64 self.imm = imm
|
|
65
|
|
66 @classmethod
|
|
67 def Create(cls, vop):
|
|
68 if type(vop) is ANumber and vop.number < 8:
|
|
69 return cls(vop.number)
|
|
70
|
205
|
71 class RegisterSet:
|
|
72 def __init__(self, regs):
|
|
73 pass
|
|
74
|
202
|
75 # 8 bit registers:
|
205
|
76 r0 = ArmReg(0, 'r0')
|
|
77 armtarget.registers.append(r0)
|
202
|
78 r4 = ArmReg(4, 'r4')
|
|
79 armtarget.registers.append(r4)
|
203
|
80 r5 = ArmReg(5, 'r5')
|
|
81 armtarget.registers.append(r5)
|
|
82 r6 = ArmReg(6, 'r6')
|
|
83 armtarget.registers.append(r6)
|
|
84 r7 = ArmReg(7, 'r7')
|
|
85 armtarget.registers.append(r7)
|
202
|
86
|
|
87 class ArmInstruction(Instruction):
|
|
88 pass
|
|
89
|
|
90 @armtarget.instruction
|
|
91 class ldr_ins(ArmInstruction):
|
|
92 mnemonic = 'ldr'
|
|
93 opcode = 1337
|
205
|
94 irpattern = 'todo'
|
202
|
95
|
205
|
96 @armtarget.instruction
|
|
97 class dcd_ins(ArmInstruction):
|
|
98 mnemonic = 'dcd'
|
|
99 def __init__(self, expr):
|
|
100 self.expr = expr
|
|
101 def encode(self):
|
|
102 return u32(self.expr)
|
202
|
103
|
203
|
104 class Operand2:
|
|
105 def __init__(self, expr):
|
|
106 if type(expr) is ANumber:
|
|
107 pass
|
|
108 pass
|
|
109
|
202
|
110 @armtarget.instruction
|
|
111 class mov_ins(ArmInstruction):
|
|
112 """ mov Rd, imm8, move immediate value into register """
|
|
113 mnemonic = 'mov'
|
203
|
114 opcode = 4 # 00100 Rd(3) imm8
|
|
115 operands = (RegOp, Imm8)
|
205
|
116 irpattern = ir.ImmLoad
|
203
|
117 def __init__(self, rd, imm):
|
|
118 self.imm = imm.imm
|
|
119 self.r = rd.num
|
205
|
120
|
|
121 @classmethod
|
|
122 def FromIr(cls, ir_ins):
|
|
123 pass
|
|
124
|
202
|
125 def encode(self):
|
|
126 rd = self.r
|
|
127 opcode = self.opcode
|
|
128 imm8 = self.imm
|
|
129 h = (opcode << 11) | (rd << 8) | imm8
|
|
130 return u16(h)
|
|
131
|
203
|
132 @armtarget.instruction
|
|
133 class movregreg_ins(ArmInstruction):
|
|
134 """ mov Rd, Rm """
|
|
135 mnemonic = 'mov'
|
|
136 opcode = 8 # 01000 Rd(3) imm8
|
|
137 operands = (RegOp, RegOp)
|
|
138 def __init__(self, rd, rm):
|
|
139 self.rd = rd
|
|
140 self.rm = rm
|
|
141 def encode(self):
|
|
142 rd = self.rd.num
|
|
143 D = (rd & 0x8) >> 3
|
|
144 assert D < 2
|
|
145 rd = rd & 0x7
|
|
146 rm = self.rm.num
|
|
147 assert rm < 16
|
|
148 opcode = self.opcode
|
|
149 h = (opcode << 11) | (3 << 9) | (D << 7) | (rm << 3) | rd
|
|
150 return u16(h)
|
|
151
|
|
152 @armtarget.instruction
|
|
153 class addregregimm3_ins(ArmInstruction):
|
|
154 """ add Rd, Rn, imm3 """
|
|
155 mnemonic = 'add'
|
|
156 opcode = 3 # 00011
|
|
157 operands = (RegOp, RegOp, Imm3)
|
205
|
158 irpattern = 3
|
203
|
159 def __init__(self, rd, rn, imm3):
|
|
160 self.rd = rd
|
|
161 self.rn = rn
|
|
162 self.imm3 = imm3
|
|
163 def encode(self):
|
|
164 rd = self.rd.num
|
|
165 rn = self.rn.num
|
|
166 imm3 = self.imm3.imm
|
|
167 opcode = self.opcode
|
|
168 h = (opcode << 11) | (1 << 10) | (imm3 << 6) | (rn << 3) | rd
|
|
169 return u16(h)
|
|
170
|
|
171 @armtarget.instruction
|
|
172 class cmpregimm8_ins(ArmInstruction):
|
|
173 """ cmp Rn, imm8 """
|
|
174 mnemonic = 'cmp'
|
|
175 opcode = 5 # 00101
|
|
176 operands = (RegOp, Imm8)
|
|
177 def __init__(self, rn, imm):
|
|
178 self.rn = rn
|
|
179 self.imm = imm
|
|
180 def encode(self):
|
|
181 rn = self.rn.num
|
|
182 imm = self.imm.imm
|
|
183 opcode = self.opcode
|
|
184 h = (opcode << 11) | (rn << 8) | imm
|
|
185 return u16(h)
|
202
|
186
|
|
187 @armtarget.instruction
|
205
|
188 class jmp_ins(ArmInstruction):
|
|
189 operands = (Label)
|
|
190 mnemonic = 'jmp'
|
|
191 def __init__(self, target_label):
|
|
192 self.target = target_label
|
|
193 def fixUp(self):
|
|
194 pass
|
|
195 def encode(self):
|
|
196 h = 1337 # TODO
|
|
197 return u16(h)
|
|
198
|
|
199 @armtarget.instruction
|
|
200 class push_ins(ArmInstruction):
|
|
201 operands = (RegisterSet)
|
|
202 mnemonic = 'push'
|
|
203 def __init__(self, regs):
|
|
204 self.regs = regs
|
|
205 def encode(self):
|
|
206 return u16(0)
|
|
207
|
|
208 @armtarget.instruction
|
|
209 class pop_ins(ArmInstruction):
|
|
210 operands = (RegisterSet)
|
|
211 mnemonic = 'pop'
|
|
212 def __init__(self, regs):
|
|
213 self.regs = regs
|
|
214 def encode(self):
|
|
215 return u16(0)
|
|
216
|
|
217 @armtarget.instruction
|
202
|
218 class yield_ins(ArmInstruction):
|
|
219 operands = ()
|
|
220 mnemonic = 'yield'
|
|
221 def encode(self):
|
|
222 return u16(0xbf10)
|
|
223
|