Operation codes#

class unit.OpCode#

Enum of stack machine instructions. These correspond to the C API’s UNIT_OP_* constants.

Most users will not need this enum directly – the methods on Procedure are the preferred way to emit instructions. This enum is exposed for advanced use cases like building generic instruction emitters.

Example#
# Preferred: use Procedure methods
proc.load_integer(42)
proc.add()

# Advanced: use OpCode enum directly
proc._add_op_int(OpCode.LOAD_INTEGER, 42)

Constants

LOAD_INTEGER#

Push an integer constant. See UNIT_OP_LOAD_INTEGER.

LOAD_STRING#

Push a string constant. See UNIT_OP_LOAD_STRING.

LOAD_ARGUMENT#

Push a function argument. See UNIT_OP_LOAD_ARGUMENT.

LOAD_LOCAL#

Push a local variable. See UNIT_OP_LOAD_LOCAL.

STORE_LOCAL#

Pop into a local variable. See UNIT_OP_STORE_LOCAL.

Arithmetic

ADD#

See UNIT_OP_ADD.

SUBTRACT#

See UNIT_OP_SUBTRACT.

MULTIPLY#

See UNIT_OP_MULTIPLY.

DIVIDE#

See UNIT_OP_DIVIDE.

MODULO#

See UNIT_OP_MODULO.

Comparisons

COMPARE_EQUAL#

See UNIT_OP_COMPARE_EQUAL.

COMPARE_NOT_EQUAL#

See UNIT_OP_COMPARE_NOT_EQUAL.

COMPARE_LESS#

See UNIT_OP_COMPARE_LESS.

COMPARE_LESS_EQUAL#

See UNIT_OP_COMPARE_LESS_EQUAL.

COMPARE_GREATER#

See UNIT_OP_COMPARE_GREATER.

COMPARE_GREATER_EQUAL#

See UNIT_OP_COMPARE_GREATER_EQUAL.

Control Flow

JUMP#

Unconditional jump. See UNIT_OP_JUMP_TO.

JUMP_IF_TRUE#

Jump if comparison is true. See UNIT_OP_JUMP_IF_TRUE.

JUMP_IF_FALSE#

Jump if comparison is false. See UNIT_OP_JUMP_IF_FALSE.

Calls

PREPARE_CALL#

Internal: prepare a call frame. See UNIT_OP_PREPARE_CALL.

CALL_NAME#

Call an external function by name. See UNIT_OP_CALL_NAME.

CALL_PROCEDURE#

Call a UNIT procedure. See UNIT_OP_CALL_PROCEDURE.

RETURN_VALUE#

Return from procedure. See UNIT_OP_RETURN_VALUE.

EXIT#

Terminate the process. See UNIT_OP_EXIT.

Stack Operations

COPY#

Duplicate a stack item. See UNIT_OP_COPY.

SWAP#

Swap stack items. See UNIT_OP_SWAP.

POP#

Discard top of stack. See UNIT_OP_POP.

Memory

READ_BYTES#

Read from memory. See UNIT_OP_READ_BYTES.

WRITE_BYTES#

Write to memory. See UNIT_OP_WRITE_BYTES.

ADDRESS_OF#

Push address of a local. See UNIT_OP_ADDRESS_OF.

Type Conversion

CONVERT#

Convert integer width. See UNIT_OP_CONVERT.