annotate python/target/arminstructions.py @ 335:582a1aaa3983

Added long branch format
author Windel Bouwman
date Mon, 17 Feb 2014 20:41:30 +0100
parents 6f4753202b9a
children d1ecc493384e
rev   line source
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
1 import struct
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 305
diff changeset
2 from ppci.asmnodes import ASymbol, AInstruction, ANumber, AUnop, ABinop
292
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 encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
220 return u32(self.expr)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
221
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
222 def relocations(self):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
223 assert not isinstance(self.expr, LabelRef)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
224 return []
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
225
292
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
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
328 def relocations(self):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
329 return [(self.label.name, 'lit_add_8')]
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
330
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
331 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
332 rt = self.rt.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
333 assert rt < 8
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
334 assert self.offset % 4 == 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
335 imm8 = self.offset >> 2
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
336 assert imm8 < 256
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
337 assert imm8 >= 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
338 h = (0x9 << 11) | (rt << 8) | imm8
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
339 return u16(h)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
340
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
341 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
342 return 'LDR {}, {}'.format(self.rt, self.label.name)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
343
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
344
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
345 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
346 class Ldr1(ls_sp_base_imm8):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
347 """ ldr Rt, [SP, imm8] """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
348 mnemonic = 'LDR'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
349 opcode = 0x98
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
350
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
351
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
352 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
353 class Str1(ls_sp_base_imm8):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
354 """ str Rt, [SP, imm8] """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
355 mnemonic = 'STR'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
356 opcode = 0x90
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
357
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
358
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
359 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
360 class Mov3(ArmInstruction):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
361 """ mov Rd, imm8, move immediate value into register """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
362 mnemonic = 'mov'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
363 opcode = 4 # 00100 Rd(3) imm8
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
364 operands = (Reg8Op, Imm8)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
365 def __init__(self, rd, imm):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
366 if type(imm) is int:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
367 imm = Imm8(imm)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
368 assert type(imm) is Imm8
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
369 self.imm = imm.imm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
370 assert type(rd) is Reg8Op, str(type(rd))
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
371 self.rd = rd
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
372
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
373 @classmethod
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
374 def fromim(cls, im):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
375 return cls(im.dst[0], im.others[0])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
376
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
377 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
378 rd = self.rd.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
379 opcode = self.opcode
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
380 imm8 = self.imm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
381 h = (opcode << 11) | (rd << 8) | imm8
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
382 return u16(h)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
383
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
384 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
385 return 'MOV {}, {}'.format(self.rd, self.imm)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
386
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
387
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
388
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
389 # Arithmatics:
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 class regregimm3_base(ArmInstruction):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
394 operands = (Reg8Op, Reg8Op, Imm3)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
395 def __init__(self, rd, rn, imm3):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
396 self.rd = rd
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
397 self.rn = rn
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
398 assert type(imm3) is Imm3
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
399 self.imm3 = imm3
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
400
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
401 @classmethod
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
402 def fromim(cls, im):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
403 return cls(im.dst[0], im.src[0], im.others[0])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
404
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
405 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
406 rd = self.rd.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
407 rn = self.rn.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
408 imm3 = self.imm3.imm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
409 opcode = self.opcode
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
410 h = (self.opcode << 9) | (imm3 << 6) | (rn << 3) | rd
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
411 return u16(h)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
412
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
413 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
414 return '{} {}, {}, {}'.format(self.mnemonic, self.rd, self.rn, self.imm3.imm)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
415
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
416 @instruction
300
Windel Bouwman
parents: 292
diff changeset
417 class Add2(regregimm3_base):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
418 """ add Rd, Rn, imm3 """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
419 mnemonic = 'add'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
420 opcode = 0b0001110
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
421
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
422
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
423 @instruction
300
Windel Bouwman
parents: 292
diff changeset
424 class Sub2(regregimm3_base):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
425 """ sub Rd, Rn, imm3 """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
426 mnemonic = 'sub'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
427 opcode = 0b0001111
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
428
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
429
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
430 class regregreg_base(ArmInstruction):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
431 """ ??? Rd, Rn, Rm """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
432 operands = (Reg8Op, Reg8Op, Reg8Op)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
433 def __init__(self, rd, rn, rm):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
434 self.rd = rd
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
435 self.rn = rn
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
436 self.rm = rm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
437
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
438 @classmethod
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
439 def fromim(cls, im):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
440 return cls(im.dst[0], im.src[0], im.src[1])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
441
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
442 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
443 rd = self.rd.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
444 rn = self.rn.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
445 rm = self.rm.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
446 h = (self.opcode << 9) | (rm << 6) | (rn << 3) | rd
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
447 return u16(h)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
448
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
449 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
450 return '{} {}, {}, {}'.format(self.mnemonic, self.rd, self.rn, self.rm)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
451
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
452
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
453 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
454 class Add(regregreg_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
455 mnemonic = 'ADD'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
456 opcode = 0b0001100
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
457
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
458
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
459 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
460 class Sub(regregreg_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
461 mnemonic = 'SUB'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
462 opcode = 0b0001101
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
463
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
464
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
465 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
466 class Mov2(ArmInstruction):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
467 """ mov rd, rm """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
468 operands = (ArmRegister, ArmRegister)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
469 mnemonic = 'MOV'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
470 def __init__(self, rd, rm):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
471 self.rd = rd
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
472 self.rm = rm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
473
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
474 @classmethod
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
475 def fromim(cls, im):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
476 return cls(im.dst[0], im.src[0])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
477
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
478 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
479 Rd = self.rd.num & 0x7
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
480 D = (self.rd.num >> 3) & 0x1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
481 Rm = self.rm.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
482 opcode = 0b01000110
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
483 return u16((opcode << 8) | (D << 7) |(Rm << 3) | Rd)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
484
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
485 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
486 return '{} {}, {}'.format(self.mnemonic, self.rd, self.rm)
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
487
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
488
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
489 @instruction
300
Windel Bouwman
parents: 292
diff changeset
490 class Mul(ArmInstruction):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
491 """ mul Rn, Rdm """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
492 operands = (Reg8Op, Reg8Op)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
493 mnemonic = 'MUL'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
494 def __init__(self, rn, rdm):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
495 self.rn = rn
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
496 self.rdm = rdm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
497
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
498 @classmethod
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
499 def fromim(cls, im):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
500 assert im.src[1] is im.dst[0]
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
501 return cls(im.src[0], im.dst[0])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
502
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
503 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
504 rn = self.rn.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
505 rdm = self.rdm.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
506 opcode = 0b0100001101
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
507 h = (opcode << 6) | (rn << 3) | rdm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
508 return u16(h)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
509
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
510 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
511 return '{} {}, {}'.format(self.mnemonic, self.rn, self.rdm)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
512
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
513
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
514 class regreg_base(ArmInstruction):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
515 """ ??? Rdn, Rm """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
516 operands = (Reg8Op, Reg8Op)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
517 # TODO: integrate with the code gen interface:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
518 src = (0, 1)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
519 dst = (0,)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
520 def __init__(self, rdn, rm):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
521 self.rdn = rdn
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
522 self.rm = rm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
523
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
524 @classmethod
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
525 def fromim(cls, im):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
526 return cls(im.src[0], im.src[1])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
527
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
528 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
529 rdn = self.rdn.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
530 rm = self.rm.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
531 h = (self.opcode << 6) | (rm << 3) | rdn
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
532 return u16(h)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
533
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
534 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
535 return '{} {}, {}'.format(self.mnemonic, self.rdn, self.rm)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
536
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
537
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
538 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
539 class movregreg_ins(regreg_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
540 """ mov Rd, Rm (reg8 operands) """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
541 mnemonic = 'mov'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
542 opcode = 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
543
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
544
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
545 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
546 class And(regreg_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
547 mnemonic = 'AND'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
548 opcode = 0b0100000000
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
549
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
550
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
551 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
552 class Orr(regreg_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
553 mnemonic = 'ORR'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
554 opcode = 0b0100001100
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
555
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
556
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
557 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
558 class Cmp(regreg_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
559 mnemonic = 'CMP'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
560 opcode = 0b0100001010
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
561
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
562
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
563 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
564 class Lsl(regreg_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
565 mnemonic = 'LSL'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
566 opcode = 0b0100000010
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
567
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
568
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
569 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
570 class cmpregimm8_ins(ArmInstruction):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
571 """ cmp Rn, imm8 """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
572 mnemonic = 'cmp'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
573 opcode = 5 # 00101
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
574 operands = (Reg8Op, Imm8)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
575 def __init__(self, rn, imm):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
576 self.rn = rn
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
577 self.imm = imm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
578
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
579 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
580 rn = self.rn.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
581 imm = self.imm.imm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
582 opcode = self.opcode
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
583 h = (opcode << 11) | (rn << 8) | imm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
584 return u16(h)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
585
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
586
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
587 # Jumping:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
588
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
589 def wrap_negative(x, bits):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
590 b = struct.unpack('<I', struct.pack('<i', x))[0]
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
591 mask = (1 << bits) - 1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
592 return b & mask
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
593
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
594 class jumpBase_ins(ArmInstruction):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
595 operands = (LabelRef,)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
596 def __init__(self, target_label):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
597 assert type(target_label) is LabelRef
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
598 self.target = target_label
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
599 self.offset = 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
600
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
601 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
602 return '{} {}'.format(self.mnemonic, self.target.name)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
603
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
604
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
605 class Imm11Reloc:
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
606 def apply(self, P, S):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
607 pass
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
608
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
609
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
610 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
611 class B(jumpBase_ins):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
612 mnemonic = 'B'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
613 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
614 imm11 = wrap_negative(self.offset >> 1, 11)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
615 h = (0b11100 << 11) | imm11 # | 1 # 1 to enable thumb mode
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
616 return u16(h)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
617
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
618 def relocations(self):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
619 return [(self.target.name, 'wrap_new11')]
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
620
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
621 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
622 class Bl(jumpBase_ins):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
623 mnemonic = 'BL'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
624 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
625 imm32 = wrap_negative(self.offset >> 1, 32)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
626 imm11 = imm32 & 0x7FF
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
627 imm10 = (imm32 >> 11) & 0x3FF
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
628 j1 = 1 # TODO: what do these mean?
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
629 j2 = 1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
630 s = (imm32 >> 24) & 0x1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
631 h1 = (0b11110 << 11) | (s << 10) | imm10
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
632 h2 = (0b1101 << 12) | (j1 << 13) | (j2 << 11) | imm11
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
633 return u16(h1) + u16(h2)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
634
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
635 def relocations(self):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
636 return [(self.target.name, 'bl_imm11_imm10')]
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
637
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
638
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
639 class cond_base_ins_short(jumpBase_ins):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
640 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
641 imm8 = wrap_negative(self.offset >> 1, 8)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
642 h = (0b1101 << 12) | (self.cond << 8) | imm8
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
643 return u16(h)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
644
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
645 def relocations(self):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
646 return [(self.target.name, 'rel8')]
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
647
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
648
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
649 class cond_base_ins(jumpBase_ins):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
650 """ Encoding T3 """
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
651 def encode(self):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
652 j1 = 1 # TODO: what do these mean?
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
653 j2 = 1
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
654 h1 = (0b11110 << 11) | (self.cond << 6)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
655 h2 = (0b1101 << 12) | (j1 << 13) | (j2 << 11)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
656 return u16(h1) + u16(h2)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
657
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
658 def relocations(self):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
659 return [(self.target.name, 'b_imm11_imm6')]
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
660
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
661
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
662 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
663 class Beq(cond_base_ins):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
664 mnemonic = 'beq'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
665 cond = 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
666
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
667
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
668 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
669 class Bne(cond_base_ins):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
670 mnemonic = 'bne'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
671 cond = 1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
672
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
673
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
674 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
675 class Blt(cond_base_ins):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
676 mnemonic = 'blt'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
677 cond = 0b1011
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
678
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
679
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
680 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
681 class Bgt(cond_base_ins):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
682 mnemonic = 'bgt'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
683 cond = 0b1100
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
684
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
685
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
686 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
687 class Push(ArmInstruction):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
688 operands = (RegisterSet,)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
689 mnemonic = 'push'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
690
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
691 def __init__(self, regs):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
692 if type(regs) is set:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
693 regs = RegisterSet(regs)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
694 assert (type(regs),) == self.operands, (type(regs),)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
695 self.regs = regs
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
696
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
697 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
698 return '{0} {{{1}}}'.format(self.mnemonic, self.regs)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
699
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
700 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
701 reg_list = 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
702 M = 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
703 for n in self.regs.registerNumbers():
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
704 if n < 8:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
705 reg_list |= (1 << n)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
706 elif n == 14:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
707 M = 1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
708 else:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
709 raise NotImplementedError('not implemented for this register')
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
710 h = (0x5a << 9) | (M << 8) | reg_list
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
711 return u16(h)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
712
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
713
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
714 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
715 class Pop(ArmInstruction):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
716 operands = (RegisterSet,)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
717 mnemonic = 'pop'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
718
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
719 def __init__(self, regs):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
720 if type(regs) is set:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
721 regs = RegisterSet(regs)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
722 assert (type(regs),) == self.operands, (type(regs),)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
723 self.regs = regs
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
724
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
725 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
726 return '{0} {{{1}}}'.format(self.mnemonic, self.regs)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
727
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
728 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
729 reg_list = 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
730 P = 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
731 for n in self.regs.registerNumbers():
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
732 if n < 8:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
733 reg_list |= (1 << n)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
734 elif n == 15:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
735 P = 1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
736 else:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
737 raise NotImplementedError('not implemented for this register')
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
738 h = (0x5E << 9) | (P << 8) | reg_list
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
739 return u16(h)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
740
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
741
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
742 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
743 class Yield(ArmInstruction):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
744 operands = ()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
745 mnemonic = 'yield'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
746
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
747 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
748 return u16(0xbf10)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
749
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
750 # misc:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
751
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
752 # add/sub SP:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
753 class addspsp_base(ArmInstruction):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
754 operands = (RegSpOp, RegSpOp, Imm7)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
755 def __init__(self, _sp, _sp2, imm7):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
756 self.imm7 = imm7.imm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
757 assert self.imm7 % 4 == 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
758 self.imm7 >>= 2
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
759
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
760 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
761 return u16((self.opcode << 7) |self.imm7)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
762
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
763 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
764 return '{} sp, sp, {}'.format(self.mnemonic, self.imm7 << 2)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
765
305
0615b5308710 Updated docs
Windel Bouwman
parents: 300
diff changeset
766
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
767 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
768 class AddSp(addspsp_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
769 mnemonic = 'add'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
770 opcode = 0b101100000
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
771
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
772
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
773 @instruction
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
774 class SubSp(addspsp_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
775 mnemonic = 'sub'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
776 opcode = 0b101100001