Enum InstructionKind

Source
pub enum InstructionKind {
Show 22 variants InitFunc(String, String, usize, usize), CheckExecReady, NextArg, ErrorIfMissingArgs, LoadConst(Value), CreateList(usize), CreateListRepeat, CreateRange(RangeKind), LoadVar(Symbol), StoreVar(usize), AssignVar(usize), StoreIndexed, LoadIndexed, Drop, Binary(BinOpKind), Unary(UnaryOpKind), Call(usize), CallDerivative(u8, usize), Return, Jump(Label), JumpIfTrue(Label), JumpIfFalse(Label),
}
Expand description

Kinds of bytecode instructions emitted by the compiler.

Bytecode can be thought of as a significantly high-level assembly language. Each instruction corresponds to a single operation that can be executed by the virtual machine, which itself can be thought of as a simple CPU.

During compilation, the compiler will associate most instructions with spans of the source code that they originated from. This is meant for debugging and to report high quality error messages for the user during runtime. The spans field of the Instruction struct contains this information.

See the Span information section of each variant for how to interpret the spans.

Variants§

§

InitFunc(String, String, usize, usize)

Initializes key VM registers for a call to a user-defined function.

This is a special instruction used in user function argument validation, and is compiled in as the first instruction of a user function.

The fields of this instruction are:

  • fn_name: The name of the function being executed.
  • fn_signature: The signature of the function being executed.
  • num_params: The total number of parameters in the function signature.
  • num_default_params: The number of default parameters in the function signature.
§

CheckExecReady

Checks if there are enough arguments on the stack to start executing the user function body, or throws a runtime error if there are too many arguments.

This is a special instruction used in user function argument validation, and is compiled in just before compiling a default argument expression. The specific order of operations is:

  • Compare the number of currently available arguments (set in the argument counter) to the number of arguments expected by the function.
  • If they are equal, the function is ready to be executed. The VM will then assign the given values to its internal variables representing the function parameters and execute the function body, jumping around as needed.
  • If there are more arguments than expected, an error is thrown.
  • Otherwise, fall-through to the default argument expression.
  • The default argument expression is compiled.
§

NextArg

Increments the argument counter by 1.

This is a special instruction used in user function argument validation, and is compiled in after each default argument expression.

§

ErrorIfMissingArgs

Checks if there are missing or extra arguments by the time the function body is reached, throwing a runtime error if so.

This is a special instruction used in user function argument validation, and is compiled in after all default argument expressions and before the function body.

§

LoadConst(Value)

Load a constant value (one known at compile time) onto the stack.

§

CreateList(usize)

Create a new list with the specified number of elements pulled from the stack.

§

CreateListRepeat

Create a new list by repeating the top value on the stack count times.

The value to repeat and the count are specified by the second-to-top and the top values on the stack, respectively.

§

CreateRange(RangeKind)

Create a new range with the start and end values on the stack.

§

LoadVar(Symbol)

Load a value stored in a variable onto the stack.

§

StoreVar(usize)

Store the top value on the stack in the current stack frame. This value is not removed from the stack.

This behavior is important since assignment expressions can be used as subexpressions in larger expressions.

§

AssignVar(usize)

Store the top value on the stack in the current stack frame. This value is removed from the stack.

This is used for assignment statements, where the value being assigned is not used in any further expressions.

§

StoreIndexed

Store the top value on the stack in the list at the index. The list and index are then both removed from the stack, while the value is retained.

The value, list, and index are specified by the third-to-top, second-to-top, and top values on the stack, respectively.

§

LoadIndexed

Load the value at the specified index in the list onto the stack. The list and index are then both removed from the stack.

The list and index are specified by the second-to-top and the top values on the stack, respectively.

§

Drop

Drops the top value from the stack.

§

Binary(BinOpKind)

Performs the binary operation on the second-to-top and top stack values.

§

Unary(UnaryOpKind)

Performs the unary operation on the top stack value.

§

Call(usize)

Call the function at the top of the value stack, passing the specified number of arguments.

Arguments are passed to the function via the value stack. The function will be popped from the stack first, followed by the arguments in reverse order.

§Span information

[
    0: outer_span[0], // span including the function name to the opening parenthesis
    1: outer_span[1], // closing parenthesis
    2: arg1,
    3: arg2,
    ...
]
§

CallDerivative(u8, usize)

Computes the nth numerical derivative of the function at the top of the stack.

The number of arguments included in the call is specified by the usize value. It is important to note that CalcScript does not support derivatives of non-unary functions. The purpose of this field is to provide a way to discover violations of this rule at runtime, when the actual function can be resolved, and report corresponding errors.

§

Return

Returns from the current function.

§

Jump(Label)

Jump to the specified label.

§

JumpIfTrue(Label)

Jumps to the specified label if the top value on the stack is the boolean true.

This will result in an error if the top value is not a boolean.

§

JumpIfFalse(Label)

Jumps to the specified label if the top value on the stack is the boolean false.

This will result in an error if the top value is not a boolean.

Trait Implementations§

Source§

impl Clone for InstructionKind

Source§

fn clone(&self) -> InstructionKind

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 InstructionKind

Source§

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

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

impl From<InstructionKind> for Instruction

Source§

fn from(kind: InstructionKind) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for InstructionKind

Source§

fn eq(&self, other: &InstructionKind) -> 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 StructuralPartialEq for InstructionKind

Auto Trait Implementations§

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> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
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<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
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> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
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.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.