-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcompilerengine.h
130 lines (113 loc) · 5.37 KB
/
compilerengine.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#ifndef COMPILERENGINE_H
#define COMPILERENGINE_H
/*
用于实时编译需要执行的代码段,生成ll代码
*/
#include <QtCore>
namespace llvm {
class LLVMContext;
class Module;
};
namespace clang {
class Decl;
class Stmt;
class Expr;
class NamedDecl;
class FunctionDecl;
class CXXRecordDecl;
class CXXMethodDecl;
class CXXConstructorDecl;
class CXXDestructorDecl;
class ClassTemplateDecl;
class ASTContext;
class CompilerInstance;
class CompilerInvocation;
class ASTUnit;
namespace driver {
class Driver;
};
namespace CodeGen {
class CodeGenModule;
class CodeGenFunction;
};
};
class CompilerUnit;
class CompilerEngine
{
public:
CompilerEngine();
~CompilerEngine();
public:
llvm::Module* conv_ctor(clang::ASTContext &ctx, clang::CXXConstructorDecl *ctor);
llvm::Module* conv_method(clang::ASTContext &ctx, clang::CXXMethodDecl *mth);
// bool check_inline_symbol();
QString mangle_ctor(clang::ASTContext &ctx, clang::CXXConstructorDecl *ctor);
QString mangle_dtor(clang::ASTContext &ctx, clang::CXXDestructorDecl *dtor);
QString mangle_method(clang::ASTContext &ctx, clang::CXXMethodDecl *ctor);
QString mangle_symbol(clang::ASTContext &ctx, clang::FunctionDecl *ctor);
// 第一种方式,拿到还未定义的symbol,到ast中查找
// 第二种方式,解析C++方法的源代码,找到这个未定义的symbol,从decl再把它编译成ll,效率更好。
void decl2def(llvm::Module *mod, clang::ASTContext &ctx,
clang::CodeGen::CodeGenModule &cgmod,
clang::Decl *decl, int level, QHash<QString, bool> noinlined);
clang::FunctionDecl* find_callee_decl_by_symbol2(clang::Decl *bdecl, QString callee_symbol);
clang::FunctionDecl* find_callee_decl_by_symbol(clang::Decl *bdecl, QString callee_symbol);
clang::FunctionDecl* find_callee_decl_by_symbol(clang::Decl *bdecl, QString callee_symbol,
clang::Stmt *bstmt);
CompilerUnit *createCompilerUnit(clang::ASTUnit *unit, clang::NamedDecl *decl);
bool destroyCompilerUnit(CompilerUnit *cu);
llvm::Module* conv_ctor2(clang::ASTUnit *unit, clang::CXXConstructorDecl *ctor, QVector<QVariant> dargs);
llvm::Module* conv_dtor(clang::ASTUnit *unit, clang::CXXDestructorDecl *dtor);
llvm::Module* conv_method2(clang::ASTUnit *unit, clang::CXXMethodDecl *mth);
llvm::Module* conv_function2(clang::ASTUnit *unit, clang::FunctionDecl *mth);
llvm::Module* conv_dargs(clang::ASTUnit *unit, clang::FunctionDecl *fd, QVector<QVariant> &dargs);
bool gen_ctor(CompilerUnit *cu, clang::CXXConstructorDecl *yactor = NULL);
bool gen_dtor(CompilerUnit *cu, clang::CXXDestructorDecl *yadtor = NULL);
bool gen_method(CompilerUnit *cu, clang::CXXMethodDecl *yamth = NULL);
bool gen_method_decl(CompilerUnit *cu, clang::CXXMethodDecl *yamth = NULL);
bool gen_free_function(CompilerUnit *cu, clang::FunctionDecl *yafun = NULL);
bool gen_undefs(CompilerUnit *cu, clang::FunctionDecl *yafun = NULL, clang::Stmt *yastmt = NULL);
bool gen_undefs2(CompilerUnit *cu, clang::FunctionDecl *yafun = NULL, clang::Stmt *yastmt = NULL);
bool gen_darg(llvm::Module *mod, QVariant &darg, int idx, clang::FunctionDecl *fd);
clang::CXXMethodDecl *get_decl_with_body(clang::CXXMethodDecl *decl);
bool find_undef_symbols(CompilerUnit *cu);
bool is_in_type_module(QString symbol);
bool instantiate_method(CompilerUnit *cu, clang::CXXMethodDecl *tmpl_mthdecl);
public: // test use
bool initCompiler();
bool tryCompile(clang::CXXRecordDecl *decl, clang::ASTContext &ctx, clang::ASTUnit *unit);
bool tryCompile2(clang::CXXRecordDecl *decl, clang::ASTContext &ctx);
bool tryCompile3(clang::CXXRecordDecl *decl, clang::ASTContext &ctx, clang::ASTUnit *unit);
bool tryCompile4(clang::CXXRecordDecl *decl, clang::ASTContext &ctx, clang::ASTUnit *unit);
bool tryCompile_tpl(clang::ClassTemplateDecl *decl, clang::ASTContext &ctx, clang::ASTUnit *unit);
bool tryTransform(clang::ClassTemplateDecl *decl, clang::ASTContext &ctx, clang::ASTUnit *unit,
clang::CXXMethodDecl *mth, clang::CXXMethodDecl *mth2);
bool tryTransform2(clang::ClassTemplateDecl *decl, clang::ASTContext &ctx, clang::ASTUnit *unit,
clang::CXXMethodDecl *mth, clang::CXXMethodDecl *mth2);
bool tryTransform3(clang::ClassTemplateDecl *decl, clang::ASTContext &ctx, clang::ASTUnit *unit,
clang::CXXMethodDecl *mth, clang::CXXMethodDecl *mth2);
public:
clang::CompilerInstance *mcis = NULL;
clang::CompilerInvocation *mciv = NULL;
clang::driver::Driver *mdrv = NULL;
// llvm::Module *mtmod = NULL;
QHash<llvm::Module*, CompilerUnit*> mcus;
// 动态构造AST语法树所用的ASTUnit/TranslateDecl
};
class CompilerUnit
{
public:
clang::CompilerInstance *mcis = NULL;
clang::ASTUnit *munit = NULL;
llvm::LLVMContext *mvmctx = NULL;
llvm::Module *mmod = NULL;
clang::CodeGen::CodeGenModule *mcgm = NULL;
clang::CodeGen::CodeGenFunction *mcgf = NULL;
clang::NamedDecl *mdecl = NULL;
clang::NamedDecl *mbdecl = NULL; // bodyed
QVector<QString> mUndefSymbols;
QHash<QString, bool> mNoinlineSymbols;
QVector<QVariant> mdargs;
llvm::Module *mremod = NULL;
};
#endif /* COMPILERENGINE_H */