Line data Source code
1 : // Copyright (C) 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_INTRINSIC_HANDLERS_H
20 : #define RUST_INTRINSIC_HANDLERS_H
21 :
22 : #include "rust-compile-context.h"
23 : #include "rust-gcc.h"
24 : #include "rust-hir-map.h"
25 : #include "rust-hir-type-check.h"
26 : #include "rust-tyty.h"
27 : #include "tree.h"
28 :
29 : namespace Rust {
30 : namespace Compile {
31 :
32 : struct IntrinsicCtx
33 : {
34 : /**
35 : * Setup the intrinsic's context with the required base info. The various
36 : * members will then get filled out by the helper calls
37 : */
38 3969 : IntrinsicCtx (Context *ctx, TyTy::FnType *fntype, location_t loc)
39 3969 : : ctx (*ctx), fntype (*fntype), param_vars ({}), param_types ({}),
40 3969 : fn (error_mark_node), loc (loc)
41 3969 : {}
42 :
43 : Context &ctx;
44 :
45 : // FIXME: Can we be in a situation where the given fntype is null and this is
46 : // a null deref?
47 : TyTy::FnType &fntype;
48 :
49 : std::vector<Bvariable *> param_vars;
50 : std::vector<tree_node *> param_types;
51 :
52 : std::vector<Bvariable *> block_variables = {};
53 :
54 : tree fn;
55 : location_t loc;
56 :
57 5107 : void add_statement (tree stmt) { ctx.add_statement (stmt); }
58 :
59 1645 : Analysis::Mappings &get_mappings () { return ctx.get_mappings (); }
60 190 : Resolver::TypeCheckContext *get_tyctx () { return ctx.get_tyctx (); }
61 :
62 : /**
63 : * Some functions require a pointer on the original inner compilation context
64 : */
65 2068 : Context *inner () const { return &ctx; }
66 :
67 : /**
68 : * Implementation details for the common intrinsic compilation logic. These
69 : * are called by Intrinsic::compile.
70 : */
71 :
72 : /**
73 : * Items can be forward compiled which means we may not need to invoke this
74 : * code. We might also have already compiled this generic function as well.
75 : */
76 : tl::optional<tree> check_for_cached_intrinsic ();
77 :
78 : /**
79 : * Maybe override the Hir Lookups for the substitutions in this context
80 : */
81 : void maybe_override_ctx ();
82 :
83 : /**
84 : * Compile the function proper to TREE
85 : */
86 : void compile_intrinsic_function ();
87 :
88 : /**
89 : * Compile and setup a function's parameters
90 : */
91 : void compile_fn_params ();
92 :
93 : /**
94 : * Set the variables for the intrinsic block to use
95 : */
96 289 : void set_block_variables (std::vector<Bvariable *> &&new_vars)
97 : {
98 289 : block_variables = std::move (new_vars);
99 : }
100 :
101 : void enter_intrinsic_block ();
102 : void finalize_intrinsic_block ();
103 : };
104 :
105 3969 : class Intrinsic
106 : {
107 : public:
108 : using CompileFn = std::function<void (IntrinsicCtx &)>;
109 :
110 3969 : Intrinsic (CompileFn closure) : closure (closure) {}
111 :
112 : tree compile (Context *ctx, TyTy::FnType *fntype, location_t loc);
113 :
114 : private:
115 : CompileFn closure;
116 : };
117 :
118 : enum class Mode
119 : {
120 : Read,
121 : Write
122 : };
123 :
124 : enum class Cache
125 : {
126 : Data,
127 : Instruction,
128 : };
129 :
130 : enum class TyAssert
131 : {
132 : ZeroValid,
133 : Inhabited,
134 : UninitValid,
135 : };
136 :
137 : namespace handlers {
138 :
139 : enum class AtomicMinMaxKind
140 : {
141 : SignedMax,
142 : SignedMin,
143 : UnsignedMax,
144 : UnsignedMin,
145 : };
146 :
147 : void rotate_left (IntrinsicCtx &ctx);
148 : void rotate_right (IntrinsicCtx &ctx);
149 :
150 : void offset (IntrinsicCtx &ctx);
151 : void sizeof_handler (IntrinsicCtx &ctx);
152 : void size_of_val_handler (IntrinsicCtx &ctx);
153 : void min_align_of_handler (IntrinsicCtx &ctx);
154 : void min_align_of_val_handler (IntrinsicCtx &ctx);
155 : void transmute (IntrinsicCtx &ctx);
156 :
157 : // FIXME: This is supposed to be just a ctx without the op right?
158 : // Or are these inner functions and thus shouldn't live here?
159 : void rotate (IntrinsicCtx &ctx, tree_code op);
160 :
161 : void uninit (IntrinsicCtx &ctx);
162 : void move_val_init (IntrinsicCtx &ctx);
163 : void assume (IntrinsicCtx &ctx);
164 : void discriminant_value (IntrinsicCtx &ctx);
165 : void variant_count (IntrinsicCtx &ctx);
166 : void bswap_handler (IntrinsicCtx &ctx);
167 : void ctlz_handler (IntrinsicCtx &ctx);
168 : void ctlz_nonzero_handler (IntrinsicCtx &ctx);
169 : void cttz_handler (IntrinsicCtx &ctx);
170 : void cttz_nonzero_handler (IntrinsicCtx &ctx);
171 :
172 : void sorry (IntrinsicCtx &ctx);
173 :
174 : void float_to_int_unchecked (IntrinsicCtx &ctx);
175 :
176 : void write_bytes_handler (IntrinsicCtx &ctx);
177 : void arith_offset_handler (IntrinsicCtx &ctx);
178 : void assert_zero_valid_handler (IntrinsicCtx &ctx);
179 :
180 : void exact_div_handler (IntrinsicCtx &ctx);
181 :
182 : void nontemporal_store (IntrinsicCtx &ctx);
183 :
184 : Intrinsic::CompileFn op_with_overflow (tree_code op);
185 :
186 : Intrinsic::CompileFn wrapping_op (tree_code op);
187 :
188 : Intrinsic::CompileFn atomic_store (int ordering);
189 : Intrinsic::CompileFn atomic_load (int ordering);
190 :
191 : Intrinsic::CompileFn atomic_xadd (int ordering);
192 : Intrinsic::CompileFn atomic_xsub (int ordering);
193 : Intrinsic::CompileFn atomic_and (int ordering);
194 : Intrinsic::CompileFn atomic_nand (int ordering);
195 : Intrinsic::CompileFn atomic_or (int ordering);
196 : Intrinsic::CompileFn atomic_xor (int ordering);
197 : Intrinsic::CompileFn atomic_compare_exchange (bool weak, int success_ordering,
198 : int failure_ordering);
199 : Intrinsic::CompileFn atomic_minmax (handlers::AtomicMinMaxKind kind,
200 : int ordering);
201 : Intrinsic::CompileFn atomic_fence (bool is_multithread, int ordering);
202 :
203 : Intrinsic::CompileFn unchecked_op (tree_code op);
204 :
205 : Intrinsic::CompileFn copy (bool overlaps);
206 : Intrinsic::CompileFn expect (bool likely);
207 : Intrinsic::CompileFn try_handler (bool is_new_api);
208 :
209 : Intrinsic::CompileFn prefetch (Mode mode, Cache cache);
210 :
211 : /**
212 : * For fadd_fast, fsub_fast, fmul_fast, fdiv_fast and frem_fast
213 : */
214 : Intrinsic::CompileFn fop_fast (ArithmeticOrLogicalOperator op);
215 :
216 : /**
217 : * For assert_zero_valid, assert_inhabited and assert_maybe_uninit.
218 : * This is how rustc does it as well since a lot of the code is reused.
219 : */
220 : Intrinsic::CompileFn type_assert (TyAssert assert);
221 :
222 : void breakpoint (IntrinsicCtx &ctx);
223 :
224 : void ptr_offset_from (IntrinsicCtx &ctx);
225 :
226 : } // namespace handlers
227 :
228 : } // namespace Compile
229 : } // namespace Rust
230 :
231 : #endif /* ! RUST_INTRINSIC_HANDLERS_H */
|