2004-09-11 20:30:11 +00:00
|
|
|
//===-- examples/HowToUseJIT/HowToUseJIT.cpp - An example use of the JIT --===//
|
2005-04-20 16:42:34 +00:00
|
|
|
//
|
2004-08-10 19:14:36 +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-10 19:14:36 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2004-08-10 19:18:51 +00:00
|
|
|
// This small program provides an example of how to quickly build a small
|
2005-04-20 16:42:34 +00:00
|
|
|
// module with two functions and execute it with the JIT.
|
|
|
|
//
|
|
|
|
// Goal:
|
2004-08-10 19:14:36 +00:00
|
|
|
// The goal of this snippet is to create in the memory
|
|
|
|
// the LLVM module consisting of two functions as follow:
|
|
|
|
//
|
|
|
|
// int add1(int x) {
|
|
|
|
// return x+1;
|
|
|
|
// }
|
2005-04-20 16:42:34 +00:00
|
|
|
//
|
2004-08-10 19:14:36 +00:00
|
|
|
// int foo() {
|
|
|
|
// return add1(10);
|
|
|
|
// }
|
2005-04-20 16:42:34 +00:00
|
|
|
//
|
|
|
|
// then compile the module via JIT, then execute the `foo'
|
2004-08-10 19:14:36 +00:00
|
|
|
// function and return result to a driver, i.e. to a "host program".
|
2005-04-20 16:42:34 +00:00
|
|
|
//
|
2004-08-10 19:14:36 +00:00
|
|
|
// Some remarks and questions:
|
2005-04-20 16:42:34 +00:00
|
|
|
//
|
2004-08-10 19:14:36 +00:00
|
|
|
// - could we invoke some code using noname functions too?
|
2005-04-20 16:42:34 +00:00
|
|
|
// e.g. evaluate "foo()+foo()" without fears to introduce
|
2004-08-10 19:14:36 +00:00
|
|
|
// conflict of temporary function name with some real
|
|
|
|
// existing function name?
|
2005-04-20 16:42:34 +00:00
|
|
|
//
|
2004-08-15 23:21:54 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2004-08-10 19:14:36 +00:00
|
|
|
|
2004-08-15 23:21:54 +00:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Constants.h"
|
2007-01-19 22:45:05 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2004-08-15 23:21:54 +00:00
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/ModuleProvider.h"
|
2006-03-24 03:11:31 +00:00
|
|
|
#include "llvm/ExecutionEngine/JIT.h"
|
|
|
|
#include "llvm/ExecutionEngine/Interpreter.h"
|
2004-08-10 19:14:36 +00:00
|
|
|
#include "llvm/ExecutionEngine/GenericValue.h"
|
2009-06-17 16:48:44 +00:00
|
|
|
#include "llvm/Target/TargetSelect.h"
|
2009-04-07 17:23:02 +00:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2008-08-23 22:23:09 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2004-08-10 19:14:36 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
int main() {
|
2009-06-17 16:48:44 +00:00
|
|
|
|
|
|
|
InitializeNativeTarget();
|
|
|
|
|
2004-08-10 19:14:36 +00:00
|
|
|
// Create some module to put our function into it.
|
|
|
|
Module *M = new Module("test");
|
|
|
|
|
2004-08-15 23:21:54 +00:00
|
|
|
// Create the add1 function entry and insert this entry into module M. The
|
|
|
|
// function will have a return type of "int" and take an argument of "int".
|
|
|
|
// The '0' terminates the list of argument types.
|
2007-01-07 07:40:09 +00:00
|
|
|
Function *Add1F =
|
|
|
|
cast<Function>(M->getOrInsertFunction("add1", Type::Int32Ty, Type::Int32Ty,
|
|
|
|
(Type *)0));
|
2004-08-10 19:14:36 +00:00
|
|
|
|
2004-08-15 23:21:54 +00:00
|
|
|
// Add a basic block to the function. As before, it automatically inserts
|
|
|
|
// because of the last argument.
|
2008-04-06 20:25:17 +00:00
|
|
|
BasicBlock *BB = BasicBlock::Create("EntryBlock", Add1F);
|
2005-04-20 16:42:34 +00:00
|
|
|
|
2004-08-15 23:21:54 +00:00
|
|
|
// Get pointers to the constant `1'.
|
2006-12-31 05:50:28 +00:00
|
|
|
Value *One = ConstantInt::get(Type::Int32Ty, 1);
|
2004-08-10 19:14:36 +00:00
|
|
|
|
2004-08-15 23:21:54 +00:00
|
|
|
// Get pointers to the integer argument of the add1 function...
|
2005-03-15 07:20:55 +00:00
|
|
|
assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
|
|
|
|
Argument *ArgX = Add1F->arg_begin(); // Get the arg
|
2004-08-15 23:21:54 +00:00
|
|
|
ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
|
2004-08-10 19:14:36 +00:00
|
|
|
|
2004-08-15 23:21:54 +00:00
|
|
|
// Create the add instruction, inserting it into the end of BB.
|
2008-05-16 19:29:10 +00:00
|
|
|
Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB);
|
2005-04-20 16:42:34 +00:00
|
|
|
|
2004-08-15 23:21:54 +00:00
|
|
|
// Create the return instruction and add it to the basic block
|
2008-04-06 20:25:17 +00:00
|
|
|
ReturnInst::Create(Add, BB);
|
2004-08-10 19:14:36 +00:00
|
|
|
|
2004-08-15 23:21:54 +00:00
|
|
|
// Now, function add1 is ready.
|
2004-08-10 19:14:36 +00:00
|
|
|
|
|
|
|
|
2004-08-15 23:21:54 +00:00
|
|
|
// Now we going to create function `foo', which returns an int and takes no
|
|
|
|
// arguments.
|
2007-01-07 07:40:09 +00:00
|
|
|
Function *FooF =
|
|
|
|
cast<Function>(M->getOrInsertFunction("foo", Type::Int32Ty, (Type *)0));
|
2004-08-10 19:14:36 +00:00
|
|
|
|
2004-08-15 23:21:54 +00:00
|
|
|
// Add a basic block to the FooF function.
|
2008-04-06 20:25:17 +00:00
|
|
|
BB = BasicBlock::Create("EntryBlock", FooF);
|
2004-08-10 19:14:36 +00:00
|
|
|
|
2004-08-15 23:21:54 +00:00
|
|
|
// Get pointers to the constant `10'.
|
2006-12-31 05:50:28 +00:00
|
|
|
Value *Ten = ConstantInt::get(Type::Int32Ty, 10);
|
2004-08-10 19:14:36 +00:00
|
|
|
|
2004-08-15 23:21:54 +00:00
|
|
|
// Pass Ten to the call call:
|
2008-04-06 20:25:17 +00:00
|
|
|
CallInst *Add1CallRes = CallInst::Create(Add1F, Ten, "add1", BB);
|
2005-05-06 05:59:50 +00:00
|
|
|
Add1CallRes->setTailCall(true);
|
2005-04-20 16:42:34 +00:00
|
|
|
|
2004-08-15 23:21:54 +00:00
|
|
|
// Create the return instruction and add it to the basic block.
|
2008-04-06 20:25:17 +00:00
|
|
|
ReturnInst::Create(Add1CallRes, BB);
|
2004-08-10 19:14:36 +00:00
|
|
|
|
2004-08-15 23:21:54 +00:00
|
|
|
// Now we create the JIT.
|
2004-08-10 19:14:36 +00:00
|
|
|
ExistingModuleProvider* MP = new ExistingModuleProvider(M);
|
2004-08-15 23:31:57 +00:00
|
|
|
ExecutionEngine* EE = ExecutionEngine::create(MP, false);
|
2004-08-15 23:21:54 +00:00
|
|
|
|
2008-08-23 22:23:09 +00:00
|
|
|
outs() << "We just constructed this LLVM module:\n\n" << *M;
|
|
|
|
outs() << "\n\nRunning foo: ";
|
|
|
|
outs().flush();
|
2004-08-10 19:14:36 +00:00
|
|
|
|
|
|
|
// Call the `foo' function with no arguments:
|
|
|
|
std::vector<GenericValue> noargs;
|
|
|
|
GenericValue gv = EE->runFunction(FooF, noargs);
|
|
|
|
|
2004-08-15 23:21:54 +00:00
|
|
|
// Import result of execution:
|
2008-08-23 22:23:09 +00:00
|
|
|
outs() << "Result: " << gv.IntVal << "\n";
|
2009-04-07 17:23:02 +00:00
|
|
|
EE->freeMachineCodeForFunction(FooF);
|
|
|
|
delete EE;
|
|
|
|
llvm_shutdown();
|
2004-08-10 19:14:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|