Skip to content

Commit df51028

Browse files
committed
Bugfixes
1 parent 8915f26 commit df51028

File tree

8 files changed

+84
-12
lines changed

8 files changed

+84
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ The project is still very early in development, but it has made significant prog
5555

5656
## Milestones
5757
- [X] Draft version of the `mycorrhiza` interop layer, capable of creating managed objects, and calling methods.
58-
- [ ] Compiling the `core` Rust crate - *Substantial chunk of core now compiles, with 32 methods having trouble compiling. The compilation still gets terminated early due to errors*
58+
- [ ] Compiling the `core` Rust crate - *Substantial chunk of core now compiles, with many methods still having trouble compiling. The compilation still gets terminated early due to errors*
5959
- [ ] Compiling the `alloc` Rust crate
6060
- [ ] Compiling the `std` Rust crate
6161
- [ ] Stack unwinding

src/assembly.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,26 @@ impl Assembly {
152152
}
153153
}
154154
}
155+
/// This is used *ONLY* to catch uncaught errors.
156+
fn checked_add_fn<'tcx>(&mut self,
157+
instance: Instance<'tcx>,
158+
tcx: TyCtxt<'tcx>,
159+
name: &str)-> Result<(), MethodCodegenError>{
160+
match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
161+
self.add_fn(instance, tcx, name)
162+
})) {
163+
Ok(success) => success,
164+
Err(payload) => {
165+
if let Some(msg) = payload.downcast_ref::<&str>() {
166+
eprintln!("fn_add panicked with unhandled message: {msg:?}");
167+
return Ok(());
168+
} else {
169+
eprintln!("fn_add panicked with no message.");
170+
return Ok(());
171+
}
172+
}
173+
}
174+
}
155175
//fn terminator_to_ops()
156176
/// Adds a rust MIR function to the assembly.
157177
pub fn add_fn<'tcx>(
@@ -313,7 +333,7 @@ impl Assembly {
313333
//let instance = crate::utilis::monomorphize(&instance,tcx);
314334
let symbol_name = crate::utilis::function_name(item.symbol_name(tcx));
315335

316-
self.add_fn(instance, tcx, &symbol_name)
336+
self.checked_add_fn(instance, tcx, &symbol_name)
317337
.expect("Could not add function!");
318338

319339
Ok(())

src/constant.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,40 @@ fn create_const_adt_from_bytes<'ctx>(
6161
creator_ops.push(CILOp::FreeTMPLocal);
6262
creator_ops
6363
}
64-
AdtKind::Enum => todo!("Can't load const enum from bytes {bytes:?}!"),
64+
AdtKind::Enum => {
65+
let variant_size = crate::utilis::enum_tag_size(adt_def.variants().len() as u64);
66+
let mut curr_offset = 0;
67+
let enum_ty = Type::from_ty(ty, tyctx, &method_instance);
68+
let enum_dotnet:DotnetTypeRef = if let Type::DotnetType(ty_ref) = &enum_ty{
69+
ty_ref.as_ref().clone()
70+
}
71+
else{
72+
panic!("Invalid enum type {enum_ty:?}");
73+
};
74+
let mut ops = vec![CILOp::NewTMPLocal(enum_ty.into())];
75+
let curr_variant = match variant_size{
76+
0=>todo!("Can't yet handle constant enums with 0-sized tags."),
77+
1=>{
78+
curr_offset = 1;
79+
let variant = bytes[0] as u32;
80+
ops.extend([CILOp::LoadAddresOfTMPLocal,CILOp::LdcI32(variant as i32),CILOp::STField(FieldDescriptor::boxed(
81+
enum_dotnet.clone(),
82+
Type::U8,
83+
"_tag".into(),
84+
))]);
85+
variant
86+
}
87+
_=>todo!("Can't yet support enums with {variant_size} wide tags."),
88+
};
89+
assert!(curr_variant < adt_def.variants().len() as u32);
90+
let active_variant = &adt_def.variants()[curr_variant.into()];
91+
for field in &active_variant.fields{
92+
todo!("Can't yet create const enum fields.");
93+
}
94+
ops.push(CILOp::FreeTMPLocal);
95+
//todo!("Can't load const enum from bytes {bytes:?}!");
96+
ops
97+
}
6598
AdtKind::Union => todo!("Can't load const union from bytes {bytes:?}!"),
6699
}
67100
}

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ const INSERT_MIR_DEBUG_COMMENTS: bool = false;
5858
const PRINT_LOCAL_TYPES: bool = false;
5959
const PRINT_TY_CONVERTION: bool = false;
6060
/// Tells the codegen to optmize the emiited CIL.
61-
const OPTIMIZE_CIL: bool = (!INSERT_MIR_DEBUG_COMMENTS) && (true);
61+
const OPTIMIZE_CIL: bool = (!INSERT_MIR_DEBUG_COMMENTS) && (false);
62+
/// Turns on the struct spliting optimzation.
63+
const SPLIT_LOCAL_STRUCTS:bool = false;
6264

