eb1ea2dc56b1ded887710b01d735eb8df8728ca7
[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   uint8_t *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   uint8_t *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   /// startGVStub - This callback is invoked when the JIT needs the address of a 
86   /// GV (e.g. function) that has not been code generated yet.  Buffer points to
87   /// memory already allocated for this stub.
88   ///
89   virtual void startGVStub(const GlobalValue* GV, void *Buffer,
90                            unsigned StubSize) = 0;
91   
92   /// finishGVStub - This callback is invoked to terminate a GV stub.
93   ///
94   virtual void *finishGVStub(const GlobalValue* F) = 0;
95
96   /// emitByte - This callback is invoked when a byte needs to be written to the
97   /// output stream.
98   ///
99   void emitByte(uint8_t B) {
100     if (CurBufferPtr != BufferEnd)
101       *CurBufferPtr++ = B;
102   }
103
104   /// emitWordLE - This callback is invoked when a 32-bit word needs to be
105   /// written to the output stream in little-endian format.
106   ///
107   void emitWordLE(uint32_t W) {
108     if (4 <= BufferEnd-CurBufferPtr) {
109       *CurBufferPtr++ = (uint8_t)(W >>  0);
110       *CurBufferPtr++ = (uint8_t)(W >>  8);
111       *CurBufferPtr++ = (uint8_t)(W >> 16);
112       *CurBufferPtr++ = (uint8_t)(W >> 24);
113     } else {
114       CurBufferPtr = BufferEnd;
115     }
116   }
117   
118   /// emitWordBE - This callback is invoked when a 32-bit word needs to be
119   /// written to the output stream in big-endian format.
120   ///
121   void emitWordBE(uint32_t W) {
122     if (4 <= BufferEnd-CurBufferPtr) {
123       *CurBufferPtr++ = (uint8_t)(W >> 24);
124       *CurBufferPtr++ = (uint8_t)(W >> 16);
125       *CurBufferPtr++ = (uint8_t)(W >>  8);
126       *CurBufferPtr++ = (uint8_t)(W >>  0);
127     } else {
128       CurBufferPtr = BufferEnd;
129     }
130   }
131
132   /// emitDWordLE - This callback is invoked when a 64-bit word needs to be
133   /// written to the output stream in little-endian format.
134   ///
135   void emitDWordLE(uint64_t W) {
136     if (8 <= BufferEnd-CurBufferPtr) {
137       *CurBufferPtr++ = (uint8_t)(W >>  0);
138       *CurBufferPtr++ = (uint8_t)(W >>  8);
139       *CurBufferPtr++ = (uint8_t)(W >> 16);
140       *CurBufferPtr++ = (uint8_t)(W >> 24);
141       *CurBufferPtr++ = (uint8_t)(W >> 32);
142       *CurBufferPtr++ = (uint8_t)(W >> 40);
143       *CurBufferPtr++ = (uint8_t)(W >> 48);
144       *CurBufferPtr++ = (uint8_t)(W >> 56);
145     } else {
146       CurBufferPtr = BufferEnd;
147     }
148   }
149   
150   /// emitDWordBE - This callback is invoked when a 64-bit word needs to be
151   /// written to the output stream in big-endian format.
152   ///
153   void emitDWordBE(uint64_t W) {
154     if (8 <= BufferEnd-CurBufferPtr) {
155       *CurBufferPtr++ = (uint8_t)(W >> 56);
156       *CurBufferPtr++ = (uint8_t)(W >> 48);
157       *CurBufferPtr++ = (uint8_t)(W >> 40);
158       *CurBufferPtr++ = (uint8_t)(W >> 32);
159       *CurBufferPtr++ = (uint8_t)(W >> 24);
160       *CurBufferPtr++ = (uint8_t)(W >> 16);
161       *CurBufferPtr++ = (uint8_t)(W >>  8);
162       *CurBufferPtr++ = (uint8_t)(W >>  0);
163     } else {
164       CurBufferPtr = BufferEnd;
165     }
166   }
167
168   /// emitAlignment - Move the CurBufferPtr pointer up the the specified
169   /// alignment (saturated to BufferEnd of course).
170   void emitAlignment(unsigned Alignment) {
171     if (Alignment == 0) Alignment = 1;
172
173     if(Alignment <= (uintptr_t)(BufferEnd-CurBufferPtr)) {
174       // Move the current buffer ptr up to the specified alignment.
175       CurBufferPtr =
176         (uint8_t*)(((uintptr_t)CurBufferPtr+Alignment-1) &
177                    ~(uintptr_t)(Alignment-1));
178     } else {
179       CurBufferPtr = BufferEnd;
180     }
181   }
182   
183
184   /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be
185   /// written to the output stream.
186   void emitULEB128Bytes(uint64_t Value) {
187     do {
188       uint8_t Byte = Value & 0x7f;
189       Value >>= 7;
190       if (Value) Byte |= 0x80;
191       emitByte(Byte);
192     } while (Value);
193   }
194   
195   /// emitSLEB128Bytes - This callback is invoked when a SLEB128 needs to be
196   /// written to the output stream.
197   void emitSLEB128Bytes(uint64_t Value) {
198     uint64_t Sign = Value >> (8 * sizeof(Value) - 1);
199     bool IsMore;
200   
201     do {
202       uint8_t Byte = Value & 0x7f;
203       Value >>= 7;
204       IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
205       if (IsMore) Byte |= 0x80;
206       emitByte(Byte);
207     } while (IsMore);
208   }
209
210   /// emitString - This callback is invoked when a String needs to be
211   /// written to the output stream.
212   void emitString(const std::string &String) {
213     for (unsigned i = 0, N = static_cast<unsigned>(String.size());
214          i < N; ++i) {
215       uint8_t C = String[i];
216       emitByte(C);
217     }
218     emitByte(0);
219   }
220   
221   /// emitInt32 - Emit a int32 directive.
222   void emitInt32(int32_t Value) {
223     if (4 <= BufferEnd-CurBufferPtr) {
224       *((uint32_t*)CurBufferPtr) = Value;
225       CurBufferPtr += 4;
226     } else {
227       CurBufferPtr = BufferEnd;
228     }
229   }
230
231   /// emitInt64 - Emit a int64 directive.
232   void emitInt64(uint64_t Value) {
233     if (8 <= BufferEnd-CurBufferPtr) {
234       *((uint64_t*)CurBufferPtr) = Value;
235       CurBufferPtr += 8;
236     } else {
237       CurBufferPtr = BufferEnd;
238     }
239   }
240   
241   /// emitInt32At - Emit the Int32 Value in Addr.
242   void emitInt32At(uintptr_t *Addr, uintptr_t Value) {
243     if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd)
244       (*(uint32_t*)Addr) = (uint32_t)Value;
245   }
246   
247   /// emitInt64At - Emit the Int64 Value in Addr.
248   void emitInt64At(uintptr_t *Addr, uintptr_t Value) {
249     if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd)
250       (*(uint64_t*)Addr) = (uint64_t)Value;
251   }
252   
253   
254   /// emitLabel - Emits a label
255   virtual void emitLabel(uint64_t LabelID) = 0;
256
257   /// allocateSpace - Allocate a block of space in the current output buffer,
258   /// returning null (and setting conditions to indicate buffer overflow) on
259   /// failure.  Alignment is the alignment in bytes of the buffer desired.
260   virtual void *allocateSpace(uintptr_t Size, unsigned Alignment) {
261     emitAlignment(Alignment);
262     void *Result;
263     
264     // Check for buffer overflow.
265     if (Size >= (uintptr_t)(BufferEnd-CurBufferPtr)) {
266       CurBufferPtr = BufferEnd;
267       Result = 0;
268     } else {
269       // Allocate the space.
270       Result = CurBufferPtr;
271       CurBufferPtr += Size;
272     }
273     
274     return Result;
275   }
276
277   /// StartMachineBasicBlock - This should be called by the target when a new
278   /// basic block is about to be emitted.  This way the MCE knows where the
279   /// start of the block is, and can implement getMachineBasicBlockAddress.
280   virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) = 0;
281   
282   /// getCurrentPCValue - This returns the address that the next emitted byte
283   /// will be output to.
284   ///
285   virtual uintptr_t getCurrentPCValue() const {
286     return (uintptr_t)CurBufferPtr;
287   }
288
289   /// getCurrentPCOffset - Return the offset from the start of the emitted
290   /// buffer that we are currently writing to.
291   uintptr_t getCurrentPCOffset() const {
292     return CurBufferPtr-BufferBegin;
293   }
294
295   /// addRelocation - Whenever a relocatable address is needed, it should be
296   /// noted with this interface.
297   virtual void addRelocation(const MachineRelocation &MR) = 0;
298
299   
300   /// FIXME: These should all be handled with relocations!
301   
302   /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in
303   /// the constant pool that was last emitted with the emitConstantPool method.
304   ///
305   virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const = 0;
306
307   /// getJumpTableEntryAddress - Return the address of the jump table with index
308   /// 'Index' in the function that last called initJumpTableInfo.
309   ///
310   virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const = 0;
311   
312   /// getMachineBasicBlockAddress - Return the address of the specified
313   /// MachineBasicBlock, only usable after the label for the MBB has been
314   /// emitted.
315   ///
316   virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const= 0;
317
318   /// getLabelAddress - Return the address of the specified LabelID, only usable
319   /// after the LabelID has been emitted.
320   ///
321   virtual uintptr_t getLabelAddress(uint64_t LabelID) const = 0;
322   
323   /// Specifies the MachineModuleInfo object. This is used for exception handling
324   /// purposes.
325   virtual void setModuleInfo(MachineModuleInfo* Info) = 0;
326 };
327
328 } // End llvm namespace
329
330 #endif