blob: 2fb2b5e872d1c037709f26111d67ac2b2016faf7 [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//
Chris Lattnerfc001bb2007-12-29 20:37:57 +00005// This file is distributed under the University of Illinois Open Source
6// 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
Garrison Vennf9fba302010-02-03 12:00:02 +000015// the LLVM module consisting of two functions as follow:
Reid Spencer26a4ba72004-08-10 19:14:36 +000016//
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
Owen Anderson8b477ed2009-07-01 16:58:40 +000037#include "llvm/LLVMContext.h"
Chris Lattnere8bf58c2004-08-15 23:21:54 +000038#include "llvm/Module.h"
39#include "llvm/Constants.h"
Reid Spencerbc0895a2007-01-19 22:45:05 +000040#include "llvm/DerivedTypes.h"
Chris Lattnere8bf58c2004-08-15 23:21:54 +000041#include "llvm/Instructions.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 Lattnerda062882009-06-17 16:48:44 +000045#include "llvm/Target/TargetSelect.h"
Torok Edwineb55f3e2009-04-07 17:23:02 +000046#include "llvm/Support/ManagedStatic.h"
Chris Lattner944fac72008-08-23 22:23:09 +000047#include "llvm/Support/raw_ostream.h"
Eric Christopherc3a627d2011-06-09 05:58:50 +000048#include "llvm/Support/IRBuilder.h"
49
Reid Spencer26a4ba72004-08-10 19:14:36 +000050using namespace llvm;
51
52int main() {
Chris Lattnerda062882009-06-17 16:48:44 +000053
54 InitializeNativeTarget();
Owen Anderson8b477ed2009-07-01 16:58:40 +000055
56 LLVMContext Context;
Chris Lattnerda062882009-06-17 16:48:44 +000057
Reid Spencer26a4ba72004-08-10 19:14:36 +000058 // Create some module to put our function into it.
Owen Anderson31895e72009-07-01 21:22:36 +000059 Module *M = new Module("test", Context);
Reid Spencer26a4ba72004-08-10 19:14:36 +000060
Chris Lattnere8bf58c2004-08-15 23:21:54 +000061 // 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.
Chris Lattner6a987542007-01-07 07:40:09 +000064 Function *Add1F =
Owen Anderson1d0be152009-08-13 21:58:54 +000065 cast<Function>(M->getOrInsertFunction("add1", Type::getInt32Ty(Context),
66 Type::getInt32Ty(Context),
Chris Lattner6a987542007-01-07 07:40:09 +000067 (Type *)0));
Reid Spencer26a4ba72004-08-10 19:14:36 +000068
Chris Lattnere8bf58c2004-08-15 23:21:54 +000069 // Add a basic block to the function. As before, it automatically inserts
70 // because of the last argument.
Owen Anderson1d0be152009-08-13 21:58:54 +000071 BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", Add1F);
Misha Brukman237cef42005-04-20 16:42:34 +000072
Eric Christopherc3a627d2011-06-09 05:58:50 +000073 // Create a basic block builder with default parameters. The builder will
74 // automatically append instructions to the basic block `BB'.
75 IRBuilder<> builder(BB);
76
Chris Lattnere8bf58c2004-08-15 23:21:54 +000077 // Get pointers to the constant `1'.
Eric Christopherc3a627d2011-06-09 05:58:50 +000078 Value *One = builder.getInt32(1);
Reid Spencer26a4ba72004-08-10 19:14:36 +000079
Chris Lattnere8bf58c2004-08-15 23:21:54 +000080 // Get pointers to the integer argument of the add1 function...
Alkis Evlogimenos1d9f2622005-03-15 07:20:55 +000081 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
82 Argument *ArgX = Add1F->arg_begin(); // Get the arg
Chris Lattnere8bf58c2004-08-15 23:21:54 +000083 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
Reid Spencer26a4ba72004-08-10 19:14:36 +000084
Chris Lattnere8bf58c2004-08-15 23:21:54 +000085 // Create the add instruction, inserting it into the end of BB.
Eric Christopherc3a627d2011-06-09 05:58:50 +000086 Value *Add = builder.CreateAdd(One, ArgX);
Misha Brukman237cef42005-04-20 16:42:34 +000087
Chris Lattnere8bf58c2004-08-15 23:21:54 +000088 // Create the return instruction and add it to the basic block
Eric Christopherc3a627d2011-06-09 05:58:50 +000089 builder.CreateRet(Add);
Reid Spencer26a4ba72004-08-10 19:14:36 +000090
Chris Lattnere8bf58c2004-08-15 23:21:54 +000091 // Now, function add1 is ready.
Reid Spencer26a4ba72004-08-10 19:14:36 +000092
93
Johnny Chene5dfa8f2011-06-09 20:11:46 +000094 // Now we're going to create function `foo', which returns an int and takes no
Chris Lattnere8bf58c2004-08-15 23:21:54 +000095 // arguments.
Chris Lattner6a987542007-01-07 07:40:09 +000096 Function *FooF =
Owen Anderson1d0be152009-08-13 21:58:54 +000097 cast<Function>(M->getOrInsertFunction("foo", Type::getInt32Ty(Context),
98 (Type *)0));
Reid Spencer26a4ba72004-08-10 19:14:36 +000099
Chris Lattnere8bf58c2004-08-15 23:21:54 +0000100 // Add a basic block to the FooF function.
Owen Anderson1d0be152009-08-13 21:58:54 +0000101 BB = BasicBlock::Create(Context, "EntryBlock", FooF);
Chris Lattnere8bf58c2004-08-15 23:21:54 +0000102
Eric Christopherc3a627d2011-06-09 05:58:50 +0000103 // Tell the basic block builder to attach itself to the new basic block
104 builder.SetInsertPoint(BB);
Chris Lattnere8bf58c2004-08-15 23:21:54 +0000105
Eric Christopherc3a627d2011-06-09 05:58:50 +0000106 // Get pointer to the constant `10'.
107 Value *Ten = builder.getInt32(10);
108
109 // Pass Ten to the call to Add1F
110 CallInst *Add1CallRes = builder.CreateCall(Add1F, Ten);
Chris Lattner47968e42005-05-06 05:59:50 +0000111 Add1CallRes->setTailCall(true);
Misha Brukman237cef42005-04-20 16:42:34 +0000112
Chris Lattnere8bf58c2004-08-15 23:21:54 +0000113 // Create the return instruction and add it to the basic block.
Eric Christopherc3a627d2011-06-09 05:58:50 +0000114 builder.CreateRet(Add1CallRes);
Reid Spencer26a4ba72004-08-10 19:14:36 +0000115
Chris Lattnere8bf58c2004-08-15 23:21:54 +0000116 // Now we create the JIT.
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000117 ExecutionEngine* EE = EngineBuilder(M).create();
Chris Lattnere8bf58c2004-08-15 23:21:54 +0000118
Chris Lattner944fac72008-08-23 22:23:09 +0000119 outs() << "We just constructed this LLVM module:\n\n" << *M;
120 outs() << "\n\nRunning foo: ";
121 outs().flush();
Reid Spencer26a4ba72004-08-10 19:14:36 +0000122
123 // Call the `foo' function with no arguments:
124 std::vector<GenericValue> noargs;
125 GenericValue gv = EE->runFunction(FooF, noargs);
126
Chris Lattnere8bf58c2004-08-15 23:21:54 +0000127 // Import result of execution:
Chris Lattner944fac72008-08-23 22:23:09 +0000128 outs() << "Result: " << gv.IntVal << "\n";
Torok Edwineb55f3e2009-04-07 17:23:02 +0000129 EE->freeMachineCodeForFunction(FooF);
130 delete EE;
131 llvm_shutdown();
Reid Spencer26a4ba72004-08-10 19:14:36 +0000132 return 0;
133}