Skip to content

Commit

Permalink
gccrs: Remove backend dependancy on resolution rib information
Browse files Browse the repository at this point in the history
When making more desugaring for the HIR we can need to add new Let bindings
which will require namesolution information but also rib information for
which block the let binding is associated which was very unnessecary. This
patch simply updates the BLOCK_CONTEXT of the current scope as we are
compiling and encounter HIR::LetStmts instead of trying to do it all
upfront which lots of wierd checks

gcc/rust/ChangeLog:

	* backend/rust-compile-base.cc (HIRCompileBase::compile_locals_for_block): removed
	* backend/rust-compile-base.h: update header
	* backend/rust-compile-block.cc (CompileBlock::visit): remove old logic
	* backend/rust-compile-expr.cc (CompileExpr::generate_closure_function): likewise
	* backend/rust-compile-stmt.cc (CompileStmt::visit): likewise
	* backend/rust-compile-var-decl.h: ensure we setup tuple bindings correctly

Signed-off-by: Philip Herron <[email protected]>
  • Loading branch information
philberty committed Nov 30, 2023
1 parent 16d0a2d commit 0ddb160
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 86 deletions.
51 changes: 2 additions & 49 deletions gcc/rust/backend/rust-compile-base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -564,35 +564,6 @@ HIRCompileBase::indirect_expression (tree expr, location_t locus)
return build_fold_indirect_ref_loc (locus, expr);
}

std::vector<Bvariable *>
HIRCompileBase::compile_locals_for_block (Context *ctx, Resolver::Rib &rib,
tree fndecl)
{
std::vector<Bvariable *> locals;
for (auto it : rib.get_declarations ())
{
NodeId node_id = it.first;
HirId ref = UNKNOWN_HIRID;
if (!ctx->get_mappings ()->lookup_node_to_hir (node_id, &ref))
continue;

// we only care about local patterns
HIR::Pattern *pattern = ctx->get_mappings ()->lookup_hir_pattern (ref);
if (pattern == nullptr)
continue;

// lookup the type
TyTy::BaseType *tyty = nullptr;
if (!ctx->get_tyctx ()->lookup_type (ref, &tyty))
continue;

// compile the local
tree type = TyTyResolveCompile::compile (ctx, tyty);
CompileVarDecl::compile (fndecl, type, pattern, locals, ctx);
}
return locals;
}

