24 for (
int j = 0; j < 8; j++)
27 crc = (crc >> 1) ^ 0xEDB88320;
46 crc = (crc >> 8) ^
crc32_table[(crc ^
byte) & 0xFF];
47 return crc ^ 0xFFFFFFFF;
61 buffer.push_back(
static_cast<u8>(value & 0xFF));
62 buffer.push_back(
static_cast<u8>((value >> 8) & 0xFF));
67 buffer.push_back(
static_cast<u8>(value & 0xFF));
68 buffer.push_back(
static_cast<u8>((value >> 8) & 0xFF));
69 buffer.push_back(
static_cast<u8>((value >> 16) & 0xFF));
70 buffer.push_back(
static_cast<u8>((value >> 24) & 0xFF));
80 for (
int i = 0; i < 8; i++)
81 buffer.push_back(
static_cast<u8>((value >> (i * 8)) & 0xFF));
87 std::memcpy(&bits, &value,
sizeof(
f64));
88 for (
int i = 0; i < 8; i++)
89 buffer.push_back(
static_cast<u8>((bits >> (i * 8)) & 0xFF));
96 buffer.push_back(
static_cast<u8>(c));
145 for (
const auto &[fieldName, fieldVal] :
s->fields)
158 for (
const auto &elem : *a)
164 throw std::runtime_error(
"BytecodeSerializer::writeValue: unknown ValueType");
184 for (
const auto &val : constants)
194 for (
const auto &[name, index] : variables)
205 for (
const auto &instr : instructions)
218 for (
const auto &[name, address] : functionEntries)
241 for (
const auto &info : structs)
246 for (
const auto &fieldName : info.fieldNames)
260 for (
int i = 0; i < 16; i++)
263 size_t dataStartPos =
buffer.size();
270 std::vector<u8> dataSection(
buffer.begin() + dataStartPos,
buffer.end());
273 std::vector<u8> tempBuffer =
buffer;
276 buffer.insert(
buffer.end(), tempBuffer.begin() + 16, tempBuffer.end());
285 std::vector<u8> data =
serialize(bytecode);
286 std::ofstream file(filename, std::ios::binary);
289 file.write(
reinterpret_cast<const char *
>(data.data()), data.size());
292 catch (
const std::exception &)
const Phasor::u8 SECTION_FUNCTIONS
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
std::vector< u8 > serialize(const Bytecode &bytecode)
Serialize bytecode to binary buffer.
void writeDouble(f64 value)
Helper method to write Double.
void writeInstructions(const std::vector< Instruction > &instructions)
Helper method to write Instruction Table.
static u32 calculateCRC32(const std::vector< u8 > &data)
Calculate CRC32 checksum.
void writeString(const std::string &str)
Helper method to write String.
void writeValue(const Value &val)
Write a single Value (recursive — handles nested structs/arrays).
void writeInt64(i64 value)
Helper method to write Int64.
void writeUInt32(u32 value)
Helper method to write UInt32.
void writeInt32(i32 value)
Helper method to write Int32.
void writeStructSection(const std::vector< StructInfo > &structs)
Helper method to write Struct Section.
void writeFunctionEntries(const std::unordered_map< std::string, int > &functionEntries)
Helper method to write Function Table.
bool saveToFile(const Bytecode &bytecode, const std::filesystem::path &filename)
Save bytecode to .phsb file.
void writeHeader(u32 dataChecksum)
Section writers.
void writeUInt8(u8 value)
Helper method to write UInt8.
void writeUInt16(u16 value)
Helper method to write UInt16.
void writeConstantPool(const std::vector< Value > &constants)
Helper method to write Constants Table.
void writeVariableMapping(const std::unordered_map< std::string, int > &variables, int nextVarIndex)
Helper method to write Variable Map Table.
A value in the Phasor VM.
PhsString asString() const noexcept
Get the value as a Small String.
std::shared_ptr< StructInstance > asStruct()
std::shared_ptr< ArrayInstance > asArray()
Get the value as an array.
ValueType getType() const noexcept
Get the type of the value.
f64 asFloat() const noexcept
Get the value as a f64.
i64 asInt() const noexcept
Get the value as an integer.
bool asBool() const noexcept
Get the value as a boolean.
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.