1928e23f9bafe41e5cce81bf98817bc9c825d972aChris Lattner//===-- llvm/CodeGen/MachineCodeEmitter.h - Code emission -------*- C++ -*-===// 2ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman// 36fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell// The LLVM Compiler Infrastructure 46fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell// 57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source 67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details. 7ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman// 86fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//===----------------------------------------------------------------------===// 9928e23f9bafe41e5cce81bf98817bc9c825d972aChris Lattner// 10928e23f9bafe41e5cce81bf98817bc9c825d972aChris Lattner// This file defines an abstract interface that is used by the machine code 11928e23f9bafe41e5cce81bf98817bc9c825d972aChris Lattner// emission framework to output the code. This allows machine code emission to 12bc0e998c497446f5448425b3cbd7f8f19a458764Misha Brukman// be separated from concerns such as resolution of call targets, and where the 13928e23f9bafe41e5cce81bf98817bc9c825d972aChris Lattner// machine code will be written (memory or disk, f.e.). 14928e23f9bafe41e5cce81bf98817bc9c825d972aChris Lattner// 15928e23f9bafe41e5cce81bf98817bc9c825d972aChris Lattner//===----------------------------------------------------------------------===// 16928e23f9bafe41e5cce81bf98817bc9c825d972aChris Lattner 17661ab811d8bbc6500f8ad3051041fa94951d1480Brian Gaeke#ifndef LLVM_CODEGEN_MACHINECODEEMITTER_H 18661ab811d8bbc6500f8ad3051041fa94951d1480Brian Gaeke#define LLVM_CODEGEN_MACHINECODEEMITTER_H 19928e23f9bafe41e5cce81bf98817bc9c825d972aChris Lattner 201f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/DataTypes.h" 2132360a7e21a4454aa7014992213823fb4319905aJeffrey Yasskin#include "llvm/Support/DebugLoc.h" 222d24e2a396a1d211baaeedf32148a3b657240170David Blaikie#include <string> 232d24e2a396a1d211baaeedf32148a3b657240170David Blaikie 24d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm { 25d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 26d1c45a6633bb000920373c57b3590191d491036fChris Lattnerclass MachineBasicBlock; 2729dd209228125b7ae7431edb9278b6038ef4a87fChris Lattnerclass MachineConstantPool; 2837efe6764568a3829fee26aba532283131d1a104Nate Begemanclass MachineJumpTableInfo; 2952e3c5c6b955bf8313c95a4339d6cf91c4547e56Chris Lattnerclass MachineFunction; 30afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffrayclass MachineModuleInfo; 3152e3c5c6b955bf8313c95a4339d6cf91c4547e56Chris Lattnerclass MachineRelocation; 328562d4ec7ce4719df7e106a3d27e4549e2cc8054Chris Lattnerclass Value; 3364ccc73593370f064081552c73aa814cb2287675Chris Lattnerclass GlobalValue; 34eccf8d05ee2a23a53ff6e892cf1c34fe5da234e0Chris Lattnerclass Function; 351611273351d75b5cbe2a67485bb9831d5916fe26Chris Lattnerclass MCSymbol; 36928e23f9bafe41e5cce81bf98817bc9c825d972aChris Lattner 3743b429b05989075b60693d57395c99b0ad789f8dChris Lattner/// MachineCodeEmitter - This class defines two sorts of methods: those for 387a2bdde0a0eebcd2125055e0eacaca040f0b766cChris Lattner/// emitting the actual bytes of machine code, and those for emitting auxiliary 3943b429b05989075b60693d57395c99b0ad789f8dChris Lattner/// structures, such as jump tables, relocations, etc. 4043b429b05989075b60693d57395c99b0ad789f8dChris Lattner/// 4143b429b05989075b60693d57395c99b0ad789f8dChris Lattner/// Emission of machine code is complicated by the fact that we don't (in 4243b429b05989075b60693d57395c99b0ad789f8dChris Lattner/// general) know the size of the machine code that we're about to emit before 4343b429b05989075b60693d57395c99b0ad789f8dChris Lattner/// we emit it. As such, we preallocate a certain amount of memory, and set the 4443b429b05989075b60693d57395c99b0ad789f8dChris Lattner/// BufferBegin/BufferEnd pointers to the start and end of the buffer. As we 4543b429b05989075b60693d57395c99b0ad789f8dChris Lattner/// emit machine instructions, we advance the CurBufferPtr to indicate the 4643b429b05989075b60693d57395c99b0ad789f8dChris Lattner/// location of the next byte to emit. In the case of a buffer overflow (we 4743b429b05989075b60693d57395c99b0ad789f8dChris Lattner/// need to emit more machine code than we have allocated space for), the 4843b429b05989075b60693d57395c99b0ad789f8dChris Lattner/// CurBufferPtr will saturate to BufferEnd and ignore stores. Once the entire 4943b429b05989075b60693d57395c99b0ad789f8dChris Lattner/// function has been emitted, the overflow condition is checked, and if it has 5043b429b05989075b60693d57395c99b0ad789f8dChris Lattner/// occurred, more memory is allocated, and we reemit the code into it. 5143b429b05989075b60693d57395c99b0ad789f8dChris Lattner/// 521fca5ff62bb2ecb5bfc8974f4dbfc56e9d3ca721Chris Lattnerclass MachineCodeEmitter { 532d24e2a396a1d211baaeedf32148a3b657240170David Blaikie virtual void anchor(); 5443b429b05989075b60693d57395c99b0ad789f8dChris Lattnerprotected: 5532d7e6ebde29faeea75ecb718b4281414b0eea0bJeffrey Yasskin /// BufferBegin/BufferEnd - Pointers to the start and end of the memory 5632d7e6ebde29faeea75ecb718b4281414b0eea0bJeffrey Yasskin /// allocated for this code buffer. 5732d7e6ebde29faeea75ecb718b4281414b0eea0bJeffrey Yasskin uint8_t *BufferBegin, *BufferEnd; 5832d7e6ebde29faeea75ecb718b4281414b0eea0bJeffrey Yasskin /// CurBufferPtr - Pointer to the next byte of memory to fill when emitting 597a2bdde0a0eebcd2125055e0eacaca040f0b766cChris Lattner /// code. This is guaranteed to be in the range [BufferBegin,BufferEnd]. If 6032d7e6ebde29faeea75ecb718b4281414b0eea0bJeffrey Yasskin /// this pointer is at BufferEnd, it will never move due to code emission, and 6132d7e6ebde29faeea75ecb718b4281414b0eea0bJeffrey Yasskin /// all code emission requests will be ignored (this is the buffer overflow 6232d7e6ebde29faeea75ecb718b4281414b0eea0bJeffrey Yasskin /// condition). 6332d7e6ebde29faeea75ecb718b4281414b0eea0bJeffrey Yasskin uint8_t *CurBufferPtr; 642a3e08b5961353fa3faeadf81f481ae9f5463427Evan Cheng 651fca5ff62bb2ecb5bfc8974f4dbfc56e9d3ca721Chris Lattnerpublic: 66ffc2d6f48544b24d3ac31f486d20c7073cb0b35fChris Lattner virtual ~MachineCodeEmitter() {} 67928e23f9bafe41e5cce81bf98817bc9c825d972aChris Lattner 68d1c45a6633bb000920373c57b3590191d491036fChris Lattner /// startFunction - This callback is invoked when the specified function is 6943b429b05989075b60693d57395c99b0ad789f8dChris Lattner /// about to be code generated. This initializes the BufferBegin/End/Ptr 7043b429b05989075b60693d57395c99b0ad789f8dChris Lattner /// fields. 71d1c45a6633bb000920373c57b3590191d491036fChris Lattner /// 72f75f9be3fb89eb6661a0ed8bfee8a6328ee5a4d1Chris Lattner virtual void startFunction(MachineFunction &F) = 0; 73ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman 74d1c45a6633bb000920373c57b3590191d491036fChris Lattner /// finishFunction - This callback is invoked when the specified function has 7543b429b05989075b60693d57395c99b0ad789f8dChris Lattner /// finished code generation. If a buffer overflow has occurred, this method 7643b429b05989075b60693d57395c99b0ad789f8dChris Lattner /// returns true (the callee is required to try again), otherwise it returns 7743b429b05989075b60693d57395c99b0ad789f8dChris Lattner /// false. 78d1c45a6633bb000920373c57b3590191d491036fChris Lattner /// 79f75f9be3fb89eb6661a0ed8bfee8a6328ee5a4d1Chris Lattner virtual bool finishFunction(MachineFunction &F) = 0; 801f25d8801f43752ee6dea6f2bfdf9430a46d20eeChris Lattner 81d1c45a6633bb000920373c57b3590191d491036fChris Lattner /// emitByte - This callback is invoked when a byte needs to be written to the 82d1c45a6633bb000920373c57b3590191d491036fChris Lattner /// output stream. 838562d4ec7ce4719df7e106a3d27e4549e2cc8054Chris Lattner /// 84186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes void emitByte(uint8_t B) { 8543b429b05989075b60693d57395c99b0ad789f8dChris Lattner if (CurBufferPtr != BufferEnd) 8643b429b05989075b60693d57395c99b0ad789f8dChris Lattner *CurBufferPtr++ = B; 8743b429b05989075b60693d57395c99b0ad789f8dChris Lattner } 888562d4ec7ce4719df7e106a3d27e4549e2cc8054Chris Lattner 89d3f0aefc33965d3d0ca6f92af4ebaea354b063c4Chris Lattner /// emitWordLE - This callback is invoked when a 32-bit word needs to be 90d3f0aefc33965d3d0ca6f92af4ebaea354b063c4Chris Lattner /// written to the output stream in little-endian format. 91d3f0aefc33965d3d0ca6f92af4ebaea354b063c4Chris Lattner /// 927e0b551e2aeefd9b32810e14b8b27b6da3ec441eBruno Cardoso Lopes void emitWordLE(uint32_t W) { 935788d1a169db3346a612a13113348d2709bdd15bEvan Cheng if (4 <= BufferEnd-CurBufferPtr) { 9432d7e6ebde29faeea75ecb718b4281414b0eea0bJeffrey Yasskin emitWordLEInto(CurBufferPtr, W); 95d3f0aefc33965d3d0ca6f92af4ebaea354b063c4Chris Lattner } else { 96d3f0aefc33965d3d0ca6f92af4ebaea354b063c4Chris Lattner CurBufferPtr = BufferEnd; 97d3f0aefc33965d3d0ca6f92af4ebaea354b063c4Chris Lattner } 98d3f0aefc33965d3d0ca6f92af4ebaea354b063c4Chris Lattner } 9932d7e6ebde29faeea75ecb718b4281414b0eea0bJeffrey Yasskin 10032d7e6ebde29faeea75ecb718b4281414b0eea0bJeffrey Yasskin /// emitWordLEInto - This callback is invoked when a 32-bit word needs to be 10132d7e6ebde29faeea75ecb718b4281414b0eea0bJeffrey Yasskin /// written to an arbitrary buffer in little-endian format. Buf must have at 10232d7e6ebde29faeea75ecb718b4281414b0eea0bJeffrey Yasskin /// least 4 bytes of available space. 10332d7e6ebde29faeea75ecb718b4281414b0eea0bJeffrey Yasskin /// 10432d7e6ebde29faeea75ecb718b4281414b0eea0bJeffrey Yasskin static void emitWordLEInto(uint8_t *&Buf, uint32_t W) { 10532d7e6ebde29faeea75ecb718b4281414b0eea0bJeffrey Yasskin *Buf++ = (uint8_t)(W >> 0); 10632d7e6ebde29faeea75ecb718b4281414b0eea0bJeffrey Yasskin *Buf++ = (uint8_t)(W >> 8); 10732d7e6ebde29faeea75ecb718b4281414b0eea0bJeffrey Yasskin *Buf++ = (uint8_t)(W >> 16); 10832d7e6ebde29faeea75ecb718b4281414b0eea0bJeffrey Yasskin *Buf++ = (uint8_t)(W >> 24); 10932d7e6ebde29faeea75ecb718b4281414b0eea0bJeffrey Yasskin } 11032d7e6ebde29faeea75ecb718b4281414b0eea0bJeffrey Yasskin 111d3f0aefc33965d3d0ca6f92af4ebaea354b063c4Chris Lattner /// emitWordBE - This callback is invoked when a 32-bit word needs to be 112d3f0aefc33965d3d0ca6f92af4ebaea354b063c4Chris Lattner /// written to the output stream in big-endian format. 11364ccc73593370f064081552c73aa814cb2287675Chris Lattner /// 1147e0b551e2aeefd9b32810e14b8b27b6da3ec441eBruno Cardoso Lopes void emitWordBE(uint32_t W) { 1155788d1a169db3346a612a13113348d2709bdd15bEvan Cheng if (4 <= BufferEnd-CurBufferPtr) { 116186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 24); 117186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 16); 118186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 8); 119186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 0); 120c9f3cc3bdaf1ec134dec1f718e7f2e735b34b17bDan Gohman } else { 121c9f3cc3bdaf1ec134dec1f718e7f2e735b34b17bDan Gohman CurBufferPtr = BufferEnd; 122c9f3cc3bdaf1ec134dec1f718e7f2e735b34b17bDan Gohman } 123c9f3cc3bdaf1ec134dec1f718e7f2e735b34b17bDan Gohman } 124c9f3cc3bdaf1ec134dec1f718e7f2e735b34b17bDan Gohman 125c9f3cc3bdaf1ec134dec1f718e7f2e735b34b17bDan Gohman /// emitDWordLE - This callback is invoked when a 64-bit word needs to be 126c9f3cc3bdaf1ec134dec1f718e7f2e735b34b17bDan Gohman /// written to the output stream in little-endian format. 127c9f3cc3bdaf1ec134dec1f718e7f2e735b34b17bDan Gohman /// 128c9f3cc3bdaf1ec134dec1f718e7f2e735b34b17bDan Gohman void emitDWordLE(uint64_t W) { 1295788d1a169db3346a612a13113348d2709bdd15bEvan Cheng if (8 <= BufferEnd-CurBufferPtr) { 130186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 0); 131186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 8); 132186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 16); 133186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 24); 134186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 32); 135186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 40); 136186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 48); 137186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 56); 138c9f3cc3bdaf1ec134dec1f718e7f2e735b34b17bDan Gohman } else { 139c9f3cc3bdaf1ec134dec1f718e7f2e735b34b17bDan Gohman CurBufferPtr = BufferEnd; 140c9f3cc3bdaf1ec134dec1f718e7f2e735b34b17bDan Gohman } 141c9f3cc3bdaf1ec134dec1f718e7f2e735b34b17bDan Gohman } 142c9f3cc3bdaf1ec134dec1f718e7f2e735b34b17bDan Gohman 143c9f3cc3bdaf1ec134dec1f718e7f2e735b34b17bDan Gohman /// emitDWordBE - This callback is invoked when a 64-bit word needs to be 144c9f3cc3bdaf1ec134dec1f718e7f2e735b34b17bDan Gohman /// written to the output stream in big-endian format. 145c9f3cc3bdaf1ec134dec1f718e7f2e735b34b17bDan Gohman /// 146c9f3cc3bdaf1ec134dec1f718e7f2e735b34b17bDan Gohman void emitDWordBE(uint64_t W) { 1475788d1a169db3346a612a13113348d2709bdd15bEvan Cheng if (8 <= BufferEnd-CurBufferPtr) { 148186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 56); 149186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 48); 150186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 40); 151186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 32); 152186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 24); 153186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 16); 154186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 8); 155186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 0); 15643b429b05989075b60693d57395c99b0ad789f8dChris Lattner } else { 15743b429b05989075b60693d57395c99b0ad789f8dChris Lattner CurBufferPtr = BufferEnd; 15843b429b05989075b60693d57395c99b0ad789f8dChris Lattner } 15943b429b05989075b60693d57395c99b0ad789f8dChris Lattner } 160f2e364ad3aaca501a30fb463c55366e4a829eccaChris Lattner 161f3b11aa6a72e0c31066a60c2e888e7a5eb5f2399Dan Gohman /// emitAlignment - Move the CurBufferPtr pointer up to the specified 1623db9e30c09404d5562001ff88c8a222edd9a5efeChris Lattner /// alignment (saturated to BufferEnd of course). 1633db9e30c09404d5562001ff88c8a222edd9a5efeChris Lattner void emitAlignment(unsigned Alignment) { 1641f4549f35c69376a07360bd57ff06ed8f636d153Chris Lattner if (Alignment == 0) Alignment = 1; 1655788d1a169db3346a612a13113348d2709bdd15bEvan Cheng 1665788d1a169db3346a612a13113348d2709bdd15bEvan Cheng if(Alignment <= (uintptr_t)(BufferEnd-CurBufferPtr)) { 1675788d1a169db3346a612a13113348d2709bdd15bEvan Cheng // Move the current buffer ptr up to the specified alignment. 1685788d1a169db3346a612a13113348d2709bdd15bEvan Cheng CurBufferPtr = 169186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes (uint8_t*)(((uintptr_t)CurBufferPtr+Alignment-1) & 170186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes ~(uintptr_t)(Alignment-1)); 1715788d1a169db3346a612a13113348d2709bdd15bEvan Cheng } else { 1723db9e30c09404d5562001ff88c8a222edd9a5efeChris Lattner CurBufferPtr = BufferEnd; 1735788d1a169db3346a612a13113348d2709bdd15bEvan Cheng } 1743db9e30c09404d5562001ff88c8a222edd9a5efeChris Lattner } 1753db9e30c09404d5562001ff88c8a222edd9a5efeChris Lattner 176afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray 177afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be 178afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray /// written to the output stream. 1797e0b551e2aeefd9b32810e14b8b27b6da3ec441eBruno Cardoso Lopes void emitULEB128Bytes(uint64_t Value) { 180afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray do { 181186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes uint8_t Byte = Value & 0x7f; 182afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray Value >>= 7; 183afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray if (Value) Byte |= 0x80; 184afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray emitByte(Byte); 185afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray } while (Value); 186afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray } 187afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray 188afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray /// emitSLEB128Bytes - This callback is invoked when a SLEB128 needs to be 189afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray /// written to the output stream. 1907e0b551e2aeefd9b32810e14b8b27b6da3ec441eBruno Cardoso Lopes void emitSLEB128Bytes(uint64_t Value) { 1917e0b551e2aeefd9b32810e14b8b27b6da3ec441eBruno Cardoso Lopes uint64_t Sign = Value >> (8 * sizeof(Value) - 1); 192afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray bool IsMore; 193afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray 194afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray do { 195186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes uint8_t Byte = Value & 0x7f; 196afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray Value >>= 7; 197afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0; 198afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray if (IsMore) Byte |= 0x80; 199afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray emitByte(Byte); 200afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray } while (IsMore); 201afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray } 202afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray 203afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray /// emitString - This callback is invoked when a String needs to be 204afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray /// written to the output stream. 205afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray void emitString(const std::string &String) { 20634cd4a484e532cc463fd5a4bf59b88d13c5467c1Evan Cheng for (unsigned i = 0, N = static_cast<unsigned>(String.size()); 20734cd4a484e532cc463fd5a4bf59b88d13c5467c1Evan Cheng i < N; ++i) { 208186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes uint8_t C = String[i]; 209afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray emitByte(C); 210afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray } 211afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray emitByte(0); 212afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray } 213afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray 214afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray /// emitInt32 - Emit a int32 directive. 215186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes void emitInt32(int32_t Value) { 2165788d1a169db3346a612a13113348d2709bdd15bEvan Cheng if (4 <= BufferEnd-CurBufferPtr) { 217afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray *((uint32_t*)CurBufferPtr) = Value; 218afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray CurBufferPtr += 4; 219afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray } else { 220afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray CurBufferPtr = BufferEnd; 221afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray } 222afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray } 223afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray 224afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray /// emitInt64 - Emit a int64 directive. 225afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray void emitInt64(uint64_t Value) { 2265788d1a169db3346a612a13113348d2709bdd15bEvan Cheng if (8 <= BufferEnd-CurBufferPtr) { 227afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray *((uint64_t*)CurBufferPtr) = Value; 228afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray CurBufferPtr += 8; 229afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray } else { 230afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray CurBufferPtr = BufferEnd; 231afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray } 232afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray } 233afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray 23467c8c4cababc846aab461910a50fd373b35b11aaNicolas Geoffray /// emitInt32At - Emit the Int32 Value in Addr. 23567c8c4cababc846aab461910a50fd373b35b11aaNicolas Geoffray void emitInt32At(uintptr_t *Addr, uintptr_t Value) { 236afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd) 23767c8c4cababc846aab461910a50fd373b35b11aaNicolas Geoffray (*(uint32_t*)Addr) = (uint32_t)Value; 238afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray } 239afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray 24067c8c4cababc846aab461910a50fd373b35b11aaNicolas Geoffray /// emitInt64At - Emit the Int64 Value in Addr. 24167c8c4cababc846aab461910a50fd373b35b11aaNicolas Geoffray void emitInt64At(uintptr_t *Addr, uintptr_t Value) { 24267c8c4cababc846aab461910a50fd373b35b11aaNicolas Geoffray if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd) 24367c8c4cababc846aab461910a50fd373b35b11aaNicolas Geoffray (*(uint64_t*)Addr) = (uint64_t)Value; 24467c8c4cababc846aab461910a50fd373b35b11aaNicolas Geoffray } 24567c8c4cababc846aab461910a50fd373b35b11aaNicolas Geoffray 24632360a7e21a4454aa7014992213823fb4319905aJeffrey Yasskin /// processDebugLoc - Records debug location information about a 24732360a7e21a4454aa7014992213823fb4319905aJeffrey Yasskin /// MachineInstruction. This is called before emitting any bytes associated 24832360a7e21a4454aa7014992213823fb4319905aJeffrey Yasskin /// with the instruction. Even if successive instructions have the same debug 24932360a7e21a4454aa7014992213823fb4319905aJeffrey Yasskin /// location, this method will be called for each one. 250af0e2726835e096e32c30c1b88cc7a6232a6ef69Devang Patel virtual void processDebugLoc(DebugLoc DL, bool BeforePrintintInsn) {} 25132360a7e21a4454aa7014992213823fb4319905aJeffrey Yasskin 252afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray /// emitLabel - Emits a label 2531611273351d75b5cbe2a67485bb9831d5916fe26Chris Lattner virtual void emitLabel(MCSymbol *Label) = 0; 254afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray 2553db9e30c09404d5562001ff88c8a222edd9a5efeChris Lattner /// allocateSpace - Allocate a block of space in the current output buffer, 2563db9e30c09404d5562001ff88c8a222edd9a5efeChris Lattner /// returning null (and setting conditions to indicate buffer overflow) on 2573db9e30c09404d5562001ff88c8a222edd9a5efeChris Lattner /// failure. Alignment is the alignment in bytes of the buffer desired. 2585788d1a169db3346a612a13113348d2709bdd15bEvan Cheng virtual void *allocateSpace(uintptr_t Size, unsigned Alignment) { 2593db9e30c09404d5562001ff88c8a222edd9a5efeChris Lattner emitAlignment(Alignment); 2605788d1a169db3346a612a13113348d2709bdd15bEvan Cheng void *Result; 2611f4549f35c69376a07360bd57ff06ed8f636d153Chris Lattner 2621f4549f35c69376a07360bd57ff06ed8f636d153Chris Lattner // Check for buffer overflow. 2635788d1a169db3346a612a13113348d2709bdd15bEvan Cheng if (Size >= (uintptr_t)(BufferEnd-CurBufferPtr)) { 2641f4549f35c69376a07360bd57ff06ed8f636d153Chris Lattner CurBufferPtr = BufferEnd; 2651f4549f35c69376a07360bd57ff06ed8f636d153Chris Lattner Result = 0; 2665788d1a169db3346a612a13113348d2709bdd15bEvan Cheng } else { 2675788d1a169db3346a612a13113348d2709bdd15bEvan Cheng // Allocate the space. 2685788d1a169db3346a612a13113348d2709bdd15bEvan Cheng Result = CurBufferPtr; 2695788d1a169db3346a612a13113348d2709bdd15bEvan Cheng CurBufferPtr += Size; 2701f4549f35c69376a07360bd57ff06ed8f636d153Chris Lattner } 2715788d1a169db3346a612a13113348d2709bdd15bEvan Cheng 2721f4549f35c69376a07360bd57ff06ed8f636d153Chris Lattner return Result; 2731f4549f35c69376a07360bd57ff06ed8f636d153Chris Lattner } 274b4432f3d4754e16c918428d34a9d8ec18ab79204Chris Lattner 275b4432f3d4754e16c918428d34a9d8ec18ab79204Chris Lattner /// StartMachineBasicBlock - This should be called by the target when a new 276b4432f3d4754e16c918428d34a9d8ec18ab79204Chris Lattner /// basic block is about to be emitted. This way the MCE knows where the 277b4432f3d4754e16c918428d34a9d8ec18ab79204Chris Lattner /// start of the block is, and can implement getMachineBasicBlockAddress. 278b4432f3d4754e16c918428d34a9d8ec18ab79204Chris Lattner virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) = 0; 2791f4549f35c69376a07360bd57ff06ed8f636d153Chris Lattner 28047012c0c711fd10901fb30643def557af7bbb4a9Chris Lattner /// getCurrentPCValue - This returns the address that the next emitted byte 28147012c0c711fd10901fb30643def557af7bbb4a9Chris Lattner /// will be output to. 28247012c0c711fd10901fb30643def557af7bbb4a9Chris Lattner /// 2835788d1a169db3346a612a13113348d2709bdd15bEvan Cheng virtual uintptr_t getCurrentPCValue() const { 2845788d1a169db3346a612a13113348d2709bdd15bEvan Cheng return (uintptr_t)CurBufferPtr; 28543b429b05989075b60693d57395c99b0ad789f8dChris Lattner } 28647012c0c711fd10901fb30643def557af7bbb4a9Chris Lattner 28747012c0c711fd10901fb30643def557af7bbb4a9Chris Lattner /// getCurrentPCOffset - Return the offset from the start of the emitted 28847012c0c711fd10901fb30643def557af7bbb4a9Chris Lattner /// buffer that we are currently writing to. 289ac57e6e498abccb117e0d61c2fa0f733845e50cbBruno Cardoso Lopes virtual uintptr_t getCurrentPCOffset() const { 29043b429b05989075b60693d57395c99b0ad789f8dChris Lattner return CurBufferPtr-BufferBegin; 29143b429b05989075b60693d57395c99b0ad789f8dChris Lattner } 29247012c0c711fd10901fb30643def557af7bbb4a9Chris Lattner 293e55fef36a93badf6c4925ea2455a1ca6b361357eBruno Cardoso Lopes /// earlyResolveAddresses - True if the code emitter can use symbol addresses 294e55fef36a93badf6c4925ea2455a1ca6b361357eBruno Cardoso Lopes /// during code emission time. The JIT is capable of doing this because it 295e55fef36a93badf6c4925ea2455a1ca6b361357eBruno Cardoso Lopes /// creates jump tables or constant pools in memory on the fly while the 296e55fef36a93badf6c4925ea2455a1ca6b361357eBruno Cardoso Lopes /// object code emitters rely on a linker to have real addresses and should 297e55fef36a93badf6c4925ea2455a1ca6b361357eBruno Cardoso Lopes /// use relocations instead. 298e55fef36a93badf6c4925ea2455a1ca6b361357eBruno Cardoso Lopes virtual bool earlyResolveAddresses() const = 0; 299e55fef36a93badf6c4925ea2455a1ca6b361357eBruno Cardoso Lopes 30047012c0c711fd10901fb30643def557af7bbb4a9Chris Lattner /// addRelocation - Whenever a relocatable address is needed, it should be 30147012c0c711fd10901fb30643def557af7bbb4a9Chris Lattner /// noted with this interface. 30247012c0c711fd10901fb30643def557af7bbb4a9Chris Lattner virtual void addRelocation(const MachineRelocation &MR) = 0; 303b4432f3d4754e16c918428d34a9d8ec18ab79204Chris Lattner 304b4432f3d4754e16c918428d34a9d8ec18ab79204Chris Lattner /// FIXME: These should all be handled with relocations! 305b4432f3d4754e16c918428d34a9d8ec18ab79204Chris Lattner 3063700a4d3a27ca8d39c73049020623cf4963d6c13Nate Begeman /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in 3073700a4d3a27ca8d39c73049020623cf4963d6c13Nate Begeman /// the constant pool that was last emitted with the emitConstantPool method. 3083700a4d3a27ca8d39c73049020623cf4963d6c13Nate Begeman /// 3095788d1a169db3346a612a13113348d2709bdd15bEvan Cheng virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const = 0; 310f2e364ad3aaca501a30fb463c55366e4a829eccaChris Lattner 3113700a4d3a27ca8d39c73049020623cf4963d6c13Nate Begeman /// getJumpTableEntryAddress - Return the address of the jump table with index 3123700a4d3a27ca8d39c73049020623cf4963d6c13Nate Begeman /// 'Index' in the function that last called initJumpTableInfo. 3133700a4d3a27ca8d39c73049020623cf4963d6c13Nate Begeman /// 3145788d1a169db3346a612a13113348d2709bdd15bEvan Cheng virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const = 0; 315b4432f3d4754e16c918428d34a9d8ec18ab79204Chris Lattner 316b4432f3d4754e16c918428d34a9d8ec18ab79204Chris Lattner /// getMachineBasicBlockAddress - Return the address of the specified 317b4432f3d4754e16c918428d34a9d8ec18ab79204Chris Lattner /// MachineBasicBlock, only usable after the label for the MBB has been 318b4432f3d4754e16c918428d34a9d8ec18ab79204Chris Lattner /// emitted. 319b4432f3d4754e16c918428d34a9d8ec18ab79204Chris Lattner /// 3205788d1a169db3346a612a13113348d2709bdd15bEvan Cheng virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const= 0; 321afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray 3221611273351d75b5cbe2a67485bb9831d5916fe26Chris Lattner /// getLabelAddress - Return the address of the specified Label, only usable 323afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray /// after the LabelID has been emitted. 324afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray /// 3251611273351d75b5cbe2a67485bb9831d5916fe26Chris Lattner virtual uintptr_t getLabelAddress(MCSymbol *Label) const = 0; 326afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray 327afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray /// Specifies the MachineModuleInfo object. This is used for exception handling 328afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray /// purposes. 329afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray virtual void setModuleInfo(MachineModuleInfo* Info) = 0; 330928e23f9bafe41e5cce81bf98817bc9c825d972aChris Lattner}; 331928e23f9bafe41e5cce81bf98817bc9c825d972aChris Lattner 332d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace 333d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 334928e23f9bafe41e5cce81bf98817bc9c825d972aChris Lattner#endif 335