Skip to content

Commit 06cc7c0

Browse files
philbertyCohenArthur
authored andcommitted
gccrs: Fix overflow intrinsic use before init
The overflow intrinsic returns a tuple of (value, boolean) where it value is the operator result and boolean if it overflowed or not. The intrinsic here did not initilize the resulting tuple and therefore was creating a use before init error resulting in garbage results Addresses Rust-GCC#1895 gcc/rust/ChangeLog: * backend/rust-compile-intrinsic.cc (op_with_overflow_inner): fix use before init Signed-off-by: Philip Herron <[email protected]>
1 parent 35dc16d commit 06cc7c0

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

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

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,25 @@ op_with_overflow_inner (Context *ctx, TyTy::FnType *fntype, tree_code op)
598598
if (!ctx->get_backend ()->function_set_parameters (fndecl, param_vars))
599599
return error_mark_node;
600600

601-
enter_intrinsic_block (ctx, fndecl);
601+
rust_assert (fntype->get_num_substitutions () == 1);
602+
auto &param_mapping = fntype->get_substs ().at (0);
603+
const TyTy::ParamType *param_tyty = param_mapping.get_param_ty ();
604+
TyTy::BaseType *resolved_tyty = param_tyty->resolve ();
605+
tree template_parameter_type
606+
= TyTyResolveCompile::compile (ctx, resolved_tyty);
607+
608+
// this should match y as well or we can take it from the TyTy structure
609+
tree tmp_stmt = error_mark_node;
610+
Bvariable *result_variable = ctx->get_backend ()->temporary_variable (
611+
fndecl, NULL_TREE, template_parameter_type, NULL_TREE,
612+
true /*address_is_taken*/, UNDEF_LOCATION, &tmp_stmt);
613+
Bvariable *bool_variable
614+
= ctx->get_backend ()->temporary_variable (fndecl, NULL_TREE,
615+
boolean_type_node, NULL_TREE,
616+
true /*address_is_taken*/,
617+
UNDEF_LOCATION, &tmp_stmt);
618+
619+
enter_intrinsic_block (ctx, fndecl, {result_variable, bool_variable});
602620

603621
// BUILTIN op_with_overflow FN BODY BEGIN
604622
auto x = ctx->get_backend ()->var_expression (x_param, UNDEF_LOCATION);
@@ -628,24 +646,20 @@ op_with_overflow_inner (Context *ctx, TyTy::FnType *fntype, tree_code op)
628646
}
629647
rust_assert (overflow_builtin != error_mark_node);
630648

631-
// this should match y as well or we can take it from the TyTy structure
632-
tree overflow_op_type = TREE_TYPE (x);
633-
tree tmp_stmt = error_mark_node;
634-
Bvariable *bvar
635-
= ctx->get_backend ()->temporary_variable (fndecl, NULL_TREE,
636-
overflow_op_type, NULL_TREE,
637-
true /*address_is_taken*/,
638-
UNDEF_LOCATION, &tmp_stmt);
639-
ctx->add_statement (tmp_stmt);
640-
641-
tree result_decl = bvar->get_tree (UNDEF_LOCATION);
649+
tree bool_decl = bool_variable->get_tree (BUILTINS_LOCATION);
650+
tree result_decl = result_variable->get_tree (BUILTINS_LOCATION);
642651
tree result_ref = build_fold_addr_expr_loc (BUILTINS_LOCATION, result_decl);
643652

644-
tree did_overflow_node
645-
= build_call_expr_loc (BUILTINS_LOCATION, overflow_builtin, 3, x, y,
646-
result_ref);
653+
tree builtin_call = build_call_expr_loc (BUILTINS_LOCATION, overflow_builtin,
654+
3, x, y, result_ref);
655+
656+
tree overflow_assignment
657+
= ctx->get_backend ()->assignment_statement (bool_decl, builtin_call,
658+
BUILTINS_LOCATION);
659+
660+
ctx->add_statement (overflow_assignment);
647661

648-
std::vector<tree> vals = {result_decl, did_overflow_node};
662+
std::vector<tree> vals = {result_decl, bool_decl};
649663
tree tuple_type = TREE_TYPE (DECL_RESULT (fndecl));
650664
tree result_expr
651665
= ctx->get_backend ()->constructor_expression (tuple_type, false, vals, -1,

0 commit comments

Comments
 (0)