void
HIRCompileBase::compile_function_body (tree fndecl,
HIR::BlockExpr &function_body,
Expand Down Expand Up @@ -750,21 +721,11 @@ HIRCompileBase::compile_function (
if (!Backend::function_set_parameters (fndecl, param_vars))
return error_mark_node;

// lookup locals
auto body_mappings = function_body->get_mappings ();
Resolver::Rib *rib = nullptr;
bool ok
= ctx->get_resolver ()->find_name_rib (body_mappings.get_nodeid (), &rib);
rust_assert (ok);

std::vector<Bvariable *> locals
= compile_locals_for_block (ctx, *rib, fndecl);

tree enclosing_scope = NULL_TREE;
location_t start_location = function_body->get_locus ();
location_t end_location = function_body->get_end_locus ();

tree code_block = Backend::block (fndecl, enclosing_scope, locals,
tree code_block = Backend::block (fndecl, enclosing_scope, {} /*locals*/,
start_location, end_location);
ctx->push_block (code_block);

Expand Down Expand Up @@ -820,7 +781,6 @@ HIRCompileBase::compile_constant_item (
tree fndecl = Backend::function (compiled_fn_type, ident, "", 0, locus);
TREE_READONLY (fndecl) = 1;

std::vector<Bvariable *> locals;
tree enclosing_scope = NULL_TREE;
location_t start_location = const_value_expr->get_locus ();
location_t end_location = const_value_expr->get_locus ();
Expand All @@ -830,16 +790,9 @@ HIRCompileBase::compile_constant_item (
= static_cast<HIR::BlockExpr *> (const_value_expr);
start_location = function_body->get_locus ();
end_location = function_body->get_end_locus ();

Resolver::Rib *rib = nullptr;
bool ok = ctx->get_resolver ()->find_name_rib (
function_body->get_mappings ().get_nodeid (), &rib);
rust_assert (ok);

locals = compile_locals_for_block (ctx, *rib, fndecl);
}

tree code_block = Backend::block (fndecl, enclosing_scope, locals,
tree code_block = Backend::block (fndecl, enclosing_scope, {} /*locals*/,
start_location, end_location);
ctx->push_block (code_block);

Expand Down
3 changes: 0 additions & 3 deletions gcc/rust/backend/rust-compile-base.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,6 @@ class HIRCompileBase

static bool mark_addressable (tree, location_t);

static std::vector<Bvariable *>
compile_locals_for_block (Context *ctx, Resolver::Rib &rib, tree fndecl);

static tree named_constant_expression (tree type_tree,
const std::string &name,
tree const_val, location_t location);
Expand Down
13 changes: 1 addition & 12 deletions gcc/rust/backend/rust-compile-block.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,9 @@ CompileBlock::visit (HIR::BlockExpr &expr)
tree fndecl = fnctx.fndecl;
location_t start_location = expr.get_locus ();
location_t end_location = expr.get_end_locus ();
auto body_mappings = expr.get_mappings ();

Resolver::Rib *rib = nullptr;
if (!ctx->get_resolver ()->find_name_rib (body_mappings.get_nodeid (), &rib))
{
rust_fatal_error (expr.get_locus (), "failed to setup locals per block");
return;
}

std::vector<Bvariable *> locals
= compile_locals_for_block (ctx, *rib, fndecl);

tree enclosing_scope = ctx->peek_enclosing_scope ();
tree new_block = Backend::block (fndecl, enclosing_scope, locals,
tree new_block = Backend::block (fndecl, enclosing_scope, {} /*locals*/,
start_location, end_location);
ctx->push_block (new_block);

Expand Down
5 changes: 1 addition & 4 deletions gcc/rust/backend/rust-compile-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2310,7 +2310,6 @@ CompileExpr::generate_closure_function (HIR::ClosureExpr &expr,
bool is_block_expr
= function_body->get_expression_type () == HIR::Expr::ExprType::Block;

std::vector<Bvariable *> locals = {};
if (is_block_expr)
{
auto body_mappings = function_body->get_mappings ();
Expand All @@ -2319,8 +2318,6 @@ CompileExpr::generate_closure_function (HIR::ClosureExpr &expr,
= ctx->get_resolver ()->find_name_rib (body_mappings.get_nodeid (),
&rib);
rust_assert (ok);

locals = compile_locals_for_block (ctx, *rib, fndecl);
}

tree enclosing_scope = NULL_TREE;
Expand All @@ -2333,7 +2330,7 @@ CompileExpr::generate_closure_function (HIR::ClosureExpr &expr,
end_location = body->get_end_locus ();
}

tree code_block = Backend::block (fndecl, enclosing_scope, locals,
tree code_block = Backend::block (fndecl, enclosing_scope, {} /*locals*/,
start_location, end_location);
ctx->push_block (code_block);

Expand Down
16 changes: 12 additions & 4 deletions gcc/rust/backend/rust-compile-stmt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "rust-compile-pattern.h"
#include "rust-compile-stmt.h"
#include "rust-compile-expr.h"
#include "rust-compile-type.h"
#include "rust-compile-var-decl.h"

namespace Rust {
namespace Compile {
Expand All @@ -44,10 +46,6 @@ CompileStmt::visit (HIR::ExprStmt &stmt)
void
CompileStmt::visit (HIR::LetStmt &stmt)
{
// nothing to do
if (!stmt.has_init_expr ())
return;

HIR::Pattern &stmt_pattern = *stmt.get_pattern ();
HirId stmt_id = stmt_pattern.get_mappings ().get_hirid ();

Expand All @@ -60,6 +58,16 @@ CompileStmt::visit (HIR::LetStmt &stmt)
return;
}

// setup var decl nodes
fncontext fnctx = ctx->peek_fn ();
tree fndecl = fnctx.fndecl;
tree translated_type = TyTyResolveCompile::compile (ctx, ty);
CompileVarDecl::compile (fndecl, translated_type, &stmt_pattern, ctx);

// nothing to do
if (!stmt.has_init_expr ())
return;

tree init = CompileExpr::Compile (stmt.get_init_expr ().get (), ctx);
// FIXME use error_mark_node, check that CompileExpr returns error_mark_node
// on failure and make this an assertion
Expand Down
69 changes: 55 additions & 14 deletions gcc/rust/backend/rust-compile-var-decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,71 @@ class CompileVarDecl : public HIRCompileBase, public HIR::HIRPatternVisitor
using HIR::HIRPatternVisitor::visit;

public:
static void compile (tree fndecl, tree translated_type, HIR::Pattern *pattern,
std::vector<Bvariable *> &locals, Context *ctx)
static std::vector<Bvariable *> compile (tree fndecl, tree translated_type,
HIR::Pattern *pattern, Context *ctx)
{
CompileVarDecl compiler (ctx, fndecl, translated_type, locals);
CompileVarDecl compiler (ctx, fndecl, translated_type);
pattern->accept_vis (compiler);
return compiler.vars;
}

void visit (HIR::IdentifierPattern &pattern) override
{
if (!pattern.is_mut ())
translated_type = Backend::immutable_type (translated_type);

Bvariable *var
= Backend::local_variable (fndecl, pattern.get_identifier ().as_string (),
translated_type, NULL /*decl_var*/,
pattern.get_locus ());
tree bind_tree = ctx->peek_enclosing_scope ();
std::string identifier = pattern.get_identifier ().as_string ();
tree decl
= build_decl (pattern.get_locus (), VAR_DECL,
Backend::get_identifier_node (identifier), translated_type);
DECL_CONTEXT (decl) = fndecl;

gcc_assert (TREE_CODE (bind_tree) == BIND_EXPR);
tree block_tree = BIND_EXPR_BLOCK (bind_tree);
gcc_assert (TREE_CODE (block_tree) == BLOCK);
DECL_CHAIN (decl) = BLOCK_VARS (block_tree);
BLOCK_VARS (block_tree) = decl;
BIND_EXPR_VARS (bind_tree) = BLOCK_VARS (block_tree);

rust_preserve_from_gc (decl);
Bvariable *var = new Bvariable (decl);

HirId stmt_id = pattern.get_mappings ().get_hirid ();
ctx->insert_var_decl (stmt_id, var);

locals.push_back (var);
vars.push_back (var);
}

void visit (HIR::TuplePattern &pattern) override
{
switch (pattern.get_items ()->get_item_type ())
{
case HIR::TuplePatternItems::ItemType::MULTIPLE: {
rust_assert (TREE_CODE (translated_type) == RECORD_TYPE);
auto &items = static_cast<HIR::TuplePatternItemsMultiple &> (
*pattern.get_items ());

size_t offs = 0;
for (auto &sub : items.get_patterns ())
{
tree sub_ty = error_mark_node;
tree field = TYPE_FIELDS (translated_type);
for (size_t i = 0; i < offs; i++)
{
field = DECL_CHAIN (field);
gcc_assert (field != NULL_TREE);
}
sub_ty = TREE_TYPE (field);
CompileVarDecl::compile (fndecl, sub_ty, sub.get (), ctx);
offs++;
}
}
break;

default:
break;
}
}

// Empty visit for unused Pattern HIR nodes.
Expand All @@ -62,21 +106,18 @@ class CompileVarDecl : public HIRCompileBase, public HIR::HIRPatternVisitor
void visit (HIR::ReferencePattern &) override {}
void visit (HIR::SlicePattern &) override {}
void visit (HIR::StructPattern &) override {}
void visit (HIR::TuplePattern &) override {}
void visit (HIR::TupleStructPattern &) override {}
void visit (HIR::WildcardPattern &) override {}

private:
CompileVarDecl (Context *ctx, tree fndecl, tree translated_type,
std::vector<Bvariable *> &locals)
: HIRCompileBase (ctx), fndecl (fndecl), translated_type (translated_type),
locals (locals)
CompileVarDecl (Context *ctx, tree fndecl, tree translated_type)
: HIRCompileBase (ctx), fndecl (fndecl), translated_type (translated_type)
{}

tree fndecl;
tree translated_type;

std::vector<Bvariable *> &locals;
std::vector<Bvariable *> vars;
};

} // namespace Compile
Expand Down

0 comments on commit 0ddb160

Please sign in to comment.