From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 726963948A4A; Thu, 6 Oct 2022 20:05:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 726963948A4A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1665086753; bh=I9ptB5mrL6M8fTGtn5nPlwwjv9jn1gZz9i2qvv35wxY=; h=From:To:Subject:Date:From; b=xWjSexfHpohmgO+oBTUH9q+VH48JZ8l/WHZaZgkV4X0EaGXECYT6V6ejNFrfT/xgh t22Iuy5vnug7h69tfPKjSdoUw//lzEO+J2yHzdrXpFfvlohb+jYvmGZrkXlHS7fEf3 Aad06z9mt50OVBL9xo3vDj98vvyHxha+x9J5Cj8Q= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] builtins: Move implementation into source file X-Act-Checkin: gcc X-Git-Author: Arthur Cohen X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: 8ff035ddc55079161d24941785114aa0f5056260 X-Git-Newrev: a8b1f98f832d45bed4d2bc932297ff5acc39b3c9 Message-Id: <20221006200553.726963948A4A@sourceware.org> Date: Thu, 6 Oct 2022 20:05:53 +0000 (GMT) List-Id: https://gcc.gnu.org/g:a8b1f98f832d45bed4d2bc932297ff5acc39b3c9 commit a8b1f98f832d45bed4d2bc932297ff5acc39b3c9 Author: Arthur Cohen Date: Thu Oct 6 10:19:11 2022 +0200 builtins: Move implementation into source file Diff: --- 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(-) diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in index 5996696e425..e8735202ae7 100644 --- a/gcc/rust/Make-lang.in +++ b/gcc/rust/Make-lang.in @@ -151,6 +151,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_;