Implement changes from Chris's feedback.
[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   /// emitByte - This callback is invoked when a byte needs to be written to the
79   /// output stream.
80   ///
81   void emitByte(uint8_t B) {
82     if (CurBufferPtr != BufferEnd)
83       *CurBufferPtr++ = B;
84   }
85
86   /// emitWordLE - This callback is invoked when a 32-bit word needs to be
87   /// written to the output stream in little-endian format.
88   ///
89   void emitWordLE(uint32_t W) {
90     if (4 <= BufferEnd-CurBufferPtr) {
91       *CurBufferPtr++ = (uint8_t)(W >>  0);
92       *CurBufferPtr++ = (uint8_t)(W >>  8);
93       *CurBufferPtr++ = (uint8_t)(W >> 16);
94       *CurBufferPtr++ = (uint8_t)(W >> 24);
95     } else {
96       CurBufferPtr = BufferEnd;
97     }
98   }
99   
100   /// emitWordBE - This callback is invoked when a 32-bit word needs to be
101   /// written to the output stream in big-endian format.
102   ///
103   void emitWordBE(uint32_t W) {
104     if (4 <= BufferEnd-CurBufferPtr) {
105       *CurBufferPtr++ = (uint8_t)(W >> 24);
106       *CurBufferPtr++ = (uint8_t)(W >> 16);
107       *CurBufferPtr++ = (uint8_t)(W >>  8);
108       *CurBufferPtr++ = (uint8_t)(W >>  0);
109     } else {
110       CurBufferPtr = BufferEnd;
111     }
112   }
113
114   /// emitDWordLE - This callback is invoked when a 64-bit word needs to be
115   /// written to the output stream in little-endian format.
116   ///
117   void emitDWordLE(uint64_t W) {
118     if (8 <= BufferEnd-CurBufferPtr) {
119       *CurBufferPtr++ = (uint8_t)(W >>  0);
120       *CurBufferPtr++ = (uint8_t)(W >>  8);
121       *CurBufferPtr++ = (uint8_t)(W >> 16);
122       *CurBufferPtr++ = (uint8_t)(W >> 24);
123       *CurBufferPtr++ = (uint8_t)(W >> 32);
124       *CurBufferPtr++ = (uint8_t)(W >> 40);
125       *CurBufferPtr++ = (uint8_t)(W >> 48);
126       *CurBufferPtr++ = (uint8_t)(W >> 56);
127     } else {
128       CurBufferPtr = BufferEnd;
129     }
130   }
131   
132   /// emitDWordBE - This callback is invoked when a 64-bit word needs to be
133   /// written to the output stream in big-endian format.
134   ///
135   void emitDWordBE(uint64_t W) {
136     if (8 <= BufferEnd-CurBufferPtr) {
137       *CurBufferPtr++ = (uint8_t)(W >> 56);
138       *CurBufferPtr++ = (uint8_t)(W >> 48);
139       *CurBufferPtr++ = (uint8_t)(W >> 40);
140       *CurBufferPtr++ = (uint8_t)(W >> 32);
141       *CurBufferPtr++ = (uint8_t)(W >> 24);
142       *CurBufferPtr++ = (uint8_t)(W >> 16);
143       *CurBufferPtr++ = (uint8_t)(W >>  8);
144       *CurBufferPtr++ = (uint8_t)(W >>  0);
145     } else {
146       CurBufferPtr = BufferEnd;
147     }
148   }
149
150   /// emitAlignment - Move the CurBufferPtr pointer up the the specified
151   /// alignment (saturated to BufferEnd of course).
152   void emitAlignment(unsigned Alignment) {
153     if (Alignment == 0) Alignment = 1;
154
155     if(Alignment <= (uintptr_t)(BufferEnd-CurBufferPtr)) {
156       // Move the current buffer ptr up to the specified alignment.
157       CurBufferPtr =
158         (uint8_t*)(((uintptr_t)CurBufferPtr+Alignment-1) &
159                    ~(uintptr_t)(Alignment-1));
160     } else {
161       CurBufferPtr = BufferEnd;
162     }
163   }
164   
165
166   /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be
167   /// written to the output stream.
168   void emitULEB128Bytes(uint64_t Value) {
169     do {
170       uint8_t Byte = Value & 0x7f;
171       Value >>= 7;
172       if (Value) Byte |= 0x80;
173       emitByte(Byte);
174     } while (Value);
175   }
176   
177   /// emitSLEB128Bytes - This callback is invoked when a SLEB128 needs to be
178   /// written to the output stream.
179   void emitSLEB128Bytes(uint64_t Value) {
180     uint64_t Sign = Value >> (8 * sizeof(Value) - 1);
181     bool IsMore;
182   
183     do {
184       uint8_t Byte = Value & 0x7f;
185       Value >>= 7;
186       IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
187       if (IsMore) Byte |= 0x80;
188       emitByte(Byte);
189     } while (IsMore);
190   }
191
192   /// emitString - This callback is invoked when a String needs to be
193   /// written to the output stream.
194   void emitString(const std::string &String) {
195     for (unsigned i = 0, N = static_cast<unsigned>(String.size());
196          i < N; ++i) {
197       uint8_t C = String[i];
198       emitByte(C);
199     }
200     emitByte(0);
201   }
202   
203   /// emitInt32 - Emit a int32 directive.
204   void emitInt32(int32_t Value) {
205     if (4 <= BufferEnd-CurBufferPtr) {
206       *((uint32_t*)CurBufferPtr) = Value;
207       CurBufferPtr += 4;
208     } else {
209       CurBufferPtr = BufferEnd;
210     }
211   }
212
213   /// emitInt64 - Emit a int64 directive.
214   void emitInt64(uint64_t Value) {
215     if (8 <= BufferEnd-CurBufferPtr) {
216       *((uint64_t*)CurBufferPtr) = Value;
217       CurBufferPtr += 8;
218     } else {
219       CurBufferPtr = BufferEnd;
220     }
221   }
222   
223   /// emitInt32At - Emit the Int32 Value in Addr.
224   void emitInt32At(uintptr_t *Addr, uintptr_t Value) {
225     if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd)
226       (*(uint32_t*)Addr) = (uint32_t)Value;
227   }
228   
229   /// emitInt64At - Emit the Int64 Value in Addr.
230   void emitInt64At(uintptr_t *Addr, uintptr_t Value) {
231     if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd)
232       (*(uint64_t*)Addr) = (uint64_t)Value;
233   }
234   
235   
236   /// emitLabel - Emits a label
237   virtual void emitLabel(uint64_t LabelID) = 0;
238
239   /// allocateSpace - Allocate a block of space in the current output buffer,
240   /// returning null (and setting conditions to indicate buffer overflow) on
241   /// failure.  Alignment is the alignment in bytes of the buffer desired.
242   virtual void *allocateSpace(uintptr_t Size, unsigned Alignment) {
243     emitAlignment(Alignment);
244     void *Result;
245     
246     // Check for buffer overflow.
247     if (Size >= (uintptr_t)(BufferEnd-CurBufferPtr)) {
248       CurBufferPtr = BufferEnd;
249       Result = 0;
250     } else {
251       // Allocate the space.
252       Result = CurBufferPtr;
253       CurBufferPtr += Size;
254     }
255     
256     return Result;
257   }
258
259   /// StartMachineBasicBlock - This should be called by the target when a new
260   /// basic block is about to be emitted.  This way the MCE knows where the
261   /// start of the block is, and can implement getMachineBasicBlockAddress.
262   virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) = 0;
263   
264   /// getCurrentPCValue - This returns the address that the next emitted byte
265   /// will be output to.
266   ///
267   virtual uintptr_t getCurrentPCValue() const {
268     return (uintptr_t)CurBufferPtr;
269   }
270
271   /// getCurrentPCOffset - Return the offset from the start of the emitted
272   /// buffer that we are currently writing to.
273   virtual uintptr_t getCurrentPCOffset() const {
274     return CurBufferPtr-BufferBegin;
275   }
276
277   /// addRelocation - Whenever a relocatable address is needed, it should be
278   /// noted with this interface.
279   virtual void addRelocation(const MachineRelocation &MR) = 0;
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 uintptr_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 uintptr_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 uintptr_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 uintptr_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