Skip to content

Commit a302f88

Browse files
authored
Merge pull request #627 from bjorn3/wip_i128
Support 128bit numbers
2 parents bba11f6 + 770c12a commit a302f88

17 files changed

+455
-232
lines changed

Cargo.lock

+40-40
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ gimli = { git = "https://github.com/gimli-rs/gimli.git" }
2727
indexmap = "1.0.2"
2828
object = "0.12.0"
2929

30+
[patch."https://github.com/CraneStation/cranelift.git"]
31+
cranelift = { git = "https://github.com/bjorn3/cretonne.git", branch = "do_not_remove_cg_clif_i128" }
32+
cranelift-module = { git = "https://github.com/bjorn3/cretonne.git", branch = "do_not_remove_cg_clif_i128" }
33+
cranelift-simplejit = { git = "https://github.com/bjorn3/cretonne.git", branch = "do_not_remove_cg_clif_i128" }
34+
cranelift-faerie = { git = "https://github.com/bjorn3/cretonne.git", branch = "do_not_remove_cg_clif_i128" }
35+
3036
# Uncomment to use local checkout of cranelift
3137
#[patch."https://github.com/CraneStation/cranelift.git"]
3238
#cranelift = { path = "../cranelift/cranelift-umbrella" }

Readme.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ $ RUSTFLAGS="-Cpanic=abort -Zcodegen-backend=$cg_clif_dir/target/debug/librustc_
2929

3030
## Not yet supported
3131

