Skip to content

Commit

Permalink
Switched to a new field representation
Browse files Browse the repository at this point in the history
  • Loading branch information
FractalFir committed Sep 28, 2024
1 parent eeb6b57 commit 5f0a62a
Show file tree
Hide file tree
Showing 37 changed files with 541 additions and 617 deletions.
4 changes: 2 additions & 2 deletions cilly/src/bin/interpreter/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use cilly::{
cil_node::CILNode,
cil_root::{CILRoot, SFI},
method::Method,
static_field_desc::StaticFieldDescriptor,
static_field_desc::StaticFieldDesc,
AsmStringContainer, FnSig, Type,
};
mod value;
Expand All @@ -29,7 +29,7 @@ struct InterpreterState<'asm> {
locals: Vec<Box<[Value]>>,
mem: FxHashMap<AllocID, Box<[u8]>>,
last_alloc: AllocID,
fields: FxHashMap<StaticFieldDescriptor, Value>,
fields: FxHashMap<StaticFieldDesc, Value>,
methods: FxHashMap<AllocID, CallSite>,
inv_methods: FxHashMap<CallSite, AllocID>,
last_alloc_method: AllocID,
Expand Down
13 changes: 5 additions & 8 deletions cilly/src/cil_node.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use crate::v2::{Assembly, ClassRef, ClassRefIdx, FnSig, Int, Type};
use crate::v2::{Assembly, ClassRef, ClassRefIdx, FieldIdx, FnSig, Int, StaticFieldDesc, Type};
use crate::{
call,
call_site::CallSite,
cil_iter::CILIterTrait,
cil_root::CILRoot,
field_desc::FieldDescriptor,
static_field_desc::StaticFieldDescriptor,
v2::hashable::{HashableF32, HashableF64},
IString,
};
Expand All @@ -30,8 +28,7 @@ pub enum CILNode {
/// A black box that prevents the bulit-in optimization engine from doing any optimizations.
BlackBox(Box<Self>),
/// Loads the value of a static variable described by the descripstor.
LDStaticField(Box<StaticFieldDescriptor>),

LDStaticField(Box<StaticFieldDesc>),
/// Converts the signed inner value to a 32 bit floating-point number.
ConvF32(Box<Self>),
/// Converts the signed inner value to a 64 bit floating-point number.
Expand Down Expand Up @@ -104,13 +101,13 @@ pub enum CILNode {
LDFieldAdress {
/// Address of the object
addr: Box<Self>,
field: Box<FieldDescriptor>,
field: FieldIdx,
},
/// Loads the value of `field` of the object `addr` points to
LDField {
/// Address of the object
addr: Box<Self>,
field: Box<FieldDescriptor>,
field: FieldIdx,
},
/// Adds 2 values together
Add(Box<Self>, Box<Self>),
Expand Down Expand Up @@ -230,7 +227,7 @@ pub enum CILNode {
/// Marks the inner pointer operation as volatile.
Volatile(Box<Self>),
UnboxAny(Box<Self>, Box<Type>),
AddressOfStaticField(Box<StaticFieldDescriptor>),
AddressOfStaticField(Box<StaticFieldDesc>),
LdNull(ClassRefIdx),
}

Expand Down
12 changes: 5 additions & 7 deletions cilly/src/cil_root.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use crate::v2::{Assembly, ClassRef, FnSig, Type};
use crate::v2::{Assembly, ClassRef, FieldIdx, FnSig, StaticFieldDesc, Type};
use crate::{
call,
call_site::CallSite,
cil_node::{CILNode, CallOpArgs},
field_desc::FieldDescriptor,
static_field_desc::StaticFieldDescriptor,
AsmString, IString,
};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -84,7 +82,7 @@ pub enum CILRoot {
SetField {
addr: Box<CILNode>,
value: Box<CILNode>,
desc: Box<FieldDescriptor>,
desc: FieldIdx,
},
SetTMPLocal {
value: CILNode,
Expand Down Expand Up @@ -142,7 +140,7 @@ pub enum CILRoot {
target: u32,
},
SetStaticField {
descr: Box<StaticFieldDescriptor>,
descr: Box<StaticFieldDesc>,
value: CILNode,
},
SourceFileInfo(SFI),
Expand Down Expand Up @@ -371,11 +369,11 @@ impl CILRoot {
Self::SourceFileInfo(Box::new((line, column, file.to_owned().into())))
}
#[must_use]
pub fn set_field(addr: CILNode, value: CILNode, desc: FieldDescriptor) -> Self {
pub fn set_field(addr: CILNode, value: CILNode, desc: FieldIdx) -> Self {
Self::SetField {
addr: Box::new(addr),
value: Box::new(value),
desc: Box::new(desc),
desc,
}
}
}
Expand Down
37 changes: 0 additions & 37 deletions cilly/src/field_desc.rs

This file was deleted.

4 changes: 1 addition & 3 deletions cilly/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![allow(clippy::module_name_repetitions)]
#![feature(iter_intersperse, pattern)]
pub mod field_desc;

pub use crate::v2::Type;
use fxhash::FxHasher;
Expand Down Expand Up @@ -58,8 +57,7 @@ pub mod entrypoint;

pub mod libc_fns;
pub mod method;
pub mod static_field_desc;
//pub mod type_def;

pub mod utilis;
pub mod v2;

Expand Down
6 changes: 3 additions & 3 deletions cilly/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use crate::{
cil_node::CILNode,
cil_root::CILRoot,
cil_tree::CILTree,
static_field_desc::StaticFieldDescriptor,
v2::{Assembly, FnSig},
IString, Type,
};
Expand Down Expand Up @@ -283,9 +282,10 @@ impl Method {
.flat_map(|block| block.iter_cil())
.call_sites()
}
/*
/// Returns the list of static fields this function references. Calls may repeat.
// TODO: make this not call `into_ops`
pub fn sflds(&self) -> impl Iterator<Item = &StaticFieldDescriptor> {
pub fn sflds(&self) -> impl Iterator<Item = &StaticFieldDesc> {
self.blocks
.iter()
.flat_map(|block| block.iter_cil())
Expand All @@ -295,7 +295,7 @@ impl Method {
CILIterElem::Root(CILRoot::SetStaticField { descr, value: _ }) => Some(descr),
_ => None,
})
}
}*/

/// Returns a call site that describes this method.
pub fn call_site(&self) -> CallSite {
Expand Down
37 changes: 0 additions & 37 deletions cilly/src/static_field_desc.rs

This file was deleted.

49 changes: 33 additions & 16 deletions cilly/src/utilis.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::fmt::Debug;

use crate::method::Method;
use crate::static_field_desc::StaticFieldDescriptor;
use crate::v2::{ClassRef, FnSig, Int};

use crate::v2::{ClassRef, FnSig, Int, StaticFieldDesc};
use crate::{
asm::Assembly, call_site::CallSite, cil_node::CILNode, cil_root::CILRoot, eq, lt, size_of,
};
use crate::{call, call_virt, conv_i32, conv_usize, ldc_i32, ldc_u32, mul, Type};

pub fn argc_argv_init_method(asm: &mut Assembly) -> CallSite {
use std::num::NonZeroU8;
let init_cs = CallSite::new(
Expand Down Expand Up @@ -90,12 +91,16 @@ pub fn argc_argv_init_method(asm: &mut Assembly) -> CallSite {

// Fill up the start block
let start_block = &mut blocks[start_bb as usize];
let status = StaticFieldDescriptor::new(None, Type::Bool, "argv_argc_init_status".into());
let status = StaticFieldDesc::new(
*asm.main_module(),
asm.alloc_string("argv_argc_init_status"),
Type::Bool,
);
start_block.trees_mut().push(
CILRoot::BTrue {
target: start_bb + 3,
sub_target: 0,
cond: CILNode::LDStaticField(Box::new(status.clone())),
cond: CILNode::LDStaticField(Box::new(status)),
}
.into(),
);
Expand Down Expand Up @@ -177,15 +182,23 @@ pub fn argc_argv_init_method(asm: &mut Assembly) -> CallSite {
let loop_end_bb = init_method.new_bb();
let mut blocks = init_method.blocks_mut();
let loop_end_block = &mut blocks[loop_end_bb as usize];
let argv_static = StaticFieldDescriptor::new(None, asm.nptr(uint8_ptr), "argv".into());
let argv_static = StaticFieldDesc::new(
*asm.main_module(),
asm.alloc_string("argv"),
asm.nptr(uint8_ptr),
);
loop_end_block.trees_mut().push(
CILRoot::SetStaticField {
descr: Box::new(argv_static),
value: CILNode::LDLoc(argv),
}
.into(),
);
let argc_static = StaticFieldDescriptor::new(None, Type::Int(Int::I32), "argc".into());
let argc_static = StaticFieldDesc::new(
*asm.main_module(),
asm.alloc_string("argc"),
Type::Int(Int::I32),
);
loop_end_block.trees_mut().push(
CILRoot::SetStaticField {
descr: Box::new(argc_static),
Expand Down Expand Up @@ -233,6 +246,7 @@ pub fn mstring_to_utf8ptr(mstring: CILNode, asm: &mut Assembly) -> CILNode {
)
.cast_ptr(asm.nptr(Type::Int(Int::U8)))
}

pub fn get_environ(asm: &mut Assembly) -> CallSite {
let uint8_ptr = asm.nptr(Type::Int(Int::U8));
let uint8_ptr_ptr = asm.nptr(uint8_ptr);
Expand Down Expand Up @@ -283,9 +297,11 @@ pub fn get_environ(asm: &mut Assembly) -> CallSite {
CILRoot::BNe {
target: ret_bb,
sub_target: 0,
a: Box::new(CILNode::LDStaticField(Box::new(
StaticFieldDescriptor::new(None, uint8_ptr_ptr, "environ".into()),
))),
a: Box::new(CILNode::LDStaticField(Box::new(StaticFieldDesc::new(
*asm.main_module(),
asm.alloc_string("environ"),
uint8_ptr_ptr,
)))),
b: Box::new(conv_usize!(ldc_u32!(0)).cast_ptr(uint8_ptr_ptr)),
}
.into(),
Expand Down Expand Up @@ -378,10 +394,10 @@ pub fn get_environ(asm: &mut Assembly) -> CallSite {
let ret = &mut blocks[ret_bb as usize];
ret.trees_mut().push(
CILRoot::Ret {
tree: CILNode::LDStaticField(Box::new(StaticFieldDescriptor::new(
None,
tree: CILNode::LDStaticField(Box::new(StaticFieldDesc::new(
*asm.main_module(),
asm.alloc_string("environ"),
uint8_ptr_ptr,
"environ".into(),
))),
}
.into(),
Expand Down Expand Up @@ -509,10 +525,10 @@ pub fn get_environ(asm: &mut Assembly) -> CallSite {
);
loop_end.trees_mut().push(
CILRoot::SetStaticField {
descr: Box::new(StaticFieldDescriptor::new(
None,
descr: Box::new(StaticFieldDesc::new(
*asm.main_module(),
asm.alloc_string("environ"),
uint8_ptr_ptr,
"environ".into(),
)),
value: CILNode::LDLoc(arr_ptr),
}
Expand Down Expand Up @@ -571,11 +587,12 @@ pub fn escape_class_name(name: &str) -> String {
.replace('!', "_excl_")
.replace('\"', "_qt_")
}
/*
#[test]
fn argv() {
let mut asm = Assembly::empty();
argc_argv_init_method(&mut asm);
}
} */

#[test]
fn environ() {
Expand Down
2 changes: 1 addition & 1 deletion cilly/src/v2/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ impl Assembly {
self.nodes.get(key)
}

pub(crate) fn alloc_field(&mut self, field: FieldDesc) -> FieldIdx {
pub fn alloc_field(&mut self, field: FieldDesc) -> FieldIdx {
self.fields.alloc(field)
}
#[must_use]
Expand Down
Loading

0 comments on commit 5f0a62a

Please sign in to comment.