Skip to content

Fix various failing tests. #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cilly/src/cil_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,12 @@ macro_rules! div {
};
}
#[macro_export]
macro_rules! div_un {
($a:expr,$b:expr) => {
CILNode::DivUn($a.into(), $b.into())
};
}
#[macro_export]
macro_rules! rem {
($a:expr,$b:expr) => {
CILNode::Rem($a.into(), $b.into())
Expand Down
146 changes: 77 additions & 69 deletions src/terminator/intrinsics/ints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use cilly::{
cil_node::CILNode,
cil_root::CILRoot,
conv_i16, conv_i32, conv_i8, conv_isize, conv_u16, conv_u32, conv_u64, conv_u8, conv_usize,
div, ldc_i32, ldc_u32, ldc_u64, rem_un, size_of, sub,
ldc_i32, ldc_u32, ldc_u64, or, rem_un, size_of, sub, shr_un, shl,
v2::{ClassRef, FnSig, Int},
Type,
};
use cilly::cil_node::CallOpArgs;
use rustc_middle::{
mir::{Operand, Place},
ty::Instance,
Expand Down Expand Up @@ -817,12 +818,61 @@ pub fn bitreverse_u8(byte: CILNode) -> CILNode {
))
}
fn bitreverse_u16(ushort: CILNode) -> CILNode {
conv_u16!(bitreverse_u8(conv_u8!(ushort.clone()))) * conv_u16!(ldc_u32!(256))
+ conv_u16!(bitreverse_u8(conv_u8!(div!(
ushort,
conv_u16!(ldc_u32!(256))
))))
let lower = bitreverse_u8(conv_u8!(ushort.clone()));
let upper = bitreverse_u8(conv_u8!(shr_un!(ushort, ldc_u32!(8))));
or!(shl!(conv_u16!(lower), ldc_u32!(8)), conv_u16!(upper))
}
fn bitreverse_u32(uint: CILNode) -> CILNode {
let lower = bitreverse_u16(conv_u16!(uint.clone()));
let upper = bitreverse_u16(conv_u16!(shr_un!(uint, ldc_u32!(16))));
or!(shl!(conv_u32!(lower), ldc_u32!(16)), conv_u32!(upper))
}
fn bitreverse_u64(ulong: CILNode) -> CILNode {
let lower = bitreverse_u32(conv_u32!(ulong.clone()));
let upper = bitreverse_u32(conv_u32!(shr_un!(ulong, ldc_u32!(32))));
or!(shl!(conv_u64!(lower), ldc_u32!(32)), conv_u64!(upper))
}

fn bitreverse_u128<'tcx>(ullong: CILNode, ctx: &mut MethodCompileCtx<'tcx, '_>) -> CILNode {
let lower = crate::casts::int_to_int(
Type::Int(Int::U128),
Type::Int(Int::U64),
ullong.clone(),
ctx.asm_mut(),
);
let shifted = call!(
CallSite::new_extern(
ClassRef::uint_128(ctx.asm_mut()),
"op_UnsignedRightShift".into(),
FnSig::new(
[Type::Int(Int::U128), Type::Int(Int::I32)].into(),
Type::Int(Int::U128)
),
true
),
[ullong, ldc_i32!(64)]
);
let upper = crate::casts::int_to_int(
Type::Int(Int::U128),
Type::Int(Int::U64),
shifted,
ctx.asm_mut(),
);

CILNode::NewObj(Box::new(CallOpArgs {
site: CallSite::boxed(
Some(ClassRef::uint_128(ctx.asm_mut())),
".ctor".into(),
FnSig::new(
[Type::Int(Int::U128), Type::Int(Int::U64), Type::Int(Int::U64)].into(),
Type::Void
),
false,
),
args: [bitreverse_u64(lower), bitreverse_u64(upper)].into(),
}))
}

