Phasor 2.2.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
Phasor::CodeGenerator Class Reference

Code generator for Phasor VM. More...

#include <CodeGen.hpp>

Collaboration diagram for Phasor::CodeGenerator:
[legend]

Public Member Functions

Bytecode generate (const AST::Program &program, const std::map< std::string, int > &existingVars={}, int nextVarIdx=0, bool replMode=false)
 Generate bytecode from program.

Private Member Functions

uint8_t allocateRegister ()
 Allocate a new register.
void freeRegister (uint8_t reg)
 Free a register.
void resetRegisters ()
 Reset register allocator.
bool isLiteralExpression (const AST::Expression *expr, Value &outValue)
 Check if expression is a compile-time literal.
ValueType inferExpressionType (const AST::Expression *expr, bool &known)
 Simple expression type inference (conservative).
void generateStatement (const AST::Statement *stmt)
 Generate bytecode from Statement.
void generateExpression (const AST::Expression *expr)
 Generate bytecode from Expression.
void generateVarDecl (const AST::VarDecl *varDecl)
 Generate bytecode from Variable Declaration.
void generateExpressionStmt (const AST::ExpressionStmt *exprStmt)
 Generate bytecode from Expression Statement.
void generatePrintStmt (const AST::PrintStmt *printStmt)
 Generate bytecode from Print Statement.
void generateImportStmt (const AST::ImportStmt *importStmt)
 Generate bytecode from Import Statement.
void generateExportStmt (const AST::ExportStmt *exportStmt)
 Generate bytecode from Export Statement.
void generateNumberExpr (const AST::NumberExpr *numExpr)
 Generate bytecode from Numeral Expression.
void generateStringExpr (const AST::StringExpr *strExpr)
 Generate bytecode from String Expression.
void generateIdentifierExpr (const AST::IdentifierExpr *identExpr)
 Generate bytecode from Identifier Expression.
void generateUnaryExpr (const AST::UnaryExpr *unaryExpr)
 Generate bytecode from Unary Expression.
void generateCallExpr (const AST::CallExpr *callExpr)
 Generate bytecode from Call Expression.
void generateBinaryExpr (const AST::BinaryExpr *binExpr)
 Generate bytecode from Binary Expression.
void generateBlockStmt (const AST::BlockStmt *blockStmt)
 Generate bytecode from Block Statement.
void generateIfStmt (const AST::IfStmt *ifStmt)
 Generate bytecode from If Statement.
void generateWhileStmt (const AST::WhileStmt *whileStmt)
 Generate bytecode from While Statement.
void generateForStmt (const AST::ForStmt *forStmt)
 Generate bytecode from For Statement.
void generateReturnStmt (const AST::ReturnStmt *returnStmt)
 Generate bytecode from Return Statement.
void generateUnsafeBlockStmt (const AST::UnsafeBlockStmt *unsafeStmt)
 Generate bytecode from Unsafe Block Statement.
void generateFunctionDecl (const AST::FunctionDecl *funcDecl)
 Generate bytecode from Function Declaration.
void generateBooleanExpr (const AST::BooleanExpr *boolExpr)
 Generate bytecode from Boolean Expression.
void generateNullExpr (const AST::NullExpr *nullExpr)
 Generate bytecode from Null Expression.
void generateAssignmentExpr (const AST::AssignmentExpr *assignExpr)
 Generate bytecode from Assignment Expression.
void generateStructDecl (const AST::StructDecl *decl)
void generateStructInstanceExpr (const AST::StructInstanceExpr *expr)
void generateFieldAccessExpr (const AST::FieldAccessExpr *expr)
void generatePostfixExpr (const AST::PostfixExpr *expr)
void generateBreakStmt ()
void generateContinueStmt ()
void generateSwitchStmt (const AST::SwitchStmt *switchStmt)

Private Attributes

Bytecode bytecode
 Generated bytecode.
bool isRepl = false
 REPL mode.
std::map< std::string, ValueTypeinferredTypes
uint8_t nextRegister = 0
 Next available register.
std::vector< bool > registerInUse
 Track which registers are in use.
std::vector< int > loopStartStack
std::vector< std::vector< int > > breakJumpsStack
std::vector< std::vector< int > > continueJumpsStack

Detailed Description

Code generator for Phasor VM.

Definition at line 242 of file CodeGen.hpp.

Member Function Documentation

◆ allocateRegister()

uint8_t Phasor::CodeGenerator::allocateRegister ( )
inlineprivate

Allocate a new register.

Definition at line 268 of file CodeGen.hpp.

Here is the caller graph for this function:

◆ freeRegister()

void Phasor::CodeGenerator::freeRegister ( uint8_t reg)
inlineprivate

Free a register.

