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 : #include "rust-intrinsic-handlers.h"
20 : #include "optional.h"
21 : #include "rust-backend.h"
22 : #include "rust-compile-context.h"
23 : #include "rust-compile-item.h"
24 : #include "rust-compile-type.h"
25 : #include "rust-compile-fnparam.h"
26 : #include "rust-builtins.h"
27 : #include "rust-diagnostics.h"
28 : #include "rust-location.h"
29 : #include "rust-constexpr.h"
30 : #include "rust-operators.h"
31 : #include "rust-session-manager.h"
32 : #include "rust-tree.h"
33 : #include "rust-tyty.h"
34 : #include "tree-core.h"
35 : #include "rust-gcc.h"
36 : #include "fold-const.h"
37 : #include "rust-constexpr.h"
38 : #include "rust-hir-item.h"
39 : #include "tree.h"
40 :
41 : // declaration taken from "stringpool.h"
42 : // the get_identifier macro causes compilation issues
43 : extern tree get_identifier (const char *);
44 :
45 : namespace Rust {
46 : namespace Compile {
47 :
48 : static tree
49 44 : lang_item_call (IntrinsicCtx &ctx, LangItem::Kind lang_item,
50 : const std::vector<tree> &args)
51 : {
52 44 : auto defid = ctx.get_mappings ().lookup_lang_item (lang_item);
53 44 : rust_assert (defid);
54 :
55 44 : auto lang_item_def = ctx.get_mappings ().lookup_defid (*defid);
56 44 : rust_assert (lang_item_def);
57 :
58 : // Let's make sure we're not calling a lang item that's a type...
59 44 : rust_assert (lang_item_def.value ()->get_item_kind ()
60 : == HIR::Item::ItemKind::Function);
61 :
62 : // Compile the lang item function
63 44 : auto fn = CompileItem::compile (*lang_item_def, &ctx.ctx);
64 :
65 44 : return Backend::call_expression (fn, args, nullptr, ctx.loc);
66 : }
67 :
68 : tree
69 3969 : Intrinsic::compile (Context *ctx, TyTy::FnType *fntype, location_t loc)
70 : {
71 3969 : auto ictx = IntrinsicCtx (ctx, fntype, loc);
72 :
73 : // Check for a cached version of the intrinsic
74 3969 : if (auto cached = ictx.check_for_cached_intrinsic ())
75 0 : return *cached;
76 :
77 : // Otherwise compile the fntype
78 3969 : ictx.compile_intrinsic_function ();
79 :
80 : // Compile its given parameters and type parameters properly
81 3969 : ictx.compile_fn_params ();
82 :
83 : // And enter its block
84 3969 : ictx.enter_intrinsic_block ();
85 :
86 : // Do the inner, intrinsic specific compilation logic
87 3969 : closure (ictx);
88 :
89 : // ...and exit its block
90 3969 : ictx.finalize_intrinsic_block ();
91 :
92 : // Once everything has been done and the intrinsic's low level compilation
93 : // logic is done, we return the compiled TREE object for the function
94 3969 : return ictx.fn;
95 3969 : }
96 :
97 : static tree
98 970 : make_unsigned_long_tree (unsigned long value)
99 : {
100 16 : return build_int_cst (integer_type_node, value);
101 : }
102 :
103 : static bool
104 1557 : is_basic_integer_type (TyTy::BaseType *type)
105 : {
106 1557 : switch (type->get_kind ())
107 : {
108 : case TyTy::INT:
109 : case TyTy::UINT:
110 : case TyTy::USIZE:
111 : case TyTy::ISIZE:
112 : return true;
113 28 : default:
114 28 : return false;
115 : break;
116 : }
117 : }
118 :
119 : void
120 3969 : IntrinsicCtx::maybe_override_ctx ()
121 : {
122 3969 : if (fntype.has_substitutions_defined ())
123 3951 : fntype.override_context ();
124 3969 : }
125 :
126 : static bool
127 1557 : check_for_basic_integer_type (const std::string &intrinsic_str,
128 : location_t locus, TyTy::BaseType *type)
129 : {
130 1557 : auto is_basic_integer = is_basic_integer_type (type);
131 1557 : if (!is_basic_integer)
132 : {
133 28 : rust_error_at (
134 : locus,
135 : "%s intrinsic can only be used with basic integer types (got %qs)",
136 56 : intrinsic_str.c_str (), type->get_name ().c_str ());
137 : }
138 :
139 1557 : return is_basic_integer;
140 : }
141 :
142 : tl::optional<tree>
143 3969 : IntrinsicCtx::check_for_cached_intrinsic ()
144 : {
145 3969 : const Resolver::CanonicalPath &canonical_path = fntype.get_ident ().path;
146 3969 : std::string asm_name = ctx.mangle_item (&fntype, canonical_path);
147 :
148 3969 : tree lookup = NULL_TREE;
149 :
150 3969 : if (ctx.lookup_function_decl (fntype.get_ty_ref (), &lookup, fntype.get_id (),
151 3969 : &fntype, asm_name))
152 0 : return lookup;
153 :
154 3969 : return tl::nullopt;
155 3969 : }
156 :
157 : void
158 3969 : IntrinsicCtx::compile_intrinsic_function ()
159 : {
160 3969 : maybe_override_ctx ();
161 :
162 3969 : const Resolver::CanonicalPath &canonical_path = fntype.get_ident ().path;
163 :
164 3969 : tree compiled_fn_type = TyTyResolveCompile::compile (&ctx, &fntype);
165 3969 : std::string ir_symbol_name
166 3969 : = canonical_path.get () + fntype.subst_as_string ();
167 3969 : std::string asm_name = ctx.mangle_item (&fntype, canonical_path);
168 :
169 3969 : unsigned int flags = 0;
170 :
171 : // store the function directly in this context's fn member
172 3969 : fn = Backend::function (compiled_fn_type, ir_symbol_name, asm_name, flags,
173 3969 : fntype.get_ident ().locus);
174 :
175 3969 : TREE_PUBLIC (fn) = 0;
176 3969 : TREE_READONLY (fn) = 1;
177 3969 : DECL_ARTIFICIAL (fn) = 1;
178 3969 : DECL_EXTERNAL (fn) = 0;
179 3969 : DECL_DECLARED_INLINE_P (fn) = 1;
180 3969 : }
181 :
182 : void
183 3969 : IntrinsicCtx::compile_fn_params ()
184 : {
185 9676 : for (auto &parm : fntype.get_params ())
186 : {
187 5707 : auto &referenced_param = parm.get_pattern ();
188 5707 : location_t param_locus = referenced_param.get_locus ();
189 :
190 5707 : auto param_tyty = parm.get_type ();
191 :
192 5707 : auto compiled_param_type = TyTyResolveCompile::compile (&ctx, param_tyty);
193 5707 : Bvariable *compiled_param_var
194 5707 : = CompileFnParam::compile (&ctx, fn, referenced_param,
195 5707 : compiled_param_type, param_locus);
196 :
197 5707 : param_vars.push_back (compiled_param_var);
198 5707 : param_types.push_back (compiled_param_type);
199 : }
200 :
201 3969 : auto ok = Backend::function_set_parameters (fn, param_vars);
202 :
203 3969 : rust_assert (ok);
204 3969 : }
205 :
206 : void
207 3969 : IntrinsicCtx::enter_intrinsic_block ()
208 : {
209 3969 : tree enclosing_scope = NULL_TREE;
210 3969 : location_t start_location = UNDEF_LOCATION;
211 3969 : location_t end_location = UNDEF_LOCATION;
212 :
213 3969 : auto block = Backend::block (fn, enclosing_scope, block_variables,
214 : start_location, end_location);
215 :
216 3969 : ctx.push_block (block);
217 3969 : }
218 :
219 : void
220 3969 : IntrinsicCtx::finalize_intrinsic_block ()
221 : {
222 3969 : tree bind_tree = ctx.pop_block ();
223 :
224 3969 : gcc_assert (TREE_CODE (bind_tree) == BIND_EXPR);
225 :
226 3969 : DECL_SAVED_TREE (fn) = bind_tree;
227 :
228 3969 : ctx.push_function (fn);
229 :
230 3969 : DECL_DECLARED_CONSTEXPR_P (fn) = 1;
231 3969 : maybe_save_constexpr_fundef (fn);
232 3969 : }
233 :
234 : static TyTy::BaseType *
235 12 : get_inner_dst (TyTy::BaseType *type)
236 : {
237 12 : TyTy::BaseType *curr = type;
238 16 : while (curr->get_kind () == TyTy::TypeKind::ADT)
239 : {
240 4 : auto variant = curr->as<TyTy::ADTType> ()->get_variants ().front ();
241 4 : curr = variant->get_field_at_index (variant->num_fields () - 1)
242 4 : ->get_field_type ();
243 : }
244 12 : return curr;
245 : }
246 :
247 : namespace handlers {
248 : namespace inner {
249 :
250 : static tree
251 786 : resolve_atomic_builtin (IntrinsicCtx &ctx, const std::string &prefix)
252 : {
253 786 : auto locus = ctx.get_mappings ().lookup_location (ctx.fntype.get_ref ());
254 786 : auto operand_type = ctx.fntype.get_substs ()[0].get_param_ty ()->resolve ();
255 :
256 786 : if (!check_for_basic_integer_type ("atomic operation", locus, operand_type))
257 16 : return error_mark_node;
258 :
259 770 : tree operand_type_tree
260 770 : = TyTyResolveCompile::compile (ctx.inner (), operand_type);
261 770 : if (operand_type_tree == error_mark_node)
262 : return error_mark_node;
263 :
264 770 : operand_type_tree = TYPE_MAIN_VARIANT (operand_type_tree);
265 770 : if (TREE_CODE (operand_type_tree) != INTEGER_TYPE)
266 : {
267 0 : rust_error_at (locus, "atomic operation requires an integer type");
268 0 : return error_mark_node;
269 : }
270 :
271 770 : HOST_WIDE_INT bytes = int_size_in_bytes (operand_type_tree);
272 :
273 1540 : std::string builtin_name = "__" + prefix + "_" + std::to_string (bytes);
274 :
275 770 : auto decl = BuiltinsContext::get ().lookup_simple_builtin (builtin_name);
276 770 : rust_assert (decl);
277 :
278 770 : return decl.value ();
279 770 : }
280 :
281 : static void
282 58 : unchecked_op (IntrinsicCtx &ctx, tree_code op)
283 : {
284 58 : auto x = Backend::var_expression (ctx.param_vars[0], UNDEF_LOCATION);
285 58 : auto y = Backend::var_expression (ctx.param_vars[1], UNDEF_LOCATION);
286 :
287 58 : auto *monomorphized_type
288 58 : = ctx.fntype.get_substs ().at (0).get_param_ty ()->resolve ();
289 :
290 58 : auto call_locus = ctx.get_mappings ().lookup_location (ctx.fntype.get_ref ());
291 58 : check_for_basic_integer_type ("unchecked operation", call_locus,
292 : monomorphized_type);
293 :
294 58 : auto expr = build2 (op, TREE_TYPE (x), x, y);
295 58 : auto return_statement
296 58 : = Backend::return_statement (ctx.fn, expr, UNDEF_LOCATION);
297 :
298 58 : ctx.add_statement (return_statement);
299 58 : }
300 :
301 : static void
302 0 : expect (IntrinsicCtx &ctx, bool likely)
303 : {
304 0 : tree expr = Backend::var_expression (ctx.param_vars[0], UNDEF_LOCATION);
305 0 : auto expect_fn_raw
306 0 : = BuiltinsContext::get ().lookup_simple_builtin ("__builtin_expect");
307 0 : rust_assert (expect_fn_raw);
308 :
309 0 : auto expect_fn = build_fold_addr_expr_loc (BUILTINS_LOCATION, *expect_fn_raw);
310 :
311 : // we need to convert the expression return type to long to match the expected
312 : // parameter type of __builtin_expect
313 0 : auto expect_src = build1 (CONVERT_EXPR, long_integer_type_node, expr);
314 0 : auto expect_value
315 0 : = make_unsigned_long_tree (static_cast<unsigned long> (likely));
316 :
317 0 : auto expect_call
318 0 : = Backend::call_expression (expect_fn, {expect_src, expect_value}, nullptr,
319 : BUILTINS_LOCATION);
320 : // the return value also needs to be casted (to bool)
321 0 : auto expect_call_bool = build1 (CONVERT_EXPR, boolean_type_node, expect_call);
322 0 : auto return_statement
323 0 : = Backend::return_statement (ctx.fn, expect_call_bool, BUILTINS_LOCATION);
324 0 : ctx.add_statement (return_statement);
325 0 : }
326 :
327 : static void
328 2 : try_handler (IntrinsicCtx &ctx, bool is_new_api)
329 : {
330 : // The following tricks are needed to make sure the try-catch blocks are not
331 : // optimized away
332 2 : TREE_READONLY (ctx.fn) = 0;
333 2 : DECL_DISREGARD_INLINE_LIMITS (ctx.fn) = 1;
334 2 : DECL_ATTRIBUTES (ctx.fn) = tree_cons (get_identifier ("always_inline"),
335 2 : NULL_TREE, DECL_ATTRIBUTES (ctx.fn));
336 :
337 2 : tree enclosing_scope = NULL_TREE;
338 :
339 2 : bool panic_is_abort = Session::get_instance ().options.get_panic_strategy ()
340 2 : == CompileOptions::PanicStrategy::Abort;
341 2 : tree try_fn = Backend::var_expression (ctx.param_vars[0], UNDEF_LOCATION);
342 2 : tree user_data = Backend::var_expression (ctx.param_vars[1], UNDEF_LOCATION);
343 2 : tree catch_fn = Backend::var_expression (ctx.param_vars[2], UNDEF_LOCATION);
344 2 : tree normal_return_stmt = NULL_TREE;
345 2 : tree error_return_stmt = NULL_TREE;
346 2 : tree try_call = Backend::call_expression (try_fn, {user_data}, nullptr,
347 : BUILTINS_LOCATION);
348 2 : tree catch_call = NULL_TREE;
349 2 : tree try_block = Backend::block (ctx.fn, enclosing_scope, {}, UNDEF_LOCATION,
350 : UNDEF_LOCATION);
351 :
352 2 : if (is_new_api)
353 : {
354 1 : auto ret_type = TyTyResolveCompile::get_unit_type (ctx.inner ());
355 1 : auto ret_expr = Backend::constructor_expression (ret_type, false, {}, -1,
356 : UNDEF_LOCATION);
357 1 : normal_return_stmt
358 1 : = Backend::return_statement (ctx.fn, ret_expr, BUILTINS_LOCATION);
359 1 : error_return_stmt
360 1 : = Backend::return_statement (ctx.fn, ret_expr, BUILTINS_LOCATION);
361 : }
362 : else
363 : {
364 1 : normal_return_stmt = Backend::return_statement (ctx.fn, integer_zero_node,
365 : BUILTINS_LOCATION);
366 1 : error_return_stmt = Backend::return_statement (ctx.fn, integer_one_node,
367 : BUILTINS_LOCATION);
368 : }
369 2 : Backend::block_add_statements (try_block,
370 2 : std::vector<tree>{try_call,
371 2 : normal_return_stmt});
372 2 : if (panic_is_abort)
373 : {
374 : // skip building the try-catch construct
375 0 : ctx.add_statement (try_block);
376 :
377 0 : return;
378 : }
379 :
380 2 : tree eh_pointer
381 2 : = build_call_expr (builtin_decl_explicit (BUILT_IN_EH_POINTER), 1,
382 : integer_zero_node);
383 2 : catch_call = Backend::call_expression (catch_fn, {user_data, eh_pointer},
384 : NULL_TREE, BUILTINS_LOCATION);
385 :
386 2 : tree catch_block = Backend::block (ctx.fn, enclosing_scope, {},
387 : UNDEF_LOCATION, UNDEF_LOCATION);
388 2 : Backend::block_add_statements (catch_block,
389 2 : std::vector<tree>{catch_call,
390 2 : error_return_stmt});
391 : // emulate what cc1plus is doing for C++ try-catch
392 2 : tree inner_eh_construct
393 2 : = Backend::exception_handler_statement (catch_call, NULL_TREE,
394 : error_return_stmt,
395 : BUILTINS_LOCATION);
396 : // TODO(liushuyu): eh_personality needs to be implemented as a runtime thing
397 2 : auto eh_construct
398 2 : = Backend::exception_handler_statement (try_block, inner_eh_construct,
399 : NULL_TREE, BUILTINS_LOCATION);
400 2 : ctx.add_statement (eh_construct);
401 : }
402 :
403 : /**
404 : * pub fn wrapping_{add, sub, mul}<T>(lhs: T, rhs: T) -> T;
405 : */
406 : static void
407 380 : wrapping_op (IntrinsicCtx &ctx, tree_code op)
408 : {
409 : // wrapping_<op> intrinsics have two parameter
410 :
411 380 : auto &lhs_param = ctx.param_vars.at (0);
412 380 : auto &rhs_param = ctx.param_vars.at (1);
413 :
414 380 : auto lhs = Backend::var_expression (lhs_param, UNDEF_LOCATION);
415 380 : auto rhs = Backend::var_expression (rhs_param, UNDEF_LOCATION);
416 :
417 : // Operations are always wrapping in Rust, as we have -fwrapv enabled by
418 : // default. The difference between a wrapping_{add, sub, mul} and a regular
419 : // arithmetic operation is that these intrinsics do not panic - they always
420 : // carry over.
421 380 : auto wrap_expr = build2 (op, TREE_TYPE (lhs), lhs, rhs);
422 :
423 380 : auto return_statement
424 380 : = Backend::return_statement (ctx.fn, wrap_expr, UNDEF_LOCATION);
425 380 : ctx.add_statement (return_statement);
426 380 : }
427 :
428 : /**
429 : * pub fn add_with_overflow<T>(x: T, y: T) -> (T, bool);
430 : */
431 : static void
432 176 : op_with_overflow (IntrinsicCtx &ctx, tree_code op)
433 : {
434 : // wrapping_<op> intrinsics have two parameter
435 :
436 176 : auto &x_param = ctx.param_vars.at (0);
437 176 : auto &y_param = ctx.param_vars.at (1);
438 :
439 176 : auto ¶m_mapping = ctx.fntype.get_substs ().at (0);
440 176 : const auto param_tyty = param_mapping.get_param_ty ();
441 176 : auto resolved_tyty = param_tyty->resolve ();
442 176 : tree template_parameter_type
443 176 : = TyTyResolveCompile::compile (ctx.inner (), resolved_tyty);
444 :
445 : // this should match y as well or we can take it from the TyTy structure
446 176 : tree tmp_stmt = error_mark_node;
447 176 : Bvariable *result_variable
448 176 : = Backend::temporary_variable (ctx.fn, NULL_TREE, template_parameter_type,
449 : NULL_TREE, true /*address_is_taken*/,
450 : UNDEF_LOCATION, &tmp_stmt);
451 176 : Bvariable *bool_variable
452 176 : = Backend::temporary_variable (ctx.fn, NULL_TREE, boolean_type_node,
453 : NULL_TREE, true /*address_is_taken*/,
454 : UNDEF_LOCATION, &tmp_stmt);
455 :
456 176 : ctx.set_block_variables ({result_variable, bool_variable});
457 :
458 176 : auto x = Backend::var_expression (x_param, UNDEF_LOCATION);
459 176 : auto y = Backend::var_expression (y_param, UNDEF_LOCATION);
460 :
461 176 : tl::optional<tree> overflow_builtin = tl::nullopt;
462 176 : auto &builtins = BuiltinsContext::get ();
463 :
464 176 : switch (op)
465 : {
466 162 : case PLUS_EXPR:
467 162 : overflow_builtin
468 162 : = builtins.lookup_simple_builtin ("__builtin_add_overflow");
469 162 : break;
470 :
471 7 : case MINUS_EXPR:
472 7 : overflow_builtin
473 7 : = builtins.lookup_simple_builtin ("__builtin_sub_overflow");
474 7 : break;
475 :
476 7 : case MULT_EXPR:
477 7 : overflow_builtin
478 7 : = builtins.lookup_simple_builtin ("__builtin_mul_overflow");
479 7 : break;
480 :
481 0 : default:
482 0 : rust_unreachable ();
483 176 : break;
484 : }
485 176 : rust_assert (overflow_builtin);
486 :
487 176 : tree bool_decl = bool_variable->get_tree (BUILTINS_LOCATION);
488 176 : tree result_decl = result_variable->get_tree (BUILTINS_LOCATION);
489 176 : tree result_ref = build_fold_addr_expr_loc (BUILTINS_LOCATION, result_decl);
490 :
491 176 : tree builtin_call = build_call_expr_loc (BUILTINS_LOCATION, *overflow_builtin,
492 : 3, x, y, result_ref);
493 :
494 176 : tree overflow_assignment
495 176 : = Backend::assignment_statement (bool_decl, builtin_call,
496 : BUILTINS_LOCATION);
497 :
498 176 : ctx.add_statement (overflow_assignment);
499 :
500 176 : std::vector<tree> vals = {result_decl, bool_decl};
501 176 : tree tuple_type = TREE_TYPE (DECL_RESULT (ctx.fn));
502 176 : tree result_expr = Backend::constructor_expression (tuple_type, false, vals,
503 : -1, UNDEF_LOCATION);
504 :
505 176 : auto return_statement
506 176 : = Backend::return_statement (ctx.fn, result_expr, UNDEF_LOCATION);
507 176 : ctx.add_statement (return_statement);
508 176 : }
509 :
510 : /**
511 : * fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
512 : * fn copy<T>(src: *const T, dst: *mut T, count: usize);
513 : */
514 : static void
515 299 : copy (IntrinsicCtx &ctx, bool overlaps)
516 : {
517 : // Most intrinsic functions are pure - not `copy_nonoverlapping` and `copy`
518 299 : TREE_READONLY (ctx.fn) = 0;
519 299 : TREE_SIDE_EFFECTS (ctx.fn) = 1;
520 :
521 299 : auto src = Backend::var_expression (ctx.param_vars[0], UNDEF_LOCATION);
522 299 : auto dst = Backend::var_expression (ctx.param_vars[1], UNDEF_LOCATION);
523 299 : auto count = Backend::var_expression (ctx.param_vars[2], UNDEF_LOCATION);
524 :
525 : // We want to create the following statement
526 : // memcpy(dst, src, size_of::<T>());
527 : // so
528 : // memcpy(dst, src, size_expr);
529 :
530 299 : auto *resolved_ty
531 299 : = ctx.fntype.get_substs ().at (0).get_param_ty ()->resolve ();
532 299 : auto param_type = TyTyResolveCompile::compile (ctx.inner (), resolved_ty);
533 :
534 299 : tree size_expr
535 299 : = build2 (MULT_EXPR, size_type_node, TYPE_SIZE_UNIT (param_type), count);
536 :
537 598 : auto memcpy_raw = BuiltinsContext::get ().lookup_simple_builtin (
538 598 : overlaps ? "__builtin_memmove" : "__builtin_memcpy");
539 299 : rust_assert (memcpy_raw);
540 :
541 299 : auto memcpy = build_fold_addr_expr_loc (UNKNOWN_LOCATION, *memcpy_raw);
542 :
543 299 : auto copy_call = Backend::call_expression (memcpy, {dst, src, size_expr},
544 : nullptr, UNDEF_LOCATION);
545 :
546 299 : ctx.add_statement (copy_call);
547 299 : }
548 :
549 : // Compiles atomic_store that has the signature (*mut T, T) -> ()
550 : static void
551 70 : atomic_store (IntrinsicCtx &ctx, int ordering)
552 : {
553 : // Most intrinsic functions are pure but not the atomic ones
554 70 : TREE_READONLY (ctx.fn) = 0;
555 70 : TREE_SIDE_EFFECTS (ctx.fn) = 1;
556 :
557 70 : std::vector<tree> types;
558 :
559 70 : auto dst = Backend::var_expression (ctx.param_vars[0], UNDEF_LOCATION);
560 70 : TREE_READONLY (dst) = 0;
561 :
562 70 : auto value = Backend::var_expression (ctx.param_vars[1], UNDEF_LOCATION);
563 70 : auto memorder = make_unsigned_long_tree (ordering);
564 :
565 70 : auto builtin_decl = resolve_atomic_builtin (ctx, "atomic_store");
566 70 : if (!builtin_decl || builtin_decl == error_mark_node)
567 14 : return;
568 :
569 56 : auto atomic_store = build_fold_addr_expr_loc (UNKNOWN_LOCATION, builtin_decl);
570 :
571 56 : auto store_call
572 56 : = Backend::call_expression (atomic_store, {dst, value, memorder}, nullptr,
573 : UNDEF_LOCATION);
574 56 : TREE_READONLY (store_call) = 0;
575 56 : TREE_SIDE_EFFECTS (store_call) = 1;
576 :
577 56 : ctx.add_statement (store_call);
578 70 : }
579 :
580 : // Compiles atomic_load that has the signature (*const T) -> T
581 : static void
582 28 : atomic_load (IntrinsicCtx &ctx, int ordering)
583 : {
584 : // Most intrinsic functions are pure but not the atomic ones
585 : // FIXME: Is atomic_load_* pure? Feels like it shouldn't so
586 28 : TREE_READONLY (ctx.fn) = 0;
587 28 : TREE_SIDE_EFFECTS (ctx.fn) = 1;
588 :
589 28 : std::vector<tree> types;
590 :
591 28 : auto src = Backend::var_expression (ctx.param_vars[0], UNDEF_LOCATION);
592 28 : auto memorder = make_unsigned_long_tree (ordering);
593 :
594 28 : auto builtin_decl = resolve_atomic_builtin (ctx, "atomic_load");
595 28 : if (!builtin_decl || builtin_decl == error_mark_node)
596 0 : return;
597 :
598 28 : auto atomic_load = build_fold_addr_expr_loc (UNKNOWN_LOCATION, builtin_decl);
599 :
600 28 : auto load_call = Backend::call_expression (atomic_load, {src, memorder},
601 : nullptr, UNDEF_LOCATION);
602 28 : auto return_statement
603 28 : = Backend::return_statement (ctx.fn, load_call, UNDEF_LOCATION);
604 :
605 28 : TREE_READONLY (load_call) = 0;
606 28 : TREE_SIDE_EFFECTS (load_call) = 1;
607 :
608 28 : ctx.add_statement (return_statement);
609 28 : }
610 :
611 : // Compiles for atomic builtins that have the signature (*mut T, T) -> T
612 : static void
613 210 : atomic_binary_op (IntrinsicCtx &ctx, const std::string &op_name, int ordering)
614 : {
615 : // Most intrinsic functions are pure but not the atomic ones
616 210 : TREE_READONLY (ctx.fn) = 0;
617 210 : TREE_SIDE_EFFECTS (ctx.fn) = 1;
618 :
619 210 : auto dst = Backend::var_expression (ctx.param_vars[0], UNDEF_LOCATION);
620 210 : TREE_READONLY (dst) = 0;
621 :
622 210 : auto value = Backend::var_expression (ctx.param_vars[1], UNDEF_LOCATION);
623 210 : auto memorder = make_unsigned_long_tree (ordering);
624 :
625 210 : auto builtin_decl = resolve_atomic_builtin (ctx, "atomic_" + op_name);
626 :
627 210 : if (!builtin_decl || builtin_decl == error_mark_node)
628 : return;
629 :
630 210 : auto atomic_binary_op
631 210 : = build_fold_addr_expr_loc (UNKNOWN_LOCATION, builtin_decl);
632 :
633 210 : auto binary_op_call
634 210 : = Backend::call_expression (atomic_binary_op, {dst, value, memorder},
635 : nullptr, UNDEF_LOCATION);
636 :
637 210 : TREE_READONLY (binary_op_call) = 0;
638 210 : TREE_SIDE_EFFECTS (binary_op_call) = 1;
639 :
640 210 : auto return_statement
641 210 : = Backend::return_statement (ctx.fn, binary_op_call, UNDEF_LOCATION);
642 210 : ctx.add_statement (return_statement);
643 : }
644 :
645 : // Compiles atomic compare exchange with signature (*mut T, T, T) -> (T, bool).
646 : static void
647 140 : atomic_compare_exchange (IntrinsicCtx &ctx, bool weak, int success_ordering,
648 : int failure_ordering)
649 : {
650 : // Most intrinsic functions are pure but not the atomic ones
651 140 : TREE_READONLY (ctx.fn) = 0;
652 140 : TREE_SIDE_EFFECTS (ctx.fn) = 1;
653 :
654 : // The intrinsic block is already open, so emit the temporary declarations
655 : // directly into it.
656 140 : tree tmp_stmt = NULL_TREE;
657 140 : Bvariable *expected_var
658 140 : = Backend::temporary_variable (ctx.fn, NULL_TREE, ctx.param_types[1],
659 : NULL_TREE, true, UNDEF_LOCATION, &tmp_stmt);
660 140 : ctx.add_statement (tmp_stmt);
661 140 : Bvariable *success_var
662 140 : = Backend::temporary_variable (ctx.fn, NULL_TREE, boolean_type_node,
663 : NULL_TREE, false, UNDEF_LOCATION, &tmp_stmt);
664 140 : ctx.add_statement (tmp_stmt);
665 :
666 140 : auto dst = Backend::var_expression (ctx.param_vars[0], UNDEF_LOCATION);
667 140 : TREE_READONLY (dst) = 0;
668 :
669 140 : auto expected = expected_var->get_tree (UNDEF_LOCATION);
670 140 : auto success = success_var->get_tree (UNDEF_LOCATION);
671 :
672 140 : auto old_value = Backend::var_expression (ctx.param_vars[1], UNDEF_LOCATION);
673 140 : auto new_value = Backend::var_expression (ctx.param_vars[2], UNDEF_LOCATION);
674 :
675 140 : auto builtin_decl = resolve_atomic_builtin (ctx, "atomic_compare_exchange");
676 :
677 140 : if (!builtin_decl || builtin_decl == error_mark_node)
678 0 : return;
679 :
680 140 : ctx.add_statement (
681 : Backend::assignment_statement (expected, old_value, UNDEF_LOCATION));
682 :
683 140 : auto expected_addr = build_fold_addr_expr_loc (UNDEF_LOCATION, expected);
684 140 : auto builtin_addr = build_fold_addr_expr_loc (UNDEF_LOCATION, builtin_decl);
685 140 : auto weak_flag = build_int_cst (boolean_type_node, weak);
686 140 : auto success_memorder = make_unsigned_long_tree (success_ordering);
687 140 : auto failure_memorder = make_unsigned_long_tree (failure_ordering);
688 :
689 140 : auto exchange_call
690 140 : = Backend::call_expression (builtin_addr,
691 : {dst, expected_addr, new_value, weak_flag,
692 : success_memorder, failure_memorder},
693 : nullptr, UNDEF_LOCATION);
694 140 : TREE_READONLY (exchange_call) = 0;
695 140 : TREE_SIDE_EFFECTS (exchange_call) = 1;
696 :
697 140 : auto assignment_statement
698 140 : = Backend::assignment_statement (success, exchange_call, UNDEF_LOCATION);
699 140 : ctx.add_statement (assignment_statement);
700 :
701 140 : auto tuple_type = TREE_TYPE (DECL_RESULT (ctx.fn));
702 140 : auto result
703 140 : = Backend::constructor_expression (tuple_type, false, {expected, success},
704 : -1, UNDEF_LOCATION);
705 140 : auto return_statement
706 140 : = Backend::return_statement (ctx.fn, result, UNDEF_LOCATION);
707 140 : ctx.add_statement (return_statement);
708 : }
709 :
710 : // Compiles atomic min/max with signature (*mut T, T) -> T
711 : //
712 : // no gcc backend built-in equivalent, so we just do a compare-and-swap loop
713 : // similar to rustc_codegen_gcc & libstdc++.
714 : //
715 : // This is equivalent to the following code:
716 : //
717 : // T old_val = __atomic_load_N (dst, failure_order);
718 : // do
719 : // {
720 : // T desired = min_or_max (old_val, value);
721 : // if (__atomic_compare_exchange_N (dst, &old_val, desired, true,
722 : // order, failure_order))
723 : // break;
724 : // } while (true);
725 : // return old_val;
726 : static void
727 170 : atomic_minmax (IntrinsicCtx &ctx, AtomicMinMaxKind kind, int ordering)
728 : {
729 : // Most intrinsic functions are pure but not the atomic ones
730 170 : TREE_READONLY (ctx.fn) = 0;
731 170 : TREE_SIDE_EFFECTS (ctx.fn) = 1;
732 :
733 170 : auto load_decl = resolve_atomic_builtin (ctx, "atomic_load");
734 170 : if (!load_decl || load_decl == error_mark_node)
735 2 : return;
736 168 : auto exchange_decl = resolve_atomic_builtin (ctx, "atomic_compare_exchange");
737 168 : if (!exchange_decl || exchange_decl == error_mark_node)
738 : return;
739 :
740 168 : auto dst = Backend::var_expression (ctx.param_vars[0], UNDEF_LOCATION);
741 168 : TREE_READONLY (dst) = 0;
742 168 : auto value = Backend::var_expression (ctx.param_vars[1], UNDEF_LOCATION);
743 168 : auto value_type = ctx.param_types[1];
744 :
745 : // release uses relaxed & acq_req uses acquire for loads on fail exchanges
746 168 : int failure_ordering = ordering;
747 168 : if (ordering == __ATOMIC_RELEASE)
748 : failure_ordering = __ATOMIC_RELAXED;
749 140 : else if (ordering == __ATOMIC_ACQ_REL)
750 28 : failure_ordering = __ATOMIC_ACQUIRE;
751 :
752 168 : auto failure_memorder = make_unsigned_long_tree (failure_ordering);
753 168 : auto load_addr = build_fold_addr_expr_loc (UNDEF_LOCATION, load_decl);
754 168 : auto load_call = Backend::call_expression (load_addr, {dst, failure_memorder},
755 : nullptr, UNDEF_LOCATION);
756 168 : TREE_READONLY (load_call) = 0;
757 168 : TREE_SIDE_EFFECTS (load_call) = 1;
758 :
759 168 : tree tmp_stmt = NULL_TREE;
760 168 : Bvariable *expected_var
761 168 : = Backend::temporary_variable (ctx.fn, NULL_TREE, value_type, load_call,
762 : true, UNDEF_LOCATION, &tmp_stmt);
763 168 : ctx.add_statement (tmp_stmt);
764 168 : auto expected = expected_var->get_tree (UNDEF_LOCATION);
765 :
766 168 : bool is_unsigned = kind == AtomicMinMaxKind::UnsignedMax
767 168 : || kind == AtomicMinMaxKind::UnsignedMin;
768 168 : auto comparison_type = is_unsigned ? unsigned_type_for (value_type)
769 84 : : signed_type_for (value_type);
770 :
771 : // handle signedness irrespective of what the original type of the var is,
772 : // in case users do things like using atomic_umin on signed types and expect
773 : // it to work correctly
774 168 : bool is_min = (kind == AtomicMinMaxKind::SignedMin
775 168 : || kind == AtomicMinMaxKind::UnsignedMin);
776 252 : auto desired = build2 (is_min ? MIN_EXPR : MAX_EXPR, comparison_type,
777 : fold_convert (comparison_type, expected),
778 : fold_convert (comparison_type, value));
779 168 : desired = fold_convert (value_type, desired);
780 :
781 168 : auto exchange_addr = build_fold_addr_expr_loc (UNDEF_LOCATION, exchange_decl);
782 168 : auto expected_addr = build_fold_addr_expr_loc (UNDEF_LOCATION, expected);
783 168 : auto exchange_call
784 168 : = Backend::call_expression (exchange_addr,
785 168 : {dst, expected_addr, desired, boolean_true_node,
786 168 : make_unsigned_long_tree (ordering),
787 : failure_memorder},
788 : nullptr, UNDEF_LOCATION);
789 168 : TREE_READONLY (exchange_call) = 0;
790 168 : TREE_SIDE_EFFECTS (exchange_call) = 1;
791 :
792 168 : auto exit = Backend::exit_expression (exchange_call, UNDEF_LOCATION);
793 168 : ctx.add_statement (Backend::loop_expression (exit, UNDEF_LOCATION));
794 168 : ctx.add_statement (
795 : Backend::return_statement (ctx.fn, expected, UNDEF_LOCATION));
796 : }
797 :
798 : static void
799 14 : atomic_fence (IntrinsicCtx &ctx, bool is_multithread, int ordering)
800 : {
801 : // Most intrinsic functions are pure but not the atomic ones
802 14 : TREE_READONLY (ctx.fn) = 0;
803 14 : TREE_SIDE_EFFECTS (ctx.fn) = 1;
804 :
805 28 : auto builtin_decl = BuiltinsContext::get ().lookup_simple_builtin (
806 14 : is_multithread ? "__atomic_thread_fence" : "__atomic_signal_fence");
807 14 : rust_assert (builtin_decl);
808 :
809 14 : auto fence = build_fold_addr_expr_loc (UNDEF_LOCATION, *builtin_decl);
810 14 : auto memorder = make_unsigned_long_tree (ordering);
811 14 : auto fence_call
812 14 : = Backend::call_expression (fence, {memorder}, nullptr, UNDEF_LOCATION);
813 14 : TREE_READONLY (fence_call) = 0;
814 14 : TREE_SIDE_EFFECTS (fence_call) = 1;
815 :
816 14 : ctx.add_statement (fence_call);
817 14 : }
818 :
819 : // Shared inner implementation for ctlz and ctlz_nonzero.
820 : //
821 : // nonzero=false → ctlz: ctlz(0) is well-defined in Rust and must return
822 : // bit_size, but __builtin_clz*(0) is undefined behaviour in C, so an
823 : // explicit arg==0 guard is emitted.
824 : //
825 : // nonzero=true → ctlz_nonzero: the caller guarantees arg != 0 (passing 0
826 : // is immediate UB in Rust), so the zero guard is omitted entirely.
827 : static void
828 422 : ctlz_handler (IntrinsicCtx &ctx, bool nonzero)
829 : {
830 422 : auto arg_param = ctx.param_vars.at (0);
831 :
832 422 : auto *monomorphized_type
833 422 : = ctx.fntype.get_substs ().at (0).get_param_ty ()->resolve ();
834 422 : auto call_locus = ctx.get_mappings ().lookup_location (ctx.fntype.get_ref ());
835 422 : if (!check_for_basic_integer_type ("ctlz", call_locus, monomorphized_type))
836 2 : return;
837 :
838 420 : auto locus = ctx.fntype.get_locus ();
839 420 : auto arg_expr = Backend::var_expression (arg_param, locus);
840 420 : tree arg_type = TREE_TYPE (arg_expr);
841 420 : unsigned bit_size = TYPE_PRECISION (arg_type);
842 :
843 : // Convert signed types to their same-width unsigned equivalent before
844 : // widening. Without this, widening a signed type sign-extends it.
845 : // For example, i8(-1) = 0xFF widened to u32 gives 0xFFFFFFFF, so
846 : // __builtin_clz(0xFFFFFFFF) = 0, then 0 - diff(24) = -24.
847 : // Converting to u8 first gives 0xFF → 0x000000FF (zero-extended), so
848 : // __builtin_clz(0x000000FF) = 24, then 24 - 24 = 0.
849 420 : tree unsigned_type
850 420 : = !TYPE_UNSIGNED (arg_type) ? unsigned_type_for (arg_type) : arg_type;
851 420 : tree unsigned_arg = fold_convert (unsigned_type, arg_expr);
852 :
853 : // Pick the narrowest GCC clz builtin whose operand type is wide enough to
854 : // hold bit_size bits. diff records how many extra leading zeros the builtin
855 : // will count due to the width difference and is subtracted from the result.
856 : //
857 : // Example: ctlz(1u8) bit_size=8, int_prec=32, diff=24.
858 : // __builtin_clz(1u) returns 31 (counts from bit 31 down to bit 0).
859 : // 31 - 24 = 7, which is the correct answer for an 8-bit value.
860 : //
861 : // TODO: 128-bit integers are not yet handled.
862 420 : unsigned int_prec = TYPE_PRECISION (unsigned_type_node);
863 420 : unsigned long_prec = TYPE_PRECISION (long_unsigned_type_node);
864 420 : unsigned longlong_prec = TYPE_PRECISION (long_long_unsigned_type_node);
865 :
866 420 : const char *builtin_name = nullptr;
867 420 : tree cast_type = NULL_TREE;
868 420 : int diff = 0;
869 :
870 420 : if (bit_size <= int_prec)
871 : {
872 : // Fits in unsigned int: covers 8/16/32-bit integers on most targets.
873 315 : builtin_name = "__builtin_clz";
874 315 : cast_type = unsigned_type_node;
875 315 : diff = static_cast<int> (int_prec - bit_size);
876 : }
877 105 : else if (bit_size <= long_prec)
878 : {
879 : // Fits in unsigned long but not unsigned int.
880 105 : builtin_name = "__builtin_clzl";
881 105 : cast_type = long_unsigned_type_node;
882 105 : diff = static_cast<int> (long_prec - bit_size);
883 : }
884 0 : else if (bit_size <= longlong_prec)
885 : {
886 : // Fits in unsigned long long but not unsigned long.
887 0 : builtin_name = "__builtin_clzll";
888 0 : cast_type = long_long_unsigned_type_node;
889 0 : diff = static_cast<int> (longlong_prec - bit_size);
890 : }
891 : else
892 : {
893 0 : rust_sorry_at (locus, "ctlz for %u-bit integers is not yet implemented",
894 : bit_size);
895 0 : return;
896 : }
897 :
898 : // Widen the unsigned arg to the chosen builtin's operand type, call it,
899 : // then subtract the padding bits. diff == 0 means the Rust type exactly
900 : // matches the builtin's operand width, so the subtraction is skipped.
901 420 : tree call_arg = fold_convert (cast_type, unsigned_arg);
902 :
903 420 : auto builtin_decl
904 420 : = BuiltinsContext::get ().lookup_simple_builtin (builtin_name);
905 420 : rust_assert (builtin_decl);
906 :
907 420 : tree builtin_fn = build_fold_addr_expr_loc (locus, *builtin_decl);
908 420 : tree clz_expr
909 420 : = Backend::call_expression (builtin_fn, {call_arg}, nullptr, locus);
910 :
911 420 : if (diff > 0)
912 : {
913 210 : tree diff_cst = build_int_cst (integer_type_node, diff);
914 210 : clz_expr
915 210 : = fold_build2 (MINUS_EXPR, integer_type_node, clz_expr, diff_cst);
916 : }
917 :
918 420 : clz_expr = fold_convert (uint32_type_node, clz_expr);
919 :
920 420 : tree final_expr;
921 420 : if (!nonzero)
922 : {
923 : // ctlz(0) must return bit_size per the Rust reference.
924 : // We cannot pass 0 to __builtin_clz* (UB), so emit:
925 : // arg == 0 ? bit_size : clz_expr
926 252 : tree zero = build_int_cst (arg_type, 0);
927 252 : tree cmp = fold_build2 (EQ_EXPR, boolean_type_node, arg_expr, zero);
928 252 : tree width_cst = build_int_cst (uint32_type_node, bit_size);
929 252 : final_expr
930 252 : = fold_build3 (COND_EXPR, uint32_type_node, cmp, width_cst, clz_expr);
931 : }
932 : else
933 : {
934 : // ctlz_nonzero: arg != 0 is guaranteed by the caller, no guard needed.
935 : final_expr = clz_expr;
936 : }
937 :
938 420 : tree result = fold_convert (TREE_TYPE (DECL_RESULT (ctx.fn)), final_expr);
939 420 : auto return_stmt = Backend::return_statement (ctx.fn, result, locus);
940 420 : ctx.add_statement (return_stmt);
941 : }
942 :
943 : // Shared inner implementation for cttz and cttz_nonzero.
944 : //
945 : // nonzero=false → cttz: cttz(0) is well-defined in Rust and must return
946 : // bit_size, but __builtin_ctz*(0) is undefined behaviour in C, so an
947 : // explicit arg==0 guard is emitted.
948 : //
949 : // nonzero=true → cttz_nonzero: the caller guarantees arg != 0 (passing 0
950 : // is immediate UB in Rust), so the zero guard is omitted entirely.
951 : static void
952 282 : cttz_handler (IntrinsicCtx &ctx, bool nonzero)
953 : {
954 282 : auto arg_param = ctx.param_vars.at (0);
955 :
956 282 : auto *monomorphized_type
957 282 : = ctx.fntype.get_substs ().at (0).get_param_ty ()->resolve ();
958 282 : auto call_locus = ctx.get_mappings ().lookup_location (ctx.fntype.get_ref ());
959 282 : if (!check_for_basic_integer_type ("cttz", call_locus, monomorphized_type))
960 2 : return;
961 :
962 280 : auto locus = ctx.fntype.get_locus ();
963 280 : auto arg_expr = Backend::var_expression (arg_param, locus);
964 280 : tree arg_type = TREE_TYPE (arg_expr);
965 280 : unsigned bit_size = TYPE_PRECISION (arg_type);
966 :
967 : // Convert signed types to their same-width unsigned equivalent before
968 : // widening. For cttz this is not strictly required for correctness (sign
969 : // extension fills high bits with 1s, which does not alter the trailing-zero
970 : // count at the low end), but it avoids relying on signed-integer
971 : // representations and keeps the approach consistent with ctlz.
972 280 : tree unsigned_type
973 280 : = !TYPE_UNSIGNED (arg_type) ? unsigned_type_for (arg_type) : arg_type;
974 280 : tree unsigned_arg = fold_convert (unsigned_type, arg_expr);
975 :
976 : // Pick the narrowest GCC ctz builtin whose operand type is wide enough to
977 : // hold bit_size bits. Unlike ctlz, no diff adjustment is needed: widening
978 : // a value zero-extends it (fills the added high bits with 0s), which does
979 : // not introduce new trailing zeros at the low end.
980 : //
981 : // Example: cttz(0b00001000_u8) = 3
982 : // Widened to u32: 0x00000008. __builtin_ctz(0x00000008) = 3.
983 : //
984 : // TODO: 128-bit integers are not yet handled.
985 280 : unsigned int_prec = TYPE_PRECISION (unsigned_type_node);
986 280 : unsigned long_prec = TYPE_PRECISION (long_unsigned_type_node);
987 280 : unsigned longlong_prec = TYPE_PRECISION (long_long_unsigned_type_node);
988 :
989 280 : const char *builtin_name = nullptr;
990 280 : tree cast_type = NULL_TREE;
991 :
992 280 : if (bit_size <= int_prec)
993 : {
994 : // Fits in unsigned int: covers 8/16/32-bit integers on most targets.
995 : builtin_name = "__builtin_ctz";
996 : cast_type = unsigned_type_node;
997 : }
998 70 : else if (bit_size <= long_prec)
999 : {
1000 : // Fits in unsigned long but not unsigned int.
1001 : builtin_name = "__builtin_ctzl";
1002 : cast_type = long_unsigned_type_node;
1003 : }
1004 0 : else if (bit_size <= longlong_prec)
1005 : {
1006 : // Fits in unsigned long long but not unsigned long.
1007 : builtin_name = "__builtin_ctzll";
1008 : cast_type = long_long_unsigned_type_node;
1009 : }
1010 : else
1011 : {
1012 0 : rust_sorry_at (locus, "cttz for %u-bit integers is not yet implemented",
1013 : bit_size);
1014 0 : return;
1015 : }
1016 :
1017 280 : tree call_arg = fold_convert (cast_type, unsigned_arg);
1018 :
1019 280 : auto builtin_decl
1020 280 : = BuiltinsContext::get ().lookup_simple_builtin (builtin_name);
1021 280 : rust_assert (builtin_decl);
1022 :
1023 280 : tree builtin_fn = build_fold_addr_expr_loc (locus, *builtin_decl);
1024 280 : tree ctz_expr
1025 280 : = Backend::call_expression (builtin_fn, {call_arg}, nullptr, locus);
1026 :
1027 280 : ctz_expr = fold_convert (uint32_type_node, ctz_expr);
1028 :
1029 280 : tree final_expr;
1030 280 : if (!nonzero)
1031 : {
1032 : // cttz(0) must return bit_size per the Rust reference.
1033 : // We cannot pass 0 to __builtin_ctz* (UB), so emit:
1034 : // arg == 0 ? bit_size : ctz_expr
1035 168 : tree zero = build_int_cst (arg_type, 0);
1036 168 : tree cmp = fold_build2 (EQ_EXPR, boolean_type_node, arg_expr, zero);
1037 168 : tree width_cst = build_int_cst (uint32_type_node, bit_size);
1038 168 : final_expr
1039 168 : = fold_build3 (COND_EXPR, uint32_type_node, cmp, width_cst, ctz_expr);
1040 : }
1041 : else
1042 : {
1043 : // cttz_nonzero: arg != 0 is guaranteed by the caller, no guard needed.
1044 : final_expr = ctz_expr;
1045 : }
1046 :
1047 280 : tree result = fold_convert (TREE_TYPE (DECL_RESULT (ctx.fn)), final_expr);
1048 280 : auto return_stmt = Backend::return_statement (ctx.fn, result, locus);
1049 280 : ctx.add_statement (return_stmt);
1050 : }
1051 :
1052 : static void
1053 6 : fop_fast (IntrinsicCtx &ctx, ArithmeticOrLogicalOperator op)
1054 : {
1055 6 : auto &lhs_param = ctx.param_vars.at (0);
1056 6 : auto &rhs_param = ctx.param_vars.at (1);
1057 6 : rust_assert (ctx.param_vars.size () == 2);
1058 :
1059 6 : tree lhs = Backend::var_expression (lhs_param, UNDEF_LOCATION);
1060 6 : tree rhs = Backend::var_expression (rhs_param, UNDEF_LOCATION);
1061 :
1062 6 : tree result_expr
1063 6 : = Backend::arithmetic_or_logical_expression (op, lhs, rhs, ctx.loc);
1064 :
1065 6 : auto return_statement
1066 6 : = Backend::return_statement (ctx.fn, result_expr, UNDEF_LOCATION);
1067 :
1068 6 : ctx.add_statement (return_statement);
1069 6 : }
1070 :
1071 : static tree
1072 44 : build_rust_string (IntrinsicCtx &ctx, const std::string &content)
1073 : {
1074 44 : TyTy::BaseType *str = nullptr;
1075 44 : TyTy::BaseType *usize = nullptr;
1076 44 : rust_assert (ctx.get_tyctx ()->lookup_builtin ("str", &str));
1077 44 : rust_assert (ctx.get_tyctx ()->lookup_builtin ("usize", &usize));
1078 :
1079 44 : tree meta_pointer = TyTyResolveCompile::compile (ctx.inner (), str);
1080 44 : tree usize_ty = TyTyResolveCompile::compile (ctx.inner (), usize);
1081 :
1082 44 : auto base = Backend::string_constant_expression (content);
1083 44 : tree data = build_fold_addr_expr (base);
1084 :
1085 44 : tree size = build_int_cstu (usize_ty, content.size ());
1086 :
1087 44 : return Backend::constructor_expression (meta_pointer, false, {data, size}, -1,
1088 44 : ctx.loc);
1089 : }
1090 :
1091 : /**
1092 : * Check if a type is uninhabited - either the Never type or a variant-less enum
1093 : */
1094 : static bool
1095 68 : is_uninhabited (TyTy::BaseType *t)
1096 : {
1097 68 : rust_assert (t);
1098 :
1099 68 : if (auto adt = t->try_as<TyTy::ADTType> ())
1100 44 : if (adt->is_enum () && adt->number_of_variants () == 0)
1101 : return true;
1102 :
1103 46 : if (t->is<TyTy::NeverType> ())
1104 22 : return true;
1105 :
1106 : return false;
1107 : }
1108 :
1109 : static void
1110 68 : type_assert (IntrinsicCtx &ctx, TyAssert assert)
1111 : {
1112 : /**
1113 : * These functions check whether a type respects a predicate *statically* - if
1114 : * the given generic is invalid, then panic at runtime, otherwise do nothing.
1115 : */
1116 :
1117 : /**
1118 : * TODO: Since gccrs currently lacks comprehensive layout engine support for
1119 : * validity ranges (such as `rustc_layout_scalar_valid_range_start`), we
1120 : * cannot accurately determine if a type is safely zeroable or can be left
1121 : * uninitialized. Therefore, this is implemented as a temporary no-op stub
1122 : * except for uninhabited types. We emit a warning regarding this limitation.
1123 : */
1124 :
1125 68 : auto t_param = ctx.fntype.get_substs ().at (0);
1126 68 : auto t_ty = t_param.get_param_ty ();
1127 68 : auto t = t_ty->resolve ();
1128 68 : auto t_name = t->get_name ();
1129 68 : auto valid = true;
1130 :
1131 : // In all cases, invalid types are... invalid. So single out never and
1132 : // variant-less enums and return early
1133 68 : if (is_uninhabited (t))
1134 44 : valid = false;
1135 :
1136 : /**
1137 : * TODO: Since gccrs currently lacks comprehensive layout engine support for
1138 : * validity ranges (such as `rustc_layout_scalar_valid_range_start`), we
1139 : * cannot accurately determine if a type is safely zeroable or can be left
1140 : * uninitialized. Therefore, this is implemented as a temporary no-op stub
1141 : * except for uninhabited types
1142 : */
1143 68 : auto always_warn = false;
1144 68 : auto fn_name = "";
1145 :
1146 : // FIXME: Once we have proper verification for the layout of the given type,
1147 : // we need to separate our panic strings based on the operation we're doing -
1148 : // asserting that a type is inhabited, zero valid, or uninit valid
1149 68 : std::string panic_str;
1150 :
1151 68 : switch (assert)
1152 : {
1153 24 : case TyAssert::Inhabited:
1154 48 : panic_str = "attempted to instantiate uninhabited type `" + t_name + "`";
1155 24 : break;
1156 23 : case TyAssert::ZeroValid:
1157 23 : panic_str = "attempted to zero-initialize type `" + t_name
1158 23 : + "`, which is invalid";
1159 :
1160 : // FIXME: Remove eventually
1161 23 : always_warn = true;
1162 23 : fn_name = "assert_zero_valid<T>";
1163 23 : break;
1164 21 : case TyAssert::UninitValid:
1165 21 : panic_str = "attempted to leave type `" + t_name
1166 21 : + "` uninitialized, which is invalid";
1167 :
1168 : // FIXME: Remove eventually
1169 21 : always_warn = true;
1170 21 : fn_name = "assert_uninit_valid<T>";
1171 21 : break;
1172 : }
1173 :
1174 68 : if (always_warn)
1175 : {
1176 44 : rust_warning_at (ctx.loc, 0,
1177 : "%qs was emitted as a no-op and "
1178 : "will not trigger - this may be invalid",
1179 : fn_name);
1180 : }
1181 :
1182 68 : if (!valid)
1183 : {
1184 44 : auto arg = build_rust_string (ctx, panic_str);
1185 :
1186 44 : auto panic_call = lang_item_call (ctx, LangItem::Kind::PANIC, {arg});
1187 :
1188 44 : ctx.add_statement (panic_call);
1189 :
1190 : // buh :(
1191 44 : TREE_READONLY (ctx.fn) = 0;
1192 44 : TREE_USED (ctx.fn) = 1;
1193 44 : TREE_SIDE_EFFECTS (ctx.fn) = 1;
1194 : }
1195 68 : }
1196 :
1197 : static void
1198 18 : prefetch (IntrinsicCtx &ctx, Mode mode, Cache cache)
1199 : {
1200 : // The core library technically allows you to pass any i32 value as a
1201 : // locality, but LLVM will then complain if the value cannot be constant
1202 : // evaluated. For now, we ignore the locality argument and instead always
1203 : // pass `3` (the most restrictive value). This allows us to still have
1204 : // prefetch behavior, just not as granular as expected. In future Rust
1205 : // versions, we hope that prefetch intrinsics will be split up according to
1206 : // locality, similarly to atomic intrinsics.
1207 : // The solution is to try and perform constant folding for the locality
1208 : // argument, or instead of creating a new function definition, modify the call
1209 : // site directly This has the bad side-effect of creating warnings about
1210 : // `unused name - locality`, which we hack away here:
1211 : // TODO: Take care of handling locality properly
1212 18 : Backend::var_expression (ctx.param_vars[1], UNDEF_LOCATION);
1213 :
1214 : // If we are doing prefetches on the instruction cache, then emit a no-op. GCC
1215 : // currently has no support for prefetching the instruction cache, and
1216 : // prefetches are allowed to be completely ignored anyways. Revisit once
1217 : // __builtin_prefetch() accepts another argument for the `cache` to use, in
1218 : // the same way that llvm.prefetch does. In the same vein as above, mark the
1219 : // given addr as used so there's no complaints.
1220 18 : if (cache == Cache::Instruction)
1221 : {
1222 2 : Backend::var_expression (ctx.param_vars[0], UNDEF_LOCATION);
1223 :
1224 2 : return;
1225 : }
1226 :
1227 : // prefetching isn't pure and shouldn't be discarded after GIMPLE
1228 16 : TREE_READONLY (ctx.fn) = 0;
1229 16 : TREE_SIDE_EFFECTS (ctx.fn) = 1;
1230 :
1231 16 : auto addr = Backend::var_expression (ctx.param_vars[0], UNDEF_LOCATION);
1232 :
1233 16 : auto rw_flag = make_unsigned_long_tree (mode == Mode::Write ? 1 : 0);
1234 :
1235 16 : auto prefetch_raw
1236 16 : = BuiltinsContext::get ().lookup_simple_builtin ("__builtin_prefetch");
1237 16 : rust_assert (prefetch_raw);
1238 :
1239 16 : auto prefetch = build_fold_addr_expr_loc (UNKNOWN_LOCATION, *prefetch_raw);
1240 :
1241 32 : auto prefetch_call = Backend::call_expression (prefetch,
1242 : {addr, rw_flag,
1243 : // locality arg
1244 16 : make_unsigned_long_tree (3)},
1245 : nullptr, UNDEF_LOCATION);
1246 :
1247 16 : TREE_READONLY (prefetch_call) = 0;
1248 16 : TREE_SIDE_EFFECTS (prefetch_call) = 1;
1249 :
1250 16 : ctx.add_statement (prefetch_call);
1251 : }
1252 :
1253 : } // namespace inner
1254 :
1255 : Intrinsic::CompileFn
1256 16233 : op_with_overflow (tree_code op)
1257 : {
1258 176 : return [op] (IntrinsicCtx &ctx) { return inner::op_with_overflow (ctx, op); };
1259 : }
1260 :
1261 : Intrinsic::CompileFn
1262 27055 : fop_fast (ArithmeticOrLogicalOperator op)
1263 : {
1264 6 : return [op] (IntrinsicCtx &ctx) { return inner::fop_fast (ctx, op); };
1265 : }
1266 :
1267 : Intrinsic::CompileFn
1268 16233 : type_assert (TyAssert assert)
1269 : {
1270 16233 : return
1271 68 : [assert] (IntrinsicCtx &ctx) { return inner::type_assert (ctx, assert); };
1272 : }
1273 :
1274 : void
1275 233 : rotate_left (IntrinsicCtx &ctx)
1276 : {
1277 233 : return handlers::rotate (ctx, LROTATE_EXPR);
1278 : }
1279 :
1280 : void
1281 190 : rotate_right (IntrinsicCtx &ctx)
1282 : {
1283 190 : return handlers::rotate (ctx, RROTATE_EXPR);
1284 : }
1285 :
1286 : Intrinsic::CompileFn
1287 16233 : wrapping_op (tree_code op)
1288 : {
1289 380 : return [op] (IntrinsicCtx &ctx) { return inner::wrapping_op (ctx, op); };
1290 : }
1291 :
1292 : Intrinsic::CompileFn
1293 32466 : atomic_store (int ordering)
1294 : {
1295 32536 : return [ordering] (IntrinsicCtx &ctx) {
1296 70 : return inner::atomic_store (ctx, ordering);
1297 32466 : };
1298 : }
1299 :
1300 : Intrinsic::CompileFn
1301 32466 : atomic_load (int ordering)
1302 : {
1303 32494 : return [ordering] (IntrinsicCtx &ctx) {
1304 28 : return inner::atomic_load (ctx, ordering);
1305 32466 : };
1306 : }
1307 :
1308 : Intrinsic::CompileFn
1309 27055 : atomic_xadd (int ordering)
1310 : {
1311 27090 : return [ordering] (IntrinsicCtx &ctx) {
1312 35 : return inner::atomic_binary_op (ctx, "fetch_add", ordering);
1313 27055 : };
1314 : }
1315 :
1316 : Intrinsic::CompileFn
1317 27055 : atomic_xsub (int ordering)
1318 : {
1319 27090 : return [ordering] (IntrinsicCtx &ctx) {
1320 35 : return inner::atomic_binary_op (ctx, "fetch_sub", ordering);
1321 27055 : };
1322 : }
1323 :
1324 : Intrinsic::CompileFn
1325 27055 : atomic_and (int ordering)
1326 : {
1327 27090 : return [ordering] (IntrinsicCtx &ctx) {
1328 35 : return inner::atomic_binary_op (ctx, "fetch_and", ordering);
1329 27055 : };
1330 : }
1331 :
1332 : Intrinsic::CompileFn
1333 27055 : atomic_nand (int ordering)
1334 : {
1335 27090 : return [ordering] (IntrinsicCtx &ctx) {
1336 35 : return inner::atomic_binary_op (ctx, "fetch_nand", ordering);
1337 27055 : };
1338 : }
1339 :
1340 : Intrinsic::CompileFn
1341 27055 : atomic_or (int ordering)
1342 : {
1343 27090 : return [ordering] (IntrinsicCtx &ctx) {
1344 35 : return inner::atomic_binary_op (ctx, "fetch_or", ordering);
1345 27055 : };
1346 : }
1347 :
1348 : Intrinsic::CompileFn
1349 27055 : atomic_xor (int ordering)
1350 : {
1351 27090 : return [ordering] (IntrinsicCtx &ctx) {
1352 35 : return inner::atomic_binary_op (ctx, "fetch_xor", ordering);
1353 27055 : };
1354 : }
1355 : Intrinsic::CompileFn
1356 54110 : atomic_compare_exchange (bool weak, int success_ordering, int failure_ordering)
1357 : {
1358 54250 : return [weak, success_ordering, failure_ordering] (IntrinsicCtx &ctx) {
1359 140 : return inner::atomic_compare_exchange (ctx, weak, success_ordering,
1360 140 : failure_ordering);
1361 54110 : };
1362 : }
1363 :
1364 : Intrinsic::CompileFn
1365 108220 : atomic_minmax (AtomicMinMaxKind kind, int ordering)
1366 : {
1367 108390 : return [kind, ordering] (IntrinsicCtx &ctx) {
1368 170 : return inner::atomic_minmax (ctx, kind, ordering);
1369 108220 : };
1370 : }
1371 :
1372 : Intrinsic::CompileFn
1373 43288 : atomic_fence (bool is_multithread, int ordering)
1374 : {
1375 43302 : return [is_multithread, ordering] (IntrinsicCtx &ctx) {
1376 14 : return inner::atomic_fence (ctx, is_multithread, ordering);
1377 43288 : };
1378 : }
1379 :
1380 : Intrinsic::CompileFn
1381 37877 : unchecked_op (tree_code op)
1382 : {
1383 58 : return [op] (IntrinsicCtx &ctx) { return inner::unchecked_op (ctx, op); };
1384 : }
1385 :
1386 : Intrinsic::CompileFn
1387 10822 : copy (bool overlaps)
1388 : {
1389 299 : return [overlaps] (IntrinsicCtx &ctx) { return inner::copy (ctx, overlaps); };
1390 : }
1391 :
1392 : Intrinsic::CompileFn
1393 10822 : expect (bool likely)
1394 : {
1395 0 : return [likely] (IntrinsicCtx &ctx) { return inner::expect (ctx, likely); };
1396 : }
1397 :
1398 : Intrinsic::CompileFn
1399 10822 : try_handler (bool is_new_api)
1400 : {
1401 10824 : return [is_new_api] (IntrinsicCtx &ctx) {
1402 2 : return inner::try_handler (ctx, is_new_api);
1403 10822 : };
1404 : }
1405 :
1406 : Intrinsic::CompileFn
1407 21644 : prefetch (Mode mode, Cache cache)
1408 : {
1409 21662 : return [mode, cache] (IntrinsicCtx &ctx) {
1410 18 : return inner::prefetch (ctx, mode, cache);
1411 21644 : };
1412 : }
1413 :
1414 : void
1415 1 : sorry (IntrinsicCtx &ctx)
1416 : {
1417 2 : rust_sorry_at (ctx.loc, "intrinsic %qs is not yet implemented",
1418 : ctx.fntype.get_identifier ().c_str ());
1419 1 : }
1420 :
1421 : void
1422 1 : assume (IntrinsicCtx &ctx)
1423 : {
1424 : // TODO: make sure this is actually helping the compiler optimize
1425 :
1426 1 : rust_assert (ctx.fntype.param_at (0).get_type ()->get_kind ()
1427 : == TyTy::TypeKind::BOOL);
1428 :
1429 : // TODO: make sure these are necessary
1430 1 : TREE_READONLY (ctx.fn) = 0;
1431 1 : DECL_DISREGARD_INLINE_LIMITS (ctx.fn) = 1;
1432 1 : DECL_ATTRIBUTES (ctx.fn) = tree_cons (get_identifier ("always_inline"),
1433 1 : NULL_TREE, DECL_ATTRIBUTES (ctx.fn));
1434 :
1435 1 : tree val = Backend::var_expression (ctx.param_vars[0], UNDEF_LOCATION);
1436 :
1437 1 : tree assume_expr = build_call_expr_internal_loc (UNDEF_LOCATION, IFN_ASSUME,
1438 : void_type_node, 1, val);
1439 1 : TREE_SIDE_EFFECTS (assume_expr) = 1;
1440 :
1441 1 : ctx.add_statement (assume_expr);
1442 1 : }
1443 :
1444 : void
1445 102 : discriminant_value (IntrinsicCtx &ctx)
1446 : {
1447 102 : auto &mapping = ctx.fntype.get_substs ().at (0);
1448 102 : auto param_ty = mapping.get_param_ty ();
1449 102 : rust_assert (param_ty->can_resolve ());
1450 102 : auto resolved = param_ty->resolve ();
1451 :
1452 102 : TyTy::BaseType *return_type = nullptr;
1453 102 : bool ok = ctx.get_tyctx ()->lookup_builtin ("isize", &return_type);
1454 102 : rust_assert (ok);
1455 :
1456 102 : bool is_adt = resolved->is<TyTy::ADTType> ();
1457 102 : bool is_enum = false;
1458 102 : if (is_adt)
1459 : {
1460 102 : const auto &adt = *static_cast<TyTy::ADTType *> (resolved);
1461 102 : auto *repr = adt.get_repr_options ().repr;
1462 102 : if (repr != nullptr)
1463 102 : return_type = repr;
1464 102 : is_enum = adt.is_enum ();
1465 : }
1466 :
1467 102 : tree result = integer_zero_node;
1468 102 : if (is_enum)
1469 : {
1470 102 : tree val = Backend::var_expression (ctx.param_vars[0], UNDEF_LOCATION);
1471 102 : tree deref = build_fold_indirect_ref_loc (UNKNOWN_LOCATION, val);
1472 102 : result = Backend::struct_field_expression (deref, 0, UNKNOWN_LOCATION);
1473 : }
1474 :
1475 102 : auto return_statement
1476 102 : = Backend::return_statement (ctx.fn, result, BUILTINS_LOCATION);
1477 102 : ctx.add_statement (return_statement);
1478 102 : }
1479 :
1480 : void
1481 7 : variant_count (IntrinsicCtx &ctx)
1482 : {
1483 7 : auto &mapping = ctx.fntype.get_substs ().at (0);
1484 7 : auto param_ty = mapping.get_param_ty ();
1485 7 : rust_assert (param_ty->can_resolve ());
1486 7 : auto resolved = param_ty->resolve ();
1487 :
1488 7 : size_t variant_count = 0;
1489 7 : bool is_adt = resolved->is<TyTy::ADTType> ();
1490 7 : if (is_adt)
1491 : {
1492 7 : const auto &adt = *static_cast<TyTy::ADTType *> (resolved);
1493 7 : variant_count = adt.number_of_variants ();
1494 : }
1495 :
1496 7 : tree result_decl = DECL_RESULT (ctx.fn);
1497 7 : tree type = TREE_TYPE (result_decl);
1498 :
1499 7 : mpz_t ival;
1500 7 : mpz_init_set_ui (ival, variant_count);
1501 7 : tree result = wide_int_to_tree (type, wi::from_mpz (type, ival, true));
1502 7 : mpz_clear (ival);
1503 :
1504 7 : auto return_statement
1505 7 : = Backend::return_statement (ctx.fn, result, BUILTINS_LOCATION);
1506 7 : ctx.add_statement (return_statement);
1507 7 : }
1508 :
1509 : void
1510 34 : move_val_init (IntrinsicCtx &ctx)
1511 : {
1512 : // NOTE: Per rustc 1.49, `move_val_init` has magic semantics - code is down
1513 : // below. We need to ensure that we have the correct behavior by doing a
1514 : // memcpy here.
1515 :
1516 : // if let Some(sym::move_val_init) = intrinsic {
1517 : // `move_val_init` has "magic" semantics - the second argument is always
1518 : // evaluated "directly" into the first one.
1519 :
1520 : // let mut args = args.into_iter();
1521 : // let ptr = args.next().expect("0 arguments to `move_val_init`");
1522 : // let val = args.next().expect("1 argument to `move_val_init`");
1523 : // assert!(args.next().is_none(), ">2 arguments to `move_val_init`");
1524 :
1525 : // let ptr = this.hir.mirror(ptr);
1526 : // let ptr_ty = ptr.ty;
1527 : // // Create an *internal* temp for the pointer, so that unsafety
1528 : // // checking won't complain about the raw pointer assignment.
1529 : // let ptr_temp = this
1530 : // .local_decls
1531 : // .push(LocalDecl::with_source_info(ptr_ty, source_info).internal());
1532 : // let ptr_temp = Place::from(ptr_temp);
1533 : // let block = unpack!(this.into(ptr_temp, block, ptr));
1534 : // this.into(this.hir.tcx().mk_place_deref(ptr_temp), block, val)
1535 :
1536 : // Most intrinsic functions are pure - not `move_val_init`
1537 34 : TREE_READONLY (ctx.fn) = 0;
1538 34 : TREE_SIDE_EFFECTS (ctx.fn) = 1;
1539 :
1540 : // get the template parameter type tree fn size_of<T>();
1541 34 : auto ¶m_mapping = ctx.fntype.get_substs ().at (0);
1542 34 : auto param_tyty = param_mapping.get_param_ty ();
1543 34 : auto resolved_tyty = param_tyty->resolve ();
1544 34 : tree template_parameter_type
1545 34 : = TyTyResolveCompile::compile (ctx.inner (), resolved_tyty);
1546 :
1547 34 : tree dst = Backend::var_expression (ctx.param_vars[0], UNDEF_LOCATION);
1548 34 : tree src = Backend::var_expression (ctx.param_vars[1], UNDEF_LOCATION);
1549 34 : tree size = TYPE_SIZE_UNIT (template_parameter_type);
1550 :
1551 34 : auto memcpy_builtin
1552 34 : = BuiltinsContext::get ().lookup_simple_builtin ("__builtin_memcpy");
1553 34 : rust_assert (memcpy_builtin);
1554 :
1555 34 : src = build_fold_addr_expr_loc (BUILTINS_LOCATION, src);
1556 34 : tree memset_call = build_call_expr_loc (BUILTINS_LOCATION, *memcpy_builtin, 3,
1557 : dst, src, size);
1558 :
1559 34 : ctx.add_statement (memset_call);
1560 34 : }
1561 :
1562 : void
1563 113 : uninit (IntrinsicCtx &ctx)
1564 : {
1565 : // uninit has _zero_ parameters its parameter is the generic one
1566 :
1567 : // Most intrinsic functions are pure - not `uninit_handler`
1568 113 : TREE_READONLY (ctx.fn) = 0;
1569 113 : TREE_SIDE_EFFECTS (ctx.fn) = 1;
1570 :
1571 : // get the template parameter type tree fn uninit<T>();
1572 113 : auto ¶m_mapping = ctx.fntype.get_substs ().at (0);
1573 113 : const auto param_tyty = param_mapping.get_param_ty ();
1574 113 : auto resolved_tyty = param_tyty->resolve ();
1575 113 : tree template_parameter_type
1576 113 : = TyTyResolveCompile::compile (ctx.inner (), resolved_tyty);
1577 :
1578 : // result temporary
1579 113 : tree dst_type = TREE_TYPE (DECL_RESULT (ctx.fn));
1580 113 : rust_assert (TYPE_SIZE_UNIT (template_parameter_type)
1581 : == TYPE_SIZE_UNIT (dst_type));
1582 :
1583 113 : tree tmp_stmt = error_mark_node;
1584 113 : Bvariable *bvar
1585 113 : = Backend::temporary_variable (ctx.fn, NULL_TREE, dst_type, NULL_TREE,
1586 : true /*address_is_taken*/, UNDEF_LOCATION,
1587 : &tmp_stmt);
1588 :
1589 113 : ctx.set_block_variables ({bvar});
1590 :
1591 113 : auto memset_builtin
1592 113 : = BuiltinsContext::get ().lookup_simple_builtin ("__builtin_memset");
1593 113 : rust_assert (memset_builtin);
1594 :
1595 : // call memset with 0x01 and size of the thing see
1596 : // https://github.com/Rust-GCC/gccrs/issues/1899
1597 :
1598 113 : tree dst = bvar->get_tree (BUILTINS_LOCATION);
1599 113 : tree dst_addr = build_fold_addr_expr_loc (BUILTINS_LOCATION, dst);
1600 113 : tree constant_byte = build_int_cst (integer_type_node, 0x01);
1601 113 : tree size_expr = TYPE_SIZE_UNIT (template_parameter_type);
1602 :
1603 113 : tree memset_call = build_call_expr_loc (BUILTINS_LOCATION, *memset_builtin, 3,
1604 : dst_addr, constant_byte, size_expr);
1605 113 : ctx.add_statement (memset_call);
1606 :
1607 113 : auto return_statement
1608 113 : = Backend::return_statement (ctx.fn, dst, UNDEF_LOCATION);
1609 113 : ctx.add_statement (return_statement);
1610 113 : }
1611 :
1612 : void
1613 423 : rotate (IntrinsicCtx &ctx, tree_code op)
1614 : {
1615 : // rotate intrinsic has two parameter
1616 :
1617 423 : auto &x_param = ctx.param_vars.at (0);
1618 423 : auto &y_param = ctx.param_vars.at (1);
1619 423 : rust_assert (ctx.param_vars.size () == 2);
1620 :
1621 423 : tree x = Backend::var_expression (x_param, UNDEF_LOCATION);
1622 423 : tree y = Backend::var_expression (y_param, UNDEF_LOCATION);
1623 423 : tree rotate_expr
1624 423 : = fold_build2_loc (BUILTINS_LOCATION, op, TREE_TYPE (x), x, y);
1625 423 : auto return_statement
1626 423 : = Backend::return_statement (ctx.fn, rotate_expr, UNDEF_LOCATION);
1627 423 : ctx.add_statement (return_statement);
1628 423 : }
1629 :
1630 : void
1631 228 : transmute (IntrinsicCtx &ctx)
1632 : {
1633 : // param to convert
1634 228 : Bvariable *convert_me_param = ctx.param_vars.at (0);
1635 228 : tree convert_me_expr
1636 228 : = Backend::var_expression (convert_me_param, UNDEF_LOCATION);
1637 :
1638 : // check for transmute pre-conditions
1639 228 : tree target_type_expr = TREE_TYPE (DECL_RESULT (ctx.fn));
1640 228 : tree source_type_expr = ctx.param_types.at (0);
1641 228 : tree target_size_expr = TYPE_SIZE (target_type_expr);
1642 228 : tree source_size_expr = TYPE_SIZE (source_type_expr);
1643 : // for some reason, unit types and other zero-sized types return NULL for the
1644 : // size expressions
1645 228 : unsigned HOST_WIDE_INT target_size
1646 228 : = target_size_expr ? TREE_INT_CST_LOW (target_size_expr) : 0;
1647 228 : unsigned HOST_WIDE_INT source_size
1648 228 : = source_size_expr ? TREE_INT_CST_LOW (source_size_expr) : 0;
1649 :
1650 : // size check for concrete types
1651 : // TODO(liushuyu): check alignment for pointers; check for dependently-sized
1652 : // types
1653 228 : if (target_size != source_size)
1654 : {
1655 7 : rust_error_at (ctx.fntype.get_locus (),
1656 : "cannot transmute between types of different sizes, or "
1657 : "dependently-sized types");
1658 7 : rust_inform (
1659 7 : ctx.fntype.get_ident ().locus, "source type: %qs (%lu bits)",
1660 7 : ctx.fntype.get_params ().at (0).get_type ()->as_string ().c_str (),
1661 : (unsigned long) source_size);
1662 7 : rust_inform (ctx.fntype.get_ident ().locus, "target type: %qs (%lu bits)",
1663 14 : ctx.fntype.get_return_type ()->as_string ().c_str (),
1664 : (unsigned long) target_size);
1665 : }
1666 :
1667 : // Return *((orig_type*)&decl) */
1668 :
1669 228 : tree t = build_fold_addr_expr_loc (UNKNOWN_LOCATION, convert_me_expr);
1670 228 : t = fold_build1_loc (UNKNOWN_LOCATION, NOP_EXPR,
1671 : build_pointer_type (target_type_expr), t);
1672 228 : tree result_expr = build_fold_indirect_ref_loc (UNKNOWN_LOCATION, t);
1673 :
1674 228 : auto return_statement
1675 228 : = Backend::return_statement (ctx.fn, result_expr, UNDEF_LOCATION);
1676 228 : ctx.add_statement (return_statement);
1677 228 : }
1678 :
1679 : void
1680 582 : sizeof_handler (IntrinsicCtx &ctx)
1681 : {
1682 : // size_of has _zero_ parameters its parameter is the generic one
1683 :
1684 : // get the template parameter type tree fn size_of<T>();
1685 582 : auto ¶m_mapping = ctx.fntype.get_substs ().at (0);
1686 582 : const auto param_tyty = param_mapping.get_param_ty ();
1687 582 : auto resolved_tyty = param_tyty->resolve ();
1688 582 : tree template_parameter_type
1689 582 : = TyTyResolveCompile::compile (ctx.inner (), resolved_tyty);
1690 :
1691 582 : tree size_expr = TYPE_SIZE_UNIT (template_parameter_type);
1692 582 : auto return_statement
1693 582 : = Backend::return_statement (ctx.fn, size_expr, UNDEF_LOCATION);
1694 582 : ctx.add_statement (return_statement);
1695 582 : }
1696 :
1697 : /**
1698 : * pub fn size_of_val<T: ?Sized>(_: *const T) -> usize;
1699 : */
1700 : void
1701 11 : size_of_val_handler (IntrinsicCtx &ctx)
1702 : {
1703 11 : auto locus = ctx.fntype.get_locus ();
1704 :
1705 11 : auto &__param = ctx.param_vars.at (0);
1706 11 : rust_assert (ctx.param_vars.size () == 1);
1707 :
1708 11 : auto ¶m_mapping = ctx.fntype.get_substs ().at (0);
1709 11 : const auto param_tyty = param_mapping.get_param_ty ();
1710 11 : auto resolved_tyty = param_tyty->resolve ();
1711 11 : tree template_parameter_type
1712 11 : = TyTyResolveCompile::compile (ctx.inner (), resolved_tyty);
1713 :
1714 11 : tree size_expr = NULL_TREE;
1715 :
1716 11 : tree param = Backend::var_expression (__param, UNDEF_LOCATION);
1717 11 : tree param_ty = TREE_TYPE (param);
1718 11 : if (RS_DST_FLAG_P (param_ty))
1719 : {
1720 8 : tree data_field = TYPE_FIELDS (param_ty);
1721 8 : tree meta_field = DECL_CHAIN (data_field);
1722 8 : tree meta_field_expr
1723 8 : = build3_loc (locus, COMPONENT_REF, TREE_TYPE (meta_field), param,
1724 : meta_field, NULL_TREE);
1725 :
1726 8 : TyTy::BaseType *inner_dst = get_inner_dst (resolved_tyty);
1727 8 : tree tail_size_expr = NULL_TREE;
1728 8 : if (inner_dst->get_kind () == TyTy::TypeKind::SLICE
1729 8 : || inner_dst->get_kind () == TyTy::TypeKind::STR)
1730 : {
1731 5 : tree elem_type = NULL_TREE;
1732 5 : if (inner_dst->get_kind () == TyTy::TypeKind::SLICE)
1733 : {
1734 4 : auto slice_tyty = static_cast<TyTy::SliceType *> (inner_dst);
1735 4 : elem_type
1736 4 : = TyTyResolveCompile::compile (ctx.inner (),
1737 4 : slice_tyty->get_element_type ());
1738 : }
1739 : else
1740 1 : elem_type = char_type_node;
1741 :
1742 5 : tree elem_size = TYPE_SIZE_UNIT (elem_type);
1743 5 : tail_size_expr
1744 5 : = build2_loc (locus, MULT_EXPR, size_type_node,
1745 : fold_convert_loc (locus, size_type_node,
1746 : meta_field_expr),
1747 : fold_convert_loc (locus, size_type_node, elem_size));
1748 : }
1749 3 : else if (inner_dst->get_kind () == TyTy::TypeKind::DYNAMIC)
1750 : {
1751 3 : tree vtable_ptr_ty = TREE_TYPE (meta_field_expr);
1752 3 : tree vtable_ty = TREE_TYPE (vtable_ptr_ty);
1753 :
1754 3 : tree vtable_ref
1755 3 : = build1_loc (locus, INDIRECT_REF, vtable_ty, meta_field_expr);
1756 :
1757 3 : tree vtable_field_0 = TYPE_FIELDS (vtable_ty);
1758 3 : tree vtable_field_size = DECL_CHAIN (vtable_field_0);
1759 3 : rust_assert (vtable_field_size != NULL_TREE);
1760 :
1761 3 : tail_size_expr
1762 3 : = build3_loc (locus, COMPONENT_REF, TREE_TYPE (vtable_field_size),
1763 : vtable_ref, vtable_field_size, NULL_TREE);
1764 : }
1765 : else
1766 : {
1767 0 : rust_unreachable ();
1768 : }
1769 8 : if (resolved_tyty->get_kind () == TyTy::TypeKind::ADT)
1770 3 : size_expr = build2_loc (locus, PLUS_EXPR, size_type_node,
1771 : fold_convert_loc (locus, size_type_node,
1772 3 : TYPE_SIZE_UNIT (
1773 : template_parameter_type)),
1774 : tail_size_expr);
1775 : else
1776 : size_expr = tail_size_expr;
1777 : }
1778 : else
1779 3 : size_expr = TYPE_SIZE_UNIT (template_parameter_type);
1780 :
1781 11 : auto return_statement
1782 11 : = Backend::return_statement (ctx.fn, size_expr, UNDEF_LOCATION);
1783 11 : ctx.add_statement (return_statement);
1784 11 : }
1785 :
1786 : /**
1787 : * pub fn min_align_of<T>() -> usize;
1788 : */
1789 : void
1790 2 : min_align_of_handler (IntrinsicCtx &ctx)
1791 : {
1792 : // min_align_of has _zero_ parameters its parameter is the generic one
1793 :
1794 : // get the template parameter type tree fn min_align_of<T>();
1795 2 : auto ¶m_mapping = ctx.fntype.get_substs ().at (0);
1796 2 : const auto param_tyty = param_mapping.get_param_ty ();
1797 2 : auto resolved_tyty = param_tyty->resolve ();
1798 2 : tree template_parameter_type
1799 2 : = TyTyResolveCompile::compile (ctx.inner (), resolved_tyty);
1800 :
1801 2 : tree align_expr
1802 2 : = build_int_cst (size_type_node, TYPE_ALIGN_UNIT (template_parameter_type));
1803 :
1804 2 : auto return_statement
1805 2 : = Backend::return_statement (ctx.fn, align_expr, UNDEF_LOCATION);
1806 2 : ctx.add_statement (return_statement);
1807 2 : }
1808 :
1809 : /**
1810 : * pub fn min_align_of_val<T: ?Sized>(_: *const T) -> usize;
1811 : */
1812 : void
1813 7 : min_align_of_val_handler (IntrinsicCtx &ctx)
1814 : {
1815 7 : auto locus = ctx.fntype.get_locus ();
1816 :
1817 7 : auto &__param = ctx.param_vars.at (0);
1818 7 : rust_assert (ctx.param_vars.size () == 1);
1819 :
1820 7 : auto ¶m_mapping = ctx.fntype.get_substs ().at (0);
1821 7 : const auto param_tyty = param_mapping.get_param_ty ();
1822 7 : auto resolved_tyty = param_tyty->resolve ();
1823 7 : tree template_parameter_type
1824 7 : = TyTyResolveCompile::compile (ctx.inner (), resolved_tyty);
1825 :
1826 7 : tree align_expr = NULL_TREE;
1827 7 : tree param = Backend::var_expression (__param, UNDEF_LOCATION);
1828 7 : tree param_ty = TREE_TYPE (param);
1829 7 : if (RS_DST_FLAG_P (param_ty))
1830 : {
1831 4 : tree data_field = TYPE_FIELDS (param_ty);
1832 4 : tree meta_field = DECL_CHAIN (data_field);
1833 4 : tree meta_field_expr
1834 4 : = build3_loc (locus, COMPONENT_REF, TREE_TYPE (meta_field), param,
1835 : meta_field, NULL_TREE);
1836 :
1837 4 : TyTy::BaseType *inner_dst = get_inner_dst (resolved_tyty);
1838 4 : tree tail_align_expr = NULL_TREE;
1839 :
1840 4 : if (inner_dst->get_kind () == TyTy::TypeKind::SLICE
1841 4 : || inner_dst->get_kind () == TyTy::TypeKind::STR)
1842 : {
1843 2 : tree elem_type = NULL_TREE;
1844 2 : if (inner_dst->get_kind () == TyTy::TypeKind::SLICE)
1845 : {
1846 1 : auto slice_tyty = static_cast<TyTy::SliceType *> (inner_dst);
1847 1 : elem_type
1848 1 : = TyTyResolveCompile::compile (ctx.inner (),
1849 1 : slice_tyty->get_element_type ());
1850 : }
1851 : else
1852 1 : elem_type = char_type_node;
1853 :
1854 2 : tail_align_expr
1855 2 : = build_int_cst (size_type_node, TYPE_ALIGN_UNIT (elem_type));
1856 : }
1857 2 : else if (inner_dst->get_kind () == TyTy::TypeKind::DYNAMIC)
1858 : {
1859 2 : tree vtable_ptr_ty = TREE_TYPE (meta_field_expr);
1860 2 : tree vtable_ty = TREE_TYPE (vtable_ptr_ty);
1861 :
1862 2 : tree vtable_ref
1863 2 : = build1_loc (locus, INDIRECT_REF, vtable_ty, meta_field_expr);
1864 :
1865 2 : tree vtable_field_0 = TYPE_FIELDS (vtable_ty);
1866 2 : tree vtable_field_1 = DECL_CHAIN (vtable_field_0);
1867 2 : tree vtable_field_align = DECL_CHAIN (vtable_field_1);
1868 2 : rust_assert (vtable_field_align != NULL_TREE);
1869 :
1870 2 : tail_align_expr
1871 2 : = build3_loc (locus, COMPONENT_REF, TREE_TYPE (vtable_field_align),
1872 : vtable_ref, vtable_field_align, NULL_TREE);
1873 : }
1874 : else
1875 : {
1876 0 : rust_unreachable ();
1877 : }
1878 :
1879 4 : if (resolved_tyty->get_kind () == TyTy::TypeKind::ADT)
1880 : {
1881 0 : tree base_align_expr
1882 0 : = build_int_cst (size_type_node,
1883 0 : TYPE_ALIGN_UNIT (template_parameter_type));
1884 0 : align_expr = build2_loc (locus, MAX_EXPR, size_type_node,
1885 : base_align_expr, tail_align_expr);
1886 : }
1887 : else
1888 : align_expr = tail_align_expr;
1889 : }
1890 : else
1891 3 : align_expr = build_int_cst (size_type_node,
1892 3 : TYPE_ALIGN_UNIT (template_parameter_type));
1893 :
1894 7 : auto return_statement
1895 7 : = Backend::return_statement (ctx.fn, align_expr, UNDEF_LOCATION);
1896 7 : ctx.add_statement (return_statement);
1897 7 : }
1898 :
1899 : void
1900 88 : offset (IntrinsicCtx &ctx)
1901 : {
1902 : // offset intrinsic has two params dst pointer and offset isize
1903 :
1904 88 : auto &dst_param = ctx.param_vars.at (0);
1905 88 : auto &size_param = ctx.param_vars.at (1);
1906 88 : rust_assert (ctx.param_vars.size () == 2);
1907 :
1908 88 : tree dst = Backend::var_expression (dst_param, UNDEF_LOCATION);
1909 88 : tree size = Backend::var_expression (size_param, UNDEF_LOCATION);
1910 88 : tree pointer_offset_expr = pointer_offset_expression (dst, size, ctx.loc);
1911 88 : auto return_statement
1912 88 : = Backend::return_statement (ctx.fn, pointer_offset_expr, UNDEF_LOCATION);
1913 88 : ctx.add_statement (return_statement);
1914 88 : }
1915 :
1916 : /**
1917 : * pub const fn bswap<T: Copy>(x: T) -> T;
1918 : */
1919 : void
1920 9 : bswap_handler (IntrinsicCtx &ctx)
1921 : {
1922 9 : auto locus = ctx.fntype.get_locus ();
1923 :
1924 9 : auto &x_param = ctx.param_vars.at (0);
1925 9 : rust_assert (ctx.param_vars.size () == 1);
1926 :
1927 9 : auto *monomorphized_type
1928 9 : = ctx.fntype.get_substs ().at (0).get_param_ty ()->resolve ();
1929 :
1930 9 : auto call_locus = ctx.get_mappings ().lookup_location (ctx.fntype.get_ref ());
1931 9 : check_for_basic_integer_type ("bswap", call_locus, monomorphized_type);
1932 :
1933 9 : tree template_parameter_type
1934 9 : = TyTyResolveCompile::compile (ctx.inner (), monomorphized_type);
1935 :
1936 9 : tree size_expr = TYPE_SIZE_UNIT (template_parameter_type);
1937 9 : unsigned HOST_WIDE_INT size = TREE_INT_CST_LOW (size_expr);
1938 :
1939 9 : auto expr_x = Backend::var_expression (x_param, locus);
1940 9 : tree result = NULL_TREE;
1941 :
1942 9 : if (size == 1)
1943 : {
1944 : result = expr_x;
1945 : }
1946 : else
1947 : {
1948 8 : tree target_type = NULL_TREE;
1949 8 : const char *builtin_name = nullptr;
1950 8 : switch (size)
1951 : {
1952 2 : case 2:
1953 2 : builtin_name = "__builtin_bswap16";
1954 2 : target_type = uint16_type_node;
1955 2 : break;
1956 2 : case 4:
1957 2 : builtin_name = "__builtin_bswap32";
1958 2 : target_type = uint32_type_node;
1959 2 : break;
1960 2 : case 8:
1961 2 : builtin_name = "__builtin_bswap64";
1962 2 : target_type = uint64_type_node;
1963 2 : break;
1964 2 : case 16:
1965 2 : builtin_name = "__builtin_bswap128";
1966 2 : target_type = uint128_type_node;
1967 2 : break;
1968 0 : default:
1969 0 : rust_unreachable ();
1970 : return;
1971 : }
1972 :
1973 8 : auto bswap_raw
1974 8 : = BuiltinsContext::get ().lookup_simple_builtin (builtin_name);
1975 :
1976 8 : if (bswap_raw)
1977 : {
1978 8 : tree bswap_fn = build_fold_addr_expr_loc (locus, *bswap_raw);
1979 :
1980 8 : auto bswap_x = build1 (CONVERT_EXPR, target_type, expr_x);
1981 :
1982 8 : auto bswap_call
1983 8 : = Backend::call_expression (bswap_fn, {bswap_x}, NULL_TREE, locus);
1984 :
1985 8 : result = build1 (CONVERT_EXPR, template_parameter_type, bswap_call);
1986 : }
1987 : else
1988 : {
1989 0 : bswap_raw = BuiltinsContext::get ().lookup_simple_builtin (
1990 0 : "__builtin_bswap64");
1991 0 : rust_assert (bswap_raw);
1992 :
1993 0 : tree bswap_fn = build_fold_addr_expr_loc (locus, *bswap_raw);
1994 :
1995 0 : tree tmp_in_stmt = error_mark_node;
1996 0 : Bvariable *in_var
1997 0 : = Backend::temporary_variable (ctx.fn, NULL_TREE,
1998 : template_parameter_type, expr_x,
1999 : true, locus, &tmp_in_stmt);
2000 0 : ctx.add_statement (tmp_in_stmt);
2001 :
2002 0 : tree addr_x
2003 0 : = build_fold_addr_expr_loc (locus, in_var->get_tree (locus));
2004 0 : tree u64_ptr_type = build_pointer_type (uint64_type_node);
2005 :
2006 0 : tree low_ptr = fold_convert (u64_ptr_type, addr_x);
2007 0 : tree low = build_fold_indirect_ref_loc (locus, low_ptr);
2008 :
2009 0 : tree high_ptr = fold_build2 (POINTER_PLUS_EXPR, u64_ptr_type, low_ptr,
2010 : size_int (8));
2011 0 : tree high = build_fold_indirect_ref_loc (locus, high_ptr);
2012 :
2013 0 : auto new_high
2014 0 : = Backend::call_expression (bswap_fn, {low}, NULL_TREE, locus);
2015 0 : auto new_low
2016 0 : = Backend::call_expression (bswap_fn, {high}, NULL_TREE, locus);
2017 :
2018 0 : tree tmp_stmt = error_mark_node;
2019 0 : Bvariable *result_var
2020 0 : = Backend::temporary_variable (ctx.fn, NULL_TREE,
2021 : template_parameter_type, NULL_TREE,
2022 : true, locus, &tmp_stmt);
2023 0 : ctx.add_statement (tmp_stmt);
2024 :
2025 0 : tree addr_res
2026 0 : = build_fold_addr_expr_loc (locus, result_var->get_tree (locus));
2027 0 : tree res_ptr = fold_convert (u64_ptr_type, addr_res);
2028 :
2029 0 : tree store_low
2030 0 : = build2 (MODIFY_EXPR, void_type_node,
2031 : build_fold_indirect_ref_loc (locus, res_ptr), new_low);
2032 0 : ctx.add_statement (store_low);
2033 :
2034 0 : tree res_high_ptr = fold_build2 (POINTER_PLUS_EXPR, u64_ptr_type,
2035 : res_ptr, size_int (8));
2036 0 : tree store_high
2037 0 : = build2 (MODIFY_EXPR, void_type_node,
2038 : build_fold_indirect_ref_loc (locus, res_high_ptr),
2039 : new_high);
2040 0 : ctx.add_statement (store_high);
2041 :
2042 0 : result = result_var->get_tree (locus);
2043 : }
2044 : }
2045 :
2046 9 : auto return_statement = Backend::return_statement (ctx.fn, result, locus);
2047 :
2048 9 : TREE_READONLY (result) = 1;
2049 :
2050 9 : ctx.add_statement (return_statement);
2051 : }
2052 :
2053 : void
2054 253 : ctlz_handler (IntrinsicCtx &ctx)
2055 : {
2056 253 : return inner::ctlz_handler (ctx, false);
2057 : }
2058 :
2059 : void
2060 169 : ctlz_nonzero_handler (IntrinsicCtx &ctx)
2061 : {
2062 169 : return inner::ctlz_handler (ctx, true);
2063 : }
2064 :
2065 : void
2066 169 : cttz_handler (IntrinsicCtx &ctx)
2067 : {
2068 169 : return inner::cttz_handler (ctx, false);
2069 : }
2070 :
2071 : void
2072 113 : cttz_nonzero_handler (IntrinsicCtx &ctx)
2073 : {
2074 113 : return inner::cttz_handler (ctx, true);
2075 : }
2076 :
2077 : /**
2078 : * pub unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
2079 : */
2080 : void
2081 1 : write_bytes_handler (IntrinsicCtx &ctx)
2082 : {
2083 1 : auto locus = ctx.fntype.get_locus ();
2084 :
2085 1 : auto &dst_param = ctx.param_vars.at (0);
2086 1 : auto &val_param = ctx.param_vars.at (1);
2087 1 : auto &count_param = ctx.param_vars.at (2);
2088 1 : rust_assert (ctx.param_vars.size () == 3);
2089 :
2090 1 : auto *monomorphized_type
2091 1 : = ctx.fntype.get_substs ().at (0).get_param_ty ()->resolve ();
2092 :
2093 1 : tree template_parameter_type
2094 1 : = TyTyResolveCompile::compile (ctx.inner (), monomorphized_type);
2095 1 : tree dst_size_expr = TYPE_SIZE_UNIT (template_parameter_type);
2096 1 : rust_assert (dst_size_expr != NULL_TREE);
2097 :
2098 1 : tree expr_dst = Backend::var_expression (dst_param, locus);
2099 1 : tree expr_val = Backend::var_expression (val_param, locus);
2100 1 : tree expr_count = Backend::var_expression (count_param, locus);
2101 :
2102 1 : tree expr_count_final
2103 1 : = fold_build2_loc (locus, MULT_EXPR, size_type_node,
2104 : fold_convert_loc (locus, size_type_node, expr_count),
2105 : fold_convert_loc (locus, size_type_node, dst_size_expr));
2106 :
2107 : // void* memset( void* dest, int ch, size_t count );
2108 1 : auto write_bytes_raw
2109 1 : = BuiltinsContext::get ().lookup_simple_builtin ("__builtin_memset");
2110 1 : rust_assert (write_bytes_raw);
2111 :
2112 1 : tree write_bytes_fn = build_fold_addr_expr_loc (locus, *write_bytes_raw);
2113 1 : tree write_bytes_call
2114 1 : = Backend::call_expression (write_bytes_fn,
2115 : {expr_dst, expr_val, expr_count_final},
2116 : NULL_TREE, locus);
2117 :
2118 1 : ctx.add_statement (write_bytes_call);
2119 :
2120 1 : TREE_READONLY (ctx.fn) = 0;
2121 1 : }
2122 :
2123 : /**
2124 : * pub fn arith_offset<T>(dst: *const T, offset: isize) -> *const T;
2125 : */
2126 : void
2127 1 : arith_offset_handler (IntrinsicCtx &ctx)
2128 : {
2129 1 : auto locus = ctx.fntype.get_locus ();
2130 :
2131 1 : auto &dst_param = ctx.param_vars.at (0);
2132 1 : auto &size_param = ctx.param_vars.at (1);
2133 1 : rust_assert (ctx.param_vars.size () == 2);
2134 :
2135 1 : tree dst = Backend::var_expression (dst_param, locus);
2136 1 : tree size = Backend::var_expression (size_param, locus);
2137 1 : tree pointer_offset_expr = pointer_offset_expression (dst, size, ctx.loc);
2138 1 : auto return_statement
2139 1 : = Backend::return_statement (ctx.fn, pointer_offset_expr, locus);
2140 1 : ctx.add_statement (return_statement);
2141 1 : }
2142 :
2143 : void
2144 1 : exact_div_handler (IntrinsicCtx &ctx)
2145 : {
2146 1 : auto lhs_param = ctx.param_vars.at (0);
2147 1 : auto rhs_param = ctx.param_vars.at (1);
2148 :
2149 1 : auto lhs = Backend::var_expression (lhs_param, ctx.loc);
2150 1 : auto rhs = Backend::var_expression (rhs_param, ctx.loc);
2151 :
2152 1 : auto div_expr
2153 1 : = build2_loc (ctx.loc, EXACT_DIV_EXPR, TREE_TYPE (lhs), lhs, rhs);
2154 :
2155 1 : auto return_stmt = Backend::return_statement (ctx.fn, div_expr, ctx.loc);
2156 :
2157 1 : ctx.add_statement (return_stmt);
2158 1 : }
2159 :
2160 : void
2161 1 : nontemporal_store (IntrinsicCtx &ctx)
2162 : {
2163 : // https://doc.rust-lang.org/std/intrinsics/fn.nontemporal_store.html
2164 : // Equivalent to ptr.write(value), but with an extra performance hint.
2165 : // AFAICT, GCC currently has no way to indicate that a write like this
2166 : // *should* be nontemporal - there are no tree attributes for it, and the code
2167 : // to check whether a store is nontemporal (nontemporal_store_p in
2168 : // tree-ssa-loop-prefetch.cc) checks manually if the store upholds certain
2169 : // conditions - it does not account for a flag on the `tree` or anything like
2170 : // that.
2171 :
2172 1 : return move_val_init (ctx);
2173 : }
2174 :
2175 : void
2176 7 : float_to_int_unchecked (IntrinsicCtx &ctx)
2177 : {
2178 7 : auto &dst_param = ctx.fntype.get_substs ().at (1);
2179 7 : const auto dst_ty = dst_param.get_param_ty ();
2180 7 : auto int_ty_resolved = dst_ty->resolve ();
2181 :
2182 7 : auto int_ty = TyTyResolveCompile::compile (ctx.inner (), int_ty_resolved);
2183 :
2184 7 : rust_assert (ctx.param_vars.size () == 1);
2185 :
2186 7 : auto &float_param = ctx.param_vars.at (0);
2187 :
2188 7 : tree float_value = Backend::var_expression (float_param, UNDEF_LOCATION);
2189 :
2190 7 : auto convert_expr
2191 7 : = Backend::convert_expression (int_ty, float_value, ctx.loc);
2192 :
2193 7 : auto return_statement
2194 7 : = Backend::return_statement (ctx.fn, convert_expr, ctx.loc);
2195 :
2196 7 : ctx.add_statement (return_statement);
2197 7 : }
2198 :
2199 : void
2200 1 : breakpoint (IntrinsicCtx &ctx)
2201 : {
2202 : // For now, we don't have access to an equivalent of LLVM's debugtrap inst. We
2203 : // work around the problem by emitting a regular trap and we hope that the
2204 : // user's debugger can catch it :) Ideally, this would emit an `int3` on x86
2205 : // rather than a `ud2`
2206 :
2207 1 : auto trap = BuiltinsContext::get ().lookup_simple_builtin ("__builtin_trap");
2208 1 : rust_assert (trap);
2209 :
2210 1 : tree trap_call = build_call_expr_loc (ctx.loc, *trap, 0);
2211 :
2212 1 : ctx.add_statement (trap_call);
2213 :
2214 1 : TREE_READONLY (ctx.fn) = 0;
2215 1 : TREE_USED (ctx.fn) = 1;
2216 1 : TREE_SIDE_EFFECTS (ctx.fn) = 1;
2217 1 : }
2218 :
2219 : void
2220 7 : ptr_offset_from (IntrinsicCtx &ctx)
2221 : {
2222 : // Per offset_from's documentation
2223 : // https://doc.rust-lang.org/std/primitive.pointer.html#method.offset_from
2224 : //
2225 : // This calculates the distance between two pointers in terms of units of `T`
2226 : // - the distance in bytes divided by `size_of::<T>()`
2227 :
2228 7 : auto ¶m_mapping = ctx.fntype.get_substs ().at (0);
2229 7 : auto param_tyty = param_mapping.get_param_ty ();
2230 7 : auto resolved_tyty = param_tyty->resolve ();
2231 7 : tree ty = TyTyResolveCompile::compile (ctx.inner (), resolved_tyty);
2232 :
2233 7 : auto pointee_size_unsigned = TYPE_SIZE_UNIT (ty);
2234 7 : auto pointee_size
2235 7 : = Backend::convert_expression (signed_size_type_node, pointee_size_unsigned,
2236 : ctx.loc);
2237 :
2238 7 : auto addr = Backend::var_expression (ctx.param_vars[0], ctx.loc);
2239 7 : auto base = Backend::var_expression (ctx.param_vars[1], ctx.loc);
2240 :
2241 : // Convert the pointers to integers
2242 7 : auto addr_int
2243 7 : = Backend::convert_expression (signed_size_type_node, addr, ctx.loc);
2244 7 : auto base_int
2245 7 : = Backend::convert_expression (signed_size_type_node, base, ctx.loc);
2246 :
2247 7 : auto byte_diff = Backend::arithmetic_or_logical_expression (
2248 : ArithmeticOrLogicalOperator::SUBTRACT, addr_int, base_int, ctx.loc);
2249 :
2250 7 : auto result = build2_loc (ctx.loc, EXACT_DIV_EXPR, signed_size_type_node,
2251 : byte_diff, pointee_size);
2252 :
2253 7 : auto return_stmt = Backend::return_statement (ctx.fn, result, ctx.loc);
2254 :
2255 7 : ctx.add_statement (return_stmt);
2256 7 : }
2257 :
2258 : } // namespace handlers
2259 : } // namespace Compile
2260 : } // namespace Rust
|