LLVM API Documentation

JITCodeEmitter.h

Go to the documentation of this file.
00001 //===-- llvm/CodeGen/JITCodeEmitter.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_JITCODEEMITTER_H
00018 #define LLVM_CODEGEN_JITCODEEMITTER_H
00019 
00020 #include <string>
00021 #include "llvm/Support/DataTypes.h"
00022 #include "llvm/Support/MathExtras.h"
00023 #include "llvm/CodeGen/MachineCodeEmitter.h"
00024 
00025 using namespace std;
00026 
00027 namespace llvm {
00028 
00029 class MachineBasicBlock;
00030 class MachineConstantPool;
00031 class MachineJumpTableInfo;
00032 class MachineFunction;
00033 class MachineModuleInfo;
00034 class MachineRelocation;
00035 class Value;
00036 class GlobalValue;
00037 class Function;
00038 
00039 /// JITCodeEmitter - This class defines two sorts of methods: those for
00040 /// emitting the actual bytes of machine code, and those for emitting auxillary
00041 /// structures, such as jump tables, relocations, etc.
00042 ///
00043 /// Emission of machine code is complicated by the fact that we don't (in
00044 /// general) know the size of the machine code that we're about to emit before
00045 /// we emit it.  As such, we preallocate a certain amount of memory, and set the
00046 /// BufferBegin/BufferEnd pointers to the start and end of the buffer.  As we
00047 /// emit machine instructions, we advance the CurBufferPtr to indicate the
00048 /// location of the next byte to emit.  In the case of a buffer overflow (we
00049 /// need to emit more machine code than we have allocated space for), the
00050 /// CurBufferPtr will saturate to BufferEnd and ignore stores.  Once the entire
00051 /// function has been emitted, the overflow condition is checked, and if it has
00052 /// occurred, more memory is allocated, and we reemit the code into it.
00053 /// 
00054 class JITCodeEmitter : public MachineCodeEmitter {
00055 public:
00056   virtual ~JITCodeEmitter() {}
00057 
00058   /// startFunction - This callback is invoked when the specified function is
00059   /// about to be code generated.  This initializes the BufferBegin/End/Ptr
00060   /// fields.
00061   ///
00062   virtual void startFunction(MachineFunction &F) = 0;
00063 
00064   /// finishFunction - This callback is invoked when the specified function has
00065   /// finished code generation.  If a buffer overflow has occurred, this method
00066   /// returns true (the callee is required to try again), otherwise it returns
00067   /// false.
00068   ///
00069   virtual bool finishFunction(MachineFunction &F) = 0;
00070   
00071   /// startGVStub - This callback is invoked when the JIT needs the
00072   /// address of a GV (e.g. function) that has not been code generated yet.
00073   /// The StubSize specifies the total size required by the stub.
00074   ///
00075   virtual void startGVStub(const GlobalValue* GV, unsigned StubSize,
00076                            unsigned Alignment = 1) = 0;
00077 
00078   /// startGVStub - This callback is invoked when the JIT needs the address of a 
00079   /// GV (e.g. function) that has not been code generated yet.  Buffer points to
00080   /// memory already allocated for this stub.
00081   ///
00082   virtual void startGVStub(const GlobalValue* GV, void *Buffer,
00083                            unsigned StubSize) = 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(uint8_t 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(uint32_t W) {
00101     if (4 <= BufferEnd-CurBufferPtr) {
00102       *CurBufferPtr++ = (uint8_t)(W >>  0);
00103       *CurBufferPtr++ = (uint8_t)(W >>  8);
00104       *CurBufferPtr++ = (uint8_t)(W >> 16);
00105       *CurBufferPtr++ = (uint8_t)(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(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 the the specified
00162   /// alignment (saturated to BufferEnd of course).
00163   void emitAlignment(unsigned Alignment) {
00164     if (Alignment == 0) Alignment = 1;
00165     uint8_t *NewPtr = (uint8_t*)RoundUpToAlignment((uintptr_t)CurBufferPtr,
00166                                                    Alignment);
00167     CurBufferPtr = std::min(NewPtr, BufferEnd);
00168   }
00169 
00170   /// emitAlignmentWithFill - Similar to emitAlignment, except that the
00171   /// extra bytes are filled with the provided byte.
00172   void emitAlignmentWithFill(unsigned Alignment, uint8_t Fill) {
00173     if (Alignment == 0) Alignment = 1;
00174     uint8_t *NewPtr = (uint8_t*)RoundUpToAlignment((uintptr_t)CurBufferPtr,
00175                                                    Alignment);
00176     // Fail if we don't have room.
00177     if (NewPtr > BufferEnd) {
00178       CurBufferPtr = BufferEnd;
00179       return;
00180     }
00181     while (CurBufferPtr < NewPtr) {
00182       *CurBufferPtr++ = Fill;
00183     }
00184   }
00185 
00186   /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be
00187   /// written to the output stream.
00188   void emitULEB128Bytes(uint64_t Value) {
00189     do {
00190       uint8_t Byte = Value & 0x7f;
00191       Value >>= 7;
00192       if (Value) Byte |= 0x80;
00193       emitByte(Byte);
00194     } while (Value);
00195   }
00196   
00197   /// emitSLEB128Bytes - This callback is invoked when a SLEB128 needs to be
00198   /// written to the output stream.
00199   void emitSLEB128Bytes(int64_t Value) {
00200     int32_t Sign = Value >> (8 * sizeof(Value) - 1);
00201     bool IsMore;
00202   
00203     do {
00204       uint8_t Byte = Value & 0x7f;
00205       Value >>= 7;
00206       IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
00207       if (IsMore) Byte |= 0x80;
00208       emitByte(Byte);
00209     } while (IsMore);
00210   }
00211 
00212   /// emitString - This callback is invoked when a String needs to be
00213   /// written to the output stream.
00214   void emitString(const std::string &String) {
00215     for (unsigned i = 0, N = static_cast<unsigned>(String.size());
00216          i < N; ++i) {
00217       uint8_t C = String[i];
00218       emitByte(C);
00219     }
00220     emitByte(0);
00221   }
00222   
00223   /// emitInt32 - Emit a int32 directive.
00224   void emitInt32(uint32_t Value) {
00225     if (4 <= BufferEnd-CurBufferPtr) {
00226       *((uint32_t*)CurBufferPtr) = Value;
00227       CurBufferPtr += 4;
00228     } else {
00229       CurBufferPtr = BufferEnd;
00230     }
00231   }
00232 
00233   /// emitInt64 - Emit a int64 directive.
00234   void emitInt64(uint64_t Value) {
00235     if (8 <= BufferEnd-CurBufferPtr) {
00236       *((uint64_t*)CurBufferPtr) = Value;
00237       CurBufferPtr += 8;
00238     } else {
00239       CurBufferPtr = BufferEnd;
00240     }
00241   }
00242   
00243   /// emitInt32At - Emit the Int32 Value in Addr.
00244   void emitInt32At(uintptr_t *Addr, uintptr_t Value) {
00245     if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd)
00246       (*(uint32_t*)Addr) = (uint32_t)Value;
00247   }
00248   
00249   /// emitInt64At - Emit the Int64 Value in Addr.
00250   void emitInt64At(uintptr_t *Addr, uintptr_t Value) {
00251     if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd)
00252       (*(uint64_t*)Addr) = (uint64_t)Value;
00253   }
00254   
00255   
00256   /// emitLabel - Emits a label
00257   virtual void emitLabel(uint64_t LabelID) = 0;
00258 
00259   /// allocateSpace - Allocate a block of space in the current output buffer,
00260   /// returning null (and setting conditions to indicate buffer overflow) on
00261   /// failure.  Alignment is the alignment in bytes of the buffer desired.
00262   virtual void *allocateSpace(uintptr_t Size, unsigned Alignment) {
00263     emitAlignment(Alignment);
00264     void *Result;
00265     
00266     // Check for buffer overflow.
00267     if (Size >= (uintptr_t)(BufferEnd-CurBufferPtr)) {
00268       CurBufferPtr = BufferEnd;
00269       Result = 0;
00270     } else {
00271       // Allocate the space.
00272       Result = CurBufferPtr;
00273       CurBufferPtr += Size;
00274     }
00275     
00276     return Result;
00277   }
00278 
00279   /// allocateGlobal - Allocate memory for a global.  Unlike allocateSpace,
00280   /// this method does not allocate memory in the current output buffer,
00281   /// because a global may live longer than the current function.
00282   virtual void *allocateGlobal(uintptr_t Size, unsigned Alignment) = 0;
00283 
00284   /// StartMachineBasicBlock - This should be called by the target when a new
00285   /// basic block is about to be emitted.  This way the MCE knows where the
00286   /// start of the block is, and can implement getMachineBasicBlockAddress.
00287   virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) = 0;
00288   
00289   /// getCurrentPCValue - This returns the address that the next emitted byte
00290   /// will be output to.
00291   ///
00292   virtual uintptr_t getCurrentPCValue() const {
00293     return (uintptr_t)CurBufferPtr;
00294   }
00295 
00296   /// getCurrentPCOffset - Return the offset from the start of the emitted
00297   /// buffer that we are currently writing to.
00298   uintptr_t getCurrentPCOffset() const {
00299     return CurBufferPtr-BufferBegin;
00300   }
00301 
00302   /// earlyResolveAddresses - True if the code emitter can use symbol addresses 
00303   /// during code emission time. The JIT is capable of doing this because it
00304   /// creates jump tables or constant pools in memory on the fly while the
00305   /// object code emitters rely on a linker to have real addresses and should
00306   /// use relocations instead.
00307   bool earlyResolveAddresses() const { return true; }
00308 
00309   /// addRelocation - Whenever a relocatable address is needed, it should be
00310   /// noted with this interface.
00311   virtual void addRelocation(const MachineRelocation &MR) = 0;
00312   
00313   /// FIXME: These should all be handled with relocations!
00314   
00315   /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in
00316   /// the constant pool that was last emitted with the emitConstantPool method.
00317   ///
00318   virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const = 0;
00319 
00320   /// getJumpTableEntryAddress - Return the address of the jump table with index
00321   /// 'Index' in the function that last called initJumpTableInfo.
00322   ///
00323   virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const = 0;
00324   
00325   /// getMachineBasicBlockAddress - Return the address of the specified
00326   /// MachineBasicBlock, only usable after the label for the MBB has been
00327   /// emitted.
00328   ///
00329   virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const= 0;
00330 
00331   /// getLabelAddress - Return the address of the specified LabelID, only usable
00332   /// after the LabelID has been emitted.
00333   ///
00334   virtual uintptr_t getLabelAddress(uint64_t LabelID) const = 0;
00335   
00336   /// Specifies the MachineModuleInfo object. This is used for exception handling
00337   /// purposes.
00338   virtual void setModuleInfo(MachineModuleInfo* Info) = 0;
00339 };
00340 
00341 } // End llvm namespace
00342 
00343 #endif