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 |
Garrison Venn | f9fba30 | 2010-02-03 12:00:02 +0000 | [diff] [blame] | 15 | // the LLVM module consisting of two functions as follow: |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 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 | |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 37 | #include "llvm/LLVMContext.h" |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 38 | #include "llvm/Module.h" |
| 39 | #include "llvm/Constants.h" |
Reid Spencer | bc0895a | 2007-01-19 22:45:05 +0000 | [diff] [blame] | 40 | #include "llvm/DerivedTypes.h" |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 41 | #include "llvm/Instructions.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 | da06288 | 2009-06-17 16:48:44 +0000 | [diff] [blame] | 45 | #include "llvm/Target/TargetSelect.h" |
Torok Edwin | eb55f3e | 2009-04-07 17:23:02 +0000 | [diff] [blame] | 46 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 47 | #include "llvm/Support/raw_ostream.h" |
Eric Christopher | c3a627d | 2011-06-09 05:58:50 +0000 | [diff] [blame^] | 48 | #include "llvm/Support/IRBuilder.h" |
| 49 | |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 50 | using namespace llvm; |
| 51 | |
| 52 | int main() { |
Chris Lattner | da06288 | 2009-06-17 16:48:44 +0000 | [diff] [blame] | 53 | |
| 54 | InitializeNativeTarget(); |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 55 | |
| 56 | LLVMContext Context; |
Chris Lattner | da06288 | 2009-06-17 16:48:44 +0000 | [diff] [blame] | 57 | |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 58 | // Create some module to put our function into it. |
Owen Anderson | 31895e7 | 2009-07-01 21:22:36 +0000 | [diff] [blame] | 59 | Module *M = new Module("test", Context); |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 60 | |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 61 | // Create the add1 function entry and insert this entry into module M. The |
| 62 | // function will have a return type of "int" and take an argument of "int". |
| 63 | // The '0' terminates the list of argument types. |
Chris Lattner | 6a98754 | 2007-01-07 07:40:09 +0000 | [diff] [blame] | 64 | Function *Add1F = |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 65 | cast<Function>(M->getOrInsertFunction("add1", Type::getInt32Ty(Context), |
| 66 | Type::getInt32Ty(Context), |
Chris Lattner | 6a98754 | 2007-01-07 07:40:09 +0000 | [diff] [blame] | 67 | (Type *)0)); |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 68 | |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 69 | // Add a basic block to the function. As before, it automatically inserts |
| 70 | // because of the last argument. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 71 | BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", Add1F); |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 72 | |
Eric Christopher | c3a627d | 2011-06-09 05:58:50 +0000 | [diff] [blame^] | 73 | // Create a basic block builder with default parameters. The builder will |
| 74 | // automatically append instructions to the basic block `BB'. |
| 75 | IRBuilder<> builder(BB); |
| 76 | |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 77 | // Get pointers to the constant `1'. |
Eric Christopher | c3a627d | 2011-06-09 05:58:50 +0000 | [diff] [blame^] | 78 | Value *One = builder.getInt32(1); |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 79 | |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 80 | // Get pointers to the integer argument of the add1 function... |
Alkis Evlogimenos | 1d9f262 | 2005-03-15 07:20:55 +0000 | [diff] [blame] | 81 | assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg |
| 82 | Argument *ArgX = Add1F->arg_begin(); // Get the arg |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 83 | ArgX->setName("AnArg"); // Give it a nice symbolic name for fun. |
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 | // Create the add instruction, inserting it into the end of BB. |
Eric Christopher | c3a627d | 2011-06-09 05:58:50 +0000 | [diff] [blame^] | 86 | Value *Add = builder.CreateAdd(One, ArgX); |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 87 | |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 88 | // Create the return instruction and add it to the basic block |
Eric Christopher | c3a627d | 2011-06-09 05:58:50 +0000 | [diff] [blame^] | 89 | builder.CreateRet(Add); |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 90 | |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 91 | // Now, function add1 is ready. |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 92 | |
| 93 | |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 94 | // Now we going to create function `foo', which returns an int and takes no |
| 95 | // arguments. |
Chris Lattner | 6a98754 | 2007-01-07 07:40:09 +0000 | [diff] [blame] | 96 | Function *FooF = |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 97 | cast<Function>(M->getOrInsertFunction("foo", Type::getInt32Ty(Context), |
| 98 | (Type *)0)); |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 99 | |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 100 | // Add a basic block to the FooF function. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 101 | BB = BasicBlock::Create(Context, "EntryBlock", FooF); |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 102 | |
Eric Christopher | c3a627d | 2011-06-09 05:58:50 +0000 | [diff] [blame^] | 103 | // Tell the basic block builder to attach itself to the new basic block |
| 104 | builder.SetInsertPoint(BB); |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 105 | |
Eric Christopher | c3a627d | 2011-06-09 05:58:50 +0000 | [diff] [blame^] | 106 | // Get pointer to the constant `10'. |
| 107 | Value *Ten = builder.getInt32(10); |
| 108 | |
| 109 | // Pass Ten to the call to Add1F |
| 110 | CallInst *Add1CallRes = builder.CreateCall(Add1F, Ten); |
Chris Lattner | 47968e4 | 2005-05-06 05:59:50 +0000 | [diff] [blame] | 111 | Add1CallRes->setTailCall(true); |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 112 | |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 113 | // Create the return instruction and add it to the basic block. |
Eric Christopher | c3a627d | 2011-06-09 05:58:50 +0000 | [diff] [blame^] | 114 | builder.CreateRet(Add1CallRes); |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 115 | |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 116 | // Now we create the JIT. |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 117 | ExecutionEngine* EE = EngineBuilder(M).create(); |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 118 | |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 119 | outs() << "We just constructed this LLVM module:\n\n" << *M; |
| 120 | outs() << "\n\nRunning foo: "; |
| 121 | outs().flush(); |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 122 | |
| 123 | // Call the `foo' function with no arguments: |
| 124 | std::vector<GenericValue> noargs; |
| 125 | GenericValue gv = EE->runFunction(FooF, noargs); |
| 126 | |
Chris Lattner | e8bf58c | 2004-08-15 23:21:54 +0000 | [diff] [blame] | 127 | // Import result of execution: |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 128 | outs() << "Result: " << gv.IntVal << "\n"; |
Torok Edwin | eb55f3e | 2009-04-07 17:23:02 +0000 | [diff] [blame] | 129 | EE->freeMachineCodeForFunction(FooF); |
| 130 | delete EE; |
| 131 | llvm_shutdown(); |
Reid Spencer | 26a4ba7 | 2004-08-10 19:14:36 +0000 | [diff] [blame] | 132 | return 0; |
| 133 | } |