11#define COMPILE_MESSAGE(msg) __pragma(message(msg))
12#elif defined(__GNUC__) || defined(__clang__)
13#define DO_PRAGMA(x) _Pragma(#x)
14#define COMPILE_MESSAGE(msg) DO_PRAGMA(message msg)
16#define COMPILE_MESSAGE(msg)
179 if (operandIndex == 0)
181 if (operandIndex == 1)
186 if (operandIndex == 0)
188 if (operandIndex == 1)
193 if (operandIndex == 0)
195 if (operandIndex == 1)
206 if (
static_cast<int>(op) >=
static_cast<int>(
OpCode::MOV))
216 std::stringstream ss;
247 for (
size_t i = 0; i < str.length(); ++i)
249 if (str[i] ==
'\\' && i + 1 < str.length())
270 result += str[i + 1];
285 std::stringstream ss;
288 ss <<
".PHIR 3.0.0" <<
"\n";
291 std::map<int, std::string> indexToVarName;
292 for (
const auto &[name, index] : bytecode.
variables)
294 indexToVarName[index] = name;
296 std::map<int, std::string> addressToFuncName;
299 addressToFuncName[address] = name;
303 ss <<
".CONSTANTS " << bytecode.
constants.size() <<
"\n";
304 for (
const auto &val : bytecode.
constants)
306 switch (val.getType())
312 ss <<
"BOOL " << (val.asBool() ?
"true" :
"false") <<
"\n";
315 ss <<
"INT " << val.asInt() <<
"\n";
318 ss <<
"FLOAT " << val.asFloat() <<
"\n";
321 ss <<
"STRING \"" <<
escapeString(val.asString()) <<
"\"\n";
324 COMPILE_MESSAGE(
"Warning: PHS_01 Structs have not been fully implemented! Line " STR(__LINE__))
325 throw std::runtime_error(
"Structs not implemented!");
328 COMPILE_MESSAGE(
"Warning: PHS_02 Arrays have not been implemented! Line " STR(__LINE__))
329 throw std::runtime_error(
"Arrays not implemented!");
335 for (
const auto &[name, index] : bytecode.
variables)
337 ss << name <<
" " << index <<
"\n";
344 ss << name <<
" " << address <<
"\n";
348 ss <<
".STRUCTS " << bytecode.
structs.size() <<
"\n";
349 for (
const auto &info : bytecode.
structs)
352 ss << info.name <<
" " << info.firstConstIndex <<
" " << info.fieldCount;
353 for (
const auto &fieldName : info.fieldNames)
355 ss <<
" " << fieldName;
361 ss <<
".INSTRUCTIONS " << bytecode.
instructions.size() <<
"\n";
364 std::stringstream instrLine;
368 int32_t operands[3] = {instr.operand1, instr.operand2, instr.operand3};
372 for (
int i = 0; i < operandCount; ++i)
383 instrLine <<
"r" << operands[i];
386 instrLine << operands[i];
387 if (operands[i] >= 0 && operands[i] <
static_cast<int>(bytecode.
constants.size()))
393 if (str.length() > 20)
394 str = str.substr(0, 20) +
"...";
395 comment =
"const[" + std::to_string(operands[i]) +
"]=\"" +
escapeString(str) +
"\"";
399 comment =
"const[" + std::to_string(operands[i]) +
"]=" + std::to_string(val.
asInt());
403 comment =
"const[" + std::to_string(operands[i]) +
"]=" + std::to_string(val.
asFloat());
408 instrLine << operands[i];
409 if (indexToVarName.count(operands[i]))
411 comment =
"var=" + indexToVarName[operands[i]];
415 instrLine << operands[i];
416 if (addressToFuncName.count(operands[i]))
418 comment =
"func=" + addressToFuncName[operands[i]];
422 instrLine << operands[i];
427 std::string lineStr = instrLine.str();
428 if (!comment.empty())
430 const size_t commentColumn = 40;
431 if (lineStr.length() < commentColumn)
433 lineStr.append(commentColumn - lineStr.length(),
' ');
440 lineStr +=
"; " + comment;
442 ss << lineStr <<
"\n";
445 std::string textData = ss.str();
446 std::vector<uint8_t> buffer;
449 buffer.insert(buffer.end(), textData.begin(), textData.end());
458 throw std::runtime_error(
"Invalid Phasor IR file: too small");
462 std::string textData(data.begin() + 8, data.end());
463 std::stringstream ss(textData);
468 while (ss >> section)
470 if (section ==
".PHIR")
474 if (version <
"3.0.0")
476 throw std::runtime_error(
"Incompatible Phasor IR version");
479 if (section ==
".CONSTANTS")
484 for (
int i = 0; i < count; ++i)
492 else if (type ==
"BOOL")
498 else if (type ==
"INT")
504 else if (type ==
"FLOAT")
510 else if (type ==
"STRING")
515 while (ss.get(c) && c !=
'"')
517 std::getline(ss, valStr,
'"');
522 else if (section ==
".VARIABLES")
526 for (
int i = 0; i < count; ++i)
534 else if (section ==
".FUNCTIONS")
538 for (
int i = 0; i < count; ++i)
542 ss >> name >> address;
546 else if (section ==
".STRUCTS")
550 for (
int i = 0; i < count; ++i)
557 std::string fieldName;
561 int index =
static_cast<int>(bytecode.
structs.size());
562 bytecode.
structs.push_back(std::move(info));
566 else if (section ==
".INSTRUCTIONS")
571 for (
int i = 0; i < count; ++i)
578 int32_t operands[3] = {0, 0, 0};
580 for (
int j = 0; j < operandCount; ++j)
586 if (token.empty() || token[0] ==
';')
589 std::getline(ss, token);
594 if (!token.empty() && token.back() ==
',')
600 if (!token.empty() && token[0] ==
'r')
602 operands[j] = std::stoi(token.substr(1));
606 operands[j] = std::stoi(token);
612 while (ss.get(c) && c !=
'\n')
616 Instruction(op, operands[0], operands[1], operands[2]));
628 std::vector<uint8_t> data =
serialize(bytecode);
629 std::ofstream file(filename, std::ios::binary);
632 file.write(
reinterpret_cast<const char *
>(data.data()), (std::streamsize)data.size());
643 std::ifstream file(filename, std::ios::binary | std::ios::ate);
645 throw std::runtime_error(
"Cannot open file");
646 std::streamsize size = file.tellg();
647 file.seekg(0, std::ios::beg);
648 std::vector<uint8_t> buffer(size);
649 if (!file.read(
reinterpret_cast<char *
>(buffer.data()), size))
650 throw std::runtime_error(
"Cannot read file");
#define COMPILE_MESSAGE(msg)
static std::vector< uint8_t > serialize(const Bytecode &bytecode)
Serialize bytecode to Phasor IR format.
static std::string unescapeString(const std::string &str)
Helper to unescape strings from text format.
static Bytecode deserialize(const std::vector< uint8_t > &data)
Deserialize Phasor IR format to bytecode.
OperandType
Operand types for instructions.
@ VARIABLE_IDX
Index into variable mapping.
@ CONSTANT_IDX
Index into constant pool.
@ FUNCTION_IDX
Index into function entries.
@ REGISTER
Register operand.
static OperandType getOperandType(OpCode op, int operandIndex)
static Bytecode loadFromFile(const std::filesystem::path &filename)
Load bytecode from .phir file.
static int getOperandCount(OpCode op)
static std::string escapeString(const std::string &str)
Helper to escape strings for text format.
static bool saveToFile(const Bytecode &bytecode, const std::filesystem::path &filename)
Save bytecode to .phir file.
A value in the Phasor VM.
double asFloat() const
Get the value as a double.
std::string asString() const
Get the value as a string.
int64_t asInt() const
Get the value as an integer.
ValueType getType() const
Get the type of the value.
The Phasor Programming Language and Runtime.
@ IGREATER_THAN
Pop b, pop a, push a > b.
@ IEQUAL
Pop b, pop a, push a == b.
@ SYSTEM_OUT
Call system function and push stdout.
@ LOG_R
R[rA] = log(R[rB]).
@ FLMOD_R
R[rA] = R[rB] % R[rC].
@ SUBSTR
Pop len, pop start, pop s, push s.substr(start, len).
@ IAND
Pop b, pop a, push a && b.
@ SET_FIELD_STATIC
Pop value and struct instance, set field by static offset.
@ MOV
Copy register to register: R[rA] = R[rB].
@ PRINTERROR_R
Print register to stderr: printerror(R[rA]).
@ IAND_R
R[rA] = R[rB] && R[rC].
@ FLMUL_R
R[rA] = R[rB] * R[rC].
@ IADD
Pop b, pop a, push a + b.
@ PUSH2_R
Push 2 registers to stack: push2(R[rA], R[rB]).
@ FLGE_R
R[rA] = R[rB] >= R[rC].
@ SYSTEM_R
Run an operating system shell command: system(R[rA]).
@ PUSH_CONST
Push constant from constant pool.
@ JUMP_IF_TRUE
Jump if top of stack is true (pops value).
@ FLGT_R
R[rA] = R[rB] > R[rC].
@ PUSH_R
Push register to stack: push(R[rA]).
@ POP_R
Pop stack to register: R[rA] = pop().
@ FLEQUAL
Pop b, pop a, push a == b.
@ GET_FIELD_STATIC
Pop struct instance, push field by static offset (structIndex, fieldOffset).
@ FLNOT_EQUAL
Pop b, pop a, push a != b.
@ FLADD_R
R[rA] = R[rB] + R[rC].
@ POP2_R
Pop 2 values from stack to registers: pop2(R[rA], R[rB]).
@ LOAD_CONST_R
Load constant to register: R[rA] = constants[immediate].
@ FLADD
Pop b, pop a, push a + b.
@ JUMP
Unconditional jump to offset.
@ FLLT_R
R[rA] = R[rB] < R[rC].
@ SET_FIELD
Pop struct, pop field name, pop value, set field value.
@ IMULTIPLY
Pop b, pop a, push a * b.
@ READLINE_R
Read line into register: readline(R[rA]).
@ CHAR_AT
Pop index, pop s, push s[index].
@ IMUL_R
R[rA] = R[rB] * R[rC].
@ FLGREATER_EQUAL
Pop b, pop a, push a >= b.
@ FLLE_R
R[rA] = R[rB] <= R[rC].
@ IGREATER_EQUAL
Pop b, pop a, push a >= b.
@ SYSTEM_ERR
Call system function and push stderr.
@ SQRT_R
R[rA] = sqrt(R[rB]).
@ ISUB_R
R[rA] = R[rB] - R[rC].
@ FLDIV_R
R[rA] = R[rB] / R[rC].
@ COS_R
R[rA] = cos(R[rB]).
@ CALL_NATIVE
Call a native function: operand is index of function name in constants.
@ ISUBTRACT
Pop b, pop a, push a - b.
@ FLMODULO
Pop b, pop a, push a % b.
@ NEW_STRUCT_INSTANCE_STATIC
Create new struct instance using struct section metadata (structIndex).
@ IDIVIDE
Pop b, pop a, push a / b.
@ STORE_VAR
Pop top of stack, store in variable slot.
@ ILE_R
R[rA] = R[rB] <= R[rC].
@ FLDIVIDE
Pop b, pop a, push a / b.
@ POW_R
R[rA] = pow(R[rB], R[rC]).
@ IMODULO
Pop b, pop a, push a % b.
@ FLAND_R
R[rA] = R[rB] && R[rC].
@ JUMP_IF_FALSE
Jump if top of stack is false (pops value).
@ INOT_EQUAL
Pop b, pop a, push a != b.
@ FLEQ_R
R[rA] = R[rB] == R[rC].
@ LOAD_VAR
Push variable value onto stack.
@ IOR
Pop b, pop a, push a || b.
@ EXP_R
R[rA] = exp(R[rB]).
@ RETURN
Return from function.
@ STORE_VAR_R
Store register to varible: variables[immediate] = R[rA].
@ READLINE
Read line from input and push onto stack.
@ IGT_R
R[rA] = R[rB] > R[rC].
@ FLSUBTRACT
Pop b, pop a, push a - b.
@ IADD_R
R[rA] = R[rB] + R[rC].
@ IDIV_R
R[rA] = R[rB] / R[rC].
@ FLLESS_EQUAL
Pop b, pop a, push a <= b.
@ SYSTEM_ERR_R
Run shell command and get output: system_out(R[rA], R[rB]).
@ FLMULTIPLY
Pop b, pop a, push a * b.
@ ILESS_EQUAL
Pop b, pop a, push a <= b.
@ PRINT_R
Print register: print(R[rA]).
@ PRINTERROR
Pop top of stack and print to stderr.
@ TAN_R
R[rA] = tan(R[rB]).
@ FLSUB_R
R[rA] = R[rB] - R[rC].
@ JUMP_BACK
Jump backwards (for loops).
@ FLOR_R
R[rA] = R[rB] || R[rC].
@ CALL
Call a user function: operand is index of function name in constants.
@ INE_R
R[rA] = R[rB] != R[rC].
@ FLNE_R
R[rA] = R[rB] != R[rC].
@ FLGREATER_THAN
Pop b, pop a, push a > b.
@ ILESS_THAN
Pop b, pop a, push a < b.
@ GET_FIELD
Pop struct, pop field name, push field value.
@ SIN_R
R[rA] = sin(R[rB]).
@ IMPORT
Import a module: operand is index of module path in constants.
@ LOAD_VAR_R
Load variable to register: R[rA] = variables[immediate].
@ FLLESS_THAN
Pop b, pop a, push a < b.
@ NEW_STRUCT
Create new struct: operand is index of struct name in constants.
@ IOR_R
R[rA] = R[rB] || R[rC].
@ ILT_R
R[rA] = R[rB] < R[rC].
@ IMOD_R
R[rA] = R[rB] % R[rC].
@ PRINT
Pop top of stack and print.
@ SYSTEM
Call a system function: operand is index of function name in constants.
@ IEQ_R
R[rA] = R[rB] == R[rC].
@ IGE_R
R[rA] = R[rB] >= R[rC].
OpCode stringToOpCode(const std::string &str)
std::string opCodeToString(OpCode op)
Complete bytecode structure.
std::vector< StructInfo > structs
List of struct descriptors.
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 > structEntries
Struct name -> index in structs.
std::map< std::string, int > variables
Variable name -> index mapping.
std::vector< Instruction > instructions
List of instructions.
Instruction with up to 5 operands Format: instruction operand1, operand2, operand3 Each instruction u...
Struct metadata stored alongside bytecode (struct section).
int firstConstIndex
Index into constants for the first default value.
std::vector< std::string > fieldNames
Field names in declaration order.
int fieldCount
Number of fields in this struct.
std::string name
Struct name.