aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/backend
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2020-12-24 21:26:26 +0000
committerPhilip Herron <herron.philip@googlemail.com>2020-12-25 18:23:22 +0000
commit467141184aa274126ff7e2a41d08bb621b7a3fdf (patch)
treef15fd81fb434787967d3837651891bfaa73fee80 /gcc/rust/backend
parent8d34ac3c1602d8506ae4b65d92075be86f5a6c9a (diff)
downloadgcc-467141184aa274126ff7e2a41d08bb621b7a3fdf.zip
gcc-467141184aa274126ff7e2a41d08bb621b7a3fdf.tar.gz
gcc-467141184aa274126ff7e2a41d08bb621b7a3fdf.tar.bz2
Implement constant expressions
Diffstat (limited to 'gcc/rust/backend')
-rw-r--r--gcc/rust/backend/rust-compile-context.h16
-rw-r--r--gcc/rust/backend/rust-compile-expr.h5
-rw-r--r--gcc/rust/backend/rust-compile-item.h19
3 files changed, 40 insertions, 0 deletions
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<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;
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