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 was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source 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 <vector>
00022 
00023 namespace llvm {
00024 
00025 class MachineBasicBlock;
00026 class MachineConstantPool;
00027 class MachineJumpTableInfo;
00028 class MachineFunction;
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 public:
00062   virtual ~MachineCodeEmitter() {}
00063 
00064   /// startFunction - This callback is invoked when the specified function is
00065   /// about to be code generated.  This initializes the BufferBegin/End/Ptr
00066   /// fields.
00067   ///
00068   virtual void startFunction(MachineFunction &F) = 0;
00069 
00070   /// finishFunction - This callback is invoked when the specified function has
00071   /// finished code generation.  If a buffer overflow has occurred, this method
00072   /// returns true (the callee is required to try again), otherwise it returns
00073   /// false.
00074   ///
00075   virtual bool finishFunction(MachineFunction &F) = 0;
00076   
00077   /// startFunctionStub - This callback is invoked when the JIT needs the
00078   /// address of a function that has not been code generated yet.  The StubSize
00079   /// specifies the total size required by the stub.  Stubs are not allowed to
00080   /// have constant pools, the can only use the other emitByte*/emitWord*
00081   /// methods.
00082   ///
00083   virtual void startFunctionStub(unsigned StubSize) = 0;
00084 
00085   /// finishFunctionStub - This callback is invoked to terminate a function
00086   /// stub.
00087   ///
00088   virtual void *finishFunctionStub(const Function *F) = 0;
00089 
00090   /// emitByte - This callback is invoked when a byte needs to be written to the
00091   /// output stream.
00092   ///
00093   void emitByte(unsigned char B) {
00094     if (CurBufferPtr != BufferEnd)
00095       *CurBufferPtr++ = B;
00096   }
00097 
00098   /// emitWordLE - This callback is invoked when a 32-bit word needs to be
00099   /// written to the output stream in little-endian format.
00100   ///
00101   void emitWordLE(unsigned W) {
00102     if (CurBufferPtr+4 <= BufferEnd) {
00103       *CurBufferPtr++ = (unsigned char)(W >>  0);
00104       *CurBufferPtr++ = (unsigned char)(W >>  8);
00105       *CurBufferPtr++ = (unsigned char)(W >> 16);
00106       *CurBufferPtr++ = (unsigned char)(W >> 24);
00107     } else {
00108       CurBufferPtr = BufferEnd;
00109     }
00110   }
00111   
00112   /// emitWordBE - This callback is invoked when a 32-bit word needs to be
00113   /// written to the output stream in big-endian format.
00114   ///
00115   void emitWordBE(unsigned W) {
00116     if (CurBufferPtr+4 <= BufferEnd) {
00117       *CurBufferPtr++ = (unsigned char)(W >> 24);
00118       *CurBufferPtr++ = (unsigned char)(W >> 16);
00119       *CurBufferPtr++ = (unsigned char)(W >>  8);
00120       *CurBufferPtr++ = (unsigned char)(W >>  0);
00121     } else {
00122       CurBufferPtr = BufferEnd;
00123     }
00124   }
00125 
00126   /// emitAlignment - Move the CurBufferPtr pointer up the the specified
00127   /// alignment (saturated to BufferEnd of course).
00128   void emitAlignment(unsigned Alignment) {
00129     if (Alignment == 0) Alignment = 1;
00130     // Move the current buffer ptr up to the specified alignment.
00131     CurBufferPtr =
00132       (unsigned char*)(((intptr_t)CurBufferPtr+Alignment-1) &
00133                        ~(intptr_t)(Alignment-1));
00134     if (CurBufferPtr > BufferEnd)
00135       CurBufferPtr = BufferEnd;
00136   }
00137   
00138   /// allocateSpace - Allocate a block of space in the current output buffer,
00139   /// returning null (and setting conditions to indicate buffer overflow) on
00140   /// failure.  Alignment is the alignment in bytes of the buffer desired.
00141   void *allocateSpace(intptr_t Size, unsigned Alignment) {
00142     emitAlignment(Alignment);
00143     void *Result = CurBufferPtr;
00144     
00145     // Allocate the space.
00146     CurBufferPtr += Size;
00147     
00148     // Check for buffer overflow.
00149     if (CurBufferPtr >= BufferEnd) {
00150       CurBufferPtr = BufferEnd;
00151       Result = 0;
00152     }
00153     return Result;
00154   }
00155 
00156   /// StartMachineBasicBlock - This should be called by the target when a new
00157   /// basic block is about to be emitted.  This way the MCE knows where the
00158   /// start of the block is, and can implement getMachineBasicBlockAddress.
00159   virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) = 0;
00160   
00161   /// getCurrentPCValue - This returns the address that the next emitted byte
00162   /// will be output to.
00163   ///
00164   virtual intptr_t getCurrentPCValue() const {
00165     return (intptr_t)CurBufferPtr;
00166   }
00167 
00168   /// getCurrentPCOffset - Return the offset from the start of the emitted
00169   /// buffer that we are currently writing to.
00170   intptr_t getCurrentPCOffset() const {
00171     return CurBufferPtr-BufferBegin;
00172   }
00173 
00174   /// addRelocation - Whenever a relocatable address is needed, it should be
00175   /// noted with this interface.
00176   virtual void addRelocation(const MachineRelocation &MR) = 0;
00177 
00178   
00179   /// FIXME: These should all be handled with relocations!
00180   
00181   /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in
00182   /// the constant pool that was last emitted with the emitConstantPool method.
00183   ///
00184   virtual intptr_t getConstantPoolEntryAddress(unsigned Index) const = 0;
00185 
00186   /// getJumpTableEntryAddress - Return the address of the jump table with index
00187   /// 'Index' in the function that last called initJumpTableInfo.
00188   ///
00189   virtual intptr_t getJumpTableEntryAddress(unsigned Index) const = 0;
00190   
00191   /// getMachineBasicBlockAddress - Return the address of the specified
00192   /// MachineBasicBlock, only usable after the label for the MBB has been
00193   /// emitted.
00194   ///
00195   virtual intptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const= 0;
00196 };
00197 
00198 } // End llvm namespace
00199 
00200 #endif