19 for (uint32_t i = 0; i < 256; i++)
22 for (
int j = 0; j < 8; j++)
26 crc = (crc >> 1) ^ 0xEDB88320;
48 uint32_t crc = 0xFFFFFFFF;
49 for (
size_t i = 0; i < size; i++)
51 crc = (crc >> 8) ^
crc32_table[(crc ^ data[i]) & 0xFF];
53 return crc ^ 0xFFFFFFFF;
60 throw std::runtime_error(
"Unexpected end of bytecode data");
68 value |=
static_cast<uint16_t
>(
readUInt8());
69 value |=
static_cast<uint16_t
>(
readUInt8()) << 8;
76 value |=
static_cast<uint32_t
>(
readUInt8());
77 value |=
static_cast<uint32_t
>(
readUInt8()) << 8;
78 value |=
static_cast<uint32_t
>(
readUInt8()) << 16;
79 value |=
static_cast<uint32_t
>(
readUInt8()) << 24;
91 for (
int i = 0; i < 8; i++)
93 value |=
static_cast<int64_t
>(
readUInt8()) << (i * 8);
101 for (
int i = 0; i < 8; i++)
103 bits |=
static_cast<uint64_t
>(
readUInt8()) << (i * 8);
106 std::memcpy(&value, &bits,
sizeof(
double));
115 for (uint16_t i = 0; i < length; i++)
117 str.push_back(
static_cast<char>(
readUInt8()));
127 throw std::runtime_error(
"Invalid bytecode file: incorrect magic number");
133 throw std::runtime_error(
"Incompatible bytecode version");
147 throw std::runtime_error(
"Expected constant pool section");
153 for (uint32_t i = 0; i < count; i++)
187 throw std::runtime_error(
"Unknown value type in constant pool");
197 throw std::runtime_error(
"Expected variable mapping section");
203 for (uint32_t i = 0; i < count; i++)
216 throw std::runtime_error(
"Expected instructions section");
222 for (uint32_t i = 0; i < count; i++)
239 throw std::runtime_error(
"Expected function entries section");
244 for (uint32_t i = 0; i < count; i++)
254 _data = buffer.data();
261 uint32_t expectedChecksum;
268 if (actualChecksum != expectedChecksum)
270 throw std::runtime_error(
"Bytecode file corrupted: checksum mismatch");
284 std::ifstream file(filename, std::ios::binary | std::ios::ate);
287 throw std::runtime_error(
"Failed to open bytecode file: " + filename.string());
290 std::streamsize size = file.tellg();
291 file.seekg(0, std::ios::beg);
293 std::vector<uint8_t> buffer(size);
294 if (!file.read(
reinterpret_cast<char *
>(buffer.data()), size))
296 throw std::runtime_error(
"Failed to read bytecode file: " + filename.string());
const uint8_t SECTION_CONSTANTS
const uint8_t SECTION_VARIABLES
void init_crc32_table_deserializer()
Deserialize CRC32 Table.
const uint8_t SECTION_FUNCTIONS
const uint8_t SECTION_INSTRUCTIONS
static uint32_t crc32_table[256]
static bool crc32_table_initialized
uint32_t readUInt32()
Helper method to read UInt32.
void readInstructions(Bytecode &bytecode)
Helper method to read Instuctions Table.
uint32_t calculateCRC32(const uint8_t *data, size_t size)
Calculate CRC32 checksum.
void readHeader(uint32_t &checksum)
Helper method to read Header.
void readConstantPool(Bytecode &bytecode)
Helper method to read Constants Table.
Bytecode loadFromFile(const std::filesystem::path &filename)
Load bytecode from .phsb file.
void readFunctionEntries(Bytecode &bytecode)
Helper method to read Function Entries.
uint16_t readUInt16()
Helper method to read UInt16.
Bytecode deserialize(const std::vector< uint8_t > &data)
Deserialize bytecode from binary buffer.
void readVariableMapping(Bytecode &bytecode)
Helper method to read Variable Table.
uint8_t readUInt8()
Helper method to read UInt8.
int32_t readInt32()
Helper method to read Int32.
A value in the Phasor VM.
The Phasor Programming Language and Runtime.
Complete bytecode structure.
std::vector< Value > constants
Constant pool.
std::map< std::string, int > functionEntries
Function name -> instruction index mapping.
int nextVarIndex
Next available variable index.
std::map< std::string, int > variables
Variable name -> index mapping.
std::vector< Instruction > instructions
List of instructions.
Instruction with up to 5 operands Format: instruction operand1, operand2, operand3,...