blob: aa572a408f8e1b4cdf71204f01a2064bfcb9dbad [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
Evan Cheng55fc2802006-07-25 20:40:54 +0000185 // Invalidate the icache if necessary.
Chris Lattnere7484012007-02-24 02:57:03 +0000186 synchronizeICache(Stub, TheJIT->getCodeEmitter()->getCurrentPCValue() -
187 (intptr_t)Stub);
Evan Cheng55fc2802006-07-25 20:40:54 +0000188
Bill Wendling832171c2006-12-07 20:04:42 +0000189 DOUT << "JIT: Stub emitted at [" << Stub << "] for function '"
190 << F->getName() << "'\n";
Chris Lattnercb479412004-11-21 03:44:32 +0000191
Chris Lattner54266522004-11-20 23:57:07 +0000192 // Finally, keep track of the stub-to-Function mapping so that the
193 // JITCompilerFn knows which function to compile!
Reid Spenceree448632005-07-12 15:51:55 +0000194 state.getStubToFunctionMap(locked)[Stub] = F;
Chris Lattner54266522004-11-20 23:57:07 +0000195 return Stub;
196}
197
Evan Chengbe8c03f2008-01-04 10:46:51 +0000198/// getGlobalValueLazyPtr - Return a lazy pointer containing the specified
199/// GV address.
200void *JITResolver::getGlobalValueLazyPtr(GlobalValue *GV, void *GVAddress) {
201 MutexGuard locked(TheJIT->lock);
202
203 // If we already have a stub for this global variable, recycle it.
204 void *&LazyPtr = state.getGlobalToLazyPtrMap(locked)[GV];
205 if (LazyPtr) return LazyPtr;
206
207 // Otherwise, codegen a new lazy pointer.
208 LazyPtr = TheJIT->getJITInfo().emitGlobalValueLazyPtr(GVAddress,
209 *TheJIT->getCodeEmitter());
210
211 DOUT << "JIT: Stub emitted at [" << LazyPtr << "] for GV '"
212 << GV->getName() << "'\n";
213
214 return LazyPtr;
215}
216
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000217/// getExternalFunctionStub - Return a stub for the function at the
218/// specified address, created lazily on demand.
219void *JITResolver::getExternalFunctionStub(void *FnAddr) {
220 // If we already have a stub for this function, recycle it.
221 void *&Stub = ExternalFnToStubMap[FnAddr];
222 if (Stub) return Stub;
223
Chris Lattnere7484012007-02-24 02:57:03 +0000224 Stub = TheJIT->getJITInfo().emitFunctionStub(FnAddr,
225 *TheJIT->getCodeEmitter());
Evan Cheng55fc2802006-07-25 20:40:54 +0000226
227 // Invalidate the icache if necessary.
Chris Lattnere7484012007-02-24 02:57:03 +0000228 synchronizeICache(Stub, TheJIT->getCodeEmitter()->getCurrentPCValue() -
229 (intptr_t)Stub);
Evan Cheng55fc2802006-07-25 20:40:54 +0000230
Bill Wendling832171c2006-12-07 20:04:42 +0000231 DOUT << "JIT: Stub emitted at [" << Stub
232 << "] for external function at '" << FnAddr << "'\n";
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000233 return Stub;
234}
235
Andrew Lenharth6a974612005-07-28 12:44:13 +0000236unsigned JITResolver::getGOTIndexForAddr(void* addr) {
237 unsigned idx = revGOTMap[addr];
238 if (!idx) {
239 idx = ++nextGOTIndex;
240 revGOTMap[addr] = idx;
Bill Wendling832171c2006-12-07 20:04:42 +0000241 DOUT << "Adding GOT entry " << idx
242 << " for addr " << addr << "\n";
Andrew Lenharth6a974612005-07-28 12:44:13 +0000243 }
244 return idx;
245}
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000246
Chris Lattner54266522004-11-20 23:57:07 +0000247/// JITCompilerFn - This function is called when a lazy compilation stub has
248/// been entered. It looks up which function this stub corresponds to, compiles
249/// it if necessary, then returns the resultant function pointer.
250void *JITResolver::JITCompilerFn(void *Stub) {
Chris Lattnere7484012007-02-24 02:57:03 +0000251 JITResolver &JR = *TheJITResolver;
Misha Brukmanf976c852005-04-21 22:55:34 +0000252
Reid Spenceree448632005-07-12 15:51:55 +0000253 MutexGuard locked(TheJIT->lock);
254
Chris Lattner54266522004-11-20 23:57:07 +0000255 // The address given to us for the stub may not be exactly right, it might be
256 // a little bit after the stub. As such, use upper_bound to find it.
257 std::map<void*, Function*>::iterator I =
Reid Spenceree448632005-07-12 15:51:55 +0000258 JR.state.getStubToFunctionMap(locked).upper_bound(Stub);
Chris Lattner21998772006-01-07 06:20:51 +0000259 assert(I != JR.state.getStubToFunctionMap(locked).begin() &&
260 "This is not a known stub!");
Chris Lattner54266522004-11-20 23:57:07 +0000261 Function *F = (--I)->second;
262
Evan Cheng9da60f92007-06-30 00:10:37 +0000263 // If we have already code generated the function, just return the address.
264 void *Result = TheJIT->getPointerToGlobalIfAvailable(F);
Chris Lattner9cab56d2006-11-09 19:32:13 +0000265
Evan Cheng9da60f92007-06-30 00:10:37 +0000266 if (!Result) {
267 // Otherwise we don't have it, do lazy compilation now.
268
269 // If lazy compilation is disabled, emit a useful error message and abort.
270 if (TheJIT->isLazyCompilationDisabled()) {
271 cerr << "LLVM JIT requested to do lazy compilation of function '"
272 << F->getName() << "' when lazy compiles are disabled!\n";
273 abort();
274 }
275
276 // We might like to remove the stub from the StubToFunction map.
277 // We can't do that! Multiple threads could be stuck, waiting to acquire the
278 // lock above. As soon as the 1st function finishes compiling the function,
279 // the next one will be released, and needs to be able to find the function
280 // it needs to call.
281 //JR.state.getStubToFunctionMap(locked).erase(I);
Chris Lattner54266522004-11-20 23:57:07 +0000282
Evan Cheng9da60f92007-06-30 00:10:37 +0000283 DOUT << "JIT: Lazily resolving function '" << F->getName()
284 << "' In stub ptr = " << Stub << " actual ptr = "
285 << I->first << "\n";
Chris Lattner54266522004-11-20 23:57:07 +0000286
Evan Cheng9da60f92007-06-30 00:10:37 +0000287 Result = TheJIT->getPointerToFunction(F);
288 }
Chris Lattner54266522004-11-20 23:57:07 +0000289
290 // We don't need to reuse this stub in the future, as F is now compiled.
Reid Spenceree448632005-07-12 15:51:55 +0000291 JR.state.getFunctionToStubMap(locked).erase(F);
Chris Lattner54266522004-11-20 23:57:07 +0000292
293 // FIXME: We could rewrite all references to this stub if we knew them.
Andrew Lenharth6a974612005-07-28 12:44:13 +0000294
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000295 // What we will do is set the compiled function address to map to the
296 // same GOT entry as the stub so that later clients may update the GOT
Andrew Lenharth6a974612005-07-28 12:44:13 +0000297 // if they see it still using the stub address.
298 // Note: this is done so the Resolver doesn't have to manage GOT memory
299 // Do this without allocating map space if the target isn't using a GOT
300 if(JR.revGOTMap.find(Stub) != JR.revGOTMap.end())
301 JR.revGOTMap[Result] = JR.revGOTMap[Stub];
302
Chris Lattner54266522004-11-20 23:57:07 +0000303 return Result;
304}
Chris Lattner688506d2003-08-14 18:35:27 +0000305
306
Chris Lattner54266522004-11-20 23:57:07 +0000307//===----------------------------------------------------------------------===//
Chris Lattner166f2262004-11-22 22:00:25 +0000308// JITEmitter code.
Chris Lattner54266522004-11-20 23:57:07 +0000309//
Chris Lattner688506d2003-08-14 18:35:27 +0000310namespace {
Chris Lattner166f2262004-11-22 22:00:25 +0000311 /// JITEmitter - The JIT implementation of the MachineCodeEmitter, which is
312 /// used to output functions to memory for execution.
313 class JITEmitter : public MachineCodeEmitter {
Chris Lattner8907b4b2007-12-05 23:39:57 +0000314 JITMemoryManager *MemMgr;
Chris Lattner688506d2003-08-14 18:35:27 +0000315
Chris Lattner6125fdd2003-05-09 03:30:07 +0000316 // When outputting a function stub in the context of some other function, we
Chris Lattner43b429b2006-05-02 18:27:26 +0000317 // save BufferBegin/BufferEnd/CurBufferPtr here.
318 unsigned char *SavedBufferBegin, *SavedBufferEnd, *SavedCurBufferPtr;
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000319
Chris Lattner5be478f2004-11-20 03:46:14 +0000320 /// Relocations - These are the relocations that the function needs, as
321 /// emitted.
322 std::vector<MachineRelocation> Relocations;
Chris Lattnerb4432f32006-05-03 17:10:41 +0000323
324 /// MBBLocations - This vector is a mapping from MBB ID's to their address.
325 /// It is filled in by the StartMachineBasicBlock callback and queried by
326 /// the getMachineBasicBlockAddress callback.
327 std::vector<intptr_t> MBBLocations;
Andrew Lenharth16ec33c2005-07-22 20:48:12 +0000328
Chris Lattner239862c2006-02-09 04:49:59 +0000329 /// ConstantPool - The constant pool for the current function.
330 ///
331 MachineConstantPool *ConstantPool;
332
333 /// ConstantPoolBase - A pointer to the first entry in the constant pool.
334 ///
335 void *ConstantPoolBase;
Nate Begeman37efe672006-04-22 18:53:45 +0000336
Nate Begeman019f8512006-09-10 23:03:44 +0000337 /// JumpTable - The jump tables for the current function.
Nate Begeman37efe672006-04-22 18:53:45 +0000338 ///
339 MachineJumpTableInfo *JumpTable;
340
341 /// JumpTableBase - A pointer to the first entry in the jump table.
342 ///
343 void *JumpTableBase;
Chris Lattnere7484012007-02-24 02:57:03 +0000344
345 /// Resolver - This contains info about the currently resolved functions.
346 JITResolver Resolver;
347 public:
Chris Lattner9f2f1422007-12-06 01:08:09 +0000348 JITEmitter(JIT &jit, JITMemoryManager *JMM) : Resolver(jit) {
349 MemMgr = JMM ? JMM : JITMemoryManager::CreateDefaultMemManager();
Chris Lattner8907b4b2007-12-05 23:39:57 +0000350 if (jit.getJITInfo().needsGOT()) {
351 MemMgr->AllocateGOT();
352 DOUT << "JIT is managing a GOT\n";
353 }
354 }
355 ~JITEmitter() {
356 delete MemMgr;
Andrew Lenharth16ec33c2005-07-22 20:48:12 +0000357 }
Chris Lattnere7484012007-02-24 02:57:03 +0000358
359 JITResolver &getJITResolver() { return Resolver; }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000360
361 virtual void startFunction(MachineFunction &F);
Chris Lattner43b429b2006-05-02 18:27:26 +0000362 virtual bool finishFunction(MachineFunction &F);
Chris Lattnerf75f9be2006-05-02 23:22:24 +0000363
364 void emitConstantPool(MachineConstantPool *MCP);
365 void initJumpTableInfo(MachineJumpTableInfo *MJTI);
Jim Laskeyb92767a2006-12-14 22:53:42 +0000366 void emitJumpTableInfo(MachineJumpTableInfo *MJTI);
Chris Lattnerf75f9be2006-05-02 23:22:24 +0000367
Evan Cheng9a1e9b92006-11-16 20:04:54 +0000368 virtual void startFunctionStub(unsigned StubSize, unsigned Alignment = 1);
Chris Lattner54266522004-11-20 23:57:07 +0000369 virtual void* finishFunctionStub(const Function *F);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000370
Chris Lattner5be478f2004-11-20 03:46:14 +0000371 virtual void addRelocation(const MachineRelocation &MR) {
372 Relocations.push_back(MR);
373 }
Chris Lattnerb4432f32006-05-03 17:10:41 +0000374
375 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) {
376 if (MBBLocations.size() <= (unsigned)MBB->getNumber())
377 MBBLocations.resize((MBB->getNumber()+1)*2);
378 MBBLocations[MBB->getNumber()] = getCurrentPCValue();
379 }
Chris Lattner5be478f2004-11-20 03:46:14 +0000380
Chris Lattnerb4432f32006-05-03 17:10:41 +0000381 virtual intptr_t getConstantPoolEntryAddress(unsigned Entry) const;
382 virtual intptr_t getJumpTableEntryAddress(unsigned Entry) const;
383
384 virtual intptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
385 assert(MBBLocations.size() > (unsigned)MBB->getNumber() &&
386 MBBLocations[MBB->getNumber()] && "MBB not emitted!");
387 return MBBLocations[MBB->getNumber()];
388 }
389
Chris Lattnere993cc22006-05-11 23:08:08 +0000390 /// deallocateMemForFunction - Deallocate all memory for the specified
391 /// function body.
392 void deallocateMemForFunction(Function *F) {
Chris Lattner8907b4b2007-12-05 23:39:57 +0000393 MemMgr->deallocateMemForFunction(F);
Chris Lattnere993cc22006-05-11 23:08:08 +0000394 }
Chris Lattner54266522004-11-20 23:57:07 +0000395 private:
Chris Lattner5e225582004-11-21 03:37:42 +0000396 void *getPointerToGlobal(GlobalValue *GV, void *Reference, bool NoNeedStub);
Evan Chengbe8c03f2008-01-04 10:46:51 +0000397 void *getPointerToGVLazyPtr(GlobalValue *V, void *Reference,
398 bool NoNeedStub);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000399 };
400}
401
Chris Lattner166f2262004-11-22 22:00:25 +0000402void *JITEmitter::getPointerToGlobal(GlobalValue *V, void *Reference,
403 bool DoesntNeedStub) {
Chris Lattner54266522004-11-20 23:57:07 +0000404 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
405 /// FIXME: If we straightened things out, this could actually emit the
406 /// global immediately instead of queuing it for codegen later!
Chris Lattner54266522004-11-20 23:57:07 +0000407 return TheJIT->getOrEmitGlobalVariable(GV);
408 }
409
410 // If we have already compiled the function, return a pointer to its body.
411 Function *F = cast<Function>(V);
412 void *ResultPtr = TheJIT->getPointerToGlobalIfAvailable(F);
413 if (ResultPtr) return ResultPtr;
414
Gabor Greifa99be512007-07-05 17:07:56 +0000415 if (F->isDeclaration() && !F->hasNotBeenReadFromBitcode()) {
Chris Lattner54266522004-11-20 23:57:07 +0000416 // If this is an external function pointer, we can force the JIT to
417 // 'compile' it, which really just adds it to the map.
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000418 if (DoesntNeedStub)
419 return TheJIT->getPointerToFunction(F);
420
Chris Lattnere7484012007-02-24 02:57:03 +0000421 return Resolver.getFunctionStub(F);
Chris Lattner54266522004-11-20 23:57:07 +0000422 }
423
Chris Lattner5e225582004-11-21 03:37:42 +0000424 // Okay, the function has not been compiled yet, if the target callback
425 // mechanism is capable of rewriting the instruction directly, prefer to do
426 // that instead of emitting a stub.
427 if (DoesntNeedStub)
Chris Lattnere7484012007-02-24 02:57:03 +0000428 return Resolver.AddCallbackAtLocation(F, Reference);
Chris Lattner5e225582004-11-21 03:37:42 +0000429
Chris Lattner54266522004-11-20 23:57:07 +0000430 // Otherwise, we have to emit a lazy resolving stub.
Chris Lattnere7484012007-02-24 02:57:03 +0000431 return Resolver.getFunctionStub(F);
Chris Lattner54266522004-11-20 23:57:07 +0000432}
433
Evan Chengbe8c03f2008-01-04 10:46:51 +0000434void *JITEmitter::getPointerToGVLazyPtr(GlobalValue *V, void *Reference,
435 bool DoesntNeedStub) {
436 // Make sure GV is emitted first.
437 // FIXME: For now, if the GV is an external function we force the JIT to
438 // compile it so the lazy pointer will contain the fully resolved address.
439 void *GVAddress = getPointerToGlobal(V, Reference, true);
440 return Resolver.getGlobalValueLazyPtr(V, GVAddress);
441}
442
443
Chris Lattner166f2262004-11-22 22:00:25 +0000444void JITEmitter::startFunction(MachineFunction &F) {
Chris Lattnere993cc22006-05-11 23:08:08 +0000445 uintptr_t ActualSize;
Chris Lattner8907b4b2007-12-05 23:39:57 +0000446 BufferBegin = CurBufferPtr = MemMgr->startFunctionBody(F.getFunction(),
447 ActualSize);
Chris Lattnere993cc22006-05-11 23:08:08 +0000448 BufferEnd = BufferBegin+ActualSize;
Chris Lattnerf75f9be2006-05-02 23:22:24 +0000449
Evan Cheng9a1e9b92006-11-16 20:04:54 +0000450 // Ensure the constant pool/jump table info is at least 4-byte aligned.
451 emitAlignment(16);
452
Chris Lattnerf75f9be2006-05-02 23:22:24 +0000453 emitConstantPool(F.getConstantPool());
454 initJumpTableInfo(F.getJumpTableInfo());
455
456 // About to start emitting the machine code for the function.
Chris Lattner0eb4d6b2006-05-03 01:03:20 +0000457 emitAlignment(std::max(F.getFunction()->getAlignment(), 8U));
Chris Lattnerf75f9be2006-05-02 23:22:24 +0000458 TheJIT->updateGlobalMapping(F.getFunction(), CurBufferPtr);
Evan Cheng55fc2802006-07-25 20:40:54 +0000459
Chris Lattnerb4432f32006-05-03 17:10:41 +0000460 MBBLocations.clear();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000461}
462
Chris Lattner43b429b2006-05-02 18:27:26 +0000463bool JITEmitter::finishFunction(MachineFunction &F) {
Chris Lattnere993cc22006-05-11 23:08:08 +0000464 if (CurBufferPtr == BufferEnd) {
465 // FIXME: Allocate more space, then try again.
Bill Wendling832171c2006-12-07 20:04:42 +0000466 cerr << "JIT: Ran out of space for generated machine code!\n";
Chris Lattnere993cc22006-05-11 23:08:08 +0000467 abort();
468 }
469
Jim Laskeyb92767a2006-12-14 22:53:42 +0000470 emitJumpTableInfo(F.getJumpTableInfo());
Chris Lattnerb4432f32006-05-03 17:10:41 +0000471
Chris Lattnera8279532006-06-16 18:09:26 +0000472 // FnStart is the start of the text, not the start of the constant pool and
473 // other per-function data.
474 unsigned char *FnStart =
475 (unsigned char *)TheJIT->getPointerToGlobalIfAvailable(F.getFunction());
476 unsigned char *FnEnd = CurBufferPtr;
477
Chris Lattner8907b4b2007-12-05 23:39:57 +0000478 MemMgr->endFunctionBody(F.getFunction(), BufferBegin, FnEnd);
Chris Lattnera8279532006-06-16 18:09:26 +0000479 NumBytes += FnEnd-FnStart;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000480
Chris Lattner5be478f2004-11-20 03:46:14 +0000481 if (!Relocations.empty()) {
Chris Lattnere884dc22005-07-20 16:29:20 +0000482 NumRelos += Relocations.size();
483
Chris Lattner5be478f2004-11-20 03:46:14 +0000484 // Resolve the relocations to concrete pointers.
485 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
486 MachineRelocation &MR = Relocations[i];
487 void *ResultPtr;
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000488 if (MR.isString()) {
Chris Lattner5be478f2004-11-20 03:46:14 +0000489 ResultPtr = TheJIT->getPointerToNamedFunction(MR.getString());
Misha Brukmanf976c852005-04-21 22:55:34 +0000490
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000491 // If the target REALLY wants a stub for this function, emit it now.
Evan Cheng02aabbf2008-01-03 02:56:28 +0000492 if (!MR.doesntNeedStub())
Chris Lattnere7484012007-02-24 02:57:03 +0000493 ResultPtr = Resolver.getExternalFunctionStub(ResultPtr);
Chris Lattnerd2d5c762006-05-03 18:55:56 +0000494 } else if (MR.isGlobalValue()) {
Chris Lattner5e225582004-11-21 03:37:42 +0000495 ResultPtr = getPointerToGlobal(MR.getGlobalValue(),
Chris Lattner43b429b2006-05-02 18:27:26 +0000496 BufferBegin+MR.getMachineCodeOffset(),
Evan Cheng02aabbf2008-01-03 02:56:28 +0000497 MR.doesntNeedStub());
Evan Chengbe8c03f2008-01-04 10:46:51 +0000498 } else if (MR.isGlobalValueLazyPtr()) {
499 ResultPtr = getPointerToGVLazyPtr(MR.getGlobalValue(),
500 BufferBegin+MR.getMachineCodeOffset(),
501 MR.doesntNeedStub());
Evan Chengf141cc42006-07-27 18:21:10 +0000502 } else if (MR.isBasicBlock()) {
503 ResultPtr = (void*)getMachineBasicBlockAddress(MR.getBasicBlock());
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000504 } else if (MR.isConstantPoolIndex()) {
Chris Lattnerd2d5c762006-05-03 18:55:56 +0000505 ResultPtr=(void*)getConstantPoolEntryAddress(MR.getConstantPoolIndex());
Evan Cheng52b510b2006-06-23 01:02:37 +0000506 } else {
507 assert(MR.isJumpTableIndex());
508 ResultPtr=(void*)getJumpTableEntryAddress(MR.getJumpTableIndex());
Chris Lattnerd2d5c762006-05-03 18:55:56 +0000509 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000510
Chris Lattner5be478f2004-11-20 03:46:14 +0000511 MR.setResultPointer(ResultPtr);
Andrew Lenharth16ec33c2005-07-22 20:48:12 +0000512
Andrew Lenharth6a974612005-07-28 12:44:13 +0000513 // if we are managing the GOT and the relocation wants an index,
514 // give it one
Chris Lattner8907b4b2007-12-05 23:39:57 +0000515 if (MR.isGOTRelative() && MemMgr->isManagingGOT()) {
Chris Lattnere7484012007-02-24 02:57:03 +0000516 unsigned idx = Resolver.getGOTIndexForAddr(ResultPtr);
Andrew Lenharth6a974612005-07-28 12:44:13 +0000517 MR.setGOTIndex(idx);
Chris Lattner8907b4b2007-12-05 23:39:57 +0000518 if (((void**)MemMgr->getGOTBase())[idx] != ResultPtr) {
Bill Wendling832171c2006-12-07 20:04:42 +0000519 DOUT << "GOT was out of date for " << ResultPtr
Chris Lattner8907b4b2007-12-05 23:39:57 +0000520 << " pointing at " << ((void**)MemMgr->getGOTBase())[idx]
Bill Wendling832171c2006-12-07 20:04:42 +0000521 << "\n";
Chris Lattner8907b4b2007-12-05 23:39:57 +0000522 ((void**)MemMgr->getGOTBase())[idx] = ResultPtr;
Andrew Lenharth6a974612005-07-28 12:44:13 +0000523 }
Andrew Lenharth16ec33c2005-07-22 20:48:12 +0000524 }
Chris Lattner5be478f2004-11-20 03:46:14 +0000525 }
526
Chris Lattner43b429b2006-05-02 18:27:26 +0000527 TheJIT->getJITInfo().relocate(BufferBegin, &Relocations[0],
Chris Lattner8907b4b2007-12-05 23:39:57 +0000528 Relocations.size(), MemMgr->getGOTBase());
Chris Lattner5be478f2004-11-20 03:46:14 +0000529 }
530
Chris Lattnerd2d5c762006-05-03 18:55:56 +0000531 // Update the GOT entry for F to point to the new code.
Chris Lattner8907b4b2007-12-05 23:39:57 +0000532 if (MemMgr->isManagingGOT()) {
Chris Lattnere7484012007-02-24 02:57:03 +0000533 unsigned idx = Resolver.getGOTIndexForAddr((void*)BufferBegin);
Chris Lattner8907b4b2007-12-05 23:39:57 +0000534 if (((void**)MemMgr->getGOTBase())[idx] != (void*)BufferBegin) {
Bill Wendling832171c2006-12-07 20:04:42 +0000535 DOUT << "GOT was out of date for " << (void*)BufferBegin
Chris Lattner8907b4b2007-12-05 23:39:57 +0000536 << " pointing at " << ((void**)MemMgr->getGOTBase())[idx] << "\n";
537 ((void**)MemMgr->getGOTBase())[idx] = (void*)BufferBegin;
Andrew Lenharth6a974612005-07-28 12:44:13 +0000538 }
539 }
540
Evan Cheng55fc2802006-07-25 20:40:54 +0000541 // Invalidate the icache if necessary.
Evan Cheng55b50532006-07-27 06:33:55 +0000542 synchronizeICache(FnStart, FnEnd-FnStart);
Evan Cheng55fc2802006-07-25 20:40:54 +0000543
Bill Wendling832171c2006-12-07 20:04:42 +0000544 DOUT << "JIT: Finished CodeGen of [" << (void*)FnStart
545 << "] Function: " << F.getFunction()->getName()
546 << ": " << (FnEnd-FnStart) << " bytes of text, "
547 << Relocations.size() << " relocations\n";
Chris Lattner5be478f2004-11-20 03:46:14 +0000548 Relocations.clear();
Anton Korobeynikov8cd4c3e2007-01-19 17:25:17 +0000549
Chris Lattnerc5633c22007-01-20 20:51:43 +0000550#ifndef NDEBUG
Anton Korobeynikovc6551ff2007-03-06 05:32:48 +0000551 if (sys::hasDisassembler())
552 DOUT << "Disassembled code:\n"
553 << sys::disassembleBuffer(FnStart, FnEnd-FnStart, (uintptr_t)FnStart);
Chris Lattnerc5633c22007-01-20 20:51:43 +0000554#endif
Anton Korobeynikov8cd4c3e2007-01-19 17:25:17 +0000555
Chris Lattner43b429b2006-05-02 18:27:26 +0000556 return false;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000557}
558
Chris Lattner166f2262004-11-22 22:00:25 +0000559void JITEmitter::emitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000560 const std::vector<MachineConstantPoolEntry> &Constants = MCP->getConstants();
Chris Lattner2c0a6a12003-11-30 04:23:21 +0000561 if (Constants.empty()) return;
562
Evan Chengcd5731d2006-09-12 20:59:59 +0000563 MachineConstantPoolEntry CPE = Constants.back();
564 unsigned Size = CPE.Offset;
565 const Type *Ty = CPE.isMachineConstantPoolEntry()
Chris Lattner8a650092006-09-13 16:21:10 +0000566 ? CPE.Val.MachineCPVal->getType() : CPE.Val.ConstVal->getType();
Duncan Sands514ab342007-11-01 20:53:16 +0000567 Size += TheJIT->getTargetData()->getABITypeSize(Ty);
Chris Lattner2c0a6a12003-11-30 04:23:21 +0000568
Chris Lattnerf75f9be2006-05-02 23:22:24 +0000569 ConstantPoolBase = allocateSpace(Size, 1 << MCP->getConstantPoolAlignment());
Chris Lattner239862c2006-02-09 04:49:59 +0000570 ConstantPool = MCP;
Chris Lattnerf75f9be2006-05-02 23:22:24 +0000571
572 if (ConstantPoolBase == 0) return; // Buffer overflow.
573
Chris Lattner239862c2006-02-09 04:49:59 +0000574 // Initialize the memory for all of the constant pool entries.
Chris Lattner3029f922006-02-09 04:46:04 +0000575 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Chris Lattner239862c2006-02-09 04:49:59 +0000576 void *CAddr = (char*)ConstantPoolBase+Constants[i].Offset;
Evan Chengcd5731d2006-09-12 20:59:59 +0000577 if (Constants[i].isMachineConstantPoolEntry()) {
578 // FIXME: add support to lower machine constant pool values into bytes!
Bill Wendling832171c2006-12-07 20:04:42 +0000579 cerr << "Initialize memory with machine specific constant pool entry"
580 << " has not been implemented!\n";
Evan Chengcd5731d2006-09-12 20:59:59 +0000581 abort();
582 }
583 TheJIT->InitializeMemory(Constants[i].Val.ConstVal, CAddr);
Chris Lattner1cc08382003-01-13 01:00:12 +0000584 }
585}
586
Nate Begeman37efe672006-04-22 18:53:45 +0000587void JITEmitter::initJumpTableInfo(MachineJumpTableInfo *MJTI) {
588 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
589 if (JT.empty()) return;
590
Chris Lattnerf75f9be2006-05-02 23:22:24 +0000591 unsigned NumEntries = 0;
Nate Begeman37efe672006-04-22 18:53:45 +0000592 for (unsigned i = 0, e = JT.size(); i != e; ++i)
Chris Lattnerf75f9be2006-05-02 23:22:24 +0000593 NumEntries += JT[i].MBBs.size();
594
595 unsigned EntrySize = MJTI->getEntrySize();
596
Nate Begeman37efe672006-04-22 18:53:45 +0000597 // Just allocate space for all the jump tables now. We will fix up the actual
598 // MBB entries in the tables after we emit the code for each block, since then
599 // we will know the final locations of the MBBs in memory.
600 JumpTable = MJTI;
Chris Lattnerf75f9be2006-05-02 23:22:24 +0000601 JumpTableBase = allocateSpace(NumEntries * EntrySize, MJTI->getAlignment());
Nate Begeman37efe672006-04-22 18:53:45 +0000602}
603
Jim Laskeyb92767a2006-12-14 22:53:42 +0000604void JITEmitter::emitJumpTableInfo(MachineJumpTableInfo *MJTI) {
Nate Begeman37efe672006-04-22 18:53:45 +0000605 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
Chris Lattnerf75f9be2006-05-02 23:22:24 +0000606 if (JT.empty() || JumpTableBase == 0) return;
Nate Begeman37efe672006-04-22 18:53:45 +0000607
Jim Laskeyb92767a2006-12-14 22:53:42 +0000608 if (TargetMachine::getRelocationModel() == Reloc::PIC_) {
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000609 assert(MJTI->getEntrySize() == 4 && "Cross JIT'ing?");
610 // For each jump table, place the offset from the beginning of the table
611 // to the target address.
612 int *SlotPtr = (int*)JumpTableBase;
Chris Lattner32ca55f2006-05-03 00:13:06 +0000613
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000614 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
615 const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs;
616 // Store the offset of the basic block for this jump table slot in the
617 // memory we allocated for the jump table in 'initJumpTableInfo'
618 intptr_t Base = (intptr_t)SlotPtr;
619 for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi)
620 *SlotPtr++ = (intptr_t)getMachineBasicBlockAddress(MBBs[mi]) - Base;
621 }
622 } else {
623 assert(MJTI->getEntrySize() == sizeof(void*) && "Cross JIT'ing?");
624
625 // For each jump table, map each target in the jump table to the address of
626 // an emitted MachineBasicBlock.
627 intptr_t *SlotPtr = (intptr_t*)JumpTableBase;
628
629 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
630 const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs;
631 // Store the address of the basic block for this jump table slot in the
632 // memory we allocated for the jump table in 'initJumpTableInfo'
633 for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi)
634 *SlotPtr++ = getMachineBasicBlockAddress(MBBs[mi]);
635 }
Nate Begeman37efe672006-04-22 18:53:45 +0000636 }
637}
638
Evan Cheng9a1e9b92006-11-16 20:04:54 +0000639void JITEmitter::startFunctionStub(unsigned StubSize, unsigned Alignment) {
Chris Lattner43b429b2006-05-02 18:27:26 +0000640 SavedBufferBegin = BufferBegin;
641 SavedBufferEnd = BufferEnd;
642 SavedCurBufferPtr = CurBufferPtr;
643
Chris Lattner8907b4b2007-12-05 23:39:57 +0000644 BufferBegin = CurBufferPtr = MemMgr->allocateStub(StubSize, Alignment);
Chris Lattner43b429b2006-05-02 18:27:26 +0000645 BufferEnd = BufferBegin+StubSize+1;
Chris Lattner6125fdd2003-05-09 03:30:07 +0000646}
647
Chris Lattner166f2262004-11-22 22:00:25 +0000648void *JITEmitter::finishFunctionStub(const Function *F) {
Chris Lattner43b429b2006-05-02 18:27:26 +0000649 NumBytes += getCurrentPCOffset();
650 std::swap(SavedBufferBegin, BufferBegin);
651 BufferEnd = SavedBufferEnd;
652 CurBufferPtr = SavedCurBufferPtr;
653 return SavedBufferBegin;
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000654}
655
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000656// getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry
657// in the constant pool that was last emitted with the 'emitConstantPool'
658// method.
659//
Chris Lattnerb4432f32006-05-03 17:10:41 +0000660intptr_t JITEmitter::getConstantPoolEntryAddress(unsigned ConstantNum) const {
Chris Lattner239862c2006-02-09 04:49:59 +0000661 assert(ConstantNum < ConstantPool->getConstants().size() &&
Misha Brukman3c944972005-04-22 04:08:30 +0000662 "Invalid ConstantPoolIndex!");
Chris Lattner239862c2006-02-09 04:49:59 +0000663 return (intptr_t)ConstantPoolBase +
664 ConstantPool->getConstants()[ConstantNum].Offset;
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000665}
666
Nate Begeman37efe672006-04-22 18:53:45 +0000667// getJumpTableEntryAddress - Return the address of the JumpTable with index
668// 'Index' in the jumpp table that was last initialized with 'initJumpTableInfo'
669//
Chris Lattnerb4432f32006-05-03 17:10:41 +0000670intptr_t JITEmitter::getJumpTableEntryAddress(unsigned Index) const {
Nate Begeman37efe672006-04-22 18:53:45 +0000671 const std::vector<MachineJumpTableEntry> &JT = JumpTable->getJumpTables();
672 assert(Index < JT.size() && "Invalid jump table index!");
673
674 unsigned Offset = 0;
675 unsigned EntrySize = JumpTable->getEntrySize();
676
677 for (unsigned i = 0; i < Index; ++i)
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000678 Offset += JT[i].MBBs.size();
679
680 Offset *= EntrySize;
Nate Begeman37efe672006-04-22 18:53:45 +0000681
Nate Begemanc34b2272006-04-25 17:46:32 +0000682 return (intptr_t)((char *)JumpTableBase + Offset);
Nate Begeman37efe672006-04-22 18:53:45 +0000683}
684
Chris Lattnere993cc22006-05-11 23:08:08 +0000685//===----------------------------------------------------------------------===//
686// Public interface to this file
687//===----------------------------------------------------------------------===//
688
Chris Lattner9f2f1422007-12-06 01:08:09 +0000689MachineCodeEmitter *JIT::createEmitter(JIT &jit, JITMemoryManager *JMM) {
690 return new JITEmitter(jit, JMM);
Chris Lattnere993cc22006-05-11 23:08:08 +0000691}
692
Misha Brukmand69c1e62003-07-28 19:09:06 +0000693// getPointerToNamedFunction - This function is used as a global wrapper to
Chris Lattner4d326fa2003-12-20 01:46:27 +0000694// JIT::getPointerToNamedFunction for the purpose of resolving symbols when
Misha Brukmand69c1e62003-07-28 19:09:06 +0000695// bugpoint is debugging the JIT. In that scenario, we are loading an .so and
696// need to resolve function(s) that are being mis-codegenerated, so we need to
697// resolve their addresses at runtime, and this is the way to do it.
698extern "C" {
699 void *getPointerToNamedFunction(const char *Name) {
Chris Lattnerfe854032006-08-16 01:24:12 +0000700 if (Function *F = TheJIT->FindFunctionNamed(Name))
Chris Lattner4d326fa2003-12-20 01:46:27 +0000701 return TheJIT->getPointerToFunction(F);
702 return TheJIT->getPointerToNamedFunction(Name);
Misha Brukmand69c1e62003-07-28 19:09:06 +0000703 }
704}
Chris Lattnere993cc22006-05-11 23:08:08 +0000705
706// getPointerToFunctionOrStub - If the specified function has been
707// code-gen'd, return a pointer to the function. If not, compile it, or use
708// a stub to implement lazy compilation if available.
709//
710void *JIT::getPointerToFunctionOrStub(Function *F) {
711 // If we have already code generated the function, just return the address.
712 if (void *Addr = getPointerToGlobalIfAvailable(F))
713 return Addr;
714
Chris Lattnere7484012007-02-24 02:57:03 +0000715 // Get a stub if the target supports it.
716 assert(dynamic_cast<JITEmitter*>(MCE) && "Unexpected MCE?");
717 JITEmitter *JE = static_cast<JITEmitter*>(getCodeEmitter());
718 return JE->getJITResolver().getFunctionStub(F);
Chris Lattnere993cc22006-05-11 23:08:08 +0000719}
720
721/// freeMachineCodeForFunction - release machine code memory for given Function.
722///
723void JIT::freeMachineCodeForFunction(Function *F) {
724 // Delete translation for this from the ExecutionEngine, so it will get
725 // retranslated next time it is used.
726 updateGlobalMapping(F, 0);
727
728 // Free the actual memory for the function body and related stuff.
729 assert(dynamic_cast<JITEmitter*>(MCE) && "Unexpected MCE?");
Chris Lattnere7484012007-02-24 02:57:03 +0000730 static_cast<JITEmitter*>(MCE)->deallocateMemForFunction(F);
Chris Lattnere993cc22006-05-11 23:08:08 +0000731}
732