Skip to content

Commit

Permalink
Numerous bugfixes to 128 bit ints and maxstack fix
Browse files Browse the repository at this point in the history
  • Loading branch information
FractalFir committed Feb 4, 2024
1 parent 4810c44 commit 808ede2
Show file tree
Hide file tree
Showing 17 changed files with 181 additions and 64 deletions.
2 changes: 1 addition & 1 deletion AssemblyUtilis/AssemblyUtilis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>

<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

Expand Down
12 changes: 6 additions & 6 deletions src/assembly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub struct Assembly {
entrypoint: Option<CallSite>,
/// List of references to external assemblies
extern_refs: HashMap<IString, AssemblyExternRef>,
extern_fns:HashMap<(IString,FnSig),IString>,
extern_fns: HashMap<(IString, FnSig), IString>,
/// List of all static fields within the assembly
static_fields: HashMap<IString, Type>,
}
Expand Down Expand Up @@ -77,7 +77,7 @@ impl Assembly {
entrypoint: None,
extern_refs: HashMap::new(),
static_fields: HashMap::new(),
extern_fns:HashMap::new(),
extern_fns: HashMap::new(),
};
let dotnet_ver = AssemblyExternRef {
version: (6, 12, 0, 0),
Expand Down Expand Up @@ -111,7 +111,7 @@ impl Assembly {
entrypoint,
extern_refs,
static_fields,
extern_fns
extern_fns,
}
}
/// Gets the typdefef at path `path`.
Expand Down Expand Up @@ -562,12 +562,12 @@ impl Assembly {
self.entrypoint = Some(entrypoint);
}

