diff --git a/arch/M68K/M68KDisassembler.c b/arch/M68K/M68KDisassembler.c index 90d7ec76cd..1a2e46959d 100644 --- a/arch/M68K/M68KDisassembler.c +++ b/arch/M68K/M68KDisassembler.c @@ -283,14 +283,29 @@ static int make_int_16(int value) return (value & 0x8000) ? value | ~0xffff : value & 0xffff; } -static void get_with_index_address_mode(m68k_info *info, cs_m68k_op *op, +/* M68000PM/AD Rev. 1, 2.2.11-2.2.15: the PC of a PC-relative EA is the + * address of its first extension word. Call before reading that word. */ +static uint8_t pc_ext_offset(const m68k_info *info) +{ + return (uint8_t)(info->pc - (uint32_t)info->baseAddress); +} + +/* M68000PM/AD Rev. 1, Fig. 2-2 and Tables 2-1/2-2: bit 3, BD SIZE 00, + * I/IS 100, and I/IS 1xx with the index suppressed are reserved. */ +static bool full_ext_is_reserved(uint32_t extension) +{ + uint32_t iis = extension & 0x7; + + return (extension & 0x8) || (extension & 0x30) == 0x0 || iis == 0x4 || + (!EXT_INDEX_REGISTER_PRESENT(extension) && iis > 0x4); +} + +static bool get_with_index_address_mode(m68k_info *info, cs_m68k_op *op, uint32_t instruction, uint32_t size, bool is_pc) { - uint32_t ext_addr = info->pc; + uint8_t pc_offset = pc_ext_offset(info); uint32_t extension = read_imm_16(info); - int32_t pc_adjust = - is_pc ? (int32_t)(ext_addr - info->baseAddress - 2) : 0; op->address_mode = M68K_AM_AREGI_INDEX_BASE_DISP; @@ -298,6 +313,9 @@ static void get_with_index_address_mode(m68k_info *info, cs_m68k_op *op, uint32_t preindex; uint32_t postindex; + if (full_ext_is_reserved(extension)) + return false; + op->mem.base_reg = M68K_REG_INVALID; op->mem.index_reg = M68K_REG_INVALID; @@ -307,7 +325,6 @@ static void get_with_index_address_mode(m68k_info *info, cs_m68k_op *op, read_imm_32(info) : (int16_t)read_imm_16(info)) : 0; - op->mem.in_disp += pc_adjust; op->mem.in_disp_size = EXT_BASE_DISPLACEMENT_PRESENT(extension) && @@ -328,9 +345,11 @@ static void get_with_index_address_mode(m68k_info *info, cs_m68k_op *op, 1 : 0; + /* A suppressed PC (ZPC) is zero and leaves pc_offset 0. */ if (EXT_BASE_REGISTER_PRESENT(extension)) { if (is_pc) { op->mem.base_reg = M68K_REG_PC; + op->mem.pc_offset = pc_offset; } else { op->mem.base_reg = M68K_REG_A0 + (instruction & 7); @@ -370,7 +389,7 @@ static void get_with_index_address_mode(m68k_info *info, cs_m68k_op *op, M68K_AM_AREGI_INDEX_BASE_DISP; } - return; + return true; } op->mem.index_reg = @@ -380,6 +399,7 @@ static void get_with_index_address_mode(m68k_info *info, cs_m68k_op *op, if (is_pc) { op->mem.base_reg = M68K_REG_PC; + op->mem.pc_offset = pc_offset; op->address_mode = M68K_AM_PCI_INDEX_8_BIT_DISP; } else { op->mem.base_reg = M68K_REG_A0 + (instruction & 7); @@ -387,12 +407,13 @@ static void get_with_index_address_mode(m68k_info *info, cs_m68k_op *op, } op->mem.disp = (int8_t)(extension & 0xff); - op->mem.disp += (int16_t)pc_adjust; op->mem.disp_size = 0; if (EXT_INDEX_SCALE(extension)) { op->mem.scale = 1 << EXT_INDEX_SCALE(extension); } + + return true; } /* Raw effective-address encoding bits, used before get_ea_mode_op() consumes @@ -480,6 +501,14 @@ static bool m68k_ea_is_alterable_control(uint32_t ir) m68k_ea_register(ir) <= M68K_EA_EXT_ABSOLUTE_LONG); } +static bool m68k_ea_is_data_alterable(uint32_t ir) +{ + uint32_t mode = m68k_ea_mode(ir); + return mode != M68K_EA_MODE_ADDR_DIRECT && + (mode != M68K_EA_MODE_EXTENDED || + m68k_ea_register(ir) <= M68K_EA_EXT_ABSOLUTE_LONG); +} + static bool m68k_ea_is_data_register_direct_or_immediate(uint32_t ir) { return m68k_ea_is_data_register_direct(ir) || m68k_ea_is_immediate(ir); @@ -585,7 +614,9 @@ static bool get_ea_mode_op(m68k_info *info, cs_m68k_op *op, case 0x36: case 0x37: /* address register indirect with index */ - get_with_index_address_mode(info, op, instruction, size, false); + if (!get_with_index_address_mode(info, op, instruction, size, + false)) + return false; break; case 0x38: @@ -600,26 +631,19 @@ static bool get_ea_mode_op(m68k_info *info, cs_m68k_op *op, op->mem.address = read_imm_32(info); break; - case 0x3a: { + case 0x3a: /* program counter with displacement */ - /* The printer computes the effective address as - * instruction_start + 2 + disp, assuming the displacement - * extension word immediately follows the opcode word. - * When extra words precede the EA (e.g. an immediate in - * BTST #imm,disp(PC)), the displacement word is further - * along. Adjust disp so the printer still produces the - * correct absolute address. */ - uint32_t disp_addr = info->pc; op->address_mode = M68K_AM_PCI_DISP; + op->mem.pc_offset = pc_ext_offset(info); op->mem.disp = (int16_t)read_imm_16(info); - op->mem.disp += (int16_t)(disp_addr - info->baseAddress - 2); op->mem.disp_size = 1; break; - } case 0x3b: /* program counter with index */ - get_with_index_address_mode(info, op, instruction, size, true); + if (!get_with_index_address_mode(info, op, instruction, size, + true)) + return false; break; case 0x3c: @@ -3016,9 +3040,24 @@ static void d68020_cpgen(m68k_info *info) return; } + /* An is not a data addressing mode, so it can be neither an FPU source + * nor an FMOVE destination, which must also be alterable. Dn holds only + * byte, word, long and single operands (M68000PM/AD Rev. 1, FMOVE and + * the arithmetic instructions). */ + if (ea_operand && m68k_ea_mode(info->ir) == M68K_EA_MODE_ADDR_DIRECT) { + invalid_insn(info); + return; + } + if (ea_operand && m68k_ea_is_data_register_direct(info->ir) && + src != M68K_FPSRC_LONG && src != M68K_FPSRC_SINGLE && + src != M68K_FPSRC_WORD && src != M68K_FPSRC_BYTE) { + invalid_insn(info); + return; + } + if (to_ea) { m68k_insn opcode = M68K_INS_FMOVE; - if (m68k_ea_is_immediate(info->ir)) { + if (!m68k_ea_is_data_alterable(info->ir)) { invalid_insn(info); return; } @@ -3067,7 +3106,7 @@ static void d68020_cpgen(m68k_info *info) MCInst_setOpcode(info->inst, M68K_INS_FETOXM1); break; case 0x09: - MCInst_setOpcode(info->inst, M68K_INS_FATANH); + MCInst_setOpcode(info->inst, M68K_INS_FTANH); break; case 0x0a: MCInst_setOpcode(info->inst, M68K_INS_FATAN); @@ -3364,6 +3403,14 @@ static void d68020_cpgen(m68k_info *info) op0->reg = M68K_REG_FP0 + src; } + /* FTST. and FTST.X FPm only test the source. The destination + * register field is ignored and never written (M68000PM/AD Rev. 1, + * FTST). */ + if (MCInst_getOpcode(info->inst) == M68K_INS_FTST) { + ext->op_count = 1; + return; + } + if (MCInst_getOpcode(info->inst) == M68K_INS_FSINCOS) { if (!ea_operand) { ext->op_size.type = M68K_SIZE_TYPE_FPU; @@ -5420,6 +5467,10 @@ static void update_am_reg_list(m68k_info *info, cs_m68k_op *op, int write) add_reg_to_rw_list(info, op->mem.base_reg, 0); break; + case M68K_AM_PCI_DISP: + add_reg_to_rw_list(info, M68K_REG_PC, 0); + break; + case M68K_AM_AREGI_INDEX_8_BIT_DISP: case M68K_AM_AREGI_INDEX_BASE_DISP: case M68K_AM_MEMI_POST_INDEX: @@ -5508,7 +5559,8 @@ static void build_regs_read_write_counts(m68k_info *info) return; } - if (MCInst_getOpcode(info->inst) == M68K_INS_BTST) { + if (MCInst_getOpcode(info->inst) == M68K_INS_BTST || + MCInst_getOpcode(info->inst) == M68K_INS_FTST) { for (i = 0; i < info->extension.op_count; ++i) update_op_reg_list(info, &info->extension.operands[i], 0); diff --git a/arch/M68K/M68KInstPrinter.c b/arch/M68K/M68KInstPrinter.c index 8e8cafc1c7..646d57bbc1 100644 --- a/arch/M68K/M68KInstPrinter.c +++ b/arch/M68K/M68KInstPrinter.c @@ -384,10 +384,19 @@ static void printImmediate(SStream *O, const cs_m68k *inst, SStream_concat(O, "#$%" PRIx64, op->imm); } -static void printIndex8BitDisp(SStream *O, uint32_t pc, const cs_m68k_op *op) +/* The detail holds encoded displacements. PC-relative EAs are printed as + * absolute targets: their PC is the address of the first extension word, and + * a suppressed PC (ZPC) is zero (M68000PM/AD Rev. 1, 2.2.11-2.2.15). */ +static uint32_t pcRelativeBase(const m68k_info *info, const cs_m68k_op *op) +{ + return op->mem.pc_offset ? info->pc + op->mem.pc_offset : 0; +} + +static void printIndex8BitDisp(SStream *O, uint32_t pc_base, + const cs_m68k_op *op) { if (op->address_mode == M68K_AM_PCI_INDEX_8_BIT_DISP) { - SStream_concat(O, "$%" PRIx32 "(pc,%s", pc + 2 + op->mem.disp, + SStream_concat(O, "$%" PRIx32 "(pc,%s", pc_base + op->mem.disp, s_spacing); } else { SStream_concat(O, "%s$%" PRIx16 "(%s,%s", @@ -399,7 +408,7 @@ static void printIndex8BitDisp(SStream *O, uint32_t pc, const cs_m68k_op *op) SStream_concat0(O, ")"); } -static void printRegAddrMode(SStream *O, uint32_t pc, const cs_m68k_op *op) +static void printRegAddrMode(SStream *O, uint32_t pc_base, const cs_m68k_op *op) { m68k_reg base_reg = op->type == M68K_OP_MEM ? op->mem.base_reg : op->reg; @@ -426,19 +435,19 @@ static void printRegAddrMode(SStream *O, uint32_t pc, const cs_m68k_op *op) (base_reg - M68K_REG_A0)); break; case M68K_AM_PCI_DISP: - SStream_concat(O, "$%" PRIx32 "(pc)", pc + 2 + op->mem.disp); + SStream_concat(O, "$%" PRIx32 "(pc)", pc_base + op->mem.disp); break; default: break; } } -static void printBaseDisp(SStream *O, uint32_t pc, const cs_m68k_op *op) +static void printBaseDisp(SStream *O, uint32_t pc_base, const cs_m68k_op *op) { int is_pc = (op->address_mode == M68K_AM_PCI_INDEX_BASE_DISP); if (is_pc) { - SStream_concat(O, "$%" PRIx32, pc + 2 + op->mem.in_disp); + SStream_concat(O, "$%" PRIx32, pc_base + op->mem.in_disp); } else if (op->mem.in_disp != 0) { SStream_concat(O, "%s$%" PRIx32, op->mem.in_disp >= 0 ? "" : "-", @@ -447,11 +456,11 @@ static void printBaseDisp(SStream *O, uint32_t pc, const cs_m68k_op *op) SStream_concat0(O, "("); - if (is_pc) { - SStream_concat0(O, "pc"); - } else if (op->mem.base_reg != M68K_REG_INVALID) { - SStream_concat(O, "a%" PRId32, op->mem.base_reg - M68K_REG_A0); - } + /* pc or an; zpc for a suppressed PC, nothing for a suppressed an */ + if (op->mem.base_reg != M68K_REG_INVALID) + SStream_concat0(O, getRegName(op->mem.base_reg)); + else if (is_pc) + SStream_concat0(O, "zpc"); if ((is_pc || op->mem.base_reg != M68K_REG_INVALID) && op->mem.index_reg != M68K_REG_INVALID) @@ -465,7 +474,7 @@ static void printBaseDisp(SStream *O, uint32_t pc, const cs_m68k_op *op) SStream_concat0(O, ")"); } -static void printMemIndirect(SStream *O, uint32_t pc, const cs_m68k_op *op) +static void printMemIndirect(SStream *O, uint32_t pc_base, const cs_m68k_op *op) { int is_pc = (op->address_mode == M68K_AM_PC_MEMI_POST_INDEX || op->address_mode == M68K_AM_PC_MEMI_PRE_INDEX); @@ -477,7 +486,7 @@ static void printMemIndirect(SStream *O, uint32_t pc, const cs_m68k_op *op) SStream_concat0(O, "(["); if (is_pc) { - SStream_concat(O, "$%" PRIx32, pc + 2 + op->mem.in_disp); + SStream_concat(O, "$%" PRIx32, pc_base + op->mem.in_disp); } else if (op->mem.in_disp != 0) { SStream_concat(O, "%s$%" PRIx32, op->mem.in_disp >= 0 ? "" : "-", @@ -485,18 +494,23 @@ static void printMemIndirect(SStream *O, uint32_t pc, const cs_m68k_op *op) } if (op->mem.base_reg != M68K_REG_INVALID) { - if (op->mem.in_disp != 0) + if (is_pc || op->mem.in_disp != 0) SStream_concat(O, ",%s%s", s_spacing, getRegName(op->mem.base_reg)); else SStream_concat(O, "%s", getRegName(op->mem.base_reg)); + } else if (is_pc) { + SStream_concat(O, ",%szpc", s_spacing); } if (is_post) SStream_concat0(O, "]"); if (op->mem.index_reg != M68K_REG_INVALID) { - SStream_concat(O, ",%s", s_spacing); + /* bd and the base register can both be suppressed */ + if (is_post || is_pc || op->mem.in_disp != 0 || + op->mem.base_reg != M68K_REG_INVALID) + SStream_concat(O, ",%s", s_spacing); printIndexReg(O, op); } @@ -514,8 +528,8 @@ static void printMemIndirect(SStream *O, uint32_t pc, const cs_m68k_op *op) SStream_concat0(O, ")"); } -static void printAddressingMode(SStream *O, uint32_t pc, const cs_m68k *inst, - const cs_m68k_op *op) +static void printAddressingMode(SStream *O, const m68k_info *info, + const cs_m68k *inst, const cs_m68k_op *op) { switch (op->address_mode) { case M68K_AM_NONE: @@ -547,7 +561,7 @@ static void printAddressingMode(SStream *O, uint32_t pc, const cs_m68k *inst, case M68K_AM_REGI_ADDR_PRE_DEC: case M68K_AM_REGI_ADDR_DISP: case M68K_AM_PCI_DISP: - printRegAddrMode(O, pc, op); + printRegAddrMode(O, pcRelativeBase(info, op), op); break; case M68K_AM_ABSOLUTE_DATA_SHORT: SStream_concat(O, "$%" PRIx32 ".w", (uint32_t)op->mem.address); @@ -560,20 +574,20 @@ static void printAddressingMode(SStream *O, uint32_t pc, const cs_m68k *inst, break; case M68K_AM_PCI_INDEX_8_BIT_DISP: case M68K_AM_AREGI_INDEX_8_BIT_DISP: - printIndex8BitDisp(O, pc, op); + printIndex8BitDisp(O, pcRelativeBase(info, op), op); break; case M68K_AM_PCI_INDEX_BASE_DISP: case M68K_AM_AREGI_INDEX_BASE_DISP: - printBaseDisp(O, pc, op); + printBaseDisp(O, pcRelativeBase(info, op), op); break; case M68K_AM_PC_MEMI_POST_INDEX: case M68K_AM_PC_MEMI_PRE_INDEX: case M68K_AM_MEMI_PRE_INDEX: case M68K_AM_MEMI_POST_INDEX: - printMemIndirect(O, pc, op); + printMemIndirect(O, pcRelativeBase(info, op), op); break; case M68K_AM_BRANCH_DISPLACEMENT: - SStream_concat(O, "$%" PRIx32, pc + 2 + op->br_disp.disp); + SStream_concat(O, "$%" PRIx32, info->pc + 2 + op->br_disp.disp); default: break; } @@ -583,11 +597,11 @@ static void printAddressingMode(SStream *O, uint32_t pc, const cs_m68k *inst, SStream_concat0(O, "&"); } -static void printCAS2(SStream *O, uint32_t pc, const cs_m68k *ext) +static void printCAS2(SStream *O, const m68k_info *info, const cs_m68k *ext) { - printAddressingMode(O, pc, ext, &ext->operands[0]); + printAddressingMode(O, info, ext, &ext->operands[0]); SStream_concat0(O, ","); - printAddressingMode(O, pc, ext, &ext->operands[1]); + printAddressingMode(O, info, ext, &ext->operands[1]); SStream_concat0(O, ","); SStream_concat(O, "(%s):(%s)", @@ -595,7 +609,7 @@ static void printCAS2(SStream *O, uint32_t pc, const cs_m68k *ext) s_reg_names[ext->operands[2].reg_pair.reg_1]); } -static void printCacheOp(SStream *O, uint32_t pc, const cs_m68k *ext) +static void printCacheOp(SStream *O, const m68k_info *info, const cs_m68k *ext) { static const char *const cache_names[] = { "nc", "dc", "ic", "bc" }; unsigned int sel = (unsigned int)ext->operands[0].imm; @@ -608,23 +622,23 @@ static void printCacheOp(SStream *O, uint32_t pc, const cs_m68k *ext) for (i = 1; i < ext->op_count; ++i) { SStream_concat(O, ",%s", s_spacing); - printAddressingMode(O, pc, ext, &ext->operands[i]); + printAddressingMode(O, info, ext, &ext->operands[i]); } } -static void printPackedMoveDestination(SStream *O, uint32_t pc, +static void printPackedMoveDestination(SStream *O, const m68k_info *info, const cs_m68k *ext) { const cs_m68k_op *k_factor = &ext->operands[2]; - printAddressingMode(O, pc, ext, &ext->operands[0]); + printAddressingMode(O, info, ext, &ext->operands[0]); SStream_concat(O, ",%s", s_spacing); - printAddressingMode(O, pc, ext, &ext->operands[1]); + printAddressingMode(O, info, ext, &ext->operands[1]); SStream_concat0(O, "{"); if (k_factor->type == M68K_OP_IMM) SStream_concat(O, "#%" PRId64, (int64_t)k_factor->imm); else - printAddressingMode(O, pc, ext, k_factor); + printAddressingMode(O, info, ext, k_factor); SStream_concat0(O, "}"); } @@ -715,12 +729,12 @@ void M68K_printInst(MCInst *MI, SStream *O, void *PrinterInfo) SStream_concat0(O, " "); if (MI->Opcode == M68K_INS_CAS2) { - printCAS2(O, info->pc, ext); + printCAS2(O, info, ext); return; } if (MI->Opcode >= M68K_INS_CINVL && MI->Opcode <= M68K_INS_CPUSHA) { - printCacheOp(O, info->pc, ext); + printCacheOp(O, info, ext); return; } @@ -728,12 +742,12 @@ void M68K_printInst(MCInst *MI, SStream *O, void *PrinterInfo) ext->op_size.type == M68K_SIZE_TYPE_FPU && ext->op_size.fpu_size == M68K_FPU_SIZE_PACKED && ext->op_count == 3) { - printPackedMoveDestination(O, info->pc, ext); + printPackedMoveDestination(O, info, ext); return; } for (i = 0; i < ext->op_count; ++i) { - printAddressingMode(O, info->pc, ext, &ext->operands[i]); + printAddressingMode(O, info, ext, &ext->operands[i]); if ((i + 1) != ext->op_count) SStream_concat(O, ",%s", s_spacing); } diff --git a/bindings/python/capstone/m68k.py b/bindings/python/capstone/m68k.py index 71f216623a..2b9af7a2f1 100644 --- a/bindings/python/capstone/m68k.py +++ b/bindings/python/capstone/m68k.py @@ -22,6 +22,7 @@ class M68KOpMem(ctypes.Structure): ("in_disp_size", ctypes.c_ubyte), ("out_disp_size", ctypes.c_ubyte), ("disp_size", ctypes.c_ubyte), + ("pc_offset", ctypes.c_ubyte), ("address", ctypes.c_uint64), ) diff --git a/bindings/python/cstest_py/src/cstest_py/details.py b/bindings/python/cstest_py/src/cstest_py/details.py index 5e7eba7d48..af232fea06 100644 --- a/bindings/python/cstest_py/src/cstest_py/details.py +++ b/bindings/python/cstest_py/src/cstest_py/details.py @@ -1241,6 +1241,10 @@ def test_expected_m68k(actual: CsInsn, expected: dict) -> bool: return False if not compare_uint8(aop.mem.offset, eop["mem"].get("offset"), "offset"): return False + if not compare_uint8( + aop.mem.pc_offset, eop["mem"].get("pc_offset"), "pc_offset" + ): + return False if not compare_uint64( aop.mem.address, eop["mem"].get("address"), "address" ): diff --git a/cstool/cstool_m68k.c b/cstool/cstool_m68k.c index 7edd14896f..1dd7a0cba5 100644 --- a/cstool/cstool_m68k.c +++ b/cstool/cstool_m68k.c @@ -142,6 +142,9 @@ void print_insn_detail_m68k(csh handle, cs_insn *ins) if (op->mem.disp != 0) printf("\t\t\toperands[%u].mem.disp: 0x%x\n", i, op->mem.disp); + if (op->mem.pc_offset != 0) + printf("\t\t\toperands[%u].mem.pc_offset: %u\n", + i, op->mem.pc_offset); if (op->mem.scale != 0) printf("\t\t\toperands[%u].mem.scale: %d\n", i, op->mem.scale); diff --git a/docs/cs_v6_release_guide.md b/docs/cs_v6_release_guide.md index 524b2188ab..e3e314deb1 100644 --- a/docs/cs_v6_release_guide.md +++ b/docs/cs_v6_release_guide.md @@ -308,6 +308,7 @@ Nonetheless, we hope this additional information is useful to you. - Added lossless detail operands for immediate 96-bit extended (`.x`) and packed-decimal (`.p`) values. Extended operands use `M68K_OP_FP_EXTENDED` with `sign_exp`, `reserved`, and `significand` in `fp_extended`; packed operands use `M68K_OP_FP_PACKED` with `header` and `fraction` in `fp_packed`. - Added `M68K_FPU_SIZE_PACKED` for `.p` operands. Packed register-to-memory moves expose their static immediate or dynamic data-register k-factor as a third detail operand. - MC68851 `PBcc`: Each of the 16 PMMU branch conditions has its own instruction ID and mnemonic. `insn.id` identifies the condition shown in the disassembly. +- PC-relative memory operands (`M68K_AM_PCI_*`, `M68K_AM_PC_MEMI_*`) report the encoded `disp` or `in_disp`, as in v5. The PC they add is `insn.address + mem.pc_offset`, the first extension word of the effective address. `mem.pc_offset` is 0 when the PC is suppressed (`ZPC`), and the PC then contributes zero. The assembly text shows the computed target address and prints a suppressed PC as `zpc`. - Expanded integration tests and refactored invalid assembly edge cases. **UX** @@ -508,6 +509,7 @@ Such an instruction is ill-defined in LLVM and should be fixed upstream. | m68k_op_mem.in_disp, m68k_op_mem.out_disp | These fields are now signed instead of unsigned. | The M68K architecture uses sign extended displacements for effective address calculation. | | m68k_op_mem.disp_size | Defines if the .disp field was encoded as a byte (false) or word (true) | Necessary for accurate printing. | | m68k_op_mem.in_disp_size, m68k_op_mem.out_disp_size | Defines if the .in_disp and .out_disp fields respectively were encoded as words (false) or longs (true) | Necessary for accurate printing. | +| m68k_op_mem.pc_offset | Offset from the instruction address to the PC of a PC-relative mode, its first extension word. 0 if the PC is not used, also for `ZPC`. Uses padding before `address`, so the struct layout is unchanged. | Immediates, register masks or FPU command words can precede the extension word, so the PC is not always `insn.address + 2`. | | `M68K_OP_MEM` storage | Memory operands now store base registers in `m68k_op_mem.base_reg` and absolute addresses in `m68k_op_mem.address`; `op->reg` and `op->imm` are only used for register and immediate operands. | Keeps memory-addressing details in `m68k_op_mem` consistently. | | `M68K_OP_FP_EXTENDED`, `cs_m68k_op.fp_extended` | Immediate `.x` operands use a dedicated type with `sign_exp`, `reserved`, and `significand` fields. | Avoids losing precision or reserved bits by converting detail data to binary64. | | `M68K_OP_FP_PACKED`, `cs_m68k_op.fp_packed` | Immediate `.p` operands use a dedicated type with `header` and `fraction` fields. | Preserves every packed-decimal digit, sign, exponent, and special-value encoding. | diff --git a/include/capstone/m68k.h b/include/capstone/m68k.h index f45f21c2cd..db72f9f2a4 100644 --- a/include/capstone/m68k.h +++ b/include/capstone/m68k.h @@ -174,6 +174,10 @@ typedef struct m68k_op_mem { uint8_t in_disp_size; ///< 0 = word, 1 = long uint8_t out_disp_size; ///< 0 = word, 1 = long uint8_t disp_size; ///< 0 = byte, 1 = word + /// Offset in bytes from the instruction address to the PC used by a + /// PC-relative mode: its first extension word, which can follow other + /// extension words. 0 if the mode does not use the PC, also for ZPC. + uint8_t pc_offset; uint64_t address; ///< absolute address for absolute MEM operands } m68k_op_mem; diff --git a/suite/cstest/include/test_detail_m68k.h b/suite/cstest/include/test_detail_m68k.h index f734391045..ed7bea3233 100644 --- a/suite/cstest/include/test_detail_m68k.h +++ b/suite/cstest/include/test_detail_m68k.h @@ -23,6 +23,7 @@ typedef struct { uint8_t bitfield; uint8_t width; uint8_t offset; + uint8_t pc_offset; uint64_t address; } TestDetailM68KOpMem; @@ -52,6 +53,8 @@ static const cyaml_schema_field_t test_detail_m68k_op_mem_mapping_schema[] = { width), CYAML_FIELD_UINT("offset", CYAML_FLAG_OPTIONAL, TestDetailM68KOpMem, offset), + CYAML_FIELD_UINT("pc_offset", CYAML_FLAG_OPTIONAL, TestDetailM68KOpMem, + pc_offset), CYAML_FIELD_UINT("address", CYAML_FLAG_OPTIONAL, TestDetailM68KOpMem, address), CYAML_FIELD_INT("in_disp_size", diff --git a/suite/cstest/src/test_detail_m68k.c b/suite/cstest/src/test_detail_m68k.c index e81c154830..02f34098fe 100644 --- a/suite/cstest/src/test_detail_m68k.c +++ b/suite/cstest/src/test_detail_m68k.c @@ -29,6 +29,7 @@ TestDetailM68KOpMem *test_detail_m68k_op_mem_clone(TestDetailM68KOpMem *mem) clone->bitfield = mem->bitfield; clone->width = mem->width; clone->offset = mem->offset; + clone->pc_offset = mem->pc_offset; clone->address = mem->address; clone->in_disp_size = mem->in_disp_size; clone->out_disp_size = mem->out_disp_size; @@ -266,6 +267,10 @@ bool test_expected_m68k(csh *handle, cs_m68k *actual, TestDetailM68K *expected) compare_uint8_ret(op->mem.offset, eop->mem->offset, false); } + if (eop->mem->pc_offset) { + compare_uint8_ret(op->mem.pc_offset, + eop->mem->pc_offset, false); + } if (eop->mem->address) { compare_uint64_ret(op->mem.address, eop->mem->address, false); diff --git a/tests/details/m68k.yaml b/tests/details/m68k.yaml index 9adf5697bd..acc567a057 100644 --- a/tests/details/m68k.yaml +++ b/tests/details/m68k.yaml @@ -1095,6 +1095,7 @@ test_cases: operands: - type: M68K_OP_MEM mem: + pc_offset: 0x0 index_reg: d2 index_size: 1 disp: 0 @@ -3197,8 +3198,8 @@ test_cases: # extra words consumed before the displacement word. # Encoding: 083a 00e8 0a54 at address 0x1050 # btst #$e8, d16(PC) where d16=0x0a54, read at PC=0x1054 - # Target: 0x1054 + 0x0a54 = 0x1aa8 - # Printer: pc + 2 + disp = 0x1050 + 2 + disp => disp must be 0xa56 + # Target: 0x1054 + 0x0a54 = 0x1aa8 (M68000PM/AD Rev. 1, 2.2.11: PC is + # the address of the extension word). The detail keeps the encoded d16. - input: bytes: [0x08, 0x3a, 0x00, 0xe8, 0x0a, 0x54] arch: "m68k" @@ -3214,7 +3215,8 @@ test_cases: imm: 0xe8 - type: M68K_OP_MEM mem: - disp: 0xa56 + pc_offset: 0x4 + disp: 0xa54 disp_size: 1 address_mode: M68K_AM_PCI_DISP @@ -3223,8 +3225,8 @@ test_cases: # Encoding: 083b 0038 0039 at address 0x3400 # btst #$38, (d8,PC,d0.w) where ext=0x0039 (brief: d0.w*1, disp=0x39) # Extension read at PC=0x3404 - # Target: 0x3404 + 0x39 = 0x343d - # Printer: pc + 2 + disp = 0x3400 + 2 + disp => disp must be 0x3b + # Target: 0x3404 + 0x39 = 0x343d (M68000PM/AD Rev. 1, 2.2.12). The detail + # keeps the encoded d8. - input: bytes: [0x08, 0x3b, 0x00, 0x38, 0x00, 0x39] arch: "m68k" @@ -3241,10 +3243,11 @@ test_cases: imm: 0x38 - type: M68K_OP_MEM mem: + pc_offset: 0x4 base_reg: pc index_reg: d0 index_size: -1 - disp: 0x3b + disp: 0x39 address_mode: M68K_AM_PCI_INDEX_8_BIT_DISP # P2-7: roxr.b with register count — d1 as shift count register, d3 as dest @@ -5086,7 +5089,8 @@ test_cases: operands: - type: M68K_OP_MEM mem: - disp: 2 + pc_offset: 0x4 + disp_size: 1 address_mode: M68K_AM_PCI_DISP - type: M68K_OP_REG_BITS register_bits: 12582912 @@ -5322,12 +5326,13 @@ test_cases: operands: - type: M68K_OP_MEM mem: - disp: 2 + pc_offset: 0x4 + disp_size: 1 address_mode: M68K_AM_PCI_DISP - type: M68K_OP_REG reg: d1 - regs_read: [d1] - regs_impl_read: [d1] + regs_read: [pc, d1] + regs_impl_read: [pc, d1] - input: bytes: [0xf2, 0x3a, 0xf8, 0x10, 0x00, 0x00] arch: "m68k" @@ -6119,19 +6124,15 @@ test_cases: address: 0x0 expected: insns: - - asm_text: "ftst fp1, fp2" + - asm_text: "ftst fp1" details: m68k: op_size_type: M68K_SIZE_TYPE_CPU operands: - type: M68K_OP_REG reg: fp1 - - type: M68K_OP_REG - reg: fp2 regs_read: [fp1] - regs_write: [fp2] regs_impl_read: [fp1] - regs_impl_write: [fp2] - input: bytes: [0x50, 0xc0] arch: "m68k" @@ -7654,7 +7655,7 @@ test_cases: address: 0x0 expected: insns: - - asm_text: "fatanh fp1, fp2" + - asm_text: "ftanh fp1, fp2" details: m68k: op_size_type: M68K_SIZE_TYPE_CPU @@ -9653,3 +9654,394 @@ test_cases: options: [CS_MODE_M68K_CFV4E] expected: insns: [] + + # PC-relative EAs: the detail holds the encoded displacements, and the + # printed target uses the address of the (first) extension word as PC + # (M68000PM/AD Rev. 1, 2.2.11-2.2.15). A d16 near the top of its range must + # not wrap when extension words precede it. + # btst.b #$5,($7ffe,pc): extension word at 0x1004, 0x1004 + 0x7ffe = 0x9002. + - input: + bytes: [0x08, 0x3a, 0x00, 0x05, 0x7f, 0xfe] + arch: "m68k" + options: [CS_OPT_DETAIL, CS_MODE_BIG_ENDIAN, CS_MODE_M68K_000] + address: 0x1000 + expected: + insns: + - asm_text: "btst.b #$5, $9002(pc)" + details: + m68k: + operands: + - type: M68K_OP_IMM + imm: 0x5 + - type: M68K_OP_MEM + mem: + pc_offset: 0x4 + disp: 0x7ffe + disp_size: 1 + address_mode: M68K_AM_PCI_DISP + # btst.b #$5,(-$8000,pc): 0x10004 - 0x8000 = 0x8004. + - input: + bytes: [0x08, 0x3a, 0x00, 0x05, 0x80, 0x00] + arch: "m68k" + options: [CS_OPT_DETAIL, CS_MODE_BIG_ENDIAN, CS_MODE_M68K_000] + address: 0x10000 + expected: + insns: + - asm_text: "btst.b #$5, $8004(pc)" + details: + m68k: + operands: + - type: M68K_OP_IMM + imm: 0x5 + - type: M68K_OP_MEM + mem: + pc_offset: 0x4 + disp: -0x8000 + disp_size: 1 + address_mode: M68K_AM_PCI_DISP + # cmpi.l #$12345678,($7ffc,pc): two immediate words precede the + # displacement, so PC is 0x2006 and the target is 0xa002. + - input: + bytes: [0x0c, 0xba, 0x12, 0x34, 0x56, 0x78, 0x7f, 0xfc] + arch: "m68k" + options: [CS_OPT_DETAIL, CS_MODE_BIG_ENDIAN, CS_MODE_M68K_020] + address: 0x2000 + expected: + insns: + - asm_text: "cmpi.l #$12345678, $a002(pc)" + details: + m68k: + operands: + - type: M68K_OP_IMM + imm: 0x12345678 + - type: M68K_OP_MEM + mem: + pc_offset: 0x6 + disp: 0x7ffc + disp_size: 1 + address_mode: M68K_AM_PCI_DISP + # movem.l ($7ffe,pc),d0-d1: the register mask precedes the displacement, + # so PC is 0x3004 and the target is 0xb002. + - input: + bytes: [0x4c, 0xfa, 0x00, 0x03, 0x7f, 0xfe] + arch: "m68k" + options: [CS_OPT_DETAIL, CS_MODE_BIG_ENDIAN, CS_MODE_M68K_000] + address: 0x3000 + expected: + insns: + - asm_text: "movem.l $b002(pc), d0-d1" + details: + m68k: + operands: + - type: M68K_OP_MEM + mem: + pc_offset: 0x4 + disp: 0x7ffe + disp_size: 1 + address_mode: M68K_AM_PCI_DISP + - type: M68K_OP_REG_BITS + register_bits: 0x3 + # btst.b #$5,(-$80,pc,d0.w): brief extension word at 0x5004 holds d8 = -0x80, + # so the base is 0x5004 - 0x80 = 0x4f84. + - input: + bytes: [0x08, 0x3b, 0x00, 0x05, 0x00, 0x80] + arch: "m68k" + options: [CS_OPT_DETAIL, CS_MODE_BIG_ENDIAN, CS_MODE_M68K_000] + address: 0x5000 + expected: + insns: + - asm_text: "btst.b #$5, $4f84(pc, d0.w)" + details: + regs_read: [d0, pc] + m68k: + operands: + - type: M68K_OP_IMM + imm: 0x5 + - type: M68K_OP_MEM + mem: + pc_offset: 0x4 + base_reg: pc + index_reg: d0 + index_size: -1 + disp: -0x80 + disp_size: -1 + address_mode: M68K_AM_PCI_INDEX_8_BIT_DISP + # fmove.l ([$1234,pc],d0.w,$10),fp0: the full-format extension word follows + # the FPU command word, so PC is 0x6004 and the intermediate address is + # read from 0x6004 + 0x1234 = 0x7238. in_disp is the encoded bd. + - input: + bytes: [0xf2, 0x3b, 0x40, 0x00, 0x01, 0x26, 0x12, 0x34, 0x00, 0x10] + arch: "m68k" + options: [CS_OPT_DETAIL, CS_MODE_BIG_ENDIAN, CS_MODE_M68K_040] + address: 0x6000 + expected: + insns: + - asm_text: "fmove.l ([$7238, pc], d0.w, $10), fp0" + details: + m68k: + operands: + - type: M68K_OP_MEM + mem: + pc_offset: 0x4 + base_reg: pc + index_reg: d0 + index_size: -1 + in_disp: 0x1234 + in_disp_size: -1 + out_disp: 0x10 + out_disp_size: -1 + address_mode: M68K_AM_PC_MEMI_POST_INDEX + - type: M68K_OP_REG + reg: fp0 + # frestore ([$1234,pc,d0.w],$10): cpRESTORE has no command word, so the + # extension word is at 0x4002 and the intermediate address is 0x5236. + - input: + bytes: [0xf3, 0x7b, 0x01, 0x22, 0x12, 0x34, 0x00, 0x10] + arch: "m68k" + options: [CS_OPT_DETAIL, CS_MODE_BIG_ENDIAN, CS_MODE_M68K_040] + address: 0x4000 + expected: + insns: + - asm_text: "frestore ([$5236, pc, d0.w], $10)" + details: + m68k: + operands: + - type: M68K_OP_MEM + mem: + pc_offset: 0x2 + base_reg: pc + index_reg: d0 + index_size: -1 + in_disp: 0x1234 + in_disp_size: -1 + out_disp: 0x10 + out_disp_size: -1 + address_mode: M68K_AM_PC_MEMI_PRE_INDEX + # frestore ([$1234,zpc,d0.w],$10): with the PC suppressed (BS = 1) the PC + # contributes zero (M68000PM/AD Rev. 1, 2.2.15), so bd is absolute. ZPC is + # printed so the text keeps the program-space reference. + - input: + bytes: [0xf3, 0x7b, 0x01, 0xa2, 0x12, 0x34, 0x00, 0x10] + arch: "m68k" + options: [CS_OPT_DETAIL, CS_MODE_BIG_ENDIAN, CS_MODE_M68K_040] + address: 0x4000 + expected: + insns: + - asm_text: "frestore ([$1234, zpc, d0.w], $10)" + details: + regs_read: [d0] + m68k: + operands: + - type: M68K_OP_MEM + mem: + pc_offset: 0x0 + base_reg: invalid + index_reg: d0 + index_size: -1 + in_disp: 0x1234 + in_disp_size: -1 + out_disp: 0x10 + out_disp_size: -1 + address_mode: M68K_AM_PC_MEMI_PRE_INDEX + # frestore ([pc,d0.w]): a null base displacement still prints the target + # and separates it from the base register. + - input: + bytes: [0xf3, 0x7b, 0x01, 0x11] + arch: "m68k" + options: [CS_OPT_DETAIL, CS_MODE_BIG_ENDIAN, CS_MODE_M68K_040] + address: 0x4000 + expected: + insns: + - asm_text: "frestore ([$4002, pc, d0.w])" + details: + m68k: + operands: + - type: M68K_OP_MEM + mem: + pc_offset: 0x2 + base_reg: pc + index_reg: d0 + index_size: -1 + address_mode: M68K_AM_PC_MEMI_PRE_INDEX + # lea ($1234,zpc,d0.w),a3: EA = bd + d0.w (M68000PM/AD Rev. 1, 2.2.13). + - input: + bytes: [0x47, 0xfb, 0x01, 0xa0, 0x12, 0x34] + arch: "m68k" + options: [CS_OPT_DETAIL, CS_MODE_BIG_ENDIAN, CS_MODE_M68K_020] + address: 0x1000 + expected: + insns: + - asm_text: "lea.l $1234(zpc, d0.w), a3" + details: + regs_read: [d0] + regs_write: [a3] + m68k: + operands: + - type: M68K_OP_MEM + mem: + pc_offset: 0x0 + base_reg: invalid + index_reg: d0 + index_size: -1 + in_disp: 0x1234 + in_disp_size: -1 + address_mode: M68K_AM_PCI_INDEX_BASE_DISP + - type: M68K_OP_REG + reg: a3 + + # M68000PM/AD Rev. 1, FTANH and FATANH: opmode 0001001 is FTANH, opmode + # 0001101 is FATANH. + - input: + bytes: [0xf2, 0x10, 0x44, 0x09, 0xf2, 0x00, 0x05, 0x0d] + arch: "m68k" + options: [CS_OPT_DETAIL, CS_MODE_BIG_ENDIAN, CS_MODE_M68K_040] + expected: + insns: + - asm_text: "ftanh.s (a0), fp0" + details: + m68k: + operands: + - type: M68K_OP_MEM + address_mode: M68K_AM_REGI_ADDR + mem: + base_reg: a0 + - type: M68K_OP_REG + reg: fp0 + regs_read: [a0] + regs_write: [fp0] + - asm_text: "fatanh fp1, fp2" + + # M68000PM/AD Rev. 1, FTST: only the source is an operand. The destination + # register field is ignored and the destination register is not modified. + - input: + bytes: [0xf2, 0x10, 0x40, 0x3a, 0xf2, 0x00, 0x59, 0x3a] + arch: "m68k" + options: [CS_OPT_DETAIL, CS_MODE_BIG_ENDIAN, CS_MODE_M68K_040] + expected: + insns: + - asm_text: "ftst.l (a0)" + details: + m68k: + op_size_cpu: M68K_CPU_SIZE_LONG + operands: + - type: M68K_OP_MEM + address_mode: M68K_AM_REGI_ADDR + mem: + base_reg: a0 + regs_read: [a0] + - asm_text: "ftst.b d0" + details: + m68k: + op_size_cpu: M68K_CPU_SIZE_BYTE + operands: + - type: M68K_OP_REG + reg: d0 + regs_read: [d0] + + # M68000PM/AD Rev. 1, FMOVE and the FPU arithmetic instructions: An is not a + # data addressing mode, so it is neither a valid FPU source nor an FMOVE + # destination. FMOVE destinations must also be alterable, which excludes + # PC-relative modes. + - input: + bytes: [0xf2, 0x09, 0x41, 0x00] + arch: "m68k" + options: [CS_OPT_DETAIL, CS_MODE_BIG_ENDIAN, CS_MODE_M68K_040] + expected: + insns: [] + - input: + bytes: [0xf2, 0x09, 0x41, 0x22] + arch: "m68k" + options: [CS_OPT_DETAIL, CS_MODE_BIG_ENDIAN, CS_MODE_M68K_040] + expected: + insns: [] + - input: + bytes: [0xf2, 0x09, 0x41, 0x00] + arch: "m68k" + options: [CS_OPT_DETAIL, CS_MODE_BIG_ENDIAN, CS_MODE_M68K_CFV4E] + expected: + insns: [] + - input: + bytes: [0xf2, 0x09, 0x61, 0x00] + arch: "m68k" + options: [CS_OPT_DETAIL, CS_MODE_BIG_ENDIAN, CS_MODE_M68K_040] + expected: + insns: [] + - input: + bytes: [0xf2, 0x3a, 0x61, 0x00, 0x00, 0x10] + arch: "m68k" + options: [CS_OPT_DETAIL, CS_MODE_BIG_ENDIAN, CS_MODE_M68K_040] + expected: + insns: [] + + # M68000PM/AD Rev. 1, FMOVE: Dn is a valid FPU operand only for byte, word, + # long and single formats. + - input: + bytes: [0xf2, 0x00, 0x54, 0x00] + arch: "m68k" + options: [CS_OPT_DETAIL, CS_MODE_BIG_ENDIAN, CS_MODE_M68K_040] + expected: + insns: [] + - input: + bytes: [0xf2, 0x00, 0x4c, 0x00] + arch: "m68k" + options: [CS_OPT_DETAIL, CS_MODE_BIG_ENDIAN, CS_MODE_M68K_040] + expected: + insns: [] + - input: + bytes: [0xf2, 0x00, 0x7c, 0x00] + arch: "m68k" + options: [CS_OPT_DETAIL, CS_MODE_BIG_ENDIAN, CS_MODE_M68K_040] + expected: + insns: [] + + # M68000PM/AD Rev. 1, Tables 2-1 and 2-2: full extension words with bit 3 + # set, BD SIZE 00, IS = 0 with I/IS 100, or IS = 1 with I/IS 1xx are + # reserved. + - input: + bytes: [0x41, 0xfb, 0x01, 0x28, 0x00, 0x10] + arch: "m68k" + options: [CS_OPT_DETAIL, CS_MODE_BIG_ENDIAN, CS_MODE_M68K_040] + expected: + insns: [] + - input: + bytes: [0x41, 0xfb, 0x01, 0x00] + arch: "m68k" + options: [CS_OPT_DETAIL, CS_MODE_BIG_ENDIAN, CS_MODE_M68K_040] + expected: + insns: [] + - input: + bytes: [0x41, 0xfb, 0x01, 0x24, 0x00, 0x10, 0x00, 0x00] + arch: "m68k" + options: [CS_OPT_DETAIL, CS_MODE_BIG_ENDIAN, CS_MODE_M68K_040] + expected: + insns: [] + - input: + bytes: [0x41, 0xfb, 0x01, 0x65, 0x00, 0x10, 0x00, 0x00] + arch: "m68k" + options: [CS_OPT_DETAIL, CS_MODE_BIG_ENDIAN, CS_MODE_M68K_040] + expected: + insns: [] + + # lea ($10,pc),a0 reads the PC, like the indexed PC modes. + - input: + bytes: [0x41, 0xfa, 0x00, 0x10] + arch: "m68k" + options: [CS_OPT_DETAIL, CS_MODE_BIG_ENDIAN, CS_MODE_M68K_020] + address: 0x1000 + expected: + insns: + - asm_text: "lea.l $1012(pc), a0" + details: + regs_read: [pc] + regs_write: [a0] + + # Memory indirect with base register and bd suppressed: no separator + # before the index register inside the brackets. + - input: + bytes: [0x4c, 0xf0, 0x03, 0x00, 0x01, 0x91, 0x4c, 0xf0, 0x03, 0x00, 0x01, 0x95] + arch: "m68k" + options: [CS_OPT_DETAIL, CS_MODE_BIG_ENDIAN, CS_MODE_M68K_040] + expected: + insns: + - asm_text: "movem.l ([d0.w]), a0-a1" + - asm_text: "movem.l ([], d0.w), a0-a1"