blob: 889b34aa95c1f5ca33eeed269f6b893334486a49 [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//
Reid Spencer26a4ba72004-08-10 19:14:36 +000013// Goal:
14// The goal of this snippet is to create in the memory
15// the LLVM module consisting of two functions as follow:
16//
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//
Chris Lattnere8bf58c2004-08-15 23:21:54 +000035//===----------------------------------------------------------------------===//
Reid Spencer26a4ba72004-08-10 19:14:36 +000036
Chris Lattnere8bf58c2004-08-15 23:21:54 +000037#include "llvm/Module.h"
38#include "llvm/Constants.h"
39#include "llvm/Type.h"
40#include "llvm/Instructions.h"
41#include "llvm/ModuleProvider.h"
Reid Spencer26a4ba72004-08-10 19:14:36 +000042#include "llvm/ExecutionEngine/ExecutionEngine.h"
43#include "llvm/ExecutionEngine/GenericValue.h"
Chris Lattnere8bf58c2004-08-15 23:21:54 +000044#include <iostream>
Reid Spencer26a4ba72004-08-10 19:14:36 +000045using namespace llvm;
46
47int main() {
Reid Spencer26a4ba72004-08-10 19:14:36 +000048 // Create some module to put our function into it.
49 Module *M = new Module("test");
50
Chris Lattnere8bf58c2004-08-15 23:21:54 +000051 // Create the add1 function entry and insert this entry into module M. The
52 // function will have a return type of "int" and take an argument of "int".
53 // The '0' terminates the list of argument types.
54 Function *Add1F = M->getOrInsertFunction("add1", Type::IntTy, Type::IntTy, 0);
Reid Spencer26a4ba72004-08-10 19:14:36 +000055
Chris Lattnere8bf58c2004-08-15 23:21:54 +000056 // Add a basic block to the function. As before, it automatically inserts
57 // because of the last argument.
58 BasicBlock *BB = new BasicBlock("EntryBlock", Add1F);
Reid Spencer26a4ba72004-08-10 19:14:36 +000059
Chris Lattnere8bf58c2004-08-15 23:21:54 +000060 // Get pointers to the constant `1'.
61 Value *One = ConstantSInt::get(Type::IntTy, 1);
Reid Spencer26a4ba72004-08-10 19:14:36 +000062
Chris Lattnere8bf58c2004-08-15 23:21:54 +000063 // Get pointers to the integer argument of the add1 function...
64 assert(Add1F->abegin() != Add1F->aend()); // Make sure there's an arg
65 Argument *ArgX = Add1F->abegin(); // Get the arg
66 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
Reid Spencer26a4ba72004-08-10 19:14:36 +000067
Chris Lattnere8bf58c2004-08-15 23:21:54 +000068 // Create the add instruction, inserting it into the end of BB.
69 Instruction *Add = BinaryOperator::createAdd(One, ArgX, "addresult", BB);
Reid Spencer26a4ba72004-08-10 19:14:36 +000070
Chris Lattnere8bf58c2004-08-15 23:21:54 +000071 // Create the return instruction and add it to the basic block
72 new ReturnInst(Add, BB);
Reid Spencer26a4ba72004-08-10 19:14:36 +000073
Chris Lattnere8bf58c2004-08-15 23:21:54 +000074 // Now, function add1 is ready.
Reid Spencer26a4ba72004-08-10 19:14:36 +000075
76
Chris Lattnere8bf58c2004-08-15 23:21:54 +000077 // Now we going to create function `foo', which returns an int and takes no
78 // arguments.
79 Function *FooF = M->getOrInsertFunction("foo", Type::IntTy, 0);
Reid Spencer26a4ba72004-08-10 19:14:36 +000080
Chris Lattnere8bf58c2004-08-15 23:21:54 +000081 // Add a basic block to the FooF function.
82 BB = new BasicBlock("EntryBlock", FooF);
83
84 // Get pointers to the constant `10'.
85 Value *Ten = ConstantSInt::get(Type::IntTy, 10);
86
87 // Pass Ten to the call call:
88 std::vector<Value*> Params;
89 Params.push_back(Ten);
90 CallInst * Add1CallRes = new CallInst(Add1F, Params, "add1", BB);
Reid Spencer26a4ba72004-08-10 19:14:36 +000091
Chris Lattnere8bf58c2004-08-15 23:21:54 +000092 // Create the return instruction and add it to the basic block.
93 new ReturnInst(Add1CallRes, BB);
Reid Spencer26a4ba72004-08-10 19:14:36 +000094
Chris Lattnere8bf58c2004-08-15 23:21:54 +000095 // Now we create the JIT.
Reid Spencer26a4ba72004-08-10 19:14:36 +000096 ExistingModuleProvider* MP = new ExistingModuleProvider(M);
Chris Lattner95f114c2004-08-15 23:31:57 +000097 ExecutionEngine* EE = ExecutionEngine::create(MP, false);
Chris Lattnere8bf58c2004-08-15 23:21:54 +000098
99 std::cout << "We just constructed this LLVM module:\n\n" << *M;
100 std::cout << "\n\nRunning foo: " << std::flush;
Reid Spencer26a4ba72004-08-10 19:14:36 +0000101
102 // Call the `foo' function with no arguments:
103 std::vector<GenericValue> noargs;
104 GenericValue gv = EE->runFunction(FooF, noargs);
105
Chris Lattnere8bf58c2004-08-15 23:21:54 +0000106 // Import result of execution:
107 std::cout << "Result: " << gv.IntVal << "\n";
Reid Spencer26a4ba72004-08-10 19:14:36 +0000108 return 0;
109}