blob: a9f10009e1eca9057d4e588525d4abc3815e856f [file] [log] [blame]
Reid Spencer66e7cd02004-09-11 20:30:11 +00001//===-- examples/HowToUseJIT/HowToUseJIT.cpp - An example use of the JIT --===//
Misha Brukman237cef42005-04-20 16:42:34 +00002//
Reid Spencer26a4ba72004-08-10 19:14:36 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerfc001bb2007-12-29 20:37:57 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman237cef42005-04-20 16:42:34 +00007//
Reid Spencer26a4ba72004-08-10 19:14:36 +00008//===----------------------------------------------------------------------===//
9//
Reid Spencer54706d62004-08-10 19:18:51 +000010// This small program provides an example of how to quickly build a small
Misha Brukman237cef42005-04-20 16:42:34 +000011// module with two functions and execute it with the JIT.
12//
13// Goal:
Reid Spencer26a4ba72004-08-10 19:14:36 +000014// 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 Brukman237cef42005-04-20 16:42:34 +000020//
Reid Spencer26a4ba72004-08-10 19:14:36 +000021// int foo() {
22// return add1(10);
23// }
Misha Brukman237cef42005-04-20 16:42:34 +000024//
25// then compile the module via JIT, then execute the `foo'
Reid Spencer26a4ba72004-08-10 19:14:36 +000026// function and return result to a driver, i.e. to a "host program".
Misha Brukman237cef42005-04-20 16:42:34 +000027//
Reid Spencer26a4ba72004-08-10 19:14:36 +000028// Some remarks and questions:
Misha Brukman237cef42005-04-20 16:42:34 +000029//
Reid Spencer26a4ba72004-08-10 19:14:36 +000030// - could we invoke some code using noname functions too?
Misha Brukman237cef42005-04-20 16:42:34 +000031// e.g. evaluate "foo()+foo()" without fears to introduce
Reid Spencer26a4ba72004-08-10 19:14:36 +000032// conflict of temporary function name with some real
33// existing function name?
Misha Brukman237cef42005-04-20 16:42:34 +000034//
Chris Lattnere8bf58c2004-08-15 23:21:54 +000035//===----------------------------------------------------------------------===//
Reid Spencer26a4ba72004-08-10 19:14:36 +000036
Chris Lattnere8bf58c2004-08-15 23:21:54 +000037#include "llvm/Module.h"
38#include "llvm/Constants.h"
Reid Spencerbc0895a2007-01-19 22:45:05 +000039#include "llvm/DerivedTypes.h"
Chris Lattnere8bf58c2004-08-15 23:21:54 +000040#include "llvm/Instructions.h"
41#include "llvm/ModuleProvider.h"
Jeff Cohenafebb442006-03-24 03:11:31 +000042#include "llvm/ExecutionEngine/JIT.h"
43#include "llvm/ExecutionEngine/Interpreter.h"
Reid Spencer26a4ba72004-08-10 19:14:36 +000044#include "llvm/ExecutionEngine/GenericValue.h"
Chris Lattnerda062882009-06-17 16:48:44 +000045#include "llvm/Target/TargetSelect.h"
Torok Edwineb55f3e2009-04-07 17:23:02 +000046#include "llvm/Support/ManagedStatic.h"
Chris Lattner944fac72008-08-23 22:23:09 +000047#include "llvm/Support/raw_ostream.h"
Reid Spencer26a4ba72004-08-10 19:14:36 +000048using namespace llvm;
49
50int main() {
Chris Lattnerda062882009-06-17 16:48:44 +000051
52 InitializeNativeTarget();
53
Reid Spencer26a4ba72004-08-10 19:14:36 +000054 // Create some module to put our function into it.
55 Module *M = new Module("test");
56
Chris Lattnere8bf58c2004-08-15 23:21:54 +000057 // 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.
Chris Lattner6a987542007-01-07 07:40:09 +000060 Function *Add1F =
61 cast<Function>(M->getOrInsertFunction("add1", Type::Int32Ty, Type::Int32Ty,
62 (Type *)0));
Reid Spencer26a4ba72004-08-10 19:14:36 +000063
Chris Lattnere8bf58c2004-08-15 23:21:54 +000064 // Add a basic block to the function. As before, it automatically inserts
65 // because of the last argument.
Gabor Greif051a9502008-04-06 20:25:17 +000066 BasicBlock *BB = BasicBlock::Create("EntryBlock", Add1F);
Misha Brukman237cef42005-04-20 16:42:34 +000067
Chris Lattnere8bf58c2004-08-15 23:21:54 +000068 // Get pointers to the constant `1'.
Reid Spencerdb8d2be2006-12-31 05:50:28 +000069 Value *One = ConstantInt::get(Type::Int32Ty, 1);
Reid Spencer26a4ba72004-08-10 19:14:36 +000070
Chris Lattnere8bf58c2004-08-15 23:21:54 +000071 // Get pointers to the integer argument of the add1 function...
Alkis Evlogimenos1d9f2622005-03-15 07:20:55 +000072 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
73 Argument *ArgX = Add1F->arg_begin(); // Get the arg
Chris Lattnere8bf58c2004-08-15 23:21:54 +000074 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
Reid Spencer26a4ba72004-08-10 19:14:36 +000075
Chris Lattnere8bf58c2004-08-15 23:21:54 +000076 // Create the add instruction, inserting it into the end of BB.
Gabor Greif7cbd8a32008-05-16 19:29:10 +000077 Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB);
Misha Brukman237cef42005-04-20 16:42:34 +000078
Chris Lattnere8bf58c2004-08-15 23:21:54 +000079 // Create the return instruction and add it to the basic block
Gabor Greif051a9502008-04-06 20:25:17 +000080 ReturnInst::Create(Add, BB);
Reid Spencer26a4ba72004-08-10 19:14:36 +000081
Chris Lattnere8bf58c2004-08-15 23:21:54 +000082 // Now, function add1 is ready.
Reid Spencer26a4ba72004-08-10 19:14:36 +000083
84
Chris Lattnere8bf58c2004-08-15 23:21:54 +000085 // Now we going to create function `foo', which returns an int and takes no
86 // arguments.
Chris Lattner6a987542007-01-07 07:40:09 +000087 Function *FooF =
88 cast<Function>(M->getOrInsertFunction("foo", Type::Int32Ty, (Type *)0));
Reid Spencer26a4ba72004-08-10 19:14:36 +000089
Chris Lattnere8bf58c2004-08-15 23:21:54 +000090 // Add a basic block to the FooF function.
Gabor Greif051a9502008-04-06 20:25:17 +000091 BB = BasicBlock::Create("EntryBlock", FooF);
Chris Lattnere8bf58c2004-08-15 23:21:54 +000092
93 // Get pointers to the constant `10'.
Reid Spencerdb8d2be2006-12-31 05:50:28 +000094 Value *Ten = ConstantInt::get(Type::Int32Ty, 10);
Chris Lattnere8bf58c2004-08-15 23:21:54 +000095
96 // Pass Ten to the call call:
Gabor Greif051a9502008-04-06 20:25:17 +000097 CallInst *Add1CallRes = CallInst::Create(Add1F, Ten, "add1", BB);
Chris Lattner47968e42005-05-06 05:59:50 +000098 Add1CallRes->setTailCall(true);
Misha Brukman237cef42005-04-20 16:42:34 +000099
Chris Lattnere8bf58c2004-08-15 23:21:54 +0000100 // Create the return instruction and add it to the basic block.
Gabor Greif051a9502008-04-06 20:25:17 +0000101 ReturnInst::Create(Add1CallRes, BB);
Reid Spencer26a4ba72004-08-10 19:14:36 +0000102
Chris Lattnere8bf58c2004-08-15 23:21:54 +0000103 // Now we create the JIT.
Reid Spencer26a4ba72004-08-10 19:14:36 +0000104 ExistingModuleProvider* MP = new ExistingModuleProvider(M);
Chris Lattner95f114c2004-08-15 23:31:57 +0000105 ExecutionEngine* EE = ExecutionEngine::create(MP, false);
Chris Lattnere8bf58c2004-08-15 23:21:54 +0000106
Chris Lattner944fac72008-08-23 22:23:09 +0000107 outs() << "We just constructed this LLVM module:\n\n" << *M;
108 outs() << "\n\nRunning foo: ";
109 outs().flush();
Reid Spencer26a4ba72004-08-10 19:14:36 +0000110
111 // Call the `foo' function with no arguments:
112 std::vector<GenericValue> noargs;
113 GenericValue gv = EE->runFunction(FooF, noargs);
114
Chris Lattnere8bf58c2004-08-15 23:21:54 +0000115 // Import result of execution:
Chris Lattner944fac72008-08-23 22:23:09 +0000116 outs() << "Result: " << gv.IntVal << "\n";
Torok Edwineb55f3e2009-04-07 17:23:02 +0000117 EE->freeMachineCodeForFunction(FooF);
118 delete EE;
119 llvm_shutdown();
Reid Spencer26a4ba72004-08-10 19:14:36 +0000120 return 0;
121}