src/isa-thumb.c (view raw)
1#include "isa-thumb.h"
2
3static const ThumbInstruction _thumbTable[0x400];
4
5// Instruction definitions
6// Beware pre-processor insanity
7
8#define APPLY(F, ...) F(__VA_ARGS__)
9
10#define COUNT_1(EMITTER, PREFIX, ...) \
11 EMITTER(PREFIX ## 0, 0, __VA_ARGS__) \
12 EMITTER(PREFIX ## 1, 1, __VA_ARGS__)
13
14#define COUNT_2(EMITTER, PREFIX, ...) \
15 COUNT_1(EMITTER, PREFIX, __VA_ARGS__) \
16 EMITTER(PREFIX ## 2, 2, __VA_ARGS__) \
17 EMITTER(PREFIX ## 3, 3, __VA_ARGS__)
18
19#define COUNT_3(EMITTER, PREFIX, ...) \
20 COUNT_2(EMITTER, PREFIX, __VA_ARGS__) \
21 EMITTER(PREFIX ## 4, 4, __VA_ARGS__) \
22 EMITTER(PREFIX ## 5, 5, __VA_ARGS__) \
23 EMITTER(PREFIX ## 6, 6, __VA_ARGS__) \
24 EMITTER(PREFIX ## 7, 7, __VA_ARGS__)
25
26#define COUNT_4(EMITTER, PREFIX, ...) \
27 COUNT_3(EMITTER, PREFIX, __VA_ARGS__) \
28 EMITTER(PREFIX ## 8, 8, __VA_ARGS__) \
29 EMITTER(PREFIX ## 9, 9, __VA_ARGS__) \
30 EMITTER(PREFIX ## A, 10, __VA_ARGS__) \
31 EMITTER(PREFIX ## B, 11, __VA_ARGS__) \
32 EMITTER(PREFIX ## C, 12, __VA_ARGS__) \
33 EMITTER(PREFIX ## D, 13, __VA_ARGS__) \
34 EMITTER(PREFIX ## E, 14, __VA_ARGS__) \
35 EMITTER(PREFIX ## F, 15, __VA_ARGS__)
36
37#define COUNT_5(EMITTER, PREFIX, ...) \
38 COUNT_4(EMITTER, PREFIX ## 0, __VA_ARGS__) \
39 EMITTER(PREFIX ## 10, 16, __VA_ARGS__) \
40 EMITTER(PREFIX ## 11, 17, __VA_ARGS__) \
41 EMITTER(PREFIX ## 12, 18, __VA_ARGS__) \
42 EMITTER(PREFIX ## 13, 19, __VA_ARGS__) \
43 EMITTER(PREFIX ## 14, 20, __VA_ARGS__) \
44 EMITTER(PREFIX ## 15, 21, __VA_ARGS__) \
45 EMITTER(PREFIX ## 16, 22, __VA_ARGS__) \
46 EMITTER(PREFIX ## 17, 23, __VA_ARGS__) \
47 EMITTER(PREFIX ## 18, 24, __VA_ARGS__) \
48 EMITTER(PREFIX ## 19, 25, __VA_ARGS__) \
49 EMITTER(PREFIX ## 1A, 26, __VA_ARGS__) \
50 EMITTER(PREFIX ## 1B, 27, __VA_ARGS__) \
51 EMITTER(PREFIX ## 1C, 28, __VA_ARGS__) \
52 EMITTER(PREFIX ## 1D, 29, __VA_ARGS__) \
53 EMITTER(PREFIX ## 1E, 30, __VA_ARGS__) \
54 EMITTER(PREFIX ## 1F, 31, __VA_ARGS__) \
55
56#define THUMB_WRITE_PC \
57 cpu->gprs[ARM_PC] = (cpu->gprs[ARM_PC] & -WORD_SIZE_THUMB) + WORD_SIZE_THUMB
58
59#define DEFINE_INSTRUCTION_THUMB(NAME, BODY) \
60 static void _ThumbInstruction ## NAME (struct ARMCore* cpu, uint16_t opcode) { \
61 BODY; \
62 }
63
64#define DEFINE_SHIFT_1_INSTRUCTION_EX_THUMB(NAME, IMMEDIATE, BODY) \
65 DEFINE_INSTRUCTION_THUMB(NAME, \
66 int immediate = IMMEDIATE; \
67 BODY;)
68
69#define DEFINE_SHIFT_1_INSTRUCTION_THUMB(NAME, BODY) \
70 COUNT_5(DEFINE_SHIFT_1_INSTRUCTION_EX_THUMB, NAME ## 1_, BODY)
71
72DEFINE_SHIFT_1_INSTRUCTION_THUMB(LSL, )
73DEFINE_SHIFT_1_INSTRUCTION_THUMB(LSR, )
74DEFINE_SHIFT_1_INSTRUCTION_THUMB(ASR, )
75
76#define DEFINE_DATA_FORM_1_INSTRUCTION_EX_THUMB(NAME, RM, BODY) \
77 DEFINE_INSTRUCTION_THUMB(NAME, \
78 int rm = RM; \
79 BODY;)
80
81#define DEFINE_DATA_FORM_1_INSTRUCTION_THUMB(NAME, BODY) \
82 COUNT_3(DEFINE_DATA_FORM_1_INSTRUCTION_EX_THUMB, NAME ## 3_R, BODY)
83
84DEFINE_DATA_FORM_1_INSTRUCTION_THUMB(ADD, )
85DEFINE_DATA_FORM_1_INSTRUCTION_THUMB(SUB, )
86
87#define DECLARE_INSTRUCTION_THUMB(EMITTER, NAME) \
88 EMITTER ## NAME
89#define DEFINE_DATA_FORM_2_INSTRUCTION_EX_THUMB(NAME, IMMEDIATE, BODY) \
90 DEFINE_INSTRUCTION_THUMB(NAME, \
91 int immediate = IMMEDIATE; \
92 BODY;)
93
94#define DEFINE_DATA_FORM_2_INSTRUCTION_THUMB(NAME, BODY) \
95 COUNT_3(DEFINE_DATA_FORM_2_INSTRUCTION_EX_THUMB, NAME ## 1_, BODY)
96
97DEFINE_DATA_FORM_2_INSTRUCTION_THUMB(ADD, )
98DEFINE_DATA_FORM_2_INSTRUCTION_THUMB(SUB, )
99
100#define DEFINE_DATA_FORM_3_INSTRUCTION_EX_THUMB(NAME, RM, BODY) \
101 DEFINE_INSTRUCTION_THUMB(NAME, \
102 int rd = RD; \
103 BODY;)
104
105#define DEFINE_DATA_FORM_3_INSTRUCTION_THUMB(NAME, BODY) \
106 COUNT_3(DEFINE_DATA_FORM_2_INSTRUCTION_EX_THUMB, NAME ## _R, BODY)
107
108DEFINE_DATA_FORM_3_INSTRUCTION_THUMB(ADD2, )
109DEFINE_DATA_FORM_3_INSTRUCTION_THUMB(CMP1, )
110DEFINE_DATA_FORM_3_INSTRUCTION_THUMB(MOV1, )
111DEFINE_DATA_FORM_3_INSTRUCTION_THUMB(SUB2, )
112
113#define DUMMY(X, ...) X,
114#define DUMMY_4(...) \
115 DUMMY(__VA_ARGS__) \
116 DUMMY(__VA_ARGS__) \
117 DUMMY(__VA_ARGS__) \
118 DUMMY(__VA_ARGS__)
119
120#define DECLARE_THUMB_EMITTER_BLOCK(EMITTER) \
121 APPLY(COUNT_5, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, LSL1_)) \
122 APPLY(COUNT_5, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, LSR1_)) \
123 APPLY(COUNT_5, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, ASR1_)) \
124 APPLY(COUNT_3, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, ADD3_R)) \
125 APPLY(COUNT_3, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, SUB3_R)) \
126 APPLY(COUNT_3, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, ADD1_)) \
127 APPLY(COUNT_3, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, SUB1_)) \
128 APPLY(COUNT_3, DUMMY_4, DECLARE_INSTRUCTION_THUMB(EMITTER, MOV1_R)) \
129 APPLY(COUNT_3, DUMMY_4, DECLARE_INSTRUCTION_THUMB(EMITTER, CMP1_R)) \
130 APPLY(COUNT_3, DUMMY_4, DECLARE_INSTRUCTION_THUMB(EMITTER, ADD2_R)) \
131 APPLY(COUNT_3, DUMMY_4, DECLARE_INSTRUCTION_THUMB(EMITTER, SUB2_R)) \
132
133static const ThumbInstruction _thumbTable[0x400] = {
134 DECLARE_THUMB_EMITTER_BLOCK(_ThumbInstruction)
135};