9#include <unordered_map>
66 return static_cast<int>(
constants.size()) - 1;
98 void emit(
OpCode op, int32_t op1 = 0, int32_t op2 = 0, int32_t op3 = 0)
120 int nextVarIdx = 0,
bool replMode =
false);
144 return static_cast<uint8_t
>(i);
147 throw std::runtime_error(
"Out of registers");
Code generator for Phasor VM.
void generateForStmt(const AST::ForStmt *forStmt)
Generate bytecode from For Statement.
Bytecode bytecode
Generated bytecode.
void generateCallExpr(const AST::CallExpr *callExpr)
Generate bytecode from Call Expression.
void generateVarDecl(const AST::VarDecl *varDecl)
Generate bytecode from Variable Declaration.
void generateIfStmt(const AST::IfStmt *ifStmt)
Generate bytecode from If Statement.
void generatePostfixExpr(const AST::PostfixExpr *expr, bool resultNeeded=true)
std::vector< std::vector< int > > breakJumpsStack
void generateContinueStmt()
void generateUnaryExpr(const AST::UnaryExpr *unaryExpr)
Generate bytecode from Unary Expression.
uint8_t nextRegister
Next available register.
void generateStructInstanceExpr(const AST::StructInstanceExpr *expr)
void generateNullExpr(const AST::NullExpr *nullExpr)
Generate bytecode from Null Expression.
void generateFunctionDecl(const AST::FunctionDecl *funcDecl)
Generate bytecode from Function Declaration.
std::unordered_map< std::string, ValueType > inferredTypes
Bytecode generate(const AST::Program &program, const std::unordered_map< std::string, int > &existingVars={}, int nextVarIdx=0, bool replMode=false)
Generate bytecode from program.
void generateWhileStmt(const AST::WhileStmt *whileStmt)
Generate bytecode from While Statement.
void generateSwitchStmt(const AST::SwitchStmt *switchStmt)
void generateBlockStmt(const AST::BlockStmt *blockStmt)
Generate bytecode from Block Statement.
void generateBooleanExpr(const AST::BooleanExpr *boolExpr)
Generate bytecode from Boolean Expression.
void generateStructDecl(const AST::StructDecl *decl)
uint8_t allocateRegister()
Allocate a new register.
std::vector< int > loopStartStack
void generatePrintStmt(const AST::PrintStmt *printStmt)
Generate bytecode from Print Statement.
void generateStringExpr(const AST::StringExpr *strExpr)
Generate bytecode from String Expression.
void generateExpression(const AST::Expression *expr, bool resultNeeded=true)
Generate bytecode from Expression.
void freeRegister(uint8_t reg)
Free a register.
ValueType inferExpressionType(const AST::Expression *expr, bool &known)
Simple expression type inference (conservative).
std::vector< std::vector< int > > continueJumpsStack
void generateExportStmt(const AST::ExportStmt *exportStmt)
Generate bytecode from Export Statement.
void generateUnsafeBlockStmt(const AST::UnsafeBlockStmt *unsafeStmt)
Generate bytecode from Unsafe Block Statement.
void generateBinaryExpr(const AST::BinaryExpr *binExpr)
Generate bytecode from Binary Expression.
void generateExpressionStmt(const AST::ExpressionStmt *exprStmt)
Generate bytecode from Expression Statement.
void generateImportStmt(const AST::ImportStmt *importStmt)
Generate bytecode from Import Statement.
void generateStatement(const AST::Statement *stmt)
Generate bytecode from Statement.
static bool isLiteralExpression(const AST::Expression *expr, Value &outValue)
Check if expression is a compile-time literal.
void generateIdentifierExpr(const AST::IdentifierExpr *identExpr)
Generate bytecode from Identifier Expression.
void generateAssignmentExpr(const AST::AssignmentExpr *assignExpr)
Generate bytecode from Assignment Expression.
void generateNumberExpr(const AST::NumberExpr *numExpr)
Generate bytecode from Numeral Expression.
std::vector< bool > registerInUse
Track which registers are in use.
void resetRegisters()
Reset register allocator.
void generateFieldAccessExpr(const AST::FieldAccessExpr *expr)
void generateReturnStmt(const AST::ReturnStmt *returnStmt)
Generate bytecode from Return Statement.
A value in the Phasor VM.
The Phasor Programming Language and Runtime.
ValueType
Runtime value types for the VM.
Assignment Expression Node.
Expression Statement Node.
Field Access Expression Node.
Function Declaration Node.
Identifier Expression Node.
Struct Instance Expression Node.
Unsafe Block Statement Node.
Variable Declaration Node.
Complete bytecode structure.
std::unordered_map< std::string, int > variables
Variable name -> index mapping.
int addStringConstant(const std::string &s)
Add a string constant with deduplication.
std::unordered_map< std::string, int > functionParamCounts
Function name -> parameter count.
std::vector< Instruction > instructions
List of instructions.
std::unordered_map< std::string, int > functionEntries
Function name -> instruction index mapping.
int nextVarIndex
Next available variable index.
std::unordered_map< std::string, int > stringConstantCache
Dedup cache for string constants.
int getOrCreateVar(const std::string &name)
Get or create a variable index.
std::vector< Value > constants
Constant pool.
int addConstant(const Value &value)
Add a constant to the pool and return its index.
std::vector< StructInfo > structs
List of struct descriptors.
void emit(OpCode op, int32_t op1=0, int32_t op2=0, int32_t op3=0)
Emit an instruction with operands.
std::unordered_map< std::string, int > structEntries
Struct name -> index in structs.
int32_t operand1
First operand.
int32_t operand2
Second operand.
Instruction(OpCode op, int32_t op1=0, int32_t op2=0, int32_t op3=0)
int32_t operand3
Third operand.
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.