aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/backend
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-11-23 23:09:25 +0000
committerGitHub <noreply@github.com>2021-11-23 23:09:25 +0000
commit717b6da459b26ace9a3c303cfa5e485ff8935709 (patch)
treefad38abf4fc4875a2d3c72336a154d3892a7f1a6 /gcc/rust/backend
parenta41851dfb5bec6f40fd89db01ae75fee557306ee (diff)
parent8b36f2b80ebd0e0304b6df4020b5992640662667 (diff)
downloadgcc-717b6da459b26ace9a3c303cfa5e485ff8935709.zip
gcc-717b6da459b26ace9a3c303cfa5e485ff8935709.tar.gz
gcc-717b6da459b26ace9a3c303cfa5e485ff8935709.tar.bz2
Merge #814
814: Set TREE_ADDRESSABLE when we need to borrow any expression r=philberty a=philberty GCC requires VAR_DECL's and PARAM_DECL's to be marked with TREE_ADDRESSABLE when the declaration will be used in borrow's ('&' getting the address). This takes into account the implicit addresses when we do autoderef in method resolution/operator-overloading. If it is not set we end up in cases like this: ```c i32 main () { i32 a.1; i32 D.86; i32 a; a = 1; a.1 = a; // this is wrong <i32 as AddAssign>::add_assign (&a.1, 2); D.86 = 0; return D.86; } ``` You can see GCC will automatically make a copy of the VAR_DECL resulting bad code-generation. Fixes #804 Co-authored-by: Philip Herron <philip.herron@embecosm.com>
Diffstat (limited to 'gcc/rust/backend')
-rw-r--r--gcc/rust/backend/rust-compile-fnparam.h33
-rw-r--r--gcc/rust/backend/rust-compile-var-decl.h23
2 files changed, 38 insertions, 18 deletions
diff --git a/gcc/rust/backend/rust-compile-fnparam.h b/gcc/rust/backend/rust-compile-fnparam.h
index 629f0f5..126b49f 100644
--- a/gcc/rust/backend/rust-compile-fnparam.h
+++ b/gcc/rust/backend/rust-compile-fnparam.h
@@ -20,6 +20,7 @@
#define RUST_COMPILE_FNPARAM
#include "rust-compile-base.h"
+#include "rust-hir-address-taken.h"
namespace Rust {
namespace Compile {
@@ -33,9 +34,9 @@ public:
HIR::FunctionParam *param, tree decl_type,
Location locus)
{
- CompileFnParam compiler (ctx, fndecl, decl_type, locus);
+ CompileFnParam compiler (ctx, fndecl, decl_type, locus, *param);
param->get_param_name ()->accept_vis (compiler);
- return compiler.translated;
+ return compiler.compiled_param;
}
void visit (HIR::IdentifierPattern &pattern) override
@@ -43,23 +44,31 @@ public:
if (!pattern.is_mut ())
decl_type = ctx->get_backend ()->immutable_type (decl_type);
- translated
+ 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.variable_ident,
- decl_type,
- false /* address_taken */,
+ decl_type, address_taken,
locus);
}
private:
- CompileFnParam (Context *ctx, tree fndecl, tree decl_type, Location locus)
+ CompileFnParam (Context *ctx, tree fndecl, tree decl_type, Location locus,
+ const HIR::FunctionParam &param)
: HIRCompileBase (ctx), fndecl (fndecl), decl_type (decl_type),
- locus (locus), translated (nullptr)
+ locus (locus), param (param),
+ compiled_param (ctx->get_backend ()->error_variable ()),
+ address_taken_context (Resolver::AddressTakenContext::get ())
{}
tree fndecl;
tree decl_type;
Location locus;
- ::Bvariable *translated;
+ const HIR::FunctionParam &param;
+ Bvariable *compiled_param;
+ const Resolver::AddressTakenContext *address_taken_context;
};
class CompileSelfParam : public HIRCompileBase
@@ -74,9 +83,13 @@ 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,
- false /* address_taken */,
- locus);
+ address_taken, locus);
}
};
diff --git a/gcc/rust/backend/rust-compile-var-decl.h b/gcc/rust/backend/rust-compile-var-decl.h
index a964fa2..097f5b8 100644
--- a/gcc/rust/backend/rust-compile-var-decl.h
+++ b/gcc/rust/backend/rust-compile-var-decl.h
@@ -20,6 +20,7 @@
#define RUST_COMPILE_VAR_DECL
#include "rust-compile-base.h"
+#include "rust-hir-address-taken.h"
namespace Rust {
namespace Compile {
@@ -33,10 +34,9 @@ public:
{
CompileVarDecl compiler (ctx, fndecl);
stmt->accept_vis (compiler);
- rust_assert (compiler.translated != nullptr);
ctx->insert_var_decl (stmt->get_mappings ().get_hirid (),
- compiler.translated);
- return compiler.translated;
+ compiler.compiled_variable);
+ return compiler.compiled_variable;
}
void visit (HIR::LetStmt &stmt) override
@@ -47,6 +47,8 @@ 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);
}
@@ -56,22 +58,27 @@ public:
if (!pattern.is_mut ())
translated_type = ctx->get_backend ()->immutable_type (translated_type);
- translated
+ compiled_variable
= ctx->get_backend ()->local_variable (fndecl, pattern.variable_ident,
translated_type, NULL /*decl_var*/,
- false /*address_taken*/, locus);
+ address_taken, locus);
}
private:
CompileVarDecl (Context *ctx, tree fndecl)
- : HIRCompileBase (ctx), fndecl (fndecl), translated_type (nullptr),
- translated (nullptr)
+ : 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 ())
{}
tree fndecl;
tree translated_type;
Location locus;
- ::Bvariable *translated;
+ Bvariable *compiled_variable;
+ bool address_taken;
+ const Resolver::AddressTakenContext *address_taken_context;
};
} // namespace Compile