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>
10namespace Phasor
11{
12
18{
19 public:
21 std::vector<uint8_t> serialize(const Bytecode &bytecode);
22
24 bool saveToFile(const Bytecode &bytecode, const std::filesystem::path &filename);
25
26 private:
27 std::vector<uint8_t> buffer;
28
29 void writeUInt8(uint8_t value);
30 void writeUInt16(uint16_t value);
31 void writeUInt32(uint32_t value);
32 void writeInt32(int32_t value);
33 void writeInt64(int64_t value);
34 void writeDouble(double value);
35 void writeString(const std::string &str);
36
38 void writeHeader(uint32_t dataChecksum);
39 void writeConstantPool(const std::vector<Value> &constants);
40 void writeVariableMapping(const std::unordered_map<std::string, int> &variables,
41 int nextVarIndex);
42 void writeInstructions(const std::vector<Instruction> &instructions);
44 const std::unordered_map<std::string, int> &functionEntries);
45
47 static uint32_t calculateCRC32(const std::vector<uint8_t> &data);
48};
49} // namespace Phasor
Bytecode binary format serializer.
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.
Definition AST.hpp:12
Complete bytecode structure.
Definition CodeGen.hpp:50