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
|
|
|
|
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/ModuleProvider.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"
|
2004-11-03 22:43:03 +01:00
|
|
|
#include <iostream>
|
2004-08-19 22:10:04 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
2004-11-03 22:43:03 +01: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 08:40:09 +01:00
|
|
|
Function *FibF =
|
|
|
|
cast<Function>(M->getOrInsertFunction("fib", Type::Int32Ty, Type::Int32Ty,
|
|
|
|
(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.
|
2008-04-06 22:25:17 +02:00
|
|
|
BasicBlock *BB = BasicBlock::Create("EntryBlock", FibF);
|
2005-04-20 18:42:34 +02:00
|
|
|
|
2004-11-03 22:43:03 +01:00
|
|
|
// Get pointers to the constants.
|
2006-12-31 06:50:28 +01:00
|
|
|
Value *One = ConstantInt::get(Type::Int32Ty, 1);
|
|
|
|
Value *Two = ConstantInt::get(Type::Int32Ty, 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.
|
2008-04-06 22:25:17 +02:00
|
|
|
BasicBlock *RetBB = BasicBlock::Create("return", FibF);
|
2004-11-03 22:43:03 +01:00
|
|
|
// Create an exit block.
|
2008-04-06 22:25:17 +02:00
|
|
|
BasicBlock* RecurseBB = BasicBlock::Create("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"
|
2006-12-23 07:05:41 +01:00
|
|
|
Value *CondInst = new ICmpInst(ICmpInst::ICMP_SLE, ArgX, Two, "cond", BB);
|
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
|
2008-04-06 22:25:17 +02:00
|
|
|
ReturnInst::Create(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
|
2008-04-06 22:25:17 +02:00
|
|
|
ReturnInst::Create(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
|
|
|
|
2004-11-03 22:43:03 +01:00
|
|
|
// Create some module to put our function into it.
|
|
|
|
Module *M = new Module("test");
|
|
|
|
|
|
|
|
// We are about to create the "fib" function:
|
|
|
|
Function *FibF = CreateFibFunction(M);
|
2004-08-19 22:10:04 +02:00
|
|
|
|
2005-04-20 18:42:34 +02:00
|
|
|
// Now we going to create JIT
|
2004-11-03 22:43:03 +01:00
|
|
|
ExistingModuleProvider *MP = new ExistingModuleProvider(M);
|
|
|
|
ExecutionEngine *EE = ExecutionEngine::create(MP, false);
|
2004-08-19 22:10:04 +02:00
|
|
|
|
2004-11-03 22:43:03 +01:00
|
|
|
std::cerr << "verifying... ";
|
2004-08-19 22:10:04 +02:00
|
|
|
if (verifyModule(*M)) {
|
2004-11-03 22:43:03 +01:00
|
|
|
std::cerr << argv[0] << ": Error constructing function!\n";
|
2004-08-19 22:10:04 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-11-03 22:43:03 +01:00
|
|
|
std::cerr << "OK\n";
|
|
|
|
std::cerr << "We just constructed this LLVM module:\n\n---------\n" << *M;
|
2004-11-05 05:11:40 +01:00
|
|
|
std::cerr << "---------\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
|
2007-08-23 07:15:32 +02:00
|
|
|
std::cout << "Result: " << GV.IntVal.toStringUnsigned(10) << "\n";
|
2004-08-19 22:10:04 +02:00
|
|
|
return 0;
|
|
|
|
}
|