From patchwork Tue Feb 21 12:00:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 59955 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1824912wrn; Tue, 21 Feb 2023 04:07:56 -0800 (PST) X-Google-Smtp-Source: AK7set9UKJhgwBPkOkC6TkfcrJR7Z9pO60B15MV3NbDQCVD0fhcrcYoqcapHa9Y4jhpPd0W12A5A X-Received: by 2002:a05:6402:b38:b0:4ac:bd6e:9975 with SMTP id bo24-20020a0564020b3800b004acbd6e9975mr4675368edb.20.1676981276265; Tue, 21 Feb 2023 04:07:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676981276; cv=none; d=google.com; s=arc-20160816; b=BzbWDbz4Ei7B2DuJRAzs8jliIZUjDpHFRkTg/BhvgQ2W24TzgeaP3IHa+l6zRBPAFK ziZHwSeNZmqQtaQcatGUnXenSBuGCJe3bNkIYSbqR/1MF434ag4+FIY9ZCOSlg7haX4B bisgeYDIu5vZVOVXxifsbRhZ96cJaXV1HAdq+E89l+hX56wXflea3EJsX8nRMZDPg8Al pMP920HJ7bu3tWXK0lVmzoBY1j2DWZgBOqcMfcfX2WqK3JL++in2Mtvznbmew8Myte14 IMTjhjmRrVDUzC5i6Gm9G4L8vJ/pvVTvtDotpPB1PGpOissAjpPH7fePTKL4emOjhm1c VZyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature:dmarc-filter :delivered-to; bh=Wy32g5WnNnlbrMJWBS95j4VbYYgvfyhaBRzC6glmet0=; b=wzXdMMtxVKDiIib83ylWUBiDoiBqLV/GW6IzRNavaz2m3U5w7mhbuMUk98azoaN0iw rG+cZJfCND4jnxjEoGAC+Q8tx2hXGgY+BPiUr/8Xht7pdj5hy90434OvRVTNJcwPv0rr 8Z4WFyqRG06l+YI2neA1TK1SYp2tz/uvYDKiEwHVTYXVCz/mBb/dLYRFSVJRx+/pnVjl qwSfBqLAaHn1FwaboBGmEOWb7OekefstdRWcbGoyfOgApNpX/w6zeW5m44waMKDRkgBw GEg0n0q7QvrPB1g/rqRHuygPizWpAPLUyXJi8129qKXYfKLrmjGy8NJ1bXOUT+bHWoTk QSNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b="Ym/abc/F"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id g18-20020a056402115200b004acc6cedef7si8383752edw.66.2023.02.21.04.07.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Feb 2023 04:07:56 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b="Ym/abc/F"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6B7F6388B6AF for ; Tue, 21 Feb 2023 12:04:52 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by sourceware.org (Postfix) with ESMTPS id 2DD16384DD00 for ; Tue, 21 Feb 2023 12:03:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2DD16384DD00 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com Received: by mail-wr1-x42c.google.com with SMTP id z8so3882171wrm.8 for ; Tue, 21 Feb 2023 04:03:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=Wy32g5WnNnlbrMJWBS95j4VbYYgvfyhaBRzC6glmet0=; b=Ym/abc/FN8ZrHVt2HzP8X4TWe/1Pv1fScZYrVFyRFtjgYjshjZKiV7O9lK8Ep7ZqHS foDdeVvMjXw277BYnbxKH6pjvSyE0CKimUOT10rw5ZIfEn5tncX+xF4teZPEcrghx8nA N7fgWLytQ498PFZZdjV33MHLm4QdG7XheQPkI/DmaXxQYyjC2QO829J6E56t8OGiWSGT /qZR1S2ddcOq0fRrG6Dh8pvmFf3smQl1byRzSFAJq4EslNKfk7DWlXvFyK9cW9gSfaa9 iWH6krF7uzqyB3sZiQC5qUzPp1+VpCOB7XdVpP2dDr6ttaRrKnV4Fot+4lnj6IIo1nSs P5rQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:reply-to: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=Wy32g5WnNnlbrMJWBS95j4VbYYgvfyhaBRzC6glmet0=; b=gyhJC3hMQDXObQnArhTpx6DlJIm+unRXysTOvJQ2APUnEsZYghQMeQYdTbqoPdfUNM WieWdk50zRtQZ6T/FMVIdTN0EnEf0E+HxrQq5rhfl4pWNrjbE3ei2L3Wae7G01X5keuT AHzZWiko4N4mdv0/jYFZ1p9LbX3aGcsDPhGA1UFsiXtalbgT3/gkevWUXsTh51Msedxz s5NSkO9MDZwiu4rblNivhau955S2XYSUEkEB543o2v7ONAzWUR6QY3+CS6VU0SqiWYmN ygxGqR5YjpbSoJv1/XJ/0n2NV58C5Bv0ZTV8w6YDclleUwtwhi+06je9t1ZZlZFIDexn JBVw== X-Gm-Message-State: AO0yUKX7NBbxCYY/fi0Ht08dTivOQEIL215KSs7FBbaYWvXTYnJwIXOG DfxCtH4KkwrynpE78VQXJCzzIA18c92F82XvxA== X-Received: by 2002:a5d:4ccd:0:b0:2c3:eaff:aae7 with SMTP id c13-20020a5d4ccd000000b002c3eaffaae7mr3963186wrt.17.1676981010786; Tue, 21 Feb 2023 04:03:30 -0800 (PST) Received: from platypus.localdomain ([62.23.166.218]) by smtp.gmail.com with ESMTPSA id c15-20020adffb4f000000b002c55b0e6ef1sm5013811wrs.4.2023.02.21.04.03.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Feb 2023 04:03:30 -0800 (PST) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Arthur Cohen Subject: [committed 008/103] gccrs: builtins: Move implementation into source file Date: Tue, 21 Feb 2023 13:00:58 +0100 Message-Id: <20230221120230.596966-9-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230221120230.596966-1-arthur.cohen@embecosm.com> References: <20230221120230.596966-1-arthur.cohen@embecosm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-14.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: arthur.cohen@embecosm.com Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758442318750580853?= X-GMAIL-MSGID: =?utf-8?q?1758442318750580853?= From: Arthur Cohen gcc/rust/ChangeLog: * Make-lang.in: Add `rust-builtins.o` as target * backend/rust-builtins.h: Refactor to new file. * backend/rust-builtins.cc: New file. --- gcc/rust/Make-lang.in | 1 + gcc/rust/backend/rust-builtins.cc | 143 ++++++++++++++++++++++++++++++ gcc/rust/backend/rust-builtins.h | 120 +++---------------------- 3 files changed, 156 insertions(+), 108 deletions(-) create mode 100644 gcc/rust/backend/rust-builtins.cc diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in index 7dad27a5dcd..a24025cb31c 100644 --- a/gcc/rust/Make-lang.in +++ b/gcc/rust/Make-lang.in @@ -156,6 +156,7 @@ GRS_OBJS = \ rust/rust-imports.o \ rust/rust-import-archive.o \ rust/rust-extern-crate.o \ + rust/rust-builtins.o \ $(END) # removed object files from here diff --git a/gcc/rust/backend/rust-builtins.cc b/gcc/rust/backend/rust-builtins.cc new file mode 100644 index 00000000000..14dc037edac --- /dev/null +++ b/gcc/rust/backend/rust-builtins.cc @@ -0,0 +1,143 @@ +// This file is part of GCC. + +// GCC 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 3, or (at your option) any later +// version. + +// GCC 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 GCC; see the file COPYING3. If not see +// . + +#include "rust-builtins.h" + +namespace Rust { +namespace Compile { + +static const int builtin_const = 1 << 0; +static const int builtin_noreturn = 1 << 1; +static const int builtin_novops = 1 << 2; + +BuiltinsContext & +BuiltinsContext::get () +{ + static BuiltinsContext instance; + return instance; +} + +bool +BuiltinsContext::lookup_simple_builtin (const std::string &name, tree *builtin) +{ + auto it = rust_intrinsic_to_gcc_builtin.find (name); + if (it == rust_intrinsic_to_gcc_builtin.end ()) + return false; + + return lookup_gcc_builtin (it->second, builtin); +} + +BuiltinsContext::BuiltinsContext () { setup (); } + +void +BuiltinsContext::setup_overflow_fns () +{ + tree overflow_type + = build_varargs_function_type_list (boolean_type_node, NULL_TREE); + + define_builtin ("add_overflow", BUILT_IN_ADD_OVERFLOW, + "__builtin_add_overflow", "add_overflow", overflow_type, 0); + define_builtin ("sub_overflow", BUILT_IN_SUB_OVERFLOW, + "__builtin_sub_overflow", "sub_overflow", overflow_type, 0); + define_builtin ("mul_overflow", BUILT_IN_MUL_OVERFLOW, + "__builtin_mul_overflow", "mul_overflow", overflow_type, 0); +} + +void +BuiltinsContext::setup_math_fns () +{ + tree math_function_type_f32 + = build_function_type_list (float_type_node, float_type_node, NULL_TREE); + + define_builtin ("sinf32", BUILT_IN_SINF, "__builtin_sinf", "sinf", + math_function_type_f32, builtin_const); + define_builtin ("sqrtf32", BUILT_IN_SQRTF, "__builtin_sqrtf", "sqrtf", + math_function_type_f32, builtin_const); +} + +void +BuiltinsContext::setup () +{ + setup_math_fns (); + setup_overflow_fns (); + + define_builtin ("unreachable", BUILT_IN_UNREACHABLE, "__builtin_unreachable", + NULL, build_function_type (void_type_node, void_list_node), + builtin_const | builtin_noreturn); + + define_builtin ("abort", BUILT_IN_ABORT, "__builtin_abort", "abort", + build_function_type (void_type_node, void_list_node), + builtin_const | builtin_noreturn); + + define_builtin ("breakpoint", BUILT_IN_TRAP, "__builtin_trap", "breakpoint", + build_function_type (void_type_node, void_list_node), + builtin_const | builtin_noreturn); + + define_builtin ("memcpy", BUILT_IN_MEMCPY, "__builtin_memcpy", "memcpy", + build_function_type_list (build_pointer_type (void_type_node), + build_pointer_type (void_type_node), + build_pointer_type (void_type_node), + size_type_node, NULL_TREE), + 0); +} + +static void +handle_flags (tree decl, int flags) +{ + if (flags & builtin_const) + TREE_READONLY (decl) = 1; + if (flags & builtin_noreturn) + TREE_READONLY (decl) = 1; + if (flags & builtin_novops) + DECL_IS_NOVOPS (decl) = 1; +} + +void +BuiltinsContext::define_builtin (const std::string rust_name, + built_in_function bcode, const char *name, + const char *libname, tree fntype, int flags) +{ + tree decl = add_builtin_function (name, fntype, bcode, BUILT_IN_NORMAL, + libname, NULL_TREE); + handle_flags (decl, flags); + set_builtin_decl (bcode, decl, true); + + this->builtin_functions_[name] = decl; + if (libname != NULL) + { + decl = add_builtin_function (libname, fntype, bcode, BUILT_IN_NORMAL, + NULL, NULL_TREE); + handle_flags (decl, flags); + + this->builtin_functions_[libname] = decl; + } + + rust_intrinsic_to_gcc_builtin[rust_name] = name; +} + +bool +BuiltinsContext::lookup_gcc_builtin (const std::string &name, tree *builtin) +{ + auto it = builtin_functions_.find (name); + if (it == builtin_functions_.end ()) + return false; + + *builtin = it->second; + return true; +} + +} // namespace Compile +} // namespace Rust diff --git a/gcc/rust/backend/rust-builtins.h b/gcc/rust/backend/rust-builtins.h index 5cd84010723..e421fa43755 100644 --- a/gcc/rust/backend/rust-builtins.h +++ b/gcc/rust/backend/rust-builtins.h @@ -78,88 +78,18 @@ namespace Compile { class BuiltinsContext { public: - static BuiltinsContext &get () - { - static BuiltinsContext instance; - return instance; - } + static BuiltinsContext &get (); - bool lookup_simple_builtin (const std::string &name, tree *builtin) - { - auto it = rust_intrinsic_to_gcc_builtin.find (name); - if (it == rust_intrinsic_to_gcc_builtin.end ()) - return false; - - return lookup_gcc_builtin (it->second, builtin); - } + bool lookup_simple_builtin (const std::string &name, tree *builtin); private: - static const int builtin_const = 1 << 0; - static const int builtin_noreturn = 1 << 1; - static const int builtin_novops = 1 << 2; - - BuiltinsContext () { setup (); } - - void setup_overflow_fns () - { - tree overflow_type - = build_varargs_function_type_list (boolean_type_node, NULL_TREE); - - define_builtin ("add_overflow", BUILT_IN_ADD_OVERFLOW, - "__builtin_add_overflow", "add_overflow", overflow_type, 0); - define_builtin ("sub_overflow", BUILT_IN_SUB_OVERFLOW, - "__builtin_sub_overflow", "sub_overflow", overflow_type, 0); - define_builtin ("mul_overflow", BUILT_IN_MUL_OVERFLOW, - "__builtin_mul_overflow", "mul_overflow", overflow_type, 0); - } - - void setup_math_fns () - { - tree math_function_type_f32 - = build_function_type_list (float_type_node, float_type_node, NULL_TREE); - - define_builtin ("sinf32", BUILT_IN_SINF, "__builtin_sinf", "sinf", - math_function_type_f32, builtin_const); - define_builtin ("sqrtf32", BUILT_IN_SQRTF, "__builtin_sqrtf", "sqrtf", - math_function_type_f32, builtin_const); - } - - void setup () - { - setup_math_fns (); - setup_overflow_fns (); - - define_builtin ("unreachable", BUILT_IN_UNREACHABLE, - "__builtin_unreachable", NULL, - build_function_type (void_type_node, void_list_node), - builtin_const | builtin_noreturn); - - define_builtin ("abort", BUILT_IN_ABORT, "__builtin_abort", "abort", - build_function_type (void_type_node, void_list_node), - builtin_const | builtin_noreturn); - - define_builtin ("breakpoint", BUILT_IN_TRAP, "__builtin_trap", "breakpoint", - build_function_type (void_type_node, void_list_node), - builtin_const | builtin_noreturn); - - define_builtin ( - "memcpy", BUILT_IN_MEMCPY, "__builtin_memcpy", "memcpy", - build_function_type_list (build_pointer_type (void_type_node), - build_pointer_type (void_type_node), - build_pointer_type (void_type_node), - size_type_node, NULL_TREE), - 0); - } - - static void handle_flags (tree decl, int flags) - { - if (flags & builtin_const) - TREE_READONLY (decl) = 1; - if (flags & builtin_noreturn) - TREE_READONLY (decl) = 1; - if (flags & builtin_novops) - DECL_IS_NOVOPS (decl) = 1; - } + BuiltinsContext (); + + void setup_overflow_fns (); + + void setup_math_fns (); + + void setup (); // Define a builtin function. BCODE is the builtin function code // defined by builtins.def. NAME is the name of the builtin function. @@ -169,35 +99,9 @@ private: // NORETURN_P is true if the function has the noreturn attribute. void define_builtin (const std::string rust_name, built_in_function bcode, const char *name, const char *libname, tree fntype, - int flags) - { - tree decl = add_builtin_function (name, fntype, bcode, BUILT_IN_NORMAL, - libname, NULL_TREE); - handle_flags (decl, flags); - set_builtin_decl (bcode, decl, true); - - this->builtin_functions_[name] = decl; - if (libname != NULL) - { - decl = add_builtin_function (libname, fntype, bcode, BUILT_IN_NORMAL, - NULL, NULL_TREE); - handle_flags (decl, flags); - - this->builtin_functions_[libname] = decl; - } - - rust_intrinsic_to_gcc_builtin[rust_name] = name; - } - - bool lookup_gcc_builtin (const std::string &name, tree *builtin) - { - auto it = builtin_functions_.find (name); - if (it == builtin_functions_.end ()) - return false; - - *builtin = it->second; - return true; - } + int flags); + + bool lookup_gcc_builtin (const std::string &name, tree *builtin); // A mapping of the GCC built-ins exposed to GCC Rust. std::map builtin_functions_;