Skip to content

Commit

Permalink
Refactors + windows fix
Browse files Browse the repository at this point in the history
  • Loading branch information
FractalFir committed Sep 28, 2024
1 parent f765968 commit ac20e7f
Show file tree
Hide file tree
Showing 17 changed files with 149 additions and 164 deletions.
1 change: 0 additions & 1 deletion cilly/src/basic_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ impl BasicBlock {
.flat_map(|tree| tree.root_mut().deref_mut().into_iter())
}*/
/// Checks if this block does nothing except cononditionaly jump to another block.
#[must_use]
pub fn handler(&self) -> Option<&Handler> {
self.handler.as_ref()
Expand Down
2 changes: 1 addition & 1 deletion cilly/src/bin/optsect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn asm_with_fuel(asm: &Assembly, path: &Path, fuel: u32) {
);
let export_time = std::time::Instant::now();
eprintln!("Prepraing to export.");
asm.export(&path, ILExporter::new(*ILASM_FLAVOUR, false));
asm.export(path, ILExporter::new(*ILASM_FLAVOUR, false));
eprintln!("Exported in {} ms", export_time.elapsed().as_millis());
let mut config_path = path.to_owned();
config_path.set_extension("runtimeconfig.json");
Expand Down
3 changes: 1 addition & 2 deletions cilly/src/v2/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ impl Assembly {
.filter(move |(id, def)| filter(self, **id, def))
}
/// Modifies the method deifinition by running the closure on it
pub fn modify_methodef(
&mut self,
modify: impl FnOnce(&mut Self, &mut MethodDef),
Expand Down Expand Up @@ -1051,7 +1050,7 @@ fn get_default_ilasm() -> String {
return "ilasm".into();
}
// Framework Path
let framework_path = PathBuf::from("C:\\Windows\\Microsoft.NET\\Framework");
let framework_path = std::path::PathBuf::from("C:\\Windows\\Microsoft.NET\\Framework");
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."));
for entry in framework_dir {
let entry = entry.unwrap();
Expand Down
5 changes: 2 additions & 3 deletions cilly/src/v2/c_exporter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ fn c_tpe(field_tpe: Type, asm: &Assembly) -> String {
"{elem}{dims}",
elem = c_tpe(asm[elem], asm),
dims = "*".repeat(dims.get() as usize)
)
.into(),
),
Type::FnPtr(_) => "void*".into(),
}
}
Expand Down Expand Up @@ -732,7 +731,7 @@ impl CExporter {
),
}
}
CILRoot::SourceFileInfo { line_start, line_len, col_start, col_len, file } => format!("#line {line_start} {file:?}", file = &asm[file]).into(),
CILRoot::SourceFileInfo { line_start, line_len, col_start, col_len, file } => format!("#line {line_start} {file:?}", file = &asm[file]),
CILRoot::SetField(info) =>{
let (field,addr,value) = info.as_ref();
let addr = Self::node_to_string(asm[*addr].clone(), asm, locals, inputs, sig);
Expand Down
1 change: 0 additions & 1 deletion cilly/src/v2/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,6 @@ pub struct ClassDef {
}
impl ClassDef {
/// Checks if this class defition has a with the name and type.
#[must_use]
pub fn has_static_field(&self, fld_name: StringIdx, fld_tpe: Type) -> bool {
self.static_fields
Expand Down
2 changes: 1 addition & 1 deletion cilly/src/v2/il_exporter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@ impl ILExporter {
}
super::CILRoot::SetStaticField { field, val } => {
self.export_node(asm, out, val, sig, locals)?;
let sfld = asm[field].clone();
let sfld = asm[field];
let owner = class_ref(sfld.owner(), asm);
let name = &asm[sfld.name()];
let tpe = type_il(&sfld.tpe(), asm);
Expand Down
70 changes: 34 additions & 36 deletions cilly/src/v2/opt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub fn opt_if_fuel<T>(new: T, original: T, fuel: &mut OptFuel) -> T {
original
}
}

impl CILNode {
// The complexity of this function is unavoidable.
#[allow(clippy::too_many_lines)]
Expand All @@ -49,46 +50,43 @@ impl CILNode {
let input = asm.alloc_node(input);
CILNode::UnOp(input, unop.clone())
}
CILNode::LdLoc(loc) => {
if *loc == idx {
if !fuel.consume(1) {
return self.clone();
}
match tpe {
Type::Float(_)
| Type::Bool
| Type::FnPtr(_)
| Type::Ptr(_)
| Type::ClassRef(_)
| Type::Int(
Int::I128
| Int::U128
| Int::USize
| Int::ISize
| Int::I64
| Int::U64
| Int::U32
| Int::I32,
)
| Type::Ref(_) => asm.get_node(new_node).clone(),
Type::Int(int @ (Int::I8 | Int::U8 | Int::I16 | Int::U16)) => {
CILNode::IntCast {
input: new_node,
target: int,
// Does not matter, since this does nothing for ints < 32 bits, which this arm handles.
extend: if int.is_signed() {
super::cilnode::ExtendKind::SignExtend
} else {
super::cilnode::ExtendKind::ZeroExtend
},
}
CILNode::LdLoc(loc) if *loc == idx => {
if !fuel.consume(1) {
return self.clone();
}
match tpe {
Type::Float(_)
| Type::Bool
| Type::FnPtr(_)
| Type::Ptr(_)
| Type::ClassRef(_)
| Type::Int(
Int::I128
| Int::U128
| Int::USize
| Int::ISize
| Int::I64
| Int::U64
| Int::U32
| Int::I32,
)
| Type::Ref(_) => asm.get_node(new_node).clone(),
Type::Int(int @ (Int::I8 | Int::U8 | Int::I16 | Int::U16)) => {
CILNode::IntCast {
input: new_node,
target: int,
// Does not matter, since this does nothing for ints < 32 bits, which this arm handles.
extend: if int.is_signed() {
super::cilnode::ExtendKind::SignExtend
} else {
super::cilnode::ExtendKind::ZeroExtend
},
}
_ => CILNode::LdLoc(*loc),
}
} else {
CILNode::LdLoc(*loc)
_ => CILNode::LdLoc(*loc),
}
}
CILNode::LdLoc(loc) => CILNode::LdLoc(*loc),
CILNode::LdLocA(loc) => CILNode::LdLocA(*loc), // This takes an address, so we can't propagate it
CILNode::LdArg(arg) => CILNode::LdArg(*arg),
CILNode::LdArgA(arg) => CILNode::LdArgA(*arg),
Expand Down
171 changes: 87 additions & 84 deletions cilly/src/v2/opt/opt_node.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,91 @@
use crate::v2::{cilnode::ExtendKind, Assembly, CILNode, Const, Int, Type};
use crate::v2::{cilnode::ExtendKind, Assembly, CILNode, Const, Int, NodeIdx, Type};

use super::{opt_if_fuel, OptFuel};

/// Optimizes an intiger cast.
fn opt_int_cast(
original: CILNode,
asm: &mut Assembly,
fuel: &mut OptFuel,
input: NodeIdx,
target: Int,
extend: ExtendKind,
) -> CILNode {
match asm.get_node(input) {
CILNode::Const(cst) => match (cst.as_ref(), target) {
(Const::U64(val), Int::USize) => opt_if_fuel(Const::USize(*val).into(), original, fuel),
(Const::I64(val), Int::ISize) => opt_if_fuel(Const::ISize(*val).into(), original, fuel),
(Const::U64(val), Int::U64) => opt_if_fuel(Const::U64(*val).into(), original, fuel),
(Const::I64(val), Int::I64) => opt_if_fuel(Const::I64(*val).into(), original, fuel),
(Const::U32(val), Int::U32) => opt_if_fuel(Const::U32(*val).into(), original, fuel),
(Const::I32(val), Int::I32) => opt_if_fuel(Const::I32(*val).into(), original, fuel),
(Const::I32(val), Int::U32) => {
opt_if_fuel(Const::U32(*val as u32).into(), original, fuel)
}
(Const::U64(val), Int::U8) => opt_if_fuel(Const::U8(*val as u8).into(), original, fuel),
(Const::I32(val), Int::USize) => match extend {
ExtendKind::SignExtend => {
opt_if_fuel(Const::USize(*val as i64 as u64).into(), original, fuel)
}
ExtendKind::ZeroExtend => {
opt_if_fuel(Const::USize(*val as u32 as u64).into(), original, fuel)
}
},
_ => original,
},
CILNode::IntCast {
input: input2,
target: target2,
extend: extend2,
} => {
if target == *target2 && extend == *extend2 {
return opt_if_fuel(asm.get_node(input).clone(), original, fuel);
}
match (target, target2) {
(Int::USize | Int::ISize, Int::USize | Int::ISize) => {
// A usize to isize cast does nothing, except change the type on the evaulation stack(the bits are unchanged).
// So, we can just create a cast like it.
opt_if_fuel(
CILNode::IntCast {
input: *input2,
target,
extend: *extend2,
},
original,
fuel,
)
}
(Int::U64 | Int::I64, Int::U64 | Int::I64) => {
// A u64 to i64 cast does nothing, except change the type on the evaulation stack(the bits are unchanged).
// So, we can just create a cast like it.
opt_if_fuel(
CILNode::IntCast {
input: *input2,
target,
extend: *extend2,
},
original,
fuel,
)
}
(Int::U32 | Int::I32, Int::U32 | Int::I32) => {
// A u64 to i64 cast does nothing, except change the type on the evaulation stack(the bits are unchanged).
// So, we can just create a cast like it.
opt_if_fuel(
CILNode::IntCast {
input: *input2,
target,
extend: *extend2,
},
original,
fuel,
)
}
_ => original,
}
}
_ => original,
}
}
pub fn opt_node(original: CILNode, asm: &mut Assembly, fuel: &mut OptFuel) -> CILNode {
match original {
CILNode::SizeOf(tpe) => match asm[tpe] {
Expand All @@ -27,87 +111,7 @@ pub fn opt_node(original: CILNode, asm: &mut Assembly, fuel: &mut OptFuel) -> CI
input,
target,
extend,
} => match asm.get_node(input) {
CILNode::Const(cst) => match (cst.as_ref(), target) {
(Const::U64(val), Int::USize) => {
opt_if_fuel(Const::USize(*val).into(), original, fuel)
}
(Const::I64(val), Int::ISize) => {
opt_if_fuel(Const::ISize(*val).into(), original, fuel)
}
(Const::U64(val), Int::U64) => opt_if_fuel(Const::U64(*val).into(), original, fuel),
(Const::I64(val), Int::I64) => opt_if_fuel(Const::I64(*val).into(), original, fuel),
(Const::U32(val), Int::U32) => opt_if_fuel(Const::U32(*val).into(), original, fuel),
(Const::I32(val), Int::I32) => opt_if_fuel(Const::I32(*val).into(), original, fuel),
(Const::I32(val), Int::U32) => {
opt_if_fuel(Const::U32(*val as u32).into(), original, fuel)
}
(Const::U64(val), Int::U8) => {
opt_if_fuel(Const::U8(*val as u8).into(), original, fuel)
}
(Const::I32(val), Int::USize) => match extend {
ExtendKind::SignExtend => {
opt_if_fuel(Const::USize(*val as i64 as u64).into(), original, fuel)
}
ExtendKind::ZeroExtend => {
opt_if_fuel(Const::USize(*val as u32 as u64).into(), original, fuel)
}
},
_ => original,
},
CILNode::IntCast {
input: input2,
target: target2,
extend: extend2,
} => {
if target == *target2 && extend == *extend2 {
return opt_if_fuel(asm.get_node(input).clone(), original, fuel);
}
match (target, target2) {
(Int::USize | Int::ISize, Int::USize | Int::ISize) => {
// A usize to isize cast does nothing, except change the type on the evaulation stack(the bits are unchanged).
// So, we can just create a cast like it.
opt_if_fuel(
CILNode::IntCast {
input: *input2,
target,
extend: *extend2,
},
original,
fuel,
)
}
(Int::U64 | Int::I64, Int::U64 | Int::I64) => {
// A u64 to i64 cast does nothing, except change the type on the evaulation stack(the bits are unchanged).
// So, we can just create a cast like it.
opt_if_fuel(
CILNode::IntCast {
input: *input2,
target,
extend: *extend2,
},
original,
fuel,
)
}
(Int::U32 | Int::I32, Int::U32 | Int::I32) => {
// A u64 to i64 cast does nothing, except change the type on the evaulation stack(the bits are unchanged).
// So, we can just create a cast like it.
opt_if_fuel(
CILNode::IntCast {
input: *input2,
target,
extend: *extend2,
},
original,
fuel,
)
}
_ => original,
}
}
_ => original,
},
} => opt_int_cast(original, asm, fuel, input, target, extend),
CILNode::Call(info) => super::inline::trivial_inline_call(info.0, &info.1, fuel, asm),
CILNode::LdInd {
addr,
Expand Down Expand Up @@ -142,7 +146,6 @@ pub fn opt_node(original: CILNode, asm: &mut Assembly, fuel: &mut OptFuel) -> CI
}
_ => original,
},

CILNode::LdField { addr, field } => match asm.get_node(addr) {
CILNode::RefToPtr(addr) => {
opt_if_fuel(CILNode::LdField { addr: *addr, field }, original, fuel)
Expand Down
1 change: 0 additions & 1 deletion cilly/src/v2/opt/simplify_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ fn block_gc(blocks: &mut Vec<BasicBlock>, asm: &Assembly) {
.cloned()
.collect();
}

pub fn simplify_bbs(
handler: Option<&mut Vec<BasicBlock>>,
asm: &mut Assembly,
Expand Down
4 changes: 2 additions & 2 deletions cilly/src/v2/typecheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,6 @@ fn test() {
let lhs = super::Const::I64(0);
let rhs = super::Const::F64(super::hashable::HashableF64(0.0));
let sum = asm.biop(lhs, rhs, BinOp::Add);
let sum = asm.alloc_node(sum);
let sig = asm.sig([], Type::Void);
let _sum = asm.alloc_node(sum);
let _sig = asm.sig([], Type::Void);
}
4 changes: 1 addition & 3 deletions src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,7 @@ fn load_scalar_ptr(
// If it is a function, patch its pointer up.
let call_info = crate::call_info::CallInfo::sig_from_instance_(finstance, ctx);
let function_name = crate::utilis::function_name(ctx.tcx().symbol_name(finstance));
return CILNode::LDFtn(
CallSite::new(None, function_name, call_info.sig().clone(), true).into(),
);
CILNode::LDFtn(CallSite::new(None, function_name, call_info.sig().clone(), true).into())
}
GlobalAlloc::VTable(..) => todo!("Unhandled global alloc {global_alloc:?}"),
}
Expand Down
Loading

0 comments on commit ac20e7f

Please sign in to comment.