From nobody Tue Sep 16 03:04:09 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=1756728046; cv=none; d=zohomail.com; s=zohoarc; b=WwsMXJw50taMgpG7zpEa66FmH156bw4u/SFV0jEhR5BVqdNfcVeirBeB5gHm0gvluV4Gi3lJQbGRkbSttbt3Nk/Z1HIdbWapM5asPytNA7i2FyK22YC58Tzf9QNN/RefdQJ9LV2xjWMiMp3+wY5ir9hvO2ABbFfJTXekc6sjID0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756728046; 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=8PJjbFuVAPTI7U71p25kyRaD/FGclmStwyCKrbtmghs=; b=c7xCc+/zZRGEsusY3PBzip9lVDF4arpf+NClX4XbYEkZOfgwvp0ZgOQcWqcAlrDOG+MbZaK644pyHhJuMXubgUBHb5Cgi8cPIWjapZLqujav9tYU52vmiMqV2+6+AW7w1h/gPQ8SXvsT+XiK9Ufr3ECgx9ZeLy/xBeW6srlSmFw= 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 1756728046331867.2316595928444; Mon, 1 Sep 2025 05:00:46 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut2yu-0006jl-Qq; Mon, 01 Sep 2025 07:45: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 1ut2yk-0006c7-UP; Mon, 01 Sep 2025 07:45:44 -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 1ut2yc-0001Gm-IH; Mon, 01 Sep 2025 07:45:41 -0400 Received: by mail-pg1-x52a.google.com with SMTP id 41be03b00d2f7-b4c72e4f1b6so2240214a12.1; Mon, 01 Sep 2025 04:45:29 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.45.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:45:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727128; x=1757331928; 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=8PJjbFuVAPTI7U71p25kyRaD/FGclmStwyCKrbtmghs=; b=jRF6w8M0IJHrZXpE4O9ehr9i2T9b2U7kjnpMD2aQDTiuUX+iA86Y9zTmYd5nccgxBw QXYBuLXrFhfn77qwLJ4N8I7Mh8drbVXPvI4FuWyvD2gmJG617yaBRNFG5hnYtx2L439/ QkyxSsUz6xZoxDPC2Ntmebf0kpIGsqn0CXglewfCBJu/cfrpKQOP3xoFfy7gpEJlAlg5 ky0uuzT69C8nA3l0ghv9pqjd5rUlWJm/x+NkKPs2d+4WWkxXFlV17WPcIz7y5cvGptio jgWgAIr9WbNsiYm+VzDLFRr0vrdtZhlYjlzzkw9FWQKgvafXxT4QUNWpd5LiVmGVBRHB 3IkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727128; x=1757331928; 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=8PJjbFuVAPTI7U71p25kyRaD/FGclmStwyCKrbtmghs=; b=kIp2pFigla1QS7Y9YpMcvSMNe3LlvjXJd5htgklLH1s8l0H8oCaT2aHM6n7ogdPYx+ zudJQ7KRwpbKV2S8kluvSkw7YTTcQZ2C+tMId4x/NC23wtpw+zy4RXYGQ9+HG6VVjKAL s0kyYMknisfldqaiYjKqUdBPrz+G6fffQMywZ4EKSETTZk2KkKlKPNpDS55OkZ9BO6Dx XwVYhuA+H+4EmRUsAF/GH/CpLkGda3SRtAvMoRekS71q+CMGNkO81U8UbBeYXu9ZNGio gzBdKe+OXnh8VZ/b7Z3WP7qEFc9QBYoWuL7CAmxl2PvCsjqxiFZ0QFKzqAjJmlJpCdck BX9w== X-Forwarded-Encrypted: i=1; AJvYcCVhGmSxb8D5necKLQ9qjrcLHgdbeo3A6c9dAOIBztue4KvT9XaxnU1p7mMT+czmRr2xtL8jett+WQ==@nongnu.org, AJvYcCXNDSRUYFC5DFgvJ4pbXTyaqp1rZgBXGrStshF6sOZb+FXrkke+TlMSO7TlR33xEIn9jmWycG8TLJulZg==@nongnu.org X-Gm-Message-State: AOJu0YwDPCfHpOvkiKcDDJvzzM92tSUfjP6hYB3JyE3HVM7NHE/zkqE7 JKIAEgnbG+xKzPEySvw9fFZnrGKwL3vwl5WHB4IieQiSp4PLAOVfjA7YUHgILQ== X-Gm-Gg: ASbGnctJHuG5geChZDXP3X9lvJx+y542iSKwHMBJ8FmB/mHyB/+Cr3JCBTZ3bGVMz9e A7IIAfz6oSR/QyGxzyRpvNLHeHQRNvpA0fCTQG9MKtZJLreZPPBDDWYo5WznQIPEBmR6H7c0/Vc 0lNEZ1NYGbgt0jRaT5tL62S75qQtv1/RJJel/buTMsnjpxnfrE3/pu4CNqXaEeDHd6Q6lcuZJk6 ek23NrrpNdEcLn+oCOz+xNERFvkubDi8VjbMbHwuoNsJzGb64PxwwmGImCu2mQ5Gnnjahk5lVko ASkg3HGopNxyXjhR8MVCPXF0RY/2SUZnMerVsq6zEi20t+L8Se6N8RbftmHY8T/Aj4PtaY3lpq5 I34qOAe8E7ZgO7l0H/0u4bw== X-Google-Smtp-Source: AGHT+IEk+JR9rLQxwWFa2CL9gxcqSzd16zyWCdHr8aNXz6jrLhN/lSHLexK71nqTr/kN7zDED+mlew== X-Received: by 2002:a17:903:3804:b0:235:f091:11e5 with SMTP id d9443c01a7336-249449037ecmr95371585ad.10.1756727128189; Mon, 01 Sep 2025 04:45:28 -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 v3 01/35] meson: Add wasm64 support to the --cpu flag Date: Mon, 1 Sep 2025 20:44:03 +0900 Message-ID: <33d88400963d0da84f44296306c4e39af1f3cf5c.1756724464.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: 1756728047887116600 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(-) V3: - This commit is under review in another patch series so please ignore it here. diff --git a/configure b/configure index 274a778764..112e4725fc 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 50c774a195..44bb7ed334 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 Tue Sep 16 03:04:09 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=1756727237; cv=none; d=zohomail.com; s=zohoarc; b=JlgPmwixiohRAo7+URWUBcL3yZ1jMyplKgEId+hmeVDfR6PbLtsMZpEj44ic93xzFDNkfwhprgZcsQCzV9WGStZum9PFXbCEaDZ69A3s8j0ZFDF0nINlrG61/kCZ1BZTWP6FMSTETMdAcI+M3H7Q6g5qKeQzXrCduehjlXVkYtw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727237; 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=+PQrFo8EBMIk0vCQdMb98g3now50VhqZJnrrrRXa3xE=; b=bpGdP2D2qgLR/jyhsTdyOrgYXoQvCJKLJmNd7S5QIDBIu4NsBjv+EcBStG/UmNtFuTc95zJHT/29bsPAozXMxVk0OWhoqTftmljWGGKPW6wIMFK3gnWYbYv7zuXfvkq4xKLaDXtiNlnytm26kD6PqGAOLKf9e2bB6fyTSsPONxU= 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 1756727237915915.6442422059883; Mon, 1 Sep 2025 04:47:17 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut2z4-0006q7-Sq; Mon, 01 Sep 2025 07:46: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 1ut2yt-0006kE-F4; Mon, 01 Sep 2025 07:45:52 -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 1ut2yj-0001Ho-K9; Mon, 01 Sep 2025 07:45:51 -0400 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-2445805aa2eso43000915ad.1; Mon, 01 Sep 2025 04:45:36 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.45.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:45:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727134; x=1757331934; 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=+PQrFo8EBMIk0vCQdMb98g3now50VhqZJnrrrRXa3xE=; b=UqBKkC+nUrm2+6qsp+AZ78DQgyCSYcp6TCzo69z9uHkx8N7Sm2KnPRBmdF0N1zxP4q NvNhnL7QAL4H0I5wWX+MM1Xxrr7IpH3vw5Vp/iG8OoReE/jVhr1mZ1nSqL6k0MtBQQgI bSHg6SeiKUpGLR7G7fIVJt8v0yyeTzx6gMOzihxcgWpXF9ZpVkXDE+Ygye85fNv6in97 6/3SftmBIUhrq9sgTL5f3Ci3Fug9OlGwzdfWyFuhyk/oqn11HBVu+AjTq9YKMH66Sd5Y 3e4VQ0tyJxkRMcbTFPiqkrxiHyfvL/9xR2wGKxl11zWKFMH553dIq0n8mP7KrGUi5TW/ aZrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727134; x=1757331934; 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=+PQrFo8EBMIk0vCQdMb98g3now50VhqZJnrrrRXa3xE=; b=oczDQkDi4HcLwU3dJhcx9ZsUbD7ymb4HEom/ofJ6DCyrMd+magnHj38EzPJnr2H5XL ywBXy6onCUVrdjzw6Jf4oObg8n6YnUC5FJK48gqe/7vFO8+kHiBJr/CAwRsJBRg8AC4m 78s6garp88dSEAzSp/w46s/UFmw/5GoeiN7btKGJPz4HJR9JGYv9zHzQ+fzhzwAL5GBu PNWkR8QpBKKnYfr8RNVmv8iFLsgX/sXvKfIZ+b9vFxJC/gHdHIv2ZW0eq6kEFVLPgF/q 1RqYQLJzJyG8Qz8MALCzhgX3xWY45Pl9c+UKaIcIk0cXD8D78s5XneAZWZBI8Aof/C8V iiLg== X-Forwarded-Encrypted: i=1; AJvYcCVZvf1FznWrfUb1/suNUlRBFZrl9cARE9R+palk+S294p+UCjBQPndlWnXUXg6G+79ifdUdb84Ez1Llcw==@nongnu.org, AJvYcCX249PDfpqdk/GAiNXhyN0bAemKR60YKFEgOAXcykxd6jTRGegAmiubmFFKIM379hX6V8v5JQRrSQ==@nongnu.org X-Gm-Message-State: AOJu0Yw8FLJgFu5zsDU7+mCD5uc6S5Wtg6WU0y3c7vWFxVIP6Kx/IbhS FUC2G+Ttzktk1ZuUX40yGACbhNdandYfPG9pOQlGVfKVfiJ0tBXY66yX/+oOPQ== X-Gm-Gg: ASbGncvNSYW3sb0UnL4sjZqu4atoIE4c6c7eMTzn60ll6JpYexpO0ykwtuTcW0kPphF 5wk0KPU2RbXDreLagPgm3hmE6JkZR4R7yoHU6wBxceBSNtNxatqhRC/1bDXit1O/+1wrJkVUrhU sAWqK4zIxHsSQBT7YeXRMia1z3EWk8AqA4GRuTJVr7QNaUwtIQ1y30kYfImopnAB28apP3PFLH+ Wh1sucuudVZN5PYg2+J71hewro4kavCuKpRqHAQ91wQxXGibMgduOahkRhXfc+msurlb11UqYAT fTOqLAs+sLl/2BTe3xYlEP2TIK4bTuTNMBczpHcXl2C6j5oINLOlWWNFQim2hVz7dkIVui7p8Hy uo0ufeK2q1FkdPPxo8Ij2VEuheEYLhhMg X-Google-Smtp-Source: AGHT+IFTP42mQXbO3bVx0khj5I4eMKvfb49LTCNklV65+sAfcX5ZtwtVIu4pyFy2Lm9MzV1oMPfdlw== X-Received: by 2002:a17:902:e810:b0:23d:fa76:5c3b with SMTP id d9443c01a7336-249448eb7a3mr92183895ad.22.1756727133912; Mon, 01 Sep 2025 04:45:33 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , 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 v3 02/35] configure: Enable to propagate -sMEMORY64 flag to Emscripten Date: Mon, 1 Sep 2025 20:44:04 +0900 Message-ID: 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::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, 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: 1756727240191124100 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(-) V3: - This commit is under review in another patch series so please ignore it here. diff --git a/configure b/configure index 112e4725fc..8ac6cacc06 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 Tue Sep 16 03:04:09 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=1756727233; cv=none; d=zohomail.com; s=zohoarc; b=AOu+PNvY5+mBAITeGccjxIXFLCdLgbjAfFVYsHo8Qc9Y1ZOKhwKSD5ISpjRMAAUcv45s6UpC/qxu+pDnEOV+erHbvFLlqinCj0N3G4/TYpnO2okcDK2SmvPFZVFSgLZ+FBaDIj3LHj5K30tDrIgdfb8E3aI4uyXDTiCe7GGkRtk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727233; 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=io12k5COHOK3Yc/em6rq8KtQkvhygCFM2FsEAygkmkY=; b=etaj3b2yNoZIQ4tTUo9+SBDWpGCuKpLHgyVejtpVRShOeuPfE85uLJdnOG14cCxn6i77aiKeu4wy+ig11jmClj+kbHQTypGl6z7R8D/aEK32HgrH7oPuu24QjLFZy5A2r0axa0QO6dYwtd1sciJNZGnaczD8YeFgHb6FKkGQ8wc= 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 1756727233926724.671662468623; Mon, 1 Sep 2025 04:47:13 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut2zP-0007G3-Ga; Mon, 01 Sep 2025 07:46:23 -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 1ut2yx-0006mQ-9P; Mon, 01 Sep 2025 07:45:56 -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 1ut2yo-0001IZ-44; Mon, 01 Sep 2025 07:45:54 -0400 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-2445806e03cso47820245ad.1; Mon, 01 Sep 2025 04:45:41 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.45.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:45:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727139; x=1757331939; 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=io12k5COHOK3Yc/em6rq8KtQkvhygCFM2FsEAygkmkY=; b=XPNaqIGaD8PNDWxeDI0+8eZSR17g8twtl8L22t4tm3B6c+8hm+LSgEjcInDSMr/PLq 9+7PbDZkMPxWiIkmVkRbMalv6/jNd2Wbo/rr5Fw38PHTBpD6LYuxJsL5ou5Im+pGqDK9 K7HdeFbKL7kYJFWLINodS4KHZStbSZiPe6IRce/HIDJyavwsGq0fl12YGj930DHrXMBk urxKe6pXutw/qkOeABci0B8N1/JLw1+q7uYy4nhooDMHudUB5PYbUGvGYE73hp7Fe+iZ 4kDhFKpaqzkr9B/VJrKQ9oHiZMbreQs1DxXhF7UMnWTRRl7C88JSJiVeIKE5nPubP+0C xeKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727139; x=1757331939; 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=io12k5COHOK3Yc/em6rq8KtQkvhygCFM2FsEAygkmkY=; b=oSdjNgRU+4A03AD7ryfPlo6AwPB1drixGvOKt1qkmR2Z5R4uSV4eXScjCWdKqsmDV6 KiKGkMCpRwQPAHgORfWhwsF4rusIavIj9C1WBpPm6qru+lOgtz4XXw8db1HNTAXtzCX5 /BrMUkAGcYiCNaoefu42GcQ7QFud2QJS4KSSHrXNU6ZwS0A82BpS/DiJgOfXwj1nZWWy IsomVUNXzclM1SZhTP1wU6yVxG9MHpKNK2CHcF1NDAIjEy5xzNXrbNDaWUJo+hC45sg/ bPk3XEdKjokYRG29MDfV9fG7pHtb3U2g5RlSOB78mrUFuEEyWlOqAsc8u7BDmj4y2Dvr 2r7Q== X-Forwarded-Encrypted: i=1; AJvYcCUm+MtUO2O33vpVnbLyHpKxkWDX3Qqw8Ih9K9mcIKIIfQCCG5Ln4NSJMy/2d3bcz/Hkw+aAsqoINw==@nongnu.org, AJvYcCX7uD8VzcOpYVej//qmy5rraeCIa49W83t/1zwCwpH1wtSN7qJf8ZncT8vOR4EF3I3e49CN8Yituom5IQ==@nongnu.org X-Gm-Message-State: AOJu0YzfBMVSoMqnAEJA8wGJN+BnkhMIRrMHnkLBZ5WrTakWl65IA93q 7d5O9aG3WAQW34VQGeZPm5zv1DheREaPY/lirffRwngGNAQqGP3uL9Y1ehHzFA== X-Gm-Gg: ASbGncsUOJFQOKiVLLZwInyh6qzJBfs7GDC+Ewg6g/jrSYi1d88K1SWhLFPCw6LqXa7 Lqa+0dF24jg/z2GUwHOl5s8hp8j6RG6NUKwRU0wpC5c2NNzY4bdx4y2c+lWMSkurDU0ly2e14U0 S3vKMU6QqsknTKv3WEpujp1vWYdHBzMG5aaCMIcnfHg4bmFFkLn8bHe4V8JF0McLcHJKJz++d3U NgZU3RCoPZUgabqNrZtQKxwRODROPgrX85BBsaAboMjWeNI6tUoWoxkjnl/fudp0rE6a3IgBUp/ 8U7GIN2qE6tdyqPpCN3C4TocXfqWhlVeetN3L8ATwREVNetIkcAu5ZyhYBJ4USlOXWZ0sV63QvX JTTqKRpIODCOgAG088r8fYoEbgRMHyNG2 X-Google-Smtp-Source: AGHT+IEvGePlQn9QMKxmtmqoa8FIVGL1cCpeRfOkWdgU8ix3zc0C3oTbwMqiTJ9WQn4JsCCwDGwPmA== X-Received: by 2002:a17:902:ce09:b0:240:8ec:fe2 with SMTP id d9443c01a7336-2494487448fmr85433825ad.4.1756727139512; Mon, 01 Sep 2025 04:45:39 -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 v3 03/35] dockerfiles: Add support for wasm64 to the wasm Dockerfile Date: Mon, 1 Sep 2025 20:44:05 +0900 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::630; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pl1-x630.google.com X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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: 1756727236292124100 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%) V3: - 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 Tue Sep 16 03:04:09 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=1756727325; cv=none; d=zohomail.com; s=zohoarc; b=lhsN/UIoPhUS/7Py6RlYO8Nz2ttfIzdIpSBIaVRgSQn05+CBqGh0BmbXqb7ILnskqsjFBvZJbTLCsfQ6RSGf03m01A03fztR++y/UiduhikY/szfDyGqtn1rV1QREhPxOLUZIZmLYXqE4JHswKL2gJp+oWCoVr7y9KSRETd6spQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727325; 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=WES0X6tSGNzNPhgOsz9aAkJISTE6g/V5BmTgRuV7mUk=; b=TXJfBq65fg0Zbdvxwz8eM+O8yftO0ZpEofusATAI+HXZo1Cm+EtsqHn11Yg6lZXMj5t6xKMmuPdYhkb5XKi2eNikFBI/2l7IR6UlQMjRAstiLf0NYCVecQwd2+jiogYhmu6d2V2nxMyT14Rxn9StuCIkcwm97ymwRN/f+vw7oAk= 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 175672732525321.522158709224755; Mon, 1 Sep 2025 04:48:45 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut2zp-0007zs-CL; Mon, 01 Sep 2025 07:46: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 1ut2z2-0006ol-6g; Mon, 01 Sep 2025 07:46:01 -0400 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ut2yw-0001JP-Dm; Mon, 01 Sep 2025 07:45:59 -0400 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-24a1270e0deso12881415ad.1; Mon, 01 Sep 2025 04:45:47 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.45.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:45:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727145; x=1757331945; 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=WES0X6tSGNzNPhgOsz9aAkJISTE6g/V5BmTgRuV7mUk=; b=DqcoPFdj+AWXPDnW23HymEIhKcDg3Wfwh3JrAO9BEgh/uwair4d48xhWxifm54okC9 9n50YPa7KXM5cqcTqgjF1DaMNEtvp22DVqjbABdVndGSBBlRBP0nTuBDL1JeCgANN4VS CFjTXlibZ4A5tiWBJSl2d6cmZB3vJ78N5JoAu1bGUhdU07ffkbPqWM37MjNXSpc1s6MV H39skfg1qn0uKhCToOyKmq6VybTh/9zBsVnd30q/tFIGnJyRLZaXv0u9VtfQra8qmZ9f JoLgdTprrMacGWSkl0TG3itfzwc/RxHHwHtuq2t6kXolALT1YMpUEH4nTSga1cn3KtZl tIFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727145; x=1757331945; 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=WES0X6tSGNzNPhgOsz9aAkJISTE6g/V5BmTgRuV7mUk=; b=I4LNDap/LbK1EHA99mwOufdHymqoh4wlqLabH6sth+se/yXo2hPlYvpcc32FvIQ5up TuOKrFOVriWxWK3waBy+e+H6Og3D7r4mN6y4kXwVWhioU//QUlDVfmNrlWfIbVZH1sqM 0KtrwV1c75cPqjE2EtxUoQqGueglD53O0h7QTtwSlIaD+rGwfwVVdRdkKV9UJ4rDHfi0 SVmSktuLBQRJaKdmtALRWnXHfG/Iw8ON85K2fqIG9cupISnNzQnVDNlDaQ8KSDSJBs9q wrSJP850618AmWz7Pr+es/fL5GzVcUzou1Rq+jSEayoWKLTMGryte7QQi81uxpVvniLE Xs3A== X-Forwarded-Encrypted: i=1; AJvYcCUlbf19HjF+4NHUBCT5tFS4yOLyE98CRhjCU+i3Ygg+x60WARisnKGCnPUZN8ykFp6+3TznbAKjbA==@nongnu.org, AJvYcCUywqYR6VzOqSSM+DH0RXkrnuhKF9hIg0Dc/4pce+0Subt6lr8pqgsbWADFrd98ojXwOeoPieP4HlL7Jw==@nongnu.org X-Gm-Message-State: AOJu0YxuvZXFFUJrR/08mEdULNnq4XyBPh5wNH4OmhjP63Qw2ROP9TVv Ax5KnuF6nvZwH0H9FxrjuitpxTlQVxeqc4UyyvYnk6wwvoyP27qAm0Sxnchjdw== X-Gm-Gg: ASbGncvxMd8mzHyStboFnT1zo3gDmNXSU5+OOxPkWYb6VRNo09uWL4br4EAOiIMql7Z Ywq+69SaWzZ9sUY1AVE8v7vQ68WPKT7+9myGS6Vjgjy656sko6Mn9qJTl35cqP5441m+s4wXMox Dn1Berh3sIEIA46UeXgEwpLdiAkZiI+YaTvTWvFYRpOSRkOarAAjKo36/OP5dRCQ1nxuDiYhr2P D6A086YW0EC6JMflmRddRyIZmUMMCxIsc2hfT8Eei6dVi8aPwKC2LNlHGM0nKlgWQO+6ZRZMAF9 9tO4wDtIVSkMkFa6YfkUDPDb9Po2VVjCEtP1TtY7bypFhefLhmPXL4H1gGld9nE9sUPB4vyjBQt u860y6dEM+vC7DN0cuFG2v/FT1QYntQ/ol4o4wo03ggE= X-Google-Smtp-Source: AGHT+IHDhFRJQBYt2ZX4BzXIeOLbiQkOdlyOuir3bILDtDa/NmCbPUbnJ/plEPsSK9snQ1kxnmscMQ== X-Received: by 2002:a17:902:e5ce:b0:248:ffb6:5bc1 with SMTP id d9443c01a7336-24944a9d689mr109011385ad.35.1756727145453; Mon, 01 Sep 2025 04:45:45 -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 v3 04/35] .gitlab-ci.d: Add build tests for wasm64 Date: Mon, 1 Sep 2025 20:44:06 +0900 Message-ID: <74f75ef5de887b7e27ba9d17e21fc083ba99fa67.1756724464.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::62d; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pl1-x62d.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, T_SPF_TEMPERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1756727326774116600 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(-) V3: - 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 Tue Sep 16 03:04:09 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=1756727378; cv=none; d=zohomail.com; s=zohoarc; b=JWzUDbN0ybrT7ro8ZnJVIWAwGQe8izEYlqZ4+gMe6z1rfgAgBls/SjBjnBIdnD3VfYoGod2DwU/1nkDkhsCMYRUq/GMpIwgBvSWJSVymp4MdpXKH05ZUwhEM9vKufMPnh2Ky7MGzCKTOF+dMK0wnt22V/YSkKQdhAO2vDxyvGTI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727378; 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=MJlrySIO4BSZhcIFP/jHtysQnEYOUCLY5ts4W8Z02P0=; b=O0bj7jkzj/ySI29KM9eDOYluD7pP82hfKK/QtL+bjFml9bc+YeYDmIGNcpTW2aFK1AsqohU5UAVRmK1RVG2qRdCzI8QPMTkohjMSCgcv7YYG1hz2aKUNbZ2KUyAhKsR+E1LY2Un3m/f7dUreoUz7zYeNMJ6azEs2talkjDMCR6w= 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 1756727378282313.2134065847978; Mon, 1 Sep 2025 04:49:38 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut2zY-0007X7-JE; Mon, 01 Sep 2025 07:46: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 1ut2z9-0006s9-TE; Mon, 01 Sep 2025 07:46:11 -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 1ut2z4-0001Kd-07; Mon, 01 Sep 2025 07:46:07 -0400 Received: by mail-pg1-x52d.google.com with SMTP id 41be03b00d2f7-b4d4881897cso1800309a12.0; Mon, 01 Sep 2025 04:45:55 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.45.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:45:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727151; x=1757331951; 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=MJlrySIO4BSZhcIFP/jHtysQnEYOUCLY5ts4W8Z02P0=; b=B8KVSunI4bO116tMB4q4xnJt4heNpxogCgi3R9Q/RW7PBQnuTPhHVlLtPTbBQG4Ye6 PHUMdUzEu8yuDo4D6IHYggYK47V9VnLEy3/4viEF6BsnV8LaMmfqx22GbA3Mu60SJA8t HvoDtP2+f3/YXpcyoK9IjpOG2eLIqEdinkaBLMLeYUamoUGPEn9WTrpIzzdEN8ggbQ7S 8FLFku2ZGucUpUb1fH3VTmtjJZnoJnKPyy52nN7nVeWOB6r8kYFVRjkzOl7BR04Dj1EB SFaDzoc03SLntiSlaxgURNOD5TMWdehsYyQzuldkOdT5YIWqzvYKM2ARYJsSVEH/F3Sr WTVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727151; x=1757331951; 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=MJlrySIO4BSZhcIFP/jHtysQnEYOUCLY5ts4W8Z02P0=; b=ampYP6/pL9ggbhrQRx7W6di41cVwQBaMMaI8QFnx+cdwLbWItsc5/F8gtg9ceyvgmm kfDb4Jc85pDcA99qFkZtiCGiAvi2DlfWNDdJq2/Mi+PLl/VwbauhqYBt6mQi65/+l+/Q 4KfjsGNHgwo9JupzzfruWHIFju0xBxDuX12ZGNI65ESlYVYdvVui1KpYVBNspuexwQ+Q nCg31Z4isPX/nv3efA5/m47Hzfjz/I5A2qRXpC4aPPqVoX1b0RG2W/DBrYZfIPMaIxXW JPhsKRIXqGtt+IZ/so+PEMh7EQx4eRJ1gTryWzsmYwW3OYqr+OVc47BPn4wpfYS11TAk Gq5g== X-Forwarded-Encrypted: i=1; AJvYcCU7FWTE9LWrQtbngual93upmNgCE8cEUKQwT13KpFK5kM9t09yYQ7WKz6AIp6rsRz+S8i8yuCZHqA==@nongnu.org, AJvYcCWAHerfjDQhtj+c+zIuYWnbFfrllVjsZDpbCmI7hKTz44U3ULq8Ljx/L4v+usUJHa+yfhOm6sP7cj+eEw==@nongnu.org X-Gm-Message-State: AOJu0YxlBn0XBdiBoB7LQm8Exd2Bx9qCNsVffli0/Chm1OG6cODxbmJ8 U6vjGg1fOMcrSsO+2xpCn7c+MaRR05XQdkOzwfI/gCCLyQ/QvpH5uhZhlyrXQA== X-Gm-Gg: ASbGnct2SEpprdwBPSNDhwpBZJailPfNbBXRPK9oXRlj+dgGXZiXFqYwCrFcTEqwYIQ Ne7b/vS36Afmfg9UMltjBJO65LcReO5dal6eqebLGzrOTzttYWuuet7ttTETiwlk1VnfpWD4Jea NRbViA/xQTOTSInz/WFk63SPCPScn8MWersjJ8nIxN9AKCT2Lf2PxUudoaiuCuIHfh9mPmrx1DP odJk9usqCZD7k7b/Gcf2Vi+7mSF95pLiix8Sht6qptzOUddL7WOPcKUE9MsUvm6qN3fxKeOxaXW 81giNl1qvqwcfl+lgMum3/PemxRBIDmLHSS1s7abYOD8Q2OVw01QU7u5FI+LbCSTpvZLuT247+5 mV6W9jMmujpfNVGwarncAKr3Ek+Pz5wvS X-Google-Smtp-Source: AGHT+IFW5pXptoNkvEWfX0rC+eB76VvDqUzzsHSUf2+rrT0WbJDA7OQuA4qZ+JSj5xn7OBcwyTx28g== X-Received: by 2002:a17:903:2288:b0:246:ae6e:e5e4 with SMTP id d9443c01a7336-249448ded1amr94443535ad.6.1756727150962; Mon, 01 Sep 2025 04:45:50 -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 v3 05/35] tcg/wasm: Add tcg-target.h and tcg-target-reg-bits.h Date: Mon, 1 Sep 2025 20:44:07 +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: 1756727379268116600 Content-Type: text/plain; charset="utf-8" The Wasm backend targets wasm64 as the host so TCG_TARGET_REG_BITS is set to 64. Since WebAssembly instructions vary in size and can include single-byte instructions, TCG_TARGET_INSN_UNIT_SIZE is set to 1. Signed-off-by: Kohei Tokunaga --- MAINTAINERS | 5 +++ tcg/wasm/tcg-target-reg-bits.h | 11 ++++++ tcg/wasm/tcg-target.h | 61 ++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 tcg/wasm/tcg-target-reg-bits.h create mode 100644 tcg/wasm/tcg-target.h V3: - Although checkpatch.pl reports the following error in tcg/wasm/tcg-target= .h, this file is based on the TCI code so it is preserved as-is. > New file 'tcg/wasm/tcg-target.h' 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..89e4b51e22 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -3999,6 +3999,11 @@ F: tcg/tci/ F: tcg/tci.c F: disas/tci.c =20 +WebAssembly TCG target +M: Kohei Tokunaga +S: Maintained +F: tcg/wasm/ + Block drivers ------------- VMDK diff --git a/tcg/wasm/tcg-target-reg-bits.h b/tcg/wasm/tcg-target-reg-bits.h new file mode 100644 index 0000000000..3dd821691f --- /dev/null +++ b/tcg/wasm/tcg-target-reg-bits.h @@ -0,0 +1,11 @@ +/* SPDX-License-Identifier: MIT */ + +#ifndef TCG_TARGET_REG_BITS_H +#define TCG_TARGET_REG_BITS_H + +#if UINTPTR_MAX !=3D UINT64_MAX +# error Unsupported pointer size for TCG target +#endif +#define TCG_TARGET_REG_BITS 64 + +#endif diff --git a/tcg/wasm/tcg-target.h b/tcg/wasm/tcg-target.h new file mode 100644 index 0000000000..f00761d19f --- /dev/null +++ b/tcg/wasm/tcg-target.h @@ -0,0 +1,61 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Tiny Code Generator for QEMU + * + * Based on tci/tcg-target.h + * + * 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. + */ + +#ifndef TCG_TARGET_H +#define TCG_TARGET_H + +#define TCG_TARGET_INSN_UNIT_SIZE 1 +#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; + +#endif /* TCG_TARGET_H */ --=20 2.43.0 From nobody Tue Sep 16 03:04:09 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=1756727252; cv=none; d=zohomail.com; s=zohoarc; b=evhxNZYaLQ8IaGmIghr6NUiokClFSevfzctUo+gnsjqzwMs/iDgdwJ4kyycH6OVJKz4+OOQ4w4rAvfZg3j5LU8DejUP5PqgI7+dU27LC82X6Ok1+v4w59KDXC5zhEN0YQB5Q0FCm1NmY2lfgGsIT6pQ53vNn6FN6HlqpslH/wOw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727252; 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=UlFgd5lXojT+BzMbp7XrzAMP3YD6Ncv6gWAYJEjREGE=; b=bdLQVbIn3p/e1PQECwx6cnzH1N8Y1QcU0fTqFGm9E1oU4ugq92wRQaL6kgX7EBFvSBqBPEhX2ptYY3vioAcjclrWkcywj+ZeBJUsJgZzyhPW8L49NRqPlL/4uK+WfbcUtH4HkmcVBO1izo51WQr7VJqYVnNllA74n1crCMD9hDk= 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 1756727252415266.7370342418076; Mon, 1 Sep 2025 04:47:32 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut2zp-0007x1-LU; Mon, 01 Sep 2025 07:46:50 -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 1ut2zD-0006sZ-Bs; Mon, 01 Sep 2025 07:46:13 -0400 Received: from mail-pg1-x532.google.com ([2607:f8b0:4864:20::532]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ut2z5-0001L9-7e; Mon, 01 Sep 2025 07:46:09 -0400 Received: by mail-pg1-x532.google.com with SMTP id 41be03b00d2f7-b47175d02dcso3633218a12.3; Mon, 01 Sep 2025 04:45:58 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.45.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:45:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727157; x=1757331957; 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=UlFgd5lXojT+BzMbp7XrzAMP3YD6Ncv6gWAYJEjREGE=; b=EQCDXNfJ6HjdIVBmxrTXcyYmlRqkYhQnNAy/3SrdLmtPOhSIIIFbszh7qjCZSmsIow jV4lfCdGp81c9qrY5zavkMIDwsfwVNOJLFTC2pIG/OdKMvME4a3u8J2pQbap9gLsgDWG x4OpCFAr3iGSPncfAny5BnYZnWpcR+b3zHuRd8+b3Q5cQGdqMaQa+Y05jhaLWu8D9FpZ 1ABRVxYHQYNPxTXp7NP5snn1SOQ4ymya/M1XZRlzGXz9OuLFCVM3JpArCnLG7yD3QXD4 4JH+HZRC+aaZ8o2HzE4WlJBsUbkuFs+6jFeCoJLluYoLFjNBu8eU0gY5EYwgbsuKC8Ps 4KIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727157; x=1757331957; 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=UlFgd5lXojT+BzMbp7XrzAMP3YD6Ncv6gWAYJEjREGE=; b=u2Of6GuO89vGRX48F7nQopqzfBpvbMdK88C3iSkCgbuNh2NMNlfcNThP3hPpaTku9a b+aO1oCaD6vm8vZe0DB2iLxC2cSLGnkokBmuzaR7mM6N82p0KJaUWbntGiTiRl537n39 tEvTao55z7cxpmkNWDPpzAEl7aG4mnWjN+TdQ60fUWYSQToE7vxe1EUlYZNSlvgqOjcw SIgCLxvXvZSTbZfaDOZMpPevGeRjsmLAfXEPQ55dAE49nH1wLHQfIYLDLxdt7X/HWD8/ rkD2xugkUnamWiBvkC32NjWbtl77xG9oP5pnSoYU0E5rArstrgCIEeGTK1l0L1y09ZLX 8f8w== X-Forwarded-Encrypted: i=1; AJvYcCU9VnJJOY8X3UT8VpYFoRzy9bftIu4bBQ21ouSbTXr4BMY3NwrUEuTwGpV/OsNiIDpkD7m8OfbYhw==@nongnu.org, AJvYcCXzQ7SRjJkz3hFjH4fN8LXoxM/aEnQNPb3GMunhhTf45mWaSxX/BhOAFZUYhH7eQtw49c4nKC2lsvD5Kw==@nongnu.org X-Gm-Message-State: AOJu0Yyy66TKX/Z6vLbhCofknEff6Ln7KkZZFRwF8VkxaNKGjpZRXYyL GvDygghpXRK6R/QdtOTvUZCwdL1HRi+RebQRp6cQbAF/C0SliJlt2jsDq13wig== X-Gm-Gg: ASbGncuvDXOGX6SF+qewR99r4temKnNdX8GX4a09mB5PZPoTTxTGUIomuNGwQ84RXEV o6y78lOM+uaz8n6OtZ6VPj+6vJtKouW71PZuhpk3gqxq5woCc+i4kjoKesEAv1l1M8aMaLCuM/g A1hWADLNnODAVTZ5Uj2kOEo+RU0hDf+i2ZXlXj7UixeE68c9sdIMr40CB3BYcgfpRk/3cGDc0Kw RAUnLTRBkb/8qJbocue/dWhB5WtI4d4thiYwjZbEh0GyHEc6pXJLpclaTyPIsSZjFqs0/nMM2G8 M8sC0f0H85aCKtC1PgYABWMFyAal2EgcgAXmzFe/oKrsZpSLQSn5ZTtoABQhB0lH10bD0H0FcTJ QXI8qfd3DiOYMQ5m08bAwQuZ27ZJZSzmF X-Google-Smtp-Source: AGHT+IEkUCnP6JXuWT8gfRp2jur/WnSaq6ah7CS9FpCZKPdUEINQcJ234j5iV+DIBRSHhcjK9DgbsA== X-Received: by 2002:a17:902:cf43:b0:249:c2f4:5af8 with SMTP id d9443c01a7336-249c2f47c40mr90316775ad.33.1756727156619; Mon, 01 Sep 2025 04:45:56 -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 v3 06/35] tcg/wasm: Add register-related definitions Date: Mon, 1 Sep 2025 20:44:08 +0900 Message-ID: <1582a3fb83f9a0f12a988bc159319362f69676e6.1756724464.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::532; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pg1-x532.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_PASS=-0.001, T_SPF_HELO_TEMPERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1756727254292124100 Content-Type: text/plain; charset="utf-8" This commit adds the register allocation definitions and register names to the Wasm backend. As in TCI, call arguments are stored on the stack buffer and the return value is placed in the registers R0 and R1 when needed. Signed-off-by: Kohei Tokunaga --- tcg/wasm/tcg-target.c.inc | 77 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 tcg/wasm/tcg-target.c.inc V3: - Although checkpatch.pl reports the following error in tcg/wasm/tcg-target= .c.inc, this file is based on the TCI code so it is preserved as-is. > New file 'tcg/wasm/tcg-target.c.inc' 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/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc new file mode 100644 index 0000000000..3affc17232 --- /dev/null +++ b/tcg/wasm/tcg-target.c.inc @@ -0,0 +1,77 @@ +/* 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. + */ + +static const int tcg_target_reg_alloc_order[] =3D { + 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, + /* 2 of these are call clobbered, so use them last. */ + TCG_REG_R1, + TCG_REG_R0, +}; + +#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 + +/* 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; +} --=20 2.43.0 From nobody Tue Sep 16 03:04:09 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=1756727522; cv=none; d=zohomail.com; s=zohoarc; b=mQSmF6fXSPFPydovlMoQV435T2TcjrLW/0bzT8p8ZOit2e2XLtsXJFfHEf3xd/juhydl7810CxJ8tRtIUQU9f5oSG6yAzyoA/MvqTf60i+E5msAd/NDJVTMuF9nxk2o6tVShRxQhx5U3FPvNR/gSk3EBUbM4KQg6qxvITsg9wDI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727522; 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=CKRYcSwafDNcekCgV+d38XwpLUOrhRn0ySHzx6DTOfk=; b=IQlkps57BTLzSDeFD7nLpWkKkkl2E4S4k8Hcn5vKYixXRwVM1aG9lSJ+c5HNNGDsHh4Id4SenMr3spqam6EGoGDgJOjN497YNxMLb0ObbtyAYOmwmY389QY8/wCyHhCRCQ60KEXy7mycsmpHE1xCw4sAKJ6aETV7odMlviii7WU= 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 1756727522631439.69445037249966; Mon, 1 Sep 2025 04:52:02 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut2zx-0008Gm-DY; Mon, 01 Sep 2025 07:46: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 1ut2zP-0007O9-N1; Mon, 01 Sep 2025 07:46:26 -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 1ut2zF-0001M2-Gg; Mon, 01 Sep 2025 07:46:23 -0400 Received: by mail-pf1-x42d.google.com with SMTP id d2e1a72fcca58-7722c88fc5fso2387746b3a.2; Mon, 01 Sep 2025 04:46:04 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.45.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:46:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727162; x=1757331962; 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=CKRYcSwafDNcekCgV+d38XwpLUOrhRn0ySHzx6DTOfk=; b=e9mkQQveZYZttx45r0QAbepTFEnh4/ksbLthEm+5p2PC6pGHH1Ie6MUaMafCeEiC8n CfVeiVJIgLV+c/1yan2UpUf3Ixs8cjHzjM4e3+neHNCZ604qgdR4/ghmfrQ3JHD05PYt RVyPzXyQscWXVx+rkzGBxldqcrygfDeBCkQMto0Shj4jnQfkoLhg4JkrBaJbltVEdqFo AAbHsbPsNzbNo/ut5YPPRGKAUQCA6MHyofWbAPUK65gXqb2NlSvkC9V8ioxofhbsgKmY gePGsYXT0zCj5z/CIJS1RlLJWypj5OP0s/syXJJQFGNbq0RwRUyyHyQRbGEnbfRg+V7Y weDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727162; x=1757331962; 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=CKRYcSwafDNcekCgV+d38XwpLUOrhRn0ySHzx6DTOfk=; b=US09Ya+rvyslys86aNfPt6i1WqWTOKaoLquTHu3tHmmroLy2GJ566OfPMlU79faDQt /AZqozyZ50rvBoz9pfaYe1YFMvqt0zJZvUABWpixG8VxU0iTc8udLK4t311Cf9aQTF8L Xvkjc8nPtzju/AO0U88LM93j6L3Fi4DsBsSbZAywNSQXjgi55XZY1l92uMzPezn3ohdm z9jPvebEdU4XpHpV6xvA2QhvKuNazGIs9+/jq9+9rTIaqyfJRxtLhdEthuL2XKXpf3jY MgWoAy7ymLLd48WidJsN1lyYIuxTdpsUAiE1E6bXCNgWq1FIeWdSPW/YXQsGv97rvjMa WILQ== X-Forwarded-Encrypted: i=1; AJvYcCUWTPSGcvEUlr59MTeDSoD0zQxXGs0PdIsH5M4dCQ12ZnPic06sd6X/c0BFEx4LC7mVEJyDm1nQlA==@nongnu.org, AJvYcCVPJdHWAOf0nCjesMj5esE7mgu8SERaVINSu7UNm1LbwJML2keLY/9dA3eQ6/luxH+eO5tHjvgnhpbw8w==@nongnu.org X-Gm-Message-State: AOJu0YxE6a91tJPNBdBi/9T3BFIdf/e5V2s5K2VFxWoEeQyEufpTO2DM M4+lV6SV0iTUcXYxx9uazHocjEcYn89Rp6pGFR836baC36WMfUL0Y75mH5VOKQ== X-Gm-Gg: ASbGncuY23+IcUT/1g/PSQ6GQKVoCkZ4aBAsMScNmxkv7JoGx3oFgTxkqiNO8EAT+gF Lsc5OdQnESAHepTnK7ssOftn5+2wuRhA3hqtrN5eA1WWsZyvnF3IoDuMEK1hN3hbRt4YHkaYnPz fBtn7hVNuVtCtlRyuoUzl1zdyUSk5v+7Zn1xqkwXb4AUdOZ2LMP02OCz8u0YsVuKihS/aCfLoZR THR6623E0eElwYsQOtmoF0ruzfpF6610Zyc65WGvRmByOwIm/B0yjmUwMMgb7N0K9cRyN+9T/fU r3CmN9DrfR/MbK077Bu9O3Ne4Wvuq7F2PEaJteOIKEh2bAUDVR/X1bFsuoUB5oKKJQNL/LXFy7X qv5fl6DXvdstWWEl5qwnEqA== X-Google-Smtp-Source: AGHT+IFkunATfldHnVpyrPyJFgoJXLdJxX35nxoCHS8d7asV94Jq+xrer2nXFHFLBVHakqvytMDG5w== X-Received: by 2002:a17:903:2301:b0:237:d734:5642 with SMTP id d9443c01a7336-24944ac6b51mr105348515ad.41.1756727162233; Mon, 01 Sep 2025 04:46:02 -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 v3 07/35] tcg/wasm: Add constraint definitions Date: Mon, 1 Sep 2025 20:44:09 +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: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_PASS=-0.001, T_SPF_HELO_TEMPERROR=0.01 autolearn=no 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: 1756727523820124101 Content-Type: text/plain; charset="utf-8" The Wasm backend integrates a forked TCI so its constraints are defined to remain compatible with TCI. Signed-off-by: Kohei Tokunaga --- tcg/wasm/tcg-target-con-set.h | 19 +++++++++++++++++++ tcg/wasm/tcg-target-con-str.h | 14 ++++++++++++++ tcg/wasm/tcg-target.c.inc | 13 +++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 tcg/wasm/tcg-target-con-set.h create mode 100644 tcg/wasm/tcg-target-con-str.h diff --git a/tcg/wasm/tcg-target-con-set.h b/tcg/wasm/tcg-target-con-set.h new file mode 100644 index 0000000000..0dc41ebe33 --- /dev/null +++ b/tcg/wasm/tcg-target-con-set.h @@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Wasm target-specific constraint sets. + * + * Based on tci/tcg-target-con-set.h + * + * 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_O1_I1(r, r) +C_O1_I2(r, r, r) +C_O1_I4(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..21ddbcc01a --- /dev/null +++ b/tcg/wasm/tcg-target-con-str.h @@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Define Wasm target-specific operand constraints. + * + * Based on tci/tcg-target-con-str.h + * + * 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.c.inc b/tcg/wasm/tcg-target.c.inc index 3affc17232..0b12c4ea03 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -75,3 +75,16 @@ static TCGReg tcg_target_call_oarg_reg(TCGCallReturnKind= kind, int slot) tcg_debug_assert(slot >=3D 0 && slot < 128 / TCG_TARGET_REG_BITS); return TCG_REG_R0 + slot; } + +static TCGConstraintSetIndex +tcg_target_op_def(TCGOpcode op, TCGType type, unsigned flags) +{ + return C_NotImplemented; +} + +/* 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; +} --=20 2.43.0 From nobody Tue Sep 16 03:04:09 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=1756727480; cv=none; d=zohomail.com; s=zohoarc; b=jGrIk0JdlnjQ6RKqW9yiEaHyTdM1RSuAvd6eRGD77cdhabKcbUiZzbS6kPR4VFAJt9PQkUQ27MQYJj4t8u0/nGB+AOX9k4a2hMO+4VQ2xE2kAUiqGdJ+n14SJQkBr+697lmMt3MwbM35qZqrR8GoTuOYTZs0vX+23uAZ0JSIFg0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727480; 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=sf80EJqAcxGGTIu8UKFlaemvFTuxkqpoIGk43YfIQag=; b=chodyvrNQ6UG72jb/9Pf3EAdz2Mw9WkNNEQiwVEM/lLF69r0+2tufrnxTH8Z8gFU0edQysVD+Rh5hOMcTVgBzARhjPfl62Mo/xwNVU06zC+91eFyN39kdwhoikx8BFRu6JgvBWm8FblTch9+dQrmBwp8ArUwKHn+aMxuVsvBzvM= 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 1756727480902956.7201869621148; Mon, 1 Sep 2025 04:51:20 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut301-0000CU-KD; Mon, 01 Sep 2025 07:47: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 1ut2zU-0007WW-MH; Mon, 01 Sep 2025 07:46:30 -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 1ut2zL-0001Mk-Bi; Mon, 01 Sep 2025 07:46:27 -0400 Received: by mail-pg1-x52d.google.com with SMTP id 41be03b00d2f7-b4f7053cc38so828099a12.2; Mon, 01 Sep 2025 04:46:10 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.46.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:46:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727168; x=1757331968; 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=sf80EJqAcxGGTIu8UKFlaemvFTuxkqpoIGk43YfIQag=; b=SHgkvCMqBIMg71Eg1hlsZMUwJouBaNKdZyPGToJfxVwpd7awdQDjLSoqkYXl7DXJGp 362t4xnesIe3n2QNLa3Wt8p9cDUImtKcPAYF8twx9iwqq+GoIevoHQ2V2tDR152ugu0z /EVu3+wbV0nDqAKQ1266nzyT0K/fXDHwQPgb1U55VC7y78+2Aqi9fiBVjbkd+RwRCbbg cwPnW8Yw4wx8VxPN/8eyvUGtFsT1bSd5A3rPUybMAme/bZwcV06JKf1cZrrfQjN8IaiS oTlmg0z/MxU3MsOLPk72sg+31r7mF1yglUGPfzjvjVFEo0NauqTK26YjYbf6S8ZRjcSW vR/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727168; x=1757331968; 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=sf80EJqAcxGGTIu8UKFlaemvFTuxkqpoIGk43YfIQag=; b=mPCmW21/kCCZMnh+ewz+DFdFc6T4v2pBUIrONf8950pyNIDNG96ZxGaNfXkzSUg5B5 YpOz/0R5h0bkzcW6uAALJYIQb+3YNtnlpTr983XdgAUpyfgot808Bqe0WdvBLYKmyhmt j1f/yvuH4aCBTpAQzifJCs6oe1MaSDrWEaKaM18c/XFEG7IZglKJ5ucf365dXWNi30GM 0CKIIjnK1IFttltI5BaJxQ8O1mP388gK45KHhS/RSb9GoUfadtkInQoip+it5nOfBe8N i7QNiNZOTfdKPD7LQTY5FAtBs/C+WxYhuP59766q22EbcwpQnsI9EcEPw/RecxPwB/ge ZHJQ== X-Forwarded-Encrypted: i=1; AJvYcCUhJw6MD6b4vllsubdgqzRAjUa41wesUhZaB8j34iJqsFcUjAzWWVjmHS6XVwkwXEjSz/XrC1tYLw==@nongnu.org, AJvYcCV6uerWzGJUR5hfBX5zQEuoyttmPderroGKdxU9gO5k3qauo3N1gFOSknrLcpH2E6mWtts9G82FQTYrXw==@nongnu.org X-Gm-Message-State: AOJu0Yz8nqiB0whQKTVOQpa7kTuLYATaEzUdPpL4xs0RJYrJCUy6XmHf 9dupekhb3kfDanJdBrWi597W8z4ve2Kux5nOG+pzkH6W89qwkMqGFDg3hXpvJg== X-Gm-Gg: ASbGncuknGAliXz454XqcCamD2F7G2OPrRPXlenevTwBbXsBUkA0EAXJ6DGPTlxxOef 87Rta96LintlVHpf9ow3/k9f2VP0wo2gItdYIN0FhDoUnI5xMhnKy6Yw5N2IzPlGTXOFh3IO838 oPL3WyufwyUQzx1XQN2psPgwY3PDyoKUsAWGADEYu0ZE1a5Z9xxaSK/iPlI9DOmboKkSNqAQMOI RR2uYHoJUugoFbrxDAAF2G4XzhZcJY5P4/agm2CTRL5DdyeDHRMUF24yaS9JCsSA9yQ9no6F8eW hMV6aC6OHojSNaJH+718M8HisB7l5oJr5gy7yHVKaXVY/xGQZgSdojtxFYifnPFVsRseyQMw1wV lRXfZ8n7tE5xih49kBdFpWg== X-Google-Smtp-Source: AGHT+IGdXU5wRXdOqIhg618Y45uQWMtwucFyOoGbDeA5xtPKGeAEjtsNAssQQYXbjMFYh12qL1tbpQ== X-Received: by 2002:a17:903:19e8:b0:246:15bb:590b with SMTP id d9443c01a7336-24944a1fca0mr91298885ad.17.1756727167977; Mon, 01 Sep 2025 04:46: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 v3 08/35] tcg/wasm: Add relocation callbacks Date: Mon, 1 Sep 2025 20:44:10 +0900 Message-ID: <5baf88bab53be1c51736b8caad24111d386f3f28.1756724464.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::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: 1756727483218124100 Content-Type: text/plain; charset="utf-8" Relocation callbacks are used for the TCI instructions to preserve the original logic of the TCI backend. Signed-off-by: Kohei Tokunaga --- tcg/wasm/tcg-target.c.inc | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index 0b12c4ea03..4bcb594360 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -88,3 +88,24 @@ static bool tcg_target_const_match(int64_t val, int ct, { return ct & TCG_CT_CONST; } + +static void tcg_out_nop_fill(tcg_insn_unit *p, int count) +{ + memset(p, 0, sizeof(*p) * count); +} + +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 + 4); + + 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(*(uint32_t *)code_ptr, 32 - type, type, diff= )); + return true; + } + return false; +} --=20 2.43.0 From nobody Tue Sep 16 03:04:09 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=1756727424; cv=none; d=zohomail.com; s=zohoarc; b=D8dLVvSHVNBJHLVsLixcYcJ1cu+pudWMCWMif0pI02r93V3y0WBXDL3GVOthlFZNPdYxktJh0P4+KRjLMBh8cN3glW7+xQPJfwQdT77LEE43HqOInidUwu8s9Ccuh43f8qjwvo6LvGyGa3E/+kjKLDzIzrZUvOPqDXdTAUVAd18= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727424; 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=vMZeL4QMsFfiljq1rOi24ctLNCC6JX7BUMH0r5U4zqw=; b=VlBg1TNKraOFuJBkSoHZGfgQGew/JbBSpIFKCmMgzIdVgvDIcbngimWXB2T+2XdR3c+mss3NXLiaWQIt4nxoZ7jZ8VX3rmslOF886y9oaBKIVK6pHwixS4hX4fWE4L8S1PrTY+OVCyiHsGMVbfsLf/nwzk3hm6X8Q0pnlTTn/Z4= 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 1756727424656990.9810315939142; Mon, 1 Sep 2025 04:50:24 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut2zx-0008NJ-Bg; Mon, 01 Sep 2025 07:46: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 1ut2zX-0007aK-62; Mon, 01 Sep 2025 07:46:32 -0400 Received: from mail-pg1-x52d.google.com ([2607:f8b0:4864:20::52d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ut2zS-0001Nl-Bs; Mon, 01 Sep 2025 07:46:30 -0400 Received: by mail-pg1-x52d.google.com with SMTP id 41be03b00d2f7-b4d1e7d5036so1804776a12.1; Mon, 01 Sep 2025 04:46:15 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.46.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:46:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727174; x=1757331974; 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=vMZeL4QMsFfiljq1rOi24ctLNCC6JX7BUMH0r5U4zqw=; b=fDjexBbhvVlSzHZWYjzoej3TtLVjBtGca4CllNOLjsYC4/BkUYKOW/kcOh0hXWHLvb TeH2jEpzNNpctqywP79B940A3LXB7z7XgetkwZskqBaW5mWhXgiNVKwM1WjU2sm0NC1B ktLleYqs9Acs+cFQWsS/4ndg0yw08KOAaK05ENSCydOecNk6dWg05m4p++g++yD7wvxu 96kmXoAXiNHDMlhL5uSKzg0nCeyZjzPUZlc7Z5Z3s1i7AKShNHM63W5DRJ5g2+oXopHD DT3suFW/tv+tZ4+LNRFvnmTZHU4eQ7Y7WX2oLqOv/rI2hCmK15FEUbC+jyQUN8HOf2cF MsSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727174; x=1757331974; 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=vMZeL4QMsFfiljq1rOi24ctLNCC6JX7BUMH0r5U4zqw=; b=YcN/ZZLYJIopljQ7C2Uq2QIfocQH9PRD1TUBeAsKKTYSoeBD72V3b6AQPWE++N+EEW zbXySDGMJGS+GomeZiAcEfArpedAAnvSVtw+f+SbQ6nPgI3DMFt5cHh/emFPEQL28r4/ A4bvAj5LFhnch1ifkHvw0I6J66uWSBQyeDqTHWH7RUbgbc66ltPSbbtDsjKxNF95QPPL tKPo0gyz7p1Y8Vi2eLZEacw1BbXEf33gD3St9dA8DIg9laNgSbknIsXEMqN1tmLQUk7v gyBBhVpUPUufBcEEMbirwcGKeF7m3RgnqA0Q4KVwY6kyBQFtGiiL+WtoLauu8tJSfb3y RZOg== X-Forwarded-Encrypted: i=1; AJvYcCVayHVhCQYu3083mg5Akqg4aCjzrjWsHRDdvG/48d7fAbjqoy2O4eLTWDIg1V2UI27yWRogaqjSUVIr5A==@nongnu.org, AJvYcCXDjF6ARtyizLyiAHvWcvzmuGuHq3za4BpGr3wup4cDMBrRD2dtYMlG1sK5o7uG4KtnaZODzB9ADw==@nongnu.org X-Gm-Message-State: AOJu0YzzHckFZEi2/WgxYQxKHGyq9eKYGueeIKCWASvLb8YBvL1Eladu 7IhkyHANzbLOQoitvFwq07ic/PD93UuM9Et5OTDX7Nnsbo42nmfMmVnc4m/CjA== X-Gm-Gg: ASbGncs74+tgscLpTlLVJHycy4uFKr+Q5NGx2bBbsWmj9D9rDZd5mhQvfiKwbVa4fyD I8oIlWQnvCQTo6fPR3wZF8NLiYfv1Q1yiOdWTseWhG3i8Hg00r8fQu4SWijgFQdKP643oN0W0Vp 0OrRwl4hqwhEnUncVOfoA6HsIAa4Wj6IFhF+fU5vKR8iLBTm3OoooM0CmS1g9J8P52OYbPrumHD VnMh+ApHLpF43brOLyKioM3agsKyy4UHBdVqY3nKg2QFu95IKgaT0rjOk7GgqyqgVwobLDB6ie4 36oaNdE5I2LqdzhK4FJ1Si/G/zzrmNW8Qow7xVn6X2dBmocGyo0JGs6TWSInQqdawxyLMC057KX HRlM1r5OczhiF9xNYHLVNRLpN1nWXYogY X-Google-Smtp-Source: AGHT+IGy3HjJdd+8bZZcRad3yC6ILWjrjkHxEZpRVXjv6dyT3DFv3u2tLi4CIJi/qTWmLCRfCfnTQw== X-Received: by 2002:a17:902:e80e:b0:248:f2cb:e52 with SMTP id d9443c01a7336-24944a6eaf6mr99835365ad.22.1756727173739; Mon, 01 Sep 2025 04:46:13 -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 v3 09/35] tcg/wasm: Add and/or/xor instructions Date: Mon, 1 Sep 2025 20:44:11 +0900 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::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: 1756727425882116600 Content-Type: text/plain; charset="utf-8" This commit adds support for generateing the and, or and xor operations. The generated Wasm codes will be instantiated and executed in the browser. Browsers tipycally limit the number of active Wasm instances and the instantiating Wasm modules introduces overhead. As a result, instantiating TBs that are rarely called is undesirable. To address this, the Wasm backend relies on the a forked subset of the TCI interpreter (tcg_qemu_tb_exec_tci function in tcg/wasm.c) for executing such TBs. The Wasm backend emits both Wasm and TCI instructions. 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 intends to be merged into the TB before tcg_gen_code returns. In the Wasm code, each TCG variable is mapped to a 64bit Wasm variable. Execution works 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]. 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 --- MAINTAINERS | 1 + tcg/wasm.c | 66 ++++++++++++++++ tcg/wasm/tcg-target.c.inc | 160 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 227 insertions(+) create mode 100644 tcg/wasm.c V3: - Although checkpatch.pl reports the following error in tcg/wasm.c, this file is based on the TCI code so it is 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 89e4b51e22..217bf2066c 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -4003,6 +4003,7 @@ WebAssembly TCG target M: Kohei Tokunaga S: Maintained F: tcg/wasm/ +F: tcg/wasm.c =20 Block drivers ------------- diff --git a/tcg/wasm.c b/tcg/wasm.c new file mode 100644 index 0000000000..9f3b1344d6 --- /dev/null +++ b/tcg/wasm.c @@ -0,0 +1,66 @@ +/* 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" + +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 uintptr_t tcg_qemu_tb_exec_tci(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)]; + + regs[TCG_AREG0] =3D (tcg_target_ulong)env; + regs[TCG_REG_CALL_STACK] =3D (uintptr_t)stack; + + for (;;) { + uint32_t insn; + TCGOpcode opc; + TCGReg r0, r1, r2; + + insn =3D *tb_ptr++; + opc =3D extract32(insn, 0, 8); + + switch (opc) { + 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; + default: + g_assert_not_reached(); + } + } +} diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index 4bcb594360..a1757b4db7 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -25,6 +25,10 @@ * THE SOFTWARE. */ =20 +#include "qemu/queue.h" + +typedef uint32_t tcg_insn_unit_tci; + static const int tcg_target_reg_alloc_order[] =3D { TCG_REG_R2, TCG_REG_R3, @@ -109,3 +113,159 @@ static bool patch_reloc(tcg_insn_unit *code_ptr, int = type, } return false; } + +/* 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 void tcg_out_op_rrr(TCGContext *s, TCGOpcode op, + TCGReg r0, TCGReg r1, TCGReg r2) +{ + tcg_insn_unit_tci insn =3D 0; + + insn =3D deposit32(insn, 0, 8, op); + insn =3D deposit32(insn, 8, 4, r0); + insn =3D deposit32(insn, 12, 4, r1); + insn =3D deposit32(insn, 16, 4, r2); + tcg_out32(s, insn); +} + +static void tgen_and(TCGContext *s, TCGType type, + TCGReg a0, TCGReg a1, TCGReg a2) +{ + tcg_out_op_rrr(s, INDEX_op_and, a0, a1, a2); + tcg_wasm_out_o1_i2(s, OPC_I64_AND, a0, a1, a2); +} + +static const TCGOutOpBinary outop_and =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_and, +}; + +static void tgen_or(TCGContext *s, TCGType type, + TCGReg a0, TCGReg a1, TCGReg a2) +{ + tcg_out_op_rrr(s, INDEX_op_or, a0, a1, a2); + tcg_wasm_out_o1_i2(s, OPC_I64_OR, a0, a1, a2); +} + +static const TCGOutOpBinary outop_or =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_or, +}; + +static void tgen_xor(TCGContext *s, TCGType type, + TCGReg a0, TCGReg a1, TCGReg a2) +{ + tcg_out_op_rrr(s, INDEX_op_xor, a0, a1, a2); + tcg_wasm_out_o1_i2(s, OPC_I64_XOR, a0, a1, a2); +} + +static const TCGOutOpBinary outop_xor =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_xor, +}; + +static void tcg_out_tb_start(TCGContext *s) +{ + init_sub_buf(); +} --=20 2.43.0 From nobody Tue Sep 16 03:04:09 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=1756727608; cv=none; d=zohomail.com; s=zohoarc; b=Jh6EJc7/WmNhia7Qk8o51TQgxUjU4CyGEuyth+nCPpllQvdg3m725z22JMiiS1sXMoVDUq2qtk+g9ZOLuIOgCPCHWqFI1Mm51DOPKjcWzI5klqDw16IpK6/OCMNkQ/OWArwl7t1UGk7fekDrtb7O7RAGz3Y72rmaQKXDhHC8J8o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727608; 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=cZn9qlLs9yOI27f7R6L8FcBRbqHv/U8xQ6luXz9uzYQ=; b=FmIRTCXOUWGpbUz5sOk6zqJ+79GegS2xCAQrzze++J6dw/KS1TZTzd4SzDOogqVysQ16lMMy7LpCitHFJ0aqHpcI63mSy3cl9nzH9oj0Gh6OJr+V2LU4AYAZit+fARj6uPZkf4kMUk+uJRK46d+sZ+IZ0DyQ0wjg0ZK86KjcEhY= 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 1756727608240722.5233575161943; Mon, 1 Sep 2025 04:53:28 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut2zz-000058-M1; Mon, 01 Sep 2025 07:46:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ut2zY-0007aM-MH; Mon, 01 Sep 2025 07:46:36 -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 1ut2zP-0001OS-9d; Mon, 01 Sep 2025 07:46:29 -0400 Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-2445805aa2eso43006445ad.1; Mon, 01 Sep 2025 04:46:20 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.46.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:46:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727179; x=1757331979; 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=cZn9qlLs9yOI27f7R6L8FcBRbqHv/U8xQ6luXz9uzYQ=; b=BJcrpIVaRARs3trpZlTya3iMpeyJJ4Us7XNQK38j6pWFX00NZsXOaPVKj1UO1g47Pl NqAX48BqCL9unTuVz9dT+5VUNn/EYG1tLwiU/6q6mWl47i3ld1MbvljV0SHbSKx31+yK YWBYiNvR6llq+9UpetL29Syti4LT1k5II2H8xXluxvLoLG6RevX11t9LAQD1Qdsp1nyH a0yk7s3g7zTos2xjJ4PoP5u4rhutGkzXzjLN/n4zJmc6Q79gyjahyVmyi3/eIrGMoDYE lp/4ZQQOG5NugS/jOtevrD+5eEO5papFXHibxNUI6dPDYtfWDKr8Ty94OGzZMD5bxejN GbTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727179; x=1757331979; 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=cZn9qlLs9yOI27f7R6L8FcBRbqHv/U8xQ6luXz9uzYQ=; b=l5x4B/JIi3pstCgdxfYXbEBm9bWPhFV98aB2P3RYhjGzXgL3rDVruU+QLJMak0Vck2 ya6EHD/wGPnx30RFG++QIW7WluU9cRtvOp5K97GstFoLTBzaFrxYcvAuzNuaoM8YatyA XOPl/LF/vOTqCyf4h+EMYcF+JCwC6u3+92kv6YEWC1nl36VcO4s+C+SuXAkDHRdRFeHh 9lhOLFDMKkrGzAVBAbmQtEXJvag/4RZxwNtkAtQTtMBNCa9TB+328rSqmP5HJlSSXsZ3 aeww8LFu3diSIsKGcGFdurMdIyZtSsTYPTgAlEUrQo7mAb5q6u9S/b7marNcjcbayspw kf5w== X-Forwarded-Encrypted: i=1; AJvYcCUcUSs1l9XOajnf7UlxvQofhA5yYqfQJrBffKspWoLl0RTKPD+pD+SnEJtc/6zbUU1o2aVIbNATa18unA==@nongnu.org, AJvYcCWVejPjpaIFM2Z7A6VOniwQoTDmVjgPXIepR6cnZbH4ZDxOm8g18GTPBGWJ0+2ArZ6jbrRsOzC9eg==@nongnu.org X-Gm-Message-State: AOJu0YzicfkWRZNzW3GF5AqWuTxU1mPRvFk/gjRblsEpTo1byl7SJmyk 8uBQZaZ868CcViF3XuJMlpCqJ07pNHLORtgnP9uWsncBDXjKAHTMnOCXCfogYw== X-Gm-Gg: ASbGncvdYI15TjrokwVQoCxekZ5o978c2SPgwksffnsmGKmbgZfkyTqL6XHvBhQwEze /JEFHcEeo4LJEzXJ3jmlmSO8cqkfmZl1nWHoDWD8gzEMEaVZW80aGV/DFVSxRQTBVKqO1UcGZbG rk6agKKuAfoTIdItUyHgzNhimuf+aRa3XXxNaMOdWMmDKx/6zt4CTZjqY8f4U+aJ83bjaSbI0ns LhL+AqZUTK6Fw/gWw3CpO9e73522/bsBYWaPVWRMrCMXBcWs/oJCYAZUTrwrWceppJXEu9tyKG/ iSQdTiRCSaLYYRn8mT1oiEuqK0W6UQMk7Onrz3Zz1mBnjoLJDS/KALme5v5KLtI5tC12nRcDhfz vpoUgcZAoOJ8ICvQRHyqd0g== X-Google-Smtp-Source: AGHT+IGNOGj69YnPJyljEQ3eSAE85qVdseIh+Xg+uwtDUGTZ5WehUZhCZ22VhMbxHvn1wZFdwRgv+w== X-Received: by 2002:a17:903:3847:b0:248:e3fb:4dc8 with SMTP id d9443c01a7336-24944aa2905mr111738315ad.39.1756727179321; Mon, 01 Sep 2025 04:46: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 v3 10/35] tcg/wasm: Add add/sub/mul instructions Date: Mon, 1 Sep 2025 20:44:12 +0900 Message-ID: <172f143f9806e41742137d975b2b80eb20f98a53.1756724464.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: 1756727610654124100 Content-Type: text/plain; charset="utf-8" The add, sub and mul operations are implemented using the corresponding instructions in Wasm. TCI instructions are also generated in the same way as the original TCI backend. Signed-off-by: Kohei Tokunaga --- tcg/wasm.c | 12 ++++++++++++ tcg/wasm/tcg-target.c.inc | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/tcg/wasm.c b/tcg/wasm.c index 9f3b1344d6..ba8a89d920 100644 --- a/tcg/wasm.c +++ b/tcg/wasm.c @@ -59,6 +59,18 @@ static uintptr_t tcg_qemu_tb_exec_tci(CPUArchState *env,= const void *v_tb_ptr) tci_args_rrr(insn, &r0, &r1, &r2); regs[r0] =3D regs[r1] ^ regs[r2]; break; + 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; default: g_assert_not_reached(); } diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index a1757b4db7..d5cf324e7b 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -140,6 +140,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, @@ -265,6 +268,42 @@ static const TCGOutOpBinary outop_xor =3D { .out_rrr =3D tgen_xor, }; =20 +static void tgen_add(TCGContext *s, TCGType type, + TCGReg a0, TCGReg a1, TCGReg a2) +{ + tcg_out_op_rrr(s, INDEX_op_add, a0, a1, a2); + tcg_wasm_out_o1_i2(s, OPC_I64_ADD, a0, a1, a2); +} + +static const TCGOutOpBinary outop_add =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_add, +}; + +static void tgen_sub(TCGContext *s, TCGType type, + TCGReg a0, TCGReg a1, TCGReg a2) +{ + tcg_out_op_rrr(s, INDEX_op_sub, a0, a1, a2); + tcg_wasm_out_o1_i2(s, OPC_I64_SUB, a0, a1, a2); +} + +static const TCGOutOpSubtract outop_sub =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_sub, +}; + +static void tgen_mul(TCGContext *s, TCGType type, + TCGReg a0, TCGReg a1, TCGReg a2) +{ + tcg_out_op_rrr(s, INDEX_op_mul, a0, a1, a2); + tcg_wasm_out_o1_i2(s, OPC_I64_MUL, a0, a1, a2); +} + +static const TCGOutOpBinary outop_mul =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_mul, +}; + static void tcg_out_tb_start(TCGContext *s) { init_sub_buf(); --=20 2.43.0 From nobody Tue Sep 16 03:04:09 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=1756727495; cv=none; d=zohomail.com; s=zohoarc; b=WsdpGQFd4+2tNLRnuW03Y41tDdAkw/xvCZS7H04qcLY0ZsDEW94O6cMBECDk8FgGUgy6M7FLaAvi2iOfmIM+1GxNnizvlFcVstlWzvL/fKLrVQUiv1cesb+eI16iFkBPmWIEU+TtIJ2esIZJVes7YYE6nKiwSGFg0M+XXaQ/AI4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727495; 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=KhPwaGI2GoqW6tt9mOEN3xV7zJdvKH3shZBj9/JQG6Q=; b=MHFAjtXR2Zy8o2H41fCL24B16toqIFpqQ9wzOfK7V3cDCt4FurF+Y4irIJILyqvEq16CrJFOAo3jsECcJvbE2wxWDG5quD9fj53Kna+1+cDPJzSHvPcOcCAbrK0QikRbgfw77qCABrYIwc8Ud8oDLHI5cms/CSasym+Cj05jSdo= 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 1756727495743537.0517249551767; Mon, 1 Sep 2025 04:51:35 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut30C-0000VP-0R; Mon, 01 Sep 2025 07:47: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 1ut2zp-0007xu-J4; Mon, 01 Sep 2025 07:46:49 -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 1ut2zc-0001Pn-28; Mon, 01 Sep 2025 07:46:48 -0400 Received: by mail-pg1-x533.google.com with SMTP id 41be03b00d2f7-b47052620a6so3804922a12.1; Mon, 01 Sep 2025 04:46:27 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.46.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:46:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727185; x=1757331985; 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=KhPwaGI2GoqW6tt9mOEN3xV7zJdvKH3shZBj9/JQG6Q=; b=lYZmm3hEyUFU4v5ifn1OxXBmLeVdbn7jZMhyg9EB8G5V9wXzGZtImShUl/UKuCaGfN rY2UZwu1Zl5+I6Oc3zmScYx0VqbrVdJefrCZPziIX0D7XgaCBbDJi1rBVEjQ2J0OnhLh U5cYOMsOEW7Ec/ZjvDeNWmN7N0NzLUzgt12v/UmjASVwHDFlaNrkcLI7To29TgPapxK6 QTHlmL8PtKm1iaiCU+uFQG84P/FqStwBaiNXX/WV4uVg5KPOh9FvXp+nX3hKLbD0zNeX 826BMhRGvgXswV19opD6m23Tsj19If/1YxvrF/ku/YfhraXN+dkDs2mDoRckLy9HaQ8K 96+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727185; x=1757331985; 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=KhPwaGI2GoqW6tt9mOEN3xV7zJdvKH3shZBj9/JQG6Q=; b=oO1LWmDl0ohXAks9J7uPQCXvPSO4WbMKZDey6bxrNsWJhxQm+psL5iFGDCUTRSL29H YPTcocUkIv8Kd5d6a1YBFgdruDvbB2q39k1gcxz71dSBCm6Rq8xY3p/GoHfGEO+XD7Aw eE4KM6bmT8GywacHkIFOrAeExvU0uN0YrDsquUk1rTPYpfQFe9dmCHd6FlNS4J72Zl6x wEfKfTP9XGKhcY4lGgp0D6oTu30hNCY5cZHokOzbqan0XGT7wv1Mzf4J1EGiWlZRLr+l jfu0QBhEF5UahNmOIHeEurNUr3/meqhvQydUrUJYnVG/QJ+kF8w4QJ1S3td6h78j4Z3I FgIg== X-Forwarded-Encrypted: i=1; AJvYcCUC+WASfOetNnHII3bKPVfuLAJZ3TKNt5pvJyd6yLawMdJ0GsYAaQ25Kv3u7IT45Oh5/nTJWnWvAw==@nongnu.org, AJvYcCVtvOyq9+hgft6Q7E5C4nF9lPs/64u+0oPTKkMV3+mRTuekmTeyAr7dOtir5zjFsYjUXQVP0kWcNyDXSg==@nongnu.org X-Gm-Message-State: AOJu0YxTlZMG/YK+/IhTKJO6c1EMqW3R7Mtat/NnP/Qxhy90Aii2xBvi 8I1oSCm6a3zm5eD51JU6+jqoqQL05cju0CzDFEoH23i1Pwp0myOQxHJTIsXJ+w== X-Gm-Gg: ASbGncurpkr31ntw8Rmnh92QB9WjO8kxFqVztP65YjgC3rjCuHCqgnVGImL1prCc4jZ dnqgFFOiq1cmhG+YU4LyiILJgUBMsn+dTAYkENm/xeKEnciuVDujM9tPJRX4YMttq3X6WL43Fjc URAVBpKYaXefaOIklQTerbjjrFHBgV8HZkYcWBl60S4c422uzm0jIgaeaq/qbdAuxDqMLKK2Z+z xv8pUFEih7qQsfJ3pX880kX513EID0gBaxUR3MCcTXuUnJLKuz+/GbIc07Jeq2SpLgBzrYcdTe1 BpB64PYlVxwGz2UOsmjIioAeI4NXotqPijB0KxZ3HbpfUeEXKpcbKf1MCKJC9mBcd9A44VxFLeh BX05gcG/SvE6gwCE6iVl99O9sV8ohdphv X-Google-Smtp-Source: AGHT+IG9naoGJ0aF4O5AaduMwXMX1qnSxFcTxa6fbcxlH0MpFPLlWeJXWo5zVW0dBSbBu4BmNltcxQ== X-Received: by 2002:a17:902:f708:b0:24a:acb7:a1a4 with SMTP id d9443c01a7336-24aacb7a5f7mr67331105ad.9.1756727185156; Mon, 01 Sep 2025 04:46: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 v3 11/35] tcg/wasm: Add shl/shr/sar instructions Date: Mon, 1 Sep 2025 20:44:13 +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, T_SPF_HELO_TEMPERROR=0.01, T_SPF_TEMPERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1756727496805116600 Content-Type: text/plain; charset="utf-8" This commit implements the shl, shr and sar operations using Wasm instructions. Since the Wasm backend uses 64bit variables, right shifts on 32bit values extract the lower 32bit of the operand before shifting. TCI instructions are also generated in the same way as the original TCI backend. Signed-off-by: Kohei Tokunaga --- tcg/wasm.c | 31 +++++++++++++ tcg/wasm/tcg-target.c.inc | 93 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) diff --git a/tcg/wasm.c b/tcg/wasm.c index ba8a89d920..b63b88e011 100644 --- a/tcg/wasm.c +++ b/tcg/wasm.c @@ -28,6 +28,15 @@ static void tci_args_rrr(uint32_t insn, TCGReg *r0, TCGR= eg *r1, TCGReg *r2) *r2 =3D extract32(insn, 16, 4); } =20 +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 uintptr_t tcg_qemu_tb_exec_tci(CPUArchState *env, const void *v_tb_= ptr) { const uint32_t *tb_ptr =3D v_tb_ptr; @@ -42,6 +51,7 @@ static uintptr_t tcg_qemu_tb_exec_tci(CPUArchState *env, = const void *v_tb_ptr) uint32_t insn; TCGOpcode opc; TCGReg r0, r1, r2; + uint8_t pos, len; =20 insn =3D *tb_ptr++; opc =3D extract32(insn, 0, 8); @@ -71,6 +81,27 @@ static uintptr_t tcg_qemu_tb_exec_tci(CPUArchState *env,= const void *v_tb_ptr) tci_args_rrr(insn, &r0, &r1, &r2); regs[r0] =3D regs[r1] * regs[r2]; break; + case INDEX_op_extract: + tci_args_rrbb(insn, &r0, &r1, &pos, &len); + regs[r0] =3D extract64(regs[r1], pos, len); + break; + case INDEX_op_sextract: + tci_args_rrbb(insn, &r0, &r1, &pos, &len); + regs[r0] =3D sextract64(regs[r1], pos, len); + break; + 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; default: g_assert_not_reached(); } diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index d5cf324e7b..3a2a707619 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -140,12 +140,21 @@ typedef enum { OPC_GLOBAL_GET =3D 0x23, OPC_GLOBAL_SET =3D 0x24, =20 + 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 @@ -219,6 +228,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 void tcg_out_op_rrr(TCGContext *s, TCGOpcode op, TCGReg r0, TCGReg r1, TCGReg r2) @@ -232,6 +262,21 @@ static void tcg_out_op_rrr(TCGContext *s, TCGOpcode op, tcg_out32(s, insn); } =20 +static void tcg_out_op_rrbb(TCGContext *s, TCGOpcode op, TCGReg r0, + TCGReg r1, uint8_t b2, uint8_t b3) +{ + tcg_insn_unit_tci 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 tgen_and(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { @@ -304,6 +349,54 @@ static const TCGOutOpBinary outop_mul =3D { .out_rrr =3D tgen_mul, }; =20 +static void tgen_shl(TCGContext *s, TCGType type, + TCGReg a0, TCGReg a1, TCGReg a2) +{ + tcg_out_op_rrr(s, INDEX_op_shl, a0, a1, a2); + tcg_wasm_out_o1_i2(s, OPC_I64_SHL, a0, a1, a2); +} + +static const TCGOutOpBinary outop_shl =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) +{ + TCGReg orig_a1 =3D a1; + if (type < TCG_TYPE_REG) { + tcg_out_op_rrbb(s, INDEX_op_extract, TCG_REG_TMP, a1, 0, 32); + 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); +} + +static const TCGOutOpBinary outop_shr =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_shr, +}; + +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_op_rrbb(s, INDEX_op_sextract, TCG_REG_TMP, a1, 0, 32); + 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); +} + +static const TCGOutOpBinary outop_sar =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_sar, +}; + static void tcg_out_tb_start(TCGContext *s) { init_sub_buf(); --=20 2.43.0 From nobody Tue Sep 16 03:04:09 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=1756727540; cv=none; d=zohomail.com; s=zohoarc; b=bWyCmWf/8b8y1h+L/aSPbRKObz/qt4xTSa73bdNoOYRb/oWgI6S2UFZrAI4NEKWsaIHaUkBiCs1qnScnBKgsa6NZZbduHCQugbOvS3dYJgjJeHBS/sqdXhKQ2JfJVJjbs1DCedDrtQF0a8W/cLfS2DtLleJGbAibUcHcm+hg0Tc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727540; 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=v4Hc+d5jiv3JpJF9e+21kAujA1CrZSCAACAVIRk8Zjg=; b=Tn48JIWBcYmwiCHl1Z3Z3t++cBDcZfBNNWEK+ui5WNYmbeBH6/Rij985nv/MI/dO4t2Bo6xEjIBig3Nb/gbX3DXdli+cvBi118hVUH8akpGtZ1W2C5gzlcHJ1z3zpL6Bpf1by+rje96WRfOgWOtqw+YjkqsF4bOw+55XCYJ+Ri8= 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 1756727540166359.7887592563728; Mon, 1 Sep 2025 04:52:20 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut30W-0001T1-8k; Mon, 01 Sep 2025 07:47:32 -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 1ut2zq-00087t-Rv; Mon, 01 Sep 2025 07:46:50 -0400 Received: from mail-pf1-x42a.google.com ([2607:f8b0:4864:20::42a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ut2zi-0001R6-6A; Mon, 01 Sep 2025 07:46:49 -0400 Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-77238a3101fso1378496b3a.0; Mon, 01 Sep 2025 04:46:32 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.46.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:46:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727191; x=1757331991; 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=v4Hc+d5jiv3JpJF9e+21kAujA1CrZSCAACAVIRk8Zjg=; b=PRiO+VKWHGd6KJrhvodqFGoC2OT5Dl4AC36SQFmaVGIQNp2dpvIs5waW7SkRQI2/2Q RDaNLZRg4DjWwzOCS0SMN5fqnZowoQUKumSSEwT4yih99BZopmKxhMxv6bUtn9iwvSHf 9TUcqmC9Ekd+kfg6mOdiL1in4ek/zYcPdE2lsgoB/ljooy6SQNTO2V4DJ2T5OVorbXLK gC516bhiZxYwqGNBwzuGI+yiBeHBTUUWrHsROdqCezzKOjQPSSYzrxT0dmQuYiA8RGPx CYn+1hzefZm8bVspelfoSfxYExNQ1jJhDZ7YtCpoVZfVyZ3EE+7QlaPrPMtQVqC1OJUc 2xDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727191; x=1757331991; 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=v4Hc+d5jiv3JpJF9e+21kAujA1CrZSCAACAVIRk8Zjg=; b=Fkli3kp2kZ7aCIOrV2RgIOGNcQCLgQvk4v7k/Nmj5McHlBuFlCkQDWoo4jF4IrsNZI lJoeFYKlRx/EGqe8LF7Y2Tie6MHh/CcRi+om59N7x0vFbGiufxnz28VszbFrtoaiDmwI oHUpzlXPsWanHptU2RbajfZ23VU19Zsar5XRt8hW1jy2IGqMvdJY+Peol96DxbCly/p9 qMqbedi3vSk9uLyReAAcYuy6mMYUMhNXkvhwhvSyyCZ6e/kLImPYITocY1nV2/uaoIDu IVLwMWnKtWVkZ/C3FG3v/YlsHuMtGwN5gtEeQFg8M0k13O8JUf03hpS+/TRjzhj/8a8v st7g== X-Forwarded-Encrypted: i=1; AJvYcCVHg+tU/gudDAh5hVYgvJYf9rOjYypg+t4J49XauGANjlxqWgsPFlEGOeIrQNv0YULM7F2Tc4xjXKtGBw==@nongnu.org, AJvYcCWUNuJi1FkW6fPFpeAKAD3m10d/uk9P9HCZh7TxYU/w/Pq1q9dd9JZ9HOwp3vKFLHqY81AS/sE1VA==@nongnu.org X-Gm-Message-State: AOJu0YyAzaW4wJaw/6vidjgxEcvoV2JTPOtoPdFqdi7iGOX/yVeUqFVV mOD8OmW9skNO6JYzSkLe+1tkyUy6iHsernZn1CySA5lhlTDn2tZjU6wb9YFGUA== X-Gm-Gg: ASbGnct18HtN7Y9NZmgPDUmxMGWL9HDlAEqiZsjNH0vdD2pgz8U9QCEdIMQUJ9IiBvG KTrszMdS8IU40O2Y92/4BGOQuape1vaVl05VRmmFwy/eWJHphp+WNuVgONbMUstptMyofFuzvtS gafYswShmirnDj2609FtO9slwhOsftn+LSWiPobCraH8OWALyr+RB5seBVPBFc2wXjfl4H0OJIg 8n3dB+uIG8iujl5Yst0V6AbDZiTKS6ItP6JfbNeFpOXWNp1u7LyVlBQWW0GHsnIfHgaN/zlOmy9 ZG4EzTBCOLIXDNG0L7N2ruxvkdaI0iviMWEZAWU+WOi0KHKf2asnEYOcbU0rrwi5hjZJhi3RMxQ CGEHlMgEcBFxWT8npzygVDw== X-Google-Smtp-Source: AGHT+IE/9YRmF0fIKwqq/Ec0OTK6g/c88HTWK+x3e7tqQ7arfTajlcpRXhbzkgB7V08qZm6TZlztCQ== X-Received: by 2002:a17:902:e80f:b0:240:968f:4d64 with SMTP id d9443c01a7336-249448f9c33mr102456575ad.11.1756727190902; Mon, 01 Sep 2025 04:46:30 -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 v3 12/35] tcg/wasm: Add setcond/negsetcond/movcond instructions Date: Mon, 1 Sep 2025 20:44:14 +0900 Message-ID: <9dbeb865a3d0a4cbf9b1e4c4da6c76d977bb927e.1756724464.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::42a; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pf1-x42a.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: 1756727541980124100 Content-Type: text/plain; charset="utf-8" These TCG instructions are implemented by using Wasm's if and else instructions. TCI instructions are also generated in the same way as the original TCI backend. Since support for TCG_COND_TSTEQ and TCG_COND_TSTNE is not yet implemented, TCG_TARGET_HAS_tst is set to 0. Signed-off-by: Kohei Tokunaga --- tcg/wasm.c | 136 +++++++++++++++++++- tcg/wasm/tcg-target-has.h | 7 ++ tcg/wasm/tcg-target-opc.h.inc | 8 ++ tcg/wasm/tcg-target.c.inc | 230 ++++++++++++++++++++++++++++++++++ 4 files changed, 380 insertions(+), 1 deletion(-) create mode 100644 tcg/wasm/tcg-target-has.h create mode 100644 tcg/wasm/tcg-target-opc.h.inc diff --git a/tcg/wasm.c b/tcg/wasm.c index b63b88e011..183dad10a2 100644 --- a/tcg/wasm.c +++ b/tcg/wasm.c @@ -21,6 +21,12 @@ #include "qemu/osdep.h" #include "tcg/tcg.h" =20 +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_rrr(uint32_t insn, TCGReg *r0, TCGReg *r1, TCGReg *r2) { *r0 =3D extract32(insn, 8, 4); @@ -37,6 +43,110 @@ static void tci_args_rrbb(uint32_t insn, TCGReg *r0, TC= GReg *r1, *i3 =3D extract32(insn, 22, 6); } =20 +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_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; + 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; + default: + g_assert_not_reached(); + } + return result; +} + static uintptr_t tcg_qemu_tb_exec_tci(CPUArchState *env, const void *v_tb_= ptr) { const uint32_t *tb_ptr =3D v_tb_ptr; @@ -50,8 +160,10 @@ static uintptr_t tcg_qemu_tb_exec_tci(CPUArchState *env= , const void *v_tb_ptr) for (;;) { uint32_t insn; TCGOpcode opc; - TCGReg r0, r1, r2; + TCGReg r0, r1, r2, r3, r4; uint8_t pos, len; + TCGCond condition; + uint32_t tmp32; =20 insn =3D *tb_ptr++; opc =3D extract32(insn, 0, 8); @@ -102,6 +214,28 @@ static uintptr_t tcg_qemu_tb_exec_tci(CPUArchState *en= v, const void *v_tb_ptr) regs[r0] =3D ((tcg_target_long)regs[r1] >> (regs[r2] % TCG_TARGET_REG_BITS)); break; + case INDEX_op_neg: + tci_args_rr(insn, &r0, &r1); + regs[r0] =3D -regs[r1]; + break; + 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; + 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; default: g_assert_not_reached(); } diff --git a/tcg/wasm/tcg-target-has.h b/tcg/wasm/tcg-target-has.h new file mode 100644 index 0000000000..7e3caf8790 --- /dev/null +++ b/tcg/wasm/tcg-target-has.h @@ -0,0 +1,7 @@ +/* SPDX-License-Identifier: MIT */ +#ifndef TCG_TARGET_HAS_H +#define TCG_TARGET_HAS_H + +#define TCG_TARGET_HAS_tst 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..57274d4569 --- /dev/null +++ b/tcg/wasm/tcg-target-opc.h.inc @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Based on tci/tcg-target-opc.h.inc + * + * These opcodes for use between the tci generator and interpreter. + */ +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.c.inc b/tcg/wasm/tcg-target.c.inc index 3a2a707619..70de3bbf83 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -137,9 +137,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_SHR_S =3D 0x75, OPC_I32_SHR_U =3D 0x76, =20 @@ -157,6 +185,10 @@ typedef enum { OPC_I64_EXTEND_I32_U =3D 0xad, } WasmInsn; =20 +typedef enum { + BLOCK_I64 =3D 0x7e, +} WasmBlockType; + #define BUF_SIZE 1024 typedef struct LinkedBufEntry { uint8_t data[BUF_SIZE]; @@ -191,6 +223,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. @@ -209,6 +258,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) { @@ -219,6 +272,25 @@ 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(); + } +} =20 static void tcg_wasm_out_o1_i2( TCGContext *s, WasmInsn opc, TCGReg ret, TCGReg arg1, TCGReg arg2) @@ -250,6 +322,85 @@ 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_op_const(s, OPC_I64_CONST, 0); + tcg_wasm_out_cond(s, type, cond, arg1, arg2); + tcg_wasm_out_op(s, OPC_I64_EXTEND_I32_U); + tcg_wasm_out_op(s, OPC_I64_SUB); + 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 void tcg_out_op_rr(TCGContext *s, TCGOpcode op, TCGReg r0, TCGReg r= 1) +{ + tcg_insn_unit_tci insn =3D 0; + + insn =3D deposit32(insn, 0, 8, op); + insn =3D deposit32(insn, 8, 4, r0); + insn =3D deposit32(insn, 12, 4, r1); + tcg_out32(s, insn); +} + static void tcg_out_op_rrr(TCGContext *s, TCGOpcode op, TCGReg r0, TCGReg r1, TCGReg r2) { @@ -277,6 +428,35 @@ static void tcg_out_op_rrbb(TCGContext *s, TCGOpcode o= p, TCGReg r0, tcg_out32(s, insn); } =20 +static void tcg_out_op_rrrc(TCGContext *s, TCGOpcode op, + TCGReg r0, TCGReg r1, TCGReg r2, TCGCond c3) +{ + tcg_insn_unit_tci insn =3D 0; + + insn =3D deposit32(insn, 0, 8, op); + insn =3D deposit32(insn, 8, 4, r0); + insn =3D deposit32(insn, 12, 4, r1); + insn =3D deposit32(insn, 16, 4, r2); + insn =3D deposit32(insn, 20, 4, c3); + 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_tci insn =3D 0; + + insn =3D deposit32(insn, 0, 8, op); + insn =3D deposit32(insn, 8, 4, r0); + insn =3D deposit32(insn, 12, 4, r1); + insn =3D deposit32(insn, 16, 4, r2); + insn =3D deposit32(insn, 20, 4, r3); + insn =3D deposit32(insn, 24, 4, r4); + insn =3D deposit32(insn, 28, 4, c5); + tcg_out32(s, insn); +} + static void tgen_and(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { @@ -397,6 +577,56 @@ static const TCGOutOpBinary outop_sar =3D { .out_rrr =3D tgen_sar, }; =20 +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 + : INDEX_op_setcond); + tcg_out_op_rrrc(s, opc, dest, arg1, arg2, cond); +} + +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, +}; + +static void tgen_negsetcond(TCGContext *s, TCGType type, TCGCond cond, + TCGReg dest, TCGReg arg1, TCGReg arg2) +{ + tgen_setcond_tci(s, type, cond, dest, arg1, arg2); + tcg_out_op_rr(s, INDEX_op_neg, dest, dest); + tcg_wasm_out_negsetcond(s, type, dest, arg1, arg2, cond); +} + +static const TCGOutOpSetcond outop_negsetcond =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_negsetcond, +}; + +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); + tcg_wasm_out_movcond(s, type, 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 tcg_out_tb_start(TCGContext *s) { init_sub_buf(); --=20 2.43.0 From nobody Tue Sep 16 03:04:09 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=1756727743; cv=none; d=zohomail.com; s=zohoarc; b=S7x/PIX0K1h5tnfbme9Y+rOVV6pfYVmK93sUrvWOhdWpXI5MhF551AVxEUrSzNDjrbd3LM4xTezI5ng/sMA+5J8OyqxmPjnp/GE3CJyFXRDtEnzthbUfW2sQpDc4NaOKqkmeIuDUm9fO9x3Inmsw0Tt7ArS+6vTfotX74UjNY2s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727743; 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=0L45vE0IuF3pvV+AN2AolMd1u+x6bFzT5jaCBTjmB4s=; b=ZmzdU11jumplpsPwlH5qhchRgCQCxiwGOYqZy34Ja11uIkguVC5SL2EHq+7BCHs+i7RO2YqNvC7ap1h560G2ScK+S4M5u+4tcmad5oSt30hhVGpT0WSFm/FPD5gjt+Bzsydnu2QaKzfGP0tTt1VSUnt4N5JTT2OQHbCTv9t6HXU= 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 1756727743263665.1209807924829; Mon, 1 Sep 2025 04:55:43 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut307-0000OE-NL; Mon, 01 Sep 2025 07:47: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 1ut2zp-000803-5o; Mon, 01 Sep 2025 07:46:49 -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 1ut2zk-0001SO-CN; Mon, 01 Sep 2025 07:46:48 -0400 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-2487a60d649so52456875ad.2; Mon, 01 Sep 2025 04:46:39 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.46.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:46:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727197; x=1757331997; 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=0L45vE0IuF3pvV+AN2AolMd1u+x6bFzT5jaCBTjmB4s=; b=Gju9Hd0Sn4B/Xvmb3zBe4yjp+GhcqXKhshTaMOZ4nrdYM1NsohDZkHLg0xQZwsWKCw +ndRzjlwkLjI3dUQe2400jpT8xC/6qGoEbefK2bCubR0bcrhm+J8f7IAGoNSGV9jqkhH amj2de7oeBflc2ToKBC+6URtez8C9+AhEIrFgOW4JwcfB2y8Rc2CGdYDJgbimab2i4F0 SMZ/LfS7pekIUjPCTFaValiArXKBY5HYySd6WYVZ6GyNAOvGagnbOmyBMKBBP0DbGw4F gtmzsFzANkvM69HEARSk8ut/GyJlmjiP64Lc8EIv2VAf4uBkEPLKhhLgqfXm5WtyiIOh /s+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727197; x=1757331997; 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=0L45vE0IuF3pvV+AN2AolMd1u+x6bFzT5jaCBTjmB4s=; b=Uk60uierzrpvZOkGpejOlCaKTeGT/q69VyKbZKmd7G3lg36j3zBjsV8q46H7CvOdLD 1y+BTNaSnTqjuUC9svJ/M+ZKjX7vYwPGXXNYi/zRAn2UW0sOnkiZG1dbeGLizVfgtPeg vqjUZCEieQolzT/iaqR8F/FY/4BgZeG1LpHmPcCFV4cnJE/PgegWr/Kgs4ZZzz/EhvVk W3ofuC2mYhepaGP4Q6jdlmqI6IrygRjYma8yu2LvStVGjnI1xStz8Tmnd9s8mcNMEJKb TDk8/CATZrOLpGIC6GJMlkCQ/FMv+jTU1M8xO2YJeJqSD9zNCThc2cz79EnmSfhVr4vr SN7g== X-Forwarded-Encrypted: i=1; AJvYcCWaJ7eOKRwN0zu8wanjuLl1bFB1eHWaWPmTv/m0c4YrGRcA/A2bJAUMAHCOFkbBHGGOBXBh2AIfQvQBAA==@nongnu.org, AJvYcCX+UFrU71LqFhm3aaNlTdc0v/9+qcKYbdyAz86s3hNkIPyMnGuHt5eSeYpB5UxwS05iJa29gpj7fQ==@nongnu.org X-Gm-Message-State: AOJu0YxnsMz1ZGAgzWbM+VIFterCO4LQHFSurCN3WX6+SSV+O2W5PuKm fnT0JJ3ZnM0SljhS2sgE4D1PgD795zYf/nXjfG0uRu4g54Yi7QauByhQ0Rl7FQ== X-Gm-Gg: ASbGncuvCcb96tSWINn6dnMzjv7XRGcPbJOZ+bgD3jITFDECsIJcw8e9KGXq/kNo4Tn E2pvdHA//j5zWuT3VXP69626SWiHlmNtj3qdboHhtu2WgVBAlulnOmOwzvLaRaUpiyxpYWteyVK YkkjBdNb4667bGj6A/iSnUnKjfwu6jkyw6Y65uYUA/gxIoK4m8w95c53ynp7rnOzlGKpcR+Iklo N6QC5mQFMRQ0g2K+U9QPx70J5ygDXySop2SSPnE1QUMyogU4ie27l9TbwqEvZTeT0xunFoIRLlQ qHcfP2OOF7l83HUf7eHu6+E6Wn7M8aanDRIMn4Uvtfnih3Cw0npNpHjBVKgP5CbiGXqiWQHEJei 0aDTp2GYWsspLtKOMM3hHog== X-Google-Smtp-Source: AGHT+IGZJnFNBlyYCr+q9R3KmQbgYYftuNln5Mk3YposoQKUyA9Iqmn/kAN/CMUrHccfv7sAbgGNGA== X-Received: by 2002:a17:902:e750:b0:248:9677:5f33 with SMTP id d9443c01a7336-24944873fa1mr119466745ad.3.1756727196756; Mon, 01 Sep 2025 04:46: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 v3 13/35] tcg/wasm: Add sextract instruction Date: Mon, 1 Sep 2025 20:44:15 +0900 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::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: 1756727743677116600 Content-Type: text/plain; charset="utf-8" The sextract operation is genereted only when the corresponding Wasm instructions are available, as specified by TCG_TARGET_sextract_valid. Signed-off-by: Kohei Tokunaga --- tcg/wasm/tcg-target-has.h | 5 +++++ tcg/wasm/tcg-target.c.inc | 42 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/tcg/wasm/tcg-target-has.h b/tcg/wasm/tcg-target-has.h index 7e3caf8790..cfb85388de 100644 --- a/tcg/wasm/tcg-target-has.h +++ b/tcg/wasm/tcg-target-has.h @@ -4,4 +4,9 @@ =20 #define TCG_TARGET_HAS_tst 0 =20 +#define TCG_TARGET_extract_valid(type, ofs, len) 0 +#define TCG_TARGET_sextract_valid(type, ofs, len) \ + ((ofs =3D=3D 0) && ((len =3D=3D 8) || (len =3D=3D 16) || (len =3D=3D 3= 2))) +#define TCG_TARGET_deposit_valid(type, ofs, len) 0 + #endif diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index 70de3bbf83..dd75deecd3 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -182,7 +182,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 { @@ -391,6 +394,33 @@ 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_sextract(TCGContext *s, TCGReg dest, TCGReg arg1, + int pos, int len) +{ + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg1)); + + if (pos =3D=3D 0) { + switch (len) { + case 8: + tcg_wasm_out_op(s, OPC_I64_EXTEND8_S); + break; + case 16: + tcg_wasm_out_op(s, OPC_I64_EXTEND16_S); + break; + case 32: + tcg_wasm_out_op(s, OPC_I32_WRAP_I64); + tcg_wasm_out_op(s, OPC_I64_EXTEND_I32_S); + break; + default: + g_assert_not_reached(); + } + } else { + g_assert_not_reached(); + } + + tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(dest)); +} + static void tcg_out_op_rr(TCGContext *s, TCGOpcode op, TCGReg r0, TCGReg r= 1) { tcg_insn_unit_tci insn =3D 0; @@ -529,6 +559,18 @@ static const TCGOutOpBinary outop_mul =3D { .out_rrr =3D tgen_mul, }; =20 +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_wasm_out_sextract(s, 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 void tgen_shl(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, TCGReg a2) { --=20 2.43.0 From nobody Tue Sep 16 03:04:09 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=1756727279; cv=none; d=zohomail.com; s=zohoarc; b=i9mhYIFFxWN4GsTE0rcSUDh5ZlWfPUgIGrme6UdWt4TmfJn9B23VcoEKFOGLmdmInuAM12uL+KXF1DYmsFvlFyRQ4ClKWYtYC6UZHPuBmkadGQorJ7VXwkSc5FsOZvfNbgOAyrrrtSCeps/gaSwlgAap8u/zE225n/n40oRC4DQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727279; 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=s01nrgYYdEqwAS86UI8KNaTqVgOOQmhiK/7nUpyNKCk=; b=Rhgtfj3Ez9AbOmSK/58+Zdku6w26cpr8xiVJdtlqW20N50Tc5+311MUdsBWQbM2ucUXujRVGswcBUU/1B9nEF+2X+0x2zWuS0M0P0aOfqh74r2HKgbQJk1KpSYE76mngWnzZGOdKeu8T2LPdP/UK5jyUhPuXeMzRRZYdh9m8soI= 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 1756727279625145.80729280545756; Mon, 1 Sep 2025 04:47:59 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut30E-0000hw-Ln; Mon, 01 Sep 2025 07:47: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 1ut305-0000NM-LI; Mon, 01 Sep 2025 07:47:05 -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 1ut2zs-0001UJ-WF; Mon, 01 Sep 2025 07:47:05 -0400 Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-2445806e03cso47828925ad.1; Mon, 01 Sep 2025 04:46:45 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.46.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:46:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727203; x=1757332003; 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=s01nrgYYdEqwAS86UI8KNaTqVgOOQmhiK/7nUpyNKCk=; b=W6+Ibi6blc7pJRVb0ZaRnbN32qSbCEYi8yjLQC7JOdSKcq4vrFZUovBehrafMNDt17 /tblmpzBR8S14iq17dNOcko3ea6se4pyThZtwZTB6QwhFYMsu3JmxvdelDrNCuw8hl13 NyFThm0Zc9p9P56klKUePq3UJjZsLzOdjv2d3kw2ab7Yy8uCpb5h8nStxD1LVtLK9VVm Ct4y4wu/ahARKQ56iwe3kOxDB3ubUgwzqcUTn6QXchl0Tf2dkGILW58UPVG42zetkJCY 5N6Nnwl8vIJcafIO7svlVUC3Oq2xSYdwnjG9Fx0yG7c0sMUdb655S8DE6s2R6+FB+Wy4 Fucg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727203; x=1757332003; 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=s01nrgYYdEqwAS86UI8KNaTqVgOOQmhiK/7nUpyNKCk=; b=QLXohJAVZO3I2QrpplPzl1Qrugp0W4uwVhxfLBC6cD/xrCqFVQ309tWFdeRRgxSBs8 n5GuCkadlxM7LB6dlVdKt2NOPqPS8L3xXcO67u8o1540QVJvCrXS4T1HfFvRNBAvrQ4P aEvM+IE8R5jeOkmtNrkxXtXuQubfpbQJKZJ7vuX0ifkuFpw+b5LVDIHK+YjjrrEC+Lfv 3uLvtnfLBe8m7a/e4rNBr8LvywyR++LhUWtMMBxZFBKS9hSY4sgPc4//+sKFZ4HJyhNm CASUGTF+lgKHaA3nSeTnmCevVZid5kpONaLu7WtJnswORnUBB7HMhP1hQnzC8JIEV3ro va7A== X-Forwarded-Encrypted: i=1; AJvYcCUSRKCUarnbei8uaQ/ZIPLy3QNIXuAnXn8suSpc/cVmM5dJ2r8be/BWj+PhjbvqomDpioGvBgCXIyEIJQ==@nongnu.org, AJvYcCVHlhjWuvBRrr/xyUdd21SFAcDEinZbXEezS82hnUO4LUzFFo2rAPVg9y6dflvJ6O0VCIaABL346w==@nongnu.org X-Gm-Message-State: AOJu0Yy351nEgrliALyQ7xzwoezCaqf/AoqV9OjnDEEvx/+qDFs/Y28a KGW8pwpyHn4l9FP4hgwAf8+aO50K+aF0QzSV1KEXRzYZCPmHlY9xd+iv7Yi/Sg== X-Gm-Gg: ASbGncsMjDmKq78N2VD6g8hjUDMJmz/jS/1n/f7HQ/iZ1TEI2Cb15PVbJmXlSjUCGHg /5ov30yF8/JtkStwkyaF6IGwOlANSsXK37BRUKUZWy9p3t5lgNSPBkkXtwKP24is++pLXoMz0AS h2m3vHDWotiqRWh8dwWKBWQkSNxrLhp5OxSFIa+Y3LnSSJZboSVPsgiVA9R458U/LDg5I8cQaou X0sOqMbKCKoTSdHm6W+myPOObF9drbiLhhz+XYamNqP7AtN6MHp1xmPXYpqfvKvPBl0CEVGiAyr Ls3vLY4WgTrLth8ff5MwRJPmUdPp1AKlOuvQa+mvesIZ0DBhAzrkGmzt2t+ccC3r8+wJiGrxMAw kHcMUqd3HW70Z5qyqvwaUUQ== X-Google-Smtp-Source: AGHT+IELLpiHsJ4DRlp/6F5d6F5fSpZ0MCUL0D5GnyFxevL9+TzoKQ82lCo+DFHxn1AcEnGnW9Q08w== X-Received: by 2002:a17:902:ce01:b0:246:831c:b1e1 with SMTP id d9443c01a7336-249448744admr106369205ad.2.1756727202562; Mon, 01 Sep 2025 04:46: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 v3 14/35] tcg/wasm: Add load and store instructions Date: Mon, 1 Sep 2025 20:44:16 +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::635; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pl1-x635.google.com X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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: 1756727280812124100 Content-Type: text/plain; charset="utf-8" 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. TCI instructions are also generated in the same way as the original TCI backend. 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.c | 87 +++++++++++ tcg/wasm/tcg-target-mo.h | 20 +++ tcg/wasm/tcg-target-opc.h.inc | 2 + tcg/wasm/tcg-target.c.inc | 286 ++++++++++++++++++++++++++++++++++ 9 files changed, 411 insertions(+), 6 deletions(-) create mode 100644 tcg/wasm/tcg-target-mo.h V3: - 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 8ac6cacc06..77365e2509 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 44bb7ed334..5b048ea70f 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 fff1521e58..82771340ba 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -386,3 +386,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 0ebe6bc52a..64845aa0b9 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' @@ -571,6 +574,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.c b/tcg/wasm.c index 183dad10a2..fa7413fc1d 100644 --- a/tcg/wasm.c +++ b/tcg/wasm.c @@ -20,6 +20,14 @@ =20 #include "qemu/osdep.h" #include "tcg/tcg.h" +#include "tcg/tcg-ldst.h" + +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; +} =20 static void tci_args_rr(uint32_t insn, TCGReg *r0, TCGReg *r1) { @@ -27,6 +35,12 @@ static void tci_args_rr(uint32_t insn, TCGReg *r0, TCGRe= g *r1) *r1 =3D extract32(insn, 12, 4); } =20 +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_rrr(uint32_t insn, TCGReg *r0, TCGReg *r1, TCGReg *r2) { *r0 =3D extract32(insn, 8, 4); @@ -34,6 +48,13 @@ static void tci_args_rrr(uint32_t insn, TCGReg *r0, TCGR= eg *r1, TCGReg *r2) *r2 =3D extract32(insn, 16, 4); } =20 +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) { @@ -161,9 +182,12 @@ static uintptr_t tcg_qemu_tb_exec_tci(CPUArchState *en= v, const void *v_tb_ptr) uint32_t insn; TCGOpcode opc; TCGReg r0, r1, r2, r3, r4; + tcg_target_ulong t1; uint8_t pos, len; TCGCond condition; uint32_t tmp32; + int32_t ofs; + void *ptr; =20 insn =3D *tb_ptr++; opc =3D extract32(insn, 0, 8); @@ -236,6 +260,69 @@ static uintptr_t tcg_qemu_tb_exec_tci(CPUArchState *en= v, const void *v_tb_ptr) tmp32 =3D tci_compare32(regs[r1], regs[r2], condition); regs[r0] =3D regs[tmp32 ? r3 : r4]; 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_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_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_st: + tci_args_rrs(insn, &r0, &r1, &ofs); + ptr =3D (void *)(regs[r1] + ofs); + *(tcg_target_ulong *)ptr =3D regs[r0]; + 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_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; default: g_assert_not_reached(); } diff --git a/tcg/wasm/tcg-target-mo.h b/tcg/wasm/tcg-target-mo.h new file mode 100644 index 0000000000..525f702293 --- /dev/null +++ b/tcg/wasm/tcg-target-mo.h @@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Define target-specific memory model + * + * Based on tci/tcg-target-mo.h + * + * 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 index 57274d4569..122b45749a 100644 --- a/tcg/wasm/tcg-target-opc.h.inc +++ b/tcg/wasm/tcg-target-opc.h.inc @@ -4,5 +4,7 @@ * * 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_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.c.inc b/tcg/wasm/tcg-target.c.inc index dd75deecd3..6bab20a6a9 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -143,6 +143,18 @@ typedef enum { OPC_GLOBAL_GET =3D 0x23, OPC_GLOBAL_SET =3D 0x24, =20 + 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_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 @@ -168,6 +180,7 @@ typedef enum { OPC_I64_GE_S =3D 0x59, OPC_I64_GE_U =3D 0x5a, =20 + OPC_I32_ADD =3D 0x6a, OPC_I32_SHR_S =3D 0x75, OPC_I32_SHR_U =3D 0x76, =20 @@ -421,6 +434,84 @@ 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 +/* + * 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); +} + +/* + * 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 void tcg_out_op_ri(TCGContext *s, TCGOpcode op, TCGReg r0, int32_t = i1) +{ + tcg_insn_unit_tci 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_rr(TCGContext *s, TCGOpcode op, TCGReg r0, TCGReg r= 1) { tcg_insn_unit_tci insn =3D 0; @@ -443,6 +534,19 @@ static void tcg_out_op_rrr(TCGContext *s, TCGOpcode op, tcg_out32(s, insn); } =20 +static void tcg_out_op_rrs(TCGContext *s, TCGOpcode op, + TCGReg r0, TCGReg r1, intptr_t i2) +{ + tcg_insn_unit_tci 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) { @@ -669,6 +773,188 @@ static const TCGOutOpMovcond outop_movcond =3D { .out =3D tgen_movcond, }; =20 +static void tcg_tci_out_movi(TCGContext *s, TCGType type, + TCGReg ret, tcg_target_long arg) +{ + switch (type) { + case TCG_TYPE_I32: + arg =3D (int32_t)arg; + /* fall through */ + case TCG_TYPE_I64: + 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 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_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_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; + } + 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; + WasmInsn wasm_opc =3D OPC_I64_LOAD; + + if (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); +} + +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); + tcg_wasm_out_ld(s, OPC_I64_LOAD8_U, 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); + tcg_wasm_out_ld(s, OPC_I64_LOAD8_S, 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); + tcg_wasm_out_ld(s, OPC_I64_LOAD16_U, 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); + tcg_wasm_out_ld(s, OPC_I64_LOAD16_S, dest, base, offset); +} + +static const TCGOutOpLoad outop_ld16s =3D { + .base.static_constraint =3D C_O1_I1(r, r), + .out =3D tgen_ld16s, +}; + +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); + tcg_wasm_out_ld(s, OPC_I64_LOAD32_U, 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); + tcg_wasm_out_ld(s, OPC_I64_LOAD32_S, dest, base, offset); +} + +static const TCGOutOpLoad outop_ld32s =3D { + .base.static_constraint =3D C_O1_I1(r, r), + .out =3D tgen_ld32s, +}; + +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); + tcg_wasm_out_st(s, OPC_I64_STORE8, 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); + tcg_wasm_out_st(s, OPC_I64_STORE16, data, base, offset); +} + +static const TCGOutOpStore outop_st16 =3D { + .base.static_constraint =3D C_O0_I2(r, r), + .out_r =3D tgen_st16, +}; + +static void tcg_out_st(TCGContext *s, TCGType type, TCGReg val, TCGReg bas= e, + intptr_t offset) +{ + TCGOpcode op =3D INDEX_op_st; + WasmInsn wasm_opc =3D OPC_I64_STORE; + + if (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); +} + +static const TCGOutOpStore outop_st =3D { + .base.static_constraint =3D C_O0_I2(r, r), + .out_r =3D tcg_out_st, +}; + +static inline bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val, + TCGReg base, intptr_t ofs) +{ + return false; +} + static void tcg_out_tb_start(TCGContext *s) { init_sub_buf(); --=20 2.43.0 From nobody Tue Sep 16 03:04:09 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=1756727443; cv=none; d=zohomail.com; s=zohoarc; b=hZ7/uO4iTl0fGQ5LWyTPARZ2WsvB/hNKEzrw3JFs/Tmb6RlFxUtKXUhTmeCIr+G1PaOQ8tuJfI9jmcKEodH+hoa0xSuTjV6QKTIbhoiAqRxfWcwShzL7d4saJhu4LMOQYK2URBlEi6mzoK/m4TMowSFrFUVXevDZD0etJhamkmU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727443; 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=PrwqoR/k34XBU1Ib0AzddvB7H1d7/7mf6zJWPIJvyec=; b=OSLDvzpNLMy1QWtTOzv9lA+y5uclFml/eg6kSAnF7nyCVpeKKUhjj88m/tvknAbrYJEzeRP/CrwlaqcV1+l5cgi1pZWs/euCxhetiL5sl0LY3AQC5a1iT1sdIbh8ARkF74rW3ZWbgLcas2DS8tLh55O4fcLRXOpTb+nGB1gqSFE= 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 1756727443693158.3290804205334; Mon, 1 Sep 2025 04:50:43 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut319-0002OT-9k; Mon, 01 Sep 2025 07:48: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 1ut307-0000Od-Vi; Mon, 01 Sep 2025 07:47:08 -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 1ut2zx-0001Vb-Ci; Mon, 01 Sep 2025 07:47:07 -0400 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-24af8cd99ddso1535445ad.0; Mon, 01 Sep 2025 04:46:50 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.46.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:46:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727208; x=1757332008; 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=PrwqoR/k34XBU1Ib0AzddvB7H1d7/7mf6zJWPIJvyec=; b=AvHfx5/OBRxvczin6dGG1tGa2Hr7Gxk7gXIBunO64uCAaM3pNNwVb5NSECHr3+8/vM RS1lIftGsU5dggc1a5ffKp5/GH9lCGavZYoxzxN1herkkUYwOJzBgnGZzTqXziSoclKc p3AK8lyQaCGmrHCscFc1Q9wEH24td/VshzQC9O9PK7Gxxa8F5mjv2FhtNL5Rpn7puQe0 7VKmfS4eNrW9a53Qk/rHCsYZ52Hhz3FHW7t/T9uBs6M0fCS2Wl+2JRTJl+O+WVwzo8+f kQyOJCDuTs4GRqlur6Mgoozq4/AJ3EP3buV0GhpgkiWzz4w8vTxVR4NwS3Vq9bOX5FcY o3/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727208; x=1757332008; 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=PrwqoR/k34XBU1Ib0AzddvB7H1d7/7mf6zJWPIJvyec=; b=wcZajMkG/uGd3ybjnxtW791EbyZSlZy0CIJFBwJvpAzqlpRDq6F+mqmybO0uDDq+ZJ JIeLVTNFIbdaK1pL4bqSS7n20esmGzWY3Vwb1QZK5bfTEgU48JYzZE7eqkABk6ly5Elf uwDUt+HiuCEoujr2rMhMZ6r+P21KqlkW3QdJhJ03shLIj283SPnKVG3qh0Tbmno4znvP AlOjwhe+QjmVwXp90gOk/rMaAhIPKvY3/P7xKeCbmbfIJkFEDY6c2wistmR3SmbZvqrw zs0cmu2CzG6mUgAzr0f4DgkORvGXKHJj2EcmQu8qNf5oDvBIqrwzbm8Gyfl71wG5Dp75 LlCg== X-Forwarded-Encrypted: i=1; AJvYcCVLj9R6V4AqV0DjuZwtCN/YPz+LcP3N6CYeJGm7KZtHD7Cv+V10AFauhvfqiIZRj23A2BwD/1jdj8aXjw==@nongnu.org, AJvYcCWN+BWt2WIe50I1bnw26EllQnOwOsMyxwlUsp28sMzGdpVqVd4Hc3NyvxGy6UIX2HdhxbozxMsUtg==@nongnu.org X-Gm-Message-State: AOJu0Yz4BBQNU/0cdr/njLCseXO8U24S8MzmBkrhxH0OZ5pt6gu8Ll24 pA+FQ0xOILIa1GHhdw9GKXoopCdAAz0mrC1htjAJ8RkoypMcyXzpqfToCQ4jmQ== X-Gm-Gg: ASbGnct/iYu/BS7wph4G+70XnSZ5HbT0osbViAGFZCk+qEcopOq4EFsx3rk4z1w5C6T KpLmHwXyLpCSIRUtu98k8RkTu6CgD9CAcRNOE6TRtMwRdSWisA221gt9l8AXq+HqwrYOc/sxGKe uVP/8touF3il7D9v5PMq/g5kBH4eWm0l7s8pa+VXv15xB9BrNMFIR53DlyT5B1NqblWfJ8Dw4Cu 3aki2rVbYfAXynoTfUOJ273zVwkk2AYJOr0wXqwi8Iv6Us4nh23eTzFnb4tiCe5TrqNFvPyAxI0 bR9Kjrt/9AFLZ3+TzwR2HywtSpuZl3A7KfDWW6NTuaTErbPsSniiKB1rz1JGZNGCjoQfMf4Oq6c fj5EoQxLDGwj5B+U9O2wg6N5iP9l2J58w X-Google-Smtp-Source: AGHT+IG7ljqu8UZn0dVIRGtdxeAUbvM2dPs27AAcqRKEiISkiA3ub34IXSaKWOhChg3Kng4jBYfHFA== X-Received: by 2002:a17:903:f8d:b0:246:255a:1913 with SMTP id d9443c01a7336-24944b29d4fmr102537615ad.39.1756727208252; Mon, 01 Sep 2025 04:46: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 v3 15/35] tcg/wasm: Add mov/movi instructions Date: Mon, 1 Sep 2025 20:44:17 +0900 Message-ID: <13eb7d5403cdad50342504eb76aef9bc0f8e0996.1756724464.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: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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: 1756727444934124100 Content-Type: text/plain; charset="utf-8" Theese operations are implemented using the corresponding instructions in Wasm. TCI instructions are also generated in the same way as the original TCI backend. Signed-off-by: Kohei Tokunaga --- tcg/wasm.c | 4 ++++ tcg/wasm/tcg-target.c.inc | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/tcg/wasm.c b/tcg/wasm.c index fa7413fc1d..2c8a7b814e 100644 --- a/tcg/wasm.c +++ b/tcg/wasm.c @@ -260,6 +260,10 @@ static uintptr_t tcg_qemu_tb_exec_tci(CPUArchState *en= v, const void *v_tb_ptr) tmp32 =3D tci_compare32(regs[r1], regs[r2], condition); regs[r0] =3D regs[tmp32 ? r3 : r4]; break; + 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; diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index 6bab20a6a9..def1f5cd5e 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -501,6 +501,28 @@ static void tcg_wasm_out_st( tcg_wasm_out_op_ldst(s, opc, 0, offset); } =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 void tcg_out_op_ri(TCGContext *s, TCGOpcode op, TCGReg r0, int32_t = i1) { tcg_insn_unit_tci insn =3D 0; @@ -798,6 +820,20 @@ static void tcg_tci_out_movi(TCGContext *s, TCGType ty= pe, } } =20 +static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg ar= g) +{ + tcg_out_op_rr(s, INDEX_op_mov, ret, arg); + tcg_wasm_out_mov(s, ret, arg); + return true; +} + +static void tcg_out_movi(TCGContext *s, TCGType type, + TCGReg ret, tcg_target_long arg) +{ + tcg_tci_out_movi(s, type, ret, arg); + tcg_wasm_out_movi(s, type, ret, arg); +} + static void stack_bounds_check(TCGReg base, intptr_t offset) { if (base =3D=3D TCG_REG_CALL_STACK) { --=20 2.43.0 From nobody Tue Sep 16 03:04:09 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=1756727780; cv=none; d=zohomail.com; s=zohoarc; b=M9j+V1EzYNyc0HxAl6QlZUYiEXw2tgRsKH6pKVDvr53vDS6GIUUZpKE4eEGFsQGRI1DxivJEiY0/npamL9ZwTqK/3dZNPSVYvaIWl9FDy80HHPOylac4RxM5ybhzMfqzetUgg5s8lGcxFswkMVwVtnphKhwmfpWKhuvc9dJmMRQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727780; 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=bQ+3MTiWQKCXF89rhsmMbp4GPDQ2G1un64hmb0oDF0M=; b=oFYkjyHurZV0wOdmvTV4i6XOA5J877BIYqZRmByXCvk884HmyZgftghhz7zhg0nojAfhV/NRK8vQZ1u8YeMfGeJ+B0BYZeDhaYbcoaGZPDxLPgnp07iTD4iOBGJswHMjxVSjM6e6mZgXGbAgf/H6D+bp2/QkqSdccNsohieQTeE= 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 1756727780335577.4414414796275; Mon, 1 Sep 2025 04:56:20 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut30K-0000qa-5F; Mon, 01 Sep 2025 07:47: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 1ut30E-0000g9-71; Mon, 01 Sep 2025 07:47:14 -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 1ut301-0001XA-Cw; Mon, 01 Sep 2025 07:47:12 -0400 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-2445806df50so33086855ad.1; Mon, 01 Sep 2025 04:46:56 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.46.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:46:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727214; x=1757332014; 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=bQ+3MTiWQKCXF89rhsmMbp4GPDQ2G1un64hmb0oDF0M=; b=g6Rb1sxq1V4MoRCmaFDIF7jx1GEOUVQeveJhwiXLbo8HshLosq+kAWJaAnt+8Vxn+g G7tDswpSmoBqjuC9WW2aIZX3pWcM51Bja/31wr48iIf3A0lBwU5xJuqHmkw36svCHdgD w4ZJfqHN2L/ke53l+BRAS2RYQibv7PZqu5UpbiDUpzpDtZrqoBfyr4iF8LfgqeeRa8P2 o3mEX/icx08t8QJMu0EIFDrsRVd4jZrz4CLhA5e2XQmHXirP3b5qV7zu310Mr/6iTknJ dNZ8y8uDJHSDffWclqe5J1otvOV0L86p99sK94bNqDECLmSB/Bb7r8D+DXn6ZYW+ftyx +xfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727214; x=1757332014; 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=bQ+3MTiWQKCXF89rhsmMbp4GPDQ2G1un64hmb0oDF0M=; b=W91qv9hfW47frIi1hVU0ciSVnk+cFISmUqxoS0HCOfGbUqjEGgZgaUixGUdZnmVVwL gtnvgrDuAs4ZUsLx7jN8OGf22ZWk5nrdOhtr5njb+PtO57wJiV/YprSpbMc0W3Jkxk2c hRX7s0B1G09HeBKvL7l2/urfKc6fqEx/jsl7p7moLifP8RphcXQY9pA19gsVENh30OZA qFCu/DJ35a2IQXyahuTNogjxLd/GWjsquoouK1fRVnR1YG99KSm1B/GrCIvXPOeubOW9 UuxxZ/8IWucyINn4vxhWIroTgay6pKRC93DG+JSDYkmyQCmLVlsp/UYjZaU6PkubaWDq EoZg== X-Forwarded-Encrypted: i=1; AJvYcCVYvpkpd+Oby/5GEFsBaECd1S0S/xp1Ai5KTM06nEDlq2L/kWAkdm0TD12Bt5SO0g3Pcl7IzusNcg==@nongnu.org, AJvYcCWGhQIvxFQHntHL3608Tii7kb51p9minn5i+L+XpkEkXscSQ2u4QzIX6iawS6uPiorE2kZG1xLUXUkivg==@nongnu.org X-Gm-Message-State: AOJu0YyIX1CkSUPdkymrUbPHOYO5Yni28gNBx1SC7Iyjwsq/98NOU58N oiagxre43ysgWZ2isxwdyJVkEMtTcxYsbUAX67pUf/2Uax9kw2UJP1dEHJcaWQ== X-Gm-Gg: ASbGncsQeqFuEMXNrrwLk9TAqy5anChHXjAYaM9dMji80nm+MVEsI2gneJ2JiRdMFHa uuWeFXdQk7HHaapZMHR01Fjo2NAuQd9Qlql0ltGuQMHELmIiuK3IEq5KldYCa5xh6AGLjZm5b7b tFIbtz+AVUNug7Kr44dXml6Brmj31BPgvec3Qe/MZSMSlv1Tw4xzCFnh/nEOpzpOCspYGn/0pfV N/vG458zEojecC99Ydoe6ubq0UJGSGKTifgkXz+0pvNpFm+5wsaqBKeDdw3OsDC9+gU6zY24eCI GPMs3W835ic1gFjJ1QlNYQlWj8bQ1Ht4526pGymvQFhLihen7LZIsgKHQmJxZiUYo/b9D5OrztP ER65nMP2nrln1F6+o2Z2nE3xK+6OR2YFcvD8MxbYjNkc= X-Google-Smtp-Source: AGHT+IEgbT6j86xFecbnFKWuYckMBN8GSvt4ydp6+BQu7+N6HT0ZkBOlxAbGvGrW4OTtOS6TJGYi6Q== X-Received: by 2002:a17:902:e745:b0:246:e1f3:77b2 with SMTP id d9443c01a7336-24944b65071mr91417215ad.53.1756727213890; Mon, 01 Sep 2025 04:46: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 v3 16/35] tcg/wasm: Add ext instructions Date: Mon, 1 Sep 2025 20:44:18 +0900 Message-ID: <004734452e90077ea9f98ab7b093ce16fc1f5bae.1756724464.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, T_SPF_TEMPERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1756727782652124100 Content-Type: text/plain; charset="utf-8" The ext operations are implemented using the corresponding instructions in Wasm. TCI instructions are also generated in the same way as the original TCI backend. Signed-off-by: Kohei Tokunaga --- tcg/wasm/tcg-target-has.h | 1 + tcg/wasm/tcg-target.c.inc | 79 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/tcg/wasm/tcg-target-has.h b/tcg/wasm/tcg-target-has.h index cfb85388de..a29ceb2ea5 100644 --- a/tcg/wasm/tcg-target-has.h +++ b/tcg/wasm/tcg-target-has.h @@ -3,6 +3,7 @@ #define TCG_TARGET_HAS_H =20 #define TCG_TARGET_HAS_tst 0 +#define TCG_TARGET_HAS_extr_i64_i32 0 =20 #define TCG_TARGET_extract_valid(type, ofs, len) 0 #define TCG_TARGET_sextract_valid(type, ofs, len) \ diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index def1f5cd5e..e41b3a0c27 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -434,6 +434,22 @@ 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_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)); +} + /* * The size of the offset field of Wasm's load/store instruction defers * depending on the "-sMEMORY64" flag value: 64bit when "-sMEMORY64=3D1" @@ -991,6 +1007,69 @@ static inline bool tcg_out_sti(TCGContext *s, TCGType= type, TCGArg val, return false; } =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_wasm_out_sextract(s, rd, rs, 0, 8); +} + +static void tcg_out_ext8u(TCGContext *s, TCGReg rd, TCGReg rs) +{ + tcg_out_op_rrbb(s, INDEX_op_extract, rd, rs, 0, 8); + tcg_wasm_out_extract(s, rd, rs, 0, 8); +} + +static void tcg_out_ext16s(TCGContext *s, TCGType type, TCGReg rd, TCGReg = rs) +{ + tcg_out_sextract(s, type, rd, rs, 0, 16); + tcg_wasm_out_sextract(s, rd, rs, 0, 16); +} + +static void tcg_out_ext16u(TCGContext *s, TCGReg rd, TCGReg rs) +{ + tcg_out_op_rrbb(s, INDEX_op_extract, rd, rs, 0, 16); + tcg_wasm_out_extract(s, rd, rs, 0, 16); +} + +static void tcg_out_ext32s(TCGContext *s, TCGReg rd, TCGReg rs) +{ + tcg_out_sextract(s, TCG_TYPE_I64, rd, rs, 0, 32); + tcg_wasm_out_sextract(s, rd, rs, 0, 32); +} + +static void tcg_out_ext32u(TCGContext *s, TCGReg rd, TCGReg rs) +{ + tcg_out_op_rrbb(s, INDEX_op_extract, rd, rs, 0, 32); + tcg_wasm_out_extract(s, rd, rs, 0, 32); +} + +static void tcg_out_exts_i32_i64(TCGContext *s, TCGReg rd, TCGReg rs) +{ + 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_out_op_rr(s, INDEX_op_mov, rd, rs); + tcg_wasm_out_extract(s, rd, rs, 0, 32); +} + +static void tgen_extrh_i64_i32(TCGContext *s, TCGType t, TCGReg a0, TCGReg= a1) +{ + tcg_out_op_rrbb(s, INDEX_op_extract, a0, a1, 32, 32); + tcg_wasm_out_extract(s, 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, +}; + static void tcg_out_tb_start(TCGContext *s) { init_sub_buf(); --=20 2.43.0 From nobody Tue Sep 16 03:04:09 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=1756727701; cv=none; d=zohomail.com; s=zohoarc; b=hKKsAyuQWj1o8Ie39WX33jhboLKWVptniY/0RILqmtbMyoA4gGwEf1KP95eXQTh47p4bfT/FblhLEY8xajIL4CMyTQO47PYa4OstjTQ5v1GVbmsTdJTV3uueTJc4Bz7N8p4mJilwHlDLzu8mGYMpQdEqP1v5qjoDHJIRw4TFVDg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727701; 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=DmjKCcJg/XvaXc+knb67SzldxejgqYeqQIRRw4J5fTg=; b=QU08eRsr7H3bjHzceIro1JMEl7kOVUfWppWfqNVtIgH9leMFsbZ0dJ6JAADXbnh+U/CS/p2n/OoVGOXaatGq2PxQRStRUxhJWXYcxrJN7ek+r3T1arm0fW0XVdASV05npkiAvx3XJB88pM9F9OfdIPo+mkQPN/r4MP/2U8aWdnI= 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 1756727701260130.89377335760798; Mon, 1 Sep 2025 04:55:01 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut31H-0003Bm-K9; Mon, 01 Sep 2025 07:48: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 1ut30F-0000kR-TD; Mon, 01 Sep 2025 07:47:16 -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 1ut309-0001Z4-Ak; Mon, 01 Sep 2025 07:47:15 -0400 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-244582738b5so37721765ad.3; Mon, 01 Sep 2025 04:47:01 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.46.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:46:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727220; x=1757332020; 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=DmjKCcJg/XvaXc+knb67SzldxejgqYeqQIRRw4J5fTg=; b=OyTMXyIm1xjHCvfRzNdA/Zli8wgTkHyCESheT5ipZ3eCkqUSnmUsJ2biCTLhPQKlLC FwKvQMg8sS3dCgBa2EMH1MidwEskFWtlI2M2YhFoSZlLFHRDZkh5NEeWqT8fDQCSfeEi vBnP6deO1Z0lGwmPfJP6udonIkQa+KCalfKrmmhUNiFDK0cs4YlisIN1ABtXbTXy0r1r evhUns2bMHF6EFL3lxBFGZ9Myfq7+AtxdmHS56UpDB8dPpngom6RuvgKTfhmS6mxF/e+ bGNcn+R9badhyPSiDinS/i2jP+75KNFF4PcLCaOtW2C+dDC4cNDr6f3oOhhT/e8Z5M7/ 1fog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727220; x=1757332020; 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=DmjKCcJg/XvaXc+knb67SzldxejgqYeqQIRRw4J5fTg=; b=ptT1BCT0RXscdTwn0lIUztPEZEvZhGrAeNR22fkntlpabNLCIm8Ic+BVJ2hkv8D3tG BZiYZHX6Kib69+rtIcnoqC8oki7wmQXgGIZx5+mKM09ybIYMAtWOVcD6wbACmrI1xi2Z EldFlwUfLkOs7clq0XSVBa91O/9UBecFv7QyRdsbNhety1KcVf0l49rfUMNF6rooxmIp YR/p8mfi7tAxE2eHm4WYCSeLAhjhAe7Jofzw2w2rK9lLffUmTf5adNeRt69rhiViAJMy 1rQm3wfHAOTpIZ/NPvkbDIAWbPxXbvAJbJu7LQqAVHjCWNvzAylUnmsCj4LmE4pqwtH9 Ryzw== X-Forwarded-Encrypted: i=1; AJvYcCUrMn2kzBOi7U1iaxGdUlkPjwJwLxyudnQfJamGx6sn3yAJPp2KLD1yI4FzMGpnaIEParBMPMt5Dw==@nongnu.org, AJvYcCVfMBzR/6MBF5LqNxcenOOuAOx4DUjv39odKYPSwXXyoV6WICEGl65Xi0hA+sbFB7H8bG1f5UQPE45hlg==@nongnu.org X-Gm-Message-State: AOJu0YzmxeXim28kg26QdFJewtMyfS7n0sItjJrhnXRJ+RfcPLe96tta B3GPZ7hcFPkGZi37t6GHPhelQJX9teBcmBUOoYXzxgMjUfMrS4BHteM0KMS4rQ== X-Gm-Gg: ASbGncvGIHpA27gLHS6xYjtr1+OhB/Wc/Qit8uvM2JFBPTPR3AxvpXl7p1nkF9sJz1M p3jbaw0l4oIgSIGSJMTgWoVM8KF+P98GmO1Av0ujoA7hD/OU4R2t/0J0fZiesPOfV44CkQszQDQ siebEvrjXE4DhRoYBtYyzFOT+sZcaT09MnsdwNUDxqGvfyXbDweKCGJH3/2iHbR1NocAMSXwYqA T309pClvL7y/0S9SFATWtPV2R4o+pMS+lRz6qWYTqfQ8toX+vTi5Rfi/RXSpU/BF9dq/MiHfdeF 5whuq00k2lAt/2J7NJa08TNlh3BJGg8cvDJH38zH/Toku3Pc7q057g9DQXQNzoON9EWlaa/7vBu j2e9IlCxev14T9ByaN2sQtIP4BGkfmipk X-Google-Smtp-Source: AGHT+IEOEBT9SP9+zMBLB2es4O64PKaker5iUpH9NqTYKUAPZ7QFIfAnPveOLn60R47NlFU9tx+xYQ== X-Received: by 2002:a17:903:1a2e:b0:240:52c8:2564 with SMTP id d9443c01a7336-24944a0f8fcmr96664405ad.26.1756727219648; Mon, 01 Sep 2025 04:46: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 v3 17/35] tcg/wasm: Add div/rem instructions Date: Mon, 1 Sep 2025 20:44:19 +0900 Message-ID: <32eaa622a5acc240179be619c09319bbb48f3f14.1756724464.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: 1756727703813124101 Content-Type: text/plain; charset="utf-8" The div and rem operations are implemented using the corresponding instructions in Wasm. TCI instructions are also generated in the same way as the original TCI backend. Signed-off-by: Kohei Tokunaga --- tcg/wasm.c | 32 +++++++++++++++++ tcg/wasm/tcg-target-opc.h.inc | 4 +++ tcg/wasm/tcg-target.c.inc | 68 +++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) diff --git a/tcg/wasm.c b/tcg/wasm.c index 2c8a7b814e..8c8dcb81c7 100644 --- a/tcg/wasm.c +++ b/tcg/wasm.c @@ -327,6 +327,38 @@ static uintptr_t tcg_qemu_tb_exec_tci(CPUArchState *en= v, const void *v_tb_ptr) ptr =3D (void *)(regs[r1] + ofs); *(uint32_t *)ptr =3D regs[r0]; break; + 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_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; default: g_assert_not_reached(); } diff --git a/tcg/wasm/tcg-target-opc.h.inc b/tcg/wasm/tcg-target-opc.h.inc index 122b45749a..5ed8c67535 100644 --- a/tcg/wasm/tcg-target-opc.h.inc +++ b/tcg/wasm/tcg-target-opc.h.inc @@ -8,3 +8,7 @@ DEF(tci_movi, 1, 0, 1, TCG_OPF_NOT_PRESENT) DEF(tci_movl, 1, 0, 1, TCG_OPF_NOT_PRESENT) DEF(tci_setcond32, 1, 2, 1, TCG_OPF_NOT_PRESENT) DEF(tci_movcond32, 1, 2, 1, TCG_OPF_NOT_PRESENT) +DEF(tci_divs32, 1, 2, 0, TCG_OPF_NOT_PRESENT) +DEF(tci_divu32, 1, 2, 0, TCG_OPF_NOT_PRESENT) +DEF(tci_rems32, 1, 2, 0, TCG_OPF_NOT_PRESENT) +DEF(tci_remu32, 1, 2, 0, TCG_OPF_NOT_PRESENT) diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index e41b3a0c27..38459a60d6 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -181,12 +181,20 @@ typedef enum { OPC_I64_GE_U =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_SHR_S =3D 0x75, OPC_I32_SHR_U =3D 0x76, =20 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, @@ -1070,6 +1078,66 @@ static const TCGOutOpUnary outop_extrh_i64_i32 =3D { .out_rr =3D tgen_extrh_i64_i32, }; =20 +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); + tcg_wasm_out_o1_i2_type(s, type, OPC_I32_DIV_S, OPC_I64_DIV_S, 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 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); + tcg_wasm_out_o1_i2_type(s, type, OPC_I32_DIV_U, OPC_I64_DIV_U, 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 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); + tcg_wasm_out_o1_i2_type(s, type, OPC_I32_REM_S, OPC_I64_REM_S, 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); + tcg_wasm_out_o1_i2_type(s, type, OPC_I32_REM_U, OPC_I64_REM_U, 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 tcg_out_tb_start(TCGContext *s) { init_sub_buf(); --=20 2.43.0 From nobody Tue Sep 16 03:04:09 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=1756727759; cv=none; d=zohomail.com; s=zohoarc; b=WNSd2xPC1k+kRkJE3MlI3jL71FKd93muHWYKRtfND8pw0C8YyMC3Lutf41ayaSxWZ+kL0xz4+WvdQWrxqSex6nJ2nOz4Ai5R57gd/+E4w+5mwrc4cViAIgTb/URq7Q+lWiq1XkourMcwLe2+Rost4F5/PFhFpltjj4hoxUZWKPk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727759; 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=QST8MQTRFZEhs5AjQPRXH0ReyeMh+BeVvkW3QZwh+1A=; b=QzJVxLU9pH16uKRBsq70afIJB87GsWkR8OmWnrpGqqIrCkdosPW+VqbedWhJPLWFEGPU6fifMgTpVS4ceZa0a9LfYQDn0dfiHCOWksfcYk2aZy4A4cZbHXwBgBdN6kT9CmZ4mXcpcAuULHlPwUQGE2piNjxNmMAeBpz6nS9DnUc= 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 1756727759973931.9780057872616; Mon, 1 Sep 2025 04:55:59 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut30U-0001RP-0N; Mon, 01 Sep 2025 07:47:30 -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 1ut30R-0001LK-Av; Mon, 01 Sep 2025 07:47:27 -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 1ut30L-0001bF-4U; Mon, 01 Sep 2025 07:47:26 -0400 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-24456ce0b96so44278855ad.0; Mon, 01 Sep 2025 04:47:07 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.46.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:47:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727225; x=1757332025; 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=QST8MQTRFZEhs5AjQPRXH0ReyeMh+BeVvkW3QZwh+1A=; b=I9Nr8S3cCBG/lQHyTg3tyjqCK5IpMKUT3hcCI6Dbr0RnS2RKGl9qROBkOXb41vQFz9 l3qm6tiISSn3DBUS/AdX9CydsU2DHOjxFaDn9KoXj2WGlISl9+EK3BS1+pJ1BW/C4EK3 bWy2E0nkaZyduJVgMmfslEBPJcA51G17fp1knIZeJee1TEJdnZQw/UlFbvwHQcsiMVym gnLQ/YOtNmrqMPiQ4o/jPQWnc3an1RHIF4fk50wZNgbZqEXEO7KQ3lNSJ3qCws1bbitb aqDZZMmF9EpDifQMqhE2bP9AWEohLx93umoZ5VZgTwr4kKMEBq+XcsAENBneNl5/537d 1bIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727225; x=1757332025; 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=QST8MQTRFZEhs5AjQPRXH0ReyeMh+BeVvkW3QZwh+1A=; b=tREkewBSnHS5H4fG5zIdxI4rsJbzVrRrGSbiOcDfYgEIj1mG1KXGv7Uyfk0LAXmtgU DEOl6afc7rTL9aFM4Fjkhl3+hPQXNAcWRBl8U5Jx3ieYp7OnJIk5OZRCNn3lJSIAacZw BbnzqkgLrH2GSfyO9WjxnjURBICNjxPwt2dgaH+9HymL4MJnW+Hnh+rCVZwXGc9znS4J I9/c1iDLqpe3pVqP4wqQ+GMu+COs+S1W9sbLuIgXJk/K/ykBZCEbCL80NoW4J4HZhrnw bwQzbWornZPSsLTj4St52cPgH6DFgSPsBGck4Fr7aV9GnZATLJSFrqH5fIeP7M1eSDYt beNg== X-Forwarded-Encrypted: i=1; AJvYcCU2/Gp0oGiwC7EBv6bw8xJP3515sKqfHQgpcJP+BdxGJeg3kuZyo1v4WOUZ5X/mPi+LWUaUbV2e1Q==@nongnu.org, AJvYcCXQ5uj+7Q3B1qxOwubfcMoWzXe/au9kHEbmPnx5Jkev1RF3VicKSYARSvGnKbGRrcI5tEB2Xioms4pkOQ==@nongnu.org X-Gm-Message-State: AOJu0YyQtW6oGfklW79hMQt/CduBvaXLeTATNA56Rb9qpN8ejICRdX8h lFZhKQ7TGHFdepaWNYICvdks1ayIzuSKKvwULIaz9AylXbuwHsYQzpv9NZjaEQ== X-Gm-Gg: ASbGncvfKgQyllazs0CE6oL8Butz8+JxEdzfmg1LPMqnI8sZeR/nTkcAy1r0W6Epdur unq9Sioec+2w8hiKDaP1OwwDCIhbqfBH3/8wPiJlYD49wgrdZS09K2ENeBgXm56fDqgSYVrE3je 6ALaDMWyYWJocYcygkwzqDG3TY7u0SFMMZh3Rmahx7zT1xbYX//bSR8whhak2X7J9mopD524BWy kd3rOtuwdcpMmIiA+d3XZiXWyWL1xD5QUY3spu//wXbE9K5QTix39zIVwW86TIvYtttok/mDKCT x8h1Xb3mZceu4P/NaMofRTtVwABHe3o+Ma61X/Hw9wOfS39fJI+Lp02sttFH3IvBc9L4bjqQX4l nDX+LWX9S1fitLNz8hYqR6YbzhaKJiglU X-Google-Smtp-Source: AGHT+IGcvRy/DceMUMT1/SF1BY0nUDrsEocsXYek2qfwdA2hXghrEfet/9dRcAQ1DhtTkyNav6ZJkQ== X-Received: by 2002:a17:902:c951:b0:24a:aeb6:f1ae with SMTP id d9443c01a7336-24aaeb6f56cmr61121845ad.11.1756727225496; Mon, 01 Sep 2025 04:47: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 v3 18/35] tcg/wasm: Add neg/ctpop instructions Date: Mon, 1 Sep 2025 20:44:20 +0900 Message-ID: <2cb39a5cb4df42d6a7b3bdc3d6449fb9a60e861f.1756724464.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::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: 1756727768681124100 Content-Type: text/plain; charset="utf-8" The neg/ctpop operations are implemented using the corresponding instructions in Wasm. TCI instructions are also generated in the same way as the original TCI backend. 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. Signed-off-by: Kohei Tokunaga --- tcg/wasm.c | 4 ++++ tcg/wasm/tcg-target.c.inc | 44 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/tcg/wasm.c b/tcg/wasm.c index 8c8dcb81c7..a5e72d8fe5 100644 --- a/tcg/wasm.c +++ b/tcg/wasm.c @@ -359,6 +359,10 @@ static uintptr_t tcg_qemu_tb_exec_tci(CPUArchState *en= v, const void *v_tb_ptr) tci_args_rrr(insn, &r0, &r1, &r2); regs[r0] =3D (uint32_t)regs[r1] % (uint32_t)regs[r2]; break; + case INDEX_op_ctpop: + tci_args_rr(insn, &r0, &r1); + regs[r0] =3D ctpop64(regs[r1]); + break; default: g_assert_not_reached(); } diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index 38459a60d6..27f3a7414b 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -188,6 +188,7 @@ typedef enum { OPC_I32_SHR_S =3D 0x75, OPC_I32_SHR_U =3D 0x76, =20 + OPC_I64_POPCNT =3D 0x7b, OPC_I64_ADD =3D 0x7c, OPC_I64_SUB =3D 0x7d, OPC_I64_MUL =3D 0x7e, @@ -547,6 +548,21 @@ static void tcg_wasm_out_movi(TCGContext *s, TCGType t= ype, 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_const(s, OPC_I64_CONST, 0); + tcg_wasm_out_op_idx(s, OPC_GLOBAL_GET, REG_IDX(arg)); + tcg_wasm_out_op(s, OPC_I64_SUB); + 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_out_op_ri(TCGContext *s, TCGOpcode op, TCGReg r0, int32_t = i1) { tcg_insn_unit_tci insn =3D 0; @@ -1138,6 +1154,34 @@ static const TCGOutOpBinary outop_remu =3D { .out_rrr =3D tgen_remu, }; =20 +static void tgen_neg(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1) +{ + tcg_out_op_rr(s, INDEX_op_neg, a0, a1); + tcg_wasm_out_neg(s, 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_ctpop(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1) +{ + tcg_out_op_rr(s, INDEX_op_ctpop, a0, a1); + tcg_wasm_out_ctpop64(s, a0, a1); +} + +static TCGConstraintSetIndex cset_ctpop(TCGType type, unsigned flags) +{ + 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 tcg_out_tb_start(TCGContext *s) { init_sub_buf(); --=20 2.43.0 From nobody Tue Sep 16 03:04:09 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=1756727454; cv=none; d=zohomail.com; s=zohoarc; b=fDM1fr8iDQjLAU5z7YEiI86pQB8eyqZhkLgiF8xy/WU04dUR+wokYrGxnZ5CyOL2vZM1Z/OIN+4EC96Jpv5B3s3VpstfUPJeYb3ZzaRYx3TMIzjGK+rNYFseSZo2WMNLC5K27fymEkEIxqxCPp2fpHrR6mPBHkHLSz4OAU2Zfb0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727454; 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=l5u/GKaMHlHVYWbQxiaNJLdfFOyogib0bqUIPS8H3LU=; b=dom1z+KGwGZrf/PN+/PhU4eKmGoFxQ6e2mM5oQ2AhLyNPP8QXXC+hB263Ja1cEPPj4rPjZuI292whJKw/4+AIPuusgp5lOP9cSJItsRIXtTLrUxUaw0Cj0XGM31h3uUHxpP9XkNKQ/gggGAo0+yHLINRji9Y3KbME1ohxH8v30Q= 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 1756727454279510.96379818303546; Mon, 1 Sep 2025 04:50:54 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut310-00023l-Tb; Mon, 01 Sep 2025 07:48: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 1ut30T-0001RN-0a; Mon, 01 Sep 2025 07:47:29 -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 1ut30L-0001cX-6X; Mon, 01 Sep 2025 07:47:28 -0400 Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-248de53d12dso25194535ad.2; Mon, 01 Sep 2025 04:47:14 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.47.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:47:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727231; x=1757332031; 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=l5u/GKaMHlHVYWbQxiaNJLdfFOyogib0bqUIPS8H3LU=; b=iYLUvMgWgbNS9VmVc0CRjp31MI9QcNbMIbqoceWhf95+KuEE5OOU4ZX7YDZFqBfsb7 EgZPp+nPXBk7CaAv583LQF21btpWSUVYtn+sK26CsWImp8htXSX9Chp4d+37puzeIXZ6 TOJ422in08ex+xT4wFhYSvxhHq50BODORCHD4Ws7afd1Ty5aLnY/EB4W1GO5iAaN1wlh 4oPGDpJTWw7za9r3ICODfyJhkLLY2goy97Gsz4xGwLIlfcq4zNJSJzePpyJQ7Ivzl4Ef u2yPGZ01NenwbdoA0jZf9NaAtcCYJKjOvDR6q6KRuMirQE+C8drgDHTfgEwtkvNtd3eT Lnwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727231; x=1757332031; 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=l5u/GKaMHlHVYWbQxiaNJLdfFOyogib0bqUIPS8H3LU=; b=n2XrsmVPrFLYs3WEN2KlrZyQi+XRSiDHYwgnN3Ufqf48TEGqewzebdUzhascpL/qH4 bFJXpAbUh/mIyNeeqxa6JszaZVMJf7ylhl1YnX7SuiCHwP7C2XDRFe3ucnDxyALL9wc3 OfnMyAeo3sHdeRAZVSujnW4cqw8kLG6HrroiXmCjN4SllcPP0RWUCnFYXn6Y63AQFk8W ZfMva4fqP38nTyCGM9Azj4+6H/h6hGjwgbKTD3L1Kt9Ny5i2mNFn9Vk9jjeb43/6pVT/ qtc3jTsbacikXfXVkssG/wbCX6ICQ1vVPG2j9ejOcylpBOi7/7Nud4PsZb28h2abMHmx DZNA== X-Forwarded-Encrypted: i=1; AJvYcCUgCoCjZMTFAF9fnnxit3yG/rJusHWUXlnguMVwGLNcwMH8jv7qrQ8lPH5DtDZv3HW7RhZdJjtfCAfRNA==@nongnu.org, AJvYcCXMF3VpKBMNheAE2CqiHPPP7XHW5V/bjvkr2cz0Cbm7N3aXT9P44j/JB4DXxZYuBcllajAnttDdcA==@nongnu.org X-Gm-Message-State: AOJu0YwSIQY51t5Z1gBGdbFDCq966d4mdLq0IHsuAskFyoGWwsB3nudh 8J2ymNdjc8W8pEz+8/tWiW8btUQAA1YYihTXevylnGAiIbTGmhKwgrD87dIc0A== X-Gm-Gg: ASbGncvCqB/T04yNlxVfsYwQN2hnG4hZlwR2ti5bIE5wFT6DlKHJrZEA+dqcMBEMFRS UUVvXmeHgWUchZglFGoXWzC2BoW/qnt4i2U7HbUy3NcG0kfo3zLbv66BNdq3L6Y3fFaueTvuizq ZEqwASzFSH4aSxxy5NLqhhVnSBoJqrmhL5Uv4r5NlYV8CtsW7rcD2aePwX0Ern9ySDMe3yzUMu3 FJp5sa1dl5KuGmRymDL7/IgEj5BMKea38yEEaGHHP7kgiKvw0yV4VclSNbVmFp2/ERWPwpC8eRE yUT1XhF7DOvIpz0x5WVZUFoWwK88/xdFAxU+YhLo59s6w4EC29v/XNTiG7O9+9dbvPHd/1l4OSs jB6uDIJIN/C2yzHJN5Kmfqs3uGKPQASf8 X-Google-Smtp-Source: AGHT+IEO4Vg8cH2671t3pzJq0DViJhQiPjnZwjXSmMqbnckDZCJoj7KZN1xofwgqQGBBlJaFSjzDrg== X-Received: by 2002:a17:902:d4cb:b0:249:3049:9748 with SMTP id d9443c01a7336-24944a98207mr97943645ad.35.1756727231385; Mon, 01 Sep 2025 04:47:11 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , 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 v3 19/35] tcg/wasm: Add rot/clz/ctz instructions Date: Mon, 1 Sep 2025 20:44:21 +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::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: 1756727457380116600 Content-Type: text/plain; charset="utf-8" The rot/clz/ctz operations are implemented using the corresponding instructions in Wasm. TCI instructions are also generated in the same way as the original TCI backend. Signed-off-by: Kohei Tokunaga --- tcg/wasm.c | 34 +++++++++++ tcg/wasm/tcg-target-opc.h.inc | 4 ++ tcg/wasm/tcg-target.c.inc | 107 ++++++++++++++++++++++++++++++++++ 3 files changed, 145 insertions(+) diff --git a/tcg/wasm.c b/tcg/wasm.c index a5e72d8fe5..2688ded58a 100644 --- a/tcg/wasm.c +++ b/tcg/wasm.c @@ -363,6 +363,40 @@ static uintptr_t tcg_qemu_tb_exec_tci(CPUArchState *en= v, const void *v_tb_ptr) tci_args_rr(insn, &r0, &r1); regs[r0] =3D ctpop64(regs[r1]); 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; + 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_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_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; default: g_assert_not_reached(); } diff --git a/tcg/wasm/tcg-target-opc.h.inc b/tcg/wasm/tcg-target-opc.h.inc index 5ed8c67535..092a5086ec 100644 --- a/tcg/wasm/tcg-target-opc.h.inc +++ b/tcg/wasm/tcg-target-opc.h.inc @@ -12,3 +12,7 @@ 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_clz32, 1, 2, 0, TCG_OPF_NOT_PRESENT) +DEF(tci_ctz32, 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) diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index 27f3a7414b..d547e7bf09 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -158,6 +158,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, @@ -169,6 +170,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, @@ -180,6 +182,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, @@ -187,7 +191,11 @@ typedef enum { OPC_I32_REM_U =3D 0x70, 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, @@ -202,6 +210,8 @@ 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, OPC_I64_EXTEND_I32_S =3D 0xac, @@ -212,6 +222,7 @@ typedef enum { =20 typedef enum { BLOCK_I64 =3D 0x7e, + BLOCK_I32 =3D 0x7f, } WasmBlockType; =20 #define BUF_SIZE 1024 @@ -563,6 +574,42 @@ static void tcg_wasm_out_ctpop64(TCGContext *s, TCGReg= ret, TCGReg arg) tcg_wasm_out_op_idx(s, OPC_GLOBAL_SET, REG_IDX(ret)); } =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_out_op_ri(TCGContext *s, TCGOpcode op, TCGReg r0, int32_t = i1) { tcg_insn_unit_tci insn =3D 0; @@ -1182,6 +1229,66 @@ static const TCGOutOpUnary outop_ctpop =3D { .out_rr =3D tgen_ctpop, }; =20 +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); + tcg_wasm_out_o1_i2_type(s, type, OPC_I32_ROTL, OPC_I64_ROTL, a0, a1, a= 2); +} + +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); + tcg_wasm_out_o1_i2_type(s, type, OPC_I32_ROTR, OPC_I64_ROTR, a0, a1, a= 2); +} + +static const TCGOutOpBinary outop_rotr =3D { + .base.static_constraint =3D C_O1_I2(r, r, r), + .out_rrr =3D tgen_rotr, +}; + +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); + tcg_wasm_out_cz(s, type, OPC_I32_CLZ, OPC_I64_CLZ, 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); + tcg_wasm_out_cz(s, type, OPC_I32_CTZ, OPC_I64_CTZ, 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 tcg_out_tb_start(TCGContext *s) { init_sub_buf(); --=20 2.43.0 From nobody Tue Sep 16 03:04:09 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=1756727328; cv=none; d=zohomail.com; s=zohoarc; b=HUKptyV3nSedY0Y8wx2d7t5YRVhLGEA9dUj1gQvldZtTpPTSdxLdzxxvAmu5gUE472sLYIJ6ecQy2tznTEEl7Cvj2G+k72qrWdGiOVajkWZ25N9pibZ8hSOkGTVUJh/+v2mZrGQ71sYviEM36qfiLozLw7jg0+XAZkZOZ34EzZI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727328; 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=Q7542XmawGswesZm/4ukj34BjNRmEdyBU7qF3DrN0sM=; b=VlSy7mGa+C7g7BDlcfLCg5IecaIhdvgYaeCZr7lbTLSlqqTWoUUfNZT95WrZ+mQxt/TdznFxZKkOR6a9WFDMxqqwYJejuNwzR3t0Cswvqw8m3kWyXdWoKKiG8E18PiL0LfjIhUf0/DM7xc0SD5n4v396Q1n7OdGe9d/0n7YJTB0= 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 1756727328109147.25740886082838; Mon, 1 Sep 2025 04:48:48 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut31B-0002Se-NF; Mon, 01 Sep 2025 07:48: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 1ut30Z-0001Xx-UL; Mon, 01 Sep 2025 07:47: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 1ut30R-0001e1-LS; Mon, 01 Sep 2025 07:47:34 -0400 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-248c7f955a2so37337315ad.2; Mon, 01 Sep 2025 04:47:19 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.47.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:47:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727237; x=1757332037; 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=Q7542XmawGswesZm/4ukj34BjNRmEdyBU7qF3DrN0sM=; b=Pmhu57nP76bc4g6481KRqVKDgVEOxknF4XWnLPMC+yoKgV9N9CKctRPRxb0zeCqHRd TUhEjpCBxSpj/gvKvhvQDa9MekotvzN3RB94CvlWFT6p+XUOXjVNPqm6nmrfmib7OUX+ gBuCRuFlWFOYpRSUh7clnTqhn6zuauKKXFQOoPyGbZgseBGCLYudO5+Ewbedux5I9OOm KmpdO6SgYThgjm3WXOYuUirYvG9uFVDUOkdUIgwQhikatCsfOsPMgbT1CwfzcAl5QXIc R5RWijjyGqfOkV4dXNZXfE9VM25+rTjPeWaocQNKn/jRJEpiAbgO/U+9Y8DADRRZ48AC aDsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727237; x=1757332037; 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=Q7542XmawGswesZm/4ukj34BjNRmEdyBU7qF3DrN0sM=; b=NB6A2At51WKix1gwbFSULW3kyM25VNgV7x9TsrRl1IsjQ3E17n5NU/klIt6BdnqAix NRy9QiPOstT3qJ/1vT+3+LGt0Uv/JQyfLnxHQUFaILMX9Q9uZljsg5x1RfQWr8SndJB5 YT+p1GCqalPqiWRleCHIa6R/ouVzMmkxAAe0IiBDRgBbavJ859BziK2fDFM82xd7J4Pv R/usDOxmVFU1YxBbXUrPJ8nVaO+UBg2ExMJnP/OL7ZNV+7rzJX1jp2+Npo532Gp1IhqR jshrhgsVLwkcJFp5PAZSlneGo9LanNHykjmPBJfWgP1PwgJ8YJjmWoclikBypg2YVrFG kU3A== X-Forwarded-Encrypted: i=1; AJvYcCWn8SLzZRWZ7M3mo62z/1z6WoSFFpUQulOqs8dG5rAiTVxdcVclg6DcWvYkVn4+8gqiQ5cKFXiAHG3c3g==@nongnu.org, AJvYcCXrMWy+6C/adIFpbCCrkTwZhl99JsRibW1XI092bSODcLAYOv+LTyw/jFAo8zWBHTFZPtSw+ma40A==@nongnu.org X-Gm-Message-State: AOJu0YwBbQbQ8s9M0XO+j3yCAgOSCGUsAiPFTnpSQ6WZ1oTFpdLYblDU jTY4S62nLrXo6SE8IkztUQB28uYLNfUSSd3WLd673VHkhNY+vKqSsjtrg7mj2Q== X-Gm-Gg: ASbGnctJ6aZZNoSFusecGk7BHvKhlnmnExOBzKy6d2jeRi7cHYbHpempEWRaZMhNQDv QVJVlimQggpYiU6nJE/OvYHFAyQdfYt+vbKyHVX7uOE7rfyvgB0Jb2dhT2dt2XnHyHriZZvULAF 8NXVOrhsYw/NsHjSlnCDn8kUgbo/A8lBUSGJoHgsKzqj9k3GGRUL5oL2o3FRXOhWFGi0S26cdcj 06gbu9WKHG3R75ATDqOQ7cl3F72zM5HFQP9PVm0U1h6Rid/fkQnse6F0Z/ZzbyKHZe/vWiws9vT X5FCNuJB2pCjghFCCt076aDHC2wnpAbf/c53VDJVhURi3KjbGtcT0ensJYupI26sw8nmdD8oLsW MclEjq0nJuXrhmQzX+kmajzg2KIRFZur+ X-Google-Smtp-Source: AGHT+IHTFKCpiG/ntV1EjNQX9r6mqNgQzbyYUamQdVBJk4ROgaiFb2Y9jYuLAx7RP8waSN4x2LYHwA== X-Received: by 2002:a17:902:e790:b0:248:79d4:93ba with SMTP id d9443c01a7336-24944b2a01amr101218455ad.39.1756727237139; Mon, 01 Sep 2025 04:47:17 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , 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 v3 20/35] tcg/wasm: Add br/brcond instructions Date: Mon, 1 Sep 2025 20:44:22 +0900 Message-ID: <50b395e34f495770da40de232b67adaf4a47cdf7.1756724464.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: 1756727329067116600 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 Wasm 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. TCI instructions are also generated in the same way as the original TCI backend. 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.c | 16 +++ tcg/wasm/tcg-target.c.inc | 196 +++++++++++++++++++++++++++++++ 13 files changed, 329 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 afac55a203..7d3e7f8cb1 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 #ifndef CONFIG_TCG_INTERPRETER /* 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.c b/tcg/wasm.c index 2688ded58a..ca67436192 100644 --- a/tcg/wasm.c +++ b/tcg/wasm.c @@ -22,6 +22,12 @@ #include "tcg/tcg.h" #include "tcg/tcg-ldst.h" =20 +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_rl(uint32_t insn, const void *tb_ptr, TCGReg *r0, void **l1) { @@ -397,6 +403,16 @@ static uintptr_t tcg_qemu_tb_exec_tci(CPUArchState *en= v, const void *v_tb_ptr) tci_args_rrr(insn, &r0, &r1, &r2); regs[r0] =3D ror32(regs[r1], regs[r2] & 31); break; + case INDEX_op_br: + tci_args_l(insn, tb_ptr, &ptr); + tb_ptr =3D ptr; + continue; + case INDEX_op_brcond: + tci_args_rl(insn, tb_ptr, &r0, &ptr); + if (regs[r0]) { + tb_ptr =3D ptr; + } + break; default: g_assert_not_reached(); } diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index d547e7bf09..ca10f97ed8 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -136,10 +136,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 +/* Global variable used for storing the current block index */ +#define BLOCK_IDX 16 + 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_GLOBAL_GET =3D 0x23, OPC_GLOBAL_SET =3D 0x24, =20 @@ -221,6 +227,7 @@ typedef enum { } WasmInsn; =20 typedef enum { + BLOCK_NORET =3D 0x40, BLOCK_I64 =3D 0x7e, BLOCK_I32 =3D 0x7f, } WasmBlockType; @@ -276,6 +283,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. @@ -286,6 +304,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); @@ -610,6 +632,134 @@ static void tcg_wasm_out_cz( } } =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 void tcg_out_op_l(TCGContext *s, TCGOpcode op, TCGLabel *l0) +{ + tcg_insn_unit_tci 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_ri(TCGContext *s, TCGOpcode op, TCGReg r0, int32_t = i1) { tcg_insn_unit_tci insn =3D 0; @@ -621,6 +771,16 @@ static void tcg_out_op_ri(TCGContext *s, TCGOpcode op,= TCGReg r0, int32_t i1) tcg_out32(s, insn); } =20 +static void tcg_out_op_rl(TCGContext *s, TCGOpcode op, TCGReg r0, TCGLabel= *l1) +{ + tcg_insn_unit_tci 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_tci insn =3D 0; @@ -1289,7 +1449,43 @@ static const TCGOutOpBinary outop_ctz =3D { .out_rrr =3D tgen_ctz, }; =20 +static void tgen_brcond(TCGContext *s, TCGType type, TCGCond cond, + TCGReg arg0, TCGReg arg1, TCGLabel *l) +{ + 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); +} + +static const TCGOutOpBrcond outop_brcond =3D { + .base.static_constraint =3D C_O0_I2(r, r), + .out_rr =3D tgen_brcond, +}; + +static void tcg_out_br(TCGContext *s, TCGLabel *l) +{ + tcg_out_op_l(s, INDEX_op_br, l); + tcg_wasm_out_br(s, l); +} + 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 2.43.0 From nobody Tue Sep 16 03:04:09 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=1756727509; cv=none; d=zohomail.com; s=zohoarc; b=VqkM1i1e289BVsd5tH4gS4f6plvefwrsjL3hgbXSX78xnBe4As+/YFhhhcnjaNIcJoJBnCKCBY90Q/u5Fthnc6TONxLqAnQkYv7NeaDaSV2Gclahel5TXtMK4P3e7wuv9PXbTTe0eiwTU3m1+/yvl7qe6tO26aI71giY7XG3CNM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727509; 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=7bJBbZ+Iwv2WKJz+iQgt35bTAqawC+zEuhzRbMD4jxc=; b=WJBcUtCdTx5AgOR2PJXFIlYCYfNmBQzU9dwII6YnD5rknCRNqaV176ptPWUcIImVMsYu0xcS1A+SpKhK68ibXU0ClkRvmm7itu6QzK37wDODYItDMYhRPwMFGpBN0/5zYd7FoEO/TFVPcN70csowoi+Kx7G/JhVK0czkDni9VAw= 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 1756727509488561.3891849302057; Mon, 1 Sep 2025 04:51:49 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut31E-00030e-Qq; Mon, 01 Sep 2025 07:48:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ut30Z-0001Xy-UN; Mon, 01 Sep 2025 07:47:44 -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 1ut30U-0001fK-OY; Mon, 01 Sep 2025 07:47:34 -0400 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-2445805aa2eso43016385ad.1; Mon, 01 Sep 2025 04:47:24 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.47.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:47:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727243; x=1757332043; 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=7bJBbZ+Iwv2WKJz+iQgt35bTAqawC+zEuhzRbMD4jxc=; b=K6HhJJpTB3E5g1NXhmwpP6jSBXgJcF+nBCBJBYbyY/hEM6qudsj2iYYST4iqE+gjau uOs5gJWZhBAKBg8J99jZA0R4HtBgl5ij0eW++cfQJHK4hAbLXMGrDHsFh9hwclBbm5Yo 12QVKkRooMwCtbbF4fedpEL3hUx2i+QzKyZLELdbKf481XLZe+jYdTy4wqsEJJ33a8+n f6AW+vpQ9MS6i5FbbuN7AD8X/Tx4HAIAGicb2Ldcs73/t+PQr2gwieZsMXJZBYtgEnQF ReQ2nI6jLfKbnwzsHQmudjca1y5u9l8Pj1IzEP1A9JWx9wjl7+3NWjKx9vatGEzEFFW4 8/Ug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727243; x=1757332043; 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=7bJBbZ+Iwv2WKJz+iQgt35bTAqawC+zEuhzRbMD4jxc=; b=dFFGbwP8VAOnAW61xv2fvD7VNZfwtfIq04YFb1yWDty4irps5b01P6uu2vCzj6GGva asw7nTjWftSVgxllIY4hhq4EpruF0FFhK8tmxbc/hF6Wajn0JOzEH1YN06Ddt8eq1fcX 5R5XxKkIFHraRT2xDqMQRbkNnjQc74e0JdzKL52DN9bsj/mSIs8xtm0I6EL2iQ37rRqo +RXE2dsAObtRY0kIWZZC4QwadT0T3XAjaBKcRN859HinAVT1vnzPgWEXDKe/VFD4jjRh 47xrK+p3QwGv4BSkwbbuv4WA1AAEa6bpLjSWdO6fDvwPNiojXRCqZpnzLJx2F+ljbpxM swFg== X-Forwarded-Encrypted: i=1; AJvYcCUmelVlKHy/mWB1yUv70l7jIY6n1ObobEcEJ1Hjo2qplzJKu66BzkXjgV9wVihv7Hcey5qqDO7p7r7TfQ==@nongnu.org, AJvYcCXRxrgD/CCUjHMkc4ip0XZmcMU+xv3swjPrN4IT9mWY6Wu14aM/EixnlAuK0CetyYdpmXn9OOC+kg==@nongnu.org X-Gm-Message-State: AOJu0YwXpkNJsxVx2izPhfihWF18aHceyctObpYIe1PvuwRVZmBIseW3 YmB47hexJdZrLqeTcZTOU2Y7sOU4P8/n4ZVRg1W1WiU+wZ8k9lQ0OB5xF7286A== X-Gm-Gg: ASbGncvtOWVMnPR/IYix0RtPSzKzRpkDtv7vULZ5DDYPxf02bhPeWsFxuUIFfSTL1GY WfLpNbiLOLzlBXi8MPdR7UziN5ti6NHFgutOFGCamgQUVbyRWbVctbnwxGLL6zSHY6Pn2fQ8+34 cg6SlKsCTTyH61C/t1DTp691QfeIiJJYMANqFD0SG+NDZdPxl2RthXS4AJN8rPYCV6fEu35PQhp TpnG5//jGKMMxJDEyf8FHdxJwPKRz1/Xi3QXbuYc9MPW6kbuNhHq8hhSa4iDLLFqGunN3MkPhcM kQdWQ5EFk+ZdIi7C/zmyoRmhWBjJ5w2/ehidHDYBZwNi1pRvvvvHx+7Ue9eRyucNAAKli3Ojuyu pQYAzOz2cVEQYG8lDV4pbNA== X-Google-Smtp-Source: AGHT+IGHeax/fqpI5YZ2Y+MLrxoWZYnkFRqSUC3w6Yc5vJYkarWfDKe0qe/WMQ/r9TQGeju5IocC7g== X-Received: by 2002:a17:903:1211:b0:249:34b:29fe with SMTP id d9443c01a7336-24944aa2e2bmr83973035ad.32.1756727242820; Mon, 01 Sep 2025 04:47:22 -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 v3 21/35] tcg/wasm: Add exit_tb/goto_tb/goto_ptr instructions Date: Mon, 1 Sep 2025 20:44:23 +0900 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::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: 1756727511055116600 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. TCI instructions are also generated in the same way as the original TCI backend. Signed-off-by: Kohei Tokunaga --- MAINTAINERS | 1 + tcg/wasm.c | 20 ++++++ tcg/wasm.h | 15 +++++ tcg/wasm/tcg-target.c.inc | 136 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 172 insertions(+) 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.c b/tcg/wasm.c index ca67436192..c54c5c5b2c 100644 --- a/tcg/wasm.c +++ b/tcg/wasm.c @@ -28,6 +28,11 @@ static void tci_args_l(uint32_t insn, const void *tb_ptr= , void **l0) *l0 =3D diff ? (void *)tb_ptr + diff : NULL; } =20 +static void tci_args_r(uint32_t insn, TCGReg *r0) +{ + *r0 =3D extract32(insn, 8, 4); +} + static void tci_args_rl(uint32_t insn, const void *tb_ptr, TCGReg *r0, void **l1) { @@ -413,6 +418,21 @@ static uintptr_t tcg_qemu_tb_exec_tci(CPUArchState *en= v, const void *v_tb_ptr) tb_ptr =3D ptr; } break; + 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; default: g_assert_not_reached(); } diff --git a/tcg/wasm.h b/tcg/wasm.h new file mode 100644 index 0000000000..9da38e4d0e --- /dev/null +++ b/tcg/wasm.h @@ -0,0 +1,15 @@ +/* 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 ca10f97ed8..c077c8ad7c 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 typedef uint32_t tcg_insn_unit_tci; =20 @@ -139,6 +140,9 @@ static const uint8_t tcg_target_reg_index[TCG_TARGET_NB= _REGS] =3D { /* Global variable used for storing the current block index */ #define BLOCK_IDX 16 =20 +/* Local variable pointing to WasmContext */ +#define CTX_IDX 0 + typedef enum { OPC_UNREACHABLE =3D 0x00, OPC_LOOP =3D 0x03, @@ -146,6 +150,8 @@ typedef enum { OPC_ELSE =3D 0x05, OPC_END =3D 0x0b, OPC_BR =3D 0x0c, + OPC_RETURN =3D 0x0f, + OPC_LOCAL_GET =3D 0x20, OPC_GLOBAL_GET =3D 0x23, OPC_GLOBAL_SET =3D 0x24, =20 @@ -751,6 +757,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 void tcg_out_op_l(TCGContext *s, TCGOpcode op, TCGLabel *l0) { tcg_insn_unit_tci insn =3D 0; @@ -760,6 +841,35 @@ static void tcg_out_op_l(TCGContext *s, TCGOpcode op, = TCGLabel *l0) tcg_out32(s, insn); } =20 +static void tcg_out_op_p(TCGContext *s, TCGOpcode op, void *p0) +{ + tcg_insn_unit_tci 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 + 4); + 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_tci 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_ri(TCGContext *s, TCGOpcode op, TCGReg r0, int32_t = i1) { tcg_insn_unit_tci insn =3D 0; @@ -1468,6 +1578,32 @@ static void tcg_out_br(TCGContext *s, TCGLabel *l) tcg_wasm_out_br(s, l); } =20 +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); +} + +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); + tcg_wasm_out_goto_tb(s, which, (intptr_t)s->code_ptr); +} + +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); +} + +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 tcg_out_tb_start(TCGContext *s) { init_sub_buf(); --=20 2.43.0 From nobody Tue Sep 16 03:04:09 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=1756727772; cv=none; d=zohomail.com; s=zohoarc; b=TfGJrmT+asgvvWE7AGzNGxakEH1CSdkCzNOcGUQqYKxHqq4I7uLfldTquPuqJXxex9h/f+5+l6VW6TT3kvW4R6dAeKDIlGRtMq1pXvFBs1SxffrrRiIrRnKWxU3+gwc3IN1nohPpUWIkr3yW+4Cw/4ZYqK9sa8UOXBVF4tCexzM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727772; 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=qtzCpMZhgIt7AYckUzYbs844uv8hugjtHhfrUacfFXw=; b=jwx2NB7AJP47k6qYDepQWtG5aWk3vBfBBoqdIm03TrEZXNixPcrlwhQjrhD2QY1vTOsK3GZVcQ+yeOim+NM6qov2r3jIBI+0kmAGHTsoV9u5qMH0Ml/x5QX0TDirBhqVNm75qw9MXi98uwJcXwL0fM9oVh5x7CBymYKztXqVJnQ= 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 1756727772121479.2308655220937; Mon, 1 Sep 2025 04:56:12 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut31y-0004CY-RE; Mon, 01 Sep 2025 07:49: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 1ut30n-0001hx-RI; Mon, 01 Sep 2025 07:47:51 -0400 Received: from mail-pf1-x42e.google.com ([2607:f8b0:4864:20::42e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ut30c-0001gx-2w; Mon, 01 Sep 2025 07:47:46 -0400 Received: by mail-pf1-x42e.google.com with SMTP id d2e1a72fcca58-76e4fc419a9so4196665b3a.0; Mon, 01 Sep 2025 04:47:31 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.47.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:47:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727249; x=1757332049; 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=qtzCpMZhgIt7AYckUzYbs844uv8hugjtHhfrUacfFXw=; b=nK1vkW2WLCnpTor2w7sjyeMMRL8oKuBby/VH8dqHaAbV80g/BoAOmxPUSRM7APtgaa 3MN3HUIes4bmWRNvNZ7/xAvmGzzDaWBhZ4c8atlwgp4vsTJw+ho7PS6+wRQz19QSxzBT gqM/jyYZkFQKuJe+i/Hkbdz0XI10u1+GNH/5V8mFOoDV1hdu8UAeQ4AC+LkuQNaolANy zr0nNG2bg3gB7Qx7EkgY1vAAJWqnjPDfd8jI4AX5kz2b/zLrtN+nr+ncvyxTXw7hJ4yS YqwQIUnXiMT183K9ogDskulKR6WKiMGWgxDw+3vfPaT95nddfzPhBVbRxltd/ll7qN4P Kipg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727249; x=1757332049; 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=qtzCpMZhgIt7AYckUzYbs844uv8hugjtHhfrUacfFXw=; b=CGCqGGh0kFPf8fTxW+SfEDvf6mitOv8ynvi+CZZZnXCi7s5Yx/RJ3OW55VuWde3do+ vq6KW3Lmfvhh5EMJHEQMGvn94EIQcFXBvXv5Gj8kcJ0Ue/w8hxysu4MkfW+k78hKCqY3 TkcC+1AFBmIRlJ+4JCzqDB4w2FSIFk8WEpTjuyvmEDPHy7xUFFR4HH8HlUO2jP9xQjW/ vpYqHTTMNmSALBPeidK4s7gn7FtyC36kB6tSiWQY/IYnVXmVZhBAIjLbVZAUwBfO7KTJ Ndy3bflluAgheXQ8t+oCwP7S5+0zXMu1W0lyQeqX8+G4ad1QEpJChfsikBn/4OmC2K1v hlbQ== X-Forwarded-Encrypted: i=1; AJvYcCV6KWNRdFtJ53AR864MJjlPkCr7VoBTqRmUWmyCwr2hK7HHcDFFppQqp+uS/iLiVyC4KCHMDkkBBw==@nongnu.org, AJvYcCVXcBMRRTCGax0dGiYRNFE9fIQcpPlEDEof3wlFOGDVE3sK8rfb1o7bc7taqw1WK7T2lB5hpa7aAHz0pg==@nongnu.org X-Gm-Message-State: AOJu0Yz+HUAdaFRGmv0+WuAuGjDjymszkv6pZA+JNKMcI/kovUE+aQIJ enUUhsiVv2wn5ueEXK1Uly0bKEqHC1dVaKOfonJeWQ8qyn/XPrfQGWamkYrhbQ== X-Gm-Gg: ASbGnctsfR392TEANMhUoNIfp2uI5+T4JLkHurv8IwJXMyjLUyBuvaLrLTHynUw/cYX BHQToL2/nRrV/9tqGfS5751bX/6NP4FzRZRrGB/0efFTkzObQiPAp5xT/XVXi2cK9Cx7dG+pu9T lQqBBLDVlbkrhiTcJLYKA58jNOnEPNdKok1VHwjdBXF3IMmwG96DUkO9ogsRD/3diObAzFRpsLN FZI/JdH00V2/SORKPi5dtpDedOPRgx6qA9JyCPLjMbnQKa/ZmrybuTA3uWrdRRI2ymHEXEZOt0B m+UGA7b33odQ/8ClnGafqOJ5BjML7aYNLWuBwvd1BzMnWAtJIUiw2QugIA3rsYm1LIlDy3o2wzu Fjwc398SEUILRAAbDrITwaA== X-Google-Smtp-Source: AGHT+IHfcqIw9dgU1rEYY4zsbl/g9tBwWiy/Vs/kHJW7TUWu0W+KfSvXD/dCwLBmL2mFdtc/r6utQQ== X-Received: by 2002:a17:902:c405:b0:244:6a96:6912 with SMTP id d9443c01a7336-24944a56332mr106636255ad.20.1756727248683; Mon, 01 Sep 2025 04:47:28 -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 v3 22/35] tcg/wasm: Add call instruction Date: Mon, 1 Sep 2025 20:44:24 +0900 Message-ID: <942c6e41ff9418ae82537b2b8eb81af543997dcc.1756724464.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::42e; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pf1-x42e.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, T_SPF_TEMPERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1756727774616124100 Content-Type: text/plain; charset="utf-8" To call QEMU functions from a TB's Wasm module, the 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. TCI instructions are also generated in the same way as the original TCI backend. [1] https://github.com/WebAssembly/tool-conventions/blob/060cf4073e46931160= c2e9ecd43177ee1fe93866/BasicCABI.md#function-arguments-and-return-values Signed-off-by: Kohei Tokunaga --- tcg/wasm.c | 63 +++++++++++++ tcg/wasm.h | 10 +++ tcg/wasm/tcg-target.c.inc | 183 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 256 insertions(+) diff --git a/tcg/wasm.c b/tcg/wasm.c index c54c5c5b2c..db0c213d92 100644 --- a/tcg/wasm.c +++ b/tcg/wasm.c @@ -21,6 +21,10 @@ #include "qemu/osdep.h" #include "tcg/tcg.h" #include "tcg/tcg-ldst.h" +#include "tcg/helper-info.h" +#include + +__thread uintptr_t tci_tb_ptr; =20 static void tci_args_l(uint32_t insn, const void *tb_ptr, void **l0) { @@ -33,6 +37,13 @@ static void tci_args_r(uint32_t insn, TCGReg *r0) *r0 =3D extract32(insn, 8, 4); } =20 +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) { @@ -204,6 +215,58 @@ static uintptr_t tcg_qemu_tb_exec_tci(CPUArchState *en= v, const void *v_tb_ptr) opc =3D extract32(insn, 0, 8); =20 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_and: tci_args_rrr(insn, &r0, &r1, &r2); regs[r0] =3D regs[r1] & regs[r2]; diff --git a/tcg/wasm.h b/tcg/wasm.h index 9da38e4d0e..a3631b34a8 100644 --- a/tcg/wasm.h +++ b/tcg/wasm.h @@ -10,6 +10,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 c077c8ad7c..0606b7de79 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -28,6 +28,14 @@ #include "qemu/queue.h" #include "../wasm.h" =20 +/* Used for function call generation. */ +#define TCG_TARGET_CALL_STACK_OFFSET 0 +#define TCG_TARGET_STACK_ALIGN 8 +#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 + typedef uint32_t tcg_insn_unit_tci; =20 static const int tcg_target_reg_alloc_order[] =3D { @@ -143,6 +151,9 @@ static const uint8_t tcg_target_reg_index[TCG_TARGET_NB= _REGS] =3D { /* Local variable pointing to WasmContext */ #define CTX_IDX 0 =20 +/* Function index */ +#define HELPER_IDX_START 0 /* The first index of helper functions */ + typedef enum { OPC_UNREACHABLE =3D 0x00, OPC_LOOP =3D 0x03, @@ -151,6 +162,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_GLOBAL_GET =3D 0x23, OPC_GLOBAL_SET =3D 0x24, @@ -832,6 +844,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 void tcg_out_op_l(TCGContext *s, TCGOpcode op, TCGLabel *l0) { tcg_insn_unit_tci insn =3D 0; @@ -1604,11 +1757,41 @@ void tb_target_set_jmp_target(const TranslationBloc= k *tb, int n, /* Always indirect, nothing to do */ } =20 +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_tci 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); + tcg_wasm_out_call(s, (intptr_t)func, info); +} + 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 Tue Sep 16 03:04:09 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=1756727833; cv=none; d=zohomail.com; s=zohoarc; b=ktIjooAJDgM3Tl/RTo4TbGbrVv4e2+ltui3tXzCqjzRLPr9Lq6Joim6/XIjA9RB7EM/PAuLj60W7h4geJdyKKJ84dsdiLHYjmbJGW3R9mpZoRMcij+cz+WkWlRHWmv4kH9lsGad/HRR65qQ5lu1m/aiNXVEKh6g1S+n5vGRIXZQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727833; 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=8qg4IJG86K4G0mySo/uPZ1k8JSbANwuevmlQrqUFr+A=; b=dztse2wRLqx0wG7I8bN6j8H+NaCphdkTRi8z8UQXO2fVchD4EYy8L7GdN0Se+3mM0GkyEoXO/9YwoYL53neZhFRqcPp/UttChBbPkVPTDJKPV0e80h2vOThDPHijXgpB3FlgrDLs/OJ+h2i4PMImANThma4m7Sjj14DEkc+Qs10= 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 1756727833538986.2625586394191; Mon, 1 Sep 2025 04:57:13 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut31L-0003MF-V1; Mon, 01 Sep 2025 07:48:25 -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 1ut30o-0001iN-81; Mon, 01 Sep 2025 07:47: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 1ut30h-0001i0-7J; Mon, 01 Sep 2025 07:47:49 -0400 Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-76e4f2e4c40so3475898b3a.2; Mon, 01 Sep 2025 04:47:37 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.47.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:47:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727255; x=1757332055; 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=8qg4IJG86K4G0mySo/uPZ1k8JSbANwuevmlQrqUFr+A=; b=np6g47wj7XPcoCHpFX8E7VSm2r7YVm3cwbtm/SlQQfxx3RFp7OvDrX8WPUaeq/YU0j B2sj17vZmLmhfJymXior/VouVtY4l5nXO8skz5Ka1qbUX8eMsXNEcQeIfudDYsmqbWBp 11JycfNbzDPzl4ztz1anA1uLmfDx6mAyK2BKUE7nIy7YPRrtXwUhZwA24gSVDyRN9+Ox 8oH+KMfrd0B7vqc4ML+2Rbrymfv1lfuYPA1k0wl5xreCHUdbodmTSysVuHEHy11WWN1C T8dW41AiOxTLkI05XfqL2azC1AbPw+GBRpSRZG5WBkuyxK3Bnu+cszSHGz1KrXnZ6TJM ZwAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727255; x=1757332055; 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=8qg4IJG86K4G0mySo/uPZ1k8JSbANwuevmlQrqUFr+A=; b=Y2KT6FjOUHImVsMi01Vo+94BdL1PAQNF4qRReHKrj+b5W0H5ojGl8kfV2wYU+lCbOI q9ByAbwrW7hZN2g1t71/AE7Dh5jPLYqBEVsHaYlnE6Dq5QJMRdmN5zUtvnioig5w7HeR UNubw+QfVR90ft8SzRI0o2uNxykwZMIqQCBXuNxvGcxIohLAqxYm0sBZr/N1sHGS4ORO GHukd/KpUNlAaY1gMW3cjfkw8kmHm7W46tCGHJpuPFhhD+mN62qebvzyE2ZB0PK/0Sk8 ZJL0tVHBWAYRzgWIJKiXN2QjnWsQ2OZjZbrPp1k8xdN4ExGzyPYmruBg/iVdR2a+czAA g2xA== X-Forwarded-Encrypted: i=1; AJvYcCWfZHDpgD1R3dm3G5yxAEUicXRVeFkRk3G3nJkyS48IlmJKjtsDRokjNrvVR4DTji/x0LFm/2ISYA==@nongnu.org, AJvYcCWugLMd8zbRp7ovoiz67HXDVIjI1jME/MtTWaid+cg3OiNLaOzj8vWWAFUmlkTjRX7SgVoOiwPI7zT+4w==@nongnu.org X-Gm-Message-State: AOJu0YzlmXMhluwhUFTmz1/ZzPCa7t4PEh/gaRV8nPrLE9VC3RqsmMXn Qrfi8h6a2iO6t8gq7P0l3WFeDBPnGIAM//hU945tayswjpMUz5EuR5HwezUg1Q== X-Gm-Gg: ASbGncujhnYv7fyklDB7E3GeWe9MtjYLOoCAwsmneEdKbZ6XsaiqDMmHZ2dBrXN571C wOYKcjrnU7G3j4+PFH9FLtYy/YzFuK9DQ57fmz0dWpi29k55EWS+WVtbt67UIJmv7xJdu9VJ/FX V26syYhcOMmgKa/U5oeKyVQhJOWw/UgiPYhjl72OLmc7cZK67p6WiSZPHtqnsisje4n3IE7Tjrh m17CaViR2lUEQm8V9LisUChnA2sHuPrlJ9rDTsES7FDdhj6uvjUooGBmzLLGfX0XjA2hQVpvwhu dh2OcGTY/vZ982k0e6t/1l3gj2i9kmaiooWq7fhsA+/VbpSROIfCCEVKNhFgsxAnZftS1n1osJj xrfGuFx7C/OVjRvYFzkdtqsxjelQ/GOk5NU9iN0rY690= X-Google-Smtp-Source: AGHT+IHgfCQ6xs2pRWt9YleEk94Rul4l0F8g7Ef29vAdFpFNuEvpbGUl+VALyCU7vI0a5me546IZ1w== X-Received: by 2002:a17:903:2a85:b0:249:f73:bb9a with SMTP id d9443c01a7336-24944b15dbdmr78452345ad.51.1756727254258; Mon, 01 Sep 2025 04:47:34 -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 v3 23/35] tcg/wasm: Add qemu_ld/qemu_st instructions Date: Mon, 1 Sep 2025 20:44:25 +0900 Message-ID: <85196f153dd3d5c11c9b4701a4f56dc082348b31.1756724464.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: 1756727835369124100 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.c | 70 ++++++++++++++++++ tcg/wasm/tcg-target-has.h | 1 + tcg/wasm/tcg-target.c.inc | 145 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 216 insertions(+) diff --git a/tcg/wasm.c b/tcg/wasm.c index db0c213d92..793c1807c2 100644 --- a/tcg/wasm.c +++ b/tcg/wasm.c @@ -63,6 +63,14 @@ static void tci_args_ri(uint32_t insn, TCGReg *r0, tcg_t= arget_ulong *i1) *i1 =3D sextract32(insn, 12, 20); } =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); @@ -190,6 +198,56 @@ static bool tci_compare64(uint64_t u0, uint64_t u1, TC= GCond condition) return result; } =20 +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(); + } +} + static uintptr_t tcg_qemu_tb_exec_tci(CPUArchState *env, const void *v_tb_= ptr) { const uint32_t *tb_ptr =3D v_tb_ptr; @@ -208,6 +266,8 @@ static uintptr_t tcg_qemu_tb_exec_tci(CPUArchState *env= , const void *v_tb_ptr) uint8_t pos, len; TCGCond condition; uint32_t tmp32; + uint64_t taddr; + MemOpIdx oi; int32_t ofs; void *ptr; =20 @@ -496,6 +556,16 @@ static uintptr_t tcg_qemu_tb_exec_tci(CPUArchState *en= v, const void *v_tb_ptr) } 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; default: g_assert_not_reached(); } diff --git a/tcg/wasm/tcg-target-has.h b/tcg/wasm/tcg-target-has.h index a29ceb2ea5..8fe9b45403 100644 --- a/tcg/wasm/tcg-target-has.h +++ b/tcg/wasm/tcg-target-has.h @@ -4,6 +4,7 @@ =20 #define TCG_TARGET_HAS_tst 0 #define TCG_TARGET_HAS_extr_i64_i32 0 +#define TCG_TARGET_HAS_qemu_ldst_i128 0 =20 #define TCG_TARGET_extract_valid(type, ofs, len) 0 #define TCG_TARGET_sextract_valid(type, ofs, len) \ diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index 0606b7de79..e1ee2f6485 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -985,6 +985,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 void tcg_out_op_l(TCGContext *s, TCGOpcode op, TCGLabel *l0) { tcg_insn_unit_tci insn =3D 0; @@ -1054,6 +1147,19 @@ static void tcg_out_op_rr(TCGContext *s, TCGOpcode o= p, TCGReg r0, TCGReg r1) tcg_out32(s, insn); } =20 +static void tcg_out_op_rrm(TCGContext *s, TCGOpcode op, + TCGReg r0, TCGReg r1, TCGArg m2) +{ + tcg_insn_unit_tci 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) { @@ -1786,6 +1892,45 @@ static void tcg_out_call(TCGContext *s, const tcg_in= sn_unit *func, tcg_wasm_out_call(s, (intptr_t)func, info); } =20 +static void tgen_qemu_ld(TCGContext *s, TCGType type, TCGReg data, + TCGReg addr, MemOpIdx oi) +{ + tcg_out_op_rrm(s, INDEX_op_qemu_ld, data, addr, oi); + tcg_wasm_out_qemu_ld(s, data, addr, oi); +} + +static const TCGOutOpQemuLdSt outop_qemu_ld =3D { + .base.static_constraint =3D C_O1_I1(r, r), + .out =3D tgen_qemu_ld, +}; + +static void tgen_qemu_st(TCGContext *s, TCGType type, TCGReg data, + TCGReg addr, MemOpIdx oi) +{ + tcg_out_op_rrm(s, INDEX_op_qemu_st, data, addr, oi); + tcg_wasm_out_qemu_st(s, data, addr, oi); +} + +static const TCGOutOpQemuLdSt outop_qemu_st =3D { + .base.static_constraint =3D C_O0_I2(r, r), + .out =3D tgen_qemu_st, +}; + +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(); +} + static void tcg_out_tb_start(TCGContext *s) { init_sub_buf(); --=20 2.43.0 From nobody Tue Sep 16 03:04:09 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=1756727815; cv=none; d=zohomail.com; s=zohoarc; b=BV1ni/5HxxRg5bF7C6cqi0/haruxIs+MuB3XMdIU/vZhBQ27UwSjjFP+3Gxbh9hZnu3B0RGQ1GyHDM8Ha/n6PhdLM2HlFjUhAXC9cmbgVVOVlubjD99XtzsB808Ki8U95Skl2yiYjtH7ELVeoiTCd8x3iODwNH+UJnT3Qk+9QGg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727815; 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=akIEERgXs7+yZRZkR9zbLlIdfnSFTSvmgC+mcTcKDAw=; b=aGw7gtVnIeijD6bIAoYJ9vJoWLs+ysw45hbA+R2F3w6hFe4Ro+YjvjaKybdd0wO3ri23vyQkWbPlWKyw2TU9NIMir8AQF1sfkXStp04NCNTR+4m3UsqBxHFiw+hPnEp5HqRdOvzeU6oKcl+yZFc+nAQ7qd66unWe0EPq52sRLRU= 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 1756727815432535.8841442764384; Mon, 1 Sep 2025 04:56:55 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut31x-0004Jq-Nn; Mon, 01 Sep 2025 07:49: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 1ut30y-00023p-Hz; Mon, 01 Sep 2025 07:48:02 -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 1ut30l-0001jF-5Q; Mon, 01 Sep 2025 07:47:57 -0400 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-2445805aa2eso43018895ad.1; Mon, 01 Sep 2025 04:47:41 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.47.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:47:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727260; x=1757332060; 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=akIEERgXs7+yZRZkR9zbLlIdfnSFTSvmgC+mcTcKDAw=; b=WuziJcNguo2eyLJhhzdkZHu0lXdb4H6OBiryDAXTgrYvr5ahpeh0EgY9PZTGfU+6QU H8R8FhaKitGpbaocnmYPwVl2K+0w000rw+oXrorTDLv6/ZNhgPsDxLq/4maVdBqMYuLE Y/nwR9LSsfbFijRzPxBL5od+t3q5Uwmh88I+5q4RpmyepUh7Il1wauH1IjfnnJRy2SA+ iDMf9rJ4iow5T4qvmmhI3h/FcE6AMdb3ZRoEpZuuCOo5EqvU7d9WoMQeljpqKqGoHZxw 1EjX2Z/m9tQ6uh+X+chw2DPpQS6gWiFfV0+c9cB8yF/yGhkeKhF5cpowKB4o5V2C8M3B tFDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727260; x=1757332060; 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=akIEERgXs7+yZRZkR9zbLlIdfnSFTSvmgC+mcTcKDAw=; b=h0xC//ekLpS9WKUigfvFp4quxFswlqs67oOzvRXgI7VhVhiS1oV3Ft0r8ZbbeA2JEU UN6tXFI0Ag3DQZM6VTLawPq90SPLcT0lplwiso7xMu8UySNP4EiJKJ5ewdAIRudUpCDS Z6VE41wwbgzHqXqvyoQ7cYF/5mpMsO5m2Qv6KKhwtF9KR1iosHzp4XlkKd8i1x7q1kla Mqqq/vmVqv06r2AfCgB7JDJR6FeQgYu9cnkgLr6KVTQ3PLkhKXvId8oZrbXKYjZJ33NN 3OQlsKE3sie2x16QycaFbyC6Di6hYGhLllx403QsxNLQ/aRCjNQTYt0eIx9k9A75xvX6 8tyg== X-Forwarded-Encrypted: i=1; AJvYcCU4oN3OYVUQChHXbviClvG+39XrJnVnzeKAcfXofTaxEvMB3EHnLiEMeuLZeAvjEqsWjPixwrLCmg==@nongnu.org, AJvYcCX51YMnr2dhTbJJgWzQjf2mTiEJsehBFa7Acya+bjKV6nEFvNvsEluMVSlI2YOpBAy8YH4rt3imQudFvg==@nongnu.org X-Gm-Message-State: AOJu0YyStQhLblZNldPKZFoEg1kdpoQKSEzXi3281H9xYCXrj72iaX18 3bRjgQFJbPPRlAgNWeGPHXOMXAgYoxVzsHz9TTaLo6UxFCjAFunqBQHC6VpFwg== X-Gm-Gg: ASbGnct3Q0MX6FolOn0jNEmbR0MR8ozloqqisR4Kq8OuVk9TzGTcjlwzyXDHbqzFhsG IjXENtjasRS+wPOl+YsA7iOwzxllQdh76s+KOhARdHWIa+VtpM0R85niAng0facV0bEskuCi1gh 0iqgrpn3YkPAeUdcSeZDMKXvj0VyK+dck1ICpbJTGobg8fAJ3zbFkn3kGbxjL6tKZdr9awtc/Or lxSqFpO9ylq9Mh7kQDG28FKAXRbBTUWhV5QTUVZRMEJaTv0RcBFUgURt+2AN0iaFEPPeau2m/NX nveJiaGTy7eE5v688txLxBXY3EDZcQDPSduBQYwRFldYdiU7VYL0/CY9cqfsebYOAc8gJAo/UQg UkWUQIFCKutKtZYXm+vH/S3ekE4nkwRoQRMIY2sp0u9M= X-Google-Smtp-Source: AGHT+IGoZH78ykrPnBnJb7ak2DPU5Ku0CRJtbTb5vWJ5yLQGCn6ko/d1/XgsT9gurpBWIb+vEU6nVA== X-Received: by 2002:a17:902:f78f:b0:249:1156:31f9 with SMTP id d9443c01a7336-249448e52c0mr95198285ad.21.1756727259789; Mon, 01 Sep 2025 04:47:39 -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 v3 24/35] tcg/wasm: Add mb instruction Date: Mon, 1 Sep 2025 20:44:26 +0900 Message-ID: <3db8ec74078663cbaabcc680e53f1d588f5f5970.1756724464.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, T_SPF_TEMPERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1756727817017124100 Content-Type: text/plain; charset="utf-8" This commit generates the mb operation. In Wasm, it uses the atomic.fence instruction as the fence operator [1]. TCI instruction is also generated in the same way as the original TCI backend using smp_mb(). [1] https://webassembly.github.io/threads/core/syntax/instructions.html#ato= mic-memory-instructions Signed-off-by: Kohei Tokunaga --- tcg/wasm.c | 4 ++++ tcg/wasm/tcg-target.c.inc | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/tcg/wasm.c b/tcg/wasm.c index 793c1807c2..1cc2e45e77 100644 --- a/tcg/wasm.c +++ b/tcg/wasm.c @@ -566,6 +566,10 @@ static uintptr_t tcg_qemu_tb_exec_tci(CPUArchState *en= v, const void *v_tb_ptr) taddr =3D regs[r1]; tci_qemu_st(env, taddr, regs[r0], oi, tb_ptr); break; + case INDEX_op_mb: + /* Ensure ordering for all kinds */ + smp_mb(); + break; default: g_assert_not_reached(); } diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index e1ee2f6485..1d639561db 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -1116,6 +1116,11 @@ static void tcg_out_op_r(TCGContext *s, TCGOpcode op= , TCGReg r0) tcg_out32(s, insn); } =20 +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_tci insn =3D 0; @@ -1931,6 +1936,20 @@ static bool tcg_out_qemu_st_slow_path(TCGContext *s,= TCGLabelQemuLdst *l) g_assert_not_reached(); } =20 +static void tcg_out_mb(TCGContext *s, unsigned a0) +{ + tcg_out_op_v(s, INDEX_op_mb); + + /* + * Wasm's threading proposal provides atomic.fence instruction as the = fence + * operator. + * https://webassembly.github.io/threads/core/syntax/instructions.html= #atomic-memory-instructions + */ + tcg_wasm_out8(s, 0xfe); + tcg_wasm_out8(s, 0x03); + tcg_wasm_out8(s, 0x00); +} + static void tcg_out_tb_start(TCGContext *s) { init_sub_buf(); --=20 2.43.0 From nobody Tue Sep 16 03:04:09 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=1756727608; cv=none; d=zohomail.com; s=zohoarc; b=DBRz1C/crlaUtNHA8iyjxosAdQCCiA+/J/W87xPAuaMwHaU6OzrrHPd+HUZ+Id05lY+Zw5xrpeT6suSql6f+WyT2jD9EwMTRsl6Ct0D7W7f5lpSLLbQ7iQSzmhfBk2kdi/lSLWIIjByJdODHNIJHajh60hIJAMSSR/viL00EBUA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727608; 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=nIiv1lrfYrU9eW05Aj0a0MU6VidnMXh6ukt52O2SkXk=; b=eBNZHJejmLfRv3zecLYEMIwZuRiB6bn+zO1+9Fa81K+H6wO7IOeo8yMU6GMYD1c1DDzNbz6x9mzymZ67IpzH6rj/keFjAUpbndxsMiYiWsIeOxrPMMeSAsthiqO4HO9K6yS7PCg3njwPn3vLvOTjZ2sMlxCAplHDJxNBd5YDALA= 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 1756727608405724.5190247281361; Mon, 1 Sep 2025 04:53:28 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut31k-0003ln-2t; Mon, 01 Sep 2025 07:48: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 1ut318-0002OJ-MJ; Mon, 01 Sep 2025 07:48:10 -0400 Received: from mail-pg1-x530.google.com ([2607:f8b0:4864:20::530]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ut30u-0001kH-KU; Mon, 01 Sep 2025 07:48:09 -0400 Received: by mail-pg1-x530.google.com with SMTP id 41be03b00d2f7-b4c3d8bd21eso2681943a12.2; Mon, 01 Sep 2025 04:47:49 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.47.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:47:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727265; x=1757332065; 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=nIiv1lrfYrU9eW05Aj0a0MU6VidnMXh6ukt52O2SkXk=; b=Mlac3vZAREPO4r4kK1wgy9RzZFyz+T0hbPnlGYlLDPwiNSkIXSyOcDAcqxr3LyfrFU wbpPGK0wPGNSuF90rj3Gn0NmIWmw/Fz7mKgWLNJmZCBL3DvFNp8HAK3ntrlAKGu11kjC sT87OP13Xg8P2gPlmjt3/qE5AM3eUIWnKQF5MWHJod9zP4qnu3vuNKrW8PXxIkj9fD2O oT1CuZzlRPA+fyTOQirv162qyIGtbnzCAxPxoD51rugSC3vAAx+FqXNn1BnFwMwCk7sa l1GsTlWoeLXcKWm4X0xM+IFG/d3Di0l/9QuB4hjfh1c8RekFYKPzvrZkHIK0qX/Jyu18 Wjvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727265; x=1757332065; 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=nIiv1lrfYrU9eW05Aj0a0MU6VidnMXh6ukt52O2SkXk=; b=tEY4KXy8Tzh8nw5vrezZjPcEE8nk8CWDRHBxIU6gMwl7XrM7LHSv+hQfvFfYymH+BQ kGVEGjWGrfk1Ek3rz+fXkE5ffP4y08T4nKBzhMCxEruVV4xDUESjkDoJ3t9FRBUGExuL pNm23OZMEEvgXotTwuzPzwN4I8KkUpoUMX5KeMgPJPMs0YC7K8gTEhb6UkXB7q7QdWVq Nypf6LLzBr3N4s38DvNYxHTznL5m3VtAREljDMCYDv/A69Zr+0NroRL73W3n0SwC1qBK ul1Rw8VC475IRi47p1KEcolevnC/6siRph9SqXY5OuwXUlfBlz54O7L95WIXNPv97W5x rgWA== X-Forwarded-Encrypted: i=1; AJvYcCVg5jbNFaCW+xGoiTkG+1hNPMNSfho+6Lxbj4PTTmEU/DUU2k00ljnEDgsisQaAJJnXygDOkv10foe4Eg==@nongnu.org, AJvYcCWXkWN07pPg/VjQzdSUrof30vmV//amRs4d+oNvRXavYB7VEOJZJL3Z8tAs6LsGqJpFQGjDtteQcA==@nongnu.org X-Gm-Message-State: AOJu0YwntnHLxg3/pyL6L3wIEL8t3PrerBXIGvjgVBnca98u55gU96UR oXtUvuOcgmwX0WVfkRnfJTOOmvqgHMwNz7v9HiiEuvmL+ikTggTwNEwr4SWw2w== X-Gm-Gg: ASbGncso0+bFTP1dzV6tr8m0TnvT796dBj4l9GgKcw/gVTB2hVEH88+dHZhxTAJhEsm Z5I4I/V3HyXsaLCyErSTymDiUC9lyRPmFx48ElMufljiLBOBbnv6ZEhpHa41b/VGgPLxVW1dATO xgS3dZp7oRsNPmsCjWrbsIpchlDXE2weh9IgDSgXakWRjoMHy33ilE4xXpszgQgjWGyGuuSKFrY X38yDJ4A3n3L9tMhpuKwn58m2kUyAFuyuuo1jSi6o2Ie2uNReds2NsHu+3VanZku085BCd2P8eM RjhNKk1Hh6CHZpT8vf5om9uEjX3hvm11VpUyUYMWnpAYYlwfjpi3ZGpJ0+lLYdJiIsnxTmvJjCN o5siM7M4m3uej3knSlwwhd3IWEsfw9Q87 X-Google-Smtp-Source: AGHT+IE+pw6GZWT18YVnSv97ZiC9pFBPWrzddhYO7EH11sfg4q6JnKNe7CRX0SskKmk7jQ2/ICnxcw== X-Received: by 2002:a17:902:eccf:b0:24a:8df4:a55c with SMTP id d9443c01a7336-24a8df4aae4mr100462065ad.48.1756727265555; Mon, 01 Sep 2025 04:47:45 -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 v3 25/35] tcg/wasm: Mark unimplemented instructions Date: Mon, 1 Sep 2025 20:44:27 +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::530; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pg1-x530.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, T_SPF_TEMPERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1756727610122116600 Content-Type: text/plain; charset="utf-8" This commit adds the C_NotImplemented constraint and provides stubs for the functions that aren't implemented in the Wasm backend. Signed-off-by: Kohei Tokunaga --- tcg/wasm/tcg-target.c.inc | 119 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index 1d639561db..598db7b4bd 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -1950,6 +1950,125 @@ static void tcg_out_mb(TCGContext *s, unsigned a0) tcg_wasm_out8(s, 0x00); } =20 +static const TCGOutOpDeposit outop_deposit =3D { + .base.static_constraint =3D C_NotImplemented, +}; + +static const TCGOutOpExtract outop_extract =3D { + .base.static_constraint =3D C_NotImplemented, +}; + +static const TCGOutOpExtract2 outop_extract2 =3D { + .base.static_constraint =3D C_NotImplemented, +}; + +static const TCGOutOpBinary outop_addco =3D { + .base.static_constraint =3D C_NotImplemented, +}; + +static const TCGOutOpAddSubCarry outop_addci =3D { + .base.static_constraint =3D C_NotImplemented, +}; + +static const TCGOutOpBinary outop_addcio =3D { + .base.static_constraint =3D C_NotImplemented, +}; + +static const TCGOutOpAddSubCarry outop_subbo =3D { + .base.static_constraint =3D C_NotImplemented, +}; + +static const TCGOutOpAddSubCarry outop_subbi =3D { + .base.static_constraint =3D C_NotImplemented, +}; + +static const TCGOutOpAddSubCarry outop_subbio =3D { + .base.static_constraint =3D C_NotImplemented, +}; + +static const TCGOutOpUnary outop_not =3D { + .base.static_constraint =3D C_NotImplemented, +}; + +static const TCGOutOpBinary outop_andc =3D { + .base.static_constraint =3D C_NotImplemented, +}; + +static const TCGOutOpBinary outop_eqv =3D { + .base.static_constraint =3D C_NotImplemented, +}; + +static const TCGOutOpBinary outop_nand =3D { + .base.static_constraint =3D C_NotImplemented, +}; + +static const TCGOutOpBinary outop_nor =3D { + .base.static_constraint =3D C_NotImplemented, +}; + +static const TCGOutOpBinary outop_orc =3D { + .base.static_constraint =3D C_NotImplemented, +}; + +static const TCGOutOpDivRem outop_divs2 =3D { + .base.static_constraint =3D C_NotImplemented, +}; + +static const TCGOutOpDivRem outop_divu2 =3D { + .base.static_constraint =3D C_NotImplemented, +}; + +static const TCGOutOpMul2 outop_muls2 =3D { + .base.static_constraint =3D C_NotImplemented, +}; + +static const TCGOutOpBinary outop_mulsh =3D { + .base.static_constraint =3D C_NotImplemented, +}; + +static const TCGOutOpMul2 outop_mulu2 =3D { + .base.static_constraint =3D C_NotImplemented, +}; + +static const TCGOutOpBinary outop_muluh =3D { + .base.static_constraint =3D C_NotImplemented, +}; + +static const TCGOutOpBswap outop_bswap16 =3D { + .base.static_constraint =3D C_NotImplemented, +}; + +static const TCGOutOpBswap outop_bswap32 =3D { + .base.static_constraint =3D C_NotImplemented, +}; + +static const TCGOutOpUnary outop_bswap64 =3D { + .base.static_constraint =3D C_NotImplemented, +}; + +static const TCGOutOpQemuLdSt2 outop_qemu_ld2 =3D { + .base.static_constraint =3D C_NotImplemented, +}; + +static const TCGOutOpQemuLdSt2 outop_qemu_st2 =3D { + .base.static_constraint =3D C_NotImplemented, +}; + +static bool tcg_out_xchg(TCGContext *s, TCGType type, TCGReg r1, TCGReg r2) +{ + return false; +} + +static void tcg_out_set_borrow(TCGContext *s) +{ + g_assert_not_reached(); +} + +static void tcg_out_set_carry(TCGContext *s) +{ + g_assert_not_reached(); +} + static void tcg_out_tb_start(TCGContext *s) { init_sub_buf(); --=20 2.43.0 From nobody Tue Sep 16 03:04:09 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=1756727770; cv=none; d=zohomail.com; s=zohoarc; b=c9iLZ1zgyvt4grGYPSABKmOMkawM/aD5h1dEFpjvvNU2LtU9jC6JJ13W8G4KxXEY1rgFoaxzMU8Fl+gDE8N9lRcIXUS1HWxt1JkCHnt2knFrahFKVSfnVTyvFLpigEDOlyUXlThJyJEaWLD2QfYJLMEt0kWA42OjPx4oRqFYFKI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727770; 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=YcsuOuyWwjyfdwSMhCo/Q1vq9Xw5uCZDKRhMY5glHGY=; b=QRz6WDGELnoJNhM+Zp1GeYghxUKv27EKgE7jcjL3HwrbHX2A6ZpeWpFrdbhxblpNSJ4ot1YfRUNVe910+itTsaopiGON4WYL6hXQ2ulU92mciMnCeWIZ0o8i598XQBD1+hn7j+SNGW4r7wkVvSwe4oWV6DsWrDIGMrvJxcopKG0= 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 1756727770462747.890536313896; Mon, 1 Sep 2025 04:56:10 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut33e-0006Po-ML; Mon, 01 Sep 2025 07:50: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 1ut31E-0002zw-8K; Mon, 01 Sep 2025 07:48:16 -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 1ut30z-0001lW-CH; Mon, 01 Sep 2025 07:48:11 -0400 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-249036375a1so28531635ad.2; Mon, 01 Sep 2025 04:47:52 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.47.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:47:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727271; x=1757332071; 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=YcsuOuyWwjyfdwSMhCo/Q1vq9Xw5uCZDKRhMY5glHGY=; b=DDi9sIfalZlUKL+uZv5ryvaDk8S2uIUbNBxErpmkvIVEo25kfR9DXLOrUf7PXwWw74 Hz61HNyrUgaYHs0SAor/mPIdR1B52xvnw0oAv6KNTmi61zL7ryYbTrUc7CZsIGNZXczs lJzhmLhpS0S2ivhbHf7H0j4cAePvZn+QOPFcSt1oeFb9DWFtDziNJw0GX8Mk5oZU6K5w qISn64Q5ewDYBfmVmHFUIEITkNvoqX2BprhvluVZL6Pa0j3vpbBM+BAZpmz6e/cFiLag VM3qd3YLM7DYBePSNiBqHiQS+K9kYAgZrgnqXEpqudslHU+EpT4drhx0AMjoMwEn3RSv Mebw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727271; x=1757332071; 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=YcsuOuyWwjyfdwSMhCo/Q1vq9Xw5uCZDKRhMY5glHGY=; b=s7BWwXz4AAJa4FHMBKsbKWQ3Lsx2fVYiZK38DXmfOuRLCZ7kBkyROm6rNgsOilrUYJ iT8DK5AvlvHR8FVmBGMstr2P5Kk3z0fjGb2Po/AN7L5SDgSZQseg6yILoARWaAP2zskb CLs5KH/e3lw+xq/OpcqSOCkF3ejRRnE4dQqJtLA3AuAyxIgLE3/PgFNyptZfEu8VApYQ KFDLKGeeM2tx8dRdwBv0lOhSQfeHeqI5o0FrsKKOzMWA7x4tPcSYRXCf2iOCZkPtXexg xCk5afckQic086GKd7/Ko2zUgGeZiElW+UJor1Js0MowIB+eGntPTBH/Xs3elZuw56qc K/5Q== X-Forwarded-Encrypted: i=1; AJvYcCX3QzalOD5vUKS37aL2cgI9p7Y0l2MquzuMoZrPR2IbVZ9zv5udu2+xEI40wRDmbZ2QhsrpcQUJq5tyLw==@nongnu.org, AJvYcCXBbSq8kXC2JkIzjhzo+NIWYkQatE7v/FISvqcy00hu50AhwvlkKaVzxgg7AYJ+kl1iXhBYjYiPBg==@nongnu.org X-Gm-Message-State: AOJu0YzjRFlzFDU/Zoy313IHmo/6s5WTegDAktj1lDWOEiPV1Mqx4Fq+ IEgqk7P/H7hYnzBoOZoP0WJqOyuvxb5s/fRQgC/CIezUrNe45tNT/fGwwk3zIA== X-Gm-Gg: ASbGncvfFThFW3ndrCzp8qiZwgJwA0JLbQWaGZPAcuapdMXrFjxJIabLaDfQsphr2CJ FnietEvv2ZmPh04iS5kWjF97pL9TUCakwJOWxsvcQqlP1pTVxJbB/Ka3Y37Bs8Cpv6KEcAEn5aB BLdxdFO5rpbPS99ozp7SsC5DjwGQM8lIYSnuR+0xolsSp0Z2n+6yEILMfheGP/Nt0HLRK8Jb3Tg oTsZkRhYb1AXYj5llGn2SIKWqts9WTtC01G15PxS1qKkDQ+gwbCD1B0IX9Z2PshGVtd1/VZ/RXB YF119twHAVIp7xHa2elVYfwSrDykyE9Ta3fOYMowj2u3xNKAQm/jRQa6BAj5TkA2R63Pl3m3DBF JS2RRe/8FpmvRudpiKhqwVCsT2QcbN1uw X-Google-Smtp-Source: AGHT+IF4k9JW28yu2OtLJ1XqaF1j/293B4OszVExYt/QthFRfpgB1k2PmXcco84Bp6e1958f9Pusig== X-Received: by 2002:a17:903:1585:b0:24a:8d33:96e3 with SMTP id d9443c01a7336-24a8d339832mr92663785ad.19.1756727271304; Mon, 01 Sep 2025 04:47: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 v3 26/35] tcg/wasm: Add initialization of fundamental registers Date: Mon, 1 Sep 2025 20:44:28 +0900 Message-ID: <6cad2aa3c86c1b67dc2742a7749bef1e752d1ac9.1756724464.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: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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: 1756727771922116600 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 a3631b34a8..f2b67c1099 100644 --- a/tcg/wasm.h +++ b/tcg/wasm.h @@ -20,6 +20,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 598db7b4bd..ba7a5efb5a 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -2071,11 +2071,30 @@ static void tcg_out_set_carry(TCGContext *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 Tue Sep 16 03:04:09 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=1756727831; cv=none; d=zohomail.com; s=zohoarc; b=F/+WVarwfqttPrL5hAQCiTQvjt/cbHdMIeqfuSDzB+OaUkfubq0eQzLZhyWkNwAWl95RhoiOfdKdNxJI8vbO9dEfo9by6z1q6uk2CxKp6x/2Lv9drQWnIBOHWvOEk7uvhndZOa5oRQi58LHOW7RjwZExpVYBZbLjWvtP7XJn03o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727831; 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=ryQZSF1JCBNKPpxur/cZne/RKVaRs4w2tj5AEzhxIQI=; b=GQk3YZakvHi9cTMHtpiIErVxCArm9bgInp6/WqZ4byXoW53wwr98QlP8VphT1Y5osOrOHlrpIg9qlyOi/B3ZWL6po7y/UjIW6iwrOTZLGEazR/faji5g34Ijg3PVU+Ep6+nvC21am1MLPUSLPOUbIssGVTV1TCW1dGxsAdz036w= 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 1756727831337319.6908610097199; Mon, 1 Sep 2025 04:57:11 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut32s-0005ML-32; Mon, 01 Sep 2025 07:49: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 1ut31D-0002zh-Tn; Mon, 01 Sep 2025 07:48:17 -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 1ut313-0001mV-PA; Mon, 01 Sep 2025 07:48:15 -0400 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-24458272c00so45481225ad.3; Mon, 01 Sep 2025 04:47:58 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.47.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:47:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727277; x=1757332077; 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=ryQZSF1JCBNKPpxur/cZne/RKVaRs4w2tj5AEzhxIQI=; b=PtJtM5Z+ZeTuEhYcnfujs0GIiS3YJFfY/bhPP+XHmyIvU3EKOZS58Bu0qACh1xUnZv 4sMDmVdvWTNT/f0AfiOjA5TX0dO6/KMiwmsQ7Le3D9Uz8bXPFZF8rp1e94BgZuTlWNf5 RpRaJpTKhDZyP0fS5czUWnsFNzWP0knzsz/sR+cPaFj09pikg7rZMlFrC9LtW9oDnU8W WlZ2UvlkmAlBEILW/1v+9dM7+5N1oo9NqrP/6/4PHHmPGXTMUEOK+/iBkWfxthQQKB5H WF9Q4/RqOqliBmRQ2R9obT5u3jU+qConrr2N6AsjSM82NS4gpj8kY4Axpms2d8LGIEGR LlDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727277; x=1757332077; 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=ryQZSF1JCBNKPpxur/cZne/RKVaRs4w2tj5AEzhxIQI=; b=WKAsFg2Mhx4H37ZaBJPQuUL/YQNxjV4G7g5i5jn45c71diS8WTOdgH87k7FjvM868c eulG+zD1bOd88MuffO3XXtRd1uh4byJVB1BgtkKWoj2LTPru30TkAYr82J+9C8rOzG9n /q7IK0h1JOwUvMKHs/6a7p6D9T/LoULhYblkrqxBG+BCgWmEbqjxy4ZOJKDAkPpOrYAq Q2yv54Bt/17kDstCeQluCS+yubwY0St0L9FtecvtBuD9PprFmXa793QkSTYl+ks1GTCH AZYyTzN8OIZwxRSUI4wOyFryESIqg1n7X/4X49nllf8CF/+4akDHNgpJOPC6PamA3crW IHMQ== X-Forwarded-Encrypted: i=1; AJvYcCVN1ACucw3A4ahkTujNCBISUanhFS3kF2zlzdxx08l72hH87dCAOyhY7+xrusqCFs1v0dUAq3e5ZA==@nongnu.org, AJvYcCWCxQEXuV/Z2DNQkhB2yV7/jlf9tmezcbA6Xs4xEURZ7GJq0LlO4hr6p7PeZ1flI4cei9Y1mQNK9t6YfQ==@nongnu.org X-Gm-Message-State: AOJu0YxjytAQUeJbc99kBayZRoEIoqljgjhKsxOK8RNmciBIpLYIt+zy Cg3ff+JTsEJeVo8HH/uuxn1lAZspP7W3y4iLBcFCO5seE6tRfb9foWrjIWjABg== X-Gm-Gg: ASbGncuWs3vN+qP8Xnabq0iVixi4AdGIwiJPif7zuHrSys/M/S5zSaUUwu+QpleHmfb /ZhoVIW+OX2wbBjdu4+J/wzMFYC2xEuzngVoZCdOc8cjix0MqfsnxoxjgBAHQkpramvhLwXORrn 9Tvz8wq13k9lYVAQSe/MW/9MT/H1sbw5rWks4PnmN3hbudqxgvUhzjL58dbp2K31v0JXyZzDiuY PB+6tf3QE1LkoM4hMYauzS4dKCAV3mvsySiB9B18Emub8Redwo3tPflBKNpAuv6oHc0F/DhmsiZ Ylc3tyajPj2jGsYSBUDgwB+WKz65ZTLD3jjEBISCJ7jY7M3uEqdiVCQOAU8sB1bfwlJT0SlSphj fj1WFQlvJTwtENbnU8rCkNg== X-Google-Smtp-Source: AGHT+IHuycS5QF/2CzpFCJqCLTuxpVAypF2RLyI+4td/jY0a+YOcLnrHqjllIx9FPrg+bSgWjt4E1g== X-Received: by 2002:a17:902:f544:b0:249:10cc:8424 with SMTP id d9443c01a7336-24944a9a7ddmr110193705ad.39.1756727277061; Mon, 01 Sep 2025 04:47: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 v3 27/35] tcg/wasm: Write wasm binary to TB Date: Mon, 1 Sep 2025 20:44:29 +0900 Message-ID: <4d80cd33e4a7d49d4d1e778d58fbedd8b6528bdc.1756724464.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::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, 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: 1756727833395124100 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 f2b67c1099..b5d9ce75da 100644 --- a/tcg/wasm.h +++ b/tcg/wasm.h @@ -32,4 +32,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 ba7a5efb5a..7663f03eaf 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -154,6 +154,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, @@ -312,6 +314,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. @@ -928,6 +943,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; @@ -944,15 +1052,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; @@ -974,6 +1123,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)); @@ -985,6 +1135,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); @@ -1018,6 +1201,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 */ @@ -1058,6 +1242,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 */ @@ -2069,14 +2254,164 @@ static void tcg_out_set_carry(TCGContext *s) g_assert_not_reached(); } =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 */ + 0x0, /* local variables (none) */ +}; + +#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) { intptr_t ofs; + struct WasmTBHeader *h; =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)); @@ -2103,10 +2438,81 @@ 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 2.43.0 From nobody Tue Sep 16 03:04:09 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=1756727608; cv=none; d=zohomail.com; s=zohoarc; b=DyaECO2p8qSAR/C5H8dXyN0ycpf0m4+ANpUGGE+fFbKbS+is9C9DTixSy5NjO22t8G+n6ocsafvO9NBPNks84rF8kOZPJOWUIFyQPiqKkSo4Lx4i+lE6PBUUUqMobgV6XCTx/kb/ZVrnRISOWcQdqIecQy+AtK1Rf5FSy9dOCYo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727608; 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=NkF/SrWBRt7mpVRvpwQdPdICO84Pyhp/wixwRcKtvEU=; b=KljvASGX9uoZ8J/LIZjOB0mkb962vQsaCeVkb6E1Y6sPYyXaTeKyaakL/1avAtjjSPIGRn211kawh3rJI/5qw68ax3nLGIZduB3ijg0ke6I7fxOa8C2bxtXayGYFXyUKPa4wIlUcF8pqmCYGB02HdlP0qevs9EQVFLOe0BReXhQ= 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 1756727608383128.08790583980465; Mon, 1 Sep 2025 04:53:28 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut333-0005SU-Cz; Mon, 01 Sep 2025 07:50:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ut31R-0003Xj-NG; Mon, 01 Sep 2025 07:48:32 -0400 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ut318-0001nq-9M; Mon, 01 Sep 2025 07:48:26 -0400 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-248c7f955a2so37342945ad.2; Mon, 01 Sep 2025 04:48:04 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.47.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:48:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727283; x=1757332083; 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=NkF/SrWBRt7mpVRvpwQdPdICO84Pyhp/wixwRcKtvEU=; b=k8yGjVed8FRLRj4fqLIK9xLS9An/GsNSP3SXXofpSmk5qPF/9gI5XMn/9us2q+3iSh hEehByRmXmXXftwmb3S0+dTiZGhfq+r5yjybGMyOqKy7NxK+RSEBozuyrKI4JQf3EUs6 1PxWY+VeXnFGPRhIEe9v4XnBuzDChFl2anexxD/gL9rBTsY0OiluDhEUCKhvoBBQ3NQ+ sXYFXhs5NPMlYDMzYSmC/FA3nOEAWQG8Auoqe1t0sKb+R4A/a/RM0OXG2Rk1WLA5hCZf NUqFvPFtvNiq5m0+9Z+J1Ka5An3VhyX5WdvsojWFNcFkoP2wGC9PkgJJ7WRef0iWKlxL QxPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727283; x=1757332083; 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=NkF/SrWBRt7mpVRvpwQdPdICO84Pyhp/wixwRcKtvEU=; b=H9G/t3rnM4jtAmUkwyqdUs8SPQdaEKsgpjtb4foU9fHuEdW8FPSZFXF7MxFzgn0jEs KTYIFHOeF7BURfg5C35pN3m2qsyyySY29IKDVRcHnH5q+yoJR4PfmaeVu4OsomnMj0Ki 1MwABDwMJJd7R6bDL1quV165YOtA7AFwoUo7hcrAGvh2bsMxMQVOUF9DgCX0QSMIcDwk EVSxQ49VCsvkvMmQEZtJ2/JuQd1AFSH4/VBfk2kntQ7Uhl415TyJdnPygEeSC8RzvswN wfZUXPdCaJzVIObGRfTotOj0I5M7TkPWXLKs+3EeK4aX4gX8AvlD5v5hO6CjigHm412f RXgw== X-Forwarded-Encrypted: i=1; AJvYcCVttcoZ0YKculnCcx/ZphYTBOSnyusa813kCcWHdlZtujLI9RPBYGS/nq/50IjbaKdHbCFplWnwfA==@nongnu.org, AJvYcCXCQXiYx9EzQL1WdMmBDTjea+iW+kfJAS+Kpm+Tt0emzuPGDsJ6DViSuso1vdlOe1oN/hwN0sw/M26JwA==@nongnu.org X-Gm-Message-State: AOJu0YzE5CMq9Cq8JXI1s9pIq8wrZ8+sy6aQ3y/bI6lKIWQtloxDIBAo pNePuSkpkaqPLKAyzNdvr71Nz1X7LZmIA6BkLCeVWwA1HStL9C9oVqiCgD9YAQ== X-Gm-Gg: ASbGnctTyXsl2GY7T2zpm8TXqLf5N1pFR3WdvhfxfVDqau4xDoSWL/fh6a89naQNDPh TCNBgf22zW725uedFXUPeKpnso/YkJBxNSwkXjF4W2lIPriLuyQVSViMAIRwlFLxa3qB7FKXPhK NYN0F6jT9Ecs883aTZx9ZlArw36XT/eb18hgHHwU3EGjWaHkVZl62mFkwECBTrlM5+kfheP3c3Y tYvNtREb3o0HChDgh6UikrXS3pYP6wfbQNDeVfiQ1cegz1r2QmHwQyrUInDkIRwyU+opq1Y+VH3 roGJu1tiKOBolSnzW15HR6a7cpQKfIYqRGUYK3XmnBI+lKMJ0wgr767l1Z2iXAf5AePAc4/GaLn idInppHOewoIXsam4kzFJzQ== X-Google-Smtp-Source: AGHT+IGUn25ZeeJI9WLAckX6lUgqQ7vUWG/PMX7jQQzeUj//EkzeoPWDh8SsJSFxSR46d+uLY0ukJA== X-Received: by 2002:a17:902:f641:b0:24a:9475:3db2 with SMTP id d9443c01a7336-24a94754493mr90911585ad.35.1756727283063; Mon, 01 Sep 2025 04:48: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 v3 28/35] tcg/wasm: Implement instantiation of Wasm binary Date: Mon, 1 Sep 2025 20:44: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::62d; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pl1-x62d.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, T_SPF_HELO_TEMPERROR=0.01, T_SPF_TEMPERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1756727610783124100 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 1cc2e45e77..15db1f9a8a 100644 --- a/tcg/wasm.c +++ b/tcg/wasm.c @@ -23,6 +23,43 @@ #include "tcg/tcg-ldst.h" #include "tcg/helper-info.h" #include +#include + +#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')); +}); =20 __thread uintptr_t tci_tb_ptr; =20 --=20 2.43.0 From nobody Tue Sep 16 03:04:09 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=1756727900; cv=none; d=zohomail.com; s=zohoarc; b=WXS3pr6G2YKn4f88XsSEyQbn7tyhqHeXqvJak8PTCqJN/PgqiUgZZqB+h5po9iVg3i0VbCqyJ6gpnWwG2ndHupF9WGYDP5p6IhRCz94y0jXlvODRaoFqe3ax09eh5HX67T7wKH0NuU5tQW8iG72Te5KHNBeUfg6roEnQxoQbQy8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727900; 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=1mhZoyyImgDjzAPXj+YimSquQazjd0N7mWZJ50wnHeY=; b=GL8k8CiiJV5DCUEdrI3Td6u5BHCr5CeLvFyuQhLmCd1+tMGywm7esLXD9DknL5ZSuPEbyRttA55hEVtGR09XwlL8q8lf/XiPFssEA4RqdfMsRdZ80Rw6YS5Qvdm/vvdf4Vr3dEIXLBzj5cjS3nu7yYdyNbrre3C+4N4SUB/m31c= 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 1756727900492761.5328398680548; Mon, 1 Sep 2025 04:58:20 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut33Y-000601-Fj; Mon, 01 Sep 2025 07:50: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 1ut31Z-0003ee-8R; Mon, 01 Sep 2025 07:48:42 -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 1ut31J-0001pm-Hw; Mon, 01 Sep 2025 07:48:34 -0400 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-248c7f955a2so37343655ad.2; Mon, 01 Sep 2025 04:48:12 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.48.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:48:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727289; x=1757332089; 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=1mhZoyyImgDjzAPXj+YimSquQazjd0N7mWZJ50wnHeY=; b=LG9a+v/9Gx1DM0ZdNVMVKRxxGCC2XE8RoKcZcb7Xt+EybY8kUorRcZ1eIqqXFGK5bH EVk59HuDZBa02wrFqtjX7mk2AOHo/bjwyIpKw529/8hX/86LxgjudQJjbAGYFZuZFcXX D8UAymZoN/aVNBfWueMRTciWp9UnMYDYSW1W5yLMxKM8j37E3e2fV/Lrsoby8lxfsTEm A7danQoWblmpHnKga399qF9JEgS+LIvQFoMKUykflIzcUlpZ+dvjw7vfRJHTwMGr2G5x hr0qVGDdkxDZ3QYZ7Rws6JrgEUamyYCgMnhC2B5KYbfhNAyeM/S4InAlf6PcthU1EHk7 vzpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727289; x=1757332089; 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=1mhZoyyImgDjzAPXj+YimSquQazjd0N7mWZJ50wnHeY=; b=WsbVBIE1e/a26W4Bsp1yBimnPJhoG36rVuccxpNcP4OC7aNfh5makzm9i4eWGPqaYK kBTebskZqnN2sHTHXcv3ri7XsROqF1DDx+6/Ak7g+8ExrkOdrviY7F+HiHCjUH5LVA9k il4pPQpIXaNHUX6Lwi5S+Blb+2Rhz+E4U8rfwLWJsHgbmlaEeuiYh+O5gDMMlpIkwjpH xc+w98cyB/BXvBbLytWuUzGcOYH+j/Smf8n8sXmSHnJGR4Zmdefz39kfvkoqjda0iMs9 GzNcIa0nQkCjn64fFo3DpS3WN5qrIJILukkU34Png9dDigIjPwmaMJSFMJeRWjKz45a7 DXSw== X-Forwarded-Encrypted: i=1; AJvYcCUaNdRHxCLzjLYjLBetk3aJCSBWJ+bp2LAtZaS7NgYlUp9xdtOCe43ctIgb2Ha8yXzsAMHApr5wHcKfhg==@nongnu.org, AJvYcCWRsAsW6mUZiWqBLdca/onDtNii3C0lLWdc5yj9VCMSlIbLXCfdCxEfw8d87bqGMqwBZbpMmE9PWQ==@nongnu.org X-Gm-Message-State: AOJu0YxfszGfLqDnYMqJhRfaGOFsbwtF3F4CRj5hbOWtcDGnWG1mEJr1 ngBoezwr66oVMvMshaEa8KWocV9sELjVtZ+f1Bm7r+4j6eygleGo5T5K9MEZwg== X-Gm-Gg: ASbGncvxBwpQapG+hOhaocb0ayBYcaURpastbiOOuunYeSEWFI/BQfX8KOSUHxwzG/4 +5Q6/FBgy51Vldbjhq0rvnWG31TNt21rouJLALeto5HorCBMCF1s+cVS6pM7eiH3f70fFfdXtm7 fMqAlqinSUzPBIHK98R8Wlo5UKYGOEnfel+8x2Vnxy5DqPFuiJ04+7b4mbWMDPNX7+ngDLydPeQ SopMo8geTEENBb04p107MnnYqKYzLyJBLnW2npsVLs2zEEIDTC77XaumEDrFkHifPN97uAquNwh Tvvz6BNPk+CgW9eWRFLho99VLavWHKibD/B+4pDGrhZ+3Y28Uncne9RCIxvvKkwy1Kci7JYTqVE RpBQT33HPhuZygKR1SWrhGZE2RAix+fth X-Google-Smtp-Source: AGHT+IHNZ8chk7HN4IaK7VGH469nQvszfLdvVQbyw/C82kWfZk/Gbu1regw3qFi2butVz/CuKmpPsg== X-Received: by 2002:a17:902:e749:b0:24a:ad84:b1bd with SMTP id d9443c01a7336-24aad84b320mr60189555ad.32.1756727288914; Mon, 01 Sep 2025 04:48: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 v3 29/35] tcg/wasm: Allow switching coroutine from a helper Date: Mon, 1 Sep 2025 20:44:31 +0900 Message-ID: <18770eb1458b4824b97608563ce6a65dc381e994.1756724464.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, T_SPF_TEMPERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1756727902011116600 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 TB's Wasm 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 | 58 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/tcg/wasm.c b/tcg/wasm.c index 15db1f9a8a..82987e9dff 100644 --- a/tcg/wasm.c +++ b/tcg/wasm.c @@ -44,6 +44,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 b5d9ce75da..fdde908557 100644 --- a/tcg/wasm.h +++ b/tcg/wasm.h @@ -30,11 +30,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 7663f03eaf..6af4d6eb07 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -152,7 +152,8 @@ static const uint8_t tcg_target_reg_index[TCG_TARGET_NB= _REGS] =3D { #define CTX_IDX 0 =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 @@ -169,6 +170,7 @@ 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, @@ -176,6 +178,7 @@ typedef enum { 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, @@ -1116,6 +1119,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) { @@ -1132,7 +1146,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) @@ -1204,6 +1227,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)); @@ -1212,6 +1243,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) @@ -1245,6 +1277,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)); @@ -1261,6 +1301,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 void tcg_out_op_l(TCGContext *s, TCGOpcode op, TCGLabel *l0) @@ -2264,6 +2305,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 @@ -2289,6 +2333,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 @@ -2427,8 +2474,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 Tue Sep 16 03:04:09 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=1756727907; cv=none; d=zohomail.com; s=zohoarc; b=fvIEo7UQFfv/q2u1xurvK1wV/2kkd0qkfLjslhwX+CqPavBZuTW1snnrTouEg0ot+XYLWLCLYOynp6Kv7lV/RCUtayCdRHlNAOkf93z9Z3QO3dyEiKR799TuMMURiMBEj096cyz9TFkw7UHfynaJK50KZ315Vvw6tuzV6Dhw5Zs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727907; 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=FrnKzdEd+MTfG/VxM7WMSinaKjZRPI/Ikt1qARO7ThI=; b=NdfESeHsP2Bb5QfsgJ3Hu+6mPALmMZqj3FIw6LrqhwyTlykkmKt9Qhwkg0E6m8ZEgKCwmko9h3Yb9Ct75XVeXZhrPDKmeAEL8Z9famVfTVVxDuSTQC0TsNLu4hmxVrJ5L0GVYGGYKFyNX2ODFrGOlBvB7jnznnnip0mfKLFhQgk= 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 1756727907778690.8912096636449; Mon, 1 Sep 2025 04:58:27 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut33m-0006kB-P5; Mon, 01 Sep 2025 07:50: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 1ut31V-0003e4-Fv; Mon, 01 Sep 2025 07:48:36 -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 1ut31L-0001ra-CX; Mon, 01 Sep 2025 07:48:31 -0400 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-2445805aa2eso43022645ad.1; Mon, 01 Sep 2025 04:48:16 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.48.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:48:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727295; x=1757332095; 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=FrnKzdEd+MTfG/VxM7WMSinaKjZRPI/Ikt1qARO7ThI=; b=XFnkYwO78Kv/Ygzvx0zJVRCpDsXkqwRqDa6gziqB8gtB3Np2rYCpCQkgSmGDgfRhhT DOc5us2YygBh8darHPQkRSuMrT8BPb5I9FEpoWDr370TE7GZYJdgtB7tvl9YiSfPtzaK H0d0G3NbAZB5zVs4V3ET3KSI0pAxpXVTldlSsoQAN0u82l4Paeo4lvFZphifRibx2YaT MMt90EFfNQPstCbksAhpe+VO/pTX1ov6BYQNIcigLKG66Khj5yqVbORU5uRcIwnFt2nC pd654HbIt/p20lrUry9R2P3P1Q5z6ugBiuShKABa4f4iL1OmBjZ7fl+VI4bQqgR/rHEu +f8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727295; x=1757332095; 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=FrnKzdEd+MTfG/VxM7WMSinaKjZRPI/Ikt1qARO7ThI=; b=bvBlfzrNP1ctRLBZxsHsdrTeV6ykmYnF05HtleAeOkcWPv4iVxTKbHYAAfnpiLb5Zb ymfRbr+9Ym3bSRU5Yka/R3QZGewvdmG0qn9xSkGISJv8ciToGsE0+YquFlZJLw2Ttsdy wwfdAymovJd214G5FfxrXpH7RFER3FOnkCO5Fl50SA2RurGV0uM6PNaQLucZe+Si9Ucj 617AeNxCe5u+zGX8PexCDDTEm3B+sVqOhyqgw06nIJArfNCM3SY9emrZ56vwi7UDakRY 5EZEDylaorhY0ekozfTxn9i0SEr61Iy+RQCPVIGVhu6vo4+5pfPFvOKEheTRySfDUqj5 82hw== X-Forwarded-Encrypted: i=1; AJvYcCUsBnfrTqPGTXtg4tt9SShmuaJQAhGwtaxJF7FTU77JTW5GpIY5nZ8E0iHR1+7HbqJiAFcVmn/dlA==@nongnu.org, AJvYcCV2t8GK9jqSBD8E8J5oG0Mfkg7hYMA7lJ1L44JybeNvDxyiUjE+w6Jf0m2aC+FsIoU+DPpcnqg7szGD+w==@nongnu.org X-Gm-Message-State: AOJu0YxCk/bxlK1VHQEPNIVlje99jly6277T1NTtWqJ+txEdNWcA31dk HJJP1f2i11Dp9m9lnedrjs5lEG/a4yXPCJdbFGOmPE+BaKF/JBERvWf5IC5O1w== X-Gm-Gg: ASbGnct4WICfPfFabzpCz8mkIRQNRJ7MLy1Oiw88pnt1hVTeE6gG1SNTUAf+6sghq2P aA3jDuryYXeEeDKk3zcb8XfMwUzCSQYSiksh+Iyp9YmEzS0dS3+RpqJUVhXnjg+O4d5KQPchous Wi1lJegqspkfhDEWs7gIU3bzE4HXgaDq3Vc0Qd2/eCDEt6CfXqnKXVD+qhZewMJR6KGeAJ4j8rw nyWkmuVb6NAmuCZKKMSRK+rYdgfBWd0WPDdaY2hJggOaOlD19tw0J1nX/vPNtlB/b/HwXpUSW0n KN2Le0alJCAPAigf8+yhRTa+flNq+AxXX6S0micMT7lI1RK93M81ZpWPD2eLj5GAgFkMKfq0yQb vyiWPIQeOsDW4nDi2/rzQtt7bJ3okCdIs X-Google-Smtp-Source: AGHT+IH8eAqlOi/gK2WG49ASJm9/0n1ujHDsC4592zhn0zFAfT6BGAEKWE+3NlatvfsauHPb2SjqMQ== X-Received: by 2002:a17:903:41d0:b0:249:27b9:2a06 with SMTP id d9443c01a7336-2494488a758mr110173015ad.2.1756727294946; Mon, 01 Sep 2025 04:48: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 v3 30/35] tcg/wasm: Enable instantiation of TBs executed many times Date: Mon, 1 Sep 2025 20:44: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::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, 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: 1756727912425124100 Content-Type: text/plain; charset="utf-8" This commit enables the instantiation and execution of TBs in wasm.c. As in TCI, the tcg_qemu_tb_exec function serves as the entrypoint for the TB execution, handling both instantiation and invocation of the Wasm module. Since browsers cause out of memory error if too many Wasm instances are created, this commit restricts instantiation to 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 | 244 +++++++++++++++++++++++++++++++++++++- tcg/wasm.h | 45 +++++++ tcg/wasm/tcg-target.c.inc | 21 ++++ 3 files changed, 307 insertions(+), 3 deletions(-) V3: - Fixed init_wasm_js to correctly cast pointers to Numbers when passing the arguments to the getInt32 and setInt32 methods. - Lowered the the maximum number of instances (MAX_INSTANCES) to avoid the out of memory error in recent versions of FireFox. diff --git a/tcg/wasm.c b/tcg/wasm.c index 82987e9dff..00f4222183 100644 --- a/tcg/wasm.c +++ b/tcg/wasm.c @@ -24,6 +24,10 @@ #include "tcg/helper-info.h" #include #include +#include "wasm.h" + +/* TBs executed more than this value will be compiled to wasm */ +#define INSTANTIATE_NUM 1500 =20 #define EM_JS_PRE(ret, name, args, body...) EM_JS(ret, name, args, body) =20 @@ -61,6 +65,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 @@ -288,9 +294,53 @@ static void tci_qemu_st(CPUArchState *env, uint64_t ta= ddr, uint64_t val, } } =20 -static uintptr_t tcg_qemu_tb_exec_tci(CPUArchState *env, const void *v_tb_= ptr) +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) { - const uint32_t *tb_ptr =3D v_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, +}; + +static uintptr_t tcg_qemu_tb_exec_tci(CPUArchState *env) +{ + 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)]; @@ -583,18 +633,32 @@ static uintptr_t tcg_qemu_tb_exec_tci(CPUArchState *e= nv, const void *v_tb_ptr) break; case INDEX_op_exit_tb: tci_args_l(insn, tb_ptr, &ptr); + ctx.tb_ptr =3D 0; return (uintptr_t)ptr; 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; 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; case INDEX_op_qemu_ld: tci_args_rrm(insn, &r0, &r1, &oi); @@ -615,3 +679,177 @@ static uintptr_t tcg_qemu_tb_exec_tci(CPUArchState *e= nv, const void *v_tb_ptr) } } } + +/* + * 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 12000 + +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(DEC_PTR(instance_done_gc), tru= e); + memory_v.setInt32(DEC_PTR(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 fdde908557..88163c28df 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) return f(ctx); } =20 +/* + * WasmInstanceInfo holds the relationship between TB and Wasm instance. + */ +struct WasmInstanceInfo { + void *tb_ptr; + wasm_tb_func tb_func; +}; + /* * 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. @@ -67,6 +75,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 6af4d6eb07..784df9e630 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -28,6 +28,9 @@ #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 @@ -2295,6 +2298,11 @@ static void tcg_out_set_carry(TCGContext *s) g_assert_not_reached(); } =20 +/* Generate global QEMU prologue and epilogue code. */ +static inline void tcg_target_qemu_prologue(TCGContext *s) +{ +} + static const uint8_t mod_1[] =3D { 0x0, 0x61, 0x73, 0x6d, /* magic */ 0x01, 0x0, 0x0, 0x0, /* version */ @@ -2443,6 +2451,7 @@ static int write_mod_code(TCGContext *s) =20 static void tcg_out_tb_start(TCGContext *s) { + int size; intptr_t ofs; struct WasmTBHeader *h; =20 @@ -2457,6 +2466,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 Tue Sep 16 03:04:09 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=1756728004; cv=none; d=zohomail.com; s=zohoarc; b=dUK2XqXjY8VJRiH0NA3vHW6dL4Jaaf57luOp9OuKi1o23qN+gsMiPKoKf6/K/LUAdQwQpEl7ks63cgelvAcmmiN3Kp9mxuqNhqfPnejUI+q2gq3dlEb5S3lPJu03fUtoQjmixieGjSb99I57RSjYQUTo4QRbynakxw2tHmvmfz0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756728004; 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=QmHEGXX25Xp5I1MM8WX8B83+pgojb2vRh/cS3EC2mCA=; b=N9hBlK0+QsUEAE888L1PaYcnH24e6Dkne/lx3ixa9dRq/s2rerxCehpGTKIFBohn14w3pdmyg8YpXxqn43ApO2/ZOlWRmPO9tKJBGXs3rZx89Q9MKYNYSYnaeDAvumSTq83foOMuIjO/geTS/jXLwIVPyjCQ7fovWiy2lZN4Bn0= 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 1756728004321706.5220368976187; Mon, 1 Sep 2025 05:00:04 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut33n-0006ne-GK; Mon, 01 Sep 2025 07:50:55 -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 1ut31p-000403-0m; Mon, 01 Sep 2025 07:48:53 -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 1ut31Z-0001sr-1N; Mon, 01 Sep 2025 07:48:51 -0400 Received: by mail-pg1-x52d.google.com with SMTP id 41be03b00d2f7-b4755f37c3eso3733004a12.3; Mon, 01 Sep 2025 04:48:24 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.48.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:48:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727301; x=1757332101; 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=QmHEGXX25Xp5I1MM8WX8B83+pgojb2vRh/cS3EC2mCA=; b=KIFJLqobWz3Sej6GXSJI8H0dFvjzynVWV26ju/tvYXPlqwG+f6dRV3CK8dt7MjfUby 1mMrA1sKJlLgjzQuvzorZaf77FH/yX+N4jBcNACZNIYCDodv6axsThmki9LcoZF9dkea ivVbJkiQxpxCij1vJ/1PKuE4Qwnu+P7J/DcoBXXnusJJ6GuAU7naEeUh0gfKJzpU+LBC vVALyum2o3yPdO+/KvTZ1UUYvIFJi1vFd4Pd4rSIFfkaId9A6hXbWcpf65mu3SvpJwbu 4hsmboOHseQp4O6O4NbYZ++RBM7UOjvfCvasQr7Xi0W36dNLREwtaZGsm5dTkxXwaOyY 3VOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727301; x=1757332101; 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=QmHEGXX25Xp5I1MM8WX8B83+pgojb2vRh/cS3EC2mCA=; b=wjuw/hRE+8VzCWVESly1BoK5EtcMb0aTO2kLjUr5KDA58uhkJNhUWlNO2WixHv3VuK QcxSRa6p7wtJ/vxivMQzCraviPR/5CG3g5127/QpVMKXbi/U2h6VZHqEtsLPT/htW8uT UtQ2402hj0mri/4ZvW41ekIJT6tK8fkTg+b+gxmCZOmbOeHh/PXWr0KWtNZpbJddj+xK kqKN5M5feJ8y7dIHlAQtccIt/tkIl8PlskiPqUSgS25scuGDl5oJdkUcpsHjsuKFErKa A2Br3PQTgW7Dj5qgKhCR+QMpm7/FwqNYP/wgNW4e6hpYZN4XpGzKuFVu9yUi1fKezuXo 7FRw== X-Forwarded-Encrypted: i=1; AJvYcCXZLh5aAw6ys/f+ZcsPi6KC16M/Pv/B+hKCv0DDXJWvH8krI/yI6hpD017Gvkj3aGLPvl+HpJ6S1ArS8g==@nongnu.org, AJvYcCXlw/OODLPEOgY6vyqDNcizVvDx8b41syoVdC7YQEJF4jZzfBoyHffx4PpijuC12MGbDpjpVtv6rg==@nongnu.org X-Gm-Message-State: AOJu0YxB4eXLDVE+D/nGqpuzbte1MyhvTAPys1a3UFvf9Ot6dSKFANSv DDTVxkdb7zwhyE2QWocU//n/uOoql7dQQg8TUppDYFMO4IC++VT+HyHLO8dhwg== X-Gm-Gg: ASbGnctzsxwbnAtsQOXSZSxJ384UqVOdor1/yoC0Wbp3D7ZvUDhSwmzIq5N2hLCj5ED oD1kR7koIKWGM+x1cBSQUY/8P3grVcnODKwoaoW1tFZfQo6IZiV5/4f/Uu4MK4KBea1dM9ITcj3 TmtuTG+7TTYNI5b2gG+rZeJYRiDB9m8OOo/5hq78jHfHpJ1b6vcBwV6+B2W1l5i1Svv68I8Jnzd lLBAD9AnA3OhV6VHKu+XQgMLrSrgAvUdrrb7i8NqZ3ajpaRVAY73jlO/vuYAiBv5TeloRD7W/Wo mvJM23KfaOvpJH92DQjH9bMVx7DGV/UtetW4qdJKIU1gv43s6ZHZmHAh6LQaoTw8FRlSxj8i5gA J3TXl0cVUsByZnMNcQjqZZA== X-Google-Smtp-Source: AGHT+IG4ew5OhDpLszsM2QJSv+5THDAd4dnbET6OJvvGHT27vpHt7anlR+RX5OOyr0xvPxYrFIN3BA== X-Received: by 2002:a17:902:fc84:b0:246:f13b:1b92 with SMTP id d9443c01a7336-24944b758c8mr97490945ad.55.1756727300902; Mon, 01 Sep 2025 04:48:20 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , 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 v3 31/35] tcg/wasm: Enable TLB lookup Date: Mon, 1 Sep 2025 20:44:33 +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, T_SPF_TEMPERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1756728005243116600 Content-Type: text/plain; charset="utf-8" This commit enables Wasm module's 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/wasm/tcg-target.c.inc | 225 +++++++++++++++++++++++++++++++++++++- 1 file changed, 222 insertions(+), 3 deletions(-) diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index 784df9e630..25691307b4 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 @@ -154,6 +158,11 @@ static const uint8_t tcg_target_reg_index[TCG_TARGET_N= B_REGS] =3D { /* Local variable pointing to WasmContext */ #define CTX_IDX 0 =20 +/* Temporary local variables */ +#define TMP32_LOCAL_0_IDX 1 +#define TMP64_LOCAL_0_IDX 2 +#define TMP64_LOCAL_1_IDX 3 + /* Function index */ #define CHECK_UNWINDING_IDX 0 /* A function to check the Asyncify status */ #define HELPER_IDX_START 1 /* The first index of helper functions */ @@ -170,6 +179,8 @@ typedef enum { OPC_RETURN =3D 0x0f, 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 @@ -1217,11 +1228,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); @@ -1230,6 +1386,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 @@ -1238,6 +1402,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)); @@ -1247,6 +1415,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) @@ -1266,12 +1436,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); @@ -1280,6 +1485,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 @@ -1288,6 +1501,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)); @@ -1305,6 +1522,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 void tcg_out_op_l(TCGContext *s, TCGOpcode op, TCGLabel *l0) @@ -2152,7 +2371,7 @@ static const TCGOutOpQemuLdSt outop_qemu_st =3D { =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) @@ -2384,7 +2603,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 */ - 0x0, /* local variables (none) */ + 0x2, 0x1, 0x7f, 0x2, 0x7e, /* local variables (32bit*1, 64bit*2) */ }; =20 #define MOD_3_PH_EXPORT_START_FUNC_IDX 102 --=20 2.43.0 From nobody Tue Sep 16 03:04:09 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=1756727555; cv=none; d=zohomail.com; s=zohoarc; b=TkuTTtSJJ1hm98xh6avYJZRL0tzePLIFon5WoqhvOHv+knslk9qPPD51n0cOI62h+H/LGJO2pIXmJHGP7r9OUglEZScdhyc7I3/VsLKsEi5ODZ3fFGs+cb+cn8G2YonYIDZNOc3GZA1QImpBZ0TNeX0Wq288uTa2Dq/cuBezUG8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727555; 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=Wrau3B2sXx6DsPs6H2dSpeAMNFagRpH9CKQrGL827lA=; b=NN7pdThWAmu7vjg26JloODxroBMnA7MxZssSFSEzJYJr7dO57XzVFEYQHS8B6p6gcZKyw47znZTFcnXgNmIM2ciPLa4bcx6eH2AivuCRH5Knr2SipWoc24EA8VLmlFlVF2voFn64B+9yHTC4mLjy+4k4qojQSWkHZiWtwPyGbgw= 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 1756727555924267.63369080740404; Mon, 1 Sep 2025 04:52:35 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut33k-0006Xw-1x; Mon, 01 Sep 2025 07:50:53 -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 1ut31j-0003td-D1; Mon, 01 Sep 2025 07:48:48 -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 1ut31b-0001uK-AP; Mon, 01 Sep 2025 07:48:47 -0400 Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-24a95f9420aso13826085ad.0; Mon, 01 Sep 2025 04:48:29 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.48.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:48:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727307; x=1757332107; 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=Wrau3B2sXx6DsPs6H2dSpeAMNFagRpH9CKQrGL827lA=; b=RHsGJMIZRUHnXUznUs4IEstOlb0NmJ+pdLrAgzx0KdUVQZ2Shy4eBOjf8Si/D6A56h 9ZRyfL97Ivh5G5zWyQltlZ2HkOMCmE0Rz7mDpaaPgkJ9g9ur1dXczr1RkyjQE1ZodznX 4uQByE9rkNW/0XYnjWcb4ziDF3KRVZGvlJKca+TzW3BPcTrIcgISUQPugPzAhQskzC/B JQxd8H71Tqa4Hl/tJNi4VwBFra6tvHYvjdMQtH5yL6M+c4YhuJ8GS6xbGxstJlg8Iy2I URJ5uPh8oGMSqr3yjNQUxuzOfABA6GoY2PFgIe4MKaMIxVa6RIHSg7KKQt3dlZXxnyuh 4ZOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727307; x=1757332107; 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=Wrau3B2sXx6DsPs6H2dSpeAMNFagRpH9CKQrGL827lA=; b=rSyCqMz2Y5d0Oc+iOQR/UyIWF02tmPeN0KckklQ3oz+yxYKW43PAnioqRlDnDNONAy VmM9zVFJS49pvWkmBt+nIfuwPDJYukKxmRQho90y9SUmaYmCIY6RLaLJN3KVbN6vx7Rz ebITC6m4Km09uqb8M4OhSqIAHn8sUCU3GZ75pWDfvFiVhsckbqb7Pg4ToEGP7sEyM7j9 nacYQ342rFcjIFMlJ9uzOu/zTiTCtNEFh8i1RaHahmx78gInxqtax4VKzYVJOoNgcS1v ltJihnsaB+9kgRdpaFCpHerWB7SKZ2n17vxCzN8N8wDZRXsadsPQCluVOKJ6fOXTsskP 3nQQ== X-Forwarded-Encrypted: i=1; AJvYcCXW1TZT77zgjxtLzreAli4we3tD/IZWP5nKZePCuou+fUDrHG2P7rBmVS/t/9ZdV2ui3dHEFrHj0g==@nongnu.org, AJvYcCXneeWKzAsazLEzvBjbC3+jjytYaLcKOwuuIpjdYZNcDjD3jKkIcekdqLp0iuPvDqBJg4p4qafYauAcBQ==@nongnu.org X-Gm-Message-State: AOJu0YxiH6/122gOg9nFpM1JXPtmmvVWQzrh8dpVY9ymIcXYGYqKUBCX turB/KUJnsmMSHRFXlT/Vw1KRX0JvWRYTtM1qfUIHO6oZjIp0SEO5dPjqO+bWA== X-Gm-Gg: ASbGncunif9t4rEr1VFp6aoocEnsMmKJzEg4NCn5MjRvCSQl9cFz45FpQcJqIaK2DDG +Tv0AVYrjHFDHaDP6vyjboHD3iTXGnVPqduddrYkyCCvJYmcdEzt18JlVD8uvfhzKkZE647WOal IBxJ7Gf+PfO86wFEemSGZD8Mt8lFjh1bFpA6uVeteezIHgG2vHN8AZc1VorijxtWoZ1zJP9FPIQ h9oYu+/l//E9p2jz06FPe+oSH7Cu9yMHZmg1+erHe13SuMHQOBXS3Sn5BWSzBk18iB9QDqevZUA uNVEeeEK7blvAOV71kIw23g1NJAcoaGnDgV/y0cRTjHzrctO3Bp7scSyH2yfsxh3cvijcrdmIBi I2NA/UH8noBkCzzlsNozywLPNC+B1KMpi X-Google-Smtp-Source: AGHT+IG8nxtpQ6bZRaZmbY6USeemcji1i292PRaIKJxgcusJVAX6OGtb50F2NtrzhiDNx3dQCmUXdg== X-Received: by 2002:a17:903:28d:b0:249:2c40:51b2 with SMTP id d9443c01a7336-24944b8143dmr101218695ad.59.1756727306740; Mon, 01 Sep 2025 04:48:26 -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 v3 32/35] tcg/wasm: Add tcg_target_init function Date: Mon, 1 Sep 2025 20:44:34 +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::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, T_SPF_TEMPERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1756727558005124100 Content-Type: text/plain; charset="utf-8" This commit adds tcg_target_init, aligning it with the Wasm backend's register and stack usage. Signed-off-by: Kohei Tokunaga --- tcg/wasm/tcg-target.c.inc | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tcg/wasm/tcg-target.c.inc b/tcg/wasm/tcg-target.c.inc index 25691307b4..3bb4864da5 100644 --- a/tcg/wasm/tcg-target.c.inc +++ b/tcg/wasm/tcg-target.c.inc @@ -2812,3 +2812,32 @@ static int tcg_out_tb_end(TCGContext *s) =20 return 0; } + +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 TCI "registers" are in the local stack frame and + * cannot be clobbered by the called helper functions. Additionally, W= asm + * modules for a TB and QEMU itself (i.e. helpers) are separated so al= so + * those variables aren't clobbered by the called helper functions. + * However, the TB 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); +} --=20 2.43.0 From nobody Tue Sep 16 03:04:09 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=1756727912; cv=none; d=zohomail.com; s=zohoarc; b=axLPSgji8ItY6n3xgymrG3OdNNJBwjfP9p7BqT71lqcH8lFzn5TXYwjD4RAyjykB9b8f51wf4BNG6RttMfQ3UeKi/Hn8AKFEsryPSLwj8Dixgv09fC/zko5OW4VZQPicDOIUx9Y0XxQTke5cyIFHYvSBfLgSvsmeJYqE0EH0bGc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727912; 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=aeqqTWC+7HoIg9vTIlz7vtwEiWiWvUi6bVMstauLERc=; b=PTUPyossJ9YQ7OKa6lNHL7Rg7lgjqkUnwRYhDaYWHl8UdfmPBdloCTgXFt7QDQakIxSUIDJmm0zIbkDXsPYaGxZ7duM8c+1NP0KfSg31jdvKeTH/q6y19flO+rD8PLPvJ9w/HmvjTjUPfjfJDF8fz4E9X3mKF0j9mVdZ4t04OLg= 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 17567279120413.13805635003564; Mon, 1 Sep 2025 04:58:32 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut33q-0006vE-VA; Mon, 01 Sep 2025 07:50:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ut31p-00041f-L5; Mon, 01 Sep 2025 07:48:54 -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 1ut31h-0001ve-AW; Mon, 01 Sep 2025 07:48:53 -0400 Received: by mail-pg1-x529.google.com with SMTP id 41be03b00d2f7-b47175d02dcso3634641a12.3; Mon, 01 Sep 2025 04:48:36 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.48.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:48:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727312; x=1757332112; 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=aeqqTWC+7HoIg9vTIlz7vtwEiWiWvUi6bVMstauLERc=; b=MpQZ967FKmaLJeY84tNvQANt/DA+3i+IH884q+LtnCWHKpIJf3Z5dJzCxG75HHAXjd wjQPG1KVp66pst8IpJe7DPd9xWwvefB0rcBfLvJ255+8ZdNHFvRvcly1hJLCbumVyZxA Pxb9Bzdxre4RFU9Hntcx4A21P4DV3e/v2TSfcUxPxddJhJk7lrv14ENs5xxh6gTcpZ7a oivUvD7x/cdXljVRwP+3XQTK5e6HFJQnaJ9H7utqAEPTGhqxlIGMPwE3G5Y3VjcE6g3o ACbobJpbpEbabDKrN4YerHRpwzBtHL5EMzoH1poevh2cksWsXS2oJtY9DhDZn7Lk0suM hD3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727312; x=1757332112; 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=aeqqTWC+7HoIg9vTIlz7vtwEiWiWvUi6bVMstauLERc=; b=JettOwc2VuWZjrYNJFfRXY8eGozC+DDN8JjpWMGwZhRGKSbqDCzLyA/jpk/Jiz428w i9EWrZ3Hc8k9kFKralMnS5Vn9CQUITc12qvgsONEg5YTmVafNj+JCTbd4NenRmwHyFlh F6futnU9jAh/BrJuAE4KdMpQJ6gLo5SYaoAmyNjrl0k/HqCqPtS3aOcDhjGD091hTPwS 1G75JBxIj8YPb3q15F36oL/SvZys5RiUtUAPw9Ve92owp2/nCUXRsDmM4R0cH11qB9tZ PQEE8H//83CNLTkPaijEuad4QJHqUUj9waaSLHvA0vwf2PWm+ZprnHVTtlIqfDZ+y1VU /91g== X-Forwarded-Encrypted: i=1; AJvYcCXMVOXVPPGm5HgkTUqtMY9wbt+PvYZNhxOvyc4iefwqM14M9Wc627pJ38xX9TBRX33GFQvdtodRc1rpng==@nongnu.org, AJvYcCXOPpXHIfw34+nw+drxd6gddNQxs+TimYMlMJa6xBSkt7rwNTnpjDEhvjPS+8n5UNptKruRXWXamw==@nongnu.org X-Gm-Message-State: AOJu0YzKwMkRRITIX8J35ymZ5gWRIaxqFEJnnsFVW2w00E+lPG5pEITI IX3iIoG4RR/F7TDDoDm4HCe9ILhijpC3dP/c5I7LpT4J5RyV4RT0UgP6Q9bffQ== X-Gm-Gg: ASbGncuPh1fJMTUif4NKqdOSg1HSZE0VzxSZWKu+Vb5BBBkz17XC3NdvFxhbNiQ5jwj KKED9kmIJi++IpO1rH2szGZ9X1cQ02AU+CTglB/KzfWTT1b9no6For8lwKn8qGrVKOB4Hdkp9ws kkZpL/yjNEOQFJ0G06/ZkrHqAxj5M8s1pmkkrBrpIHNOJt4Q0njJyXXDvwvZyJKpisJUH/tn1Ee rZYtLtrnzMQ05DgpXvSH6lE+2yF86WT/1AvIuZfZ1HfArPmGfZE6VHga4d4yaU069UhLbEkjFjF MjZgrgqKR6h3DWF8YHwsc3ud7XF3knq5Tf4+I5c1Pk+kNrl6jxItPyK9/HWbmLN7R+AClKH4lr4 NW8Y3chd/TNFVJm2DxdDenQ== X-Google-Smtp-Source: AGHT+IE2P0y3EXRBcz5+VxcwlR2IflV3hZpTShg6qxom2+2ldzNKmBy6OUihtfnE2AV1TxSTNXnCpw== X-Received: by 2002:a17:903:166e:b0:248:a054:e1cc with SMTP id d9443c01a7336-249448de11cmr114129205ad.18.1756727312243; Mon, 01 Sep 2025 04:48: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 v3 33/35] meson.build: enable to build Wasm backend Date: Mon, 1 Sep 2025 20:44:35 +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: 1756727913984116600 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 | 6 ++++-- tcg/meson.build | 5 +++++ tcg/region.c | 10 +++++----- tcg/tcg.c | 14 +++++++------- 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 5b048ea70f..42b9004a20 100644 --- a/meson.build +++ b/meson.build @@ -920,9 +920,9 @@ 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') + error('wasm32 host requires --enable-tcg-interpreter') endif elif get_option('tcg_interpreter') warning('Use of the TCG interpreter is not recommended on this host') @@ -938,6 +938,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 7d3e7f8cb1..bd8f8e565f 100644 --- a/tcg/tcg.c +++ b/tcg/tcg.c @@ -256,7 +256,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 @@ -1443,7 +1443,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) { /* @@ -1520,7 +1520,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) { @@ -1897,7 +1897,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 @@ -1916,7 +1916,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 @@ -1953,7 +1953,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, @@ -7055,7 +7055,7 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb,= uint64_t pc_start) return i; } =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 Tue Sep 16 03:04:09 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=1756727761; cv=none; d=zohomail.com; s=zohoarc; b=A+Y7GaGIw+Fo2jtasMl3dnjoVRohVKbo+4Sz0z7ktStAFf1fY2y18g8aARIA6zgGLUulOIRTcbOfTMSSyT0YXBfO927hGt72nAEAURa2IiAfLKcf2O79EQvY5EQyEG9XMSRklgl5ApwQ0H6sUR50mewIb4sD5e2nb6lbJ9t7zMQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727761; 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=izBMAHH1elIGUTPUqS9W9SlkS9NCVx/jVWrYgAA/7nc=; b=O6GuUEyLuf3wZTrHl1ksG1qNWFrnIeGnnda1e7zcmYI6y3bXltC3H3kfkZXek6j87tRT0BLnybnELznxoqckcHGH95TL7FvV9KTLZGfBTiRgFaxmFuCI6dq9bNupWM1p8xXKN10IhTN+9VRBG1TDddySR/DU5J24U9efl6ou1rw= 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 1756727761801252.604341490342; Mon, 1 Sep 2025 04:56:01 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut33r-0006uy-WD; Mon, 01 Sep 2025 07:51: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 1ut31r-00045d-JJ; Mon, 01 Sep 2025 07:48:56 -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 1ut31j-0001wJ-Ph; Mon, 01 Sep 2025 07:48:54 -0400 Received: by mail-pg1-x52b.google.com with SMTP id 41be03b00d2f7-b49cf1d4f6fso2913748a12.3; Mon, 01 Sep 2025 04:48:40 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.48.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:48:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727318; x=1757332118; 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=izBMAHH1elIGUTPUqS9W9SlkS9NCVx/jVWrYgAA/7nc=; b=SXKBQsnQ0i9/Ql+19vkruBWJLVHrbLrNla4poytlaRU+4Z3F6dz4rg0YdSCYs1l38G 6/5kR57UEdMTw9rUshMwZqCBXQrplWY35SEjY36FBl+eV9O1c1/CES2aK4HSKepB1SdR FqV/SvDvd63xOLcWvM9y+kgtdY0+OM/Om7+chdIkteWgNhJVfkfNVTqhJcOoj1l8VmRd fwYZ94elel8pvFNEsCT571RUEHzGfAk4KZiVciu5BRGZhJIB990K6l1C0vCYADv0xa7u 9glaoMTe9wimmBuTIAqWJjukFnEWCxHkcEjX8RxcNAn316ZnG5FJQ6QW+NXrTaJeiD1X tBKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727318; x=1757332118; 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=izBMAHH1elIGUTPUqS9W9SlkS9NCVx/jVWrYgAA/7nc=; b=TWYKlARHCBssglYev79rKs8aImXN41M/PhuVq2DlQdLzIkPD1eg7mO+r+fiOpVWVh8 LGRzUQxPvEik4fh2h/Qie2jb1tbvdKyLZRzUpmJS+mMstbXxLUKtXQB/EvVs5jfEn4x4 s7NlZJOR1v+coLpnWu3zMY+Sl2uU0g22UnqbgKZw9ZK9zFXSYzCZSrlNjsa85yRzzQ7s VocnC8pa4xW5TkLnMPxFsT8FrSGNeppZf600SoJ3I5YCNAF17lNaujXWN1X64hrf0xgO s8Qcdk7Pc1VNB43AgJfyAxQ/xOFy3NjCd65z+lpTcs3PoDP82mbl0qZQlnq7r75nnUof rb/g== X-Forwarded-Encrypted: i=1; AJvYcCUHWXUZ6rNK+dmWPj6Y5aV2UQ+E3qYYO7zMeakjslOF+sYDLZ+dFaOMRnvITw5vEUkpbGeREOJbkg==@nongnu.org, AJvYcCXUXqKbhEyVfdQoiUnviFa2DeRWmg6NHn2ukVYvlBnlH7elt7DcPV+sONhjyPWL/xYyC0wvV5BmAZpnJA==@nongnu.org X-Gm-Message-State: AOJu0Yw7ZsNiU73tetM3syg/sD0A1E/9Jr07hjic0e8y/1/wN1zsO3hs X+002tb+I/19HmlTpGwZ4KY3VC/NPoZvCzGfvci9tlnncHe76nECwb40gn6Odg== X-Gm-Gg: ASbGnctm/DdgEwDv09Sbd1s5o1BIOjM42+BPSgt6EA2/MjbWwgNZe+Wd/wbuhjB4Cy3 pRHF+2Db6ctaPjm1tMztAzN6lnRGJSb3uRmVW9RLeHyupQJPjqdFq1l5/bjTYAKUGsiVn6OPqQp +wg9bSCAWQTmjUcWtrWS2/QrpXT7X/o54T8Zsx3jBIBRUgSuKDxEdITaJBwu2FyFe8hycmtpgnx nh/gn2PlkG1M6l2JUBuQbZ8qXDvsnGhbuVeWjXfo8HyxVBsbS7a8iNfObvBhGkmJ4dipSinzdZG 0LE6TUxrrAR6Thztr5Mcuj6ushh0lKBhIOlOgvo/9faB+pqQZWwNb9zjq+HpopLXy0psk9sC6d1 PoTDVhxTk1HIo2Ld/VfqJ6OsyXLOcAxWna7d7QjDwhC8= X-Google-Smtp-Source: AGHT+IEnkcgQiFfDG8r/K1svMya16Z3P/LTmYW2pXxVN1DRaAJAjP9qgGXMZmBc2ozDpfFJaTEwWXQ== X-Received: by 2002:a17:902:cec8:b0:249:44b5:d5b6 with SMTP id d9443c01a7336-24944b5d87emr96061635ad.40.1756727317902; Mon, 01 Sep 2025 04:48:37 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , 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 v3 34/35] meson.build: Propagate optimization flag for linking on Emscripten Date: Mon, 1 Sep 2025 20:44:36 +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::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: 1756727764466124100 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 42b9004a20..ffd16fc167 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 Tue Sep 16 03:04:09 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=1756727742; cv=none; d=zohomail.com; s=zohoarc; b=HY6yiCR73akbJt5oYvv7/OWqqellmjlCgxvadozN/rJDvTPGu58ICmGGFRtm4jWOFzTIIup8GpPYqH+qJOtD7np43TPtZ37JfIi7G2AiixxGTXPuD3AHP1jSwj7xwU60etBZFs1hilU6uM1NFiX5M87/JimXfFPMA5jgfAy4tWQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756727742; 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=L41MYroyz7zAc6vbqFlgy9GYItIqYxbyyOq32EDcfPsPmwgWEBDi5MPOzvtootld/PVE6tu+eFN8tZCEtyDLinzVNDIGAtzuxFN6U1bz6U+SNRZtbsAZxNFJcapVWz8cr8FeMcZS7d8675QEevcBnwjyOiKij3gXV4HP3XrgW1E= 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 17567277429071002.0764242264812; Mon, 1 Sep 2025 04:55:42 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ut33x-000745-Fv; Mon, 01 Sep 2025 07:51:05 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ut31v-0004Cu-1k; Mon, 01 Sep 2025 07:48:59 -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 1ut31o-0001xI-I0; Mon, 01 Sep 2025 07:48:58 -0400 Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-24abf0215e8so8982865ad.1; Mon, 01 Sep 2025 04:48:46 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:750a:cb0b:f0f9:68f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-249065a8036sm103038775ad.129.2025.09.01.04.48.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 04:48:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756727323; x=1757332123; 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=WT70pG7X7/DRAKFq3fnlIsfRev1wDgayeN4HVVpACNY5WWQMCAp7FG0Mvvy3LoVAO9 qZ6BL3FliVQH9Z49TZEsKO9A2OexydyJx1sSmhXDBpt3OZpc7r++PebCdfLmuxvEzJ+U yjWPQ8ZBxZZ6oLRJTyq4t+oUCRJVA2FqF7eNtQXSpqfqz3PiU3J38fduAA+W451ot12+ o55P5Gs7BRk9Bg1tY8FxTuv6c2n+MgEv8OaX8tzLLkn0tI+13AwKXmlCMALT9TTi/Xxe sEo3niZYTWTZA5GfG450akzeMclf8Vjqa8ZKoZboQfdxPlBIspL6eUQ+nN8d6LJHAFm/ wTWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756727323; x=1757332123; 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=ZiSzv9Ca7J1TGzjOvAgUbFhbGvM1wOn6ft4SmVIJcJRY66PZ56wgxjdiWQ1YT/dXdp HEL5GUHuKDhpxUBs2Qk+B+9dmgVAzycs85L/cHN0Vro5VD042MBh8+8f5G3ol4INy18d gQ/9KUxO+HS2TmPFLOyoP/Z0qEVVGXATGhCBhhPOgklwASVeV/P6KA0uCIIrIZzrgq0X Khh16bclMZhb5Q+wf1nNVy2NWsnztW4UO7Eo7hrARfWxdqT7qcBAhAWOEhSYLhvuDhwx 5pQ1D9/lxAWgUjjAegsbVcqO0IamGsf6arMNSB3VOx9ANmcq4f8cX/PJqz3rlp8x0u3T 406w== X-Forwarded-Encrypted: i=1; AJvYcCUoN3PGf4DQGFNVG56yhPulBMd5qLuQfE07IUOQAz8sWbveUq/kPRHmSgpzgDWanrIUKvg3HG78pZc/Qg==@nongnu.org, AJvYcCWx533ojmyOqj+5wUVGuhh2M2WvgimZUQBRAApUxj0RH6tZ2x/mqsqXuq8BzqhizmD6nWTRyp+BSg==@nongnu.org X-Gm-Message-State: AOJu0YwXDp14Vbj4c0fpfmLAbVkBG/v6Q3BjTMwwMcD70GSjBjkx6kjr woLncn6ZD6B8uJ0io57qlRF0eVHoakLmUlYvL3GiikJCEQ3V0koboVpSrnId+A== X-Gm-Gg: ASbGnctOqX40vbWwSdBJk7C51lLW2WjxHdHcraG/jqeOgrFQAqOPlTb8Vev7eQjAcEw lxaTgMYQvbUvyVXKSLhNLTGz0HcJNml55u97WlLMdzecri3U0CbN6PqGepIJwMoD5jRuiMD174l IuihoQ3KpQX8eEkrj58FYd7zIBqFdniz653Prf270biCgc9vcj9WUUvpjHILmOnqqE9jLHmxlxj Mg5sjJhWngZcH6hty0SC/HdUiFmk0U/HwXuAvekxrIg8cCjuzMfsUe8vQ2NyMAGPmrczH7+QSSQ A8ckUfafpQVuhCajbIYuphu0MS9Oq/lAdrpN/WSuwmPw4CLjmnTTmmVMhTG1dGKP4Ehqh3NClWr 5QgNQLyWv2OKCs6AqQD7kJA== X-Google-Smtp-Source: AGHT+IGBuKo7oc8jEgSjGF83e8jdS2ypBE6voQc1b4u6zfd+jYJdQPYmrsSnDAf6fCCLCxvmT9JGqA== X-Received: by 2002:a17:903:2c0e:b0:246:92f5:1c07 with SMTP id d9443c01a7336-24944b01822mr81547615ad.51.1756727323340; Mon, 01 Sep 2025 04:48:43 -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 v3 35/35] .gitlab-ci.d: build wasm backend in CI Date: Mon, 1 Sep 2025 20:44:37 +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: 1756727743620116600 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