HowToUseJIT.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //===-- examples/HowToUseJIT/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 small program provides an example of how to quickly build a small
  11. // module with two functions and execute it with the JIT.
  12. //
  13. // Goal:
  14. // The goal of this snippet is to create in the memory
  15. // the LLVM module consisting of two functions as follow:
  16. //
  17. // int add1(int x) {
  18. // return x+1;
  19. // }
  20. //
  21. // int foo() {
  22. // return add1(10);
  23. // }
  24. //
  25. // then compile the module via JIT, then execute the `foo'
  26. // function and return result to a driver, i.e. to a "host program".
  27. //
  28. // Some remarks and questions:
  29. //
  30. // - could we invoke some code using noname functions too?
  31. // e.g. evaluate "foo()+foo()" without fears to introduce
  32. // conflict of temporary function name with some real
  33. // existing function name?
  34. //
  35. //===----------------------------------------------------------------------===//
  36. #include "llvm/Module.h"
  37. #include "llvm/Constants.h"
  38. #include "llvm/Type.h"
  39. #include "llvm/Instructions.h"
  40. #include "llvm/ModuleProvider.h"
  41. #include "llvm/ExecutionEngine/ExecutionEngine.h"
  42. #include "llvm/ExecutionEngine/GenericValue.h"
  43. #include <iostream>
  44. using namespace llvm;
  45. int main() {
  46. // Create some module to put our function into it.
  47. Module *M = new Module("test");
  48. // Create the add1 function entry and insert this entry into module M. The
  49. // function will have a return type of "int" and take an argument of "int".
  50. // The '0' terminates the list of argument types.
  51. Function *Add1F = M->getOrInsertFunction("add1", Type::IntTy, Type::IntTy, 0);
  52. // Add a basic block to the function. As before, it automatically inserts
  53. // because of the last argument.
  54. BasicBlock *BB = new BasicBlock("EntryBlock", Add1F);
  55. // Get pointers to the constant `1'.
  56. Value *One = ConstantSInt::get(Type::IntTy, 1);
  57. // Get pointers to the integer argument of the add1 function...
  58. assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
  59. Argument *ArgX = Add1F->arg_begin(); // Get the arg
  60. ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
  61. // Create the add instruction, inserting it into the end of BB.
  62. Instruction *Add = BinaryOperator::createAdd(One, ArgX, "addresult", BB);
  63. // Create the return instruction and add it to the basic block
  64. new ReturnInst(Add, BB);
  65. // Now, function add1 is ready.
  66. // Now we going to create function `foo', which returns an int and takes no
  67. // arguments.
  68. Function *FooF = M->getOrInsertFunction("foo", Type::IntTy, 0);
  69. // Add a basic block to the FooF function.
  70. BB = new BasicBlock("EntryBlock", FooF);
  71. // Get pointers to the constant `10'.
  72. Value *Ten = ConstantSInt::get(Type::IntTy, 10);
  73. // Pass Ten to the call call:
  74. std::vector<Value*> Params;
  75. Params.push_back(Ten);
  76. CallInst *Add1CallRes = new CallInst(Add1F, Params, "add1", BB);
  77. Add1CallRes->setTailCall(true);
  78. // Create the return instruction and add it to the basic block.
  79. new ReturnInst(Add1CallRes, BB);
  80. // Now we create the JIT.
  81. ExistingModuleProvider* MP = new ExistingModuleProvider(M);
  82. ExecutionEngine* EE = ExecutionEngine::create(MP, false);
  83. std::cout << "We just constructed this LLVM module:\n\n" << *M;
  84. std::cout << "\n\nRunning foo: " << std::flush;
  85. // Call the `foo' function with no arguments:
  86. std::vector<GenericValue> noargs;
  87. GenericValue gv = EE->runFunction(FooF, noargs);
  88. // Import result of execution:
  89. std::cout << "Result: " << gv.IntVal << "\n";
  90. return 0;
  91. }