Phasor 3.3.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
BytecodeSerializer.hpp
Go to the documentation of this file.
1#pragma once
2#include "../CodeGen.hpp"
3#include <cstdint>
4#include <fstream>
5#include <string>
6#include <vector>
7#include <filesystem>
8#include <unordered_map>
9#include <phsint.hpp>
10
12namespace Phasor
13{
14
20{
21 public:
23 std::vector<u8> serialize(const Bytecode &bytecode);
24
26 bool saveToFile(const Bytecode &bytecode, const std::filesystem::path &filename);
27
28 private:
29 std::vector<u8> buffer;
30
31 void writeUInt8(u8 value);
32 void writeUInt16(u16 value);
33 void writeUInt32(u32 value);
34 void writeInt32(i32 value);
35 void writeInt64(i64 value);
36 void writeDouble(f64 value);
37 void writeString(const std::string &str);
38
40 void writeValue(const Value &val);
41
43 void writeHeader(u32 dataChecksum);
44 void writeConstantPool(const std::vector<Value> &constants);
45 void writeVariableMapping(const std::unordered_map<std::string, int> &variables,
46 int nextVarIndex);
47 void writeInstructions(const std::vector<Instruction> &instructions);
49 const std::unordered_map<std::string, int> &functionEntries);
50 void writeStructSection(const std::vector<StructInfo> &structs);
51
53 static u32 calculateCRC32(const std::vector<u8> &data);
54};
55} // namespace Phasor
Bytecode binary format serializer.
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.
Definition Value.hpp:59
The Phasor Programming Language and Runtime.
Definition AST.hpp:13
int64_t i64
Definition phsint.hpp:16
uint8_t u8
Definition phsint.hpp:9
double f64
Definition phsint.hpp:7
int32_t i32
Definition phsint.hpp:15
uint16_t u16
Definition phsint.hpp:10
uint32_t u32
Definition phsint.hpp:11
Complete bytecode structure.
Definition CodeGen.hpp:50