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