Definition at line 286 of file CodeGen.hpp.

Here is the caller graph for this function:

◆ generate()

Bytecode Phasor::CodeGenerator::generate ( const AST::Program & program,
const std::map< std::string, int > & existingVars = {},
int nextVarIdx = 0,
bool replMode = false )

Generate bytecode from program.

Parameters
programProgram to generate bytecode for
existingVarsExisting variables to use
nextVarIdxNext available variable index
replModeREPL mode
Returns
Bytecode Generated bytecode

Definition at line 7 of file CodeGen.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generateAssignmentExpr()

void Phasor::CodeGenerator::generateAssignmentExpr ( const AST::AssignmentExpr * assignExpr)
private

Generate bytecode from Assignment Expression.

Definition at line 903 of file CodeGen.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generateBinaryExpr()

void Phasor::CodeGenerator::generateBinaryExpr ( const AST::BinaryExpr * binExpr)
private

Generate bytecode from Binary Expression.

Definition at line 429 of file CodeGen.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generateBlockStmt()

void Phasor::CodeGenerator::generateBlockStmt ( const AST::BlockStmt * blockStmt)
private

Generate bytecode from Block Statement.

Definition at line 681 of file CodeGen.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generateBooleanExpr()

void Phasor::CodeGenerator::generateBooleanExpr ( const AST::BooleanExpr * boolExpr)
private

Generate bytecode from Boolean Expression.

Definition at line 886 of file CodeGen.cpp.

Here is the caller graph for this function:

◆ generateBreakStmt()

void Phasor::CodeGenerator::generateBreakStmt ( )
private

Definition at line 813 of file CodeGen.cpp.

Here is the caller graph for this function:

◆ generateCallExpr()

void Phasor::CodeGenerator::generateCallExpr ( const AST::CallExpr * callExpr)
private

Generate bytecode from Call Expression.

Definition at line 335 of file CodeGen.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generateContinueStmt()

void Phasor::CodeGenerator::generateContinueStmt ( )
private

Definition at line 825 of file CodeGen.cpp.

Here is the caller graph for this function:

◆ generateExportStmt()

void Phasor::CodeGenerator::generateExportStmt ( const AST::ExportStmt * exportStmt)
private

Generate bytecode from Export Statement.

Definition at line 268 of file CodeGen.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generateExpression()

void Phasor::CodeGenerator::generateExpression ( const AST::Expression * expr)
private

Generate bytecode from Expression.

Definition at line 157 of file CodeGen.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generateExpressionStmt()

void Phasor::CodeGenerator::generateExpressionStmt ( const AST::ExpressionStmt * exprStmt)
private

Generate bytecode from Expression Statement.

Definition at line 243 of file CodeGen.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generateFieldAccessExpr()

void Phasor::CodeGenerator::generateFieldAccessExpr ( const AST::FieldAccessExpr * expr)
private

Definition at line 986 of file CodeGen.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generateForStmt()

void Phasor::CodeGenerator::generateForStmt ( const AST::ForStmt * forStmt)
private

Generate bytecode from For Statement.

Definition at line 752 of file CodeGen.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generateFunctionDecl()

void Phasor::CodeGenerator::generateFunctionDecl ( const AST::FunctionDecl * funcDecl)
private

Generate bytecode from Function Declaration.

Definition at line 855 of file CodeGen.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generateIdentifierExpr()

void Phasor::CodeGenerator::generateIdentifierExpr ( const AST::IdentifierExpr * identExpr)
private

Generate bytecode from Identifier Expression.

Definition at line 305 of file CodeGen.cpp.

Here is the caller graph for this function:

◆ generateIfStmt()

void Phasor::CodeGenerator::generateIfStmt ( const AST::IfStmt * ifStmt)
private

Generate bytecode from If Statement.

Definition at line 689 of file CodeGen.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generateImportStmt()

void Phasor::CodeGenerator::generateImportStmt ( const AST::ImportStmt * importStmt)
private

Generate bytecode from Import Statement.

Definition at line 262 of file CodeGen.cpp.

Here is the caller graph for this function:

◆ generateNullExpr()

void Phasor::CodeGenerator::generateNullExpr ( const AST::NullExpr * nullExpr)
private

Generate bytecode from Null Expression.

Definition at line 898 of file CodeGen.cpp.

Here is the caller graph for this function:

◆ generateNumberExpr()

void Phasor::CodeGenerator::generateNumberExpr ( const AST::NumberExpr * numExpr)
private

Generate bytecode from Numeral Expression.

Definition at line 274 of file CodeGen.cpp.

Here is the caller graph for this function:

◆ generatePostfixExpr()

void Phasor::CodeGenerator::generatePostfixExpr ( const AST::PostfixExpr * expr)
private

