-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclvm.cpp
535 lines (440 loc) · 16.8 KB
/
clvm.cpp
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#include "fix_clang_undef_eai.h"
#include <QtCore>
#include "llvm/IR/LLVMContext.h"
// #include "RemoteMemoryManager.h"
// #include "RemoteTarget.h"
// #include "RemoteTargetExternal.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/CodeGen/LinkAllCodegenComponents.h"
#include "llvm/ExecutionEngine/GenericValue.h"
#include "llvm/ExecutionEngine/Interpreter.h"
// #include "llvm/ExecutionEngine/JIT.h" // depcreated
#include "llvm/ExecutionEngine/JITEventListener.h"
// #include "llvm/ExecutionEngine/JITMemoryManager.h"
#include "llvm/ExecutionEngine/MCJIT.h"
#include "llvm/ExecutionEngine/ObjectCache.h"
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
#include "llvm/IR/ValueSymbolTable.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/TypeBuilder.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/Object/Archive.h"
#include "llvm/Object/ObjectFile.h"
// #include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/DynamicLibrary.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/Memory.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/PluginLoader.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Instrumentation.h"
#include <cerrno>
#include <llvm/Support/SourceMgr.h>
#include <llvm/AsmParser/Parser.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Module.h>
#include <clang/Frontend/CompilerInvocation.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Lex/PreprocessorOptions.h>
#include "llvm/Support/Host.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "clang/Basic/DiagnosticOptions.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/CompilerInvocation.h"
#include "clang/Basic/TargetOptions.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Lex/ModuleLoader.h"
#include "clang/CodeGen/ModuleBuilder.h"
#include "clang/CodeGen/CodeGenAction.h"
#include "clang/Driver/Compilation.h"
#include "clang/Driver/Driver.h"
#include "clang/Driver/Tool.h"
////////
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/CodeGen/CommandFlags.h"
#include "llvm/LTO/LTOCodeGenerator.h"
#include "llvm/LTO/LTOModule.h"
#include "llvm/Target/TargetOptions.h"
#include "frontengine.h"
#include "clvm_operator.h"
#include "clvm.h"
#include "ivm/dbghelper.h"
#include "ivm/clvmjitlistener.h"
// FIXME: MCTargetOptionsCommandFlags.h中几个函数的multiple definition问题
// #include "clvm_letacy.cpp"
llvm::GenericValue jit_execute_func(llvm::Module *mod, llvm::Function *func)
{
llvm::GenericValue gv;
llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmPrinter();
llvm::InitializeNativeTargetAsmParser();
llvm::ExecutionEngine *EE = NULL;
qDebug()<<"222222222";
std::string Error;
std::unique_ptr<llvm::Module> tmod(mod);
llvm::EngineBuilder builder(std::move(tmod));
qDebug()<<EE<<"aaa is sym search disabled:";
// builder.setUseMCJIT(true);
// builder.setMArch("core2"); // 这种强制设置可能引起问题,使用三个InitailzeNativexxx函数初始化。
EE = builder.create();
qDebug()<<EE<<"is sym search disabled:"<<EE->isSymbolSearchingDisabled();
//* 这些行还是有用的,否则在用Qt相关类时崩溃(在普通的print无问题)
// 不过这些调用应用是每个函数都调用还是初始化调用一次呢。
EE->finalizeObject();
EE->runStaticConstructorsDestructors(false);
int miret = -1;
llvm::GenericValue rgv;
std::vector<std::string> argv;
std::vector<llvm::GenericValue> gargv;
gv = EE->runFunction(func, gargv);
// Run static destructors.
EE->runStaticConstructorsDestructors(true);
qDebug()<<"run done.";
return gv;
}
static IROperator *irop = NULL;
static IROperator *getIROp()
{
if (::irop == NULL) {
::irop = new IROperator();
}
return ::irop;
}
/*
从llvm::GenericValue转换成这个方法的实际类型。
还需要有很多特定的转换要处理。
*/
QVariant mapGV2Variant(QString klass, QString method, QString symbol_name, llvm::GenericValue gv)
{
IROperator *irop = getIROp();
QVector<QVariant> args;
// QString retype = irop->resolve_return_type(klass, method, args, symbol_name);
QVariant vretype;
bool bret = irop->mfe->get_method_return_type(klass, method, args, symbol_name, vretype);
QString retype = vretype.toString();
// if retype is empty, maybe a ctor
if (retype.isEmpty()) return QVariant();
if (retype == "int" || retype == "uint" || retype == "long" || retype == "ulong"
|| retype == "qlonglong" || retype == "qulonglong"
|| retype == "short" || retype == "ushort" ) {
return QVariant((qlonglong)llvm::GVTOP(gv));
} else if (retype == "bool") {
return QVariant((bool)llvm::GVTOP(gv));
} else if (retype == "double" || retype == "float") {
return QVariant((double)(long)llvm::GVTOP(gv));
} else if (retype == "void") {
return QVariant();
}
if (retype == "QString") {
return QVariant(QString(*(QString*)llvm::GVTOP(gv)));
}
if (retype == "QString &") {
return QVariant(QString(*(QString*)llvm::GVTOP(gv)));
}
return QVariant();
}
void *jit_vm_new(QString klass, QVector<QVariant> args)
{
void *jo = NULL;
IROperator *irop = getIROp();
llvm::Module *module = irop->dmod;
llvm::IRBuilder<> &builder = irop->builder;
llvm::LLVMContext &ctx = irop->ctx;
// entry func
llvm::Function *entry_func =
llvm::Function::Create(llvm::FunctionType::get(builder.getVoidTy()->getPointerTo(), false),
llvm::Function::ExternalLinkage,
"yamain", module);
builder.SetInsertPoint(llvm::BasicBlock::Create(ctx, "eee", entry_func));
irop->knew(klass);
// module->dump();
DUMP_IR(module);
llvm::GenericValue gv = jit_execute_func(module, entry_func);
jo = llvm::GVTOP(gv);
return jo;
}
QVariant jit_vm_call(void *kthis, QString klass, QString method, QVector<QVariant> args)
{
IROperator *irop = getIROp();
llvm::Module *module = irop->dmod;
llvm::IRBuilder<> &builder = irop->builder;
llvm::LLVMContext &ctx = irop->ctx;
QString symbol_name;
// entry func
std::vector<llvm::Type *> entry_func_type = {builder.getVoidTy()->getPointerTo()};
llvm::ArrayRef<llvm::Type*> ref_entry_func_type(entry_func_type);
llvm::Function *entry_func =
llvm::Function::Create(llvm::FunctionType::get(builder.getVoidTy()->getPointerTo(),
ref_entry_func_type, false),
llvm::Function::ExternalLinkage,
"yamain2", module);
builder.SetInsertPoint(llvm::BasicBlock::Create(ctx, "eee", entry_func));
int i = 0;
for (auto it = entry_func->arg_begin(); it != entry_func->arg_end(); it ++, i++) {
llvm::Argument &a = *it;
if (i == 0) a.setName("kthis");
else a.setName(QString("fa%1").arg(i).toStdString());
}
irop->call(kthis, klass, method, args, symbol_name);
// module->dump();
DUMP_IR(module);
qDebug()<<"============module dump end.";
llvm::GenericValue gv = jit_execute_func(module, entry_func);
int iret = gv.IntVal.getZExtValue();
qDebug()<<"raw ret:"<<iret<<llvm::GVTOP(gv);
// how to known return type
// 需要把gv解释并转换为为什么类型呢。
// 怎么转呢?
// GenericValue要求首先要知道返回值的类型才能解释
int tyno = QMetaType::Int;
switch (tyno) {
case QMetaType::Int:
iret = (long)(llvm::GVTOP(gv));
// return QVariant(iret);
default:
break;
}
qDebug()<<"symbol name:"<<symbol_name;
QVariant vret = mapGV2Variant(klass, method, symbol_name, gv);
entry_func->deleteBody(); // 删除函数体
entry_func->removeFromParent(); // 删除整个函数
// entry_func->eraseFromParent(); // 清理干净, 再执行这个就crash,为什么提供了这几个不同的方法呢?
// entry_func->replaceAllUsesWith(llvm::UndefValue::get(entry_func->getType()));
// entry_func->dropAllReferences();
delete entry_func;
// qDebug()<<"===============";
// module->dump();
// qDebug()<<"===============";
return vret;
return QVariant();
}
//////////////////////////////////
/////
//////////////////////////////////
Clvm::Clvm() : QThread()
{
static char *argv[] = {
(char*)"prettystack"
};
static int argc = 1;
llvm::sys::PrintStackTraceOnErrorSignal();
llvm::PrettyStackTraceProgram X(argc, argv);
init();
initCompiler();
}
Clvm::~Clvm()
{
}
bool Clvm::init()
{
// 是由于定义了链接参数:-Wl,--allow-multiple-definition引起的上面这个错误。
// CommandLine Error: Option 'load' registered more than once!
llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmPrinter();
llvm::InitializeNativeTargetAsmParser();
llvm::InitializeNativeTargetDisassembler();
return true;
}
bool Clvm::initCompiler()
{
clang::CompilerInstance *cis = new clang::CompilerInstance();
clang::CompilerInvocation *civ = new clang::CompilerInvocation();
cis->createDiagnostics();
cis->createFileManager();
cis->createSourceManager(cis->getFileManager());
cis->createFrontendTimer();
// cis.createASTContext();
// cis.createModuleManager();
std::string path = "./myjitqt";
clang::driver::Driver *drv = new clang::driver::Driver(path, llvm::sys::getProcessTriple(),
cis->getDiagnostics());
drv->setTitle("myjitclangpp");
drv->setCheckInputsExist(false);
this->mcis = cis;
this->mciv = civ;
this->mdrv = drv;
static char *argv[] = {
(char*)"myjitqtrunner", (char*)"flycode.cxx",
(char*)"-fPIC", (char*)"-x", (char*)"c++",
(char*)"-I/usr/include/qt", (char*)"-I/usr/include/qt/QtCore",
(char*)"-I/usr/lib/clang/3.5.0/include",
};
static int argc = 8;
char **targv = argv;
llvm::SmallVector<const char*, 16> drv_args(targv, targv + argc);
drv_args.push_back("-S");
clang::driver::Compilation *C = drv->BuildCompilation(drv_args);
const clang::driver::JobList &Jobs = C->getJobs();
const clang::driver::Command *Cmd = llvm::cast<clang::driver::Command>(&(*Jobs.begin()));
const clang::driver::ArgStringList &CCArgs = Cmd->getArguments();
// clang::CompilerInvocation *civ = new clang::CompilerInvocation();
bool bret = clang::CompilerInvocation::CreateFromArgs(*civ,
const_cast<const char**>(CCArgs.data()),
const_cast<const char**>(CCArgs.data()) + CCArgs.size(),
cis->getDiagnostics());
cis->setInvocation(civ);
return true;
}
bool Clvm::initExecutionEngine()
{
return true;
}
llvm::GenericValue
Clvm::execute(QString &code, std::vector<llvm::GenericValue> &args, QString func_entry)
{
llvm::GenericValue rgv;
bool bret;
const char *pcode = strdup(code.toLatin1().data());
std::unique_ptr<llvm::MemoryBuffer> mbuf = llvm::MemoryBuffer::getMemBuffer(pcode);
clang::PreprocessorOptions &ppOpt = this->mcis->getPreprocessorOpts();
ppOpt.addRemappedFile("flycode.cxx", mbuf.release());
clang::EmitLLVMOnlyAction llvm_only_action;
bret = mcis->ExecuteAction(llvm_only_action);
std::unique_ptr<llvm::Module> mod = llvm_only_action.takeModule();
qDebug()<<"compile code done."<<mod.get();
if (mod == NULL) {
} else {
rgv = run_module_func(mod.get(), args, func_entry);
}
return rgv;
}
llvm::GenericValue
Clvm::execute2(llvm::Module *mod, QString func_entry)
{
// 只能放在这,run action之后,exeucte function之前
llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmPrinter();
llvm::InitializeNativeTargetAsmParser();
// llvm::InitializeNativeTargetDisassembler();
// run module
std::string errstr;
std::unique_ptr<llvm::Module> tmod(mod);
llvm::EngineBuilder eb(std::move(tmod));
eb.setErrorStr(&errstr);
// eb.setUseMCJIT(true);
llvm::ExecutionEngine *EE = eb.create();
// ClvmJitListener *lsner = new ClvmJitListener();
if (!mlsner) mlsner = ClvmJitListener::inst();
EE->RegisterJITEventListener(mlsner);
//*
qDebug()<<"before load obj...";
llvm::ErrorOr<llvm::object::OwningBinary<llvm::object::ObjectFile> > obj =
llvm::object::ObjectFile::createObjectFile("/usr/lib/libQt5Sql.so");
if (!obj) {
qDebug()<<"create obj file error.";
} else {
// crash, 是不是因为当前已经是在.so中了呢?
// EE->addObjectFile(std::unique_ptr<llvm::object::ObjectFile>(obj.get()));
}
// */
EE->finalizeObject();
EE->runStaticConstructorsDestructors(false);
llvm::Function *etyfn = NULL;
QString mangle_name;
llvm::ValueSymbolTable &vst = mod->getValueSymbolTable();
for (auto sit = vst.begin(); sit != vst.end(); sit++) {
auto k = sit->first();
auto v = sit->second;
if (QString(v->getName().data()).startsWith("__PRETTY_FUNCTION__")) {
continue;
}
if (QString(v->getName().data()).startsWith("_Z15__jit_main_tmplv")) {
continue;
}
// if (QString(v->getName().data()).indexOf("jit_main") >= 0) {
if (QString(v->getName().data()).indexOf(func_entry) >= 0) {
etyfn = mod->getFunction(v->getName());
mangle_name = QString(v->getName().data());
// break;
}
qDebug()<<""<<k.data()<<v->getName().data();
// v->dump();
}
qDebug()<<"our fun:"<<func_entry<<mangle_name<<etyfn;
std::vector<llvm::GenericValue> args;
llvm::GenericValue rgv = EE->runFunction(etyfn, args);
qDebug()<<"err:"<<errstr.c_str();
EE->runStaticConstructorsDestructors(true);
// cleanups
qDebug()<<"run code done."<<llvm::GVTOP(rgv)<<rgv.IntVal.getZExtValue();
return rgv;
}
llvm::GenericValue
Clvm::run_module_func(llvm::Module *mod, std::vector<llvm::GenericValue> &args, QString func_entry)
{
// 只能放在这,run action之后,exeucte function之前
llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmPrinter();
llvm::InitializeNativeTargetAsmParser();
// llvm::InitializeNativeTargetDisassembler();
// run module
std::unique_ptr<llvm::Module> tmod(mod);
llvm::EngineBuilder eb(std::move(tmod));
// eb.setUseMCJIT(true);
llvm::ExecutionEngine *EE = eb.create();
//*
qDebug()<<"before load obj...";
llvm::ErrorOr<llvm::object::OwningBinary<llvm::object::ObjectFile> > obj =
llvm::object::ObjectFile::createObjectFile("/usr/lib/libQt5Sql.so");
if (!obj) {
qDebug()<<"create obj file error.";
} else {
// crash, 是不是因为当前已经是在.so中了呢?
// EE->addObjectFile(std::unique_ptr<llvm::object::ObjectFile>(obj.get()));
}
// */
EE->finalizeObject();
EE->runStaticConstructorsDestructors(false);
llvm::Function *etyfn = NULL;
QString mangle_name;
llvm::ValueSymbolTable &vst = mod->getValueSymbolTable();
for (auto sit = vst.begin(); sit != vst.end(); sit++) {
auto k = sit->first();
auto v = sit->second;
if (QString(v->getName().data()).startsWith("__PRETTY_FUNCTION__")) {
continue;
}
// if (QString(v->getName().data()).indexOf("jit_main") >= 0) {
if (QString(v->getName().data()).indexOf(func_entry) >= 0) {
etyfn = mod->getFunction(v->getName());
mangle_name = QString(v->getName().data());
// break;
}
qDebug()<<""<<k.data()<<v->getName().data();
// v->dump();
}
qDebug()<<"our fun:"<<func_entry<<mangle_name<<etyfn;
// std::vector<llvm::GenericValue> eeargs;
llvm::GenericValue rgv = EE->runFunction(etyfn, args);
EE->runStaticConstructorsDestructors(true);
// cleanups
qDebug()<<"run code done.";
return rgv;
}
void Clvm::run()
{
}
void Clvm::deleteAsync(QString klass_name, void *obj)
{
}