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 | |
| 58 | /// emitAlignment - Move the CurBufferPtr pointer up the the specified |
| 59 | /// alignment (saturated to BufferEnd of course). |
| 60 | void ObjectCodeEmitter::emitAlignment(unsigned Alignment /* 0 */, |
| 61 | uint8_t fill /* 0 */) { |
| 62 | BO->emitAlignment(Alignment, fill); |
| 63 | } |
| 64 | |
| 65 | /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be |
| 66 | /// written to the data stream. |
| 67 | void ObjectCodeEmitter::emitULEB128Bytes(uint64_t Value) { |
| 68 | BO->emitULEB128Bytes(Value); |
| 69 | } |
| 70 | |
| 71 | /// emitSLEB128Bytes - This callback is invoked when a SLEB128 needs to be |
| 72 | /// written to the data stream. |
| 73 | void ObjectCodeEmitter::emitSLEB128Bytes(uint64_t Value) { |
| 74 | BO->emitSLEB128Bytes(Value); |
| 75 | } |
| 76 | |
| 77 | /// emitString - This callback is invoked when a String needs to be |
| 78 | /// written to the data stream. |
| 79 | void ObjectCodeEmitter::emitString(const std::string &String) { |
| 80 | BO->emitString(String); |
| 81 | } |
| 82 | |
| 83 | /// getCurrentPCValue - This returns the address that the next emitted byte |
| 84 | /// will be output to. |
| 85 | uintptr_t ObjectCodeEmitter::getCurrentPCValue() const { |
| 86 | return BO->getCurrentPCOffset(); |
| 87 | } |
| 88 | |
| 89 | /// getCurrentPCOffset - Return the offset from the start of the emitted |
| 90 | /// buffer that we are currently writing to. |
| 91 | uintptr_t ObjectCodeEmitter::getCurrentPCOffset() const { |
| 92 | return BO->getCurrentPCOffset(); |
| 93 | } |
| 94 | |
| 95 | /// addRelocation - Whenever a relocatable address is needed, it should be |
| 96 | /// noted with this interface. |
| 97 | void ObjectCodeEmitter::addRelocation(const MachineRelocation& relocation) { |
| 98 | BO->addRelocation(relocation); |
| 99 | } |
| 100 | |
| 101 | /// StartMachineBasicBlock - This should be called by the target when a new |
| 102 | /// basic block is about to be emitted. This way the MCE knows where the |
| 103 | /// start of the block is, and can implement getMachineBasicBlockAddress. |
| 104 | void ObjectCodeEmitter::StartMachineBasicBlock(MachineBasicBlock *MBB) { |
| 105 | if (MBBLocations.size() <= (unsigned)MBB->getNumber()) |
| 106 | MBBLocations.resize((MBB->getNumber()+1)*2); |
| 107 | MBBLocations[MBB->getNumber()] = getCurrentPCOffset(); |
| 108 | } |
| 109 | |
| 110 | /// getMachineBasicBlockAddress - Return the address of the specified |
| 111 | /// MachineBasicBlock, only usable after the label for the MBB has been |
| 112 | /// emitted. |
| 113 | uintptr_t |
| 114 | ObjectCodeEmitter::getMachineBasicBlockAddress(MachineBasicBlock *MBB) const { |
| 115 | assert(MBBLocations.size() > (unsigned)MBB->getNumber() && |
| 116 | MBBLocations[MBB->getNumber()] && "MBB not emitted!"); |
| 117 | return MBBLocations[MBB->getNumber()]; |
| 118 | } |
| 119 | |
| 120 | /// getJumpTableEntryAddress - Return the address of the jump table with index |
| 121 | /// 'Index' in the function that last called initJumpTableInfo. |
| 122 | uintptr_t ObjectCodeEmitter::getJumpTableEntryAddress(unsigned Index) const { |
| 123 | assert(JTLocations.size() > Index && "JT not emitted!"); |
| 124 | return JTLocations[Index]; |
| 125 | } |
| 126 | |
| 127 | /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in |
| 128 | /// the constant pool that was last emitted with the emitConstantPool method. |
| 129 | uintptr_t ObjectCodeEmitter::getConstantPoolEntryAddress(unsigned Index) const { |
| 130 | assert(CPLocations.size() > Index && "CP not emitted!"); |
| 131 | return CPLocations[Index]; |
| 132 | } |
| 133 | |
| 134 | /// getConstantPoolEntrySection - Return the section of the 'Index' entry in |
| 135 | /// the constant pool that was last emitted with the emitConstantPool method. |
| 136 | uintptr_t ObjectCodeEmitter::getConstantPoolEntrySection(unsigned Index) const { |
| 137 | assert(CPSections.size() > Index && "CP not emitted!"); |
| 138 | return CPSections[Index]; |
| 139 | } |
| 140 | |
| 141 | } // end namespace llvm |
| 142 | |