v 0. Pasted by konstantin as cpp at 2009-04-03 16:56:05 MSK and set expiration to never.

Paste will expire never.

  1. #include "stdafx.h"
  2. #include "iostream"
  3.  
  4. #include "llvm/Module.h"
  5. #include "llvm/Function.h"
  6. #include "llvm/PassManager.h"
  7. #include "llvm/CallingConv.h"
  8. #include "llvm/Analysis/Verifier.h"
  9. #include "llvm/Assembly/PrintModulePass.h"
  10. #include "llvm/Support/IRBuilder.h"
  11. #include "llvm/Support/raw_ostream.h"
  12. #include "llvm/ModuleProvider.h"
  13. #include "llvm/ExecutionEngine/ExecutionEngine.h"
  14. #include "llvm/ExecutionEngine/Jit.h"
  15. #include "llvm/ExecutionEngine/GenericValue.h"
  16. //#include "llvm/ExecutionEngine/Interpreter.h"
  17. #include "llvm/Target/TargetData.h"
  18.  
  19. #pragma comment(lib, "VMCore.lib")
  20. #pragma comment(lib, "support.lib")
  21. #pragma comment(lib, "system.lib")
  22. #pragma comment(lib, "ExecutionEngine.lib")
  23. #pragma comment(lib, "Target.lib")
  24. #pragma comment(lib, "Transforms.lib")
  25. #pragma comment(lib, "CodeGen.lib")
  26. #pragma comment(lib, "Analysis.lib")
  27. #pragma comment(lib, "x86.lib")
  28. #pragma comment(linker, "/include:_X86TargetMachineModule")
  29.  
  30.  
  31. using namespace llvm;
  32.  
  33.  
  34. void make_function(Module ** m, Function ** func) {
  35.       //Создаем модуль
  36.       Module* mod = new Module("test");
  37.  
  38.       //Создаем функцию 'int add(int, int)'
  39.       Constant* c = mod->getOrInsertFunction("add",
  40.       /*ret type*/                           IntegerType::get(32),
  41.       /*args*/                               IntegerType::get(32),
  42.                                              IntegerType::get(32),                                       
  43.       /*varargs terminated with null*/       NULL);
  44.      
  45.       Function* add = cast<Function>(c);
  46.       add->setCallingConv(CallingConv::C);
  47.      
  48.       Function::arg_iterator args = add->arg_begin();
  49.       Value* x = args++; 
  50.       Value* y = args++;
  51.  
  52.       //Создаем тело функции
  53.       BasicBlock* block = BasicBlock::Create("entry", add);
  54.       IRBuilder<> builder(block);
  55.  
  56.       Value* tmp =  builder.CreateBinOp(Instruction::Add, x, y, "tmp")
  57.                     builder.CreateRet(tmp);
  58.  
  59.       *func = add;
  60.       *m = mod;
  61. }
  62.  
  63. int _tmain(int argc, _TCHAR* argv[])
  64. {
  65.     Function * add;
  66.     Module * Mod;
  67.     make_function(&Mod, &add);
  68.  
  69.     ExistingModuleProvider * mp = new ExistingModuleProvider(Mod);
  70.     std::string error;
  71.     ExecutionEngine * engine = ExecutionEngine::createJIT(mp, &error);
  72.     if (!engine)
  73.     {
  74.         std::cout << "Error: " << error;
  75.     }
  76.  
  77.     std::cout << "Created\n" << *Mod;
  78.  
  79.     int (*fp)(int, int) = (int (*)(int, int))engine->getPointerToFunction(add);
  80.  
  81.     int res = fp(10, 5);   
  82.     std::cout << "Result: " << res;
  83.  
  84.     delete Mod;
  85.     return 0;
  86. }


Editing is locked.