63 return static_cast<int>(
constants.size()) - 1;
80 void emit(
OpCode op, int32_t op1 = 0, int32_t op2 = 0, int32_t op3 = 0)
102 int nextVarIdx = 0,
bool replMode =
false);
126 return static_cast<uint8_t
>(i);
129 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.
std::map< std::string, ValueType > inferredTypes
void generateVarDecl(const AST::VarDecl *varDecl)
Generate bytecode from Variable Declaration.
void generateIfStmt(const AST::IfStmt *ifStmt)
Generate bytecode from If Statement.
std::vector< std::vector< int > > breakJumpsStack
void generateContinueStmt()
void generateUnaryExpr(const AST::UnaryExpr *unaryExpr)
Generate bytecode from Unary Expression.
void generatePostfixExpr(const AST::PostfixExpr *expr)
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.
void generateWhileStmt(const AST::WhileStmt *whileStmt)
Generate bytecode from While Statement.
void generateSwitchStmt(const AST::SwitchStmt *switchStmt)
Bytecode generate(const AST::Program &program, const std::map< std::string, int > &existingVars={}, int nextVarIdx=0, bool replMode=false)
Generate bytecode from program.
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 freeRegister(uint8_t reg)
Free a register.
ValueType inferExpressionType(const AST::Expression *expr, bool &known)
Simple expression type inference (conservative).
void generateExpression(const AST::Expression *expr)
Generate bytecode from Expression.
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.
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::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 > functionParamCounts
Function name -> parameter count.
int getOrCreateVar(const std::string &name)
Get or create a variable index.
std::map< std::string, int > structEntries
Struct name -> index in structs.
std::map< std::string, int > variables
Variable name -> index mapping.
int addConstant(const Value &value)
Add a constant to the pool and return its index.
void emit(OpCode op, int32_t op1=0, int32_t op2=0, int32_t op3=0)
Emit an instruction with operands.
std::vector< Instruction > instructions
List of instructions.
Instruction with up to 5 operands Format: instruction operand1, operand2, operand3 Each instruction u...
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.