Skip to content

Commit ac20e7f

Browse files
committed
Refactors + windows fix
1 parent f765968 commit ac20e7f

File tree

17 files changed

+149
-164
lines changed

17 files changed

+149
-164
lines changed

cilly/src/basic_block.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ impl BasicBlock {
208208
.flat_map(|tree| tree.root_mut().deref_mut().into_iter())
209209
}*/
210210
/// Checks if this block does nothing except cononditionaly jump to another block.
211-
212211
#[must_use]
213212
pub fn handler(&self) -> Option<&Handler> {
214213
self.handler.as_ref()

cilly/src/bin/optsect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn asm_with_fuel(asm: &Assembly, path: &Path, fuel: u32) {
1414
);
1515
let export_time = std::time::Instant::now();
1616
eprintln!("Prepraing to export.");
17-
asm.export(&path, ILExporter::new(*ILASM_FLAVOUR, false));
17+
asm.export(path, ILExporter::new(*ILASM_FLAVOUR, false));
1818
eprintln!("Exported in {} ms", export_time.elapsed().as_millis());
1919
let mut config_path = path.to_owned();
2020
config_path.set_extension("runtimeconfig.json");

cilly/src/v2/asm.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ impl Assembly {
191191
.filter(move |(id, def)| filter(self, **id, def))
192192
}
193193
/// Modifies the method deifinition by running the closure on it
194-
195194
pub fn modify_methodef(
196195
&mut self,
197196
modify: impl FnOnce(&mut Self, &mut MethodDef),
@@ -1051,7 +1050,7 @@ fn get_default_ilasm() -> String {
10511050
return "ilasm".into();
10521051
}
10531052
// Framework Path
1054-
let framework_path = PathBuf::from("C:\\Windows\\Microsoft.NET\\Framework");
1053+
let framework_path = std::path::PathBuf::from("C:\\Windows\\Microsoft.NET\\Framework");
10551054
let framework_dir = std::fs::read_dir(&framework_path).unwrap_or_else(|_| panic!("Could not find the .NET framework directory at {framework_path:?}, when searching for ilasm."));
10561055
for entry in framework_dir {
10571056
let entry = entry.unwrap();

cilly/src/v2/c_exporter/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ fn c_tpe(field_tpe: Type, asm: &Assembly) -> String {
8686
"{elem}{dims}",
8787
elem = c_tpe(asm[elem], asm),
8888
dims = "*".repeat(dims.get() as usize)
89-
)
90-
.into(),
89+
),
9190
Type::FnPtr(_) => "void*".into(),
9291
}
9392
}
@@ -732,7 +731,7 @@ impl CExporter {
732731
),
733732
}
734733
}
735-
CILRoot::SourceFileInfo { line_start, line_len, col_start, col_len, file } => format!("#line {line_start} {file:?}", file = &asm[file]).into(),
734+
CILRoot::SourceFileInfo { line_start, line_len, col_start, col_len, file } => format!("#line {line_start} {file:?}", file = &asm[file]),
736735
CILRoot::SetField(info) =>{
737736
let (field,addr,value) = info.as_ref();
738737
let addr = Self::node_to_string(asm[*addr].clone(), asm, locals, inputs, sig);

cilly/src/v2/class.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,6 @@ pub struct ClassDef {
397397
}
398398
impl ClassDef {
399399
/// Checks if this class defition has a with the name and type.
400-
401400
#[must_use]
402401
pub fn has_static_field(&self, fld_name: StringIdx, fld_tpe: Type) -> bool {
403402
self.static_fields

cilly/src/v2/il_exporter/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,7 @@ impl ILExporter {
11241124
}
11251125
super::CILRoot::SetStaticField { field, val } => {
11261126
self.export_node(asm, out, val, sig, locals)?;
1127-
let sfld = asm[field].clone();
1127+
let sfld = asm[field];
11281128
let owner = class_ref(sfld.owner(), asm);
11291129
let name = &asm[sfld.name()];
11301130
let tpe = type_il(&sfld.tpe(), asm);

cilly/src/v2/opt/mod.rs

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub fn opt_if_fuel<T>(new: T, original: T, fuel: &mut OptFuel) -> T {
2323
original
2424
}
2525
}
26+
2627
impl CILNode {
2728
// The complexity of this function is unavoidable.
2829
#[allow(clippy::too_many_lines)]
@@ -49,46 +50,43 @@ impl CILNode {
4950
let input = asm.alloc_node(input);
5051
CILNode::UnOp(input, unop.clone())
5152
}
52-
CILNode::LdLoc(loc) => {
53-
if *loc == idx {
54-
if !fuel.consume(1) {
55-
return self.clone();
56-
}
57-
match tpe {
58-
Type::Float(_)
59-
| Type::Bool
60-
| Type::FnPtr(_)
61-
| Type::Ptr(_)
62-
| Type::ClassRef(_)
63-
| Type::Int(
64-
Int::I128
65-
| Int::U128
66-
| Int::USize
67-
| Int::ISize
68-
| Int::I64
69-
| Int::U64
70-
| Int::U32
71-
| Int::I32,
72-
)
73-
| Type::Ref(_) => asm.get_node(new_node).clone(),
74-
Type::Int(int @ (Int::I8 | Int::U8 | Int::I16 | Int::U16)) => {
75-
CILNode::IntCast {
76-
input: new_node,
77-
target: int,
78-
// Does not matter, since this does nothing for ints < 32 bits, which this arm handles.
79-
extend: if int.is_signed() {
80-
super::cilnode::ExtendKind::SignExtend
81-
} else {
82-
super::cilnode::ExtendKind::ZeroExtend
83-
},
84-
}
53+
CILNode::LdLoc(loc) if *loc == idx => {
54+
if !fuel.consume(1) {
55+
return self.clone();
56+
}
57+
match tpe {
58+
Type::Float(_)
59+
| Type::Bool
60+
| Type::FnPtr(_)
61+
| Type::Ptr(_)
62+
| Type::ClassRef(_)
63+
| Type::Int(
64+
Int::I128
65+
| Int::U128
66+
| Int::USize
67+
| Int::ISize
68+
| Int::I64
69+
| Int::U64
70+
| Int::U32
71+
| Int::I32,
72+
)
73+
| Type::Ref(_) => asm.get_node(new_node).clone(),
74+
Type::Int(int @ (Int::I8 | Int::U8 | Int::I16 | Int::U16)) => {
75+
CILNode::IntCast {
76+
input: new_node,
77+
target: int,
78+
// Does not matter, since this does nothing for ints < 32 bits, which this arm handles.
79+
extend: if int.is_signed() {
80+
super::cilnode::ExtendKind::SignExtend
81+
} else {
82+
super::cilnode::ExtendKind::ZeroExtend
83+
},
8584
}
86-
_ => CILNode::LdLoc(*loc),
8785
}
88-
} else {
89-
CILNode::LdLoc(*loc)
86+
_ => CILNode::LdLoc(*loc),
9087
}
9188
}
89+
CILNode::LdLoc(loc) => CILNode::LdLoc(*loc),
9290
CILNode::LdLocA(loc) => CILNode::LdLocA(*loc), // This takes an address, so we can't propagate it
9391
CILNode::LdArg(arg) => CILNode::LdArg(*arg),
9492
CILNode::LdArgA(arg) => CILNode::LdArgA(*arg),

cilly/src/v2/opt/opt_node.rs

Lines changed: 87 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,91 @@
1-
use crate::v2::{cilnode::ExtendKind, Assembly, CILNode, Const, Int, Type};
1+
use crate::v2::{cilnode::ExtendKind, Assembly, CILNode, Const, Int, NodeIdx, Type};
22

33
use super::{opt_if_fuel, OptFuel};
4-
4+
/// Optimizes an intiger cast.
5+
fn opt_int_cast(
6+
original: CILNode,
7+
asm: &mut Assembly,
8+
fuel: &mut OptFuel,
9+
input: NodeIdx,
10+
target: Int,
11+
extend: ExtendKind,
12+
) -> CILNode {
13+
match asm.get_node(input) {
14+
CILNode::Const(cst) => match (cst.as_ref(), target) {
15+
(Const::U64(val), Int::USize) => opt_if_fuel(Const::USize(*val).into(), original, fuel),
16+
(Const::I64(val), Int::ISize) => opt_if_fuel(Const::ISize(*val).into(), original, fuel),
17+
(Const::U64(val), Int::U64) => opt_if_fuel(Const::U64(*val).into(), original, fuel),
18+
(Const::I64(val), Int::I64) => opt_if_fuel(Const::I64(*val).into(), original, fuel),
19+
(Const::U32(val), Int::U32) => opt_if_fuel(Const::U32(*val).into(), original, fuel),
20+
(Const::I32(val), Int::I32) => opt_if_fuel(Const::I32(*val).into(), original, fuel),
21+
(Const::I32(val), Int::U32) => {
22+
opt_if_fuel(Const::U32(*val as u32).into(), original, fuel)
23+
}
24+
(Const::U64(val), Int::U8) => opt_if_fuel(Const::U8(*val as u8).into(), original, fuel),
25+
(Const::I32(val), Int::USize) => match extend {
26+
ExtendKind::SignExtend => {
27+
opt_if_fuel(Const::USize(*val as i64 as u64).into(), original, fuel)
28+
}
29+
ExtendKind::ZeroExtend => {
30+
opt_if_fuel(Const::USize(*val as u32 as u64).into(), original, fuel)
31+
}
32+
},
33+
_ => original,
34+
},
35+
CILNode::IntCast {
36+
input: input2,
37+
target: target2,
38+
extend: extend2,
39+
} => {
40+
if target == *target2 && extend == *extend2 {
41+
return opt_if_fuel(asm.get_node(input).clone(), original, fuel);
42+
}
43+
match (target, target2) {
44+
(Int::USize | Int::ISize, Int::USize | Int::ISize) => {
45+
// A usize to isize cast does nothing, except change the type on the evaulation stack(the bits are unchanged).
46+
// So, we can just create a cast like it.
47+
opt_if_fuel(
48+
CILNode::IntCast {
49+
input: *input2,
50+
target,
51+
extend: *extend2,
52+
},
53+
original,
54+
fuel,
55+
)
56+
}
57+
(Int::U64 | Int::I64, Int::U64 | Int::I64) => {
58+
// A u64 to i64 cast does nothing, except change the type on the evaulation stack(the bits are unchanged).
59+
// So, we can just create a cast like it.
60+
opt_if_fuel(
61+
CILNode::IntCast {
62+
input: *input2,
63+
target,
64+
extend: *extend2,
65+
},
66+
original,
67+
fuel,
68+
)
69+
}
70+
(Int::U32 | Int::I32, Int::U32 | Int::I32) => {
71+
// A u64 to i64 cast does nothing, except change the type on the evaulation stack(the bits are unchanged).
72+
// So, we can just create a cast like it.
73+
opt_if_fuel(
74+
CILNode::IntCast {
75+
input: *input2,
76+
target,
77+
extend: *extend2,
78+
},
79+
original,
80+
fuel,
81+
)
82+
}
83+
_ => original,
84+
}
85+
}
86+
_ => original,
87+
}
88+
}
589
pub fn opt_node(original: CILNode, asm: &mut Assembly, fuel: &mut OptFuel) -> CILNode {
690
match original {
791
CILNode::SizeOf(tpe) => match asm[tpe] {
@@ -27,87 +111,7 @@ pub fn opt_node(original: CILNode, asm: &mut Assembly, fuel: &mut OptFuel) -> CI
27111
input,
28112
target,
29113
extend,
30-
} => match asm.get_node(input) {
31-
CILNode::Const(cst) => match (cst.as_ref(), target) {
32-
(Const::U64(val), Int::USize) => {
33-
opt_if_fuel(Const::USize(*val).into(), original, fuel)
34-
}
35-
(Const::I64(val), Int::ISize) => {
36-
opt_if_fuel(Const::ISize(*val).into(), original, fuel)
37-
}
38-
(Const::U64(val), Int::U64) => opt_if_fuel(Const::U64(*val).into(), original, fuel),
39-
(Const::I64(val), Int::I64) => opt_if_fuel(Const::I64(*val).into(), original, fuel),
40-
(Const::U32(val), Int::U32) => opt_if_fuel(Const::U32(*val).into(), original, fuel),
41-
(Const::I32(val), Int::I32) => opt_if_fuel(Const::I32(*val).into(), original, fuel),
42-
(Const::I32(val), Int::U32) => {
43-
opt_if_fuel(Const::U32(*val as u32).into(), original, fuel)
44-
}
45-
(Const::U64(val), Int::U8) => {
46-
opt_if_fuel(Const::U8(*val as u8).into(), original, fuel)
47-
}
48-
(Const::I32(val), Int::USize) => match extend {
49-
ExtendKind::SignExtend => {
50-
opt_if_fuel(Const::USize(*val as i64 as u64).into(), original, fuel)
51-
}
52-
ExtendKind::ZeroExtend => {
53-
opt_if_fuel(Const::USize(*val as u32 as u64).into(), original, fuel)
54-
}
55-
},
56-
_ => original,
57-
},
58-
CILNode::IntCast {
59-
input: input2,
60-
target: target2,
61-
extend: extend2,
62-
} => {
63-
if target == *target2 && extend == *extend2 {
64-
return opt_if_fuel(asm.get_node(input).clone(), original, fuel);
65-
}
66-
match (target, target2) {
67-
(Int::USize | Int::ISize, Int::USize | Int::ISize) => {
68-
// A usize to isize cast does nothing, except change the type on the evaulation stack(the bits are unchanged).
69-
// So, we can just create a cast like it.
70-
opt_if_fuel(
71-
CILNode::IntCast {
72-
input: *input2,
73-
target,
74-
extend: *extend2,
75-
},
76-
original,
77-
fuel,
78-
)
79-
}
80-
(Int::U64 | Int::I64, Int::U64 | Int::I64) => {
81-
// A u64 to i64 cast does nothing, except change the type on the evaulation stack(the bits are unchanged).
82-
// So, we can just create a cast like it.
83-
opt_if_fuel(
84-
CILNode::IntCast {
85-
input: *input2,
86-
target,
87-
extend: *extend2,
88-
},
89-
original,
90-
fuel,
91-
)
92-
}
93-
(Int::U32 | Int::I32, Int::U32 | Int::I32) => {
94-
// A u64 to i64 cast does nothing, except change the type on the evaulation stack(the bits are unchanged).
95-
// So, we can just create a cast like it.
96-
opt_if_fuel(
97-
CILNode::IntCast {
98-
input: *input2,
99-
target,
100-
extend: *extend2,
101-
},
102-
original,
103-
fuel,
104-
)
105-
}
106-
_ => original,
107-
}
108-
}
109-
_ => original,
110-
},
114+
} => opt_int_cast(original, asm, fuel, input, target, extend),
111115
CILNode::Call(info) => super::inline::trivial_inline_call(info.0, &info.1, fuel, asm),
112116
CILNode::LdInd {
113117
addr,
@@ -142,7 +146,6 @@ pub fn opt_node(original: CILNode, asm: &mut Assembly, fuel: &mut OptFuel) -> CI
142146
}
143147
_ => original,
144148
},
145-
146149
CILNode::LdField { addr, field } => match asm.get_node(addr) {
147150
CILNode::RefToPtr(addr) => {
148151
opt_if_fuel(CILNode::LdField { addr: *addr, field }, original, fuel)

cilly/src/v2/opt/simplify_handlers.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ fn block_gc(blocks: &mut Vec<BasicBlock>, asm: &Assembly) {
3434
.cloned()
3535
.collect();
3636
}
37-
3837
pub fn simplify_bbs(
3938
handler: Option<&mut Vec<BasicBlock>>,
4039
asm: &mut Assembly,

cilly/src/v2/typecheck.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,6 @@ fn test() {
804804
let lhs = super::Const::I64(0);
805805
let rhs = super::Const::F64(super::hashable::HashableF64(0.0));
806806
let sum = asm.biop(lhs, rhs, BinOp::Add);
807-
let sum = asm.alloc_node(sum);
808-
let sig = asm.sig([], Type::Void);
807+
let _sum = asm.alloc_node(sum);
808+
let _sig = asm.sig([], Type::Void);
809809
}

src/constant.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,7 @@ fn load_scalar_ptr(
211211
// If it is a function, patch its pointer up.
212212
let call_info = crate::call_info::CallInfo::sig_from_instance_(finstance, ctx);
213213
let function_name = crate::utilis::function_name(ctx.tcx().symbol_name(finstance));
214-
return CILNode::LDFtn(
215-
CallSite::new(None, function_name, call_info.sig().clone(), true).into(),
216-
);
214+
CILNode::LDFtn(CallSite::new(None, function_name, call_info.sig().clone(), true).into())
217215
}
218216
GlobalAlloc::VTable(..) => todo!("Unhandled global alloc {global_alloc:?}"),
219217
}

0 commit comments

Comments
 (0)