topazc
CodeGenerator Class Reference

Code generator class. More...

#include <codegen.hpp>

Classes

struct  PathPart
 Structure of part of path to object. More...

Public Member Functions

 CodeGenerator (std::vector< AST::StmtPtr > &s, std::string lp, std::string fn, bool id)
void generate ()
 Method for generating LLVM IR code.
void print_ir ()
 Method for printing generated LLVM IR code.
std::unique_ptr< llvm::Module > get_module ()
 Method for getting current LLVM Module.

Private Member Functions

void generate_stmt (AST::Stmt &stmt)
 Method for generating LLVM IR code for passing statement.
void generate_var_decl_stmt (AST::VarDeclStmt &vds)
 Method for generating LLVM IR code for variable definition.
void generate_var_asgn_stmt (AST::VarAsgnStmt &vas)
 Method for generating LLVM IR code for variable assignment.
void generate_func_decl_stmt (AST::FuncDeclStmt &fds)
 Method for generating LLVM IR code for function definition.
void generate_func_call_stmt (AST::FuncCallStmt &fcs)
 Method for generating LLVM IR code for function calling.
void generate_return_stmt (AST::ReturnStmt &rs)
 Method for generating LLVM IR code for 'return'.
void generate_if_else_stmt (AST::IfElseStmt &ies)
 Method for generating LLVM IR code for control flow operators.
void generate_while_cycle_stmt (AST::WhileCycleStmt &wcs)
 Method for generating LLVM IR code for while cycle.
void generate_do_while_cycle_stmt (AST::DoWhileCycleStmt &dwcs)
 Method for generating LLVM IR code for do-while cycle.
void generate_for_cycle_stmt (AST::ForCycleStmt &fcs)
 Method for generating LLVM IR code for for cycle.
void generate_break_stmt (AST::BreakStmt &bs)
 Method for generating LLVM IR code for break statement.
void generate_continue_stmt (AST::ContinueStmt &cs)
 Method for generating LLVM IR code for continue statement.
void generate_module_stmt (AST::ModuleStmt &ms)
 Method for generating LLVM IR code for module definition.
void generate_use_module_stmt (AST::UseModuleStmt &ums)
 Method for generating LLVM IR code for import the module.
void generate_extern_stmt (AST::ExternStmt &es)
 Method for generating LLVM IR code for extern calls.
llvm::Value * generate_expr (AST::Expr &expr)
 Method for generating LLVM IR code for expressions.
llvm::Value * generate_literal_expr (AST::Literal &lit)
 Method for generating LLVM IR code for literals.
llvm::Value * generate_binary_expr (AST::BinaryExpr &be)
 Method for generating LLVM IR code for binary expressions.
llvm::Value * generate_unary_expr (AST::UnaryExpr &ue)
 Method for generating LLVM IR code for unary expressions.
llvm::Value * generate_var_expr (AST::VarExpr &ve)
 Method for generating LLVM IR code for variable expressions.
llvm::Value * generate_func_call_expr (AST::FuncCallExpr &fce)
 Method for generating LLVM IR code for function calling expressions.
llvm::Value * generate_obj_chain_expr (AST::ChainObjects &co)
 Method for generating LLVM IR code for chain of objects expression.
llvm::Type * type_to_llvm (AST::Type type)
 Method for converting AST::Type to llvm::Type.
llvm::Type * get_common_type (llvm::Type *left, llvm::Type *right)
 Method for getting comon type between two types.
llvm::Value * implicitly_cast (llvm::Value *val, llvm::Type *expected_type)
 Method for implicitly cast value to expected type.
std::string get_mangled_name (std::string base_name)
 Method for getting mangled name.
std::vector< PathPartget_resolved_name (std::string mangled_name)
 Method for getting resolved name by mangled name.

Private Attributes

std::string libs_path
std::string file_name
std::vector< AST::StmtPtr > & stmts
bool is_debug
llvm::LLVMContext context
llvm::IRBuilder builder
std::unique_ptr< llvm::Module > module
std::stack< std::map< std::string, llvm::Value * > > variables
std::map< std::string, std::vector< llvm::Function * > > functions
std::stack< llvm::Type * > functions_ret_types
std::stack< std::pair< llvm::BasicBlock *, llvm::BasicBlock * > > loop_blocks
std::stack< PathPartcurrent_path

Detailed Description

Code generator class.

