blob: 16f4657c62bd1a9fd5f46206a4f319866eb28135 [file] [log] [blame]
Reid Spencer26a4ba72004-08-10 19:14:36 +00001//===--- HowToUseJIT.cpp - An example use of the JIT ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
Reid Spencer54706d62004-08-10 19:18:51 +000010// This small program provides an example of how to quickly build a small
11// module with two functions and execute it with the JIT.
Reid Spencer26a4ba72004-08-10 19:14:36 +000012//
13//===------------------------------------------------------------------------===
14
15// Goal:
16// The goal of this snippet is to create in the memory
17// the LLVM module consisting of two functions as follow:
18//
19// int add1(int x) {
20// return x+1;
21// }
22//
23// int foo() {
24// return add1(10);
25// }
26//
27// then compile the module via JIT, then execute the `foo'
28// function and return result to a driver, i.e. to a "host program".
29//
30// Some remarks and questions:
31//
32// - could we invoke some code using noname functions too?
33// e.g. evaluate "foo()+foo()" without fears to introduce
34// conflict of temporary function name with some real
35// existing function name?
36//
37
38#include <iostream>
39
40#include <llvm/Module.h>
41#include <llvm/DerivedTypes.h>
42#include <llvm/Constants.h>
43#include <llvm/Instructions.h>
44#include <llvm/ModuleProvider.h>
45
46#include "llvm/ExecutionEngine/ExecutionEngine.h"
47#include "llvm/ExecutionEngine/GenericValue.h"
48
49
50using namespace llvm;
51
52int main() {
53
54 // Create some module to put our function into it.
55 Module *M = new Module("test");
56
57
58 // We are about to create the add1 function:
59 Function *Add1F;
60
61 {
62 // first create type for the single argument of add1 function:
63 // the type is 'int ()'
64 std::vector<const Type*> ArgT(1);
65 ArgT[0] = Type::IntTy;
66
67 // now create full type of the add1 function:
68 FunctionType *Add1T = FunctionType::get(Type::IntTy, // type of result
69 ArgT,
70 /*not vararg*/false);
71
72 // Now create the add1 function entry and
73 // insert this entry into module M
74 // (By passing a module as the last parameter to the Function constructor,
75 // it automatically gets appended to the Module.)
76 Add1F = new Function(Add1T,
77 Function::ExternalLinkage, // maybe too much
78 "add1", M);
79
80 // Add a basic block to the function... (again, it automatically inserts
81 // because of the last argument.)
82 BasicBlock *BB = new BasicBlock("EntryBlock of add1 function", Add1F);
83
84 // Get pointers to the constant `1'...
85 Value *One = ConstantSInt::get(Type::IntTy, 1);
86
87 // Get pointers to the integer argument of the add1 function...
88 assert(Add1F->abegin() != Add1F->aend()); // Make sure there's an arg
89 Argument &ArgX = Add1F->afront(); // Get the arg
90
91 // Create the add instruction... does not insert...
92 Instruction *Add = BinaryOperator::create(Instruction::Add, One, &ArgX,
93 "addresult");
94
95 // explicitly insert it into the basic block...
96 BB->getInstList().push_back(Add);
97
98 // Create the return instruction and add it to the basic block
99 BB->getInstList().push_back(new ReturnInst(Add));
100
101 // function add1 is ready
102 }
103
104
105 // now we going to create function `foo':
106 Function *FooF;
107
108 {
109 // Create the foo function type:
110 FunctionType *FooT =
111 FunctionType::get(Type::IntTy, // result has type: 'int ()'
112 std::vector<const Type*>(), // no arguments
113 /*not vararg*/false);
114
115 // create the entry for function `foo' and insert
116 // this entry into module M:
117 FooF =
118 new Function(FooT,
119 Function::ExternalLinkage, // too wide?
120 "foo", M);
121
122 // Add a basic block to the FooF function...
123 BasicBlock *BB = new BasicBlock("EntryBlock of add1 function", FooF);
124
125 // Get pointers to the constant `10'...
126 Value *Ten = ConstantSInt::get(Type::IntTy, 10);
127
128 // Put the argument Ten on stack and make call:
129 // ...
130 std::vector<Value*> Params;
131 Params.push_back(Ten);
132 CallInst * Add1CallRes = new CallInst(Add1F, Params, "add1", BB);
133
134 // Create the return instruction and add it to the basic block
135 BB->getInstList().push_back(new ReturnInst(Add1CallRes));
136
137 }
138
139 // Now we going to create JIT ??
140 ExistingModuleProvider* MP = new ExistingModuleProvider(M);
141 ExecutionEngine* EE = ExecutionEngine::create( MP, true );
142
143 // Call the `foo' function with no arguments:
144 std::vector<GenericValue> noargs;
145 GenericValue gv = EE->runFunction(FooF, noargs);
146
147 // import result of execution:
148 std::cout << "Result: " << gv.IntVal << std:: endl;
149
150 return 0;
151}