Reid Spencer | 66e7cd0 | 2004-09-11 20:30:11 +0000 | [diff] [blame] | 1 | //===-- examples/HowToUseJIT/HowToUseJIT.cpp - An example use of the JIT --===// |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | fc001bb | 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. |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Reid Spencer | 54706d6 | 2004-08-10 19:18:51 +0000 | [diff] [blame] | 10 | // This small program provides an example of how to quickly build a small |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 11 | // module with two functions and execute it with the JIT. |
| 12 | // |
| 13 | // Goal: |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 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 | // } |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 20 | // |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 21 | // int foo() { |
| 22 | // return add1(10); |
| 23 | // } |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 24 | // |
| 25 | // then compile the module via JIT, then execute the `foo' |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 26 | // function and return result to a driver, i.e. to a "host program". |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 27 | // |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 28 | // Some remarks and questions: |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 29 | // |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 30 | // - could we invoke some code using noname functions too? |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 31 | // e.g. evaluate "foo()+foo()" without fears to introduce |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 32 | // conflict of temporary function name with some real |
| 33 | // existing function name? |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 34 | // |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 35 | //===----------------------------------------------------------------------===// |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 36 | |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 37 | #include "llvm/Module.h" |
| 38 | #include "llvm/Constants.h" |
Reid Spencer | bc0895a | 2007-01-19 22:45:05 +0000 | [diff] [blame] | 39 | #include "llvm/DerivedTypes.h" |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 40 | #include "llvm/Instructions.h" |
| 41 | #include "llvm/ModuleProvider.h" |
Jeff Cohen | afebb44 | 2006-03-24 03:11:31 +0000 | [diff] [blame] | 42 | #include "llvm/ExecutionEngine/JIT.h" |
| 43 | #include "llvm/ExecutionEngine/Interpreter.h" |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 44 | #include "llvm/ExecutionEngine/GenericValue.h" |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 45 | #include <iostream> |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 46 | using namespace llvm; |
| 47 | |
| 48 | int main() { |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 49 | // Create some module to put our function into it. |
| 50 | Module *M = new Module("test"); |
| 51 | |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 52 | // Create the add1 function entry and insert this entry into module M. The |
| 53 | // function will have a return type of "int" and take an argument of "int". |
| 54 | // The '0' terminates the list of argument types. |
Chris Lattner | 6a98754 | 2007-01-07 07:40:09 +0000 | [diff] [blame] | 55 | Function *Add1F = |
| 56 | cast<Function>(M->getOrInsertFunction("add1", Type::Int32Ty, Type::Int32Ty, |
| 57 | (Type *)0)); |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 58 | |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 59 | // Add a basic block to the function. As before, it automatically inserts |
| 60 | // because of the last argument. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame^] | 61 | BasicBlock *BB = BasicBlock::Create("EntryBlock", Add1F); |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 62 | |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 63 | // Get pointers to the constant `1'. |
Reid Spencer | db8d2be | 2006-12-31 05:50:28 +0000 | [diff] [blame] | 64 | Value *One = ConstantInt::get(Type::Int32Ty, 1); |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 65 | |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 66 | // Get pointers to the integer argument of the add1 function... |
Alkis Evlogimenos | 1d9f262 | 2005-03-15 07:20:55 +0000 | [diff] [blame] | 67 | assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg |
| 68 | Argument *ArgX = Add1F->arg_begin(); // Get the arg |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 69 | ArgX->setName("AnArg"); // Give it a nice symbolic name for fun. |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 70 | |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 71 | // Create the add instruction, inserting it into the end of BB. |
| 72 | Instruction *Add = BinaryOperator::createAdd(One, ArgX, "addresult", BB); |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 73 | |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 74 | // Create the return instruction and add it to the basic block |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame^] | 75 | ReturnInst::Create(Add, BB); |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 76 | |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 77 | // Now, function add1 is ready. |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 78 | |
| 79 | |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 80 | // Now we going to create function `foo', which returns an int and takes no |
| 81 | // arguments. |
Chris Lattner | 6a98754 | 2007-01-07 07:40:09 +0000 | [diff] [blame] | 82 | Function *FooF = |
| 83 | cast<Function>(M->getOrInsertFunction("foo", Type::Int32Ty, (Type *)0)); |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 84 | |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 85 | // Add a basic block to the FooF function. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame^] | 86 | BB = BasicBlock::Create("EntryBlock", FooF); |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 87 | |
| 88 | // Get pointers to the constant `10'. |
Reid Spencer | db8d2be | 2006-12-31 05:50:28 +0000 | [diff] [blame] | 89 | Value *Ten = ConstantInt::get(Type::Int32Ty, 10); |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 90 | |
| 91 | // Pass Ten to the call call: |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame^] | 92 | CallInst *Add1CallRes = CallInst::Create(Add1F, Ten, "add1", BB); |
Chris Lattner | 47968e4 | 2005-05-06 05:59:50 +0000 | [diff] [blame] | 93 | Add1CallRes->setTailCall(true); |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 94 | |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 95 | // Create the return instruction and add it to the basic block. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame^] | 96 | ReturnInst::Create(Add1CallRes, BB); |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 97 | |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 98 | // Now we create the JIT. |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 99 | ExistingModuleProvider* MP = new ExistingModuleProvider(M); |
Chris Lattner | 95f114c | 2004-08-15 23:31:57 +0000 | [diff] [blame] | 100 | ExecutionEngine* EE = ExecutionEngine::create(MP, false); |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 101 | |
| 102 | std::cout << "We just constructed this LLVM module:\n\n" << *M; |
| 103 | std::cout << "\n\nRunning foo: " << std::flush; |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 104 | |
| 105 | // Call the `foo' function with no arguments: |
| 106 | std::vector<GenericValue> noargs; |
| 107 | GenericValue gv = EE->runFunction(FooF, noargs); |
| 108 | |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 109 | // Import result of execution: |
Chris Lattner | 9132a2b | 2007-08-23 05:15:32 +0000 | [diff] [blame] | 110 | std::cout << "Result: " << gv.IntVal.toStringUnsigned(10) << "\n"; |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 111 | return 0; |
| 112 | } |