Definition at line 23 of file codegen.hpp.

Constructor & Destructor Documentation

◆ CodeGenerator()

CodeGenerator::CodeGenerator ( std::vector< AST::StmtPtr > & s,
std::string lp,
std::string fn,
bool id )
inline

Definition at line 54 of file codegen.hpp.

Member Function Documentation

◆ generate()

void CodeGenerator::generate ( )

Method for generating LLVM IR code.

This method generating LLVM IR code for all AST statements

Definition at line 15 of file codegen.cpp.

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

◆ generate_binary_expr()

llvm::Value * CodeGenerator::generate_binary_expr ( AST::BinaryExpr & be)
private

Method for generating LLVM IR code for binary expressions.

This method generating LLVM IR code for binary expressions and returns it. If type of passed binary operator is unsupported by current version of compiler, then throwing exception

Parameters
beBinary expression for generating
Returns
Generated LLVM value

Definition at line 549 of file codegen.cpp.

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

◆ generate_break_stmt()

void CodeGenerator::generate_break_stmt ( AST::BreakStmt & bs)
private

Method for generating LLVM IR code for break statement.

This method generating LLVM IR code for break statement

Parameters
bsBreak statement

Definition at line 372 of file codegen.cpp.

Here is the caller graph for this function:

◆ generate_continue_stmt()

void CodeGenerator::generate_continue_stmt ( AST::ContinueStmt & cs)
private

Method for generating LLVM IR code for continue statement.

This method generating LLVM IR code for continue statement

Parameters
csContinue statement

Definition at line 376 of file codegen.cpp.

Here is the caller graph for this function:

◆ generate_do_while_cycle_stmt()

void CodeGenerator::generate_do_while_cycle_stmt ( AST::DoWhileCycleStmt & dwcs)
private

Method for generating LLVM IR code for do-while cycle.

This method generating LLVM IR code for do-while cycle

Parameters
dwcsDo-while cycle

Definition at line 314 of file codegen.cpp.

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

◆ generate_expr()

llvm::Value * CodeGenerator::generate_expr ( AST::Expr & expr)
private

Method for generating LLVM IR code for expressions.

This method generating LLVM IR cide for passing expression. If passed expression is unsupported by current version of compiler, then throwing exception

Parameters
exprExpression for code generating

Definition at line 497 of file codegen.cpp.

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

◆ generate_extern_stmt()

void CodeGenerator::generate_extern_stmt ( AST::ExternStmt & es)
private

Method for generating LLVM IR code for extern calls.

This method generating LLVM IR code for extern calls

Parameters
esExtern calls

Definition at line 479 of file codegen.cpp.

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

◆ generate_for_cycle_stmt()

void CodeGenerator::generate_for_cycle_stmt ( AST::ForCycleStmt & fcs)
private

Method for generating LLVM IR code for for cycle.

This method generating LLVM IR code for for cycle

Parameters
fcsFor cycle

Definition at line 338 of file codegen.cpp.

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

◆ generate_func_call_expr()

llvm::Value * CodeGenerator::generate_func_call_expr ( AST::FuncCallExpr & fce)
private

Method for generating LLVM IR code for function calling expressions.

This method generating LLVM IR code for function calling expressions and returns it

Parameters
fceFunction calling expression for generating
Returns
Generated LLVM value

Definition at line 689 of file codegen.cpp.

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

◆ generate_func_call_stmt()

void CodeGenerator::generate_func_call_stmt ( AST::FuncCallStmt & fcs)
private

Method for generating LLVM IR code for function calling.

This method generating LLVM IR code for function calling

Parameters
fdsFunction calling statement

Definition at line 197 of file codegen.cpp.

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

◆ generate_func_decl_stmt()

void CodeGenerator::generate_func_decl_stmt ( AST::FuncDeclStmt & fds)
private

Method for generating LLVM IR code for function definition.

This method generating LLVM IR code for function definition

Parameters
fdsFunction declaration statement

Definition at line 107 of file codegen.cpp.

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

◆ generate_if_else_stmt()

void CodeGenerator::generate_if_else_stmt ( AST::IfElseStmt & ies)
private

Method for generating LLVM IR code for control flow operators.

This method generating LLVM IR code for control flow operators

Parameters
iesControl flow operator

Definition at line 257 of file codegen.cpp.

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

◆ generate_literal_expr()

