From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747745709; cv=none; d=zohomail.com; s=zohoarc; b=dkca5Xp06yDCIkn0Q/t02C53LjCwm3apUlPuIebBUKd+3PMr6RUeweMXZuheROMbHP7AUepJ8ISA2KxpJpwDPnr+YirX6HEAU8wKsgu1ILSisH7jN6tgvVeSaQaHa4P9DogmvWWbLQcRhrglWIbEmTbZHL7fI4E9qpfZgPpMPn0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747745709; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=vekLxTveVMbhs50pP7sG5QPtXmnNSLOY0DjxSOEGfC4=; b=eTvyID4bnsoFslRj1eNtSkxcvi0Y9A9Ysr5tChRX/4U3XK53wf/fa1BNeBbVyyzrEt3SOseJTd3zDoAwMLocyOjGbACK69A2zgRR1gVIt7MKSNOVxXoBOHafWGt6k1NT6XQvhntZAYNMDj70YzNVKHF1aFZNNlrrFsqZpIg33vs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747745709166775.2018447588516; Tue, 20 May 2025 05:55:09 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMSR-0000g2-01; Tue, 20 May 2025 08:52:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMSO-0000fR-N8; Tue, 20 May 2025 08:52:32 -0400 Received: from mail-pg1-x52d.google.com ([2607:f8b0:4864:20::52d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMSI-0000ec-L8; Tue, 20 May 2025 08:52:31 -0400 Received: by mail-pg1-x52d.google.com with SMTP id 41be03b00d2f7-7fd581c2bf4so4467565a12.3; Tue, 20 May 2025 05:52:23 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.52.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:52:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745542; x=1748350342; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=vekLxTveVMbhs50pP7sG5QPtXmnNSLOY0DjxSOEGfC4=; b=aHFv7G4EmiEdZxJbZUVYOH95y8Ij7GvQXb/eXNwoqrP9BvynCZR2MlSkefWCbgd281 CVDbst+Zhbjp8CNNJY8ItchYOEfZRXlyDsPErl8qN0kOceOmFtLZFTWkFJ/YEgXkEWlE s2VrwZM50qwAEYxpBLTLGntQKI9+FmxkRQjO1b+8qo8PfAvyuzo5T6wePFUuvQPuWl+J FEmXKFyTBWoRVs0Fy+J5mKRlziyuLfLbWvq4PIGeT8O0GdqXQwm7pcL7HZSYNjAmNQ3c pT5QZPE0ImPCNL+//Z3mWsXFqKOJOeKdOnDz9VOQ9obj0OdozvN7kNzOeekIcf8nSCHK C25Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745542; x=1748350342; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vekLxTveVMbhs50pP7sG5QPtXmnNSLOY0DjxSOEGfC4=; b=ZBa+5iG3/ElB4Y2/zWHG+/5y7qcnl0ZM4+dcz7XWSQt+Jd37VvziZwQWUN6ZOo8jBV J+JfAoUczEN7D4j0KxyfwG90G5Z9mAc7pKzmNa9NKJjc9oXMgObG3VQceseBx0zaHfW4 n46f2Ki1y3izpwXSmuIDfguEE+nkTjoza4TfjNWoAoau9LwiW4f79hevNW3Aq92fy+AO PFuAalXV5HLxeIukzxm3sa8iJk4CyrZC0zFpk+SHWV1Kz8O4ODiLhZBryXEH1tJFBLfc sCHcXwUA0e2sI2pG7Uw52RM/4UC6H30+eFsj9zIu5bjLZSkQ5lmY8hthf3xbMHRGQVTC 6RFQ== X-Forwarded-Encrypted: i=1; AJvYcCVpXZd+c+W6hJxiUdS2lC4nqe6SvHN2XgoH6qMWfsyHuiI9j2aO8YWkE0i7n+CbBAxwJLC+iMk0dA==@nongnu.org, AJvYcCWI/Yq6QOsX1k0M7GHFBQT5io+O97IrWYMSbwceigNtQkBie0hkX5FXFD7zxPoOa6UIOK+DaFms/7/OIg==@nongnu.org X-Gm-Message-State: AOJu0Yy6gIKeIyAzRBdrO7/u4okGT5IHPhOg2MEHAa5TrU9/KYpRB0U6 UyIxGhu+E++n44SZgVoz65AC1NkkjPA0GVgYt+G5Zdprozbtdja6M7TTs0bpy6as X-Gm-Gg: ASbGncvOsEuYBesEuL9+7bnoKmP5S+NgDAaLwUGKLcV4m+5AzTEJ9wPRWam4zynprh+ xPcWE3USOIUnx/tzvHS+WEVGJ7wyji62gwtxpouYA8w5qtmaX3N7bwPBNeq01nR9v5142bl8hNr 2QMNQFTGpXzSRZ1NlkDD7TLFGimOSFOp4oNiHuqXCoac2AXA2iO14NQwXa4u2b8GVeoQpQ2wHtc CooP0O533qKkLbWhrp0kdNOpE8AgbyvY+l66khQ6dBQQyUK0ADiR4I4d2Xl6rnssiiAhjoO829g 6njS+LxfG/xj2e8MTqDSpA15Qx/n4xz0A5dhtNlDxlSix127wiM= X-Google-Smtp-Source: AGHT+IFt/FnFv1LFOJ54Gf7OfT6kFDlsJn/3w/W5Aj4TCdXPtUnFEuBFLjXKSluu9zgU9ztVaLi33w== X-Received: by 2002:a17:90b:1dd1:b0:305:5f32:d9f5 with SMTP id 98e67ed59e1d1-30e7d4f9220mr28773559a91.7.1747745541518; Tue, 20 May 2025 05:52:21 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 01/33] tcg: Fork TCI for wasm32 backend Date: Tue, 20 May 2025 21:51:03 +0900 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::52d; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pg1-x52d.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747745710694116600 Content-Type: text/plain; charset="utf-8" Wasm backend is implemented based on the TCI backend and utilizes a forked TCI to execute TBs. Signed-off-by: Kohei Tokunaga --- MAINTAINERS | 6 + include/accel/tcg/getpc.h | 2 +- include/tcg/helper-info.h | 4 +- include/tcg/tcg.h | 2 +- tcg/meson.build | 5 + tcg/region.c | 10 +- tcg/tcg.c | 16 +- tcg/wasm32.c | 1076 ++++++++++++++++++++++++ tcg/wasm32/tcg-target-con-set.h | 21 + tcg/wasm32/tcg-target-con-str.h | 11 + tcg/wasm32/tcg-target-has.h | 22 + tcg/wasm32/tcg-target-mo.h | 17 + tcg/wasm32/tcg-target-opc.h.inc | 15 + tcg/wasm32/tcg-target-reg-bits.h | 18 + tcg/wasm32/tcg-target.c.inc | 1320 ++++++++++++++++++++++++++++++ tcg/wasm32/tcg-target.h | 76 ++ 16 files changed, 2604 insertions(+), 17 deletions(-) create mode 100644 tcg/wasm32.c create mode 100644 tcg/wasm32/tcg-target-con-set.h create mode 100644 tcg/wasm32/tcg-target-con-str.h create mode 100644 tcg/wasm32/tcg-target-has.h create mode 100644 tcg/wasm32/tcg-target-mo.h create mode 100644 tcg/wasm32/tcg-target-opc.h.inc create mode 100644 tcg/wasm32/tcg-target-reg-bits.h create mode 100644 tcg/wasm32/tcg-target.c.inc create mode 100644 tcg/wasm32/tcg-target.h diff --git a/MAINTAINERS b/MAINTAINERS index 7060cf49b9..ac5070d058 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -3948,6 +3948,12 @@ F: tcg/tci/ F: tcg/tci.c F: disas/tci.c =20 +WebAssembly TCG target +M: Kohei Tokunaga +S: Maintained +F: tcg/wasm32/ +F: tcg/wasm32.c + Block drivers ------------- VMDK diff --git a/include/accel/tcg/getpc.h b/include/accel/tcg/getpc.h index 0fc08addcf..3901655715 100644 --- a/include/accel/tcg/getpc.h +++ b/include/accel/tcg/getpc.h @@ -9,7 +9,7 @@ #define ACCEL_TCG_GETPC_H =20 /* 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 __thread uintptr_t tci_tb_ptr; # define GETPC() tci_tb_ptr #else diff --git a/include/tcg/helper-info.h b/include/tcg/helper-info.h index 909fe73afa..9b4e8832a8 100644 --- a/include/tcg/helper-info.h +++ b/include/tcg/helper-info.h @@ -9,7 +9,7 @@ #ifndef TCG_HELPER_INFO_H #define TCG_HELPER_INFO_H =20 -#ifdef CONFIG_TCG_INTERPRETER +#if defined(CONFIG_TCG_INTERPRETER) || defined(EMSCRIPTEN) #include #endif #include "tcg-target-reg-bits.h" @@ -48,7 +48,7 @@ struct TCGHelperInfo { const char *name; =20 /* Used with g_once_init_enter. */ -#ifdef CONFIG_TCG_INTERPRETER +#if defined(CONFIG_TCG_INTERPRETER) || defined(EMSCRIPTEN) ffi_cif *cif; #else uintptr_t init; diff --git a/include/tcg/tcg.h b/include/tcg/tcg.h index 3fa5a7aed2..041d8035bc 100644 --- a/include/tcg/tcg.h +++ b/include/tcg/tcg.h @@ -967,7 +967,7 @@ static inline size_t tcg_current_code_size(TCGContext *= s) #define TB_EXIT_IDXMAX 1 #define TB_EXIT_REQUESTED 3 =20 -#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); diff --git a/tcg/meson.build b/tcg/meson.build index bd2821e4b5..a20acfd44f 100644 --- a/tcg/meson.build +++ b/tcg/meson.build @@ -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 =3D=3D 'emscripten' + libffi =3D dependency('libffi', version: '>=3D3.0', required: true, + method: 'pkg-config') + specific_ss.add(libffi) + specific_ss.add(files('wasm32.c')) endif =20 tcg_ss.add(when: libdw, if_true: files('debuginfo.c')) diff --git a/tcg/region.c b/tcg/region.c index 7ea0b37a84..68cb6f18b7 100644 --- a/tcg/region.c +++ b/tcg/region.c @@ -94,7 +94,7 @@ bool in_code_gen_buffer(const void *p) return (size_t)(p - region.start_aligned) <=3D region.total_size; } =20 -#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; } =20 -#ifndef CONFIG_TCG_INTERPRETER +#if !defined(CONFIG_TCG_INTERPRETER) && !defined(EMSCRIPTEN) #ifdef CONFIG_POSIX #include "qemu/memfd.h" =20 @@ -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 */ =20 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, unsig= ned max_threads) * Work with the page protections set up with the initial mapping. */ need_prot =3D PROT_READ | PROT_WRITE; -#ifndef CONFIG_TCG_INTERPRETER +#if !defined(CONFIG_TCG_INTERPRETER) && !defined(EMSCRIPTEN) if (tcg_splitwx_diff =3D=3D 0) { need_prot |=3D host_prot_read_exec(); } diff --git a/tcg/tcg.c b/tcg/tcg.c index ae27a2607d..2746458a64 100644 --- a/tcg/tcg.c +++ b/tcg/tcg.c @@ -253,7 +253,7 @@ TCGv_env tcg_env; const void *tcg_code_gen_epilogue; uintptr_t tcg_splitwx_diff; =20 -#ifndef CONFIG_TCG_INTERPRETER +#if !defined(CONFIG_TCG_INTERPRETER) && !defined(EMSCRIPTEN) tcg_prologue_fn *tcg_qemu_tb_exec; #endif =20 @@ -1117,7 +1117,7 @@ typedef struct TCGOutOpSubtract { =20 #include "tcg-target.c.inc" =20 -#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)) @@ -1438,7 +1438,7 @@ static TCGHelperInfo info_helper_st128_mmu =3D { | dh_typemask(ptr, 5) /* uintptr_t ra */ }; =20 -#ifdef CONFIG_TCG_INTERPRETER +#if defined(CONFIG_TCG_INTERPRETER) || defined(EMSCRIPTEN) static ffi_type *typecode_to_ffi(int argmask) { /* @@ -1515,7 +1515,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 */ =20 static inline bool arg_slot_reg_p(unsigned arg_slot) { @@ -1892,7 +1892,7 @@ void tcg_prologue_init(void) s->code_buf =3D s->code_gen_ptr; s->data_gen_ptr =3D NULL; =20 -#ifndef CONFIG_TCG_INTERPRETER +#if !defined(CONFIG_TCG_INTERPRETER) && !defined(EMSCRIPTEN) tcg_qemu_tb_exec =3D (tcg_prologue_fn *)tcg_splitwx_to_rx(s->code_ptr); #endif =20 @@ -1911,7 +1911,7 @@ void tcg_prologue_init(void) prologue_size =3D tcg_current_code_size(s); perf_report_prologue(s->code_gen_ptr, prologue_size); =20 -#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 @@ -1948,7 +1948,7 @@ void tcg_prologue_init(void) } } =20 -#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, @@ -7046,7 +7046,7 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb,= uint64_t pc_start) return -2; } =20 -#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, diff --git a/tcg/wasm32.c b/tcg/wasm32.c new file mode 100644 index 0000000000..6de9b26b76 --- /dev/null +++ b/tcg/wasm32.c @@ -0,0 +1,1076 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * WebAssembly backend with forked TCI, based on tci.c + * + * Copyright (c) 2009, 2011, 2016 Stefan Weil + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "qemu/osdep.h" +#include "tcg/tcg.h" +#include "tcg/helper-info.h" +#include "tcg/tcg-ldst.h" +#include "disas/dis-asm.h" +#include "tcg-has.h" +#include + + +#define ctpop_tr glue(ctpop, TCG_TARGET_REG_BITS) +#define deposit_tr glue(deposit, TCG_TARGET_REG_BITS) +#define extract_tr glue(extract, TCG_TARGET_REG_BITS) +#define sextract_tr glue(sextract, TCG_TARGET_REG_BITS) + +/* + * Enable TCI assertions only when debugging TCG (and without NDEBUG defin= ed). + * Without assertions, the interpreter runs much faster. + */ +#if defined(CONFIG_DEBUG_TCG) +# define tci_assert(cond) assert(cond) +#else +# define tci_assert(cond) ((void)(cond)) +#endif + +__thread uintptr_t tci_tb_ptr; + +static void tci_write_reg64(tcg_target_ulong *regs, uint32_t high_index, + uint32_t low_index, uint64_t value) +{ + regs[low_index] =3D (uint32_t)value; + regs[high_index] =3D value >> 32; +} + +/* Create a 64 bit value from two 32 bit values. */ +static uint64_t tci_uint64(uint32_t high, uint32_t low) +{ + return ((uint64_t)high << 32) + low; +} + +/* + * Load sets of arguments all at once. The naming convention is: + * tci_args_ + * where arguments is a sequence of + * + * b =3D immediate (bit position) + * c =3D condition (TCGCond) + * i =3D immediate (uint32_t) + * I =3D immediate (tcg_target_ulong) + * l =3D label or pointer + * m =3D immediate (MemOpIdx) + * n =3D immediate (call return length) + * r =3D register + * s =3D signed ldst offset + */ + +static void tci_args_l(uint32_t insn, const void *tb_ptr, void **l0) +{ + int diff =3D sextract32(insn, 12, 20); + *l0 =3D diff ? (void *)tb_ptr + diff : NULL; +} + +static void tci_args_r(uint32_t insn, TCGReg *r0) +{ + *r0 =3D extract32(insn, 8, 4); +} + +static void tci_args_nl(uint32_t insn, const void *tb_ptr, + uint8_t *n0, void **l1) +{ + *n0 =3D extract32(insn, 8, 4); + *l1 =3D sextract32(insn, 12, 20) + (void *)tb_ptr; +} + +static void tci_args_rl(uint32_t insn, const void *tb_ptr, + TCGReg *r0, void **l1) +{ + *r0 =3D extract32(insn, 8, 4); + *l1 =3D sextract32(insn, 12, 20) + (void *)tb_ptr; +} + +static void tci_args_rr(uint32_t insn, TCGReg *r0, TCGReg *r1) +{ + *r0 =3D extract32(insn, 8, 4); + *r1 =3D extract32(insn, 12, 4); +} + +static void tci_args_ri(uint32_t insn, TCGReg *r0, tcg_target_ulong *i1) +{ + *r0 =3D extract32(insn, 8, 4); + *i1 =3D sextract32(insn, 12, 20); +} + +static void tci_args_rrm(uint32_t insn, TCGReg *r0, + TCGReg *r1, MemOpIdx *m2) +{ + *r0 =3D extract32(insn, 8, 4); + *r1 =3D extract32(insn, 12, 4); + *m2 =3D extract32(insn, 16, 16); +} + +static void tci_args_rrr(uint32_t insn, TCGReg *r0, TCGReg *r1, TCGReg *r2) +{ + *r0 =3D extract32(insn, 8, 4); + *r1 =3D extract32(insn, 12, 4); + *r2 =3D extract32(insn, 16, 4); +} + +static void tci_args_rrs(uint32_t insn, TCGReg *r0, TCGReg *r1, int32_t *i= 2) +{ + *r0 =3D extract32(insn, 8, 4); + *r1 =3D extract32(insn, 12, 4); + *i2 =3D sextract32(insn, 16, 16); +} + +static void tci_args_rrbb(uint32_t insn, TCGReg *r0, TCGReg *r1, + uint8_t *i2, uint8_t *i3) +{ + *r0 =3D extract32(insn, 8, 4); + *r1 =3D extract32(insn, 12, 4); + *i2 =3D extract32(insn, 16, 6); + *i3 =3D extract32(insn, 22, 6); +} + +static void tci_args_rrrc(uint32_t insn, + TCGReg *r0, TCGReg *r1, TCGReg *r2, TCGCond *c3) +{ + *r0 =3D extract32(insn, 8, 4); + *r1 =3D extract32(insn, 12, 4); + *r2 =3D extract32(insn, 16, 4); + *c3 =3D extract32(insn, 20, 4); +} + +static void tci_args_rrrbb(uint32_t insn, TCGReg *r0, TCGReg *r1, + TCGReg *r2, uint8_t *i3, uint8_t *i4) +{ + *r0 =3D extract32(insn, 8, 4); + *r1 =3D extract32(insn, 12, 4); + *r2 =3D extract32(insn, 16, 4); + *i3 =3D extract32(insn, 20, 6); + *i4 =3D extract32(insn, 26, 6); +} + +static void tci_args_rrrr(uint32_t insn, + TCGReg *r0, TCGReg *r1, TCGReg *r2, TCGReg *r3) +{ + *r0 =3D extract32(insn, 8, 4); + *r1 =3D extract32(insn, 12, 4); + *r2 =3D extract32(insn, 16, 4); + *r3 =3D extract32(insn, 20, 4); +} + +static void tci_args_rrrrrc(uint32_t insn, TCGReg *r0, TCGReg *r1, + TCGReg *r2, TCGReg *r3, TCGReg *r4, TCGCond *c= 5) +{ + *r0 =3D extract32(insn, 8, 4); + *r1 =3D extract32(insn, 12, 4); + *r2 =3D extract32(insn, 16, 4); + *r3 =3D extract32(insn, 20, 4); + *r4 =3D extract32(insn, 24, 4); + *c5 =3D extract32(insn, 28, 4); +} + +static bool tci_compare32(uint32_t u0, uint32_t u1, TCGCond condition) +{ + bool result =3D false; + int32_t i0 =3D u0; + int32_t i1 =3D u1; + switch (condition) { + case TCG_COND_EQ: + result =3D (u0 =3D=3D u1); + break; + case TCG_COND_NE: + result =3D (u0 !=3D u1); + break; + case TCG_COND_LT: + result =3D (i0 < i1); + break; + case TCG_COND_GE: + result =3D (i0 >=3D i1); + break; + case TCG_COND_LE: + result =3D (i0 <=3D i1); + break; + case TCG_COND_GT: + result =3D (i0 > i1); + break; + case TCG_COND_LTU: + result =3D (u0 < u1); + break; + case TCG_COND_GEU: + result =3D (u0 >=3D u1); + break; + case TCG_COND_LEU: + result =3D (u0 <=3D u1); + break; + case TCG_COND_GTU: + result =3D (u0 > u1); + break; + case TCG_COND_TSTEQ: + result =3D (u0 & u1) =3D=3D 0; + break; + case TCG_COND_TSTNE: + result =3D (u0 & u1) !=3D 0; + break; + default: + g_assert_not_reached(); + } + return result; +} + +static bool tci_compare64(uint64_t u0, uint64_t u1, TCGCond condition) +{ + bool result =3D false; + int64_t i0 =3D u0; + int64_t i1 =3D u1; + switch (condition) { + case TCG_COND_EQ: + result =3D (u0 =3D=3D u1); + break; + case TCG_COND_NE: + result =3D (u0 !=3D u1); + break; + case TCG_COND_LT: + result =3D (i0 < i1); + break; + case TCG_COND_GE: + result =3D (i0 >=3D i1); + break; + case TCG_COND_LE: + result =3D (i0 <=3D i1); + break; + case TCG_COND_GT: + result =3D (i0 > i1); + break; + case TCG_COND_LTU: + result =3D (u0 < u1); + break; + case TCG_COND_GEU: + result =3D (u0 >=3D u1); + break; + case TCG_COND_LEU: + result =3D (u0 <=3D u1); + break; + case TCG_COND_GTU: + result =3D (u0 > u1); + break; + case TCG_COND_TSTEQ: + result =3D (u0 & u1) =3D=3D 0; + break; + case TCG_COND_TSTNE: + result =3D (u0 & u1) !=3D 0; + break; + default: + g_assert_not_reached(); + } + return result; +} + +static uint64_t tci_qemu_ld(CPUArchState *env, uint64_t taddr, + MemOpIdx oi, const void *tb_ptr) +{ + MemOp mop =3D get_memop(oi); + uintptr_t ra =3D (uintptr_t)tb_ptr; + + switch (mop & MO_SSIZE) { + case MO_UB: + return helper_ldub_mmu(env, taddr, oi, ra); + case MO_SB: + return helper_ldsb_mmu(env, taddr, oi, ra); + case MO_UW: + return helper_lduw_mmu(env, taddr, oi, ra); + case MO_SW: + return helper_ldsw_mmu(env, taddr, oi, ra); + case MO_UL: + return helper_ldul_mmu(env, taddr, oi, ra); + case MO_SL: + return helper_ldsl_mmu(env, taddr, oi, ra); + case MO_UQ: + return helper_ldq_mmu(env, taddr, oi, ra); + default: + g_assert_not_reached(); + } +} + +static void tci_qemu_st(CPUArchState *env, uint64_t taddr, uint64_t val, + MemOpIdx oi, const void *tb_ptr) +{ + MemOp mop =3D get_memop(oi); + uintptr_t ra =3D (uintptr_t)tb_ptr; + + switch (mop & MO_SIZE) { + case MO_UB: + helper_stb_mmu(env, taddr, val, oi, ra); + break; + case MO_UW: + helper_stw_mmu(env, taddr, val, oi, ra); + break; + case MO_UL: + helper_stl_mmu(env, taddr, val, oi, ra); + break; + case MO_UQ: + helper_stq_mmu(env, taddr, val, oi, ra); + break; + default: + g_assert_not_reached(); + } +} + +/* 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) +{ + const uint32_t *tb_ptr =3D v_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)]; + bool carry =3D false; + + regs[TCG_AREG0] =3D (tcg_target_ulong)env; + regs[TCG_REG_CALL_STACK] =3D (uintptr_t)stack; + tci_assert(tb_ptr); + + for (;;) { + uint32_t insn; + TCGOpcode opc; + TCGReg r0, r1, r2, r3, r4; + tcg_target_ulong t1; + TCGCond condition; + uint8_t pos, len; + uint32_t tmp32; + uint64_t tmp64, taddr; + MemOpIdx oi; + int32_t ofs; + void *ptr; + + insn =3D *tb_ptr++; + opc =3D extract32(insn, 0, 8); + + switch (opc) { + case INDEX_op_call: + { + void *call_slots[MAX_CALL_IARGS]; + ffi_cif *cif; + void *func; + unsigned i, s, n; + + tci_args_nl(insn, tb_ptr, &len, &ptr); + func =3D ((void **)ptr)[0]; + cif =3D ((void **)ptr)[1]; + + n =3D cif->nargs; + for (i =3D s =3D 0; i < n; ++i) { + ffi_type *t =3D cif->arg_types[i]; + call_slots[i] =3D &stack[s]; + s +=3D DIV_ROUND_UP(t->size, 8); + } + + /* Helper functions may need to access the "return address= " */ + tci_tb_ptr =3D (uintptr_t)tb_ptr; + ffi_call(cif, func, stack, call_slots); + } + + switch (len) { + case 0: /* void */ + break; + case 1: /* uint32_t */ + /* + * The result winds up "left-aligned" in the stack[0] slot. + * Note that libffi has an odd special case in that it will + * always widen an integral result to ffi_arg. + */ + if (sizeof(ffi_arg) =3D=3D 8) { + regs[TCG_REG_R0] =3D (uint32_t)stack[0]; + } else { + regs[TCG_REG_R0] =3D *(uint32_t *)stack; + } + break; + case 2: /* uint64_t */ + /* + * For TCG_TARGET_REG_BITS =3D=3D 32, the register pair + * must stay in host memory order. + */ + memcpy(®s[TCG_REG_R0], stack, 8); + break; + case 3: /* Int128 */ + memcpy(®s[TCG_REG_R0], stack, 16); + break; + default: + g_assert_not_reached(); + } + break; + + case INDEX_op_br: + tci_args_l(insn, tb_ptr, &ptr); + tb_ptr =3D ptr; + continue; +#if TCG_TARGET_REG_BITS =3D=3D 32 + case INDEX_op_setcond2_i32: + tci_args_rrrrrc(insn, &r0, &r1, &r2, &r3, &r4, &condition); + regs[r0] =3D tci_compare64(tci_uint64(regs[r2], regs[r1]), + tci_uint64(regs[r4], regs[r3]), + condition); + break; +#elif TCG_TARGET_REG_BITS =3D=3D 64 + case INDEX_op_setcond: + tci_args_rrrc(insn, &r0, &r1, &r2, &condition); + regs[r0] =3D tci_compare64(regs[r1], regs[r2], condition); + break; + case INDEX_op_movcond: + tci_args_rrrrrc(insn, &r0, &r1, &r2, &r3, &r4, &condition); + tmp32 =3D tci_compare64(regs[r1], regs[r2], condition); + regs[r0] =3D regs[tmp32 ? r3 : r4]; + break; +#endif + case INDEX_op_mov: + tci_args_rr(insn, &r0, &r1); + regs[r0] =3D regs[r1]; + break; + case INDEX_op_tci_movi: + tci_args_ri(insn, &r0, &t1); + regs[r0] =3D t1; + break; + case INDEX_op_tci_movl: + tci_args_rl(insn, tb_ptr, &r0, &ptr); + regs[r0] =3D *(tcg_target_ulong *)ptr; + break; + case INDEX_op_tci_setcarry: + carry =3D true; + break; + + /* Load/store operations (32 bit). */ + + case INDEX_op_ld8u: + tci_args_rrs(insn, &r0, &r1, &ofs); + ptr =3D (void *)(regs[r1] + ofs); + regs[r0] =3D *(uint8_t *)ptr; + break; + case INDEX_op_ld8s: + tci_args_rrs(insn, &r0, &r1, &ofs); + ptr =3D (void *)(regs[r1] + ofs); + regs[r0] =3D *(int8_t *)ptr; + break; + case INDEX_op_ld16u: + tci_args_rrs(insn, &r0, &r1, &ofs); + ptr =3D (void *)(regs[r1] + ofs); + regs[r0] =3D *(uint16_t *)ptr; + break; + case INDEX_op_ld16s: + tci_args_rrs(insn, &r0, &r1, &ofs); + ptr =3D (void *)(regs[r1] + ofs); + regs[r0] =3D *(int16_t *)ptr; + break; + case INDEX_op_ld: + tci_args_rrs(insn, &r0, &r1, &ofs); + ptr =3D (void *)(regs[r1] + ofs); + regs[r0] =3D *(tcg_target_ulong *)ptr; + break; + case INDEX_op_st8: + tci_args_rrs(insn, &r0, &r1, &ofs); + ptr =3D (void *)(regs[r1] + ofs); + *(uint8_t *)ptr =3D regs[r0]; + break; + case INDEX_op_st16: + tci_args_rrs(insn, &r0, &r1, &ofs); + ptr =3D (void *)(regs[r1] + ofs); + *(uint16_t *)ptr =3D regs[r0]; + break; + case INDEX_op_st: + tci_args_rrs(insn, &r0, &r1, &ofs); + ptr =3D (void *)(regs[r1] + ofs); + *(tcg_target_ulong *)ptr =3D regs[r0]; + break; + + /* Arithmetic operations (mixed 32/64 bit). */ + + case INDEX_op_add: + tci_args_rrr(insn, &r0, &r1, &r2); + regs[r0] =3D regs[r1] + regs[r2]; + break; + case INDEX_op_sub: + tci_args_rrr(insn, &r0, &r1, &r2); + regs[r0] =3D regs[r1] - regs[r2]; + break; + case INDEX_op_mul: + tci_args_rrr(insn, &r0, &r1, &r2); + regs[r0] =3D regs[r1] * regs[r2]; + break; + case INDEX_op_and: + tci_args_rrr(insn, &r0, &r1, &r2); + regs[r0] =3D regs[r1] & regs[r2]; + break; + case INDEX_op_or: + tci_args_rrr(insn, &r0, &r1, &r2); + regs[r0] =3D regs[r1] | regs[r2]; + break; + case INDEX_op_xor: + tci_args_rrr(insn, &r0, &r1, &r2); + regs[r0] =3D regs[r1] ^ regs[r2]; + break; + case INDEX_op_andc: + tci_args_rrr(insn, &r0, &r1, &r2); + regs[r0] =3D regs[r1] & ~regs[r2]; + break; + case INDEX_op_orc: + tci_args_rrr(insn, &r0, &r1, &r2); + regs[r0] =3D regs[r1] | ~regs[r2]; + break; + case INDEX_op_eqv: + tci_args_rrr(insn, &r0, &r1, &r2); + regs[r0] =3D ~(regs[r1] ^ regs[r2]); + break; + case INDEX_op_nand: + tci_args_rrr(insn, &r0, &r1, &r2); + regs[r0] =3D ~(regs[r1] & regs[r2]); + break; + case INDEX_op_nor: + tci_args_rrr(insn, &r0, &r1, &r2); + regs[r0] =3D ~(regs[r1] | regs[r2]); + break; + case INDEX_op_neg: + tci_args_rr(insn, &r0, &r1); + regs[r0] =3D -regs[r1]; + break; + case INDEX_op_not: + tci_args_rr(insn, &r0, &r1); + regs[r0] =3D ~regs[r1]; + break; + case INDEX_op_ctpop: + tci_args_rr(insn, &r0, &r1); + regs[r0] =3D ctpop_tr(regs[r1]); + break; + case INDEX_op_addco: + tci_args_rrr(insn, &r0, &r1, &r2); + t1 =3D regs[r1] + regs[r2]; + carry =3D t1 < regs[r1]; + regs[r0] =3D t1; + break; + case INDEX_op_addci: + tci_args_rrr(insn, &r0, &r1, &r2); + regs[r0] =3D regs[r1] + regs[r2] + carry; + break; + case INDEX_op_addcio: + tci_args_rrr(insn, &r0, &r1, &r2); + if (carry) { + t1 =3D regs[r1] + regs[r2] + 1; + carry =3D t1 <=3D regs[r1]; + } else { + t1 =3D regs[r1] + regs[r2]; + carry =3D t1 < regs[r1]; + } + regs[r0] =3D t1; + break; + case INDEX_op_subbo: + tci_args_rrr(insn, &r0, &r1, &r2); + carry =3D regs[r1] < regs[r2]; + regs[r0] =3D regs[r1] - regs[r2]; + break; + case INDEX_op_subbi: + tci_args_rrr(insn, &r0, &r1, &r2); + regs[r0] =3D regs[r1] - regs[r2] - carry; + break; + case INDEX_op_subbio: + tci_args_rrr(insn, &r0, &r1, &r2); + if (carry) { + carry =3D regs[r1] <=3D regs[r2]; + regs[r0] =3D regs[r1] - regs[r2] - 1; + } else { + carry =3D regs[r1] < regs[r2]; + regs[r0] =3D regs[r1] - regs[r2]; + } + break; + case INDEX_op_muls2: + tci_args_rrrr(insn, &r0, &r1, &r2, &r3); +#if TCG_TARGET_REG_BITS =3D=3D 32 + tmp64 =3D (int64_t)(int32_t)regs[r2] * (int32_t)regs[r3]; + tci_write_reg64(regs, r1, r0, tmp64); +#else + muls64(®s[r0], ®s[r1], regs[r2], regs[r3]); +#endif + break; + case INDEX_op_mulu2: + tci_args_rrrr(insn, &r0, &r1, &r2, &r3); +#if TCG_TARGET_REG_BITS =3D=3D 32 + tmp64 =3D (uint64_t)(uint32_t)regs[r2] * (uint32_t)regs[r3]; + tci_write_reg64(regs, r1, r0, tmp64); +#else + mulu64(®s[r0], ®s[r1], regs[r2], regs[r3]); +#endif + break; + + /* Arithmetic operations (32 bit). */ + + case INDEX_op_tci_divs32: + tci_args_rrr(insn, &r0, &r1, &r2); + regs[r0] =3D (int32_t)regs[r1] / (int32_t)regs[r2]; + break; + case INDEX_op_tci_divu32: + tci_args_rrr(insn, &r0, &r1, &r2); + regs[r0] =3D (uint32_t)regs[r1] / (uint32_t)regs[r2]; + break; + case INDEX_op_tci_rems32: + tci_args_rrr(insn, &r0, &r1, &r2); + regs[r0] =3D (int32_t)regs[r1] % (int32_t)regs[r2]; + break; + case INDEX_op_tci_remu32: + tci_args_rrr(insn, &r0, &r1, &r2); + regs[r0] =3D (uint32_t)regs[r1] % (uint32_t)regs[r2]; + break; + case INDEX_op_tci_clz32: + tci_args_rrr(insn, &r0, &r1, &r2); + tmp32 =3D regs[r1]; + regs[r0] =3D tmp32 ? clz32(tmp32) : regs[r2]; + break; + case INDEX_op_tci_ctz32: + tci_args_rrr(insn, &r0, &r1, &r2); + tmp32 =3D regs[r1]; + regs[r0] =3D tmp32 ? ctz32(tmp32) : regs[r2]; + break; + case INDEX_op_tci_setcond32: + tci_args_rrrc(insn, &r0, &r1, &r2, &condition); + regs[r0] =3D tci_compare32(regs[r1], regs[r2], condition); + break; + case INDEX_op_tci_movcond32: + tci_args_rrrrrc(insn, &r0, &r1, &r2, &r3, &r4, &condition); + tmp32 =3D tci_compare32(regs[r1], regs[r2], condition); + regs[r0] =3D regs[tmp32 ? r3 : r4]; + break; + + /* Shift/rotate operations. */ + + case INDEX_op_shl: + tci_args_rrr(insn, &r0, &r1, &r2); + regs[r0] =3D regs[r1] << (regs[r2] % TCG_TARGET_REG_BITS); + break; + case INDEX_op_shr: + tci_args_rrr(insn, &r0, &r1, &r2); + regs[r0] =3D regs[r1] >> (regs[r2] % TCG_TARGET_REG_BITS); + break; + case INDEX_op_sar: + tci_args_rrr(insn, &r0, &r1, &r2); + regs[r0] =3D ((tcg_target_long)regs[r1] + >> (regs[r2] % TCG_TARGET_REG_BITS)); + break; + case INDEX_op_tci_rotl32: + tci_args_rrr(insn, &r0, &r1, &r2); + regs[r0] =3D rol32(regs[r1], regs[r2] & 31); + break; + case INDEX_op_tci_rotr32: + tci_args_rrr(insn, &r0, &r1, &r2); + regs[r0] =3D ror32(regs[r1], regs[r2] & 31); + break; + case INDEX_op_deposit: + tci_args_rrrbb(insn, &r0, &r1, &r2, &pos, &len); + regs[r0] =3D deposit_tr(regs[r1], pos, len, regs[r2]); + break; + case INDEX_op_extract: + tci_args_rrbb(insn, &r0, &r1, &pos, &len); + regs[r0] =3D extract_tr(regs[r1], pos, len); + break; + case INDEX_op_sextract: + tci_args_rrbb(insn, &r0, &r1, &pos, &len); + regs[r0] =3D sextract_tr(regs[r1], pos, len); + break; + case INDEX_op_brcond: + tci_args_rl(insn, tb_ptr, &r0, &ptr); + if (regs[r0]) { + tb_ptr =3D ptr; + } + break; + case INDEX_op_bswap16: + tci_args_rr(insn, &r0, &r1); + regs[r0] =3D bswap16(regs[r1]); + break; + case INDEX_op_bswap32: + tci_args_rr(insn, &r0, &r1); + regs[r0] =3D bswap32(regs[r1]); + break; +#if TCG_TARGET_REG_BITS =3D=3D 64 + /* Load/store operations (64 bit). */ + + case INDEX_op_ld32u: + tci_args_rrs(insn, &r0, &r1, &ofs); + ptr =3D (void *)(regs[r1] + ofs); + regs[r0] =3D *(uint32_t *)ptr; + break; + case INDEX_op_ld32s: + tci_args_rrs(insn, &r0, &r1, &ofs); + ptr =3D (void *)(regs[r1] + ofs); + regs[r0] =3D *(int32_t *)ptr; + break; + case INDEX_op_st32: + tci_args_rrs(insn, &r0, &r1, &ofs); + ptr =3D (void *)(regs[r1] + ofs); + *(uint32_t *)ptr =3D regs[r0]; + break; + + /* Arithmetic operations (64 bit). */ + + case INDEX_op_divs: + tci_args_rrr(insn, &r0, &r1, &r2); + regs[r0] =3D (int64_t)regs[r1] / (int64_t)regs[r2]; + break; + case INDEX_op_divu: + tci_args_rrr(insn, &r0, &r1, &r2); + regs[r0] =3D (uint64_t)regs[r1] / (uint64_t)regs[r2]; + break; + case INDEX_op_rems: + tci_args_rrr(insn, &r0, &r1, &r2); + regs[r0] =3D (int64_t)regs[r1] % (int64_t)regs[r2]; + break; + case INDEX_op_remu: + tci_args_rrr(insn, &r0, &r1, &r2); + regs[r0] =3D (uint64_t)regs[r1] % (uint64_t)regs[r2]; + break; + case INDEX_op_clz: + tci_args_rrr(insn, &r0, &r1, &r2); + regs[r0] =3D regs[r1] ? clz64(regs[r1]) : regs[r2]; + break; + case INDEX_op_ctz: + tci_args_rrr(insn, &r0, &r1, &r2); + regs[r0] =3D regs[r1] ? ctz64(regs[r1]) : regs[r2]; + break; + + /* Shift/rotate operations (64 bit). */ + + case INDEX_op_rotl: + tci_args_rrr(insn, &r0, &r1, &r2); + regs[r0] =3D rol64(regs[r1], regs[r2] & 63); + break; + case INDEX_op_rotr: + tci_args_rrr(insn, &r0, &r1, &r2); + regs[r0] =3D ror64(regs[r1], regs[r2] & 63); + break; + case INDEX_op_ext_i32_i64: + tci_args_rr(insn, &r0, &r1); + regs[r0] =3D (int32_t)regs[r1]; + break; + case INDEX_op_extu_i32_i64: + tci_args_rr(insn, &r0, &r1); + regs[r0] =3D (uint32_t)regs[r1]; + break; + case INDEX_op_bswap64: + tci_args_rr(insn, &r0, &r1); + regs[r0] =3D bswap64(regs[r1]); + break; +#endif /* TCG_TARGET_REG_BITS =3D=3D 64 */ + + /* QEMU specific operations. */ + + case INDEX_op_exit_tb: + tci_args_l(insn, tb_ptr, &ptr); + return (uintptr_t)ptr; + + case INDEX_op_goto_tb: + tci_args_l(insn, tb_ptr, &ptr); + tb_ptr =3D *(void **)ptr; + break; + + case INDEX_op_goto_ptr: + tci_args_r(insn, &r0); + ptr =3D (void *)regs[r0]; + if (!ptr) { + return 0; + } + tb_ptr =3D ptr; + break; + + case INDEX_op_qemu_ld: + tci_args_rrm(insn, &r0, &r1, &oi); + taddr =3D regs[r1]; + regs[r0] =3D tci_qemu_ld(env, taddr, oi, tb_ptr); + break; + + case INDEX_op_qemu_st: + tci_args_rrm(insn, &r0, &r1, &oi); + taddr =3D regs[r1]; + tci_qemu_st(env, taddr, regs[r0], oi, tb_ptr); + break; + + case INDEX_op_qemu_ld2: + tcg_debug_assert(TCG_TARGET_REG_BITS =3D=3D 32); + tci_args_rrrr(insn, &r0, &r1, &r2, &r3); + taddr =3D regs[r2]; + oi =3D regs[r3]; + tmp64 =3D tci_qemu_ld(env, taddr, oi, tb_ptr); + tci_write_reg64(regs, r1, r0, tmp64); + break; + + case INDEX_op_qemu_st2: + tcg_debug_assert(TCG_TARGET_REG_BITS =3D=3D 32); + tci_args_rrrr(insn, &r0, &r1, &r2, &r3); + tmp64 =3D tci_uint64(regs[r1], regs[r0]); + taddr =3D regs[r2]; + oi =3D regs[r3]; + tci_qemu_st(env, taddr, tmp64, oi, tb_ptr); + break; + + case INDEX_op_mb: + /* Ensure ordering for all kinds */ + smp_mb(); + break; + default: + g_assert_not_reached(); + } + } +} + +/* + * Disassembler that matches the interpreter + */ + +static const char *str_r(TCGReg r) +{ + static const char regs[TCG_TARGET_NB_REGS][4] =3D { + "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", + "r8", "r9", "r10", "r11", "r12", "r13", "env", "sp" + }; + + QEMU_BUILD_BUG_ON(TCG_AREG0 !=3D TCG_REG_R14); + QEMU_BUILD_BUG_ON(TCG_REG_CALL_STACK !=3D 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] =3D { + [TCG_COND_NEVER] =3D "never", + [TCG_COND_ALWAYS] =3D "always", + [TCG_COND_EQ] =3D "eq", + [TCG_COND_NE] =3D "ne", + [TCG_COND_LT] =3D "lt", + [TCG_COND_GE] =3D "ge", + [TCG_COND_LE] =3D "le", + [TCG_COND_GT] =3D "gt", + [TCG_COND_LTU] =3D "ltu", + [TCG_COND_GEU] =3D "geu", + [TCG_COND_LEU] =3D "leu", + [TCG_COND_GTU] =3D "gtu", + [TCG_COND_TSTEQ] =3D "tsteq", + [TCG_COND_TSTNE] =3D "tstne", + }; + + assert((unsigned)c < ARRAY_SIZE(cond)); + assert(cond[c][0] !=3D 0); + return cond[c]; +} + +/* Disassemble TCI bytecode. */ +int print_insn_tci(bfd_vma addr, disassemble_info *info) +{ + const uint32_t *tb_ptr =3D (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 =3D *tb_ptr++; + + info->fprintf_func(info->stream, "%08x ", insn); + + op =3D extract32(insn, 0, 8); + def =3D &tcg_op_defs[op]; + op_name =3D 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, pt= r); + 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 =3D=3D 0) { + info->fprintf_func(info->stream, "align"); + break; + } + /* fall through */ + + default: + info->fprintf_func(info->stream, "illegal opcode %d", op); + break; + } + + return sizeof(insn); +} diff --git a/tcg/wasm32/tcg-target-con-set.h b/tcg/wasm32/tcg-target-con-se= t.h new file mode 100644 index 0000000000..ae2dc3b844 --- /dev/null +++ b/tcg/wasm32/tcg-target-con-set.h @@ -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 outputs and 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) diff --git a/tcg/wasm32/tcg-target-con-str.h b/tcg/wasm32/tcg-target-con-st= r.h new file mode 100644 index 0000000000..87c0f19e9c --- /dev/null +++ b/tcg/wasm32/tcg-target-con-str.h @@ -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)) diff --git a/tcg/wasm32/tcg-target-has.h b/tcg/wasm32/tcg-target-has.h new file mode 100644 index 0000000000..ab07ce1fcb --- /dev/null +++ b/tcg/wasm32/tcg-target-has.h @@ -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 =3D=3D 64 +#define TCG_TARGET_HAS_extr_i64_i32 0 +#endif /* TCG_TARGET_REG_BITS =3D=3D 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 diff --git a/tcg/wasm32/tcg-target-mo.h b/tcg/wasm32/tcg-target-mo.h new file mode 100644 index 0000000000..779872e39a --- /dev/null +++ b/tcg/wasm32/tcg-target-mo.h @@ -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 diff --git a/tcg/wasm32/tcg-target-opc.h.inc b/tcg/wasm32/tcg-target-opc.h.= inc new file mode 100644 index 0000000000..4eb32ed736 --- /dev/null +++ b/tcg/wasm32/tcg-target-opc.h.inc @@ -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) diff --git a/tcg/wasm32/tcg-target-reg-bits.h b/tcg/wasm32/tcg-target-reg-b= its.h new file mode 100644 index 0000000000..dcb1a203f8 --- /dev/null +++ b/tcg/wasm32/tcg-target-reg-bits.h @@ -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 =3D=3D UINT32_MAX +# define TCG_TARGET_REG_BITS 32 +#elif UINTPTR_MAX =3D=3D UINT64_MAX +# define TCG_TARGET_REG_BITS 64 +#else +# error Unknown pointer size for tci target +#endif + +#endif diff --git a/tcg/wasm32/tcg-target.c.inc b/tcg/wasm32/tcg-target.c.inc new file mode 100644 index 0000000000..33b81f1fe2 --- /dev/null +++ b/tcg/wasm32/tcg-target.c.inc @@ -0,0 +1,1320 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Tiny Code Generator for QEMU + * + * Copyright (c) 2009, 2011 Stefan Weil + * + * Based on tci/tcg-target.c.inc + * + * Permission is hereby granted, free of charge, to any person obtaining a= copy + * of this software and associated documentation files (the "Software"), t= o deal + * in the Software without restriction, including without limitation the r= ights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or se= ll + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OT= HER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING= FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS = IN + * THE SOFTWARE. + */ + +/* Used for function call generation. */ +#define TCG_TARGET_CALL_STACK_OFFSET 0 +#define TCG_TARGET_STACK_ALIGN 8 +#if TCG_TARGET_REG_BITS =3D=3D 32 +# define TCG_TARGET_CALL_ARG_I32 TCG_CALL_ARG_EVEN +# define TCG_TARGET_CALL_ARG_I64 TCG_CALL_ARG_EVEN +# define TCG_TARGET_CALL_ARG_I128 TCG_CALL_ARG_EVEN +#else +# define TCG_TARGET_CALL_ARG_I32 TCG_CALL_ARG_NORMAL +# define TCG_TARGET_CALL_ARG_I64 TCG_CALL_ARG_NORMAL +# define TCG_TARGET_CALL_ARG_I128 TCG_CALL_ARG_NORMAL +#endif +#define TCG_TARGET_CALL_RET_I128 TCG_CALL_RET_NORMAL + +static TCGConstraintSetIndex +tcg_target_op_def(TCGOpcode op, TCGType type, unsigned flags) +{ + return C_NotImplemented; +} + +static const int tcg_target_reg_alloc_order[] =3D { + TCG_REG_R4, + TCG_REG_R5, + TCG_REG_R6, + TCG_REG_R7, + TCG_REG_R8, + TCG_REG_R9, + TCG_REG_R10, + TCG_REG_R11, + TCG_REG_R12, + TCG_REG_R13, + TCG_REG_R14, + TCG_REG_R15, + /* Either 2 or 4 of these are call clobbered, so use them last. */ + TCG_REG_R3, + TCG_REG_R2, + TCG_REG_R1, + TCG_REG_R0, +}; + +/* No call arguments via registers. All will be stored on the "stack". */ +static const int tcg_target_call_iarg_regs[] =3D { }; + +static TCGReg tcg_target_call_oarg_reg(TCGCallReturnKind kind, int slot) +{ + tcg_debug_assert(kind =3D=3D TCG_CALL_RET_NORMAL); + tcg_debug_assert(slot >=3D 0 && slot < 128 / TCG_TARGET_REG_BITS); + return TCG_REG_R0 + slot; +} + +#ifdef CONFIG_DEBUG_TCG +static const char *const tcg_target_reg_names[TCG_TARGET_NB_REGS] =3D { + "r00", + "r01", + "r02", + "r03", + "r04", + "r05", + "r06", + "r07", + "r08", + "r09", + "r10", + "r11", + "r12", + "r13", + "r14", + "r15", +}; +#endif + +static bool patch_reloc(tcg_insn_unit *code_ptr, int type, + intptr_t value, intptr_t addend) +{ + intptr_t diff =3D value - (intptr_t)(code_ptr + 1); + + tcg_debug_assert(addend =3D=3D 0); + tcg_debug_assert(type =3D=3D 20); + + if (diff =3D=3D sextract32(diff, 0, type)) { + tcg_patch32(code_ptr, deposit32(*code_ptr, 32 - type, type, diff)); + return true; + } + return false; +} + +static void stack_bounds_check(TCGReg base, intptr_t offset) +{ + if (base =3D=3D TCG_REG_CALL_STACK) { + tcg_debug_assert(offset >=3D 0); + tcg_debug_assert(offset < (TCG_STATIC_CALL_ARGS_SIZE + + TCG_STATIC_FRAME_SIZE)); + } +} + +static void tcg_out_op_l(TCGContext *s, TCGOpcode op, TCGLabel *l0) +{ + tcg_insn_unit insn =3D 0; + + tcg_out_reloc(s, s->code_ptr, 20, l0, 0); + insn =3D deposit32(insn, 0, 8, op); + tcg_out32(s, insn); +} + +static void tcg_out_op_p(TCGContext *s, TCGOpcode op, void *p0) +{ + tcg_insn_unit insn =3D 0; + intptr_t diff; + + /* Special case for exit_tb: map null -> 0. */ + if (p0 =3D=3D NULL) { + diff =3D 0; + } else { + diff =3D p0 - (void *)(s->code_ptr + 1); + tcg_debug_assert(diff !=3D 0); + if (diff !=3D sextract32(diff, 0, 20)) { + tcg_raise_tb_overflow(s); + } + } + insn =3D deposit32(insn, 0, 8, op); + insn =3D deposit32(insn, 12, 20, diff); + tcg_out32(s, insn); +} + +static void tcg_out_op_r(TCGContext *s, TCGOpcode op, TCGReg r0) +{ + tcg_insn_unit insn =3D 0; + + insn =3D deposit32(insn, 0, 8, op); + insn =3D deposit32(insn, 8, 4, r0); + tcg_out32(s, insn); +} + +static void tcg_out_op_v(TCGContext *s, TCGOpcode op) +{ + tcg_out32(s, (uint8_t)op); +} + +static void tcg_out_op_ri(TCGContext *s, TCGOpcode op, TCGReg r0, int32_t = i1) +{ + tcg_insn_unit insn =3D 0; + + tcg_debug_assert(i1 =3D=3D sextract32(i1, 0, 20)); + insn =3D deposit32(insn, 0, 8, op); + insn =3D deposit32(insn, 8, 4, r0); + insn =3D deposit32(insn, 12, 20, i1); + tcg_out32(s, insn); +} + +static void tcg_out_op_rl(TCGContext *s, TCGOpcode op, TCGReg r0, TCGLabel= *l1) +{ + tcg_insn_unit insn =3D 0; + + tcg_out_reloc(s, s->code_ptr, 20, l1, 0); + insn =3D deposit32(insn, 0, 8, op); + insn =3D deposit32(insn, 8, 4, r0); + tcg_out32(s, insn); +} + +static void tcg_out_op_rr(TCGContext *s, TCGOpcode op, TCGReg r0, TCGReg r= 1) +{ + tcg_insn_unit insn =3D 0; + + insn =3D deposit32(insn, 0, 8, op); + insn =3D deposit32(insn, 8, 4, r0); + insn =3D deposit32(insn, 12, 4, r1); + tcg_out32(s, insn); +} + +static void tcg_out_op_rrm(TCGContext *s, TCGOpcode op, + TCGReg r0, TCGReg r1, TCGArg m2) +{ + tcg_insn_unit insn =3D 0; + + tcg_debug_assert(m2 =3D=3D extract32(m2, 0, 16)); + insn =3D deposit32(insn, 0, 8, op); + insn =3D deposit32(insn, 8, 4, r0); + insn =3D deposit32(insn, 12, 4, r1); + insn =3D deposit32(insn, 16, 16, m2); + tcg_out32(s, insn); +} + +static void tcg_out_op_rrr(TCGContext *s, TCGOpcode op, + TCGReg r0, TCGReg r1, TCGReg r2) +{ + tcg_insn_unit insn =3D 0; + + insn =3D deposit32(insn, 0, 8, op); + insn =3D deposit32(insn, 8, 4, r0); + insn =3D deposit32(insn, 12, 4, r1); + insn =3D deposit32(insn, 16, 4, r2); + tcg_out32(s, insn); +} + +static void tcg_out_op_rrs(TCGContext *s, TCGOpcode op, + TCGReg r0, TCGReg r1, intptr_t i2) +{ + tcg_insn_unit insn =3D 0; + + tcg_debug_assert(i2 =3D=3D sextract32(i2, 0, 16)); + insn =3D deposit32(insn, 0, 8, op); + insn =3D deposit32(insn, 8, 4, r0); + insn =3D deposit32(insn, 12, 4, r1); + insn =3D deposit32(insn, 16, 16, i2); + tcg_out32(s, insn); +} + +static void tcg_out_op_rrbb(TCGContext *s, TCGOpcode op, TCGReg r0, + TCGReg r1, uint8_t b2, uint8_t b3) +{ + tcg_insn_unit insn =3D 0; + + tcg_debug_assert(b2 =3D=3D extract32(b2, 0, 6)); + tcg_debug_assert(b3 =3D=3D extract32(b3, 0, 6)); + insn =3D deposit32(insn, 0, 8, op); + insn =3D deposit32(insn, 8, 4, r0); + insn =3D deposit32(insn, 12, 4, r1); + insn =3D deposit32(insn, 16, 6, b2); + insn =3D deposit32(insn, 22, 6, b3); + tcg_out32(s, insn); +} + +static void tcg_out_op_rrrc(TCGContext *s, TCGOpcode op, + TCGReg r0, TCGReg r1, TCGReg r2, TCGCond c3) +{ + tcg_insn_unit insn =3D 0; + + insn =3D deposit32(insn, 0, 8, op); + insn =3D deposit32(insn, 8, 4, r0); + insn =3D deposit32(insn, 12, 4, r1); + insn =3D deposit32(insn, 16, 4, r2); + insn =3D deposit32(insn, 20, 4, c3); + tcg_out32(s, insn); +} + +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 =3D 0; + + tcg_debug_assert(b3 =3D=3D extract32(b3, 0, 6)); + tcg_debug_assert(b4 =3D=3D extract32(b4, 0, 6)); + insn =3D deposit32(insn, 0, 8, op); + insn =3D deposit32(insn, 8, 4, r0); + insn =3D deposit32(insn, 12, 4, r1); + insn =3D deposit32(insn, 16, 4, r2); + insn =3D deposit32(insn, 20, 6, b3); + insn =3D deposit32(insn, 26, 6, b4); + 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 =3D 0; + + insn =3D deposit32(insn, 0, 8, op); + insn =3D deposit32(insn, 8, 4, r0); + insn =3D deposit32(insn, 12, 4, r1); + insn =3D deposit32(insn, 16, 4, r2); + insn =3D 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) +{ + tcg_insn_unit insn =3D 0; + + insn =3D deposit32(insn, 0, 8, op); + insn =3D deposit32(insn, 8, 4, r0); + insn =3D deposit32(insn, 12, 4, r1); + insn =3D deposit32(insn, 16, 4, r2); + insn =3D deposit32(insn, 20, 4, r3); + insn =3D deposit32(insn, 24, 4, r4); + insn =3D deposit32(insn, 28, 4, c5); + tcg_out32(s, insn); +} + +static void tcg_out_ldst(TCGContext *s, TCGOpcode op, TCGReg val, + TCGReg base, intptr_t offset) +{ + stack_bounds_check(base, offset); + if (offset !=3D sextract32(offset, 0, 16)) { + tcg_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 =3D TCG_REG_TMP; + offset =3D 0; + } + tcg_out_op_rrs(s, op, val, base, offset); +} + +static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg val, TCGReg bas= e, + intptr_t offset) +{ + TCGOpcode op =3D INDEX_op_ld; + + if (TCG_TARGET_REG_BITS =3D=3D 64 && type =3D=3D TCG_TYPE_I32) { + op =3D INDEX_op_ld32u; + } + tcg_out_ldst(s, op, val, base, offset); +} + +static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg ar= g) +{ + tcg_out_op_rr(s, INDEX_op_mov, 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 =3D=3D 64 + arg =3D (int32_t)arg; + /* fall through */ + case TCG_TYPE_I64: +#endif + break; + default: + g_assert_not_reached(); + } + + if (arg =3D=3D sextract32(arg, 0, 20)) { + tcg_out_op_ri(s, INDEX_op_tci_movi, ret, arg); + } else { + tcg_insn_unit insn =3D 0; + + new_pool_label(s, arg, 20, s->code_ptr, 0); + insn =3D deposit32(insn, 0, 8, INDEX_op_tci_movl); + insn =3D deposit32(insn, 8, 4, ret); + tcg_out32(s, insn); + } +} + +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); +} + +static const TCGOutOpExtract outop_extract =3D { + .base.static_constraint =3D C_O1_I1(r, r), + .out_rr =3D tcg_out_extract, +}; + +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); +} + +static const TCGOutOpExtract outop_sextract =3D { + .base.static_constraint =3D C_O1_I1(r, r), + .out_rr =3D tcg_out_sextract, +}; + +static const TCGOutOpExtract2 outop_extract2 =3D { + .base.static_constraint =3D C_NotImplemented, +}; + +static void tcg_out_ext8s(TCGContext *s, TCGType type, TCGReg rd, TCGReg r= s) +{ + tcg_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); +} + +static void tcg_out_ext16s(TCGContext *s, TCGType type, TCGReg rd, TCGReg = rs) +{ + tcg_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); +} + +static void tcg_out_ext32s(TCGContext *s, TCGReg rd, TCGReg rs) +{ + tcg_debug_assert(TCG_TARGET_REG_BITS =3D=3D 64); + tcg_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 =3D=3D 64); + tcg_out_extract(s, TCG_TYPE_I64, rd, rs, 0, 32); +} + +static void tcg_out_exts_i32_i64(TCGContext *s, TCGReg rd, TCGReg rs) +{ + tcg_out_ext32s(s, rd, rs); +} + +static void tcg_out_extu_i32_i64(TCGContext *s, TCGReg rd, TCGReg rs) +{ + tcg_out_ext32u(s, rd, rs); +} + +static void tcg_out_extrl_i64_i32(TCGContext *s, TCGReg rd, TCGReg rs) +{ + tcg_debug_assert(TCG_TARGET_REG_BITS =3D=3D 64); + tcg_out_mov(s, TCG_TYPE_I32, rd, rs); +} + +static bool tcg_out_xchg(TCGContext *s, TCGType type, TCGReg r1, TCGReg r2) +{ + return false; +} + +static void tcg_out_addi_ptr(TCGContext *s, TCGReg rd, TCGReg rs, + tcg_target_long imm) +{ + /* This function is only used for passing structs by reference. */ + g_assert_not_reached(); +} + +static void tcg_out_call(TCGContext *s, const tcg_insn_unit *func, + const TCGHelperInfo *info) +{ + ffi_cif *cif =3D info->cif; + tcg_insn_unit insn =3D 0; + uint8_t which; + + if (cif->rtype =3D=3D &ffi_type_void) { + which =3D 0; + } else { + tcg_debug_assert(cif->rtype->size =3D=3D 4 || + cif->rtype->size =3D=3D 8 || + cif->rtype->size =3D=3D 16); + which =3D ctz32(cif->rtype->size) - 1; + } + new_pool_l2(s, 20, s->code_ptr, 0, (uintptr_t)func, (uintptr_t)cif); + insn =3D deposit32(insn, 0, 8, INDEX_op_call); + insn =3D deposit32(insn, 8, 4, which); + tcg_out32(s, insn); +} + +static void tcg_out_exit_tb(TCGContext *s, uintptr_t arg) +{ + tcg_out_op_p(s, INDEX_op_exit_tb, (void *)arg); +} + +static void tcg_out_goto_tb(TCGContext *s, int which) +{ + /* indirect jump method. */ + tcg_out_op_p(s, INDEX_op_goto_tb, (void *)get_jmp_target_addr(s, which= )); + set_jmp_reset_offset(s, which); +} + +static void tcg_out_goto_ptr(TCGContext *s, TCGReg a0) +{ + tcg_out_op_r(s, INDEX_op_goto_ptr, a0); +} + +void tb_target_set_jmp_target(const TranslationBlock *tb, int n, + uintptr_t jmp_rx, uintptr_t jmp_rw) +{ + /* Always indirect, nothing to do */ +} + +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); +} + +static const TCGOutOpBinary outop_add =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_add, +}; + +static TCGConstraintSetIndex cset_addsubcarry(TCGType type, unsigned flags) +{ + return type =3D=3D 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 =3D { + .base.static_constraint =3D C_Dynamic, + .base.dynamic_constraint =3D cset_addsubcarry, + .out_rrr =3D tgen_addco, +}; + +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 =3D { + .base.static_constraint =3D C_Dynamic, + .base.dynamic_constraint =3D cset_addsubcarry, + .out_rrr =3D tgen_addci, +}; + +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 =3D { + .base.static_constraint =3D C_Dynamic, + .base.dynamic_constraint =3D cset_addsubcarry, + .out_rrr =3D tgen_addcio, +}; + +static void tcg_out_set_carry(TCGContext *s) +{ + tcg_out_op_v(s, INDEX_op_tci_setcarry); +} + +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); +} + +static const TCGOutOpBinary outop_and =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_and, +}; + +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); +} + +static const TCGOutOpBinary outop_andc =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_andc, +}; + +static void tgen_clz(TCGContext *s, TCGType type, + TCGReg a0, TCGReg a1, TCGReg a2) +{ + TCGOpcode opc =3D (type =3D=3D TCG_TYPE_I32 + ? INDEX_op_tci_clz32 + : INDEX_op_clz); + tcg_out_op_rrr(s, opc, a0, a1, a2); +} + +static const TCGOutOpBinary outop_clz =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_clz, +}; + +static void tgen_ctz(TCGContext *s, TCGType type, + TCGReg a0, TCGReg a1, TCGReg a2) +{ + TCGOpcode opc =3D (type =3D=3D TCG_TYPE_I32 + ? INDEX_op_tci_ctz32 + : INDEX_op_ctz); + tcg_out_op_rrr(s, opc, a0, a1, a2); +} + +static const TCGOutOpBinary outop_ctz =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_ctz, +}; + +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); +} + +static const TCGOutOpDeposit outop_deposit =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_deposit, +}; + +static void tgen_divs(TCGContext *s, TCGType type, + TCGReg a0, TCGReg a1, TCGReg a2) +{ + TCGOpcode opc =3D (type =3D=3D TCG_TYPE_I32 + ? INDEX_op_tci_divs32 + : INDEX_op_divs); + tcg_out_op_rrr(s, opc, a0, a1, a2); +} + +static const TCGOutOpBinary outop_divs =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_divs, +}; + +static const TCGOutOpDivRem outop_divs2 =3D { + .base.static_constraint =3D C_NotImplemented, +}; + +static void tgen_divu(TCGContext *s, TCGType type, + TCGReg a0, TCGReg a1, TCGReg a2) +{ + TCGOpcode opc =3D (type =3D=3D TCG_TYPE_I32 + ? INDEX_op_tci_divu32 + : INDEX_op_divu); + tcg_out_op_rrr(s, opc, a0, a1, a2); +} + +static const TCGOutOpBinary outop_divu =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_divu, +}; + +static const TCGOutOpDivRem outop_divu2 =3D { + .base.static_constraint =3D C_NotImplemented, +}; + +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); +} + +static const TCGOutOpBinary outop_eqv =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_eqv, +}; + +#if TCG_TARGET_REG_BITS =3D=3D 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); +} + +static const TCGOutOpUnary outop_extrh_i64_i32 =3D { + .base.static_constraint =3D C_O1_I1(r, r), + .out_rr =3D tgen_extrh_i64_i32, +}; +#endif + +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); +} + +static const TCGOutOpBinary outop_mul =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_mul, +}; + +static TCGConstraintSetIndex cset_mul2(TCGType type, unsigned flags) +{ + return type =3D=3D TCG_TYPE_REG ? C_O2_I2(r, r, r, r) : C_NotImplement= ed; +} + +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 =3D { + .base.static_constraint =3D C_Dynamic, + .base.dynamic_constraint =3D cset_mul2, + .out_rrrr =3D tgen_muls2, +}; + +static const TCGOutOpBinary outop_mulsh =3D { + .base.static_constraint =3D 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 =3D { + .base.static_constraint =3D C_Dynamic, + .base.dynamic_constraint =3D cset_mul2, + .out_rrrr =3D tgen_mulu2, +}; + +static const TCGOutOpBinary outop_muluh =3D { + .base.static_constraint =3D C_NotImplemented, +}; + +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); +} + +static const TCGOutOpBinary outop_nand =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_nand, +}; + +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); +} + +static const TCGOutOpBinary outop_nor =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_nor, +}; + +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); +} + +static const TCGOutOpBinary outop_or =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_or, +}; + +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); +} + +static const TCGOutOpBinary outop_orc =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_orc, +}; + +static void tgen_rems(TCGContext *s, TCGType type, + TCGReg a0, TCGReg a1, TCGReg a2) +{ + TCGOpcode opc =3D (type =3D=3D TCG_TYPE_I32 + ? INDEX_op_tci_rems32 + : INDEX_op_rems); + tcg_out_op_rrr(s, opc, a0, a1, a2); +} + +static const TCGOutOpBinary outop_rems =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_rems, +}; + +static void tgen_remu(TCGContext *s, TCGType type, + TCGReg a0, TCGReg a1, TCGReg a2) +{ + TCGOpcode opc =3D (type =3D=3D TCG_TYPE_I32 + ? INDEX_op_tci_remu32 + : INDEX_op_remu); + tcg_out_op_rrr(s, opc, a0, a1, a2); +} + +static const TCGOutOpBinary outop_remu =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_remu, +}; + +static void tgen_rotl(TCGContext *s, TCGType type, + TCGReg a0, TCGReg a1, TCGReg a2) +{ + TCGOpcode opc =3D (type =3D=3D TCG_TYPE_I32 + ? INDEX_op_tci_rotl32 + : INDEX_op_rotl); + tcg_out_op_rrr(s, opc, a0, a1, a2); +} + +static const TCGOutOpBinary outop_rotl =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_rotl, +}; + +static void tgen_rotr(TCGContext *s, TCGType type, + TCGReg a0, TCGReg a1, TCGReg a2) +{ + TCGOpcode opc =3D (type =3D=3D TCG_TYPE_I32 + ? INDEX_op_tci_rotr32 + : INDEX_op_rotr); + tcg_out_op_rrr(s, opc, a0, a1, a2); +} + +static const TCGOutOpBinary outop_rotr =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_rotr, +}; + +static void tgen_sar(TCGContext *s, TCGType type, + TCGReg a0, TCGReg a1, TCGReg a2) +{ + if (type < TCG_TYPE_REG) { + tcg_out_ext32s(s, TCG_REG_TMP, a1); + a1 =3D TCG_REG_TMP; + } + tcg_out_op_rrr(s, INDEX_op_sar, a0, a1, a2); +} + +static const TCGOutOpBinary outop_sar =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_sar, +}; + +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); +} + +static const TCGOutOpBinary outop_shl =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_shl, +}; + +static void tgen_shr(TCGContext *s, TCGType type, + TCGReg a0, TCGReg a1, TCGReg a2) +{ + if (type < TCG_TYPE_REG) { + tcg_out_ext32u(s, TCG_REG_TMP, a1); + a1 =3D TCG_REG_TMP; + } + tcg_out_op_rrr(s, INDEX_op_shr, a0, a1, a2); +} + +static const TCGOutOpBinary outop_shr =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_shr, +}; + +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); +} + +static const TCGOutOpSubtract outop_sub =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D 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 =3D { + .base.static_constraint =3D C_Dynamic, + .base.dynamic_constraint =3D cset_addsubcarry, + .out_rrr =3D tgen_subbo, +}; + +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 =3D { + .base.static_constraint =3D C_Dynamic, + .base.dynamic_constraint =3D cset_addsubcarry, + .out_rrr =3D tgen_subbi, +}; + +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 =3D { + .base.static_constraint =3D C_Dynamic, + .base.dynamic_constraint =3D cset_addsubcarry, + .out_rrr =3D tgen_subbio, +}; + +static void tcg_out_set_borrow(TCGContext *s) +{ + tcg_out_op_v(s, INDEX_op_tci_setcarry); /* borrow =3D=3D carry */ +} + +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); +} + +static const TCGOutOpBinary outop_xor =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_xor, +}; + +static void tgen_ctpop(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1) +{ + tcg_out_op_rr(s, INDEX_op_ctpop, a0, a1); +} + +static TCGConstraintSetIndex cset_ctpop(TCGType type, unsigned flags) +{ + return type =3D=3D TCG_TYPE_REG ? C_O1_I1(r, r) : C_NotImplemented; +} + +static const TCGOutOpUnary outop_ctpop =3D { + .base.static_constraint =3D C_Dynamic, + .base.dynamic_constraint =3D cset_ctpop, + .out_rr =3D tgen_ctpop, +}; + +static void tgen_bswap16(TCGContext *s, TCGType type, + TCGReg a0, TCGReg a1, unsigned flags) +{ + 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); + } +} + +static const TCGOutOpBswap outop_bswap16 =3D { + .base.static_constraint =3D C_O1_I1(r, r), + .out_rr =3D tgen_bswap16, +}; + +static void tgen_bswap32(TCGContext *s, TCGType type, + TCGReg a0, TCGReg a1, unsigned flags) +{ + 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); + } +} + +static const TCGOutOpBswap outop_bswap32 =3D { + .base.static_constraint =3D C_O1_I1(r, r), + .out_rr =3D tgen_bswap32, +}; + +#if TCG_TARGET_REG_BITS =3D=3D 64 +static void tgen_bswap64(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1) +{ + tcg_out_op_rr(s, INDEX_op_bswap64, a0, a1); +} + +static const TCGOutOpUnary outop_bswap64 =3D { + .base.static_constraint =3D C_O1_I1(r, r), + .out_rr =3D tgen_bswap64, +}; +#endif + +static void tgen_neg(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1) +{ + tcg_out_op_rr(s, INDEX_op_neg, a0, a1); +} + +static const TCGOutOpUnary outop_neg =3D { + .base.static_constraint =3D C_O1_I1(r, r), + .out_rr =3D tgen_neg, +}; + +static void tgen_not(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1) +{ + tcg_out_op_rr(s, INDEX_op_not, a0, a1); +} + +static const TCGOutOpUnary outop_not =3D { + .base.static_constraint =3D C_O1_I1(r, r), + .out_rr =3D tgen_not, +}; + +static void tgen_setcond(TCGContext *s, TCGType type, TCGCond cond, + TCGReg dest, TCGReg arg1, TCGReg arg2) +{ + TCGOpcode opc =3D (type =3D=3D TCG_TYPE_I32 + ? INDEX_op_tci_setcond32 + : INDEX_op_setcond); + tcg_out_op_rrrc(s, opc, dest, arg1, arg2, cond); +} + +static const TCGOutOpSetcond outop_setcond =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_setcond, +}; + +static void tgen_negsetcond(TCGContext *s, TCGType type, TCGCond cond, + TCGReg dest, TCGReg arg1, TCGReg arg2) +{ + tgen_setcond(s, type, cond, dest, arg1, arg2); + tgen_neg(s, type, dest, dest); +} + +static const TCGOutOpSetcond outop_negsetcond =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_negsetcond, +}; + +static void tgen_brcond(TCGContext *s, TCGType type, TCGCond cond, + TCGReg arg0, TCGReg arg1, TCGLabel *l) +{ + tgen_setcond(s, type, cond, TCG_REG_TMP, arg0, arg1); + tcg_out_op_rl(s, INDEX_op_brcond, TCG_REG_TMP, l); +} + +static const TCGOutOpBrcond outop_brcond =3D { + .base.static_constraint =3D C_O0_I2(r, r), + .out_rr =3D tgen_brcond, +}; + +static void tgen_movcond(TCGContext *s, TCGType type, TCGCond cond, + TCGReg ret, TCGReg c1, TCGArg c2, bool const_c2, + TCGArg vt, bool const_vt, TCGArg vf, bool consf_v= f) +{ + TCGOpcode opc =3D (type =3D=3D TCG_TYPE_I32 + ? INDEX_op_tci_movcond32 + : INDEX_op_movcond); + tcg_out_op_rrrrrc(s, opc, ret, c1, c2, vt, vf, cond); +} + +static const TCGOutOpMovcond outop_movcond =3D { + .base.static_constraint =3D C_O1_I4(r, r, r, r, r), + .out =3D tgen_movcond, +}; + +static void tgen_brcond2(TCGContext *s, TCGCond cond, TCGReg al, TCGReg ah, + TCGArg bl, bool const_bl, + TCGArg bh, bool const_bh, TCGLabel *l) +{ + tcg_out_op_rrrrrc(s, INDEX_op_setcond2_i32, TCG_REG_TMP, + al, ah, bl, bh, cond); + tcg_out_op_rl(s, INDEX_op_brcond, TCG_REG_TMP, l); +} + +#if TCG_TARGET_REG_BITS !=3D 32 +__attribute__((unused)) +#endif +static const TCGOutOpBrcond2 outop_brcond2 =3D { + .base.static_constraint =3D C_O0_I4(r, r, r, r), + .out =3D tgen_brcond2, +}; + +static void tgen_setcond2(TCGContext *s, TCGCond cond, TCGReg ret, + TCGReg al, TCGReg ah, + TCGArg bl, bool const_bl, + TCGArg bh, bool const_bh) +{ + tcg_out_op_rrrrrc(s, INDEX_op_setcond2_i32, ret, al, ah, bl, bh, cond); +} + +#if TCG_TARGET_REG_BITS !=3D 32 +__attribute__((unused)) +#endif +static const TCGOutOpSetcond2 outop_setcond2 =3D { + .base.static_constraint =3D C_O1_I4(r, r, r, r, r), + .out =3D tgen_setcond2, +}; + +static void tcg_out_mb(TCGContext *s, unsigned a0) +{ + tcg_out_op_v(s, INDEX_op_mb); +} + +static void tcg_out_br(TCGContext *s, TCGLabel *l) +{ + tcg_out_op_l(s, INDEX_op_br, l); +} + +static void tgen_ld8u(TCGContext *s, TCGType type, TCGReg dest, + TCGReg base, ptrdiff_t offset) +{ + tcg_out_ldst(s, INDEX_op_ld8u, dest, base, offset); +} + +static const TCGOutOpLoad outop_ld8u =3D { + .base.static_constraint =3D C_O1_I1(r, r), + .out =3D tgen_ld8u, +}; + +static void tgen_ld8s(TCGContext *s, TCGType type, TCGReg dest, + TCGReg base, ptrdiff_t offset) +{ + tcg_out_ldst(s, INDEX_op_ld8s, dest, base, offset); +} + +static const TCGOutOpLoad outop_ld8s =3D { + .base.static_constraint =3D C_O1_I1(r, r), + .out =3D tgen_ld8s, +}; + +static void tgen_ld16u(TCGContext *s, TCGType type, TCGReg dest, + TCGReg base, ptrdiff_t offset) +{ + tcg_out_ldst(s, INDEX_op_ld16u, dest, base, offset); +} + +static const TCGOutOpLoad outop_ld16u =3D { + .base.static_constraint =3D C_O1_I1(r, r), + .out =3D tgen_ld16u, +}; + +static void tgen_ld16s(TCGContext *s, TCGType type, TCGReg dest, + TCGReg base, ptrdiff_t offset) +{ + tcg_out_ldst(s, INDEX_op_ld16s, dest, base, offset); +} + +static const TCGOutOpLoad outop_ld16s =3D { + .base.static_constraint =3D C_O1_I1(r, r), + .out =3D tgen_ld16s, +}; + +#if TCG_TARGET_REG_BITS =3D=3D 64 +static void tgen_ld32u(TCGContext *s, TCGType type, TCGReg dest, + TCGReg base, ptrdiff_t offset) +{ + tcg_out_ldst(s, INDEX_op_ld32u, dest, base, offset); +} + +static const TCGOutOpLoad outop_ld32u =3D { + .base.static_constraint =3D C_O1_I1(r, r), + .out =3D tgen_ld32u, +}; + +static void tgen_ld32s(TCGContext *s, TCGType type, TCGReg dest, + TCGReg base, ptrdiff_t offset) +{ + tcg_out_ldst(s, INDEX_op_ld32s, dest, base, offset); +} + +static const TCGOutOpLoad outop_ld32s =3D { + .base.static_constraint =3D C_O1_I1(r, r), + .out =3D tgen_ld32s, +}; +#endif + +static void tgen_st8(TCGContext *s, TCGType type, TCGReg data, + TCGReg base, ptrdiff_t offset) +{ + tcg_out_ldst(s, INDEX_op_st8, data, base, offset); +} + +static const TCGOutOpStore outop_st8 =3D { + .base.static_constraint =3D C_O0_I2(r, r), + .out_r =3D tgen_st8, +}; + +static void tgen_st16(TCGContext *s, TCGType type, TCGReg data, + TCGReg base, ptrdiff_t offset) +{ + tcg_out_ldst(s, INDEX_op_st16, data, base, offset); +} + +static const TCGOutOpStore outop_st16 =3D { + .base.static_constraint =3D C_O0_I2(r, r), + .out_r =3D tgen_st16, +}; + +static const TCGOutOpStore outop_st =3D { + .base.static_constraint =3D C_O0_I2(r, r), + .out_r =3D tcg_out_st, +}; + +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); +} + +static const TCGOutOpQemuLdSt outop_qemu_ld =3D { + .base.static_constraint =3D C_O1_I1(r, r), + .out =3D tgen_qemu_ld, +}; + +static void tgen_qemu_ld2(TCGContext *s, TCGType type, TCGReg datalo, + TCGReg datahi, TCGReg addr, MemOpIdx oi) +{ + tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_TMP, oi); + tcg_out_op_rrrr(s, INDEX_op_qemu_ld2, datalo, datahi, addr, TCG_REG_TM= P); +} + +static const TCGOutOpQemuLdSt2 outop_qemu_ld2 =3D { + .base.static_constraint =3D + TCG_TARGET_REG_BITS =3D=3D 64 ? C_NotImplemented : C_O2_I1(r, r, r= ), + .out =3D + TCG_TARGET_REG_BITS =3D=3D 64 ? NULL : tgen_qemu_ld2, +}; + +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); +} + +static const TCGOutOpQemuLdSt outop_qemu_st =3D { + .base.static_constraint =3D C_O0_I2(r, r), + .out =3D tgen_qemu_st, +}; + +static void tgen_qemu_st2(TCGContext *s, TCGType type, TCGReg datalo, + TCGReg datahi, TCGReg addr, MemOpIdx oi) +{ + tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_TMP, oi); + tcg_out_op_rrrr(s, INDEX_op_qemu_st2, datalo, datahi, addr, TCG_REG_TM= P); +} + +static const TCGOutOpQemuLdSt2 outop_qemu_st2 =3D { + .base.static_constraint =3D + TCG_TARGET_REG_BITS =3D=3D 64 ? C_NotImplemented : C_O0_I3(r, r, r= ), + .out =3D + TCG_TARGET_REG_BITS =3D=3D 64 ? NULL : tgen_qemu_st2, +}; + +static void tcg_out_st(TCGContext *s, TCGType type, TCGReg val, TCGReg bas= e, + intptr_t offset) +{ + TCGOpcode op =3D INDEX_op_st; + + if (TCG_TARGET_REG_BITS =3D=3D 64 && type =3D=3D TCG_TYPE_I32) { + op =3D INDEX_op_st32; + } + tcg_out_ldst(s, op, val, base, offset); +} + +static inline bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val, + TCGReg base, intptr_t ofs) +{ + return false; +} + +/* Test if a constant matches the constraint. */ +static bool tcg_target_const_match(int64_t val, int ct, + TCGType type, TCGCond cond, int vece) +{ + return ct & TCG_CT_CONST; +} + +static void tcg_out_nop_fill(tcg_insn_unit *p, int count) +{ + memset(p, 0, sizeof(*p) * count); +} + +static void tcg_target_init(TCGContext *s) +{ + /* The current code uses uint8_t for tcg operations. */ + tcg_debug_assert(tcg_op_defs_max <=3D UINT8_MAX); + + /* Registers available for 32 bit operations. */ + tcg_target_available_regs[TCG_TYPE_I32] =3D BIT(TCG_TARGET_NB_REGS) - = 1; + /* Registers available for 64 bit operations. */ + tcg_target_available_regs[TCG_TYPE_I64] =3D BIT(TCG_TARGET_NB_REGS) - = 1; + /* + * The interpreter "registers" are in the local stack frame and + * cannot be clobbered by the called helper functions. However, + * the interpreter assumes a 128-bit return value and assigns to + * the return value registers. + */ + tcg_target_call_clobber_regs =3D + MAKE_64BIT_MASK(TCG_REG_R0, 128 / TCG_TARGET_REG_BITS); + + s->reserved_regs =3D 0; + tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP); + tcg_regset_set_reg(s->reserved_regs, TCG_REG_CALL_STACK); + + /* The call arguments come first, followed by the temp storage. */ + tcg_set_frame(s, TCG_REG_CALL_STACK, TCG_STATIC_CALL_ARGS_SIZE, + TCG_STATIC_FRAME_SIZE); +} + +/* Generate global QEMU prologue and epilogue code. */ +static inline void tcg_target_qemu_prologue(TCGContext *s) +{ +} + +static void tcg_out_tb_start(TCGContext *s) +{ + /* nothing to do */ +} + +bool tcg_target_has_memory_bswap(MemOp memop) +{ + return true; +} + +static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l) +{ + g_assert_not_reached(); +} + +static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *l) +{ + g_assert_not_reached(); +} diff --git a/tcg/wasm32/tcg-target.h b/tcg/wasm32/tcg-target.h new file mode 100644 index 0000000000..bd03aa1bc4 --- /dev/null +++ b/tcg/wasm32/tcg-target.h @@ -0,0 +1,76 @@ +/* + * Tiny Code Generator for QEMU + * + * Copyright (c) 2009, 2011 Stefan Weil + * + * Permission is hereby granted, free of charge, to any person obtaining a= copy + * of this software and associated documentation files (the "Software"), t= o deal + * in the Software without restriction, including without limitation the r= ights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or se= ll + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OT= HER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING= FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS = IN + * THE SOFTWARE. + */ + +/* + * This code implements a TCG which does not generate machine code for some + * real target machine but which generates virtual machine code for an + * interpreter. Interpreted pseudo code is slow, but it works on any host. + * + * Some remarks might help in understanding the code: + * + * "target" or "TCG target" is the machine which runs the generated code. + * This is different to the usual meaning in QEMU where "target" is the + * emulated machine. So normally QEMU host is identical to TCG target. + * Here the TCG target is a virtual machine, but this virtual machine must + * use the same word size like the real machine. + * Therefore, we need both 32 and 64 bit virtual machines (interpreter). + */ + +#ifndef TCG_TARGET_H +#define TCG_TARGET_H + +#define TCG_TARGET_INTERPRETER 1 +#define TCG_TARGET_INSN_UNIT_SIZE 4 +#define MAX_CODE_GEN_BUFFER_SIZE ((size_t)-1) + +/* Number of registers available. */ +#define TCG_TARGET_NB_REGS 16 + +/* List of registers which are used by TCG. */ +typedef enum { + TCG_REG_R0 =3D 0, + TCG_REG_R1, + TCG_REG_R2, + TCG_REG_R3, + TCG_REG_R4, + TCG_REG_R5, + TCG_REG_R6, + TCG_REG_R7, + TCG_REG_R8, + TCG_REG_R9, + TCG_REG_R10, + TCG_REG_R11, + TCG_REG_R12, + TCG_REG_R13, + TCG_REG_R14, + TCG_REG_R15, + + TCG_REG_TMP =3D TCG_REG_R13, + TCG_AREG0 =3D TCG_REG_R14, + TCG_REG_CALL_STACK =3D TCG_REG_R15, +} TCGReg; + +#define HAVE_TCG_QEMU_TB_EXEC + +#endif /* TCG_TARGET_H */ --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747745669; cv=none; d=zohomail.com; s=zohoarc; b=YYYuHTprGUe9fxgtQEsP4ACeT8KZ3WE7p6VJMDP+rW5NaEgSIPkZk8sWwN/X8SpqdUuOsxFIp+1ZiGkKm7Ah9K/YIBdBpwrKHNLrIl7fdGU1cTnh/IYhre0QlT5qeVY+iohoLV5fhl7jXKMybHo1CARfwhqj7LkTbZdTQ6XG354= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747745669; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=YCCbP2uzO+MJ8NbcqqepaB7q1+AuafpCtigscHDquGQ=; b=bvUI606r0KLBnDsei2D5sFLFiCd6Cv7muO3HBI1ZnZpk3KoRDQz+cMr+mgcxPFaPXNVN8Konbq9jy8BEtGfdxhsnbdKMCv9HmpZNb9QksSIo8YmzUF00zt+ZPWK2agd4pjnBU6WaO7UCdqTKAy1D4gfAWdv+8hzZMnGWSKil1mw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747745669176475.9177490528622; Tue, 20 May 2025 05:54:29 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMSS-0000hL-An; Tue, 20 May 2025 08:52:36 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMSQ-0000fz-Og; Tue, 20 May 2025 08:52:34 -0400 Received: from mail-pj1-x1031.google.com ([2607:f8b0:4864:20::1031]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMSN-0000g8-Vp; Tue, 20 May 2025 08:52:34 -0400 Received: by mail-pj1-x1031.google.com with SMTP id 98e67ed59e1d1-30820167b47so4510967a91.0; Tue, 20 May 2025 05:52:28 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.52.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:52:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745547; x=1748350347; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YCCbP2uzO+MJ8NbcqqepaB7q1+AuafpCtigscHDquGQ=; b=IQGi1tifn3QhPTv/JIGKD0pYM3ppL4DvQDHPaLqlK0jq8VNwfQDq/s0BU+plW+IQVA t7fyc82jjZKUNWe+Juz1nhr+37CiJhTjeia1D7rtBrxTjcIY/BKZcC+76lsUO0dN5PPe uvwzwFvvj5XyHu5/vx896JNVC1Fr4zVLA20Wv4GByJEqT7FHFYljHHO2dBROc2rIBbGX /qSjh19Gs5/4cFAPYfTG17iL4fY99UILST93Fey8yADI0TIxX5cjR6zPH3/UstD+wQsx gjzblLpsSAkPDaHWF8BC7xtn5eN0o6eBL9WOZibHFDxh/b1eteV1dhhvMRQb/OCLHyX1 oWaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745547; x=1748350347; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YCCbP2uzO+MJ8NbcqqepaB7q1+AuafpCtigscHDquGQ=; b=v6c2bXLgVX1t0zm35HFkS1ncD3Fhowh0+MbbL6I86pSYKjmjkhV5CUXaCxW54OH/zi 6tpi/KEy8Kr53k7ujwTtD1RU30iZrHVAl/z5IdWpgl/GCFGBS0UX/ADv8s/wJ3DVA3/r v+swvm+x0MX7dTaveKvR5Ed8H32uAvMyj2ExNeefT4ceXIhUxhvsO0GlkClYFPPBgbfT IVmnhTzID8BZoIjKdB5XqdYHr+b1snxyitlz0zS839kMsxuyqe4ZMUb4+PFL9kOh5Vwu aezfYAvjOqiawDtrxcYVWzk170K1kFpU7Npt2DqoMfFdrLbqnaKVTXex8JETk0d0qXsl yQEA== X-Forwarded-Encrypted: i=1; AJvYcCVw4xUqM2kFwUbheN6e/TPBC1qthK8UB2XjhQBcLjp8KF790+i5HGmpnT3qiOe0Bg7w6hB3dLuNMTgVgg==@nongnu.org, AJvYcCW9Q3Nn2u5apX2EKeV5zaxEspGLCaKvOx6hM8PtSrU2yUCVAdOxwHZKbN70KgGa3T8WpMdhMXSWBA==@nongnu.org X-Gm-Message-State: AOJu0Yy1U2waPBo/EbRKsGmT0GfgOuEMD/P8LEjRq18dAvhRZaJ7z1a4 oyLJ826bVhQG/OZJ9fgUMNjYrge0NZFDjLmY7ZYkFdrYrHsLl5M5YwEwroxY20Eo X-Gm-Gg: ASbGncvI34Ipbn0fisJZnW7gncMcxUdFFMX5IeQPK0AUBWTWiVGVX3i3u7X5mHr/l0C blGyYchwzqwhQa/IgjKTezM8W0bCcOEWBhUVFxQ8QE1Ysy3kqV2ijZ8sM85WlFV2PBz/iVecHM+ EncnzWlGwKZtKPYjp5rFjk8NkCZkODjibmO9Jl8lPU0Guj4a9WLa7Jz+N6JQVLpwWeWdClzlS3+ r9/ggBgWVjZwBDdfVS0RYdI8l5n9XB7Kh7K9y0zv0UPI1hxHVQgCEmCoUvvTa3qS5N0kB9eMKEx nqEEviulfFwP8eTBYmt+p4+Z+GsH84u0nC0/RGI+YZBVoH4Gd7c= X-Google-Smtp-Source: AGHT+IEjBag9dXZJu+xHTsTGS7j8HoI7T587dh1AvKbVfugRzu2h05FUzil69GsAslX9TWOh2Ke26Q== X-Received: by 2002:a17:90a:e7c2:b0:30e:8e0f:9bfc with SMTP id 98e67ed59e1d1-30e8e0f9f15mr21819390a91.13.1747745547437; Tue, 20 May 2025 05:52:27 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 02/33] tcg/wasm32: Do not use TCI disassembler in Wasm backend Date: Tue, 20 May 2025 21:51:04 +0900 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1031; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pj1-x1031.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747745670244116600 Content-Type: text/plain; charset="utf-8" Wasm backend should implement its own disassember for Wasm instructions. Signed-off-by: Kohei Tokunaga --- tcg/wasm32.c | 243 +-------------------------------------------------- 1 file changed, 1 insertion(+), 242 deletions(-) diff --git a/tcg/wasm32.c b/tcg/wasm32.c index 6de9b26b76..4bc53d76d0 100644 --- a/tcg/wasm32.c +++ b/tcg/wasm32.c @@ -831,246 +831,5 @@ uintptr_t QEMU_DISABLE_CFI tcg_qemu_tb_exec(CPUArchSt= ate *env, } =20 /* - * 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] =3D { - "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", - "r8", "r9", "r10", "r11", "r12", "r13", "env", "sp" - }; - - QEMU_BUILD_BUG_ON(TCG_AREG0 !=3D TCG_REG_R14); - QEMU_BUILD_BUG_ON(TCG_REG_CALL_STACK !=3D 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] =3D { - [TCG_COND_NEVER] =3D "never", - [TCG_COND_ALWAYS] =3D "always", - [TCG_COND_EQ] =3D "eq", - [TCG_COND_NE] =3D "ne", - [TCG_COND_LT] =3D "lt", - [TCG_COND_GE] =3D "ge", - [TCG_COND_LE] =3D "le", - [TCG_COND_GT] =3D "gt", - [TCG_COND_LTU] =3D "ltu", - [TCG_COND_GEU] =3D "geu", - [TCG_COND_LEU] =3D "leu", - [TCG_COND_GTU] =3D "gtu", - [TCG_COND_TSTEQ] =3D "tsteq", - [TCG_COND_TSTNE] =3D "tstne", - }; - - assert((unsigned)c < ARRAY_SIZE(cond)); - assert(cond[c][0] !=3D 0); - return cond[c]; -} - -/* Disassemble TCI bytecode. */ -int print_insn_tci(bfd_vma addr, disassemble_info *info) -{ - const uint32_t *tb_ptr =3D (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 =3D *tb_ptr++; - - info->fprintf_func(info->stream, "%08x ", insn); - - op =3D extract32(insn, 0, 8); - def =3D &tcg_op_defs[op]; - op_name =3D 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, pt= r); - 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 =3D=3D 0) { - info->fprintf_func(info->stream, "align"); - break; - } - /* fall through */ - - default: - info->fprintf_func(info->stream, "illegal opcode %d", op); - break; - } - - return sizeof(insn); -} --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747746315; cv=none; d=zohomail.com; s=zohoarc; b=IqOFJG9Gaem1/qbvkNyZwE/uzLyDUbv7RO76ELFX1FRp2XBZlI1Rf/wIXLGA0Up6ZtequvQfJWwsFwTfY6X+j7ngAeVAyjJnI1UmAfO9s4FqBvx2LGWu+/D86+p24dlXj1RThOdtVb8y6+parN6WDl3yUKA2+rvmmU7O+f25Htk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747746315; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=NZx+To6+GeRUeSP4cgenq374zNADHJo0hk3kjG6JK1E=; b=I8vfKtNTilpsXKUbQ1y6LT6aCt7sIgycht8B/LOhTDKtiNHs18e+oJZCXedwdysDEYNun0wUEtI3V/o+4Jx4NHLmUibH1bma577/y8hdhBv6YvYF14DNDzCBpCqEuuH5sg4DhNg6++ZfX0LQN/s4s6LCqlvn8bQ0wsNMy677ov4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747746315979607.2889980620236; Tue, 20 May 2025 06:05:15 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMSX-0000iv-9a; Tue, 20 May 2025 08:52:41 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMSV-0000iF-28; Tue, 20 May 2025 08:52:39 -0400 Received: from mail-pj1-x102e.google.com ([2607:f8b0:4864:20::102e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMSS-0000hH-LN; Tue, 20 May 2025 08:52:38 -0400 Received: by mail-pj1-x102e.google.com with SMTP id 98e67ed59e1d1-30e7bfef27dso3610205a91.0; Tue, 20 May 2025 05:52:34 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.52.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:52:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745553; x=1748350353; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=NZx+To6+GeRUeSP4cgenq374zNADHJo0hk3kjG6JK1E=; b=iAv5IEFflmrPbvwGwVAqgT8ABtdeJv0LbiTZLHto7MNQxBpG9Ohc89JwEN+8txV6Qv SQ7Hihg5Johd51J6smQpvatvle6Qn9q1oX/pkqQZ4pthHDL1m/x7DK/TzBA3UanDLCkF yevbcd3gKH6VYISnK5fxdUnKjQnzl/gfkwbb3BvARNbF4rwABuEEhh104MtN45eyNn2I sYZXX4rW6REPF0Doe3EMCTwsz6c61U5R886tF5atoYBJRGtNxFyNqRZ+K4FgMgd2+Ay9 +BYc10h3uvamJTZ0buaMY7yMjuIuio7YtmXZ6VM4ics049E4FkGhVOdGfwVeUvgydHfY KoKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745553; x=1748350353; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NZx+To6+GeRUeSP4cgenq374zNADHJo0hk3kjG6JK1E=; b=MUhbZg4ubdrw7ve3tXGRkNc4qIXPTVfboORUCFmDMYuLIA7q+64o2jKP1awfePb+An fEx3s2wm02XIyaD0782RLKvgSIVSKjkYAnMn0MblYcAWHk9o548EboqCSlN9V4unJ6WD wGs8P3CCElFSNPLetwAReZupgsMF9Gp90uw6p1QX0T67Tc/JZTQFNxregaiowmfiYegF KweP3wZ5HtwrV1QzYf955zfe9erur9UYJ/Gz10hpBovqI4Z8txrdCjlucCdqPRmqAXb5 pYF0n6VKtwqGB5+i3z4XyXOjsBq3ozD0aUajvzYCLDNvFd6oR+Ytt+4nvUtDTzP2TmJ9 adWA== X-Forwarded-Encrypted: i=1; AJvYcCU1KtyVjyauGZ1S6RUi5LUZ7w4BMFV8tWDDYp88cT8Uyrf93VeJ9Vfr0j8mxuGCU9qQLYJ23knJiVEmVA==@nongnu.org, AJvYcCWfIwqEXwbxk8ff+8Uu6gzSklC+v7CaKsgSKNyDaPdRbVlGNjM0RbPKpBaX3ZcC5qHoNTrPsbB81A==@nongnu.org X-Gm-Message-State: AOJu0YxSNAYMlcDXGxhszIwtZ1/23rY/bCa2E93F5jK3F5gnuFjc9BY/ YerKyo8Iny2Y6P+15eO6DQn+Xcsz9pIhpkJWJojYZ5m/WhhI0jVILE55rs8gdVAJ X-Gm-Gg: ASbGncvFPa1x8hLy57Y4o/fTiG79dCWK59mboPLb/w4ITlVYnCIkjbxWIB+HFgOeidL vl+JD2mEtTLDKyuviGrEPyvoFgScKxro/a9qk5vel0IHxCG7DsXUx+w5B2NQQJCFwrRFWlnc993 p+lAlfaJUECmRPVq5G6RFGHYv52jGb4HlHLhKNyFZqXu6RYFiF0w46XRKamvnMIrTa1RUfyRGCe 0r/3uxCjPpk1OdHHQsKn8cl6ToG6/W3PbtmrRrsIfeMzukwefVwD59Td9Bz4JzQFvQDmtJSJLaC kd7Nfbpccr/oUA0dKcYIPXbO2zZ7BtXeb23tNw6ThHvJj6XdZLKonUXSD3EeHw== X-Google-Smtp-Source: AGHT+IHBlXaJvgNi8ZmIupZbQwe/gK71oK9VqvVg9wYGIpSs4wakRV4DDk3Cz065hGwWCWDovzWzVA== X-Received: by 2002:a17:90b:574b:b0:30a:5a9a:9fec with SMTP id 98e67ed59e1d1-30e830c4f3emr25133773a91.7.1747745553046; Tue, 20 May 2025 05:52:33 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 03/33] meson: Enable to build wasm backend Date: Tue, 20 May 2025 21:51:05 +0900 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::102e; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pj1-x102e.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747746318384116600 Content-Type: text/plain; charset="utf-8" Now that there is a backend for WebAssembly build (/tcg/wasm32/), the requirement of --enable-tcg-interpreter in meson.build can be removed. Signed-off-by: Kohei Tokunaga --- meson.build | 4 ---- 1 file changed, 4 deletions(-) diff --git a/meson.build b/meson.build index ad2053f968..c533243157 100644 --- a/meson.build +++ b/meson.build @@ -911,10 +911,6 @@ if have_tcg if not get_option('tcg_interpreter') error('Unsupported CPU @0@, try --enable-tcg-interpreter'.format(cpu= )) endif - elif host_arch =3D=3D 'wasm32' - if not get_option('tcg_interpreter') - error('WebAssembly host requires --enable-tcg-interpreter') - endif elif get_option('tcg_interpreter') warning('Use of the TCG interpreter is not recommended on this host') warning('architecture. There is a native TCG execution backend availab= le') --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747746410; cv=none; d=zohomail.com; s=zohoarc; b=m2Dp7X4bd44eXoTY8AIjELrEbfx54802UHf6uTXoOrlMFHSEaNAwXz5gXth2JkcLJftv0jAK1PtSHj9OBJ7lGn21Tw/AEoJo14jpdwSaaJ/0uD3TuGxPwGbxYFpT6ibzoWUgBelravw7lBGaLGDcKPogv8K5ZiMzk+BZtOi6bqk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747746410; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=T0PaUQzzHF5lFv29qjDHJ20TRr//FMkUoGozxMxxEDU=; b=GthahOW2P3txBE9PqBo+ayX0xf0o7LoTVnIcbbRgH3B4XcXeXflBaYYIDc0pgEWY62JMvo5nA9MBkShQ90GzE2DtGrK8f1a+FSPnQj2YVaLx7LGips8UvHPsstG90LaAdAYfKWmooBAh7NCI9uqtl5zwpht8IY8GP7wcvfm0Iqo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 174774641049075.27584051795168; Tue, 20 May 2025 06:06:50 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMSb-0000kK-AU; Tue, 20 May 2025 08:52:45 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMSa-0000k4-A2; Tue, 20 May 2025 08:52:44 -0400 Received: from mail-pj1-x1035.google.com ([2607:f8b0:4864:20::1035]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMSX-0000iZ-RA; Tue, 20 May 2025 08:52:44 -0400 Received: by mail-pj1-x1035.google.com with SMTP id 98e67ed59e1d1-30e80f03760so3988330a91.0; Tue, 20 May 2025 05:52:40 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.52.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:52:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745559; x=1748350359; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=T0PaUQzzHF5lFv29qjDHJ20TRr//FMkUoGozxMxxEDU=; b=FS3V0Pvzr2N10i15ejl4MWc5HhSaoGPjbjgxb+gnshBGDqxaR5/mT+w1dzsEfHQyzl ssPrxvM44s5qcgTF0+H+VBKrsVOpH1MIpUvTZty7t8Dhte8MnKGFakBgtygxedzNK8NQ Qbk0bgzDj8cEoD1/EruOMuViML2FsONiwW8mZ8oG7qP+UxufcdOH+R4C1TR4CWqp2Iv4 hIk7QftCfBvBOx/M+FahQZNHFia5Zca08k5asF0aAKCkCDaj9FhYT49WWXPF/UmH4bAQ xi3XNZFJxWY+e1hR9pH7wtXAhOVmhO8uRnrDXOHrUZD8ZQ+If8gdohYFykSWbFY3DlXD bCHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745559; x=1748350359; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=T0PaUQzzHF5lFv29qjDHJ20TRr//FMkUoGozxMxxEDU=; b=EHAafzbqmC0nfRJD7UXVSw2b6isGgmfSlPlOpt0D5Qg41TgJ6/n8hzSyXQiFJCPVLj B9PJSUzu/7loASUw+u/SARxRkCMVFd87W/v5l72DqZlpKF7TUtXL/aKZQ3eraSt5Of0i GCy31OALFDzYqAbfm2UJfR8aKRXkhPN7BtnjNdGVq2kOlL/YT71EWeBUd9tx3i2K2+7t Q3jnip2xtkwFqgYhEestDIFdmwGPD+MiF8XXLWx4hDp3JuDIT/vDjNMgjnCT3mlr1Cf/ 6I+QzPnIP3vboquP9QdsXyhyJqmufn/YpwG3dzzUQ7Ky3ZQzwKeBlmUoXoqeiQTkVrtP nryg== X-Forwarded-Encrypted: i=1; AJvYcCU5bjgQMXlDh5qnFCjFznM+WwlUjRx6v8M+sdy8fHaIpa63qTAYirRLkoPPC1ehgQJ9cZUPg4oprZ200A==@nongnu.org, AJvYcCVHb+O5FG25BH3j2/0N/b/Xl1gnuaHPuNXZ7UVEaKpDyWPfcBPaV7CdIWsGdrnc6CGsNhnxMTGnrQ==@nongnu.org X-Gm-Message-State: AOJu0Yzf9gv8G8FaQWQiNpuNcHSm5TF/J/FG5bzONl55XpEgQdZocFq0 sEiX9IJEOV3zz68rQBtqNKyqQgf+T4as07mGORjFwxSTDlyBCoGT6+NLmrAU9/+b X-Gm-Gg: ASbGncuKr1e7rZbY7HY5xB5H323UVHn7f85UdkqAJcIgrrWjXY9WZoK3YvxPLC/dp0E /0jQdV7uHxytJlO0QDD66JQHfEWefo1QlzcBuVxjRLmicsaMBs+Vm8IME1eE3oe3xglpluUJg6I F4YjrmvlG2dbc1rlvQcrDSHvEU4r9qsUQIQcGIFgIT3qLG2CoMxYETYsLQWpSQCYKITETfrCEWV ybuDns4gHWiPRr11+W661/znJ3Y7CX67X8Oh76Fc45uhBy9KqFCebpktgbiTJ+McdUQ5EknVoom nq/zWLPsY0lE8osv2/Pi1Sj3ijIcZ4YslyZmQXuf1IRTBz32z+w= X-Google-Smtp-Source: AGHT+IG4z7uH3nJBTNDQfyLfNQV18rt8GBswaQum9LIXlORmpxAC/0+aqvV7Ejz1IYPaI3IHgNwiMg== X-Received: by 2002:a17:90b:3a48:b0:2ff:72f8:3708 with SMTP id 98e67ed59e1d1-30e8314fdb5mr30880280a91.17.1747745558661; Tue, 20 May 2025 05:52:38 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 04/33] tcg/wasm32: Set TCG_TARGET_INSN_UNIT_SIZE to 1 Date: Tue, 20 May 2025 21:51:06 +0900 Message-ID: <9e2af431443c9b4b70f1fc1ee02e1030318c0b8f.1747744132.git.ktokunaga.mail@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1035; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pj1-x1035.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747746413174116600 Content-Type: text/plain; charset="utf-8" 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. Signed-off-by: Kohei Tokunaga --- tcg/wasm32/tcg-target.c.inc | 40 ++++++++++++++++++++----------------- tcg/wasm32/tcg-target.h | 2 +- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/tcg/wasm32/tcg-target.c.inc b/tcg/wasm32/tcg-target.c.inc index 33b81f1fe2..126f9c0de7 100644 --- a/tcg/wasm32/tcg-target.c.inc +++ b/tcg/wasm32/tcg-target.c.inc @@ -39,6 +39,8 @@ #endif #define TCG_TARGET_CALL_RET_I128 TCG_CALL_RET_NORMAL =20 +typedef uint32_t tcg_insn_unit_tci; + static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op, TCGType type, unsigned flags) { @@ -96,16 +98,18 @@ static const char *const tcg_target_reg_names[TCG_TARGE= T_NB_REGS] =3D { }; #endif =20 -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 =3D (tcg_insn_unit_tci *)code_ptr_i; intptr_t diff =3D value - (intptr_t)(code_ptr + 1); =20 tcg_debug_assert(addend =3D=3D 0); tcg_debug_assert(type =3D=3D 20); =20 if (diff =3D=3D 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; @@ -122,7 +126,7 @@ static void stack_bounds_check(TCGReg base, intptr_t of= fset) =20 static void tcg_out_op_l(TCGContext *s, TCGOpcode op, TCGLabel *l0) { - tcg_insn_unit insn =3D 0; + tcg_insn_unit_tci insn =3D 0; =20 tcg_out_reloc(s, s->code_ptr, 20, l0, 0); insn =3D deposit32(insn, 0, 8, op); @@ -131,14 +135,14 @@ static void tcg_out_op_l(TCGContext *s, TCGOpcode op,= TCGLabel *l0) =20 static void tcg_out_op_p(TCGContext *s, TCGOpcode op, void *p0) { - tcg_insn_unit insn =3D 0; + tcg_insn_unit_tci insn =3D 0; intptr_t diff; =20 /* Special case for exit_tb: map null -> 0. */ if (p0 =3D=3D NULL) { diff =3D 0; } else { - diff =3D p0 - (void *)(s->code_ptr + 1); + diff =3D p0 - (void *)(s->code_ptr + 4); tcg_debug_assert(diff !=3D 0); if (diff !=3D sextract32(diff, 0, 20)) { tcg_raise_tb_overflow(s); @@ -151,7 +155,7 @@ static void tcg_out_op_p(TCGContext *s, TCGOpcode op, v= oid *p0) =20 static void tcg_out_op_r(TCGContext *s, TCGOpcode op, TCGReg r0) { - tcg_insn_unit insn =3D 0; + tcg_insn_unit_tci insn =3D 0; =20 insn =3D deposit32(insn, 0, 8, op); insn =3D deposit32(insn, 8, 4, r0); @@ -165,7 +169,7 @@ static void tcg_out_op_v(TCGContext *s, TCGOpcode op) =20 static void tcg_out_op_ri(TCGContext *s, TCGOpcode op, TCGReg r0, int32_t = i1) { - tcg_insn_unit insn =3D 0; + tcg_insn_unit_tci insn =3D 0; =20 tcg_debug_assert(i1 =3D=3D sextract32(i1, 0, 20)); insn =3D deposit32(insn, 0, 8, op); @@ -176,7 +180,7 @@ static void tcg_out_op_ri(TCGContext *s, TCGOpcode op, = TCGReg r0, int32_t i1) =20 static void tcg_out_op_rl(TCGContext *s, TCGOpcode op, TCGReg r0, TCGLabel= *l1) { - tcg_insn_unit insn =3D 0; + tcg_insn_unit_tci insn =3D 0; =20 tcg_out_reloc(s, s->code_ptr, 20, l1, 0); insn =3D deposit32(insn, 0, 8, op); @@ -186,7 +190,7 @@ static void tcg_out_op_rl(TCGContext *s, TCGOpcode op, = TCGReg r0, TCGLabel *l1) =20 static void tcg_out_op_rr(TCGContext *s, TCGOpcode op, TCGReg r0, TCGReg r= 1) { - tcg_insn_unit insn =3D 0; + tcg_insn_unit_tci insn =3D 0; =20 insn =3D deposit32(insn, 0, 8, op); insn =3D deposit32(insn, 8, 4, r0); @@ -197,7 +201,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 =3D 0; + tcg_insn_unit_tci insn =3D 0; =20 tcg_debug_assert(m2 =3D=3D extract32(m2, 0, 16)); insn =3D deposit32(insn, 0, 8, op); @@ -210,7 +214,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 =3D 0; + tcg_insn_unit_tci insn =3D 0; =20 insn =3D deposit32(insn, 0, 8, op); insn =3D deposit32(insn, 8, 4, r0); @@ -222,7 +226,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 =3D 0; + tcg_insn_unit_tci insn =3D 0; =20 tcg_debug_assert(i2 =3D=3D sextract32(i2, 0, 16)); insn =3D deposit32(insn, 0, 8, op); @@ -235,7 +239,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 =3D 0; + tcg_insn_unit_tci insn =3D 0; =20 tcg_debug_assert(b2 =3D=3D extract32(b2, 0, 6)); tcg_debug_assert(b3 =3D=3D extract32(b3, 0, 6)); @@ -250,7 +254,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 =3D 0; + tcg_insn_unit_tci insn =3D 0; =20 insn =3D deposit32(insn, 0, 8, op); insn =3D deposit32(insn, 8, 4, r0); @@ -263,7 +267,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 =3D 0; + tcg_insn_unit_tci insn =3D 0; =20 tcg_debug_assert(b3 =3D=3D extract32(b3, 0, 6)); tcg_debug_assert(b4 =3D=3D extract32(b4, 0, 6)); @@ -279,7 +283,7 @@ static void tcg_out_op_rrrbb(TCGContext *s, TCGOpcode o= p, TCGReg r0, static void tcg_out_op_rrrr(TCGContext *s, TCGOpcode op, TCGReg r0, TCGReg r1, TCGReg r2, TCGReg r3) { - tcg_insn_unit insn =3D 0; + tcg_insn_unit_tci insn =3D 0; =20 insn =3D deposit32(insn, 0, 8, op); insn =3D deposit32(insn, 8, 4, r0); @@ -293,7 +297,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 =3D 0; + tcg_insn_unit_tci insn =3D 0; =20 insn =3D deposit32(insn, 0, 8, op); insn =3D deposit32(insn, 8, 4, r0); @@ -452,7 +456,7 @@ static void tcg_out_call(TCGContext *s, const tcg_insn_= unit *func, const TCGHelperInfo *info) { ffi_cif *cif =3D info->cif; - tcg_insn_unit insn =3D 0; + tcg_insn_unit_tci insn =3D 0; uint8_t which; =20 if (cif->rtype =3D=3D &ffi_type_void) { diff --git a/tcg/wasm32/tcg-target.h b/tcg/wasm32/tcg-target.h index bd03aa1bc4..2f0c27c905 100644 --- a/tcg/wasm32/tcg-target.h +++ b/tcg/wasm32/tcg-target.h @@ -41,7 +41,7 @@ #define TCG_TARGET_H =20 #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) =20 /* Number of registers available. */ --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747745661; cv=none; d=zohomail.com; s=zohoarc; b=mAAz4WFZS52euosuRi1t0ybELl5kuy317RuWTKRxbdLM9+7c/iRgYGdAyFVO6wTQCYKI1ENd3SuJRAz3P597RQDFBekF1Hu/Ng9fBFkbQhEIZaJrh5pcuEiO0DQRDzESTcqYQBFnbpmeEW791cGXS2XaXpiIw9AjmoDQgLAl93g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747745661; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=p6Z5PLd1E/Owph8lAEt4RDxXTFlaySbmORvNcNjfV1I=; b=WRuMlxMyucTb+kMEfaBvt6C5hbo5a9gUBtUjaDfc9X/Hd1J/RyKsV63S/DoiflffIJd/Ae5inN2H5EqhzLC0nXYinGYFPxb9mr1QCx8+mzm+4XlEnfH8AeboCfc3o1iRfUXhNzMG6LBUk60dC9AK4GhoJEyTjMJP6K40bjERl7I= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747745660996330.9442180830483; Tue, 20 May 2025 05:54:20 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMSh-0000mQ-Ho; Tue, 20 May 2025 08:52:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMSf-0000lj-HM; Tue, 20 May 2025 08:52:49 -0400 Received: from mail-pj1-x102d.google.com ([2607:f8b0:4864:20::102d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMSd-0000jj-40; Tue, 20 May 2025 08:52:49 -0400 Received: by mail-pj1-x102d.google.com with SMTP id 98e67ed59e1d1-306b6ae4fb2so4489590a91.3; Tue, 20 May 2025 05:52:45 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.52.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:52:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745564; x=1748350364; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=p6Z5PLd1E/Owph8lAEt4RDxXTFlaySbmORvNcNjfV1I=; b=Q1tT0WNUwH+JTbdmAESN1DVQEuZ9kVJMKfCWJfOL4AwIvBmxjNMedMtm2IvKkdER0c OqKfiyfeowoIoxcvDgQVqDjW1ZR4VNJwZLXjFA5+GfMxqVxLYBZV3VMOnFhXmTa6tQBj 2s0CZIv0JYGL8qfixq/ZN7FVHVGTN2GWHW0KbzyvXdrw6VOtleVUyn5rS/slL5pAgRj7 Ebqc3UBreBbu3vaxkkohoBp5wqFv5cFmWIg2szcd8X83+3y4SDkN8Ii26DAKgGzKYxR8 vrRedEGn2sKg21dawM0nneWCyFdMOM/fCS6n8vGzNcvJZWh58WWdWyxBvCgmx/SGYiHa +J7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745564; x=1748350364; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=p6Z5PLd1E/Owph8lAEt4RDxXTFlaySbmORvNcNjfV1I=; b=W6B0ofh30IyEt1ejo53hSEiOmQ4FjYcicnY/pmoYz7ETE9gWMhESHIj7re+P3Op22c jkB9loR1/B86e7A+5uSrRLLdMrjpJxtd8QujYYf5meckQBn8/7UPkWPray1zhM712VTG BqeZb7WwpbWhUTryv8RsBR5eUUuCfi4TyUbqwLNNIGXCUZvm89+6e1HSHBhEwwb5RqOZ gdkdRRkOb49ZqEd/QlFtGSy7QGF3WMDXOleYXMhXI2i1jri1yIG6Cc9wsuQwNqBuJU1i 0BJ62/deDnd5wXUJIIlKctgdJ6EZFUEsdtK2Jkzjsgey3tMd23jRM5j5tV/mBEcVwaAY uPTQ== X-Forwarded-Encrypted: i=1; AJvYcCUSoVGFtUKP34nT9xGNlUKau+6LtoJ6iBH70I+EwmbldoinIF0aOX9d90xo3UJSNyXgAFs+ddYkYnd0Og==@nongnu.org, AJvYcCWuNIJiaJ68+RofakNlBcA668bEmtcXeBDT1Y7YLz7KeTYQA7zBNpQ1rTrE4Oy0AQj3r2ylbuSvFg==@nongnu.org X-Gm-Message-State: AOJu0Yw3sO7bpIbbFcxyHhdAeUs+PoVIAMfSJ5FnrfRKOCJVQJyHt1NM xcLL5b+dSNEhnlPgbYptvXpr3RZ4Z/BYL12cMeSXop9I8/9W/f1GKCpAvR7qcVek X-Gm-Gg: ASbGncsj+Q00nPmdUcfawA7nMzoizUrh3RbDB1LcAHxun2Bm9qs9V1yAsrTSNscGYNH flu9IYqpi5YpSTEfyqduSPxjk4R/bceAUsMpHDlb1Zwh4ZZDE8DFUyXUcYlG3OsrbkYdMSVYDPy t1LWAZ/Yw5eCSujUb3jWikjE2oL7OyiSwYPNvXk7yNIPnduyu+siphWCoyJmYL4MVzd8jI0kneM lcyq1U8GjMqMFa5kDGr1LQFHfzoVvVvPh6zjLvVoG2IO28G9n5gaDIki62ycyYqfyhSL+YiOqG+ ZzNgEq5u8g+a7oJM/E6sN1jfpriwfOAjGoz8DlPdMu5Uuv+44ug= X-Google-Smtp-Source: AGHT+IGav8G7VTphYSuRGrIc749qNZXTTFQq2XCpKhtKKY5hmmw6L/nazwAm2gCLphSuQxNyXadgJA== X-Received: by 2002:a17:90a:e7cf:b0:30a:9025:84d1 with SMTP id 98e67ed59e1d1-30e7d558d59mr29217944a91.16.1747745564197; Tue, 20 May 2025 05:52:44 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 05/33] tcg/wasm32: Add and/or/xor instructions Date: Tue, 20 May 2025 21:51:07 +0900 Message-ID: <582f109d49c5ef294ce7ad55ac75466f3128431a.1747744132.git.ktokunaga.mail@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::102d; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pj1-x102d.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747745662033116600 Content-Type: text/plain; charset="utf-8" This commit implements and, or and xor operations using Wasm instructions. Each TCG variable is mapped to a 64bit Wasm variable. In Wasm, and/or/xor instructions operate on values 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 separated buffer allocated via tcg_malloc(). These two code buffers must be merged into the final code buffer before tcg_gen_code returns. [1] https://webassembly.github.io/spec/core/binary/index.html Signed-off-by: Kohei Tokunaga --- tcg/wasm32/tcg-target.c.inc | 137 +++++++++++++++++++++++++++++++++++- 1 file changed, 136 insertions(+), 1 deletion(-) diff --git a/tcg/wasm32/tcg-target.c.inc b/tcg/wasm32/tcg-target.c.inc index 126f9c0de7..e3a35c8415 100644 --- a/tcg/wasm32/tcg-target.c.inc +++ b/tcg/wasm32/tcg-target.c.inc @@ -98,6 +98,138 @@ static const char *const tcg_target_reg_names[TCG_TARGE= T_NB_REGS] =3D { }; #endif =20 +/* converts a TCG register to a wasm variable index */ +static const uint8_t tcg_target_reg_index[TCG_TARGET_NB_REGS] =3D { + 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 BUF_SIZE 1024 +typedef struct LinkedBuf { + struct LinkedBuf *next; + uint8_t data[BUF_SIZE]; + uint32_t size; +} LinkedBuf; + +static LinkedBuf *new_linked_buf(void) +{ + LinkedBuf *p =3D tcg_malloc(sizeof(LinkedBuf)); + p->size =3D 0; + p->next =3D NULL; + return p; +} + +static inline LinkedBuf *linked_buf_out8(LinkedBuf *buf, uint8_t v) +{ + if (buf->size =3D=3D BUF_SIZE) { + buf->next =3D new_linked_buf(); + buf =3D buf->next; + } + buf->data[buf->size++] =3D v; + return buf; +} + +static inline int linked_buf_len(LinkedBuf *buf) +{ + int total =3D 0; + for (LinkedBuf *p =3D buf; p; p =3D p->next) { + total +=3D p->size; + } + return total; +} + +static inline void linked_buf_write(LinkedBuf *buf, void *dst) +{ + for (LinkedBuf *p =3D buf; p; p =3D p->next) { + memcpy(dst, p->data, p->size); + dst +=3D p->size; + } +} + +/* + * wasm code is generataed in the dynamically allocated buffer which + * are managed as a linked list. + */ +__thread LinkedBuf *sub_buf_root; +__thread LinkedBuf *sub_buf_cur; + +static void init_sub_buf(void) +{ + sub_buf_root =3D new_linked_buf(); + sub_buf_cur =3D sub_buf_root; +} + +static inline int sub_buf_len(void) +{ + return linked_buf_len(sub_buf_root); +} + +static inline void tcg_wasm_out8(TCGContext *s, uint32_t v) +{ + sub_buf_cur =3D linked_buf_out8(sub_buf_cur, v); +} + +static void tcg_wasm_out_op_i64_and(TCGContext *s) +{ + tcg_wasm_out8(s, 0x83); +} +static void tcg_wasm_out_op_i64_or(TCGContext *s) +{ + tcg_wasm_out8(s, 0x84); +} +static void tcg_wasm_out_op_i64_xor(TCGContext *s) +{ + tcg_wasm_out8(s, 0x85); +} +static void tcg_wasm_out_op_var(TCGContext *s, uint8_t instr, uint8_t i) +{ + tcg_wasm_out8(s, instr); + tcg_wasm_out8(s, i); +} +static void tcg_wasm_out_op_global_get(TCGContext *s, uint8_t i) +{ + tcg_wasm_out_op_var(s, 0x23, i); +} +static void tcg_wasm_out_op_global_set(TCGContext *s, uint8_t i) +{ + tcg_wasm_out_op_var(s, 0x24, i); +} +static void tcg_wasm_out_op_global_get_r(TCGContext *s, TCGReg r0) +{ + tcg_wasm_out_op_global_get(s, tcg_target_reg_index[r0]); +} +static void tcg_wasm_out_op_global_set_r(TCGContext *s, TCGReg r0) +{ + tcg_wasm_out_op_global_set(s, tcg_target_reg_index[r0]); +} + +#define tcg_wasm_out_i64_calc(op) \ + static void tcg_wasm_out_i64_calc_##op( \ + TCGContext *s, TCGReg ret, TCGReg arg1, TCGReg arg2) \ + { \ + tcg_wasm_out_op_global_get_r(s, arg1); \ + tcg_wasm_out_op_global_get_r(s, arg2); \ + tcg_wasm_out_op_i64_##op(s); \ + tcg_wasm_out_op_global_set_r(s, ret); \ + } +tcg_wasm_out_i64_calc(and); +tcg_wasm_out_i64_calc(or); +tcg_wasm_out_i64_calc(xor); + static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, intptr_t value, intptr_t addend) { @@ -557,6 +689,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_i64_calc_and(s, a0, a1, a2); } =20 static const TCGOutOpBinary outop_and =3D { @@ -747,6 +880,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_i64_calc_or(s, a0, a1, a2); } =20 static const TCGOutOpBinary outop_or =3D { @@ -918,6 +1052,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_i64_calc_xor(s, a0, a1, a2); } =20 static const TCGOutOpBinary outop_xor =3D { @@ -1305,7 +1440,7 @@ static inline void tcg_target_qemu_prologue(TCGContex= t *s) =20 static void tcg_out_tb_start(TCGContext *s) { - /* nothing to do */ + init_sub_buf(); } =20 bool tcg_target_has_memory_bswap(MemOp memop) --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747745707; cv=none; d=zohomail.com; s=zohoarc; b=aq2EqhXzpsGuUKxVcSqu4mik9UegZyi62SvOdC+MC4KNcXP0qB8YlBGkG23e6HBEACY86NvxN5gCVh9kiBh9+OgmjM33DhaxgAgkTtlf6t4qXsGzNZFxSJdElwAA5CEgfiYm6uwg31MHqAMPAjrygGH7Lg5TxV5oUVNIeSZbMYo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747745707; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=a3aqhzF4R3IS+EMJ/1tKp0zJy/CqsldgOlglF6FuQWs=; b=KGKxB/gpDYBcBNQvviv6pjatXFnYexuENHkPxNfQOeRm+yb/XalU7kHdqFx/4i2BSRec6Bmpwb2be8WbATIYb3Hc/9t7PZ+4bpvVs/DBPIz/Les+u0IeYz3YtGqrCNaYUKf68RnBCpYHpcUPOE25DP7t7PqEhH53AD/iqxNXqO0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747745707485945.7603281873627; Tue, 20 May 2025 05:55:07 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMSn-0000nc-9N; Tue, 20 May 2025 08:52:57 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMSl-0000nH-Og; Tue, 20 May 2025 08:52:55 -0400 Received: from mail-pg1-x52c.google.com ([2607:f8b0:4864:20::52c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMSj-0000km-Pv; Tue, 20 May 2025 08:52:55 -0400 Received: by mail-pg1-x52c.google.com with SMTP id 41be03b00d2f7-b07d607dc83so4408265a12.1; Tue, 20 May 2025 05:52:51 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.52.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:52:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745570; x=1748350370; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=a3aqhzF4R3IS+EMJ/1tKp0zJy/CqsldgOlglF6FuQWs=; b=HA1WC3hu4zSh8PAcbUVBiNaz73QcLt7zeNxEOgXIbKK9/bbmHFVHpjbAaNHiNIQXcn RPCB2vMCK2k3FIOTtj6FKgrPx7iWZD6+ytMlf2T1GLl1919gs25i6Ygdu1sIGGnhy4bY Q5rVYrArWpKXNscFLcQeZXVss7skRSyxIDSvYuhhWPPxmUi4eE3V3ZYhuKkVJ/eJCiPb kmpNwGwNoWSKaiFt0vmSh1Vkv5cUpXSK+xbGS+Neifh+QMiKsL1pdZql3d+Rj6r42Sax 37cSU30Z+VX0bCPK2k2/lKxxcbBW/pP26qmx1skTzTFWOl+p6JgvNQu7qGLvoc/4gz/N OqHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745570; x=1748350370; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=a3aqhzF4R3IS+EMJ/1tKp0zJy/CqsldgOlglF6FuQWs=; b=iJLeB5PtrVjIbLAt3mW900KCrOY8EXZTU7TNW81a3UXlOl+VeChqFEkcA54J2Z+I1h L4a2L645e8ZkjptO7aHjk1W7LTplHlAmd3OjYNbD5eqjIbShphmb5jjcyC2/pCW45BXh 5KLfxjphF6OFgFW9EuLgf6DyI1IHWgu/A4lE8y9LWYsuKhzt17mTGz8BbaT8anUumMGG KBsxUKjUiPAAThEbnghUzgAWdLcuRkcm9j/azYmDMceUQ1sU6te+15ilPkwTXOhPQaNW 8K1hinlyW/2TNaReZUvj15LdP0vUrLZfwKRvuQV46YYsxfLhwia5GQIl+BrwpqXGED/c L0lQ== X-Forwarded-Encrypted: i=1; AJvYcCWKJTa6ZkC4bX6b0kXvwQaxq6F8elLd8m7YsKDq8KXBYDR9BuYZIycQGgj7Mt1pE2yB5hOc0ZtmFg==@nongnu.org, AJvYcCXwSS8rnIbyQaPiSEw0sGTwY9Zt07FSPrxjG+DyWh0IsEaeRRo6f1lX36UG9MduTRswZPXW+dSPYm6G7w==@nongnu.org X-Gm-Message-State: AOJu0Yyexx8nL/1VVVHMIE8eRqkeZtKN+W9cyTaIkGHabkZIG30ww5RR 14vQvYGGYNPi9OkRNfvq6NDWSUD9DEbqhtw7v6RaDRX6Et+hvyiyloBVtC4yrTYR X-Gm-Gg: ASbGncsHO4h3zs+dSGWqZ/qHH9TNIwZnAfMFAC+5W2HJKXsoBjiV8TlsWt7qGHZvHG2 VhxubqDvH6od07CnmkZ6mh1TFdik3OXGPGYXR3tSQZdhuaCTmFucbgk1EFtzwQDh6YDeqYn4LBP /9NpvsdbYug5uqPH5T6oyE2M5Hs9OiVw+a2IyE5kIb99TUZW63MhjcsvJLm+mOP9l3UJxHEcHFs WDsQ1yXnJ6EfEIPCzdtYY0ctQz5GozK6T3chMY4OSOR47O6jLQg/XF0qtCNt5sm7qiP9pFmrFuU uWPUbuvQIq2OiySRRZmXFE19Z9UKN0QdlLxw58X3VmmK0hOt5jTE5hUhVG9esg== X-Google-Smtp-Source: AGHT+IEyrDe8Cdf9g26TH4wCWMETQ0+XWu9RhDQiEyiZTUrrFG1lMyYXyT2WhVrPka++rXuTJgqTBw== X-Received: by 2002:a17:90b:3e8e:b0:30e:54be:37cc with SMTP id 98e67ed59e1d1-30e7d5a93a3mr21688031a91.23.1747745569700; Tue, 20 May 2025 05:52:49 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 06/33] tcg/wasm32: Add add/sub/mul instructions Date: Tue, 20 May 2025 21:51:08 +0900 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::52c; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pg1-x52c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747745708347116600 Content-Type: text/plain; charset="utf-8" Add, sub and mul operations are implemented using the corresponding instructions in Wasm. Signed-off-by: Kohei Tokunaga --- tcg/wasm32/tcg-target.c.inc | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tcg/wasm32/tcg-target.c.inc b/tcg/wasm32/tcg-target.c.inc index e3a35c8415..e4204d3956 100644 --- a/tcg/wasm32/tcg-target.c.inc +++ b/tcg/wasm32/tcg-target.c.inc @@ -195,6 +195,18 @@ static void tcg_wasm_out_op_i64_xor(TCGContext *s) { tcg_wasm_out8(s, 0x85); } +static void tcg_wasm_out_op_i64_add(TCGContext *s) +{ + tcg_wasm_out8(s, 0x7c); +} +static void tcg_wasm_out_op_i64_sub(TCGContext *s) +{ + tcg_wasm_out8(s, 0x7d); +} +static void tcg_wasm_out_op_i64_mul(TCGContext *s) +{ + tcg_wasm_out8(s, 0x7e); +} static void tcg_wasm_out_op_var(TCGContext *s, uint8_t instr, uint8_t i) { tcg_wasm_out8(s, instr); @@ -229,6 +241,9 @@ static void tcg_wasm_out_op_global_set_r(TCGContext *s,= TCGReg r0) tcg_wasm_out_i64_calc(and); tcg_wasm_out_i64_calc(or); tcg_wasm_out_i64_calc(xor); +tcg_wasm_out_i64_calc(add); +tcg_wasm_out_i64_calc(sub); +tcg_wasm_out_i64_calc(mul); =20 static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, intptr_t value, intptr_t addend) @@ -632,6 +647,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_i64_calc_add(s, a0, a1, a2); } =20 static const TCGOutOpBinary outop_add =3D { @@ -810,6 +826,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_i64_calc_mul(s, a0, a1, a2); } =20 static const TCGOutOpBinary outop_mul =3D { @@ -1000,6 +1017,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_i64_calc_sub(s, a0, a1, a2); } =20 static const TCGOutOpSubtract outop_sub =3D { --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747745644; cv=none; d=zohomail.com; s=zohoarc; b=crM2hqJSq6mDkAKol8sciVKCA1l73yergJ6U3DFm0hAbAxsd0O+BsiMyY8q9n8LtB1q/ojVZD6K30mVQgVKCtyJsCZkl1MoNpC4i3UjSQJcxtGmBuPryT+mTlUwC0sPQ3Bz4jYOkHsCCUuVODxlgB/82cXhTsXEWj0GUOAMJoMo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747745644; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Bns4HZO2Nb4MGLYLQJB6/ZEKS2CSFb3ypYim5fOiTl8=; b=Q7rse69F1XqSogIju23q3O9PhKbsK0wdgFtSzBtdzXv3+cFp/El8dv6oNyajLeWBoIwZVnRl8HXVHSthVm3ztzH2CA+kiBBdFHdNsf6bQmVnK7mcLpU55Cf7FM3Wa7QKDm7k40Z4WXcdvgyMxYjNRX4/CZENQY6LkRwMWpBzlbs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747745644096720.9896653410099; Tue, 20 May 2025 05:54:04 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMSw-0000rv-BH; Tue, 20 May 2025 08:53:07 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMSu-0000rI-9a; Tue, 20 May 2025 08:53:04 -0400 Received: from mail-pj1-x102c.google.com ([2607:f8b0:4864:20::102c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMSp-0000ln-Kq; Tue, 20 May 2025 08:53:04 -0400 Received: by mail-pj1-x102c.google.com with SMTP id 98e67ed59e1d1-30572effb26so5232744a91.0; Tue, 20 May 2025 05:52:56 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.52.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:52:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745575; x=1748350375; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Bns4HZO2Nb4MGLYLQJB6/ZEKS2CSFb3ypYim5fOiTl8=; b=ToMGXGQUo7ViGnuieqa5JEDvfIfhbpXOf/PdQGbgypCyFHEeVG2zc7yvm6OxvIimKD 6DUDbUrHXovfZxYNeRZmWdBdylx1isq747DRHFLwcgEj4gEzT40H2cb4Vjzqh83XpyEn gcydvQV6nNaQ+kwduPZDhSriR3hf16atl0RCYYADHjlNso4cKVaFfZoi+UVd0PmTL1gl SQIF4sHoN9tw9XxTuWL9S+uNoU7RFgDV6ae6LP9Mk9gBnxSZiDU3irgWWtuHGHCjVCtJ uGp2cWdl+Ca9eKZ10hF5989EjMX8zGsTX65YM+bznTQLYKVkoRt4nz7/kTcf7ITBAnoS LMeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745575; x=1748350375; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Bns4HZO2Nb4MGLYLQJB6/ZEKS2CSFb3ypYim5fOiTl8=; b=opE/+IVOllyCN5evqs3aKHb7VyLFhah00LyxZ2gyOqW08S2z3fb0QpsrfFseFb8GhG 8Sn2nM9mz0mepOT2CPSNz8k7D85DQ2D4iI1b9Y8NOwqjx361WM1jJpl2iVcPj1d24tqg W/ewaXbGXtu1hdUxadvATZMQbGFBxw7eqKvpVWxywUFWnYo9HAYkriF5OKoQ9k6Qnh+R V3MLGc+/ZtJ15FxJOjUzmcLVGRCVUIhgPSl24XDM0UZba3oinNtlXrza7aX8sMG+ci4w uFGa+BZGdbWCLbjlTURQkv+nbDM3dFfbLxeZqDriqlisre5ioZOYDzDrhigylszEoWFa SLLw== X-Forwarded-Encrypted: i=1; AJvYcCW7TISJ/Xuwr4EJ78TcsahpyT52tSpCsYtw8uoPmAAmoveDBCvPSS/Nu1j39yt91dirneG8H+SjWG3Utw==@nongnu.org, AJvYcCXhiZUrmjcPc08tUWnWmbAILlxSHWIpOwmNtT6ThoHy9G3EMTTlWhJZSZWlfM2Sb6nTem/ZmhpZUg==@nongnu.org X-Gm-Message-State: AOJu0YxCHLHwbTXw6odBm47aNKZHqdbiBSRA5eGxSPS84exn1UwDh2Zq 6QczKDaRs0zz/XuFKXv2nCXDmvrm1aeL7M+IAnm/Ybx1z/bfhTXQfG4DvUg+JJ0+ X-Gm-Gg: ASbGncug4xssH/1gfPV91/guAR8m/+snvjmyyBQAMf/xemF5EO6Iws3MHAI8x+IlRQ8 hVXPzwWGxOnx8MZdlXCe3EGghZp1Cmk7ktUCPh2EpnIyK5+/Dx+562t6hEVhHWt/g8pmFEmJcT9 JcI7eesdQf4yAH9ejSJJiqmnjZTkzi1vNJw8Gntlg8olMLC/ujApxWgfcWCP+pEctEDa7ZM+AIb JI78JUd0YXJQdn18E4u5NrW5XOjFrf/Uol0zA5XGRwVFm9zD8k5AtjPQW8GAthFxdCpLjS4B+vU GjqnAgO4ExMPRuJF/1NLIim7dxiIJsTddgSG7HSnzxMCBuU8JoufnYz+rLPV7Q== X-Google-Smtp-Source: AGHT+IGlDhFteL3xifkGxWzP+tChzeTDjC8nEVDX/xHHzS8M7A6BHOyu+hh3zK9skEJFg3yaRl/GPA== X-Received: by 2002:a17:90b:33c2:b0:30e:6a9d:d787 with SMTP id 98e67ed59e1d1-30e7d50b04bmr28998390a91.11.1747745575202; Tue, 20 May 2025 05:52:55 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 07/33] tcg/wasm32: Add shl/shr/sar instructions Date: Tue, 20 May 2025 21:51:09 +0900 Message-ID: <1451b4a4c7547f4077709557f07f3002cfc3d889.1747744132.git.ktokunaga.mail@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::102c; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pj1-x102c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747745646141116600 Content-Type: text/plain; charset="utf-8" This commit implements shl, shr and sar operations using Wasm instructions. The Wasm backend uses 64bit variables so the right shift operation for 32bit values needs to extract the lower 32bit of the operand before shifting. Additionally, since constant values must be encoded in LEB128 format, this commit introduces an encoder function implemented following [1]. [1] https://en.wikipedia.org/wiki/LEB128 Signed-off-by: Kohei Tokunaga --- tcg/wasm32/tcg-target.c.inc | 107 ++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) diff --git a/tcg/wasm32/tcg-target.c.inc b/tcg/wasm32/tcg-target.c.inc index e4204d3956..b9f2c9c195 100644 --- a/tcg/wasm32/tcg-target.c.inc +++ b/tcg/wasm32/tcg-target.c.inc @@ -207,6 +207,26 @@ static void tcg_wasm_out_op_i64_mul(TCGContext *s) { tcg_wasm_out8(s, 0x7e); } +static void tcg_wasm_out_op_i64_shl(TCGContext *s) +{ + tcg_wasm_out8(s, 0x86); +} +static void tcg_wasm_out_op_i64_shr_s(TCGContext *s) +{ + tcg_wasm_out8(s, 0x87); +} +static void tcg_wasm_out_op_i64_shr_u(TCGContext *s) +{ + tcg_wasm_out8(s, 0x88); +} +static void tcg_wasm_out_op_i32_wrap_i64(TCGContext *s) +{ + tcg_wasm_out8(s, 0xa7); +} +static void tcg_wasm_out_op_i64_extend_i32_s(TCGContext *s) +{ + tcg_wasm_out8(s, 0xac); +} static void tcg_wasm_out_op_var(TCGContext *s, uint8_t instr, uint8_t i) { tcg_wasm_out8(s, instr); @@ -245,6 +265,88 @@ tcg_wasm_out_i64_calc(add); tcg_wasm_out_i64_calc(sub); tcg_wasm_out_i64_calc(mul); =20 +static void tcg_wasm_out_leb128_sint64_t(TCGContext *s, int64_t v) +{ + bool more =3D true; + uint8_t b; + while (more) { + b =3D v & 0x7f; + v >>=3D 7; + if (((v =3D=3D 0) && ((b & 0x40) =3D=3D 0)) || + ((v =3D=3D -1) && ((b & 0x40) !=3D 0))) { + more =3D false; + } else { + b |=3D 0x80; + } + tcg_wasm_out8(s, b); + } +} + +static void tcg_wasm_out_op_i64_const(TCGContext *s, int64_t v) +{ + tcg_wasm_out8(s, 0x42); + tcg_wasm_out_leb128_sint64_t(s, v); +} + +static void tcg_wasm_out_shl(TCGContext *s, TCGReg ret, + TCGReg arg1, TCGReg arg2) +{ + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_global_get_r(s, arg2); + tcg_wasm_out_op_i64_shl(s); + tcg_wasm_out_op_global_set_r(s, ret); +} + +static void tcg_wasm_out_shr_u( + TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + switch (type) { + case TCG_TYPE_I32: + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_i64_const(s, 0xffffffff); + tcg_wasm_out_op_i64_and(s); + tcg_wasm_out_op_global_get_r(s, arg2); + tcg_wasm_out_op_i64_const(s, 0x7f); + tcg_wasm_out_op_i64_and(s); + tcg_wasm_out_op_i64_shr_u(s); + tcg_wasm_out_op_global_set_r(s, ret); + break; + case TCG_TYPE_I64: + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_global_get_r(s, arg2); + tcg_wasm_out_op_i64_shr_u(s); + tcg_wasm_out_op_global_set_r(s, ret); + break; + default: + g_assert_not_reached(); + } +} + +static void tcg_wasm_out_shr_s( + TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + switch (type) { + case TCG_TYPE_I32: + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_i32_wrap_i64(s); + tcg_wasm_out_op_i64_extend_i32_s(s); + tcg_wasm_out_op_global_get_r(s, arg2); + tcg_wasm_out_op_i64_const(s, 0x7f); + tcg_wasm_out_op_i64_and(s); + tcg_wasm_out_op_i64_shr_s(s); + tcg_wasm_out_op_global_set_r(s, ret); + break; + case TCG_TYPE_I64: + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_global_get_r(s, arg2); + tcg_wasm_out_op_i64_shr_s(s); + tcg_wasm_out_op_global_set_r(s, ret); + break; + default: + g_assert_not_reached(); + } +} + static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, intptr_t value, intptr_t addend) { @@ -975,11 +1077,13 @@ static const TCGOutOpBinary outop_rotr =3D { static void tgen_sar(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { + TCGReg orig_a1 =3D a1; if (type < TCG_TYPE_REG) { tcg_out_ext32s(s, TCG_REG_TMP, a1); a1 =3D TCG_REG_TMP; } tcg_out_op_rrr(s, INDEX_op_sar, a0, a1, a2); + tcg_wasm_out_shr_s(s, type, a0, orig_a1, a2); } =20 static const TCGOutOpBinary outop_sar =3D { @@ -991,6 +1095,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_shl(s, a0, a1, a2); } =20 static const TCGOutOpBinary outop_shl =3D { @@ -1001,11 +1106,13 @@ static const TCGOutOpBinary outop_shl =3D { static void tgen_shr(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { + TCGReg orig_a1 =3D a1; if (type < TCG_TYPE_REG) { tcg_out_ext32u(s, TCG_REG_TMP, a1); a1 =3D TCG_REG_TMP; } tcg_out_op_rrr(s, INDEX_op_shr, a0, a1, a2); + tcg_wasm_out_shr_u(s, type, a0, orig_a1, a2); } =20 static const TCGOutOpBinary outop_shr =3D { --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747745693; cv=none; d=zohomail.com; s=zohoarc; b=bA1WHY89zTVkoM7nuLp3kfY2Ip28p/PLeUL9dMP2BWqQhMs1DXkkK9dmlO5KZUQhBBIAQgkF5QmLJY7j+dokstC/Vv0VI/rvgaIJD0ongwfYwiJ4GZ7as5sOfIaqG3yTTigoSSCMl0RPWDZtbb+i6gru9vl1i/Eu32wbmBgntK8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747745693; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=CN+y9XhejQ1H6My7no7RtPqzXKOX9HgjCuOWMfl1Tw8=; b=fBorp8DykOKyJ3M8lCoGsdYEunOuTX6pcPvqOQUI4hHGhL9MP7GRz/crcDivV38ur+oa7E5RLg6VVKirqfr+SXWUwA9B8oINm0fIMIWJsqZ+sppSfADqxUmvP3MNplx0d0Q7uj/5B0dUwsKgmicqK+OK2HOzjz3VJjn5ePm66eQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747745693213632.1157758571975; Tue, 20 May 2025 05:54:53 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMT0-0000tX-Sz; Tue, 20 May 2025 08:53:10 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMSw-0000rq-Ty; Tue, 20 May 2025 08:53:07 -0400 Received: from mail-pj1-x102f.google.com ([2607:f8b0:4864:20::102f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMSt-0000mW-IV; Tue, 20 May 2025 08:53:05 -0400 Received: by mail-pj1-x102f.google.com with SMTP id 98e67ed59e1d1-30eb1c68386so2425242a91.2; Tue, 20 May 2025 05:53:02 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.52.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:53:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745581; x=1748350381; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=CN+y9XhejQ1H6My7no7RtPqzXKOX9HgjCuOWMfl1Tw8=; b=WEv+PzUkDdtguGtsk1Gv1bjqXEfc4gPVptBS/PiGNDWZisSOaRORcD6QYiphteNCVq ohTkzdhafyKuRAg45w4ltajvLkYcnys3TgPwXVv2KitDi8+KMt7meAilPgGTxLsHfLFJ th3K62fW9xfph4DGRomZmrxDh3zZ3RWOe30HSnHj0I1JyERYfcDwwqEhyiR9NLeqPn6A L8GQJ1lj5uI684INjkk/ayNgRV3oEU3qBo98dNoxqBXZbBXikou18USpZrD96z7R7KPi tPgHlwtB6yMdDnHvCnb7zzauGmk1VLpB4AI4MVMrEYAL8Fw2ekWe92ftxjPZN/oVQaKS 0OsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745581; x=1748350381; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=CN+y9XhejQ1H6My7no7RtPqzXKOX9HgjCuOWMfl1Tw8=; b=CTwSGZ7ErK1/CUFf2eqtnypBFzO7KqUHUgQNDuyXy4Mftjdk8+v4M2ihWF8vqNHRA7 xpZb+kI3FnoSENSc+B7BZEL/4ckO46MW0XpWai4uuOLP0dHRwmsUa/OvgYRizBrJJvPc 6Q+duVeJpeyXRpmZKp17l0x48Ja914C1blmCrKX9wGtTdQd62+ZuyVreYSr3eNoN/4Ai 9skvc/GRVCgpo0siRrG/im/aySevvbCmkXls7JY1/N0lsD/Jah9K2TTogWYEFeNG6tvj Im/ScBhKDWc7S1BbXB5JJhSG4GBrn/KbU2DSXWfRa6i7g0atEaAsVB4NYGtJFZml/6SH ASBw== X-Forwarded-Encrypted: i=1; AJvYcCVCkCAUliC0WWwZyTCNxyFGR4vGrc7pfCfxQUyKnNrCWPcugGPTQXr9QlLRmw7AfroWLu7O+WjIAw==@nongnu.org, AJvYcCXRkmNKhctT+pOiuTgnxgB7yYv8csdXLqEbIimCNP1LDJq0VjfSHKIUtuW+h3/mgzQhVCmtd/310jbptg==@nongnu.org X-Gm-Message-State: AOJu0YzrxHb4GAd79Bp9MuaWu23GIzALjpsPuBH9c4zUdgfRCS6UZcRM l6v4m87C7gm5FSd6sFeT4bN/fdu45+KUvfT2Ia1UNhuOEApbxwmNTqY+eb6wZI8p X-Gm-Gg: ASbGncsqwxQSLckwofN7a7p2cn9z8O/3nDEADn54E36VP4KyZyNhjckAA8U6yUFipqf 6ETEYrGBiTy7H4M4zK4JvbKZNx80/flA5z8PS++hAsXETx9gruT1Tl+yKuG2WfiimWCw4gWY619 skrcLQ7IgKGSPpHcz5NptmDd0HulS7mYfvCtn89Ye00slHneAKMOZO8aLVYWGnLc6nWIyZJd+NE ieUqf0mopCvFuzWiYZXi//Wr3MGHCRGYZrPfFhVLA2AsGqg+LvdYmmwDAzl3DlH7VhDn4bbdnQf vIyFTCooVqRer9vQLQI5EAzUHxWlYGQYVupxFVzUHNvGVu2tuNNWSGGIXANgfQ== X-Google-Smtp-Source: AGHT+IHs9HPY+pCVl4LDFEjCRpmaV5LycVby3DD8NvGSExZ1Rp4gMLYFjDp9u1xumNLIdKrLoH5AZA== X-Received: by 2002:a17:90a:d604:b0:30a:9316:75a2 with SMTP id 98e67ed59e1d1-30e7d52adcfmr27622113a91.10.1747745580944; Tue, 20 May 2025 05:53:00 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 08/33] tcg/wasm32: Add setcond/negsetcond/movcond instructions Date: Tue, 20 May 2025 21:51:10 +0900 Message-ID: <9381168a9d8ea609cba8461a242320c374e2487f.1747744132.git.ktokunaga.mail@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::102f; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pj1-x102f.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747745694529116600 Content-Type: text/plain; charset="utf-8" This commit implements setcond and movcond operations using Wasm's if/else instructions. Support for TCG_COND_TSTEQ and TCG_COND_TSTNE is not yet implemented, so TCG_TARGET_HAS_tst is set to 0. Signed-off-by: Kohei Tokunaga --- tcg/wasm32/tcg-target-has.h | 2 +- tcg/wasm32/tcg-target.c.inc | 136 ++++++++++++++++++++++++++++++++++-- 2 files changed, 133 insertions(+), 5 deletions(-) diff --git a/tcg/wasm32/tcg-target-has.h b/tcg/wasm32/tcg-target-has.h index ab07ce1fcb..1eaa8f65f6 100644 --- a/tcg/wasm32/tcg-target-has.h +++ b/tcg/wasm32/tcg-target-has.h @@ -13,7 +13,7 @@ =20 #define TCG_TARGET_HAS_qemu_ldst_i128 0 =20 -#define TCG_TARGET_HAS_tst 1 +#define TCG_TARGET_HAS_tst 0 =20 #define TCG_TARGET_extract_valid(type, ofs, len) 1 #define TCG_TARGET_sextract_valid(type, ofs, len) 1 diff --git a/tcg/wasm32/tcg-target.c.inc b/tcg/wasm32/tcg-target.c.inc index b9f2c9c195..b1ed16ad7a 100644 --- a/tcg/wasm32/tcg-target.c.inc +++ b/tcg/wasm32/tcg-target.c.inc @@ -227,6 +227,23 @@ static void tcg_wasm_out_op_i64_extend_i32_s(TCGContex= t *s) { tcg_wasm_out8(s, 0xac); } +static void tcg_wasm_out_op_i64_extend_i32_u(TCGContext *s) +{ + tcg_wasm_out8(s, 0xad); +} +static void tcg_wasm_out_op_if_ret_i64(TCGContext *s) +{ + tcg_wasm_out8(s, 0x04); + tcg_wasm_out8(s, 0x7e); +} +static void tcg_wasm_out_op_else(TCGContext *s) +{ + tcg_wasm_out8(s, 0x05); +} +static void tcg_wasm_out_op_end(TCGContext *s) +{ + tcg_wasm_out8(s, 0x0b); +} static void tcg_wasm_out_op_var(TCGContext *s, uint8_t instr, uint8_t i) { tcg_wasm_out8(s, instr); @@ -265,6 +282,42 @@ tcg_wasm_out_i64_calc(add); tcg_wasm_out_i64_calc(sub); tcg_wasm_out_i64_calc(mul); =20 +static const struct { + uint8_t i32; + uint8_t i64; +} tcg_cond_to_inst[] =3D { + [TCG_COND_EQ] =3D { 0x46 /* i32.eq */ , 0x51 /* i64.eq */}, + [TCG_COND_NE] =3D { 0x47 /* i32.ne */ , 0x52 /* i64.ne */}, + [TCG_COND_LT] =3D { 0x48 /* i32.lt_s */ , 0x53 /* i64.lt_s */}, + [TCG_COND_GE] =3D { 0x4e /* i32.ge_s */ , 0x59 /* i64.ge_s */}, + [TCG_COND_LE] =3D { 0x4c /* i32.le_s */ , 0x57 /* i64.le_s */}, + [TCG_COND_GT] =3D { 0x4a /* i32.gt_s */ , 0x55 /* i64.gt_s */}, + [TCG_COND_LTU] =3D { 0x49 /* i32.lt_u */ , 0x54 /* i64.lt_u */}, + [TCG_COND_GEU] =3D { 0x4f /* i32.ge_u */ , 0x5a /* i64.ge_u */}, + [TCG_COND_LEU] =3D { 0x4d /* i32.le_u */ , 0x58 /* i64.le_u */}, + [TCG_COND_GTU] =3D { 0x4b /* i32.gt_u */ , 0x56 /* i64.gt_u */} +}; + +static void tcg_wasm_out_op_cond_i64( + TCGContext *s, TCGCond cond, TCGReg arg1, TCGReg arg2) +{ + uint8_t op =3D tcg_cond_to_inst[cond].i64; + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_global_get_r(s, arg2); + tcg_wasm_out8(s, op); +} + +static void tcg_wasm_out_op_cond_i32( + TCGContext *s, TCGCond cond, TCGReg arg1, TCGReg arg2) +{ + uint8_t op =3D tcg_cond_to_inst[cond].i32; + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_i32_wrap_i64(s); + tcg_wasm_out_op_global_get_r(s, arg2); + tcg_wasm_out_op_i32_wrap_i64(s); + tcg_wasm_out8(s, op); +} + static void tcg_wasm_out_leb128_sint64_t(TCGContext *s, int64_t v) { bool more =3D true; @@ -288,6 +341,12 @@ static void tcg_wasm_out_op_i64_const(TCGContext *s, i= nt64_t v) tcg_wasm_out_leb128_sint64_t(s, v); } =20 +static void tcg_wasm_out_op_not(TCGContext *s) +{ + tcg_wasm_out_op_i64_const(s, -1); + tcg_wasm_out_op_i64_xor(s); +} + static void tcg_wasm_out_shl(TCGContext *s, TCGReg ret, TCGReg arg1, TCGReg arg2) { @@ -347,6 +406,66 @@ static void tcg_wasm_out_shr_s( } } =20 +static void tcg_wasm_out_setcond(TCGContext *s, TCGType type, TCGReg ret, + TCGReg arg1, TCGReg arg2, TCGCond cond) +{ + switch (type) { + case TCG_TYPE_I32: + tcg_wasm_out_op_cond_i32(s, cond, arg1, arg2); + break; + case TCG_TYPE_I64: + tcg_wasm_out_op_cond_i64(s, cond, arg1, arg2); + break; + default: + g_assert_not_reached(); + } + tcg_wasm_out_op_i64_extend_i32_u(s); + tcg_wasm_out_op_global_set_r(s, ret); +} + +static void tcg_wasm_out_negsetcond(TCGContext *s, TCGType type, TCGReg re= t, + TCGReg arg1, TCGReg arg2, TCGCond cond) +{ + switch (type) { + case TCG_TYPE_I32: + tcg_wasm_out_op_cond_i32(s, cond, arg1, arg2); + break; + case TCG_TYPE_I64: + tcg_wasm_out_op_cond_i64(s, cond, arg1, arg2); + break; + default: + g_assert_not_reached(); + } + tcg_wasm_out_op_i64_extend_i32_u(s); + tcg_wasm_out_op_not(s); + tcg_wasm_out_op_i64_const(s, 1); + tcg_wasm_out_op_i64_add(s); + tcg_wasm_out_op_global_set_r(s, ret); +} + +static void tcg_wasm_out_movcond(TCGContext *s, TCGType type, TCGReg ret, + TCGReg c1, TCGReg c2, + TCGReg v1, TCGReg v2, + TCGCond cond) +{ + switch (type) { + case TCG_TYPE_I32: + tcg_wasm_out_op_cond_i32(s, cond, c1, c2); + break; + case TCG_TYPE_I64: + tcg_wasm_out_op_cond_i64(s, cond, c1, c2); + break; + default: + g_assert_not_reached(); + } + tcg_wasm_out_op_if_ret_i64(s); + tcg_wasm_out_op_global_get_r(s, v1); + tcg_wasm_out_op_else(s); + tcg_wasm_out_op_global_get_r(s, v2); + tcg_wasm_out_op_end(s); + tcg_wasm_out_op_global_set_r(s, ret); +} + static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, intptr_t value, intptr_t addend) { @@ -1261,8 +1380,8 @@ static const TCGOutOpUnary outop_not =3D { .out_rr =3D tgen_not, }; =20 -static void tgen_setcond(TCGContext *s, TCGType type, TCGCond cond, - TCGReg dest, TCGReg arg1, TCGReg arg2) +static void tgen_setcond_tci(TCGContext *s, TCGType type, TCGCond cond, + TCGReg dest, TCGReg arg1, TCGReg arg2) { TCGOpcode opc =3D (type =3D=3D TCG_TYPE_I32 ? INDEX_op_tci_setcond32 @@ -1270,6 +1389,13 @@ static void tgen_setcond(TCGContext *s, TCGType type= , TCGCond cond, tcg_out_op_rrrc(s, opc, dest, arg1, arg2, cond); } =20 +static void tgen_setcond(TCGContext *s, TCGType type, TCGCond cond, + TCGReg dest, TCGReg arg1, TCGReg arg2) +{ + tgen_setcond_tci(s, type, cond, dest, arg1, arg2); + tcg_wasm_out_setcond(s, type, dest, arg1, arg2, cond); +} + static const TCGOutOpSetcond outop_setcond =3D { .base.static_constraint =3D C_O1_I2(r, r, r), .out_rrr =3D tgen_setcond, @@ -1278,8 +1404,9 @@ static const TCGOutOpSetcond outop_setcond =3D { static void tgen_negsetcond(TCGContext *s, TCGType type, TCGCond cond, TCGReg dest, TCGReg arg1, TCGReg arg2) { - tgen_setcond(s, type, cond, dest, arg1, arg2); + tgen_setcond_tci(s, type, cond, dest, arg1, arg2); tgen_neg(s, type, dest, dest); + tcg_wasm_out_negsetcond(s, type, dest, arg1, arg2, cond); } =20 static const TCGOutOpSetcond outop_negsetcond =3D { @@ -1290,7 +1417,7 @@ static const TCGOutOpSetcond outop_negsetcond =3D { static void tgen_brcond(TCGContext *s, TCGType type, TCGCond cond, TCGReg arg0, TCGReg arg1, TCGLabel *l) { - tgen_setcond(s, type, cond, TCG_REG_TMP, arg0, arg1); + tgen_setcond_tci(s, type, cond, TCG_REG_TMP, arg0, arg1); tcg_out_op_rl(s, INDEX_op_brcond, TCG_REG_TMP, l); } =20 @@ -1307,6 +1434,7 @@ static void tgen_movcond(TCGContext *s, TCGType type,= TCGCond cond, ? INDEX_op_tci_movcond32 : INDEX_op_movcond); tcg_out_op_rrrrrc(s, opc, ret, c1, c2, vt, vf, cond); + tcg_wasm_out_movcond(s, type, ret, c1, c2, vt, vf, cond); } =20 static const TCGOutOpMovcond outop_movcond =3D { --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747745648; cv=none; d=zohomail.com; s=zohoarc; b=cJ06mj/vDi5TaO248v4hk6thQTEOnWD3VANO3a8HdSJ5Ggvl6ERxiwDsYx4Yw6WKqiWlcn2IIpRw407HH+7LFXhSj8SmMOztL7HJgpEn7XXFQF02t/uWbSsYOrZPgKerNHGsvyobEqFxmNvlH/qy6ATDfgTkf+u0QTrFyYn5aA4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747745648; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=GOF0U/wy86P4v/Jfdj/dCcLnbz7PDbEdix7I3iArBFY=; b=hqHuww+JW0my0uHp5YfpMeThJUddN6MMH744y0ZIA+2k/qOhVcl0/9RejS3ZR1PVachmnLdu8MTEQThYZngELtxify+sjB4AWz5Jfl565lKri21AEOxwaaIwe9DH8miO1CKRzbVYD+aALeHJtQ63sj5ej4s82xXKJdgiulM5FdQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747745648648122.37887258031003; Tue, 20 May 2025 05:54:08 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMT5-0000vT-I7; Tue, 20 May 2025 08:53:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMT3-0000v9-Q1; Tue, 20 May 2025 08:53:13 -0400 Received: from mail-pg1-x532.google.com ([2607:f8b0:4864:20::532]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMT0-0000nT-JT; Tue, 20 May 2025 08:53:13 -0400 Received: by mail-pg1-x532.google.com with SMTP id 41be03b00d2f7-b07d607dc83so4408541a12.1; Tue, 20 May 2025 05:53:08 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.53.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:53:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745587; x=1748350387; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=GOF0U/wy86P4v/Jfdj/dCcLnbz7PDbEdix7I3iArBFY=; b=OqlzmJ/1Z6j2qq/wkatKi56wkp73813eiYEcfGvf9DL1r8mlQaXuEh+uOhYZsbGGji wvOP7Lm2RJGulHsxU9xCNxoNb9f7NF2oxGZcoXtmM0qd3dVD3CQBwCNJZ/HSMpHp6R10 Lr9tXx+38cpDKgkqHbta4XkvmUG5cchcxTcvql1iXrfunJiUROSD6YBahh0Up/1DcB5/ aycFdA/yMc52KMSo3SOOPLGOXScaGz7Kf9ijbXcAj3ySv2nBssAO8Tkpl+X7v65PpGjm 4a5ciVZfydfDXBgv17PRH3WRc4KD53jg7OkTzcr/g8T/WGA9kc1WIv/YRtbOGVCQ3qFo 3LNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745587; x=1748350387; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GOF0U/wy86P4v/Jfdj/dCcLnbz7PDbEdix7I3iArBFY=; b=Rf8IzjahmQHByEGidYN68ZM/vbB8V9CtolgPhdRhtuxBysMonLdC8Nu3RTnRGiigpa 76kSZldsRBq6pSJtGbla5ygdPj7KmhGWuZLPy6AAizd0sJe1XDM12EVsUzSeeTH2OQb2 Uc6DPFGE1VHT4Nbx++No5f5Y+uKx+Ye737FySJSS5jAecMOWBBrHOKpGm8HH4/q52YpI NFMd2yrgsfQisVvfo5c5JaorTPDNifyGPD/o0/7WnUVeGPcdsdlUbE2YWwI3eco8jrmH nc4TatIm6V36JwxZIbElD+F6V3Q0bcZ6Jac8A7yud3CpWAbTBDKma2pVMWjYgp/8aHPS Epug== X-Forwarded-Encrypted: i=1; AJvYcCUmyfOR7oFw38tN11y2VovewXhjDtv5W6NPdkOG7hSgF9MCXXo41NMPgvoCiL0WCZy68Z/HGDH63yOSXw==@nongnu.org, AJvYcCVcQ/oTazgJ/tFbEGGP8enuOKkopYZRDXw8bb5CEg0qD89jmEzpGY0zluRQyoEIPvd9V8BMFP1e7Q==@nongnu.org X-Gm-Message-State: AOJu0Yyt6Ejl9gvVdkZBqNtovJ0lwsQf1vRC2BH8G0dGOLgFAoKa23pE KE6xMQ07WAeOLILz/y7hMAlhhfyJKq0ygYuxMjzbQPXwLNP/sI1knX+ISc5bJSzq X-Gm-Gg: ASbGncty52Wsv9l4jw1dUbEWsu8k9Y1Is7xVaFMs5UhNhy0c7TU/Jg+gw9VY137N60P Y2CTIUV2F2THg658Gy22PM7eJYAUng49awp6zgpfvnAWsxTQ8dRPAgCWLbxjmUjRDacDSaY9zLg VQVWLl/oBIAnO7TCcSQRbj5jzAewQjOUx1mSlxVn+iyIOaoKhfD6bp0zaZWqRuwhHIjlfkFDNmb hqlbmrT4SyL3UO3fQCMm1Mp59NIXKZUjE6mXO+RLqw7LXyH72eLdaUMmVivvU1y6a4/XXPiTzBz vOWkdCC4+t3HVDf56y8XkoyXlK+f33W5gYnX6z5xga6x4Qq7mAE= X-Google-Smtp-Source: AGHT+IF2IdPC31h39sXeDasbK/cNnL9bKcljFz/XsOKGHNbEKDiHJj1SNgrvTkhpjoGGDiOzqccXsQ== X-Received: by 2002:a17:90b:570d:b0:309:fe2b:306f with SMTP id 98e67ed59e1d1-30e7d5a8b14mr21480941a91.26.1747745586726; Tue, 20 May 2025 05:53:06 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 09/33] tcg/wasm32: Add deposit/sextract/extract instrcutions Date: Tue, 20 May 2025 21:51:11 +0900 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::532; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pg1-x532.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747745649910116600 Content-Type: text/plain; charset="utf-8" This implements deposit, sextract and extract operations. The tcg_out_[s]extract functions are used by several other functions (e.g. tcg_out_ext*) and are intended to emit TCI code. So they have been renamed to tcg_tci_out_[s]extract. Signed-off-by: Kohei Tokunaga --- tcg/wasm32/tcg-target.c.inc | 122 ++++++++++++++++++++++++++++++++---- 1 file changed, 109 insertions(+), 13 deletions(-) diff --git a/tcg/wasm32/tcg-target.c.inc b/tcg/wasm32/tcg-target.c.inc index b1ed16ad7a..d732d2f3c0 100644 --- a/tcg/wasm32/tcg-target.c.inc +++ b/tcg/wasm32/tcg-target.c.inc @@ -231,6 +231,20 @@ static void tcg_wasm_out_op_i64_extend_i32_u(TCGContex= t *s) { tcg_wasm_out8(s, 0xad); } + +static void tcg_wasm_out_op_i32_and(TCGContext *s) +{ + tcg_wasm_out8(s, 0x71); +} +static void tcg_wasm_out_op_i32_or(TCGContext *s) +{ + tcg_wasm_out8(s, 0x72); +} +static void tcg_wasm_out_op_i32_shl(TCGContext *s) +{ + tcg_wasm_out8(s, 0x74); +} + static void tcg_wasm_out_op_if_ret_i64(TCGContext *s) { tcg_wasm_out8(s, 0x04); @@ -318,6 +332,23 @@ static void tcg_wasm_out_op_cond_i32( tcg_wasm_out8(s, op); } =20 +static void tcg_wasm_out_leb128_sint32_t(TCGContext *s, int32_t v) +{ + bool more =3D true; + uint8_t b; + while (more) { + b =3D v & 0x7f; + v >>=3D 7; + if (((v =3D=3D 0) && ((b & 0x40) =3D=3D 0)) || + ((v =3D=3D -1) && ((b & 0x40) !=3D 0))) { + more =3D false; + } else { + b |=3D 0x80; + } + tcg_wasm_out8(s, b); + } +} + static void tcg_wasm_out_leb128_sint64_t(TCGContext *s, int64_t v) { bool more =3D true; @@ -335,6 +366,12 @@ static void tcg_wasm_out_leb128_sint64_t(TCGContext *s= , int64_t v) } } =20 +static void tcg_wasm_out_op_i32_const(TCGContext *s, int32_t v) +{ + tcg_wasm_out8(s, 0x41); + tcg_wasm_out_leb128_sint32_t(s, v); +} + static void tcg_wasm_out_op_i64_const(TCGContext *s, int64_t v) { tcg_wasm_out8(s, 0x42); @@ -466,6 +503,50 @@ static void tcg_wasm_out_movcond(TCGContext *s, TCGTyp= e type, TCGReg ret, tcg_wasm_out_op_global_set_r(s, ret); } =20 +static void tcg_wasm_out_deposit(TCGContext *s, + TCGReg dest, TCGReg arg1, TCGReg arg2, + int pos, int len) +{ + int64_t mask =3D (((int64_t)1 << len) - 1) << pos; + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_i64_const(s, ~mask); + tcg_wasm_out_op_i64_and(s); + tcg_wasm_out_op_global_get_r(s, arg2); + tcg_wasm_out_op_i64_const(s, pos); + tcg_wasm_out_op_i64_shl(s); + tcg_wasm_out_op_i64_const(s, mask); + tcg_wasm_out_op_i64_and(s); + tcg_wasm_out_op_i64_or(s); + tcg_wasm_out_op_global_set_r(s, dest); +} + +static void tcg_wasm_out_extract(TCGContext *s, TCGReg dest, TCGReg arg1, + int pos, int len) +{ + int64_t mask =3D ~0ULL >> (64 - len); + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_i64_const(s, pos); + tcg_wasm_out_op_i64_shr_u(s); + tcg_wasm_out_op_i64_const(s, mask); + tcg_wasm_out_op_i64_and(s); + tcg_wasm_out_op_global_set_r(s, dest); +} + +static void tcg_wasm_out_sextract(TCGContext *s, TCGReg dest, TCGReg arg1, + int pos, int len) +{ + int rs =3D 64 - len; + int sl =3D rs - pos; + tcg_wasm_out_op_global_get_r(s, arg1); + if (sl > 0) { + tcg_wasm_out_op_i64_const(s, sl); + tcg_wasm_out_op_i64_shl(s); + } + tcg_wasm_out_op_i64_const(s, rs); + tcg_wasm_out_op_i64_shr_s(s); + tcg_wasm_out_op_global_set_r(s, dest); +} + static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, intptr_t value, intptr_t addend) { @@ -677,6 +758,12 @@ static void tcg_out_op_rrrrrc(TCGContext *s, TCGOpcode= op, tcg_out32(s, insn); } =20 +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) { @@ -737,7 +824,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); } =20 static const TCGOutOpExtract outop_extract =3D { @@ -745,10 +833,17 @@ static const TCGOutOpExtract outop_extract =3D { .out_rr =3D tcg_out_extract, }; =20 +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); } =20 static const TCGOutOpExtract outop_sextract =3D { @@ -762,34 +857,34 @@ static const TCGOutOpExtract2 outop_extract2 =3D { =20 static void tcg_out_ext8s(TCGContext *s, TCGType type, TCGReg rd, TCGReg r= s) { - tcg_out_sextract(s, type, rd, rs, 0, 8); + tcg_tci_out_sextract(s, type, rd, rs, 0, 8); } =20 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); } =20 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); } =20 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); } =20 static void tcg_out_ext32s(TCGContext *s, TCGReg rd, TCGReg rs) { tcg_debug_assert(TCG_TARGET_REG_BITS =3D=3D 64); - tcg_out_sextract(s, TCG_TYPE_I64, rd, rs, 0, 32); + tcg_tci_out_sextract(s, TCG_TYPE_I64, rd, rs, 0, 32); } =20 static void tcg_out_ext32u(TCGContext *s, TCGReg rd, TCGReg rs) { tcg_debug_assert(TCG_TARGET_REG_BITS =3D=3D 64); - tcg_out_extract(s, TCG_TYPE_I64, rd, rs, 0, 32); + tcg_tci_out_extract(s, TCG_TYPE_I64, rd, rs, 0, 32); } =20 static void tcg_out_exts_i32_i64(TCGContext *s, TCGReg rd, TCGReg rs) @@ -977,6 +1072,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); } =20 static const TCGOutOpDeposit outop_deposit =3D { @@ -1034,7 +1130,7 @@ static const TCGOutOpBinary outop_eqv =3D { #if TCG_TARGET_REG_BITS =3D=3D 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); } =20 static const TCGOutOpUnary outop_extrh_i64_i32 =3D { @@ -1198,7 +1294,7 @@ static void tgen_sar(TCGContext *s, TCGType type, { TCGReg orig_a1 =3D 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 =3D TCG_REG_TMP; } tcg_out_op_rrr(s, INDEX_op_sar, a0, a1, a2); @@ -1227,7 +1323,7 @@ static void tgen_shr(TCGContext *s, TCGType type, { TCGReg orig_a1 =3D 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 =3D TCG_REG_TMP; } tcg_out_op_rrr(s, INDEX_op_shr, a0, a1, a2); @@ -1325,7 +1421,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); } } =20 @@ -1339,7 +1435,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); } } =20 --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747745757; cv=none; d=zohomail.com; s=zohoarc; b=lY+QMwsJcQGldt2Nw79I5pFVpuEPAPdydg2RtIxOFgat0Q+aoy2ZXAdRjU5qEDRIUNgwRyjS4ILflmaAK03M9Pzv9zwM7bWbzfFRppCX4jpRQ5LCIp74Da9XiEXum2fzGPgBiOMFeCXmLuG8kwuvDETmWtM8OVdxXhGoBahEw9Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747745757; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=rd5mjjrQF9ATa0gHuVt6j9q1L70Kmu641JXtCbaxJks=; b=HoTePMWkghPENARmaFEgwrzo8tYvrXKHPJ+wketTGx+7sa/m8CQHj0SS0tCWX4e85gC5MZD10Dn+XUnCBgj6EoUk9lNAt0ZrJSZr+BJSKVHBtFEZm1HXGvLic9fzC66iQjbib1swwSavrjx5Rcm5uWrCLgvddvVsBIVWSxc9D9Q= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747745757781337.8294091037735; Tue, 20 May 2025 05:55:57 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMTR-00017L-Ac; Tue, 20 May 2025 08:53:39 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMT8-0000wF-ME; Tue, 20 May 2025 08:53:23 -0400 Received: from mail-pf1-x434.google.com ([2607:f8b0:4864:20::434]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMT5-0000o6-IS; Tue, 20 May 2025 08:53:18 -0400 Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-7424ccbef4eso5534037b3a.2; Tue, 20 May 2025 05:53:13 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.53.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:53:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745592; x=1748350392; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=rd5mjjrQF9ATa0gHuVt6j9q1L70Kmu641JXtCbaxJks=; b=VTi/mBScmuU8R6Od4yLF959BDd5iLT2/HNAK6TIG5LHdd9AiBVUke48MBdbhT2SEGO /vDEULP6YFWDBGTUuW63CHdtwM/1Uj42wLZzTlq6rDl47U0KPJF1gamWCfrgFIqD2u77 JbbT3ORDJeCAhYs3cA5ZSej4P5/2YOPLyqvGtuoNH3TUVv2DhnwU7h4oqo4S3CV/VqAr llXw4f7PvLD0Sb5zRgC0vQalhof1vHadszXeQ5QBxIYdgvIFx0Ykr8WBbNgEPloZ7F2Y FA+7is2STVFPlhX+Ohf+uRzCL/RcjUB3HAUw6SM6PkTJWKN2iIQeuclbENyaiIdDH99C c5Vw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745592; x=1748350392; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rd5mjjrQF9ATa0gHuVt6j9q1L70Kmu641JXtCbaxJks=; b=Y0W+RDU6KG4iBok9uf2p5yftBcAyF9Q0K+3E2qPwKdbaVzYIUzR1CTMlh3Ne1w8nK7 5WoycHt1ykrN43BMg9CR3zR9nkSFAw+hNDPRRWAkC194jDEB/orHFuJ3YgKVeb2K34Zb uaHTQV2lPFtgY0EQDDFothIUbuH7uo9+axgRMZ7j+Sd7OTajl9zwU61WxM+NwUpZHuJQ csbOBm1z1jYM36myC+XKf2ciy3RUnhohKsPWoJ+OpUZTIpe5h5eXvsrA2NuAoa2FjCby VdukOs5zPe3gld8JMF2nLq5VUPDAy4ioFazPXqnGpXclErAQ52v5HhMNNa7KKcd7V3Jd JJCw== X-Forwarded-Encrypted: i=1; AJvYcCWSGRu9b7KwPC6FUxP5kYq5R/mpi24YFY9Fk+t+GHgmVc4hbgzZsNsY0Sthcc2KxklaU3TGj6ECKYutQw==@nongnu.org, AJvYcCWVQiYFVDvsx0PEq320KLauqwF/1GXPn6bgBetCXJPT+EkAvrJg3RBxlpFEcng3hoNJK95Z7iCDUw==@nongnu.org X-Gm-Message-State: AOJu0YxJ4G3CoYWVj1QU6vIAre88qv8V7trxIK6MKbHx/H7tlWS4M1TS tZpzf/TqMWCevHW+hHVzpGgBDKH5WW0CT9JPo6UmaxqXcvaL1PDw4+QNb93IOqta X-Gm-Gg: ASbGncvn87Yv6FA8x1QOVdUi5eR+glq1MzQkG3oPNmSO551YM/05f76NCzY7ZaCaKD4 A6LuVtIaANm54mGDuc/8Tf5NiFv++0q4NNDHthawZwG0ujFducQgt2nsmmQIzh3yGMlcGbQc+Tl siGbknvvuoYi059+3hTZ5Jw4S3ttsnQxabOLkh9DHr92L4NUGrbwSjXvzquBsbKZ+sM1SCiXfyV Vz+SHYpkG7CpWZE5yprEVi3sHaeaj8CO5PKjKshoQPPyO/g4DBRB3GUc4uoftbtNRbeA/zqhMCP IQI+fz+XhHtO29zO7qvKBILvAAaA9QhTqn94SAXpvxxIAOLfVoI= X-Google-Smtp-Source: AGHT+IF2FW3kjy4TuydJk2c6oRxPAshlC6eugRri80AQsOEYEwiyFGuQRsLOkSsx4VXlAPW/OPcUHw== X-Received: by 2002:a17:90b:3c8f:b0:30c:540b:9ba with SMTP id 98e67ed59e1d1-30e7d51fa7cmr25863329a91.10.1747745592411; Tue, 20 May 2025 05:53:12 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 10/33] tcg/wasm32: Add load and store instructions Date: Tue, 20 May 2025 21:51:12 +0900 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::434; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pf1-x434.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747745759299116600 Content-Type: text/plain; charset="utf-8" This commit implements load and store operations using Wasm memory instructions. Since Wasm's load/store instructions don't support negative offset, address calculations are performed separately before the memory access. Signed-off-by: Kohei Tokunaga --- tcg/wasm32/tcg-target.c.inc | 242 ++++++++++++++++++++++++++++++++++++ 1 file changed, 242 insertions(+) diff --git a/tcg/wasm32/tcg-target.c.inc b/tcg/wasm32/tcg-target.c.inc index d732d2f3c0..9b024b03b9 100644 --- a/tcg/wasm32/tcg-target.c.inc +++ b/tcg/wasm32/tcg-target.c.inc @@ -232,6 +232,10 @@ static void tcg_wasm_out_op_i64_extend_i32_u(TCGContex= t *s) tcg_wasm_out8(s, 0xad); } =20 +static void tcg_wasm_out_op_i32_add(TCGContext *s) +{ + tcg_wasm_out8(s, 0x6a); +} static void tcg_wasm_out_op_i32_and(TCGContext *s) { tcg_wasm_out8(s, 0x71); @@ -279,6 +283,11 @@ static void tcg_wasm_out_op_global_set_r(TCGContext *s= , TCGReg r0) { tcg_wasm_out_op_global_set(s, tcg_target_reg_index[r0]); } +static void tcg_wasm_out_op_global_get_r_i32(TCGContext *s, TCGReg r0) +{ + tcg_wasm_out_op_global_get(s, tcg_target_reg_index[r0]); + tcg_wasm_out_op_i32_wrap_i64(s); +} =20 #define tcg_wasm_out_i64_calc(op) \ static void tcg_wasm_out_i64_calc_##op( \ @@ -366,6 +375,19 @@ static void tcg_wasm_out_leb128_sint64_t(TCGContext *s= , int64_t v) } } =20 +static void tcg_wasm_out_leb128_uint32_t(TCGContext *s, uint32_t v) +{ + uint8_t b; + do { + b =3D v & 0x7f; + v >>=3D 7; + if (v !=3D 0) { + b |=3D 0x80; + } + tcg_wasm_out8(s, b); + } while (v !=3D 0); +} + static void tcg_wasm_out_op_i32_const(TCGContext *s, int32_t v) { tcg_wasm_out8(s, 0x41); @@ -378,6 +400,68 @@ static void tcg_wasm_out_op_i64_const(TCGContext *s, i= nt64_t v) tcg_wasm_out_leb128_sint64_t(s, v); } =20 +static void tcg_wasm_out_op_loadstore( + TCGContext *s, uint8_t instr, uint32_t a, uint32_t o) +{ + tcg_wasm_out8(s, instr); + tcg_wasm_out_leb128_uint32_t(s, a); + tcg_wasm_out_leb128_uint32_t(s, o); +} + +static void tcg_wasm_out_op_i64_store(TCGContext *s, uint32_t a, uint32_t = o) +{ + tcg_wasm_out_op_loadstore(s, 0x37, a, o); +} +static void tcg_wasm_out_op_i64_store8(TCGContext *s, uint32_t a, uint32_t= o) +{ + tcg_wasm_out_op_loadstore(s, 0x3c, a, o); +} + +static void tcg_wasm_out_op_i64_store16(TCGContext *s, uint32_t a, uint32_= t o) +{ + tcg_wasm_out_op_loadstore(s, 0x3d, a, o); +} + +static void tcg_wasm_out_op_i64_store32(TCGContext *s, uint32_t a, uint32_= t o) +{ + tcg_wasm_out_op_loadstore(s, 0x3e, a, o); +} + +static void tcg_wasm_out_op_i64_load(TCGContext *s, uint32_t a, uint32_t o) +{ + tcg_wasm_out_op_loadstore(s, 0x29, a, o); +} + + static void tcg_wasm_out_op_i64_load8_s(TCGContext *s, uint32_t a, uint32= _t o) +{ + tcg_wasm_out_op_loadstore(s, 0x30, a, o); +} + +static void tcg_wasm_out_op_i64_load8_u(TCGContext *s, uint32_t a, uint32_= t o) +{ + tcg_wasm_out_op_loadstore(s, 0x31, a, o); +} + +static void tcg_wasm_out_op_i64_load16_s(TCGContext *s, uint32_t a, uint32= _t o) +{ + tcg_wasm_out_op_loadstore(s, 0x32, a, o); +} + +static void tcg_wasm_out_op_i64_load16_u(TCGContext *s, uint32_t a, uint32= _t o) +{ + tcg_wasm_out_op_loadstore(s, 0x33, a, o); +} + +static void tcg_wasm_out_op_i64_load32_s(TCGContext *s, uint32_t a, uint32= _t o) +{ + tcg_wasm_out_op_loadstore(s, 0x34, a, o); +} + +static void tcg_wasm_out_op_i64_load32_u(TCGContext *s, uint32_t a, uint32= _t o) +{ + tcg_wasm_out_op_loadstore(s, 0x35, a, o); +} + static void tcg_wasm_out_op_not(TCGContext *s) { tcg_wasm_out_op_i64_const(s, -1); @@ -547,6 +631,154 @@ static void tcg_wasm_out_sextract(TCGContext *s, TCGR= eg dest, TCGReg arg1, tcg_wasm_out_op_global_set_r(s, dest); } =20 +static void tcg_wasm_out_ld( + TCGContext *s, TCGType type, TCGReg val, TCGReg base, intptr_t offset) +{ + tcg_wasm_out_op_global_get_r_i32(s, base); + if (offset < 0) { + tcg_wasm_out_op_i32_const(s, offset); + tcg_wasm_out_op_i32_add(s); + offset =3D 0; + } + switch (type) { + case TCG_TYPE_I32: + tcg_wasm_out_op_i64_load32_u(s, 0, offset); + break; + case TCG_TYPE_I64: + tcg_wasm_out_op_i64_load(s, 0, offset); + break; + default: + g_assert_not_reached(); + } + tcg_wasm_out_op_global_set_r(s, val); +} + +static void tcg_wasm_out_ld8s(TCGContext *s, TCGReg val, + TCGReg base, intptr_t offset) +{ + tcg_wasm_out_op_global_get_r_i32(s, base); + if (offset < 0) { + tcg_wasm_out_op_i32_const(s, offset); + tcg_wasm_out_op_i32_add(s); + offset =3D 0; + } + tcg_wasm_out_op_i64_load8_s(s, 0, offset); + tcg_wasm_out_op_global_set_r(s, val); +} + +static void tcg_wasm_out_ld8u(TCGContext *s, TCGReg val, + TCGReg base, intptr_t offset) +{ + tcg_wasm_out_op_global_get_r_i32(s, base); + if (offset < 0) { + tcg_wasm_out_op_i32_const(s, offset); + tcg_wasm_out_op_i32_add(s); + offset =3D 0; + } + tcg_wasm_out_op_i64_load8_u(s, 0, offset); + tcg_wasm_out_op_global_set_r(s, val); +} + +static void tcg_wasm_out_ld16s(TCGContext *s, TCGReg val, + TCGReg base, intptr_t offset) +{ + tcg_wasm_out_op_global_get_r_i32(s, base); + if (offset < 0) { + tcg_wasm_out_op_i32_const(s, offset); + tcg_wasm_out_op_i32_add(s); + offset =3D 0; + } + tcg_wasm_out_op_i64_load16_s(s, 0, offset); + tcg_wasm_out_op_global_set_r(s, val); +} + +static void tcg_wasm_out_ld16u(TCGContext *s, TCGReg val, + TCGReg base, intptr_t offset) +{ + tcg_wasm_out_op_global_get_r_i32(s, base); + if (offset < 0) { + tcg_wasm_out_op_i32_const(s, offset); + tcg_wasm_out_op_i32_add(s); + offset =3D 0; + } + tcg_wasm_out_op_i64_load16_u(s, 0, offset); + tcg_wasm_out_op_global_set_r(s, val); +} + +static void tcg_wasm_out_ld32s(TCGContext *s, TCGReg val, + TCGReg base, intptr_t offset) +{ + tcg_wasm_out_op_global_get_r_i32(s, base); + if (offset < 0) { + tcg_wasm_out_op_i32_const(s, offset); + tcg_wasm_out_op_i32_add(s); + offset =3D 0; + } + tcg_wasm_out_op_i64_load32_s(s, 0, offset); + tcg_wasm_out_op_global_set_r(s, val); +} + +static void tcg_wasm_out_ld32u(TCGContext *s, TCGReg val, + TCGReg base, intptr_t offset) +{ + tcg_wasm_out_op_global_get_r_i32(s, base); + if (offset < 0) { + tcg_wasm_out_op_i32_const(s, offset); + tcg_wasm_out_op_i32_add(s); + offset =3D 0; + } + tcg_wasm_out_op_i64_load32_u(s, 0, offset); + tcg_wasm_out_op_global_set_r(s, val); +} + +static void tcg_wasm_out_st(TCGContext *s, TCGType type, TCGReg val, + TCGReg base, intptr_t offset) +{ + tcg_wasm_out_op_global_get_r_i32(s, base); + if (offset < 0) { + tcg_wasm_out_op_i32_const(s, offset); + tcg_wasm_out_op_i32_add(s); + offset =3D 0; + } + tcg_wasm_out_op_global_get_r(s, val); + switch (type) { + case TCG_TYPE_I32: + tcg_wasm_out_op_i64_store32(s, 0, offset); + break; + case TCG_TYPE_I64: + tcg_wasm_out_op_i64_store(s, 0, offset); + break; + default: + g_assert_not_reached(); + } +} + +static void tcg_wasm_out_st8(TCGContext *s, TCGReg val, + TCGReg base, intptr_t offset) +{ + tcg_wasm_out_op_global_get_r_i32(s, base); + if (offset < 0) { + tcg_wasm_out_op_i32_const(s, offset); + tcg_wasm_out_op_i32_add(s); + offset =3D 0; + } + tcg_wasm_out_op_global_get_r(s, val); + tcg_wasm_out_op_i64_store8(s, 0, offset); +} + +static void tcg_wasm_out_st16(TCGContext *s, TCGReg val, + TCGReg base, intptr_t offset) +{ + tcg_wasm_out_op_global_get_r_i32(s, base); + if (offset < 0) { + tcg_wasm_out_op_i32_const(s, offset); + tcg_wasm_out_op_i32_add(s); + offset =3D 0; + } + tcg_wasm_out_op_global_get_r(s, val); + tcg_wasm_out_op_i64_store16(s, 0, offset); +} + static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, intptr_t value, intptr_t addend) { @@ -786,6 +1018,7 @@ static void tcg_out_ld(TCGContext *s, TCGType type, TC= GReg val, TCGReg base, op =3D INDEX_op_ld32u; } tcg_out_ldst(s, op, val, base, offset); + tcg_wasm_out_ld(s, type, val, base, offset); } =20 static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg ar= g) @@ -1585,6 +1818,7 @@ static void tgen_ld8u(TCGContext *s, TCGType type, TC= GReg dest, TCGReg base, ptrdiff_t offset) { tcg_out_ldst(s, INDEX_op_ld8u, dest, base, offset); + tcg_wasm_out_ld8u(s, dest, base, offset); } =20 static const TCGOutOpLoad outop_ld8u =3D { @@ -1596,6 +1830,7 @@ static void tgen_ld8s(TCGContext *s, TCGType type, TC= GReg dest, TCGReg base, ptrdiff_t offset) { tcg_out_ldst(s, INDEX_op_ld8s, dest, base, offset); + tcg_wasm_out_ld8s(s, dest, base, offset); } =20 static const TCGOutOpLoad outop_ld8s =3D { @@ -1607,6 +1842,7 @@ static void tgen_ld16u(TCGContext *s, TCGType type, T= CGReg dest, TCGReg base, ptrdiff_t offset) { tcg_out_ldst(s, INDEX_op_ld16u, dest, base, offset); + tcg_wasm_out_ld16u(s, dest, base, offset); } =20 static const TCGOutOpLoad outop_ld16u =3D { @@ -1618,6 +1854,7 @@ static void tgen_ld16s(TCGContext *s, TCGType type, T= CGReg dest, TCGReg base, ptrdiff_t offset) { tcg_out_ldst(s, INDEX_op_ld16s, dest, base, offset); + tcg_wasm_out_ld16s(s, dest, base, offset); } =20 static const TCGOutOpLoad outop_ld16s =3D { @@ -1630,6 +1867,7 @@ static void tgen_ld32u(TCGContext *s, TCGType type, T= CGReg dest, TCGReg base, ptrdiff_t offset) { tcg_out_ldst(s, INDEX_op_ld32u, dest, base, offset); + tcg_wasm_out_ld32u(s, dest, base, offset); } =20 static const TCGOutOpLoad outop_ld32u =3D { @@ -1641,6 +1879,7 @@ static void tgen_ld32s(TCGContext *s, TCGType type, T= CGReg dest, TCGReg base, ptrdiff_t offset) { tcg_out_ldst(s, INDEX_op_ld32s, dest, base, offset); + tcg_wasm_out_ld32s(s, dest, base, offset); } =20 static const TCGOutOpLoad outop_ld32s =3D { @@ -1653,6 +1892,7 @@ static void tgen_st8(TCGContext *s, TCGType type, TCG= Reg data, TCGReg base, ptrdiff_t offset) { tcg_out_ldst(s, INDEX_op_st8, data, base, offset); + tcg_wasm_out_st8(s, data, base, offset); } =20 static const TCGOutOpStore outop_st8 =3D { @@ -1664,6 +1904,7 @@ static void tgen_st16(TCGContext *s, TCGType type, TC= GReg data, TCGReg base, ptrdiff_t offset) { tcg_out_ldst(s, INDEX_op_st16, data, base, offset); + tcg_wasm_out_st16(s, data, base, offset); } =20 static const TCGOutOpStore outop_st16 =3D { @@ -1735,6 +1976,7 @@ static void tcg_out_st(TCGContext *s, TCGType type, T= CGReg val, TCGReg base, op =3D INDEX_op_st32; } tcg_out_ldst(s, op, val, base, offset); + tcg_wasm_out_st(s, type, val, base, offset); } =20 static inline bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val, --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747745705; cv=none; d=zohomail.com; s=zohoarc; b=iVb5ecK3/if16I+hQ6pV8lFwsdiHZCoBiJtDTBSKdlGHvPG/BZ2oE1WM+umRBHVv+90loFGk8pLfbCGBEJwK2P1sTeR9eY6SRxrZmYK9aZ9ZYZDg/m7yAo3NB1ak3bmv14Vp6KIw9IumnrdFb84WgioRtu6CqXyFOcfz+3vnycI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747745705; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=NlI3ADKsZrHAbqgaoSffPjMOx8QCa6ijCQD0G6eY21w=; b=bttlRzhOhIF4kDilWJrIvVoXOuZItTMWCfX5qsDQmIEy/ZUsZah8BVPoCnlBfEhVdMzNro9UeC/Ykx4J6vzRyxLfKJQr5OtnGOsGBjNiJjNp5mQCOQOSoL8G2MntZ5ywjjsXR5k+TaUPDwb4t9CGaOc8dDdKgEk4mudF89VBi3M= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747745705112645.8712788807692; Tue, 20 May 2025 05:55:05 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMTl-0001La-M6; Tue, 20 May 2025 08:53:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMTH-0000zx-Rh; Tue, 20 May 2025 08:53:33 -0400 Received: from mail-pj1-x1036.google.com ([2607:f8b0:4864:20::1036]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMTE-0000or-LK; Tue, 20 May 2025 08:53:27 -0400 Received: by mail-pj1-x1036.google.com with SMTP id 98e67ed59e1d1-30e8f4dbb72so4422473a91.1; Tue, 20 May 2025 05:53:19 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.53.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:53:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745598; x=1748350398; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=NlI3ADKsZrHAbqgaoSffPjMOx8QCa6ijCQD0G6eY21w=; b=Xqg0FTZdBrTI6mDt8QOa8iDFqurk3Yz++hhM/kQNTuKppo8ZhRL93mx23lHTvRpjJq ih44RmWN35S+MgEzH7/FPQVpblxeZherByFw53aqj8FHtBDq7a+jiR5oTqTA0jseX84H BNi2cTxSvKtpxuuAI8Mdi97oP6rVCbr/41jdD2j08MJ7amZqkyC+a4O7bJct2UwPnE3A 0l/GhnlKRrjcDB2Gm6G3N9vxeZn0rT7zuYNqfnOFjJfavUiGAI7Ssg2vkRDm51IpBMbw alvBTAbShp4ZMofb3d38Oib6xMHzFTVtskOr1xEQAedh+4omANqLw0M33Ac/6oWklvn+ fWaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745598; x=1748350398; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NlI3ADKsZrHAbqgaoSffPjMOx8QCa6ijCQD0G6eY21w=; b=XLh+wJit/bM09Bf15F4M49m/Tv+F3W8YioUEhqYMAnMiZBdiWuul3rFYgE0qqqUawR GAABWZV/Ad2XXOTT1cyPEmxonMHpyQmPfBymXoEnsQ1aFJheDDNsd+dpaz5tZj83X+/4 wLvV8dT913dqT9a0pTrMLpbzHKeIbE0DDQsH/tW2V3ZnuyZIuJ6niuCNQxxI4sRDeX+C yNU6TOUS5N6zExDLzhebvDHlz4M21e/PNkKmpHhFrdSb64Rhfy0rnClKfkY06Fw5zWVE BaXE0SmDG6Q79vsdmusBOVo0I2DGioOMf0P/RyvfdRVYgvS/Hg2FieKrDCz3G/I9kJn/ byiQ== X-Forwarded-Encrypted: i=1; AJvYcCUbzWej7dKdsRro3MFCHekzH7kCZ8FHNSBu/PyZnswA9uuHb2kfCnzkPq4jpGGX6HAzkp9kIW5rdg==@nongnu.org, AJvYcCWYmEJXUzXxM2wb9LIAsEClykpMxAqCoLY+OXGGDT7bJ9ZC4RC6FpoWFZVzkRBOTIEU+YrcnUwd3jFGww==@nongnu.org X-Gm-Message-State: AOJu0Yw+znrONYMEwNOZBRvZhfN3agtfkd5TAtO9IcRjQpzG9MT80Kh8 DCjViRuxnUNXujhW6P//E0xRfbdswjczIJYFPmF6V7bqq9QIzBDw8yzFnu3fONy8 X-Gm-Gg: ASbGnct11BqkNUer29wCDSeR07YgHpdGPqkJgtiYgcNqDZoexBbXo8K0em+o0VQ3NYy BHVyLOfICVVFgc3x5s5RyMd0hJHACUrotrPjXj/d2Vsmz7eosoCGX5kUTvgW0MaVOUQ4x7flIUT /iK2U4p1syDhz1EDvsO/r5nOUzJyswyRBdoc1zUBiHC0zw9JDsmL6pfh5MZKQzV0RF5vq30vI0o 7mYhDZRs2cTMDK8AO9usheRfKko+ngZyh+1WZ5ZRDRhbAYM9idMGcesHpOpCDZ9VKr4MJg/fvak W6thxdsa72Q8FpAlE2+PERLGlI7WUvKear8VER5CJxGvwvGWlu6QPb2Bm7ztKg== X-Google-Smtp-Source: AGHT+IHCRPMEIPAOztUBdUPNL63cIBCPrltzc/+WoGIQ2txQn80tKWzqSX2p8oCJQMC86V6TS7yRxg== X-Received: by 2002:a17:90b:5603:b0:2ee:9b2c:3253 with SMTP id 98e67ed59e1d1-30e8323cd14mr22649462a91.30.1747745597888; Tue, 20 May 2025 05:53:17 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 11/33] tcg/wasm32: Add mov/movi instructions Date: Tue, 20 May 2025 21:51:13 +0900 Message-ID: <6e482a0b2d32d06f7877f1f310a0e7a81e85f88f.1747744132.git.ktokunaga.mail@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1036; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pj1-x1036.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747745706455116600 Content-Type: text/plain; charset="utf-8" This commit implements mov/movi instructions. The tcg_out_mov[i] functions are used by several other functions and are intended to emit TCI code. So they have been renamed to tcg_tci_out_mov[i]. Signed-off-by: Kohei Tokunaga --- tcg/wasm32/tcg-target.c.inc | 85 ++++++++++++++++++++++++++----------- 1 file changed, 60 insertions(+), 25 deletions(-) diff --git a/tcg/wasm32/tcg-target.c.inc b/tcg/wasm32/tcg-target.c.inc index 9b024b03b9..90a5705442 100644 --- a/tcg/wasm32/tcg-target.c.inc +++ b/tcg/wasm32/tcg-target.c.inc @@ -779,6 +779,28 @@ static void tcg_wasm_out_st16(TCGContext *s, TCGReg va= l, tcg_wasm_out_op_i64_store16(s, 0, offset); } =20 +static void tcg_wasm_out_mov(TCGContext *s, TCGReg ret, TCGReg arg) +{ + tcg_wasm_out_op_global_get_r(s, arg); + tcg_wasm_out_op_global_set_r(s, 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_i64_const(s, (int32_t)arg); + break; + case TCG_TYPE_I64: + tcg_wasm_out_op_i64_const(s, arg); + break; + default: + g_assert_not_reached(); + } + tcg_wasm_out_op_global_set_r(s, ret); +} + static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, intptr_t value, intptr_t addend) { @@ -990,6 +1012,33 @@ static void tcg_out_op_rrrrrc(TCGContext *s, TCGOpcod= e op, tcg_out32(s, insn); } =20 +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 =3D=3D 64 + arg =3D (int32_t)arg; + /* fall through */ + case TCG_TYPE_I64: +#endif + break; + default: + g_assert_not_reached(); + } + + if (arg =3D=3D sextract32(arg, 0, 20)) { + tcg_out_op_ri(s, INDEX_op_tci_movi, ret, arg); + } else { + tcg_insn_unit_tci insn =3D 0; + + new_pool_label(s, arg, 20, s->code_ptr, 0); + insn =3D deposit32(insn, 0, 8, INDEX_op_tci_movl); + insn =3D 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) { @@ -1001,7 +1050,7 @@ static void tcg_out_ldst(TCGContext *s, TCGOpcode op,= TCGReg val, { stack_bounds_check(base, offset); if (offset !=3D 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 =3D TCG_REG_TMP; offset =3D 0; @@ -1021,37 +1070,23 @@ static void tcg_out_ld(TCGContext *s, TCGType type,= TCGReg val, TCGReg base, tcg_wasm_out_ld(s, type, val, base, offset); } =20 -static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg ar= g) +static void tcg_tci_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGRe= g arg) { tcg_out_op_rr(s, INDEX_op_mov, ret, arg); +} + +static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg ar= g) +{ + tcg_tci_out_mov(s, type, ret, arg); + tcg_wasm_out_mov(s, ret, arg); return true; } =20 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 =3D=3D 64 - arg =3D (int32_t)arg; - /* fall through */ - case TCG_TYPE_I64: -#endif - break; - default: - g_assert_not_reached(); - } - - if (arg =3D=3D sextract32(arg, 0, 20)) { - tcg_out_op_ri(s, INDEX_op_tci_movi, ret, arg); - } else { - tcg_insn_unit insn =3D 0; - - new_pool_label(s, arg, 20, s->code_ptr, 0); - insn =3D deposit32(insn, 0, 8, INDEX_op_tci_movl); - insn =3D 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); } =20 static void tcg_out_extract(TCGContext *s, TCGType type, TCGReg rd, @@ -1133,7 +1168,7 @@ static void tcg_out_extu_i32_i64(TCGContext *s, TCGRe= g rd, TCGReg rs) static void tcg_out_extrl_i64_i32(TCGContext *s, TCGReg rd, TCGReg rs) { tcg_debug_assert(TCG_TARGET_REG_BITS =3D=3D 64); - tcg_out_mov(s, TCG_TYPE_I32, rd, rs); + tcg_tci_out_mov(s, TCG_TYPE_I32, rd, rs); } =20 static bool tcg_out_xchg(TCGContext *s, TCGType type, TCGReg r1, TCGReg r2) --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747746550; cv=none; d=zohomail.com; s=zohoarc; b=DLtMnVz1McfTpumZIlSKQoaq+E/nmRErJvm0gXVPHbneAjiKiuwRUBWYG1ZzELzZc68prwfwQtpv9uqxrsv7QYMVwmYfdTXF44siAF1/HCCHa+vMV3UHa2GiLHGleFRuHM0fA9JDQwRRoNwCA7T+naWCTBOw+3kJPtcjBPi/2lo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747746550; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=DrYdTbrh0HLfQlm+zOWduDlR9Q8yHBtmdY6iR829V3U=; b=KDI1Jl3P9N7xZ3twpFsM1ndZiCdIa7RyP/zuTkpl7z7JsgiPDGGSpsLpxDp++rXJnlw0y/FGlW564B1IFomR0dz5PuzBk0O8zndqP+ewjB1F9lC2ul0p/lpbBLDL7g8hs8UwMxdchvxJKM7SPklKJizsSglNO7BvJkl3TWEdxo0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747746550635607.1203139543425; Tue, 20 May 2025 06:09:10 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMTb-0001Fb-Pu; Tue, 20 May 2025 08:53:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMTN-00013p-NM; Tue, 20 May 2025 08:53:35 -0400 Received: from mail-pf1-x436.google.com ([2607:f8b0:4864:20::436]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMTI-0000pV-3c; Tue, 20 May 2025 08:53:31 -0400 Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-742c5eb7d1cso3484264b3a.3; Tue, 20 May 2025 05:53:24 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.53.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:53:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745603; x=1748350403; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=DrYdTbrh0HLfQlm+zOWduDlR9Q8yHBtmdY6iR829V3U=; b=GWRoDjxrciK3o5xWpiLhtYMT1DOx32/pLIQZEHUjx7+LuNFNEqw1dGpRZFAedlbqzx YA4B7YAQsZZ4Y2M2OsqaUOhWyRk+mN4eUrmcC6JRyZIZwwxxBmWJ7xzDdlaxdqFa4NnQ 6VpNXMm+WTE2oH++tAkeOEVdPoYF3sx3Fd9xt75dtJI4jw7bnjU8FOqvEZX9UtXxlvnO +W2J6LQthmnNJIjSuxuUeYnh3nl1/NyyLy065YRK0eA/sPSiE3H2lS3yN7l6GUA19exT 0R4QgD5KbAuQKrzWlE3Q0fqYKaZW3VA54hHGw8vyCdIkTLdvUKmyiNK/MEN0k4GiEXK+ qUfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745603; x=1748350403; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DrYdTbrh0HLfQlm+zOWduDlR9Q8yHBtmdY6iR829V3U=; b=gCxYJ6STR3NnNG2fwo9CF6I+Ti33+rmYXGKN/qbfT2QcB91iq0kh/XfvLHu8oou0G5 LkmOi6walaZ2xzlLo2v9S6yGZXW59TTu12HnGwXBJ+shQKk6IAvCH2Yfac0tBHEkfJ7h 4dDZDxwzVEKoW800ZDSfiyznrKgXUW8xM8WbeJ8t9TzgDQNuCOuPZ1N38T+UJ6/zbcgc /IBu3Q0utW33SyexgbN5BGOa4guL4zMkqQrXQhveINGMCIYvQln60fK8W68/gfI8XZFV BgA7ZIGBRaq2dAZdlCo5zCEmJDTZqlPwUJnJa+lYw1KQxMnHZiUg7OpABqykR15Pe0CX JvoA== X-Forwarded-Encrypted: i=1; AJvYcCVIlka4tbEuM443LHZnNB9BlqDXOC1KBkZg1NGxlziFaATfwlZmm4W7x47qvxa35GzVFC4uF0xwviTqlA==@nongnu.org, AJvYcCXEo4+YY5ONosEXgXHlaLA9Ug7WbULN281Z1baoE6DsWyh0edbcdDT/sh37kALC2WLQYydyOdIZSw==@nongnu.org X-Gm-Message-State: AOJu0Yzezqz2p8x47rsnaLDXovIeZQl0YoDPIgIm2fnGYkhnsVvM/F9p xk8dAkC9eAwFvoKEFd8wxboeQ2wXjELIbd9n6ThrgOUZ8tdyOukQMfvFXZiHy+OP X-Gm-Gg: ASbGnct66awqvXM9Oo4kMPvdv4V/UlMhb9x6al06Ckts1bXAMMWjf4nmdDUfIvgqsdh ipDJgwaHUK4AXKGkwSN8X8y55h1pUIk7jKKKD7WViHox26ZEjVYWykwptMa3bOYs/LLZDLHr4ki /BWqSYnY29vbfhQy1FEfca/cVpPExd8k3cpidxukUVn1FNFY4fNUU3x8KyFlnO3XwiL5RpQHywf kG9Mlb3Gh/pZbp4R58FLtbRcHtwPet8Z7RFNcIPEj0qB6hC5EaDcqk+l6T6mOZHzWgAMV/2Bj4E XLbxVJcnjkt+2DFvLYVXsoUfh/ItVEmV3FeEL5uCRThFPtIuju0mHK1dE3mTYw== X-Google-Smtp-Source: AGHT+IHSVeSZrqI1p+LKyaXlkp/QemBYcHu0gGDpX+Iuujio4I5h58FLti/L/Smc9nkP49kM+CaNOw== X-Received: by 2002:a17:90b:4a50:b0:301:98fc:9b5a with SMTP id 98e67ed59e1d1-30e7d500953mr24868929a91.6.1747745603425; Tue, 20 May 2025 05:53:23 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 12/33] tcg/wasm32: Add ext instructions Date: Tue, 20 May 2025 21:51:14 +0900 Message-ID: <6c4e1684c2c1207c01ceff9168cf3031707040c6.1747744132.git.ktokunaga.mail@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::436; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pf1-x436.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747746552961116600 Content-Type: text/plain; charset="utf-8" This commit implements the ext operations using Wasm's extend instructions. Signed-off-by: Kohei Tokunaga --- tcg/wasm32/tcg-target.c.inc | 79 ++++++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/tcg/wasm32/tcg-target.c.inc b/tcg/wasm32/tcg-target.c.inc index 90a5705442..dff79a9854 100644 --- a/tcg/wasm32/tcg-target.c.inc +++ b/tcg/wasm32/tcg-target.c.inc @@ -231,7 +231,14 @@ static void tcg_wasm_out_op_i64_extend_i32_u(TCGContex= t *s) { tcg_wasm_out8(s, 0xad); } - +static void tcg_wasm_out_op_i64_extend8_s(TCGContext *s) +{ + tcg_wasm_out8(s, 0xc2); +} +static void tcg_wasm_out_op_i64_extend16_s(TCGContext *s) +{ + tcg_wasm_out8(s, 0xc3); +} static void tcg_wasm_out_op_i32_add(TCGContext *s) { tcg_wasm_out8(s, 0x6a); @@ -631,6 +638,60 @@ static void tcg_wasm_out_sextract(TCGContext *s, TCGRe= g dest, TCGReg arg1, tcg_wasm_out_op_global_set_r(s, dest); } =20 +static void tcg_wasm_out_ext8s(TCGContext *s, TCGReg rd, TCGReg rs) +{ + tcg_wasm_out_op_global_get_r(s, rs); + tcg_wasm_out_op_i64_extend8_s(s); + tcg_wasm_out_op_global_set_r(s, rd); +} + +static void tcg_wasm_out_ext8u(TCGContext *s, TCGReg rd, TCGReg rs) +{ + tcg_wasm_out_op_global_get_r(s, rs); + tcg_wasm_out_op_i64_const(s, 0xff); + tcg_wasm_out_op_i64_and(s); + tcg_wasm_out_op_global_set_r(s, rd); +} + +static void tcg_wasm_out_ext16s(TCGContext *s, TCGReg rd, TCGReg rs) +{ + tcg_wasm_out_op_global_get_r(s, rs); + tcg_wasm_out_op_i64_extend16_s(s); + tcg_wasm_out_op_global_set_r(s, rd); +} + +static void tcg_wasm_out_ext16u(TCGContext *s, TCGReg rd, TCGReg rs) +{ + tcg_wasm_out_op_global_get_r(s, rs); + tcg_wasm_out_op_i64_const(s, 0xffff); + tcg_wasm_out_op_i64_and(s); + tcg_wasm_out_op_global_set_r(s, rd); +} + +static void tcg_wasm_out_ext32s(TCGContext *s, TCGReg rd, TCGReg rs) +{ + tcg_wasm_out_op_global_get_r(s, rs); + tcg_wasm_out_op_i32_wrap_i64(s); + tcg_wasm_out_op_i64_extend_i32_s(s); + tcg_wasm_out_op_global_set_r(s, rd); +} + +static void tcg_wasm_out_ext32u(TCGContext *s, TCGReg rd, TCGReg rs) +{ + tcg_wasm_out_op_global_get_r(s, rs); + tcg_wasm_out_op_i64_const(s, 0xffffffff); + tcg_wasm_out_op_i64_and(s); + tcg_wasm_out_op_global_set_r(s, rd); +} + +static void tcg_wasm_out_extrl_i64_i32(TCGContext *s, TCGReg rd, TCGReg rs) +{ + tcg_wasm_out_op_global_get_r(s, rs); + tcg_wasm_out_op_i64_const(s, 0xffffffff); + tcg_wasm_out_op_i64_and(s); + tcg_wasm_out_op_global_set_r(s, rd); +} + static void tcg_wasm_out_ld( TCGContext *s, TCGType type, TCGReg val, TCGReg base, intptr_t offset) { @@ -1126,33 +1187,39 @@ static const TCGOutOpExtract2 outop_extract2 =3D { static void tcg_out_ext8s(TCGContext *s, TCGType type, TCGReg rd, TCGReg r= s) { tcg_tci_out_sextract(s, type, rd, rs, 0, 8); + tcg_wasm_out_ext8s(s, rd, rs); } =20 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_ext8u(s, rd, rs); } =20 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_ext16s(s, rd, rs); } =20 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_ext16u(s, rd, rs); } =20 static void tcg_out_ext32s(TCGContext *s, TCGReg rd, TCGReg rs) { tcg_debug_assert(TCG_TARGET_REG_BITS =3D=3D 64); tcg_tci_out_sextract(s, TCG_TYPE_I64, rd, rs, 0, 32); + tcg_wasm_out_ext32s(s, rd, rs); } =20 static void tcg_out_ext32u(TCGContext *s, TCGReg rd, TCGReg rs) { tcg_debug_assert(TCG_TARGET_REG_BITS =3D=3D 64); tcg_tci_out_extract(s, TCG_TYPE_I64, rd, rs, 0, 32); + tcg_wasm_out_ext32u(s, rd, rs); } =20 static void tcg_out_exts_i32_i64(TCGContext *s, TCGReg rd, TCGReg rs) @@ -1169,6 +1236,7 @@ static void tcg_out_extrl_i64_i32(TCGContext *s, TCGR= eg rd, TCGReg rs) { tcg_debug_assert(TCG_TARGET_REG_BITS =3D=3D 64); tcg_tci_out_mov(s, TCG_TYPE_I32, rd, rs); + tcg_wasm_out_extrl_i64_i32(s, rd, rs); } =20 static bool tcg_out_xchg(TCGContext *s, TCGType type, TCGReg r1, TCGReg r2) @@ -1396,9 +1464,18 @@ static const TCGOutOpBinary outop_eqv =3D { }; =20 #if TCG_TARGET_REG_BITS =3D=3D 64 +static void tcg_wasm_out_extrh_i64_i32(TCGContext *s, TCGReg a0, TCGReg a1) +{ + tcg_wasm_out_op_global_get_r(s, a1); + tcg_wasm_out_op_i64_const(s, 32); + tcg_wasm_out_op_i64_shr_u(s); + tcg_wasm_out_op_global_set_r(s, a0); +} + 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_extrh_i64_i32(s, a0, a1); } =20 static const TCGOutOpUnary outop_extrh_i64_i32 =3D { --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747745729; cv=none; d=zohomail.com; s=zohoarc; b=JbHo+bbiLtGxWc0h6utwk+wDK69xXXoCLiWVbf0KNkE1pnKDu0Bd7GUvqVCiKnAJ9pwTJTveG3TmvJD6zzo8AHZcs+Y7nPzN6l3FPXjlIo08QIkw1j3RRnBWzCMRpgvC8rgtSaDV9aQuO4HwgRFUT3ia3UabzRQwND/6fLJ2WzE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747745729; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=rhT21ZbeuAH6PwlwaEXlZP3fAXk3Eqmf4zwdAMPd7KE=; b=BVz3fHr89Bat5pX1JgJk4wvPs18V8grR2ME9Ro3eBBEZbIFFVuzjhSRuX7RogjxlCyMFB7Sk1wLQYcLDVB7HAUVH6qIDjxlG863smM+5mk5DIE/FJV106unExBjoc81fxqumUfTJ23jwhnDum3bguGok/uFYuhNQxUI3Xy8NJUk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747745729655766.3844648187479; Tue, 20 May 2025 05:55:29 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMTw-0001dL-TT; Tue, 20 May 2025 08:54:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMTS-00018e-Vn; Tue, 20 May 2025 08:53:40 -0400 Received: from mail-pj1-x1034.google.com ([2607:f8b0:4864:20::1034]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMTO-0000qT-MT; Tue, 20 May 2025 08:53:38 -0400 Received: by mail-pj1-x1034.google.com with SMTP id 98e67ed59e1d1-306bf444ba2so5434630a91.1; Tue, 20 May 2025 05:53:31 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.53.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:53:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745609; x=1748350409; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=rhT21ZbeuAH6PwlwaEXlZP3fAXk3Eqmf4zwdAMPd7KE=; b=ky2Nl//34hQZRKz+H2SOibDuVn6PSeebZPvMRa/RVyIIoncR5O+P6sP+V0hAlXlHK6 Ul98jVm5d4KO3umE3bRmCpMOSPIORA36q0ROcHCPJ6OB8xxMAU8UIYiWOrJ4QAhLoTYJ qb7y3VG5Apf6BnEqMfAWD0ygNGmztVGIGuHx/VlTYfVqtFHRpwl8ZpB+0Y+SAdAz+nl1 BJ45OLUrtUs/3d4xeHbxkfeL/r6PIkkIshUB91hCep3zABl6jY8G7l7N+Xqsh8AjEARD aj1+CmYQjMrsXNOBU5wa6SMmxz2egfsO86z9K4b+pXfdq+9r9rWGvijkNOvghnmK1GJM j+4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745609; x=1748350409; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rhT21ZbeuAH6PwlwaEXlZP3fAXk3Eqmf4zwdAMPd7KE=; b=i7mLgy0cTkCjiyias/rN7oxUyudhW0SDoAr4gqrnr5t8QYCMXKCBbGmOxN87KvdphN L3xX1iFHyWsu8iqGkHJHXNBem6ze3OyFU9YQsvR1RPauBItSYbwFCqZ61SqHqTZb5J1r FpTK59S6+gDtypiI7vliohlpQIihJljfvIFaz6V958xivBcT98b8H44gdLNBlwtveLF4 WWoltPhS6fj97pt7erxprmqXgfG7Z1aVTT0pZGVjFm3RSeNtTaV1uaxPORipdubtHks6 NWutbfOUjWe71hQcOdmIBDJdudO9ZSEdNP8okDbd/J8xDwD8gW+Cl2eU2Yug4WcTORTD yt5g== X-Forwarded-Encrypted: i=1; AJvYcCV5M3L+DqDNiYRMSq3pVc2mYN6HuBrCjCfiIZ4wwNmK/+WuhO69GALTvLEQ80FqWwUMYaHHjk0DIQ==@nongnu.org, AJvYcCVBv5hoZJfFsjzcEDOsK174UBSPJ6ROVyopJjivi3d+eAuMDF6t1ZxpyjW3Yt2NFQ0AT03K0ZPv1A8gHg==@nongnu.org X-Gm-Message-State: AOJu0YzO7N8xwupb7UvvYE/9wgv9R7sqbcG8zMtwo9zWDe5VRQI7TvSk j89PejwA7e3KKnHhMcAzM67Q/p1LICdfZWEipe+/0abLENyk/G6qqdK1xmSGyDQv X-Gm-Gg: ASbGnctmmbplr2ndAZwEZhhenJiy2jx4eyGvUXdClXDsY1y8X2LuFU+25f6YM28PZTQ M0bDH7NQiF81fumkKdXPlM4WIxQw831C2LgAeMSdS4TP+GkE5uGwzRyFazJ4X+iPm9322dAWUgL Zus+iCKRgJRMHRc/dpoEymH+piIELdiI734IDGRkMs4Rj78uNnC0yozxtEhYMG2yjs8yhYUZFhH sX0RGihEFiLEm04pTckQhRqWW+WFoYVIAqrFtNkNg8FbsBWTlsa3cSHQwL5JVsiPlf36QhDmNbu GkvYSvBm4CqwB89mb55MCyzlrf8Ed6nDWzTR6c1KkMUq/A4vchs= X-Google-Smtp-Source: AGHT+IELYgC8rXzpzxpWLznRI/A+e/6gacRu3KFNEtrvOk6g5j42KY2JurSbdVRhmlxiD4imG3/sXg== X-Received: by 2002:a17:90b:35cc:b0:30e:54be:37e7 with SMTP id 98e67ed59e1d1-30e7d5b7521mr27787860a91.24.1747745609377; Tue, 20 May 2025 05:53:29 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 13/33] tcg/wasm32: Add bswap instructions Date: Tue, 20 May 2025 21:51:15 +0900 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1034; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pj1-x1034.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747745730783116600 Content-Type: text/plain; charset="utf-8" This commit implements the bswap operation using Wasm instructions. Signed-off-by: Kohei Tokunaga --- tcg/wasm32/tcg-target.c.inc | 136 ++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) diff --git a/tcg/wasm32/tcg-target.c.inc b/tcg/wasm32/tcg-target.c.inc index dff79a9854..1a6069e288 100644 --- a/tcg/wasm32/tcg-target.c.inc +++ b/tcg/wasm32/tcg-target.c.inc @@ -118,6 +118,11 @@ static const uint8_t tcg_target_reg_index[TCG_TARGET_N= B_REGS] =3D { 15, /* TCG_REG_R15 */ }; =20 +/* Temporary local variables */ +#define TMP32_LOCAL_0_IDX 1 +#define TMP32_LOCAL_1_IDX 2 +#define TMP64_LOCAL_0_IDX 3 + #define BUF_SIZE 1024 typedef struct LinkedBuf { struct LinkedBuf *next; @@ -219,6 +224,10 @@ static void tcg_wasm_out_op_i64_shr_u(TCGContext *s) { tcg_wasm_out8(s, 0x88); } +static void tcg_wasm_out_op_i64_rotr(TCGContext *s) +{ + tcg_wasm_out8(s, 0x8a); +} static void tcg_wasm_out_op_i32_wrap_i64(TCGContext *s) { tcg_wasm_out8(s, 0xa7); @@ -255,6 +264,18 @@ static void tcg_wasm_out_op_i32_shl(TCGContext *s) { tcg_wasm_out8(s, 0x74); } +static void tcg_wasm_out_op_i32_shr_s(TCGContext *s) +{ + tcg_wasm_out8(s, 0x75); +} +static void tcg_wasm_out_op_i32_shr_u(TCGContext *s) +{ + tcg_wasm_out8(s, 0x76); +} +static void tcg_wasm_out_op_i32_rotr(TCGContext *s) +{ + tcg_wasm_out8(s, 0x78); +} =20 static void tcg_wasm_out_op_if_ret_i64(TCGContext *s) { @@ -295,6 +316,14 @@ static void tcg_wasm_out_op_global_get_r_i32(TCGContex= t *s, TCGReg r0) tcg_wasm_out_op_global_get(s, tcg_target_reg_index[r0]); tcg_wasm_out_op_i32_wrap_i64(s); } +static void tcg_wasm_out_op_local_get(TCGContext *s, uint8_t i) +{ + tcg_wasm_out_op_var(s, 0x20, i); +} +static void tcg_wasm_out_op_local_set(TCGContext *s, uint8_t i) +{ + tcg_wasm_out_op_var(s, 0x21, i); +} =20 #define tcg_wasm_out_i64_calc(op) \ static void tcg_wasm_out_i64_calc_##op( \ @@ -692,6 +721,110 @@ static void tcg_wasm_out_extrl_i64_i32(TCGContext *s,= TCGReg rd, TCGReg rs) tcg_wasm_out_op_global_set_r(s, rd); } =20 +static void tcg_wasm_out_bswap64( + TCGContext *s, TCGReg dest, TCGReg src) +{ + tcg_wasm_out_op_global_get_r(s, src); + tcg_wasm_out_op_i64_const(s, 32); + tcg_wasm_out_op_i64_rotr(s); + tcg_wasm_out_op_local_set(s, TMP64_LOCAL_0_IDX); + + tcg_wasm_out_op_local_get(s, TMP64_LOCAL_0_IDX); + tcg_wasm_out_op_i64_const(s, 0xff000000ff000000); + tcg_wasm_out_op_i64_and(s); + tcg_wasm_out_op_i64_const(s, 24); + tcg_wasm_out_op_i64_shr_u(s); + + tcg_wasm_out_op_local_get(s, TMP64_LOCAL_0_IDX); + tcg_wasm_out_op_i64_const(s, 0x00ff000000ff0000); + tcg_wasm_out_op_i64_and(s); + tcg_wasm_out_op_i64_const(s, 8); + tcg_wasm_out_op_i64_shr_u(s); + + tcg_wasm_out_op_i64_or(s); + + tcg_wasm_out_op_local_get(s, TMP64_LOCAL_0_IDX); + tcg_wasm_out_op_i64_const(s, 0x0000ff000000ff00); + tcg_wasm_out_op_i64_and(s); + tcg_wasm_out_op_i64_const(s, 8); + tcg_wasm_out_op_i64_shl(s); + + tcg_wasm_out_op_local_get(s, TMP64_LOCAL_0_IDX); + tcg_wasm_out_op_i64_const(s, 0x000000ff000000ff); + tcg_wasm_out_op_i64_and(s); + tcg_wasm_out_op_i64_const(s, 24); + tcg_wasm_out_op_i64_shl(s); + + tcg_wasm_out_op_i64_or(s); + + tcg_wasm_out_op_i64_or(s); + tcg_wasm_out_op_global_set_r(s, dest); +} + +static void tcg_wasm_out_bswap32( + TCGContext *s, TCGReg dest, TCGReg src, int flags) +{ + tcg_wasm_out_op_global_get_r(s, src); + tcg_wasm_out_op_i32_wrap_i64(s); + tcg_wasm_out_op_local_set(s, TMP32_LOCAL_0_IDX); + + tcg_wasm_out_op_local_get(s, TMP32_LOCAL_0_IDX); + tcg_wasm_out_op_i32_const(s, 16); + tcg_wasm_out_op_i32_rotr(s); + tcg_wasm_out_op_local_set(s, TMP32_LOCAL_0_IDX); + + tcg_wasm_out_op_local_get(s, TMP32_LOCAL_0_IDX); + tcg_wasm_out_op_i32_const(s, 0xff00ff00); + tcg_wasm_out_op_i32_and(s); + tcg_wasm_out_op_i32_const(s, 8); + tcg_wasm_out_op_i32_shr_u(s); + + tcg_wasm_out_op_local_get(s, TMP32_LOCAL_0_IDX); + tcg_wasm_out_op_i32_const(s, 0x00ff00ff); + tcg_wasm_out_op_i32_and(s); + tcg_wasm_out_op_i32_const(s, 8); + tcg_wasm_out_op_i32_shl(s); + + tcg_wasm_out_op_i32_or(s); + if (flags & TCG_BSWAP_OS) { + tcg_wasm_out_op_i64_extend_i32_s(s); + } else { + tcg_wasm_out_op_i64_extend_i32_u(s); + } + tcg_wasm_out_op_global_set_r(s, dest); +} + +static void tcg_wasm_out_bswap16( + TCGContext *s, TCGReg dest, TCGReg src, int flags) +{ + tcg_wasm_out_op_global_get_r(s, src); + tcg_wasm_out_op_i32_wrap_i64(s); + tcg_wasm_out_op_local_set(s, TMP32_LOCAL_0_IDX); + + tcg_wasm_out_op_local_get(s, TMP32_LOCAL_0_IDX); + tcg_wasm_out_op_i32_const(s, 8); + tcg_wasm_out_op_i32_rotr(s); + tcg_wasm_out_op_local_set(s, TMP32_LOCAL_0_IDX); + + tcg_wasm_out_op_local_get(s, TMP32_LOCAL_0_IDX); + tcg_wasm_out_op_i32_const(s, 0x000000ff); + tcg_wasm_out_op_i32_and(s); + + tcg_wasm_out_op_local_get(s, TMP32_LOCAL_0_IDX); + tcg_wasm_out_op_i32_const(s, 0xff000000); + tcg_wasm_out_op_i32_and(s); + tcg_wasm_out_op_i32_const(s, 16); + if (flags & TCG_BSWAP_OS) { + tcg_wasm_out_op_i32_shr_s(s); + } else { + tcg_wasm_out_op_i32_shr_u(s); + } + + tcg_wasm_out_op_i32_or(s); + tcg_wasm_out_op_i64_extend_i32_u(s); + tcg_wasm_out_op_global_set_r(s, dest); +} + static void tcg_wasm_out_ld( TCGContext *s, TCGType type, TCGReg val, TCGReg base, intptr_t offset) { @@ -1768,6 +1901,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); } =20 static const TCGOutOpBswap outop_bswap16 =3D { @@ -1782,6 +1916,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); } =20 static const TCGOutOpBswap outop_bswap32 =3D { @@ -1793,6 +1928,7 @@ static const TCGOutOpBswap outop_bswap32 =3D { 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); } =20 static const TCGOutOpUnary outop_bswap64 =3D { --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747745780; cv=none; d=zohomail.com; s=zohoarc; b=CO749RXm/o3RIn6Wc2Q0VC8mHbWkPXTJWHTM79bnYYJrLxsckxSARLvoQzKrA+r6931Bbm0K5eE+QD4++U2+9nrcawhj0Il00dr40Oqtk9t67O6RHKuFpJNSbqHi1tsrKyZZYfzDGpgkKlSujQ5TWWVm3LQhikW5fYJtRjx+3dk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747745780; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=u2q8jR3NRvJr0acKyFRjELmA4piI07T787KQwfPUSTw=; b=PkwdbdzcRhpvut3R3ytMsrNcFCaA7A3HKWnQ3oyKs/AOhXgolbAbSblM+Z7AeHH279ZlkSDFzEDtZHG3gBk7zJ8P7fOiBKCMxvjwWk6XDJFvubXY2TWE67SaxbMnE9kefIUv5o4Wd+r1u1HgQfZQmDFJ4qXWX8fcyOP652MBDy0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747745780519849.963286865532; Tue, 20 May 2025 05:56:20 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMU0-0001iA-FI; Tue, 20 May 2025 08:54:12 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMTW-0001CB-3i; Tue, 20 May 2025 08:53:42 -0400 Received: from mail-pj1-x1036.google.com ([2607:f8b0:4864:20::1036]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMTT-0000rK-Vr; Tue, 20 May 2025 08:53:41 -0400 Received: by mail-pj1-x1036.google.com with SMTP id 98e67ed59e1d1-30e8a968b15so3515840a91.3; Tue, 20 May 2025 05:53:36 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.53.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:53:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745615; x=1748350415; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=u2q8jR3NRvJr0acKyFRjELmA4piI07T787KQwfPUSTw=; b=X7ANuVPZmKiwXiAzJ/Gh/YDs74dLEFTBO26MalOir7KEy1pqYTQTPH6hyqOosgpVLG JDXTVFIdEb0qCfz04fkFZe9Vvx8HHSCYf3bYJFqCLNZPl2yF+/1t26F4Truc06VWxFhh ewlyrl9YsrA8U4U374Y5SXnHaN3VvBjS+aUZ9uvJlb3J69wt6Xn8Hw22VbyTPn+wGu+N cc75+stSWapEJ5rjelNq1h5PI+RaL0lNSF5rgUeGoxlzMdlJjU+nJ91XaEJ/X9jeGv4d CReUu8l8/OZX6PdF/HgmtNkS2AnOw34PngPGaMFdkgIuphXDOBJCg5M/kbFyXgwsmtC1 SgdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745615; x=1748350415; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=u2q8jR3NRvJr0acKyFRjELmA4piI07T787KQwfPUSTw=; b=vQCOZNu62tPrmfMEjmXQvAT0Tac3W9GXfy/62RafDarU9dVLPejKPSMPcezHQBECia jBxLkJ/kJCZMGNTNi3IJRE7p+dWQmPb132bAy+GsRXY3d3hmOzPZNcUE1ui8/sTbgj2V GHoDnBUeBQZdEnfhKVVJMlCBgYlG+OOKi0PedwdMvM880C/7FM3kO2ZeBKxAr9sAweTZ GEsJ9xmX96WTwNif3/dY1SjsD/vztVtT+t9InOHC882AtdH/CwqnNwqD65IsYZi2rfjd fI8nLsNhVMWm8AWngLDAR+RqO95wGhxxpi5Lso4eQiqQdcZz+W5x9JrISVeuNC4MFLus RHUA== X-Forwarded-Encrypted: i=1; AJvYcCWymA+yx1fw/twS63bVsSVCKkO+RIaeBqBrQZ5P9mGRNsTAi3PTvScYIsvFJq6G6M6/qNbmhkQwKQ==@nongnu.org, AJvYcCXqLJlqS3j1kU0yUJBH9Qxk35CD7qlHg4zsY4OJEZx8Ft3hybOrxi+7YJ+uas0obgfAtcTsC1iUPRNiHQ==@nongnu.org X-Gm-Message-State: AOJu0YypG1vSg+zqaP/oJ5t9fLAS7yiVjNWgnFww8jOEdnkTlkrEm4/o unSa2BbvtyAlMP6NizB4YIu36QtERBiW6Se3uzHyHKVeeSJP2FRCGTJAaTtLrZwa X-Gm-Gg: ASbGncvSf5wpUtnr9mfqu1zoKmg5fLVXOS5301518qyjliosr4+rGveYurR1x89FXwJ E3haPgT1FINFyeooTfFTQuFUJAkaRSUxy7b3wvKxesLeSamMQDNP0M7b2qxFqPteRxEfm9nK2X9 4S6OabkCl2j3zt36jf576DEy+mfAuKbJMoPYJcu7+RQePye3+tp7Bplc0/fcHCDXLyDC4RqKSD4 JAffxwFHlWMloVWdRHoa4hWb/FnAfP0XzXB/cAszxKBa98//7d3a7lNhti8H2cp7aL0HMWVNFWN oGOPr/esnBVVI+VpY1cY6KmDky3d7nFuVj0nqiGU93HMrvfC8dKOC7weB8afyQ== X-Google-Smtp-Source: AGHT+IH4pYR+xBvjcKBjbRkQTBRJT8LDwYpUVuUOiwlfPNjevxQmlAFw2Wowy5/kkLk1V1frDu+5lA== X-Received: by 2002:a17:90a:e70f:b0:2ff:5e4e:864 with SMTP id 98e67ed59e1d1-30e832162d4mr26751801a91.25.1747745615178; Tue, 20 May 2025 05:53:35 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 14/33] tcg/wasm32: Add rem/div instructions Date: Tue, 20 May 2025 21:51:16 +0900 Message-ID: <46ce312e6bfd409ab1ba882470bf4f1508e545e5.1747744132.git.ktokunaga.mail@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1036; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pj1-x1036.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747745781497116600 Content-Type: text/plain; charset="utf-8" This commit implements rem and div operations using Wasm's rem/div instructions. Signed-off-by: Kohei Tokunaga --- tcg/wasm32/tcg-target.c.inc | 120 ++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) diff --git a/tcg/wasm32/tcg-target.c.inc b/tcg/wasm32/tcg-target.c.inc index 1a6069e288..66d3977d31 100644 --- a/tcg/wasm32/tcg-target.c.inc +++ b/tcg/wasm32/tcg-target.c.inc @@ -212,6 +212,22 @@ static void tcg_wasm_out_op_i64_mul(TCGContext *s) { tcg_wasm_out8(s, 0x7e); } +static void tcg_wasm_out_op_i64_div_s(TCGContext *s) +{ + tcg_wasm_out8(s, 0x7f); +} +static void tcg_wasm_out_op_i64_div_u(TCGContext *s) +{ + tcg_wasm_out8(s, 0x80); +} +static void tcg_wasm_out_op_i64_rem_s(TCGContext *s) +{ + tcg_wasm_out8(s, 0x81); +} +static void tcg_wasm_out_op_i64_rem_u(TCGContext *s) +{ + tcg_wasm_out8(s, 0x82); +} static void tcg_wasm_out_op_i64_shl(TCGContext *s) { tcg_wasm_out8(s, 0x86); @@ -995,6 +1011,106 @@ static void tcg_wasm_out_movi(TCGContext *s, TCGType= type, tcg_wasm_out_op_global_set_r(s, ret); } =20 +static void tcg_wasm_out_rem_s( + TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + switch (type) { + case TCG_TYPE_I32: + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_i32_wrap_i64(s); + tcg_wasm_out_op_i64_extend_i32_s(s); + tcg_wasm_out_op_global_get_r(s, arg2); + tcg_wasm_out_op_i32_wrap_i64(s); + tcg_wasm_out_op_i64_extend_i32_s(s); + tcg_wasm_out_op_i64_rem_s(s); + tcg_wasm_out_op_global_set_r(s, ret); + break; + case TCG_TYPE_I64: + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_global_get_r(s, arg2); + tcg_wasm_out_op_i64_rem_s(s); + tcg_wasm_out_op_global_set_r(s, ret); + break; + default: + g_assert_not_reached(); + } +} + +static void tcg_wasm_out_rem_u( + TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + switch (type) { + case TCG_TYPE_I32: + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_i64_const(s, 0xffffffff); + tcg_wasm_out_op_i64_and(s); + tcg_wasm_out_op_global_get_r(s, arg2); + tcg_wasm_out_op_i64_const(s, 0xffffffff); + tcg_wasm_out_op_i64_and(s); + tcg_wasm_out_op_i64_rem_u(s); + tcg_wasm_out_op_global_set_r(s, ret); + break; + case TCG_TYPE_I64: + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_global_get_r(s, arg2); + tcg_wasm_out_op_i64_rem_u(s); + tcg_wasm_out_op_global_set_r(s, ret); + break; + default: + g_assert_not_reached(); + } +} + +static void tcg_wasm_out_div_s( + TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + switch (type) { + case TCG_TYPE_I32: + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_i32_wrap_i64(s); + tcg_wasm_out_op_i64_extend_i32_s(s); + tcg_wasm_out_op_global_get_r(s, arg2); + tcg_wasm_out_op_i32_wrap_i64(s); + tcg_wasm_out_op_i64_extend_i32_s(s); + tcg_wasm_out_op_i64_div_s(s); + tcg_wasm_out_op_global_set_r(s, ret); + break; + case TCG_TYPE_I64: + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_global_get_r(s, arg2); + tcg_wasm_out_op_i64_div_s(s); + tcg_wasm_out_op_global_set_r(s, ret); + break; + default: + g_assert_not_reached(); + } +} + +static void tcg_wasm_out_div_u( + TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + switch (type) { + case TCG_TYPE_I32: + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_i64_const(s, 0xffffffff); + tcg_wasm_out_op_i64_and(s); + tcg_wasm_out_op_global_get_r(s, arg2); + tcg_wasm_out_op_i64_const(s, 0xffffffff); + tcg_wasm_out_op_i64_and(s); + tcg_wasm_out_op_i64_div_u(s); + tcg_wasm_out_op_global_set_r(s, ret); + break; + case TCG_TYPE_I64: + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_global_get_r(s, arg2); + tcg_wasm_out_op_i64_div_u(s); + tcg_wasm_out_op_global_set_r(s, ret); + break; + default: + g_assert_not_reached(); + } +} + static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, intptr_t value, intptr_t addend) { @@ -1556,6 +1672,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_div_s(s, type, a0, a1, a2); } =20 static const TCGOutOpBinary outop_divs =3D { @@ -1574,6 +1691,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_div_u(s, type, a0, a1, a2); } =20 static const TCGOutOpBinary outop_divu =3D { @@ -1718,6 +1836,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_rem_s(s, type, a0, a1, a2); } =20 static const TCGOutOpBinary outop_rems =3D { @@ -1732,6 +1851,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_rem_u(s, type, a0, a1, a2); } =20 static const TCGOutOpBinary outop_remu =3D { --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747745715; cv=none; d=zohomail.com; s=zohoarc; b=XT94WEbOIrDbqXRvnaehwVMKGIZ7SN+yYJwpbynaZdIpZ+VOcanVSVk9hVu0hltKXayDpeikfaKv4cGVQ6fJA3DovJi4wSC6nb0lDoS4bZplvGjkTxLbnxIg+OOhPApvbQMWlo7HUVRBICfZlVUFDfBiec23A0L8ZX6iMGECY2w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747745715; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=muYREexfGkNao6CbCGkvnw0ziWfWGPqgkjYPqsUlkEQ=; b=nQJNIw84PHb9Lkc/4YgcqdHCgxHI/5ng/uY0b1G7IQnN75IwZxzwowymhLIQs+cuB5gKVIzPxFaGpzqaJVTZ5V37fQMUVNbkhA9LAZfqG9+LRIV2F8+RP1aQLMnc02hIk/xzjSO9C19EqMOr/jg+0epegETKSRv2fwue3aS2OzI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747745715661239.46844876117098; Tue, 20 May 2025 05:55:15 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMU0-0001hB-5j; Tue, 20 May 2025 08:54:12 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMTb-0001H4-M3; Tue, 20 May 2025 08:53:47 -0400 Received: from mail-pj1-x1033.google.com ([2607:f8b0:4864:20::1033]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMTZ-0000sX-AC; Tue, 20 May 2025 08:53:47 -0400 Received: by mail-pj1-x1033.google.com with SMTP id 98e67ed59e1d1-30e9e81d517so2784234a91.1; Tue, 20 May 2025 05:53:42 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.53.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:53:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745621; x=1748350421; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=muYREexfGkNao6CbCGkvnw0ziWfWGPqgkjYPqsUlkEQ=; b=UDxU7e+I1vZRXuNAr33LF4zFGlilhPfvmyGUMHSS6FYVVSlvMAqGld8B+WzV0p/5FS RCNXq8MuDghmvX2sf/PrW03xBWTrr29aNvCkogw2KedhnWtglKVBI6L8u/vB+ABx7BWi RqJduN+H0E2n2adUw7wkOoEVJRWbcdvVNd24SRPlseB6IApv+YwTTGV4I/nw4nQXoFDh fWP2N4TTcNDLp9aQnOID2C2E1dfxbUkvnYPhJKOZD67lgsNchwfi6JyTb4qHwY/E2sAp OWIojBNhVm7mO0MKDkBIsAQ/l3mSg+MEoxdZd+IBq0a72dVcMJoYDcsRxZQ7/GNgnc83 knUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745621; x=1748350421; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=muYREexfGkNao6CbCGkvnw0ziWfWGPqgkjYPqsUlkEQ=; b=H7AKup+/iEB3bMOqmM0TxhsKIFnh2dql0FLlGyZdlbXD9sDyL7dk/fB3kFwiDHQqM8 jTsK2FhamWt0ntlFyHq7DQWvN0RtfNd3T/rnP2qlGzg0z/TmnrAuC7TAFJd6quh7REgh +9xL+4kbQfPM/4VgIKgFCrLV9NyR01MAdO+iZYaqiA8X5uewrJcJ+ARh7Ct33AWLobVE dKMsWCNXRt+rfYSvP1+7hqfxQlC43+nkNnwW6EFoyqeVvr+8Uwn7rQ37fuPX6fYP5KOS acSSjTx5aclb+bRGAjA1YnZ8xq/swvWNZPwtZIVSdG12hbdHes/yHcE1VjBgqq2pLiS0 0E0w== X-Forwarded-Encrypted: i=1; AJvYcCW80hthT4n8dbGJ3/JnxQKGE/kcTLWcfZfk6fJl09j1Nlp4XbOcjT96pd4FDxE22SDWQThSkRPd4g5g8g==@nongnu.org, AJvYcCXf4+8y+L8+OVpndmPB5BAP44AlQTBws+vsmCg4WnXcuGM8SbLhkEvo6k7a1ugg+m8WMd6ElYrMPQ==@nongnu.org X-Gm-Message-State: AOJu0YzLFKP1dNIhc+hX4uqE2l1ij4xRAG+vTRF6Qg5qVHVP66KkAbSF 7fXX/0QeaP3OWFBcyrg0oZzWCFQvaNEknX6IfEC5DjsgQbmIyXu4B5fzvEr9XgAb X-Gm-Gg: ASbGnctwGNXOqKXu/dw6ftu60/VJRdsW9njtYtP5qBtNh9gz57qmEmPZ3Djbk+lZPcM DW8TDzWg/DKOcuqECCzqyKJLDYn6Ot5xccNlkvrgvMbc6AWjkst9TuN6OhFLS+tIBzpxzdR0vDv gISn4cAJbIE2eN7VZXIcU9dhDTlUuCuksM1xjxYL7GNcupaalFAVb3eVhCOmoaH/kMa6uL1IH75 WTrgcwL5FhGWiluA7Ps9DNh3darkRFazvvg8LtPBwffW7uONmuvkiOWAJ71AOI4fwyYKe2G6rK0 HJDylzWo2eurG/PRSAvdbOWlU/ydmq6iq6BEEtrvXC3BGu6yJVA= X-Google-Smtp-Source: AGHT+IG0tzA7tiR6O7xDX+cIgsbg7F1YbaiTFl+UQp9GDGzuq1KjR9A3Fqt0K9BadY1lb0SPFB22fA== X-Received: by 2002:a17:90b:51cb:b0:2fe:84d6:cdf9 with SMTP id 98e67ed59e1d1-30e7d5ac8abmr21767013a91.26.1747745620806; Tue, 20 May 2025 05:53:40 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 15/33] tcg/wasm32: Add andc/orc/eqv/nand/nor instructions Date: Tue, 20 May 2025 21:51:17 +0900 Message-ID: <711394a374ef6df21e8759a1768ab542a787353a.1747744132.git.ktokunaga.mail@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1033; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pj1-x1033.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747745716414116600 Content-Type: text/plain; charset="utf-8" This commit implements andc, orc, eqv, nand and nor operations using Wasm instructions. Signed-off-by: Kohei Tokunaga --- tcg/wasm32/tcg-target.c.inc | 55 +++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tcg/wasm32/tcg-target.c.inc b/tcg/wasm32/tcg-target.c.inc index 66d3977d31..50d772f3d6 100644 --- a/tcg/wasm32/tcg-target.c.inc +++ b/tcg/wasm32/tcg-target.c.inc @@ -520,6 +520,56 @@ static void tcg_wasm_out_op_not(TCGContext *s) tcg_wasm_out_op_i64_xor(s); } =20 +static void tcg_wasm_out_andc( + TCGContext *s, TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_global_get_r(s, arg2); + tcg_wasm_out_op_not(s); + tcg_wasm_out_op_i64_and(s); + tcg_wasm_out_op_global_set_r(s, ret); +} + +static void tcg_wasm_out_orc( + TCGContext *s, TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_global_get_r(s, arg2); + tcg_wasm_out_op_not(s); + tcg_wasm_out_op_i64_or(s); + tcg_wasm_out_op_global_set_r(s, ret); +} + +static void tcg_wasm_out_eqv( + TCGContext *s, TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_global_get_r(s, arg2); + tcg_wasm_out_op_i64_xor(s); + tcg_wasm_out_op_not(s); + tcg_wasm_out_op_global_set_r(s, ret); +} + +static void tcg_wasm_out_nand( + TCGContext *s, TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_global_get_r(s, arg2); + tcg_wasm_out_op_i64_and(s); + tcg_wasm_out_op_not(s); + tcg_wasm_out_op_global_set_r(s, ret); +} + +static void tcg_wasm_out_nor( + TCGContext *s, TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_global_get_r(s, arg2); + tcg_wasm_out_op_i64_or(s); + tcg_wasm_out_op_not(s); + tcg_wasm_out_op_global_set_r(s, ret); +} + static void tcg_wasm_out_shl(TCGContext *s, TCGReg ret, TCGReg arg1, TCGReg arg2) { @@ -1618,6 +1668,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); } =20 static const TCGOutOpBinary outop_andc =3D { @@ -1707,6 +1758,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); } =20 static const TCGOutOpBinary outop_eqv =3D { @@ -1788,6 +1840,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); } =20 static const TCGOutOpBinary outop_nand =3D { @@ -1799,6 +1852,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); } =20 static const TCGOutOpBinary outop_nor =3D { @@ -1822,6 +1876,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); } =20 static const TCGOutOpBinary outop_orc =3D { --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747746506; cv=none; d=zohomail.com; s=zohoarc; b=TS0howI3ivroRtUosfu/o36gAEiWLqeTvHpxXjcU/wod9svuM++lxFS0USHcIF43baBuFAo44HMbcO+h55DOzuQw37fo2GqGBE0dwY2Rng+64Z/1XKgbkBhrI8AUA2fyhRcZ8Qe5HEguOKPHu+KDeA4hzJ7GlyOzgTDstL9PC/I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747746506; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=qVm/z5b/WNdO6MaO5hkoo2oUhPq3LCHlkm5p2hpglKI=; b=GCXFy77IyRDFFlZEpop1/5W5dUfFI28MXoW+yyouBAUPYe/Di2u5WTduMc+vqQTb7XEEY+vCRrGYMzj4lb50Ta45uCk1qTTWEmDiOjJ1t1xYRZ0rjxv8iSoWpN4eaLfUY2OcsY5+XR/ZQEDp6CEJDz3qpC2qeW7d7EDiXhEGZYg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747746506810905.6424356954753; Tue, 20 May 2025 06:08:26 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMU5-0001sU-Rs; Tue, 20 May 2025 08:54:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMTg-0001Kl-Gj; Tue, 20 May 2025 08:53:55 -0400 Received: from mail-pj1-x1036.google.com ([2607:f8b0:4864:20::1036]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMTe-0000u3-EY; Tue, 20 May 2025 08:53:52 -0400 Received: by mail-pj1-x1036.google.com with SMTP id 98e67ed59e1d1-30e542e4187so4082032a91.3; Tue, 20 May 2025 05:53:47 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.53.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:53:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745626; x=1748350426; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=qVm/z5b/WNdO6MaO5hkoo2oUhPq3LCHlkm5p2hpglKI=; b=RnHl702K+maMYPeuo1KN+erRMZbfZmWSBo32GmmLdC8QL+Zi/OqpBj201RFu6Zm1fu gjWbxE+Hh0/7XjzJ1BeLDTVAEvGnS3qtaQtBjcm/UIyjgP/vB9Vhk3lfPKxbxwHtlBnY W1oo+ldKt0NhMHrhnCqmM9CI5wZENNvu5IcfGlV0eCKRcTdRySYWMady8X9mwtws9q4O pKi9bNDB3J++ibL5M2PZjv2L4XX6+en59ow3j/0AexB+r+lA9b63uA/lgTg8liTEsG1O vSBiEjYW/z8rgJoq9hHQ7j2ofkBScXvW9eWMr/M7ZE2E8IXkM2hbfWFgZqmI/QY9gyau 9VrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745626; x=1748350426; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qVm/z5b/WNdO6MaO5hkoo2oUhPq3LCHlkm5p2hpglKI=; b=xTL+1MbvtPF4JqGl0iz4EOxPdQYueaP//nzVRG7YyFaqKo6Q/8izswYE6ccqjj2piq nve83h1SkSl6a4p5Tt7FQoK0Bf6D+GcI8znW8BeDpspqId+22i5CfAOOKRGWnLStMieY 3O/EVlTe1DgNuFJUoojUGIPTOt1e0eLCfQ5yCaPyboflV+T63YpPMUIpb1hz1YcayrS8 AeNaZZ4W80D059c8dTcdCCX80AH9L2rFNBcuxLaFu0B4vyMCsEWbJ1So3U8pMGDaLaOP md96S0+FJbt+EmrzxXDHv2d4QjB60Kw0XduVPvpt71+6DhWJuavSyMIfYj1/C/PCSEI/ 84tg== X-Forwarded-Encrypted: i=1; AJvYcCU+I91WQY3VuTtgjxfTjnXDLAv1go2GfzkXre6kiDMpJIUwv4ehmIsTxwFFFD1rCoaHB3teHDq52GzMpw==@nongnu.org, AJvYcCUwHkeJnAKYI8OeFYZuTAo8rOYvnpe619AFpnEnIVbmSzUEU5mC3mPWzneAvDyra61yFPNcDBPw8A==@nongnu.org X-Gm-Message-State: AOJu0YzTMUeP6SlripAVskC8zpTi4n1mS5PbDyWh2bMKu5hdziMf0efm sP93ZRhF1f/zPGEqrfESxc7uZ8Y2iyiBDCMGXS6rt1k75JxkMHW2l11cYgHxhZYR X-Gm-Gg: ASbGncvBqPhLWHgg9jyq8t16BwXCT5D8xh4Ns5ulbTEue4a7tC19BJIkfMBoaxnJgJL QSIdKnIX6kiNc2VPuFaBiyCMHIOJBoDKOgbZbKdHvyTk4HQVtg3dMtN7tLUoFdBQoNZRS4EmnPu 4nnalQnTiaVtlAtuXOqxgHtRsher4k2sSeK8wmBKDT1GfKU3+ztGxvraS+OlKyIhTJCvyXv9CZS 3hw9zq3w49KqWbIdrKEYQuBdHraDtlQcIUKw0bujFCW2PwfLrQkvoNKd983z4XUJxqGvieps/vd WPyITSzlB0li2VVVBhESY2yv8v5Qky3chQyfutCmoUu7MQxinAg= X-Google-Smtp-Source: AGHT+IEMrdC1V+LESFaMuMDw5tT3hhKjWCJ/qaG6s6FWhGMJ038w54wCgcmH9JUMZKT3Ksn+vd9Iow== X-Received: by 2002:a17:90b:28c5:b0:2fc:a3b7:108e with SMTP id 98e67ed59e1d1-30e7d4fea80mr26291198a91.4.1747745626401; Tue, 20 May 2025 05:53:46 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 16/33] tcg/wasm32: Add neg/not/ctpop instructions Date: Tue, 20 May 2025 21:51:18 +0900 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1036; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pj1-x1036.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747746508173116600 Content-Type: text/plain; charset="utf-8" This commit implements neg, not and ctpop operations using Wasm instructions. Signed-off-by: Kohei Tokunaga --- tcg/wasm32/tcg-target.c.inc | 39 +++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/tcg/wasm32/tcg-target.c.inc b/tcg/wasm32/tcg-target.c.inc index 50d772f3d6..e5de2f69bd 100644 --- a/tcg/wasm32/tcg-target.c.inc +++ b/tcg/wasm32/tcg-target.c.inc @@ -200,6 +200,10 @@ static void tcg_wasm_out_op_i64_xor(TCGContext *s) { tcg_wasm_out8(s, 0x85); } +static void tcg_wasm_out_op_i64_popcnt(TCGContext *s) +{ + tcg_wasm_out8(s, 0x7b); +} static void tcg_wasm_out_op_i64_add(TCGContext *s) { tcg_wasm_out8(s, 0x7c); @@ -570,6 +574,29 @@ static void tcg_wasm_out_nor( tcg_wasm_out_op_global_set_r(s, ret); } =20 +static void tcg_wasm_out_neg(TCGContext *s, TCGReg ret, TCGReg arg) +{ + tcg_wasm_out_op_global_get_r(s, arg); + tcg_wasm_out_op_not(s); + tcg_wasm_out_op_i64_const(s, 1); + tcg_wasm_out_op_i64_add(s); + tcg_wasm_out_op_global_set_r(s, ret); +} + +static void tcg_wasm_out_not(TCGContext *s, TCGReg ret, TCGReg arg) +{ + tcg_wasm_out_op_global_get_r(s, arg); + tcg_wasm_out_op_not(s); + tcg_wasm_out_op_global_set_r(s, ret); +} + +static void tcg_wasm_out_ctpop(TCGContext *s, TCGReg dest, TCGReg src) +{ + tcg_wasm_out_op_global_get_r(s, src); + tcg_wasm_out_op_i64_popcnt(s); + tcg_wasm_out_op_global_set_r(s, dest); +} + static void tcg_wasm_out_shl(TCGContext *s, TCGReg ret, TCGReg arg1, TCGReg arg2) { @@ -2056,6 +2083,7 @@ static const TCGOutOpBinary outop_xor =3D { 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_ctpop(s, a0, a1); } =20 static TCGConstraintSetIndex cset_ctpop(TCGType type, unsigned flags) @@ -2112,9 +2140,15 @@ static const TCGOutOpUnary outop_bswap64 =3D { }; #endif =20 -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); } =20 static const TCGOutOpUnary outop_neg =3D { @@ -2125,6 +2159,7 @@ static const TCGOutOpUnary outop_neg =3D { 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); } =20 static const TCGOutOpUnary outop_not =3D { @@ -2157,7 +2192,7 @@ static void tgen_negsetcond(TCGContext *s, TCGType ty= pe, 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); } =20 --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747745699; cv=none; d=zohomail.com; s=zohoarc; b=GJVGeNrI1xXI6uLvJ9DXefjq9b819sE3kLJvJ65Bu/GuMXdCpFI/+P3Avu60w+fbVXELeaQWFNojb/KKgDnv+wHynRAVRWlkX5+KFbmCGJfrwvlmG85AsZ/5+zWmo7NHloAXH16keBoiRKXXYKz/rtCvhDrFqTroJlvpEKG6lrk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747745699; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=2hDX6tqZfoQbfyom/brX/vCkpGWr5vOfzD19dhVKfxE=; b=nCPh1tCMHDBwJDbTaEEO1Hu4RdgQ8ve7Hnio0gZceiUxAUz3xslfnX0wddKQfaCirSsYiE1FlMgsh6CQ6/mZiohgkpf8x9fuRvpqkGGn/YNpUt/afCapYrbAeJGbwWeC9dtfxaFwg4NlLhfoADwL2Tpn/Iu6Pa0s3nw0YwHIGp4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747745699186930.7095033303584; Tue, 20 May 2025 05:54:59 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMUD-0002Hj-3m; Tue, 20 May 2025 08:54:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMTm-0001Nd-Tg; Tue, 20 May 2025 08:53:59 -0400 Received: from mail-pj1-x1030.google.com ([2607:f8b0:4864:20::1030]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMTk-0000vM-HZ; Tue, 20 May 2025 08:53:58 -0400 Received: by mail-pj1-x1030.google.com with SMTP id 98e67ed59e1d1-30ed99132abso2201898a91.3; Tue, 20 May 2025 05:53:53 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.53.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:53:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745632; x=1748350432; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=2hDX6tqZfoQbfyom/brX/vCkpGWr5vOfzD19dhVKfxE=; b=NW+UhHjncqUV5XRLY1hOMN8fMDnCo9VIdXJSATOzjLCqqR6KNaBPiHR5mDptZ6Zh0u jO0ozr73pVv0kxjDNjVF2sqciLF6Bg5ItSrFCFxmFGlWLEsd1AXah9dQvRGyy5XWuEkm ZvaHxZQxTOVjILjafrC944bt1Owfz3R3JnyItkwwn23sPZS+nkcEJwUGPmz+myeBqA9f DqEFpJO8Zubrr/1jDBG+yM64/NWuJsVqgdq3dBa/eAcH5ITQop5zMja08q0tnYJWJuGF /xCZaAXFz6YTG6tkJp9Xsw2ogBsz2mgL05kqLm1nYAGcCrfftvu5bqHu5OIKlhc7xp/3 moCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745632; x=1748350432; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2hDX6tqZfoQbfyom/brX/vCkpGWr5vOfzD19dhVKfxE=; b=LecCGoflZL8iP4r9Ds8x4MRu//9sYfZyD4qM7hxl9mzDVR/i+m3MWLQ8p5bEVsoUC4 5I1IJraniYXCRoKhhHu8ERinGWa/qSwPdTwci5U5qs23f4t7gmweTVDij2rxJaceDRVn NQce3nHm0izrbkYMrNf+4kLvt7nwmOJbBq98ZswxnsvJWQ9tDsIbP1VVa/a6j6cS9jL1 HFQNnytrrq24V3ZuW2NahqpGhuP7z4M8pHDgai+/n05Tu08A9uyl2qsCG/BxDcy0+Qn+ Cuvr/VU21Ki6JbyZZ/51cJCDGOlNjqyj5/FGiDyq/+wWfWMQfcRR6UZS4khsZZ6J9kK2 kUoQ== X-Forwarded-Encrypted: i=1; AJvYcCVyzpD6RZJiAljswiU+QfLhwP4HJooiOWQfCM/YPp/TkDnPRZSZj9e/KTTTK4GRmAcLlkcySjm/4A==@nongnu.org, AJvYcCXPsZtuuhOkymiBDNJFnNCBPp0p4QMEnyANL3alSWSanxVhJc78NsUohNAe44ulSNggsGWkl9vCT4ZccA==@nongnu.org X-Gm-Message-State: AOJu0YypskfLSarWo5Dki1Io7uNHrEqsGCU529cTXX1qVDrb10Niz84N 3feO2sixJvfpKryPciiEhmcTFB5PskC/YfXiM4pnHUHJmvps2bWV3YFL6SOiIvPZ X-Gm-Gg: ASbGncul9OEkHHBY/ieQjtiG8bjUJNO7va5I9ZZ2LlaNM+bMqk8v2HCAeWQ03hedHHx AnZH1jNdYK5sSryZpUlkJDnW931KIwvzpa7f8OQkOm7umI623zKiko2xgPg+8A9jei8Axeo3ElN b2ue731xkkaLDxwE3kxoxBBPW4ZO5OmHuSMHAfKTvwszrHMDKz94khPQJuY0LcnwJ+FL5weRCKt ZwohxF3gdFhpX6mY2J3d4YgeaKHiIcHFSQ1cBngB8zJqzKpezu2PXebbjaURn9KW0cPAUoCEq0y tt52CkB3TZR0xrTSsDW8AkSZzG+351yRVy5UgWWcq1BKkoNNX0I= X-Google-Smtp-Source: AGHT+IE4U2UXF2xs+H6bxIt71NsbCMeWEhfCOMavrX7tJFkvHIkvkiyDXs60OAAtuPGm3U++zpH5Cg== X-Received: by 2002:a17:90b:28c5:b0:2ee:f076:20f1 with SMTP id 98e67ed59e1d1-30e7d2def4fmr30108178a91.0.1747745632031; Tue, 20 May 2025 05:53:52 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 17/33] tcg/wasm32: Add rot/clz/ctz instructions Date: Tue, 20 May 2025 21:51:19 +0900 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1030; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pj1-x1030.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747745701371116600 Content-Type: text/plain; charset="utf-8" This commit implements rot, clz and ctz operations using Wasm instructions. Signed-off-by: Kohei Tokunaga --- tcg/wasm32/tcg-target.c.inc | 155 ++++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) diff --git a/tcg/wasm32/tcg-target.c.inc b/tcg/wasm32/tcg-target.c.inc index e5de2f69bd..75e47f8c8c 100644 --- a/tcg/wasm32/tcg-target.c.inc +++ b/tcg/wasm32/tcg-target.c.inc @@ -200,6 +200,14 @@ static void tcg_wasm_out_op_i64_xor(TCGContext *s) { tcg_wasm_out8(s, 0x85); } +static void tcg_wasm_out_op_i64_clz(TCGContext *s) +{ + tcg_wasm_out8(s, 0x79); +} +static void tcg_wasm_out_op_i64_ctz(TCGContext *s) +{ + tcg_wasm_out8(s, 0x7a); +} static void tcg_wasm_out_op_i64_popcnt(TCGContext *s) { tcg_wasm_out8(s, 0x7b); @@ -244,6 +252,10 @@ static void tcg_wasm_out_op_i64_shr_u(TCGContext *s) { tcg_wasm_out8(s, 0x88); } +static void tcg_wasm_out_op_i64_rotl(TCGContext *s) +{ + tcg_wasm_out8(s, 0x89); +} static void tcg_wasm_out_op_i64_rotr(TCGContext *s) { tcg_wasm_out8(s, 0x8a); @@ -268,6 +280,14 @@ static void tcg_wasm_out_op_i64_extend16_s(TCGContext = *s) { tcg_wasm_out8(s, 0xc3); } +static void tcg_wasm_out_op_i32_clz(TCGContext *s) +{ + tcg_wasm_out8(s, 0x67); +} +static void tcg_wasm_out_op_i32_ctz(TCGContext *s) +{ + tcg_wasm_out8(s, 0x68); +} static void tcg_wasm_out_op_i32_add(TCGContext *s) { tcg_wasm_out8(s, 0x6a); @@ -292,16 +312,33 @@ static void tcg_wasm_out_op_i32_shr_u(TCGContext *s) { tcg_wasm_out8(s, 0x76); } +static void tcg_wasm_out_op_i32_rotl(TCGContext *s) +{ + tcg_wasm_out8(s, 0x77); +} static void tcg_wasm_out_op_i32_rotr(TCGContext *s) { tcg_wasm_out8(s, 0x78); } +static void tcg_wasm_out_op_i32_eqz(TCGContext *s) +{ + tcg_wasm_out8(s, 0x45); +} +static void tcg_wasm_out_op_i64_eqz(TCGContext *s) +{ + tcg_wasm_out8(s, 0x50); +} =20 static void tcg_wasm_out_op_if_ret_i64(TCGContext *s) { tcg_wasm_out8(s, 0x04); tcg_wasm_out8(s, 0x7e); } +static void tcg_wasm_out_op_if_ret_i32(TCGContext *s) +{ + tcg_wasm_out8(s, 0x04); + tcg_wasm_out8(s, 0x7f); +} static void tcg_wasm_out_op_else(TCGContext *s) { tcg_wasm_out8(s, 0x05); @@ -360,6 +397,8 @@ tcg_wasm_out_i64_calc(xor); tcg_wasm_out_i64_calc(add); tcg_wasm_out_i64_calc(sub); tcg_wasm_out_i64_calc(mul); +tcg_wasm_out_i64_calc(rotl); +tcg_wasm_out_i64_calc(rotr); =20 static const struct { uint8_t i32; @@ -918,6 +957,118 @@ static void tcg_wasm_out_bswap16( tcg_wasm_out_op_global_set_r(s, dest); } =20 +static void tcg_wasm_out_rotl( + TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + switch (type) { + case TCG_TYPE_I32: + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_i32_wrap_i64(s); + tcg_wasm_out_op_global_get_r(s, arg2); + tcg_wasm_out_op_i32_wrap_i64(s); + tcg_wasm_out_op_i32_rotl(s); + tcg_wasm_out_op_i64_extend_i32_s(s); + tcg_wasm_out_op_global_set_r(s, ret); + break; + case TCG_TYPE_I64: + tcg_wasm_out_i64_calc_rotl(s, ret, arg1, arg2); + break; + default: + g_assert_not_reached(); + } +} + +static void tcg_wasm_out_rotr( + TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + switch (type) { + case TCG_TYPE_I32: + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_i32_wrap_i64(s); + tcg_wasm_out_op_global_get_r(s, arg2); + tcg_wasm_out_op_i32_wrap_i64(s); + tcg_wasm_out_op_i32_rotr(s); + tcg_wasm_out_op_i64_extend_i32_s(s); + tcg_wasm_out_op_global_set_r(s, ret); + break; + case TCG_TYPE_I64: + tcg_wasm_out_i64_calc_rotr(s, ret, arg1, arg2); + break; + default: + g_assert_not_reached(); + } +} + +static void tcg_wasm_out_clz( + TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + switch (type) { + case TCG_TYPE_I32: + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_i32_wrap_i64(s); + tcg_wasm_out_op_i32_eqz(s); + tcg_wasm_out_op_if_ret_i32(s); + tcg_wasm_out_op_global_get_r(s, arg2); + tcg_wasm_out_op_i32_wrap_i64(s); + tcg_wasm_out_op_else(s); + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_i32_wrap_i64(s); + tcg_wasm_out_op_i32_clz(s); + tcg_wasm_out_op_end(s); + tcg_wasm_out_op_i64_extend_i32_s(s); + tcg_wasm_out_op_global_set_r(s, ret); + break; + case TCG_TYPE_I64: + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_i64_eqz(s); + tcg_wasm_out_op_if_ret_i64(s); + tcg_wasm_out_op_global_get_r(s, arg2); + tcg_wasm_out_op_else(s); + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_i64_clz(s); + tcg_wasm_out_op_end(s); + tcg_wasm_out_op_global_set_r(s, ret); + break; + default: + g_assert_not_reached(); + } +} + +static void tcg_wasm_out_ctz( + TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + switch (type) { + case TCG_TYPE_I32: + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_i32_wrap_i64(s); + tcg_wasm_out_op_i32_eqz(s); + tcg_wasm_out_op_if_ret_i32(s); + tcg_wasm_out_op_global_get_r(s, arg2); + tcg_wasm_out_op_i32_wrap_i64(s); + tcg_wasm_out_op_else(s); + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_i32_wrap_i64(s); + tcg_wasm_out_op_i32_ctz(s); + tcg_wasm_out_op_end(s); + tcg_wasm_out_op_i64_extend_i32_s(s); + tcg_wasm_out_op_global_set_r(s, ret); + break; + case TCG_TYPE_I64: + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_i64_eqz(s); + tcg_wasm_out_op_if_ret_i64(s); + tcg_wasm_out_op_global_get_r(s, arg2); + tcg_wasm_out_op_else(s); + tcg_wasm_out_op_global_get_r(s, arg1); + tcg_wasm_out_op_i64_ctz(s); + tcg_wasm_out_op_end(s); + tcg_wasm_out_op_global_set_r(s, ret); + break; + default: + g_assert_not_reached(); + } +} + static void tcg_wasm_out_ld( TCGContext *s, TCGType type, TCGReg val, TCGReg base, intptr_t offset) { @@ -1710,6 +1861,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_clz(s, type, a0, a1, a2); } =20 static const TCGOutOpBinary outop_clz =3D { @@ -1724,6 +1876,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_ctz(s, type, a0, a1, a2); } =20 static const TCGOutOpBinary outop_ctz =3D { @@ -1948,6 +2101,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_rotl(s, type, a0, a1, a2); } =20 static const TCGOutOpBinary outop_rotl =3D { @@ -1962,6 +2116,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_rotr(s, type, a0, a1, a2); } =20 static const TCGOutOpBinary outop_rotr =3D { --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747745738; cv=none; d=zohomail.com; s=zohoarc; b=BMJ/+E4NiXxXSjPgWRnMMl8lzdgbxt+k4ZNYHJpbA+5K+LFDYBeGrQAbm4mKBFjHIG0zAXI94kiZgE4uvB+PHXBYRqhR9Ar3aOOqnmLM8s3R83Zgl8hsqvzs+TW0TVj+/AqH96h28BECgUoQh4Rh5qGzV3UoEXok+zMq5Eycc9c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747745738; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=KbZX43g+ERfcOdN/FYEqrH5HB45spFaPpgCTkfGLlg0=; b=FGoKyyz7G3LxUUW0DGGflZijwYazSQPuACeCNrdzCZImWBJXxbWxuLSWw3e/9XoJAIonRo/OUF/Rbw+qovYYWZu0epHmC+ZEEMY/n5nFDw6tX9x+eRlnTBpJ03YXJY6xjt/pxE0Eh412nAQEn7erDxISugy3Brj+3fllfJJjaPk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747745738158747.3520501919573; Tue, 20 May 2025 05:55:38 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMU9-00022S-0e; Tue, 20 May 2025 08:54:21 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMTv-0001c1-EH; Tue, 20 May 2025 08:54:07 -0400 Received: from mail-pj1-x1030.google.com ([2607:f8b0:4864:20::1030]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMTp-0000w8-6z; Tue, 20 May 2025 08:54:07 -0400 Received: by mail-pj1-x1030.google.com with SMTP id 98e67ed59e1d1-30ec226b7d6so2581874a91.2; Tue, 20 May 2025 05:53:58 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.53.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:53:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745638; x=1748350438; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=KbZX43g+ERfcOdN/FYEqrH5HB45spFaPpgCTkfGLlg0=; b=gr1IKavKYMZ4VRNAVe1LjvWHHoscZiTfzjUM3NQ5ZH4O9b8P5e2ZCtC0oeXpxjay68 GFYjHfgqWLYWtWewblLQb4xAWxilaqwW2McNFo550TKeaxFKbJZ/FeimiJXFR4UUrrFm ygZZJZpVZTrUJ1+fcQG/lUgyBSGYTI8AV4OQWbZDp1NumGbpf3xha6bi4792sFVoP2OP nnTKl7ihdgrKvR2QpD3XdNoG1dGwbjmODljbqdxCntfPtgNAHatNoiPHswvoNDrnaPV8 Oe8F+POqTYVJFBxzpcNbdJHzFyHQrt/6F9zCmp6OhZ+TvgN9wSB1KzwAI3wUCYR0TaqQ l4PQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745638; x=1748350438; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KbZX43g+ERfcOdN/FYEqrH5HB45spFaPpgCTkfGLlg0=; b=m7EzyNDMIcHIurW7DjROjINT8QcEAltBcxOfstjSagijt4Zt4WJ1wqdXwlPQxUXBPa rQSw+KJkhwa/1r1O2JZU4O5lwfuQ5RTz7/4NvKYAcDmXb4CtQTgeMCzYi04ujJQKFgLT 10RTor904lxeuHGjGKxerZ2eDHy8HoU7fx/X6ivhof2aZwLy2C6kJpB6Zm7sH4N5tDzG ub0nz/lgUlfLkjNMhJDFLbp9Lln6vKFemYwxuBVGlRMBFN3M7VvsXd7s7xJ+gT22V43/ UkSmnxHfuKdjQaRLmr4q2AW7tErj7IZ/I2ewu7YtFucJlHcbU08l1kLZPwsy25nAFyJx 2yag== X-Forwarded-Encrypted: i=1; AJvYcCWVWzn8mSz/GRS3krtD7wn9H5WzlGnNGxsoLnUQBYgu6oHM6HKh0rYq44dW/w8gGE5aSvaKUizCrw==@nongnu.org, AJvYcCWiWcRaENWzgglnsGWeqQBQQuMgi9wOvZnXnnNEoaIqEE4jR6SNu2OHDA2K4zDnOuycR52zRWvIisNrpg==@nongnu.org X-Gm-Message-State: AOJu0YxFTdutFLV70ubFGEd4rVSJ11h3mzelZEk8SxPwSpLdvX6F3Uyn 7ReQuy1cT1mnQuoM+xzc+Qdi1hC03cUuSpHvb/iGl4USwsJ+MIt28tjOqA2Dy0oZ X-Gm-Gg: ASbGnctvZNcd8w2Lyvzb72c+5c5/Vh4exZMW2HaCQpB08/YdNdV27JIq+wbwUQ1z++r TLd+EaTKm567Ox6G9BN4mnyQYwVn+LzjVm6EiprHW4gk5x5lqCP7M9Hi8t5XSzkDD+oTAjYN4yO 1Uk1JBvzDyViFW5cbUKITvsML5mWPy2BLSej9SKFvwyNsFNydZ2KduRl59ArI5Qw58CDoqjb8IU WaAy53Awa3fboG8g/+wq8rDMygBKYc77NgnMVUgGs68MseFuOIxIW3lmHbn2Gc38RBib52jsB7w 0I4AWb3P8oCvjafyfrmt7/UbhvzWEdtGLTVHUJqPi25FGIt41Ac= X-Google-Smtp-Source: AGHT+IHuOm/86ztaL4MQCbuQAhFYIVkJJTKeycgm07PUBHqOLU5sp7gXPnJumnQRDpcHc7aR0NgZzA== X-Received: by 2002:a17:90b:1dd1:b0:305:5f32:d9f5 with SMTP id 98e67ed59e1d1-30e7d4f9220mr28781405a91.7.1747745637647; Tue, 20 May 2025 05:53:57 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 18/33] tcg/wasm32: Add addc/subb instructions Date: Tue, 20 May 2025 21:51:20 +0900 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1030; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pj1-x1030.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747745740918116600 Content-Type: text/plain; charset="utf-8" This commit implements addc and subb operations using Wasm instructions. A carry flag is introduced as the 16th variable in the module following other 15 variables that represent TCG variables. Signed-off-by: Kohei Tokunaga --- tcg/wasm32/tcg-target.c.inc | 151 ++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) diff --git a/tcg/wasm32/tcg-target.c.inc b/tcg/wasm32/tcg-target.c.inc index 75e47f8c8c..167850ea7c 100644 --- a/tcg/wasm32/tcg-target.c.inc +++ b/tcg/wasm32/tcg-target.c.inc @@ -118,6 +118,11 @@ static const uint8_t tcg_target_reg_index[TCG_TARGET_N= B_REGS] =3D { 15, /* TCG_REG_R15 */ }; =20 +/* + * Global variable to store the carry flag + */ +#define CARRY_IDX 16 + /* Temporary local variables */ #define TMP32_LOCAL_0_IDX 1 #define TMP32_LOCAL_1_IDX 2 @@ -324,10 +329,23 @@ static void tcg_wasm_out_op_i32_eqz(TCGContext *s) { tcg_wasm_out8(s, 0x45); } +static void tcg_wasm_out_op_i64_lt_u(TCGContext *s) +{ + tcg_wasm_out8(s, 0x54); +} +static void tcg_wasm_out_op_i64_le_u(TCGContext *s) +{ + tcg_wasm_out8(s, 0x58); +} static void tcg_wasm_out_op_i64_eqz(TCGContext *s) { tcg_wasm_out8(s, 0x50); } +static void tcg_wasm_out_op_if_noret(TCGContext *s) +{ + tcg_wasm_out8(s, 0x04); + tcg_wasm_out8(s, 0x40); +} =20 static void tcg_wasm_out_op_if_ret_i64(TCGContext *s) { @@ -1789,10 +1807,28 @@ static TCGConstraintSetIndex cset_addsubcarry(TCGTy= pe type, unsigned flags) return type =3D=3D TCG_TYPE_REG ? C_O1_I2(r, r, r) : C_NotImplemented; } =20 +static void tcg_wasm_out_addco(TCGContext *s, TCGReg a0, TCGReg a1, TCGReg= a2) +{ + tcg_wasm_out_op_global_get_r(s, a1); + tcg_wasm_out_op_global_get_r(s, a2); + tcg_wasm_out_op_i64_add(s); + tcg_wasm_out_op_global_set_r(s, a0); + tcg_wasm_out_op_global_get_r(s, a0); + if (a0 =3D=3D a1) { + tcg_wasm_out_op_global_get_r(s, a2); + } else { + tcg_wasm_out_op_global_get_r(s, a1); + } + tcg_wasm_out_op_i64_lt_u(s); + tcg_wasm_out_op_i64_extend_i32_s(s); + tcg_wasm_out_op_global_set(s, CARRY_IDX); +} + 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); + tcg_wasm_out_addco(s, a0, a1, a2); } =20 static const TCGOutOpBinary outop_addco =3D { @@ -1801,10 +1837,21 @@ static const TCGOutOpBinary outop_addco =3D { .out_rrr =3D tgen_addco, }; =20 +static void tcg_wasm_out_addci(TCGContext *s, TCGReg a0, TCGReg a1, TCGReg= a2) +{ + tcg_wasm_out_op_global_get_r(s, a1); + tcg_wasm_out_op_global_get_r(s, a2); + tcg_wasm_out_op_i64_add(s); + tcg_wasm_out_op_global_get(s, CARRY_IDX); + tcg_wasm_out_op_i64_add(s); + tcg_wasm_out_op_global_set_r(s, a0); +} + 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); + tcg_wasm_out_addci(s, a0, a1, a2); } =20 static const TCGOutOpAddSubCarry outop_addci =3D { @@ -1813,10 +1860,51 @@ static const TCGOutOpAddSubCarry outop_addci =3D { .out_rrr =3D tgen_addci, }; =20 +static void tcg_wasm_out_addcio(TCGContext *s, TCGReg a0, TCGReg a1, TCGRe= g a2) +{ + tcg_wasm_out_op_global_get(s, CARRY_IDX); + tcg_wasm_out_op_if_noret(s); + + tcg_wasm_out_op_global_get_r(s, a1); + tcg_wasm_out_op_global_get_r(s, a2); + tcg_wasm_out_op_i64_add(s); + tcg_wasm_out_op_i64_const(s, 1); + tcg_wasm_out_op_i64_add(s); + tcg_wasm_out_op_global_set_r(s, a0); + tcg_wasm_out_op_global_get_r(s, a0); + if (a0 =3D=3D a1) { + tcg_wasm_out_op_global_get_r(s, a2); + } else { + tcg_wasm_out_op_global_get_r(s, a1); + } + tcg_wasm_out_op_i64_le_u(s); + tcg_wasm_out_op_i64_extend_i32_s(s); + tcg_wasm_out_op_global_set(s, CARRY_IDX); + + tcg_wasm_out_op_else(s); + + tcg_wasm_out_op_global_get_r(s, a1); + tcg_wasm_out_op_global_get_r(s, a2); + tcg_wasm_out_op_i64_add(s); + tcg_wasm_out_op_global_set_r(s, a0); + tcg_wasm_out_op_global_get_r(s, a0); + if (a0 =3D=3D a1) { + tcg_wasm_out_op_global_get_r(s, a2); + } else { + tcg_wasm_out_op_global_get_r(s, a1); + } + tcg_wasm_out_op_i64_lt_u(s); + tcg_wasm_out_op_i64_extend_i32_s(s); + tcg_wasm_out_op_global_set(s, CARRY_IDX); + + tcg_wasm_out_op_end(s); +} + 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); + tcg_wasm_out_addcio(s, a0, a1, a2); } =20 static const TCGOutOpBinary outop_addcio =3D { @@ -1828,6 +1916,8 @@ static const TCGOutOpBinary outop_addcio =3D { static void tcg_out_set_carry(TCGContext *s) { tcg_out_op_v(s, INDEX_op_tci_setcarry); + tcg_wasm_out_op_i64_const(s, 1); + tcg_wasm_out_op_global_set(s, CARRY_IDX); } =20 static void tgen_and(TCGContext *s, TCGType type, @@ -2182,10 +2272,25 @@ static const TCGOutOpSubtract outop_sub =3D { .out_rrr =3D tgen_sub, }; =20 +static void tcg_wasm_out_subbo(TCGContext *s, TCGReg a0, TCGReg a1, TCGReg= a2) +{ + tcg_wasm_out_op_global_get_r(s, a1); + tcg_wasm_out_op_global_get_r(s, a2); + tcg_wasm_out_op_i64_lt_u(s); + tcg_wasm_out_op_i64_extend_i32_s(s); + tcg_wasm_out_op_global_set(s, CARRY_IDX); + + tcg_wasm_out_op_global_get_r(s, a1); + tcg_wasm_out_op_global_get_r(s, a2); + tcg_wasm_out_op_i64_sub(s); + tcg_wasm_out_op_global_set_r(s, a0); +} + 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); + tcg_wasm_out_subbo(s, a0, a1, a2); } =20 static const TCGOutOpAddSubCarry outop_subbo =3D { @@ -2194,10 +2299,21 @@ static const TCGOutOpAddSubCarry outop_subbo =3D { .out_rrr =3D tgen_subbo, }; =20 +static void tcg_wasm_out_subbi(TCGContext *s, TCGReg a0, TCGReg a1, TCGReg= a2) +{ + tcg_wasm_out_op_global_get_r(s, a1); + tcg_wasm_out_op_global_get_r(s, a2); + tcg_wasm_out_op_i64_sub(s); + tcg_wasm_out_op_global_get(s, CARRY_IDX); + tcg_wasm_out_op_i64_sub(s); + tcg_wasm_out_op_global_set_r(s, a0); +} + 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); + tcg_wasm_out_subbi(s, a0, a1, a2); } =20 static const TCGOutOpAddSubCarry outop_subbi =3D { @@ -2206,10 +2322,43 @@ static const TCGOutOpAddSubCarry outop_subbi =3D { .out_rrr =3D tgen_subbi, }; =20 +static void tcg_wasm_out_subbio(TCGContext *s, TCGReg a0, TCGReg a1, TCGRe= g a2) +{ + tcg_wasm_out_op_global_get(s, CARRY_IDX); + tcg_wasm_out_op_if_noret(s); + + tcg_wasm_out_op_global_get_r(s, a1); + tcg_wasm_out_op_global_get_r(s, a2); + tcg_wasm_out_op_i64_le_u(s); + tcg_wasm_out_op_i64_extend_i32_s(s); + tcg_wasm_out_op_global_set(s, CARRY_IDX); + tcg_wasm_out_op_global_get_r(s, a1); + tcg_wasm_out_op_global_get_r(s, a2); + tcg_wasm_out_op_i64_sub(s); + tcg_wasm_out_op_i64_const(s, 1); + tcg_wasm_out_op_i64_sub(s); + tcg_wasm_out_op_global_set_r(s, a0); + + tcg_wasm_out_op_else(s); + + tcg_wasm_out_op_global_get_r(s, a1); + tcg_wasm_out_op_global_get_r(s, a2); + tcg_wasm_out_op_i64_lt_u(s); + tcg_wasm_out_op_i64_extend_i32_s(s); + tcg_wasm_out_op_global_set(s, CARRY_IDX); + tcg_wasm_out_op_global_get_r(s, a1); + tcg_wasm_out_op_global_get_r(s, a2); + tcg_wasm_out_op_i64_sub(s); + tcg_wasm_out_op_global_set_r(s, a0); + + tcg_wasm_out_op_end(s); +} + 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); + tcg_wasm_out_subbio(s, a0, a1, a2); } =20 static const TCGOutOpAddSubCarry outop_subbio =3D { @@ -2221,6 +2370,8 @@ static const TCGOutOpAddSubCarry outop_subbio =3D { static void tcg_out_set_borrow(TCGContext *s) { tcg_out_op_v(s, INDEX_op_tci_setcarry); /* borrow =3D=3D carry */ + tcg_wasm_out_op_i64_const(s, 1); + tcg_wasm_out_op_global_set(s, CARRY_IDX); } =20 static void tgen_xor(TCGContext *s, TCGType type, --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747746486; cv=none; d=zohomail.com; s=zohoarc; b=Z5+WG+Ae+ND28CDkgtC49hBf+fbdFLDYunrF5qomTKyn3sOr2vUsuT7IMwhMvkj7MjzrN8xKca6JWhCDMrYKjvFXXmvNPjAe5pTMXT58Rv0RYWrpJcXcmg9h4GLThd6fvifj5SaTNZ45ChqtfYjzFLuOlfnDROvx5/CqlIxrthE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747746486; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=ny2gj+P5qO3fOEdwuuQ9ZXSBwum4GBrBb6IEryXJJDw=; b=fcijgJwxdKb+83/HFsmK9XONPgiuO/DX977hcv9MGueCcKFliO6+ec/lmPx71StfY7YWQJtHrnG/HWlYE6MRJj3al8aQqY1xlIPNLkxsS0r5NeaG/+PMnlobHKbU7jKLkIpaUMXMW6ysgsQZo2xEXMtzkSmgI1bsJW4uZaCxSlw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747746486650655.0525582519577; Tue, 20 May 2025 06:08:06 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMUg-0003Ch-03; Tue, 20 May 2025 08:54:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMTx-0001fj-RX; Tue, 20 May 2025 08:54:10 -0400 Received: from mail-pg1-x531.google.com ([2607:f8b0:4864:20::531]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMTu-0000wo-IK; Tue, 20 May 2025 08:54:09 -0400 Received: by mail-pg1-x531.google.com with SMTP id 41be03b00d2f7-af523f4511fso4384159a12.0; Tue, 20 May 2025 05:54:04 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.53.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:54:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745643; x=1748350443; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ny2gj+P5qO3fOEdwuuQ9ZXSBwum4GBrBb6IEryXJJDw=; b=N5wqTudM7K9TpvnamPJffdwLqQnXZlCkLvcUCtJvrWoiaitC7BBzz1RmsUmEbm+XsD O8cZhas0FG8U3jwqQUX9pQXq4OUnHrAIWiK1MoicegJjqAAt8EdmAI/VxV+TmIZ9QtQr E4gPGYnXdlZROcVGv6lfAbuK0IF7Hb0p/jiwVuwChOePXfpfuKPUmtPDvELeNB1XzMSM qLjvR6/vHp/cU9CqvVoBJz2WY0CMTbbwh5u32Ge78iIu9nI2SdQnolbj7/H0EN8YW3+v hyN9tXAMONIh51lhxGLMMMpKXxakPoXwdjYDIKfen6ApNXB4xLLuxxF6q2HkDgDP2IsW dlew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745643; x=1748350443; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ny2gj+P5qO3fOEdwuuQ9ZXSBwum4GBrBb6IEryXJJDw=; b=pmqbDQp2RVAeYer9bP6HdrVBpxMreHg0cKSFmecljSGXU71bu9Yvlt+tU+XcTDSXyS nPBNv+rhACVOO0rlaZh30IvLBgUjGwrdfmHvMDCG8pzdujthvU6YyM7qasAfjwyKwLCe P96zfsIFqQjjhXQn8c/mvCbbxerutk1rK0mno15GPPfhGpCeojaaI1PlhYYNCK/OqQUQ MU/GSNjC/WbGmJN/tQxdjWagHmm2Vm2rJW4YArFN0ycA7Ye30PKZ0Kx1LrguOOOJe1sp YlzUxFiZNspB1IymhUp6TXoU/MkmIKNGz5h63eae5nwU2lWRaz5oYvURMr1v9kTXPgd7 WINQ== X-Forwarded-Encrypted: i=1; AJvYcCUaXzg/OF7Qxwyd7sA0sOKkgrfBZigRylP5uLHRtDrmGkH/kXiHwoR8ZXIFmEKX8IqqcZ7+YeihJDtH3g==@nongnu.org, AJvYcCVUlBf8oqVfFHpYVTX0McbikS0qeJLAd8LSMTnemO1KWm5tUKV6hMUxulXhWkiRYzj9n1dn/BtOFA==@nongnu.org X-Gm-Message-State: AOJu0YyPVVhAP5qQJoRWBuueJ0Q3S1pb9g722cszHQX5E26b8TqqYNzI rk/A70MZpMkg4GHm+X7kLpKwdodehmdA/WByEzvu1AjJw1MdmZNGcnwyRhD5MYFx X-Gm-Gg: ASbGncvcB6bcj5D9goYEQjZ/274tAcYFkdCkhl/0U9/wVLGE6VYlRfBPb9mZPcnlyB/ v3rkIg9srf5AOZ9WUWwVGiAPq31J1dEBdUQu1ItjsnenVDTbcbaXhJ+fn35KTsvtK/W8HnvvaQf 97UeMePdfCdq2TmCVZa+qnY3Nfj71JGusodoX+ES3rxiEJQCrTj/T6zHm4X0RVov0pWSRFL/Q8p 1KuYVOtIpwjF+0k4uCBHdQl1Yidc0DpBA4ShHlyHnSJniUL5q0VZkarVLEu023BaUN8nj4lUlGG Eva8PwWui5twsP8mWQfzPdfJWTG1kf0daOuPp6JpVKuCaRRbEYQ= X-Google-Smtp-Source: AGHT+IG3jBPOn/k3aPkAjgq6xDexOXXxHya/0hQuUYzVk8W1vJT86lB8gaIyA+uiA0eBqDY/HQ+GKA== X-Received: by 2002:a17:90b:4b0f:b0:2f6:be57:49d2 with SMTP id 98e67ed59e1d1-30e7d545920mr29666251a91.17.1747745643411; Tue, 20 May 2025 05:54:03 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 19/33] tcg/wasm32: Add br/brcond instructions Date: Tue, 20 May 2025 21:51:21 +0900 Message-ID: <65a405aecd6e2212217919337a546379699f1ed0.1747744132.git.ktokunaga.mail@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::531; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pg1-x531.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747746488141116600 Content-Type: text/plain; charset="utf-8" Wasm does not support direct jumps to arbitrary code addresses, so label-based control flow is implemented using Wasm's control flow instructions. As illustrated in the pseudo-code below, each TB wraps its instructions inside a large loop. Each set of codes separated by labels is placed inside an "if" block. Br is implemented by breaking out of the current block and conditionally entering the target block: loop if ... code after label1 end if ... code after label2 end ... end Each block within the TB is assigned a unique int32 ID. The topmost "if" block is assigned ID 0, and subsequent blocks are assigned incrementally. To control br, this commit introduces a 17th Wasm variable BLOCK_PTR_IDX which holds the ID of the target block. The br instruction sets this variable to the target block's ID, breaks from the current if block, and allows the control flow to move forward. Each if block checks whether the BLOCK_PTR_IDX variable matches its assigned ID. If it does, execution proceeds within that block. The start of the global loop and the first if block is generated in tcg_out_tb_start. To properly close the blocks, this commit also introduces a new TCG backend callback tcg_out_tb_end which emits the "end" instructions for the final if block and the loop block in the Wasm backend. 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. In this callback, the mapping between label IDs and block IDs is recorded in LabelInfo, which is later used to resolve br instructions. Since the block ID for a label might not be known at the time a br instruction is generated, a placeholder (longer than 32bit and encoded as LEB128) is emitted instead. These placeholders are tracked in BlockPlaceholder and resolved later. Signed-off-by: Kohei Tokunaga --- 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/wasm32/tcg-target.c.inc | 180 +++++++++++++++++++++++++++++++ 12 files changed, 297 insertions(+) diff --git a/tcg/aarch64/tcg-target.c.inc b/tcg/aarch64/tcg-target.c.inc index 4cb647cb34..78ad3e913a 100644 --- a/tcg/aarch64/tcg-target.c.inc +++ b/tcg/aarch64/tcg-target.c.inc @@ -3518,6 +3518,17 @@ static void tcg_out_tb_start(TCGContext *s) tcg_out_bti(s, BTI_J); } =20 +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; diff --git a/tcg/arm/tcg-target.c.inc b/tcg/arm/tcg-target.c.inc index 447e43583e..2d911b1fe6 100644 --- a/tcg/arm/tcg-target.c.inc +++ b/tcg/arm/tcg-target.c.inc @@ -3441,6 +3441,17 @@ static void tcg_out_tb_start(TCGContext *s) /* nothing to do */ } =20 +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]; diff --git a/tcg/i386/tcg-target.c.inc b/tcg/i386/tcg-target.c.inc index 09fce27b06..2c7bad092f 100644 --- a/tcg/i386/tcg-target.c.inc +++ b/tcg/i386/tcg-target.c.inc @@ -4761,6 +4761,17 @@ static void tcg_out_tb_start(TCGContext *s) /* nothing to do */ } =20 +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); diff --git a/tcg/loongarch64/tcg-target.c.inc b/tcg/loongarch64/tcg-target.= c.inc index e5580d69a8..113c5df7fc 100644 --- a/tcg/loongarch64/tcg-target.c.inc +++ b/tcg/loongarch64/tcg-target.c.inc @@ -2658,6 +2658,17 @@ static void tcg_out_tb_start(TCGContext *s) /* nothing to do */ } =20 +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 =3D 0; i < count; ++i) { diff --git a/tcg/mips/tcg-target.c.inc b/tcg/mips/tcg-target.c.inc index 2c0457e588..965c4717c6 100644 --- a/tcg/mips/tcg-target.c.inc +++ b/tcg/mips/tcg-target.c.inc @@ -2745,6 +2745,17 @@ static void tcg_out_tb_start(TCGContext *s) /* nothing to do */ } =20 +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(); diff --git a/tcg/ppc/tcg-target.c.inc b/tcg/ppc/tcg-target.c.inc index 2e94778104..d0b1e46709 100644 --- a/tcg/ppc/tcg-target.c.inc +++ b/tcg/ppc/tcg-target.c.inc @@ -2859,6 +2859,17 @@ static void tcg_out_tb_start(TCGContext *s) } } =20 +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); diff --git a/tcg/riscv/tcg-target.c.inc b/tcg/riscv/tcg-target.c.inc index f9417d15f7..de76d9fa8d 100644 --- a/tcg/riscv/tcg-target.c.inc +++ b/tcg/riscv/tcg-target.c.inc @@ -2983,6 +2983,17 @@ static void tcg_out_tb_start(TCGContext *s) init_setting_vtype(s); } =20 +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; diff --git a/tcg/s390x/tcg-target.c.inc b/tcg/s390x/tcg-target.c.inc index 7ca0071f24..c4404c999c 100644 --- a/tcg/s390x/tcg-target.c.inc +++ b/tcg/s390x/tcg-target.c.inc @@ -3830,6 +3830,17 @@ static void tcg_out_tb_start(TCGContext *s) /* nothing to do */ } =20 +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)); diff --git a/tcg/sparc64/tcg-target.c.inc b/tcg/sparc64/tcg-target.c.inc index 9e004fb511..7f9b8e5aad 100644 --- a/tcg/sparc64/tcg-target.c.inc +++ b/tcg/sparc64/tcg-target.c.inc @@ -1017,6 +1017,17 @@ static void tcg_out_tb_start(TCGContext *s) /* nothing to do */ } =20 +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; diff --git a/tcg/tcg.c b/tcg/tcg.c index 2746458a64..778e84c40c 100644 --- a/tcg/tcg.c +++ b/tcg/tcg.c @@ -115,6 +115,7 @@ static void tcg_register_jit_int(const void *buf, size_= t size, =20 /* Forward declarations for functions declared and used in tcg-target.c.in= c. */ 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 arg= 1, intptr_t arg2); static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg ar= g); @@ -186,6 +187,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); =20 #ifndef CONFIG_USER_ONLY #define guest_base ({ qemu_build_not_reached(); (uintptr_t)0; }) @@ -360,6 +362,7 @@ static void tcg_out_label(TCGContext *s, TCGLabel *l) tcg_debug_assert(!l->has_value); l->has_value =3D 1; l->u.value_ptr =3D tcg_splitwx_to_rx(s->code_ptr); + tcg_out_label_cb(s, l); } =20 TCGLabel *gen_new_label(void) @@ -7045,6 +7048,10 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb= , uint64_t pc_start) if (!tcg_resolve_relocs(s)) { return -2; } + i =3D tcg_out_tb_end(s); + if (i < 0) { + return i; + } =20 #if !defined(CONFIG_TCG_INTERPRETER) && !defined(EMSCRIPTEN) /* flush instruction cache */ diff --git a/tcg/tci/tcg-target.c.inc b/tcg/tci/tcg-target.c.inc index 35c66a4836..d99d06c1da 100644 --- a/tcg/tci/tcg-target.c.inc +++ b/tcg/tci/tcg-target.c.inc @@ -1301,6 +1301,17 @@ static void tcg_out_tb_start(TCGContext *s) /* nothing to do */ } =20 +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; diff --git a/tcg/wasm32/tcg-target.c.inc b/tcg/wasm32/tcg-target.c.inc index 167850ea7c..ea0d1ca874 100644 --- a/tcg/wasm32/tcg-target.c.inc +++ b/tcg/wasm32/tcg-target.c.inc @@ -123,6 +123,11 @@ static const uint8_t tcg_target_reg_index[TCG_TARGET_N= B_REGS] =3D { */ #define CARRY_IDX 16 =20 +/* + * Global variable Index used for storing the current block index + */ +#define BLOCK_PTR_IDX 17 + /* Temporary local variables */ #define TMP32_LOCAL_0_IDX 1 #define TMP32_LOCAL_1_IDX 2 @@ -341,6 +346,16 @@ static void tcg_wasm_out_op_i64_eqz(TCGContext *s) { tcg_wasm_out8(s, 0x50); } +static void tcg_wasm_out_op_br(TCGContext *s, int i) +{ + tcg_wasm_out8(s, 0x0c); + tcg_wasm_out8(s, i); +} +static void tcg_wasm_out_op_loop_noret(TCGContext *s) +{ + tcg_wasm_out8(s, 0x03); + tcg_wasm_out8(s, 0x40); +} static void tcg_wasm_out_op_if_noret(TCGContext *s) { tcg_wasm_out8(s, 0x04); @@ -1357,6 +1372,152 @@ static void tcg_wasm_out_div_u( } } =20 +typedef struct LabelInfo { + struct LabelInfo *next; + int label; + int block; +} LabelInfo; + +__thread LabelInfo *label_info; + +static void init_label_info(void) +{ + label_info =3D NULL; +} + +static void add_label(int label, int block) +{ + LabelInfo *e =3D tcg_malloc(sizeof(LabelInfo)); + e->label =3D label; + e->block =3D block; + e->next =3D NULL; + if (label_info =3D=3D NULL) { + label_info =3D e; + return; + } + LabelInfo *last =3D label_info; + for (LabelInfo *p =3D last; p; p =3D p->next) { + last =3D p; + } + last->next =3D e; +} + +typedef struct BlockPlaceholder { + struct BlockPlaceholder *next; + int label; + int pos; +} BlockPlaceholder; + +__thread BlockPlaceholder *block_placeholder; + +__thread int block_idx; + +static void init_blocks(void) +{ + block_placeholder =3D NULL; + block_idx =3D 0; +} + +static void add_block_placeholder(int label, int pos) +{ + BlockPlaceholder *e =3D tcg_malloc(sizeof(BlockPlaceholder)); + e->label =3D label; + e->pos =3D pos; + e->next =3D NULL; + if (block_placeholder =3D=3D NULL) { + block_placeholder =3D e; + return; + } + BlockPlaceholder *last =3D block_placeholder; + for (BlockPlaceholder *p =3D last; p; p =3D p->next) { + last =3D p; + } + last->next =3D e; +} + +static int get_block_of_label(int label) +{ + for (LabelInfo *p =3D label_info; p; p =3D p->next) { + if (p->label =3D=3D label) { + return p->block; + } + } + return -1; +} + +static void tcg_wasm_out_new_block(TCGContext *s) +{ + tcg_wasm_out_op_end(s); /* close this block */ + + /* next block */ + tcg_wasm_out_op_global_get(s, BLOCK_PTR_IDX); + tcg_wasm_out_op_i64_const(s, ++block_idx); + tcg_wasm_out_op_i64_le_u(s); + tcg_wasm_out_op_if_noret(s); +} + +static void tcg_out_label_cb(TCGContext *s, TCGLabel *l) +{ + add_label(l->id, block_idx + 1); + tcg_wasm_out_new_block(s); +} + +static void tcg_wasm_out_op_br_to_label(TCGContext *s, TCGLabel *l, bool b= r_if) +{ + int toploop_depth =3D 1; + if (br_if) { + tcg_wasm_out_op_if_noret(s); + toploop_depth++; + } + tcg_wasm_out8(s, 0x42); /* i64.const */ + + add_block_placeholder(l->id, sub_buf_len()); + + tcg_wasm_out8(s, 0x80); /* filled before instantiation */ + tcg_wasm_out8(s, 0x80); + tcg_wasm_out8(s, 0x80); + tcg_wasm_out8(s, 0x80); + tcg_wasm_out8(s, 0x00); + tcg_wasm_out_op_global_set(s, BLOCK_PTR_IDX); + if (get_block_of_label(l->id) !=3D -1) { + /* + * The label is placed before this br, branch to the top of loop + */ + tcg_wasm_out_op_br(s, toploop_depth); + } else { + /* + * The label will be generated after this br, + * branch to the end of the current block + */ + tcg_wasm_out_op_br(s, toploop_depth - 1); + } + if (br_if) { + tcg_wasm_out_op_end(s); + } +} + +static void tcg_wasm_out_br(TCGContext *s, TCGLabel *l) +{ + tcg_wasm_out_op_br_to_label(s, l, false); +} + +static void tcg_wasm_out_brcond(TCGContext *s, TCGType type, + TCGReg arg1, TCGReg arg2, + TCGCond cond, TCGLabel *l) +{ + switch (type) { + case TCG_TYPE_I32: + tcg_wasm_out_op_cond_i32(s, cond, arg1, arg2); + break; + case TCG_TYPE_I64: + tcg_wasm_out_op_cond_i64(s, cond, arg1, arg2); + break; + default: + g_assert_not_reached(); + } + tcg_wasm_out_op_br_to_label(s, l, true); +} + static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, intptr_t value, intptr_t addend) { @@ -2512,6 +2673,7 @@ static void tgen_brcond(TCGContext *s, TCGType type, = TCGCond cond, { tgen_setcond_tci(s, type, cond, TCG_REG_TMP, arg0, arg1); tcg_out_op_rl(s, INDEX_op_brcond, TCG_REG_TMP, l); + tcg_wasm_out_brcond(s, type, arg0, arg1, cond, l); } =20 static const TCGOutOpBrcond outop_brcond =3D { @@ -2576,6 +2738,7 @@ static void tcg_out_mb(TCGContext *s, unsigned a0) static void tcg_out_br(TCGContext *s, TCGLabel *l) { tcg_out_op_l(s, INDEX_op_br, l); + tcg_wasm_out_br(s, l); } =20 static void tgen_ld8u(TCGContext *s, TCGType type, TCGReg dest, @@ -2796,6 +2959,23 @@ static inline void tcg_target_qemu_prologue(TCGConte= xt *s) static void tcg_out_tb_start(TCGContext *s) { init_sub_buf(); + init_blocks(); + init_label_info(); + + tcg_wasm_out_op_loop_noret(s); + tcg_wasm_out_op_global_get(s, BLOCK_PTR_IDX); + tcg_wasm_out_op_i64_eqz(s); + tcg_wasm_out_op_if_noret(s); +} + +static int tcg_out_tb_end(TCGContext *s) +{ + tcg_wasm_out_op_end(s); /* end if */ + tcg_wasm_out_op_end(s); /* end loop */ + tcg_wasm_out8(s, 0x0); /* unreachable */ + tcg_wasm_out_op_end(s); /* end func */ + + return 0; } =20 bool tcg_target_has_memory_bswap(MemOp memop) --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747746413; cv=none; d=zohomail.com; s=zohoarc; b=eb53dJ36ciJP1zH1Kva7HzZ5HdQUxkOSI/XwzcrlODAJK2vZk9NAHh1/OD9C/JapJDnpVT4os0gTuos0z2qnXiw1PHCcc3s342WwcFfGd8cJxISbkjB0Ej74l+mVhVTSBakOLSRfP7gTr87uU7lVmH46BnON0ve7TkRvGXsSTJ4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747746413; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=cDG0pwUWpFnKg/l7uQqlMEQTPTDfS039RvtWFwYIZBA=; b=ULC19h04Z8rprJiiRfPBEPTHs9sVkJEhLtoRJ/RWp0dZjm5/hUuFQJfD3UuSeIYQl07qgiAR2yyyU4839RGklgWf9VPIp6KcVz8l4RaLUaLVqOZutkTzeCfN7z2LMeUk7J6QFDt2wL7uxkLArxXyG6rP0ID9dN9er/YXun3YhSY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747746413575263.5797058934479; Tue, 20 May 2025 06:06:53 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMUj-0003Rf-IM; Tue, 20 May 2025 08:54:58 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMUH-0002OR-9q; Tue, 20 May 2025 08:54:32 -0400 Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMUE-0000zS-W2; Tue, 20 May 2025 08:54:29 -0400 Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-23229fdaff4so29806875ad.1; Tue, 20 May 2025 05:54:22 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.54.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:54:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745660; x=1748350460; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=cDG0pwUWpFnKg/l7uQqlMEQTPTDfS039RvtWFwYIZBA=; b=QNwefYrTCo4Q9vrUasKatJr34MtZdVx0Mpms1c7GT/jtgMnF4yw1vhVHmZb6Abzxjk 5HTpwNZzr4L2pqFV908xBzKss1OLlqixlOpb1woa9uam3h4cJIZjDDHyAwwflIsmzuUm 61rK9/GgZOya+XdC5RIFNoBcStXcKPdE8cmwcM7FZ7PmWNfWNptNLu2c29MYqwKg4niN 4vLIUUZ9gxruQ/6Yu98c8M+kx2NpK90rF19zbL8Y6shvSHhIYB9NhBGDH2v9O16RCR4v pODfSICmjJ5JsKtN0jOGfXUZbJwhqAfRLoPJofWvTF/x5w6wt1FT+2Xnl4Ev98yCqXdq zADg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745660; x=1748350460; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=cDG0pwUWpFnKg/l7uQqlMEQTPTDfS039RvtWFwYIZBA=; b=goWktzt2YjOlq/gFk2T+pNK29J01xhZhF1nUMQ+mu72uaZ0CNGmO+K0IDKbZ7jS4pO QDHaytiVmATNYcOXmzwSS3pCpRIvqwD2mb0tU8yXa6Zi9tPglxHwfYknOxQU/9itCxAz 9jlq295D/0/lxvRb/1Fwl56LN0XbeToIXVCOmpTM0vGzIWCvJ7odLyBQkwVm7/Eg3bLk jlQ4BHcj95Tu0VdJ2PW+v2+jYUGZ2ML5eG2+su59P80TKpEdaxmsQFPOzFqWagJFu7mw K1YEVB1i1uC44lC4+ztstkIgQLohpGrKoUmNFTetaH5HYWR4C+o4cFElQ4L+Zm0oTwxe Z70w== X-Forwarded-Encrypted: i=1; AJvYcCVvTGkYYMpZlGiFT+Mh90ViZDDeP+jwLKotaVAtxER+IdZgVcAdoeRlejleTD0sI1zUADMivWiM4m+AlQ==@nongnu.org, AJvYcCWGJ0H+64r49n5KbgkASVWsrdi1dkBSsg6Eazpj+dpEemh1bUnOOgMMKHzBIFcdaTJoLgjlo0IcOw==@nongnu.org X-Gm-Message-State: AOJu0YykACIgSbpbCZkBMIEFZc89BWJc5lJOBlsfu+UcC9mRJodN7UMB KdB5TbH8a+aGvfifh5gyOsAIjaYM2n6H6mAuKWNZOSuwwXaADMCF/HOvG76A+Jg5 X-Gm-Gg: ASbGnctdAlK1uT9Esi1YB25qsuq0ahAyEtRMz1kXoGdIN+P7WEg70LaikAFFke4oE73 arkvJUM0dnLws5LrYTP5a1WgaVfmDY5lK2buDNCxSNHeCUhAnTbNDYZEJAE+A8knL6EpTNrywIx buo68VbdyACHvjdkPPctcZ8aCs9ZZZXKO4r/8Qww5N5S/JH34Wgi9gCjeREF25ZvH5j4lbMNE/U 04HMwfeDENPJ/fZ4xwWiQ0kLa4zaBiC/e1cY7zFxvfFIThRwQAYA+UZmsDOAH4yZnuMEVD3UOEM eAb8rUe+e/C5YjSS/7ho6B2gARx9Bb5W2fe62L4p+BKK+wqeiKU= X-Google-Smtp-Source: AGHT+IHsAaRA9611AxQxc5NVjTREuknnJ6IQooXldl0fBT1ED1f+IK7+hrgUEmMdzmSvtkHTD8mfWg== X-Received: by 2002:a17:90b:2c8c:b0:30e:823f:ef31 with SMTP id 98e67ed59e1d1-30e823ff071mr24002616a91.29.1747745648991; Tue, 20 May 2025 05:54:08 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 20/33] tcg/wasm32: Add exit_tb/goto_tb/goto_ptr instructions Date: Tue, 20 May 2025 21:51:22 +0900 Message-ID: <021c0cb8cc58c4cb673b38b75d20d27e8dec1c40.1747744132.git.ktokunaga.mail@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::631; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pl1-x631.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747746415115116600 Content-Type: text/plain; charset="utf-8" In 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 which is accessible as the local variable at index 0. 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. Signed-off-by: Kohei Tokunaga --- MAINTAINERS | 1 + tcg/wasm32.h | 17 ++++++ tcg/wasm32/tcg-target.c.inc | 111 ++++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 tcg/wasm32.h diff --git a/MAINTAINERS b/MAINTAINERS index ac5070d058..3ca93f90de 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -3953,6 +3953,7 @@ M: Kohei Tokunaga S: Maintained F: tcg/wasm32/ F: tcg/wasm32.c +F: tcg/wasm32.h =20 Block drivers ------------- diff --git a/tcg/wasm32.h b/tcg/wasm32.h new file mode 100644 index 0000000000..ffa359b7dc --- /dev/null +++ b/tcg/wasm32.h @@ -0,0 +1,17 @@ +/* + * SPDX-License-Identifier: GPL-2.0-or-later + */ +#ifndef TCG_WASM32_H +#define TCG_WASM32_H + +/* + * wasmContext is a data shared among QEMU and wasm modules. + */ +struct wasmContext { + /* + * Pointer to the TB to be executed. + */ + void *tb_ptr; +}; + +#endif diff --git a/tcg/wasm32/tcg-target.c.inc b/tcg/wasm32/tcg-target.c.inc index ea0d1ca874..77db50cf85 100644 --- a/tcg/wasm32/tcg-target.c.inc +++ b/tcg/wasm32/tcg-target.c.inc @@ -25,6 +25,8 @@ * THE SOFTWARE. */ =20 +#include "../wasm32.h" + /* Used for function call generation. */ #define TCG_TARGET_CALL_STACK_OFFSET 0 #define TCG_TARGET_STACK_ALIGN 8 @@ -128,6 +130,11 @@ static const uint8_t tcg_target_reg_index[TCG_TARGET_N= B_REGS] =3D { */ #define BLOCK_PTR_IDX 17 =20 +/* + * pointer to wasmContext + */ +#define CTX_IDX 0 + /* Temporary local variables */ #define TMP32_LOCAL_0_IDX 1 #define TMP32_LOCAL_1_IDX 2 @@ -334,6 +341,14 @@ static void tcg_wasm_out_op_i32_eqz(TCGContext *s) { tcg_wasm_out8(s, 0x45); } +static void tcg_wasm_out_op_i32_eq(TCGContext *s) +{ + tcg_wasm_out8(s, 0x46); +} +static void tcg_wasm_out_op_i32_ne(TCGContext *s) +{ + tcg_wasm_out8(s, 0x47); +} static void tcg_wasm_out_op_i64_lt_u(TCGContext *s) { tcg_wasm_out8(s, 0x54); @@ -380,6 +395,10 @@ static void tcg_wasm_out_op_end(TCGContext *s) { tcg_wasm_out8(s, 0x0b); } +static void tcg_wasm_out_op_return(TCGContext *s) +{ + tcg_wasm_out8(s, 0x0f); +} static void tcg_wasm_out_op_var(TCGContext *s, uint8_t instr, uint8_t i) { tcg_wasm_out8(s, instr); @@ -590,6 +609,16 @@ static void tcg_wasm_out_op_i64_load32_u(TCGContext *s= , uint32_t a, uint32_t o) tcg_wasm_out_op_loadstore(s, 0x35, a, o); } =20 +static void tcg_wasm_out_op_i32_load(TCGContext *s, uint32_t a, uint32_t o) +{ + tcg_wasm_out_op_loadstore(s, 0x28, a, o); +} + +static void tcg_wasm_out_op_i32_store(TCGContext *s, uint32_t a, uint32_t = o) +{ + tcg_wasm_out_op_loadstore(s, 0x36, a, o); +} + static void tcg_wasm_out_op_not(TCGContext *s) { tcg_wasm_out_op_i64_const(s, -1); @@ -1518,6 +1547,85 @@ static void tcg_wasm_out_brcond(TCGContext *s, TCGTy= pe type, tcg_wasm_out_op_br_to_label(s, l, true); } =20 +#define tcg_wasm_out_ctx_i32_store_const(s, f, v) \ + do { \ + tcg_wasm_out_op_local_get(s, CTX_IDX); \ + tcg_wasm_out_op_i32_const(s, v); \ + tcg_wasm_out_op_i32_store(s, 0, offsetof(struct wasmContext, f)); \ + } while (0) + +#define tcg_wasm_out_ctx_i32_store_r(s, f, r) \ + do { \ + tcg_wasm_out_op_local_get(s, CTX_IDX); \ + tcg_wasm_out_op_global_get_r(s, r); \ + tcg_wasm_out_op_i32_wrap_i64(s); \ + tcg_wasm_out_op_i32_store(s, 0, offsetof(struct wasmContext, f)); \ + } while (0) + +#define tcg_wasm_out_ctx_i32_store_local32(s, f, var) \ + do { \ + tcg_wasm_out_op_local_get(s, CTX_IDX); \ + tcg_wasm_out_op_local_get(s, var); \ + tcg_wasm_out_op_i32_store(s, 0, offsetof(struct wasmContext, f)); \ + } while (0) + +#define tcg_wasm_out_ctx_i32_load(s, f) \ + do { \ + tcg_wasm_out_op_local_get(s, CTX_IDX); \ + tcg_wasm_out_op_i32_load(s, 0, offsetof(struct wasmContext, f)); \ + } while (0) + +static void tcg_wasm_out_exit_tb(TCGContext *s, uintptr_t arg) +{ + tcg_wasm_out_ctx_i32_store_const(s, tb_ptr, 0); + tcg_wasm_out_op_i32_const(s, (int32_t)arg); + tcg_wasm_out_op_return(s); +} + +static void tcg_wasm_out_goto_ptr(TCGContext *s, TCGReg arg) +{ + tcg_wasm_out_op_global_get_r(s, arg); + tcg_wasm_out_op_i32_wrap_i64(s); + tcg_wasm_out_ctx_i32_load(s, tb_ptr); + tcg_wasm_out_op_i32_eq(s); + tcg_wasm_out_op_if_noret(s); + tcg_wasm_out_op_i64_const(s, 0); + tcg_wasm_out_op_global_set(s, BLOCK_PTR_IDX); + tcg_wasm_out_op_br(s, 2); /* br to the top of loop */ + tcg_wasm_out_op_end(s); + + tcg_wasm_out_ctx_i32_store_r(s, tb_ptr, arg); + tcg_wasm_out_op_i32_const(s, 0); + tcg_wasm_out_op_return(s); +} + +static void tcg_wasm_out_goto_tb( + TCGContext *s, int which, uint32_t cur_reset_ptr) +{ + tcg_wasm_out_op_i32_const(s, (int32_t)get_jmp_target_addr(s, which)); + tcg_wasm_out_op_i32_load(s, 0, 0); + tcg_wasm_out_op_local_set(s, TMP32_LOCAL_0_IDX); + + tcg_wasm_out_op_local_get(s, TMP32_LOCAL_0_IDX); + tcg_wasm_out_op_i32_const(s, cur_reset_ptr); + tcg_wasm_out_op_i32_ne(s); + tcg_wasm_out_op_if_noret(s); + + tcg_wasm_out_op_local_get(s, TMP32_LOCAL_0_IDX); + tcg_wasm_out_ctx_i32_load(s, tb_ptr); + tcg_wasm_out_op_i32_eq(s); + tcg_wasm_out_op_if_noret(s); + tcg_wasm_out_op_i64_const(s, 0); + tcg_wasm_out_op_global_set(s, BLOCK_PTR_IDX); + tcg_wasm_out_op_br(s, 3); /* br to the top of loop */ + tcg_wasm_out_op_end(s); + + tcg_wasm_out_ctx_i32_store_local32(s, tb_ptr, TMP32_LOCAL_0_IDX); + tcg_wasm_out_op_i32_const(s, 0); + tcg_wasm_out_op_return(s); + tcg_wasm_out_op_end(s); +} + static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, intptr_t value, intptr_t addend) { @@ -1931,6 +2039,7 @@ static void tcg_out_call(TCGContext *s, const tcg_ins= n_unit *func, static void tcg_out_exit_tb(TCGContext *s, uintptr_t arg) { tcg_out_op_p(s, INDEX_op_exit_tb, (void *)arg); + tcg_wasm_out_exit_tb(s, arg); } =20 static void tcg_out_goto_tb(TCGContext *s, int which) @@ -1938,11 +2047,13 @@ static void tcg_out_goto_tb(TCGContext *s, int whic= h) /* indirect jump method. */ tcg_out_op_p(s, INDEX_op_goto_tb, (void *)get_jmp_target_addr(s, which= )); set_jmp_reset_offset(s, which); + tcg_wasm_out_goto_tb(s, which, (uint32_t)s->code_ptr); } =20 static void tcg_out_goto_ptr(TCGContext *s, TCGReg a0) { tcg_out_op_r(s, INDEX_op_goto_ptr, a0); + tcg_wasm_out_goto_ptr(s, a0); } =20 void tb_target_set_jmp_target(const TranslationBlock *tb, int n, --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747746595; cv=none; d=zohomail.com; s=zohoarc; b=l9d4UEogMrC1X0B7YbI43a5azGL71iVGn76sBDi1RZLM2ez9iD27xqPjAVosZv6qLBuPXilo3iGZjX1otgubt0w68uXo2k7yjjS8V7F4H8Auq3He3xKY0w1o5uiQzY70c8FD1dcPfOmhhP2kParmrIHB5X8y0o5/4tp1rivPRHI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747746595; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=b7+9niXXB1Fhurf42nBGF3zTCxGh2saaYSnPS8Sr56Q=; b=PhdU/ATX1RauTx9iZCE1khAfvjjPvHEvRTqmVJuIOrPcADMdz6eFk11F0b8C2J1eyeRu0NLZHcNmbTFa6St9YoBGnHXr+YaMwd/fu62mXk94HvyO5UX0O/f3GwlMQgSXiqzb3lSe8EIIbBF5dTqE5wbcNPgv+MYkPBBb4kV9WzY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747746595743553.7716287558229; Tue, 20 May 2025 06:09:55 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMUr-0003wi-KA; Tue, 20 May 2025 08:55:06 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMUJ-0002P4-OX; Tue, 20 May 2025 08:54:34 -0400 Received: from mail-pf1-x433.google.com ([2607:f8b0:4864:20::433]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMUA-0000yq-Op; Tue, 20 May 2025 08:54:29 -0400 Received: by mail-pf1-x433.google.com with SMTP id d2e1a72fcca58-7399a2dc13fso7049939b3a.2; Tue, 20 May 2025 05:54:18 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.54.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:54:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745655; x=1748350455; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=b7+9niXXB1Fhurf42nBGF3zTCxGh2saaYSnPS8Sr56Q=; b=bY8tgUjknmdX+GOSZ8eZ7qg7aRq7RLrXFoPJ0yGhXLo8cMTX/15z5sK6sXbCy+EX9+ l/I02ZkYalwayZq8YQZslG4FhNh+PE/ub0yNzT4Wbn+fx8oKMsmY0j26gEHnEkg8Cd0O 8ULFqehMeTSVlzdrcboZH11IHQrXBAAgYRMSPibunEZ+L3FzuBnmqug6gIfDX6MA7N2A j/o7bJXfu3EOGI2Ru/iifrGdcFzn/86oOJhJD6KjMnvgmoqi/pAG6ienUjx2AUpoGLNV RdCtP/VnpafIvrhPTNNSVAJ2HpBtpZZfiy2LneE+/E4Q0FIl9FHgyIA6oKF7mIZrcy/o K4iA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745655; x=1748350455; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=b7+9niXXB1Fhurf42nBGF3zTCxGh2saaYSnPS8Sr56Q=; b=j9fn4id0v8qDHRI18x9cU2QbMoatN5m+SVp3zb4D1REmT72zwJfRl8dJEtq17qzqDL zCG5+Aj66zhqdo8PQq2Q0GkMfHNp9Ye4c9Jyj/hODsuyRzWmbg96w8EgzpFATyIF+Sl/ g8wMAAukc/71OXkdz7tlrQ2sjoYD9YthDWzuWXP+W1WSLWgNaVo/aSUq7VAaoK0S3Ck5 vaQv8WW1Ab6bG/jVk67qdelNzPNAv7Hc4jUtu6yAqD/1+fmgC5/XITUoTMze8ze678Kn lg9y5e8PGZHrPtaJLxBubqF85piMx1k7SwH0Fjdd8VJQpOGwQ48diwfevUHwU2DFD5jG x0eA== X-Forwarded-Encrypted: i=1; AJvYcCVzlUJXggNrYYCid3r4SLpqSS6SQuMmPqX88F96HfQCsEOvvbSWTTISr3CUPe/4yhmy4qkG+EwbJWsv+A==@nongnu.org, AJvYcCWzo2uI0tilYgUyulmNf96BKO+UZZrDHtZqEQNb1L4yJl1XPw2lH6/SHgZohg2pFA3uipuJFyA/cw==@nongnu.org X-Gm-Message-State: AOJu0Yw+3y1198UNHAGhsLm4Kj+9Ll37GXzgvANvFNV0fW1JpMgCsNRf vl/1WKleayu8rhxv6buvDTQ76G84w19H6g81tQ8kAXWEz3jJEv2i0HAT9Fr0C6fU X-Gm-Gg: ASbGncswCTHDsWFMhMe7Bgqsp1+ZYmJQh5j9MAlY0rDTZzw9wh9Xgg0d64Hb4qxg4Cf IvNh5EzABTAmG9c9ewy6q4qQWlh/AvtRw46XoftRQpcJ60LW1K8gEo8LGnNWtdB4+SgCb2DaaPC QOKQBqb4n/Yk/RwOVeeetW2EqY6AWKepoywU3CiqosrzwsvxvzcGd7ewtkrihqj54jReBhgEuJm uykkbC6wL/nP8GVQCMcs56iLVcJ0XMse9TbJ/mvTVKvfVMXwuP8oLXDDBeYFUyt7RFeAW4jnvaC oew1MxdR0Crqia07oA+JEYsYi24MMBuui4W36B9FCSM9rBuV90W+9CmV7ESw5g== X-Google-Smtp-Source: AGHT+IFPlqRQ1YPOgXYMn8sylaCHdOyEFt8VqLuMxdXEvsU5rL1gozNwHMX/DB8mCdVHm0qWPQn8Vw== X-Received: by 2002:a17:90b:33c2:b0:2ff:58b8:5c46 with SMTP id 98e67ed59e1d1-30e830f7b18mr25403623a91.8.1747745654522; Tue, 20 May 2025 05:54:14 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 21/33] tcg/wasm32: Add call instruction Date: Tue, 20 May 2025 21:51:23 +0900 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::433; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pf1-x433.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747746597916116600 Content-Type: text/plain; charset="utf-8" To 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 a unique ID (starting from 0) to the target function. The mapping between the function pointer and its assigned ID is recorded in the HelperInfo structure. 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 stack before the call. After the function returns, the result is retrieved from the stack and set in the corresponding TCG variable. In our Emscripten build configuration with !has_int128_type, a 128-bit value is represented by the Int128 struct. These values are passed indirectly via pointer parameters and returned via a prepended pointer argument, as described in [1]. [1] https://github.com/WebAssembly/tool-conventions/blob/060cf4073e46931160= c2e9ecd43177ee1fe93866/BasicCABI.md#function-arguments-and-return-values Signed-off-by: Kohei Tokunaga --- tcg/wasm32.h | 10 +++ tcg/wasm32/tcg-target.c.inc | 170 ++++++++++++++++++++++++++++++++++++ 2 files changed, 180 insertions(+) diff --git a/tcg/wasm32.h b/tcg/wasm32.h index ffa359b7dc..1944249891 100644 --- a/tcg/wasm32.h +++ b/tcg/wasm32.h @@ -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; }; =20 #endif diff --git a/tcg/wasm32/tcg-target.c.inc b/tcg/wasm32/tcg-target.c.inc index 77db50cf85..708af1fbb6 100644 --- a/tcg/wasm32/tcg-target.c.inc +++ b/tcg/wasm32/tcg-target.c.inc @@ -140,6 +140,9 @@ static const uint8_t tcg_target_reg_index[TCG_TARGET_NB= _REGS] =3D { #define TMP32_LOCAL_1_IDX 2 #define TMP64_LOCAL_0_IDX 3 =20 +/* function index */ +#define HELPER_IDX_START 0 /* helper funcitons */ + #define BUF_SIZE 1024 typedef struct LinkedBuf { struct LinkedBuf *next; @@ -619,6 +622,12 @@ static void tcg_wasm_out_op_i32_store(TCGContext *s, u= int32_t a, uint32_t o) tcg_wasm_out_op_loadstore(s, 0x36, a, o); } =20 +static void tcg_wasm_out_op_call(TCGContext *s, uint32_t func_idx) +{ + tcg_wasm_out8(s, 0x10); + tcg_wasm_out_leb128_uint32_t(s, func_idx); +} + static void tcg_wasm_out_op_not(TCGContext *s) { tcg_wasm_out_op_i64_const(s, -1); @@ -1626,6 +1635,165 @@ static void tcg_wasm_out_goto_tb( tcg_wasm_out_op_end(s); } =20 +static void push_arg_i64(TCGContext *s, int *stack_offset) +{ + tcg_wasm_out_op_global_get_r(s, TCG_REG_CALL_STACK); + tcg_wasm_out_op_i32_wrap_i64(s); + tcg_wasm_out_op_i64_load(s, 0, *stack_offset); + *stack_offset =3D *stack_offset + 8; +} + +static void gen_call(TCGContext *s, const TCGHelperInfo *info, int func_id= x) +{ + unsigned typemask =3D info->typemask; + int rettype =3D typemask & 7; + int stack_offset =3D 0; + + if (rettype =3D=3D dh_typecode_i128) { + /* receive 128bit return value via the buffer */ + tcg_wasm_out_ctx_i32_load(s, buf128); + } + + for (typemask >>=3D 3; typemask; typemask >>=3D 3) { + int typecode =3D typemask & 7; + if (typecode =3D=3D dh_typecode_void) { + continue; + } + switch (typecode) { + case dh_typecode_i32: + case dh_typecode_s32: + case dh_typecode_ptr: + push_arg_i64(s, &stack_offset); + tcg_wasm_out_op_i32_wrap_i64(s); + break; + case dh_typecode_i64: + case dh_typecode_s64: + push_arg_i64(s, &stack_offset); + break; + case dh_typecode_i128: + tcg_wasm_out_op_global_get_r(s, TCG_REG_CALL_STACK); + tcg_wasm_out_op_i32_wrap_i64(s); + tcg_wasm_out_op_i32_const(s, stack_offset); + tcg_wasm_out_op_i32_add(s); + stack_offset +=3D 16; + break; + default: + g_assert_not_reached(); + } + } + + tcg_wasm_out_op_call(s, func_idx); + + if (rettype !=3D dh_typecode_void) { + switch (rettype) { + case dh_typecode_i32: + case dh_typecode_s32: + case dh_typecode_ptr: + tcg_wasm_out_op_i64_extend_i32_s(s); + tcg_wasm_out_op_global_set_r(s, TCG_REG_R0); + break; + case dh_typecode_i64: + case dh_typecode_s64: + tcg_wasm_out_op_global_set_r(s, TCG_REG_R0); + break; + case dh_typecode_i128: + tcg_wasm_out_ctx_i32_load(s, buf128); + tcg_wasm_out_op_i64_load(s, 0, 0); + tcg_wasm_out_op_global_set_r(s, TCG_REG_R0); + tcg_wasm_out_ctx_i32_load(s, buf128); + tcg_wasm_out_op_i64_load(s, 0, 8); + tcg_wasm_out_op_global_set_r(s, TCG_REG_R1); + break; + default: + g_assert_not_reached(); + } + } + + return; +} + +typedef struct HelperInfo { + struct HelperInfo *next; + uint32_t idx_on_qemu; +} HelperInfo; + +__thread HelperInfo *helpers; + +static void init_helpers(void) +{ + helpers =3D NULL; +} + +static int register_helper(TCGContext *s, int helper_idx_on_qemu) +{ + int idx =3D HELPER_IDX_START; + + tcg_debug_assert(helper_idx_on_qemu >=3D 0); + + HelperInfo *e =3D tcg_malloc(sizeof(HelperInfo)); + e->idx_on_qemu =3D helper_idx_on_qemu; + e->next =3D NULL; + if (helpers =3D=3D NULL) { + helpers =3D e; + return idx; + } + HelperInfo *last =3D helpers; + for (HelperInfo *p =3D last; p; p =3D p->next) { + last =3D p; + idx++; + } + last->next =3D e; + return idx; +} + +static int helpers_len(void) +{ + int n =3D 0; + for (HelperInfo *p =3D helpers; p; p =3D p->next) { + n++; + } + return n; +} + +static inline int helpers_copy(uint32_t *dst) +{ + void *start =3D dst; + for (HelperInfo *p =3D helpers; p; p =3D p->next) { + *dst++ =3D p->idx_on_qemu; + } + return (int)dst - (int)start; +} + + +static int get_helper_idx(TCGContext *s, int helper_idx_on_qemu) +{ + int idx =3D HELPER_IDX_START; + + for (HelperInfo *p =3D helpers; p; p =3D p->next) { + if (p->idx_on_qemu =3D=3D helper_idx_on_qemu) { + return idx; + } + idx++; + } + return -1; +} + +static void tcg_wasm_out_call(TCGContext *s, int func, + const TCGHelperInfo *info) +{ + int func_idx =3D get_helper_idx(s, (int)func); + if (func_idx < 0) { + func_idx =3D register_helper(s, (int)func); + } + + tcg_wasm_out_ctx_i32_load(s, tci_tb_ptr); + tcg_wasm_out_op_i32_const(s, (int32_t)s->code_ptr); + tcg_wasm_out_op_i32_store(s, 0, 0); + + gen_call(s, info, func_idx); +} + + static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, intptr_t value, intptr_t addend) { @@ -2034,6 +2202,7 @@ static void tcg_out_call(TCGContext *s, const tcg_ins= n_unit *func, insn =3D deposit32(insn, 0, 8, INDEX_op_call); insn =3D deposit32(insn, 8, 4, which); tcg_out32(s, insn); + tcg_wasm_out_call(s, (int)func, info); } =20 static void tcg_out_exit_tb(TCGContext *s, uintptr_t arg) @@ -3072,6 +3241,7 @@ static void tcg_out_tb_start(TCGContext *s) init_sub_buf(); init_blocks(); init_label_info(); + init_helpers(); =20 tcg_wasm_out_op_loop_noret(s); tcg_wasm_out_op_global_get(s, BLOCK_PTR_IDX); --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747746305; cv=none; d=zohomail.com; s=zohoarc; b=Ak0x0DCsPwpB6dFUXBmVodPG3dY8iOSfemya9X77oyJPnkVK8bBA0p4o8MtykjQZ/UUZgIkvffX7dUZrnnBDvjKL3abFnDAfbUH9qyxn+AT8tY6iLrdoe7k76ZZl2laW4mZHwmeIn+jJHUmQqOA2C+oVcC3dtUGT2nz4q1DXxZI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747746305; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=TW1RsXRiDX3fR+HjAtpX8n14C4CKemsei/P4rdx2cjg=; b=HpS6jwL8oXQKuhrOhj2NjMkaFehFg1vALFcDLVDyUHbJZLfacYQhZ7CSyZv3z0vvnvsVWSfxOcDnuvuEcCxYSRqxtaP7npIe5Q/tU3x530duoWqE0Jj11Zxs/fijCWOaq7V5KHUwAu8RkUJtFXsYYWSNY1IHme+W+T9LLid1iYg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747746304626428.15252266175185; Tue, 20 May 2025 06:05:04 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMUs-000429-VH; Tue, 20 May 2025 08:55:08 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMUQ-0002Tc-07; Tue, 20 May 2025 08:54:40 -0400 Received: from mail-pg1-x533.google.com ([2607:f8b0:4864:20::533]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMUM-000113-CF; Tue, 20 May 2025 08:54:37 -0400 Received: by mail-pg1-x533.google.com with SMTP id 41be03b00d2f7-b26f01c638fso3938626a12.1; Tue, 20 May 2025 05:54:32 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.54.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:54:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745671; x=1748350471; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=TW1RsXRiDX3fR+HjAtpX8n14C4CKemsei/P4rdx2cjg=; b=IbRbXF6CR07Yk3SkQebshaQYPAqXSF/x9IZw78JtJGs+sq2sLhg17nicQJ6zZDPkse /rqQVKSgKu1UhZCeSsCxIx/OVerLHzdoxiuv5rcQ1FW09imOC4wWlTNu25nt35NEmTPu WkMrW6HUVyOW4+MC7wlicfwyp/25JplvfNRATQQXSwVS1KeBxJf3C6fOz70UcLEW993Q cyo5IsrXTNnZRo9OlRA+Jn/MSVdyE9PhQN4hcheLqykuAsEENMxGuladKGRzhprEgr/B 8/CEFvKS1kR7ksxUeSXsfgMl7wbiKQ01f85yC2VhvpGIC2d8I/9/6K3UG+yHvhLkJE9c nAag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745671; x=1748350471; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TW1RsXRiDX3fR+HjAtpX8n14C4CKemsei/P4rdx2cjg=; b=IXeh8G9NYcVowzkIWSXRCYVH5qfTITJgZ4CqpUy0TcknIAgqLex0yD3eDFN/+Lu0hj FGa5uuqj/rrCYwjMv6o5JwG+xfutW+1jVDHZbzaUGsMM80ld28NF4maSc8K06kRgKtf4 hh7I5DsBQSwW3KqhL/I0dqHrt2WkJKNIwSqQEKn2XDvVK0QSJcysJBEMNjjjXDbsYGGX Mns3hfSpRh05YTg1jQsTIGeZWO/IGnqXj4iBPqGHljcbOGYEkQWX1WBqWsSAW7CgV/pj 2b0B2n0nzSEcA5nik+odBL5KsH5ZMJRKAYAwlx1UQqr2/e2Yh1X1x2RY7EgBF3vlKReM 6fqg== X-Forwarded-Encrypted: i=1; AJvYcCUIf9TbFojvmeL3gz7ISwbIhEotpg0ERuwld2nHELocBpvuHJpRo6V8tCPYJJ9e6WsNqV6emXX6NA==@nongnu.org, AJvYcCX4moMwlwETCPatVHsw1cWa6+2cqjYdLLUBzANOW9e1CJod74ld/SWY21vz7bn/ewoE4xUyWtc+KbdJbA==@nongnu.org X-Gm-Message-State: AOJu0YwFNBMRNZYIhQiR2QTGP2zrbeAIbIPKDSH0ZDbMMPU8hD2oMY8V zlpcd/Twka/M7Ev7RYt+FBcPZp3+AlQEWsYMJ47sJacI++j7Yer1KJvLHb1qVRAH X-Gm-Gg: ASbGncsf+xDlER3vuaSEcyQKXVNgykh2wjRigXDG1LKr6XM/j4IpGMH+jTqARHFSIhz QrvZ0vS/8XQBvD3Gs2ILwdnNu2PrIjI9ElcXnCYNvyfXcX7XXQSM/sPYUkpGefMDrD9jkCAbxvV p/eD0f/oOyt0B9Jk3AY7O3tMNw9uS5vrEIDcbfZZTZdCyIkH6G/da/4F4Md1B9iQQ+n1s/99eew uyvKpJnaCyc+AtT3LsAQAK0ve+rizdmZZhyHiEOguTZbvEpKdytzZgmQQ0f/t72bPDyLlDYXazr WrofQW/QoPsd6vGdBS0LP7D64BWtJghHGeVi2Xlt44Ydn4iHSxU4zGbGkTE8sw== X-Google-Smtp-Source: AGHT+IEugiza3fgduRxn2ow9OvjRvHyOfJwN+IFpHUh7HXpHpxCh+bAzoEvIlSNWAfmhOeZyaMK5eQ== X-Received: by 2002:a17:90b:3905:b0:2fe:b8ba:62de with SMTP id 98e67ed59e1d1-30e7d5a96d7mr26190895a91.25.1747745660242; Tue, 20 May 2025 05:54:20 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 22/33] tcg/wasm32: Add qemu_ld/qemu_st instructions Date: Tue, 20 May 2025 21:51:24 +0900 Message-ID: <50dd6a8e7cce06b191b1dbb3c5cc470e0819a566.1747744132.git.ktokunaga.mail@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::533; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pg1-x533.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747746306550116600 Content-Type: text/plain; charset="utf-8" This commit adds qemu_ld and qemu_st by calling the helper functions corresponding to MemOp. Signed-off-by: Kohei Tokunaga --- tcg/wasm32/tcg-target.c.inc | 108 ++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/tcg/wasm32/tcg-target.c.inc b/tcg/wasm32/tcg-target.c.inc index 708af1fbb6..ea9131e6fe 100644 --- a/tcg/wasm32/tcg-target.c.inc +++ b/tcg/wasm32/tcg-target.c.inc @@ -1793,6 +1793,112 @@ static void tcg_wasm_out_call(TCGContext *s, int fu= nc, gen_call(s, info, func_idx); } =20 +static void *qemu_ld_helper_ptr(uint32_t oi) +{ + MemOp mop =3D 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) +{ + int helper_idx; + int func_idx; + bool addr64 =3D s->addr_type =3D=3D TCG_TYPE_I64; + + helper_idx =3D (uint32_t)qemu_ld_helper_ptr(oi); + func_idx =3D get_helper_idx(s, helper_idx); + if (func_idx < 0) { + func_idx =3D register_helper(s, helper_idx); + } + + if (!addr64) { + tcg_wasm_out_ext32u(s, TCG_REG_TMP, addr_reg); + addr_reg =3D TCG_REG_TMP; + } + + /* call helper */ + tcg_wasm_out_op_global_get_r(s, TCG_AREG0); + tcg_wasm_out_op_i32_wrap_i64(s); + tcg_wasm_out_op_global_get_r(s, addr_reg); + tcg_wasm_out_op_i32_const(s, oi); + tcg_wasm_out_op_i32_const(s, (int32_t)s->code_ptr); + + tcg_wasm_out_op_call(s, func_idx); + tcg_wasm_out_op_global_set_r(s, data_reg); +} + +static void *qemu_st_helper_ptr(uint32_t oi) +{ + MemOp mop =3D 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) +{ + int helper_idx; + int func_idx; + bool addr64 =3D s->addr_type =3D=3D TCG_TYPE_I64; + MemOp mop =3D get_memop(oi); + + helper_idx =3D (uint32_t)qemu_st_helper_ptr(oi); + func_idx =3D get_helper_idx(s, helper_idx); + if (func_idx < 0) { + func_idx =3D register_helper(s, helper_idx); + } + + if (!addr64) { + tcg_wasm_out_ext32u(s, TCG_REG_TMP, addr_reg); + addr_reg =3D TCG_REG_TMP; + } + + /* call helper */ + tcg_wasm_out_op_global_get_r(s, TCG_AREG0); + tcg_wasm_out_op_i32_wrap_i64(s); + tcg_wasm_out_op_global_get_r(s, addr_reg); + switch (mop & MO_SSIZE) { + case MO_UQ: + tcg_wasm_out_op_global_get_r(s, data_reg); + break; + default: + tcg_wasm_out_op_global_get_r(s, data_reg); + tcg_wasm_out_op_i32_wrap_i64(s); + break; + } + tcg_wasm_out_op_i32_const(s, oi); + tcg_wasm_out_op_i32_const(s, (int32_t)s->code_ptr); + + tcg_wasm_out_op_call(s, func_idx); +} =20 static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, intptr_t value, intptr_t addend) @@ -3128,6 +3234,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); } =20 static const TCGOutOpQemuLdSt outop_qemu_ld =3D { @@ -3153,6 +3260,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); } =20 static const TCGOutOpQemuLdSt outop_qemu_st =3D { --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747746182; cv=none; d=zohomail.com; s=zohoarc; b=HdV53IzexRz+x1X3SHYOFOpwI2RKrlB2u5CathmsCaZrsvZSnVsCq+GNNJNnZ94tTll/kViAecg5CqmmqY+vt/bb8lnP1n/fscNbgz4m4XznsvQ9Wq2PG5K5mHj3c/FObufQAtpOR8MzXsWsMdeJx3rPmMWZZlb64pXfKZ3JD0I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747746182; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=CvwVU9F6cQaGXnx8rRhBrrA5oqA9zp4XDJF1ywgSwYE=; b=Conmi7hIrT8RLzOkEzWoqgLHfxRHtluMrAlEWr9wAZd4wri8cgLDW8EGKcJT+Ahk/TM8e7nUW2NtFBdudOwzOg0N9rNEydsRQJ2TDF3fo3hmXuDVysktPbqVC6oiYt+F1UBsLp7Fkh9XTAY1lLiXjamPye4/cdpczLckJ0//L70= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747746182693690.7706390274462; Tue, 20 May 2025 06:03:02 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMUn-0003jH-3g; Tue, 20 May 2025 08:55:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMUL-0002PE-Mw; Tue, 20 May 2025 08:54:38 -0400 Received: from mail-pj1-x1036.google.com ([2607:f8b0:4864:20::1036]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMUH-000109-Td; Tue, 20 May 2025 08:54:33 -0400 Received: by mail-pj1-x1036.google.com with SMTP id 98e67ed59e1d1-3081fe5987eso4693622a91.3; Tue, 20 May 2025 05:54:26 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.54.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:54:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745666; x=1748350466; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=CvwVU9F6cQaGXnx8rRhBrrA5oqA9zp4XDJF1ywgSwYE=; b=MG/WRv+ucLbCFUp3eM3Z7kWJAQiXtR0g75vxSu0i0YuLzhnPNdLPk7WF/P+lZhiYTG 4Gq/ax0aNybz3lU21WXg4zcqzQrBUdO+WZsPKve2RQe+KD6lAKguT2oDlfibaWy+Qvk+ 3zY+pL6+Zu44cqJktzsqr25OtT5hueRVpbkDFMSG52nV2ZxSLuqWo3EdNXC/7+9tuvzq Q6XGk548J2Nphi8WhS2SAAWSNUhAMYiso0Dl6pusbjIgXis3qWD8EW/68vIdhViMdr8d V0pbY8kaiMrfEXjXsd3qkdSAq2OgjL3N+kTOXfv5Nw0ehHLYz7VHpJy0jQ5ROgEnqQoa cilQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745666; x=1748350466; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=CvwVU9F6cQaGXnx8rRhBrrA5oqA9zp4XDJF1ywgSwYE=; b=DxQpsYQYvqqE6fcdgJjT4R/kEekfxnytQjquKAEjEtr8rGHRxqvH7zYgKrj/lcJv8a c9D2RWeXtL5tcJ8pVJWpM1PMCEQ1nX1LqwaWdL+MboT54wXliE2jbYInzLIyCqWxj9Cb JoVppFkeIatxeChNsm6hrWrOEmNsMfXyZdWHx11wpMoTZv0K5IVQ5YxyOgjeNK9pmUwR c2irMgNqu7h6ls+AoTNau3yF1UQwpgJ31/T3BtwqKR10L1J0vR59k2BdX87Kl2wntBGF 2/Cwpg2XJKpfpKTJRiMlSAuPe4AggRgkRmpTVgv/fDNGDxCT5vTjUtuCkM3LX3JazbDt qKKg== X-Forwarded-Encrypted: i=1; AJvYcCX3e3z1djNt9T6kksi4e0GuQhomiOBMX6Za25GYeRGxbMoH+OoqvfVRFQv+zzAesmOKSoP+QmiCYVSleA==@nongnu.org, AJvYcCX4adKZYjyrCrZKVMpxbs06aK2i73ab0ay0NWSslnA0HOm9hd8tZ5ntLbFsNq69bJ7CfEce25EzhQ==@nongnu.org X-Gm-Message-State: AOJu0YzfU6L7DEZb85cwYgKfKvtXrWDDxDO8/DIQk8N39TzE0ug4saMY +1BotJBUy0ofEf6WVS998wTPDD02hm99ucyJ4pZkl8gcOn/zScrqwTwN3sEUbMtq X-Gm-Gg: ASbGncsRma2nghd6z2SLhBGTul46FnUlbg3Kzrpzu8Ic6Fd3VAnd4elzToHh4QO8xT8 u19VgFSBIoT0NtqkCuU49Z/G53+Jz7NH6VRcHvEr+GXqFezo2BdCtdHoHGuZFVPtBYUP1b4rMJD gj6kWSt+suUGmeehQXkwRTLI7cAIAyhqyKMexBgH5mSTJwiIPdHQMhFrr08VBHfHSSBGv2lh7Hr fSe+qMaKP+S67pqf5DdfVKJZBJ2/nIBiIWpcPJ5tTScm+ryMIl5yetQKCgDGFgttptp1pOduEkL GG/mi/qHi86ZIos3x46d+GIRUK3UsVy32X2r8d2TT5B5DxSV3w8= X-Google-Smtp-Source: AGHT+IHUv1Ky3GKBjVeftojRctD7ekQgFkcQPAUZGG0rypCRZbOSLLaaaceS/2YOW575tisr9dvdyA== X-Received: by 2002:a17:90b:3c90:b0:30e:37be:6963 with SMTP id 98e67ed59e1d1-30e83228d1cmr24997945a91.29.1747745665822; Tue, 20 May 2025 05:54:25 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 23/33] include/exec: Allow using 64bit guest addresses on emscripten Date: Tue, 20 May 2025 21:51:25 +0900 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1036; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pj1-x1036.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747746184217116600 Content-Type: text/plain; charset="utf-8" To enable 64-bit guest support in Wasm 32bit memory model today, it was necessary to partially revert recent changes that removed support for different pointer widths between the host and guest (e.g. commits a70af12addd9060fdf8f3dbd42b42e3072c3914f and bf455ec50b6fea15b4d2493059365bf94c706273) when compiling with Emscripten. While this serves as a temporary workaround, a long-term solution could involve adopting Wasm's 64-bit memory model once it gains broader support, as it is currently not widely adopted (e.g. unsupported by Safari and libffi). Signed-off-by: Kohei Tokunaga --- accel/tcg/cputlb.c | 8 ++++---- include/exec/helper-head.h.inc | 6 ++++++ include/exec/tlb-common.h | 14 ++++++++++---- include/exec/vaddr.h | 11 +++++++++++ include/qemu/atomic.h | 4 ++++ include/tcg/tcg.h | 4 ++++ meson.build | 8 +++++--- 7 files changed, 44 insertions(+), 11 deletions(-) V1: - Although I tried to use "#if HOST_LONG_BITS >=3D TARGET_LONG_BITS" based = on Paolo's suggestion from the previous patch series, TARGET_LONG_BITS is marked as poisoned in include/exec/poison.h and cannot be used directly. diff --git a/accel/tcg/cputlb.c b/accel/tcg/cputlb.c index 5f6d7c601c..b15e9e80ee 100644 --- a/accel/tcg/cputlb.c +++ b/accel/tcg/cputlb.c @@ -109,13 +109,13 @@ static inline uint64_t tlb_read_idx(const CPUTLBEntry= *entry, { /* Do not rearrange the CPUTLBEntry structure members. */ QEMU_BUILD_BUG_ON(offsetof(CPUTLBEntry, addr_read) !=3D - MMU_DATA_LOAD * sizeof(uintptr_t)); + MMU_DATA_LOAD * sizeof(tlb_addr)); QEMU_BUILD_BUG_ON(offsetof(CPUTLBEntry, addr_write) !=3D - MMU_DATA_STORE * sizeof(uintptr_t)); + MMU_DATA_STORE * sizeof(tlb_addr)); QEMU_BUILD_BUG_ON(offsetof(CPUTLBEntry, addr_code) !=3D - MMU_INST_FETCH * sizeof(uintptr_t)); + MMU_INST_FETCH * sizeof(tlb_addr)); =20 - const uintptr_t *ptr =3D &entry->addr_idx[access_type]; + const tlb_addr *ptr =3D &entry->addr_idx[access_type]; /* ofs might correspond to .addr_write, so use qatomic_read */ return qatomic_read(ptr); } diff --git a/include/exec/helper-head.h.inc b/include/exec/helper-head.h.inc index 5b248fd713..7dfb4cfa46 100644 --- a/include/exec/helper-head.h.inc +++ b/include/exec/helper-head.h.inc @@ -58,6 +58,7 @@ # define dh_ctype_tl target_ulong #endif /* COMPILING_PER_TARGET */ =20 +#ifndef EMSCRIPTEN #if __SIZEOF_POINTER__ =3D=3D 4 # define dh_alias_vaddr i32 # define dh_typecode_vaddr dh_typecode_i32 @@ -68,6 +69,11 @@ # error "sizeof pointer is different from {4,8}" #endif /* __SIZEOF_POINTER__ */ # define dh_ctype_vaddr uintptr_t +#else +# define dh_alias_vaddr i64 +# define dh_typecode_vaddr dh_typecode_i64 +# define dh_ctype_vaddr uint64_t +#endif =20 /* We can't use glue() here because it falls foul of C preprocessor recursive expansion rules. */ diff --git a/include/exec/tlb-common.h b/include/exec/tlb-common.h index 03b5a8ffc7..679054bb44 100644 --- a/include/exec/tlb-common.h +++ b/include/exec/tlb-common.h @@ -19,14 +19,20 @@ #ifndef EXEC_TLB_COMMON_H #define EXEC_TLB_COMMON_H 1 =20 +#ifndef EMSCRIPTEN #define CPU_TLB_ENTRY_BITS (HOST_LONG_BITS =3D=3D 32 ? 4 : 5) +typedef uintptr_t tlb_addr; +#else +#define CPU_TLB_ENTRY_BITS 5 +typedef uint64_t tlb_addr; +#endif =20 /* Minimalized TLB entry for use by TCG fast path. */ typedef union CPUTLBEntry { struct { - uintptr_t addr_read; - uintptr_t addr_write; - uintptr_t addr_code; + tlb_addr addr_read; + tlb_addr addr_write; + tlb_addr addr_code; /* * Addend to virtual address to get host address. IO accesses * use the corresponding iotlb value. @@ -37,7 +43,7 @@ typedef union CPUTLBEntry { * Padding to get a power of two size, as well as index * access to addr_{read,write,code}. */ - uintptr_t addr_idx[(1 << CPU_TLB_ENTRY_BITS) / sizeof(uintptr_t)]; + tlb_addr addr_idx[(1 << CPU_TLB_ENTRY_BITS) / sizeof(tlb_addr)]; } CPUTLBEntry; =20 QEMU_BUILD_BUG_ON(sizeof(CPUTLBEntry) !=3D (1 << CPU_TLB_ENTRY_BITS)); diff --git a/include/exec/vaddr.h b/include/exec/vaddr.h index 28bec632fb..ff57f944dd 100644 --- a/include/exec/vaddr.h +++ b/include/exec/vaddr.h @@ -9,6 +9,7 @@ * We do not support 64-bit guest on 32-host and detect at configure time. * Therefore, a host pointer width will always fit a guest pointer. */ +#ifndef EMSCRIPTEN typedef uintptr_t vaddr; #define VADDR_PRId PRIdPTR #define VADDR_PRIu PRIuPTR @@ -16,5 +17,15 @@ typedef uintptr_t vaddr; #define VADDR_PRIx PRIxPTR #define VADDR_PRIX PRIXPTR #define VADDR_MAX UINTPTR_MAX +#else +/* Explicitly define this as 64bit on emscripten */ +typedef uint64_t vaddr; +#define VADDR_PRId PRId64 +#define VADDR_PRIu PRIu64 +#define VADDR_PRIo PRIo64 +#define VADDR_PRIx PRIx64 +#define VADDR_PRIX PRIX64 +#define VADDR_MAX UINT64_MAX +#endif =20 #endif diff --git a/include/qemu/atomic.h b/include/qemu/atomic.h index f80cba24cf..76a8fbcd8c 100644 --- a/include/qemu/atomic.h +++ b/include/qemu/atomic.h @@ -56,6 +56,7 @@ */ #define signal_barrier() __atomic_signal_fence(__ATOMIC_SEQ_CST) =20 +#ifndef EMSCRIPTEN /* * Sanity check that the size of an atomic operation isn't "overly large". * Despite the fact that e.g. i686 has 64-bit atomic operations, we do not @@ -63,6 +64,9 @@ * bit of sanity checking that other 32-bit hosts might build. */ #define ATOMIC_REG_SIZE sizeof(void *) +#else +#define ATOMIC_REG_SIZE 8 /* wasm supports 64bit atomics */ +#endif =20 /* Weak atomic operations prevent the compiler moving other * loads/stores past the atomic operation load/store. However there is diff --git a/include/tcg/tcg.h b/include/tcg/tcg.h index 041d8035bc..62bc2c4ea8 100644 --- a/include/tcg/tcg.h +++ b/include/tcg/tcg.h @@ -218,6 +218,7 @@ typedef struct TCGv_ptr_d *TCGv_ptr; typedef struct TCGv_vec_d *TCGv_vec; typedef TCGv_ptr TCGv_env; =20 +#ifndef EMSCRIPTEN #if __SIZEOF_POINTER__ =3D=3D 4 typedef TCGv_i32 TCGv_vaddr; #elif __SIZEOF_POINTER__ =3D=3D 8 @@ -225,6 +226,9 @@ typedef TCGv_i64 TCGv_vaddr; #else # error "sizeof pointer is different from {4,8}" #endif /* __SIZEOF_POINTER__ */ +#else +typedef TCGv_i64 TCGv_vaddr; +#endif =20 /* call flags */ /* Helper does not read globals (either directly or through an exception).= It diff --git a/meson.build b/meson.build index c533243157..f6ed867819 100644 --- a/meson.build +++ b/meson.build @@ -3322,9 +3322,11 @@ foreach target : target_dirs =20 target_kconfig =3D [] foreach sym: accelerators - # Disallow 64-bit on 32-bit emulation and virtualization - if host_long_bits < config_target['TARGET_LONG_BITS'].to_int() - continue + if host_arch !=3D 'wasm32' + # Disallow 64-bit on 32-bit emulation and virtualization + if host_long_bits < config_target['TARGET_LONG_BITS'].to_int() + continue + endif endif if sym =3D=3D 'CONFIG_TCG' or target in accelerator_targets.get(sym, [= ]) config_target +=3D { sym: 'y' } --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747746397; cv=none; d=zohomail.com; s=zohoarc; b=lBnHGbNAEFP4abP5UChwDelKWDOH9Mx9VLiUFCsI1LIk7PZnifyBBdzAYkKwnX6vXTpmum9klTApNfxZwbZuv8RZPmWxSEruFO3QeZNCEepB481LE2WBpq2OTvexbQ17IBZZPxLQKH571h9EBwZnS3tzu9FGBMlPsGhjflQ3E9E= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747746397; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=1G0lGd9H5YB3nSr0o+UY98M1y1yG48dZRtLGwlQiSB0=; b=QV5isF3qA2Y08FKribsUPAQS0POc3p2Q1tNVc4BfLz2PD1Gxhv54reRWulszP+04E2z/zKRowX1QvNT/ESvrNDgI8Qks/viYSZ9v3COM8w33QRyojQv9jUIu/9cwpPsBkDulFXxP+dpbMwTUpabPqaPdTdM2Y9Pom3X79FJJvlM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747746397688687.9217062638016; Tue, 20 May 2025 06:06:37 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMV2-0004ZQ-SI; Tue, 20 May 2025 08:55:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMUP-0002Rj-Cs; Tue, 20 May 2025 08:54:40 -0400 Received: from mail-pj1-x1030.google.com ([2607:f8b0:4864:20::1030]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMUM-000117-RI; Tue, 20 May 2025 08:54:37 -0400 Received: by mail-pj1-x1030.google.com with SMTP id 98e67ed59e1d1-30e9397802eso2970361a91.1; Tue, 20 May 2025 05:54:32 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.54.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:54:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745672; x=1748350472; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=1G0lGd9H5YB3nSr0o+UY98M1y1yG48dZRtLGwlQiSB0=; b=ZKc5xag9u76rxyy5Ow3RT9IYqEQR3Yt2KlboQd2REK9iwW2MoW2bb5G00j213vMbqx 0sAa48dprkoQJhi7/jhpUBNrK5UxiALvVBjAsOb0CLIzOHIMo5oAEXkZ0AvEp2wEoLWe X6HB+ZY5uk8IxzagOBpLgLzQRccDB2O3xZG3vU/llK04c7/HRuLrD9oUSuU6dDf6IWOi 9aM0gST6LaMiYQOAP0W2JM1t9hZ+v74n5sjDTdLkCSp7lJKPVsM5wn/6PTBSS7sa8Yj0 srnjSlENfINlw6c5tLqpLBjAWJZyMqoGouJ8rLV5Wug7CForW8MqFjtdmNWZh/tAMvJl NFLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745672; x=1748350472; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1G0lGd9H5YB3nSr0o+UY98M1y1yG48dZRtLGwlQiSB0=; b=ZyNrIKqU0a3dJ0s02Gfktkrk9ddub93oNAwuSfPj0fuSaFX9euzYHfsqTMtfgZyAdV AxAuJKRs0rJdSPcTQkgZsCQRU6S28VuxgEdcpndlMVON3Q1uaKimW9FUDkKnoHbkl/iG 9tYrhrwnOSR71R8DNOLvb7mLJZpO0KVBsS0Xz9LgIw/ihZMLhF/c/nCnLVjPGZacfHeX 7E3GGDtRQoQ2RXmHbi/YbZnG1JOEggYBHh9d3bdRFcLeqWbcrs+vDyBzQmocEdi0qafT 8yaXADQd3r4CKHJkDa4hPyjpE7r3e5Epv1pTQdhY8Rn/sAv9WQ4kF3f77NYWW2d9kqwm 3Fmg== X-Forwarded-Encrypted: i=1; AJvYcCUFlG/rEqTyx6MHQWdx81NnrI6dnPoX75X1/1Y8YP56NHzStQvc80PcqxhZISrLPHFEU87142HorUM85A==@nongnu.org, AJvYcCXLV1Wk6uf7s3MFSFjIsIN1vNzKaVn9Fik2Fe2uuDNPna6EkxJMLrT5T2VT2Nhq9v6M1PFjRm0qpg==@nongnu.org X-Gm-Message-State: AOJu0YyTNShCvaUalQxADikbfeUr4jSXQnupOH6h3faorrlwnlHaFUaD 8P07HVxiHSFfOD73B/aGgb4H13Htcjur+tMmWNEuCQDpIzrqEgsHiZ/cQDb0aNS0 X-Gm-Gg: ASbGncviqcGfD214hARw6E9Y9CFYCwNjZD7S5hH/XREi1bFlEkWf/vnmWT3aZW2Vuiz whWhSHUINqtVeEeQlgOP9q8bzk7EE1PLkCoIYXV2U3a1SJkHYDvYr2KL1XkVAVQU9/nqWp3dLv+ uiLCKf9caDbr7Ys3y6OHqO2q/cVs3wm6Xyet9JWCx6ysYZSurcNxmydNfbaK+EB6AtMW7F7Ffxy 4rak5Q2LFuvU6fpADtWvZA2hH5uhlJI2EbIx3rMU5UaMlYNlWuPhk7YoRdIMaUQR536Vx51G97A GR167I5/uHvT9aNC5zDzpCj6u3IOTc3pMi1SO6eEgYnGa0yyau4bnbhrbNI3lA== X-Google-Smtp-Source: AGHT+IH5jPj4xy/PBDXM5vca25pM/qZpEeUcxUsvmm+yucQ5nKRbbIWJIY0zAt2Pcv7CYRhywuR8Uw== X-Received: by 2002:a17:90b:2d47:b0:2f8:b2c:5ef3 with SMTP id 98e67ed59e1d1-30e7d520bfemr28537283a91.14.1747745671552; Tue, 20 May 2025 05:54:31 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 24/33] tcg/wasm32: Set TCG_TARGET_REG_BITS to 64 Date: Tue, 20 May 2025 21:51:26 +0900 Message-ID: <8e72ec7b0735c14a2359e95606373ac1ade5a33e.1747744132.git.ktokunaga.mail@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1030; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pj1-x1030.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747746405123116600 Content-Type: text/plain; charset="utf-8" This commit enables to Wasm backend to run as a 64bit backend with removing TCG_TARGET_REG_BITS =3D 32 macros. Signed-off-by: Kohei Tokunaga --- tcg/wasm32.c | 5 ++- tcg/wasm32/tcg-target-reg-bits.h | 8 +--- tcg/wasm32/tcg-target.c.inc | 69 +++----------------------------- 3 files changed, 9 insertions(+), 73 deletions(-) diff --git a/tcg/wasm32.c b/tcg/wasm32.c index 4bc53d76d0..b238ccf6d6 100644 --- a/tcg/wasm32.c +++ b/tcg/wasm32.c @@ -370,8 +370,9 @@ uintptr_t QEMU_DISABLE_CFI tcg_qemu_tb_exec(CPUArchStat= e *env, unsigned i, s, n; =20 tci_args_nl(insn, tb_ptr, &len, &ptr); - func =3D ((void **)ptr)[0]; - cif =3D ((void **)ptr)[1]; + tcg_target_ulong *data =3D (tcg_target_ulong *)ptr; + func =3D (void *)data[0]; + cif =3D (void *)data[1]; =20 n =3D cif->nargs; for (i =3D s =3D 0; i < n; ++i) { diff --git a/tcg/wasm32/tcg-target-reg-bits.h b/tcg/wasm32/tcg-target-reg-b= its.h index dcb1a203f8..375feccf91 100644 --- a/tcg/wasm32/tcg-target-reg-bits.h +++ b/tcg/wasm32/tcg-target-reg-bits.h @@ -7,12 +7,6 @@ #ifndef TCG_TARGET_REG_BITS_H #define TCG_TARGET_REG_BITS_H =20 -#if UINTPTR_MAX =3D=3D UINT32_MAX -# define TCG_TARGET_REG_BITS 32 -#elif UINTPTR_MAX =3D=3D UINT64_MAX -# define TCG_TARGET_REG_BITS 64 -#else -# error Unknown pointer size for tci target -#endif +#define TCG_TARGET_REG_BITS 64 =20 #endif diff --git a/tcg/wasm32/tcg-target.c.inc b/tcg/wasm32/tcg-target.c.inc index ea9131e6fe..9fad96d0fd 100644 --- a/tcg/wasm32/tcg-target.c.inc +++ b/tcg/wasm32/tcg-target.c.inc @@ -30,15 +30,9 @@ /* Used for function call generation. */ #define TCG_TARGET_CALL_STACK_OFFSET 0 #define TCG_TARGET_STACK_ALIGN 8 -#if TCG_TARGET_REG_BITS =3D=3D 32 -# define TCG_TARGET_CALL_ARG_I32 TCG_CALL_ARG_EVEN -# define TCG_TARGET_CALL_ARG_I64 TCG_CALL_ARG_EVEN -# define TCG_TARGET_CALL_ARG_I128 TCG_CALL_ARG_EVEN -#else -# define TCG_TARGET_CALL_ARG_I32 TCG_CALL_ARG_NORMAL -# define TCG_TARGET_CALL_ARG_I64 TCG_CALL_ARG_NORMAL -# define TCG_TARGET_CALL_ARG_I128 TCG_CALL_ARG_NORMAL -#endif +#define TCG_TARGET_CALL_ARG_I32 TCG_CALL_ARG_NORMAL +#define TCG_TARGET_CALL_ARG_I64 TCG_CALL_ARG_NORMAL +#define TCG_TARGET_CALL_ARG_I128 TCG_CALL_ARG_NORMAL #define TCG_TARGET_CALL_RET_I128 TCG_CALL_RET_NORMAL =20 typedef uint32_t tcg_insn_unit_tci; @@ -3083,39 +3077,6 @@ static const TCGOutOpMovcond outop_movcond =3D { .out =3D tgen_movcond, }; =20 -static void tgen_brcond2(TCGContext *s, TCGCond cond, TCGReg al, TCGReg ah, - TCGArg bl, bool const_bl, - TCGArg bh, bool const_bh, TCGLabel *l) -{ - tcg_out_op_rrrrrc(s, INDEX_op_setcond2_i32, TCG_REG_TMP, - al, ah, bl, bh, cond); - tcg_out_op_rl(s, INDEX_op_brcond, TCG_REG_TMP, l); -} - -#if TCG_TARGET_REG_BITS !=3D 32 -__attribute__((unused)) -#endif -static const TCGOutOpBrcond2 outop_brcond2 =3D { - .base.static_constraint =3D C_O0_I4(r, r, r, r), - .out =3D tgen_brcond2, -}; - -static void tgen_setcond2(TCGContext *s, TCGCond cond, TCGReg ret, - TCGReg al, TCGReg ah, - TCGArg bl, bool const_bl, - TCGArg bh, bool const_bh) -{ - tcg_out_op_rrrrrc(s, INDEX_op_setcond2_i32, ret, al, ah, bl, bh, cond); -} - -#if TCG_TARGET_REG_BITS !=3D 32 -__attribute__((unused)) -#endif -static const TCGOutOpSetcond2 outop_setcond2 =3D { - .base.static_constraint =3D C_O1_I4(r, r, r, r, r), - .out =3D tgen_setcond2, -}; - static void tcg_out_mb(TCGContext *s, unsigned a0) { tcg_out_op_v(s, INDEX_op_mb); @@ -3242,18 +3203,8 @@ static const TCGOutOpQemuLdSt outop_qemu_ld =3D { .out =3D tgen_qemu_ld, }; =20 -static void tgen_qemu_ld2(TCGContext *s, TCGType type, TCGReg datalo, - TCGReg datahi, TCGReg addr, MemOpIdx oi) -{ - tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_TMP, oi); - tcg_out_op_rrrr(s, INDEX_op_qemu_ld2, datalo, datahi, addr, TCG_REG_TM= P); -} - static const TCGOutOpQemuLdSt2 outop_qemu_ld2 =3D { - .base.static_constraint =3D - TCG_TARGET_REG_BITS =3D=3D 64 ? C_NotImplemented : C_O2_I1(r, r, r= ), - .out =3D - TCG_TARGET_REG_BITS =3D=3D 64 ? NULL : tgen_qemu_ld2, + .base.static_constraint =3D C_NotImplemented, }; =20 static void tgen_qemu_st(TCGContext *s, TCGType type, TCGReg data, @@ -3268,18 +3219,8 @@ static const TCGOutOpQemuLdSt outop_qemu_st =3D { .out =3D tgen_qemu_st, }; =20 -static void tgen_qemu_st2(TCGContext *s, TCGType type, TCGReg datalo, - TCGReg datahi, TCGReg addr, MemOpIdx oi) -{ - tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_TMP, oi); - tcg_out_op_rrrr(s, INDEX_op_qemu_st2, datalo, datahi, addr, TCG_REG_TM= P); -} - static const TCGOutOpQemuLdSt2 outop_qemu_st2 =3D { - .base.static_constraint =3D - TCG_TARGET_REG_BITS =3D=3D 64 ? C_NotImplemented : C_O0_I3(r, r, r= ), - .out =3D - TCG_TARGET_REG_BITS =3D=3D 64 ? NULL : tgen_qemu_st2, + .base.static_constraint =3D C_NotImplemented, }; =20 static void tcg_out_st(TCGContext *s, TCGType type, TCGReg val, TCGReg bas= e, --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747746386; cv=none; d=zohomail.com; s=zohoarc; b=TN9pFKHpEsOHfP9jecp68Twq9cZ0V9XmWg9JOrQy1gAKlFlft/c/CKSuS7U68bQPRUWOttYl1XqjzNUtQpfep6jYSqmpsclSszU0reNwKDFk7NU3kEwqOLZ1Rijb+/X3Jo+OMwTTq/Qx+7180yw0S3QGtYrq7PIsEIsA1pTHF6g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747746386; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=7eabZrzKs+cL55IaJg89VzpBHaEX4pjQLmBcxb/tbcE=; b=boYELcuUgfKRN+jthYlaMIAjcZtzkl8cDjKm5+sVK0w4PNuEFc1MzSNsvzkg2oMuJscYRoviWWl69ZQtZNL30pLlQ5vTZZKBlcOPDUAojJSTH8eCAAgHVSOvmmF5rzusa7lRgSof+kcyDbXu//t5hX2thc9LO8Fj+Okveh4i8ZQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747746386877924.6142913058852; Tue, 20 May 2025 06:06:26 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMUj-0003WJ-GF; Tue, 20 May 2025 08:54:57 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMUc-0002wv-8M; Tue, 20 May 2025 08:54:50 -0400 Received: from mail-pj1-x102d.google.com ([2607:f8b0:4864:20::102d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMUX-00012I-ID; Tue, 20 May 2025 08:54:49 -0400 Received: by mail-pj1-x102d.google.com with SMTP id 98e67ed59e1d1-30572effb26so5234209a91.0; Tue, 20 May 2025 05:54:39 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.54.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:54:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745677; x=1748350477; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=7eabZrzKs+cL55IaJg89VzpBHaEX4pjQLmBcxb/tbcE=; b=PFJBH4kPO8TLd/9LqAMq9z01rA9Jn/YwOjBIujf+ZuYsqiPzIPZrpdyLa0b6VzQ7U1 9qQ6pxl79yCqyGNjYFMaiJV4wJB2pD8hBWqDTzp642MzsQq90PNybIKflAUnUM2IMzCw O3NMkRJyNHlHLSgv6Mc4geOMMoDU/4q4gzmqfsHoD5tc94lIAg+E5XSICH8MdanbORb+ W6LAjpgcVJaY06KyOQDidw50H79jpcgS8M23MGK1xdH2M3giVYpVN02Bs4GH5x88yADb XYWNvb8VyNz8GsQxQgcf/6hylwETysa4F79ng/OHRZ46ZehDhiKHlfrc3Zkx6FamDS0H xEFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745677; x=1748350477; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7eabZrzKs+cL55IaJg89VzpBHaEX4pjQLmBcxb/tbcE=; b=atFm3phTa2ptgtZAVJhinIkZnV5OT8fDZvGbyMZQ+JBBdlZt9hP645gCQKuU8LmIp9 rbBAFE+BSw0KSsBhvEkYt1h8Np5n4G+hHf9RDY7Rm5zUhexqMWXG/rLSCnYXeRipcAmz ly0QyMfBOhBsyPUSAVN77czkEkVCcIriJQA3DJ7kK1P9HVaerKXlZQMDJ5M2gLPKUDL0 y42cAWPwClovbDFdC0DVEFEFXKjWKUGQmckJ1UGe0Lh5L/bk/TGLT8xLukW7KQ4k7icY 6lNj/5EHPZ81219vEV82GNkXFaDtUg+aaepZH8mZnTiba8o7dB95P9JPEVsK0lvvhGeG dWqg== X-Forwarded-Encrypted: i=1; AJvYcCVDcctndDE1NUCRpfIehupCh0zTj21on2UJqUzKLt6ZMDN2jLpDSTmfnww9/s9Y1rykZT6jzWmneDWPDA==@nongnu.org, AJvYcCW+Rg64lDcS+zynl8mU99kqcknp0820fBPeVyykXi/s6RKDuDm8dBOi9Yao32gkAvz6aQHRnTb/uA==@nongnu.org X-Gm-Message-State: AOJu0YxsrSwtsBD1iUti0G35nzgrRSfKtwLMT394N9TLDdg/bMRPtZAS LfU0ZZrBXtBRc3T8jZFmSbYj/Yu1hyrlgHEO4DsCVlQ7RwP7yp3e2McGvXKNbyCD X-Gm-Gg: ASbGncvmnak87sXOT7NW0sVwFacF+T5qeGmEMGToO69UrGz2jXHhzoggmPTWiTG6xZZ G3WyW5YrCAJn101Os/xhcjN9MF8y8ftzY11fwZMw/RDyShmut5itVsK0iPhBjIs7UZCnXq25GYF gmvg5KWv4Ws1wAzIIDst8fafJ74YSMRQJgmzZFZyqKSO/jwKSMBWMnJp53SgFN66ntz/4zxPNAq wPgIYCDSCmuxa+iXzDgf4rXfnCb/bAVq6EJ5v4Xn++X3y8NHJGpPwIjg/Ufg2bdpvUsZy89e3s+ KpzB8/v3Gn4uI6FZo5+1QDpSp3/Yy6xwVNDVvgjBy3TWWjA4uvQ= X-Google-Smtp-Source: AGHT+IEZCo8TLYMPhGsfPoyVD7cxyqqI5vVtGGele8JzfzU/R/+q5jO0KcIFs8Gd49flFHnnc/IHyQ== X-Received: by 2002:a17:90b:6c8:b0:30e:8f60:b4c with SMTP id 98e67ed59e1d1-30e8f600d77mr26832066a91.16.1747745677188; Tue, 20 May 2025 05:54:37 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 25/33] tcg/wasm32: Set mulu2/muls2 as unimplemented Date: Tue, 20 May 2025 21:51:27 +0900 Message-ID: <0e228847d902b25d643bcbee028d67e3cb0f0464.1747744132.git.ktokunaga.mail@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::102d; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pj1-x102d.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747746389543116600 Content-Type: text/plain; charset="utf-8" These operations have no direct equivalents in Wasm, so they are left unimplemented and delegated to helper functions. Signed-off-by: Kohei Tokunaga --- tcg/wasm32/tcg-target.c.inc | 38 ++----------------------------------- 1 file changed, 2 insertions(+), 36 deletions(-) diff --git a/tcg/wasm32/tcg-target.c.inc b/tcg/wasm32/tcg-target.c.inc index 9fad96d0fd..df34097267 100644 --- a/tcg/wasm32/tcg-target.c.inc +++ b/tcg/wasm32/tcg-target.c.inc @@ -2076,19 +2076,6 @@ static void tcg_out_op_rrrbb(TCGContext *s, TCGOpcod= e op, TCGReg r0, tcg_out32(s, insn); } =20 -static void tcg_out_op_rrrr(TCGContext *s, TCGOpcode op, - TCGReg r0, TCGReg r1, TCGReg r2, TCGReg r3) -{ - tcg_insn_unit_tci insn =3D 0; - - insn =3D deposit32(insn, 0, 8, op); - insn =3D deposit32(insn, 8, 4, r0); - insn =3D deposit32(insn, 12, 4, r1); - insn =3D deposit32(insn, 16, 4, r2); - insn =3D 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) @@ -2610,37 +2597,16 @@ static const TCGOutOpBinary outop_mul =3D { .out_rrr =3D tgen_mul, }; =20 -static TCGConstraintSetIndex cset_mul2(TCGType type, unsigned flags) -{ - return type =3D=3D TCG_TYPE_REG ? C_O2_I2(r, r, r, r) : C_NotImplement= ed; -} - -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 =3D { - .base.static_constraint =3D C_Dynamic, - .base.dynamic_constraint =3D cset_mul2, - .out_rrrr =3D tgen_muls2, + .base.static_constraint =3D C_NotImplemented, }; =20 static const TCGOutOpBinary outop_mulsh =3D { .base.static_constraint =3D C_NotImplemented, }; =20 -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 =3D { - .base.static_constraint =3D C_Dynamic, - .base.dynamic_constraint =3D cset_mul2, - .out_rrrr =3D tgen_mulu2, + .base.static_constraint =3D C_NotImplemented, }; =20 static const TCGOutOpBinary outop_muluh =3D { --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747746379; cv=none; d=zohomail.com; s=zohoarc; b=la4dAlDLZIopuYljoqczdOGONAFEmnbp2MtfugL0JokCj/mocLj4scKd4Htajf3i87yzPo/ZUeiHZyueaQPcSEqKwzsqPrIhnTmWG74NIyaWSErbet2+/bsGVUvXlaYUP4c96dBR4ajjTGsTzy1X7XJAb/b4lUjVzK+9/4N8Cgs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747746379; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=VMl+BLj6shot1+8OfcaCTtS5NXf/RyeuCk4yd6sbvuo=; b=O2MHDq/25pzYhdUZzOdRqLhWXne0vriVznsuEmD8yLgGQ4buVM4143WnM5ty7xqpTdRSwwME/V0wEBo0yjHysNqcnQVD4JcG064qV+Wi9WuIcjpUJt7gmkk0vU17G61O2ByVVEUsForuDd5XrxjK46KjUJOu425FxV11hKAtWe4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747746378966578.958478446059; Tue, 20 May 2025 06:06:18 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMV2-0004ZH-Qx; Tue, 20 May 2025 08:55:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMUf-0003AO-0k; Tue, 20 May 2025 08:54:53 -0400 Received: from mail-pj1-x102e.google.com ([2607:f8b0:4864:20::102e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMUb-00013Q-LS; Tue, 20 May 2025 08:54:52 -0400 Received: by mail-pj1-x102e.google.com with SMTP id 98e67ed59e1d1-30ecc762cb7so2331624a91.1; Tue, 20 May 2025 05:54:44 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.54.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:54:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745682; x=1748350482; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=VMl+BLj6shot1+8OfcaCTtS5NXf/RyeuCk4yd6sbvuo=; b=E2lYGbaXmWH+5v7FIpilWZjsivnVVMXQU+HGLbg8ihEmP3O3esilmjRsIK/SmQGmqq iLIQz7k3q0dDXohHBrMlG1wkeOwt7sSLo0o0V5ti//jZqap9OydZukifGW3sNjYPM3HP KouiMFKFZrQF8kEibsngmERxpQTfnMWXo1s5JDSWXXkUEtOffNheMqKd/ytbTMJ3ntaT o/tAr08GYLlhhsYMCr2egDxa/GnQ9gWjNTrGS9GYSxN3upXjffGtg3J042Pfv1R+URWj S+eP4/eiz26UwTFquxkPm3lawwKdTAI64W11SXj9+JsBtjPPhVjUdl9ALFLUd7HwtXVW XC+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745682; x=1748350482; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VMl+BLj6shot1+8OfcaCTtS5NXf/RyeuCk4yd6sbvuo=; b=q2nlCgMcZShpZCO7ksuBxnHECtBX21dvSBupcmyn6UGCixVIu5tG6gD71WqQM5wCep quhZOonh8M/tGjEjlorvgcIbl1Dzwm1AqPToHruGtrtRIZt6+Z4mQ609J5oadfjkPw17 9CumSUsSWpHt7TJqE7c8aqYrlZt1ry5yI/r6N1ggJc/DNp5uPeBE291I6En9R5qHMDlz Km5V8zcbN6kw/gTzbnJ7wB7FRxt70ubvjqAIgjnNPjqDirX7QzcRbmvNPhcahv7h53gI aZgff52cJbrLFsaWIOPWUQjMr+qZLl4fdN3LiS9vrSX5y+wso/ThVKHdJylaALnoN6z7 pS8Q== X-Forwarded-Encrypted: i=1; AJvYcCV64DX7NsI6uRs03+RqjzxliIOqc9xbTpjtkuD4fLlBh9mW9R4zddM/GMfYb+79XqGjCanyRKrGxQ==@nongnu.org, AJvYcCWLrZHKe2CiZGAaYxKeH5Q/CMmAvssumqQDMxDfoakSvAazj9/yXFeORKQQLk4GCNvqrHI+QH7vlFPXPA==@nongnu.org X-Gm-Message-State: AOJu0Ywlt3rMTk+QEi0nxj/C/X6/ZjL0cx9d9gZBvi0MH9g5OY3TIwSU LmXo8kkOk1MHO7GZtuDxmh+8T96NpZPdBtvVTCvXTLfxcZbOnW1hUuJKostMGLs2 X-Gm-Gg: ASbGncukI6QTtlzkHdbuYwNba13yx6HtIUbspFDed9jkCjSBHaNtcRzVi7PBs1n4HF5 s2pn43sdoMR+IRzmNNdd+zss0O/uNdNWXO/7+4LX3uiKkl45RFW6/fSLDup8D4mW7Eo+xP7moIm j0Nd8wk45f2u3iwPJ5jKEf4ihVw/Hl/nQ2gJupCSdtW3qeSAGaQURICTa5jyVA2/Vogz+uDPCjC 2aWqn0CyySHVLxK/WY5LIIwFye+QIcNHAj1o2anevxpjVv9AHA9RihY/7SI9iuD2Z8NONES4/OB FSHneA9gGXIG32HCQRHzeFvLIID9RdjdbzoybrZKuUdrHp4qnGIKIdXmAlSCkg== X-Google-Smtp-Source: AGHT+IFLR9WZD1ZfyejmhDg0dIA5O+JyaV0hB93IqpSi5hqodoOEgtN1hNTLUEDuyzBJRJYgqpPyVg== X-Received: by 2002:a17:90a:e7c2:b0:2ff:6e58:89f5 with SMTP id 98e67ed59e1d1-30e7dc4ed52mr24245041a91.6.1747745682506; Tue, 20 May 2025 05:54:42 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 26/33] tcg/wasm32: Add initialization of fundamental registers Date: Tue, 20 May 2025 21:51:28 +0900 Message-ID: <0e9635f3bd25e31fd39eea32242101c2b847784d.1747744132.git.ktokunaga.mail@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::102e; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pj1-x102e.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747746381177116600 Content-Type: text/plain; charset="utf-8" This 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. Since TB execution begins at the first block, the BLOCK_PTR_IDX variable is initialized to 0. Signed-off-by: Kohei Tokunaga --- tcg/wasm32.h | 10 ++++++++++ tcg/wasm32/tcg-target.c.inc | 17 +++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/tcg/wasm32.h b/tcg/wasm32.h index 1944249891..ab23e26eca 100644 --- a/tcg/wasm32.h +++ b/tcg/wasm32.h @@ -22,6 +22,16 @@ struct wasmContext { * Buffer to store 128bit return value on call. */ void *buf128; + + /* + * Pointer to CPUArchState struct. + */ + CPUArchState *env; + + /* + * Pointer to a stack array. + */ + uint64_t *stack; }; =20 #endif diff --git a/tcg/wasm32/tcg-target.c.inc b/tcg/wasm32/tcg-target.c.inc index df34097267..4b7cd784cb 100644 --- a/tcg/wasm32/tcg-target.c.inc +++ b/tcg/wasm32/tcg-target.c.inc @@ -3258,6 +3258,23 @@ static void tcg_out_tb_start(TCGContext *s) init_label_info(); init_helpers(); =20 + /* generate wasm code to initialize fundamental registers */ + tcg_wasm_out_op_global_get_r(s, TCG_AREG0); + tcg_wasm_out_op_i64_eqz(s); + tcg_wasm_out_op_if_noret(s); + + tcg_wasm_out_ctx_i32_load(s, env); + tcg_wasm_out_op_i64_extend_i32_u(s); + tcg_wasm_out_op_global_set_r(s, TCG_AREG0); + + tcg_wasm_out_ctx_i32_load(s, stack); + tcg_wasm_out_op_i64_extend_i32_u(s); + tcg_wasm_out_op_global_set_r(s, TCG_REG_CALL_STACK); + tcg_wasm_out_op_end(s); + + tcg_wasm_out_op_i64_const(s, 0); + tcg_wasm_out_op_global_set(s, BLOCK_PTR_IDX); + tcg_wasm_out_op_loop_noret(s); tcg_wasm_out_op_global_get(s, BLOCK_PTR_IDX); tcg_wasm_out_op_i64_eqz(s); --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747746504; cv=none; d=zohomail.com; s=zohoarc; b=eC8ux2Vit1hrODma9eNbDSKYJyf6gZWijf5njGNksQSENStWAviFG3Ty54y2XIOyP/ryziidqgwjgzLG6fx82qoarQ77rxGQj24dn4nlmJumDZyOvkUqnLndVBO49t3F5GnnL/tvgFVOM3lQI7nEGAD+h6fdmpuiLHn/0SkytvY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747746504; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Km97/3qRAEAEgLIAK0qw3k5JzHlsxkZR+9MPB91ZrAE=; b=j463xt6xNSsIl9j3KEWNsQSektViQTC7fXQ4selI7TW/Bz2o6ZFX2Y2NJitG4X2ZLKmMpz1+M0xWhSCPp7qxScy82+KHXeKfa/FGeo1VSsJi2mm+RdPaDOOs+tDR9aPRQWDi2r3se7fozL2X/s0otkpv5uWVBuLclSo3bwGkh5c= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747746504130514.4950651831861; Tue, 20 May 2025 06:08:24 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMVg-00067o-98; Tue, 20 May 2025 08:55:56 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMUg-0003M5-Uy; Tue, 20 May 2025 08:54:55 -0400 Received: from mail-pj1-x102d.google.com ([2607:f8b0:4864:20::102d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMUd-00014t-J7; Tue, 20 May 2025 08:54:54 -0400 Received: by mail-pj1-x102d.google.com with SMTP id 98e67ed59e1d1-3081f72c271so4814961a91.0; Tue, 20 May 2025 05:54:49 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.54.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:54:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745688; x=1748350488; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Km97/3qRAEAEgLIAK0qw3k5JzHlsxkZR+9MPB91ZrAE=; b=ZH9sSVKAb/zY1Vwwwn0/BZZHN73RO2MhW2HnxXZ6JMqJtFoKTZ/CjmhLqGdap3kEJp r+UnqPy2YnvkG91UfPhx0Txt0yyDhS9Iww3Gixq6d4UO0FO9Tj30ta8YzgVB7BxxH4ot HLB0xOsNsdES4KNAvTc1sepEhvKOW0kuqk/kahcUIunOfCZk06oIm4DMshtTREA+xjiE uvYfLDAL7uZ6LP9hzkUIEiuU2A8z8p0cVaoXZ1c7bmB9m3mNMrAdqZUFY3EucwiSBSlb jHE/s/l+QjHp40HO/Lo4GSqV3xX+MW/30blzs2xL/LqH4pZWXnzwLk8hGzSR8KuZsSEW R5CA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745688; x=1748350488; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Km97/3qRAEAEgLIAK0qw3k5JzHlsxkZR+9MPB91ZrAE=; b=FtYdlvKon+KQPdrMxExhAFObdshJ80BG8Z8MtFHowtcWjGkjk1ouaYFgMoHPrKpFfD E+pDku2utUoVHf68/iJTY7Z2JEDVCBLkt0bLf7Hg3xEL1+W3Lj0O0+pq1x4Yzlnbfo0G vQ0B5GZduXJkkiQN9inr9pzaVqAnVnaLZ32IPEnNOXDhMoBLf3D2yrIb9rmtH4IPiK7e AhkS6ivQ680Zh2gNli5bGUS9/pthic6Ox8zPsECgw2S+eHnx0aodpQa0nY283Ve2KIh7 1SDbOKa1DSv2H3jOvs7W2xyf+U1xJ+yAnoecon72zW9NaPJ+e5/Ji8524I81GopR2wy0 83uw== X-Forwarded-Encrypted: i=1; AJvYcCWPe7yXePRQJQjqpX2ZtjMUMjFMKlcyFEPJCmDMYbJAhM93Wb605f4NgbaBZ8SoKjb7EBEgET8ye0lDLA==@nongnu.org, AJvYcCXZ62g0Td0DbQ3F3V3AriFATdJcKcaAuBCudY0CTD0uq0irKhRjCkUo0zRB6q7NM4h5OYCEZsQLHA==@nongnu.org X-Gm-Message-State: AOJu0YxEOsD2iBGS2nROBmq08ocPltaJAs0bKEiFuG0miKOgD3xRdaHC bw9c13oMoPcV4KVftY2Z3UAushJhhn93nIcYz3hNA/d56AiqccZmlh68YW4Kz3Sw X-Gm-Gg: ASbGncs71aaTkw5PgmpQaPrJggBtl8EkpvkGGDd7xKPBmKJ5xBE62qr7Yn731Teukgq 5PzUZazLsW0lEi034gGQ7J+A6yMZpfDBqHwWqI6yYTV4diIuEDOwMN+0v065qKTRtSSr6JBEn5m ef33bTg62QRue5YC/yzj4ANQXSoxYkVdynvb0tWOiTb+lKhioXM5qHOhcpg7R/7Os7SnROc4Mz2 jkrA9ays1vUI7Cq5HEK7rdUSK/GPZFpZSDa8xo4xnEa+AWsdXn6WfEW3RfjIfsYo7yzmZWq7WQQ akl9MerWQGeGPuePpYyXkXBZxAXwWSMCacvPVeB7OY4xxzi2xcg= X-Google-Smtp-Source: AGHT+IFRSqU0y67I8zqhxLqwH5mfvyWM8AKPYHvJC4qpHwSBV2SavMTw+mHSzA2TMrDBEruEplLvuA== X-Received: by 2002:a17:90a:ec8c:b0:30c:540b:9ac with SMTP id 98e67ed59e1d1-30e830ebd3fmr27835549a91.10.1747745688216; Tue, 20 May 2025 05:54:48 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 27/33] tcg/wasm32: Write wasm binary to TB Date: Tue, 20 May 2025 21:51:29 +0900 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::102d; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pj1-x102d.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747746506601116600 Content-Type: text/plain; charset="utf-8" This 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, followed by the following data: - TCI code - Wasm code - Array of function indices imported into the Wasm instance The wasmTBHeader contains pointers to each of these elements. 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] and copying the Wasm code body from sub_buf to the code buffer. 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 assigned function IDs. Each function's type signature must also be encoded in the Wasm module head= er. To support this, each call, qemu_ld and qemu_st operation records the target function's type information to a buffer. Memory is shared between QEMU and the TBs and is imported to the Wasm module with the name "env.buffer". [1] https://webassembly.github.io/spec/core/binary/modules.html [2] https://github.com/WebAssembly/threads/blob/b2567bff61ee6fbe731934f0ed1= 7a5d48dc9ab01/proposals/threads/Overview.md Signed-off-by: Kohei Tokunaga --- tcg/wasm32.h | 26 +++ tcg/wasm32/tcg-target.c.inc | 390 ++++++++++++++++++++++++++++++++++++ 2 files changed, 416 insertions(+) diff --git a/tcg/wasm32.h b/tcg/wasm32.h index ab23e26eca..f8651af4ee 100644 --- a/tcg/wasm32.h +++ b/tcg/wasm32.h @@ -34,4 +34,30 @@ struct wasmContext { uint64_t *stack; }; =20 +/* Instantiated Wasm function of a TB */ +typedef int32_t (*wasm_tb_func)(struct wasmContext *); + +/* + * TB of wasm backend starts from a header which stores 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 diff --git a/tcg/wasm32/tcg-target.c.inc b/tcg/wasm32/tcg-target.c.inc index 4b7cd784cb..d9a3abae70 100644 --- a/tcg/wasm32/tcg-target.c.inc +++ b/tcg/wasm32/tcg-target.c.inc @@ -485,6 +485,30 @@ static void tcg_wasm_out_op_cond_i32( tcg_wasm_out8(s, op); } =20 +static void fill_uint32_leb128(uint8_t *b, uint32_t v) +{ + do { + *b |=3D v & 0x7f; + v >>=3D 7; + b++; + } while (v !=3D 0); +} + +static int write_uint32_leb128(uint8_t *b, uint32_t v) +{ + uint8_t *base =3D b; + do { + *b =3D v & 0x7f; + v >>=3D 7; + if (v !=3D 0) { + *b |=3D 0x80; + } + b++; + } while (v !=3D 0); + + return (int)(b - base); +} + static void tcg_wasm_out_leb128_sint32_t(TCGContext *s, int32_t v) { bool more =3D true; @@ -1706,6 +1730,105 @@ static void gen_call(TCGContext *s, const TCGHelper= Info *info, int func_idx) return; } =20 +__thread LinkedBuf *types_buf_root; +__thread LinkedBuf *types_buf_cur; + +static void init_types_buf(void) +{ + types_buf_root =3D new_linked_buf(); + types_buf_cur =3D types_buf_root; +} + +static inline void types_buf_out8(uint8_t v) +{ + types_buf_cur =3D linked_buf_out8(types_buf_cur, v); +} + +static inline int types_buf_len(void) +{ + return linked_buf_len(types_buf_root); +} + +static void types_out_leb128_uint32(uint32_t v) +{ + uint8_t b; + do { + b =3D v & 0x7f; + v >>=3D 7; + if (v !=3D 0) { + b |=3D 0x80; + } + types_buf_out8(b); + } while (v !=3D 0); +} + +static void gen_func_type_call(TCGContext *s, const TCGHelperInfo *info) +{ + unsigned typemask =3D info->typemask; + int rettype =3D typemask & 7; + int vec_size =3D 0; + + if (rettype =3D=3D dh_typecode_i128) { + vec_size++; + } + for (int m =3D typemask >> 3; m; m >>=3D 3) { + int typecode =3D m & 7; + if (typecode !=3D dh_typecode_void) { + vec_size++; + } + } + + types_buf_out8(0x60); + types_out_leb128_uint32(vec_size); + + if (rettype =3D=3D dh_typecode_i128) { + types_buf_out8(0x7f); + } + + for (int m =3D typemask >> 3; m; m >>=3D 3) { + int typecode =3D m & 7; + if (typecode =3D=3D dh_typecode_void) { + continue; + } + switch (typecode) { + case dh_typecode_i32: + case dh_typecode_s32: + case dh_typecode_ptr: + types_buf_out8(0x7f); + break; + case dh_typecode_i64: + case dh_typecode_s64: + types_buf_out8(0x7e); + break; + case dh_typecode_i128: + types_buf_out8(0x7f); + break; + default: + g_assert_not_reached(); + } + } + + if ((rettype =3D=3D dh_typecode_void) || (rettype =3D=3D dh_typecode_i= 128)) { + types_buf_out8(0x0); + } else { + types_buf_out8(0x1); + switch (rettype) { + case dh_typecode_i32: + case dh_typecode_s32: + case dh_typecode_ptr: + types_buf_out8(0x7f); + break; + case dh_typecode_i64: + case dh_typecode_s64: + types_buf_out8(0x7e); + break; + default: + g_assert_not_reached(); + } + } + return; +} + typedef struct HelperInfo { struct HelperInfo *next; uint32_t idx_on_qemu; @@ -1778,6 +1901,7 @@ static void tcg_wasm_out_call(TCGContext *s, int func, int func_idx =3D get_helper_idx(s, (int)func); if (func_idx < 0) { func_idx =3D register_helper(s, (int)func); + gen_func_type_call(s, info); } =20 tcg_wasm_out_ctx_i32_load(s, tci_tb_ptr); @@ -1787,6 +1911,39 @@ static void tcg_wasm_out_call(TCGContext *s, int fun= c, gen_call(s, info, func_idx); } =20 +static void gen_func_type_qemu_ld(TCGContext *s, uint32_t oi) +{ + types_buf_out8(0x60); + types_buf_out8(0x4); + types_buf_out8(0x7f); + types_buf_out8(0x7e); + types_buf_out8(0x7f); + types_buf_out8(0x7f); + types_buf_out8(0x1); + types_buf_out8(0x7e); +} + +static void gen_func_type_qemu_st(TCGContext *s, uint32_t oi) +{ + MemOp mop =3D get_memop(oi); + + types_buf_out8(0x60); + types_buf_out8(0x5); + types_buf_out8(0x7f); + types_buf_out8(0x7e); + switch (mop & MO_SSIZE) { + case MO_UQ: + types_buf_out8(0x7e); + break; + default: + types_buf_out8(0x7f); + break; + } + types_buf_out8(0x7f); + types_buf_out8(0x7f); + types_buf_out8(0x0); +} + static void *qemu_ld_helper_ptr(uint32_t oi) { MemOp mop =3D get_memop(oi); @@ -1821,6 +1978,7 @@ static void tcg_wasm_out_qemu_ld(TCGContext *s, TCGRe= g data_reg, func_idx =3D get_helper_idx(s, helper_idx); if (func_idx < 0) { func_idx =3D register_helper(s, helper_idx); + gen_func_type_qemu_ld(s, oi); } =20 if (!addr64) { @@ -1868,6 +2026,7 @@ static void tcg_wasm_out_qemu_st(TCGContext *s, TCGRe= g data_reg, func_idx =3D get_helper_idx(s, helper_idx); if (func_idx < 0) { func_idx =3D register_helper(s, helper_idx); + gen_func_type_qemu_st(s, oi); } =20 if (!addr64) { @@ -3251,12 +3410,207 @@ static inline void tcg_target_qemu_prologue(TCGCon= text *s) { } =20 +static const uint8_t mod_1[] =3D { + 0x0, 0x61, 0x73, 0x6d, /* magic */ + 0x01, 0x0, 0x0, 0x0, /* version */ + + 0x01, /* type section */ + 0x80, 0x80, 0x80, 0x80, 0x00, /* placehodler for size */ + 0x80, 0x80, 0x80, 0x80, 0x00, /* placehodler for num of types vec */ + 0x60, /* 0: Type of "start" function */ + 0x01, 0x7f, /* arg: ctx pointer (i32) */ + 0x01, 0x7f, /* return: res (i32) */ +}; + +static const uint8_t mod_2[] =3D { + 0x02, /* import section */ + 0x80, 0x80, 0x80, 0x80, 0x00, /* placehodler for size */ + 0x80, 0x80, 0x80, 0x80, 0x00, /* placehodler for imports n= um */ + 0x03, 0x65, 0x6e, 0x76, /* module: "env" */ + 0x06, 0x62, 0x75, 0x66, 0x66, 0x65, 0x72, /* name: "buffer" */ + 0x02, 0x03, /* shared mem */ + 0x00, 0xff, 0xff, 0x03, /* min: 0, max: 0xffff pages= */ +}; + +static const uint8_t mod_3[] =3D { + 0x03, /* function section */ + 2, 1, 0x00, /* function type 0 */ + + 0x06, /* global section */ + 0x5b, /* section size */ + 18, /* num of global vars */ + 0x7e, 0x01, 0x42, 0x00, 0x0b, /* 0-cleared 64bit var */ + 0x7e, 0x01, 0x42, 0x00, 0x0b, /* 0-cleared 64bit var */ + 0x7e, 0x01, 0x42, 0x00, 0x0b, /* 0-cleared 64bit var */ + 0x7e, 0x01, 0x42, 0x00, 0x0b, /* 0-cleared 64bit var */ + 0x7e, 0x01, 0x42, 0x00, 0x0b, /* 0-cleared 64bit var */ + 0x7e, 0x01, 0x42, 0x00, 0x0b, /* 0-cleared 64bit var */ + 0x7e, 0x01, 0x42, 0x00, 0x0b, /* 0-cleared 64bit var */ + 0x7e, 0x01, 0x42, 0x00, 0x0b, /* 0-cleared 64bit var */ + 0x7e, 0x01, 0x42, 0x00, 0x0b, /* 0-cleared 64bit var */ + 0x7e, 0x01, 0x42, 0x00, 0x0b, /* 0-cleared 64bit var */ + 0x7e, 0x01, 0x42, 0x00, 0x0b, /* 0-cleared 64bit var */ + 0x7e, 0x01, 0x42, 0x00, 0x0b, /* 0-cleared 64bit var */ + 0x7e, 0x01, 0x42, 0x00, 0x0b, /* 0-cleared 64bit var */ + 0x7e, 0x01, 0x42, 0x00, 0x0b, /* 0-cleared 64bit var */ + 0x7e, 0x01, 0x42, 0x00, 0x0b, /* 0-cleared 64bit var */ + 0x7e, 0x01, 0x42, 0x00, 0x0b, /* 0-cleared 64bit var */ + 0x7e, 0x01, 0x42, 0x00, 0x0b, /* 0-cleared 64bit var */ + 0x7e, 0x01, 0x42, 0x00, 0x0b, /* 0-cleared 64bit var */ + + 0x07, /* export section */ + 13, /* size of section */ + 1, /* num of funcs */ + 0x05, 0x73, 0x74, 0x61, 0x72, 0x74, /* "start" function */ + 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, /* placeholder for func index*/ +}; + +static const uint8_t mod_4[] =3D { + 0x0a, /* code section */ + 0x80, 0x80, 0x80, 0x80, 0x00, /* placeholder for section size*/ + 1, /* num of codes */ + 0x80, 0x80, 0x80, 0x80, 0x00, /* placeholder for code size */ + 0x2, 0x2, 0x7f, 0x1, 0x7e, /* variables (32bit*2, 64bit*1) */ +}; + +static int write_mod_1(TCGContext *s) +{ + void *base =3D s->code_ptr; + int helpers_num =3D helpers_len(); + + if (unlikely(((void *)s->code_ptr + sizeof(mod_1) + types_buf_len()) + > s->code_gen_highwater)) { + return -1; + } + + memcpy(s->code_ptr, mod_1, sizeof(mod_1)); + s->code_ptr +=3D sizeof(mod_1); + linked_buf_write(types_buf_root, s->code_ptr); + s->code_ptr +=3D types_buf_len(); + + uint32_t type_section_size =3D types_buf_len() + 10; + fill_uint32_leb128(base + 9, type_section_size); + fill_uint32_leb128(base + 14, HELPER_IDX_START + helpers_num + 1); + + return 0; +} + +static int write_mod_2(TCGContext *s) +{ + void *base =3D s->code_ptr; + int helpers_num =3D helpers_len(); + + if (unlikely(((void *)s->code_ptr + sizeof(mod_2)) + > s->code_gen_highwater)) { + return -1; + } + + tcg_debug_assert(helpers_num <=3D INT_MAX); + memcpy(s->code_ptr, mod_2, sizeof(mod_2)); + s->code_ptr +=3D sizeof(mod_2); + for (int i =3D 0; i < helpers_num; i++) { + int typeidx =3D HELPER_IDX_START + i + 1; + char buf[11]; /* enough for decimal int max + NULL*/ + int n =3D snprintf(buf, sizeof(buf), "%d", i); + tcg_debug_assert(n < sizeof(buf)); + *(uint8_t *)s->code_ptr++ =3D 6; /* helper */ + *(uint8_t *)s->code_ptr++ =3D 0x68; + *(uint8_t *)s->code_ptr++ =3D 0x65; + *(uint8_t *)s->code_ptr++ =3D 0x6c; + *(uint8_t *)s->code_ptr++ =3D 0x70; + *(uint8_t *)s->code_ptr++ =3D 0x65; + *(uint8_t *)s->code_ptr++ =3D 0x72; + s->code_ptr +=3D write_uint32_leb128((uint8_t *)s->code_ptr, n); + memcpy(s->code_ptr, buf, n); + s->code_ptr +=3D n; + *(uint8_t *)s->code_ptr++ =3D 0x00; /* type(0) */ + s->code_ptr +=3D write_uint32_leb128((uint8_t *)s->code_ptr, typei= dx); + if (unlikely(((void *)s->code_ptr > s->code_gen_highwater))) { + return -1; + } + } + + uint32_t import_section_size =3D (int)s->code_ptr - (int)base - 6; + fill_uint32_leb128(base + 1, import_section_size); + fill_uint32_leb128(base + 6, HELPER_IDX_START + helpers_num + 1); + + return 0; +} + +static int write_mod_3(TCGContext *s) +{ + void *base =3D s->code_ptr; + + if (unlikely(((void *)s->code_ptr + sizeof(mod_3)) + > s->code_gen_highwater)) { + return -1; + } + + memcpy(s->code_ptr, mod_3, sizeof(mod_3)); + s->code_ptr +=3D sizeof(mod_3); + + int startidx =3D HELPER_IDX_START + helpers_len(); + fill_uint32_leb128(base + 107, startidx); + + return 0; +} + +static int write_mod_4(TCGContext *s) +{ + void *base =3D s->code_ptr; + + if (unlikely(((void *)s->code_ptr + sizeof(mod_4)) + > s->code_gen_highwater)) { + return -1; + } + + memcpy(s->code_ptr, mod_4, sizeof(mod_4)); + s->code_ptr +=3D sizeof(mod_4); + + int code_size =3D sub_buf_len() + 5; + fill_uint32_leb128(base + 1, code_size + 6); + fill_uint32_leb128(base + 7, code_size); + + return 0; +} + +static int write_mod_code(TCGContext *s) +{ + void *base =3D s->code_ptr; + int code_size =3D sub_buf_len(); + + if (unlikely(((void *)s->code_ptr + code_size) > s->code_gen_highwater= )) { + return -1; + } + linked_buf_write(sub_buf_root, s->code_ptr); + s->code_ptr +=3D code_size; + for (BlockPlaceholder *p =3D block_placeholder; p; p =3D p->next) { + uint8_t *ph =3D p->pos + base; + int blk =3D get_block_of_label(p->label); + tcg_debug_assert(blk >=3D 0); + fill_uint32_leb128(ph, blk); + } + + return 0; +} + static void tcg_out_tb_start(TCGContext *s) { + int size; + struct wasmTBHeader *h; + init_sub_buf(); init_blocks(); init_label_info(); init_helpers(); + init_types_buf(); + + /* TB starts from a header */ + h =3D (struct wasmTBHeader *)(s->code_ptr); + s->code_ptr +=3D sizeof(struct wasmTBHeader); + + /* Followed by TCI code */ + h->tci_ptr =3D s->code_ptr; =20 /* generate wasm code to initialize fundamental registers */ tcg_wasm_out_op_global_get_r(s, TCG_AREG0); @@ -3283,11 +3637,47 @@ static void tcg_out_tb_start(TCGContext *s) =20 static int tcg_out_tb_end(TCGContext *s) { + int res; + struct wasmTBHeader *h =3D (struct wasmTBHeader *)(s->code_buf); + tcg_wasm_out_op_end(s); /* end if */ tcg_wasm_out_op_end(s); /* end loop */ tcg_wasm_out8(s, 0x0); /* unreachable */ tcg_wasm_out_op_end(s); /* end func */ =20 + /* write wasm blob */ + h->wasm_ptr =3D s->code_ptr; + res =3D write_mod_1(s); + if (res < 0) { + return res; + } + res =3D write_mod_2(s); + if (res < 0) { + return res; + } + res =3D write_mod_3(s); + if (res < 0) { + return res; + } + res =3D write_mod_4(s); + if (res < 0) { + return res; + } + res =3D write_mod_code(s); + if (res < 0) { + return res; + } + h->wasm_size =3D (int)s->code_ptr - (int)h->wasm_ptr; + + /* record imported helper functions */ + if (unlikely(((void *)s->code_ptr + helpers_len() * 4) + > s->code_gen_highwater)) { + return -1; + } + h->import_ptr =3D s->code_ptr; + s->code_ptr +=3D helpers_copy((uint32_t *)s->code_ptr); + h->import_size =3D (int)s->code_ptr - (int)h->import_ptr; + return 0; } =20 --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747746486; cv=none; d=zohomail.com; s=zohoarc; b=Le4gtv1UPic4greMwmwA/mhmvqV/Dv3qwXVYIaMgOttaWoQ9sUv2doD2lnH9CHT7PRYCAmGYNUUm21n63uWBMI2aB5Xqii5frgIstxM1sH2NjbRgdfHCKYgh57vkwBKJfz3QS1u3t3p3dZVNdAlvwcPKMPJl/vJGk1cTt7hQKSA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747746486; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=s7pn51WkI2VfDBAIgVkjLNvwgbH/QMeYb332RfFRnmc=; b=mjmSUqRCdFY8Yi1VS0DbiYWoRMdTThwJIhw8u11RLGgVnFjAjv+NYerdCdkkZp0TC2TBpzmdQ7+fHGuON1vvwiILJUyEuYxA2AAFVdhDXYHmguVE96CL5wrNaUvH+4ZschvnXW65nlHTZMCy7uWKjQfxaoIRFXpWEIOdsZcWNzY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 174774648594466.22382390138705; Tue, 20 May 2025 06:08:05 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMVj-0006R0-Qx; Tue, 20 May 2025 08:55:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMUq-0003yW-EB; Tue, 20 May 2025 08:55:05 -0400 Received: from mail-pj1-x1032.google.com ([2607:f8b0:4864:20::1032]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMUj-00015x-Me; Tue, 20 May 2025 08:55:03 -0400 Received: by mail-pj1-x1032.google.com with SMTP id 98e67ed59e1d1-30e87549254so5713647a91.2; Tue, 20 May 2025 05:54:55 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.54.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:54:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745694; x=1748350494; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=s7pn51WkI2VfDBAIgVkjLNvwgbH/QMeYb332RfFRnmc=; b=JPjLrzDtlYr/Fwyd131Qf+KRdRdrKxoGy2EJn4qZi71S2S7CMWVZ9wxSPjhh1EXtNf si75N9ZfJQgGbOg6xpSaBQjW7HISrTpZq81mbqfvbGqvCeG8YxBBdXt5huR7mjuaccxj HURWFpir9qrS2aINbAke1PKux7T6vVZmh0M60Id+9gHefmix3jTypto+3ip1ly6tzACT KEYj2OOhK0/a/tYZs1ThNsIPh4XEboXLYfz2kvDwjZCbtWMlTO4o3SsvXhcQQvj+8R8B qDKmwL0ATKIYt/3cPsOtCgNL7i/ZGU1faavA9Zt3wcQfjGhaPyLR2T9DNOCUsEW50mAC LlQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745694; x=1748350494; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=s7pn51WkI2VfDBAIgVkjLNvwgbH/QMeYb332RfFRnmc=; b=P/WlCJfkw3DM9SXWgcZvoh6e+H5ub0+vEoFb6GRbvsIVcDU7u4NXsB/i1RYRVBl1Yt kWu210PAJ7Pd/o13wUD5qSwd6snupOF1UF367rJPe8DL9WoOfFqzMBkE9Yo9fDOEcIse ZKrmnqDxe5IkvWGLvgaVWXIQ7Roa+T7fW8NniXLFPjU3w9LNIsSv+VzJYkoR8RenECAN t9BC9NgGBhGrdRw4MyFfTUbeIIYBAYVWXm+aJLQ5jpXpBbMHPqciY1rd8aV/1Uw5y/yG bmBQoIuabFy1obskdbTimSsM69tuB/hyeEKzb9/Q3TZ3dOiqtngM7J05pa5jeGyIY5Cu r3cQ== X-Forwarded-Encrypted: i=1; AJvYcCWR0Kq2fHf1qLL6ew6rdWPvUkdsCKGVXLicvUdoHS7gm2hCdpFh7U1KEw/7uaCvIbfr+hfuVnJ8vQ==@nongnu.org, AJvYcCXTkGnhcIfQIYQ8olbYjH4l6vNVG0xq1E4Klc2rlf/JuDfYXlNWxOakypklwaUmXqP67vWfC3Mvm3Zxyg==@nongnu.org X-Gm-Message-State: AOJu0Yyzp3b2VjSyHbo4zkTdyFMaoS/HkFn2XComtNllnel5bRUDr6KL m0BKU4mqRvLlyjrmxTy0b1xO+c95m7OO5XdL1vsSCsoRa5UcwbO4Mvf5fZm5T2F8 X-Gm-Gg: ASbGncs1lpkHWjxJ9XDJvtL8m4ztux8vnsa06lhIJSd9J4CYExvk3Pq+oG77bUVFyJ+ hGOS8wd9DFi2ZJo88AblK9RmA26ElP90/d0Qi2Gz+HyzyIb2YSx4rsSa7T1MAT1sHtH/nXQAcIC Jr/odpKg8vCRNnDMHQ8cz/yVZJxcJGBiDddS7VX6ZL3ISSZwDmDYiXSKfHVIL7osArVrVop4rvX ajfGzAYVMQZrTc552frESkexQDvia5EOdSNk6O4EvQIPU5BbqlUMmrGOyNBB2LKgi81ZeCvL7pX /X8bwp94UwqNQpgHlbFUBxsxcmixL0Du86mkF+f3P2jiSMp7SELohpSstq+gaw== X-Google-Smtp-Source: AGHT+IGajpd6KWApJBhXUBZOQ39gUNizGUA6mw1/uq8/j3XxCq6FiAhKcVdhKBCbeeWVn1V+o+KQDA== X-Received: by 2002:a17:90b:3f4c:b0:2ee:5958:828 with SMTP id 98e67ed59e1d1-30e7d522155mr29183315a91.9.1747745693818; Tue, 20 May 2025 05:54:53 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 28/33] tcg/wasm32: Implement instantiation of Wasm binary Date: Tue, 20 May 2025 21:51:30 +0900 Message-ID: <2433bd9fe6eed2f93f511195cedbfde0fc4a89a4.1747744132.git.ktokunaga.mail@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1032; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pj1-x1032.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747746487927116600 Content-Type: text/plain; charset="utf-8" instantiate_wasm is a function that instantiates a TB's Wasm binary, importing the functions as specified by its arguments. Following the header definition in wasm32/tcg-target.c.inc, QEMU's memory is imported into the module as "env.buffer", and helper functions are imported as "helper.". 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. Note Since FireFox 138, WebAssembly.Module no longer accepts a SharedArrayBuffer as input [2] as reported by Nicolas Vandeginste in my downstream fork[3]. 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/Inter= acting-with-code.html#calling-javascript-functions-as-function-pointers-fro= m-c [2] https://bugzilla.mozilla.org/show_bug.cgi?id=3D1965217 [3] https://github.com/ktock/qemu-wasm/pull/25 Signed-off-by: Kohei Tokunaga --- tcg/wasm32.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tcg/wasm32.c b/tcg/wasm32.c index b238ccf6d6..f2269c0a22 100644 --- a/tcg/wasm32.c +++ b/tcg/wasm32.c @@ -25,6 +25,7 @@ #include "disas/dis-asm.h" #include "tcg-has.h" #include +#include =20 =20 #define ctpop_tr glue(ctpop, TCG_TARGET_REG_BITS) @@ -44,6 +45,29 @@ =20 __thread uintptr_t tci_tb_ptr; =20 +EM_JS(int, instantiate_wasm, (int wasm_begin, + int wasm_size, + int import_vec_begin, + int import_vec_size), +{ + const memory_v =3D new DataView(HEAP8.buffer); + const wasm =3D HEAP8.subarray(wasm_begin, wasm_begin + wasm_size); + var helper =3D {}; + for (var i =3D 0; i < import_vec_size / 4; i++) { + helper[i] =3D wasmTable.get( + memory_v.getInt32(import_vec_begin + i * 4, true)); + } + const mod =3D new WebAssembly.Module(new Uint8Array(wasm)); + const inst =3D new WebAssembly.Instance(mod, { + "env" : { + "buffer" : wasmMemory, + }, + "helper" : helper, + }); + + return 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) { --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747746221; cv=none; d=zohomail.com; s=zohoarc; b=QCA+wGVC8CJwPn/PLSyKVVFJLTgFRnm1Tnf+OmCTkAr7x9MrOU5RNDbOMB8v2BuePvcSgpFB6EVezyOyXONk5Efewc8aUx0x0+G4h9LbYgy2njcxa0FqDOIrAar8lczgqLkR3gHPaBP2Hj4QVY1V3s6y5I4SP8dqXulAenxXE2o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747746221; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=ldWym2VLMHax4CE85S7n09RdQWchgzVNNktVRnfbMqs=; b=csidpbGJNszTSqSzONVVkraMESG1M3tVPqxGWxQv4tCiUwOnQgGQ8jxVZpRGKitEneupD4iDn05qEqWCkevyngsNLLYH5AqHNV4iUoThe7u7pCDyk87AYFe+VVndW+Lj+hPHiAoW8n4Y+U27g3na7qp51Nb6h3bK9VatGDGCJBI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747746220238261.3416582510288; Tue, 20 May 2025 06:03:40 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMVo-0006g8-Af; Tue, 20 May 2025 08:56:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMUu-0004B6-0k; Tue, 20 May 2025 08:55:10 -0400 Received: from mail-pg1-x534.google.com ([2607:f8b0:4864:20::534]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMUq-00016y-ES; Tue, 20 May 2025 08:55:07 -0400 Received: by mail-pg1-x534.google.com with SMTP id 41be03b00d2f7-b26f5cd984cso3920614a12.3; Tue, 20 May 2025 05:55:01 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.54.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:54:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745700; x=1748350500; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ldWym2VLMHax4CE85S7n09RdQWchgzVNNktVRnfbMqs=; b=BAhMoekwQS/pYCSpZhpaUVpWPuVMIywB9qxgYclvl8akkEvbXMiVG1OqEaUDqa3JM1 pmGp6/zbnncnSsjSBQb/HVJTfIaZfbGDedvpXUtrB9RkEF/Y+uZMYjrpsvXD+y3CM0B3 oZYNL3udS8hSGofdS6axIyMg2/UTPqmcHIjSTi09XdYCNdcJbfTmpPYXUn21zL93nnXT otFKIdZ7xeuSgwCKR4AJSBbSz0fs7oAQL7obfXt7buj/jKtqSiOIN6AEdv6Kx+vi62dG ApdJM+R2YqE1jbZP0TZT1gG7X0cRHF6SzfH7LDB9+/3vq0UEEF7YipBWgo061jq94R8t 55wA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745700; x=1748350500; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ldWym2VLMHax4CE85S7n09RdQWchgzVNNktVRnfbMqs=; b=JP9MVLT60tKiX9DA6x6Uj5kNuwn7nr57lff/BtQemwrOcZb6w44HpUFk6GsyXahuQI sZ9TZdYmuBlTex1dzZQQl6Y86UuEn+teqd4hvJnHArz2nXQgMXrSxGd3WuUmCg8fhUh4 xiyNP9MP6TKl7UsxzsJR1I6l1AlIAuKo2qEUsURDFq4C+CE+4V6Kv74LEYT6mUnRddQZ G5pO8+pX0ka2e0ivNH1EWf9dZw6sXkrgWDOmobiepw9HDcSE851N4klg1HHnP2GDTxSe LyONUk+4PLyYclIN71P0POnK24lHJFMuTqBcxTFMA6nd2ngtXAzqxN2RJFOzRAwzUBh9 U4QQ== X-Forwarded-Encrypted: i=1; AJvYcCWvoYLOu+p1NodwMytM9U2lAWdOPapSKMbmT8P13jIs3lKGI6CpJWb4ybg61V3pQOZeEE3aPAskJ3G7HQ==@nongnu.org, AJvYcCXD2BetYAy+dnIR1i4m4Ecr9ODauymQwHGdZblVF5+F25QV8nk4aoP2booKbAgk1/2NGwZcoqFMzA==@nongnu.org X-Gm-Message-State: AOJu0Yw9yodpszRoZYnz7NyqlFYXS8ks9p+sw+NeeKDbVIwZ8MhJRtTu UCpX3feKIKu5mtlcDqr4GcS63rWfzv0+EWTGvFZCiNK7sEzdPPjScbGiJ006q+8M X-Gm-Gg: ASbGncsn7kx4zAnVgqPkyba1tT3/XuL8UtdApOvB3NqVEpTQoXZXNa++eI+WKD9FcAL jKROZfTWseaHnzU03DsJrTSIDNhcPRgKUpU/TKz+ftIp4KR3uxHWu3Ulh/HMFCiQTtUpYytz/wH QMFwpUyh5fuDj5EvtDXgocYF5BgM1egtfHf+id2OkEcCQCGls2/f2tGXeTkVW4CF/x7DGBtyjv2 QSF2Wr+orlzv6oj2o/PjdfRJ42L8FPfiNKGeuCCBLF8Cwp6YhXj1/SpU0I1wn0eYZjK7aBCmokG f37vdaa+OYCxCxNicJe0wW5742cqJjlkMrRPY4EYIPbvZjZlb0U= X-Google-Smtp-Source: AGHT+IErjVtBwvRt2kikcLU8V+gM9yO/qzQGjvrVXya4JW8hWhPZkE2oRWPWRlCaDe3Zlo+61szrMA== X-Received: by 2002:a17:90b:4b0f:b0:2f6:be57:49d2 with SMTP id 98e67ed59e1d1-30e7d545920mr29668717a91.17.1747745699558; Tue, 20 May 2025 05:54:59 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 29/33] tcg/wasm32: Allow Asyncify unwinding from TB Date: Tue, 20 May 2025 21:51:31 +0900 Message-ID: <39e1655832058b6c16fde756d96e90bb7eae1db0.1747744132.git.ktokunaga.mail@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::534; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pg1-x534.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747746233183116600 Content-Type: text/plain; charset="utf-8" Emscripten's Fiber coroutine implements coroutine switching using the stack unwinding and rewinding capabilities of Asyncify [1]. When a coroutine yields (i.e. switches out), Asyncify unwinds the stack, returning control to Emscripten's JS code (Fiber.trampoline()), which then performs stack rewinding to resume execution in the target coroutine. Stack unwinding is implemented by a sequence of immediate function returns, while rewinding works by re-entering the functions in the call stack, skipping any code between the top of the function and the original call position [2]. This commit modifies the Wasm TB modules to support Fiber coroutines. Assuming the TCG CPU loop is executed by only one coroutine per thread, a TB module must allow helper functions to unwind and be resumed via rewinding. Specifically: - When a helper returns due to an unwind, the module must immediately return to its caller, allowing unwinding to propagate. - When being called again for a rewind, the module must skip any code between the top of the function and the call position that triggered the unwind, and directly enter the helper. To support this: - TBs now check the Asyncify.state JS object after each helper call. If unwinding is in progress, the TB immediately returns control to the caller. - Each function call is preceded by a block boundary and an update of the BLOCK_PTR_IDX variable. This enables the TB to re-enter execution at the correct point during a rewind, skipping earlier blocks. Additionally, this commit introduces wasmContext.do_init which is a flag indicating whether the TB should reset the BLOCK_PTR_IDX variable to 0 (i.e. start from the beginning). In call_wasm_tb, this is always set (ctx.do_init =3D 1) to ensure normal TB execution begins at the first block. Once the TB resets the BLOCK_PTR_IDX variable, it also clears do_init. During a rewind, the C code does not set ctx.do_init to 1, allowing the TB to preserve the BLOCK_PTR_IDX value from the previous unwind 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-asynci= fy Signed-off-by: Kohei Tokunaga --- tcg/wasm32.c | 3 ++ tcg/wasm32.h | 11 ++++++++ tcg/wasm32/tcg-target.c.inc | 56 +++++++++++++++++++++++++++++++++++-- 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/tcg/wasm32.c b/tcg/wasm32.c index f2269c0a22..e6a3dbf750 100644 --- a/tcg/wasm32.c +++ b/tcg/wasm32.c @@ -53,6 +53,9 @@ EM_JS(int, instantiate_wasm, (int wasm_begin, const memory_v =3D new DataView(HEAP8.buffer); const wasm =3D HEAP8.subarray(wasm_begin, wasm_begin + wasm_size); var helper =3D {}; + helper.u =3D () =3D> { + return (Asyncify.state !=3D Asyncify.State.Unwinding) ? 1 : 0; + }; for (var i =3D 0; i < import_vec_size / 4; i++) { helper[i] =3D wasmTable.get( memory_v.getInt32(import_vec_begin + i * 4, true)); diff --git a/tcg/wasm32.h b/tcg/wasm32.h index f8651af4ee..f2749f1e0e 100644 --- a/tcg/wasm32.h +++ b/tcg/wasm32.h @@ -32,11 +32,22 @@ struct wasmContext { * Pointer to a stack array. */ uint64_t *stack; + + /* + * Flag indicates whether to initialize basic registers(1) or not(0). + */ + uint32_t do_init; }; =20 /* Instantiated Wasm function of a TB */ typedef int32_t (*wasm_tb_func)(struct wasmContext *); =20 +static inline int32_t call_wasm_tb(wasm_tb_func f, struct wasmContext *ctx) +{ + ctx->do_init =3D 1; /* reset block index (rewinding will skip this) */ + return f(ctx); +} + /* * TB of wasm backend starts from a header which stores pointers for each = data * stored in the following region in the TB. diff --git a/tcg/wasm32/tcg-target.c.inc b/tcg/wasm32/tcg-target.c.inc index d9a3abae70..04cd9b6e4a 100644 --- a/tcg/wasm32/tcg-target.c.inc +++ b/tcg/wasm32/tcg-target.c.inc @@ -135,7 +135,8 @@ static const uint8_t tcg_target_reg_index[TCG_TARGET_NB= _REGS] =3D { #define TMP64_LOCAL_0_IDX 3 =20 /* function index */ -#define HELPER_IDX_START 0 /* helper funcitons */ +#define CHECK_UNWINDING_IDX 0 /* a funtion of checking Asyncify status */ +#define HELPER_IDX_START 1 /* helper funcitons */ =20 #define BUF_SIZE 1024 typedef struct LinkedBuf { @@ -1895,6 +1896,17 @@ static int get_helper_idx(TCGContext *s, int helper_= idx_on_qemu) return -1; } =20 +static void tcg_wasm_out_handle_unwinding(TCGContext *s) +{ + tcg_wasm_out_op_call(s, CHECK_UNWINDING_IDX); + tcg_wasm_out_op_i32_eqz(s); + tcg_wasm_out_op_if_noret(s); + tcg_wasm_out_op_i32_const(s, 0); + /* returns if unwinding */ + tcg_wasm_out_op_return(s); + tcg_wasm_out_op_end(s); +} + static void tcg_wasm_out_call(TCGContext *s, int func, const TCGHelperInfo *info) { @@ -1908,7 +1920,16 @@ static void tcg_wasm_out_call(TCGContext *s, int fun= c, tcg_wasm_out_op_i32_const(s, (int32_t)s->code_ptr); tcg_wasm_out_op_i32_store(s, 0, 0); =20 + /* + * update the block index so that the possible rewinding will + * skip this block + */ + tcg_wasm_out_op_i64_const(s, block_idx + 1); + tcg_wasm_out_op_global_set(s, BLOCK_PTR_IDX); + tcg_wasm_out_new_block(s); + gen_call(s, info, func_idx); + tcg_wasm_out_handle_unwinding(s); } =20 static void gen_func_type_qemu_ld(TCGContext *s, uint32_t oi) @@ -1986,6 +2007,14 @@ static void tcg_wasm_out_qemu_ld(TCGContext *s, TCGR= eg data_reg, addr_reg =3D TCG_REG_TMP; } =20 + /* + * update the block index so that the possible rewinding will + * skip this block + */ + tcg_wasm_out_op_i64_const(s, block_idx + 1); + tcg_wasm_out_op_global_set(s, BLOCK_PTR_IDX); + tcg_wasm_out_new_block(s); + /* call helper */ tcg_wasm_out_op_global_get_r(s, TCG_AREG0); tcg_wasm_out_op_i32_wrap_i64(s); @@ -1995,6 +2024,7 @@ static void tcg_wasm_out_qemu_ld(TCGContext *s, TCGRe= g data_reg, =20 tcg_wasm_out_op_call(s, func_idx); tcg_wasm_out_op_global_set_r(s, data_reg); + tcg_wasm_out_handle_unwinding(s); } =20 static void *qemu_st_helper_ptr(uint32_t oi) @@ -2034,6 +2064,14 @@ static void tcg_wasm_out_qemu_st(TCGContext *s, TCGR= eg data_reg, addr_reg =3D TCG_REG_TMP; } =20 + /* + * update the block index so that the possible rewinding will + * skip this block + */ + tcg_wasm_out_op_i64_const(s, block_idx + 1); + tcg_wasm_out_op_global_set(s, BLOCK_PTR_IDX); + tcg_wasm_out_new_block(s); + /* call helper */ tcg_wasm_out_op_global_get_r(s, TCG_AREG0); tcg_wasm_out_op_i32_wrap_i64(s); @@ -2051,6 +2089,7 @@ static void tcg_wasm_out_qemu_st(TCGContext *s, TCGRe= g data_reg, tcg_wasm_out_op_i32_const(s, (int32_t)s->code_ptr); =20 tcg_wasm_out_op_call(s, func_idx); + tcg_wasm_out_handle_unwinding(s); } =20 static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, @@ -3420,6 +3459,9 @@ static const uint8_t mod_1[] =3D { 0x60, /* 0: Type of "start" function */ 0x01, 0x7f, /* arg: ctx pointer (i32) */ 0x01, 0x7f, /* return: res (i32) */ + 0x60, /* 1: Type of the asyncify helper */ + 0x0, /* no argument */ + 0x01, 0x7f, /* return: res (i32) */ }; =20 static const uint8_t mod_2[] =3D { @@ -3430,6 +3472,9 @@ static const uint8_t mod_2[] =3D { 0x06, 0x62, 0x75, 0x66, 0x66, 0x65, 0x72, /* name: "buffer" */ 0x02, 0x03, /* shared mem */ 0x00, 0xff, 0xff, 0x03, /* min: 0, max: 0xffff pages= */ + 0x06, 0x68, 0x65, 0x6c, 0x70, 0x65, 0x72, /* module: "helper" */ + 0x01, 0x75, /* name: "u" */ + 0x00, 0x01, /* func type 1 */ }; =20 static const uint8_t mod_3[] =3D { @@ -3488,7 +3533,7 @@ static int write_mod_1(TCGContext *s) linked_buf_write(types_buf_root, s->code_ptr); s->code_ptr +=3D types_buf_len(); =20 - uint32_t type_section_size =3D types_buf_len() + 10; + uint32_t type_section_size =3D types_buf_len() + 14; fill_uint32_leb128(base + 9, type_section_size); fill_uint32_leb128(base + 14, HELPER_IDX_START + helpers_num + 1); =20 @@ -3613,6 +3658,11 @@ static void tcg_out_tb_start(TCGContext *s) h->tci_ptr =3D s->code_ptr; =20 /* generate wasm code to initialize fundamental registers */ + tcg_wasm_out_ctx_i32_load(s, do_init); + tcg_wasm_out_op_i32_const(s, 0); + tcg_wasm_out_op_i32_ne(s); + tcg_wasm_out_op_if_noret(s); + tcg_wasm_out_op_global_get_r(s, TCG_AREG0); tcg_wasm_out_op_i64_eqz(s); tcg_wasm_out_op_if_noret(s); @@ -3626,8 +3676,10 @@ static void tcg_out_tb_start(TCGContext *s) tcg_wasm_out_op_global_set_r(s, TCG_REG_CALL_STACK); tcg_wasm_out_op_end(s); =20 + tcg_wasm_out_ctx_i32_store_const(s, do_init, 0); tcg_wasm_out_op_i64_const(s, 0); tcg_wasm_out_op_global_set(s, BLOCK_PTR_IDX); + tcg_wasm_out_op_end(s); =20 tcg_wasm_out_op_loop_noret(s); tcg_wasm_out_op_global_get(s, BLOCK_PTR_IDX); --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747746302; cv=none; d=zohomail.com; s=zohoarc; b=en4SOqXqTX3KhJO1qEX5v3fHiCZk/vf2zGdJWZr2i0n3LRBBp+WcduWu5gtyXtMDT0Iatr4yoRl7H3UAagtAE8exQ+yDxjq8otgWl8neYXH6ap6WnBQ96JKNaaMhkEzasmJySTxOcAIUtquHkDRux17Eez8HjJZkuF1AK4r1Ecs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747746302; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=CBZg2rkxk5ny0INYpq4DIhkYLh/SGhQqukUB+EhzpVk=; b=OAw5juBnR49RgOtEg+TQ+XjMYREbMkGW98EunMW79gC9SCL2Go6obqfE/+/Tz71kLkgbARzpT5Zj5mAy89M6T+BG2SVqUf1LsmavZkZh8BplsPLQLggVm1+Q3tts3gZ6Url/bzCH1rrMS0EbzmnO/2S9Xq723/gE5gn814h6CQE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747746302065742.8818106378352; Tue, 20 May 2025 06:05:02 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMVe-0005sv-3C; Tue, 20 May 2025 08:55:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMV1-0004Zv-IV; Tue, 20 May 2025 08:55:16 -0400 Received: from mail-pj1-x1032.google.com ([2607:f8b0:4864:20::1032]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMUy-0001J3-2i; Tue, 20 May 2025 08:55:15 -0400 Received: by mail-pj1-x1032.google.com with SMTP id 98e67ed59e1d1-30ea7770bd2so4208220a91.0; Tue, 20 May 2025 05:55:06 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.54.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:55:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745705; x=1748350505; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=CBZg2rkxk5ny0INYpq4DIhkYLh/SGhQqukUB+EhzpVk=; b=QqVuH3DBkBKxU+hXy9ZrgxQSehxYxLGZ5LZO54a1qtl1P/sMCPCaIns3nLOFNHmeA1 +aZCH5yB16YT4kalzPUGMVc/VESDGfaZ213d8II0NOZMS/MTetYc1W7LrhGQUPs7g6HQ BH4rmNuoGv3cZOffPQjT59wPVrGZppcjledk3WScxDeUMhlNVyUmUtrNHx3IOFr8704T xG+Bdn0AOFYI/UeR9xapmFGI+aa8BRgYu3dGwg7JcBwt5W1HQgfMW/KPyJHkYiaxAJ8s bS2Pc5LrlgQVA5BuH2ds1r321qVRYIpJPt2W8LtgFFpXLbzniDI0VLHBm0Y8gnD+lT1K jA/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745705; x=1748350505; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=CBZg2rkxk5ny0INYpq4DIhkYLh/SGhQqukUB+EhzpVk=; b=j8LyALEraS2XxQKWb1/hhsYYuykQOKgibLZxqW4SD4ru8ItXJ4scVtYjFn5QEu0h25 r+BOmAalP414BqCmd0r0hzRJZZHnmmyrBiLQieAuXy/9pZJ9HmVo0NgFPh/zfb4UiKtD M6OJ0tCTnv4kBX6kN+FWOa3qh8IbxopY0eLoVvjMquJbORJmO9YoHszo7mQLzQNZvWNB jIGTX2OSsBUdwcdPoFMGMHnNa7Yzys2WdCxwdgonIh+gmTP7pYQGKC2YSBMRAYiD2AZb SYf4zyn4wysysS+UGKtz8tzPjYSCrBQVRb9nXoI4PU5E04aW++R1Aj2fSFlzFex0NSA8 sW0Q== X-Forwarded-Encrypted: i=1; AJvYcCVu6rBQYsC01esdzVVYn5s/6k6vNAYsbMwWVTAWW0fxFTU4aW8k+duu6ebHLj3I/K+lXZbKgg7y8AsJtA==@nongnu.org, AJvYcCWyUQbu2eD1rEYAsi13f0ArJ86uto5rrptqwZhU0sm73Q6PlRucE2D4k4mbnEK1QD44wlvxLrCTng==@nongnu.org X-Gm-Message-State: AOJu0YzPN2oSpclQNtFUkIcq/ys+v6aFIQQo/iJJIt3w5QqRDWWGPzNb I1gd+YcR1SZ9Z20G8t/Pn7rN/ebl2RG1/aOz3Q0XLWOrGQ67RVeboKUg28L1BpB8 X-Gm-Gg: ASbGncsDdCkiDoUGy3Hp4IR0ONCC2a6Q2G1GOcdTiTa6mAt/yvHAN4QN1Wu7zyArerd D9HwPj4fy5hXXa6X8naTUHNe1Az2CRUFotPeaDqQiCYoWvYkpF25D/Fi7Z/TiA9hHBv7gUlbLTj 6MmGOb/m4108Qe7h4iPxvIK/4GVh0MWq7Z7lzu56muBJUmLIifX/cAxuDGVI/l0UNnhWxgZ3y6O ve3T0HdZuDX0PsY3wcG5+4Eas5/jbXdPDuHSAM6QBcZUEV5dKE6oCL0/Us/g28PsWrsYvPB0kCP cNjR+IZ7KKUSHFWHEoZYV9jwQz9dceyE/MAnSjdve8gBymMcgIo= X-Google-Smtp-Source: AGHT+IFVDwR9OMJl+zS8v+j14qOrqSC22HwHzudg6SLEv6QJtDFsh1QVYN472BwElteO3wpZaIh/uQ== X-Received: by 2002:a17:90b:3905:b0:2ee:5bc9:75c3 with SMTP id 98e67ed59e1d1-30e7d501dd0mr21918201a91.5.1747745705216; Tue, 20 May 2025 05:55:05 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 30/33] tcg/wasm32: Enable instantiation of TBs executed many times Date: Tue, 20 May 2025 21:51:32 +0900 Message-ID: <75d5017b4af1853c2924a21b406792c6154ac08e.1747744132.git.ktokunaga.mail@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1032; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pj1-x1032.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747746304579116600 Content-Type: text/plain; charset="utf-8" This commit enables instantiations of TBs in wasm32.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 max 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 via wasmTBHeader's info_ptr (or its array if there are multiple threads). This allows tcg_qemu_tb_exec to resolve the instance function pointer from TB. When a new instantiation risks exceeding the limit, the Wasm backend doesn't perform the instantiation (i.e. TB continues to be executed on TCI), instead, removal of older Wasm instances is triggered using Emscripten's removeFunction function. Once the removal of the instance is detected via FinalizationRegistry API[1], instances_global is decremented, which allows instantiation of new modules again. [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Globa= l_Objects/FinalizationRegistry Signed-off-by: Kohei Tokunaga --- tcg/wasm32.c | 241 +++++++++++++++++++++++++++++++++++- tcg/wasm32.h | 45 +++++++ tcg/wasm32/tcg-target.c.inc | 17 +++ 3 files changed, 299 insertions(+), 4 deletions(-) diff --git a/tcg/wasm32.c b/tcg/wasm32.c index e6a3dbf750..fe6f2f17da 100644 --- a/tcg/wasm32.c +++ b/tcg/wasm32.c @@ -26,6 +26,7 @@ #include "tcg-has.h" #include #include +#include "wasm32.h" =20 =20 #define ctpop_tr glue(ctpop, TCG_TARGET_REG_BITS) @@ -45,6 +46,9 @@ =20 __thread uintptr_t tci_tb_ptr; =20 +/* TBs executed more than this value will be compiled to wasm */ +#define INSTANTIATE_NUM 1500 + EM_JS(int, instantiate_wasm, (int wasm_begin, int wasm_size, int import_vec_begin, @@ -68,6 +72,8 @@ EM_JS(int, instantiate_wasm, (int wasm_begin, "helper" : helper, }); =20 + Module.__wasm32_tb.inst_gc_registry.register(inst, "tbinstance"); + return addFunction(inst.exports.start, 'ii'); }); =20 @@ -353,16 +359,44 @@ static void tci_qemu_st(CPUArchState *env, uint64_t t= addr, uint64_t val, } } =20 +__thread int 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 *i= nfo) +{ + set_info(tb_ptr, thread_idx, info); +} + +__thread struct wasmContext ctx =3D { + .tb_ptr =3D 0, + .stack =3D NULL, + .do_init =3D 1, + .buf128 =3D 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 =3D v_tb_ptr; + uint32_t *tb_ptr =3D 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)]; @@ -384,6 +418,7 @@ uintptr_t QEMU_DISABLE_CFI tcg_qemu_tb_exec(CPUArchStat= e *env, MemOpIdx oi; int32_t ofs; void *ptr; + int32_t counter; =20 insn =3D *tb_ptr++; opc =3D extract32(insn, 0, 8); @@ -802,20 +837,40 @@ uintptr_t QEMU_DISABLE_CFI tcg_qemu_tb_exec(CPUArchSt= ate *env, =20 case INDEX_op_exit_tb: tci_args_l(insn, tb_ptr, &ptr); + ctx.tb_ptr =3D 0; return (uintptr_t)ptr; =20 case INDEX_op_goto_tb: tci_args_l(insn, tb_ptr, &ptr); - tb_ptr =3D *(void **)ptr; + if (*(uint32_t **)ptr !=3D tb_ptr) { + tb_ptr =3D *(uint32_t **)ptr; + ctx.tb_ptr =3D tb_ptr; + counter =3D get_counter_local(tb_ptr); + if ((counter >=3D 0) && (counter < INSTANTIATE_NUM)) { + set_counter_local(tb_ptr, counter + 1); + } else { + return 0; /* enter to wasm TB */ + } + tb_ptr =3D get_tci_ptr(tb_ptr); + } break; =20 case INDEX_op_goto_ptr: tci_args_r(insn, &r0); ptr =3D (void *)regs[r0]; if (!ptr) { + ctx.tb_ptr =3D 0; return 0; } tb_ptr =3D ptr; + ctx.tb_ptr =3D tb_ptr; + counter =3D get_counter_local(tb_ptr); + if ((counter >=3D 0) && (counter < INSTANTIATE_NUM)) { + set_counter_local(tb_ptr, counter + 1); + } else { + return 0; /* enter to wasm TB */ + } + tb_ptr =3D get_tci_ptr(tb_ptr); break; =20 case INDEX_op_qemu_ld: @@ -861,3 +916,181 @@ uintptr_t QEMU_DISABLE_CFI tcg_qemu_tb_exec(CPUArchSt= ate *env, /* * TODO: Disassembler is not implemented */ + +/* + * Max number of instances can exist simultaneously. + * + * If the number of instances reaches this and a new instance needs to be + * created, old instances are removed so that new instances can be created + * without hitting the browser's limit. + */ +#define MAX_INSTANCES 15000 + +int instances_global; + +/* Avoid overwrapping of begin/end pointers */ +#define INSTANCES_BUF_MAX (MAX_INSTANCES + 1) + +__thread struct wasmInstanceInfo instances[INSTANCES_BUF_MAX]; +__thread int instances_begin; +__thread int instances_end; + +static void add_instance(wasm_tb_func tb_func, void *tb_ptr) +{ + instances[instances_end].tb_func =3D tb_func; + instances[instances_end].tb_ptr =3D tb_ptr; + set_info_local(tb_ptr, &(instances[instances_end])); + instances_end =3D (instances_end + 1) % INSTANCES_BUF_MAX; + + qatomic_inc(&instances_global); +} + +__thread int instance_pending_gc; +__thread int instance_done_gc; + +static void remove_old_instances(void) +{ + int num; + if (instance_pending_gc > 0) { + return; + } + if (instances_begin <=3D instances_end) { + num =3D instances_end - instances_begin; + } else { + num =3D instances_end + (INSTANCES_BUF_MAX - instances_begin); + } + /* removes the half of the oldest instances in the buffer */ + num /=3D 2; + for (int i =3D 0; i < num; i++) { + EM_ASM({ removeFunction($0); }, instances[instances_begin].tb_func= ); + instances[instances_begin].tb_ptr =3D NULL; + instances_begin =3D (instances_begin + 1) % INSTANCES_BUF_MAX; + } + instance_pending_gc +=3D 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 =3D get_info_local(tb_ptr); + if (elm =3D=3D NULL) { + return NULL; + } + if (elm->tb_ptr !=3D tb_ptr) { + /* + * This TB was instantiated but has been removed. Set counter to t= he + * max value so that this will be instantiated again at the next + * invocation. + */ + set_counter_local(tb_ptr, INSTANTIATE_NUM); + set_info_local(tb_ptr, NULL); + return NULL; + } + return elm->tb_func; +} + +static void check_instance_garbage_collected(void) +{ + if (instance_done_gc > 0) { + qatomic_sub(&instances_global, instance_done_gc); + instance_pending_gc -=3D instance_done_gc; + instance_done_gc =3D 0; + } +} + +EM_JS(void, init_wasm32_js, (int instance_done_gc_ptr), +{ + Module.__wasm32_tb =3D { + inst_gc_registry: new FinalizationRegistry((i) =3D> { + if (i =3D=3D "tbinstance") { + const memory_v =3D new DataView(HEAP8.buffer); + let v =3D memory_v.getInt32(instance_done_gc_ptr, true); + memory_v.setInt32(instance_done_gc_ptr, v + 1, true); + } + }) + }; +}); + +#define MAX_EXEC_NUM 50000 +__thread int exec_cnt =3D 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 =3D=3D 0) { + if (!can_add_instance()) { + emscripten_sleep(0); + check_instance_garbage_collected(); + } + exec_cnt =3D MAX_EXEC_NUM; + } +} + +int thread_idx_max; + +static void init_wasm32(void) +{ + thread_idx =3D qatomic_fetch_inc(&thread_idx_max); + ctx.stack =3D g_malloc(TCG_STATIC_CALL_ARGS_SIZE + TCG_STATIC_FRAME_SI= ZE); + ctx.buf128 =3D g_malloc(16); + ctx.tci_tb_ptr =3D (uint32_t *)&tci_tb_ptr; + init_wasm32_js((int)&instance_done_gc); +} + +__thread bool initdone; + +uintptr_t tcg_qemu_tb_exec(CPUArchState *env, const void *v_tb_ptr) +{ + if (!initdone) { + init_wasm32(); + initdone =3D true; + } + ctx.env =3D env; + ctx.tb_ptr =3D (void *)v_tb_ptr; + while (true) { + trysleep(); + struct wasmTBHeader *header =3D (struct wasmTBHeader *)ctx.tb_ptr; + int32_t counter =3D get_counter_local(header); + uint32_t res; + wasm_tb_func tb_func =3D get_instance_from_tb(ctx.tb_ptr); + if (tb_func) { + /* + * call the instance if available + */ + res =3D call_wasm_tb(tb_func, &ctx); + } else if (counter < INSTANTIATE_NUM) { + /* + * run it on TCI if the counter value is small + */ + set_counter_local(ctx.tb_ptr, counter + 1); + res =3D 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_instance_garbage_collected(); + res =3D tcg_qemu_tb_exec_tci(env); + } else { + /* + * instantiate and run TB as Wasm + */ + tb_func =3D (wasm_tb_func)instantiate_wasm((int)header->wasm_p= tr, + header->wasm_size, + (int)header->import_p= tr, + header->import_size); + add_instance(tb_func, ctx.tb_ptr); + res =3D call_wasm_tb(tb_func, &ctx); + } + if (!ctx.tb_ptr) { + return res; + } + } +} diff --git a/tcg/wasm32.h b/tcg/wasm32.h index f2749f1e0e..9a3230e87c 100644 --- a/tcg/wasm32.h +++ b/tcg/wasm32.h @@ -48,6 +48,14 @@ static inline int32_t call_wasm_tb(wasm_tb_func f, struc= t wasmContext *ctx) return f(ctx); } =20 +/* + * wasmInstanceInfo holds the relationship between TB and Wasm instance. + */ +struct wasmInstanceInfo { + void *tb_ptr; + wasm_tb_func tb_func; +}; + /* * TB of wasm backend starts from a header which stores pointers for each = data * stored in the following region in the TB. @@ -69,6 +77,43 @@ struct wasmTBHeader { */ void *import_ptr; int import_size; + + /* + * Counter holds how many times the TB is executed before instantiation + * for each thread. + */ + int32_t *counter_ptr; + + /* + * Pointer to the instance information on each thread. + */ + struct wasmInstanceInfo **info_ptr; }; =20 +static inline uint32_t *get_tci_ptr(void *tb_ptr) +{ + return (uint32_t *)(((struct wasmTBHeader *)tb_ptr)->tci_ptr); +} + +static inline int32_t get_counter(void *tb_ptr, int idx) +{ + return ((struct wasmTBHeader *)tb_ptr)->counter_ptr[idx]; +} + +static inline void set_counter(void *tb_ptr, int idx, int v) +{ + ((struct wasmTBHeader *)tb_ptr)->counter_ptr[idx] =3D v; +} + +static inline struct wasmInstanceInfo *get_info(void *tb_ptr, int idx) +{ + return ((struct wasmTBHeader *)tb_ptr)->info_ptr[idx]; +} + +static inline void set_info(void *tb_ptr, int idx, + struct wasmInstanceInfo *info) +{ + ((struct wasmTBHeader *)tb_ptr)->info_ptr[idx] =3D info; +} + #endif diff --git a/tcg/wasm32/tcg-target.c.inc b/tcg/wasm32/tcg-target.c.inc index 04cd9b6e4a..f0c51a5d3d 100644 --- a/tcg/wasm32/tcg-target.c.inc +++ b/tcg/wasm32/tcg-target.c.inc @@ -27,6 +27,11 @@ =20 #include "../wasm32.h" =20 +/* + * This is included to get the max 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 @@ -3654,6 +3659,18 @@ static void tcg_out_tb_start(TCGContext *s) h =3D (struct wasmTBHeader *)(s->code_ptr); s->code_ptr +=3D sizeof(struct wasmTBHeader); =20 + /* locate counters */ + h->counter_ptr =3D (int32_t *)s->code_ptr; + size =3D tcg_max_ctxs * sizeof(int32_t); + memset(s->code_ptr, 0, size); + s->code_ptr +=3D size; + + /* locate instance information */ + h->info_ptr =3D (struct wasmInstanceInfo **)s->code_ptr; + size =3D tcg_max_ctxs * sizeof(void *); + memset(s->code_ptr, 0, size); + s->code_ptr +=3D size; + /* Followed by TCI code */ h->tci_ptr =3D s->code_ptr; =20 --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747745789; cv=none; d=zohomail.com; s=zohoarc; b=N06E9DSk91mvBv2YcDiu5f9whjPUw2IQoG/b+VeQizwBDs50hEcKqXI1Tc5n9oH4fm8rvmFfwexlv0iyNJRYlccXQrT2TXdO0W3jHu5bUak/KaUTlnZNQmIGghrjrhvvVNsS1E572lO0ClP14z+iNIvuNU+VthNIX74Ru0CH9Ck= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747745789; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=D01VAfUBGZHAoC2ddrbmO4/0n/qVM5JIZp6Wbld3EfA=; b=ZywjCU55ud1zCwvZrdLYbge/SAoh8UHJTJ1928TD09Y8av/x2sN/KGrl7Zs9AYBMW9wBRZXGyFtvOii4YRziJjebfrMmLrTQ3g+HcS0j55rqCOVO5gzPRRrbSYvjkQ/Pg+nKXInKyRlheslx8efg/Wrfu+TlNENNSRwHBW+nG68= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747745789073129.99689772907107; Tue, 20 May 2025 05:56:29 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMVp-0006sp-Iv; Tue, 20 May 2025 08:56:05 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMV7-0004pM-6P; Tue, 20 May 2025 08:55:22 -0400 Received: from mail-pj1-x1032.google.com ([2607:f8b0:4864:20::1032]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMV0-0001KC-GE; Tue, 20 May 2025 08:55:20 -0400 Received: by mail-pj1-x1032.google.com with SMTP id 98e67ed59e1d1-309fac646adso6373973a91.1; Tue, 20 May 2025 05:55:12 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.55.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:55:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745711; x=1748350511; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=D01VAfUBGZHAoC2ddrbmO4/0n/qVM5JIZp6Wbld3EfA=; b=MKJ4jX+lQEFuVPvv0Pfqfyu8vD1PlZDeY2orP83QMxUcAOLB/ILmtC/UCv5vsbE1p5 jZvo9sRNolnsW8bQ2zK/gsPuw604qGTBba8NQX1DRBX8MaSFlFabD5j3KQdQM780aRqP jqT6s20CtkwytkxNerMskSz5GWXH7JFf2ZUl3IJ8J9nxIVH/M5N/ap8oGoLmYDFKXY9/ fTEdqgir5bbn1nqdkwePbBYZgF/51iczX7MVdlw4yTKNFPuwRJvLzZ9JivaQxniEnGmN 1fvBIKwe724UIitOuFj+z8K9b9lSeSbTIPmsbyTjB+1avbLUtJsNNgPgrFwAH5t56/ip KDGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745711; x=1748350511; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=D01VAfUBGZHAoC2ddrbmO4/0n/qVM5JIZp6Wbld3EfA=; b=IoJfd3E2MgYa0df5WMFVrq2QhnuHWd4KIPcOCWrOGBINLQ505xlGokco8LpfWLtAF5 VvJUT+8ixSsTAcpjYUKw0D5MnYjZyVxg5zr+otRBDWmqarnQuQNavCt+R97tU5l2Sjr9 vNdkzYC7b9ALl1ZmLyeQM2dTW8vIMTde2+kBQi0NWJ3dx39e+cpnR0ZHPWczdTnjuNtP 4llR6sG3/8LeYVgI74oukaZsm3Ua+gYdGsIBSHfExAJrUV/WAQ1SaFxtibLm630tkNJE Pmh/N+kFZt5BdQnfw5iI2Bi/VvAXgCDKoikisP8TeUxwgKl94441nOnYbpM1eefAWRue 7ibw== X-Forwarded-Encrypted: i=1; AJvYcCX7slssh2s+Psbea2BZL/gv3D6PduFCMBFMfmQxCdaFDIQl/COxy15tK795wnW0nA3L0SNfguJtxg==@nongnu.org, AJvYcCX9mNnoM41mtBaBwnqrhET5qPiv9F3OJC5jx2Htpf5ME12zSPsVgmv18p6V0ScIkyEDv+IvhdIgTtqR0Q==@nongnu.org X-Gm-Message-State: AOJu0YxJ3ZcdAcF87PuuZl1FaL02FL1JNv2Tv5cUVK57W6Ysvs6NcTPu njVSKjRpcx5lL2SbbyWCDoQDZo5G2QZbkQDH8rJpJ9yMPtQD2EyH7pFZcftOuX5F X-Gm-Gg: ASbGncuQQqwj7vdSofWr3NgADb3fNy5M8vSwRJMYkodr29Kifz8uZnEgC8rSDzxppTu kC/2knoJLelFs9qv/5jz/zDSy45ITyg2FsvyHBUwO+OjUjM6w6UEuCGzVD8x9VdF/u76LDD31aw t1SaX9Dbplb5gTfJfGUceeeWjHA7K20ZzZcB0z8ljJTcTEBfUujTzm3Jx68F4FAcRkY+xSMluel jT1hI/9CbMJHhD/i5Fh6S9NI/nS/Fo1SXYQdgSI2O+udkqU4sYIeiz2lNvR9Sg9XxHsxRotFcSP W6H1oHVWpQ5K5ZhaaxIZ2LNr3mAv6mC0ueWiAHx+lqxDYc+x+5k= X-Google-Smtp-Source: AGHT+IGOMOt/FkM/rdBI6wkt/aJrDNn9TYfU+or40zKKV8LX/mutFAggKceozq56SFUpc/Yq4TPZPg== X-Received: by 2002:a17:90b:3d86:b0:2fa:17e4:b1cf with SMTP id 98e67ed59e1d1-30e7dc1e956mr27665623a91.2.1747745711292; Tue, 20 May 2025 05:55:11 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 31/33] tcg/wasm32: Enable TLB lookup Date: Tue, 20 May 2025 21:51:33 +0900 Message-ID: <746962847ecb7947fa9315342d9ea953ad849607.1747744132.git.ktokunaga.mail@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1032; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pj1-x1032.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747745789864116600 Content-Type: text/plain; charset="utf-8" This 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. Signed-off-by: Kohei Tokunaga --- tcg/wasm32/tcg-target.c.inc | 223 +++++++++++++++++++++++++++++++++++- 1 file changed, 221 insertions(+), 2 deletions(-) diff --git a/tcg/wasm32/tcg-target.c.inc b/tcg/wasm32/tcg-target.c.inc index f0c51a5d3d..a2815db6b5 100644 --- a/tcg/wasm32/tcg-target.c.inc +++ b/tcg/wasm32/tcg-target.c.inc @@ -3,8 +3,12 @@ * Tiny Code Generator for QEMU * * Copyright (c) 2009, 2011 Stefan Weil + * Copyright (c) 2018 SiFive, Inc + * Copyright (c) 2008-2009 Arnaud Patard + * Copyright (c) 2009 Aurelien Jarno + * Copyright (c) 2008 Fabrice Bellard * - * Based on tci/tcg-target.c.inc + * Based on tci/tcg-target.c.inc and riscv/tcg-target.c.inc * * Permission is hereby granted, free of charge, to any person obtaining a= copy * of this software and associated documentation files (the "Software"), t= o deal @@ -364,6 +368,10 @@ static void tcg_wasm_out_op_i64_eqz(TCGContext *s) { tcg_wasm_out8(s, 0x50); } +static void tcg_wasm_out_op_i64_eq(TCGContext *s) +{ + tcg_wasm_out8(s, 0x51); +} static void tcg_wasm_out_op_br(TCGContext *s, int i) { tcg_wasm_out8(s, 0x0c); @@ -436,6 +444,10 @@ static void tcg_wasm_out_op_local_set(TCGContext *s, u= int8_t i) { tcg_wasm_out_op_var(s, 0x21, i); } +static void tcg_wasm_out_op_local_tee(TCGContext *s, uint8_t i) +{ + tcg_wasm_out_op_var(s, 0x22, i); +} =20 #define tcg_wasm_out_i64_calc(op) \ static void tcg_wasm_out_i64_calc_##op( \ @@ -1993,12 +2005,161 @@ static void *qemu_ld_helper_ptr(uint32_t oi) } } =20 +static void tcg_wasm_out_i32_load_s(TCGContext *s, int off) +{ + if (off < 0) { + tcg_wasm_out_op_i32_const(s, off); + tcg_wasm_out_op_i32_add(s); + off =3D 0; + } + tcg_wasm_out_op_i32_load(s, 0, off); +} + +static void tcg_wasm_out_i64_load_s(TCGContext *s, int off) +{ + if (off < 0) { + tcg_wasm_out_op_i32_const(s, off); + tcg_wasm_out_op_i32_add(s); + off =3D 0; + } + tcg_wasm_out_op_i64_load(s, 0, off); +} + +#define MIN_TLB_MASK_TABLE_OFS INT_MIN + +static uint8_t prepare_host_addr_wasm(TCGContext *s, uint8_t *hit_var, + TCGReg addr_reg, MemOpIdx oi, + bool is_ld) +{ + MemOp opc =3D get_memop(oi); + TCGAtomAlign aa; + unsigned a_mask; + unsigned s_bits =3D opc & MO_SIZE; + unsigned s_mask =3D (1u << s_bits) - 1; + int mem_index =3D get_mmuidx(oi); + int fast_ofs =3D tlb_mask_table_ofs(s, mem_index); + int mask_ofs =3D fast_ofs + offsetof(CPUTLBDescFast, mask); + int table_ofs =3D fast_ofs + offsetof(CPUTLBDescFast, table); + int add_off =3D offsetof(CPUTLBEntry, addend); + tcg_target_long compare_mask; + + if (!tcg_use_softmmu) { + g_assert_not_reached(); + } + + *hit_var =3D TMP64_LOCAL_0_IDX; + tcg_wasm_out_op_i64_const(s, 0); + tcg_wasm_out_op_local_set(s, *hit_var); + + aa =3D atom_and_align_for_opc(s, opc, MO_ATOM_IFALIGN, false); + a_mask =3D (1u << aa.align) - 1; + + /* Get the CPUTLBEntry offset */ + tcg_wasm_out_op_global_get_r(s, addr_reg); + tcg_wasm_out_op_i64_const(s, s->page_bits - CPU_TLB_ENTRY_BITS); + tcg_wasm_out_op_i64_shr_u(s); + tcg_wasm_out_op_i32_wrap_i64(s); + tcg_wasm_out_op_global_get_r_i32(s, TCG_AREG0); + tcg_wasm_out_i32_load_s(s, mask_ofs); + tcg_wasm_out_op_i32_and(s); + + /* Get the pointer to the target CPUTLBEntry */ + tcg_wasm_out_op_global_get_r_i32(s, TCG_AREG0); + tcg_wasm_out_i32_load_s(s, table_ofs); + tcg_wasm_out_op_i32_add(s); + tcg_wasm_out_op_local_tee(s, TMP32_LOCAL_0_IDX); + + /* Load the tlb copmarator */ + tcg_wasm_out_i64_load_s( + s, is_ld ? offsetof(CPUTLBEntry, addr_read) + : offsetof(CPUTLBEntry, addr_write)); + + /* + * For aligned accesses, we check the first byte and include the + * alignment bits within the address. For unaligned access, we + * check that we don't cross pages using the address of the last + * byte of the access. + */ + tcg_wasm_out_op_global_get_r(s, addr_reg); + if (a_mask < s_mask) { + tcg_wasm_out_op_i64_const(s, s_mask - a_mask); + tcg_wasm_out_op_i64_add(s); + } + compare_mask =3D (uint64_t)s->page_mask | a_mask; + tcg_wasm_out_op_i64_const(s, compare_mask); + tcg_wasm_out_op_i64_and(s); + + /* Compare masked address with the TLB entry. */ + tcg_wasm_out_op_i64_eq(s); + tcg_wasm_out_op_if_noret(s); + + /* TLB Hit - translate address using addend. */ + tcg_wasm_out_op_local_get(s, TMP32_LOCAL_0_IDX); + tcg_wasm_out_i32_load_s(s, add_off); + tcg_wasm_out_op_global_get_r(s, addr_reg); + tcg_wasm_out_op_i32_wrap_i64(s); + tcg_wasm_out_op_i32_add(s); + tcg_wasm_out_op_local_set(s, TMP32_LOCAL_1_IDX); + tcg_wasm_out_op_i64_const(s, 1); + tcg_wasm_out_op_local_set(s, *hit_var); + + tcg_wasm_out_op_end(s); + + return TMP32_LOCAL_1_IDX; +} + +static void tcg_wasm_out_qemu_ld_direct( + TCGContext *s, TCGReg r, uint8_t base, MemOp opc) +{ + switch (opc & (MO_SSIZE)) { + case MO_UB: + tcg_wasm_out_op_local_get(s, base); + tcg_wasm_out_op_i64_load8_u(s, 0, 0); + tcg_wasm_out_op_global_set_r(s, r); + break; + case MO_SB: + tcg_wasm_out_op_local_get(s, base); + tcg_wasm_out_op_i64_load8_s(s, 0, 0); + tcg_wasm_out_op_global_set_r(s, r); + break; + case MO_UW: + tcg_wasm_out_op_local_get(s, base); + tcg_wasm_out_op_i64_load16_u(s, 0, 0); + tcg_wasm_out_op_global_set_r(s, r); + break; + case MO_SW: + tcg_wasm_out_op_local_get(s, base); + tcg_wasm_out_op_i64_load16_s(s, 0, 0); + tcg_wasm_out_op_global_set_r(s, r); + break; + case MO_UL: + tcg_wasm_out_op_local_get(s, base); + tcg_wasm_out_op_i64_load32_u(s, 0, 0); + tcg_wasm_out_op_global_set_r(s, r); + break; + case MO_SL: + tcg_wasm_out_op_local_get(s, base); + tcg_wasm_out_op_i64_load32_s(s, 0, 0); + tcg_wasm_out_op_global_set_r(s, r); + break; + case MO_UQ: + tcg_wasm_out_op_local_get(s, base); + tcg_wasm_out_op_i64_load(s, 0, 0); + tcg_wasm_out_op_global_set_r(s, r); + break; + default: + g_assert_not_reached(); + } +} + static void tcg_wasm_out_qemu_ld(TCGContext *s, TCGReg data_reg, TCGReg addr_reg, MemOpIdx oi) { int helper_idx; int func_idx; bool addr64 =3D s->addr_type =3D=3D TCG_TYPE_I64; + MemOp mop =3D get_memop(oi); + uint8_t base_var, hit_var; =20 helper_idx =3D (uint32_t)qemu_ld_helper_ptr(oi); func_idx =3D get_helper_idx(s, helper_idx); @@ -2012,6 +2173,14 @@ static void tcg_wasm_out_qemu_ld(TCGContext *s, TCGR= eg data_reg, addr_reg =3D TCG_REG_TMP; } =20 + base_var =3D prepare_host_addr_wasm(s, &hit_var, addr_reg, oi, true); + tcg_wasm_out_op_local_get(s, hit_var); + tcg_wasm_out_op_i64_const(s, 1); + tcg_wasm_out_op_i64_eq(s); + tcg_wasm_out_op_if_noret(s); + tcg_wasm_out_qemu_ld_direct(s, data_reg, base_var, mop); /* fast path = */ + tcg_wasm_out_op_end(s); + /* * update the block index so that the possible rewinding will * skip this block @@ -2020,6 +2189,10 @@ static void tcg_wasm_out_qemu_ld(TCGContext *s, TCGR= eg data_reg, tcg_wasm_out_op_global_set(s, BLOCK_PTR_IDX); tcg_wasm_out_new_block(s); =20 + tcg_wasm_out_op_local_get(s, hit_var); + tcg_wasm_out_op_i64_eqz(s); + tcg_wasm_out_op_if_noret(s); + /* call helper */ tcg_wasm_out_op_global_get_r(s, TCG_AREG0); tcg_wasm_out_op_i32_wrap_i64(s); @@ -2030,6 +2203,8 @@ static void tcg_wasm_out_qemu_ld(TCGContext *s, TCGRe= g data_reg, tcg_wasm_out_op_call(s, func_idx); tcg_wasm_out_op_global_set_r(s, data_reg); tcg_wasm_out_handle_unwinding(s); + + tcg_wasm_out_op_end(s); } =20 static void *qemu_st_helper_ptr(uint32_t oi) @@ -2049,6 +2224,35 @@ static void *qemu_st_helper_ptr(uint32_t oi) } } =20 +static void tcg_wasm_out_qemu_st_direct( + TCGContext *s, TCGReg lo, uint8_t base, MemOp opc) +{ + switch (opc & (MO_SSIZE)) { + case MO_8: + tcg_wasm_out_op_local_get(s, base); + tcg_wasm_out_op_global_get_r(s, lo); + tcg_wasm_out_op_i64_store8(s, 0, 0); + break; + case MO_16: + tcg_wasm_out_op_local_get(s, base); + tcg_wasm_out_op_global_get_r(s, lo); + tcg_wasm_out_op_i64_store16(s, 0, 0); + break; + case MO_32: + tcg_wasm_out_op_local_get(s, base); + tcg_wasm_out_op_global_get_r(s, lo); + tcg_wasm_out_op_i64_store32(s, 0, 0); + break; + case MO_64: + tcg_wasm_out_op_local_get(s, base); + tcg_wasm_out_op_global_get_r(s, lo); + tcg_wasm_out_op_i64_store(s, 0, 0); + break; + default: + g_assert_not_reached(); + } +} + static void tcg_wasm_out_qemu_st(TCGContext *s, TCGReg data_reg, TCGReg addr_reg, MemOpIdx oi) { @@ -2056,6 +2260,7 @@ static void tcg_wasm_out_qemu_st(TCGContext *s, TCGRe= g data_reg, int func_idx; bool addr64 =3D s->addr_type =3D=3D TCG_TYPE_I64; MemOp mop =3D get_memop(oi); + uint8_t base_var, hit_var; =20 helper_idx =3D (uint32_t)qemu_st_helper_ptr(oi); func_idx =3D get_helper_idx(s, helper_idx); @@ -2069,6 +2274,14 @@ static void tcg_wasm_out_qemu_st(TCGContext *s, TCGR= eg data_reg, addr_reg =3D TCG_REG_TMP; } =20 + base_var =3D prepare_host_addr_wasm(s, &hit_var, addr_reg, oi, false); + tcg_wasm_out_op_local_get(s, hit_var); + tcg_wasm_out_op_i64_const(s, 1); + tcg_wasm_out_op_i64_eq(s); + tcg_wasm_out_op_if_noret(s); + tcg_wasm_out_qemu_st_direct(s, data_reg, base_var, mop); /* fast path = */ + tcg_wasm_out_op_end(s); + /* * update the block index so that the possible rewinding will * skip this block @@ -2077,6 +2290,10 @@ static void tcg_wasm_out_qemu_st(TCGContext *s, TCGR= eg data_reg, tcg_wasm_out_op_global_set(s, BLOCK_PTR_IDX); tcg_wasm_out_new_block(s); =20 + tcg_wasm_out_op_local_get(s, hit_var); + tcg_wasm_out_op_i64_eqz(s); + tcg_wasm_out_op_if_noret(s); + /* call helper */ tcg_wasm_out_op_global_get_r(s, TCG_AREG0); tcg_wasm_out_op_i32_wrap_i64(s); @@ -2095,6 +2312,8 @@ static void tcg_wasm_out_qemu_st(TCGContext *s, TCGRe= g data_reg, =20 tcg_wasm_out_op_call(s, func_idx); tcg_wasm_out_handle_unwinding(s); + + tcg_wasm_out_op_end(s); } =20 static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, @@ -3752,7 +3971,7 @@ static int tcg_out_tb_end(TCGContext *s) =20 bool tcg_target_has_memory_bswap(MemOp memop) { - return true; + return false; } =20 static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l) --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747746566; cv=none; d=zohomail.com; s=zohoarc; b=AObx3CVT6PaUDFonbuRjrfy6pfEZJzOQem0obHDMjXovwgNtqvsM0yuzRNQaZRXhAcnlGmebE76QWLTV/G7VD4e3maQ5wrFuYpuYYFvBt14+i3K7VW++stmGGeadPMd9NkUGR8lbnV+Z+hsGEszivgi6uL6ThKCI9SnOPyL6TZc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747746566; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=pjfIs5gITyjRMKLD5/LED/i/qyf7Ai8DnNu3qgZkijk=; b=IiNPlRXTaVh3SUtbXwdsDbsO/fbzh/EyGbN8bCp9OSqf5TAHc//IZ5TEMQdNhCa9s3sJlajvytBXopbYtfPkf/CLkcS/0KlrRvtz4F96XOx8gcoNnau+IueAD21EJhYuWZYS/zTwfIUiEN8uKezYJlte8gTvNNtPmL8gJ8/bfs8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747746566173671.0450475704711; Tue, 20 May 2025 06:09:26 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMVv-0007A9-5N; Tue, 20 May 2025 08:56:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMVE-00050x-Q1; Tue, 20 May 2025 08:55:32 -0400 Received: from mail-pj1-x1029.google.com ([2607:f8b0:4864:20::1029]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMV7-0001LT-L4; Tue, 20 May 2025 08:55:28 -0400 Received: by mail-pj1-x1029.google.com with SMTP id 98e67ed59e1d1-30e8feb1830so3375184a91.0; Tue, 20 May 2025 05:55:18 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.55.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:55:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745717; x=1748350517; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=pjfIs5gITyjRMKLD5/LED/i/qyf7Ai8DnNu3qgZkijk=; b=RKI+4I0q4EOlQkt2gIQGujUhIc2MQHMTAqfr71jJZpLYXByZMjX1Jfmf3LJ3dB9LMi relHILGb5bsFjVbTyFgoZ9rPElnBNIYC48as0iZl4gsfk0iei/8UemCLQ9cbSlKmup42 ldltbRjs5f+3fLN4sueQ0PCqoeOG7mErqpu0E/O9RlXadtS05uFoZoik3Bqc9apUQfd/ OT7Rf1V7BfvCX5R+YqNj7rmOJZRlRqCHjQDSiFZAUkWEy/4pg8PKTQSmvKEhClTy5NZ6 0k2stGAIL5ME2lf3SYtMgzIuy8MLP+PVSCsK0hTnsS9F0KYvMLer1hpnquspcqmhIZoo e8Yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745717; x=1748350517; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=pjfIs5gITyjRMKLD5/LED/i/qyf7Ai8DnNu3qgZkijk=; b=ZDe/9IR3zTn2cz5fryq9dHfA4C71YugLDogks7Ka1litIZG+y/b+dBAM9Y4idAKK5U 6sobzvpp0ungbRHb1IIF7c7xVAukWpTthzuBZ7UQ3ca9Azp9pSa3LK6cy1AFA9aEpZVe Xy39ZlZrreTTfa2irn0H60IiOhacm3emdX7vDMmyrklslqII8/GxT13gfcaiSc0PKQzz ZeysXSgWGn9n9RgfAo17SJS1KDwOpKEAX/6HLmrVOWvw4AnMlL2Y2aMJcHrhQJZsiRtW nAUfZPvcMNWDkMyZCcrP0lYwncrD7qtYlMGLR71QWq1xnZTw3EnruN033sVld23GeTLd +OwA== X-Forwarded-Encrypted: i=1; AJvYcCW2UbVfyN3nZLJV4o7Ekv5bfRL8Ws62TC21E/cA9k/ct8MOQ6O8ueCZk4WP4EUA55zM9YBuWioCS3jB2Q==@nongnu.org, AJvYcCXjhTufwYSrX23UaLZ0MCSphFKCkqC9sqe21A5XAvLphyWf8KMKyUt6iAZhVlCcYonZQ9sTyrehbg==@nongnu.org X-Gm-Message-State: AOJu0YzqfS6MLqk6mJvDTs1YDNCceWI75/r5Xyc5gjboEKa1lSDNacr4 ktjovmy5CPQR8lzJ2y0hAj104HPg3uJ0NE/T0TXnKVsHXh8XMm+R6vpNGjyExryw X-Gm-Gg: ASbGnctGv4KiZ+PXvjtDGCK3tr96PKJrmQbN0aD+YsjR/Ps5UCg94qxLyl4AJvPTyX5 lMBMxtf3nkGFFxlOVf7IVBarIHHFpWMyIRMErPwfXUTnNdqARkYoVqC+ammYStmLmDq1r2d77A5 qpdbfMvccmXckrvbEt4yJSawHHs3Ts2pyyhHkahQZ2RzIrFxfYsQpO8KQpLbzBQhYB0ozfz1/t0 jBKLb0xvdAeGh/axd4sNscYaxKCEcsebp19kGGDejzhav2ZhDsIchu3O1rhT7Ha8IZzuFc0tNQL q3ZyMJUcs4jA2gnoJrt1RG2WNNvi/1JIFegrhUABCZMsncvoo7danmu0Oh8+CQ== X-Google-Smtp-Source: AGHT+IFctWoCBwXZjWVIzlvOeAGpN4yYvW8TpdBxj/ko0x2Fmf9rUlfVzBwaYGHdhUFZEuZxeGUt7Q== X-Received: by 2002:a17:90b:3505:b0:305:2d27:7c9f with SMTP id 98e67ed59e1d1-30e7d555aadmr28951571a91.16.1747745716767; Tue, 20 May 2025 05:55:16 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 32/33] meson: Propagate optimization flag for linking on Emscripten Date: Tue, 20 May 2025 21:51:34 +0900 Message-ID: <8d969f197a9b3785fd6bca4ec57e3a42afdf62c0.1747744132.git.ktokunaga.mail@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1029; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pj1-x1029.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, T_SPF_TEMPERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747746567100116600 Content-Type: text/plain; charset="utf-8" Emscripten uses the optimization flag at 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-emscrip= ten-optimizes Signed-off-by: Kohei Tokunaga --- meson.build | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/meson.build b/meson.build index f6ed867819..91e182092d 100644 --- a/meson.build +++ b/meson.build @@ -869,6 +869,10 @@ elif host_os =3D=3D 'openbsd' # Disable OpenBSD W^X if available emulator_link_args =3D cc.get_supported_link_arguments('-Wl,-z,wxneede= d') endif +elif host_os =3D=3D 'emscripten' + if get_option('optimization') !=3D 'plain' + emulator_link_args +=3D ['-O' + get_option('optimization')] + endif endif =20 ############################################### --=20 2.43.0 From nobody Sat Nov 1 12:34:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1747746552; cv=none; d=zohomail.com; s=zohoarc; b=lqm9Ppj7Tj1rhHSBlRW4/Eb8H9s1JotbHt8z6xXFsDbnchoiOFGTBQP860Y97xmlyseDn5wr95glbHyIGbCBWwTpSAq7cIFBKW9y0Fe4NfzWUUJtCPe3HFxrGsto93zg6/HpI9Xz+jBxYak6zXk7s9LnNA96Tnp3TkF17KcWGSk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747746552; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=byOBE0LXqcmVn/uVzQGaT6uRuP0TTdALhSny48CvJBE=; b=e4nG6IwOJEODgPjE9/CMELlABUWgiz0Raui0bSuWQ3ERlYnWh7G8wT99OorLWAJ7ixriPa3WDFHhFkKgACcdcF4L+Qei2H2jevj0i83fPCEJtvpIFuaNDiMP4ODh3EfCQreIV9KwIRgRrWqD2fDGVYp6FHPw9/QaLLXN3GVxZO4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747746552746960.1747734829369; Tue, 20 May 2025 06:09:12 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHMVt-00071O-7j; Tue, 20 May 2025 08:56:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHMVI-00052Q-RX; Tue, 20 May 2025 08:55:36 -0400 Received: from mail-pf1-x433.google.com ([2607:f8b0:4864:20::433]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uHMVC-0001MT-0b; Tue, 20 May 2025 08:55:30 -0400 Received: by mail-pf1-x433.google.com with SMTP id d2e1a72fcca58-73bf5aa95e7so5065026b3a.1; Tue, 20 May 2025 05:55:22 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:1bde:8310:e993:5dcc]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30f36364f9asm1625916a91.4.2025.05.20.05.55.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 05:55:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747745721; x=1748350521; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=byOBE0LXqcmVn/uVzQGaT6uRuP0TTdALhSny48CvJBE=; b=k87iyALfdwYYmqYrDFFXJcoJ4RHkHvoojJanFB5HuK84xlM4rWYgw2bW/XbXOl+kgk KxFApBpZB/vMCzslCNQO9UHgLZkGMwc3gaLL0JMiKiAtPvr2HaOZipSxR4bVNM7D2SIX oV1jaCqeTq7Q+ytn0S0xzyo43roCorxI+XWTdPb3i6CmLen/CiQoNWyzmAOcno5G70yI yrKorVEdPGDKWlYoG3KeTq0ApQiRjHFyJIztYB0/xyZF8s8WWtaR8Fikfy4fsp3d3gPN PtorremaRNUFHy8rXlKO9udD3/BG5/9ZfnNHLexi0C974ohVwitbnwmx7IxkD3o5RuAi ot7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747745721; x=1748350521; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=byOBE0LXqcmVn/uVzQGaT6uRuP0TTdALhSny48CvJBE=; b=uEHrjFL6qhJs8CKZGKpQ/rwPWCErdAVABQhrM8/jZx0aKRlMvzmn+gMm/m4qS+/isH pcF6hZKSVecpbUq+L85nPda6ZbljY9W9ADzRCKN/oc0fdp+r2Le+ekZYvtxAs4Gy5LAm UoyJuiVDKxh/d9aXZtcV1igAw85fRaYEeuu/rX5AlxJlgBnK6l4ENgojMv7UVZQ71hsw WVjtmKr6AtlrL8tTVxXYLMA/OnayMULhuoPxHJ1hYWvcQwSgBJIrGfG7DJ/MZ26gjA3b 5PLiX+Gtqyi2/kVDScCa1xGB4OjaLRIdY2mn0P09XGo0oHrM2aLTLMj2jS0PbW2oj0A/ 1SjA== X-Forwarded-Encrypted: i=1; AJvYcCV7gSn5jK/Zm0LsO1mTs2ql3rbaeBCiK5TKenWZ0Lu5Amp9M2fRBRELhDg+mHrj/5G4/4UEcg1nSA==@nongnu.org, AJvYcCVOw5X3wB7HhpqMkdCpltv5RMgoV1ISx99V+CEMx3Y6ZnlIlV8vOmB6OT1q4a9DvZqGn3J6iyUpzx0vnw==@nongnu.org X-Gm-Message-State: AOJu0YzMeO/r15mnH7geoBznFj94CEOxCTc/yldILrIIe9uJc8nA1HOj zd3tFNZHWi05kn8NvOgWc8mT5VLYoCfrFOwf7o/u+vs2QzgDcOJlcKA4Am7oq0SC X-Gm-Gg: ASbGncuSkkuzy0cKDbbWizBopawh3o0bN+5KbHflD6VUg5UqdPBjOAfhKCN00M5oT2i fNT4boPMULXoLjhYKTu3qZX5EKdvhhCQYxLTokKs4PD3vvRSA83rG0T2BDBQE40XQ0Z1FyMATRJ mBofMjj+IvUs3+046Bg5djO16uwV5cSAd8/qZdC1SA3MBPe3cl0UI6g/Vxl1yLMMpnhTtY1lBJh 2ZuQCkNXf5MjgATG3vKaCjXx2stB49H0mMEoP7Tuui3dcoKWw8k0pVmRwvUWnK9K5kXsN2k432p FTOZpOqOWTHW9sEYuCvUmyle1gNh9zdKabqwihBQdJx4L5ABC3Y= X-Google-Smtp-Source: AGHT+IHCNeXOoJTyXF78Adx/1Dxrax03p01KnXN3FbNUlv+jV9FjmGCEvba2dD4sWsgzbjHjN7ba3Q== X-Received: by 2002:a17:90b:1c05:b0:2ff:5ec1:6c6a with SMTP id 98e67ed59e1d1-30e7d55ba27mr27946322a91.18.1747745721589; Tue, 20 May 2025 05:55:21 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Richard Henderson , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Stefan Hajnoczi , Pierrick Bouvier , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH 33/33] .gitlab-ci.d: build wasm backend in CI Date: Tue, 20 May 2025 21:51:35 +0900 Message-ID: <36a7f4334e9e08691d7496809a5d06b23de22e26.1747744132.git.ktokunaga.mail@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::433; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pf1-x433.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1747746554839116600 Content-Type: text/plain; charset="utf-8" Check if wasm backend can be built in CI. Signed-off-by: Kohei Tokunaga --- .gitlab-ci.d/buildtest.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.d/buildtest.yml b/.gitlab-ci.d/buildtest.yml index 248aaed137..fc42de231a 100644 --- a/.gitlab-ci.d/buildtest.yml +++ b/.gitlab-ci.d/buildtest.yml @@ -800,4 +800,4 @@ build-wasm: job: wasm-emsdk-cross-container variables: IMAGE: emsdk-wasm32-cross - CONFIGURE_ARGS: --static --disable-tools --enable-debug --enable-tcg-i= nterpreter + CONFIGURE_ARGS: --static --disable-tools --enable-debug --=20 2.43.0