pub fn bitreverse<'tcx>(
args: &[Spanned<Operand<'tcx>>],
destination: &Place<'tcx>,
Expand All @@ -848,82 +898,40 @@ pub fn bitreverse<'tcx>(
Type::Int(Int::I8) => conv_i8!(bitreverse_u8(val)),
Type::Int(Int::U16) => bitreverse_u16(val),
Type::Int(Int::I16) => conv_i16!(bitreverse_u16(conv_u16!(val))),
Type::Int(Int::U32) => call!(
CallSite::builtin(
"bitreverse_u32".into(),
FnSig::new([Type::Int(Int::U32)].into(), Type::Int(Int::U32)),
true
),
[val]
),
Type::Int(Int::U32) => bitreverse_u32(val),
Type::Int(Int::I32) => crate::casts::int_to_int(
Type::Int(Int::U32),
Type::Int(Int::I32),
call!(
CallSite::builtin(
"bitreverse_u32".into(),
FnSig::new([Type::Int(Int::U32)].into(), Type::Int(Int::U32)),
true
),
[crate::casts::int_to_int(
Type::Int(Int::I32),
Type::Int(Int::U32),
val,
ctx.asm_mut()
)]
),
bitreverse_u32(crate::casts::int_to_int(
Type::Int(Int::I32),
Type::Int(Int::U32),
val,
ctx.asm_mut(),
)),
ctx.asm_mut(),
),
Type::Int(Int::U64) => call!(
CallSite::builtin(
"bitreverse_u64".into(),
FnSig::new([Type::Int(Int::U64)].into(), Type::Int(Int::U64)),
true
),
[val]
),
Type::Int(Int::U64) => bitreverse_u64(val),
Type::Int(Int::I64) => crate::casts::int_to_int(
Type::Int(Int::U64),
Type::Int(Int::I64),
call!(
CallSite::builtin(
"bitreverse_u64".into(),
FnSig::new([Type::Int(Int::U64)].into(), Type::Int(Int::U64)),
true
),
[crate::casts::int_to_int(
Type::Int(Int::I64),
Type::Int(Int::U64),
val,
ctx.asm_mut()
)]
),
bitreverse_u64(crate::casts::int_to_int(
Type::Int(Int::I64),
Type::Int(Int::U64),
val,
ctx.asm_mut(),
)),
ctx.asm_mut(),
),
Type::Int(Int::U128) => call!(
CallSite::builtin(
"bitreverse_u128".into(),
FnSig::new([Type::Int(Int::U128)].into(), Type::Int(Int::U128),),
true
),
[val]
),
Type::Int(Int::U128) => bitreverse_u128(val, ctx),
Type::Int(Int::I128) => crate::casts::int_to_int(
Type::Int(Int::U128),
Type::Int(Int::I128),
call!(
CallSite::builtin(
"bitreverse_u128".into(),
FnSig::new([Type::Int(Int::U128)].into(), Type::Int(Int::U128),),
true
),
[crate::casts::int_to_int(
Type::Int(Int::I128),
Type::Int(Int::U128),
val,
ctx.asm_mut()
)]
),
bitreverse_u128(crate::casts::int_to_int(
Type::Int(Int::I128),
Type::Int(Int::U128),
val,
ctx.asm_mut(),
), ctx),
ctx.asm_mut(),
),

Expand Down
28 changes: 14 additions & 14 deletions test/alloc/slice_to_owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ fn to_vec(s: &[u8], alloc: Allocator) -> RawVec<u8> {
let count = s.iter().count();
unsafe {
printf(
"Prepraing to coppy. Spare cap %d. Data length %d. Iter count %d\n\0".as_ptr() as *const i8,
"Prepraing to coppy. Spare cap %d. Data length %d. Iter count %d\n\0".as_ptr() as *const core::ffi::c_char,
slots.len() as u32,
s.len() as u32,
count as u32,
Expand All @@ -464,20 +464,20 @@ fn to_vec(s: &[u8], alloc: Allocator) -> RawVec<u8> {
let mut idx = 0;
let mut iter = s.iter();
if !iter.next().is_some(){
unsafe { printf("Iter should have returned Some but returned None :(.\n\0".as_ptr() as *const i8) };
unsafe { printf("Iter should have returned Some but returned None :(.\n\0".as_ptr() as *const core::ffi::c_char) };
core::intrinsics::abort();
}
for b in s.iter(){
unsafe { printf("Copying at %d:%d\n\0".as_ptr() as *const i8,idx as u32,*b as u32) };
unsafe { printf("Copying at %d:%d\n\0".as_ptr() as *const core::ffi::c_char, idx as u32,*b as u32) };
guard.num_init = idx;

//let addr = &mut slots[idx] as *mut _ as *mut u8;
slots[idx].write(*b);
//unsafe{*addr = *b};

idx += 1;
}
unsafe { printf("Coppy done.\n\0".as_ptr() as *const i8) };
unsafe { printf("Coppy done.\n\0".as_ptr() as *const core::ffi::c_char) };
core::mem::forget(guard);
// SAFETY:
// the vec was allocated and initialized above to at least this length.
Expand Down Expand Up @@ -535,26 +535,26 @@ where
}
fn slice_iter_test(s: &[u8]) {
for b in s.iter() {
unsafe { printf("Iter byte %d\n\0".as_ptr() as *const i8, *b as u32) };
unsafe { printf("Iter byte %d\n\0".as_ptr() as *const core::ffi::c_char, *b as u32) };
}
}
fn slice_iter_enumerate_test1(s: &[u8]) {
for (i, b) in enumerate_(s.iter()) {
unsafe {
printf(
"IterEnum1 byte %d at index %d\n\0".as_ptr() as *const i8,
"IterEnum1 byte %d at index %d\n\0".as_ptr() as *const core::ffi::c_char,
*b as u32,
i as u32,
)
};
}
}
fn slice_iter_enumerate_test2(s: &[u8]) {

for (i, b) in s.iter().enumerate() {
unsafe {
printf(
"Iter Enum2 byte %d at index %d\n\0".as_ptr() as *const i8,
"Iter Enum2 byte %d at index %d\n\0".as_ptr() as *const core::ffi::c_char,
*b as u32,
i as u32,
)
Expand All @@ -565,11 +565,11 @@ fn enm_tuple_test(){
let v = 0;
let itr:Option<<std::iter::Enumerate<std::slice::Iter<'_, u8>> as Iterator>::Item> = Some((0_usize,&v));
if !black_box(itr).is_some(){
unsafe{printf("WTF? Some is... not some?\n\0".as_ptr() as *const i8)};
unsafe{printf("WTF? Some is... not some?\n\0".as_ptr() as *const core::ffi::c_char)};
core::intrinsics::abort();
}
else{
unsafe{printf("Ok. Some is some.\n\0".as_ptr() as *const i8)};
unsafe{printf("Ok. Some is some.\n\0".as_ptr() as *const core::ffi::c_char)};
}
}
fn uninit_test(){
Expand All @@ -587,7 +587,7 @@ fn uninit_test(){
}
}
fn main() {
/*
/*
enm_tuple_test();


Expand All @@ -603,7 +603,7 @@ fn main() {
slice_iter_enumerate_test2(original.as_bytes());
*/
uninit_test();
/*
/*
unsafe { printf(owned.as_mut_ptr() as *const i8) };
unsafe { printf("\n\0".as_ptr() as *const i8) };
if (original.len() != owned.len()) {
Expand Down
4 changes: 2 additions & 2 deletions test/arthm/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ pub fn min<T: Ord>(a: T, b: T) -> T {
//min_by(a,b,|a,b|a.cmp(b))
}
pub fn min_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
unsafe { printf("Preparing to call the closure!\n\0".as_ptr() as *const i8) };
unsafe { printf("Preparing to call the closure!\n\0".as_ptr() as *const core::ffi::c_char) };
let res = compare(&v1, &v2);
unsafe { printf("Called the closure!\n\0".as_ptr() as *const i8) };
unsafe { printf("Called the closure!\n\0".as_ptr() as *const core::ffi::c_char) };
match res {
Ordering::Less | Ordering::Equal => v1,
Ordering::Greater => v2,
Expand Down
12 changes: 6 additions & 6 deletions test/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::intrinsics::sqrtf32;
use core::panic::PanicInfo;
#[allow(dead_code)]
extern "C" {
fn puts(msg: *const u8);
fn puts(msg: *const core::ffi::c_char);
fn malloc(size: usize) -> *mut core::ffi::c_void;
fn free(ptr: *mut core::ffi::c_void);
fn realloc(ptr: *mut core::ffi::c_void, size: usize) -> *mut core::ffi::c_void;
Expand All @@ -20,7 +20,7 @@ fn panic(_panic: &PanicInfo<'_>) -> ! {
core::intrinsics::abort();
}
#[start]
fn start(_argc: isize, _argv: *const *const u8) -> isize {
fn start(_argc: isize, _argv: *const *const core::ffi::c_char) -> isize {
main();
// 'All OK!' message
let msg = 0x00_21_4B_4F_20_6C_6C_41_i64;
Expand Down Expand Up @@ -243,13 +243,13 @@ impl Put for f32 {}
impl Put for f64 {}
fn println(msg: &str) {
unsafe {
let tmp = malloc(msg.len() + 1) as *mut u8;
let tmp_slice: &mut [u8] = core::slice::from_raw_parts_mut(tmp, msg.len() + 1);
let tmp = malloc(msg.len() + 1) as *mut core::ffi::c_char;
let tmp_slice: &mut [core::ffi::c_char] = core::slice::from_raw_parts_mut(tmp, msg.len() + 1);
tmp_slice[..msg.len()].clone_from_slice(msg.as_bytes());
tmp_slice[msg.len()] = b'\0';
printf(
"%s\n\0".as_ptr() as *const i8,
tmp_slice.as_ptr() as *const i8,
"%s\n\0".as_ptr() as *const core::ffi::c_char,
tmp_slice.as_ptr() as *const core::ffi::c_char,
);
free(tmp as *mut core::ffi::c_void);
}
Expand Down
4 changes: 2 additions & 2 deletions test/fuzz/fail11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,12 +484,12 @@ fn fn6() {

unsafe {
printf(
"offset of fld0:%p. size of fld0:%x\n\0".as_ptr() as *const i8,
"offset of fld0:%p. size of fld0:%x\n\0".as_ptr() as *const core::ffi::c_char,
core::ptr::addr_of!(tmp5.fld0) as usize - core::ptr::addr_of!(tmp5) as usize,
core::mem::size_of_val(&tmp5.fld0) as u32,
);
printf(
"offset of fld1:%p. size of fld1:%x\n\0".as_ptr() as *const i8,
"offset of fld1:%p. size of fld1:%x\n\0".as_ptr() as *const core::ffi::c_char,
core::ptr::addr_of!(tmp5.fld1) as usize - core::ptr::addr_of!(tmp5) as usize,
core::mem::size_of_val(&tmp5.fld1) as u32,
);
Expand Down
2 changes: 1 addition & 1 deletion test/intrinsics/caller_location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn main() {
}
unsafe {
printf(
"%s:%d:%d\n\0".as_ptr() as *const i8,
"%s:%d:%d\n\0".as_ptr() as *const core::ffi::c_char,
fcopy,
cloc.line(),
cloc.column(),
Expand Down
6 changes: 3 additions & 3 deletions test/std/arg_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static ARGC: AtomicIsize = AtomicIsize::new(0);
static ARGV: AtomicPtr<*const u8> = AtomicPtr::new(ptr::null_mut());
extern "C" {
fn fork() -> i32;
static mut environ: *mut *mut i8;
static mut environ: *mut *mut core::ffi::c_char;
}
unsafe fn really_init(argc: isize, argv: *const *const u8) {
// These don't need to be ordered with each other or other stores,
Expand Down Expand Up @@ -62,7 +62,7 @@ static ARGV_INIT_ARRAY: extern "C" fn(core::ffi::c_int, *const *const u8, *const
};

#[no_mangle]
fn load_environ() -> *mut *mut i8 {
fn load_environ() -> *mut *mut core::ffi::c_char {
unsafe { environ }
}
fn main() {
Expand All @@ -76,7 +76,7 @@ fn main() {
let fresh0 = i;
i = i + 1;
printf(
b"%s\n\0" as *const u8 as *const i8,
b"%s\n\0" as *const u8 as *const core::ffi::c_char,
*environ.offset(fresh0 as isize),
);
}
Expand Down
Loading
Loading