Rename startFunctionStub to startGVStub since it's also used for GV non-lazy ptr.
[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 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/Support/DataTypes.h"
21
22 namespace llvm {
23
24 class MachineBasicBlock;
25 class MachineConstantPool;
26 class MachineJumpTableInfo;
27 class MachineFunction;
28 class MachineModuleInfo;
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
62 public:
63   virtual ~MachineCodeEmitter() {}
64
65   /// startFunction - This callback is invoked when the specified function is
66   /// about to be code generated.  This initializes the BufferBegin/End/Ptr
67   /// fields.
68   ///
69   virtual void startFunction(MachineFunction &F) = 0;
70
71   /// finishFunction - This callback is invoked when the specified function has
72   /// finished code generation.  If a buffer overflow has occurred, this method
73   /// returns true (the callee is required to try again), otherwise it returns
74   /// false.
75   ///
76   virtual bool finishFunction(MachineFunction &F) = 0;
77   
78   /// startGVStub - This callback is invoked when the JIT needs the
79   /// address of a GV (e.g. function) that has not been code generated yet.
80   /// The StubSize specifies the total size required by the stub.
81   ///
82   virtual void startGVStub(const GlobalValue* GV, unsigned StubSize,
83                            unsigned Alignment = 1) = 0;
84
85   /// finishGVStub - This callback is invoked to terminate a GV stub.
86   ///
87   virtual void *finishGVStub(const GlobalValue* F) = 0;
88
89   /// emitByte - This callback is invoked when a byte needs to be written to the
90   /// output stream.
91   ///
92   void emitByte(unsigned char B) {
93     if (CurBufferPtr != BufferEnd)
94       *CurBufferPtr++ = B;
95   }
96
97   /// emitWordLE - This callback is invoked when a 32-bit word needs to be
98   /// written to the output stream in little-endian format.
99   ///
100   void emitWordLE(unsigned W) {
101     if (CurBufferPtr+4 <= BufferEnd) {
102       *CurBufferPtr++ = (unsigned char)(W >>  0);
103       *CurBufferPtr++ = (unsigned char)(W >>  8);
104       *CurBufferPtr++ = (unsigned char)(W >> 16);
105       *CurBufferPtr++ = (unsigned char)(W >> 24);
106     } else {
107       CurBufferPtr = BufferEnd;
108     }
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(unsigned W) {
115     if (CurBufferPtr+4 <= BufferEnd) {
116       *CurBufferPtr++ = (unsigned char)(W >> 24);
117       *CurBufferPtr++ = (unsigned char)(W >> 16);
118       *CurBufferPtr++ = (unsigned char)(W >>  8);
119       *CurBufferPtr++ = (unsigned char)(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 (CurBufferPtr+8 <= BufferEnd) {
130       *CurBufferPtr++ = (unsigned char)(W >>  0);
131       *CurBufferPtr++ = (unsigned char)(W >>  8);
132       *CurBufferPtr++ = (unsigned char)(W >> 16);
133       *CurBufferPtr++ = (unsigned char)(W >> 24);
134       *CurBufferPtr++ = (unsigned char)(W >> 32);
135       *CurBufferPtr++ = (unsigned char)(W >> 40);
136       *CurBufferPtr++ = (unsigned char)(W >> 48);
137       *CurBufferPtr++ = (unsigned char)(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 (CurBufferPtr+8 <= BufferEnd) {
148       *CurBufferPtr++ = (unsigned char)(W >> 56);
149       *CurBufferPtr++ = (unsigned char)(W >> 48);
150       *CurBufferPtr++ = (unsigned char)(W >> 40);
151       *CurBufferPtr++ = (unsigned char)(W >> 32);
152       *CurBufferPtr++ = (unsigned char)(W >> 24);
153       *CurBufferPtr++ = (unsigned char)(W >> 16);
154       *CurBufferPtr++ = (unsigned char)(W >>  8);
155       *CurBufferPtr++ = (unsigned char)(W >>  0);
156     } else {
157       CurBufferPtr = BufferEnd;
158     }
159   }
160
161   /// emitAlignment - Move the CurBufferPtr pointer up the the specified
162   /// alignment (saturated to BufferEnd of course).
163   void emitAlignment(unsigned Alignment) {
164     if (Alignment == 0) Alignment = 1;
165     // Move the current buffer ptr up to the specified alignment.
166     CurBufferPtr =
167       (unsigned char*)(((intptr_t)CurBufferPtr+Alignment-1) &
168                        ~(intptr_t)(Alignment-1));
169     if (CurBufferPtr > BufferEnd)
170       CurBufferPtr = BufferEnd;
171   }
172   
173
174   /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be
175   /// written to the output stream.
176   void emitULEB128Bytes(unsigned Value) {
177     do {
178       unsigned char Byte = Value & 0x7f;
179       Value >>= 7;
180       if (Value) Byte |= 0x80;
181       emitByte(Byte);
182     } while (Value);
183   }
184   
185   /// emitSLEB128Bytes - This callback is invoked when a SLEB128 needs to be
186   /// written to the output stream.
187   void emitSLEB128Bytes(int Value) {
188     int Sign = Value >> (8 * sizeof(Value) - 1);
189     bool IsMore;
190   
191     do {
192       unsigned char Byte = Value & 0x7f;
193       Value >>= 7;
194       IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
195       if (IsMore) Byte |= 0x80;
196       emitByte(Byte);
197     } while (IsMore);
198   }
199
200   /// emitString - This callback is invoked when a String needs to be
201   /// written to the output stream.
202   void emitString(const std::string &String) {
203     for (unsigned i = 0, N = static_cast<unsigned>(String.size());
204          i < N; ++i) {
205       unsigned char C = String[i];
206       emitByte(C);
207     }
208     emitByte(0);
209   }
210   
211   /// emitInt32 - Emit a int32 directive.
212   void emitInt32(int Value) {
213     if (CurBufferPtr+4 <= BufferEnd) {
214       *((uint32_t*)CurBufferPtr) = Value;
215       CurBufferPtr += 4;
216     } else {
217       CurBufferPtr = BufferEnd;
218     }
219   }
220
221   /// emitInt64 - Emit a int64 directive.
222   void emitInt64(uint64_t Value) {
223     if (CurBufferPtr+8 <= BufferEnd) {
224       *((uint64_t*)CurBufferPtr) = Value;
225       CurBufferPtr += 8;
226     } else {
227       CurBufferPtr = BufferEnd;
228     }
229   }
230   
231   /// emitAt - Emit Value in Addr
232   void emitAt(uintptr_t *Addr, uintptr_t Value) {
233     if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd)
234       (*Addr) = Value;
235   }
236   
237   /// emitLabel - Emits a label
238   virtual void emitLabel(uint64_t LabelID) = 0;
239
240   /// allocateSpace - Allocate a block of space in the current output buffer,
241   /// returning null (and setting conditions to indicate buffer overflow) on
242   /// failure.  Alignment is the alignment in bytes of the buffer desired.
243   virtual void *allocateSpace(intptr_t Size, unsigned Alignment) {
244     emitAlignment(Alignment);
245     void *Result = CurBufferPtr;
246     
247     // Allocate the space.
248     CurBufferPtr += Size;
249     
250     // Check for buffer overflow.
251     if (CurBufferPtr >= BufferEnd) {
252       CurBufferPtr = BufferEnd;
253       Result = 0;
254     }
255     return Result;
256   }
257
258   /// StartMachineBasicBlock - This should be called by the target when a new
259   /// basic block is about to be emitted.  This way the MCE knows where the
260   /// start of the block is, and can implement getMachineBasicBlockAddress.
261   virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) = 0;
262   
263   /// getCurrentPCValue - This returns the address that the next emitted byte
264   /// will be output to.
265   ///
266   virtual intptr_t getCurrentPCValue() const {
267     return (intptr_t)CurBufferPtr;
268   }
269
270   /// getCurrentPCOffset - Return the offset from the start of the emitted
271   /// buffer that we are currently writing to.
272   intptr_t getCurrentPCOffset() const {
273     return CurBufferPtr-BufferBegin;
274   }
275
276   /// addRelocation - Whenever a relocatable address is needed, it should be
277   /// noted with this interface.
278   virtual void addRelocation(const MachineRelocation &MR) = 0;
279
280   
281   /// FIXME: These should all be handled with relocations!
282   
283   /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in
284   /// the constant pool that was last emitted with the emitConstantPool method.
285   ///
286   virtual intptr_t getConstantPoolEntryAddress(unsigned Index) const = 0;
287
288   /// getJumpTableEntryAddress - Return the address of the jump table with index
289   /// 'Index' in the function that last called initJumpTableInfo.
290   ///
291   virtual intptr_t getJumpTableEntryAddress(unsigned Index) const = 0;
292   
293   /// getMachineBasicBlockAddress - Return the address of the specified
294   /// MachineBasicBlock, only usable after the label for the MBB has been
295   /// emitted.
296   ///
297   virtual intptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const= 0;
298
299   /// getLabelAddress - Return the address of the specified LabelID, only usable
300   /// after the LabelID has been emitted.
301   ///
302   virtual intptr_t getLabelAddress(uint64_t LabelID) const = 0;
303   
304   /// Specifies the MachineModuleInfo object. This is used for exception handling
305   /// purposes.
306   virtual void setModuleInfo(MachineModuleInfo* Info) = 0;
307 };
308
309 } // End llvm namespace
310
311 #endif