blob: 8b5071b31d503c6c0160da84507da82d26a62a9f [file] [log] [blame]
Chris Lattner166f2262004-11-22 22:00:25 +00001//===-- JITEmitter.cpp - Write machine code to executable memory ----------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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();
Chris Lattner688506d2003-08-14 18:35:27 +000057
58 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 {
123 /// JITResolver - Keep track of, and resolve, call sites for functions that
124 /// have not yet been compiled.
125 class JITResolver {
Chris Lattner5e225582004-11-21 03:37:42 +0000126 /// MCE - The MachineCodeEmitter to use to emit stubs with.
Chris Lattner54266522004-11-20 23:57:07 +0000127 MachineCodeEmitter &MCE;
128
Chris Lattner5e225582004-11-21 03:37:42 +0000129 /// LazyResolverFn - The target lazy resolver function that we actually
130 /// rewrite instructions to use.
131 TargetJITInfo::LazyResolverFn LazyResolverFn;
132
Chris Lattner54266522004-11-20 23:57:07 +0000133 // FunctionToStubMap - Keep track of the stub created for a particular
134 // function so that we can reuse them if necessary.
135 std::map<Function*, void*> FunctionToStubMap;
136
137 // StubToFunctionMap - Keep track of the function that each stub corresponds
138 // to.
139 std::map<void*, Function*> StubToFunctionMap;
140
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000141 /// ExternalFnToStubMap - This is the equivalent of FunctionToStubMap for
142 /// external functions.
143 std::map<void*, void*> ExternalFnToStubMap;
Chris Lattner54266522004-11-20 23:57:07 +0000144 public:
Chris Lattner5e225582004-11-21 03:37:42 +0000145 JITResolver(MachineCodeEmitter &mce) : MCE(mce) {
146 LazyResolverFn =
147 TheJIT->getJITInfo().getLazyResolverFunction(JITCompilerFn);
148 }
Chris Lattner54266522004-11-20 23:57:07 +0000149
150 /// getFunctionStub - This returns a pointer to a function stub, creating
151 /// one on demand as needed.
152 void *getFunctionStub(Function *F);
153
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000154 /// getExternalFunctionStub - Return a stub for the function at the
155 /// specified address, created lazily on demand.
156 void *getExternalFunctionStub(void *FnAddr);
157
Chris Lattner5e225582004-11-21 03:37:42 +0000158 /// AddCallbackAtLocation - If the target is capable of rewriting an
159 /// instruction without the use of a stub, record the location of the use so
160 /// we know which function is being used at the location.
161 void *AddCallbackAtLocation(Function *F, void *Location) {
162 /// Get the target-specific JIT resolver function.
163 StubToFunctionMap[Location] = F;
164 return (void*)LazyResolverFn;
165 }
166
Chris Lattner54266522004-11-20 23:57:07 +0000167 /// JITCompilerFn - This function is called to resolve a stub to a compiled
168 /// address. If the LLVM Function corresponding to the stub has not yet
169 /// been compiled, this function compiles it first.
170 static void *JITCompilerFn(void *Stub);
171 };
172}
173
174/// getJITResolver - This function returns the one instance of the JIT resolver.
175///
176static JITResolver &getJITResolver(MachineCodeEmitter *MCE = 0) {
177 static JITResolver TheJITResolver(*MCE);
178 return TheJITResolver;
179}
180
181/// getFunctionStub - This returns a pointer to a function stub, creating
182/// one on demand as needed.
183void *JITResolver::getFunctionStub(Function *F) {
Chris Lattner54266522004-11-20 23:57:07 +0000184 // If we already have a stub for this function, recycle it.
185 void *&Stub = FunctionToStubMap[F];
186 if (Stub) return Stub;
187
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000188 // Call the lazy resolver function unless we already KNOW it is an external
189 // function, in which case we just skip the lazy resolution step.
190 void *Actual = (void*)LazyResolverFn;
Chris Lattner69435702005-02-20 18:43:35 +0000191 if (F->isExternal() && F->hasExternalLinkage())
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000192 Actual = TheJIT->getPointerToFunction(F);
193
Chris Lattner54266522004-11-20 23:57:07 +0000194 // Otherwise, codegen a new stub. For now, the stub will call the lazy
195 // resolver function.
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000196 Stub = TheJIT->getJITInfo().emitFunctionStub(Actual, MCE);
197
Chris Lattner69435702005-02-20 18:43:35 +0000198 if (Actual != (void*)LazyResolverFn) {
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000199 // If we are getting the stub for an external function, we really want the
200 // address of the stub in the GlobalAddressMap for the JIT, not the address
201 // of the external function.
202 TheJIT->updateGlobalMapping(F, Stub);
203 }
Chris Lattner54266522004-11-20 23:57:07 +0000204
Chris Lattnercb479412004-11-21 03:44:32 +0000205 DEBUG(std::cerr << "JIT: Stub emitted at [" << Stub << "] for function '"
Chris Lattner6f717202004-11-22 21:48:33 +0000206 << F->getName() << "'\n");
Chris Lattnercb479412004-11-21 03:44:32 +0000207
Chris Lattner54266522004-11-20 23:57:07 +0000208 // Finally, keep track of the stub-to-Function mapping so that the
209 // JITCompilerFn knows which function to compile!
210 StubToFunctionMap[Stub] = F;
211 return Stub;
212}
213
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000214/// getExternalFunctionStub - Return a stub for the function at the
215/// specified address, created lazily on demand.
216void *JITResolver::getExternalFunctionStub(void *FnAddr) {
217 // If we already have a stub for this function, recycle it.
218 void *&Stub = ExternalFnToStubMap[FnAddr];
219 if (Stub) return Stub;
220
221 Stub = TheJIT->getJITInfo().emitFunctionStub(FnAddr, MCE);
222 DEBUG(std::cerr << "JIT: Stub emitted at [" << Stub
223 << "] for external function at '" << FnAddr << "'\n");
224 return Stub;
225}
226
227
Chris Lattner54266522004-11-20 23:57:07 +0000228/// JITCompilerFn - This function is called when a lazy compilation stub has
229/// been entered. It looks up which function this stub corresponds to, compiles
230/// it if necessary, then returns the resultant function pointer.
231void *JITResolver::JITCompilerFn(void *Stub) {
232 JITResolver &JR = getJITResolver();
233
234 // The address given to us for the stub may not be exactly right, it might be
235 // a little bit after the stub. As such, use upper_bound to find it.
236 std::map<void*, Function*>::iterator I =
237 JR.StubToFunctionMap.upper_bound(Stub);
238 assert(I != JR.StubToFunctionMap.begin() && "This is not a known stub!");
239 Function *F = (--I)->second;
240
241 // The target function will rewrite the stub so that the compilation callback
242 // function is no longer called from this stub.
243 JR.StubToFunctionMap.erase(I);
244
Chris Lattnercb479412004-11-21 03:44:32 +0000245 DEBUG(std::cerr << "JIT: Lazily resolving function '" << F->getName()
Chris Lattner54266522004-11-20 23:57:07 +0000246 << "' In stub ptr = " << Stub << " actual ptr = "
247 << I->first << "\n");
248
249 void *Result = TheJIT->getPointerToFunction(F);
250
251 // We don't need to reuse this stub in the future, as F is now compiled.
252 JR.FunctionToStubMap.erase(F);
253
254 // FIXME: We could rewrite all references to this stub if we knew them.
255 return Result;
256}
Chris Lattner688506d2003-08-14 18:35:27 +0000257
258
Chris Lattnere518b712004-12-05 07:19:16 +0000259// getPointerToFunctionOrStub - If the specified function has been
260// code-gen'd, return a pointer to the function. If not, compile it, or use
261// a stub to implement lazy compilation if available.
262//
263void *JIT::getPointerToFunctionOrStub(Function *F) {
264 // If we have already code generated the function, just return the address.
265 if (void *Addr = getPointerToGlobalIfAvailable(F))
266 return Addr;
267
268 // Get a stub if the target supports it
269 return getJITResolver(MCE).getFunctionStub(F);
270}
271
272
273
Chris Lattner54266522004-11-20 23:57:07 +0000274//===----------------------------------------------------------------------===//
Chris Lattner166f2262004-11-22 22:00:25 +0000275// JITEmitter code.
Chris Lattner54266522004-11-20 23:57:07 +0000276//
Chris Lattner688506d2003-08-14 18:35:27 +0000277namespace {
Chris Lattner166f2262004-11-22 22:00:25 +0000278 /// JITEmitter - The JIT implementation of the MachineCodeEmitter, which is
279 /// used to output functions to memory for execution.
280 class JITEmitter : public MachineCodeEmitter {
Chris Lattner688506d2003-08-14 18:35:27 +0000281 JITMemoryManager MemMgr;
282
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000283 // CurBlock - The start of the current block of memory. CurByte - The
284 // current byte being emitted to.
Chris Lattner6125fdd2003-05-09 03:30:07 +0000285 unsigned char *CurBlock, *CurByte;
286
287 // When outputting a function stub in the context of some other function, we
288 // save CurBlock and CurByte here.
289 unsigned char *SavedCurBlock, *SavedCurByte;
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000290
291 // ConstantPoolAddresses - Contains the location for each entry in the
292 // constant pool.
Chris Lattner1cc08382003-01-13 01:00:12 +0000293 std::vector<void*> ConstantPoolAddresses;
Chris Lattner5be478f2004-11-20 03:46:14 +0000294
295 /// Relocations - These are the relocations that the function needs, as
296 /// emitted.
297 std::vector<MachineRelocation> Relocations;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000298 public:
Chris Lattner166f2262004-11-22 22:00:25 +0000299 JITEmitter(JIT &jit) { TheJIT = &jit; }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000300
301 virtual void startFunction(MachineFunction &F);
302 virtual void finishFunction(MachineFunction &F);
Chris Lattner1cc08382003-01-13 01:00:12 +0000303 virtual void emitConstantPool(MachineConstantPool *MCP);
Chris Lattner54266522004-11-20 23:57:07 +0000304 virtual void startFunctionStub(unsigned StubSize);
305 virtual void* finishFunctionStub(const Function *F);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000306 virtual void emitByte(unsigned char B);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000307 virtual void emitWord(unsigned W);
Brian Gaekeaea1b582004-04-23 17:11:14 +0000308 virtual void emitWordAt(unsigned W, unsigned *Ptr);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000309
Chris Lattner5be478f2004-11-20 03:46:14 +0000310 virtual void addRelocation(const MachineRelocation &MR) {
311 Relocations.push_back(MR);
312 }
313
314 virtual uint64_t getCurrentPCValue();
315 virtual uint64_t getCurrentPCOffset();
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000316 virtual uint64_t getConstantPoolEntryAddress(unsigned Entry);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000317
Chris Lattner54266522004-11-20 23:57:07 +0000318 private:
Chris Lattner5e225582004-11-21 03:37:42 +0000319 void *getPointerToGlobal(GlobalValue *GV, void *Reference, bool NoNeedStub);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000320 };
321}
322
Chris Lattner4d326fa2003-12-20 01:46:27 +0000323MachineCodeEmitter *JIT::createEmitter(JIT &jit) {
Chris Lattner166f2262004-11-22 22:00:25 +0000324 return new JITEmitter(jit);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000325}
326
Chris Lattner166f2262004-11-22 22:00:25 +0000327void *JITEmitter::getPointerToGlobal(GlobalValue *V, void *Reference,
328 bool DoesntNeedStub) {
Chris Lattner54266522004-11-20 23:57:07 +0000329 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
330 /// FIXME: If we straightened things out, this could actually emit the
331 /// global immediately instead of queuing it for codegen later!
Chris Lattner54266522004-11-20 23:57:07 +0000332 return TheJIT->getOrEmitGlobalVariable(GV);
333 }
334
335 // If we have already compiled the function, return a pointer to its body.
336 Function *F = cast<Function>(V);
337 void *ResultPtr = TheJIT->getPointerToGlobalIfAvailable(F);
338 if (ResultPtr) return ResultPtr;
339
Chris Lattner532343b2004-11-30 17:41:49 +0000340 if (F->hasExternalLinkage() && F->isExternal()) {
Chris Lattner54266522004-11-20 23:57:07 +0000341 // If this is an external function pointer, we can force the JIT to
342 // 'compile' it, which really just adds it to the map.
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000343 if (DoesntNeedStub)
344 return TheJIT->getPointerToFunction(F);
345
346 return getJITResolver(this).getFunctionStub(F);
Chris Lattner54266522004-11-20 23:57:07 +0000347 }
348
Chris Lattner5e225582004-11-21 03:37:42 +0000349 // Okay, the function has not been compiled yet, if the target callback
350 // mechanism is capable of rewriting the instruction directly, prefer to do
351 // that instead of emitting a stub.
352 if (DoesntNeedStub)
353 return getJITResolver(this).AddCallbackAtLocation(F, Reference);
354
Chris Lattner54266522004-11-20 23:57:07 +0000355 // Otherwise, we have to emit a lazy resolving stub.
356 return getJITResolver(this).getFunctionStub(F);
357}
358
Chris Lattner166f2262004-11-22 22:00:25 +0000359void JITEmitter::startFunction(MachineFunction &F) {
Chris Lattner688506d2003-08-14 18:35:27 +0000360 CurByte = CurBlock = MemMgr.startFunctionBody();
Chris Lattner4d326fa2003-12-20 01:46:27 +0000361 TheJIT->addGlobalMapping(F.getFunction(), CurBlock);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000362}
363
Chris Lattner166f2262004-11-22 22:00:25 +0000364void JITEmitter::finishFunction(MachineFunction &F) {
Chris Lattner688506d2003-08-14 18:35:27 +0000365 MemMgr.endFunctionBody(CurByte);
Chris Lattner1cc08382003-01-13 01:00:12 +0000366 ConstantPoolAddresses.clear();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000367 NumBytes += CurByte-CurBlock;
368
Chris Lattner5be478f2004-11-20 03:46:14 +0000369 if (!Relocations.empty()) {
370 // Resolve the relocations to concrete pointers.
371 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
372 MachineRelocation &MR = Relocations[i];
373 void *ResultPtr;
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000374 if (MR.isString()) {
Chris Lattner5be478f2004-11-20 03:46:14 +0000375 ResultPtr = TheJIT->getPointerToNamedFunction(MR.getString());
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000376
377 // If the target REALLY wants a stub for this function, emit it now.
378 if (!MR.doesntNeedFunctionStub())
379 ResultPtr = getJITResolver(this).getExternalFunctionStub(ResultPtr);
380 } else
Chris Lattner5e225582004-11-21 03:37:42 +0000381 ResultPtr = getPointerToGlobal(MR.getGlobalValue(),
382 CurBlock+MR.getMachineCodeOffset(),
383 MR.doesntNeedFunctionStub());
Chris Lattner5be478f2004-11-20 03:46:14 +0000384 MR.setResultPointer(ResultPtr);
385 }
386
387 TheJIT->getJITInfo().relocate(CurBlock, &Relocations[0],
388 Relocations.size());
389 }
390
Chris Lattnercb479412004-11-21 03:44:32 +0000391 DEBUG(std::cerr << "JIT: Finished CodeGen of [" << (void*)CurBlock
Misha Brukman1d440852003-06-06 06:52:35 +0000392 << "] Function: " << F.getFunction()->getName()
Chris Lattner5be478f2004-11-20 03:46:14 +0000393 << ": " << CurByte-CurBlock << " bytes of text, "
394 << Relocations.size() << " relocations\n");
395 Relocations.clear();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000396}
397
Chris Lattner166f2262004-11-22 22:00:25 +0000398void JITEmitter::emitConstantPool(MachineConstantPool *MCP) {
Chris Lattner1cc08382003-01-13 01:00:12 +0000399 const std::vector<Constant*> &Constants = MCP->getConstants();
Chris Lattner2c0a6a12003-11-30 04:23:21 +0000400 if (Constants.empty()) return;
401
Chris Lattner1cc08382003-01-13 01:00:12 +0000402 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Chris Lattner2c0a6a12003-11-30 04:23:21 +0000403 const Type *Ty = Constants[i]->getType();
Chris Lattnera8101c12005-01-08 20:07:03 +0000404 unsigned Size = (unsigned)TheJIT->getTargetData().getTypeSize(Ty);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000405 unsigned Alignment = TheJIT->getTargetData().getTypeAlignment(Ty);
Chris Lattner2c0a6a12003-11-30 04:23:21 +0000406
Chris Lattner281a6012005-01-10 18:23:22 +0000407 void *Addr = MemMgr.allocateConstant(Size, Alignment);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000408 TheJIT->InitializeMemory(Constants[i], Addr);
Misha Brukman91de3522003-11-30 00:50:53 +0000409 ConstantPoolAddresses.push_back(Addr);
Chris Lattner1cc08382003-01-13 01:00:12 +0000410 }
411}
412
Chris Lattner166f2262004-11-22 22:00:25 +0000413void JITEmitter::startFunctionStub(unsigned StubSize) {
Chris Lattner6125fdd2003-05-09 03:30:07 +0000414 SavedCurBlock = CurBlock; SavedCurByte = CurByte;
Chris Lattner688506d2003-08-14 18:35:27 +0000415 CurByte = CurBlock = MemMgr.allocateStub(StubSize);
Chris Lattner6125fdd2003-05-09 03:30:07 +0000416}
417
Chris Lattner166f2262004-11-22 22:00:25 +0000418void *JITEmitter::finishFunctionStub(const Function *F) {
Chris Lattner6125fdd2003-05-09 03:30:07 +0000419 NumBytes += CurByte-CurBlock;
Chris Lattner6125fdd2003-05-09 03:30:07 +0000420 std::swap(CurBlock, SavedCurBlock);
421 CurByte = SavedCurByte;
422 return SavedCurBlock;
423}
424
Chris Lattner166f2262004-11-22 22:00:25 +0000425void JITEmitter::emitByte(unsigned char B) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000426 *CurByte++ = B; // Write the byte to memory
427}
428
Chris Lattner166f2262004-11-22 22:00:25 +0000429void JITEmitter::emitWord(unsigned W) {
Chris Lattner688506d2003-08-14 18:35:27 +0000430 // This won't work if the endianness of the host and target don't agree! (For
431 // a JIT this can't happen though. :)
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000432 *(unsigned*)CurByte = W;
433 CurByte += sizeof(unsigned);
434}
435
Chris Lattner166f2262004-11-22 22:00:25 +0000436void JITEmitter::emitWordAt(unsigned W, unsigned *Ptr) {
Brian Gaekeaea1b582004-04-23 17:11:14 +0000437 *Ptr = W;
438}
439
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000440// getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry
441// in the constant pool that was last emitted with the 'emitConstantPool'
442// method.
443//
Chris Lattner166f2262004-11-22 22:00:25 +0000444uint64_t JITEmitter::getConstantPoolEntryAddress(unsigned ConstantNum) {
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000445 assert(ConstantNum < ConstantPoolAddresses.size() &&
446 "Invalid ConstantPoolIndex!");
447 return (intptr_t)ConstantPoolAddresses[ConstantNum];
448}
449
450// getCurrentPCValue - This returns the address that the next emitted byte
451// will be output to.
452//
Chris Lattner166f2262004-11-22 22:00:25 +0000453uint64_t JITEmitter::getCurrentPCValue() {
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000454 return (intptr_t)CurByte;
455}
456
Chris Lattner166f2262004-11-22 22:00:25 +0000457uint64_t JITEmitter::getCurrentPCOffset() {
Chris Lattner5be478f2004-11-20 03:46:14 +0000458 return (intptr_t)CurByte-(intptr_t)CurBlock;
459}
460
Misha Brukmand69c1e62003-07-28 19:09:06 +0000461// getPointerToNamedFunction - This function is used as a global wrapper to
Chris Lattner4d326fa2003-12-20 01:46:27 +0000462// JIT::getPointerToNamedFunction for the purpose of resolving symbols when
Misha Brukmand69c1e62003-07-28 19:09:06 +0000463// bugpoint is debugging the JIT. In that scenario, we are loading an .so and
464// need to resolve function(s) that are being mis-codegenerated, so we need to
465// resolve their addresses at runtime, and this is the way to do it.
466extern "C" {
467 void *getPointerToNamedFunction(const char *Name) {
Chris Lattner4d326fa2003-12-20 01:46:27 +0000468 Module &M = TheJIT->getModule();
Misha Brukmand69c1e62003-07-28 19:09:06 +0000469 if (Function *F = M.getNamedFunction(Name))
Chris Lattner4d326fa2003-12-20 01:46:27 +0000470 return TheJIT->getPointerToFunction(F);
471 return TheJIT->getPointerToNamedFunction(Name);
Misha Brukmand69c1e62003-07-28 19:09:06 +0000472 }
473}