1 #ifndef __TAN_SRC_CODEGEN_CODE_GENERATOR_H__
2 #define __TAN_SRC_CODEGEN_CODE_GENERATOR_H__
5 #include "common/compiler_action.h"
7 #include <llvm/Target/TargetMachine.h>
8 #include <llvm/IR/IRBuilder.h>
9 #include <llvm/IR/Value.h>
10 #include <llvm/IR/DIBuilder.h>
11 #include <llvm/IR/Instructions.h>
12 #include <llvm/IR/BasicBlock.h>
13 using llvm::AllocaInst;
14 using llvm::BasicBlock;
15 using llvm::DIBuilder;
16 using llvm::IRBuilder;
17 using llvm::TargetMachine;
32 void init(
Package *package)
override;
33 void run_impl(
Package *package);
34 llvm::Value *cached_visit(
ASTBase *p);
35 void default_visit(
ASTBase *)
override;
37 void emit_to_file(
const str &filename);
42 [[noreturn]]
void error(ErrorType type,
ASTBase *p,
const str &message);
53 llvm::Value *convert_llvm_type_to(
Expr *expr,
Type *dest);
56 llvm::Value *load_if_is_lvalue(
Expr *expr);
58 llvm::Type *to_llvm_type(
Type *p);
59 llvm::Metadata *to_llvm_metadata(
Type *p, llvm::DIFile *di_file, uint32_t lineno);
60 llvm::DISubroutineType *create_function_debug_info_type(llvm::Metadata *ret, vector<llvm::Metadata *> args);
63 umap<Type *, llvm::Type *> _llvm_type_cache{};
64 umap<Type *, llvm::Metadata *> _llvm_meta_cache{};
65 umap<ASTBase *, llvm::Value *> _llvm_value_cache{};
68 umap<TokenizedSourceFile *, llvm::DIFile *> _di_files{};
71 llvm::IRBuilder<> *_builder =
nullptr;
72 llvm::DIBuilder *_di_builder =
nullptr;
73 llvm::LLVMContext *_llvm_ctx =
nullptr;
74 llvm::Module *_module =
nullptr;
75 vector<llvm::DIScope *> _di_scope{};
76 llvm::TargetMachine *_target_machine =
nullptr;
79 llvm::DIScope *get_current_di_scope()
const;
80 void push_di_scope(llvm::DIScope *scope);
82 void set_current_debug_location(
ASTBase *p);
83 llvm::DebugLoc debug_loc_of_node(
ASTBase *p, llvm::MDNode *scope =
nullptr);
92 AllocaInst *create_block_alloca(BasicBlock *block, llvm::Type *type,
size_t size = 1,
const str &name =
"");
95 Value *codegen_var_arg_decl(
Decl *p);
96 Value *codegen_struct_default_value(
StructType *ty);
97 Value *codegen_type_default_value(
Type *p);
98 Value *codegen_literals(
Literal *p);
99 Value *codegen_func_prototype(
FunctionDecl *p,
bool import_ =
false);
112 llvm::DIFile *get_or_create_di_file(
ASTBase *p);
115 DECLARE_AST_VISITOR_IMPL(
Package);
118 DECLARE_AST_VISITOR_IMPL(
If);
119 DECLARE_AST_VISITOR_IMPL(
VarDecl);
120 DECLARE_AST_VISITOR_IMPL(
ArgDecl);
121 DECLARE_AST_VISITOR_IMPL(
Return);
126 DECLARE_AST_VISITOR_IMPL(
Cast);
130 DECLARE_AST_VISITOR_IMPL(
Import);
141 DECLARE_AST_VISITOR_IMPL(
Loop);
143 DECLARE_AST_VISITOR_IMPL(
VarRef);
Represent if-[else] or if-elif-[else] statements.
A generic representation of Intrinsic variables/functions.
Type is immutable once created. The exception is StructType. Its information is updated in multiple s...