From: Kohei Tokunaga <ktokunaga.mail@gmail.com>
To: qemu-devel@nongnu.org
Cc: "Alex Bennée" <alex.bennee@linaro.org>,
"Philippe Mathieu-Daudé" <philmd@linaro.org>,
"Thomas Huth" <thuth@redhat.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Richard Henderson" <richard.henderson@linaro.org>,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Daniel P . Berrangé" <berrange@redhat.com>,
"WANG Xuerui" <git@xen0n.name>,
"Aurelien Jarno" <aurelien@aurel32.net>,
"Huacai Chen" <chenhuacai@kernel.org>,
"Jiaxun Yang" <jiaxun.yang@flygoat.com>,
"Aleksandar Rikalo" <arikalo@gmail.com>,
"Palmer Dabbelt" <palmer@dabbelt.com>,
"Alistair Francis" <Alistair.Francis@wdc.com>,
"Stefan Weil" <sw@weilnetz.de>,
qemu-arm@nongnu.org, qemu-riscv@nongnu.org,
"Stefan Hajnoczi" <stefanha@redhat.com>,
"Pierrick Bouvier" <pierrick.bouvier@linaro.org>,
ktokunaga.mail@gmail.com
Subject: [PATCH v3 17/35] tcg/wasm: Add div/rem instructions
Date: Mon, 1 Sep 2025 20:44:19 +0900 [thread overview]
Message-ID: <32eaa622a5acc240179be619c09319bbb48f3f14.1756724464.git.ktokunaga.mail@gmail.com> (raw)
In-Reply-To: <cover.1756724464.git.ktokunaga.mail@gmail.com>
The div and rem operations are implemented using the corresponding
instructions in Wasm. TCI instructions are also generated in the same way as
the original TCI backend.
Signed-off-by: Kohei Tokunaga <ktokunaga.mail@gmail.com>
---
tcg/wasm.c | 32 +++++++++++++++++
tcg/wasm/tcg-target-opc.h.inc | 4 +++
tcg/wasm/tcg-target.c.inc | 68 +++++++++++++++++++++++++++++++++++
3 files changed, 104 insertions(+)
diff --git a/tcg/wasm.c b/tcg/wasm.c
index 2c8a7b814e..8c8dcb81c7 100644
--- a/tcg/wasm.c
+++ b/tcg/wasm.c
@@ -327,6 +327,38 @@ static uintptr_t tcg_qemu_tb_exec_tci(CPUArchState *env, const void *v_tb_ptr)
ptr = (void *)(regs[r1] + ofs);
*(uint32_t *)ptr = regs[r0];
break;
+ case INDEX_op_divs:
+ tci_args_rrr(insn, &r0, &r1, &r2);
+ regs[r0] = (int64_t)regs[r1] / (int64_t)regs[r2];
+ break;
+ case INDEX_op_divu:
+ tci_args_rrr(insn, &r0, &r1, &r2);
+ regs[r0] = (uint64_t)regs[r1] / (uint64_t)regs[r2];
+ break;
+ case INDEX_op_rems:
+ tci_args_rrr(insn, &r0, &r1, &r2);
+ regs[r0] = (int64_t)regs[r1] % (int64_t)regs[r2];
+ break;
+ case INDEX_op_remu:
+ tci_args_rrr(insn, &r0, &r1, &r2);
+ regs[r0] = (uint64_t)regs[r1] % (uint64_t)regs[r2];
+ break;
+ case INDEX_op_tci_divs32:
+ tci_args_rrr(insn, &r0, &r1, &r2);
+ regs[r0] = (int32_t)regs[r1] / (int32_t)regs[r2];
+ break;
+ case INDEX_op_tci_divu32:
+ tci_args_rrr(insn, &r0, &r1, &r2);
+ regs[r0] = (uint32_t)regs[r1] / (uint32_t)regs[r2];
+ break;
+ case INDEX_op_tci_rems32:
+ tci_args_rrr(insn, &r0, &r1, &r2);
+ regs[r0] = (int32_t)regs[r1] % (int32_t)regs[r2];
+ break;
+ case INDEX_op_tci_remu32:
+ tci_args_rrr(insn, &r0, &r1, &r2);
+ regs[r0] = (uint32_t)regs[r1] % (uint32_t)regs[r2];
+ break;
default:
g_assert_not_reached();
}
diff --git a/tcg/wasm/tcg-target-opc.h.inc b/tcg/wasm/tcg-target-opc.h.inc
index 122b45749a..5ed8c67535 100644
--- a/tcg/wasm/tcg-target-opc.h.inc
+++ b/tcg/wasm/tcg-target-opc.h.inc
@@ -8,3 +8,7 @@ DEF(tci_movi, 1, 0, 1, TCG_OPF_NOT_PRESENT)
DEF(tci_movl, 1, 0, 1, TCG_OPF_NOT_PRESENT)
DEF(tci_setcond32, 1, 2, 1, TCG_OPF_NOT_PRESENT)
DEF(tci_movcond32, 1, 2, 1, TCG_OPF_NOT_PRESENT)
+DEF(tci_divs32, 1, 2, 0, TCG_OPF_NOT_PRESENT)
+DEF(tci_divu32, 1, 2, 0, TCG_OPF_NOT_PRESENT)
+DEF(tci_rems32, 1, 2, 0, TCG_OPF_NOT_PRESENT)
+DEF(tci_remu32, 1, 2, 0, TCG_OPF_NOT_PRESENT)
diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc
index e41b3a0c27..38459a60d6 100644
--- a/tcg/wasm/tcg-target.c.inc
+++ b/tcg/wasm/tcg-target.c.inc
@@ -181,12 +181,20 @@ typedef enum {
OPC_I64_GE_U = 0x5a,
OPC_I32_ADD = 0x6a,
+ OPC_I32_DIV_S = 0x6d,
+ OPC_I32_DIV_U = 0x6e,
+ OPC_I32_REM_S = 0x6f,
+ OPC_I32_REM_U = 0x70,
OPC_I32_SHR_S = 0x75,
OPC_I32_SHR_U = 0x76,
OPC_I64_ADD = 0x7c,
OPC_I64_SUB = 0x7d,
OPC_I64_MUL = 0x7e,
+ OPC_I64_DIV_S = 0x7f,
+ OPC_I64_DIV_U = 0x80,
+ OPC_I64_REM_S = 0x81,
+ OPC_I64_REM_U = 0x82,
OPC_I64_AND = 0x83,
OPC_I64_OR = 0x84,
OPC_I64_XOR = 0x85,
@@ -1070,6 +1078,66 @@ static const TCGOutOpUnary outop_extrh_i64_i32 = {
.out_rr = tgen_extrh_i64_i32,
};
+static void tgen_divs(TCGContext *s, TCGType type,
+ TCGReg a0, TCGReg a1, TCGReg a2)
+{
+ TCGOpcode opc = (type == TCG_TYPE_I32
+ ? INDEX_op_tci_divs32
+ : INDEX_op_divs);
+ tcg_out_op_rrr(s, opc, a0, a1, a2);
+ tcg_wasm_out_o1_i2_type(s, type, OPC_I32_DIV_S, OPC_I64_DIV_S, a0, a1, a2);
+}
+
+static const TCGOutOpBinary outop_divs = {
+ .base.static_constraint = C_O1_I2(r, r, r),
+ .out_rrr = tgen_divs,
+};
+
+static void tgen_divu(TCGContext *s, TCGType type,
+ TCGReg a0, TCGReg a1, TCGReg a2)
+{
+ TCGOpcode opc = (type == TCG_TYPE_I32
+ ? INDEX_op_tci_divu32
+ : INDEX_op_divu);
+ tcg_out_op_rrr(s, opc, a0, a1, a2);
+ tcg_wasm_out_o1_i2_type(s, type, OPC_I32_DIV_U, OPC_I64_DIV_U, a0, a1, a2);
+}
+
+static const TCGOutOpBinary outop_divu = {
+ .base.static_constraint = C_O1_I2(r, r, r),
+ .out_rrr = tgen_divu,
+};
+
+static void tgen_rems(TCGContext *s, TCGType type,
+ TCGReg a0, TCGReg a1, TCGReg a2)
+{
+ TCGOpcode opc = (type == TCG_TYPE_I32
+ ? INDEX_op_tci_rems32
+ : INDEX_op_rems);
+ tcg_out_op_rrr(s, opc, a0, a1, a2);
+ tcg_wasm_out_o1_i2_type(s, type, OPC_I32_REM_S, OPC_I64_REM_S, a0, a1, a2);
+}
+
+static const TCGOutOpBinary outop_rems = {
+ .base.static_constraint = C_O1_I2(r, r, r),
+ .out_rrr = tgen_rems,
+};
+
+static void tgen_remu(TCGContext *s, TCGType type,
+ TCGReg a0, TCGReg a1, TCGReg a2)
+{
+ TCGOpcode opc = (type == TCG_TYPE_I32
+ ? INDEX_op_tci_remu32
+ : INDEX_op_remu);
+ tcg_out_op_rrr(s, opc, a0, a1, a2);
+ tcg_wasm_out_o1_i2_type(s, type, OPC_I32_REM_U, OPC_I64_REM_U, a0, a1, a2);
+}
+
+static const TCGOutOpBinary outop_remu = {
+ .base.static_constraint = C_O1_I2(r, r, r),
+ .out_rrr = tgen_remu,
+};
+
static void tcg_out_tb_start(TCGContext *s)
{
init_sub_buf();
--
2.43.0
next prev parent reply other threads:[~2025-09-01 11:55 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-01 11:44 [PATCH v3 00/35] wasm: Add Wasm TCG backend based on wasm64 Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 01/35] meson: Add wasm64 support to the --cpu flag Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 02/35] configure: Enable to propagate -sMEMORY64 flag to Emscripten Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 03/35] dockerfiles: Add support for wasm64 to the wasm Dockerfile Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 04/35] .gitlab-ci.d: Add build tests for wasm64 Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 05/35] tcg/wasm: Add tcg-target.h and tcg-target-reg-bits.h Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 06/35] tcg/wasm: Add register-related definitions Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 07/35] tcg/wasm: Add constraint definitions Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 08/35] tcg/wasm: Add relocation callbacks Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 09/35] tcg/wasm: Add and/or/xor instructions Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 10/35] tcg/wasm: Add add/sub/mul instructions Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 11/35] tcg/wasm: Add shl/shr/sar instructions Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 12/35] tcg/wasm: Add setcond/negsetcond/movcond instructions Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 13/35] tcg/wasm: Add sextract instruction Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 14/35] tcg/wasm: Add load and store instructions Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 15/35] tcg/wasm: Add mov/movi instructions Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 16/35] tcg/wasm: Add ext instructions Kohei Tokunaga
2025-09-01 11:44 ` Kohei Tokunaga [this message]
2025-09-01 11:44 ` [PATCH v3 18/35] tcg/wasm: Add neg/ctpop instructions Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 19/35] tcg/wasm: Add rot/clz/ctz instructions Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 20/35] tcg/wasm: Add br/brcond instructions Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 21/35] tcg/wasm: Add exit_tb/goto_tb/goto_ptr instructions Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 22/35] tcg/wasm: Add call instruction Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 23/35] tcg/wasm: Add qemu_ld/qemu_st instructions Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 24/35] tcg/wasm: Add mb instruction Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 25/35] tcg/wasm: Mark unimplemented instructions Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 26/35] tcg/wasm: Add initialization of fundamental registers Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 27/35] tcg/wasm: Write wasm binary to TB Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 28/35] tcg/wasm: Implement instantiation of Wasm binary Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 29/35] tcg/wasm: Allow switching coroutine from a helper Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 30/35] tcg/wasm: Enable instantiation of TBs executed many times Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 31/35] tcg/wasm: Enable TLB lookup Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 32/35] tcg/wasm: Add tcg_target_init function Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 33/35] meson.build: enable to build Wasm backend Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 34/35] meson.build: Propagate optimization flag for linking on Emscripten Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 35/35] .gitlab-ci.d: build wasm backend in CI Kohei Tokunaga
2025-09-29 22:20 ` [PATCH v3 00/35] wasm: Add Wasm TCG backend based on wasm64 Pierrick Bouvier
2025-09-30 8:26 ` Kohei Tokunaga
2025-10-01 23:08 ` Pierrick Bouvier
2025-10-02 1:33 ` Pierrick Bouvier
2025-10-02 15:59 ` Kohei Tokunaga
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=32eaa622a5acc240179be619c09319bbb48f3f14.1756724464.git.ktokunaga.mail@gmail.com \
--to=ktokunaga.mail@gmail.com \
--cc=Alistair.Francis@wdc.com \
--cc=alex.bennee@linaro.org \
--cc=arikalo@gmail.com \
--cc=aurelien@aurel32.net \
--cc=berrange@redhat.com \
--cc=chenhuacai@kernel.org \
--cc=git@xen0n.name \
--cc=jiaxun.yang@flygoat.com \
--cc=marcandre.lureau@redhat.com \
--cc=palmer@dabbelt.com \
--cc=pbonzini@redhat.com \
--cc=philmd@linaro.org \
--cc=pierrick.bouvier@linaro.org \
--cc=qemu-arm@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=qemu-riscv@nongnu.org \
--cc=richard.henderson@linaro.org \
--cc=stefanha@redhat.com \
--cc=sw@weilnetz.de \
--cc=thuth@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).