diff options
author | Philip Herron <philip.herron@embecosm.com> | 2020-12-21 18:32:15 +0000 |
---|---|---|
committer | Philip Herron <herron.philip@googlemail.com> | 2020-12-23 13:04:50 +0000 |
commit | aa2fbb5e48f6218035d7bde1336345cebf120d3e (patch) | |
tree | e69dd540813c69f43fc05a76d33395a896791292 /gcc/rust/backend | |
parent | b021811ab700156e1e3d56200d585b3180264f4f (diff) | |
download | gcc-aa2fbb5e48f6218035d7bde1336345cebf120d3e.zip gcc-aa2fbb5e48f6218035d7bde1336345cebf120d3e.tar.gz gcc-aa2fbb5e48f6218035d7bde1336345cebf120d3e.tar.bz2 |
Bring conditionals back since the HIR change.
Diffstat (limited to 'gcc/rust/backend')
-rw-r--r-- | gcc/rust/backend/rust-compile-block.h | 112 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-expr.h | 26 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-stmt.h | 12 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile.cc | 109 |
4 files changed, 259 insertions, 0 deletions
diff --git a/gcc/rust/backend/rust-compile-block.h b/gcc/rust/backend/rust-compile-block.h new file mode 100644 index 0000000..b17fb05 --- /dev/null +++ b/gcc/rust/backend/rust-compile-block.h @@ -0,0 +1,112 @@ +// Copyright (C) 2020 Free Software Foundation, Inc. + +// This file is part of GCC. + +// GCC is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3, or (at your option) any later +// version. + +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. + +// You should have received a copy of the GNU General Public License +// along with GCC; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#ifndef RUST_COMPILE_BLOCK +#define RUST_COMPILE_BLOCK + +#include "rust-compile-base.h" +#include "rust-compile-tyty.h" + +namespace Rust { +namespace Compile { + +class CompileBlock : public HIRCompileBase +{ +public: + static Bblock *compile (HIR::BlockExpr *expr, Context *ctx) + { + CompileBlock compiler (ctx); + expr->accept_vis (compiler); + return compiler.translated; + } + + ~CompileBlock () {} + + void visit (HIR::BlockExpr &expr); + +private: + CompileBlock (Context *ctx) : HIRCompileBase (ctx), translated (nullptr) {} + + Bblock *translated; +}; + +class CompileConditionalBlocks : public HIRCompileBase +{ +public: + static Bstatement *compile (HIR::IfExpr *expr, Context *ctx) + { + CompileConditionalBlocks resolver (ctx); + expr->accept_vis (resolver); + return resolver.translated; + } + + ~CompileConditionalBlocks () {} + + void visit (HIR::IfExpr &expr); + + void visit (HIR::IfExprConseqElse &expr); + + void visit (HIR::IfExprConseqIf &expr); + +private: + CompileConditionalBlocks (Context *ctx) + : HIRCompileBase (ctx), translated (nullptr) + {} + + Bstatement *translated; +}; + +class CompileExprWithBlock : public HIRCompileBase +{ +public: + static Bstatement *compile (HIR::ExprWithBlock *expr, Context *ctx) + { + CompileExprWithBlock resolver (ctx); + expr->accept_vis (resolver); + return resolver.translated; + } + + ~CompileExprWithBlock () {} + + void visit (HIR::IfExpr &expr) + { + translated = CompileConditionalBlocks::compile (&expr, ctx); + } + + void visit (HIR::IfExprConseqElse &expr) + { + translated = CompileConditionalBlocks::compile (&expr, ctx); + } + + void visit (HIR::IfExprConseqIf &expr) + { + translated = CompileConditionalBlocks::compile (&expr, ctx); + } + +private: + CompileExprWithBlock (Context *ctx) + : HIRCompileBase (ctx), translated (nullptr) + {} + + Bstatement *translated; +}; + +} // namespace Compile +} // namespace Rust + +#endif // RUST_COMPILE_BLOCK diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h index 7808af2..854fe5e 100644 --- a/gcc/rust/backend/rust-compile-expr.h +++ b/gcc/rust/backend/rust-compile-expr.h @@ -22,6 +22,7 @@ #include "rust-compile-base.h" #include "rust-compile-tyty.h" #include "rust-compile-resolve-path.h" +#include "rust-compile-block.h" namespace Rust { namespace Compile { @@ -273,6 +274,31 @@ public: expr.get_locus ()); } + void visit (HIR::IfExpr &expr) + { + auto stmt = CompileConditionalBlocks::compile (&expr, ctx); + ctx->add_statement (stmt); + } + + void visit (HIR::IfExprConseqElse &expr) + { + auto stmt = CompileConditionalBlocks::compile (&expr, ctx); + ctx->add_statement (stmt); + } + + void visit (HIR::IfExprConseqIf &expr) + { + auto stmt = CompileConditionalBlocks::compile (&expr, ctx); + ctx->add_statement (stmt); + } + + void visit (HIR::BlockExpr &expr) + { + auto code_block = CompileBlock::compile (&expr, ctx); + auto block_stmt = ctx->get_backend ()->block_statement (code_block); + ctx->add_statement (block_stmt); + } + private: CompileExpr (Context *ctx) : HIRCompileBase (ctx), translated (nullptr) {} diff --git a/gcc/rust/backend/rust-compile-stmt.h b/gcc/rust/backend/rust-compile-stmt.h index 0a08130..5f7decb 100644 --- a/gcc/rust/backend/rust-compile-stmt.h +++ b/gcc/rust/backend/rust-compile-stmt.h @@ -38,6 +38,18 @@ public: virtual ~CompileStmt () {} + void visit (HIR::ExprStmtWithBlock &stmt) + { + ok = true; + auto translated = CompileExpr::Compile (stmt.get_expr (), ctx); + + // these can be null + if (translated == nullptr) + return; + + gcc_unreachable (); + } + void visit (HIR::ExprStmtWithoutBlock &stmt) { ok = true; diff --git a/gcc/rust/backend/rust-compile.cc b/gcc/rust/backend/rust-compile.cc index 11c380b..02fa3a0 100644 --- a/gcc/rust/backend/rust-compile.cc +++ b/gcc/rust/backend/rust-compile.cc @@ -18,6 +18,7 @@ #include "rust-compile.h" #include "rust-compile-item.h" +#include "rust-compile-expr.h" namespace Rust { namespace Compile { @@ -43,5 +44,113 @@ CompileCrate::go () CompileItem::compile (it->get (), ctx); } +// rust-compile-block.h + +void +CompileBlock::visit (HIR::BlockExpr &expr) +{ + fncontext fnctx = ctx->peek_fn (); + Bfunction *fndecl = fnctx.fndecl; + Location start_location = expr.get_locus (); + Location end_location = expr.get_closing_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; + rib->iterate_decls ([&] (NodeId n) mutable -> bool { + Resolver::Definition d; + bool ok = ctx->get_resolver ()->lookup_definition (n, &d); + rust_assert (ok); + + HIR::Stmt *decl = nullptr; + ok = ctx->get_mappings ()->resolve_nodeid_to_stmt (d.parent, &decl); + rust_assert (ok); + + Bvariable *compiled = CompileVarDecl::compile (fndecl, decl, ctx); + locals.push_back (compiled); + + return true; + }); + + Bblock *enclosing_scope = ctx->peek_enclosing_scope (); + Bblock *new_block + = ctx->get_backend ()->block (fndecl, enclosing_scope, locals, + start_location, end_location); + ctx->push_block (new_block); + + expr.iterate_stmts ([&] (HIR::Stmt *s) mutable -> bool { + CompileStmt::Compile (s, ctx); + return true; + }); + + ctx->pop_block (); + translated = new_block; +} + +void +CompileConditionalBlocks::visit (HIR::IfExpr &expr) +{ + fncontext fnctx = ctx->peek_fn (); + Bfunction *fndecl = fnctx.fndecl; + Bexpression *condition_expr + = CompileExpr::Compile (expr.get_if_condition (), ctx); + Bblock *then_block = CompileBlock::compile (expr.get_if_block (), ctx); + + translated + = ctx->get_backend ()->if_statement (fndecl, condition_expr, then_block, + NULL, expr.get_locus ()); +} + +void +CompileConditionalBlocks::visit (HIR::IfExprConseqElse &expr) +{ + fncontext fnctx = ctx->peek_fn (); + Bfunction *fndecl = fnctx.fndecl; + Bexpression *condition_expr + = CompileExpr::Compile (expr.get_if_condition (), ctx); + Bblock *then_block = CompileBlock::compile (expr.get_if_block (), ctx); + Bblock *else_block = CompileBlock::compile (expr.get_else_block (), ctx); + + translated + = ctx->get_backend ()->if_statement (fndecl, condition_expr, then_block, + else_block, expr.get_locus ()); +} + +void +CompileConditionalBlocks::visit (HIR::IfExprConseqIf &expr) +{ + fncontext fnctx = ctx->peek_fn (); + Bfunction *fndecl = fnctx.fndecl; + Bexpression *condition_expr + = CompileExpr::Compile (expr.get_if_condition (), ctx); + Bblock *then_block = CompileBlock::compile (expr.get_if_block (), ctx); + + // else block + std::vector<Bvariable *> locals; + Location start_location = expr.get_conseq_if_expr ()->get_locus (); + Location end_location = expr.get_conseq_if_expr ()->get_locus (); // FIXME + Bblock *enclosing_scope = ctx->peek_enclosing_scope (); + Bblock *else_block + = ctx->get_backend ()->block (fndecl, enclosing_scope, locals, + start_location, end_location); + ctx->push_block (else_block); + + Bstatement *else_stmt_decl + = CompileConditionalBlocks::compile (expr.get_conseq_if_expr (), ctx); + ctx->add_statement (else_stmt_decl); + + ctx->pop_block (); + + translated + = ctx->get_backend ()->if_statement (fndecl, condition_expr, then_block, + else_block, expr.get_locus ()); +} + } // namespace Compile } // namespace Rust |