annotate python/target/arminstructions.py @ 340:c7cc54c0dfdf devel

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