blob: 8e3b6dc4a2733c8c41b9eefa2efc52e596deceb7 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- examples/HowToUseJIT/HowToUseJIT.cpp - An example use of the JIT --===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner45ca7c12007-12-29 20:37:57 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This small program provides an example of how to quickly build a small
11// module with two functions and execute it with the JIT.
12//
13// Goal:
14// The goal of this snippet is to create in the memory
Garrison Venndd075b22010-02-03 12:00:02 +000015// the LLVM module consisting of two functions as follow:
Dan Gohmanf17a25c2007-07-18 16:29:46 +000016//
17// int add1(int x) {
18// return x+1;
19// }
20//
21// int foo() {
22// return add1(10);
23// }
24//
25// then compile the module via JIT, then execute the `foo'
26// function and return result to a driver, i.e. to a "host program".
27//
28// Some remarks and questions:
29//
30// - could we invoke some code using noname functions too?
31// e.g. evaluate "foo()+foo()" without fears to introduce
32// conflict of temporary function name with some real
33// existing function name?
34//
35//===----------------------------------------------------------------------===//
36
Owen Anderson25209b42009-07-01 16:58:40 +000037#include "llvm/LLVMContext.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038#include "llvm/Module.h"
39#include "llvm/Constants.h"
40#include "llvm/DerivedTypes.h"
41#include "llvm/Instructions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042#include "llvm/ExecutionEngine/JIT.h"
43#include "llvm/ExecutionEngine/Interpreter.h"
44#include "llvm/ExecutionEngine/GenericValue.h"
Chris Lattner8ed24c52009-06-17 16:48:44 +000045#include "llvm/Target/TargetSelect.h"
Edwin Török29258f72009-04-07 17:23:02 +000046#include "llvm/Support/ManagedStatic.h"
Chris Lattner1fefaac2008-08-23 22:23:09 +000047#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048using namespace llvm;
49
50int main() {
Chris Lattner8ed24c52009-06-17 16:48:44 +000051
52 InitializeNativeTarget();
Owen Anderson25209b42009-07-01 16:58:40 +000053
54 LLVMContext Context;
Chris Lattner8ed24c52009-06-17 16:48:44 +000055
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 // Create some module to put our function into it.
Owen Andersona148fdd2009-07-01 21:22:36 +000057 Module *M = new Module("test", Context);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058
59 // Create the add1 function entry and insert this entry into module M. The
60 // function will have a return type of "int" and take an argument of "int".
61 // The '0' terminates the list of argument types.
62 Function *Add1F =
Owen Anderson35b47072009-08-13 21:58:54 +000063 cast<Function>(M->getOrInsertFunction("add1", Type::getInt32Ty(Context),
64 Type::getInt32Ty(Context),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 (Type *)0));
66
67 // Add a basic block to the function. As before, it automatically inserts
68 // because of the last argument.
Owen Anderson35b47072009-08-13 21:58:54 +000069 BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", Add1F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070
71 // Get pointers to the constant `1'.
Owen Anderson35b47072009-08-13 21:58:54 +000072 Value *One = ConstantInt::get(Type::getInt32Ty(Context), 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073
74 // Get pointers to the integer argument of the add1 function...
75 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
76 Argument *ArgX = Add1F->arg_begin(); // Get the arg
77 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
78
79 // Create the add instruction, inserting it into the end of BB.
Gabor Greifa645dd32008-05-16 19:29:10 +000080 Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081
82 // Create the return instruction and add it to the basic block
Owen Anderson35b47072009-08-13 21:58:54 +000083 ReturnInst::Create(Context, Add, BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084
85 // Now, function add1 is ready.
86
87
88 // Now we going to create function `foo', which returns an int and takes no
89 // arguments.
90 Function *FooF =
Owen Anderson35b47072009-08-13 21:58:54 +000091 cast<Function>(M->getOrInsertFunction("foo", Type::getInt32Ty(Context),
92 (Type *)0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093
94 // Add a basic block to the FooF function.
Owen Anderson35b47072009-08-13 21:58:54 +000095 BB = BasicBlock::Create(Context, "EntryBlock", FooF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096
97 // Get pointers to the constant `10'.
Owen Anderson35b47072009-08-13 21:58:54 +000098 Value *Ten = ConstantInt::get(Type::getInt32Ty(Context), 10);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099
100 // Pass Ten to the call call:
Gabor Greifd6da1d02008-04-06 20:25:17 +0000101 CallInst *Add1CallRes = CallInst::Create(Add1F, Ten, "add1", BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102 Add1CallRes->setTailCall(true);
103
104 // Create the return instruction and add it to the basic block.
Owen Anderson35b47072009-08-13 21:58:54 +0000105 ReturnInst::Create(Context, Add1CallRes, BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106
107 // Now we create the JIT.
Reid Klecknerfa3585b2009-07-18 00:42:18 +0000108 ExecutionEngine* EE = EngineBuilder(M).create();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109
Chris Lattner1fefaac2008-08-23 22:23:09 +0000110 outs() << "We just constructed this LLVM module:\n\n" << *M;
111 outs() << "\n\nRunning foo: ";
112 outs().flush();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113
114 // Call the `foo' function with no arguments:
115 std::vector<GenericValue> noargs;
116 GenericValue gv = EE->runFunction(FooF, noargs);
117
118 // Import result of execution:
Chris Lattner1fefaac2008-08-23 22:23:09 +0000119 outs() << "Result: " << gv.IntVal << "\n";
Edwin Török29258f72009-04-07 17:23:02 +0000120 EE->freeMachineCodeForFunction(FooF);
121 delete EE;
122 llvm_shutdown();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 return 0;
124}