Opcode

Enum Opcode 

Source
#[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

Source

pub const INSTRUCTION_SIZE: usize = 4usize

The size of an instruction (1 byte opcode + 3 bytes operand).

Source

pub fn jump_forward(from: usize, to: usize) -> Result<(Self, Opr24), JumpTooFar>

Constructs a JumpForward instruction.

Source

pub fn jump_forward_if_falsy( from: usize, to: usize, ) -> Result<(Self, Opr24), JumpTooFar>

Constructs a JumpForwardIfFalsy instruction.

Source

pub fn jump_forward_if_truthy( from: usize, to: usize, ) -> Result<(Self, Opr24), JumpTooFar>

Constructs a JumpForwardIfTruthy instruction.

Source

pub fn jump_backward( from: usize, to: usize, ) -> Result<(Self, Opr24), JumpTooFar>

Constructs a JumpBackward instruction.

Trait Implementations§

Source§

impl Clone for Opcode

Source§

fn clone(&self) -> Opcode

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Opcode

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl EncodeInstruction for Opcode

Source§

fn encode_instruction(&self, bytes: &mut [u8; 4])

Source§

impl PartialEq for Opcode

Source§

fn eq(&self, other: &Opcode) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Opcode

Source§

impl Eq for Opcode

Source§

impl StructuralPartialEq for Opcode

Auto Trait Implementations§

§

impl Freeze for Opcode

§

impl RefUnwindSafe for Opcode

§

impl Send for Opcode

§

impl Sync for Opcode

§

impl Unpin for Opcode

§

impl UnwindSafe for Opcode

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.