blob: 7f6a89ed4858bd02a2f47f02938b9f3915dce96c [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 Lattnerbd199fb2002-12-24 00:01:05 +000019#include "llvm/CodeGen/MachineCodeEmitter.h"
20#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner1cc08382003-01-13 01:00:12 +000021#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner5be478f2004-11-20 03:46:14 +000022#include "llvm/CodeGen/MachineRelocation.h"
Chris Lattner1cc08382003-01-13 01:00:12 +000023#include "llvm/Target/TargetData.h"
Chris Lattner5be478f2004-11-20 03:46:14 +000024#include "llvm/Target/TargetJITInfo.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000025#include "llvm/Support/Debug.h"
26#include "llvm/ADT/Statistic.h"
Reid Spencer52b0ba62004-09-11 04:31:03 +000027#include "llvm/System/Memory.h"
Chris Lattnerc19aade2003-12-08 08:06:28 +000028using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Chris Lattnerbd199fb2002-12-24 00:01:05 +000030namespace {
Chris Lattnere7386562003-10-20 05:45:49 +000031 Statistic<> NumBytes("jit", "Number of bytes of machine code compiled");
Chris Lattner4d326fa2003-12-20 01:46:27 +000032 JIT *TheJIT = 0;
Chris Lattner54266522004-11-20 23:57:07 +000033}
Chris Lattnerbd199fb2002-12-24 00:01:05 +000034
Chris Lattner54266522004-11-20 23:57:07 +000035
36//===----------------------------------------------------------------------===//
37// JITMemoryManager code.
38//
39namespace {
Chris Lattner688506d2003-08-14 18:35:27 +000040 /// JITMemoryManager - Manage memory for the JIT code generation in a logical,
41 /// sane way. This splits a large block of MAP_NORESERVE'd memory into two
42 /// sections, one for function stubs, one for the functions themselves. We
43 /// have to do this because we may need to emit a function stub while in the
44 /// middle of emitting a function, and we don't know how large the function we
45 /// are emitting is. This never bothers to release the memory, because when
46 /// we are ready to destroy the JIT, the program exits.
47 class JITMemoryManager {
Reid Spencer33189e72004-09-13 22:38:11 +000048 sys::MemoryBlock MemBlock; // Virtual memory block allocated RWX
Chris Lattner688506d2003-08-14 18:35:27 +000049 unsigned char *MemBase; // Base of block of memory, start of stub mem
50 unsigned char *FunctionBase; // Start of the function body area
Chris Lattner281a6012005-01-10 18:23:22 +000051 unsigned char *ConstantPool; // Memory allocated for constant pools
52 unsigned char *CurStubPtr, *CurFunctionPtr, *CurConstantPtr;
Chris Lattner688506d2003-08-14 18:35:27 +000053 public:
54 JITMemoryManager();
Reid Spencer4af3da62004-12-13 16:04:04 +000055 ~JITMemoryManager();
Chris Lattner688506d2003-08-14 18:35:27 +000056
57 inline unsigned char *allocateStub(unsigned StubSize);
Chris Lattner281a6012005-01-10 18:23:22 +000058 inline unsigned char *allocateConstant(unsigned ConstantSize,
59 unsigned Alignment);
Chris Lattner688506d2003-08-14 18:35:27 +000060 inline unsigned char *startFunctionBody();
Chris Lattner281a6012005-01-10 18:23:22 +000061 inline void endFunctionBody(unsigned char *FunctionEnd);
Chris Lattner688506d2003-08-14 18:35:27 +000062 };
63}
64
Chris Lattner688506d2003-08-14 18:35:27 +000065JITMemoryManager::JITMemoryManager() {
66 // Allocate a 16M block of memory...
Reid Spencer33189e72004-09-13 22:38:11 +000067 MemBlock = sys::Memory::AllocateRWX((16 << 20));
Reid Spencer52b0ba62004-09-11 04:31:03 +000068 MemBase = reinterpret_cast<unsigned char*>(MemBlock.base());
Chris Lattner688506d2003-08-14 18:35:27 +000069 FunctionBase = MemBase + 512*1024; // Use 512k for stubs
70
71 // Allocate stubs backwards from the function base, allocate functions forward
72 // from the function base.
73 CurStubPtr = CurFunctionPtr = FunctionBase;
Chris Lattner281a6012005-01-10 18:23:22 +000074
75 ConstantPool = new unsigned char [512*1024]; // Use 512k for constant pools
76 CurConstantPtr = ConstantPool + 512*1024;
Chris Lattner688506d2003-08-14 18:35:27 +000077}
78
Reid Spencer4af3da62004-12-13 16:04:04 +000079JITMemoryManager::~JITMemoryManager() {
80 sys::Memory::ReleaseRWX(MemBlock);
Chris Lattner281a6012005-01-10 18:23:22 +000081 delete[] ConstantPool;
Reid Spencer4af3da62004-12-13 16:04:04 +000082}
83
Chris Lattner688506d2003-08-14 18:35:27 +000084unsigned char *JITMemoryManager::allocateStub(unsigned StubSize) {
85 CurStubPtr -= StubSize;
86 if (CurStubPtr < MemBase) {
87 std::cerr << "JIT ran out of memory for function stubs!\n";
88 abort();
89 }
90 return CurStubPtr;
91}
92
Chris Lattner281a6012005-01-10 18:23:22 +000093unsigned char *JITMemoryManager::allocateConstant(unsigned ConstantSize,
94 unsigned Alignment) {
95 // Reserve space and align pointer.
96 CurConstantPtr -= ConstantSize;
97 CurConstantPtr =
98 (unsigned char *)((intptr_t)CurConstantPtr & ~((intptr_t)Alignment - 1));
99
100 if (CurConstantPtr < ConstantPool) {
101 std::cerr << "JIT ran out of memory for constant pools!\n";
102 abort();
103 }
104 return CurConstantPtr;
105}
106
Chris Lattner688506d2003-08-14 18:35:27 +0000107unsigned char *JITMemoryManager::startFunctionBody() {
Chris Lattner5be478f2004-11-20 03:46:14 +0000108 // Round up to an even multiple of 8 bytes, this should eventually be target
Chris Lattner688506d2003-08-14 18:35:27 +0000109 // specific.
Chris Lattner5be478f2004-11-20 03:46:14 +0000110 return (unsigned char*)(((intptr_t)CurFunctionPtr + 7) & ~7);
Chris Lattner688506d2003-08-14 18:35:27 +0000111}
112
113void JITMemoryManager::endFunctionBody(unsigned char *FunctionEnd) {
114 assert(FunctionEnd > CurFunctionPtr);
115 CurFunctionPtr = FunctionEnd;
116}
117
Chris Lattner54266522004-11-20 23:57:07 +0000118//===----------------------------------------------------------------------===//
119// JIT lazy compilation code.
120//
121namespace {
122 /// JITResolver - Keep track of, and resolve, call sites for functions that
123 /// have not yet been compiled.
124 class JITResolver {
Chris Lattner5e225582004-11-21 03:37:42 +0000125 /// MCE - The MachineCodeEmitter to use to emit stubs with.
Chris Lattner54266522004-11-20 23:57:07 +0000126 MachineCodeEmitter &MCE;
127
Chris Lattner5e225582004-11-21 03:37:42 +0000128 /// LazyResolverFn - The target lazy resolver function that we actually
129 /// rewrite instructions to use.
130 TargetJITInfo::LazyResolverFn LazyResolverFn;
131
Chris Lattner54266522004-11-20 23:57:07 +0000132 // FunctionToStubMap - Keep track of the stub created for a particular
133 // function so that we can reuse them if necessary.
134 std::map<Function*, void*> FunctionToStubMap;
135
136 // StubToFunctionMap - Keep track of the function that each stub corresponds
137 // to.
138 std::map<void*, Function*> StubToFunctionMap;
139
140 public:
Chris Lattner5e225582004-11-21 03:37:42 +0000141 JITResolver(MachineCodeEmitter &mce) : MCE(mce) {
142 LazyResolverFn =
143 TheJIT->getJITInfo().getLazyResolverFunction(JITCompilerFn);
144 }
Chris Lattner54266522004-11-20 23:57:07 +0000145
146 /// getFunctionStub - This returns a pointer to a function stub, creating
147 /// one on demand as needed.
148 void *getFunctionStub(Function *F);
149
Chris Lattner5e225582004-11-21 03:37:42 +0000150 /// AddCallbackAtLocation - If the target is capable of rewriting an
151 /// instruction without the use of a stub, record the location of the use so
152 /// we know which function is being used at the location.
153 void *AddCallbackAtLocation(Function *F, void *Location) {
154 /// Get the target-specific JIT resolver function.
155 StubToFunctionMap[Location] = F;
156 return (void*)LazyResolverFn;
157 }
158
Chris Lattner54266522004-11-20 23:57:07 +0000159 /// JITCompilerFn - This function is called to resolve a stub to a compiled
160 /// address. If the LLVM Function corresponding to the stub has not yet
161 /// been compiled, this function compiles it first.
162 static void *JITCompilerFn(void *Stub);
163 };
164}
165
166/// getJITResolver - This function returns the one instance of the JIT resolver.
167///
168static JITResolver &getJITResolver(MachineCodeEmitter *MCE = 0) {
169 static JITResolver TheJITResolver(*MCE);
170 return TheJITResolver;
171}
172
173/// getFunctionStub - This returns a pointer to a function stub, creating
174/// one on demand as needed.
175void *JITResolver::getFunctionStub(Function *F) {
Chris Lattner54266522004-11-20 23:57:07 +0000176 // If we already have a stub for this function, recycle it.
177 void *&Stub = FunctionToStubMap[F];
178 if (Stub) return Stub;
179
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000180 // Call the lazy resolver function unless we already KNOW it is an external
181 // function, in which case we just skip the lazy resolution step.
182 void *Actual = (void*)LazyResolverFn;
183 if (F->hasExternalLinkage())
184 Actual = TheJIT->getPointerToFunction(F);
185
Chris Lattner54266522004-11-20 23:57:07 +0000186 // Otherwise, codegen a new stub. For now, the stub will call the lazy
187 // resolver function.
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000188 Stub = TheJIT->getJITInfo().emitFunctionStub(Actual, MCE);
189
190 if (F->hasExternalLinkage()) {
191 // If we are getting the stub for an external function, we really want the
192 // address of the stub in the GlobalAddressMap for the JIT, not the address
193 // of the external function.
194 TheJIT->updateGlobalMapping(F, Stub);
195 }
Chris Lattner54266522004-11-20 23:57:07 +0000196
Chris Lattnercb479412004-11-21 03:44:32 +0000197 DEBUG(std::cerr << "JIT: Stub emitted at [" << Stub << "] for function '"
Chris Lattner6f717202004-11-22 21:48:33 +0000198 << F->getName() << "'\n");
Chris Lattnercb479412004-11-21 03:44:32 +0000199
Chris Lattner54266522004-11-20 23:57:07 +0000200 // Finally, keep track of the stub-to-Function mapping so that the
201 // JITCompilerFn knows which function to compile!
202 StubToFunctionMap[Stub] = F;
203 return Stub;
204}
205
206/// JITCompilerFn - This function is called when a lazy compilation stub has
207/// been entered. It looks up which function this stub corresponds to, compiles
208/// it if necessary, then returns the resultant function pointer.
209void *JITResolver::JITCompilerFn(void *Stub) {
210 JITResolver &JR = getJITResolver();
211
212 // The address given to us for the stub may not be exactly right, it might be
213 // a little bit after the stub. As such, use upper_bound to find it.
214 std::map<void*, Function*>::iterator I =
215 JR.StubToFunctionMap.upper_bound(Stub);
216 assert(I != JR.StubToFunctionMap.begin() && "This is not a known stub!");
217 Function *F = (--I)->second;
218
219 // The target function will rewrite the stub so that the compilation callback
220 // function is no longer called from this stub.
221 JR.StubToFunctionMap.erase(I);
222
Chris Lattnercb479412004-11-21 03:44:32 +0000223 DEBUG(std::cerr << "JIT: Lazily resolving function '" << F->getName()
Chris Lattner54266522004-11-20 23:57:07 +0000224 << "' In stub ptr = " << Stub << " actual ptr = "
225 << I->first << "\n");
226
227 void *Result = TheJIT->getPointerToFunction(F);
228
229 // We don't need to reuse this stub in the future, as F is now compiled.
230 JR.FunctionToStubMap.erase(F);
231
232 // FIXME: We could rewrite all references to this stub if we knew them.
233 return Result;
234}
Chris Lattner688506d2003-08-14 18:35:27 +0000235
236
Chris Lattnere518b712004-12-05 07:19:16 +0000237// getPointerToFunctionOrStub - If the specified function has been
238// code-gen'd, return a pointer to the function. If not, compile it, or use
239// a stub to implement lazy compilation if available.
240//
241void *JIT::getPointerToFunctionOrStub(Function *F) {
242 // If we have already code generated the function, just return the address.
243 if (void *Addr = getPointerToGlobalIfAvailable(F))
244 return Addr;
245
246 // Get a stub if the target supports it
247 return getJITResolver(MCE).getFunctionStub(F);
248}
249
250
251
Chris Lattner54266522004-11-20 23:57:07 +0000252//===----------------------------------------------------------------------===//
Chris Lattner166f2262004-11-22 22:00:25 +0000253// JITEmitter code.
Chris Lattner54266522004-11-20 23:57:07 +0000254//
Chris Lattner688506d2003-08-14 18:35:27 +0000255namespace {
Chris Lattner166f2262004-11-22 22:00:25 +0000256 /// JITEmitter - The JIT implementation of the MachineCodeEmitter, which is
257 /// used to output functions to memory for execution.
258 class JITEmitter : public MachineCodeEmitter {
Chris Lattner688506d2003-08-14 18:35:27 +0000259 JITMemoryManager MemMgr;
260
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000261 // CurBlock - The start of the current block of memory. CurByte - The
262 // current byte being emitted to.
Chris Lattner6125fdd2003-05-09 03:30:07 +0000263 unsigned char *CurBlock, *CurByte;
264
265 // When outputting a function stub in the context of some other function, we
266 // save CurBlock and CurByte here.
267 unsigned char *SavedCurBlock, *SavedCurByte;
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000268
269 // ConstantPoolAddresses - Contains the location for each entry in the
270 // constant pool.
Chris Lattner1cc08382003-01-13 01:00:12 +0000271 std::vector<void*> ConstantPoolAddresses;
Chris Lattner5be478f2004-11-20 03:46:14 +0000272
273 /// Relocations - These are the relocations that the function needs, as
274 /// emitted.
275 std::vector<MachineRelocation> Relocations;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000276 public:
Chris Lattner166f2262004-11-22 22:00:25 +0000277 JITEmitter(JIT &jit) { TheJIT = &jit; }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000278
279 virtual void startFunction(MachineFunction &F);
280 virtual void finishFunction(MachineFunction &F);
Chris Lattner1cc08382003-01-13 01:00:12 +0000281 virtual void emitConstantPool(MachineConstantPool *MCP);
Chris Lattner54266522004-11-20 23:57:07 +0000282 virtual void startFunctionStub(unsigned StubSize);
283 virtual void* finishFunctionStub(const Function *F);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000284 virtual void emitByte(unsigned char B);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000285 virtual void emitWord(unsigned W);
Brian Gaekeaea1b582004-04-23 17:11:14 +0000286 virtual void emitWordAt(unsigned W, unsigned *Ptr);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000287
Chris Lattner5be478f2004-11-20 03:46:14 +0000288 virtual void addRelocation(const MachineRelocation &MR) {
289 Relocations.push_back(MR);
290 }
291
292 virtual uint64_t getCurrentPCValue();
293 virtual uint64_t getCurrentPCOffset();
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000294 virtual uint64_t getConstantPoolEntryAddress(unsigned Entry);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000295
Chris Lattner54266522004-11-20 23:57:07 +0000296 private:
Chris Lattner5e225582004-11-21 03:37:42 +0000297 void *getPointerToGlobal(GlobalValue *GV, void *Reference, bool NoNeedStub);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000298 };
299}
300
Chris Lattner4d326fa2003-12-20 01:46:27 +0000301MachineCodeEmitter *JIT::createEmitter(JIT &jit) {
Chris Lattner166f2262004-11-22 22:00:25 +0000302 return new JITEmitter(jit);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000303}
304
Chris Lattner166f2262004-11-22 22:00:25 +0000305void *JITEmitter::getPointerToGlobal(GlobalValue *V, void *Reference,
306 bool DoesntNeedStub) {
Chris Lattner54266522004-11-20 23:57:07 +0000307 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
308 /// FIXME: If we straightened things out, this could actually emit the
309 /// global immediately instead of queuing it for codegen later!
Chris Lattner54266522004-11-20 23:57:07 +0000310 return TheJIT->getOrEmitGlobalVariable(GV);
311 }
312
313 // If we have already compiled the function, return a pointer to its body.
314 Function *F = cast<Function>(V);
315 void *ResultPtr = TheJIT->getPointerToGlobalIfAvailable(F);
316 if (ResultPtr) return ResultPtr;
317
Chris Lattner532343b2004-11-30 17:41:49 +0000318 if (F->hasExternalLinkage() && F->isExternal()) {
Chris Lattner54266522004-11-20 23:57:07 +0000319 // If this is an external function pointer, we can force the JIT to
320 // 'compile' it, which really just adds it to the map.
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000321 if (DoesntNeedStub)
322 return TheJIT->getPointerToFunction(F);
323
324 return getJITResolver(this).getFunctionStub(F);
Chris Lattner54266522004-11-20 23:57:07 +0000325 }
326
Chris Lattner5e225582004-11-21 03:37:42 +0000327 // Okay, the function has not been compiled yet, if the target callback
328 // mechanism is capable of rewriting the instruction directly, prefer to do
329 // that instead of emitting a stub.
330 if (DoesntNeedStub)
331 return getJITResolver(this).AddCallbackAtLocation(F, Reference);
332
Chris Lattner54266522004-11-20 23:57:07 +0000333 // Otherwise, we have to emit a lazy resolving stub.
334 return getJITResolver(this).getFunctionStub(F);
335}
336
Chris Lattner166f2262004-11-22 22:00:25 +0000337void JITEmitter::startFunction(MachineFunction &F) {
Chris Lattner688506d2003-08-14 18:35:27 +0000338 CurByte = CurBlock = MemMgr.startFunctionBody();
Chris Lattner4d326fa2003-12-20 01:46:27 +0000339 TheJIT->addGlobalMapping(F.getFunction(), CurBlock);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000340}
341
Chris Lattner166f2262004-11-22 22:00:25 +0000342void JITEmitter::finishFunction(MachineFunction &F) {
Chris Lattner688506d2003-08-14 18:35:27 +0000343 MemMgr.endFunctionBody(CurByte);
Chris Lattner1cc08382003-01-13 01:00:12 +0000344 ConstantPoolAddresses.clear();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000345 NumBytes += CurByte-CurBlock;
346
Chris Lattner5be478f2004-11-20 03:46:14 +0000347 if (!Relocations.empty()) {
348 // Resolve the relocations to concrete pointers.
349 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
350 MachineRelocation &MR = Relocations[i];
351 void *ResultPtr;
Chris Lattner54266522004-11-20 23:57:07 +0000352 if (MR.isString())
Chris Lattner5be478f2004-11-20 03:46:14 +0000353 ResultPtr = TheJIT->getPointerToNamedFunction(MR.getString());
Chris Lattner54266522004-11-20 23:57:07 +0000354 else
Chris Lattner5e225582004-11-21 03:37:42 +0000355 ResultPtr = getPointerToGlobal(MR.getGlobalValue(),
356 CurBlock+MR.getMachineCodeOffset(),
357 MR.doesntNeedFunctionStub());
Chris Lattner5be478f2004-11-20 03:46:14 +0000358 MR.setResultPointer(ResultPtr);
359 }
360
361 TheJIT->getJITInfo().relocate(CurBlock, &Relocations[0],
362 Relocations.size());
363 }
364
Chris Lattnercb479412004-11-21 03:44:32 +0000365 DEBUG(std::cerr << "JIT: Finished CodeGen of [" << (void*)CurBlock
Misha Brukman1d440852003-06-06 06:52:35 +0000366 << "] Function: " << F.getFunction()->getName()
Chris Lattner5be478f2004-11-20 03:46:14 +0000367 << ": " << CurByte-CurBlock << " bytes of text, "
368 << Relocations.size() << " relocations\n");
369 Relocations.clear();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000370}
371
Chris Lattner166f2262004-11-22 22:00:25 +0000372void JITEmitter::emitConstantPool(MachineConstantPool *MCP) {
Chris Lattner1cc08382003-01-13 01:00:12 +0000373 const std::vector<Constant*> &Constants = MCP->getConstants();
Chris Lattner2c0a6a12003-11-30 04:23:21 +0000374 if (Constants.empty()) return;
375
Chris Lattner1cc08382003-01-13 01:00:12 +0000376 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Chris Lattner2c0a6a12003-11-30 04:23:21 +0000377 const Type *Ty = Constants[i]->getType();
Chris Lattnera8101c12005-01-08 20:07:03 +0000378 unsigned Size = (unsigned)TheJIT->getTargetData().getTypeSize(Ty);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000379 unsigned Alignment = TheJIT->getTargetData().getTypeAlignment(Ty);
Chris Lattner2c0a6a12003-11-30 04:23:21 +0000380
Chris Lattner281a6012005-01-10 18:23:22 +0000381 void *Addr = MemMgr.allocateConstant(Size, Alignment);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000382 TheJIT->InitializeMemory(Constants[i], Addr);
Misha Brukman91de3522003-11-30 00:50:53 +0000383 ConstantPoolAddresses.push_back(Addr);
Chris Lattner1cc08382003-01-13 01:00:12 +0000384 }
385}
386
Chris Lattner166f2262004-11-22 22:00:25 +0000387void JITEmitter::startFunctionStub(unsigned StubSize) {
Chris Lattner6125fdd2003-05-09 03:30:07 +0000388 SavedCurBlock = CurBlock; SavedCurByte = CurByte;
Chris Lattner688506d2003-08-14 18:35:27 +0000389 CurByte = CurBlock = MemMgr.allocateStub(StubSize);
Chris Lattner6125fdd2003-05-09 03:30:07 +0000390}
391
Chris Lattner166f2262004-11-22 22:00:25 +0000392void *JITEmitter::finishFunctionStub(const Function *F) {
Chris Lattner6125fdd2003-05-09 03:30:07 +0000393 NumBytes += CurByte-CurBlock;
Chris Lattner6125fdd2003-05-09 03:30:07 +0000394 std::swap(CurBlock, SavedCurBlock);
395 CurByte = SavedCurByte;
396 return SavedCurBlock;
397}
398
Chris Lattner166f2262004-11-22 22:00:25 +0000399void JITEmitter::emitByte(unsigned char B) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000400 *CurByte++ = B; // Write the byte to memory
401}
402
Chris Lattner166f2262004-11-22 22:00:25 +0000403void JITEmitter::emitWord(unsigned W) {
Chris Lattner688506d2003-08-14 18:35:27 +0000404 // This won't work if the endianness of the host and target don't agree! (For
405 // a JIT this can't happen though. :)
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000406 *(unsigned*)CurByte = W;
407 CurByte += sizeof(unsigned);
408}
409
Chris Lattner166f2262004-11-22 22:00:25 +0000410void JITEmitter::emitWordAt(unsigned W, unsigned *Ptr) {
Brian Gaekeaea1b582004-04-23 17:11:14 +0000411 *Ptr = W;
412}
413
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000414// getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry
415// in the constant pool that was last emitted with the 'emitConstantPool'
416// method.
417//
Chris Lattner166f2262004-11-22 22:00:25 +0000418uint64_t JITEmitter::getConstantPoolEntryAddress(unsigned ConstantNum) {
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000419 assert(ConstantNum < ConstantPoolAddresses.size() &&
420 "Invalid ConstantPoolIndex!");
421 return (intptr_t)ConstantPoolAddresses[ConstantNum];
422}
423
424// getCurrentPCValue - This returns the address that the next emitted byte
425// will be output to.
426//
Chris Lattner166f2262004-11-22 22:00:25 +0000427uint64_t JITEmitter::getCurrentPCValue() {
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000428 return (intptr_t)CurByte;
429}
430
Chris Lattner166f2262004-11-22 22:00:25 +0000431uint64_t JITEmitter::getCurrentPCOffset() {
Chris Lattner5be478f2004-11-20 03:46:14 +0000432 return (intptr_t)CurByte-(intptr_t)CurBlock;
433}
434
Misha Brukmand69c1e62003-07-28 19:09:06 +0000435// getPointerToNamedFunction - This function is used as a global wrapper to
Chris Lattner4d326fa2003-12-20 01:46:27 +0000436// JIT::getPointerToNamedFunction for the purpose of resolving symbols when
Misha Brukmand69c1e62003-07-28 19:09:06 +0000437// bugpoint is debugging the JIT. In that scenario, we are loading an .so and
438// need to resolve function(s) that are being mis-codegenerated, so we need to
439// resolve their addresses at runtime, and this is the way to do it.
440extern "C" {
441 void *getPointerToNamedFunction(const char *Name) {
Chris Lattner4d326fa2003-12-20 01:46:27 +0000442 Module &M = TheJIT->getModule();
Misha Brukmand69c1e62003-07-28 19:09:06 +0000443 if (Function *F = M.getNamedFunction(Name))
Chris Lattner4d326fa2003-12-20 01:46:27 +0000444 return TheJIT->getPointerToFunction(F);
445 return TheJIT->getPointerToNamedFunction(Name);
Misha Brukmand69c1e62003-07-28 19:09:06 +0000446 }
447}