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 37605e30e9SChandler Carruth #include "llvm/ExecutionEngine/GenericValue.h" 38605e30e9SChandler Carruth #include "llvm/ExecutionEngine/Interpreter.h" 39*b9fd9ed3SEric Christopher #include "llvm/ExecutionEngine/JIT.h" 40005f27a0SChandler Carruth #include "llvm/IR/Constants.h" 41005f27a0SChandler Carruth #include "llvm/IR/DerivedTypes.h" 42005f27a0SChandler Carruth #include "llvm/IR/IRBuilder.h" 43005f27a0SChandler Carruth #include "llvm/IR/Instructions.h" 44005f27a0SChandler Carruth #include "llvm/IR/LLVMContext.h" 45005f27a0SChandler Carruth #include "llvm/IR/Module.h" 4673312b3fSTorok Edwin #include "llvm/Support/ManagedStatic.h" 47605e30e9SChandler Carruth #include "llvm/Support/TargetSelect.h" 480c19df48SChris Lattner #include "llvm/Support/raw_ostream.h" 498ad343f2SEric Christopher 50b987b7ceSReid Spencer using namespace llvm; 51b987b7ceSReid Spencer 52b987b7ceSReid Spencer int main() { 53d24df245SChris Lattner 54d24df245SChris Lattner InitializeNativeTarget(); 55d24df245SChris Lattner 566773d388SOwen Anderson LLVMContext Context; 576773d388SOwen Anderson 58b987b7ceSReid Spencer // Create some module to put our function into it. 591cf085d5SOwen Anderson Module *M = new Module("test", Context); 60b987b7ceSReid Spencer 61b987b7ceSReid Spencer // Create the add1 function entry and insert this entry into module M. The 62b987b7ceSReid Spencer // function will have a return type of "int" and take an argument of "int". 63b987b7ceSReid Spencer // The '0' terminates the list of argument types. 64b800b392SChris Lattner Function *Add1F = 6555f1c09eSOwen Anderson cast<Function>(M->getOrInsertFunction("add1", Type::getInt32Ty(Context), 6655f1c09eSOwen Anderson Type::getInt32Ty(Context), 67b800b392SChris Lattner (Type *)0)); 68b987b7ceSReid Spencer 69b987b7ceSReid Spencer // Add a basic block to the function. As before, it automatically inserts 70b987b7ceSReid Spencer // because of the last argument. 7155f1c09eSOwen Anderson BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", Add1F); 72b987b7ceSReid Spencer 738ad343f2SEric Christopher // Create a basic block builder with default parameters. The builder will 748ad343f2SEric Christopher // automatically append instructions to the basic block `BB'. 758ad343f2SEric Christopher IRBuilder<> builder(BB); 768ad343f2SEric Christopher 77b987b7ceSReid Spencer // Get pointers to the constant `1'. 788ad343f2SEric Christopher Value *One = builder.getInt32(1); 79b987b7ceSReid Spencer 80b987b7ceSReid Spencer // Get pointers to the integer argument of the add1 function... 81222dcd44SAlkis Evlogimenos assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg 82222dcd44SAlkis Evlogimenos Argument *ArgX = Add1F->arg_begin(); // Get the arg 83b987b7ceSReid Spencer ArgX->setName("AnArg"); // Give it a nice symbolic name for fun. 84b987b7ceSReid Spencer 85b987b7ceSReid Spencer // Create the add instruction, inserting it into the end of BB. 868ad343f2SEric Christopher Value *Add = builder.CreateAdd(One, ArgX); 87b987b7ceSReid Spencer 88b987b7ceSReid Spencer // Create the return instruction and add it to the basic block 898ad343f2SEric Christopher builder.CreateRet(Add); 90b987b7ceSReid Spencer 91b987b7ceSReid Spencer // Now, function add1 is ready. 92b987b7ceSReid Spencer 93b987b7ceSReid Spencer 949e4b6899SJohnny Chen // Now we're going to create function `foo', which returns an int and takes no 95b987b7ceSReid Spencer // arguments. 96b800b392SChris Lattner Function *FooF = 9755f1c09eSOwen Anderson cast<Function>(M->getOrInsertFunction("foo", Type::getInt32Ty(Context), 9855f1c09eSOwen Anderson (Type *)0)); 99b987b7ceSReid Spencer 100b987b7ceSReid Spencer // Add a basic block to the FooF function. 10155f1c09eSOwen Anderson BB = BasicBlock::Create(Context, "EntryBlock", FooF); 102b987b7ceSReid Spencer 1038ad343f2SEric Christopher // Tell the basic block builder to attach itself to the new basic block 1048ad343f2SEric Christopher builder.SetInsertPoint(BB); 105b987b7ceSReid Spencer 1068ad343f2SEric Christopher // Get pointer to the constant `10'. 1078ad343f2SEric Christopher Value *Ten = builder.getInt32(10); 1088ad343f2SEric Christopher 1098ad343f2SEric Christopher // Pass Ten to the call to Add1F 1108ad343f2SEric Christopher CallInst *Add1CallRes = builder.CreateCall(Add1F, Ten); 111eaf625dcSChris Lattner Add1CallRes->setTailCall(true); 112b987b7ceSReid Spencer 113b987b7ceSReid Spencer // Create the return instruction and add it to the basic block. 1148ad343f2SEric Christopher builder.CreateRet(Add1CallRes); 115b987b7ceSReid Spencer 116b987b7ceSReid Spencer // Now we create the JIT. 117fc8a2d5aSReid Kleckner ExecutionEngine* EE = EngineBuilder(M).create(); 118b987b7ceSReid Spencer 1190c19df48SChris Lattner outs() << "We just constructed this LLVM module:\n\n" << *M; 1200c19df48SChris Lattner outs() << "\n\nRunning foo: "; 1210c19df48SChris Lattner outs().flush(); 122b987b7ceSReid Spencer 123b987b7ceSReid Spencer // Call the `foo' function with no arguments: 124b987b7ceSReid Spencer std::vector<GenericValue> noargs; 125b987b7ceSReid Spencer GenericValue gv = EE->runFunction(FooF, noargs); 126b987b7ceSReid Spencer 127b987b7ceSReid Spencer // Import result of execution: 1280c19df48SChris Lattner outs() << "Result: " << gv.IntVal << "\n"; 129*b9fd9ed3SEric Christopher EE->freeMachineCodeForFunction(FooF); 13073312b3fSTorok Edwin delete EE; 13173312b3fSTorok Edwin llvm_shutdown(); 132b987b7ceSReid Spencer return 0; 133b987b7ceSReid Spencer } 134