23 for (
int j = 0; j < 8; j++)
26 crc = (crc >> 1) ^ 0xEDB88320;
44 for (
size_t i = 0; i < size; i++)
45 crc = (crc >> 8) ^
crc32_table[(crc ^ data[i]) & 0xFF];
46 return crc ^ 0xFFFFFFFF;
56 throw std::runtime_error(
"Unexpected end of bytecode data");
86 for (
int i = 0; i < 8; i++)
94 for (
int i = 0; i < 8; i++)
97 std::memcpy(&value, &bits,
sizeof(
f64));
106 for (
u16 i = 0; i < length; i++)
107 str.push_back(
static_cast<char>(
readUInt8()));
141 for (
u32 i = 0; i < fieldCount; ++i)
153 std::vector<Value> elements;
154 elements.reserve(elementCount);
155 for (
u32 i = 0; i < elementCount; ++i)
161 throw std::runtime_error(
"BytecodeDeserializer::readValue: unknown type tag " +
162 std::to_string(typeTag));
174 throw std::runtime_error(
"Invalid bytecode file: incorrect magic number");
178 throw std::runtime_error(
"Incompatible bytecode version");
190 throw std::runtime_error(
"Expected constant pool section");
194 for (
u32 i = 0; i < count; i++)
202 throw std::runtime_error(
"Expected variable mapping section");
206 for (
u32 i = 0; i < count; i++)
218 throw std::runtime_error(
"Expected instructions section");
222 for (
u32 i = 0; i < count; i++)
236 throw std::runtime_error(
"Expected function entries section");
239 for (
u32 i = 0; i < count; i++)
256 throw std::runtime_error(
"Expected struct definitions section");
259 bytecode.
structs.reserve(structCount);
261 for (
u32 i = 0; i < structCount; i++)
272 int index =
static_cast<int>(bytecode.
structs.size());
273 bytecode.
structs.push_back(std::move(info));
284 _data = buffer.data();
290 u32 expectedChecksum;
295 if (actualChecksum != expectedChecksum)
296 throw std::runtime_error(
"Bytecode file corrupted: checksum mismatch");
310 std::ifstream file(filename, std::ios::binary | std::ios::ate);
312 throw std::runtime_error(
"Failed to open bytecode file: " + filename.string());
314 std::streamsize size = file.tellg();
315 file.seekg(0, std::ios::beg);
317 std::vector<u8> fileBuffer(size);
318 if (!file.read(
reinterpret_cast<char *
>(fileBuffer.data()), size))
319 throw std::runtime_error(
"Failed to read bytecode file: " + filename.string());
const Phasor::u8 SECTION_FUNCTIONS
void init_crc32_table_deserializer()
const Phasor::u8 SECTION_VARIABLES
const Phasor::u8 SECTION_STRUCTS
const Phasor::u8 SECTION_CONSTANTS
const Phasor::u8 SECTION_INSTRUCTIONS
static Phasor::u32 crc32_table[256]
static bool crc32_table_initialized
void readInstructions(Bytecode &bytecode)
Helper method to read Instructions Table.
i64 readInt64()
Helper method to read Int64.
void readConstantPool(Bytecode &bytecode)
Helper method to read Constants Table.
Value readValue()
Read a single Value (recursive — handles nested structs/arrays).
Bytecode deserialize(const std::vector< u8 > &data)
Deserialize bytecode from binary buffer.
Bytecode loadFromFile(const std::filesystem::path &filename)
Load bytecode from .phsb file.
void readFunctionEntries(Bytecode &bytecode)
Helper method to read Function Entries.
u16 readUInt16()
Helper method to read UInt16.
u32 readUInt32()
Helper method to read UInt32.
static u32 calculateCRC32(const u8 *data, size_t size)
Calculate CRC32 checksum.
void readStructSection(Bytecode &bytecode)
Helper method to read Struct Section.
u8 readUInt8()
Helper method to read UInt8.
void readVariableMapping(Bytecode &bytecode)
Helper method to read Variable Table.
f64 readDouble()
Helper method to read Double.
void readHeader(u32 &checksum)
Helper method to read Header.
i32 readInt32()
Helper method to read Int32.
std::string readString()
Helper method to read String.
A value in the Phasor VM.
static Value createStruct(const PhsString &name)
void setField(const PhsString &name, Value value)
static Value createArray(std::vector< Value > elements={})
The Phasor Programming Language and Runtime.
Complete bytecode structure.
std::unordered_map< std::string, int > variables
Variable name -> index mapping.
std::vector< Instruction > instructions
List of instructions.
std::unordered_map< std::string, int > functionEntries
Function name -> instruction index mapping.
int nextVarIndex
Next available variable index.
std::vector< Value > constants
Constant pool.
std::vector< StructInfo > structs
List of struct descriptors.
std::unordered_map< std::string, int > structEntries
Struct name -> index in structs.
Struct metadata stored alongside bytecode (struct section).
int firstConstIndex
Index into constants for the first default value.
std::vector< std::string > fieldNames
Field names in declaration order.
int fieldCount
Number of fields in this struct.
std::string name
Struct name.