blob: 47adee00192eab0333eccfafb1407808af5c4d86 [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 Lattner4d326fa2003-12-20 01:46:27 +000033 JIT *TheJIT = 0;
Chris Lattner54266522004-11-20 23:57:07 +000034}
Chris Lattnerbd199fb2002-12-24 00:01:05 +000035
Chris Lattner54266522004-11-20 23:57:07 +000036
37//===----------------------------------------------------------------------===//
38// JITMemoryManager code.
39//
40namespace {
Chris Lattner688506d2003-08-14 18:35:27 +000041 /// JITMemoryManager - Manage memory for the JIT code generation in a logical,
42 /// sane way. This splits a large block of MAP_NORESERVE'd memory into two
43 /// sections, one for function stubs, one for the functions themselves. We
44 /// have to do this because we may need to emit a function stub while in the
45 /// middle of emitting a function, and we don't know how large the function we
46 /// are emitting is. This never bothers to release the memory, because when
47 /// we are ready to destroy the JIT, the program exits.
48 class JITMemoryManager {
Reid Spencer33189e72004-09-13 22:38:11 +000049 sys::MemoryBlock MemBlock; // Virtual memory block allocated RWX
Chris Lattner688506d2003-08-14 18:35:27 +000050 unsigned char *MemBase; // Base of block of memory, start of stub mem
51 unsigned char *FunctionBase; // Start of the function body area
Chris Lattner281a6012005-01-10 18:23:22 +000052 unsigned char *ConstantPool; // Memory allocated for constant pools
53 unsigned char *CurStubPtr, *CurFunctionPtr, *CurConstantPtr;
Chris Lattner688506d2003-08-14 18:35:27 +000054 public:
55 JITMemoryManager();
Reid Spencer4af3da62004-12-13 16:04:04 +000056 ~JITMemoryManager();
Misha Brukmanf976c852005-04-21 22:55:34 +000057
Chris Lattner688506d2003-08-14 18:35:27 +000058 inline unsigned char *allocateStub(unsigned StubSize);
Chris Lattner281a6012005-01-10 18:23:22 +000059 inline unsigned char *allocateConstant(unsigned ConstantSize,
60 unsigned Alignment);
Chris Lattner688506d2003-08-14 18:35:27 +000061 inline unsigned char *startFunctionBody();
Chris Lattner281a6012005-01-10 18:23:22 +000062 inline void endFunctionBody(unsigned char *FunctionEnd);
Chris Lattner688506d2003-08-14 18:35:27 +000063 };
64}
65
Chris Lattner688506d2003-08-14 18:35:27 +000066JITMemoryManager::JITMemoryManager() {
67 // Allocate a 16M block of memory...
Reid Spencer33189e72004-09-13 22:38:11 +000068 MemBlock = sys::Memory::AllocateRWX((16 << 20));
Reid Spencer52b0ba62004-09-11 04:31:03 +000069 MemBase = reinterpret_cast<unsigned char*>(MemBlock.base());
Chris Lattner688506d2003-08-14 18:35:27 +000070 FunctionBase = MemBase + 512*1024; // Use 512k for stubs
71
72 // Allocate stubs backwards from the function base, allocate functions forward
73 // from the function base.
74 CurStubPtr = CurFunctionPtr = FunctionBase;
Chris Lattner281a6012005-01-10 18:23:22 +000075
76 ConstantPool = new unsigned char [512*1024]; // Use 512k for constant pools
77 CurConstantPtr = ConstantPool + 512*1024;
Chris Lattner688506d2003-08-14 18:35:27 +000078}
79
Reid Spencer4af3da62004-12-13 16:04:04 +000080JITMemoryManager::~JITMemoryManager() {
81 sys::Memory::ReleaseRWX(MemBlock);
Chris Lattner281a6012005-01-10 18:23:22 +000082 delete[] ConstantPool;
Reid Spencer4af3da62004-12-13 16:04:04 +000083}
84
Chris Lattner688506d2003-08-14 18:35:27 +000085unsigned char *JITMemoryManager::allocateStub(unsigned StubSize) {
86 CurStubPtr -= StubSize;
87 if (CurStubPtr < MemBase) {
88 std::cerr << "JIT ran out of memory for function stubs!\n";
89 abort();
90 }
91 return CurStubPtr;
92}
93
Chris Lattner281a6012005-01-10 18:23:22 +000094unsigned char *JITMemoryManager::allocateConstant(unsigned ConstantSize,
95 unsigned Alignment) {
96 // Reserve space and align pointer.
97 CurConstantPtr -= ConstantSize;
98 CurConstantPtr =
99 (unsigned char *)((intptr_t)CurConstantPtr & ~((intptr_t)Alignment - 1));
100
101 if (CurConstantPtr < ConstantPool) {
102 std::cerr << "JIT ran out of memory for constant pools!\n";
103 abort();
104 }
105 return CurConstantPtr;
106}
107
Chris Lattner688506d2003-08-14 18:35:27 +0000108unsigned char *JITMemoryManager::startFunctionBody() {
Chris Lattner5be478f2004-11-20 03:46:14 +0000109 // Round up to an even multiple of 8 bytes, this should eventually be target
Chris Lattner688506d2003-08-14 18:35:27 +0000110 // specific.
Chris Lattner5be478f2004-11-20 03:46:14 +0000111 return (unsigned char*)(((intptr_t)CurFunctionPtr + 7) & ~7);
Chris Lattner688506d2003-08-14 18:35:27 +0000112}
113
114void JITMemoryManager::endFunctionBody(unsigned char *FunctionEnd) {
115 assert(FunctionEnd > CurFunctionPtr);
116 CurFunctionPtr = FunctionEnd;
117}
118
Chris Lattner54266522004-11-20 23:57:07 +0000119//===----------------------------------------------------------------------===//
120// JIT lazy compilation code.
121//
122namespace {
Reid Spenceree448632005-07-12 15:51:55 +0000123 class JITResolverState {
124 private:
125 /// FunctionToStubMap - Keep track of the stub created for a particular
126 /// function so that we can reuse them if necessary.
127 std::map<Function*, void*> FunctionToStubMap;
128
129 /// StubToFunctionMap - Keep track of the function that each stub
130 /// corresponds to.
131 std::map<void*, Function*> StubToFunctionMap;
132
133 public:
134 std::map<Function*, void*>& getFunctionToStubMap(const MutexGuard& locked) {
135 assert(locked.holds(TheJIT->lock));
136 return FunctionToStubMap;
137 }
138
139 std::map<void*, Function*>& getStubToFunctionMap(const MutexGuard& locked) {
140 assert(locked.holds(TheJIT->lock));
141 return StubToFunctionMap;
142 }
143 };
144
Chris Lattner54266522004-11-20 23:57:07 +0000145 /// JITResolver - Keep track of, and resolve, call sites for functions that
146 /// have not yet been compiled.
147 class JITResolver {
Chris Lattner5e225582004-11-21 03:37:42 +0000148 /// MCE - The MachineCodeEmitter to use to emit stubs with.
Chris Lattner54266522004-11-20 23:57:07 +0000149 MachineCodeEmitter &MCE;
150
Chris Lattner5e225582004-11-21 03:37:42 +0000151 /// LazyResolverFn - The target lazy resolver function that we actually
152 /// rewrite instructions to use.
153 TargetJITInfo::LazyResolverFn LazyResolverFn;
154
Reid Spenceree448632005-07-12 15:51:55 +0000155 JITResolverState state;
Chris Lattner54266522004-11-20 23:57:07 +0000156
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000157 /// ExternalFnToStubMap - This is the equivalent of FunctionToStubMap for
158 /// external functions.
159 std::map<void*, void*> ExternalFnToStubMap;
Chris Lattner54266522004-11-20 23:57:07 +0000160 public:
Chris Lattner5e225582004-11-21 03:37:42 +0000161 JITResolver(MachineCodeEmitter &mce) : MCE(mce) {
162 LazyResolverFn =
163 TheJIT->getJITInfo().getLazyResolverFunction(JITCompilerFn);
164 }
Chris Lattner54266522004-11-20 23:57:07 +0000165
166 /// getFunctionStub - This returns a pointer to a function stub, creating
167 /// one on demand as needed.
168 void *getFunctionStub(Function *F);
169
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000170 /// getExternalFunctionStub - Return a stub for the function at the
171 /// specified address, created lazily on demand.
172 void *getExternalFunctionStub(void *FnAddr);
173
Chris Lattner5e225582004-11-21 03:37:42 +0000174 /// AddCallbackAtLocation - If the target is capable of rewriting an
175 /// instruction without the use of a stub, record the location of the use so
176 /// we know which function is being used at the location.
177 void *AddCallbackAtLocation(Function *F, void *Location) {
Reid Spenceree448632005-07-12 15:51:55 +0000178 MutexGuard locked(TheJIT->lock);
Chris Lattner5e225582004-11-21 03:37:42 +0000179 /// Get the target-specific JIT resolver function.
Reid Spenceree448632005-07-12 15:51:55 +0000180 state.getStubToFunctionMap(locked)[Location] = F;
Chris Lattner5e225582004-11-21 03:37:42 +0000181 return (void*)LazyResolverFn;
182 }
183
Chris Lattner54266522004-11-20 23:57:07 +0000184 /// JITCompilerFn - This function is called to resolve a stub to a compiled
185 /// address. If the LLVM Function corresponding to the stub has not yet
186 /// been compiled, this function compiles it first.
187 static void *JITCompilerFn(void *Stub);
188 };
189}
190
191/// getJITResolver - This function returns the one instance of the JIT resolver.
192///
193static JITResolver &getJITResolver(MachineCodeEmitter *MCE = 0) {
194 static JITResolver TheJITResolver(*MCE);
195 return TheJITResolver;
196}
197
198/// getFunctionStub - This returns a pointer to a function stub, creating
199/// one on demand as needed.
200void *JITResolver::getFunctionStub(Function *F) {
Reid Spenceree448632005-07-12 15:51:55 +0000201 MutexGuard locked(TheJIT->lock);
202
Chris Lattner54266522004-11-20 23:57:07 +0000203 // If we already have a stub for this function, recycle it.
Reid Spenceree448632005-07-12 15:51:55 +0000204 void *&Stub = state.getFunctionToStubMap(locked)[F];
Chris Lattner54266522004-11-20 23:57:07 +0000205 if (Stub) return Stub;
206
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000207 // Call the lazy resolver function unless we already KNOW it is an external
208 // function, in which case we just skip the lazy resolution step.
209 void *Actual = (void*)LazyResolverFn;
Chris Lattner69435702005-02-20 18:43:35 +0000210 if (F->isExternal() && F->hasExternalLinkage())
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000211 Actual = TheJIT->getPointerToFunction(F);
Misha Brukmanf976c852005-04-21 22:55:34 +0000212
Chris Lattner54266522004-11-20 23:57:07 +0000213 // Otherwise, codegen a new stub. For now, the stub will call the lazy
214 // resolver function.
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000215 Stub = TheJIT->getJITInfo().emitFunctionStub(Actual, MCE);
216
Chris Lattner69435702005-02-20 18:43:35 +0000217 if (Actual != (void*)LazyResolverFn) {
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000218 // If we are getting the stub for an external function, we really want the
219 // address of the stub in the GlobalAddressMap for the JIT, not the address
220 // of the external function.
221 TheJIT->updateGlobalMapping(F, Stub);
222 }
Chris Lattner54266522004-11-20 23:57:07 +0000223
Chris Lattnercb479412004-11-21 03:44:32 +0000224 DEBUG(std::cerr << "JIT: Stub emitted at [" << Stub << "] for function '"
Chris Lattner6f717202004-11-22 21:48:33 +0000225 << F->getName() << "'\n");
Chris Lattnercb479412004-11-21 03:44:32 +0000226
Chris Lattner54266522004-11-20 23:57:07 +0000227 // Finally, keep track of the stub-to-Function mapping so that the
228 // JITCompilerFn knows which function to compile!
Reid Spenceree448632005-07-12 15:51:55 +0000229 state.getStubToFunctionMap(locked)[Stub] = F;
Chris Lattner54266522004-11-20 23:57:07 +0000230 return Stub;
231}
232
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000233/// getExternalFunctionStub - Return a stub for the function at the
234/// specified address, created lazily on demand.
235void *JITResolver::getExternalFunctionStub(void *FnAddr) {
236 // If we already have a stub for this function, recycle it.
237 void *&Stub = ExternalFnToStubMap[FnAddr];
238 if (Stub) return Stub;
239
240 Stub = TheJIT->getJITInfo().emitFunctionStub(FnAddr, MCE);
241 DEBUG(std::cerr << "JIT: Stub emitted at [" << Stub
242 << "] for external function at '" << FnAddr << "'\n");
243 return Stub;
244}
245
246
Chris Lattner54266522004-11-20 23:57:07 +0000247/// JITCompilerFn - This function is called when a lazy compilation stub has
248/// been entered. It looks up which function this stub corresponds to, compiles
249/// it if necessary, then returns the resultant function pointer.
250void *JITResolver::JITCompilerFn(void *Stub) {
251 JITResolver &JR = getJITResolver();
Misha Brukmanf976c852005-04-21 22:55:34 +0000252
Reid Spenceree448632005-07-12 15:51:55 +0000253 MutexGuard locked(TheJIT->lock);
254
Chris Lattner54266522004-11-20 23:57:07 +0000255 // The address given to us for the stub may not be exactly right, it might be
256 // a little bit after the stub. As such, use upper_bound to find it.
257 std::map<void*, Function*>::iterator I =
Reid Spenceree448632005-07-12 15:51:55 +0000258 JR.state.getStubToFunctionMap(locked).upper_bound(Stub);
259 assert(I != JR.state.getStubToFunctionMap(locked).begin() && "This is not a known stub!");
Chris Lattner54266522004-11-20 23:57:07 +0000260 Function *F = (--I)->second;
261
Reid Spenceree448632005-07-12 15:51:55 +0000262 // We might like to remove the stub from the StubToFunction map.
263 // We can't do that! Multiple threads could be stuck, waiting to acquire the
264 // lock above. As soon as the 1st function finishes compiling the function,
265 // the next one will be released, and needs to be able to find the function it needs
266 // to call.
267 //JR.state.getStubToFunctionMap(locked).erase(I);
Chris Lattner54266522004-11-20 23:57:07 +0000268
Chris Lattnercb479412004-11-21 03:44:32 +0000269 DEBUG(std::cerr << "JIT: Lazily resolving function '" << F->getName()
Chris Lattner54266522004-11-20 23:57:07 +0000270 << "' In stub ptr = " << Stub << " actual ptr = "
271 << I->first << "\n");
272
273 void *Result = TheJIT->getPointerToFunction(F);
274
275 // We don't need to reuse this stub in the future, as F is now compiled.
Reid Spenceree448632005-07-12 15:51:55 +0000276 JR.state.getFunctionToStubMap(locked).erase(F);
Chris Lattner54266522004-11-20 23:57:07 +0000277
278 // FIXME: We could rewrite all references to this stub if we knew them.
279 return Result;
280}
Chris Lattner688506d2003-08-14 18:35:27 +0000281
282
Chris Lattnere518b712004-12-05 07:19:16 +0000283// getPointerToFunctionOrStub - If the specified function has been
284// code-gen'd, return a pointer to the function. If not, compile it, or use
285// a stub to implement lazy compilation if available.
286//
287void *JIT::getPointerToFunctionOrStub(Function *F) {
288 // If we have already code generated the function, just return the address.
289 if (void *Addr = getPointerToGlobalIfAvailable(F))
290 return Addr;
291
292 // Get a stub if the target supports it
293 return getJITResolver(MCE).getFunctionStub(F);
294}
295
296
297
Chris Lattner54266522004-11-20 23:57:07 +0000298//===----------------------------------------------------------------------===//
Chris Lattner166f2262004-11-22 22:00:25 +0000299// JITEmitter code.
Chris Lattner54266522004-11-20 23:57:07 +0000300//
Chris Lattner688506d2003-08-14 18:35:27 +0000301namespace {
Chris Lattner166f2262004-11-22 22:00:25 +0000302 /// JITEmitter - The JIT implementation of the MachineCodeEmitter, which is
303 /// used to output functions to memory for execution.
304 class JITEmitter : public MachineCodeEmitter {
Chris Lattner688506d2003-08-14 18:35:27 +0000305 JITMemoryManager MemMgr;
306
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000307 // CurBlock - The start of the current block of memory. CurByte - The
308 // current byte being emitted to.
Chris Lattner6125fdd2003-05-09 03:30:07 +0000309 unsigned char *CurBlock, *CurByte;
310
311 // When outputting a function stub in the context of some other function, we
312 // save CurBlock and CurByte here.
313 unsigned char *SavedCurBlock, *SavedCurByte;
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000314
315 // ConstantPoolAddresses - Contains the location for each entry in the
316 // constant pool.
Chris Lattner1cc08382003-01-13 01:00:12 +0000317 std::vector<void*> ConstantPoolAddresses;
Chris Lattner5be478f2004-11-20 03:46:14 +0000318
319 /// Relocations - These are the relocations that the function needs, as
320 /// emitted.
321 std::vector<MachineRelocation> Relocations;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000322 public:
Chris Lattner166f2262004-11-22 22:00:25 +0000323 JITEmitter(JIT &jit) { TheJIT = &jit; }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000324
325 virtual void startFunction(MachineFunction &F);
326 virtual void finishFunction(MachineFunction &F);
Chris Lattner1cc08382003-01-13 01:00:12 +0000327 virtual void emitConstantPool(MachineConstantPool *MCP);
Chris Lattner54266522004-11-20 23:57:07 +0000328 virtual void startFunctionStub(unsigned StubSize);
329 virtual void* finishFunctionStub(const Function *F);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000330 virtual void emitByte(unsigned char B);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000331 virtual void emitWord(unsigned W);
Brian Gaekeaea1b582004-04-23 17:11:14 +0000332 virtual void emitWordAt(unsigned W, unsigned *Ptr);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000333
Chris Lattner5be478f2004-11-20 03:46:14 +0000334 virtual void addRelocation(const MachineRelocation &MR) {
335 Relocations.push_back(MR);
336 }
337
338 virtual uint64_t getCurrentPCValue();
339 virtual uint64_t getCurrentPCOffset();
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000340 virtual uint64_t getConstantPoolEntryAddress(unsigned Entry);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000341
Chris Lattner54266522004-11-20 23:57:07 +0000342 private:
Chris Lattner5e225582004-11-21 03:37:42 +0000343 void *getPointerToGlobal(GlobalValue *GV, void *Reference, bool NoNeedStub);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000344 };
345}
346
Chris Lattner4d326fa2003-12-20 01:46:27 +0000347MachineCodeEmitter *JIT::createEmitter(JIT &jit) {
Chris Lattner166f2262004-11-22 22:00:25 +0000348 return new JITEmitter(jit);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000349}
350
Chris Lattner166f2262004-11-22 22:00:25 +0000351void *JITEmitter::getPointerToGlobal(GlobalValue *V, void *Reference,
352 bool DoesntNeedStub) {
Chris Lattner54266522004-11-20 23:57:07 +0000353 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
354 /// FIXME: If we straightened things out, this could actually emit the
355 /// global immediately instead of queuing it for codegen later!
Chris Lattner54266522004-11-20 23:57:07 +0000356 return TheJIT->getOrEmitGlobalVariable(GV);
357 }
358
359 // If we have already compiled the function, return a pointer to its body.
360 Function *F = cast<Function>(V);
361 void *ResultPtr = TheJIT->getPointerToGlobalIfAvailable(F);
362 if (ResultPtr) return ResultPtr;
363
Chris Lattner532343b2004-11-30 17:41:49 +0000364 if (F->hasExternalLinkage() && F->isExternal()) {
Chris Lattner54266522004-11-20 23:57:07 +0000365 // If this is an external function pointer, we can force the JIT to
366 // 'compile' it, which really just adds it to the map.
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000367 if (DoesntNeedStub)
368 return TheJIT->getPointerToFunction(F);
369
370 return getJITResolver(this).getFunctionStub(F);
Chris Lattner54266522004-11-20 23:57:07 +0000371 }
372
Chris Lattner5e225582004-11-21 03:37:42 +0000373 // Okay, the function has not been compiled yet, if the target callback
374 // mechanism is capable of rewriting the instruction directly, prefer to do
375 // that instead of emitting a stub.
376 if (DoesntNeedStub)
377 return getJITResolver(this).AddCallbackAtLocation(F, Reference);
378
Chris Lattner54266522004-11-20 23:57:07 +0000379 // Otherwise, we have to emit a lazy resolving stub.
380 return getJITResolver(this).getFunctionStub(F);
381}
382
Chris Lattner166f2262004-11-22 22:00:25 +0000383void JITEmitter::startFunction(MachineFunction &F) {
Chris Lattner688506d2003-08-14 18:35:27 +0000384 CurByte = CurBlock = MemMgr.startFunctionBody();
Chris Lattner4d326fa2003-12-20 01:46:27 +0000385 TheJIT->addGlobalMapping(F.getFunction(), CurBlock);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000386}
387
Chris Lattner166f2262004-11-22 22:00:25 +0000388void JITEmitter::finishFunction(MachineFunction &F) {
Chris Lattner688506d2003-08-14 18:35:27 +0000389 MemMgr.endFunctionBody(CurByte);
Chris Lattner1cc08382003-01-13 01:00:12 +0000390 ConstantPoolAddresses.clear();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000391 NumBytes += CurByte-CurBlock;
392
Chris Lattner5be478f2004-11-20 03:46:14 +0000393 if (!Relocations.empty()) {
394 // Resolve the relocations to concrete pointers.
395 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
396 MachineRelocation &MR = Relocations[i];
397 void *ResultPtr;
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000398 if (MR.isString()) {
Chris Lattner5be478f2004-11-20 03:46:14 +0000399 ResultPtr = TheJIT->getPointerToNamedFunction(MR.getString());
Misha Brukmanf976c852005-04-21 22:55:34 +0000400
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000401 // If the target REALLY wants a stub for this function, emit it now.
402 if (!MR.doesntNeedFunctionStub())
403 ResultPtr = getJITResolver(this).getExternalFunctionStub(ResultPtr);
404 } else
Chris Lattner5e225582004-11-21 03:37:42 +0000405 ResultPtr = getPointerToGlobal(MR.getGlobalValue(),
406 CurBlock+MR.getMachineCodeOffset(),
407 MR.doesntNeedFunctionStub());
Chris Lattner5be478f2004-11-20 03:46:14 +0000408 MR.setResultPointer(ResultPtr);
409 }
410
411 TheJIT->getJITInfo().relocate(CurBlock, &Relocations[0],
412 Relocations.size());
413 }
414
Chris Lattnercb479412004-11-21 03:44:32 +0000415 DEBUG(std::cerr << "JIT: Finished CodeGen of [" << (void*)CurBlock
Misha Brukman1d440852003-06-06 06:52:35 +0000416 << "] Function: " << F.getFunction()->getName()
Chris Lattner5be478f2004-11-20 03:46:14 +0000417 << ": " << CurByte-CurBlock << " bytes of text, "
418 << Relocations.size() << " relocations\n");
419 Relocations.clear();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000420}
421
Chris Lattner166f2262004-11-22 22:00:25 +0000422void JITEmitter::emitConstantPool(MachineConstantPool *MCP) {
Chris Lattner1cc08382003-01-13 01:00:12 +0000423 const std::vector<Constant*> &Constants = MCP->getConstants();
Chris Lattner2c0a6a12003-11-30 04:23:21 +0000424 if (Constants.empty()) return;
425
Chris Lattner1cc08382003-01-13 01:00:12 +0000426 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Chris Lattner2c0a6a12003-11-30 04:23:21 +0000427 const Type *Ty = Constants[i]->getType();
Chris Lattnera8101c12005-01-08 20:07:03 +0000428 unsigned Size = (unsigned)TheJIT->getTargetData().getTypeSize(Ty);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000429 unsigned Alignment = TheJIT->getTargetData().getTypeAlignment(Ty);
Chris Lattner2c0a6a12003-11-30 04:23:21 +0000430
Chris Lattner281a6012005-01-10 18:23:22 +0000431 void *Addr = MemMgr.allocateConstant(Size, Alignment);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000432 TheJIT->InitializeMemory(Constants[i], Addr);
Misha Brukman91de3522003-11-30 00:50:53 +0000433 ConstantPoolAddresses.push_back(Addr);
Chris Lattner1cc08382003-01-13 01:00:12 +0000434 }
435}
436
Chris Lattner166f2262004-11-22 22:00:25 +0000437void JITEmitter::startFunctionStub(unsigned StubSize) {
Chris Lattner6125fdd2003-05-09 03:30:07 +0000438 SavedCurBlock = CurBlock; SavedCurByte = CurByte;
Chris Lattner688506d2003-08-14 18:35:27 +0000439 CurByte = CurBlock = MemMgr.allocateStub(StubSize);
Chris Lattner6125fdd2003-05-09 03:30:07 +0000440}
441
Chris Lattner166f2262004-11-22 22:00:25 +0000442void *JITEmitter::finishFunctionStub(const Function *F) {
Chris Lattner6125fdd2003-05-09 03:30:07 +0000443 NumBytes += CurByte-CurBlock;
Chris Lattner6125fdd2003-05-09 03:30:07 +0000444 std::swap(CurBlock, SavedCurBlock);
445 CurByte = SavedCurByte;
446 return SavedCurBlock;
447}
448
Chris Lattner166f2262004-11-22 22:00:25 +0000449void JITEmitter::emitByte(unsigned char B) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000450 *CurByte++ = B; // Write the byte to memory
451}
452
Chris Lattner166f2262004-11-22 22:00:25 +0000453void JITEmitter::emitWord(unsigned W) {
Chris Lattner688506d2003-08-14 18:35:27 +0000454 // This won't work if the endianness of the host and target don't agree! (For
455 // a JIT this can't happen though. :)
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000456 *(unsigned*)CurByte = W;
457 CurByte += sizeof(unsigned);
458}
459
Chris Lattner166f2262004-11-22 22:00:25 +0000460void JITEmitter::emitWordAt(unsigned W, unsigned *Ptr) {
Brian Gaekeaea1b582004-04-23 17:11:14 +0000461 *Ptr = W;
462}
463
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000464// getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry
465// in the constant pool that was last emitted with the 'emitConstantPool'
466// method.
467//
Chris Lattner166f2262004-11-22 22:00:25 +0000468uint64_t JITEmitter::getConstantPoolEntryAddress(unsigned ConstantNum) {
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000469 assert(ConstantNum < ConstantPoolAddresses.size() &&
Misha Brukman3c944972005-04-22 04:08:30 +0000470 "Invalid ConstantPoolIndex!");
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000471 return (intptr_t)ConstantPoolAddresses[ConstantNum];
472}
473
474// getCurrentPCValue - This returns the address that the next emitted byte
475// will be output to.
476//
Chris Lattner166f2262004-11-22 22:00:25 +0000477uint64_t JITEmitter::getCurrentPCValue() {
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000478 return (intptr_t)CurByte;
479}
480
Chris Lattner166f2262004-11-22 22:00:25 +0000481uint64_t JITEmitter::getCurrentPCOffset() {
Chris Lattner5be478f2004-11-20 03:46:14 +0000482 return (intptr_t)CurByte-(intptr_t)CurBlock;
483}
484
Misha Brukmand69c1e62003-07-28 19:09:06 +0000485// getPointerToNamedFunction - This function is used as a global wrapper to
Chris Lattner4d326fa2003-12-20 01:46:27 +0000486// JIT::getPointerToNamedFunction for the purpose of resolving symbols when
Misha Brukmand69c1e62003-07-28 19:09:06 +0000487// bugpoint is debugging the JIT. In that scenario, we are loading an .so and
488// need to resolve function(s) that are being mis-codegenerated, so we need to
489// resolve their addresses at runtime, and this is the way to do it.
490extern "C" {
491 void *getPointerToNamedFunction(const char *Name) {
Chris Lattner4d326fa2003-12-20 01:46:27 +0000492 Module &M = TheJIT->getModule();
Misha Brukmand69c1e62003-07-28 19:09:06 +0000493 if (Function *F = M.getNamedFunction(Name))
Chris Lattner4d326fa2003-12-20 01:46:27 +0000494 return TheJIT->getPointerToFunction(F);
495 return TheJIT->getPointerToNamedFunction(Name);
Misha Brukmand69c1e62003-07-28 19:09:06 +0000496 }
497}