aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/backend
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-04-22 12:04:06 +0000
committerGitHub <noreply@github.com>2021-04-22 12:04:06 +0000
commitbb85500ecc4e355d766836d06c98c50688a92ff1 (patch)
tree4ea21cfffc1a6879641cd28f665d02e00976e4a2 /gcc/rust/backend
parentdd194669e3a43bcfba7183540c77fb1ce56212c0 (diff)
parent393b783549e1247dfed5b86f098fb10d1eaf6659 (diff)
downloadgcc-bb85500ecc4e355d766836d06c98c50688a92ff1.zip
gcc-bb85500ecc4e355d766836d06c98c50688a92ff1.tar.gz
gcc-bb85500ecc4e355d766836d06c98c50688a92ff1.tar.bz2
Merge #383
383: Reuse fold-const.c from GCC to enforce const array capacities r=philberty a=philberty Rust allows for constant eval for cases like array capacity, the initial version of this code forced the programmer to only use literals which was not correct but got the type system off the ground. This now takes advantage of the GCC backend object to offer 3 helprs. 1. check for constant Bexpression* are equal 2. extract a size_t constant value from the Bexpression* 3. to string the Bexpression constant We can get away with the extraction of the value here because we know its going to be a usize for array capacity but some thought is needed if these methods are to be reused in other cases. There is a new ConstFold namespace which should be extensible for const functions later on and const generics. Fixes: #296 Co-authored-by: Philip Herron <philip.herron@embecosm.com>
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.h18
2 files changed, 22 insertions, 12 deletions
diff --git a/gcc/rust/backend/rust-compile-context.h b/gcc/rust/backend/rust-compile-context.h
index 8866575..226cec6 100644
--- a/gcc/rust/backend/rust-compile-context.h
+++ b/gcc/rust/backend/rust-compile-context.h
@@ -27,6 +27,7 @@
#include "rust-compile-tyty.h"
#include "rust-ast-full.h"
#include "rust-hir-full.h"
+#include "rust-hir-const-fold-ctx.h"
namespace Rust {
namespace Compile {
@@ -43,7 +44,8 @@ public:
Context (::Backend *backend)
: backend (backend), resolver (Resolver::Resolver::get ()),
tyctx (Resolver::TypeCheckContext::get ()),
- mappings (Analysis::Mappings::get ())
+ mappings (Analysis::Mappings::get ()),
+ const_ctx (ConstFold::Context::get ())
{
// insert the builtins
auto builtins = resolver->get_builtin_types ();
@@ -104,6 +106,7 @@ public:
Resolver::Resolver *get_resolver () { return resolver; }
Resolver::TypeCheckContext *get_tyctx () { return tyctx; }
Analysis::Mappings *get_mappings () { return mappings; }
+ ConstFold::Context *get_const_ctx () { return const_ctx; }
void push_block (Bblock *scope)
{
@@ -260,6 +263,7 @@ private:
Resolver::Resolver *resolver;
Resolver::TypeCheckContext *tyctx;
Analysis::Mappings *mappings;
+ ConstFold::Context *const_ctx;
// state
std::vector<fncontext> fn_stack;
@@ -420,16 +424,10 @@ public:
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_element_type ());
- translated = ctx->get_backend ()->array_type (element_type, length);
+ translated
+ = ctx->get_backend ()->array_type (element_type, type.get_capacity ());
}
void visit (TyTy::BoolType &type) override
diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h
index f98ebc6..59ae815 100644
--- a/gcc/rust/backend/rust-compile-expr.h
+++ b/gcc/rust/backend/rust-compile-expr.h
@@ -275,7 +275,12 @@ public:
return;
}
- Btype *array_type = TyTyResolveCompile::compile (ctx, tyty);
+ rust_assert (tyty->get_kind () == TyTy::TypeKind::ARRAY);
+ TyTy::ArrayType *array_tyty = static_cast<TyTy::ArrayType *> (tyty);
+ capacity_expr = array_tyty->get_capacity ();
+
+ Btype *array_type = TyTyResolveCompile::compile (ctx, array_tyty);
+ rust_assert (array_type != nullptr);
expr.get_internal_elements ()->accept_vis (*this);
std::vector<unsigned long> indexes;
@@ -302,7 +307,11 @@ public:
Bexpression *translated_expr
= CompileExpr::Compile (elems.get_elem_to_copy (), ctx);
- for (size_t i = 0; i < elems.get_num_elements (); ++i)
+ size_t capacity;
+ bool ok = ctx->get_backend ()->const_size_cast (capacity_expr, &capacity);
+ rust_assert (ok);
+
+ for (size_t i = 0; i < capacity; ++i)
constructor.push_back (translated_expr);
}
@@ -786,9 +795,12 @@ public:
}
private:
- CompileExpr (Context *ctx) : HIRCompileBase (ctx), translated (nullptr) {}
+ CompileExpr (Context *ctx)
+ : HIRCompileBase (ctx), translated (nullptr), capacity_expr (nullptr)
+ {}
Bexpression *translated;
+ Bexpression *capacity_expr;
std::vector<Bexpression *> constructor;
};