LLVM API Documentation
00001 //===-- llvm/CodeGen/BinaryObject.h - Binary Object. -----------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines a Binary Object Aka. "blob" for holding data from code 00011 // generators, ready for data to the object module code writters. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_CODEGEN_BINARYOBJECT_H 00016 #define LLVM_CODEGEN_BINARYOBJECT_H 00017 00018 #include "llvm/Support/DataTypes.h" 00019 00020 #include <string> 00021 #include <vector> 00022 00023 namespace llvm { 00024 00025 class MachineRelocation; 00026 typedef std::vector<uint8_t> BinaryData; 00027 00028 class BinaryObject { 00029 protected: 00030 std::string Name; 00031 bool IsLittleEndian; 00032 bool Is64Bit; 00033 BinaryData Data; 00034 std::vector<MachineRelocation> Relocations; 00035 00036 public: 00037 /// Constructors and destructor 00038 BinaryObject() {} 00039 00040 BinaryObject(bool isLittleEndian, bool is64Bit) 00041 : IsLittleEndian(isLittleEndian), Is64Bit(is64Bit) {} 00042 00043 BinaryObject(const std::string &name, bool isLittleEndian, bool is64Bit) 00044 : Name(name), IsLittleEndian(isLittleEndian), Is64Bit(is64Bit) {} 00045 00046 ~BinaryObject() {} 00047 00048 /// getName - get name of BinaryObject 00049 inline std::string getName() const { return Name; } 00050 00051 /// get size of binary data 00052 size_t size() const { 00053 return Data.size(); 00054 } 00055 00056 /// get binary data 00057 BinaryData& getData() { 00058 return Data; 00059 } 00060 00061 /// get machine relocations 00062 const std::vector<MachineRelocation>& getRelocations() const { 00063 return Relocations; 00064 } 00065 00066 /// hasRelocations - Return true if 'Relocations' is not empty 00067 bool hasRelocations() const { 00068 return !Relocations.empty(); 00069 } 00070 00071 /// emitZeros - This callback is invoked to emit a arbitrary number 00072 /// of zero bytes to the data stream. 00073 inline void emitZeros(unsigned Size) { 00074 for (unsigned i=0; i < Size; ++i) 00075 emitByte(0); 00076 } 00077 00078 /// emitByte - This callback is invoked when a byte needs to be 00079 /// written to the data stream. 00080 inline void emitByte(uint8_t B) { 00081 Data.push_back(B); 00082 } 00083 00084 /// emitWord16 - This callback is invoked when a 16-bit word needs to be 00085 /// written to the data stream in correct endian format and correct size. 00086 inline void emitWord16(uint16_t W) { 00087 if (IsLittleEndian) 00088 emitWord16LE(W); 00089 else 00090 emitWord16BE(W); 00091 } 00092 00093 /// emitWord16LE - This callback is invoked when a 16-bit word needs to be 00094 /// written to the data stream in correct endian format and correct size. 00095 inline void emitWord16LE(uint16_t W) { 00096 Data.push_back((uint8_t)(W >> 0)); 00097 Data.push_back((uint8_t)(W >> 8)); 00098 } 00099 00100 /// emitWord16BE - This callback is invoked when a 16-bit word needs to be 00101 /// written to the data stream in correct endian format and correct size. 00102 inline void emitWord16BE(uint16_t W) { 00103 Data.push_back((uint8_t)(W >> 8)); 00104 Data.push_back((uint8_t)(W >> 0)); 00105 } 00106 00107 /// emitWord - This callback is invoked when a word needs to be 00108 /// written to the data stream in correct endian format and correct size. 00109 inline void emitWord(uint64_t W) { 00110 if (!Is64Bit) 00111 emitWord32(W); 00112 else 00113 emitWord64(W); 00114 } 00115 00116 /// emitWord32 - This callback is invoked when a 32-bit word needs to be 00117 /// written to the data stream in correct endian format. 00118 inline void emitWord32(uint32_t W) { 00119 if (IsLittleEndian) 00120 emitWordLE(W); 00121 else 00122 emitWordBE(W); 00123 } 00124 00125 /// emitWord64 - This callback is invoked when a 32-bit word needs to be 00126 /// written to the data stream in correct endian format. 00127 inline void emitWord64(uint64_t W) { 00128 if (IsLittleEndian) 00129 emitDWordLE(W); 00130 else 00131 emitDWordBE(W); 00132 } 00133 00134 /// emitWord64 - This callback is invoked when a x86_fp80 needs to be 00135 /// written to the data stream in correct endian format. 00136 inline void emitWordFP80(const uint64_t *W, unsigned PadSize) { 00137 if (IsLittleEndian) { 00138 emitWord64(W[0]); 00139 emitWord16(W[1]); 00140 } else { 00141 emitWord16(W[1]); 00142 emitWord64(W[0]); 00143 } 00144 emitZeros(PadSize); 00145 } 00146 00147 /// emitWordLE - This callback is invoked when a 32-bit word needs to be 00148 /// written to the data stream in little-endian format. 00149 inline void emitWordLE(uint32_t W) { 00150 Data.push_back((uint8_t)(W >> 0)); 00151 Data.push_back((uint8_t)(W >> 8)); 00152 Data.push_back((uint8_t)(W >> 16)); 00153 Data.push_back((uint8_t)(W >> 24)); 00154 } 00155 00156 /// emitWordBE - This callback is invoked when a 32-bit word needs to be 00157 /// written to the data stream in big-endian format. 00158 /// 00159 inline void emitWordBE(uint32_t W) { 00160 Data.push_back((uint8_t)(W >> 24)); 00161 Data.push_back((uint8_t)(W >> 16)); 00162 Data.push_back((uint8_t)(W >> 8)); 00163 Data.push_back((uint8_t)(W >> 0)); 00164 } 00165 00166 /// emitDWordLE - This callback is invoked when a 64-bit word needs to be 00167 /// written to the data stream in little-endian format. 00168 inline void emitDWordLE(uint64_t W) { 00169 Data.push_back((uint8_t)(W >> 0)); 00170 Data.push_back((uint8_t)(W >> 8)); 00171 Data.push_back((uint8_t)(W >> 16)); 00172 Data.push_back((uint8_t)(W >> 24)); 00173 Data.push_back((uint8_t)(W >> 32)); 00174 Data.push_back((uint8_t)(W >> 40)); 00175 Data.push_back((uint8_t)(W >> 48)); 00176 Data.push_back((uint8_t)(W >> 56)); 00177 } 00178 00179 /// emitDWordBE - This callback is invoked when a 64-bit word needs to be 00180 /// written to the data stream in big-endian format. 00181 inline void emitDWordBE(uint64_t W) { 00182 Data.push_back((uint8_t)(W >> 56)); 00183 Data.push_back((uint8_t)(W >> 48)); 00184 Data.push_back((uint8_t)(W >> 40)); 00185 Data.push_back((uint8_t)(W >> 32)); 00186 Data.push_back((uint8_t)(W >> 24)); 00187 Data.push_back((uint8_t)(W >> 16)); 00188 Data.push_back((uint8_t)(W >> 8)); 00189 Data.push_back((uint8_t)(W >> 0)); 00190 } 00191 00192 /// fixByte - This callback is invoked when a byte needs to be 00193 /// fixup the buffer. 00194 inline void fixByte(uint8_t B, uint32_t offset) { 00195 Data[offset] = B; 00196 } 00197 00198 /// fixWord16 - This callback is invoked when a 16-bit word needs to 00199 /// fixup the data stream in correct endian format. 00200 inline void fixWord16(uint16_t W, uint32_t offset) { 00201 if (IsLittleEndian) 00202 fixWord16LE(W, offset); 00203 else 00204 fixWord16BE(W, offset); 00205 } 00206 00207 /// emitWord16LE - This callback is invoked when a 16-bit word needs to 00208 /// fixup the data stream in little endian format. 00209 inline void fixWord16LE(uint16_t W, uint32_t offset) { 00210 Data[offset] = (uint8_t)(W >> 0); 00211 Data[++offset] = (uint8_t)(W >> 8); 00212 } 00213 00214 /// fixWord16BE - This callback is invoked when a 16-bit word needs to 00215 /// fixup data stream in big endian format. 00216 inline void fixWord16BE(uint16_t W, uint32_t offset) { 00217 Data[offset] = (uint8_t)(W >> 8); 00218 Data[++offset] = (uint8_t)(W >> 0); 00219 } 00220 00221 /// emitWord - This callback is invoked when a word needs to 00222 /// fixup the data in correct endian format and correct size. 00223 inline void fixWord(uint64_t W, uint32_t offset) { 00224 if (!Is64Bit) 00225 fixWord32(W, offset); 00226 else 00227 fixWord64(W, offset); 00228 } 00229 00230 /// fixWord32 - This callback is invoked when a 32-bit word needs to 00231 /// fixup the data in correct endian format. 00232 inline void fixWord32(uint32_t W, uint32_t offset) { 00233 if (IsLittleEndian) 00234 fixWord32LE(W, offset); 00235 else 00236 fixWord32BE(W, offset); 00237 } 00238 00239 /// fixWord32LE - This callback is invoked when a 32-bit word needs to 00240 /// fixup the data in little endian format. 00241 inline void fixWord32LE(uint32_t W, uint32_t offset) { 00242 Data[offset] = (uint8_t)(W >> 0); 00243 Data[++offset] = (uint8_t)(W >> 8); 00244 Data[++offset] = (uint8_t)(W >> 16); 00245 Data[++offset] = (uint8_t)(W >> 24); 00246 } 00247 00248 /// fixWord32BE - This callback is invoked when a 32-bit word needs to 00249 /// fixup the data in big endian format. 00250 inline void fixWord32BE(uint32_t W, uint32_t offset) { 00251 Data[offset] = (uint8_t)(W >> 24); 00252 Data[++offset] = (uint8_t)(W >> 16); 00253 Data[++offset] = (uint8_t)(W >> 8); 00254 Data[++offset] = (uint8_t)(W >> 0); 00255 } 00256 00257 /// fixWord64 - This callback is invoked when a 64-bit word needs to 00258 /// fixup the data in correct endian format. 00259 inline void fixWord64(uint64_t W, uint32_t offset) { 00260 if (IsLittleEndian) 00261 fixWord64LE(W, offset); 00262 else 00263 fixWord64BE(W, offset); 00264 } 00265 00266 /// fixWord64BE - This callback is invoked when a 64-bit word needs to 00267 /// fixup the data in little endian format. 00268 inline void fixWord64LE(uint64_t W, uint32_t offset) { 00269 Data[offset] = (uint8_t)(W >> 0); 00270 Data[++offset] = (uint8_t)(W >> 8); 00271 Data[++offset] = (uint8_t)(W >> 16); 00272 Data[++offset] = (uint8_t)(W >> 24); 00273 Data[++offset] = (uint8_t)(W >> 32); 00274 Data[++offset] = (uint8_t)(W >> 40); 00275 Data[++offset] = (uint8_t)(W >> 48); 00276 Data[++offset] = (uint8_t)(W >> 56); 00277 } 00278 00279 /// fixWord64BE - This callback is invoked when a 64-bit word needs to 00280 /// fixup the data in big endian format. 00281 inline void fixWord64BE(uint64_t W, uint32_t offset) { 00282 Data[offset] = (uint8_t)(W >> 56); 00283 Data[++offset] = (uint8_t)(W >> 48); 00284 Data[++offset] = (uint8_t)(W >> 40); 00285 Data[++offset] = (uint8_t)(W >> 32); 00286 Data[++offset] = (uint8_t)(W >> 24); 00287 Data[++offset] = (uint8_t)(W >> 16); 00288 Data[++offset] = (uint8_t)(W >> 8); 00289 Data[++offset] = (uint8_t)(W >> 0); 00290 } 00291 00292 /// emitAlignment - Pad the data to the specified alignment. 00293 void emitAlignment(unsigned Alignment, uint8_t fill = 0) { 00294 if (Alignment <= 1) return; 00295 unsigned PadSize = -Data.size() & (Alignment-1); 00296 for (unsigned i = 0; i<PadSize; ++i) 00297 Data.push_back(fill); 00298 } 00299 00300 /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be 00301 /// written to the data stream. 00302 void emitULEB128Bytes(uint64_t Value) { 00303 do { 00304 uint8_t Byte = (uint8_t)(Value & 0x7f); 00305 Value >>= 7; 00306 if (Value) Byte |= 0x80; 00307 emitByte(Byte); 00308 } while (Value); 00309 } 00310 00311 /// emitSLEB128Bytes - This callback is invoked when a SLEB128 needs to be 00312 /// written to the data stream. 00313 void emitSLEB128Bytes(int64_t Value) { 00314 int Sign = Value >> (8 * sizeof(Value) - 1); 00315 bool IsMore; 00316 00317 do { 00318 uint8_t Byte = (uint8_t)(Value & 0x7f); 00319 Value >>= 7; 00320 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0; 00321 if (IsMore) Byte |= 0x80; 00322 emitByte(Byte); 00323 } while (IsMore); 00324 } 00325 00326 /// emitString - This callback is invoked when a String needs to be 00327 /// written to the data stream. 00328 void emitString(const std::string &String) { 00329 for (unsigned i = 0, N = static_cast<unsigned>(String.size()); i<N; ++i) { 00330 unsigned char C = String[i]; 00331 emitByte(C); 00332 } 00333 emitByte(0); 00334 } 00335 00336 /// getCurrentPCOffset - Return the offset from the start of the emitted 00337 /// buffer that we are currently writing to. 00338 uintptr_t getCurrentPCOffset() const { 00339 return Data.size(); 00340 } 00341 00342 /// addRelocation - Whenever a relocatable address is needed, it should be 00343 /// noted with this interface. 00344 void addRelocation(const MachineRelocation& relocation) { 00345 Relocations.push_back(relocation); 00346 } 00347 00348 }; 00349 00350 } // end namespace llvm 00351 00352 #endif 00353