Home | History | Annotate | Download | only in CodeGen
      1 //===-- llvm/CodeGen/MachineCodeEmitter.h - Code emission -------*- 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 // This file defines an abstract interface that is used by the machine code
     11 // emission framework to output the code.  This allows machine code emission to
     12 // be separated from concerns such as resolution of call targets, and where the
     13 // machine code will be written (memory or disk, f.e.).
     14 //
     15 //===----------------------------------------------------------------------===//
     16 
     17 #ifndef LLVM_CODEGEN_MACHINECODEEMITTER_H
     18 #define LLVM_CODEGEN_MACHINECODEEMITTER_H
     19 
     20 #include "llvm/IR/DebugLoc.h"
     21 #include "llvm/Support/DataTypes.h"
     22 #include <string>
     23 
     24 namespace llvm {
     25 
     26 class MachineBasicBlock;
     27 class MachineConstantPool;
     28 class MachineJumpTableInfo;
     29 class MachineFunction;
     30 class MachineModuleInfo;
     31 class MachineRelocation;
     32 class Value;
     33 class GlobalValue;
     34 class Function;
     35 class MCSymbol;
     36 
     37 /// MachineCodeEmitter - This class defines two sorts of methods: those for
     38 /// emitting the actual bytes of machine code, and those for emitting auxiliary
     39 /// structures, such as jump tables, relocations, etc.
     40 ///
     41 /// Emission of machine code is complicated by the fact that we don't (in
     42 /// general) know the size of the machine code that we're about to emit before
     43 /// we emit it.  As such, we preallocate a certain amount of memory, and set the
     44 /// BufferBegin/BufferEnd pointers to the start and end of the buffer.  As we
     45 /// emit machine instructions, we advance the CurBufferPtr to indicate the
     46 /// location of the next byte to emit.  In the case of a buffer overflow (we
     47 /// need to emit more machine code than we have allocated space for), the
     48 /// CurBufferPtr will saturate to BufferEnd and ignore stores.  Once the entire
     49 /// function has been emitted, the overflow condition is checked, and if it has
     50 /// occurred, more memory is allocated, and we reemit the code into it.
     51 ///
     52 class MachineCodeEmitter {
     53   virtual void anchor();
     54 protected:
     55   /// BufferBegin/BufferEnd - Pointers to the start and end of the memory
     56   /// allocated for this code buffer.
     57   uint8_t *BufferBegin, *BufferEnd;
     58   /// CurBufferPtr - Pointer to the next byte of memory to fill when emitting
     59   /// code.  This is guaranteed to be in the range [BufferBegin,BufferEnd].  If
     60   /// this pointer is at BufferEnd, it will never move due to code emission, and
     61   /// all code emission requests will be ignored (this is the buffer overflow
     62   /// condition).
     63   uint8_t *CurBufferPtr;
     64 
     65 public:
     66   virtual ~MachineCodeEmitter() {}
     67 
     68   /// startFunction - This callback is invoked when the specified function is
     69   /// about to be code generated.  This initializes the BufferBegin/End/Ptr
     70   /// fields.
     71   ///
     72   virtual void startFunction(MachineFunction &F) = 0;
     73 
     74   /// finishFunction - This callback is invoked when the specified function has
     75   /// finished code generation.  If a buffer overflow has occurred, this method
     76   /// returns true (the callee is required to try again), otherwise it returns
     77   /// false.
     78   ///
     79   virtual bool finishFunction(MachineFunction &F) = 0;
     80 
     81   /// emitByte - This callback is invoked when a byte needs to be written to the
     82   /// output stream.
     83   ///
     84   void emitByte(uint8_t B) {
     85     if (CurBufferPtr != BufferEnd)
     86       *CurBufferPtr++ = B;
     87   }
     88 
     89   /// emitWordLE - This callback is invoked when a 32-bit word needs to be
     90   /// written to the output stream in little-endian format.
     91   ///
     92   void emitWordLE(uint32_t W) {
     93     if (4 <= BufferEnd-CurBufferPtr) {
     94       emitWordLEInto(CurBufferPtr, W);
     95     } else {
     96       CurBufferPtr = BufferEnd;
     97     }
     98   }
     99 
    100   /// emitWordLEInto - This callback is invoked when a 32-bit word needs to be
    101   /// written to an arbitrary buffer in little-endian format.  Buf must have at
    102   /// least 4 bytes of available space.
    103   ///
    104   static void emitWordLEInto(uint8_t *&Buf, uint32_t W) {
    105     *Buf++ = (uint8_t)(W >>  0);
    106     *Buf++ = (uint8_t)(W >>  8);
    107     *Buf++ = (uint8_t)(W >> 16);
    108     *Buf++ = (uint8_t)(W >> 24);
    109   }
    110 
    111   /// emitWordBE - This callback is invoked when a 32-bit word needs to be
    112   /// written to the output stream in big-endian format.
    113   ///
    114   void emitWordBE(uint32_t W) {
    115     if (4 <= BufferEnd-CurBufferPtr) {
    116       *CurBufferPtr++ = (uint8_t)(W >> 24);
    117       *CurBufferPtr++ = (uint8_t)(W >> 16);
    118       *CurBufferPtr++ = (uint8_t)(W >>  8);
    119       *CurBufferPtr++ = (uint8_t)(W >>  0);
    120     } else {
    121       CurBufferPtr = BufferEnd;
    122     }
    123   }
    124 
    125   /// emitDWordLE - This callback is invoked when a 64-bit word needs to be
    126   /// written to the output stream in little-endian format.
    127   ///
    128   void emitDWordLE(uint64_t W) {
    129     if (8 <= BufferEnd-CurBufferPtr) {
    130       *CurBufferPtr++ = (uint8_t)(W >>  0);
    131       *CurBufferPtr++ = (uint8_t)(W >>  8);
    132       *CurBufferPtr++ = (uint8_t)(W >> 16);
    133       *CurBufferPtr++ = (uint8_t)(W >> 24);
    134       *CurBufferPtr++ = (uint8_t)(W >> 32);
    135       *CurBufferPtr++ = (uint8_t)(W >> 40);
    136       *CurBufferPtr++ = (uint8_t)(W >> 48);
    137       *CurBufferPtr++ = (uint8_t)(W >> 56);
    138     } else {
    139       CurBufferPtr = BufferEnd;
    140     }
    141   }
    142 
    143   /// emitDWordBE - This callback is invoked when a 64-bit word needs to be
    144   /// written to the output stream in big-endian format.
    145   ///
    146   void emitDWordBE(uint64_t W) {
    147     if (8 <= BufferEnd-CurBufferPtr) {
    148       *CurBufferPtr++ = (uint8_t)(W >> 56);
    149       *CurBufferPtr++ = (uint8_t)(W >> 48);
    150       *CurBufferPtr++ = (uint8_t)(W >> 40);
    151       *CurBufferPtr++ = (uint8_t)(W >> 32);
    152       *CurBufferPtr++ = (uint8_t)(W >> 24);
    153       *CurBufferPtr++ = (uint8_t)(W >> 16);
    154       *CurBufferPtr++ = (uint8_t)(W >>  8);
    155       *CurBufferPtr++ = (uint8_t)(W >>  0);
    156     } else {
    157       CurBufferPtr = BufferEnd;
    158     }
    159   }
    160 
    161   /// emitAlignment - Move the CurBufferPtr pointer up to the specified
    162   /// alignment (saturated to BufferEnd of course).
    163   void emitAlignment(unsigned Alignment) {
    164     if (Alignment == 0) Alignment = 1;
    165 
    166     if(Alignment <= (uintptr_t)(BufferEnd-CurBufferPtr)) {
    167       // Move the current buffer ptr up to the specified alignment.
    168       CurBufferPtr =
    169         (uint8_t*)(((uintptr_t)CurBufferPtr+Alignment-1) &
    170                    ~(uintptr_t)(Alignment-1));
    171     } else {
    172       CurBufferPtr = BufferEnd;
    173     }
    174   }
    175 
    176 
    177   /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be
    178   /// written to the output stream.
    179   void emitULEB128Bytes(uint64_t Value) {
    180     do {
    181       uint8_t Byte = Value & 0x7f;
    182       Value >>= 7;
    183       if (Value) Byte |= 0x80;
    184       emitByte(Byte);
    185     } while (Value);
    186   }
    187 
    188   /// emitSLEB128Bytes - This callback is invoked when a SLEB128 needs to be
    189   /// written to the output stream.
    190   void emitSLEB128Bytes(uint64_t Value) {
    191     uint64_t Sign = Value >> (8 * sizeof(Value) - 1);
    192     bool IsMore;
    193 
    194     do {
    195       uint8_t Byte = Value & 0x7f;
    196       Value >>= 7;
    197       IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
    198       if (IsMore) Byte |= 0x80;
    199       emitByte(Byte);
    200     } while (IsMore);
    201   }
    202 
    203   /// emitString - This callback is invoked when a String needs to be
    204   /// written to the output stream.
    205   void emitString(const std::string &String) {
    206     for (unsigned i = 0, N = static_cast<unsigned>(String.size());
    207          i < N; ++i) {
    208       uint8_t C = String[i];
    209       emitByte(C);
    210     }
    211     emitByte(0);
    212   }
    213 
    214   /// emitInt32 - Emit a int32 directive.
    215   void emitInt32(int32_t Value) {
    216     if (4 <= BufferEnd-CurBufferPtr) {
    217       *((uint32_t*)CurBufferPtr) = Value;
    218       CurBufferPtr += 4;
    219     } else {
    220       CurBufferPtr = BufferEnd;
    221     }
    222   }
    223 
    224   /// emitInt64 - Emit a int64 directive.
    225   void emitInt64(uint64_t Value) {
    226     if (8 <= BufferEnd-CurBufferPtr) {
    227       *((uint64_t*)CurBufferPtr) = Value;
    228       CurBufferPtr += 8;
    229     } else {
    230       CurBufferPtr = BufferEnd;
    231     }
    232   }
    233 
    234   /// emitInt32At - Emit the Int32 Value in Addr.
    235   void emitInt32At(uintptr_t *Addr, uintptr_t Value) {
    236     if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd)
    237       (*(uint32_t*)Addr) = (uint32_t)Value;
    238   }
    239 
    240   /// emitInt64At - Emit the Int64 Value in Addr.
    241   void emitInt64At(uintptr_t *Addr, uintptr_t Value) {
    242     if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd)
    243       (*(uint64_t*)Addr) = (uint64_t)Value;
    244   }
    245 
    246   /// processDebugLoc - Records debug location information about a
    247   /// MachineInstruction.  This is called before emitting any bytes associated
    248   /// with the instruction.  Even if successive instructions have the same debug
    249   /// location, this method will be called for each one.
    250   virtual void processDebugLoc(DebugLoc DL, bool BeforePrintintInsn) {}
    251 
    252   /// emitLabel - Emits a label
    253   virtual void emitLabel(MCSymbol *Label) = 0;
    254 
    255   /// allocateSpace - Allocate a block of space in the current output buffer,
    256   /// returning null (and setting conditions to indicate buffer overflow) on
    257   /// failure.  Alignment is the alignment in bytes of the buffer desired.
    258   virtual void *allocateSpace(uintptr_t Size, unsigned Alignment) {
    259     emitAlignment(Alignment);
    260     void *Result;
    261 
    262     // Check for buffer overflow.
    263     if (Size >= (uintptr_t)(BufferEnd-CurBufferPtr)) {
    264       CurBufferPtr = BufferEnd;
    265       Result = nullptr;
    266     } else {
    267       // Allocate the space.
    268       Result = CurBufferPtr;
    269       CurBufferPtr += Size;
    270     }
    271 
    272     return Result;
    273   }
    274 
    275   /// StartMachineBasicBlock - This should be called by the target when a new
    276   /// basic block is about to be emitted.  This way the MCE knows where the
    277   /// start of the block is, and can implement getMachineBasicBlockAddress.
    278   virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) = 0;
    279 
    280   /// getCurrentPCValue - This returns the address that the next emitted byte
    281   /// will be output to.
    282   ///
    283   virtual uintptr_t getCurrentPCValue() const {
    284     return (uintptr_t)CurBufferPtr;
    285   }
    286 
    287   /// getCurrentPCOffset - Return the offset from the start of the emitted
    288   /// buffer that we are currently writing to.
    289   virtual uintptr_t getCurrentPCOffset() const {
    290     return CurBufferPtr-BufferBegin;
    291   }
    292 
    293   /// earlyResolveAddresses - True if the code emitter can use symbol addresses
    294   /// during code emission time. The JIT is capable of doing this because it
    295   /// creates jump tables or constant pools in memory on the fly while the
    296   /// object code emitters rely on a linker to have real addresses and should
    297   /// use relocations instead.
    298   virtual bool earlyResolveAddresses() const = 0;
    299 
    300   /// addRelocation - Whenever a relocatable address is needed, it should be
    301   /// noted with this interface.
    302   virtual void addRelocation(const MachineRelocation &MR) = 0;
    303 
    304   /// FIXME: These should all be handled with relocations!
    305 
    306   /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in
    307   /// the constant pool that was last emitted with the emitConstantPool method.
    308   ///
    309   virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const = 0;
    310 
    311   /// getJumpTableEntryAddress - Return the address of the jump table with index
    312   /// 'Index' in the function that last called initJumpTableInfo.
    313   ///
    314   virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const = 0;
    315 
    316   /// getMachineBasicBlockAddress - Return the address of the specified
    317   /// MachineBasicBlock, only usable after the label for the MBB has been
    318   /// emitted.
    319   ///
    320   virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const= 0;
    321 
    322   /// getLabelAddress - Return the address of the specified Label, only usable
    323   /// after the LabelID has been emitted.
    324   ///
    325   virtual uintptr_t getLabelAddress(MCSymbol *Label) const = 0;
    326 
    327   /// Specifies the MachineModuleInfo object. This is used for exception handling
    328   /// purposes.
    329   virtual void setModuleInfo(MachineModuleInfo* Info) = 0;
    330 };
    331 
    332 } // End llvm namespace
    333 
    334 #endif
    335