Line data Source code
1 : // This file is part of GCC.
2 :
3 : // GCC is free software; you can redistribute it and/or modify it under
4 : // the terms of the GNU General Public License as published by the Free
5 : // Software Foundation; either version 3, or (at your option) any later
6 : // version.
7 :
8 : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
9 : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 : // for more details.
12 :
13 : // You should have received a copy of the GNU General Public License
14 : // along with GCC; see the file COPYING3. If not see
15 : // <http://www.gnu.org/licenses/>.
16 :
17 : #include "rust-compile-intrinsic.h"
18 : #include "rust-compile-context.h"
19 : #include "rust-builtins.h"
20 : #include "rust-diagnostics.h"
21 : #include "tree-core.h"
22 : #include "rust-intrinsic-handlers.h"
23 : #include "rust-intrinsic-values.h"
24 :
25 : namespace Rust {
26 : namespace Compile {
27 :
28 : using IValue = Values::Intrinsics;
29 :
30 : static const std::map<std::string, handlers::HandlerBuilder> generic_intrinsics
31 : = {{IValue::OFFSET, handlers::offset},
32 : {IValue::SIZE_OF, handlers::sizeof_handler},
33 : {IValue::TRANSMUTE, handlers::transmute},
34 : {IValue::ROTATE_LEFT, handlers::rotate_left},
35 : {IValue::ROTATE_RIGHT, handlers::rotate_right},
36 : {IValue::WRAPPING_ADD, handlers::wrapping_op (PLUS_EXPR)},
37 : {IValue::WRAPPING_SUB, handlers::wrapping_op (MINUS_EXPR)},
38 : {IValue::WRAPPING_MUL, handlers::wrapping_op (MULT_EXPR)},
39 : {IValue::ADD_WITH_OVERFLOW, handlers::op_with_overflow (PLUS_EXPR)},
40 : {IValue::SUB_WITH_OVERFLOW, handlers::op_with_overflow (MINUS_EXPR)},
41 : {IValue::MUL_WITH_OVERFLOW, handlers::op_with_overflow (MULT_EXPR)},
42 : {IValue::COPY, handlers::copy (true)},
43 : {IValue::COPY_NONOVERLAPPING, handlers::copy (false)},
44 : {IValue::PREFETCH_READ_DATA, handlers::prefetch_read_data},
45 : {IValue::PREFETCH_WRITE_DATA, handlers::prefetch_write_data},
46 : {IValue::ATOMIC_STORE_SEQCST, handlers::atomic_store (__ATOMIC_SEQ_CST)},
47 : {IValue::ATOMIC_STORE_RELEASE, handlers::atomic_store (__ATOMIC_RELEASE)},
48 : {IValue::ATOMIC_STORE_RELAXED, handlers::atomic_store (__ATOMIC_RELAXED)},
49 : {IValue::ATOMIC_STORE_UNORDERED,
50 : handlers::atomic_store (__ATOMIC_RELAXED)},
51 : {IValue::ATOMIC_LOAD_SEQCST, handlers::atomic_load (__ATOMIC_SEQ_CST)},
52 : {IValue::ATOMIC_LOAD_ACQUIRE, handlers::atomic_load (__ATOMIC_ACQUIRE)},
53 : {IValue::ATOMIC_LOAD_RELAXED, handlers::atomic_load (__ATOMIC_RELAXED)},
54 : {IValue::ATOMIC_LOAD_UNORDERED, handlers::atomic_load (__ATOMIC_RELAXED)},
55 : {IValue::UNCHECKED_ADD, handlers::unchecked_op (PLUS_EXPR)},
56 : {IValue::UNCHECKED_SUB, handlers::unchecked_op (MINUS_EXPR)},
57 : {IValue::UNCHECKED_MUL, handlers::unchecked_op (MULT_EXPR)},
58 : {IValue::UNCHECKED_DIV, handlers::unchecked_op (TRUNC_DIV_EXPR)},
59 : {IValue::UNCHECKED_REM, handlers::unchecked_op (TRUNC_MOD_EXPR)},
60 : {IValue::UNCHECKED_SHL, handlers::unchecked_op (LSHIFT_EXPR)},
61 : {IValue::UNCHECKED_SHR, handlers::unchecked_op (RSHIFT_EXPR)},
62 : {IValue::UNINIT, handlers::uninit},
63 : {IValue::MOVE_VAL_INIT, handlers::move_val_init},
64 : {IValue::LIKELY, handlers::expect (true)},
65 : {IValue::UNLIKELY, handlers::expect (false)},
66 : {IValue::ASSUME, handlers::assume},
67 : {IValue::TRY, handlers::try_handler (false)},
68 : {IValue::CATCH_UNWIND, handlers::try_handler (true)},
69 : {IValue::DISCRIMINANT_VALUE, handlers::discriminant_value},
70 : {IValue::VARIANT_COUNT, handlers::variant_count},
71 : {IValue::BSWAP, handlers::bswap_handler},
72 : {IValue::CTLZ, handlers::ctlz_handler},
73 : {IValue::CTLZ_NONZERO, handlers::ctlz_nonzero_handler},
74 : {IValue::CTTZ, handlers::cttz_handler},
75 : {IValue::CTTZ_NONZERO, handlers::cttz_nonzero_handler}};
76 :
77 3351 : Intrinsics::Intrinsics (Context *ctx) : ctx (ctx) {}
78 :
79 : /**
80 : * Returns a FUNC_DECL corresponding to the intrinsic function FNTYPE. If a
81 : * corresponding builtin exists, returns it. If not, search in the generic
82 : * intrinsics declared and delegate the return to the corresponding handler.
83 : *
84 : * @param fntype The Rust function type that should be implemented by the
85 : * compiler
86 : */
87 : tree
88 3351 : Intrinsics::compile (TyTy::FnType *fntype)
89 : {
90 3351 : rust_assert (fntype->get_abi () == ABI::INTRINSIC);
91 :
92 3351 : tree builtin = error_mark_node;
93 3351 : BuiltinsContext &builtin_ctx = BuiltinsContext::get ();
94 :
95 6702 : if (builtin_ctx.lookup_simple_builtin (fntype->get_identifier (), &builtin))
96 449 : return builtin;
97 :
98 : // is it an generic builtin?
99 5804 : auto it = generic_intrinsics.find (fntype->get_identifier ());
100 2902 : if (it != generic_intrinsics.end ())
101 2895 : return it->second (ctx, fntype);
102 :
103 7 : location_t locus = ctx->get_mappings ().lookup_location (fntype->get_ref ());
104 14 : rust_error_at (locus, ErrorCode::E0093,
105 : "unrecognized intrinsic function: %qs",
106 7 : fntype->get_identifier ().c_str ());
107 :
108 7 : return error_mark_node;
109 : }
110 :
111 : } // namespace Compile
112 : } // namespace Rust
|