From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by sourceware.org (Postfix) with ESMTPS id F37653857831 for ; Tue, 30 Jan 2024 12:11:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F37653857831 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org F37653857831 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::42c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706616718; cv=none; b=pjppZBjdwShtPpd2XiMZ5YkLUnfIdmqwJ7kepHUdBrEmk9pt1htUn4btAbW7lbsJmGTWPZgPS9v+MbGyIZuacsII1bOaxHuyGLrgCPQisI/+Dlx2Saj2kaKFAXgMZrqXwg8tgPD4OuRivFQ6RlLii29d77+mg7gAMLhap8zvikc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706616718; c=relaxed/simple; bh=muSG+mnMwyjYEBCCsIh5pSUVpgtWCtuAdvhiU/IKaS8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=TThslqsmXTT1JJ1UWOMMdHf5hqUbsH6e1gOBZErtzZQMOn96fLwx52d4s+ATdAdANaoZ2Ni7ZxX14mXUrxBmTCLEc7DEtQa+4Q3RU1bcF69xwZuWC+wR2vVhsvVHKPMbPD1l8nkBRyaaNJwacEKfmQRV8KOxRIcAoiKBJ3gxfbM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wr1-x42c.google.com with SMTP id ffacd0b85a97d-33ae51f8111so1919956f8f.2 for ; Tue, 30 Jan 2024 04:11:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1706616687; x=1707221487; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=9GQw+X1oqOP859F5/qSRO7FfkR9Dk6k7ONV6/zfxrVE=; b=gt1hC2VBRjoYXXLNW2nxdtQI4zUZNAqdvy1ZlxmCVO5NLuZGlM0Hn1buHPHrrtkjO6 J/SCS0mAbU8Br8Cac2aBiry09i3/YzhqBhg+KQFtyUUOKVSg5SXAdNIAnGm1mkRJ8MQ1 HXMvUlLqXyXvcnqWGrvIRyGwPI3erFzN1Z6feLsW0YrlLkbM0BL11UaHrZfGZFs1cQ1O bkGgXl7i6ZbSeLWAURPdnmH7m14tpz+GtEUDZ3ceHcIGJOaXL0BtkHi7j928vGKrU4vY DeyuHets6kIKAs9LXyvaX/VAzPSLVpQoXcImL6/WL521jxVwvjY0LxY+niyDUfqR99Xj I7JQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706616687; x=1707221487; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=9GQw+X1oqOP859F5/qSRO7FfkR9Dk6k7ONV6/zfxrVE=; b=cMvnF+E+Dj8fWl6AbLyx3aLFtfKRvcR5xfjDE1v0gOvlFkKRB1XfaGh4l0n63WZydR V+qKB1elkeZJ+HPpcRCxeE+b2LrXiHTmciq8w8waoVLC533JZwp0BewmWu1T/7eZn54X dYiMo2T0ck2yipm1a+B2dgBMtF68uk9wWm/nsYmoIeGpMpJQM3Gu/hJxVZ610Xvj2jWR fVaZSk39cFlql7Hvy16N3UmrdSIrsl9BNWqJGrcIs5Nq9UQFtIutuoy+hePPPieazmej ikHZF1Sf4GJAWTT6UVO8s1FcS4RVKVeLnGRLgjvqPywDyUl5VuUTm4e2fI11kNNtKR1k HPDw== X-Gm-Message-State: AOJu0YzirHS+2dFsqMETAqGSmLjUJyEZzc9mu/WsroWdznRX4fbBIx57 y1XN/KN4YhRw1ulrcgjMLV213CbcilJYHHk2hxdh3/z08wHAnLZmJ1A2G/sQJw== X-Google-Smtp-Source: AGHT+IGQQBc7E8wIn6YtJpi3/ySO2EQ3yLgghANY+O8zkpeaw/UGRZJG1Yd7m6wpXnX40LLcb/y7nQ== X-Received: by 2002:adf:f5c8:0:b0:33a:e544:ecc7 with SMTP id k8-20020adff5c8000000b0033ae544ecc7mr5091416wrp.9.1706616686829; Tue, 30 Jan 2024 04:11:26 -0800 (PST) Received: from platypus.localdomain ([62.23.166.218]) by smtp.gmail.com with ESMTPSA id f9-20020a056000036900b00339307d9d31sm10569894wrf.112.2024.01.30.04.11.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 04:11:26 -0800 (PST) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Philip Herron Subject: [COMMITTED 049/101] gccrs: Remove backend dependancy on resolution rib information Date: Tue, 30 Jan 2024 13:07:05 +0100 Message-ID: <20240130121026.807464-52-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20240130121026.807464-2-arthur.cohen@embecosm.com> References: <20240130121026.807464-2-arthur.cohen@embecosm.com> Reply-To: arthur.cohen@embecosm.com MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-14.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: From: Philip Herron 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 --- gcc/rust/backend/rust-compile-base.cc | 51 +----------------- gcc/rust/backend/rust-compile-base.h | 3 -- gcc/rust/backend/rust-compile-block.cc | 13 +---- gcc/rust/backend/rust-compile-expr.cc | 5 +- gcc/rust/backend/rust-compile-stmt.cc | 16 ++++-- gcc/rust/backend/rust-compile-var-decl.h | 69 +++++++++++++++++++----- 6 files changed, 71 insertions(+), 86 deletions(-) diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc index ae9f6707b72..fcab75bef1c 100644 --- a/gcc/rust/backend/rust-compile-base.cc +++ b/gcc/rust/backend/rust-compile-base.cc @@ -564,35 +564,6 @@ HIRCompileBase::indirect_expression (tree expr, location_t locus) return build_fold_indirect_ref_loc (locus, expr); } -std::vector -HIRCompileBase::compile_locals_for_block (Context *ctx, Resolver::Rib &rib, - tree fndecl) -{ - std::vector 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, @@ -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 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); @@ -820,7 +781,6 @@ HIRCompileBase::compile_constant_item ( tree fndecl = Backend::function (compiled_fn_type, ident, "", 0, locus); TREE_READONLY (fndecl) = 1; - std::vector locals; tree enclosing_scope = NULL_TREE; location_t start_location = const_value_expr->get_locus (); location_t end_location = const_value_expr->get_locus (); @@ -830,16 +790,9 @@ HIRCompileBase::compile_constant_item ( = static_cast (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); diff --git a/gcc/rust/backend/rust-compile-base.h b/gcc/rust/backend/rust-compile-base.h index 65291234b23..c5816584c72 100644 --- a/gcc/rust/backend/rust-compile-base.h +++ b/gcc/rust/backend/rust-compile-base.h @@ -145,9 +145,6 @@ protected: static bool mark_addressable (tree, location_t); - static std::vector - 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); diff --git a/gcc/rust/backend/rust-compile-block.cc b/gcc/rust/backend/rust-compile-block.cc index 83ecc672592..10a70999773 100644 --- a/gcc/rust/backend/rust-compile-block.cc +++ b/gcc/rust/backend/rust-compile-block.cc @@ -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 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); diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index 870ecab5ba0..e0917f68660 100644 --- a/gcc/rust/backend/rust-compile-expr.cc +++ b/gcc/rust/backend/rust-compile-expr.cc @@ -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 locals = {}; if (is_block_expr) { auto body_mappings = function_body->get_mappings (); @@ -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; @@ -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); diff --git a/gcc/rust/backend/rust-compile-stmt.cc b/gcc/rust/backend/rust-compile-stmt.cc index c006f4ace62..e7ba370eb8e 100644 --- a/gcc/rust/backend/rust-compile-stmt.cc +++ b/gcc/rust/backend/rust-compile-stmt.cc @@ -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 { @@ -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 (); @@ -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 diff --git a/gcc/rust/backend/rust-compile-var-decl.h b/gcc/rust/backend/rust-compile-var-decl.h index 370e939cd0c..45ca01d4f70 100644 --- a/gcc/rust/backend/rust-compile-var-decl.h +++ b/gcc/rust/backend/rust-compile-var-decl.h @@ -30,11 +30,12 @@ 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 &locals, Context *ctx) + static std::vector 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 @@ -42,15 +43,58 @@ public: 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 ( + *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. @@ -62,21 +106,18 @@ public: 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 &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 &locals; + std::vector vars; }; } // namespace Compile -- 2.42.1