LLVM API Documentation
00001 //===-- llvm/CodeGen/ObjectCodeEmitter.h - Object Code Emitter -*- 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 // Generalized Object Code Emitter, works with ObjectModule and BinaryObject. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_CODEGEN_OBJECTCODEEMITTER_H 00015 #define LLVM_CODEGEN_OBJECTCODEEMITTER_H 00016 00017 #include "llvm/CodeGen/MachineCodeEmitter.h" 00018 00019 namespace llvm { 00020 00021 class BinaryObject; 00022 class MachineBasicBlock; 00023 class MachineCodeEmitter; 00024 class MachineFunction; 00025 class MachineConstantPool; 00026 class MachineJumpTableInfo; 00027 class MachineModuleInfo; 00028 00029 class ObjectCodeEmitter : public MachineCodeEmitter { 00030 protected: 00031 00032 /// Binary Object (Section or Segment) we are emitting to. 00033 BinaryObject *BO; 00034 00035 /// MBBLocations - This vector is a mapping from MBB ID's to their address. 00036 /// It is filled in by the StartMachineBasicBlock callback and queried by 00037 /// the getMachineBasicBlockAddress callback. 00038 std::vector<uintptr_t> MBBLocations; 00039 00040 /// LabelLocations - This vector is a mapping from Label ID's to their 00041 /// address. 00042 std::vector<uintptr_t> LabelLocations; 00043 00044 /// CPLocations - This is a map of constant pool indices to offsets from the 00045 /// start of the section for that constant pool index. 00046 std::vector<uintptr_t> CPLocations; 00047 00048 /// CPSections - This is a map of constant pool indices to the Section 00049 /// containing the constant pool entry for that index. 00050 std::vector<uintptr_t> CPSections; 00051 00052 /// JTLocations - This is a map of jump table indices to offsets from the 00053 /// start of the section for that jump table index. 00054 std::vector<uintptr_t> JTLocations; 00055 00056 public: 00057 ObjectCodeEmitter(); 00058 ObjectCodeEmitter(BinaryObject *bo); 00059 virtual ~ObjectCodeEmitter(); 00060 00061 /// setBinaryObject - set the BinaryObject we are writting to 00062 void setBinaryObject(BinaryObject *bo); 00063 00064 /// emitByte - This callback is invoked when a byte needs to be 00065 /// written to the data stream, without buffer overflow testing. 00066 void emitByte(uint8_t B); 00067 00068 /// emitWordLE - This callback is invoked when a 32-bit word needs to be 00069 /// written to the data stream in little-endian format. 00070 void emitWordLE(uint32_t W); 00071 00072 /// emitWordBE - This callback is invoked when a 32-bit word needs to be 00073 /// written to the data stream in big-endian format. 00074 void emitWordBE(uint32_t W); 00075 00076 /// emitDWordLE - This callback is invoked when a 64-bit word needs to be 00077 /// written to the data stream in little-endian format. 00078 void emitDWordLE(uint64_t W); 00079 00080 /// emitDWordBE - This callback is invoked when a 64-bit word needs to be 00081 /// written to the data stream in big-endian format. 00082 void emitDWordBE(uint64_t W); 00083 00084 /// emitAlignment - Move the CurBufferPtr pointer up the the specified 00085 /// alignment (saturated to BufferEnd of course). 00086 void emitAlignment(unsigned Alignment = 0, uint8_t fill = 0); 00087 00088 /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be 00089 /// written to the data stream. 00090 void emitULEB128Bytes(uint64_t Value); 00091 00092 /// emitSLEB128Bytes - This callback is invoked when a SLEB128 needs to be 00093 /// written to the data stream. 00094 void emitSLEB128Bytes(uint64_t Value); 00095 00096 /// emitString - This callback is invoked when a String needs to be 00097 /// written to the data stream. 00098 void emitString(const std::string &String); 00099 00100 /// getCurrentPCValue - This returns the address that the next emitted byte 00101 /// will be output to. 00102 uintptr_t getCurrentPCValue() const; 00103 00104 /// getCurrentPCOffset - Return the offset from the start of the emitted 00105 /// buffer that we are currently writing to. 00106 uintptr_t getCurrentPCOffset() const; 00107 00108 /// addRelocation - Whenever a relocatable address is needed, it should be 00109 /// noted with this interface. 00110 void addRelocation(const MachineRelocation& relocation); 00111 00112 /// earlyResolveAddresses - True if the code emitter can use symbol addresses 00113 /// during code emission time. The JIT is capable of doing this because it 00114 /// creates jump tables or constant pools in memory on the fly while the 00115 /// object code emitters rely on a linker to have real addresses and should 00116 /// use relocations instead. 00117 bool earlyResolveAddresses() const { return false; } 00118 00119 /// startFunction - This callback is invoked when the specified function is 00120 /// about to be code generated. This initializes the BufferBegin/End/Ptr 00121 /// fields. 00122 virtual void startFunction(MachineFunction &F) = 0; 00123 00124 /// finishFunction - This callback is invoked when the specified function has 00125 /// finished code generation. If a buffer overflow has occurred, this method 00126 /// returns true (the callee is required to try again), otherwise it returns 00127 /// false. 00128 virtual bool finishFunction(MachineFunction &F) = 0; 00129 00130 /// StartMachineBasicBlock - This should be called by the target when a new 00131 /// basic block is about to be emitted. This way the MCE knows where the 00132 /// start of the block is, and can implement getMachineBasicBlockAddress. 00133 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB); 00134 00135 /// getMachineBasicBlockAddress - Return the address of the specified 00136 /// MachineBasicBlock, only usable after the label for the MBB has been 00137 /// emitted. 00138 virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const; 00139 00140 /// emitLabel - Emits a label 00141 virtual void emitLabel(uint64_t LabelID) = 0; 00142 00143 /// getLabelAddress - Return the address of the specified LabelID, only usable 00144 /// after the LabelID has been emitted. 00145 virtual uintptr_t getLabelAddress(uint64_t LabelID) const = 0; 00146 00147 /// emitJumpTables - Emit all the jump tables for a given jump table info 00148 /// record to the appropriate section. 00149 virtual void emitJumpTables(MachineJumpTableInfo *MJTI) = 0; 00150 00151 /// getJumpTableEntryAddress - Return the address of the jump table with index 00152 /// 'Index' in the function that last called initJumpTableInfo. 00153 virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const; 00154 00155 /// emitConstantPool - For each constant pool entry, figure out which section 00156 /// the constant should live in, allocate space for it, and emit it to the 00157 /// Section data buffer. 00158 virtual void emitConstantPool(MachineConstantPool *MCP) = 0; 00159 00160 /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in 00161 /// the constant pool that was last emitted with the emitConstantPool method. 00162 virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const; 00163 00164 /// getConstantPoolEntrySection - Return the section of the 'Index' entry in 00165 /// the constant pool that was last emitted with the emitConstantPool method. 00166 virtual uintptr_t getConstantPoolEntrySection(unsigned Index) const; 00167 00168 /// Specifies the MachineModuleInfo object. This is used for exception handling 00169 /// purposes. 00170 virtual void setModuleInfo(MachineModuleInfo* Info) = 0; 00171 // to be implemented or depreciated with MachineModuleInfo 00172 00173 }; // end class ObjectCodeEmitter 00174 00175 } // end namespace llvm 00176 00177 #endif 00178