diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2022-05-26 18:50:14 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-26 18:50:14 +0000 |
commit | 7a94948e65db2fef6fd93c94e70ead168b71513d (patch) | |
tree | 9d408e4971321f0e1bd887efda2ccf1f1c8553bf /gcc | |
parent | a4455d8f522cad015f3540f118f8c6ce2775fccb (diff) | |
parent | 1a04c501e41ec0dee74d520a7ca4373cb7df7a48 (diff) | |
download | gcc-7a94948e65db2fef6fd93c94e70ead168b71513d.zip gcc-7a94948e65db2fef6fd93c94e70ead168b71513d.tar.gz gcc-7a94948e65db2fef6fd93c94e70ead168b71513d.tar.bz2 |
Merge #1283
1283: const folding in gccrs: remove ConstCtx class. r=philberty a=abbasfaisal
Card: [Link](https://github.com/Rust-GCC/gccrs/projects/16#card-82300522)
This class had potential to hinder porting further const folding code from C++. This edit makes it easy to copy code from constexpr.cc to rust-constexpr.cc and so on.
Structs `constexpr_ctx` and `constexpr_global_ctx` have been copied as well as to keep `constexpr_ops_count` after removing the class. These structs will be filled further as the port carries on. The prototypes inside ConstCtx have been copied over to rust-constexpr.cc as static prototypes.
Co-authored-by: Faisal Abbas <90.abbasfaisal@gmail.com>
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/backend/rust-compile-base.cc | 4 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-expr.cc | 2 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-expr.h | 2 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-pattern.cc | 6 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-resolve-path.cc | 2 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-type.cc | 2 | ||||
-rw-r--r-- | gcc/rust/backend/rust-constexpr.cc | 86 | ||||
-rw-r--r-- | gcc/rust/backend/rust-constexpr.h | 17 |
8 files changed, 67 insertions, 54 deletions
diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc index 7b0a375..3de80d9 100644 --- a/gcc/rust/backend/rust-compile-base.cc +++ b/gcc/rust/backend/rust-compile-base.cc @@ -555,7 +555,7 @@ HIRCompileBase::compile_constant_item ( if (!is_block_expr) { tree value = CompileExpr::Compile (const_value_expr, ctx); - folded_expr = ConstCtx::fold (value); + folded_expr = fold_expr (value); } else { @@ -605,7 +605,7 @@ HIRCompileBase::compile_constant_item ( // lets fold it into a call expr tree call = build_call_array_loc (locus.gcc_location (), const_type, fndecl, 0, NULL); - folded_expr = ConstCtx::fold (call); + folded_expr = fold_expr (call); } return named_constant_expression (const_type, ident, folded_expr, locus); diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index 4168bb8..b7dad12 100644 --- a/gcc/rust/backend/rust-compile-expr.cc +++ b/gcc/rust/backend/rust-compile-expr.cc @@ -449,7 +449,7 @@ CompileExpr::visit (HIR::CallExpr &expr) { HIR::Expr *discrim_expr = variant->get_discriminant (); tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx); - tree folded_discrim_expr = ConstCtx::fold (discrim_expr_node); + tree folded_discrim_expr = fold_expr (discrim_expr_node); tree qualifier = folded_discrim_expr; ctor_arguments.push_back (qualifier); diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h index 655ffbb..593e1f9 100644 --- a/gcc/rust/backend/rust-compile-expr.h +++ b/gcc/rust/backend/rust-compile-expr.h @@ -492,7 +492,7 @@ public: { HIR::Expr *discrim_expr = variant->get_discriminant (); tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx); - tree folded_discrim_expr = ConstCtx::fold (discrim_expr_node); + tree folded_discrim_expr = fold_expr (discrim_expr_node); tree qualifier = folded_discrim_expr; ctor_arguments.push_back (qualifier); diff --git a/gcc/rust/backend/rust-compile-pattern.cc b/gcc/rust/backend/rust-compile-pattern.cc index aefa4eb..2f3449a 100644 --- a/gcc/rust/backend/rust-compile-pattern.cc +++ b/gcc/rust/backend/rust-compile-pattern.cc @@ -52,7 +52,7 @@ CompilePatternCaseLabelExpr::visit (HIR::PathInExpression &pattern) HIR::Expr *discrim_expr = variant->get_discriminant (); tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx); - tree folded_discrim_expr = ConstCtx::fold (discrim_expr_node); + tree folded_discrim_expr = fold_expr (discrim_expr_node); tree case_low = folded_discrim_expr; case_label_expr @@ -132,7 +132,7 @@ compile_range_pattern_bound (HIR::RangePatternBound *bound, result = ResolvePathRef::Compile (ref.get_path (), ctx); // If the path resolves to a const expression, fold it. - result = ConstCtx::fold (result); + result = fold_expr (result); } break; @@ -143,7 +143,7 @@ compile_range_pattern_bound (HIR::RangePatternBound *bound, result = ResolvePathRef::Compile (ref.get_qualified_path (), ctx); // If the path resolves to a const expression, fold it. - result = ConstCtx::fold (result); + result = fold_expr (result); } } diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc b/gcc/rust/backend/rust-compile-resolve-path.cc index ca96a27..4423912 100644 --- a/gcc/rust/backend/rust-compile-resolve-path.cc +++ b/gcc/rust/backend/rust-compile-resolve-path.cc @@ -99,7 +99,7 @@ ResolvePathRef::resolve (const HIR::PathIdentSegment &final_segment, // make the ctor for the union HIR::Expr *discrim_expr = variant->get_discriminant (); tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx); - tree folded_discrim_expr = ConstCtx::fold (discrim_expr_node); + tree folded_discrim_expr = fold_expr (discrim_expr_node); tree qualifier = folded_discrim_expr; return ctx->get_backend ()->constructor_expression (compiled_adt_type, diff --git a/gcc/rust/backend/rust-compile-type.cc b/gcc/rust/backend/rust-compile-type.cc index 6068e0d..240abe0 100644 --- a/gcc/rust/backend/rust-compile-type.cc +++ b/gcc/rust/backend/rust-compile-type.cc @@ -371,7 +371,7 @@ TyTyResolveCompile::visit (const TyTy::ArrayType &type) tree element_type = TyTyResolveCompile::compile (ctx, type.get_element_type ()); tree capacity_expr = CompileExpr::Compile (&type.get_capacity_expr (), ctx); - tree folded_capacity_expr = ConstCtx::fold (capacity_expr); + tree folded_capacity_expr = fold_expr (capacity_expr); translated = ctx->get_backend ()->array_type (element_type, folded_capacity_expr); diff --git a/gcc/rust/backend/rust-constexpr.cc b/gcc/rust/backend/rust-constexpr.cc index aee41e4..f77fb3a 100644 --- a/gcc/rust/backend/rust-constexpr.cc +++ b/gcc/rust/backend/rust-constexpr.cc @@ -29,6 +29,18 @@ namespace Rust { namespace Compile { +struct constexpr_global_ctx +{ + HOST_WIDE_INT constexpr_ops_count; + + constexpr_global_ctx () : constexpr_ops_count (0) {} +}; + +struct constexpr_ctx +{ + constexpr_global_ctx *global; +}; + static tree constant_value_1 (tree decl, bool strict_p, bool return_aggregate_cst_ok_p, bool unshare_p); @@ -39,22 +51,39 @@ static void non_const_var_error (location_t loc, tree r); static tree -get_function_named_in_call (tree t); +constexpr_expression (const constexpr_ctx *ctx, tree); -ConstCtx::ConstCtx () : constexpr_ops_count (0) {} +static tree +constexpr_fn_retval (const constexpr_ctx *ctx, tree r); + +static tree +eval_store_expression (const constexpr_ctx *ctx, tree r); + +static tree +eval_call_expression (const constexpr_ctx *ctx, tree r); + +static tree +eval_binary_expression (const constexpr_ctx *ctx, tree r); + +static tree +get_function_named_in_call (tree t); tree -ConstCtx::fold (tree expr) +fold_expr (tree expr) { - tree folded = ConstCtx ().constexpr_expression (expr); + constexpr_global_ctx global_ctx; + constexpr_ctx ctx = {&global_ctx}; + + tree folded = constexpr_expression (&ctx, expr); rust_assert (folded != NULL_TREE); return folded; } -tree -ConstCtx::constexpr_expression (tree t) +static tree +constexpr_expression (const constexpr_ctx *ctx, tree t) { location_t loc = EXPR_LOCATION (t); + if (CONSTANT_CLASS_P (t)) { if (TREE_OVERFLOW (t)) @@ -67,7 +96,7 @@ ConstCtx::constexpr_expression (tree t) } // Avoid excessively long constexpr evaluations - if (++constexpr_ops_count >= constexpr_ops_limit) + if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit) { rust_error_at ( Location (loc), @@ -136,20 +165,20 @@ ConstCtx::constexpr_expression (tree t) case LTGT_EXPR: case RANGE_EXPR: case COMPLEX_EXPR: - r = eval_binary_expression (t); + r = eval_binary_expression (ctx, t); break; case CALL_EXPR: - r = eval_call_expression (t); + r = eval_call_expression (ctx, t); break; case RETURN_EXPR: rust_assert (TREE_OPERAND (t, 0) != NULL_TREE); - r = constexpr_expression (TREE_OPERAND (t, 0)); + r = constexpr_expression (ctx, TREE_OPERAND (t, 0)); break; case MODIFY_EXPR: - r = eval_store_expression (t); + r = eval_store_expression (ctx, t); break; default: @@ -159,8 +188,8 @@ ConstCtx::constexpr_expression (tree t) return r; } -tree -ConstCtx::eval_store_expression (tree t) +static tree +eval_store_expression (const constexpr_ctx *ctx, tree t) { tree init = TREE_OPERAND (t, 1); if (TREE_CLOBBER_P (init)) @@ -176,7 +205,7 @@ ConstCtx::eval_store_expression (tree t) { /* Evaluate the value to be stored without knowing what object it will be stored in, so that any side-effects happen first. */ - init = ConstCtx::fold (init); + init = fold_expr (init); } bool evaluated = false; @@ -190,7 +219,7 @@ ConstCtx::eval_store_expression (tree t) object = probe; else { - probe = constexpr_expression (probe); + probe = constexpr_expression (ctx, probe); evaluated = true; } break; @@ -202,16 +231,15 @@ ConstCtx::eval_store_expression (tree t) /* Subroutine of cxx_eval_constant_expression. Like cxx_eval_unary_expression, except for binary expressions. */ - -tree -ConstCtx::eval_binary_expression (tree t) +static tree +eval_binary_expression (const constexpr_ctx *ctx, tree t) { tree orig_lhs = TREE_OPERAND (t, 0); tree orig_rhs = TREE_OPERAND (t, 1); tree lhs, rhs; - lhs = constexpr_expression (orig_lhs); - rhs = constexpr_expression (orig_rhs); + lhs = constexpr_expression (ctx, orig_lhs); + rhs = constexpr_expression (ctx, orig_rhs); location_t loc = EXPR_LOCATION (t); enum tree_code code = TREE_CODE (t); @@ -223,19 +251,19 @@ ConstCtx::eval_binary_expression (tree t) // Subroutine of cxx_eval_constant_expression. // Evaluate the call expression tree T in the context of OLD_CALL expression // evaluation. -tree -ConstCtx::eval_call_expression (tree t) +static tree +eval_call_expression (const constexpr_ctx *ctx, tree t) { tree fun = get_function_named_in_call (t); - return constexpr_fn_retval (DECL_SAVED_TREE (fun)); + return constexpr_fn_retval (ctx, DECL_SAVED_TREE (fun)); } // Subroutine of check_constexpr_fundef. BODY is the body of a function // declared to be constexpr, or a sub-statement thereof. Returns the // return value if suitable, error_mark_node for a statement not allowed in // a constexpr function, or NULL_TREE if no return value was found. -tree -ConstCtx::constexpr_fn_retval (tree body) +static tree +constexpr_fn_retval (const constexpr_ctx *ctx, tree body) { switch (TREE_CODE (body)) { @@ -243,7 +271,7 @@ ConstCtx::constexpr_fn_retval (tree body) tree expr = NULL_TREE; for (tree stmt : tsi_range (body)) { - tree s = constexpr_fn_retval (stmt); + tree s = constexpr_fn_retval (ctx, stmt); if (s == error_mark_node) return error_mark_node; else if (s == NULL_TREE) @@ -258,7 +286,7 @@ ConstCtx::constexpr_fn_retval (tree body) } case RETURN_EXPR: - return constexpr_expression (body); + return constexpr_expression (ctx, body); case DECL_EXPR: { tree decl = DECL_EXPR_DECL (body); @@ -270,11 +298,11 @@ ConstCtx::constexpr_fn_retval (tree body) } case CLEANUP_POINT_EXPR: - return constexpr_fn_retval (TREE_OPERAND (body, 0)); + return constexpr_fn_retval (ctx, TREE_OPERAND (body, 0)); case BIND_EXPR: { tree b = BIND_EXPR_BODY (body); - return constexpr_fn_retval (b); + return constexpr_fn_retval (ctx, b); } break; diff --git a/gcc/rust/backend/rust-constexpr.h b/gcc/rust/backend/rust-constexpr.h index 40d9159..3cfcec8 100644 --- a/gcc/rust/backend/rust-constexpr.h +++ b/gcc/rust/backend/rust-constexpr.h @@ -23,22 +23,7 @@ namespace Rust { namespace Compile { -class ConstCtx -{ -public: - static tree fold (tree); - - tree constexpr_expression (tree); - tree eval_binary_expression (tree); - tree eval_call_expression (tree); - tree constexpr_fn_retval (tree); - tree eval_store_expression (tree); - -private: - ConstCtx (); - - HOST_WIDE_INT constexpr_ops_count; -}; +extern tree fold_expr (tree); } // namespace Compile } // namespace Rust |