Skip to content

Commit

Permalink
Further work on porting th eexporter
Browse files Browse the repository at this point in the history
  • Loading branch information
FractalFir committed Jul 25, 2024
1 parent 6b0edb0 commit 16d9fd6
Show file tree
Hide file tree
Showing 24 changed files with 723 additions and 221 deletions.
8 changes: 4 additions & 4 deletions cilly/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ edge [fontname=\"Helvetica,Arial,sans-serif\"]\nnode [shape=box];\n".to_string()
pub fn sizeof_tpedef(&self, tpe: &crate::DotnetTypeRef) -> std::num::NonZeroU32 {
assert!(tpe.asm().is_none());
self.types
.get(tpe.name_path(&self.string_map))
.get(tpe.name_path())
.unwrap()
.explict_size()
.unwrap()
Expand Down Expand Up @@ -516,7 +516,7 @@ edge [fontname=\"Helvetica,Arial,sans-serif\"]\nnode [shape=box];\n".to_string()
.flat_map(Method::dotnet_types)
.filter_map(|tpe| match tpe.asm() {
Some(_) => None,
None => Some(IString::from(tpe.name_path(&self.string_map))),
None => Some(IString::from(tpe.name_path())),
})
.map(|name| (name.clone(), self.types.get(&name).unwrap().clone()))
.collect();
Expand All @@ -533,7 +533,7 @@ edge [fontname=\"Helvetica,Arial,sans-serif\"]\nnode [shape=box];\n".to_string()
})
.filter_map(|tpe| match tpe.asm() {
Some(_) => None,
None => Some(IString::from(tpe.name_path(&self.string_map))),
None => Some(IString::from(tpe.name_path())),
})
.map(|name| (name.clone(), self.types.get(&name).unwrap().clone())),
);
Expand All @@ -552,7 +552,7 @@ edge [fontname=\"Helvetica,Arial,sans-serif\"]\nnode [shape=box];\n".to_string()
.filter_map(Type::dotnet_refs)
.filter_map(|tpe| match tpe.asm() {
Some(_) => None,
None => Some(IString::from(tpe.name_path(&self.string_map))),
None => Some(IString::from(tpe.name_path())),
})
//.map(|(a,b)|a.into())
.map(|name: IString| {
Expand Down
87 changes: 54 additions & 33 deletions cilly/src/bin/linker/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use cilly::{
ilasm_exporter::ILASM_FLAVOUR,
ldc_i32,
method::{Method, MethodType},
DotnetTypeRef, FnSig, IString, IlasmFlavour, Type,
DotnetTypeRef, FnSig, IlasmFlavour, Type,
};
//use assembly::Assembly;
use lazy_static::lazy_static;
Expand Down Expand Up @@ -559,48 +559,69 @@ fn main() {
);
}
} else {
// Run ILASM
export::export_assembly(&final_assembly, output_file_path, is_lib)
.expect("Assembly export faliure!");
let path: std::path::PathBuf = output_file_path.into();
if *CILLY_V2 {
let tmp = cilly::v2::Assembly::from_v1(&final_assembly);
tmp.save_tmp(&mut std::fs::File::create(path.with_extension("cilly2")).unwrap())
.unwrap();
tmp.export(
path,
&path,
cilly::v2::il_exporter::ILExporter::new(*ILASM_FLAVOUR),
);
todo!();
}
final_assembly
.save_tmp(&mut std::fs::File::create(path.with_extension("cilly")).unwrap())
.unwrap();
// Run AOT compiler
aot_compile_mode.compile(output_file_path);
if cargo_support {
let bootstrap = bootstrap_source(&path, output_file_path, "dotnet");
let bootstrap_path = path.with_extension("rs");
let mut bootstrap_file = std::fs::File::create(&bootstrap_path).unwrap();
bootstrap_file.write_all(bootstrap.as_bytes()).unwrap();
let path = std::env::var("PATH").unwrap();
let out = std::process::Command::new("rustc")
.arg("-O")
.arg(bootstrap_path)
.arg("-o")
.arg(output_file_path)
.env_clear()
.env("PATH", path)
.output()
.unwrap();
assert!(
out.stderr.is_empty(),
"{}",
String::from_utf8(out.stderr).unwrap()
);
}
} else {
// Run ILASM
export::export_assembly(&final_assembly, output_file_path, is_lib)
.expect("Assembly export faliure!");
final_assembly
.save_tmp(&mut std::fs::File::create(path.with_extension("cilly")).unwrap())
.unwrap();
// Run AOT compiler
aot_compile_mode.compile(output_file_path);

// Cargo integration
// Cargo integration

if cargo_support {
let bootstrap = bootstrap_source(&path, output_file_path, "dotnet");
let bootstrap_path = path.with_extension("rs");
let mut bootstrap_file = std::fs::File::create(&bootstrap_path).unwrap();
bootstrap_file.write_all(bootstrap.as_bytes()).unwrap();
let path = std::env::var("PATH").unwrap();
let out = std::process::Command::new("rustc")
.arg("-O")
.arg(bootstrap_path)
.arg("-o")
.arg(output_file_path)
.env_clear()
.env("PATH", path)
.output()
.unwrap();
assert!(
out.stderr.is_empty(),
"{}",
String::from_utf8(out.stderr).unwrap()
);
if cargo_support {
let bootstrap = bootstrap_source(&path, output_file_path, "dotnet");
let bootstrap_path = path.with_extension("rs");
let mut bootstrap_file = std::fs::File::create(&bootstrap_path).unwrap();
bootstrap_file.write_all(bootstrap.as_bytes()).unwrap();
let path = std::env::var("PATH").unwrap();
let out = std::process::Command::new("rustc")
.arg("-O")
.arg(bootstrap_path)
.arg("-o")
.arg(output_file_path)
.env_clear()
.env("PATH", path)
.output()
.unwrap();
assert!(
out.stderr.is_empty(),
"{}",
String::from_utf8(out.stderr).unwrap()
);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion cilly/src/c_exporter/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ fn tree_code(
}
fn call_site_to_name(call_site: &CallSite, strings: &AsmStringContainer) -> String {
if let Some(class) = call_site.class() {
let class = escape_type_name(class.name_path(strings));
let class = escape_type_name(class.name_path());
let name = call_site.name();
let name = escape_type_name(name);
let mangled_overloads: String = call_site
Expand Down
12 changes: 4 additions & 8 deletions cilly/src/c_exporter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl AssemblyExporter for CExporter {
.filter_map(|field| field.1.as_dotnet())
.filter_map(|tpe| {
if tpe.asm().is_none() {
Some(escape_type_name(tpe.name_path(asm.string_map())))
Some(escape_type_name(tpe.name_path()))
} else {
None
}
Expand Down Expand Up @@ -349,20 +349,16 @@ fn c_tpe(tpe: &Type, string_map: &AsmStringContainer) -> Cow<'static, str> {
}
Type::DotnetType(tref) => {
if let Some(asm) = tref.asm() {
match (asm, tref.name_path(string_map)) {
match (asm, tref.name_path()) {
("System.Runtime", "System.UInt128") => return c_tpe(&Type::U128, string_map),
("System.Runtime", "System.Int128") => return c_tpe(&Type::I128, string_map),
_ => println!("Type {tref:?} is not supported in C"),
}
}
if tref.is_valuetype() {
escape_type_name(tref.name_path(string_map)).into()
escape_type_name(tref.name_path()).into()
} else {
format!(
"{name}*",
name = escape_type_name(tref.name_path(string_map))
)
.into()
format!("{name}*", name = escape_type_name(tref.name_path())).into()
}
}
Type::DelegatePtr(_sig) => "void*".into(),
Expand Down
11 changes: 10 additions & 1 deletion cilly/src/call_site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl CallSite {
)
}
#[must_use]
pub fn alloc() -> Self {
pub fn aligned_alloc() -> Self {
Self::new_extern(
DotnetTypeRef::native_mem(),
"AlignedAlloc".into(),
Expand All @@ -53,6 +53,15 @@ impl CallSite {
)
}
#[must_use]
pub fn alloc() -> Self {
Self::new_extern(
DotnetTypeRef::marshal(),
"AllocHGlobal".into(),
FnSig::new(&[Type::I32], Type::ISize),
true,
)
}
#[must_use]
pub fn realloc() -> Self {
Self::new(
Some(DotnetTypeRef::native_mem()),
Expand Down
2 changes: 1 addition & 1 deletion cilly/src/cil_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,7 @@ impl CILNode {
continue;
}
if let Type::DotnetType(dt) = arg_tpe {
if dt.name_path(vctx.strings()).contains("System.Object") {
if dt.name_path().contains("System.Object") {
continue;
}
}
Expand Down
4 changes: 2 additions & 2 deletions cilly/src/dotnet_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ impl DotnetTypeRef {
}
}
#[must_use]
pub fn name_path<'a, 'b: 'a>(&'a self, strings: &'b AsmStringContainer) -> &'a str {
pub fn name_path<'a, 'b: 'a>(&'a self) -> &'a str {
match self {
DotnetTypeRef::Full { name_path, .. } => name_path,
DotnetTypeRef::OptimizedRustStruct { name } => strings.get(*name),
DotnetTypeRef::OptimizedRustStruct { .. } => panic!(),
}
}

Expand Down
10 changes: 5 additions & 5 deletions cilly/src/ilasm_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1269,13 +1269,13 @@ pub fn type_cil(tpe: &Type, strings: &AsmStringContainer) -> Cow<'static, str> {
pub fn dotnet_type_ref_cli(dotnet_type: &DotnetTypeRef, strings: &AsmStringContainer) -> String {
let prefix = dotnet_type.tpe_prefix();
if Some("System.Runtime") == dotnet_type.asm()
&& "System.String" == dotnet_type.name_path(strings)
&& "System.String" == dotnet_type.name_path()
&& !dotnet_type.is_valuetype()
{
return "string".into();
}
if Some("System.Runtime") == dotnet_type.asm()
&& "System.Object" == dotnet_type.name_path(strings)
&& "System.Object" == dotnet_type.name_path()
&& !dotnet_type.is_valuetype()
{
return "object".into();
Expand All @@ -1286,12 +1286,12 @@ pub fn dotnet_type_ref_cli(dotnet_type: &DotnetTypeRef, strings: &AsmStringConta
String::new()
};
let name = if dotnet_type.generics().is_empty() {
dotnet_type.name_path(strings).to_string()
dotnet_type.name_path().to_string()
} else {
format!(
"{name}`{count}",
count = dotnet_type.generics().len(),
name = dotnet_type.name_path(strings).to_string()
name = dotnet_type.name_path().to_string()
)
};

Expand All @@ -1309,7 +1309,7 @@ pub fn dotnet_type_ref_extends(
} else {
String::new()
};
let name = dotnet_type.name_path(strings);
let name = dotnet_type.name_path();

let generics = generics_str(dotnet_type.generics(), strings);
format!("{asm}'{name}'{generics}")
Expand Down
2 changes: 1 addition & 1 deletion cilly/src/js_exporter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ fn node_code(method: &Method, node: &CILNode, strings: &AsmStringContainer) -> I
}
fn call_site_to_name(call_site: &CallSite, strings: &AsmStringContainer) -> String {
if let Some(class) = call_site.class() {
let class = escape_type_name(class.name_path(strings));
let class = escape_type_name(class.name_path());
let name = call_site.name();
let name = escape_type_name(name);
let mangled_overloads: String = call_site
Expand Down
2 changes: 1 addition & 1 deletion cilly/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub fn mangle(tpe: &Type, strings: &AsmStringContainer) -> std::borrow::Cow<'sta
tpe.generics().is_empty(),
"Arrays of generic .NET types not supported yet"
);
tpe.name_path(strings).replace('.', "_").into()
tpe.name_path().replace('.', "_").into()
}
Type::ManagedArray { element, dims } => {
format!("a{}{}", dims, mangle(element, strings)).into()
Expand Down
12 changes: 10 additions & 2 deletions cilly/src/utilis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub fn argc_argv_init_method(asm: &mut Assembly) -> CallSite {
};
// Alloc argv
let tree = call!(
CallSite::alloc(),
CallSite::aligned_alloc(),
[
mul!(
conv_usize!(CILNode::LDLoc(argc)),
Expand Down Expand Up @@ -450,7 +450,8 @@ pub fn get_environ(asm: &mut Assembly) -> CallSite {
init.trees_mut().push(
CILRoot::STLoc {
local: arr_ptr,
tree: call!(CallSite::alloc(), [arr_size, arr_align]).cast_ptr(ptr!(ptr!(Type::U8))),
tree: call!(CallSite::aligned_alloc(), [arr_size, arr_align])
.cast_ptr(ptr!(ptr!(Type::U8))),
}
.into(),
);
Expand Down Expand Up @@ -650,6 +651,13 @@ pub fn encode(mut int: u64) -> String {
}
res
}
/// Checks if all elements in a slice are truly unquie.
#[track_caller]
pub fn assert_unique<T: std::hash::Hash + PartialEq + Eq>(val: &[T]) {
let mut set = std::collections::HashSet::new();
set.extend(val.iter());
assert_eq!(set.len(), val.len());
}
#[test]
fn argv() {
let mut asm = Assembly::empty();
Expand Down
Loading

0 comments on commit 16d9fd6

Please sign in to comment.