Skip to content

Commit

Permalink
Fixes to the typechecking code, and aligement / sizeof NullOps.
Browse files Browse the repository at this point in the history
  • Loading branch information
FractalFir committed Oct 30, 2024
1 parent df480e0 commit bc86d05
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 16 deletions.
25 changes: 19 additions & 6 deletions cilly/src/bin/linker/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ pub fn call_alias(
.iter()
.zip(original_inputs.iter())
.enumerate()
.map(|(arg, (tpe, original_tpe))| {
if tpe == original_tpe {
.map(|(arg, (target_type, original_tpe))| {
if target_type == original_tpe {
asm.alloc_node(CILNode::LdArg(arg as u32))
} else {
match (tpe, original_tpe) {
match (target_type, original_tpe) {
(
Type::Ptr(_) | Type::Int(Int::ISize | Int::USize) | Type::FnPtr(_),
Type::ClassRef(_),
) => {
// Transmute to a pointer
let ptr_address = asm.alloc_node(CILNode::LdArgA(arg as u32));
let tpe = asm.alloc_type(*tpe);
let tpe = asm.alloc_type(*target_type);
asm.alloc_node(CILNode::LdInd {
addr: ptr_address,
tpe,
Expand All @@ -50,11 +50,24 @@ pub fn call_alias(
extend: cilly::v2::cilnode::ExtendKind::ZeroExtend,
})
}
(
Type::Int(int @ (Int::ISize | Int::USize)),
Type::Int(Int::ISize | Int::USize)
) => {
let src = asm.alloc_node(CILNode::LdArg(arg as u32));
asm.alloc_node(CILNode::IntCast { input: src, target:*int, extend:cilly::cilnode::ExtendKind::ZeroExtend })

},
(
Type::Ptr(_) | Type::Int(Int::ISize | Int::USize) | Type::FnPtr(_),
Type::Ptr(_) | Type::Int(Int::ISize | Int::USize) | Type::FnPtr(_),
) | (Type::Int(Int::I64),Type::Int(Int::U64)) => asm.alloc_node(CILNode::LdArg(arg as u32)),
_ => todo!("can't auto convert {original_tpe:?} to {tpe:?} when autogenrating wrappers."),
) => {

asm.alloc_node(CILNode::LdArg(arg as u32))
},

(Type::Int(Int::I64),Type::Int(Int::U64)) => asm.alloc_node(CILNode::LdArg(arg as u32)),
_ => todo!("can't auto convert {original_tpe:?} to {target_type:?} when autogenrating wrappers."),
}
}
})
Expand Down
4 changes: 2 additions & 2 deletions cilly/src/v2/builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,10 @@ pub fn insert_exception(asm: &mut Assembly, patcher: &mut MissingMethodPatcher)
false,
0,
extends,
vec![(Type::Int(Int::USize), data_pointer, Some(0))],
vec![(Type::Int(Int::USize), data_pointer, None)],
vec![],
Access::Public,
Some(NonZeroU32::new(8).unwrap()),
None,
None,
));
let ctor = asm.alloc_string(".ctor");
Expand Down
7 changes: 5 additions & 2 deletions cilly/src/v2/c_exporter/c_header.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,10 @@ uint64_t System_UInt64_RotateLeftu8i4u8(uint64_t val, int32_t ammount)
fprintf(stderr, "Can't System_UInt64_RotateLeftu8i4u8 yet.\n");
abort();
}

unsigned __int128 System_UInt128_RotateRightu16i4u16(unsigned __int128 val, int32_t amount){
fprintf(stderr, "Can't System_UInt128_RotateRightu16i4u16 yet.\n");
abort();
}

uint8_t System_Byte_RotateLeftu1i4u1(uint8_t val, int32_t ammount)
{
Expand All @@ -380,7 +383,7 @@ uint8_t System_Byte_RotateLeftu1i4u1(uint8_t val, int32_t ammount)
}
unsigned __int128 System_UInt128_LeadingZeroCountu16u16(unsigned __int128 val)
{
fprintf(stderr, "Can't System_Byte_RotateLeftu1i4u1 yet.\n");
fprintf(stderr, "Can't System_UInt128_LeadingZeroCountu16u16 yet.\n");
abort();
}
uint32_t System_Math_Minu4u4u4(uint32_t lhs, uint32_t rhs)
Expand Down
2 changes: 1 addition & 1 deletion cilly/src/v2/c_exporter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn c_tpe(field_tpe: Type, asm: &Assembly) -> String {
super::Float::F16 => todo!(),
super::Float::F32 => "float".into(),
super::Float::F64 => "double".into(),
super::Float::F128 => todo!(),
super::Float::F128 => "_Float128".into(),
},
Type::PlatformString => "char*".into(),
Type::PlatformChar => "char".into(),
Expand Down
1 change: 1 addition & 0 deletions cilly/src/v2/tpe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ impl Type {
&& cref.asm().map(|s| asm[s].as_ref()) == Some("System.Runtime")
&& &asm[cref.name()] == "System.UInt128"
}
(Type::Int(Int::U16 | Int::I16), Type::PlatformChar) => true,
(Type::Ptr(ptr), Type::Ref(rf)) => ptr == rf,
// TODO: check generics propely?
(_, Type::PlatformGeneric(_, _)) => true,
Expand Down
14 changes: 13 additions & 1 deletion cilly/src/v2/typecheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ pub enum TypeCheckError {
got: Type,
expected: Type,
},
ValueTypeCompare {
lhs: Type,
rhs: Type,
},
}
pub fn typecheck_err_to_string(
root_idx: super::RootIdx,
Expand Down Expand Up @@ -201,7 +205,15 @@ impl BinOp {
},
BinOp::Eq => {
if lhs == rhs || lhs.is_assignable_to(rhs, asm) {
Ok(Type::Bool)
if let Type::ClassRef(cref) = lhs {
if asm[cref].is_valuetype() {
Err(TypeCheckError::ValueTypeCompare { lhs, rhs })
} else {
Ok(Type::Bool)
}
} else {
Ok(Type::Bool)
}
} else {
Err(TypeCheckError::WrongBinopArgs {
lhs,
Expand Down
4 changes: 2 additions & 2 deletions src/assembly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ fn allocation_initializer_method(
local: 0,
tree: Box::new(call!(
asm.alloc_methodref(alloc),
[conv_isize!(CILNode::V2(asm.alloc_node(
i32::try_from(bytes.len()).expect("Static alloc too big")
[CILNode::V2(asm.alloc_node(Const::ISize(
i64::try_from(bytes.len()).expect("Static alloc too big")
)))]
))
.cast_ptr(asm.nptr(Type::Int(Int::U8))),
Expand Down
4 changes: 2 additions & 2 deletions src/rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ pub fn handle_rvalue<'tcx>(
}
NullOp::AlignOf => {
let algin = crate::utilis::align_of(ctx.monomorphize(*ty), ctx.tcx());
CILNode::V2(ctx.alloc_node(algin))
CILNode::V2(ctx.alloc_node(Const::USize(algin)))
}
NullOp::OffsetOf(fields) => {
let layout = ctx.layout_of(*ty);
let offset = ctx
.tcx()
.offset_of_subfield(ParamEnv::reveal_all(), layout, fields.iter())
.bytes();
CILNode::V2(ctx.alloc_node(offset))
CILNode::V2(ctx.alloc_node(Const::USize(offset)))
}
rustc_middle::mir::NullOp::UbChecks => {
let ub_checks = ctx.tcx().sess.ub_checks();
Expand Down

0 comments on commit bc86d05

Please sign in to comment.