2020-09-17 13:09:37 -07:00
|
|
|
use crate::{obj::prelude::*, vm::consts::*};
|
2020-09-01 17:32:48 -07:00
|
|
|
|
2020-09-14 16:32:00 -07:00
|
|
|
#[derive(Debug, PartialEq, Clone, Copy)]
|
2020-09-01 17:32:48 -07:00
|
|
|
pub enum Inst {
|
|
|
|
|
/// Push a literal symbol object to the stack.
|
|
|
|
|
PushSym(Sym),
|
|
|
|
|
|
2020-09-14 14:09:29 -07:00
|
|
|
/// Push a const value reference to the stack.
|
|
|
|
|
PushConst(ConstHandle),
|
|
|
|
|
|
|
|
|
|
/// Looks up and pushes a local value.
|
2020-09-27 19:33:18 -07:00
|
|
|
LoadLocal(Name),
|
|
|
|
|
|
|
|
|
|
/// Looks up and pushes a global value.
|
|
|
|
|
LoadGlobal(Name),
|
2020-09-14 14:09:29 -07:00
|
|
|
|
2020-09-01 17:32:48 -07:00
|
|
|
/// Pop a value from the stack, possibly into a local symbol.
|
2020-09-27 19:33:18 -07:00
|
|
|
PopLocal(Option<Name>),
|
|
|
|
|
|
|
|
|
|
/// Pop a value from the stack, possibly into a global symbol.
|
|
|
|
|
PopGlobal(Option<Name>),
|
2020-09-01 17:32:48 -07:00
|
|
|
|
|
|
|
|
/// Pops a symbol value and an object reference.
|
|
|
|
|
///
|
|
|
|
|
/// This will get an attr from the object reference pointed to by the symbol.
|
2020-09-14 14:09:29 -07:00
|
|
|
GetAttr(Sym),
|
2020-09-01 17:32:48 -07:00
|
|
|
|
2020-09-24 15:29:44 -07:00
|
|
|
/// Pops a target reference and a source reference from the stack.
|
2020-09-01 17:32:48 -07:00
|
|
|
///
|
2020-09-14 14:09:29 -07:00
|
|
|
/// The target reference will have the given symbol attribute assigned to the source.
|
2020-09-01 17:32:48 -07:00
|
|
|
///
|
2020-09-14 14:09:29 -07:00
|
|
|
/// In code, it would look like this:
|
|
|
|
|
///
|
2020-09-16 17:18:31 -07:00
|
|
|
/// `target.symbol = source`
|
2020-09-14 14:09:29 -07:00
|
|
|
///
|
|
|
|
|
SetAttr(Sym),
|
2020-09-01 17:32:48 -07:00
|
|
|
|
2020-11-06 16:57:25 -08:00
|
|
|
/// Pops the top value off of the stack and checks if it is a `Bool` of a true value.
|
|
|
|
|
CheckTruth,
|
|
|
|
|
|
2020-09-01 17:32:48 -07:00
|
|
|
/// Jump to a given address in the current function unconditionally.
|
|
|
|
|
Jump(usize),
|
|
|
|
|
|
|
|
|
|
/// Jump to a given address in the current function if the condition flag is true.
|
|
|
|
|
///
|
|
|
|
|
/// The condition flag may be set by an internal function.
|
|
|
|
|
JumpTrue(usize),
|
|
|
|
|
|
2020-09-14 14:09:29 -07:00
|
|
|
/// Calls a function with the supplied number of arguments.
|
|
|
|
|
///
|
|
|
|
|
/// The stack, from bottom to top, should contain the function followed by the arguments.
|
|
|
|
|
///
|
|
|
|
|
/// After the function has returned, the VM will have popped the arguments and function
|
|
|
|
|
/// pointer, and the return value will be on top of the stack.
|
2020-09-01 17:32:48 -07:00
|
|
|
Call(usize),
|
|
|
|
|
|
2020-09-14 14:09:29 -07:00
|
|
|
/// Indexes a value, e.g. a list or a dict.
|
|
|
|
|
///
|
|
|
|
|
/// The stack, from bottom to top, should have the expression being indexed, and then the
|
|
|
|
|
/// indexed value.
|
|
|
|
|
Index,
|
|
|
|
|
|
2020-09-01 17:32:48 -07:00
|
|
|
/// Pops the top value from the stack, and returns from the function, using the popped value as
|
|
|
|
|
/// a return value.
|
|
|
|
|
Return,
|
|
|
|
|
}
|
2020-09-17 13:09:37 -07:00
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// impl Inst
|
|
|
|
|
//
|
|
|
|
|
impl Inst {
|
2020-11-10 19:17:27 -08:00
|
|
|
/// Gets the printable name of this instruction.
|
|
|
|
|
pub const fn name(&self) -> &'static str {
|
2020-09-17 13:09:37 -07:00
|
|
|
match self {
|
|
|
|
|
Inst::PushSym(_) => "PUSH_SYM",
|
|
|
|
|
Inst::PushConst(_) => "PUSH_CONST",
|
2020-09-27 19:33:18 -07:00
|
|
|
Inst::LoadLocal(_) => "LOAD_LOCAL",
|
|
|
|
|
Inst::LoadGlobal(_) => "LOAD_GLOBAL",
|
|
|
|
|
Inst::PopLocal(_) => "POP_LOCAL",
|
|
|
|
|
Inst::PopGlobal(_) => "POP_GLOBAL",
|
2020-09-17 13:09:37 -07:00
|
|
|
Inst::GetAttr(_) => "GET_ATTR",
|
|
|
|
|
Inst::SetAttr(_) => "SET_ATTR",
|
2020-11-06 16:57:25 -08:00
|
|
|
Inst::CheckTruth => "CHECK_TRUTH",
|
2020-09-17 13:09:37 -07:00
|
|
|
Inst::Jump(_) => "JUMP",
|
|
|
|
|
Inst::JumpTrue(_) => "JUMP_TRUE",
|
|
|
|
|
Inst::Call(_) => "CALL",
|
|
|
|
|
Inst::Index => "INDEX",
|
|
|
|
|
Inst::Return => "RETURN",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-24 15:29:44 -07:00
|
|
|
|
2020-11-06 16:57:25 -08:00
|
|
|
unsafe impl shredder::marker::GcDrop for Inst {}
|