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
|
2010-02-03 12:00:02 +00:00
|
|
|
// the LLVM module consisting of two functions as follow:
|
2004-08-10 19:14:36 +00:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
2012-12-04 10:16:57 +00:00
|
|
|
#include "llvm/ExecutionEngine/GenericValue.h"
|
|
|
|
#include "llvm/ExecutionEngine/Interpreter.h"
|
2013-01-02 11:56:33 +00:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/IRBuilder.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2009-04-07 17:23:02 +00:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2012-12-04 10:16:57 +00:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2008-08-23 22:23:09 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-06-09 05:58:50 +00:00
|
|
|
|
2004-08-10 19:14:36 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
int main() {
|
2009-06-17 16:48:44 +00:00
|
|
|
|
|
|
|
InitializeNativeTarget();
|
2009-07-01 16:58:40 +00:00
|
|
|
|
|
|
|
LLVMContext Context;
|
2009-06-17 16:48:44 +00:00
|
|
|
|
2004-08-10 19:14:36 +00:00
|
|
|
// Create some module to put our function into it.
|
2014-08-19 04:04:25 +00:00
|
|
|
std::unique_ptr<Module> Owner = make_unique<Module>("test", Context);
|
|
|
|
Module *M = Owner.get();
|
2004-08-10 19:14:36 +00:00
|
|
|
|
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 =
|
2009-08-13 21:58:54 +00:00
|
|
|
cast<Function>(M->getOrInsertFunction("add1", Type::getInt32Ty(Context),
|
|
|
|
Type::getInt32Ty(Context),
|
2007-01-07 07:40:09 +00:00
|
|
|
(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.
|
2009-08-13 21:58:54 +00:00
|
|
|
BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", Add1F);
|
2005-04-20 16:42:34 +00:00
|
|
|
|
2011-06-09 05:58:50 +00:00
|
|
|
// Create a basic block builder with default parameters. The builder will
|
|
|
|
// automatically append instructions to the basic block `BB'.
|
|
|
|
IRBuilder<> builder(BB);
|
|
|
|
|
2004-08-15 23:21:54 +00:00
|
|
|
// Get pointers to the constant `1'.
|
2011-06-09 05:58:50 +00:00
|
|
|
Value *One = builder.getInt32(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.
|
2011-06-09 05:58:50 +00:00
|
|
|
Value *Add = builder.CreateAdd(One, ArgX);
|
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
|
2011-06-09 05:58:50 +00:00
|
|
|
builder.CreateRet(Add);
|
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
|
|
|
|
|
|
|
|
2011-06-09 20:11:46 +00:00
|
|
|
// Now we're going to create function `foo', which returns an int and takes no
|
2004-08-15 23:21:54 +00:00
|
|
|
// arguments.
|
2007-01-07 07:40:09 +00:00
|
|
|
Function *FooF =
|
2009-08-13 21:58:54 +00:00
|
|
|
cast<Function>(M->getOrInsertFunction("foo", Type::getInt32Ty(Context),
|
|
|
|
(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.
|
2009-08-13 21:58:54 +00:00
|
|
|
BB = BasicBlock::Create(Context, "EntryBlock", FooF);
|
2004-08-10 19:14:36 +00:00
|
|
|
|
2011-06-09 05:58:50 +00:00
|
|
|
// Tell the basic block builder to attach itself to the new basic block
|
|
|
|
builder.SetInsertPoint(BB);
|
|
|
|
|
|
|
|
// Get pointer to the constant `10'.
|
|
|
|
Value *Ten = builder.getInt32(10);
|
2004-08-10 19:14:36 +00:00
|
|
|
|
2011-06-09 05:58:50 +00:00
|
|
|
// Pass Ten to the call to Add1F
|
|
|
|
CallInst *Add1CallRes = builder.CreateCall(Add1F, Ten);
|
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.
|
2011-06-09 05:58:50 +00:00
|
|
|
builder.CreateRet(Add1CallRes);
|
2004-08-10 19:14:36 +00:00
|
|
|
|
2004-08-15 23:21:54 +00:00
|
|
|
// Now we create the JIT.
|
2014-08-19 04:04:25 +00:00
|
|
|
ExecutionEngine* EE = EngineBuilder(std::move(Owner)).create();
|
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
|
|
|
delete EE;
|
|
|
|
llvm_shutdown();
|
2004-08-10 19:14:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|