/* * libbinrec: a recompiling translator for machine code * Copyright (c) 2016 Andrew Church * * This software may be copied and redistributed under certain conditions; * see the file "COPYING" in the source code distribution for details. * NO WARRANTY is provided with this software. */ #include "src/rtl.h" #include "src/rtl-internal.h" #include "tests/common.h" #include "tests/log-capture.h" int main(void) { binrec_setup_t setup; memset(&setup, 0, sizeof(setup)); setup.log = log_capture; binrec_t *handle; EXPECT(handle = binrec_create_handle(&setup)); RTLUnit *unit; EXPECT(unit = rtl_create_unit(handle)); int reg1, reg2, reg3; EXPECT(reg1 = rtl_alloc_register(unit, RTLTYPE_INT32)); EXPECT(reg2 = rtl_alloc_register(unit, RTLTYPE_INT32)); EXPECT(reg3 = rtl_alloc_register(unit, RTLTYPE_ADDRESS)); int insn; EXPECT(rtl_add_insn(unit, RTLOP_LOAD_IMM, reg1, 0, 0, 10)); EXPECT(rtl_add_insn(unit, RTLOP_LOAD_IMM, reg2, 0, 0, 20)); EXPECT_EQ(insn = rtl_add_chain_insn(unit, reg1, reg2), 2); EXPECT(rtl_add_insn(unit, RTLOP_LOAD_IMM, reg3, 0, 0, 30)); EXPECT(rtl_add_insn(unit, RTLOP_CHAIN_RESOLVE, 0, reg3, 0, insn)); EXPECT_EQ(unit->insns[4].opcode, RTLOP_CHAIN_RESOLVE); EXPECT_EQ(unit->insns[4].src1, reg3); EXPECT_EQ(unit->insns[4].src_imm, insn); EXPECT_EQ(unit->regs[reg3].birth, 3); EXPECT_EQ(unit->regs[reg3].death, 4); EXPECT(unit->have_block); EXPECT_FALSE(unit->error); EXPECT(rtl_finalize_unit(unit)); const char *disassembly = " 0: LOAD_IMM r1, 10\n" " 1: LOAD_IMM r2, 20\n" " 2: CHAIN r1, r2\n" " r1: 10\n" " r2: 20\n" " 3: LOAD_IMM r3, 0x1E\n" " 4: CHAIN_RESOLVE @2, r3\n" " r3: 0x1E\n" "\n" "Block 0: --> [0,4] --> \n" ; EXPECT_STREQ(rtl_disassemble_unit(unit, true), disassembly); EXPECT_STREQ(get_log_messages(), NULL); rtl_destroy_unit(unit); binrec_destroy_handle(handle); return EXIT_SUCCESS; }