From nobody Sat Nov 15 07:22: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=1755627904; cv=none; d=zohomail.com; s=zohoarc; b=VDUJKenR6vePim6jX+wrbhyEKV2mzEM0BMM59rPAiwkfHmwuE6WDhoK5HD1A+rH8ihZXDhZGYtuT25GwiXxcBllcV4OMWp9EW7n5LdJR9/urJpSyDbF+7gg1hDFTi1sj3KT0oY2hZ98rWiOTjFLEHyo3j2HBb+9JksjIIkDtaDk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755627904; 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=cvAd64/o7YFABVEip01g3of0GXJUvHRPN0JKXxULcjE=; b=lvGO7coDLOuVCqYQX7UMGEu6ZzyLMIYSb5g9X6qu88WMFFNXhIVBdvRrT+zGcBm7tA/TWm/DLRkjAiFcdPrypEV3t5kU2cL6SmhP4w/a3SS2o3WQcIe7EcJj8prCSMONMw+YXBnBObrP6wczXuoAkUfKWKsfNfLXnLkovbI2RhM= 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 1755627904625980.4619164715356; Tue, 19 Aug 2025 11:25:04 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoR0f-0007DZ-JG; Tue, 19 Aug 2025 14:24:37 -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 1uoR0R-0006vj-K3; Tue, 19 Aug 2025 14:24:26 -0400 Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uoR0N-0003w8-NH; Tue, 19 Aug 2025 14:24:23 -0400 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-2445806df50so42112535ad.1; Tue, 19 Aug 2025 11:24:17 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:29ce:ef58:c2c9:e52f]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-245ed33b4c2sm3725125ad.25.2025.08.19.11.24.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:24:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627856; x=1756232656; 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=cvAd64/o7YFABVEip01g3of0GXJUvHRPN0JKXxULcjE=; b=EnoV/WTArKvIucg0hJy0sIXHMoVzuCxoleEmfNC9yt55Dp/7RSOknALCFCCIHzLGM6 f4MGd4rOtxbY7DNz1o7pum1P/jEfzjMVA46ln2flwB+IJoHy79KTwWArUUyXCmccuOTr JbrdUpgY2uK83XLTgQ/4hY5fubh/I8q4hHsGoifD0HBcdMfZz9Ki3fiCGx8UkX0nJAHI QbggLLfAsfVmn47Gr+7OceVSq3j5sK8x9Qh/3HboHslaaBNAj8n8xN3EEwLKPTJFTw2D kuoTZ6MbVgoX69S+94hfjzFTaTsnpvM/w+XVFxJutKUSUPrqfSIPWAODo80eZWAnyvEb CXIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627856; x=1756232656; 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=cvAd64/o7YFABVEip01g3of0GXJUvHRPN0JKXxULcjE=; b=f28avEUVpIUlrAh+WEzw8kWwrGUVhPl2RGw9v+nhPv3GIRMwjU01AliFHgMihQbsif H5vwm73WVtb0nm4/u87zw3gNirdyfo4MeOWv6rNbKhHiyIfOrTFwPyec07zHIr7nrhx7 wEFlNfvgJm6ti82OcZ+koF9cDgdySktwvQismfsfNY0sjwmsoiVUzffzncZdxJfmPjSH J+IeDtruPC6ABCvWKbZuFyhQ9C4aA5G2i9wmueeffsw8w0wKPDofBeM+C7zp2nOMG5wO s5dhTQgY/4so2AsYx9WQXWn1BG9D7bU1zCJdICBiudWVaGaAELsda7iewltQrWKlcscq 94FQ== X-Forwarded-Encrypted: i=1; AJvYcCVAhQVuM+A6/fxd5G6idm2AmvDiivbfRdvdaTWjYj+NnI40dpV/vsEA7rSVRTWXj+LV0NfJq/qJFkOvhg==@nongnu.org, AJvYcCXgX5a4R08NMVasmJ2RWAYpjLsjN3S27JmvfLL1yF6XvEK/cnit/GAE4t3iJnJ505zunH/72w2Iyw==@nongnu.org X-Gm-Message-State: AOJu0YwsKZ45D8p122ir844svQeNBI14UFd6mC5e1hx+0k3pa6x/AaLu c8jMHratoSr9PRbuJYU7KouaByT1d3Nz6wZx8XQCjNQGF+UKQvAbLGghL9TnWXSM X-Gm-Gg: ASbGncvHZ4DnKcorvtzwutX2MeJEVziasSFSl9bFtM7jDiDk6OjkHNwk/HmzsNhJb9h qSH34LF6T2SLFzCleTkrw+VysGW8IRPe/AJJSpwK4/JYaSaGCW6A/EdCvkIYeYTSlEvBwyi8qmb EO8EDP1BRhHxDttx9vDDCKOYdvVQxTJu9x0Hmh0H1ffI4gx8o/VtldKBIFNchRT7eoFavBQ+vuW L5T1Z7wFzxk8HnWPJ4wC7rM2ZkYe3ku06WaRPzaOzQ9a1kB3lGFei4e0WyLgGm5YvmyxKL3/P65 zsCOkDPYI/ZQINljzB7p038JEUT+cWHvcSUhg8TUSkVws5xjHf/TxJiY5mQ+LZRlVt+SeAvloG9 fXlpJJ6RT+dNkxKEBPfvSRQ== X-Google-Smtp-Source: AGHT+IHuZ8U+vlcir8gg6hbrfziS3yK1asp1CaavGAsbW0jFBojbL7aMVgcROLIlcaAo/Dfeek23jA== X-Received: by 2002:a17:903:3bd0:b0:240:8fa2:15d2 with SMTP id d9443c01a7336-245e0488c15mr49727335ad.33.1755627855965; Tue, 19 Aug 2025 11:24:15 -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 , Paolo Bonzini , Richard Henderson , =?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 , qemu-arm@nongnu.org, qemu-riscv@nongnu.org, Stefan Hajnoczi , Pierrick Bouvier , ktokunaga.mail@gmail.com Subject: [PATCH 18/35] tcg/wasm: Add bswap instructions Date: Wed, 20 Aug 2025 03:21:47 +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::634; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pl1-x634.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: 1755627908324124100 Content-Type: text/plain; charset="utf-8" This commit introduces Wasm module local variables[1] assigned from the index 0. These variables are used as temporary storage during calculations. [1] https://webassembly.github.io/spec/core/binary/instructions.html#variab= le-instructions Signed-off-by: Kohei Tokunaga --- tcg/wasm/tcg-target.c.inc | 117 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index afaea76d5c..7f4ec250ff 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -116,10 +116,16 @@ static const uint8_t tcg_target_reg_index[TCG_TARGET_= NB_REGS] =3D { =20 #define REG_IDX(r) tcg_target_reg_index[r] =20 +/* Temporary local variables */ +#define TMP32_LOCAL_0_IDX 0 +#define TMP64_LOCAL_0_IDX 1 + typedef enum { OPC_IF =3D 0x04, OPC_ELSE =3D 0x05, OPC_END =3D 0x0b, + OPC_LOCAL_GET =3D 0x20, + OPC_LOCAL_SET =3D 0x21, OPC_GLOBAL_GET =3D 0x23, OPC_GLOBAL_SET =3D 0x24, =20 @@ -163,9 +169,12 @@ typedef enum { OPC_I64_GE_U =3D 0x5a, =20 OPC_I32_ADD =3D 0x6a, + OPC_I32_AND =3D 0x71, + OPC_I32_OR =3D 0x72, OPC_I32_SHL =3D 0x74, OPC_I32_SHR_S =3D 0x75, OPC_I32_SHR_U =3D 0x76, + OPC_I32_ROTR =3D 0x78, =20 OPC_I64_ADD =3D 0x7c, OPC_I64_SUB =3D 0x7d, @@ -176,6 +185,7 @@ typedef enum { OPC_I64_SHL =3D 0x86, OPC_I64_SHR_S =3D 0x87, OPC_I64_SHR_U =3D 0x88, + OPC_I64_ROTR =3D 0x8a, =20 OPC_I32_WRAP_I64 =3D 0xa7, OPC_I64_EXTEND_I32_S =3D 0xac, @@ -524,6 +534,110 @@ static void tcg_wasm_out_sextract(TCGContext *s, TCGR= eg dest, TCGReg arg1, tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(dest)); } =20 +static void tcg_wasm_out_bswap64( + TCGContext *s, TCGReg dest, TCGReg src) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(src)); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 32); + tcg_wasm_out_op(s, OPC_I64_ROTR); + tcg_wasm_out_op_idx(s, OPC_LOCAL_SET, TMP64_LOCAL_0_IDX); + + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, TMP64_LOCAL_0_IDX); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 0xff000000ff000000); + tcg_wasm_out_op(s, OPC_I64_AND); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 24); + tcg_wasm_out_op(s, OPC_I64_SHR_U); + + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, TMP64_LOCAL_0_IDX); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 0x00ff000000ff0000); + tcg_wasm_out_op(s, OPC_I64_AND); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 8); + tcg_wasm_out_op(s, OPC_I64_SHR_U); + + tcg_wasm_out_op(s, OPC_I64_OR); + + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, TMP64_LOCAL_0_IDX); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 0x0000ff000000ff00); + tcg_wasm_out_op(s, OPC_I64_AND); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 8); + tcg_wasm_out_op(s, OPC_I64_SHL); + + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, TMP64_LOCAL_0_IDX); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 0x000000ff000000ff); + tcg_wasm_out_op(s, OPC_I64_AND); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 24); + tcg_wasm_out_op(s, OPC_I64_SHL); + + tcg_wasm_out_op(s, OPC_I64_OR); + + tcg_wasm_out_op(s, OPC_I64_OR); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(dest)); +} + +static void tcg_wasm_out_bswap32( + TCGContext *s, TCGReg dest, TCGReg src, int flags) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(src)); + tcg_wasm_out_op(s, OPC_I32_WRAP_I64); + tcg_wasm_out_op_idx(s, OPC_LOCAL_SET, TMP32_LOCAL_0_IDX); + + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, TMP32_LOCAL_0_IDX); + tcg_wasm_out_op_const(s, OPC_I32_CONST, 16); + tcg_wasm_out_op(s, OPC_I32_ROTR); + tcg_wasm_out_op_idx(s, OPC_LOCAL_SET, TMP32_LOCAL_0_IDX); + + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, TMP32_LOCAL_0_IDX); + tcg_wasm_out_op_const(s, OPC_I32_CONST, 0xff00ff00); + tcg_wasm_out_op(s, OPC_I32_AND); + tcg_wasm_out_op_const(s, OPC_I32_CONST, 8); + tcg_wasm_out_op(s, OPC_I32_SHR_U); + + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, TMP32_LOCAL_0_IDX); + tcg_wasm_out_op_const(s, OPC_I32_CONST, 0x00ff00ff); + tcg_wasm_out_op(s, OPC_I32_AND); + tcg_wasm_out_op_const(s, OPC_I32_CONST, 8); + tcg_wasm_out_op(s, OPC_I32_SHL); + + tcg_wasm_out_op(s, OPC_I32_OR); + if (flags & TCG_BSWAP_OS) { + tcg_wasm_out_op(s, OPC_I64_EXTEND_I32_S); + } else { + tcg_wasm_out_op(s, OPC_I64_EXTEND_I32_U); + } + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(dest)); +} + +static void tcg_wasm_out_bswap16( + TCGContext *s, TCGReg dest, TCGReg src, int flags) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(src)); + tcg_wasm_out_op(s, OPC_I32_WRAP_I64); + tcg_wasm_out_op_idx(s, OPC_LOCAL_SET, TMP32_LOCAL_0_IDX); + + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, TMP32_LOCAL_0_IDX); + tcg_wasm_out_op_const(s, OPC_I32_CONST, 8); + tcg_wasm_out_op(s, OPC_I32_ROTR); + tcg_wasm_out_op_idx(s, OPC_LOCAL_SET, TMP32_LOCAL_0_IDX); + + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, TMP32_LOCAL_0_IDX); + tcg_wasm_out_op_const(s, OPC_I32_CONST, 0x000000ff); + tcg_wasm_out_op(s, OPC_I32_AND); + + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, TMP32_LOCAL_0_IDX); + tcg_wasm_out_op_const(s, OPC_I32_CONST, 0xff000000); + tcg_wasm_out_op(s, OPC_I32_AND); + tcg_wasm_out_op_const(s, OPC_I32_CONST, 16); + if (flags & TCG_BSWAP_OS) { + tcg_wasm_out_op(s, OPC_I32_SHR_S); + } else { + tcg_wasm_out_op(s, OPC_I32_SHR_U); + } + + tcg_wasm_out_op(s, OPC_I32_OR); + tcg_wasm_out_op(s, OPC_I64_EXTEND_I32_U); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(dest)); +} + static void tcg_wasm_out_mov(TCGContext *s, TCGReg ret, TCGReg arg) { tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg)); @@ -1448,6 +1562,7 @@ static void tgen_bswap16(TCGContext *s, TCGType type, if (flags & TCG_BSWAP_OS) { tcg_tci_out_sextract(s, TCG_TYPE_REG, a0, a0, 0, 16); } + tcg_wasm_out_bswap16(s, a0, a1, flags); } =20 static const TCGOutOpBswap outop_bswap16 =3D { @@ -1462,6 +1577,7 @@ static void tgen_bswap32(TCGContext *s, TCGType type, if (flags & TCG_BSWAP_OS) { tcg_tci_out_sextract(s, TCG_TYPE_REG, a0, a0, 0, 32); } + tcg_wasm_out_bswap32(s, a0, a1, flags); } =20 static const TCGOutOpBswap outop_bswap32 =3D { @@ -1473,6 +1589,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