diff options
author | Philip Herron <philip.herron@embecosm.com> | 2022-02-10 15:40:23 +0000 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2022-02-10 15:40:55 +0000 |
commit | 859732e4d167dfe83b29cebc757f21ba2c342f33 (patch) | |
tree | 2616a6c1fdc52ae274b836a055e710f19bc59ffb /gcc/rust/backend | |
parent | f6ba472caf42db1f5f2f98b73afccf448b36c322 (diff) | |
download | gcc-859732e4d167dfe83b29cebc757f21ba2c342f33.zip gcc-859732e4d167dfe83b29cebc757f21ba2c342f33.tar.gz gcc-859732e4d167dfe83b29cebc757f21ba2c342f33.tar.bz2 |
Remove AddressTakenContext
We can reuse more C front-end code c_mark_addressable can be used instead
of trying to track TREE_ADDRESSABLE as part of type-checking. This also
pulls the GCC::Backend::address_expression to be part of the HIRCompileBase
class during code-generation.
Diffstat (limited to 'gcc/rust/backend')
-rw-r--r-- | gcc/rust/backend/rust-compile-base.cc | 81 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-base.h | 4 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-expr.cc | 17 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-expr.h | 8 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-fnparam.h | 19 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-var-decl.h | 17 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile.cc | 3 |
7 files changed, 109 insertions, 40 deletions
diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc index 82a38e7..81598c4 100644 --- a/gcc/rust/backend/rust-compile-base.cc +++ b/gcc/rust/backend/rust-compile-base.cc @@ -17,6 +17,7 @@ // <http://www.gnu.org/licenses/>. #include "rust-compile-base.h" +#include "fold-const.h" #include "stringpool.h" namespace Rust { @@ -88,5 +89,85 @@ HIRCompileBase::setup_abi_options (tree fndecl, ABI abi) } } +// ported from gcc/c/c-typecheck.c +// +// Mark EXP saying that we need to be able to take the +// address of it; it should not be allocated in a register. +// Returns true if successful. ARRAY_REF_P is true if this +// is for ARRAY_REF construction - in that case we don't want +// to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE, +// it is fine to use ARRAY_REFs for vector subscripts on vector +// register variables. +bool +HIRCompileBase::mark_addressable (tree exp, Location locus) +{ + tree x = exp; + + while (1) + switch (TREE_CODE (x)) + { + case VIEW_CONVERT_EXPR: + if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE + && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0)))) + return true; + x = TREE_OPERAND (x, 0); + break; + + case COMPONENT_REF: + // TODO + // if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1))) + // { + // error ("cannot take address of bit-field %qD", TREE_OPERAND (x, + // 1)); return false; + // } + + /* FALLTHRU */ + case ADDR_EXPR: + case ARRAY_REF: + case REALPART_EXPR: + case IMAGPART_EXPR: + x = TREE_OPERAND (x, 0); + break; + + case COMPOUND_LITERAL_EXPR: + TREE_ADDRESSABLE (x) = 1; + TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1; + return true; + + case CONSTRUCTOR: + TREE_ADDRESSABLE (x) = 1; + return true; + + case VAR_DECL: + case CONST_DECL: + case PARM_DECL: + case RESULT_DECL: + // (we don't have a concept of a "register" declaration) + // fallthrough */ + + /* FALLTHRU */ + case FUNCTION_DECL: + TREE_ADDRESSABLE (x) = 1; + + /* FALLTHRU */ + default: + return true; + } + + return false; +} + +tree +HIRCompileBase::address_expression (tree expr, Location location) +{ + if (expr == error_mark_node) + return error_mark_node; + + if (!mark_addressable (expr, location)) + return error_mark_node; + + return build_fold_addr_expr_loc (location.gcc_location (), expr); +} + } // namespace Compile } // namespace Rust diff --git a/gcc/rust/backend/rust-compile-base.h b/gcc/rust/backend/rust-compile-base.h index f318e81..ec75356 100644 --- a/gcc/rust/backend/rust-compile-base.h +++ b/gcc/rust/backend/rust-compile-base.h @@ -77,6 +77,10 @@ protected: const HIR::FunctionQualifiers &qualifiers, const AST::AttrVec &attrs); static void setup_abi_options (tree fndecl, ABI abi); + + static tree address_expression (tree, Location); + + static bool mark_addressable (tree, Location); }; } // namespace Compile diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index d49a6dc..f65e1fd 100644 --- a/gcc/rust/backend/rust-compile-expr.cc +++ b/gcc/rust/backend/rust-compile-expr.cc @@ -124,6 +124,13 @@ CompileExpr::visit (HIR::NegationExpr &expr) } void +CompileExpr::visit (HIR::BorrowExpr &expr) +{ + tree main_expr = CompileExpr::Compile (expr.get_expr ().get (), ctx); + translated = address_expression (main_expr, expr.get_locus ()); +} + +void CompileExpr::visit (HIR::DereferenceExpr &expr) { TyTy::BaseType *tyty = nullptr; @@ -973,7 +980,7 @@ CompileExpr::compile_string_literal (const HIR::LiteralExpr &expr, auto base = ctx->get_backend ()->string_constant_expression ( literal_value.as_string ()); - return ctx->get_backend ()->address_expression (base, expr.get_locus ()); + return address_expression (base, expr.get_locus ()); } tree @@ -1006,8 +1013,7 @@ CompileExpr::compile_byte_string_literal (const HIR::LiteralExpr &expr, vals, expr.get_locus ()); - return ctx->get_backend ()->address_expression (constructed, - expr.get_locus ()); + return address_expression (constructed, expr.get_locus ()); } tree @@ -1190,7 +1196,7 @@ HIRCompileBase::resolve_adjustements ( case Resolver::Adjustment::AdjustmentType::IMM_REF: case Resolver::Adjustment::AdjustmentType::MUT_REF: - e = ctx->get_backend ()->address_expression (e, locus); + e = address_expression (e, locus); break; case Resolver::Adjustment::AdjustmentType::DEREF_REF: @@ -1235,8 +1241,7 @@ HIRCompileBase::resolve_deref_adjustment (Resolver::Adjustment &adjustment, != Resolver::Adjustment::AdjustmentType::ERROR; if (needs_borrow) { - adjusted_argument - = ctx->get_backend ()->address_expression (expression, locus); + adjusted_argument = address_expression (expression, locus); } // make the call diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h index 43eff72..592d280 100644 --- a/gcc/rust/backend/rust-compile-expr.h +++ b/gcc/rust/backend/rust-compile-expr.h @@ -797,13 +797,7 @@ public: ctx->add_statement (goto_label); } - void visit (HIR::BorrowExpr &expr) override - { - tree main_expr = CompileExpr::Compile (expr.get_expr ().get (), ctx); - - translated - = ctx->get_backend ()->address_expression (main_expr, expr.get_locus ()); - } + void visit (HIR::BorrowExpr &expr) override; void visit (HIR::DereferenceExpr &expr) override; diff --git a/gcc/rust/backend/rust-compile-fnparam.h b/gcc/rust/backend/rust-compile-fnparam.h index d8b63b6..137bcad 100644 --- a/gcc/rust/backend/rust-compile-fnparam.h +++ b/gcc/rust/backend/rust-compile-fnparam.h @@ -20,7 +20,6 @@ #define RUST_COMPILE_FNPARAM #include "rust-compile-base.h" -#include "rust-hir-address-taken.h" namespace Rust { namespace Compile { @@ -34,7 +33,7 @@ public: HIR::FunctionParam *param, tree decl_type, Location locus) { - CompileFnParam compiler (ctx, fndecl, decl_type, locus, *param); + CompileFnParam compiler (ctx, fndecl, decl_type, locus); param->get_param_name ()->accept_vis (compiler); return compiler.compiled_param; } @@ -45,9 +44,6 @@ public: decl_type = ctx->get_backend ()->immutable_type (decl_type); bool address_taken = false; - address_taken_context->lookup_addess_taken ( - param.get_mappings ().get_hirid (), &address_taken); - compiled_param = ctx->get_backend ()->parameter_variable ( fndecl, pattern.get_identifier (), decl_type, address_taken, locus); } @@ -63,20 +59,15 @@ public: } private: - CompileFnParam (Context *ctx, tree fndecl, tree decl_type, Location locus, - const HIR::FunctionParam ¶m) + CompileFnParam (Context *ctx, tree fndecl, tree decl_type, Location locus) : HIRCompileBase (ctx), fndecl (fndecl), decl_type (decl_type), - locus (locus), param (param), - compiled_param (ctx->get_backend ()->error_variable ()), - address_taken_context (Resolver::AddressTakenContext::get ()) + locus (locus), compiled_param (ctx->get_backend ()->error_variable ()) {} tree fndecl; tree decl_type; Location locus; - const HIR::FunctionParam ¶m; Bvariable *compiled_param; - const Resolver::AddressTakenContext *address_taken_context; }; class CompileSelfParam : public HIRCompileBase @@ -91,11 +82,7 @@ public: if (is_immutable) decl_type = ctx->get_backend ()->immutable_type (decl_type); - const auto &address_taken_context = Resolver::AddressTakenContext::get (); bool address_taken = false; - address_taken_context->lookup_addess_taken ( - self.get_mappings ().get_hirid (), &address_taken); - return ctx->get_backend ()->parameter_variable (fndecl, "self", decl_type, address_taken, locus); } diff --git a/gcc/rust/backend/rust-compile-var-decl.h b/gcc/rust/backend/rust-compile-var-decl.h index 48becfa..1da6cd4 100644 --- a/gcc/rust/backend/rust-compile-var-decl.h +++ b/gcc/rust/backend/rust-compile-var-decl.h @@ -20,7 +20,6 @@ #define RUST_COMPILE_VAR_DECL #include "rust-compile-base.h" -#include "rust-hir-address-taken.h" namespace Rust { namespace Compile { @@ -47,9 +46,6 @@ public: &resolved_type); rust_assert (ok); - address_taken_context->lookup_addess_taken ( - stmt.get_mappings ().get_hirid (), &address_taken); - translated_type = TyTyResolveCompile::compile (ctx, resolved_type); stmt.get_pattern ()->accept_vis (*this); } @@ -59,6 +55,9 @@ public: if (!pattern.is_mut ()) translated_type = ctx->get_backend ()->immutable_type (translated_type); + // this gets updated when the compilation _actually_ wants to take an + // address + bool address_taken = false; compiled_variable = ctx->get_backend ()->local_variable (fndecl, pattern.get_identifier (), translated_type, NULL /*decl_var*/, @@ -68,6 +67,10 @@ public: void visit (HIR::WildcardPattern &pattern) override { translated_type = ctx->get_backend ()->immutable_type (translated_type); + + // this gets updated when the compilation _actually_ wants to take an + // address + bool address_taken = false; compiled_variable = ctx->get_backend ()->local_variable (fndecl, "_", translated_type, NULL /*decl_var*/, address_taken, @@ -78,17 +81,13 @@ private: CompileVarDecl (Context *ctx, tree fndecl) : HIRCompileBase (ctx), fndecl (fndecl), translated_type (ctx->get_backend ()->error_type ()), - compiled_variable (ctx->get_backend ()->error_variable ()), - address_taken (false), - address_taken_context (Resolver::AddressTakenContext::get ()) + compiled_variable (ctx->get_backend ()->error_variable ()) {} tree fndecl; tree translated_type; Location locus; Bvariable *compiled_variable; - bool address_taken; - const Resolver::AddressTakenContext *address_taken_context; }; } // namespace Compile diff --git a/gcc/rust/backend/rust-compile.cc b/gcc/rust/backend/rust-compile.cc index b0447af..2299ddb 100644 --- a/gcc/rust/backend/rust-compile.cc +++ b/gcc/rust/backend/rust-compile.cc @@ -387,8 +387,7 @@ HIRCompileBase::coerce_to_dyn_object (tree compiled_ref, rust_assert (ok); resulting_dyn_object_ref - = ctx->get_backend ()->address_expression (resulting_dyn_object_ref, - locus); + = address_expression (resulting_dyn_object_ref, locus); } return resulting_dyn_object_ref; } |