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

Public Member Functions

None __init__ (self)
bytes serialize (self, Bytecode bytecode)
None save_to_file (self, Bytecode bytecode, Path path)

Protected Member Functions

None _write_constant_pool (self, List[Value] constants)
None _write_variable_mapping (self, Dict[str, int] variables, int next_var_index)
None _write_function_entries (self, Dict[str, int] function_entries)
None _write_instructions (self, List[Instruction] instructions)
None _write_value (self, Value value)
None _write_uint8 (self, int v)
None _write_uint16 (self, int v)
None _write_uint32 (self, int v)
None _write_int32 (self, int v)
None _write_int64 (self, int v)
None _write_double (self, float v)
None _write_string (self, str s)

Protected Attributes

bytearray _buf = bytearray()

Detailed Description

Converts a :class:`~phasor.Bytecode.Bytecode` object into its binary ``.phsb`` representation.

Definition at line 23 of file Serializer.py.

Constructor & Destructor Documentation

◆ __init__()

None phasor.Serializer.BytecodeSerializer.__init__ ( self)
Initialise the serializer with an empty write buffer.

Definition at line 26 of file Serializer.py.

Member Function Documentation

◆ _write_constant_pool()

None phasor.Serializer.BytecodeSerializer._write_constant_pool ( self,
List[Value] constants )
protected
Write the :data:`~phasor.Metadata.SEC_CONSTANTS` section: count followed by each :class:`~phasor.Value.Value`.

Definition at line 73 of file Serializer.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _write_double()

None phasor.Serializer.BytecodeSerializer._write_double ( self,
float v )
protected
Append a little-endian IEEE 754 double to the buffer.

Definition at line 154 of file Serializer.py.

Here is the caller graph for this function:

◆ _write_function_entries()

None phasor.Serializer.BytecodeSerializer._write_function_entries ( self,
Dict[str, int] function_entries )
protected
Write the :data:`~phasor.Metadata.SEC_FUNCTIONS` section: count then each name→instruction-index entry point.

Definition at line 91 of file Serializer.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _write_instructions()

None phasor.Serializer.BytecodeSerializer._write_instructions ( self,
List[Instruction] instructions )
protected
Write the :data:`~phasor.Metadata.SEC_INSTRUCTIONS` section: count then each :class:`~phasor.Instruction.Instruction` as ``uint8`` opcode + five ``int32`` operands.

Definition at line 99 of file Serializer.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _write_int32()

None phasor.Serializer.BytecodeSerializer._write_int32 ( self,
int v )
protected
Append a little-endian signed 32-bit integer to the buffer.

Definition at line 146 of file Serializer.py.

Here is the caller graph for this function:

◆ _write_int64()

None phasor.Serializer.BytecodeSerializer._write_int64 ( self,
int v )
protected
Append a little-endian signed 64-bit integer to the buffer.

Definition at line 150 of file Serializer.py.

Here is the caller graph for this function:

◆ _write_string()

None phasor.Serializer.BytecodeSerializer._write_string ( self,
str s )
protected
Append a length-prefixed UTF-8 string (uint16 length + bytes) to the buffer.

Definition at line 158 of file Serializer.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _write_uint16()

None phasor.Serializer.BytecodeSerializer._write_uint16 ( self,
int v )
protected
Append a little-endian unsigned 16-bit integer to the buffer.

Definition at line 138 of file Serializer.py.

Here is the caller graph for this function:

◆ _write_uint32()

None phasor.Serializer.BytecodeSerializer._write_uint32 ( self,
int v )
protected
Append a little-endian unsigned 32-bit integer to the buffer.

Definition at line 142 of file Serializer.py.

Here is the caller graph for this function:

◆ _write_uint8()

None phasor.Serializer.BytecodeSerializer._write_uint8 ( self,
int v )
protected
Append a single unsigned byte to the buffer.

Definition at line 134 of file Serializer.py.

Here is the caller graph for this function:

◆ _write_value()

None phasor.Serializer.BytecodeSerializer._write_value ( self,
Value value )
protected
Write a :class:`~phasor.Value.Value` as a ``uint8`` type tag followed by its payload.

Raises:
    NotImplementedError: If :attr:`value.type <phasor.Value.Value.type>` is not one of
        Null, Bool, Int, Float, or String.

Definition at line 109 of file Serializer.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _write_variable_mapping()

None phasor.Serializer.BytecodeSerializer._write_variable_mapping ( self,
Dict[str, int] variables,
int next_var_index )
protected
Write the :data:`~phasor.Metadata.SEC_VARIABLES` section: count, :attr:`~phasor.Bytecode.Bytecode.next_var_index`, then each name→slot pair.

Definition at line 80 of file Serializer.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ save_to_file()

None phasor.Serializer.BytecodeSerializer.save_to_file ( self,
Bytecode bytecode,
Path path )
Serialise *bytecode* and write the result to *path*.

Parent directories are created automatically if they do not exist.

Args:
    bytecode: The :class:`~phasor.Bytecode.Bytecode` object to serialise.
    path: Destination file path; typically ends with ``.phsb``.

Definition at line 60 of file Serializer.py.

Here is the call graph for this function:

◆ serialize()

bytes phasor.Serializer.BytecodeSerializer.serialize ( self,
Bytecode bytecode )
Serialise *bytecode* to the ``.phsb`` wire format.

Writes a 16-byte header (magic, version, flags, CRC-32 checksum) followed
by the constants, variables, functions, and instructions sections in order.

Args:
    bytecode: The :class:`~phasor.Bytecode.Bytecode` object to serialise.

Returns:
    The complete ``.phsb`` binary as a :class:`bytes` object.

Definition at line 31 of file Serializer.py.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ _buf

phasor.Serializer.BytecodeSerializer._buf = bytearray()
protected

Definition at line 28 of file Serializer.py.


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