From 68553a756b8cb85cfc38e1706716eb423b8ce47e Mon Sep 17 00:00:00 2001 From: Jesse Brault Date: Mon, 30 Dec 2024 12:50:55 -0600 Subject: [PATCH] Remove old, unused code from vm; remove pub from various struct fields. --- src/bin/dvm/main.rs | 12 ++--- src/vm/dm_type.rs | 14 ------ src/vm/dvm_value.rs | 14 ++++++ src/vm/mem.rs | 10 ++-- src/vm/mod.rs | 17 ++++--- src/vm/object.rs | 6 +-- src/vm/object_type.rs | 84 +++++++++++++++++++++------------ src/vm/platform/std_lib/core.rs | 4 +- src/vm/pointer.rs | 27 ----------- 9 files changed, 92 insertions(+), 96 deletions(-) delete mode 100644 src/vm/pointer.rs diff --git a/src/bin/dvm/main.rs b/src/bin/dvm/main.rs index a386e64..aaee09a 100644 --- a/src/bin/dvm/main.rs +++ b/src/bin/dvm/main.rs @@ -32,9 +32,9 @@ fn main() { let array_impl_ptr_size_fld = DmField::new("ptr_size", DmType::USize); let array_impl_length_fld = DmField::new("length", DmType::Long); - array_impl.fields.push(array_impl_ptr_address_fld); - array_impl.fields.push(array_impl_ptr_size_fld); - array_impl.fields.push(array_impl_length_fld); + array_impl.add_field(array_impl_ptr_address_fld); + array_impl.add_field(array_impl_ptr_size_fld); + array_impl.add_field(array_impl_length_fld); // std::core::ArrayImpl::_ctor_0( // r0: self @@ -56,7 +56,7 @@ fn main() { None, ); let array_impl_ctor_0_method = DmMethod::new(array_impl_ctor_0_fn, None); - array_impl.methods.push(Rc::new(array_impl_ctor_0_method)); + array_impl.add_method(array_impl_ctor_0_method); // Add Array and ArrayImpl to array lib array_lib.interfaces.push(array_int_rc.clone()); @@ -71,7 +71,7 @@ fn main() { let mut string_impl = DmImplementation::new("std::core::StringImpl", "StringImpl", None); let bytes_field = DmField::new("bytes", DmType::Object); - string_impl.fields.push(bytes_field); + string_impl.add_field(bytes_field); // std::core::String::_ctor_0( // r0: self @@ -90,7 +90,7 @@ fn main() { let string_ctor_0_method = DmMethod::new(string_ctor_0_fn, None); - string_impl.methods.push(Rc::new(string_ctor_0_method)); + string_impl.add_method(string_ctor_0_method); string_lib.interfaces.push(Rc::new(string_int)); string_lib.implementations.push(Rc::new(string_impl)); diff --git a/src/vm/dm_type.rs b/src/vm/dm_type.rs index 7204c61..d88a726 100644 --- a/src/vm/dm_type.rs +++ b/src/vm/dm_type.rs @@ -8,17 +8,3 @@ pub enum DmType { Object, USize, } - -impl DmType { - pub fn size_in_bytes(&self) -> usize { - match self { - DmType::Byte => size_of::(), - DmType::Int => size_of::(), - DmType::Long => size_of::(), - DmType::Double => size_of::(), - DmType::Boolean => size_of::(), - DmType::Object => size_of::(), - DmType::USize => size_of::(), - } - } -} diff --git a/src/vm/dvm_value.rs b/src/vm/dvm_value.rs index aa15d05..7931766 100644 --- a/src/vm/dvm_value.rs +++ b/src/vm/dvm_value.rs @@ -23,6 +23,13 @@ impl DvmValue { } } + pub fn is_long(&self) -> bool { + match self { + DvmValue::Long(_) => true, + _ => false, + } + } + pub fn expect_object(&self) -> Rc { if let DvmValue::Object(o) = self { o.clone() @@ -45,4 +52,11 @@ impl DvmValue { panic!("Expected DvmValue::USize, but found DvmValue::{:?}", self); } } + + pub fn is_usize(&self) -> bool { + match self { + DvmValue::USize(_) => true, + _ => false, + } + } } diff --git a/src/vm/mem.rs b/src/vm/mem.rs index f47cfcc..36143b3 100644 --- a/src/vm/mem.rs +++ b/src/vm/mem.rs @@ -18,21 +18,21 @@ unsafe fn read_field(data_pointer: *const u8, dm_type: &DmType) -> DvmValue { pub fn read_field_by_name(field_name: &str, self_object: &DvmObject) -> DvmValue { let (field_index, field) = self_object .implementation() - .fields + .fields() .iter() .enumerate() .find(|(_index, field)| field.name() == field_name) .expect(&format!( "Cannot find field {} in {}", field_name, - self_object.implementation().fqn + self_object.implementation().fqn() )); let data_pointer = self_object.data()[field_index]; unsafe { read_field(data_pointer, field.dm_type()) } } pub fn read_field_by_index(index: usize, self_object: &DvmObject) -> DvmValue { - let field = &self_object.implementation().fields[index]; + let field = &self_object.implementation().fields()[index]; let data_pointer = self_object.data()[index]; unsafe { read_field(data_pointer, field.dm_type()) } } @@ -68,13 +68,13 @@ unsafe fn write_field(data_pointer: *mut u8, value: DvmValue) { pub fn write_field_by_name(field_name: &str, self_object: &DvmObject, value: DvmValue) { let field_index = self_object .implementation() - .fields + .fields() .iter() .position(|field| field.name() == field_name) .expect(&format!( "Cannot find field {} in {}", field_name, - self_object.implementation().fqn + self_object.implementation().fqn() )); let data_pointer = self_object.data()[field_index]; unsafe { write_field(data_pointer, value) }; diff --git a/src/vm/mod.rs b/src/vm/mod.rs index 77f243c..2cbb988 100644 --- a/src/vm/mod.rs +++ b/src/vm/mod.rs @@ -2,11 +2,10 @@ pub mod dm_type; pub mod dvm_value; pub mod lib; pub mod mem; -mod object; +pub mod object; pub mod object_type; pub mod op_codes; pub mod platform; -mod pointer; pub mod util; use crate::vm::dvm_value::DvmValue; @@ -68,7 +67,7 @@ impl DvmContext { for implementation_function in lib .implementations .iter() - .flat_map(|implementation| &implementation.functions) + .flat_map(|implementation| implementation.functions()) { self.functions.insert( implementation_function.fqn().to_string(), @@ -78,7 +77,7 @@ impl DvmContext { for method in lib .implementations .iter() - .flat_map(|implementation| &implementation.methods) + .flat_map(|implementation| implementation.methods()) { self.functions .insert(method.dm_fn().fqn().to_string(), method.dm_fn().clone()); @@ -324,7 +323,7 @@ pub fn run_byte_code(state: &mut DvmState, context: &DvmContext, byte_code: &[u8 .find_map(|lib| { lib.implementations .iter() - .find(|implementation| implementation.fqn == impl_name) + .find(|implementation| implementation.fqn() == impl_name) }) .unwrap_or_else(|| { dvm_panic!(&format!("Implementation not found: {}", impl_name), state) @@ -446,7 +445,7 @@ pub fn run_byte_code(state: &mut DvmState, context: &DvmContext, byte_code: &[u8 let self_obj = args.get(0).unwrap().expect_object(); let method = self_obj .implementation() - .get_method(&symbol_name, &self_obj) + .find_method(&symbol_name, &self_obj) .unwrap_or_else(|| { dvm_panic!( &format!("Could not find method with name: {}", symbol_name), @@ -500,7 +499,7 @@ mod run_code_tests { fn impl_with_object_field() -> DmImplementation { let mut dm_impl = DmImplementation::new("ImplWithObjectField", "ImplWithObjectField", None); let object_field = DmField::new("object_field", DmType::Object); - dm_impl.fields.push(object_field); + dm_impl.add_field(object_field); dm_impl } @@ -549,8 +548,8 @@ mod run_code_tests { dummy_lib.implementations.push(dummy_impl_rc.clone()); let mut code = Vec::new(); - add_alloc(&mut code, 0, &dummy_impl_rc.fqn); - add_alloc(&mut code, 1, &dummy_impl_rc.fqn); + add_alloc(&mut code, 0, &dummy_impl_rc.fqn()); + add_alloc(&mut code, 1, &dummy_impl_rc.fqn()); add_store(&mut code, 0, 0, 1); add_load(&mut code, 2, 0, 0); diff --git a/src/vm/object.rs b/src/vm/object.rs index 5275d15..9951be3 100644 --- a/src/vm/object.rs +++ b/src/vm/object.rs @@ -24,8 +24,8 @@ fn layout_for(dm_type: &DmType) -> Layout { impl DvmObject { pub fn new(implementation: Rc) -> Self { - let mut data = vec![ptr::null_mut(); implementation.fields.len()]; - for (index, field) in implementation.fields.iter().enumerate() { + let mut data = vec![ptr::null_mut(); implementation.fields().len()]; + for (index, field) in implementation.fields().iter().enumerate() { data[index] = unsafe { alloc(layout_for(field.dm_type())) } } DvmObject { @@ -45,7 +45,7 @@ impl DvmObject { impl Drop for DvmObject { fn drop(&mut self) { - for (index, field) in self.implementation.fields.iter().enumerate() { + for (index, field) in self.implementation.fields().iter().enumerate() { unsafe { dealloc(self.data[index], layout_for(field.dm_type())) } } } diff --git a/src/vm/object_type.rs b/src/vm/object_type.rs index 36854a3..7ebb11a 100644 --- a/src/vm/object_type.rs +++ b/src/vm/object_type.rs @@ -138,7 +138,7 @@ impl DmInterface { .find(|&dm_fn| dm_fn.fqn == name) .is_some() { - return self_object.implementation().get_method(name, self_object); + return self_object.implementation().find_method(name, self_object); } None } @@ -146,16 +146,12 @@ impl DmInterface { pub fn get_virtual_methods(&self) -> Vec> { self.virtual_methods.clone() } - - pub fn get_property(&self, name: &str, self_object: &DvmObject) -> Option<&DmProperty> { - todo!() - } } #[derive(Debug, Eq)] pub struct DmVirtualMethod { - pub fqn: String, - pub short_name: String, + fqn: String, + short_name: String, } impl PartialEq for DmVirtualMethod { @@ -164,14 +160,31 @@ impl PartialEq for DmVirtualMethod { } } +impl DmVirtualMethod { + pub fn new(fqn: &str, short_name: &str) -> Self { + DmVirtualMethod { + fqn: fqn.to_string(), + short_name: short_name.to_string(), + } + } + + pub fn fqn(&self) -> &str { + self.fqn.as_str() + } + + pub fn short_name(&self) -> &str { + self.short_name.as_str() + } +} + #[derive(Debug, Eq)] pub struct DmImplementation { - pub fqn: String, - pub short_name: String, - pub interface: Option>, - pub functions: Vec>, - pub methods: Vec>, - pub fields: Vec, + fqn: String, + short_name: String, + interface: Option>, + functions: Vec>, + methods: Vec>, + fields: Vec, } impl PartialEq for DmImplementation { @@ -192,11 +205,31 @@ impl DmImplementation { } } + pub fn fqn(&self) -> &str { + self.fqn.as_str() + } + + pub fn short_name(&self) -> &str { + self.short_name.as_str() + } + pub fn interface(&self) -> Option> { self.interface.clone() } - pub fn get_method(&self, name: &str, self_object: &DvmObject) -> Option> { + pub fn functions(&self) -> Vec> { + self.functions.clone() + } + + pub fn methods(&self) -> Vec> { + self.methods.clone() + } + + pub fn add_method(&mut self, dm_method: DmMethod) { + self.methods.push(Rc::new(dm_method)); + } + + pub fn find_method(&self, name: &str, self_object: &DvmObject) -> Option> { for method in &self.methods { if method.dm_fn.fqn == name { return Some(method.clone()); @@ -209,28 +242,19 @@ impl DmImplementation { None } - pub fn get_property(&self, name: &str, self_object: &DvmObject) -> Option> { - todo!() + pub fn fields(&self) -> &Vec { + &self.fields } - pub fn get_field(&self, name: &str, self_object: &DvmObject) -> Option<&DmField> { + pub fn add_field(&mut self, dm_field: DmField) { + self.fields.push(dm_field); + } + + pub fn find_field(&self, name: &str, self_object: &DvmObject) -> Option<&DmField> { self.fields.iter().find(|field| field.name == name) } } -#[derive(Debug, Eq)] -pub struct DmProperty { - pub name: String, - pub data_offset: usize, - pub primitive_type: Rc, -} - -impl PartialEq for DmProperty { - fn eq(&self, other: &Self) -> bool { - self.name == other.name - } -} - #[derive(Debug, Eq)] pub struct DmField { name: String, diff --git a/src/vm/platform/std_lib/core.rs b/src/vm/platform/std_lib/core.rs index 208e7df..2617a80 100644 --- a/src/vm/platform/std_lib/core.rs +++ b/src/vm/platform/std_lib/core.rs @@ -35,7 +35,7 @@ fn get_string(dvm_value: &DvmValue) -> String { } fn object_to_string(alloc_object_rc: Rc) -> String { - if alloc_object_rc.implementation().fqn == "std::core::StringImpl" { + if alloc_object_rc.implementation().fqn() == "std::core::StringImpl" { extract_string_from_string(alloc_object_rc.clone()) } else { todo!("what happens if we don't have a String?") @@ -44,7 +44,7 @@ fn object_to_string(alloc_object_rc: Rc) -> String { fn extract_string_from_string(string_object: Rc) -> String { let bytes_object = read_field_by_name("bytes", &string_object).expect_object(); - if bytes_object.implementation().fqn != "std::core::ArrayImpl" { + if bytes_object.implementation().fqn() != "std::core::ArrayImpl" { panic!("String.bytes field is not a std::core::ArrayImpl"); } diff --git a/src/vm/pointer.rs b/src/vm/pointer.rs deleted file mode 100644 index 43fc7ec..0000000 --- a/src/vm/pointer.rs +++ /dev/null @@ -1,27 +0,0 @@ -use std::fmt::{Display, Formatter}; - -#[derive(Debug, PartialEq, Eq)] -pub struct DvmPointer { - address: *mut u8, - size: usize, -} - -impl DvmPointer { - pub fn new(address: *mut u8, size: usize) -> DvmPointer { - DvmPointer { address, size } - } - - pub fn address(&self) -> *mut u8 { - self.address - } - - pub fn size(&self) -> usize { - self.size - } -} - -impl Display for DvmPointer { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - f.write_fmt(format_args!("0x{:x}", self.address as usize)) - } -}