From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 840E5385140A; Wed, 31 Aug 2022 14:10:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 840E5385140A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1661955010; bh=82NhQPz/cWP8k9h+FJ/noJl+teuGNJqfI+qpPqGojxE=; h=From:To:Subject:Date:From; b=EzOll7/1LMu5xaya0o5oh+HjqGd3Krb6eh5VoWbgZG/Zc8NUha46Ke2ar+RJHO6Zr ihe/dcTkk4/unjk8NvxKGQm8QuaczyeM5HCypXld31nrL9p3gfXUEPMjFMbV+L/E+m BeeHCEfk7/uHTF/OxVGcP2yaD/hOP69iuXs6bov0= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] Create canonical process of compiling constant items X-Act-Checkin: gcc X-Git-Author: Philip Herron X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: b1cf3a384d41fa34c1eb819696e66fa65625d8db X-Git-Newrev: 5acb1375c9c57b4cc0af13f4ccea0d609942bc0a Message-Id: <20220831141010.840E5385140A@sourceware.org> Date: Wed, 31 Aug 2022 14:10:10 +0000 (GMT) List-Id: https://gcc.gnu.org/g:5acb1375c9c57b4cc0af13f4ccea0d609942bc0a commit 5acb1375c9c57b4cc0af13f4ccea0d609942bc0a Author: Philip Herron Date: Thu Aug 25 14:46:32 2022 +0100 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. 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 c56a0eab6de..a5643d2f625 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); }