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