Phasor 3.1.1
Stack VM based Programming Language
Loading...
Searching...
No Matches
phasor.Bytecode.Bytecode Class Reference

Public Member Functions

int add_constant (self, Value value)
int find_or_add_constant (self, Value value)
int get_or_create_var (self, str name)
int emit (self, OpCode op, int op1=0, int op2=0, int op3=0)
None patch_operand1 (self, int instr_index, int value)
None save (self, Path|str path)
"Bytecode" load (cls, Path|str path)
"Bytecode" from_bytes (cls, bytes|bytearray data)
bytes to_bytes (self)
"Bytecode" from_native_binary (cls, Path|str path)
str disassemble (self)
str __repr__ (self)

Static Public Attributes

List instructions = field(default_factory=list)
List constants = field(default_factory=list)
Dict variables = field(default_factory=dict)
Dict function_entries = field(default_factory=dict)
int next_var_index = 0

Detailed Description

In-memory representation of a compiled Phasor program.

Holds all data needed to run or serialise the program: the instruction
stream, the constant pool, the variable name→slot mapping, function entry
points, and the next free variable slot counter.

Attributes:
    instructions: Ordered list of :class:`~phasor.Instruction.Instruction` objects forming the program.
    constants: Constant pool; entries are indexed by :attr:`~phasor.OpCode.OpCode.PUSH_CONST` / :attr:`~phasor.OpCode.OpCode.LOAD_CONST_R`.
    variables: Maps each variable name to its integer slot index.
    function_entries: Maps each function name to the index of its first instruction.
    next_var_index: Next available variable slot; serialised as part of the variables section.

Definition at line 20 of file Bytecode.py.

Member Function Documentation

◆ __repr__()

str phasor.Bytecode.Bytecode.__repr__ ( self)
Return a summary showing instruction, constant, variable, and function counts.

Definition at line 146 of file Bytecode.py.

◆ add_constant()

int phasor.Bytecode.Bytecode.add_constant ( self,
Value value )
Append *value* to the constant pool and return its index.

Definition at line 41 of file Bytecode.py.

Here is the caller graph for this function:

◆ disassemble()

str phasor.Bytecode.Bytecode.disassemble ( self)
Return a human-readable disassembly of :attr:`instructions`.

Function entry points from :attr:`function_entries` are printed as
``<function name>:`` labels above their first instruction.

Definition at line 128 of file Bytecode.py.

◆ emit()

int phasor.Bytecode.Bytecode.emit ( self,
OpCode op,
int op1 = 0,
int op2 = 0,
int op3 = 0 )
Append a new :class:`~phasor.Instruction.Instruction` to :attr:`instructions` and return its index.

Args:
    op: The :class:`~phasor.OpCode.OpCode` for this instruction.
    op1 … op5: Operand values; unused operands should be left as ``0``.

Returns:
    The zero-based index of the newly appended instruction.

Definition at line 61 of file Bytecode.py.

◆ find_or_add_constant()

int phasor.Bytecode.Bytecode.find_or_add_constant ( self,
Value value )
Return the index of an existing equal constant, or add a new one.

Definition at line 47 of file Bytecode.py.

Here is the call graph for this function:

◆ from_bytes()

"Bytecode" phasor.Bytecode.Bytecode.from_bytes ( cls,
bytes | bytearray data )
Deserialise a :class:`Bytecode` from a raw ``.phsb`` byte buffer.

Delegates to :class:`~phasor.Deserializer.BytecodeDeserializer`.

Definition at line 105 of file Bytecode.py.

Here is the caller graph for this function:

◆ from_native_binary()

"Bytecode" phasor.Bytecode.Bytecode.from_native_binary ( cls,
Path | str path )
Extract and deserialise bytecode from an ELF/PE/MachO binary's ``.phsb`` section.

Definition at line 122 of file Bytecode.py.

Here is the call graph for this function:

◆ get_or_create_var()

int phasor.Bytecode.Bytecode.get_or_create_var ( self,
str name )
Return the slot index for *name*, creating it if absent.

Definition at line 54 of file Bytecode.py.

◆ load()

"Bytecode" phasor.Bytecode.Bytecode.load ( cls,
Path | str path )
Read a ``.phsb`` file at *path* and return a deserialised :class:`Bytecode`.

Delegates to :class:`~phasor.Deserializer.BytecodeDeserializer`.

Definition at line 96 of file Bytecode.py.

◆ patch_operand1()

None phasor.Bytecode.Bytecode.patch_operand1 ( self,
int instr_index,
int value )
Overwrite ``operand1`` of the instruction at *instr_index* in-place.

Typically used to back-patch forward-jump offsets after the jump target
is known.

Args:
    instr_index: Index into :attr:`instructions` of the instruction to patch.
    value: New value for ``operand1``.

Definition at line 75 of file Bytecode.py.

◆ save()

None phasor.Bytecode.Bytecode.save ( self,
Path | str path )
Serialise this object and write it to a ``.phsb`` file at *path*.

Delegates to :class:`~phasor.Serializer.BytecodeSerializer`.

Definition at line 87 of file Bytecode.py.

◆ to_bytes()

bytes phasor.Bytecode.Bytecode.to_bytes ( self)
Serialise this object to a raw ``.phsb`` byte buffer.

Delegates to :class:`~phasor.Serializer.BytecodeSerializer`.

Definition at line 113 of file Bytecode.py.

Member Data Documentation

◆ constants

List phasor.Bytecode.Bytecode.constants = field(default_factory=list)
static

Definition at line 36 of file Bytecode.py.

◆ function_entries

Dict phasor.Bytecode.Bytecode.function_entries = field(default_factory=dict)
static

Definition at line 38 of file Bytecode.py.

◆ instructions

phasor.Bytecode.Bytecode.instructions = field(default_factory=list)
static

Definition at line 35 of file Bytecode.py.

◆ next_var_index

int phasor.Bytecode.Bytecode.next_var_index = 0
static

Definition at line 39 of file Bytecode.py.

◆ variables

Dict phasor.Bytecode.Bytecode.variables = field(default_factory=dict)
static

Definition at line 37 of file Bytecode.py.


The documentation for this class was generated from the following file: