From 4ee07ea6b0286ed265e06bdbf09550d3b97fe4f0 Mon Sep 17 00:00:00 2001 From: Luciano Ciccariello Date: Sun, 19 May 2024 12:29:04 +0100 Subject: [PATCH] Add support for `seb`/`seh` instructions from mips32r2 (#273) --- m2c/arch_mips.py | 4 ++++ tests/end_to_end/seb-seh/seb-mwcc-out.c | 8 ++++++++ tests/end_to_end/seb-seh/seb-mwcc.s | 19 +++++++++++++++++++ tests/end_to_end/seb-seh/seh-mwcc-out.c | 8 ++++++++ tests/end_to_end/seb-seh/seh-mwcc.s | 19 +++++++++++++++++++ 5 files changed, 58 insertions(+) create mode 100644 tests/end_to_end/seb-seh/seb-mwcc-out.c create mode 100644 tests/end_to_end/seb-seh/seb-mwcc.s create mode 100644 tests/end_to_end/seb-seh/seh-mwcc-out.c create mode 100644 tests/end_to_end/seb-seh/seh-mwcc.s diff --git a/m2c/arch_mips.py b/m2c/arch_mips.py index 5f9d00ef..fe4f1c60 100644 --- a/m2c/arch_mips.py +++ b/m2c/arch_mips.py @@ -62,6 +62,7 @@ as_intish, as_s64, as_sintish, + as_type, as_u32, as_u64, as_uintish, @@ -1662,6 +1663,9 @@ def eval_fn(s: NodeState, a: InstrArgs) -> None: # Unaligned loads "lwl": lambda a: handle_lwl(a), "lwr": lambda a: handle_lwr(a), + # Sign extend + "seb": lambda a: as_type(a.reg(1), Type.s8(), silent=False), + "seh": lambda a: as_type(a.reg(1), Type.s16(), silent=False), } @staticmethod diff --git a/tests/end_to_end/seb-seh/seb-mwcc-out.c b/tests/end_to_end/seb-seh/seb-mwcc-out.c new file mode 100644 index 00000000..68221e9a --- /dev/null +++ b/tests/end_to_end/seb-seh/seb-mwcc-out.c @@ -0,0 +1,8 @@ +s8 foo(s8); /* extern */ + +s8 test(s32 arg0) { + s32 sp10; + + sp10 = arg0; + return (s8) (foo((s8) sp10) + 1); +} diff --git a/tests/end_to_end/seb-seh/seb-mwcc.s b/tests/end_to_end/seb-seh/seb-mwcc.s new file mode 100644 index 00000000..db025250 --- /dev/null +++ b/tests/end_to_end/seb-seh/seb-mwcc.s @@ -0,0 +1,19 @@ +glabel test +addiu sp,sp,-0x20 +sw ra,0xc(sp) +sw s0,8(sp) +sw a0,0x10(sp) +lw v0,0x10(sp) +seb a0,v0 +jal foo +nop +seb v0,v0 +addiu v0,v0,1 +seb v0,v0 +seb s0,v0 +move v0,s0 +lw ra,0xc(sp) +lw s0,8(sp) +addiu sp,sp,0x20 +jr ra +nop diff --git a/tests/end_to_end/seb-seh/seh-mwcc-out.c b/tests/end_to_end/seb-seh/seh-mwcc-out.c new file mode 100644 index 00000000..e9ac06a0 --- /dev/null +++ b/tests/end_to_end/seb-seh/seh-mwcc-out.c @@ -0,0 +1,8 @@ +s16 foo(s16); /* extern */ + +s16 test(s32 arg0) { + s32 sp10; + + sp10 = arg0; + return (s16) (foo((s16) sp10) + 1); +} diff --git a/tests/end_to_end/seb-seh/seh-mwcc.s b/tests/end_to_end/seb-seh/seh-mwcc.s new file mode 100644 index 00000000..44e3b95d --- /dev/null +++ b/tests/end_to_end/seb-seh/seh-mwcc.s @@ -0,0 +1,19 @@ +glabel test +addiu sp,sp,-0x20 +sw ra,0xc(sp) +sw s0,8(sp) +sw a0,0x10(sp) +lw v0,0x10(sp) +seh a0,v0 +jal foo +nop +seh v0,v0 +addiu v0,v0,1 +seh v0,v0 +seh s0,v0 +move v0,s0 +lw ra,0xc(sp) +lw s0,8(sp) +addiu sp,sp,0x20 +jr ra +nop