blob: 8fb9e5659def04eb3bf61d8a366d0f581438d79e [file] [log] [blame]
Reid Spencer66e7cd02004-09-11 20:30:11 +00001//===-- examples/HowToUseJIT/HowToUseJIT.cpp - An example use of the JIT --===//
Misha Brukman237cef42005-04-20 16:42:34 +00002//
Reid Spencer26a4ba72004-08-10 19:14:36 +00003// 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.
Misha Brukman237cef42005-04-20 16:42:34 +00007//
Reid Spencer26a4ba72004-08-10 19:14:36 +00008//===----------------------------------------------------------------------===//
9//
Reid Spencer54706d62004-08-10 19:18:51 +000010// This small program provides an example of how to quickly build a small
Misha Brukman237cef42005-04-20 16:42:34 +000011// module with two functions and execute it with the JIT.
12//
13// Goal:
Reid Spencer26a4ba72004-08-10 19:14:36 +000014// 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// }
Misha Brukman237cef42005-04-20 16:42:34 +000020//
Reid Spencer26a4ba72004-08-10 19:14:36 +000021// int foo() {
22// return add1(10);
23// }
Misha Brukman237cef42005-04-20 16:42:34 +000024//
25// then compile the module via JIT, then execute the `foo'
Reid Spencer26a4ba72004-08-10 19:14:36 +000026// function and return result to a driver, i.e. to a "host program".
Misha Brukman237cef42005-04-20 16:42:34 +000027//
Reid Spencer26a4ba72004-08-10 19:14:36 +000028// Some remarks and questions:
Misha Brukman237cef42005-04-20 16:42:34 +000029//
Reid Spencer26a4ba72004-08-10 19:14:36 +000030// - could we invoke some code using noname functions too?
Misha Brukman237cef42005-04-20 16:42:34 +000031// e.g. evaluate "foo()+foo()" without fears to introduce
Reid Spencer26a4ba72004-08-10 19:14:36 +000032// conflict of temporary function name with some real
33// existing function name?
Misha Brukman237cef42005-04-20 16:42:34 +000034//
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"
Jeff Cohenafebb442006-03-24 03:11:31 +000042#include "llvm/ExecutionEngine/JIT.h"
43#include "llvm/ExecutionEngine/Interpreter.h"
Reid Spencer26a4ba72004-08-10 19:14:36 +000044#include "llvm/ExecutionEngine/GenericValue.h"
Chris Lattnere8bf58c2004-08-15 23:21:54 +000045#include <iostream>
Reid Spencer26a4ba72004-08-10 19:14:36 +000046using namespace llvm;
47
48int main() {
Reid Spencer26a4ba72004-08-10 19:14:36 +000049 // Create some module to put our function into it.
50 Module *M = new Module("test");
51
Chris Lattnere8bf58c2004-08-15 23:21:54 +000052 // 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.
Jeff Cohen66c5fd62005-10-23 04:37:20 +000055 Function *Add1F = M->getOrInsertFunction("add1", Type::IntTy, Type::IntTy,
56 (Type *)0);
Reid Spencer26a4ba72004-08-10 19:14:36 +000057
Chris Lattnere8bf58c2004-08-15 23:21:54 +000058 // Add a basic block to the function. As before, it automatically inserts
59 // because of the last argument.
60 BasicBlock *BB = new BasicBlock("EntryBlock", Add1F);
Misha Brukman237cef42005-04-20 16:42:34 +000061
Chris Lattnere8bf58c2004-08-15 23:21:54 +000062 // Get pointers to the constant `1'.
63 Value *One = ConstantSInt::get(Type::IntTy, 1);
Reid Spencer26a4ba72004-08-10 19:14:36 +000064
Chris Lattnere8bf58c2004-08-15 23:21:54 +000065 // Get pointers to the integer argument of the add1 function...
Alkis Evlogimenos1d9f2622005-03-15 07:20:55 +000066 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
67 Argument *ArgX = Add1F->arg_begin(); // Get the arg
Chris Lattnere8bf58c2004-08-15 23:21:54 +000068 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
Reid Spencer26a4ba72004-08-10 19:14:36 +000069
Chris Lattnere8bf58c2004-08-15 23:21:54 +000070 // Create the add instruction, inserting it into the end of BB.
71 Instruction *Add = BinaryOperator::createAdd(One, ArgX, "addresult", BB);
Misha Brukman237cef42005-04-20 16:42:34 +000072
Chris Lattnere8bf58c2004-08-15 23:21:54 +000073 // Create the return instruction and add it to the basic block
74 new ReturnInst(Add, BB);
Reid Spencer26a4ba72004-08-10 19:14:36 +000075
Chris Lattnere8bf58c2004-08-15 23:21:54 +000076 // Now, function add1 is ready.
Reid Spencer26a4ba72004-08-10 19:14:36 +000077
78
Chris Lattnere8bf58c2004-08-15 23:21:54 +000079 // Now we going to create function `foo', which returns an int and takes no
80 // arguments.
Jeff Cohen66c5fd62005-10-23 04:37:20 +000081 Function *FooF = M->getOrInsertFunction("foo", Type::IntTy, (Type *)0);
Reid Spencer26a4ba72004-08-10 19:14:36 +000082
Chris Lattnere8bf58c2004-08-15 23:21:54 +000083 // Add a basic block to the FooF function.
84 BB = new BasicBlock("EntryBlock", FooF);
85
86 // Get pointers to the constant `10'.
87 Value *Ten = ConstantSInt::get(Type::IntTy, 10);
88
89 // Pass Ten to the call call:
90 std::vector<Value*> Params;
91 Params.push_back(Ten);
Chris Lattner47968e42005-05-06 05:59:50 +000092 CallInst *Add1CallRes = new CallInst(Add1F, Params, "add1", BB);
93 Add1CallRes->setTailCall(true);
Misha Brukman237cef42005-04-20 16:42:34 +000094
Chris Lattnere8bf58c2004-08-15 23:21:54 +000095 // Create the return instruction and add it to the basic block.
96 new ReturnInst(Add1CallRes, BB);
Reid Spencer26a4ba72004-08-10 19:14:36 +000097
Chris Lattnere8bf58c2004-08-15 23:21:54 +000098 // Now we create the JIT.
Reid Spencer26a4ba72004-08-10 19:14:36 +000099 ExistingModuleProvider* MP = new ExistingModuleProvider(M);
Chris Lattner95f114c2004-08-15 23:31:57 +0000100 ExecutionEngine* EE = ExecutionEngine::create(MP, false);
Chris Lattnere8bf58c2004-08-15 23:21:54 +0000101
102 std::cout << "We just constructed this LLVM module:\n\n" << *M;
103 std::cout << "\n\nRunning foo: " << std::flush;
Reid Spencer26a4ba72004-08-10 19:14:36 +0000104
105 // Call the `foo' function with no arguments:
106 std::vector<GenericValue> noargs;
107 GenericValue gv = EE->runFunction(FooF, noargs);
108
Chris Lattnere8bf58c2004-08-15 23:21:54 +0000109 // Import result of execution:
110 std::cout << "Result: " << gv.IntVal << "\n";
Reid Spencer26a4ba72004-08-10 19:14:36 +0000111 return 0;
112}