Skip to content

Commit ba83619

Browse files
committed
Numerous C-mode fixes
1 parent 71d1cfa commit ba83619

File tree

8 files changed

+184
-121
lines changed

8 files changed

+184
-121
lines changed

bin/tester.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ fn get_test_list(exec_path: &String) -> Vec<String> {
5858
cmd.arg("--list");
5959
let out = cmd.output().unwrap();
6060
let stdout = std::str::from_utf8(&out.stdout).unwrap();
61+
assert!(stdout.contains("tests"));
6162
stdout
6263
.split('\n')
6364
.map(|name| {

cilly/src/bin/linker/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ fn main() {
425425
cilly::v2::builtins::stack_addr(&mut final_assembly, &mut overrides);
426426
cilly::v2::builtins::transmute(&mut final_assembly, &mut overrides);
427427
cilly::v2::builtins::create_slice(&mut final_assembly, &mut overrides);
428-
428+
cilly::v2::builtins::math::bitreverse(&mut final_assembly, &mut overrides);
429429
if *C_MODE {
430430
cilly::v2::builtins::insert_exeception_stub(&mut final_assembly, &mut overrides);
431431
externs.insert("__dso_handle", LIBC.clone());
@@ -447,6 +447,7 @@ fn main() {
447447
"pthread_key_delete",
448448
"pthread_join",
449449
"pthread_setspecific",
450+
"ldexpf"
450451
] {
451452
externs.insert(fnc, LIBC.clone());
452453
}

cilly/src/v2/builtins/math.rs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ pub fn math(asm: &mut Assembly, patcher: &mut MissingMethodPatcher) {
148148
sinh(asm, patcher);
149149
coshf(asm, patcher);
150150
cosh(asm, patcher);
151+
152+
}
153+
pub fn bitreverse(asm: &mut Assembly, patcher: &mut MissingMethodPatcher){
151154
bitreverse_u32(asm, patcher);
152155
bitreverse_u64(asm, patcher);
153156
bitreverse_u128(asm, patcher);
@@ -216,7 +219,7 @@ fn bitreverse_u64(asm: &mut Assembly, patcher: &mut MissingMethodPatcher) {
216219
let inv_mask = asm.alloc_node(Const::U64(!masks[i]));
217220
let masked = asm.alloc_node(CILNode::BinOp(curr, mask, BinOp::And));
218221
let inv_masked = asm.alloc_node(CILNode::BinOp(curr, inv_mask, BinOp::And));
219-
let shift_ammount = asm.alloc_node(Const::I64(shift));
222+
let shift_ammount = asm.alloc_node(Const::I32(shift));
220223
let masked_shifted =
221224
asm.alloc_node(CILNode::BinOp(masked, shift_ammount, BinOp::ShrUn));
222225
let inv_masked_shifted =
@@ -290,29 +293,13 @@ fn bitreverse_u128(asm: &mut Assembly, patcher: &mut MissingMethodPatcher) {
290293
];
291294
while shift > 0 {
292295
let curr_mask = masks[i];
293-
let mask = CILNode::Call(Box::new((
294-
u128_ctor,
295-
([
296-
asm.alloc_node(Const::U64((curr_mask >> 64) as u64)),
297-
asm.alloc_node(Const::U64(curr_mask as u64)),
298-
])
299-
.into(),
300-
)));
301-
let mask = asm.alloc_node(mask);
296+
let mask = asm.alloc_node(Const::U128(curr_mask));
302297
let curr_mask = !masks[i];
303-
let inv_mask = CILNode::Call(Box::new((
304-
u128_ctor,
305-
([
306-
asm.alloc_node(Const::U64((curr_mask >> 64) as u64)),
307-
asm.alloc_node(Const::U64(curr_mask as u64)),
308-
])
309-
.into(),
310-
)));
311-
let inv_mask = asm.alloc_node(inv_mask);
298+
let inv_mask = asm.alloc_node(Const::U128(curr_mask));
312299
let masked = asm.alloc_node(CILNode::Call(Box::new((and, [curr, mask].into()))));
313300
let inv_masked =
314301
asm.alloc_node(CILNode::Call(Box::new((and, [curr, inv_mask].into()))));
315-
let shift_ammount = asm.alloc_node(Const::I64(shift));
302+
let shift_ammount = asm.alloc_node(Const::I32(shift));
316303
let masked_shifted = asm.alloc_node(CILNode::Call(Box::new((
317304
rshift,
318305
[masked, shift_ammount].into(),

cilly/src/v2/builtins/mod.rs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -175,17 +175,24 @@ fn insert_rust_realloc(asm: &mut Assembly, patcher: &mut MissingMethodPatcher, u
175175
Box::new(super::cilnode::PtrCastRes::Ptr(void_idx)),
176176
));
177177
let align = asm.alloc_node(CILNode::LdArg(2));
178-
let new_size = asm.alloc_node(CILNode::LdArg(3));
178+
179179
let align = asm.alloc_node(CILNode::IntCast {
180180
input: align,
181181
target: Int::USize,
182182
extend: super::cilnode::ExtendKind::ZeroExtend,
183183
});
184+
let new_size = asm.alloc_node(CILNode::LdArg(3));
184185
let new_size = asm.alloc_node(CILNode::IntCast {
185186
input: new_size,
186187
target: Int::USize,
187188
extend: super::cilnode::ExtendKind::ZeroExtend,
188189
});
190+
let old_size = asm.alloc_node(CILNode::LdArg(1));
191+
let old_size = asm.alloc_node(CILNode::IntCast {
192+
input: old_size,
193+
target: Int::USize,
194+
extend: super::cilnode::ExtendKind::ZeroExtend,
195+
});
189196
let void_ptr = asm.nptr(Type::Void);
190197
let mm_malloc_sig = asm.sig([Type::Int(Int::USize), Type::Int(Int::USize)], void_ptr);
191198
// 1. call _mm_malloc
@@ -205,7 +212,7 @@ fn insert_rust_realloc(asm: &mut Assembly, patcher: &mut MissingMethodPatcher, u
205212
let call_mm_malloc = asm.alloc_root(CILRoot::StLoc(0, _mm_malloc));
206213
// 2. memcpy the buffer.
207214
let buff = asm.alloc_node(CILNode::LdLoc(0));
208-
let copy = asm.alloc_root(CILRoot::CpBlk(Box::new((buff, ptr, new_size))));
215+
let copy = asm.alloc_root(CILRoot::CpBlk(Box::new((buff, ptr, old_size))));
209216
// 3. free the old buffer
210217
let aligned_free = asm.alloc_string("_mm_free");
211218
let mm_free_sig = asm.sig([void_ptr], Type::Void);
@@ -545,20 +552,37 @@ pub fn transmute(asm: &mut Assembly, patcher: &mut MissingMethodPatcher) {
545552
let name = asm.alloc_string("transmute");
546553
let generator = move |mref: MethodRefIdx, asm: &mut Assembly| {
547554
let target = *asm[asm[mref].sig()].output();
555+
let source = asm[asm[mref].sig()].inputs()[0];
556+
let source = asm.alloc_type(source);
548557
let target_idx = asm.alloc_type(target);
549558
let addr = asm.alloc_node(CILNode::LdArgA(0));
550-
let ptr = asm.alloc_node(CILNode::RefToPtr(addr));
551-
let ptr = asm.alloc_node(CILNode::PtrCast(ptr, Box::new(PtrCastRes::Ptr(target_idx))));
552-
let valuetype = asm.alloc_node(CILNode::LdInd {
553-
addr: ptr,
554-
tpe: target_idx,
555-
volatile: false,
556-
});
557-
let ret = asm.alloc_root(CILRoot::Ret(valuetype));
558-
MethodImpl::MethodBody {
559-
blocks: vec![BasicBlock::new(vec![ret], 0, None)],
560-
locals: vec![],
559+
if asm.alignof_type(source) >= asm.alignof_type(target_idx){
560+
let ptr = asm.alloc_node(CILNode::RefToPtr(addr));
561+
let ptr = asm.alloc_node(CILNode::PtrCast(ptr, Box::new(PtrCastRes::Ptr(target_idx))));
562+
let valuetype = asm.alloc_node(CILNode::LdInd {
563+
addr: ptr,
564+
tpe: target_idx,
565+
volatile: false,
566+
});
567+
let ret = asm.alloc_root(CILRoot::Ret(valuetype));
568+
MethodImpl::MethodBody {
569+
blocks: vec![BasicBlock::new(vec![ret], 0, None)],
570+
locals: vec![],
571+
}
572+
}else{
573+
let dst = asm.alloc_node(CILNode::LdLocA(0));
574+
let size = asm.alloc_node(CILNode::SizeOf(source));
575+
let load = asm.alloc_root(CILRoot::CpBlk(Box::new((dst,addr,size))));
576+
let ret = asm.alloc_node(CILNode::LdLoc(0));
577+
let ret = asm.alloc_root(CILRoot::Ret(ret));
578+
MethodImpl::MethodBody {
579+
blocks: vec![BasicBlock::new(vec![load,ret], 0, None)],
580+
locals: vec![(None, target_idx)],
581+
}
582+
561583
}
584+
585+
562586
};
563587
patcher.insert(name, Box::new(generator));
564588
}

0 commit comments

Comments
 (0)