From ab00acfb55eafa3df8edecbd5f54c7d678784ee7 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Thu, 26 Jul 2018 10:59:57 +0200 Subject: [PATCH] Add binop impls for TyBool --- mini_core.rs | 25 +++++++++++++++++++++++++ src/base.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/mini_core.rs b/mini_core.rs index 2496eb1bf4a..3829c0f588a 100644 --- a/mini_core.rs +++ b/mini_core.rs @@ -8,6 +8,7 @@ pub trait Sized {} #[lang="copy"] pub unsafe trait Copy {} +unsafe impl Copy for bool {} unsafe impl Copy for u8 {} unsafe impl Copy for u16 {} unsafe impl Copy for u32 {} @@ -40,6 +41,30 @@ impl Mul for u8 { } } +#[lang="bitor"] +pub trait BitOr { + type Output; + + #[must_use] + fn bitor(self, rhs: RHS) -> Self::Output; +} + +impl BitOr for bool { + type Output = bool; + + fn bitor(self, rhs: bool) -> bool { + self | rhs + } +} + +impl<'a> BitOr for &'a bool { + type Output = bool; + + fn bitor(self, rhs: bool) -> bool { + *self | rhs + } +} + #[lang = "eq"] pub trait PartialEq { fn eq(&self, other: &Rhs) -> bool; diff --git a/src/base.rs b/src/base.rs index d5c67480ef2..23dfab91f15 100644 --- a/src/base.rs +++ b/src/base.rs @@ -242,6 +242,9 @@ fn trans_stmt<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, cur_ebb: Ebb, stmt: & let rhs = trans_operand(fx, rhs).load_value(fx); let res = match ty.sty { + TypeVariants::TyBool => { + trans_bool_binop(fx, *bin_op, lhs, rhs, lval.layout().ty) + } TypeVariants::TyUint(_) => { trans_int_binop(fx, *bin_op, lhs, rhs, lval.layout().ty, false, false) } @@ -426,6 +429,33 @@ macro_rules! binop_match { } } +pub fn trans_bool_binop<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, bin_op: BinOp, lhs: Value, rhs: Value, ty: Ty<'tcx>) -> CValue<'tcx> { + let res = binop_match! { + fx, bin_op, false, lhs, rhs, "bool"; + Add (_) bug; + Sub (_) bug; + Mul (_) bug; + Div (_) bug; + Rem (_) bug; + BitXor (_) bxor; + BitAnd (_) band; + BitOr (_) bor; + Shl (_) bug; + Shr (_) bug; + + Eq (_) icmp(Equal); + Lt (_) icmp(UnsignedLessThan); + Le (_) icmp(UnsignedLessThanOrEqual); + Ne (_) icmp(NotEqual); + Ge (_) icmp(UnsignedGreaterThanOrEqual); + Gt (_) icmp(UnsignedGreaterThan); + + Offset (_) bug; + }; + + CValue::ByVal(res, fx.layout_of(ty)) +} + pub fn trans_int_binop<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, bin_op: BinOp, lhs: Value, rhs: Value, ty: Ty<'tcx>, signed: bool, _checked: bool) -> CValue<'tcx> { let res = binop_match! { fx, bin_op, signed, lhs, rhs, "int/uint";