From 11118be71770cefc3bf971798ac610dd2b4df807 Mon Sep 17 00:00:00 2001 From: Dylan Fei Date: Tue, 23 Dec 2025 15:54:35 +0800 Subject: [PATCH] Fix pointer difference issue in aarch64/riscv64 When calculating the difference between two pointers (TOK_PDIV), the AArch64 and RISC-V64 backends used unsigned division instructions (udiv/divu). This patch moves TOK_PDIV to use signed division instructions (sdiv/div) for both backends, ensuring correct behavior as per the C standard. --- arm64-gen.c | 2 +- riscv64-gen.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/arm64-gen.c b/arm64-gen.c index 01ea3909..2038aeba 100644 --- a/arm64-gen.c +++ b/arm64-gen.c @@ -1756,6 +1756,7 @@ static void arm64_gen_opil(int op, uint32_t l) o(0x4b000000 | l << 31 | x | a << 5 | b << 16); // sub break; case '/': + case TOK_PDIV: o(0x1ac00c00 | l << 31 | x | a << 5 | b << 16); // sdiv break; case '^': @@ -1798,7 +1799,6 @@ static void arm64_gen_opil(int op, uint32_t l) o(0x1ac02400 | l << 31 | x | a << 5 | b << 16); // lsr break; case TOK_UDIV: - case TOK_PDIV: o(0x1ac00800 | l << 31 | x | a << 5 | b << 16); // udiv break; case TOK_UGE: diff --git a/riscv64-gen.c b/riscv64-gen.c index e03d6e42..19c76682 100644 --- a/riscv64-gen.c +++ b/riscv64-gen.c @@ -1110,6 +1110,7 @@ static void gen_opil(int op, int ll) ER(0x33 | ll, 0, d, a, b, 1); // mul d, a, b break; case '/': + case TOK_PDIV: ER(0x33 | ll, 4, d, a, b, 1); // div d, a, b break; case '&': @@ -1127,7 +1128,6 @@ static void gen_opil(int op, int ll) case TOK_UMOD: ER(0x33 | ll, 7, d, a, b, 1); // remu d, a, b break; - case TOK_PDIV: case TOK_UDIV: ER(0x33 | ll, 5, d, a, b, 1); // divu d, a, b break;