2004-09-11 22:30:11 +02:00
|
|
|
//===--- examples/Fibonacci/fibonacci.cpp - An example use of the JIT -----===//
|
2005-04-20 18:42:34 +02:00
|
|
|
//
|
2004-08-19 22:10:04 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:37:57 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-20 18:42:34 +02:00
|
|
|
//
|
2004-08-19 22:10:04 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2004-11-03 22:43:03 +01:00
|
|
|
// This small program provides an example of how to build quickly a small module
|
|
|
|
// with function Fibonacci and execute it with the JIT.
|
2004-08-19 22:10:04 +02:00
|
|
|
//
|
2004-11-03 22:43:03 +01:00
|
|
|
// The goal of this snippet is to create in the memory the LLVM module
|
|
|
|
// consisting of one function as follow:
|
2004-08-19 22:10:04 +02:00
|
|
|
//
|
2004-11-03 22:43:03 +01:00
|
|
|
// int fib(int x) {
|
|
|
|
// if(x<=2) return 1;
|
|
|
|
// return fib(x-1)+fib(x-2);
|
|
|
|
// }
|
2005-04-20 18:42:34 +02:00
|
|
|
//
|
2004-11-03 22:43:03 +01:00
|
|
|
// Once we have this, we compile the module via JIT, then execute the `fib'
|
2004-08-19 22:10:04 +02:00
|
|
|
// function and return result to a driver, i.e. to a "host program".
|
|
|
|
//
|
2004-11-03 22:43:03 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
2004-08-19 22:10:04 +02:00
|
|
|
|
2009-07-01 18:58:40 +02:00
|
|
|
#include "llvm/LLVMContext.h"
|
2004-11-03 22:43:03 +01:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/Analysis/Verifier.h"
|
2006-03-24 04:11:31 +01:00
|
|
|
#include "llvm/ExecutionEngine/JIT.h"
|
|
|
|
#include "llvm/ExecutionEngine/Interpreter.h"
|
2004-08-19 22:10:04 +02:00
|
|
|
#include "llvm/ExecutionEngine/GenericValue.h"
|
2008-08-24 00:23:09 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-12-01 02:56:27 +01:00
|
|
|
#include "llvm/Target/TargetSelect.h"
|
2004-08-19 22:10:04 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
2009-07-15 01:09:55 +02:00
|
|
|
static Function *CreateFibFunction(Module *M, LLVMContext &Context) {
|
2004-11-03 22:43:03 +01:00
|
|
|
// Create the fib function and insert it into module M. This function is said
|
|
|
|
// to return an int and take an int parameter.
|
2007-01-07 08:40:09 +01:00
|
|
|
Function *FibF =
|
2009-08-13 23:58:54 +02:00
|
|
|
cast<Function>(M->getOrInsertFunction("fib", Type::getInt32Ty(Context),
|
|
|
|
Type::getInt32Ty(Context),
|
2007-01-07 08:40:09 +01:00
|
|
|
(Type *)0));
|
2005-04-20 18:42:34 +02:00
|
|
|
|
2004-11-03 22:43:03 +01:00
|
|
|
// Add a basic block to the function.
|
2009-08-13 23:58:54 +02:00
|
|
|
BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", FibF);
|
2005-04-20 18:42:34 +02:00
|
|
|
|
2004-11-03 22:43:03 +01:00
|
|
|
// Get pointers to the constants.
|
2009-08-13 23:58:54 +02:00
|
|
|
Value *One = ConstantInt::get(Type::getInt32Ty(Context), 1);
|
|
|
|
Value *Two = ConstantInt::get(Type::getInt32Ty(Context), 2);
|
2004-08-19 22:10:04 +02:00
|
|
|
|
2004-11-03 22:43:03 +01:00
|
|
|
// Get pointer to the integer argument of the add1 function...
|
2005-03-15 08:12:30 +01:00
|
|
|
Argument *ArgX = FibF->arg_begin(); // Get the arg.
|
2004-11-03 22:43:03 +01:00
|
|
|
ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
|
2004-08-19 22:10:04 +02:00
|
|
|
|
2004-11-03 22:43:03 +01:00
|
|
|
// Create the true_block.
|
2009-08-13 23:58:54 +02:00
|
|
|
BasicBlock *RetBB = BasicBlock::Create(Context, "return", FibF);
|
2004-11-03 22:43:03 +01:00
|
|
|
// Create an exit block.
|
2009-08-13 23:58:54 +02:00
|
|
|
BasicBlock* RecurseBB = BasicBlock::Create(Context, "recurse", FibF);
|
2004-08-19 22:10:04 +02:00
|
|
|
|
2008-03-13 04:29:42 +01:00
|
|
|
// Create the "if (arg <= 2) goto exitbb"
|
2009-07-10 01:48:35 +02:00
|
|
|
Value *CondInst = new ICmpInst(*BB, ICmpInst::ICMP_SLE, ArgX, Two, "cond");
|
2008-04-06 22:25:17 +02:00
|
|
|
BranchInst::Create(RetBB, RecurseBB, CondInst, BB);
|
2004-08-19 22:10:04 +02:00
|
|
|
|
2004-11-03 22:43:03 +01:00
|
|
|
// Create: ret int 1
|
2009-08-13 23:58:54 +02:00
|
|
|
ReturnInst::Create(Context, One, RetBB);
|
2005-04-20 18:42:34 +02:00
|
|
|
|
2004-11-03 22:43:03 +01:00
|
|
|
// create fib(x-1)
|
2008-05-16 21:29:10 +02:00
|
|
|
Value *Sub = BinaryOperator::CreateSub(ArgX, One, "arg", RecurseBB);
|
2008-04-06 22:25:17 +02:00
|
|
|
CallInst *CallFibX1 = CallInst::Create(FibF, Sub, "fibx1", RecurseBB);
|
2005-05-06 08:21:59 +02:00
|
|
|
CallFibX1->setTailCall();
|
2005-04-20 18:42:34 +02:00
|
|
|
|
2004-11-03 22:43:03 +01:00
|
|
|
// create fib(x-2)
|
2008-05-16 21:29:10 +02:00
|
|
|
Sub = BinaryOperator::CreateSub(ArgX, Two, "arg", RecurseBB);
|
2008-04-06 22:25:17 +02:00
|
|
|
CallInst *CallFibX2 = CallInst::Create(FibF, Sub, "fibx2", RecurseBB);
|
2005-05-06 08:21:59 +02:00
|
|
|
CallFibX2->setTailCall();
|
2005-05-06 07:59:50 +02:00
|
|
|
|
2004-11-03 22:43:03 +01:00
|
|
|
|
|
|
|
// fib(x-1)+fib(x-2)
|
2008-05-16 21:29:10 +02:00
|
|
|
Value *Sum = BinaryOperator::CreateAdd(CallFibX1, CallFibX2,
|
2004-11-03 22:43:03 +01:00
|
|
|
"addresult", RecurseBB);
|
2005-04-20 18:42:34 +02:00
|
|
|
|
2004-11-03 22:43:03 +01:00
|
|
|
// Create the return instruction and add it to the basic block
|
2009-08-13 23:58:54 +02:00
|
|
|
ReturnInst::Create(Context, Sum, RecurseBB);
|
2004-08-19 22:10:04 +02:00
|
|
|
|
2004-11-03 22:43:03 +01:00
|
|
|
return FibF;
|
|
|
|
}
|
2004-08-19 22:10:04 +02:00
|
|
|
|
|
|
|
|
2004-11-03 22:43:03 +01:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
int n = argc > 1 ? atol(argv[1]) : 24;
|
2004-08-19 22:10:04 +02:00
|
|
|
|
2009-12-01 02:56:27 +01:00
|
|
|
InitializeNativeTarget();
|
2009-07-01 18:58:40 +02:00
|
|
|
LLVMContext Context;
|
|
|
|
|
2004-11-03 22:43:03 +01:00
|
|
|
// Create some module to put our function into it.
|
2010-09-06 01:09:30 +02:00
|
|
|
OwningPtr<Module> M(new Module("test", Context));
|
2004-11-03 22:43:03 +01:00
|
|
|
|
|
|
|
// We are about to create the "fib" function:
|
2010-09-06 01:09:30 +02:00
|
|
|
Function *FibF = CreateFibFunction(M.get(), Context);
|
2004-08-19 22:10:04 +02:00
|
|
|
|
2005-04-20 18:42:34 +02:00
|
|
|
// Now we going to create JIT
|
2009-12-01 02:56:27 +01:00
|
|
|
std::string errStr;
|
2010-09-06 01:09:30 +02:00
|
|
|
ExecutionEngine *EE =
|
|
|
|
EngineBuilder(M.get())
|
|
|
|
.setErrorStr(&errStr)
|
|
|
|
.setEngineKind(EngineKind::JIT)
|
|
|
|
.create();
|
2009-12-01 02:56:27 +01:00
|
|
|
|
|
|
|
if (!EE) {
|
2010-09-06 01:09:30 +02:00
|
|
|
errs() << argv[0] << ": Failed to construct ExecutionEngine: " << errStr
|
|
|
|
<< "\n";
|
2009-12-01 02:56:27 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2004-08-19 22:10:04 +02:00
|
|
|
|
2008-08-24 00:23:09 +02:00
|
|
|
errs() << "verifying... ";
|
2004-08-19 22:10:04 +02:00
|
|
|
if (verifyModule(*M)) {
|
2008-08-24 00:23:09 +02:00
|
|
|
errs() << argv[0] << ": Error constructing function!\n";
|
2004-08-19 22:10:04 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-08-24 00:23:09 +02:00
|
|
|
errs() << "OK\n";
|
|
|
|
errs() << "We just constructed this LLVM module:\n\n---------\n" << *M;
|
|
|
|
errs() << "---------\nstarting fibonacci(" << n << ") with JIT...\n";
|
2004-08-19 22:10:04 +02:00
|
|
|
|
2004-11-05 05:11:40 +01:00
|
|
|
// Call the Fibonacci function with argument n:
|
2004-11-03 22:43:03 +01:00
|
|
|
std::vector<GenericValue> Args(1);
|
2007-03-06 18:24:31 +01:00
|
|
|
Args[0].IntVal = APInt(32, n);
|
2004-11-03 22:43:03 +01:00
|
|
|
GenericValue GV = EE->runFunction(FibF, Args);
|
2004-08-19 22:10:04 +02:00
|
|
|
|
2004-11-03 22:43:03 +01:00
|
|
|
// import result of execution
|
2008-08-24 00:23:09 +02:00
|
|
|
outs() << "Result: " << GV.IntVal << "\n";
|
2010-09-06 01:09:30 +02:00
|
|
|
|
2004-08-19 22:10:04 +02:00
|
|
|
return 0;
|
|
|
|
}
|