Silence -Wunused-variable in release builds.
[llvm/stm8.git] / examples / HowToUseJIT / HowToUseJIT.cpp
blob2fb2b5e872d1c037709f26111d67ac2b2016faf7
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/LLVMContext.h"
38 #include "llvm/Module.h"
39 #include "llvm/Constants.h"
40 #include "llvm/DerivedTypes.h"
41 #include "llvm/Instructions.h"
42 #include "llvm/ExecutionEngine/JIT.h"
43 #include "llvm/ExecutionEngine/Interpreter.h"
44 #include "llvm/ExecutionEngine/GenericValue.h"
45 #include "llvm/Target/TargetSelect.h"
46 #include "llvm/Support/ManagedStatic.h"
47 #include "llvm/Support/raw_ostream.h"
48 #include "llvm/Support/IRBuilder.h"
50 using namespace llvm;
52 int main() {
54 InitializeNativeTarget();
56 LLVMContext Context;
58 // Create some module to put our function into it.
59 Module *M = new Module("test", Context);
61 // Create the add1 function entry and insert this entry into module M. The
62 // function will have a return type of "int" and take an argument of "int".
63 // The '0' terminates the list of argument types.
64 Function *Add1F =
65 cast<Function>(M->getOrInsertFunction("add1", Type::getInt32Ty(Context),
66 Type::getInt32Ty(Context),
67 (Type *)0));
69 // Add a basic block to the function. As before, it automatically inserts
70 // because of the last argument.
71 BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", Add1F);
73 // Create a basic block builder with default parameters. The builder will
74 // automatically append instructions to the basic block `BB'.
75 IRBuilder<> builder(BB);
77 // Get pointers to the constant `1'.
78 Value *One = builder.getInt32(1);
80 // Get pointers to the integer argument of the add1 function...
81 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
82 Argument *ArgX = Add1F->arg_begin(); // Get the arg
83 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
85 // Create the add instruction, inserting it into the end of BB.
86 Value *Add = builder.CreateAdd(One, ArgX);
88 // Create the return instruction and add it to the basic block
89 builder.CreateRet(Add);
91 // Now, function add1 is ready.
94 // Now we're going to create function `foo', which returns an int and takes no
95 // arguments.
96 Function *FooF =
97 cast<Function>(M->getOrInsertFunction("foo", Type::getInt32Ty(Context),
98 (Type *)0));
100 // Add a basic block to the FooF function.
101 BB = BasicBlock::Create(Context, "EntryBlock", FooF);
103 // Tell the basic block builder to attach itself to the new basic block
104 builder.SetInsertPoint(BB);
106 // Get pointer to the constant `10'.
107 Value *Ten = builder.getInt32(10);
109 // Pass Ten to the call to Add1F
110 CallInst *Add1CallRes = builder.CreateCall(Add1F, Ten);
111 Add1CallRes->setTailCall(true);
113 // Create the return instruction and add it to the basic block.
114 builder.CreateRet(Add1CallRes);
116 // Now we create the JIT.
117 ExecutionEngine* EE = EngineBuilder(M).create();
119 outs() << "We just constructed this LLVM module:\n\n" << *M;
120 outs() << "\n\nRunning foo: ";
121 outs().flush();
123 // Call the `foo' function with no arguments:
124 std::vector<GenericValue> noargs;
125 GenericValue gv = EE->runFunction(FooF, noargs);
127 // Import result of execution:
128 outs() << "Result: " << gv.IntVal << "\n";
129 EE->freeMachineCodeForFunction(FooF);
130 delete EE;
131 llvm_shutdown();
132 return 0;