43b14ca38f9359ca368aad40e2060f5daab6dfe7
[oota-llvm.git] / include / llvm / CodeGen / MachineCodeEmitter.h
1 //===-- llvm/CodeGen/MachineCodeEmitter.h - Code emission -------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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/Support/DataTypes.h"
21 #include <vector>
22
23 namespace llvm {
24
25 class MachineBasicBlock;
26 class MachineConstantPool;
27 class MachineJumpTableInfo;
28 class MachineFunction;
29 class MachineRelocation;
30 class Value;
31 class GlobalValue;
32 class Function;
33
34 /// MachineCodeEmitter - This class defines two sorts of methods: those for
35 /// emitting the actual bytes of machine code, and those for emitting auxillary
36 /// structures, such as jump tables, relocations, etc.
37 ///
38 /// Emission of machine code is complicated by the fact that we don't (in
39 /// general) know the size of the machine code that we're about to emit before
40 /// we emit it.  As such, we preallocate a certain amount of memory, and set the
41 /// BufferBegin/BufferEnd pointers to the start and end of the buffer.  As we
42 /// emit machine instructions, we advance the CurBufferPtr to indicate the
43 /// location of the next byte to emit.  In the case of a buffer overflow (we
44 /// need to emit more machine code than we have allocated space for), the
45 /// CurBufferPtr will saturate to BufferEnd and ignore stores.  Once the entire
46 /// function has been emitted, the overflow condition is checked, and if it has
47 /// occurred, more memory is allocated, and we reemit the code into it.
48 /// 
49 class MachineCodeEmitter {
50 protected:
51   /// BufferBegin/BufferEnd - Pointers to the start and end of the memory
52   /// allocated for this code buffer.
53   unsigned char *BufferBegin, *BufferEnd;
54   
55   /// CurBufferPtr - Pointer to the next byte of memory to fill when emitting 
56   /// code.  This is guranteed to be in the range [BufferBegin,BufferEnd].  If
57   /// this pointer is at BufferEnd, it will never move due to code emission, and
58   /// all code emission requests will be ignored (this is the buffer overflow
59   /// condition).
60   unsigned char *CurBufferPtr;
61 public:
62   virtual ~MachineCodeEmitter() {}
63
64   /// startFunction - This callback is invoked when the specified function is
65   /// about to be code generated.  This initializes the BufferBegin/End/Ptr
66   /// fields.
67   ///
68   virtual void startFunction(MachineFunction &F) = 0;
69
70   /// finishFunction - This callback is invoked when the specified function has
71   /// finished code generation.  If a buffer overflow has occurred, this method
72   /// returns true (the callee is required to try again), otherwise it returns
73   /// false.
74   ///
75   virtual bool finishFunction(MachineFunction &F) = 0;
76   
77   /// emitJumpTableInfo - This callback is invoked to output the jump tables
78   /// for the function.  In addition to a pointer to the MachineJumpTableInfo,
79   /// this function also takes a map of MBB IDs to addresses, so that the final
80   /// addresses of the MBBs can be written to the jump tables.
81   virtual void emitJumpTableInfo(MachineJumpTableInfo *MJTI,
82                                  std::vector<uint64_t> &MBBM) = 0;
83   
84   /// startFunctionStub - This callback is invoked when the JIT needs the
85   /// address of a function that has not been code generated yet.  The StubSize
86   /// specifies the total size required by the stub.  Stubs are not allowed to
87   /// have constant pools, the can only use the other emitByte*/emitWord*
88   /// methods.
89   ///
90   virtual void startFunctionStub(unsigned StubSize) = 0;
91
92   /// finishFunctionStub - This callback is invoked to terminate a function
93   /// stub.
94   ///
95   virtual void *finishFunctionStub(const Function *F) = 0;
96
97   /// emitByte - This callback is invoked when a byte needs to be written to the
98   /// output stream.
99   ///
100   void emitByte(unsigned char B) {
101     if (CurBufferPtr != BufferEnd)
102       *CurBufferPtr++ = B;
103   }
104
105   /// emitWordLE - This callback is invoked when a 32-bit word needs to be
106   /// written to the output stream in little-endian format.
107   ///
108   void emitWordLE(unsigned W) {
109     if (CurBufferPtr+4 <= BufferEnd) {
110       *CurBufferPtr++ = (unsigned char)(W >>  0);
111       *CurBufferPtr++ = (unsigned char)(W >>  8);
112       *CurBufferPtr++ = (unsigned char)(W >> 16);
113       *CurBufferPtr++ = (unsigned char)(W >> 24);
114     } else {
115       CurBufferPtr = BufferEnd;
116     }
117   }
118   
119   /// emitWordBE - This callback is invoked when a 32-bit word needs to be
120   /// written to the output stream in big-endian format.
121   ///
122   void emitWordBE(unsigned W) {
123     if (CurBufferPtr+4 <= BufferEnd) {
124       *CurBufferPtr++ = (unsigned char)(W >> 24);
125       *CurBufferPtr++ = (unsigned char)(W >> 16);
126       *CurBufferPtr++ = (unsigned char)(W >>  8);
127       *CurBufferPtr++ = (unsigned char)(W >>  0);
128     } else {
129       CurBufferPtr = BufferEnd;
130     }
131   }
132
133   /// emitAlignment - Move the CurBufferPtr pointer up the the specified
134   /// alignment (saturated to BufferEnd of course).
135   void emitAlignment(unsigned Alignment) {
136     if (Alignment == 0) Alignment = 1;
137     // Move the current buffer ptr up to the specified alignment.
138     CurBufferPtr =
139       (unsigned char*)(((intptr_t)CurBufferPtr+Alignment-1) & ~(Alignment-1));
140     if (CurBufferPtr > BufferEnd)
141       CurBufferPtr = BufferEnd;
142   }
143   
144   /// allocateSpace - Allocate a block of space in the current output buffer,
145   /// returning null (and setting conditions to indicate buffer overflow) on
146   /// failure.  Alignment is the alignment in bytes of the buffer desired.
147   void *allocateSpace(intptr_t Size, unsigned Alignment) {
148     emitAlignment(Alignment);
149     void *Result = CurBufferPtr;
150     
151     // Allocate the space.
152     CurBufferPtr += Size;
153     
154     // Check for buffer overflow.
155     if (CurBufferPtr >= BufferEnd) {
156       CurBufferPtr = BufferEnd;
157       Result = 0;
158     }
159     return Result;
160   }
161   
162   
163   /// getCurrentPCValue - This returns the address that the next emitted byte
164   /// will be output to.
165   ///
166   virtual intptr_t getCurrentPCValue() const {
167     return (intptr_t)CurBufferPtr;
168   }
169
170   /// getCurrentPCOffset - Return the offset from the start of the emitted
171   /// buffer that we are currently writing to.
172   intptr_t getCurrentPCOffset() const {
173     return CurBufferPtr-BufferBegin;
174   }
175
176   /// addRelocation - Whenever a relocatable address is needed, it should be
177   /// noted with this interface.
178   virtual void addRelocation(const MachineRelocation &MR) = 0;
179
180   /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in
181   /// the constant pool that was last emitted with the emitConstantPool method.
182   ///
183   virtual uint64_t getConstantPoolEntryAddress(unsigned Index) = 0;
184
185   /// getJumpTableEntryAddress - Return the address of the jump table with index
186   /// 'Index' in the function that last called initJumpTableInfo.
187   ///
188   virtual uint64_t getJumpTableEntryAddress(unsigned Index) = 0;
189 };
190
191 } // End llvm namespace
192
193 #endif