llvm.org GIT mirror llvm / 51cc3c1
Correlate stubs with functions in JIT: when emitting a stub, the JIT tells the memory manager which function the stub will resolve. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@49814 91177308-0d34-0410-b5e6-96231b3b80d8 Nicolas Geoffray 12 years ago
15 changed file(s) with 87 addition(s) and 60 deletion(s). Raw diff Collapse all Expand all
8181 /// have constant pools, the can only use the other emitByte*/emitWord*
8282 /// methods.
8383 ///
84 virtual void startFunctionStub(unsigned StubSize, unsigned Alignment = 1) = 0;
84 virtual void startFunctionStub(const GlobalValue* F, unsigned StubSize,
85 unsigned Alignment = 1) = 0;
8586
8687 /// finishFunctionStub - This callback is invoked to terminate a function
8788 /// stub.
8889 ///
89 virtual void *finishFunctionStub(const Function *F) = 0;
90 virtual void *finishFunctionStub(const GlobalValue* F) = 0;
9091
9192 /// emitByte - This callback is invoked when a byte needs to be written to the
9293 /// output stream.
7373 /// thunk for it. The stub should be "close" to the current function body,
7474 /// but should not be included in the 'actualsize' returned by
7575 /// startFunctionBody.
76 virtual unsigned char *allocateStub(unsigned StubSize, unsigned Alignment) =0;
76 virtual unsigned char *allocateStub(const GlobalValue* F, unsigned StubSize,
77 unsigned Alignment) =0;
7778
7879
7980 /// endFunctionBody - This method is called when the JIT is done codegen'ing
2222
2323 namespace llvm {
2424 class Function;
25 class GlobalValue;
2526 class MachineBasicBlock;
2627 class MachineCodeEmitter;
2728 class MachineRelocation;
4041 virtual void replaceMachineCodeForFunction(void *Old, void *New) = 0;
4142
4243 /// emitGlobalValueLazyPtr - Use the specified MachineCodeEmitter object to
43 /// emit a lazy pointer which contains the address of the specified GV.
44 virtual void *emitGlobalValueLazyPtr(void *GV, MachineCodeEmitter &MCE) {
44 /// emit a lazy pointer which contains the address of the specified ptr.
45 virtual void *emitGlobalValueLazyPtr(const GlobalValue* GV, void *ptr,
46 MachineCodeEmitter &MCE) {
4547 assert(0 && "This target doesn't implement emitGlobalValueLazyPtr!");
4648 return 0;
4749 }
4951 /// emitFunctionStub - Use the specified MachineCodeEmitter object to emit a
5052 /// small native function that simply calls the function at the specified
5153 /// address. Return the address of the resultant function.
52 virtual void *emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) {
54 virtual void *emitFunctionStub(const Function* F, void *Fn,
55 MachineCodeEmitter &MCE) {
5356 assert(0 && "This target doesn't implement emitFunctionStub!");
5457 return 0;
5558 }
113113
114114
115115 /// JIT SPECIFIC FUNCTIONS - DO NOT IMPLEMENT THESE HERE!
116 void startFunctionStub(unsigned StubSize, unsigned Alignment = 1) {
116 void startFunctionStub(const GlobalValue* F, unsigned StubSize,
117 unsigned Alignment = 1) {
117118 assert(0 && "JIT specific function called!");
118119 abort();
119120 }
120 void *finishFunctionStub(const Function *F) {
121 void *finishFunctionStub(const GlobalValue *F) {
121122 assert(0 && "JIT specific function called!");
122123 abort();
123124 return 0;
140140 virtual void setModuleInfo(llvm::MachineModuleInfo* MMI) { }
141141
142142 /// JIT SPECIFIC FUNCTIONS - DO NOT IMPLEMENT THESE HERE!
143 virtual void startFunctionStub(unsigned StubSize, unsigned Alignment = 1) {
143 virtual void startFunctionStub(const GlobalValue* F, unsigned StubSize,
144 unsigned Alignment = 1) {
144145 assert(0 && "JIT specific function called!");
145146 abort();
146147 }
147 virtual void *finishFunctionStub(const Function *F) {
148 virtual void *finishFunctionStub(const GlobalValue* F) {
148149 assert(0 && "JIT specific function called!");
149150 abort();
150151 return 0;
174174
175175 // Otherwise, codegen a new stub. For now, the stub will call the lazy
176176 // resolver function.
177 Stub = TheJIT->getJITInfo().emitFunctionStub(Actual,
177 Stub = TheJIT->getJITInfo().emitFunctionStub(F, Actual,
178178 *TheJIT->getCodeEmitter());
179179
180180 if (Actual != (void*)(intptr_t)LazyResolverFn) {
203203 if (LazyPtr) return LazyPtr;
204204
205205 // Otherwise, codegen a new lazy pointer.
206 LazyPtr = TheJIT->getJITInfo().emitGlobalValueLazyPtr(GVAddress,
206 LazyPtr = TheJIT->getJITInfo().emitGlobalValueLazyPtr(GV, GVAddress,
207207 *TheJIT->getCodeEmitter());
208208
209209 DOUT << "JIT: Stub emitted at [" << LazyPtr << "] for GV '"
219219 void *&Stub = ExternalFnToStubMap[FnAddr];
220220 if (Stub) return Stub;
221221
222 Stub = TheJIT->getJITInfo().emitFunctionStub(FnAddr,
222 Stub = TheJIT->getJITInfo().emitFunctionStub(0, FnAddr,
223223 *TheJIT->getCodeEmitter());
224224
225225 DOUT << "JIT: Stub emitted at [" << Stub
502502 void initJumpTableInfo(MachineJumpTableInfo *MJTI);
503503 void emitJumpTableInfo(MachineJumpTableInfo *MJTI);
504504
505 virtual void startFunctionStub(unsigned StubSize, unsigned Alignment = 1);
506 virtual void* finishFunctionStub(const Function *F);
505 virtual void startFunctionStub(const GlobalValue* F, unsigned StubSize,
506 unsigned Alignment = 1);
507 virtual void* finishFunctionStub(const GlobalValue *F);
507508
508509 virtual void addRelocation(const MachineRelocation &MR) {
509510 Relocations.push_back(MR);
821822 }
822823 }
823824
824 void JITEmitter::startFunctionStub(unsigned StubSize, unsigned Alignment) {
825 void JITEmitter::startFunctionStub(const GlobalValue* F, unsigned StubSize,
826 unsigned Alignment) {
825827 SavedBufferBegin = BufferBegin;
826828 SavedBufferEnd = BufferEnd;
827829 SavedCurBufferPtr = CurBufferPtr;
828830
829 BufferBegin = CurBufferPtr = MemMgr->allocateStub(StubSize, Alignment);
831 BufferBegin = CurBufferPtr = MemMgr->allocateStub(F, StubSize, Alignment);
830832 BufferEnd = BufferBegin+StubSize+1;
831833 }
832834
833 void *JITEmitter::finishFunctionStub(const Function *F) {
835 void *JITEmitter::finishFunctionStub(const GlobalValue* F) {
834836 NumBytes += getCurrentPCOffset();
835837 std::swap(SavedBufferBegin, BufferBegin);
836838 BufferEnd = SavedBufferEnd;
1010 //
1111 //===----------------------------------------------------------------------===//
1212
13 #include "llvm/GlobalValue.h"
1314 #include "llvm/ExecutionEngine/JITMemoryManager.h"
1415 #include "llvm/Support/Compiler.h"
1516 #include "llvm/System/Memory.h"
264265
265266 void AllocateGOT();
266267
267 unsigned char *allocateStub(unsigned StubSize, unsigned Alignment);
268 unsigned char *allocateStub(const GlobalValue* F, unsigned StubSize,
269 unsigned Alignment);
268270
269271 /// startFunctionBody - When a function starts, allocate a block of free
270272 /// executable memory, returning a pointer to it and its actual size.
437439 Blocks.clear();
438440 }
439441
440 unsigned char *DefaultJITMemoryManager::allocateStub(unsigned StubSize,
442 unsigned char *DefaultJITMemoryManager::allocateStub(const GlobalValue* F,
443 unsigned StubSize,
441444 unsigned Alignment) {
442445 CurStubPtr -= StubSize;
443446 CurStubPtr = (unsigned char*)(((intptr_t)CurStubPtr) &
1414 #include "ARMJITInfo.h"
1515 #include "ARMRelocations.h"
1616 #include "ARMSubtarget.h"
17 #include "llvm/Function.h"
1718 #include "llvm/CodeGen/MachineCodeEmitter.h"
1819 #include "llvm/Config/alloca.h"
1920 #include
9293 return ARMCompilationCallback;
9394 }
9495
95 void *ARMJITInfo::emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) {
96 void *ARMJITInfo::emitFunctionStub(const Function* F, void *Fn,
97 MachineCodeEmitter &MCE) {
9698 unsigned addr = (intptr_t)Fn;
9799 // If this is just a call to an external function, emit a branch instead of a
98100 // call. The code is the same except for one bit of the last instruction.
99101 if (Fn != (void*)(intptr_t)ARMCompilationCallback) {
100102 // branch to the corresponding function addr
101103 // the stub is 8-byte size and 4-aligned
102 MCE.startFunctionStub(8, 4);
104 MCE.startFunctionStub(F, 8, 4);
103105 MCE.emitWordLE(0xE51FF004); // LDR PC, [PC,#-4]
104106 MCE.emitWordLE(addr); // addr of function
105107 } else {
106108 // branch and link to the corresponding function addr
107109 // the stub is 20-byte size and 4-aligned
108 MCE.startFunctionStub(20, 4);
110 MCE.startFunctionStub(F, 20, 4);
109111 MCE.emitWordLE(0xE92D4800); // STMFD SP!, [R11, LR]
110112 MCE.emitWordLE(0xE28FE004); // ADD LR, PC, #4
111113 MCE.emitWordLE(0xE51FF004); // LDR PC, [PC,#-4]
113115 MCE.emitWordLE(0xE8BD8800); // LDMFD SP!, [R11, PC]
114116 }
115117
116 return MCE.finishFunctionStub(0);
118 return MCE.finishFunctionStub(F);
117119 }
118120
119121 /// relocate - Before the JIT can run a block of code that has been emitted,
3333 /// emitFunctionStub - Use the specified MachineCodeEmitter object to emit a
3434 /// small native function that simply calls the function at the specified
3535 /// address.
36 virtual void *emitFunctionStub(void *Fn, MachineCodeEmitter &MCE);
36 virtual void *emitFunctionStub(const Function* F, void *Fn,
37 MachineCodeEmitter &MCE);
3738
3839 /// getLazyResolverFunction - Expose the lazy resolver to the JIT.
3940 virtual LazyResolverFn getLazyResolverFunction(JITCompilerFn);
1313 #define DEBUG_TYPE "jit"
1414 #include "AlphaJITInfo.h"
1515 #include "AlphaRelocations.h"
16 #include "llvm/Function.h"
1617 #include "llvm/CodeGen/MachineCodeEmitter.h"
1718 #include "llvm/Config/alloca.h"
1819 #include "llvm/Support/Debug.h"
189190 #endif
190191 }
191192
192 void *AlphaJITInfo::emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) {
193 void *AlphaJITInfo::emitFunctionStub(const Function* F, void *Fn,
194 MachineCodeEmitter &MCE) {
193195 //assert(Fn == AlphaCompilationCallback && "Where are you going?\n");
194196 //Do things in a stupid slow way!
195 MCE.startFunctionStub(19*4);
197 MCE.startFunctionStub(F, 19*4);
196198 void* Addr = (void*)(intptr_t)MCE.getCurrentPCValue();
197199 for (int x = 0; x < 19; ++ x)
198200 MCE.emitWordLE(0);
199201 EmitBranchToAt(Addr, Fn);
200202 DOUT << "Emitting Stub to " << Fn << " at [" << Addr << "]\n";
201 return MCE.finishFunctionStub(0);
203 return MCE.finishFunctionStub(F);
202204 }
203205
204206 TargetJITInfo::LazyResolverFn
2828 explicit AlphaJITInfo(TargetMachine &tm) : TM(tm)
2929 { useGOT = true; }
3030
31 virtual void *emitFunctionStub(void *Fn, MachineCodeEmitter &MCE);
31 virtual void *emitFunctionStub(const Function* F, void *Fn,
32 MachineCodeEmitter &MCE);
3233 virtual LazyResolverFn getLazyResolverFunction(JITCompilerFn);
3334 virtual void relocate(void *Function, MachineRelocation *MR,
3435 unsigned NumRelocs, unsigned char* GOTBase);
1414 #include "PPCJITInfo.h"
1515 #include "PPCRelocations.h"
1616 #include "PPCTargetMachine.h"
17 #include "llvm/Function.h"
1718 #include "llvm/CodeGen/MachineCodeEmitter.h"
1819 #include "llvm/Config/alloca.h"
1920 #include "llvm/Support/Debug.h"
337338 #endif
338339 }
339340
340 void *PPCJITInfo::emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) {
341 void *PPCJITInfo::emitFunctionStub(const Function* F, void *Fn,
342 MachineCodeEmitter &MCE) {
341343 // If this is just a call to an external function, emit a branch instead of a
342344 // call. The code is the same except for one bit of the last instruction.
343345 if (Fn != (void*)(intptr_t)PPC32CompilationCallback &&
344346 Fn != (void*)(intptr_t)PPC64CompilationCallback) {
345 MCE.startFunctionStub(7*4);
347 MCE.startFunctionStub(F, 7*4);
346348 intptr_t Addr = (intptr_t)MCE.getCurrentPCValue();
347349 MCE.emitWordBE(0);
348350 MCE.emitWordBE(0);
353355 MCE.emitWordBE(0);
354356 EmitBranchToAt(Addr, (intptr_t)Fn, false, is64Bit);
355357 SyncICache((void*)Addr, 7*4);
356 return MCE.finishFunctionStub(0);
357 }
358
359 MCE.startFunctionStub(10*4);
358 return MCE.finishFunctionStub(F);
359 }
360
361 MCE.startFunctionStub(F, 10*4);
360362 intptr_t Addr = (intptr_t)MCE.getCurrentPCValue();
361363 if (is64Bit) {
362364 MCE.emitWordBE(0xf821ffb1); // stdu r1,-80(r1)
381383 MCE.emitWordBE(0);
382384 EmitBranchToAt(BranchAddr, (intptr_t)Fn, true, is64Bit);
383385 SyncICache((void*)Addr, 10*4);
384 return MCE.finishFunctionStub(0);
386 return MCE.finishFunctionStub(F);
385387 }
386388
387389
2828 is64Bit = tmIs64Bit;
2929 }
3030
31 virtual void *emitFunctionStub(void *Fn, MachineCodeEmitter &MCE);
31 virtual void *emitFunctionStub(const Function* F, void *Fn,
32 MachineCodeEmitter &MCE);
3233 virtual LazyResolverFn getLazyResolverFunction(JITCompilerFn);
3334 virtual void relocate(void *Function, MachineRelocation *MR,
3435 unsigned NumRelocs, unsigned char* GOTBase);
1414 #include "X86JITInfo.h"
1515 #include "X86Relocations.h"
1616 #include "X86Subtarget.h"
17 #include "llvm/Function.h"
1718 #include "llvm/CodeGen/MachineCodeEmitter.h"
1819 #include "llvm/Config/alloca.h"
1920 #include
390391 return X86CompilationCallback;
391392 }
392393
393 void *X86JITInfo::emitGlobalValueLazyPtr(void *GV, MachineCodeEmitter &MCE) {
394 void *X86JITInfo::emitGlobalValueLazyPtr(const GlobalValue* GV, void *ptr,
395 MachineCodeEmitter &MCE) {
394396 #if defined (X86_64_JIT)
395397 MCE.startFunctionStub(8, 8);
396 MCE.emitWordLE(((unsigned *)&GV)[0]);
397 MCE.emitWordLE(((unsigned *)&GV)[1]);
398 #else
399 MCE.startFunctionStub(4, 4);
400 MCE.emitWordLE((intptr_t)GV);
401 #endif
402 return MCE.finishFunctionStub(0);
403 }
404
405 void *X86JITInfo::emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) {
398 MCE.emitWordLE(((unsigned *)&ptr)[0]);
399 MCE.emitWordLE(((unsigned *)&ptr)[1]);
400 #else
401 MCE.startFunctionStub(GV, 4, 4);
402 MCE.emitWordLE((intptr_t)ptr);
403 #endif
404 return MCE.finishFunctionStub(GV);
405 }
406
407 void *X86JITInfo::emitFunctionStub(const Function* F, void *Fn,
408 MachineCodeEmitter &MCE) {
406409 // Note, we cast to intptr_t here to silence a -pedantic warning that
407410 // complains about casting a function pointer to a normal pointer.
408411 #if defined (X86_32_JIT) && !defined (_MSC_VER)
413416 #endif
414417 if (NotCC) {
415418 #if defined (X86_64_JIT)
416 MCE.startFunctionStub(13, 4);
419 MCE.startFunctionStub(F, 13, 4);
417420 MCE.emitByte(0x49); // REX prefix
418421 MCE.emitByte(0xB8+2); // movabsq r10
419422 MCE.emitWordLE(((unsigned *)&Fn)[0]);
422425 MCE.emitByte(0xFF); // jmpq *r10
423426 MCE.emitByte(2 | (4 << 3) | (3 << 6));
424427 #else
425 MCE.startFunctionStub(5, 4);
428 MCE.startFunctionStub(F, 5, 4);
426429 MCE.emitByte(0xE9);
427430 MCE.emitWordLE((intptr_t)Fn-MCE.getCurrentPCValue()-4);
428431 #endif
429 return MCE.finishFunctionStub(0);
430 }
431
432 #if defined (X86_64_JIT)
433 MCE.startFunctionStub(14, 4);
432 return MCE.finishFunctionStub(F);
433 }
434
435 #if defined (X86_64_JIT)
436 MCE.startFunctionStub(F, 14, 4);
434437 MCE.emitByte(0x49); // REX prefix
435438 MCE.emitByte(0xB8+2); // movabsq r10
436439 MCE.emitWordLE(((unsigned *)&Fn)[0]);
439442 MCE.emitByte(0xFF); // callq *r10
440443 MCE.emitByte(2 | (2 << 3) | (3 << 6));
441444 #else
442 MCE.startFunctionStub(6, 4);
445 MCE.startFunctionStub(F, 6, 4);
443446 MCE.emitByte(0xE8); // Call with 32 bit pc-rel destination...
444447
445448 MCE.emitWordLE((intptr_t)Fn-MCE.getCurrentPCValue()-4);
446449 #endif
447450
448451 MCE.emitByte(0xCD); // Interrupt - Just a marker identifying the stub!
449 return MCE.finishFunctionStub(0);
452 return MCE.finishFunctionStub(F);
450453 }
451454
452455 /// getPICJumpTableEntry - Returns the value of the jumptable entry for the
1313 #ifndef X86JITINFO_H
1414 #define X86JITINFO_H
1515
16 #include "llvm/Function.h"
1617 #include "llvm/Target/TargetJITInfo.h"
1718
1819 namespace llvm {
3233 virtual void replaceMachineCodeForFunction(void *Old, void *New);
3334
3435 /// emitGlobalValueLazyPtr - Use the specified MachineCodeEmitter object to
35 /// emit a lazy pointer which contains the address of the specified GV.
36 virtual void *emitGlobalValueLazyPtr(void *GV, MachineCodeEmitter &MCE);
36 /// emit a lazy pointer which contains the address of the specified ptr.
37 virtual void *emitGlobalValueLazyPtr(const GlobalValue* GV, void *ptr,
38 MachineCodeEmitter &MCE);
3739
3840 /// emitFunctionStub - Use the specified MachineCodeEmitter object to emit a
3941 /// small native function that simply calls the function at the specified
4042 /// address.
41 virtual void *emitFunctionStub(void *Fn, MachineCodeEmitter &MCE);
43 virtual void *emitFunctionStub(const Function* F, void *Fn,
44 MachineCodeEmitter &MCE);
4245
4346 /// getPICJumpTableEntry - Returns the value of the jumptable entry for the
4447 /// specific basic block.