LLVM API Documentation

MachineCodeEmitter.h
Go to the documentation of this file.
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/IR/DebugLoc.h"
00021 #include "llvm/Support/DataTypes.h"
00022 #include <string>
00023 
00024 namespace llvm {
00025 
00026 class MachineBasicBlock;
00027 class MachineConstantPool;
00028 class MachineJumpTableInfo;
00029 class MachineFunction;
00030 class MachineModuleInfo;
00031 class MachineRelocation;
00032 class Value;
00033 class GlobalValue;
00034 class Function;
00035 class MCSymbol;
00036 
00037 /// MachineCodeEmitter - This class defines two sorts of methods: those for
00038 /// emitting the actual bytes of machine code, and those for emitting auxiliary
00039 /// structures, such as jump tables, relocations, etc.
00040 ///
00041 /// Emission of machine code is complicated by the fact that we don't (in
00042 /// general) know the size of the machine code that we're about to emit before
00043 /// we emit it.  As such, we preallocate a certain amount of memory, and set the
00044 /// BufferBegin/BufferEnd pointers to the start and end of the buffer.  As we
00045 /// emit machine instructions, we advance the CurBufferPtr to indicate the
00046 /// location of the next byte to emit.  In the case of a buffer overflow (we
00047 /// need to emit more machine code than we have allocated space for), the
00048 /// CurBufferPtr will saturate to BufferEnd and ignore stores.  Once the entire
00049 /// function has been emitted, the overflow condition is checked, and if it has
00050 /// occurred, more memory is allocated, and we reemit the code into it.
00051 /// 
00052 class MachineCodeEmitter {
00053   virtual void anchor();
00054 protected:
00055   /// BufferBegin/BufferEnd - Pointers to the start and end of the memory
00056   /// allocated for this code buffer.
00057   uint8_t *BufferBegin, *BufferEnd;
00058   /// CurBufferPtr - Pointer to the next byte of memory to fill when emitting
00059   /// code.  This is guaranteed to be in the range [BufferBegin,BufferEnd].  If
00060   /// this pointer is at BufferEnd, it will never move due to code emission, and
00061   /// all code emission requests will be ignored (this is the buffer overflow
00062   /// condition).
00063   uint8_t *CurBufferPtr;
00064 
00065 public:
00066   virtual ~MachineCodeEmitter() {}
00067 
00068   /// startFunction - This callback is invoked when the specified function is
00069   /// about to be code generated.  This initializes the BufferBegin/End/Ptr
00070   /// fields.
00071   ///
00072   virtual void startFunction(MachineFunction &F) = 0;
00073 
00074   /// finishFunction - This callback is invoked when the specified function has
00075   /// finished code generation.  If a buffer overflow has occurred, this method
00076   /// returns true (the callee is required to try again), otherwise it returns
00077   /// false.
00078   ///
00079   virtual bool finishFunction(MachineFunction &F) = 0;
00080 
00081   /// emitByte - This callback is invoked when a byte needs to be written to the
00082   /// output stream.
00083   ///
00084   void emitByte(uint8_t B) {
00085     if (CurBufferPtr != BufferEnd)
00086       *CurBufferPtr++ = B;
00087   }
00088 
00089   /// emitWordLE - This callback is invoked when a 32-bit word needs to be
00090   /// written to the output stream in little-endian format.
00091   ///
00092   void emitWordLE(uint32_t W) {
00093     if (4 <= BufferEnd-CurBufferPtr) {
00094       emitWordLEInto(CurBufferPtr, W);
00095     } else {
00096       CurBufferPtr = BufferEnd;
00097     }
00098   }
00099 
00100   /// emitWordLEInto - This callback is invoked when a 32-bit word needs to be
00101   /// written to an arbitrary buffer in little-endian format.  Buf must have at
00102   /// least 4 bytes of available space.
00103   ///
00104   static void emitWordLEInto(uint8_t *&Buf, uint32_t W) {
00105     *Buf++ = (uint8_t)(W >>  0);
00106     *Buf++ = (uint8_t)(W >>  8);
00107     *Buf++ = (uint8_t)(W >> 16);
00108     *Buf++ = (uint8_t)(W >> 24);
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(uint32_t W) {
00115     if (4 <= BufferEnd-CurBufferPtr) {
00116       *CurBufferPtr++ = (uint8_t)(W >> 24);
00117       *CurBufferPtr++ = (uint8_t)(W >> 16);
00118       *CurBufferPtr++ = (uint8_t)(W >>  8);
00119       *CurBufferPtr++ = (uint8_t)(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++ = (uint8_t)(W >>  0);
00131       *CurBufferPtr++ = (uint8_t)(W >>  8);
00132       *CurBufferPtr++ = (uint8_t)(W >> 16);
00133       *CurBufferPtr++ = (uint8_t)(W >> 24);
00134       *CurBufferPtr++ = (uint8_t)(W >> 32);
00135       *CurBufferPtr++ = (uint8_t)(W >> 40);
00136       *CurBufferPtr++ = (uint8_t)(W >> 48);
00137       *CurBufferPtr++ = (uint8_t)(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++ = (uint8_t)(W >> 56);
00149       *CurBufferPtr++ = (uint8_t)(W >> 48);
00150       *CurBufferPtr++ = (uint8_t)(W >> 40);
00151       *CurBufferPtr++ = (uint8_t)(W >> 32);
00152       *CurBufferPtr++ = (uint8_t)(W >> 24);
00153       *CurBufferPtr++ = (uint8_t)(W >> 16);
00154       *CurBufferPtr++ = (uint8_t)(W >>  8);
00155       *CurBufferPtr++ = (uint8_t)(W >>  0);
00156     } else {
00157       CurBufferPtr = BufferEnd;
00158     }
00159   }
00160 
00161   /// emitAlignment - Move the CurBufferPtr pointer up to 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         (uint8_t*)(((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(uint64_t Value) {
00180     do {
00181       uint8_t 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(uint64_t Value) {
00191     uint64_t Sign = Value >> (8 * sizeof(Value) - 1);
00192     bool IsMore;
00193   
00194     do {
00195       uint8_t 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       uint8_t C = String[i];
00209       emitByte(C);
00210     }
00211     emitByte(0);
00212   }
00213   
00214   /// emitInt32 - Emit a int32 directive.
00215   void emitInt32(int32_t 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   /// processDebugLoc - Records debug location information about a
00247   /// MachineInstruction.  This is called before emitting any bytes associated
00248   /// with the instruction.  Even if successive instructions have the same debug
00249   /// location, this method will be called for each one.
00250   virtual void processDebugLoc(DebugLoc DL, bool BeforePrintintInsn) {}
00251 
00252   /// emitLabel - Emits a label
00253   virtual void emitLabel(MCSymbol *Label) = 0;
00254 
00255   /// allocateSpace - Allocate a block of space in the current output buffer,
00256   /// returning null (and setting conditions to indicate buffer overflow) on
00257   /// failure.  Alignment is the alignment in bytes of the buffer desired.
00258   virtual void *allocateSpace(uintptr_t Size, unsigned Alignment) {
00259     emitAlignment(Alignment);
00260     void *Result;
00261     
00262     // Check for buffer overflow.
00263     if (Size >= (uintptr_t)(BufferEnd-CurBufferPtr)) {
00264       CurBufferPtr = BufferEnd;
00265       Result = nullptr;
00266     } else {
00267       // Allocate the space.
00268       Result = CurBufferPtr;
00269       CurBufferPtr += Size;
00270     }
00271     
00272     return Result;
00273   }
00274 
00275   /// StartMachineBasicBlock - This should be called by the target when a new
00276   /// basic block is about to be emitted.  This way the MCE knows where the
00277   /// start of the block is, and can implement getMachineBasicBlockAddress.
00278   virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) = 0;
00279   
00280   /// getCurrentPCValue - This returns the address that the next emitted byte
00281   /// will be output to.
00282   ///
00283   virtual uintptr_t getCurrentPCValue() const {
00284     return (uintptr_t)CurBufferPtr;
00285   }
00286 
00287   /// getCurrentPCOffset - Return the offset from the start of the emitted
00288   /// buffer that we are currently writing to.
00289   virtual uintptr_t getCurrentPCOffset() const {
00290     return CurBufferPtr-BufferBegin;
00291   }
00292 
00293   /// earlyResolveAddresses - True if the code emitter can use symbol addresses 
00294   /// during code emission time. The JIT is capable of doing this because it
00295   /// creates jump tables or constant pools in memory on the fly while the
00296   /// object code emitters rely on a linker to have real addresses and should
00297   /// use relocations instead.
00298   virtual bool earlyResolveAddresses() const = 0;
00299 
00300   /// addRelocation - Whenever a relocatable address is needed, it should be
00301   /// noted with this interface.
00302   virtual void addRelocation(const MachineRelocation &MR) = 0;
00303   
00304   /// FIXME: These should all be handled with relocations!
00305   
00306   /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in
00307   /// the constant pool that was last emitted with the emitConstantPool method.
00308   ///
00309   virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const = 0;
00310 
00311   /// getJumpTableEntryAddress - Return the address of the jump table with index
00312   /// 'Index' in the function that last called initJumpTableInfo.
00313   ///
00314   virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const = 0;
00315   
00316   /// getMachineBasicBlockAddress - Return the address of the specified
00317   /// MachineBasicBlock, only usable after the label for the MBB has been
00318   /// emitted.
00319   ///
00320   virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const= 0;
00321 
00322   /// getLabelAddress - Return the address of the specified Label, only usable
00323   /// after the LabelID has been emitted.
00324   ///
00325   virtual uintptr_t getLabelAddress(MCSymbol *Label) const = 0;
00326   
00327   /// Specifies the MachineModuleInfo object. This is used for exception handling
00328   /// purposes.
00329   virtual void setModuleInfo(MachineModuleInfo* Info) = 0;
00330 };
00331 
00332 } // End llvm namespace
00333 
00334 #endif