From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7905) id 20FE4384F00C; Tue, 31 Jan 2023 13:15:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 20FE4384F00C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1675170938; bh=Q8x1LQ4U1MgjsNci0iRLGUasNYs3evKPDjtl5E4wdh8=; h=From:To:Subject:Date:From; b=fUmmVo+kaesx+nZ2/MW2Q5H7zJ4MBj1VmS5FeuGahtZ/Iq7eqhdcUEkHzcjduDbjH 6Ut9sqlSO5AflJb8qHCajLi7hhqDDUT+pQezWB4496R1poibK0+A99ZPw49+pBHmxT Beq6TLs9Y5RUne6WOtyigrwg1HRViZ9PQHzTMs9s= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Arthur Cohen To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-5550] gccrs: Create canonical process of compiling constant items X-Act-Checkin: gcc X-Git-Author: Philip Herron X-Git-Refname: refs/heads/master X-Git-Oldrev: 1eabeb5aecc954d2c237f5997605a73a37dd1c12 X-Git-Newrev: ffa16a4590ad47e3f4ba098e2090c3a53dccd5c7 Message-Id: <20230131131538.20FE4384F00C@sourceware.org> Date: Tue, 31 Jan 2023 13:15:38 +0000 (GMT) List-Id: https://gcc.gnu.org/g:ffa16a4590ad47e3f4ba098e2090c3a53dccd5c7 commit r13-5550-gffa16a4590ad47e3f4ba098e2090c3a53dccd5c7 Author: Philip Herron Date: Thu Aug 25 14:46:32 2022 +0100 gccrs: Create canonical process of compiling constant items In order to compile a block expression constant, the simplest way for us was to reuse what code we have and to generate an artifical function which does not get added to the translation unit. The constant then becomes a CALL_EXPR to this artifical function which we can pass to the constexpr evaluator to resolve the result of this artifical 'CALL_EXPR'. Before this patch we seperated the difference between block expressions and non block expressions in constants. So for non block expressions we simply compiled them as if it was a simple constant but this is not guaranteed to be the case in rust, for example coercion sites can generate temporaries during autoderef which we let the constant evaluator resolve for us. This makes all constants handled in the same way to simplify the logic here. gcc/rust/ChangeLog: * backend/rust-compile-base.cc: Improve compilation pipeline and simplify function. Diff: --- gcc/rust/backend/rust-compile-base.cc | 103 ++++++++++++++++++---------------- 1 file changed, 55 insertions(+), 48 deletions(-) diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc index e1506b377ce..568abf9ca2c 100644 --- a/gcc/rust/backend/rust-compile-base.cc +++ b/gcc/rust/backend/rust-compile-base.cc @@ -652,65 +652,72 @@ HIRCompileBase::compile_constant_item ( bool is_block_expr = const_value_expr->get_expression_type () == HIR::Expr::ExprType::Block; - // compile the expression - tree folded_expr = error_mark_node; - if (!is_block_expr) - { - tree value = CompileExpr::Compile (const_value_expr, ctx); - folded_expr = fold_expr (value); - } - else - { - // in order to compile a block expr we want to reuse as much existing - // machineary that we already have. This means the best approach is to - // make a _fake_ function with a block so it can hold onto temps then - // use our constexpr code to fold it completely or error_mark_node - Backend::typed_identifier receiver; - tree compiled_fn_type = ctx->get_backend ()->function_type ( - receiver, {}, {Backend::typed_identifier ("_", const_type, locus)}, - NULL, locus); - - tree fndecl - = ctx->get_backend ()->function (compiled_fn_type, ident, "", 0, locus); - TREE_READONLY (fndecl) = 1; + // in order to compile a block expr we want to reuse as much existing + // machineary that we already have. This means the best approach is to + // make a _fake_ function with a block so it can hold onto temps then + // use our constexpr code to fold it completely or error_mark_node + Backend::typed_identifier receiver; + tree compiled_fn_type = ctx->get_backend ()->function_type ( + receiver, {}, {Backend::typed_identifier ("_", const_type, locus)}, NULL, + locus); + + tree fndecl + = ctx->get_backend ()->function (compiled_fn_type, ident, "", 0, locus); + TREE_READONLY (fndecl) = 1; + + tree enclosing_scope = NULL_TREE; - tree enclosing_scope = NULL_TREE; + Location start_location = const_value_expr->get_locus (); + Location end_location = const_value_expr->get_locus (); + if (is_block_expr) + { HIR::BlockExpr *function_body = static_cast (const_value_expr); - Location start_location = function_body->get_locus (); - Location end_location = function_body->get_end_locus (); + start_location = function_body->get_locus (); + end_location = function_body->get_end_locus (); + } - tree code_block - = ctx->get_backend ()->block (fndecl, enclosing_scope, {}, - start_location, end_location); - ctx->push_block (code_block); + tree code_block = ctx->get_backend ()->block (fndecl, enclosing_scope, {}, + start_location, end_location); + ctx->push_block (code_block); - bool address_is_taken = false; - tree ret_var_stmt = NULL_TREE; - Bvariable *return_address - = ctx->get_backend ()->temporary_variable (fndecl, code_block, - const_type, NULL, - address_is_taken, locus, - &ret_var_stmt); + bool address_is_taken = false; + tree ret_var_stmt = NULL_TREE; + Bvariable *return_address + = ctx->get_backend ()->temporary_variable (fndecl, code_block, const_type, + NULL, address_is_taken, locus, + &ret_var_stmt); - ctx->add_statement (ret_var_stmt); - ctx->push_fn (fndecl, return_address); + ctx->add_statement (ret_var_stmt); + ctx->push_fn (fndecl, return_address); + if (is_block_expr) + { + HIR::BlockExpr *function_body + = static_cast (const_value_expr); compile_function_body (ctx, fndecl, *function_body, true); - tree bind_tree = ctx->pop_block (); + } + else + { + tree value = CompileExpr::Compile (const_value_expr, ctx); + tree return_expr = ctx->get_backend ()->return_statement ( + fndecl, {value}, const_value_expr->get_locus ()); + ctx->add_statement (return_expr); + } - gcc_assert (TREE_CODE (bind_tree) == BIND_EXPR); - DECL_SAVED_TREE (fndecl) = bind_tree; - DECL_DECLARED_CONSTEXPR_P (fndecl) = 1; - maybe_save_constexpr_fundef (fndecl); + tree bind_tree = ctx->pop_block (); + + gcc_assert (TREE_CODE (bind_tree) == BIND_EXPR); + DECL_SAVED_TREE (fndecl) = bind_tree; + DECL_DECLARED_CONSTEXPR_P (fndecl) = 1; + maybe_save_constexpr_fundef (fndecl); - ctx->pop_fn (); + ctx->pop_fn (); - // lets fold it into a call expr - tree call = build_call_array_loc (locus.gcc_location (), const_type, - fndecl, 0, NULL); - folded_expr = fold_expr (call); - } + // lets fold it into a call expr + tree call + = build_call_array_loc (locus.gcc_location (), const_type, fndecl, 0, NULL); + tree folded_expr = fold_expr (call); return named_constant_expression (const_type, ident, folded_expr, locus); }