2022-08-07 22:40:29 +02:00
|
|
|
//! This module implements the instruction set running on the abstract
|
|
|
|
//! machine implemented by tvix.
|
|
|
|
|
2022-09-13 14:58:55 +02:00
|
|
|
use std::ops::{AddAssign, Sub};
|
|
|
|
|
2022-08-26 19:48:39 +02:00
|
|
|
/// Index of a constant in the current code chunk.
|
2022-08-26 19:46:43 +02:00
|
|
|
#[repr(transparent)]
|
2022-09-18 18:38:53 +02:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
2022-08-07 22:40:29 +02:00
|
|
|
pub struct ConstantIdx(pub usize);
|
|
|
|
|
2022-08-26 19:48:39 +02:00
|
|
|
/// Index of an instruction in the current code chunk.
|
2022-08-26 19:46:43 +02:00
|
|
|
#[repr(transparent)]
|
2022-08-07 22:40:29 +02:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub struct CodeIdx(pub usize);
|
|
|
|
|
2022-09-13 14:58:55 +02:00
|
|
|
impl AddAssign<usize> for CodeIdx {
|
|
|
|
fn add_assign(&mut self, rhs: usize) {
|
|
|
|
*self = CodeIdx(self.0 + rhs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sub<usize> for CodeIdx {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn sub(self, rhs: usize) -> Self::Output {
|
|
|
|
CodeIdx(self.0 - rhs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-26 19:54:39 +02:00
|
|
|
/// Index of a value in the runtime stack.
|
|
|
|
#[repr(transparent)]
|
2022-09-03 02:06:20 +02:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd)]
|
2022-08-26 19:54:39 +02:00
|
|
|
pub struct StackIdx(pub usize);
|
|
|
|
|
2022-08-26 20:48:51 +02:00
|
|
|
/// Index of an upvalue within a closure's upvalue list.
|
|
|
|
#[repr(transparent)]
|
2022-08-31 03:56:02 +02:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
2022-08-26 20:48:51 +02:00
|
|
|
pub struct UpvalueIdx(pub usize);
|
|
|
|
|
2022-08-26 19:48:39 +02:00
|
|
|
/// Offset by which an instruction pointer should change in a jump.
|
2022-08-26 19:46:43 +02:00
|
|
|
#[repr(transparent)]
|
2022-09-18 18:38:53 +02:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
2022-08-26 19:46:43 +02:00
|
|
|
pub struct JumpOffset(pub usize);
|
|
|
|
|
2022-08-26 19:58:18 +02:00
|
|
|
/// Provided count for an instruction (could represent e.g. a number
|
|
|
|
/// of elements).
|
|
|
|
#[repr(transparent)]
|
2022-09-18 18:38:53 +02:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
2022-08-26 19:58:18 +02:00
|
|
|
pub struct Count(pub usize);
|
|
|
|
|
2022-08-12 16:19:14 +02:00
|
|
|
#[warn(variant_size_differences)]
|
2022-09-18 18:38:53 +02:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
2022-08-07 22:40:29 +02:00
|
|
|
pub enum OpCode {
|
2022-09-05 00:30:58 +02:00
|
|
|
/// Push a constant onto the stack.
|
2022-08-07 22:40:29 +02:00
|
|
|
OpConstant(ConstantIdx),
|
|
|
|
|
2022-09-05 00:30:58 +02:00
|
|
|
/// Discard a value from the stack.
|
2022-08-11 12:12:07 +02:00
|
|
|
OpPop,
|
|
|
|
|
2022-08-07 22:40:29 +02:00
|
|
|
// Push a literal value.
|
|
|
|
OpNull,
|
|
|
|
OpTrue,
|
|
|
|
OpFalse,
|
2022-08-08 01:16:28 +02:00
|
|
|
|
2022-08-08 01:51:28 +02:00
|
|
|
// Unary operators
|
|
|
|
OpInvert,
|
|
|
|
OpNegate,
|
|
|
|
|
2022-08-08 01:32:07 +02:00
|
|
|
// Arithmetic binary operators
|
2022-08-08 01:16:28 +02:00
|
|
|
OpAdd,
|
|
|
|
OpSub,
|
|
|
|
OpMul,
|
|
|
|
OpDiv,
|
2022-08-08 01:32:07 +02:00
|
|
|
|
2022-08-11 12:12:07 +02:00
|
|
|
// Comparison operators
|
2022-08-08 01:51:28 +02:00
|
|
|
OpEqual,
|
2022-08-11 10:37:04 +02:00
|
|
|
OpLess,
|
|
|
|
OpLessOrEq,
|
|
|
|
OpMore,
|
|
|
|
OpMoreOrEq,
|
2022-08-09 15:53:09 +02:00
|
|
|
|
2022-08-11 12:12:07 +02:00
|
|
|
// Logical operators & generic jumps
|
2022-08-26 19:46:43 +02:00
|
|
|
OpJump(JumpOffset),
|
|
|
|
OpJumpIfTrue(JumpOffset),
|
|
|
|
OpJumpIfFalse(JumpOffset),
|
|
|
|
OpJumpIfNotFound(JumpOffset),
|
2022-08-11 12:12:07 +02:00
|
|
|
|
2022-08-09 15:53:09 +02:00
|
|
|
// Attribute sets
|
2022-09-18 22:07:43 +02:00
|
|
|
/// Construct an attribute set from the given number of key-value pairs on the top of the stack
|
|
|
|
///
|
|
|
|
/// Note that this takes the count of *pairs*, not the number of *stack values* - the actual
|
|
|
|
/// number of values popped off the stack will be twice the argument to this op
|
2022-08-26 19:58:18 +02:00
|
|
|
OpAttrs(Count),
|
2022-08-10 20:01:15 +02:00
|
|
|
OpAttrsUpdate,
|
2022-08-11 14:29:11 +02:00
|
|
|
OpAttrsSelect,
|
2022-08-26 17:40:55 +02:00
|
|
|
OpAttrsTrySelect,
|
2022-09-17 18:10:40 +02:00
|
|
|
OpHasAttr,
|
2022-08-09 16:11:02 +02:00
|
|
|
|
2022-08-14 23:13:57 +02:00
|
|
|
// `with`-handling
|
2022-08-26 19:54:39 +02:00
|
|
|
OpPushWith(StackIdx),
|
2022-08-14 23:38:30 +02:00
|
|
|
OpPopWith,
|
2022-08-15 00:13:17 +02:00
|
|
|
OpResolveWith,
|
2022-08-14 23:13:57 +02:00
|
|
|
|
2022-08-09 16:11:02 +02:00
|
|
|
// Lists
|
2022-08-26 19:58:18 +02:00
|
|
|
OpList(Count),
|
2022-08-11 10:50:38 +02:00
|
|
|
OpConcat,
|
2022-08-09 16:44:34 +02:00
|
|
|
|
|
|
|
// Strings
|
2022-08-26 19:58:18 +02:00
|
|
|
OpInterpolate(Count),
|
2022-09-15 16:38:35 +02:00
|
|
|
/// Force the Value on the stack and coerce it to a string, always using
|
|
|
|
/// `CoercionKind::Weak`.
|
|
|
|
OpCoerceToString,
|
2022-08-11 13:56:27 +02:00
|
|
|
|
2022-10-10 05:46:51 +02:00
|
|
|
// Paths
|
2022-10-10 20:43:51 +02:00
|
|
|
/// Attempt to resolve the Value on the stack using the configured [`NixSearchPath`][]
|
2022-10-10 05:46:51 +02:00
|
|
|
///
|
2022-10-10 20:43:51 +02:00
|
|
|
/// [`NixSearchPath`]: crate::nix_search_path::NixSearchPath
|
2022-10-10 05:46:51 +02:00
|
|
|
OpFindFile,
|
|
|
|
|
2022-10-15 16:42:27 +02:00
|
|
|
/// Attempt to resolve a path literal relative to the home dir
|
|
|
|
OpResolveHomePath,
|
|
|
|
|
2022-08-11 13:56:27 +02:00
|
|
|
// Type assertion operators
|
|
|
|
OpAssertBool,
|
2022-08-13 16:34:20 +02:00
|
|
|
|
2022-09-05 00:30:58 +02:00
|
|
|
/// Access local identifiers with statically known positions.
|
2022-08-26 19:54:39 +02:00
|
|
|
OpGetLocal(StackIdx),
|
2022-08-13 19:17:25 +02:00
|
|
|
|
2022-09-05 00:30:58 +02:00
|
|
|
/// Close scopes while leaving their expression value around.
|
2022-08-26 19:58:18 +02:00
|
|
|
OpCloseScope(Count), // number of locals to pop
|
2022-08-16 14:53:35 +02:00
|
|
|
|
2022-10-10 18:56:11 +02:00
|
|
|
/// Return an error indicating that an `assert` failed
|
|
|
|
OpAssertFail,
|
2022-08-24 01:26:58 +02:00
|
|
|
|
2022-08-26 23:21:08 +02:00
|
|
|
// Lambdas & closures
|
2022-08-24 01:26:58 +02:00
|
|
|
OpCall,
|
2022-09-04 22:16:59 +02:00
|
|
|
OpTailCall,
|
2022-08-26 20:48:51 +02:00
|
|
|
OpGetUpvalue(UpvalueIdx),
|
2022-08-26 23:21:08 +02:00
|
|
|
OpClosure(ConstantIdx),
|
|
|
|
|
2022-08-28 22:53:20 +02:00
|
|
|
// Thunks
|
|
|
|
OpThunk(ConstantIdx),
|
2022-08-29 17:33:02 +02:00
|
|
|
OpForce,
|
2022-08-28 22:53:20 +02:00
|
|
|
|
2022-08-28 16:28:20 +02:00
|
|
|
/// Finalise initialisation of the upvalues of the value in the
|
|
|
|
/// given stack index after the scope is fully bound.
|
|
|
|
OpFinalise(StackIdx),
|
|
|
|
|
2022-08-26 23:21:08 +02:00
|
|
|
// The closure and thunk creation instructions have a variable
|
|
|
|
// number of arguments to the instruction, which is represented
|
|
|
|
// here by making their data part of the opcodes.
|
|
|
|
//
|
|
|
|
// The VM skips over these by advancing the instruction pointer
|
|
|
|
// according to the count.
|
|
|
|
DataLocalIdx(StackIdx),
|
2022-08-28 15:50:46 +02:00
|
|
|
DataDeferredLocal(StackIdx),
|
2022-08-26 23:21:08 +02:00
|
|
|
DataUpvalueIdx(UpvalueIdx),
|
2022-09-06 22:13:48 +02:00
|
|
|
DataCaptureWith,
|
2022-08-07 22:40:29 +02:00
|
|
|
}
|