Debian Codegraph @

gcc/rust/backend/rust-compile-intrinsic.cc

raw
1 // This file is part of GCC.
2
3 // GCC is free software; you can redistribute it and/or modify it under
4 // the terms of the GNU General Public License as published by the Free
5 // Software Foundation; either version 3, or (at your option) any later
6 // version.
7
8 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
9 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 // for more details.
12
13 // You should have received a copy of the GNU General Public License
14 // along with GCC; see the file COPYING3. If not see
15 // <http://www.gnu.org/licenses/>.
16
17 #include "rust-compile-intrinsic.h"
18 #include "rust-compile-context.h"
19 #include "rust-compile-type.h"
20 #include "rust-compile-fnparam.h"
21 #include "rust-builtins.h"
22 #include "rust-diagnostics.h"
23 #include "rust-location.h"
24 #include "rust-constexpr.h"
25 #include "rust-session-manager.h"
26 #include "rust-tree.h"
27 #include "tree-core.h"
28 #include "rust-gcc.h"
29 #include "fold-const.h"
30 #include "langhooks.h"
31 #include "rust-constexpr.h"
32
33 // declaration taken from "stringpool.h"
34 // the get_identifier macro causes compilation issues
35 extern tree
36 get_identifier (const char *);
37
38 namespace Rust {
39 namespace Compile {
40
41 static bool
42 is_basic_integer_type (TyTy::BaseType *type)
43 {
44 switch (type->get_kind ())
45 {
46 case TyTy::INT:
47 case TyTy::UINT:
48 case TyTy::USIZE:
49 case TyTy::ISIZE:
50 return true;
51 default:
52 return false;
53 break;
54 }
55 }
56
57 static bool
58 check_for_basic_integer_type (const std::string &intrinsic_str,
59 location_t locus, TyTy::BaseType *type)
60 {
61 auto is_basic_integer = is_basic_integer_type (type);
62 if (!is_basic_integer)
63 {
64 rust_error_at (
65 locus,
66 "%s intrinsics can only be used with basic integer types (got %qs)",
67 intrinsic_str.c_str (), type->get_name ().c_str ());
68 }
69
70 return is_basic_integer;
71 }
72
73 static tree
74 offset_handler (Context *ctx, TyTy::FnType *fntype);
75 static tree
76 sizeof_handler (Context *ctx, TyTy::FnType *fntype);
77 static tree
78 transmute_handler (Context *ctx, TyTy::FnType *fntype);
79 static tree
80 rotate_handler (Context *ctx, TyTy::FnType *fntype, tree_code op);
81 static tree
82 wrapping_op_handler_inner (Context *ctx, TyTy::FnType *fntype, tree_code op);
83 static tree
84 op_with_overflow_inner (Context *ctx, TyTy::FnType *fntype, tree_code op);
85 static tree
86 uninit_handler (Context *ctx, TyTy::FnType *fntype);
87 static tree
88 move_val_init_handler (Context *ctx, TyTy::FnType *fntype);
89 static tree
90 assume_handler (Context *ctx, TyTy::FnType *fntype);
91 static tree
92 discriminant_value_handler (Context *ctx, TyTy::FnType *fntype);
93 static tree
94 variant_count_handler (Context *ctx, TyTy::FnType *fntype);
95
96 enum class Prefetch
97 {
98 Read,
99 Write
100 };
101
102 static tree
103 prefetch_data_handler (Context *ctx, TyTy::FnType *fntype, Prefetch kind);
104
105 static inline tree
106 rotate_left_handler (Context *ctx, TyTy::FnType *fntype)
107 {
108 return rotate_handler (ctx, fntype, LROTATE_EXPR);
109 }
110 static inline tree
111 rotate_right_handler (Context *ctx, TyTy::FnType *fntype)
112 {
113 return rotate_handler (ctx, fntype, RROTATE_EXPR);
114 }
115
116 const static std::function<tree (Context *, TyTy::FnType *)>
117 wrapping_op_handler (tree_code op)
118 {
119 return [op] (Context *ctx, TyTy::FnType *fntype) {
120 return wrapping_op_handler_inner (ctx, fntype, op);
121 };
122 }
123
124 const static std::function<tree (Context *, TyTy::FnType *)>
125 op_with_overflow (tree_code op)
126 {
127 return [op] (Context *ctx, TyTy::FnType *fntype) {
128 return op_with_overflow_inner (ctx, fntype, op);
129 };
130 }
131
132 static inline tree
133 prefetch_read_data (Context *ctx, TyTy::FnType *fntype)
134 {
135 return prefetch_data_handler (ctx, fntype, Prefetch::Read);
136 }
137 static inline tree
138 prefetch_write_data (Context *ctx, TyTy::FnType *fntype)
139 {
140 return prefetch_data_handler (ctx, fntype, Prefetch::Write);
141 }
142
143 static tree
144 atomic_store_handler_inner (Context *ctx, TyTy::FnType *fntype, int ordering);
145 static tree
146 atomic_load_handler_inner (Context *ctx, TyTy::FnType *fntype, int ordering);
147
148 static inline std::function<tree (Context *, TyTy::FnType *)>
149 atomic_store_handler (int ordering)
150 {
151 return [ordering] (Context *ctx, TyTy::FnType *fntype) {
152 return atomic_store_handler_inner (ctx, fntype, ordering);
153 };
154 }
155
156 static inline std::function<tree (Context *, TyTy::FnType *)>
157 atomic_load_handler (int ordering)
158 {
159 return [ordering] (Context *ctx, TyTy::FnType *fntype) {
160 return atomic_load_handler_inner (ctx, fntype, ordering);
161 };
162 }
163
164 static inline tree
165 unchecked_op_inner (Context *ctx, TyTy::FnType *fntype, tree_code op);
166
167 const static std::function<tree (Context *, TyTy::FnType *)>
168 unchecked_op_handler (tree_code op)
169 {
170 return [op] (Context *ctx, TyTy::FnType *fntype) {
171 return unchecked_op_inner (ctx, fntype, op);
172 };
173 }
174
175 static inline tree
176 copy_handler_inner (Context *ctx, TyTy::FnType *fntype, bool overlaps);
177
178 const static std::function<tree (Context *, TyTy::FnType *)>
179 copy_handler (bool overlaps)
180 {
181 return [overlaps] (Context *ctx, TyTy::FnType *fntype) {
182 return copy_handler_inner (ctx, fntype, overlaps);
183 };
184 }
185
186 static inline tree
187 expect_handler_inner (Context *ctx, TyTy::FnType *fntype, bool likely);
188
189 const static std::function<tree (Context *, TyTy::FnType *)>
190 expect_handler (bool likely)
191 {
192 return [likely] (Context *ctx, TyTy::FnType *fntype) {
193 return expect_handler_inner (ctx, fntype, likely);
194 };
195 }
196
197 static tree
198 try_handler_inner (Context *ctx, TyTy::FnType *fntype, bool is_new_api);
199
200 const static std::function<tree (Context *, TyTy::FnType *)>
201 try_handler (bool is_new_api)
202 {
203 return [is_new_api] (Context *ctx, TyTy::FnType *fntype) {
204 return try_handler_inner (ctx, fntype, is_new_api);
205 };
206 }
207
208 inline tree
209 sorry_handler (Context *ctx, TyTy::FnType *fntype)
210 {
211 rust_sorry_at (fntype->get_locus (), "intrinsic %qs is not yet implemented",
212 fntype->get_identifier ().c_str ());
213
214 return error_mark_node;
215 }
216
217 static const std::map<std::string,
218 std::function<tree (Context *, TyTy::FnType *)>>
219 generic_intrinsics
220 = {{"offset", offset_handler},
221 {"size_of", sizeof_handler},
222 {"transmute", transmute_handler},
223 {"rotate_left", rotate_left_handler},
224 {"rotate_right", rotate_right_handler},
225 {"wrapping_add", wrapping_op_handler (PLUS_EXPR)},
226 {"wrapping_sub", wrapping_op_handler (MINUS_EXPR)},
227 {"wrapping_mul", wrapping_op_handler (MULT_EXPR)},
228 {"add_with_overflow", op_with_overflow (PLUS_EXPR)},
229 {"sub_with_overflow", op_with_overflow (MINUS_EXPR)},
230 {"mul_with_overflow", op_with_overflow (MULT_EXPR)},
231 {"copy", copy_handler (true)},
232 {"copy_nonoverlapping", copy_handler (false)},
233 {"prefetch_read_data", prefetch_read_data},
234 {"prefetch_write_data", prefetch_write_data},
235 {"atomic_store_seqcst", atomic_store_handler (__ATOMIC_SEQ_CST)},
236 {"atomic_store_release", atomic_store_handler (__ATOMIC_RELEASE)},
237 {"atomic_store_relaxed", atomic_store_handler (__ATOMIC_RELAXED)},
238 {"atomic_store_unordered", atomic_store_handler (__ATOMIC_RELAXED)},
239 {"atomic_load_seqcst", atomic_load_handler (__ATOMIC_SEQ_CST)},
240 {"atomic_load_acquire", atomic_load_handler (__ATOMIC_ACQUIRE)},
241 {"atomic_load_relaxed", atomic_load_handler (__ATOMIC_RELAXED)},
242 {"atomic_load_unordered", atomic_load_handler (__ATOMIC_RELAXED)},
243 {"unchecked_add", unchecked_op_handler (PLUS_EXPR)},
244 {"unchecked_sub", unchecked_op_handler (MINUS_EXPR)},
245 {"unchecked_mul", unchecked_op_handler (MULT_EXPR)},
246 {"unchecked_div", unchecked_op_handler (TRUNC_DIV_EXPR)},
247 {"unchecked_rem", unchecked_op_handler (TRUNC_MOD_EXPR)},
248 {"unchecked_shl", unchecked_op_handler (LSHIFT_EXPR)},
249 {"unchecked_shr", unchecked_op_handler (RSHIFT_EXPR)},
250 {"uninit", uninit_handler},
251 {"move_val_init", move_val_init_handler},
252 {"likely", expect_handler (true)},
253 {"unlikely", expect_handler (false)},
254 {"assume", assume_handler},
255 {"try", try_handler (false)},
256 {"catch_unwind", try_handler (true)},
257 {"discriminant_value", discriminant_value_handler},
258 {"variant_count", variant_count_handler}};
259
260 Intrinsics::Intrinsics (Context *ctx) : ctx (ctx) {}
261
262 /**
263 * Returns a FUNC_DECL corresponding to the intrinsic function FNTYPE. If a
264 * corresponding builtin exists, returns it. If not, search in the generic
265 * intrinsics declared and delegate the return to the corresponding handler.
266 *
267 * @param fntype The Rust function type that should be implemented by the
268 * compiler
269 */
270 tree
271 Intrinsics::compile (TyTy::FnType *fntype)
272 {
273 rust_assert (fntype->get_abi () == ABI::INTRINSIC);
274
275 tree builtin = error_mark_node;
276 BuiltinsContext &builtin_ctx = BuiltinsContext::get ();
277
278 if (builtin_ctx.lookup_simple_builtin (fntype->get_identifier (), &builtin))
279 return builtin;
280
281 // is it an generic builtin?
282 auto it = generic_intrinsics.find (fntype->get_identifier ());
283 if (it != generic_intrinsics.end ())
284 return it->second (ctx, fntype);
285
286 location_t locus = ctx->get_mappings ().lookup_location (fntype->get_ref ());
287 rust_error_at (locus, ErrorCode::E0093,
288 "unrecognized intrinsic function: %qs",
289 fntype->get_identifier ().c_str ());
290
291 return error_mark_node;
292 }
293
294 /**
295 * Items can be forward compiled which means we may not need to invoke this
296 * code. We might also have already compiled this generic function as well.
297 */
298 static bool
299 check_for_cached_intrinsic (Context *ctx, TyTy::FnType *fntype, tree *lookup)
300 {
301 const Resolver::CanonicalPath &canonical_path = fntype->get_ident ().path;
302 std::string asm_name = ctx->mangle_item (fntype, canonical_path);
303 if (ctx->lookup_function_decl (fntype->get_ty_ref (), lookup,
304 fntype->get_id (), fntype, asm_name))
305 {
306 return true;
307 }
308
309 return false;
310 }
311
312 /**
313 * Maybe override the Hir Lookups for the substituions in this context
314 */
315 static void
316 maybe_override_ctx (TyTy::FnType *fntype)
317 {
318 if (fntype->has_substitutions_defined ())
319 fntype->override_context ();
320 }
321
322 /**
323 * Compile and setup a function's parameters
324 */
325 static void
326 compile_fn_params (Context *ctx, TyTy::FnType *fntype, tree fndecl,
327 std::vector<Bvariable *> *compiled_param_variables,
328 std::vector<tree_node *> *compiled_param_types = nullptr)
329 {
330 for (auto &parm : fntype->get_params ())
331 {
332 auto &referenced_param = parm.get_pattern ();
333 auto param_tyty = parm.get_type ();
334 auto compiled_param_type = TyTyResolveCompile::compile (ctx, param_tyty);
335
336 location_t param_locus = referenced_param.get_locus ();
337 Bvariable *compiled_param_var
338 = CompileFnParam::compile (ctx, fndecl, referenced_param,
339 compiled_param_type, param_locus);
340
341 compiled_param_variables->push_back (compiled_param_var);
342 if (compiled_param_types)
343 compiled_param_types->push_back (compiled_param_type);
344 }
345 }
346
347 static tree
348 compile_intrinsic_function (Context *ctx, TyTy::FnType *fntype)
349 {
350 maybe_override_ctx (fntype);
351
352 const Resolver::CanonicalPath &canonical_path = fntype->get_ident ().path;
353
354 tree compiled_fn_type = TyTyResolveCompile::compile (ctx, fntype);
355 std::string ir_symbol_name
356 = canonical_path.get () + fntype->subst_as_string ();
357 std::string asm_name = ctx->mangle_item (fntype, canonical_path);
358
359 unsigned int flags = 0;
360 tree fndecl = Backend::function (compiled_fn_type, ir_symbol_name, asm_name,
361 flags, fntype->get_ident ().locus);
362
363 TREE_PUBLIC (fndecl) = 0;
364 TREE_READONLY (fndecl) = 1;
365 DECL_ARTIFICIAL (fndecl) = 1;
366 DECL_EXTERNAL (fndecl) = 0;
367 DECL_DECLARED_INLINE_P (fndecl) = 1;
368
369 return fndecl;
370 }
371
372 static void
373 enter_intrinsic_block (Context *ctx, tree fndecl,
374 const std::vector<Bvariable *> &vars = {})
375 {
376 tree enclosing_scope = NULL_TREE;
377 location_t start_location = UNDEF_LOCATION;
378 location_t end_location = UNDEF_LOCATION;
379
380 auto block = Backend::block (fndecl, enclosing_scope, vars, start_location,
381 end_location);
382
383 ctx->push_block (block);
384 }
385
386 static void
387 finalize_intrinsic_block (Context *ctx, tree fndecl)
388 {
389 tree bind_tree = ctx->pop_block ();
390
391 gcc_assert (TREE_CODE (bind_tree) == BIND_EXPR);
392
393 DECL_SAVED_TREE (fndecl) = bind_tree;
394
395 ctx->push_function (fndecl);
396
397 DECL_DECLARED_CONSTEXPR_P (fndecl) = 1;
398 maybe_save_constexpr_fundef (fndecl);
399 }
400
401 static tree
402 offset_handler (Context *ctx, TyTy::FnType *fntype)
403 {
404 // offset intrinsic has two params dst pointer and offset isize
405 rust_assert (fntype->get_params ().size () == 2);
406
407 auto fndecl = compile_intrinsic_function (ctx, fntype);
408
409 std::vector<Bvariable *> param_vars;
410 compile_fn_params (ctx, fntype, fndecl, &param_vars);
411
412 auto &dst_param = param_vars.at (0);
413 auto &size_param = param_vars.at (1);
414 rust_assert (param_vars.size () == 2);
415 if (!Backend::function_set_parameters (fndecl, param_vars))
416 return error_mark_node;
417
418 enter_intrinsic_block (ctx, fndecl);
419
420 // BUILTIN offset FN BODY BEGIN
421 tree dst = Backend::var_expression (dst_param, UNDEF_LOCATION);
422 tree size = Backend::var_expression (size_param, UNDEF_LOCATION);
423 tree pointer_offset_expr
424 = pointer_offset_expression (dst, size, BUILTINS_LOCATION);
425 auto return_statement
426 = Backend::return_statement (fndecl, pointer_offset_expr, UNDEF_LOCATION);
427 ctx->add_statement (return_statement);
428 // BUILTIN offset FN BODY END
429
430 finalize_intrinsic_block (ctx, fndecl);
431
432 return fndecl;
433 }
434
435 static tree
436 sizeof_handler (Context *ctx, TyTy::FnType *fntype)
437 {
438 // size_of has _zero_ parameters its parameter is the generic one
439 rust_assert (fntype->get_params ().size () == 0);
440
441 tree lookup = NULL_TREE;
442 if (check_for_cached_intrinsic (ctx, fntype, &lookup))
443 return lookup;
444
445 auto fndecl = compile_intrinsic_function (ctx, fntype);
446
447 // get the template parameter type tree fn size_of<T>();
448 rust_assert (fntype->get_num_substitutions () == 1);
449 auto &param_mapping = fntype->get_substs ().at (0);
450 const TyTy::ParamType *param_tyty = param_mapping.get_param_ty ();
451 TyTy::BaseType *resolved_tyty = param_tyty->resolve ();
452 tree template_parameter_type
453 = TyTyResolveCompile::compile (ctx, resolved_tyty);
454
455 enter_intrinsic_block (ctx, fndecl);
456
457 // BUILTIN size_of FN BODY BEGIN
458 tree size_expr = TYPE_SIZE_UNIT (template_parameter_type);
459 auto return_statement
460 = Backend::return_statement (fndecl, size_expr, UNDEF_LOCATION);
461 ctx->add_statement (return_statement);
462 // BUILTIN size_of FN BODY END
463
464 finalize_intrinsic_block (ctx, fndecl);
465
466 return fndecl;
467 }
468
469 static tree
470 transmute_handler (Context *ctx, TyTy::FnType *fntype)
471 {
472 // transmute intrinsic has one parameter
473 rust_assert (fntype->get_params ().size () == 1);
474
475 tree lookup = NULL_TREE;
476 if (check_for_cached_intrinsic (ctx, fntype, &lookup))
477 return lookup;
478
479 auto fndecl = compile_intrinsic_function (ctx, fntype);
480
481 std::vector<Bvariable *> param_vars;
482 std::vector<tree_node *> compiled_types;
483 compile_fn_params (ctx, fntype, fndecl, &param_vars, &compiled_types);
484
485 if (!Backend::function_set_parameters (fndecl, param_vars))
486 return error_mark_node;
487
488 // param to convert
489 Bvariable *convert_me_param = param_vars.at (0);
490 tree convert_me_expr
491 = Backend::var_expression (convert_me_param, UNDEF_LOCATION);
492
493 // check for transmute pre-conditions
494 tree target_type_expr = TREE_TYPE (DECL_RESULT (fndecl));
495 tree source_type_expr = compiled_types.at (0);
496 tree target_size_expr = TYPE_SIZE (target_type_expr);
497 tree source_size_expr = TYPE_SIZE (source_type_expr);
498 // for some reason, unit types and other zero-sized types return NULL for the
499 // size expressions
500 unsigned HOST_WIDE_INT target_size
501 = target_size_expr ? TREE_INT_CST_LOW (target_size_expr) : 0;
502 unsigned HOST_WIDE_INT source_size
503 = source_size_expr ? TREE_INT_CST_LOW (source_size_expr) : 0;
504
505 // size check for concrete types
506 // TODO(liushuyu): check alignment for pointers; check for dependently-sized
507 // types
508 if (target_size != source_size)
509 {
510 rust_error_at (fntype->get_locus (),
511 "cannot transmute between types of different sizes, or "
512 "dependently-sized types");
513 rust_inform (
514 fntype->get_ident ().locus, "source type: %qs (%lu bits)",
515 fntype->get_params ().at (0).get_type ()->as_string ().c_str (),
516 (unsigned long) source_size);
517 rust_inform (fntype->get_ident ().locus, "target type: %qs (%lu bits)",
518 fntype->get_return_type ()->as_string ().c_str (),
519 (unsigned long) target_size);
520 }
521
522 enter_intrinsic_block (ctx, fndecl);
523
524 // BUILTIN transmute FN BODY BEGIN
525
526 // Return *((orig_type*)&decl) */
527
528 tree t = build_fold_addr_expr_loc (UNKNOWN_LOCATION, convert_me_expr);
529 t = fold_build1_loc (UNKNOWN_LOCATION, NOP_EXPR,
530 build_pointer_type (target_type_expr), t);
531 tree result_expr = build_fold_indirect_ref_loc (UNKNOWN_LOCATION, t);
532
533 auto return_statement
534 = Backend::return_statement (fndecl, result_expr, UNDEF_LOCATION);
535 ctx->add_statement (return_statement);
536 // BUILTIN transmute FN BODY END
537
538 finalize_intrinsic_block (ctx, fndecl);
539
540 return fndecl;
541 }
542
543 static tree
544 rotate_handler (Context *ctx, TyTy::FnType *fntype, tree_code op)
545 {
546 // rotate intrinsic has two parameter
547 rust_assert (fntype->get_params ().size () == 2);
548
549 tree lookup = NULL_TREE;
550 if (check_for_cached_intrinsic (ctx, fntype, &lookup))
551 return lookup;
552
553 auto fndecl = compile_intrinsic_function (ctx, fntype);
554
555 // setup the params
556 std::vector<Bvariable *> param_vars;
557 compile_fn_params (ctx, fntype, fndecl, &param_vars);
558
559 auto &x_param = param_vars.at (0);
560 auto &y_param = param_vars.at (1);
561 rust_assert (param_vars.size () == 2);
562 if (!Backend::function_set_parameters (fndecl, param_vars))
563 return error_mark_node;
564
565 enter_intrinsic_block (ctx, fndecl);
566
567 // BUILTIN rotate FN BODY BEGIN
568 tree x = Backend::var_expression (x_param, UNDEF_LOCATION);
569 tree y = Backend::var_expression (y_param, UNDEF_LOCATION);
570 tree rotate_expr
571 = fold_build2_loc (BUILTINS_LOCATION, op, TREE_TYPE (x), x, y);
572 auto return_statement
573 = Backend::return_statement (fndecl, rotate_expr, UNDEF_LOCATION);
574 ctx->add_statement (return_statement);
575 // BUILTIN rotate FN BODY END
576
577 finalize_intrinsic_block (ctx, fndecl);
578
579 return fndecl;
580 }
581
582 /**
583 * pub fn wrapping_{add, sub, mul}<T>(lhs: T, rhs: T) -> T;
584 */
585 static tree
586 wrapping_op_handler_inner (Context *ctx, TyTy::FnType *fntype, tree_code op)
587 {
588 // wrapping_<op> intrinsics have two parameter
589 rust_assert (fntype->get_params ().size () == 2);
590
591 tree lookup = NULL_TREE;
592 if (check_for_cached_intrinsic (ctx, fntype, &lookup))
593 return lookup;
594
595 auto fndecl = compile_intrinsic_function (ctx, fntype);
596
597 // setup the params
598 std::vector<Bvariable *> param_vars;
599 compile_fn_params (ctx, fntype, fndecl, &param_vars);
600
601 auto &lhs_param = param_vars.at (0);
602 auto &rhs_param = param_vars.at (1);
603
604 if (!Backend::function_set_parameters (fndecl, param_vars))
605 return error_mark_node;
606
607 enter_intrinsic_block (ctx, fndecl);
608
609 // BUILTIN wrapping_<op> FN BODY BEGIN
610 auto lhs = Backend::var_expression (lhs_param, UNDEF_LOCATION);
611 auto rhs = Backend::var_expression (rhs_param, UNDEF_LOCATION);
612
613 // Operations are always wrapping in Rust, as we have -fwrapv enabled by
614 // default. The difference between a wrapping_{add, sub, mul} and a regular
615 // arithmetic operation is that these intrinsics do not panic - they always
616 // carry over.
617 auto wrap_expr = build2 (op, TREE_TYPE (lhs), lhs, rhs);
618
619 auto return_statement
620 = Backend::return_statement (fndecl, wrap_expr, UNDEF_LOCATION);
621 ctx->add_statement (return_statement);
622 // BUILTIN wrapping_<op> FN BODY END
623
624 finalize_intrinsic_block (ctx, fndecl);
625
626 return fndecl;
627 }
628
629 /**
630 * pub fn add_with_overflow<T>(x: T, y: T) -> (T, bool);
631 */
632 static tree
633 op_with_overflow_inner (Context *ctx, TyTy::FnType *fntype, tree_code op)
634 {
635 // wrapping_<op> intrinsics have two parameter
636 rust_assert (fntype->get_params ().size () == 2);
637
638 tree lookup = NULL_TREE;
639 if (check_for_cached_intrinsic (ctx, fntype, &lookup))
640 return lookup;
641
642 auto fndecl = compile_intrinsic_function (ctx, fntype);
643
644 // setup the params
645 std::vector<Bvariable *> param_vars;
646 compile_fn_params (ctx, fntype, fndecl, &param_vars);
647
648 auto &x_param = param_vars.at (0);
649 auto &y_param = param_vars.at (1);
650
651 if (!Backend::function_set_parameters (fndecl, param_vars))
652 return error_mark_node;
653
654 rust_assert (fntype->get_num_substitutions () == 1);
655 auto &param_mapping = fntype->get_substs ().at (0);
656 const TyTy::ParamType *param_tyty = param_mapping.get_param_ty ();
657 TyTy::BaseType *resolved_tyty = param_tyty->resolve ();
658 tree template_parameter_type
659 = TyTyResolveCompile::compile (ctx, resolved_tyty);
660
661 // this should match y as well or we can take it from the TyTy structure
662 tree tmp_stmt = error_mark_node;
663 Bvariable *result_variable
664 = Backend::temporary_variable (fndecl, NULL_TREE, template_parameter_type,
665 NULL_TREE, true /*address_is_taken*/,
666 UNDEF_LOCATION, &tmp_stmt);
667 Bvariable *bool_variable
668 = Backend::temporary_variable (fndecl, NULL_TREE, boolean_type_node,
669 NULL_TREE, true /*address_is_taken*/,
670 UNDEF_LOCATION, &tmp_stmt);
671
672 enter_intrinsic_block (ctx, fndecl, {result_variable, bool_variable});
673
674 // BUILTIN op_with_overflow FN BODY BEGIN
675 auto x = Backend::var_expression (x_param, UNDEF_LOCATION);
676 auto y = Backend::var_expression (y_param, UNDEF_LOCATION);
677
678 tree overflow_builtin = error_mark_node;
679 switch (op)
680 {
681 case PLUS_EXPR:
682 BuiltinsContext::get ().lookup_simple_builtin ("__builtin_add_overflow",
683 &overflow_builtin);
684 break;
685
686 case MINUS_EXPR:
687 BuiltinsContext::get ().lookup_simple_builtin ("__builtin_sub_overflow",
688 &overflow_builtin);
689 break;
690
691 case MULT_EXPR:
692 BuiltinsContext::get ().lookup_simple_builtin ("__builtin_mul_overflow",
693 &overflow_builtin);
694 break;
695
696 default:
697 rust_unreachable ();
698 break;
699 }
700 rust_assert (overflow_builtin != error_mark_node);
701
702 tree bool_decl = bool_variable->get_tree (BUILTINS_LOCATION);
703 tree result_decl = result_variable->get_tree (BUILTINS_LOCATION);
704 tree result_ref = build_fold_addr_expr_loc (BUILTINS_LOCATION, result_decl);
705
706 tree builtin_call = build_call_expr_loc (BUILTINS_LOCATION, overflow_builtin,
707 3, x, y, result_ref);
708
709 tree overflow_assignment
710 = Backend::assignment_statement (bool_decl, builtin_call,
711 BUILTINS_LOCATION);
712
713 ctx->add_statement (overflow_assignment);
714
715 std::vector<tree> vals = {result_decl, bool_decl};
716 tree tuple_type = TREE_TYPE (DECL_RESULT (fndecl));
717 tree result_expr = Backend::constructor_expression (tuple_type, false, vals,
718 -1, UNDEF_LOCATION);
719
720 auto return_statement
721 = Backend::return_statement (fndecl, result_expr, UNDEF_LOCATION);
722 ctx->add_statement (return_statement);
723
724 // BUILTIN wrapping_<op> FN BODY END
725
726 finalize_intrinsic_block (ctx, fndecl);
727
728 return fndecl;
729 }
730
731 /**
732 * fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
733 * fn copy<T>(src: *const T, dst: *mut T, count: usize);
734 */
735 static tree
736 copy_handler_inner (Context *ctx, TyTy::FnType *fntype, bool overlaps)
737 {
738 rust_assert (fntype->get_params ().size () == 3);
739 rust_assert (fntype->get_num_substitutions () == 1);
740
741 tree lookup = NULL_TREE;
742 if (check_for_cached_intrinsic (ctx, fntype, &lookup))
743 return lookup;
744
745 auto fndecl = compile_intrinsic_function (ctx, fntype);
746
747 // Most intrinsic functions are pure - not `copy_nonoverlapping` and `copy`
748 TREE_READONLY (fndecl) = 0;
749 TREE_SIDE_EFFECTS (fndecl) = 1;
750
751 // setup the params
752 std::vector<Bvariable *> param_vars;
753 compile_fn_params (ctx, fntype, fndecl, &param_vars);
754
755 if (!Backend::function_set_parameters (fndecl, param_vars))
756 return error_mark_node;
757
758 enter_intrinsic_block (ctx, fndecl);
759
760 // BUILTIN copy_nonoverlapping BODY BEGIN
761
762 auto src = Backend::var_expression (param_vars[0], UNDEF_LOCATION);
763 auto dst = Backend::var_expression (param_vars[1], UNDEF_LOCATION);
764 auto count = Backend::var_expression (param_vars[2], UNDEF_LOCATION);
765
766 // We want to create the following statement
767 // memcpy(dst, src, size_of::<T>());
768 // so
769 // memcpy(dst, src, size_expr);
770
771 auto *resolved_ty = fntype->get_substs ().at (0).get_param_ty ()->resolve ();
772 auto param_type = TyTyResolveCompile::compile (ctx, resolved_ty);
773
774 tree size_expr
775 = build2 (MULT_EXPR, size_type_node, TYPE_SIZE_UNIT (param_type), count);
776
777 tree memcpy_raw = nullptr;
778 BuiltinsContext::get ().lookup_simple_builtin (overlaps ? "__builtin_memmove"
779 : "__builtin_memcpy",
780 &memcpy_raw);
781 rust_assert (memcpy_raw);
782 auto memcpy = build_fold_addr_expr_loc (UNKNOWN_LOCATION, memcpy_raw);
783
784 auto copy_call = Backend::call_expression (memcpy, {dst, src, size_expr},
785 nullptr, UNDEF_LOCATION);
786
787 ctx->add_statement (copy_call);
788
789 // BUILTIN copy_nonoverlapping BODY END
790
791 finalize_intrinsic_block (ctx, fndecl);
792
793 return fndecl;
794 }
795
796 static tree
797 make_unsigned_long_tree (unsigned long value)
798 {
799 return build_int_cst (integer_type_node, value);
800 }
801
802 static tree
803 prefetch_data_handler (Context *ctx, TyTy::FnType *fntype, Prefetch kind)
804 {
805 rust_assert (fntype->get_params ().size () == 2);
806
807 tree lookup = NULL_TREE;
808 if (check_for_cached_intrinsic (ctx, fntype, &lookup))
809 return lookup;
810
811 auto fndecl = compile_intrinsic_function (ctx, fntype);
812
813 // prefetching isn't pure and shouldn't be discarded after GIMPLE
814 TREE_READONLY (fndecl) = 0;
815 TREE_SIDE_EFFECTS (fndecl) = 1;
816
817 std::vector<Bvariable *> args;
818 compile_fn_params (ctx, fntype, fndecl, &args);
819
820 if (!Backend::function_set_parameters (fndecl, args))
821 return error_mark_node;
822
823 enter_intrinsic_block (ctx, fndecl);
824
825 auto addr = Backend::var_expression (args[0], UNDEF_LOCATION);
826
827 // The core library technically allows you to pass any i32 value as a
828 // locality, but LLVM will then complain if the value cannot be constant
829 // evaluated. For now, we ignore the locality argument and instead always
830 // pass `3` (the most restrictive value). This allows us to still have
831 // prefetch behavior, just not as granular as expected. In future Rust
832 // versions, we hope that prefetch intrinsics will be split up according to
833 // locality, similarly to atomic intrinsics.
834 // The solution is to try and perform constant folding for the locality
835 // argument, or instead of creating a new function definition, modify the call
836 // site directly This has the bad side-effect of creating warnings about
837 // `unused name - locality`, which we hack away here:
838 // TODO: Take care of handling locality properly
839 Backend::var_expression (args[1], UNDEF_LOCATION);
840
841 auto rw_flag = make_unsigned_long_tree (kind == Prefetch::Write ? 1 : 0);
842
843 auto prefetch_raw = NULL_TREE;
844 auto ok = BuiltinsContext::get ().lookup_simple_builtin ("__builtin_prefetch",
845 &prefetch_raw);
846 rust_assert (ok);
847 auto prefetch = build_fold_addr_expr_loc (UNKNOWN_LOCATION, prefetch_raw);
848
849 auto prefetch_call = Backend::call_expression (prefetch,
850 {addr, rw_flag,
851 // locality arg
852 make_unsigned_long_tree (3)},
853 nullptr, UNDEF_LOCATION);
854
855 TREE_READONLY (prefetch_call) = 0;
856 TREE_SIDE_EFFECTS (prefetch_call) = 1;
857
858 ctx->add_statement (prefetch_call);
859
860 finalize_intrinsic_block (ctx, fndecl);
861
862 return fndecl;
863 }
864
865 static std::string
866 build_atomic_builtin_name (const std::string &prefix, location_t locus,
867 TyTy::BaseType *operand_type)
868 {
869 static const std::map<std::string, std::string> allowed_types = {
870 {"i8", "1"}, {"i16", "2"}, {"i32", "4"}, {"i64", "8"},
871 {"i128", "16"}, {"isize", "8"}, {"u8", "1"}, {"u16", "2"},
872 {"u32", "4"}, {"u64", "8"}, {"u128", "16"}, {"usize", "8"},
873 };
874
875 // TODO: Can we maybe get the generic version (atomic_store_n) to work... This
876 // would be so much better
877
878 std::string result = "__" + prefix; // + "n";
879
880 auto type_name = operand_type->get_name ();
881 if (type_name == "usize" || type_name == "isize")
882 {
883 rust_sorry_at (
884 locus, "atomics are not yet available for size types (usize, isize)");
885 return "";
886 }
887
888 if (type_name.at (0) == 'i')
889 {
890 rust_sorry_at (locus, "atomics are not yet supported for signed "
891 "integer types (i8, i16, i32, i64, i128)");
892 return "";
893 }
894
895 auto type_size_str = allowed_types.find (type_name);
896
897 if (!check_for_basic_integer_type ("atomic", locus, operand_type))
898 return "";
899
900 result += type_size_str->second;
901
902 return result;
903 }
904
905 static tree
906 atomic_store_handler_inner (Context *ctx, TyTy::FnType *fntype, int ordering)
907 {
908 rust_assert (fntype->get_params ().size () == 2);
909 rust_assert (fntype->get_num_substitutions () == 1);
910
911 tree lookup = NULL_TREE;
912 if (check_for_cached_intrinsic (ctx, fntype, &lookup))
913 return lookup;
914
915 auto fndecl = compile_intrinsic_function (ctx, fntype);
916
917 // Most intrinsic functions are pure but not the atomic ones
918 TREE_READONLY (fndecl) = 0;
919 TREE_SIDE_EFFECTS (fndecl) = 1;
920
921 // setup the params
922 std::vector<Bvariable *> param_vars;
923 std::vector<tree> types;
924 compile_fn_params (ctx, fntype, fndecl, &param_vars, &types);
925
926 auto ok = Backend::function_set_parameters (fndecl, param_vars);
927 rust_assert (ok);
928
929 enter_intrinsic_block (ctx, fndecl);
930
931 auto dst = Backend::var_expression (param_vars[0], UNDEF_LOCATION);
932 TREE_READONLY (dst) = 0;
933
934 auto value = Backend::var_expression (param_vars[1], UNDEF_LOCATION);
935 auto memorder = make_unsigned_long_tree (ordering);
936
937 auto monomorphized_type
938 = fntype->get_substs ()[0].get_param_ty ()->resolve ();
939
940 auto builtin_name
941 = build_atomic_builtin_name ("atomic_store_", fntype->get_locus (),
942 monomorphized_type);
943 if (builtin_name.empty ())
944 return error_mark_node;
945
946 tree atomic_store_raw = nullptr;
947 BuiltinsContext::get ().lookup_simple_builtin (builtin_name,
948 &atomic_store_raw);
949 rust_assert (atomic_store_raw);
950
951 auto atomic_store
952 = build_fold_addr_expr_loc (UNKNOWN_LOCATION, atomic_store_raw);
953
954 auto store_call
955 = Backend::call_expression (atomic_store, {dst, value, memorder}, nullptr,
956 UNDEF_LOCATION);
957 TREE_READONLY (store_call) = 0;
958 TREE_SIDE_EFFECTS (store_call) = 1;
959
960 ctx->add_statement (store_call);
961 finalize_intrinsic_block (ctx, fndecl);
962
963 return fndecl;
964 }
965
966 static tree
967 atomic_load_handler_inner (Context *ctx, TyTy::FnType *fntype, int ordering)
968 {
969 rust_assert (fntype->get_params ().size () == 1);
970 rust_assert (fntype->get_num_substitutions () == 1);
971
972 tree lookup = NULL_TREE;
973 if (check_for_cached_intrinsic (ctx, fntype, &lookup))
974 return lookup;
975
976 auto fndecl = compile_intrinsic_function (ctx, fntype);
977
978 // Most intrinsic functions are pure but not the atomic ones
979 // FIXME: Is atomic_load_* pure? Feels like it shouldn't so
980 TREE_READONLY (fndecl) = 0;
981 TREE_SIDE_EFFECTS (fndecl) = 1;
982
983 // setup the params
984 std::vector<Bvariable *> param_vars;
985 std::vector<tree> types;
986 compile_fn_params (ctx, fntype, fndecl, &param_vars, &types);
987
988 auto ok = Backend::function_set_parameters (fndecl, param_vars);
989 rust_assert (ok);
990
991 enter_intrinsic_block (ctx, fndecl);
992
993 auto src = Backend::var_expression (param_vars[0], UNDEF_LOCATION);
994 auto memorder = make_unsigned_long_tree (ordering);
995
996 auto monomorphized_type
997 = fntype->get_substs ()[0].get_param_ty ()->resolve ();
998
999 auto builtin_name
1000 = build_atomic_builtin_name ("atomic_load_", fntype->get_locus (),
1001 monomorphized_type);
1002 if (builtin_name.empty ())
1003 return error_mark_node;
1004
1005 tree atomic_load_raw = nullptr;
1006 BuiltinsContext::get ().lookup_simple_builtin (builtin_name,
1007 &atomic_load_raw);
1008 rust_assert (atomic_load_raw);
1009
1010 auto atomic_load
1011 = build_fold_addr_expr_loc (UNKNOWN_LOCATION, atomic_load_raw);
1012
1013 auto load_call = Backend::call_expression (atomic_load, {src, memorder},
1014 nullptr, UNDEF_LOCATION);
1015 auto return_statement
1016 = Backend::return_statement (fndecl, load_call, UNDEF_LOCATION);
1017
1018 TREE_READONLY (load_call) = 0;
1019 TREE_SIDE_EFFECTS (load_call) = 1;
1020
1021 ctx->add_statement (return_statement);
1022
1023 finalize_intrinsic_block (ctx, fndecl);
1024
1025 return fndecl;
1026 }
1027
1028 static inline tree
1029 unchecked_op_inner (Context *ctx, TyTy::FnType *fntype, tree_code op)
1030 {
1031 rust_assert (fntype->get_params ().size () == 2);
1032 rust_assert (fntype->get_num_substitutions () == 1);
1033
1034 tree lookup = NULL_TREE;
1035 if (check_for_cached_intrinsic (ctx, fntype, &lookup))
1036 return lookup;
1037
1038 auto fndecl = compile_intrinsic_function (ctx, fntype);
1039
1040 // setup the params
1041 std::vector<Bvariable *> param_vars;
1042 compile_fn_params (ctx, fntype, fndecl, &param_vars);
1043
1044 if (!Backend::function_set_parameters (fndecl, param_vars))
1045 return error_mark_node;
1046
1047 enter_intrinsic_block (ctx, fndecl);
1048
1049 // BUILTIN unchecked_<op> BODY BEGIN
1050
1051 auto x = Backend::var_expression (param_vars[0], UNDEF_LOCATION);
1052 auto y = Backend::var_expression (param_vars[1], UNDEF_LOCATION);
1053
1054 auto *monomorphized_type
1055 = fntype->get_substs ().at (0).get_param_ty ()->resolve ();
1056
1057 check_for_basic_integer_type ("unchecked operation", fntype->get_locus (),
1058 monomorphized_type);
1059
1060 auto expr = build2 (op, TREE_TYPE (x), x, y);
1061 auto return_statement
1062 = Backend::return_statement (fndecl, expr, UNDEF_LOCATION);
1063
1064 ctx->add_statement (return_statement);
1065
1066 // BUILTIN unchecked_<op> BODY END
1067
1068 finalize_intrinsic_block (ctx, fndecl);
1069
1070 return fndecl;
1071 }
1072
1073 static tree
1074 uninit_handler (Context *ctx, TyTy::FnType *fntype)
1075 {
1076 // uninit has _zero_ parameters its parameter is the generic one
1077 rust_assert (fntype->get_params ().size () == 0);
1078
1079 tree lookup = NULL_TREE;
1080 if (check_for_cached_intrinsic (ctx, fntype, &lookup))
1081 return lookup;
1082
1083 auto fndecl = compile_intrinsic_function (ctx, fntype);
1084
1085 // Most intrinsic functions are pure - not `uninit_handler`
1086 TREE_READONLY (fndecl) = 0;
1087 TREE_SIDE_EFFECTS (fndecl) = 1;
1088
1089 // get the template parameter type tree fn uninit<T>();
1090 rust_assert (fntype->get_num_substitutions () == 1);
1091 auto &param_mapping = fntype->get_substs ().at (0);
1092 const TyTy::ParamType *param_tyty = param_mapping.get_param_ty ();
1093 TyTy::BaseType *resolved_tyty = param_tyty->resolve ();
1094 tree template_parameter_type
1095 = TyTyResolveCompile::compile (ctx, resolved_tyty);
1096
1097 // result temporary
1098 tree dst_type = TREE_TYPE (DECL_RESULT (fndecl));
1099 rust_assert (TYPE_SIZE_UNIT (template_parameter_type)
1100 == TYPE_SIZE_UNIT (dst_type));
1101
1102 tree tmp_stmt = error_mark_node;
1103 Bvariable *bvar
1104 = Backend::temporary_variable (fndecl, NULL_TREE, dst_type, NULL_TREE,
1105 true /*address_is_taken*/, UNDEF_LOCATION,
1106 &tmp_stmt);
1107
1108 enter_intrinsic_block (ctx, fndecl, {bvar});
1109
1110 // BUILTIN size_of FN BODY BEGIN
1111
1112 tree memset_builtin = error_mark_node;
1113 BuiltinsContext::get ().lookup_simple_builtin ("__builtin_memset",
1114 &memset_builtin);
1115 rust_assert (memset_builtin != error_mark_node);
1116
1117 // call memset with 0x01 and size of the thing see
1118 // https://github.com/Rust-GCC/gccrs/issues/1899
1119
1120 tree dst = bvar->get_tree (BUILTINS_LOCATION);
1121 tree dst_addr = build_fold_addr_expr_loc (BUILTINS_LOCATION, dst);
1122 tree constant_byte = build_int_cst (integer_type_node, 0x01);
1123 tree size_expr = TYPE_SIZE_UNIT (template_parameter_type);
1124
1125 tree memset_call = build_call_expr_loc (BUILTINS_LOCATION, memset_builtin, 3,
1126 dst_addr, constant_byte, size_expr);
1127 ctx->add_statement (memset_call);
1128
1129 auto return_statement
1130 = Backend::return_statement (fndecl, dst, UNDEF_LOCATION);
1131 ctx->add_statement (return_statement);
1132 // BUILTIN size_of FN BODY END
1133
1134 finalize_intrinsic_block (ctx, fndecl);
1135
1136 return fndecl;
1137 }
1138
1139 static tree
1140 move_val_init_handler (Context *ctx, TyTy::FnType *fntype)
1141 {
1142 rust_assert (fntype->get_params ().size () == 2);
1143
1144 tree lookup = NULL_TREE;
1145 if (check_for_cached_intrinsic (ctx, fntype, &lookup))
1146 return lookup;
1147
1148 auto fndecl = compile_intrinsic_function (ctx, fntype);
1149
1150 // Most intrinsic functions are pure - not `move_val_init`
1151 TREE_READONLY (fndecl) = 0;
1152 TREE_SIDE_EFFECTS (fndecl) = 1;
1153
1154 // get the template parameter type tree fn size_of<T>();
1155 rust_assert (fntype->get_num_substitutions () == 1);
1156 auto &param_mapping = fntype->get_substs ().at (0);
1157 const TyTy::ParamType *param_tyty = param_mapping.get_param_ty ();
1158 TyTy::BaseType *resolved_tyty = param_tyty->resolve ();
1159 tree template_parameter_type
1160 = TyTyResolveCompile::compile (ctx, resolved_tyty);
1161
1162 std::vector<Bvariable *> param_vars;
1163 compile_fn_params (ctx, fntype, fndecl, &param_vars);
1164
1165 if (!Backend::function_set_parameters (fndecl, param_vars))
1166 return error_mark_node;
1167
1168 enter_intrinsic_block (ctx, fndecl);
1169
1170 // BUILTIN size_of FN BODY BEGIN
1171
1172 tree dst = Backend::var_expression (param_vars[0], UNDEF_LOCATION);
1173 tree src = Backend::var_expression (param_vars[1], UNDEF_LOCATION);
1174 tree size = TYPE_SIZE_UNIT (template_parameter_type);
1175
1176 tree memcpy_builtin = error_mark_node;
1177 BuiltinsContext::get ().lookup_simple_builtin ("__builtin_memcpy",
1178 &memcpy_builtin);
1179 rust_assert (memcpy_builtin != error_mark_node);
1180
1181 src = build_fold_addr_expr_loc (BUILTINS_LOCATION, src);
1182 tree memset_call = build_call_expr_loc (BUILTINS_LOCATION, memcpy_builtin, 3,
1183 dst, src, size);
1184
1185 ctx->add_statement (memset_call);
1186 // BUILTIN size_of FN BODY END
1187
1188 finalize_intrinsic_block (ctx, fndecl);
1189
1190 return fndecl;
1191 }
1192
1193 static inline tree
1194 expect_handler_inner (Context *ctx, TyTy::FnType *fntype, bool likely)
1195 {
1196 rust_assert (fntype->get_params ().size () == 1);
1197
1198 tree lookup = NULL_TREE;
1199 if (check_for_cached_intrinsic (ctx, fntype, &lookup))
1200 return lookup;
1201
1202 auto fndecl = compile_intrinsic_function (ctx, fntype);
1203
1204 enter_intrinsic_block (ctx, fndecl);
1205
1206 // BUILTIN expect_handler_inner FN BODY BEGIN
1207 // setup the params
1208 std::vector<Bvariable *> param_vars;
1209 compile_fn_params (ctx, fntype, fndecl, &param_vars);
1210 tree expr = Backend::var_expression (param_vars[0], UNDEF_LOCATION);
1211 tree expect_fn_raw = nullptr;
1212 BuiltinsContext::get ().lookup_simple_builtin ("__builtin_expect",
1213 &expect_fn_raw);
1214 rust_assert (expect_fn_raw);
1215 auto expect_fn = build_fold_addr_expr_loc (BUILTINS_LOCATION, expect_fn_raw);
1216
1217 // we need to convert the expression return type to long to match the expected
1218 // parameter type of __builtin_expect
1219 auto expect_src = build1 (CONVERT_EXPR, long_integer_type_node, expr);
1220 auto expect_value
1221 = make_unsigned_long_tree (static_cast<unsigned long> (likely));
1222
1223 auto expect_call
1224 = Backend::call_expression (expect_fn, {expect_src, expect_value}, nullptr,
1225 BUILTINS_LOCATION);
1226 // the return value also needs to be casted (to bool)
1227 auto expect_call_bool = build1 (CONVERT_EXPR, boolean_type_node, expect_call);
1228 auto return_statement
1229 = Backend::return_statement (fndecl, expect_call_bool, BUILTINS_LOCATION);
1230 ctx->add_statement (return_statement);
1231 // BUILTIN expect_handler_inner FN BODY END
1232
1233 finalize_intrinsic_block (ctx, fndecl);
1234
1235 return fndecl;
1236 }
1237
1238 static tree
1239 assume_handler (Context *ctx, TyTy::FnType *fntype)
1240 {
1241 // TODO: make sure this is actually helping the compiler optimize
1242
1243 rust_assert (fntype->get_params ().size () == 1);
1244 rust_assert (fntype->param_at (0).get_type ()->get_kind ()
1245 == TyTy::TypeKind::BOOL);
1246
1247 tree lookup = NULL_TREE;
1248 if (check_for_cached_intrinsic (ctx, fntype, &lookup))
1249 return lookup;
1250
1251 auto fndecl = compile_intrinsic_function (ctx, fntype);
1252
1253 // TODO: make sure these are necessary
1254 TREE_READONLY (fndecl) = 0;
1255 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
1256 DECL_ATTRIBUTES (fndecl) = tree_cons (get_identifier ("always_inline"),
1257 NULL_TREE, DECL_ATTRIBUTES (fndecl));
1258
1259 std::vector<Bvariable *> param_vars;
1260 compile_fn_params (ctx, fntype, fndecl, &param_vars);
1261
1262 if (!Backend::function_set_parameters (fndecl, param_vars))
1263 return error_mark_node;
1264
1265 enter_intrinsic_block (ctx, fndecl);
1266
1267 // BUILTIN assume FN BODY BEGIN
1268
1269 tree val = Backend::var_expression (param_vars[0], UNDEF_LOCATION);
1270
1271 tree assume_expr = build_call_expr_internal_loc (UNDEF_LOCATION, IFN_ASSUME,
1272 void_type_node, 1, val);
1273 TREE_SIDE_EFFECTS (assume_expr) = 1;
1274
1275 ctx->add_statement (assume_expr);
1276 // BUILTIN assume FN BODY END
1277
1278 finalize_intrinsic_block (ctx, fndecl);
1279
1280 return fndecl;
1281 }
1282
1283 static tree
1284 try_handler_inner (Context *ctx, TyTy::FnType *fntype, bool is_new_api)
1285 {
1286 rust_assert (fntype->get_params ().size () == 3);
1287
1288 tree lookup = NULL_TREE;
1289 if (check_for_cached_intrinsic (ctx, fntype, &lookup))
1290 return lookup;
1291 auto fndecl = compile_intrinsic_function (ctx, fntype);
1292
1293 enter_intrinsic_block (ctx, fndecl);
1294
1295 // The following tricks are needed to make sure the try-catch blocks are not
1296 // optimized away
1297 TREE_READONLY (fndecl) = 0;
1298 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
1299 DECL_ATTRIBUTES (fndecl) = tree_cons (get_identifier ("always_inline"),
1300 NULL_TREE, DECL_ATTRIBUTES (fndecl));
1301
1302 // BUILTIN try_handler FN BODY BEGIN
1303 // setup the params
1304 std::vector<Bvariable *> param_vars;
1305 compile_fn_params (ctx, fntype, fndecl, &param_vars);
1306 if (!Backend::function_set_parameters (fndecl, param_vars))
1307 return error_mark_node;
1308 tree enclosing_scope = NULL_TREE;
1309
1310 bool panic_is_abort = Session::get_instance ().options.get_panic_strategy ()
1311 == CompileOptions::PanicStrategy::Abort;
1312 tree try_fn = Backend::var_expression (param_vars[0], UNDEF_LOCATION);
1313 tree user_data = Backend::var_expression (param_vars[1], UNDEF_LOCATION);
1314 tree catch_fn = Backend::var_expression (param_vars[2], UNDEF_LOCATION);
1315 tree normal_return_stmt = NULL_TREE;
1316 tree error_return_stmt = NULL_TREE;
1317 tree try_call = Backend::call_expression (try_fn, {user_data}, nullptr,
1318 BUILTINS_LOCATION);
1319 tree catch_call = NULL_TREE;
1320 tree try_block = Backend::block (fndecl, enclosing_scope, {}, UNDEF_LOCATION,
1321 UNDEF_LOCATION);
1322
1323 if (is_new_api)
1324 {
1325 auto ret_type = TyTyResolveCompile::get_unit_type (ctx);
1326 auto ret_expr = Backend::constructor_expression (ret_type, false, {}, -1,
1327 UNDEF_LOCATION);
1328 normal_return_stmt
1329 = Backend::return_statement (fndecl, ret_expr, BUILTINS_LOCATION);
1330 error_return_stmt
1331 = Backend::return_statement (fndecl, ret_expr, BUILTINS_LOCATION);
1332 }
1333 else
1334 {
1335 normal_return_stmt = Backend::return_statement (fndecl, integer_zero_node,
1336 BUILTINS_LOCATION);
1337 error_return_stmt = Backend::return_statement (fndecl, integer_one_node,
1338 BUILTINS_LOCATION);
1339 }
1340 Backend::block_add_statements (try_block,
1341 std::vector<tree>{try_call,
1342 normal_return_stmt});
1343 if (panic_is_abort)
1344 {
1345 // skip building the try-catch construct
1346 ctx->add_statement (try_block);
1347 finalize_intrinsic_block (ctx, fndecl);
1348 return fndecl;
1349 }
1350
1351 tree eh_pointer
1352 = build_call_expr (builtin_decl_explicit (BUILT_IN_EH_POINTER), 1,
1353 integer_zero_node);
1354 catch_call = Backend::call_expression (catch_fn, {user_data, eh_pointer},
1355 NULL_TREE, BUILTINS_LOCATION);
1356
1357 tree catch_block = Backend::block (fndecl, enclosing_scope, {},
1358 UNDEF_LOCATION, UNDEF_LOCATION);
1359 Backend::block_add_statements (catch_block,
1360 std::vector<tree>{catch_call,
1361 error_return_stmt});
1362 // emulate what cc1plus is doing for C++ try-catch
1363 tree inner_eh_construct
1364 = Backend::exception_handler_statement (catch_call, NULL_TREE,
1365 error_return_stmt,
1366 BUILTINS_LOCATION);
1367 // TODO(liushuyu): eh_personality needs to be implemented as a runtime thing
1368 auto eh_construct
1369 = Backend::exception_handler_statement (try_block, inner_eh_construct,
1370 NULL_TREE, BUILTINS_LOCATION);
1371 ctx->add_statement (eh_construct);
1372 // BUILTIN try_handler FN BODY END
1373 finalize_intrinsic_block (ctx, fndecl);
1374
1375 return fndecl;
1376 }
1377
1378 static tree
1379 discriminant_value_handler (Context *ctx, TyTy::FnType *fntype)
1380 {
1381 rust_assert (fntype->get_params ().size () == 1);
1382 rust_assert (fntype->get_return_type ()->is<TyTy::PlaceholderType> ());
1383 rust_assert (fntype->has_substitutions ());
1384 rust_assert (fntype->get_num_type_params () == 1);
1385 auto &mapping = fntype->get_substs ().at (0);
1386 auto param_ty = mapping.get_param_ty ();
1387 rust_assert (param_ty->can_resolve ());
1388 auto resolved = param_ty->resolve ();
1389 auto p = static_cast<TyTy::PlaceholderType *> (fntype->get_return_type ());
1390
1391 TyTy::BaseType *return_type = nullptr;
1392 bool ok = ctx->get_tyctx ()->lookup_builtin ("isize", &return_type);
1393 rust_assert (ok);
1394
1395 bool is_adt = resolved->is<TyTy::ADTType> ();
1396 bool is_enum = false;
1397 if (is_adt)
1398 {
1399 const auto &adt = *static_cast<TyTy::ADTType *> (resolved);
1400 return_type = adt.get_repr_options ().repr;
1401 rust_assert (return_type != nullptr);
1402 is_enum = adt.is_enum ();
1403 }
1404
1405 p->set_associated_type (return_type->get_ref ());
1406
1407 tree lookup = NULL_TREE;
1408 if (check_for_cached_intrinsic (ctx, fntype, &lookup))
1409 return lookup;
1410
1411 auto fndecl = compile_intrinsic_function (ctx, fntype);
1412
1413 std::vector<Bvariable *> param_vars;
1414 compile_fn_params (ctx, fntype, fndecl, &param_vars);
1415
1416 if (!Backend::function_set_parameters (fndecl, param_vars))
1417 return error_mark_node;
1418
1419 enter_intrinsic_block (ctx, fndecl);
1420
1421 // BUILTIN disriminant_value FN BODY BEGIN
1422
1423 tree result = integer_zero_node;
1424 if (is_enum)
1425 {
1426 tree val = Backend::var_expression (param_vars[0], UNDEF_LOCATION);
1427 tree deref = build_fold_indirect_ref_loc (UNKNOWN_LOCATION, val);
1428 result = Backend::struct_field_expression (deref, 0, UNKNOWN_LOCATION);
1429 }
1430
1431 auto return_statement
1432 = Backend::return_statement (fndecl, result, BUILTINS_LOCATION);
1433 ctx->add_statement (return_statement);
1434
1435 // BUILTIN disriminant_value FN BODY END
1436
1437 finalize_intrinsic_block (ctx, fndecl);
1438
1439 return fndecl;
1440 }
1441
1442 static tree
1443 variant_count_handler (Context *ctx, TyTy::FnType *fntype)
1444 {
1445 rust_assert (fntype->get_num_type_params () == 1);
1446 auto &mapping = fntype->get_substs ().at (0);
1447 auto param_ty = mapping.get_param_ty ();
1448 rust_assert (param_ty->can_resolve ());
1449 auto resolved = param_ty->resolve ();
1450
1451 size_t variant_count = 0;
1452 bool is_adt = resolved->is<TyTy::ADTType> ();
1453 if (is_adt)
1454 {
1455 const auto &adt = *static_cast<TyTy::ADTType *> (resolved);
1456 variant_count = adt.number_of_variants ();
1457 }
1458
1459 tree lookup = NULL_TREE;
1460 if (check_for_cached_intrinsic (ctx, fntype, &lookup))
1461 return lookup;
1462
1463 auto fndecl = compile_intrinsic_function (ctx, fntype);
1464
1465 std::vector<Bvariable *> param_vars;
1466 compile_fn_params (ctx, fntype, fndecl, &param_vars);
1467
1468 if (!Backend::function_set_parameters (fndecl, param_vars))
1469 return error_mark_node;
1470
1471 enter_intrinsic_block (ctx, fndecl);
1472
1473 // BUILTIN disriminant_value FN BODY BEGIN
1474 tree result_decl = DECL_RESULT (fndecl);
1475 tree type = TREE_TYPE (result_decl);
1476
1477 mpz_t ival;
1478 mpz_init_set_ui (ival, variant_count);
1479 tree result = wide_int_to_tree (type, wi::from_mpz (type, ival, true));
1480 mpz_clear (ival);
1481
1482 auto return_statement
1483 = Backend::return_statement (fndecl, result, BUILTINS_LOCATION);
1484 ctx->add_statement (return_statement);
1485
1486 // BUILTIN disriminant_value FN BODY END
1487
1488 finalize_intrinsic_block (ctx, fndecl);
1489
1490 return fndecl;
1491 }
1492
1493 } // namespace Compile
1494 } // namespace Rust