Files
nixjit/src/bytecode.rs

126 lines
3.2 KiB
Rust

use ecow::EcoString;
use hashbrown::HashMap;
use crate::ty::common::Const;
use crate::ty::internal::Param;
type Slice<T> = Box<[T]>;
pub type OpCodes = Slice<OpCode>;
#[derive(Debug, Clone, Copy)]
pub enum OpCode {
/// load a constant onto stack
Const { idx: usize },
/// load a dynamic var onto stack
LookUp { sym: usize },
/// load a var from let binding onto stack
LookUpLet { level: usize, idx: usize },
/// load a thunk lazily onto stack
LoadThunk { idx: usize },
/// load a thunk value onto stack
LoadValue { idx: usize },
/// let TOS capture current environment
CaptureEnv,
/// force TOS to value
ForceValue,
/// TODO:
InsertValue,
/// [ .. func arg ] consume 2 elements, call `func` with arg
Call,
/// make a function
Func { idx: usize },
/// load a function argument
Arg { level: usize },
/// consume 1 element, assert TOS is true
Assert,
/// jump forward
Jmp { step: usize },
/// [ .. cond ] consume 1 element, if `cond` is false, then jump forward
JmpIfFalse { step: usize },
/// push an empty attribute set onto stack
AttrSet { cap: usize },
/// finalize the recursive attribute set at TOS
FinalizeLet,
/// [ .. set value ] consume 1 element, push a static kv pair (`name`, `value`) into `set`
PushStaticAttr { name: usize },
/// [ .. set name value ] consume 2 elements, push a dynamic kv pair (`name`, `value`) in to `set`
PushDynamicAttr,
/// push an empty list onto stack
List { cap: usize },
/// [ .. list elem ] consume 1 element, push `elem` into `list`
PushElem,
/// convert the string as TOS to a path
Path,
/// [ .. a b ] consume 2 elements, perform a string concatenation `a` + `b`
ConcatString,
/// [ .. a b ] consume 2 elements, perform a binary operation `a` `op` `b`
BinOp { op: BinOp },
/// [ .. a ] consume 1 element, perform a unary operation `op` `a`
UnOp { op: UnOp },
/// set TOS to the bool value of whether TOS contains `sym`
HasAttr { sym: usize },
/// [ .. set sym ] consume 2 elements, set TOS to the bool value of whether `set` contains `sym`
HasDynamicAttr,
/// [ .. set ] select `sym` from `set`
Select { sym: usize },
/// [ .. set default ] select `sym` from `set` or `default`
SelectOrDefault { sym: usize },
/// [ .. set sym ] select `sym` from `set`
SelectDynamic,
/// [ .. set sym default ] select `sym` from `set` or `default`
SelectDynamicOrDefault,
/// enter the with environment of the attribute set at TOS
EnterWithEnv,
/// exit current envrironment
LeaveEnv,
/// TODO:
PopEnv,
/// illegal operation, used as termporary placeholder
Illegal,
}
#[derive(Debug, Clone, Copy)]
pub enum BinOp {
Add,
Sub,
Mul,
Div,
And,
Or,
Eq,
Lt,
Con,
Upd,
}
#[derive(Debug, Clone, Copy)]
pub enum UnOp {
Neg,
Not,
}
#[derive(Debug)]
pub struct Func {
pub param: Param,
pub opcodes: OpCodes,
}
#[derive(Debug)]
pub struct Program {
pub top_level: OpCodes,
pub thunks: Slice<OpCodes>,
pub funcs: Slice<Func>,
pub symbols: Vec<EcoString>,
pub symmap: HashMap<EcoString, usize>,
pub consts: Box<[Const]>,
}