From 4c744bf2d69cd9f1155152525a57574c71078978 Mon Sep 17 00:00:00 2001 From: Jesse Brault Date: Thu, 30 Jul 2026 21:05:13 -0500 Subject: [PATCH] Sketching mem. --- dvm-lib/mem.md | 49 ++++++++++++++ dvm-lib/src/lib.rs | 2 + dvm-lib/src/vm/mem.rs | 148 ++++++++++++++++++++++++++++++++++++++++++ dvm-lib/src/vm/mod.rs | 1 + 4 files changed, 200 insertions(+) create mode 100644 dvm-lib/mem.md create mode 100644 dvm-lib/src/vm/mem.rs diff --git a/dvm-lib/mem.md b/dvm-lib/mem.md new file mode 100644 index 0000000..dc97742 --- /dev/null +++ b/dvm-lib/mem.md @@ -0,0 +1,49 @@ +# Object/Memory/Heap Concepts for Deimos/DVM + +```rust +use std::any::TypeId; + +trait Trace { + fn grays(&self) -> &[GcAny]; +} + +struct Gc { + inner: *mut GcInner, +} + +impl Gc { + fn data(&self) -> &T { + &self.inner.data + } +} + +struct GcMut { + inner: *mut GcInner, +} + +struct GcInner { + data: T, + gc_count: usize, + gc_mut_count: bool, + next: Option, + color: bool, +} + +struct GcAny { + inner: *mut (), // like void* in C + v_table: *const GcAnyVTable, + type_id: *const TypeId, +} + +enum Value { + Object(Gc), + Int(i32), + String(Gc), + Null +} + +struct Object { + class: *const Class, + +} +``` \ No newline at end of file diff --git a/dvm-lib/src/lib.rs b/dvm-lib/src/lib.rs index 88c25a3..03b2088 100644 --- a/dvm-lib/src/lib.rs +++ b/dvm-lib/src/lib.rs @@ -1,3 +1,5 @@ +#![feature(ptr_metadata)] + pub mod instruction; pub mod platform_function; pub mod vm; diff --git a/dvm-lib/src/vm/mem.rs b/dvm-lib/src/vm/mem.rs new file mode 100644 index 0000000..4c7431e --- /dev/null +++ b/dvm-lib/src/vm/mem.rs @@ -0,0 +1,148 @@ +use std::any::TypeId; +use std::ptr::Pointee; +use std::rc::Rc; +use std::sync::OnceLock; + +trait Trace { + fn grays(&self) -> Vec; +} + +struct Gc { + inner: *mut GcInner, +} + +impl Gc { + fn from_inner(inner: *mut GcInner) -> Self { + Self { inner } + } + + fn data(&self) -> &T { + unsafe { &(*self.inner).data } + } + + fn data_mut(&mut self) -> &mut T { + unsafe { &mut (*self.inner).data } + } +} + +impl Gc { + fn erase(&self) -> GcAny { + GcAny::from_gc_inner(self.inner) + } +} + +struct GcMut { + inner: *mut GcInner, +} + +struct GcInner { + borrow_count: usize, + borrow_mut_count: bool, + next: Option, + color: bool, + data: T, +} + +impl GcInner { + fn new(data: T) -> Self { + Self { + borrow_count: 0, + borrow_mut_count: false, + next: None, + color: false, + data, + } + } +} + +struct GcAny { + inner_ptr: *mut (), // like void* in C + inner_ptr_metadata: *const (), // type-erased metadata + v_table: *const GcAnyVTable, + type_id: *const TypeId, +} + +impl GcAny { + fn from_gc_inner(gc_inner: *mut GcInner) -> Self { + fn trace( + inner_ptr: *const (), + inner_ptr_metadata: *const (), + ) -> Vec { + let metadata: as Pointee>::Metadata = + unsafe { std::mem::transmute_copy(&inner_ptr_metadata) }; + let gc_inner: *const GcInner = std::ptr::from_raw_parts(inner_ptr, metadata); + unsafe { &*gc_inner }.data.grays() + } + + let (inner_ptr, metadata) = gc_inner.to_raw_parts(); + let inner_pointer_metadata = unsafe { std::mem::transmute_copy(&metadata) }; + + static V_TABLE: OnceLock = OnceLock::new(); + let v_table = V_TABLE.get_or_init(|| GcAnyVTable { trace: trace:: }); + + static TYPE_ID: OnceLock = OnceLock::new(); + let type_id = TYPE_ID.get_or_init(|| TypeId::of::()); + + Self { + inner_ptr, + inner_ptr_metadata: inner_pointer_metadata, + v_table, + type_id, + } + } + + fn trace(&self) -> Vec { + ((unsafe { &*self.v_table }).trace)(self.inner_ptr, self.inner_ptr_metadata) + } + + fn is(&self) -> bool { + unsafe { *self.type_id == TypeId::of::() } + } + + fn downcast(&self) -> Option> { + if self.is::() { + Some(Gc::from_inner(self.inner_ptr as *mut GcInner)) + } else { + None + } + } +} + +struct GcAnyVTable { + trace: fn(*const (), *const ()) -> Vec, +} + +enum Value { + Object(Gc), + Int(i32), + String(Gc), + Null, +} + +struct Class { + name: Rc, +} + +#[repr(C)] +struct Object { + class: *const Class, + fields: [Value], +} + +impl Trace for Object { + fn grays(&self) -> Vec { + vec![] // todo + } +} + +impl Trace for str { + fn grays(&self) -> Vec { + vec![] + } +} + +impl From<&str> for Gc { + fn from(value: &str) -> Self { + todo!() + } +} diff --git a/dvm-lib/src/vm/mod.rs b/dvm-lib/src/vm/mod.rs index 033c18d..54124fc 100644 --- a/dvm-lib/src/vm/mod.rs +++ b/dvm-lib/src/vm/mod.rs @@ -21,6 +21,7 @@ pub mod array; pub mod class; pub mod constant; pub mod function; +mod mem; pub mod object; pub mod operand; mod operand_helpers;