annotate python/ppci/target/thumb/instructions.py @ 348:442fb043d149

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