topazc
codegen.hpp
Go to the documentation of this file.
1
6
7#pragma once
8#include "../parser/ast.hpp"
9#include <llvm/Support/raw_ostream.h>
10#include <llvm/IR/LLVMContext.h>
11#include <llvm/IR/BasicBlock.h>
12#include <llvm/IR/IRBuilder.h>
13#include <llvm/IR/Function.h>
14#include <llvm/IR/Module.h>
15#include <llvm/IR/Value.h>
16#include <llvm/IR/Type.h>
17#include <stack>
18#include <map>
19
24private:
25 std::string libs_path;
26 std::string file_name;
27 std::vector<AST::StmtPtr>& stmts;
28 bool is_debug;
29 llvm::LLVMContext context;
30 llvm::IRBuilder<> builder;
31 std::unique_ptr<llvm::Module> module;
32 std::stack<std::map<std::string, llvm::Value*>> variables;
33 std::map<std::string, std::vector<llvm::Function*>> functions;
34 std::stack<llvm::Type*> functions_ret_types;
35 std::stack<std::pair<llvm::BasicBlock*, llvm::BasicBlock*>> loop_blocks;
36
40 struct PathPart {
41 std::string name;
42
50 };
51 std::stack<PathPart> current_path;
52
53public:
54 CodeGenerator(std::vector<AST::StmtPtr>& s, std::string lp, std::string fn, bool id) : context(), builder(context), module(std::make_unique<llvm::Module>(fn, context)), stmts(s), libs_path(lp), file_name(fn), is_debug(id) {
55 variables.push({});
56 }
57
63 void generate();
64
70 void print_ir() {
71 module->print(llvm::outs(), nullptr);
72 }
73
81 std::unique_ptr<llvm::Module> get_module() {
82 return std::move(module);
83 }
84
85private:
93 void generate_stmt(AST::Stmt& stmt);
94
104
113
122
131
140
149
158
167
176
185
194
203
212
221
229 llvm::Value *generate_expr(AST::Expr& expr);
230
240 llvm::Value *generate_literal_expr(AST::Literal& lit);
241
251 llvm::Value *generate_binary_expr(AST::BinaryExpr& be);
252
262 llvm::Value *generate_unary_expr(AST::UnaryExpr& ue);
263
273 llvm::Value *generate_var_expr(AST::VarExpr& ve);
274
285
296
306 llvm::Type *type_to_llvm(AST::Type type);
307
318 llvm::Type *get_common_type(llvm::Type *left, llvm::Type *right);
319
330 llvm::Value *implicitly_cast(llvm::Value *val, llvm::Type *expected_type);
331
342 std::string get_mangled_name(std::string base_name);
343
353 std::vector<PathPart> get_resolved_name(std::string mangled_name);
354};
Header file for defining AST tree elements.
Binary expression container.
Definition ast.hpp:219
Statement of break.
Definition ast.hpp:401
Chain of objects expression container.
Definition ast.hpp:273
Statement of break.
Definition ast.hpp:410
Statement of do-while cycle.
Definition ast.hpp:375
Base class of expression.
Definition ast.hpp:117
Statement of extern calls.
Definition ast.hpp:444
Statement of for cycle.
Definition ast.hpp:387
Function calling expression container.
Definition ast.hpp:261
Statement of functions calling.
Definition ast.hpp:328
Statement of functions declaration.
Definition ast.hpp:313
Statement of control flow operator.
Definition ast.hpp:351
Base class of literal.
Definition ast.hpp:133
Statement of module definition.
Definition ast.hpp:419
Statement of 'return'.
Definition ast.hpp:340
Base class of statement.
Definition ast.hpp:106
Unary expression container.
Definition ast.hpp:234
Statement of import the module.
Definition ast.hpp:433
Statement of assignment of variable.
Definition ast.hpp:300
Statement of variable declaration.
Definition ast.hpp:286
Variable expression container.
Definition ast.hpp:248
Statement of while cycle.
Definition ast.hpp:363
void generate_while_cycle_stmt(AST::WhileCycleStmt &wcs)
Method for generating LLVM IR code for while cycle.
Definition codegen.cpp:290
void generate_module_stmt(AST::ModuleStmt &ms)
Method for generating LLVM IR code for module definition.
Definition codegen.cpp:380
void generate_do_while_cycle_stmt(AST::DoWhileCycleStmt &dwcs)
Method for generating LLVM IR code for do-while cycle.
Definition codegen.cpp:314
void generate_var_asgn_stmt(AST::VarAsgnStmt &vas)
Method for generating LLVM IR code for variable assignment.
Definition codegen.cpp:89
llvm::Value * generate_var_expr(AST::VarExpr &ve)
Method for generating LLVM IR code for variable expressions.
Definition codegen.cpp:665
void generate_extern_stmt(AST::ExternStmt &es)
Method for generating LLVM IR code for extern calls.
Definition codegen.cpp:479
llvm::LLVMContext context
Definition codegen.hpp:29
llvm::Type * get_common_type(llvm::Type *left, llvm::Type *right)
Method for getting comon type between two types.
Definition codegen.cpp:789
std::string get_mangled_name(std::string base_name)
Method for getting mangled name.
Definition codegen.cpp:846
llvm::Value * generate_func_call_expr(AST::FuncCallExpr &fce)
Method for generating LLVM IR code for function calling expressions.
Definition codegen.cpp:689
void generate_func_call_stmt(AST::FuncCallStmt &fcs)
Method for generating LLVM IR code for function calling.
Definition codegen.cpp:197
void generate_continue_stmt(AST::ContinueStmt &cs)
Method for generating LLVM IR code for continue statement.
Definition codegen.cpp:376
void generate_use_module_stmt(AST::UseModuleStmt &ums)
Method for generating LLVM IR code for import the module.
Definition codegen.cpp:390
void generate_break_stmt(AST::BreakStmt &bs)
Method for generating LLVM IR code for break statement.
Definition codegen.cpp:372
std::vector< PathPart > get_resolved_name(std::string mangled_name)
Method for getting resolved name by mangled name.
Definition codegen.cpp:862
void generate_var_decl_stmt(AST::VarDeclStmt &vds)
Method for generating LLVM IR code for variable definition.
Definition codegen.cpp:69
void generate_if_else_stmt(AST::IfElseStmt &ies)
Method for generating LLVM IR code for control flow operators.
Definition codegen.cpp:257
std::string file_name
Definition codegen.hpp:26
void generate_return_stmt(AST::ReturnStmt &rs)
Method for generating LLVM IR code for 'return'.
Definition codegen.cpp:244
std::string libs_path
Definition codegen.hpp:25
std::stack< std::map< std::string, llvm::Value * > > variables
Definition codegen.hpp:32
llvm::Value * generate_literal_expr(AST::Literal &lit)
Method for generating LLVM IR code for literals.
Definition codegen.cpp:521
void generate_for_cycle_stmt(AST::ForCycleStmt &fcs)
Method for generating LLVM IR code for for cycle.
Definition codegen.cpp:338
std::vector< AST::StmtPtr > & stmts
Definition codegen.hpp:27
std::map< std::string, std::vector< llvm::Function * > > functions
Definition codegen.hpp:33
CodeGenerator(std::vector< AST::StmtPtr > &s, std::string lp, std::string fn, bool id)
Definition codegen.hpp:54
llvm::Value * generate_obj_chain_expr(AST::ChainObjects &co)
Method for generating LLVM IR code for chain of objects expression.
Definition codegen.cpp:736
std::unique_ptr< llvm::Module > module
Definition codegen.hpp:31
std::stack< std::pair< llvm::BasicBlock *, llvm::BasicBlock * > > loop_blocks
Definition codegen.hpp:35
llvm::Value * generate_binary_expr(AST::BinaryExpr &be)
Method for generating LLVM IR code for binary expressions.
Definition codegen.cpp:549
llvm::Value * generate_expr(AST::Expr &expr)
Method for generating LLVM IR code for expressions.
Definition codegen.cpp:497
void generate_stmt(AST::Stmt &stmt)
Method for generating LLVM IR code for passing statement.
Definition codegen.cpp:21
llvm::Value * generate_unary_expr(AST::UnaryExpr &ue)
Method for generating LLVM IR code for unary expressions.
Definition codegen.cpp:629
llvm::Value * implicitly_cast(llvm::Value *val, llvm::Type *expected_type)
Method for implicitly cast value to expected type.
Definition codegen.cpp:813
std::stack< llvm::Type * > functions_ret_types
Definition codegen.hpp:34
llvm::Type * type_to_llvm(AST::Type type)
Method for converting AST::Type to llvm::Type.
Definition codegen.cpp:753
void generate_func_decl_stmt(AST::FuncDeclStmt &fds)
Method for generating LLVM IR code for function definition.
Definition codegen.cpp:107
std::unique_ptr< llvm::Module > get_module()
Method for getting current LLVM Module.
Definition codegen.hpp:81
std::stack< PathPart > current_path
Definition codegen.hpp:51
void print_ir()
Method for printing generated LLVM IR code.
Definition codegen.hpp:70
llvm::IRBuilder builder
Definition codegen.hpp:30
void generate()
Method for generating LLVM IR code.
Definition codegen.cpp:15
Structure for describing the type.
Definition ast.hpp:37
Structure of part of path to object.
Definition codegen.hpp:40
Object
Object from path (module or class).
Definition codegen.hpp:46
enum CodeGenerator::PathPart::Object object