Merge branch 'master' into msp430
[llvm/msp430.git] / examples / HowToUseJIT / HowToUseJIT.cpp
blobb5c6d111914f36cc482efd76bd0ff7fd2addcf83
1 //===-- examples/HowToUseJIT/HowToUseJIT.cpp - An example use of the JIT --===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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.
13 // Goal:
14 // The goal of this snippet is to create in the memory
15 // the LLVM module consisting of two functions as follow:
17 // int add1(int x) {
18 // return x+1;
19 // }
21 // int foo() {
22 // return add1(10);
23 // }
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".
28 // Some remarks and questions:
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?
35 //===----------------------------------------------------------------------===//
37 #include "llvm/Module.h"
38 #include "llvm/Constants.h"
39 #include "llvm/DerivedTypes.h"
40 #include "llvm/Instructions.h"
41 #include "llvm/ModuleProvider.h"
42 #include "llvm/ExecutionEngine/JIT.h"
43 #include "llvm/ExecutionEngine/Interpreter.h"
44 #include "llvm/ExecutionEngine/GenericValue.h"
45 #include "llvm/Support/ManagedStatic.h"
46 #include "llvm/Support/raw_ostream.h"
47 using namespace llvm;
49 int main() {
50 // Create some module to put our function into it.
51 Module *M = new Module("test");
53 // Create the add1 function entry and insert this entry into module M. The
54 // function will have a return type of "int" and take an argument of "int".
55 // The '0' terminates the list of argument types.
56 Function *Add1F =
57 cast<Function>(M->getOrInsertFunction("add1", Type::Int32Ty, Type::Int32Ty,
58 (Type *)0));
60 // Add a basic block to the function. As before, it automatically inserts
61 // because of the last argument.
62 BasicBlock *BB = BasicBlock::Create("EntryBlock", Add1F);
64 // Get pointers to the constant `1'.
65 Value *One = ConstantInt::get(Type::Int32Ty, 1);
67 // Get pointers to the integer argument of the add1 function...
68 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
69 Argument *ArgX = Add1F->arg_begin(); // Get the arg
70 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
72 // Create the add instruction, inserting it into the end of BB.
73 Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB);
75 // Create the return instruction and add it to the basic block
76 ReturnInst::Create(Add, BB);
78 // Now, function add1 is ready.
81 // Now we going to create function `foo', which returns an int and takes no
82 // arguments.
83 Function *FooF =
84 cast<Function>(M->getOrInsertFunction("foo", Type::Int32Ty, (Type *)0));
86 // Add a basic block to the FooF function.
87 BB = BasicBlock::Create("EntryBlock", FooF);
89 // Get pointers to the constant `10'.
90 Value *Ten = ConstantInt::get(Type::Int32Ty, 10);
92 // Pass Ten to the call call:
93 CallInst *Add1CallRes = CallInst::Create(Add1F, Ten, "add1", BB);
94 Add1CallRes->setTailCall(true);
96 // Create the return instruction and add it to the basic block.
97 ReturnInst::Create(Add1CallRes, BB);
99 // Now we create the JIT.
100 ExistingModuleProvider* MP = new ExistingModuleProvider(M);
101 ExecutionEngine* EE = ExecutionEngine::create(MP, false);
103 outs() << "We just constructed this LLVM module:\n\n" << *M;
104 outs() << "\n\nRunning foo: ";
105 outs().flush();
107 // Call the `foo' function with no arguments:
108 std::vector<GenericValue> noargs;
109 GenericValue gv = EE->runFunction(FooF, noargs);
111 // Import result of execution:
112 outs() << "Result: " << gv.IntVal << "\n";
113 EE->freeMachineCodeForFunction(FooF);
114 delete EE;
115 llvm_shutdown();
116 return 0;