blob: bd00c964ccab76738553815ade5bd2cfe13c57f2 [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
51 unsigned char *CurStubPtr, *CurFunctionPtr;
52 public:
53 JITMemoryManager();
Reid Spencer4af3da62004-12-13 16:04:04 +000054 ~JITMemoryManager();
Chris Lattner688506d2003-08-14 18:35:27 +000055
56 inline unsigned char *allocateStub(unsigned StubSize);
57 inline unsigned char *startFunctionBody();
58 inline void endFunctionBody(unsigned char *FunctionEnd);
59 };
60}
61
Chris Lattner688506d2003-08-14 18:35:27 +000062JITMemoryManager::JITMemoryManager() {
63 // Allocate a 16M block of memory...
Reid Spencer33189e72004-09-13 22:38:11 +000064 MemBlock = sys::Memory::AllocateRWX((16 << 20));
Reid Spencer52b0ba62004-09-11 04:31:03 +000065 MemBase = reinterpret_cast<unsigned char*>(MemBlock.base());
Chris Lattner688506d2003-08-14 18:35:27 +000066 FunctionBase = MemBase + 512*1024; // Use 512k for stubs
67
68 // Allocate stubs backwards from the function base, allocate functions forward
69 // from the function base.
70 CurStubPtr = CurFunctionPtr = FunctionBase;
71}
72
Reid Spencer4af3da62004-12-13 16:04:04 +000073JITMemoryManager::~JITMemoryManager() {
74 sys::Memory::ReleaseRWX(MemBlock);
75}
76
Chris Lattner688506d2003-08-14 18:35:27 +000077unsigned char *JITMemoryManager::allocateStub(unsigned StubSize) {
78 CurStubPtr -= StubSize;
79 if (CurStubPtr < MemBase) {
80 std::cerr << "JIT ran out of memory for function stubs!\n";
81 abort();
82 }
83 return CurStubPtr;
84}
85
86unsigned char *JITMemoryManager::startFunctionBody() {
Chris Lattner5be478f2004-11-20 03:46:14 +000087 // Round up to an even multiple of 8 bytes, this should eventually be target
Chris Lattner688506d2003-08-14 18:35:27 +000088 // specific.
Chris Lattner5be478f2004-11-20 03:46:14 +000089 return (unsigned char*)(((intptr_t)CurFunctionPtr + 7) & ~7);
Chris Lattner688506d2003-08-14 18:35:27 +000090}
91
92void JITMemoryManager::endFunctionBody(unsigned char *FunctionEnd) {
93 assert(FunctionEnd > CurFunctionPtr);
94 CurFunctionPtr = FunctionEnd;
95}
96
Chris Lattner54266522004-11-20 23:57:07 +000097//===----------------------------------------------------------------------===//
98// JIT lazy compilation code.
99//
100namespace {
101 /// JITResolver - Keep track of, and resolve, call sites for functions that
102 /// have not yet been compiled.
103 class JITResolver {
Chris Lattner5e225582004-11-21 03:37:42 +0000104 /// MCE - The MachineCodeEmitter to use to emit stubs with.
Chris Lattner54266522004-11-20 23:57:07 +0000105 MachineCodeEmitter &MCE;
106
Chris Lattner5e225582004-11-21 03:37:42 +0000107 /// LazyResolverFn - The target lazy resolver function that we actually
108 /// rewrite instructions to use.
109 TargetJITInfo::LazyResolverFn LazyResolverFn;
110
Chris Lattner54266522004-11-20 23:57:07 +0000111 // FunctionToStubMap - Keep track of the stub created for a particular
112 // function so that we can reuse them if necessary.
113 std::map<Function*, void*> FunctionToStubMap;
114
115 // StubToFunctionMap - Keep track of the function that each stub corresponds
116 // to.
117 std::map<void*, Function*> StubToFunctionMap;
118
119 public:
Chris Lattner5e225582004-11-21 03:37:42 +0000120 JITResolver(MachineCodeEmitter &mce) : MCE(mce) {
121 LazyResolverFn =
122 TheJIT->getJITInfo().getLazyResolverFunction(JITCompilerFn);
123 }
Chris Lattner54266522004-11-20 23:57:07 +0000124
125 /// getFunctionStub - This returns a pointer to a function stub, creating
126 /// one on demand as needed.
127 void *getFunctionStub(Function *F);
128
Chris Lattner5e225582004-11-21 03:37:42 +0000129 /// AddCallbackAtLocation - If the target is capable of rewriting an
130 /// instruction without the use of a stub, record the location of the use so
131 /// we know which function is being used at the location.
132 void *AddCallbackAtLocation(Function *F, void *Location) {
133 /// Get the target-specific JIT resolver function.
134 StubToFunctionMap[Location] = F;
135 return (void*)LazyResolverFn;
136 }
137
Chris Lattner54266522004-11-20 23:57:07 +0000138 /// JITCompilerFn - This function is called to resolve a stub to a compiled
139 /// address. If the LLVM Function corresponding to the stub has not yet
140 /// been compiled, this function compiles it first.
141 static void *JITCompilerFn(void *Stub);
142 };
143}
144
145/// getJITResolver - This function returns the one instance of the JIT resolver.
146///
147static JITResolver &getJITResolver(MachineCodeEmitter *MCE = 0) {
148 static JITResolver TheJITResolver(*MCE);
149 return TheJITResolver;
150}
151
152/// getFunctionStub - This returns a pointer to a function stub, creating
153/// one on demand as needed.
154void *JITResolver::getFunctionStub(Function *F) {
Chris Lattner54266522004-11-20 23:57:07 +0000155 // If we already have a stub for this function, recycle it.
156 void *&Stub = FunctionToStubMap[F];
157 if (Stub) return Stub;
158
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000159 // Call the lazy resolver function unless we already KNOW it is an external
160 // function, in which case we just skip the lazy resolution step.
161 void *Actual = (void*)LazyResolverFn;
162 if (F->hasExternalLinkage())
163 Actual = TheJIT->getPointerToFunction(F);
164
Chris Lattner54266522004-11-20 23:57:07 +0000165 // Otherwise, codegen a new stub. For now, the stub will call the lazy
166 // resolver function.
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000167 Stub = TheJIT->getJITInfo().emitFunctionStub(Actual, MCE);
168
169 if (F->hasExternalLinkage()) {
170 // If we are getting the stub for an external function, we really want the
171 // address of the stub in the GlobalAddressMap for the JIT, not the address
172 // of the external function.
173 TheJIT->updateGlobalMapping(F, Stub);
174 }
Chris Lattner54266522004-11-20 23:57:07 +0000175
Chris Lattnercb479412004-11-21 03:44:32 +0000176 DEBUG(std::cerr << "JIT: Stub emitted at [" << Stub << "] for function '"
Chris Lattner6f717202004-11-22 21:48:33 +0000177 << F->getName() << "'\n");
Chris Lattnercb479412004-11-21 03:44:32 +0000178
Chris Lattner54266522004-11-20 23:57:07 +0000179 // Finally, keep track of the stub-to-Function mapping so that the
180 // JITCompilerFn knows which function to compile!
181 StubToFunctionMap[Stub] = F;
182 return Stub;
183}
184
185/// JITCompilerFn - This function is called when a lazy compilation stub has
186/// been entered. It looks up which function this stub corresponds to, compiles
187/// it if necessary, then returns the resultant function pointer.
188void *JITResolver::JITCompilerFn(void *Stub) {
189 JITResolver &JR = getJITResolver();
190
191 // The address given to us for the stub may not be exactly right, it might be
192 // a little bit after the stub. As such, use upper_bound to find it.
193 std::map<void*, Function*>::iterator I =
194 JR.StubToFunctionMap.upper_bound(Stub);
195 assert(I != JR.StubToFunctionMap.begin() && "This is not a known stub!");
196 Function *F = (--I)->second;
197
198 // The target function will rewrite the stub so that the compilation callback
199 // function is no longer called from this stub.
200 JR.StubToFunctionMap.erase(I);
201
Chris Lattnercb479412004-11-21 03:44:32 +0000202 DEBUG(std::cerr << "JIT: Lazily resolving function '" << F->getName()
Chris Lattner54266522004-11-20 23:57:07 +0000203 << "' In stub ptr = " << Stub << " actual ptr = "
204 << I->first << "\n");
205
206 void *Result = TheJIT->getPointerToFunction(F);
207
208 // We don't need to reuse this stub in the future, as F is now compiled.
209 JR.FunctionToStubMap.erase(F);
210
211 // FIXME: We could rewrite all references to this stub if we knew them.
212 return Result;
213}
Chris Lattner688506d2003-08-14 18:35:27 +0000214
215
Chris Lattnere518b712004-12-05 07:19:16 +0000216// getPointerToFunctionOrStub - If the specified function has been
217// code-gen'd, return a pointer to the function. If not, compile it, or use
218// a stub to implement lazy compilation if available.
219//
220void *JIT::getPointerToFunctionOrStub(Function *F) {
221 // If we have already code generated the function, just return the address.
222 if (void *Addr = getPointerToGlobalIfAvailable(F))
223 return Addr;
224
225 // Get a stub if the target supports it
226 return getJITResolver(MCE).getFunctionStub(F);
227}
228
229
230
Chris Lattner54266522004-11-20 23:57:07 +0000231//===----------------------------------------------------------------------===//
Chris Lattner166f2262004-11-22 22:00:25 +0000232// JITEmitter code.
Chris Lattner54266522004-11-20 23:57:07 +0000233//
Chris Lattner688506d2003-08-14 18:35:27 +0000234namespace {
Chris Lattner166f2262004-11-22 22:00:25 +0000235 /// JITEmitter - The JIT implementation of the MachineCodeEmitter, which is
236 /// used to output functions to memory for execution.
237 class JITEmitter : public MachineCodeEmitter {
Chris Lattner688506d2003-08-14 18:35:27 +0000238 JITMemoryManager MemMgr;
239
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000240 // CurBlock - The start of the current block of memory. CurByte - The
241 // current byte being emitted to.
Chris Lattner6125fdd2003-05-09 03:30:07 +0000242 unsigned char *CurBlock, *CurByte;
243
244 // When outputting a function stub in the context of some other function, we
245 // save CurBlock and CurByte here.
246 unsigned char *SavedCurBlock, *SavedCurByte;
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000247
248 // ConstantPoolAddresses - Contains the location for each entry in the
249 // constant pool.
Chris Lattner1cc08382003-01-13 01:00:12 +0000250 std::vector<void*> ConstantPoolAddresses;
Chris Lattner5be478f2004-11-20 03:46:14 +0000251
252 /// Relocations - These are the relocations that the function needs, as
253 /// emitted.
254 std::vector<MachineRelocation> Relocations;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000255 public:
Chris Lattner166f2262004-11-22 22:00:25 +0000256 JITEmitter(JIT &jit) { TheJIT = &jit; }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000257
258 virtual void startFunction(MachineFunction &F);
259 virtual void finishFunction(MachineFunction &F);
Chris Lattner1cc08382003-01-13 01:00:12 +0000260 virtual void emitConstantPool(MachineConstantPool *MCP);
Chris Lattner54266522004-11-20 23:57:07 +0000261 virtual void startFunctionStub(unsigned StubSize);
262 virtual void* finishFunctionStub(const Function *F);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000263 virtual void emitByte(unsigned char B);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000264 virtual void emitWord(unsigned W);
Brian Gaekeaea1b582004-04-23 17:11:14 +0000265 virtual void emitWordAt(unsigned W, unsigned *Ptr);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000266
Chris Lattner5be478f2004-11-20 03:46:14 +0000267 virtual void addRelocation(const MachineRelocation &MR) {
268 Relocations.push_back(MR);
269 }
270
271 virtual uint64_t getCurrentPCValue();
272 virtual uint64_t getCurrentPCOffset();
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000273 virtual uint64_t getConstantPoolEntryAddress(unsigned Entry);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000274
Chris Lattner54266522004-11-20 23:57:07 +0000275 private:
Chris Lattner5e225582004-11-21 03:37:42 +0000276 void *getPointerToGlobal(GlobalValue *GV, void *Reference, bool NoNeedStub);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000277 };
278}
279
Chris Lattner4d326fa2003-12-20 01:46:27 +0000280MachineCodeEmitter *JIT::createEmitter(JIT &jit) {
Chris Lattner166f2262004-11-22 22:00:25 +0000281 return new JITEmitter(jit);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000282}
283
Chris Lattner166f2262004-11-22 22:00:25 +0000284void *JITEmitter::getPointerToGlobal(GlobalValue *V, void *Reference,
285 bool DoesntNeedStub) {
Chris Lattner54266522004-11-20 23:57:07 +0000286 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
287 /// FIXME: If we straightened things out, this could actually emit the
288 /// global immediately instead of queuing it for codegen later!
Chris Lattner54266522004-11-20 23:57:07 +0000289 return TheJIT->getOrEmitGlobalVariable(GV);
290 }
291
292 // If we have already compiled the function, return a pointer to its body.
293 Function *F = cast<Function>(V);
294 void *ResultPtr = TheJIT->getPointerToGlobalIfAvailable(F);
295 if (ResultPtr) return ResultPtr;
296
Chris Lattner532343b2004-11-30 17:41:49 +0000297 if (F->hasExternalLinkage() && F->isExternal()) {
Chris Lattner54266522004-11-20 23:57:07 +0000298 // If this is an external function pointer, we can force the JIT to
299 // 'compile' it, which really just adds it to the map.
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000300 if (DoesntNeedStub)
301 return TheJIT->getPointerToFunction(F);
302
303 return getJITResolver(this).getFunctionStub(F);
Chris Lattner54266522004-11-20 23:57:07 +0000304 }
305
Chris Lattner5e225582004-11-21 03:37:42 +0000306 // Okay, the function has not been compiled yet, if the target callback
307 // mechanism is capable of rewriting the instruction directly, prefer to do
308 // that instead of emitting a stub.
309 if (DoesntNeedStub)
310 return getJITResolver(this).AddCallbackAtLocation(F, Reference);
311
Chris Lattner54266522004-11-20 23:57:07 +0000312 // Otherwise, we have to emit a lazy resolving stub.
313 return getJITResolver(this).getFunctionStub(F);
314}
315
Chris Lattner166f2262004-11-22 22:00:25 +0000316void JITEmitter::startFunction(MachineFunction &F) {
Chris Lattner688506d2003-08-14 18:35:27 +0000317 CurByte = CurBlock = MemMgr.startFunctionBody();
Chris Lattner4d326fa2003-12-20 01:46:27 +0000318 TheJIT->addGlobalMapping(F.getFunction(), CurBlock);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000319}
320
Chris Lattner166f2262004-11-22 22:00:25 +0000321void JITEmitter::finishFunction(MachineFunction &F) {
Chris Lattner688506d2003-08-14 18:35:27 +0000322 MemMgr.endFunctionBody(CurByte);
Chris Lattner1cc08382003-01-13 01:00:12 +0000323 ConstantPoolAddresses.clear();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000324 NumBytes += CurByte-CurBlock;
325
Chris Lattner5be478f2004-11-20 03:46:14 +0000326 if (!Relocations.empty()) {
327 // Resolve the relocations to concrete pointers.
328 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
329 MachineRelocation &MR = Relocations[i];
330 void *ResultPtr;
Chris Lattner54266522004-11-20 23:57:07 +0000331 if (MR.isString())
Chris Lattner5be478f2004-11-20 03:46:14 +0000332 ResultPtr = TheJIT->getPointerToNamedFunction(MR.getString());
Chris Lattner54266522004-11-20 23:57:07 +0000333 else
Chris Lattner5e225582004-11-21 03:37:42 +0000334 ResultPtr = getPointerToGlobal(MR.getGlobalValue(),
335 CurBlock+MR.getMachineCodeOffset(),
336 MR.doesntNeedFunctionStub());
Chris Lattner5be478f2004-11-20 03:46:14 +0000337 MR.setResultPointer(ResultPtr);
338 }
339
340 TheJIT->getJITInfo().relocate(CurBlock, &Relocations[0],
341 Relocations.size());
342 }
343
Chris Lattnercb479412004-11-21 03:44:32 +0000344 DEBUG(std::cerr << "JIT: Finished CodeGen of [" << (void*)CurBlock
Misha Brukman1d440852003-06-06 06:52:35 +0000345 << "] Function: " << F.getFunction()->getName()
Chris Lattner5be478f2004-11-20 03:46:14 +0000346 << ": " << CurByte-CurBlock << " bytes of text, "
347 << Relocations.size() << " relocations\n");
348 Relocations.clear();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000349}
350
Chris Lattner166f2262004-11-22 22:00:25 +0000351void JITEmitter::emitConstantPool(MachineConstantPool *MCP) {
Chris Lattner1cc08382003-01-13 01:00:12 +0000352 const std::vector<Constant*> &Constants = MCP->getConstants();
Chris Lattner2c0a6a12003-11-30 04:23:21 +0000353 if (Constants.empty()) return;
354
355 std::vector<unsigned> ConstantOffset;
356 ConstantOffset.reserve(Constants.size());
357
358 // Calculate how much space we will need for all the constants, and the offset
359 // each one will live in.
360 unsigned TotalSize = 0;
Chris Lattner1cc08382003-01-13 01:00:12 +0000361 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Chris Lattner2c0a6a12003-11-30 04:23:21 +0000362 const Type *Ty = Constants[i]->getType();
Chris Lattnera8101c12005-01-08 20:07:03 +0000363 unsigned Size = (unsigned)TheJIT->getTargetData().getTypeSize(Ty);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000364 unsigned Alignment = TheJIT->getTargetData().getTypeAlignment(Ty);
Chris Lattner2c0a6a12003-11-30 04:23:21 +0000365 // Make sure to take into account the alignment requirements of the type.
366 TotalSize = (TotalSize + Alignment-1) & ~(Alignment-1);
367
368 // Remember the offset this element lives at.
369 ConstantOffset.push_back(TotalSize);
370 TotalSize += Size; // Reserve space for the constant.
371 }
372
373 // Now that we know how much memory to allocate, do so.
374 char *Pool = new char[TotalSize];
375
376 // Actually output all of the constants, and remember their addresses.
377 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
378 void *Addr = Pool + ConstantOffset[i];
Chris Lattner4d326fa2003-12-20 01:46:27 +0000379 TheJIT->InitializeMemory(Constants[i], Addr);
Misha Brukman91de3522003-11-30 00:50:53 +0000380 ConstantPoolAddresses.push_back(Addr);
Chris Lattner1cc08382003-01-13 01:00:12 +0000381 }
382}
383
Chris Lattner166f2262004-11-22 22:00:25 +0000384void JITEmitter::startFunctionStub(unsigned StubSize) {
Chris Lattner6125fdd2003-05-09 03:30:07 +0000385 SavedCurBlock = CurBlock; SavedCurByte = CurByte;
Chris Lattner688506d2003-08-14 18:35:27 +0000386 CurByte = CurBlock = MemMgr.allocateStub(StubSize);
Chris Lattner6125fdd2003-05-09 03:30:07 +0000387}
388
Chris Lattner166f2262004-11-22 22:00:25 +0000389void *JITEmitter::finishFunctionStub(const Function *F) {
Chris Lattner6125fdd2003-05-09 03:30:07 +0000390 NumBytes += CurByte-CurBlock;
Chris Lattner6125fdd2003-05-09 03:30:07 +0000391 std::swap(CurBlock, SavedCurBlock);
392 CurByte = SavedCurByte;
393 return SavedCurBlock;
394}
395
Chris Lattner166f2262004-11-22 22:00:25 +0000396void JITEmitter::emitByte(unsigned char B) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000397 *CurByte++ = B; // Write the byte to memory
398}
399
Chris Lattner166f2262004-11-22 22:00:25 +0000400void JITEmitter::emitWord(unsigned W) {
Chris Lattner688506d2003-08-14 18:35:27 +0000401 // This won't work if the endianness of the host and target don't agree! (For
402 // a JIT this can't happen though. :)
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000403 *(unsigned*)CurByte = W;
404 CurByte += sizeof(unsigned);
405}
406
Chris Lattner166f2262004-11-22 22:00:25 +0000407void JITEmitter::emitWordAt(unsigned W, unsigned *Ptr) {
Brian Gaekeaea1b582004-04-23 17:11:14 +0000408 *Ptr = W;
409}
410
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000411// getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry
412// in the constant pool that was last emitted with the 'emitConstantPool'
413// method.
414//
Chris Lattner166f2262004-11-22 22:00:25 +0000415uint64_t JITEmitter::getConstantPoolEntryAddress(unsigned ConstantNum) {
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000416 assert(ConstantNum < ConstantPoolAddresses.size() &&
417 "Invalid ConstantPoolIndex!");
418 return (intptr_t)ConstantPoolAddresses[ConstantNum];
419}
420
421// getCurrentPCValue - This returns the address that the next emitted byte
422// will be output to.
423//
Chris Lattner166f2262004-11-22 22:00:25 +0000424uint64_t JITEmitter::getCurrentPCValue() {
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000425 return (intptr_t)CurByte;
426}
427
Chris Lattner166f2262004-11-22 22:00:25 +0000428uint64_t JITEmitter::getCurrentPCOffset() {
Chris Lattner5be478f2004-11-20 03:46:14 +0000429 return (intptr_t)CurByte-(intptr_t)CurBlock;
430}
431
Misha Brukmand69c1e62003-07-28 19:09:06 +0000432// getPointerToNamedFunction - This function is used as a global wrapper to
Chris Lattner4d326fa2003-12-20 01:46:27 +0000433// JIT::getPointerToNamedFunction for the purpose of resolving symbols when
Misha Brukmand69c1e62003-07-28 19:09:06 +0000434// bugpoint is debugging the JIT. In that scenario, we are loading an .so and
435// need to resolve function(s) that are being mis-codegenerated, so we need to
436// resolve their addresses at runtime, and this is the way to do it.
437extern "C" {
438 void *getPointerToNamedFunction(const char *Name) {
Chris Lattner4d326fa2003-12-20 01:46:27 +0000439 Module &M = TheJIT->getModule();
Misha Brukmand69c1e62003-07-28 19:09:06 +0000440 if (Function *F = M.getNamedFunction(Name))
Chris Lattner4d326fa2003-12-20 01:46:27 +0000441 return TheJIT->getPointerToFunction(F);
442 return TheJIT->getPointerToNamedFunction(Name);
Misha Brukmand69c1e62003-07-28 19:09:06 +0000443 }
444}