Skip to content

Commit 0236f3f

Browse files
committed
Refactored Ctx, type handling and porjection handling into separate crates.
1 parent a316ec5 commit 0236f3f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1938
-1557
lines changed

Cargo.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ libc = "0.2.153"
1313
libloading = "0.8.1"
1414

1515
postcard = { version = "1.0.8", features = ["use-std"] }
16+
rustc_codegen_clr_place = {path = "./rustc_codegen_clr_place"}
17+
rustc_codegen_clr_ctx = {path = "./rustc_codegen_clr_ctx"}
18+
rustc_codegen_clr_type = {path = "./rustc_codegen_clr_type"}
1619

1720
rustc-demangle = "0.1.23"
1821
cilly = {path = "./cilly"}
@@ -25,7 +28,7 @@ crate-type = ["dylib"]
2528

2629
[workspace]
2730
members = [ "cilly", "dotnet_aot",
28-
"mycorrhiza",
31+
"mycorrhiza", "rustc_codegen_clr_ctx", "rustc_codegen_clr_place", "rustc_codegen_clr_type",
2932
]
3033
exclude = ["rust/src/bootstrap","backup_rust/src/bootstrap"]
3134
[profile.release]

rustc_codegen_clr_ctx/Cargo.toml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "rustc_codegen_clr_ctx"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
cilly = {path = "../cilly"}

rustc_codegen_clr_ctx/src/lib.rs

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#![feature(rustc_private)]
2+
extern crate rustc_abi;
3+
extern crate rustc_middle;
4+
extern crate rustc_driver;
5+
use cilly::v2::Assembly;
6+
use rustc_middle::ty::layout::HasTypingEnv;
7+
use rustc_middle::ty::{Instance, PseudoCanonicalInput, TyCtxt};
8+
pub struct MethodCompileCtx<'tcx, 'asm> {
9+
tcx: TyCtxt<'tcx>,
10+
method: Option<&'tcx rustc_middle::mir::Body<'tcx>>,
11+
method_instance: Instance<'tcx>,
12+
asm: &'asm mut Assembly,
13+
}
14+
15+
impl std::ops::DerefMut for MethodCompileCtx<'_, '_> {
16+
#[allow(clippy::mut_mut)]
17+
fn deref_mut(&mut self) -> &mut Self::Target {
18+
&mut self.asm
19+
}
20+
}
21+
22+
impl<'asm> std::ops::Deref for MethodCompileCtx<'_, 'asm> {
23+
type Target = &'asm mut Assembly;
24+
25+
fn deref(&self) -> &Self::Target {
26+
&self.asm
27+
}
28+
}
29+
30+
impl<'tcx, 'asm> MethodCompileCtx<'tcx, 'asm> {
31+
#[must_use]
32+
/// Creates a [`MethodCompileCtx`] with a certain MIR body.
33+
pub fn with_body<'a: 'asm>(&'a mut self, body: &'tcx rustc_middle::mir::Body<'tcx>) -> Self {
34+
assert!(
35+
self.method.is_none(),
36+
"ERROR: attempt to change the body of a method compilation context"
37+
);
38+
Self {
39+
tcx: self.tcx,
40+
method: Some(body),
41+
method_instance: self.method_instance,
42+
asm: self.asm,
43+
}
44+
}
45+
pub fn new(
46+
tcx: TyCtxt<'tcx>,
47+
method: Option<&'tcx rustc_middle::mir::Body<'tcx>>,
48+
method_instance: Instance<'tcx>,
49+
asm: &'asm mut Assembly,
50+
) -> Self {
51+
Self {
52+
tcx,
53+
method,
54+
method_instance,
55+
asm,
56+
}
57+
}
58+
pub fn tcx_and_asm(&mut self) -> (TyCtxt<'tcx>, &mut Assembly) {
59+
(self.tcx, self.asm)
60+
}
61+
/// Returns the type context this method is compiled in.
62+
#[must_use]
63+
pub fn tcx(&self) -> TyCtxt<'tcx> {
64+
self.tcx
65+
}
66+
/// Returns the MIR body of this method is compiled.
67+
#[must_use]
68+
pub fn body(&self) -> &'tcx rustc_middle::mir::Body<'tcx> {
69+
self.method.unwrap()
70+
}
71+
#[must_use]
72+
/// Returns the Instance representing the current method
73+
pub fn instance(&self) -> Instance<'tcx> {
74+
self.method_instance
75+
}
76+
pub fn monomorphize<T: rustc_middle::ty::TypeFoldable<TyCtxt<'tcx>> + Clone>(
77+
&self,
78+
ty: T,
79+
) -> T {
80+
self.instance()
81+
.instantiate_mir_and_normalize_erasing_regions(
82+
self.tcx(),
83+
rustc_middle::ty::TypingEnv::fully_monomorphized(),
84+
rustc_middle::ty::EarlyBinder::bind(ty),
85+
)
86+
}
87+
88+
#[must_use]
89+
pub fn layout_of(
90+
&self,
91+
ty: rustc_middle::ty::Ty<'tcx>,
92+
) -> rustc_middle::ty::layout::TyAndLayout<'tcx> {
93+
let ty = self.monomorphize(ty);
94+
self.tcx
95+
.layout_of(PseudoCanonicalInput {
96+
typing_env: rustc_middle::ty::TypingEnv::fully_monomorphized(),
97+
value: ty,
98+
})
99+
.expect("Could not get type layout!")
100+
}
101+
102+
pub fn asm_mut<'s: 'a, 'a>(&'s mut self) -> &'a mut Assembly {
103+
self.asm
104+
}
105+
#[must_use]
106+
pub fn asm<'s: 'a, 'a>(&'s self) -> &'a Assembly {
107+
self.asm
108+
}
109+
}
110+
impl<'tcx> rustc_middle::ty::layout::HasTyCtxt<'tcx> for MethodCompileCtx<'tcx, '_> {
111+
fn tcx(&self) -> TyCtxt<'tcx> {
112+
self.tcx
113+
}
114+
}
115+
impl rustc_abi::HasDataLayout for MethodCompileCtx<'_, '_> {
116+
fn data_layout(&self) -> &rustc_abi::TargetDataLayout {
117+
self.tcx.data_layout()
118+
}
119+
}
120+
impl<'tcx> HasTypingEnv<'tcx> for MethodCompileCtx<'tcx, '_> {
121+
fn typing_env(&self) -> rustc_middle::ty::TypingEnv<'tcx> {
122+
rustc_middle::ty::TypingEnv::fully_monomorphized()
123+
}
124+
}

