Skip to content

Commit 1115270

Browse files
committed
Some work on fixing unsizinv
1 parent bf8c0c7 commit 1115270

File tree

14 files changed

+497
-113
lines changed

14 files changed

+497
-113
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ rustc-demangle = "0.1.23"
2020
cilly = {path = "./cilly"}
2121
serde = { version = "1.0.183", features = ["derive"] }
2222
strsim = "0.11.1"
23+
2324
[lib]
2425
crate-type=["rlib", "cdylib"]
2526

cargo_tests/build_std/src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ fn should_pass() {}
55
fn should_panic() {
66
panic!();
77
}
8+
fn main() {
9+
println!("Hi!");
10+
}

cilly/src/asm.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ edge [fontname=\"Helvetica,Arial,sans-serif\"]\nnode [shape=box];\n".to_string()
179179
.find(|&tpe| tpe.0.as_ref() == path)
180180
.map(|t| t.1)
181181
}
182-
pub fn finalize(&mut self) {
182+
pub fn resolve_method_aliases(&mut self) {
183183
for method in self
184184
.types
185185
.iter_mut()
@@ -192,7 +192,8 @@ edge [fontname=\"Helvetica,Arial,sans-serif\"]\nnode [shape=box];\n".to_string()
192192
.next()
193193
{
194194
let Some(target) = self.functions.get(site) else {
195-
panic!("can't find {site:?}");
195+
eprintln!("can't find {site:?}");
196+
eprintln!("can't find {site:?}");
196197
continue;
197198
};
198199
method.set_locals(target.locals());

cilly/src/bin/linker/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,7 @@ fn main() {
730730

731731
let (mut final_assembly, linkables) =
732732
load::load_assemblies(to_link.as_slice(), ar_to_link.as_slice());
733+
final_assembly.resolve_method_aliases();
733734
// Aplly certain fixes/workarounds to the final assembly
734735
override_errno(&mut final_assembly);
735736
patch::patch_all(&mut final_assembly);
@@ -751,9 +752,9 @@ fn main() {
751752
|| output_file_path.contains(".so")
752753
|| output_file_path.contains(".o");
753754
add_mandatory_statics(&mut final_assembly);
754-
final_assembly.finalize();
755+
755756
if !is_lib {
756-
final_assembly.eliminate_dead_code();
757+
//final_assembly.eliminate_dead_code();
757758
}
758759
if *C_MODE {
759760
type Exporter = cilly::c_exporter::CExporter;

setup_rustc_fork.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ cat > config.toml <<EOF
2222
change-id = 999999
2323
2424
[build]
25-
rustc = "rustc"
25+
rustc = "$(rustup which rustc)"
2626
cargo = "$(rustup which cargo)"
2727
full-bootstrap = false
2828
local-rebuild = true

src/assembly.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@ pub fn add_fn<'tcx>(
306306
}
307307
let mir = tcx.instance_mir(instance.def);
308308
if name.contains("rustc_codegen_clr_comptime_entrypoint") {
309+
if name.contains("rustc_codegen_clr_not_magic") {
310+
return Ok(());
311+
}
309312
crate::comptime::interpret(asm, instance, tcx, mir, cache);
310313
return Ok(());
311314
}
@@ -884,6 +887,7 @@ impl<'tcx, 'validator, 'type_cache> MethodCompileCtx<'tcx, 'validator, 'type_cac
884887
&self,
885888
ty: rustc_middle::ty::Ty<'tcx>,
886889
) -> rustc_middle::ty::layout::TyAndLayout<'tcx> {
890+
let ty = self.monomorphize(ty);
887891
self.tcx
888892
.layout_of(rustc_middle::ty::ParamEnvAnd {
889893
param_env: ParamEnv::reveal_all(),
@@ -892,3 +896,18 @@ impl<'tcx, 'validator, 'type_cache> MethodCompileCtx<'tcx, 'validator, 'type_cac
892896
.expect("Could not get type layout!")
893897
}
894898
}
899+
impl<'tcx> rustc_middle::ty::layout::HasTyCtxt<'tcx> for MethodCompileCtx<'tcx, '_, '_> {
900+
fn tcx(&self) -> TyCtxt<'tcx> {
901+
self.tcx
902+
}
903+
}
904+
impl rustc_abi::HasDataLayout for MethodCompileCtx<'_, '_, '_> {
905+
fn data_layout(&self) -> &rustc_abi::TargetDataLayout {
906+
self.tcx.data_layout()
907+
}
908+
}
909+
impl<'tcx> rustc_middle::ty::layout::HasParamEnv<'tcx> for MethodCompileCtx<'tcx, '_, '_> {
910+
fn param_env(&self) -> ParamEnv<'tcx> {
911+
ParamEnv::reveal_all()
912+
}
913+
}

