Skip to content

Commit

Permalink
Moved adress slice operations to use CILNode::create_slice instead of…
Browse files Browse the repository at this point in the history
… TMP locals
  • Loading branch information
FractalFir committed Oct 30, 2024
1 parent 577ea65 commit 21fc9f5
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 113 deletions.
3 changes: 0 additions & 3 deletions cilly/src/bin/linker/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,13 @@ pub fn call_alias(
) => {
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(_),
) => {

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
33 changes: 33 additions & 0 deletions cilly/src/cil_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,39 @@ pub enum CILNode {
}

impl CILNode {
pub fn create_slice(
slice_tpe: ClassRefIdx,
asm: &mut Assembly,
metadata: Self,
ptr: Self,
) -> Self {
let metadata_name = asm.alloc_string(crate::METADATA);
let metadata_field = asm.alloc_field(crate::FieldDesc::new(
slice_tpe,
metadata_name,
Type::Int(Int::USize),
));
let data_ptr_name = asm.alloc_string(crate::DATA_PTR);
let void_ptr = asm.nptr(Type::Void);
let ptr_field = asm.alloc_field(crate::FieldDesc::new(slice_tpe, data_ptr_name, void_ptr));
CILNode::TemporaryLocal(Box::new((
asm.alloc_type(Type::ClassRef(slice_tpe)),
[
CILRoot::SetField {
addr: Box::new(CILNode::LoadAddresOfTMPLocal),
value: Box::new(metadata),
desc: (metadata_field),
},
CILRoot::SetField {
addr: Box::new(CILNode::LoadAddresOfTMPLocal),
value: Box::new(ptr),
desc: ptr_field,
},
]
.into(),
CILNode::LoadTMPLocal,
)))
}
pub fn const_u128(value: u128, asm: &mut Assembly) -> CILNode {
CILNode::V2(asm.alloc_node(value))
}
Expand Down
6 changes: 6 additions & 0 deletions cilly/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ pub mod method;
pub mod utilis;
pub mod v2;
pub use v2::*;
/// The metadata of a slice
pub const METADATA: &str = "m";
/// The data pointer of a slice
pub const DATA_PTR: &str = "d";
/// The tag of an enum
pub const ENUM_TAG: &str = "v";
#[macro_export]
macro_rules! config {
($name:ident,bool,$default:expr) => {
Expand Down
7 changes: 1 addition & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,7 @@ pub extern "Rust" fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
std::alloc::set_alloc_error_hook(alloc_erorr_hook::custom_alloc_error_hook);
Box::new(MyBackend)
}
/// The metadata of a slice
const METADATA: &str = "m";
/// The data pointer of a slice
const DATA_PTR: &str = "d";
/// The tag of an enum
const ENUM_TAG: &str = "v";
pub use cilly::{DATA_PTR, ENUM_TAG, METADATA};
/*
Compiler test flags, used to skip tests which cause crashes.
Expand Down
86 changes: 16 additions & 70 deletions src/place/adress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,30 +158,9 @@ fn field_address<'a>(
let obj_addr = ld_field!(addr_calc.clone(),addr_descr);
let metadata_descr = ctx.alloc_field(FieldDesc::new(curr_type_fat_ptr.as_class_ref().unwrap(),metadata_name,Type::Int(Int::USize)));
let metadata = ld_field!(addr_calc,metadata_descr);
let field_fat_ptr = ctx.type_from_cache(Ty::new_ptr(
ctx.tcx(),
field_ty,
rustc_middle::ty::Mutability::Mut,
));
CILNode::TemporaryLocal(Box::new((
ctx.alloc_type(field_fat_ptr),
[
CILRoot::SetField {
addr: Box::new(CILNode::LoadAddresOfTMPLocal),
value: Box::new(obj_addr
+ CILNode::V2(ctx.alloc_node(Const::USize(u64::from(offset))))),
desc: ctx.alloc_field(FieldDesc::new(field_fat_ptr.as_class_ref().unwrap(),data_ptr_name,void_ptr)),
},
CILRoot::SetField {
addr: Box::new(CILNode::LoadAddresOfTMPLocal),
value: Box::new(metadata
),
desc: ctx.alloc_field(FieldDesc::new(field_fat_ptr.as_class_ref().unwrap(),metadata_name,Type::Int(Int::USize),)),
},
]
.into(),
CILNode::LoadTMPLocal,
)))
let ptr =obj_addr
+ CILNode::V2(ctx.alloc_node(Const::USize(u64::from(offset))));
CILNode::create_slice(curr_type_fat_ptr.as_class_ref().unwrap(), ctx, metadata, ptr)
}
}
}
Expand Down Expand Up @@ -264,56 +243,23 @@ pub fn place_elem_adress<'tcx>(
let data_ptr_name = ctx.alloc_string(crate::DATA_PTR);
let void_ptr = ctx.nptr(Type::Void);
let ptr_field = ctx.alloc_field(FieldDesc::new(curr_type, data_ptr_name, void_ptr));
CILNode::TemporaryLocal(Box::new((
ctx.alloc_type(Type::ClassRef(curr_type)),
[
CILRoot::SetField {
addr: Box::new(CILNode::LoadAddresOfTMPLocal),
value: Box::new(CILNode::Sub(
Box::new(ld_field!(addr_calc.clone(), metadata_field)),
Box::new(CILNode::V2(ctx.alloc_node(Const::USize(*to + 1)))),
)),
desc: (metadata_field),
},
CILRoot::SetField {
addr: Box::new(CILNode::LoadAddresOfTMPLocal),
value: Box::new(
ld_field!(addr_calc, ptr_field)
+ CILNode::V2(ctx.alloc_node(Const::USize(*from))),
),
desc: ptr_field,
},
]
.into(),
CILNode::LoadTMPLocal,
)))
let metadata = CILNode::Sub(
Box::new(ld_field!(addr_calc.clone(), metadata_field)),
Box::new(CILNode::V2(ctx.alloc_node(Const::USize(*to + 1)))),
);
let data_ptr = ld_field!(addr_calc, ptr_field)
+ CILNode::V2(ctx.alloc_node(Const::USize(*from)));
CILNode::create_slice(curr_type, ctx, metadata, data_ptr)
} else {
let void_ptr = ctx.nptr(Type::Void);
let data_ptr = ctx.alloc_string(crate::DATA_PTR);
let metadata = ctx.alloc_string(crate::METADATA);
let metadata_field =
ctx.alloc_field(FieldDesc::new(curr_type, metadata, Type::Int(Int::USize)));

let ptr_field = ctx.alloc_field(FieldDesc::new(curr_type, data_ptr, void_ptr));
CILNode::TemporaryLocal(Box::new((
ctx.alloc_type(Type::ClassRef(curr_type)),
[
CILRoot::SetField {
addr: Box::new(CILNode::LoadAddresOfTMPLocal),
value: Box::new(CILNode::V2(ctx.alloc_node(Const::USize(to - from)))),
desc: (metadata_field),
},
CILRoot::SetField {
addr: Box::new(CILNode::LoadAddresOfTMPLocal),
value: Box::new(
ld_field!(addr_calc, ptr_field)
+ CILNode::V2(ctx.alloc_node(Const::USize(*from))),
),
desc: ptr_field,
},
]
.into(),
CILNode::LoadTMPLocal,
)))
let metadata = CILNode::V2(ctx.alloc_node(Const::USize(to - from)));
let data_ptr = ld_field!(addr_calc, ptr_field)
+ CILNode::V2(ctx.alloc_node(Const::USize(*from)));

CILNode::create_slice(curr_type, ctx, metadata, data_ptr)
}
}
PlaceElem::ConstantIndex {
Expand Down
9 changes: 5 additions & 4 deletions src/rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ pub fn handle_rvalue<'tcx>(
) => todo!("Dyn star casts unspoorted"),
}
}
const SIMPLE_REPEAT_CAP: u64 = 16;
fn repeat<'tcx>(
rvalue: &Rvalue<'tcx>,
ctx: &mut MethodCompileCtx<'tcx, '_>,
Expand Down Expand Up @@ -408,8 +409,8 @@ fn repeat<'tcx>(
};
return (vec![init], place_get(target_location, ctx));
}
// Check if there are more than 16 elements. If so, use mecmpy to accelerate initialzation
if times > 16 {
// Check if there are more than SIMPLE_REPEAT_CAP elements. If so, use mecmpy to accelerate initialzation
if times > SIMPLE_REPEAT_CAP {
let place_address = place_adress(target_location, ctx);
let mut branches = Vec::new();
let arr_ref = ctx.nref(array);
Expand All @@ -421,7 +422,7 @@ fn repeat<'tcx>(
vec![].into(),
);
let mref = ctx.alloc_methodref(mref);
for idx in 0..16 {
for idx in 0..SIMPLE_REPEAT_CAP {
branches.push(CILRoot::Call {
site: mref,
args: [
Expand All @@ -432,7 +433,7 @@ fn repeat<'tcx>(
.into(),
});
}
let mut curr_len = 16;
let mut curr_len = SIMPLE_REPEAT_CAP;
while curr_len < times {
// Copy curr_len elements if possible, otherwise this is the last iteration, so copy the reminder.
let curr_copy_size = curr_len.min(times - curr_len);
Expand Down
4 changes: 2 additions & 2 deletions src/terminator/intrinsics/atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::{
utilis::field_descrptor,
};
use cilly::{
call, cil_node::CILNode, cil_root::CILRoot, cilnode::MethodKind, conv_usize,
v2::ClassRef, Int, MethodRef, Type,
call, cil_node::CILNode, cil_root::CILRoot, cilnode::MethodKind, conv_usize, v2::ClassRef, Int,
MethodRef, Type,
};
use rustc_middle::{
mir::{Operand, Place},
Expand Down
7 changes: 1 addition & 6 deletions src/terminator/intrinsics/mem.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
use crate::{assembly::MethodCompileCtx, operand::handle_operand, place::place_set};
use cilly::{
cil_node::CILNode,
cil_root::CILRoot,
conv_usize, eq,
Int, IntoAsmIndex, Type,
};
use cilly::{cil_node::CILNode, cil_root::CILRoot, conv_usize, eq, Int, IntoAsmIndex, Type};
use rustc_middle::{
mir::{Operand, Place},
ty::Instance,
Expand Down
11 changes: 4 additions & 7 deletions src/terminator/intrinsics/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use crate::{
assembly::MethodCompileCtx,
operand::handle_operand,
place::place_set,
};
use crate::{assembly::MethodCompileCtx, operand::handle_operand, place::place_set};
use cilly::{
call,
cil_node::CILNode,
cil_root::CILRoot,
cilnode::MethodKind, conv_i16, conv_i32, conv_i64, conv_i8, conv_isize, conv_u16, conv_u32,
conv_u64, conv_u8, conv_usize,
cilnode::MethodKind,
conv_i16, conv_i32, conv_i64, conv_i8, conv_isize, conv_u16, conv_u32, conv_u64, conv_u8,
conv_usize,
v2::{ClassRef, Float, Int},
Const, IntoAsmIndex, MethodRef, Type,
};
Expand Down
5 changes: 1 addition & 4 deletions src/terminator/intrinsics/ptr.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use crate::{assembly::MethodCompileCtx, operand::handle_operand, place::place_set};
use cilly::{
cil_node::CILNode,
cil_root::CILRoot,
conv_isize, conv_usize,
Int, IntoAsmIndex, Type,
cil_node::CILNode, cil_root::CILRoot, conv_isize, conv_usize, Int, IntoAsmIndex, Type,
};
use rustc_middle::{
mir::{Operand, Place},
Expand Down
14 changes: 3 additions & 11 deletions src/terminator/intrinsics/tpe.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
use crate::{assembly::MethodCompileCtx, place::place_set};
use cilly::{
call, call_virt,
cil_node::CILNode,
cil_root::CILRoot,
cilnode::MethodKind,
conv_u32,
v2::ClassRef,
Int, MethodRef, Type,
};
use rustc_middle::{
mir::Place,
ty::Instance,
call, call_virt, cil_node::CILNode, cil_root::CILRoot, cilnode::MethodKind, conv_u32,
v2::ClassRef, Int, MethodRef, Type,
};
use rustc_middle::{mir::Place, ty::Instance};
pub fn type_id<'tcx>(
destination: &Place<'tcx>,
call_instance: Instance<'tcx>,
Expand Down

0 comments on commit 21fc9f5

Please sign in to comment.