1 //===-- llvm/CodeGen/ObjectCodeEmitter.h - Object Code Emitter -*- 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 // Generalized Object Code Emitter, works with ObjectModule and BinaryObject. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_CODEGEN_OBJECTCODEEMITTER_H 15 #define LLVM_CODEGEN_OBJECTCODEEMITTER_H 16 17 #include "llvm/CodeGen/MachineCodeEmitter.h" 18 19 namespace llvm { 20 21 class BinaryObject; 22 class MachineBasicBlock; 23 class MachineCodeEmitter; 24 class MachineFunction; 25 class MachineConstantPool; 26 class MachineJumpTableInfo; 27 class MachineModuleInfo; 28 29 class ObjectCodeEmitter : public MachineCodeEmitter { 30 protected: 31 32 /// Binary Object (Section or Segment) we are emitting to. 33 BinaryObject *BO; 34 35 /// MBBLocations - This vector is a mapping from MBB ID's to their address. 36 /// It is filled in by the StartMachineBasicBlock callback and queried by 37 /// the getMachineBasicBlockAddress callback. 38 std::vector<uintptr_t> MBBLocations; 39 40 /// LabelLocations - This vector is a mapping from Label ID's to their 41 /// address. 42 std::vector<uintptr_t> LabelLocations; 43 44 /// CPLocations - This is a map of constant pool indices to offsets from the 45 /// start of the section for that constant pool index. 46 std::vector<uintptr_t> CPLocations; 47 48 /// CPSections - This is a map of constant pool indices to the Section 49 /// containing the constant pool entry for that index. 50 std::vector<uintptr_t> CPSections; 51 52 /// JTLocations - This is a map of jump table indices to offsets from the 53 /// start of the section for that jump table index. 54 std::vector<uintptr_t> JTLocations; 55 56 public: 57 ObjectCodeEmitter(); 58 ObjectCodeEmitter(BinaryObject *bo); 59 virtual ~ObjectCodeEmitter(); 60 61 /// setBinaryObject - set the BinaryObject we are writting to 62 void setBinaryObject(BinaryObject *bo); 63 64 /// emitByte - This callback is invoked when a byte needs to be 65 /// written to the data stream, without buffer overflow testing. 66 void emitByte(uint8_t B); 67 68 /// emitWordLE - This callback is invoked when a 32-bit word needs to be 69 /// written to the data stream in little-endian format. 70 void emitWordLE(uint32_t W); 71 72 /// emitWordBE - This callback is invoked when a 32-bit word needs to be 73 /// written to the data stream in big-endian format. 74 void emitWordBE(uint32_t W); 75 76 /// emitDWordLE - This callback is invoked when a 64-bit word needs to be 77 /// written to the data stream in little-endian format. 78 void emitDWordLE(uint64_t W); 79 80 /// emitDWordBE - This callback is invoked when a 64-bit word needs to be 81 /// written to the data stream in big-endian format. 82 void emitDWordBE(uint64_t W); 83 84 /// emitAlignment - Move the CurBufferPtr pointer up to the specified 85 /// alignment (saturated to BufferEnd of course). 86 void emitAlignment(unsigned Alignment = 0, uint8_t fill = 0); 87 88 /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be 89 /// written to the data stream. 90 void emitULEB128Bytes(uint64_t Value); 91 92 /// emitSLEB128Bytes - This callback is invoked when a SLEB128 needs to be 93 /// written to the data stream. 94 void emitSLEB128Bytes(uint64_t Value); 95 96 /// emitString - This callback is invoked when a String needs to be 97 /// written to the data stream. 98 void emitString(const std::string &String); 99 100 /// getCurrentPCValue - This returns the address that the next emitted byte 101 /// will be output to. 102 uintptr_t getCurrentPCValue() const; 103 104 /// getCurrentPCOffset - Return the offset from the start of the emitted 105 /// buffer that we are currently writing to. 106 uintptr_t getCurrentPCOffset() const; 107 108 /// addRelocation - Whenever a relocatable address is needed, it should be 109 /// noted with this interface. 110 void addRelocation(const MachineRelocation& relocation); 111 112 /// earlyResolveAddresses - True if the code emitter can use symbol addresses 113 /// during code emission time. The JIT is capable of doing this because it 114 /// creates jump tables or constant pools in memory on the fly while the 115 /// object code emitters rely on a linker to have real addresses and should 116 /// use relocations instead. earlyResolveAddresses()117 bool earlyResolveAddresses() const { return false; } 118 119 /// startFunction - This callback is invoked when the specified function is 120 /// about to be code generated. This initializes the BufferBegin/End/Ptr 121 /// fields. 122 virtual void startFunction(MachineFunction &F) = 0; 123 124 /// finishFunction - This callback is invoked when the specified function has 125 /// finished code generation. If a buffer overflow has occurred, this method 126 /// returns true (the callee is required to try again), otherwise it returns 127 /// false. 128 virtual bool finishFunction(MachineFunction &F) = 0; 129 130 /// StartMachineBasicBlock - This should be called by the target when a new 131 /// basic block is about to be emitted. This way the MCE knows where the 132 /// start of the block is, and can implement getMachineBasicBlockAddress. 133 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB); 134 135 /// getMachineBasicBlockAddress - Return the address of the specified 136 /// MachineBasicBlock, only usable after the label for the MBB has been 137 /// emitted. 138 virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const; 139 140 /// emitJumpTables - Emit all the jump tables for a given jump table info 141 /// record to the appropriate section. 142 virtual void emitJumpTables(MachineJumpTableInfo *MJTI) = 0; 143 144 /// getJumpTableEntryAddress - Return the address of the jump table with index 145 /// 'Index' in the function that last called initJumpTableInfo. 146 virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const; 147 148 /// emitConstantPool - For each constant pool entry, figure out which section 149 /// the constant should live in, allocate space for it, and emit it to the 150 /// Section data buffer. 151 virtual void emitConstantPool(MachineConstantPool *MCP) = 0; 152 153 /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in 154 /// the constant pool that was last emitted with the emitConstantPool method. 155 virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const; 156 157 /// getConstantPoolEntrySection - Return the section of the 'Index' entry in 158 /// the constant pool that was last emitted with the emitConstantPool method. 159 virtual uintptr_t getConstantPoolEntrySection(unsigned Index) const; 160 161 /// Specifies the MachineModuleInfo object. This is used for exception handling 162 /// purposes. 163 virtual void setModuleInfo(MachineModuleInfo* Info) = 0; 164 // to be implemented or depreciated with MachineModuleInfo 165 166 }; // end class ObjectCodeEmitter 167 168 } // end namespace llvm 169 170 #endif 171 172