diff options
author | SimplyTheOther <simplytheother@gmail.com> | 2020-12-25 18:00:23 +0800 |
---|---|---|
committer | SimplyTheOther <simplytheother@gmail.com> | 2020-12-25 18:00:23 +0800 |
commit | faf78e75e4ec3c989e452d47dc37a0be1706bf08 (patch) | |
tree | 8cd4b2b11b9d7e259466fff9638c0057fcc52340 /gcc/rust/backend | |
parent | 859720937474816d4d386664d56d80a9e840f06f (diff) | |
parent | 8d34ac3c1602d8506ae4b65d92075be86f5a6c9a (diff) | |
download | gcc-faf78e75e4ec3c989e452d47dc37a0be1706bf08.zip gcc-faf78e75e4ec3c989e452d47dc37a0be1706bf08.tar.gz gcc-faf78e75e4ec3c989e452d47dc37a0be1706bf08.tar.bz2 |
Merge branch 'master' of https://github.com/redbrain/gccrs
Diffstat (limited to 'gcc/rust/backend')
-rw-r--r-- | gcc/rust/backend/cscope.h | 18 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-block.h | 112 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-context.h | 16 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-expr.h | 78 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-stmt.h | 12 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile.cc | 109 |
6 files changed, 342 insertions, 3 deletions
diff --git a/gcc/rust/backend/cscope.h b/gcc/rust/backend/cscope.h index ee4f955..3ba837c 100644 --- a/gcc/rust/backend/cscope.h +++ b/gcc/rust/backend/cscope.h @@ -1,3 +1,21 @@ +// 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/>. + #pragma once #include "rust-system.h" 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-context.h b/gcc/rust/backend/rust-compile-context.h index 1924b52..5736bf2 100644 --- a/gcc/rust/backend/rust-compile-context.h +++ b/gcc/rust/backend/rust-compile-context.h @@ -211,6 +211,19 @@ public: void visit (TyTy::FnType &type) override { gcc_unreachable (); } + void visit (TyTy::ArrayType &type) override + { + mpz_t ival; + mpz_init_set_ui (ival, type.get_capacity ()); + + Btype *capacity_type = ctx->get_backend ()->integer_type (true, 32); + Bexpression *length + = ctx->get_backend ()->integer_constant_expression (capacity_type, ival); + + Btype *element_type = TyTyResolveCompile::compile (ctx, type.get_type ()); + translated = ctx->get_backend ()->array_type (element_type, length); + } + void visit (TyTy::BoolType &type) override { ::Btype *compiled_type = nullptr; @@ -221,9 +234,6 @@ public: void visit (TyTy::IntType &type) override { - printf ("type [%s] has ref: %u\n", type.as_string ().c_str (), - type.get_ref ()); - ::Btype *compiled_type = nullptr; bool ok = ctx->lookup_compiled_types (type.get_ref (), &compiled_type); rust_assert (ok); diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h index 7808af2..5c3206a 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 { @@ -168,6 +169,57 @@ public: ctx->add_statement (assignment); } + void visit (HIR::ArrayIndexExpr &expr) + { + Bexpression *array = CompileExpr::Compile (expr.get_array_expr (), ctx); + Bexpression *index = CompileExpr::Compile (expr.get_index_expr (), ctx); + translated + = ctx->get_backend ()->array_index_expression (array, index, + expr.get_locus ()); + } + + void visit (HIR::ArrayExpr &expr) + { + TyTy::TyBase *tyty = nullptr; + if (!ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (), + &tyty)) + { + rust_fatal_error (expr.get_locus (), + "did not resolve type for this array expr"); + return; + } + + Btype *array_type = TyTyResolveCompile::compile (ctx, tyty); + + expr.get_internal_elements ()->accept_vis (*this); + std::vector<unsigned long> indexes; + for (size_t i = 0; i < constructor.size (); i++) + indexes.push_back (i); + + translated + = ctx->get_backend ()->array_constructor_expression (array_type, indexes, + constructor, + expr.get_locus ()); + } + + void visit (HIR::ArrayElemsValues &elems) + { + elems.iterate ([&] (HIR::Expr *e) mutable -> bool { + Bexpression *translated_expr = CompileExpr::Compile (e, ctx); + constructor.push_back (translated_expr); + return true; + }); + } + + void visit (HIR::ArrayElemsCopied &elems) + { + Bexpression *translated_expr + = CompileExpr::Compile (elems.get_elem_to_copy (), ctx); + + for (size_t i = 0; i < elems.get_num_elements (); ++i) + constructor.push_back (translated_expr); + } + void visit (HIR::ArithmeticOrLogicalExpr &expr) { Operator op; @@ -273,10 +325,36 @@ 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) {} Bexpression *translated; + std::vector<Bexpression *> constructor; }; } // namespace Compile 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 |