39 std::ofstream file(outputPath);
50 catch (
const std::exception &)
65 output <<
"// Phasor VM Program\n";
67 output <<
"#pragma once\n";
68 output <<
"#include <cstddef>\n";
69 output <<
"#include <string>\n\n";
80 const std::string sectionPrefixPragma =
"#pragma section(\".phsb\", read)\n";
81 const std::string sectionAttr =
"__declspec(allocate(\".phsb\")) ";
82#elif defined(__APPLE__)
83 const std::string sectionPrefixPragma =
"";
84 const std::string sectionAttr =
"__attribute__((section(\"__DATA,__phsb\"))) ";
85#elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
86 const std::string sectionPrefixPragma =
"";
87 const std::string sectionAttr =
"__attribute__((section(\".phsb\"))) ";
89 const std::string sectionPrefixPragma =
"";
90 const std::string sectionAttr =
"";
93 if (!sectionPrefixPragma.empty())
95 output << sectionPrefixPragma;
100 output << sectionAttr <<
"constexpr unsigned char embeddedBytecode[] = {\n";
126 output << std::dec <<
"\n};\n";
131 std::vector<unsigned char> result;
132 std::istringstream stream(input);
135 while (stream >> token)
138 if (token.size() >= 3 && token[0] ==
'0' && (token[1] ==
'x' || token[1] ==
'X'))
141 std::istringstream hexStream(token);
142 hexStream >> std::hex >> byte;
143 result.push_back(
static_cast<unsigned char>(
byte));
152 std::ostringstream escaped;
173 if (c >= 32 && c <= 126)
179 escaped <<
"\\x" << std::hex << std::setw(2) << std::setfill(
'0')
180 <<
static_cast<int>(
static_cast<unsigned char>(c));
185 return escaped.str();
212 if ((std::isalnum(c) != 0) || c ==
'_')
223 if (!result.empty() && (std::isdigit(result[0]) != 0))
225 result =
"_" + result;
228 return result.empty() ?
"PhasorModule" : result;
Bytecode binary format deserializer.
Bytecode deserialize(const std::vector< uint8_t > &data)
Deserialize bytecode from binary buffer.
Bytecode binary format serializer.
std::vector< uint8_t > serialize(const Bytecode &bytecode)
Serialize bytecode to binary buffer.
std::vector< uint8_t > serializedBytecode
Serialized bytecode in .phsb format.
void generateFileHeader()
static std::string getValueTypeString(ValueType type)
void generateModuleName()
bool generate(const Bytecode &bytecode, const std::filesystem::path &outputPath, const std::string &moduleName="")
Generate C++ header file from bytecode.
static std::string escapeString(const std::string &str)
void generateEmbeddedBytecode()
static std::vector< unsigned char > parseEmbeddedBytecode(const std::string &input)
const Bytecode * bytecode
std::ostringstream output
Output stream for generated code.
static std::string sanitizeModuleName(const std::string &name)
Bytecode generateBytecodeFromEmbedded(const std::string &input)
Generate Bytecode object from embedded bytecode string.
The Phasor Programming Language and Runtime.
ValueType
Runtime value types for the VM.
Complete bytecode structure.