From nobody Sat Nov 15 05:21:41 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=1755627885; cv=none; d=zohomail.com; s=zohoarc; b=cV0xfGaOyG0X7TU9AsZhFB+zK2acBwCrLApjVTaqSzDvTRJ+oT31VCwuXuCSqvy46czaif9Jci1kK4t1YQ1iWGzsfORmzQ8JciE9xrOosf52mdmSs1OjyMIQ8HchQaVzkotqkHpLjxMGcCkyB/+W2gC3v5sfo1896MEs+g39/Lk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755627885; 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=+FbK9ZmTXg2xQSJZsVexzLKV577HxgsyGAmP/Gwi0cg=; b=NdJwVJ5VN+1MJ/rp4mpegpXRGPYoAMDN3VLiB5lSwFbXzYvqT/7LfMuCeFFa3doFUqn8Uwh2d8vtvYBfzPjv5hTw293/q/DjaXwu5EsQ/hbue9/CuIV5OP6q+nz1y1qw0iu+eGh1z4J1J6tQaoTIdIxjVTOT4vk5C6WuNt/uryU= 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 1755627885199571.6089576593827; Tue, 19 Aug 2025 11:24:45 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoQys-0004r7-JY; Tue, 19 Aug 2025 14:22:47 -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 1uoQyq-0004qE-JV; Tue, 19 Aug 2025 14:22:44 -0400 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uoQyo-0003km-Dg; Tue, 19 Aug 2025 14:22:44 -0400 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-2449788923eso16448135ad.2; Tue, 19 Aug 2025 11:22:41 -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.22.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:22:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627760; x=1756232560; 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=+FbK9ZmTXg2xQSJZsVexzLKV577HxgsyGAmP/Gwi0cg=; b=Pu3YEvS8M+QWRbHejBl6Lg9oG2OhbT6LqbgUxD6m3bbfN5zDfQOSGqcSs9pjGafcCT Wk5qBp0ZLBF/UgixEBXNH2g62tpJTBpV+6uz61AU20z1gzHgXwod87j40rUL13geaCS6 u9xDkbmL6CvdYR9XAFOk4XZx74c3S6M3PEVqMEdOrSEMJfjEczJoCGEz8buolOcRyzKk 4T7v8J9UVLYFdKcPYGVaRDcJWIDm7/gDx2bgcqgNOkp3FEWtqwnjlDeq73xkyEoM9JDg 2fxipDESxESQMQV2b147Nlu3aG+cd8pXKMPNVnvgodXKOpy4Izd5VZt8EtDbWwu6Z63v JK5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627760; x=1756232560; 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=+FbK9ZmTXg2xQSJZsVexzLKV577HxgsyGAmP/Gwi0cg=; b=ItmKB1GIxSEcjmu3f+kb7FTo/xMbxHeI7rc7WX6hsgc56eFJm8KCumAjNrKPsHxVVI MW8iCUmYOp61N02XeMGf4SKRiBDRBTo5hTp76zU7M+XmmInP8uthemmzpMkWLGm+3tg1 wtG+UlJj29Ppw7IxvZcXMsyWXPGCvo8P/gm0J+1zbj5J93BrBoGFL+YYAQfhBHm6PH/w 6Q9mneri6Xz84p8sTsvyPNh+KCYZC3Hz10VwitNH4Fhms7JZjhCDF1BirpGp7bQ/8LMZ GVt3LXi6713zFJhee1Jfd5idZKGBi31zonYh56/RROWsvjaMlGOFpOxFUcyOUlD2Is+i wUWg== X-Forwarded-Encrypted: i=1; AJvYcCUZSXII99Xz+rqH5PesdLbGpvVpau31gxZK2hJ1ZLDX5f0AeNKBzHBkMxV2jVOH8ikYqtd0PzAKsw==@nongnu.org, AJvYcCVFkhJ3V1VRjDaTjjEHP9kQWI9+l8re0V225WhXC5rIYOKWjgfTGdJODnBp8v9UhqPv5C+1I5hzeDRs6Q==@nongnu.org X-Gm-Message-State: AOJu0YzpWwBMFBiv8bla+MZ6hRrZ4P4BHdOxRCh9A9/pIMctZuIMF86R hyzppBxfz6GKDyvXAHBrDoHzi1vbz1N4wOGPVUxqCceMBJLyWoveG/Rw6R4g9lsY X-Gm-Gg: ASbGncs/W/8vl78xUmDya22B+VXTG2qlg1V7VJSScG/BlGVnizJxL21VCi5BTzEUS2V ryv57xSZ9WIDNYjqGRmBO5+tBGHkeTP4p+6nFlC1cHgZ8baTwEJ4EdBClI0IJGBQKwQ84VJ11WS on0zwRZvRZ7OSy7xa7sp6f0F/TUrvmnUBpU+dqtJeNRlki6+tgNmMx6/129AC8s6E4pPTrwkS1r PB0y5hC3nQst7iK3NI+CMbbvQ2sZtNuv8oudvoPR1slj0t1XqXcHlp/SJ4QF/gPU5kxoP928G0l s8Z/TbW4JoINqcqRs2S0fyUYUKdNa/A1JpJlUbyadXQYdM/zK81t0tXfR92ozuuVjBGhAViujvx dxTePukelu3GKdrKSfL4/YcWXwQOl8HIW X-Google-Smtp-Source: AGHT+IEDH6izQI/U6+5s+QiG5ryEh4M4Zw+H4hvy/7aeTEr0e6FPH6hKsGVlP8FRZJvogGt+aAtfKA== X-Received: by 2002:a17:902:ef07:b0:235:f143:9b07 with SMTP id d9443c01a7336-245e04347c9mr42010395ad.5.1755627760155; Tue, 19 Aug 2025 11:22: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 , 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 01/35] meson: Add wasm64 support to the --cpu flag Date: Wed, 20 Aug 2025 03:21:30 +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::633; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pl1-x633.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: 1755627885746116600 Content-Type: text/plain; charset="utf-8" wasm64 target enables 64bit pointers using Emscripten's -sMEMORY64=3D1 flag[1]. This enables QEMU to run 64bit guests. Although the configure script uses "uname -m" as the fallback value when "cpu" is empty, this can't be used for Emscripten which targets to Wasm. So, in wasm build, this commit fixes configure to require --cpu flag to be explicitly specified by the user. [1] https://emscripten.org/docs/tools_reference/settings_reference.html#mem= ory64 Signed-off-by: Kohei Tokunaga Reviewed-by: Pierrick Bouvier --- configure | 6 +++++- meson.build | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) V1: - This commit is under review in another patch series so please ignore it here. diff --git a/configure b/configure index 825057ebf1..7f3893a42f 100755 --- a/configure +++ b/configure @@ -365,7 +365,6 @@ elif check_define __APPLE__; then host_os=3Ddarwin elif check_define EMSCRIPTEN ; then host_os=3Demscripten - cpu=3Dwasm32 cross_compile=3D"yes" else # This is a fatal error, but don't report it yet, because we @@ -425,6 +424,8 @@ elif check_define __aarch64__ ; then cpu=3D"aarch64" elif check_define __loongarch64 ; then cpu=3D"loongarch64" +elif check_define EMSCRIPTEN ; then + error_exit "wasm32 or wasm64 must be specified to the cpu flag" else # Using uname is really broken, but it is just a fallback for architectu= res # that are going to use TCI anyway @@ -535,6 +536,9 @@ case "$cpu" in wasm32) CPU_CFLAGS=3D"-m32" ;; + wasm64) + CPU_CFLAGS=3D"-m64 -sMEMORY64=3D1" + ;; esac =20 if test -n "$host_arch" && { diff --git a/meson.build b/meson.build index e53cd5b413..291fe3f0d0 100644 --- a/meson.build +++ b/meson.build @@ -52,7 +52,7 @@ qapi_trace_events =3D [] bsd_oses =3D ['gnu/kfreebsd', 'freebsd', 'netbsd', 'openbsd', 'dragonfly',= 'darwin'] supported_oses =3D ['windows', 'freebsd', 'netbsd', 'openbsd', 'darwin', '= sunos', 'linux', 'emscripten'] supported_cpus =3D ['ppc', 'ppc64', 's390x', 'riscv32', 'riscv64', 'x86', = 'x86_64', - 'arm', 'aarch64', 'loongarch64', 'mips', 'mips64', 'sparc64', 'wasm32'] + 'arm', 'aarch64', 'loongarch64', 'mips', 'mips64', 'sparc64', 'wasm32', = 'wasm64'] =20 cpu =3D host_machine.cpu_family() =20 @@ -916,7 +916,7 @@ if have_tcg if not get_option('tcg_interpreter') error('Unsupported CPU @0@, try --enable-tcg-interpreter'.format(cpu= )) endif - elif host_arch =3D=3D 'wasm32' + elif host_arch =3D=3D 'wasm32' or host_arch =3D=3D 'wasm64' if not get_option('tcg_interpreter') error('WebAssembly host requires --enable-tcg-interpreter') endif --=20 2.43.0 From nobody Sat Nov 15 05:21:41 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=1755628095; cv=none; d=zohomail.com; s=zohoarc; b=fdL2ZqhHm10WS8Y+QBKNVHi9A0aeBxzR3jTKUh1LO+puje0bID0G2BPaGH10oOoW6aZd26sLM7OLiSe8ajHkvjc+ck2/oVxc4xP++zwp4wyLUgCXaQH/4fMwJxdJJAE7IDOJQet5W3goc0bqhbKzPfUkzBjlK0hMl5l5M+HVcR4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755628095; h=Content-Type: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=zvwZVYkwT7L+29JJJedvOVMf75yz0b8pNBu4QKDuOyI=; b=JY6Rm5S4b3GuHRHvh4tQUFvbHljS0R/7wAO27KUIMgolY8RE99bJ3V5hdLZt0zNTGncARDhBy+TuYosCRPxjXWbRExIFGEZbsEHWKeqafynIItX5YtuPPUWltNLs1rG/7yrOke6ejH6GjdRLSHXazOxEu56oZdSbQFAeX7C6K+I= 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 1755628095684361.0541282671744; Tue, 19 Aug 2025 11:28:15 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoQyx-0004sG-5z; Tue, 19 Aug 2025 14:22: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 1uoQyw-0004rx-6F; Tue, 19 Aug 2025 14:22:50 -0400 Received: from mail-pl1-x62a.google.com ([2607:f8b0:4864:20::62a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uoQyu-0003lR-7t; Tue, 19 Aug 2025 14:22:49 -0400 Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-244581cc971so66734785ad.2; Tue, 19 Aug 2025 11:22:47 -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.22.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:22:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627766; x=1756232566; 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=zvwZVYkwT7L+29JJJedvOVMf75yz0b8pNBu4QKDuOyI=; b=cgHyR3ezyAIJu7KHaR4zlNK8BomaDoWXQzVUIF+/oLM8Mn8ZTnrhdyqu1Rot3KdFvQ LEgSd8SddgMIOxxe5ApLKFhxbDmVWcu5hXRyw4Q7ygiydarURg8GnQU7B/UaahYf6Mef 69OeM5RO4kcHJiR0eK/hDiOo6jWbr22ohrsqIVed5j9LWvpy4i7+GlKN2SgO8dlPZ0z8 SYDEJ/vSZ1ftGIpQefo1UC6BL9OtqwgUFV6vccpbgJ0FeKBrRGUITU01lL9QvzYNjfFn ENHfwtzD7vgB2mzdEsFAwDE+hmX/pBbN25Tb036NUB2s0517IXuSi3IiItJxY2eCHa1G iJcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627766; x=1756232566; 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=zvwZVYkwT7L+29JJJedvOVMf75yz0b8pNBu4QKDuOyI=; b=w1bLX9+X73SAtJ2tjmiyqpNW9Mg807RVOby75Qb92XW83EsfQJLnG/DfsUErBeEmEr /8I/oOdzCmWlooJNF9JGGnFbJUZkETdRk8ZG7RE2EN78KtgQ/mnOt6AygF6B+EMi+ml6 3bgPeGXu7B2KOZXWOSvzdV/icoc2jogIUwn8hE1u4D840I4mdsaoZotsaR38vsxgUlhg NXbxIYqPQdj0kGVbabQ2UyEoRmVEWARNGaTsV4bClJ44RRvw5nJhN3S5UkzbxLlDO/G0 6tiyDnHMsm00UGXqH8pfIuJfVVC1KNHSW45tbaIsTZuMGvh/3u9Yc5pNH7ZG/UV4dpD2 2heg== X-Forwarded-Encrypted: i=1; AJvYcCWDo0zkCLNYU3t4EPBGznu9+rrZAOSRL90xNKZoiA+jZD1QzJELQ+l/wZJat5pFk1BUkszStUPdtQ==@nongnu.org, AJvYcCXnr1eCxmyB2C/hfNLi4uG0Z1RYZtQY4dxUwHC4u5YR4udf9M2zbCAMMDSt9IvOgzlZhxlGdMH2lhz+PQ==@nongnu.org X-Gm-Message-State: AOJu0Yzmbv2PBPGLEMfm0dJHEXHhQ5Xyj2Pulbu5jv4U3UjNbKrGT+3F iMGoVdzBQbtBHTjr7PUz4QlawrWBKf4D1x2GU9L9rgQSzhRodly+lVgZw1kazE2n X-Gm-Gg: ASbGncuOyaDUZd/II1Mk4wIdN1SHLKnLif1BY/0QLejenW/WfHX6H3pIeahaJGGHc9w +n2bpjvDimeksD3938Y8XSxNcF5BdYGJ3lKbo5jC6vs1yiWiVmUub7j531q3rG/W+HIjIyRJzAj sR8K3FlotQbWri+w3hrOz5Pr+9Pw/kGiiWz5MwkAXVkgxe4sFPXKEigzn2C5irYWbMF+ZKIO0b5 arcTd6rXls5/InpxqtVjcrPkkDNy73VPvOidis4eAU/cdvm2RzhFiQW8CKC+7GKcpkMoSbkY/gi u+Br8sSM9rOL+wVn5vyXkTp6p4VpLUnL3H6xV8ilMHkafmBdbJaBpDGVx/EFzB99B+n6n01XriQ toS4JSC26ZBBc7rF0xv6n8Q== X-Google-Smtp-Source: AGHT+IEnH7r5HCtDn1tBZq14+QqiRFSdewNpZPU4arw+CqSkaiw8ixeWuTyGfm0qKedCQAY0kVuU1A== X-Received: by 2002:a17:902:f682:b0:23f:df36:5f0c with SMTP id d9443c01a7336-245e047d995mr51450535ad.29.1755627766028; Tue, 19 Aug 2025 11:22: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 , 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 02/35] configure: Enable to propagate -sMEMORY64 flag to Emscripten Date: Wed, 20 Aug 2025 03:21:31 +0900 Message-ID: <574dee5a5cb19d15dbfbe94cea31de23189596a9.1755623521.git.ktokunaga.mail@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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::62a; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pl1-x62a.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: 1755628098170116600 Currently there are some engines that don't support wasm64 (e.g. unsupported on Safari[1]). To mitigate this issue, the configure script allows the user to use Emscripten's compatibility feature, "-sMEMORY64=3D2" flag[2]. Emscripten's "-sMEMORY64=3D2" flag still enables 64bit pointers in C code. = But this flag lowers the output binary into wasm32, with limiting the maximum memory size to 4GB. So QEMU can run on wasm32 engines. [1] https://webassembly.org/features/ [2] https://emscripten.org/docs/tools_reference/settings_reference.html#mem= ory64 Signed-off-by: Kohei Tokunaga Reviewed-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Pierrick Bouvier --- configure | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) V1: - This commit is under review in another patch series so please ignore it here. diff --git a/configure b/configure index 7f3893a42f..0587577da9 100755 --- a/configure +++ b/configure @@ -182,6 +182,10 @@ EXTRA_CXXFLAGS=3D"" EXTRA_OBJCFLAGS=3D"" EXTRA_LDFLAGS=3D"" =20 +# The value is propagated to Emscripten's "-sMEMORY64" flag. +# https://emscripten.org/docs/tools_reference/settings_reference.html#memo= ry64 +wasm64_memory64=3D1 + # Default value for a variable defining feature "foo". # * foo=3D"no" feature will only be used if --enable-foo arg is given # * foo=3D"" feature will be searched for, and if found, will be used @@ -239,6 +243,8 @@ for opt do ;; --without-default-features) default_feature=3D"no" ;; + --wasm64-32bit-address-limit) wasm64_memory64=3D"2" + ;; esac done =20 @@ -537,7 +543,7 @@ case "$cpu" in CPU_CFLAGS=3D"-m32" ;; wasm64) - CPU_CFLAGS=3D"-m64 -sMEMORY64=3D1" + CPU_CFLAGS=3D"-m64 -sMEMORY64=3D$wasm64_memory64" ;; esac =20 @@ -795,6 +801,8 @@ for opt do ;; --disable-rust) rust=3Ddisabled ;; + --wasm64-32bit-address-limit) + ;; # everything else has the same name in configure and meson --*) meson_option_parse "$opt" "$optarg" ;; @@ -920,6 +928,8 @@ Advanced options (experts only): --disable-containers don't use containers for cross-building --container-engine=3DTYPE which container engine to use [$container_eng= ine] --gdb=3DGDB-path gdb to use for gdbstub tests [$gdb_bin] + --wasm64-32bit-address-limit Restrict wasm64 address space to 32-bit (de= fault + is to use the whole 64-bit range). EOF meson_options_help cat << EOF --=20 2.43.0 From nobody Sat Nov 15 05:21:41 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=1755627963; cv=none; d=zohomail.com; s=zohoarc; b=Zz7h9il3pMxnv4twcVNeUW3i0nm3hMPcZ9Dujfhkif2ZJsmlBWb1g3WOiRSPI8ogEy/UYPGGP+q0kATPOgddYl4W59e/G+OQp32zbFFkCrggL4SKRQZwRUDGn7FwcDzZerEXiXywrK1HEV2Ix59UJ1cKlLpNSV7btqDMXnLKGFQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755627963; 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=m3ANe70t7odIvNSjekeC3CnCW0I6T4H5dpW4Ym224S4=; b=hNt1szpzdLcV8s/PnoFj+LeTxINoHOZdMnGXHWZHTNUQV6u8ti74++VZME05S/JCJqRll0+BWRQApUZR1Ypzi9Vm1H8kpXYpbBap5ma2HQWO+z75ZKO2+vxTMk7ECdwNuukCPS3AaoeXV9bTL28mDxCvQndduCEhDt/X0un5Rg8= 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 1755627963322885.3010364004577; Tue, 19 Aug 2025 11:26:03 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoQz3-0004uH-F3; Tue, 19 Aug 2025 14:22: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 1uoQz2-0004tl-0p; Tue, 19 Aug 2025 14:22:56 -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 1uoQz0-0003lz-4B; Tue, 19 Aug 2025 14:22:55 -0400 Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-2445805aa2eso46766095ad.1; Tue, 19 Aug 2025 11:22:53 -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.22.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:22:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627772; x=1756232572; 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=m3ANe70t7odIvNSjekeC3CnCW0I6T4H5dpW4Ym224S4=; b=dH/mLsmuoDVkHFQf5Le+tEAyL+mbFrvXbwXTt8Mf/H8jI1Oe+j+xX40iI5Ef/0/cNk GKLaYXHi7WC14MQFmRxZl9ybt6O40VIvzYwCsITICVQVchPPFAwcKEsWshu75XOVIoOS BfOVPJE4JuBK9kSxk4gkAvZGv66DkUdJ1tmHPAvIm5XRCRNUNDOPhsQO2mRYlGWuO+3O bDjGjpyRqV+W60Rzap7tUPWL+IPak/B4F3X6UdBtne71jugtJo+a8mhefk9PZIWhLUad 2bEf0KN3nC3CXGzwHEqo4J2O9ytCOJorJQgVRvLYdgV2yghOo3UHrsADDDOatqyJkKRQ Uwbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627772; x=1756232572; 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=m3ANe70t7odIvNSjekeC3CnCW0I6T4H5dpW4Ym224S4=; b=OB3rVW/r9oWkc9PcxvhR5UvYhD6ZRBiolMxIYbIxO0FSQBiIQi2TfpnJQw9XYxEvV6 sJAGx41tad2uXg3eeF04rf3Cg6ByQUOtepNnc8dISOeVldSHuL1CtUNF4RE92WdjQZJQ s7WocjFPha9WdNHAmaBYgNEDfZ7IwYM9Y5uEEQ/css1p7Mg6/Xk3YTiLzt3F1VQ2kpe/ 4T79wdkwoP4VqzDCI60rMN4vV/KjxQ4hE2lb2cnjxnvPcJRuMek4h36hIm/ObQOKrq4/ FQSnMy27VhHNnK12WIfCnr84VGW0PapRT6dOx18rSdjUTIUkz6Jr4yIxLeNoD+OP5+Js VQIA== X-Forwarded-Encrypted: i=1; AJvYcCVIBRiD0nRRKGpySP3TYk8/JVRMIVrhUGhl3OukQbQVsajs+Hkz5f+Z8xDv81E8/Tq9Xtezu4TuMxz10g==@nongnu.org, AJvYcCVoAjy5GdKg2l/E/IbvehYhPAjDn4fAbzFlCJdSsMaOzyZIs7z1YYCJ5uinYHpCccHIjjyU/YxxYQ==@nongnu.org X-Gm-Message-State: AOJu0Yz1g26ZsDVrpt0vaos/XAnIBfHqLjbLSHBp/d2W1OuahLM4uwvG 3KCVT9eDs/qdEPsEKCz7biwgAbLY2O8vVlg1tFqzM7FD9k3qYvJT1pYvrf8o5ZFs X-Gm-Gg: ASbGncucTCqnYU1OpEwWoUgtlZtrBZZcLDG+ViOJ9j6uFtRQfCX+ul9Fjmc5LOntV97 nMvaYU5sTkort48o4Vfxun2bYnK0Wia+d1Pberx+7lyPqEz6HpzbxOdoytBuRVyajyK5CSBn7dG TFMEGzCq8k8B7fKwGD4TM9fLlnuQIjh6oRiVN2iicUAhaxsPuBXGMQ3lysnNAgGulRSBDOI/97W RZE2C2imDAtdhiydSOJdhbiQ38CrBnGwv1fISXZlTYhhqwa+Hho6TB78WtaSB0ElRGwbGxS+KF2 LZzRTkLsPDDcRpQ9p4kkOIxEazBvoJ4YC4TfsP8tkHq5v5SPUzuA7u2Hn+CB0LnzV/UO1qCYRNQ 6yI5XLpSULjE6sl94Wu2C3g== X-Google-Smtp-Source: AGHT+IFSOfmqmQwlY5FKYTKLcAxqFVoAIzlmCFXkkROj2pLEYm36aa1f5uLBEAVXs7zJRrt1Bdnxfg== X-Received: by 2002:a17:903:3bcb:b0:23f:fa79:15d0 with SMTP id d9443c01a7336-245e04b8650mr52168525ad.46.1755627771666; Tue, 19 Aug 2025 11:22:51 -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 03/35] dockerfiles: Add support for wasm64 to the wasm Dockerfile Date: Wed, 20 Aug 2025 03:21:32 +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::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: 1755627964877116600 Content-Type: text/plain; charset="utf-8" This commit fixes Dockerfile of the wasm build to support both of wasm32 and wasm64 build. Dockerfile takes the following build arguments and use these values for building dependencies. - TARGET_CPU: target wasm arch (wasm32 or wasm64) - WASM64_MEMORY64: target -sMEMORY64 mode (1 or 2) Signed-off-by: Kohei Tokunaga Reviewed-by: Pierrick Bouvier --- MAINTAINERS | 2 +- ...2-cross.docker =3D> emsdk-wasm-cross.docker} | 29 ++++++++++++++----- 2 files changed, 23 insertions(+), 8 deletions(-) rename tests/docker/dockerfiles/{emsdk-wasm32-cross.docker =3D> emsdk-wasm= -cross.docker} (85%) V1: - This commit is under review in another patch series so please ignore it here. diff --git a/MAINTAINERS b/MAINTAINERS index a07086ed76..433a44118d 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -647,7 +647,7 @@ F: include/system/os-wasm.h F: os-wasm.c F: util/coroutine-wasm.c F: configs/meson/emscripten.txt -F: tests/docker/dockerfiles/emsdk-wasm32-cross.docker +F: tests/docker/dockerfiles/emsdk-wasm-cross.docker =20 Alpha Machines -------------- diff --git a/tests/docker/dockerfiles/emsdk-wasm32-cross.docker b/tests/doc= ker/dockerfiles/emsdk-wasm-cross.docker similarity index 85% rename from tests/docker/dockerfiles/emsdk-wasm32-cross.docker rename to tests/docker/dockerfiles/emsdk-wasm-cross.docker index 60a7d02f56..4b41be62ab 100644 --- a/tests/docker/dockerfiles/emsdk-wasm32-cross.docker +++ b/tests/docker/dockerfiles/emsdk-wasm-cross.docker @@ -1,14 +1,17 @@ # syntax =3D docker/dockerfile:1.5 =20 -ARG EMSDK_VERSION_QEMU=3D3.1.50 +ARG EMSDK_VERSION_QEMU=3D4.0.10 ARG ZLIB_VERSION=3D1.3.1 ARG GLIB_MINOR_VERSION=3D2.84 ARG GLIB_VERSION=3D${GLIB_MINOR_VERSION}.0 ARG PIXMAN_VERSION=3D0.44.2 -ARG FFI_VERSION=3Dv3.4.7 +ARG FFI_VERSION=3Dv3.5.2 ARG MESON_VERSION=3D1.5.0 +ARG TARGET_CPU=3Dwasm32 +ARG WASM64_MEMORY64=3D0 =20 -FROM emscripten/emsdk:$EMSDK_VERSION_QEMU AS build-base +FROM emscripten/emsdk:$EMSDK_VERSION_QEMU AS build-base-common +ARG TARGET_CPU ARG MESON_VERSION ENV TARGET=3D/builddeps/target ENV CPATH=3D"$TARGET/include" @@ -33,8 +36,8 @@ RUN < /cross.meson [host_machine] system =3D 'emscripten' -cpu_family =3D 'wasm32' -cpu =3D 'wasm32' +cpu_family =3D '${TARGET_CPU}' +cpu =3D '${TARGET_CPU}' endian =3D 'little' =20 [binaries] @@ -46,6 +49,16 @@ pkgconfig =3D ['pkg-config', '--static'] EOT EOF =20 +FROM build-base-common AS build-base-wasm32 + +FROM build-base-common AS build-base-wasm64 +ARG WASM64_MEMORY64 +ENV CFLAGS=3D"$CFLAGS -sMEMORY64=3D${WASM64_MEMORY64}" +ENV CXXFLAGS=3D"$CXXFLAGS -sMEMORY64=3D${WASM64_MEMORY64}" +ENV LDFLAGS=3D"$LDFLAGS -sMEMORY64=3D${WASM64_MEMORY64}" + +FROM build-base-${TARGET_CPU} AS build-base + FROM build-base AS zlib-dev ARG ZLIB_VERSION RUN mkdir -p /zlib @@ -56,17 +69,19 @@ RUN emconfigure ./configure --prefix=3D$TARGET --static RUN emmake make install -j$(nproc) =20 FROM build-base AS libffi-dev +ARG TARGET_CPU +ARG WASM64_MEMORY64 ARG FFI_VERSION RUN mkdir -p /libffi RUN git clone https://github.com/libffi/libffi /libffi WORKDIR /libffi RUN git checkout $FFI_VERSION RUN autoreconf -fiv -RUN emconfigure ./configure --host=3Dwasm32-unknown-linux \ +RUN emconfigure ./configure --host=3D${TARGET_CPU}-unknown-linux \ --prefix=3D$TARGET --enable-static \ --disable-shared --disable-dependency-tracking \ --disable-builddir --disable-multi-os-directory \ - --disable-raw-api --disable-docs + --disable-raw-api --disable-docs WASM64_MEMORY64=3D${WASM64_MEMORY64} RUN emmake make install SUBDIRS=3D'include' -j$(nproc) =20 FROM build-base AS pixman-dev --=20 2.43.0 From nobody Sat Nov 15 05:21:41 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=1755627842; cv=none; d=zohomail.com; s=zohoarc; b=M4QklYVEGPYRQvpyTQStLGRyPPIru8fHUdNoseL8BwuLgONNTxQJZoXL1/S2hkRXVI0179O7deyuvucVOmIN5PqAozD1wUjSfT6rBZdRp1pIJA7jC+Ysh3lGt0IM1pY80m7l1pa3SaBu1NGwIgbWU0EvL9zs9Opbt/JzXAONq1c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755627842; 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=mnwM1NKHDi9Rdp/eRcwiDwz6kKklNhllSveakiPYS/g=; b=b0MKOw+KOmDZrKiKm36Eg7UXyv1mcPzahkoOE6igcuhyGWBCS5FDQf25mr7FOQs7i0SQW+UuM7gK2CJZtEVfbku+Cg9BNeyVYB8je99b4EODpg/aHXRjmnYvzr2KQoEI8QItyNDbLpRCtRjqgjx2FyoE9pA5KSDPY1Crl/LZaxk= 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 1755627842131514.0135726682532; Tue, 19 Aug 2025 11:24:02 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoQz7-0004w1-V2; Tue, 19 Aug 2025 14:23:02 -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 1uoQz7-0004vT-Cc; Tue, 19 Aug 2025 14:23:01 -0400 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uoQz5-0003mh-Hy; Tue, 19 Aug 2025 14:23:01 -0400 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-24458263458so45862225ad.3; Tue, 19 Aug 2025 11:22:58 -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.22.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:22:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627777; x=1756232577; 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=mnwM1NKHDi9Rdp/eRcwiDwz6kKklNhllSveakiPYS/g=; b=SuZiL/le0pNdXBCetxuteCOVM6vTSzqw2E8XMe0czcxaONhP1eftGMZE7cnufhH/fj okP2Ltl34SIqrG1jELFtf1aeKLhk3aCX4cRiyM8GSyeSkyF+wmtdC5tp44u/0zePaovT HsY4mnJZwMuFcwuigEseYZ1xuT08vBwojNUGck1Ldtm95wz844uq78PQFnK/m5cv7a8x LXyAOCl6IzLSpc36gfTw0w4L4retHR14EeJ9gWujK8pKaLJx167JAdawvxx9qtKjrMVW HpnI2Ps7F8WQUYgbNLqrhroZJ+WR84/yzAPUyrJXi7E7PvdnywMh14Hn/STIwYIqS7LR 09lg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627777; x=1756232577; 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=mnwM1NKHDi9Rdp/eRcwiDwz6kKklNhllSveakiPYS/g=; b=BCXlVG2FL0dVSMKDNlLHgfg/NRKQmQDqohN2n0qxlSM7XXDgb67FhYaxs4g0woBGpB MtDPQCszFdMZN5rMlG4FWKgsVnsNNMcF7tRMbW5lpZJzKGATVFmslBNZruqBEHXGRE8A VOxp5z2IqUzWYCNk9tSHNYuLZ6u9PyB5sbNSdrn8thIvIrsnncpkPBfNK0FupRmVqbDz 9bp0NLz2NduzfalTL/f0DQdsme4MGr/nd2unPyXY11nZaS9GJxXZyS8kwLR2d/GY/74z WbEsYDOuEryYrvdYdKNtGB3bmvuxHwBhf2LU/mBD9kyJ7hxfbC3NarDadDVt2UwANCfk pIhw== X-Forwarded-Encrypted: i=1; AJvYcCV/jvv7JRIgTihAh6PnPCRJc2nNMe7sL212WfqFMO4BF8dcU5/y+4HMdCveQt+hQFYqZMiZdB2rc7pY2g==@nongnu.org, AJvYcCWPrxLi490RVTBnw0T1dzinetmNkWo4aA9+H0wkUkBnm0bbOeWQSHOyPgodmS92qyPq3k4UcYoK0A==@nongnu.org X-Gm-Message-State: AOJu0Yx2mziGM+TMAPbdOd8vxvYCZs54d/qUtfMnUuTYuw9MJWw91ykh UPgYmTMOCe1Tim6N12CokMQIxsx22CZXQGBPI5dbUkBa0ISSNKyDVxiWc0QLD6tL X-Gm-Gg: ASbGncvMo7nM//NhFMQRe/nfwh/22oco7pv9vCA2/uZBaKFgyqwPgtm5v1AsxhxC4mG UfDKgsQor+bNN6snDCClULITMFA/Z3OVLe+yJBud80xvH0WNpu6GZbev6GRhrLNNGz1s3Map/9n k1z24mlePLsS8LCcBjrlUkjDB1eBEqmFBDJEoQrvbTtB40WlMf9hYzacgkODzQ/t2w/vu4kzLq0 lWe13abepJMvFDkeYZEq0Ke8T4MQTKefh+M7Ot3wI1UEMgagqTuusTRUOhwK5OjSpa199tiwCzq 3I3lG7SrAmvM15swlB2lhHTaZmjOw0KmjzTlSsj80iKvHLi2r/ZewuXddgTDgYknPbHe2y/sq9n Ktgf9kxCaTJESNtYyIlaXfqsvmgY9UvIp X-Google-Smtp-Source: AGHT+IFBsiEtnYUR+SskuxqrM1PYZdjvBSImy/uoMDx63y0gSjeoy+527k5Zq5vjFVAwnR57bFS7YA== X-Received: by 2002:a17:903:2ac5:b0:234:9656:7db9 with SMTP id d9443c01a7336-245e04866a6mr49403075ad.32.1755627777331; Tue, 19 Aug 2025 11:22: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 , 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 04/35] .gitlab-ci.d: Add build tests for wasm64 Date: Wed, 20 Aug 2025 03:21:33 +0900 Message-ID: <85c09a7c387d8fb9bbcd5b887c3d219c59a0807b.1755623521.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::633; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pl1-x633.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: 1755627845565124100 Content-Type: text/plain; charset="utf-8" The wasm builds are tested for 3 targets: wasm32, wasm64(-sMEMORY64=3D1) and wasm64(-sMEMORY64=3D2). The CI builds the containers using the same Dockerf= ile (emsdk-wasm-cross.docker) with different build args. Signed-off-by: Kohei Tokunaga --- .gitlab-ci.d/buildtest.yml | 26 ++++++++++++++++++++++---- .gitlab-ci.d/container-cross.yml | 20 ++++++++++++++++++-- .gitlab-ci.d/container-template.yml | 4 +++- 3 files changed, 43 insertions(+), 7 deletions(-) V1: - This commit is under review in another patch series so please ignore it here. diff --git a/.gitlab-ci.d/buildtest.yml b/.gitlab-ci.d/buildtest.yml index d888a60063..77ae8f8281 100644 --- a/.gitlab-ci.d/buildtest.yml +++ b/.gitlab-ci.d/buildtest.yml @@ -787,11 +787,29 @@ coverity: # Always manual on forks even if $QEMU_CI =3D=3D "2" - when: manual =20 -build-wasm: +build-wasm32-32bit: extends: .wasm_build_job_template timeout: 2h needs: - job: wasm-emsdk-cross-container + job: wasm32-32bit-emsdk-cross-container variables: - IMAGE: emsdk-wasm32-cross - CONFIGURE_ARGS: --static --disable-tools --enable-debug --enable-tcg-i= nterpreter + IMAGE: emsdk-wasm32-32bit-cross + CONFIGURE_ARGS: --static --cpu=3Dwasm32 --disable-tools --enable-debug= --enable-tcg-interpreter + +build-wasm64-64bit: + extends: .wasm_build_job_template + timeout: 2h + needs: + job: wasm64-64bit-emsdk-cross-container + variables: + IMAGE: emsdk-wasm64-64bit-cross + CONFIGURE_ARGS: --static --cpu=3Dwasm64 --disable-tools --enable-debug= --enable-tcg-interpreter + +build-wasm64-32bit: + extends: .wasm_build_job_template + timeout: 2h + needs: + job: wasm64-32bit-emsdk-cross-container + variables: + IMAGE: emsdk-wasm64-32bit-cross + CONFIGURE_ARGS: --static --cpu=3Dwasm64 --wasm64-32bit-address-limit -= -disable-tools --enable-debug --enable-tcg-interpreter diff --git a/.gitlab-ci.d/container-cross.yml b/.gitlab-ci.d/container-cros= s.yml index 8d3be53b75..84c4be49f4 100644 --- a/.gitlab-ci.d/container-cross.yml +++ b/.gitlab-ci.d/container-cross.yml @@ -92,7 +92,23 @@ win64-fedora-cross-container: variables: NAME: fedora-win64-cross =20 -wasm-emsdk-cross-container: +wasm32-32bit-emsdk-cross-container: extends: .container_job_template variables: - NAME: emsdk-wasm32-cross + NAME: emsdk-wasm32-32bit-cross + BUILD_ARGS: --build-arg TARGET_CPU=3Dwasm32 + DOCKERFILE: emsdk-wasm-cross + +wasm64-64bit-emsdk-cross-container: + extends: .container_job_template + variables: + NAME: emsdk-wasm64-64bit-cross + BUILD_ARGS: --build-arg TARGET_CPU=3Dwasm64 --build-arg WASM64_MEMORY6= 4=3D1 + DOCKERFILE: emsdk-wasm-cross + +wasm64-32bit-emsdk-cross-container: + extends: .container_job_template + variables: + NAME: emsdk-wasm64-32bit-cross + BUILD_ARGS: --build-arg TARGET_CPU=3Dwasm64 --build-arg WASM64_MEMORY6= 4=3D2 + DOCKERFILE: emsdk-wasm-cross diff --git a/.gitlab-ci.d/container-template.yml b/.gitlab-ci.d/container-t= emplate.yml index 4eec72f383..01ca840413 100644 --- a/.gitlab-ci.d/container-template.yml +++ b/.gitlab-ci.d/container-template.yml @@ -10,12 +10,14 @@ - export COMMON_TAG=3D"$CI_REGISTRY/qemu-project/qemu/qemu/$NAME:lates= t" - docker login $CI_REGISTRY -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PA= SSWORD" - until docker info; do sleep 1; done + - export DOCKERFILE_NAME=3D${DOCKERFILE:-$NAME} script: - echo "TAG:$TAG" - echo "COMMON_TAG:$COMMON_TAG" - docker build --tag "$TAG" --cache-from "$TAG" --cache-from "$COMMON_= TAG" --build-arg BUILDKIT_INLINE_CACHE=3D1 - -f "tests/docker/dockerfiles/$NAME.docker" "." + $BUILD_ARGS + -f "tests/docker/dockerfiles/$DOCKERFILE_NAME.docker" "." - docker push "$TAG" after_script: - docker logout --=20 2.43.0 From nobody Sat Nov 15 05:21:41 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=1755627979; cv=none; d=zohomail.com; s=zohoarc; b=iaAbXotOI8Dz1Qe7gpm8Lkgk5v06qiXmOk7LcaKhJiPNI4Qm0csBkJTd/Q9e6xmkOybeI9Jq84RAFc+USm4ZuhlR5t8/JgQNQhEwzh7sv8outSohSY0BemllVedoMoRWvbE8KqwEnj4/bwUWnrV542xombWhCl3MX7g8ktc7yxo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755627979; 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=y2PNWscI+Gm3l+OVtOuKl3eCyc13I8ViniKQQPhf/dE=; b=PH2D9O6OCzBlvIhXjJ39s56UakLUGGCkrVb8derxzOgiq8F3W2W7E3UuMkzCSfGipG90xcdzuBjBHDKD5zlry83BstW9zfAdCQ+Y2Vu8M0nReOJLTIUxcSAfxSiWmZjImaRa7ZkDlCz2+y+5kck/laEVQh9Co+/YnUPID4jzFCQ= 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 1755627979231119.32579751039998; Tue, 19 Aug 2025 11:26:19 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoQzI-0004yZ-Fc; Tue, 19 Aug 2025 14:23: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 1uoQzG-0004xr-HP; Tue, 19 Aug 2025 14:23:10 -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 1uoQzB-0003nY-UP; Tue, 19 Aug 2025 14:23:10 -0400 Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-2445826fd9dso66414615ad.3; Tue, 19 Aug 2025 11:23:04 -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.22.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:23:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627784; x=1756232584; 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=y2PNWscI+Gm3l+OVtOuKl3eCyc13I8ViniKQQPhf/dE=; b=dkTTj53T4SoX2hDvYspF+8U4v1GUIIk33ohFtM8xW7N5h7TyOC9arbDDus740hK2sG quvzR07aQndLVZbHBzsrlH3GLCkCIan51/J3VzqLMMib6dlTn4hXoDbCVPwq6ov88Hg8 MYzUNcdkbKvwikKWnh7/0GlRrZ6VCd+QkTgfEWEUNIz8LZSMEkzurDsvVvEMsCBb1YFD NvOCtNi7MF0GJvHHzrCJnYFlUbS1QWwr6No2fUr0ukD9lSHPZdY6VOXGJosrKmQKSX8m JROD2XOhOL/u67/ylpAh0LWXCxfCHNyYYS0JIYSd15gm4xtVdTzGznmPwzuP1+8pAT56 53QA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627784; x=1756232584; 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=y2PNWscI+Gm3l+OVtOuKl3eCyc13I8ViniKQQPhf/dE=; b=m6iOe8skMC5PquaplqLBbfnHk9+ttOFmRdlH95UpmRc1awUPKGivq17IYSKSKrOj8z rYb+/e7bxUei5PSq1mt+Kw0oQtf5q/bNJ1UawDtYSyEnqKlhYmOFnzUNZE/Hy++14eYR qC7WhNNXhSryQfteeW3W2EspXopDVh7CkWtY33M1alxpGPLgO0G8cFGF4VBF+Qma6ved ioC5kOoELAbxbHK9+elfdEShgx7aSmzidrx14DOMGfrcWSorQyvKXm85q4VtWUe72FZc aGCPWcflCGIMa+zCc1LFQu88cNd+uBEPhjYWlowIOeoiBPxu+I0K7BRqUI9ELhSV1hkx knOw== X-Forwarded-Encrypted: i=1; AJvYcCV4+cgYb2KnaAVrqyawBwc5EQvitLiNEwc+uk8lWu0PSaz83vYclo9HIBM7Y0293BH2Ei7gvyN7gA==@nongnu.org, AJvYcCV6zWqR98GP4B6eftgfiqndbTfJnwPrSPB4/TrworNickHnJBwQbq/VSzGzwl7YG2ylJx4L2Co8GzHD1g==@nongnu.org X-Gm-Message-State: AOJu0Ywd81ogfVCQZtoUiS7XpVmHYrJY1eDhOe5xj7SZUhGthhPrVDuW PvjHJc81F5eU8MemaEqFh0M8PI10XKBd6XTAm2nCwxGkHHSdBSD9Oow0ifnZIqkB X-Gm-Gg: ASbGnctOL/gLW9AXgjUrL+tDJ4Yb1R4OTTj7ZccRMvFRUK8PEEjdhlZCeqzhGw01Wt5 k3x0XWfKtuLNOuX7JtHfVzIu6pIPdFWFxT9ngwmyDUM/cAmHYWCxxqaeSoxHyXP+dggJARz9c3B fk2gObosOyiSIOrGK+Lbb25PGT4nTR793uQbhjch8lheGLGCzekbUgFzqWM+wAp6f6VfNjbIgDY idwZws58Tf+Y/+hWMDziDyJHWIbCioifI0cRGfU6DS9GJRV4Az+0uPnAv3LRQgEt6lCYZxbuIbk ShzUhCFifno8Fxbrz7n1Dw4+9CGsxtkTPYABsSlyDSRYTRGBpb4m8Bw6brfqqX9TGs1dgGj5tHo jKxmR5JbkLVNSlCe+bmewDg== X-Google-Smtp-Source: AGHT+IETQlpGxL21Jn7/DVLT/wx15wcwcMxN1bVfpl1qxkYwyknp+TIOG75zMHKXybntmrASmVfWow== X-Received: by 2002:a17:903:1108:b0:240:150e:57ba with SMTP id d9443c01a7336-245e02d7372mr40749305ad.3.1755627783108; Tue, 19 Aug 2025 11:23: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 , 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 05/35] tcg: Fork TCI for wasm backend Date: Wed, 20 Aug 2025 03:21:34 +0900 Message-ID: <630c91852939acf3c7fdd95c37b363fa5d4e60fd.1755623521.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: 1755627980972124100 Content-Type: text/plain; charset="utf-8" The Wasm backend is implemented based on the forked TCI backend with utilizing the TCI interpreter to execute TBs. Signed-off-by: Kohei Tokunaga --- MAINTAINERS | 6 + tcg/wasm.c | 1076 ++++++++++++++++++++++++++ tcg/wasm/tcg-target-con-set.h | 21 + tcg/wasm/tcg-target-con-str.h | 11 + tcg/wasm/tcg-target-has.h | 22 + tcg/wasm/tcg-target-mo.h | 17 + tcg/wasm/tcg-target-opc.h.inc | 15 + tcg/wasm/tcg-target-reg-bits.h | 18 + tcg/wasm/tcg-target.c.inc | 1320 ++++++++++++++++++++++++++++++++ tcg/wasm/tcg-target.h | 77 ++ 10 files changed, 2583 insertions(+) create mode 100644 tcg/wasm.c create mode 100644 tcg/wasm/tcg-target-con-set.h create mode 100644 tcg/wasm/tcg-target-con-str.h create mode 100644 tcg/wasm/tcg-target-has.h create mode 100644 tcg/wasm/tcg-target-mo.h create mode 100644 tcg/wasm/tcg-target-opc.h.inc create mode 100644 tcg/wasm/tcg-target-reg-bits.h create mode 100644 tcg/wasm/tcg-target.c.inc create mode 100644 tcg/wasm/tcg-target.h V1: - Although checkpatch.pl reports the following error in tcg/wasm.c, tcg/wasm/tcg-target.c.inc and tcg/wasm/tcg-target.h, these files were copied from TCI codes so they are preserved as-is. > New file 'tcg/wasm.c' must not have license boilerplate header > text, only the SPDX-License-Identifier, unless this file was copied from > existing code already having such text. diff --git a/MAINTAINERS b/MAINTAINERS index 433a44118d..217bf2066c 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -3999,6 +3999,12 @@ F: tcg/tci/ F: tcg/tci.c F: disas/tci.c =20 +WebAssembly TCG target +M: Kohei Tokunaga +S: Maintained +F: tcg/wasm/ +F: tcg/wasm.c + Block drivers ------------- VMDK diff --git a/tcg/wasm.c b/tcg/wasm.c new file mode 100644 index 0000000000..6de9b26b76 --- /dev/null +++ b/tcg/wasm.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/wasm/tcg-target-con-set.h b/tcg/wasm/tcg-target-con-set.h new file mode 100644 index 0000000000..ae2dc3b844 --- /dev/null +++ b/tcg/wasm/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/wasm/tcg-target-con-str.h b/tcg/wasm/tcg-target-con-str.h new file mode 100644 index 0000000000..87c0f19e9c --- /dev/null +++ b/tcg/wasm/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/wasm/tcg-target-has.h b/tcg/wasm/tcg-target-has.h new file mode 100644 index 0000000000..ab07ce1fcb --- /dev/null +++ b/tcg/wasm/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/wasm/tcg-target-mo.h b/tcg/wasm/tcg-target-mo.h new file mode 100644 index 0000000000..779872e39a --- /dev/null +++ b/tcg/wasm/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/wasm/tcg-target-opc.h.inc b/tcg/wasm/tcg-target-opc.h.inc new file mode 100644 index 0000000000..4eb32ed736 --- /dev/null +++ b/tcg/wasm/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/wasm/tcg-target-reg-bits.h b/tcg/wasm/tcg-target-reg-bits.h new file mode 100644 index 0000000000..dcb1a203f8 --- /dev/null +++ b/tcg/wasm/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/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc new file mode 100644 index 0000000000..33b81f1fe2 --- /dev/null +++ b/tcg/wasm/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/wasm/tcg-target.h b/tcg/wasm/tcg-target.h new file mode 100644 index 0000000000..00befa2fcc --- /dev/null +++ b/tcg/wasm/tcg-target.h @@ -0,0 +1,77 @@ +/* SPDX-License-Identifier: MIT */ +/* + * 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 15 05:21:41 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=1755627907; cv=none; d=zohomail.com; s=zohoarc; b=VuPAiC5LbFwoOE3IKSWiCd8Pemp58qoc5H+GUvsansI+AlWFEV9OKAGrEqZoz5Ovxo5wjpZmHICT+z9o1IE4bVr7Me1VuCKLDMkPgsLWFhlhOjdODqxquu81Xlc7WsmMIFkwuiSdTY+fBjZgwC8f3tVEHc6iEMq3DyPPvcUx7tg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755627907; 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=9eMUDTlPd/3eddRITWEfvNp607+F5yB1eBk0tdgpi6Q=; b=OFphDvIXHxauk77ezOGgKoH/LwPZQQXsknEv1V09koEdzeplmGcL6I2I/4NMsB6+5LVKICJiXjZj2wzE62neYgGLnOCwD8J1L5j3r1A/rkVdS8C3AdTAM6QXvKpQ6JJX39Ntdc2hsI6cBbxifS5TJ9g5U4HLyjRtkG6+qte6lms= 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 1755627907312131.25986859946352; Tue, 19 Aug 2025 11:25:07 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoQzK-0004zm-OU; Tue, 19 Aug 2025 14:23:14 -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 1uoQzJ-0004zA-4C; Tue, 19 Aug 2025 14:23:13 -0400 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uoQzG-0003o0-Rh; Tue, 19 Aug 2025 14:23:12 -0400 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-2430c5b1b32so1199235ad.1; Tue, 19 Aug 2025 11:23:09 -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.23.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:23:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627789; x=1756232589; 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=9eMUDTlPd/3eddRITWEfvNp607+F5yB1eBk0tdgpi6Q=; b=UwhcOaYShUYLPL0AfqgyQNZt286/HVUDNybcbmv7SLO9YtlXaHgK/GUmFaqr/KA7dF gk5g/l25ah7XYyBiXJ1QdSWxlUY/TFdZ7r2LdrLOoZSPbn9bo7KmMnx8aLalNe/+wPk3 yBvWbTK4VD6CxfuEBIjRqY/j7E1RoA8OFtQk5wwDj8B9hJTnn1ris0cgiyhOFqbYdHaI AJ4U2eVli1zqXoqbfGyFlfv3tLUyrLwJflRhO0hePDYQt50WHVxxjC2qg9q8mtRr1RD3 W/vFSxLANTK+PMAW7vVOWZQtyu7Wjc3UyUzgWXyLo9hwWhR+ooe8y9F5OQqmfVQpT3Uk QNnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627789; x=1756232589; 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=9eMUDTlPd/3eddRITWEfvNp607+F5yB1eBk0tdgpi6Q=; b=BwECImHb9EzNaCLOUxsUaqOsimnYmCRgDwmiuNULiS/aAcNKnwIIJILkdk8CLBxami Old/yIaUJ301ZXbi5ndEZozOEaj3/Vy61sfEDLevwVnjzFxTdrcm39cVEtqbwd+J2yWk mU7XU0lmAv/OUAQR48jhb/6ClNDDOYxiTP8GuRPYHCLlIHivjyveTgr+JPC/j/iD7gSn PTcvbBNAm0clwWhc1gZrYMJbJTGmdPCd7aY9syLrc+zQul+9X1LyfsHIZsWpxZcQLFOk jeHZYE/BdVOaeBH4syJv3KPIYIwsDey8GApLqUZ2GCtInaefEszbYGM75BaZFI45V1d8 iuoQ== X-Forwarded-Encrypted: i=1; AJvYcCW75Nw3PqOouaZL3WQ6hf2f25FlRixkHrL6s+IEfveAvhk5JbTVgSnEwJntoIW4QusI/5GZgW8Olg==@nongnu.org, AJvYcCX2CwMIfjG2tEU7eyPJGmww2mDcK5YD7LKmMqBYAEOSIXEVVKqQJ6lEylTogSnEtyXd6RGA75lwUNRArA==@nongnu.org X-Gm-Message-State: AOJu0Yya//F6Yg/VcteQqLeuSBlbtK9XHF7vY6djX2RYAsEbRtM55whn AMtoKIjGu3Dk+Et9x3KpG9hfLsI1Z8GH4JmNEj52tdUTGZBEKPkQfmXPY01fE9JW X-Gm-Gg: ASbGncvXYOGFKO9VrNcg01KBAnHfZah7BxLsEylXXnvqAegDM16JvAymgLKM0/kqqIs 0wf9Q4oPXHZkzZRmesCj9XTlpn//N0JBBqnWx1L4dLrlY92a0tnvdXJlD2nLdIg+iSzgi1rdKwH 0aMLDfU2bnJ+RqW0xWstzyqsGgUgIkiFL7T9De9RXVEAyuL7Xko6De3hI2U2llTcJ5n1YHPQkm0 oI+f0xv52DNg399k9OLEn9v+/gTidEgngIF4ATMqHkGbI4lIdNYmzSX+Wy3+Pjy2E5PsjXNK7c4 LaTRZWhiV4lzTe94iexQsGW6wzkmMXgmsIhJx0RxV8oajogwlCfbTLzjeBKfOZKRDYHw34uYgCx hgXgqva4nO0IfG2Gp6PDIjVOu6KHry9t9 X-Google-Smtp-Source: AGHT+IG8E7Oog9qP8xcCbUMzEgno04/0g+VD5DAsJlH9ff4zpLY/DUFetYwmN3rgpV9AMqiMyB7AKg== X-Received: by 2002:a17:903:1a6f:b0:233:d3e7:6fd6 with SMTP id d9443c01a7336-245e0a01363mr46485895ad.19.1755627788652; Tue, 19 Aug 2025 11:23: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 , 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 06/35] tcg/wasm: Do not use TCI disassembler in Wasm backend Date: Wed, 20 Aug 2025 03:21:35 +0900 Message-ID: <331e4d5f47af18d9f26cfad41bf610f5044f37d1.1755623521.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::636; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pl1-x636.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: 1755627908075116600 Content-Type: text/plain; charset="utf-8" The Wasm backend should implement its own disassember for Wasm instructions. Signed-off-by: Kohei Tokunaga --- tcg/wasm.c | 243 +---------------------------------------------------- 1 file changed, 1 insertion(+), 242 deletions(-) diff --git a/tcg/wasm.c b/tcg/wasm.c index 6de9b26b76..4bc53d76d0 100644 --- a/tcg/wasm.c +++ b/tcg/wasm.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 15 05:21:41 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=1755627861; cv=none; d=zohomail.com; s=zohoarc; b=W6PUT9MvDwMofnUZntmP0OMECFP5GoAWxsOA4haQz63P/P/w3gofuQRjVVdxha0Xkx0s98ZceSJ3tj3DaJF6V1B88fZDKpKQn2yxI/WFGcr/oMD3A0tM8FUUlCJ0v/Z9+fyOsS3e6bj2+F/hyrrl8kNoM4QBJJ6YnoyuhE/wTBE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755627861; 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=DJZUW5SkPZ1D/2azqNWBfnN/qyeeJFmopfU4EryG5yc=; b=O+37rQq0v7rGlVmq38dQBbSbWJgOtLa+XP9hI6ACFu+p3EimeEevVb8Ycj78BPW+z95Gs9OnLNZcr99GNPuu5n+dxPLfblwdO2boNlO5W3gM7dQcQ3Q+HDDARJOS9h9rHwcm3wFp/mt48+SbDiYLbnRhgvCZ3YrpZgIzbv6t86g= 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 1755627861319281.49540313108287; Tue, 19 Aug 2025 11:24:21 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoQzQ-00051B-KD; Tue, 19 Aug 2025 14:23:20 -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 1uoQzP-00050d-0g; Tue, 19 Aug 2025 14:23:19 -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 1uoQzM-0003og-Ol; Tue, 19 Aug 2025 14:23:18 -0400 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-2430c3684d1so1527375ad.0; Tue, 19 Aug 2025 11:23:15 -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.23.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:23:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627795; x=1756232595; 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=DJZUW5SkPZ1D/2azqNWBfnN/qyeeJFmopfU4EryG5yc=; b=WN8/aMAStpeP8aP/KIWb+2MYgvUYQZxmY67zrWfHnDJoQD5bSydS6k8xl8ZieRyBZV r2c7wEDvpiLlSby3es1hVicv4QsAEPnNuAA2PQQltcEXekJuT4ZzwlSMNMSvAgbQFpuI K41oFbQI4cjcI6VJTmcp8SlKpXfpUcrzbHTXA1Ip8ENZdjp6je96Zn4TChPhwpjhRtZJ 6A38pVrm5itX1n9Cg+2xO/PZpvF8QULfH3VCvONTGvX9FwP8ke6VA9e9DfGjAWKwlRJ5 ZtZkL+FvZt2awULUlt1suCNXg3ElD3O68ThdF60md98fY326Pn+mSvamC0ITkZyQQhqH h61Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627795; x=1756232595; 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=DJZUW5SkPZ1D/2azqNWBfnN/qyeeJFmopfU4EryG5yc=; b=MdeZWINn7cqpXol8qRgLF2FCPEHo+DN0One+o7kleCz5SbbvHe7mYcGwERpSwti5nM j5MWc/efoTiML+yfnLYC/gWIhQl1Ila9BuFcRnjlSgNZdqamsl9fpwqsIlfZn2KAm1Pc GFPGgYn9szBtjbVsJJKY6LxeCc/OlLIYx4nTEB86+UDipo3BcbjiTf6vHJdJ9kWB6x9t fcgCCjFQGqkxE8rpfJvONDU5JI/OxxajxEjhftHQMV4FDhQEXwttVbzS0l8hDD/VZjhu 7coLtzS0J/Z9DLieejKLA0fOXb/bmklocShw66rx1qQJbWTXpidoCZLV0E2hdxkzEOff JqcQ== X-Forwarded-Encrypted: i=1; AJvYcCU2TCBWAVVNDkY3iJjHez06k4bTJQo90MIuzJ2DZgP5eoewjGxjSabQBgiWoq5H7MC9fHohaZWsJRDrGA==@nongnu.org, AJvYcCUwNZmIAoAYG1hFVdYRmm8UsroA9JISkrzaIgLSTmOXSvwnq78M6T9137JHbmEzeVUMBpLig1Nnrg==@nongnu.org X-Gm-Message-State: AOJu0YzglsJaT18QflQFu4yvS7a/zJca2XYx/ekCBo4XQWop5jcQE+Cp 1xQnEskOAWvFdkcxO9hBOA8rd56/NSiW++YUB07/xUungkv9U9s9MoC2/sQsjzjs X-Gm-Gg: ASbGncvZwUyDo7QGKvhne5l6EPpqAJkztxgvxlPC5O3wi3mIuEK+N6jl+/V+ZCWZ1Vs /lNFYFs/DdtF4uKQK6hdhUxYf9N/fh6AuKq3i6Mir5GGcaphLoNMqjEHc6L9SUlYr2gBA74rwDS wBYojoMhMkDEQvXU6TmQLXSN3rR6RFnqfNKWAAzGrn4EchDMa3yaIURvN7oekd6hjsYlJlT2YG2 ErM3RpiBxAwk520SoRIBsXkMvRIxzv2Kp4kJrs8Q4efZZAxWqyOnyI9NbuveQKyv/dy4TJ0bTqx aniNLkyCXRICGVm9yjauSqiUygiDaeH+ZhmtY+I7PXSdB+322TLVsLve/Z49kdhbFRMlWn4k443 wB3bmge6FY57Tnbf2o4D4RQ== X-Google-Smtp-Source: AGHT+IE1DXsAlifv+0HeHVzjdFhK/fe4e28HAlK/5IlGDcES7xxNfGPbu3ymi3SH6UDYdH1/ttrtRg== X-Received: by 2002:a17:902:da92:b0:243:afef:cd3a with SMTP id d9443c01a7336-245e09d0bfcmr49799055ad.15.1755627794556; Tue, 19 Aug 2025 11:23: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 , 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 07/35] tcg/wasm: Set TCG_TARGET_REG_BITS to 64 Date: Wed, 20 Aug 2025 03:21:36 +0900 Message-ID: <13f715223405247ebedb340123cf8019d0913dd9.1755623521.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::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: 1755627863757116600 Content-Type: text/plain; charset="utf-8" The Wasm backend targets wasm64 as the host. Signed-off-by: Kohei Tokunaga --- tcg/wasm/tcg-target-reg-bits.h | 9 ++--- tcg/wasm/tcg-target.c.inc | 69 +++------------------------------- 2 files changed, 8 insertions(+), 70 deletions(-) diff --git a/tcg/wasm/tcg-target-reg-bits.h b/tcg/wasm/tcg-target-reg-bits.h index dcb1a203f8..9b0b7c6c3b 100644 --- a/tcg/wasm/tcg-target-reg-bits.h +++ b/tcg/wasm/tcg-target-reg-bits.h @@ -7,12 +7,9 @@ #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 +#if UINTPTR_MAX !=3D UINT64_MAX +# error Unsupported pointer size for TCG target #endif +# define TCG_TARGET_REG_BITS 64 =20 #endif diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index 33b81f1fe2..efec95e74f 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -28,15 +28,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 static TCGConstraintSetIndex @@ -1050,39 +1044,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); @@ -1199,18 +1160,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, @@ -1224,18 +1175,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 15 05:21:41 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=1755628107; cv=none; d=zohomail.com; s=zohoarc; b=V5MX+ZIMCMVsww+MGaBVlL/tsfTnj3RGriDRIS+eoA2pc0dFGeTko3jkXkgrRRXRkPtZLsbmXMOV7bt7JnkVPC6tpXVQNWhYF02d4fAXB7E3J4lTTGnl3HjU3OC0yiBI5f4Us4vWQhZlbW3g20Np0HB7j7Acc7pjL9En2XDMuQ8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755628107; 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=hEe0HN7Xf3vBBHUAfg1FxQ2bKTW63w/E0lVNLcPFW3I=; b=fpjHUwGZvsTJy8HFT7OIBO2GcZWe1wu1D2p4Hs8lLqjYxPNsXgdb4LzDH5Scc4Yd+RdaPqDRmbqs0BuSOwzjyquymJfy5u1J8GKowK//NwW+3/RbkOpz2esUQ5F/oUrJZDAi5HxzRolljTpiO/9AgfbkYZ1dd5JzhnJbLZZxy3Q= 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 1755628107469715.9302065640298; Tue, 19 Aug 2025 11:28:27 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoQzZ-00056Q-4e; Tue, 19 Aug 2025 14:23:29 -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 1uoQzV-000536-5i; Tue, 19 Aug 2025 14:23:26 -0400 Received: from mail-pg1-x529.google.com ([2607:f8b0:4864:20::529]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uoQzS-0003p2-Tu; Tue, 19 Aug 2025 14:23:24 -0400 Received: by mail-pg1-x529.google.com with SMTP id 41be03b00d2f7-b47175d02dcso4698660a12.3; Tue, 19 Aug 2025 11:23:21 -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.23.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:23:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627800; x=1756232600; 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=hEe0HN7Xf3vBBHUAfg1FxQ2bKTW63w/E0lVNLcPFW3I=; b=aQ7yijkDQhxfsHVYXh58q9h5GF0nOEY6M42zA/xaSdHJT5/jn0QceImK22bHeLmggD gi0yUCD4yfUCRtzMF+oc1zDpe85MQWIWfC2ogD4+HPCqXBKeI3ospfjZqCdAqdY7+Xir IqkKNQRWRvIcT+SKBQ047hvmPo7BIFMwufu8o2UbwQin67xcBv1yTWDcV9NB3/7d9/lF 6gYyn3T4iJs/xGDMQda57vpBQc6pwcWTyTU9iJuOpJNj0VCSO9LINQlSna5uiKHATVkM BPD7QokpjYFbHddIUYqAmXzcKQ+zgxw+pWBN2VLMETTqrbtOTnfru4zXaQMBAxrKDtWC PEkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627800; x=1756232600; 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=hEe0HN7Xf3vBBHUAfg1FxQ2bKTW63w/E0lVNLcPFW3I=; b=n2mLwlASlcFvhAsyrMVuJSiVA3cK6JEpVCtcI6d3fpKtp2flir5tDCtFZoWZIEhd0Q 82CU5g7lXswQ0TM5jhzShhlhYT50hZXyk02tm/Z9Onuf007q2zI7kNf1PrfBHcylwBab YXntHMCTKpSJUteHbQygY6dLlobqbaXR36sL5uh25e5Nu8Wj0zh2bi8az+ekOeoUi1fU HfuBedin0zteFHBuKAUDRMxQhd9/tZP7VHGEM2xQulZ/ehemqXQPTa/ZkmV/IPzKTN6a vggbFtfqy2J7sdUKuJG17wjIN+5YWSt/Wd9GgMtThQ9MIq5Cykq6lvqad/ED6AZx0DXI WeIw== X-Forwarded-Encrypted: i=1; AJvYcCVUT8FleC6PbltIgr36M3loyz7FJYMCq1jIAZxK/JQeAWBazhG/Yn8t0G0WvVtuh71n+FQkoJHHnQ==@nongnu.org, AJvYcCWO7Fqv4Am3umO5CL6Z9zSfTJoIPHFwsgwMa9HKZtoGLQa0rLstmABHgBud9+12d9LwfusjDzFYm4skRA==@nongnu.org X-Gm-Message-State: AOJu0YwWP3avh7dH6jxIEkdDbiKthZI+LyIy/2uXYy5RS5yRxdLzAplT 0o9Ao3qW38PDdmzyeXzs3N+jPUxIL2B8njbqmgq9kxTFKRWZ0sSY0xwiIkgQn+Yu X-Gm-Gg: ASbGncu+aDWJugYwYX2C1xEtWEsK5bcO0n139LlHKG3D7M7dLpXQx4AulbLSKsk9OJJ +aHb8OTpYS7A1wwBf/hEuyMo9yHWK0KxAfF4Xz+k/wV9t4xFmXKAVZu1o+Eifv13iO0OxcoYyBv +aSh0gGMS93axM1TFtIlui6kHQG5yu0onjeM2Cn2+0h8XsemGDJ4qNkGDTt1kYYi0Je8iOFVWUh 3DrkUdBP5aztOqjKSw6Qsby3q9FfUXJBuXd/OlD8n0BRiICxOEGxdM9V3dcPk/nObL64bXK+cur CkWPiCMs03dsWhhZljat6M/Pth7nxWNuuTfL5+j51+Vmo44Zz5XXE4TGPK5jHoKjkM6ZTO5YfWo 5QzWXg2JgDjBWbfW3WTYbgbie6RA+rj4C X-Google-Smtp-Source: AGHT+IHtNnKipR3Fypo/eDFtdgSl7BprlRg5emqbe3ysrJHUAV2vD8MkoDbuZM3v7oL0TVitf5GA4A== X-Received: by 2002:a17:903:4b2b:b0:244:5311:8ed4 with SMTP id d9443c01a7336-245e0504d55mr41833065ad.55.1755627799997; Tue, 19 Aug 2025 11:23:19 -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 08/35] meson: Enable to build wasm backend Date: Wed, 20 Aug 2025 03:21:37 +0900 Message-ID: <3bfbeb81abed474af3b984911b3185e072339efb.1755623521.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::529; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pg1-x529.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: 1755628108486116600 Content-Type: text/plain; charset="utf-8" Enable to use tcg/wasm as the TCG backend for the WebAssembly (wasm64) build. Signed-off-by: Kohei Tokunaga --- include/accel/tcg/getpc.h | 2 +- include/tcg/helper-info.h | 4 ++-- include/tcg/tcg.h | 2 +- meson.build | 4 +++- tcg/meson.build | 5 +++++ tcg/region.c | 10 +++++----- tcg/tcg.c | 16 ++++++++-------- 7 files changed, 25 insertions(+), 18 deletions(-) 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 a6d9aa50d4..b91818d982 100644 --- a/include/tcg/tcg.h +++ b/include/tcg/tcg.h @@ -963,7 +963,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/meson.build b/meson.build index 291fe3f0d0..263a72df61 100644 --- a/meson.build +++ b/meson.build @@ -916,7 +916,7 @@ if have_tcg if not get_option('tcg_interpreter') error('Unsupported CPU @0@, try --enable-tcg-interpreter'.format(cpu= )) endif - elif host_arch =3D=3D 'wasm32' or host_arch =3D=3D 'wasm64' + elif host_arch =3D=3D 'wasm32' if not get_option('tcg_interpreter') error('WebAssembly host requires --enable-tcg-interpreter') endif @@ -934,6 +934,8 @@ if have_tcg tcg_arch =3D 'i386' elif host_arch =3D=3D 'ppc64' tcg_arch =3D 'ppc' + elif host_arch =3D=3D 'wasm64' + tcg_arch =3D 'wasm' endif add_project_arguments('-iquote', meson.current_source_dir() / 'tcg' / tc= g_arch, language: all_languages) diff --git a/tcg/meson.build b/tcg/meson.build index 706a6eb260..1563f4fd30 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('wasm.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 afac55a203..e6f8f9db5c 100644 --- a/tcg/tcg.c +++ b/tcg/tcg.c @@ -254,7 +254,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 @@ -1118,7 +1118,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)) @@ -1440,7 +1440,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) { /* @@ -1517,7 +1517,7 @@ static ffi_cif *init_ffi_layout(TCGHelperInfo *info) #else #define HELPER_INFO_INIT(I) (&(I)->init) #define HELPER_INFO_INIT_VAL(I) 1 -#endif /* CONFIG_TCG_INTERPRETER */ +#endif /* CONFIG_TCG_INTERPRETER || EMSCRIPTEN */ =20 static inline bool arg_slot_reg_p(unsigned arg_slot) { @@ -1894,7 +1894,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 @@ -1913,7 +1913,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 @@ -1950,7 +1950,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, @@ -7048,7 +7048,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, --=20 2.43.0 From nobody Sat Nov 15 05:21:41 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=1755627848; cv=none; d=zohomail.com; s=zohoarc; b=N4US8BKRarp7YXKJjlV5NacKuBPz8ui1l19vuQLbPV8VIgtpYxh9Vq82+yUZ00JSHInrjG8xJS2JJxJWBcYFalXBl03e2voJDy8q4WkQlmX5UVp9GkOlOLBv3mlANpUx3X3woqbksOGdWIuOUtm/zTTCUoq485vLmYVwL+VEGxI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755627848; 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=2oFY77tRx6dazr6YCQwTL9wWp6iPtE6SNv60RdyQaWg=; b=mcoNkvAiJvae0BswzgmZI9J3dNarNMkuYS6AgIiRpdNyclxCtLQvQ7x0PhKfyU4ETHfX7uh0gez+xodaaxYIjPpPdG0wbkygTkKoYTX2I1d/LoBgDnGLOKQ3ZtKvKbpoat5sVfkIS2yvWbHkgG/F5/0UHreOMOUCBXCJuC7QgFA= 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 1755627848359332.7274294842699; Tue, 19 Aug 2025 11:24:08 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoQzg-00058h-53; Tue, 19 Aug 2025 14:23: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 1uoQza-00057H-OH; Tue, 19 Aug 2025 14:23:30 -0400 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uoQzX-0003pk-W3; Tue, 19 Aug 2025 14:23:29 -0400 Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-2445827be70so62677015ad.3; Tue, 19 Aug 2025 11:23:27 -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.23.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:23:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627806; x=1756232606; 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=2oFY77tRx6dazr6YCQwTL9wWp6iPtE6SNv60RdyQaWg=; b=mVLcYAjTmHkdNDPg5yv/XEw2mMK10I3i8cJmS58uwzJVW1eJSAf3ea0qwTugXrdSqd 92BwGzYmiEYodPn/9okflZvt3N2pHKQAE4rmcVreKdH+LU3eArpDJtN5vF6y5l9V7rYh Ia3KdC3JUQoig5A323fcspjYzA28LrwVd2R81i+ZuMug7a1IiwFgQxq+KJ8zVKOQ3xyT +yavxlYbNR2sTcOnQUkixvolQlV5Wg1KFA81C30q8FWnxm2EF2Ki31HT/p1J7GEqoajv SV6ef+uo0eAqfVtXqXDxGrAf+w0OpP4ay1KYZvSCjoirGhg+C9fEyVJoLg9QETs4SFt5 JKbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627806; x=1756232606; 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=2oFY77tRx6dazr6YCQwTL9wWp6iPtE6SNv60RdyQaWg=; b=ZpDR9S6xqCiY0SGi+NQqZG1WteRB6KB2y4iZKCu3BP23zWKBSHZ748b6qtTbab7i4B xlwC9HMD36/PDt+OnY2FUCFblrZMToecxZ9IOHTAG2PT9Ebqn08hTzprPQ+LcVDCd+vE dOFSPYf3Tm74WBqidwpPPUaSzwztdOLz667nb+C34UeAFx/Jr9G7v2KfCVIoctwqP2RU XXD5O5D/FtkIXuOWIGf2jHizXKuoh8c8yRJytVTi1+6YwDTd9kOJeiJAWLvXsu751PqX RC3blyLRLvS183YN3rQK5Ski8j98tQpacEVryey5fHfmQSr+1DiNSpOT8qekXL4UUbFx k/FA== X-Forwarded-Encrypted: i=1; AJvYcCVanMzxvPjbGIm9PjodaSdNeQ2Ui1ebntsQhZ430Zw8nMqdzwwL/XIwzbQrhMkHo04ynrnkXINZdjuPaw==@nongnu.org, AJvYcCVmy7aZmq9e8WzxdrBuaXIDYFs9TjHFU/2BmD94rPWFEPJCq8zGFNuwpreTL75KUBfRiAeZyfzkzg==@nongnu.org X-Gm-Message-State: AOJu0YzDA/2/P9tzt051/LnqyMK4ZitXjFVYmEMKLjh5kWE1qFKOYazZ 7FwpAHc8STuBw4qapl4jjFYva34KuD95bsDRdO1M18uM1VI3XQWioY8f2uFcMJTB X-Gm-Gg: ASbGncsoud1uazpsgnvO9uMIzxSCLl/SAkB60mlt617I5ufNfoTkFFzaYEmM2Ytksan enwttVTNPZH9qzLG9rJcGP+lkZX1ax8199BEpZk+4oqqZ2Tb8hvwa/Vzii6NspDxJGq86p5BTcC QQqdCiTafnmpjlNf/q/zFUYXVQFH7aOOjQzOtwrClrng9J1lwobsGyE0nVS09ytYEJTiRNOuKMn IysOXd1UPCj31B1VjgUThQC6j/hldgEDfE4RnsT+m5VyUrdOa3Nr90M+Afn65n4lhZ4zDyS3902 iRagUl7ClMZWS4zNhK6IXjRJYRE7WH6tnVpC0PSDCRnP87TwnbHe4Kugl1Vk3PHVikpSEcBMTVo a6XfzJpC22mY5Hy/Q25uAJyZfKVsRFrKnWfSNkPeNg1w= X-Google-Smtp-Source: AGHT+IGKLgmqjAJmEQUg6Jnid/CSW2HNdVpbvUo+ag9zk7QdNfQNMtPvlVnA7R+RaqiT5cbw9TmyPQ== X-Received: by 2002:a17:903:196b:b0:240:640a:b2e4 with SMTP id d9443c01a7336-245e04df64cmr44629585ad.49.1755627805827; Tue, 19 Aug 2025 11:23: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 , 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 09/35] tcg/wasm: Set TCG_TARGET_INSN_UNIT_SIZE to 1 Date: Wed, 20 Aug 2025 03:21:38 +0900 Message-ID: <37c473f501f89041fa180e652bcd8b12dbe8b4db.1755623521.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::62b; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pl1-x62b.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: 1755627851661124100 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/wasm/tcg-target.c.inc | 38 +++++++++++++++++++++----------------- tcg/wasm/tcg-target.h | 2 +- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index efec95e74f..f1c329eabd 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -33,6 +33,8 @@ #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; + static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op, TCGType type, unsigned flags) { @@ -90,16 +92,18 @@ static const char *const tcg_target_reg_names[TCG_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; @@ -116,7 +120,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); @@ -125,14 +129,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); @@ -145,7 +149,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); @@ -159,7 +163,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); @@ -170,7 +174,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); @@ -180,7 +184,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); @@ -191,7 +195,7 @@ static void tcg_out_op_rr(TCGContext *s, TCGOpcode op, = TCGReg r0, TCGReg r1) static void tcg_out_op_rrm(TCGContext *s, TCGOpcode op, TCGReg r0, TCGReg r1, TCGArg m2) { - tcg_insn_unit insn =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); @@ -204,7 +208,7 @@ static void tcg_out_op_rrm(TCGContext *s, TCGOpcode op, static void tcg_out_op_rrr(TCGContext *s, TCGOpcode op, TCGReg r0, TCGReg r1, TCGReg r2) { - tcg_insn_unit insn =3D 0; + tcg_insn_unit_tci insn =3D 0; =20 insn =3D deposit32(insn, 0, 8, op); insn =3D deposit32(insn, 8, 4, r0); @@ -216,7 +220,7 @@ static void tcg_out_op_rrr(TCGContext *s, TCGOpcode op, static void tcg_out_op_rrs(TCGContext *s, TCGOpcode op, TCGReg r0, TCGReg r1, intptr_t i2) { - tcg_insn_unit insn =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); @@ -229,7 +233,7 @@ static void tcg_out_op_rrs(TCGContext *s, TCGOpcode op, static void tcg_out_op_rrbb(TCGContext *s, TCGOpcode op, TCGReg r0, TCGReg r1, uint8_t b2, uint8_t b3) { - tcg_insn_unit insn =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)); @@ -244,7 +248,7 @@ static void tcg_out_op_rrbb(TCGContext *s, TCGOpcode op= , TCGReg r0, static void tcg_out_op_rrrc(TCGContext *s, TCGOpcode op, TCGReg r0, TCGReg r1, TCGReg r2, TCGCond c3) { - tcg_insn_unit insn =3D 0; + tcg_insn_unit_tci insn =3D 0; =20 insn =3D deposit32(insn, 0, 8, op); insn =3D deposit32(insn, 8, 4, r0); @@ -257,7 +261,7 @@ static void tcg_out_op_rrrc(TCGContext *s, TCGOpcode op, static void tcg_out_op_rrrbb(TCGContext *s, TCGOpcode op, TCGReg r0, TCGReg r1, TCGReg r2, uint8_t b3, uint8_t b4) { - tcg_insn_unit insn =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)); @@ -287,7 +291,7 @@ static void tcg_out_op_rrrrrc(TCGContext *s, TCGOpcode = op, TCGReg r0, TCGReg r1, TCGReg r2, TCGReg r3, TCGReg r4, TCGCond c5) { - tcg_insn_unit insn =3D 0; + tcg_insn_unit_tci insn =3D 0; =20 insn =3D deposit32(insn, 0, 8, op); insn =3D deposit32(insn, 8, 4, r0); @@ -446,7 +450,7 @@ static void tcg_out_call(TCGContext *s, const tcg_insn_= unit *func, const TCGHelperInfo *info) { ffi_cif *cif =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/wasm/tcg-target.h b/tcg/wasm/tcg-target.h index 00befa2fcc..b3d540198b 100644 --- a/tcg/wasm/tcg-target.h +++ b/tcg/wasm/tcg-target.h @@ -42,7 +42,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 15 05:21:41 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=1755628392; cv=none; d=zohomail.com; s=zohoarc; b=RfcSPaMj6tVw6FXpsM/NwAeBiFAcr2tR50mfBiZmXPakihZP6bsuOX6xpT8JO2EpDogmnjEBXP3qSUsP9dEbseK0kyAz33YbmT65wOEwafsBI5w+MDNgOtA+gEmzxk+kLf7X2JrleIv4Kp/6orxeApvcADymxadV2Cf8GhyWkIM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755628391; 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=WhEVncf4NDGTpSVsEeHM/Vwx8xoF4Yy1hqyc+pyiVsI=; b=Gzh/0Af53pg0jCKeSapeXmYn8oAqJCnVHEa1bm/UpfeEXctTe/x2S28CYz3N7wlqraMHF2EtegKEoEE0fdKoB134dEi6DjhLi045vALoU7PUu5kenpvs5lapW1EFtpjudZtqF9tTflT0bpOrCPxr4c73OdHteHiUPSKgNsMPybo= 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 1755628391992569.0812558821299; Tue, 19 Aug 2025 11:33:11 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoQzr-0005RP-Kf; Tue, 19 Aug 2025 14:23:49 -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 1uoQzf-00058x-OJ; Tue, 19 Aug 2025 14:23:36 -0400 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uoQzd-0003qA-Kh; Tue, 19 Aug 2025 14:23:35 -0400 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-2445826fd9dso66421275ad.3; Tue, 19 Aug 2025 11:23:32 -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.23.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:23:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627811; x=1756232611; 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=WhEVncf4NDGTpSVsEeHM/Vwx8xoF4Yy1hqyc+pyiVsI=; b=HywhTC2urmDNhS3yk8wY6Q6LIGHMPP4OsZ3WHvVWCTVugH9pk/AceFjRozv3dq1TaG ATjchglw6FcC+V/fkGu5ia51aFY3+2T/QxJ7o2JUb/+BmmUdKKYyuK+Jz19kdpFkjLU8 M7AqsQMZ+yHikKa6qZ3S6aKQLcuK8pLjBG2HCXqzvtLLJeuy73JH0iFkxL3zXzr/hQ9B +kirEIytCyh2zoRisdHK25DLPnYqalsoFvPnTz4Fhlt3+/U0A53jwdk52UhHuLiyyGxy gx5hnsHf5yC+WaJr+Wjn0Kpxf2L7Kf1jvNEZSJHORbuI2bFqNl7S6OINYEnnPyS+d6sb eAVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627811; x=1756232611; 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=WhEVncf4NDGTpSVsEeHM/Vwx8xoF4Yy1hqyc+pyiVsI=; b=J4r+6oMEK7iAgExTN7wcQw5QZbo8+z8Alz/sPAWapfV1rUu5AemLWQWnD2WWe1/5oJ Cl/GhGIHL78Rw3DubFD7LT739djNrD+jDQouoOvq2aSF006NHMKzXHCtldSsKOsDiNJR ey35siqsyHHwQzb/KEZhJjqGplDbIAaig95GgAguN7clc7nQWfcw6K9jqVECtTyZmmnx n8hHHnymToRsmV4PuE0JHy78KdqnDJf78MObOiXcR90coYHZeBfGcR6llv/6vL72pXOa VRPUsWyQ8xYKUvSc/5qMUq0JMas71QE3JNzkKdVFxC+TvQMmuAk+pB5zsrpRvjC1OkiP 4q3A== X-Forwarded-Encrypted: i=1; AJvYcCUcUpBfet3ragMTp3g0k/eBTlSisjvEouT5Uw0o7WkF+NcQxSODitIfMThOpOQ+d82q6OJp0W1eOQ==@nongnu.org, AJvYcCXyizsUCOjqd2fvlR/ULarlv0lrByVZ63L5JsItcR222g6ULJdLvmLt4lPE+ciQO6VvNy+y4KIrdoTnvA==@nongnu.org X-Gm-Message-State: AOJu0YzTxIEuubgUGy3zMk/tO6D8Esj9xUcjiBdRTFSGohmcxGFWdLWa BpcIjZIARkTAt9cG0K++aNtpARASoS0BjG9h6q9mDpZI9ohOk7qbq5UvIokivWVj X-Gm-Gg: ASbGnct2o5onrbBdkmHGAHXtQdbS10WQvm7VNUk6a61lxAEnBkcy2Wb3/olohY4My8X 0tqGda8VzFTxb85ylOUBp/ClRDlsUUoYyr1EV4nD4xvFwfVj2s8Z8YWapNB1rr83NFL/Ro6eX6X 02vSilR6Nu1zdcq8DhNkfoO2yvUv713vz5zYPYM0U+3+L7kwmgh0ng9T0aQcXIsHB6ZyPrH4V3S xeV/bBccTrPcDJfQkJmbANY82kOMcFbS9hvNGEHWeSbwGhsOR/yThhugEKfV9ZkP8o1uBWzjZfV GnkfL+3bAs557qjJU0a+FIBZpjMak9J6SFiB0rWaxjTAR5MC5fhiY5Pe/fBU1KuPA3mzmb07UQE jp8YhTr6t6mPcmHEJaJ80TQ== X-Google-Smtp-Source: AGHT+IF71gLDQSPGnuG33RBH3DrItmDEU1rYVFkDKy9aqdKKfsQe2uL3bj/ENFiMeeysCK+kYocgDA== X-Received: by 2002:a17:903:3bc6:b0:240:4faa:75cd with SMTP id d9443c01a7336-245e04d6698mr47334125ad.48.1755627811457; Tue, 19 Aug 2025 11:23: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 , 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 10/35] tcg/wasm: Add and/or/xor instructions Date: Wed, 20 Aug 2025 03:21:39 +0900 Message-ID: <8a445f65e74dc24aa68273bfb43377b52be830d0.1755623521.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::632; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pl1-x632.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: 1755628393642124100 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, these instructions work by first pushing the operands into the Wasm's stack using get instructions. The result is left on the stack and this can be assigned to a variable by popping it using a set instruction. The Wasm binary format is documented at [1]. In this backend, TCI instructions are emitted to s->code_ptr, while the corresponding Wasm instructions are generated into a separate buffer allocated via tcg_malloc(). This buffer is intended to be merged into the TB before tcg_gen_code returns. Additionally, since the Wasm instuction's index operand must be LEB128-encoded, this commit introduces an encoder function implemented following [2]. [1] https://webassembly.github.io/spec/core/binary/index.html [2] https://en.wikipedia.org/wiki/LEB128 Signed-off-by: Kohei Tokunaga --- tcg/wasm/tcg-target.c.inc | 110 +++++++++++++++++++++++++++++++++++++- 1 file changed, 109 insertions(+), 1 deletion(-) diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index f1c329eabd..8f7ead5e69 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -25,6 +25,8 @@ * THE SOFTWARE. */ =20 +#include "qemu/queue.h" + /* Used for function call generation. */ #define TCG_TARGET_CALL_STACK_OFFSET 0 #define TCG_TARGET_STACK_ALIGN 8 @@ -92,6 +94,109 @@ static const char *const tcg_target_reg_names[TCG_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 REG_IDX(r) tcg_target_reg_index[r] + +typedef enum { + OPC_GLOBAL_GET =3D 0x23, + OPC_GLOBAL_SET =3D 0x24, + + OPC_I64_AND =3D 0x83, + OPC_I64_OR =3D 0x84, + OPC_I64_XOR =3D 0x85, +} WasmInsn; + +#define BUF_SIZE 1024 +typedef struct LinkedBufEntry { + uint8_t data[BUF_SIZE]; + uint32_t size; + QSIMPLEQ_ENTRY(LinkedBufEntry) entry; +} LinkedBufEntry; + +typedef QSIMPLEQ_HEAD(, LinkedBufEntry) LinkedBuf; + +static void linked_buf_out8(LinkedBuf *linked_buf, uint8_t v) +{ + LinkedBufEntry *buf =3D QSIMPLEQ_LAST(linked_buf, LinkedBufEntry, entr= y); + if (!buf || (buf->size =3D=3D BUF_SIZE)) { + LinkedBufEntry *e =3D tcg_malloc(sizeof(LinkedBufEntry)); + e->size =3D 0; + QSIMPLEQ_INSERT_TAIL(linked_buf, e, entry); + buf =3D e; + } + buf->data[buf->size++] =3D v; +} + +static void linked_buf_out_leb128(LinkedBuf *p, uint64_t v) +{ + uint8_t b; + do { + b =3D v & 0x7f; + v >>=3D 7; + if (v !=3D 0) { + b |=3D 0x80; + } + linked_buf_out8(p, b); + } while (v !=3D 0); +} + +/* + * wasm code is generataed in the dynamically allocated buffer which + * are managed as a linked list. + */ +static __thread LinkedBuf sub_buf; + +static void init_sub_buf(void) +{ + QSIMPLEQ_INIT(&sub_buf); +} +static void tcg_wasm_out8(TCGContext *s, uint8_t v) +{ + linked_buf_out8(&sub_buf, v); +} +static void tcg_wasm_out_leb128(TCGContext *s, uint64_t v) +{ + linked_buf_out_leb128(&sub_buf, v); +} + +static void tcg_wasm_out_op(TCGContext *s, WasmInsn opc) +{ + tcg_wasm_out8(s, opc); +} +static void tcg_wasm_out_op_idx(TCGContext *s, WasmInsn opc, uint32_t idx) +{ + tcg_wasm_out8(s, opc); + tcg_wasm_out_leb128(s, idx); +} + +static void tcg_wasm_out_o1_i2( + TCGContext *s, WasmInsn opc, TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg2)); + tcg_wasm_out_op(s, opc); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); +} + static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, intptr_t value, intptr_t addend) { @@ -551,6 +656,7 @@ static void tgen_and(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { tcg_out_op_rrr(s, INDEX_op_and, a0, a1, a2); + tcg_wasm_out_o1_i2(s, OPC_I64_AND, a0, a1, a2); } =20 static const TCGOutOpBinary outop_and =3D { @@ -741,6 +847,7 @@ static void tgen_or(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { tcg_out_op_rrr(s, INDEX_op_or, a0, a1, a2); + tcg_wasm_out_o1_i2(s, OPC_I64_OR, a0, a1, a2); } =20 static const TCGOutOpBinary outop_or =3D { @@ -912,6 +1019,7 @@ static void tgen_xor(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { tcg_out_op_rrr(s, INDEX_op_xor, a0, a1, a2); + tcg_wasm_out_o1_i2(s, OPC_I64_XOR, a0, a1, a2); } =20 static const TCGOutOpBinary outop_xor =3D { @@ -1246,7 +1354,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 15 05:21:41 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=1755628398; cv=none; d=zohomail.com; s=zohoarc; b=gMG5c8xHLMvniZiHXAB2BigYoPYFSx77NF+ZIE+M+F8+GdKxuVpBsDdQnENso2/CO7Ek3HkcPoep0SsxPG+DQNlew+OZLs3I43ih7TFlXSA3Ir+C7XberZ+hN+sqMXfdNNA7DRKljkoy6k6qvf88+HCys8+hldpqrC0PkAFHe/4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755628398; 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=qwwpP8Qm6yRdkuHPQNpOEFmILh0DyGgQsAD0yWxqZSw=; b=XQ4PmbBgfEg9ZyOnZaaxne5C5Nc7a0nuWT8CsjqjCkHpySNXlPa1PI7g9UtZiHgvLsTIWj0LmZRv+JHAXU80S9mpdbE6EGqBo8Ccr47Qqt3i+VGus60Xiay5AlINtV1D/sIFcZ9jLWIkUtIuBRdgc6KVgrwibpJmTUsBDZkSFgw= 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 175562839833464.84600408223332; Tue, 19 Aug 2025 11:33:18 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoQzt-0005To-OD; Tue, 19 Aug 2025 14:23:49 -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 1uoQzm-0005Jm-3O; Tue, 19 Aug 2025 14:23:43 -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 1uoQzi-0003r4-WF; Tue, 19 Aug 2025 14:23:41 -0400 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-244580523a0so50335025ad.1; Tue, 19 Aug 2025 11:23:37 -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.23.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:23:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627817; x=1756232617; 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=qwwpP8Qm6yRdkuHPQNpOEFmILh0DyGgQsAD0yWxqZSw=; b=L9oHCiibe1LRZIMjGYtigaA9rLQELAclSy7AYAAwoIIs8NFw8YmruwWqQ9tTv1JMLP aKQkin7X+IrJ6xoiFRbgpw+dRcqTURJ8sYU+GdW2wuxh2E0vWXAbQC8guozIJCtX4hXI fBWBLLYFA2Dpl+g+a/FB0XdHgdZP4hVWztmAYGuIPdH21t6zuYxiAHxJkXJr+Z1gMeSM g07Ho0cJtcHqbxM2n9H+7/BKsweK2g9OP3eOcIqNL6O6qBsVYowcFcbllQO7Ubn5tHqS 1xMqt6dqtVMZPUeuXvzRxm9dXIkyIxxlkjmIXC8vRahEeh82FFE8eyeOx7Fq5RrtwZpS /G3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627817; x=1756232617; 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=qwwpP8Qm6yRdkuHPQNpOEFmILh0DyGgQsAD0yWxqZSw=; b=lSTSIWNjuHK6YWKbsPi1lsCCq3fPkXPHeP5HmFdr4yFtIvH8sH3ijjS+ESt90gMDm2 Djti+8ualbRHMirZZXJm/gsKpCd13p29K2fkyOv9mqo73nmjtjWkvhONNRhzQdq4kD3F sh6abrMzP9/oDNKdZQ/smh8DcSqlEc/ccDQEOYXA/k8C07zBXzjVlY4WVCkGHOL70qnN jwfPy1WLhGUhH+uJOrMp4nqgZ/E6+mKAhCg1U7Qspc5KYInneKPpoChESs5+qwHtOKc/ rSNtgPQDycvDwOdbupscYa8pX8ryAiZZbofHIqappReLnxfcG/fbh1SxhrUnKGjnxFD9 0iUQ== X-Forwarded-Encrypted: i=1; AJvYcCUK2U+fBbwWkrBRvR78hK4ljyydN9BhpHW8cIV8GgS5Qi7k3616JU97HtSuOHS+Dhe4PqShFF4KCQ==@nongnu.org, AJvYcCWdk6rqGs7CtuiYDmGVUhIpDexHZQIcBgqncyuK9kzBCAdWv2AfsW9WfLvqJDWmCVqcebiqukI6hXJPfA==@nongnu.org X-Gm-Message-State: AOJu0Ywu3+GoLDKyXJhEMdl5/5+Z+FySnz9dZAayTZ88FfXcJ6Z2SaNA /FKFgb+46yZ0CRRXChCRPh2Ft1oBiqAGjxQhkyF6mnjglimT3Oc3aIrX95YExUHN X-Gm-Gg: ASbGncuIy7gNxPRSZT+eblXRO7RiBXWWpYRHLDS4U8pPu1RCIWqH0WteojD2iCC3qLK OEnxOUwpFDsYrfONhrWM4G7daYFaC1JzI15k6MXA1IDMhhLpGL65YXVHSWuy49FIUadpf9urm3H 8kgE77EklvuOtU1PmpWtfM8coqI0TsPKIRvWglU/FElCk5+dkOL4Lfi90c0lZLwe5ckSfd4KxMc kbW0sutDFrc3ne4eYa84qr9CvZIRLiwvLq+5i/Q3s9qijmMruM3/As0fWH9zaLyfcYxQ05ZQqtP RSIn7xHSCQ6WqaZ18ZavbRBGolIeBllJl/3QgZnUt/YeRrIFYj1SG3HX4TMztmoopgU5+Bo9MBa 0VFIX/BOQ2ju6N2vWCdzSwgRsH1jM6P8k X-Google-Smtp-Source: AGHT+IG037rj1DjmMOxlhC8+jtmYSnV8dQpyxDZN9Hkh7deiTmKhgUFXJ8d8hTtaZsexdVYt8Sj01g== X-Received: by 2002:a17:902:ce0b:b0:23d:d0e7:754b with SMTP id d9443c01a7336-245e04919bfmr42930885ad.18.1755627816944; Tue, 19 Aug 2025 11:23:36 -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 11/35] tcg/wasm: Add add/sub/mul instructions Date: Wed, 20 Aug 2025 03:21:40 +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: 1755628399692124100 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/wasm/tcg-target.c.inc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index 8f7ead5e69..e1b10c57b0 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -120,6 +120,9 @@ typedef enum { OPC_GLOBAL_GET =3D 0x23, OPC_GLOBAL_SET =3D 0x24, =20 + OPC_I64_ADD =3D 0x7c, + OPC_I64_SUB =3D 0x7d, + OPC_I64_MUL =3D 0x7e, OPC_I64_AND =3D 0x83, OPC_I64_OR =3D 0x84, OPC_I64_XOR =3D 0x85, @@ -599,6 +602,7 @@ static void tgen_add(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { tcg_out_op_rrr(s, INDEX_op_add, a0, a1, a2); + tcg_wasm_out_o1_i2(s, OPC_I64_ADD, a0, a1, a2); } =20 static const TCGOutOpBinary outop_add =3D { @@ -777,6 +781,7 @@ static void tgen_mul(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { tcg_out_op_rrr(s, INDEX_op_mul, a0, a1, a2); + tcg_wasm_out_o1_i2(s, OPC_I64_MUL, a0, a1, a2); } =20 static const TCGOutOpBinary outop_mul =3D { @@ -967,6 +972,7 @@ static void tgen_sub(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { tcg_out_op_rrr(s, INDEX_op_sub, a0, a1, a2); + tcg_wasm_out_o1_i2(s, OPC_I64_SUB, a0, a1, a2); } =20 static const TCGOutOpSubtract outop_sub =3D { --=20 2.43.0 From nobody Sat Nov 15 05:21:41 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=1755627976; cv=none; d=zohomail.com; s=zohoarc; b=ItiRYrz5T0T3wsY7NS5AQomYQtoIdlgueKAtYOnRCvrlvxNsokFkuzYV6qF/5EJ+C9zyTLjRaLUvhszZYXqjCMYmYQytZeZnsaZsjY42soDX/7sBVglG+tHjg2vd5jy4yXMozW5M9gAJDLSPfXOpsThAmjlzmFtxuedLHziOjoo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755627976; 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=eDaxTkVmNHgEN/pCYynl1Q9zMx/LngyuWvcU/DTVG5s=; b=hzaPgdr66cIRF1EfClEPdb+OK0asQqJhirmyUlP9IHNZ/HYkaI+vXA/SsPN6BWfrdaQri4rs5MG6Abw/uuPGV/3M/CSYJ+zIy4BEB2baF0ovC0Eeimf4zjvGK84z8cTSy6TbUah8mHVZqpbsVmLpr4SJW6swj6R18OrqhJ8SzSg= 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 1755627976908530.1664657369209; Tue, 19 Aug 2025 11:26:16 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoQzw-0005g3-8r; Tue, 19 Aug 2025 14:23:52 -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 1uoQzt-0005Tx-GB; Tue, 19 Aug 2025 14:23:49 -0400 Received: from mail-pg1-x52a.google.com ([2607:f8b0:4864:20::52a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uoQzp-0003rn-Os; Tue, 19 Aug 2025 14:23:48 -0400 Received: by mail-pg1-x52a.google.com with SMTP id 41be03b00d2f7-b474e8d6d01so1393473a12.0; Tue, 19 Aug 2025 11:23:44 -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.23.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:23:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627823; x=1756232623; 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=eDaxTkVmNHgEN/pCYynl1Q9zMx/LngyuWvcU/DTVG5s=; b=k1MROYhX8L1sCefGX8P6OnJOXB6ZUfZOqgdseknYxg6aETfRON30WhFx9sH+v6ecUU 20bXDJ33oDNXLy0bH3X5Y5qAhm579LFxOS0fM/i2To9cm5Hw/XC14Ez5KSRQqR/jRoa7 qmhj194P5fEwX93yjBtuR8qsCcRsTP17GLa3QUirCqQvYZAtDbwTNn/0iuYCvDhzhxRY F9YIWHPr5c27JXM41cMAQdtHf/LKFTuD947ntYPGYKeRDrG2Le0H6IqGEyo4YWyHnVlj ijQ44NECz7Pbdj5Et8Q+FQ2sBUZr7lwo/vT3ywHRilwUnxsk2pe7n7SOqONigr0BQ6fJ 5t4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627823; x=1756232623; 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=eDaxTkVmNHgEN/pCYynl1Q9zMx/LngyuWvcU/DTVG5s=; b=saoRFVyHIt43uUyi3SpN8p0VU0JS2BLnC6ElTPNFGAgAfab8nlS1kvKlCvY40K05KH G6Q760cYKsCUOUmZts0o4GuHNtnpajQAf0wBVSJjyW2ouPsWCGULZwj/2eBbYH/ZcYp8 SFUtmoqan/YZtNE8voplvhyo7XmVik5wJY1Ff6yvRBOiGQs/8FGigTbXIgLbY7C/hPcv ygOb9P66O3obIAsRQRs/g0XMFyHvo9+gK9ijNv0qlCH1fUujWWJ/l2++rbMZ3G/T6krU ZfdEvx4cV9Fcmq8Hiaro0uQ/SGrBld1AM+EBq6jzGvkwAyTlffbJ384M480jBBWC6Ca+ 6pfg== X-Forwarded-Encrypted: i=1; AJvYcCU4JQxjarLtiVRChNLZ/eIp2Byl1taX3c4kXE8ByCtTNoKFacuR72VYAzT93vWRV73bVUPPX82Sz+A3zQ==@nongnu.org, AJvYcCUFibZs4r1uA39LewVKzc6HjGfmS9kfBuNDXeYbkh9KIEyVm2wu3PZb35MtMN3/1hb3jmpAsJ27nA==@nongnu.org X-Gm-Message-State: AOJu0Yx2x7M9PdyzizvxKJKS0X+2BgkV01lArO4ar6CsWJOAVglkxZzB dGZ8bD2sYPXZkZGNBje064Vbg8WWOfCZcR2svzfPxTi6LkBoawD0C76xNWGosQmn X-Gm-Gg: ASbGncuxPHXYHpfFaasyX0dgYN5Jn9Mx/VLUjqL3ejjuHJL7k/xe7gOv6wR11DEFrh8 F9s91jjL62ArMS5TP89jggJRnFdT7YE1TGwuYO1bemwcH9/4M+GgiIRJuiigB96ItKqMUP+2NVd 0Sbo6eSYqJgKkcJ5GhHGQCHleUg/QPx41ny0QJ0NNRZ7y+lvxGPhkMKqQZsAZtPfcqj3+Aw/YMs csISdGqFXBzxdHjPmVIA9m2WDe9MHMIx4v+m6T0mgkG7vamwnYf9tI64UOGTg+ZMCuy92GDxYiR zx7vXXMcIUi2++0uZijwrqoJP/pfA5Jt1GYk6UjYcnJ/TynUMfGGX0amyrZnBUZhMWv+B+LRO3k Ew8G3F7DUb6r/x6+g26AlQ1H4uYbu052o X-Google-Smtp-Source: AGHT+IFgiZyY03En/iqAp7Fe45Qscuvesp0t1K+erMqvymFeGjE649/ub+gJU7Nv0+7e/+W5CTxQtA== X-Received: by 2002:a17:902:f60a:b0:240:2953:4b6b with SMTP id d9443c01a7336-245e02c129fmr47704645ad.2.1755627822609; Tue, 19 Aug 2025 11:23: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 , 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 12/35] tcg/wasm: Add shl/shr/sar instructions Date: Wed, 20 Aug 2025 03:21:41 +0900 Message-ID: <1df7099b73d5a3926a05f2606b5dde9e10f05532.1755623521.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::52a; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pg1-x52a.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: 1755627978735124100 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 extract the lower 32bit of the operand before shifting. Signed-off-by: Kohei Tokunaga --- tcg/wasm/tcg-target.c.inc | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index e1b10c57b0..81e83a8bdf 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -120,12 +120,22 @@ typedef enum { OPC_GLOBAL_GET =3D 0x23, OPC_GLOBAL_SET =3D 0x24, =20 + OPC_I32_SHL =3D 0x74, + OPC_I32_SHR_S =3D 0x75, + OPC_I32_SHR_U =3D 0x76, + OPC_I64_ADD =3D 0x7c, OPC_I64_SUB =3D 0x7d, OPC_I64_MUL =3D 0x7e, OPC_I64_AND =3D 0x83, OPC_I64_OR =3D 0x84, OPC_I64_XOR =3D 0x85, + OPC_I64_SHL =3D 0x86, + OPC_I64_SHR_S =3D 0x87, + OPC_I64_SHR_U =3D 0x88, + + OPC_I32_WRAP_I64 =3D 0xa7, + OPC_I64_EXTEND_I32_U =3D 0xad, } WasmInsn; =20 #define BUF_SIZE 1024 @@ -199,6 +209,27 @@ static void tcg_wasm_out_o1_i2( tcg_wasm_out_op(s, opc); tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); } +static void tcg_wasm_out_o1_i2_type( + TCGContext *s, TCGType type, WasmInsn opc32, WasmInsn opc64, + TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + switch (type) { + case TCG_TYPE_I32: + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + tcg_wasm_out_op(s, OPC_I32_WRAP_I64); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg2)); + tcg_wasm_out_op(s, OPC_I32_WRAP_I64); + tcg_wasm_out_op(s, opc32); + tcg_wasm_out_op(s, OPC_I64_EXTEND_I32_U); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); + break; + case TCG_TYPE_I64: + tcg_wasm_out_o1_i2(s, opc64, ret, arg1, arg2); + break; + default: + g_assert_not_reached(); + } +} =20 static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, intptr_t value, intptr_t addend) @@ -930,11 +961,14 @@ static const TCGOutOpBinary outop_rotr =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_o1_i2_type(s, type, OPC_I32_SHR_S, OPC_I64_SHR_S, + a0, orig_a1, a2); } =20 static const TCGOutOpBinary outop_sar =3D { @@ -946,6 +980,7 @@ static void tgen_shl(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { tcg_out_op_rrr(s, INDEX_op_shl, a0, a1, a2); + tcg_wasm_out_o1_i2(s, OPC_I64_SHL, a0, a1, a2); } =20 static const TCGOutOpBinary outop_shl =3D { @@ -956,11 +991,14 @@ 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_o1_i2_type(s, type, OPC_I32_SHR_U, OPC_I64_SHR_U, + a0, orig_a1, a2); } =20 static const TCGOutOpBinary outop_shr =3D { --=20 2.43.0 From nobody Sat Nov 15 05:21:41 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=1755627896; cv=none; d=zohomail.com; s=zohoarc; b=SwS6Z3tmwyrJUKPNDar+n3LWauo0SMBy7PQaGsE3KQsALm7HcWNNXz/7vD3IrjCnFhxAxk0ma04YnDglhTp2rDmYn2ilg8sX0H5qasZqYz7f/FvOkBBSvKAJqlc8TRnuRfGwZs8DrQtCWWL4hYCE5PfuwxZH9Ti/88XAwoj8Z80= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755627896; 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=W7pidbMisoPIBJ7RWKvtdHMzW1in7nxoy5d87oIlaqo=; b=kRO9BE1uUt9bmtIEhO0flC+1kPZyDJwvbUt4BdZWat5L8L8dINiGIMhwgm7B3isqlR5zrBUr78JjhnBA/Si7EU5hz6XZxgVbetGlXDryQwh94obmBh0TFe1LDAMD5jOB/pS2WtQ95d5yqiicq3uhtWn6v2v7sKcaGhZP/2Rm/LU= 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 1755627896929311.5885940686536; Tue, 19 Aug 2025 11:24:56 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoQzy-0005iq-Ip; Tue, 19 Aug 2025 14:23: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 1uoQzw-0005hF-Kv; Tue, 19 Aug 2025 14:23:52 -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 1uoQzu-0003sV-Gz; Tue, 19 Aug 2025 14:23:52 -0400 Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-76e2e8e2d2dso3635556b3a.1; Tue, 19 Aug 2025 11:23:49 -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.23.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:23:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627828; x=1756232628; 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=W7pidbMisoPIBJ7RWKvtdHMzW1in7nxoy5d87oIlaqo=; b=cYsh0NM8VE/pV2sODlGGMAHUAkKAikCd73vkcokecHv4epKUX5nMqGJ5sv6YclBD3C TpIhtWCKmT1jSvbX9fGM2rwEtgs47PXjXLO6isKIMe6nUTfF0PMvhwXcc3Bx+MLJZ2Uc /J0uhkkuuyxUu0gUqxmfHUNFTLqcpIRt/Bpl+9Y/4uA77C3IS3v0LdK0o5SiifEhaqy+ qaKWU2aMaL6z13N6t1fTV69Ou7qGMx8HaJ4kPX8nw9O91N9eSFwQdbn/3iowjYEt9xKr boXQZPxrMG6rFzdBqicv20wk2BrmoFM8OiAsexWqcR4Cam5pmhwH4jEjnRt4QGBFmUd2 +TWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627828; x=1756232628; 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=W7pidbMisoPIBJ7RWKvtdHMzW1in7nxoy5d87oIlaqo=; b=F06WANXnQuTCYkfhDWlVWklyf61a1DzkP2rvMJccGF2miVwG72UxX3Wp8GS2WxzFaj 5cih/FcdkwFQNpHh3Q0BQOIKk9yb6cOykBHZOOWA+XbN49IQX6ipmKoAvI4LJef8lfnj 1Lo2T+lxLorNfcocrOPctsVCTgShOcC7FlXqdDdMG2Di+3O4r0u3sAp3qmjT1oCkEWbo wo/oVeVu1PjmfP/KaWK8AUsaeBedlcoLoEFp2fxdvkprbAMXzyr3JDCBU/7SGKSq4KqM 3gAhRqOnBEppqLtLFWVhLN6UILRfujK296lXOrvlSdEX7+4lqZ1LkCtMHoMsOVO/GYNo np2A== X-Forwarded-Encrypted: i=1; AJvYcCURMYsmCwLjVs6DA5X7kHOvDHH9qF6bgyUBT29QEirjtmyWSr68zCbUMZnojNKFDXEK44/y6K0zynTFRA==@nongnu.org, AJvYcCWpNypxBNtR1Q7EZ9e1KBH/Lsva9EqA6vK3oMP5SJp2nGxKnTZ1mHekWDyq/1V7/daRk6MrOjWNwQ==@nongnu.org X-Gm-Message-State: AOJu0YyQNLJAtdmiaL+QRv36f/L0rHtCTBf2cB5bGWmN3UH4MyFJ8HNh /TRg9qJELc0LBR1LalFOtIBHITIkyIcXQo6K0VnmyQColA0XswAyfCJtjhaG9SXe X-Gm-Gg: ASbGncu+Rt2U3QQpd6LfdYclPeeW/ZZEegb3zzQDtboH2c6RSf6RJLSDJ2K5MDSZ737 511h8Z/8XR5lU0DXyjw6NEbhsHv8Ho4rxyRhZET0JAT8HxOmlbRv4+TZX1j1rGbfy8lJkix5a4z 7wRo138k7TnzGJJILAHezEMNELZ78Q2enPhcEeAJ/JLMxNWs1CWN21wSURatjsDE36DFYcKlv6C fPXRMftNeXc14g0zCrJF406MiCoPrQljCDznPrRkBP1jyJuMMBRgvJLnDmZk/HFeFPHaUh3KSMA IXklyTGl/uXCmQ3z6/KQQ4FM4F4EecNbN5f4ReFA+ZfUfGQCSGn4WwhKgoycAwwbLr7H0Dx8noV uLgpWleEHAf016Y1GXJtu4w== X-Google-Smtp-Source: AGHT+IEwVPKyWzyRg9IMeGfXhOTZdVHPlfwIN5f+/d7UPxKYiNnemrPGIPwhSx05r9r028BpoyjUUg== X-Received: by 2002:a17:903:2449:b0:242:9bc4:f1c9 with SMTP id d9443c01a7336-245e0504d60mr50238875ad.56.1755627828182; Tue, 19 Aug 2025 11:23: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 , 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 13/35] tcg/wasm: Add setcond/negsetcond/movcond instructions Date: Wed, 20 Aug 2025 03:21:42 +0900 Message-ID: <21259acc327a42f7bff3aca6135439ee065d416f.1755623521.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: 1755627899962124100 Content-Type: text/plain; charset="utf-8" These TCG instructions are implemented by using Wasm's if and else instructions. Support for TCG_COND_TSTEQ and TCG_COND_TSTNE is not yet implemented, so TCG_TARGET_HAS_tst is set to 0. The tgen_setcond function was used by several other functions (e.g. tgen_negsetcond) and intended specifically for emitting TCI code. So it has been renamed to tgen_negsetcond_tci. Signed-off-by: Kohei Tokunaga --- tcg/wasm/tcg-target-has.h | 2 +- tcg/wasm/tcg-target.c.inc | 166 +++++++++++++++++++++++++++++++++++++- 2 files changed, 163 insertions(+), 5 deletions(-) diff --git a/tcg/wasm/tcg-target-has.h b/tcg/wasm/tcg-target-has.h index ab07ce1fcb..1eaa8f65f6 100644 --- a/tcg/wasm/tcg-target-has.h +++ b/tcg/wasm/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/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index 81e83a8bdf..03cb3b2f46 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -117,9 +117,37 @@ static const uint8_t tcg_target_reg_index[TCG_TARGET_N= B_REGS] =3D { #define REG_IDX(r) tcg_target_reg_index[r] =20 typedef enum { + OPC_IF =3D 0x04, + OPC_ELSE =3D 0x05, + OPC_END =3D 0x0b, OPC_GLOBAL_GET =3D 0x23, OPC_GLOBAL_SET =3D 0x24, =20 + OPC_I32_CONST =3D 0x41, + OPC_I64_CONST =3D 0x42, + + OPC_I32_EQ =3D 0x46, + OPC_I32_NE =3D 0x47, + OPC_I32_LT_S =3D 0x48, + OPC_I32_LT_U =3D 0x49, + OPC_I32_GT_S =3D 0x4a, + OPC_I32_GT_U =3D 0x4b, + OPC_I32_LE_S =3D 0x4c, + OPC_I32_LE_U =3D 0x4d, + OPC_I32_GE_S =3D 0x4e, + OPC_I32_GE_U =3D 0x4f, + + OPC_I64_EQ =3D 0x51, + OPC_I64_NE =3D 0x52, + OPC_I64_LT_S =3D 0x53, + OPC_I64_LT_U =3D 0x54, + OPC_I64_GT_S =3D 0x55, + OPC_I64_GT_U =3D 0x56, + OPC_I64_LE_S =3D 0x57, + OPC_I64_LE_U =3D 0x58, + OPC_I64_GE_S =3D 0x59, + OPC_I64_GE_U =3D 0x5a, + OPC_I32_SHL =3D 0x74, OPC_I32_SHR_S =3D 0x75, OPC_I32_SHR_U =3D 0x76, @@ -138,6 +166,12 @@ typedef enum { OPC_I64_EXTEND_I32_U =3D 0xad, } WasmInsn; =20 +typedef enum { + BLOCK_NORET =3D 0x40, + BLOCK_I64 =3D 0x7e, + BLOCK_I32 =3D 0x7f, +} WasmBlockType; + #define BUF_SIZE 1024 typedef struct LinkedBufEntry { uint8_t data[BUF_SIZE]; @@ -172,6 +206,23 @@ static void linked_buf_out_leb128(LinkedBuf *p, uint64= _t v) } while (v !=3D 0); } =20 +static void linked_buf_out_sleb128(LinkedBuf *p, 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; + } + linked_buf_out8(p, b); + } +} + /* * wasm code is generataed in the dynamically allocated buffer which * are managed as a linked list. @@ -190,6 +241,10 @@ static void tcg_wasm_out_leb128(TCGContext *s, uint64_= t v) { linked_buf_out_leb128(&sub_buf, v); } +static void tcg_wasm_out_sleb128(TCGContext *s, int64_t v) +{ + linked_buf_out_sleb128(&sub_buf, v); +} =20 static void tcg_wasm_out_op(TCGContext *s, WasmInsn opc) { @@ -200,6 +255,30 @@ static void tcg_wasm_out_op_idx(TCGContext *s, WasmIns= n opc, uint32_t idx) tcg_wasm_out8(s, opc); tcg_wasm_out_leb128(s, idx); } +static void tcg_wasm_out_op_block(TCGContext *s, WasmInsn opc, WasmBlockTy= pe t) +{ + tcg_wasm_out8(s, opc); + tcg_wasm_out8(s, t); +} +static void tcg_wasm_out_op_const(TCGContext *s, WasmInsn opc, int64_t v) +{ + tcg_wasm_out8(s, opc); + switch (opc) { + case OPC_I32_CONST: + tcg_wasm_out_sleb128(s, (int32_t)v); + break; + case OPC_I64_CONST: + tcg_wasm_out_sleb128(s, v); + break; + default: + g_assert_not_reached(); + } +} +static void tcg_wasm_out_op_not(TCGContext *s) +{ + tcg_wasm_out_op_const(s, OPC_I64_CONST, -1); + tcg_wasm_out_op(s, OPC_I64_XOR); +} =20 static void tcg_wasm_out_o1_i2( TCGContext *s, WasmInsn opc, TCGReg ret, TCGReg arg1, TCGReg arg2) @@ -231,6 +310,76 @@ static void tcg_wasm_out_o1_i2_type( } } =20 +static const struct { + WasmInsn i32; + WasmInsn i64; +} tcg_cond_to_inst[] =3D { + [TCG_COND_EQ] =3D { OPC_I32_EQ, OPC_I64_EQ }, + [TCG_COND_NE] =3D { OPC_I32_NE, OPC_I64_NE }, + [TCG_COND_LT] =3D { OPC_I32_LT_S, OPC_I64_LT_S }, + [TCG_COND_GE] =3D { OPC_I32_GE_S, OPC_I64_GE_S }, + [TCG_COND_LE] =3D { OPC_I32_LE_S, OPC_I64_LE_S }, + [TCG_COND_GT] =3D { OPC_I32_GT_S, OPC_I64_GT_S }, + [TCG_COND_LTU] =3D { OPC_I32_LT_U, OPC_I64_LT_U }, + [TCG_COND_GEU] =3D { OPC_I32_GE_U, OPC_I64_GE_U }, + [TCG_COND_LEU] =3D { OPC_I32_LE_U, OPC_I64_LE_U }, + [TCG_COND_GTU] =3D { OPC_I32_GT_U, OPC_I64_GT_U } +}; + +static void tcg_wasm_out_cond( + TCGContext *s, TCGType type, TCGCond cond, TCGReg arg1, TCGReg arg2) +{ + switch (type) { + case TCG_TYPE_I32: + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + tcg_wasm_out_op(s, OPC_I32_WRAP_I64); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg2)); + tcg_wasm_out_op(s, OPC_I32_WRAP_I64); + tcg_wasm_out_op(s, tcg_cond_to_inst[cond].i32); + break; + case TCG_TYPE_I64: + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg2)); + tcg_wasm_out_op(s, tcg_cond_to_inst[cond].i64); + break; + default: + g_assert_not_reached(); + } +} + +static void tcg_wasm_out_setcond(TCGContext *s, TCGType type, TCGReg ret, + TCGReg arg1, TCGReg arg2, TCGCond cond) +{ + tcg_wasm_out_cond(s, type, cond, arg1, arg2); + tcg_wasm_out_op(s, OPC_I64_EXTEND_I32_U); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); +} + +static void tcg_wasm_out_negsetcond(TCGContext *s, TCGType type, TCGReg re= t, + TCGReg arg1, TCGReg arg2, TCGCond cond) +{ + tcg_wasm_out_cond(s, type, cond, arg1, arg2); + tcg_wasm_out_op(s, OPC_I64_EXTEND_I32_U); + tcg_wasm_out_op_not(s); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 1); + tcg_wasm_out_op(s, OPC_I64_ADD); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); +} + +static void tcg_wasm_out_movcond(TCGContext *s, TCGType type, TCGReg ret, + TCGReg c1, TCGReg c2, + TCGReg v1, TCGReg v2, + TCGCond cond) +{ + tcg_wasm_out_cond(s, type, cond, c1, c2); + tcg_wasm_out_op_block(s, OPC_IF, BLOCK_I64); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(v1)); + tcg_wasm_out_op(s, OPC_ELSE); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(v2)); + tcg_wasm_out_op(s, OPC_END); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); +} + static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, intptr_t value, intptr_t addend) { @@ -1147,8 +1296,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 @@ -1156,6 +1305,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, @@ -1164,8 +1320,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 { @@ -1176,7 +1333,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 @@ -1193,6 +1350,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 15 05:21:41 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=1755627868; cv=none; d=zohomail.com; s=zohoarc; b=eG/RAIiq466QZKNZ7Bf0N2P1y193FLVTl6m5WI5tF0f3UUcTx0FmHbLrOIs/FsftNPb6j7uQ5y2syTFcs+z1pcv3LamqwNUbBydGlyxNVpuYHuGmqjpCZgBjmh0nm3nI3gDgD/ukFQdm5R+wNqotqtIjkPC6ziWuoxWzf9tKhC4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755627868; 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=ykVIUD/HZ1RbVXRktPCz8rfsNfoXb77ATTdPNzikBa8=; b=G2TVBFTWHDspoSBhtak++qp9l+Xi7VbaGgx+1249nuSyg4/6xw81sops5CczRFf87hXG4oQl2qJUMax2V5LcQYANCSEvJ8Yxyr5lsGE0cs7HxNM2296Pe4hOKvB3/qo6t6QgTmdWWO6eQQgNwS3rERYnYVQ9ic7NhbgPXjBJxXY= 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 1755627867993930.4837076674561; Tue, 19 Aug 2025 11:24:27 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoR0B-00063a-4y; Tue, 19 Aug 2025 14:24: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 1uoR06-0005t6-0E; Tue, 19 Aug 2025 14:24:02 -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 1uoQzz-0003tR-Sd; Tue, 19 Aug 2025 14:23:59 -0400 Received: by mail-pg1-x533.google.com with SMTP id 41be03b00d2f7-b472fd93ad1so2786552a12.0; Tue, 19 Aug 2025 11:23:54 -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.23.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:23:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627834; x=1756232634; 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=ykVIUD/HZ1RbVXRktPCz8rfsNfoXb77ATTdPNzikBa8=; b=IA22oqJclGqQLSShC9aJmXgacH45BuWcgzegj8dcQvtp7cXu7bbao2dJ/XjSx1RpFd M5W3lDTyg7ciHZNtP6tIoNlBpTNBPgWL2xpSgtrrQI01HhMAij0+GGBmezj+lInZgb6d 8MEFNSK1LFAs3PkucAHwm5JRbCVhYehPZwloDZf/4B5l156VwDssekViowz8hMY8GyIU JlKedutv9jDhvIFjQvc0Tyc6/I7RAoq71sbhYTmSzIZDke8v+nfXbqiA7Jx0+lUCzs2m c1pkOawlG+GTgJSXZ0fl9H81DUFjTWMEY725ESlUBJ7iKijTHC3r5B8X+MWb5uJMTSWM 6MEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627834; x=1756232634; 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=ykVIUD/HZ1RbVXRktPCz8rfsNfoXb77ATTdPNzikBa8=; b=wuWJ+KI3wtYxzx+juqDfajcdkq2SeFMU2fU+ft4YaIG3E85V0SytIQk0enlz+gKtNU 2SFS/9i7eivUEDh8zfxLPWF0AMg6lpKRMt3vvHX2Ri9z90/atOKRsR0JFtjWbmqRf9op q0aTK2OwHYywZvZGZENS6comnS7cbEWLAWpaKoS4BRy3Ks2DpZa9hyDBWaJE6N7/tE/0 KqJ5VK/iXkMAi5D2S1xJpIWZz9FHmGnwTgF2G9gpo6q7xDPGvAeI4AqlhvlGZPIKInhM G3KumZgyDQjgaQ62iKGnK89mvD4TySJXyK4pypNC4hSTUvDDHoC8zyjFXG1o4h19Nrql Jjog== X-Forwarded-Encrypted: i=1; AJvYcCV5np4G2/I8i/QmqXjcVyvzYw54KNZw6liLVkEZT6R5Q867ATvzpRqViA1cv1Cai+uln7HHlALHxw==@nongnu.org, AJvYcCWgHNrz9kcqTeRbUVR2tx5UW/4+4aHA+nDHige1uCK8XUkenpm8kMLJDbzYCNg9o7iULRq/8b7KP+2QXw==@nongnu.org X-Gm-Message-State: AOJu0YzklNurooTLUQSDqVCndHNvWf+ZrJAOPNRhcrExWt5FaTpZ8Yp0 qLwrf5kvsQpDIic18LcPE0RBmhtJUE9d8xOrzAuYA/YJmSotJApghUSozWLHqRUq X-Gm-Gg: ASbGncuO/ACHAmqyBygoICXWwSG1B0fDJdjLx+XPOA7TAmp0IyxAELp9UvqoCCvJkvQ vETf9xevu/K/uGeUdqbmvfpCkJRRTdK14wljyDONl8oufstoI/dI5Q8ekjWi9lY+nzF6e71UGSM 6RmMpioqasGCoeox5I5vZXHrHMziR5AsGEhmpxFo98xDbaOXmHEP+c5sfIaiZaLBJmxrxdK8h3+ oNFZmg/fIeEr4CI9mWC3ATqhbRdf+yBR/es6weQH5ZJZkKtJXen8+YC8mYEIS3tBeC0Z6Gj7joc Rlqcpj6ws2RBA7sGnhRld+bHYRvFHCULWKDYENS8DdTJ/x9g2okuezDxerCowQ6Aq99b/k1vgIy gfi4/9323haDwGU+rKTkhZt/ytHOULQJQ X-Google-Smtp-Source: AGHT+IHkTJyh71TMZ1dvRbUUt9zt76fSqMzH69SwvpGbK7QiB0lli5yfz5jrR66mr0P/DcV+1N8Zaw== X-Received: by 2002:a17:902:e783:b0:235:c9a7:d5f5 with SMTP id d9443c01a7336-245e0307a8fmr46014385ad.13.1755627833675; Tue, 19 Aug 2025 11:23: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 , 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 14/35] tcg/wasm: Add deposit/sextract/extract instrcutions Date: Wed, 20 Aug 2025 03:21:43 +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::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: 1755627869756124100 Content-Type: text/plain; charset="utf-8" The tcg_out_extract and tcg_out_sextract functions were used by several other functions (e.g. tcg_out_ext*) and intended to emit TCI code. So they have been renamed to tcg_tci_out_extract and tcg_tci_out_sextract. Signed-off-by: Kohei Tokunaga --- tcg/wasm/tcg-target.c.inc | 104 +++++++++++++++++++++++++++++++++----- 1 file changed, 91 insertions(+), 13 deletions(-) diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index 03cb3b2f46..6220b43f98 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -163,7 +163,10 @@ typedef enum { OPC_I64_SHR_U =3D 0x88, =20 OPC_I32_WRAP_I64 =3D 0xa7, + OPC_I64_EXTEND_I32_S =3D 0xac, OPC_I64_EXTEND_I32_U =3D 0xad, + OPC_I64_EXTEND8_S =3D 0xc2, + OPC_I64_EXTEND16_S =3D 0xc3, } WasmInsn; =20 typedef enum { @@ -380,6 +383,66 @@ static void tcg_wasm_out_movcond(TCGContext *s, TCGTyp= e type, TCGReg ret, tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(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_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + tcg_wasm_out_op_const(s, OPC_I64_CONST, ~mask); + tcg_wasm_out_op(s, OPC_I64_AND); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg2)); + tcg_wasm_out_op_const(s, OPC_I64_CONST, pos); + tcg_wasm_out_op(s, OPC_I64_SHL); + tcg_wasm_out_op_const(s, OPC_I64_CONST, mask); + tcg_wasm_out_op(s, OPC_I64_AND); + tcg_wasm_out_op(s, OPC_I64_OR); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(dest)); +} + +static void tcg_wasm_out_extract(TCGContext *s, TCGReg dest, TCGReg arg1, + int pos, int len) +{ + int64_t mask =3D ~0ULL >> (64 - len); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + if (pos > 0) { + tcg_wasm_out_op_const(s, OPC_I64_CONST, pos); + tcg_wasm_out_op(s, OPC_I64_SHR_U); + } + if ((pos + len) < 64) { + tcg_wasm_out_op_const(s, OPC_I64_CONST, mask); + tcg_wasm_out_op(s, OPC_I64_AND); + } + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(dest)); +} + +static void tcg_wasm_out_sextract(TCGContext *s, TCGReg dest, TCGReg arg1, + int pos, int len) +{ + int discard =3D 64 - len; + int high =3D discard - pos; + + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + + if ((pos =3D=3D 0) && (len =3D=3D 8)) { + tcg_wasm_out_op(s, OPC_I64_EXTEND8_S); + } else if ((pos =3D=3D 0) && (len =3D=3D 16)) { + tcg_wasm_out_op(s, OPC_I64_EXTEND16_S); + } else if ((pos =3D=3D 0) && (len =3D=3D 32)) { + tcg_wasm_out_op(s, OPC_I32_WRAP_I64); + tcg_wasm_out_op(s, OPC_I64_EXTEND_I32_S); + } else { + if (high > 0) { + tcg_wasm_out_op_const(s, OPC_I64_CONST, high); + tcg_wasm_out_op(s, OPC_I64_SHL); + } + tcg_wasm_out_op_const(s, OPC_I64_CONST, discard); + tcg_wasm_out_op(s, OPC_I64_SHR_S); + } + + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(dest)); +} + static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, intptr_t value, intptr_t addend) { @@ -591,6 +654,12 @@ static void tcg_out_op_rrrrrc(TCGContext *s, TCGOpcode= op, tcg_out32(s, insn); } =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) { @@ -651,7 +720,8 @@ static void tcg_out_movi(TCGContext *s, TCGType type, static void tcg_out_extract(TCGContext *s, TCGType type, TCGReg rd, TCGReg rs, unsigned pos, unsigned len) { - tcg_out_op_rrbb(s, INDEX_op_extract, rd, rs, pos, len); + tcg_tci_out_extract(s, type, rd, rs, pos, len); + tcg_wasm_out_extract(s, rd, rs, pos, len); } =20 static const TCGOutOpExtract outop_extract =3D { @@ -659,10 +729,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 { @@ -676,34 +753,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) @@ -891,6 +968,7 @@ static void tgen_deposit(TCGContext *s, TCGType type, T= CGReg 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 { @@ -948,7 +1026,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 { @@ -1112,7 +1190,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); @@ -1142,7 +1220,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); @@ -1241,7 +1319,7 @@ static void tgen_bswap16(TCGContext *s, TCGType type, { tcg_out_op_rr(s, INDEX_op_bswap16, a0, a1); if (flags & TCG_BSWAP_OS) { - tcg_out_sextract(s, TCG_TYPE_REG, a0, a0, 0, 16); + tcg_tci_out_sextract(s, TCG_TYPE_REG, a0, a0, 0, 16); } } =20 @@ -1255,7 +1333,7 @@ static void tgen_bswap32(TCGContext *s, TCGType type, { tcg_out_op_rr(s, INDEX_op_bswap32, a0, a1); if (flags & TCG_BSWAP_OS) { - tcg_out_sextract(s, TCG_TYPE_REG, a0, a0, 0, 32); + tcg_tci_out_sextract(s, TCG_TYPE_REG, a0, a0, 0, 32); } } =20 --=20 2.43.0 From nobody Sat Nov 15 05:21:41 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=1755628099; cv=none; d=zohomail.com; s=zohoarc; b=FXzzfBHBRcVOGqksFXTkb2eSvZAcyYbRkF/HHtBNXjwsT5sFgXS0EXBC7u9V2uRJ03eAb2SkRc4nT3z6TuvGfm3B2XxEv2xmUsxC5VW7MN7eDZASwrkljPSNJq//yBOANDHzOy3fL6to0W793fPnWAJiCySRPTJAL9W9E8KL6mQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755628099; 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=gV7V9dWB/HkcHsbdYX7u5nmfU2D9G0qv0kcTVXJBfyE=; b=Z6ZSjE7HeN/B0XCov0/b90KBbvQkOLG2WNLG+hLcRrxbaMAPzzMoL6NLh8WjLyGSVHjzidUe4sVgpDSVa9bvb/xEyTLpnTscYD+yu8SDizrIgrOISRDzcKD6u9usIxbnizeFGKKwGMttVqJWpOxg1PSLn5kEAf6BLE+MqJrP54s= 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 1755628099498615.0583509695509; Tue, 19 Aug 2025 11:28:19 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoR0H-0006Fk-Dt; Tue, 19 Aug 2025 14:24:13 -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 1uoR09-000618-It; Tue, 19 Aug 2025 14:24:06 -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 1uoR06-0003tk-9D; Tue, 19 Aug 2025 14:24:05 -0400 Received: by mail-pj1-x1034.google.com with SMTP id 98e67ed59e1d1-32326e69f1dso5991102a91.3; Tue, 19 Aug 2025 11:24:00 -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.23.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:23:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627839; x=1756232639; 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=gV7V9dWB/HkcHsbdYX7u5nmfU2D9G0qv0kcTVXJBfyE=; b=S2qfQAQtb9KR8YkQZamt290Stnrv7pvkLVlJTuGHv7OOcF7xxRL2gfaqyDrCWF5EN6 RB9YcpoVZ/Y9v0wzhCOJQkTTyGYTJXfc6Pk0hfHtsSMoE4e8/eETnF9Tabhxc7SrBKq4 0i8wUWhme+wPNStm01eFZk5rqs9g2BG3ucEdKnQmubeem3/OxiZ0w6DLnkJhcamDDV9C Bk5K9jOA04q7yP3ybX+Ji+IF75op894vGJLgWdBDfH7SsHGnUvH8WBnL+CtqnfpX3B1q +s+sRUGR6Qlw9rf+Ig4ghe/Lwr0ew8CMpFewp8BsG/rBHWX8cmIeCUBYAjpNNTsVaQHX ydCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627839; x=1756232639; 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=gV7V9dWB/HkcHsbdYX7u5nmfU2D9G0qv0kcTVXJBfyE=; b=DTWOF4PgoFIbnSxoZvMTjKY+fijbFszZLAgrVbKTBApUiAFaaqH6rIcjF7MyGK+b/2 CR9ZqBsmtVG7A97VSMb2bVskfbCzYJaeme/yxHjc/tUrgq5zxR2V162fgWGkQBtzpHyp fnj7nfyv6+nJ3p6ag5hwFrNG8a/sNO5bX3i0kBbg7gwQiq86srXzABdfN+pWOeASsBX4 aebT9tfq+4TCZ3YUAE5oB5M64D56FsNPGw4+UVHPx1kTQfEnZQNPBpdCrmpKQ0attyi0 Pn3nTRGa3g7/xwK5Gh+vq1czrUpdlMXcL9XO+yCszcm/sRpB1v/ft/ESfPPmHkJ6SJnR 54VA== X-Forwarded-Encrypted: i=1; AJvYcCX+ywdz34iJKwn3WtPrQhltAccP0lUAj3gwLkHLQoj+LzgxDok5ZEwqmYfYDLudQ/+gkzOYJtc4Lw==@nongnu.org, AJvYcCXgydSInbLuKyxFyUGP0qh+qMUTqwOxMABwxvkGyHw+ByxjloFIVWGFl43uFdsws6c+2ss0uvkR8BEH7w==@nongnu.org X-Gm-Message-State: AOJu0YwtHQL+oGZ/0iL06YH3KPi7tDHsDUfkbHx0KbagnUGLcctPdGRv XcP1jhdj63SNnJ/YfxqglU+dwDGgBmY7eJBouOj/vA0qoZC6z87aKisDNDqBPj61 X-Gm-Gg: ASbGncueYNP2D/mvMuRSqrRQAYHtpq/EWClQWFcbnJXm3Zg17XtGRRQMegBH8V/fLVy L7DPYKldr4n4C8OJZD4QOAFJs/6Tn3fTyGgba1Oho8JQJlr22k2sKWCkUUirwR6TXvQIMwu3lZ4 OVFAV0LWC4IQwn1xCEeOA0pyJ2V/Im9JFxWFIV/KD3Kb9ajijDiHnHzbO376a1C4IUqMkkJtj7Q CVKGpFVVNTkD2dx5jOWdncznAbIFOmDc+7EGyTStpO7neLiVe8PtuWObLsIp8h5UuNQClpPRe97 hNsS9tQKqYU2dZIl6HonqbijBN1Ncrzjf3dXN8KPJSMxROGSZ0CmipcVPAl9jskc53XV9jwQD2A 3yJAQB0Ix8WAwe9bd5+TKAgZqPaXBNuF3qR/NRl1OFOM= X-Google-Smtp-Source: AGHT+IFHVgk93sTkVZnRGsKD1u9m+QLZdANfoF1I5cm+bO+57vxHkLNidvt0MjpwxbR+xa8dexYrYQ== X-Received: by 2002:a17:903:292:b0:242:c66f:9f75 with SMTP id d9443c01a7336-245e04fe6d0mr34958435ad.55.1755627839479; Tue, 19 Aug 2025 11:23: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 , 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 15/35] tcg/wasm: Add load and store instructions Date: Wed, 20 Aug 2025 03:21:44 +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: 1755628102393124100 Content-Type: text/plain; charset="utf-8" This commit implements load and store operations using Wasm's memory instructions. Since Wasm load and store instructions don't support negative offsets, address calculations are performed separately before the memory access. When Emscripten's -sMEMORY64=3D2 is enabled, the address size must be 32bits. So this commit updates the build tools to propagate this flag to the C code via the WASM64_MEMORY64_2 macro. In this case, the emitted code casts pointers to 32bit before memory oprations. Additionally, the declaration of "--wasm64-32bit-address-limit" flag has been moved from the configure script to meson.build. So the flag name is updated to "--enable-wasm64-32bit-address-limit" to follow Meson's naming conventions. Signed-off-by: Kohei Tokunaga --- .gitlab-ci.d/buildtest.yml | 2 +- configure | 8 ++- meson.build | 4 ++ meson_options.txt | 3 ++ scripts/meson-buildoptions.sh | 5 ++ tcg/wasm/tcg-target.c.inc | 95 +++++++++++++++++++++++++++++++++++ 6 files changed, 111 insertions(+), 6 deletions(-) V1: - Although checkpatch.pl reports an error "line over 90 characters" in scripts/meson-buildoptions.sh, the changes were automatically generated by meson-buildoptions.py and are preserved as-is. diff --git a/.gitlab-ci.d/buildtest.yml b/.gitlab-ci.d/buildtest.yml index 77ae8f8281..a97bb89714 100644 --- a/.gitlab-ci.d/buildtest.yml +++ b/.gitlab-ci.d/buildtest.yml @@ -812,4 +812,4 @@ build-wasm64-32bit: job: wasm64-32bit-emsdk-cross-container variables: IMAGE: emsdk-wasm64-32bit-cross - CONFIGURE_ARGS: --static --cpu=3Dwasm64 --wasm64-32bit-address-limit -= -disable-tools --enable-debug --enable-tcg-interpreter + CONFIGURE_ARGS: --static --cpu=3Dwasm64 --enable-wasm64-32bit-address-= limit --disable-tools --enable-debug --enable-tcg-interpreter diff --git a/configure b/configure index 0587577da9..da0b97027f 100755 --- a/configure +++ b/configure @@ -243,7 +243,9 @@ for opt do ;; --without-default-features) default_feature=3D"no" ;; - --wasm64-32bit-address-limit) wasm64_memory64=3D"2" + --enable-wasm64-32bit-address-limit) wasm64_memory64=3D"2" + ;; + --disable-wasm64-32bit-address-limit) wasm64_memory64=3D"1" ;; esac done @@ -801,8 +803,6 @@ for opt do ;; --disable-rust) rust=3Ddisabled ;; - --wasm64-32bit-address-limit) - ;; # everything else has the same name in configure and meson --*) meson_option_parse "$opt" "$optarg" ;; @@ -928,8 +928,6 @@ Advanced options (experts only): --disable-containers don't use containers for cross-building --container-engine=3DTYPE which container engine to use [$container_eng= ine] --gdb=3DGDB-path gdb to use for gdbstub tests [$gdb_bin] - --wasm64-32bit-address-limit Restrict wasm64 address space to 32-bit (de= fault - is to use the whole 64-bit range). EOF meson_options_help cat << EOF diff --git a/meson.build b/meson.build index 263a72df61..5fee61a256 100644 --- a/meson.build +++ b/meson.build @@ -393,6 +393,10 @@ elif host_os =3D=3D 'windows' if compiler.get_id() =3D=3D 'clang' and compiler.get_linker_id() !=3D 'l= d.lld' error('On windows, you need to use lld with clang - use msys2 clang64/= clangarm64 env') endif +elif host_os =3D=3D 'emscripten' + if cpu =3D=3D 'wasm64' and get_option('wasm64_32bit_address_limit') + qemu_common_flags +=3D '-DWASM64_MEMORY64_2' + endif endif =20 # Choose instruction set (currently x86-only) diff --git a/meson_options.txt b/meson_options.txt index dd33530750..0d05109b84 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -388,3 +388,6 @@ option('rust', type: 'feature', value: 'disabled', description: 'Rust support') option('strict_rust_lints', type: 'boolean', value: false, description: 'Enable stricter set of Rust warnings') + +option('wasm64_32bit_address_limit', type: 'boolean', value: false, + description: 'Restrict wasm64 address space to 32-bit (default is t= o use the whole 64-bit range).') diff --git a/scripts/meson-buildoptions.sh b/scripts/meson-buildoptions.sh index d559e260ed..18faf9ca30 100644 --- a/scripts/meson-buildoptions.sh +++ b/scripts/meson-buildoptions.sh @@ -56,6 +56,9 @@ meson_options_help() { printf "%s\n" ' dtrace/ftrace/log/nop/simple/s= yslog/ust)' printf "%s\n" ' --enable-tsan enable thread sanitizer' printf "%s\n" ' --enable-ubsan enable undefined behaviour san= itizer' + printf "%s\n" ' --enable-wasm64-32bit-address-limit' + printf "%s\n" ' Restrict wasm64 address space = to 32-bit (default' + printf "%s\n" ' is to use the whole 64-bit ran= ge).' printf "%s\n" ' --firmwarepath=3DVALUES search PATH for firmware fil= es [share/qemu-' printf "%s\n" ' firmware]' printf "%s\n" ' --iasl=3DVALUE Path to ACPI disassembler' @@ -576,6 +579,8 @@ _meson_option_parse() { --disable-vte) printf "%s" -Dvte=3Ddisabled ;; --enable-vvfat) printf "%s" -Dvvfat=3Denabled ;; --disable-vvfat) printf "%s" -Dvvfat=3Ddisabled ;; + --enable-wasm64-32bit-address-limit) printf "%s" -Dwasm64_32bit_addres= s_limit=3Dtrue ;; + --disable-wasm64-32bit-address-limit) printf "%s" -Dwasm64_32bit_addre= ss_limit=3Dfalse ;; --enable-werror) printf "%s" -Dwerror=3Dtrue ;; --disable-werror) printf "%s" -Dwerror=3Dfalse ;; --enable-whpx) printf "%s" -Dwhpx=3Denabled ;; diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index 6220b43f98..c7da6ae055 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -123,6 +123,20 @@ typedef enum { OPC_GLOBAL_GET =3D 0x23, OPC_GLOBAL_SET =3D 0x24, =20 + OPC_I32_LOAD =3D 0x28, + OPC_I64_LOAD =3D 0x29, + OPC_I64_LOAD8_S =3D 0x30, + OPC_I64_LOAD8_U =3D 0x31, + OPC_I64_LOAD16_S =3D 0x32, + OPC_I64_LOAD16_U =3D 0x33, + OPC_I64_LOAD32_S =3D 0x34, + OPC_I64_LOAD32_U =3D 0x35, + OPC_I32_STORE =3D 0x36, + OPC_I64_STORE =3D 0x37, + OPC_I64_STORE8 =3D 0x3c, + OPC_I64_STORE16 =3D 0x3d, + OPC_I64_STORE32 =3D 0x3e, + OPC_I32_CONST =3D 0x41, OPC_I64_CONST =3D 0x42, =20 @@ -148,6 +162,7 @@ typedef enum { OPC_I64_GE_S =3D 0x59, OPC_I64_GE_U =3D 0x5a, =20 + OPC_I32_ADD =3D 0x6a, OPC_I32_SHL =3D 0x74, OPC_I32_SHR_S =3D 0x75, OPC_I32_SHR_U =3D 0x76, @@ -283,6 +298,24 @@ static void tcg_wasm_out_op_not(TCGContext *s) tcg_wasm_out_op(s, OPC_I64_XOR); } =20 +/* + * The size of the offset field of Wasm's load/store instruction defers + * depending on the "-sMEMORY64" flag value: 64bit when "-sMEMORY64=3D1" + * and 32bit when "-sMEMORY64=3D2". + */ +#if defined(WASM64_MEMORY64_2) +typedef uint32_t wasm_ldst_offset_t; +#else +typedef uint64_t wasm_ldst_offset_t; +#endif +static void tcg_wasm_out_op_ldst( + TCGContext *s, WasmInsn instr, uint32_t a, wasm_ldst_offset_t o) +{ + tcg_wasm_out_op(s, instr); + tcg_wasm_out_leb128(s, a); + tcg_wasm_out_leb128(s, (wasm_ldst_offset_t)o); +} + static void tcg_wasm_out_o1_i2( TCGContext *s, WasmInsn opc, TCGReg ret, TCGReg arg1, TCGReg arg2) { @@ -313,6 +346,54 @@ static void tcg_wasm_out_o1_i2_type( } } =20 +/* + * tcg_wasm_out_norm_ptr emits instructions to adjust the 64bit pointer va= lue + * at the top of the stack to satisfy Wasm's memory addressing requirement= s. + */ +static intptr_t tcg_wasm_out_norm_ptr(TCGContext *s, intptr_t offset) +{ +#if defined(WASM64_MEMORY64_2) + /* + * If Emscripten's "-sMEMORY64=3D2" is enabled, + * the address size is limited to 32bit. + */ + tcg_wasm_out_op(s, OPC_I32_WRAP_I64); +#endif + /* + * Wasm's load/store instructions don't support negative value in + * the offset field. So this function calculates the target address + * using the base and the offset and makes the offset field 0. + */ + if (offset < 0) { +#if defined(WASM64_MEMORY64_2) + tcg_wasm_out_op_const(s, OPC_I32_CONST, offset); + tcg_wasm_out_op(s, OPC_I32_ADD); +#else + tcg_wasm_out_op_const(s, OPC_I64_CONST, offset); + tcg_wasm_out_op(s, OPC_I64_ADD); +#endif + offset =3D 0; + } + return offset; +} +static void tcg_wasm_out_ld( + TCGContext *s, WasmInsn opc, TCGReg val, TCGReg base, intptr_t offset) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(base)); + offset =3D tcg_wasm_out_norm_ptr(s, offset); + tcg_wasm_out_op_ldst(s, opc, 0, offset); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(val)); +} + +static void tcg_wasm_out_st( + TCGContext *s, WasmInsn opc, TCGReg val, TCGReg base, intptr_t offset) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(base)); + offset =3D tcg_wasm_out_norm_ptr(s, offset); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(val)); + tcg_wasm_out_op_ldst(s, opc, 0, offset); +} + static const struct { WasmInsn i32; WasmInsn i64; @@ -677,11 +758,14 @@ static void tcg_out_ld(TCGContext *s, TCGType type, T= CGReg val, TCGReg base, intptr_t offset) { TCGOpcode op =3D INDEX_op_ld; + WasmInsn wasm_opc =3D OPC_I64_LOAD; =20 if (TCG_TARGET_REG_BITS =3D=3D 64 && type =3D=3D TCG_TYPE_I32) { op =3D INDEX_op_ld32u; + wasm_opc =3D OPC_I64_LOAD32_U; } tcg_out_ldst(s, op, val, base, offset); + tcg_wasm_out_ld(s, wasm_opc, val, base, offset); } =20 static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg ar= g) @@ -1450,6 +1534,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_ld(s, OPC_I64_LOAD8_U, dest, base, offset); } =20 static const TCGOutOpLoad outop_ld8u =3D { @@ -1461,6 +1546,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_ld(s, OPC_I64_LOAD8_S, dest, base, offset); } =20 static const TCGOutOpLoad outop_ld8s =3D { @@ -1472,6 +1558,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_ld(s, OPC_I64_LOAD16_U, dest, base, offset); } =20 static const TCGOutOpLoad outop_ld16u =3D { @@ -1483,6 +1570,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_ld(s, OPC_I64_LOAD16_S, dest, base, offset); } =20 static const TCGOutOpLoad outop_ld16s =3D { @@ -1495,6 +1583,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_ld(s, OPC_I64_LOAD32_U, dest, base, offset); } =20 static const TCGOutOpLoad outop_ld32u =3D { @@ -1506,6 +1595,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_ld(s, OPC_I64_LOAD32_S, dest, base, offset); } =20 static const TCGOutOpLoad outop_ld32s =3D { @@ -1518,6 +1608,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_st(s, OPC_I64_STORE8, data, base, offset); } =20 static const TCGOutOpStore outop_st8 =3D { @@ -1529,6 +1620,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_st(s, OPC_I64_STORE16, data, base, offset); } =20 static const TCGOutOpStore outop_st16 =3D { @@ -1575,11 +1667,14 @@ static void tcg_out_st(TCGContext *s, TCGType type,= TCGReg val, TCGReg base, intptr_t offset) { TCGOpcode op =3D INDEX_op_st; + WasmInsn wasm_opc =3D OPC_I64_STORE; =20 if (TCG_TARGET_REG_BITS =3D=3D 64 && type =3D=3D TCG_TYPE_I32) { op =3D INDEX_op_st32; + wasm_opc =3D OPC_I64_STORE32; } tcg_out_ldst(s, op, val, base, offset); + tcg_wasm_out_st(s, wasm_opc, val, base, offset); } =20 static inline bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val, --=20 2.43.0 From nobody Sat Nov 15 05:21:41 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=1755627918; cv=none; d=zohomail.com; s=zohoarc; b=CdGU0+qSb06j6eRhz79GWYPw2xJUehtDb7M4mhuitJX5OxrUHzR2Vo62s4RK5mPmv1N3iWuIZOXJfqt2tQ9/s6474sooPSSUHzq6XYW941P7S/Vrru72kEcXOR8geKS0rgJckiNNaWuRc+3uIvjJUSgmOz1DMQ8nkXFfTMM7mbM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755627918; 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=Lk2Yx8bK7zSWcPnckyiRTc9Ulm/rb4Y7KbbWAMUJAus=; b=L5SVW7GEc6F/H8Q8zu7wE65xonL6GzzoSBNA36ueF6gEK75jHLjqaCr+969AnKouwVPAXFEqDJJPM+7vTvEdEDKFG8rKyVmdF7q4Et1SFF4P4ajlmCDnH5/kjVtut5pjfEWFUTaZ6Li0+EQ5UPmQ+w4xbnBuMIfSs1yQFlpFAmU= 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 1755627918180453.9469154065008; Tue, 19 Aug 2025 11:25:18 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoR0I-0006LE-My; Tue, 19 Aug 2025 14:24:14 -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 1uoR0G-0006D4-1b; Tue, 19 Aug 2025 14:24:12 -0400 Received: from mail-pf1-x42d.google.com ([2607:f8b0:4864:20::42d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uoR0C-0003uU-Tv; Tue, 19 Aug 2025 14:24:11 -0400 Received: by mail-pf1-x42d.google.com with SMTP id d2e1a72fcca58-76e2e88c6a6so5244847b3a.1; Tue, 19 Aug 2025 11:24:06 -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.23.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:24:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627845; x=1756232645; 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=Lk2Yx8bK7zSWcPnckyiRTc9Ulm/rb4Y7KbbWAMUJAus=; b=FF72qoAS0qTksH4THuaAqGFKnYM62anplzRCUWpp2VXp8NQPWUpkIdBTsCez4LujTY HsV9AR2Pr7k4qNtg1LlwcWBlHHgFP+qpoQn8CzGjJY6l5llPDJKTAItVl56g0Ope2ZLk ikdFtxNZrpi8tyGficB0jB8myZWCCP4bWBxUgWrfmAaLwGdm8RDMkkxynfiwpwOAukkZ 0dUHWBhxuW5FWSil40YXpYAapzGAeWHLimmSFMaqJY9U65Fdc5m75/xi5h5k4lqbyIUj JZnTIbplNiOyt2XSqo2pX71zV2GbYkKZVicVMxoIgOlcFcrrF6E9jIfDIV6MEkxcPy+x Epog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627845; x=1756232645; 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=Lk2Yx8bK7zSWcPnckyiRTc9Ulm/rb4Y7KbbWAMUJAus=; b=jrEVLNC9J8GdWuizJb0C6sq9DjoF2h0v9IxwnBTEDWOMLskyRub527FLCRFAIjSgP/ BKNXy62BuaJlS/OXiDo07T1Asmm5pwFD1cCxMMq80vUeJ+jXlUdnjP5gU2sh5n5UaSxP oi2Ilck0BKFJZz+uWo9D4jb4r71qBEiEp3JxahbYxPSsQje/yEQV9MFv5NxlDNJP4Oq6 St0uhW8fEnGWP7hBFHit9SyyrT3WTiDSIBn9dgu02y5QxoeLV6LTpJE0DXQClTMlvhzQ HnpRnvDutDSbtyi+pYBOhmSXtvQ53kAYE8KFJqjXBHOGZE8Bm/QmWlUjhaqP0NNH18XJ kGYw== X-Forwarded-Encrypted: i=1; AJvYcCVvzjdK32PVqI8Ky30BNef/G1I9Mj+rFI4cH4CG/g1q3/oQLf6cry+DRKcZWy0fUnQdDRZWH6g4j+7W0g==@nongnu.org, AJvYcCWxUU1VpMru2eZUWj94kef9hMU8lchu1yPKKW/upHrmV5rX6s0FeorFEwZVls2EhmDId+oUyTifew==@nongnu.org X-Gm-Message-State: AOJu0YwHMdWMdeZlMcZERHs4pzNPUODt3hlRkcXGGjq/A9Fa7XfEYjoj B+jteSLr3ZMLc0qtlQWf5bHERZh8CuNBIr5ykT7WuaAbF+zIepk3jGAWos6NCH6R X-Gm-Gg: ASbGncsUx/TEEyoE4hKJUeYUEMTU4EIxe7ANdX6WSt2Z8d/MvzGDT8NBttGaxCXblj3 gvpdXj+vv7jtcZlKKJcFfnfeWdFfcRIglRmlnpWpt+J7L5tkN41FkfR0EUO1HZp8ZU7+ZKCKrNg nlrr87fu3Fv5GsQfY0H7cOAWtrYSDoIa6GyS2gTbTGfETACpHXC3rLXhG22ejmELnW9AcHL6zjo VuqiXpainbB5nxWMifD3h/fu9cIz4caxme3g75dPwh6k4/ZHUz1ARv1hbRUc32LZ0/KKaXHeZVi RQOk96w8ZAOVwwM1dcke0ty9gP2nSJByK5FjM7sgqRwqbWB/v/BsZG3b+yN12jTnOOZkDZQE6Z5 Yc73P+62kPpJuIHQhPpclnm5aCicBReIwVS7SmvCACAk= X-Google-Smtp-Source: AGHT+IFo0O9d6lUvgk6SML1wwE+NBqP30D9IhObZ+SO91sE8A93CRPYQIshu9HJQmCe7WqNtxOBqcA== X-Received: by 2002:a17:902:cecd:b0:242:a1ee:6c3f with SMTP id d9443c01a7336-245e02db248mr42235245ad.4.1755627845057; Tue, 19 Aug 2025 11:24: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 , 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 16/35] tcg/wasm: Add mov/movi instructions Date: Wed, 20 Aug 2025 03:21:45 +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::42d; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pf1-x42d.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: 1755627921174124100 Content-Type: text/plain; charset="utf-8" The tcg_out_mov and tcg_out_movi functions were used by several other functions and intended to emit TCI code. So they have been renamed to tcg_tci_out_mov and tcg_tci_out_movi. Signed-off-by: Kohei Tokunaga --- tcg/wasm/tcg-target.c.inc | 85 +++++++++++++++++++++++++++------------ 1 file changed, 60 insertions(+), 25 deletions(-) diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index c7da6ae055..ec5c45c69e 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -524,6 +524,28 @@ static void tcg_wasm_out_sextract(TCGContext *s, TCGRe= g dest, TCGReg arg1, tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(dest)); } =20 +static void tcg_wasm_out_mov(TCGContext *s, TCGReg ret, TCGReg arg) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg)); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); +} + +static void tcg_wasm_out_movi(TCGContext *s, TCGType type, + TCGReg ret, tcg_target_long arg) +{ + switch (type) { + case TCG_TYPE_I32: + tcg_wasm_out_op_const(s, OPC_I64_CONST, (int32_t)arg); + break; + case TCG_TYPE_I64: + tcg_wasm_out_op_const(s, OPC_I64_CONST, arg); + break; + default: + g_assert_not_reached(); + } + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); +} + static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, intptr_t value, intptr_t addend) { @@ -735,6 +757,33 @@ static void tcg_out_op_rrrrrc(TCGContext *s, TCGOpcode= op, tcg_out32(s, insn); } =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) { @@ -746,7 +795,7 @@ static void tcg_out_ldst(TCGContext *s, TCGOpcode op, T= CGReg 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; @@ -768,37 +817,23 @@ static void tcg_out_ld(TCGContext *s, TCGType type, T= CGReg val, TCGReg base, tcg_wasm_out_ld(s, wasm_opc, 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, @@ -880,7 +915,7 @@ static void tcg_out_extu_i32_i64(TCGContext *s, TCGReg = rd, TCGReg rs) static void tcg_out_extrl_i64_i32(TCGContext *s, TCGReg rd, TCGReg rs) { tcg_debug_assert(TCG_TARGET_REG_BITS =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 15 05:21:41 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=1755627993; cv=none; d=zohomail.com; s=zohoarc; b=jKlZ7fIv0Uuxg3UkifJ2jZHYrRdgYMT2pHgTvr30Ftvnh7/MoehAdGtQukAbkPAPS7avYVy/al3LobpDQoLeITHcFIuUh22plxuNSFlKCp/V9MWB4ApPsR3LrBzEp6tHfXuEMEQ2mzxTQsbglXBrAgTkUSmslnO/kG/n/XFuNq0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755627993; 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=aldgkhhllwPVwgK3Tprk6u/cv55YnkKoytwWfl/DwwM=; b=PkvNyJ87O1FFtHDuLD3FBO/kHNYvxomzqRb+DTqaEa7Wr2lFTySWbPKJI/BkQpm0sEeRj3tQtwZ1ZfBT/acbCm85BFVjZq0vhCw+Q4Rc5tzg+pWmTIp2twPRE31mXUDJg645Eedcjn45Avos3zgi2NSnOwZ6IoRdCuXubCmJ6XQ= 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 1755627993668875.6386438936718; Tue, 19 Aug 2025 11:26:33 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoR0K-0006W9-Ij; Tue, 19 Aug 2025 14:24:16 -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 1uoR0I-0006Q2-UF; Tue, 19 Aug 2025 14:24:15 -0400 Received: from mail-pl1-x62a.google.com ([2607:f8b0:4864:20::62a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uoR0G-0003vc-UR; Tue, 19 Aug 2025 14:24:14 -0400 Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-244581c3b43so49726705ad.2; Tue, 19 Aug 2025 11:24:11 -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.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:24:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627851; x=1756232651; 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=aldgkhhllwPVwgK3Tprk6u/cv55YnkKoytwWfl/DwwM=; b=Ze/3s5G2jw8X++dIukJNQpandAFl5sJ4rnruos6kpwuc4fKs9n6fneu8tafX7Pxgeg JtubclVjYJWIv80Jn7JZI9xRXrZ1Ka08vJsSz+POIM7PSFNiD/zYTuYpQllaKr3jWUWS iWTG+T/JIWlRqMTONR2V8e7qlDYDmFbM0tCeNPt+N64Fc2Mx/NRVNs6EDkBZLggW8wwU 2Z/OqfOCOaUBluZ6ddDliyzUfEBNUr37MBFNGVYMHv1t2eUwMCRb3EafBzV2W3lRq2pg jcjGqrGy9RO6K5NPHFSdzS59KABvR+TWfJTxlpaXmsTleZzzxJvhHkUQ/gbJe/wC+VM/ OMgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627851; x=1756232651; 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=aldgkhhllwPVwgK3Tprk6u/cv55YnkKoytwWfl/DwwM=; b=wIPuODjnBVFId/9OMis9jgjWkdRQS9j4IF0ZJW2B/lY+9SESLmsQNWuMw7ANwnfe+M NbsMkEZHDRx3lWKmzAEd9uJoAdGuY5vQnT6J3uwWRBt7g+Er9mXSFNzU1NSh9R9lRekF LJiN6xbij/qHRZwV+GVGn0oLlqsRv4eWAKMnjglD1VJQab7APRn6JpIdrtLvXhSsIlSS 3xmQaxIxNAU4A8Lk0tpOvB0q+jtbIOXIrhTciK6N2SbtwZ3A310LjXJG5K/F7rHwiw9Q XmyDM7s6TfCXYwwhzIJe3pnS5Cd/maZwlLWL6GY6/V9zHf8l56jP7thBfLDmOGrA7di9 S7sw== X-Forwarded-Encrypted: i=1; AJvYcCW0Iy2ij0HQU+zS4p3eLnOIfLL2Mk3J8PNU4r4dSIpSdC9xWTVC1d1Xx5HptAKW2jAaRms5Jjb0pw==@nongnu.org, AJvYcCWUyBHW2gYrcoDCIswyRBwur5QQcJI/J530pOaiMy7/C0lu/iCq1+bunFaeQo8KxiqcHpVhZGvgqyOhAg==@nongnu.org X-Gm-Message-State: AOJu0Yybq5NkFxYUkCN8seHyEPvQi8W9FWsaRSEWcQtggfu2yGYXUY5f cIXeBF2u1yLJh2qLm2ODJGE6CZxwJd4ImFSHDSwD44JbRkQGp7C7zzEOiNueFu3e X-Gm-Gg: ASbGncva6TpdzYPSqYSzD/AoRYpETB/rMEWLRlT1ocBtL1AT0UiROSmUA2oZq3ehXJT VdF3FpMZpOcsrS0MQyQ+3Eq/1AAW70c+/wmBiX1yiwreDIvHjPZcZn4Ze9nOFMY61FCdHZGVqeV E1qtN2fuhWWF/EaiE0AAPycJLllHRl5DP600jEIP+hVVE+99NmQzLWITLPDnZCTx3039rW8Jx2L /HsEHSXt8U+xuOKBFay9Oll93UgBXSBWHBxLVb8/bO+l/O+Qz4zMg1uSnF61Nv2BoaBrUgax7d9 x4eF0PJPgU0Lub+y4QJnsIaaEcCwGmj44Cisg7tCbvcuaoxLP8/8twLmUc+7vJdCu7Lyk1Du7r1 RsV4RDXGsvtYe+MyinCaO3dvzaRYnsguq X-Google-Smtp-Source: AGHT+IGy4zper5z8+c6W3jFkay3BSH10qUQUpUebXInUfpPeXXoPgpXhYD8gnsksDJcoHr2U3RR2nw== X-Received: by 2002:a17:903:2f90:b0:243:a5:f1f2 with SMTP id d9443c01a7336-245e04f7574mr50681395ad.56.1755627850615; Tue, 19 Aug 2025 11:24:10 -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 17/35] tcg/wasm: Add ext instructions Date: Wed, 20 Aug 2025 03:21:46 +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::62a; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pl1-x62a.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: 1755627994914124100 Content-Type: text/plain; charset="utf-8" This commit implements the ext operations using Wasm instructions. Signed-off-by: Kohei Tokunaga --- tcg/wasm/tcg-target.c.inc | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index ec5c45c69e..afaea76d5c 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -873,33 +873,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_sextract(s, rd, rs, 0, 8); } =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_extract(s, rd, rs, 0, 8); } =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_sextract(s, rd, rs, 0, 16); } =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_extract(s, 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_tci_out_sextract(s, TCG_TYPE_I64, rd, rs, 0, 32); + tcg_wasm_out_sextract(s, 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_tci_out_extract(s, TCG_TYPE_I64, rd, rs, 0, 32); + tcg_wasm_out_extract(s, rd, rs, 0, 32); } =20 static void tcg_out_exts_i32_i64(TCGContext *s, TCGReg rd, TCGReg rs) @@ -916,6 +922,7 @@ static void tcg_out_extrl_i64_i32(TCGContext *s, TCGReg= rd, TCGReg rs) { tcg_debug_assert(TCG_TARGET_REG_BITS =3D=3D 64); tcg_tci_out_mov(s, TCG_TYPE_I32, rd, rs); + tcg_wasm_out_extract(s, rd, rs, 0, 32); } =20 static bool tcg_out_xchg(TCGContext *s, TCGType type, TCGReg r1, TCGReg r2) @@ -1146,6 +1153,7 @@ static const TCGOutOpBinary outop_eqv =3D { static void tgen_extrh_i64_i32(TCGContext *s, TCGType t, TCGReg a0, TCGReg= a1) { tcg_tci_out_extract(s, TCG_TYPE_I64, a0, a1, 32, 32); + tcg_wasm_out_extract(s, a0, a1, 32, 32); } =20 static const TCGOutOpUnary outop_extrh_i64_i32 =3D { --=20 2.43.0 From nobody Sat Nov 15 05:21:41 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 From nobody Sat Nov 15 05:21:41 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=1755627978; cv=none; d=zohomail.com; s=zohoarc; b=FwJ3eo4ek/inuZ+SbDDKBTBdkVN3zLHas/BagvQkbR2LReZEO/khd95CwNed1qeg96N43FANHX4qu27+EfRmfzVSOEU1+q78t4U3VQPJ3r6PLeqBPV9XbIUz8uYd6Ewj1BgOBkojl5KPQwnnYNsFPoviSWosSRWp/MBmdLxijPs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755627978; 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=/6GK2cxJ5WGy5SduMQnUpkla/rJ/wahqug23mpIUzYg=; b=E6bvRKJy4mdReQ4vLLo+pAC1p0A1TbHNO4dhE/CN8cRh1KHZ4S3+Ren4AdCEzY5aGK8rc5ae3I8czskvPPfpJW80bhyR7O7H6Ng8X7CkEG297B4Lbt64TH2TU2CESdjo3oA6uDeF13y1Lpz9CwIbagWCm+zanhnHtsHsPWDBqq4= 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 175562797762017.640899528770774; Tue, 19 Aug 2025 11:26:17 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoR0g-0007I8-TF; Tue, 19 Aug 2025 14:24: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 1uoR0V-0006zA-FL; Tue, 19 Aug 2025 14:24:29 -0400 Received: from mail-pg1-x52f.google.com ([2607:f8b0:4864:20::52f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uoR0T-0003wm-JJ; Tue, 19 Aug 2025 14:24:27 -0400 Received: by mail-pg1-x52f.google.com with SMTP id 41be03b00d2f7-b471740e488so5268347a12.1; Tue, 19 Aug 2025 11:24:23 -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.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:24:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627862; x=1756232662; 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=/6GK2cxJ5WGy5SduMQnUpkla/rJ/wahqug23mpIUzYg=; b=F4pzXVfLtPXToOgSxi6+YKCgTObB1gJEGZh1iewMxk4G6jyUQV3m0pOXpn+El0Yffz TgQk5IIvI0cssC+Vtj9spoJA5aRNo6q7tSHdTAgG0tqAYSal6hOatHBYedaOp038Z1e9 pVLCktaEyg3gKBNofiay0qpLo2NNG+qsQXzN3kPx+WESwDlh/WQPzvpsYYGyfYfi0C2D XuVuMQs5YXq8saKkim63pDPU5wXQdIXljpWE601Mv1T+Rqv0841agaye05fv4OloSQ4Q Sp5xGLpkZii6hmI6z9/kGkmSu1rK83HBEIcDpLv2VCkYPx4Nn9Fvzsw4zCL7e3eUdp/2 pQFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627862; x=1756232662; 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=/6GK2cxJ5WGy5SduMQnUpkla/rJ/wahqug23mpIUzYg=; b=pwiTCoOmJm6VLl2F9vwgc9F4dNspLsptBERG97uCQJLy2TYZsm86uyDrALjuDW3Hob ZBEmK3k8TH1+G34KGzRAR9CJ/YikNs0UpU8qCRMrYlqqKqe8cGhULNPUJfWg1rhj2LnP f0Jlt1qVSPrPG9mAmgAHIzz0pEtO1F2KtKKVEcJV0mv7fvGWX8vwcP2GFCsGU3EDWTt1 n0rR82t1cMR1psJxIhGgRZx5UvNm0awSUb4u+nignZ514kjqdZnY2MB+Ye9woHcGU2Zf hKXHgENHgFXQS17U0MKyBsvG8l0IKhupH/mwc6OrU2fRM+uMUb81iVu5KlvMtDSlg+ZQ FsvA== X-Forwarded-Encrypted: i=1; AJvYcCX4hF3/tWu94HK37J++HLzWfKMFwWFtQvYzN6cnYG1oVWUjcyiHLyFTV+TGMkSW1OwY2vDnxDfuL6LJFA==@nongnu.org, AJvYcCX9UrzEXycbMut9AeLneltAtyN7AkBrvkkf5AHKU3VLs4kV6jR4JEW6DZWmh1a9aYGS4OXm0OBMiw==@nongnu.org X-Gm-Message-State: AOJu0YzFqTiJn1/7sgf1gm8wYUa/dLnNm0v+pkAz0fcpB1o5S2RLdDcw DWz+lAnztSED2GXqIdBFaZZ4C6aRy7ootZ0EevrJFp0tXmqron+IgYW6nXVjIV1w X-Gm-Gg: ASbGncsv4PLVEHnp1+6rijqMgx9S8qmBa1aJVFcBvtEGHGRsjYBB44CtBe6ulGAxPdQ qSw9q9f26pHr+qx1SLYsxXGHhPRj0WJxECO4yoZbPJFyutZphNBqybbwhPWg3GQ+qbxAs4uoNT/ 7Ku9YhSHgEN6hRfeh1n8iOw1aMAWDMjbUiLGDjpd0NW09mqWdaitsm032G+RGY+njeToVZIcBAO W7xhUZIwHgW82QSSvjcpZk1K2fbm+5mrIdwjOf7YMI9/xBGnYYv0oyjTzOs9BVGCw1V3eRzjgE0 HJ+As1g7zoCwiYz0qY0kWGmvUCQgh+57lOU+VCGlLZseTLDG4bpUratk6hjTp9k+Y9Pixzd5MU8 UvR6he02FGy2CqvqEmgQ3odakLcV9HA2e X-Google-Smtp-Source: AGHT+IHhFMDhPArlNHUwVnNgnDJMV6cpx6htu16N7Wai7AkC8baHBHwa/9yDMsbh2IgWwF5CV/uzMA== X-Received: by 2002:a17:903:244b:b0:238:120:134a with SMTP id d9443c01a7336-245e02d751dmr52105605ad.22.1755627861991; Tue, 19 Aug 2025 11:24: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 , 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 19/35] tcg/wasm: Add rem/div instructions Date: Wed, 20 Aug 2025 03:21:48 +0900 Message-ID: <403bca28fa44371d890187cf5b5c0eb59793e812.1755623522.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::52f; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pg1-x52f.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: 1755627978775116600 Content-Type: text/plain; charset="utf-8" This commit implements rem and div operations using Wasm's rem and div instructions. Signed-off-by: Kohei Tokunaga --- tcg/wasm/tcg-target.c.inc | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index 7f4ec250ff..01ef7d32f3 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -169,6 +169,10 @@ typedef enum { OPC_I64_GE_U =3D 0x5a, =20 OPC_I32_ADD =3D 0x6a, + OPC_I32_DIV_S =3D 0x6d, + OPC_I32_DIV_U =3D 0x6e, + OPC_I32_REM_S =3D 0x6f, + OPC_I32_REM_U =3D 0x70, OPC_I32_AND =3D 0x71, OPC_I32_OR =3D 0x72, OPC_I32_SHL =3D 0x74, @@ -179,6 +183,10 @@ typedef enum { OPC_I64_ADD =3D 0x7c, OPC_I64_SUB =3D 0x7d, OPC_I64_MUL =3D 0x7e, + OPC_I64_DIV_S =3D 0x7f, + OPC_I64_DIV_U =3D 0x80, + OPC_I64_REM_S =3D 0x81, + OPC_I64_REM_U =3D 0x82, OPC_I64_AND =3D 0x83, OPC_I64_OR =3D 0x84, OPC_I64_XOR =3D 0x85, @@ -1223,6 +1231,7 @@ static void tgen_divs(TCGContext *s, TCGType type, ? INDEX_op_tci_divs32 : INDEX_op_divs); tcg_out_op_rrr(s, opc, a0, a1, a2); + tcg_wasm_out_o1_i2_type(s, type, OPC_I32_DIV_S, OPC_I64_DIV_S, a0, a1,= a2); } =20 static const TCGOutOpBinary outop_divs =3D { @@ -1241,6 +1250,7 @@ static void tgen_divu(TCGContext *s, TCGType type, ? INDEX_op_tci_divu32 : INDEX_op_divu); tcg_out_op_rrr(s, opc, a0, a1, a2); + tcg_wasm_out_o1_i2_type(s, type, OPC_I32_DIV_U, OPC_I64_DIV_U, a0, a1,= a2); } =20 static const TCGOutOpBinary outop_divu =3D { @@ -1377,6 +1387,7 @@ static void tgen_rems(TCGContext *s, TCGType type, ? INDEX_op_tci_rems32 : INDEX_op_rems); tcg_out_op_rrr(s, opc, a0, a1, a2); + tcg_wasm_out_o1_i2_type(s, type, OPC_I32_REM_S, OPC_I64_REM_S, a0, a1,= a2); } =20 static const TCGOutOpBinary outop_rems =3D { @@ -1391,6 +1402,7 @@ static void tgen_remu(TCGContext *s, TCGType type, ? INDEX_op_tci_remu32 : INDEX_op_remu); tcg_out_op_rrr(s, opc, a0, a1, a2); + tcg_wasm_out_o1_i2_type(s, type, OPC_I32_REM_U, OPC_I64_REM_U, a0, a1,= a2); } =20 static const TCGOutOpBinary outop_remu =3D { --=20 2.43.0 From nobody Sat Nov 15 05:21:41 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=1755627956; cv=none; d=zohomail.com; s=zohoarc; b=IgtZCh8gOKW0eqbke2/kBrUfXcZ/V9NlueCJxFGriPylFsZ41DBoi3uj8+/XupvdE2w6JVNYXAFYoToa9UudmrHQjiUv+GsMBKdFfQf1e4+rH8iQkXgLUHa8yGeXVQ4/XVNgTR5tkrny7LhkSBTEfC3JUcX2y4ckzFNTzCI3o54= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755627956; 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=/Uxl4hRz+IS3ofUlHMTimcLwfnwpWGHBSdo9rZYe2Aw=; b=IbByV3KPqWzLKNftge8AdaYRFZe41hZwZucofFF0rPmF3bFRQPAvKVuv+Tbi3Ej9YJIq2Xrof1bi1/Mia88i+prZm1yHSVMSUHVXFl6qx8vohEvN/eRxHkwWF6hRvwl++QeRYpif9HT+aMyVa2rhI/x9miGVyW2lRFPhQzsdTy0= 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 1755627956101566.5105094884923; Tue, 19 Aug 2025 11:25:56 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoR0i-0007XE-Nn; Tue, 19 Aug 2025 14:24:40 -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 1uoR0a-0007Bf-Fb; Tue, 19 Aug 2025 14:24:33 -0400 Received: from mail-pg1-x529.google.com ([2607:f8b0:4864:20::529]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uoR0X-0003xb-Sj; Tue, 19 Aug 2025 14:24:31 -0400 Received: by mail-pg1-x529.google.com with SMTP id 41be03b00d2f7-b471754c159so3818632a12.3; Tue, 19 Aug 2025 11:24:28 -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.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:24:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627867; x=1756232667; 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=/Uxl4hRz+IS3ofUlHMTimcLwfnwpWGHBSdo9rZYe2Aw=; b=lA/cDKtvbNT3e+Xfd2A5PlJdZTsOFBIl1KKuALUolntb9M4SnZrYrlpTJuGPWLauA5 6sF4Lh4iBrAVbWL4B/1tacioqma1049MTuevd8ehhWVBdTyAFzgvR0FEs26WMJ7J9fBA twuV2Uk1njTOZhmXYEoMCvW4Eve8Z0jig99nCMimy7q0WmRfTuYVor510F/KXnEt2FfB j8upAYEZegh62ieXgPbjXsWrQNPRCq0poQg9NXqtdjB9GeOrGWvvRht2Cv1Tb73R9vqo I/xG3G6dXAqVwiJO0drEFhXabneRdbr8JE6f3ohGhyjRCoDkosPXjtnF94e72/aCnT9X rB/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627867; x=1756232667; 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=/Uxl4hRz+IS3ofUlHMTimcLwfnwpWGHBSdo9rZYe2Aw=; b=ajQblg1Ku8Y1zKjJw8BZMv2nTNpwP2MTAn/CZm3swSI+n8rgeOu2MeWxvIb/8lEX0V HpAk5vm9ckVwKOT2azRTFdiNeDOCUE0AVNiRtvC1de5J57B3UrvV1FjRUhf7jb/Ra4Ts wSydMWIoKJmr8/QWfGxhhl1fve71sgSHJrJJg24CPS7o2PPHSmNaZWVEjzWifT66goSO 1ON768aURf25IdttUBYjmxVmSHZSg2/gc6jY4LWK1b7AHzxDPlC2ROcG3FRXHpW7Zxkr nP//UsbJ2S5HxkpgYXNIm1Tp7QqLsBxWpdsd2KtrEbN72MIfnci6P0P0FclVwTLQmvXI vPZA== X-Forwarded-Encrypted: i=1; AJvYcCV7Svwv5wjStHe4oPw0NHBJkvu+aPpRda/VPpp805GR1oWLltx2XrYq3AgDAmQqxNNAlOwvjfewAw==@nongnu.org, AJvYcCWslTV2RH+PfjQoY8o69QvR/kqQAXFS4N1aa9JiAo25tG5Lg4DSUm1QQAYOxJvnORASsDQcfysLX/VL5A==@nongnu.org X-Gm-Message-State: AOJu0YwzP54EdgeNtCjh8tapbs/NfE1/A7Uwl0v82pMQTF/+bEamJNR6 iVBLv7rH7wYuyOucBzpbGNA/fALL5956xFSdslzXj5s69Nwuojrp3K+xYWfyPhGJ X-Gm-Gg: ASbGnctJfMH4zgxcnHU24XvSrA1Twbmh6uJtN1zmNTS4gOuN/I00mlczgtIBZdTXp0t JQ5RjEs2lxzdHknD7qF9EVPjrgB9iF8vGYm+m6SVO9M2A5BXQDCgremb0Am4tfYbsk5Jjxigrgp nBhywxH09qqY+4smBLgfpoutWQKkd/7V9yqtXy2O6F4BJ88BuI3RJghZRZyiIX2UUwCUqYKuR2U CEIaOOerhzQgOm3eAObo+p9X7fMQmF+RCaufCVz5bdAXIxQxRDJLDHvF4+XIhBD0r3MpPJJo6Z/ odJmDLHYPHd36UGBPSCc9pIXgPL2uxbyHJXpQp+ieunnu18yiXqn8SYc/Q1lye2EWiLc6Gq6dWz sbQlaq9oxpcZMp3sBC+VmqMrVeGOeSVJk X-Google-Smtp-Source: AGHT+IFptKn6/1+hfiOBYp4Ar7Op0/yESM5nLqJes5p4R9zBhW3KpTmz1veH2B/fTkU4utWReLHnWw== X-Received: by 2002:a17:902:f541:b0:23f:f074:415e with SMTP id d9443c01a7336-245e030fd76mr35962265ad.14.1755627867321; Tue, 19 Aug 2025 11:24: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 , 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 20/35] tcg/wasm: Add andc/orc/eqv/nand/nor instructions Date: Wed, 20 Aug 2025 03:21:49 +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::529; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pg1-x529.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: 1755627956601116600 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/wasm/tcg-target.c.inc | 55 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index 01ef7d32f3..3c0374cd01 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -449,6 +449,56 @@ static void tcg_wasm_out_cond( } } =20 +static void tcg_wasm_out_andc( + TCGContext *s, TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg2)); + tcg_wasm_out_op_not(s); + tcg_wasm_out_op(s, OPC_I64_AND); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); +} + +static void tcg_wasm_out_orc( + TCGContext *s, TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg2)); + tcg_wasm_out_op_not(s); + tcg_wasm_out_op(s, OPC_I64_OR); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); +} + +static void tcg_wasm_out_eqv( + TCGContext *s, TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg2)); + tcg_wasm_out_op(s, OPC_I64_XOR); + tcg_wasm_out_op_not(s); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); +} + +static void tcg_wasm_out_nand( + TCGContext *s, TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg2)); + tcg_wasm_out_op(s, OPC_I64_AND); + tcg_wasm_out_op_not(s); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); +} + +static void tcg_wasm_out_nor( + TCGContext *s, TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg2)); + tcg_wasm_out_op(s, OPC_I64_OR); + tcg_wasm_out_op_not(s); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); +} + static void tcg_wasm_out_setcond(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1, TCGReg arg2, TCGCond cond) { @@ -1177,6 +1227,7 @@ static void tgen_andc(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { tcg_out_op_rrr(s, INDEX_op_andc, a0, a1, a2); + tcg_wasm_out_andc(s, a0, a1, a2); } =20 static const TCGOutOpBinary outop_andc =3D { @@ -1266,6 +1317,7 @@ static void tgen_eqv(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { tcg_out_op_rrr(s, INDEX_op_eqv, a0, a1, a2); + tcg_wasm_out_eqv(s, a0, a1, a2); } =20 static const TCGOutOpBinary outop_eqv =3D { @@ -1339,6 +1391,7 @@ static void tgen_nand(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { tcg_out_op_rrr(s, INDEX_op_nand, a0, a1, a2); + tcg_wasm_out_nand(s, a0, a1, a2); } =20 static const TCGOutOpBinary outop_nand =3D { @@ -1350,6 +1403,7 @@ static void tgen_nor(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { tcg_out_op_rrr(s, INDEX_op_nor, a0, a1, a2); + tcg_wasm_out_nor(s, a0, a1, a2); } =20 static const TCGOutOpBinary outop_nor =3D { @@ -1373,6 +1427,7 @@ static void tgen_orc(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { tcg_out_op_rrr(s, INDEX_op_orc, a0, a1, a2); + tcg_wasm_out_orc(s, a0, a1, a2); } =20 static const TCGOutOpBinary outop_orc =3D { --=20 2.43.0 From nobody Sat Nov 15 05:21:41 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=1755628146; cv=none; d=zohomail.com; s=zohoarc; b=axP4WYpT44W3RccDhfpubWhzcaQJikwzr1EvAld9X2ki5o3ohSktHXm4wSMoJuzzweiCcQJULnme+xqrBvEQAe7YlHr/pDtPoQGVJc4Cuulrb5VFW4XThnk7YCc6YN4LgqJ5NVXZ001dGThRrNlbLgaNMc9/AhgIjv4/sA5aTUA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755628146; 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=i6f7sVT4bGYsPhkhxxnC/Q6xqoXFxbpoICuYkhSX10A=; b=j1EFlZSSOdBc/PX0NojFrQm0qo7VdmzHfL/vdN210lY460A2oKZSBJwfsmEoIDbfTqAheOjNZgs/u+AXZoZd8YyXHhm7hkp1z/B1/YiyNi9lz96uupkoQVfg87CY1mWZBSQrSd6lXkBam0ITUYTH1gJHv/EBPm0dUkYi8EA3X0c= 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 1755628146903237.62675045904473; Tue, 19 Aug 2025 11:29:06 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoR0n-000860-Re; Tue, 19 Aug 2025 14:24:46 -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 1uoR0g-0007HH-1t; Tue, 19 Aug 2025 14:24:38 -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 1uoR0d-0003yG-8Y; Tue, 19 Aug 2025 14:24:37 -0400 Received: by mail-pg1-x52c.google.com with SMTP id 41be03b00d2f7-b471756592cso3862706a12.3; Tue, 19 Aug 2025 11:24:34 -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.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:24:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627873; x=1756232673; 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=i6f7sVT4bGYsPhkhxxnC/Q6xqoXFxbpoICuYkhSX10A=; b=gZ5yFs+tcZky0TEOki1x7JUBNTlj/MzKlh07yY7agJjvCRMcilp/ibeiZzy1KnIRpa /8wswvDqIOpX3vBEF1rDlkxVy1VhpElGIzGug3EMF8lZL1wRJ09+R6SfVHjsAM0fg4iI yEv2camTtcvACMNcuL9VnkPp8wYYN8H6KbUNnfzSjk0VesTHs2MFXL24kg/92lxwmhr6 O5ttXwKWS5C75ESobj50kt9SmV7OZGbRxJvtvtQ1R+2X3iGZh5iZxw8+Yh7yJKiM2ro+ p70LAUkwY4FW2qadWjYDZiXGi4DcR7Y0ii4VjidmWSCSTz80BKy/K/i6jDg5vGL/bZjR oG/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627873; x=1756232673; 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=i6f7sVT4bGYsPhkhxxnC/Q6xqoXFxbpoICuYkhSX10A=; b=teS4X7HWUfeRH4MwkTGmQkqOByvZOgd3CuqFqGTBWbbUhBCXdkrvih0fWgKtEOLBWf dUhcaz7fGCctx5dgMwCBxrrQxDdp4H/vS54qBozWSvJU69bYRjYVnS16b99+qoHgnmje b0oyubTOhDsl+pZPFMD3y+QgyA1M/wLNv4y74uOzalp1OBBZQNWdrSV274sXhriLUxjF ZiW4gWlM43qtqDr6h2nti/N5FMROwn+Sx8oFIDjtbgFzIn9gWBvBY/jcj/gi0nQw5XJl ynk+3WlLlhVW2DhK68lj04XRj0CkFHdm5S/MZsQVMoGLEmp2bI1W+v/qUJuufb6bwccb VUkA== X-Forwarded-Encrypted: i=1; AJvYcCVHTtytr6mYfnsJRCuSAABRNN2EpRKq4a85YrI627KxhuoI1xKlwjC0X112ihDqBPSzWAokNCDA+fTz3w==@nongnu.org, AJvYcCX4WCWtZdAPjB5U1+WJ9lpRx2a96hgkIFfvZAKVhACTkw0PTHXmcZH2xG2HYPBbyTPTHn/S6FbxkQ==@nongnu.org X-Gm-Message-State: AOJu0Yyy0nQNTyTBqxUHn4Drc3t6yeBPidiXi4yPDBTebtJ8hyb8RiV7 Erv3IWYGQSDZhZRDMqUfwBRf89niUJAoqI/bdbZLAzFk7omSQXLTMR+xQsfWvHML X-Gm-Gg: ASbGncsmogQLXU90zelXckRmVEYdwCsKj85z7m8DNH3SS1iELaWRXsS03w4i4WWSptt tztG98RDDahgsqtxkpsclLR/G63p/3io6FkmLzC7jYoQBtSBPS/kyz1z0ilGoPNoScajiFc0ovp fHd+XWv/DusuCZy8chmLpx9U3e3yj3ePslE5PN5ahTJ1sPrxs6pnTQqbcUeJ7388CNeSOFi9g9Q jjMI+ZdczZND6r8XjIiske5EdpUWbSkBD04BnP+SFno3ifz5fib3tB8Zz/ZCBtUKmwqAbZzxliH Ty37lYOs+2Q74k11ArK+FKyfspyXlzgcdMqH/UVZB3HA9nMI3ByHwBLXye6WfV6mieb7jnul5UK FPQyxgwHmYdFRwauL5pPvxplklE/Nqnob X-Google-Smtp-Source: AGHT+IEy/0Agp5IS6C8/NwyH610FoMQVkjWxGkrobBfEBNSM11Dp8E1r/XXFQWWpYkW0NCusONsDQQ== X-Received: by 2002:a17:902:f64d:b0:240:8ce5:f514 with SMTP id d9443c01a7336-245e0470413mr49560815ad.34.1755627872830; Tue, 19 Aug 2025 11:24:32 -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 21/35] tcg/wasm: Add neg/not/ctpop instructions Date: Wed, 20 Aug 2025 03:21:50 +0900 Message-ID: <0eb3445353ddde2f34bac187bf8d3ecf02f106b4.1755623522.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::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: 1755628148805116600 Content-Type: text/plain; charset="utf-8" The Wasm backend implements only TCG_TARGET_REG_BITS=3D64 so the ctpop instruction is generated only for 64bit operations, as declared in cset_ctpop. Therefore, this commit adds only the 64bit version of ctpop implementation. The tgen_neg function was used by several functions and intended to emit TCI code. So it have been renamed to tgen_neg_tci. Signed-off-by: Kohei Tokunaga --- tcg/wasm/tcg-target.c.inc | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index 3c0374cd01..0ba16e8dce 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -180,6 +180,7 @@ typedef enum { OPC_I32_SHR_U =3D 0x76, OPC_I32_ROTR =3D 0x78, =20 + OPC_I64_POPCNT =3D 0x7b, OPC_I64_ADD =3D 0x7c, OPC_I64_SUB =3D 0x7d, OPC_I64_MUL =3D 0x7e, @@ -499,6 +500,29 @@ static void tcg_wasm_out_nor( tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); } =20 +static void tcg_wasm_out_neg(TCGContext *s, TCGReg ret, TCGReg arg) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg)); + tcg_wasm_out_op_not(s); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 1); + tcg_wasm_out_op(s, OPC_I64_ADD); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); +} + +static void tcg_wasm_out_not(TCGContext *s, TCGReg ret, TCGReg arg) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg)); + tcg_wasm_out_op_not(s); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); +} + +static void tcg_wasm_out_ctpop64(TCGContext *s, TCGReg ret, TCGReg arg) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg)); + tcg_wasm_out_op(s, OPC_I64_POPCNT); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); +} + static void tcg_wasm_out_setcond(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1, TCGReg arg2, TCGCond cond) { @@ -1609,6 +1633,7 @@ static const TCGOutOpBinary outop_xor =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_ctpop64(s, a0, a1); } =20 static TCGConstraintSetIndex cset_ctpop(TCGType type, unsigned flags) @@ -1665,9 +1690,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 { @@ -1678,6 +1709,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 { @@ -1710,7 +1742,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 15 05:21:41 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=1755628100; cv=none; d=zohomail.com; s=zohoarc; b=iYkIopHRdLcibv7c5mWx0qp3vkoW1RiA5XuH0ZAnVxEABCwYs7ocrz7/LaruXH3Mnk54PiC7z2+Frxuc2HNYaqdhYUtcTJkeu4IPGm3BnPwGMNz+oIil/WCuE4vdA3hfr/YCHxD0iZsrnQrqYUyuxPH+NMbAs1pAHPjsqeqZkkI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755628100; 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=OWNu5ctXTfQFZ5CgldinQ05FNeoPiocV4Ar7WZw/JeE=; b=Xvd6DtKuAtjE56WT6fsydCZnBnkItyt9FooHWZRt1Vlx3uesjBPQEJMes02X7hbqfGOlG5QY9yjdCwU0WPahBM/KE9m1hJI1FNPBgHt+BCoMC8hl1hFG4VJD6LUTF19rACqg9ZrYHsTpi+McS2GQ3gwNN6rky/pCur1/RECheY4= 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 1755628100216678.6289587752016; Tue, 19 Aug 2025 11:28:20 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoR0n-00085f-QO; Tue, 19 Aug 2025 14:24: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 1uoR0l-0007qe-DS; Tue, 19 Aug 2025 14:24:43 -0400 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uoR0j-0003z8-4P; Tue, 19 Aug 2025 14:24:43 -0400 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-24458272c00so56799635ad.3; Tue, 19 Aug 2025 11:24:39 -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.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:24:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627878; x=1756232678; 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=OWNu5ctXTfQFZ5CgldinQ05FNeoPiocV4Ar7WZw/JeE=; b=SXUKVxoV5xnRBXamlb8454XPJpoFgw7I/p+CYZEWSRAQJ395B8vnZ2quymdnCXqXkC iJ9kzZUhvXGtFPHaS0GHIcriYYR9ti0Z0enQDTJ/WO04igthjZ1XsQEPQrP2XSO3AmgJ Dr56ViAhOyzYH3No8vz91W0UzDByEwgRJOO0IUJ50aeCZEZ/WaSI/grovSGC8J9jIP7g EmHqY+RoOIFOiMg0HnYWYt4j73l78C01+cQINFC+BSY2E1VUq0ziENhi2tFexRqKBTw7 E9HZv0aTy/1UZJ6NhU6VKV0n7uQkguip/G4COxPICfMRTB2QonISxxtO+SAGQseN2tqQ ZPag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627878; x=1756232678; 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=OWNu5ctXTfQFZ5CgldinQ05FNeoPiocV4Ar7WZw/JeE=; b=YE3cQgCrg6xmDU4hKu3kbQ79oSIHLTx+gcciK/8Bm9IJIsHtQqllc4VAaveXX1S/uM 8+g4jfhGP95BZFQVeflhwko6qH9q7PnfNd6ut/cqTnDCQQoNzU8LZb26ME//tBDTQ/PU Q3TZHul+22C314TXpEbrI7teCH+gyrdEhkVHWNFVST5CHkfX4aEQKPDL6fUWRF0L6ZF0 TsM78Oa6kZY/N4GTemM2bN37aUAJYMc6NUhiEJg8zpu/IjIbKVbDVhETECh5TvKoMxBg JwD3w7hG9f52+eggkvH6TtPgmjPWICVaP8Yq8ngHucRWoozDu/FQ2T20sStO23HgpY7d TwWA== X-Forwarded-Encrypted: i=1; AJvYcCUhL3PVHOo/swlegGQbMHGr+WJPDWActPpqv12W1yPHJM8IxcwoZbb6TaJt4++wx/ok3Q//CxQ8zw==@nongnu.org, AJvYcCUqS9hIQdaS5C1IVeQDVhcFB3BpsF5LxN+1QjH71E9KYi2/lbJg8ZgJrCvaJ1IZE7R6LbTvmYneSCqW/A==@nongnu.org X-Gm-Message-State: AOJu0YxAiP4whYdkZLlYGccCzhrfA5SUxLErOoBaHEoCfKCvtzyycSDX WBBJ7LkX61lNdB1DZB0vr+M98hXubyZza0kSrENR8FarZtWgXeOtx/JMAFUVBqN2 X-Gm-Gg: ASbGncvfdcuIeoRm2jzqWrvvkthSYtliSOocV7YgGVQCwItK8+UQ+zCtRng19C+K4W3 rNPIMAYJOqsOAOCirTSWSMSYS4Hh8WC0+DsLVWg5zErA6FPPFAOdvTFwiTd/LlRQ/8eCViCIwjk xHc+jMfGkTDpLrzb+EVV0EeCCN9ygZuCev54QGWCHdc6IBrMAADSuQO0K+Lm0SDs/38cVyxpOEg YrOLx+5CDZOynrRTAdYP2PLrxVTTwMsNXB070gI8/OZzA1oRQbHPcLNq6/QKSisrChK4dhJIs04 9IXgLpW1o5oDanatFYI/YfcbX/VL1ZsaYg1nHM8TumVpFTz03g4TnDYuti1KnT8vMuOK+QPmszW mZpfyd3pgrUmA0agWv0I5HA== X-Google-Smtp-Source: AGHT+IH0be1qXHijSKazkYQXR8wgSHthFWobhAyyLJxEzkcdf0k6n8OsDNtc98oZTaR7o3iSXer8dw== X-Received: by 2002:a17:903:28e:b0:243:926:b1f3 with SMTP id d9443c01a7336-245e030c986mr49966605ad.24.1755627878462; Tue, 19 Aug 2025 11:24: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 , 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 22/35] tcg/wasm: Add rot/clz/ctz instructions Date: Wed, 20 Aug 2025 03:21:51 +0900 Message-ID: <4698352d4cc892db9b31fa2e6e4c225f789149f4.1755623522.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::632; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pl1-x632.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: 1755628102296124100 Content-Type: text/plain; charset="utf-8" This commit implements rot, clz and ctz operations using Wasm instructions. Signed-off-by: Kohei Tokunaga --- tcg/wasm/tcg-target.c.inc | 48 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index 0ba16e8dce..74f3177753 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -146,6 +146,7 @@ typedef enum { OPC_I32_CONST =3D 0x41, OPC_I64_CONST =3D 0x42, =20 + OPC_I32_EQZ =3D 0x45, OPC_I32_EQ =3D 0x46, OPC_I32_NE =3D 0x47, OPC_I32_LT_S =3D 0x48, @@ -157,6 +158,7 @@ typedef enum { OPC_I32_GE_S =3D 0x4e, OPC_I32_GE_U =3D 0x4f, =20 + OPC_I64_EQZ =3D 0x50, OPC_I64_EQ =3D 0x51, OPC_I64_NE =3D 0x52, OPC_I64_LT_S =3D 0x53, @@ -168,6 +170,8 @@ typedef enum { OPC_I64_GE_S =3D 0x59, OPC_I64_GE_U =3D 0x5a, =20 + OPC_I32_CLZ =3D 0x67, + OPC_I32_CTZ =3D 0x68, OPC_I32_ADD =3D 0x6a, OPC_I32_DIV_S =3D 0x6d, OPC_I32_DIV_U =3D 0x6e, @@ -178,8 +182,11 @@ typedef enum { OPC_I32_SHL =3D 0x74, OPC_I32_SHR_S =3D 0x75, OPC_I32_SHR_U =3D 0x76, + OPC_I32_ROTL =3D 0x77, OPC_I32_ROTR =3D 0x78, =20 + OPC_I64_CLZ =3D 0x79, + OPC_I64_CTZ =3D 0x7a, OPC_I64_POPCNT =3D 0x7b, OPC_I64_ADD =3D 0x7c, OPC_I64_SUB =3D 0x7d, @@ -194,6 +201,7 @@ typedef enum { OPC_I64_SHL =3D 0x86, OPC_I64_SHR_S =3D 0x87, OPC_I64_SHR_U =3D 0x88, + OPC_I64_ROTL =3D 0x89, OPC_I64_ROTR =3D 0x8a, =20 OPC_I32_WRAP_I64 =3D 0xa7, @@ -720,6 +728,42 @@ static void tcg_wasm_out_bswap16( tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(dest)); } =20 +static void tcg_wasm_out_cz( + TCGContext *s, TCGType type, WasmInsn opc32, WasmInsn opc64, + TCGReg ret, TCGReg arg1, TCGReg arg2) +{ + switch (type) { + case TCG_TYPE_I32: + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + tcg_wasm_out_op(s, OPC_I32_WRAP_I64); + tcg_wasm_out_op(s, OPC_I32_EQZ); + tcg_wasm_out_op_block(s, OPC_IF, BLOCK_I32); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg2)); + tcg_wasm_out_op(s, OPC_I32_WRAP_I64); + tcg_wasm_out_op(s, OPC_ELSE); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + tcg_wasm_out_op(s, OPC_I32_WRAP_I64); + tcg_wasm_out_op(s, opc32); + tcg_wasm_out_op(s, OPC_END); + tcg_wasm_out_op(s, OPC_I64_EXTEND_I32_U); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); + break; + case TCG_TYPE_I64: + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + tcg_wasm_out_op(s, OPC_I64_EQZ); + tcg_wasm_out_op_block(s, OPC_IF, BLOCK_I64); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg2)); + tcg_wasm_out_op(s, OPC_ELSE); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + tcg_wasm_out_op(s, opc64); + tcg_wasm_out_op(s, OPC_END); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); + break; + default: + g_assert_not_reached(); + } +} + static void tcg_wasm_out_mov(TCGContext *s, TCGReg ret, TCGReg arg) { tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg)); @@ -1266,6 +1310,7 @@ static void tgen_clz(TCGContext *s, TCGType type, ? INDEX_op_tci_clz32 : INDEX_op_clz); tcg_out_op_rrr(s, opc, a0, a1, a2); + tcg_wasm_out_cz(s, type, OPC_I32_CLZ, OPC_I64_CLZ, a0, a1, a2); } =20 static const TCGOutOpBinary outop_clz =3D { @@ -1280,6 +1325,7 @@ static void tgen_ctz(TCGContext *s, TCGType type, ? INDEX_op_tci_ctz32 : INDEX_op_ctz); tcg_out_op_rrr(s, opc, a0, a1, a2); + tcg_wasm_out_cz(s, type, OPC_I32_CTZ, OPC_I64_CTZ, a0, a1, a2); } =20 static const TCGOutOpBinary outop_ctz =3D { @@ -1496,6 +1542,7 @@ static void tgen_rotl(TCGContext *s, TCGType type, ? INDEX_op_tci_rotl32 : INDEX_op_rotl); tcg_out_op_rrr(s, opc, a0, a1, a2); + tcg_wasm_out_o1_i2_type(s, type, OPC_I32_ROTL, OPC_I64_ROTL, a0, a1, a= 2); } =20 static const TCGOutOpBinary outop_rotl =3D { @@ -1510,6 +1557,7 @@ static void tgen_rotr(TCGContext *s, TCGType type, ? INDEX_op_tci_rotr32 : INDEX_op_rotr); tcg_out_op_rrr(s, opc, a0, a1, a2); + tcg_wasm_out_o1_i2_type(s, type, OPC_I32_ROTR, OPC_I64_ROTL, a0, a1, a= 2); } =20 static const TCGOutOpBinary outop_rotr =3D { --=20 2.43.0 From nobody Sat Nov 15 05:21:41 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=1755627969; cv=none; d=zohomail.com; s=zohoarc; b=AmPwc3GmdKzii7Qahmb1jW3ylm2jNR6BqE1x40XwVDThEFhBx6H2ROkHsVjt58wwf+8DnlIKf/1lZWT/ILAtfanx9NzkHbpmcYo88rpfOyPBGGn60mO1yBX86kMeTO4v9NCqZNDvWecU2wR/udD/Q38llWgF0kfTJVQYBn/ABfA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755627969; 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=1XQ5Jo5crue3zHzTvhYx20Kx9ei5eKTIPqnLlwVAWPs=; b=Pp0G7HSeB83r9B0UvjvPGZF+vRsoxphzA+2CDKQDjDJGHKEU8mIVA/ypwW19ArXqLeAP4piYkHhGs3W1NgdK4oYCpN6xGM7dk8Fft4G+8VRUseAiSq40tucO19cYg6u6QFJh39PW1YoE6fQbHkzCGEeikZnuG93S9VswE5gDutk= 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 17556279698661016.0497671529533; Tue, 19 Aug 2025 11:26:09 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoR1J-0001TY-F5; Tue, 19 Aug 2025 14:25:22 -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 1uoR0x-0000mo-Jz; Tue, 19 Aug 2025 14:24:55 -0400 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uoR0p-00042G-Co; Tue, 19 Aug 2025 14:24:55 -0400 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-2445827be70so62692985ad.3; Tue, 19 Aug 2025 11:24:46 -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.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:24:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627885; x=1756232685; 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=1XQ5Jo5crue3zHzTvhYx20Kx9ei5eKTIPqnLlwVAWPs=; b=Afk5xVzqN+lU7dplQz0yFQbKr94rCf6FsFxCH6vpdwFLjfZKv3gMGgDAv5LKK/mb2V +t1kDOy0lHz50PS64qE5poWn33edVS7t+tD9+XmQZoKiB4xWkmn6qB4gN2XlZoFArFf6 LTYgyMp3PrVwqm+GXNo67wJuCrBOP/P3o9b7Ebt79xSLTQt1YsTmOXeQrkK+Z/8JQ4kf DGnc+o/Ny8R0dai0BBOBLyGyFf4TiP1U+uCChyWasPz5jkQjWZr3BbI8tna0ATgZeZsT XDZUu+29H2AiMQ+77JOZeHHL3JMzmWI2cE4YhkZ8kQnrZLIyNUdhtNNtLKI9CegiTweC lqkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627885; x=1756232685; 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=1XQ5Jo5crue3zHzTvhYx20Kx9ei5eKTIPqnLlwVAWPs=; b=N0ZRzK2UsyMr7uXOmABwqi6kC5IrdKWnCfu5Ypz1nx1KLNK9jQCvqrrEUWxQqQRewM GPZHwGjkMd5gSikMIRfa245fDsdbyPcFPbY7TbrKctlwO7XsZ8hJvaibofUM+zXa5Sup I/7dufScozIM7Q1d9XoCiCql6PxGnQTwD2cUo1dv5aZTZ7sR13naC2rwPYr/vTbITGEC iC1xPBofIzQAUVKHDkTPAL6OoN8bL0I/GRsCjzc5kVsf0XhdryI2fmrQ92yf71gHI+WR QW8qrL1cifjs3FomhAeDCCeFbJ4eS+qQUT3Qazfut1EoL1GA8qd13NZ6N4SVSixsgahx JubA== X-Forwarded-Encrypted: i=1; AJvYcCVbn8/7Sh1tonjgwjuVbR+Lau5xOxlDELdb0Tqci90UrHXjGh1GHPijUrURb1QVDaFZFP2mRQQ8lJPY2A==@nongnu.org, AJvYcCWFJ5CuqYuLe8OH6P3C1Gck0RhOWbaBm2Qh6PUNqHj9WGSU2Jb+BncE2RdS5z06draSc84ln35ZNA==@nongnu.org X-Gm-Message-State: AOJu0YyEXVppPzo3ZpFXCkmN3OJIDKafRj7TXyGQoV1VvQTeb6KA5SvS Z8tAW7mS5GBaFZmcQ+dZlz7nSozP/ynmk+KigYO2rW8m1bTVsVvqKNRkaU76c5ig X-Gm-Gg: ASbGncuGEKfpVxueEbIrFI+wBsiIRWw3W8R2lCBDtr/C80uS2IKpV8RpK68RXie8Li8 NvsmRIkfiNrpfJu9l+MrZnITN1xdREnKpNDp7VcgsT3RiYMNDV+XiDbVXQ1nSvxeYpLrsx0M0pe vq8wSd6lwkyGqYP8mesuYx/ulF+UiaWOzph8a78pkGqG+Oq29dyGfxE0tJci6iCKYlvWs0EBxPR 0hrYBLkKCDH9m2p/S87f4C3gx9uCAaKKAntl0f9Qe5WWQ8QH2mzMkG6PSEXBUmvdDJ8O4Yzd/6b zFwoiyrsSui98UkmNLhSgxaryYEw8KXEpOhyS6pDH5eT5hnWqad/OO7mugX1Y5abydGCx7/Uis6 EzxzvlnNpN8AW+lNRd8886JxnLCOteqtu X-Google-Smtp-Source: AGHT+IHppkBEiVRzCvKspSBGJBXnWfMgsr4FxgKwz9oYd5SYS+peW3zywCNd8mthuilECxWqcUwqEA== X-Received: by 2002:a17:903:19ef:b0:244:5cde:d384 with SMTP id d9443c01a7336-245e047bfa5mr50100465ad.31.1755627884309; Tue, 19 Aug 2025 11:24: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 , 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 23/35] tcg/wasm: Add br/brcond instructions Date: Wed, 20 Aug 2025 03:21:52 +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::629; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pl1-x629.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: 1755627972903124100 Content-Type: text/plain; charset="utf-8" Wasm does not support direct jumps to arbitrary code addresses, so br and brcond are implemented using Wasm's control flow instructions. As illustrated in the pseudo-code below, each TB wraps instructions inside a large loop. Each set of codes separated by TCG labels is placed inside an "if" block. Br is implemented by breaking out of the current block and entering the target block: loop if ... code after the first label end if ... code after the second label end ... end Each block is assigned an unique integer ID. The br implementation sets the destination block's ID in BLOCK_IDX Wasm variable and breaks from the current if block. As control flow continues, each if block checks whether the BLOCK_IDX matches its own ID. If so, execution resumes within that block. The tcg_out_tb_start function generates the start of the global loop and the first if block. To properly close these blocks, this commit also introduces a new callback tcg_out_tb_end which emits the "end" instructions for the final if block and the loop. Another new callback tcg_out_label_cb is used to emit block boundaries, specifically the end of the previous block and the if of the next block, at label positions. It also records the mapping between label IDs and block IDs in a LabelInfo list. Since the block ID for a label might not be known when a br instruction is generated, a placeholder is emitted instead. These placeholders are tracked in a BlockPlaceholder list and resolved later using LabelInfo. 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/wasm/tcg-target.c.inc | 161 +++++++++++++++++++++++++++++++ 12 files changed, 278 insertions(+) diff --git a/tcg/aarch64/tcg-target.c.inc b/tcg/aarch64/tcg-target.c.inc index 3b088b7bd9..9323161607 100644 --- a/tcg/aarch64/tcg-target.c.inc +++ b/tcg/aarch64/tcg-target.c.inc @@ -3514,6 +3514,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 836894b16a..bd8428491a 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 088c6c9264..cf8b50e162 100644 --- a/tcg/i386/tcg-target.c.inc +++ b/tcg/i386/tcg-target.c.inc @@ -4759,6 +4759,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 10c69211ac..75f6a97b2b 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 400eafbab4..d1241912ac 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 b8b23d44d5..20cc2594b8 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 31b9f7d87a..63e7438291 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 84a9e73a46..457e568d30 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 5e5c3f1cda..ae695b115b 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 e6f8f9db5c..8b44cd3078 100644 --- a/tcg/tcg.c +++ b/tcg/tcg.c @@ -116,6 +116,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); @@ -187,6 +188,7 @@ static void tcg_out_call(TCGContext *s, const tcg_insn_= unit *target, static TCGReg tcg_target_call_oarg_reg(TCGCallReturnKind kind, int slot); static bool tcg_target_const_match(int64_t val, int ct, TCGType type, TCGCond cond, int vece); +static void tcg_out_label_cb(TCGContext *s, TCGLabel *l); =20 #ifndef CONFIG_USER_ONLY #define guest_base ({ qemu_build_not_reached(); (uintptr_t)0; }) @@ -361,6 +363,7 @@ static void tcg_out_label(TCGContext *s, TCGLabel *l) tcg_debug_assert(!l->has_value); l->has_value =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) @@ -7047,6 +7050,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/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index 74f3177753..a9fad306cb 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -116,14 +116,22 @@ static const uint8_t tcg_target_reg_index[TCG_TARGET_= NB_REGS] =3D { =20 #define REG_IDX(r) tcg_target_reg_index[r] =20 +/* + * Global variable used for storing the current block index + */ +#define BLOCK_IDX 16 + /* Temporary local variables */ #define TMP32_LOCAL_0_IDX 0 #define TMP64_LOCAL_0_IDX 1 =20 typedef enum { + OPC_UNREACHABLE =3D 0x00, + OPC_LOOP =3D 0x03, OPC_IF =3D 0x04, OPC_ELSE =3D 0x05, OPC_END =3D 0x0b, + OPC_BR =3D 0x0c, OPC_LOCAL_GET =3D 0x20, OPC_LOCAL_SET =3D 0x21, OPC_GLOBAL_GET =3D 0x23, @@ -268,6 +276,17 @@ static void linked_buf_out_sleb128(LinkedBuf *p, int64= _t v) } } =20 +static int linked_buf_len(LinkedBuf *p) +{ + int total =3D 0; + LinkedBufEntry *e; + + QSIMPLEQ_FOREACH(e, p, entry) { + total +=3D e->size; + } + return total; +} + /* * wasm code is generataed in the dynamically allocated buffer which * are managed as a linked list. @@ -278,6 +297,10 @@ static void init_sub_buf(void) { QSIMPLEQ_INIT(&sub_buf); } +static int sub_buf_len(void) +{ + return linked_buf_len(&sub_buf); +} static void tcg_wasm_out8(TCGContext *s, uint8_t v) { linked_buf_out8(&sub_buf, v); @@ -786,6 +809,125 @@ static void tcg_wasm_out_movi(TCGContext *s, TCGType = type, tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); } =20 +typedef struct LabelInfo { + int label; + int block; + QSIMPLEQ_ENTRY(LabelInfo) entry; +} LabelInfo; + +static __thread QSIMPLEQ_HEAD(, LabelInfo) label_info; + +static void init_label_info(void) +{ + QSIMPLEQ_INIT(&label_info); +} + +static void add_label(int label, int block) +{ + LabelInfo *e =3D tcg_malloc(sizeof(LabelInfo)); + e->label =3D label; + e->block =3D block; + QSIMPLEQ_INSERT_TAIL(&label_info, e, entry); +} + +typedef struct BlockPlaceholder { + int label; + int pos; + QSIMPLEQ_ENTRY(BlockPlaceholder) entry; +} BlockPlaceholder; + +static __thread QSIMPLEQ_HEAD(, BlockPlaceholder) block_placeholder; +static __thread int64_t cur_block_idx; + +static void init_blocks(void) +{ + QSIMPLEQ_INIT(&block_placeholder); + cur_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; + QSIMPLEQ_INSERT_TAIL(&block_placeholder, e, entry); +} + +static int get_block_of_label(int label) +{ + LabelInfo *e; + QSIMPLEQ_FOREACH(e, &label_info, entry) { + if (e->label =3D=3D label) { + return e->block; + } + } + return -1; +} + +static void tcg_wasm_out_new_block(TCGContext *s) +{ + tcg_wasm_out_op(s, OPC_END); /* close this block */ + + /* next block */ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, BLOCK_IDX); + tcg_wasm_out_op_const(s, OPC_I64_CONST, ++cur_block_idx); + tcg_wasm_out_op(s, OPC_I64_LE_U); + tcg_wasm_out_op_block(s, OPC_IF, BLOCK_NORET); +} + +static void tcg_out_label_cb(TCGContext *s, TCGLabel *l) +{ + add_label(l->id, cur_block_idx + 1); + tcg_wasm_out_new_block(s); +} + +static void tcg_wasm_out_br_to_label(TCGContext *s, TCGLabel *l, bool br_i= f) +{ + int toploop_depth =3D 1; + if (br_if) { + tcg_wasm_out_op_block(s, OPC_IF, BLOCK_NORET); + toploop_depth++; + } + tcg_wasm_out8(s, OPC_I64_CONST); + + add_block_placeholder(l->id, sub_buf_len()); + + tcg_wasm_out8(s, 0x80); /* placeholder for the target block idx */ + tcg_wasm_out8(s, 0x80); + tcg_wasm_out8(s, 0x80); + tcg_wasm_out8(s, 0x80); + tcg_wasm_out8(s, 0x00); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, BLOCK_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_idx(s, OPC_BR, toploop_depth); + } else { + /* + * The label will be generated after this br, + * branch to the end of the current block + */ + tcg_wasm_out_op_idx(s, OPC_BR, toploop_depth - 1); + } + if (br_if) { + tcg_wasm_out_op(s, OPC_END); + } +} + +static void tcg_wasm_out_br(TCGContext *s, TCGLabel *l) +{ + tcg_wasm_out_br_to_label(s, l, false); +} + +static void tcg_wasm_out_brcond(TCGContext *s, TCGType type, + TCGReg arg1, TCGReg arg2, + TCGCond cond, TCGLabel *l) +{ + tcg_wasm_out_cond(s, type, cond, arg1, arg2); + tcg_wasm_out_br_to_label(s, l, true); +} + static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, intptr_t value, intptr_t addend) { @@ -1804,6 +1946,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 { @@ -1835,6 +1978,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, @@ -2037,6 +2181,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_block(s, OPC_LOOP, BLOCK_NORET); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, BLOCK_IDX); + tcg_wasm_out_op(s, OPC_I64_EQZ); + tcg_wasm_out_op_block(s, OPC_IF, BLOCK_NORET); +} + +static int tcg_out_tb_end(TCGContext *s) +{ + tcg_wasm_out_op(s, OPC_END); /* end if */ + tcg_wasm_out_op(s, OPC_END); /* end loop */ + tcg_wasm_out_op(s, OPC_UNREACHABLE); + tcg_wasm_out_op(s, OPC_END); /* end func */ + + return 0; } =20 bool tcg_target_has_memory_bswap(MemOp memop) --=20 2.43.0 From nobody Sat Nov 15 05:21:41 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=1755628000; cv=none; d=zohomail.com; s=zohoarc; b=fuggGd5Yyg0NIc2q9zniKnKKtJOX+qPCec6ovEBaf73+GkYzwhwjGI6YH8Z1sxPEAaNghLJyRiDAzVNl8N80Ib2Uhvkh/Lpab+Fke6iVZMvjMx0xz029iSX6zR3ZQmrkRV/rd068Yicmwq/q+oA5LJlxGd6zjvtMtt4xnLsVAmo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755628000; 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=nHtHKkltqbJxqH57j4a6ITgFlcnXNSgJcWlXt3E5jp8=; b=nu1iwvaEVlu/K70l2nHfgmDHZOWZfeYLGnXzYZd8ByUUBPvelpzAocxs9XEV9wWw5IzrPgaVaXu2P0Ga8vPIqpieh0+OtHHuIq0ll9BBqNVSaW6oJV1VCFKt/MxI4JABdGadT2sv8U60STOpR8J+uavqe9VdFTa2KrHCJmlWmgk= 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 1755628000124346.9533492373863; Tue, 19 Aug 2025 11:26:40 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoR10-00012b-OK; Tue, 19 Aug 2025 14:24: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 1uoR0x-0000oG-OZ; Tue, 19 Aug 2025 14:24:56 -0400 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uoR0v-00044s-K2; Tue, 19 Aug 2025 14:24:55 -0400 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-2449978aceaso12733665ad.2; Tue, 19 Aug 2025 11:24:52 -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.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:24:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627890; x=1756232690; 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=nHtHKkltqbJxqH57j4a6ITgFlcnXNSgJcWlXt3E5jp8=; b=ei9hBZGb1nhJPtXYYk8BXS4Npt+AaoMK0d9Qk28YDd9w1vFmGmWF7JUUfNw8QuvIrW V4AzPlyPx7IWrbhpUW4dsJFKy4O+fwVVg/GJbaTTA1YkWB7DWbK6mGIKiTUg0AOfFh/k +fro+C5uFb90JjtUW99jH7MfS7joDybt8qjoyJX4pY03SIuywOz78e+ns1BzctbEX0qs wCSNdt2FvnA8fhUdo5Urj90FKUYIYMsOl/RCbwqn024mtYtIQG10AtCqUZo3N3HDV0a2 8JDnB22qMRj9NaH9G1YwWrTBJjBi/kAy0SqOFOMlOqAInnd/7kS4Ekj/98l0TV+QqRvW dsWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627890; x=1756232690; 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=nHtHKkltqbJxqH57j4a6ITgFlcnXNSgJcWlXt3E5jp8=; b=oouwQw1UXY93wzD81JEC697i+f2ZgJitifumOUDy2rbuC/q81rdvMgpqROKcapxY6/ sAzWtrOv+3f5FsD4sJs+x5wwoGnRPs3fZaBE3txem62qrrs6dIhLc2CWwvg/pg82PNYz HS5ZoPnzdXjgTwPtH/j2Xe9Zonj+Xiy4oqiia4ColUNFbPCM/8uKXyl1zUJRCfQu4kri vW2g2d5qyLPcYkg+73XGGc9rGFTuJsNNocOb/Yzr5TRnQPBYryU9iwboG8cLr8QX+ELv 6vMlq9/2XOIjj0Eu+tj9hTvf+MQq/1OawWXJ1vPrJGilax+1igtWXQHG9wM8ZXoh1EhD Tz4A== X-Forwarded-Encrypted: i=1; AJvYcCVIDEIsBrP0eVpUu0tsjUmv0iaQ4C9kP9EymfqUHgy/mzM6uYd9ioVxA2sRuc1o4imzoDzxV5zN2/FfDw==@nongnu.org, AJvYcCXGFsleAr5Wm0YebJ/MyPXMiGn+5AfmNc20W+jLdVz8AAqBPQyxBWBelu0p1K8SS89r81P+W5UMYQ==@nongnu.org X-Gm-Message-State: AOJu0YxMQiB8j++uQiPlT6mRUJVS1fBBn0wGzkDCJ4xL6K4/oiBzl4Yy 33/mgO1WtXqoWIJYaAYEzi1G29N+a002C6W0aAj6UPLOuPheQsJWE4YVz+/08brF X-Gm-Gg: ASbGncuRSnVG8Z+4MaemRhFgnASCrd5MhYVjE5JMn84R4HAGxUpnv8IIvcC9EbgLeWc N4pohzZB/L11a9MWwal6cV+hzf+YpKTftzoLBzUp/lYgZwvmHnb37ie2VhtseXcIWfW6pK11Hy2 hMoKUqmQkxgz3TsdU1U+rGbL3M09dINWs4GWcZh85HgVtJEOeQ4Jh+KRLh7kOaDMIQ4KTDZz+Zq dis8eX5wi9H8Jms1B9jxGi3JnjsJseJYztv/MhX4Ik3JtezO/lbjBYW+8RMnBdczATLXvIvqsF0 esokRTvEnXUf2XWK4wReQyOBZeLuhrOq6hPJwf449XEMWZnzcweOrk5nIjq5N8jLahypODmJhPA QCFS2cizt6uaDZoMiZLu04A== X-Google-Smtp-Source: AGHT+IHVFObN2OZfxz/17D56FXqeV+VEOhsEqc+70KK/t31LD8HxRla2+m00umgKw+aUD82qxKYoPw== X-Received: by 2002:a17:902:c952:b0:244:9b69:c920 with SMTP id d9443c01a7336-245e0307e4dmr41772335ad.14.1755627889862; Tue, 19 Aug 2025 11:24: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 , 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 24/35] tcg/wasm: Add exit_tb/goto_tb/goto_ptr instructions Date: Wed, 20 Aug 2025 03:21:53 +0900 Message-ID: <10976da800d72ebd0a91b4898ac40831f85fa827.1755623522.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::629; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pl1-x629.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: 1755628001240116600 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. 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/wasm.h | 17 ++++++++ tcg/wasm/tcg-target.c.inc | 89 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 tcg/wasm.h diff --git a/MAINTAINERS b/MAINTAINERS index 217bf2066c..d528b9ec90 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -4004,6 +4004,7 @@ M: Kohei Tokunaga S: Maintained F: tcg/wasm/ F: tcg/wasm.c +F: tcg/wasm.h =20 Block drivers ------------- diff --git a/tcg/wasm.h b/tcg/wasm.h new file mode 100644 index 0000000000..bd12f1039b --- /dev/null +++ b/tcg/wasm.h @@ -0,0 +1,17 @@ +/* + * SPDX-License-Identifier: GPL-2.0-or-later + */ +#ifndef TCG_WASM_H +#define TCG_WASM_H + +/* + * WasmContext is a data shared among QEMU and wasm modules. + */ +struct WasmContext { + /* + * Pointer to the TB to be executed. + */ + void *tb_ptr; +}; + +#endif diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index a9fad306cb..c907a18d9e 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -26,6 +26,7 @@ */ =20 #include "qemu/queue.h" +#include "../wasm.h" =20 /* Used for function call generation. */ #define TCG_TARGET_CALL_STACK_OFFSET 0 @@ -121,9 +122,14 @@ static const uint8_t tcg_target_reg_index[TCG_TARGET_N= B_REGS] =3D { */ #define BLOCK_IDX 16 =20 +/* + * pointer to WasmContext + */ +#define CTX_IDX 0 + /* Temporary local variables */ -#define TMP32_LOCAL_0_IDX 0 -#define TMP64_LOCAL_0_IDX 1 +#define TMP32_LOCAL_0_IDX 1 +#define TMP64_LOCAL_0_IDX 2 =20 typedef enum { OPC_UNREACHABLE =3D 0x00, @@ -132,6 +138,7 @@ typedef enum { OPC_ELSE =3D 0x05, OPC_END =3D 0x0b, OPC_BR =3D 0x0c, + OPC_RETURN =3D 0x0f, OPC_LOCAL_GET =3D 0x20, OPC_LOCAL_SET =3D 0x21, OPC_GLOBAL_GET =3D 0x23, @@ -928,6 +935,81 @@ static void tcg_wasm_out_brcond(TCGContext *s, TCGType= type, tcg_wasm_out_br_to_label(s, l, true); } =20 +#define CTX_OFFSET(f) offsetof(struct WasmContext, f) + +static intptr_t tcg_wasm_out_get_ctx(TCGContext *s, intptr_t off) +{ + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, CTX_IDX); + return tcg_wasm_out_norm_ptr(s, off); +} + +static void tcg_wasm_out_exit_tb(TCGContext *s, uintptr_t arg) +{ + intptr_t ofs; + + /* Store ctx.tb_ptr =3D 0 which indicates there is no next TB */ + ofs =3D tcg_wasm_out_get_ctx(s, CTX_OFFSET(tb_ptr)); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 0); + tcg_wasm_out_op_ldst(s, OPC_I64_STORE, 0, ofs); + + /* Return the control to the caller */ + tcg_wasm_out_op_const(s, OPC_I64_CONST, arg); + tcg_wasm_out_op(s, OPC_RETURN); +} + +static void tcg_wasm_out_goto(TCGContext *s, TCGReg target, int block_dept= h) +{ + intptr_t ofs; + + /* Check if the target TB is the same as the current TB */ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(target)); + ofs =3D tcg_wasm_out_get_ctx(s, CTX_OFFSET(tb_ptr)); + tcg_wasm_out_op_ldst(s, OPC_I64_LOAD, 0, ofs); + tcg_wasm_out_op(s, OPC_I64_EQ); + + /* + * If the target TB is the same as the current TB, no need to return t= o the + * caller. Just branch to the top of the current TB. + */ + tcg_wasm_out_op_block(s, OPC_IF, BLOCK_NORET); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 0); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, BLOCK_IDX); + tcg_wasm_out_op_idx(s, OPC_BR, block_depth); /* br to the top of loop = */ + tcg_wasm_out_op(s, OPC_END); + + /* Store the target TB to ctx.tb_ptr and return */ + ofs =3D tcg_wasm_out_get_ctx(s, CTX_OFFSET(tb_ptr)); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(target)); + tcg_wasm_out_op_ldst(s, OPC_I64_STORE, 0, ofs); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 0); + tcg_wasm_out_op(s, OPC_RETURN); +} + +static void tcg_wasm_out_goto_ptr(TCGContext *s, TCGReg arg) +{ + tcg_wasm_out_goto(s, arg, 2); +} + +static void tcg_wasm_out_goto_tb( + TCGContext *s, int which, uintptr_t cur_reset_ptr) +{ + intptr_t ofs; + + /* Set the target TB in the tmp variable. */ + tcg_wasm_out_op_const(s, OPC_I64_CONST, get_jmp_target_addr(s, which)); + ofs =3D tcg_wasm_out_norm_ptr(s, 0); + tcg_wasm_out_op_ldst(s, OPC_I64_LOAD, 0, ofs); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(TCG_REG_TMP)); + + /* Goto the target TB if it's registered. */ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(TCG_REG_TMP)); + tcg_wasm_out_op_const(s, OPC_I64_CONST, cur_reset_ptr); + tcg_wasm_out_op(s, OPC_I64_NE); + tcg_wasm_out_op_block(s, OPC_IF, BLOCK_NORET); + tcg_wasm_out_goto(s, TCG_REG_TMP, 3); + tcg_wasm_out_op(s, OPC_END); +} + static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, intptr_t value, intptr_t addend) { @@ -1343,6 +1425,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) @@ -1350,11 +1433,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, (intptr_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 15 05:21:41 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=1755628329; cv=none; d=zohomail.com; s=zohoarc; b=fo4zHhGEJpKL5+fnoCbSJ/KNVblCtS/ogphbbR7W86XtSMOt0B1xZ6UM2ohIfwVxsceU/zPGYyyLI79wBRsGrO1niyWKLrLlHzSzJxKFkvjBTXnwcShUrYRHe57rANLSufpJEtxALPERN4kEzmxxwnatMhBV04ubDZq3BumqXKo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755628329; 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=NtkykzeH9qSOmbsg3y5wTXrba4XSdnuJVlbAm8R2kzI=; b=Iw1R7bOzgkyMLYAadCQ/P7wGjOyLeojhU1l8e18InE+B6XJYASgWiJI0izmm9lVNaC6NqB65My4RasKHfMPQxM7rOECtN7BOVplkHsjBTiUxF8wRzgmU7ZPtjdYh1xuFUn0/qXcNu/1ShjSRQ5/gniYPR76OmGi0Hs7DN0fOPx4= 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 17556283290201008.9763961151472; Tue, 19 Aug 2025 11:32:09 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoR1M-0001V9-Gx; Tue, 19 Aug 2025 14:25:24 -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 1uoR12-0001Ds-JI; Tue, 19 Aug 2025 14:25:04 -0400 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uoR10-00047B-6v; Tue, 19 Aug 2025 14:25:00 -0400 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-244581caca6so43636935ad.2; Tue, 19 Aug 2025 11:24:56 -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.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:24:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627895; x=1756232695; 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=NtkykzeH9qSOmbsg3y5wTXrba4XSdnuJVlbAm8R2kzI=; b=c+YzA6+QduWhhgdHNM69JsjOqFu3ewznMNULBp9H4NpsE8Gumm1oBJ4fHc/bQO/EgL NlKwSWnqCTCDNuT55dPTem8edkL0krtd6XgWwy5VTQwG69vKAU1ISI9yPp8qCBSoiABj QRqClQF4ceAchctFzN8ElQwsgHYP+A18fd+PWCJ8KNv6bcXTRaIZLetJCRK8r9I6bY1Z 25Fx4yfMOYCN3z1ib9ytqXa85nuE02jgFR2/p/9wj/rHEvSxHNL877i3Djr9VfxzvTO/ +94xUQkFKq/FnTfM834yc+1d5G3oqPNwJWxLjdU1i0rIjL1UdPs8uAhyvQUT2ghMUkzr HMUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627895; x=1756232695; 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=NtkykzeH9qSOmbsg3y5wTXrba4XSdnuJVlbAm8R2kzI=; b=sWk4RpE58bm7ElhoM57se9Pk5GJfImEG6waqq660YCbgAmbQXtcIZD+H2ohFz0GtqL w3+oMh5PgU136/xNFt25V67i3sawn0PvUn2wW1HPcEl8S3i326b8H9kL3a6LNfB0tM3a E+R1rvnOfCo6SsCQKt4MFzpV++Vr8EE1m6UwsSSonfNziqjOcVk5/uz4kLNCozwiSpnw sV2TP5bcU5oxPeLRYOD+MumIzMUcn83VWOgCQzjRBnQyv/nPlDsP3B+mk45yZPeGPQgG aE01VSKE+sllOi7bV0IkgAK1unLXr1X3svKH2lhf85MM/FzbrjNHDBX39+b80RMkDXOh cXLQ== X-Forwarded-Encrypted: i=1; AJvYcCUwgBEu7Py4srdTApDvlXbzTnXtc3v+Zo+05n2vUQA1+gR5c2ZesCYV2G8EyoWpuK3ca29DIzrW6A6zfg==@nongnu.org, AJvYcCVklafB320x2IdmoziXTt3CSAvf3UaFq68YZGsiB66DQ9XaFW/7N9Nk+b7PUilbj+gaZi6Q/fzGxg==@nongnu.org X-Gm-Message-State: AOJu0Yx9u291x2Y9WIfvf5QRIOAw6J9pHJeZdJLx9JsgN9szdF0EKb56 DGP2kIS+QPnv7cZOHSPbIlE2cnlPwyosavxcEWDZV5putmh4bb9jswgtOaqlHQ== X-Gm-Gg: ASbGncuNp9HXFjjZfsWyBInhFINsSngA/BeaO2ctHIHFV9b8TwYk6gIUfKIE/Pv1QXi g8tSlezXJVmAa4L64HTvVy2dPSLFsKfgQTYzN8JKlZCvKQ8aceDhkpX8NWj3HEVMMPi4ZqjEUB+ VdRVGWjcBLBND5g2ICxv0qUCGmcPvzatrpmbk69EU4R5tJEWfQFdHzN18Y3Gnr/+k3o6htKh0hj +YJoz2rLnzuA3kkRrS1+9RzEx3lUviWoIqPjL0SpaWrZDShoxnTaGb2z4gg4tEysH8dnWMvylnn yqzPQxmKp/SF9hGOGOT1axfJUGZU2MF73jn+kvtZCcncjlm/uF2BFc5msWqMhP2x3E3dp2EgpWi 6AehvOSaObV/dlM6vObWvrybC5yCOI5nU X-Google-Smtp-Source: AGHT+IE2n81WeAJI9yVIAZcxR3gmAe13JD+midgBrZ1wm09MfeB1BLWUozNOsvM0q2GQjsT+C1M/ig== X-Received: by 2002:a17:903:2ac5:b0:240:3909:5359 with SMTP id d9443c01a7336-245e04b7bc7mr41833565ad.40.1755627895554; Tue, 19 Aug 2025 11:24: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 , 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 25/35] tcg/wasm: Add call instruction Date: Wed, 20 Aug 2025 03:21:54 +0900 Message-ID: <1c2779b1aa2858a5d3609a00c1b1e8476703f3cb.1755623522.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::632; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pl1-x632.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: 1755628330909116600 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 the ID (starting from 0) to the target function. The mapping between the function pointer and its assigned ID is recorded in a list of HelperInfo. Since Wasm's call instruction requires arguments to be pushed onto the Wasm stack, the backend retrieves the function arguments from TCG's stack array and pushes them to the Wasm stack before the call. After the function returns, the result is retrieved from the Wasm stack and set in the corresponding TCG variable. In the Emscripten build configured with !has_int128_type, a 128bit value is represented by the Int128 struct. Such values are passed to the function via pointer parameters and returned via a prepended pointer argument, as described in [1]. For this prepended buffer area, the module expects a pre-allocated Int128 buffer from the caller via ctx.buf128. Helper functions expect the target of the return instruction via the GETPC macro (the tci_tb_ptr variable in TCI). However, unlike other architectures, Wasm doesn't have a register pointing to the return target. To emulate this behaviour, the Wasm module sets the instruction pointer to the corresponding TCI instruction (s->code_ptr) in tci_tb_ptr passed via the WasmContext. [1] https://github.com/WebAssembly/tool-conventions/blob/060cf4073e46931160= c2e9ecd43177ee1fe93866/BasicCABI.md#function-arguments-and-return-values Signed-off-by: Kohei Tokunaga --- tcg/wasm.h | 10 +++ tcg/wasm/tcg-target.c.inc | 147 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 157 insertions(+) diff --git a/tcg/wasm.h b/tcg/wasm.h index bd12f1039b..fba8b16503 100644 --- a/tcg/wasm.h +++ b/tcg/wasm.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/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index c907a18d9e..d7d4fd4e58 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -131,6 +131,9 @@ static const uint8_t tcg_target_reg_index[TCG_TARGET_NB= _REGS] =3D { #define TMP32_LOCAL_0_IDX 1 #define TMP64_LOCAL_0_IDX 2 =20 +/* Function index */ +#define HELPER_IDX_START 0 /* The first index of helper functions */ + typedef enum { OPC_UNREACHABLE =3D 0x00, OPC_LOOP =3D 0x03, @@ -139,6 +142,7 @@ typedef enum { OPC_END =3D 0x0b, OPC_BR =3D 0x0c, OPC_RETURN =3D 0x0f, + OPC_CALL =3D 0x10, OPC_LOCAL_GET =3D 0x20, OPC_LOCAL_SET =3D 0x21, OPC_GLOBAL_GET =3D 0x23, @@ -1010,6 +1014,147 @@ static void tcg_wasm_out_goto_tb( tcg_wasm_out_op(s, OPC_END); } =20 +static void push_arg_i64(TCGContext *s, int *stack_offset) +{ + intptr_t ofs; + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(TCG_REG_CALL_STACK)); + ofs =3D tcg_wasm_out_norm_ptr(s, *stack_offset); + tcg_wasm_out_op_ldst(s, OPC_I64_LOAD, 0, ofs); + *stack_offset =3D *stack_offset + 8; +} + +static void gen_call(TCGContext *s, + const TCGHelperInfo *info, uint32_t func_idx) +{ + unsigned typemask =3D info->typemask; + int rettype =3D typemask & 7; + int stack_offset =3D 0; + intptr_t ofs; + + if (rettype =3D=3D dh_typecode_i128) { + /* receive 128bit return value via the buffer */ + ofs =3D tcg_wasm_out_get_ctx(s, CTX_OFFSET(buf128)); + tcg_wasm_out_op_ldst(s, OPC_I64_LOAD, 0, ofs); + } + + for (typemask >>=3D 3; typemask; typemask >>=3D 3) { + switch (typemask & 7) { + case dh_typecode_void: + break; + case dh_typecode_i32: + case dh_typecode_s32: + push_arg_i64(s, &stack_offset); + tcg_wasm_out_op(s, OPC_I32_WRAP_I64); + break; + case dh_typecode_i64: + case dh_typecode_s64: + push_arg_i64(s, &stack_offset); + break; + case dh_typecode_i128: + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(TCG_REG_CALL_ST= ACK)); + tcg_wasm_out_op_const(s, OPC_I64_CONST, stack_offset); + tcg_wasm_out_op(s, OPC_I64_ADD); + stack_offset +=3D 16; + break; + case dh_typecode_ptr: + push_arg_i64(s, &stack_offset); + break; + default: + g_assert_not_reached(); + } + } + + tcg_wasm_out_op_idx(s, OPC_CALL, func_idx); + + switch (rettype) { + case dh_typecode_void: + break; + case dh_typecode_i32: + case dh_typecode_s32: + tcg_wasm_out_op(s, OPC_I64_EXTEND_I32_S); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(TCG_REG_R0)); + break; + case dh_typecode_i64: + case dh_typecode_s64: + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(TCG_REG_R0)); + break; + case dh_typecode_i128: + ofs =3D tcg_wasm_out_get_ctx(s, CTX_OFFSET(buf128)); + tcg_wasm_out_op_ldst(s, OPC_I64_LOAD, 0, ofs); + ofs =3D tcg_wasm_out_norm_ptr(s, 0); + tcg_wasm_out_op_ldst(s, OPC_I64_LOAD, 0, ofs); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(TCG_REG_R0)); + ofs =3D tcg_wasm_out_get_ctx(s, CTX_OFFSET(buf128)); + tcg_wasm_out_op_ldst(s, OPC_I64_LOAD, 0, ofs); + ofs =3D tcg_wasm_out_norm_ptr(s, 8); + tcg_wasm_out_op_ldst(s, OPC_I64_LOAD, 0, ofs); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(TCG_REG_R1)); + break; + case dh_typecode_ptr: + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(TCG_REG_R0)); + break; + default: + g_assert_not_reached(); + } +} + +typedef struct HelperInfo { + intptr_t idx_on_qemu; + QSIMPLEQ_ENTRY(HelperInfo) entry; +} HelperInfo; + +static __thread QSIMPLEQ_HEAD(, HelperInfo) helpers; +__thread uint32_t helper_idx; + +static void init_helpers(void) +{ + QSIMPLEQ_INIT(&helpers); + helper_idx =3D HELPER_IDX_START; +} + +static uint32_t register_helper(TCGContext *s, intptr_t helper_idx_on_qemu) +{ + 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; + QSIMPLEQ_INSERT_TAIL(&helpers, e, entry); + + return helper_idx++; +} + +static int64_t get_helper_idx(TCGContext *s, intptr_t helper_idx_on_qemu) +{ + uint32_t idx =3D HELPER_IDX_START; + HelperInfo *e; + + QSIMPLEQ_FOREACH(e, &helpers, entry) { + if (e->idx_on_qemu =3D=3D helper_idx_on_qemu) { + return idx; + } + idx++; + } + return -1; +} + +static void tcg_wasm_out_call(TCGContext *s, intptr_t func, + const TCGHelperInfo *info) +{ + intptr_t ofs; + int64_t func_idx =3D get_helper_idx(s, func); + if (func_idx < 0) { + func_idx =3D register_helper(s, func); + } + + ofs =3D tcg_wasm_out_get_ctx(s, CTX_OFFSET(tci_tb_ptr)); + tcg_wasm_out_op_ldst(s, OPC_I64_LOAD, 0, ofs); + ofs =3D tcg_wasm_out_norm_ptr(s, 0); + tcg_wasm_out_op_const(s, OPC_I64_CONST, (uint64_t)s->code_ptr); + tcg_wasm_out_op_ldst(s, OPC_I64_STORE, 0, ofs); + + gen_call(s, info, func_idx); +} + static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, intptr_t value, intptr_t addend) { @@ -1420,6 +1565,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, (intptr_t)func, info); } =20 static void tcg_out_exit_tb(TCGContext *s, uintptr_t arg) @@ -2268,6 +2414,7 @@ static void tcg_out_tb_start(TCGContext *s) init_sub_buf(); init_blocks(); init_label_info(); + init_helpers(); =20 tcg_wasm_out_op_block(s, OPC_LOOP, BLOCK_NORET); tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, BLOCK_IDX); --=20 2.43.0 From nobody Sat Nov 15 05:21:41 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=1755628146; cv=none; d=zohomail.com; s=zohoarc; b=CvKvWp+QOaUu6Fuvj66hTbw0yTfKTCaBqVdchUS6ijsLD0TlgzfWNdJt99VQdFamWhM6F5KCD7tAeA1fESfkTTucUP6E5GcW9HxWr4eYDb03dAszXfxkYS5duPffUWkXPZmFgj38b1ecewPiGYe0fN83J5Y+91IgOsqJUZxq8eQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755628146; 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=mJ2AmRGKg71BgJxwHycIvBP2PoRXyVHsYPqFMrkrais=; b=Rbto5doPV7qYL9kld2pEqJT1Ro8a25yB6gL0PGxLYnKNykMbud/A619FKRyV5fEhECbW++ZA3ony3shNOkJF6H+E1c4ydN7PsIgSBrv2UF2c56LRqpzqqoqA4TV38EDwIwO0a1k6XbUGgOenvJVcZUVhhLrFhSAN3QJ0vjarF+M= 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 1755628146766128.11851150410394; Tue, 19 Aug 2025 11:29:06 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoR1l-00022X-9i; Tue, 19 Aug 2025 14:25:47 -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 1uoR1B-0001TN-2g; Tue, 19 Aug 2025 14:25:10 -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 1uoR16-00049e-R4; Tue, 19 Aug 2025 14:25:07 -0400 Received: by mail-pg1-x52d.google.com with SMTP id 41be03b00d2f7-b474e8d6d04so1586294a12.0; Tue, 19 Aug 2025 11:25:02 -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.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:25:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627901; x=1756232701; 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=mJ2AmRGKg71BgJxwHycIvBP2PoRXyVHsYPqFMrkrais=; b=QI2brFdqdm8L+BvzgVoo+S+dka6AkBgcQsFmyNDY1och6f63T8Eeg4CI+J3pdqFFbw t4DDcjvwTxu+z19u/xP4nwUDkaeKIJKEtRGyKuDx+e/8yxA0m8tXJ53OgEMLmwRZgbxd XFU1tZItEjzX04Kfl2nBK6dORmSf3Jmof1wliw4IZe+Gd3NNmYwgVXknY4TGQxAbatqi Nj40FQvHFXPhHdTQDgEFcaXBQEyOFKOF0ABC0ZfYyiq5VL1ZZzby/X+fQ8VIWE6mqrLv tZiZkCVeqBZyFJdKD1oS84pyQ6dSMGG21e4DE6OcFA+vTTkM5eZc/90GIXnsF81SUi8r uIJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627901; x=1756232701; 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=mJ2AmRGKg71BgJxwHycIvBP2PoRXyVHsYPqFMrkrais=; b=l4DR0GkKSF/9WMX4wH8s7OIJgcshF140IUCyuzfg1aGCDP62vibloUt0vTq9EonhsU CZCwN3vsapFBI3ks+hJnpJ0omvim5os8n2TMS7gESdPLBdjvK7ewmzTNagKZN92aKqgX Z3Q4jkwuhBVypCL8RlyLx/m9KauFqQ0F1xE3R/zGB6wLXofdkZbg5tzjVW7S6BtzFA46 7DiaC6pAMjmr8R79Coh2Me+R+uY/NEpOzj+Qg4xEJTiwaVbfVt9bfWSAfSbTnQDChc55 sVlNYyfqouHKvw31eVhMNzPTYKLeaYDVJ49zgzavb2QFrVrInrJb0A0rB0bM+z7F2QiQ oKNg== X-Forwarded-Encrypted: i=1; AJvYcCVSa3zsz2o6u7XsNRqjgeInAVqaGhx8if+xLx3G6DPD4+IIxnm6sMvFSZSBhvJI2Y77B3R7e/qiJA==@nongnu.org, AJvYcCVqHj0zKWluqThVPalM7otf3g3uneRI7OmhkQ9o0rYi7H8MiCqErLL4B8tyd6HagJdURISxAZ2HzCCZQg==@nongnu.org X-Gm-Message-State: AOJu0YzstkXe73wCt94GlAt7lDLjOs4paab9YPx9XzjkTvwhWFC3Z6c0 uKQE3+iodo/JTRM4Mb+tfyM3pToBZ40acpEZH0vZ1JWLT2cqo3K5Cf48a7pASA== X-Gm-Gg: ASbGncvCFYXjxeZnUIj3l64Rj9V/sIRSjHtS9XjQf1Xh2vVqyQYPHjPx6EmZK6ZXydb vxTGtfAaGASv3PvnvxHgdGVYzAHsiZMMpEb1zxbgmRkTCbPzmfhDoq428bubPd3MiqQo5KIYjmc KoQXwgkbpx0OzwGnoIcA1eVyFFII5H/qgcljOROquVtnOBMtbj8yskja2E1uUVvUd7RNkznLvfj xTZnTJzRrLTwPb2J8nRjaqCcGS0jHIq+6PxfASc/NAmL2EFV8JiOmDnPmKZacc+bLxsjOK77Ukv ReHTNsQW5dP+tL2lSOPUkmqlWubAYFYtKMWnwEVJyP9FXmGl626FyS3uQrnhAojMf+K41zRabHR O4IHK0OtBkniU/m+tXpjAxA== X-Google-Smtp-Source: AGHT+IEKRG45BU+fO3Sa1lBHip2Ekmy/CpCu0yzlt0rex6zd0LjhamAbNxOkzWjxSEtMr0H2n48Bbg== X-Received: by 2002:a17:903:2346:b0:23e:22dc:665c with SMTP id d9443c01a7336-245e0492439mr52750485ad.33.1755627901085; Tue, 19 Aug 2025 11:25:01 -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 26/35] tcg/wasm: Add qemu_ld/qemu_st instructions Date: Wed, 20 Aug 2025 03:21:55 +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: 1755628148813116600 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/wasm/tcg-target.c.inc | 95 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index d7d4fd4e58..db92463941 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -1155,6 +1155,99 @@ static void tcg_wasm_out_call(TCGContext *s, intptr_= t func, 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) +{ + intptr_t helper_idx; + int64_t func_idx; + + helper_idx =3D (intptr_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); + } + + /* call the target helper */ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(TCG_AREG0)); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(addr_reg)); + tcg_wasm_out_op_const(s, OPC_I32_CONST, oi); + tcg_wasm_out_op_const(s, OPC_I64_CONST, (intptr_t)s->code_ptr); + + tcg_wasm_out_op_idx(s, OPC_CALL, func_idx); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(data_reg)); +} + +static void *qemu_st_helper_ptr(uint32_t oi) +{ + MemOp mop =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) +{ + intptr_t helper_idx; + int64_t func_idx; + MemOp mop =3D get_memop(oi); + + helper_idx =3D (intptr_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); + } + + /* call the target helper */ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(TCG_AREG0)); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(addr_reg)); + switch (mop & MO_SSIZE) { + case MO_UQ: + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(data_reg)); + break; + default: + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(data_reg)); + tcg_wasm_out_op(s, OPC_I32_WRAP_I64); + break; + } + tcg_wasm_out_op_const(s, OPC_I32_CONST, oi); + tcg_wasm_out_op_const(s, OPC_I64_CONST, (intptr_t)s->code_ptr); + + tcg_wasm_out_op_idx(s, OPC_CALL, func_idx); +} + static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, intptr_t value, intptr_t addend) { @@ -2319,6 +2412,7 @@ static void tgen_qemu_ld(TCGContext *s, TCGType type,= TCGReg data, TCGReg addr, MemOpIdx oi) { tcg_out_op_rrm(s, INDEX_op_qemu_ld, data, addr, oi); + tcg_wasm_out_qemu_ld(s, data, addr, oi); } =20 static const TCGOutOpQemuLdSt outop_qemu_ld =3D { @@ -2334,6 +2428,7 @@ static void tgen_qemu_st(TCGContext *s, TCGType type,= TCGReg data, TCGReg addr, MemOpIdx oi) { tcg_out_op_rrm(s, INDEX_op_qemu_st, data, addr, oi); + tcg_wasm_out_qemu_st(s, data, addr, oi); } =20 static const TCGOutOpQemuLdSt outop_qemu_st =3D { --=20 2.43.0 From nobody Sat Nov 15 05:21:41 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=1755628380; cv=none; d=zohomail.com; s=zohoarc; b=PLY4R4el6qHgqlexefCULQNRO78qlEUhQrSRVw6Rs2fAA6+AIpwVIb3fyI+yBgSq6G+YUbcEbN/YBkF0twtydLbtPac+cPpwvn46ST9sf4SX4oA+5plOaRjjW4WpBCyD9YXn+4uoDNlkmEAj1A0eXPSrFTTgABbG8aJxREVgoWk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755628380; 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=38R8q6vawta6VEEn3UhBfaFAfRNcb+fDXcq44g+j6XI=; b=cIAnLrWihNyCLOQRvtdnstMbpPtfXOYrxUVepp7sDT4pQBHKLA4MABGKTwXxRyv3Oid/W8YLPEuKG66GSOf60GCBBkV243LD9ZMF4Oba62e+fCw4SvgnOuGBzu98FYaxIU8rVfUYbf/MujF4Qlfc1Ja+SYSHEZm4GPIHUt0gSBo= 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 1755628380600533.6535886841589; Tue, 19 Aug 2025 11:33:00 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoR1s-0002PF-CA; Tue, 19 Aug 2025 14:25:52 -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 1uoR1F-0001ax-N7; Tue, 19 Aug 2025 14:25:15 -0400 Received: from mail-pg1-x535.google.com ([2607:f8b0:4864:20::535]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uoR1C-0004NT-LM; Tue, 19 Aug 2025 14:25:12 -0400 Received: by mail-pg1-x535.google.com with SMTP id 41be03b00d2f7-b4746fd4793so2094204a12.1; Tue, 19 Aug 2025 11:25:08 -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.25.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:25:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627907; x=1756232707; 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=38R8q6vawta6VEEn3UhBfaFAfRNcb+fDXcq44g+j6XI=; b=ZBvC6rsVgn0EIU70i4l0l2O5X5onSVqxk1umeYVX1Xr1QOjrLEUJYY4wXKpbGudgF5 qz3B1eGJu/NqeCwTrGvb4Juet0dK+MVloq0U1Cyi3wjtQ4Wgsz1fUOz0XigazjGkU2tL GeMiNfb4u6a4fSCRCzASgpQUNpifHxSX+GmXodPMZnUkFWG6HusKNE7NCwuYr7N8ODIv z+kYdbQw1k/SesycRC7TxxsikbPHrdGLPLH+zyl/TCM14L+/pHnifRvGVhJ9+FNbAY9n 3qe6+rzt4ZoBnLBo2lR6U+8Q29nOCXqLYF/5PfM12FuaeEv/mG7KGHL0blOH76QilaZd mC/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627907; x=1756232707; 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=38R8q6vawta6VEEn3UhBfaFAfRNcb+fDXcq44g+j6XI=; b=fK5S+I7+vyYenVn43TLXCRjPJuNfQtR/8WZxX/2Avs+x6EOQNC4eQ8zFjDdYETqrr6 4ecxqt+Jx7a1HNi2knXI7/CSZA8yGzI/9ozzIWxUI1yQbh10YPkMU0UpmJwrdr3aEg7r p/K41B8V9X339qrIfbApKDDx4pigMswZNY/2zaFQKWthA+iYChQFtPncMjNj/eKts7SS rjVFvu13EXdt9rhS3CLDMDAiQj21vtRCUqs0NhFQL9OuZdzzcR5MwVneue1NRA/qOaQL jtvc6pEe/SjH2hK+V3QIYrrzTPLLJ6tw3RnsKA8cXWJEb5RCciMVT5ItnDos1NyJF/z0 9VKw== X-Forwarded-Encrypted: i=1; AJvYcCUTZe2Dc9xzq0EA+k84F4wSNnIXlcQiZ6rzdOnvYNUIlUmkumbDgBxWYxQcE89ja1A2C0DBTBlshg==@nongnu.org, AJvYcCUn7VgG/2aHn94zVI9KjdF3YLYLfETImPrxuUS/BvJ2HCVWwp91mqIM6Q6aQOme48je7vr1LFfrTns9aQ==@nongnu.org X-Gm-Message-State: AOJu0YzmxIyYZ1SNN6yJaw/fW05gTmROyrQTvDi/WiPRYeNVMudimmzO kOb9+39ZMuqPb/1oOAZqmgUlNBQ/qxrZS+ADJRzMVun5WCyG1opsQVVazQjwVg== X-Gm-Gg: ASbGnctAMYU6LDRZ3ng+6hnPepjtGfTWrOMO7JUvp4pRGruyylWoON/4WvYd47CmdCs WON+Vtu8NMghJ0KrtSgA9s5T/6u0N3wIhm08UhMIHhwi4bg1tbdaysN0tdCnUxl9MnOdknHv208 hScY5WgbqjUYYUIfjeIr9dVja3LxhKp7VEjaO9mlEj5Oxj7d8r7NqU7v9Y5JmfGN6jguK2AFuZe h41oICV6aC/wIh62YGM3QbJ12AFllc3bIMtpBB5JvUE78SBCSpwBSYihZKbRJZLzdezS9BuAKXe OVnrohM36c8/EsSsTrp4OyDp/6CTE6dvr2bpoTBlLruOhuAq3WtgJiGQZrHXh92DxbujFyVu5VV AXtyRbHUzlHMWeg7njXNl+tGN1gvm8Yuj X-Google-Smtp-Source: AGHT+IEtRh5Ys+LvLgNyJJXQQ1tJucdVdrOgQY9Sg8hA3J1OXBf2j4KYbPYpxOJiDhkE0ZXTco6CsQ== X-Received: by 2002:a17:903:1984:b0:243:47:f61b with SMTP id d9443c01a7336-245e04b937dmr44393485ad.45.1755627907160; Tue, 19 Aug 2025 11:25:07 -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 27/35] tcg/wasm: Mark unimplemented instructions as C_NotImplemented Date: Wed, 20 Aug 2025 03:21:56 +0900 Message-ID: <26484aa71afe4dc97ec21115b8a6063ecef0d7b5.1755623522.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::535; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pg1-x535.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: 1755628381473116600 Content-Type: text/plain; charset="utf-8" This commit explicitly marks functions which aren't implemented in the Wasm backend as C_NotImplemented. Signed-off-by: Kohei Tokunaga --- tcg/wasm/tcg-target.c.inc | 107 ++++---------------------------------- 1 file changed, 10 insertions(+), 97 deletions(-) diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index db92463941..6b8df4e9d7 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -1430,19 +1430,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 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) @@ -1699,50 +1686,21 @@ static const TCGOutOpBinary outop_add =3D { .out_rrr =3D tgen_add, }; =20 -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, + .base.static_constraint =3D C_NotImplemented, }; =20 -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, + .base.static_constraint =3D C_NotImplemented, }; =20 -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, + .base.static_constraint =3D C_NotImplemented, }; =20 static void tcg_out_set_carry(TCGContext *s) { - tcg_out_op_v(s, INDEX_op_tci_setcarry); + g_assert_not_reached(); } =20 static void tgen_and(TCGContext *s, TCGType type, @@ -1886,37 +1844,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 { @@ -2091,45 +2028,21 @@ static const TCGOutOpSubtract outop_sub =3D { .out_rrr =3D tgen_sub, }; =20 -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, + .base.static_constraint =3D C_NotImplemented, }; =20 -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, + .base.static_constraint =3D C_NotImplemented, }; =20 -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, + .base.static_constraint =3D C_NotImplemented, }; =20 static void tcg_out_set_borrow(TCGContext *s) { - tcg_out_op_v(s, INDEX_op_tci_setcarry); /* borrow =3D=3D carry */ + g_assert_not_reached(); } =20 static void tgen_xor(TCGContext *s, TCGType type, --=20 2.43.0 From nobody Sat Nov 15 05:21:41 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=1755628014; cv=none; d=zohomail.com; s=zohoarc; b=HmsZj1ujDN5qN1UmX+YutgkP7Ss+g0dUytd0P7SsK90OrLs1SSQ2h3nPwlsINkmmBkaacdHuXsaSnR4rVEIoFOIQpp+To14QKh2IT6Ry942NVLAHjnsvIbYDe8wroo8Rtxu6Z+lHERjV6tWbBCWQ17tDF8gh9ZsZtIuPXXCo+Kc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755628014; 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=i6T+1QDRLYXTheY8fOoVhm2NDU4PgwoPKgJ7RKVMPII=; b=Ctz0nZqR6d0AAnJg3VBqO+dFFxNqrRqSKa2Ou4utHPXE44A8ibUPlYZu9adZE6Kel5p3AIFs5vEuh+qsj/a+h7JkbF6xMyJNVT3xnhuTQWKXqlrjtov2Jeyp9Ou3FM3HWq0QS+g54QlbKBn1MivfGRDuHVF1u2r9R2zfTl7J+QE= 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 1755628014751120.62329272998329; Tue, 19 Aug 2025 11:26:54 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoR1z-000382-Q0; Tue, 19 Aug 2025 14:26:00 -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 1uoR1I-0001dY-Jv; Tue, 19 Aug 2025 14:25:20 -0400 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uoR1G-0004QX-Rj; Tue, 19 Aug 2025 14:25:16 -0400 Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-244581cc971so66763425ad.2; Tue, 19 Aug 2025 11:25:13 -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.25.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:25:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627913; x=1756232713; 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=i6T+1QDRLYXTheY8fOoVhm2NDU4PgwoPKgJ7RKVMPII=; b=Zg/PuwNgFdWRD/7iAO/3qscjyKLlIUu1Y3RR1KJYoBRv8uaoppJtdRWGnxll1p0no1 fRP+uRG+X5TbjP60Llmx7EXMwYxgshWX9z1OQDnnbRmZS3H3c1zlTqvZYqvCL/292VK6 s4CXkYtp1izzpTdh54wRCTldeaUGuUDJfV6TaCWukKilJQ90zUJMcBHv9cxDMPvp03Mm +a0nllMUMo1lwO3TIEmBF/w7r3Jd8XYE4dKB2vnLEpigdxHZuAaax0Ozm5zxhwsTT2la HlkanryGzyyGyxyFLr0U5ezXkNXTDfEPjmZIZ9tgHgIaMzaIWQ66QB0chY9Ry0dciewM FKeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627913; x=1756232713; 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=i6T+1QDRLYXTheY8fOoVhm2NDU4PgwoPKgJ7RKVMPII=; b=UQ+hhk/Lj6vBw4PjIQ8E3Uyz5YRxlhS/zo9rXtqjZK50Xv87u5AcxbZu0Mh4w3ixRL TWsT7EC1Y1NYBxWG8M+4phBIFbSOhzUnMWjJQQAp7WnxOaT2qg6ip+1E8JfsLReQiEVR BptPZhSOBaJAZWPLSha3QtxSp7kZo33VkZVTkK7UqXBaxzv8poxhOnEF3qv5As3wivmr KaN3ra3N5uNR8HU5O1jh+hAn+dGYw9Ix9amEZjUNVZSEcQTydWSMLQoiwow3H4JW/ssB pw5oviZyczYa8mMZI72o8YlhSe6E5ECOwOpjkAGa8786dkcNaQ7WL9nBDUg/RDU6B+4j pQAg== X-Forwarded-Encrypted: i=1; AJvYcCUK5xLdR7rw9bqurKzff8Pab0rS0VNPjeJ/1P26cdNS3gYHF7vZBUzzOAQfDuyrwKDw3aU7xcKYRsoeUQ==@nongnu.org, AJvYcCXqiN5P33ciGkDT9I2rE0Fsri+MqBrPYAU04fwvmmycr6Z1fFGqyqpM96eo+OuMJoEAtWX6OuRxRw==@nongnu.org X-Gm-Message-State: AOJu0YzTlQu3AiiDHzuvq9jZ3dD1STRSguZ1ciWk3xhejbVs9U6JYUzs qZTyMbcsQDKj+ULTcLEUSDLYYFMogIgEIrEJ9/EPg6Y/pCqQ3PA6A2IK1udk1g== X-Gm-Gg: ASbGncvj3K1gMWY/JNQgIuBixaeUuLh6n6APve5zDs7IODxdSNLDG9Dz7m5Q6PChDvx 6rgfdzvQyN4vl499DkJ/JPxhq8knd/BCO13VEyzpPXHDecM87De4LMM4I7KZixFF37Hp7SWe1zW rQ9O4NR+//gKtRtdnDRdX00CgkXRm1y1XlhCUO+JPrFBCtbkfq74vxP+mSgnqpXipVKVgwP5+25 GYj+K8SlCeoZv2inbn3bnoQj6s9f31ZYpldnPk4VCUeA5zdCjFJ4X2pP7vd+oFjqAs7WWRERF4R O+YJBlXJfwzaOVh2iwgaOf8dFCrlQzjLmcGopB8sL0I8Ijan25fGYVK+leiwgLLA86LlvLFAiBm ro/rfdMd/BX+1KqGSx03Xng== X-Google-Smtp-Source: AGHT+IF4it6/RPjibYLZoKOzO6MtaQFbtQBalU0FKjcuo3N6I392a8XCOhA6Nk4m/QRx3UV+6X38XQ== X-Received: by 2002:a17:903:fab:b0:240:1f25:d443 with SMTP id d9443c01a7336-245e047f53bmr49652015ad.30.1755627912677; Tue, 19 Aug 2025 11:25: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 , 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 28/35] tcg/wasm: Add initialization of fundamental registers Date: Wed, 20 Aug 2025 03:21:57 +0900 Message-ID: <2e9d7dfd8cc6bf87295714e926561c8a1d0705cc.1755623522.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::635; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pl1-x635.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: 1755628015125116600 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. The BLOCK_IDX variable is initialized to 0 as TB execution begins at the first block. Signed-off-by: Kohei Tokunaga --- tcg/wasm.h | 10 ++++++++++ tcg/wasm/tcg-target.c.inc | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/tcg/wasm.h b/tcg/wasm.h index fba8b16503..91567bb964 100644 --- a/tcg/wasm.h +++ b/tcg/wasm.h @@ -22,6 +22,16 @@ struct WasmContext { * Buffer to store 128bit return value on call. */ void *buf128; + + /* + * Pointer to the CPUArchState struct. + */ + CPUArchState *env; + + /* + * Pointer to a stack array. + */ + uint64_t *stack; }; =20 #endif diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index 6b8df4e9d7..0182d072ca 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -2419,11 +2419,30 @@ static inline void tcg_target_qemu_prologue(TCGCont= ext *s) =20 static void tcg_out_tb_start(TCGContext *s) { + intptr_t ofs; + init_sub_buf(); init_blocks(); init_label_info(); init_helpers(); =20 + /* Initialize fundamental registers */ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(TCG_AREG0)); + tcg_wasm_out_op(s, OPC_I64_EQZ); + tcg_wasm_out_op_block(s, OPC_IF, BLOCK_NORET); + + ofs =3D tcg_wasm_out_get_ctx(s, CTX_OFFSET(env)); + tcg_wasm_out_op_ldst(s, OPC_I64_LOAD, 0, ofs); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(TCG_AREG0)); + + ofs =3D tcg_wasm_out_get_ctx(s, CTX_OFFSET(stack)); + tcg_wasm_out_op_ldst(s, OPC_I64_LOAD, 0, ofs); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(TCG_REG_CALL_STACK)); + tcg_wasm_out_op(s, OPC_END); + + tcg_wasm_out_op_const(s, OPC_I64_CONST, 0); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, BLOCK_IDX); + tcg_wasm_out_op_block(s, OPC_LOOP, BLOCK_NORET); tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, BLOCK_IDX); tcg_wasm_out_op(s, OPC_I64_EQZ); --=20 2.43.0 From nobody Sat Nov 15 05:21:41 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=1755628413; cv=none; d=zohomail.com; s=zohoarc; b=GXMD4DnRFHGvkHa5IlckUojbESDaMRIGQcDYsDfD4NBQF2hXF+8HkjbiusJemzJSelZKcJSxH1OcZ20ZWkn75ah03/QgSPk/JmckoBlvAbFRj4f1YW8xzdKB/ERc3hUwrl/Tbrp/zI8AqUQ6V1XyzTCB5M4z0j56bwOxgJuybJs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755628413; 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=WuRB4oP9pixbhp/lPMdwLr/QGICRtfwCnmuRzPh+aCY=; b=WXoM4kSbRhU6RBkl5P3NB9ErXjxLa7/3TXD6bzOUlzNDnmBbpollbyhpdJfGMhW8EdaitF47QmK7rPDF635Q5e1JH1BE2Cw79jaOMEZpSMkbWOgqMeHb+MbRriRaigsk16H1NE8QpjS7ImSm/seUuZx6mLtbNA7YvV8/dwHxQdk= 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 1755628413690592.1440595049859; Tue, 19 Aug 2025 11:33:33 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoR24-0003Xv-Ol; Tue, 19 Aug 2025 14:26: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 1uoR1P-0001ib-Pq; Tue, 19 Aug 2025 14:25:27 -0400 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uoR1M-0004Tn-D4; Tue, 19 Aug 2025 14:25:23 -0400 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-24458317464so63748925ad.3; Tue, 19 Aug 2025 11:25:19 -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.25.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:25:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627918; x=1756232718; 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=WuRB4oP9pixbhp/lPMdwLr/QGICRtfwCnmuRzPh+aCY=; b=aEIDg3ODPRZfLbIAoN05KMQq7EyrKCywh7Dhd+jBdvB6x5rdwIBBds/uu1ItSpuTlD uSd0iuc78d0Xziz7/+EoW+xW/sM7lniXiyjGhwbZ5/WCsFz8+Mp9BhDThGde+zd7sj+P XeEcu0G6MWnoBChOewK/NJ4UGvl+vAA8NM6rAxwNDk9UncYfze2XAJWJcXVEINNVXmo1 RDDwvkundAeDtWpO457NGvtKSY6MRRkKWvwJQLN2V1U7u9Wl7utDDFjbIeawSMCikKSx Ip9SBwCdbgcdnRK3q0aoqZaVZH0U7wCI2ctEvBo6rPluC9Ml6ABN9SoAqbKjTg1TBoIq dalw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627918; x=1756232718; 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=WuRB4oP9pixbhp/lPMdwLr/QGICRtfwCnmuRzPh+aCY=; b=S23TzgNn04C6hGBpxLpbQ9z/o1O6gcLsF5UxejwX0k1mRGRuNR26HybeU8buRC87PP Nv0CyX9PoddluEtRbAyxANC0dHllCe81OK2iDZ2BOaHOdRJtGn7PGWYkYCgd4P2m2bUC dMAbXD/ZKSVw8vQHCqNFYKvr7N/P5gI5vafGhAGMfxqAJWYT3WukGPaGXXhoBOKxhG7x I+DY4DT8AIPBdm5KZKk7qmJCxM06241vwkdiH07FS1Ug3OkJdorDdPSfNtmAFe9rPMuG 7gs+uY3/CFp32mmqyo2sYAYckuHRAgTK9xHbqtQ3C/LKB4lPljhv0jx1kIWy+M1qXVFU kdUg== X-Forwarded-Encrypted: i=1; AJvYcCWoVmpZj7DD3hUsNoE4yFPxIBp74ZSfBQRb+9TFV0d7DbYn1vm1LD/dI1kJ3ey/b6IfDQ2nlHehK9YU6A==@nongnu.org, AJvYcCX/dVcHgDBDpXiszkpRWjFnfmqDJqDG6I+KLQkyRQzd+AMMp7D5ignNQiKNQ5Mv84hi34ikkps8EA==@nongnu.org X-Gm-Message-State: AOJu0YxKm3pCVxsB2jceDwBHP0djW9VMHAFfgfDG2MXJRep/N2m0Ewqp EpHDKMxoFip/Eolo+sKE279SrTgtFsSyQzemtl1HCbo3V7lJD4LYflgxgWlqHA== X-Gm-Gg: ASbGnct9pdozdQr0tbUC2jYBSxIZumds+Xy0NbJAUjYpL5+9Ym7m/0emkMLibVsQjss Lgnysvcjq6I8Aw52AObDhTfWJLNFWmvBBXVU/JOJpupskASJWt2rCrJJAScpULkyOMO3c+b+W6r yohj5wjHdGB0zQkLdP+iAFGy/tVeUtqFqsSFAToUIyzzDMhQ1xGbv0L+y0CViRNfKdYVEgY39cz mX/WJRYFWq0mzWNHhe4keMYY/JDLJUECUnMutcjuTyW3tYsrX4lOrL0M5IPJJfV4f/ndCYzfUJM aWoaiwDQJ/lQP8Y2fbptY3NDM3hIXymDxceCQIgpuwQSIX/nRWnbNdurYcvKtIrRKhfAN9me9eO 2rT9CGVQdvqQViSUTN8x4LPTC4e9rbgxxCONfarvp654= X-Google-Smtp-Source: AGHT+IGLbwYdUvRZxBausc8t816l6nUvd6o1vcHjU1Ol4NfAtjN/KoqQ547mZJo3CDKuKmSfyO6tfQ== X-Received: by 2002:a17:903:1ac6:b0:242:ff89:d724 with SMTP id d9443c01a7336-245e0542724mr43233545ad.47.1755627918007; Tue, 19 Aug 2025 11:25:18 -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 29/35] tcg/wasm: Write wasm binary to TB Date: Wed, 20 Aug 2025 03:21:58 +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::632; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pl1-x632.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: 1755628415955124100 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 which contains pointers to the following data: - TCI code - Wasm code - Array of helper function pointers imported into the Wasm instance tcg_out_tb_start writes the WasmTBHeader to the code buffer. tcg_out_tb_end generates the full Wasm executable binary by creating the Wasm module header following the spec[1][2][3] and copying the Wasm code body from sub_buf to the TB. This Wasm binary is placed after the TCI code which was emitted earlier. Additionally, an array of imported function pointers is appended to the TB. They are used during Wasm module instantiation. Function are imported to Wasm with names like "helper.0", "helper.1", etc., where the number corresponds to the array index. Each function's type signature must also be encoded in the Wasm module header. To support this, every emission of "call", "qemu_ld" and "qemu_st" operations also records the target function's type information in a buffer which will be copied to the code buffer during tcg_out_tb_end. Memory is shared between QEMU and the TBs and is imported to the Wasm module with the name "env.memory". [1] https://webassembly.github.io/spec/core/binary/modules.html [2] https://github.com/WebAssembly/threads/blob/b2567bff61ee6fbe731934f0ed1= 7a5d48dc9ab01/proposals/threads/Overview.md [3] https://github.com/WebAssembly/memory64/blob/9003cd5e24e53b84cd9027ea3d= d7ae57159a6db1/proposals/memory64/Overview.md Signed-off-by: Kohei Tokunaga --- tcg/wasm.h | 26 +++ tcg/wasm/tcg-target.c.inc | 406 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 432 insertions(+) diff --git a/tcg/wasm.h b/tcg/wasm.h index 91567bb964..260b7ddf6f 100644 --- a/tcg/wasm.h +++ b/tcg/wasm.h @@ -34,4 +34,30 @@ struct WasmContext { uint64_t *stack; }; =20 +/* Instantiated Wasm function of a TB */ +typedef uintptr_t (*wasm_tb_func)(struct WasmContext *); + +/* + * A TB of the Wasm backend starts from a header which contains pointers f= or + * 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/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index 0182d072ca..a1dbdf1c3c 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -134,6 +134,8 @@ static const uint8_t tcg_target_reg_index[TCG_TARGET_NB= _REGS] =3D { /* Function index */ #define HELPER_IDX_START 0 /* The first index of helper functions */ =20 +#define PTR_TYPE 0x7e + typedef enum { OPC_UNREACHABLE =3D 0x00, OPC_LOOP =3D 0x03, @@ -298,6 +300,19 @@ static int linked_buf_len(LinkedBuf *p) return total; } =20 +static int linked_buf_write(LinkedBuf *p, void *dst) +{ + int total =3D 0; + LinkedBufEntry *e; + + QSIMPLEQ_FOREACH(e, p, entry) { + memcpy(dst, e->data, e->size); + dst +=3D e->size; + total +=3D e->size; + } + return total; +} + /* * wasm code is generataed in the dynamically allocated buffer which * are managed as a linked list. @@ -1098,6 +1113,99 @@ static void gen_call(TCGContext *s, } } =20 +static __thread LinkedBuf types_buf; + +static void init_types_buf(void) +{ + QSIMPLEQ_INIT(&types_buf); +} + +static void types_buf_out8(uint8_t v) +{ + linked_buf_out8(&types_buf, v); +} + +static void gen_func_type_call(TCGContext *s, const TCGHelperInfo *info) +{ + unsigned typemask =3D info->typemask; + int rettype =3D typemask & 7; + uint32_t vec_size =3D 0; + + if (rettype =3D=3D dh_typecode_i128) { + vec_size++; + } + for (int m =3D typemask >> 3; m; m >>=3D 3) { + if ((m & 7) !=3D dh_typecode_void) { + vec_size++; + } + } + + types_buf_out8(0x60); + linked_buf_out_leb128(&types_buf, vec_size); + + if (rettype =3D=3D dh_typecode_i128) { + types_buf_out8(PTR_TYPE); + } + + for (int m =3D typemask >> 3; m; m >>=3D 3) { + switch (m & 7) { + case dh_typecode_void: + break; + case dh_typecode_i32: + case dh_typecode_s32: + 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(PTR_TYPE); + break; + case dh_typecode_ptr: + types_buf_out8(PTR_TYPE); + break; + default: + g_assert_not_reached(); + } + } + + switch (rettype) { + case dh_typecode_void: + case dh_typecode_i128: + types_buf_out8(0x0); + break; + case dh_typecode_i32: + case dh_typecode_s32: + types_buf_out8(0x1); + types_buf_out8(0x7f); + break; + case dh_typecode_i64: + case dh_typecode_s64: + types_buf_out8(0x1); + types_buf_out8(0x7e); + break; + case dh_typecode_ptr: + types_buf_out8(0x1); + types_buf_out8(PTR_TYPE); + break; + default: + g_assert_not_reached(); + } +} + +static __thread LinkedBuf imports_buf; + +static void init_imports_buf(void) +{ + QSIMPLEQ_INIT(&imports_buf); +} + +static void imports_buf_out8(uint8_t v) +{ + linked_buf_out8(&imports_buf, v); +} + typedef struct HelperInfo { intptr_t idx_on_qemu; QSIMPLEQ_ENTRY(HelperInfo) entry; @@ -1114,15 +1222,56 @@ static void init_helpers(void) =20 static uint32_t register_helper(TCGContext *s, intptr_t helper_idx_on_qemu) { + uint32_t typeidx =3D helper_idx + 1; + char buf[11]; /* enough for decimal int max + NULL*/ + int n =3D snprintf(buf, sizeof(buf), "%d", helper_idx - HELPER_IDX_STA= RT); + tcg_debug_assert(helper_idx_on_qemu >=3D 0); =20 HelperInfo *e =3D tcg_malloc(sizeof(HelperInfo)); e->idx_on_qemu =3D helper_idx_on_qemu; QSIMPLEQ_INSERT_TAIL(&helpers, e, entry); =20 + tcg_debug_assert(n < sizeof(buf)); + imports_buf_out8(6); /* helper */ + imports_buf_out8(0x68); + imports_buf_out8(0x65); + imports_buf_out8(0x6c); + imports_buf_out8(0x70); + imports_buf_out8(0x65); + imports_buf_out8(0x72); + linked_buf_out_leb128(&imports_buf, (uint32_t)n); + for (int i =3D 0; i < n; i++) { + imports_buf_out8(buf[i]); + } + imports_buf_out8(0); /* type(0) */ + linked_buf_out_leb128(&imports_buf, typeidx); + return helper_idx++; } =20 +static int helpers_len(void) +{ + int n =3D 0; + HelperInfo *e; + + QSIMPLEQ_FOREACH(e, &helpers, entry) { + n++; + } + return n; +} + +static int helpers_write_to_array(intptr_t *dst) +{ + intptr_t *start =3D dst; + HelperInfo *e; + + QSIMPLEQ_FOREACH(e, &helpers, entry) { + *dst++ =3D e->idx_on_qemu; + } + return (intptr_t)dst - (intptr_t)start; +} + static int64_t get_helper_idx(TCGContext *s, intptr_t helper_idx_on_qemu) { uint32_t idx =3D HELPER_IDX_START; @@ -1144,6 +1293,7 @@ static void tcg_wasm_out_call(TCGContext *s, intptr_t= func, int64_t func_idx =3D get_helper_idx(s, func); if (func_idx < 0) { func_idx =3D register_helper(s, func); + gen_func_type_call(s, info); } =20 ofs =3D tcg_wasm_out_get_ctx(s, CTX_OFFSET(tci_tb_ptr)); @@ -1155,6 +1305,39 @@ static void tcg_wasm_out_call(TCGContext *s, intptr_= t func, 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(PTR_TYPE); + types_buf_out8(0x7e); + types_buf_out8(0x7f); + types_buf_out8(PTR_TYPE); + 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(PTR_TYPE); + 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(PTR_TYPE); + types_buf_out8(0x0); +} + static void *qemu_ld_helper_ptr(uint32_t oi) { MemOp mop =3D get_memop(oi); @@ -1188,6 +1371,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 /* call the target helper */ @@ -1228,6 +1412,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 /* call the target helper */ @@ -2417,14 +2602,164 @@ 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, PTR_TYPE, /* arg: ctx pointer */ + 0x01, PTR_TYPE, /* return: res */ +}; + +#define MOD_1_PH_TYPE_SECTION_SIZE_OFF 9 +#define MOD_1_PH_TYPE_VEC_NUM_OFF 14 + +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, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, /* name: "memory" */ +#if defined(WASM64_MEMORY64_2) + /* 32bit memory is used for Emscripten's "-sMEMORY64=3D2" configuratio= n. */ + 0x02, 0x03, /* shared mem */ + 0x00, 0x80, 0x80, 0x04, /* min: 0, max: 65536 pages = */ +#else + /* + * 64bit memory is used for Emscripten's "-sMEMORY64=3D1" configuratio= n. + * Note: the maximum 64bit memory size of the engine implementations is + * limited to 262144 pages(16GiB) + * https://webassembly.github.io/memory64/js-api/#limits + */ + 0x02, 0x07, /* shared mem(64bit) */ + 0x00, 0x80, 0x80, 0x10, /* min: 0, max: 262144 pages= */ +#endif +}; + +#define MOD_2_PH_IMPORT_SECTION_SIZE_OFF 1 +#define MOD_2_PH_IMPORT_VEC_NUM_OFF 6 + +static const uint8_t mod_3[] =3D { + 0x03, /* function section */ + 2, 1, 0x00, /* function type 0 */ + + 0x06, /* global section */ + 86, /* section size */ + 17, /* 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 */ + + 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*/ + + 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, 0x1, 0x7f, 0x1, 0x7e, /* local variables (32bit*1, 64bit*1) */ +}; + +#define MOD_3_PH_EXPORT_START_FUNC_IDX 102 +#define MOD_3_PH_CODE_SECTION_SIZE_OFF 108 +#define MOD_3_PH_CODE_SIZE_OFF 114 +#define MOD_3_VARIABLES_SIZE 5 +#define MOD_3_CODE_SECTION_SIZE_ADD 11 + +static void fill_uint32_leb128(uint8_t *b, uint32_t v) +{ + do { + *b |=3D v & 0x7f; + v >>=3D 7; + b++; + } while (v !=3D 0); +} + +typedef struct FillValueU32 { + int64_t offset; + uint32_t value; +} FillValueU32; + +static int write_mod(TCGContext *s, const uint8_t mod[], int len, + FillValueU32 values[], int values_len) +{ + void *base =3D s->code_ptr; + + if (unlikely(((void *)s->code_ptr + len) + > s->code_gen_highwater)) { + return -1; + } + + memcpy(s->code_ptr, mod, len); + s->code_ptr +=3D len; + + for (int i =3D 0; i < values_len; i++) { + fill_uint32_leb128(base + values[i].offset, values[i].value); + } + + return 0; +} + +static int write_mod_code(TCGContext *s) +{ + void *base =3D s->code_ptr; + int code_size =3D sub_buf_len(); + BlockPlaceholder *e; + + if (unlikely(((void *)s->code_ptr + code_size) > s->code_gen_highwater= )) { + return -1; + } + linked_buf_write(&sub_buf, s->code_ptr); + s->code_ptr +=3D code_size; + + QSIMPLEQ_FOREACH(e, &block_placeholder, entry) { + uint8_t *ph =3D e->pos + base; + int blk =3D get_block_of_label(e->label); + tcg_debug_assert(blk >=3D 0); + fill_uint32_leb128(ph, blk); + } + + return 0; +} + static void tcg_out_tb_start(TCGContext *s) { + struct WasmTBHeader *h; intptr_t ofs; =20 init_sub_buf(); init_blocks(); init_label_info(); init_helpers(); + init_types_buf(); + init_imports_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 /* Initialize fundamental registers */ tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(TCG_AREG0)); @@ -2451,11 +2786,82 @@ 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(s, OPC_END); /* end if */ tcg_wasm_out_op(s, OPC_END); /* end loop */ tcg_wasm_out_op(s, OPC_UNREACHABLE); tcg_wasm_out_op(s, OPC_END); /* end func */ =20 + /* write wasm blob */ + h->wasm_ptr =3D s->code_ptr; + + res =3D write_mod(s, mod_1, sizeof(mod_1), (FillValueU32[]) { + { + MOD_1_PH_TYPE_SECTION_SIZE_OFF, + linked_buf_len(&types_buf) + + sizeof(mod_1) - MOD_1_PH_TYPE_VEC_NUM_OFF + }, + { + MOD_1_PH_TYPE_VEC_NUM_OFF, + HELPER_IDX_START + helpers_len() + 1/* start */ + }, + }, 2); + if (res < 0) { + return res; + } + s->code_ptr +=3D linked_buf_write(&types_buf, s->code_ptr); + + res =3D write_mod(s, mod_2, sizeof(mod_2), (FillValueU32[]) { + { + MOD_2_PH_IMPORT_SECTION_SIZE_OFF, + linked_buf_len(&imports_buf) + + sizeof(mod_2) - MOD_2_PH_IMPORT_VEC_NUM_OFF + }, + { + MOD_2_PH_IMPORT_VEC_NUM_OFF, + HELPER_IDX_START + helpers_len() + 1/* memory */ + }, + }, 2); + if (res < 0) { + return res; + } + s->code_ptr +=3D linked_buf_write(&imports_buf, s->code_ptr); + + res =3D write_mod(s, mod_3, sizeof(mod_3), (FillValueU32[]) { + { + MOD_3_PH_EXPORT_START_FUNC_IDX, + HELPER_IDX_START + helpers_len() + }, + { + MOD_3_PH_CODE_SECTION_SIZE_OFF, + sub_buf_len() + MOD_3_CODE_SECTION_SIZE_ADD + }, + { + MOD_3_PH_CODE_SIZE_OFF, + sub_buf_len() + MOD_3_VARIABLES_SIZE + }, + }, 3); + if (res < 0) { + return res; + } + + res =3D write_mod_code(s); + if (res < 0) { + return res; + } + h->wasm_size =3D (intptr_t)s->code_ptr - (intptr_t)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_write_to_array((intptr_t *)s->code_ptr); + h->import_size =3D (intptr_t)s->code_ptr - (intptr_t)h->import_ptr; + return 0; } =20 --=20 2.43.0 From nobody Sat Nov 15 05:21:41 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=1755628394; cv=none; d=zohomail.com; s=zohoarc; b=F74dclDVnHB5KQP2tXky7uBW20yuSglQbJJEZMYR5TwRTilKA7iVKdYshGd/ifkdP/4gKqYQX4OukTE82ARh7hvTnr+VAIqLaq/kXpP+iPLSc/TjG8PNkPnbsiTRqiQ9OdaDx6Azi5O91UZocEZM7fWRpP3dgUbKfqpGJ3VIBKI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755628394; 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=VLeDWSkTQRfvzmWorL2lfp3RyCcc75fwMMerpGbsxQw=; b=OwZZqRbVzkt9+rEO/2/h+/K4Ii8vey4ZmknviZmoBiQUqFI+KmE1I0n879cCd1E30CQQ7VIEHasN08xakTrh3wYFwdHPSDjqeQ+JWPmDGuQFWCajZvz2EvgEBET14ansmx5SBHkbmSA0uAOXePrpVgOrz1ig+iZPlyZsZl3GZVk= 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 1755628394224206.59472183270384; Tue, 19 Aug 2025 11:33:14 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoR29-000414-Cn; Tue, 19 Aug 2025 14:26: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 1uoR1U-0001jZ-3r; Tue, 19 Aug 2025 14:25:29 -0400 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uoR1S-0004Wh-4J; Tue, 19 Aug 2025 14:25:27 -0400 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-24458272c00so56805435ad.3; Tue, 19 Aug 2025 11:25:24 -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.25.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:25:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627924; x=1756232724; 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=VLeDWSkTQRfvzmWorL2lfp3RyCcc75fwMMerpGbsxQw=; b=FPSJGQB5OM9hYQL6G6EAocsweHFGroRIHK8IBqF11TMKdepgsZc9MN/+Frqzm4ncdE pwSEvtr6kmkxM9x8txHtdWo6TQ4PRFfkOloy4gB0EJptV8un4afnZ4I88HofFQUpGfP6 OTa4k+oyHyyIBRcplCHUQSwUNIpL1JLGlHNbSHuhsasjWvXbb0DLXXACE7Imj7OWcMkH wXdiiVLiA2rTZLP5+3UW84p1HcQDiDp1fGWjvHDJ+bRqDkemq/41TCJoFWkQryBr9F6f nyMjd67Dwy46UaheYaq1jDq4ouFGh9ra0vNx1Mz7wp4TSKLbAx1IwkZ+VTUMvX7w7zvx fmdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627924; x=1756232724; 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=VLeDWSkTQRfvzmWorL2lfp3RyCcc75fwMMerpGbsxQw=; b=rjxxXHuSyZJ1zl0x0G64lA6MZeU/RjcKV8UAPQaSX+CWbw8mLEoHi9Iz8hfqjhk/2U dH5l0BxyR2HAra6RkQPQ5QPNiWaGyPfUKKZVvqEW2XC6ESSDLWrjZRoMMCB5/BQwHpsf daYy2/m9/uB4lSwWjgR0ywvPti86k+HrkLB83OfXyT2ieePtAmmOB3vHkv3aRRomph2t etVmCuv+6ESax5fqI+Coh9NOa8EkiDuYtue1a9WadZzelSyAMjjKAgortmpq7mgiAAmq le/woY1LI1+T3jtqWwatQZukPDGw0qPfGPk1fTn0xmoyXrv2YqYJqJsktgS8Ztsgszxm IEwg== X-Forwarded-Encrypted: i=1; AJvYcCUK9hQ9BiMEOO/9myyyOmoEYxOHU+pFiuZB6EPpNCKBX9OK/T257LWpkWLeDRq5b5A5drB0NMz/Xw==@nongnu.org, AJvYcCVzVZTHOVZ8xUxWLRWUHgkx0s61oE86mKVzQjYqaPL4LdPoAS3EKuduY3Rp4cim/+8YSvWhfrVe0kiERA==@nongnu.org X-Gm-Message-State: AOJu0Yxbq/a3zEFbxMXGQKFbXTh72nyEk5gI+GL9esFJskPhUES7E/sW ZbuDU8/5+ccRvc0cirUeW2/Q8rgfN731I5imESvhPL1hUGk3DxL/ZJ5sbYsgUQ== X-Gm-Gg: ASbGncuU57SoStnrKoUH54Hw3Mg8W0xaJpezVYcyZxUluTYOR3W6EUDAzs5Q5Ps0sLJ gmeGGt9WubgtkYPsVcyDhWYunrkcfRwcF4khGPcLZFof0uv0mH69XJH0stp7ylleqKzsb5+zfVS wRsgOXlKJoGKgUdcKmRG9GGEFW/LH3yfkAVtMkxGgpWVh7XcnfdRC3pgSdmWpc2XO8IodXsy1rY THM3VrkwSFb9AnOwWopnJQvbdbcpagUE0bGZBI0tOfbZ34DxFbM0nE9PSaMHacVMuWY4aIa48S3 PHi52MDb0yaZUBJ9SbtGGWupWa37SMFOaSOGN6ohL/2+MuT5CoaskGQcfHi9GGFn7vhGGiCXOsW elhcbJha+CpclNLdZ54ES91NK8Y3wWCIVNPKLNdBdw0s= X-Google-Smtp-Source: AGHT+IGoOqJOj1cPfn78hD8vlFxsKQWwq6CGFnUar6hxXovlXPO3dsvLvIk8dmpGKHJa3T43IyBUJg== X-Received: by 2002:a17:903:fab:b0:240:1f25:d443 with SMTP id d9443c01a7336-245e047f53bmr49657645ad.30.1755627923708; Tue, 19 Aug 2025 11:25: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 , 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 30/35] tcg/wasm: Implement instantiation of Wasm binary Date: Wed, 20 Aug 2025 03:21:59 +0900 Message-ID: <2ed7c66cf4fc77e69262486f0c735d747b4dcba1.1755623522.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::629; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pl1-x629.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: 1755628395630124100 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 wasm/tcg-target.c.inc, QEMU's memory is imported into the module as "env.memory", 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. Since the subarray() method doesn't accept a BigInt value which is used for the 64bit pointer value, it is converted to a Number (i53) using bigintToI53Checked method of Emscripten. Although this conversion (64bit to 53bit) drops higher bits, the maximum memory size of the engine implementations is currently limited to 16GiB[2] so we can assume that the pointers are within the Number's range. Note that since FireFox 138, WebAssembly.Module no longer accepts a SharedArrayBuffer as input [3] as reported by Nicolas Vandeginste in my fork[4]. This commit ensures that WebAssembly.Module() is passed a Uint8Array created from the binary data on a SharedArrayBuffer. [1] https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Inter= acting-with-code.html#calling-javascript-functions-as-function-pointers-fro= m-c [2] https://webassembly.github.io/memory64/js-api/#limits [3] https://bugzilla.mozilla.org/show_bug.cgi?id=3D1965217 [4] https://github.com/ktock/qemu-wasm/pull/25 Signed-off-by: Kohei Tokunaga --- tcg/wasm.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tcg/wasm.c b/tcg/wasm.c index 4bc53d76d0..835167f769 100644 --- a/tcg/wasm.c +++ b/tcg/wasm.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,42 @@ =20 __thread uintptr_t tci_tb_ptr; =20 +#define EM_JS_PRE(ret, name, args, body...) EM_JS(ret, name, args, body) + +#define DEC_PTR(p) bigintToI53Checked(p) +#define ENC_PTR(p) BigInt(p) +#if defined(WASM64_MEMORY64_2) +#define ENC_WASM_TABLE_IDX(i) Number(i) +#else +#define ENC_WASM_TABLE_IDX(i) i +#endif + +EM_JS_PRE(void*, instantiate_wasm, (void *wasm_begin, + int wasm_size, + void *import_vec_begin, + int import_vec_size), +{ + const memory_v =3D new DataView(HEAP8.buffer); + const wasm =3D HEAP8.subarray(DEC_PTR(wasm_begin), + DEC_PTR(wasm_begin) + wasm_size); + var helper =3D {}; + const entsize =3D TCG_TARGET_REG_BITS / 8; + for (var i =3D 0; i < import_vec_size / entsize; i++) { + const idx =3D memory_v.getBigInt64( + DEC_PTR(import_vec_begin) + i * entsize, true); + helper[i] =3D wasmTable.get(ENC_WASM_TABLE_IDX(idx)); + } + const mod =3D new WebAssembly.Module(new Uint8Array(wasm)); + const inst =3D new WebAssembly.Instance(mod, { + "env" : { + "memory" : wasmMemory, + }, + "helper" : helper, + }); + + return ENC_PTR(addFunction(inst.exports.start, 'ii')); +}); + static void tci_write_reg64(tcg_target_ulong *regs, uint32_t high_index, uint32_t low_index, uint64_t value) { --=20 2.43.0 From nobody Sat Nov 15 05:21:41 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=1755627976; cv=none; d=zohomail.com; s=zohoarc; b=FFmge9utvOvMlKXohkvNJkPevlH2TlyGXqPqTy2J6Qjl5upwRUMHGBRnInJHcDRsZk6nPWT8qEkhhHcL8WdfGOGiNXhy46RjcF/bGO/ofb9P/nS4i8Q2drb5hxrPG2IwoNzX0lMPVRoudx0Ulolmn2FtAvfklZCUBvobqX6GEm8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755627976; 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=2uY8pkG0EyUHp7TJZzW6pu+thngmuu/hzgZ+1SF/arE=; b=T4Wo5sEalFHaYdkiyms+INrwB3PgfGEFba2ZFiP/V0bb91KTDN7Vi4VJji+74ryTTiKK0Gh8XPmfDqeTHsJgowCRR+CrNGi7BLb8DQJQovs/aZGjl/e1x1YnEo9w8B8DtGT/4YzGU4ADOQiPEUmxRNBRWoZ22hU4HVz9OgQZ51Q= 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 1755627976627244.30822069449448; Tue, 19 Aug 2025 11:26:16 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoR1z-000380-QH; Tue, 19 Aug 2025 14:26:00 -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 1uoR1a-0001nB-BX; Tue, 19 Aug 2025 14:25:34 -0400 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uoR1X-0004aJ-L5; Tue, 19 Aug 2025 14:25:34 -0400 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-244581cc971so66766965ad.2; Tue, 19 Aug 2025 11:25:30 -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.25.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:25:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627929; x=1756232729; 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=2uY8pkG0EyUHp7TJZzW6pu+thngmuu/hzgZ+1SF/arE=; b=Xf85jjU6kJ/YfPrqQNIwjUaxZkwrvoRYWW7ajLYQ4uS37EA1XQNoi926SQ4koFvAWD 7ZmiuwU8RJOch28DHcD3kXnMiPCFO4xXASGx+r8FyGu/VVTbJm4vC1FyIocJ98IjH1Jw ZgwY5oyXq1VTmAwF6qQi0qsUUyBNgL0Gp8M5Xf7gS9+paIAIoYCXvqF3/kt85w7Tt5wN ikYetGuNS/VGAm1KX2Kd/5wbmm/R+udsMgg/UobaL8n3cZecTYe7d4xAR+X77GK50DSK FJ1Ysfqx23Scy6VsSA31dxFYyDHVfYzt1buc/ovLguK4sAHUQ8+Yt4GB9TvEZCsJUPur zgEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627929; x=1756232729; 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=2uY8pkG0EyUHp7TJZzW6pu+thngmuu/hzgZ+1SF/arE=; b=jn6MuBTgb78eW+Rqy15tleW+qms2W5Cu5nGQNtSLb4KjzmiqwFS+LtzjN1+usBxwSB Hbmitxnt+0C8Hk3P4/iF1+ErRKcgq93W/UeWEbyW3Fj9G5vmOIrq0qChAOktHscn/PtD w1FxR+WfCNnosfn8/tIxRqxzZhOQafBVowxklW+GLEVOc50cekUhqUTOJy8V5EHunjAW ZITfr2Ghj+Lh3QCfB0z308QsVIFDvIUCYHQugHCk/raUYuN6sAwdl4fkHTAQbd+rOL/c RgiAMHUyPVnMqek6nrpqto049w9Xm/yV5tZ4TuYo9ayP7lZ2yN8jOB081K5os7FKA0Iu O2GA== X-Forwarded-Encrypted: i=1; AJvYcCUMdBT5CJkZCkV624+saTW8dpHWn0NXbrWJbNI/8OBR4Nabt50Ap14PP2j2iA4KWgQAhVIf3ehGXgVrqw==@nongnu.org, AJvYcCWus+lsa3q8CuYRGmvgrqwt2QHbV3Sq6AULmsp3KY9ePo3kGXZeLHlnRRZWDaBH1jsvb4aBl5NCKQ==@nongnu.org X-Gm-Message-State: AOJu0YwD4OMk5GWDy7Tfl6YXw7m0yyAHds9pfzEqCi3HE4lMQauR8A5W ymzhNf6gqZcpEs8FRWYiECfNq9tEPPXCEuQvq8s6oCHihiaciOGwlKcNclFVvg== X-Gm-Gg: ASbGncu4fjepK3q+NqkNnQ4I90QHFUY+DfoZ9r0PSLNIci+dvK47aa+tssSkHYJypLB g4XgYIFEES51u/xZyWHPBe2aHFQnVy0RLQNqrmP6TYFVaHJPOXWd3Rmp5eyHL7laeGgbHFYMutZ wiS7/dqSuWzV+4+RV+HA0jZidY4UZvh5Y+uPStKXca25CYlise/Qiv3o9c0aXmMIJpX4bKohM77 oDMsMdM/j2FYtIptCH5+OgVOCvjQrYuNuBIf6wCaIaVJRjyRsNAuadUx2PvHasun0aBP3zfhkc1 vYHovIx8wtWNYsl/BTWfOo3GZwOSArQdhRt3Wa/dy9H2I39jllMCl2AT54K/wpYQwQaoX602144 YTYUTgENr5PPDT9rX+xsUziVeuFM6ULDDqb4ayz3+BtM= X-Google-Smtp-Source: AGHT+IGmOOmGHsheZIHtUrQVMjqPoTwL3iFRff+4Tim/REDZu1OCu8CID8Q/YPxCaoWIXFLBRsQLPA== X-Received: by 2002:a17:903:2443:b0:242:8a7:6a6c with SMTP id d9443c01a7336-245e02d7629mr46827595ad.17.1755627929480; Tue, 19 Aug 2025 11:25: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 , 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 31/35] tcg/wasm: Allow switching coroutine from a helper Date: Wed, 20 Aug 2025 03:22:00 +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::629; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pl1-x629.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: 1755627979016116600 Content-Type: text/plain; charset="utf-8" Emscripten's Fiber coroutine implements coroutine switching using Asyncify's stack unwinding and rewinding features [1]. When a coroutine yields (i.e. switches out), Asyncify unwinds the stack, returning control to Emscripten's JS code (Fiber.trampoline()). Then execution resumes in the target coroutine by rewinding the stack. Stack unwinding is implemented by a sequence of immediate function returns, while rewinding re-enters the functions in the call stack, skipping any code between the function's entry point and the original call position [2]. This commit updates the Wasm TB module to allow helper functions to trigger coroutine switching. Particaully, the TB handles the unwinding and rewinding flows as follows: - The TB check the Asyncify.state JS object after each helper call. If unwinding is in progress, the TB immediately returns to the caller so that the unwinding can continue. - Each function call is preceded by a block boundary and an update of the BLOCK_IDX variable. This enables rewinding to skip any code between the function's entry point and the original call position. Additionally, this commit introduces WasmContext.do_init which is a flag indicating whether the TB should reset the BLOCK_IDX variable to 0 (i.e. start from the beginning). call_wasm_tb is a newly introduced wrapper function for the Wasm module's entrypoint and this sets "do_init =3D 1" to ensure normal TB execution begins at the first block. During a rewinding, the C code does not set do_init to 1, allowing the TB to preserve the BLOCK_IDX value from the previous unwinding and correctly resume execution from the last unwound block. [1] https://emscripten.org/docs/api_reference/fiber.h.html [2] https://kripken.github.io/blog/wasm/2019/07/16/asyncify.html#new-asynci= fy Signed-off-by: Kohei Tokunaga --- tcg/wasm.c | 3 +++ tcg/wasm.h | 11 ++++++++ tcg/wasm/tcg-target.c.inc | 56 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/tcg/wasm.c b/tcg/wasm.c index 835167f769..f879ab0d4a 100644 --- a/tcg/wasm.c +++ b/tcg/wasm.c @@ -64,6 +64,9 @@ EM_JS_PRE(void*, instantiate_wasm, (void *wasm_begin, const wasm =3D HEAP8.subarray(DEC_PTR(wasm_begin), DEC_PTR(wasm_begin) + wasm_size); var helper =3D {}; + helper.u =3D () =3D> { + return (Asyncify.state !=3D Asyncify.State.Unwinding) ? 1 : 0; + }; const entsize =3D TCG_TARGET_REG_BITS / 8; for (var i =3D 0; i < import_vec_size / entsize; i++) { const idx =3D memory_v.getBigInt64( diff --git a/tcg/wasm.h b/tcg/wasm.h index 260b7ddf6f..a7e2ba0dd7 100644 --- a/tcg/wasm.h +++ b/tcg/wasm.h @@ -32,11 +32,22 @@ struct WasmContext { * Pointer to a stack array. */ uint64_t *stack; + + /* + * Flag indicating whether to initialize the block index(1) or not(0). + */ + uint32_t do_init; }; =20 /* Instantiated Wasm function of a TB */ typedef uintptr_t (*wasm_tb_func)(struct WasmContext *); =20 +static inline uintptr_t call_wasm_tb(wasm_tb_func f, struct WasmContext *c= tx) +{ + ctx->do_init =3D 1; /* reset the block index (rewinding will skip this= ) */ + return f(ctx); +}; + /* * A TB of the Wasm backend starts from a header which contains pointers f= or * each data stored in the following region in the TB. diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index a1dbdf1c3c..f1b7ec5f47 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -132,7 +132,8 @@ static const uint8_t tcg_target_reg_index[TCG_TARGET_NB= _REGS] =3D { #define TMP64_LOCAL_0_IDX 2 =20 /* Function index */ -#define HELPER_IDX_START 0 /* The first index of helper functions */ +#define CHECK_UNWINDING_IDX 0 /* A function to check the Asyncify status */ +#define HELPER_IDX_START 1 /* The first index of helper functions */ =20 #define PTR_TYPE 0x7e =20 @@ -1286,6 +1287,17 @@ static int64_t get_helper_idx(TCGContext *s, intptr_= t helper_idx_on_qemu) return -1; } =20 +static void tcg_wasm_out_handle_unwinding(TCGContext *s) +{ + tcg_wasm_out_op_idx(s, OPC_CALL, CHECK_UNWINDING_IDX); + tcg_wasm_out_op(s, OPC_I32_EQZ); + tcg_wasm_out_op_block(s, OPC_IF, BLOCK_NORET); + tcg_wasm_out_op_const(s, OPC_I64_CONST, 0); + /* returns if unwinding */ + tcg_wasm_out_op(s, OPC_RETURN); + tcg_wasm_out_op(s, OPC_END); +} + static void tcg_wasm_out_call(TCGContext *s, intptr_t func, const TCGHelperInfo *info) { @@ -1302,7 +1314,16 @@ static void tcg_wasm_out_call(TCGContext *s, intptr_= t func, tcg_wasm_out_op_const(s, OPC_I64_CONST, (uint64_t)s->code_ptr); tcg_wasm_out_op_ldst(s, OPC_I64_STORE, 0, ofs); =20 + /* + * update the block index so that the possible rewinding will + * skip this block + */ + tcg_wasm_out_op_const(s, OPC_I64_CONST, cur_block_idx + 1); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, BLOCK_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) @@ -1374,6 +1395,14 @@ static void tcg_wasm_out_qemu_ld(TCGContext *s, TCGR= eg data_reg, gen_func_type_qemu_ld(s, oi); } =20 + /* + * update the block index so that the possible rewinding will + * skip this block + */ + tcg_wasm_out_op_const(s, OPC_I64_CONST, cur_block_idx + 1); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, BLOCK_IDX); + tcg_wasm_out_new_block(s); + /* call the target helper */ tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(TCG_AREG0)); tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(addr_reg)); @@ -1382,6 +1411,7 @@ static void tcg_wasm_out_qemu_ld(TCGContext *s, TCGRe= g data_reg, =20 tcg_wasm_out_op_idx(s, OPC_CALL, func_idx); tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(data_reg)); + tcg_wasm_out_handle_unwinding(s); } =20 static void *qemu_st_helper_ptr(uint32_t oi) @@ -1415,6 +1445,14 @@ static void tcg_wasm_out_qemu_st(TCGContext *s, TCGR= eg data_reg, gen_func_type_qemu_st(s, oi); } =20 + /* + * update the block index so that the possible rewinding will + * skip this block + */ + tcg_wasm_out_op_const(s, OPC_I64_CONST, cur_block_idx + 1); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, BLOCK_IDX); + tcg_wasm_out_new_block(s); + /* call the target helper */ tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(TCG_AREG0)); tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(addr_reg)); @@ -1431,6 +1469,7 @@ static void tcg_wasm_out_qemu_st(TCGContext *s, TCGRe= g data_reg, tcg_wasm_out_op_const(s, OPC_I64_CONST, (intptr_t)s->code_ptr); =20 tcg_wasm_out_op_idx(s, OPC_CALL, func_idx); + tcg_wasm_out_handle_unwinding(s); } =20 static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, @@ -2612,6 +2651,9 @@ static const uint8_t mod_1[] =3D { 0x60, /* 0: Type of "start" function */ 0x01, PTR_TYPE, /* arg: ctx pointer */ 0x01, PTR_TYPE, /* return: res */ + 0x60, /* 1: Type of the asyncify helper */ + 0x0, /* no argument */ + 0x01, 0x7f, /* return: res (i32) */ }; =20 #define MOD_1_PH_TYPE_SECTION_SIZE_OFF 9 @@ -2637,6 +2679,9 @@ static const uint8_t mod_2[] =3D { 0x02, 0x07, /* shared mem(64bit) */ 0x00, 0x80, 0x80, 0x10, /* min: 0, max: 262144 pages= */ #endif + 0x06, 0x68, 0x65, 0x6c, 0x70, 0x65, 0x72, /* module: "helper" */ + 0x01, 0x75, /* name: "u" */ + 0x00, 0x01, /* func type 1 */ }; =20 #define MOD_2_PH_IMPORT_SECTION_SIZE_OFF 1 @@ -2775,8 +2820,17 @@ static void tcg_out_tb_start(TCGContext *s) tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(TCG_REG_CALL_STACK)); tcg_wasm_out_op(s, OPC_END); =20 + ofs =3D tcg_wasm_out_get_ctx(s, CTX_OFFSET(do_init)); + tcg_wasm_out_op_ldst(s, OPC_I32_LOAD, 0, ofs); + tcg_wasm_out_op_const(s, OPC_I32_CONST, 0); + tcg_wasm_out_op(s, OPC_I32_NE); + tcg_wasm_out_op_block(s, OPC_IF, BLOCK_NORET); tcg_wasm_out_op_const(s, OPC_I64_CONST, 0); tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, BLOCK_IDX); + ofs =3D tcg_wasm_out_get_ctx(s, CTX_OFFSET(do_init)); + tcg_wasm_out_op_const(s, OPC_I32_CONST, 0); + tcg_wasm_out_op_ldst(s, OPC_I32_STORE, 0, ofs); + tcg_wasm_out_op(s, OPC_END); =20 tcg_wasm_out_op_block(s, OPC_LOOP, BLOCK_NORET); tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, BLOCK_IDX); --=20 2.43.0 From nobody Sat Nov 15 05:21:41 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=1755628388; cv=none; d=zohomail.com; s=zohoarc; b=NDyu4XagPMsogy7tFvBn2F7TNadu1WPVTQfMcRZs3ZBzJ7cQRg/xeNUYTu2UkN88bNcSq7J6XZ3x+qqdN84iNq/mvA+JRjTsMlOLVbMcKUo6lfNWsLx2EH0uvMLN2NQ1gjeb9yPykt5ofAcnJ9/PQK7qnJBoNysqbkBYP1+/1RM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755628388; 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=7lEO6Ug5ERIuH8Mlq3IETtFos+2BtbJerwnarkvJecM=; b=dC2lDRXu+s5MAAxyN5Gio4F/3YjzH7P3lFPTr7BhxR3C4WTuLbByagioEGNMnBpY6vsQDF5LOE/yCc7UryN2/1IHRwO+utKmw3jtZaRJOPnCrfKkCHU6ZHIAK+yGkZD5VlcM7mhKBuLl5x8ImDmR6JjXfKXDQ00NG+hj1VzPbAk= 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 1755628388111771.2680423900155; Tue, 19 Aug 2025 11:33:08 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoR2c-0005OB-6P; Tue, 19 Aug 2025 14:26:38 -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 1uoR1j-00024g-06; Tue, 19 Aug 2025 14:25:44 -0400 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uoR1d-0004eM-Sq; Tue, 19 Aug 2025 14:25:40 -0400 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-24458317464so63752345ad.3; Tue, 19 Aug 2025 11:25:36 -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.25.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:25:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627935; x=1756232735; 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=7lEO6Ug5ERIuH8Mlq3IETtFos+2BtbJerwnarkvJecM=; b=IyHiUcuNtjalZgBpJ6X8KRRxwhTkcvLYgG3nvKOZwBM27VV2xc2vhz9RQX8yklNuA/ X3YWHvAXguYkaDs2LqyWFPlUIdOTrTDRCKagFEE//49H2afBf0KADOs4v2Lq7AjN3CDK Dw9oeKMXJRKxZzchFuwN+BZXNJCYdhOUXMrIqZL2EjCt57v23ZmugAsp1VJFzrBiCGEa 8syq9Pl0l+tFkwCEiBiO39w8eAu6teTINtcZHlT9Sib1NusOE3a1AJIFI+Smn4NkDU+X EVyRZ0xHk9lhJlSP/71oklB7r+mNTs3CFacafM8lXRwAZDrdoHPf0Geg0Ags5dd7JH+r jtwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627935; x=1756232735; 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=7lEO6Ug5ERIuH8Mlq3IETtFos+2BtbJerwnarkvJecM=; b=GNOOzbjX8c6/qgFsRzBAUmXCS5NvBurgkOEOPs/fjLdolfN+W4/uk9XekaacHytfId mAiBOjcmGRcUy5b3qangmj3OpOwBouan8/JP+FBb8YIM8cYzE6HofmGZCkbZFQUxti4H 0wHVSn7jnl8zEvOh/jNAoltCaWVjGFRS+p8zH1dpUQgyaOtU/TBgXly2GsY+F2Hbmnlr irBJebmdPNkcazzCCp3vqf3pNbywULqzoc9hl8eu4RpgR+yMjk9QpeC8t4riDRCqjfUN CKwaNAEVHZdYHZjrmNpZDzp37xl75C8CNB4906jdVGN7gr9VOLCNHT1c5LN9Z+qvfqJO 2ivg== X-Forwarded-Encrypted: i=1; AJvYcCU/bhnq225tQxHj1O+38mdUSbTpo1lZg0KGvcYsWEKl9Q1+nuzs2iupbY+x8sgWmuShnweZG+5ZVQ==@nongnu.org, AJvYcCXZlRtO9icMWtEtzQNK4iiR8gnGR9WQHoZLW4sJQsXdFkgAjk8XTx8SmOF1bJienbleXsee1IBz5IjK5Q==@nongnu.org X-Gm-Message-State: AOJu0YwmGnWBvkzbT5ArQZt+t8LSADsyC3/ZCd4igNGt7vlyNzuPMWW+ 9EFjqsWhdt181Lh0miS5t2vAVAlapfq9r6dqrJvxXDB4uAFUGhmLo/q3t90g5A== X-Gm-Gg: ASbGncvmOga6ekMLqJS0L+X7J8sMMFooE4rxwDnBg9f6Tkoqz+bIGr2qPuLUayZC9ew 2MsRKjxBlK54x3FG7FCU/8c0BGaorZm4V7G//LCG7NmkAisTioetkAyPwwj+D9Mwg+OStUOd0/o QTHDzfsvHrnrnkP0dbL9nsAxFX4btPNoskmuP1PQz8cVvWN+DLInROFvRJmu4RWu8QNjLNtQZ8m kbPMumMQik6zHndrQ/SZfRg8nnb1bf0sjp/rnG6/SIb3Er+6OFxt/5rEVWWhLQyebk6U6ts4mcp PIrUGwv9rFbZs8PFCZUbbBJHhruxEUnsTUMw1VEm6LZEb2uN8u5gI/xWel0u2cAgFzaWIuXoUJr +X0IDtLXgQS8JV/OwkJx+IryvfTriyCM9 X-Google-Smtp-Source: AGHT+IFWVd0UgV/zaRXnBgiI+AmRtfvKgtPmB4/a6lFAeRl7AuAImttXXw2MX3VLyHz7TjWtt6fn9g== X-Received: by 2002:a17:902:c947:b0:244:5089:ff44 with SMTP id d9443c01a7336-245e0526b5emr45861735ad.41.1755627935261; Tue, 19 Aug 2025 11:25: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 , 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 32/35] tcg/wasm: Enable instantiation of TBs executed many times Date: Wed, 20 Aug 2025 03:22:01 +0900 Message-ID: <0a68602fe660f0c77bf8b54fd26e4933e7255dec.1755623522.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::633; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pl1-x633.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: 1755628389741116600 Content-Type: text/plain; charset="utf-8" This commit enables instantiations of TBs in wasm.c. Browsers cause out of memory error if too many Wasm instances are created so the number of instances needs to be limited. So this commit restricts instantiation only for TBs that are called many times. This commit adds a counter (or its array if there are multiple threads) to the TB. Each time a TB is executed on TCI, the counter on TB is incremented. If it reaches to a threshold, that TB is instantiated as Wasm via instantiate_wasm. The total number of instances are tracked by the instances_global variable and its maximum number is limited by MAX_INSTANCES. When a Wasm module is instantiated, instances_global is incremented and the instance's function pointer is recorded to an array of WasmInstanceInfo. Each TB refers to the WasmInstanceInfo entry via WasmTBHeader's info_ptr (or its array if there are multiple threads). This allows tcg_qemu_tb_exec to resolve the instance's function pointer from the TB. When a new instantiation would exceed the limit, the Wasm backend doesn't perform instantiation (i.e. TB continues execution on TCI). Instead, it triggers the removal of older Wasm instances using Emscripten's removeFunction function. Once the removal is completed and detected via FinalizationRegistry API[1], instances_global is decremented, allowing new modules to be instantiated. [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Globa= l_Objects/FinalizationRegistry Signed-off-by: Kohei Tokunaga --- tcg/wasm.c | 245 +++++++++++++++++++++++++++++++++++++- tcg/wasm.h | 45 +++++++ tcg/wasm/tcg-target.c.inc | 18 +++ 3 files changed, 304 insertions(+), 4 deletions(-) diff --git a/tcg/wasm.c b/tcg/wasm.c index f879ab0d4a..abc5d6b940 100644 --- a/tcg/wasm.c +++ b/tcg/wasm.c @@ -26,6 +26,7 @@ #include "tcg-has.h" #include #include +#include "wasm.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 + #define EM_JS_PRE(ret, name, args, body...) EM_JS(ret, name, args, body) =20 #define DEC_PTR(p) bigintToI53Checked(p) @@ -81,6 +85,8 @@ EM_JS_PRE(void*, instantiate_wasm, (void *wasm_begin, "helper" : helper, }); =20 + Module.__wasm_tb.inst_gc_registry.register(inst, "tbinstance"); + return ENC_PTR(addFunction(inst.exports.start, 'ii')); }); =20 @@ -366,16 +372,59 @@ static void tci_qemu_st(CPUArchState *env, uint64_t t= addr, uint64_t val, } } =20 +static __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); +} + +/* + * inc_counter increments the execution counter in the specified TB. + * If the counter reaches the limit, it returns true otherwise returns fal= se. + */ +static inline bool inc_counter(void *tb_ptr) +{ + int32_t counter =3D get_counter_local(tb_ptr); + if ((counter >=3D 0) && (counter < INSTANTIATE_NUM)) { + set_counter_local(tb_ptr, counter + 1); + } else { + return true; /* enter to wasm TB */ + } + return false; +} + +static __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)]; @@ -814,20 +863,34 @@ 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 (tb_ptr !=3D *(void **)ptr) { + tb_ptr =3D *(void **)ptr; + ctx.tb_ptr =3D tb_ptr; + if (inc_counter(tb_ptr)) { + 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; + if (inc_counter(tb_ptr)) { + return 0; /* enter to wasm TB */ + } + tb_ptr =3D get_tci_ptr(tb_ptr); break; =20 case INDEX_op_qemu_ld: @@ -873,3 +936,177 @@ uintptr_t QEMU_DISABLE_CFI tcg_qemu_tb_exec(CPUArchSt= ate *env, /* * TODO: Disassembler is not implemented */ + +/* + * The maximum number of instances that can exist simultaneously + * + * If this limit is reached and a new instance is required, older instance= s are + * removed to allow creation of new ones without exceeding the browser's l= imit. + */ +#define MAX_INSTANCES 15000 + +static int instances_global; + +/* Avoid overwrapping of begin/end pointers */ +#define INSTANCES_BUF_MAX (MAX_INSTANCES + 1) + +static __thread struct WasmInstanceInfo instances[INSTANCES_BUF_MAX]; +static __thread int instances_begin; +static __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); +} + +static __thread int instance_pending_gc; +static __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 before, but has been removed. Set coun= ter to + * the max value so that this will be instantiated. + */ + set_counter_local(tb_ptr, INSTANTIATE_NUM); + set_info_local(tb_ptr, NULL); + return NULL; + } + return elm->tb_func; +} + +static void check_gc_completion(void) +{ + if (instance_done_gc > 0) { + qatomic_sub(&instances_global, instance_done_gc); + instance_pending_gc -=3D instance_done_gc; + instance_done_gc =3D 0; + } +} + +EM_JS_PRE(void, init_wasm_js, (void *instance_done_gc), +{ + Module.__wasm_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, true); + memory_v.setInt32(instance_done_gc, v + 1, true); + } + }) + }; +}); + +#define MAX_EXEC_NUM 50000 +static __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_gc_completion(); + } + exec_cnt =3D MAX_EXEC_NUM; + } +} + +static int thread_idx_max; + +static void init_wasm(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_wasm_js(&instance_done_gc); +} + +static __thread bool initdone; + +uintptr_t tcg_qemu_tb_exec(CPUArchState *env, const void *v_tb_ptr) +{ + if (!initdone) { + init_wasm(); + initdone =3D true; + } + ctx.env =3D env; + ctx.tb_ptr =3D (void *)v_tb_ptr; + while (true) { + trysleep(); + uintptr_t res; + wasm_tb_func tb_func =3D get_instance_from_tb(ctx.tb_ptr); + if (tb_func) { + /* + * Call the Wasm instance + */ + res =3D call_wasm_tb(tb_func, &ctx); + } else if (!inc_counter(ctx.tb_ptr)) { + /* + * Run it on TCI because the counter value is small + */ + 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_gc_completion(); + res =3D tcg_qemu_tb_exec_tci(env); + } else { + /* + * Instantiate and run the Wasm module + */ + struct WasmTBHeader *header =3D (struct WasmTBHeader *)ctx.tb_= ptr; + tb_func =3D (wasm_tb_func)instantiate_wasm(header->wasm_ptr, + header->wasm_size, + header->import_ptr, + header->import_size); + add_instance(tb_func, ctx.tb_ptr); + res =3D call_wasm_tb(tb_func, &ctx); + } + if (!ctx.tb_ptr) { + return res; + } + } +} diff --git a/tcg/wasm.h b/tcg/wasm.h index a7e2ba0dd7..a9306529e7 100644 --- a/tcg/wasm.h +++ b/tcg/wasm.h @@ -46,6 +46,14 @@ static inline uintptr_t call_wasm_tb(wasm_tb_func f, str= uct WasmContext *ctx) { ctx->do_init =3D 1; /* reset the block index (rewinding will skip this= ) */ return f(ctx); +} + +/* + * WasmInstanceInfo holds the relationship between TB and Wasm instance. + */ +struct WasmInstanceInfo { + void *tb_ptr; + wasm_tb_func tb_func; }; =20 /* @@ -69,6 +77,43 @@ struct WasmTBHeader { */ void *import_ptr; int import_size; + + /* + * Counter holds how many times the TB is executed before the instanti= ation + * for each thread. + */ + int32_t *counter_ptr; + + /* + * Pointer to the instance information on each thread. + */ + struct WasmInstanceInfo **info_ptr; }; =20 +static inline void *get_tci_ptr(void *tb_ptr) +{ + return ((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/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index f1b7ec5f47..cf84c3ca4f 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -28,6 +28,11 @@ #include "qemu/queue.h" #include "../wasm.h" =20 +/* + * This is included to get the number of threads via tcg_max_ctxs. + */ +#include "../tcg-internal.h" + /* Used for function call generation. */ #define TCG_TARGET_CALL_STACK_OFFSET 0 #define TCG_TARGET_STACK_ALIGN 8 @@ -2789,6 +2794,7 @@ static int write_mod_code(TCGContext *s) =20 static void tcg_out_tb_start(TCGContext *s) { + int size; struct WasmTBHeader *h; intptr_t ofs; =20 @@ -2803,6 +2809,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 the 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 15 05:21:41 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=1755628382; cv=none; d=zohomail.com; s=zohoarc; b=BA0TDlkEK2CsDUqlWlAM3P+GAjWfE7eXihku1ZiASB7Z7/tq/jqj1G4mBnepdA4MfKRYK5pqcH2A42FyzaEGqbB93Hc0k66jbM75YPXic6TwJbNh8AIPDnNBrirXIZLPAzXquvu8q4Nb5vbV4Xz8MV8eGNcRI1N2lQ+rIyUGd4A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755628382; 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=kBeoWpxMqpFxA4dbDYsLUVKeHSoVnPre/U93n/Il/C8=; b=j1S4Eo9XvquoBvDr+8Y0Hupr33CDefCd7YNN4ePiVGvfxIFOKpSXAjJIpeo9cfpn1Ysfyu3nzLE9QVsv+Ujhn1qQEl/Z03cFMVLbZtu5+GHngXsEPW2qeplJA+KwAeXpx8ZKeJTNrqiBgF3yBlbO2q4hMdxmWYvKKJtlIGAc3LU= 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 1755628382336912.673556892048; Tue, 19 Aug 2025 11:33:02 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoR23-0003L7-RV; Tue, 19 Aug 2025 14:26:03 -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 1uoR1m-0002GO-Tq; Tue, 19 Aug 2025 14:25:49 -0400 Received: from mail-pg1-x52b.google.com ([2607:f8b0:4864:20::52b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uoR1k-0004hg-6p; Tue, 19 Aug 2025 14:25:46 -0400 Received: by mail-pg1-x52b.google.com with SMTP id 41be03b00d2f7-b47052620a6so138987a12.1; Tue, 19 Aug 2025 11:25:42 -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.25.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:25:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627941; x=1756232741; 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=kBeoWpxMqpFxA4dbDYsLUVKeHSoVnPre/U93n/Il/C8=; b=OFi7aaFdRAC12q+NJ/v1A2a52rME61bGFudGIhXenI14dFdYnjFrpQtfPRfn4rueKP N1Tptg4MzUmFJmJ3POePnG3bWnZeL3/cT4nVddctcTi8K26LmCa1dEcZRxQXRkGvuq1z NHfjxYIYCCXfpyLemg7FPcrF1ifPAqkhboNMlvKBy5SdInrAiICYMb0EKfbCpDDZe40E +bopRRl9O3HA+8bdqjFXq5SatleEACvSG/pjH5OQ3glU78sq86IOEbKeyoGmnnwi69Iw Gnv1Mg9xD4sQUSA0Do17RYWlCnqpaeh/evntmj+3wKQWkhnG98CsYcXB1aqBxzze1iZu NK2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627941; x=1756232741; 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=kBeoWpxMqpFxA4dbDYsLUVKeHSoVnPre/U93n/Il/C8=; b=BPyt0RDUhhiAf65DHD8bVaBgBZ47k+76I8HFHOkHRthwHXdFXRLximNYqj+BysZOjw plSQ66UIO9r5YEptjETZPqX8l/sXrE5N3Fx+UJDTfCikpXmMobn0GsDc0fOvjImq1Jt9 Ml4bJ70I/QR89dObhYJld64T4/aeGLzYvzI943LGceh6FmdRZiFXkmjY+Mfx2oCkI1M5 +e9DpCvEHjhreD06hGjuZJzOQ8bV47MfaTt2aqaHQ/O2snzzVeuYzoZoQG9NKzxOYvux mUuGlmoV09CN2KftcZD/dIyBh+py+FsqE47Wt8PctaeqVf3H+UpoIKbE+dvkOGUTaqkK KE6g== X-Forwarded-Encrypted: i=1; AJvYcCUNrKqwpRZ+9kHaNNbWcpT8r14PaLbl5jlIL+RwlVy4wdF3MvKgKbla6l5o91nKeaSkBYBzhERveg==@nongnu.org, AJvYcCW7hzTSUErJpm7BFWqeAmOoxs9Cip/KvEdew5c2NlDF2zvave5+hBRupvjCPdNiJ/JEy7eNxEk7953iNQ==@nongnu.org X-Gm-Message-State: AOJu0Yx+15jCxqgpHHDMFSAdOyxdZMviHD1rYp1IA/WMxpi8a1ibj/8+ L1FiT1NEItj5uA6tE2gn2mG6yGX6J//k8O9tG/e73qyl2ZZlMSrHa1jqO+MgRw== X-Gm-Gg: ASbGncuVDSEPK+wQA4b6oVRgwPXsNwkGLmZ4wAOMbqSevERkwI6n3ZiuekWn6yhkAEX RNC3fjp0Sn1Eo0R8dna1KizHWJLA4ajR9Pxwt/d6k0tYUTV56W4ps3G9jHRYU3uSfYpDQ7zpoQI 1mROeqzxb5Ag7pEeVUxcXkK3ey2tk6kg5wgYZx26TczYT+5FymhhrmlAamgqLIUQDOUNRoETqD2 tZA1aAdnvXB5XXAYn7EJiSyTRHj5eS2ujQzJgN0Yf/POHrtbDFewYillWXaS4Al2UfVbvSLp7Zd 8qU+39nApcusOXrBKZhgqLslh6Jqo6/WioLwcihjWW93CO3xd/nsTWHJqontENMPJY6cf2vvUAj 08GqV6KUAxZp3G21scMPM29GAPj1DK23X X-Google-Smtp-Source: AGHT+IEg5BHR89mZpBj2A/kNyLiBp7nvUU6QGf4SO99SjEhDpzuRgb8WZEnZJ2Glii82GX34QIeeZw== X-Received: by 2002:a17:902:cecb:b0:240:8381:45b9 with SMTP id d9443c01a7336-245ee00fb33mr4780775ad.8.1755627941254; Tue, 19 Aug 2025 11:25:41 -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 33/35] tcg/wasm: Enable TLB lookup Date: Wed, 20 Aug 2025 03:22:02 +0900 Message-ID: <6cb6e34d6699e050b65cd7938d127de38844da3b.1755623522.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::52b; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pg1-x52b.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: 1755628383814116600 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/tcg.c | 2 +- tcg/wasm/tcg-target.c.inc | 220 +++++++++++++++++++++++++++++++++++++- 2 files changed, 218 insertions(+), 4 deletions(-) diff --git a/tcg/tcg.c b/tcg/tcg.c index 8b44cd3078..6da7689711 100644 --- a/tcg/tcg.c +++ b/tcg/tcg.c @@ -1121,7 +1121,7 @@ typedef struct TCGOutOpSubtract { =20 #include "tcg-target.c.inc" =20 -#if !defined(CONFIG_TCG_INTERPRETER) && !defined(EMSCRIPTEN) +#if !defined(CONFIG_TCG_INTERPRETER) /* Validate CPUTLBDescFast placement. */ QEMU_BUILD_BUG_ON((int)(offsetof(CPUNegativeOffsetState, tlb.f[0]) - sizeof(CPUNegativeOffsetState)) diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index cf84c3ca4f..b068257fe4 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/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 @@ -135,6 +139,7 @@ static const uint8_t tcg_target_reg_index[TCG_TARGET_NB= _REGS] =3D { /* Temporary local variables */ #define TMP32_LOCAL_0_IDX 1 #define TMP64_LOCAL_0_IDX 2 +#define TMP64_LOCAL_1_IDX 3 =20 /* Function index */ #define CHECK_UNWINDING_IDX 0 /* A function to check the Asyncify status */ @@ -153,6 +158,7 @@ typedef enum { OPC_CALL =3D 0x10, OPC_LOCAL_GET =3D 0x20, OPC_LOCAL_SET =3D 0x21, + OPC_LOCAL_TEE =3D 0x22, OPC_GLOBAL_GET =3D 0x23, OPC_GLOBAL_SET =3D 0x24, =20 @@ -1387,11 +1393,156 @@ static void *qemu_ld_helper_ptr(uint32_t oi) } } =20 +#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; + int offset; + + uint8_t tmp1 =3D TMP64_LOCAL_0_IDX; + uint8_t tmp2 =3D TMP64_LOCAL_1_IDX; + + if (!tcg_use_softmmu) { + g_assert_not_reached(); + } + + *hit_var =3D TMP32_LOCAL_0_IDX; + tcg_wasm_out_op_const(s, OPC_I32_CONST, 0); + tcg_wasm_out_op_idx(s, OPC_LOCAL_SET, *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_idx(s, OPC_GLOBAL_GET, REG_IDX(addr_reg)); + tcg_wasm_out_op_const(s, OPC_I64_CONST, + TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS); + tcg_wasm_out_op(s, OPC_I64_SHR_U); + + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(TCG_AREG0)); + offset =3D tcg_wasm_out_norm_ptr(s, mask_ofs); + tcg_wasm_out_op_ldst(s, OPC_I64_LOAD, 0, offset); + tcg_wasm_out_op(s, OPC_I64_AND); + + /* Get the pointer to the target CPUTLBEntry */ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(TCG_AREG0)); + offset =3D tcg_wasm_out_norm_ptr(s, table_ofs); + tcg_wasm_out_op_ldst(s, OPC_I64_LOAD, 0, offset); + tcg_wasm_out_op(s, OPC_I64_ADD); + tcg_wasm_out_op_idx(s, OPC_LOCAL_TEE, tmp1); + + /* Load the tlb copmarator */ + offset =3D tcg_wasm_out_norm_ptr(s, is_ld ? offsetof(CPUTLBEntry, addr= _read) + : offsetof(CPUTLBEntry, addr_write)); + tcg_wasm_out_op_ldst(s, OPC_I64_LOAD, 0, offset); + + /* + * 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_idx(s, OPC_GLOBAL_GET, REG_IDX(addr_reg)); + if (a_mask < s_mask) { + tcg_wasm_out_op_const(s, OPC_I64_CONST, s_mask - a_mask); + tcg_wasm_out_op(s, OPC_I64_ADD); + } + compare_mask =3D (uint64_t)TARGET_PAGE_MASK | a_mask; + tcg_wasm_out_op_const(s, OPC_I64_CONST, compare_mask); + tcg_wasm_out_op(s, OPC_I64_AND); + + /* Compare masked address with the TLB entry. */ + tcg_wasm_out_op(s, OPC_I64_EQ); + + tcg_wasm_out_op_block(s, OPC_IF, BLOCK_NORET); + + /* TLB Hit - translate address using addend. */ + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, tmp1); + offset =3D tcg_wasm_out_norm_ptr(s, add_off); + tcg_wasm_out_op_ldst(s, OPC_I64_LOAD, 0, offset); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(addr_reg)); + tcg_wasm_out_op(s, OPC_I64_ADD); + tcg_wasm_out_op_idx(s, OPC_LOCAL_SET, tmp2); + tcg_wasm_out_op_const(s, OPC_I32_CONST, 1); + tcg_wasm_out_op_idx(s, OPC_LOCAL_SET, *hit_var); + + tcg_wasm_out_op(s, OPC_END); + + return tmp2; +} + +static void tcg_wasm_out_qemu_ld_direct( + TCGContext *s, TCGReg r, uint8_t base, MemOp opc) +{ + intptr_t ofs; + switch (opc & (MO_SSIZE)) { + case MO_UB: + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, base); + ofs =3D tcg_wasm_out_norm_ptr(s, 0); + tcg_wasm_out_op_ldst(s, OPC_I64_LOAD8_U, 0, ofs); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(r)); + break; + case MO_SB: + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, base); + ofs =3D tcg_wasm_out_norm_ptr(s, 0); + tcg_wasm_out_op_ldst(s, OPC_I64_LOAD8_S, 0, ofs); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(r)); + break; + case MO_UW: + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, base); + ofs =3D tcg_wasm_out_norm_ptr(s, 0); + tcg_wasm_out_op_ldst(s, OPC_I64_LOAD16_U, 0, ofs); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(r)); + break; + case MO_SW: + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, base); + ofs =3D tcg_wasm_out_norm_ptr(s, 0); + tcg_wasm_out_op_ldst(s, OPC_I64_LOAD16_S, 0, ofs); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(r)); + break; + case MO_UL: + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, base); + ofs =3D tcg_wasm_out_norm_ptr(s, 0); + tcg_wasm_out_op_ldst(s, OPC_I64_LOAD32_U, 0, ofs); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(r)); + break; + case MO_SL: + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, base); + ofs =3D tcg_wasm_out_norm_ptr(s, 0); + tcg_wasm_out_op_ldst(s, OPC_I64_LOAD32_S, 0, ofs); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(r)); + break; + case MO_UQ: + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, base); + ofs =3D tcg_wasm_out_norm_ptr(s, 0); + tcg_wasm_out_op_ldst(s, OPC_I64_LOAD, 0, ofs); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(r)); + break; + default: + g_assert_not_reached(); + } +} + static void tcg_wasm_out_qemu_ld(TCGContext *s, TCGReg data_reg, TCGReg addr_reg, MemOpIdx oi) { intptr_t helper_idx; int64_t func_idx; + MemOp mop =3D get_memop(oi); + uint8_t base_var, hit_var; =20 helper_idx =3D (intptr_t)qemu_ld_helper_ptr(oi); func_idx =3D get_helper_idx(s, helper_idx); @@ -1400,6 +1551,14 @@ static void tcg_wasm_out_qemu_ld(TCGContext *s, TCGR= eg data_reg, gen_func_type_qemu_ld(s, oi); } =20 + base_var =3D prepare_host_addr_wasm(s, &hit_var, addr_reg, oi, true); + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, hit_var); + tcg_wasm_out_op_const(s, OPC_I32_CONST, 1); + tcg_wasm_out_op(s, OPC_I32_EQ); + tcg_wasm_out_op_block(s, OPC_IF, BLOCK_NORET); + tcg_wasm_out_qemu_ld_direct(s, data_reg, base_var, mop); /* fast path = */ + tcg_wasm_out_op(s, OPC_END); + /* * update the block index so that the possible rewinding will * skip this block @@ -1408,6 +1567,10 @@ static void tcg_wasm_out_qemu_ld(TCGContext *s, TCGR= eg data_reg, tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, BLOCK_IDX); tcg_wasm_out_new_block(s); =20 + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, hit_var); + tcg_wasm_out_op(s, OPC_I32_EQZ); + tcg_wasm_out_op_block(s, OPC_IF, BLOCK_NORET); + /* call the target helper */ tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(TCG_AREG0)); tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(addr_reg)); @@ -1417,6 +1580,8 @@ static void tcg_wasm_out_qemu_ld(TCGContext *s, TCGRe= g data_reg, tcg_wasm_out_op_idx(s, OPC_CALL, func_idx); tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(data_reg)); tcg_wasm_out_handle_unwinding(s); + + tcg_wasm_out_op(s, OPC_END); } =20 static void *qemu_st_helper_ptr(uint32_t oi) @@ -1436,12 +1601,47 @@ 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) +{ + intptr_t ofs; + switch (opc & (MO_SSIZE)) { + case MO_8: + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, base); + ofs =3D tcg_wasm_out_norm_ptr(s, 0); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(lo)); + tcg_wasm_out_op_ldst(s, OPC_I64_STORE8, 0, ofs); + break; + case MO_16: + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, base); + ofs =3D tcg_wasm_out_norm_ptr(s, 0); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(lo)); + tcg_wasm_out_op_ldst(s, OPC_I64_STORE16, 0, ofs); + break; + case MO_32: + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, base); + ofs =3D tcg_wasm_out_norm_ptr(s, 0); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(lo)); + tcg_wasm_out_op_ldst(s, OPC_I64_STORE32, 0, ofs); + break; + case MO_64: + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, base); + ofs =3D tcg_wasm_out_norm_ptr(s, 0); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(lo)); + tcg_wasm_out_op_ldst(s, OPC_I64_STORE, 0, ofs); + break; + default: + g_assert_not_reached(); + } +} + static void tcg_wasm_out_qemu_st(TCGContext *s, TCGReg data_reg, TCGReg addr_reg, MemOpIdx oi) { intptr_t helper_idx; int64_t func_idx; MemOp mop =3D get_memop(oi); + uint8_t base_var, hit_var; =20 helper_idx =3D (intptr_t)qemu_st_helper_ptr(oi); func_idx =3D get_helper_idx(s, helper_idx); @@ -1450,6 +1650,14 @@ static void tcg_wasm_out_qemu_st(TCGContext *s, TCGR= eg data_reg, gen_func_type_qemu_st(s, oi); } =20 + base_var =3D prepare_host_addr_wasm(s, &hit_var, addr_reg, oi, false); + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, hit_var); + tcg_wasm_out_op_const(s, OPC_I32_CONST, 1); + tcg_wasm_out_op(s, OPC_I32_EQ); + tcg_wasm_out_op_block(s, OPC_IF, BLOCK_NORET); + tcg_wasm_out_qemu_st_direct(s, data_reg, base_var, mop); /* fast path = */ + tcg_wasm_out_op(s, OPC_END); + /* * update the block index so that the possible rewinding will * skip this block @@ -1458,6 +1666,10 @@ static void tcg_wasm_out_qemu_st(TCGContext *s, TCGR= eg data_reg, tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, BLOCK_IDX); tcg_wasm_out_new_block(s); =20 + tcg_wasm_out_op_idx(s, OPC_LOCAL_GET, hit_var); + tcg_wasm_out_op(s, OPC_I32_EQZ); + tcg_wasm_out_op_block(s, OPC_IF, BLOCK_NORET); + /* call the target helper */ tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(TCG_AREG0)); tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(addr_reg)); @@ -1475,6 +1687,8 @@ static void tcg_wasm_out_qemu_st(TCGContext *s, TCGRe= g data_reg, =20 tcg_wasm_out_op_idx(s, OPC_CALL, func_idx); tcg_wasm_out_handle_unwinding(s); + + tcg_wasm_out_op(s, OPC_END); } =20 static bool patch_reloc(tcg_insn_unit *code_ptr_i, int type, @@ -2727,7 +2941,7 @@ static const uint8_t mod_3[] =3D { 0x80, 0x80, 0x80, 0x80, 0x00, /* placeholder for section size*/ 1, /* num of codes */ 0x80, 0x80, 0x80, 0x80, 0x00, /* placeholder for code size */ - 0x2, 0x1, 0x7f, 0x1, 0x7e, /* local variables (32bit*1, 64bit*1) */ + 0x2, 0x1, 0x7f, 0x2, 0x7e, /* local variables (32bit*1, 64bit*2) */ }; =20 #define MOD_3_PH_EXPORT_START_FUNC_IDX 102 @@ -2939,7 +3153,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 15 05:21:41 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=1755628098; cv=none; d=zohomail.com; s=zohoarc; b=hgkqwx8ihi/pj7IUlNRGPZcdeFzuEE25/pinTZlmr6ndF5Z2LnCsMO8tVx2gHeTCfCwyf6RRvE8kxoaSgeXaSzrYhGum/8RofxFYYzaQYLTpdqph+49PV+z+VVRm13qIGwgeM7QX5etdgglo22huZxXJEegwjfQVlCmXv/bKIS4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755628098; 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=D0ZS0eANdrz5r5AN9KWFnrb1+idZJ7pMgAy4XohkPPI=; b=JJsZYlrsXNqpTAySByFNvky61vPMDT/H0md+h1O9oRL02vAFTKNgBVt2Nz85ssQhamHhD8vqAe2cUbMziJKmEyBXunS15sWCOq374C5PQjynCVq7he9Owr5tn/tN3g1iBUjk33IIwOgYPye/9CLkTfHOEEWLWEt7cac2ROskC9k= 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 1755628098062865.297579584938; Tue, 19 Aug 2025 11:28:18 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoR2B-0004M0-L0; Tue, 19 Aug 2025 14:26: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 1uoR1s-0002SG-14; Tue, 19 Aug 2025 14:25:52 -0400 Received: from mail-pg1-x52f.google.com ([2607:f8b0:4864:20::52f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uoR1o-0004kT-TC; Tue, 19 Aug 2025 14:25:51 -0400 Received: by mail-pg1-x52f.google.com with SMTP id 41be03b00d2f7-b4717330f9eso4046105a12.1; Tue, 19 Aug 2025 11:25:47 -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.25.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:25:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627947; x=1756232747; 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=D0ZS0eANdrz5r5AN9KWFnrb1+idZJ7pMgAy4XohkPPI=; b=fR25nJV+LVNK5NvOKdzdH0WETCqKRxngqeVOBXHsnzsy1VXQCVGyiT97emZ4nvlfHe uaAmTSXy6k52saaINFg4QKbZPy+/+584veFendIme/z7IYqefoFM8LNyE9t6q5ALlBy+ ug/Bctajcb7DpJW+F0lQM+iC/LXpmoax2MsfZqfQj2CF61fSXr7cBqXV6CgzMMb9xWL2 NwYvCvTp4WA9pxVxSDmUpEO/Aj8pO8nresnhcG8URYU+2Ixm5iWjss/JwUGDhLUXgHcT /fnE6xhTjK6wFHKsKl/jhzAN0Han/OeU8UcsZE8eFqpp88m7Y1x9R87sPwxlegybUh1t rNUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627947; x=1756232747; 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=D0ZS0eANdrz5r5AN9KWFnrb1+idZJ7pMgAy4XohkPPI=; b=XLmL+aVbN1ZKxXmS16BzYumiUqtopHHC5UxFp89jJNMjVhAJ5snijoZQKRsXiUTlAr OFy9sBq86efezDsPX5U44aolwTQ6qjkhsv4OEqxYJrx2D8HmN615eu+XwwgMJsSj3uwn ZWp3emxbReRHpmbFi4aSlvDHK7+N6sHL2u5YHgVKHDi18+JbN8xn5yfPRTMENDEKVNF9 sBe+cMpMgT8MTWeAPUHz+HStAYA5rJhu6W63+PS4iA3nMGGid77smMsW7cot/EpsiFZi SURIpzAyWj+PkEFqPowsBVSrin0UIQEixOik4cDskEFPKVkyjZRcusmeMVK7r8I4QI45 YENg== X-Forwarded-Encrypted: i=1; AJvYcCU41Wo3dS2iyendSCsD+Op27FvE6+553yG286RpA9xUXcTgRiLP51oYlri2emJYBkKjrAAw3BvJmA==@nongnu.org, AJvYcCUPxE0kSMa51zyxLvo7iKRzAShYi5CHpinCLt2Bwp1FtUB6keDYUYd/YI2qHA2jc1BlZZquNYNM+nYwtw==@nongnu.org X-Gm-Message-State: AOJu0YwioIgypGZ70982ovWGL2+0Rf/QzUzW2hawgc26KErEp1694O/a sNE7BOggi0E7xu8gZ3cLRje508rBwtzYGp1vxxQIzoSpyfa4B/L6j9s6R6kwyg== X-Gm-Gg: ASbGnctc5/6qFq23KpeZoGu1EXWlSyNc4c2rP6mEeivS4jTrqM3Kcl1aVp7O+xFhejm 4bczX90GW9NCwWOOJjLyzND9mI0O7dI3pc6OIJxXFATrkgOkJZaGLa98WefFEjIDFKvGafz21vL w0jsryOg7F1W29/NlLFf0McYw6yVboMqpceA5cUqIhDa8XQD0MEp1fcq9TaFzbYCBfJ9QX7JxEZ sLnQiE1OBm3QHlP05Iq+Uj9EVmgP1+qDBGmLcmaV1FyYmP0Wem3zHZXwRunbap0W+4+f3DOV1RM GGBgWWs8dNJ44Cql0VTI428eLqruLRtKU7cB9UkFhtN75tNkcGQVPa3KgFZ3SX6RRv4S96jG5px TKcPmizW3i/3n8snQMwZjkg== X-Google-Smtp-Source: AGHT+IHWOvZcNEk7FSavWVndeZWYqWhy5js2qNQ/82vDiEuhfJ4Ba51BpMgeRVwrF5FC0XW1ixu/BQ== X-Received: by 2002:a17:902:cf4a:b0:235:e1d6:4e22 with SMTP id d9443c01a7336-245e02dc4afmr44719865ad.18.1755627946786; Tue, 19 Aug 2025 11:25: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 , 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 34/35] meson: Propagate optimization flag for linking on Emscripten Date: Wed, 20 Aug 2025 03:22:03 +0900 Message-ID: <07548c6633e8f6be080a988a9e60c3c2beee6eec.1755623522.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::52f; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pg1-x52f.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: 1755628100223124100 Content-Type: text/plain; charset="utf-8" Emscripten uses the optimization flag at the link time to enable optimizations via Binaryen [1]. While meson.build currently recognizes the -Doptimization option, it does not propagate it to the linking. This commit updates meson.build to propagate the optimization flag to the linking when targeting WebAssembly. [1] https://emscripten.org/docs/optimizing/Optimizing-Code.html#how-emscrip= ten-optimizes Signed-off-by: Kohei Tokunaga --- meson.build | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/meson.build b/meson.build index 5fee61a256..a98b9f836c 100644 --- a/meson.build +++ b/meson.build @@ -878,6 +878,12 @@ 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' + # Emscripten uses the optimization flag also during the link time. + # https://emscripten.org/docs/optimizing/Optimizing-Code.html#how-emscri= pten-optimizes + 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 15 05:21:41 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=1755628301; cv=none; d=zohomail.com; s=zohoarc; b=EFPBkj+UGy+EQYGmzZqzdg23EI1/avICe+cWzN8/aMLtvP4s1f6zTeYARuDNDPs2tBJo5nKKW3ynTAOgyfoo66iOOCRGRgeuTQ62C9e1JHEXCWr5rUUSy8ocw0RrXty8kup6xzI3TB/Az2QwynguVZKuFFOKCAOsBqRmkUYbYs4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755628301; 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=oCoav4mrpfqHaiFY4ajN/hsX2ml/OjgBByRUO4LMbYo=; b=G1dxP9W7RH3i83qan9HI3YeaqIKPfxznCq2gdaG5YnHcS/IlqNkxiZnmOtd5VGncat7jo68S3dBd+Jf70sjlneLYbTT5rLGyJ8t+V+64+d6cCKWfsVSCLduxS5TRL80jRZFAbcZH24PULTw4Gahp6//6QWZaeYhob914TafwZxk= 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 1755628301114542.2630797285966; Tue, 19 Aug 2025 11:31:41 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uoR2f-0005ps-Q5; Tue, 19 Aug 2025 14:26:42 -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 1uoR1x-0002zh-0C; Tue, 19 Aug 2025 14:25:58 -0400 Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uoR1u-0004lj-VD; Tue, 19 Aug 2025 14:25:56 -0400 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-2445818eb6eso42558305ad.2; Tue, 19 Aug 2025 11:25:53 -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.25.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Aug 2025 11:25:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755627952; x=1756232752; 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=oCoav4mrpfqHaiFY4ajN/hsX2ml/OjgBByRUO4LMbYo=; b=GGoLiNSncVJcDMR7UE+Jm88FmOoJi77G/YpeEGT66uMR8appXuBl98sqhlPRwO/exD 6mdaLt2ZaWtZturCNwitsonbWvRd6WZlo8qeTaWTp8yp3OVTSvb1o9vLuyP1OglHvJVT CFJEKN2+DonkEgkpITuoiEvEqubovfyHeGjaiRtRqR+mrNtJxKFaMhdjsXpzcTTaGvL1 8AfB1DzOviWGEoVbI6HylJZSFF6tOKDwvjZWdMohC5O7QN+5mWId06R94Ek4pHECAlJu t3rjBHVg5ucJaGaUh3jIi00/e9GPNCLHZ8WaC6IkdKFZ/h08iSYDoaY0DQKtRo0SQC5p Ii8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755627952; x=1756232752; 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=oCoav4mrpfqHaiFY4ajN/hsX2ml/OjgBByRUO4LMbYo=; b=nJYjatmNlaezB14vTi4u6uqJys9sDqlyA1Z9vzvKcGAD0WOmAcTUHQEiXvjY156IaC cJ2vN70xMmBDjN2jL4H5vWPojtTcKSNiAgCtcixuC67V7ufifI4yemuO7cjAdHOgTs8i NJD5nZXPHePfpQJ81KJatMbnkJMkGmTHLW0k1CX4KsbX5AfiT4n3AkyLAitGUQYQkRhP ZjndJtsLbJI9rPo+3RmdhMCBr2sj0jvl0Kc4V/w1YKp7qdHSr5zdcx8eZnUKZNiE2KOa u+XmUt1DomKWe1LmWxnQCObq8oA0FUmhdUZfi7nOtYrZiZizJtLxaou4CRCeBn+gUVzv RpdQ== X-Forwarded-Encrypted: i=1; AJvYcCVj6bMWn99vXSEMtReT989Yh8/9QvXV406Fl4lStPdhv8sWLWHZmPTjLBTKZeKTQSToBxEfKiK/1I6R7A==@nongnu.org, AJvYcCXm87ouYvru5n0EfBPU9adahD6IJRAAZ4j0XFepcVc2jdpH6NUZhGaxPf0EUYwFoKDVT+737b81Rg==@nongnu.org X-Gm-Message-State: AOJu0Yyg/ghGg3dy42trlPaGC1v4k9B4g6I4NGS+QujY1MrBs/M2y3V2 HhT8yLmfwtRCDQnb/4TYZLNsmvrjzjsdzjdRy1gZ5XTya+TT0+4l0Ga4O4bhxw== X-Gm-Gg: ASbGnctaYwUfzuOhHLyLCHECTVi5VaAgPfhvil/lN0d2Onx8b8zgtsR6snig4gu0vk8 +brCtEw5XIlympSe06TDNCt2Bg3RueHcF54tQwCEEEPxYPW5ZCFnJ1saYHq7ynZzod8vzCefoOf uKSEafzLiTF5sx2HpG7ysVcgpV9uxTCm8nOzI6K9m0lcWqRJBQbWPYAKunNEghKGczblH4CraH7 eN0ddXdUgDxbOTCiebKvpOb+rDzxuX2tfoFUMsll/j13G+N4BBWl1gdWlAu20ucmMXh+AvR03jF HLitf4GN47WUVgjwT22o3yKFcX6TfDbBQT83AEiNW5DYPcnHsCXypYMl20Kt4ovOqU+NYwhGAs6 QRXsbNwLzru8bC3v50ZbWwYnXLFkq8xYa X-Google-Smtp-Source: AGHT+IEWycNXEqEmopq8EixwTWTn4xWQBkQoJhPpo4jeqVAyWKapU2YM8AiI4lexRcfYoxOBYQS0dQ== X-Received: by 2002:a17:903:37cd:b0:234:c8ec:51b5 with SMTP id d9443c01a7336-245e04c8f3cmr41111305ad.53.1755627952428; Tue, 19 Aug 2025 11:25: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 , 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 35/35] .gitlab-ci.d: build wasm backend in CI Date: Wed, 20 Aug 2025 03:22: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::630; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pl1-x630.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: 1755628302723124100 Content-Type: text/plain; charset="utf-8" This commit adds the build tests for the wasm backend. Signed-off-by: Kohei Tokunaga --- .gitlab-ci.d/buildtest.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitlab-ci.d/buildtest.yml b/.gitlab-ci.d/buildtest.yml index a97bb89714..16a3322277 100644 --- a/.gitlab-ci.d/buildtest.yml +++ b/.gitlab-ci.d/buildtest.yml @@ -803,7 +803,7 @@ build-wasm64-64bit: job: wasm64-64bit-emsdk-cross-container variables: IMAGE: emsdk-wasm64-64bit-cross - CONFIGURE_ARGS: --static --cpu=3Dwasm64 --disable-tools --enable-debug= --enable-tcg-interpreter + CONFIGURE_ARGS: --static --cpu=3Dwasm64 --disable-tools --enable-debug =20 build-wasm64-32bit: extends: .wasm_build_job_template @@ -812,4 +812,4 @@ build-wasm64-32bit: job: wasm64-32bit-emsdk-cross-container variables: IMAGE: emsdk-wasm64-32bit-cross - CONFIGURE_ARGS: --static --cpu=3Dwasm64 --enable-wasm64-32bit-address-= limit --disable-tools --enable-debug --enable-tcg-interpreter + CONFIGURE_ARGS: --static --cpu=3Dwasm64 --enable-wasm64-32bit-address-= limit --disable-tools --enable-debug --=20 2.43.0