Skip to content

Commit

Permalink
Moved some more code away from TMPLocals
Browse files Browse the repository at this point in the history
  • Loading branch information
FractalFir committed Oct 31, 2024
1 parent c1a3806 commit bd2d711
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 120 deletions.
27 changes: 27 additions & 0 deletions cilly/src/cil_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,33 @@ pub enum CILNode {
}

impl CILNode {
pub fn ovf_check_tuple(
asm: &mut Assembly,
tuple: ClassRefIdx,
out_of_range: Self,
val: Self,
tpe: Type,
) -> CILNode {
let item2 = asm.alloc_string("Item2");
let item1 = asm.alloc_string("Item1");
CILNode::TemporaryLocal(Box::new((
asm.alloc_type(tuple),
[
CILRoot::SetField {
addr: Box::new(CILNode::LoadAddresOfTMPLocal),
value: Box::new(out_of_range),
desc: asm.alloc_field(crate::FieldDesc::new(tuple, item2, Type::Bool)),
},
CILRoot::SetField {
addr: Box::new(CILNode::LoadAddresOfTMPLocal),
value: Box::new(val),
desc: asm.alloc_field(crate::FieldDesc::new(tuple, item1, tpe)),
},
]
.into(),
CILNode::LoadTMPLocal,
)))
}
pub fn create_slice(
slice_tpe: ClassRefIdx,

Expand Down
48 changes: 0 additions & 48 deletions cilly/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,54 +67,6 @@ impl Method {
.nodes()
.any(|node| matches!(node, CILNode::LDLocA(loc) if *loc == local))
}
/// For a `local`, returns the *values* of all its assigements. This *will not include the root, only the nodes*!
/// WARNING: this ONLY works on "finalized" CIL, and DOES NOT SUPPORT SUBTREES
pub fn local_sets(&self, local: u32) -> impl Iterator<Item = &CILNode> {
{
let this = &self;
this.blocks()
.iter()
.flat_map(|block| block.iter_tree_roots())
}
.filter_map(move |root| match root {
CILRoot::STLoc { local: loc, tree } if (*loc == local) => Some(tree),
_ => None,
})
}
pub fn direct_set_count(&self, local: u32) -> usize {
self.local_sets(local).count()
}
pub fn const_opt_pass(&mut self) {
use crate::cil_iter_mut::CILIterMutTrait;
// If a local is set only once, and its address is never taken, it is likely to be const
// TODO: this is inefficient Consider checking all locals at once?
let luo = LocalUsageInfo::from_method(self);
let locals_address_not_taken: Box<[_]> = (0..(self.locals().len()))
.filter(|idx| !luo.is_address_taken(*idx))
.collect();

for local in locals_address_not_taken {
let sets = self.local_sets(local as u32);
if let Some(val) = all_evals_identical(sets) {
let mut tmp: Vec<_> = self
.blocks
.iter_mut()
.flat_map(|block| block.all_trees_mut())
.map(|tree| tree.root_mut())
.collect();

tmp.iter_mut()
.flat_map(|tree| tree.deref_mut().into_iter().nodes())
.for_each(|node| match node {
CILNode::LDLoc(loc) if *loc == local as u32 => *node = val.clone(),
CILNode::LDLocA(loc) if *loc == local as u32 => {
panic!("const propagation failed: the address of const taken")
}
_ => (),
});
}
}
}

/// Iterates over each `CILNode` and `CILRoot`.
pub fn iter_cil(&self) -> impl Iterator<Item = CILIterElem> {
Expand Down
21 changes: 1 addition & 20 deletions src/binop/checked/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,7 @@ use rustc_middle::ty::{IntTy, Ty, TyKind, UintTy};

pub fn result_tuple(tpe: Type, out_of_range: CILNode, val: CILNode, asm: &mut Assembly) -> CILNode {
let tuple = crate::r#type::simple_tuple(&[tpe, Type::Bool], asm);
let item2 = asm.alloc_string("Item2");
let item1 = asm.alloc_string("Item1");
CILNode::TemporaryLocal(Box::new((
asm.alloc_type(tuple),
[
CILRoot::SetField {
addr: Box::new(CILNode::LoadAddresOfTMPLocal),
value: Box::new(out_of_range),
desc: asm.alloc_field(FieldDesc::new(tuple, item2, Type::Bool)),
},
CILRoot::SetField {
addr: Box::new(CILNode::LoadAddresOfTMPLocal),
value: Box::new(val),
desc: asm.alloc_field(FieldDesc::new(tuple, item1, tpe)),
},
]
.into(),
CILNode::LoadTMPLocal,
)))
//CILNode::T
CILNode::ovf_check_tuple(asm, tuple, out_of_range, val, tpe)
}
pub fn zero(ty: Ty, asm: &mut Assembly) -> CILNode {
match ty.kind() {
Expand Down
47 changes: 14 additions & 33 deletions src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,18 +238,12 @@ fn load_const_scalar<'tcx>(
if matches!(scalar_type, Type::Ptr(_) | Type::FnPtr(_)) {
return load_scalar_ptr(ctx, ptr).cast_ptr(scalar_type);
}
let uint8_ptr = ctx.nptr(Type::Int(Int::U8));
return CILNode::LdObj {
obj: Box::new(scalar_type),
ptr: Box::new(CILNode::TemporaryLocal(Box::new((
ctx.alloc_type(uint8_ptr),
[CILRoot::SetTMPLocal {
value: load_scalar_ptr(ctx, ptr),
}]
.into(),
CILNode::LoadAddresOfTMPLocal.cast_ptr(ctx.nptr(scalar_type)),
)))),
};
return CILNode::transmute_on_stack(
load_scalar_ptr(ctx, ptr),
ctx.nptr(Type::Int(Int::U8)),
scalar_type,
ctx,
);
}
};

Expand All @@ -265,30 +259,17 @@ fn load_const_scalar<'tcx>(
TyKind::Tuple(elements) => {
if elements.is_empty() {
let scalar_ptr = ctx.nptr(scalar_type);
CILNode::TemporaryLocal(Box::new((
ctx.alloc_type(scalar_ptr),
[].into(),
CILNode::LdObj {
ptr: CILNode::LoadTMPLocal.into(),
obj: Type::Void.into(),
},
)))
CILNode::uninit_val(scalar_ptr, ctx)
} else {
CILNode::LdObj {
ptr: Box::new(
CILNode::PointerToConstValue(Box::new(scalar_u128))
.cast_ptr(ctx.nptr(scalar_type)),
),
obj: scalar_type.into(),
}
CILNode::V2(ctx.alloc_node(scalar_u128)).transmute_on_stack(
Type::Int(Int::U128),
scalar_type,
ctx,
)
}
}
TyKind::Adt(_, _) | TyKind::Closure(_, _) => CILNode::LdObj {
ptr: Box::new(
CILNode::PointerToConstValue(Box::new(scalar_u128)).cast_ptr(ctx.nptr(scalar_type)),
),
obj: scalar_type.into(),
},
TyKind::Adt(_, _) | TyKind::Closure(_, _) => CILNode::V2(ctx.alloc_node(scalar_u128))
.transmute_on_stack(Type::Int(Int::U128), scalar_type, ctx),
TyKind::Char => CILNode::V2(ctx.alloc_node(u32::try_from(scalar_u128).unwrap())),
_ => todo!("Can't load scalar constants of type {scalar_ty:?}!"),
}
Expand Down
1 change: 0 additions & 1 deletion src/place/adress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::{
use cilly::{
call,
cil_node::CILNode,
cil_root::CILRoot,
conv_usize, ld_field,
v2::{cilnode::MethodKind, FieldDesc, Int, MethodRef},
Const, IntoAsmIndex, Type,
Expand Down
21 changes: 3 additions & 18 deletions src/rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,31 +506,16 @@ fn ptr_to_ptr<'tcx>(
let target_fat = pointer_to_is_fat(*target_pointed_to, ctx.tcx(), ctx.instance());
match (src_fat, target_fat) {
(true, true) => {
let parrent = handle_operand(operand, ctx);

let target_ptr = ctx.nptr(target_type);
let tmp = CILNode::TemporaryLocal(Box::new((
ctx.alloc_type(source_type),
[CILRoot::SetTMPLocal { value: parrent }].into(),
Box::new(CILNode::LoadAddresOfTMPLocal).cast_ptr(target_ptr),
)));
crate::place::deref_op(crate::place::PlaceTy::Ty(target), ctx, tmp)
CILNode::transmute_on_stack(handle_operand(operand, ctx), source_type, target_type, ctx)
}
(true, false) => {
let field_desc = FieldDesc::new(
get_type(source, ctx).as_class_ref().unwrap(),
ctx.alloc_string(crate::DATA_PTR),
ctx.nptr(cilly::v2::Type::Void),
);
CILNode::TemporaryLocal(Box::new((
ctx.alloc_type(source_type),
[CILRoot::SetTMPLocal {
value: handle_operand(operand, ctx),
}]
.into(),
ld_field!(CILNode::LoadAddresOfTMPLocal, ctx.alloc_field(field_desc))
.cast_ptr(target_type),
)))
ld_field!(operand_address(operand, ctx), ctx.alloc_field(field_desc))
.cast_ptr(target_type)
}
(false, true) => {
panic!("ERROR: a non-unsizing cast turned a sized ptr into an unsized one")
Expand Down

0 comments on commit bd2d711

Please sign in to comment.