llvm::Value * CodeGenerator::generate_literal_expr ( AST::Literal & lit)
private

Method for generating LLVM IR code for literals.

This method generating LLVM IR code for literals and returns it. If type of passed literal is TYPE_STRING_LIT, then generating global string variable with value as literal value

Parameters
litLiteral for generating
Returns
Generated LLVM value

Definition at line 521 of file codegen.cpp.

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

◆ generate_module_stmt()

void CodeGenerator::generate_module_stmt ( AST::ModuleStmt & ms)
private

Method for generating LLVM IR code for module definition.

This method generating LLVM IR code for module definition

Parameters
msModule definition statement

Definition at line 380 of file codegen.cpp.

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

◆ generate_obj_chain_expr()

llvm::Value * CodeGenerator::generate_obj_chain_expr ( AST::ChainObjects & co)
private

Method for generating LLVM IR code for chain of objects expression.

This method generating LLVM IR code for all expressions in path from chain

Parameters
coChain of objects expression for generating
Returns
Generated LLVM value

Definition at line 736 of file codegen.cpp.

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

◆ generate_return_stmt()

void CodeGenerator::generate_return_stmt ( AST::ReturnStmt & rs)
private

Method for generating LLVM IR code for 'return'.

This method generating LLVM IR code for 'return'

Parameters
rsReturn statement

Definition at line 244 of file codegen.cpp.

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

◆ generate_stmt()

void CodeGenerator::generate_stmt ( AST::Stmt & stmt)
private

Method for generating LLVM IR code for passing statement.

This method generating LLVM IR code for passing statement. If passed statement is unsupported by current version of compiler, then throwing exception

Parameters
stmtStatement for code generating

Definition at line 21 of file codegen.cpp.

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

◆ generate_unary_expr()

llvm::Value * CodeGenerator::generate_unary_expr ( AST::UnaryExpr & ue)
private

Method for generating LLVM IR code for unary expressions.

This method generating LLVM IR code for unary expressions and returns it. If type of passed unary operator is unsupported by current version of compiler, then throwing exception

Parameters
beUnary expression for generating
Returns
Generated LLVM value

Definition at line 629 of file codegen.cpp.

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

◆ generate_use_module_stmt()

void CodeGenerator::generate_use_module_stmt ( AST::UseModuleStmt & ums)
private

Method for generating LLVM IR code for import the module.

This method generating LLVM IR code for import the module

Parameters
umsImporting module

Definition at line 390 of file codegen.cpp.

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

◆ generate_var_asgn_stmt()

void CodeGenerator::generate_var_asgn_stmt ( AST::VarAsgnStmt & vas)
private

Method for generating LLVM IR code for variable assignment.

This method generating LLVM IR code for variable assignment

Parameters
vdsVariable assignment statement

Definition at line 89 of file codegen.cpp.

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

◆ generate_var_decl_stmt()

void CodeGenerator::generate_var_decl_stmt ( AST::VarDeclStmt & vds)
private

Method for generating LLVM IR code for variable definition.

This method generating LLVM IR code for variable definition. If variable definition in the global view scope, then generating global variable. Otherwise generating local variable

Parameters
vdsVariable declaration statement

Definition at line 69 of file codegen.cpp.

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

◆ generate_var_expr()

llvm::Value * CodeGenerator::generate_var_expr ( AST::VarExpr & ve)
private

Method for generating LLVM IR code for variable expressions.

This method generating LLVM IR code for variable expressions and returns it

Parameters
veVariable expression for generating
Returns
Generated LLVM value

Definition at line 665 of file codegen.cpp.

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

◆ generate_while_cycle_stmt()

void CodeGenerator::generate_while_cycle_stmt ( AST::WhileCycleStmt & wcs)
private

Method for generating LLVM IR code for while cycle.

This method generating LLVM IR code for while cycle

Parameters
wcsWhile cycle

Definition at line 290 of file codegen.cpp.

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

◆ get_common_type()

llvm::Type * CodeGenerator::get_common_type ( llvm::Type * left,
llvm::Type * right )
private

Method for getting comon type between two types.

This method getting common type between two passed types and returns it. If common type does not exist, then throwing exception

Parameters
leftType to be implicitly cast
rightType to be implicitly cast to
Returns
Common type between two passed types

Definition at line 789 of file codegen.cpp.

Here is the caller graph for this function:

