aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/backend
diff options
context:
space:
mode:
authorSimplyTheOther <simplytheother@gmail.com>2021-01-08 15:09:08 +0800
committerSimplyTheOther <simplytheother@gmail.com>2021-01-08 15:09:08 +0800
commit238a31b33f79cddfaa40c5fd748495a5f2b34630 (patch)
treebc217d37b5e3ee41b0a69265341945389d1f4dfb /gcc/rust/backend
parentb5f86dca7e6b53ca3701ef01ae36070a760dff78 (diff)
parentaf04ea2222b6407fb3e83759ae332d600495380c (diff)
downloadgcc-238a31b33f79cddfaa40c5fd748495a5f2b34630.zip
gcc-238a31b33f79cddfaa40c5fd748495a5f2b34630.tar.gz
gcc-238a31b33f79cddfaa40c5fd748495a5f2b34630.tar.bz2
Merge branch 'master' of https://github.com/redbrain/gccrs
Diffstat (limited to 'gcc/rust/backend')
-rw-r--r--gcc/rust/backend/rust-compile-context.h32
-rw-r--r--gcc/rust/backend/rust-compile-expr.h57
-rw-r--r--gcc/rust/backend/rust-compile-item.h76
-rw-r--r--gcc/rust/backend/rust-compile-resolve-path.cc30
-rw-r--r--gcc/rust/backend/rust-compile-resolve-path.h27
-rw-r--r--gcc/rust/backend/rust-compile-struct-field-expr.h52
-rw-r--r--gcc/rust/backend/rust-compile-tyty.h42
-rw-r--r--gcc/rust/backend/rust-compile.cc9
8 files changed, 318 insertions, 7 deletions
diff --git a/gcc/rust/backend/rust-compile-context.h b/gcc/rust/backend/rust-compile-context.h
index 5736bf2..d241921 100644
--- a/gcc/rust/backend/rust-compile-context.h
+++ b/gcc/rust/backend/rust-compile-context.h
@@ -141,6 +141,21 @@ public:
return true;
}
+ void insert_const_decl (HirId id, ::Bexpression *expr)
+ {
+ compiled_consts[id] = expr;
+ }
+
+ bool lookup_const_decl (HirId id, ::Bexpression **expr)
+ {
+ auto it = compiled_consts.find (id);
+ if (it == compiled_consts.end ())
+ return false;
+
+ *expr = it->second;
+ return true;
+ }
+
void push_fn (::Bfunction *fn, ::Bvariable *ret_addr)
{
fn_stack.push_back (fncontext{fn, ret_addr});
@@ -183,6 +198,7 @@ private:
std::map<HirId, ::Bvariable *> compiled_var_decls;
std::map<HirId, ::Btype *> compiled_type_map;
std::map<HirId, ::Bfunction *> compiled_fn_map;
+ std::map<HirId, ::Bexpression *> compiled_consts;
std::vector< ::std::vector<Bstatement *> > statements;
std::vector< ::Bblock *> scope_stack;
@@ -211,6 +227,14 @@ public:
void visit (TyTy::FnType &type) override { gcc_unreachable (); }
+ void visit (TyTy::ADTType &type) override
+ {
+ ::Btype *compiled_type = nullptr;
+ bool ok = ctx->lookup_compiled_types (type.get_ref (), &compiled_type);
+ rust_assert (ok);
+ translated = compiled_type;
+ }
+
void visit (TyTy::ArrayType &type) override
{
mpz_t ival;
@@ -248,6 +272,14 @@ public:
translated = compiled_type;
}
+ void visit (TyTy::FloatType &type) override
+ {
+ ::Btype *compiled_type = nullptr;
+ bool ok = ctx->lookup_compiled_types (type.get_ref (), &compiled_type);
+ rust_assert (ok);
+ translated = compiled_type;
+ }
+
private:
TyTyResolveCompile (Context *ctx) : ctx (ctx) {}
diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h
index 5c3206a..9081000 100644
--- a/gcc/rust/backend/rust-compile-expr.h
+++ b/gcc/rust/backend/rust-compile-expr.h
@@ -23,6 +23,7 @@
#include "rust-compile-tyty.h"
#include "rust-compile-resolve-path.h"
#include "rust-compile-block.h"
+#include "rust-compile-struct-field-expr.h"
namespace Rust {
namespace Compile {
@@ -56,7 +57,7 @@ public:
void visit (HIR::CallExpr &expr)
{
- Bexpression *fn = ResolvePath::Compile (expr.get_fnexpr (), ctx);
+ Bexpression *fn = ResolvePathRef::Compile (expr.get_fnexpr (), ctx);
rust_assert (fn != nullptr);
std::vector<Bexpression *> args;
@@ -105,6 +106,11 @@ public:
return;
}
+ // this could be a constant reference
+ if (ctx->lookup_const_decl (ref, &translated))
+ return;
+
+ // must be an identifier
Bvariable *var = nullptr;
if (!ctx->lookup_var_decl (ref, &var))
{
@@ -149,6 +155,36 @@ public:
}
return;
+ case HIR::Literal::FLOAT: {
+ printf ("FLOATY BOYO: [%s]\n", expr.as_string ().c_str ());
+
+ mpfr_t fval;
+ if (mpfr_init_set_str (fval, expr.as_string ().c_str (), 10,
+ MPFR_RNDN)
+ != 0)
+ {
+ rust_fatal_error (expr.get_locus (),
+ "bad float number in literal");
+ return;
+ }
+
+ 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 literal expr");
+ return;
+ }
+
+ printf ("tyty float is [%s]\n", tyty->as_string ().c_str ());
+
+ Btype *type = TyTyResolveCompile::compile (ctx, tyty);
+ translated
+ = ctx->get_backend ()->float_constant_expression (type, fval);
+ }
+ return;
+
default:
rust_fatal_error (expr.get_locus (), "unknown literal");
return;
@@ -350,6 +386,25 @@ public:
ctx->add_statement (block_stmt);
}
+ void visit (HIR::StructExprStructFields &struct_expr)
+ {
+ Btype *type
+ = ResolvePathType::Compile (&struct_expr.get_struct_name (), ctx);
+
+ // this assumes all fields are in order from type resolution and if a base
+ // struct was specified those fields are filed via accesors
+ std::vector<Bexpression *> vals;
+ struct_expr.iterate ([&] (HIR::StructExprField *field) mutable -> bool {
+ Bexpression *expr = CompileStructExprField::Compile (field, ctx);
+ vals.push_back (expr);
+ return true;
+ });
+
+ translated
+ = ctx->get_backend ()->constructor_expression (type, vals,
+ struct_expr.get_locus ());
+ }
+
private:
CompileExpr (Context *ctx) : HIRCompileBase (ctx), translated (nullptr) {}
diff --git a/gcc/rust/backend/rust-compile-item.h b/gcc/rust/backend/rust-compile-item.h
index f131a89..90630bb 100644
--- a/gcc/rust/backend/rust-compile-item.h
+++ b/gcc/rust/backend/rust-compile-item.h
@@ -23,6 +23,7 @@
#include "rust-compile-tyty.h"
#include "rust-compile-var-decl.h"
#include "rust-compile-stmt.h"
+#include "rust-compile-expr.h"
namespace Rust {
namespace Compile {
@@ -38,6 +39,81 @@ public:
virtual ~CompileItem () {}
+ void visit (HIR::StructStruct &struct_decl)
+ {
+ std::vector<Backend::Btyped_identifier> fields;
+ struct_decl.iterate ([&] (HIR::StructField &field) mutable -> bool {
+ TyTy::TyBase *resolved_type = nullptr;
+ bool ok
+ = ctx->get_tyctx ()->lookup_type (field.get_mappings ().get_hirid (),
+ &resolved_type);
+ rust_assert (ok);
+
+ Btype *compiled_field_ty
+ = TyTyCompile::compile (ctx->get_backend (), resolved_type);
+
+ Backend::Btyped_identifier f (field.field_name, compiled_field_ty,
+ field.get_locus ());
+ fields.push_back (std::move (f));
+ return true;
+ });
+
+ Btype *struct_type_record = ctx->get_backend ()->struct_type (fields);
+ Btype *named_struct
+ = ctx->get_backend ()->named_type (struct_decl.get_identifier (),
+ struct_type_record,
+ struct_decl.get_locus ());
+ ctx->push_type (named_struct);
+ ctx->insert_compiled_type (struct_decl.get_mappings ().get_hirid (),
+ named_struct);
+ }
+
+ void visit (HIR::StaticItem &var)
+ {
+ TyTy::TyBase *resolved_type = nullptr;
+ bool ok = ctx->get_tyctx ()->lookup_type (var.get_mappings ().get_hirid (),
+ &resolved_type);
+ rust_assert (ok);
+
+ Btype *type = TyTyResolveCompile::compile (ctx, resolved_type);
+ Bexpression *value = CompileExpr::Compile (var.get_expr (), ctx);
+
+ std::string name = var.get_identifier ();
+ // FIXME need name mangling
+ std::string asm_name = "__" + var.get_identifier ();
+
+ bool is_external = false;
+ bool is_hidden = false;
+ bool in_unique_section = true;
+
+ Bvariable *static_global
+ = ctx->get_backend ()->global_variable (name, asm_name, type, is_external,
+ is_hidden, in_unique_section,
+ var.get_locus ());
+ ctx->get_backend ()->global_variable_set_init (static_global, value);
+
+ ctx->insert_var_decl (var.get_mappings ().get_hirid (), static_global);
+ ctx->push_var (static_global);
+ }
+
+ void visit (HIR::ConstantItem &constant)
+ {
+ TyTy::TyBase *resolved_type = nullptr;
+ bool ok
+ = ctx->get_tyctx ()->lookup_type (constant.get_mappings ().get_hirid (),
+ &resolved_type);
+ rust_assert (ok);
+
+ ::Btype *type = TyTyResolveCompile::compile (ctx, resolved_type);
+ Bexpression *value = CompileExpr::Compile (constant.get_expr (), ctx);
+
+ Bexpression *const_expr = ctx->get_backend ()->named_constant_expression (
+ type, constant.get_identifier (), value, constant.get_locus ());
+
+ ctx->push_const (const_expr);
+ ctx->insert_const_decl (constant.get_mappings ().get_hirid (), const_expr);
+ }
+
void visit (HIR::Function &function)
{
// items can be forward compiled which means we may not need to invoke this
diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc b/gcc/rust/backend/rust-compile-resolve-path.cc
index c5c646d..c24005e 100644
--- a/gcc/rust/backend/rust-compile-resolve-path.cc
+++ b/gcc/rust/backend/rust-compile-resolve-path.cc
@@ -25,7 +25,7 @@ namespace Rust {
namespace Compile {
void
-ResolvePath::visit (HIR::PathInExpression &expr)
+ResolvePathRef::visit (HIR::PathInExpression &expr)
{
// need to look up the reference for this identifier
NodeId ref_node_id;
@@ -70,5 +70,33 @@ ResolvePath::visit (HIR::PathInExpression &expr)
= ctx->get_backend ()->function_code_expression (fn, expr.get_locus ());
}
+void
+ResolvePathType::visit (HIR::PathInExpression &expr)
+{
+ // need to look up the reference for this identifier
+ NodeId ref_node_id;
+ if (!ctx->get_resolver ()->lookup_resolved_name (
+ expr.get_mappings ().get_nodeid (), &ref_node_id))
+ {
+ rust_fatal_error (expr.get_locus (), "failed to look up resolved name");
+ return;
+ }
+
+ HirId ref;
+ if (!ctx->get_mappings ()->lookup_node_to_hir (
+ expr.get_mappings ().get_crate_num (), ref_node_id, &ref))
+ {
+ rust_fatal_error (expr.get_locus (), "reverse lookup failure");
+ return;
+ }
+
+ // assumes paths are functions for now
+ if (!ctx->lookup_compiled_types (ref, &resolved))
+ {
+ rust_fatal_error (expr.get_locus (), "forward decl was not compiled");
+ return;
+ }
+}
+
} // namespace Compile
} // namespace Rust
diff --git a/gcc/rust/backend/rust-compile-resolve-path.h b/gcc/rust/backend/rust-compile-resolve-path.h
index d8f393d..a5543d2 100644
--- a/gcc/rust/backend/rust-compile-resolve-path.h
+++ b/gcc/rust/backend/rust-compile-resolve-path.h
@@ -25,27 +25,44 @@
namespace Rust {
namespace Compile {
-class ResolvePath : public HIRCompileBase
+class ResolvePathRef : public HIRCompileBase
{
public:
static Bexpression *Compile (HIR::Expr *expr, Context *ctx)
{
- ResolvePath resolver (ctx);
+ ResolvePathRef resolver (ctx);
expr->accept_vis (resolver);
rust_assert (resolver.resolved != nullptr);
return resolver.resolved;
}
- virtual ~ResolvePath () {}
-
void visit (HIR::PathInExpression &expr);
private:
- ResolvePath (Context *ctx) : HIRCompileBase (ctx), resolved (nullptr) {}
+ ResolvePathRef (Context *ctx) : HIRCompileBase (ctx), resolved (nullptr) {}
Bexpression *resolved;
};
+class ResolvePathType : public HIRCompileBase
+{
+public:
+ static Btype *Compile (HIR::Expr *expr, Context *ctx)
+ {
+ ResolvePathType resolver (ctx);
+ expr->accept_vis (resolver);
+ rust_assert (resolver.resolved != nullptr);
+ return resolver.resolved;
+ }
+
+ void visit (HIR::PathInExpression &expr);
+
+private:
+ ResolvePathType (Context *ctx) : HIRCompileBase (ctx), resolved (nullptr) {}
+
+ Btype *resolved;
+};
+
} // namespace Compile
} // namespace Rust
diff --git a/gcc/rust/backend/rust-compile-struct-field-expr.h b/gcc/rust/backend/rust-compile-struct-field-expr.h
new file mode 100644
index 0000000..a394f7a
--- /dev/null
+++ b/gcc/rust/backend/rust-compile-struct-field-expr.h
@@ -0,0 +1,52 @@
+// 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_STRUCT_FIELD_EXPR
+#define RUST_COMPILE_STRUCT_FIELD_EXPR
+
+#include "rust-compile-base.h"
+#include "rust-compile-tyty.h"
+
+namespace Rust {
+namespace Compile {
+
+class CompileStructExprField : public HIRCompileBase
+{
+public:
+ static Bexpression *Compile (HIR::StructExprField *field, Context *ctx)
+ {
+ CompileStructExprField compiler (ctx);
+ field->accept_vis (compiler);
+ rust_assert (compiler.translated != nullptr);
+ return compiler.translated;
+ }
+
+ void visit (HIR::StructExprFieldIdentifierValue &field);
+
+private:
+ CompileStructExprField (Context *ctx)
+ : HIRCompileBase (ctx), translated (nullptr)
+ {}
+
+ Bexpression *translated;
+};
+
+} // namespace Compile
+} // namespace Rust
+
+#endif // RUST_COMPILE_STRUCT_FIELD_EXPR
diff --git a/gcc/rust/backend/rust-compile-tyty.h b/gcc/rust/backend/rust-compile-tyty.h
index 66d2472..528f90e 100644
--- a/gcc/rust/backend/rust-compile-tyty.h
+++ b/gcc/rust/backend/rust-compile-tyty.h
@@ -111,6 +111,18 @@ public:
= backend->named_type ("i32", backend->integer_type (false, 32),
Linemap::predeclared_location ());
return;
+
+ case TyTy::IntType::I64:
+ translated
+ = backend->named_type ("i64", backend->integer_type (false, 64),
+ Linemap::predeclared_location ());
+ return;
+
+ case TyTy::IntType::I128:
+ translated
+ = backend->named_type ("i128", backend->integer_type (false, 128),
+ Linemap::predeclared_location ());
+ return;
}
gcc_unreachable ();
}
@@ -135,7 +147,37 @@ public:
= backend->named_type ("i32", backend->integer_type (true, 32),
Linemap::predeclared_location ());
return;
+
+ case TyTy::UintType::U64:
+ translated
+ = backend->named_type ("u64", backend->integer_type (true, 64),
+ Linemap::predeclared_location ());
+ return;
+
+ case TyTy::UintType::U128:
+ translated
+ = backend->named_type ("u128", backend->integer_type (true, 128),
+ Linemap::predeclared_location ());
+ return;
+ }
+ gcc_unreachable ();
+ }
+
+ void visit (TyTy::FloatType &type) override
+ {
+ switch (type.get_kind ())
+ {
+ case TyTy::FloatType::F32:
+ translated = backend->named_type ("f32", backend->float_type (32),
+ Linemap::predeclared_location ());
+ return;
+
+ case TyTy::FloatType::F64:
+ translated = backend->named_type ("f64", backend->float_type (64),
+ Linemap::predeclared_location ());
+ return;
}
+
gcc_unreachable ();
}
diff --git a/gcc/rust/backend/rust-compile.cc b/gcc/rust/backend/rust-compile.cc
index 02fa3a0..24b45ee 100644
--- a/gcc/rust/backend/rust-compile.cc
+++ b/gcc/rust/backend/rust-compile.cc
@@ -19,6 +19,7 @@
#include "rust-compile.h"
#include "rust-compile-item.h"
#include "rust-compile-expr.h"
+#include "rust-compile-struct-field-expr.h"
namespace Rust {
namespace Compile {
@@ -152,5 +153,13 @@ CompileConditionalBlocks::visit (HIR::IfExprConseqIf &expr)
else_block, expr.get_locus ());
}
+// rust-compile-struct-field-expr.h
+
+void
+CompileStructExprField::visit (HIR::StructExprFieldIdentifierValue &field)
+{
+ translated = CompileExpr::Compile (field.get_value (), ctx);
+}
+
} // namespace Compile
} // namespace Rust