blob: 70479fcaa826bda3207299004fd6ec49b480bdba [file] [log] [blame]
Chris Lattner166f2262004-11-22 22:00:25 +00001//===-- JITEmitter.cpp - Write machine code to executable memory ----------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerbd199fb2002-12-24 00:01:05 +00009//
Chris Lattner5be478f2004-11-20 03:46:14 +000010// This file defines a MachineCodeEmitter object that is used by the JIT to
11// write machine code to memory and remember where relocatable values are.
Chris Lattnerbd199fb2002-12-24 00:01:05 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattner3785fad2003-08-05 17:00:32 +000015#define DEBUG_TYPE "jit"
Chris Lattner4d326fa2003-12-20 01:46:27 +000016#include "JIT.h"
Chris Lattner2c0a6a12003-11-30 04:23:21 +000017#include "llvm/Constant.h"
18#include "llvm/Module.h"
Chris Lattner5b3a4552005-03-17 15:38:16 +000019#include "llvm/Type.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000020#include "llvm/CodeGen/MachineCodeEmitter.h"
21#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner1cc08382003-01-13 01:00:12 +000022#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner5be478f2004-11-20 03:46:14 +000023#include "llvm/CodeGen/MachineRelocation.h"
Chris Lattner1cc08382003-01-13 01:00:12 +000024#include "llvm/Target/TargetData.h"
Chris Lattner5be478f2004-11-20 03:46:14 +000025#include "llvm/Target/TargetJITInfo.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/Support/Debug.h"
27#include "llvm/ADT/Statistic.h"
Reid Spencer52b0ba62004-09-11 04:31:03 +000028#include "llvm/System/Memory.h"
Chris Lattnerc19aade2003-12-08 08:06:28 +000029using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000030
Chris Lattnerbd199fb2002-12-24 00:01:05 +000031namespace {
Chris Lattnere7386562003-10-20 05:45:49 +000032 Statistic<> NumBytes("jit", "Number of bytes of machine code compiled");
Chris Lattnere884dc22005-07-20 16:29:20 +000033 Statistic<> NumRelos("jit", "Number of relocations applied");
Chris Lattner4d326fa2003-12-20 01:46:27 +000034 JIT *TheJIT = 0;
Chris Lattner54266522004-11-20 23:57:07 +000035}
Chris Lattnerbd199fb2002-12-24 00:01:05 +000036
Chris Lattner54266522004-11-20 23:57:07 +000037
38//===----------------------------------------------------------------------===//
39// JITMemoryManager code.
40//
41namespace {
Chris Lattner688506d2003-08-14 18:35:27 +000042 /// JITMemoryManager - Manage memory for the JIT code generation in a logical,
43 /// sane way. This splits a large block of MAP_NORESERVE'd memory into two
44 /// sections, one for function stubs, one for the functions themselves. We
45 /// have to do this because we may need to emit a function stub while in the
46 /// middle of emitting a function, and we don't know how large the function we
47 /// are emitting is. This never bothers to release the memory, because when
48 /// we are ready to destroy the JIT, the program exits.
49 class JITMemoryManager {
Reid Spencer33189e72004-09-13 22:38:11 +000050 sys::MemoryBlock MemBlock; // Virtual memory block allocated RWX
Chris Lattner688506d2003-08-14 18:35:27 +000051 unsigned char *MemBase; // Base of block of memory, start of stub mem
52 unsigned char *FunctionBase; // Start of the function body area
Chris Lattner281a6012005-01-10 18:23:22 +000053 unsigned char *ConstantPool; // Memory allocated for constant pools
54 unsigned char *CurStubPtr, *CurFunctionPtr, *CurConstantPtr;
Andrew Lenharth16ec33c2005-07-22 20:48:12 +000055 unsigned char *GOTBase; //Target Specific reserved memory
Chris Lattner688506d2003-08-14 18:35:27 +000056 public:
Andrew Lenharth16ec33c2005-07-22 20:48:12 +000057 JITMemoryManager(bool useGOT);
Reid Spencer4af3da62004-12-13 16:04:04 +000058 ~JITMemoryManager();
Misha Brukmanf976c852005-04-21 22:55:34 +000059
Chris Lattner688506d2003-08-14 18:35:27 +000060 inline unsigned char *allocateStub(unsigned StubSize);
Chris Lattner281a6012005-01-10 18:23:22 +000061 inline unsigned char *allocateConstant(unsigned ConstantSize,
62 unsigned Alignment);
Chris Lattner688506d2003-08-14 18:35:27 +000063 inline unsigned char *startFunctionBody();
Chris Lattner281a6012005-01-10 18:23:22 +000064 inline void endFunctionBody(unsigned char *FunctionEnd);
Andrew Lenharth16ec33c2005-07-22 20:48:12 +000065 inline unsigned char* getGOTBase() const;
66
67 inline bool isManagingGOT() const;
Chris Lattner688506d2003-08-14 18:35:27 +000068 };
69}
70
Andrew Lenharth16ec33c2005-07-22 20:48:12 +000071JITMemoryManager::JITMemoryManager(bool useGOT) {
Chris Lattner688506d2003-08-14 18:35:27 +000072 // Allocate a 16M block of memory...
Reid Spencer33189e72004-09-13 22:38:11 +000073 MemBlock = sys::Memory::AllocateRWX((16 << 20));
Reid Spencer52b0ba62004-09-11 04:31:03 +000074 MemBase = reinterpret_cast<unsigned char*>(MemBlock.base());
Andrew Lenharth16ec33c2005-07-22 20:48:12 +000075 ConstantPool = MemBase;
76 GOTBase = ConstantPool + 512*1024; //512 for constants
77 //8k number of entries in the GOT
78 FunctionBase = GOTBase + 8192 * sizeof(void*) + 512*1024; // Use 512k for stubs
79
80 //make it easier to tell if we are managing the GOT
81 if (!useGOT)
82 GOTBase = NULL;
Chris Lattner688506d2003-08-14 18:35:27 +000083
84 // Allocate stubs backwards from the function base, allocate functions forward
85 // from the function base.
86 CurStubPtr = CurFunctionPtr = FunctionBase;
Chris Lattner281a6012005-01-10 18:23:22 +000087
Chris Lattner281a6012005-01-10 18:23:22 +000088 CurConstantPtr = ConstantPool + 512*1024;
Chris Lattner688506d2003-08-14 18:35:27 +000089}
90
Reid Spencer4af3da62004-12-13 16:04:04 +000091JITMemoryManager::~JITMemoryManager() {
92 sys::Memory::ReleaseRWX(MemBlock);
93}
94
Chris Lattner688506d2003-08-14 18:35:27 +000095unsigned char *JITMemoryManager::allocateStub(unsigned StubSize) {
96 CurStubPtr -= StubSize;
97 if (CurStubPtr < MemBase) {
98 std::cerr << "JIT ran out of memory for function stubs!\n";
99 abort();
100 }
101 return CurStubPtr;
102}
103
Chris Lattner281a6012005-01-10 18:23:22 +0000104unsigned char *JITMemoryManager::allocateConstant(unsigned ConstantSize,
105 unsigned Alignment) {
106 // Reserve space and align pointer.
107 CurConstantPtr -= ConstantSize;
108 CurConstantPtr =
109 (unsigned char *)((intptr_t)CurConstantPtr & ~((intptr_t)Alignment - 1));
110
111 if (CurConstantPtr < ConstantPool) {
112 std::cerr << "JIT ran out of memory for constant pools!\n";
113 abort();
114 }
115 return CurConstantPtr;
116}
117
Chris Lattner688506d2003-08-14 18:35:27 +0000118unsigned char *JITMemoryManager::startFunctionBody() {
Chris Lattner5be478f2004-11-20 03:46:14 +0000119 // Round up to an even multiple of 8 bytes, this should eventually be target
Chris Lattner688506d2003-08-14 18:35:27 +0000120 // specific.
Chris Lattner5be478f2004-11-20 03:46:14 +0000121 return (unsigned char*)(((intptr_t)CurFunctionPtr + 7) & ~7);
Chris Lattner688506d2003-08-14 18:35:27 +0000122}
123
124void JITMemoryManager::endFunctionBody(unsigned char *FunctionEnd) {
125 assert(FunctionEnd > CurFunctionPtr);
126 CurFunctionPtr = FunctionEnd;
127}
128
Andrew Lenharth16ec33c2005-07-22 20:48:12 +0000129unsigned char* JITMemoryManager::getGOTBase() const {
130 return GOTBase;
131}
132
133bool JITMemoryManager::isManagingGOT() const {
134 return GOTBase != NULL;
135}
136
Chris Lattner54266522004-11-20 23:57:07 +0000137//===----------------------------------------------------------------------===//
138// JIT lazy compilation code.
139//
140namespace {
Reid Spenceree448632005-07-12 15:51:55 +0000141 class JITResolverState {
142 private:
143 /// FunctionToStubMap - Keep track of the stub created for a particular
144 /// function so that we can reuse them if necessary.
145 std::map<Function*, void*> FunctionToStubMap;
146
147 /// StubToFunctionMap - Keep track of the function that each stub
148 /// corresponds to.
149 std::map<void*, Function*> StubToFunctionMap;
150
151 public:
152 std::map<Function*, void*>& getFunctionToStubMap(const MutexGuard& locked) {
153 assert(locked.holds(TheJIT->lock));
154 return FunctionToStubMap;
155 }
156
157 std::map<void*, Function*>& getStubToFunctionMap(const MutexGuard& locked) {
158 assert(locked.holds(TheJIT->lock));
159 return StubToFunctionMap;
160 }
161 };
162
Chris Lattner54266522004-11-20 23:57:07 +0000163 /// JITResolver - Keep track of, and resolve, call sites for functions that
164 /// have not yet been compiled.
165 class JITResolver {
Chris Lattner5e225582004-11-21 03:37:42 +0000166 /// MCE - The MachineCodeEmitter to use to emit stubs with.
Chris Lattner54266522004-11-20 23:57:07 +0000167 MachineCodeEmitter &MCE;
168
Chris Lattner5e225582004-11-21 03:37:42 +0000169 /// LazyResolverFn - The target lazy resolver function that we actually
170 /// rewrite instructions to use.
171 TargetJITInfo::LazyResolverFn LazyResolverFn;
172
Reid Spenceree448632005-07-12 15:51:55 +0000173 JITResolverState state;
Chris Lattner54266522004-11-20 23:57:07 +0000174
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000175 /// ExternalFnToStubMap - This is the equivalent of FunctionToStubMap for
176 /// external functions.
177 std::map<void*, void*> ExternalFnToStubMap;
Chris Lattner54266522004-11-20 23:57:07 +0000178 public:
Chris Lattner5e225582004-11-21 03:37:42 +0000179 JITResolver(MachineCodeEmitter &mce) : MCE(mce) {
180 LazyResolverFn =
181 TheJIT->getJITInfo().getLazyResolverFunction(JITCompilerFn);
182 }
Chris Lattner54266522004-11-20 23:57:07 +0000183
184 /// getFunctionStub - This returns a pointer to a function stub, creating
185 /// one on demand as needed.
186 void *getFunctionStub(Function *F);
187
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000188 /// getExternalFunctionStub - Return a stub for the function at the
189 /// specified address, created lazily on demand.
190 void *getExternalFunctionStub(void *FnAddr);
191
Chris Lattner5e225582004-11-21 03:37:42 +0000192 /// AddCallbackAtLocation - If the target is capable of rewriting an
193 /// instruction without the use of a stub, record the location of the use so
194 /// we know which function is being used at the location.
195 void *AddCallbackAtLocation(Function *F, void *Location) {
Reid Spenceree448632005-07-12 15:51:55 +0000196 MutexGuard locked(TheJIT->lock);
Chris Lattner5e225582004-11-21 03:37:42 +0000197 /// Get the target-specific JIT resolver function.
Reid Spenceree448632005-07-12 15:51:55 +0000198 state.getStubToFunctionMap(locked)[Location] = F;
Chris Lattner5e225582004-11-21 03:37:42 +0000199 return (void*)LazyResolverFn;
200 }
201
Chris Lattner54266522004-11-20 23:57:07 +0000202 /// JITCompilerFn - This function is called to resolve a stub to a compiled
203 /// address. If the LLVM Function corresponding to the stub has not yet
204 /// been compiled, this function compiles it first.
205 static void *JITCompilerFn(void *Stub);
206 };
207}
208
209/// getJITResolver - This function returns the one instance of the JIT resolver.
210///
211static JITResolver &getJITResolver(MachineCodeEmitter *MCE = 0) {
212 static JITResolver TheJITResolver(*MCE);
213 return TheJITResolver;
214}
215
216/// getFunctionStub - This returns a pointer to a function stub, creating
217/// one on demand as needed.
218void *JITResolver::getFunctionStub(Function *F) {
Reid Spenceree448632005-07-12 15:51:55 +0000219 MutexGuard locked(TheJIT->lock);
220
Chris Lattner54266522004-11-20 23:57:07 +0000221 // If we already have a stub for this function, recycle it.
Reid Spenceree448632005-07-12 15:51:55 +0000222 void *&Stub = state.getFunctionToStubMap(locked)[F];
Chris Lattner54266522004-11-20 23:57:07 +0000223 if (Stub) return Stub;
224
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000225 // Call the lazy resolver function unless we already KNOW it is an external
226 // function, in which case we just skip the lazy resolution step.
227 void *Actual = (void*)LazyResolverFn;
Chris Lattner69435702005-02-20 18:43:35 +0000228 if (F->isExternal() && F->hasExternalLinkage())
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000229 Actual = TheJIT->getPointerToFunction(F);
Misha Brukmanf976c852005-04-21 22:55:34 +0000230
Chris Lattner54266522004-11-20 23:57:07 +0000231 // Otherwise, codegen a new stub. For now, the stub will call the lazy
232 // resolver function.
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000233 Stub = TheJIT->getJITInfo().emitFunctionStub(Actual, MCE);
234
Chris Lattner69435702005-02-20 18:43:35 +0000235 if (Actual != (void*)LazyResolverFn) {
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000236 // If we are getting the stub for an external function, we really want the
237 // address of the stub in the GlobalAddressMap for the JIT, not the address
238 // of the external function.
239 TheJIT->updateGlobalMapping(F, Stub);
240 }
Chris Lattner54266522004-11-20 23:57:07 +0000241
Chris Lattnercb479412004-11-21 03:44:32 +0000242 DEBUG(std::cerr << "JIT: Stub emitted at [" << Stub << "] for function '"
Chris Lattner6f717202004-11-22 21:48:33 +0000243 << F->getName() << "'\n");
Chris Lattnercb479412004-11-21 03:44:32 +0000244
Chris Lattner54266522004-11-20 23:57:07 +0000245 // Finally, keep track of the stub-to-Function mapping so that the
246 // JITCompilerFn knows which function to compile!
Reid Spenceree448632005-07-12 15:51:55 +0000247 state.getStubToFunctionMap(locked)[Stub] = F;
Chris Lattner54266522004-11-20 23:57:07 +0000248 return Stub;
249}
250
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000251/// getExternalFunctionStub - Return a stub for the function at the
252/// specified address, created lazily on demand.
253void *JITResolver::getExternalFunctionStub(void *FnAddr) {
254 // If we already have a stub for this function, recycle it.
255 void *&Stub = ExternalFnToStubMap[FnAddr];
256 if (Stub) return Stub;
257
258 Stub = TheJIT->getJITInfo().emitFunctionStub(FnAddr, MCE);
259 DEBUG(std::cerr << "JIT: Stub emitted at [" << Stub
260 << "] for external function at '" << FnAddr << "'\n");
261 return Stub;
262}
263
264
Chris Lattner54266522004-11-20 23:57:07 +0000265/// JITCompilerFn - This function is called when a lazy compilation stub has
266/// been entered. It looks up which function this stub corresponds to, compiles
267/// it if necessary, then returns the resultant function pointer.
268void *JITResolver::JITCompilerFn(void *Stub) {
269 JITResolver &JR = getJITResolver();
Misha Brukmanf976c852005-04-21 22:55:34 +0000270
Reid Spenceree448632005-07-12 15:51:55 +0000271 MutexGuard locked(TheJIT->lock);
272
Chris Lattner54266522004-11-20 23:57:07 +0000273 // The address given to us for the stub may not be exactly right, it might be
274 // a little bit after the stub. As such, use upper_bound to find it.
275 std::map<void*, Function*>::iterator I =
Reid Spenceree448632005-07-12 15:51:55 +0000276 JR.state.getStubToFunctionMap(locked).upper_bound(Stub);
277 assert(I != JR.state.getStubToFunctionMap(locked).begin() && "This is not a known stub!");
Chris Lattner54266522004-11-20 23:57:07 +0000278 Function *F = (--I)->second;
279
Reid Spenceree448632005-07-12 15:51:55 +0000280 // We might like to remove the stub from the StubToFunction map.
281 // We can't do that! Multiple threads could be stuck, waiting to acquire the
282 // lock above. As soon as the 1st function finishes compiling the function,
283 // the next one will be released, and needs to be able to find the function it needs
284 // to call.
285 //JR.state.getStubToFunctionMap(locked).erase(I);
Chris Lattner54266522004-11-20 23:57:07 +0000286
Chris Lattnercb479412004-11-21 03:44:32 +0000287 DEBUG(std::cerr << "JIT: Lazily resolving function '" << F->getName()
Chris Lattner54266522004-11-20 23:57:07 +0000288 << "' In stub ptr = " << Stub << " actual ptr = "
289 << I->first << "\n");
290
291 void *Result = TheJIT->getPointerToFunction(F);
292
293 // We don't need to reuse this stub in the future, as F is now compiled.
Reid Spenceree448632005-07-12 15:51:55 +0000294 JR.state.getFunctionToStubMap(locked).erase(F);
Chris Lattner54266522004-11-20 23:57:07 +0000295
296 // FIXME: We could rewrite all references to this stub if we knew them.
297 return Result;
298}
Chris Lattner688506d2003-08-14 18:35:27 +0000299
300
Chris Lattnere518b712004-12-05 07:19:16 +0000301// getPointerToFunctionOrStub - If the specified function has been
302// code-gen'd, return a pointer to the function. If not, compile it, or use
303// a stub to implement lazy compilation if available.
304//
305void *JIT::getPointerToFunctionOrStub(Function *F) {
306 // If we have already code generated the function, just return the address.
307 if (void *Addr = getPointerToGlobalIfAvailable(F))
308 return Addr;
309
310 // Get a stub if the target supports it
311 return getJITResolver(MCE).getFunctionStub(F);
312}
313
314
315
Chris Lattner54266522004-11-20 23:57:07 +0000316//===----------------------------------------------------------------------===//
Chris Lattner166f2262004-11-22 22:00:25 +0000317// JITEmitter code.
Chris Lattner54266522004-11-20 23:57:07 +0000318//
Chris Lattner688506d2003-08-14 18:35:27 +0000319namespace {
Chris Lattner166f2262004-11-22 22:00:25 +0000320 /// JITEmitter - The JIT implementation of the MachineCodeEmitter, which is
321 /// used to output functions to memory for execution.
322 class JITEmitter : public MachineCodeEmitter {
Chris Lattner688506d2003-08-14 18:35:27 +0000323 JITMemoryManager MemMgr;
324
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000325 // CurBlock - The start of the current block of memory. CurByte - The
326 // current byte being emitted to.
Chris Lattner6125fdd2003-05-09 03:30:07 +0000327 unsigned char *CurBlock, *CurByte;
328
329 // When outputting a function stub in the context of some other function, we
330 // save CurBlock and CurByte here.
331 unsigned char *SavedCurBlock, *SavedCurByte;
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000332
333 // ConstantPoolAddresses - Contains the location for each entry in the
334 // constant pool.
Chris Lattner1cc08382003-01-13 01:00:12 +0000335 std::vector<void*> ConstantPoolAddresses;
Chris Lattner5be478f2004-11-20 03:46:14 +0000336
337 /// Relocations - These are the relocations that the function needs, as
338 /// emitted.
339 std::vector<MachineRelocation> Relocations;
Andrew Lenharth16ec33c2005-07-22 20:48:12 +0000340
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000341 public:
Andrew Lenharth16ec33c2005-07-22 20:48:12 +0000342 JITEmitter(JIT &jit)
343 :MemMgr(jit.getJITInfo().needsGOT()),
344 nextGOTIndex(0)
345 {
346 TheJIT = &jit;
347 DEBUG(std::cerr <<
348 (MemMgr.isManagingGOT() ? "JIT is managing GOT\n"
349 : "JIT is not managing GOT\n"));
350 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000351
352 virtual void startFunction(MachineFunction &F);
353 virtual void finishFunction(MachineFunction &F);
Chris Lattner1cc08382003-01-13 01:00:12 +0000354 virtual void emitConstantPool(MachineConstantPool *MCP);
Chris Lattner54266522004-11-20 23:57:07 +0000355 virtual void startFunctionStub(unsigned StubSize);
356 virtual void* finishFunctionStub(const Function *F);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000357 virtual void emitByte(unsigned char B);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000358 virtual void emitWord(unsigned W);
Brian Gaekeaea1b582004-04-23 17:11:14 +0000359 virtual void emitWordAt(unsigned W, unsigned *Ptr);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000360
Chris Lattner5be478f2004-11-20 03:46:14 +0000361 virtual void addRelocation(const MachineRelocation &MR) {
362 Relocations.push_back(MR);
363 }
364
365 virtual uint64_t getCurrentPCValue();
366 virtual uint64_t getCurrentPCOffset();
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000367 virtual uint64_t getConstantPoolEntryAddress(unsigned Entry);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000368
Chris Lattner54266522004-11-20 23:57:07 +0000369 private:
Chris Lattner5e225582004-11-21 03:37:42 +0000370 void *getPointerToGlobal(GlobalValue *GV, void *Reference, bool NoNeedStub);
Andrew Lenharth16ec33c2005-07-22 20:48:12 +0000371 unsigned nextGOTIndex;
372 std::map<void*, unsigned> revGOTMap;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000373 };
374}
375
Chris Lattner4d326fa2003-12-20 01:46:27 +0000376MachineCodeEmitter *JIT::createEmitter(JIT &jit) {
Chris Lattner166f2262004-11-22 22:00:25 +0000377 return new JITEmitter(jit);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000378}
379
Chris Lattner166f2262004-11-22 22:00:25 +0000380void *JITEmitter::getPointerToGlobal(GlobalValue *V, void *Reference,
381 bool DoesntNeedStub) {
Chris Lattner54266522004-11-20 23:57:07 +0000382 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
383 /// FIXME: If we straightened things out, this could actually emit the
384 /// global immediately instead of queuing it for codegen later!
Chris Lattner54266522004-11-20 23:57:07 +0000385 return TheJIT->getOrEmitGlobalVariable(GV);
386 }
387
388 // If we have already compiled the function, return a pointer to its body.
389 Function *F = cast<Function>(V);
390 void *ResultPtr = TheJIT->getPointerToGlobalIfAvailable(F);
391 if (ResultPtr) return ResultPtr;
392
Chris Lattner532343b2004-11-30 17:41:49 +0000393 if (F->hasExternalLinkage() && F->isExternal()) {
Chris Lattner54266522004-11-20 23:57:07 +0000394 // If this is an external function pointer, we can force the JIT to
395 // 'compile' it, which really just adds it to the map.
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000396 if (DoesntNeedStub)
397 return TheJIT->getPointerToFunction(F);
398
399 return getJITResolver(this).getFunctionStub(F);
Chris Lattner54266522004-11-20 23:57:07 +0000400 }
401
Chris Lattner5e225582004-11-21 03:37:42 +0000402 // Okay, the function has not been compiled yet, if the target callback
403 // mechanism is capable of rewriting the instruction directly, prefer to do
404 // that instead of emitting a stub.
405 if (DoesntNeedStub)
406 return getJITResolver(this).AddCallbackAtLocation(F, Reference);
407
Chris Lattner54266522004-11-20 23:57:07 +0000408 // Otherwise, we have to emit a lazy resolving stub.
409 return getJITResolver(this).getFunctionStub(F);
410}
411
Chris Lattner166f2262004-11-22 22:00:25 +0000412void JITEmitter::startFunction(MachineFunction &F) {
Chris Lattner688506d2003-08-14 18:35:27 +0000413 CurByte = CurBlock = MemMgr.startFunctionBody();
Chris Lattner4d326fa2003-12-20 01:46:27 +0000414 TheJIT->addGlobalMapping(F.getFunction(), CurBlock);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000415}
416
Chris Lattner166f2262004-11-22 22:00:25 +0000417void JITEmitter::finishFunction(MachineFunction &F) {
Chris Lattner688506d2003-08-14 18:35:27 +0000418 MemMgr.endFunctionBody(CurByte);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000419 NumBytes += CurByte-CurBlock;
420
Chris Lattner5be478f2004-11-20 03:46:14 +0000421 if (!Relocations.empty()) {
Chris Lattnere884dc22005-07-20 16:29:20 +0000422 NumRelos += Relocations.size();
423
Chris Lattner5be478f2004-11-20 03:46:14 +0000424 // Resolve the relocations to concrete pointers.
425 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
426 MachineRelocation &MR = Relocations[i];
427 void *ResultPtr;
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000428 if (MR.isString()) {
Chris Lattner5be478f2004-11-20 03:46:14 +0000429 ResultPtr = TheJIT->getPointerToNamedFunction(MR.getString());
Misha Brukmanf976c852005-04-21 22:55:34 +0000430
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000431 // If the target REALLY wants a stub for this function, emit it now.
432 if (!MR.doesntNeedFunctionStub())
433 ResultPtr = getJITResolver(this).getExternalFunctionStub(ResultPtr);
Andrew Lenharth16ec33c2005-07-22 20:48:12 +0000434 } else if (MR.isGlobalValue())
Chris Lattner5e225582004-11-21 03:37:42 +0000435 ResultPtr = getPointerToGlobal(MR.getGlobalValue(),
436 CurBlock+MR.getMachineCodeOffset(),
437 MR.doesntNeedFunctionStub());
Andrew Lenharth16ec33c2005-07-22 20:48:12 +0000438 else //ConstantPoolIndex
439 ResultPtr =
440 (void*)getConstantPoolEntryAddress(MR.getConstantPoolIndex());
441
Chris Lattner5be478f2004-11-20 03:46:14 +0000442 MR.setResultPointer(ResultPtr);
Andrew Lenharth16ec33c2005-07-22 20:48:12 +0000443
444 // if we are managing the got, check to see if this pointer has all ready
445 // been allocated a GOT entry. If not, give it the next one.
446 if (MemMgr.isManagingGOT()) {
447 if (!revGOTMap[ResultPtr])
448 revGOTMap[ResultPtr] = ++nextGOTIndex;
449 ((void**)MemMgr.getGOTBase())[revGOTMap[ResultPtr]] = ResultPtr;
450 if(MR.isGOTRelative())
451 MR.setGOTIndex(revGOTMap[ResultPtr]);
452 }
Chris Lattner5be478f2004-11-20 03:46:14 +0000453 }
454
455 TheJIT->getJITInfo().relocate(CurBlock, &Relocations[0],
Andrew Lenharth16ec33c2005-07-22 20:48:12 +0000456 Relocations.size(), MemMgr.getGOTBase());
Chris Lattner5be478f2004-11-20 03:46:14 +0000457 }
458
Chris Lattnercb479412004-11-21 03:44:32 +0000459 DEBUG(std::cerr << "JIT: Finished CodeGen of [" << (void*)CurBlock
Misha Brukman1d440852003-06-06 06:52:35 +0000460 << "] Function: " << F.getFunction()->getName()
Chris Lattner5be478f2004-11-20 03:46:14 +0000461 << ": " << CurByte-CurBlock << " bytes of text, "
462 << Relocations.size() << " relocations\n");
463 Relocations.clear();
Andrew Lenharth16ec33c2005-07-22 20:48:12 +0000464 ConstantPoolAddresses.clear();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000465}
466
Chris Lattner166f2262004-11-22 22:00:25 +0000467void JITEmitter::emitConstantPool(MachineConstantPool *MCP) {
Chris Lattner1cc08382003-01-13 01:00:12 +0000468 const std::vector<Constant*> &Constants = MCP->getConstants();
Chris Lattner2c0a6a12003-11-30 04:23:21 +0000469 if (Constants.empty()) return;
470
Chris Lattner1cc08382003-01-13 01:00:12 +0000471 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Chris Lattner2c0a6a12003-11-30 04:23:21 +0000472 const Type *Ty = Constants[i]->getType();
Chris Lattnera8101c12005-01-08 20:07:03 +0000473 unsigned Size = (unsigned)TheJIT->getTargetData().getTypeSize(Ty);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000474 unsigned Alignment = TheJIT->getTargetData().getTypeAlignment(Ty);
Chris Lattner2c0a6a12003-11-30 04:23:21 +0000475
Chris Lattner281a6012005-01-10 18:23:22 +0000476 void *Addr = MemMgr.allocateConstant(Size, Alignment);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000477 TheJIT->InitializeMemory(Constants[i], Addr);
Misha Brukman91de3522003-11-30 00:50:53 +0000478 ConstantPoolAddresses.push_back(Addr);
Chris Lattner1cc08382003-01-13 01:00:12 +0000479 }
480}
481
Chris Lattner166f2262004-11-22 22:00:25 +0000482void JITEmitter::startFunctionStub(unsigned StubSize) {
Chris Lattner6125fdd2003-05-09 03:30:07 +0000483 SavedCurBlock = CurBlock; SavedCurByte = CurByte;
Chris Lattner688506d2003-08-14 18:35:27 +0000484 CurByte = CurBlock = MemMgr.allocateStub(StubSize);
Chris Lattner6125fdd2003-05-09 03:30:07 +0000485}
486
Chris Lattner166f2262004-11-22 22:00:25 +0000487void *JITEmitter::finishFunctionStub(const Function *F) {
Chris Lattner6125fdd2003-05-09 03:30:07 +0000488 NumBytes += CurByte-CurBlock;
Chris Lattner6125fdd2003-05-09 03:30:07 +0000489 std::swap(CurBlock, SavedCurBlock);
490 CurByte = SavedCurByte;
491 return SavedCurBlock;
492}
493
Chris Lattner166f2262004-11-22 22:00:25 +0000494void JITEmitter::emitByte(unsigned char B) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000495 *CurByte++ = B; // Write the byte to memory
496}
497
Chris Lattner166f2262004-11-22 22:00:25 +0000498void JITEmitter::emitWord(unsigned W) {
Chris Lattner688506d2003-08-14 18:35:27 +0000499 // This won't work if the endianness of the host and target don't agree! (For
500 // a JIT this can't happen though. :)
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000501 *(unsigned*)CurByte = W;
502 CurByte += sizeof(unsigned);
503}
504
Chris Lattner166f2262004-11-22 22:00:25 +0000505void JITEmitter::emitWordAt(unsigned W, unsigned *Ptr) {
Brian Gaekeaea1b582004-04-23 17:11:14 +0000506 *Ptr = W;
507}
508
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000509// getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry
510// in the constant pool that was last emitted with the 'emitConstantPool'
511// method.
512//
Chris Lattner166f2262004-11-22 22:00:25 +0000513uint64_t JITEmitter::getConstantPoolEntryAddress(unsigned ConstantNum) {
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000514 assert(ConstantNum < ConstantPoolAddresses.size() &&
Misha Brukman3c944972005-04-22 04:08:30 +0000515 "Invalid ConstantPoolIndex!");
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000516 return (intptr_t)ConstantPoolAddresses[ConstantNum];
517}
518
519// getCurrentPCValue - This returns the address that the next emitted byte
520// will be output to.
521//
Chris Lattner166f2262004-11-22 22:00:25 +0000522uint64_t JITEmitter::getCurrentPCValue() {
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000523 return (intptr_t)CurByte;
524}
525
Chris Lattner166f2262004-11-22 22:00:25 +0000526uint64_t JITEmitter::getCurrentPCOffset() {
Chris Lattner5be478f2004-11-20 03:46:14 +0000527 return (intptr_t)CurByte-(intptr_t)CurBlock;
528}
529
Misha Brukmand69c1e62003-07-28 19:09:06 +0000530// getPointerToNamedFunction - This function is used as a global wrapper to
Chris Lattner4d326fa2003-12-20 01:46:27 +0000531// JIT::getPointerToNamedFunction for the purpose of resolving symbols when
Misha Brukmand69c1e62003-07-28 19:09:06 +0000532// bugpoint is debugging the JIT. In that scenario, we are loading an .so and
533// need to resolve function(s) that are being mis-codegenerated, so we need to
534// resolve their addresses at runtime, and this is the way to do it.
535extern "C" {
536 void *getPointerToNamedFunction(const char *Name) {
Chris Lattner4d326fa2003-12-20 01:46:27 +0000537 Module &M = TheJIT->getModule();
Misha Brukmand69c1e62003-07-28 19:09:06 +0000538 if (Function *F = M.getNamedFunction(Name))
Chris Lattner4d326fa2003-12-20 01:46:27 +0000539 return TheJIT->getPointerToFunction(F);
540 return TheJIT->getPointerToNamedFunction(Name);
Misha Brukmand69c1e62003-07-28 19:09:06 +0000541 }
542}