Skip to content

Commit b4e55cd

Browse files
committed
Refactor num.rs
1 parent db071db commit b4e55cd

File tree

4 files changed

+183
-269
lines changed

4 files changed

+183
-269
lines changed

src/base.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -264,30 +264,19 @@ fn trans_stmt<'a, 'tcx: 'a>(
264264
let lhs = trans_operand(fx, lhs);
265265
let rhs = trans_operand(fx, rhs);
266266

267-
let res = match lhs.layout().ty.sty {
268-
ty::Bool => crate::num::trans_bool_binop(fx, *bin_op, lhs, rhs),
269-
ty::Uint(_) | ty::Int(_ )=> {
270-
crate::num::trans_int_binop(fx, *bin_op, lhs, rhs, lval.layout().ty)
271-
}
272-
ty::Float(_) => crate::num::trans_float_binop(fx, *bin_op, lhs, rhs, lval.layout().ty),
273-
ty::Char => crate::num::trans_char_binop(fx, *bin_op, lhs, rhs, lval.layout().ty),
274-
ty::RawPtr(..) | ty::FnPtr(..) => {
275-
crate::num::trans_ptr_binop(fx, *bin_op, lhs, rhs, lval.layout().ty)
276-
}
277-
_ => unimplemented!("{:?}({:?}, {:?})", bin_op, lhs.layout().ty, rhs.layout().ty),
278-
};
267+
let res = crate::num::codegen_binop(fx, *bin_op, lhs, rhs);
279268
lval.write_cvalue(fx, res);
280269
}
281270
Rvalue::CheckedBinaryOp(bin_op, lhs, rhs) => {
282271
let lhs = trans_operand(fx, lhs);
283272
let rhs = trans_operand(fx, rhs);
284273

285274
let res = if !fx.tcx.sess.overflow_checks() {
286-
let val = crate::num::trans_int_binop(fx, *bin_op, lhs, rhs, lhs.layout().ty).load_scalar(fx);
275+
let val = crate::num::trans_int_binop(fx, *bin_op, lhs, rhs).load_scalar(fx);
287276
let is_overflow = fx.bcx.ins().iconst(types::I8, 0);
288277
CValue::by_val_pair(val, is_overflow, lval.layout())
289278
} else {
290-
crate::num::trans_checked_int_binop(fx, *bin_op, lhs, rhs, lval.layout().ty)
279+
crate::num::trans_checked_int_binop(fx, *bin_op, lhs, rhs)
291280
};
292281

293282
lval.write_cvalue(fx, res);
@@ -314,6 +303,7 @@ fn trans_stmt<'a, 'tcx: 'a>(
314303
ty::Int(_) => {
315304
let clif_ty = fx.clif_type(layout.ty).unwrap();
316305
if clif_ty == types::I128 {
306+
// FIXME implement it
317307
crate::trap::trap_unreachable_ret_value(fx, layout, "i128 neg is not yet supported").load_scalar(fx)
318308
} else {
319309
let zero = fx.bcx.ins().iconst(clif_ty, 0);

src/codegen_i128.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ pub fn maybe_codegen<'a, 'tcx>(
88
checked: bool,
99
lhs: CValue<'tcx>,
1010
rhs: CValue<'tcx>,
11-
out_ty: Ty<'tcx>,
1211
) -> Option<CValue<'tcx>> {
1312
if lhs.layout().ty != fx.tcx.types.u128 && lhs.layout().ty != fx.tcx.types.i128 {
1413
return None;
@@ -26,13 +25,15 @@ pub fn maybe_codegen<'a, 'tcx>(
2625
}
2726
BinOp::Add | BinOp::Sub if !checked => return None,
2827
BinOp::Add => {
28+
let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
2929
return Some(if is_signed {
3030
fx.easy_call("__rust_i128_addo", &[lhs, rhs], out_ty)
3131
} else {
3232
fx.easy_call("__rust_u128_addo", &[lhs, rhs], out_ty)
3333
})
3434
}
3535
BinOp::Sub => {
36+
let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
3637
return Some(if is_signed {
3738
fx.easy_call("__rust_i128_subo", &[lhs, rhs], out_ty)
3839
} else {
@@ -42,6 +43,7 @@ pub fn maybe_codegen<'a, 'tcx>(
4243
BinOp::Offset => unreachable!("offset should only be used on pointers, not 128bit ints"),
4344
BinOp::Mul => {
4445
let res = if checked {
46+
let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
4547
if is_signed {
4648
fx.easy_call("__rust_i128_mulo", &[lhs, rhs], out_ty)
4749
} else {
@@ -91,18 +93,7 @@ pub fn maybe_codegen<'a, 'tcx>(
9193
// } else {
9294
// msb_cc
9395
// }
94-
let cc = match (bin_op, is_signed) {
95-
(BinOp::Ge, false) => IntCC::UnsignedGreaterThanOrEqual,
96-
(BinOp::Gt, false) => IntCC::UnsignedGreaterThan,
97-
(BinOp::Lt, false) => IntCC::UnsignedLessThan,
98-
(BinOp::Le, false) => IntCC::UnsignedLessThanOrEqual,
99-
100-
(BinOp::Ge, true) => IntCC::SignedGreaterThanOrEqual,
101-
(BinOp::Gt, true) => IntCC::SignedGreaterThan,
102-
(BinOp::Lt, true) => IntCC::SignedLessThan,
103-
(BinOp::Le, true) => IntCC::SignedLessThanOrEqual,
104-
_ => unreachable!(),
105-
};
96+
let cc = crate::num::bin_op_to_intcc(bin_op, is_signed).unwrap();
10697

10798
let msb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_msb, rhs_msb);
10899
let lsb_cc = fx.bcx.ins().icmp(cc, lhs_lsb, rhs_lsb);
@@ -160,6 +151,7 @@ pub fn maybe_codegen<'a, 'tcx>(
160151
};
161152
if let Some(val) = val {
162153
if let Some(is_overflow) = is_overflow {
154+
let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
163155
let val = val.load_scalar(fx);
164156
return Some(CValue::by_val_pair(val, is_overflow, fx.layout_of(out_ty)))
165157
} else {
@@ -186,6 +178,7 @@ pub fn maybe_codegen<'a, 'tcx>(
186178
(_, _) => unreachable!(),
187179
};
188180
if let Some(is_overflow) = is_overflow {
181+
let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
189182
let val = val.load_scalar(fx);
190183
Some(CValue::by_val_pair(val, is_overflow, fx.layout_of(out_ty)))
191184
} else {

src/intrinsics.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ pub fn codegen_intrinsic_call<'a, 'tcx: 'a>(
446446
"unchecked_shr" => BinOp::Shr,
447447
_ => unimplemented!("intrinsic {}", intrinsic),
448448
};
449-
let res = crate::num::trans_int_binop(fx, bin_op, x, y, ret.layout().ty);
449+
let res = crate::num::trans_int_binop(fx, bin_op, x, y);
450450
ret.write_cvalue(fx, res);
451451
};
452452
_ if intrinsic.ends_with("_with_overflow"), (c x, c y) {
@@ -463,7 +463,6 @@ pub fn codegen_intrinsic_call<'a, 'tcx: 'a>(
463463
bin_op,
464464
x,
465465
y,
466-
ret.layout().ty,
467466
);
468467
ret.write_cvalue(fx, res);
469468
};
@@ -480,7 +479,6 @@ pub fn codegen_intrinsic_call<'a, 'tcx: 'a>(
480479
bin_op,
481480
x,
482481
y,
483-
ret.layout().ty,
484482
);
485483
ret.write_cvalue(fx, res);
486484
};
@@ -499,7 +497,6 @@ pub fn codegen_intrinsic_call<'a, 'tcx: 'a>(
499497
bin_op,
500498
x,
501499
y,
502-
fx.tcx.mk_tup([T, fx.tcx.types.bool].into_iter()),
503500
);
504501

505502
let (val, has_overflow) = checked_res.load_scalar_pair(fx);

0 commit comments

Comments
 (0)