wasm: Add Wasm TCG backend based on wasm64
Subsystems (2)
PATCH 4/35 .gitlab-ci.d: Add build tests for wasm64
In reply to Kohei TokunagaThe wasm builds are tested for 3 targets: wasm32, wasm64(-sMEMORY64=1) and wasm64(-sMEMORY64=2). The CI builds the containers using the same Dockerfile (emsdk-wasm-cross.docker) with different build args.
--- .gitlab-ci.d/buildtest.yml | 26 ++++++++++++++++++++++---- .gitlab-ci.d/container-cross.yml | 20 ++++++++++++++++++-- .gitlab-ci.d/container-template.yml | 4 +++- 3 files changed, 43 insertions(+), 7 deletions(-)
V1:
- This commit is under review in another patch series so please ignore it here.
.gitlab-ci.d/buildtest.yml +22 -4
@@ -787,11 +787,29 @@ coverity: # Always manual on forks even if $QEMU_CI == "2" - when: manual -build-wasm: +build-wasm32-32bit: extends: .wasm_build_job_template timeout: 2h needs: - job: wasm-emsdk-cross-container + job: wasm32-32bit-emsdk-cross-container variables: - IMAGE: emsdk-wasm32-cross - CONFIGURE_ARGS: --static --disable-tools --enable-debug --enable-tcg-interpreter + IMAGE: emsdk-wasm32-32bit-cross + CONFIGURE_ARGS: --static --cpu=wasm32 --disable-tools --enable-debug --enable-tcg-interpreter + +build-wasm64-64bit: + extends: .wasm_build_job_template + timeout: 2h + needs: + job: wasm64-64bit-emsdk-cross-container + variables: + IMAGE: emsdk-wasm64-64bit-cross + CONFIGURE_ARGS: --static --cpu=wasm64 --disable-tools --enable-debug --enable-tcg-interpreter + +build-wasm64-32bit: + extends: .wasm_build_job_template + timeout: 2h + needs: + job: wasm64-32bit-emsdk-cross-container + variables: + IMAGE: emsdk-wasm64-32bit-cross + CONFIGURE_ARGS: --static --cpu=wasm64 --wasm64-32bit-address-limit --disable-tools --enable-debug --enable-tcg-interpreter
.gitlab-ci.d/container-cross.yml +18 -2
@@ -92,7 +92,23 @@ win64-fedora-cross-container: variables: NAME: fedora-win64-cross -wasm-emsdk-cross-container: +wasm32-32bit-emsdk-cross-container: extends: .container_job_template variables: - NAME: emsdk-wasm32-cross + NAME: emsdk-wasm32-32bit-cross + BUILD_ARGS: --build-arg TARGET_CPU=wasm32 + DOCKERFILE: emsdk-wasm-cross + +wasm64-64bit-emsdk-cross-container: + extends: .container_job_template + variables: + NAME: emsdk-wasm64-64bit-cross + BUILD_ARGS: --build-arg TARGET_CPU=wasm64 --build-arg WASM64_MEMORY64=1 + DOCKERFILE: emsdk-wasm-cross + +wasm64-32bit-emsdk-cross-container: + extends: .container_job_template + variables: + NAME: emsdk-wasm64-32bit-cross + BUILD_ARGS: --build-arg TARGET_CPU=wasm64 --build-arg WASM64_MEMORY64=2 + DOCKERFILE: emsdk-wasm-cross
.gitlab-ci.d/container-template.yml +3 -1
@@ -10,12 +10,14 @@ - export COMMON_TAG="$CI_REGISTRY/qemu-project/qemu/qemu/$NAME:latest" - docker login $CI_REGISTRY -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" - until docker info; do sleep 1; done + - export DOCKERFILE_NAME=${DOCKERFILE:-$NAME} script: - echo "TAG:$TAG" - echo "COMMON_TAG:$COMMON_TAG" - docker build --tag "$TAG" --cache-from "$TAG" --cache-from "$COMMON_TAG" --build-arg BUILDKIT_INLINE_CACHE=1 - -f "tests/docker/dockerfiles/$NAME.docker" "." + $BUILD_ARGS + -f "tests/docker/dockerfiles/$DOCKERFILE_NAME.docker" "." - docker push "$TAG" after_script: - docker logout
Signature · 1 line
2.43.0
cover wasm: Add Wasm TCG backend based on wasm64
This patch series adds a TCG backend for WebAssembly. Unlike eariler attempts [1], it is implemented using Emscripten's wasm64 target to support 64bit guests.
The first four commits are temporarily imported from a separated patch series which enables 64bit guests using wasm64 [2]. These commits are under review in that series and are included here only to allow subsequent patches to build. Please ignore them when reviewing this series.
# New TCG Backend for Browsers
A new TCG backend translates IR instructions into Wasm instructions and runs them using the browser's WebAssembly APIs (WebAssembly.Module and WebAssembly.instantiate). To minimize compilation overhead and avoid hitting the browser's limitation of the number of instances, this backend integrates a forked TCI. TBs run on TCI by default, with frequently executed TBs compiled into WebAssembly.
# 64bit guests support by wasm64
Support for 64bit guests is being reviewed in a separated patch series [2], which enables QEMU to use 64bit pointers by compiling with the --cpu=wasm64 flag. The Wasm TCG backend is based on this feature and also requires --cpu=wasm64.
QEMU compiled with --cpu=wasm64 runs on wasm64-capable engines. To support engines which don't support wasm64 (e.g. Safari), the Wasm backend can use the compatibility flag "--enable-wasm64-32bit-address-limit" also introduced in [2]. This flag enables 64bit pointers in the C code while Emscripten lowers the output binary to wasm32 and limits the maximum memory size to 4GB. As a result, the Wasm backend can run on wasm32 engiens while supporting 64bit guests.
Note: The flag was originally named --wasm64-32bit-address-limit but this patch series moved it from the configure script into meson_options.txt. To follow Meson's naming conventions, it was renamed to --enable-wasm64-32bit-address-limit.
# Overview of build process
To compile QEMU with Emscripten, the following dependencies are required. The emsdk-wasm-cross.docker environment includes all necessary components and can be used as the build environment:
- Emscripten SDK (emsdk) v4.0.10
- Libraries cross-compiled with Emscripten (please see also emsdk-wasm-cross.docker for build steps)
- GLib v2.84.0
- zlib v1.3.1
- libffi v3.5.2
- Pixman v0.44.2
The configure script supports --cpu=wasm64 flag to compile QEMU with 64bit pointer support.
emconfigure ./configure --cpu=wasm64 \
--static --disable-tools \
--target-list=x86_64-softmmuemmake make -j$(nproc)
If the output needs to run on wasm32 engines, use
"--enable-wasm64-32bit-address-limit" flag.
emconfigure ./configure --cpu=wasm64 --enable-wasm64-32bit-address-limit \
--static --disable-tools \
--target-list=x86_64-softmmuemmake make -j$(nproc)
Either of the above commands generates the following files:
- qemu-system-x86_64.js
- qemu-system-x86_64.wasm
Guest images can be packaged using Emscripten's file_packager.py tool. For example, if the images are stored in a directory named "pack", the following command packages them, allowing QEMU to access them through Emscripten's virtual filesystem:
/path/to/file_packager.py qemu-system-x86_64.data --preload pack > load.js
This process generates the following files:
- qemu-system-x86_64.data
- load.js
Emscripten allows passing arguments to the QEMU command via the Module object in JavaScript:
Module['arguments'] = [
'-nographic', '-m', '512M',
'-L', 'pack/',
'-drive', 'if=virtio,format=raw,file=pack/rootfs.bin',
'-kernel', 'pack/bzImage',
'-append', 'earlyprintk=ttyS0 console=ttyS0 root=/dev/vda loglevel=7',];
The sample repository [3] (tcgdev64 branch) provides a complete setup, including an HTML file that implements a terminal UI.
[1] https://patchew.org/QEMU/[email protected]/
[2] https://patchew.org/QEMU/[email protected]/
[3] https://github.com/ktock/qemu-wasm-sample/tree/tcgdev64
Kohei Tokunaga (35):
meson: Add wasm64 support to the --cpu flag
configure: Enable to propagate -sMEMORY64 flag to Emscripten
dockerfiles: Add support for wasm64 to the wasm Dockerfile .gitlab-ci.d: Add build tests for wasm64
tcg: Fork TCI for wasm backend
tcg/wasm: Do not use TCI disassembler in Wasm backend tcg/wasm: Set TCG_TARGET_REG_BITS to 64
meson: Enable to build wasm backend
tcg/wasm: Set TCG_TARGET_INSN_UNIT_SIZE to 1 tcg/wasm: Add and/or/xor instructions
tcg/wasm: Add add/sub/mul instructions
tcg/wasm: Add shl/shr/sar instructions
tcg/wasm: Add setcond/negsetcond/movcond instructions tcg/wasm: Add deposit/sextract/extract instrcutions tcg/wasm: Add load and store instructions
tcg/wasm: Add mov/movi instructions
tcg/wasm: Add ext instructions
tcg/wasm: Add bswap instructions
tcg/wasm: Add rem/div instructions
tcg/wasm: Add andc/orc/eqv/nand/nor instructions tcg/wasm: Add neg/not/ctpop instructions
tcg/wasm: Add rot/clz/ctz instructions
tcg/wasm: Add br/brcond instructions
tcg/wasm: Add exit_tb/goto_tb/goto_ptr instructions tcg/wasm: Add call instruction
tcg/wasm: Add qemu_ld/qemu_st instructions tcg/wasm: Mark unimplemented instructions as C_NotImplemented tcg/wasm: Add initialization of fundamental registers tcg/wasm: Write wasm binary to TB
tcg/wasm: Implement instantiation of Wasm binary tcg/wasm: Allow switching coroutine from a helper tcg/wasm: Enable instantiation of TBs executed many times tcg/wasm: Enable TLB lookup
meson: Propagate optimization flag for linking on Emscripten .gitlab-ci.d: build wasm backend in CI
.gitlab-ci.d/buildtest.yml | 26 +-
.gitlab-ci.d/container-cross.yml | 20 +-
.gitlab-ci.d/container-template.yml | 4 +-
MAINTAINERS | 9 +-
configure | 14 +-
include/accel/tcg/getpc.h | 2 +-
include/tcg/helper-info.h | 4 +-
include/tcg/tcg.h | 2 +-
meson.build | 14 +-
meson_options.txt | 3 +
scripts/meson-buildoptions.sh | 5 +
tcg/aarch64/tcg-target.c.inc | 11 +
tcg/arm/tcg-target.c.inc | 11 +
tcg/i386/tcg-target.c.inc | 11 +
tcg/loongarch64/tcg-target.c.inc | 11 +
tcg/meson.build | 5 +
tcg/mips/tcg-target.c.inc | 11 +
tcg/ppc/tcg-target.c.inc | 11 +
tcg/region.c | 10 +-
tcg/riscv/tcg-target.c.inc | 11 +
tcg/s390x/tcg-target.c.inc | 11 +
tcg/sparc64/tcg-target.c.inc | 11 +
tcg/tcg.c | 23 +-
tcg/tci/tcg-target.c.inc | 11 +
tcg/wasm.c | 1112 ++++++
tcg/wasm.h | 119 +
tcg/wasm/tcg-target-con-set.h | 21 +
tcg/wasm/tcg-target-con-str.h | 11 +
tcg/wasm/tcg-target-has.h | 22 +
tcg/wasm/tcg-target-mo.h | 17 +
tcg/wasm/tcg-target-opc.h.inc | 15 +
tcg/wasm/tcg-target-reg-bits.h | 15 +
tcg/wasm/tcg-target.c.inc | 3167 +++++++++++++++++
tcg/wasm/tcg-target.h | 77 +
...2-cross.docker => emsdk-wasm-cross.docker} | 29 +-
35 files changed, 4822 insertions(+), 34 deletions(-)
create mode 100644 tcg/wasm.c
create mode 100644 tcg/wasm.h
create mode 100644 tcg/wasm/tcg-target-con-set.h
create mode 100644 tcg/wasm/tcg-target-con-str.h
create mode 100644 tcg/wasm/tcg-target-has.h
create mode 100644 tcg/wasm/tcg-target-mo.h
create mode 100644 tcg/wasm/tcg-target-opc.h.inc
create mode 100644 tcg/wasm/tcg-target-reg-bits.h
create mode 100644 tcg/wasm/tcg-target.c.inc
create mode 100644 tcg/wasm/tcg-target.h
rename tests/docker/dockerfiles/{emsdk-wasm32-cross.docker => emsdk-wasm-cross.docker} (85%)Signature · 1 line
2.43.0
PATCH 9/35 tcg/wasm: Set TCG_TARGET_INSN_UNIT_SIZE to 1
WebAssembly instructions vary in size, including single-byte instructions. This commit sets TCG_TARGET_INSN_UNIT_SIZE to 1 and updates the TCI fork to use "tcg_insn_unit_tci" (a uint32_t) for 4-byte operations.
--- tcg/wasm/tcg-target.c.inc | 38 +++++++++++++++++++++----------------- tcg/wasm/tcg-target.h | 2 +- 2 files changed, 22 insertions(+), 18 deletions(-)
tcg/wasm/tcg-target.c.inc +21 -17
@@ -33,6 +33,8 @@ #define TCG_TARGET_CALL_ARG_I128 TCG_CALL_ARG_NORMAL #define TCG_TARGET_CALL_RET_I128 TCG_CALL_RET_NORMAL +typedef uint32_t tcg_insn_unit_tci; + static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op, TCGType type, unsigned flags) {
@@ -90,16 +92,18 @@ static const char *const tcg_target_reg_names[TCG_TARGET_NB_REGS] = { }; #endif -static bool patch_reloc(tcg_insn_unit *code_ptr, int type, +static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, intptr_t value, intptr_t addend) { + tcg_insn_unit_tci *code_ptr = (tcg_insn_unit_tci *)code_ptr_i; intptr_t diff = value - (intptr_t)(code_ptr + 1); tcg_debug_assert(addend == 0); tcg_debug_assert(type == 20); if (diff == sextract32(diff, 0, type)) { - tcg_patch32(code_ptr, deposit32(*code_ptr, 32 - type, type, diff)); + tcg_patch32((tcg_insn_unit *)code_ptr, + deposit32(*code_ptr, 32 - type, type, diff)); return true; } return false;
@@ -116,7 +120,7 @@ static void stack_bounds_check(TCGReg base, intptr_t offset) static void tcg_out_op_l(TCGContext *s, TCGOpcode op, TCGLabel *l0) { - tcg_insn_unit insn = 0; + tcg_insn_unit_tci insn = 0; tcg_out_reloc(s, s->code_ptr, 20, l0, 0); insn = deposit32(insn, 0, 8, op);
@@ -125,14 +129,14 @@ static void tcg_out_op_l(TCGContext *s, TCGOpcode op, TCGLabel *l0) static void tcg_out_op_p(TCGContext *s, TCGOpcode op, void *p0) { - tcg_insn_unit insn = 0; + tcg_insn_unit_tci insn = 0; intptr_t diff; /* Special case for exit_tb: map null -> 0. */ if (p0 == NULL) { diff = 0; } else { - diff = p0 - (void *)(s->code_ptr + 1); + diff = p0 - (void *)(s->code_ptr + 4); tcg_debug_assert(diff != 0); if (diff != sextract32(diff, 0, 20)) { tcg_raise_tb_overflow(s);
@@ -145,7 +149,7 @@ static void tcg_out_op_p(TCGContext *s, TCGOpcode op, void *p0) static void tcg_out_op_r(TCGContext *s, TCGOpcode op, TCGReg r0) { - tcg_insn_unit insn = 0; + tcg_insn_unit_tci insn = 0; insn = deposit32(insn, 0, 8, op); insn = deposit32(insn, 8, 4, r0);
@@ -159,7 +163,7 @@ static void tcg_out_op_v(TCGContext *s, TCGOpcode op) static void tcg_out_op_ri(TCGContext *s, TCGOpcode op, TCGReg r0, int32_t i1) { - tcg_insn_unit insn = 0; + tcg_insn_unit_tci insn = 0; tcg_debug_assert(i1 == sextract32(i1, 0, 20)); insn = deposit32(insn, 0, 8, op);
@@ -170,7 +174,7 @@ static void tcg_out_op_ri(TCGContext *s, TCGOpcode op, TCGReg r0, int32_t i1) static void tcg_out_op_rl(TCGContext *s, TCGOpcode op, TCGReg r0, TCGLabel *l1) { - tcg_insn_unit insn = 0; + tcg_insn_unit_tci insn = 0; tcg_out_reloc(s, s->code_ptr, 20, l1, 0); insn = deposit32(insn, 0, 8, op);
@@ -180,7 +184,7 @@ static void tcg_out_op_rl(TCGContext *s, TCGOpcode op, TCGReg r0, TCGLabel *l1) static void tcg_out_op_rr(TCGContext *s, TCGOpcode op, TCGReg r0, TCGReg r1) { - tcg_insn_unit insn = 0; + tcg_insn_unit_tci insn = 0; insn = deposit32(insn, 0, 8, op); insn = deposit32(insn, 8, 4, r0);
@@ -191,7 +195,7 @@ static void tcg_out_op_rr(TCGContext *s, TCGOpcode op, TCGReg r0, TCGReg r1) static void tcg_out_op_rrm(TCGContext *s, TCGOpcode op, TCGReg r0, TCGReg r1, TCGArg m2) { - tcg_insn_unit insn = 0; + tcg_insn_unit_tci insn = 0; tcg_debug_assert(m2 == extract32(m2, 0, 16)); insn = deposit32(insn, 0, 8, op);
@@ -204,7 +208,7 @@ static void tcg_out_op_rrm(TCGContext *s, TCGOpcode op, static void tcg_out_op_rrr(TCGContext *s, TCGOpcode op, TCGReg r0, TCGReg r1, TCGReg r2) { - tcg_insn_unit insn = 0; + tcg_insn_unit_tci insn = 0; insn = deposit32(insn, 0, 8, op); insn = deposit32(insn, 8, 4, r0);
@@ -216,7 +220,7 @@ static void tcg_out_op_rrr(TCGContext *s, TCGOpcode op, static void tcg_out_op_rrs(TCGContext *s, TCGOpcode op, TCGReg r0, TCGReg r1, intptr_t i2) { - tcg_insn_unit insn = 0; + tcg_insn_unit_tci insn = 0; tcg_debug_assert(i2 == sextract32(i2, 0, 16)); insn = deposit32(insn, 0, 8, op);
@@ -229,7 +233,7 @@ static void tcg_out_op_rrs(TCGContext *s, TCGOpcode op, static void tcg_out_op_rrbb(TCGContext *s, TCGOpcode op, TCGReg r0, TCGReg r1, uint8_t b2, uint8_t b3) { - tcg_insn_unit insn = 0; + tcg_insn_unit_tci insn = 0; tcg_debug_assert(b2 == extract32(b2, 0, 6)); tcg_debug_assert(b3 == extract32(b3, 0, 6));
@@ -244,7 +248,7 @@ static void tcg_out_op_rrbb(TCGContext *s, TCGOpcode op, TCGReg r0, static void tcg_out_op_rrrc(TCGContext *s, TCGOpcode op, TCGReg r0, TCGReg r1, TCGReg r2, TCGCond c3) { - tcg_insn_unit insn = 0; + tcg_insn_unit_tci insn = 0; insn = deposit32(insn, 0, 8, op); insn = deposit32(insn, 8, 4, r0);
@@ -257,7 +261,7 @@ static void tcg_out_op_rrrc(TCGContext *s, TCGOpcode op, static void tcg_out_op_rrrbb(TCGContext *s, TCGOpcode op, TCGReg r0, TCGReg r1, TCGReg r2, uint8_t b3, uint8_t b4) { - tcg_insn_unit insn = 0; + tcg_insn_unit_tci insn = 0; tcg_debug_assert(b3 == extract32(b3, 0, 6)); tcg_debug_assert(b4 == extract32(b4, 0, 6));
@@ -287,7 +291,7 @@ static void tcg_out_op_rrrrrc(TCGContext *s, TCGOpcode op, TCGReg r0, TCGReg r1, TCGReg r2, TCGReg r3, TCGReg r4, TCGCond c5) { - tcg_insn_unit insn = 0; + tcg_insn_unit_tci insn = 0; insn = deposit32(insn, 0, 8, op); insn = deposit32(insn, 8, 4, r0);
@@ -446,7 +450,7 @@ static void tcg_out_call(TCGContext *s, const tcg_insn_unit *func, const TCGHelperInfo *info) { ffi_cif *cif = info->cif; - tcg_insn_unit insn = 0; + tcg_insn_unit_tci insn = 0; uint8_t which; if (cif->rtype == &ffi_type_void) {
tcg/wasm/tcg-target.h +1 -1
@@ -42,7 +42,7 @@#define TCG_TARGET_H #define TCG_TARGET_INTERPRETER 1 -#define TCG_TARGET_INSN_UNIT_SIZE 4 +#define TCG_TARGET_INSN_UNIT_SIZE 1#define MAX_CODE_GEN_BUFFER_SIZE ((size_t)-1)/* Number of registers available. */
Signature · 1 line
2.43.0
PATCH 7/35 tcg/wasm: Set TCG_TARGET_REG_BITS to 64
In reply to Kohei TokunagaThe Wasm backend targets wasm64 as the host.
--- tcg/wasm/tcg-target-reg-bits.h | 9 ++--- tcg/wasm/tcg-target.c.inc | 69 +++------------------------------- 2 files changed, 8 insertions(+), 70 deletions(-)
tcg/wasm/tcg-target-reg-bits.h +3 -6
@@ -7,12 +7,9 @@#ifndef TCG_TARGET_REG_BITS_H #define TCG_TARGET_REG_BITS_H -#if UINTPTR_MAX ==UINT32_MAX -# define TCG_TARGET_REG_BITS 32 -#elif UINTPTR_MAX ==UINT64_MAX -# define TCG_TARGET_REG_BITS 64 -#else -# error Unknown pointer size for tci target +#if UINTPTR_MAX !=UINT64_MAX +# error Unsupported pointer size for TCG target#endif +# define TCG_TARGET_REG_BITS 64#endif
tcg/wasm/tcg-target.c.inc +5 -64
95 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH 13/35 tcg/wasm: Add setcond/negsetcond/movcond instructions
In reply to Kohei TokunagaThese TCG instructions are implemented by using Wasm's if and else instructions. Support for TCG_COND_TSTEQ and TCG_COND_TSTNE is not yet implemented, so TCG_TARGET_HAS_tst is set to 0.
The tgen_setcond function was used by several other functions (e.g. tgen_negsetcond) and intended specifically for emitting TCI code. So it has been renamed to tgen_negsetcond_tci.
--- tcg/wasm/tcg-target-has.h | 2 +- tcg/wasm/tcg-target.c.inc | 166 +++++++++++++++++++++++++++++++++++++- 2 files changed, 163 insertions(+), 5 deletions(-)
tcg/wasm/tcg-target-has.h +1 -1
@@ -13,7 +13,7 @@#define TCG_TARGET_HAS_qemu_ldst_i128 0 -#define TCG_TARGET_HAS_tst 1 +#define TCG_TARGET_HAS_tst 0#define TCG_TARGET_extract_valid (type, ofs, len) 1#define TCG_TARGET_sextract_valid (type, ofs, len) 1
tcg/wasm/tcg-target.c.inc +162 -4
236 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH 3/35 dockerfiles: Add support for wasm64 to the wasm Dockerfile
In reply to Kohei TokunagaThis commit fixes Dockerfile of the wasm build to support both of wasm32 and wasm64 build. Dockerfile takes the following build arguments and use these values for building dependencies.
- TARGET_CPU: target wasm arch (wasm32 or wasm64)
- WASM64_MEMORY64: target -sMEMORY64 mode (1 or 2)
---
MAINTAINERS | 2 +-
...2-cross.docker => emsdk-wasm-cross.docker} | 29 ++++++++++++++-----
2 files changed, 23 insertions(+), 8 deletions(-)
rename tests/docker/dockerfiles/{emsdk-wasm32-cross.docker => emsdk-wasm-cross.docker} (85%)V1:
- This commit is under review in another patch series so please ignore it here.
MAINTAINERS +1 -1
@@ -647,7 +647,7 @@ F: include/system/os-wasm.h F: os-wasm.c F: util/coroutine-wasm.c F: configs/meson/emscripten.txt -F: tests/docker/dockerfiles/emsdk-wasm32-cross.docker +F: tests/docker/dockerfiles/emsdk-wasm-cross.docker Alpha Machines --------------
tests/docker/dockerfiles/emsdk-wasm-cross.docker renamed from tests/docker/dockerfiles/emsdk-wasm32-cross.docker +22 -7
@@ -1,14 +1,17 @@ # syntax = docker/dockerfile:1.5 -ARG EMSDK_VERSION_QEMU=3.1.50 +ARG EMSDK_VERSION_QEMU=4.0.10 ARG ZLIB_VERSION=1.3.1 ARG GLIB_MINOR_VERSION=2.84 ARG GLIB_VERSION=${GLIB_MINOR_VERSION}.0 ARG PIXMAN_VERSION=0.44.2 -ARG FFI_VERSION=v3.4.7 +ARG FFI_VERSION=v3.5.2 ARG MESON_VERSION=1.5.0 +ARG TARGET_CPU=wasm32 +ARG WASM64_MEMORY64=0 -FROM emscripten/emsdk:$EMSDK_VERSION_QEMU AS build-base +FROM emscripten/emsdk:$EMSDK_VERSION_QEMU AS build-base-common +ARG TARGET_CPU ARG MESON_VERSION ENV TARGET=/builddeps/target ENV CPATH="$TARGET/include"
@@ -33,8 +36,8 @@ RUN <<EOF cat <<EOT > /cross.meson [host_machine] system = 'emscripten' -cpu_family = 'wasm32' -cpu = 'wasm32' +cpu_family = '${TARGET_CPU}' +cpu = '${TARGET_CPU}' endian = 'little' [binaries]
@@ -46,6 +49,16 @@ pkgconfig = ['pkg-config', '--static'] EOT EOF +FROM build-base-common AS build-base-wasm32 + +FROM build-base-common AS build-base-wasm64 +ARG WASM64_MEMORY64 +ENV CFLAGS="$CFLAGS -sMEMORY64=${WASM64_MEMORY64}" +ENV CXXFLAGS="$CXXFLAGS -sMEMORY64=${WASM64_MEMORY64}" +ENV LDFLAGS="$LDFLAGS -sMEMORY64=${WASM64_MEMORY64}" + +FROM build-base-${TARGET_CPU} AS build-base + FROM build-base AS zlib-dev ARG ZLIB_VERSION RUN mkdir -p /zlib
@@ -56,17 +69,19 @@ RUN emconfigure ./configure --prefix=$TARGET --static RUN emmake make install -j$(nproc) FROM build-base AS libffi-dev +ARG TARGET_CPU +ARG WASM64_MEMORY64 ARG FFI_VERSION RUN mkdir -p /libffi RUN git clone https://github.com/libffi/libffi /libffi WORKDIR /libffi RUN git checkout $FFI_VERSION RUN autoreconf -fiv -RUN emconfigure ./configure --host=wasm32-unknown-linux \ +RUN emconfigure ./configure --host=${TARGET_CPU}-unknown-linux \ --prefix=$TARGET --enable-static \ --disable-shared --disable-dependency-tracking \ --disable-builddir --disable-multi-os-directory \ - --disable-raw-api --disable-docs + --disable-raw-api --disable-docs WASM64_MEMORY64=${WASM64_MEMORY64} RUN emmake make install SUBDIRS='include' -j$(nproc) FROM build-base AS pixman-dev
Signature · 1 line
2.43.0
PATCH 15/35 tcg/wasm: Add load and store instructions
In reply to Kohei TokunagaThis commit implements load and store operations using Wasm's memory instructions. Since Wasm load and store instructions don't support negative offsets, address calculations are performed separately before the memory access.
When Emscripten's -sMEMORY64=2 is enabled, the address size must be 32bits. So this commit updates the build tools to propagate this flag to the C code via the WASM64_MEMORY64_2 macro. In this case, the emitted code casts pointers to 32bit before memory oprations.
Additionally, the declaration of "--wasm64-32bit-address-limit" flag has been moved from the configure script to meson.build. So the flag name is updated to "--enable-wasm64-32bit-address-limit" to follow Meson's naming conventions.
--- .gitlab-ci.d/buildtest.yml | 2 +- configure | 8 ++- meson.build | 4 ++ meson_options.txt | 3 ++ scripts/meson-buildoptions.sh | 5 ++ tcg/wasm/tcg-target.c.inc | 95 +++++++++++++++++++++++++++++++++++ 6 files changed, 111 insertions(+), 6 deletions(-)
V1:
- Although checkpatch.pl reports an error "line over 90 characters" in scripts/meson-buildoptions.sh, the changes were automatically generated by meson-buildoptions.py and are preserved as-is.
.gitlab-ci.d/buildtest.yml +1 -1
@@ -812,4 +812,4 @@ build-wasm64-32bit: job: wasm64-32bit-emsdk-cross-container variables: IMAGE: emsdk-wasm64-32bit-cross - CONFIGURE_ARGS: --static --cpu=wasm64 --wasm64-32bit-address-limit --disable-tools --enable-debug --enable-tcg-interpreter + CONFIGURE_ARGS: --static --cpu=wasm64 --enable-wasm64-32bit-address-limit --disable-tools --enable-debug --enable-tcg-interpreter
configure +3 -5
@@ -243,7 +243,9 @@ for opt do ;; --without-default-features) default_feature="no" ;; - --wasm64-32bit-address-limit) wasm64_memory64="2" + --enable-wasm64-32bit-address-limit) wasm64_memory64="2" + ;; + --disable-wasm64-32bit-address-limit) wasm64_memory64="1" ;; esac done
@@ -801,8 +803,6 @@ for opt do ;; --disable-rust) rust=disabled ;; - --wasm64-32bit-address-limit) - ;; # everything else has the same name in configure and meson --*) meson_option_parse "$opt" "$optarg" ;;
@@ -928,8 +928,6 @@ Advanced options (experts only): --disable-containers don't use containers for cross-building --container-engine=TYPE which container engine to use [$container_engine] --gdb=GDB-path gdb to use for gdbstub tests [$gdb_bin] - --wasm64-32bit-address-limit Restrict wasm64 address space to 32-bit (default - is to use the whole 64-bit range). EOF meson_options_help cat << EOF
meson.build +4 -0
@@ -393,6 +393,10 @@ elif host_os == 'windows' if compiler.get_id() == 'clang' and compiler.get_linker_id() != 'ld.lld' error('On windows, you need to use lld with clang - use msys2 clang64/clangarm64 env') endif +elif host_os == 'emscripten' + if cpu == 'wasm64' and get_option('wasm64_32bit_address_limit') + qemu_common_flags += '-DWASM64_MEMORY64_2' + endif endif # Choose instruction set (currently x86-only)
meson_options.txt +3 -0
@@ -388,3 +388,6 @@ option('rust', type: 'feature', value: 'disabled', description: 'Rust support') option('strict_rust_lints', type: 'boolean', value: false, description: 'Enable stricter set of Rust warnings') + +option('wasm64_32bit_address_limit', type: 'boolean', value: false, + description: 'Restrict wasm64 address space to 32-bit (default is to use the whole 64-bit range).')
scripts/meson-buildoptions.sh +5 -0
@@ -56,6 +56,9 @@ meson_options_help() { printf "%s\n" ' dtrace/ftrace/log/nop/simple/syslog/ust)' printf "%s\n" ' --enable-tsan enable thread sanitizer' printf "%s\n" ' --enable-ubsan enable undefined behaviour sanitizer' + printf "%s\n" ' --enable-wasm64-32bit-address-limit' + printf "%s\n" ' Restrict wasm64 address space to 32-bit (default' + printf "%s\n" ' is to use the whole 64-bit range).' printf "%s\n" ' --firmwarepath=VALUES search PATH for firmware files [share/qemu-' printf "%s\n" ' firmware]' printf "%s\n" ' --iasl=VALUE Path to ACPI disassembler'
@@ -576,6 +579,8 @@ _meson_option_parse() { --disable-vte) printf "%s" -Dvte=disabled ;; --enable-vvfat) printf "%s" -Dvvfat=enabled ;; --disable-vvfat) printf "%s" -Dvvfat=disabled ;; + --enable-wasm64-32bit-address-limit) printf "%s" -Dwasm64_32bit_address_limit=true ;; + --disable-wasm64-32bit-address-limit) printf "%s" -Dwasm64_32bit_address_limit=false ;; --enable-werror) printf "%s" -Dwerror=true ;; --disable-werror) printf "%s" -Dwerror=false ;; --enable-whpx) printf "%s" -Dwhpx=enabled ;;
tcg/wasm/tcg-target.c.inc +95 -0
189 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH 14/35 tcg/wasm: Add deposit/sextract/extract instrcutions
In reply to Kohei TokunagaThe tcg_out_extract and tcg_out_sextract functions were used by several other functions (e.g. tcg_out_ext*) and intended to emit TCI code. So they have been renamed to tcg_tci_out_extract and tcg_tci_out_sextract.
--- tcg/wasm/tcg-target.c.inc | 104 +++++++++++++++++++++++++++++++++----- 1 file changed, 91 insertions(+), 13 deletions(-)
tcg/wasm/tcg-target.c.inc +91 -13
@@ -163,7 +163,10 @@ typedef enum { OPC_I64_SHR_U = 0x88, OPC_I32_WRAP_I64 = 0xa7, + OPC_I64_EXTEND_I32_S = 0xac, OPC_I64_EXTEND_I32_U = 0xad, + OPC_I64_EXTEND8_S = 0xc2, + OPC_I64_EXTEND16_S = 0xc3, } WasmInsn; typedef enum {
@@ -380,6 +383,66 @@ static void tcg_wasm_out_movcond(TCGContext *s, TCGType type, TCGReg ret, tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); } +static void tcg_wasm_out_deposit(TCGContext *s, + TCGReg dest, TCGReg arg1, TCGReg arg2, + int pos, int len) +{ + int64_t mask = (((int64_t)1 << len) - 1) << pos; + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + tcg_wasm_out_op_const(s, OPC_I64_CONST, ~mask); + tcg_wasm_out_op(s, OPC_I64_AND); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg2)); + tcg_wasm_out_op_const(s, OPC_I64_CONST, pos); + tcg_wasm_out_op(s, OPC_I64_SHL); + tcg_wasm_out_op_const(s, OPC_I64_CONST, mask); + tcg_wasm_out_op(s, OPC_I64_AND); + tcg_wasm_out_op(s, OPC_I64_OR); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(dest)); +} + +static void tcg_wasm_out_extract(TCGContext *s, TCGReg dest, TCGReg arg1, + int pos, int len) +{ + int64_t mask = ~0ULL >> (64 - len); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + if (pos > 0) { + tcg_wasm_out_op_const(s, OPC_I64_CONST, pos); + tcg_wasm_out_op(s, OPC_I64_SHR_U); + } + if ((pos + len) < 64) { + tcg_wasm_out_op_const(s, OPC_I64_CONST, mask); + tcg_wasm_out_op(s, OPC_I64_AND); + } + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(dest)); +} + +static void tcg_wasm_out_sextract(TCGContext *s, TCGReg dest, TCGReg arg1, + int pos, int len) +{ + int discard = 64 - len; + int high = discard - pos; + + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + + if ((pos == 0) && (len == 8)) { + tcg_wasm_out_op(s, OPC_I64_EXTEND8_S); + } else if ((pos == 0) && (len == 16)) { + tcg_wasm_out_op(s, OPC_I64_EXTEND16_S); + } else if ((pos == 0) && (len == 32)) { + tcg_wasm_out_op(s, OPC_I32_WRAP_I64); + tcg_wasm_out_op(s, OPC_I64_EXTEND_I32_S); + } else { + if (high > 0) { + tcg_wasm_out_op_const(s, OPC_I64_CONST, high); + tcg_wasm_out_op(s, OPC_I64_SHL); + } + tcg_wasm_out_op_const(s, OPC_I64_CONST, discard); + tcg_wasm_out_op(s, OPC_I64_SHR_S); + } + + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(dest)); +} + static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, intptr_t value, intptr_t addend) {
@@ -591,6 +654,12 @@ static void tcg_out_op_rrrrrc(TCGContext *s, TCGOpcode op, tcg_out32(s, insn); } +static void tcg_tci_out_extract(TCGContext *s, TCGType type, TCGReg rd, + TCGReg rs, unsigned pos, unsigned len) +{ + tcg_out_op_rrbb(s, INDEX_op_extract, rd, rs, pos, len); +} + static void tcg_out_ldst(TCGContext *s, TCGOpcode op, TCGReg val, TCGReg base, intptr_t offset) {
@@ -651,7 +720,8 @@ static void tcg_out_movi(TCGContext *s, TCGType type, static void tcg_out_extract(TCGContext *s, TCGType type, TCGReg rd, TCGReg rs, unsigned pos, unsigned len) { - tcg_out_op_rrbb(s, INDEX_op_extract, rd, rs, pos, len); + tcg_tci_out_extract(s, type, rd, rs, pos, len); + tcg_wasm_out_extract(s, rd, rs, pos, len); } static const TCGOutOpExtract outop_extract = {
@@ -659,10 +729,17 @@ static const TCGOutOpExtract outop_extract = { .out_rr = tcg_out_extract, }; +static void tcg_tci_out_sextract(TCGContext *s, TCGType type, TCGReg rd, + TCGReg rs, unsigned pos, unsigned len) +{ + tcg_out_op_rrbb(s, INDEX_op_sextract, rd, rs, pos, len); +} + static void tcg_out_sextract(TCGContext *s, TCGType type, TCGReg rd, TCGReg rs, unsigned pos, unsigned len) { - tcg_out_op_rrbb(s, INDEX_op_sextract, rd, rs, pos, len); + tcg_tci_out_sextract(s, type, rd, rs, pos, len); + tcg_wasm_out_sextract(s, rd, rs, pos, len); } static const TCGOutOpExtract outop_sextract = {
@@ -676,34 +753,34 @@ static const TCGOutOpExtract2 outop_extract2 = { static void tcg_out_ext8s(TCGContext *s, TCGType type, TCGReg rd, TCGReg rs) { - tcg_out_sextract(s, type, rd, rs, 0, 8); + tcg_tci_out_sextract(s, type, rd, rs, 0, 8); } static void tcg_out_ext8u(TCGContext *s, TCGReg rd, TCGReg rs) { - tcg_out_extract(s, TCG_TYPE_REG, rd, rs, 0, 8); + tcg_tci_out_extract(s, TCG_TYPE_REG, rd, rs, 0, 8); } static void tcg_out_ext16s(TCGContext *s, TCGType type, TCGReg rd, TCGReg rs) { - tcg_out_sextract(s, type, rd, rs, 0, 16); + tcg_tci_out_sextract(s, type, rd, rs, 0, 16); } static void tcg_out_ext16u(TCGContext *s, TCGReg rd, TCGReg rs) { - tcg_out_extract(s, TCG_TYPE_REG, rd, rs, 0, 16); + tcg_tci_out_extract(s, TCG_TYPE_REG, rd, rs, 0, 16); } static void tcg_out_ext32s(TCGContext *s, TCGReg rd, TCGReg rs) { tcg_debug_assert(TCG_TARGET_REG_BITS == 64); - tcg_out_sextract(s, TCG_TYPE_I64, rd, rs, 0, 32); + tcg_tci_out_sextract(s, TCG_TYPE_I64, rd, rs, 0, 32); } static void tcg_out_ext32u(TCGContext *s, TCGReg rd, TCGReg rs) { tcg_debug_assert(TCG_TARGET_REG_BITS == 64); - tcg_out_extract(s, TCG_TYPE_I64, rd, rs, 0, 32); + tcg_tci_out_extract(s, TCG_TYPE_I64, rd, rs, 0, 32); } static void tcg_out_exts_i32_i64(TCGContext *s, TCGReg rd, TCGReg rs)
@@ -891,6 +968,7 @@ static void tgen_deposit(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2, unsigned ofs, unsigned len) { tcg_out_op_rrrbb(s, INDEX_op_deposit, a0, a1, a2, ofs, len); + tcg_wasm_out_deposit(s, a0, a1, a2, ofs, len); } static const TCGOutOpDeposit outop_deposit = {
@@ -948,7 +1026,7 @@ static const TCGOutOpBinary outop_eqv = { #if TCG_TARGET_REG_BITS == 64 static void tgen_extrh_i64_i32(TCGContext *s, TCGType t, TCGReg a0, TCGReg a1) { - tcg_out_extract(s, TCG_TYPE_I64, a0, a1, 32, 32); + tcg_tci_out_extract(s, TCG_TYPE_I64, a0, a1, 32, 32); } static const TCGOutOpUnary outop_extrh_i64_i32 = {
@@ -1112,7 +1190,7 @@ static void tgen_sar(TCGContext *s, TCGType type, { TCGReg orig_a1 = a1; if (type < TCG_TYPE_REG) { - tcg_out_ext32s(s, TCG_REG_TMP, a1); + tcg_tci_out_sextract(s, TCG_TYPE_I64, TCG_REG_TMP, a1, 0, 32); a1 = TCG_REG_TMP; } tcg_out_op_rrr(s, INDEX_op_sar, a0, a1, a2);
@@ -1142,7 +1220,7 @@ static void tgen_shr(TCGContext *s, TCGType type, { TCGReg orig_a1 = a1; if (type < TCG_TYPE_REG) { - tcg_out_ext32u(s, TCG_REG_TMP, a1); + tcg_tci_out_extract(s, TCG_TYPE_I64, TCG_REG_TMP, a1, 0, 32); a1 = TCG_REG_TMP; } tcg_out_op_rrr(s, INDEX_op_shr, a0, a1, a2);
@@ -1241,7 +1319,7 @@ static void tgen_bswap16(TCGContext *s, TCGType type, { tcg_out_op_rr(s, INDEX_op_bswap16, a0, a1); if (flags & TCG_BSWAP_OS) { - tcg_out_sextract(s, TCG_TYPE_REG, a0, a0, 0, 16); + tcg_tci_out_sextract(s, TCG_TYPE_REG, a0, a0, 0, 16); } }
@@ -1255,7 +1333,7 @@ static void tgen_bswap32(TCGContext *s, TCGType type, { tcg_out_op_rr(s, INDEX_op_bswap32, a0, a1); if (flags & TCG_BSWAP_OS) { - tcg_out_sextract(s, TCG_TYPE_REG, a0, a0, 0, 32); + tcg_tci_out_sextract(s, TCG_TYPE_REG, a0, a0, 0, 32); } }
Signature · 1 line
2.43.0
PATCH 16/35 tcg/wasm: Add mov/movi instructions
In reply to Kohei TokunagaThe tcg_out_mov and tcg_out_movi functions were used by several other functions and intended to emit TCI code. So they have been renamed to tcg_tci_out_mov and tcg_tci_out_movi.
--- tcg/wasm/tcg-target.c.inc | 85 +++++++++++++++++++++++++++------------ 1 file changed, 60 insertions(+), 25 deletions(-)
tcg/wasm/tcg-target.c.inc +60 -25
@@ -524,6 +524,28 @@ static void tcg_wasm_out_sextract(TCGContext *s, TCGReg dest, TCGReg arg1, tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(dest)); } +static void tcg_wasm_out_mov(TCGContext *s, TCGReg ret, TCGReg arg) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg)); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); +} + +static void tcg_wasm_out_movi(TCGContext *s, TCGType type, + TCGReg ret, tcg_target_long arg) +{ + switch (type) { + case TCG_TYPE_I32: + tcg_wasm_out_op_const(s, OPC_I64_CONST, (int32_t)arg); + break; + case TCG_TYPE_I64: + tcg_wasm_out_op_const(s, OPC_I64_CONST, arg); + break; + default: + g_assert_not_reached(); + } + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); +} + static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, intptr_t value, intptr_t addend) {
@@ -735,6 +757,33 @@ static void tcg_out_op_rrrrrc(TCGContext *s, TCGOpcode op, tcg_out32(s, insn); } +static void tcg_tci_out_movi(TCGContext *s, TCGType type, + TCGReg ret, tcg_target_long arg) +{ + switch (type) { + case TCG_TYPE_I32: +#if TCG_TARGET_REG_BITS == 64 + arg = (int32_t)arg; + /* fall through */ + case TCG_TYPE_I64: +#endif + break; + default: + g_assert_not_reached(); + } + + if (arg == sextract32(arg, 0, 20)) { + tcg_out_op_ri(s, INDEX_op_tci_movi, ret, arg); + } else { + tcg_insn_unit_tci insn = 0; + + new_pool_label(s, arg, 20, s->code_ptr, 0); + insn = deposit32(insn, 0, 8, INDEX_op_tci_movl); + insn = deposit32(insn, 8, 4, ret); + tcg_out32(s, insn); + } +} + static void tcg_tci_out_extract(TCGContext *s, TCGType type, TCGReg rd, TCGReg rs, unsigned pos, unsigned len) {
@@ -746,7 +795,7 @@ static void tcg_out_ldst(TCGContext *s, TCGOpcode op, TCGReg val, { stack_bounds_check(base, offset); if (offset != sextract32(offset, 0, 16)) { - tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP, offset); + tcg_tci_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP, offset); tcg_out_op_rrr(s, INDEX_op_add, TCG_REG_TMP, TCG_REG_TMP, base); base = TCG_REG_TMP; offset = 0;
@@ -768,37 +817,23 @@ static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg val, TCGReg base, tcg_wasm_out_ld(s, wasm_opc, val, base, offset); } -static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg) +static void tcg_tci_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg) { tcg_out_op_rr(s, INDEX_op_mov, ret, arg); +} + +static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg) +{ + tcg_tci_out_mov(s, type, ret, arg); + tcg_wasm_out_mov(s, ret, arg); return true; } static void tcg_out_movi(TCGContext *s, TCGType type, TCGReg ret, tcg_target_long arg) { - switch (type) { - case TCG_TYPE_I32: -#if TCG_TARGET_REG_BITS == 64 - arg = (int32_t)arg; - /* fall through */ - case TCG_TYPE_I64: -#endif - break; - default: - g_assert_not_reached(); - } - - if (arg == sextract32(arg, 0, 20)) { - tcg_out_op_ri(s, INDEX_op_tci_movi, ret, arg); - } else { - tcg_insn_unit insn = 0; - - new_pool_label(s, arg, 20, s->code_ptr, 0); - insn = deposit32(insn, 0, 8, INDEX_op_tci_movl); - insn = deposit32(insn, 8, 4, ret); - tcg_out32(s, insn); - } + tcg_tci_out_movi(s, type, ret, arg); + tcg_wasm_out_movi(s, type, ret, arg); } static void tcg_out_extract(TCGContext *s, TCGType type, TCGReg rd,
@@ -880,7 +915,7 @@ static void tcg_out_extu_i32_i64(TCGContext *s, TCGReg rd, TCGReg rs) static void tcg_out_extrl_i64_i32(TCGContext *s, TCGReg rd, TCGReg rs) { tcg_debug_assert(TCG_TARGET_REG_BITS == 64); - tcg_out_mov(s, TCG_TYPE_I32, rd, rs); + tcg_tci_out_mov(s, TCG_TYPE_I32, rd, rs); } static bool tcg_out_xchg(TCGContext *s, TCGType type, TCGReg r1, TCGReg r2)
Signature · 1 line
2.43.0
PATCH 11/35 tcg/wasm: Add add/sub/mul instructions
In reply to Kohei TokunagaAdd, sub and mul operations are implemented using the corresponding instructions in Wasm.
--- tcg/wasm/tcg-target.c.inc | 6 ++++++ 1 file changed, 6 insertions(+)
tcg/wasm/tcg-target.c.inc +6 -0
@@ -120,6 +120,9 @@ typedef enum { OPC_GLOBAL_GET = 0x23, OPC_GLOBAL_SET = 0x24, + OPC_I64_ADD = 0x7c, + OPC_I64_SUB = 0x7d, + OPC_I64_MUL = 0x7e, OPC_I64_AND = 0x83, OPC_I64_OR = 0x84, OPC_I64_XOR = 0x85,
@@ -599,6 +602,7 @@ static void tgen_add(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { tcg_out_op_rrr(s, INDEX_op_add, a0, a1, a2); + tcg_wasm_out_o1_i2(s, OPC_I64_ADD, a0, a1, a2); } static const TCGOutOpBinary outop_add = {
@@ -777,6 +781,7 @@ static void tgen_mul(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { tcg_out_op_rrr(s, INDEX_op_mul, a0, a1, a2); + tcg_wasm_out_o1_i2(s, OPC_I64_MUL, a0, a1, a2); } static const TCGOutOpBinary outop_mul = {
@@ -967,6 +972,7 @@ static void tgen_sub(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { tcg_out_op_rrr(s, INDEX_op_sub, a0, a1, a2); + tcg_wasm_out_o1_i2(s, OPC_I64_SUB, a0, a1, a2); } static const TCGOutOpSubtract outop_sub = {
Signature · 1 line
2.43.0
PATCH 1/35 meson: Add wasm64 support to the --cpu flag
In reply to Kohei Tokunagawasm64 target enables 64bit pointers using Emscripten's -sMEMORY64=1 flag[1]. This enables QEMU to run 64bit guests.
Although the configure script uses "uname -m" as the fallback value when "cpu" is empty, this can't be used for Emscripten which targets to Wasm. So, in wasm build, this commit fixes configure to require --cpu flag to be explicitly specified by the user.
[1] https://emscripten.org/docs/tools_reference/settings_reference.html#memory64
--- configure | 6 +++++- meson.build | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-)
V1:
- This commit is under review in another patch series so please ignore it here.
configure +5 -1
@@ -365,7 +365,6 @@ elif check_define __APPLE__; then host_os=darwin elif check_define EMSCRIPTEN ; then host_os=emscripten - cpu=wasm32 cross_compile="yes" else # This is a fatal error, but don't report it yet, because we
@@ -425,6 +424,8 @@ elif check_define __aarch64__ ; then cpu="aarch64" elif check_define __loongarch64 ; then cpu="loongarch64" +elif check_define EMSCRIPTEN ; then + error_exit "wasm32 or wasm64 must be specified to the cpu flag" else # Using uname is really broken, but it is just a fallback for architectures # that are going to use TCI anyway
@@ -535,6 +536,9 @@ case "$cpu" in wasm32) CPU_CFLAGS="-m32" ;; + wasm64) + CPU_CFLAGS="-m64 -sMEMORY64=1" + ;; esac if test -n "$host_arch" && {
meson.build +2 -2
@@ -52,7 +52,7 @@ qapi_trace_events = [] bsd_oses = ['gnu/kfreebsd', 'freebsd', 'netbsd', 'openbsd', 'dragonfly', 'darwin'] supported_oses = ['windows', 'freebsd', 'netbsd', 'openbsd', 'darwin', 'sunos', 'linux', 'emscripten'] supported_cpus = ['ppc', 'ppc64', 's390x', 'riscv32', 'riscv64', 'x86', 'x86_64', - 'arm', 'aarch64', 'loongarch64', 'mips', 'mips64', 'sparc64', 'wasm32'] + 'arm', 'aarch64', 'loongarch64', 'mips', 'mips64', 'sparc64', 'wasm32', 'wasm64'] cpu = host_machine.cpu_family()
@@ -916,7 +916,7 @@ if have_tcg if not get_option('tcg_interpreter') error('Unsupported CPU @0@, try --enable-tcg-interpreter'.format(cpu)) endif - elif host_arch == 'wasm32' + elif host_arch == 'wasm32' or host_arch == 'wasm64' if not get_option('tcg_interpreter') error('WebAssembly host requires --enable-tcg-interpreter') endif
Signature · 1 line
2.43.0
PATCH 17/35 tcg/wasm: Add ext instructions
In reply to Kohei TokunagaThis commit implements the ext operations using Wasm instructions.
--- tcg/wasm/tcg-target.c.inc | 8 ++++++++ 1 file changed, 8 insertions(+)
tcg/wasm/tcg-target.c.inc +8 -0
@@ -873,33 +873,39 @@ static const TCGOutOpExtract2 outop_extract2 = { static void tcg_out_ext8s(TCGContext *s, TCGType type, TCGReg rd, TCGReg rs) { tcg_tci_out_sextract(s, type, rd, rs, 0, 8); + tcg_wasm_out_sextract(s, rd, rs, 0, 8); } static void tcg_out_ext8u(TCGContext *s, TCGReg rd, TCGReg rs) { tcg_tci_out_extract(s, TCG_TYPE_REG, rd, rs, 0, 8); + tcg_wasm_out_extract(s, rd, rs, 0, 8); } static void tcg_out_ext16s(TCGContext *s, TCGType type, TCGReg rd, TCGReg rs) { tcg_tci_out_sextract(s, type, rd, rs, 0, 16); + tcg_wasm_out_sextract(s, rd, rs, 0, 16); } static void tcg_out_ext16u(TCGContext *s, TCGReg rd, TCGReg rs) { tcg_tci_out_extract(s, TCG_TYPE_REG, rd, rs, 0, 16); + tcg_wasm_out_extract(s, rd, rs, 0, 16); } static void tcg_out_ext32s(TCGContext *s, TCGReg rd, TCGReg rs) { tcg_debug_assert(TCG_TARGET_REG_BITS == 64); tcg_tci_out_sextract(s, TCG_TYPE_I64, rd, rs, 0, 32); + tcg_wasm_out_sextract(s, rd, rs, 0, 32); } static void tcg_out_ext32u(TCGContext *s, TCGReg rd, TCGReg rs) { tcg_debug_assert(TCG_TARGET_REG_BITS == 64); tcg_tci_out_extract(s, TCG_TYPE_I64, rd, rs, 0, 32); + tcg_wasm_out_extract(s, rd, rs, 0, 32); } static void tcg_out_exts_i32_i64(TCGContext *s, TCGReg rd, TCGReg rs)
@@ -916,6 +922,7 @@ static void tcg_out_extrl_i64_i32(TCGContext *s, TCGReg rd, TCGReg rs) { tcg_debug_assert(TCG_TARGET_REG_BITS == 64); tcg_tci_out_mov(s, TCG_TYPE_I32, rd, rs); + tcg_wasm_out_extract(s, rd, rs, 0, 32); } static bool tcg_out_xchg(TCGContext *s, TCGType type, TCGReg r1, TCGReg r2)
@@ -1146,6 +1153,7 @@ static const TCGOutOpBinary outop_eqv = { static void tgen_extrh_i64_i32(TCGContext *s, TCGType t, TCGReg a0, TCGReg a1) { tcg_tci_out_extract(s, TCG_TYPE_I64, a0, a1, 32, 32); + tcg_wasm_out_extract(s, a0, a1, 32, 32); } static const TCGOutOpUnary outop_extrh_i64_i32 = {
Signature · 1 line
2.43.0
PATCH 10/35 tcg/wasm: Add and/or/xor instructions
In reply to Kohei TokunagaThis commit implements and, or and xor operations using Wasm instructions. Each TCG variable is mapped to a 64bit Wasm variable. In Wasm, these instructions work by first pushing the operands into the Wasm's stack using get instructions. The result is left on the stack and this can be assigned to a variable by popping it using a set instruction. The Wasm binary format is documented at [1].
In this backend, TCI instructions are emitted to s->code_ptr, while the corresponding Wasm instructions are generated into a separate buffer allocated via tcg_malloc(). This buffer is intended to be merged into the TB before tcg_gen_code returns.
Additionally, since the Wasm instuction's index operand must be LEB128-encoded, this commit introduces an encoder function implemented following [2].
[1] https://webassembly.github.io/spec/core/binary/index.html
[2] https://en.wikipedia.org/wiki/LEB128
--- tcg/wasm/tcg-target.c.inc | 110 +++++++++++++++++++++++++++++++++++++- 1 file changed, 109 insertions(+), 1 deletion(-)
tcg/wasm/tcg-target.c.inc +109 -1
@@ -25,6 +25,8 @@ * THE SOFTWARE. */ +#include "qemu/queue.h" + /* Used for function call generation. */ #define TCG_TARGET_CALL_STACK_OFFSET 0 #define TCG_TARGET_STACK_ALIGN 8
@@ -92,6 +94,109 @@ static const char *const tcg_target_reg_names[TCG_TARGET_NB_REGS] = { }; #endif +/* converts a TCG register to a wasm variable index */ +static const uint8_t tcg_target_reg_index[TCG_TARGET_NB_REGS] = { + 0, /* TCG_REG_R0 */ + 1, /* TCG_REG_R1 */ + 2, /* TCG_REG_R2 */ + 3, /* TCG_REG_R3 */ + 4, /* TCG_REG_R4 */ + 5, /* TCG_REG_R5 */ + 6, /* TCG_REG_R6 */ + 7, /* TCG_REG_R7 */ + 8, /* TCG_REG_R8 */ + 9, /* TCG_REG_R9 */ + 10, /* TCG_REG_R10 */ + 11, /* TCG_REG_R11 */ + 12, /* TCG_REG_R12 */ + 13, /* TCG_REG_R13 */ + 14, /* TCG_REG_R14 */ + 15, /* TCG_REG_R15 */ +}; + +#define REG_IDX(r) tcg_target_reg_index[r] + +typedef enum { + OPC_GLOBAL_GET = 0x23, + OPC_GLOBAL_SET = 0x24, + + OPC_I64_AND = 0x83, + OPC_I64_OR = 0x84, + OPC_I64_XOR = 0x85, +} WasmInsn; + +#define BUF_SIZE 1024 +typedef struct LinkedBufEntry { + uint8_t data[BUF_SIZE]; + uint32_t size; + QSIMPLEQ_ENTRY(LinkedBufEntry) entry; +} LinkedBufEntry; + +typedef QSIMPLEQ_HEAD(, LinkedBufEntry) LinkedBuf; + +static void linked_buf_out8(LinkedBuf *linked_buf, uint8_t v) +{ + LinkedBufEntry *buf = QSIMPLEQ_LAST(linked_buf, LinkedBufEntry, entry); + if (!buf || (buf->size == BUF_SIZE)) { + LinkedBufEntry *e = tcg_malloc(sizeof(LinkedBufEntry)); + e->size = 0; + QSIMPLEQ_INSERT_TAIL(linked_buf, e, entry); + buf = e; + } + buf->data[buf->size++] = v; +} + +static void linked_buf_out_leb128(LinkedBuf *p, uint64_t v) +{ + uint8_t b; + do { + b = v & 0x7f; + v >>= 7; + if (v != 0) { + b |= 0x80; + } + linked_buf_out8(p, b); + } while (v != 0); +} + +/* + * wasm code is generataed in the dynamically allocated buffer which + * are managed as a linked list. + */ +static __thread LinkedBuf sub_buf; + +static void init_sub_buf(void) +{ + QSIMPLEQ_INIT(&sub_buf); +} +static void tcg_wasm_out8(TCGContext *s, uint8_t v) +{ + linked_buf_out8(&sub_buf, v); +} +static void tcg_wasm_out_leb128(TCGContext *s, uint64_t v) +{ + linked_buf_out_leb128(&sub_buf, v); +} + +static void tcg_wasm_out_op(TCGContext *s, WasmInsn opc) +{ + tcg_wasm_out8(s, opc); +} +static void tcg_wasm_out_op_idx(TCGContext *s, WasmInsn opc, uint32_t idx) +{ + tcg_wasm_out8(s, opc); + tcg_wasm_out_leb128(s, idx); +} + +static void tcg_wasm_out_o1_i2( + TCGContext *s, WasmInsn opc, TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg2)); + tcg_wasm_out_op(s, opc); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); +} + static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, intptr_t value, intptr_t addend) {
@@ -551,6 +656,7 @@ static void tgen_and(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { tcg_out_op_rrr(s, INDEX_op_and, a0, a1, a2); + tcg_wasm_out_o1_i2(s, OPC_I64_AND, a0, a1, a2); } static const TCGOutOpBinary outop_and = {
@@ -741,6 +847,7 @@ static void tgen_or(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { tcg_out_op_rrr(s, INDEX_op_or, a0, a1, a2); + tcg_wasm_out_o1_i2(s, OPC_I64_OR, a0, a1, a2); } static const TCGOutOpBinary outop_or = {
@@ -912,6 +1019,7 @@ static void tgen_xor(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { tcg_out_op_rrr(s, INDEX_op_xor, a0, a1, a2); + tcg_wasm_out_o1_i2(s, OPC_I64_XOR, a0, a1, a2); } static const TCGOutOpBinary outop_xor = {
@@ -1246,7 +1354,7 @@ static inline void tcg_target_qemu_prologue(TCGContext *s) static void tcg_out_tb_start(TCGContext *s) { - /* nothing to do */ + init_sub_buf(); } bool tcg_target_has_memory_bswap(MemOp memop)
Signature · 1 line
2.43.0
PATCH 18/35 tcg/wasm: Add bswap instructions
In reply to Kohei TokunagaThis commit introduces Wasm module local variables[1] assigned from the index 0. These variables are used as temporary storage during calculations.
[1] https://webassembly.github.io/spec/core/binary/instructions.html#variable-instructions
--- tcg/wasm/tcg-target.c.inc | 117 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+)
tcg/wasm/tcg-target.c.inc +117 -0
@@ -116,10 +116,16 @@ static const uint8_t tcg_target_reg_index[TCG_TARGET_NB_REGS] = { #define REG_IDX(r) tcg_target_reg_index[r] +/* Temporary local variables */ +#define TMP32_LOCAL_0_IDX 0 +#define TMP64_LOCAL_0_IDX 1 + typedef enum { OPC_IF = 0x04, OPC_ELSE = 0x05, OPC_END = 0x0b, + OPC_LOCAL_GET = 0x20, + OPC_LOCAL_SET = 0x21, OPC_GLOBAL_GET = 0x23, OPC_GLOBAL_SET = 0x24,
@@ -163,9 +169,12 @@ typedef enum { OPC_I64_GE_U = 0x5a, OPC_I32_ADD = 0x6a, + OPC_I32_AND = 0x71, + OPC_I32_OR = 0x72, OPC_I32_SHL = 0x74, OPC_I32_SHR_S = 0x75, OPC_I32_SHR_U = 0x76, + OPC_I32_ROTR = 0x78, OPC_I64_ADD = 0x7c, OPC_I64_SUB = 0x7d,
@@ -176,6 +185,7 @@ typedef enum { OPC_I64_SHL = 0x86, OPC_I64_SHR_S = 0x87, OPC_I64_SHR_U = 0x88, + OPC_I64_ROTR = 0x8a, OPC_I32_WRAP_I64 = 0xa7, OPC_I64_EXTEND_I32_S = 0xac,
@@ -524,6 +534,110 @@ static void tcg_wasm_out_sextract(TCGContext *s, TCGReg dest, TCGReg arg1, tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(dest)); } +static void tcg_wasm_out_bswap64( + TCGContext *s, TCGReg dest, TCGReg src) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(src)); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 32); + tcg_wasm_out_op(s, OPC_I64_ROTR); + tcg_wasm_out_op_idx(s, OPC_LOCAL_SET, TMP64_LOCAL_0_IDX); + + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, TMP64_LOCAL_0_IDX); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 0xff000000ff000000); + tcg_wasm_out_op(s, OPC_I64_AND); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 24); + tcg_wasm_out_op(s, OPC_I64_SHR_U); + + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, TMP64_LOCAL_0_IDX); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 0x00ff000000ff0000); + tcg_wasm_out_op(s, OPC_I64_AND); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 8); + tcg_wasm_out_op(s, OPC_I64_SHR_U); + + tcg_wasm_out_op(s, OPC_I64_OR); + + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, TMP64_LOCAL_0_IDX); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 0x0000ff000000ff00); + tcg_wasm_out_op(s, OPC_I64_AND); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 8); + tcg_wasm_out_op(s, OPC_I64_SHL); + + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, TMP64_LOCAL_0_IDX); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 0x000000ff000000ff); + tcg_wasm_out_op(s, OPC_I64_AND); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 24); + tcg_wasm_out_op(s, OPC_I64_SHL); + + tcg_wasm_out_op(s, OPC_I64_OR); + + tcg_wasm_out_op(s, OPC_I64_OR); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(dest)); +} + +static void tcg_wasm_out_bswap32( + TCGContext *s, TCGReg dest, TCGReg src, int flags) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(src)); + tcg_wasm_out_op(s, OPC_I32_WRAP_I64); + tcg_wasm_out_op_idx(s, OPC_LOCAL_SET, TMP32_LOCAL_0_IDX); + + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, TMP32_LOCAL_0_IDX); + tcg_wasm_out_op_const(s, OPC_I32_CONST, 16); + tcg_wasm_out_op(s, OPC_I32_ROTR); + tcg_wasm_out_op_idx(s, OPC_LOCAL_SET, TMP32_LOCAL_0_IDX); + + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, TMP32_LOCAL_0_IDX); + tcg_wasm_out_op_const(s, OPC_I32_CONST, 0xff00ff00); + tcg_wasm_out_op(s, OPC_I32_AND); + tcg_wasm_out_op_const(s, OPC_I32_CONST, 8); + tcg_wasm_out_op(s, OPC_I32_SHR_U); + + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, TMP32_LOCAL_0_IDX); + tcg_wasm_out_op_const(s, OPC_I32_CONST, 0x00ff00ff); + tcg_wasm_out_op(s, OPC_I32_AND); + tcg_wasm_out_op_const(s, OPC_I32_CONST, 8); + tcg_wasm_out_op(s, OPC_I32_SHL); + + tcg_wasm_out_op(s, OPC_I32_OR); + if (flags & TCG_BSWAP_OS) { + tcg_wasm_out_op(s, OPC_I64_EXTEND_I32_S); + } else { + tcg_wasm_out_op(s, OPC_I64_EXTEND_I32_U); + } + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(dest)); +} + +static void tcg_wasm_out_bswap16( + TCGContext *s, TCGReg dest, TCGReg src, int flags) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(src)); + tcg_wasm_out_op(s, OPC_I32_WRAP_I64); + tcg_wasm_out_op_idx(s, OPC_LOCAL_SET, TMP32_LOCAL_0_IDX); + + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, TMP32_LOCAL_0_IDX); + tcg_wasm_out_op_const(s, OPC_I32_CONST, 8); + tcg_wasm_out_op(s, OPC_I32_ROTR); + tcg_wasm_out_op_idx(s, OPC_LOCAL_SET, TMP32_LOCAL_0_IDX); + + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, TMP32_LOCAL_0_IDX); + tcg_wasm_out_op_const(s, OPC_I32_CONST, 0x000000ff); + tcg_wasm_out_op(s, OPC_I32_AND); + + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, TMP32_LOCAL_0_IDX); + tcg_wasm_out_op_const(s, OPC_I32_CONST, 0xff000000); + tcg_wasm_out_op(s, OPC_I32_AND); + tcg_wasm_out_op_const(s, OPC_I32_CONST, 16); + if (flags & TCG_BSWAP_OS) { + tcg_wasm_out_op(s, OPC_I32_SHR_S); + } else { + tcg_wasm_out_op(s, OPC_I32_SHR_U); + } + + tcg_wasm_out_op(s, OPC_I32_OR); + tcg_wasm_out_op(s, OPC_I64_EXTEND_I32_U); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(dest)); +} + static void tcg_wasm_out_mov(TCGContext *s, TCGReg ret, TCGReg arg) { tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg));
@@ -1448,6 +1562,7 @@ static void tgen_bswap16(TCGContext *s, TCGType type, if (flags & TCG_BSWAP_OS) { tcg_tci_out_sextract(s, TCG_TYPE_REG, a0, a0, 0, 16); } + tcg_wasm_out_bswap16(s, a0, a1, flags); } static const TCGOutOpBswap outop_bswap16 = {
@@ -1462,6 +1577,7 @@ static void tgen_bswap32(TCGContext *s, TCGType type, if (flags & TCG_BSWAP_OS) { tcg_tci_out_sextract(s, TCG_TYPE_REG, a0, a0, 0, 32); } + tcg_wasm_out_bswap32(s, a0, a1, flags); } static const TCGOutOpBswap outop_bswap32 = {
@@ -1473,6 +1589,7 @@ static const TCGOutOpBswap outop_bswap32 = { static void tgen_bswap64(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1) { tcg_out_op_rr(s, INDEX_op_bswap64, a0, a1); + tcg_wasm_out_bswap64(s, a0, a1); } static const TCGOutOpUnary outop_bswap64 = {
Signature · 1 line
2.43.0
PATCH 6/35 tcg/wasm: Do not use TCI disassembler in Wasm backend
In reply to Kohei TokunagaThe Wasm backend should implement its own disassember for Wasm instructions.
--- tcg/wasm.c | 243 +---------------------------------------------------- 1 file changed, 1 insertion(+), 242 deletions(-)
tcg/wasm.c +1 -242
@@ -831,246 +831,5 @@ uintptr_t QEMU_DISABLE_CFI tcg_qemu_tb_exec(CPUArchState *env, }/* -* Disassembler that matches the interpreter +* TODO: Disassembler is not implemented */ - -static const char *str_r (TCGReg r) -{ -static const char regs[TCG_TARGET_NB_REGS ][4 ] = { -"r0" ,"r1" ,"r2" ,"r3" ,"r4" ,"r5" ,"r6" ,"r7" , -"r8" ,"r9" ,"r10" ,"r11" ,"r12" ,"r13" ,"env" ,"sp" - }; - -QEMU_BUILD_BUG_ON (TCG_AREG0 !=TCG_REG_R14 ); -QEMU_BUILD_BUG_ON (TCG_REG_CALL_STACK !=TCG_REG_R15 ); - -assert ((unsigned )r <TCG_TARGET_NB_REGS ); -return regs[r]; -} - -static const char *str_c (TCGCond c) -{ -static const char cond[16 ][8 ] = { - [TCG_COND_NEVER ] ="never" , - [TCG_COND_ALWAYS ] ="always" , - [TCG_COND_EQ ] ="eq" , - [TCG_COND_NE ] ="ne" , - [TCG_COND_LT ] ="lt" , - [TCG_COND_GE ] ="ge" , - [TCG_COND_LE ] ="le" , - [TCG_COND_GT ] ="gt" , - [TCG_COND_LTU ] ="ltu" , - [TCG_COND_GEU ] ="geu" , - [TCG_COND_LEU ] ="leu" , - [TCG_COND_GTU ] ="gtu" , - [TCG_COND_TSTEQ ] ="tsteq" , - [TCG_COND_TSTNE ] ="tstne" , - }; - -assert ((unsigned )c <ARRAY_SIZE (cond)); -assert (cond[c][0 ] !=0 ); -return cond[c]; -} - -/* Disassemble TCI bytecode. */ -int print_insn_tci (bfd_vma addr,disassemble_info *info) -{ -const uint32_t *tb_ptr = (const void *)(uintptr_t )addr; -const TCGOpDef *def; -const char *op_name; -uint32_t insn; -TCGOpcode op; -TCGReg r0, r1, r2, r3, r4; -tcg_target_ulong i1; -int32_t s2; -TCGCond c; -MemOpIdx oi; -uint8_t pos, len; -void *ptr; - -/* TCI is always the host, so we don't need to load indirect. */ - insn = *tb_ptr++; - - info->fprintf_func (info->stream,"%08x " , insn); - - op =extract32 (insn,0 ,8 ); - def = &tcg_op_defs[op]; - op_name = def->name; - -switch (op) { -case INDEX_op_br: -case INDEX_op_exit_tb: -case INDEX_op_goto_tb: -tci_args_l (insn, tb_ptr, &ptr); - info->fprintf_func (info->stream,"%-12s %p" , op_name, ptr); -break ; - -case INDEX_op_goto_ptr: -tci_args_r (insn, &r0); - info->fprintf_func (info->stream,"%-12s %s" , op_name,str_r (r0)); -break ; - -case INDEX_op_call: -tci_args_nl (insn, tb_ptr, &len, &ptr); - info->fprintf_func (info->stream,"%-12s %d, %p" , op_name, len, ptr); -break ; - -case INDEX_op_brcond: -tci_args_rl (insn, tb_ptr, &r0, &ptr); - info->fprintf_func (info->stream,"%-12s %s, 0, ne, %p" , - op_name,str_r (r0), ptr); -break ; - -case INDEX_op_setcond: -case INDEX_op_tci_setcond32: -tci_args_rrrc (insn, &r0, &r1, &r2, &c); - info->fprintf_func (info->stream,"%-12s %s, %s, %s, %s" , - op_name,str_r (r0),str_r (r1),str_r (r2),str_c (c)); -break ; - -case INDEX_op_tci_movi: -tci_args_ri (insn, &r0, &i1); - info->fprintf_func (info->stream,"%-12s %s, 0x%" TCG_PRIlx, - op_name,str_r (r0), i1); -break ; - -case INDEX_op_tci_movl: -tci_args_rl (insn, tb_ptr, &r0, &ptr); - info->fprintf_func (info->stream,"%-12s %s, %p" , - op_name,str_r (r0), ptr); -break ; - -case INDEX_op_tci_setcarry: - info->fprintf_func (info->stream,"%-12s" , op_name); -break ; - -case INDEX_op_ld8u: -case INDEX_op_ld8s: -case INDEX_op_ld16u: -case INDEX_op_ld16s: -case INDEX_op_ld32u: -case INDEX_op_ld: -case INDEX_op_st8: -case INDEX_op_st16: -case INDEX_op_st32: -case INDEX_op_st: -tci_args_rrs (insn, &r0, &r1, &s2); - info->fprintf_func (info->stream,"%-12s %s, %s, %d" , - op_name,str_r (r0),str_r (r1), s2); -break ; - -case INDEX_op_bswap16: -case INDEX_op_bswap32: -case INDEX_op_ctpop: -case INDEX_op_mov: -case INDEX_op_neg: -case INDEX_op_not: -case INDEX_op_ext_i32_i64: -case INDEX_op_extu_i32_i64: -case INDEX_op_bswap64: -tci_args_rr (insn, &r0, &r1); - info->fprintf_func (info->stream,"%-12s %s, %s" , - op_name,str_r (r0),str_r (r1)); -break ; - -case INDEX_op_add: -case INDEX_op_addci: -case INDEX_op_addcio: -case INDEX_op_addco: -case INDEX_op_and: -case INDEX_op_andc: -case INDEX_op_clz: -case INDEX_op_ctz: -case INDEX_op_divs: -case INDEX_op_divu: -case INDEX_op_eqv: -case INDEX_op_mul: -case INDEX_op_nand: -case INDEX_op_nor: -case INDEX_op_or: -case INDEX_op_orc: -case INDEX_op_rems: -case INDEX_op_remu: -case INDEX_op_rotl: -case INDEX_op_rotr: -case INDEX_op_sar: -case INDEX_op_shl: -case INDEX_op_shr: -case INDEX_op_sub: -case INDEX_op_subbi: -case INDEX_op_subbio: -case INDEX_op_subbo: -case INDEX_op_xor: -case INDEX_op_tci_ctz32: -case INDEX_op_tci_clz32: -case INDEX_op_tci_divs32: -case INDEX_op_tci_divu32: -case INDEX_op_tci_rems32: -case INDEX_op_tci_remu32: -case INDEX_op_tci_rotl32: -case INDEX_op_tci_rotr32: -tci_args_rrr (insn, &r0, &r1, &r2); - info->fprintf_func (info->stream,"%-12s %s, %s, %s" , - op_name,str_r (r0),str_r (r1),str_r (r2)); -break ; - -case INDEX_op_deposit: -tci_args_rrrbb (insn, &r0, &r1, &r2, &pos, &len); - info->fprintf_func (info->stream,"%-12s %s, %s, %s, %d, %d" , - op_name,str_r (r0),str_r (r1),str_r (r2), pos, len); -break ; - -case INDEX_op_extract: -case INDEX_op_sextract: -tci_args_rrbb (insn, &r0, &r1, &pos, &len); - info->fprintf_func (info->stream,"%-12s %s,%s,%d,%d" , - op_name,str_r (r0),str_r (r1), pos, len); -break ; - -case INDEX_op_tci_movcond32: -case INDEX_op_movcond: -case INDEX_op_setcond2_i32: -tci_args_rrrrrc (insn, &r0, &r1, &r2, &r3, &r4, &c); - info->fprintf_func (info->stream,"%-12s %s, %s, %s, %s, %s, %s" , - op_name,str_r (r0),str_r (r1),str_r (r2), -str_r (r3),str_r (r4),str_c (c)); -break ; - -case INDEX_op_muls2: -case INDEX_op_mulu2: -tci_args_rrrr (insn, &r0, &r1, &r2, &r3); - info->fprintf_func (info->stream,"%-12s %s, %s, %s, %s" , - op_name,str_r (r0),str_r (r1), -str_r (r2),str_r (r3)); -break ; - -case INDEX_op_qemu_ld: -case INDEX_op_qemu_st: -tci_args_rrm (insn, &r0, &r1, &oi); - info->fprintf_func (info->stream,"%-12s %s, %s, %x" , - op_name,str_r (r0),str_r (r1), oi); -break ; - -case INDEX_op_qemu_ld2: -case INDEX_op_qemu_st2: -tci_args_rrrr (insn, &r0, &r1, &r2, &r3); - info->fprintf_func (info->stream,"%-12s %s, %s, %s, %s" , - op_name,str_r (r0),str_r (r1), -str_r (r2),str_r (r3)); -break ; - -case 0 : -/* tcg_out_nop_fill uses zeros */ -if (insn ==0 ) { - info->fprintf_func (info->stream,"align" ); -break ; - } -/* fall through */ - -default : - info->fprintf_func (info->stream,"illegal opcode %d" , op); -break ; - } - -return sizeof (insn); -}
Signature · 1 line
2.43.0
PATCH 8/35 meson: Enable to build wasm backend
In reply to Kohei TokunagaEnable to use tcg/wasm as the TCG backend for the WebAssembly (wasm64) build.
--- include/accel/tcg/getpc.h | 2 +- include/tcg/helper-info.h | 4 ++-- include/tcg/tcg.h | 2 +- meson.build | 4 +++- tcg/meson.build | 5 +++++ tcg/region.c | 10 +++++----- tcg/tcg.c | 16 ++++++++-------- 7 files changed, 25 insertions(+), 18 deletions(-)
include/accel/tcg/getpc.h +1 -1
@@ -9,7 +9,7 @@#define ACCEL_TCG_GETPC_H /* GETPC is the true target of the return instruction that we'll execute. */ -#ifdef CONFIG_TCG_INTERPRETER +#if defined(CONFIG_TCG_INTERPRETER ) || defined(EMSCRIPTEN )extern __threaduintptr_t tci_tb_ptr;# define GETPC () tci_tb_ptr#else
include/tcg/helper-info.h +2 -2
@@ -9,7 +9,7 @@#ifndef TCG_HELPER_INFO_H #define TCG_HELPER_INFO_H -#ifdef CONFIG_TCG_INTERPRETER +#if defined(CONFIG_TCG_INTERPRETER ) || defined(EMSCRIPTEN )#include <ffi.h> #endif #include "tcg-target-reg-bits.h"
@@ -48,7 +48,7 @@ struct TCGHelperInfo {const char *name;/* Used with g_once_init_enter. */ -#ifdef CONFIG_TCG_INTERPRETER +#if defined(CONFIG_TCG_INTERPRETER ) || defined(EMSCRIPTEN )ffi_cif *cif;#else uintptr_t init;
include/tcg/tcg.h +1 -1
@@ -963,7 +963,7 @@ static inline size_t tcg_current_code_size(TCGContext *s)#define TB_EXIT_IDXMAX 1#define TB_EXIT_REQUESTED 3 -#ifdef CONFIG_TCG_INTERPRETER +#if defined(CONFIG_TCG_INTERPRETER ) || defined(EMSCRIPTEN )uintptr_t tcg_qemu_tb_exec (CPUArchState *env,const void *tb_ptr);#else typedef uintptr_t tcg_prologue_fn (CPUArchState *env,const void *tb_ptr);
meson.build +3 -1
@@ -916,7 +916,7 @@ if have_tcg if not get_option('tcg_interpreter') error('Unsupported CPU @0@, try --enable-tcg-interpreter'.format(cpu)) endif - elif host_arch == 'wasm32' or host_arch == 'wasm64' + elif host_arch == 'wasm32' if not get_option('tcg_interpreter') error('WebAssembly host requires --enable-tcg-interpreter') endif
@@ -934,6 +934,8 @@ if have_tcg tcg_arch = 'i386' elif host_arch == 'ppc64' tcg_arch = 'ppc' + elif host_arch == 'wasm64' + tcg_arch = 'wasm' endif add_project_arguments('-iquote', meson.current_source_dir() / 'tcg' / tcg_arch, language: all_languages)
tcg/meson.build +5 -0
@@ -20,6 +20,11 @@ if get_option('tcg_interpreter') method: 'pkg-config') tcg_ss.add(libffi) tcg_ss.add(files('tci.c')) +elif host_os == 'emscripten' + libffi = dependency('libffi', version: '>=3.0', required: true, + method: 'pkg-config') + specific_ss.add(libffi) + specific_ss.add(files('wasm.c')) endif tcg_ss.add(when: libdw, if_true: files('debuginfo.c'))
tcg/region.c +5 -5
@@ -94,7 +94,7 @@ bool in_code_gen_buffer(const void *p)return (size_t )(p - region.start_aligned) <= region.total_size; } -#ifndef CONFIG_TCG_INTERPRETER +#if !defined(CONFIG_TCG_INTERPRETER ) && !defined(EMSCRIPTEN )static int host_prot_read_exec (void ) {#if defined(CONFIG_LINUX ) && defined(HOST_AARCH64 ) && defined(PROT_BTI )
@@ -569,7 +569,7 @@ static int alloc_code_gen_buffer_anon(size_t size, int prot,return prot; } -#ifndef CONFIG_TCG_INTERPRETER +#if !defined(CONFIG_TCG_INTERPRETER ) && !defined(EMSCRIPTEN )#ifdef CONFIG_POSIX #include "qemu/memfd.h"
@@ -667,11 +667,11 @@ static int alloc_code_gen_buffer_splitwx_vmremap(size_t size, Error **errp)return PROT_READ |PROT_WRITE ; }#endif /* CONFIG_DARWIN */ -#endif /* CONFIG_TCG_INTERPRETER */ +#endif /* !CONFIG_TCG_INTERPRETER && !EMSCRIPTEN */ static int alloc_code_gen_buffer_splitwx (size_t size,Error **errp) { -#ifndef CONFIG_TCG_INTERPRETER +#if !defined(CONFIG_TCG_INTERPRETER ) && !defined(EMSCRIPTEN )# ifdef CONFIG_DARWIN return alloc_code_gen_buffer_splitwx_vmremap (size, errp);# endif
@@ -813,7 +813,7 @@ void tcg_region_init(size_t tb_size, int splitwx, unsigned max_threads)* Work with the page protections set up with the initial mapping. */ need_prot =PROT_READ |PROT_WRITE ; -#ifndef CONFIG_TCG_INTERPRETER +#if !defined(CONFIG_TCG_INTERPRETER ) && !defined(EMSCRIPTEN )if (tcg_splitwx_diff ==0 ) { need_prot |=host_prot_read_exec (); }
tcg/tcg.c +8 -8
@@ -254,7 +254,7 @@ TCGv_env tcg_env;const void *tcg_code_gen_epilogue;uintptr_t tcg_splitwx_diff; -#ifndef CONFIG_TCG_INTERPRETER +#if !defined(CONFIG_TCG_INTERPRETER ) && !defined(EMSCRIPTEN )tcg_prologue_fn *tcg_qemu_tb_exec;#endif
@@ -1118,7 +1118,7 @@ typedef struct TCGOutOpSubtract {#include "tcg-target.c.inc" -#ifndef CONFIG_TCG_INTERPRETER +#if !defined(CONFIG_TCG_INTERPRETER ) && !defined(EMSCRIPTEN )/* Validate CPUTLBDescFast placement. */ QEMU_BUILD_BUG_ON((int)(offsetof(CPUNegativeOffsetState, tlb.f[0]) -sizeof (CPUNegativeOffsetState))
@@ -1440,7 +1440,7 @@ static TCGHelperInfo info_helper_st128_mmu = { | dh_typemask(ptr, 5) /* uintptr_t ra */ }; -#ifdef CONFIG_TCG_INTERPRETER +#if defined(CONFIG_TCG_INTERPRETER ) || defined(EMSCRIPTEN )static ffi_type *typecode_to_ffi (int argmask) {/*
@@ -1517,7 +1517,7 @@ static ffi_cif *init_ffi_layout(TCGHelperInfo *info)#else #define HELPER_INFO_INIT (I ) (&(I)->init)#define HELPER_INFO_INIT_VAL (I ) 1 -#endif /* CONFIG_TCG_INTERPRETER */ +#endif /* CONFIG_TCG_INTERPRETER || EMSCRIPTEN */ static inline bool arg_slot_reg_p (unsigned arg_slot) {
@@ -1894,7 +1894,7 @@ void tcg_prologue_init(void) s->code_buf = s->code_gen_ptr; s->data_gen_ptr =NULL ; -#ifndef CONFIG_TCG_INTERPRETER +#if !defined(CONFIG_TCG_INTERPRETER ) && !defined(EMSCRIPTEN ) tcg_qemu_tb_exec = (tcg_prologue_fn *)tcg_splitwx_to_rx (s->code_ptr);#endif
@@ -1913,7 +1913,7 @@ void tcg_prologue_init(void) prologue_size =tcg_current_code_size (s);perf_report_prologue (s->code_gen_ptr, prologue_size); -#ifndef CONFIG_TCG_INTERPRETER +#if !defined(CONFIG_TCG_INTERPRETER ) && !defined(EMSCRIPTEN )flush_idcache_range ((uintptr_t )tcg_splitwx_to_rx (s->code_buf), (uintptr_t )s->code_buf, prologue_size);#endif
@@ -1950,7 +1950,7 @@ void tcg_prologue_init(void) } } -#ifndef CONFIG_TCG_INTERPRETER +#if !defined(CONFIG_TCG_INTERPRETER ) && !defined(EMSCRIPTEN )/* * Assert that goto_ptr is implemented completely, setting an epilogue. * For tci, we use NULL as the signal to return from the interpreter,
@@ -7048,7 +7048,7 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb, uint64_t pc_start)return -2 ; } -#ifndef CONFIG_TCG_INTERPRETER +#if !defined(CONFIG_TCG_INTERPRETER ) && !defined(EMSCRIPTEN )/* flush instruction cache */ flush_idcache_range((uintptr_t)tcg_splitwx_to_rx(s->code_buf), (uintptr_t)s->code_buf,
Signature · 1 line
2.43.0
PATCH 20/35 tcg/wasm: Add andc/orc/eqv/nand/nor instructions
In reply to Kohei TokunagaThis commit implements andc, orc, eqv, nand and nor operations using Wasm instructions.
--- tcg/wasm/tcg-target.c.inc | 55 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+)
tcg/wasm/tcg-target.c.inc +55 -0
@@ -449,6 +449,56 @@ static void tcg_wasm_out_cond( } } +static void tcg_wasm_out_andc( + TCGContext *s, TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg2)); + tcg_wasm_out_op_not(s); + tcg_wasm_out_op(s, OPC_I64_AND); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); +} + +static void tcg_wasm_out_orc( + TCGContext *s, TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg2)); + tcg_wasm_out_op_not(s); + tcg_wasm_out_op(s, OPC_I64_OR); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); +} + +static void tcg_wasm_out_eqv( + TCGContext *s, TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg2)); + tcg_wasm_out_op(s, OPC_I64_XOR); + tcg_wasm_out_op_not(s); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); +} + +static void tcg_wasm_out_nand( + TCGContext *s, TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg2)); + tcg_wasm_out_op(s, OPC_I64_AND); + tcg_wasm_out_op_not(s); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); +} + +static void tcg_wasm_out_nor( + TCGContext *s, TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg2)); + tcg_wasm_out_op(s, OPC_I64_OR); + tcg_wasm_out_op_not(s); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); +} + static void tcg_wasm_out_setcond(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1, TCGReg arg2, TCGCond cond) {
@@ -1177,6 +1227,7 @@ static void tgen_andc(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { tcg_out_op_rrr(s, INDEX_op_andc, a0, a1, a2); + tcg_wasm_out_andc(s, a0, a1, a2); } static const TCGOutOpBinary outop_andc = {
@@ -1266,6 +1317,7 @@ static void tgen_eqv(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { tcg_out_op_rrr(s, INDEX_op_eqv, a0, a1, a2); + tcg_wasm_out_eqv(s, a0, a1, a2); } static const TCGOutOpBinary outop_eqv = {
@@ -1339,6 +1391,7 @@ static void tgen_nand(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { tcg_out_op_rrr(s, INDEX_op_nand, a0, a1, a2); + tcg_wasm_out_nand(s, a0, a1, a2); } static const TCGOutOpBinary outop_nand = {
@@ -1350,6 +1403,7 @@ static void tgen_nor(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { tcg_out_op_rrr(s, INDEX_op_nor, a0, a1, a2); + tcg_wasm_out_nor(s, a0, a1, a2); } static const TCGOutOpBinary outop_nor = {
@@ -1373,6 +1427,7 @@ static void tgen_orc(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { tcg_out_op_rrr(s, INDEX_op_orc, a0, a1, a2); + tcg_wasm_out_orc(s, a0, a1, a2); } static const TCGOutOpBinary outop_orc = {
Signature · 1 line
2.43.0
PATCH 23/35 tcg/wasm: Add br/brcond instructions
In reply to Kohei TokunagaWasm does not support direct jumps to arbitrary code addresses, so br and brcond are implemented using Wasm's control flow instructions.
As illustrated in the pseudo-code below, each TB wraps instructions inside a large loop. Each set of codes separated by TCG labels is placed inside an "if" block. Br is implemented by breaking out of the current block and entering the target block:
loop
if
... code after the first label
end
if
... code after the second label
end
...
end
Each block is assigned an unique integer ID. The br implementation sets the destination block's ID in BLOCK_IDX Wasm variable and breaks from the current if block. As control flow continues, each if block checks whether the BLOCK_IDX matches its own ID. If so, execution resumes within that block.
The tcg_out_tb_start function generates the start of the global loop and the first if block. To properly close these blocks, this commit also introduces a new callback tcg_out_tb_end which emits the "end" instructions for the final if block and the loop.
Another new callback tcg_out_label_cb is used to emit block boundaries, specifically the end of the previous block and the if of the next block, at label positions. It also records the mapping between label IDs and block IDs in a LabelInfo list.
Since the block ID for a label might not be known when a br instruction is generated, a placeholder is emitted instead. These placeholders are tracked in a BlockPlaceholder list and resolved later using LabelInfo.
--- tcg/aarch64/tcg-target.c.inc | 11 +++ tcg/arm/tcg-target.c.inc | 11 +++ tcg/i386/tcg-target.c.inc | 11 +++ tcg/loongarch64/tcg-target.c.inc | 11 +++ tcg/mips/tcg-target.c.inc | 11 +++ tcg/ppc/tcg-target.c.inc | 11 +++ tcg/riscv/tcg-target.c.inc | 11 +++ tcg/s390x/tcg-target.c.inc | 11 +++ tcg/sparc64/tcg-target.c.inc | 11 +++ tcg/tcg.c | 7 ++ tcg/tci/tcg-target.c.inc | 11 +++ tcg/wasm/tcg-target.c.inc | 161 +++++++++++++++++++++++++++++++ 12 files changed, 278 insertions(+)
tcg/aarch64/tcg-target.c.inc +11 -0
@@ -3514,6 +3514,17 @@ static void tcg_out_tb_start(TCGContext *s) tcg_out_bti(s, BTI_J); } +static int tcg_out_tb_end(TCGContext *s) +{ + /* nothing to do */ + return 0; +} + +static void tcg_out_label_cb(TCGContext *s, TCGLabel *l) +{ + /* nothing to do */ +} + static void tcg_out_nop_fill(tcg_insn_unit *p, int count) { int i;
tcg/arm/tcg-target.c.inc +11 -0
@@ -3441,6 +3441,17 @@ static void tcg_out_tb_start(TCGContext *s) /* nothing to do */ } +static int tcg_out_tb_end(TCGContext *s) +{ + /* nothing to do */ + return 0; +} + +static void tcg_out_label_cb(TCGContext *s, TCGLabel *l) +{ + /* nothing to do */ +} + typedef struct { DebugFrameHeader h; uint8_t fde_def_cfa[4];
tcg/i386/tcg-target.c.inc +11 -0
@@ -4759,6 +4759,17 @@ static void tcg_out_tb_start(TCGContext *s) /* nothing to do */ } +static int tcg_out_tb_end(TCGContext *s) +{ + /* nothing to do */ + return 0; +} + +static void tcg_out_label_cb(TCGContext *s, TCGLabel *l) +{ + /* nothing to do */ +} + static void tcg_out_nop_fill(tcg_insn_unit *p, int count) { memset(p, 0x90, count);
tcg/loongarch64/tcg-target.c.inc +11 -0
@@ -2658,6 +2658,17 @@ static void tcg_out_tb_start(TCGContext *s) /* nothing to do */ } +static int tcg_out_tb_end(TCGContext *s) +{ + /* nothing to do */ + return 0; +} + +static void tcg_out_label_cb(TCGContext *s, TCGLabel *l) +{ + /* nothing to do */ +} + static void tcg_out_nop_fill(tcg_insn_unit *p, int count) { for (int i = 0; i < count; ++i) {
tcg/mips/tcg-target.c.inc +11 -0
@@ -2745,6 +2745,17 @@ static void tcg_out_tb_start(TCGContext *s) /* nothing to do */ } +static int tcg_out_tb_end(TCGContext *s) +{ + /* nothing to do */ + return 0; +} + +static void tcg_out_label_cb(TCGContext *s, TCGLabel *l) +{ + /* nothing to do */ +} + static void tcg_target_init(TCGContext *s) { tcg_target_detect_isa();
tcg/ppc/tcg-target.c.inc +11 -0
@@ -2859,6 +2859,17 @@ static void tcg_out_tb_start(TCGContext *s) } } +static int tcg_out_tb_end(TCGContext *s) +{ + /* nothing to do */ + return 0; +} + +static void tcg_out_label_cb(TCGContext *s, TCGLabel *l) +{ + /* nothing to do */ +} + static void tcg_out_exit_tb(TCGContext *s, uintptr_t arg) { tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R3, arg);
tcg/riscv/tcg-target.c.inc +11 -0
@@ -2983,6 +2983,17 @@ static void tcg_out_tb_start(TCGContext *s) init_setting_vtype(s); } +static int tcg_out_tb_end(TCGContext *s) +{ + /* nothing to do */ + return 0; +} + +static void tcg_out_label_cb(TCGContext *s, TCGLabel *l) +{ + /* nothing to do */ +} + static bool vtype_check(unsigned vtype) { unsigned long tmp;
tcg/s390x/tcg-target.c.inc +11 -0
@@ -3830,6 +3830,17 @@ static void tcg_out_tb_start(TCGContext *s) /* nothing to do */ } +static int tcg_out_tb_end(TCGContext *s) +{ + /* nothing to do */ + return 0; +} + +static void tcg_out_label_cb(TCGContext *s, TCGLabel *l) +{ + /* nothing to do */ +} + static void tcg_out_nop_fill(tcg_insn_unit *p, int count) { memset(p, 0x07, count * sizeof(tcg_insn_unit));
tcg/sparc64/tcg-target.c.inc +11 -0
@@ -1017,6 +1017,17 @@ static void tcg_out_tb_start(TCGContext *s) /* nothing to do */ } +static int tcg_out_tb_end(TCGContext *s) +{ + /* nothing to do */ + return 0; +} + +static void tcg_out_label_cb(TCGContext *s, TCGLabel *l) +{ + /* nothing to do */ +} + static void tcg_out_nop_fill(tcg_insn_unit *p, int count) { int i;
tcg/tcg.c +7 -0
@@ -116,6 +116,7 @@ static void tcg_register_jit_int(const void *buf, size_t size,/* Forward declarations for functions declared and used in tcg-target.c.inc. */ static void tcg_out_tb_start (TCGContext *s); +static int tcg_out_tb_end (TCGContext *s);static void tcg_out_ld (TCGContext *s,TCGType type,TCGReg ret,TCGReg arg1,intptr_t arg2);static bool tcg_out_mov (TCGContext *s,TCGType type,TCGReg ret,TCGReg arg);
@@ -187,6 +188,7 @@ static void tcg_out_call(TCGContext *s, const tcg_insn_unit *target,static TCGReg tcg_target_call_oarg_reg (TCGCallReturnKind kind,int slot);static bool tcg_target_const_match (int64_t val,int ct,TCGType type,TCGCond cond,int vece); +static void tcg_out_label_cb (TCGContext *s,TCGLabel *l);#ifndef CONFIG_USER_ONLY #define guest_base ({ qemu_build_not_reached(); (uintptr_t)0; })
@@ -361,6 +363,7 @@ static void tcg_out_label(TCGContext *s, TCGLabel *l)tcg_debug_assert (!l->has_value); l->has_value =1 ; l->u.value_ptr =tcg_splitwx_to_rx (s->code_ptr); +tcg_out_label_cb (s, l); }TCGLabel *gen_new_label (void )
@@ -7047,6 +7050,10 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb, uint64_t pc_start)if (!tcg_resolve_relocs (s)) {return -2 ; } + i =tcg_out_tb_end (s); +if (i <0 ) { +return i; + }#if !defined(CONFIG_TCG_INTERPRETER ) && !defined(EMSCRIPTEN )/* flush instruction cache */
tcg/tci/tcg-target.c.inc +11 -0
@@ -1301,6 +1301,17 @@ static void tcg_out_tb_start(TCGContext *s) /* nothing to do */ } +static int tcg_out_tb_end(TCGContext *s) +{ + /* nothing to do */ + return 0; +} + +static void tcg_out_label_cb(TCGContext *s, TCGLabel *l) +{ + /* nothing to do */ +} + bool tcg_target_has_memory_bswap(MemOp memop) { return true;
tcg/wasm/tcg-target.c.inc +161 -0
211 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH 32/35 tcg/wasm: Enable instantiation of TBs executed many times
In reply to Kohei TokunagaThis commit enables instantiations of TBs in wasm.c. Browsers cause out of memory error if too many Wasm instances are created so the number of instances needs to be limited. So this commit restricts instantiation only for TBs that are called many times.
This commit adds a counter (or its array if there are multiple threads) to the TB. Each time a TB is executed on TCI, the counter on TB is incremented. If it reaches to a threshold, that TB is instantiated as Wasm via instantiate_wasm.
The total number of instances are tracked by the instances_global variable and its maximum number is limited by MAX_INSTANCES. When a Wasm module is instantiated, instances_global is incremented and the instance's function pointer is recorded to an array of WasmInstanceInfo.
Each TB refers to the WasmInstanceInfo entry via WasmTBHeader's info_ptr (or its array if there are multiple threads). This allows tcg_qemu_tb_exec to resolve the instance's function pointer from the TB.
When a new instantiation would exceed the limit, the Wasm backend doesn't perform instantiation (i.e. TB continues execution on TCI). Instead, it triggers the removal of older Wasm instances using Emscripten's removeFunction function. Once the removal is completed and detected via FinalizationRegistry API[1], instances_global is decremented, allowing new modules to be instantiated.
--- tcg/wasm.c | 245 +++++++++++++++++++++++++++++++++++++- tcg/wasm.h | 45 +++++++ tcg/wasm/tcg-target.c.inc | 18 +++ 3 files changed, 304 insertions(+), 4 deletions(-)
tcg/wasm.c +241 -4
@@ -26,6 +26,7 @@#include "tcg-has.h" #include <ffi.h> #include <emscripten.h> +#include "wasm.h" #define ctpop_tr glue(ctpop, TCG_TARGET_REG_BITS)
@@ -45,6 +46,9 @@ __threaduintptr_t tci_tb_ptr; +/* TBs executed more than this value will be compiled to wasm */ +#define INSTANTIATE_NUM 1500 + #define EM_JS_PRE(ret, name, args, body...) EM_JS(ret, name, args, body)#define DEC_PTR (p) bigintToI53Checked(p)
@@ -81,6 +85,8 @@ EM_JS_PRE(void*, instantiate_wasm, (void *wasm_begin, "helper" : helper, }); + Module.__wasm_tb.inst_gc_registry.register (inst,"tbinstance" ); +return ENC_PTR (addFunction (inst.exports.start,'ii' )); });
@@ -366,16 +372,59 @@ static void tci_qemu_st(CPUArchState *env, uint64_t taddr, uint64_t val, } } +static __threadint thread_idx; + +static inline int32_t get_counter_local (void *tb_ptr) +{ +return get_counter (tb_ptr, thread_idx); +} + +static inline void set_counter_local (void *tb_ptr,int v) +{ +set_counter (tb_ptr, thread_idx, v); +} + +static inline struct WasmInstanceInfo *get_info_local (void *tb_ptr) +{ +return get_info (tb_ptr, thread_idx); +} + +static inline void set_info_local (void *tb_ptr,struct WasmInstanceInfo *info) +{ +set_info (tb_ptr, thread_idx, info); +} + +/* +* inc_counter increments the execution counter in the specified TB. +* If the counter reaches the limit, it returns true otherwise returns false. +*/ +static inline bool inc_counter (void *tb_ptr) +{ +int32_t counter =get_counter_local (tb_ptr); +if ((counter >=0 ) && (counter <INSTANTIATE_NUM )) { +set_counter_local (tb_ptr, counter +1 ); + }else { +return true;/* enter to wasm TB */ + } +return false; +} + +static __threadstruct WasmContext ctx = { + .tb_ptr =0 , + .stack =NULL , + .do_init =1 , + .buf128 =NULL , +}; +/* Interpret pseudo code in tb. */ /* * Disable CFI checks. * One possible operation in the pseudo code is a call to binary code. * Therefore, disable CFI checks in the interpreter function */ -uintptr_t QEMU_DISABLE_CFI tcg_qemu_tb_exec(CPUArchState *env, -const void *v_tb_ptr) +static uintptr_t QEMU_DISABLE_CFI tcg_qemu_tb_exec_tci(CPUArchState *env) { -const uint32_t *tb_ptr = v_tb_ptr; +uint32_t *tb_ptr =get_tci_ptr (ctx.tb_ptr);tcg_target_ulong regs[TCG_TARGET_NB_REGS ];uint64_t stack[(TCG_STATIC_CALL_ARGS_SIZE +TCG_STATIC_FRAME_SIZE ) /sizeof (uint64_t )];
@@ -814,20 +863,34 @@ uintptr_t QEMU_DISABLE_CFI tcg_qemu_tb_exec(CPUArchState *env,case INDEX_op_exit_tb:tci_args_l (insn, tb_ptr, &ptr); + ctx.tb_ptr =0 ;return (uintptr_t )ptr;case INDEX_op_goto_tb:tci_args_l (insn, tb_ptr, &ptr); - tb_ptr = *(void **)ptr; +if (tb_ptr != *(void **)ptr) { + tb_ptr = *(void **)ptr; + ctx.tb_ptr = tb_ptr; +if (inc_counter (tb_ptr)) { +return 0 ;/* enter to wasm TB */ + } + tb_ptr =get_tci_ptr (tb_ptr); + }break ;case INDEX_op_goto_ptr:tci_args_r (insn, &r0); ptr = (void *)regs[r0];if (!ptr) { + ctx.tb_ptr =0 ;return 0 ; } tb_ptr = ptr; + ctx.tb_ptr = tb_ptr; +if (inc_counter (tb_ptr)) { +return 0 ;/* enter to wasm TB */ + } + tb_ptr =get_tci_ptr (tb_ptr);break ;case INDEX_op_qemu_ld:
@@ -873,3 +936,177 @@ uintptr_t QEMU_DISABLE_CFI tcg_qemu_tb_exec(CPUArchState *env,/* * TODO: Disassembler is not implemented */ + +/* +* The maximum number of instances that can exist simultaneously +* +* If this limit is reached and a new instance is required, older instances are +* removed to allow creation of new ones without exceeding the browser's limit. +*/ +#define MAX_INSTANCES 15000 + +static int instances_global; + +/* Avoid overwrapping of begin/end pointers */ +#define INSTANCES_BUF_MAX (MAX_INSTANCES + 1) + +static __threadstruct WasmInstanceInfo instances[INSTANCES_BUF_MAX ]; +static __threadint instances_begin; +static __threadint instances_end; + +static void add_instance (wasm_tb_func tb_func,void *tb_ptr) +{ + instances[instances_end].tb_func = tb_func; + instances[instances_end].tb_ptr = tb_ptr; +set_info_local (tb_ptr, &(instances[instances_end])); + instances_end = (instances_end +1 ) %INSTANCES_BUF_MAX ; + +qatomic_inc (&instances_global); +} + +static __threadint instance_pending_gc; +static __threadint instance_done_gc; + +static void remove_old_instances (void ) +{ +int num; +if (instance_pending_gc >0 ) { +return ; + } +if (instances_begin <= instances_end) { + num = instances_end - instances_begin; + }else { + num = instances_end + (INSTANCES_BUF_MAX - instances_begin); + } +/* removes the half of the oldest instances in the buffer */ + num /=2 ; +for (int i =0 ; i < num; i++) { +EM_ASM ({removeFunction ($0); }, instances[instances_begin].tb_func); + instances[instances_begin].tb_ptr =NULL ; + instances_begin = (instances_begin +1 ) %INSTANCES_BUF_MAX ; + } + instance_pending_gc += num; +} + +static bool can_add_instance (void ) +{ +return qatomic_read (&instances_global) <MAX_INSTANCES ; +} + +static wasm_tb_func get_instance_from_tb (void *tb_ptr) +{ +struct WasmInstanceInfo *elm =get_info_local (tb_ptr); +if (elm ==NULL ) { +return NULL ; + } +if (elm->tb_ptr != tb_ptr) { +/* +* This TB was instantiated before, but has been removed. Set counter to +* the max value so that this will be instantiated. +*/ +set_counter_local (tb_ptr,INSTANTIATE_NUM ); +set_info_local (tb_ptr,NULL ); +return NULL ; + } +return elm->tb_func; +} + +static void check_gc_completion (void ) +{ +if (instance_done_gc >0 ) { +qatomic_sub (&instances_global, instance_done_gc); + instance_pending_gc -= instance_done_gc; + instance_done_gc =0 ; + } +} + +EM_JS_PRE(void, init_wasm_js, (void *instance_done_gc), +{ + Module.__wasm_tb = { + inst_gc_registry: new FinalizationRegistry((i) => { + if (i == "tbinstance") { + const memory_v = new DataView(HEAP8.buffer); + let v = memory_v.getInt32(instance_done_gc, true); + memory_v.setInt32(instance_done_gc, v + 1, true); + } + }) + }; +}); + +#define MAX_EXEC_NUM 50000 +static __thread int exec_cnt = MAX_EXEC_NUM; +static inline void trysleep(void) +{ + /* + * Even during running TBs continuously, try to return the control + * to the browser periodically and allow browsers doing tasks. + */ + if (--exec_cnt == 0) { + if (!can_add_instance()) { + emscripten_sleep(0); + check_gc_completion(); + } + exec_cnt = MAX_EXEC_NUM; + } +} + +static int thread_idx_max; + +static void init_wasm(void) +{ + thread_idx = qatomic_fetch_inc(&thread_idx_max); + ctx.stack = g_malloc(TCG_STATIC_CALL_ARGS_SIZE + TCG_STATIC_FRAME_SIZE); + ctx.buf128 = g_malloc(16); + ctx.tci_tb_ptr = (uint32_t *)&tci_tb_ptr; + init_wasm_js(&instance_done_gc); +} + +static __thread bool initdone; + +uintptr_t tcg_qemu_tb_exec(CPUArchState *env, const void *v_tb_ptr) +{ + if (!initdone) { + init_wasm(); + initdone = true; + } + ctx.env = env; + ctx.tb_ptr = (void *)v_tb_ptr; + while (true) { + trysleep(); + uintptr_t res; + wasm_tb_func tb_func = get_instance_from_tb(ctx.tb_ptr); + if (tb_func) { + /* + * Call the Wasm instance + */ + res = call_wasm_tb(tb_func, &ctx); + } else if (!inc_counter(ctx.tb_ptr)) { + /* + * Run it on TCI because the counter value is small + */ + res = tcg_qemu_tb_exec_tci(env); + } else if (!can_add_instance()) { + /* + * Too many instances has been created, try removing older + * instances and keep running this TB on TCI + */ + remove_old_instances(); + check_gc_completion(); + res = tcg_qemu_tb_exec_tci(env); + } else { + /* + * Instantiate and run the Wasm module + */ + struct WasmTBHeader *header = (struct WasmTBHeader *)ctx.tb_ptr; + tb_func = (wasm_tb_func)instantiate_wasm(header->wasm_ptr, + header->wasm_size, + header->import_ptr, + header->import_size); + add_instance(tb_func, ctx.tb_ptr); + res = call_wasm_tb(tb_func, &ctx); + } + if (!ctx.tb_ptr) { + return res; + } + } +}
tcg/wasm.h +45 -0
57 lines not on this page · raw message
tcg/wasm/tcg-target.c.inc +18 -0
@@ -28,6 +28,11 @@ #include "qemu/queue.h" #include "../wasm.h" +/* + * This is included to get the number of threads via tcg_max_ctxs. + */ +#include "../tcg-internal.h" + /* Used for function call generation. */ #define TCG_TARGET_CALL_STACK_OFFSET 0 #define TCG_TARGET_STACK_ALIGN 8
@@ -2789,6 +2794,7 @@ static int write_mod_code(TCGContext *s) static void tcg_out_tb_start(TCGContext *s) { + int size; struct WasmTBHeader *h; intptr_t ofs;
@@ -2803,6 +2809,18 @@ static void tcg_out_tb_start(TCGContext *s) h = (struct WasmTBHeader *)(s->code_ptr); s->code_ptr += sizeof(struct WasmTBHeader); + /* locate counters */ + h->counter_ptr = (int32_t *)s->code_ptr; + size = tcg_max_ctxs * sizeof(int32_t); + memset(s->code_ptr, 0, size); + s->code_ptr += size; + + /* locate the instance information */ + h->info_ptr = (struct WasmInstanceInfo **)s->code_ptr; + size = tcg_max_ctxs * sizeof(void *); + memset(s->code_ptr, 0, size); + s->code_ptr += size; + /* Followed by TCI code */ h->tci_ptr = s->code_ptr;
Signature · 1 line
2.43.0
PATCH 12/35 tcg/wasm: Add shl/shr/sar instructions
In reply to Kohei TokunagaThis commit implements shl, shr and sar operations using Wasm instructions. The Wasm backend uses 64bit variables so the right shift operation for 32bit values extract the lower 32bit of the operand before shifting.
--- tcg/wasm/tcg-target.c.inc | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)
tcg/wasm/tcg-target.c.inc +38 -0
@@ -120,12 +120,22 @@ typedef enum { OPC_GLOBAL_GET = 0x23, OPC_GLOBAL_SET = 0x24, + OPC_I32_SHL = 0x74, + OPC_I32_SHR_S = 0x75, + OPC_I32_SHR_U = 0x76, + OPC_I64_ADD = 0x7c, OPC_I64_SUB = 0x7d, OPC_I64_MUL = 0x7e, OPC_I64_AND = 0x83, OPC_I64_OR = 0x84, OPC_I64_XOR = 0x85, + OPC_I64_SHL = 0x86, + OPC_I64_SHR_S = 0x87, + OPC_I64_SHR_U = 0x88, + + OPC_I32_WRAP_I64 = 0xa7, + OPC_I64_EXTEND_I32_U = 0xad, } WasmInsn; #define BUF_SIZE 1024
@@ -199,6 +209,27 @@ static void tcg_wasm_out_o1_i2( tcg_wasm_out_op(s, opc); tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); } +static void tcg_wasm_out_o1_i2_type( + TCGContext *s, TCGType type, WasmInsn opc32, WasmInsn opc64, + TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + switch (type) { + case TCG_TYPE_I32: + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + tcg_wasm_out_op(s, OPC_I32_WRAP_I64); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg2)); + tcg_wasm_out_op(s, OPC_I32_WRAP_I64); + tcg_wasm_out_op(s, opc32); + tcg_wasm_out_op(s, OPC_I64_EXTEND_I32_U); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); + break; + case TCG_TYPE_I64: + tcg_wasm_out_o1_i2(s, opc64, ret, arg1, arg2); + break; + default: + g_assert_not_reached(); + } +} static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, intptr_t value, intptr_t addend)
@@ -930,11 +961,14 @@ static const TCGOutOpBinary outop_rotr = { static void tgen_sar(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { + TCGReg orig_a1 = a1; if (type < TCG_TYPE_REG) { tcg_out_ext32s(s, TCG_REG_TMP, a1); a1 = TCG_REG_TMP; } tcg_out_op_rrr(s, INDEX_op_sar, a0, a1, a2); + tcg_wasm_out_o1_i2_type(s, type, OPC_I32_SHR_S, OPC_I64_SHR_S, + a0, orig_a1, a2); } static const TCGOutOpBinary outop_sar = {
@@ -946,6 +980,7 @@ static void tgen_shl(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { tcg_out_op_rrr(s, INDEX_op_shl, a0, a1, a2); + tcg_wasm_out_o1_i2(s, OPC_I64_SHL, a0, a1, a2); } static const TCGOutOpBinary outop_shl = {
@@ -956,11 +991,14 @@ static const TCGOutOpBinary outop_shl = { static void tgen_shr(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { + TCGReg orig_a1 = a1; if (type < TCG_TYPE_REG) { tcg_out_ext32u(s, TCG_REG_TMP, a1); a1 = TCG_REG_TMP; } tcg_out_op_rrr(s, INDEX_op_shr, a0, a1, a2); + tcg_wasm_out_o1_i2_type(s, type, OPC_I32_SHR_U, OPC_I64_SHR_U, + a0, orig_a1, a2); } static const TCGOutOpBinary outop_shr = {
Signature · 1 line
2.43.0
PATCH 31/35 tcg/wasm: Allow switching coroutine from a helper
In reply to Kohei TokunagaEmscripten's Fiber coroutine implements coroutine switching using Asyncify's stack unwinding and rewinding features [1]. When a coroutine yields (i.e. switches out), Asyncify unwinds the stack, returning control to Emscripten's JS code (Fiber.trampoline()). Then execution resumes in the target coroutine by rewinding the stack. Stack unwinding is implemented by a sequence of immediate function returns, while rewinding re-enters the functions in the call stack, skipping any code between the function's entry point and the original call position [2].
This commit updates the Wasm TB module to allow helper functions to trigger coroutine switching. Particaully, the TB handles the unwinding and rewinding flows as follows:
- The TB check the Asyncify.state JS object after each helper call. If unwinding is in progress, the TB immediately returns to the caller so that the unwinding can continue.
- Each function call is preceded by a block boundary and an update of the BLOCK_IDX variable. This enables rewinding to skip any code between the function's entry point and the original call position.
Additionally, this commit introduces WasmContext.do_init which is a flag indicating whether the TB should reset the BLOCK_IDX variable to 0 (i.e. start from the beginning). call_wasm_tb is a newly introduced wrapper function for the Wasm module's entrypoint and this sets "do_init = 1" to ensure normal TB execution begins at the first block. During a rewinding, the C code does not set do_init to 1, allowing the TB to preserve the BLOCK_IDX value from the previous unwinding and correctly resume execution from the last unwound block.
[1] https://emscripten.org/docs/api_reference/fiber.h.html
[2] https://kripken.github.io/blog/wasm/2019/07/16/asyncify.html#new-asyncify
--- tcg/wasm.c | 3 +++ tcg/wasm.h | 11 ++++++++ tcg/wasm/tcg-target.c.inc | 56 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 69 insertions(+), 1 deletion(-)
tcg/wasm.c +3 -0
@@ -64,6 +64,9 @@ EM_JS_PRE(void*, instantiate_wasm, (void *wasm_begin, const wasm = HEAP8.subarray(DEC_PTR(wasm_begin),DEC_PTR (wasm_begin) + wasm_size);var helper = {}; + helper.u = () => { + return (Asyncify.state != Asyncify.State.Unwinding) ? 1 : 0; + }; const entsize = TCG_TARGET_REG_BITS / 8;for (var i =0 ; i < import_vec_size / entsize; i++) { const idx = memory_v.getBigInt64(
tcg/wasm.h +11 -0
@@ -32,11 +32,22 @@ struct WasmContext {* Pointer to a stack array. */ uint64_t *stack; + +/* +* Flag indicating whether to initialize the block index(1) or not(0). +*/ +uint32_t do_init; };/* Instantiated Wasm function of a TB */ typedef uintptr_t (*wasm_tb_func )(struct WasmContext *); +static inline uintptr_t call_wasm_tb (wasm_tb_func f,struct WasmContext *ctx) +{ + ctx->do_init =1 ;/* reset the block index (rewinding will skip this) */ +return f (ctx); +}; +/* * A TB of the Wasm backend starts from a header which contains pointers for * each data stored in the following region in the TB.
tcg/wasm/tcg-target.c.inc +55 -1
119 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH 19/35 tcg/wasm: Add rem/div instructions
In reply to Kohei TokunagaThis commit implements rem and div operations using Wasm's rem and div instructions.
--- tcg/wasm/tcg-target.c.inc | 12 ++++++++++++ 1 file changed, 12 insertions(+)
tcg/wasm/tcg-target.c.inc +12 -0
@@ -169,6 +169,10 @@ 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_AND = 0x71, OPC_I32_OR = 0x72, OPC_I32_SHL = 0x74,
@@ -179,6 +183,10 @@ typedef enum { 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,
@@ -1223,6 +1231,7 @@ static void tgen_divs(TCGContext *s, TCGType type, ? 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 = {
@@ -1241,6 +1250,7 @@ static void tgen_divu(TCGContext *s, TCGType type, ? 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 = {
@@ -1377,6 +1387,7 @@ static void tgen_rems(TCGContext *s, TCGType type, ? 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 = {
@@ -1391,6 +1402,7 @@ static void tgen_remu(TCGContext *s, TCGType type, ? 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 = {
Signature · 1 line
2.43.0
PATCH 5/35 tcg: Fork TCI for wasm backend
In reply to Kohei TokunagaThe Wasm backend is implemented based on the forked TCI backend with utilizing the TCI interpreter to execute TBs.
--- MAINTAINERS | 6 + tcg/wasm.c | 1076 ++++++++++++++++++++++++++ tcg/wasm/tcg-target-con-set.h | 21 + tcg/wasm/tcg-target-con-str.h | 11 + tcg/wasm/tcg-target-has.h | 22 + tcg/wasm/tcg-target-mo.h | 17 + tcg/wasm/tcg-target-opc.h.inc | 15 + tcg/wasm/tcg-target-reg-bits.h | 18 + tcg/wasm/tcg-target.c.inc | 1320 ++++++++++++++++++++++++++++++++ tcg/wasm/tcg-target.h | 77 ++ 10 files changed, 2583 insertions(+) create mode 100644 tcg/wasm.c create mode 100644 tcg/wasm/tcg-target-con-set.h create mode 100644 tcg/wasm/tcg-target-con-str.h create mode 100644 tcg/wasm/tcg-target-has.h create mode 100644 tcg/wasm/tcg-target-mo.h create mode 100644 tcg/wasm/tcg-target-opc.h.inc create mode 100644 tcg/wasm/tcg-target-reg-bits.h create mode 100644 tcg/wasm/tcg-target.c.inc create mode 100644 tcg/wasm/tcg-target.h
V1:
- Although checkpatch.pl reports the following error in tcg/wasm.c, tcg/wasm/tcg-target.c.inc and tcg/wasm/tcg-target.h, these files were copied from TCI codes so they are preserved as-is.
Quoted 3 lines
New file '
tcg/wasm.c' must not have license boilerplate header text, only the SPDX-License-Identifier, unless this file was copied from existing code already having such text.
MAINTAINERS +6 -0
@@ -3999,6 +3999,12 @@ F: tcg/tci/ F: tcg/tci.c F: disas/tci.c +WebAssembly TCG target +M: Kohei Tokunaga <[email protected]> +S: Maintained +F: tcg/wasm/ +F: tcg/wasm.c + Block drivers ------------- VMDK
tcg/wasm.c new file +1076 -0
1,076 lines not on this page · raw message
tcg/wasm/tcg-target-con-set.h new file +21 -0
@@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: MIT */ +/* +* TCI target-specific constraint sets. +* Copyright (c) 2021 Linaro +*/ + +/* +* C_On_Im(...) defines a constraint set with <n> outputs and <m> inputs. +* Each operand should be a sequence of constraint letters as defined by +* tcg-target-con-str.h; the constraint combination is inclusive or. +*/ +C_O0_I1(r) +C_O0_I2(r, r) +C_O0_I3(r, r, r) +C_O0_I4(r, r, r, r) +C_O1_I1(r, r) +C_O1_I2(r, r, r) +C_O1_I4(r, r, r, r, r) +C_O2_I1(r, r, r) +C_O2_I2(r, r, r, r) +C_O2_I4(r, r, r, r, r, r)
tcg/wasm/tcg-target-con-str.h new file +11 -0
@@ -0,0 +1,11 @@ +/* SPDX-License-Identifier: MIT */ +/* +* Define TCI target-specific operand constraints. +* Copyright (c) 2021 Linaro +*/ + +/* +* Define constraint letters for register sets: +* REGS(letter, register_mask) +*/ +REGS ('r' ,MAKE_64BIT_MASK (0 ,TCG_TARGET_NB_REGS ))
tcg/wasm/tcg-target-has.h new file +22 -0
@@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: MIT */ +/* +* Define target-specific opcode support +* Copyright (c) 2009, 2011 Stefan Weil +*/ + +#ifndef TCG_TARGET_HAS_H +#define TCG_TARGET_HAS_H + +#if TCG_TARGET_REG_BITS ==64 +#define TCG_TARGET_HAS_extr_i64_i32 0 +#endif /* TCG_TARGET_REG_BITS == 64 */ + +#define TCG_TARGET_HAS_qemu_ldst_i128 0 + +#define TCG_TARGET_HAS_tst 1 + +#define TCG_TARGET_extract_valid (type, ofs, len) 1 +#define TCG_TARGET_sextract_valid (type, ofs, len) 1 +#define TCG_TARGET_deposit_valid (type, ofs, len) 1 + +#endif
tcg/wasm/tcg-target-mo.h new file +17 -0
@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: MIT */ +/* +* Define target-specific memory model +* Copyright (c) 2009, 2011 Stefan Weil +*/ + +#ifndef TCG_TARGET_MO_H +#define TCG_TARGET_MO_H + +/* +* We could notice __i386__ or __s390x__ and reduce the barriers depending +* on the host. But if you want performance, you use the normal backend. +* We prefer consistency across hosts on this. +*/ +#define TCG_TARGET_DEFAULT_MO 0 + +#endif
tcg/wasm/tcg-target-opc.h.inc new file +15 -0
@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: MIT */ +/* These opcodes for use between the tci generator and interpreter. */ +DEF(tci_movi, 1, 0, 1, TCG_OPF_NOT_PRESENT) +DEF(tci_movl, 1, 0, 1, TCG_OPF_NOT_PRESENT) +DEF(tci_setcarry, 0, 0, 0, TCG_OPF_NOT_PRESENT) +DEF(tci_clz32, 1, 2, 0, TCG_OPF_NOT_PRESENT) +DEF(tci_ctz32, 1, 2, 0, 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) +DEF(tci_rotl32, 1, 2, 0, TCG_OPF_NOT_PRESENT) +DEF(tci_rotr32, 1, 2, 0, TCG_OPF_NOT_PRESENT) +DEF(tci_setcond32, 1, 2, 1, TCG_OPF_NOT_PRESENT) +DEF(tci_movcond32, 1, 2, 1, TCG_OPF_NOT_PRESENT)
tcg/wasm/tcg-target-reg-bits.h new file +18 -0
@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: MIT */ +/* +* Define target-specific register size +* Copyright (c) 2009, 2011 Stefan Weil +*/ + +#ifndef TCG_TARGET_REG_BITS_H +#define TCG_TARGET_REG_BITS_H + +#if UINTPTR_MAX ==UINT32_MAX +# define TCG_TARGET_REG_BITS 32 +#elif UINTPTR_MAX ==UINT64_MAX +# define TCG_TARGET_REG_BITS 64 +#else +# error Unknown pointer size for tci target +#endif + +#endif
tcg/wasm/tcg-target.c.inc new file +1320 -0
1,320 lines not on this page · raw message
tcg/wasm/tcg-target.h new file +77 -0
77 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH 24/35 tcg/wasm: Add exit_tb/goto_tb/goto_ptr instructions
In reply to Kohei TokunagaIn the Wasm backend, each TB is compiled to a separeted Wasm module. Control transfer between TBs (i.e. from one Wasm module to another) is handled by the caller of the module.
The goto_tb and goto_ptr operations are implemented by returning control to the caller using the return instruction. The destination TB's pointer is passed to the caller via a shared WasmContext structure which is accessible from both the Wasm module and the caller. This WasmContext must be provided to the module as an argument.
If the destination TB is the current TB itself, there is no need to return control to the caller. Instead, execution can jump directly to the top of the loop within the TB.
The exit_tb operation sets the pointer in WasmContext to 0, indicating that there is no destination TB.
--- MAINTAINERS | 1 + tcg/wasm.h | 17 ++++++++ tcg/wasm/tcg-target.c.inc | 89 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 tcg/wasm.h
MAINTAINERS +1 -0
@@ -4004,6 +4004,7 @@ M: Kohei Tokunaga <[email protected]> S: Maintained F: tcg/wasm/ F: tcg/wasm.c +F: tcg/wasm.h Block drivers -------------
tcg/wasm.h new file +17 -0
@@ -0,0 +1,17 @@ +/* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ +#ifndef TCG_WASM_H +#define TCG_WASM_H + +/* +* WasmContext is a data shared among QEMU and wasm modules. +*/ +struct WasmContext { +/* +* Pointer to the TB to be executed. +*/ +void *tb_ptr; +}; + +#endif
tcg/wasm/tcg-target.c.inc +87 -2
131 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH 28/35 tcg/wasm: Add initialization of fundamental registers
In reply to Kohei TokunagaThis commit adds initialization of TCG_AREG0 and TCG_REG_CALL_STACK at the beginning of each TB. The CPUArchState struct and the stack array are passed from the caller via the WasmContext structure. The BLOCK_IDX variable is initialized to 0 as TB execution begins at the first block.
--- tcg/wasm.h | 10 ++++++++++ tcg/wasm/tcg-target.c.inc | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+)
tcg/wasm.h +10 -0
@@ -22,6 +22,16 @@ struct WasmContext {* Buffer to store 128bit return value on call. */ void *buf128; + +/* +* Pointer to the CPUArchState struct. +*/ +CPUArchState *env; + +/* +* Pointer to a stack array. +*/ + uint64_t *stack; };#endif
tcg/wasm/tcg-target.c.inc +19 -0
@@ -2419,11 +2419,30 @@ static inline void tcg_target_qemu_prologue(TCGContext *s) static void tcg_out_tb_start(TCGContext *s) { + intptr_t ofs; + init_sub_buf(); init_blocks(); init_label_info(); init_helpers(); + /* Initialize fundamental registers */ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(TCG_AREG0)); + tcg_wasm_out_op(s, OPC_I64_EQZ); + tcg_wasm_out_op_block(s, OPC_IF, BLOCK_NORET); + + ofs = tcg_wasm_out_get_ctx(s, CTX_OFFSET(env)); + tcg_wasm_out_op_ldst(s, OPC_I64_LOAD, 0, ofs); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(TCG_AREG0)); + + ofs = tcg_wasm_out_get_ctx(s, CTX_OFFSET(stack)); + tcg_wasm_out_op_ldst(s, OPC_I64_LOAD, 0, ofs); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(TCG_REG_CALL_STACK)); + tcg_wasm_out_op(s, OPC_END); + + tcg_wasm_out_op_const(s, OPC_I64_CONST, 0); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, BLOCK_IDX); + tcg_wasm_out_op_block(s, OPC_LOOP, BLOCK_NORET); tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, BLOCK_IDX); tcg_wasm_out_op(s, OPC_I64_EQZ);
Signature · 1 line
2.43.0
PATCH 34/35 meson: Propagate optimization flag for linking on Emscripten
In reply to Kohei TokunagaEmscripten uses the optimization flag at the link time to enable optimizations via Binaryen [1]. While meson.build currently recognizes the -Doptimization option, it does not propagate it to the linking. This commit updates meson.build to propagate the optimization flag to the linking when targeting WebAssembly.
[1] https://emscripten.org/docs/optimizing/Optimizing-Code.html#how-emscripten-optimizes
--- meson.build | 6 ++++++ 1 file changed, 6 insertions(+)
meson.build +6 -0
@@ -878,6 +878,12 @@ elif host_os == 'openbsd' # Disable OpenBSD W^X if available emulator_link_args = cc.get_supported_link_arguments('-Wl,-z,wxneeded') endif +elif host_os == 'emscripten' + # Emscripten uses the optimization flag also during the link time. + # https://emscripten.org/docs/optimizing/Optimizing-Code.html#how-emscripten-optimizes + if get_option('optimization') != 'plain' + emulator_link_args += ['-O' + get_option('optimization')] + endif endif ###############################################
Signature · 1 line
2.43.0
PATCH 27/35 tcg/wasm: Mark unimplemented instructions as C_NotImplemented
In reply to Kohei TokunagaThis commit explicitly marks functions which aren't implemented in the Wasm backend as C_NotImplemented.
--- tcg/wasm/tcg-target.c.inc | 107 ++++---------------------------------- 1 file changed, 10 insertions(+), 97 deletions(-)
tcg/wasm/tcg-target.c.inc +10 -97
@@ -1430,19 +1430,6 @@ static void tcg_out_op_rrrbb(TCGContext *s, TCGOpcode op, TCGReg r0, tcg_out32(s, insn); } -static void tcg_out_op_rrrr(TCGContext *s, TCGOpcode op, - TCGReg r0, TCGReg r1, TCGReg r2, TCGReg r3) -{ - tcg_insn_unit insn = 0; - - insn = deposit32(insn, 0, 8, op); - insn = deposit32(insn, 8, 4, r0); - insn = deposit32(insn, 12, 4, r1); - insn = deposit32(insn, 16, 4, r2); - insn = deposit32(insn, 20, 4, r3); - tcg_out32(s, insn); -} - static void tcg_out_op_rrrrrc(TCGContext *s, TCGOpcode op, TCGReg r0, TCGReg r1, TCGReg r2, TCGReg r3, TCGReg r4, TCGCond c5)
@@ -1699,50 +1686,21 @@ static const TCGOutOpBinary outop_add = { .out_rrr = tgen_add, }; -static TCGConstraintSetIndex cset_addsubcarry(TCGType type, unsigned flags) -{ - return type == TCG_TYPE_REG ? C_O1_I2(r, r, r) : C_NotImplemented; -} - -static void tgen_addco(TCGContext *s, TCGType type, - TCGReg a0, TCGReg a1, TCGReg a2) -{ - tcg_out_op_rrr(s, INDEX_op_addco, a0, a1, a2); -} - static const TCGOutOpBinary outop_addco = { - .base.static_constraint = C_Dynamic, - .base.dynamic_constraint = cset_addsubcarry, - .out_rrr = tgen_addco, + .base.static_constraint = C_NotImplemented, }; -static void tgen_addci(TCGContext *s, TCGType type, - TCGReg a0, TCGReg a1, TCGReg a2) -{ - tcg_out_op_rrr(s, INDEX_op_addci, a0, a1, a2); -} - static const TCGOutOpAddSubCarry outop_addci = { - .base.static_constraint = C_Dynamic, - .base.dynamic_constraint = cset_addsubcarry, - .out_rrr = tgen_addci, + .base.static_constraint = C_NotImplemented, }; -static void tgen_addcio(TCGContext *s, TCGType type, - TCGReg a0, TCGReg a1, TCGReg a2) -{ - tcg_out_op_rrr(s, INDEX_op_addcio, a0, a1, a2); -} - static const TCGOutOpBinary outop_addcio = { - .base.static_constraint = C_Dynamic, - .base.dynamic_constraint = cset_addsubcarry, - .out_rrr = tgen_addcio, + .base.static_constraint = C_NotImplemented, }; static void tcg_out_set_carry(TCGContext *s) { - tcg_out_op_v(s, INDEX_op_tci_setcarry); + g_assert_not_reached(); } static void tgen_and(TCGContext *s, TCGType type,
@@ -1886,37 +1844,16 @@ static const TCGOutOpBinary outop_mul = { .out_rrr = tgen_mul, }; -static TCGConstraintSetIndex cset_mul2(TCGType type, unsigned flags) -{ - return type == TCG_TYPE_REG ? C_O2_I2(r, r, r, r) : C_NotImplemented; -} - -static void tgen_muls2(TCGContext *s, TCGType type, - TCGReg a0, TCGReg a1, TCGReg a2, TCGReg a3) -{ - tcg_out_op_rrrr(s, INDEX_op_muls2, a0, a1, a2, a3); -} - static const TCGOutOpMul2 outop_muls2 = { - .base.static_constraint = C_Dynamic, - .base.dynamic_constraint = cset_mul2, - .out_rrrr = tgen_muls2, + .base.static_constraint = C_NotImplemented, }; static const TCGOutOpBinary outop_mulsh = { .base.static_constraint = C_NotImplemented, }; -static void tgen_mulu2(TCGContext *s, TCGType type, - TCGReg a0, TCGReg a1, TCGReg a2, TCGReg a3) -{ - tcg_out_op_rrrr(s, INDEX_op_mulu2, a0, a1, a2, a3); -} - static const TCGOutOpMul2 outop_mulu2 = { - .base.static_constraint = C_Dynamic, - .base.dynamic_constraint = cset_mul2, - .out_rrrr = tgen_mulu2, + .base.static_constraint = C_NotImplemented, }; static const TCGOutOpBinary outop_muluh = {
@@ -2091,45 +2028,21 @@ static const TCGOutOpSubtract outop_sub = { .out_rrr = tgen_sub, }; -static void tgen_subbo(TCGContext *s, TCGType type, - TCGReg a0, TCGReg a1, TCGReg a2) -{ - tcg_out_op_rrr(s, INDEX_op_subbo, a0, a1, a2); -} - static const TCGOutOpAddSubCarry outop_subbo = { - .base.static_constraint = C_Dynamic, - .base.dynamic_constraint = cset_addsubcarry, - .out_rrr = tgen_subbo, + .base.static_constraint = C_NotImplemented, }; -static void tgen_subbi(TCGContext *s, TCGType type, - TCGReg a0, TCGReg a1, TCGReg a2) -{ - tcg_out_op_rrr(s, INDEX_op_subbi, a0, a1, a2); -} - static const TCGOutOpAddSubCarry outop_subbi = { - .base.static_constraint = C_Dynamic, - .base.dynamic_constraint = cset_addsubcarry, - .out_rrr = tgen_subbi, + .base.static_constraint = C_NotImplemented, }; -static void tgen_subbio(TCGContext *s, TCGType type, - TCGReg a0, TCGReg a1, TCGReg a2) -{ - tcg_out_op_rrr(s, INDEX_op_subbio, a0, a1, a2); -} - static const TCGOutOpAddSubCarry outop_subbio = { - .base.static_constraint = C_Dynamic, - .base.dynamic_constraint = cset_addsubcarry, - .out_rrr = tgen_subbio, + .base.static_constraint = C_NotImplemented, }; static void tcg_out_set_borrow(TCGContext *s) { - tcg_out_op_v(s, INDEX_op_tci_setcarry); /* borrow == carry */ + g_assert_not_reached(); } static void tgen_xor(TCGContext *s, TCGType type,
Signature · 1 line
2.43.0
PATCH 21/35 tcg/wasm: Add neg/not/ctpop instructions
In reply to Kohei TokunagaThe Wasm backend implements only TCG_TARGET_REG_BITS=64 so the ctpop instruction is generated only for 64bit operations, as declared in cset_ctpop. Therefore, this commit adds only the 64bit version of ctpop implementation.
The tgen_neg function was used by several functions and intended to emit TCI code. So it have been renamed to tgen_neg_tci.
--- tcg/wasm/tcg-target.c.inc | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-)
tcg/wasm/tcg-target.c.inc +34 -2
@@ -180,6 +180,7 @@ typedef enum { OPC_I32_SHR_U = 0x76, OPC_I32_ROTR = 0x78, + OPC_I64_POPCNT = 0x7b, OPC_I64_ADD = 0x7c, OPC_I64_SUB = 0x7d, OPC_I64_MUL = 0x7e,
@@ -499,6 +500,29 @@ static void tcg_wasm_out_nor( tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); } +static void tcg_wasm_out_neg(TCGContext *s, TCGReg ret, TCGReg arg) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg)); + tcg_wasm_out_op_not(s); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 1); + tcg_wasm_out_op(s, OPC_I64_ADD); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); +} + +static void tcg_wasm_out_not(TCGContext *s, TCGReg ret, TCGReg arg) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg)); + tcg_wasm_out_op_not(s); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); +} + +static void tcg_wasm_out_ctpop64(TCGContext *s, TCGReg ret, TCGReg arg) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg)); + tcg_wasm_out_op(s, OPC_I64_POPCNT); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); +} + static void tcg_wasm_out_setcond(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1, TCGReg arg2, TCGCond cond) {
@@ -1609,6 +1633,7 @@ static const TCGOutOpBinary outop_xor = { static void tgen_ctpop(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1) { tcg_out_op_rr(s, INDEX_op_ctpop, a0, a1); + tcg_wasm_out_ctpop64(s, a0, a1); } static TCGConstraintSetIndex cset_ctpop(TCGType type, unsigned flags)
@@ -1665,9 +1690,15 @@ static const TCGOutOpUnary outop_bswap64 = { }; #endif -static void tgen_neg(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1) +static void tgen_neg_tci(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1) { tcg_out_op_rr(s, INDEX_op_neg, a0, a1); + } + +static void tgen_neg(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1) +{ + tgen_neg_tci(s, type, a0, a1); + tcg_wasm_out_neg(s, a0, a1); } static const TCGOutOpUnary outop_neg = {
@@ -1678,6 +1709,7 @@ static const TCGOutOpUnary outop_neg = { static void tgen_not(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1) { tcg_out_op_rr(s, INDEX_op_not, a0, a1); + tcg_wasm_out_not(s, a0, a1); } static const TCGOutOpUnary outop_not = {
@@ -1710,7 +1742,7 @@ static void tgen_negsetcond(TCGContext *s, TCGType type, TCGCond cond, TCGReg dest, TCGReg arg1, TCGReg arg2) { tgen_setcond_tci(s, type, cond, dest, arg1, arg2); - tgen_neg(s, type, dest, dest); + tgen_neg_tci(s, type, dest, dest); tcg_wasm_out_negsetcond(s, type, dest, arg1, arg2, cond); }
Signature · 1 line
2.43.0
PATCH 33/35 tcg/wasm: Enable TLB lookup
In reply to Kohei TokunagaThis commit enables qemu_ld and qemu_st to perform TLB lookups, following the approach used in other backends such as RISC-V. Unlike other backends, the Wasm backend cannot use ldst labels, as jumping to specific code addresses (e.g. raddr) is not possible in Wasm. Instead, each TLB lookup is followed by a if branch: if the lookup succeeds, the memory is accessed directly; otherwise, a fallback helper function is invoked. Support for MO_BSWAP is not yet implemented, so has_memory_bswap is set to false.
--- tcg/tcg.c | 2 +- tcg/wasm/tcg-target.c.inc | 220 +++++++++++++++++++++++++++++++++++++- 2 files changed, 218 insertions(+), 4 deletions(-)
tcg/tcg.c +1 -1
@@ -1121,7 +1121,7 @@ typedef struct TCGOutOpSubtract {#include "tcg-target.c.inc" -#if !defined(CONFIG_TCG_INTERPRETER ) && !defined(EMSCRIPTEN ) +#if !defined(CONFIG_TCG_INTERPRETER )/* Validate CPUTLBDescFast placement. */ QEMU_BUILD_BUG_ON((int)(offsetof(CPUNegativeOffsetState, tlb.f[0]) -sizeof (CPUNegativeOffsetState))
tcg/wasm/tcg-target.c.inc +217 -3
310 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH 2/35 configure: Enable to propagate -sMEMORY64 flag to Emscripten
In reply to Kohei TokunagaCurrently there are some engines that don't support wasm64 (e.g. unsupported on Safari[1]). To mitigate this issue, the configure script allows the user to use Emscripten's compatibility feature, "-sMEMORY64=2" flag[2].
Emscripten's "-sMEMORY64=2" flag still enables 64bit pointers in C code. But this flag lowers the output binary into wasm32, with limiting the maximum memory size to 4GB. So QEMU can run on wasm32 engines.
[1] https://webassembly.org/features/
[2] https://emscripten.org/docs/tools_reference/settings_reference.html#memory64
--- configure | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
V1:
- This commit is under review in another patch series so please ignore it here.
configure +11 -1
@@ -182,6 +182,10 @@ EXTRA_CXXFLAGS="" EXTRA_OBJCFLAGS="" EXTRA_LDFLAGS="" +# The value is propagated to Emscripten's "-sMEMORY64" flag. +# https://emscripten.org/docs/tools_reference/settings_reference.html#memory64 +wasm64_memory64=1 + # Default value for a variable defining feature "foo". # * foo="no" feature will only be used if --enable-foo arg is given # * foo="" feature will be searched for, and if found, will be used
@@ -239,6 +243,8 @@ for opt do ;; --without-default-features) default_feature="no" ;; + --wasm64-32bit-address-limit) wasm64_memory64="2" + ;; esac done
@@ -537,7 +543,7 @@ case "$cpu" in CPU_CFLAGS="-m32" ;; wasm64) - CPU_CFLAGS="-m64 -sMEMORY64=1" + CPU_CFLAGS="-m64 -sMEMORY64=$wasm64_memory64" ;; esac
@@ -795,6 +801,8 @@ for opt do ;; --disable-rust) rust=disabled ;; + --wasm64-32bit-address-limit) + ;; # everything else has the same name in configure and meson --*) meson_option_parse "$opt" "$optarg" ;;
@@ -920,6 +928,8 @@ Advanced options (experts only): --disable-containers don't use containers for cross-building --container-engine=TYPE which container engine to use [$container_engine] --gdb=GDB-path gdb to use for gdbstub tests [$gdb_bin] + --wasm64-32bit-address-limit Restrict wasm64 address space to 32-bit (default + is to use the whole 64-bit range). EOF meson_options_help cat << EOF
Signature · 1 line
2.43.0
PATCH 22/35 tcg/wasm: Add rot/clz/ctz instructions
In reply to Kohei TokunagaThis commit implements rot, clz and ctz operations using Wasm instructions.
--- tcg/wasm/tcg-target.c.inc | 48 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+)
tcg/wasm/tcg-target.c.inc +48 -0
@@ -146,6 +146,7 @@ typedef enum { OPC_I32_CONST = 0x41, OPC_I64_CONST = 0x42, + OPC_I32_EQZ = 0x45, OPC_I32_EQ = 0x46, OPC_I32_NE = 0x47, OPC_I32_LT_S = 0x48,
@@ -157,6 +158,7 @@ typedef enum { OPC_I32_GE_S = 0x4e, OPC_I32_GE_U = 0x4f, + OPC_I64_EQZ = 0x50, OPC_I64_EQ = 0x51, OPC_I64_NE = 0x52, OPC_I64_LT_S = 0x53,
@@ -168,6 +170,8 @@ typedef enum { OPC_I64_GE_S = 0x59, OPC_I64_GE_U = 0x5a, + OPC_I32_CLZ = 0x67, + OPC_I32_CTZ = 0x68, OPC_I32_ADD = 0x6a, OPC_I32_DIV_S = 0x6d, OPC_I32_DIV_U = 0x6e,
@@ -178,8 +182,11 @@ typedef enum { OPC_I32_SHL = 0x74, OPC_I32_SHR_S = 0x75, OPC_I32_SHR_U = 0x76, + OPC_I32_ROTL = 0x77, OPC_I32_ROTR = 0x78, + OPC_I64_CLZ = 0x79, + OPC_I64_CTZ = 0x7a, OPC_I64_POPCNT = 0x7b, OPC_I64_ADD = 0x7c, OPC_I64_SUB = 0x7d,
@@ -194,6 +201,7 @@ typedef enum { OPC_I64_SHL = 0x86, OPC_I64_SHR_S = 0x87, OPC_I64_SHR_U = 0x88, + OPC_I64_ROTL = 0x89, OPC_I64_ROTR = 0x8a, OPC_I32_WRAP_I64 = 0xa7,
@@ -720,6 +728,42 @@ static void tcg_wasm_out_bswap16( tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(dest)); } +static void tcg_wasm_out_cz( + TCGContext *s, TCGType type, WasmInsn opc32, WasmInsn opc64, + TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + switch (type) { + case TCG_TYPE_I32: + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + tcg_wasm_out_op(s, OPC_I32_WRAP_I64); + tcg_wasm_out_op(s, OPC_I32_EQZ); + tcg_wasm_out_op_block(s, OPC_IF, BLOCK_I32); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg2)); + tcg_wasm_out_op(s, OPC_I32_WRAP_I64); + tcg_wasm_out_op(s, OPC_ELSE); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + tcg_wasm_out_op(s, OPC_I32_WRAP_I64); + tcg_wasm_out_op(s, opc32); + tcg_wasm_out_op(s, OPC_END); + tcg_wasm_out_op(s, OPC_I64_EXTEND_I32_U); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); + break; + case TCG_TYPE_I64: + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + tcg_wasm_out_op(s, OPC_I64_EQZ); + tcg_wasm_out_op_block(s, OPC_IF, BLOCK_I64); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg2)); + tcg_wasm_out_op(s, OPC_ELSE); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + tcg_wasm_out_op(s, opc64); + tcg_wasm_out_op(s, OPC_END); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); + break; + default: + g_assert_not_reached(); + } +} + static void tcg_wasm_out_mov(TCGContext *s, TCGReg ret, TCGReg arg) { tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg));
@@ -1266,6 +1310,7 @@ static void tgen_clz(TCGContext *s, TCGType type, ? INDEX_op_tci_clz32 : INDEX_op_clz); tcg_out_op_rrr(s, opc, a0, a1, a2); + tcg_wasm_out_cz(s, type, OPC_I32_CLZ, OPC_I64_CLZ, a0, a1, a2); } static const TCGOutOpBinary outop_clz = {
@@ -1280,6 +1325,7 @@ static void tgen_ctz(TCGContext *s, TCGType type, ? INDEX_op_tci_ctz32 : INDEX_op_ctz); tcg_out_op_rrr(s, opc, a0, a1, a2); + tcg_wasm_out_cz(s, type, OPC_I32_CTZ, OPC_I64_CTZ, a0, a1, a2); } static const TCGOutOpBinary outop_ctz = {
@@ -1496,6 +1542,7 @@ static void tgen_rotl(TCGContext *s, TCGType type, ? INDEX_op_tci_rotl32 : INDEX_op_rotl); tcg_out_op_rrr(s, opc, a0, a1, a2); + tcg_wasm_out_o1_i2_type(s, type, OPC_I32_ROTL, OPC_I64_ROTL, a0, a1, a2); } static const TCGOutOpBinary outop_rotl = {
@@ -1510,6 +1557,7 @@ static void tgen_rotr(TCGContext *s, TCGType type, ? INDEX_op_tci_rotr32 : INDEX_op_rotr); tcg_out_op_rrr(s, opc, a0, a1, a2); + tcg_wasm_out_o1_i2_type(s, type, OPC_I32_ROTR, OPC_I64_ROTL, a0, a1, a2); } static const TCGOutOpBinary outop_rotr = {
Signature · 1 line
2.43.0
PATCH 26/35 tcg/wasm: Add qemu_ld/qemu_st instructions
In reply to Kohei TokunagaThis commit adds qemu_ld and qemu_st by calling the helper functions corresponding to MemOp.
--- tcg/wasm/tcg-target.c.inc | 95 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+)
tcg/wasm/tcg-target.c.inc +95 -0
@@ -1155,6 +1155,99 @@ static void tcg_wasm_out_call(TCGContext *s, intptr_t func, gen_call(s, info, func_idx); } +static void *qemu_ld_helper_ptr(uint32_t oi) +{ + MemOp mop = get_memop(oi); + switch (mop & MO_SSIZE) { + case MO_UB: + return helper_ldub_mmu; + case MO_SB: + return helper_ldsb_mmu; + case MO_UW: + return helper_lduw_mmu; + case MO_SW: + return helper_ldsw_mmu; + case MO_UL: + return helper_ldul_mmu; + case MO_SL: + return helper_ldsl_mmu; + case MO_UQ: + return helper_ldq_mmu; + default: + g_assert_not_reached(); + } +} + +static void tcg_wasm_out_qemu_ld(TCGContext *s, TCGReg data_reg, + TCGReg addr_reg, MemOpIdx oi) +{ + intptr_t helper_idx; + int64_t func_idx; + + helper_idx = (intptr_t)qemu_ld_helper_ptr(oi); + func_idx = get_helper_idx(s, helper_idx); + if (func_idx < 0) { + func_idx = register_helper(s, helper_idx); + } + + /* call the target helper */ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(TCG_AREG0)); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(addr_reg)); + tcg_wasm_out_op_const(s, OPC_I32_CONST, oi); + tcg_wasm_out_op_const(s, OPC_I64_CONST, (intptr_t)s->code_ptr); + + tcg_wasm_out_op_idx(s, OPC_CALL, func_idx); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(data_reg)); +} + +static void *qemu_st_helper_ptr(uint32_t oi) +{ + MemOp mop = get_memop(oi); + switch (mop & MO_SIZE) { + case MO_8: + return helper_stb_mmu; + case MO_16: + return helper_stw_mmu; + case MO_32: + return helper_stl_mmu; + case MO_64: + return helper_stq_mmu; + default: + g_assert_not_reached(); + } +} + +static void tcg_wasm_out_qemu_st(TCGContext *s, TCGReg data_reg, + TCGReg addr_reg, MemOpIdx oi) +{ + intptr_t helper_idx; + int64_t func_idx; + MemOp mop = get_memop(oi); + + helper_idx = (intptr_t)qemu_st_helper_ptr(oi); + func_idx = get_helper_idx(s, helper_idx); + if (func_idx < 0) { + func_idx = register_helper(s, helper_idx); + } + + /* call the target helper */ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(TCG_AREG0)); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(addr_reg)); + switch (mop & MO_SSIZE) { + case MO_UQ: + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(data_reg)); + break; + default: + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(data_reg)); + tcg_wasm_out_op(s, OPC_I32_WRAP_I64); + break; + } + tcg_wasm_out_op_const(s, OPC_I32_CONST, oi); + tcg_wasm_out_op_const(s, OPC_I64_CONST, (intptr_t)s->code_ptr); + + tcg_wasm_out_op_idx(s, OPC_CALL, func_idx); +} + static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, intptr_t value, intptr_t addend) {
@@ -2319,6 +2412,7 @@ static void tgen_qemu_ld(TCGContext *s, TCGType type, TCGReg data, TCGReg addr, MemOpIdx oi) { tcg_out_op_rrm(s, INDEX_op_qemu_ld, data, addr, oi); + tcg_wasm_out_qemu_ld(s, data, addr, oi); } static const TCGOutOpQemuLdSt outop_qemu_ld = {
@@ -2334,6 +2428,7 @@ static void tgen_qemu_st(TCGContext *s, TCGType type, TCGReg data, TCGReg addr, MemOpIdx oi) { tcg_out_op_rrm(s, INDEX_op_qemu_st, data, addr, oi); + tcg_wasm_out_qemu_st(s, data, addr, oi); } static const TCGOutOpQemuLdSt outop_qemu_st = {
Signature · 1 line
2.43.0
PATCH 30/35 tcg/wasm: Implement instantiation of Wasm binary
In reply to Kohei Tokunagainstantiate_wasm is a function that instantiates a TB's Wasm binary, importing the functions as specified by its arguments. Following the header definition in wasm/tcg-target.c.inc, QEMU's memory is imported into the module as "env.memory", and helper functions are imported as "helper.<idx>".
The instantiated Wasm module is imported to QEMU using Emscripten's "addFunction" feature[1] which returns a function pointer. This allows QEMU to call this module directly from C code via that pointer.
Since the subarray() method doesn't accept a BigInt value which is used for the 64bit pointer value, it is converted to a Number (i53) using bigintToI53Checked method of Emscripten. Although this conversion (64bit to 53bit) drops higher bits, the maximum memory size of the engine implementations is currently limited to 16GiB[2] so we can assume that the pointers are within the Number's range.
Note that since FireFox 138, WebAssembly.Module no longer accepts a SharedArrayBuffer as input [3] as reported by Nicolas Vandeginste in my fork[4]. This commit ensures that WebAssembly.Module() is passed a Uint8Array created from the binary data on a SharedArrayBuffer.
[1] https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html#calling-javascript-functions-as-function-pointers-from-c
[2] https://webassembly.github.io/memory64/js-api/#limits
[3] https://bugzilla.mozilla.org/show_bug.cgi?id=1965217
[4] https://github.com/ktock/qemu-wasm/pull/25
--- tcg/wasm.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+)
tcg/wasm.c +37 -0
@@ -25,6 +25,7 @@#include "disas/dis-asm.h" #include "tcg-has.h" #include <ffi.h> +#include <emscripten.h> #define ctpop_tr glue(ctpop, TCG_TARGET_REG_BITS)
@@ -44,6 +45,42 @@ __threaduintptr_t tci_tb_ptr; +#define EM_JS_PRE(ret, name, args, body...) EM_JS(ret, name, args, body) + +#define DEC_PTR (p) bigintToI53Checked(p) +#define ENC_PTR (p) BigInt(p) +#if defined(WASM64_MEMORY64_2 ) +#define ENC_WASM_TABLE_IDX (i) Number(i) +#else +#define ENC_WASM_TABLE_IDX (i) i +#endif + +EM_JS_PRE(void*, instantiate_wasm, (void *wasm_begin, + int wasm_size, + void *import_vec_begin, + int import_vec_size), +{ + const memory_v = new DataView(HEAP8.buffer); + const wasm = HEAP8.subarray(DEC_PTR(wasm_begin), +DEC_PTR (wasm_begin) + wasm_size); +var helper = {}; + const entsize = TCG_TARGET_REG_BITS / 8; +for (var i =0 ; i < import_vec_size / entsize; i++) { + const idx = memory_v.getBigInt64( +DEC_PTR (import_vec_begin) + i * entsize, true); + helper[i] = wasmTable.get (ENC_WASM_TABLE_IDX (idx)); + } + const mod = new WebAssembly.Module(new Uint8Array(wasm)); + const inst = new WebAssembly.Instance(mod, { + "env" : { + "memory" : wasmMemory, + }, + "helper" : helper, + }); + +return ENC_PTR (addFunction (inst.exports.start,'ii' )); +}); +static void tci_write_reg64 (tcg_target_ulong *regs,uint32_t high_index,uint32_t low_index,uint64_t value) {
Signature · 1 line
2.43.0
PATCH 35/35 .gitlab-ci.d: build wasm backend in CI
In reply to Kohei TokunagaThis commit adds the build tests for the wasm backend.
--- .gitlab-ci.d/buildtest.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
.gitlab-ci.d/buildtest.yml +2 -2
@@ -803,7 +803,7 @@ build-wasm64-64bit: job: wasm64-64bit-emsdk-cross-container variables: IMAGE: emsdk-wasm64-64bit-cross - CONFIGURE_ARGS: --static --cpu=wasm64 --disable-tools --enable-debug --enable-tcg-interpreter + CONFIGURE_ARGS: --static --cpu=wasm64 --disable-tools --enable-debug build-wasm64-32bit: extends: .wasm_build_job_template
@@ -812,4 +812,4 @@ build-wasm64-32bit: job: wasm64-32bit-emsdk-cross-container variables: IMAGE: emsdk-wasm64-32bit-cross - CONFIGURE_ARGS: --static --cpu=wasm64 --enable-wasm64-32bit-address-limit --disable-tools --enable-debug --enable-tcg-interpreter + CONFIGURE_ARGS: --static --cpu=wasm64 --enable-wasm64-32bit-address-limit --disable-tools --enable-debug
Signature · 1 line
2.43.0
PATCH 25/35 tcg/wasm: Add call instruction
In reply to Kohei TokunagaTo call QEMU functions from a TB (i.e. a Wasm module), those functions must be imported into the module.
Wasm's call instruction can invoke an imported function using a locally assigned function index. When a call TCG operation is generated, the Wasm backend assigns the ID (starting from 0) to the target function. The mapping between the function pointer and its assigned ID is recorded in a list of HelperInfo.
Since Wasm's call instruction requires arguments to be pushed onto the Wasm stack, the backend retrieves the function arguments from TCG's stack array and pushes them to the Wasm stack before the call. After the function returns, the result is retrieved from the Wasm stack and set in the corresponding TCG variable.
In the Emscripten build configured with !has_int128_type, a 128bit value is represented by the Int128 struct. Such values are passed to the function via pointer parameters and returned via a prepended pointer argument, as described in [1]. For this prepended buffer area, the module expects a pre-allocated Int128 buffer from the caller via ctx.buf128.
Helper functions expect the target of the return instruction via the GETPC macro (the tci_tb_ptr variable in TCI). However, unlike other architectures, Wasm doesn't have a register pointing to the return target. To emulate this behaviour, the Wasm module sets the instruction pointer to the corresponding TCI instruction (s->code_ptr) in tci_tb_ptr passed via the WasmContext.
--- tcg/wasm.h | 10 +++ tcg/wasm/tcg-target.c.inc | 147 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 157 insertions(+)
tcg/wasm.h +10 -0
@@ -12,6 +12,16 @@ struct WasmContext {* Pointer to the TB to be executed. */ void *tb_ptr; + +/* +* Pointer to the tci_tb_ptr variable. +*/ +void *tci_tb_ptr; + +/* +* Buffer to store 128bit return value on call. +*/ + void *buf128; };#endif
tcg/wasm/tcg-target.c.inc +147 -0
177 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH 29/35 tcg/wasm: Write wasm binary to TB
In reply to Kohei TokunagaThis commit updates tcg_out_tb_start and tcg_out_tb_end to emit Wasm binaries into the TB code buffer. The generated Wasm binary defines a function of type wasm_tb_func which takes a WasmContext, executes the TB, and returns a result. In the Wasm backend, each TB starts with a WasmTBHeader which contains pointers to the following data:
- TCI code
- Wasm code
- Array of helper function pointers imported into the Wasm instance
tcg_out_tb_start writes the WasmTBHeader to the code buffer. tcg_out_tb_end generates the full Wasm executable binary by creating the Wasm module header following the spec[1][2][3] and copying the Wasm code body from sub_buf to the TB. This Wasm binary is placed after the TCI code which was emitted earlier.
Additionally, an array of imported function pointers is appended to the TB. They are used during Wasm module instantiation. Function are imported to Wasm with names like "helper.0", "helper.1", etc., where the number corresponds to the array index.
Each function's type signature must also be encoded in the Wasm module header. To support this, every emission of "call", "qemu_ld" and "qemu_st" operations also records the target function's type information in a buffer which will be copied to the code buffer during tcg_out_tb_end.
Memory is shared between QEMU and the TBs and is imported to the Wasm module with the name "env.memory".
[1] https://webassembly.github.io/spec/core/binary/modules.html
[2] https://github.com/WebAssembly/threads/blob/b2567bff61ee6fbe731934f0ed17a5d48dc9ab01/proposals/threads/Overview.md
[3] https://github.com/WebAssembly/memory64/blob/9003cd5e24e53b84cd9027ea3dd7ae57159a6db1/proposals/memory64/Overview.md
--- tcg/wasm.h | 26 +++ tcg/wasm/tcg-target.c.inc | 406 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 432 insertions(+)
tcg/wasm.h +26 -0
@@ -34,4 +34,30 @@ struct WasmContext { uint64_t *stack; }; +/* Instantiated Wasm function of a TB */ +typedef uintptr_t (*wasm_tb_func )(struct WasmContext *); + +/* +* A TB of the Wasm backend starts from a header which contains pointers for +* each data stored in the following region in the TB. +*/ +struct WasmTBHeader { +/* +* Pointer to the region containing TCI instructions. +*/ +void *tci_ptr; + +/* +* Pointer to the region containing Wasm instructions. +*/ +void *wasm_ptr; +int wasm_size; + +/* +* Pointer to the array containing imported function pointers. +*/ +void *import_ptr; +int import_size; +}; +#endif
tcg/wasm/tcg-target.c.inc +406 -0
488 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH 05/35] tcg: Fork TCI for wasm backend
In reply to Kohei TokunagaQuoted 4 lines · Kohei Tokunaga
The Wasm backend is implemented based on the forked TCI backend with utilizing the TCI interpreter to execute TBs.
Signed-off-by Kohei Tokunaga <[email protected]>
This is the wrong way to go about things.
Don't copy the tci backend and replace it piece by piece. Start with a clean slate and add the pieces one at a time.
r~
Re: [PATCH 14/35] tcg/wasm: Add deposit/sextract/extract instrcutions
In reply to Kohei TokunagaQuoted 50 lines · Kohei Tokunaga
The
tcg_out_extractandtcg_out_sextractfunctions were used by several other functions (e.g. tcg_out_ext*) and intended to emit TCI code. So they have been renamed totcg_tci_out_extractandtcg_tci_out_sextract.Signed-off-by Kohei Tokunaga <[email protected]>--- tcg/wasm/tcg-target.c.inc | 104 +++++++++++++++++++++++++++++++++----- 1 file changed, 91 insertions(+), 13 deletions(-)tcg/wasm/tcg-target.c.inc +25
-0@@ -163,7 +163,10 @@ typedef enum { OPC_I64_SHR_U = 0x88, OPC_I32_WRAP_I64 = 0xa7, + OPC_I64_EXTEND_I32_S = 0xac, OPC_I64_EXTEND_I32_U = 0xad, + OPC_I64_EXTEND8_S = 0xc2, + OPC_I64_EXTEND16_S = 0xc3, } WasmInsn; typedef enum {@@ -380,6 +383,66 @@ static void tcg_wasm_out_movcond(TCGContext *s, TCGType type, TCGReg ret, tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); } +static void tcg_wasm_out_deposit(TCGContext *s, + TCGReg dest, TCGReg arg1, TCGReg arg2, + int pos, int len) +{ + int64_t mask = (((int64_t)1 << len) - 1) << pos; + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + tcg_wasm_out_op_const(s, OPC_I64_CONST, ~mask); + tcg_wasm_out_op(s, OPC_I64_AND); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg2)); + tcg_wasm_out_op_const(s, OPC_I64_CONST, pos); + tcg_wasm_out_op(s, OPC_I64_SHL); + tcg_wasm_out_op_const(s, OPC_I64_CONST, mask); + tcg_wasm_out_op(s, OPC_I64_AND); + tcg_wasm_out_op(s, OPC_I64_OR); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(dest)); +} + +static void tcg_wasm_out_extract(TCGContext *s, TCGReg dest, TCGReg arg1, + int pos, int len) +{ + int64_t mask = ~0ULL >> (64 - len); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1));
tcg/wasm/tcg-target.c.inc static void tcg_wasm_out_movcond(TCGContext *s, TCGType type, TCGReg ret, · line 417
Quoted 10 lines
tcg/wasm/tcg-target.c.inc +10
-0+ if (pos > 0) { + tcg_wasm_out_op_const(s, OPC_I64_CONST, pos); + tcg_wasm_out_op(s, OPC_I64_SHR_U); + } + if ((pos + len) < 64) { + tcg_wasm_out_op_const(s, OPC_I64_CONST, mask); + tcg_wasm_out_op(s, OPC_I64_AND); + } + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(dest)); +}
This is no better than the generic tcg expansion.
You should omit it.
Quoted 6 lines
tcg/wasm/tcg-target.c.inc +6
-0+ +static void tcg_wasm_out_sextract(TCGContext *s, TCGReg dest, TCGReg arg1, + int pos, int len) +{ + int discard = 64 - len; + int high = discard - pos;
tcg/wasm/tcg-target.c.inc static void tcg_wasm_out_movcond(TCGContext *s, TCGType type, TCGReg ret, · line 433
Quoted 10 lines
tcg/wasm/tcg-target.c.inc +10
-0+ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + + if ((pos == 0) && (len == 8)) { + tcg_wasm_out_op(s, OPC_I64_EXTEND8_S); + } else if ((pos == 0) && (len == 16)) { + tcg_wasm_out_op(s, OPC_I64_EXTEND16_S); + } else if ((pos == 0) && (len == 32)) { + tcg_wasm_out_op(s, OPC_I32_WRAP_I64); + tcg_wasm_out_op(s, OPC_I64_EXTEND_I32_S);
This is worth keeping.
Compare tcg/i386/tcg-target-has.h, tcg_target_sextract_valid.
r~
Re: [PATCH 18/35] tcg/wasm: Add bswap instructions
In reply to Kohei TokunagaQuoted 29 lines · Kohei Tokunaga
tcg/wasm/tcg-target.c.inc +29
-0+static void tcg_wasm_out_bswap64( + TCGContext *s, TCGReg dest, TCGReg src) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(src)); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 32); + tcg_wasm_out_op(s, OPC_I64_ROTR); + tcg_wasm_out_op_idx(s, OPC_LOCAL_SET, TMP64_LOCAL_0_IDX); + + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, TMP64_LOCAL_0_IDX); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 0xff000000ff000000); + tcg_wasm_out_op(s, OPC_I64_AND); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 24); + tcg_wasm_out_op(s, OPC_I64_SHR_U); + + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, TMP64_LOCAL_0_IDX); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 0x00ff000000ff0000); + tcg_wasm_out_op(s, OPC_I64_AND); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 8); + tcg_wasm_out_op(s, OPC_I64_SHR_U); + + tcg_wasm_out_op(s, OPC_I64_OR); + + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, TMP64_LOCAL_0_IDX); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 0x0000ff000000ff00); + tcg_wasm_out_op(s, OPC_I64_AND); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 8); + tcg_wasm_out_op(s, OPC_I64_SHL); + + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, TMP64_LOCAL_0_IDX);
tcg/wasm/tcg-target.c.inc static void tcg_wasm_out_sextract(TCGContext *s, TCGReg dest, TCGReg arg1, · line 575
Quoted 10 lines
tcg/wasm/tcg-target.c.inc +10
-0+ tcg_wasm_out_op_const(s, OPC_I64_CONST, 0x000000ff000000ff); + tcg_wasm_out_op(s, OPC_I64_AND); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 24); + tcg_wasm_out_op(s, OPC_I64_SHL); + + tcg_wasm_out_op(s, OPC_I64_OR); + + tcg_wasm_out_op(s, OPC_I64_OR); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(dest)); +}
Is this any better than the default expansion?
r~
Re: [PATCH 20/35] tcg/wasm: Add andc/orc/eqv/nand/nor instructions
In reply to Kohei TokunagaQuoted 15 lines · Kohei Tokunaga
This commit implements andc, orc, eqv, nand and nor operations using Wasm instructions.
Signed-off-by Kohei Tokunaga <[email protected]>--- tcg/wasm/tcg-target.c.inc | 55 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+)tcg/wasm/tcg-target.c.inc +0
-0@@ -449,6 +449,56 @@ static void tcg_wasm_out_cond( } }
tcg/wasm/tcg-target.c.inc static void tcg_wasm_out_cond( · line 460
Quoted 10 lines
tcg/wasm/tcg-target.c.inc +9
-0+static void tcg_wasm_out_andc( + TCGContext *s, TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg2)); + tcg_wasm_out_op_not(s); + tcg_wasm_out_op(s, OPC_I64_AND); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); +}
Don't implement stuff that's not present in the ISA. This will be handled generically.
r~
Re: [PATCH 21/35] tcg/wasm: Add neg/not/ctpop instructions
In reply to Kohei Tokunagatcg/wasm/tcg-target.c.inc static void tcg_wasm_out_nor( · line 510
Quoted 8 lines · Kohei Tokunaga
tcg/wasm/tcg-target.c.inc +8
-0+static void tcg_wasm_out_neg(TCGContext *s, TCGReg ret, TCGReg arg) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg)); + tcg_wasm_out_op_not(s); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 1); + tcg_wasm_out_op(s, OPC_I64_ADD); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); +}
This is an odd expansion. I would have expected ret = 0 - arg.
r~
Re: [PATCH 05/35] tcg: Fork TCI for wasm backend
In reply to Richard HendersonHi Richard, thank you for your review.
Quoted 10 lines · Kohei Tokunaga
Quoted 4 lines
The Wasm backend is implemented based on the forked TCI backend with utilizing the TCI interpreter to execute TBs.
Signed-off-by Kohei Tokunaga <[email protected]>This is the wrong way to go about things.
Don't copy the tci backend and replace it piece by piece. Start with a clean slate and add the pieces one at a time.
Thank you for the suggestion. I'll reorganize the patches and add the pieces one at a time, starting from scratch.
Regards,
Kohei
Re: [PATCH 14/35] tcg/wasm: Add deposit/sextract/extract instrcutions
In reply to Richard HendersonHi Richard, thank you for the feedback.
Quoted 3 lines · Kohei Tokunaga
Quoted 2 lines
The
tcg_out_extractandtcg_out_sextractfunctions were used by several other functions (e.g. tcg_out_ext*) and intended to emit TCI code. So
they
Quoted 21 lines
Quoted 21 lines
have been renamed to
tcg_tci_out_extractandtcg_tci_out_sextract.Signed-off-by Kohei Tokunaga <[email protected]>--- tcg/wasm/tcg-target.c.inc | 104 +++++++++++++++++++++++++++++++++----- 1 file changed, 91 insertions(+), 13 deletions(-)tcg/wasm/tcg-target.c.inc +3
-0@@ -163,7 +163,10 @@ typedef enum { OPC_I64_SHR_U = 0x88, OPC_I32_WRAP_I64 = 0xa7, + OPC_I64_EXTEND_I32_S = 0xac, OPC_I64_EXTEND_I32_U = 0xad, + OPC_I64_EXTEND8_S = 0xc2, + OPC_I64_EXTEND16_S = 0xc3, } WasmInsn;
Quoted 2 lines
tcg/wasm/tcg-target.c.inc static void tcg_wasm_out_movcond(TCGContext *s,
Quoted 2 lines
tcg/wasm/tcg-target.c.inc +0
-0typedef enum {@@ -380,6 +383,66 @@ static void tcg_wasm_out_movcond(TCGContext *s,
TCGType type, TCGReg ret,
Quoted 13 lines
Quoted 13 lines
tcg/wasm/tcg-target.c.inc +10
-0tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); } +static void tcg_wasm_out_deposit(TCGContext *s, + TCGReg dest, TCGReg arg1, TCGReg arg2, + int pos, int len) +{ + int64_t mask = (((int64_t)1 << len) - 1) << pos; + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + tcg_wasm_out_op_const(s, OPC_I64_CONST, ~mask); + tcg_wasm_out_op(s, OPC_I64_AND); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg2)); + tcg_wasm_out_op_const(s, OPC_I64_CONST, pos);
Quoted 8 lines
tcg/wasm/tcg-target.c.inc static void tcg_wasm_out_movcond(TCGContext *s, TCGType type, TCGReg ret,
Quoted 8 lines
tcg/wasm/tcg-target.c.inc +8
-0+ tcg_wasm_out_op(s, OPC_I64_SHL); + tcg_wasm_out_op_const(s, OPC_I64_CONST, mask); + tcg_wasm_out_op(s, OPC_I64_AND); + tcg_wasm_out_op(s, OPC_I64_OR); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(dest)); +} + +static void tcg_wasm_out_extract(TCGContext *s, TCGReg dest, TCGReg
arg1,
Quoted 18 lines
Quoted 4 lines
tcg/wasm/tcg-target.c.inc +4
-0+ int pos, int len) +{ + int64_t mask = ~0ULL >> (64 - len); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1));tcg/wasm/tcg-target.c.inc static void tcg_wasm_out_movcond(TCGContext *s, TCGType type, TCGReg ret, · line 417
Quoted 10 lines
tcg/wasm/tcg-target.c.inc +10
-0+ if (pos > 0) { + tcg_wasm_out_op_const(s, OPC_I64_CONST, pos); + tcg_wasm_out_op(s, OPC_I64_SHR_U); + } + if ((pos + len) < 64) { + tcg_wasm_out_op_const(s, OPC_I64_CONST, mask); + tcg_wasm_out_op(s, OPC_I64_AND); + } + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(dest)); +}This is no better than the generic tcg expansion.
You should omit it.
Quoted 2 lines
tcg/wasm/tcg-target.c.inc static void tcg_wasm_out_movcond(TCGContext *s, · line 419
Quoted 2 lines
tcg/wasm/tcg-target.c.inc +2
-0+ +static void tcg_wasm_out_sextract(TCGContext *s, TCGReg dest, TCGReg
arg1,
Quoted 9 lines
Quoted 9 lines
tcg/wasm/tcg-target.c.inc +9
-0+ int pos, int len) +{ + int discard = 64 - len; + int high = discard - pos; + + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + + if ((pos == 0) && (len == 8)) { + tcg_wasm_out_op(s, OPC_I64_EXTEND8_S);
Quoted 8 lines
tcg/wasm/tcg-target.c.inc static void tcg_wasm_out_movcond(TCGContext *s, TCGType type, TCGReg ret, · line 433
Quoted 5 lines
tcg/wasm/tcg-target.c.inc +5
-0+ } else if ((pos == 0) && (len == 16)) { + tcg_wasm_out_op(s, OPC_I64_EXTEND16_S); + } else if ((pos == 0) && (len == 32)) { + tcg_wasm_out_op(s, OPC_I32_WRAP_I64); + tcg_wasm_out_op(s, OPC_I64_EXTEND_I32_S);This is worth keeping.
Comparetcg/i386/tcg-target-has.h,tcg_target_sextract_valid.
I'll apply them in the next version of the patch series.
Regards,
Kohei
Re: [PATCH 18/35] tcg/wasm: Add bswap instructions
In reply to Richard HendersonHi Richard,
Quoted 34 lines · Kohei Tokunaga
Quoted 33 lines
tcg/wasm/tcg-target.c.inc +33
-0+static void tcg_wasm_out_bswap64( + TCGContext *s, TCGReg dest, TCGReg src) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(src)); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 32); + tcg_wasm_out_op(s, OPC_I64_ROTR); + tcg_wasm_out_op_idx(s, OPC_LOCAL_SET, TMP64_LOCAL_0_IDX); + + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, TMP64_LOCAL_0_IDX); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 0xff000000ff000000); + tcg_wasm_out_op(s, OPC_I64_AND); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 24); + tcg_wasm_out_op(s, OPC_I64_SHR_U); + + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, TMP64_LOCAL_0_IDX); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 0x00ff000000ff0000); + tcg_wasm_out_op(s, OPC_I64_AND); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 8); + tcg_wasm_out_op(s, OPC_I64_SHR_U); + + tcg_wasm_out_op(s, OPC_I64_OR); + + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, TMP64_LOCAL_0_IDX); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 0x0000ff000000ff00); + tcg_wasm_out_op(s, OPC_I64_AND); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 8); + tcg_wasm_out_op(s, OPC_I64_SHL); + + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, TMP64_LOCAL_0_IDX); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 0x000000ff000000ff); + tcg_wasm_out_op(s, OPC_I64_AND); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 24); + tcg_wasm_out_op(s, OPC_I64_SHL);
Quoted 8 lines
tcg/wasm/tcg-target.c.inc static void tcg_wasm_out_sextract(TCGContext *s, TCGReg dest, TCGReg arg1, · line 575
Quoted 6 lines
tcg/wasm/tcg-target.c.inc +6
-0+ + tcg_wasm_out_op(s, OPC_I64_OR); + + tcg_wasm_out_op(s, OPC_I64_OR); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(dest)); +}Is this any better than the default expansion?
Thank you for pointing this out. I think it can rely on the the default expansion, so I'll remove it in the next version of the patch series.
Regards,
Kohei
Re: [PATCH 21/35] tcg/wasm: Add neg/not/ctpop instructions
In reply to Richard HendersonHi Richard,
Quoted 11 lines · Kohei Tokunaga
tcg/wasm/tcg-target.c.inc static void tcg_wasm_out_nor( · line 510
Quoted 8 lines
tcg/wasm/tcg-target.c.inc +8
-0+static void tcg_wasm_out_neg(TCGContext *s, TCGReg ret, TCGReg arg) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg)); + tcg_wasm_out_op_not(s); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 1); + tcg_wasm_out_op(s, OPC_I64_ADD); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); +}This is an odd expansion. I would have expected ret = 0 - arg.
Thank you for the suggestion. I'll change this to ret = 0 - arg.
Regards,
Kohei
Re: [PATCH 20/35] tcg/wasm: Add andc/orc/eqv/nand/nor instructions
In reply to Richard HendersonHi Richard,
Quoted 2 lines · Kohei Tokunaga
Quoted 1 line
This commit implements andc, orc, eqv, nand and nor operations using
Wasm
Quoted 15 lines
Quoted 15 lines
instructions.
Signed-off-by Kohei Tokunaga <[email protected]>--- tcg/wasm/tcg-target.c.inc | 55 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+)tcg/wasm/tcg-target.c.inc +0
-0@@ -449,6 +449,56 @@ static void tcg_wasm_out_cond( } }
Quoted 12 lines
tcg/wasm/tcg-target.c.inc static void tcg_wasm_out_cond( · line 460
Quoted 9 lines
tcg/wasm/tcg-target.c.inc +9
-0+static void tcg_wasm_out_andc( + TCGContext *s, TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg2)); + tcg_wasm_out_op_not(s); + tcg_wasm_out_op(s, OPC_I64_AND); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); +}Don't implement stuff that's not present in the ISA. This will be handled generically.
Thank you for the feedback. I'll remove them and rely on the default expansion.
Regards,
Kohei