2022-08-07 22:40:29 +02:00
|
|
|
//! This module implements the instruction set running on the abstract
|
|
|
|
//! machine implemented by tvix.
|
|
|
|
|
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-08-07 22:40:29 +02:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
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-08-26 19:54:39 +02:00
|
|
|
/// Index of a value in the runtime stack.
|
|
|
|
#[repr(transparent)]
|
2022-08-26 20:48:51 +02:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
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)]
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
|
|
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)]
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
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)]
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub struct Count(pub usize);
|
|
|
|
|
2022-08-25 17:39:27 +02:00
|
|
|
#[allow(clippy::enum_variant_names)]
|
2022-08-12 16:19:14 +02:00
|
|
|
#[warn(variant_size_differences)]
|
2022-08-07 22:40:29 +02:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub enum OpCode {
|
|
|
|
// Push a constant onto the stack.
|
|
|
|
OpConstant(ConstantIdx),
|
|
|
|
|
2022-08-11 12:12:07 +02:00
|
|
|
// Discard a value from the stack.
|
|
|
|
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-08-26 19:58:18 +02:00
|
|
|
OpAttrs(Count),
|
|
|
|
OpAttrPath(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-08-11 16:06:23 +02:00
|
|
|
OpAttrsIsSet,
|
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-08-11 13:56:27 +02:00
|
|
|
|
|
|
|
// Type assertion operators
|
|
|
|
OpAssertBool,
|
2022-08-13 16:34:20 +02:00
|
|
|
|
2022-08-13 19:17:25 +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-08-13 16:34:20 +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
|
|
|
|
|
|
|
// Asserts stack top is a boolean, and true.
|
|
|
|
OpAssert,
|
2022-08-24 01:26:58 +02:00
|
|
|
|
|
|
|
// Lambdas
|
|
|
|
OpCall,
|
2022-08-26 20:48:51 +02:00
|
|
|
OpGetUpvalue(UpvalueIdx),
|
2022-08-07 22:40:29 +02:00
|
|
|
}
|