annotate python/target/arminstructions.py @ 292:534b94b40aa8

Fixup reorganize
author Windel Bouwman
date Wed, 27 Nov 2013 08:06:42 +0100
parents
children 158068af716c
rev   line source
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
1 import struct
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
2 from asmnodes import ASymbol, AInstruction, ANumber, AUnop, ABinop
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
3 from .basetarget import Register, Instruction, Target, Label, LabelRef
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
4 from .basetarget import Imm32, Imm8, Imm7, Imm3
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
5
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
6
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
7 def u16(h):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
8 return struct.pack('<H', h)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
9
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
10 def u32(x):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
11 return struct.pack('<I', x)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
12
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
13 # Operands:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
14
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
15 class ArmRegister(Register):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
16 def __init__(self, num, name):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
17 super().__init__(name)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
18 self.num = num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
19
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
20 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
21 return self.name
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
22
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
23 @classmethod
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
24 def Create(cls, vop):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
25 if type(vop) is ASymbol:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
26 name = vop.name
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
27 regs = {}
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
28 for r in registers:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
29 regs[r.name] = r
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
30 if name in regs:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
31 r = regs[name]
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
32 if isinstance(r, cls):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
33 return r
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
34
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
35
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
36 class Reg8Op(ArmRegister):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
37 pass
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
38
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
39
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
40 class Reg16Op(ArmRegister):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
41 pass
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
42
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
43
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
44 R0 = Reg8Op(0, 'r0')
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
45 R1 = Reg8Op(1, 'r1')
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
46 R2 = Reg8Op(2, 'r2')
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
47 R3 = Reg8Op(3, 'r3')
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
48 R4 = Reg8Op(4, 'r4')
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
49 R5 = Reg8Op(5, 'r5')
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
50 R6 = Reg8Op(6, 'r6')
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
51 R7 = Reg8Op(7, 'r7')
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
52 # Other registers:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
53 # TODO
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
54 SP = ArmRegister(13, 'sp')
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
55 LR = ArmRegister(14, 'lr')
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
56 PC = ArmRegister(15, 'pc')
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
57
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
58 registers = [R0, R1, R2, R3, R4, R5, R6, R7, SP, LR, PC]
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
59
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
60
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
61 class RegSpOp:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
62 @classmethod
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
63 def Create(cls, vop):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
64 if type(vop) is ASymbol:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
65 if vop.name.lower() == 'sp':
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
66 return cls()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
67
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
68
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
69 def getRegNum(n):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
70 for r in registers:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
71 if r.num == n:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
72 return r
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
73
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
74
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
75 def getRegisterRange(n1, n2):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
76 regs = []
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
77 if n1.num < n2.num:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
78 for n in range(n1.num, n2.num + 1):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
79 r = getRegNum(n)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
80 assert r
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
81 regs.append(r)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
82 return regs
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
83
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
84
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
85 def isRegOffset(regname, x, y):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
86 if type(x) is ASymbol and type(y) is ANumber and x.name.upper() == regname:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
87 return y.number
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
88 elif type(y) is ASymbol and type(x) is ANumber and y.name.upper() == regname:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
89 return x.number
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
90
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
91
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
92 class MemRegXRel:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
93 def __init__(self, offset):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
94 assert offset % 4 == 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
95 self.offset = offset
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
96
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
97 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
98 return '[{}, #{}]'.format(self.regname, self.offset)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
99
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
100 @classmethod
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
101 def Create(cls, vop):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
102 if type(vop) is AUnop and vop.operation == '[]' and type(vop.arg) is ABinop and vop.arg.op == '+':
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
103 vop = vop.arg # descent
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
104 offset = isRegOffset(cls.regname, vop.arg1, vop.arg2)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
105 if type(offset) is int:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
106 if offset % 4 == 0:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
107 offset = vop.arg2.number
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
108 return cls(offset)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
109 elif type(vop) is ASymbol and vop.name.upper() == self.regname:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
110 return cls(0)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
111
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
112
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
113 class MemSpRel(MemRegXRel):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
114 regname = 'SP'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
115
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
116
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
117 class MemR8Rel:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
118 def __init__(self, basereg, offset):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
119 assert type(basereg) is Reg8Op
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
120 assert type(offset) is int
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
121 self.basereg = basereg
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
122 self.offset = offset
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
123
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
124 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
125 return '[{}, #{}]'.format(self.basereg, self.offset)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
126
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
127 @classmethod
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
128 def Create(cls, vop):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
129 if type(vop) is AUnop and vop.operation == '[]':
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
130 vop = vop.arg # descent
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
131 if type(vop) is ABinop:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
132 if vop.op == '+' and type(vop.arg1) is ASymbol and type(vop.arg2) is ANumber:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
133 offset = vop.arg2.number
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
134 if offset > 120:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
135 return
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
136 basereg = Reg8Op.Create(vop.arg1)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
137 if not basereg:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
138 return
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
139 else:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
140 return
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
141 elif type(vop) is ASymbol:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
142 offset = 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
143 basereg = Reg8Op.Create(vop)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
144 if not basereg:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
145 return
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
146 else:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
147 return
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
148 return cls(getRegNum(basereg.num), offset)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
149
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
150
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
151 class RegisterSet:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
152 def __init__(self, regs):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
153 assert type(regs) is set
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
154 self.regs = regs
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
155
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
156 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
157 return ','.join([str(r) for r in self.regs])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
158
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
159 @classmethod
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
160 def Create(cls, vop):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
161 assert type(vop) is AUnop and vop.operation == '{}'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
162 assert type(vop.arg) is list
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
163 regs = set()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
164 for arg in vop.arg:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
165 if type(arg) is ASymbol:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
166 reg = ArmRegister.Create(arg)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
167 if not reg:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
168 return
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
169 regs.add(reg)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
170 elif type(arg) is ABinop and arg.op == '-':
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
171 reg1 = ArmRegister.Create(arg.arg1)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
172 reg2 = ArmRegister.Create(arg.arg2)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
173 if not reg1:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
174 return
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
175 if not reg2:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
176 return
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
177 for r in getRegisterRange(reg1, reg2):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
178 regs.add(r)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
179 else:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
180 raise Exception('Cannot be')
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
181 return cls(regs)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
182
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
183 def registerNumbers(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
184 return [r.num for r in self.regs]
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
185
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
186
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
187
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
188 # Instructions:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
189
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
190 class ArmInstruction(Instruction):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
191 pass
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
192
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
193
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
194 allins = []
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
195
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
196
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
197 def instruction(i):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
198 allins.append(i)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
199 return i
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
200
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
201
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
202 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
203 class Dcd(ArmInstruction):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
204 mnemonic = 'dcd'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
205 operands = (Imm32,)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
206 def __init__(self, expr):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
207 if isinstance(expr, Imm32):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
208 self.expr = expr.imm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
209 self.label = None
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
210 elif isinstance(expr, LabelRef):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
211 self.expr = 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
212 self.label = expr
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
213 elif isinstance(expr, int):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
214 self.expr = expr
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
215 self.label = None
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
216 else:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
217 raise NotImplementedError()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
218
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
219 def resolve(self, f):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
220 if self.label:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
221 self.expr = f(self.label.name)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
222
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
223 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
224 return u32(self.expr)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
225
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
226 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
227 return 'DCD 0x{0:X}'.format(self.expr)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
228
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
229
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
230 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
231 class nop_ins(ArmInstruction):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
232 mnemonic = 'nop'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
233 operands = tuple()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
234
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
235 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
236 return bytes()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
237
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
238 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
239 return 'NOP'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
240
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
241
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
242 # Memory related
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
243
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
244 class LS_imm5_base(ArmInstruction):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
245 """ ??? Rt, [Rn, imm5] """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
246 operands = (Reg8Op, MemR8Rel)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
247 def __init__(self, rt, memop):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
248 assert memop.offset % 4 == 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
249 self.imm5 = memop.offset >> 2
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
250 self.rn = memop.basereg.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
251 self.rt = rt
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
252 self.memloc = memop
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
253 assert self.rn < 8
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
254 assert self.rt.num < 8
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
255
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
256 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
257 Rn = self.rn
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
258 Rt = self.rt.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
259 imm5 = self.imm5
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
260
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
261 h = (self.opcode << 11) | (imm5 << 6) | (Rn << 3) | Rt
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
262 return u16(h)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
263
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
264
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
265 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
266 return '{} {}, {}'.format(self.mnemonic, self.rt, self.memloc)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
267
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
268
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
269 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
270 class Str2(LS_imm5_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
271 mnemonic = 'STR'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
272 opcode = 0xC
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
273
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
274 @classmethod
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
275 def fromim(cls, im):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
276 mem = MemR8Rel(im.src[0], im.others[0])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
277 return cls(im.src[1], mem)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
278
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
279
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
280 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
281 class Ldr2(LS_imm5_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
282 mnemonic = 'LDR'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
283 opcode = 0xD
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
284
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
285 @classmethod
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
286 def fromim(cls, im):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
287 mem = MemR8Rel(im.src[0], im.others[0])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
288 return cls(im.dst[0], mem)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
289
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
290 class ls_sp_base_imm8(ArmInstruction):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
291 operands = (Reg8Op, MemSpRel)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
292 def __init__(self, rt, memop):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
293 self.rt = rt
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
294 self.offset = memop.offset
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
295
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
296 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
297 rt = self.rt.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
298 assert rt < 8
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
299 imm8 = self.offset >> 2
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
300 assert imm8 < 256
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
301 h = (self.opcode << 8) | (rt << 8) | imm8
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
302 return u16(h)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
303
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
304 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
305 return '{} {}, [sp,#{}]'.format(self.mnemonic, self.rt, self.offset)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
306
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
307 def align(x, m):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
308 while ((x % m) != 0):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
309 x = x + 1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
310 return x
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
311
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
312
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
313 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
314 class Ldr3(ArmInstruction):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
315 """ ldr Rt, LABEL, load value from pc relative position """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
316 mnemonic = 'ldr'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
317 operands = (Reg8Op, LabelRef)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
318 def __init__(self, rt, label):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
319 assert isinstance(label, LabelRef)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
320 self.rt = rt
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
321 self.label = label
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
322 self.offset = 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
323
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
324 @classmethod
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
325 def fromim(cls, im):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
326 return cls(im.dst[0], im.others[0])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
327
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
328 def resolve(self, f):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
329 la = f(self.label.name)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
330 sa = align(self.address + 2, 4)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
331 self.offset = (la - sa)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
332 if self.offset < 0:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
333 self.offset = 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
334
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
335 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
336 rt = self.rt.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
337 assert rt < 8
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
338 assert self.offset % 4 == 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
339 imm8 = self.offset >> 2
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
340 assert imm8 < 256
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
341 assert imm8 >= 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
342 h = (0x9 << 11) | (rt << 8) | imm8
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
343 return u16(h)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
344
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
345 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
346 return 'LDR {}, {}'.format(self.rt, self.label.name)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
347
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
348
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
349 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
350 class Ldr1(ls_sp_base_imm8):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
351 """ ldr Rt, [SP, imm8] """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
352 mnemonic = 'LDR'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
353 opcode = 0x98
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
354
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
355
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
356 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
357 class Str1(ls_sp_base_imm8):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
358 """ str Rt, [SP, imm8] """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
359 mnemonic = 'STR'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
360 opcode = 0x90
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
361
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
362
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
363 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
364 class Mov3(ArmInstruction):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
365 """ mov Rd, imm8, move immediate value into register """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
366 mnemonic = 'mov'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
367 opcode = 4 # 00100 Rd(3) imm8
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
368 operands = (Reg8Op, Imm8)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
369 def __init__(self, rd, imm):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
370 if type(imm) is int:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
371 imm = Imm8(imm)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
372 assert type(imm) is Imm8
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
373 self.imm = imm.imm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
374 assert type(rd) is Reg8Op, str(type(rd))
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
375 self.rd = rd
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
376
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
377 @classmethod
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
378 def fromim(cls, im):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
379 return cls(im.dst[0], im.others[0])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
380
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
381 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
382 rd = self.rd.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
383 opcode = self.opcode
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
384 imm8 = self.imm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
385 h = (opcode << 11) | (rd << 8) | imm8
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
386 return u16(h)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
387
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
388 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
389 return 'MOV {}, {}'.format(self.rd, self.imm)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
390
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
391
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
392
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
393 # Arithmatics:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
394
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
395
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
396
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
397 class regregimm3_base(ArmInstruction):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
398 operands = (Reg8Op, Reg8Op, Imm3)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
399 def __init__(self, rd, rn, imm3):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
400 self.rd = rd
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
401 self.rn = rn
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
402 assert type(imm3) is Imm3
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
403 self.imm3 = imm3
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
404
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
405 @classmethod
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
406 def fromim(cls, im):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
407 return cls(im.dst[0], im.src[0], im.others[0])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
408
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
409 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
410 rd = self.rd.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
411 rn = self.rn.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
412 imm3 = self.imm3.imm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
413 opcode = self.opcode
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
414 h = (self.opcode << 9) | (imm3 << 6) | (rn << 3) | rd
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
415 return u16(h)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
416
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
417 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
418 return '{} {}, {}, {}'.format(self.mnemonic, self.rd, self.rn, self.imm3.imm)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
419
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
420 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
421 class addregregimm3_ins(regregimm3_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
422 """ add Rd, Rn, imm3 """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
423 mnemonic = 'add'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
424 opcode = 0b0001110
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
425
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
426
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
427 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
428 class subregregimm3_ins(regregimm3_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
429 """ sub Rd, Rn, imm3 """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
430 mnemonic = 'sub'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
431 opcode = 0b0001111
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
432
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
433
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
434 class regregreg_base(ArmInstruction):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
435 """ ??? Rd, Rn, Rm """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
436 operands = (Reg8Op, Reg8Op, Reg8Op)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
437 def __init__(self, rd, rn, rm):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
438 self.rd = rd
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
439 self.rn = rn
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
440 self.rm = rm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
441
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
442 @classmethod
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
443 def fromim(cls, im):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
444 return cls(im.dst[0], im.src[0], im.src[1])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
445
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
446 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
447 rd = self.rd.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
448 rn = self.rn.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
449 rm = self.rm.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
450 h = (self.opcode << 9) | (rm << 6) | (rn << 3) | rd
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
451 return u16(h)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
452
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
453 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
454 return '{} {}, {}, {}'.format(self.mnemonic, self.rd, self.rn, self.rm)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
455
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
456
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
457 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
458 class Add(regregreg_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
459 mnemonic = 'ADD'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
460 opcode = 0b0001100
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
461
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
462
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
463 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
464 class Sub(regregreg_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
465 mnemonic = 'SUB'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
466 opcode = 0b0001101
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
467
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
468
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
469 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
470 class Mov2(ArmInstruction):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
471 """ mov rd, rm """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
472 operands = (ArmRegister, ArmRegister)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
473 mnemonic = 'MOV'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
474 def __init__(self, rd, rm):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
475 self.rd = rd
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
476 self.rm = rm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
477
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
478 @classmethod
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
479 def fromim(cls, im):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
480 return cls(im.dst[0], im.src[0])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
481
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
482 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
483 Rd = self.rd.num & 0x7
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
484 D = (self.rd.num >> 3) & 0x1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
485 Rm = self.rm.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
486 opcode = 0b01000110
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
487 return u16((opcode << 8) | (D << 7) |(Rm << 3) | Rd)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
488
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
489 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
490 return '{} {}, {}'.format(self.mnemonic, self.rd, self.rm)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
491
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
492
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
493 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
494 class mulregreg_ins(ArmInstruction):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
495 """ mul Rn, Rdm """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
496 operands = (Reg8Op, Reg8Op)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
497 mnemonic = 'MUL'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
498 def __init__(self, rn, rdm):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
499 self.rn = rn
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
500 self.rdm = rdm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
501
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
502 @classmethod
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
503 def fromim(cls, im):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
504 assert im.src[1] is im.dst[0]
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
505 return cls(im.src[0], im.dst[0])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
506
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
507 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
508 rn = self.rn.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
509 rdm = self.rdm.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
510 opcode = 0b0100001101
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
511 h = (opcode << 6) | (rn << 3) | rdm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
512 return u16(h)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
513
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
514 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
515 return '{} {}, {}'.format(self.mnemonic, self.rn, self.rdm)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
516
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
517
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
518 class regreg_base(ArmInstruction):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
519 """ ??? Rdn, Rm """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
520 operands = (Reg8Op, Reg8Op)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
521 # TODO: integrate with the code gen interface:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
522 src = (0, 1)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
523 dst = (0,)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
524 def __init__(self, rdn, rm):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
525 self.rdn = rdn
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
526 self.rm = rm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
527
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
528 @classmethod
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
529 def fromim(cls, im):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
530 return cls(im.src[0], im.src[1])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
531
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
532 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
533 rdn = self.rdn.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
534 rm = self.rm.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
535 h = (self.opcode << 6) | (rm << 3) | rdn
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
536 return u16(h)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
537
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
538 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
539 return '{} {}, {}'.format(self.mnemonic, self.rdn, self.rm)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
540
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
541
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
542 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
543 class movregreg_ins(regreg_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
544 """ mov Rd, Rm (reg8 operands) """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
545 mnemonic = 'mov'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
546 opcode = 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
547
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
548
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
549 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
550 class And(regreg_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
551 mnemonic = 'AND'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
552 opcode = 0b0100000000
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
553
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
554
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
555 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
556 class Orr(regreg_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
557 mnemonic = 'ORR'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
558 opcode = 0b0100001100
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
559
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
560
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
561 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
562 class Cmp(regreg_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
563 mnemonic = 'CMP'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
564 opcode = 0b0100001010
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
565
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
566
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
567 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
568 class Lsl(regreg_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
569 mnemonic = 'LSL'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
570 opcode = 0b0100000010
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
571
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
572
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
573 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
574 class cmpregimm8_ins(ArmInstruction):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
575 """ cmp Rn, imm8 """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
576 mnemonic = 'cmp'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
577 opcode = 5 # 00101
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
578 operands = (Reg8Op, Imm8)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
579 def __init__(self, rn, imm):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
580 self.rn = rn
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
581 self.imm = imm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
582
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
583 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
584 rn = self.rn.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
585 imm = self.imm.imm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
586 opcode = self.opcode
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
587 h = (opcode << 11) | (rn << 8) | imm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
588 return u16(h)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
589
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
590
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
591 # Jumping:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
592
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
593 def wrap_negative(x, bits):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
594 b = struct.unpack('<I', struct.pack('<i', x))[0]
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
595 mask = (1 << bits) - 1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
596 return b & mask
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
597
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
598 class jumpBase_ins(ArmInstruction):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
599 operands = (LabelRef,)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
600 def __init__(self, target_label):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
601 assert type(target_label) is LabelRef
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
602 self.target = target_label
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
603 self.offset = 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
604
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
605 def resolve(self, f):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
606 la = f(self.target.name)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
607 sa = self.address + 4
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
608 self.offset = (la - sa)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
609
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
610 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
611 return '{} {}'.format(self.mnemonic, self.target.name)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
612
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
613
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
614 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
615 class B(jumpBase_ins):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
616 mnemonic = 'B'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
617 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
618 imm11 = wrap_negative(self.offset >> 1, 11)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
619 h = (0b11100 << 11) | imm11 # | 1 # 1 to enable thumb mode
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
620 return u16(h)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
621
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
622
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
623 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
624 class Bl(jumpBase_ins):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
625 mnemonic = 'BL'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
626 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
627 imm32 = wrap_negative(self.offset >> 1, 32)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
628 imm11 = imm32 & 0x7FF
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
629 imm10 = (imm32 >> 11) & 0x3FF
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
630 j1 = 1 # TODO: what do these mean?
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
631 j2 = 1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
632 s = (imm32 >> 24) & 0x1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
633 h1 = (0b11110 << 11) | (s << 10) | imm10
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
634 h2 = (0b1101 << 12) | (j1 << 13) | (j2 << 11) | imm11
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
635 return u16(h1) + u16(h2)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
636
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
637
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
638 class cond_base_ins(jumpBase_ins):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
639 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
640 imm8 = wrap_negative(self.offset >> 1, 8)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
641 h = (0b1101 << 12) | (self.cond << 8) | imm8
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
642 return u16(h)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
643
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
644
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
645 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
646 class Beq(cond_base_ins):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
647 mnemonic = 'beq'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
648 cond = 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
649
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
650
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
651 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
652 class Bne(cond_base_ins):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
653 mnemonic = 'bne'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
654 cond = 1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
655
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
656
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
657 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
658 class Blt(cond_base_ins):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
659 mnemonic = 'blt'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
660 cond = 0b1011
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
661
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
662
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
663 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
664 class Bgt(cond_base_ins):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
665 mnemonic = 'bgt'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
666 cond = 0b1100
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
667
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
668
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
669 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
670 class Push(ArmInstruction):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
671 operands = (RegisterSet,)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
672 mnemonic = 'push'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
673
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
674 def __init__(self, regs):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
675 if type(regs) is set:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
676 regs = RegisterSet(regs)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
677 assert (type(regs),) == self.operands, (type(regs),)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
678 self.regs = regs
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
679
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
680 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
681 return '{0} {{{1}}}'.format(self.mnemonic, self.regs)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
682
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
683 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
684 reg_list = 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
685 M = 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
686 for n in self.regs.registerNumbers():
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
687 if n < 8:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
688 reg_list |= (1 << n)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
689 elif n == 14:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
690 M = 1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
691 else:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
692 raise NotImplementedError('not implemented for this register')
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
693 h = (0x5a << 9) | (M << 8) | reg_list
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
694 return u16(h)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
695
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
696
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
697 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
698 class Pop(ArmInstruction):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
699 operands = (RegisterSet,)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
700 mnemonic = 'pop'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
701
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
702 def __init__(self, regs):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
703 if type(regs) is set:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
704 regs = RegisterSet(regs)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
705 assert (type(regs),) == self.operands, (type(regs),)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
706 self.regs = regs
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
707
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
708 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
709 return '{0} {{{1}}}'.format(self.mnemonic, self.regs)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
710
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
711 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
712 reg_list = 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
713 P = 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
714 for n in self.regs.registerNumbers():
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
715 if n < 8:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
716 reg_list |= (1 << n)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
717 elif n == 15:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
718 P = 1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
719 else:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
720 raise NotImplementedError('not implemented for this register')
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
721 h = (0x5E << 9) | (P << 8) | reg_list
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
722 return u16(h)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
723
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
724
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
725 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
726 class Yield(ArmInstruction):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
727 operands = ()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
728 mnemonic = 'yield'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
729
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
730 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
731 return u16(0xbf10)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
732
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
733 # misc:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
734
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
735 # add/sub SP:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
736 class addspsp_base(ArmInstruction):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
737 operands = (RegSpOp, RegSpOp, Imm7)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
738 def __init__(self, _sp, _sp2, imm7):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
739 self.imm7 = imm7.imm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
740 assert self.imm7 % 4 == 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
741 self.imm7 >>= 2
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
742
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
743 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
744 return u16((self.opcode << 7) |self.imm7)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
745
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
746 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
747 return '{} sp, sp, {}'.format(self.mnemonic, self.imm7 << 2)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
748
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
749 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
750 class AddSp(addspsp_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
751 mnemonic = 'add'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
752 opcode = 0b101100000
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
753
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
754
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
755 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
756 class SubSp(addspsp_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
757 mnemonic = 'sub'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
758 opcode = 0b101100001