HowToUseJIT.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //===--- HowToUseJIT.cpp - An example use of the JIT ----------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file was developed by Valery A. Khamenya and is distributed under the
  6. // University of Illinois Open Source License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This tool provides a single point of access to the LLVM compilation tools.
  11. // It has many options. To discover the options supported please refer to the
  12. // tools' manual page (docs/CommandGuide/html/llvmc.html) or run the tool with
  13. // the --help option.
  14. //
  15. //===------------------------------------------------------------------------===
  16. // Goal:
  17. // The goal of this snippet is to create in the memory
  18. // the LLVM module consisting of two functions as follow:
  19. //
  20. // int add1(int x) {
  21. // return x+1;
  22. // }
  23. //
  24. // int foo() {
  25. // return add1(10);
  26. // }
  27. //
  28. // then compile the module via JIT, then execute the `foo'
  29. // function and return result to a driver, i.e. to a "host program".
  30. //
  31. // Some remarks and questions:
  32. //
  33. // - could we invoke some code using noname functions too?
  34. // e.g. evaluate "foo()+foo()" without fears to introduce
  35. // conflict of temporary function name with some real
  36. // existing function name?
  37. //
  38. #include <iostream>
  39. #include <llvm/Module.h>
  40. #include <llvm/DerivedTypes.h>
  41. #include <llvm/Constants.h>
  42. #include <llvm/Instructions.h>
  43. #include <llvm/ModuleProvider.h>
  44. #include "llvm/ExecutionEngine/ExecutionEngine.h"
  45. #include "llvm/ExecutionEngine/GenericValue.h"
  46. using namespace llvm;
  47. int main() {
  48. // Create some module to put our function into it.
  49. Module *M = new Module("test");
  50. // We are about to create the add1 function:
  51. Function *Add1F;
  52. {
  53. // first create type for the single argument of add1 function:
  54. // the type is 'int ()'
  55. std::vector<const Type*> ArgT(1);
  56. ArgT[0] = Type::IntTy;
  57. // now create full type of the add1 function:
  58. FunctionType *Add1T = FunctionType::get(Type::IntTy, // type of result
  59. ArgT,
  60. /*not vararg*/false);
  61. // Now create the add1 function entry and
  62. // insert this entry into module M
  63. // (By passing a module as the last parameter to the Function constructor,
  64. // it automatically gets appended to the Module.)
  65. Add1F = new Function(Add1T,
  66. Function::ExternalLinkage, // maybe too much
  67. "add1", M);
  68. // Add a basic block to the function... (again, it automatically inserts
  69. // because of the last argument.)
  70. BasicBlock *BB = new BasicBlock("EntryBlock of add1 function", Add1F);
  71. // Get pointers to the constant `1'...
  72. Value *One = ConstantSInt::get(Type::IntTy, 1);
  73. // Get pointers to the integer argument of the add1 function...
  74. assert(Add1F->abegin() != Add1F->aend()); // Make sure there's an arg
  75. Argument &ArgX = Add1F->afront(); // Get the arg
  76. // Create the add instruction... does not insert...
  77. Instruction *Add = BinaryOperator::create(Instruction::Add, One, &ArgX,
  78. "addresult");
  79. // explicitly insert it into the basic block...
  80. BB->getInstList().push_back(Add);
  81. // Create the return instruction and add it to the basic block
  82. BB->getInstList().push_back(new ReturnInst(Add));
  83. // function add1 is ready
  84. }
  85. // now we going to create function `foo':
  86. Function *FooF;
  87. {
  88. // Create the foo function type:
  89. FunctionType *FooT =
  90. FunctionType::get(Type::IntTy, // result has type: 'int ()'
  91. std::vector<const Type*>(), // no arguments
  92. /*not vararg*/false);
  93. // create the entry for function `foo' and insert
  94. // this entry into module M:
  95. FooF =
  96. new Function(FooT,
  97. Function::ExternalLinkage, // too wide?
  98. "foo", M);
  99. // Add a basic block to the FooF function...
  100. BasicBlock *BB = new BasicBlock("EntryBlock of add1 function", FooF);
  101. // Get pointers to the constant `10'...
  102. Value *Ten = ConstantSInt::get(Type::IntTy, 10);
  103. // Put the argument Ten on stack and make call:
  104. // ...
  105. std::vector<Value*> Params;
  106. Params.push_back(Ten);
  107. CallInst * Add1CallRes = new CallInst(Add1F, Params, "add1", BB);
  108. // Create the return instruction and add it to the basic block
  109. BB->getInstList().push_back(new ReturnInst(Add1CallRes));
  110. }
  111. // Now we going to create JIT ??
  112. ExistingModuleProvider* MP = new ExistingModuleProvider(M);
  113. ExecutionEngine* EE = ExecutionEngine::create( MP, true );
  114. // Call the `foo' function with no arguments:
  115. std::vector<GenericValue> noargs;
  116. GenericValue gv = EE->runFunction(FooF, noargs);
  117. // import result of execution:
  118. std::cout << "Result: " << gv.IntVal << std:: endl;
  119. return 0;
  120. }