Skip to content

Commit bb3676b

Browse files
committed
Extended typechecks, fixed a type bug when creating aggregate thin pointers.
1 parent b257a16 commit bb3676b

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

cilly/src/v2/cilroot.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,11 @@ impl CILRoot {
680680
locals: &[(Option<StringIdx>, TypeIdx)],
681681
) -> String {
682682
match self {
683+
Self::StInd(boxed) => {
684+
let (addr, val, tpe, is_volitile) = boxed.as_ref();
685+
let tpe = tpe.mangle(asm);
686+
format!("StInd{{addr:{addr:?},val:{val:?},tpe:{tpe},is_volitile:{is_volitile}}}")
687+
}
683688
Self::StLoc(loc, val) => match locals.get(*loc as usize) {
684689
Some((Some(name), tpe)) => format!(
685690
"StLoc({loc}: {loc_tpe:?} {name:?}, {val:?})",
@@ -688,7 +693,7 @@ impl CILRoot {
688693
),
689694
Some((None, tpe)) => format!(
690695
"StLoc({loc}: {loc_tpe},{val:?})",
691-
loc_tpe = asm[*tpe].clone().mangle(asm)
696+
loc_tpe = asm[*tpe].clone().mangle(asm),
692697
),
693698
None => format!("{self:?}"),
694699
},

cilly/src/v2/typecheck.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ pub enum TypeCheckError {
101101
lhs: Type,
102102
rhs: Type,
103103
},
104+
WriteWrongAddr {
105+
addr: Type,
106+
tpe: Type,
107+
},
108+
WriteWrongValue {
109+
tpe: Type,
110+
value: Type,
111+
},
104112
}
105113
pub fn typecheck_err_to_string(
106114
root_idx: super::RootIdx,
@@ -122,8 +130,8 @@ pub fn typecheck_err_to_string(
122130
.collect();
123131
let root_string = root.display(asm, sig, locals);
124132
match root.typecheck(sig, locals, asm){
125-
Ok(ok)=> format!("digraph G{{edge [dir=\"back\"];\n{nodes} r{root_idx} [label = \"{root_string:?}\" color = \"green\"] r{root_idx} ->{root_connections}}}",root_idx = root_idx.as_bimap_index()),
126-
Err(err)=> format!("digraph G{{edge [dir=\"back\"];\\n{nodes} r{root_idx} [label = \"{root_string:?}\n{err:?}\" color = \"red\"] r{root_idx} ->{root_connections}}}",root_idx = root_idx.as_bimap_index()),
133+
Ok(ok)=> format!("digraph G{{edge [dir=\"back\"];\n{nodes} r{root_idx} [label = \"{root_string}\" color = \"green\"] r{root_idx} ->{root_connections}}}",root_idx = root_idx.as_bimap_index()),
134+
Err(err)=> format!("digraph G{{edge [dir=\"back\"];\\n{nodes} r{root_idx} [label = \"{root_string}\n{err:?}\" color = \"red\"] r{root_idx} ->{root_connections}}}",root_idx = root_idx.as_bimap_index()),
127135
}
128136
}
129137
pub fn display_typecheck_err(
@@ -856,6 +864,33 @@ impl CILRoot {
856864
Ok(())
857865
}
858866
}
867+
Self::StInd(boxed) => {
868+
let (addr, value, tpe, _) = boxed.as_ref();
869+
let addr = asm[*addr].clone().typecheck(sig, locals, asm)?;
870+
let value = asm[*value].clone().typecheck(sig, locals, asm)?;
871+
let Some(addr_points_to) = addr.pointed_to().map(|tpe| asm[tpe]) else {
872+
return Err(TypeCheckError::WriteWrongAddr { addr, tpe: *tpe });
873+
};
874+
if !(tpe.is_assignable_to(addr_points_to, asm)
875+
|| addr_points_to
876+
.as_int()
877+
.zip(tpe.as_int())
878+
.is_some_and(|(a, b)| a.as_unsigned() == b.as_unsigned())
879+
|| addr_points_to == Type::Bool && *tpe == Type::Int(Int::I8))
880+
{
881+
return Err(TypeCheckError::WriteWrongAddr { addr, tpe: *tpe });
882+
}
883+
if !(value.is_assignable_to(*tpe, asm)
884+
|| value
885+
.as_int()
886+
.zip(tpe.as_int())
887+
.is_some_and(|(a, b)| a.as_unsigned() == b.as_unsigned())
888+
|| value == Type::Bool && *tpe == Type::Int(Int::I8))
889+
{
890+
return Err(TypeCheckError::WriteWrongValue { tpe: *tpe, value });
891+
}
892+
Ok(())
893+
}
859894
_ => {
860895
for node in self.nodes() {
861896
asm.get_node(*node).clone().typecheck(sig, locals, asm)?;

src/aggregate.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
assembly::MethodCompileCtx,
33
operand::handle_operand,
4-
place::place_get,
4+
place::{place_get, place_set},
55
r#type::{get_type, pointer_to_is_fat},
66
utilis::{adt::set_discr, field_name},
77
};
@@ -220,14 +220,14 @@ pub fn handle_aggregate<'tcx>(
220220
"data_ty:{data_ty:?} is a zst. That is bizzare, cause it should be a pointer?"
221221
);
222222
let data_type = ctx.type_from_cache(data_ty);
223-
let fat_ptr_type_ptr = ctx.nptr(fat_ptr_type);
223+
let ptr_tpe = ctx.type_from_cache(pointee);
224224
assert_ne!(data_type, Type::Void);
225225
// Pointer is thin, just directly assign
226226
return CILNode::SubTrees(Box::new((
227-
[CILRoot::STIndPtr(
228-
init_addr,
229-
handle_operand(data, ctx).cast_ptr(ctx.nptr(fat_ptr_type_ptr)),
230-
Box::new(ctx.nptr(fat_ptr_type)),
227+
[place_set(
228+
target_location,
229+
handle_operand(data, ctx).cast_ptr(ctx.nptr(ptr_tpe)),
230+
ctx,
231231
)]
232232
.into(),
233233
Box::new(place_get(target_location, ctx)),

0 commit comments

Comments
 (0)