Skip to content

Commit 4e2e8c6

Browse files
committed
Moved parts of projection handling to use v2 node
1 parent bd2d711 commit 4e2e8c6

File tree

8 files changed

+127
-116
lines changed

8 files changed

+127
-116
lines changed

cilly/src/cil_node.rs

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

226226
impl CILNode {
227+
pub fn stack_addr(val: Self, tpe: TypeIdx) -> Self {
228+
CILNode::TemporaryLocal(Box::new((
229+
tpe,
230+
[CILRoot::SetTMPLocal { value: val }].into(),
231+
CILNode::LoadAddresOfTMPLocal,
232+
)))
233+
}
227234
pub fn ovf_check_tuple(
228235
asm: &mut Assembly,
229236
tuple: ClassRefIdx,

src/constant.rs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -146,32 +146,21 @@ fn load_scalar_ptr(
146146
MethodKind::Static,
147147
vec![].into(),
148148
);
149-
return CILNode::TemporaryLocal(Box::new((
149+
return CILNode::stack_addr(
150+
CILNode::Call(Box::new(CallOpArgs {
151+
args: Box::new([]),
152+
site: ctx.alloc_methodref(mref),
153+
})),
150154
ctx.alloc_type(u8_ptr_ptr),
151-
[CILRoot::SetTMPLocal {
152-
value: CILNode::Call(Box::new(CallOpArgs {
153-
args: Box::new([]),
154-
site: ctx.alloc_methodref(mref),
155-
})),
156-
}]
157-
.into(),
158-
CILNode::LoadAddresOfTMPLocal,
159-
)));
155+
);
160156
}
161157
let attrs = ctx.tcx().codegen_fn_attrs(def_id);
162158

163159
if attrs.import_linkage.is_some() {
164160
// TODO: this could cause issues if the pointer to the static is not imediatly dereferenced.
165161
let site = get_fn_from_static_name(&name, ctx);
166162
let ptr_sig = Type::FnPtr(ctx[site].sig());
167-
return CILNode::TemporaryLocal(Box::new((
168-
ctx.alloc_type(ptr_sig),
169-
[CILRoot::SetTMPLocal {
170-
value: CILNode::LDFtn(site),
171-
}]
172-
.into(),
173-
CILNode::LoadAddresOfTMPLocal,
174-
)));
163+
return CILNode::stack_addr(CILNode::LDFtn(site), ctx.alloc_type(ptr_sig));
175164
}
176165
if let Some(section) = attrs.link_section {
177166
panic!("static {name} requires special linkage in section {section:?}");

src/operand.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ pub(crate) fn handle_operand<'tcx>(
1313
) -> CILNode {
1414
let res = ctx.type_from_cache(ctx.monomorphize(operand.ty(ctx.body(), ctx.tcx())));
1515
if res == Type::Void {
16-
return CILNode::TemporaryLocal(Box::new((
17-
ctx.alloc_type(Type::Void),
18-
[].into(),
19-
CILNode::LoadTMPLocal,
20-
)));
16+
return CILNode::uninit_val(Type::Void, ctx);
2117
}
2218
match operand {
2319
Operand::Copy(place) | Operand::Move(place) => crate::place::place_get(place, ctx),
@@ -33,11 +29,7 @@ pub(crate) fn operand_address<'tcx>(
3329
Operand::Constant(const_val) => {
3430
let local_type = ctx.type_from_cache(operand.ty(ctx.body(), ctx.tcx()));
3531
let constant = crate::constant::handle_constant(const_val, ctx);
36-
let ptr = CILNode::TemporaryLocal(Box::new((
37-
ctx.alloc_type(local_type),
38-
[CILRoot::SetTMPLocal { value: constant }].into(),
39-
CILNode::LoadAddresOfTMPLocal,
40-
)));
32+
let ptr = CILNode::stack_addr(constant, ctx.alloc_type(local_type));
4133
crate::place::deref_op(
4234
crate::place::PlaceTy::Ty(operand.ty(ctx.body(), ctx.tcx())),
4335
ctx,

src/place/adress.rs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@ use cilly::{
99
cil_node::CILNode,
1010
conv_usize, ld_field,
1111
v2::{cilnode::MethodKind, FieldDesc, Int, MethodRef},
12-
Const, IntoAsmIndex, Type,
12+
Assembly, Const, IntoAsmIndex, NodeIdx, Type,
1313
};
1414
use rustc_middle::{
1515
mir::PlaceElem,
1616
ty::{Ty, TyKind},
1717
};
18-
pub fn local_adress(local: usize, method: &rustc_middle::mir::Body) -> CILNode {
19-
if let Some(spread_arg) = method.spread_arg
18+
pub fn local_adress(local: usize, method: &rustc_middle::mir::Body, asm: &mut Assembly) -> NodeIdx {
19+
let local = if let Some(spread_arg) = method.spread_arg
2020
&& local == spread_arg.as_usize()
2121
{
22-
return CILNode::MRefToRawPtr(Box::new(CILNode::LDLocA(
22+
cilly::CILNode::LdLocA(
2323
(method.local_decls.len() - method.arg_count)
2424
.try_into()
2525
.unwrap(),
26-
)));
27-
}
28-
if local == 0 {
29-
CILNode::MRefToRawPtr(CILNode::LDLocA(0).into())
30-
} else if local > method.arg_count {
31-
CILNode::MRefToRawPtr(
32-
CILNode::LDLocA(u32::try_from(local - method.arg_count).unwrap()).into(),
3326
)
27+
} else if local == 0 {
28+
cilly::CILNode::LdLocA(0)
29+
} else if local > method.arg_count {
30+
cilly::CILNode::LdLocA(u32::try_from(local - method.arg_count).unwrap())
3431
} else {
35-
CILNode::MRefToRawPtr(CILNode::LDArgA(u32::try_from(local - 1).unwrap()).into())
36-
}
32+
cilly::CILNode::LdArgA(u32::try_from(local - 1).unwrap())
33+
};
34+
let local = asm.alloc_node(local);
35+
asm.alloc_node(cilly::CILNode::RefToPtr(local))
3736
}
3837
pub fn address_last_dereference<'tcx>(
3938
target_ty: Ty<'tcx>,
@@ -197,7 +196,7 @@ pub fn place_elem_adress<'tcx>(
197196
let curr_ty = curr_type
198197
.as_ty()
199198
.expect("INVALID PLACE: Indexing into enum variant???");
200-
let index = crate::place::local_get(index.as_usize(), ctx.body());
199+
let index = crate::place::local_get(index.as_usize(), ctx.body(), ctx);
201200
match curr_ty.kind() {
202201
TyKind::Slice(inner) => {
203202
let inner = ctx.monomorphize(*inner);
@@ -209,9 +208,16 @@ pub fn place_elem_adress<'tcx>(
209208
let desc = ctx.alloc_field(FieldDesc::new(slice, data_ptr_name, void_ptr));
210209
// This is a false positive
211210
// #[allow(unused_parens)]
211+
let size = ctx.size_of(inner_type);
212+
let size = size.into_idx(ctx);
213+
let size = ctx.alloc_node(cilly::CILNode::IntCast {
214+
input: size,
215+
target: Int::USize,
216+
extend: cilly::cilnode::ExtendKind::ZeroExtend,
217+
});
218+
let offset = ctx.biop(index, size, cilly::BinOp::Mul);
212219
(ld_field!(addr_calc.clone(), desc)).cast_ptr(ctx.nptr(inner_type))
213-
+ conv_usize!(CILNode::V2(ctx.size_of(inner_type).into_idx(ctx)))
214-
* conv_usize!(index)
220+
+ CILNode::V2(ctx.alloc_node(offset))
215221
}
216222
TyKind::Array(element, _length) => {
217223
let element = ctx.monomorphize(*element);
@@ -227,7 +233,7 @@ pub fn place_elem_adress<'tcx>(
227233
MethodKind::Instance,
228234
vec![].into(),
229235
);
230-
call!(ctx.alloc_methodref(mref), [addr_calc, index])
236+
call!(ctx.alloc_methodref(mref), [addr_calc, CILNode::V2(index)])
231237
}
232238
_ => {
233239
rustc_middle::ty::print::with_no_trimmed_paths! {todo!("Can't index into {curr_ty}!")}

src/place/body.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ use cilly::{
1111
cil_node::CILNode,
1212
conv_usize, ld_field,
1313
v2::{cilnode::MethodKind, FieldDesc, Int, MethodRef},
14-
Const, IntoAsmIndex, Type,
14+
Const, IntoAsmIndex, NodeIdx, Type,
1515
};
1616
use rustc_middle::mir::PlaceElem;
1717
use rustc_middle::ty::{Ty, TyKind};
18-
pub fn local_body<'tcx>(local: usize, ctx: &mut MethodCompileCtx<'tcx, '_>) -> (CILNode, Ty<'tcx>) {
18+
pub fn local_body<'tcx>(local: usize, ctx: &mut MethodCompileCtx<'tcx, '_>) -> (NodeIdx, Ty<'tcx>) {
1919
let ty = ctx.body().local_decls[local.into()].ty;
2020
let ty = ctx.monomorphize(ty);
2121
if body_ty_is_by_adress(ty, ctx) {
22-
(super::adress::local_adress(local, ctx.body()), ty)
22+
(super::adress::local_adress(local, ctx.body(), ctx), ty)
2323
} else {
24-
(super::get::local_get(local, ctx.body()), ty)
24+
(super::get::local_get(local, ctx.body(), ctx), ty)
2525
}
2626
}
2727
fn body_field<'a>(
@@ -132,7 +132,7 @@ pub fn place_elem_body_index<'tcx>(
132132
parrent_node: CILNode,
133133
index: rustc_middle::mir::Local,
134134
) -> (PlaceTy<'tcx>, CILNode) {
135-
let index = crate::place::local_get(index.as_usize(), ctx.body());
135+
let index = crate::place::local_get(index.as_usize(), ctx.body(), ctx);
136136
match curr_ty.kind() {
137137
TyKind::Slice(inner) => {
138138
let inner = ctx.monomorphize(*inner);
@@ -143,12 +143,17 @@ pub fn place_elem_body_index<'tcx>(
143143
ctx.alloc_string(crate::DATA_PTR),
144144
ctx.nptr(Type::Void),
145145
);
146+
let size = ctx.size_of(inner_type);
147+
let size = size.into_idx(ctx);
148+
let size = ctx.alloc_node(cilly::CILNode::IntCast {
149+
input: size,
150+
target: Int::USize,
151+
extend: cilly::cilnode::ExtendKind::ZeroExtend,
152+
});
153+
let offset = ctx.biop(index, size, cilly::BinOp::Mul);
146154
let addr = ld_field!(parrent_node, ctx.alloc_field(desc))
147155
.cast_ptr(ctx.nptr(inner_type))
148-
+ (index
149-
* CILNode::ZeroExtendToUSize(
150-
CILNode::V2(ctx.size_of(inner_type).into_idx(ctx)).into(),
151-
));
156+
+ CILNode::V2(ctx.alloc_node(offset));
152157

153158
if body_ty_is_by_adress(inner, ctx) {
154159
(inner.into(), addr)
@@ -165,6 +170,12 @@ pub fn place_elem_body_index<'tcx>(
165170
let array_type = ctx.type_from_cache(curr_ty);
166171
let array_dotnet = array_type.as_class_ref().expect("Non array type");
167172
let arr_ref = ctx.nref(array_type);
173+
let index = ctx.alloc_node(cilly::CILNode::IntCast {
174+
input: index,
175+
target: Int::USize,
176+
extend: cilly::cilnode::ExtendKind::ZeroExtend,
177+
});
178+
let index = CILNode::V2(index);
168179
if body_ty_is_by_adress(element, ctx) {
169180
let elem_ptr = ctx.nptr(element_type);
170181
let mref = MethodRef::new(
@@ -174,10 +185,7 @@ pub fn place_elem_body_index<'tcx>(
174185
MethodKind::Instance,
175186
vec![].into(),
176187
);
177-
let ops = call!(
178-
ctx.alloc_methodref(mref),
179-
[parrent_node, CILNode::ZeroExtendToUSize(index.into())]
180-
);
188+
let ops = call!(ctx.alloc_methodref(mref), [parrent_node, index]);
181189
((element).into(), ops)
182190
} else {
183191
let mref = MethodRef::new(
@@ -187,10 +195,7 @@ pub fn place_elem_body_index<'tcx>(
187195
MethodKind::Instance,
188196
vec![].into(),
189197
);
190-
let ops = call!(
191-
ctx.alloc_methodref(mref),
192-
[parrent_node, CILNode::ZeroExtendToUSize(index.into())]
193-
);
198+
let ops = call!(ctx.alloc_methodref(mref), [parrent_node, index]);
194199
((element).into(), ops)
195200
}
196201
}

src/place/get.rs

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use crate::{assembly::MethodCompileCtx, r#type::fat_ptr_to};
22
use cilly::{
3+
asm::Assembly,
34
call,
45
cil_node::CILNode,
56
conv_usize, ld_field,
67
v2::{cilnode::MethodKind, FieldDesc, Int, MethodRef},
7-
Const, IntoAsmIndex, Type,
8+
Const, IntoAsmIndex, NodeIdx, Type,
89
};
910
use rustc_middle::{
1011
mir::{Place, PlaceElem},
@@ -14,33 +15,41 @@ use rustc_middle::{
1415

1516
use super::body_ty_is_by_adress;
1617

17-
pub(super) fn local_get(local: usize, method: &rustc_middle::mir::Body) -> CILNode {
18-
if let Some(spread_arg) = method.spread_arg
19-
&& local == spread_arg.as_usize()
20-
{
21-
return CILNode::LDLoc(
22-
(method.local_decls.len() - method.arg_count)
23-
.try_into()
24-
.unwrap(),
25-
);
26-
}
27-
if local == 0 {
28-
CILNode::LDLoc(0)
29-
} else if local > method.arg_count {
30-
CILNode::LDLoc(
31-
u32::try_from(local - method.arg_count)
32-
.expect("Method has more than 2^32 local varaibles"),
33-
)
34-
} else {
35-
CILNode::LDArg(u32::try_from(local - 1).expect("Method has more than 2^32 local variables"))
36-
}
18+
pub(super) fn local_get(
19+
local: usize,
20+
method: &rustc_middle::mir::Body,
21+
asm: &mut Assembly,
22+
) -> NodeIdx {
23+
asm.alloc_node(
24+
if let Some(spread_arg) = method.spread_arg
25+
&& local == spread_arg.as_usize()
26+
{
27+
cilly::CILNode::LdLoc(
28+
(method.local_decls.len() - method.arg_count)
29+
.try_into()
30+
.unwrap(),
31+
)
32+
} else if local == 0 {
33+
cilly::CILNode::LdLoc(0)
34+
} else if local > method.arg_count {
35+
cilly::CILNode::LdLoc(
36+
u32::try_from(local - method.arg_count)
37+
.expect("Method has more than 2^32 local varaibles"),
38+
)
39+
} else {
40+
cilly::CILNode::LdArg(
41+
u32::try_from(local - 1).expect("Method has more than 2^32 local variables"),
42+
)
43+
},
44+
)
3745
}
3846
/// Returns the ops for getting the value of place.
3947
pub fn place_get<'tcx>(place: &Place<'tcx>, ctx: &mut MethodCompileCtx<'tcx, '_>) -> CILNode {
4048
if place.projection.is_empty() {
41-
local_get(place.local.as_usize(), ctx.body())
49+
CILNode::V2(local_get(place.local.as_usize(), ctx.body(), ctx))
4250
} else {
43-
let (mut op, mut ty) = super::local_body(place.local.as_usize(), ctx);
51+
let (op, mut ty) = super::local_body(place.local.as_usize(), ctx);
52+
let mut op = CILNode::V2(op);
4453
ty = ctx.monomorphize(ty);
4554
let mut ty = ty.into();
4655

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

128137
let index_type = ctx.monomorphize(ctx.body().local_decls[*index].ty);
129-
let index = crate::place::local_get(index.as_usize(), ctx.body());
138+
let index = crate::place::local_get(index.as_usize(), ctx.body(), ctx);
130139

131140
match curr_ty.kind() {
132141
TyKind::Slice(inner) => {
@@ -140,15 +149,17 @@ fn place_elem_get<'a>(
140149
ctx.alloc_string(crate::DATA_PTR),
141150
ctx.nptr(Type::Void),
142151
);
143-
let size = crate::casts::int_to_int(
144-
Type::Int(Int::I32),
145-
index_type,
146-
CILNode::V2(ctx.size_of(inner_type).into_idx(ctx)),
147-
ctx,
148-
);
152+
let size = ctx.size_of(inner_type);
153+
let size = size.into_idx(ctx);
154+
let size = ctx.alloc_node(cilly::CILNode::IntCast {
155+
input: size,
156+
target: Int::USize,
157+
extend: cilly::cilnode::ExtendKind::ZeroExtend,
158+
});
159+
let offset = ctx.biop(index, size, cilly::BinOp::Mul);
149160
let addr = (ld_field!(addr_calc, ctx.alloc_field(desc))
150161
.cast_ptr(ctx.nptr(inner_type)))
151-
+ (index * size);
162+
+ CILNode::V2(ctx.alloc_node(offset));
152163
super::deref_op(super::PlaceTy::Ty(inner), ctx, addr)
153164
}
154165
TyKind::Array(element, _length) => {
@@ -164,10 +175,7 @@ fn place_elem_get<'a>(
164175
MethodKind::Instance,
165176
vec![].into(),
166177
);
167-
call!(
168-
ctx.alloc_methodref(mref),
169-
[addr_calc, CILNode::ZeroExtendToUSize(index.into())]
170-
)
178+
call!(ctx.alloc_methodref(mref), [addr_calc, CILNode::V2(index)])
171179
}
172180
_ => {
173181
rustc_middle::ty::print::with_no_trimmed_paths! {todo!("Can't index into {curr_ty}!")}

0 commit comments

Comments
 (0)