1 //! Replaces 128-bit operators with lang item calls where necessary
3 use cranelift_codegen::ir::ArgumentPurpose;
7 pub(crate) fn maybe_codegen<'tcx>(
8 fx: &mut FunctionCx<'_, '_, 'tcx>,
13 ) -> Option<CValue<'tcx>> {
14 if lhs.layout().ty != fx.tcx.types.u128
15 && lhs.layout().ty != fx.tcx.types.i128
16 && rhs.layout().ty != fx.tcx.types.u128
17 && rhs.layout().ty != fx.tcx.types.i128
22 let is_signed = type_sign(lhs.layout().ty);
25 BinOp::BitAnd | BinOp::BitOr | BinOp::BitXor => {
29 BinOp::Add | BinOp::Sub if !checked => None,
30 BinOp::Mul if !checked || is_signed => {
32 let val_ty = if is_signed { fx.tcx.types.i128 } else { fx.tcx.types.u128 };
33 if fx.tcx.sess.target.is_like_windows {
34 let ret_place = CPlace::new_stack_slot(fx, lhs.layout());
35 let (lhs_ptr, lhs_extra) = lhs.force_stack(fx);
36 let (rhs_ptr, rhs_extra) = rhs.force_stack(fx);
37 assert!(lhs_extra.is_none());
38 assert!(rhs_extra.is_none());
40 ret_place.to_ptr().get_addr(fx),
47 AbiParam::special(fx.pointer_type, ArgumentPurpose::StructReturn),
48 AbiParam::new(fx.pointer_type),
49 AbiParam::new(fx.pointer_type),
54 Some(ret_place.to_cvalue(fx))
56 Some(fx.easy_call("__multi3", &[lhs, rhs], val_ty))
59 let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
60 let oflow = CPlace::new_stack_slot(fx, fx.layout_of(fx.tcx.types.i32));
61 let lhs = lhs.load_scalar(fx);
62 let rhs = rhs.load_scalar(fx);
63 let oflow_ptr = oflow.to_ptr().get_addr(fx);
64 let res = fx.lib_call(
67 AbiParam::new(types::I128),
68 AbiParam::new(types::I128),
69 AbiParam::new(fx.pointer_type),
71 vec![AbiParam::new(types::I128)],
72 &[lhs, rhs, oflow_ptr],
74 let oflow = oflow.to_cvalue(fx).load_scalar(fx);
75 let oflow = fx.bcx.ins().ireduce(types::I8, oflow);
76 Some(CValue::by_val_pair(res, oflow, fx.layout_of(out_ty)))
79 BinOp::Add | BinOp::Sub | BinOp::Mul => {
81 let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
82 let out_place = CPlace::new_stack_slot(fx, fx.layout_of(out_ty));
83 let (param_types, args) = if fx.tcx.sess.target.is_like_windows {
84 let (lhs_ptr, lhs_extra) = lhs.force_stack(fx);
85 let (rhs_ptr, rhs_extra) = rhs.force_stack(fx);
86 assert!(lhs_extra.is_none());
87 assert!(rhs_extra.is_none());
90 AbiParam::special(fx.pointer_type, ArgumentPurpose::StructReturn),
91 AbiParam::new(fx.pointer_type),
92 AbiParam::new(fx.pointer_type),
94 [out_place.to_ptr().get_addr(fx), lhs_ptr.get_addr(fx), rhs_ptr.get_addr(fx)],
99 AbiParam::special(fx.pointer_type, ArgumentPurpose::StructReturn),
100 AbiParam::new(types::I128),
101 AbiParam::new(types::I128),
103 [out_place.to_ptr().get_addr(fx), lhs.load_scalar(fx), rhs.load_scalar(fx)],
106 let name = match (bin_op, is_signed) {
107 (BinOp::Add, false) => "__rust_u128_addo",
108 (BinOp::Add, true) => "__rust_i128_addo",
109 (BinOp::Sub, false) => "__rust_u128_subo",
110 (BinOp::Sub, true) => "__rust_i128_subo",
111 (BinOp::Mul, false) => "__rust_u128_mulo",
114 fx.lib_call(name, param_types, vec![], &args);
115 Some(out_place.to_cvalue(fx))
117 BinOp::Offset => unreachable!("offset should only be used on pointers, not 128bit ints"),
118 BinOp::Div | BinOp::Rem => {
120 let name = match (bin_op, is_signed) {
121 (BinOp::Div, false) => "__udivti3",
122 (BinOp::Div, true) => "__divti3",
123 (BinOp::Rem, false) => "__umodti3",
124 (BinOp::Rem, true) => "__modti3",
127 if fx.tcx.sess.target.is_like_windows {
128 let (lhs_ptr, lhs_extra) = lhs.force_stack(fx);
129 let (rhs_ptr, rhs_extra) = rhs.force_stack(fx);
130 assert!(lhs_extra.is_none());
131 assert!(rhs_extra.is_none());
132 let args = [lhs_ptr.get_addr(fx), rhs_ptr.get_addr(fx)];
133 let ret = fx.lib_call(
135 vec![AbiParam::new(fx.pointer_type), AbiParam::new(fx.pointer_type)],
136 vec![AbiParam::new(types::I64X2)],
139 // FIXME use bitcast instead of store to get from i64x2 to i128
140 let ret_place = CPlace::new_stack_slot(fx, lhs.layout());
141 ret_place.to_ptr().store(fx, ret, MemFlags::trusted());
142 Some(ret_place.to_cvalue(fx))
144 Some(fx.easy_call(name, &[lhs, rhs], lhs.layout().ty))
147 BinOp::Lt | BinOp::Le | BinOp::Eq | BinOp::Ge | BinOp::Gt | BinOp::Ne => {
151 BinOp::Shl | BinOp::Shr => None,