Skip to content

Commit

Permalink
Moved parts of projection handling to use v2 node
Browse files Browse the repository at this point in the history
  • Loading branch information
FractalFir committed Nov 1, 2024
1 parent bd2d711 commit 4e2e8c6
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 116 deletions.
7 changes: 7 additions & 0 deletions cilly/src/cil_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,13 @@ pub enum CILNode {
}

impl CILNode {
pub fn stack_addr(val: Self, tpe: TypeIdx) -> Self {
CILNode::TemporaryLocal(Box::new((
tpe,
[CILRoot::SetTMPLocal { value: val }].into(),
CILNode::LoadAddresOfTMPLocal,
)))
}
pub fn ovf_check_tuple(
asm: &mut Assembly,
tuple: ClassRefIdx,
Expand Down
25 changes: 7 additions & 18 deletions src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,32 +146,21 @@ fn load_scalar_ptr(
MethodKind::Static,
vec![].into(),
);
return CILNode::TemporaryLocal(Box::new((
return CILNode::stack_addr(
CILNode::Call(Box::new(CallOpArgs {
args: Box::new([]),
site: ctx.alloc_methodref(mref),
})),
ctx.alloc_type(u8_ptr_ptr),
[CILRoot::SetTMPLocal {
value: CILNode::Call(Box::new(CallOpArgs {
args: Box::new([]),
site: ctx.alloc_methodref(mref),
})),
}]
.into(),
CILNode::LoadAddresOfTMPLocal,
)));
);
}
let attrs = ctx.tcx().codegen_fn_attrs(def_id);

if attrs.import_linkage.is_some() {
// TODO: this could cause issues if the pointer to the static is not imediatly dereferenced.
let site = get_fn_from_static_name(&name, ctx);
let ptr_sig = Type::FnPtr(ctx[site].sig());
return CILNode::TemporaryLocal(Box::new((
ctx.alloc_type(ptr_sig),
[CILRoot::SetTMPLocal {
value: CILNode::LDFtn(site),
}]
.into(),
CILNode::LoadAddresOfTMPLocal,
)));
return CILNode::stack_addr(CILNode::LDFtn(site), ctx.alloc_type(ptr_sig));
}
if let Some(section) = attrs.link_section {
panic!("static {name} requires special linkage in section {section:?}");
Expand Down
12 changes: 2 additions & 10 deletions src/operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ pub(crate) fn handle_operand<'tcx>(
) -> CILNode {
let res = ctx.type_from_cache(ctx.monomorphize(operand.ty(ctx.body(), ctx.tcx())));
if res == Type::Void {
return CILNode::TemporaryLocal(Box::new((
ctx.alloc_type(Type::Void),
[].into(),
CILNode::LoadTMPLocal,
)));
return CILNode::uninit_val(Type::Void, ctx);
}
match operand {
Operand::Copy(place) | Operand::Move(place) => crate::place::place_get(place, ctx),
Expand All @@ -33,11 +29,7 @@ pub(crate) fn operand_address<'tcx>(
Operand::Constant(const_val) => {
let local_type = ctx.type_from_cache(operand.ty(ctx.body(), ctx.tcx()));
let constant = crate::constant::handle_constant(const_val, ctx);
let ptr = CILNode::TemporaryLocal(Box::new((
ctx.alloc_type(local_type),
[CILRoot::SetTMPLocal { value: constant }].into(),
CILNode::LoadAddresOfTMPLocal,
)));
let ptr = CILNode::stack_addr(constant, ctx.alloc_type(local_type));
crate::place::deref_op(
crate::place::PlaceTy::Ty(operand.ty(ctx.body(), ctx.tcx())),
ctx,
Expand Down
40 changes: 23 additions & 17 deletions src/place/adress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,30 @@ use cilly::{
cil_node::CILNode,
conv_usize, ld_field,
v2::{cilnode::MethodKind, FieldDesc, Int, MethodRef},
Const, IntoAsmIndex, Type,
Assembly, Const, IntoAsmIndex, NodeIdx, Type,
};
use rustc_middle::{
mir::PlaceElem,
ty::{Ty, TyKind},
};
pub fn local_adress(local: usize, method: &rustc_middle::mir::Body) -> CILNode {
if let Some(spread_arg) = method.spread_arg
pub fn local_adress(local: usize, method: &rustc_middle::mir::Body, asm: &mut Assembly) -> NodeIdx {
let local = if let Some(spread_arg) = method.spread_arg
&& local == spread_arg.as_usize()
{
return CILNode::MRefToRawPtr(Box::new(CILNode::LDLocA(
cilly::CILNode::LdLocA(
(method.local_decls.len() - method.arg_count)
.try_into()
.unwrap(),
)));
}
if local == 0 {
CILNode::MRefToRawPtr(CILNode::LDLocA(0).into())
} else if local > method.arg_count {
CILNode::MRefToRawPtr(
CILNode::LDLocA(u32::try_from(local - method.arg_count).unwrap()).into(),
)
} else if local == 0 {
cilly::CILNode::LdLocA(0)
} else if local > method.arg_count {
cilly::CILNode::LdLocA(u32::try_from(local - method.arg_count).unwrap())
} else {
CILNode::MRefToRawPtr(CILNode::LDArgA(u32::try_from(local - 1).unwrap()).into())
}
cilly::CILNode::LdArgA(u32::try_from(local - 1).unwrap())
};
let local = asm.alloc_node(local);
asm.alloc_node(cilly::CILNode::RefToPtr(local))
}
pub fn address_last_dereference<'tcx>(
target_ty: Ty<'tcx>,
Expand Down Expand Up @@ -197,7 +196,7 @@ pub fn place_elem_adress<'tcx>(
let curr_ty = curr_type
.as_ty()
.expect("INVALID PLACE: Indexing into enum variant???");
let index = crate::place::local_get(index.as_usize(), ctx.body());
let index = crate::place::local_get(index.as_usize(), ctx.body(), ctx);
match curr_ty.kind() {
TyKind::Slice(inner) => {
let inner = ctx.monomorphize(*inner);
Expand All @@ -209,9 +208,16 @@ pub fn place_elem_adress<'tcx>(
let desc = ctx.alloc_field(FieldDesc::new(slice, data_ptr_name, void_ptr));
// This is a false positive
// #[allow(unused_parens)]
let size = ctx.size_of(inner_type);
let size = size.into_idx(ctx);
let size = ctx.alloc_node(cilly::CILNode::IntCast {
input: size,
target: Int::USize,
extend: cilly::cilnode::ExtendKind::ZeroExtend,
});
let offset = ctx.biop(index, size, cilly::BinOp::Mul);
(ld_field!(addr_calc.clone(), desc)).cast_ptr(ctx.nptr(inner_type))
+ conv_usize!(CILNode::V2(ctx.size_of(inner_type).into_idx(ctx)))
* conv_usize!(index)
+ CILNode::V2(ctx.alloc_node(offset))
}
TyKind::Array(element, _length) => {
let element = ctx.monomorphize(*element);
Expand All @@ -227,7 +233,7 @@ pub fn place_elem_adress<'tcx>(
MethodKind::Instance,
vec![].into(),
);
call!(ctx.alloc_methodref(mref), [addr_calc, index])
call!(ctx.alloc_methodref(mref), [addr_calc, CILNode::V2(index)])
}
_ => {
rustc_middle::ty::print::with_no_trimmed_paths! {todo!("Can't index into {curr_ty}!")}
Expand Down
39 changes: 22 additions & 17 deletions src/place/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ use cilly::{
cil_node::CILNode,
conv_usize, ld_field,
v2::{cilnode::MethodKind, FieldDesc, Int, MethodRef},
Const, IntoAsmIndex, Type,
Const, IntoAsmIndex, NodeIdx, Type,
};
use rustc_middle::mir::PlaceElem;
use rustc_middle::ty::{Ty, TyKind};
pub fn local_body<'tcx>(local: usize, ctx: &mut MethodCompileCtx<'tcx, '_>) -> (CILNode, Ty<'tcx>) {
pub fn local_body<'tcx>(local: usize, ctx: &mut MethodCompileCtx<'tcx, '_>) -> (NodeIdx, Ty<'tcx>) {
let ty = ctx.body().local_decls[local.into()].ty;
let ty = ctx.monomorphize(ty);
if body_ty_is_by_adress(ty, ctx) {
(super::adress::local_adress(local, ctx.body()), ty)
(super::adress::local_adress(local, ctx.body(), ctx), ty)
} else {
(super::get::local_get(local, ctx.body()), ty)
(super::get::local_get(local, ctx.body(), ctx), ty)
}
}
fn body_field<'a>(
Expand Down Expand Up @@ -132,7 +132,7 @@ pub fn place_elem_body_index<'tcx>(
parrent_node: CILNode,
index: rustc_middle::mir::Local,
) -> (PlaceTy<'tcx>, CILNode) {
let index = crate::place::local_get(index.as_usize(), ctx.body());
let index = crate::place::local_get(index.as_usize(), ctx.body(), ctx);
match curr_ty.kind() {
TyKind::Slice(inner) => {
let inner = ctx.monomorphize(*inner);
Expand All @@ -143,12 +143,17 @@ pub fn place_elem_body_index<'tcx>(
ctx.alloc_string(crate::DATA_PTR),
ctx.nptr(Type::Void),
);
let size = ctx.size_of(inner_type);
let size = size.into_idx(ctx);
let size = ctx.alloc_node(cilly::CILNode::IntCast {
input: size,
target: Int::USize,
extend: cilly::cilnode::ExtendKind::ZeroExtend,
});
let offset = ctx.biop(index, size, cilly::BinOp::Mul);
let addr = ld_field!(parrent_node, ctx.alloc_field(desc))
.cast_ptr(ctx.nptr(inner_type))
+ (index
* CILNode::ZeroExtendToUSize(
CILNode::V2(ctx.size_of(inner_type).into_idx(ctx)).into(),
));
+ CILNode::V2(ctx.alloc_node(offset));

if body_ty_is_by_adress(inner, ctx) {
(inner.into(), addr)
Expand All @@ -165,6 +170,12 @@ pub fn place_elem_body_index<'tcx>(
let array_type = ctx.type_from_cache(curr_ty);
let array_dotnet = array_type.as_class_ref().expect("Non array type");
let arr_ref = ctx.nref(array_type);
let index = ctx.alloc_node(cilly::CILNode::IntCast {
input: index,
target: Int::USize,
extend: cilly::cilnode::ExtendKind::ZeroExtend,
});
let index = CILNode::V2(index);
if body_ty_is_by_adress(element, ctx) {
let elem_ptr = ctx.nptr(element_type);
let mref = MethodRef::new(
Expand All @@ -174,10 +185,7 @@ pub fn place_elem_body_index<'tcx>(
MethodKind::Instance,
vec![].into(),
);
let ops = call!(
ctx.alloc_methodref(mref),
[parrent_node, CILNode::ZeroExtendToUSize(index.into())]
);
let ops = call!(ctx.alloc_methodref(mref), [parrent_node, index]);
((element).into(), ops)
} else {
let mref = MethodRef::new(
Expand All @@ -187,10 +195,7 @@ pub fn place_elem_body_index<'tcx>(
MethodKind::Instance,
vec![].into(),
);
let ops = call!(
ctx.alloc_methodref(mref),
[parrent_node, CILNode::ZeroExtendToUSize(index.into())]
);
let ops = call!(ctx.alloc_methodref(mref), [parrent_node, index]);
((element).into(), ops)
}
}
Expand Down
78 changes: 43 additions & 35 deletions src/place/get.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::{assembly::MethodCompileCtx, r#type::fat_ptr_to};
use cilly::{
asm::Assembly,
call,
cil_node::CILNode,
conv_usize, ld_field,
v2::{cilnode::MethodKind, FieldDesc, Int, MethodRef},
Const, IntoAsmIndex, Type,
Const, IntoAsmIndex, NodeIdx, Type,
};
use rustc_middle::{
mir::{Place, PlaceElem},
Expand All @@ -14,33 +15,41 @@ use rustc_middle::{

use super::body_ty_is_by_adress;

pub(super) fn local_get(local: usize, method: &rustc_middle::mir::Body) -> CILNode {
if let Some(spread_arg) = method.spread_arg
&& local == spread_arg.as_usize()
{
return CILNode::LDLoc(
(method.local_decls.len() - method.arg_count)
.try_into()
.unwrap(),
);
}
if local == 0 {
CILNode::LDLoc(0)
} else if local > method.arg_count {
CILNode::LDLoc(
u32::try_from(local - method.arg_count)
.expect("Method has more than 2^32 local varaibles"),
)
} else {
CILNode::LDArg(u32::try_from(local - 1).expect("Method has more than 2^32 local variables"))
}
pub(super) fn local_get(
local: usize,
method: &rustc_middle::mir::Body,
asm: &mut Assembly,
) -> NodeIdx {
asm.alloc_node(
if let Some(spread_arg) = method.spread_arg
&& local == spread_arg.as_usize()
{
cilly::CILNode::LdLoc(
(method.local_decls.len() - method.arg_count)
.try_into()
.unwrap(),
)
} else if local == 0 {
cilly::CILNode::LdLoc(0)
} else if local > method.arg_count {
cilly::CILNode::LdLoc(
u32::try_from(local - method.arg_count)
.expect("Method has more than 2^32 local varaibles"),
)
} else {
cilly::CILNode::LdArg(
u32::try_from(local - 1).expect("Method has more than 2^32 local variables"),
)
},
)
}
/// Returns the ops for getting the value of place.
pub fn place_get<'tcx>(place: &Place<'tcx>, ctx: &mut MethodCompileCtx<'tcx, '_>) -> CILNode {
if place.projection.is_empty() {
local_get(place.local.as_usize(), ctx.body())
CILNode::V2(local_get(place.local.as_usize(), ctx.body(), ctx))
} else {
let (mut op, mut ty) = super::local_body(place.local.as_usize(), ctx);
let (op, mut ty) = super::local_body(place.local.as_usize(), ctx);
let mut op = CILNode::V2(op);
ty = ctx.monomorphize(ty);
let mut ty = ty.into();

Expand Down Expand Up @@ -126,7 +135,7 @@ fn place_elem_get<'a>(
.expect("INVALID PLACE: Indexing into enum variant???");

let index_type = ctx.monomorphize(ctx.body().local_decls[*index].ty);
let index = crate::place::local_get(index.as_usize(), ctx.body());
let index = crate::place::local_get(index.as_usize(), ctx.body(), ctx);

match curr_ty.kind() {
TyKind::Slice(inner) => {
Expand All @@ -140,15 +149,17 @@ fn place_elem_get<'a>(
ctx.alloc_string(crate::DATA_PTR),
ctx.nptr(Type::Void),
);
let size = crate::casts::int_to_int(
Type::Int(Int::I32),
index_type,
CILNode::V2(ctx.size_of(inner_type).into_idx(ctx)),
ctx,
);
let size = ctx.size_of(inner_type);
let size = size.into_idx(ctx);
let size = ctx.alloc_node(cilly::CILNode::IntCast {
input: size,
target: Int::USize,
extend: cilly::cilnode::ExtendKind::ZeroExtend,
});
let offset = ctx.biop(index, size, cilly::BinOp::Mul);
let addr = (ld_field!(addr_calc, ctx.alloc_field(desc))
.cast_ptr(ctx.nptr(inner_type)))
+ (index * size);
+ CILNode::V2(ctx.alloc_node(offset));
super::deref_op(super::PlaceTy::Ty(inner), ctx, addr)
}
TyKind::Array(element, _length) => {
Expand All @@ -164,10 +175,7 @@ fn place_elem_get<'a>(
MethodKind::Instance,
vec![].into(),
);
call!(
ctx.alloc_methodref(mref),
[addr_calc, CILNode::ZeroExtendToUSize(index.into())]
)
call!(ctx.alloc_methodref(mref), [addr_calc, CILNode::V2(index)])
}
_ => {
rustc_middle::ty::print::with_no_trimmed_paths! {todo!("Can't index into {curr_ty}!")}
Expand Down
Loading

0 comments on commit 4e2e8c6

Please sign in to comment.