Skip to content

Commit

Permalink
object
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszcz committed May 15, 2024
1 parent c7ddc1f commit c94a9f1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
9 changes: 9 additions & 0 deletions runtime/rust/src/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,12 @@ pub type Pointer = Word;

pub const INITIAL_STACK_CAPACITY: usize = 1024;
pub const INITIAL_MEMORY_CAPACITY: usize = 1024;
pub const OBJECT_HEADER_SIZE: usize = 1;

pub fn to_usize(s: Word) -> usize {
s as usize
}

pub fn to_word(s: usize) -> Word {
s as Word
}
1 change: 1 addition & 0 deletions runtime/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod defs;
pub mod memory;
pub mod object;
pub mod stack;

pub fn add(left: usize, right: usize) -> usize {
Expand Down
39 changes: 39 additions & 0 deletions runtime/rust/src/object.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Object access and manipulation

use super::defs::*;
use super::memory::*;

const UID_SHIFT: usize = 0;
const UID_MASK: Word = 0xff;
const NFIELDS_SHIFT: usize = 8;
const NFIELDS_MASK: Word = 0xffffff;

pub fn get_header_uid(h: Word) -> Word {
(h >> UID_SHIFT) & UID_MASK
}

pub fn get_header_nfields(h: Word) -> usize {
to_usize((h >> NFIELDS_SHIFT) & NFIELDS_MASK)
}

pub fn make_header(uid: Word, nfields: usize) -> Word {
(uid << UID_SHIFT) | (to_word(nfields) << NFIELDS_SHIFT)
}

impl Memory {
pub fn get_header(self: &Memory, ptr: Pointer) -> Word {
self[to_usize(ptr)]
}

pub fn get_field(self: &Memory, ptr: Pointer, idx: Word) -> Word {
self[to_usize(ptr) + OBJECT_HEADER_SIZE + to_usize(idx)]
}

pub fn get_uid(self: &Memory, ptr: Pointer) -> Word {
get_header_uid(self.get_header(ptr))
}

pub fn get_nfields(self: &Memory, ptr: Pointer) -> usize {
get_header_nfields(self.get_header(ptr))
}
}

0 comments on commit c94a9f1

Please sign in to comment.