src/comptime.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,18 @@ pub fn interpret<'tcx>(
4040
match &statement.kind {
4141
StatementKind::Assign(bx) => {
4242
let (target, rvalue) = bx.as_ref();
43-
let Rvalue::Use(src) = rvalue else { panic!() };
43+
let src = match rvalue {
44+
Rvalue::Use(src) => src,
45+
Rvalue::Cast(
46+
rustc_middle::mir::CastKind::PointerCoercion(
47+
rustc_middle::ty::adjustment::PointerCoercion::ReifyFnPointer,
48+
),
49+
_,
50+
_,
51+
) => continue,
52+
_ => panic!(),
53+
};
54+
4455
let src = src.place().unwrap().as_local().unwrap();
4556
let target = target.as_local().unwrap();
4657
locals[usize::from(target)] = locals[usize::from(src)].clone();
@@ -130,6 +141,8 @@ pub fn interpret<'tcx>(
130141
.expect("ERROR: unuported operation in interop type definiton.");
131142
asm.add_typedef(locals[usize::from(local)].as_type_def().unwrap().clone());
132143
ComptimeLocalVar::Void
144+
} else if function_name == "black_box".into() {
145+
ComptimeLocalVar::NotSet
133146
} else if function_name.contains("rustc_codegen_clr_add_field_def") {
134147
let src = args[0]
135148
.node

src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ config_flag! {INSERT_MIR_DEBUG_COMMENTS,false,"Tells the codegen to insert comme
3939
config_flag! {PRINT_LOCAL_TYPES,false,"Prints local types of all compiled MIR functions."}
4040
config_flag! {VALIDTE_VALUES,false,"Tells the codegen to insert additional checks on each variable asigement."}
4141
config_flag! {OPTIMIZE_CIL,true,"Tells the codegen to optmize the emiited CIL."}
42+
43+
config_flag! {NEW_UNSIZE,false,"Turns out the new unsizing code"}
44+
4245
lazy_static! {
4346
#[doc = "Tells the codegen to escape class and method names."]pub static ref ESCAPE_NAMES:bool = {
4447
std::env::vars().find_map(|(key,value)|if key == stringify!(ESCAPE_NAMES){

src/rvalue.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use crate::{
22
assembly::MethodCompileCtx,
3-
operand::handle_operand,
3+
operand::{handle_operand, operand_address},
4+
place::{place_address_raw, place_get},
45
r#type::{pointer_to_is_fat, Type},
6+
unsize::coerce_unsized_into,
57
};
68
use cilly::{
79
call_site::CallSite, cil_node::CILNode, cil_root::CILRoot, conv_usize,
@@ -45,7 +47,31 @@ pub fn handle_rvalue<'tcx>(
4547
dst,
4648
) => ptr_to_ptr(ctx, operand, *dst),
4749
Rvalue::Cast(CastKind::PointerCoercion(PointerCoercion::Unsize), operand, target) => {
48-
crate::unsize::unsize(ctx, operand, *target)
50+
if *crate::config::NEW_UNSIZE {
51+
let src_cil = handle_operand(operand, ctx);
52+
let src_cil = match src_cil.validate(ctx.validator(), None).unwrap() {
53+
Type::Ptr(_) => src_cil,
54+
Type::DotnetType(_) => operand_address(operand, ctx),
55+
_ => todo!(),
56+
};
57+
let src_type = src_cil.validate(ctx.validator(), None).unwrap();
58+
assert!(matches!(src_type, Type::Ptr(_)), "{src_type:?}");
59+
let dst_ty = ctx.layout_of(target_location.ty(ctx.body(), ctx.tcx()).ty);
60+
let dst_cil = place_address_raw(target_location, ctx);
61+
let coerce = coerce_unsized_into(
62+
ctx,
63+
src_cil,
64+
ctx.layout_of(operand.ty(ctx.body(), ctx.tcx())),
65+
dst_ty,
66+
dst_cil,
67+
);
68+
CILNode::SubTrees(Box::new((
69+
coerce.into(),
70+
Box::new(place_get(target_location, ctx)),
71+
)))
72+
} else {
73+
crate::unsize::unsize(ctx, operand, *target)
74+
}
4975
}
5076
Rvalue::BinaryOp(binop, operands) => {
5177
crate::binop::binop(*binop, &operands.0, &operands.1, ctx)

0 commit comments

Comments
 (0)