blob: b38e778bfc7bbf53b8c138eafd3b5f6c401d2a66 [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;
Chris Lattner688506d2003-08-14 18:35:27 +000055 public:
56 JITMemoryManager();
Reid Spencer4af3da62004-12-13 16:04:04 +000057 ~JITMemoryManager();
Misha Brukmanf976c852005-04-21 22:55:34 +000058
Chris Lattner688506d2003-08-14 18:35:27 +000059 inline unsigned char *allocateStub(unsigned StubSize);
Chris Lattner281a6012005-01-10 18:23:22 +000060 inline unsigned char *allocateConstant(unsigned ConstantSize,
61 unsigned Alignment);
Chris Lattner688506d2003-08-14 18:35:27 +000062 inline unsigned char *startFunctionBody();
Chris Lattner281a6012005-01-10 18:23:22 +000063 inline void endFunctionBody(unsigned char *FunctionEnd);
Chris Lattner688506d2003-08-14 18:35:27 +000064 };
65}
66
Chris Lattner688506d2003-08-14 18:35:27 +000067JITMemoryManager::JITMemoryManager() {
68 // Allocate a 16M block of memory...
Reid Spencer33189e72004-09-13 22:38:11 +000069 MemBlock = sys::Memory::AllocateRWX((16 << 20));
Reid Spencer52b0ba62004-09-11 04:31:03 +000070 MemBase = reinterpret_cast<unsigned char*>(MemBlock.base());
Chris Lattner688506d2003-08-14 18:35:27 +000071 FunctionBase = MemBase + 512*1024; // Use 512k for stubs
72
73 // Allocate stubs backwards from the function base, allocate functions forward
74 // from the function base.
75 CurStubPtr = CurFunctionPtr = FunctionBase;
Chris Lattner281a6012005-01-10 18:23:22 +000076
77 ConstantPool = new unsigned char [512*1024]; // Use 512k for constant pools
78 CurConstantPtr = ConstantPool + 512*1024;
Chris Lattner688506d2003-08-14 18:35:27 +000079}
80
Reid Spencer4af3da62004-12-13 16:04:04 +000081JITMemoryManager::~JITMemoryManager() {
82 sys::Memory::ReleaseRWX(MemBlock);
Chris Lattner281a6012005-01-10 18:23:22 +000083 delete[] ConstantPool;
Reid Spencer4af3da62004-12-13 16:04:04 +000084}
85
Chris Lattner688506d2003-08-14 18:35:27 +000086unsigned char *JITMemoryManager::allocateStub(unsigned StubSize) {
87 CurStubPtr -= StubSize;
88 if (CurStubPtr < MemBase) {
89 std::cerr << "JIT ran out of memory for function stubs!\n";
90 abort();
91 }
92 return CurStubPtr;
93}
94
Chris Lattner281a6012005-01-10 18:23:22 +000095unsigned char *JITMemoryManager::allocateConstant(unsigned ConstantSize,
96 unsigned Alignment) {
97 // Reserve space and align pointer.
98 CurConstantPtr -= ConstantSize;
99 CurConstantPtr =
100 (unsigned char *)((intptr_t)CurConstantPtr & ~((intptr_t)Alignment - 1));
101
102 if (CurConstantPtr < ConstantPool) {
103 std::cerr << "JIT ran out of memory for constant pools!\n";
104 abort();
105 }
106 return CurConstantPtr;
107}
108
Chris Lattner688506d2003-08-14 18:35:27 +0000109unsigned char *JITMemoryManager::startFunctionBody() {
Chris Lattner5be478f2004-11-20 03:46:14 +0000110 // Round up to an even multiple of 8 bytes, this should eventually be target
Chris Lattner688506d2003-08-14 18:35:27 +0000111 // specific.
Chris Lattner5be478f2004-11-20 03:46:14 +0000112 return (unsigned char*)(((intptr_t)CurFunctionPtr + 7) & ~7);
Chris Lattner688506d2003-08-14 18:35:27 +0000113}
114
115void JITMemoryManager::endFunctionBody(unsigned char *FunctionEnd) {
116 assert(FunctionEnd > CurFunctionPtr);
117 CurFunctionPtr = FunctionEnd;
118}
119
Chris Lattner54266522004-11-20 23:57:07 +0000120//===----------------------------------------------------------------------===//
121// JIT lazy compilation code.
122//
123namespace {
Reid Spenceree448632005-07-12 15:51:55 +0000124 class JITResolverState {
125 private:
126 /// FunctionToStubMap - Keep track of the stub created for a particular
127 /// function so that we can reuse them if necessary.
128 std::map<Function*, void*> FunctionToStubMap;
129
130 /// StubToFunctionMap - Keep track of the function that each stub
131 /// corresponds to.
132 std::map<void*, Function*> StubToFunctionMap;
133
134 public:
135 std::map<Function*, void*>& getFunctionToStubMap(const MutexGuard& locked) {
136 assert(locked.holds(TheJIT->lock));
137 return FunctionToStubMap;
138 }
139
140 std::map<void*, Function*>& getStubToFunctionMap(const MutexGuard& locked) {
141 assert(locked.holds(TheJIT->lock));
142 return StubToFunctionMap;
143 }
144 };
145
Chris Lattner54266522004-11-20 23:57:07 +0000146 /// JITResolver - Keep track of, and resolve, call sites for functions that
147 /// have not yet been compiled.
148 class JITResolver {
Chris Lattner5e225582004-11-21 03:37:42 +0000149 /// MCE - The MachineCodeEmitter to use to emit stubs with.
Chris Lattner54266522004-11-20 23:57:07 +0000150 MachineCodeEmitter &MCE;
151
Chris Lattner5e225582004-11-21 03:37:42 +0000152 /// LazyResolverFn - The target lazy resolver function that we actually
153 /// rewrite instructions to use.
154 TargetJITInfo::LazyResolverFn LazyResolverFn;
155
Reid Spenceree448632005-07-12 15:51:55 +0000156 JITResolverState state;
Chris Lattner54266522004-11-20 23:57:07 +0000157
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000158 /// ExternalFnToStubMap - This is the equivalent of FunctionToStubMap for
159 /// external functions.
160 std::map<void*, void*> ExternalFnToStubMap;
Chris Lattner54266522004-11-20 23:57:07 +0000161 public:
Chris Lattner5e225582004-11-21 03:37:42 +0000162 JITResolver(MachineCodeEmitter &mce) : MCE(mce) {
163 LazyResolverFn =
164 TheJIT->getJITInfo().getLazyResolverFunction(JITCompilerFn);
165 }
Chris Lattner54266522004-11-20 23:57:07 +0000166
167 /// getFunctionStub - This returns a pointer to a function stub, creating
168 /// one on demand as needed.
169 void *getFunctionStub(Function *F);
170
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000171 /// getExternalFunctionStub - Return a stub for the function at the
172 /// specified address, created lazily on demand.
173 void *getExternalFunctionStub(void *FnAddr);
174
Chris Lattner5e225582004-11-21 03:37:42 +0000175 /// AddCallbackAtLocation - If the target is capable of rewriting an
176 /// instruction without the use of a stub, record the location of the use so
177 /// we know which function is being used at the location.
178 void *AddCallbackAtLocation(Function *F, void *Location) {
Reid Spenceree448632005-07-12 15:51:55 +0000179 MutexGuard locked(TheJIT->lock);
Chris Lattner5e225582004-11-21 03:37:42 +0000180 /// Get the target-specific JIT resolver function.
Reid Spenceree448632005-07-12 15:51:55 +0000181 state.getStubToFunctionMap(locked)[Location] = F;
Chris Lattner5e225582004-11-21 03:37:42 +0000182 return (void*)LazyResolverFn;
183 }
184
Chris Lattner54266522004-11-20 23:57:07 +0000185 /// JITCompilerFn - This function is called to resolve a stub to a compiled
186 /// address. If the LLVM Function corresponding to the stub has not yet
187 /// been compiled, this function compiles it first.
188 static void *JITCompilerFn(void *Stub);
189 };
190}
191
192/// getJITResolver - This function returns the one instance of the JIT resolver.
193///
194static JITResolver &getJITResolver(MachineCodeEmitter *MCE = 0) {
195 static JITResolver TheJITResolver(*MCE);
196 return TheJITResolver;
197}
198
199/// getFunctionStub - This returns a pointer to a function stub, creating
200/// one on demand as needed.
201void *JITResolver::getFunctionStub(Function *F) {
Reid Spenceree448632005-07-12 15:51:55 +0000202 MutexGuard locked(TheJIT->lock);
203
Chris Lattner54266522004-11-20 23:57:07 +0000204 // If we already have a stub for this function, recycle it.
Reid Spenceree448632005-07-12 15:51:55 +0000205 void *&Stub = state.getFunctionToStubMap(locked)[F];
Chris Lattner54266522004-11-20 23:57:07 +0000206 if (Stub) return Stub;
207
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000208 // Call the lazy resolver function unless we already KNOW it is an external
209 // function, in which case we just skip the lazy resolution step.
210 void *Actual = (void*)LazyResolverFn;
Chris Lattner69435702005-02-20 18:43:35 +0000211 if (F->isExternal() && F->hasExternalLinkage())
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000212 Actual = TheJIT->getPointerToFunction(F);
Misha Brukmanf976c852005-04-21 22:55:34 +0000213
Chris Lattner54266522004-11-20 23:57:07 +0000214 // Otherwise, codegen a new stub. For now, the stub will call the lazy
215 // resolver function.
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000216 Stub = TheJIT->getJITInfo().emitFunctionStub(Actual, MCE);
217
Chris Lattner69435702005-02-20 18:43:35 +0000218 if (Actual != (void*)LazyResolverFn) {
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000219 // If we are getting the stub for an external function, we really want the
220 // address of the stub in the GlobalAddressMap for the JIT, not the address
221 // of the external function.
222 TheJIT->updateGlobalMapping(F, Stub);
223 }
Chris Lattner54266522004-11-20 23:57:07 +0000224
Chris Lattnercb479412004-11-21 03:44:32 +0000225 DEBUG(std::cerr << "JIT: Stub emitted at [" << Stub << "] for function '"
Chris Lattner6f717202004-11-22 21:48:33 +0000226 << F->getName() << "'\n");
Chris Lattnercb479412004-11-21 03:44:32 +0000227
Chris Lattner54266522004-11-20 23:57:07 +0000228 // Finally, keep track of the stub-to-Function mapping so that the
229 // JITCompilerFn knows which function to compile!
Reid Spenceree448632005-07-12 15:51:55 +0000230 state.getStubToFunctionMap(locked)[Stub] = F;
Chris Lattner54266522004-11-20 23:57:07 +0000231 return Stub;
232}
233
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000234/// getExternalFunctionStub - Return a stub for the function at the
235/// specified address, created lazily on demand.
236void *JITResolver::getExternalFunctionStub(void *FnAddr) {
237 // If we already have a stub for this function, recycle it.
238 void *&Stub = ExternalFnToStubMap[FnAddr];
239 if (Stub) return Stub;
240
241 Stub = TheJIT->getJITInfo().emitFunctionStub(FnAddr, MCE);
242 DEBUG(std::cerr << "JIT: Stub emitted at [" << Stub
243 << "] for external function at '" << FnAddr << "'\n");
244 return Stub;
245}
246
247
Chris Lattner54266522004-11-20 23:57:07 +0000248/// JITCompilerFn - This function is called when a lazy compilation stub has
249/// been entered. It looks up which function this stub corresponds to, compiles
250/// it if necessary, then returns the resultant function pointer.
251void *JITResolver::JITCompilerFn(void *Stub) {
252 JITResolver &JR = getJITResolver();
Misha Brukmanf976c852005-04-21 22:55:34 +0000253
Reid Spenceree448632005-07-12 15:51:55 +0000254 MutexGuard locked(TheJIT->lock);
255
Chris Lattner54266522004-11-20 23:57:07 +0000256 // The address given to us for the stub may not be exactly right, it might be
257 // a little bit after the stub. As such, use upper_bound to find it.
258 std::map<void*, Function*>::iterator I =
Reid Spenceree448632005-07-12 15:51:55 +0000259 JR.state.getStubToFunctionMap(locked).upper_bound(Stub);
260 assert(I != JR.state.getStubToFunctionMap(locked).begin() && "This is not a known stub!");
Chris Lattner54266522004-11-20 23:57:07 +0000261 Function *F = (--I)->second;
262
Reid Spenceree448632005-07-12 15:51:55 +0000263 // We might like to remove the stub from the StubToFunction map.
264 // We can't do that! Multiple threads could be stuck, waiting to acquire the
265 // lock above. As soon as the 1st function finishes compiling the function,
266 // the next one will be released, and needs to be able to find the function it needs
267 // to call.
268 //JR.state.getStubToFunctionMap(locked).erase(I);
Chris Lattner54266522004-11-20 23:57:07 +0000269
Chris Lattnercb479412004-11-21 03:44:32 +0000270 DEBUG(std::cerr << "JIT: Lazily resolving function '" << F->getName()
Chris Lattner54266522004-11-20 23:57:07 +0000271 << "' In stub ptr = " << Stub << " actual ptr = "
272 << I->first << "\n");
273
274 void *Result = TheJIT->getPointerToFunction(F);
275
276 // We don't need to reuse this stub in the future, as F is now compiled.
Reid Spenceree448632005-07-12 15:51:55 +0000277 JR.state.getFunctionToStubMap(locked).erase(F);
Chris Lattner54266522004-11-20 23:57:07 +0000278
279 // FIXME: We could rewrite all references to this stub if we knew them.
280 return Result;
281}
Chris Lattner688506d2003-08-14 18:35:27 +0000282
283
Chris Lattnere518b712004-12-05 07:19:16 +0000284// getPointerToFunctionOrStub - If the specified function has been
285// code-gen'd, return a pointer to the function. If not, compile it, or use
286// a stub to implement lazy compilation if available.
287//
288void *JIT::getPointerToFunctionOrStub(Function *F) {
289 // If we have already code generated the function, just return the address.
290 if (void *Addr = getPointerToGlobalIfAvailable(F))
291 return Addr;
292
293 // Get a stub if the target supports it
294 return getJITResolver(MCE).getFunctionStub(F);
295}
296
297
298
Chris Lattner54266522004-11-20 23:57:07 +0000299//===----------------------------------------------------------------------===//
Chris Lattner166f2262004-11-22 22:00:25 +0000300// JITEmitter code.
Chris Lattner54266522004-11-20 23:57:07 +0000301//
Chris Lattner688506d2003-08-14 18:35:27 +0000302namespace {
Chris Lattner166f2262004-11-22 22:00:25 +0000303 /// JITEmitter - The JIT implementation of the MachineCodeEmitter, which is
304 /// used to output functions to memory for execution.
305 class JITEmitter : public MachineCodeEmitter {
Chris Lattner688506d2003-08-14 18:35:27 +0000306 JITMemoryManager MemMgr;
307
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000308 // CurBlock - The start of the current block of memory. CurByte - The
309 // current byte being emitted to.
Chris Lattner6125fdd2003-05-09 03:30:07 +0000310 unsigned char *CurBlock, *CurByte;
311
312 // When outputting a function stub in the context of some other function, we
313 // save CurBlock and CurByte here.
314 unsigned char *SavedCurBlock, *SavedCurByte;
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000315
316 // ConstantPoolAddresses - Contains the location for each entry in the
317 // constant pool.
Chris Lattner1cc08382003-01-13 01:00:12 +0000318 std::vector<void*> ConstantPoolAddresses;
Chris Lattner5be478f2004-11-20 03:46:14 +0000319
320 /// Relocations - These are the relocations that the function needs, as
321 /// emitted.
322 std::vector<MachineRelocation> Relocations;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000323 public:
Chris Lattner166f2262004-11-22 22:00:25 +0000324 JITEmitter(JIT &jit) { TheJIT = &jit; }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000325
326 virtual void startFunction(MachineFunction &F);
327 virtual void finishFunction(MachineFunction &F);
Chris Lattner1cc08382003-01-13 01:00:12 +0000328 virtual void emitConstantPool(MachineConstantPool *MCP);
Chris Lattner54266522004-11-20 23:57:07 +0000329 virtual void startFunctionStub(unsigned StubSize);
330 virtual void* finishFunctionStub(const Function *F);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000331 virtual void emitByte(unsigned char B);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000332 virtual void emitWord(unsigned W);
Brian Gaekeaea1b582004-04-23 17:11:14 +0000333 virtual void emitWordAt(unsigned W, unsigned *Ptr);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000334
Chris Lattner5be478f2004-11-20 03:46:14 +0000335 virtual void addRelocation(const MachineRelocation &MR) {
336 Relocations.push_back(MR);
337 }
338
339 virtual uint64_t getCurrentPCValue();
340 virtual uint64_t getCurrentPCOffset();
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000341 virtual uint64_t getConstantPoolEntryAddress(unsigned Entry);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000342
Chris Lattner54266522004-11-20 23:57:07 +0000343 private:
Chris Lattner5e225582004-11-21 03:37:42 +0000344 void *getPointerToGlobal(GlobalValue *GV, void *Reference, bool NoNeedStub);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000345 };
346}
347
Chris Lattner4d326fa2003-12-20 01:46:27 +0000348MachineCodeEmitter *JIT::createEmitter(JIT &jit) {
Chris Lattner166f2262004-11-22 22:00:25 +0000349 return new JITEmitter(jit);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000350}
351
Chris Lattner166f2262004-11-22 22:00:25 +0000352void *JITEmitter::getPointerToGlobal(GlobalValue *V, void *Reference,
353 bool DoesntNeedStub) {
Chris Lattner54266522004-11-20 23:57:07 +0000354 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
355 /// FIXME: If we straightened things out, this could actually emit the
356 /// global immediately instead of queuing it for codegen later!
Chris Lattner54266522004-11-20 23:57:07 +0000357 return TheJIT->getOrEmitGlobalVariable(GV);
358 }
359
360 // If we have already compiled the function, return a pointer to its body.
361 Function *F = cast<Function>(V);
362 void *ResultPtr = TheJIT->getPointerToGlobalIfAvailable(F);
363 if (ResultPtr) return ResultPtr;
364
Chris Lattner532343b2004-11-30 17:41:49 +0000365 if (F->hasExternalLinkage() && F->isExternal()) {
Chris Lattner54266522004-11-20 23:57:07 +0000366 // If this is an external function pointer, we can force the JIT to
367 // 'compile' it, which really just adds it to the map.
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000368 if (DoesntNeedStub)
369 return TheJIT->getPointerToFunction(F);
370
371 return getJITResolver(this).getFunctionStub(F);
Chris Lattner54266522004-11-20 23:57:07 +0000372 }
373
Chris Lattner5e225582004-11-21 03:37:42 +0000374 // Okay, the function has not been compiled yet, if the target callback
375 // mechanism is capable of rewriting the instruction directly, prefer to do
376 // that instead of emitting a stub.
377 if (DoesntNeedStub)
378 return getJITResolver(this).AddCallbackAtLocation(F, Reference);
379
Chris Lattner54266522004-11-20 23:57:07 +0000380 // Otherwise, we have to emit a lazy resolving stub.
381 return getJITResolver(this).getFunctionStub(F);
382}
383
Chris Lattner166f2262004-11-22 22:00:25 +0000384void JITEmitter::startFunction(MachineFunction &F) {
Chris Lattner688506d2003-08-14 18:35:27 +0000385 CurByte = CurBlock = MemMgr.startFunctionBody();
Chris Lattner4d326fa2003-12-20 01:46:27 +0000386 TheJIT->addGlobalMapping(F.getFunction(), CurBlock);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000387}
388
Chris Lattner166f2262004-11-22 22:00:25 +0000389void JITEmitter::finishFunction(MachineFunction &F) {
Chris Lattner688506d2003-08-14 18:35:27 +0000390 MemMgr.endFunctionBody(CurByte);
Chris Lattner1cc08382003-01-13 01:00:12 +0000391 ConstantPoolAddresses.clear();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000392 NumBytes += CurByte-CurBlock;
393
Chris Lattner5be478f2004-11-20 03:46:14 +0000394 if (!Relocations.empty()) {
Chris Lattnere884dc22005-07-20 16:29:20 +0000395 NumRelos += Relocations.size();
396
Chris Lattner5be478f2004-11-20 03:46:14 +0000397 // Resolve the relocations to concrete pointers.
398 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
399 MachineRelocation &MR = Relocations[i];
400 void *ResultPtr;
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000401 if (MR.isString()) {
Chris Lattner5be478f2004-11-20 03:46:14 +0000402 ResultPtr = TheJIT->getPointerToNamedFunction(MR.getString());
Misha Brukmanf976c852005-04-21 22:55:34 +0000403
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000404 // If the target REALLY wants a stub for this function, emit it now.
405 if (!MR.doesntNeedFunctionStub())
406 ResultPtr = getJITResolver(this).getExternalFunctionStub(ResultPtr);
407 } else
Chris Lattner5e225582004-11-21 03:37:42 +0000408 ResultPtr = getPointerToGlobal(MR.getGlobalValue(),
409 CurBlock+MR.getMachineCodeOffset(),
410 MR.doesntNeedFunctionStub());
Chris Lattner5be478f2004-11-20 03:46:14 +0000411 MR.setResultPointer(ResultPtr);
412 }
413
414 TheJIT->getJITInfo().relocate(CurBlock, &Relocations[0],
415 Relocations.size());
416 }
417
Chris Lattnercb479412004-11-21 03:44:32 +0000418 DEBUG(std::cerr << "JIT: Finished CodeGen of [" << (void*)CurBlock
Misha Brukman1d440852003-06-06 06:52:35 +0000419 << "] Function: " << F.getFunction()->getName()
Chris Lattner5be478f2004-11-20 03:46:14 +0000420 << ": " << CurByte-CurBlock << " bytes of text, "
421 << Relocations.size() << " relocations\n");
422 Relocations.clear();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000423}
424
Chris Lattner166f2262004-11-22 22:00:25 +0000425void JITEmitter::emitConstantPool(MachineConstantPool *MCP) {
Chris Lattner1cc08382003-01-13 01:00:12 +0000426 const std::vector<Constant*> &Constants = MCP->getConstants();
Chris Lattner2c0a6a12003-11-30 04:23:21 +0000427 if (Constants.empty()) return;
428
Chris Lattner1cc08382003-01-13 01:00:12 +0000429 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Chris Lattner2c0a6a12003-11-30 04:23:21 +0000430 const Type *Ty = Constants[i]->getType();
Chris Lattnera8101c12005-01-08 20:07:03 +0000431 unsigned Size = (unsigned)TheJIT->getTargetData().getTypeSize(Ty);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000432 unsigned Alignment = TheJIT->getTargetData().getTypeAlignment(Ty);
Chris Lattner2c0a6a12003-11-30 04:23:21 +0000433
Chris Lattner281a6012005-01-10 18:23:22 +0000434 void *Addr = MemMgr.allocateConstant(Size, Alignment);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000435 TheJIT->InitializeMemory(Constants[i], Addr);
Misha Brukman91de3522003-11-30 00:50:53 +0000436 ConstantPoolAddresses.push_back(Addr);
Chris Lattner1cc08382003-01-13 01:00:12 +0000437 }
438}
439
Chris Lattner166f2262004-11-22 22:00:25 +0000440void JITEmitter::startFunctionStub(unsigned StubSize) {
Chris Lattner6125fdd2003-05-09 03:30:07 +0000441 SavedCurBlock = CurBlock; SavedCurByte = CurByte;
Chris Lattner688506d2003-08-14 18:35:27 +0000442 CurByte = CurBlock = MemMgr.allocateStub(StubSize);
Chris Lattner6125fdd2003-05-09 03:30:07 +0000443}
444
Chris Lattner166f2262004-11-22 22:00:25 +0000445void *JITEmitter::finishFunctionStub(const Function *F) {
Chris Lattner6125fdd2003-05-09 03:30:07 +0000446 NumBytes += CurByte-CurBlock;
Chris Lattner6125fdd2003-05-09 03:30:07 +0000447 std::swap(CurBlock, SavedCurBlock);
448 CurByte = SavedCurByte;
449 return SavedCurBlock;
450}
451
Chris Lattner166f2262004-11-22 22:00:25 +0000452void JITEmitter::emitByte(unsigned char B) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000453 *CurByte++ = B; // Write the byte to memory
454}
455
Chris Lattner166f2262004-11-22 22:00:25 +0000456void JITEmitter::emitWord(unsigned W) {
Chris Lattner688506d2003-08-14 18:35:27 +0000457 // This won't work if the endianness of the host and target don't agree! (For
458 // a JIT this can't happen though. :)
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000459 *(unsigned*)CurByte = W;
460 CurByte += sizeof(unsigned);
461}
462
Chris Lattner166f2262004-11-22 22:00:25 +0000463void JITEmitter::emitWordAt(unsigned W, unsigned *Ptr) {
Brian Gaekeaea1b582004-04-23 17:11:14 +0000464 *Ptr = W;
465}
466
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000467// getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry
468// in the constant pool that was last emitted with the 'emitConstantPool'
469// method.
470//
Chris Lattner166f2262004-11-22 22:00:25 +0000471uint64_t JITEmitter::getConstantPoolEntryAddress(unsigned ConstantNum) {
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000472 assert(ConstantNum < ConstantPoolAddresses.size() &&
Misha Brukman3c944972005-04-22 04:08:30 +0000473 "Invalid ConstantPoolIndex!");
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000474 return (intptr_t)ConstantPoolAddresses[ConstantNum];
475}
476
477// getCurrentPCValue - This returns the address that the next emitted byte
478// will be output to.
479//
Chris Lattner166f2262004-11-22 22:00:25 +0000480uint64_t JITEmitter::getCurrentPCValue() {
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000481 return (intptr_t)CurByte;
482}
483
Chris Lattner166f2262004-11-22 22:00:25 +0000484uint64_t JITEmitter::getCurrentPCOffset() {
Chris Lattner5be478f2004-11-20 03:46:14 +0000485 return (intptr_t)CurByte-(intptr_t)CurBlock;
486}
487
Misha Brukmand69c1e62003-07-28 19:09:06 +0000488// getPointerToNamedFunction - This function is used as a global wrapper to
Chris Lattner4d326fa2003-12-20 01:46:27 +0000489// JIT::getPointerToNamedFunction for the purpose of resolving symbols when
Misha Brukmand69c1e62003-07-28 19:09:06 +0000490// bugpoint is debugging the JIT. In that scenario, we are loading an .so and
491// need to resolve function(s) that are being mis-codegenerated, so we need to
492// resolve their addresses at runtime, and this is the way to do it.
493extern "C" {
494 void *getPointerToNamedFunction(const char *Name) {
Chris Lattner4d326fa2003-12-20 01:46:27 +0000495 Module &M = TheJIT->getModule();
Misha Brukmand69c1e62003-07-28 19:09:06 +0000496 if (Function *F = M.getNamedFunction(Name))
Chris Lattner4d326fa2003-12-20 01:46:27 +0000497 return TheJIT->getPointerToFunction(F);
498 return TheJIT->getPointerToNamedFunction(Name);
Misha Brukmand69c1e62003-07-28 19:09:06 +0000499 }
500}