From 467141184aa274126ff7e2a41d08bb621b7a3fdf Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Thu, 24 Dec 2020 21:26:26 +0000 Subject: Implement constant expressions --- gcc/rust/backend/rust-compile-context.h | 16 ++++++++++++++++ gcc/rust/backend/rust-compile-expr.h | 5 +++++ gcc/rust/backend/rust-compile-item.h | 19 +++++++++++++++++++ 3 files changed, 40 insertions(+) (limited to 'gcc/rust/backend') diff --git a/gcc/rust/backend/rust-compile-context.h b/gcc/rust/backend/rust-compile-context.h index 5736bf2..288dbda 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 compiled_var_decls; std::map compiled_type_map; std::map compiled_fn_map; + std::map compiled_consts; std::vector< ::std::vector > statements; std::vector< ::Bblock *> scope_stack; diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h index 5c3206a..871b4ba 100644 --- a/gcc/rust/backend/rust-compile-expr.h +++ b/gcc/rust/backend/rust-compile-expr.h @@ -105,6 +105,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)) { diff --git a/gcc/rust/backend/rust-compile-item.h b/gcc/rust/backend/rust-compile-item.h index f131a89..10bdce0 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,24 @@ public: virtual ~CompileItem () {} + 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 -- cgit v1.1