32-
* Good non-rust abi support ([scalar pair and vector are passed by-ref](https://github.com/bjorn3/rustc_codegen_cranelift/issues/10))
32+
* Good non-rust abi support ([vectors are passed by-ref](https://github.com/bjorn3/rustc_codegen_cranelift/issues/10))
3333
* Checked binops ([some missing instructions in cranelift](https://github.com/CraneStation/cranelift/issues/460))
3434
* Inline assembly ([no cranelift support](https://github.com/CraneStation/cranelift/issues/444))
35-
* u128 and i128 ([no cranelift support](https://github.com/CraneStation/cranelift/issues/354))
3635
* SIMD ([tracked here](https://github.com/bjorn3/rustc_codegen_cranelift/issues/171))
3736

3837
## Troubleshooting

build_sysroot/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ alloc_system = { path = "./alloc_system" }
1414
[patch.crates-io]
1515
rustc-std-workspace-core = { path = "./sysroot_src/src/tools/rustc-std-workspace-core" }
1616
rustc-std-workspace-alloc = { path = "./rustc-std-workspace-alloc" }
17-
compiler_builtins = { path = "./compiler_builtins" }
1817

1918
[profile.release]
2019
debug = true

build_sysroot/compiler_builtins/Cargo.toml

-20
This file was deleted.

build_sysroot/compiler_builtins/lib.rs

-9
This file was deleted.

example/mini_core.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ impl Neg for i16 {
314314
type Output = i16;
315315

316316
fn neg(self) -> i16 {
317-
-self
317+
self
318318
}
319319
}
320320

example/mini_core_hello_world.rs

-17
Original file line numberDiff line numberDiff line change
@@ -236,21 +236,4 @@ fn main() {
236236
unsafe { assert_eq!(ABC as usize, 0); }
237237

238238
&mut (|| Some(0 as *const ())) as &mut FnMut() -> Option<*const ()>;
239-
240-
// checked binops
241-
let zeroi8 = 0i8;
242-
let oneu8 = 1u8;
243-
let onei8 = 1i8;
244-
zeroi8 - 1;
245-
oneu8 - 1;
246-
zeroi8 - -2i8;
247-
#[allow(unreachable_code)]
248-
{
249-
if false {
250-
let minustwoi8 = -2i8;
251-
oneu8 + 255;
252-
onei8 + 127;
253-
minustwoi8 - 127;
254-
}
255-
}
256239
}

example/std_example.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![feature(core_intrinsics)]
22

33
use std::io::Write;
4+
use std::intrinsics;
45

56
fn main() {
67
let _ = ::std::iter::repeat('a' as u8).take(10).collect::<Vec<_>>();
@@ -29,6 +30,18 @@ fn main() {
2930
println!("{}", 2.3f32.ceil());
3031
println!("{}", 2.3f32.min(1.0));
3132
println!("{}", 2.3f32.max(1.0));
33+
34+
assert_eq!(0b0000000000000000000000000010000010000000000000000000000000000000_0000000000100000000000000000000000001000000000000100000000000000u128.leading_zeros(), 26);
35+
assert_eq!(0b0000000000000000000000000010000000000000000000000000000000000000_0000000000000000000000000000000000001000000000000000000010000000u128.trailing_zeros(), 7);
36+
37+
0i128.checked_div(2i128);
38+
0u128.checked_div(2u128);
39+
assert_eq!(1u128 + 2, 3);
40+
41+
assert_eq!(0b100010000000000000000000000000000u128 >> 10, 0b10001000000000000000000u128);
42+
assert_eq!(0xFEDCBA987654321123456789ABCDEFu128 >> 64, 0xFEDCBA98765432u128);
43+
assert_eq!(0xFEDCBA987654321123456789ABCDEFu128 as i128 >> 64, 0xFEDCBA98765432i128);
44+
assert_eq!(353985398u128 * 932490u128, 330087843781020u128);
3245
}
3346

3447
#[derive(PartialEq)]

src/abi.rs

+20-25
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub fn scalar_to_clif_type(tcx: TyCtxt, scalar: Scalar) -> Type {
8787
Integer::I16 => types::I16,
8888
Integer::I32 => types::I32,
8989
Integer::I64 => types::I64,
90-
Integer::I128 => unimpl!("u/i128"),
90+
Integer::I128 => types::I128,
9191
},
9292
Primitive::Float(flt) => match flt {
9393
FloatTy::F32 => types::F32,
@@ -252,14 +252,12 @@ impl<'a, 'tcx: 'a, B: Backend + 'a> FunctionCx<'a, 'tcx, B> {
252252
&mut self,
253253
name: &str,
254254
input_tys: Vec<types::Type>,
255-
output_ty: Option<types::Type>,
255+
output_tys: Vec<types::Type>,
256256
args: &[Value],
257-
) -> Option<Value> {
257+
) -> &[Value] {
258258
let sig = Signature {
259259
params: input_tys.iter().cloned().map(AbiParam::new).collect(),
260-
returns: output_ty
261-
.map(|output_ty| vec![AbiParam::new(output_ty)])
262-
.unwrap_or(Vec::new()),
260+
returns: output_tys.iter().cloned().map(AbiParam::new).collect(),
263261
call_conv: CallConv::SystemV,
264262
};
265263
let func_id = self
@@ -270,12 +268,10 @@ impl<'a, 'tcx: 'a, B: Backend + 'a> FunctionCx<'a, 'tcx, B> {
270268
.module
271269
.declare_func_in_func(func_id, &mut self.bcx.func);
272270
let call_inst = self.bcx.ins().call(func_ref, args);
273-
if output_ty.is_none() {
274-
return None;
275-
}
271+
self.add_comment(call_inst, format!("easy_call {}", name));
276272
let results = self.bcx.inst_results(call_inst);
277-
assert_eq!(results.len(), 1);
278-
Some(results[0])
273+
assert!(results.len() <= 2, "{}", results.len());
274+
results
279275
}
280276

281277
pub fn easy_call(
@@ -294,23 +290,22 @@ impl<'a, 'tcx: 'a, B: Backend + 'a> FunctionCx<'a, 'tcx, B> {
294290
})
295291
.unzip();
296292
let return_layout = self.layout_of(return_ty);
297-
let return_ty = if let ty::Tuple(tup) = return_ty.sty {
298-
if !tup.is_empty() {
299-
bug!("easy_call( (...) -> <non empty tuple> ) is not allowed");
300-
}
301-
None
293+
let return_tys = if let ty::Tuple(tup) = return_ty.sty {
294+
tup.types().map(|ty| self.clif_type(ty).unwrap()).collect()
302295
} else {
303-
Some(self.clif_type(return_ty).unwrap())
296+
vec![self.clif_type(return_ty).unwrap()]
304297
};
305-
if let Some(val) = self.lib_call(name, input_tys, return_ty, &args) {
306-
CValue::by_val(val, return_layout)
307-
} else {
308-
CValue::by_ref(
298+
let ret_vals = self.lib_call(name, input_tys, return_tys, &args);
299+
match *ret_vals {
300+
[] => CValue::by_ref(
309301
self.bcx
310302
.ins()
311303
.iconst(self.pointer_type, self.pointer_type.bytes() as i64),
312304
return_layout,
313-
)
305+
),
306+
[val] => CValue::by_val(val, return_layout),
307+
[val, extra] => CValue::by_val_pair(val, extra, return_layout),
308+
_ => unreachable!(),
314309
}
315310
}
316311

@@ -346,7 +341,7 @@ fn add_arg_comment<'a, 'tcx: 'a>(
346341
};
347342
let pass_mode = format!("{:?}", pass_mode);
348343
fx.add_global_comment(format!(
349-
"{msg:5} {local:>3}{local_field:<5} {params:10} {pass_mode:20} {ssa:10} {ty:?}",
344+
"{msg:5} {local:>3}{local_field:<5} {params:10} {pass_mode:36} {ssa:10} {ty:?}",
350345
msg = msg,
351346
local = format!("{:?}", local),
352347
local_field = local_field,
@@ -360,7 +355,7 @@ fn add_arg_comment<'a, 'tcx: 'a>(
360355
#[cfg(debug_assertions)]
361356
fn add_local_header_comment(fx: &mut FunctionCx<impl Backend>) {
362357
fx.add_global_comment(format!(
363-
"msg loc.idx param pass mode ssa flags ty"
358+
"msg loc.idx param pass mode ssa flags ty"
364359
));
365360
}
366361

@@ -846,7 +841,7 @@ pub fn codegen_drop<'a, 'tcx: 'a>(
846841
);
847842
drop_place.write_place_ref(fx, arg_place);
848843
let arg_value = arg_place.to_cvalue(fx);
849-
crate::abi::codegen_call_inner(
844+
codegen_call_inner(
850845
fx,
851846
None,
852847
drop_fn_ty,

0 commit comments

Comments
 (0)