LLVM API Documentation
00001 //===-- llvm/CodeGen/MachineCodeEmitter.h - Code emission -------*- 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 // This file defines an abstract interface that is used by the machine code 00011 // emission framework to output the code. This allows machine code emission to 00012 // be separated from concerns such as resolution of call targets, and where the 00013 // machine code will be written (memory or disk, f.e.). 00014 // 00015 //===----------------------------------------------------------------------===// 00016 00017 #ifndef LLVM_CODEGEN_MACHINECODEEMITTER_H 00018 #define LLVM_CODEGEN_MACHINECODEEMITTER_H 00019 00020 #include "llvm/Support/DataTypes.h" 00021 #include "llvm/Support/DebugLoc.h" 00022 00023 namespace llvm { 00024 00025 class MachineBasicBlock; 00026 class MachineConstantPool; 00027 class MachineJumpTableInfo; 00028 class MachineFunction; 00029 class MachineModuleInfo; 00030 class MachineRelocation; 00031 class Value; 00032 class GlobalValue; 00033 class Function; 00034 00035 /// MachineCodeEmitter - This class defines two sorts of methods: those for 00036 /// emitting the actual bytes of machine code, and those for emitting auxillary 00037 /// structures, such as jump tables, relocations, etc. 00038 /// 00039 /// Emission of machine code is complicated by the fact that we don't (in 00040 /// general) know the size of the machine code that we're about to emit before 00041 /// we emit it. As such, we preallocate a certain amount of memory, and set the 00042 /// BufferBegin/BufferEnd pointers to the start and end of the buffer. As we 00043 /// emit machine instructions, we advance the CurBufferPtr to indicate the 00044 /// location of the next byte to emit. In the case of a buffer overflow (we 00045 /// need to emit more machine code than we have allocated space for), the 00046 /// CurBufferPtr will saturate to BufferEnd and ignore stores. Once the entire 00047 /// function has been emitted, the overflow condition is checked, and if it has 00048 /// occurred, more memory is allocated, and we reemit the code into it. 00049 /// 00050 class MachineCodeEmitter { 00051 protected: 00052 /// BufferBegin/BufferEnd - Pointers to the start and end of the memory 00053 /// allocated for this code buffer. 00054 uint8_t *BufferBegin, *BufferEnd; 00055 00056 /// CurBufferPtr - Pointer to the next byte of memory to fill when emitting 00057 /// code. This is guranteed to be in the range [BufferBegin,BufferEnd]. If 00058 /// this pointer is at BufferEnd, it will never move due to code emission, and 00059 /// all code emission requests will be ignored (this is the buffer overflow 00060 /// condition). 00061 uint8_t *CurBufferPtr; 00062 00063 public: 00064 virtual ~MachineCodeEmitter() {} 00065 00066 /// startFunction - This callback is invoked when the specified function is 00067 /// about to be code generated. This initializes the BufferBegin/End/Ptr 00068 /// fields. 00069 /// 00070 virtual void startFunction(MachineFunction &F) = 0; 00071 00072 /// finishFunction - This callback is invoked when the specified function has 00073 /// finished code generation. If a buffer overflow has occurred, this method 00074 /// returns true (the callee is required to try again), otherwise it returns 00075 /// false. 00076 /// 00077 virtual bool finishFunction(MachineFunction &F) = 0; 00078 00079 /// emitByte - This callback is invoked when a byte needs to be written to the 00080 /// output stream. 00081 /// 00082 void emitByte(uint8_t B) { 00083 if (CurBufferPtr != BufferEnd) 00084 *CurBufferPtr++ = B; 00085 } 00086 00087 /// emitWordLE - This callback is invoked when a 32-bit word needs to be 00088 /// written to the output stream in little-endian format. 00089 /// 00090 void emitWordLE(uint32_t W) { 00091 if (4 <= BufferEnd-CurBufferPtr) { 00092 *CurBufferPtr++ = (uint8_t)(W >> 0); 00093 *CurBufferPtr++ = (uint8_t)(W >> 8); 00094 *CurBufferPtr++ = (uint8_t)(W >> 16); 00095 *CurBufferPtr++ = (uint8_t)(W >> 24); 00096 } else { 00097 CurBufferPtr = BufferEnd; 00098 } 00099 } 00100 00101 /// emitWordBE - This callback is invoked when a 32-bit word needs to be 00102 /// written to the output stream in big-endian format. 00103 /// 00104 void emitWordBE(uint32_t W) { 00105 if (4 <= BufferEnd-CurBufferPtr) { 00106 *CurBufferPtr++ = (uint8_t)(W >> 24); 00107 *CurBufferPtr++ = (uint8_t)(W >> 16); 00108 *CurBufferPtr++ = (uint8_t)(W >> 8); 00109 *CurBufferPtr++ = (uint8_t)(W >> 0); 00110 } else { 00111 CurBufferPtr = BufferEnd; 00112 } 00113 } 00114 00115 /// emitDWordLE - This callback is invoked when a 64-bit word needs to be 00116 /// written to the output stream in little-endian format. 00117 /// 00118 void emitDWordLE(uint64_t W) { 00119 if (8 <= BufferEnd-CurBufferPtr) { 00120 *CurBufferPtr++ = (uint8_t)(W >> 0); 00121 *CurBufferPtr++ = (uint8_t)(W >> 8); 00122 *CurBufferPtr++ = (uint8_t)(W >> 16); 00123 *CurBufferPtr++ = (uint8_t)(W >> 24); 00124 *CurBufferPtr++ = (uint8_t)(W >> 32); 00125 *CurBufferPtr++ = (uint8_t)(W >> 40); 00126 *CurBufferPtr++ = (uint8_t)(W >> 48); 00127 *CurBufferPtr++ = (uint8_t)(W >> 56); 00128 } else { 00129 CurBufferPtr = BufferEnd; 00130 } 00131 } 00132 00133 /// emitDWordBE - This callback is invoked when a 64-bit word needs to be 00134 /// written to the output stream in big-endian format. 00135 /// 00136 void emitDWordBE(uint64_t W) { 00137 if (8 <= BufferEnd-CurBufferPtr) { 00138 *CurBufferPtr++ = (uint8_t)(W >> 56); 00139 *CurBufferPtr++ = (uint8_t)(W >> 48); 00140 *CurBufferPtr++ = (uint8_t)(W >> 40); 00141 *CurBufferPtr++ = (uint8_t)(W >> 32); 00142 *CurBufferPtr++ = (uint8_t)(W >> 24); 00143 *CurBufferPtr++ = (uint8_t)(W >> 16); 00144 *CurBufferPtr++ = (uint8_t)(W >> 8); 00145 *CurBufferPtr++ = (uint8_t)(W >> 0); 00146 } else { 00147 CurBufferPtr = BufferEnd; 00148 } 00149 } 00150 00151 /// emitAlignment - Move the CurBufferPtr pointer up the the specified 00152 /// alignment (saturated to BufferEnd of course). 00153 void emitAlignment(unsigned Alignment) { 00154 if (Alignment == 0) Alignment = 1; 00155 00156 if(Alignment <= (uintptr_t)(BufferEnd-CurBufferPtr)) { 00157 // Move the current buffer ptr up to the specified alignment. 00158 CurBufferPtr = 00159 (uint8_t*)(((uintptr_t)CurBufferPtr+Alignment-1) & 00160 ~(uintptr_t)(Alignment-1)); 00161 } else { 00162 CurBufferPtr = BufferEnd; 00163 } 00164 } 00165 00166 00167 /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be 00168 /// written to the output stream. 00169 void emitULEB128Bytes(uint64_t Value) { 00170 do { 00171 uint8_t Byte = Value & 0x7f; 00172 Value >>= 7; 00173 if (Value) Byte |= 0x80; 00174 emitByte(Byte); 00175 } while (Value); 00176 } 00177 00178 /// emitSLEB128Bytes - This callback is invoked when a SLEB128 needs to be 00179 /// written to the output stream. 00180 void emitSLEB128Bytes(uint64_t Value) { 00181 uint64_t Sign = Value >> (8 * sizeof(Value) - 1); 00182 bool IsMore; 00183 00184 do { 00185 uint8_t Byte = Value & 0x7f; 00186 Value >>= 7; 00187 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0; 00188 if (IsMore) Byte |= 0x80; 00189 emitByte(Byte); 00190 } while (IsMore); 00191 } 00192 00193 /// emitString - This callback is invoked when a String needs to be 00194 /// written to the output stream. 00195 void emitString(const std::string &String) { 00196 for (unsigned i = 0, N = static_cast<unsigned>(String.size()); 00197 i < N; ++i) { 00198 uint8_t C = String[i]; 00199 emitByte(C); 00200 } 00201 emitByte(0); 00202 } 00203 00204 /// emitInt32 - Emit a int32 directive. 00205 void emitInt32(int32_t Value) { 00206 if (4 <= BufferEnd-CurBufferPtr) { 00207 *((uint32_t*)CurBufferPtr) = Value; 00208 CurBufferPtr += 4; 00209 } else { 00210 CurBufferPtr = BufferEnd; 00211 } 00212 } 00213 00214 /// emitInt64 - Emit a int64 directive. 00215 void emitInt64(uint64_t Value) { 00216 if (8 <= BufferEnd-CurBufferPtr) { 00217 *((uint64_t*)CurBufferPtr) = Value; 00218 CurBufferPtr += 8; 00219 } else { 00220 CurBufferPtr = BufferEnd; 00221 } 00222 } 00223 00224 /// emitInt32At - Emit the Int32 Value in Addr. 00225 void emitInt32At(uintptr_t *Addr, uintptr_t Value) { 00226 if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd) 00227 (*(uint32_t*)Addr) = (uint32_t)Value; 00228 } 00229 00230 /// emitInt64At - Emit the Int64 Value in Addr. 00231 void emitInt64At(uintptr_t *Addr, uintptr_t Value) { 00232 if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd) 00233 (*(uint64_t*)Addr) = (uint64_t)Value; 00234 } 00235 00236 /// processDebugLoc - Records debug location information about a 00237 /// MachineInstruction. This is called before emitting any bytes associated 00238 /// with the instruction. Even if successive instructions have the same debug 00239 /// location, this method will be called for each one. 00240 virtual void processDebugLoc(DebugLoc DL) {} 00241 00242 /// emitLabel - Emits a label 00243 virtual void emitLabel(uint64_t LabelID) = 0; 00244 00245 /// allocateSpace - Allocate a block of space in the current output buffer, 00246 /// returning null (and setting conditions to indicate buffer overflow) on 00247 /// failure. Alignment is the alignment in bytes of the buffer desired. 00248 virtual void *allocateSpace(uintptr_t Size, unsigned Alignment) { 00249 emitAlignment(Alignment); 00250 void *Result; 00251 00252 // Check for buffer overflow. 00253 if (Size >= (uintptr_t)(BufferEnd-CurBufferPtr)) { 00254 CurBufferPtr = BufferEnd; 00255 Result = 0; 00256 } else { 00257 // Allocate the space. 00258 Result = CurBufferPtr; 00259 CurBufferPtr += Size; 00260 } 00261 00262 return Result; 00263 } 00264 00265 /// StartMachineBasicBlock - This should be called by the target when a new 00266 /// basic block is about to be emitted. This way the MCE knows where the 00267 /// start of the block is, and can implement getMachineBasicBlockAddress. 00268 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) = 0; 00269 00270 /// getCurrentPCValue - This returns the address that the next emitted byte 00271 /// will be output to. 00272 /// 00273 virtual uintptr_t getCurrentPCValue() const { 00274 return (uintptr_t)CurBufferPtr; 00275 } 00276 00277 /// getCurrentPCOffset - Return the offset from the start of the emitted 00278 /// buffer that we are currently writing to. 00279 virtual uintptr_t getCurrentPCOffset() const { 00280 return CurBufferPtr-BufferBegin; 00281 } 00282 00283 /// earlyResolveAddresses - True if the code emitter can use symbol addresses 00284 /// during code emission time. The JIT is capable of doing this because it 00285 /// creates jump tables or constant pools in memory on the fly while the 00286 /// object code emitters rely on a linker to have real addresses and should 00287 /// use relocations instead. 00288 virtual bool earlyResolveAddresses() const = 0; 00289 00290 /// addRelocation - Whenever a relocatable address is needed, it should be 00291 /// noted with this interface. 00292 virtual void addRelocation(const MachineRelocation &MR) = 0; 00293 00294 /// FIXME: These should all be handled with relocations! 00295 00296 /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in 00297 /// the constant pool that was last emitted with the emitConstantPool method. 00298 /// 00299 virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const = 0; 00300 00301 /// getJumpTableEntryAddress - Return the address of the jump table with index 00302 /// 'Index' in the function that last called initJumpTableInfo. 00303 /// 00304 virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const = 0; 00305 00306 /// getMachineBasicBlockAddress - Return the address of the specified 00307 /// MachineBasicBlock, only usable after the label for the MBB has been 00308 /// emitted. 00309 /// 00310 virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const= 0; 00311 00312 /// getLabelAddress - Return the address of the specified LabelID, only usable 00313 /// after the LabelID has been emitted. 00314 /// 00315 virtual uintptr_t getLabelAddress(uint64_t LabelID) const = 0; 00316 00317 /// Specifies the MachineModuleInfo object. This is used for exception handling 00318 /// purposes. 00319 virtual void setModuleInfo(MachineModuleInfo* Info) = 0; 00320 }; 00321 00322 } // End llvm namespace 00323 00324 #endif