llvm.org GIT mirror llvm / 5788d1a
Fix MachineCodeEmitter to use uintptr_t instead of intptr_t. This avoids some overflow issues. Patch by Thomas Jablin. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@60828 91177308-0d34-0410-b5e6-96231b3b80d8 Evan Cheng 11 years ago
10 changed file(s) with 79 addition(s) and 67 deletion(s). Raw diff Collapse all Expand all
9797 /// written to the output stream in little-endian format.
9898 ///
9999 void emitWordLE(unsigned W) {
100 if (CurBufferPtr+4 <= BufferEnd) {
100 if (4 <= BufferEnd-CurBufferPtr) {
101101 *CurBufferPtr++ = (unsigned char)(W >> 0);
102102 *CurBufferPtr++ = (unsigned char)(W >> 8);
103103 *CurBufferPtr++ = (unsigned char)(W >> 16);
111111 /// written to the output stream in big-endian format.
112112 ///
113113 void emitWordBE(unsigned W) {
114 if (CurBufferPtr+4 <= BufferEnd) {
114 if (4 <= BufferEnd-CurBufferPtr) {
115115 *CurBufferPtr++ = (unsigned char)(W >> 24);
116116 *CurBufferPtr++ = (unsigned char)(W >> 16);
117117 *CurBufferPtr++ = (unsigned char)(W >> 8);
125125 /// written to the output stream in little-endian format.
126126 ///
127127 void emitDWordLE(uint64_t W) {
128 if (CurBufferPtr+8 <= BufferEnd) {
128 if (8 <= BufferEnd-CurBufferPtr) {
129129 *CurBufferPtr++ = (unsigned char)(W >> 0);
130130 *CurBufferPtr++ = (unsigned char)(W >> 8);
131131 *CurBufferPtr++ = (unsigned char)(W >> 16);
143143 /// written to the output stream in big-endian format.
144144 ///
145145 void emitDWordBE(uint64_t W) {
146 if (CurBufferPtr+8 <= BufferEnd) {
146 if (8 <= BufferEnd-CurBufferPtr) {
147147 *CurBufferPtr++ = (unsigned char)(W >> 56);
148148 *CurBufferPtr++ = (unsigned char)(W >> 48);
149149 *CurBufferPtr++ = (unsigned char)(W >> 40);
161161 /// alignment (saturated to BufferEnd of course).
162162 void emitAlignment(unsigned Alignment) {
163163 if (Alignment == 0) Alignment = 1;
164 // Move the current buffer ptr up to the specified alignment.
165 CurBufferPtr =
166 (unsigned char*)(((intptr_t)CurBufferPtr+Alignment-1) &
167 ~(intptr_t)(Alignment-1));
168 if (CurBufferPtr > BufferEnd)
169 CurBufferPtr = BufferEnd;
164
165 if(Alignment <= (uintptr_t)(BufferEnd-CurBufferPtr)) {
166 // Move the current buffer ptr up to the specified alignment.
167 CurBufferPtr =
168 (unsigned char*)(((uintptr_t)CurBufferPtr+Alignment-1) &
169 ~(uintptr_t)(Alignment-1));
170 } else {
171 CurBufferPtr = BufferEnd;
172 }
170173 }
171174
172175
209212
210213 /// emitInt32 - Emit a int32 directive.
211214 void emitInt32(int Value) {
212 if (CurBufferPtr+4 <= BufferEnd) {
215 if (4 <= BufferEnd-CurBufferPtr) {
213216 *((uint32_t*)CurBufferPtr) = Value;
214217 CurBufferPtr += 4;
215218 } else {
219222
220223 /// emitInt64 - Emit a int64 directive.
221224 void emitInt64(uint64_t Value) {
222 if (CurBufferPtr+8 <= BufferEnd) {
225 if (8 <= BufferEnd-CurBufferPtr) {
223226 *((uint64_t*)CurBufferPtr) = Value;
224227 CurBufferPtr += 8;
225228 } else {
246249 /// allocateSpace - Allocate a block of space in the current output buffer,
247250 /// returning null (and setting conditions to indicate buffer overflow) on
248251 /// failure. Alignment is the alignment in bytes of the buffer desired.
249 virtual void *allocateSpace(intptr_t Size, unsigned Alignment) {
252 virtual void *allocateSpace(uintptr_t Size, unsigned Alignment) {
250253 emitAlignment(Alignment);
251 void *Result = CurBufferPtr;
252
253 // Allocate the space.
254 CurBufferPtr += Size;
254 void *Result;
255255
256256 // Check for buffer overflow.
257 if (CurBufferPtr >= BufferEnd) {
257 if (Size >= (uintptr_t)(BufferEnd-CurBufferPtr)) {
258258 CurBufferPtr = BufferEnd;
259259 Result = 0;
260 }
260 } else {
261 // Allocate the space.
262 Result = CurBufferPtr;
263 CurBufferPtr += Size;
264 }
265
261266 return Result;
262267 }
263268
269274 /// getCurrentPCValue - This returns the address that the next emitted byte
270275 /// will be output to.
271276 ///
272 virtual intptr_t getCurrentPCValue() const {
273 return (intptr_t)CurBufferPtr;
277 virtual uintptr_t getCurrentPCValue() const {
278 return (uintptr_t)CurBufferPtr;
274279 }
275280
276281 /// getCurrentPCOffset - Return the offset from the start of the emitted
277282 /// buffer that we are currently writing to.
278 intptr_t getCurrentPCOffset() const {
283 uintptr_t getCurrentPCOffset() const {
279284 return CurBufferPtr-BufferBegin;
280285 }
281286
289294 /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in
290295 /// the constant pool that was last emitted with the emitConstantPool method.
291296 ///
292 virtual intptr_t getConstantPoolEntryAddress(unsigned Index) const = 0;
297 virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const = 0;
293298
294299 /// getJumpTableEntryAddress - Return the address of the jump table with index
295300 /// 'Index' in the function that last called initJumpTableInfo.
296301 ///
297 virtual intptr_t getJumpTableEntryAddress(unsigned Index) const = 0;
302 virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const = 0;
298303
299304 /// getMachineBasicBlockAddress - Return the address of the specified
300305 /// MachineBasicBlock, only usable after the label for the MBB has been
301306 /// emitted.
302307 ///
303 virtual intptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const= 0;
308 virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const= 0;
304309
305310 /// getLabelAddress - Return the address of the specified LabelID, only usable
306311 /// after the LabelID has been emitted.
307312 ///
308 virtual intptr_t getLabelAddress(uint64_t LabelID) const = 0;
313 virtual uintptr_t getLabelAddress(uint64_t LabelID) const = 0;
309314
310315 /// Specifies the MachineModuleInfo object. This is used for exception handling
311316 /// purposes.
4848
4949 /// Offset - This is the offset from the start of the code buffer of the
5050 /// relocation to perform.
51 intptr_t Offset;
51 uintptr_t Offset;
5252
5353 /// ConstantVal - A field that may be used by the target relocation type.
5454 intptr_t ConstantVal;
7878
7979 /// MachineRelocation::getGV - Return a relocation entry for a GlobalValue.
8080 ///
81 static MachineRelocation getGV(intptr_t offset, unsigned RelocationType,
81 static MachineRelocation getGV(uintptr_t offset, unsigned RelocationType,
8282 GlobalValue *GV, intptr_t cst = 0,
8383 bool NeedStub = 0,
8484 bool GOTrelative = 0) {
9797
9898 /// MachineRelocation::getIndirectSymbol - Return a relocation entry for an
9999 /// indirect symbol.
100 static MachineRelocation getIndirectSymbol(intptr_t offset,
100 static MachineRelocation getIndirectSymbol(uintptr_t offset,
101101 unsigned RelocationType,
102102 GlobalValue *GV, intptr_t cst = 0,
103103 bool NeedStub = 0,
117117
118118 /// MachineRelocation::getBB - Return a relocation entry for a BB.
119119 ///
120 static MachineRelocation getBB(intptr_t offset,unsigned RelocationType,
120 static MachineRelocation getBB(uintptr_t offset,unsigned RelocationType,
121121 MachineBasicBlock *MBB, intptr_t cst = 0) {
122122 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
123123 MachineRelocation Result;
135135 /// MachineRelocation::getExtSym - Return a relocation entry for an external
136136 /// symbol, like "free".
137137 ///
138 static MachineRelocation getExtSym(intptr_t offset, unsigned RelocationType,
138 static MachineRelocation getExtSym(uintptr_t offset, unsigned RelocationType,
139139 const char *ES, intptr_t cst = 0,
140140 bool GOTrelative = 0) {
141141 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
154154 /// MachineRelocation::getConstPool - Return a relocation entry for a constant
155155 /// pool entry.
156156 ///
157 static MachineRelocation getConstPool(intptr_t offset,unsigned RelocationType,
157 static MachineRelocation getConstPool(uintptr_t offset,unsigned RelocationType,
158158 unsigned CPI, intptr_t cst = 0,
159159 bool letTargetResolve = false) {
160160 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
173173 /// MachineRelocation::getJumpTable - Return a relocation entry for a jump
174174 /// table entry.
175175 ///
176 static MachineRelocation getJumpTable(intptr_t offset,unsigned RelocationType,
176 static MachineRelocation getJumpTable(uintptr_t offset,unsigned RelocationType,
177177 unsigned JTI, intptr_t cst = 0,
178178 bool letTargetResolve = false) {
179179 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
5959
6060 /// getPICJumpTableEntry - Returns the value of the jumptable entry for the
6161 /// specific basic block.
62 virtual intptr_t getPICJumpTableEntry(intptr_t BB, intptr_t JTBase) {
62 virtual uintptr_t getPICJumpTableEntry(uintptr_t BB, uintptr_t JTBase) {
6363 assert(0 && "This target doesn't implement getPICJumpTableEntry!");
6464 return 0;
6565 }
8484 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) {
8585 }
8686
87 virtual intptr_t getConstantPoolEntryAddress(unsigned Index) const {
87 virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const {
8888 assert(0 && "CP not implementated yet!");
8989 return 0;
9090 }
91 virtual intptr_t getJumpTableEntryAddress(unsigned Index) const {
91 virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const {
9292 assert(0 && "JT not implementated yet!");
9393 return 0;
9494 }
9595
96 virtual intptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
96 virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
9797 assert(0 && "JT not implementated yet!");
9898 return 0;
9999 }
100100
101 virtual intptr_t getLabelAddress(uint64_t Label) const {
101 virtual uintptr_t getLabelAddress(uint64_t Label) const {
102102 assert(0 && "Label address not implementated yet!");
103103 abort();
104104 return 0;
7474
7575 /// CPLocations - This is a map of constant pool indices to offsets from the
7676 /// start of the section for that constant pool index.
77 std::vector<intptr_t> CPLocations;
77 std::vector<uintptr_t> CPLocations;
7878
7979 /// CPSections - This is a map of constant pool indices to the MachOSection
8080 /// containing the constant pool entry for that index.
8282
8383 /// JTLocations - This is a map of jump table indices to offsets from the
8484 /// start of the section for that jump table index.
85 std::vector<intptr_t> JTLocations;
85 std::vector<uintptr_t> JTLocations;
8686
8787 /// MBBLocations - This vector is a mapping from MBB ID's to their address.
8888 /// It is filled in by the StartMachineBasicBlock callback and queried by
8989 /// the getMachineBasicBlockAddress callback.
90 std::vector<intptr_t> MBBLocations;
90 std::vector<uintptr_t> MBBLocations;
9191
9292 public:
9393 MachOCodeEmitter(MachOWriter &mow) : MOW(mow), TM(MOW.TM) {
105105 void emitConstantPool(MachineConstantPool *MCP);
106106 void emitJumpTables(MachineJumpTableInfo *MJTI);
107107
108 virtual intptr_t getConstantPoolEntryAddress(unsigned Index) const {
108 virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const {
109109 assert(CPLocations.size() > Index && "CP not emitted!");
110110 return CPLocations[Index];
111111 }
112 virtual intptr_t getJumpTableEntryAddress(unsigned Index) const {
112 virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const {
113113 assert(JTLocations.size() > Index && "JT not emitted!");
114114 return JTLocations[Index];
115115 }
120120 MBBLocations[MBB->getNumber()] = getCurrentPCOffset();
121121 }
122122
123 virtual intptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
123 virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
124124 assert(MBBLocations.size() > (unsigned)MBB->getNumber() &&
125125 MBBLocations[MBB->getNumber()] && "MBB not emitted!");
126126 return MBBLocations[MBB->getNumber()];
127127 }
128128
129 virtual intptr_t getLabelAddress(uint64_t Label) const {
129 virtual uintptr_t getLabelAddress(uint64_t Label) const {
130130 assert(0 && "get Label not implemented");
131131 abort();
132132 return 0;
466466 /// MBBLocations - This vector is a mapping from MBB ID's to their address.
467467 /// It is filled in by the StartMachineBasicBlock callback and queried by
468468 /// the getMachineBasicBlockAddress callback.
469 std::vector<intptr_t> MBBLocations;
469 std::vector<uintptr_t> MBBLocations;
470470
471471 /// ConstantPool - The constant pool for the current function.
472472 ///
492492
493493 /// LabelLocations - This vector is a mapping from Label ID's to their
494494 /// address.
495 std::vector<intptr_t> LabelLocations;
495 std::vector<uintptr_t> LabelLocations;
496496
497497 /// MMI - Machine module info for exception informations
498498 MachineModuleInfo* MMI;
536536
537537 /// allocateSpace - Reserves space in the current block if any, or
538538 /// allocate a new one of the given size.
539 virtual void *allocateSpace(intptr_t Size, unsigned Alignment);
539 virtual void *allocateSpace(uintptr_t Size, unsigned Alignment);
540540
541541 virtual void addRelocation(const MachineRelocation &MR) {
542542 Relocations.push_back(MR);
550550 << (void*) getCurrentPCValue() << "]\n";
551551 }
552552
553 virtual intptr_t getConstantPoolEntryAddress(unsigned Entry) const;
554 virtual intptr_t getJumpTableEntryAddress(unsigned Entry) const;
555
556 virtual intptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
553 virtual uintptr_t getConstantPoolEntryAddress(unsigned Entry) const;
554 virtual uintptr_t getJumpTableEntryAddress(unsigned Entry) const;
555
556 virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
557557 assert(MBBLocations.size() > (unsigned)MBB->getNumber() &&
558558 MBBLocations[MBB->getNumber()] && "MBB not emitted!");
559559 return MBBLocations[MBB->getNumber()];
571571 LabelLocations[LabelID] = getCurrentPCValue();
572572 }
573573
574 virtual intptr_t getLabelAddress(uint64_t LabelID) const {
574 virtual uintptr_t getLabelAddress(uint64_t LabelID) const {
575575 assert(LabelLocations.size() > (unsigned)LabelID &&
576576 LabelLocations[LabelID] && "Label not emitted!");
577577 return LabelLocations[LabelID];
962962 unsigned char *FnEnd = CurBufferPtr;
963963
964964 MemMgr->endFunctionBody(F.getFunction(), BufferBegin, FnEnd);
965
966 if (CurBufferPtr == BufferEnd) {
967 // FIXME: Allocate more space, then try again.
968 cerr << "JIT: Ran out of space for generated machine code!\n";
969 abort();
970 }
971
965972 BufferBegin = CurBufferPtr = 0;
966973 NumBytes += FnEnd-FnStart;
967974
10431050 return false;
10441051 }
10451052
1046 void* JITEmitter::allocateSpace(intptr_t Size, unsigned Alignment) {
1053 void* JITEmitter::allocateSpace(uintptr_t Size, unsigned Alignment) {
10471054 if (BufferBegin)
10481055 return MachineCodeEmitter::allocateSpace(Size, Alignment);
10491056
11281135 const std::vector &MBBs = JT[i].MBBs;
11291136 // Store the offset of the basic block for this jump table slot in the
11301137 // memory we allocated for the jump table in 'initJumpTableInfo'
1131 intptr_t Base = (intptr_t)SlotPtr;
1138 uintptr_t Base = (uintptr_t)SlotPtr;
11321139 for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) {
1133 intptr_t MBBAddr = getMachineBasicBlockAddress(MBBs[mi]);
1140 uintptr_t MBBAddr = getMachineBasicBlockAddress(MBBs[mi]);
11341141 *SlotPtr++ = TheJIT->getJITInfo().getPICJumpTableEntry(MBBAddr, Base);
11351142 }
11361143 }
11731180 // in the constant pool that was last emitted with the 'emitConstantPool'
11741181 // method.
11751182 //
1176 intptr_t JITEmitter::getConstantPoolEntryAddress(unsigned ConstantNum) const {
1183 uintptr_t JITEmitter::getConstantPoolEntryAddress(unsigned ConstantNum) const {
11771184 assert(ConstantNum < ConstantPool->getConstants().size() &&
11781185 "Invalid ConstantPoolIndex!");
1179 return (intptr_t)ConstantPoolBase +
1186 return (uintptr_t)ConstantPoolBase +
11801187 ConstantPool->getConstants()[ConstantNum].Offset;
11811188 }
11821189
11831190 // getJumpTableEntryAddress - Return the address of the JumpTable with index
11841191 // 'Index' in the jumpp table that was last initialized with 'initJumpTableInfo'
11851192 //
1186 intptr_t JITEmitter::getJumpTableEntryAddress(unsigned Index) const {
1193 uintptr_t JITEmitter::getJumpTableEntryAddress(unsigned Index) const {
11871194 const std::vector &JT = JumpTable->getJumpTables();
11881195 assert(Index < JT.size() && "Invalid jump table index!");
11891196
11951202
11961203 Offset *= EntrySize;
11971204
1198 return (intptr_t)((char *)JumpTableBase + Offset);
1205 return (uintptr_t)((char *)JumpTableBase + Offset);
11991206 }
12001207
12011208 //===----------------------------------------------------------------------===//
10771077
10781078 void ARMCodeEmitter::emitInlineJumpTable(unsigned JTIndex) {
10791079 // Remember the base address of the inline jump table.
1080 intptr_t JTBase = MCE.getCurrentPCValue();
1080 uintptr_t JTBase = MCE.getCurrentPCValue();
10811081 JTI->addJumpTableBaseAddr(JTIndex, JTBase);
10821082 DOUT << " ** Jump Table #" << JTIndex << " @ " << (void*)JTBase << '\n';
10831083
554554 MCE.emitByte(BaseOpcode);
555555 emitConstant(0, X86InstrInfo::sizeOfImm(Desc));
556556 // Remember PIC base.
557 PICBaseOffset = MCE.getCurrentPCOffset();
557 PICBaseOffset = (intptr_t) MCE.getCurrentPCOffset();
558558 X86JITInfo *JTI = TM.getJITInfo();
559559 JTI->setPICBase(MCE.getCurrentPCValue());
560560 break;
475475
476476 /// getPICJumpTableEntry - Returns the value of the jumptable entry for the
477477 /// specific basic block.
478 intptr_t X86JITInfo::getPICJumpTableEntry(intptr_t BB, intptr_t Entry) {
478 uintptr_t X86JITInfo::getPICJumpTableEntry(uintptr_t BB, uintptr_t Entry) {
479479 #if defined(X86_64_JIT)
480480 return BB - Entry;
481481 #else
2121
2222 class X86JITInfo : public TargetJITInfo {
2323 X86TargetMachine &TM;
24 intptr_t PICBase;
24 uintptr_t PICBase;
2525 char* TLSOffset;
2626 public:
2727 explicit X86JITInfo(X86TargetMachine &tm) : TM(tm) {
5050
5151 /// getPICJumpTableEntry - Returns the value of the jumptable entry for the
5252 /// specific basic block.
53 virtual intptr_t getPICJumpTableEntry(intptr_t BB, intptr_t JTBase);
53 virtual uintptr_t getPICJumpTableEntry(uintptr_t BB, uintptr_t JTBase);
5454
5555 /// getLazyResolverFunction - Expose the lazy resolver to the JIT.
5656 virtual LazyResolverFn getLazyResolverFunction(JITCompilerFn);
6868
6969 /// setPICBase / getPICBase - Getter / setter of PICBase, used to compute
7070 /// PIC jumptable entry.
71 void setPICBase(intptr_t Base) { PICBase = Base; }
72 intptr_t getPICBase() const { return PICBase; }
71 void setPICBase(uintptr_t Base) { PICBase = Base; }
72 uintptr_t getPICBase() const { return PICBase; }
7373 };
7474 }
7575