Paste will expire never.
- #include "stdafx.h"
- #include "iostream"
- #include "llvm/Module.h"
- #include "llvm/Function.h"
- #include "llvm/PassManager.h"
- #include "llvm/CallingConv.h"
- #include "llvm/Analysis/Verifier.h"
- #include "llvm/Assembly/PrintModulePass.h"
- #include "llvm/Support/IRBuilder.h"
- #include "llvm/Support/raw_ostream.h"
- #include "llvm/ModuleProvider.h"
- #include "llvm/ExecutionEngine/ExecutionEngine.h"
- #include "llvm/ExecutionEngine/Jit.h"
- #include "llvm/ExecutionEngine/GenericValue.h"
- //#include "llvm/ExecutionEngine/Interpreter.h"
- #include "llvm/Target/TargetData.h"
- #pragma comment(lib, "VMCore.lib")
- #pragma comment(lib, "support.lib")
- #pragma comment(lib, "system.lib")
- #pragma comment(lib, "ExecutionEngine.lib")
- #pragma comment(lib, "Target.lib")
- #pragma comment(lib, "Transforms.lib")
- #pragma comment(lib, "CodeGen.lib")
- #pragma comment(lib, "Analysis.lib")
- #pragma comment(lib, "x86.lib")
- #pragma comment(linker, "/include:_X86TargetMachineModule")
- using namespace llvm;
- void make_function(Module ** m, Function ** func) {
- //Создаем модуль
- Module* mod = new Module("test");
- //Создаем функцию 'int add(int, int)'
- Constant* c = mod->getOrInsertFunction("add",
- /*ret type*/ IntegerType::get(32),
- /*args*/ IntegerType::get(32),
- IntegerType::get(32),
- /*varargs terminated with null*/ NULL);
- Function* add = cast<Function>(c);
- add->setCallingConv(CallingConv::C);
- Function::arg_iterator args = add->arg_begin();
- Value* x = args++;
- Value* y = args++;
- //Создаем тело функции
- BasicBlock* block = BasicBlock::Create("entry", add);
- IRBuilder<> builder(block);
- Value* tmp = builder.CreateBinOp(Instruction::Add, x, y, "tmp");
- builder.CreateRet(tmp);
- *func = add;
- *m = mod;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- Function * add;
- Module * Mod;
- make_function(&Mod, &add);
- ExistingModuleProvider * mp = new ExistingModuleProvider(Mod);
- std::string error;
- ExecutionEngine * engine = ExecutionEngine::createJIT(mp, &error);
- if (!engine)
- {
- std::cout << "Error: " << error;
- }
- std::cout << "Created\n" << *Mod;
- int (*fp)(int, int) = (int (*)(int, int))engine->getPointerToFunction(add);
- int res = fp(10, 5);
- std::cout << "Result: " << res;
- delete Mod;
- return 0;
- }
Editing is locked.