diff --git a/src/isa-thumb.c b/src/isa-thumb.c index 3e9bd03b7..dcb1f8842 100644 --- a/src/isa-thumb.c +++ b/src/isa-thumb.c @@ -3,40 +3,55 @@ static const ThumbInstruction _thumbTable[0x400]; // Instruction definitions +// Beware pre-processor insanity #define APPLY(F, ...) F(__VA_ARGS__) -#define DUMMY(...) __VA_ARGS__ #define COUNT_1(EMITTER, PREFIX, ...) \ - EMITTER(PREFIX ## 0, __VA_ARGS__) \ - EMITTER(PREFIX ## 1, __VA_ARGS__) + EMITTER(PREFIX ## 0, 0, __VA_ARGS__) \ + EMITTER(PREFIX ## 1, 1, __VA_ARGS__) #define COUNT_2(EMITTER, PREFIX, ...) \ COUNT_1(EMITTER, PREFIX, __VA_ARGS__) \ - EMITTER(PREFIX ## 2, __VA_ARGS__) \ - EMITTER(PREFIX ## 3, __VA_ARGS__) + EMITTER(PREFIX ## 2, 2, __VA_ARGS__) \ + EMITTER(PREFIX ## 3, 3, __VA_ARGS__) #define COUNT_3(EMITTER, PREFIX, ...) \ COUNT_2(EMITTER, PREFIX, __VA_ARGS__) \ - EMITTER(PREFIX ## 4, __VA_ARGS__) \ - EMITTER(PREFIX ## 5, __VA_ARGS__) \ - EMITTER(PREFIX ## 6, __VA_ARGS__) \ - EMITTER(PREFIX ## 7, __VA_ARGS__) + EMITTER(PREFIX ## 4, 4, __VA_ARGS__) \ + EMITTER(PREFIX ## 5, 5, __VA_ARGS__) \ + EMITTER(PREFIX ## 6, 6, __VA_ARGS__) \ + EMITTER(PREFIX ## 7, 7, __VA_ARGS__) #define COUNT_4(EMITTER, PREFIX, ...) \ COUNT_3(EMITTER, PREFIX, __VA_ARGS__) \ - EMITTER(PREFIX ## 8, __VA_ARGS__) \ - EMITTER(PREFIX ## 9, __VA_ARGS__) \ - EMITTER(PREFIX ## A, __VA_ARGS__) \ - EMITTER(PREFIX ## B, __VA_ARGS__) \ - EMITTER(PREFIX ## C, __VA_ARGS__) \ - EMITTER(PREFIX ## D, __VA_ARGS__) \ - EMITTER(PREFIX ## E, __VA_ARGS__) \ - EMITTER(PREFIX ## F, __VA_ARGS__) + EMITTER(PREFIX ## 8, 8, __VA_ARGS__) \ + EMITTER(PREFIX ## 9, 9, __VA_ARGS__) \ + EMITTER(PREFIX ## A, 10, __VA_ARGS__) \ + EMITTER(PREFIX ## B, 11, __VA_ARGS__) \ + EMITTER(PREFIX ## C, 12, __VA_ARGS__) \ + EMITTER(PREFIX ## D, 13, __VA_ARGS__) \ + EMITTER(PREFIX ## E, 14, __VA_ARGS__) \ + EMITTER(PREFIX ## F, 15, __VA_ARGS__) #define COUNT_5(EMITTER, PREFIX, ...) \ COUNT_4(EMITTER, PREFIX ## 0, __VA_ARGS__) \ - COUNT_4(EMITTER, PREFIX ## 1, __VA_ARGS__) + EMITTER(PREFIX ## 10, 16, __VA_ARGS__) \ + EMITTER(PREFIX ## 11, 17, __VA_ARGS__) \ + EMITTER(PREFIX ## 12, 18, __VA_ARGS__) \ + EMITTER(PREFIX ## 13, 19, __VA_ARGS__) \ + EMITTER(PREFIX ## 14, 20, __VA_ARGS__) \ + EMITTER(PREFIX ## 15, 21, __VA_ARGS__) \ + EMITTER(PREFIX ## 16, 22, __VA_ARGS__) \ + EMITTER(PREFIX ## 17, 23, __VA_ARGS__) \ + EMITTER(PREFIX ## 18, 24, __VA_ARGS__) \ + EMITTER(PREFIX ## 19, 25, __VA_ARGS__) \ + EMITTER(PREFIX ## 1A, 26, __VA_ARGS__) \ + EMITTER(PREFIX ## 1B, 27, __VA_ARGS__) \ + EMITTER(PREFIX ## 1C, 28, __VA_ARGS__) \ + EMITTER(PREFIX ## 1D, 29, __VA_ARGS__) \ + EMITTER(PREFIX ## 1E, 30, __VA_ARGS__) \ + EMITTER(PREFIX ## 1F, 31, __VA_ARGS__) \ #define THUMB_WRITE_PC \ cpu->gprs[ARM_PC] = (cpu->gprs[ARM_PC] & -WORD_SIZE_THUMB) + WORD_SIZE_THUMB @@ -46,20 +61,39 @@ static const ThumbInstruction _thumbTable[0x400]; BODY; \ } -#define DEFINE_SHIFT_INSTRUCTION_THUMB(NAME, BODY) \ - COUNT_5(DEFINE_INSTRUCTION_THUMB, NAME ## _, BODY) +#define DEFINE_SHIFT_1_INSTRUCTION_EX_THUMB(NAME, IMMEDIATE, BODY) \ + DEFINE_INSTRUCTION_THUMB(NAME, \ + int immediate = IMMEDIATE; \ + BODY;) -DEFINE_SHIFT_INSTRUCTION_THUMB(LSL, ) -DEFINE_SHIFT_INSTRUCTION_THUMB(LSR, ) -DEFINE_SHIFT_INSTRUCTION_THUMB(ASR, ) +#define DEFINE_SHIFT_1_INSTRUCTION_THUMB(NAME, BODY) \ + COUNT_5(DEFINE_SHIFT_1_INSTRUCTION_EX_THUMB, NAME ## 1_, BODY) + +DEFINE_SHIFT_1_INSTRUCTION_THUMB(LSL, ) +DEFINE_SHIFT_1_INSTRUCTION_THUMB(LSR, ) +DEFINE_SHIFT_1_INSTRUCTION_THUMB(ASR, ) + +#define DEFINE_ADD_SUB_3_INSTRUCTION_EX_THUMB(NAME, RM, BODY) \ + DEFINE_INSTRUCTION_THUMB(NAME, \ + int rm = RM; \ + BODY;) + +#define DEFINE_ADD_SUB_3_INSTRUCTION_THUMB(NAME, BODY) \ + COUNT_3(DEFINE_ADD_SUB_3_INSTRUCTION_EX_THUMB, NAME ## 3_R, BODY) + +DEFINE_ADD_SUB_3_INSTRUCTION_THUMB(ADD, ) +DEFINE_ADD_SUB_3_INSTRUCTION_THUMB(SUB, ) #define DECLARE_INSTRUCTION_THUMB(EMITTER, NAME) \ EMITTER ## NAME +#define DUMMY(X, ...) X, #define DECLARE_THUMB_EMITTER_BLOCK(EMITTER) \ - APPLY(COUNT_5, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, LSL_)) \ - APPLY(COUNT_5, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, LSR_)) \ - APPLY(COUNT_5, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, ASR_)) \ + APPLY(COUNT_5, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, LSL1_)) \ + APPLY(COUNT_5, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, LSR1_)) \ + APPLY(COUNT_5, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, ASR1_)) \ + APPLY(COUNT_3, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, ADD3_R)) \ + APPLY(COUNT_3, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, SUB3_R)) \ static const ThumbInstruction _thumbTable[0x400] = { DECLARE_THUMB_EMITTER_BLOCK(_ThumbInstruction)