Implement memory decoding
Jeffrey Pfau jeffrey@endrift.com
Fri, 01 Nov 2013 02:12:34 -0700
2 files changed,
50 insertions(+),
5 deletions(-)
M
src/arm/decoder-thumb.c
→
src/arm/decoder-thumb.c
@@ -29,7 +29,7 @@ #define DEFINE_IMMEDIATE_5_DECODER_MEM_THUMB(NAME, IMMEDIATE, MNEMONIC) \
DEFINE_THUMB_DECODER(NAME, MNEMONIC, \ info->op1.reg = opcode & 0x0007; \ info->memory.baseReg = (opcode >> 3) & 0x0007; \ - info->memory.offset.shifterReg = IMMEDIATE << 2; \ + info->memory.offset.immediate = IMMEDIATE << 2; \ info->operandFormat = ARM_OPERAND_REGISTER_1 | \ ARM_OPERAND_AFFECTED_1 | \ ARM_OPERAND_MEMORY_2; \@@ -333,7 +333,7 @@ static int _decodeRegisterList(int list, char* buffer, int blen) {
if (blen <= 0) { return 0; } - int total = 1; + int total = 0; strncpy(buffer, "{", blen); ADVANCE(1); int i;@@ -381,8 +381,52 @@ return total;
} static int _decodeMemory(struct ARMMemoryAccess memory, char* buffer, int blen) { - // TODO - return 0; + if (blen <= 0) { + return 0; + } + int total = 0; + strncpy(buffer, "[", blen); + ADVANCE(1); + int written; + if (memory.format & ARM_MEMORY_REGISTER_BASE) { + written = _decodeRegister(memory.baseReg, buffer, blen); + ADVANCE(written); + if (memory.format & (ARM_MEMORY_REGISTER_OFFSET | ARM_MEMORY_IMMEDIATE_OFFSET) && !(memory.format & ARM_MEMORY_POST_INCREMENT)) { + strncpy(buffer, ", ", blen); + ADVANCE(2); + } + } + if (memory.format & ARM_MEMORY_POST_INCREMENT) { + strncpy(buffer, "], ", blen); + ADVANCE(3); + } + if (memory.format & ARM_MEMORY_IMMEDIATE_OFFSET) { + if (memory.format & ARM_MEMORY_OFFSET_SUBTRACT) { + written = snprintf(buffer, blen, "#-%i", memory.offset.immediate); + ADVANCE(written); + } else { + written = snprintf(buffer, blen, "#%i", memory.offset.immediate); + ADVANCE(written); + } + } else if (memory.format & ARM_MEMORY_REGISTER_OFFSET) { + if (memory.format & ARM_MEMORY_OFFSET_SUBTRACT) { + strncpy(buffer, "-", blen); + ADVANCE(1); + } + written = _decodeRegister(memory.offset.reg, buffer, blen); + ADVANCE(written); + } + // TODO: shifted registers + + if (!(memory.format & ARM_MEMORY_POST_INCREMENT)) { + strncpy(buffer, "]", blen); + ADVANCE(1); + } + if (memory.format & ARM_MEMORY_PRE_INCREMENT) { + strncpy(buffer, "!", blen); + ADVANCE(1); + } + return total; } static const char* _thumbMnemonicStrings[] = {
M
src/arm/decoder.h
→
src/arm/decoder.h
@@ -43,7 +43,8 @@ ARM_MEMORY_IMMEDIATE_OFFSET = 0x0002,
ARM_MEMORY_REGISTER_OFFSET = 0x0004, ARM_MEMORY_SHIFTED_OFFSET = 0x0008, ARM_MEMORY_PRE_INCREMENT = 0x0010, - ARM_MEMORY_POST_INCREMENT = 0x0020 + ARM_MEMORY_POST_INCREMENT = 0x0020, + ARM_MEMORY_OFFSET_SUBTRACT = 0x0040 }; union ARMOperand {