blob: d49b64f8069d773e1a793644a76d6f2f17ec4f9d [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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"
Nate Begeman37efe672006-04-22 18:53:45 +000023#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner5be478f2004-11-20 03:46:14 +000024#include "llvm/CodeGen/MachineRelocation.h"
Chris Lattner8907b4b2007-12-05 23:39:57 +000025#include "llvm/ExecutionEngine/JITMemoryManager.h"
Chris Lattner1cc08382003-01-13 01:00:12 +000026#include "llvm/Target/TargetData.h"
Chris Lattner5be478f2004-11-20 03:46:14 +000027#include "llvm/Target/TargetJITInfo.h"
Jim Laskeyacd80ac2006-12-14 19:17:33 +000028#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000029#include "llvm/Support/Debug.h"
Chris Lattnere7fd5532006-05-08 22:00:52 +000030#include "llvm/Support/MutexGuard.h"
Anton Korobeynikovfd58e6e2007-01-23 10:26:08 +000031#include "llvm/System/Disassembler.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000032#include "llvm/ADT/Statistic.h"
Andrew Lenhartha00269b2005-07-29 23:40:16 +000033#include <algorithm>
Chris Lattnerc19aade2003-12-08 08:06:28 +000034using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000035
Chris Lattner36343732006-12-19 22:43:32 +000036STATISTIC(NumBytes, "Number of bytes of machine code compiled");
37STATISTIC(NumRelos, "Number of relocations applied");
38static JIT *TheJIT = 0;
Chris Lattner54266522004-11-20 23:57:07 +000039
Andrew Lenhartha00269b2005-07-29 23:40:16 +000040
Chris Lattner54266522004-11-20 23:57:07 +000041//===----------------------------------------------------------------------===//
42// JIT lazy compilation code.
43//
44namespace {
Reid Spenceree448632005-07-12 15:51:55 +000045 class JITResolverState {
46 private:
47 /// FunctionToStubMap - Keep track of the stub created for a particular
48 /// function so that we can reuse them if necessary.
49 std::map<Function*, void*> FunctionToStubMap;
50
51 /// StubToFunctionMap - Keep track of the function that each stub
52 /// corresponds to.
53 std::map<void*, Function*> StubToFunctionMap;
Jeff Cohen00b168892005-07-27 06:12:32 +000054
Evan Chengbe8c03f2008-01-04 10:46:51 +000055 /// GlobalToLazyPtrMap - Keep track of the lazy pointer created for a
56 /// particular GlobalVariable so that we can reuse them if necessary.
57 std::map<GlobalValue*, void*> GlobalToLazyPtrMap;
58
Reid Spenceree448632005-07-12 15:51:55 +000059 public:
60 std::map<Function*, void*>& getFunctionToStubMap(const MutexGuard& locked) {
61 assert(locked.holds(TheJIT->lock));
62 return FunctionToStubMap;
63 }
Jeff Cohen00b168892005-07-27 06:12:32 +000064
Reid Spenceree448632005-07-12 15:51:55 +000065 std::map<void*, Function*>& getStubToFunctionMap(const MutexGuard& locked) {
66 assert(locked.holds(TheJIT->lock));
67 return StubToFunctionMap;
68 }
Evan Chengbe8c03f2008-01-04 10:46:51 +000069
70 std::map<GlobalValue*, void*>&
71 getGlobalToLazyPtrMap(const MutexGuard& locked) {
72 assert(locked.holds(TheJIT->lock));
73 return GlobalToLazyPtrMap;
74 }
Reid Spenceree448632005-07-12 15:51:55 +000075 };
Jeff Cohen00b168892005-07-27 06:12:32 +000076
Chris Lattner54266522004-11-20 23:57:07 +000077 /// JITResolver - Keep track of, and resolve, call sites for functions that
78 /// have not yet been compiled.
79 class JITResolver {
Chris Lattner5e225582004-11-21 03:37:42 +000080 /// LazyResolverFn - The target lazy resolver function that we actually
81 /// rewrite instructions to use.
82 TargetJITInfo::LazyResolverFn LazyResolverFn;
83
Reid Spenceree448632005-07-12 15:51:55 +000084 JITResolverState state;
Chris Lattner54266522004-11-20 23:57:07 +000085
Chris Lattnerd91ff7c2005-04-18 01:44:27 +000086 /// ExternalFnToStubMap - This is the equivalent of FunctionToStubMap for
87 /// external functions.
88 std::map<void*, void*> ExternalFnToStubMap;
Andrew Lenharth6a974612005-07-28 12:44:13 +000089
90 //map addresses to indexes in the GOT
91 std::map<void*, unsigned> revGOTMap;
92 unsigned nextGOTIndex;
93
Chris Lattnere7484012007-02-24 02:57:03 +000094 static JITResolver *TheJITResolver;
Chris Lattner54266522004-11-20 23:57:07 +000095 public:
Chris Lattnere7484012007-02-24 02:57:03 +000096 JITResolver(JIT &jit) : nextGOTIndex(0) {
97 TheJIT = &jit;
98
99 LazyResolverFn = jit.getJITInfo().getLazyResolverFunction(JITCompilerFn);
100 assert(TheJITResolver == 0 && "Multiple JIT resolvers?");
101 TheJITResolver = this;
102 }
103
104 ~JITResolver() {
105 TheJITResolver = 0;
Chris Lattner5e225582004-11-21 03:37:42 +0000106 }
Chris Lattner54266522004-11-20 23:57:07 +0000107
108 /// getFunctionStub - This returns a pointer to a function stub, creating
109 /// one on demand as needed.
110 void *getFunctionStub(Function *F);
111
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000112 /// getExternalFunctionStub - Return a stub for the function at the
113 /// specified address, created lazily on demand.
114 void *getExternalFunctionStub(void *FnAddr);
115
Evan Chengbe8c03f2008-01-04 10:46:51 +0000116 /// getGlobalValueLazyPtr - Return a lazy pointer containing the specified
117 /// GV address.
118 void *getGlobalValueLazyPtr(GlobalValue *V, void *GVAddress);
119
Chris Lattner5e225582004-11-21 03:37:42 +0000120 /// AddCallbackAtLocation - If the target is capable of rewriting an
121 /// instruction without the use of a stub, record the location of the use so
122 /// we know which function is being used at the location.
123 void *AddCallbackAtLocation(Function *F, void *Location) {
Reid Spenceree448632005-07-12 15:51:55 +0000124 MutexGuard locked(TheJIT->lock);
Chris Lattner5e225582004-11-21 03:37:42 +0000125 /// Get the target-specific JIT resolver function.
Reid Spenceree448632005-07-12 15:51:55 +0000126 state.getStubToFunctionMap(locked)[Location] = F;
Chris Lattner870286a2006-06-01 17:29:22 +0000127 return (void*)(intptr_t)LazyResolverFn;
Chris Lattner5e225582004-11-21 03:37:42 +0000128 }
129
Andrew Lenharth6a974612005-07-28 12:44:13 +0000130 /// getGOTIndexForAddress - Return a new or existing index in the GOT for
Chris Lattner8907b4b2007-12-05 23:39:57 +0000131 /// an address. This function only manages slots, it does not manage the
Andrew Lenharth6a974612005-07-28 12:44:13 +0000132 /// contents of the slots or the memory associated with the GOT.
Chris Lattner8907b4b2007-12-05 23:39:57 +0000133 unsigned getGOTIndexForAddr(void *addr);
Andrew Lenharth6a974612005-07-28 12:44:13 +0000134
Chris Lattner54266522004-11-20 23:57:07 +0000135 /// JITCompilerFn - This function is called to resolve a stub to a compiled
136 /// address. If the LLVM Function corresponding to the stub has not yet
137 /// been compiled, this function compiles it first.
138 static void *JITCompilerFn(void *Stub);
139 };
140}
141
Chris Lattnere7484012007-02-24 02:57:03 +0000142JITResolver *JITResolver::TheJITResolver = 0;
Chris Lattner54266522004-11-20 23:57:07 +0000143
Evan Cheng55b50532006-07-27 06:33:55 +0000144#if (defined(__POWERPC__) || defined (__ppc__) || defined(_POWER)) && \
145 defined(__APPLE__)
146extern "C" void sys_icache_invalidate(const void *Addr, size_t len);
147#endif
148
149/// synchronizeICache - On some targets, the JIT emitted code must be
150/// explicitly refetched to ensure correct execution.
151static void synchronizeICache(const void *Addr, size_t len) {
152#if (defined(__POWERPC__) || defined (__ppc__) || defined(_POWER)) && \
153 defined(__APPLE__)
Jim Laskey2e9f3682006-07-27 13:40:34 +0000154 sys_icache_invalidate(Addr, len);
Evan Cheng55b50532006-07-27 06:33:55 +0000155#endif
156}
157
Chris Lattner54266522004-11-20 23:57:07 +0000158/// getFunctionStub - This returns a pointer to a function stub, creating
159/// one on demand as needed.
160void *JITResolver::getFunctionStub(Function *F) {
Reid Spenceree448632005-07-12 15:51:55 +0000161 MutexGuard locked(TheJIT->lock);
162
Chris Lattner54266522004-11-20 23:57:07 +0000163 // If we already have a stub for this function, recycle it.
Reid Spenceree448632005-07-12 15:51:55 +0000164 void *&Stub = state.getFunctionToStubMap(locked)[F];
Chris Lattner54266522004-11-20 23:57:07 +0000165 if (Stub) return Stub;
166
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000167 // Call the lazy resolver function unless we already KNOW it is an external
168 // function, in which case we just skip the lazy resolution step.
Chris Lattner870286a2006-06-01 17:29:22 +0000169 void *Actual = (void*)(intptr_t)LazyResolverFn;
Gabor Greifa99be512007-07-05 17:07:56 +0000170 if (F->isDeclaration() && !F->hasNotBeenReadFromBitcode())
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000171 Actual = TheJIT->getPointerToFunction(F);
Misha Brukmanf976c852005-04-21 22:55:34 +0000172
Chris Lattner54266522004-11-20 23:57:07 +0000173 // Otherwise, codegen a new stub. For now, the stub will call the lazy
174 // resolver function.
Chris Lattnere7484012007-02-24 02:57:03 +0000175 Stub = TheJIT->getJITInfo().emitFunctionStub(Actual,
176 *TheJIT->getCodeEmitter());
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000177
Chris Lattner870286a2006-06-01 17:29:22 +0000178 if (Actual != (void*)(intptr_t)LazyResolverFn) {
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000179 // If we are getting the stub for an external function, we really want the
180 // address of the stub in the GlobalAddressMap for the JIT, not the address
181 // of the external function.
182 TheJIT->updateGlobalMapping(F, Stub);
183 }
Chris Lattner54266522004-11-20 23:57:07 +0000184
Bill Wendling832171c2006-12-07 20:04:42 +0000185 DOUT << "JIT: Stub emitted at [" << Stub << "] for function '"
186 << F->getName() << "'\n";
Chris Lattnercb479412004-11-21 03:44:32 +0000187
Chris Lattner54266522004-11-20 23:57:07 +0000188 // Finally, keep track of the stub-to-Function mapping so that the
189 // JITCompilerFn knows which function to compile!
Reid Spenceree448632005-07-12 15:51:55 +0000190 state.getStubToFunctionMap(locked)[Stub] = F;
Chris Lattner54266522004-11-20 23:57:07 +0000191 return Stub;
192}
193
Evan Chengbe8c03f2008-01-04 10:46:51 +0000194/// getGlobalValueLazyPtr - Return a lazy pointer containing the specified
195/// GV address.
196void *JITResolver::getGlobalValueLazyPtr(GlobalValue *GV, void *GVAddress) {
197 MutexGuard locked(TheJIT->lock);
198
199 // If we already have a stub for this global variable, recycle it.
200 void *&LazyPtr = state.getGlobalToLazyPtrMap(locked)[GV];
201 if (LazyPtr) return LazyPtr;
202
203 // Otherwise, codegen a new lazy pointer.
204 LazyPtr = TheJIT->getJITInfo().emitGlobalValueLazyPtr(GVAddress,
205 *TheJIT->getCodeEmitter());
206
207 DOUT << "JIT: Stub emitted at [" << LazyPtr << "] for GV '"
208 << GV->getName() << "'\n";
209
210 return LazyPtr;
211}
212
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000213/// getExternalFunctionStub - Return a stub for the function at the
214/// specified address, created lazily on demand.
215void *JITResolver::getExternalFunctionStub(void *FnAddr) {
216 // If we already have a stub for this function, recycle it.
217 void *&Stub = ExternalFnToStubMap[FnAddr];
218 if (Stub) return Stub;
219
Chris Lattnere7484012007-02-24 02:57:03 +0000220 Stub = TheJIT->getJITInfo().emitFunctionStub(FnAddr,
221 *TheJIT->getCodeEmitter());
Evan Cheng55fc2802006-07-25 20:40:54 +0000222
Bill Wendling832171c2006-12-07 20:04:42 +0000223 DOUT << "JIT: Stub emitted at [" << Stub
224 << "] for external function at '" << FnAddr << "'\n";
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000225 return Stub;
226}
227
Andrew Lenharth6a974612005-07-28 12:44:13 +0000228unsigned JITResolver::getGOTIndexForAddr(void* addr) {
229 unsigned idx = revGOTMap[addr];
230 if (!idx) {
231 idx = ++nextGOTIndex;
232 revGOTMap[addr] = idx;
Bill Wendling832171c2006-12-07 20:04:42 +0000233 DOUT << "Adding GOT entry " << idx
234 << " for addr " << addr << "\n";
Andrew Lenharth6a974612005-07-28 12:44:13 +0000235 }
236 return idx;
237}
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000238
Chris Lattner54266522004-11-20 23:57:07 +0000239/// JITCompilerFn - This function is called when a lazy compilation stub has
240/// been entered. It looks up which function this stub corresponds to, compiles
241/// it if necessary, then returns the resultant function pointer.
242void *JITResolver::JITCompilerFn(void *Stub) {
Chris Lattnere7484012007-02-24 02:57:03 +0000243 JITResolver &JR = *TheJITResolver;
Misha Brukmanf976c852005-04-21 22:55:34 +0000244
Reid Spenceree448632005-07-12 15:51:55 +0000245 MutexGuard locked(TheJIT->lock);
246
Chris Lattner54266522004-11-20 23:57:07 +0000247 // The address given to us for the stub may not be exactly right, it might be
248 // a little bit after the stub. As such, use upper_bound to find it.
249 std::map<void*, Function*>::iterator I =
Reid Spenceree448632005-07-12 15:51:55 +0000250 JR.state.getStubToFunctionMap(locked).upper_bound(Stub);
Chris Lattner21998772006-01-07 06:20:51 +0000251 assert(I != JR.state.getStubToFunctionMap(locked).begin() &&
252 "This is not a known stub!");
Chris Lattner54266522004-11-20 23:57:07 +0000253 Function *F = (--I)->second;
254
Evan Cheng9da60f92007-06-30 00:10:37 +0000255 // If we have already code generated the function, just return the address.
256 void *Result = TheJIT->getPointerToGlobalIfAvailable(F);
Chris Lattner9cab56d2006-11-09 19:32:13 +0000257
Evan Cheng9da60f92007-06-30 00:10:37 +0000258 if (!Result) {
259 // Otherwise we don't have it, do lazy compilation now.
260
261 // If lazy compilation is disabled, emit a useful error message and abort.
262 if (TheJIT->isLazyCompilationDisabled()) {
263 cerr << "LLVM JIT requested to do lazy compilation of function '"
264 << F->getName() << "' when lazy compiles are disabled!\n";
265 abort();
266 }
267
268 // We might like to remove the stub from the StubToFunction map.
269 // We can't do that! Multiple threads could be stuck, waiting to acquire the
270 // lock above. As soon as the 1st function finishes compiling the function,
271 // the next one will be released, and needs to be able to find the function
272 // it needs to call.
273 //JR.state.getStubToFunctionMap(locked).erase(I);
Chris Lattner54266522004-11-20 23:57:07 +0000274
Evan Cheng9da60f92007-06-30 00:10:37 +0000275 DOUT << "JIT: Lazily resolving function '" << F->getName()
276 << "' In stub ptr = " << Stub << " actual ptr = "
277 << I->first << "\n";
Chris Lattner54266522004-11-20 23:57:07 +0000278
Evan Cheng9da60f92007-06-30 00:10:37 +0000279 Result = TheJIT->getPointerToFunction(F);
280 }
Chris Lattner54266522004-11-20 23:57:07 +0000281
282 // We don't need to reuse this stub in the future, as F is now compiled.
Reid Spenceree448632005-07-12 15:51:55 +0000283 JR.state.getFunctionToStubMap(locked).erase(F);
Chris Lattner54266522004-11-20 23:57:07 +0000284
285 // FIXME: We could rewrite all references to this stub if we knew them.
Andrew Lenharth6a974612005-07-28 12:44:13 +0000286
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000287 // What we will do is set the compiled function address to map to the
288 // same GOT entry as the stub so that later clients may update the GOT
Andrew Lenharth6a974612005-07-28 12:44:13 +0000289 // if they see it still using the stub address.
290 // Note: this is done so the Resolver doesn't have to manage GOT memory
291 // Do this without allocating map space if the target isn't using a GOT
292 if(JR.revGOTMap.find(Stub) != JR.revGOTMap.end())
293 JR.revGOTMap[Result] = JR.revGOTMap[Stub];
294
Chris Lattner54266522004-11-20 23:57:07 +0000295 return Result;
296}
Chris Lattner688506d2003-08-14 18:35:27 +0000297
298
Chris Lattner54266522004-11-20 23:57:07 +0000299//===----------------------------------------------------------------------===//
Chris Lattner166f2262004-11-22 22:00:25 +0000300// JITEmitter code.
Chris Lattner54266522004-11-20 23:57:07 +0000301//
Chris Lattner688506d2003-08-14 18:35:27 +0000302namespace {
Chris Lattner166f2262004-11-22 22:00:25 +0000303 /// JITEmitter - The JIT implementation of the MachineCodeEmitter, which is
304 /// used to output functions to memory for execution.
305 class JITEmitter : public MachineCodeEmitter {
Chris Lattner8907b4b2007-12-05 23:39:57 +0000306 JITMemoryManager *MemMgr;
Chris Lattner688506d2003-08-14 18:35:27 +0000307
Chris Lattner6125fdd2003-05-09 03:30:07 +0000308 // When outputting a function stub in the context of some other function, we
Chris Lattner43b429b2006-05-02 18:27:26 +0000309 // save BufferBegin/BufferEnd/CurBufferPtr here.
310 unsigned char *SavedBufferBegin, *SavedBufferEnd, *SavedCurBufferPtr;
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000311
Chris Lattner5be478f2004-11-20 03:46:14 +0000312 /// Relocations - These are the relocations that the function needs, as
313 /// emitted.
314 std::vector<MachineRelocation> Relocations;
Chris Lattnerb4432f32006-05-03 17:10:41 +0000315
316 /// MBBLocations - This vector is a mapping from MBB ID's to their address.
317 /// It is filled in by the StartMachineBasicBlock callback and queried by
318 /// the getMachineBasicBlockAddress callback.
319 std::vector<intptr_t> MBBLocations;
Andrew Lenharth16ec33c2005-07-22 20:48:12 +0000320
Chris Lattner239862c2006-02-09 04:49:59 +0000321 /// ConstantPool - The constant pool for the current function.
322 ///
323 MachineConstantPool *ConstantPool;
324
325 /// ConstantPoolBase - A pointer to the first entry in the constant pool.
326 ///
327 void *ConstantPoolBase;
Nate Begeman37efe672006-04-22 18:53:45 +0000328
Nate Begeman019f8512006-09-10 23:03:44 +0000329 /// JumpTable - The jump tables for the current function.
Nate Begeman37efe672006-04-22 18:53:45 +0000330 ///
331 MachineJumpTableInfo *JumpTable;
332
333 /// JumpTableBase - A pointer to the first entry in the jump table.
334 ///
335 void *JumpTableBase;
Evan Cheng2a3e08b2008-01-05 02:26:58 +0000336
Chris Lattnere7484012007-02-24 02:57:03 +0000337 /// Resolver - This contains info about the currently resolved functions.
338 JITResolver Resolver;
339 public:
Chris Lattner9f2f1422007-12-06 01:08:09 +0000340 JITEmitter(JIT &jit, JITMemoryManager *JMM) : Resolver(jit) {
341 MemMgr = JMM ? JMM : JITMemoryManager::CreateDefaultMemManager();
Chris Lattner8907b4b2007-12-05 23:39:57 +0000342 if (jit.getJITInfo().needsGOT()) {
343 MemMgr->AllocateGOT();
344 DOUT << "JIT is managing a GOT\n";
345 }
346 }
347 ~JITEmitter() {
348 delete MemMgr;
Andrew Lenharth16ec33c2005-07-22 20:48:12 +0000349 }
Chris Lattnere7484012007-02-24 02:57:03 +0000350
351 JITResolver &getJITResolver() { return Resolver; }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000352
353 virtual void startFunction(MachineFunction &F);
Chris Lattner43b429b2006-05-02 18:27:26 +0000354 virtual bool finishFunction(MachineFunction &F);
Chris Lattnerf75f9be2006-05-02 23:22:24 +0000355
356 void emitConstantPool(MachineConstantPool *MCP);
357 void initJumpTableInfo(MachineJumpTableInfo *MJTI);
Jim Laskeyb92767a2006-12-14 22:53:42 +0000358 void emitJumpTableInfo(MachineJumpTableInfo *MJTI);
Chris Lattnerf75f9be2006-05-02 23:22:24 +0000359
Evan Cheng9a1e9b92006-11-16 20:04:54 +0000360 virtual void startFunctionStub(unsigned StubSize, unsigned Alignment = 1);
Chris Lattner54266522004-11-20 23:57:07 +0000361 virtual void* finishFunctionStub(const Function *F);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000362
Chris Lattner5be478f2004-11-20 03:46:14 +0000363 virtual void addRelocation(const MachineRelocation &MR) {
364 Relocations.push_back(MR);
365 }
Chris Lattnerb4432f32006-05-03 17:10:41 +0000366
367 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) {
368 if (MBBLocations.size() <= (unsigned)MBB->getNumber())
369 MBBLocations.resize((MBB->getNumber()+1)*2);
370 MBBLocations[MBB->getNumber()] = getCurrentPCValue();
371 }
Chris Lattner5be478f2004-11-20 03:46:14 +0000372
Chris Lattnerb4432f32006-05-03 17:10:41 +0000373 virtual intptr_t getConstantPoolEntryAddress(unsigned Entry) const;
374 virtual intptr_t getJumpTableEntryAddress(unsigned Entry) const;
Evan Cheng2a3e08b2008-01-05 02:26:58 +0000375
Chris Lattnerb4432f32006-05-03 17:10:41 +0000376 virtual intptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
377 assert(MBBLocations.size() > (unsigned)MBB->getNumber() &&
378 MBBLocations[MBB->getNumber()] && "MBB not emitted!");
379 return MBBLocations[MBB->getNumber()];
380 }
381
Chris Lattnere993cc22006-05-11 23:08:08 +0000382 /// deallocateMemForFunction - Deallocate all memory for the specified
383 /// function body.
384 void deallocateMemForFunction(Function *F) {
Chris Lattner8907b4b2007-12-05 23:39:57 +0000385 MemMgr->deallocateMemForFunction(F);
Chris Lattnere993cc22006-05-11 23:08:08 +0000386 }
Chris Lattner54266522004-11-20 23:57:07 +0000387 private:
Chris Lattner5e225582004-11-21 03:37:42 +0000388 void *getPointerToGlobal(GlobalValue *GV, void *Reference, bool NoNeedStub);
Evan Chengbe8c03f2008-01-04 10:46:51 +0000389 void *getPointerToGVLazyPtr(GlobalValue *V, void *Reference,
390 bool NoNeedStub);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000391 };
392}
393
Chris Lattner166f2262004-11-22 22:00:25 +0000394void *JITEmitter::getPointerToGlobal(GlobalValue *V, void *Reference,
395 bool DoesntNeedStub) {
Chris Lattner54266522004-11-20 23:57:07 +0000396 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
397 /// FIXME: If we straightened things out, this could actually emit the
398 /// global immediately instead of queuing it for codegen later!
Chris Lattner54266522004-11-20 23:57:07 +0000399 return TheJIT->getOrEmitGlobalVariable(GV);
400 }
401
402 // If we have already compiled the function, return a pointer to its body.
403 Function *F = cast<Function>(V);
404 void *ResultPtr = TheJIT->getPointerToGlobalIfAvailable(F);
405 if (ResultPtr) return ResultPtr;
406
Gabor Greifa99be512007-07-05 17:07:56 +0000407 if (F->isDeclaration() && !F->hasNotBeenReadFromBitcode()) {
Chris Lattner54266522004-11-20 23:57:07 +0000408 // If this is an external function pointer, we can force the JIT to
409 // 'compile' it, which really just adds it to the map.
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000410 if (DoesntNeedStub)
411 return TheJIT->getPointerToFunction(F);
412
Chris Lattnere7484012007-02-24 02:57:03 +0000413 return Resolver.getFunctionStub(F);
Chris Lattner54266522004-11-20 23:57:07 +0000414 }
415
Chris Lattner5e225582004-11-21 03:37:42 +0000416 // Okay, the function has not been compiled yet, if the target callback
417 // mechanism is capable of rewriting the instruction directly, prefer to do
418 // that instead of emitting a stub.
419 if (DoesntNeedStub)
Chris Lattnere7484012007-02-24 02:57:03 +0000420 return Resolver.AddCallbackAtLocation(F, Reference);
Chris Lattner5e225582004-11-21 03:37:42 +0000421
Chris Lattner54266522004-11-20 23:57:07 +0000422 // Otherwise, we have to emit a lazy resolving stub.
Chris Lattnere7484012007-02-24 02:57:03 +0000423 return Resolver.getFunctionStub(F);
Chris Lattner54266522004-11-20 23:57:07 +0000424}
425
Evan Chengbe8c03f2008-01-04 10:46:51 +0000426void *JITEmitter::getPointerToGVLazyPtr(GlobalValue *V, void *Reference,
427 bool DoesntNeedStub) {
428 // Make sure GV is emitted first.
429 // FIXME: For now, if the GV is an external function we force the JIT to
430 // compile it so the lazy pointer will contain the fully resolved address.
431 void *GVAddress = getPointerToGlobal(V, Reference, true);
432 return Resolver.getGlobalValueLazyPtr(V, GVAddress);
433}
434
435
Chris Lattner166f2262004-11-22 22:00:25 +0000436void JITEmitter::startFunction(MachineFunction &F) {
Chris Lattnere993cc22006-05-11 23:08:08 +0000437 uintptr_t ActualSize;
Chris Lattner8907b4b2007-12-05 23:39:57 +0000438 BufferBegin = CurBufferPtr = MemMgr->startFunctionBody(F.getFunction(),
439 ActualSize);
Chris Lattnere993cc22006-05-11 23:08:08 +0000440 BufferEnd = BufferBegin+ActualSize;
Chris Lattnerf75f9be2006-05-02 23:22:24 +0000441
Evan Cheng9a1e9b92006-11-16 20:04:54 +0000442 // Ensure the constant pool/jump table info is at least 4-byte aligned.
443 emitAlignment(16);
444
Chris Lattnerf75f9be2006-05-02 23:22:24 +0000445 emitConstantPool(F.getConstantPool());
446 initJumpTableInfo(F.getJumpTableInfo());
447
448 // About to start emitting the machine code for the function.
Chris Lattner0eb4d6b2006-05-03 01:03:20 +0000449 emitAlignment(std::max(F.getFunction()->getAlignment(), 8U));
Chris Lattnerf75f9be2006-05-02 23:22:24 +0000450 TheJIT->updateGlobalMapping(F.getFunction(), CurBufferPtr);
Evan Cheng55fc2802006-07-25 20:40:54 +0000451
Chris Lattnerb4432f32006-05-03 17:10:41 +0000452 MBBLocations.clear();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000453}
454
Chris Lattner43b429b2006-05-02 18:27:26 +0000455bool JITEmitter::finishFunction(MachineFunction &F) {
Chris Lattnere993cc22006-05-11 23:08:08 +0000456 if (CurBufferPtr == BufferEnd) {
457 // FIXME: Allocate more space, then try again.
Bill Wendling832171c2006-12-07 20:04:42 +0000458 cerr << "JIT: Ran out of space for generated machine code!\n";
Chris Lattnere993cc22006-05-11 23:08:08 +0000459 abort();
460 }
461
Jim Laskeyb92767a2006-12-14 22:53:42 +0000462 emitJumpTableInfo(F.getJumpTableInfo());
Chris Lattnerb4432f32006-05-03 17:10:41 +0000463
Chris Lattnera8279532006-06-16 18:09:26 +0000464 // FnStart is the start of the text, not the start of the constant pool and
465 // other per-function data.
466 unsigned char *FnStart =
467 (unsigned char *)TheJIT->getPointerToGlobalIfAvailable(F.getFunction());
468 unsigned char *FnEnd = CurBufferPtr;
469
Chris Lattner8907b4b2007-12-05 23:39:57 +0000470 MemMgr->endFunctionBody(F.getFunction(), BufferBegin, FnEnd);
Chris Lattnera8279532006-06-16 18:09:26 +0000471 NumBytes += FnEnd-FnStart;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000472
Chris Lattner5be478f2004-11-20 03:46:14 +0000473 if (!Relocations.empty()) {
Chris Lattnere884dc22005-07-20 16:29:20 +0000474 NumRelos += Relocations.size();
475
Chris Lattner5be478f2004-11-20 03:46:14 +0000476 // Resolve the relocations to concrete pointers.
477 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
478 MachineRelocation &MR = Relocations[i];
479 void *ResultPtr;
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000480 if (MR.isString()) {
Chris Lattner5be478f2004-11-20 03:46:14 +0000481 ResultPtr = TheJIT->getPointerToNamedFunction(MR.getString());
Misha Brukmanf976c852005-04-21 22:55:34 +0000482
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000483 // If the target REALLY wants a stub for this function, emit it now.
Evan Cheng02aabbf2008-01-03 02:56:28 +0000484 if (!MR.doesntNeedStub())
Chris Lattnere7484012007-02-24 02:57:03 +0000485 ResultPtr = Resolver.getExternalFunctionStub(ResultPtr);
Chris Lattnerd2d5c762006-05-03 18:55:56 +0000486 } else if (MR.isGlobalValue()) {
Chris Lattner5e225582004-11-21 03:37:42 +0000487 ResultPtr = getPointerToGlobal(MR.getGlobalValue(),
Chris Lattner43b429b2006-05-02 18:27:26 +0000488 BufferBegin+MR.getMachineCodeOffset(),
Evan Cheng02aabbf2008-01-03 02:56:28 +0000489 MR.doesntNeedStub());
Evan Chengbe8c03f2008-01-04 10:46:51 +0000490 } else if (MR.isGlobalValueLazyPtr()) {
491 ResultPtr = getPointerToGVLazyPtr(MR.getGlobalValue(),
492 BufferBegin+MR.getMachineCodeOffset(),
493 MR.doesntNeedStub());
Evan Chengf141cc42006-07-27 18:21:10 +0000494 } else if (MR.isBasicBlock()) {
495 ResultPtr = (void*)getMachineBasicBlockAddress(MR.getBasicBlock());
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000496 } else if (MR.isConstantPoolIndex()) {
Chris Lattnerd2d5c762006-05-03 18:55:56 +0000497 ResultPtr=(void*)getConstantPoolEntryAddress(MR.getConstantPoolIndex());
Evan Cheng52b510b2006-06-23 01:02:37 +0000498 } else {
499 assert(MR.isJumpTableIndex());
500 ResultPtr=(void*)getJumpTableEntryAddress(MR.getJumpTableIndex());
Chris Lattnerd2d5c762006-05-03 18:55:56 +0000501 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000502
Chris Lattner5be478f2004-11-20 03:46:14 +0000503 MR.setResultPointer(ResultPtr);
Andrew Lenharth16ec33c2005-07-22 20:48:12 +0000504
Andrew Lenharth6a974612005-07-28 12:44:13 +0000505 // if we are managing the GOT and the relocation wants an index,
506 // give it one
Chris Lattner8907b4b2007-12-05 23:39:57 +0000507 if (MR.isGOTRelative() && MemMgr->isManagingGOT()) {
Chris Lattnere7484012007-02-24 02:57:03 +0000508 unsigned idx = Resolver.getGOTIndexForAddr(ResultPtr);
Andrew Lenharth6a974612005-07-28 12:44:13 +0000509 MR.setGOTIndex(idx);
Chris Lattner8907b4b2007-12-05 23:39:57 +0000510 if (((void**)MemMgr->getGOTBase())[idx] != ResultPtr) {
Bill Wendling832171c2006-12-07 20:04:42 +0000511 DOUT << "GOT was out of date for " << ResultPtr
Chris Lattner8907b4b2007-12-05 23:39:57 +0000512 << " pointing at " << ((void**)MemMgr->getGOTBase())[idx]
Bill Wendling832171c2006-12-07 20:04:42 +0000513 << "\n";
Chris Lattner8907b4b2007-12-05 23:39:57 +0000514 ((void**)MemMgr->getGOTBase())[idx] = ResultPtr;
Andrew Lenharth6a974612005-07-28 12:44:13 +0000515 }
Andrew Lenharth16ec33c2005-07-22 20:48:12 +0000516 }
Chris Lattner5be478f2004-11-20 03:46:14 +0000517 }
518
Chris Lattner43b429b2006-05-02 18:27:26 +0000519 TheJIT->getJITInfo().relocate(BufferBegin, &Relocations[0],
Chris Lattner8907b4b2007-12-05 23:39:57 +0000520 Relocations.size(), MemMgr->getGOTBase());
Chris Lattner5be478f2004-11-20 03:46:14 +0000521 }
522
Chris Lattnerd2d5c762006-05-03 18:55:56 +0000523 // Update the GOT entry for F to point to the new code.
Chris Lattner8907b4b2007-12-05 23:39:57 +0000524 if (MemMgr->isManagingGOT()) {
Chris Lattnere7484012007-02-24 02:57:03 +0000525 unsigned idx = Resolver.getGOTIndexForAddr((void*)BufferBegin);
Chris Lattner8907b4b2007-12-05 23:39:57 +0000526 if (((void**)MemMgr->getGOTBase())[idx] != (void*)BufferBegin) {
Bill Wendling832171c2006-12-07 20:04:42 +0000527 DOUT << "GOT was out of date for " << (void*)BufferBegin
Chris Lattner8907b4b2007-12-05 23:39:57 +0000528 << " pointing at " << ((void**)MemMgr->getGOTBase())[idx] << "\n";
529 ((void**)MemMgr->getGOTBase())[idx] = (void*)BufferBegin;
Andrew Lenharth6a974612005-07-28 12:44:13 +0000530 }
531 }
532
Evan Cheng55fc2802006-07-25 20:40:54 +0000533 // Invalidate the icache if necessary.
Evan Cheng55b50532006-07-27 06:33:55 +0000534 synchronizeICache(FnStart, FnEnd-FnStart);
Evan Cheng55fc2802006-07-25 20:40:54 +0000535
Bill Wendling832171c2006-12-07 20:04:42 +0000536 DOUT << "JIT: Finished CodeGen of [" << (void*)FnStart
537 << "] Function: " << F.getFunction()->getName()
538 << ": " << (FnEnd-FnStart) << " bytes of text, "
539 << Relocations.size() << " relocations\n";
Chris Lattner5be478f2004-11-20 03:46:14 +0000540 Relocations.clear();
Anton Korobeynikov8cd4c3e2007-01-19 17:25:17 +0000541
Chris Lattnerc5633c22007-01-20 20:51:43 +0000542#ifndef NDEBUG
Anton Korobeynikovc6551ff2007-03-06 05:32:48 +0000543 if (sys::hasDisassembler())
544 DOUT << "Disassembled code:\n"
545 << sys::disassembleBuffer(FnStart, FnEnd-FnStart, (uintptr_t)FnStart);
Chris Lattnerc5633c22007-01-20 20:51:43 +0000546#endif
Anton Korobeynikov8cd4c3e2007-01-19 17:25:17 +0000547
Chris Lattner43b429b2006-05-02 18:27:26 +0000548 return false;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000549}
550
Chris Lattner166f2262004-11-22 22:00:25 +0000551void JITEmitter::emitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000552 const std::vector<MachineConstantPoolEntry> &Constants = MCP->getConstants();
Chris Lattner2c0a6a12003-11-30 04:23:21 +0000553 if (Constants.empty()) return;
554
Evan Chengcd5731d2006-09-12 20:59:59 +0000555 MachineConstantPoolEntry CPE = Constants.back();
556 unsigned Size = CPE.Offset;
557 const Type *Ty = CPE.isMachineConstantPoolEntry()
Chris Lattner8a650092006-09-13 16:21:10 +0000558 ? CPE.Val.MachineCPVal->getType() : CPE.Val.ConstVal->getType();
Duncan Sands514ab342007-11-01 20:53:16 +0000559 Size += TheJIT->getTargetData()->getABITypeSize(Ty);
Chris Lattner2c0a6a12003-11-30 04:23:21 +0000560
Chris Lattnerf75f9be2006-05-02 23:22:24 +0000561 ConstantPoolBase = allocateSpace(Size, 1 << MCP->getConstantPoolAlignment());
Chris Lattner239862c2006-02-09 04:49:59 +0000562 ConstantPool = MCP;
Chris Lattnerf75f9be2006-05-02 23:22:24 +0000563
564 if (ConstantPoolBase == 0) return; // Buffer overflow.
565
Chris Lattner239862c2006-02-09 04:49:59 +0000566 // Initialize the memory for all of the constant pool entries.
Chris Lattner3029f922006-02-09 04:46:04 +0000567 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Chris Lattner239862c2006-02-09 04:49:59 +0000568 void *CAddr = (char*)ConstantPoolBase+Constants[i].Offset;
Evan Chengcd5731d2006-09-12 20:59:59 +0000569 if (Constants[i].isMachineConstantPoolEntry()) {
570 // FIXME: add support to lower machine constant pool values into bytes!
Bill Wendling832171c2006-12-07 20:04:42 +0000571 cerr << "Initialize memory with machine specific constant pool entry"
572 << " has not been implemented!\n";
Evan Chengcd5731d2006-09-12 20:59:59 +0000573 abort();
574 }
575 TheJIT->InitializeMemory(Constants[i].Val.ConstVal, CAddr);
Chris Lattner1cc08382003-01-13 01:00:12 +0000576 }
577}
578
Nate Begeman37efe672006-04-22 18:53:45 +0000579void JITEmitter::initJumpTableInfo(MachineJumpTableInfo *MJTI) {
580 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
581 if (JT.empty()) return;
582
Chris Lattnerf75f9be2006-05-02 23:22:24 +0000583 unsigned NumEntries = 0;
Nate Begeman37efe672006-04-22 18:53:45 +0000584 for (unsigned i = 0, e = JT.size(); i != e; ++i)
Chris Lattnerf75f9be2006-05-02 23:22:24 +0000585 NumEntries += JT[i].MBBs.size();
586
587 unsigned EntrySize = MJTI->getEntrySize();
588
Nate Begeman37efe672006-04-22 18:53:45 +0000589 // Just allocate space for all the jump tables now. We will fix up the actual
590 // MBB entries in the tables after we emit the code for each block, since then
591 // we will know the final locations of the MBBs in memory.
592 JumpTable = MJTI;
Chris Lattnerf75f9be2006-05-02 23:22:24 +0000593 JumpTableBase = allocateSpace(NumEntries * EntrySize, MJTI->getAlignment());
Nate Begeman37efe672006-04-22 18:53:45 +0000594}
595
Jim Laskeyb92767a2006-12-14 22:53:42 +0000596void JITEmitter::emitJumpTableInfo(MachineJumpTableInfo *MJTI) {
Nate Begeman37efe672006-04-22 18:53:45 +0000597 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
Chris Lattnerf75f9be2006-05-02 23:22:24 +0000598 if (JT.empty() || JumpTableBase == 0) return;
Nate Begeman37efe672006-04-22 18:53:45 +0000599
Jim Laskeyb92767a2006-12-14 22:53:42 +0000600 if (TargetMachine::getRelocationModel() == Reloc::PIC_) {
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000601 assert(MJTI->getEntrySize() == 4 && "Cross JIT'ing?");
602 // For each jump table, place the offset from the beginning of the table
603 // to the target address.
604 int *SlotPtr = (int*)JumpTableBase;
Chris Lattner32ca55f2006-05-03 00:13:06 +0000605
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000606 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
607 const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs;
608 // Store the offset of the basic block for this jump table slot in the
609 // memory we allocated for the jump table in 'initJumpTableInfo'
610 intptr_t Base = (intptr_t)SlotPtr;
Evan Cheng2a3e08b2008-01-05 02:26:58 +0000611 for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) {
612 intptr_t MBBAddr = getMachineBasicBlockAddress(MBBs[mi]);
613 *SlotPtr++ = TheJIT->getJITInfo().getPICJumpTableEntry(MBBAddr, Base);
614 }
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000615 }
616 } else {
617 assert(MJTI->getEntrySize() == sizeof(void*) && "Cross JIT'ing?");
618
619 // For each jump table, map each target in the jump table to the address of
620 // an emitted MachineBasicBlock.
621 intptr_t *SlotPtr = (intptr_t*)JumpTableBase;
622
623 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
624 const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs;
625 // Store the address of the basic block for this jump table slot in the
626 // memory we allocated for the jump table in 'initJumpTableInfo'
627 for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi)
628 *SlotPtr++ = getMachineBasicBlockAddress(MBBs[mi]);
629 }
Nate Begeman37efe672006-04-22 18:53:45 +0000630 }
631}
632
Evan Cheng9a1e9b92006-11-16 20:04:54 +0000633void JITEmitter::startFunctionStub(unsigned StubSize, unsigned Alignment) {
Chris Lattner43b429b2006-05-02 18:27:26 +0000634 SavedBufferBegin = BufferBegin;
635 SavedBufferEnd = BufferEnd;
636 SavedCurBufferPtr = CurBufferPtr;
637
Chris Lattner8907b4b2007-12-05 23:39:57 +0000638 BufferBegin = CurBufferPtr = MemMgr->allocateStub(StubSize, Alignment);
Chris Lattner43b429b2006-05-02 18:27:26 +0000639 BufferEnd = BufferBegin+StubSize+1;
Chris Lattner6125fdd2003-05-09 03:30:07 +0000640}
641
Chris Lattner166f2262004-11-22 22:00:25 +0000642void *JITEmitter::finishFunctionStub(const Function *F) {
Chris Lattner43b429b2006-05-02 18:27:26 +0000643 NumBytes += getCurrentPCOffset();
644 std::swap(SavedBufferBegin, BufferBegin);
645 BufferEnd = SavedBufferEnd;
646 CurBufferPtr = SavedCurBufferPtr;
647 return SavedBufferBegin;
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000648}
649
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000650// getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry
651// in the constant pool that was last emitted with the 'emitConstantPool'
652// method.
653//
Chris Lattnerb4432f32006-05-03 17:10:41 +0000654intptr_t JITEmitter::getConstantPoolEntryAddress(unsigned ConstantNum) const {
Chris Lattner239862c2006-02-09 04:49:59 +0000655 assert(ConstantNum < ConstantPool->getConstants().size() &&
Misha Brukman3c944972005-04-22 04:08:30 +0000656 "Invalid ConstantPoolIndex!");
Chris Lattner239862c2006-02-09 04:49:59 +0000657 return (intptr_t)ConstantPoolBase +
658 ConstantPool->getConstants()[ConstantNum].Offset;
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000659}
660
Nate Begeman37efe672006-04-22 18:53:45 +0000661// getJumpTableEntryAddress - Return the address of the JumpTable with index
662// 'Index' in the jumpp table that was last initialized with 'initJumpTableInfo'
663//
Chris Lattnerb4432f32006-05-03 17:10:41 +0000664intptr_t JITEmitter::getJumpTableEntryAddress(unsigned Index) const {
Nate Begeman37efe672006-04-22 18:53:45 +0000665 const std::vector<MachineJumpTableEntry> &JT = JumpTable->getJumpTables();
666 assert(Index < JT.size() && "Invalid jump table index!");
667
668 unsigned Offset = 0;
669 unsigned EntrySize = JumpTable->getEntrySize();
670
671 for (unsigned i = 0; i < Index; ++i)
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000672 Offset += JT[i].MBBs.size();
673
674 Offset *= EntrySize;
Nate Begeman37efe672006-04-22 18:53:45 +0000675
Nate Begemanc34b2272006-04-25 17:46:32 +0000676 return (intptr_t)((char *)JumpTableBase + Offset);
Nate Begeman37efe672006-04-22 18:53:45 +0000677}
678
Chris Lattnere993cc22006-05-11 23:08:08 +0000679//===----------------------------------------------------------------------===//
680// Public interface to this file
681//===----------------------------------------------------------------------===//
682
Chris Lattner9f2f1422007-12-06 01:08:09 +0000683MachineCodeEmitter *JIT::createEmitter(JIT &jit, JITMemoryManager *JMM) {
684 return new JITEmitter(jit, JMM);
Chris Lattnere993cc22006-05-11 23:08:08 +0000685}
686
Misha Brukmand69c1e62003-07-28 19:09:06 +0000687// getPointerToNamedFunction - This function is used as a global wrapper to
Chris Lattner4d326fa2003-12-20 01:46:27 +0000688// JIT::getPointerToNamedFunction for the purpose of resolving symbols when
Misha Brukmand69c1e62003-07-28 19:09:06 +0000689// bugpoint is debugging the JIT. In that scenario, we are loading an .so and
690// need to resolve function(s) that are being mis-codegenerated, so we need to
691// resolve their addresses at runtime, and this is the way to do it.
692extern "C" {
693 void *getPointerToNamedFunction(const char *Name) {
Chris Lattnerfe854032006-08-16 01:24:12 +0000694 if (Function *F = TheJIT->FindFunctionNamed(Name))
Chris Lattner4d326fa2003-12-20 01:46:27 +0000695 return TheJIT->getPointerToFunction(F);
696 return TheJIT->getPointerToNamedFunction(Name);
Misha Brukmand69c1e62003-07-28 19:09:06 +0000697 }
698}
Chris Lattnere993cc22006-05-11 23:08:08 +0000699
700// getPointerToFunctionOrStub - If the specified function has been
701// code-gen'd, return a pointer to the function. If not, compile it, or use
702// a stub to implement lazy compilation if available.
703//
704void *JIT::getPointerToFunctionOrStub(Function *F) {
705 // If we have already code generated the function, just return the address.
706 if (void *Addr = getPointerToGlobalIfAvailable(F))
707 return Addr;
708
Chris Lattnere7484012007-02-24 02:57:03 +0000709 // Get a stub if the target supports it.
710 assert(dynamic_cast<JITEmitter*>(MCE) && "Unexpected MCE?");
711 JITEmitter *JE = static_cast<JITEmitter*>(getCodeEmitter());
712 return JE->getJITResolver().getFunctionStub(F);
Chris Lattnere993cc22006-05-11 23:08:08 +0000713}
714
715/// freeMachineCodeForFunction - release machine code memory for given Function.
716///
717void JIT::freeMachineCodeForFunction(Function *F) {
718 // Delete translation for this from the ExecutionEngine, so it will get
719 // retranslated next time it is used.
720 updateGlobalMapping(F, 0);
721
722 // Free the actual memory for the function body and related stuff.
723 assert(dynamic_cast<JITEmitter*>(MCE) && "Unexpected MCE?");
Chris Lattnere7484012007-02-24 02:57:03 +0000724 static_cast<JITEmitter*>(MCE)->deallocateMemForFunction(F);
Chris Lattnere993cc22006-05-11 23:08:08 +0000725}
726