pub fn extern_fns(&self) -> &HashMap<(IString,FnSig),IString> {
pub fn extern_fns(&self) -> &HashMap<(IString, FnSig), IString> {
&self.extern_fns
}

pub fn add_extern_fn(&mut self, name:IString,sig:FnSig,lib:IString) {
self.extern_fns.insert((name,sig),lib);
pub fn add_extern_fn(&mut self, name: IString, sig: FnSig, lib: IString) {
self.extern_fns.insert((name, sig), lib);
}
}
fn link_static_initializers(a: Option<&Method>, b: Option<&Method>) -> Option<Method> {
Expand Down
7 changes: 4 additions & 3 deletions src/assembly_exporter/ilasm_exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl AssemblyExporter for ILASMExporter {
let mut encoded_asm = Vec::with_capacity(0x1_00);
let mut methods = Vec::with_capacity(0x1_00);
write!(encoded_asm, ".assembly {asm_name}{{}}").expect("Write error!");
write!(methods, ".class RustModule{{").expect("Write error!");
write!(methods, ".class beforefieldinit RustModule{{").expect("Write error!");
Self {
encoded_asm,
methods,
Expand Down Expand Up @@ -121,7 +121,8 @@ impl AssemblyExporter for ILASMExporter {
writeln!(
self.methods,
".method private hidebysig static pinvokeimpl(\"{lib_path}\" cdecl) {output} '{name}'("
).unwrap();
)
.unwrap();
let mut input_iter = sig.inputs().iter();
if let Some(input) = input_iter.next() {
write!(self.methods, "{}", non_void_type_cil(input)).unwrap();
Expand Down Expand Up @@ -246,7 +247,7 @@ fn method_cil(w: &mut impl Write, method: &Method) -> std::io::Result<()> {
escaped_type = non_void_type_cil(&local.1)
)?;
}
writeln!(w, "\n\t)")?;
writeln!(w, "\n\t)\n.maxstack {maxstack}\n",maxstack = method.maxstack())?;
for op in method.get_ops() {
writeln!(w, "\t{op_cli}", op_cli = super::ilasm_op::op_cli(op))?;
}
Expand Down
2 changes: 1 addition & 1 deletion src/assembly_exporter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub trait AssemblyExporter: Sized {
asm_exporter.add_method(method);
}
}
for ((name,sig),lib) in asm.extern_fns(){
for ((name, sig), lib) in asm.extern_fns() {
asm_exporter.add_extern_method(lib, name, sig);
}
println!(
Expand Down
15 changes: 10 additions & 5 deletions src/bin/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ fn patch_missing_method(call_site: &cil::CallSite) -> method::Method {
call_site.name(),
vec![],
);

let ops = rustc_codegen_clr::cil::CILOp::throw_msg(&format!(
"Tried to invoke missing method {name}",
name = call_site.name()
Expand All @@ -103,16 +103,21 @@ fn autopatch(asm: &mut Assembly) {
let mut patched = std::collections::HashMap::new();
let mut externs = Vec::new();
for call in call_sites {
if call.name() == "printf"{
externs.push((call.name().into(),call.signature().to_owned(),"/lib64/libc.so.6".into()));
if call.name() == "printf" {
externs.push((
call.name().into(),
call.signature().to_owned(),
"/lib64/libc.so.6".into(),
));
continue;
}
if !patched.contains_key(call) {

patched.insert(call.clone(), patch_missing_method(call));
}
}
externs.into_iter().for_each(|(name,sig,lib)|{asm.add_extern_fn(name,sig,lib)});
externs
.into_iter()
.for_each(|(name, sig, lib)| asm.add_extern_fn(name, sig, lib));
patched
.values()
.for_each(|method| asm.add_method(method.clone()));
Expand Down
2 changes: 1 addition & 1 deletion src/binop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ fn sub_unchecked<'tyctx>(
fn ne_unchecked<'tyctx>(ty_a: Ty<'tyctx>, ty_b: Ty<'tyctx>) -> Vec<CILOp> {
vec![eq_unchecked(ty_a, ty_b), CILOp::LdcI32(0), CILOp::Eq]
}
fn eq_unchecked<'tyctx>(ty_a: Ty<'tyctx>, _ty_b: Ty<'tyctx>) -> CILOp {
pub fn eq_unchecked<'tyctx>(ty_a: Ty<'tyctx>, _ty_b: Ty<'tyctx>) -> CILOp {
//vec![CILOp::Eq]
match ty_a.kind() {
TyKind::Uint(uint) => match uint {
Expand Down
22 changes: 15 additions & 7 deletions src/casts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,22 @@ pub fn to_int(target: Type) -> Vec<CILOp> {
}
/// Returns CIL ops required to casts from intiger type `src` to `target`
pub fn int_to_float(src: Type, target: Type) -> Vec<CILOp> {
if matches!(src, Type::I128 ) {
vec![CILOp::Call(CallSite::boxed(DotnetTypeRef::int_128().into(),"op_Explicit".into(),FnSig::new(&[src],&target),true))]
if matches!(src, Type::I128) {
vec![CILOp::Call(CallSite::boxed(
DotnetTypeRef::int_128().into(),
"op_Explicit".into(),
FnSig::new(&[src], &target),
true,
))]
//todo!("Casting from 128 bit intiegers is not supported!")
}
else if matches!(src, Type::U128 ) {
vec![CILOp::Call(CallSite::boxed(DotnetTypeRef::uint_128().into(),"op_Explicit".into(),FnSig::new(&[src],&target),true))]
}
else if matches!(target, Type::I128 | Type::U128) {
} else if matches!(src, Type::U128) {
vec![CILOp::Call(CallSite::boxed(
DotnetTypeRef::uint_128().into(),
"op_Explicit".into(),
FnSig::new(&[src], &target),
true,
))]
} else if matches!(target, Type::I128 | Type::U128) {
todo!("Casting to 128 bit intiegers is not supported!")
} else {
match target {
Expand Down
4 changes: 3 additions & 1 deletion src/compile_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,8 @@ run_test! {types,slice,unstable}
run_test! {types,statics,stable}
run_test! {types,async_types,unstable}
run_test! {types,self_referential_statics,stable}
run_test! {types,int128,unstable}

run_test! {std,main,unstable}

run_test! {control_flow,cf_for,stable}
Expand All @@ -568,7 +570,7 @@ run_test! {intrinsics,type_id,stable}
run_test! {intrinsics,ptr_offset_from_unsigned,stable}

run_test! {fuzz,test0,stable}
run_test! {fuzz,fuzz0,stable}
run_test! {fuzz,fuzz0,unstable}
run_test! {fuzz,test1,stable}
cargo_test! {hello_world,stable}
cargo_test! {std_hello_world,stable}
Expand Down
Loading

0 comments on commit 808ede2

Please sign in to comment.