Skip to content

Commit

Permalink
closure
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszcz committed May 16, 2024
1 parent c94a9f1 commit 7d69d5e
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 41 deletions.
77 changes: 77 additions & 0 deletions runtime/rust/src/closure.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Closure manipulation

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

pub const CLOSURE_HEADER_SIZE: usize = 3;

impl Memory {
pub fn alloc_closure(self: &mut Memory, fid: Word, args: &[Word], largs: usize) -> Pointer {
let p = self.alloc(args.len() + CLOSURE_HEADER_SIZE);
self.set_closure_fid(p, fid);
self.set_closure_nargs(p, args.len());
self.set_closure_largs(p, largs);
self.set_closure_args(p, args);
p
}

pub fn extend_closure(self: &mut Memory, ptr: Pointer, args: &[Word]) -> Pointer {
let p = self.alloc(args.len() + CLOSURE_HEADER_SIZE);
self.set_closure_fid(p, self.get_closure_fid(ptr));
self.set_closure_nargs(p, self.get_closure_nargs(ptr) + args.len());
self.set_closure_largs(p, self.get_closure_largs(ptr) - args.len());
let i1 = to_usize(ptr) + CLOSURE_HEADER_SIZE;
let nargs = self.get_closure_nargs(ptr);
let i2 = to_usize(p) + CLOSURE_HEADER_SIZE;
for i in 0..nargs - 1 {
self[i2 + i] = self[i1 + i]
}
self[i2 + nargs..i2 + nargs + args.len() - 1].copy_from_slice(args);
p
}

// Function id
pub fn get_closure_fid(self: &Memory, ptr: Pointer) -> Word {
self[to_usize(ptr)]
}

// The number of arguments stored in the closure
pub fn get_closure_nargs(self: &Memory, ptr: Pointer) -> usize {
to_usize(self[to_usize(ptr) + 1])
}

// The number of arguments remaining for a call to the function
pub fn get_closure_largs(self: &Memory, ptr: Pointer) -> usize {
to_usize(self[to_usize(ptr) + 2])
}

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

pub fn get_closure_args(self: &mut Memory, ptr: Pointer) -> &mut [Word] {
let i = to_usize(ptr) + CLOSURE_HEADER_SIZE;
let nargs = self.get_closure_nargs(ptr);
&mut self[i..i + nargs - 1]
}

pub fn set_closure_fid(self: &mut Memory, ptr: Pointer, uid: Word) {
self[to_usize(ptr)] = uid
}

pub fn set_closure_nargs(self: &mut Memory, ptr: Pointer, nargs: usize) {
self[to_usize(ptr) + 1] = to_word(nargs)
}

pub fn set_closure_largs(self: &mut Memory, ptr: Pointer, largs: usize) {
self[to_usize(ptr) + 1] = to_word(largs)
}

pub fn set_closure_arg(self: &mut Memory, ptr: Pointer, idx: Word, x: Word) {
self[to_usize(ptr) + CLOSURE_HEADER_SIZE + to_usize(idx)] = x
}

pub fn set_closure_args(self: &mut Memory, ptr: Pointer, args: &[Word]) {
self.get_closure_args(ptr).copy_from_slice(args);
}
}
50 changes: 50 additions & 0 deletions runtime/rust/src/constr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Constructor access and manipulation

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

pub const CONSTR_HEADER_SIZE: usize = 2;

impl Memory {
pub fn alloc_constr(self: &mut Memory, uid: Word, args: &[Word]) -> Pointer {
let p = self.alloc(args.len() + CONSTR_HEADER_SIZE);
self.set_constr_uid(p, uid);
self.set_constr_nargs(p, args.len());
self.set_constr_args(p, args);
p
}

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

pub fn get_constr_nargs(self: &Memory, ptr: Pointer) -> usize {
to_usize(self[to_usize(ptr) + 1])
}

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

pub fn get_constr_args(self: &mut Memory, ptr: Pointer) -> &mut [Word] {
let i = to_usize(ptr) + CONSTR_HEADER_SIZE;
let nargs = self.get_constr_nargs(ptr);
&mut self[i..i + nargs - 1]
}

pub fn set_constr_uid(self: &mut Memory, ptr: Pointer, uid: Word) {
self[to_usize(ptr)] = uid
}

pub fn set_constr_nargs(self: &mut Memory, ptr: Pointer, nfields: usize) {
self[to_usize(ptr) + 1] = to_word(nfields)
}

pub fn set_constr_arg(self: &mut Memory, ptr: Pointer, idx: Word, x: Word) {
self[to_usize(ptr) + CONSTR_HEADER_SIZE + to_usize(idx)] = x
}

pub fn set_constr_args(self: &mut Memory, ptr: Pointer, args: &[Word]) {
self.get_constr_args(ptr).copy_from_slice(args);
}
}
1 change: 0 additions & 1 deletion runtime/rust/src/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ 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
Expand Down
3 changes: 2 additions & 1 deletion runtime/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod closure;
pub mod constr;
pub mod defs;
pub mod memory;
pub mod object;
pub mod stack;

pub fn add(left: usize, right: usize) -> usize {
Expand Down
39 changes: 0 additions & 39 deletions runtime/rust/src/object.rs

This file was deleted.

0 comments on commit 7d69d5e

Please sign in to comment.