6365
/// Changes `.locals` into `.locals init`. Causes the runtime to always initialize local variables.
6466
/// Try turining on in cause of issues. If it fixes them, then their root cause is UB(eg. use of uninitailized memory).

src/opt/mod.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ pub fn opt_method(method: &mut Method, asm: &Assembly) {
133133
remove_zombie_sets(method.ops_mut());
134134
method.ops_mut().retain(|op| *op != CILOp::Nop);
135135
try_alias_locals(method.ops_mut());
136-
try_split_locals(method, asm);
136+
if crate::SPLIT_LOCAL_STRUCTS{
137+
try_split_locals(method, asm);
138+
}
137139
remove_unused_locals(method);
138140
try_inline_all(method, asm);
139141
}
@@ -637,6 +639,10 @@ fn try_split_locals(method: &mut Method, asm: &Assembly) {
637639
.1
638640
.as_dotnet()
639641
.expect("Can't spilt non-dotnet types!");
642+
if dotnet_tpe.name_path().contains("PtrRepr"){
643+
eprintln!("WARINING: PtrRepr is bugged and causes issues during optimzation. It will not be optimized. TODO: figure out why the field `const_ptr` can't be found.");
644+
continue;
645+
}
640646
let type_def = asm.get_typedef_by_path(dotnet_tpe.name_path());
641647
let type_def = if let Some(type_def) = type_def {
642648
type_def
@@ -668,28 +674,37 @@ fn try_split_locals(method: &mut Method, asm: &Assembly) {
668674
continue;
669675
}
670676
if let CILOp::LDField(field_desc) = op2 {
671-
let field_idx = morphic_fields
677+
let field_idx = if let Some(field) = morphic_fields
672678
.iter()
673679
.position(|mfield| mfield.0 == field_desc.name())
674-
.expect("Cound not find field during spliting!");
680+
{field}
681+
else{
682+
panic!("ERROR: field spliting failed because field {field_desc:?} could not be found. This may be caused by an error during codegen");
683+
};
675684
method.ops_mut()[index] = CILOp::Nop;
676685
method.ops_mut()[index + 1] = CILOp::LDLoc((field_idx + local_map_start) as u32);
677686
continue;
678687
}
679688
if let CILOp::LDFieldAdress(field_desc) = op2 {
680-
let field_idx = morphic_fields
689+
let field_idx = if let Some(field) = morphic_fields
681690
.iter()
682691
.position(|mfield| mfield.0 == field_desc.name())
683-
.expect("Cound not find field during spliting!");
692+
{field}
693+
else{
694+
panic!("ERROR: field spliting failed because field {field_desc:?} could not be found. This may be caused by an error during codegen");
695+
};
684696
method.ops_mut()[index] = CILOp::Nop;
685697
method.ops_mut()[index + 1] = CILOp::LDLocA((field_idx + local_map_start) as u32);
686698
continue;
687699
}
688700
if let CILOp::STField(field_desc) = op3 {
689-
let field_idx = morphic_fields
701+
let field_idx = if let Some(field) = morphic_fields
690702
.iter()
691703
.position(|mfield| mfield.0 == field_desc.name())
692-
.expect("Cound not find field during spliting!");
704+
{field}
705+
else{
706+
panic!("ERROR: field spliting failed because field {field_desc:?} could not be found. This may be caused by an error during codegen");
707+
};
693708
method.ops_mut()[index] = CILOp::Nop;
694709
method.ops_mut()[index + 2] = CILOp::STLoc((field_idx + local_map_start) as u32);
695710
continue;

src/type.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ impl Type {
430430
Self::FnDef(call)
431431
}
432432
TyKind::Array(element, length) => {
433+
//let length = crate::utilis::monomorphize(method, length, tyctx)
433434
let length = crate::utilis::try_resolve_const_size(length).unwrap();
434435

435436
let element = Type::from_ty(*element, tyctx, method);

src/type_def.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ impl TypeDef {
233233
)];
234234
let mut explicit_offsets = vec![0];
235235
let tag_size = enum_tag_size(adt_def.variants().len() as u64);
236-
assert_ne!(tag_size, 0, "ERROR:{name} has a zero sized tag!");
236+
//assert_ne!(tag_size, 0, "ERROR:{name} has a zero sized tag!");
237237
explicit_offsets.extend(adt_def.variants().iter().map(|_| tag_size));
238238
let mut inner_types = vec![];
239239
for variant in adt_def.variants() {

src/utilis.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ pub fn tag_from_enum_variants(variants: u64) -> crate::r#type::Type {
108108
let var_size = enum_tag_size(variants);
109109
println!("variants:{variants}tag_size:{var_size}");
110110
match var_size {
111+
0 => Type::Void,
111112
1 => Type::U8,
112113
2 => Type::U16,
113114
4 => Type::U32,

0 commit comments

Comments
 (0)