Remove old, unused code from vm; remove pub from various struct fields.

This commit is contained in:
Jesse Brault 2024-12-30 12:50:55 -06:00
parent 258fe824bd
commit 68553a756b
9 changed files with 92 additions and 96 deletions

View File

@ -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));

View File

@ -8,17 +8,3 @@ pub enum DmType {
Object,
USize,
}
impl DmType {
pub fn size_in_bytes(&self) -> usize {
match self {
DmType::Byte => size_of::<u8>(),
DmType::Int => size_of::<i32>(),
DmType::Long => size_of::<i64>(),
DmType::Double => size_of::<f64>(),
DmType::Boolean => size_of::<bool>(),
DmType::Object => size_of::<usize>(),
DmType::USize => size_of::<usize>(),
}
}
}

View File

@ -23,6 +23,13 @@ impl DvmValue {
}
}
pub fn is_long(&self) -> bool {
match self {
DvmValue::Long(_) => true,
_ => false,
}
}
pub fn expect_object(&self) -> Rc<DvmObject> {
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,
}
}
}

View File

@ -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) };

View File

@ -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);

View File

@ -24,8 +24,8 @@ fn layout_for(dm_type: &DmType) -> Layout {
impl DvmObject {
pub fn new(implementation: Rc<DmImplementation>) -> 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())) }
}
}

View File

@ -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<Rc<DmVirtualMethod>> {
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<Rc<DmInterface>>,
pub functions: Vec<Rc<DmFn>>,
pub methods: Vec<Rc<DmMethod>>,
pub fields: Vec<DmField>,
fqn: String,
short_name: String,
interface: Option<Rc<DmInterface>>,
functions: Vec<Rc<DmFn>>,
methods: Vec<Rc<DmMethod>>,
fields: Vec<DmField>,
}
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<Rc<DmInterface>> {
self.interface.clone()
}
pub fn get_method(&self, name: &str, self_object: &DvmObject) -> Option<Rc<DmMethod>> {
pub fn functions(&self) -> Vec<Rc<DmFn>> {
self.functions.clone()
}
pub fn methods(&self) -> Vec<Rc<DmMethod>> {
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<Rc<DmMethod>> {
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<Rc<DmProperty>> {
todo!()
pub fn fields(&self) -> &Vec<DmField> {
&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<DmType>,
}
impl PartialEq for DmProperty {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
}
}
#[derive(Debug, Eq)]
pub struct DmField {
name: String,

View File

@ -35,7 +35,7 @@ fn get_string(dvm_value: &DvmValue) -> String {
}
fn object_to_string(alloc_object_rc: Rc<DvmObject>) -> 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<DvmObject>) -> String {
fn extract_string_from_string(string_object: Rc<DvmObject>) -> 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");
}

View File

@ -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))
}
}