feat: generalize Stack

This commit is contained in:
2025-05-15 11:20:59 +08:00
parent 2293b9e2de
commit 3e7a8a1c05
3 changed files with 17 additions and 18 deletions

View File

@@ -5,6 +5,7 @@
mod builtins;
mod bytecode;
mod ty;
mod stack;
pub mod compile;
pub mod error;

View File

@@ -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::<Value>();
pub struct Stack<'vm, const CAP: usize> {
pub struct Stack<T, const CAP: usize> {
// items: Box<[MaybeUninit<Value<'vm>>; CAP]>,
items: [MaybeUninit<Value<'vm>>; CAP],
items: [MaybeUninit<T>; CAP],
top: usize,
}
@@ -22,7 +19,7 @@ macro_rules! into {
};
}
impl<'vm, const CAP: usize> Stack<'vm, CAP> {
impl<T, const CAP: usize> Stack<T, CAP> {
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<T, const CAP: usize> Deref for Stack<T, CAP> {
type Target = [T];
fn deref(&self) -> &Self::Target {
into!(&self.items[0..self.top])
}
}
impl<const CAP: usize> Drop for Stack<'_, CAP> {
impl<T, const CAP: usize> Drop for Stack<T, CAP> {
fn drop(&mut self) {
self.items.as_mut_slice()[0..self.top]
.iter_mut()

View File

@@ -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::<Value>();
pub fn run(prog: Program, jit: JITContext<'_>) -> Result<p::Value> {
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<Env<'vm>>) -> Result<Value<'vm>> {
let mut stack = Stack::<STACK_SIZE>::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<Value<'vm>, CAP>,
env: &Rc<Env<'vm>>,
) -> Result<usize> {
match opcode {