Skip to content

Commit bc45e80

Browse files
committed
Implement most 128bit binops
1 parent 39cda4d commit bc45e80

File tree

5 files changed

+114
-40
lines changed

5 files changed

+114
-40
lines changed

example/std_example.rs

+1-19
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,5 @@
33
use std::io::Write;
44

55
fn main() {
6-
assert_eq!((1u128 + 2) as u16, 3);
7-
}
8-
9-
#[derive(PartialEq)]
10-
enum LoopState {
11-
Continue(()),
12-
Break(())
13-
}
14-
15-
pub enum Instruction {
16-
Increment,
17-
Loop,
18-
}
19-
20-
fn map(a: Option<(u8, Box<Instruction>)>) -> Option<Box<Instruction>> {
21-
match a {
22-
None => None,
23-
Some((_, instr)) => Some(instr),
24-
}
6+
assert_eq!(1u128 + 2, 3);
257
}

src/abi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ pub fn codegen_drop<'a, 'tcx: 'a>(
846846
);
847847
drop_place.write_place_ref(fx, arg_place);
848848
let arg_value = arg_place.to_cvalue(fx);
849-
crate::abi::codegen_call_inner(
849+
codegen_call_inner(
850850
fx,
851851
None,
852852
drop_fn_ty,

src/base.rs

+4-20
Original file line numberDiff line numberDiff line change
@@ -783,18 +783,8 @@ pub fn trans_int_binop<'a, 'tcx: 'a>(
783783
);
784784
}
785785

786-
if lhs.layout().ty == fx.tcx.types.u128 || lhs.layout().ty == fx.tcx.types.i128 {
787-
if out_ty == fx.tcx.types.bool {
788-
let layout = fx.layout_of(fx.tcx.types.bool);
789-
let val = fx.bcx.ins().iconst(types::I8, 0);
790-
return CValue::by_val(val, layout);
791-
} else {
792-
let layout = fx.layout_of(out_ty);
793-
let a = fx.bcx.ins().iconst(types::I64, 42);
794-
let b = fx.bcx.ins().iconst(types::I64, 0);
795-
let val = fx.bcx.ins().iconcat(a, b);
796-
return CValue::by_val(val, layout);
797-
}
786+
if let Some(res) = crate::codegen_i128::maybe_codegen(fx, bin_op, false, signed, lhs, rhs, out_ty) {
787+
return res;
798788
}
799789

800790
binop_match! {
@@ -846,14 +836,8 @@ pub fn trans_checked_int_binop<'a, 'tcx: 'a>(
846836

847837
let lhs = in_lhs.load_scalar(fx);
848838
let rhs = in_rhs.load_scalar(fx);
849-
let res = if in_lhs.layout().ty == fx.tcx.types.u128 || in_lhs.layout().ty == fx.tcx.types.i128 {
850-
match (bin_op, signed) {
851-
_ => {
852-
let a = fx.bcx.ins().iconst(types::I64, 42);
853-
let b = fx.bcx.ins().iconst(types::I64, 0);
854-
fx.bcx.ins().iconcat(a, b)
855-
}
856-
}
839+
let res = if let Some(res) = crate::codegen_i128::maybe_codegen(fx, bin_op, true, signed, in_lhs, in_rhs, out_ty) {
840+
return res;
857841
} else {
858842
match bin_op {
859843
BinOp::Add => fx.bcx.ins().iadd(lhs, rhs),

src/codegen_i128.rs

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
}

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ mod allocator;
3434
mod analyze;
3535
mod archive;
3636
mod base;
37+
mod codegen_i128;
3738
mod common;
3839
mod constant;
3940
mod debuginfo;

0 commit comments

Comments
 (0)