blob: 8e3b6dc4a2733c8c41b9eefa2efc52e596deceb7 [file] [log] [blame]
Reid Spencer987319d2004-09-11 20:30:11 +00001//===-- examples/HowToUseJIT/HowToUseJIT.cpp - An example use of the JIT --===//
Misha Brukman2f72baf2005-04-20 16:42:34 +00002//
Reid Spencerb8773e32004-08-10 19:14:36 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerbcf65db2007-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 Brukman2f72baf2005-04-20 16:42:34 +00007//
Reid Spencerb8773e32004-08-10 19:14:36 +00008//===----------------------------------------------------------------------===//
9//
Reid Spencere66c3072004-08-10 19:18:51 +000010// This small program provides an example of how to quickly build a small
Misha Brukman2f72baf2005-04-20 16:42:34 +000011// module with two functions and execute it with the JIT.
12//
13// Goal:
Reid Spencerb8773e32004-08-10 19:14:36 +000014// The goal of this snippet is to create in the memory
Garrison Venn8665bcd2010-02-03 12:00:02 +000015// the LLVM module consisting of two functions as follow:
Reid Spencerb8773e32004-08-10 19:14:36 +000016//
17// int add1(int x) {
18// return x+1;
19// }
Misha Brukman2f72baf2005-04-20 16:42:34 +000020//
Reid Spencerb8773e32004-08-10 19:14:36 +000021// int foo() {
22// return add1(10);
23// }
Misha Brukman2f72baf2005-04-20 16:42:34 +000024//
25// then compile the module via JIT, then execute the `foo'
Reid Spencerb8773e32004-08-10 19:14:36 +000026// function and return result to a driver, i.e. to a "host program".
Misha Brukman2f72baf2005-04-20 16:42:34 +000027//
Reid Spencerb8773e32004-08-10 19:14:36 +000028// Some remarks and questions:
Misha Brukman2f72baf2005-04-20 16:42:34 +000029//
Reid Spencerb8773e32004-08-10 19:14:36 +000030// - could we invoke some code using noname functions too?
Misha Brukman2f72baf2005-04-20 16:42:34 +000031// e.g. evaluate "foo()+foo()" without fears to introduce
Reid Spencerb8773e32004-08-10 19:14:36 +000032// conflict of temporary function name with some real
33// existing function name?
Misha Brukman2f72baf2005-04-20 16:42:34 +000034//
Chris Lattnerf56b6922004-08-15 23:21:54 +000035//===----------------------------------------------------------------------===//
Reid Spencerb8773e32004-08-10 19:14:36 +000036
Owen Anderson6773d382009-07-01 16:58:40 +000037#include "llvm/LLVMContext.h"
Chris Lattnerf56b6922004-08-15 23:21:54 +000038#include "llvm/Module.h"
39#include "llvm/Constants.h"
Reid Spencer727f31b2007-01-19 22:45:05 +000040#include "llvm/DerivedTypes.h"
Chris Lattnerf56b6922004-08-15 23:21:54 +000041#include "llvm/Instructions.h"
Jeff Cohene46cbe72006-03-24 03:11:31 +000042#include "llvm/ExecutionEngine/JIT.h"
43#include "llvm/ExecutionEngine/Interpreter.h"
Reid Spencerb8773e32004-08-10 19:14:36 +000044#include "llvm/ExecutionEngine/GenericValue.h"
Chris Lattnerd24df242009-06-17 16:48:44 +000045#include "llvm/Target/TargetSelect.h"
Torok Edwin73312b32009-04-07 17:23:02 +000046#include "llvm/Support/ManagedStatic.h"
Chris Lattner0c19df42008-08-23 22:23:09 +000047#include "llvm/Support/raw_ostream.h"
Reid Spencerb8773e32004-08-10 19:14:36 +000048using namespace llvm;
49
50int main() {
Chris Lattnerd24df242009-06-17 16:48:44 +000051
52 InitializeNativeTarget();
Owen Anderson6773d382009-07-01 16:58:40 +000053
54 LLVMContext Context;
Chris Lattnerd24df242009-06-17 16:48:44 +000055
Reid Spencerb8773e32004-08-10 19:14:36 +000056 // Create some module to put our function into it.
Owen Anderson1cf085d2009-07-01 21:22:36 +000057 Module *M = new Module("test", Context);
Reid Spencerb8773e32004-08-10 19:14:36 +000058
Chris Lattnerf56b6922004-08-15 23:21:54 +000059 // Create the add1 function entry and insert this entry into module M. The
60 // function will have a return type of "int" and take an argument of "int".
61 // The '0' terminates the list of argument types.
Chris Lattnerb800b392007-01-07 07:40:09 +000062 Function *Add1F =
Owen Anderson55f1c092009-08-13 21:58:54 +000063 cast<Function>(M->getOrInsertFunction("add1", Type::getInt32Ty(Context),
64 Type::getInt32Ty(Context),
Chris Lattnerb800b392007-01-07 07:40:09 +000065 (Type *)0));
Reid Spencerb8773e32004-08-10 19:14:36 +000066
Chris Lattnerf56b6922004-08-15 23:21:54 +000067 // Add a basic block to the function. As before, it automatically inserts
68 // because of the last argument.
Owen Anderson55f1c092009-08-13 21:58:54 +000069 BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", Add1F);
Misha Brukman2f72baf2005-04-20 16:42:34 +000070
Chris Lattnerf56b6922004-08-15 23:21:54 +000071 // Get pointers to the constant `1'.
Owen Anderson55f1c092009-08-13 21:58:54 +000072 Value *One = ConstantInt::get(Type::getInt32Ty(Context), 1);
Reid Spencerb8773e32004-08-10 19:14:36 +000073
Chris Lattnerf56b6922004-08-15 23:21:54 +000074 // Get pointers to the integer argument of the add1 function...
Alkis Evlogimenos222dcd42005-03-15 07:20:55 +000075 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
76 Argument *ArgX = Add1F->arg_begin(); // Get the arg
Chris Lattnerf56b6922004-08-15 23:21:54 +000077 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
Reid Spencerb8773e32004-08-10 19:14:36 +000078
Chris Lattnerf56b6922004-08-15 23:21:54 +000079 // Create the add instruction, inserting it into the end of BB.
Gabor Greife1f6e4b2008-05-16 19:29:10 +000080 Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB);
Misha Brukman2f72baf2005-04-20 16:42:34 +000081
Chris Lattnerf56b6922004-08-15 23:21:54 +000082 // Create the return instruction and add it to the basic block
Owen Anderson55f1c092009-08-13 21:58:54 +000083 ReturnInst::Create(Context, Add, BB);
Reid Spencerb8773e32004-08-10 19:14:36 +000084
Chris Lattnerf56b6922004-08-15 23:21:54 +000085 // Now, function add1 is ready.
Reid Spencerb8773e32004-08-10 19:14:36 +000086
87
Chris Lattnerf56b6922004-08-15 23:21:54 +000088 // Now we going to create function `foo', which returns an int and takes no
89 // arguments.
Chris Lattnerb800b392007-01-07 07:40:09 +000090 Function *FooF =
Owen Anderson55f1c092009-08-13 21:58:54 +000091 cast<Function>(M->getOrInsertFunction("foo", Type::getInt32Ty(Context),
92 (Type *)0));
Reid Spencerb8773e32004-08-10 19:14:36 +000093
Chris Lattnerf56b6922004-08-15 23:21:54 +000094 // Add a basic block to the FooF function.
Owen Anderson55f1c092009-08-13 21:58:54 +000095 BB = BasicBlock::Create(Context, "EntryBlock", FooF);
Chris Lattnerf56b6922004-08-15 23:21:54 +000096
97 // Get pointers to the constant `10'.
Owen Anderson55f1c092009-08-13 21:58:54 +000098 Value *Ten = ConstantInt::get(Type::getInt32Ty(Context), 10);
Chris Lattnerf56b6922004-08-15 23:21:54 +000099
100 // Pass Ten to the call call:
Gabor Greife9ecc682008-04-06 20:25:17 +0000101 CallInst *Add1CallRes = CallInst::Create(Add1F, Ten, "add1", BB);
Chris Lattnereaf625d2005-05-06 05:59:50 +0000102 Add1CallRes->setTailCall(true);
Misha Brukman2f72baf2005-04-20 16:42:34 +0000103
Chris Lattnerf56b6922004-08-15 23:21:54 +0000104 // Create the return instruction and add it to the basic block.
Owen Anderson55f1c092009-08-13 21:58:54 +0000105 ReturnInst::Create(Context, Add1CallRes, BB);
Reid Spencerb8773e32004-08-10 19:14:36 +0000106
Chris Lattnerf56b6922004-08-15 23:21:54 +0000107 // Now we create the JIT.
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000108 ExecutionEngine* EE = EngineBuilder(M).create();
Chris Lattnerf56b6922004-08-15 23:21:54 +0000109
Chris Lattner0c19df42008-08-23 22:23:09 +0000110 outs() << "We just constructed this LLVM module:\n\n" << *M;
111 outs() << "\n\nRunning foo: ";
112 outs().flush();
Reid Spencerb8773e32004-08-10 19:14:36 +0000113
114 // Call the `foo' function with no arguments:
115 std::vector<GenericValue> noargs;
116 GenericValue gv = EE->runFunction(FooF, noargs);
117
Chris Lattnerf56b6922004-08-15 23:21:54 +0000118 // Import result of execution:
Chris Lattner0c19df42008-08-23 22:23:09 +0000119 outs() << "Result: " << gv.IntVal << "\n";
Torok Edwin73312b32009-04-07 17:23:02 +0000120 EE->freeMachineCodeForFunction(FooF);
121 delete EE;
122 llvm_shutdown();
Reid Spencerb8773e32004-08-10 19:14:36 +0000123 return 0;
124}