9#include <unordered_map>
66 return static_cast<int>(
constants.size()) - 1;
120 int nextVarIdx = 0,
bool replMode =
false);
144 return static_cast<u8>(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.
void generateArrayLiteralExpr(const AST::ArrayLiteralExpr *arrayLit, bool resultNeeded)
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 generateArrayAccessExpr(const AST::ArrayAccessExpr *arrayAccess, bool resultNeeded)
u8 nextRegister
Next available register.
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)
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.
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 freeRegister(u8 reg)
Free a register.
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.
u8 allocateRegister()
Allocate a new register.
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.
Array Access Expression Node.
Array Literal Expression Node.
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.
void emit(OpCode op, i32 op1=0, i32 op2=0, i32 op3=0)
Emit an instruction with operands.
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.
std::unordered_map< std::string, int > structEntries
Struct name -> index in structs.
i32 operand1
First operand.
Instruction(OpCode op, i32 op1=0, i32 op2=0, i32 op3=0)
i32 operand2
Second operand.
i32 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.