1987319d3SReid Spencer //===-- examples/HowToUseJIT/HowToUseJIT.cpp - An example use of the JIT --===// 2b987b7ceSReid Spencer // 3b987b7ceSReid Spencer // The LLVM Compiler Infrastructure 4b987b7ceSReid Spencer // 5bcf65db6SChris Lattner // This file is distributed under the University of Illinois Open Source 6bcf65db6SChris Lattner // License. See LICENSE.TXT for details. 7b987b7ceSReid Spencer // 8b987b7ceSReid Spencer //===----------------------------------------------------------------------===// 9b987b7ceSReid Spencer // 10b987b7ceSReid Spencer // This small program provides an example of how to quickly build a small 11b987b7ceSReid Spencer // module with two functions and execute it with the JIT. 12b987b7ceSReid Spencer // 13b987b7ceSReid Spencer // Goal: 14b987b7ceSReid Spencer // The goal of this snippet is to create in the memory 15b987b7ceSReid Spencer // the LLVM module consisting of two functions as follow: 16b987b7ceSReid Spencer // 17b987b7ceSReid Spencer // int add1(int x) { 18b987b7ceSReid Spencer // return x+1; 19b987b7ceSReid Spencer // } 20b987b7ceSReid Spencer // 21b987b7ceSReid Spencer // int foo() { 22b987b7ceSReid Spencer // return add1(10); 23b987b7ceSReid Spencer // } 24b987b7ceSReid Spencer // 25b987b7ceSReid Spencer // then compile the module via JIT, then execute the `foo' 26b987b7ceSReid Spencer // function and return result to a driver, i.e. to a "host program". 27b987b7ceSReid Spencer // 28b987b7ceSReid Spencer // Some remarks and questions: 29b987b7ceSReid Spencer // 30b987b7ceSReid Spencer // - could we invoke some code using noname functions too? 31b987b7ceSReid Spencer // e.g. evaluate "foo()+foo()" without fears to introduce 32b987b7ceSReid Spencer // conflict of temporary function name with some real 33b987b7ceSReid Spencer // existing function name? 34b987b7ceSReid Spencer // 35b987b7ceSReid Spencer //===----------------------------------------------------------------------===// 36b987b7ceSReid Spencer 376773d388SOwen Anderson #include "llvm/LLVMContext.h" 38b987b7ceSReid Spencer #include "llvm/Module.h" 39b987b7ceSReid Spencer #include "llvm/Constants.h" 40727f31b2SReid Spencer #include "llvm/DerivedTypes.h" 41b987b7ceSReid Spencer #include "llvm/Instructions.h" 42b987b7ceSReid Spencer #include "llvm/ModuleProvider.h" 43e46cbe7aSJeff Cohen #include "llvm/ExecutionEngine/JIT.h" 44e46cbe7aSJeff Cohen #include "llvm/ExecutionEngine/Interpreter.h" 45b987b7ceSReid Spencer #include "llvm/ExecutionEngine/GenericValue.h" 46d24df245SChris Lattner #include "llvm/Target/TargetSelect.h" 4773312b3fSTorok Edwin #include "llvm/Support/ManagedStatic.h" 480c19df48SChris Lattner #include "llvm/Support/raw_ostream.h" 49b987b7ceSReid Spencer using namespace llvm; 50b987b7ceSReid Spencer 51b987b7ceSReid Spencer int main() { 52d24df245SChris Lattner 53d24df245SChris Lattner InitializeNativeTarget(); 54d24df245SChris Lattner 556773d388SOwen Anderson LLVMContext Context; 566773d388SOwen Anderson 57b987b7ceSReid Spencer // Create some module to put our function into it. 581cf085d5SOwen Anderson Module *M = new Module("test", Context); 59b987b7ceSReid Spencer 60b987b7ceSReid Spencer // Create the add1 function entry and insert this entry into module M. The 61b987b7ceSReid Spencer // function will have a return type of "int" and take an argument of "int". 62b987b7ceSReid Spencer // The '0' terminates the list of argument types. 63b800b392SChris Lattner Function *Add1F = 64b800b392SChris Lattner cast<Function>(M->getOrInsertFunction("add1", Type::Int32Ty, Type::Int32Ty, 65b800b392SChris Lattner (Type *)0)); 66b987b7ceSReid Spencer 67b987b7ceSReid Spencer // Add a basic block to the function. As before, it automatically inserts 68b987b7ceSReid Spencer // because of the last argument. 69e9ecc68dSGabor Greif BasicBlock *BB = BasicBlock::Create("EntryBlock", Add1F); 70b987b7ceSReid Spencer 71b987b7ceSReid Spencer // Get pointers to the constant `1'. 72*b6b25300SOwen Anderson Value *One = Context.getConstantInt(Type::Int32Ty, 1); 73b987b7ceSReid Spencer 74b987b7ceSReid Spencer // Get pointers to the integer argument of the add1 function... 75222dcd44SAlkis Evlogimenos assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg 76222dcd44SAlkis Evlogimenos Argument *ArgX = Add1F->arg_begin(); // Get the arg 77b987b7ceSReid Spencer ArgX->setName("AnArg"); // Give it a nice symbolic name for fun. 78b987b7ceSReid Spencer 79b987b7ceSReid Spencer // Create the add instruction, inserting it into the end of BB. 80e1f6e4b2SGabor Greif Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB); 81b987b7ceSReid Spencer 82b987b7ceSReid Spencer // Create the return instruction and add it to the basic block 83e9ecc68dSGabor Greif ReturnInst::Create(Add, BB); 84b987b7ceSReid Spencer 85b987b7ceSReid Spencer // Now, function add1 is ready. 86b987b7ceSReid Spencer 87b987b7ceSReid Spencer 88b987b7ceSReid Spencer // Now we going to create function `foo', which returns an int and takes no 89b987b7ceSReid Spencer // arguments. 90b800b392SChris Lattner Function *FooF = 91b800b392SChris Lattner cast<Function>(M->getOrInsertFunction("foo", Type::Int32Ty, (Type *)0)); 92b987b7ceSReid Spencer 93b987b7ceSReid Spencer // Add a basic block to the FooF function. 94e9ecc68dSGabor Greif BB = BasicBlock::Create("EntryBlock", FooF); 95b987b7ceSReid Spencer 96b987b7ceSReid Spencer // Get pointers to the constant `10'. 97*b6b25300SOwen Anderson Value *Ten = Context.getConstantInt(Type::Int32Ty, 10); 98b987b7ceSReid Spencer 99b987b7ceSReid Spencer // Pass Ten to the call call: 100e9ecc68dSGabor Greif CallInst *Add1CallRes = CallInst::Create(Add1F, Ten, "add1", BB); 101eaf625dcSChris Lattner Add1CallRes->setTailCall(true); 102b987b7ceSReid Spencer 103b987b7ceSReid Spencer // Create the return instruction and add it to the basic block. 104e9ecc68dSGabor Greif ReturnInst::Create(Add1CallRes, BB); 105b987b7ceSReid Spencer 106b987b7ceSReid Spencer // Now we create the JIT. 107b987b7ceSReid Spencer ExistingModuleProvider* MP = new ExistingModuleProvider(M); 108b987b7ceSReid Spencer ExecutionEngine* EE = ExecutionEngine::create(MP, false); 109b987b7ceSReid Spencer 1100c19df48SChris Lattner outs() << "We just constructed this LLVM module:\n\n" << *M; 1110c19df48SChris Lattner outs() << "\n\nRunning foo: "; 1120c19df48SChris Lattner outs().flush(); 113b987b7ceSReid Spencer 114b987b7ceSReid Spencer // Call the `foo' function with no arguments: 115b987b7ceSReid Spencer std::vector<GenericValue> noargs; 116b987b7ceSReid Spencer GenericValue gv = EE->runFunction(FooF, noargs); 117b987b7ceSReid Spencer 118b987b7ceSReid Spencer // Import result of execution: 1190c19df48SChris Lattner outs() << "Result: " << gv.IntVal << "\n"; 12073312b3fSTorok Edwin EE->freeMachineCodeForFunction(FooF); 12173312b3fSTorok Edwin delete EE; 12273312b3fSTorok Edwin llvm_shutdown(); 123b987b7ceSReid Spencer return 0; 124b987b7ceSReid Spencer } 125