1 //===-- examples/HowToUseJIT/HowToUseJIT.cpp - An example use of the JIT --===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Valery A. Khamenya and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 // The goal of this snippet is to create in the memory
15 // the LLVM module consisting of two functions as follow:
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"
49 // Create some module to put our function into it.
50 Module *M = new Module("test");
52 // Create the add1 function entry and insert this entry into module M. The
53 // function will have a return type of "int" and take an argument of "int".
54 // The '0' terminates the list of argument types.
56 cast<Function>(M->getOrInsertFunction("add1", Type::Int32Ty, Type::Int32Ty,
59 // Add a basic block to the function. As before, it automatically inserts
60 // because of the last argument.
61 BasicBlock *BB = new BasicBlock("EntryBlock", Add1F);
63 // Get pointers to the constant `1'.
64 Value *One = ConstantInt::get(Type::Int32Ty, 1);
66 // Get pointers to the integer argument of the add1 function...
67 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
68 Argument *ArgX = Add1F->arg_begin(); // Get the arg
69 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
71 // Create the add instruction, inserting it into the end of BB.
72 Instruction *Add = BinaryOperator::createAdd(One, ArgX, "addresult", BB);
74 // Create the return instruction and add it to the basic block
75 new ReturnInst(Add, BB);
77 // Now, function add1 is ready.
80 // Now we going to create function `foo', which returns an int and takes no
83 cast<Function>(M->getOrInsertFunction("foo", Type::Int32Ty, (Type *)0));
85 // Add a basic block to the FooF function.
86 BB = new BasicBlock("EntryBlock", FooF);
88 // Get pointers to the constant `10'.
89 Value *Ten = ConstantInt::get(Type::Int32Ty, 10);
91 // Pass Ten to the call call:
92 std::vector<Value*> Params;
93 Params.push_back(Ten);
94 CallInst *Add1CallRes = new CallInst(Add1F, Params, "add1", BB);
95 Add1CallRes->setTailCall(true);
97 // Create the return instruction and add it to the basic block.
98 new ReturnInst(Add1CallRes, BB);
100 // Now we create the JIT.
101 ExistingModuleProvider* MP = new ExistingModuleProvider(M);
102 ExecutionEngine* EE = ExecutionEngine::create(MP, false);
104 std::cout << "We just constructed this LLVM module:\n\n" << *M;
105 std::cout << "\n\nRunning foo: " << std::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 std::cout << "Result: " << gv.Int32Val << "\n";