diff --git a/src/lib.rs b/src/lib.rs index 008795c..6d3dc87 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ mod builtins; mod bytecode; mod ty; +mod stack; pub mod compile; pub mod error; diff --git a/src/vm/stack.rs b/src/stack.rs similarity index 76% rename from src/vm/stack.rs rename to src/stack.rs index 915ef82..21e5e83 100644 --- a/src/vm/stack.rs +++ b/src/stack.rs @@ -1,14 +1,11 @@ -use std::mem::{MaybeUninit, replace, size_of, transmute}; +use std::mem::{MaybeUninit, replace, transmute}; use std::ops::Deref; use crate::error::*; -use crate::ty::internal::Value; -pub const STACK_SIZE: usize = 8 * 1024 / size_of::(); - -pub struct Stack<'vm, const CAP: usize> { +pub struct Stack { // items: Box<[MaybeUninit>; CAP]>, - items: [MaybeUninit>; CAP], + items: [MaybeUninit; CAP], top: usize, } @@ -22,7 +19,7 @@ macro_rules! into { }; } -impl<'vm, const CAP: usize> Stack<'vm, CAP> { +impl Stack { pub fn new() -> Self { Stack { items: [const { MaybeUninit::uninit() }; CAP], @@ -30,7 +27,7 @@ impl<'vm, const CAP: usize> Stack<'vm, CAP> { } } - pub fn push(&mut self, item: Value<'vm>) -> Result<()> { + pub fn push(&mut self, item: T) -> Result<()> { self.items .get_mut(self.top) .map_or_else( @@ -42,7 +39,7 @@ impl<'vm, const CAP: usize> Stack<'vm, CAP> { Ok(()) } - pub fn pop(&mut self) -> Value<'vm> { + pub fn pop(&mut self) -> T { self.top -= 1; let item = self.items.get_mut(self.top).unwrap(); @@ -52,7 +49,7 @@ impl<'vm, const CAP: usize> Stack<'vm, CAP> { unsafe { replace(item, MaybeUninit::uninit()).assume_init() } } - pub fn tos(&self) -> Result<&Value<'vm>> { + pub fn tos(&self) -> Result<&T> { if self.top == 0 { panic!("stack empty") } else { @@ -60,7 +57,7 @@ impl<'vm, const CAP: usize> Stack<'vm, CAP> { } } - pub fn tos_mut(&mut self) -> Result<&mut Value<'vm>> { + pub fn tos_mut(&mut self) -> Result<&mut T> { if self.top == 0 { panic!("stack empty") } else { @@ -69,14 +66,14 @@ impl<'vm, const CAP: usize> Stack<'vm, CAP> { } } -impl<'vm, const CAP: usize> Deref for Stack<'vm, CAP> { - type Target = [Value<'vm>]; +impl Deref for Stack { + type Target = [T]; fn deref(&self) -> &Self::Target { into!(&self.items[0..self.top]) } } -impl Drop for Stack<'_, CAP> { +impl Drop for Stack { fn drop(&mut self) { self.items.as_mut_slice()[0..self.top] .iter_mut() diff --git a/src/vm/mod.rs b/src/vm/mod.rs index a2a14cf..f29958c 100644 --- a/src/vm/mod.rs +++ b/src/vm/mod.rs @@ -7,18 +7,19 @@ use crate::ty::common::Symbol; use crate::ty::internal::*; use crate::ty::public as p; -use stack::{STACK_SIZE, Stack}; +use crate::stack::Stack; pub use env::Env; pub use jit::JITContext; mod env; mod jit; -mod stack; #[cfg(test)] mod test; +const STACK_SIZE: usize = 8 * 1024 / size_of::(); + pub fn run(prog: Program, jit: JITContext<'_>) -> Result { let vm = VM::new( prog.thunks, @@ -57,7 +58,7 @@ impl<'vm, 'jit: 'vm> VM<'jit> { } pub fn eval(&'vm self, opcodes: OpCodes, env: Rc>) -> Result> { - let mut stack = Stack::::new(); + let mut stack = Stack::<_, STACK_SIZE>::new(); let mut iter = opcodes.into_iter(); while let Some(opcode) = iter.next() { let jmp = self.single_op(opcode, &mut stack, &env)?; @@ -75,7 +76,7 @@ impl<'vm, 'jit: 'vm> VM<'jit> { fn single_op<'s, const CAP: usize>( &'vm self, opcode: OpCode, - stack: &'s mut Stack<'vm, CAP>, + stack: &'s mut Stack, CAP>, env: &Rc>, ) -> Result { match opcode {