Skip to content

Commit 4aee0c1

Browse files
committed
A lot of fixes to the C_MODE
1 parent 77ef069 commit 4aee0c1

File tree

14 files changed

+658
-286
lines changed

14 files changed

+658
-286
lines changed

bin/tester.rs

Lines changed: 100 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,90 @@
33
use core::str;
44
use std::collections::HashSet;
55
use std::path::Path;
6+
#[macro_export]
7+
macro_rules! config {
8+
($name:ident,bool,$default:expr) => {
9+
pub static $name: std::sync::LazyLock<bool> = std::sync::LazyLock::new(|| {
10+
std::env::vars()
11+
.find_map(|(key, value)| {
12+
if key == stringify!($name) {
13+
Some(value)
14+
} else {
15+
None
16+
}
17+
})
18+
.map(|value| match value.as_ref() {
19+
"0" | "false" | "False" | "FALSE" => false,
20+
"1" | "true" | "True" | "TRUE" => true,
21+
_ => panic!(
22+
"Boolean enviroment variable {} has invalid value {}",
23+
stringify!($name),
24+
value
25+
),
26+
})
27+
.unwrap_or($default)
28+
});
29+
};
30+
($name:ident,bool,$default:expr,$comment:literal) => {
31+
#[doc = $comment]
32+
pub static $name: std::sync::LazyLock<bool> = std::sync::LazyLock::new(|| {
33+
std::env::vars()
34+
.find_map(|(key, value)| {
35+
if key == stringify!($name) {
36+
Some(value)
37+
} else {
38+
None
39+
}
40+
})
41+
.map(|value| match value.as_ref() {
42+
"0" | "false" | "False" | "FALSE" => false,
43+
"1" | "true" | "True" | "TRUE" => true,
44+
_ => panic!(
45+
"Boolean enviroment variable {} has invalid value {}",
46+
stringify!($name),
47+
value
48+
),
49+
})
50+
.unwrap_or($default)
51+
});
52+
};
53+
}
54+
config!(C_MODE, bool, false);
655
fn get_test_list(exec_path: &String) -> Vec<String> {
7-
let mut cmd = std::process::Command::new("dotnet");
8-
cmd.arg(exec_path.clone());
9-
cmd.arg("--list");
10-
let out = cmd.output().unwrap();
11-
let stdout = std::str::from_utf8(&out.stdout).unwrap();
12-
stdout
13-
.split('\n')
14-
.map(|name| {
15-
name.trim()
16-
.rsplit_once(':')
17-
.unwrap_or(("", ""))
18-
.0
19-
.to_owned()
20-
})
21-
.filter(|name| !name.is_empty())
22-
.collect()
56+
if *C_MODE {
57+
let mut cmd = std::process::Command::new(exec_path);
58+
cmd.arg("--list");
59+
let out = cmd.output().unwrap();
60+
let stdout = std::str::from_utf8(&out.stdout).unwrap();
61+
stdout
62+
.split('\n')
63+
.map(|name| {
64+
name.trim()
65+
.rsplit_once(':')
66+
.unwrap_or(("", ""))
67+
.0
68+
.to_owned()
69+
})
70+
.filter(|name| !name.is_empty())
71+
.collect()
72+
} else {
73+
let mut cmd = std::process::Command::new("dotnet");
74+
cmd.arg(exec_path.clone());
75+
cmd.arg("--list");
76+
let out = cmd.output().unwrap();
77+
let stdout = std::str::from_utf8(&out.stdout).unwrap();
78+
stdout
79+
.split('\n')
80+
.map(|name| {
81+
name.trim()
82+
.rsplit_once(':')
83+
.unwrap_or(("", ""))
84+
.0
85+
.to_owned()
86+
})
87+
.filter(|name| !name.is_empty())
88+
.collect()
89+
}
2390
}
2491

