src/isa-thumb.c (view raw)
1#include "isa-thumb.h"
2
3#include "isa-inlines.h"
4
5static const ThumbInstruction _thumbTable[0x400];
6
7void ThumbStep(struct ARMCore* cpu) {
8 uint32_t address = cpu->gprs[ARM_PC];
9 cpu->gprs[ARM_PC] = address + WORD_SIZE_THUMB;
10 address -= WORD_SIZE_THUMB;
11 uint16_t opcode = ((uint16_t*) cpu->memory->activeRegion)[(address & cpu->memory->activeMask) >> 1];
12 ThumbInstruction instruction = _thumbTable[opcode >> 6];
13 instruction(cpu, opcode);
14}
15
16// Instruction definitions
17// Beware pre-processor insanity
18
19#define THUMB_ADDITION_S(M, N, D) \
20 cpu->cpsr.n = ARM_SIGN(D); \
21 cpu->cpsr.z = !(D); \
22 cpu->cpsr.c = ARM_CARRY_FROM(M, N, D); \
23 cpu->cpsr.v = ARM_V_ADDITION(M, N, D);
24
25#define THUMB_SUBTRACTION_S(M, N, D) \
26 cpu->cpsr.n = ARM_SIGN(D); \
27 cpu->cpsr.z = !(D); \
28 cpu->cpsr.c = ARM_BORROW_FROM(M, N, D); \
29 cpu->cpsr.v = ARM_V_SUBTRACTION(M, N, D);
30
31#define THUMB_NEUTRAL_S(M, N, D) \
32 cpu->cpsr.n = ARM_SIGN(D); \
33 cpu->cpsr.z = !(D);
34
35#define THUMB_ADDITION(D, M, N) \
36 int n = N; \
37 int m = M; \
38 D = M + N; \
39 THUMB_ADDITION_S(m, n, D)
40
41#define APPLY(F, ...) F(__VA_ARGS__)
42
43#define COUNT_1(EMITTER, PREFIX, ...) \
44 EMITTER(PREFIX ## 0, 0, __VA_ARGS__) \
45 EMITTER(PREFIX ## 1, 1, __VA_ARGS__)
46
47#define COUNT_2(EMITTER, PREFIX, ...) \
48 COUNT_1(EMITTER, PREFIX, __VA_ARGS__) \
49 EMITTER(PREFIX ## 2, 2, __VA_ARGS__) \
50 EMITTER(PREFIX ## 3, 3, __VA_ARGS__)
51
52#define COUNT_3(EMITTER, PREFIX, ...) \
53 COUNT_2(EMITTER, PREFIX, __VA_ARGS__) \
54 EMITTER(PREFIX ## 4, 4, __VA_ARGS__) \
55 EMITTER(PREFIX ## 5, 5, __VA_ARGS__) \
56 EMITTER(PREFIX ## 6, 6, __VA_ARGS__) \
57 EMITTER(PREFIX ## 7, 7, __VA_ARGS__)
58
59#define COUNT_4(EMITTER, PREFIX, ...) \
60 COUNT_3(EMITTER, PREFIX, __VA_ARGS__) \
61 EMITTER(PREFIX ## 8, 8, __VA_ARGS__) \
62 EMITTER(PREFIX ## 9, 9, __VA_ARGS__) \
63 EMITTER(PREFIX ## A, 10, __VA_ARGS__) \
64 EMITTER(PREFIX ## B, 11, __VA_ARGS__) \
65 EMITTER(PREFIX ## C, 12, __VA_ARGS__) \
66 EMITTER(PREFIX ## D, 13, __VA_ARGS__) \
67 EMITTER(PREFIX ## E, 14, __VA_ARGS__) \
68 EMITTER(PREFIX ## F, 15, __VA_ARGS__)
69
70#define COUNT_5(EMITTER, PREFIX, ...) \
71 COUNT_4(EMITTER, PREFIX ## 0, __VA_ARGS__) \
72 EMITTER(PREFIX ## 10, 16, __VA_ARGS__) \
73 EMITTER(PREFIX ## 11, 17, __VA_ARGS__) \
74 EMITTER(PREFIX ## 12, 18, __VA_ARGS__) \
75 EMITTER(PREFIX ## 13, 19, __VA_ARGS__) \
76 EMITTER(PREFIX ## 14, 20, __VA_ARGS__) \
77 EMITTER(PREFIX ## 15, 21, __VA_ARGS__) \
78 EMITTER(PREFIX ## 16, 22, __VA_ARGS__) \
79 EMITTER(PREFIX ## 17, 23, __VA_ARGS__) \
80 EMITTER(PREFIX ## 18, 24, __VA_ARGS__) \
81 EMITTER(PREFIX ## 19, 25, __VA_ARGS__) \
82 EMITTER(PREFIX ## 1A, 26, __VA_ARGS__) \
83 EMITTER(PREFIX ## 1B, 27, __VA_ARGS__) \
84 EMITTER(PREFIX ## 1C, 28, __VA_ARGS__) \
85 EMITTER(PREFIX ## 1D, 29, __VA_ARGS__) \
86 EMITTER(PREFIX ## 1E, 30, __VA_ARGS__) \
87 EMITTER(PREFIX ## 1F, 31, __VA_ARGS__) \
88
89#define DEFINE_INSTRUCTION_THUMB(NAME, BODY) \
90 static void _ThumbInstruction ## NAME (struct ARMCore* cpu, uint16_t opcode) { \
91 BODY; \
92 }
93
94#define DEFINE_IMMEDIATE_5_INSTRUCTION_EX_THUMB(NAME, IMMEDIATE, BODY) \
95 DEFINE_INSTRUCTION_THUMB(NAME, \
96 int immediate = IMMEDIATE; \
97 int rd = opcode & 0x0007; \
98 int rm = (opcode >> 3) & 0x0007; \
99 BODY;)
100
101#define DEFINE_IMMEDIATE_5_INSTRUCTION_THUMB(NAME, BODY) \
102 COUNT_5(DEFINE_IMMEDIATE_5_INSTRUCTION_EX_THUMB, NAME ## _, BODY)
103
104DEFINE_IMMEDIATE_5_INSTRUCTION_THUMB(LSL1, \
105 if (!immediate) { \
106 cpu->gprs[rd] = cpu->gprs[rm]; \
107 } else { \
108 cpu->cpsr.c = cpu->gprs[rm] & (1 << (32 - immediate)); \
109 cpu->gprs[rd] = cpu->gprs[rm] << immediate; \
110 } \
111 THUMB_NEUTRAL_S( , , cpu->gprs[rd]);)
112
113DEFINE_IMMEDIATE_5_INSTRUCTION_THUMB(LSR1,
114 if (!immediate) { \
115 cpu->cpsr.c = ARM_SIGN(cpu->gprs[rm]); \
116 cpu->gprs[rd] = 0; \
117 } else { \
118 cpu->cpsr.c = cpu->gprs[rm] & (1 << (immediate - 1)); \
119 cpu->gprs[rd] = ((uint32_t) cpu->gprs[rm]) >> immediate; \
120 } \
121 THUMB_NEUTRAL_S( , , cpu->gprs[rd]);)
122
123DEFINE_IMMEDIATE_5_INSTRUCTION_THUMB(ASR1, ARM_STUB)
124
125DEFINE_IMMEDIATE_5_INSTRUCTION_THUMB(LDR1, cpu->gprs[rd] = cpu->memory->load32(cpu->memory, cpu->gprs[rm] + immediate * 4))
126DEFINE_IMMEDIATE_5_INSTRUCTION_THUMB(LDRB1, ARM_STUB)
127DEFINE_IMMEDIATE_5_INSTRUCTION_THUMB(LDRH1, ARM_STUB)
128DEFINE_IMMEDIATE_5_INSTRUCTION_THUMB(STR1, cpu->memory->store32(cpu->memory, cpu->gprs[rm] + immediate * 4, cpu->gprs[rd]))
129DEFINE_IMMEDIATE_5_INSTRUCTION_THUMB(STRB1, ARM_STUB)
130DEFINE_IMMEDIATE_5_INSTRUCTION_THUMB(STRH1, cpu->memory->store16(cpu->memory, cpu->gprs[rm] + immediate * 2, cpu->gprs[rd]))
131
132#define DEFINE_DATA_FORM_1_INSTRUCTION_EX_THUMB(NAME, RM, BODY) \
133 DEFINE_INSTRUCTION_THUMB(NAME, \
134 int rm = RM; \
135 int rd = opcode & 0x0007; \
136 int rn = (opcode >> 3) & 0x0007; \
137 BODY;)
138
139#define DEFINE_DATA_FORM_1_INSTRUCTION_THUMB(NAME, BODY) \
140 COUNT_3(DEFINE_DATA_FORM_1_INSTRUCTION_EX_THUMB, NAME ## 3_R, BODY)
141
142DEFINE_DATA_FORM_1_INSTRUCTION_THUMB(ADD, THUMB_ADDITION(cpu->gprs[rd], cpu->gprs[rn], cpu->gprs[rm]))
143DEFINE_DATA_FORM_1_INSTRUCTION_THUMB(SUB, ARM_STUB)
144
145#define DEFINE_DATA_FORM_2_INSTRUCTION_EX_THUMB(NAME, IMMEDIATE, BODY) \
146 DEFINE_INSTRUCTION_THUMB(NAME, \
147 int immediate = IMMEDIATE; \
148 int rd = opcode & 0x0007; \
149 int rn = (opcode >> 3) & 0x0007; \
150 BODY;)
151
152#define DEFINE_DATA_FORM_2_INSTRUCTION_THUMB(NAME, BODY) \
153 COUNT_3(DEFINE_DATA_FORM_2_INSTRUCTION_EX_THUMB, NAME ## 1_, BODY)
154
155DEFINE_DATA_FORM_2_INSTRUCTION_THUMB(ADD, THUMB_ADDITION(cpu->gprs[rd], cpu->gprs[rn], immediate))
156
157DEFINE_DATA_FORM_2_INSTRUCTION_THUMB(SUB, ARM_STUB)
158
159#define DEFINE_DATA_FORM_3_INSTRUCTION_EX_THUMB(NAME, RD, BODY) \
160 DEFINE_INSTRUCTION_THUMB(NAME, \
161 int rd = RD; \
162 int immediate = opcode & 0x00FF; \
163 BODY;)
164
165#define DEFINE_DATA_FORM_3_INSTRUCTION_THUMB(NAME, BODY) \
166 COUNT_3(DEFINE_DATA_FORM_3_INSTRUCTION_EX_THUMB, NAME ## _R, BODY)
167
168DEFINE_DATA_FORM_3_INSTRUCTION_THUMB(ADD2, THUMB_ADDITION(cpu->gprs[rd], cpu->gprs[rd], immediate))
169
170
171DEFINE_DATA_FORM_3_INSTRUCTION_THUMB(CMP1, int aluOut = cpu->gprs[rd] - immediate; THUMB_SUBTRACTION_S(cpu->gprs[rd], immediate, aluOut))
172DEFINE_DATA_FORM_3_INSTRUCTION_THUMB(MOV1, cpu->gprs[rd] = immediate; THUMB_NEUTRAL_S(, , cpu->gprs[rd]))
173DEFINE_DATA_FORM_3_INSTRUCTION_THUMB(SUB2, ARM_STUB)
174
175#define DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(NAME, BODY) \
176 DEFINE_INSTRUCTION_THUMB(NAME, \
177 int rd = opcode & 0x0007; \
178 int rn = (opcode >> 3) & 0x0007; \
179 BODY;)
180
181DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(AND, cpu->gprs[rd] = cpu->gprs[rd] & cpu->gprs[rn]; THUMB_NEUTRAL_S( , , cpu->gprs[rd]))
182DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(EOR, cpu->gprs[rd] = cpu->gprs[rd] ^ cpu->gprs[rn]; THUMB_NEUTRAL_S( , , cpu->gprs[rd]))
183DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(LSL2, ARM_STUB)
184DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(LSR2, ARM_STUB)
185DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(ASR2, \
186 int rs = cpu->gprs[rn] & 0xFF; \
187 if (rs) { \
188 if (rs < 32) { \
189 cpu->cpsr.c = cpu->gprs[rd] & (1 << (rs - 1)); \
190 cpu->gprs[rd] >>= rs; \
191 } else { \
192 cpu->cpsr.c = ARM_SIGN(cpu->gprs[rd]); \
193 if (cpu->cpsr.c) { \
194 cpu->gprs[rd] = 0xFFFFFFFF; \
195 } else { \
196 cpu->gprs[rd] = 0; \
197 } \
198 } \
199 } \
200 THUMB_NEUTRAL_S( , , cpu->gprs[rd]))
201
202DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(ADC, ARM_STUB)
203DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(SBC, ARM_STUB)
204DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(ROR, ARM_STUB)
205DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(TST, ARM_STUB)
206DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(NEG, ARM_STUB)
207DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(CMP2, ARM_STUB)
208DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(CMN, ARM_STUB)
209DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(ORR, cpu->gprs[rd] = cpu->gprs[rd] | cpu->gprs[rn]; THUMB_NEUTRAL_S( , , cpu->gprs[rd]))
210DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(MUL, ARM_STUB)
211DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(BIC, ARM_STUB)
212DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(MVN, ARM_STUB)
213
214#define DEFINE_INSTRUCTION_WITH_HIGH_EX_THUMB(NAME, H1, H2, BODY) \
215 DEFINE_INSTRUCTION_THUMB(NAME, \
216 int rd = opcode & 0x0007 | H1; \
217 int rm = (opcode >> 3) & 0x0007 | H2; \
218 BODY;)
219
220#define DEFINE_INSTRUCTION_WITH_HIGH_THUMB(NAME, BODY) \
221 DEFINE_INSTRUCTION_WITH_HIGH_EX_THUMB(NAME ## 00, 0, 0, BODY) \
222 DEFINE_INSTRUCTION_WITH_HIGH_EX_THUMB(NAME ## 01, 0, 8, BODY) \
223 DEFINE_INSTRUCTION_WITH_HIGH_EX_THUMB(NAME ## 10, 8, 0, BODY) \
224 DEFINE_INSTRUCTION_WITH_HIGH_EX_THUMB(NAME ## 11, 8, 8, BODY)
225
226DEFINE_INSTRUCTION_WITH_HIGH_THUMB(ADD4, cpu->gprs[rd] += cpu->gprs[rm])
227DEFINE_INSTRUCTION_WITH_HIGH_THUMB(CMP3, int32_t aluOut = cpu->gprs[rd] - cpu->gprs[rm]; THUMB_SUBTRACTION_S(cpu->gprs[rd], cpu->gprs[rm], aluOut))
228DEFINE_INSTRUCTION_WITH_HIGH_THUMB(MOV3, cpu->gprs[rd] = cpu->gprs[rm])
229
230#define DEFINE_IMMEDIATE_WITH_REGISTER_EX_THUMB(NAME, RD, BODY) \
231 DEFINE_INSTRUCTION_THUMB(NAME, \
232 int rd = RD; \
233 int immediate = (opcode & 0x00FF) << 2; \
234 BODY;)
235
236#define DEFINE_IMMEDIATE_WITH_REGISTER_THUMB(NAME, BODY) \
237 COUNT_3(DEFINE_IMMEDIATE_WITH_REGISTER_EX_THUMB, NAME ## _R, BODY)
238
239DEFINE_IMMEDIATE_WITH_REGISTER_THUMB(LDR3, cpu->gprs[rd] = cpu->memory->load32(cpu->memory, cpu->gprs[ARM_PC] + immediate))
240DEFINE_IMMEDIATE_WITH_REGISTER_THUMB(LDR4, cpu->gprs[rd] = cpu->memory->load32(cpu->memory, cpu->gprs[ARM_SP] + immediate))
241DEFINE_IMMEDIATE_WITH_REGISTER_THUMB(STR3, cpu->memory->store32(cpu->memory, cpu->gprs[ARM_SP] + immediate, cpu->gprs[rd]))
242
243DEFINE_IMMEDIATE_WITH_REGISTER_THUMB(ADD5, ARM_STUB)
244DEFINE_IMMEDIATE_WITH_REGISTER_THUMB(ADD6, cpu->gprs[rd] = cpu->gprs[ARM_SP] + immediate)
245
246#define DEFINE_LOAD_STORE_WITH_REGISTER_EX_THUMB(NAME, RM, BODY) \
247 DEFINE_INSTRUCTION_THUMB(NAME, \
248 int rm = RM; \
249 BODY;)
250
251#define DEFINE_LOAD_STORE_WITH_REGISTER_THUMB(NAME, BODY) \
252 COUNT_3(DEFINE_LOAD_STORE_WITH_REGISTER_EX_THUMB, NAME ## _R, BODY)
253
254DEFINE_LOAD_STORE_WITH_REGISTER_THUMB(LDR2, ARM_STUB)
255DEFINE_LOAD_STORE_WITH_REGISTER_THUMB(LDRB2, ARM_STUB)
256DEFINE_LOAD_STORE_WITH_REGISTER_THUMB(LDRH2, ARM_STUB)
257DEFINE_LOAD_STORE_WITH_REGISTER_THUMB(LDRSB, ARM_STUB)
258DEFINE_LOAD_STORE_WITH_REGISTER_THUMB(LDRSH, ARM_STUB)
259DEFINE_LOAD_STORE_WITH_REGISTER_THUMB(STR2, ARM_STUB)
260DEFINE_LOAD_STORE_WITH_REGISTER_THUMB(STRB2, ARM_STUB)
261DEFINE_LOAD_STORE_WITH_REGISTER_THUMB(STRH2, ARM_STUB)
262
263#define DEFINE_LOAD_STORE_MULTIPLE_EX_THUMB(NAME, RS, ADDRESS, LOOP, BODY, OP, PRE_BODY, POST_BODY, WRITEBACK) \
264 DEFINE_INSTRUCTION_THUMB(NAME, \
265 int rn = (opcode >> 8) & 0x000F; \
266 int rs = RS; \
267 int32_t address = ADDRESS; \
268 int m; \
269 int i; \
270 PRE_BODY; \
271 for LOOP { \
272 if (rs & m) { \
273 BODY; \
274 address OP 4; \
275 } \
276 } \
277 POST_BODY; \
278 WRITEBACK;)
279
280#define DEFINE_LOAD_STORE_MULTIPLE_THUMB(NAME, BODY, WRITEBACK) \
281 COUNT_3(DEFINE_LOAD_STORE_MULTIPLE_EX_THUMB, NAME ## _R, cpu->gprs[rn], (m = 0x01, i = 0; i < 8; m <<= 1, ++i), BODY, +=, , , WRITEBACK)
282
283DEFINE_LOAD_STORE_MULTIPLE_THUMB(LDMIA,\
284 cpu->gprs[i] = cpu->memory->load32(cpu->memory, address), \
285 if (!((1 << rn) & rs)) { \
286 cpu->gprs[rn] = address; \
287 })
288
289DEFINE_LOAD_STORE_MULTIPLE_THUMB(STMIA, \
290 cpu->memory->store32(cpu->memory, address, cpu->gprs[i]), \
291 cpu->gprs[rn] = address)
292
293#define DEFINE_CONDITIONAL_BRANCH_THUMB(COND) \
294 DEFINE_INSTRUCTION_THUMB(B ## COND, \
295 if (ARM_COND_ ## COND) { \
296 int8_t immediate = opcode; \
297 cpu->gprs[ARM_PC] += immediate << 1; \
298 THUMB_WRITE_PC; \
299 })
300
301DEFINE_CONDITIONAL_BRANCH_THUMB(EQ)
302DEFINE_CONDITIONAL_BRANCH_THUMB(NE)
303DEFINE_CONDITIONAL_BRANCH_THUMB(CS)
304DEFINE_CONDITIONAL_BRANCH_THUMB(CC)
305DEFINE_CONDITIONAL_BRANCH_THUMB(MI)
306DEFINE_CONDITIONAL_BRANCH_THUMB(PL)
307DEFINE_CONDITIONAL_BRANCH_THUMB(VS)
308DEFINE_CONDITIONAL_BRANCH_THUMB(VC)
309DEFINE_CONDITIONAL_BRANCH_THUMB(LS)
310DEFINE_CONDITIONAL_BRANCH_THUMB(HI)
311DEFINE_CONDITIONAL_BRANCH_THUMB(GE)
312DEFINE_CONDITIONAL_BRANCH_THUMB(LT)
313DEFINE_CONDITIONAL_BRANCH_THUMB(GT)
314DEFINE_CONDITIONAL_BRANCH_THUMB(LE)
315
316DEFINE_INSTRUCTION_THUMB(ADD7, cpu->gprs[ARM_SP] += (opcode & 0x7F) << 2)
317DEFINE_INSTRUCTION_THUMB(SUB4, cpu->gprs[ARM_SP] -= (opcode & 0x7F) << 2)
318
319DEFINE_LOAD_STORE_MULTIPLE_EX_THUMB(POP, \
320 opcode & 0x00FF, \
321 cpu->gprs[ARM_SP], \
322 (m = 0x01, i = 0; i < 8; m <<= 1, ++i), \
323 cpu->gprs[i] = cpu->memory->load32(cpu->memory, address), \
324 +=, \
325 , , \
326 cpu->gprs[ARM_SP] = address)
327
328DEFINE_LOAD_STORE_MULTIPLE_EX_THUMB(POPR, \
329 opcode & 0x00FF, \
330 cpu->gprs[ARM_SP], \
331 (m = 0x01, i = 0; i < 8; m <<= 1, ++i), \
332 cpu->gprs[i] = cpu->memory->load32(cpu->memory, address), \
333 +=, \
334 , \
335 cpu->gprs[ARM_PC] = cpu->memory->load32(cpu->memory, address) & 0xFFFFFFFE; \
336 address += 4;, \
337 cpu->gprs[ARM_SP] = address)
338
339DEFINE_LOAD_STORE_MULTIPLE_EX_THUMB(PUSH, \
340 opcode & 0x00FF, \
341 cpu->gprs[ARM_SP] - 4, \
342 (m = 0x80, i = 7; m; m >>= 1, --i), \
343 cpu->memory->store32(cpu->memory, address, cpu->gprs[i]), \
344 -=, \
345 , , \
346 cpu->gprs[ARM_SP] = address + 4)
347
348DEFINE_LOAD_STORE_MULTIPLE_EX_THUMB(PUSHR, \
349 opcode & 0x00FF, \
350 cpu->gprs[ARM_SP] - 4, \
351 (m = 0x80, i = 7; m; m >>= 1, --i), \
352 cpu->memory->store32(cpu->memory, address, cpu->gprs[i]), \
353 -=, \
354 cpu->memory->store32(cpu->memory, address, cpu->gprs[ARM_LR]); \
355 address -= 4;, \
356 , \
357 cpu->gprs[ARM_SP] = address + 4)
358
359DEFINE_INSTRUCTION_THUMB(ILL, ARM_STUB)
360DEFINE_INSTRUCTION_THUMB(BKPT, ARM_STUB)
361DEFINE_INSTRUCTION_THUMB(B, \
362 int16_t immediate = (opcode & 0x07FF) << 5; \
363 cpu->gprs[ARM_PC] += (((int32_t) immediate) >> 4); \
364 THUMB_WRITE_PC;)
365
366DEFINE_INSTRUCTION_THUMB(BL1, \
367 int16_t immediate = (opcode & 0x07FF) << 5; \
368 cpu->gprs[ARM_LR] = cpu->gprs[ARM_PC] + (((int32_t) immediate) << 7);)
369
370DEFINE_INSTRUCTION_THUMB(BL2, \
371 uint16_t immediate = (opcode & 0x07FF) << 1; \
372 uint32_t pc = cpu->gprs[ARM_PC]; \
373 cpu->gprs[ARM_PC] = cpu->gprs[ARM_LR] + immediate; \
374 cpu->gprs[ARM_LR] = pc - 1; \
375 THUMB_WRITE_PC;)
376
377DEFINE_INSTRUCTION_THUMB(BX, ARM_STUB)
378DEFINE_INSTRUCTION_THUMB(SWI, ARM_STUB)
379
380#define DECLARE_INSTRUCTION_THUMB(EMITTER, NAME) \
381 EMITTER ## NAME
382
383#define DECLARE_INSTRUCTION_WITH_HIGH_THUMB(EMITTER, NAME) \
384 DECLARE_INSTRUCTION_THUMB(EMITTER, NAME ## 00), \
385 DECLARE_INSTRUCTION_THUMB(EMITTER, NAME ## 01), \
386 DECLARE_INSTRUCTION_THUMB(EMITTER, NAME ## 10), \
387 DECLARE_INSTRUCTION_THUMB(EMITTER, NAME ## 11)
388
389#define DUMMY(X, ...) X,
390#define DUMMY_4(...) \
391 DUMMY(__VA_ARGS__) \
392 DUMMY(__VA_ARGS__) \
393 DUMMY(__VA_ARGS__) \
394 DUMMY(__VA_ARGS__)
395
396#define DECLARE_THUMB_EMITTER_BLOCK(EMITTER) \
397 APPLY(COUNT_5, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, LSL1_)) \
398 APPLY(COUNT_5, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, LSR1_)) \
399 APPLY(COUNT_5, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, ASR1_)) \
400 APPLY(COUNT_3, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, ADD3_R)) \
401 APPLY(COUNT_3, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, SUB3_R)) \
402 APPLY(COUNT_3, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, ADD1_)) \
403 APPLY(COUNT_3, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, SUB1_)) \
404 APPLY(COUNT_3, DUMMY_4, DECLARE_INSTRUCTION_THUMB(EMITTER, MOV1_R)) \
405 APPLY(COUNT_3, DUMMY_4, DECLARE_INSTRUCTION_THUMB(EMITTER, CMP1_R)) \
406 APPLY(COUNT_3, DUMMY_4, DECLARE_INSTRUCTION_THUMB(EMITTER, ADD2_R)) \
407 APPLY(COUNT_3, DUMMY_4, DECLARE_INSTRUCTION_THUMB(EMITTER, SUB2_R)) \
408 DECLARE_INSTRUCTION_THUMB(EMITTER, AND), \
409 DECLARE_INSTRUCTION_THUMB(EMITTER, EOR), \
410 DECLARE_INSTRUCTION_THUMB(EMITTER, LSL2), \
411 DECLARE_INSTRUCTION_THUMB(EMITTER, LSR2), \
412 DECLARE_INSTRUCTION_THUMB(EMITTER, ASR2), \
413 DECLARE_INSTRUCTION_THUMB(EMITTER, ADC), \
414 DECLARE_INSTRUCTION_THUMB(EMITTER, SBC), \
415 DECLARE_INSTRUCTION_THUMB(EMITTER, ROR), \
416 DECLARE_INSTRUCTION_THUMB(EMITTER, TST), \
417 DECLARE_INSTRUCTION_THUMB(EMITTER, NEG), \
418 DECLARE_INSTRUCTION_THUMB(EMITTER, CMP2), \
419 DECLARE_INSTRUCTION_THUMB(EMITTER, CMN), \
420 DECLARE_INSTRUCTION_THUMB(EMITTER, ORR), \
421 DECLARE_INSTRUCTION_THUMB(EMITTER, MUL), \
422 DECLARE_INSTRUCTION_THUMB(EMITTER, BIC), \
423 DECLARE_INSTRUCTION_THUMB(EMITTER, MVN), \
424 DECLARE_INSTRUCTION_WITH_HIGH_THUMB(EMITTER, ADD4), \
425 DECLARE_INSTRUCTION_WITH_HIGH_THUMB(EMITTER, CMP3), \
426 DECLARE_INSTRUCTION_WITH_HIGH_THUMB(EMITTER, MOV3), \
427 DECLARE_INSTRUCTION_THUMB(EMITTER, BX), \
428 DECLARE_INSTRUCTION_THUMB(EMITTER, BX), \
429 DECLARE_INSTRUCTION_THUMB(EMITTER, ILL), \
430 DECLARE_INSTRUCTION_THUMB(EMITTER, ILL), \
431 APPLY(COUNT_3, DUMMY_4, DECLARE_INSTRUCTION_THUMB(EMITTER, LDR3_R)) \
432 APPLY(COUNT_3, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, STR2_R)) \
433 APPLY(COUNT_3, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, STRH2_R)) \
434 APPLY(COUNT_3, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, STRB2_R)) \
435 APPLY(COUNT_3, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, LDRSB_R)) \
436 APPLY(COUNT_3, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, LDR2_R)) \
437 APPLY(COUNT_3, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, LDRH2_R)) \
438 APPLY(COUNT_3, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, LDRB2_R)) \
439 APPLY(COUNT_3, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, LDRSH_R)) \
440 APPLY(COUNT_5, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, STR1_)) \
441 APPLY(COUNT_5, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, LDR1_)) \
442 APPLY(COUNT_5, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, STRB1_)) \
443 APPLY(COUNT_5, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, LDRB1_)) \
444 APPLY(COUNT_5, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, STRH1_)) \
445 APPLY(COUNT_5, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, LDRH1_)) \
446 APPLY(COUNT_3, DUMMY_4, DECLARE_INSTRUCTION_THUMB(EMITTER, STR3_R)) \
447 APPLY(COUNT_3, DUMMY_4, DECLARE_INSTRUCTION_THUMB(EMITTER, LDR4_R)) \
448 APPLY(COUNT_3, DUMMY_4, DECLARE_INSTRUCTION_THUMB(EMITTER, ADD5_R)) \
449 APPLY(COUNT_3, DUMMY_4, DECLARE_INSTRUCTION_THUMB(EMITTER, ADD6_R)) \
450 DECLARE_INSTRUCTION_THUMB(EMITTER, ADD7), \
451 DECLARE_INSTRUCTION_THUMB(EMITTER, ADD7), \
452 DECLARE_INSTRUCTION_THUMB(EMITTER, SUB4), \
453 DECLARE_INSTRUCTION_THUMB(EMITTER, SUB4), \
454 DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, ILL)), \
455 DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, ILL)), \
456 DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, ILL)), \
457 DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, PUSH)), \
458 DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, PUSHR)), \
459 DO_8(DECLARE_INSTRUCTION_THUMB(EMITTER, ILL)), \
460 DO_8(DECLARE_INSTRUCTION_THUMB(EMITTER, ILL)), \
461 DO_8(DECLARE_INSTRUCTION_THUMB(EMITTER, ILL)), \
462 DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, POP)), \
463 DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, POPR)), \
464 DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BKPT)), \
465 DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, ILL)), \
466 APPLY(COUNT_3, DUMMY_4, DECLARE_INSTRUCTION_THUMB(EMITTER, STMIA_R)) \
467 APPLY(COUNT_3, DUMMY_4, DECLARE_INSTRUCTION_THUMB(EMITTER, LDMIA_R)) \
468 DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BEQ)), \
469 DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BNE)), \
470 DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BCS)), \
471 DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BCC)), \
472 DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BMI)), \
473 DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BPL)), \
474 DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BVS)), \
475 DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BVC)), \
476 DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BHI)), \
477 DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BLS)), \
478 DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BGE)), \
479 DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BLT)), \
480 DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BGT)), \
481 DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BLE)), \
482 DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, ILL)), \
483 DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, SWI)), \
484 DO_8(DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, B))), \
485 DO_8(DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, ILL))), \
486 DO_8(DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BL1))), \
487 DO_8(DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BL2))) \
488
489static const ThumbInstruction _thumbTable[0x400] = {
490 DECLARE_THUMB_EMITTER_BLOCK(_ThumbInstruction)
491};