annotate python/target/arminstructions.py @ 341:4d204f6f7d4e devel

Rewrite of assembler parts
author Windel Bouwman
date Fri, 28 Feb 2014 18:07:14 +0100
parents c7cc54c0dfdf
children
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
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
6 from .armtoken import ThumbToken, ArmToken
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
7 from .armregisters import R0, ArmRegister, SP
340
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents: 336
diff changeset
8
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
9
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
10
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
11 def u16(h):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
12 return struct.pack('<H', h)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
13
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
14 def u32(x):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
15 return struct.pack('<I', x)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
16
336
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 335
diff changeset
17
340
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents: 336
diff changeset
18 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
19
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 335
diff changeset
20
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
21 # Operands:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
22
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
23
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
24
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
25 class RegSpOp:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
26 @classmethod
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
27 def Create(cls, vop):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
28 if type(vop) is ASymbol:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
29 if vop.name.lower() == 'sp':
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
30 return cls()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
31
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
32
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
33 def getRegNum(n):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
34 for r in registers:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
35 if r.num == n:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
36 return r
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
37
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
38
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
39 def getRegisterRange(n1, n2):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
40 regs = []
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
41 if n1.num < n2.num:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
42 for n in range(n1.num, n2.num + 1):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
43 r = getRegNum(n)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
44 assert r
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
45 regs.append(r)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
46 return regs
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
47
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
48
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
49 def isRegOffset(regname, x, y):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
50 if type(x) is ASymbol and type(y) is ANumber and x.name.upper() == regname:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
51 return y.number
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
52 elif type(y) is ASymbol and type(x) is ANumber and y.name.upper() == regname:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
53 return x.number
340
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents: 336
diff changeset
54
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
55
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
56
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
57 # Instructions:
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
58
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
59 class ThumbInstruction(Instruction):
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
60 pass
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
61
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
62
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
63
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
64 class Dcd(ThumbInstruction):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
65 def __init__(self, expr):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
66 if isinstance(expr, Imm32):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
67 self.expr = expr.imm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
68 self.label = None
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
69 elif isinstance(expr, LabelRef):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
70 self.expr = 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
71 self.label = expr
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
72 elif isinstance(expr, int):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
73 self.expr = expr
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
74 self.label = None
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
75 else:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
76 raise NotImplementedError()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
77
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
78 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
79 return u32(self.expr)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
80
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
81 def relocations(self):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
82 assert not isinstance(self.expr, LabelRef)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
83 return []
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
84
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
85 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
86 return 'DCD 0x{0:X}'.format(self.expr)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
87
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
88
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
89 class nop_ins(ThumbInstruction):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
90 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
91 return bytes()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
92
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
93 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
94 return 'NOP'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
95
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
96
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
97 # Memory related
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
98
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
99 class LS_imm5_base(ThumbInstruction):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
100 """ ??? Rt, [Rn, imm5] """
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
101 def __init__(self, rt, rn, imm5):
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
102 assert imm5 % 4 == 0
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
103 self.imm5 = imm5 >> 2
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
104 self.rn = rn
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
105 self.rt = rt
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
106 assert self.rn.num < 8
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
107 assert self.rt.num < 8
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
108 self.token = ThumbToken()
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
109
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
110 def encode(self):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
111 Rn = self.rn.num
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
112 Rt = self.rt.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
113 imm5 = self.imm5
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
114 self.token[0:3] = Rt
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
115 self.token[3:6] = Rn
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
116 self.token[6:11] = imm5
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
117 self.token[11:16] = self.opcode
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
118 return self.token.encode()
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
119
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
120 def __repr__(self):
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
121 mnemonic = "???"
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
122 return '{} {}, [{}, {}]'.format(mnemonic, self.rt, self.rn, self.imm5)
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
123
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
124
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
125 class Str2(LS_imm5_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
126 opcode = 0xC
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
127
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
128 @classmethod
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
129 def fromim(cls, im):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
130 return cls(im.src[1], im.src[0], im.others[0])
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
131
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
132
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
133 class Ldr2(LS_imm5_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
134 opcode = 0xD
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
135
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
136 @classmethod
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
137 def fromim(cls, im):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
138 return cls(im.dst[0], im.src[0], im.others[0])
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
139
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
140 class ls_sp_base_imm8(ThumbInstruction):
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
141 def __init__(self, rt, offset):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
142 self.rt = rt
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
143 self.offset = offset
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
144
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
145 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
146 rt = self.rt.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
147 assert rt < 8
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
148 imm8 = self.offset >> 2
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
149 assert imm8 < 256
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
150 h = (self.opcode << 8) | (rt << 8) | imm8
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
151 return u16(h)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
152
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
153 def __repr__(self):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
154 mnemonic = self.__class__.__name__
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
155 return '{} {}, [sp,#{}]'.format(mnemonic, self.rt, self.offset)
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
156
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
157 def align(x, m):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
158 while ((x % m) != 0):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
159 x = x + 1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
160 return x
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
161
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
162 def Ldr(*args):
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
163 if len(args) == 2 and isinstance(args[0], ArmRegister) \
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
164 and isinstance(args[1], str):
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
165 return Ldr3(*args)
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
166 else:
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
167 raise Exception()
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
168
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
169
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
170 class Ldr3(ThumbInstruction):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
171 """ ldr Rt, LABEL, load value from pc relative position """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
172 mnemonic = 'ldr'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
173 def __init__(self, rt, label):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
174 self.rt = rt
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
175 self.label = label
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
176
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
177 @classmethod
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
178 def fromim(cls, im):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
179 return cls(im.dst[0], im.others[0])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
180
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
181 def relocations(self):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
182 return [(self.label, 'lit_add_8')]
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
183
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
184 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
185 rt = self.rt.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
186 assert rt < 8
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
187 imm8 = 0
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
188 h = (0x9 << 11) | (rt << 8) | imm8
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
189 return u16(h)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
190
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
191 def __repr__(self):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
192 return 'LDR {}, {}'.format(self.rt, self.label)
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
193
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
194
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
195 class Ldr1(ls_sp_base_imm8):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
196 """ ldr Rt, [SP, imm8] """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
197 opcode = 0x98
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
198
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
199
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
200 class Str1(ls_sp_base_imm8):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
201 """ str Rt, [SP, imm8] """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
202 opcode = 0x90
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
203
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
204
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
205 class Mov3(ThumbInstruction):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
206 """ mov Rd, imm8, move immediate value into register """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
207 opcode = 4 # 00100 Rd(3) imm8
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
208 def __init__(self, rd, imm):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
209 assert imm < 256
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
210 self.imm = imm
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
211 self.rd = rd
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
212 self.token = ThumbToken()
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
213
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
214 @classmethod
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
215 def fromim(cls, im):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
216 return cls(im.dst[0], im.others[0])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
217
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
218 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
219 rd = self.rd.num
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
220 self.token[8:11] = rd
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
221 self.token[0:8] = self.imm
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
222 self.token[11:16] = self.opcode
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
223 return self.token.encode()
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
224
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
225 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
226 return 'MOV {}, {}'.format(self.rd, self.imm)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
227
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 # Arithmatics:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
231
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
232
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
233
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
234 class regregimm3_base(ThumbInstruction):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
235 def __init__(self, rd, rn, imm3):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
236 self.rd = rd
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
237 self.rn = rn
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
238 assert imm3 < 8
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
239 self.imm3 = imm3
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
240 self.token = ThumbToken()
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
241
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
242 @classmethod
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
243 def fromim(cls, im):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
244 return cls(im.dst[0], im.src[0], im.others[0])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
245
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
246 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
247 rd = self.rd.num
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
248 self.token[0:3] = rd
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
249 self.token[3:6] = self.rn.num
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
250 self.token[6:9] = self.imm3
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
251 self.token[9:16] = self.opcode
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
252 return self.token.encode()
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
253
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
254 def __repr__(self):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
255 mnemonic = self.__class__.__name__
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
256 return '{} {}, {}, {}'.format(mnemonic, self.rd, self.rn, self.imm3)
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
257
340
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents: 336
diff changeset
258
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents: 336
diff changeset
259
300
Windel Bouwman
parents: 292
diff changeset
260 class Add2(regregimm3_base):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
261 """ add Rd, Rn, imm3 """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
262 opcode = 0b0001110
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
263
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
264
300
Windel Bouwman
parents: 292
diff changeset
265 class Sub2(regregimm3_base):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
266 """ sub Rd, Rn, imm3 """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
267 opcode = 0b0001111
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
268
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
269
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
270 def Sub(*args):
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
271 if len(args) == 3 and args[0] is SP and args[1] is SP and \
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
272 isinstance(args[2], int) and args[2] < 256:
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
273 return SubSp(args[2])
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
274 elif len(args) == 3 and isinstance(args[0], ArmRegister) and \
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
275 isinstance(args[1], ArmRegister) and isinstance(args[2], int) and \
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
276 args[2] < 8:
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
277 return Sub2(args[0], args[1], args[2])
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
278 else:
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
279 raise Exception()
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
280
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
281 class regregreg_base(ThumbInstruction):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
282 """ ??? Rd, Rn, Rm """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
283 def __init__(self, rd, rn, rm):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
284 self.rd = rd
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
285 self.rn = rn
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
286 self.rm = rm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
287
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
288 @classmethod
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
289 def fromim(cls, im):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
290 return cls(im.dst[0], im.src[0], im.src[1])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
291
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
292 def encode(self):
340
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents: 336
diff changeset
293 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
294 at.rd = self.rd.num
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
295 rn = self.rn.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
296 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
297 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
298 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
299 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
300 return at.encode()
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
301
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
302 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
303 return '{} {}, {}, {}'.format(self.mnemonic, self.rd, self.rn, self.rm)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
304
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
305
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
306 class Add3(regregreg_base):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
307 mnemonic = 'ADD'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
308 opcode = 0b0001100
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
309
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
310
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
311 class Sub3(regregreg_base):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
312 mnemonic = 'SUB'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
313 opcode = 0b0001101
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
314
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
315
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
316 class Mov2(ThumbInstruction):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
317 """ mov rd, rm """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
318 mnemonic = 'MOV'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
319 def __init__(self, rd, rm):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
320 self.rd = rd
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
321 self.rm = rm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
322
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
323 @classmethod
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
324 def fromim(cls, im):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
325 return cls(im.dst[0], im.src[0])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
326
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
327 def encode(self):
340
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents: 336
diff changeset
328 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
329 at.rd = self.rd.num & 0x7
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
330 D = (self.rd.num >> 3) & 0x1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
331 Rm = self.rm.num
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
332 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
333 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
334 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
335 at[7] = D
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
336 return at.encode()
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
337
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
338 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
339 return '{} {}, {}'.format(self.mnemonic, self.rd, self.rm)
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
340
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
341
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
342 class Mul(ThumbInstruction):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
343 """ mul Rn, Rdm """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
344 mnemonic = 'MUL'
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
345 def __init__(self, rn, rdm):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
346 self.rn = rn
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
347 self.rdm = rdm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
348
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
349 @classmethod
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
350 def fromim(cls, im):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
351 assert im.src[1] is im.dst[0]
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
352 return cls(im.src[0], im.dst[0])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
353
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
354 def encode(self):
340
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents: 336
diff changeset
355 at = ThumbToken()
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
356 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
357 at.rd = self.rdm.num
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
358 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
359 #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
360 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
361 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
362 return at.encode()
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
363
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
364 def __repr__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
365 return '{} {}, {}'.format(self.mnemonic, self.rn, self.rdm)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
366
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
367
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
368 class regreg_base(ThumbInstruction):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
369 """ ??? Rdn, Rm """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
370 def __init__(self, rdn, rm):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
371 self.rdn = rdn
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
372 self.rm = rm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
373
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
374 @classmethod
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
375 def fromim(cls, im):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
376 return cls(im.src[0], im.src[1])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
377
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
378 def encode(self):
340
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents: 336
diff changeset
379 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
380 at.rd = self.rdn.num
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
381 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
382 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
383 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
384 return at.encode()
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
385
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
386 def __repr__(self):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
387 mnemonic = self.__class__.__name__
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
388 return '{} {}, {}'.format(mnemonic, self.rdn, self.rm)
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
389
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
390
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
391 class movregreg_ins(regreg_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
392 """ mov Rd, Rm (reg8 operands) """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
393 opcode = 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
394
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
395
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
396 class And(regreg_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
397 opcode = 0b0100000000
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
398
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
399
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
400 class Orr(regreg_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
401 opcode = 0b0100001100
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
402
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
403
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
404 class Cmp(regreg_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
405 opcode = 0b0100001010
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
406
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
407
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
408 class Lsl(regreg_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
409 opcode = 0b0100000010
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
410
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
411
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
412 class cmpregimm8_ins(ThumbInstruction):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
413 """ cmp Rn, imm8 """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
414 opcode = 5 # 00101
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
415 def __init__(self, rn, imm):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
416 self.rn = rn
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
417 self.imm = imm
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
418
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
419 def encode(self):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
420 at = ThumbToken()
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
421 at[0:8] = self.imm.imm
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
422 at[8:11] = self.rn.num
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
423 at[11:16] = self.opcode
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
424 return at.encode()
292
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 # Jumping:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
428
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
429 class jumpBase_ins(ThumbInstruction):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
430 def __init__(self, target_label):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
431 self.target = target_label
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
432 self.offset = 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
433
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
434 def __repr__(self):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
435 mnemonic = self.__class__.__name__
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
436 return '{} {}'.format(mnemonic, self.target)
292
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 class B(jumpBase_ins):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
440 def encode(self):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
441 h = (0b11100 << 11) | 0
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
442 # | 1 # 1 to enable thumb mode
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
443 return u16(h)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
444
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
445 def relocations(self):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
446 return [(self.target, 'wrap_new11')]
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
447
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
448 class Bl(jumpBase_ins):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
449 def encode(self):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
450 imm11 = 0
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
451 imm10 = 0
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
452 j1 = 1 # TODO: what do these mean?
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
453 j2 = 1
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
454 s = 0
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
455 h1 = (0b11110 << 11) | (s << 10) | imm10
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
456 h2 = (0b1101 << 12) | (j1 << 13) | (j2 << 11) | imm11
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
457 return u16(h1) + u16(h2)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
458
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
459 def relocations(self):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
460 return [(self.target, 'bl_imm11_imm10')]
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
461
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
462
336
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 335
diff changeset
463 class cond_base_ins(jumpBase_ins):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
464 def encode(self):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
465 imm8 = 0
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
466 h = (0b1101 << 12) | (self.cond << 8) | imm8
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
467 return u16(h)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
468
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
469 def relocations(self):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
470 return [(self.target, 'rel8')]
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
471
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
472
336
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 335
diff changeset
473 class cond_base_ins_long(jumpBase_ins):
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
474 """ Encoding T3 """
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
475 def encode(self):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
476 j1 = 1 # TODO: what do these mean?
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
477 j2 = 1
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
478 h1 = (0b11110 << 11) | (self.cond << 6)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
479 h2 = (0b1101 << 12) | (j1 << 13) | (j2 << 11)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
480 return u16(h1) + u16(h2)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
481
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
482 def relocations(self):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
483 return [(self.target, 'b_imm11_imm6')]
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
484
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
485
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
486 class Beq(cond_base_ins):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
487 cond = 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
488
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
489
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
490 class Bne(cond_base_ins):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
491 cond = 1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
492
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
493
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
494 class Blt(cond_base_ins):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
495 cond = 0b1011
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
496
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
497
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
498 class Bgt(cond_base_ins):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
499 cond = 0b1100
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
500
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
501
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
502 class Push(ThumbInstruction):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
503 def __init__(self, regs):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
504 assert type(regs) is set
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
505 self.regs = regs
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
506
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
507 def __repr__(self):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
508 return 'Push {{{}}}'.format(self.regs)
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
509
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
510 def encode(self):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
511 at = ThumbToken()
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
512 for n in register_numbers(self.regs):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
513 if n < 8:
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
514 at[n] = 1
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
515 elif n == 14:
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
516 at[8] = 1
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
517 else:
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
518 raise NotImplementedError('not implemented for {}'.format(n))
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
519 at[9:16] = 0x5a
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
520 return at.encode()
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
521
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
522
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
523
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
524 def register_numbers(regs):
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
525 for r in regs:
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
526 yield r.num
340
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents: 336
diff changeset
527
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
528 class Pop(ThumbInstruction):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
529 def __init__(self, regs):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
530 assert type(regs) is set
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
531 self.regs = regs
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
532 self.token = ThumbToken()
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
533
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
534 def __repr__(self):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
535 return 'Pop {{{}}}'.format(self.regs)
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
536
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
537 def encode(self):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
538 for n in register_numbers(self.regs):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
539 if n < 8:
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
540 self.token[n] = 1
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
541 elif n == 15:
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
542 self.token[8] = 1
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
543 else:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
544 raise NotImplementedError('not implemented for this register')
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
545 self.token[9:16] = 0x5E
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
546 return self.token.encode()
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
547
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
548
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
549
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
550 class Yield(ThumbInstruction):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
551 def encode(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
552 return u16(0xbf10)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
553
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
554 # misc:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
555
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
556 # add/sub SP:
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
557 class addspsp_base(ThumbInstruction):
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
558 def __init__(self, imm7):
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
559 self.imm7 = imm7
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
560 assert self.imm7 % 4 == 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
561 self.imm7 >>= 2
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
562
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
563 def encode(self):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
564 return u16((self.opcode << 7) | self.imm7)
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
565
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
566 def __repr__(self):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
567 mnemonic = self.__class__.__name__
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
568 return '{} sp, sp, {}'.format(mnemonic, self.imm7 << 2)
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
569
305
0615b5308710 Updated docs
Windel Bouwman
parents: 300
diff changeset
570
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
571 class AddSp(addspsp_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
572 opcode = 0b101100000
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
573
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
574
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
575 class SubSp(addspsp_base):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents:
diff changeset
576 opcode = 0b101100001