Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- examples/HowToUseJIT/HowToUseJIT.cpp - An example use of the JIT --===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 45ca7c1 | 2007-12-29 20:37:57 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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 | |
| 37 | #include "llvm/Module.h" |
| 38 | #include "llvm/Constants.h" |
| 39 | #include "llvm/DerivedTypes.h" |
| 40 | #include "llvm/Instructions.h" |
| 41 | #include "llvm/ModuleProvider.h" |
| 42 | #include "llvm/ExecutionEngine/JIT.h" |
| 43 | #include "llvm/ExecutionEngine/Interpreter.h" |
| 44 | #include "llvm/ExecutionEngine/GenericValue.h" |
Chris Lattner | 8ed24c5 | 2009-06-17 16:48:44 +0000 | [diff] [blame] | 45 | #include "llvm/Target/TargetSelect.h" |
Edwin Török | 29258f7 | 2009-04-07 17:23:02 +0000 | [diff] [blame] | 46 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | 1fefaac | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 47 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 48 | using namespace llvm; |
| 49 | |
| 50 | int main() { |
Chris Lattner | 8ed24c5 | 2009-06-17 16:48:44 +0000 | [diff] [blame] | 51 | |
| 52 | InitializeNativeTarget(); |
| 53 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 54 | // Create some module to put our function into it. |
| 55 | Module *M = new Module("test"); |
| 56 | |
| 57 | // Create the add1 function entry and insert this entry into module M. The |
| 58 | // function will have a return type of "int" and take an argument of "int". |
| 59 | // The '0' terminates the list of argument types. |
| 60 | Function *Add1F = |
| 61 | cast<Function>(M->getOrInsertFunction("add1", Type::Int32Ty, Type::Int32Ty, |
| 62 | (Type *)0)); |
| 63 | |
| 64 | // Add a basic block to the function. As before, it automatically inserts |
| 65 | // because of the last argument. |
Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 66 | BasicBlock *BB = BasicBlock::Create("EntryBlock", Add1F); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 67 | |
| 68 | // Get pointers to the constant `1'. |
| 69 | Value *One = ConstantInt::get(Type::Int32Ty, 1); |
| 70 | |
| 71 | // Get pointers to the integer argument of the add1 function... |
| 72 | assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg |
| 73 | Argument *ArgX = Add1F->arg_begin(); // Get the arg |
| 74 | ArgX->setName("AnArg"); // Give it a nice symbolic name for fun. |
| 75 | |
| 76 | // Create the add instruction, inserting it into the end of BB. |
Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 77 | Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 78 | |
| 79 | // Create the return instruction and add it to the basic block |
Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 80 | ReturnInst::Create(Add, BB); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 81 | |
| 82 | // Now, function add1 is ready. |
| 83 | |
| 84 | |
| 85 | // Now we going to create function `foo', which returns an int and takes no |
| 86 | // arguments. |
| 87 | Function *FooF = |
| 88 | cast<Function>(M->getOrInsertFunction("foo", Type::Int32Ty, (Type *)0)); |
| 89 | |
| 90 | // Add a basic block to the FooF function. |
Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 91 | BB = BasicBlock::Create("EntryBlock", FooF); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 92 | |
| 93 | // Get pointers to the constant `10'. |
| 94 | Value *Ten = ConstantInt::get(Type::Int32Ty, 10); |
| 95 | |
| 96 | // Pass Ten to the call call: |
Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 97 | CallInst *Add1CallRes = CallInst::Create(Add1F, Ten, "add1", BB); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 98 | Add1CallRes->setTailCall(true); |
| 99 | |
| 100 | // Create the return instruction and add it to the basic block. |
Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 101 | ReturnInst::Create(Add1CallRes, BB); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 102 | |
| 103 | // Now we create the JIT. |
| 104 | ExistingModuleProvider* MP = new ExistingModuleProvider(M); |
| 105 | ExecutionEngine* EE = ExecutionEngine::create(MP, false); |
| 106 | |
Chris Lattner | 1fefaac | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 107 | outs() << "We just constructed this LLVM module:\n\n" << *M; |
| 108 | outs() << "\n\nRunning foo: "; |
| 109 | outs().flush(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 110 | |
| 111 | // Call the `foo' function with no arguments: |
| 112 | std::vector<GenericValue> noargs; |
| 113 | GenericValue gv = EE->runFunction(FooF, noargs); |
| 114 | |
| 115 | // Import result of execution: |
Chris Lattner | 1fefaac | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 116 | outs() << "Result: " << gv.IntVal << "\n"; |
Edwin Török | 29258f7 | 2009-04-07 17:23:02 +0000 | [diff] [blame] | 117 | EE->freeMachineCodeForFunction(FooF); |
| 118 | delete EE; |
| 119 | llvm_shutdown(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 120 | return 0; |
| 121 | } |