Skip to content

Commit 8caef7b

Browse files
authored
Merge pull request #22 from FractalFir/type_caching
Complete rewrite of the type handling system
2 parents 4b807af + aada87e commit 8caef7b

Some content is hidden

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

46 files changed

+3175
-1736
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ edition = "2021"
88
[dependencies]
99
ar = "0.9.0"
1010
colored = "2.0.4"
11+
either = "1.9.0"
1112
lazy_static = "1.4.0"
1213
postcard = { version = "1.0.6", features = ["use-std"] }
1314
serde = { version = "1.0.183", features = ["derive"] }

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ The project is still very early in development, but it has made significant prog
5555

5656
## Milestones
5757
- [X] Draft version of the `mycorrhiza` interop layer, capable of creating managed objects, and calling methods.
58-
- [ ] Compiling the `core` Rust crate - *Substantial chunk of core now compiles, with many methods still having trouble compiling. The compilation still gets terminated early due to errors*
58+
- [X] Compiling the `core` Rust crate - *Core now compiles, but is not fully functional. Around ~100 function instances don't compile at all, and a couple more compile with errors.
5959
- [ ] Compiling the `alloc` Rust crate
6060
- [ ] Compiling the `std` Rust crate
6161
- [ ] Stack unwinding
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[build]
22
target = "wasm64-unknown-unknown"
33
[unstable]
4-
build-std = ["panic_abort","alloc"]
4+
build-std = ["panic_abort","core","alloc"]
55

