2020-09-14 14:09:29 -07:00
|
|
|
pub mod consts;
|
2020-09-24 15:29:44 -07:00
|
|
|
pub mod error; // TODO : not needed?
|
2020-09-14 16:32:00 -07:00
|
|
|
pub mod frame;
|
|
|
|
|
pub mod inst;
|
2020-09-18 15:45:54 -07:00
|
|
|
pub mod package;
|
2020-09-24 15:29:44 -07:00
|
|
|
pub(crate) mod signal;
|
2020-09-01 17:32:48 -07:00
|
|
|
|
2020-09-14 16:32:00 -07:00
|
|
|
use crate::{
|
2020-09-18 13:57:51 -07:00
|
|
|
obj::{reserved::*, prelude::*},
|
2020-09-24 15:29:44 -07:00
|
|
|
vm::{frame::*, inst::*, package::*, signal::*},
|
2020-09-14 16:32:00 -07:00
|
|
|
};
|
2020-09-01 17:32:48 -07:00
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2020-09-18 15:45:54 -07:00
|
|
|
pub struct Vm<'p> {
|
2020-09-01 17:32:48 -07:00
|
|
|
stack: Vec<ObjRef>,
|
|
|
|
|
frames: Vec<Frame>,
|
|
|
|
|
pc: usize,
|
|
|
|
|
condition: bool,
|
2020-09-18 15:45:54 -07:00
|
|
|
package: &'p Package,
|
2020-09-14 16:32:00 -07:00
|
|
|
}
|
|
|
|
|
|
2020-09-18 15:45:54 -07:00
|
|
|
impl<'p> Vm<'p> {
|
|
|
|
|
pub fn new(package: &'p Package) -> Self {
|
2020-09-01 17:32:48 -07:00
|
|
|
Self {
|
|
|
|
|
stack: Default::default(),
|
2020-09-14 16:32:00 -07:00
|
|
|
frames: vec![],
|
2020-09-01 17:32:48 -07:00
|
|
|
pc: 0,
|
|
|
|
|
condition: false,
|
2020-09-18 15:45:54 -07:00
|
|
|
package,
|
2020-09-01 17:32:48 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-18 15:45:54 -07:00
|
|
|
/// Gets the package that is currently loaded by this VM.
|
|
|
|
|
pub fn package(&self) -> &'p Package {
|
|
|
|
|
self.package
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 16:32:00 -07:00
|
|
|
/// Gets the current stack frame, if any.
|
|
|
|
|
pub fn frame(&self) -> Option<&Frame> {
|
|
|
|
|
self.frames.last()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Gets the current stack frame mutably, if any.
|
|
|
|
|
pub fn frame_mut(&mut self) -> Option<&mut Frame> {
|
|
|
|
|
self.frames.last_mut()
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 16:37:16 -07:00
|
|
|
/// Gets the list of stack frames.
|
|
|
|
|
pub fn frames(&self) -> &Vec<Frame> {
|
|
|
|
|
&self.frames
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 16:32:00 -07:00
|
|
|
/// Gets the stack.
|
2020-09-01 17:32:48 -07:00
|
|
|
pub fn stack(&self) -> &Vec<ObjRef> {
|
|
|
|
|
&self.stack
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 16:32:00 -07:00
|
|
|
/// Gets the stack, mutably.
|
2020-09-01 17:32:48 -07:00
|
|
|
pub fn stack_mut(&mut self) -> &mut Vec<ObjRef> {
|
|
|
|
|
&mut self.stack
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 16:32:00 -07:00
|
|
|
/// Pushes a value to the stack.
|
2020-09-01 17:32:48 -07:00
|
|
|
pub fn push(&mut self, value: ObjRef) {
|
|
|
|
|
self.stack_mut().push(value);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 16:32:00 -07:00
|
|
|
/// Pops a value from the stack.
|
2020-09-01 17:32:48 -07:00
|
|
|
pub fn pop(&mut self) -> Option<ObjRef> {
|
|
|
|
|
self.stack_mut().pop()
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 16:32:00 -07:00
|
|
|
/// Gets the current program counter address.
|
2020-09-01 17:32:48 -07:00
|
|
|
pub fn pc(&self) -> usize {
|
|
|
|
|
self.pc
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 16:32:00 -07:00
|
|
|
/// Set the next program counter value.
|
|
|
|
|
///
|
|
|
|
|
/// This may cause the running program to crash. Handle with care.
|
|
|
|
|
pub fn set_pc(&mut self, pc: usize) {
|
|
|
|
|
self.pc = pc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Gets whether the condition flag has been set or not.
|
2020-09-01 17:32:48 -07:00
|
|
|
pub fn condition(&self) -> bool {
|
|
|
|
|
self.condition
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 16:32:00 -07:00
|
|
|
/// Sets the condition flag to the specified value.
|
|
|
|
|
pub fn set_condition(&mut self, condition: bool) {
|
|
|
|
|
self.condition = condition;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-24 15:29:44 -07:00
|
|
|
/// Resumes execution of the current program.
|
|
|
|
|
pub fn resume(&mut self) {
|
|
|
|
|
while let Some(frame) = self.frame().cloned() {
|
|
|
|
|
let signal = match frame.kind() {
|
|
|
|
|
FrameKind::Native(fun, args) => {
|
|
|
|
|
read_obj!(let fun_obj = fun);
|
|
|
|
|
(fun_obj.fun())(fun.clone(), self, args.clone())
|
|
|
|
|
}
|
|
|
|
|
FrameKind::User { .. } => {
|
|
|
|
|
// run the user function until it returns control to the VM
|
|
|
|
|
loop {
|
|
|
|
|
if let Some(signal) = self.tick() {
|
|
|
|
|
break signal;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
self.handle_signal(signal);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Handles a signal targeting the VM.
|
|
|
|
|
///
|
|
|
|
|
/// The signal may originate from the VM itself, or from an external location. Signals
|
|
|
|
|
/// generally interrupt control flow of a program.
|
|
|
|
|
pub fn handle_signal(&mut self, signal: Signal) {
|
|
|
|
|
match signal {
|
|
|
|
|
Signal::Call(caller, args) => {
|
|
|
|
|
self.begin_call(caller, args);
|
|
|
|
|
}
|
|
|
|
|
Signal::Return => {
|
|
|
|
|
let retval = self.stack.pop()
|
|
|
|
|
.expect("return value");
|
|
|
|
|
match self.frames.pop().unwrap().kind() {
|
|
|
|
|
// no-op; return value should be the TOS
|
|
|
|
|
FrameKind::Native(_, _) => { },
|
|
|
|
|
FrameKind::User { last_pc, stack_base, fun: _, } => {
|
|
|
|
|
// pop return value, reset PC, clean stack, and push return value
|
|
|
|
|
self.set_pc(*last_pc);
|
|
|
|
|
self.stack.truncate(*stack_base);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
self.stack.push(retval);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Sets up the stack for a function call.
|
|
|
|
|
fn begin_call(&mut self, caller: ObjRef, args: Vec<ObjRef>) {
|
|
|
|
|
// create stack frame
|
|
|
|
|
let stack_frame = if let Some(user_fun) = std::any::Any::downcast_ref::<UserFunRef>(&caller) {
|
2020-09-27 19:33:18 -07:00
|
|
|
let names: FrameLocals = {
|
2020-09-24 15:29:44 -07:00
|
|
|
read_obj!(let fun_ref = user_fun);
|
|
|
|
|
fun_ref.locals()
|
|
|
|
|
.iter()
|
2020-09-27 19:33:18 -07:00
|
|
|
.enumerate()
|
2020-09-24 15:29:44 -07:00
|
|
|
.zip(args.into_iter())
|
2020-09-27 19:33:18 -07:00
|
|
|
.map(|((index, _), arg)| (index, arg.clone()))
|
2020-09-24 15:29:44 -07:00
|
|
|
.collect()
|
|
|
|
|
};
|
2020-09-27 19:33:18 -07:00
|
|
|
// TODO : check function arity vs argument count
|
2020-09-24 15:29:44 -07:00
|
|
|
|
2020-09-26 18:31:23 -07:00
|
|
|
Frame::new(names, FrameKind::User {
|
2020-09-24 15:29:44 -07:00
|
|
|
last_pc: self.pc(),
|
|
|
|
|
stack_base: self.stack().len(),
|
|
|
|
|
fun: user_fun.clone(),
|
|
|
|
|
})
|
|
|
|
|
} else if let Some(native_fun) = std::any::Any::downcast_ref::<NativeFunRef>(&caller) {
|
|
|
|
|
Frame::new(Default::default(), FrameKind::Native(native_fun.clone(), args))
|
|
|
|
|
} else {
|
|
|
|
|
// TODO : throw an error when error handling is figured out
|
|
|
|
|
panic!("can't call object {:?}", caller);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// push a new stack frame
|
|
|
|
|
self.frames.push(stack_frame);
|
2020-09-14 16:32:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Gets the current instruction.
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn load_inst(&self) -> Inst {
|
2020-09-24 15:29:44 -07:00
|
|
|
let user_fun = if let FrameKind::User { fun, .. } = self.frame().expect("frame").kind() {
|
|
|
|
|
fun
|
2020-09-14 16:32:00 -07:00
|
|
|
} else {
|
2020-09-24 15:29:44 -07:00
|
|
|
panic!("expected user function frame")
|
|
|
|
|
};
|
|
|
|
|
read_obj!(let user_fun = user_fun);
|
|
|
|
|
user_fun.code()[self.pc()]
|
2020-09-14 16:32:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Run a single instruction - inlined version.
|
|
|
|
|
///
|
|
|
|
|
/// Since this is inlined, it is probably a bad idea to allow users to use it everywhere. The
|
|
|
|
|
/// exposed API function `tick()` calls this function, but does not inline itself (unless the
|
|
|
|
|
/// optimizer thinks it's a good idea to do so).
|
|
|
|
|
#[inline]
|
2020-09-24 15:29:44 -07:00
|
|
|
fn tick(&mut self) -> Option<Signal> {
|
2020-09-14 16:32:00 -07:00
|
|
|
let inst = self.load_inst();
|
|
|
|
|
let mut next_pc = self.pc() + 1;
|
2020-09-24 15:29:44 -07:00
|
|
|
let mut signal = None;
|
2020-09-14 16:32:00 -07:00
|
|
|
match inst {
|
|
|
|
|
Inst::PushSym(sym) => {
|
|
|
|
|
let sym_ref = global_sym_ref(sym);
|
|
|
|
|
self.push(sym_ref);
|
|
|
|
|
}
|
|
|
|
|
Inst::PushConst(hdl) => {
|
2020-09-18 15:45:54 -07:00
|
|
|
let obj_ref = self.package()
|
|
|
|
|
.const_pool()
|
|
|
|
|
.get(hdl)
|
|
|
|
|
.clone();
|
2020-09-14 16:32:00 -07:00
|
|
|
self.push(obj_ref);
|
|
|
|
|
}
|
2020-09-27 19:33:18 -07:00
|
|
|
Inst::LoadLocal(local) => {
|
2020-09-24 15:29:44 -07:00
|
|
|
let obj_ref = self.frames()
|
|
|
|
|
.iter()
|
|
|
|
|
.rev()
|
2020-09-27 19:33:18 -07:00
|
|
|
.filter_map(|frame| frame.locals().get(&local.index()))
|
2020-09-24 15:29:44 -07:00
|
|
|
.cloned()
|
|
|
|
|
.next();
|
|
|
|
|
if let Some(obj_ref) = obj_ref {
|
|
|
|
|
self.push(obj_ref);
|
|
|
|
|
} else {
|
2020-09-27 19:33:18 -07:00
|
|
|
todo!("TODO: implement \"local value not found\" lookup")
|
2020-09-24 15:29:44 -07:00
|
|
|
}
|
|
|
|
|
}
|
2020-09-27 19:33:18 -07:00
|
|
|
Inst::LoadGlobal(global) => {
|
|
|
|
|
let obj_ref = self.frames()
|
|
|
|
|
.first()
|
|
|
|
|
.and_then(|frame| frame.locals().get(&global.index()))
|
|
|
|
|
.cloned();
|
|
|
|
|
if let Some(obj_ref) = obj_ref {
|
|
|
|
|
self.push(obj_ref);
|
|
|
|
|
} else {
|
|
|
|
|
todo!("TODO: implement \"local value not found\" lookup")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Inst::PopLocal(name) => {
|
2020-09-24 15:29:44 -07:00
|
|
|
let tos = self.pop()
|
|
|
|
|
.expect("stack underflow");
|
|
|
|
|
// pop into name
|
|
|
|
|
if let Some(name) = name {
|
|
|
|
|
self.set_local(name, tos);
|
|
|
|
|
}
|
|
|
|
|
// else discard
|
|
|
|
|
}
|
2020-09-27 19:33:18 -07:00
|
|
|
Inst::PopGlobal(name) => {
|
|
|
|
|
let tos = self.pop()
|
|
|
|
|
.expect("stack underflow");
|
|
|
|
|
// pop into name
|
|
|
|
|
if let Some(name) = name {
|
|
|
|
|
self.set_global(name, tos);
|
|
|
|
|
}
|
|
|
|
|
// else discard
|
|
|
|
|
}
|
2020-09-14 16:32:00 -07:00
|
|
|
Inst::GetAttr(sym) => {
|
|
|
|
|
let obj_ref = self.pop().expect("getattr object");
|
|
|
|
|
read_obj!(let obj = obj_ref);
|
|
|
|
|
let attr = obj
|
|
|
|
|
.get_attr(sym)
|
|
|
|
|
.unwrap_or_else(|| global_sym_ref(NIL_NAME.sym));
|
|
|
|
|
self.push(attr);
|
|
|
|
|
}
|
2020-09-24 15:29:44 -07:00
|
|
|
Inst::SetAttr(sym) => {
|
|
|
|
|
let target = self.pop()
|
|
|
|
|
.expect("no target available for SetAttr");
|
|
|
|
|
let source = self.pop()
|
|
|
|
|
.expect("no source available for SetAttr");
|
|
|
|
|
write_obj!(let target = target);
|
|
|
|
|
if let Some(attrs) = target.attrs_mut() {
|
|
|
|
|
attrs.insert(sym, source);
|
|
|
|
|
} else {
|
|
|
|
|
todo!("TODO: throw an error for attributes that can't be set");
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-14 16:32:00 -07:00
|
|
|
Inst::Jump(addr) => {
|
|
|
|
|
next_pc = addr;
|
|
|
|
|
}
|
|
|
|
|
Inst::JumpTrue(addr) => {
|
|
|
|
|
if self.condition {
|
|
|
|
|
next_pc = addr;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-24 15:29:44 -07:00
|
|
|
Inst::Call(argc) => {
|
|
|
|
|
let stack_top = self.stack.len() - argc;
|
|
|
|
|
let args = self.stack.split_off(stack_top);
|
|
|
|
|
let caller = self.pop().unwrap();
|
|
|
|
|
signal = Some(Signal::Call(caller, args));
|
|
|
|
|
}
|
2020-09-14 16:32:00 -07:00
|
|
|
Inst::Index => todo!(),
|
2020-09-24 15:29:44 -07:00
|
|
|
Inst::Return => { signal = Some(Signal::Return); }
|
2020-09-14 16:32:00 -07:00
|
|
|
Inst::UnNeg => todo!(),
|
|
|
|
|
Inst::UnPos => todo!(),
|
|
|
|
|
Inst::BinPlus => todo!(),
|
|
|
|
|
Inst::BinMinus => todo!(),
|
|
|
|
|
Inst::BinMul => todo!(),
|
|
|
|
|
Inst::BinDiv => todo!(),
|
|
|
|
|
Inst::BinEq => todo!(),
|
|
|
|
|
Inst::BinNeq => todo!(),
|
|
|
|
|
Inst::BinLt => todo!(),
|
|
|
|
|
Inst::BinLe => todo!(),
|
|
|
|
|
Inst::BinGt => todo!(),
|
|
|
|
|
Inst::BinGe => todo!(),
|
|
|
|
|
Inst::BinAnd => todo!(),
|
|
|
|
|
Inst::BinOr => todo!(),
|
|
|
|
|
}
|
|
|
|
|
self.set_pc(next_pc);
|
2020-09-24 15:29:44 -07:00
|
|
|
|
|
|
|
|
signal
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn set_local(&mut self, name: Name, value: ObjRef) {
|
2020-09-27 19:33:18 -07:00
|
|
|
let frame = self.frame_mut().unwrap();
|
|
|
|
|
let locals = frame.locals_mut();
|
|
|
|
|
locals.insert(name.index(), value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn set_global(&mut self, name: Name, value: ObjRef) {
|
|
|
|
|
let frame = self.frames.first_mut().unwrap();
|
|
|
|
|
let locals = frame.locals_mut();
|
|
|
|
|
locals.insert(name.index(), value);
|
2020-09-24 15:29:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_local(&mut self, name: Name) -> Option<ObjRef> {
|
2020-09-27 19:33:18 -07:00
|
|
|
self.frame()
|
|
|
|
|
.and_then(|frame| frame.locals().get(&name.index()))
|
2020-09-24 15:29:44 -07:00
|
|
|
.cloned()
|
2020-09-14 16:32:00 -07:00
|
|
|
}
|
2020-09-01 17:32:48 -07:00
|
|
|
}
|