1+ /*
2+ * @Author: sxf
3+ * @Date: 2015-12-26 09:51:14
4+ * @Last Modified by: sxf
5+ * @Last Modified time: 2015-12-26 19:21:50
6+ */
7+
8+ #include " PackageJIT.h"
9+ #include < string>
10+ #include < iostream>
11+ #include " MetaScriptRunner.h"
12+
13+ #include " llvm/ExecutionEngine/GenericValue.h"
14+ #include " llvm/ExecutionEngine/Interpreter.h"
15+ #include " llvm/IR/Constants.h"
16+ #include " llvm/IR/DerivedTypes.h"
17+ #include " llvm/IR/IRBuilder.h"
18+ #include " llvm/IR/Instructions.h"
19+ #include " llvm/IR/LLVMContext.h"
20+ #include " llvm/IR/Module.h"
21+ #include < llvm/IRReader/IRReader.h>
22+ #include < llvm/Support/SourceMgr.h>
23+ #include " llvm/Support/ManagedStatic.h"
24+ #include " llvm/Support/TargetSelect.h"
25+ #include < llvm/Support/MemoryBuffer.h>
26+ #include " llvm/Support/raw_ostream.h"
27+ #include < llvm/Support/DynamicLibrary.h>
28+
29+ using namespace llvm ;
30+ using namespace std ;
31+
32+ class PackageJIT_private
33+ {
34+ public:
35+ PackageJIT_private () {
36+ InitializeNativeTarget ();
37+ }
38+ ~PackageJIT_private () {
39+ delete EE;
40+ }
41+ LLVMContext context;
42+ ExecutionEngine* EE = NULL ;
43+
44+ void initEE (std::unique_ptr<Module> Owner) {
45+ if (EE == NULL )
46+ EE = EngineBuilder (std::move (Owner)).create ();
47+ else
48+ EE->addModule (std::move (Owner));
49+ }
50+
51+ void LoadPlugin (const std::string& path, const std::string& name, MetaScriptRunner* msr) {
52+
53+ SMDiagnostic error;
54+ std::unique_ptr<Module> Owner = getLazyIRFileModule (path, error, context);
55+ if (Owner == nullptr ) {
56+ cout << " Load Error: " << path << endl;
57+ Owner->dump ();
58+ return ;
59+ }
60+
61+ initEE (std::move (Owner));
62+
63+ string func_name = name + " _elite_plugin_init" ;
64+ Function* func = EE->FindFunctionNamed (func_name.c_str ());
65+
66+ std::vector<GenericValue> args;
67+ args.push_back (GenericValue (msr->getCodeGenContext ()));
68+ GenericValue gv = EE->runFunction (func, args);
69+ // plugin_init_func init = (plugin_init_func)(intptr_t)(EE->getPointerToFunction(func));
70+ // init(msr->getCodeGenContext());
71+ }
72+
73+ };
74+
75+
76+
77+ PackageJIT::PackageJIT () {
78+ }
79+
80+ PackageJIT::~PackageJIT () {
81+ delete priv;
82+ }
83+
84+
85+ void PackageJIT::LoadPlugin (const std::string& path, const std::string& name, MetaScriptRunner* msr) {
86+ getInstance ()->LoadPlugin (path, name, msr);
87+ }
88+
89+ void PackageJIT::AddSymbol (const std::string& name, void * ptr) {
90+ llvm::sys::DynamicLibrary::AddSymbol (name, ptr);
91+ }
92+
93+ PackageJIT_private* PackageJIT::getInstance () {
94+ if (priv == NULL )
95+ priv = new PackageJIT_private ();
96+ return priv;
97+ }
98+
99+ PackageJIT_private* PackageJIT::priv = NULL ;
0 commit comments