9#define COMPILE_MESSAGE(msg) __pragma(message(msg))
10#elif defined(__GNUC__) || defined(__clang__)
11#define DO_PRAGMA(x) _Pragma(#x)
12#define COMPILE_MESSAGE(msg) DO_PRAGMA(message msg)
14#define COMPILE_MESSAGE(msg)
31 for (uint32_t i = 0; i < 256; i++)
34 for (
int j = 0; j < 8; j++)
38 crc = (crc >> 1) ^ 0xEDB88320;
60 uint32_t crc = 0xFFFFFFFF;
61 for (uint8_t
byte : data)
63 crc = (crc >> 8) ^
crc32_table[(crc ^
byte) & 0xFF];
65 return crc ^ 0xFFFFFFFF;
75 buffer.push_back(
static_cast<uint8_t
>(value & 0xFF));
76 buffer.push_back(
static_cast<uint8_t
>((value >> 8) & 0xFF));
81 buffer.push_back(
static_cast<uint8_t
>(value & 0xFF));
82 buffer.push_back(
static_cast<uint8_t
>((value >> 8) & 0xFF));
83 buffer.push_back(
static_cast<uint8_t
>((value >> 16) & 0xFF));
84 buffer.push_back(
static_cast<uint8_t
>((value >> 24) & 0xFF));
94 for (
int i = 0; i < 8; i++)
96 buffer.push_back(
static_cast<uint8_t
>((value >> (i * 8)) & 0xFF));
103 std::memcpy(&bits, &value,
sizeof(
double));
104 for (
int i = 0; i < 8; i++)
106 buffer.push_back(
static_cast<uint8_t
>((bits >> (i * 8)) & 0xFF));
115 buffer.push_back(
static_cast<uint8_t
>(c));
130 writeUInt32(
static_cast<uint32_t
>(constants.size()));
132 for (
const auto &constant : constants)
159COMPILE_MESSAGE(
"Warning: PHS_01 Structs have not been implemented! Line " STR(__LINE__))
160 throw std::runtime_error(
"Structs have not been implemented!");
163COMPILE_MESSAGE(
"Warning: PHS_02 Arrays have not been implemented! Line " STR(__LINE__))
164 throw std::runtime_error(
"Arrays have not been implemented!");
174 writeUInt32(
static_cast<uint32_t
>(variables.size()));
177 for (
const auto &[name, index] : variables)
187 writeUInt32(
static_cast<uint32_t
>(instructions.size()));
189 for (
const auto &instr : instructions)
203 writeUInt32(
static_cast<uint32_t
>(functionEntries.size()));
205 for (
const auto &[name, address] : functionEntries)
216 for (
int i = 0; i < 16; i++)
222 size_t dataStartPos =
buffer.size();
229 std::vector<uint8_t> dataSection(
buffer.begin() + dataStartPos,
buffer.end());
233 std::vector<uint8_t> tempBuffer =
buffer;
238 buffer.insert(
buffer.end(), tempBuffer.begin() + 16, tempBuffer.end());
247 std::vector<uint8_t> data =
serialize(bytecode);
249 std::ofstream file(filename, std::ios::binary);
255 file.write(
reinterpret_cast<const char *
>(data.data()), data.size());
260 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
#define COMPILE_MESSAGE(msg)
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 writeFunctionEntries(const std::map< std::string, int > &functionEntries)
Helper method to write Function Table.
void writeUInt8(uint8_t value)
Helper method to write UInt8.
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.
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::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::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.