|
| 1 | +//! Replaces 128-bit operators with lang item calls |
| 2 | +
|
| 3 | +use crate::prelude::*; |
| 4 | + |
| 5 | +pub fn maybe_codegen<'a, 'tcx>( |
| 6 | + fx: &mut FunctionCx<'a, 'tcx, impl Backend>, |
| 7 | + bin_op: BinOp, |
| 8 | + checked: bool, |
| 9 | + is_signed: bool, |
| 10 | + lhs: CValue<'tcx>, |
| 11 | + rhs: CValue<'tcx>, |
| 12 | + out_ty: Ty<'tcx>, |
| 13 | +) -> Option<CValue<'tcx>> { |
| 14 | + if lhs.layout().ty != fx.tcx.types.u128 && lhs.layout().ty != fx.tcx.types.i128 { |
| 15 | + return None; |
| 16 | + } |
| 17 | + |
| 18 | + let lhs_val = lhs.load_scalar(fx); |
| 19 | + let rhs_val = rhs.load_scalar(fx); |
| 20 | + |
| 21 | + match bin_op { |
| 22 | + BinOp::Add | BinOp::Sub | BinOp::BitAnd | BinOp::BitOr | BinOp::BitXor => return None, |
| 23 | + BinOp::Offset => unreachable!("offset should only be used on pointers, not 128bit ints"), |
| 24 | + BinOp::Mul => { |
| 25 | + let res = if checked { |
| 26 | + if is_signed { |
| 27 | + let oflow_place = CPlace::new_stack_slot(fx, fx.tcx.types.i32); |
| 28 | + let oflow_addr = oflow_place.to_addr(fx); |
| 29 | + let oflow_addr = CValue::by_val(oflow_addr, fx.layout_of(fx.tcx.mk_mut_ptr(fx.tcx.types.i32))); |
| 30 | + let val = fx.easy_call("__muloti4", &[lhs, rhs, oflow_addr], fx.tcx.types.i128); |
| 31 | + let val = val.load_scalar(fx); |
| 32 | + let oflow = oflow_place.to_cvalue(fx).load_scalar(fx); |
| 33 | + let oflow = fx.bcx.ins().icmp_imm(IntCC::NotEqual, oflow, 0); |
| 34 | + let oflow = fx.bcx.ins().bint(types::I8, oflow); |
| 35 | + CValue::by_val_pair(val, oflow, fx.layout_of(out_ty)) |
| 36 | + } else { |
| 37 | + // FIXME implement it |
| 38 | + let out_layout = fx.layout_of(out_ty); |
| 39 | + return Some(crate::trap::trap_unreachable_ret_value(fx, out_layout, format!("unimplemented 128bit checked binop unsigned mul"))); |
| 40 | + } |
| 41 | + } else { |
| 42 | + let val_ty = if is_signed { fx.tcx.types.i128 } else { fx.tcx.types.u128 }; |
| 43 | + fx.easy_call("__multi3", &[lhs, rhs], val_ty) |
| 44 | + }; |
| 45 | + return Some(res); |
| 46 | + } |
| 47 | + BinOp::Div => { |
| 48 | + let res = if checked { |
| 49 | + // FIXME implement it |
| 50 | + let out_layout = fx.layout_of(out_ty); |
| 51 | + return Some(crate::trap::trap_unreachable_ret_value(fx, out_layout, format!("unimplemented 128bit checked binop div"))); |
| 52 | + } else { |
| 53 | + if is_signed { |
| 54 | + fx.easy_call("__divti3", &[lhs, rhs], fx.tcx.types.i128) |
| 55 | + } else { |
| 56 | + fx.easy_call("__udivti3", &[lhs, rhs], fx.tcx.types.u128) |
| 57 | + } |
| 58 | + }; |
| 59 | + return Some(res); |
| 60 | + } |
| 61 | + BinOp::Rem => { |
| 62 | + let res = if checked { |
| 63 | + // FIXME implement it |
| 64 | + let out_layout = fx.layout_of(out_ty); |
| 65 | + return Some(crate::trap::trap_unreachable_ret_value(fx, out_layout, format!("unimplemented 128bit checked binop rem"))); |
| 66 | + } else { |
| 67 | + if is_signed { |
| 68 | + fx.easy_call("__modti3", &[lhs, rhs], fx.tcx.types.i128) |
| 69 | + } else { |
| 70 | + fx.easy_call("__umodti3", &[lhs, rhs], fx.tcx.types.u128) |
| 71 | + } |
| 72 | + }; |
| 73 | + return Some(res); |
| 74 | + } |
| 75 | + BinOp::Lt | BinOp::Le | BinOp::Eq | BinOp::Ge | BinOp::Gt | BinOp::Ne => { |
| 76 | + assert!(!checked); |
| 77 | + let (lhs_lsb, lhs_msb) = fx.bcx.ins().isplit(lhs_val); |
| 78 | + let (rhs_lsb, rhs_msb) = fx.bcx.ins().isplit(rhs_val); |
| 79 | + let res = match (bin_op, is_signed) { |
| 80 | + (BinOp::Eq, _) => { |
| 81 | + let lsb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_lsb, rhs_lsb); |
| 82 | + let msb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_msb, rhs_msb); |
| 83 | + fx.bcx.ins().band(lsb_eq, msb_eq) |
| 84 | + } |
| 85 | + (BinOp::Ne, _) => { |
| 86 | + let lsb_ne = fx.bcx.ins().icmp(IntCC::NotEqual, lhs_lsb, rhs_lsb); |
| 87 | + let msb_ne = fx.bcx.ins().icmp(IntCC::NotEqual, lhs_msb, rhs_msb); |
| 88 | + fx.bcx.ins().bor(lsb_ne, msb_ne) |
| 89 | + } |
| 90 | + _ => { |
| 91 | + // FIXME implement it |
| 92 | + let out_layout = fx.layout_of(out_ty); |
| 93 | + return Some(crate::trap::trap_unreachable_ret_value(fx, out_layout, format!("unimplemented 128bit binop {:?}", bin_op))); |
| 94 | + }, |
| 95 | + }; |
| 96 | + |
| 97 | + let res = fx.bcx.ins().bint(types::I8, res); |
| 98 | + let res = CValue::by_val(res, fx.layout_of(fx.tcx.types.bool)); |
| 99 | + return Some(res); |
| 100 | + } |
| 101 | + BinOp::Shl | BinOp::Shr => { |
| 102 | + // FIXME implement it |
| 103 | + let out_layout = fx.layout_of(out_ty); |
| 104 | + return Some(crate::trap::trap_unreachable_ret_value(fx, out_layout, format!("unimplemented 128bit binop {:?}", bin_op))); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments