2004-09-12 04:30:11 +08:00
|
|
|
//===-- examples/HowToUseJIT/HowToUseJIT.cpp - An example use of the JIT --===//
|
2005-04-21 00:42:34 +08:00
|
|
|
//
|
2004-08-11 03:14:36 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:37:57 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 00:42:34 +08:00
|
|
|
//
|
2004-08-11 03:14:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2004-08-11 03:18:51 +08:00
|
|
|
// This small program provides an example of how to quickly build a small
|
2005-04-21 00:42:34 +08:00
|
|
|
// module with two functions and execute it with the JIT.
|
|
|
|
//
|
|
|
|
// Goal:
|
2004-08-11 03:14:36 +08:00
|
|
|
// The goal of this snippet is to create in the memory
|
2010-02-03 20:00:02 +08:00
|
|
|
// the LLVM module consisting of two functions as follow:
|
2004-08-11 03:14:36 +08:00
|
|
|
//
|
|
|
|
// int add1(int x) {
|
|
|
|
// return x+1;
|
|
|
|
// }
|
2005-04-21 00:42:34 +08:00
|
|
|
//
|
2004-08-11 03:14:36 +08:00
|
|
|
// int foo() {
|
|
|
|
// return add1(10);
|
|
|
|
// }
|
2005-04-21 00:42:34 +08:00
|
|
|
//
|
|
|
|
// then compile the module via JIT, then execute the `foo'
|
2004-08-11 03:14:36 +08:00
|
|
|
// function and return result to a driver, i.e. to a "host program".
|
2005-04-21 00:42:34 +08:00
|
|
|
//
|
2004-08-11 03:14:36 +08:00
|
|
|
// Some remarks and questions:
|
2005-04-21 00:42:34 +08:00
|
|
|
//
|
2004-08-11 03:14:36 +08:00
|
|
|
// - could we invoke some code using noname functions too?
|
2005-04-21 00:42:34 +08:00
|
|
|
// e.g. evaluate "foo()+foo()" without fears to introduce
|
2004-08-11 03:14:36 +08:00
|
|
|
// conflict of temporary function name with some real
|
|
|
|
// existing function name?
|
2005-04-21 00:42:34 +08:00
|
|
|
//
|
2004-08-16 07:21:54 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2004-08-11 03:14:36 +08:00
|
|
|
|
2015-03-02 09:04:34 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2016-05-25 09:18:36 +08:00
|
|
|
#include "llvm/ExecutionEngine/ExecutionEngine.h"
|
2012-12-04 18:16:57 +08:00
|
|
|
#include "llvm/ExecutionEngine/GenericValue.h"
|
2016-05-25 09:18:36 +08:00
|
|
|
#include "llvm/IR/Argument.h"
|
|
|
|
#include "llvm/IR/BasicBlock.h"
|
2013-01-02 19:56:33 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
2016-05-25 09:18:36 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
2013-01-02 19:56:33 +08:00
|
|
|
#include "llvm/IR/IRBuilder.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2016-05-25 09:18:36 +08:00
|
|
|
#include "llvm/IR/Type.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
2009-04-08 01:23:02 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2012-12-04 18:16:57 +08:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2008-08-24 06:23:09 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2016-05-25 09:18:36 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
2011-06-09 13:58:50 +08:00
|
|
|
|
2004-08-11 03:14:36 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
int main() {
|
2009-06-18 00:48:44 +08:00
|
|
|
InitializeNativeTarget();
|
2009-07-02 00:58:40 +08:00
|
|
|
|
|
|
|
LLVMContext Context;
|
2009-06-18 00:48:44 +08:00
|
|
|
|
2004-08-11 03:14:36 +08:00
|
|
|
// Create some module to put our function into it.
|
2014-08-19 12:04:25 +08:00
|
|
|
std::unique_ptr<Module> Owner = make_unique<Module>("test", Context);
|
|
|
|
Module *M = Owner.get();
|
2004-08-11 03:14:36 +08:00
|
|
|
|
2004-08-16 07:21:54 +08: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 15:40:09 +08:00
|
|
|
Function *Add1F =
|
2009-08-14 05:58:54 +08:00
|
|
|
cast<Function>(M->getOrInsertFunction("add1", Type::getInt32Ty(Context),
|
|
|
|
Type::getInt32Ty(Context),
|
2015-09-30 02:02:48 +08:00
|
|
|
nullptr));
|
2004-08-11 03:14:36 +08:00
|
|
|
|
2004-08-16 07:21:54 +08:00
|
|
|
// Add a basic block to the function. As before, it automatically inserts
|
|
|
|
// because of the last argument.
|
2009-08-14 05:58:54 +08:00
|
|
|
BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", Add1F);
|
2005-04-21 00:42:34 +08:00
|
|
|
|
2011-06-09 13:58:50 +08: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-16 07:21:54 +08:00
|
|
|
// Get pointers to the constant `1'.
|
2011-06-09 13:58:50 +08:00
|
|
|
Value *One = builder.getInt32(1);
|
2004-08-11 03:14:36 +08:00
|
|
|
|
2004-08-16 07:21:54 +08:00
|
|
|
// Get pointers to the integer argument of the add1 function...
|
2005-03-15 15:20:55 +08:00
|
|
|
assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
|
2015-11-07 08:55:46 +08:00
|
|
|
Argument *ArgX = &*Add1F->arg_begin(); // Get the arg
|
2004-08-16 07:21:54 +08:00
|
|
|
ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
|
2004-08-11 03:14:36 +08:00
|
|
|
|
2004-08-16 07:21:54 +08:00
|
|
|
// Create the add instruction, inserting it into the end of BB.
|
2011-06-09 13:58:50 +08:00
|
|
|
Value *Add = builder.CreateAdd(One, ArgX);
|
2005-04-21 00:42:34 +08:00
|
|
|
|
2004-08-16 07:21:54 +08:00
|
|
|
// Create the return instruction and add it to the basic block
|
2011-06-09 13:58:50 +08:00
|
|
|
builder.CreateRet(Add);
|
2004-08-11 03:14:36 +08:00
|
|
|
|
2004-08-16 07:21:54 +08:00
|
|
|
// Now, function add1 is ready.
|
2004-08-11 03:14:36 +08:00
|
|
|
|
2011-06-10 04:11:46 +08:00
|
|
|
// Now we're going to create function `foo', which returns an int and takes no
|
2004-08-16 07:21:54 +08:00
|
|
|
// arguments.
|
2007-01-07 15:40:09 +08:00
|
|
|
Function *FooF =
|
2009-08-14 05:58:54 +08:00
|
|
|
cast<Function>(M->getOrInsertFunction("foo", Type::getInt32Ty(Context),
|
2015-09-30 02:02:48 +08:00
|
|
|
nullptr));
|
2004-08-11 03:14:36 +08:00
|
|
|
|
2004-08-16 07:21:54 +08:00
|
|
|
// Add a basic block to the FooF function.
|
2009-08-14 05:58:54 +08:00
|
|
|
BB = BasicBlock::Create(Context, "EntryBlock", FooF);
|
2004-08-11 03:14:36 +08:00
|
|
|
|
2011-06-09 13:58:50 +08: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-11 03:14:36 +08:00
|
|
|
|
2011-06-09 13:58:50 +08:00
|
|
|
// Pass Ten to the call to Add1F
|
|
|
|
CallInst *Add1CallRes = builder.CreateCall(Add1F, Ten);
|
2005-05-06 13:59:50 +08:00
|
|
|
Add1CallRes->setTailCall(true);
|
2005-04-21 00:42:34 +08:00
|
|
|
|
2004-08-16 07:21:54 +08:00
|
|
|
// Create the return instruction and add it to the basic block.
|
2011-06-09 13:58:50 +08:00
|
|
|
builder.CreateRet(Add1CallRes);
|
2004-08-11 03:14:36 +08:00
|
|
|
|
2004-08-16 07:21:54 +08:00
|
|
|
// Now we create the JIT.
|
2014-08-19 12:04:25 +08:00
|
|
|
ExecutionEngine* EE = EngineBuilder(std::move(Owner)).create();
|
2004-08-16 07:21:54 +08:00
|
|
|
|
2008-08-24 06:23:09 +08:00
|
|
|
outs() << "We just constructed this LLVM module:\n\n" << *M;
|
|
|
|
outs() << "\n\nRunning foo: ";
|
|
|
|
outs().flush();
|
2004-08-11 03:14:36 +08:00
|
|
|
|
|
|
|
// Call the `foo' function with no arguments:
|
|
|
|
std::vector<GenericValue> noargs;
|
|
|
|
GenericValue gv = EE->runFunction(FooF, noargs);
|
|
|
|
|
2004-08-16 07:21:54 +08:00
|
|
|
// Import result of execution:
|
2008-08-24 06:23:09 +08:00
|
|
|
outs() << "Result: " << gv.IntVal << "\n";
|
2009-04-08 01:23:02 +08:00
|
|
|
delete EE;
|
|
|
|
llvm_shutdown();
|
2004-08-11 03:14:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|