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 ObjectCodeEmitter()21ObjectCodeEmitter::ObjectCodeEmitter() : BO(0) {} ObjectCodeEmitter(BinaryObject * bo)22ObjectCodeEmitter::ObjectCodeEmitter(BinaryObject *bo) : BO(bo) {} ~ObjectCodeEmitter()23ObjectCodeEmitter::~ObjectCodeEmitter() {} 24 25 /// setBinaryObject - set the BinaryObject we are writting to setBinaryObject(BinaryObject * bo)26void 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. emitByte(uint8_t B)30void 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. emitWordLE(uint32_t W)36void 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. emitWordBE(uint32_t W)42void 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. emitDWordLE(uint64_t W)48void 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. emitDWordBE(uint64_t W)54void ObjectCodeEmitter::emitDWordBE(uint64_t W) { 55 BO->emitDWordBE(W); 56 } 57 58 /// emitAlignment - Align 'BO' to the necessary alignment boundary. emitAlignment(unsigned Alignment,uint8_t fill)59void 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. emitULEB128Bytes(uint64_t Value)66void 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. emitSLEB128Bytes(uint64_t Value)72void 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. emitString(const std::string & String)78void 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. getCurrentPCValue() const84uintptr_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. getCurrentPCOffset() const90uintptr_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. addRelocation(const MachineRelocation & relocation)96void 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. StartMachineBasicBlock(MachineBasicBlock * MBB)103void 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 getMachineBasicBlockAddress(MachineBasicBlock * MBB) const113ObjectCodeEmitter::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. getJumpTableEntryAddress(unsigned Index) const121uintptr_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. getConstantPoolEntryAddress(unsigned Index) const128uintptr_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. getConstantPoolEntrySection(unsigned Index) const135uintptr_t ObjectCodeEmitter::getConstantPoolEntrySection(unsigned Index) const { 136 assert(CPSections.size() > Index && "CP not emitted!"); 137 return CPSections[Index]; 138 } 139 140 } // end namespace llvm 141 142