From 290fc4f416c94a86aa5e3d22b785890a12686972 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Wed, 6 Jan 2021 14:53:35 +0000 Subject: Add test to cover handling hex, binary and octal number literals. This also ensure the type suffix is respected against the number. --- gcc/rust/backend/rust-compile-tyty.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'gcc/rust/backend') diff --git a/gcc/rust/backend/rust-compile-tyty.h b/gcc/rust/backend/rust-compile-tyty.h index 528f90e..2d4e1f0 100644 --- a/gcc/rust/backend/rust-compile-tyty.h +++ b/gcc/rust/backend/rust-compile-tyty.h @@ -132,19 +132,19 @@ public: switch (type.get_kind ()) { case TyTy::UintType::U8: - translated = backend->named_type ("i8", backend->integer_type (true, 8), + translated = backend->named_type ("u8", backend->integer_type (true, 8), Linemap::predeclared_location ()); return; case TyTy::UintType::U16: translated - = backend->named_type ("i16", backend->integer_type (true, 16), + = backend->named_type ("u16", backend->integer_type (true, 16), Linemap::predeclared_location ()); return; case TyTy::UintType::U32: translated - = backend->named_type ("i32", backend->integer_type (true, 32), + = backend->named_type ("u32", backend->integer_type (true, 32), Linemap::predeclared_location ()); return; -- cgit v1.1 From 4e0189ed288223f8b376eedd286f5bdff5b35698 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Wed, 6 Jan 2021 18:18:42 +0000 Subject: Implicit Returns support. For implict returns we must consider cases with a block having multiple returns: HIR::BlockExpr Stmts { ... return x } HIR::BlockExpr final_expression { x + 1 } Although the code above is bad this is valid rust code and the rust compiler correctly identifies the final_expression as unreachable. This dead code eliminiation is done as part of AST to HIR lowering. Type resolution examines all blocks to identifiy if they terminate a function with a return/final expression it must correspond accordngly. If the block is the final block the resulting termination of the block must match the return type of the function, else the block must conform to unit type. --- gcc/rust/backend/rust-compile-item.h | 17 +++++++++++++++++ gcc/rust/backend/rust-compile.cc | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) (limited to 'gcc/rust/backend') diff --git a/gcc/rust/backend/rust-compile-item.h b/gcc/rust/backend/rust-compile-item.h index 90630bb..f1b39da 100644 --- a/gcc/rust/backend/rust-compile-item.h +++ b/gcc/rust/backend/rust-compile-item.h @@ -232,6 +232,23 @@ public: return true; }); + if (function_body->has_expr ()) + { + // the previous passes will ensure this is a valid return + // dead code elimination should remove any bad trailing expressions + Bexpression *compiled_expr + = CompileExpr::Compile (function_body->expr.get (), ctx); + rust_assert (compiled_expr != nullptr); + + auto fncontext = ctx->peek_fn (); + + std::vector retstmts; + retstmts.push_back (compiled_expr); + auto s = ctx->get_backend ()->return_statement ( + fncontext.fndecl, retstmts, function_body->expr->get_locus_slow ()); + ctx->add_statement (s); + } + ctx->pop_block (); auto body = ctx->get_backend ()->block_statement (code_block); if (!ctx->get_backend ()->function_set_body (fndecl, body)) diff --git a/gcc/rust/backend/rust-compile.cc b/gcc/rust/backend/rust-compile.cc index 24b45ee..a52f183 100644 --- a/gcc/rust/backend/rust-compile.cc +++ b/gcc/rust/backend/rust-compile.cc @@ -90,6 +90,23 @@ CompileBlock::visit (HIR::BlockExpr &expr) return true; }); + if (expr.has_expr ()) + { + // the previous passes will ensure this is a valid return + // dead code elimination should remove any bad trailing expressions + Bexpression *compiled_expr = CompileExpr::Compile (expr.expr.get (), ctx); + rust_assert (compiled_expr != nullptr); + + auto fncontext = ctx->peek_fn (); + + std::vector retstmts; + retstmts.push_back (compiled_expr); + auto s + = ctx->get_backend ()->return_statement (fncontext.fndecl, retstmts, + expr.expr->get_locus_slow ()); + ctx->add_statement (s); + } + ctx->pop_block (); translated = new_block; } -- cgit v1.1 From 06d946d52234e2776be002662439d43e3d557673 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Wed, 6 Jan 2021 12:54:57 +0000 Subject: Respect the f32 and f64 suffix on literals Rust is permissive for integers being marked as floats so the check in the lexer can be removed here. --- gcc/rust/backend/rust-compile-expr.h | 4 ---- 1 file changed, 4 deletions(-) (limited to 'gcc/rust/backend') diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h index 9081000..0370129 100644 --- a/gcc/rust/backend/rust-compile-expr.h +++ b/gcc/rust/backend/rust-compile-expr.h @@ -156,8 +156,6 @@ 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) @@ -177,8 +175,6 @@ public: 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); -- cgit v1.1 From 5a11dd79fcbcfc8b3fcd41f809c7fa373d637e41 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Fri, 8 Jan 2021 15:12:19 +0000 Subject: Make TyTyVisitor a pure abstract class This will help enforce consistency of visitors to fix issues with TyTy unification rules. --- gcc/rust/backend/rust-compile-context.h | 6 ++++ gcc/rust/backend/rust-compile-tyty.h | 56 ++++++++++++++++++++++++++++----- 2 files changed, 54 insertions(+), 8 deletions(-) (limited to 'gcc/rust/backend') diff --git a/gcc/rust/backend/rust-compile-context.h b/gcc/rust/backend/rust-compile-context.h index d241921..298ff50 100644 --- a/gcc/rust/backend/rust-compile-context.h +++ b/gcc/rust/backend/rust-compile-context.h @@ -221,12 +221,18 @@ public: virtual ~TyTyResolveCompile () {} + void visit (TyTy::ErrorType &type) override { gcc_unreachable (); } + void visit (TyTy::UnitType &type) override { gcc_unreachable (); } void visit (TyTy::InferType &type) override { gcc_unreachable (); } void visit (TyTy::FnType &type) override { gcc_unreachable (); } + void visit (TyTy::StructFieldType &type) override { gcc_unreachable (); } + + void visit (TyTy::ParamType &type) override { gcc_unreachable (); } + void visit (TyTy::ADTType &type) override { ::Btype *compiled_type = nullptr; diff --git a/gcc/rust/backend/rust-compile-tyty.h b/gcc/rust/backend/rust-compile-tyty.h index 2d4e1f0..137b74b 100644 --- a/gcc/rust/backend/rust-compile-tyty.h +++ b/gcc/rust/backend/rust-compile-tyty.h @@ -43,13 +43,19 @@ public: ~TyTyCompile () {} - void visit (TyTy::InferType &type) override - { - // there shouldn't be any of these left - gcc_unreachable (); - } + void visit (TyTy::ErrorType &type) override { gcc_unreachable (); } + + void visit (TyTy::UnitType &type) override { gcc_unreachable (); } + + void visit (TyTy::InferType &type) override { gcc_unreachable (); } + + void visit (TyTy::StructFieldType &type) override { gcc_unreachable (); } - void visit (TyTy::UnitType &type) override {} + void visit (TyTy::ParamType &type) override { gcc_unreachable (); } + + void visit (TyTy::ADTType &type) override { gcc_unreachable (); } + + void visit (TyTy::ArrayType &type) override { gcc_unreachable (); } void visit (TyTy::FnType &type) override { @@ -82,8 +88,6 @@ public: mappings->lookup_location (type.get_ref ())); } - void visit (TyTy::ParamType &type) override {} - void visit (TyTy::BoolType &type) override { translated = backend->named_type ("bool", backend->bool_type (), @@ -205,6 +209,18 @@ public: ~TyTyExtractParamsFromFnType () {} + void visit (TyTy::UnitType &type) override { gcc_unreachable (); } + void visit (TyTy::InferType &type) override { gcc_unreachable (); } + void visit (TyTy::StructFieldType &type) override { gcc_unreachable (); } + void visit (TyTy::ADTType &type) override { gcc_unreachable (); } + void visit (TyTy::ParamType &type) override { gcc_unreachable (); } + void visit (TyTy::ArrayType &type) override { gcc_unreachable (); } + void visit (TyTy::BoolType &type) override { gcc_unreachable (); } + void visit (TyTy::IntType &type) override { gcc_unreachable (); } + void visit (TyTy::UintType &type) override { gcc_unreachable (); } + void visit (TyTy::FloatType &type) override { gcc_unreachable (); } + void visit (TyTy::ErrorType &type) override { gcc_unreachable (); } + void visit (TyTy::FnType &type) override { ok = true; @@ -234,6 +250,18 @@ public: ~TyTyExtractRetFromFnType () {} + void visit (TyTy::UnitType &type) override { gcc_unreachable (); } + void visit (TyTy::InferType &type) override { gcc_unreachable (); } + void visit (TyTy::StructFieldType &type) override { gcc_unreachable (); } + void visit (TyTy::ADTType &type) override { gcc_unreachable (); } + void visit (TyTy::ParamType &type) override { gcc_unreachable (); } + void visit (TyTy::ArrayType &type) override { gcc_unreachable (); } + void visit (TyTy::BoolType &type) override { gcc_unreachable (); } + void visit (TyTy::IntType &type) override { gcc_unreachable (); } + void visit (TyTy::UintType &type) override { gcc_unreachable (); } + void visit (TyTy::FloatType &type) override { gcc_unreachable (); } + void visit (TyTy::ErrorType &type) override { gcc_unreachable (); } + void visit (TyTy::FnType &type) override { ok = true; @@ -261,6 +289,18 @@ public: ~TyTyCompileParam () {} + void visit (TyTy::UnitType &type) override { gcc_unreachable (); } + void visit (TyTy::InferType &type) override { gcc_unreachable (); } + void visit (TyTy::StructFieldType &type) override { gcc_unreachable (); } + void visit (TyTy::ADTType &type) override { gcc_unreachable (); } + void visit (TyTy::FnType &type) override { gcc_unreachable (); } + void visit (TyTy::ArrayType &type) override { gcc_unreachable (); } + void visit (TyTy::BoolType &type) override { gcc_unreachable (); } + void visit (TyTy::IntType &type) override { gcc_unreachable (); } + void visit (TyTy::UintType &type) override { gcc_unreachable (); } + void visit (TyTy::FloatType &type) override { gcc_unreachable (); } + void visit (TyTy::ErrorType &type) override { gcc_unreachable (); } + void visit (TyTy::ParamType &type) override { auto btype = TyTyCompile::compile (backend, type.get_base_type ()); -- cgit v1.1