llvm.org GIT mirror llvm / 2a3e08b
X86 JIT PIC jumptable support. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@45616 91177308-0d34-0410-b5e6-96231b3b80d8 Evan Cheng 12 years ago
6 changed file(s) with 44 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
5757 /// all code emission requests will be ignored (this is the buffer overflow
5858 /// condition).
5959 unsigned char *CurBufferPtr;
60
6061 public:
6162 virtual ~MachineCodeEmitter() {}
6263
5454 return 0;
5555 }
5656
57 /// getPICJumpTableEntry - Returns the value of the jumptable entry for the
58 /// specific basic block.
59 virtual intptr_t getPICJumpTableEntry(intptr_t BB, intptr_t JTBase) {
60 assert(0 && "This target doesn't implement getPICJumpTableEntry!");
61 return 0;
62 }
63
5764 /// LazyResolverFn - This typedef is used to represent the function that
5865 /// unresolved call points should invoke. This is a target specific
5966 /// function that knows how to walk the stack and find out which stub the
340340 /// JumpTableBase - A pointer to the first entry in the jump table.
341341 ///
342342 void *JumpTableBase;
343
343
344344 /// Resolver - This contains info about the currently resolved functions.
345345 JITResolver Resolver;
346346 public:
379379
380380 virtual intptr_t getConstantPoolEntryAddress(unsigned Entry) const;
381381 virtual intptr_t getJumpTableEntryAddress(unsigned Entry) const;
382
382
383383 virtual intptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
384384 assert(MBBLocations.size() > (unsigned)MBB->getNumber() &&
385385 MBBLocations[MBB->getNumber()] && "MBB not emitted!");
615615 // Store the offset of the basic block for this jump table slot in the
616616 // memory we allocated for the jump table in 'initJumpTableInfo'
617617 intptr_t Base = (intptr_t)SlotPtr;
618 for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi)
619 *SlotPtr++ = (intptr_t)getMachineBasicBlockAddress(MBBs[mi]) - Base;
618 for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) {
619 intptr_t MBBAddr = getMachineBasicBlockAddress(MBBs[mi]);
620 *SlotPtr++ = TheJIT->getJITInfo().getPICJumpTableEntry(MBBAddr, Base);
621 }
620622 }
621623 } else {
622624 assert(MJTI->getEntrySize() == sizeof(void*) && "Cross JIT'ing?");
1313
1414 #define DEBUG_TYPE "x86-emitter"
1515 #include "X86InstrInfo.h"
16 #include "X86JITInfo.h"
1617 #include "X86Subtarget.h"
1718 #include "X86TargetMachine.h"
1819 #include "X86Relocations.h"
3637 const TargetData *TD;
3738 TargetMachine &TM;
3839 MachineCodeEmitter &MCE;
39 intptr_t PICBase;
40 intptr_t PICBaseOffset;
4041 bool Is64BitMode;
4142 bool IsPIC;
4243 public:
4344 static char ID;
4445 explicit Emitter(TargetMachine &tm, MachineCodeEmitter &mce)
4546 : MachineFunctionPass((intptr_t)&ID), II(0), TD(0), TM(tm),
46 MCE(mce), PICBase(0), Is64BitMode(false),
47 MCE(mce), PICBaseOffset(0), Is64BitMode(false),
4748 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
4849 Emitter(TargetMachine &tm, MachineCodeEmitter &mce,
4950 const X86InstrInfo &ii, const TargetData &td, bool is64)
5051 : MachineFunctionPass((intptr_t)&ID), II(&ii), TD(&td), TM(tm),
51 MCE(mce), PICBase(0), Is64BitMode(is64),
52 MCE(mce), PICBaseOffset(0), Is64BitMode(is64),
5253 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
5354
5455 bool runOnMachineFunction(MachineFunction &MF);
147148 bool isLazy /* = false */) {
148149 intptr_t RelocCST = 0;
149150 if (Reloc == X86::reloc_picrel_word)
150 RelocCST = PICBase;
151 RelocCST = PICBaseOffset;
151152 else if (Reloc == X86::reloc_pcrel_word)
152153 RelocCST = PCAdj;
153154 MachineRelocation MR = isLazy
165166 /// be emitted to the current location in the function, and allow it to be PC
166167 /// relative.
167168 void Emitter::emitExternalSymbolAddress(const char *ES, unsigned Reloc) {
168 intptr_t RelocCST = (Reloc == X86::reloc_picrel_word) ? PICBase : 0;
169 intptr_t RelocCST = (Reloc == X86::reloc_picrel_word) ? PICBaseOffset : 0;
169170 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
170171 Reloc, ES, RelocCST));
171172 if (Reloc == X86::reloc_absolute_dword)
181182 intptr_t PCAdj /* = 0 */) {
182183 intptr_t RelocCST = 0;
183184 if (Reloc == X86::reloc_picrel_word)
184 RelocCST = PICBase;
185 RelocCST = PICBaseOffset;
185186 else if (Reloc == X86::reloc_pcrel_word)
186187 RelocCST = PCAdj;
187188 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
198199 intptr_t PCAdj /* = 0 */) {
199200 intptr_t RelocCST = 0;
200201 if (Reloc == X86::reloc_picrel_word)
201 RelocCST = PICBase;
202 RelocCST = PICBaseOffset;
202203 else if (Reloc == X86::reloc_pcrel_word)
203204 RelocCST = PCAdj;
204205 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
614615 case X86::FP_REG_KILL:
615616 break;
616617 #endif
617 case X86::MOVPC32r:
618 case X86::MOVPC32r: {
618619 // This emits the "call" portion of this pseudo instruction.
619620 MCE.emitByte(BaseOpcode);
620621 emitConstant(0, sizeOfImm(Desc));
621 PICBase = MCE.getCurrentPCOffset();
622 // Remember PIC base.
623 PICBaseOffset = MCE.getCurrentPCOffset();
624 X86JITInfo *JTI = dynamic_cast(TM.getJITInfo());
625 JTI->setPICBase(MCE.getCurrentPCValue());
622626 break;
627 }
623628 }
624629 CurOp = NumOps;
625630 break;
439439 return MCE.finishFunctionStub(0);
440440 }
441441
442 /// getPICJumpTableEntry - Returns the value of the jumptable entry for the
443 /// specific basic block.
444 intptr_t X86JITInfo::getPICJumpTableEntry(intptr_t BB, intptr_t Entry) {
445 return BB - PICBase;
446 }
447
442448 /// relocate - Before the JIT can run a block of code that has been emitted,
443449 /// it must rewrite the code to contain the actual addresses of any
444450 /// referenced global symbols.
2020
2121 class X86JITInfo : public TargetJITInfo {
2222 X86TargetMachine &TM;
23 intptr_t PICBase;
2324 public:
2425 X86JITInfo(X86TargetMachine &tm) : TM(tm) {useGOT = 0;}
2526
3940 /// address.
4041 virtual void *emitFunctionStub(void *Fn, MachineCodeEmitter &MCE);
4142
43 /// getPICJumpTableEntry - Returns the value of the jumptable entry for the
44 /// specific basic block.
45 virtual intptr_t getPICJumpTableEntry(intptr_t BB, intptr_t JTBase);
46
4247 /// getLazyResolverFunction - Expose the lazy resolver to the JIT.
4348 virtual LazyResolverFn getLazyResolverFunction(JITCompilerFn);
4449
4752 /// referenced global symbols.
4853 virtual void relocate(void *Function, MachineRelocation *MR,
4954 unsigned NumRelocs, unsigned char* GOTBase);
55
56 /// setPICBase / getPICBase - Getter / setter of PICBase, used to compute
57 /// PIC jumptable entry.
58 void setPICBase(intptr_t Base) { PICBase = Base; }
59 intptr_t getPICBase() const { return PICBase; }
5060 };
5161 }
5262