From dc4f2341150e4c8356a8e81ad9e0a6908d10c5df Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 29 Jul 2018 17:09:17 +0200 Subject: [PATCH] Implement float binops --- Readme.md | 1 - src/base.rs | 38 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/Readme.md b/Readme.md index 320ea0fa67f..7c1efe263f6 100644 --- a/Readme.md +++ b/Readme.md @@ -41,7 +41,6 @@ $ ./build.sh * Unsized types * Slice indexing * Sub slice -* Closures * Some rvalue's * Inline assembly diff --git a/src/base.rs b/src/base.rs index e58a8af8285..6c060be973b 100644 --- a/src/base.rs +++ b/src/base.rs @@ -252,6 +252,9 @@ fn trans_stmt<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, cur_ebb: Ebb, stmt: & TypeVariants::TyInt(_) => { trans_int_binop(fx, *bin_op, lhs, rhs, lval.layout().ty, true, false) } + TypeVariants::TyFloat(_) => { + trans_float_binop(fx, *bin_op, lhs, rhs, lval.layout().ty) + } TypeVariants::TyChar => { trans_char_binop(fx, *bin_op, lhs, rhs, lval.layout().ty) } @@ -413,6 +416,10 @@ macro_rules! binop_match { let b = $fx.bcx.ins().icmp(IntCC::$cc, $lhs, $rhs); $fx.bcx.ins().bint(types::I8, b) }}; + (@single $fx:expr, $bug_fmt:expr, $var:expr, $lhs:expr, $rhs:expr, fcmp($cc:ident)) => {{ + let b = $fx.bcx.ins().fcmp(FloatCC::$cc, $lhs, $rhs); + $fx.bcx.ins().bint(types::I8, b) + }}; (@single $fx:expr, $bug_fmt:expr, $var:expr, $lhs:expr, $rhs:expr, $name:ident) => { $fx.bcx.ins().$name($lhs, $rhs) }; @@ -430,7 +437,7 @@ 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> { +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; @@ -492,7 +499,34 @@ pub fn trans_int_binop<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, bin_op: BinO CValue::ByVal(res, fx.layout_of(ty)) } -pub fn trans_char_binop<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, bin_op: BinOp, lhs: Value, rhs: Value, ty: Ty<'tcx>) -> CValue<'tcx> { +fn trans_float_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 (_) fadd; + Sub (_) fsub; + Mul (_) fmul; + Div (_) fdiv; + Rem (_) bug; + BitXor (_) bxor; + BitAnd (_) band; + BitOr (_) bor; + Shl (_) bug; + Shr (_) bug; + + Eq (_) fcmp(Equal); + Lt (_) fcmp(LessThan); + Le (_) fcmp(LessThanOrEqual); + Ne (_) fcmp(NotEqual); + Ge (_) fcmp(GreaterThanOrEqual); + Gt (_) fcmp(GreaterThan); + + Offset (_) bug; + }; + + CValue::ByVal(res, fx.layout_of(ty)) +} + +fn trans_char_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, "char"; Add (_) bug;