rustc_codegen_clr_place/Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "rustc_codegen_clr_place"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
cilly = {path = "../cilly"}
8+
rustc_codegen_clr_ctx = {path = "../rustc_codegen_clr_ctx"}
9+
rustc_codegen_clr_type = {path = "../rustc_codegen_clr_type"}

src/place/adress.rs rustc_codegen_clr_place/src/adress.rs

+65-56
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
use super::PlaceTy;
2-
use crate::{
3-
assembly::MethodCompileCtx,
4-
assert_morphic,
5-
r#type::{fat_ptr_to, pointer_to_is_fat},
6-
};
2+
use crate::pointer_to_is_fat;
73
use cilly::{
8-
call,
4+
Assembly, Const, IntoAsmIndex, NodeIdx, Type, call,
95
cil_node::CILNode,
106
conv_usize, ld_field,
11-
v2::{cilnode::MethodKind, FieldDesc, Int, MethodRef},
12-
Assembly, Const, IntoAsmIndex, NodeIdx, Type,
7+
v2::{FieldDesc, Int, MethodRef, cilnode::MethodKind},
138
};
9+
use rustc_codegen_clr_ctx::MethodCompileCtx;
10+
use rustc_codegen_clr_type::{adt::{enum_field_descriptor, field_descrptor, FieldOffsetIterator}, r#type::fat_ptr_to, GetTypeExt};
1411
use rustc_middle::{
1512
mir::PlaceElem,
1613
ty::{Ty, TyKind},
@@ -55,7 +52,7 @@ pub fn address_last_dereference<'tcx>(
5552
pointer_to_is_fat(target_ty, ctx.tcx(), ctx.instance()),
5653
) {
5754
(true, false) => {
58-
let data_ptr_name = ctx.alloc_string(crate::DATA_PTR);
55+
let data_ptr_name = ctx.alloc_string(cilly::DATA_PTR);
5956
let void_ptr = ctx.nptr(Type::Void);
6057
CILNode::LDIndPtr {
6158
ptr: Box::new(CILNode::LDField {
@@ -89,21 +86,22 @@ fn field_address<'a>(
8986
let curr_type = ctx.monomorphize(curr_type);
9087
let field_ty = ctx.monomorphize(field_type);
9188
match (
92-
crate::r#type::pointer_to_is_fat(curr_type, ctx.tcx(), ctx.instance()),
93-
crate::r#type::pointer_to_is_fat(field_ty, ctx.tcx(), ctx.instance()),
89+
pointer_to_is_fat(curr_type, ctx.tcx(), ctx.instance()),
90+
pointer_to_is_fat(field_ty, ctx.tcx(), ctx.instance()),
9491
) {
9592
(false, false) => {
96-
let field_desc = crate::utilis::field_descrptor(curr_type, field_index, ctx);
97-
CILNode::LDFieldAdress {
98-
addr: addr_calc.into(),
99-
field: (field_desc),
100-
}
93+
let field_desc = field_descrptor(curr_type, field_index, ctx);
94+
CILNode::LDFieldAdress {
95+
addr: addr_calc.into(),
96+
field: (field_desc),
97+
}
10198
}
102-
(false, true) => panic!("Sized type {curr_type:?} contains an unsized field of type {field_ty}. This is a bug."),
103-
(true,false)=>{
104-
let mut explicit_offset_iter = crate::utilis::adt::FieldOffsetIterator::fields(
105-
ctx.layout_of(curr_type).layout.0 .0.clone(),
106-
);
99+
(false, true) => panic!(
100+
"Sized type {curr_type:?} contains an unsized field of type {field_ty}. This is a bug."
101+
),
102+
(true, false) => {
103+
let mut explicit_offset_iter =
104+
FieldOffsetIterator::fields(ctx.layout_of(curr_type).layout.0.0.clone());
107105
let offset = explicit_offset_iter
108106
.nth(field_index as usize)
109107
.expect("Field index not in field offset iterator");
@@ -112,19 +110,23 @@ fn field_address<'a>(
112110
curr_type,
113111
rustc_middle::ty::Mutability::Mut,
114112
));
115-
let data_ptr_name = ctx.alloc_string(crate::DATA_PTR);
113+
let data_ptr_name = ctx.alloc_string(cilly::DATA_PTR);
116114
let void_ptr = ctx.nptr(Type::Void);
117-
let addr_descr = ctx.alloc_field(FieldDesc::new(curr_type_fat_ptr.as_class_ref().unwrap(),data_ptr_name,void_ptr));
115+
let addr_descr = ctx.alloc_field(FieldDesc::new(
116+
curr_type_fat_ptr.as_class_ref().unwrap(),
117+
data_ptr_name,
118+
void_ptr,
119+
));
118120
// Get the address of the unsized object.
119-
let obj_addr = ld_field!(addr_calc,addr_descr);
121+
let obj_addr = ld_field!(addr_calc, addr_descr);
120122
let obj = ctx.type_from_cache(field_type);
121123
// Add the offset to the object.
122-
(obj_addr + CILNode::V2(ctx.alloc_node(Const::USize(u64::from(offset))))).cast_ptr(ctx.nptr(obj))
123-
},
124-
(true,true)=>{
125-
let mut explicit_offset_iter = crate::utilis::adt::FieldOffsetIterator::fields(
126-
ctx.layout_of(curr_type).layout.0 .0.clone(),
127-
);
124+
(obj_addr + CILNode::V2(ctx.alloc_node(Const::USize(u64::from(offset)))))
125+
.cast_ptr(ctx.nptr(obj))
126+
}
127+
(true, true) => {
128+
let mut explicit_offset_iter =
129+
FieldOffsetIterator::fields(ctx.layout_of(curr_type).layout.0.0.clone());
128130
let offset = explicit_offset_iter
129131
.nth(field_index as usize)
130132
.expect("Field index not in field offset iterator");
@@ -133,29 +135,37 @@ fn field_address<'a>(
133135
curr_type,
134136
rustc_middle::ty::Mutability::Mut,
135137
));
136-
let data_ptr_name = ctx.alloc_string(crate::DATA_PTR);
137-
let metadata_name = ctx.alloc_string(crate::METADATA);
138-
let void_ptr = ctx.nptr(Type::Void);
138+
let data_ptr_name = ctx.alloc_string(cilly::DATA_PTR);
139+
let metadata_name = ctx.alloc_string(cilly::METADATA);
140+
let void_ptr = ctx.nptr(Type::Void);
139141

140-
let addr_descr = ctx.alloc_field(FieldDesc::new(curr_type_fat_ptr.as_class_ref().unwrap(),data_ptr_name,void_ptr));
141-
// Get the address of the unsized object.
142-
let obj_addr = ld_field!(addr_calc.clone(),addr_descr);
143-
let metadata_descr = ctx.alloc_field(FieldDesc::new(curr_type_fat_ptr.as_class_ref().unwrap(),metadata_name,Type::Int(Int::USize)));
144-
let metadata = ld_field!(addr_calc,metadata_descr);
145-
let ptr = obj_addr
146-
+ CILNode::V2(ctx.alloc_node(Const::USize(u64::from(offset))));
147-
let field_fat_ptr = ctx.type_from_cache(Ty::new_ptr(
148-
ctx.tcx(),
149-
field_ty,
150-
rustc_middle::ty::Mutability::Mut,
151-
));
152-
CILNode::create_slice(field_fat_ptr.as_class_ref().unwrap(), ctx, metadata, ptr)
142+
let addr_descr = ctx.alloc_field(FieldDesc::new(
143+
curr_type_fat_ptr.as_class_ref().unwrap(),
144+
data_ptr_name,
145+
void_ptr,
146+
));
147+
// Get the address of the unsized object.
148+
let obj_addr = ld_field!(addr_calc.clone(), addr_descr);
149+
let metadata_descr = ctx.alloc_field(FieldDesc::new(
150+
curr_type_fat_ptr.as_class_ref().unwrap(),
151+
metadata_name,
152+
Type::Int(Int::USize),
153+
));
154+
let metadata = ld_field!(addr_calc, metadata_descr);
155+
let ptr =
156+
obj_addr + CILNode::V2(ctx.alloc_node(Const::USize(u64::from(offset))));
157+
let field_fat_ptr = ctx.type_from_cache(Ty::new_ptr(
158+
ctx.tcx(),
159+
field_ty,
160+
rustc_middle::ty::Mutability::Mut,
161+
));
162+
CILNode::create_slice(field_fat_ptr.as_class_ref().unwrap(), ctx, metadata, ptr)
153163
}
154164
}
155165
}
156166
super::PlaceTy::EnumVariant(enm, var_idx) => {
157167
let owner = ctx.monomorphize(enm);
158-
let field_desc = crate::utilis::enum_field_descriptor(owner, field_index, var_idx, ctx);
168+
let field_desc = enum_field_descriptor(owner, field_index, var_idx, ctx);
159169
CILNode::LDFieldAdress {
160170
addr: addr_calc.into(),
161171
field: field_desc,
@@ -171,7 +181,6 @@ pub fn place_elem_adress<'tcx>(
171181
addr_calc: CILNode,
172182
) -> CILNode {
173183
let curr_type = curr_type.monomorphize(ctx);
174-
assert_morphic!(curr_type);
175184

176185
match place_elem {
177186
PlaceElem::Deref => address_last_dereference(place_ty, curr_type, ctx, addr_calc),
@@ -182,14 +191,14 @@ pub fn place_elem_adress<'tcx>(
182191
let curr_ty = curr_type
183192
.as_ty()
184193
.expect("INVALID PLACE: Indexing into enum variant???");
185-
let index = crate::place::local_get(index.as_usize(), ctx.body(), ctx);
194+
let index = crate::local_get(index.as_usize(), ctx.body(), ctx);
186195
match curr_ty.kind() {
187196
TyKind::Slice(inner) => {
188197
let inner = ctx.monomorphize(*inner);
189198
let inner_type = ctx.type_from_cache(inner);
190199
let slice = fat_ptr_to(inner, ctx);
191200

192-
let data_ptr_name = ctx.alloc_string(crate::DATA_PTR);
201+
let data_ptr_name = ctx.alloc_string(cilly::DATA_PTR);
193202
let void_ptr = ctx.nptr(Type::Void);
194203
let desc = ctx.alloc_field(FieldDesc::new(slice, data_ptr_name, void_ptr));
195204
// This is a false positive
@@ -210,21 +219,21 @@ pub fn place_elem_adress<'tcx>(
210219
call!(ctx.alloc_methodref(mref), [addr_calc, CILNode::V2(index)])
211220
}
212221
_ => {
213-
todo!("Can't index into {curr_ty}!")
222+
todo!("Can't index into {curr_ty}!")
214223
}
215224
}
216225
}
217226
PlaceElem::Subslice { from, to, from_end } => {
218227
let curr_type = fat_ptr_to(curr_type.as_ty().expect("Can't index into an enum!"), ctx);
219228

220229
if *from_end {
221-
let metadata_name = ctx.alloc_string(crate::METADATA);
230+
let metadata_name = ctx.alloc_string(cilly::METADATA);
222231
let metadata_field = ctx.alloc_field(FieldDesc::new(
223232
curr_type,
224233
metadata_name,
225234
Type::Int(Int::USize),
226235
));
227-
let data_ptr_name = ctx.alloc_string(crate::DATA_PTR);
236+
let data_ptr_name = ctx.alloc_string(cilly::DATA_PTR);
228237
let void_ptr = ctx.nptr(Type::Void);
229238
let ptr_field = ctx.alloc_field(FieldDesc::new(curr_type, data_ptr_name, void_ptr));
230239
let metadata = CILNode::Sub(
@@ -236,7 +245,7 @@ pub fn place_elem_adress<'tcx>(
236245
CILNode::create_slice(curr_type, ctx, metadata, data_ptr)
237246
} else {
238247
let void_ptr = ctx.nptr(Type::Void);
239-
let data_ptr = ctx.alloc_string(crate::DATA_PTR);
248+
let data_ptr = ctx.alloc_string(cilly::DATA_PTR);
240249

241250
let ptr_field = ctx.alloc_field(FieldDesc::new(curr_type, data_ptr, void_ptr));
242251
let metadata = CILNode::V2(ctx.alloc_node(Const::USize(to - from)));
@@ -264,9 +273,9 @@ pub fn place_elem_adress<'tcx>(
264273
let inner_type = ctx.type_from_cache(inner);
265274
let slice = fat_ptr_to(Ty::new_slice(ctx.tcx(), inner), ctx);
266275
let void_ptr = ctx.nptr(Type::Void);
267-
let data_ptr = ctx.alloc_string(crate::DATA_PTR);
276+
let data_ptr = ctx.alloc_string(cilly::DATA_PTR);
268277
let desc = ctx.alloc_field(FieldDesc::new(slice, data_ptr, void_ptr));
269-
let metadata = ctx.alloc_string(crate::METADATA);
278+
let metadata = ctx.alloc_string(cilly::METADATA);
270279
let len =
271280
ctx.alloc_field(FieldDesc::new(slice, metadata, Type::Int(Int::USize)));
272281
let index = if *from_end {

0 commit comments

Comments
 (0)