Instructions

enum UNIT_Instruction

Enumerated type containing the ID for all UNIT stack-based instructions.

enumerator UNIT_OP_LOAD_STRING
enumerator UNIT_OP_LOAD_INTEGER
enumerator UNIT_OP_ADD
enumerator UNIT_OP_SUBTRACT
enumerator UNIT_OP_MULTIPLY
enumerator UNIT_OP_DIVIDE
enumerator UNIT_OP_MODULO
enumerator UNIT_OP_RETURN_VALUE
enumerator UNIT_OP_EXIT
enumerator UNIT_OP_POP
enumerator UNIT_OP_PREPARE_CALL
enumerator UNIT_OP_CALL_NAME
enumerator UNIT_OP_STORE_LOCAL
enumerator UNIT_OP_LOAD_LOCAL
enumerator UNIT_OP_ADDRESS_OF
enumerator UNIT_OP_COMPARE_EQUAL
enumerator UNIT_OP_COMPARE_NOT_EQUAL
enumerator UNIT_OP_COMPARE_GREATER
enumerator UNIT_OP_COMPARE_GREATER_EQUAL
enumerator UNIT_OP_COMPARE_LESS
enumerator UNIT_OP_COMPARE_LESS_EQUAL
enumerator UNIT_OP_JUMP_TO
enumerator UNIT_OP_JUMP_IF_TRUE
enumerator UNIT_OP_JUMP_IF_FALSE
enumerator UNIT_OP_COPY
enumerator UNIT_OP_SWAP
enumerator UNIT_OP_READ_BYTES
enumerator UNIT_OP_WRITE_BYTES
enumerator UNIT_OP_CAST
enumerator UNIT_OP_PUSH_VIRTUAL

Constants

Opcode

Effect

Description

UNIT_OP_LOAD_INTEGER

stack.push(oparg)

Push a constant 32-bit integer onto the stack.

UNIT_OP_LOAD_STRING

stack.push(strings[oparg])

Push a constant string onto the stack.

Arithmetic

Opcode

Effect

Description

UNIT_OP_ADD

stack.push(stack.pop(top - 1) + stack.pop(top))

Add two numbers together.

UNIT_OP_SUBTRACT

stack.push(stack.pop(top - 1) - stack.pop(top))

Subtract two numbers.

UNIT_OP_MULTIPLY

stack.push(stack.pop(top - 1) * stack.pop(top))

Multiply two numbers together.

UNIT_OP_DIVIDE

stack.push(stack.pop(top - 1) / stack.pop(top))

Divide two numbers.

UNIT_OP_MODULO

stack.push(stack.pop(top - 1) % stack.pop(top))

Take the remainder of two numbers.

Local Variables

Opcode

Effect

Description

UNIT_OP_STORE_LOCAL

locals[oparg] = stack.pop(top)

Store a value into a local variable.

UNIT_OP_LOAD_LOCAL

stack.push(locals[oparg])

Read the value of a local variable.

UNIT_OP_ADDRESS_OF

stack.push(&locals[oparg])

Push the memory address of a local variable.

Memory Access

Opcode

Effect

Description

UNIT_OP_READ_BYTES

stack.push(read(stack.pop(top), oparg))

Read oparg bytes from a memory address.

UNIT_OP_WRITE_BYTES

write(stack.pop(top - 1), stack.pop(top), oparg)

Write oparg bytes to a memory address.

Comparisons

Opcode

Effect

Description

UNIT_OP_COMPARE_EQUAL

stack.push(stack.pop(top - 1) == stack.pop(top))

Compare two values for equality.

UNIT_OP_COMPARE_NOT_EQUAL

stack.push(stack.pop(top - 1) != stack.pop(top))

Compare two values for inequality.

UNIT_OP_COMPARE_GREATER

stack.push(stack.pop(top - 1) > stack.pop(top))

Check whether a value is greater than another.

UNIT_OP_COMPARE_GREATER_EQUAL

stack.push(stack.pop(top - 1) >= stack.pop(top))

Check whether a value is greater than or equal to another.

UNIT_OP_COMPARE_LESS

stack.push(stack.pop(top - 1) < stack.pop(top))

Check whether a value is less than another.

UNIT_OP_COMPARE_LESS_EQUAL

stack.push(stack.pop(top - 1) <= stack.pop(top))

Check whether a value is less than or equal to another.

Control Flow

Opcode

Effect

Description

UNIT_OP_JUMP_TO

goto jump_labels[oparg]

Unconditionally jump to a label.

UNIT_OP_JUMP_IF_TRUE

if stack.pop(top): goto jump_labels[oparg]

Jump to a label if the top of the stack is nonzero.

UNIT_OP_JUMP_IF_FALSE

if not stack.pop(top): goto jump_labels[oparg]

Jump to a label if the top of the stack is zero.

Function Calls

Opcode

Effect

Description

UNIT_OP_PREPARE_CALL

Collects oparg items from the stack as call arguments.

Prepare arguments for a function call. Used internally by UNIT_Procedure_AddCall().

UNIT_OP_CALL_NAME

stack.push(names[oparg](args))

Call a named function and push its return value.

UNIT_OP_PUSH_VIRTUAL

stack.push(<parameter>)

Declare a procedure parameter. Used at the start of a procedure body.

Stack Manipulation

Opcode

Effect

Description

UNIT_OP_POP

stack.pop(top)

Discard the value on top of the stack.

UNIT_OP_COPY

stack.push(stack[top - oparg])

Duplicate a stack item. With no argument, copies the top.

UNIT_OP_SWAP

swap(stack[top], stack[top - oparg])

Swap two stack items. With no argument, swaps the top two.

Type Conversion

Opcode

Effect

Description

UNIT_OP_CAST

stack.push((type)stack.pop(top))

Cast the top of the stack to the integer type specified by oparg.

Program Control

Opcode

Effect

Description

UNIT_OP_RETURN_VALUE

return stack.pop(top)

Return a value and end execution of the current procedure.

UNIT_OP_EXIT

exit(stack.pop(top))

Exit the entire program with the given status code.