Bruno Cardoso Lopes | 8ae058a | 2009-07-06 05:16:40 +0000 | [diff] [blame] | 1 | //===-- llvm/CodeGen/ObjectCodeEmitter.cpp -------------------- -*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/CodeGen/BinaryObject.h" |
| 11 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 12 | #include "llvm/CodeGen/MachineRelocation.h" |
| 13 | #include "llvm/CodeGen/ObjectCodeEmitter.h" |
| 14 | |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | // ObjectCodeEmitter Implementation |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | namespace llvm { |
| 20 | |
| 21 | ObjectCodeEmitter::ObjectCodeEmitter() : BO(0) {} |
| 22 | ObjectCodeEmitter::ObjectCodeEmitter(BinaryObject *bo) : BO(bo) {} |
| 23 | ObjectCodeEmitter::~ObjectCodeEmitter() {} |
| 24 | |
| 25 | /// setBinaryObject - set the BinaryObject we are writting to |
| 26 | void ObjectCodeEmitter::setBinaryObject(BinaryObject *bo) { BO = bo; } |
| 27 | |
| 28 | /// emitByte - This callback is invoked when a byte needs to be |
| 29 | /// written to the data stream, without buffer overflow testing. |
| 30 | void ObjectCodeEmitter::emitByte(uint8_t B) { |
| 31 | BO->emitByte(B); |
| 32 | } |
| 33 | |
| 34 | /// emitWordLE - This callback is invoked when a 32-bit word needs to be |
| 35 | /// written to the data stream in little-endian format. |
| 36 | void ObjectCodeEmitter::emitWordLE(uint32_t W) { |
| 37 | BO->emitWordLE(W); |
| 38 | } |
| 39 | |
| 40 | /// emitWordBE - This callback is invoked when a 32-bit word needs to be |
| 41 | /// written to the data stream in big-endian format. |
| 42 | void ObjectCodeEmitter::emitWordBE(uint32_t W) { |
| 43 | BO->emitWordBE(W); |
| 44 | } |
| 45 | |
| 46 | /// emitDWordLE - This callback is invoked when a 64-bit word needs to be |
| 47 | /// written to the data stream in little-endian format. |
| 48 | void ObjectCodeEmitter::emitDWordLE(uint64_t W) { |
| 49 | BO->emitDWordLE(W); |
| 50 | } |
| 51 | |
| 52 | /// emitDWordBE - This callback is invoked when a 64-bit word needs to be |
| 53 | /// written to the data stream in big-endian format. |
| 54 | void ObjectCodeEmitter::emitDWordBE(uint64_t W) { |
| 55 | BO->emitDWordBE(W); |
| 56 | } |
| 57 | |
Bruno Cardoso Lopes | 52d0851 | 2009-08-05 06:57:03 +0000 | [diff] [blame] | 58 | /// emitAlignment - Align 'BO' to the necessary alignment boundary. |
Bruno Cardoso Lopes | 8ae058a | 2009-07-06 05:16:40 +0000 | [diff] [blame] | 59 | void ObjectCodeEmitter::emitAlignment(unsigned Alignment /* 0 */, |
| 60 | uint8_t fill /* 0 */) { |
| 61 | BO->emitAlignment(Alignment, fill); |
| 62 | } |
| 63 | |
| 64 | /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be |
| 65 | /// written to the data stream. |
| 66 | void ObjectCodeEmitter::emitULEB128Bytes(uint64_t Value) { |
| 67 | BO->emitULEB128Bytes(Value); |
| 68 | } |
| 69 | |
| 70 | /// emitSLEB128Bytes - This callback is invoked when a SLEB128 needs to be |
| 71 | /// written to the data stream. |
| 72 | void ObjectCodeEmitter::emitSLEB128Bytes(uint64_t Value) { |
| 73 | BO->emitSLEB128Bytes(Value); |
| 74 | } |
| 75 | |
| 76 | /// emitString - This callback is invoked when a String needs to be |
| 77 | /// written to the data stream. |
| 78 | void ObjectCodeEmitter::emitString(const std::string &String) { |
| 79 | BO->emitString(String); |
| 80 | } |
| 81 | |
| 82 | /// getCurrentPCValue - This returns the address that the next emitted byte |
| 83 | /// will be output to. |
| 84 | uintptr_t ObjectCodeEmitter::getCurrentPCValue() const { |
| 85 | return BO->getCurrentPCOffset(); |
| 86 | } |
| 87 | |
| 88 | /// getCurrentPCOffset - Return the offset from the start of the emitted |
| 89 | /// buffer that we are currently writing to. |
| 90 | uintptr_t ObjectCodeEmitter::getCurrentPCOffset() const { |
| 91 | return BO->getCurrentPCOffset(); |
| 92 | } |
| 93 | |
| 94 | /// addRelocation - Whenever a relocatable address is needed, it should be |
| 95 | /// noted with this interface. |
| 96 | void ObjectCodeEmitter::addRelocation(const MachineRelocation& relocation) { |
| 97 | BO->addRelocation(relocation); |
| 98 | } |
| 99 | |
| 100 | /// StartMachineBasicBlock - This should be called by the target when a new |
| 101 | /// basic block is about to be emitted. This way the MCE knows where the |
| 102 | /// start of the block is, and can implement getMachineBasicBlockAddress. |
| 103 | void ObjectCodeEmitter::StartMachineBasicBlock(MachineBasicBlock *MBB) { |
| 104 | if (MBBLocations.size() <= (unsigned)MBB->getNumber()) |
| 105 | MBBLocations.resize((MBB->getNumber()+1)*2); |
| 106 | MBBLocations[MBB->getNumber()] = getCurrentPCOffset(); |
| 107 | } |
| 108 | |
| 109 | /// getMachineBasicBlockAddress - Return the address of the specified |
| 110 | /// MachineBasicBlock, only usable after the label for the MBB has been |
| 111 | /// emitted. |
| 112 | uintptr_t |
| 113 | ObjectCodeEmitter::getMachineBasicBlockAddress(MachineBasicBlock *MBB) const { |
| 114 | assert(MBBLocations.size() > (unsigned)MBB->getNumber() && |
| 115 | MBBLocations[MBB->getNumber()] && "MBB not emitted!"); |
| 116 | return MBBLocations[MBB->getNumber()]; |
| 117 | } |
| 118 | |
| 119 | /// getJumpTableEntryAddress - Return the address of the jump table with index |
| 120 | /// 'Index' in the function that last called initJumpTableInfo. |
| 121 | uintptr_t ObjectCodeEmitter::getJumpTableEntryAddress(unsigned Index) const { |
| 122 | assert(JTLocations.size() > Index && "JT not emitted!"); |
| 123 | return JTLocations[Index]; |
| 124 | } |
| 125 | |
| 126 | /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in |
| 127 | /// the constant pool that was last emitted with the emitConstantPool method. |
| 128 | uintptr_t ObjectCodeEmitter::getConstantPoolEntryAddress(unsigned Index) const { |
| 129 | assert(CPLocations.size() > Index && "CP not emitted!"); |
| 130 | return CPLocations[Index]; |
| 131 | } |
| 132 | |
| 133 | /// getConstantPoolEntrySection - Return the section of the 'Index' entry in |
| 134 | /// the constant pool that was last emitted with the emitConstantPool method. |
| 135 | uintptr_t ObjectCodeEmitter::getConstantPoolEntrySection(unsigned Index) const { |
| 136 | assert(CPSections.size() > Index && "CP not emitted!"); |
| 137 | return CPSections[Index]; |
| 138 | } |
| 139 | |
Bruno Cardoso Lopes | 8ae058a | 2009-07-06 05:16:40 +0000 | [diff] [blame] | 140 | } // end namespace llvm |
| 141 | |