From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gnu.wildebeest.org (wildebeest.demon.nl [212.238.236.112]) by sourceware.org (Postfix) with ESMTPS id 991B93858D34 for ; Sun, 15 Aug 2021 19:56:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 991B93858D34 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=klomp.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=klomp.org Received: from reform (unknown [172.31.128.28]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id 690BD302FBB1; Sun, 15 Aug 2021 21:56:09 +0200 (CEST) Received: by reform (Postfix, from userid 1000) id DB8E82E81E01; Sun, 15 Aug 2021 21:56:08 +0200 (CEST) From: Mark Wielaard To: gcc-rust@gcc.gnu.org Cc: Mark Wielaard Subject: [PATCH] Add support for const bool and const float Date: Sun, 15 Aug 2021 21:55:26 +0200 Message-Id: <20210815195526.59273-1-mark@klomp.org> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-rust@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: gcc-rust mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 15 Aug 2021 19:56:28 -0000 Handle BOOL and FLOAT in ConstFoldExpr::visit (HIR::LiteralExpr) to make it possible to create const bool, f32 and f64 constants. Add a new testcase "primconsts.rs". Not yet handled are const char and &str types. --- gcc/rust/typecheck/rust-hir-const-fold.h | 30 ++++++++ .../rust/compile/torture/primconsts.rs | 72 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 gcc/testsuite/rust/compile/torture/primconsts.rs diff --git a/gcc/rust/typecheck/rust-hir-const-fold.h b/gcc/rust/typecheck/rust-hir-const-fold.h index f6c66163fc1..8efbb183403 100644 --- a/gcc/rust/typecheck/rust-hir-const-fold.h +++ b/gcc/rust/typecheck/rust-hir-const-fold.h @@ -315,6 +315,36 @@ public: } return; + case HIR::Literal::BOOL: { + bool bval = literal_value->as_string ().compare ("true") == 0; + folded = ctx->get_backend ()->boolean_constant_expression (bval); + } + return; + + case HIR::Literal::FLOAT: { + mpfr_t fval; + if (mpfr_init_set_str (fval, literal_value->as_string ().c_str (), 10, + MPFR_RNDN) + != 0) + { + rust_fatal_error (expr.get_locus (), + "bad floating-point number in literal"); + return; + } + + TyTy::BaseType *tyty = nullptr; + if (!tyctx->lookup_type (expr.get_mappings ().get_hirid (), &tyty)) + { + rust_fatal_error (expr.get_locus (), + "did not resolve type for this literal expr"); + return; + } + + Btype *type = ConstFoldType::fold (tyty, ctx->get_backend ()); + folded = ctx->get_backend ()->float_constant_expression (type, fval); + } + return; + /* handle other literals */ default: diff --git a/gcc/testsuite/rust/compile/torture/primconsts.rs b/gcc/testsuite/rust/compile/torture/primconsts.rs new file mode 100644 index 00000000000..bcf9456d059 --- /dev/null +++ b/gcc/testsuite/rust/compile/torture/primconsts.rs @@ -0,0 +1,72 @@ +const TRUE: bool = true; +const FALSE: bool = !TRUE; + +const U8ZERO: u8 = 0; +const U8ONE: u8 = U8ZERO + 1; +const U16ZERO: u16 = 0; +const U16ONE: u16 = U16ZERO + 1; +const U32ZERO: u32 = 0; +const U32ONE: u32 = U32ZERO + 1; +const U64ZERO: u64 = 0; +const U64ONE: u64 = U64ZERO + 1; +const U128ZERO: u128 = 0; +const U128ONE: u128 = U128ZERO + 1; + +const I8ZERO: i8 = 0; +const I8ONE: i8 = I8ZERO + 1; +const I16ZERO: i16 = 0; +const I16ONE: i16 = I16ZERO + 1; +const I32ZERO: i32 = 0; +const I32ONE: i32 = I32ZERO + 1; +const I64ZERO: i64 = 0; +const I64ONE: i64 = I64ZERO + 1; +const I128ZERO: i128 = 0; +const I128ONE: i128 = I128ZERO + 1; + +const F32ZERO: f32 = 0.0; +const F32ONE: f32 = F32ZERO + 1.0; +const F64ZERO: f64 = 0.0; +const F64ONE: f64 = F64ZERO + 1.0; + +const USIZEZERO: usize = 0; +const USIZEONE: usize = USIZEZERO + 1; +const ISIZEZERO: isize = 0; +const ISIZEONE: isize = ISIZEZERO + 1; + +/* Not yet supported +const CHARPI: char = '\u{03C0}'; +const STRHELLO: &str = "Hello World!"; +*/ + +extern "C" { fn abort (); } + +pub fn main () +{ + if TRUE == FALSE { unsafe { abort (); } } + if U8ZERO > U8ONE { unsafe { abort (); } } + if U16ZERO > U16ONE { unsafe { abort (); } } + if U32ZERO > U32ONE { unsafe { abort (); } } + if U64ZERO > U64ONE { unsafe { abort (); } } + if U128ZERO > U128ONE { unsafe { abort (); } } + + if I8ONE <= I8ZERO { unsafe { abort (); } } + if I16ONE <= I16ZERO { unsafe { abort (); } } + if I32ONE <= I32ZERO { unsafe { abort (); } } + if I64ONE <= I64ZERO { unsafe { abort (); } } + if I128ONE <= I128ZERO { unsafe { abort (); } } + + if F32ZERO + F32ONE != F32ONE { unsafe { abort (); } } + if F64ZERO + F64ONE != F64ONE { unsafe { abort (); } } + + if USIZEZERO + USIZEONE - USIZEONE + USIZEZERO != USIZEZERO + { + unsafe { abort (); } + } + if ISIZEZERO + ISIZEONE - ISIZEONE + ISIZEZERO != ISIZEZERO + { + unsafe { abort (); } + } + + // if CHARPI != '\u{03c0}' { unsafe { abort (); } } + // if STRHELLO != "Hello World!" { unsafe { abort (); } } +} -- 2.32.0