-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
129 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.