2592
fn run_test(
@@ -33,7 +100,10 @@ fn run_test(
33100
cmd.arg("-k");
34101
cmd.arg("20");
35102
cmd.arg("20");
36-
cmd.arg("dotnet");
103+
if !*C_MODE {
104+
cmd.arg("dotnet");
105+
}
106+
37107
cmd.arg(exec_path);
38108
cmd.arg(test_name);
39109
let out = cmd.output().unwrap();
@@ -52,7 +122,12 @@ fn run_test(
52122
}
53123
}
54124
fn successes_from_disk(exec_name: &str) -> Vec<String> {
55-
let Ok(file) = std::fs::File::open(format!("success_{exec_name}.txt")) else {
125+
let name = if *C_MODE {
126+
format!("c_success_{exec_name}.txt")
127+
} else {
128+
format!("success_{exec_name}.txt")
129+
};
130+
let Ok(file) = std::fs::File::open(name) else {
56131
return Vec::default();
57132
};
58133
use std::io::BufRead;
@@ -63,8 +138,13 @@ fn successes_from_disk(exec_name: &str) -> Vec<String> {
63138
}
64139
fn successes_to_disk(successes: &[String], exec_name: &str) {
65140
use std::io::Write;
66-
std::fs::File::create(format!("success_{exec_name}.txt"))
67-
.unwrap()
141+
let name = if *C_MODE {
142+
format!("c_success_{exec_name}.txt")
143+
} else {
144+
format!("success_{exec_name}.txt")
145+
};
146+
std::fs::File::create(&name)
147+
.unwrap_or_else(|err| panic!("file {name} could not be created due to {err:?}"))
68148
.write_all(
69149
successes
70150
.iter()

cilly/src/bin/linker/main.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,23 @@ fn main() {
246246
final_assembly.new_methodref(marshal, "FreeHGlobal", sig, MethodKind::Static, []);
247247
let mref = final_assembly[allochglobal].clone();
248248
call_alias(&mut overrides, &mut final_assembly, "free", mref);
249+
} else {
250+
let void_ptr = final_assembly.nptr(Type::Void);
251+
let sig = final_assembly.sig(
252+
[void_ptr, void_ptr, void_ptr, void_ptr],
253+
Type::Int(Int::I32),
254+
);
255+
let main_module = final_assembly.main_module();
256+
let allochglobal = final_assembly.new_methodref(
257+
*main_module,
258+
"pthread_create_wrapper",
259+
sig,
260+
MethodKind::Static,
261+
[],
262+
);
263+
let mref = final_assembly[allochglobal].clone();
264+
externs.insert("pthread_create_wrapper", LIBC.clone());
265+
call_alias(&mut overrides, &mut final_assembly, "pthread_create", mref);
249266
}
250267
if !*PANIC_MANAGED_BT {
251268
overrides.insert(
@@ -372,11 +389,44 @@ fn main() {
372389
externs.insert("_mm_malloc", LIBC.clone());
373390
externs.insert("_mm_free", LIBC.clone());
374391
externs.insert("abort", LIBC.clone());
392+
for fnc in [
393+
"pthread_getattr_np",
394+
"pthread_attr_getguardsize",
395+
"pthread_attr_getstack",
396+
"pthread_attr_destroy",
397+
"pthread_self",
398+
"pthread_create",
399+
"pthread_detach",
400+
"pthread_attr_setstacksize",
401+
"pthread_attr_init",
402+
"pthread_setname_np",
403+
"pthread_key_create",
404+
"pthread_key_delete",
405+
"pthread_join",
406+
"pthread_setspecific",
407+
] {
408+
externs.insert(fnc, LIBC.clone());
409+
}
410+
overrides.insert(
411+
final_assembly.alloc_string("argc_argv_init"),
412+
Box::new(|_, asm| {
413+
let blocks = vec![BasicBlock::new(
414+
vec![asm.alloc_root(CILRoot::VoidRet)],
415+
0,
416+
None,
417+
)];
418+
MethodImpl::MethodBody {
419+
blocks,
420+
locals: vec![],
421+
}
422+
}),
423+
);
375424
} else {
376425
cilly::v2::builtins::instert_threading(&mut final_assembly, &mut overrides);
377426
cilly::v2::builtins::math::math(&mut final_assembly, &mut overrides);
378427
cilly::v2::builtins::simd::simd(&mut final_assembly, &mut overrides);
379428
cilly::v2::builtins::insert_exception(&mut final_assembly, &mut overrides);
429+
cilly::v2::builtins::argc_argv_init(&mut final_assembly, &mut overrides);
380430
}
381431

382432
// Ensure the cctor and tcctor exist!

cilly/src/bin/linker/patch.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use cilly::{
2+
cilnode::PtrCastRes,
23
v2::{asm::MissingMethodPatcher, Assembly, CILNode, CILRoot, Int, MethodRef, Type},
34
IString,
45
};
@@ -32,6 +33,7 @@ pub fn call_alias(
3233
// Transmute to a pointer
3334
let ptr_address = asm.alloc_node(CILNode::LdArgA(arg as u32));
3435
let tpe = asm.alloc_type(*target_type);
36+
let ptr_address = asm.alloc_node(CILNode::PtrCast(ptr_address, Box::new(PtrCastRes::Ptr(tpe))));
3537
asm.alloc_node(CILNode::LdInd {
3638
addr: ptr_address,
3739
tpe,
@@ -57,6 +59,20 @@ pub fn call_alias(
5759
let src = asm.alloc_node(CILNode::LdArg(arg as u32));
5860
asm.alloc_node(CILNode::IntCast { input: src, target:*int, extend:cilly::cilnode::ExtendKind::ZeroExtend })
5961
},
62+
(
63+
Type::Ptr(dst) ,
64+
Type::Ptr(_) | Type::Int(Int::ISize | Int::USize) | Type::FnPtr(_) ,
65+
)=>{
66+
let arg = asm.alloc_node(CILNode::LdArg(arg as u32));
67+
asm.alloc_node(CILNode::PtrCast(arg, Box::new(PtrCastRes::Ptr(*dst))))
68+
}
69+
(
70+
Type::FnPtr(dst) ,
71+
Type::Ptr(_) | Type::Int(Int::ISize | Int::USize) | Type::FnPtr(_) ,
72+
)=>{
73+
let arg = asm.alloc_node(CILNode::LdArg(arg as u32));
74+
asm.alloc_node(CILNode::PtrCast(arg, Box::new(PtrCastRes::FnPtr(*dst))))
75+
}
6076
(
6177
Type::Ptr(_) | Type::Int(Int::ISize | Int::USize) | Type::FnPtr(_),
6278
Type::Ptr(_) | Type::Int(Int::ISize | Int::USize) | Type::FnPtr(_),

0 commit comments

Comments
 (0)