20 for (uint32_t i = 0; i < 256; i++)
23 for (
int j = 0; j < 8; j++)
27 crc = (crc >> 1) ^ 0xEDB88320;
49 uint32_t crc = 0xFFFFFFFF;
50 for (uint8_t
byte : data)
52 crc = (crc >> 8) ^
crc32_table[(crc ^
byte) & 0xFF];
54 return crc ^ 0xFFFFFFFF;
64 buffer.push_back(
static_cast<uint8_t
>(value & 0xFF));
65 buffer.push_back(
static_cast<uint8_t
>((value >> 8) & 0xFF));
70 buffer.push_back(
static_cast<uint8_t
>(value & 0xFF));
71 buffer.push_back(
static_cast<uint8_t
>((value >> 8) & 0xFF));
72 buffer.push_back(
static_cast<uint8_t
>((value >> 16) & 0xFF));
73 buffer.push_back(
static_cast<uint8_t
>((value >> 24) & 0xFF));
83 for (
int i = 0; i < 8; i++)
85 buffer.push_back(
static_cast<uint8_t
>((value >> (i * 8)) & 0xFF));
92 std::memcpy(&bits, &value,
sizeof(
double));
93 for (
int i = 0; i < 8; i++)
95 buffer.push_back(
static_cast<uint8_t
>((bits >> (i * 8)) & 0xFF));
104 buffer.push_back(
static_cast<uint8_t
>(c));
119 writeUInt32(
static_cast<uint32_t
>(constants.size()));
121 for (
const auto &constant : constants)
148#warning "Warning: PHS_01 Structs have not been implemented!"
149 throw std::runtime_error(
"Structs have not been implemented!");
152#warning "Warning: PHS_02 Arrays have not been implemented!"
153 throw std::runtime_error(
"Arrays have not been implemented!");
162 writeUInt32(
static_cast<uint32_t
>(variables.size()));
165 for (
const auto &[name, index] : variables)
175 writeUInt32(
static_cast<uint32_t
>(instructions.size()));
177 for (
const auto &instr : instructions)
189 writeUInt32(
static_cast<uint32_t
>(functionEntries.size()));
191 for (
const auto &[name, address] : functionEntries)
202 for (
int i = 0; i < 16; i++)
208 size_t dataStartPos =
buffer.size();
215 std::vector<uint8_t> dataSection(
buffer.begin() + dataStartPos,
buffer.end());
219 std::vector<uint8_t> tempBuffer =
buffer;
224 buffer.insert(
buffer.end(), tempBuffer.begin() + 16, tempBuffer.end());
233 std::vector<uint8_t> data =
serialize(bytecode);
235 std::ofstream file(filename, std::ios::binary);
241 file.write(
reinterpret_cast<const char *
>(data.data()), data.size());
246 catch (
const std::exception &)
const uint8_t SECTION_CONSTANTS
const uint8_t SECTION_VARIABLES
const uint8_t SECTION_FUNCTIONS
const uint8_t SECTION_INSTRUCTIONS
static uint32_t crc32_table[256]
static bool crc32_table_initialized
void init_crc32_table()
Init CRC32 Table.
void writeUInt16(uint16_t value)
Helper method to write UInt16.
void writeInstructions(const std::vector< Instruction > &instructions)
Helper method to write Instruction Table.
void writeString(const std::string &str)
Helper method to write String.
void writeInt64(int64_t value)
Helper method to write Int64.
void writeUInt8(uint8_t value)
Helper method to write UInt8.
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.
std::vector< uint8_t > buffer
void writeInt32(int32_t value)
Helper method to write Int32.
static uint32_t calculateCRC32(const std::vector< uint8_t > &data)
Calculate CRC32 checksum.
std::vector< uint8_t > serialize(const Bytecode &bytecode)
Serialize bytecode to binary buffer.
void writeHeader(uint32_t dataChecksum)
Section writers.
void writeConstantPool(const std::vector< Value > &constants)
Helper method to write Constants Table.
void writeUInt32(uint32_t value)
Helper method to write UInt32.
void writeDouble(double value)
Helper method to write Double.
void writeVariableMapping(const std::unordered_map< std::string, int > &variables, int nextVarIndex)
Helper method to write Variable Map Table.
The Phasor Programming Language and Runtime.
ValueType
Runtime value types for the VM.
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.