LLVM API Documentation
00001 //===-- llvm/CodeGen/ObjectCodeEmitter.cpp -------------------- -*- 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 #include "llvm/CodeGen/BinaryObject.h" 00011 #include "llvm/CodeGen/MachineBasicBlock.h" 00012 #include "llvm/CodeGen/MachineRelocation.h" 00013 #include "llvm/CodeGen/ObjectCodeEmitter.h" 00014 00015 //===----------------------------------------------------------------------===// 00016 // ObjectCodeEmitter Implementation 00017 //===----------------------------------------------------------------------===// 00018 00019 namespace llvm { 00020 00021 ObjectCodeEmitter::ObjectCodeEmitter() : BO(0) {} 00022 ObjectCodeEmitter::ObjectCodeEmitter(BinaryObject *bo) : BO(bo) {} 00023 ObjectCodeEmitter::~ObjectCodeEmitter() {} 00024 00025 /// setBinaryObject - set the BinaryObject we are writting to 00026 void ObjectCodeEmitter::setBinaryObject(BinaryObject *bo) { BO = bo; } 00027 00028 /// emitByte - This callback is invoked when a byte needs to be 00029 /// written to the data stream, without buffer overflow testing. 00030 void ObjectCodeEmitter::emitByte(uint8_t B) { 00031 BO->emitByte(B); 00032 } 00033 00034 /// emitWordLE - This callback is invoked when a 32-bit word needs to be 00035 /// written to the data stream in little-endian format. 00036 void ObjectCodeEmitter::emitWordLE(uint32_t W) { 00037 BO->emitWordLE(W); 00038 } 00039 00040 /// emitWordBE - This callback is invoked when a 32-bit word needs to be 00041 /// written to the data stream in big-endian format. 00042 void ObjectCodeEmitter::emitWordBE(uint32_t W) { 00043 BO->emitWordBE(W); 00044 } 00045 00046 /// emitDWordLE - This callback is invoked when a 64-bit word needs to be 00047 /// written to the data stream in little-endian format. 00048 void ObjectCodeEmitter::emitDWordLE(uint64_t W) { 00049 BO->emitDWordLE(W); 00050 } 00051 00052 /// emitDWordBE - This callback is invoked when a 64-bit word needs to be 00053 /// written to the data stream in big-endian format. 00054 void ObjectCodeEmitter::emitDWordBE(uint64_t W) { 00055 BO->emitDWordBE(W); 00056 } 00057 00058 /// emitAlignment - Align 'BO' to the necessary alignment boundary. 00059 void ObjectCodeEmitter::emitAlignment(unsigned Alignment /* 0 */, 00060 uint8_t fill /* 0 */) { 00061 BO->emitAlignment(Alignment, fill); 00062 } 00063 00064 /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be 00065 /// written to the data stream. 00066 void ObjectCodeEmitter::emitULEB128Bytes(uint64_t Value) { 00067 BO->emitULEB128Bytes(Value); 00068 } 00069 00070 /// emitSLEB128Bytes - This callback is invoked when a SLEB128 needs to be 00071 /// written to the data stream. 00072 void ObjectCodeEmitter::emitSLEB128Bytes(uint64_t Value) { 00073 BO->emitSLEB128Bytes(Value); 00074 } 00075 00076 /// emitString - This callback is invoked when a String needs to be 00077 /// written to the data stream. 00078 void ObjectCodeEmitter::emitString(const std::string &String) { 00079 BO->emitString(String); 00080 } 00081 00082 /// getCurrentPCValue - This returns the address that the next emitted byte 00083 /// will be output to. 00084 uintptr_t ObjectCodeEmitter::getCurrentPCValue() const { 00085 return BO->getCurrentPCOffset(); 00086 } 00087 00088 /// getCurrentPCOffset - Return the offset from the start of the emitted 00089 /// buffer that we are currently writing to. 00090 uintptr_t ObjectCodeEmitter::getCurrentPCOffset() const { 00091 return BO->getCurrentPCOffset(); 00092 } 00093 00094 /// addRelocation - Whenever a relocatable address is needed, it should be 00095 /// noted with this interface. 00096 void ObjectCodeEmitter::addRelocation(const MachineRelocation& relocation) { 00097 BO->addRelocation(relocation); 00098 } 00099 00100 /// StartMachineBasicBlock - This should be called by the target when a new 00101 /// basic block is about to be emitted. This way the MCE knows where the 00102 /// start of the block is, and can implement getMachineBasicBlockAddress. 00103 void ObjectCodeEmitter::StartMachineBasicBlock(MachineBasicBlock *MBB) { 00104 if (MBBLocations.size() <= (unsigned)MBB->getNumber()) 00105 MBBLocations.resize((MBB->getNumber()+1)*2); 00106 MBBLocations[MBB->getNumber()] = getCurrentPCOffset(); 00107 } 00108 00109 /// getMachineBasicBlockAddress - Return the address of the specified 00110 /// MachineBasicBlock, only usable after the label for the MBB has been 00111 /// emitted. 00112 uintptr_t 00113 ObjectCodeEmitter::getMachineBasicBlockAddress(MachineBasicBlock *MBB) const { 00114 assert(MBBLocations.size() > (unsigned)MBB->getNumber() && 00115 MBBLocations[MBB->getNumber()] && "MBB not emitted!"); 00116 return MBBLocations[MBB->getNumber()]; 00117 } 00118 00119 /// getJumpTableEntryAddress - Return the address of the jump table with index 00120 /// 'Index' in the function that last called initJumpTableInfo. 00121 uintptr_t ObjectCodeEmitter::getJumpTableEntryAddress(unsigned Index) const { 00122 assert(JTLocations.size() > Index && "JT not emitted!"); 00123 return JTLocations[Index]; 00124 } 00125 00126 /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in 00127 /// the constant pool that was last emitted with the emitConstantPool method. 00128 uintptr_t ObjectCodeEmitter::getConstantPoolEntryAddress(unsigned Index) const { 00129 assert(CPLocations.size() > Index && "CP not emitted!"); 00130 return CPLocations[Index]; 00131 } 00132 00133 /// getConstantPoolEntrySection - Return the section of the 'Index' entry in 00134 /// the constant pool that was last emitted with the emitConstantPool method. 00135 uintptr_t ObjectCodeEmitter::getConstantPoolEntrySection(unsigned Index) const { 00136 assert(CPSections.size() > Index && "CP not emitted!"); 00137 return CPSections[Index]; 00138 } 00139 00140 } // end namespace llvm 00141