blob: beeca3b0f65c8b1bdf1873bb6ec127559e7af2f5 [file] [log] [blame]
Chris Lattnerc5753052004-11-22 22:00:25 +00001//===-- JITEmitter.cpp - Write machine code to executable memory ----------===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukman10468d82005-04-21 22:55:34 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner996fe012002-12-24 00:01:05 +00009//
Chris Lattner6cf7a432004-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 Lattner996fe012002-12-24 00:01:05 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattneree937c82003-08-05 17:00:32 +000015#define DEBUG_TYPE "jit"
Chris Lattner9bcae072003-12-20 01:46:27 +000016#include "JIT.h"
Chris Lattner98e72b42003-11-30 04:23:21 +000017#include "llvm/Constant.h"
18#include "llvm/Module.h"
Chris Lattner7b9020a2005-03-17 15:38:16 +000019#include "llvm/Type.h"
Chris Lattner996fe012002-12-24 00:01:05 +000020#include "llvm/CodeGen/MachineCodeEmitter.h"
21#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner4ba3bbb2003-01-13 01:00:12 +000022#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begeman4ca2ea52006-04-22 18:53:45 +000023#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner6cf7a432004-11-20 03:46:14 +000024#include "llvm/CodeGen/MachineRelocation.h"
Chris Lattner55d8c3f2007-12-05 23:39:57 +000025#include "llvm/ExecutionEngine/JITMemoryManager.h"
Chris Lattner4ba3bbb2003-01-13 01:00:12 +000026#include "llvm/Target/TargetData.h"
Chris Lattner6cf7a432004-11-20 03:46:14 +000027#include "llvm/Target/TargetJITInfo.h"
Jim Laskey70323a82006-12-14 19:17:33 +000028#include "llvm/Target/TargetMachine.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000029#include "llvm/Support/Debug.h"
Chris Lattner6d8dd182006-05-08 22:00:52 +000030#include "llvm/Support/MutexGuard.h"
Anton Korobeynikova41d9ec2007-01-23 10:26:08 +000031#include "llvm/System/Disassembler.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000032#include "llvm/ADT/Statistic.h"
Andrew Lenharth09402182005-07-29 23:40:16 +000033#include <algorithm>
Chris Lattnerc0e1b072003-12-08 08:06:28 +000034using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000035
Chris Lattnerc346ecd2006-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 Lattner60d815a2004-11-20 23:57:07 +000039
Andrew Lenharth09402182005-07-29 23:40:16 +000040
Chris Lattner60d815a2004-11-20 23:57:07 +000041//===----------------------------------------------------------------------===//
42// JIT lazy compilation code.
43//
44namespace {
Reid Spencer79876f52005-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 Cohen5f4ef3c2005-07-27 06:12:32 +000054
Reid Spencer79876f52005-07-12 15:51:55 +000055 public:
56 std::map<Function*, void*>& getFunctionToStubMap(const MutexGuard& locked) {
57 assert(locked.holds(TheJIT->lock));
58 return FunctionToStubMap;
59 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000060
Reid Spencer79876f52005-07-12 15:51:55 +000061 std::map<void*, Function*>& getStubToFunctionMap(const MutexGuard& locked) {
62 assert(locked.holds(TheJIT->lock));
63 return StubToFunctionMap;
64 }
65 };
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000066
Chris Lattner60d815a2004-11-20 23:57:07 +000067 /// JITResolver - Keep track of, and resolve, call sites for functions that
68 /// have not yet been compiled.
69 class JITResolver {
Chris Lattner65f66382004-11-21 03:37:42 +000070 /// LazyResolverFn - The target lazy resolver function that we actually
71 /// rewrite instructions to use.
72 TargetJITInfo::LazyResolverFn LazyResolverFn;
73
Reid Spencer79876f52005-07-12 15:51:55 +000074 JITResolverState state;
Chris Lattner60d815a2004-11-20 23:57:07 +000075
Chris Lattnered5189d2005-04-18 01:44:27 +000076 /// ExternalFnToStubMap - This is the equivalent of FunctionToStubMap for
77 /// external functions.
78 std::map<void*, void*> ExternalFnToStubMap;
Andrew Lenharth3444cf52005-07-28 12:44:13 +000079
80 //map addresses to indexes in the GOT
81 std::map<void*, unsigned> revGOTMap;
82 unsigned nextGOTIndex;
83
Chris Lattner05858a92007-02-24 02:57:03 +000084 static JITResolver *TheJITResolver;
Chris Lattner60d815a2004-11-20 23:57:07 +000085 public:
Chris Lattner05858a92007-02-24 02:57:03 +000086 JITResolver(JIT &jit) : nextGOTIndex(0) {
87 TheJIT = &jit;
88
89 LazyResolverFn = jit.getJITInfo().getLazyResolverFunction(JITCompilerFn);
90 assert(TheJITResolver == 0 && "Multiple JIT resolvers?");
91 TheJITResolver = this;
92 }
93
94 ~JITResolver() {
95 TheJITResolver = 0;
Chris Lattner65f66382004-11-21 03:37:42 +000096 }
Chris Lattner60d815a2004-11-20 23:57:07 +000097
98 /// getFunctionStub - This returns a pointer to a function stub, creating
99 /// one on demand as needed.
100 void *getFunctionStub(Function *F);
101
Chris Lattnered5189d2005-04-18 01:44:27 +0000102 /// getExternalFunctionStub - Return a stub for the function at the
103 /// specified address, created lazily on demand.
104 void *getExternalFunctionStub(void *FnAddr);
105
Chris Lattner65f66382004-11-21 03:37:42 +0000106 /// AddCallbackAtLocation - If the target is capable of rewriting an
107 /// instruction without the use of a stub, record the location of the use so
108 /// we know which function is being used at the location.
109 void *AddCallbackAtLocation(Function *F, void *Location) {
Reid Spencer79876f52005-07-12 15:51:55 +0000110 MutexGuard locked(TheJIT->lock);
Chris Lattner65f66382004-11-21 03:37:42 +0000111 /// Get the target-specific JIT resolver function.
Reid Spencer79876f52005-07-12 15:51:55 +0000112 state.getStubToFunctionMap(locked)[Location] = F;
Chris Lattner71819be2006-06-01 17:29:22 +0000113 return (void*)(intptr_t)LazyResolverFn;
Chris Lattner65f66382004-11-21 03:37:42 +0000114 }
115
Andrew Lenharth3444cf52005-07-28 12:44:13 +0000116 /// getGOTIndexForAddress - Return a new or existing index in the GOT for
Chris Lattner55d8c3f2007-12-05 23:39:57 +0000117 /// an address. This function only manages slots, it does not manage the
Andrew Lenharth3444cf52005-07-28 12:44:13 +0000118 /// contents of the slots or the memory associated with the GOT.
Chris Lattner55d8c3f2007-12-05 23:39:57 +0000119 unsigned getGOTIndexForAddr(void *addr);
Andrew Lenharth3444cf52005-07-28 12:44:13 +0000120
Chris Lattner60d815a2004-11-20 23:57:07 +0000121 /// JITCompilerFn - This function is called to resolve a stub to a compiled
122 /// address. If the LLVM Function corresponding to the stub has not yet
123 /// been compiled, this function compiles it first.
124 static void *JITCompilerFn(void *Stub);
125 };
126}
127
Chris Lattner05858a92007-02-24 02:57:03 +0000128JITResolver *JITResolver::TheJITResolver = 0;
Chris Lattner60d815a2004-11-20 23:57:07 +0000129
Evan Cheng93386e02006-07-27 06:33:55 +0000130#if (defined(__POWERPC__) || defined (__ppc__) || defined(_POWER)) && \
131 defined(__APPLE__)
132extern "C" void sys_icache_invalidate(const void *Addr, size_t len);
133#endif
134
135/// synchronizeICache - On some targets, the JIT emitted code must be
136/// explicitly refetched to ensure correct execution.
137static void synchronizeICache(const void *Addr, size_t len) {
138#if (defined(__POWERPC__) || defined (__ppc__) || defined(_POWER)) && \
139 defined(__APPLE__)
Jim Laskey8307be42006-07-27 13:40:34 +0000140 sys_icache_invalidate(Addr, len);
Evan Cheng93386e02006-07-27 06:33:55 +0000141#endif
142}
143
Chris Lattner60d815a2004-11-20 23:57:07 +0000144/// getFunctionStub - This returns a pointer to a function stub, creating
145/// one on demand as needed.
146void *JITResolver::getFunctionStub(Function *F) {
Reid Spencer79876f52005-07-12 15:51:55 +0000147 MutexGuard locked(TheJIT->lock);
148
Chris Lattner60d815a2004-11-20 23:57:07 +0000149 // If we already have a stub for this function, recycle it.
Reid Spencer79876f52005-07-12 15:51:55 +0000150 void *&Stub = state.getFunctionToStubMap(locked)[F];
Chris Lattner60d815a2004-11-20 23:57:07 +0000151 if (Stub) return Stub;
152
Chris Lattner50c2e112004-11-22 07:24:43 +0000153 // Call the lazy resolver function unless we already KNOW it is an external
154 // function, in which case we just skip the lazy resolution step.
Chris Lattner71819be2006-06-01 17:29:22 +0000155 void *Actual = (void*)(intptr_t)LazyResolverFn;
Gabor Greife16561c2007-07-05 17:07:56 +0000156 if (F->isDeclaration() && !F->hasNotBeenReadFromBitcode())
Chris Lattner50c2e112004-11-22 07:24:43 +0000157 Actual = TheJIT->getPointerToFunction(F);
Misha Brukman10468d82005-04-21 22:55:34 +0000158
Chris Lattner60d815a2004-11-20 23:57:07 +0000159 // Otherwise, codegen a new stub. For now, the stub will call the lazy
160 // resolver function.
Chris Lattner05858a92007-02-24 02:57:03 +0000161 Stub = TheJIT->getJITInfo().emitFunctionStub(Actual,
162 *TheJIT->getCodeEmitter());
Chris Lattner50c2e112004-11-22 07:24:43 +0000163
Chris Lattner71819be2006-06-01 17:29:22 +0000164 if (Actual != (void*)(intptr_t)LazyResolverFn) {
Chris Lattner50c2e112004-11-22 07:24:43 +0000165 // If we are getting the stub for an external function, we really want the
166 // address of the stub in the GlobalAddressMap for the JIT, not the address
167 // of the external function.
168 TheJIT->updateGlobalMapping(F, Stub);
169 }
Chris Lattner60d815a2004-11-20 23:57:07 +0000170
Evan Chengf6acb342006-07-25 20:40:54 +0000171 // Invalidate the icache if necessary.
Chris Lattner05858a92007-02-24 02:57:03 +0000172 synchronizeICache(Stub, TheJIT->getCodeEmitter()->getCurrentPCValue() -
173 (intptr_t)Stub);
Evan Chengf6acb342006-07-25 20:40:54 +0000174
Bill Wendling22e978a2006-12-07 20:04:42 +0000175 DOUT << "JIT: Stub emitted at [" << Stub << "] for function '"
176 << F->getName() << "'\n";
Chris Lattner9de8e222004-11-21 03:44:32 +0000177
Chris Lattner60d815a2004-11-20 23:57:07 +0000178 // Finally, keep track of the stub-to-Function mapping so that the
179 // JITCompilerFn knows which function to compile!
Reid Spencer79876f52005-07-12 15:51:55 +0000180 state.getStubToFunctionMap(locked)[Stub] = F;
Chris Lattner60d815a2004-11-20 23:57:07 +0000181 return Stub;
182}
183
Chris Lattnered5189d2005-04-18 01:44:27 +0000184/// getExternalFunctionStub - Return a stub for the function at the
185/// specified address, created lazily on demand.
186void *JITResolver::getExternalFunctionStub(void *FnAddr) {
187 // If we already have a stub for this function, recycle it.
188 void *&Stub = ExternalFnToStubMap[FnAddr];
189 if (Stub) return Stub;
190
Chris Lattner05858a92007-02-24 02:57:03 +0000191 Stub = TheJIT->getJITInfo().emitFunctionStub(FnAddr,
192 *TheJIT->getCodeEmitter());
Evan Chengf6acb342006-07-25 20:40:54 +0000193
194 // Invalidate the icache if necessary.
Chris Lattner05858a92007-02-24 02:57:03 +0000195 synchronizeICache(Stub, TheJIT->getCodeEmitter()->getCurrentPCValue() -
196 (intptr_t)Stub);
Evan Chengf6acb342006-07-25 20:40:54 +0000197
Bill Wendling22e978a2006-12-07 20:04:42 +0000198 DOUT << "JIT: Stub emitted at [" << Stub
199 << "] for external function at '" << FnAddr << "'\n";
Chris Lattnered5189d2005-04-18 01:44:27 +0000200 return Stub;
201}
202
Andrew Lenharth3444cf52005-07-28 12:44:13 +0000203unsigned JITResolver::getGOTIndexForAddr(void* addr) {
204 unsigned idx = revGOTMap[addr];
205 if (!idx) {
206 idx = ++nextGOTIndex;
207 revGOTMap[addr] = idx;
Bill Wendling22e978a2006-12-07 20:04:42 +0000208 DOUT << "Adding GOT entry " << idx
209 << " for addr " << addr << "\n";
Andrew Lenharth3444cf52005-07-28 12:44:13 +0000210 }
211 return idx;
212}
Chris Lattnered5189d2005-04-18 01:44:27 +0000213
Chris Lattner60d815a2004-11-20 23:57:07 +0000214/// JITCompilerFn - This function is called when a lazy compilation stub has
215/// been entered. It looks up which function this stub corresponds to, compiles
216/// it if necessary, then returns the resultant function pointer.
217void *JITResolver::JITCompilerFn(void *Stub) {
Chris Lattner05858a92007-02-24 02:57:03 +0000218 JITResolver &JR = *TheJITResolver;
Misha Brukman10468d82005-04-21 22:55:34 +0000219
Reid Spencer79876f52005-07-12 15:51:55 +0000220 MutexGuard locked(TheJIT->lock);
221
Chris Lattner60d815a2004-11-20 23:57:07 +0000222 // The address given to us for the stub may not be exactly right, it might be
223 // a little bit after the stub. As such, use upper_bound to find it.
224 std::map<void*, Function*>::iterator I =
Reid Spencer79876f52005-07-12 15:51:55 +0000225 JR.state.getStubToFunctionMap(locked).upper_bound(Stub);
Chris Lattner2e8b93a2006-01-07 06:20:51 +0000226 assert(I != JR.state.getStubToFunctionMap(locked).begin() &&
227 "This is not a known stub!");
Chris Lattner60d815a2004-11-20 23:57:07 +0000228 Function *F = (--I)->second;
229
Evan Chenga1a3cc12007-06-30 00:10:37 +0000230 // If we have already code generated the function, just return the address.
231 void *Result = TheJIT->getPointerToGlobalIfAvailable(F);
Chris Lattner5fdbe552006-11-09 19:32:13 +0000232
Evan Chenga1a3cc12007-06-30 00:10:37 +0000233 if (!Result) {
234 // Otherwise we don't have it, do lazy compilation now.
235
236 // If lazy compilation is disabled, emit a useful error message and abort.
237 if (TheJIT->isLazyCompilationDisabled()) {
238 cerr << "LLVM JIT requested to do lazy compilation of function '"
239 << F->getName() << "' when lazy compiles are disabled!\n";
240 abort();
241 }
242
243 // We might like to remove the stub from the StubToFunction map.
244 // We can't do that! Multiple threads could be stuck, waiting to acquire the
245 // lock above. As soon as the 1st function finishes compiling the function,
246 // the next one will be released, and needs to be able to find the function
247 // it needs to call.
248 //JR.state.getStubToFunctionMap(locked).erase(I);
Chris Lattner60d815a2004-11-20 23:57:07 +0000249
Evan Chenga1a3cc12007-06-30 00:10:37 +0000250 DOUT << "JIT: Lazily resolving function '" << F->getName()
251 << "' In stub ptr = " << Stub << " actual ptr = "
252 << I->first << "\n";
Chris Lattner60d815a2004-11-20 23:57:07 +0000253
Evan Chenga1a3cc12007-06-30 00:10:37 +0000254 Result = TheJIT->getPointerToFunction(F);
255 }
Chris Lattner60d815a2004-11-20 23:57:07 +0000256
257 // We don't need to reuse this stub in the future, as F is now compiled.
Reid Spencer79876f52005-07-12 15:51:55 +0000258 JR.state.getFunctionToStubMap(locked).erase(F);
Chris Lattner60d815a2004-11-20 23:57:07 +0000259
260 // FIXME: We could rewrite all references to this stub if we knew them.
Andrew Lenharth3444cf52005-07-28 12:44:13 +0000261
Jeff Cohen546fd592005-07-30 18:33:25 +0000262 // What we will do is set the compiled function address to map to the
263 // same GOT entry as the stub so that later clients may update the GOT
Andrew Lenharth3444cf52005-07-28 12:44:13 +0000264 // if they see it still using the stub address.
265 // Note: this is done so the Resolver doesn't have to manage GOT memory
266 // Do this without allocating map space if the target isn't using a GOT
267 if(JR.revGOTMap.find(Stub) != JR.revGOTMap.end())
268 JR.revGOTMap[Result] = JR.revGOTMap[Stub];
269
Chris Lattner60d815a2004-11-20 23:57:07 +0000270 return Result;
271}
Chris Lattnerf26be842003-08-14 18:35:27 +0000272
273
Chris Lattner60d815a2004-11-20 23:57:07 +0000274//===----------------------------------------------------------------------===//
Chris Lattnerc5753052004-11-22 22:00:25 +0000275// JITEmitter code.
Chris Lattner60d815a2004-11-20 23:57:07 +0000276//
Chris Lattnerf26be842003-08-14 18:35:27 +0000277namespace {
Chris Lattnerc5753052004-11-22 22:00:25 +0000278 /// JITEmitter - The JIT implementation of the MachineCodeEmitter, which is
279 /// used to output functions to memory for execution.
280 class JITEmitter : public MachineCodeEmitter {
Chris Lattner55d8c3f2007-12-05 23:39:57 +0000281 JITMemoryManager *MemMgr;
Chris Lattnerf26be842003-08-14 18:35:27 +0000282
Chris Lattnerfdbd98b2003-05-09 03:30:07 +0000283 // When outputting a function stub in the context of some other function, we
Chris Lattnerc9aa3712006-05-02 18:27:26 +0000284 // save BufferBegin/BufferEnd/CurBufferPtr here.
285 unsigned char *SavedBufferBegin, *SavedBufferEnd, *SavedCurBufferPtr;
Chris Lattner6b689e32003-06-01 23:24:36 +0000286
Chris Lattner6cf7a432004-11-20 03:46:14 +0000287 /// Relocations - These are the relocations that the function needs, as
288 /// emitted.
289 std::vector<MachineRelocation> Relocations;
Chris Lattner1d8ee1f2006-05-03 17:10:41 +0000290
291 /// MBBLocations - This vector is a mapping from MBB ID's to their address.
292 /// It is filled in by the StartMachineBasicBlock callback and queried by
293 /// the getMachineBasicBlockAddress callback.
294 std::vector<intptr_t> MBBLocations;
Andrew Lenharth940b07c2005-07-22 20:48:12 +0000295
Chris Lattner729ffe92006-02-09 04:49:59 +0000296 /// ConstantPool - The constant pool for the current function.
297 ///
298 MachineConstantPool *ConstantPool;
299
300 /// ConstantPoolBase - A pointer to the first entry in the constant pool.
301 ///
302 void *ConstantPoolBase;
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000303
Nate Begemana0d95a82006-09-10 23:03:44 +0000304 /// JumpTable - The jump tables for the current function.
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000305 ///
306 MachineJumpTableInfo *JumpTable;
307
308 /// JumpTableBase - A pointer to the first entry in the jump table.
309 ///
310 void *JumpTableBase;
Chris Lattner05858a92007-02-24 02:57:03 +0000311
312 /// Resolver - This contains info about the currently resolved functions.
313 JITResolver Resolver;
314 public:
Chris Lattnerdc351b92007-12-06 01:08:09 +0000315 JITEmitter(JIT &jit, JITMemoryManager *JMM) : Resolver(jit) {
316 MemMgr = JMM ? JMM : JITMemoryManager::CreateDefaultMemManager();
Chris Lattner55d8c3f2007-12-05 23:39:57 +0000317 if (jit.getJITInfo().needsGOT()) {
318 MemMgr->AllocateGOT();
319 DOUT << "JIT is managing a GOT\n";
320 }
321 }
322 ~JITEmitter() {
323 delete MemMgr;
Andrew Lenharth940b07c2005-07-22 20:48:12 +0000324 }
Chris Lattner05858a92007-02-24 02:57:03 +0000325
326 JITResolver &getJITResolver() { return Resolver; }
Chris Lattner996fe012002-12-24 00:01:05 +0000327
328 virtual void startFunction(MachineFunction &F);
Chris Lattnerc9aa3712006-05-02 18:27:26 +0000329 virtual bool finishFunction(MachineFunction &F);
Chris Lattnerb8065a92006-05-02 23:22:24 +0000330
331 void emitConstantPool(MachineConstantPool *MCP);
332 void initJumpTableInfo(MachineJumpTableInfo *MJTI);
Jim Laskey39589552006-12-14 22:53:42 +0000333 void emitJumpTableInfo(MachineJumpTableInfo *MJTI);
Chris Lattnerb8065a92006-05-02 23:22:24 +0000334
Evan Chenge03ca9b2006-11-16 20:04:54 +0000335 virtual void startFunctionStub(unsigned StubSize, unsigned Alignment = 1);
Chris Lattner60d815a2004-11-20 23:57:07 +0000336 virtual void* finishFunctionStub(const Function *F);
Chris Lattner6b689e32003-06-01 23:24:36 +0000337
Chris Lattner6cf7a432004-11-20 03:46:14 +0000338 virtual void addRelocation(const MachineRelocation &MR) {
339 Relocations.push_back(MR);
340 }
Chris Lattner1d8ee1f2006-05-03 17:10:41 +0000341
342 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) {
343 if (MBBLocations.size() <= (unsigned)MBB->getNumber())
344 MBBLocations.resize((MBB->getNumber()+1)*2);
345 MBBLocations[MBB->getNumber()] = getCurrentPCValue();
346 }
Chris Lattner6cf7a432004-11-20 03:46:14 +0000347
Chris Lattner1d8ee1f2006-05-03 17:10:41 +0000348 virtual intptr_t getConstantPoolEntryAddress(unsigned Entry) const;
349 virtual intptr_t getJumpTableEntryAddress(unsigned Entry) const;
350
351 virtual intptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
352 assert(MBBLocations.size() > (unsigned)MBB->getNumber() &&
353 MBBLocations[MBB->getNumber()] && "MBB not emitted!");
354 return MBBLocations[MBB->getNumber()];
355 }
356
Chris Lattner873ef132006-05-11 23:08:08 +0000357 /// deallocateMemForFunction - Deallocate all memory for the specified
358 /// function body.
359 void deallocateMemForFunction(Function *F) {
Chris Lattner55d8c3f2007-12-05 23:39:57 +0000360 MemMgr->deallocateMemForFunction(F);
Chris Lattner873ef132006-05-11 23:08:08 +0000361 }
Chris Lattner60d815a2004-11-20 23:57:07 +0000362 private:
Chris Lattner65f66382004-11-21 03:37:42 +0000363 void *getPointerToGlobal(GlobalValue *GV, void *Reference, bool NoNeedStub);
Chris Lattner996fe012002-12-24 00:01:05 +0000364 };
365}
366
Chris Lattnerc5753052004-11-22 22:00:25 +0000367void *JITEmitter::getPointerToGlobal(GlobalValue *V, void *Reference,
368 bool DoesntNeedStub) {
Chris Lattner60d815a2004-11-20 23:57:07 +0000369 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
370 /// FIXME: If we straightened things out, this could actually emit the
371 /// global immediately instead of queuing it for codegen later!
Chris Lattner60d815a2004-11-20 23:57:07 +0000372 return TheJIT->getOrEmitGlobalVariable(GV);
373 }
374
375 // If we have already compiled the function, return a pointer to its body.
376 Function *F = cast<Function>(V);
377 void *ResultPtr = TheJIT->getPointerToGlobalIfAvailable(F);
378 if (ResultPtr) return ResultPtr;
379
Gabor Greife16561c2007-07-05 17:07:56 +0000380 if (F->isDeclaration() && !F->hasNotBeenReadFromBitcode()) {
Chris Lattner60d815a2004-11-20 23:57:07 +0000381 // If this is an external function pointer, we can force the JIT to
382 // 'compile' it, which really just adds it to the map.
Chris Lattner50c2e112004-11-22 07:24:43 +0000383 if (DoesntNeedStub)
384 return TheJIT->getPointerToFunction(F);
385
Chris Lattner05858a92007-02-24 02:57:03 +0000386 return Resolver.getFunctionStub(F);
Chris Lattner60d815a2004-11-20 23:57:07 +0000387 }
388
Chris Lattner65f66382004-11-21 03:37:42 +0000389 // Okay, the function has not been compiled yet, if the target callback
390 // mechanism is capable of rewriting the instruction directly, prefer to do
391 // that instead of emitting a stub.
392 if (DoesntNeedStub)
Chris Lattner05858a92007-02-24 02:57:03 +0000393 return Resolver.AddCallbackAtLocation(F, Reference);
Chris Lattner65f66382004-11-21 03:37:42 +0000394
Chris Lattner60d815a2004-11-20 23:57:07 +0000395 // Otherwise, we have to emit a lazy resolving stub.
Chris Lattner05858a92007-02-24 02:57:03 +0000396 return Resolver.getFunctionStub(F);
Chris Lattner60d815a2004-11-20 23:57:07 +0000397}
398
Chris Lattnerc5753052004-11-22 22:00:25 +0000399void JITEmitter::startFunction(MachineFunction &F) {
Chris Lattner873ef132006-05-11 23:08:08 +0000400 uintptr_t ActualSize;
Chris Lattner55d8c3f2007-12-05 23:39:57 +0000401 BufferBegin = CurBufferPtr = MemMgr->startFunctionBody(F.getFunction(),
402 ActualSize);
Chris Lattner873ef132006-05-11 23:08:08 +0000403 BufferEnd = BufferBegin+ActualSize;
Chris Lattnerb8065a92006-05-02 23:22:24 +0000404
Evan Chenge03ca9b2006-11-16 20:04:54 +0000405 // Ensure the constant pool/jump table info is at least 4-byte aligned.
406 emitAlignment(16);
407
Chris Lattnerb8065a92006-05-02 23:22:24 +0000408 emitConstantPool(F.getConstantPool());
409 initJumpTableInfo(F.getJumpTableInfo());
410
411 // About to start emitting the machine code for the function.
Chris Lattner37c39b92006-05-03 01:03:20 +0000412 emitAlignment(std::max(F.getFunction()->getAlignment(), 8U));
Chris Lattnerb8065a92006-05-02 23:22:24 +0000413 TheJIT->updateGlobalMapping(F.getFunction(), CurBufferPtr);
Evan Chengf6acb342006-07-25 20:40:54 +0000414
Chris Lattner1d8ee1f2006-05-03 17:10:41 +0000415 MBBLocations.clear();
Chris Lattner996fe012002-12-24 00:01:05 +0000416}
417
Chris Lattnerc9aa3712006-05-02 18:27:26 +0000418bool JITEmitter::finishFunction(MachineFunction &F) {
Chris Lattner873ef132006-05-11 23:08:08 +0000419 if (CurBufferPtr == BufferEnd) {
420 // FIXME: Allocate more space, then try again.
Bill Wendling22e978a2006-12-07 20:04:42 +0000421 cerr << "JIT: Ran out of space for generated machine code!\n";
Chris Lattner873ef132006-05-11 23:08:08 +0000422 abort();
423 }
424
Jim Laskey39589552006-12-14 22:53:42 +0000425 emitJumpTableInfo(F.getJumpTableInfo());
Chris Lattner1d8ee1f2006-05-03 17:10:41 +0000426
Chris Lattnere7962662006-06-16 18:09:26 +0000427 // FnStart is the start of the text, not the start of the constant pool and
428 // other per-function data.
429 unsigned char *FnStart =
430 (unsigned char *)TheJIT->getPointerToGlobalIfAvailable(F.getFunction());
431 unsigned char *FnEnd = CurBufferPtr;
432
Chris Lattner55d8c3f2007-12-05 23:39:57 +0000433 MemMgr->endFunctionBody(F.getFunction(), BufferBegin, FnEnd);
Chris Lattnere7962662006-06-16 18:09:26 +0000434 NumBytes += FnEnd-FnStart;
Chris Lattner996fe012002-12-24 00:01:05 +0000435
Chris Lattner6cf7a432004-11-20 03:46:14 +0000436 if (!Relocations.empty()) {
Chris Lattner05732c82005-07-20 16:29:20 +0000437 NumRelos += Relocations.size();
438
Chris Lattner6cf7a432004-11-20 03:46:14 +0000439 // Resolve the relocations to concrete pointers.
440 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
441 MachineRelocation &MR = Relocations[i];
442 void *ResultPtr;
Chris Lattnered5189d2005-04-18 01:44:27 +0000443 if (MR.isString()) {
Chris Lattner6cf7a432004-11-20 03:46:14 +0000444 ResultPtr = TheJIT->getPointerToNamedFunction(MR.getString());
Misha Brukman10468d82005-04-21 22:55:34 +0000445
Chris Lattnered5189d2005-04-18 01:44:27 +0000446 // If the target REALLY wants a stub for this function, emit it now.
Evan Cheng563fcc32008-01-03 02:56:28 +0000447 if (!MR.doesntNeedStub())
Chris Lattner05858a92007-02-24 02:57:03 +0000448 ResultPtr = Resolver.getExternalFunctionStub(ResultPtr);
Chris Lattner005d7172006-05-03 18:55:56 +0000449 } else if (MR.isGlobalValue()) {
Chris Lattner65f66382004-11-21 03:37:42 +0000450 ResultPtr = getPointerToGlobal(MR.getGlobalValue(),
Chris Lattnerc9aa3712006-05-02 18:27:26 +0000451 BufferBegin+MR.getMachineCodeOffset(),
Evan Cheng563fcc32008-01-03 02:56:28 +0000452 MR.doesntNeedStub());
Evan Cheng78bf1072006-07-27 18:21:10 +0000453 } else if (MR.isBasicBlock()) {
454 ResultPtr = (void*)getMachineBasicBlockAddress(MR.getBasicBlock());
Jim Laskey70323a82006-12-14 19:17:33 +0000455 } else if (MR.isConstantPoolIndex()) {
Chris Lattner005d7172006-05-03 18:55:56 +0000456 ResultPtr=(void*)getConstantPoolEntryAddress(MR.getConstantPoolIndex());
Evan Cheng6a349392006-06-23 01:02:37 +0000457 } else {
458 assert(MR.isJumpTableIndex());
459 ResultPtr=(void*)getJumpTableEntryAddress(MR.getJumpTableIndex());
Chris Lattner005d7172006-05-03 18:55:56 +0000460 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000461
Chris Lattner6cf7a432004-11-20 03:46:14 +0000462 MR.setResultPointer(ResultPtr);
Andrew Lenharth940b07c2005-07-22 20:48:12 +0000463
Andrew Lenharth3444cf52005-07-28 12:44:13 +0000464 // if we are managing the GOT and the relocation wants an index,
465 // give it one
Chris Lattner55d8c3f2007-12-05 23:39:57 +0000466 if (MR.isGOTRelative() && MemMgr->isManagingGOT()) {
Chris Lattner05858a92007-02-24 02:57:03 +0000467 unsigned idx = Resolver.getGOTIndexForAddr(ResultPtr);
Andrew Lenharth3444cf52005-07-28 12:44:13 +0000468 MR.setGOTIndex(idx);
Chris Lattner55d8c3f2007-12-05 23:39:57 +0000469 if (((void**)MemMgr->getGOTBase())[idx] != ResultPtr) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000470 DOUT << "GOT was out of date for " << ResultPtr
Chris Lattner55d8c3f2007-12-05 23:39:57 +0000471 << " pointing at " << ((void**)MemMgr->getGOTBase())[idx]
Bill Wendling22e978a2006-12-07 20:04:42 +0000472 << "\n";
Chris Lattner55d8c3f2007-12-05 23:39:57 +0000473 ((void**)MemMgr->getGOTBase())[idx] = ResultPtr;
Andrew Lenharth3444cf52005-07-28 12:44:13 +0000474 }
Andrew Lenharth940b07c2005-07-22 20:48:12 +0000475 }
Chris Lattner6cf7a432004-11-20 03:46:14 +0000476 }
477
Chris Lattnerc9aa3712006-05-02 18:27:26 +0000478 TheJIT->getJITInfo().relocate(BufferBegin, &Relocations[0],
Chris Lattner55d8c3f2007-12-05 23:39:57 +0000479 Relocations.size(), MemMgr->getGOTBase());
Chris Lattner6cf7a432004-11-20 03:46:14 +0000480 }
481
Chris Lattner005d7172006-05-03 18:55:56 +0000482 // Update the GOT entry for F to point to the new code.
Chris Lattner55d8c3f2007-12-05 23:39:57 +0000483 if (MemMgr->isManagingGOT()) {
Chris Lattner05858a92007-02-24 02:57:03 +0000484 unsigned idx = Resolver.getGOTIndexForAddr((void*)BufferBegin);
Chris Lattner55d8c3f2007-12-05 23:39:57 +0000485 if (((void**)MemMgr->getGOTBase())[idx] != (void*)BufferBegin) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000486 DOUT << "GOT was out of date for " << (void*)BufferBegin
Chris Lattner55d8c3f2007-12-05 23:39:57 +0000487 << " pointing at " << ((void**)MemMgr->getGOTBase())[idx] << "\n";
488 ((void**)MemMgr->getGOTBase())[idx] = (void*)BufferBegin;
Andrew Lenharth3444cf52005-07-28 12:44:13 +0000489 }
490 }
491
Evan Chengf6acb342006-07-25 20:40:54 +0000492 // Invalidate the icache if necessary.
Evan Cheng93386e02006-07-27 06:33:55 +0000493 synchronizeICache(FnStart, FnEnd-FnStart);
Evan Chengf6acb342006-07-25 20:40:54 +0000494
Bill Wendling22e978a2006-12-07 20:04:42 +0000495 DOUT << "JIT: Finished CodeGen of [" << (void*)FnStart
496 << "] Function: " << F.getFunction()->getName()
497 << ": " << (FnEnd-FnStart) << " bytes of text, "
498 << Relocations.size() << " relocations\n";
Chris Lattner6cf7a432004-11-20 03:46:14 +0000499 Relocations.clear();
Anton Korobeynikov3e956972007-01-19 17:25:17 +0000500
Chris Lattner7476fa42007-01-20 20:51:43 +0000501#ifndef NDEBUG
Anton Korobeynikov666d23d2007-03-06 05:32:48 +0000502 if (sys::hasDisassembler())
503 DOUT << "Disassembled code:\n"
504 << sys::disassembleBuffer(FnStart, FnEnd-FnStart, (uintptr_t)FnStart);
Chris Lattner7476fa42007-01-20 20:51:43 +0000505#endif
Anton Korobeynikov3e956972007-01-19 17:25:17 +0000506
Chris Lattnerc9aa3712006-05-02 18:27:26 +0000507 return false;
Chris Lattner996fe012002-12-24 00:01:05 +0000508}
509
Chris Lattnerc5753052004-11-22 22:00:25 +0000510void JITEmitter::emitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerba972642006-02-09 04:22:52 +0000511 const std::vector<MachineConstantPoolEntry> &Constants = MCP->getConstants();
Chris Lattner98e72b42003-11-30 04:23:21 +0000512 if (Constants.empty()) return;
513
Evan Cheng3228e752006-09-12 20:59:59 +0000514 MachineConstantPoolEntry CPE = Constants.back();
515 unsigned Size = CPE.Offset;
516 const Type *Ty = CPE.isMachineConstantPoolEntry()
Chris Lattnercd347792006-09-13 16:21:10 +0000517 ? CPE.Val.MachineCPVal->getType() : CPE.Val.ConstVal->getType();
Duncan Sands44b87212007-11-01 20:53:16 +0000518 Size += TheJIT->getTargetData()->getABITypeSize(Ty);
Chris Lattner98e72b42003-11-30 04:23:21 +0000519
Chris Lattnerb8065a92006-05-02 23:22:24 +0000520 ConstantPoolBase = allocateSpace(Size, 1 << MCP->getConstantPoolAlignment());
Chris Lattner729ffe92006-02-09 04:49:59 +0000521 ConstantPool = MCP;
Chris Lattnerb8065a92006-05-02 23:22:24 +0000522
523 if (ConstantPoolBase == 0) return; // Buffer overflow.
524
Chris Lattner729ffe92006-02-09 04:49:59 +0000525 // Initialize the memory for all of the constant pool entries.
Chris Lattnerf6190822006-02-09 04:46:04 +0000526 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Chris Lattner729ffe92006-02-09 04:49:59 +0000527 void *CAddr = (char*)ConstantPoolBase+Constants[i].Offset;
Evan Cheng3228e752006-09-12 20:59:59 +0000528 if (Constants[i].isMachineConstantPoolEntry()) {
529 // FIXME: add support to lower machine constant pool values into bytes!
Bill Wendling22e978a2006-12-07 20:04:42 +0000530 cerr << "Initialize memory with machine specific constant pool entry"
531 << " has not been implemented!\n";
Evan Cheng3228e752006-09-12 20:59:59 +0000532 abort();
533 }
534 TheJIT->InitializeMemory(Constants[i].Val.ConstVal, CAddr);
Chris Lattner4ba3bbb2003-01-13 01:00:12 +0000535 }
536}
537
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000538void JITEmitter::initJumpTableInfo(MachineJumpTableInfo *MJTI) {
539 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
540 if (JT.empty()) return;
541
Chris Lattnerb8065a92006-05-02 23:22:24 +0000542 unsigned NumEntries = 0;
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000543 for (unsigned i = 0, e = JT.size(); i != e; ++i)
Chris Lattnerb8065a92006-05-02 23:22:24 +0000544 NumEntries += JT[i].MBBs.size();
545
546 unsigned EntrySize = MJTI->getEntrySize();
547
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000548 // Just allocate space for all the jump tables now. We will fix up the actual
549 // MBB entries in the tables after we emit the code for each block, since then
550 // we will know the final locations of the MBBs in memory.
551 JumpTable = MJTI;
Chris Lattnerb8065a92006-05-02 23:22:24 +0000552 JumpTableBase = allocateSpace(NumEntries * EntrySize, MJTI->getAlignment());
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000553}
554
Jim Laskey39589552006-12-14 22:53:42 +0000555void JITEmitter::emitJumpTableInfo(MachineJumpTableInfo *MJTI) {
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000556 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
Chris Lattnerb8065a92006-05-02 23:22:24 +0000557 if (JT.empty() || JumpTableBase == 0) return;
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000558
Jim Laskey39589552006-12-14 22:53:42 +0000559 if (TargetMachine::getRelocationModel() == Reloc::PIC_) {
Jim Laskey70323a82006-12-14 19:17:33 +0000560 assert(MJTI->getEntrySize() == 4 && "Cross JIT'ing?");
561 // For each jump table, place the offset from the beginning of the table
562 // to the target address.
563 int *SlotPtr = (int*)JumpTableBase;
Chris Lattner0574e472006-05-03 00:13:06 +0000564
Jim Laskey70323a82006-12-14 19:17:33 +0000565 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
566 const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs;
567 // Store the offset of the basic block for this jump table slot in the
568 // memory we allocated for the jump table in 'initJumpTableInfo'
569 intptr_t Base = (intptr_t)SlotPtr;
570 for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi)
571 *SlotPtr++ = (intptr_t)getMachineBasicBlockAddress(MBBs[mi]) - Base;
572 }
573 } else {
574 assert(MJTI->getEntrySize() == sizeof(void*) && "Cross JIT'ing?");
575
576 // For each jump table, map each target in the jump table to the address of
577 // an emitted MachineBasicBlock.
578 intptr_t *SlotPtr = (intptr_t*)JumpTableBase;
579
580 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
581 const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs;
582 // Store the address of the basic block for this jump table slot in the
583 // memory we allocated for the jump table in 'initJumpTableInfo'
584 for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi)
585 *SlotPtr++ = getMachineBasicBlockAddress(MBBs[mi]);
586 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000587 }
588}
589
Evan Chenge03ca9b2006-11-16 20:04:54 +0000590void JITEmitter::startFunctionStub(unsigned StubSize, unsigned Alignment) {
Chris Lattnerc9aa3712006-05-02 18:27:26 +0000591 SavedBufferBegin = BufferBegin;
592 SavedBufferEnd = BufferEnd;
593 SavedCurBufferPtr = CurBufferPtr;
594
Chris Lattner55d8c3f2007-12-05 23:39:57 +0000595 BufferBegin = CurBufferPtr = MemMgr->allocateStub(StubSize, Alignment);
Chris Lattnerc9aa3712006-05-02 18:27:26 +0000596 BufferEnd = BufferBegin+StubSize+1;
Chris Lattnerfdbd98b2003-05-09 03:30:07 +0000597}
598
Chris Lattnerc5753052004-11-22 22:00:25 +0000599void *JITEmitter::finishFunctionStub(const Function *F) {
Chris Lattnerc9aa3712006-05-02 18:27:26 +0000600 NumBytes += getCurrentPCOffset();
601 std::swap(SavedBufferBegin, BufferBegin);
602 BufferEnd = SavedBufferEnd;
603 CurBufferPtr = SavedCurBufferPtr;
604 return SavedBufferBegin;
Chris Lattner6b689e32003-06-01 23:24:36 +0000605}
606
Chris Lattner6b689e32003-06-01 23:24:36 +0000607// getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry
608// in the constant pool that was last emitted with the 'emitConstantPool'
609// method.
610//
Chris Lattner1d8ee1f2006-05-03 17:10:41 +0000611intptr_t JITEmitter::getConstantPoolEntryAddress(unsigned ConstantNum) const {
Chris Lattner729ffe92006-02-09 04:49:59 +0000612 assert(ConstantNum < ConstantPool->getConstants().size() &&
Misha Brukman5191b4b2005-04-22 04:08:30 +0000613 "Invalid ConstantPoolIndex!");
Chris Lattner729ffe92006-02-09 04:49:59 +0000614 return (intptr_t)ConstantPoolBase +
615 ConstantPool->getConstants()[ConstantNum].Offset;
Chris Lattner6b689e32003-06-01 23:24:36 +0000616}
617
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000618// getJumpTableEntryAddress - Return the address of the JumpTable with index
619// 'Index' in the jumpp table that was last initialized with 'initJumpTableInfo'
620//
Chris Lattner1d8ee1f2006-05-03 17:10:41 +0000621intptr_t JITEmitter::getJumpTableEntryAddress(unsigned Index) const {
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000622 const std::vector<MachineJumpTableEntry> &JT = JumpTable->getJumpTables();
623 assert(Index < JT.size() && "Invalid jump table index!");
624
625 unsigned Offset = 0;
626 unsigned EntrySize = JumpTable->getEntrySize();
627
628 for (unsigned i = 0; i < Index; ++i)
Jim Laskey70323a82006-12-14 19:17:33 +0000629 Offset += JT[i].MBBs.size();
630
631 Offset *= EntrySize;
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000632
Nate Begeman48ccd3f2006-04-25 17:46:32 +0000633 return (intptr_t)((char *)JumpTableBase + Offset);
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000634}
635
Chris Lattner873ef132006-05-11 23:08:08 +0000636//===----------------------------------------------------------------------===//
637// Public interface to this file
638//===----------------------------------------------------------------------===//
639
Chris Lattnerdc351b92007-12-06 01:08:09 +0000640MachineCodeEmitter *JIT::createEmitter(JIT &jit, JITMemoryManager *JMM) {
641 return new JITEmitter(jit, JMM);
Chris Lattner873ef132006-05-11 23:08:08 +0000642}
643
Misha Brukman057f4602003-07-28 19:09:06 +0000644// getPointerToNamedFunction - This function is used as a global wrapper to
Chris Lattner9bcae072003-12-20 01:46:27 +0000645// JIT::getPointerToNamedFunction for the purpose of resolving symbols when
Misha Brukman057f4602003-07-28 19:09:06 +0000646// bugpoint is debugging the JIT. In that scenario, we are loading an .so and
647// need to resolve function(s) that are being mis-codegenerated, so we need to
648// resolve their addresses at runtime, and this is the way to do it.
649extern "C" {
650 void *getPointerToNamedFunction(const char *Name) {
Chris Lattner0621cae2006-08-16 01:24:12 +0000651 if (Function *F = TheJIT->FindFunctionNamed(Name))
Chris Lattner9bcae072003-12-20 01:46:27 +0000652 return TheJIT->getPointerToFunction(F);
653 return TheJIT->getPointerToNamedFunction(Name);
Misha Brukman057f4602003-07-28 19:09:06 +0000654 }
655}
Chris Lattner873ef132006-05-11 23:08:08 +0000656
657// getPointerToFunctionOrStub - If the specified function has been
658// code-gen'd, return a pointer to the function. If not, compile it, or use
659// a stub to implement lazy compilation if available.
660//
661void *JIT::getPointerToFunctionOrStub(Function *F) {
662 // If we have already code generated the function, just return the address.
663 if (void *Addr = getPointerToGlobalIfAvailable(F))
664 return Addr;
665
Chris Lattner05858a92007-02-24 02:57:03 +0000666 // Get a stub if the target supports it.
667 assert(dynamic_cast<JITEmitter*>(MCE) && "Unexpected MCE?");
668 JITEmitter *JE = static_cast<JITEmitter*>(getCodeEmitter());
669 return JE->getJITResolver().getFunctionStub(F);
Chris Lattner873ef132006-05-11 23:08:08 +0000670}
671
672/// freeMachineCodeForFunction - release machine code memory for given Function.
673///
674void JIT::freeMachineCodeForFunction(Function *F) {
675 // Delete translation for this from the ExecutionEngine, so it will get
676 // retranslated next time it is used.
677 updateGlobalMapping(F, 0);
678
679 // Free the actual memory for the function body and related stuff.
680 assert(dynamic_cast<JITEmitter*>(MCE) && "Unexpected MCE?");
Chris Lattner05858a92007-02-24 02:57:03 +0000681 static_cast<JITEmitter*>(MCE)->deallocateMemForFunction(F);
Chris Lattner873ef132006-05-11 23:08:08 +0000682}
683