2004-11-22 22:00:25 +00:00
|
|
|
//===-- JITEmitter.cpp - Write machine code to executable memory ----------===//
|
2005-04-21 22:55:34 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 22:55:34 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-12-24 00:01:05 +00:00
|
|
|
//
|
2004-11-20 03:46:14 +00:00
|
|
|
// This file defines a MachineCodeEmitter object that is used by the JIT to
|
|
|
|
// write machine code to memory and remember where relocatable values are.
|
2002-12-24 00:01:05 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-08-05 17:00:32 +00:00
|
|
|
#define DEBUG_TYPE "jit"
|
2003-12-20 01:46:27 +00:00
|
|
|
#include "JIT.h"
|
2008-02-13 18:39:37 +00:00
|
|
|
#include "JITDwarfEmitter.h"
|
2008-08-07 01:30:15 +00:00
|
|
|
#include "llvm/Constants.h"
|
2003-11-30 04:23:21 +00:00
|
|
|
#include "llvm/Module.h"
|
2008-08-07 01:30:15 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2009-05-30 20:51:52 +00:00
|
|
|
#include "llvm/CodeGen/JITCodeEmitter.h"
|
2002-12-24 00:01:05 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2003-01-13 01:00:12 +00:00
|
|
|
#include "llvm/CodeGen/MachineConstantPool.h"
|
2006-04-22 18:53:45 +00:00
|
|
|
#include "llvm/CodeGen/MachineJumpTableInfo.h"
|
2008-02-13 18:39:37 +00:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
2004-11-20 03:46:14 +00:00
|
|
|
#include "llvm/CodeGen/MachineRelocation.h"
|
2008-08-07 01:30:15 +00:00
|
|
|
#include "llvm/ExecutionEngine/GenericValue.h"
|
2009-06-25 02:04:04 +00:00
|
|
|
#include "llvm/ExecutionEngine/JITEventListener.h"
|
|
|
|
#include "llvm/ExecutionEngine/JITMemoryManager.h"
|
2009-05-18 21:06:40 +00:00
|
|
|
#include "llvm/CodeGen/MachineCodeInfo.h"
|
2003-01-13 01:00:12 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2004-11-20 03:46:14 +00:00
|
|
|
#include "llvm/Target/TargetJITInfo.h"
|
2006-12-14 19:17:33 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2008-02-13 18:39:37 +00:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-11 13:10:19 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2006-05-08 22:00:52 +00:00
|
|
|
#include "llvm/Support/MutexGuard.h"
|
2009-04-19 18:32:03 +00:00
|
|
|
#include "llvm/Support/ValueHandle.h"
|
2009-07-11 13:10:19 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2007-01-23 10:26:08 +00:00
|
|
|
#include "llvm/System/Disassembler.h"
|
2008-06-25 17:18:44 +00:00
|
|
|
#include "llvm/System/Memory.h"
|
2008-04-18 20:59:31 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2008-11-07 09:02:17 +00:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2009-02-18 08:31:02 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2005-07-29 23:40:16 +00:00
|
|
|
#include <algorithm>
|
2008-11-05 23:44:08 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
#include <iomanip>
|
|
|
|
#endif
|
2003-12-08 08:06:28 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2006-12-19 22:43:32 +00:00
|
|
|
STATISTIC(NumBytes, "Number of bytes of machine code compiled");
|
|
|
|
STATISTIC(NumRelos, "Number of relocations applied");
|
|
|
|
static JIT *TheJIT = 0;
|
2002-12-24 00:01:05 +00:00
|
|
|
|
2005-07-29 23:40:16 +00:00
|
|
|
|
2004-11-20 23:57:07 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// JIT lazy compilation code.
|
|
|
|
//
|
|
|
|
namespace {
|
2005-07-12 15:51:55 +00:00
|
|
|
class JITResolverState {
|
2009-04-27 05:09:44 +00:00
|
|
|
public:
|
|
|
|
typedef std::map<AssertingVH<Function>, void*> FunctionToStubMapTy;
|
|
|
|
typedef std::map<void*, Function*> StubToFunctionMapTy;
|
|
|
|
typedef std::map<AssertingVH<GlobalValue>, void*> GlobalToIndirectSymMapTy;
|
2005-07-12 15:51:55 +00:00
|
|
|
private:
|
|
|
|
/// FunctionToStubMap - Keep track of the stub created for a particular
|
|
|
|
/// function so that we can reuse them if necessary.
|
2009-04-27 05:09:44 +00:00
|
|
|
FunctionToStubMapTy FunctionToStubMap;
|
2005-07-12 15:51:55 +00:00
|
|
|
|
|
|
|
/// StubToFunctionMap - Keep track of the function that each stub
|
|
|
|
/// corresponds to.
|
2009-04-27 05:09:44 +00:00
|
|
|
StubToFunctionMapTy StubToFunctionMap;
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2008-11-10 01:52:24 +00:00
|
|
|
/// GlobalToIndirectSymMap - Keep track of the indirect symbol created for a
|
2008-01-04 10:46:51 +00:00
|
|
|
/// particular GlobalVariable so that we can reuse them if necessary.
|
2009-04-27 05:09:44 +00:00
|
|
|
GlobalToIndirectSymMapTy GlobalToIndirectSymMap;
|
2008-01-04 10:46:51 +00:00
|
|
|
|
2005-07-12 15:51:55 +00:00
|
|
|
public:
|
2009-04-27 05:09:44 +00:00
|
|
|
FunctionToStubMapTy& getFunctionToStubMap(const MutexGuard& locked) {
|
2005-07-12 15:51:55 +00:00
|
|
|
assert(locked.holds(TheJIT->lock));
|
|
|
|
return FunctionToStubMap;
|
|
|
|
}
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2009-04-27 05:09:44 +00:00
|
|
|
StubToFunctionMapTy& getStubToFunctionMap(const MutexGuard& locked) {
|
2005-07-12 15:51:55 +00:00
|
|
|
assert(locked.holds(TheJIT->lock));
|
|
|
|
return StubToFunctionMap;
|
|
|
|
}
|
2008-01-04 10:46:51 +00:00
|
|
|
|
2009-04-27 05:09:44 +00:00
|
|
|
GlobalToIndirectSymMapTy& getGlobalToIndirectSymMap(const MutexGuard& locked) {
|
2008-01-04 10:46:51 +00:00
|
|
|
assert(locked.holds(TheJIT->lock));
|
2008-11-10 01:52:24 +00:00
|
|
|
return GlobalToIndirectSymMap;
|
2008-01-04 10:46:51 +00:00
|
|
|
}
|
2005-07-12 15:51:55 +00:00
|
|
|
};
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2004-11-20 23:57:07 +00:00
|
|
|
/// JITResolver - Keep track of, and resolve, call sites for functions that
|
|
|
|
/// have not yet been compiled.
|
|
|
|
class JITResolver {
|
2009-04-27 05:09:44 +00:00
|
|
|
typedef JITResolverState::FunctionToStubMapTy FunctionToStubMapTy;
|
|
|
|
typedef JITResolverState::StubToFunctionMapTy StubToFunctionMapTy;
|
|
|
|
typedef JITResolverState::GlobalToIndirectSymMapTy GlobalToIndirectSymMapTy;
|
|
|
|
|
2004-11-21 03:37:42 +00:00
|
|
|
/// LazyResolverFn - The target lazy resolver function that we actually
|
|
|
|
/// rewrite instructions to use.
|
|
|
|
TargetJITInfo::LazyResolverFn LazyResolverFn;
|
|
|
|
|
2005-07-12 15:51:55 +00:00
|
|
|
JITResolverState state;
|
2004-11-20 23:57:07 +00:00
|
|
|
|
2005-04-18 01:44:27 +00:00
|
|
|
/// ExternalFnToStubMap - This is the equivalent of FunctionToStubMap for
|
|
|
|
/// external functions.
|
|
|
|
std::map<void*, void*> ExternalFnToStubMap;
|
2005-07-28 12:44:13 +00:00
|
|
|
|
2009-03-13 07:51:59 +00:00
|
|
|
/// revGOTMap - map addresses to indexes in the GOT
|
2005-07-28 12:44:13 +00:00
|
|
|
std::map<void*, unsigned> revGOTMap;
|
|
|
|
unsigned nextGOTIndex;
|
|
|
|
|
2007-02-24 02:57:03 +00:00
|
|
|
static JITResolver *TheJITResolver;
|
2004-11-20 23:57:07 +00:00
|
|
|
public:
|
2008-03-25 22:06:05 +00:00
|
|
|
explicit JITResolver(JIT &jit) : nextGOTIndex(0) {
|
2007-02-24 02:57:03 +00:00
|
|
|
TheJIT = &jit;
|
|
|
|
|
|
|
|
LazyResolverFn = jit.getJITInfo().getLazyResolverFunction(JITCompilerFn);
|
|
|
|
assert(TheJITResolver == 0 && "Multiple JIT resolvers?");
|
|
|
|
TheJITResolver = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
~JITResolver() {
|
|
|
|
TheJITResolver = 0;
|
2004-11-21 03:37:42 +00:00
|
|
|
}
|
2004-11-20 23:57:07 +00:00
|
|
|
|
2008-11-13 21:50:50 +00:00
|
|
|
/// getFunctionStubIfAvailable - This returns a pointer to a function stub
|
|
|
|
/// if it has already been created.
|
|
|
|
void *getFunctionStubIfAvailable(Function *F);
|
|
|
|
|
2004-11-20 23:57:07 +00:00
|
|
|
/// getFunctionStub - This returns a pointer to a function stub, creating
|
2009-02-18 08:31:02 +00:00
|
|
|
/// one on demand as needed. If empty is true, create a function stub
|
|
|
|
/// pointing at address 0, to be filled in later.
|
2009-03-07 06:41:19 +00:00
|
|
|
void *getFunctionStub(Function *F);
|
2004-11-20 23:57:07 +00:00
|
|
|
|
2005-04-18 01:44:27 +00:00
|
|
|
/// getExternalFunctionStub - Return a stub for the function at the
|
|
|
|
/// specified address, created lazily on demand.
|
|
|
|
void *getExternalFunctionStub(void *FnAddr);
|
|
|
|
|
2008-11-10 01:52:24 +00:00
|
|
|
/// getGlobalValueIndirectSym - Return an indirect symbol containing the
|
2008-11-05 01:50:32 +00:00
|
|
|
/// specified GV address.
|
2008-11-10 01:52:24 +00:00
|
|
|
void *getGlobalValueIndirectSym(GlobalValue *V, void *GVAddress);
|
2008-01-04 10:46:51 +00:00
|
|
|
|
2004-11-21 03:37:42 +00:00
|
|
|
/// AddCallbackAtLocation - If the target is capable of rewriting an
|
|
|
|
/// instruction without the use of a stub, record the location of the use so
|
|
|
|
/// we know which function is being used at the location.
|
|
|
|
void *AddCallbackAtLocation(Function *F, void *Location) {
|
2005-07-12 15:51:55 +00:00
|
|
|
MutexGuard locked(TheJIT->lock);
|
2004-11-21 03:37:42 +00:00
|
|
|
/// Get the target-specific JIT resolver function.
|
2005-07-12 15:51:55 +00:00
|
|
|
state.getStubToFunctionMap(locked)[Location] = F;
|
2006-06-01 17:29:22 +00:00
|
|
|
return (void*)(intptr_t)LazyResolverFn;
|
2004-11-21 03:37:42 +00:00
|
|
|
}
|
2009-02-18 08:31:02 +00:00
|
|
|
|
|
|
|
void getRelocatableGVs(SmallVectorImpl<GlobalValue*> &GVs,
|
2009-03-05 06:34:37 +00:00
|
|
|
SmallVectorImpl<void*> &Ptrs);
|
|
|
|
|
|
|
|
GlobalValue *invalidateStub(void *Stub);
|
2004-11-21 03:37:42 +00:00
|
|
|
|
2005-07-28 12:44:13 +00:00
|
|
|
/// getGOTIndexForAddress - Return a new or existing index in the GOT for
|
2007-12-05 23:39:57 +00:00
|
|
|
/// an address. This function only manages slots, it does not manage the
|
2005-07-28 12:44:13 +00:00
|
|
|
/// contents of the slots or the memory associated with the GOT.
|
2007-12-05 23:39:57 +00:00
|
|
|
unsigned getGOTIndexForAddr(void *addr);
|
2005-07-28 12:44:13 +00:00
|
|
|
|
2004-11-20 23:57:07 +00:00
|
|
|
/// JITCompilerFn - This function is called to resolve a stub to a compiled
|
|
|
|
/// address. If the LLVM Function corresponding to the stub has not yet
|
|
|
|
/// been compiled, this function compiles it first.
|
|
|
|
static void *JITCompilerFn(void *Stub);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2007-02-24 02:57:03 +00:00
|
|
|
JITResolver *JITResolver::TheJITResolver = 0;
|
2004-11-20 23:57:07 +00:00
|
|
|
|
2008-11-13 21:50:50 +00:00
|
|
|
/// getFunctionStubIfAvailable - This returns a pointer to a function stub
|
|
|
|
/// if it has already been created.
|
|
|
|
void *JITResolver::getFunctionStubIfAvailable(Function *F) {
|
|
|
|
MutexGuard locked(TheJIT->lock);
|
|
|
|
|
|
|
|
// If we already have a stub for this function, recycle it.
|
|
|
|
void *&Stub = state.getFunctionToStubMap(locked)[F];
|
|
|
|
return Stub;
|
|
|
|
}
|
|
|
|
|
2004-11-20 23:57:07 +00:00
|
|
|
/// getFunctionStub - This returns a pointer to a function stub, creating
|
|
|
|
/// one on demand as needed.
|
2009-03-07 06:41:19 +00:00
|
|
|
void *JITResolver::getFunctionStub(Function *F) {
|
2005-07-12 15:51:55 +00:00
|
|
|
MutexGuard locked(TheJIT->lock);
|
|
|
|
|
2004-11-20 23:57:07 +00:00
|
|
|
// If we already have a stub for this function, recycle it.
|
2005-07-12 15:51:55 +00:00
|
|
|
void *&Stub = state.getFunctionToStubMap(locked)[F];
|
2004-11-20 23:57:07 +00:00
|
|
|
if (Stub) return Stub;
|
|
|
|
|
2009-03-07 06:41:19 +00:00
|
|
|
// Call the lazy resolver function unless we are JIT'ing non-lazily, in which
|
|
|
|
// case we must resolve the symbol now.
|
|
|
|
void *Actual = TheJIT->isLazyCompilationDisabled()
|
|
|
|
? (void *)0 : (void *)(intptr_t)LazyResolverFn;
|
|
|
|
|
|
|
|
// If this is an external declaration, attempt to resolve the address now
|
|
|
|
// to place in the stub.
|
2009-01-05 05:32:42 +00:00
|
|
|
if (F->isDeclaration() && !F->hasNotBeenReadFromBitcode()) {
|
2004-11-22 07:24:43 +00:00
|
|
|
Actual = TheJIT->getPointerToFunction(F);
|
2005-04-21 22:55:34 +00:00
|
|
|
|
2009-01-05 05:32:42 +00:00
|
|
|
// If we resolved the symbol to a null address (eg. a weak external)
|
2009-03-07 06:41:19 +00:00
|
|
|
// don't emit a stub. Return a null pointer to the application. If dlsym
|
|
|
|
// stubs are enabled, not being able to resolve the address is not
|
|
|
|
// meaningful.
|
|
|
|
if (!Actual && !TheJIT->areDlsymStubsEnabled()) return 0;
|
2009-01-05 05:32:42 +00:00
|
|
|
}
|
|
|
|
|
2009-03-07 06:41:19 +00:00
|
|
|
// Codegen a new stub, calling the lazy resolver or the actual address of the
|
|
|
|
// external function, if it was resolved.
|
2008-04-16 20:46:05 +00:00
|
|
|
Stub = TheJIT->getJITInfo().emitFunctionStub(F, Actual,
|
2007-02-24 02:57:03 +00:00
|
|
|
*TheJIT->getCodeEmitter());
|
2004-11-22 07:24:43 +00:00
|
|
|
|
2006-06-01 17:29:22 +00:00
|
|
|
if (Actual != (void*)(intptr_t)LazyResolverFn) {
|
2004-11-22 07:24:43 +00:00
|
|
|
// If we are getting the stub for an external function, we really want the
|
|
|
|
// address of the stub in the GlobalAddressMap for the JIT, not the address
|
|
|
|
// of the external function.
|
|
|
|
TheJIT->updateGlobalMapping(F, Stub);
|
|
|
|
}
|
2004-11-20 23:57:07 +00:00
|
|
|
|
2009-03-05 06:48:16 +00:00
|
|
|
DOUT << "JIT: Stub emitted at [" << Stub << "] for function '"
|
2006-12-07 20:04:42 +00:00
|
|
|
<< F->getName() << "'\n";
|
2004-11-21 03:44:32 +00:00
|
|
|
|
2004-11-20 23:57:07 +00:00
|
|
|
// Finally, keep track of the stub-to-Function mapping so that the
|
|
|
|
// JITCompilerFn knows which function to compile!
|
2005-07-12 15:51:55 +00:00
|
|
|
state.getStubToFunctionMap(locked)[Stub] = F;
|
2009-02-18 08:31:02 +00:00
|
|
|
|
2009-03-07 06:41:19 +00:00
|
|
|
// If we are JIT'ing non-lazily but need to call a function that does not
|
|
|
|
// exist yet, add it to the JIT's work list so that we can fill in the stub
|
|
|
|
// address later.
|
|
|
|
if (!Actual && TheJIT->isLazyCompilationDisabled())
|
|
|
|
if (!F->isDeclaration() || F->hasNotBeenReadFromBitcode())
|
|
|
|
TheJIT->addPendingFunction(F);
|
2009-02-18 08:31:02 +00:00
|
|
|
|
2004-11-20 23:57:07 +00:00
|
|
|
return Stub;
|
|
|
|
}
|
|
|
|
|
2008-11-10 01:52:24 +00:00
|
|
|
/// getGlobalValueIndirectSym - Return a lazy pointer containing the specified
|
2008-01-04 10:46:51 +00:00
|
|
|
/// GV address.
|
2008-11-10 01:52:24 +00:00
|
|
|
void *JITResolver::getGlobalValueIndirectSym(GlobalValue *GV, void *GVAddress) {
|
2008-01-04 10:46:51 +00:00
|
|
|
MutexGuard locked(TheJIT->lock);
|
|
|
|
|
|
|
|
// If we already have a stub for this global variable, recycle it.
|
2008-11-10 01:52:24 +00:00
|
|
|
void *&IndirectSym = state.getGlobalToIndirectSymMap(locked)[GV];
|
|
|
|
if (IndirectSym) return IndirectSym;
|
2008-01-04 10:46:51 +00:00
|
|
|
|
2008-11-10 23:26:16 +00:00
|
|
|
// Otherwise, codegen a new indirect symbol.
|
2008-11-10 01:52:24 +00:00
|
|
|
IndirectSym = TheJIT->getJITInfo().emitGlobalValueIndirectSym(GV, GVAddress,
|
2008-11-05 01:50:32 +00:00
|
|
|
*TheJIT->getCodeEmitter());
|
2008-01-04 10:46:51 +00:00
|
|
|
|
2008-11-10 01:52:24 +00:00
|
|
|
DOUT << "JIT: Indirect symbol emitted at [" << IndirectSym << "] for GV '"
|
2008-01-04 10:46:51 +00:00
|
|
|
<< GV->getName() << "'\n";
|
|
|
|
|
2008-11-10 01:52:24 +00:00
|
|
|
return IndirectSym;
|
2008-01-04 10:46:51 +00:00
|
|
|
}
|
|
|
|
|
2005-04-18 01:44:27 +00:00
|
|
|
/// getExternalFunctionStub - Return a stub for the function at the
|
|
|
|
/// specified address, created lazily on demand.
|
|
|
|
void *JITResolver::getExternalFunctionStub(void *FnAddr) {
|
|
|
|
// If we already have a stub for this function, recycle it.
|
|
|
|
void *&Stub = ExternalFnToStubMap[FnAddr];
|
|
|
|
if (Stub) return Stub;
|
|
|
|
|
2008-04-16 20:46:05 +00:00
|
|
|
Stub = TheJIT->getJITInfo().emitFunctionStub(0, FnAddr,
|
2007-02-24 02:57:03 +00:00
|
|
|
*TheJIT->getCodeEmitter());
|
2006-07-25 20:40:54 +00:00
|
|
|
|
2006-12-07 20:04:42 +00:00
|
|
|
DOUT << "JIT: Stub emitted at [" << Stub
|
|
|
|
<< "] for external function at '" << FnAddr << "'\n";
|
2005-04-18 01:44:27 +00:00
|
|
|
return Stub;
|
|
|
|
}
|
|
|
|
|
2005-07-28 12:44:13 +00:00
|
|
|
unsigned JITResolver::getGOTIndexForAddr(void* addr) {
|
|
|
|
unsigned idx = revGOTMap[addr];
|
|
|
|
if (!idx) {
|
|
|
|
idx = ++nextGOTIndex;
|
|
|
|
revGOTMap[addr] = idx;
|
2008-11-07 22:30:29 +00:00
|
|
|
DOUT << "JIT: Adding GOT entry " << idx << " for addr [" << addr << "]\n";
|
2005-07-28 12:44:13 +00:00
|
|
|
}
|
|
|
|
return idx;
|
|
|
|
}
|
2005-04-18 01:44:27 +00:00
|
|
|
|
2009-02-18 08:31:02 +00:00
|
|
|
void JITResolver::getRelocatableGVs(SmallVectorImpl<GlobalValue*> &GVs,
|
|
|
|
SmallVectorImpl<void*> &Ptrs) {
|
|
|
|
MutexGuard locked(TheJIT->lock);
|
|
|
|
|
2009-04-27 05:09:44 +00:00
|
|
|
FunctionToStubMapTy &FM = state.getFunctionToStubMap(locked);
|
|
|
|
GlobalToIndirectSymMapTy &GM = state.getGlobalToIndirectSymMap(locked);
|
2009-02-18 08:31:02 +00:00
|
|
|
|
2009-04-27 05:09:44 +00:00
|
|
|
for (FunctionToStubMapTy::iterator i = FM.begin(), e = FM.end(); i != e; ++i){
|
2009-02-18 08:31:02 +00:00
|
|
|
Function *F = i->first;
|
|
|
|
if (F->isDeclaration() && F->hasExternalLinkage()) {
|
|
|
|
GVs.push_back(i->first);
|
|
|
|
Ptrs.push_back(i->second);
|
|
|
|
}
|
|
|
|
}
|
2009-04-27 05:09:44 +00:00
|
|
|
for (GlobalToIndirectSymMapTy::iterator i = GM.begin(), e = GM.end();
|
2009-02-18 08:31:02 +00:00
|
|
|
i != e; ++i) {
|
|
|
|
GVs.push_back(i->first);
|
|
|
|
Ptrs.push_back(i->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-05 06:34:37 +00:00
|
|
|
GlobalValue *JITResolver::invalidateStub(void *Stub) {
|
|
|
|
MutexGuard locked(TheJIT->lock);
|
|
|
|
|
2009-04-27 05:09:44 +00:00
|
|
|
FunctionToStubMapTy &FM = state.getFunctionToStubMap(locked);
|
|
|
|
StubToFunctionMapTy &SM = state.getStubToFunctionMap(locked);
|
|
|
|
GlobalToIndirectSymMapTy &GM = state.getGlobalToIndirectSymMap(locked);
|
2009-03-05 06:34:37 +00:00
|
|
|
|
|
|
|
// Look up the cheap way first, to see if it's a function stub we are
|
|
|
|
// invalidating. If so, remove it from both the forward and reverse maps.
|
|
|
|
if (SM.find(Stub) != SM.end()) {
|
|
|
|
Function *F = SM[Stub];
|
|
|
|
SM.erase(Stub);
|
|
|
|
FM.erase(F);
|
|
|
|
return F;
|
|
|
|
}
|
|
|
|
|
2009-03-11 07:03:43 +00:00
|
|
|
// Otherwise, it might be an indirect symbol stub. Find it and remove it.
|
2009-04-27 05:09:44 +00:00
|
|
|
for (GlobalToIndirectSymMapTy::iterator i = GM.begin(), e = GM.end();
|
2009-03-05 06:34:37 +00:00
|
|
|
i != e; ++i) {
|
|
|
|
if (i->second != Stub)
|
|
|
|
continue;
|
|
|
|
GlobalValue *GV = i->first;
|
|
|
|
GM.erase(i);
|
|
|
|
return GV;
|
|
|
|
}
|
|
|
|
|
2009-03-11 07:03:43 +00:00
|
|
|
// Lastly, check to see if it's in the ExternalFnToStubMap.
|
|
|
|
for (std::map<void *, void *>::iterator i = ExternalFnToStubMap.begin(),
|
|
|
|
e = ExternalFnToStubMap.end(); i != e; ++i) {
|
|
|
|
if (i->second != Stub)
|
|
|
|
continue;
|
|
|
|
ExternalFnToStubMap.erase(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-03-05 06:34:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-11-20 23:57:07 +00:00
|
|
|
/// JITCompilerFn - This function is called when a lazy compilation stub has
|
|
|
|
/// been entered. It looks up which function this stub corresponds to, compiles
|
|
|
|
/// it if necessary, then returns the resultant function pointer.
|
|
|
|
void *JITResolver::JITCompilerFn(void *Stub) {
|
2007-02-24 02:57:03 +00:00
|
|
|
JITResolver &JR = *TheJITResolver;
|
2008-10-03 07:27:08 +00:00
|
|
|
|
|
|
|
Function* F = 0;
|
|
|
|
void* ActualPtr = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
// Only lock for getting the Function. The call getPointerToFunction made
|
|
|
|
// in this function might trigger function materializing, which requires
|
|
|
|
// JIT lock to be unlocked.
|
|
|
|
MutexGuard locked(TheJIT->lock);
|
|
|
|
|
|
|
|
// The address given to us for the stub may not be exactly right, it might be
|
|
|
|
// a little bit after the stub. As such, use upper_bound to find it.
|
2009-04-27 05:09:44 +00:00
|
|
|
StubToFunctionMapTy::iterator I =
|
2008-10-03 07:27:08 +00:00
|
|
|
JR.state.getStubToFunctionMap(locked).upper_bound(Stub);
|
|
|
|
assert(I != JR.state.getStubToFunctionMap(locked).begin() &&
|
|
|
|
"This is not a known stub!");
|
|
|
|
F = (--I)->second;
|
|
|
|
ActualPtr = I->first;
|
|
|
|
}
|
2004-11-20 23:57:07 +00:00
|
|
|
|
2007-06-30 00:10:37 +00:00
|
|
|
// If we have already code generated the function, just return the address.
|
|
|
|
void *Result = TheJIT->getPointerToGlobalIfAvailable(F);
|
|
|
|
|
|
|
|
if (!Result) {
|
|
|
|
// Otherwise we don't have it, do lazy compilation now.
|
|
|
|
|
|
|
|
// If lazy compilation is disabled, emit a useful error message and abort.
|
|
|
|
if (TheJIT->isLazyCompilationDisabled()) {
|
2009-07-11 13:10:19 +00:00
|
|
|
llvm_report_error("LLVM JIT requested to do lazy compilation of function '"
|
|
|
|
+ F->getName() + "' when lazy compiles are disabled!");
|
2007-06-30 00:10:37 +00:00
|
|
|
}
|
2006-11-09 19:32:13 +00:00
|
|
|
|
2007-06-30 00:10:37 +00:00
|
|
|
// We might like to remove the stub from the StubToFunction map.
|
|
|
|
// We can't do that! Multiple threads could be stuck, waiting to acquire the
|
|
|
|
// lock above. As soon as the 1st function finishes compiling the function,
|
|
|
|
// the next one will be released, and needs to be able to find the function
|
|
|
|
// it needs to call.
|
|
|
|
//JR.state.getStubToFunctionMap(locked).erase(I);
|
|
|
|
|
|
|
|
DOUT << "JIT: Lazily resolving function '" << F->getName()
|
|
|
|
<< "' In stub ptr = " << Stub << " actual ptr = "
|
2008-10-03 07:27:08 +00:00
|
|
|
<< ActualPtr << "\n";
|
2007-06-30 00:10:37 +00:00
|
|
|
|
|
|
|
Result = TheJIT->getPointerToFunction(F);
|
|
|
|
}
|
2008-10-03 07:27:08 +00:00
|
|
|
|
|
|
|
// Reacquire the lock to erase the stub in the map.
|
|
|
|
MutexGuard locked(TheJIT->lock);
|
2004-11-20 23:57:07 +00:00
|
|
|
|
|
|
|
// We don't need to reuse this stub in the future, as F is now compiled.
|
2005-07-12 15:51:55 +00:00
|
|
|
JR.state.getFunctionToStubMap(locked).erase(F);
|
2004-11-20 23:57:07 +00:00
|
|
|
|
|
|
|
// FIXME: We could rewrite all references to this stub if we knew them.
|
2005-07-28 12:44:13 +00:00
|
|
|
|
2005-07-30 18:33:25 +00:00
|
|
|
// What we will do is set the compiled function address to map to the
|
|
|
|
// same GOT entry as the stub so that later clients may update the GOT
|
2005-07-28 12:44:13 +00:00
|
|
|
// if they see it still using the stub address.
|
|
|
|
// Note: this is done so the Resolver doesn't have to manage GOT memory
|
|
|
|
// Do this without allocating map space if the target isn't using a GOT
|
|
|
|
if(JR.revGOTMap.find(Stub) != JR.revGOTMap.end())
|
|
|
|
JR.revGOTMap[Result] = JR.revGOTMap[Stub];
|
|
|
|
|
2004-11-20 23:57:07 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2004-11-22 22:00:25 +00:00
|
|
|
// JITEmitter code.
|
2004-11-20 23:57:07 +00:00
|
|
|
//
|
2003-08-14 18:35:27 +00:00
|
|
|
namespace {
|
2004-11-22 22:00:25 +00:00
|
|
|
/// JITEmitter - The JIT implementation of the MachineCodeEmitter, which is
|
|
|
|
/// used to output functions to memory for execution.
|
2009-05-30 20:51:52 +00:00
|
|
|
class JITEmitter : public JITCodeEmitter {
|
2007-12-05 23:39:57 +00:00
|
|
|
JITMemoryManager *MemMgr;
|
2003-08-14 18:35:27 +00:00
|
|
|
|
2003-05-09 03:30:07 +00:00
|
|
|
// When outputting a function stub in the context of some other function, we
|
2006-05-02 18:27:26 +00:00
|
|
|
// save BufferBegin/BufferEnd/CurBufferPtr here.
|
2009-06-04 00:15:51 +00:00
|
|
|
uint8_t *SavedBufferBegin, *SavedBufferEnd, *SavedCurBufferPtr;
|
2003-06-01 23:24:36 +00:00
|
|
|
|
2004-11-20 03:46:14 +00:00
|
|
|
/// Relocations - These are the relocations that the function needs, as
|
|
|
|
/// emitted.
|
|
|
|
std::vector<MachineRelocation> Relocations;
|
2006-05-03 17:10:41 +00:00
|
|
|
|
|
|
|
/// MBBLocations - This vector is a mapping from MBB ID's to their address.
|
|
|
|
/// It is filled in by the StartMachineBasicBlock callback and queried by
|
|
|
|
/// the getMachineBasicBlockAddress callback.
|
2008-12-10 02:32:19 +00:00
|
|
|
std::vector<uintptr_t> MBBLocations;
|
2005-07-22 20:48:12 +00:00
|
|
|
|
2006-02-09 04:49:59 +00:00
|
|
|
/// ConstantPool - The constant pool for the current function.
|
|
|
|
///
|
|
|
|
MachineConstantPool *ConstantPool;
|
|
|
|
|
|
|
|
/// ConstantPoolBase - A pointer to the first entry in the constant pool.
|
|
|
|
///
|
|
|
|
void *ConstantPoolBase;
|
2006-04-22 18:53:45 +00:00
|
|
|
|
2009-03-13 07:51:59 +00:00
|
|
|
/// ConstPoolAddresses - Addresses of individual constant pool entries.
|
|
|
|
///
|
|
|
|
SmallVector<uintptr_t, 8> ConstPoolAddresses;
|
|
|
|
|
2006-09-10 23:03:44 +00:00
|
|
|
/// JumpTable - The jump tables for the current function.
|
2006-04-22 18:53:45 +00:00
|
|
|
///
|
|
|
|
MachineJumpTableInfo *JumpTable;
|
|
|
|
|
|
|
|
/// JumpTableBase - A pointer to the first entry in the jump table.
|
|
|
|
///
|
|
|
|
void *JumpTableBase;
|
2008-01-05 02:26:58 +00:00
|
|
|
|
2007-02-24 02:57:03 +00:00
|
|
|
/// Resolver - This contains info about the currently resolved functions.
|
|
|
|
JITResolver Resolver;
|
2008-02-13 18:39:37 +00:00
|
|
|
|
|
|
|
/// DE - The dwarf emitter for the jit.
|
|
|
|
JITDwarfEmitter *DE;
|
|
|
|
|
|
|
|
/// LabelLocations - This vector is a mapping from Label ID's to their
|
|
|
|
/// address.
|
2008-12-10 02:32:19 +00:00
|
|
|
std::vector<uintptr_t> LabelLocations;
|
2008-02-13 18:39:37 +00:00
|
|
|
|
|
|
|
/// MMI - Machine module info for exception informations
|
|
|
|
MachineModuleInfo* MMI;
|
|
|
|
|
2008-08-07 01:30:15 +00:00
|
|
|
// GVSet - a set to keep track of which globals have been seen
|
2008-11-07 09:02:17 +00:00
|
|
|
SmallPtrSet<const GlobalVariable*, 8> GVSet;
|
2008-08-07 01:30:15 +00:00
|
|
|
|
2009-03-05 06:34:37 +00:00
|
|
|
// CurFn - The llvm function being emitted. Only valid during
|
|
|
|
// finishFunction().
|
|
|
|
const Function *CurFn;
|
2009-07-16 21:07:26 +00:00
|
|
|
|
|
|
|
/// Information about emitted code, which is passed to the
|
|
|
|
/// JITEventListeners. This is reset in startFunction and used in
|
|
|
|
/// finishFunction.
|
|
|
|
JITEvent_EmittedFunctionDetails EmissionDetails;
|
|
|
|
|
2009-03-05 06:34:37 +00:00
|
|
|
// CurFnStubUses - For a given Function, a vector of stubs that it
|
|
|
|
// references. This facilitates the JIT detecting that a stub is no
|
|
|
|
// longer used, so that it may be deallocated.
|
|
|
|
DenseMap<const Function *, SmallVector<void*, 1> > CurFnStubUses;
|
|
|
|
|
|
|
|
// StubFnRefs - For a given pointer to a stub, a set of Functions which
|
|
|
|
// reference the stub. When the count of a stub's references drops to zero,
|
|
|
|
// the stub is unused.
|
|
|
|
DenseMap<void *, SmallPtrSet<const Function*, 1> > StubFnRefs;
|
|
|
|
|
2009-03-11 07:03:43 +00:00
|
|
|
// ExtFnStubs - A map of external function names to stubs which have entries
|
|
|
|
// in the JITResolver's ExternalFnToStubMap.
|
|
|
|
StringMap<void *> ExtFnStubs;
|
2009-05-18 21:06:40 +00:00
|
|
|
|
2009-07-16 21:07:26 +00:00
|
|
|
DebugLocTuple PrevDLT;
|
|
|
|
|
2007-02-24 02:57:03 +00:00
|
|
|
public:
|
2009-07-23 01:40:54 +00:00
|
|
|
JITEmitter(JIT &jit, JITMemoryManager *JMM) : Resolver(jit), CurFn(0) {
|
2007-12-06 01:08:09 +00:00
|
|
|
MemMgr = JMM ? JMM : JITMemoryManager::CreateDefaultMemManager();
|
2007-12-05 23:39:57 +00:00
|
|
|
if (jit.getJITInfo().needsGOT()) {
|
|
|
|
MemMgr->AllocateGOT();
|
|
|
|
DOUT << "JIT is managing a GOT\n";
|
|
|
|
}
|
2008-02-13 18:39:37 +00:00
|
|
|
|
|
|
|
if (ExceptionHandling) DE = new JITDwarfEmitter(jit);
|
2007-12-05 23:39:57 +00:00
|
|
|
}
|
|
|
|
~JITEmitter() {
|
|
|
|
delete MemMgr;
|
2008-02-13 18:39:37 +00:00
|
|
|
if (ExceptionHandling) delete DE;
|
2005-07-22 20:48:12 +00:00
|
|
|
}
|
2008-08-20 00:28:12 +00:00
|
|
|
|
|
|
|
/// classof - Methods for support type inquiry through isa, cast, and
|
|
|
|
/// dyn_cast:
|
|
|
|
///
|
|
|
|
static inline bool classof(const JITEmitter*) { return true; }
|
|
|
|
static inline bool classof(const MachineCodeEmitter*) { return true; }
|
2007-02-24 02:57:03 +00:00
|
|
|
|
|
|
|
JITResolver &getJITResolver() { return Resolver; }
|
2002-12-24 00:01:05 +00:00
|
|
|
|
|
|
|
virtual void startFunction(MachineFunction &F);
|
2006-05-02 18:27:26 +00:00
|
|
|
virtual bool finishFunction(MachineFunction &F);
|
2006-05-02 23:22:24 +00:00
|
|
|
|
|
|
|
void emitConstantPool(MachineConstantPool *MCP);
|
|
|
|
void initJumpTableInfo(MachineJumpTableInfo *MJTI);
|
2006-12-14 22:53:42 +00:00
|
|
|
void emitJumpTableInfo(MachineJumpTableInfo *MJTI);
|
2006-05-02 23:22:24 +00:00
|
|
|
|
2008-11-08 08:02:53 +00:00
|
|
|
virtual void startGVStub(const GlobalValue* GV, unsigned StubSize,
|
2008-04-16 20:46:05 +00:00
|
|
|
unsigned Alignment = 1);
|
2009-02-18 08:31:02 +00:00
|
|
|
virtual void startGVStub(const GlobalValue* GV, void *Buffer,
|
|
|
|
unsigned StubSize);
|
2008-11-08 08:02:53 +00:00
|
|
|
virtual void* finishGVStub(const GlobalValue *GV);
|
2003-06-01 23:24:36 +00:00
|
|
|
|
2008-10-21 11:42:16 +00:00
|
|
|
/// allocateSpace - Reserves space in the current block if any, or
|
|
|
|
/// allocate a new one of the given size.
|
2008-12-10 02:32:19 +00:00
|
|
|
virtual void *allocateSpace(uintptr_t Size, unsigned Alignment);
|
2008-10-21 11:42:16 +00:00
|
|
|
|
2009-07-08 21:59:57 +00:00
|
|
|
/// allocateGlobal - Allocate memory for a global. Unlike allocateSpace,
|
|
|
|
/// this method does not allocate memory in the current output buffer,
|
|
|
|
/// because a global may live longer than the current function.
|
|
|
|
virtual void *allocateGlobal(uintptr_t Size, unsigned Alignment);
|
|
|
|
|
2004-11-20 03:46:14 +00:00
|
|
|
virtual void addRelocation(const MachineRelocation &MR) {
|
|
|
|
Relocations.push_back(MR);
|
|
|
|
}
|
2006-05-03 17:10:41 +00:00
|
|
|
|
|
|
|
virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) {
|
|
|
|
if (MBBLocations.size() <= (unsigned)MBB->getNumber())
|
|
|
|
MBBLocations.resize((MBB->getNumber()+1)*2);
|
|
|
|
MBBLocations[MBB->getNumber()] = getCurrentPCValue();
|
2008-11-07 22:30:29 +00:00
|
|
|
DOUT << "JIT: Emitting BB" << MBB->getNumber() << " at ["
|
|
|
|
<< (void*) getCurrentPCValue() << "]\n";
|
2006-05-03 17:10:41 +00:00
|
|
|
}
|
|
|
|
|
2008-12-10 02:32:19 +00:00
|
|
|
virtual uintptr_t getConstantPoolEntryAddress(unsigned Entry) const;
|
|
|
|
virtual uintptr_t getJumpTableEntryAddress(unsigned Entry) const;
|
2008-01-05 02:26:58 +00:00
|
|
|
|
2008-12-10 02:32:19 +00:00
|
|
|
virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
|
2006-05-03 17:10:41 +00:00
|
|
|
assert(MBBLocations.size() > (unsigned)MBB->getNumber() &&
|
|
|
|
MBBLocations[MBB->getNumber()] && "MBB not emitted!");
|
|
|
|
return MBBLocations[MBB->getNumber()];
|
|
|
|
}
|
2004-11-20 03:46:14 +00:00
|
|
|
|
2006-05-11 23:08:08 +00:00
|
|
|
/// deallocateMemForFunction - Deallocate all memory for the specified
|
|
|
|
/// function body.
|
2009-07-23 01:40:54 +00:00
|
|
|
void deallocateMemForFunction(Function *F);
|
2009-03-11 07:03:43 +00:00
|
|
|
|
|
|
|
/// AddStubToCurrentFunction - Mark the current function being JIT'd as
|
|
|
|
/// using the stub at the specified address. Allows
|
|
|
|
/// deallocateMemForFunction to also remove stubs no longer referenced.
|
|
|
|
void AddStubToCurrentFunction(void *Stub);
|
|
|
|
|
|
|
|
/// getExternalFnStubs - Accessor for the JIT to find stubs emitted for
|
|
|
|
/// MachineRelocations that reference external functions by name.
|
|
|
|
const StringMap<void*> &getExternalFnStubs() const { return ExtFnStubs; }
|
2008-02-13 18:39:37 +00:00
|
|
|
|
2009-07-16 21:07:26 +00:00
|
|
|
virtual void processDebugLoc(DebugLoc DL);
|
|
|
|
|
2008-02-13 18:39:37 +00:00
|
|
|
virtual void emitLabel(uint64_t LabelID) {
|
|
|
|
if (LabelLocations.size() <= LabelID)
|
|
|
|
LabelLocations.resize((LabelID+1)*2);
|
|
|
|
LabelLocations[LabelID] = getCurrentPCValue();
|
|
|
|
}
|
|
|
|
|
2008-12-10 02:32:19 +00:00
|
|
|
virtual uintptr_t getLabelAddress(uint64_t LabelID) const {
|
2008-02-13 18:39:37 +00:00
|
|
|
assert(LabelLocations.size() > (unsigned)LabelID &&
|
|
|
|
LabelLocations[LabelID] && "Label not emitted!");
|
|
|
|
return LabelLocations[LabelID];
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void setModuleInfo(MachineModuleInfo* Info) {
|
|
|
|
MMI = Info;
|
|
|
|
if (ExceptionHandling) DE->setModuleInfo(Info);
|
|
|
|
}
|
|
|
|
|
2008-10-03 16:17:20 +00:00
|
|
|
void setMemoryExecutable(void) {
|
|
|
|
MemMgr->setMemoryExecutable();
|
|
|
|
}
|
2009-02-18 08:31:02 +00:00
|
|
|
|
|
|
|
JITMemoryManager *getMemMgr(void) const { return MemMgr; }
|
2008-10-03 16:17:20 +00:00
|
|
|
|
2004-11-20 23:57:07 +00:00
|
|
|
private:
|
2004-11-21 03:37:42 +00:00
|
|
|
void *getPointerToGlobal(GlobalValue *GV, void *Reference, bool NoNeedStub);
|
2008-11-10 01:52:24 +00:00
|
|
|
void *getPointerToGVIndirectSym(GlobalValue *V, void *Reference,
|
2008-11-10 23:26:16 +00:00
|
|
|
bool NoNeedStub);
|
2008-08-07 01:30:15 +00:00
|
|
|
unsigned addSizeOfGlobal(const GlobalVariable *GV, unsigned Size);
|
|
|
|
unsigned addSizeOfGlobalsInConstantVal(const Constant *C, unsigned Size);
|
|
|
|
unsigned addSizeOfGlobalsInInitializer(const Constant *Init, unsigned Size);
|
|
|
|
unsigned GetSizeOfGlobalsInBytes(MachineFunction &MF);
|
2002-12-24 00:01:05 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2004-11-22 22:00:25 +00:00
|
|
|
void *JITEmitter::getPointerToGlobal(GlobalValue *V, void *Reference,
|
|
|
|
bool DoesntNeedStub) {
|
2009-02-18 08:31:02 +00:00
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
|
2004-11-20 23:57:07 +00:00
|
|
|
return TheJIT->getOrEmitGlobalVariable(GV);
|
2009-02-18 08:31:02 +00:00
|
|
|
|
2008-06-25 20:21:35 +00:00
|
|
|
if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
|
2008-09-09 20:05:04 +00:00
|
|
|
return TheJIT->getPointerToGlobal(GA->resolveAliasedGlobal(false));
|
2004-11-20 23:57:07 +00:00
|
|
|
|
|
|
|
// If we have already compiled the function, return a pointer to its body.
|
|
|
|
Function *F = cast<Function>(V);
|
2008-11-13 21:50:50 +00:00
|
|
|
void *ResultPtr;
|
2009-03-05 06:34:37 +00:00
|
|
|
if (!DoesntNeedStub && !TheJIT->isLazyCompilationDisabled()) {
|
2008-11-13 21:50:50 +00:00
|
|
|
// Return the function stub if it's already created.
|
|
|
|
ResultPtr = Resolver.getFunctionStubIfAvailable(F);
|
2009-03-05 06:34:37 +00:00
|
|
|
if (ResultPtr)
|
|
|
|
AddStubToCurrentFunction(ResultPtr);
|
|
|
|
} else {
|
2008-11-13 21:50:50 +00:00
|
|
|
ResultPtr = TheJIT->getPointerToGlobalIfAvailable(F);
|
2009-03-05 06:34:37 +00:00
|
|
|
}
|
2004-11-20 23:57:07 +00:00
|
|
|
if (ResultPtr) return ResultPtr;
|
|
|
|
|
2009-02-18 08:31:02 +00:00
|
|
|
// If this is an external function pointer, we can force the JIT to
|
2009-03-07 06:41:19 +00:00
|
|
|
// 'compile' it, which really just adds it to the map. In dlsym mode,
|
|
|
|
// external functions are forced through a stub, regardless of reloc type.
|
|
|
|
if (F->isDeclaration() && !F->hasNotBeenReadFromBitcode() &&
|
|
|
|
DoesntNeedStub && !TheJIT->areDlsymStubsEnabled())
|
2009-02-18 08:31:02 +00:00
|
|
|
return TheJIT->getPointerToFunction(F);
|
|
|
|
|
2004-11-21 03:37:42 +00:00
|
|
|
// Okay, the function has not been compiled yet, if the target callback
|
|
|
|
// mechanism is capable of rewriting the instruction directly, prefer to do
|
2009-03-07 06:41:19 +00:00
|
|
|
// that instead of emitting a stub. This uses the lazy resolver, so is not
|
|
|
|
// legal if lazy compilation is disabled.
|
|
|
|
if (DoesntNeedStub && !TheJIT->isLazyCompilationDisabled())
|
2007-02-24 02:57:03 +00:00
|
|
|
return Resolver.AddCallbackAtLocation(F, Reference);
|
2004-11-21 03:37:42 +00:00
|
|
|
|
2009-03-07 06:41:19 +00:00
|
|
|
// Otherwise, we have to emit a stub.
|
2009-03-05 06:34:37 +00:00
|
|
|
void *StubAddr = Resolver.getFunctionStub(F);
|
|
|
|
|
|
|
|
// Add the stub to the current function's list of referenced stubs, so we can
|
2009-03-07 06:41:19 +00:00
|
|
|
// deallocate them if the current function is ever freed. It's possible to
|
|
|
|
// return null from getFunctionStub in the case of a weak extern that fails
|
|
|
|
// to resolve.
|
|
|
|
if (StubAddr)
|
|
|
|
AddStubToCurrentFunction(StubAddr);
|
|
|
|
|
2009-03-05 06:34:37 +00:00
|
|
|
return StubAddr;
|
2004-11-20 23:57:07 +00:00
|
|
|
}
|
|
|
|
|
2008-11-10 01:52:24 +00:00
|
|
|
void *JITEmitter::getPointerToGVIndirectSym(GlobalValue *V, void *Reference,
|
2008-11-10 23:26:16 +00:00
|
|
|
bool NoNeedStub) {
|
2009-03-05 06:34:37 +00:00
|
|
|
// Make sure GV is emitted first, and create a stub containing the fully
|
|
|
|
// resolved address.
|
2008-01-04 10:46:51 +00:00
|
|
|
void *GVAddress = getPointerToGlobal(V, Reference, true);
|
2009-03-05 06:34:37 +00:00
|
|
|
void *StubAddr = Resolver.getGlobalValueIndirectSym(V, GVAddress);
|
|
|
|
|
|
|
|
// Add the stub to the current function's list of referenced stubs, so we can
|
|
|
|
// deallocate them if the current function is ever freed.
|
|
|
|
AddStubToCurrentFunction(StubAddr);
|
|
|
|
|
|
|
|
return StubAddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JITEmitter::AddStubToCurrentFunction(void *StubAddr) {
|
|
|
|
if (!TheJIT->areDlsymStubsEnabled())
|
|
|
|
return;
|
|
|
|
|
|
|
|
assert(CurFn && "Stub added to current function, but current function is 0!");
|
|
|
|
|
|
|
|
SmallVectorImpl<void*> &StubsUsed = CurFnStubUses[CurFn];
|
|
|
|
StubsUsed.push_back(StubAddr);
|
|
|
|
|
|
|
|
SmallPtrSet<const Function *, 1> &FnRefs = StubFnRefs[StubAddr];
|
|
|
|
FnRefs.insert(CurFn);
|
2008-01-04 10:46:51 +00:00
|
|
|
}
|
|
|
|
|
2009-07-16 21:07:26 +00:00
|
|
|
void JITEmitter::processDebugLoc(DebugLoc DL) {
|
|
|
|
if (!DL.isUnknown()) {
|
|
|
|
DebugLocTuple CurDLT = EmissionDetails.MF->getDebugLocTuple(DL);
|
|
|
|
|
|
|
|
if (CurDLT.CompileUnit != 0 && PrevDLT != CurDLT) {
|
|
|
|
JITEvent_EmittedFunctionDetails::LineStart NextLine;
|
|
|
|
NextLine.Address = getCurrentPCValue();
|
|
|
|
NextLine.Loc = DL;
|
|
|
|
EmissionDetails.LineStarts.push_back(NextLine);
|
|
|
|
}
|
|
|
|
|
|
|
|
PrevDLT = CurDLT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-13 07:51:59 +00:00
|
|
|
static unsigned GetConstantPoolSizeInBytes(MachineConstantPool *MCP,
|
|
|
|
const TargetData *TD) {
|
2008-04-18 20:59:31 +00:00
|
|
|
const std::vector<MachineConstantPoolEntry> &Constants = MCP->getConstants();
|
|
|
|
if (Constants.empty()) return 0;
|
|
|
|
|
2009-03-13 07:51:59 +00:00
|
|
|
unsigned Size = 0;
|
|
|
|
for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
|
|
|
|
MachineConstantPoolEntry CPE = Constants[i];
|
|
|
|
unsigned AlignMask = CPE.getAlignment() - 1;
|
|
|
|
Size = (Size + AlignMask) & ~AlignMask;
|
|
|
|
const Type *Ty = CPE.getType();
|
2009-05-09 07:06:46 +00:00
|
|
|
Size += TD->getTypeAllocSize(Ty);
|
2009-03-13 07:51:59 +00:00
|
|
|
}
|
2008-04-18 20:59:31 +00:00
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned GetJumpTableSizeInBytes(MachineJumpTableInfo *MJTI) {
|
|
|
|
const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
|
|
|
|
if (JT.empty()) return 0;
|
|
|
|
|
|
|
|
unsigned NumEntries = 0;
|
|
|
|
for (unsigned i = 0, e = JT.size(); i != e; ++i)
|
|
|
|
NumEntries += JT[i].MBBs.size();
|
|
|
|
|
|
|
|
unsigned EntrySize = MJTI->getEntrySize();
|
|
|
|
|
|
|
|
return NumEntries * EntrySize;
|
|
|
|
}
|
|
|
|
|
2008-04-20 23:39:44 +00:00
|
|
|
static uintptr_t RoundUpToAlign(uintptr_t Size, unsigned Alignment) {
|
2008-04-18 20:59:31 +00:00
|
|
|
if (Alignment == 0) Alignment = 1;
|
2008-04-20 23:39:44 +00:00
|
|
|
// Since we do not know where the buffer will be allocated, be pessimistic.
|
|
|
|
return Size + Alignment;
|
2008-04-18 20:59:31 +00:00
|
|
|
}
|
2008-01-04 10:46:51 +00:00
|
|
|
|
2008-08-07 01:30:15 +00:00
|
|
|
/// addSizeOfGlobal - add the size of the global (plus any alignment padding)
|
|
|
|
/// into the running total Size.
|
|
|
|
|
|
|
|
unsigned JITEmitter::addSizeOfGlobal(const GlobalVariable *GV, unsigned Size) {
|
|
|
|
const Type *ElTy = GV->getType()->getElementType();
|
2009-05-09 07:06:46 +00:00
|
|
|
size_t GVSize = (size_t)TheJIT->getTargetData()->getTypeAllocSize(ElTy);
|
2008-08-07 01:30:15 +00:00
|
|
|
size_t GVAlign =
|
|
|
|
(size_t)TheJIT->getTargetData()->getPreferredAlignment(GV);
|
2008-11-06 17:46:04 +00:00
|
|
|
DOUT << "JIT: Adding in size " << GVSize << " alignment " << GVAlign;
|
2008-08-07 01:30:15 +00:00
|
|
|
DEBUG(GV->dump());
|
|
|
|
// Assume code section ends with worst possible alignment, so first
|
|
|
|
// variable needs maximal padding.
|
|
|
|
if (Size==0)
|
|
|
|
Size = 1;
|
|
|
|
Size = ((Size+GVAlign-1)/GVAlign)*GVAlign;
|
|
|
|
Size += GVSize;
|
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// addSizeOfGlobalsInConstantVal - find any globals that we haven't seen yet
|
|
|
|
/// but are referenced from the constant; put them in GVSet and add their
|
|
|
|
/// size into the running total Size.
|
|
|
|
|
|
|
|
unsigned JITEmitter::addSizeOfGlobalsInConstantVal(const Constant *C,
|
|
|
|
unsigned Size) {
|
|
|
|
// If its undefined, return the garbage.
|
|
|
|
if (isa<UndefValue>(C))
|
|
|
|
return Size;
|
|
|
|
|
|
|
|
// If the value is a ConstantExpr
|
|
|
|
if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
|
|
|
|
Constant *Op0 = CE->getOperand(0);
|
|
|
|
switch (CE->getOpcode()) {
|
|
|
|
case Instruction::GetElementPtr:
|
|
|
|
case Instruction::Trunc:
|
|
|
|
case Instruction::ZExt:
|
|
|
|
case Instruction::SExt:
|
|
|
|
case Instruction::FPTrunc:
|
|
|
|
case Instruction::FPExt:
|
|
|
|
case Instruction::UIToFP:
|
|
|
|
case Instruction::SIToFP:
|
|
|
|
case Instruction::FPToUI:
|
|
|
|
case Instruction::FPToSI:
|
|
|
|
case Instruction::PtrToInt:
|
|
|
|
case Instruction::IntToPtr:
|
|
|
|
case Instruction::BitCast: {
|
|
|
|
Size = addSizeOfGlobalsInConstantVal(Op0, Size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Instruction::Add:
|
2009-06-04 22:49:04 +00:00
|
|
|
case Instruction::FAdd:
|
2008-08-07 01:30:15 +00:00
|
|
|
case Instruction::Sub:
|
2009-06-04 22:49:04 +00:00
|
|
|
case Instruction::FSub:
|
2008-08-07 01:30:15 +00:00
|
|
|
case Instruction::Mul:
|
2009-06-04 22:49:04 +00:00
|
|
|
case Instruction::FMul:
|
2008-08-07 01:30:15 +00:00
|
|
|
case Instruction::UDiv:
|
|
|
|
case Instruction::SDiv:
|
|
|
|
case Instruction::URem:
|
|
|
|
case Instruction::SRem:
|
|
|
|
case Instruction::And:
|
|
|
|
case Instruction::Or:
|
|
|
|
case Instruction::Xor: {
|
|
|
|
Size = addSizeOfGlobalsInConstantVal(Op0, Size);
|
|
|
|
Size = addSizeOfGlobalsInConstantVal(CE->getOperand(1), Size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2009-07-11 13:10:19 +00:00
|
|
|
std::string msg;
|
|
|
|
raw_string_ostream Msg(msg);
|
|
|
|
Msg << "ConstantExpr not handled: " << *CE;
|
|
|
|
llvm_report_error(Msg.str());
|
2008-08-07 01:30:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (C->getType()->getTypeID() == Type::PointerTyID)
|
|
|
|
if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(C))
|
2008-11-07 09:02:17 +00:00
|
|
|
if (GVSet.insert(GV))
|
2008-08-07 01:30:15 +00:00
|
|
|
Size = addSizeOfGlobal(GV, Size);
|
|
|
|
|
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// addSizeOfGLobalsInInitializer - handle any globals that we haven't seen yet
|
|
|
|
/// but are referenced from the given initializer.
|
|
|
|
|
|
|
|
unsigned JITEmitter::addSizeOfGlobalsInInitializer(const Constant *Init,
|
|
|
|
unsigned Size) {
|
|
|
|
if (!isa<UndefValue>(Init) &&
|
|
|
|
!isa<ConstantVector>(Init) &&
|
|
|
|
!isa<ConstantAggregateZero>(Init) &&
|
|
|
|
!isa<ConstantArray>(Init) &&
|
|
|
|
!isa<ConstantStruct>(Init) &&
|
|
|
|
Init->getType()->isFirstClassType())
|
|
|
|
Size = addSizeOfGlobalsInConstantVal(Init, Size);
|
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// GetSizeOfGlobalsInBytes - walk the code for the function, looking for
|
|
|
|
/// globals; then walk the initializers of those globals looking for more.
|
|
|
|
/// If their size has not been considered yet, add it into the running total
|
|
|
|
/// Size.
|
|
|
|
|
|
|
|
unsigned JITEmitter::GetSizeOfGlobalsInBytes(MachineFunction &MF) {
|
|
|
|
unsigned Size = 0;
|
|
|
|
GVSet.clear();
|
|
|
|
|
|
|
|
for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
|
|
|
|
MBB != E; ++MBB) {
|
|
|
|
for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
|
|
|
|
I != E; ++I) {
|
|
|
|
const TargetInstrDesc &Desc = I->getDesc();
|
|
|
|
const MachineInstr &MI = *I;
|
|
|
|
unsigned NumOps = Desc.getNumOperands();
|
|
|
|
for (unsigned CurOp = 0; CurOp < NumOps; CurOp++) {
|
|
|
|
const MachineOperand &MO = MI.getOperand(CurOp);
|
2008-10-03 15:45:36 +00:00
|
|
|
if (MO.isGlobal()) {
|
2008-08-07 01:30:15 +00:00
|
|
|
GlobalValue* V = MO.getGlobal();
|
|
|
|
const GlobalVariable *GV = dyn_cast<const GlobalVariable>(V);
|
|
|
|
if (!GV)
|
|
|
|
continue;
|
|
|
|
// If seen in previous function, it will have an entry here.
|
|
|
|
if (TheJIT->getPointerToGlobalIfAvailable(GV))
|
|
|
|
continue;
|
|
|
|
// If seen earlier in this function, it will have an entry here.
|
|
|
|
// FIXME: it should be possible to combine these tables, by
|
|
|
|
// assuming the addresses of the new globals in this module
|
|
|
|
// start at 0 (or something) and adjusting them after codegen
|
|
|
|
// complete. Another possibility is to grab a marker bit in GV.
|
2008-11-07 09:02:17 +00:00
|
|
|
if (GVSet.insert(GV))
|
2008-08-07 01:30:15 +00:00
|
|
|
// A variable as yet unseen. Add in its size.
|
|
|
|
Size = addSizeOfGlobal(GV, Size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-11-06 17:46:04 +00:00
|
|
|
DOUT << "JIT: About to look through initializers\n";
|
2008-08-07 01:30:15 +00:00
|
|
|
// Look for more globals that are referenced only from initializers.
|
|
|
|
// GVSet.end is computed each time because the set can grow as we go.
|
2008-11-07 09:02:17 +00:00
|
|
|
for (SmallPtrSet<const GlobalVariable *, 8>::iterator I = GVSet.begin();
|
2008-08-07 01:30:15 +00:00
|
|
|
I != GVSet.end(); I++) {
|
|
|
|
const GlobalVariable* GV = *I;
|
|
|
|
if (GV->hasInitializer())
|
|
|
|
Size = addSizeOfGlobalsInInitializer(GV->getInitializer(), Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
2004-11-22 22:00:25 +00:00
|
|
|
void JITEmitter::startFunction(MachineFunction &F) {
|
2008-11-06 17:46:04 +00:00
|
|
|
DOUT << "JIT: Starting CodeGen of Function "
|
|
|
|
<< F.getFunction()->getName() << "\n";
|
|
|
|
|
2008-04-18 20:59:31 +00:00
|
|
|
uintptr_t ActualSize = 0;
|
2008-10-03 16:17:20 +00:00
|
|
|
// Set the memory writable, if it's not already
|
|
|
|
MemMgr->setMemoryWritable();
|
2008-04-20 17:44:19 +00:00
|
|
|
if (MemMgr->NeedsExactSize()) {
|
2008-11-06 17:46:04 +00:00
|
|
|
DOUT << "JIT: ExactSize\n";
|
2008-04-18 20:59:31 +00:00
|
|
|
const TargetInstrInfo* TII = F.getTarget().getInstrInfo();
|
|
|
|
MachineJumpTableInfo *MJTI = F.getJumpTableInfo();
|
|
|
|
MachineConstantPool *MCP = F.getConstantPool();
|
|
|
|
|
|
|
|
// Ensure the constant pool/jump table info is at least 4-byte aligned.
|
2008-04-20 23:39:44 +00:00
|
|
|
ActualSize = RoundUpToAlign(ActualSize, 16);
|
2008-04-18 20:59:31 +00:00
|
|
|
|
|
|
|
// Add the alignment of the constant pool
|
2009-03-13 07:51:59 +00:00
|
|
|
ActualSize = RoundUpToAlign(ActualSize, MCP->getConstantPoolAlignment());
|
2008-04-18 20:59:31 +00:00
|
|
|
|
|
|
|
// Add the constant pool size
|
2009-03-13 07:51:59 +00:00
|
|
|
ActualSize += GetConstantPoolSizeInBytes(MCP, TheJIT->getTargetData());
|
2008-04-18 20:59:31 +00:00
|
|
|
|
|
|
|
// Add the aligment of the jump table info
|
2008-04-20 23:39:44 +00:00
|
|
|
ActualSize = RoundUpToAlign(ActualSize, MJTI->getAlignment());
|
2008-04-18 20:59:31 +00:00
|
|
|
|
|
|
|
// Add the jump table size
|
|
|
|
ActualSize += GetJumpTableSizeInBytes(MJTI);
|
|
|
|
|
|
|
|
// Add the alignment for the function
|
2008-04-20 23:39:44 +00:00
|
|
|
ActualSize = RoundUpToAlign(ActualSize,
|
|
|
|
std::max(F.getFunction()->getAlignment(), 8U));
|
2008-04-18 20:59:31 +00:00
|
|
|
|
|
|
|
// Add the function size
|
|
|
|
ActualSize += TII->GetFunctionSizeInBytes(F);
|
2008-08-07 01:30:15 +00:00
|
|
|
|
2008-11-06 17:46:04 +00:00
|
|
|
DOUT << "JIT: ActualSize before globals " << ActualSize << "\n";
|
2008-08-07 01:30:15 +00:00
|
|
|
// Add the size of the globals that will be allocated after this function.
|
|
|
|
// These are all the ones referenced from this function that were not
|
|
|
|
// previously allocated.
|
|
|
|
ActualSize += GetSizeOfGlobalsInBytes(F);
|
2008-11-06 17:46:04 +00:00
|
|
|
DOUT << "JIT: ActualSize after globals " << ActualSize << "\n";
|
2008-04-18 20:59:31 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 23:39:57 +00:00
|
|
|
BufferBegin = CurBufferPtr = MemMgr->startFunctionBody(F.getFunction(),
|
|
|
|
ActualSize);
|
2006-05-11 23:08:08 +00:00
|
|
|
BufferEnd = BufferBegin+ActualSize;
|
2006-05-02 23:22:24 +00:00
|
|
|
|
2006-11-16 20:04:54 +00:00
|
|
|
// Ensure the constant pool/jump table info is at least 4-byte aligned.
|
|
|
|
emitAlignment(16);
|
|
|
|
|
2006-05-02 23:22:24 +00:00
|
|
|
emitConstantPool(F.getConstantPool());
|
|
|
|
initJumpTableInfo(F.getJumpTableInfo());
|
|
|
|
|
|
|
|
// About to start emitting the machine code for the function.
|
2006-05-03 01:03:20 +00:00
|
|
|
emitAlignment(std::max(F.getFunction()->getAlignment(), 8U));
|
2006-05-02 23:22:24 +00:00
|
|
|
TheJIT->updateGlobalMapping(F.getFunction(), CurBufferPtr);
|
2006-07-25 20:40:54 +00:00
|
|
|
|
2006-05-03 17:10:41 +00:00
|
|
|
MBBLocations.clear();
|
2009-07-16 21:07:26 +00:00
|
|
|
|
|
|
|
EmissionDetails.MF = &F;
|
|
|
|
EmissionDetails.LineStarts.clear();
|
2002-12-24 00:01:05 +00:00
|
|
|
}
|
|
|
|
|
2006-05-02 18:27:26 +00:00
|
|
|
bool JITEmitter::finishFunction(MachineFunction &F) {
|
2006-05-11 23:08:08 +00:00
|
|
|
if (CurBufferPtr == BufferEnd) {
|
2009-07-23 01:40:54 +00:00
|
|
|
// FIXME: Allocate more space, then try again.
|
|
|
|
llvm_report_error("JIT: Ran out of space for generated machine code!");
|
2006-05-11 23:08:08 +00:00
|
|
|
}
|
2009-07-23 01:40:54 +00:00
|
|
|
|
2006-12-14 22:53:42 +00:00
|
|
|
emitJumpTableInfo(F.getJumpTableInfo());
|
2009-07-23 01:40:54 +00:00
|
|
|
|
2006-06-16 18:09:26 +00:00
|
|
|
// FnStart is the start of the text, not the start of the constant pool and
|
|
|
|
// other per-function data.
|
2009-06-04 00:15:51 +00:00
|
|
|
uint8_t *FnStart =
|
|
|
|
(uint8_t *)TheJIT->getPointerToGlobalIfAvailable(F.getFunction());
|
2002-12-24 00:01:05 +00:00
|
|
|
|
2009-04-30 23:01:58 +00:00
|
|
|
// FnEnd is the end of the function's machine code.
|
2009-06-04 00:15:51 +00:00
|
|
|
uint8_t *FnEnd = CurBufferPtr;
|
2009-04-30 23:01:58 +00:00
|
|
|
|
2004-11-20 03:46:14 +00:00
|
|
|
if (!Relocations.empty()) {
|
2009-03-05 06:34:37 +00:00
|
|
|
CurFn = F.getFunction();
|
2005-07-20 16:29:20 +00:00
|
|
|
NumRelos += Relocations.size();
|
|
|
|
|
2004-11-20 03:46:14 +00:00
|
|
|
// Resolve the relocations to concrete pointers.
|
|
|
|
for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
|
|
|
|
MachineRelocation &MR = Relocations[i];
|
2008-11-03 07:14:02 +00:00
|
|
|
void *ResultPtr = 0;
|
2008-10-29 23:54:46 +00:00
|
|
|
if (!MR.letTargetResolve()) {
|
2008-11-08 07:37:34 +00:00
|
|
|
if (MR.isExternalSymbol()) {
|
2009-01-05 05:32:42 +00:00
|
|
|
ResultPtr = TheJIT->getPointerToNamedFunction(MR.getExternalSymbol(),
|
|
|
|
false);
|
2008-11-08 07:37:34 +00:00
|
|
|
DOUT << "JIT: Map \'" << MR.getExternalSymbol() << "\' to ["
|
2008-11-08 07:22:53 +00:00
|
|
|
<< ResultPtr << "]\n";
|
2008-10-29 23:54:46 +00:00
|
|
|
|
|
|
|
// If the target REALLY wants a stub for this function, emit it now.
|
2009-03-11 07:03:43 +00:00
|
|
|
if (!MR.doesntNeedStub()) {
|
|
|
|
if (!TheJIT->areDlsymStubsEnabled()) {
|
|
|
|
ResultPtr = Resolver.getExternalFunctionStub(ResultPtr);
|
|
|
|
} else {
|
|
|
|
void *&Stub = ExtFnStubs[MR.getExternalSymbol()];
|
|
|
|
if (!Stub) {
|
|
|
|
Stub = Resolver.getExternalFunctionStub((void *)&Stub);
|
|
|
|
AddStubToCurrentFunction(Stub);
|
|
|
|
}
|
|
|
|
ResultPtr = Stub;
|
|
|
|
}
|
|
|
|
}
|
2008-10-29 23:54:46 +00:00
|
|
|
} else if (MR.isGlobalValue()) {
|
|
|
|
ResultPtr = getPointerToGlobal(MR.getGlobalValue(),
|
|
|
|
BufferBegin+MR.getMachineCodeOffset(),
|
|
|
|
MR.doesntNeedStub());
|
2008-11-10 01:52:24 +00:00
|
|
|
} else if (MR.isIndirectSymbol()) {
|
|
|
|
ResultPtr = getPointerToGVIndirectSym(MR.getGlobalValue(),
|
2008-01-04 10:46:51 +00:00
|
|
|
BufferBegin+MR.getMachineCodeOffset(),
|
|
|
|
MR.doesntNeedStub());
|
2008-10-29 23:54:46 +00:00
|
|
|
} else if (MR.isBasicBlock()) {
|
|
|
|
ResultPtr = (void*)getMachineBasicBlockAddress(MR.getBasicBlock());
|
|
|
|
} else if (MR.isConstantPoolIndex()) {
|
|
|
|
ResultPtr = (void*)getConstantPoolEntryAddress(MR.getConstantPoolIndex());
|
|
|
|
} else {
|
|
|
|
assert(MR.isJumpTableIndex());
|
|
|
|
ResultPtr=(void*)getJumpTableEntryAddress(MR.getJumpTableIndex());
|
|
|
|
}
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2008-10-29 23:54:46 +00:00
|
|
|
MR.setResultPointer(ResultPtr);
|
|
|
|
}
|
2005-07-22 20:48:12 +00:00
|
|
|
|
2005-07-28 12:44:13 +00:00
|
|
|
// if we are managing the GOT and the relocation wants an index,
|
|
|
|
// give it one
|
2007-12-05 23:39:57 +00:00
|
|
|
if (MR.isGOTRelative() && MemMgr->isManagingGOT()) {
|
2007-02-24 02:57:03 +00:00
|
|
|
unsigned idx = Resolver.getGOTIndexForAddr(ResultPtr);
|
2005-07-28 12:44:13 +00:00
|
|
|
MR.setGOTIndex(idx);
|
2007-12-05 23:39:57 +00:00
|
|
|
if (((void**)MemMgr->getGOTBase())[idx] != ResultPtr) {
|
2008-11-06 17:46:04 +00:00
|
|
|
DOUT << "JIT: GOT was out of date for " << ResultPtr
|
2007-12-05 23:39:57 +00:00
|
|
|
<< " pointing at " << ((void**)MemMgr->getGOTBase())[idx]
|
2006-12-07 20:04:42 +00:00
|
|
|
<< "\n";
|
2007-12-05 23:39:57 +00:00
|
|
|
((void**)MemMgr->getGOTBase())[idx] = ResultPtr;
|
2005-07-28 12:44:13 +00:00
|
|
|
}
|
2005-07-22 20:48:12 +00:00
|
|
|
}
|
2004-11-20 03:46:14 +00:00
|
|
|
}
|
|
|
|
|
2009-03-05 06:34:37 +00:00
|
|
|
CurFn = 0;
|
2006-05-02 18:27:26 +00:00
|
|
|
TheJIT->getJITInfo().relocate(BufferBegin, &Relocations[0],
|
2007-12-05 23:39:57 +00:00
|
|
|
Relocations.size(), MemMgr->getGOTBase());
|
2004-11-20 03:46:14 +00:00
|
|
|
}
|
|
|
|
|
2006-05-03 18:55:56 +00:00
|
|
|
// Update the GOT entry for F to point to the new code.
|
2007-12-05 23:39:57 +00:00
|
|
|
if (MemMgr->isManagingGOT()) {
|
2007-02-24 02:57:03 +00:00
|
|
|
unsigned idx = Resolver.getGOTIndexForAddr((void*)BufferBegin);
|
2007-12-05 23:39:57 +00:00
|
|
|
if (((void**)MemMgr->getGOTBase())[idx] != (void*)BufferBegin) {
|
2008-11-06 17:46:04 +00:00
|
|
|
DOUT << "JIT: GOT was out of date for " << (void*)BufferBegin
|
2007-12-05 23:39:57 +00:00
|
|
|
<< " pointing at " << ((void**)MemMgr->getGOTBase())[idx] << "\n";
|
|
|
|
((void**)MemMgr->getGOTBase())[idx] = (void*)BufferBegin;
|
2005-07-28 12:44:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-30 23:01:58 +00:00
|
|
|
// CurBufferPtr may have moved beyond FnEnd, due to memory allocation for
|
|
|
|
// global variables that were referenced in the relocations.
|
|
|
|
MemMgr->endFunctionBody(F.getFunction(), BufferBegin, CurBufferPtr);
|
2008-12-10 02:32:19 +00:00
|
|
|
|
|
|
|
if (CurBufferPtr == BufferEnd) {
|
2009-07-23 01:40:54 +00:00
|
|
|
// FIXME: Allocate more space, then try again.
|
|
|
|
llvm_report_error("JIT: Ran out of space for generated machine code!");
|
2008-12-10 02:32:19 +00:00
|
|
|
}
|
|
|
|
|
2008-10-21 11:42:16 +00:00
|
|
|
BufferBegin = CurBufferPtr = 0;
|
|
|
|
NumBytes += FnEnd-FnStart;
|
|
|
|
|
2006-07-25 20:40:54 +00:00
|
|
|
// Invalidate the icache if necessary.
|
2008-06-25 17:18:44 +00:00
|
|
|
sys::Memory::InvalidateInstructionCache(FnStart, FnEnd-FnStart);
|
2009-06-25 02:04:04 +00:00
|
|
|
|
|
|
|
TheJIT->NotifyFunctionEmitted(*F.getFunction(), FnStart, FnEnd-FnStart,
|
2009-07-16 21:07:26 +00:00
|
|
|
EmissionDetails);
|
2006-07-25 20:40:54 +00:00
|
|
|
|
2006-12-07 20:04:42 +00:00
|
|
|
DOUT << "JIT: Finished CodeGen of [" << (void*)FnStart
|
|
|
|
<< "] Function: " << F.getFunction()->getName()
|
|
|
|
<< ": " << (FnEnd-FnStart) << " bytes of text, "
|
|
|
|
<< Relocations.size() << " relocations\n";
|
2009-05-18 21:06:40 +00:00
|
|
|
|
2004-11-20 03:46:14 +00:00
|
|
|
Relocations.clear();
|
2009-03-13 07:51:59 +00:00
|
|
|
ConstPoolAddresses.clear();
|
2007-01-19 17:25:17 +00:00
|
|
|
|
2008-09-18 07:54:21 +00:00
|
|
|
// Mark code region readable and executable if it's not so already.
|
2008-10-03 16:17:20 +00:00
|
|
|
MemMgr->setMemoryExecutable();
|
2008-09-18 07:54:21 +00:00
|
|
|
|
2007-01-20 20:51:43 +00:00
|
|
|
#ifndef NDEBUG
|
2008-11-05 23:44:08 +00:00
|
|
|
{
|
2008-11-12 08:22:43 +00:00
|
|
|
if (sys::hasDisassembler()) {
|
|
|
|
DOUT << "JIT: Disassembled code:\n";
|
2008-11-06 17:46:04 +00:00
|
|
|
DOUT << sys::disassembleBuffer(FnStart, FnEnd-FnStart, (uintptr_t)FnStart);
|
2008-11-12 08:22:43 +00:00
|
|
|
} else {
|
|
|
|
DOUT << "JIT: Binary code:\n";
|
2008-11-05 23:44:08 +00:00
|
|
|
DOUT << std::hex;
|
2009-06-04 00:15:51 +00:00
|
|
|
uint8_t* q = FnStart;
|
2008-11-12 08:22:43 +00:00
|
|
|
for (int i = 0; q < FnEnd; q += 4, ++i) {
|
|
|
|
if (i == 4)
|
|
|
|
i = 0;
|
|
|
|
if (i == 0)
|
|
|
|
DOUT << "JIT: " << std::setw(8) << std::setfill('0')
|
|
|
|
<< (long)(q - FnStart) << ": ";
|
|
|
|
bool Done = false;
|
|
|
|
for (int j = 3; j >= 0; --j) {
|
|
|
|
if (q + j >= FnEnd)
|
|
|
|
Done = true;
|
|
|
|
else
|
|
|
|
DOUT << std::setw(2) << std::setfill('0') << (unsigned short)q[j];
|
|
|
|
}
|
|
|
|
if (Done)
|
|
|
|
break;
|
|
|
|
DOUT << ' ';
|
|
|
|
if (i == 3)
|
2008-11-06 01:18:29 +00:00
|
|
|
DOUT << '\n';
|
2008-11-05 23:44:08 +00:00
|
|
|
}
|
|
|
|
DOUT << std::dec;
|
2008-11-06 01:18:29 +00:00
|
|
|
DOUT<< '\n';
|
2008-11-05 23:44:08 +00:00
|
|
|
}
|
|
|
|
}
|
2007-01-19 17:25:17 +00:00
|
|
|
#endif
|
2008-02-13 18:39:37 +00:00
|
|
|
if (ExceptionHandling) {
|
2008-04-18 20:59:31 +00:00
|
|
|
uintptr_t ActualSize = 0;
|
2008-02-13 18:39:37 +00:00
|
|
|
SavedBufferBegin = BufferBegin;
|
|
|
|
SavedBufferEnd = BufferEnd;
|
|
|
|
SavedCurBufferPtr = CurBufferPtr;
|
2008-04-18 20:59:31 +00:00
|
|
|
|
2008-04-20 17:44:19 +00:00
|
|
|
if (MemMgr->NeedsExactSize()) {
|
|
|
|
ActualSize = DE->GetDwarfTableSizeInBytes(F, *this, FnStart, FnEnd);
|
2008-04-18 20:59:31 +00:00
|
|
|
}
|
2008-02-13 18:39:37 +00:00
|
|
|
|
|
|
|
BufferBegin = CurBufferPtr = MemMgr->startExceptionTable(F.getFunction(),
|
|
|
|
ActualSize);
|
|
|
|
BufferEnd = BufferBegin+ActualSize;
|
2009-06-04 00:15:51 +00:00
|
|
|
uint8_t* FrameRegister = DE->EmitDwarfTable(F, *this, FnStart, FnEnd);
|
2008-03-07 20:05:43 +00:00
|
|
|
MemMgr->endExceptionTable(F.getFunction(), BufferBegin, CurBufferPtr,
|
|
|
|
FrameRegister);
|
2008-02-13 18:39:37 +00:00
|
|
|
BufferBegin = SavedBufferBegin;
|
|
|
|
BufferEnd = SavedBufferEnd;
|
|
|
|
CurBufferPtr = SavedCurBufferPtr;
|
|
|
|
|
|
|
|
TheJIT->RegisterTable(FrameRegister);
|
|
|
|
}
|
2008-09-02 08:14:01 +00:00
|
|
|
|
|
|
|
if (MMI)
|
|
|
|
MMI->EndFunction();
|
2008-02-13 18:39:37 +00:00
|
|
|
|
2006-05-02 18:27:26 +00:00
|
|
|
return false;
|
2002-12-24 00:01:05 +00:00
|
|
|
}
|
|
|
|
|
2009-03-05 06:34:37 +00:00
|
|
|
/// deallocateMemForFunction - Deallocate all memory for the specified
|
|
|
|
/// function body. Also drop any references the function has to stubs.
|
2009-07-23 01:40:54 +00:00
|
|
|
void JITEmitter::deallocateMemForFunction(Function *F) {
|
2009-03-05 06:34:37 +00:00
|
|
|
MemMgr->deallocateMemForFunction(F);
|
|
|
|
|
|
|
|
// If the function did not reference any stubs, return.
|
|
|
|
if (CurFnStubUses.find(F) == CurFnStubUses.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// For each referenced stub, erase the reference to this function, and then
|
|
|
|
// erase the list of referenced stubs.
|
|
|
|
SmallVectorImpl<void *> &StubList = CurFnStubUses[F];
|
|
|
|
for (unsigned i = 0, e = StubList.size(); i != e; ++i) {
|
|
|
|
void *Stub = StubList[i];
|
2009-03-11 07:03:43 +00:00
|
|
|
|
|
|
|
// If we already invalidated this stub for this function, continue.
|
|
|
|
if (StubFnRefs.count(Stub) == 0)
|
|
|
|
continue;
|
|
|
|
|
2009-03-05 06:34:37 +00:00
|
|
|
SmallPtrSet<const Function *, 1> &FnRefs = StubFnRefs[Stub];
|
|
|
|
FnRefs.erase(F);
|
|
|
|
|
|
|
|
// If this function was the last reference to the stub, invalidate the stub
|
|
|
|
// in the JITResolver. Were there a memory manager deallocateStub routine,
|
|
|
|
// we could call that at this point too.
|
|
|
|
if (FnRefs.empty()) {
|
2009-03-07 06:41:19 +00:00
|
|
|
DOUT << "\nJIT: Invalidated Stub at [" << Stub << "]\n";
|
2009-03-11 07:03:43 +00:00
|
|
|
StubFnRefs.erase(Stub);
|
|
|
|
|
|
|
|
// Invalidate the stub. If it is a GV stub, update the JIT's global
|
|
|
|
// mapping for that GV to zero, otherwise, search the string map of
|
|
|
|
// external function names to stubs and remove the entry for this stub.
|
2009-03-07 06:41:19 +00:00
|
|
|
GlobalValue *GV = Resolver.invalidateStub(Stub);
|
2009-03-11 07:03:43 +00:00
|
|
|
if (GV) {
|
|
|
|
TheJIT->updateGlobalMapping(GV, 0);
|
|
|
|
} else {
|
|
|
|
for (StringMapIterator<void*> i = ExtFnStubs.begin(),
|
|
|
|
e = ExtFnStubs.end(); i != e; ++i) {
|
|
|
|
if (i->second == Stub) {
|
|
|
|
ExtFnStubs.erase(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-03-05 06:34:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
CurFnStubUses.erase(F);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-10 02:32:19 +00:00
|
|
|
void* JITEmitter::allocateSpace(uintptr_t Size, unsigned Alignment) {
|
2008-10-21 11:42:16 +00:00
|
|
|
if (BufferBegin)
|
2009-05-30 20:51:52 +00:00
|
|
|
return JITCodeEmitter::allocateSpace(Size, Alignment);
|
2008-10-21 11:42:16 +00:00
|
|
|
|
|
|
|
// create a new memory block if there is no active one.
|
|
|
|
// care must be taken so that BufferBegin is invalidated when a
|
|
|
|
// block is trimmed
|
|
|
|
BufferBegin = CurBufferPtr = MemMgr->allocateSpace(Size, Alignment);
|
|
|
|
BufferEnd = BufferBegin+Size;
|
|
|
|
return CurBufferPtr;
|
|
|
|
}
|
|
|
|
|
2009-07-08 21:59:57 +00:00
|
|
|
void* JITEmitter::allocateGlobal(uintptr_t Size, unsigned Alignment) {
|
|
|
|
// Delegate this call through the memory manager.
|
|
|
|
return MemMgr->allocateGlobal(Size, Alignment);
|
|
|
|
}
|
|
|
|
|
2004-11-22 22:00:25 +00:00
|
|
|
void JITEmitter::emitConstantPool(MachineConstantPool *MCP) {
|
2008-11-07 09:02:17 +00:00
|
|
|
if (TheJIT->getJITInfo().hasCustomConstantPool())
|
2008-10-30 23:44:39 +00:00
|
|
|
return;
|
2008-11-07 09:02:17 +00:00
|
|
|
|
2006-02-09 04:22:52 +00:00
|
|
|
const std::vector<MachineConstantPoolEntry> &Constants = MCP->getConstants();
|
2003-11-30 04:23:21 +00:00
|
|
|
if (Constants.empty()) return;
|
|
|
|
|
2009-03-13 07:51:59 +00:00
|
|
|
unsigned Size = GetConstantPoolSizeInBytes(MCP, TheJIT->getTargetData());
|
|
|
|
unsigned Align = MCP->getConstantPoolAlignment();
|
2008-04-12 00:22:01 +00:00
|
|
|
ConstantPoolBase = allocateSpace(Size, Align);
|
2006-02-09 04:49:59 +00:00
|
|
|
ConstantPool = MCP;
|
2006-05-02 23:22:24 +00:00
|
|
|
|
|
|
|
if (ConstantPoolBase == 0) return; // Buffer overflow.
|
|
|
|
|
2008-04-12 00:22:01 +00:00
|
|
|
DOUT << "JIT: Emitted constant pool at [" << ConstantPoolBase
|
|
|
|
<< "] (size: " << Size << ", alignment: " << Align << ")\n";
|
|
|
|
|
2006-02-09 04:49:59 +00:00
|
|
|
// Initialize the memory for all of the constant pool entries.
|
2009-03-13 07:51:59 +00:00
|
|
|
unsigned Offset = 0;
|
2006-02-09 04:46:04 +00:00
|
|
|
for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
|
2009-03-13 07:51:59 +00:00
|
|
|
MachineConstantPoolEntry CPE = Constants[i];
|
|
|
|
unsigned AlignMask = CPE.getAlignment() - 1;
|
|
|
|
Offset = (Offset + AlignMask) & ~AlignMask;
|
|
|
|
|
|
|
|
uintptr_t CAddr = (uintptr_t)ConstantPoolBase + Offset;
|
|
|
|
ConstPoolAddresses.push_back(CAddr);
|
|
|
|
if (CPE.isMachineConstantPoolEntry()) {
|
2006-09-12 20:59:59 +00:00
|
|
|
// FIXME: add support to lower machine constant pool values into bytes!
|
2009-07-11 13:10:19 +00:00
|
|
|
llvm_report_error("Initialize memory with machine specific constant pool"
|
|
|
|
"entry has not been implemented!");
|
2006-09-12 20:59:59 +00:00
|
|
|
}
|
2009-03-13 07:51:59 +00:00
|
|
|
TheJIT->InitializeMemory(CPE.Val.ConstVal, (void*)CAddr);
|
|
|
|
DOUT << "JIT: CP" << i << " at [0x"
|
|
|
|
<< std::hex << CAddr << std::dec << "]\n";
|
|
|
|
|
|
|
|
const Type *Ty = CPE.Val.ConstVal->getType();
|
2009-05-09 07:06:46 +00:00
|
|
|
Offset += TheJIT->getTargetData()->getTypeAllocSize(Ty);
|
2003-01-13 01:00:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-22 18:53:45 +00:00
|
|
|
void JITEmitter::initJumpTableInfo(MachineJumpTableInfo *MJTI) {
|
2008-11-07 09:02:17 +00:00
|
|
|
if (TheJIT->getJITInfo().hasCustomJumpTables())
|
|
|
|
return;
|
|
|
|
|
2006-04-22 18:53:45 +00:00
|
|
|
const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
|
|
|
|
if (JT.empty()) return;
|
|
|
|
|
2006-05-02 23:22:24 +00:00
|
|
|
unsigned NumEntries = 0;
|
2006-04-22 18:53:45 +00:00
|
|
|
for (unsigned i = 0, e = JT.size(); i != e; ++i)
|
2006-05-02 23:22:24 +00:00
|
|
|
NumEntries += JT[i].MBBs.size();
|
|
|
|
|
|
|
|
unsigned EntrySize = MJTI->getEntrySize();
|
|
|
|
|
2006-04-22 18:53:45 +00:00
|
|
|
// Just allocate space for all the jump tables now. We will fix up the actual
|
|
|
|
// MBB entries in the tables after we emit the code for each block, since then
|
|
|
|
// we will know the final locations of the MBBs in memory.
|
|
|
|
JumpTable = MJTI;
|
2006-05-02 23:22:24 +00:00
|
|
|
JumpTableBase = allocateSpace(NumEntries * EntrySize, MJTI->getAlignment());
|
2006-04-22 18:53:45 +00:00
|
|
|
}
|
|
|
|
|
2006-12-14 22:53:42 +00:00
|
|
|
void JITEmitter::emitJumpTableInfo(MachineJumpTableInfo *MJTI) {
|
2008-11-07 09:02:17 +00:00
|
|
|
if (TheJIT->getJITInfo().hasCustomJumpTables())
|
|
|
|
return;
|
|
|
|
|
2006-04-22 18:53:45 +00:00
|
|
|
const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
|
2006-05-02 23:22:24 +00:00
|
|
|
if (JT.empty() || JumpTableBase == 0) return;
|
2006-04-22 18:53:45 +00:00
|
|
|
|
2006-12-14 22:53:42 +00:00
|
|
|
if (TargetMachine::getRelocationModel() == Reloc::PIC_) {
|
2006-12-14 19:17:33 +00:00
|
|
|
assert(MJTI->getEntrySize() == 4 && "Cross JIT'ing?");
|
|
|
|
// For each jump table, place the offset from the beginning of the table
|
|
|
|
// to the target address.
|
|
|
|
int *SlotPtr = (int*)JumpTableBase;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = JT.size(); i != e; ++i) {
|
|
|
|
const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs;
|
|
|
|
// Store the offset of the basic block for this jump table slot in the
|
|
|
|
// memory we allocated for the jump table in 'initJumpTableInfo'
|
2008-12-10 02:32:19 +00:00
|
|
|
uintptr_t Base = (uintptr_t)SlotPtr;
|
2008-01-05 02:26:58 +00:00
|
|
|
for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) {
|
2008-12-10 02:32:19 +00:00
|
|
|
uintptr_t MBBAddr = getMachineBasicBlockAddress(MBBs[mi]);
|
2008-01-05 02:26:58 +00:00
|
|
|
*SlotPtr++ = TheJIT->getJITInfo().getPICJumpTableEntry(MBBAddr, Base);
|
|
|
|
}
|
2006-12-14 19:17:33 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(MJTI->getEntrySize() == sizeof(void*) && "Cross JIT'ing?");
|
|
|
|
|
|
|
|
// For each jump table, map each target in the jump table to the address of
|
|
|
|
// an emitted MachineBasicBlock.
|
|
|
|
intptr_t *SlotPtr = (intptr_t*)JumpTableBase;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = JT.size(); i != e; ++i) {
|
|
|
|
const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs;
|
|
|
|
// Store the address of the basic block for this jump table slot in the
|
|
|
|
// memory we allocated for the jump table in 'initJumpTableInfo'
|
|
|
|
for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi)
|
|
|
|
*SlotPtr++ = getMachineBasicBlockAddress(MBBs[mi]);
|
|
|
|
}
|
2006-04-22 18:53:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-08 08:02:53 +00:00
|
|
|
void JITEmitter::startGVStub(const GlobalValue* GV, unsigned StubSize,
|
|
|
|
unsigned Alignment) {
|
2006-05-02 18:27:26 +00:00
|
|
|
SavedBufferBegin = BufferBegin;
|
|
|
|
SavedBufferEnd = BufferEnd;
|
|
|
|
SavedCurBufferPtr = CurBufferPtr;
|
|
|
|
|
2008-11-08 08:02:53 +00:00
|
|
|
BufferBegin = CurBufferPtr = MemMgr->allocateStub(GV, StubSize, Alignment);
|
2006-05-02 18:27:26 +00:00
|
|
|
BufferEnd = BufferBegin+StubSize+1;
|
2003-05-09 03:30:07 +00:00
|
|
|
}
|
|
|
|
|
2009-02-18 08:31:02 +00:00
|
|
|
void JITEmitter::startGVStub(const GlobalValue* GV, void *Buffer,
|
|
|
|
unsigned StubSize) {
|
|
|
|
SavedBufferBegin = BufferBegin;
|
|
|
|
SavedBufferEnd = BufferEnd;
|
|
|
|
SavedCurBufferPtr = CurBufferPtr;
|
|
|
|
|
2009-06-04 00:15:51 +00:00
|
|
|
BufferBegin = CurBufferPtr = (uint8_t *)Buffer;
|
2009-02-18 08:31:02 +00:00
|
|
|
BufferEnd = BufferBegin+StubSize+1;
|
|
|
|
}
|
|
|
|
|
2008-11-08 08:02:53 +00:00
|
|
|
void *JITEmitter::finishGVStub(const GlobalValue* GV) {
|
2006-05-02 18:27:26 +00:00
|
|
|
NumBytes += getCurrentPCOffset();
|
|
|
|
std::swap(SavedBufferBegin, BufferBegin);
|
|
|
|
BufferEnd = SavedBufferEnd;
|
|
|
|
CurBufferPtr = SavedCurBufferPtr;
|
|
|
|
return SavedBufferBegin;
|
2003-06-01 23:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry
|
|
|
|
// in the constant pool that was last emitted with the 'emitConstantPool'
|
|
|
|
// method.
|
|
|
|
//
|
2008-12-10 02:32:19 +00:00
|
|
|
uintptr_t JITEmitter::getConstantPoolEntryAddress(unsigned ConstantNum) const {
|
2006-02-09 04:49:59 +00:00
|
|
|
assert(ConstantNum < ConstantPool->getConstants().size() &&
|
2005-04-22 04:08:30 +00:00
|
|
|
"Invalid ConstantPoolIndex!");
|
2009-03-13 07:51:59 +00:00
|
|
|
return ConstPoolAddresses[ConstantNum];
|
2003-06-01 23:24:36 +00:00
|
|
|
}
|
|
|
|
|
2006-04-22 18:53:45 +00:00
|
|
|
// getJumpTableEntryAddress - Return the address of the JumpTable with index
|
|
|
|
// 'Index' in the jumpp table that was last initialized with 'initJumpTableInfo'
|
|
|
|
//
|
2008-12-10 02:32:19 +00:00
|
|
|
uintptr_t JITEmitter::getJumpTableEntryAddress(unsigned Index) const {
|
2006-04-22 18:53:45 +00:00
|
|
|
const std::vector<MachineJumpTableEntry> &JT = JumpTable->getJumpTables();
|
|
|
|
assert(Index < JT.size() && "Invalid jump table index!");
|
|
|
|
|
|
|
|
unsigned Offset = 0;
|
|
|
|
unsigned EntrySize = JumpTable->getEntrySize();
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < Index; ++i)
|
2006-12-14 19:17:33 +00:00
|
|
|
Offset += JT[i].MBBs.size();
|
|
|
|
|
|
|
|
Offset *= EntrySize;
|
2006-04-22 18:53:45 +00:00
|
|
|
|
2008-12-10 02:32:19 +00:00
|
|
|
return (uintptr_t)((char *)JumpTableBase + Offset);
|
2006-04-22 18:53:45 +00:00
|
|
|
}
|
|
|
|
|
2006-05-11 23:08:08 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Public interface to this file
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-05-30 20:51:52 +00:00
|
|
|
JITCodeEmitter *JIT::createEmitter(JIT &jit, JITMemoryManager *JMM) {
|
2007-12-06 01:08:09 +00:00
|
|
|
return new JITEmitter(jit, JMM);
|
2006-05-11 23:08:08 +00:00
|
|
|
}
|
|
|
|
|
2003-07-28 19:09:06 +00:00
|
|
|
// getPointerToNamedFunction - This function is used as a global wrapper to
|
2003-12-20 01:46:27 +00:00
|
|
|
// JIT::getPointerToNamedFunction for the purpose of resolving symbols when
|
2003-07-28 19:09:06 +00:00
|
|
|
// bugpoint is debugging the JIT. In that scenario, we are loading an .so and
|
|
|
|
// need to resolve function(s) that are being mis-codegenerated, so we need to
|
|
|
|
// resolve their addresses at runtime, and this is the way to do it.
|
|
|
|
extern "C" {
|
|
|
|
void *getPointerToNamedFunction(const char *Name) {
|
2006-08-16 01:24:12 +00:00
|
|
|
if (Function *F = TheJIT->FindFunctionNamed(Name))
|
2003-12-20 01:46:27 +00:00
|
|
|
return TheJIT->getPointerToFunction(F);
|
|
|
|
return TheJIT->getPointerToNamedFunction(Name);
|
2003-07-28 19:09:06 +00:00
|
|
|
}
|
|
|
|
}
|
2006-05-11 23:08:08 +00:00
|
|
|
|
|
|
|
// getPointerToFunctionOrStub - If the specified function has been
|
|
|
|
// code-gen'd, return a pointer to the function. If not, compile it, or use
|
|
|
|
// a stub to implement lazy compilation if available.
|
|
|
|
//
|
|
|
|
void *JIT::getPointerToFunctionOrStub(Function *F) {
|
|
|
|
// If we have already code generated the function, just return the address.
|
|
|
|
if (void *Addr = getPointerToGlobalIfAvailable(F))
|
|
|
|
return Addr;
|
|
|
|
|
2007-02-24 02:57:03 +00:00
|
|
|
// Get a stub if the target supports it.
|
2009-05-30 20:51:52 +00:00
|
|
|
assert(isa<JITEmitter>(JCE) && "Unexpected MCE?");
|
2008-08-20 00:28:12 +00:00
|
|
|
JITEmitter *JE = cast<JITEmitter>(getCodeEmitter());
|
2007-02-24 02:57:03 +00:00
|
|
|
return JE->getJITResolver().getFunctionStub(F);
|
2006-05-11 23:08:08 +00:00
|
|
|
}
|
|
|
|
|
2009-02-18 08:31:02 +00:00
|
|
|
void JIT::updateFunctionStub(Function *F) {
|
|
|
|
// Get the empty stub we generated earlier.
|
2009-05-30 20:51:52 +00:00
|
|
|
assert(isa<JITEmitter>(JCE) && "Unexpected MCE?");
|
2009-02-18 08:31:02 +00:00
|
|
|
JITEmitter *JE = cast<JITEmitter>(getCodeEmitter());
|
|
|
|
void *Stub = JE->getJITResolver().getFunctionStub(F);
|
|
|
|
|
|
|
|
// Tell the target jit info to rewrite the stub at the specified address,
|
|
|
|
// rather than creating a new one.
|
|
|
|
void *Addr = getPointerToGlobalIfAvailable(F);
|
|
|
|
getJITInfo().emitFunctionStubAtAddr(F, Addr, Stub, *getCodeEmitter());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// updateDlsymStubTable - Emit the data necessary to relocate the stubs
|
|
|
|
/// that were emitted during code generation.
|
|
|
|
///
|
|
|
|
void JIT::updateDlsymStubTable() {
|
2009-05-30 20:51:52 +00:00
|
|
|
assert(isa<JITEmitter>(JCE) && "Unexpected MCE?");
|
2009-02-18 08:31:02 +00:00
|
|
|
JITEmitter *JE = cast<JITEmitter>(getCodeEmitter());
|
|
|
|
|
|
|
|
SmallVector<GlobalValue*, 8> GVs;
|
|
|
|
SmallVector<void*, 8> Ptrs;
|
2009-03-11 07:03:43 +00:00
|
|
|
const StringMap<void *> &ExtFns = JE->getExternalFnStubs();
|
2009-02-18 08:31:02 +00:00
|
|
|
|
|
|
|
JE->getJITResolver().getRelocatableGVs(GVs, Ptrs);
|
|
|
|
|
2009-03-11 07:03:43 +00:00
|
|
|
unsigned nStubs = GVs.size() + ExtFns.size();
|
|
|
|
|
2009-02-18 08:31:02 +00:00
|
|
|
// If there are no relocatable stubs, return.
|
2009-03-11 07:03:43 +00:00
|
|
|
if (nStubs == 0)
|
2009-02-18 08:31:02 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// If there are no new relocatable stubs, return.
|
|
|
|
void *CurTable = JE->getMemMgr()->getDlsymTable();
|
2009-03-11 07:03:43 +00:00
|
|
|
if (CurTable && (*(unsigned *)CurTable == nStubs))
|
2009-02-18 08:31:02 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Calculate the size of the stub info
|
2009-03-11 07:03:43 +00:00
|
|
|
unsigned offset = 4 + 4 * nStubs + sizeof(intptr_t) * nStubs;
|
2009-02-18 08:31:02 +00:00
|
|
|
|
|
|
|
SmallVector<unsigned, 8> Offsets;
|
|
|
|
for (unsigned i = 0; i != GVs.size(); ++i) {
|
|
|
|
Offsets.push_back(offset);
|
2009-07-21 08:54:24 +00:00
|
|
|
offset += GVs[i]->getName().size() + 1;
|
2009-02-18 08:31:02 +00:00
|
|
|
}
|
2009-03-11 07:03:43 +00:00
|
|
|
for (StringMapConstIterator<void*> i = ExtFns.begin(), e = ExtFns.end();
|
|
|
|
i != e; ++i) {
|
|
|
|
Offsets.push_back(offset);
|
|
|
|
offset += strlen(i->first()) + 1;
|
|
|
|
}
|
2009-02-18 08:31:02 +00:00
|
|
|
|
2009-03-05 06:34:37 +00:00
|
|
|
// Allocate space for the new "stub", which contains the dlsym table.
|
2009-02-18 08:31:02 +00:00
|
|
|
JE->startGVStub(0, offset, 4);
|
|
|
|
|
|
|
|
// Emit the number of records
|
2009-05-30 20:51:52 +00:00
|
|
|
JE->emitInt32(nStubs);
|
2009-02-18 08:31:02 +00:00
|
|
|
|
|
|
|
// Emit the string offsets
|
2009-03-11 07:03:43 +00:00
|
|
|
for (unsigned i = 0; i != nStubs; ++i)
|
2009-05-30 20:51:52 +00:00
|
|
|
JE->emitInt32(Offsets[i]);
|
2009-02-18 08:31:02 +00:00
|
|
|
|
2009-03-04 19:10:38 +00:00
|
|
|
// Emit the pointers. Verify that they are at least 2-byte aligned, and set
|
|
|
|
// the low bit to 0 == GV, 1 == Function, so that the client code doing the
|
|
|
|
// relocation can write the relocated pointer at the appropriate place in
|
|
|
|
// the stub.
|
|
|
|
for (unsigned i = 0; i != GVs.size(); ++i) {
|
|
|
|
intptr_t Ptr = (intptr_t)Ptrs[i];
|
|
|
|
assert((Ptr & 1) == 0 && "Stub pointers must be at least 2-byte aligned!");
|
|
|
|
|
|
|
|
if (isa<Function>(GVs[i]))
|
|
|
|
Ptr |= (intptr_t)1;
|
|
|
|
|
2009-03-11 07:03:43 +00:00
|
|
|
if (sizeof(Ptr) == 8)
|
2009-05-30 20:51:52 +00:00
|
|
|
JE->emitInt64(Ptr);
|
2009-03-11 07:03:43 +00:00
|
|
|
else
|
2009-05-30 20:51:52 +00:00
|
|
|
JE->emitInt32(Ptr);
|
2009-03-11 07:03:43 +00:00
|
|
|
}
|
|
|
|
for (StringMapConstIterator<void*> i = ExtFns.begin(), e = ExtFns.end();
|
|
|
|
i != e; ++i) {
|
|
|
|
intptr_t Ptr = (intptr_t)i->second | 1;
|
|
|
|
|
|
|
|
if (sizeof(Ptr) == 8)
|
2009-05-30 20:51:52 +00:00
|
|
|
JE->emitInt64(Ptr);
|
2009-02-18 08:31:02 +00:00
|
|
|
else
|
2009-05-30 20:51:52 +00:00
|
|
|
JE->emitInt32(Ptr);
|
2009-03-04 19:10:38 +00:00
|
|
|
}
|
2009-02-18 08:31:02 +00:00
|
|
|
|
2009-03-05 06:34:37 +00:00
|
|
|
// Emit the strings.
|
2009-02-18 08:31:02 +00:00
|
|
|
for (unsigned i = 0; i != GVs.size(); ++i)
|
2009-05-30 20:51:52 +00:00
|
|
|
JE->emitString(GVs[i]->getName());
|
2009-03-11 07:03:43 +00:00
|
|
|
for (StringMapConstIterator<void*> i = ExtFns.begin(), e = ExtFns.end();
|
|
|
|
i != e; ++i)
|
2009-05-30 20:51:52 +00:00
|
|
|
JE->emitString(i->first());
|
2009-02-18 08:31:02 +00:00
|
|
|
|
2009-03-05 06:34:37 +00:00
|
|
|
// Tell the JIT memory manager where it is. The JIT Memory Manager will
|
|
|
|
// deallocate space for the old one, if one existed.
|
2009-02-18 08:31:02 +00:00
|
|
|
JE->getMemMgr()->SetDlsymTable(JE->finishGVStub(0));
|
|
|
|
}
|
|
|
|
|
2006-05-11 23:08:08 +00:00
|
|
|
/// freeMachineCodeForFunction - release machine code memory for given Function.
|
|
|
|
///
|
|
|
|
void JIT::freeMachineCodeForFunction(Function *F) {
|
2008-08-07 01:30:15 +00:00
|
|
|
|
2006-05-11 23:08:08 +00:00
|
|
|
// Delete translation for this from the ExecutionEngine, so it will get
|
|
|
|
// retranslated next time it is used.
|
2008-04-04 05:51:42 +00:00
|
|
|
void *OldPtr = updateGlobalMapping(F, 0);
|
|
|
|
|
|
|
|
if (OldPtr)
|
2009-06-25 02:04:04 +00:00
|
|
|
TheJIT->NotifyFreeingMachineCode(*F, OldPtr);
|
2006-05-11 23:08:08 +00:00
|
|
|
|
|
|
|
// Free the actual memory for the function body and related stuff.
|
2009-05-30 20:51:52 +00:00
|
|
|
assert(isa<JITEmitter>(JCE) && "Unexpected MCE?");
|
|
|
|
cast<JITEmitter>(JCE)->deallocateMemForFunction(F);
|
2006-05-11 23:08:08 +00:00
|
|
|
}
|