Skip to content

Commit db071db

Browse files
committed
Calculate sign in trans{,_checked}_int_binop instead of caller
1 parent 32cb5b8 commit db071db

File tree

4 files changed

+17
-42
lines changed

4 files changed

+17
-42
lines changed

src/base.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -261,40 +261,33 @@ fn trans_stmt<'a, 'tcx: 'a>(
261261
place.write_place_ref(fx, lval);
262262
}
263263
Rvalue::BinaryOp(bin_op, lhs, rhs) => {
264-
let ty = fx.monomorphize(&lhs.ty(fx.mir, fx.tcx));
265264
let lhs = trans_operand(fx, lhs);
266265
let rhs = trans_operand(fx, rhs);
267266

268-
let res = match ty.sty {
267+
let res = match lhs.layout().ty.sty {
269268
ty::Bool => crate::num::trans_bool_binop(fx, *bin_op, lhs, rhs),
270-
ty::Uint(_) => {
271-
crate::num::trans_int_binop(fx, *bin_op, lhs, rhs, lval.layout().ty, false)
272-
}
273-
ty::Int(_) => {
274-
crate::num::trans_int_binop(fx, *bin_op, lhs, rhs, lval.layout().ty, true)
269+
ty::Uint(_) | ty::Int(_ )=> {
270+
crate::num::trans_int_binop(fx, *bin_op, lhs, rhs, lval.layout().ty)
275271
}
276272
ty::Float(_) => crate::num::trans_float_binop(fx, *bin_op, lhs, rhs, lval.layout().ty),
277273
ty::Char => crate::num::trans_char_binop(fx, *bin_op, lhs, rhs, lval.layout().ty),
278274
ty::RawPtr(..) | ty::FnPtr(..) => {
279275
crate::num::trans_ptr_binop(fx, *bin_op, lhs, rhs, lval.layout().ty)
280276
}
281-
_ => unimplemented!("binop {:?} for {:?}", bin_op, ty),
277+
_ => unimplemented!("{:?}({:?}, {:?})", bin_op, lhs.layout().ty, rhs.layout().ty),
282278
};
283279
lval.write_cvalue(fx, res);
284280
}
285281
Rvalue::CheckedBinaryOp(bin_op, lhs, rhs) => {
286-
let ty = fx.monomorphize(&lhs.ty(fx.mir, fx.tcx));
287282
let lhs = trans_operand(fx, lhs);
288283
let rhs = trans_operand(fx, rhs);
289284

290-
let signed = type_sign(ty);
291-
292285
let res = if !fx.tcx.sess.overflow_checks() {
293-
let val = crate::num::trans_int_binop(fx, *bin_op, lhs, rhs, lhs.layout().ty, signed).load_scalar(fx);
286+
let val = crate::num::trans_int_binop(fx, *bin_op, lhs, rhs, lhs.layout().ty).load_scalar(fx);
294287
let is_overflow = fx.bcx.ins().iconst(types::I8, 0);
295288
CValue::by_val_pair(val, is_overflow, lval.layout())
296289
} else {
297-
crate::num::trans_checked_int_binop(fx, *bin_op, lhs, rhs, lval.layout().ty, signed)
290+
crate::num::trans_checked_int_binop(fx, *bin_op, lhs, rhs, lval.layout().ty)
298291
};
299292

300293
lval.write_cvalue(fx, res);

src/codegen_i128.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ pub fn maybe_codegen<'a, 'tcx>(
66
fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
77
bin_op: BinOp,
88
checked: bool,
9-
is_signed: bool,
109
lhs: CValue<'tcx>,
1110
rhs: CValue<'tcx>,
1211
out_ty: Ty<'tcx>,
@@ -18,6 +17,8 @@ pub fn maybe_codegen<'a, 'tcx>(
1817
let lhs_val = lhs.load_scalar(fx);
1918
let rhs_val = rhs.load_scalar(fx);
2019

20+
let is_signed = type_sign(lhs.layout().ty);
21+
2122
match bin_op {
2223
BinOp::BitAnd | BinOp::BitOr | BinOp::BitXor => {
2324
assert!(!checked);

src/intrinsics.rs

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -446,28 +446,10 @@ pub fn codegen_intrinsic_call<'a, 'tcx: 'a>(
446446
"unchecked_shr" => BinOp::Shr,
447447
_ => unimplemented!("intrinsic {}", intrinsic),
448448
};
449-
let res = match ret.layout().ty.sty {
450-
ty::Uint(_) => crate::num::trans_int_binop(
451-
fx,
452-
bin_op,
453-
x,
454-
y,
455-
ret.layout().ty,
456-
false,
457-
),
458-
ty::Int(_) => crate::num::trans_int_binop(
459-
fx,
460-
bin_op,
461-
x,
462-
y,
463-
ret.layout().ty,
464-
true,
465-
),
466-
_ => panic!(),
467-
};
449+
let res = crate::num::trans_int_binop(fx, bin_op, x, y, ret.layout().ty);
468450
ret.write_cvalue(fx, res);
469451
};
470-
_ if intrinsic.ends_with("_with_overflow"), <T> (c x, c y) {
452+
_ if intrinsic.ends_with("_with_overflow"), (c x, c y) {
471453
assert_eq!(x.layout().ty, y.layout().ty);
472454
let bin_op = match intrinsic {
473455
"add_with_overflow" => BinOp::Add,
@@ -482,11 +464,10 @@ pub fn codegen_intrinsic_call<'a, 'tcx: 'a>(
482464
x,
483465
y,
484466
ret.layout().ty,
485-
type_sign(T),
486467
);
487468
ret.write_cvalue(fx, res);
488469
};
489-
_ if intrinsic.starts_with("overflowing_"), <T> (c x, c y) {
470+
_ if intrinsic.starts_with("overflowing_"), (c x, c y) {
490471
assert_eq!(x.layout().ty, y.layout().ty);
491472
let bin_op = match intrinsic {
492473
"overflowing_add" => BinOp::Add,
@@ -500,7 +481,6 @@ pub fn codegen_intrinsic_call<'a, 'tcx: 'a>(
500481
x,
501482
y,
502483
ret.layout().ty,
503-
type_sign(T),
504484
);
505485
ret.write_cvalue(fx, res);
506486
};
@@ -520,7 +500,6 @@ pub fn codegen_intrinsic_call<'a, 'tcx: 'a>(
520500
x,
521501
y,
522502
fx.tcx.mk_tup([T, fx.tcx.types.bool].into_iter()),
523-
signed,
524503
);
525504

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

src/num.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ pub fn trans_int_binop<'a, 'tcx: 'a>(
7878
lhs: CValue<'tcx>,
7979
rhs: CValue<'tcx>,
8080
out_ty: Ty<'tcx>,
81-
signed: bool,
8281
) -> CValue<'tcx> {
8382
if bin_op != BinOp::Shl && bin_op != BinOp::Shr {
8483
assert_eq!(
@@ -93,10 +92,12 @@ pub fn trans_int_binop<'a, 'tcx: 'a>(
9392
_ => unreachable!("Out ty {:?} is not an integer or bool", out_ty),
9493
}
9594

96-
if let Some(res) = crate::codegen_i128::maybe_codegen(fx, bin_op, false, signed, lhs, rhs, out_ty) {
95+
if let Some(res) = crate::codegen_i128::maybe_codegen(fx, bin_op, false, lhs, rhs, out_ty) {
9796
return res;
9897
}
9998

99+
let signed = type_sign(lhs.layout().ty);
100+
100101
let (lhs, rhs) = if
101102
(bin_op == BinOp::Eq || bin_op == BinOp::Ne)
102103
&& (lhs.layout().ty.sty == fx.tcx.types.i8.sty || lhs.layout().ty.sty == fx.tcx.types.i16.sty)
@@ -149,7 +150,6 @@ pub fn trans_checked_int_binop<'a, 'tcx: 'a>(
149150
in_lhs: CValue<'tcx>,
150151
in_rhs: CValue<'tcx>,
151152
out_ty: Ty<'tcx>,
152-
signed: bool,
153153
) -> CValue<'tcx> {
154154
if bin_op != BinOp::Shl && bin_op != BinOp::Shr {
155155
assert_eq!(
@@ -162,10 +162,12 @@ pub fn trans_checked_int_binop<'a, 'tcx: 'a>(
162162
let lhs = in_lhs.load_scalar(fx);
163163
let rhs = in_rhs.load_scalar(fx);
164164

165-
if let Some(res) = crate::codegen_i128::maybe_codegen(fx, bin_op, true, signed, in_lhs, in_rhs, out_ty) {
165+
if let Some(res) = crate::codegen_i128::maybe_codegen(fx, bin_op, true, in_lhs, in_rhs, out_ty) {
166166
return res;
167167
}
168168

169+
let signed = type_sign(in_lhs.layout().ty);
170+
169171
let (res, has_overflow) = match bin_op {
170172
BinOp::Add => {
171173
/*let (val, c_out) = fx.bcx.ins().iadd_cout(lhs, rhs);

0 commit comments

Comments
 (0)