#[repr(u8)]pub enum Opcode {
Show 47 variants
Nop = 0,
PushNil = 1,
PushTrue = 2,
PushFalse = 3,
PushNumber = 4,
PushString = 5,
CreateClosure = 6,
CreateType = 7,
CreateStruct = 8,
CreateTrait = 9,
CreateList = 10,
CreateDict = 11,
AssignGlobal = 12,
SinkGlobal = 13,
GetGlobal = 14,
AssignLocal = 15,
SinkLocal = 16,
GetLocal = 17,
AssignUpvalue = 18,
SinkUpvalue = 19,
GetUpvalue = 20,
CloseLocal = 21,
AssignField = 22,
SinkField = 23,
GetField = 24,
Swap = 25,
Discard = 26,
JumpForward = 27,
JumpForwardIfFalsy = 28,
JumpForwardIfTruthy = 29,
JumpBackward = 30,
EnterBreakableBlock = 31,
ExitBreakableBlock = 32,
Call = 33,
CallMethod = 34,
Return = 35,
Implement = 36,
Negate = 37,
Add = 38,
Subtract = 39,
Multiply = 40,
Divide = 41,
Not = 42,
Equal = 43,
Less = 44,
LessEqual = 45,
Halt = 46,
}Expand description
A VM opcode.
Variants§
Nop = 0
Doesn’t do anything. Used as a default zero value if something goes wrong. Also used for backpatching purposes.
PushNil = 1
Pushes nil onto the stack.
PushTrue = 2
Pushes true onto the stack.
PushFalse = 3
Pushes false onto the stack.
PushNumber = 4
Pushes a number onto the stack. Must be followed by an f64.
PushString = 5
Pushes a string onto the stack. Must be followed by a string.
CreateClosure = 6
Creates a closure from the function with the given ID and pushes it onto the stack.
CreateType = 7
Creates a unique type that can be later implemented. Must be followed by a string indicating the type’s name.
CreateStruct = 8
Creates a struct instance from the type at the top of the stack, with the specified amount of fields.
CreateTrait = 9
Creates an instance of the trait with the given ID and pushes it onto the stack.
CreateList = 10
Creates a list from operand values that are at the top of the stack.
CreateDict = 11
Creates a dict from operand * 2 values that are at the top of the stack. The values have
to be arranged in key, value, key, value... order, from bottom to top.
AssignGlobal = 12
Assigns the value at the top of the stack to a global. The value stays on the stack.
SinkGlobal = 13
Sinks the value at the top of the stack to a global. The value is consumed.
GetGlobal = 14
Loads a value from a global.
AssignLocal = 15
Assigns the value at the top of the stack to a local. The value stays on the stack.
SinkLocal = 16
Sinks the value at the top of the stack to a local. The value is consumed.
GetLocal = 17
Loads a value from a local.
AssignUpvalue = 18
Assigns the value at the top of the stack to an upvalue. The value stays on the stack.
SinkUpvalue = 19
Sinks the value at the top of the stack to an upvalue. The value is consumed.
GetUpvalue = 20
Loads a value from an upvalue.
CloseLocal = 21
Closes a local in its upvalue.
AssignField = 22
Assigns to a field in the struct on the top of the stack. The struct is consumed but the value remains on the stack. Assumes the second value from top is a struct and not something else.
SinkField = 23
Sinks to a field in the struct on the top of the stack. Both the struct and the value are consumed.
GetField = 24
Loads a field from the struct on the top of the stack. Assumes the value on top is a struct and not something else.
Swap = 25
Swaps the two values at the top of the stack.
Discard = 26
Removes the value at the top of the stack.
JumpForward = 27
Jumps the program counter forward by an amount of bytes.
JumpForwardIfFalsy = 28
Jumps the program counter forward by an amount of bytes if the value at the top of the stack is falsy.
JumpForwardIfTruthy = 29
Jumps the program counter forward by an amount of bytes if the value at the top of the stack is truthy.
JumpBackward = 30
Jumps the program counter backward by an amount of bytes.
Due to how the VM increments the program counter, the actual amount is operand - 4.
EnterBreakableBlock = 31
Enters a breakable block by pushing the break sentinel value onto the stack.
ExitBreakableBlock = 32
Exits the n-th breakable block (counted from innermost) by popping values off the stack
until .0 sentinels are removed.
Call = 33
Calls a function with .0 arguments.
CallMethod = 34
Calls the nth method with a arguments, where a is encoded in the lower 8 bits, and
n is encoded in the upper 16 bits of .0.
Return = 35
Returns to the calling function.
Implement = 36
Implements a struct according to a prototype identified by the operand.
Negate = 37
Negates a number (prefix -).
Add = 38
Adds two numbers together (infix +).
Subtract = 39
Subtracts a number from another number (infix -).
Multiply = 40
Multiplies two numbers together (infix *).
Divide = 41
Divides a number by another number (infix /).
Not = 42
Flips a boolean-like value (truthy values become false and falsy values become true).
Equal = 43
Compares two values for equality.
Less = 44
Compares two values for less-than relation.
LessEqual = 45
Compares two values for less-than-or-equal relation.
Halt = 46
Halts the interpreter loop.
Implementations§
Source§impl Opcode
impl Opcode
Sourcepub const INSTRUCTION_SIZE: usize = 4usize
pub const INSTRUCTION_SIZE: usize = 4usize
The size of an instruction (1 byte opcode + 3 bytes operand).
Sourcepub fn jump_forward(from: usize, to: usize) -> Result<(Self, Opr24), JumpTooFar>
pub fn jump_forward(from: usize, to: usize) -> Result<(Self, Opr24), JumpTooFar>
Constructs a JumpForward instruction.
Sourcepub fn jump_forward_if_falsy(
from: usize,
to: usize,
) -> Result<(Self, Opr24), JumpTooFar>
pub fn jump_forward_if_falsy( from: usize, to: usize, ) -> Result<(Self, Opr24), JumpTooFar>
Constructs a JumpForwardIfFalsy instruction.
Sourcepub fn jump_forward_if_truthy(
from: usize,
to: usize,
) -> Result<(Self, Opr24), JumpTooFar>
pub fn jump_forward_if_truthy( from: usize, to: usize, ) -> Result<(Self, Opr24), JumpTooFar>
Constructs a JumpForwardIfTruthy instruction.
Sourcepub fn jump_backward(
from: usize,
to: usize,
) -> Result<(Self, Opr24), JumpTooFar>
pub fn jump_backward( from: usize, to: usize, ) -> Result<(Self, Opr24), JumpTooFar>
Constructs a JumpBackward instruction.