cargo_tests/build_alloc/src/main.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#![allow(internal_features,unused_imports,incomplete_features,unused_variables,dead_code,improper_ctypes_definitions)]
22
#![feature(lang_items,adt_const_params,associated_type_defaults,core_intrinsics,start)]
33
#![no_std]
4-
//use mycorrhiza::{start,panic_handler};
5-
//panic_handler!{}
6-
//start!{}
4+
5+
use mycorrhiza::{start,panic_handler};
6+
panic_handler!{}
7+
start!{}
78
//#[lang = "eh_personality"]
89
//fn rust_eh_personality() {}
910
fn main() {

cargo_tests/build_core/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ edition = "2021"
77

88
[dependencies]
99
mycorrhiza = {path="../../mycorrhiza"}
10-
[workspace]
10+
[workspace]
11+
12+
codegen-units = 1

cargo_tests/build_core/src/main.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#![allow(internal_features,unused_imports,incomplete_features,unused_variables,dead_code,improper_ctypes_definitions)]
22
#![feature(lang_items,adt_const_params,associated_type_defaults,core_intrinsics,start)]
33
#![no_std]
4-
//use mycorrhiza::{start,panic_handler};
5-
//panic_handler!{}
6-
//start!{}
4+
5+
use mycorrhiza::{start,panic_handler};
6+
panic_handler!{}
7+
start!{}
78
//#[lang = "eh_personality"]
89
//fn rust_eh_personality() {}
910
fn main() {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[build]
22
target = "wasm64-unknown-unknown"
33
[unstable]
4-
build-std = ["panic_abort","std"]
4+
build-std = ["core","alloc","std","panic_abort"]
55

cargo_tests/build_std/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ edition = "2021"
77

88
[dependencies]
99
mycorrhiza = {path="../../mycorrhiza"}
10-
[workspace]
10+
[workspace]
11+
[profile.release.build-override]
12+
codegen-units = 1

cargo_tests/build_std/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![allow(internal_features,unused_imports,incomplete_features,unused_variables,dead_code,improper_ctypes_definitions)]
22
#![feature(lang_items,adt_const_params,associated_type_defaults,core_intrinsics,start)]
3-
#![no_std]
3+
//#![no_std]
4+
45
//use mycorrhiza::{start,panic_handler};
56
//panic_handler!{}
67
//start!{}

cargo_tests/fastrand_test/src/main.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![no_std]
22
#![feature(start,core_intrinsics,lang_items)]
3-
//use glam::*;
3+
use fastrand::*;
44
use mycorrhiza::{start,panic_handler};
55
use core::hint::black_box;
66
panic_handler!{}
@@ -10,5 +10,9 @@ fn rust_eh_personality() {}
1010
fn main() {
1111
let tuple = (2,black_box(8));
1212
black_box(tuple);
13-
mycorrhiza::system::runtime::Console;
13+
let mut rng = black_box(fastrand::Rng::with_seed(0xDEAD_BEEF));
14+
//let mut bytes: Vec<u8> = repeat_with(|| ).take(10_000).collect();
15+
16+
17+
mycorrhiza::system::console::Console::writeln_u64(black_box(rng.u64(..)));
1418
}

src/aggregate.rs

Lines changed: 81 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use crate::cil_op::{CILOp, FieldDescriptor};
2-
use crate::r#type::{DotnetTypeRef, Type};
2+
use crate::r#type::{DotnetTypeRef, TyCache, Type};
33
use crate::utilis::{field_name, monomorphize};
44
use rustc_index::IndexVec;
55
use rustc_middle::mir::{AggregateKind, Operand, Place};
6-
use rustc_middle::ty::{AdtDef, AdtKind, GenericArg, Instance, List, ParamEnv, Ty, TyCtxt, TyKind};
6+
use rustc_middle::ty::{
7+
AdtDef, AdtKind, EarlyBinder, GenericArg, Instance, List, ParamEnv, Ty, TyCtxt, TyKind,
8+
};
79
use rustc_target::abi::FieldIdx;
810
/// Returns the CIL ops to create the aggreagate value specifed by `aggregate_kind` at `target_location`. Uses indivlidual values specifed by `value_index`
911
pub fn handle_aggregate<'tyctx>(
@@ -13,6 +15,7 @@ pub fn handle_aggregate<'tyctx>(
1315
aggregate_kind: &AggregateKind<'tyctx>,
1416
value_index: &IndexVec<FieldIdx, Operand<'tyctx>>,
1517
method_instance: Instance<'tyctx>,
18+
tycache: &mut TyCache,
1619
) -> Vec<CILOp> {
1720
// Get CIL ops for each value
1821
let values: Vec<_> = value_index
@@ -21,14 +24,18 @@ pub fn handle_aggregate<'tyctx>(
2124
.map(|operand| {
2225
(
2326
operand.0 as u32,
24-
crate::operand::handle_operand(operand.1, tyctx, method, method_instance),
27+
crate::operand::handle_operand(operand.1, tyctx, method, method_instance, tycache),
2528
)
2629
})
2730
.collect();
2831
match aggregate_kind {
2932
AggregateKind::Adt(adt_def, variant_idx, subst, _utai, active_field) => {
30-
let penv = ParamEnv::empty();
31-
let adt_type = Instance::resolve(tyctx, penv, *adt_def, subst)
33+
let penv = ParamEnv::reveal_all();
34+
let subst = crate::utilis::monomorphize(&method_instance, *subst, tyctx);
35+
//eprintln!("Preparing to resolve {adt_def:?} {subst:?}");
36+
let adt_type = Instance::resolve(tyctx, penv, *adt_def, subst);
37+
38+
let adt_type = adt_type
3239
.expect("Could not resolve instance")
3340
.expect("Could not resolve instance")
3441
.ty(tyctx, penv);
@@ -48,15 +55,22 @@ pub fn handle_aggregate<'tyctx>(
4855
variant_idx.as_u32(),
4956
values,
5057
method_instance,
51-
active_field,
58+
&active_field,
59+
tycache,
5260
)
5361
}
5462
AggregateKind::Array(element) => {
55-
let element = Type::from_ty(*element, tyctx, &method_instance);
63+
let element = crate::utilis::monomorphize(&method_instance, *element, tyctx);
64+
let element = tycache.type_from_cache(element, tyctx, Some(method_instance));
5665
let array_type = DotnetTypeRef::array(element.clone(), value_index.len());
5766
let mut ops: Vec<CILOp> = Vec::with_capacity(values.len() * 2);
58-
let array_getter =
59-
super::place::place_adress(target_location, tyctx, method, method_instance);
67+
let array_getter = super::place::place_adress(
68+
target_location,
69+
tyctx,
70+
method,
71+
method_instance,
72+
tycache,
73+
);
6074
let sig = crate::function_sig::FnSig::new(
6175
&[array_type.clone().into(), Type::USize, Type::GenericArg(0)],
6276
&Type::Void,
@@ -75,19 +89,30 @@ pub fn handle_aggregate<'tyctx>(
7589
tyctx,
7690
method,
7791
method_instance,
92+
tycache,
7893
));
7994
ops
8095
}
8196
AggregateKind::Tuple => {
8297
if values.len() > 8 {
8398
todo!("Tuples with more than 8 fields are not supported yet.");
8499
} else {
85-
let tuple_getter =
86-
super::place::place_adress(target_location, tyctx, method, method_instance);
100+
let tuple_getter = super::place::place_adress(
101+
target_location,
102+
tyctx,
103+
method,
104+
method_instance,
105+
tycache,
106+
);
87107
let types: Vec<_> = value_index
88108
.iter()
89109
.map(|operand| {
90-
Type::from_ty(operand.ty(method, tyctx), tyctx, &method_instance)
110+
let operand_ty = crate::utilis::monomorphize(
111+
&method_instance,
112+
operand.ty(method, tyctx),
113+
tyctx,
114+
);
115+
tycache.type_from_cache(operand_ty, tyctx, Some(method_instance))
91116
})
92117
.collect();
93118
let dotnet_tpe = crate::r#type::tuple_type(&types);
@@ -107,6 +132,7 @@ pub fn handle_aggregate<'tyctx>(
107132
tyctx,
108133
method,
109134
method_instance,
135+
tycache,
110136
));
111137
ops
112138
}
@@ -126,56 +152,54 @@ fn aggregate_adt<'tyctx>(
126152
fields: Vec<(u32, Vec<CILOp>)>,
127153
method_instance: Instance<'tyctx>,
128154
_active_field: &Option<FieldIdx>,
155+
type_cache: &mut crate::r#type::TyCache,
129156
) -> Vec<CILOp> {
130157
let adt_type = crate::utilis::monomorphize(&method_instance, adt_type, tyctx);
131-
let adt_type_ref = Type::from_ty(adt_type, tyctx, &method_instance);
158+
let adt_type_ref = type_cache.type_from_cache(adt_type, tyctx, Some(method_instance));
132159
let adt_type_ref = if let Type::DotnetType(type_ref) = adt_type_ref {
133160
type_ref.as_ref().clone()
134161
} else {
135162
panic!("Can't get fields of type {adt_type:?}");
136163
};
137164
match adt.adt_kind() {
138165
AdtKind::Struct => {
139-
let obj_getter =
140-
crate::place::place_adress(target_location, tyctx, method, method_instance);
166+
let obj_getter = crate::place::place_adress(
167+
target_location,
168+
tyctx,
169+
method,
170+
method_instance,
171+
type_cache,
172+
);
141173
let mut ops: Vec<CILOp> = Vec::with_capacity(fields.len() * 2);
142174
for field in fields {
143175
ops.extend(obj_getter.iter().cloned());
144176
ops.extend(field.1);
145-
let field_def = adt
146-
.all_fields()
147-
.nth(field.0 as usize)
148-
.expect("Could not find field!");
149-
let field_type = field_def.ty(tyctx, subst);
150-
151-
let field_type = if crate::utilis::is_ty_alias(field_type) {
152-
Type::from_ty(
153-
crate::utilis::monomorphize(&method_instance, field_type, tyctx),
154-
tyctx,
155-
&method_instance,
156-
)
157-
} else {
158-
crate::utilis::generic_field_ty(adt_type, field.0, tyctx, method_instance)
159-
};
160-
let field_name = field_name(adt_type, field.0);
161-
let field_desc = crate::cil_op::FieldDescriptor::boxed(
162-
adt_type_ref.clone(),
163-
field_type,
164-
field_name,
177+
let field_desc = crate::utilis::field_descrptor(
178+
adt_type,
179+
field.0,
180+
tyctx,
181+
method_instance,
182+
type_cache,
165183
);
166-
ops.push(CILOp::STField(field_desc));
184+
ops.push(CILOp::STField(field_desc.into()));
167185
}
168186
ops.extend(crate::place::place_get(
169187
target_location,
170188
tyctx,
171189
method,
172190
method_instance,
191+
type_cache,
173192
));
174193
ops
175194
}
176195
AdtKind::Enum => {
177-
let adt_adress_ops =
178-
crate::place::place_adress(target_location, tyctx, method, method_instance);
196+
let adt_adress_ops = crate::place::place_adress(
197+
target_location,
198+
tyctx,
199+
method,
200+
method_instance,
201+
type_cache,
202+
);
179203

180204
let mut variant_type = adt_type_ref.clone(); //adt_type.variant_type(variant).expect("Can't get variant index");
181205
let variant_name = crate::utilis::variant_name(adt_type, variant_idx);
@@ -203,16 +227,15 @@ fn aggregate_adt<'tyctx>(
203227
ops.extend(variant_address.clone());
204228
ops.extend(field_value.1.clone());
205229
let field_name = field.name.to_string();
206-
let field_name = crate::type_def::escape_field_name(&field_name);
207-
let field = crate::utilis::generic_field_ty(
208-
adt_type,
209-
field_idx as u32,
230+
let field_name = crate::r#type::escape_field_name(&field_name);
231+
let field_type = type_cache.type_from_cache(
232+
field.ty(tyctx, subst),
210233
tyctx,
211-
method_instance,
234+
Some(method_instance),
212235
);
213236
ops.push(CILOp::STField(Box::new(FieldDescriptor::new(
214237
variant_type.clone(),
215-
field,
238+
field_type,
216239
field_name,
217240
))));
218241
}
@@ -233,12 +256,18 @@ fn aggregate_adt<'tyctx>(
233256
tyctx,
234257
method,
235258
method_instance,
259+
type_cache,
236260
));
237261
ops
238262
}
239263
AdtKind::Union => {
240-
let obj_getter =
241-
crate::place::place_adress(target_location, tyctx, method, method_instance);
264+
let obj_getter = crate::place::place_adress(
265+
target_location,
266+
tyctx,
267+
method,
268+
method_instance,
269+
type_cache,
270+
);
242271
let mut ops: Vec<CILOp> = Vec::with_capacity(fields.len() * 2);
243272
for field in fields {
244273
ops.extend(obj_getter.iter().cloned());
@@ -247,23 +276,25 @@ fn aggregate_adt<'tyctx>(
247276
.all_fields()
248277
.nth(field.0 as usize)
249278
.expect("Could not find field!");
250-
let _field_type = field_def.ty(tyctx, subst);
251-
279+
let field_type = field_def.ty(tyctx, subst);
280+
let field_type = crate::utilis::monomorphize(&method_instance, field_type, tyctx);
252281
let field_type =
253-
crate::utilis::generic_field_ty(adt_type, field.0, tyctx, method_instance);
282+
type_cache.type_from_cache(field_type, tyctx, Some(method_instance));
254283
let field_name = field_name(adt_type, field.0);
255284
let field_desc = crate::cil_op::FieldDescriptor::boxed(
256285
adt_type_ref.clone(),
257286
field_type,
258287
field_name,
259288
);
260-
ops.push(CILOp::STField(field_desc));
289+
290+
ops.push(CILOp::STField(field_desc.into()));
261291
}
262292
ops.extend(crate::place::place_get(
263293
target_location,
264294
tyctx,
265295
method,
266296
method_instance,
297+
type_cache,
267298
));
268299
ops
269300
}

0 commit comments

Comments
 (0)