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 n
th 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
impl Clone for InstructionKind
Source§fn clone(&self) -> InstructionKind
fn clone(&self) -> InstructionKind
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more