1 //! Replaces 128-bit operators with lang item calls
5 pub fn maybe_codegen<'a, 'tcx>(
6 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
13 ) -> Option<CValue<'tcx>> {
14 if lhs.layout().ty != fx.tcx.types.u128 && lhs.layout().ty != fx.tcx.types.i128 {
18 let lhs_val = lhs.load_scalar(fx);
19 let rhs_val = rhs.load_scalar(fx);
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"),
25 let res = if checked {
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))
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")));
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)
48 let res = if checked {
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")));
54 fx.easy_call("__divti3", &[lhs, rhs], fx.tcx.types.i128)
56 fx.easy_call("__udivti3", &[lhs, rhs], fx.tcx.types.u128)
62 let res = if checked {
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")));
68 fx.easy_call("__modti3", &[lhs, rhs], fx.tcx.types.i128)
70 fx.easy_call("__umodti3", &[lhs, rhs], fx.tcx.types.u128)
75 BinOp::Lt | BinOp::Le | BinOp::Eq | BinOp::Ge | BinOp::Gt | BinOp::Ne => {
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) {
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)
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)
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)));
97 let res = fx.bcx.ins().bint(types::I8, res);
98 let res = CValue::by_val(res, fx.layout_of(fx.tcx.types.bool));
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)));