LCOV - code coverage report
Current view: top level - gcc/rust/backend - rust-builtins.h (source / functions) Coverage Total Hit
Test: gcc.info Lines: 7.1 % 14 1
Test Date: 2026-11-14 16:39:50 Functions: 0.0 % 1 0
Legend: Lines:     hit not hit

            Line data    Source code
       1              : // Copyright (C) 2020-2026 Free Software Foundation, Inc.
       2              : 
       3              : // This file is part of GCC.
       4              : 
       5              : // GCC is free software; you can redistribute it and/or modify it under
       6              : // the terms of the GNU General Public License as published by the Free
       7              : // Software Foundation; either version 3, or (at your option) any later
       8              : // version.
       9              : 
      10              : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      11              : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
      12              : // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      13              : // for more details.
      14              : 
      15              : // You should have received a copy of the GNU General Public License
      16              : // along with GCC; see the file COPYING3.  If not see
      17              : // <http://www.gnu.org/licenses/>.
      18              : 
      19              : #ifndef RUST_BUILTINS_H
      20              : #define RUST_BUILTINS_H
      21              : 
      22              : #include "rust-system.h"
      23              : #include "rust-tree.h"
      24              : #include "langhooks.h"
      25              : #include "tree.h"
      26              : #include "selftest.h"
      27              : #include "util/expected.h"
      28              : #include "util/optional.h"
      29              : #include "util/rust-ggc.h"
      30              : #include "rust-diagnostics.h"
      31              : 
      32              : namespace Rust {
      33              : namespace Compile {
      34              : 
      35              : // https://github.com/rust-lang/rust/blob/master/library/core/src/intrinsics.rs
      36              : // https://github.com/rust-lang/rust/blob/master/compiler/rustc_codegen_llvm/src/intrinsic.rs
      37              : // https://github.com/Rust-GCC/gccrs/issues/658
      38              : //
      39              : //   let llvm_name = match name {
      40              : //     sym::sqrtf32 => "llvm.sqrt.f32",
      41              : //     sym::sqrtf64 => "llvm.sqrt.f64",
      42              : //     sym::powif32 => "llvm.powi.f32",
      43              : //     sym::powif64 => "llvm.powi.f64",
      44              : //     sym::sinf32 => "llvm.sin.f32",
      45              : //     sym::sinf64 => "llvm.sin.f64",
      46              : //     sym::cosf32 => "llvm.cos.f32",
      47              : //     sym::cosf64 => "llvm.cos.f64",
      48              : //     sym::powf32 => "llvm.pow.f32",
      49              : //     sym::powf64 => "llvm.pow.f64",
      50              : //     sym::expf32 => "llvm.exp.f32",
      51              : //     sym::expf64 => "llvm.exp.f64",
      52              : //     sym::exp2f32 => "llvm.exp2.f32",
      53              : //     sym::exp2f64 => "llvm.exp2.f64",
      54              : //     sym::logf32 => "llvm.log.f32",
      55              : //     sym::logf64 => "llvm.log.f64",
      56              : //     sym::log10f32 => "llvm.log10.f32",
      57              : //     sym::log10f64 => "llvm.log10.f64",
      58              : //     sym::log2f32 => "llvm.log2.f32",
      59              : //     sym::log2f64 => "llvm.log2.f64",
      60              : //     sym::fmaf32 => "llvm.fma.f32",
      61              : //     sym::fmaf64 => "llvm.fma.f64",
      62              : //     sym::fabsf32 => "llvm.fabs.f32",
      63              : //     sym::fabsf64 => "llvm.fabs.f64",
      64              : //     sym::minnumf32 => "llvm.minnum.f32",
      65              : //     sym::minnumf64 => "llvm.minnum.f64",
      66              : //     sym::maxnumf32 => "llvm.maxnum.f32",
      67              : //     sym::maxnumf64 => "llvm.maxnum.f64",
      68              : //     sym::copysignf32 => "llvm.copysign.f32",
      69              : //     sym::copysignf64 => "llvm.copysign.f64",
      70              : //     sym::floorf32 => "llvm.floor.f32",
      71              : //     sym::floorf64 => "llvm.floor.f64",
      72              : //     sym::ceilf32 => "llvm.ceil.f32",
      73              : //     sym::ceilf64 => "llvm.ceil.f64",
      74              : //     sym::truncf32 => "llvm.trunc.f32",
      75              : //     sym::truncf64 => "llvm.trunc.f64",
      76              : //     sym::rintf32 => "llvm.rint.f32",
      77              : //     sym::rintf64 => "llvm.rint.f64",
      78              : //     sym::nearbyintf32 => "llvm.nearbyint.f32",
      79              : //     sym::nearbyintf64 => "llvm.nearbyint.f64",
      80              : //     sym::roundf32 => "llvm.round.f32",
      81              : //     sym::roundf64 => "llvm.round.f64",
      82              : //     _ => return None,
      83              : // };
      84              : // Some(cx.get_intrinsic(&llvm_name))
      85              : 
      86              : /**
      87              :  * Error type for Builtins::map_llvm_to_gcc_builtin
      88              :  */
      89              : struct LlvmBuiltinMappingError
      90              : {
      91              :   enum class Kind
      92              :   {
      93              :     NOT_MAPPED,
      94              :     TARGET_UNAVAILABLE,
      95              :   } kind;
      96              : 
      97            0 :   static LlvmBuiltinMappingError NotMapped ()
      98              :   {
      99            0 :     return LlvmBuiltinMappingError (Kind::NOT_MAPPED);
     100              :   }
     101              : 
     102            0 :   static LlvmBuiltinMappingError TargetUnavailable ()
     103              :   {
     104            0 :     return LlvmBuiltinMappingError (Kind::TARGET_UNAVAILABLE);
     105              :   }
     106              : 
     107              :   /**
     108              :    * Emit the error for the missing intrinsic
     109              :    */
     110            0 :   void emit (const GGC::Ident &name, location_t loc)
     111              :   {
     112            0 :     switch (kind)
     113              :       {
     114            0 :       case LlvmBuiltinMappingError::Kind::TARGET_UNAVAILABLE:
     115            0 :         rust_error_at (loc,
     116              :                        "LLVM intrinsic %qs is not available for this target",
     117              :                        name.c_str ());
     118            0 :         return;
     119            0 :       case LlvmBuiltinMappingError::Kind::NOT_MAPPED:
     120            0 :         rust_sorry_at (loc, "LLVM intrinsic %qs is not supported at the moment",
     121              :                        name.c_str ());
     122            0 :         return;
     123              :       }
     124              :   }
     125              : 
     126              : private:
     127            0 :   LlvmBuiltinMappingError (Kind kind) : kind (kind) {}
     128              : };
     129              : 
     130              : enum class LlvmBuiltinAdapter
     131              : {
     132              :   OUTPUT_POINTER_VALUE_STATUS,
     133              :   OUTPUT_POINTER_STATUS_VALUE,
     134              :   FORWARD_ARGUMENTS,
     135              : };
     136              : 
     137       229426 : struct LlvmBuiltinMapping
     138              : {
     139              :   std::string gcc_name;
     140              :   LlvmBuiltinAdapter adapter;
     141              : };
     142              : 
     143              : class BuiltinsContext
     144              : {
     145              : public:
     146              :   static BuiltinsContext &get ();
     147              : 
     148              :   tl::optional<tree> lookup_simple_builtin (const std::string &name);
     149              : 
     150              :   tl::expected<std::pair<tree, LlvmBuiltinAdapter>, LlvmBuiltinMappingError>
     151              :   map_llvm_to_gcc_builtin (const std::string &name);
     152              : 
     153              :   void register_builtin (tree decl);
     154              : 
     155              : private:
     156              :   enum Type
     157              :   {
     158              : #define DEF_PRIMITIVE_TYPE(NAME, V) NAME,
     159              : #define DEF_FUNCTION_TYPE_0(NAME, R) NAME,
     160              : #define DEF_FUNCTION_TYPE_1(NAME, R, A1) NAME,
     161              : #define DEF_FUNCTION_TYPE_2(NAME, R, A1, A2) NAME,
     162              : #define DEF_FUNCTION_TYPE_3(NAME, R, A1, A2, A3) NAME,
     163              : #define DEF_FUNCTION_TYPE_4(NAME, R, A1, A2, A3, A4) NAME,
     164              : #define DEF_FUNCTION_TYPE_5(NAME, R, A1, A2, A3, A4, A5) NAME,
     165              : #define DEF_FUNCTION_TYPE_6(NAME, R, A1, A2, A3, A4, A5, A6) NAME,
     166              : #define DEF_FUNCTION_TYPE_7(NAME, R, A1, A2, A3, A4, A5, A6, A7) NAME,
     167              : #define DEF_FUNCTION_TYPE_8(NAME, R, A1, A2, A3, A4, A5, A6, A7, A8) NAME,
     168              : #define DEF_FUNCTION_TYPE_9(NAME, R, A1, A2, A3, A4, A5, A6, A7, A8, A9) NAME,
     169              : #define DEF_FUNCTION_TYPE_10(NAME, R, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10) \
     170              :   NAME,
     171              : #define DEF_FUNCTION_TYPE_11(NAME, R, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, \
     172              :                              A11)                                              \
     173              :   NAME,
     174              : #define DEF_FUNCTION_TYPE_VAR_0(NAME, R) NAME,
     175              : #define DEF_FUNCTION_TYPE_VAR_1(NAME, R, A1) NAME,
     176              : #define DEF_FUNCTION_TYPE_VAR_2(NAME, R, A1, A2) NAME,
     177              : #define DEF_FUNCTION_TYPE_VAR_3(NAME, R, A1, A2, A3) NAME,
     178              : #define DEF_FUNCTION_TYPE_VAR_4(NAME, R, A1, A2, A3, A4) NAME,
     179              : #define DEF_FUNCTION_TYPE_VAR_5(NAME, R, A1, A2, A3, A4, A5) NAME,
     180              : #define DEF_FUNCTION_TYPE_VAR_6(NAME, R, A1, A2, A3, A4, A5, A6) NAME,
     181              : #define DEF_FUNCTION_TYPE_VAR_7(NAME, R, A1, A2, A3, A4, A5, A6, A7) NAME,
     182              : #define DEF_FUNCTION_TYPE_VAR_11(NAME, R, A1, A2, A3, A4, A5, A6, A7, A8, A9,  \
     183              :                                  A10, A11)                                     \
     184              :   NAME,
     185              : #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
     186              : 
     187              : #include "builtin-types.def"
     188              : 
     189              : #undef DEF_PRIMITIVE_TYPE
     190              : #undef DEF_FUNCTION_TYPE_0
     191              : #undef DEF_FUNCTION_TYPE_1
     192              : #undef DEF_FUNCTION_TYPE_2
     193              : #undef DEF_FUNCTION_TYPE_3
     194              : #undef DEF_FUNCTION_TYPE_4
     195              : #undef DEF_FUNCTION_TYPE_5
     196              : #undef DEF_FUNCTION_TYPE_6
     197              : #undef DEF_FUNCTION_TYPE_7
     198              : #undef DEF_FUNCTION_TYPE_8
     199              : #undef DEF_FUNCTION_TYPE_9
     200              : #undef DEF_FUNCTION_TYPE_10
     201              : #undef DEF_FUNCTION_TYPE_11
     202              : #undef DEF_FUNCTION_TYPE_VAR_0
     203              : #undef DEF_FUNCTION_TYPE_VAR_1
     204              : #undef DEF_FUNCTION_TYPE_VAR_2
     205              : #undef DEF_FUNCTION_TYPE_VAR_3
     206              : #undef DEF_FUNCTION_TYPE_VAR_4
     207              : #undef DEF_FUNCTION_TYPE_VAR_5
     208              : #undef DEF_FUNCTION_TYPE_VAR_6
     209              : #undef DEF_FUNCTION_TYPE_VAR_7
     210              : #undef DEF_FUNCTION_TYPE_VAR_11
     211              : #undef DEF_POINTER_TYPE
     212              : 
     213              :     BT_LAST,
     214              :   };
     215              : 
     216              :   enum Attr
     217              :   {
     218              : #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
     219              : #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
     220              : #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
     221              : #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
     222              : #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
     223              : 
     224              : #include "builtin-attrs.def"
     225              : 
     226              : #undef DEF_ATTR_NULL_TREE
     227              : #undef DEF_ATTR_INT
     228              : #undef DEF_ATTR_STRING
     229              : #undef DEF_ATTR_IDENT
     230              : #undef DEF_ATTR_TREE_LIST
     231              : 
     232              :     ATTR_LAST,
     233              :   };
     234              : 
     235              :   enum class SetupState
     236              :   {
     237              :     UNINITIALIZED,
     238              :     INITIALIZING,
     239              :     READY,
     240              :   };
     241              : 
     242              :   SetupState setup_state;
     243              : 
     244              :   /**
     245              :    * All builtin types, as defined in `builtin-types.def`
     246              :    *
     247              :    * This array is filled by the `define_builtin_types` method, during the first
     248              :    * initialization of the `BuiltinsContext`
     249              :    */
     250              :   tree builtin_types[Type::BT_LAST + 1];
     251              : 
     252              :   /**
     253              :    * Similarly, this array contains all builtin attributes, as defined in
     254              :    * `builtin-attr.def`
     255              :    *
     256              :    * This array is filled by the `define_builtin_attributes` method, during the
     257              :    * first initialization of the `BuiltinsContext`
     258              :    */
     259              :   tree builtin_attributes[Attr::ATTR_LAST + 1];
     260              : 
     261              :   void define_function_type (Type def, Type ret, bool is_variadic, size_t n,
     262              :                              ...);
     263              :   void define_builtin_types ();
     264              :   void define_builtin_attributes ();
     265              :   void define_builtins ();
     266              : 
     267              :   void register_rust_mappings ();
     268              :   void register_llvm_to_gcc_builtin ();
     269              : 
     270              :   BuiltinsContext ();
     271              : 
     272              :   void setup_overflow_fns ();
     273              :   void setup_math_fns ();
     274              :   void setup_atomic_fns ();
     275              : 
     276              :   void setup ();
     277              : 
     278              :   tl::optional<tree> lookup_gcc_builtin (const std::string &name);
     279              : 
     280              :   // A mapping of the GCC built-ins exposed to GCC Rust.
     281              :   std::map<std::string, tree> builtin_functions;
     282              :   std::map<std::string, std::string> rust_intrinsic_to_gcc_builtin;
     283              :   std::map<std::string, LlvmBuiltinMapping> llvm_to_gcc_builtin;
     284              : };
     285              : 
     286              : } // namespace Compile
     287              : } // namespace Rust
     288              : 
     289              : #endif // RUST_BUILTINS_H
        

Generated by: LCOV version 2.5.0-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.