◆ get_mangled_name()

std::string CodeGenerator::get_mangled_name ( std::string base_name)
private

Method for getting mangled name.

This method returns mangled name based passed object name and CodeGenerator::current_path. Names of parts of path separated '-' (if current part of path is module) or '#' (if current part of path is class)

Parameters
base_nameBased name for mangling
Returns
Mangled name

Definition at line 846 of file codegen.cpp.

Here is the caller graph for this function:

◆ get_module()

std::unique_ptr< llvm::Module > CodeGenerator::get_module ( )
inline

Method for getting current LLVM Module.

This method returning current LLVM Module. This method needs the entry point 'main'

Returns
Current LLVM Module

Definition at line 81 of file codegen.hpp.

Here is the caller graph for this function:

◆ get_resolved_name()

std::vector< CodeGenerator::PathPart > CodeGenerator::get_resolved_name ( std::string mangled_name)
private

Method for getting resolved name by mangled name.

This method returns resolved name by passed mangled name

Parameters
mangled_nameMangled name for resolving
Returns
Vector to PathPart

Definition at line 862 of file codegen.cpp.

Here is the caller graph for this function:

◆ implicitly_cast()

llvm::Value * CodeGenerator::implicitly_cast ( llvm::Value * val,
llvm::Type * expected_type )
private

Method for implicitly cast value to expected type.

This method implicitly cast passed value to passed expected type and returns casted value. If cast is imposible, then returns nullptr

Parameters
valValue for casting
expected_typeType to be converted to
Returns
Casted value

Definition at line 813 of file codegen.cpp.

Here is the caller graph for this function:

◆ print_ir()

void CodeGenerator::print_ir ( )
inline

Method for printing generated LLVM IR code.

This method printing generated LLVM IR code into llvm::outs(). This method needs the entry point 'main'

Definition at line 70 of file codegen.hpp.

Here is the caller graph for this function:

◆ type_to_llvm()

llvm::Type * CodeGenerator::type_to_llvm ( AST::Type type)
private

Method for converting AST::Type to llvm::Type.

This method converting passed AST::Type to llvm::Type and returns it. If type of passed AST::Type is unsupported by current version of compiler, then throwing exception

Parameters
typeAST::Type for converting
Returns
Converted type to llvm::Type

Definition at line 753 of file codegen.cpp.

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

Member Data Documentation

◆ builder

llvm::IRBuilder CodeGenerator::builder
private

LLVM IR Builder

Definition at line 30 of file codegen.hpp.

◆ context

llvm::LLVMContext CodeGenerator::context
private

LLVM Context

Definition at line 29 of file codegen.hpp.

◆ current_path

std::stack<PathPart> CodeGenerator::current_path
private

Stack to current path to some object

Definition at line 51 of file codegen.hpp.

◆ file_name

std::string CodeGenerator::file_name
private

Absolute path to the Topaz source code

Definition at line 26 of file codegen.hpp.

◆ functions

std::map<std::string, std::vector<llvm::Function*> > CodeGenerator::functions
private

Functions table

Definition at line 33 of file codegen.hpp.

◆ functions_ret_types

std::stack<llvm::Type*> CodeGenerator::functions_ret_types
private

Stack of functions return types

Definition at line 34 of file codegen.hpp.

◆ is_debug

bool CodeGenerator::is_debug
private

Flag for debug exception

Definition at line 28 of file codegen.hpp.

◆ libs_path

std::string CodeGenerator::libs_path
private

Absolute path to the Topaz libraries

Definition at line 25 of file codegen.hpp.

◆ loop_blocks

std::stack<std::pair<llvm::BasicBlock*, llvm::BasicBlock*> > CodeGenerator::loop_blocks
private

Stack of branches into cycles. First for 'break', second for 'continue'

Definition at line 35 of file codegen.hpp.

◆ module

std::unique_ptr<llvm::Module> CodeGenerator::module
private

LLVM Module (module name is relative path to the Topaz source code)

Definition at line 31 of file codegen.hpp.

◆ stmts

std::vector<AST::StmtPtr>& CodeGenerator::stmts
private

AST Tree (statements from Parser)

Definition at line 27 of file codegen.hpp.

◆ variables

std::stack<std::map<std::string, llvm::Value*> > CodeGenerator::variables
private

View scope of the variables table

Definition at line 32 of file codegen.hpp.


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