Definition at line 998 of file CodeGen.cpp.

Here is the caller graph for this function:

◆ generatePrintStmt()

void Phasor::CodeGenerator::generatePrintStmt ( const AST::PrintStmt * printStmt)
private

Generate bytecode from Print Statement.

Definition at line 256 of file CodeGen.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generateReturnStmt()

void Phasor::CodeGenerator::generateReturnStmt ( const AST::ReturnStmt * returnStmt)
private

Generate bytecode from Return Statement.

Definition at line 837 of file CodeGen.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generateStatement()

void Phasor::CodeGenerator::generateStatement ( const AST::Statement * stmt)
private

Generate bytecode from Statement.

Definition at line 88 of file CodeGen.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generateStringExpr()

void Phasor::CodeGenerator::generateStringExpr ( const AST::StringExpr * strExpr)
private

Generate bytecode from String Expression.

Definition at line 299 of file CodeGen.cpp.

Here is the caller graph for this function:

◆ generateStructDecl()

void Phasor::CodeGenerator::generateStructDecl ( const AST::StructDecl * decl)
private

Definition at line 1038 of file CodeGen.cpp.

Here is the caller graph for this function:

◆ generateStructInstanceExpr()

void Phasor::CodeGenerator::generateStructInstanceExpr ( const AST::StructInstanceExpr * expr)
private

Definition at line 953 of file CodeGen.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generateSwitchStmt()

void Phasor::CodeGenerator::generateSwitchStmt ( const AST::SwitchStmt * switchStmt)
private

Definition at line 1080 of file CodeGen.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generateUnaryExpr()

void Phasor::CodeGenerator::generateUnaryExpr ( const AST::UnaryExpr * unaryExpr)
private

Generate bytecode from Unary Expression.

Definition at line 311 of file CodeGen.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generateUnsafeBlockStmt()

void Phasor::CodeGenerator::generateUnsafeBlockStmt ( const AST::UnsafeBlockStmt * unsafeStmt)
private

Generate bytecode from Unsafe Block Statement.

Definition at line 850 of file CodeGen.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generateVarDecl()

void Phasor::CodeGenerator::generateVarDecl ( const AST::VarDecl * varDecl)
private

Generate bytecode from Variable Declaration.

Definition at line 208 of file CodeGen.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generateWhileStmt()

void Phasor::CodeGenerator::generateWhileStmt ( const AST::WhileStmt * whileStmt)
private

Generate bytecode from While Statement.

Definition at line 714 of file CodeGen.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ inferExpressionType()

ValueType Phasor::CodeGenerator::inferExpressionType ( const AST::Expression * expr,
bool & known )
private

Simple expression type inference (conservative).

Parameters
exprexpression to inspect
knownset to true when a type is known
Returns
inferred ValueType (valid only when known == true)

Definition at line 62 of file CodeGen.cpp.

Here is the call graph for this function:

◆ isLiteralExpression()

bool Phasor::CodeGenerator::isLiteralExpression ( const AST::Expression * expr,
Value & outValue )
private

Check if expression is a compile-time literal.

Definition at line 23 of file CodeGen.cpp.

Here is the caller graph for this function:

◆ resetRegisters()

void Phasor::CodeGenerator::resetRegisters ( )
inlineprivate

Reset register allocator.

Definition at line 295 of file CodeGen.hpp.

Member Data Documentation

◆ breakJumpsStack

std::vector<std::vector<int> > Phasor::CodeGenerator::breakJumpsStack
private

Definition at line 344 of file CodeGen.hpp.

◆ bytecode

Bytecode Phasor::CodeGenerator::bytecode
private

Generated bytecode.

Definition at line 258 of file CodeGen.hpp.

◆ continueJumpsStack

std::vector<std::vector<int> > Phasor::CodeGenerator::continueJumpsStack
private

Definition at line 345 of file CodeGen.hpp.

◆ inferredTypes

std::map<std::string, ValueType> Phasor::CodeGenerator::inferredTypes
private

Definition at line 261 of file CodeGen.hpp.

◆ isRepl

bool Phasor::CodeGenerator::isRepl = false
private

REPL mode.

Definition at line 259 of file CodeGen.hpp.

◆ loopStartStack

std::vector<int> Phasor::CodeGenerator::loopStartStack
private

Definition at line 343 of file CodeGen.hpp.

◆ nextRegister

uint8_t Phasor::CodeGenerator::nextRegister = 0
private

Next available register.

Definition at line 264 of file CodeGen.hpp.

◆ registerInUse

std::vector<bool> Phasor::CodeGenerator::registerInUse
private

Track which registers are in use.

Definition at line 265 of file CodeGen.hpp.


The documentation for this class was generated from the following files: