Skip to content

Commit bd2d711

Browse files
committed
Moved some more code away from TMPLocals
1 parent c1a3806 commit bd2d711

File tree

6 files changed

+45
-120
lines changed

6 files changed

+45
-120
lines changed

cilly/src/cil_node.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,33 @@ pub enum CILNode {
224224
}
225225

226226
impl CILNode {
227+
pub fn ovf_check_tuple(
228+
asm: &mut Assembly,
229+
tuple: ClassRefIdx,
230+
out_of_range: Self,
231+
val: Self,
232+
tpe: Type,
233+
) -> CILNode {
234+
let item2 = asm.alloc_string("Item2");
235+
let item1 = asm.alloc_string("Item1");
236+
CILNode::TemporaryLocal(Box::new((
237+
asm.alloc_type(tuple),
238+
[
239+
CILRoot::SetField {
240+
addr: Box::new(CILNode::LoadAddresOfTMPLocal),
241+
value: Box::new(out_of_range),
242+
desc: asm.alloc_field(crate::FieldDesc::new(tuple, item2, Type::Bool)),
243+
},
244+
CILRoot::SetField {
245+
addr: Box::new(CILNode::LoadAddresOfTMPLocal),
246+
value: Box::new(val),
247+
desc: asm.alloc_field(crate::FieldDesc::new(tuple, item1, tpe)),
248+
},
249+
]
250+
.into(),
251+
CILNode::LoadTMPLocal,
252+
)))
253+
}
227254
pub fn create_slice(
228255
slice_tpe: ClassRefIdx,
229256

cilly/src/method.rs

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -67,54 +67,6 @@ impl Method {
6767
.nodes()
6868
.any(|node| matches!(node, CILNode::LDLocA(loc) if *loc == local))
6969
}
70-
/// For a `local`, returns the *values* of all its assigements. This *will not include the root, only the nodes*!
71-
/// WARNING: this ONLY works on "finalized" CIL, and DOES NOT SUPPORT SUBTREES
72-
pub fn local_sets(&self, local: u32) -> impl Iterator<Item = &CILNode> {
73-
{
74-
let this = &self;
75-
this.blocks()
76-
.iter()
77-
.flat_map(|block| block.iter_tree_roots())
78-
}
79-
.filter_map(move |root| match root {
80-
CILRoot::STLoc { local: loc, tree } if (*loc == local) => Some(tree),
81-
_ => None,
82-
})
83-
}
84-
pub fn direct_set_count(&self, local: u32) -> usize {
85-
self.local_sets(local).count()
86-
}
87-
pub fn const_opt_pass(&mut self) {
88-
use crate::cil_iter_mut::CILIterMutTrait;
89-
// If a local is set only once, and its address is never taken, it is likely to be const
90-
// TODO: this is inefficient Consider checking all locals at once?
91-
let luo = LocalUsageInfo::from_method(self);
92-
let locals_address_not_taken: Box<[_]> = (0..(self.locals().len()))
93-
.filter(|idx| !luo.is_address_taken(*idx))
94-
.collect();
95-
96-
for local in locals_address_not_taken {
97-
let sets = self.local_sets(local as u32);
98-
if let Some(val) = all_evals_identical(sets) {
99-
let mut tmp: Vec<_> = self
100-
.blocks
101-
.iter_mut()
102-
.flat_map(|block| block.all_trees_mut())
103-
.map(|tree| tree.root_mut())
104-
.collect();
105-
106-
tmp.iter_mut()
107-
.flat_map(|tree| tree.deref_mut().into_iter().nodes())
108-
.for_each(|node| match node {
109-
CILNode::LDLoc(loc) if *loc == local as u32 => *node = val.clone(),
110-
CILNode::LDLocA(loc) if *loc == local as u32 => {
111-
panic!("const propagation failed: the address of const taken")
112-
}
113-
_ => (),
114-
});
115-
}
116-
}
117-
}
11870

11971
/// Iterates over each `CILNode` and `CILRoot`.
12072
pub fn iter_cil(&self) -> impl Iterator<Item = CILIterElem> {

src/binop/checked/mod.rs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,7 @@ use rustc_middle::ty::{IntTy, Ty, TyKind, UintTy};
1111

1212
pub fn result_tuple(tpe: Type, out_of_range: CILNode, val: CILNode, asm: &mut Assembly) -> CILNode {
1313
let tuple = crate::r#type::simple_tuple(&[tpe, Type::Bool], asm);
14-
let item2 = asm.alloc_string("Item2");
15-
let item1 = asm.alloc_string("Item1");
16-
CILNode::TemporaryLocal(Box::new((
17-
asm.alloc_type(tuple),
18-
[
19-
CILRoot::SetField {
20-
addr: Box::new(CILNode::LoadAddresOfTMPLocal),
21-
value: Box::new(out_of_range),
22-
desc: asm.alloc_field(FieldDesc::new(tuple, item2, Type::Bool)),
23-
},
24-
CILRoot::SetField {
25-
addr: Box::new(CILNode::LoadAddresOfTMPLocal),
26-
value: Box::new(val),
27-
desc: asm.alloc_field(FieldDesc::new(tuple, item1, tpe)),
28-
},
29-
]
30-
.into(),
31-
CILNode::LoadTMPLocal,
32-
)))
33-
//CILNode::T
14+
CILNode::ovf_check_tuple(asm, tuple, out_of_range, val, tpe)
3415
}
3516
pub fn zero(ty: Ty, asm: &mut Assembly) -> CILNode {
3617
match ty.kind() {

src/constant.rs

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -238,18 +238,12 @@ fn load_const_scalar<'tcx>(
238238
if matches!(scalar_type, Type::Ptr(_) | Type::FnPtr(_)) {
239239
return load_scalar_ptr(ctx, ptr).cast_ptr(scalar_type);
240240
}
241-
let uint8_ptr = ctx.nptr(Type::Int(Int::U8));
242-
return CILNode::LdObj {
243-
obj: Box::new(scalar_type),
244-
ptr: Box::new(CILNode::TemporaryLocal(Box::new((
245-
ctx.alloc_type(uint8_ptr),
246-
[CILRoot::SetTMPLocal {
247-
value: load_scalar_ptr(ctx, ptr),
248-
}]
249-
.into(),
250-
CILNode::LoadAddresOfTMPLocal.cast_ptr(ctx.nptr(scalar_type)),
251-
)))),
252-
};
241+
return CILNode::transmute_on_stack(
242+
load_scalar_ptr(ctx, ptr),
243+
ctx.nptr(Type::Int(Int::U8)),
244+
scalar_type,
245+
ctx,
246+
);
253247
}
254248
};
255249

@@ -265,30 +259,17 @@ fn load_const_scalar<'tcx>(
265259
TyKind::Tuple(elements) => {
266260
if elements.is_empty() {
267261
let scalar_ptr = ctx.nptr(scalar_type);
268-
CILNode::TemporaryLocal(Box::new((
269-
ctx.alloc_type(scalar_ptr),
270-
[].into(),
271-
CILNode::LdObj {
272-
ptr: CILNode::LoadTMPLocal.into(),
273-
obj: Type::Void.into(),
274-
},
275-
)))
262+
CILNode::uninit_val(scalar_ptr, ctx)
276263
} else {
277-
CILNode::LdObj {
278-
ptr: Box::new(
279-
CILNode::PointerToConstValue(Box::new(scalar_u128))
280-
.cast_ptr(ctx.nptr(scalar_type)),
281-
),
282-
obj: scalar_type.into(),
283-
}
264+
CILNode::V2(ctx.alloc_node(scalar_u128)).transmute_on_stack(
265+
Type::Int(Int::U128),
266+
scalar_type,
267+
ctx,
268+
)
284269
}
285270
}
286-
TyKind::Adt(_, _) | TyKind::Closure(_, _) => CILNode::LdObj {
287-
ptr: Box::new(
288-
CILNode::PointerToConstValue(Box::new(scalar_u128)).cast_ptr(ctx.nptr(scalar_type)),
289-
),
290-
obj: scalar_type.into(),
291-
},
271+
TyKind::Adt(_, _) | TyKind::Closure(_, _) => CILNode::V2(ctx.alloc_node(scalar_u128))
272+
.transmute_on_stack(Type::Int(Int::U128), scalar_type, ctx),
292273
TyKind::Char => CILNode::V2(ctx.alloc_node(u32::try_from(scalar_u128).unwrap())),
293274
_ => todo!("Can't load scalar constants of type {scalar_ty:?}!"),
294275
}

src/place/adress.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::{
77
use cilly::{
88
call,
99
cil_node::CILNode,
10-
cil_root::CILRoot,
1110
conv_usize, ld_field,
1211
v2::{cilnode::MethodKind, FieldDesc, Int, MethodRef},
1312
Const, IntoAsmIndex, Type,

src/rvalue.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -506,31 +506,16 @@ fn ptr_to_ptr<'tcx>(
506506
let target_fat = pointer_to_is_fat(*target_pointed_to, ctx.tcx(), ctx.instance());
507507
match (src_fat, target_fat) {
508508
(true, true) => {
509-
let parrent = handle_operand(operand, ctx);
510-
511-
let target_ptr = ctx.nptr(target_type);
512-
let tmp = CILNode::TemporaryLocal(Box::new((
513-
ctx.alloc_type(source_type),
514-
[CILRoot::SetTMPLocal { value: parrent }].into(),
515-
Box::new(CILNode::LoadAddresOfTMPLocal).cast_ptr(target_ptr),
516-
)));
517-
crate::place::deref_op(crate::place::PlaceTy::Ty(target), ctx, tmp)
509+
CILNode::transmute_on_stack(handle_operand(operand, ctx), source_type, target_type, ctx)
518510
}
519511
(true, false) => {
520512
let field_desc = FieldDesc::new(
521513
get_type(source, ctx).as_class_ref().unwrap(),
522514
ctx.alloc_string(crate::DATA_PTR),
523515
ctx.nptr(cilly::v2::Type::Void),
524516
);
525-
CILNode::TemporaryLocal(Box::new((
526-
ctx.alloc_type(source_type),
527-
[CILRoot::SetTMPLocal {
528-
value: handle_operand(operand, ctx),
529-
}]
530-
.into(),
531-
ld_field!(CILNode::LoadAddresOfTMPLocal, ctx.alloc_field(field_desc))
532-
.cast_ptr(target_type),
533-
)))
517+
ld_field!(operand_address(operand, ctx), ctx.alloc_field(field_desc))
518+
.cast_ptr(target_type)
534519
}
535520
(false, true) => {
536521
panic!("ERROR: a non-unsizing cast turned a sized ptr into an unsized one")

0 commit comments

Comments
 (0)