Convert more abort() calls to llvm_report_error().
[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 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/raw_ostream.h"
23
24 namespace llvm {
25
26 class MachineBasicBlock;
27 class MachineConstantPool;
28 class MachineJumpTableInfo;
29 class MachineFunction;
30 class MachineModuleInfo;
31 class MachineRelocation;
32 class Value;
33 class GlobalValue;
34 class Function;
35
36 /// MachineCodeEmitter - This class defines two sorts of methods: those for
37 /// emitting the actual bytes of machine code, and those for emitting auxillary
38 /// structures, such as jump tables, relocations, etc.
39 ///
40 /// Emission of machine code is complicated by the fact that we don't (in
41 /// general) know the size of the machine code that we're about to emit before
42 /// we emit it.  As such, we preallocate a certain amount of memory, and set the
43 /// BufferBegin/BufferEnd pointers to the start and end of the buffer.  As we
44 /// emit machine instructions, we advance the CurBufferPtr to indicate the
45 /// location of the next byte to emit.  In the case of a buffer overflow (we
46 /// need to emit more machine code than we have allocated space for), the
47 /// CurBufferPtr will saturate to BufferEnd and ignore stores.  Once the entire
48 /// function has been emitted, the overflow condition is checked, and if it has
49 /// occurred, more memory is allocated, and we reemit the code into it.
50 /// 
51 class MachineCodeEmitter {
52 protected:
53   /// BufferBegin/BufferEnd - Pointers to the start and end of the memory
54   /// allocated for this code buffer.
55   uint8_t *BufferBegin, *BufferEnd;
56   
57   /// CurBufferPtr - Pointer to the next byte of memory to fill when emitting 
58   /// code.  This is guranteed to be in the range [BufferBegin,BufferEnd].  If
59   /// this pointer is at BufferEnd, it will never move due to code emission, and
60   /// all code emission requests will be ignored (this is the buffer overflow
61   /// condition).
62   uint8_t *CurBufferPtr;
63
64 public:
65   virtual ~MachineCodeEmitter() {}
66
67   /// startFunction - This callback is invoked when the specified function is
68   /// about to be code generated.  This initializes the BufferBegin/End/Ptr
69   /// fields.
70   ///
71   virtual void startFunction(MachineFunction &F) = 0;
72
73   /// finishFunction - This callback is invoked when the specified function has
74   /// finished code generation.  If a buffer overflow has occurred, this method
75   /// returns true (the callee is required to try again), otherwise it returns
76   /// false.
77   ///
78   virtual bool finishFunction(MachineFunction &F) = 0;
79
80   /// emitByte - This callback is invoked when a byte needs to be written to the
81   /// output stream.
82   ///
83   void emitByte(uint8_t B) {
84     if (CurBufferPtr != BufferEnd)
85       *CurBufferPtr++ = B;
86   }
87
88   /// emitWordLE - This callback is invoked when a 32-bit word needs to be
89   /// written to the output stream in little-endian format.
90   ///
91   void emitWordLE(uint32_t W) {
92     if (4 <= BufferEnd-CurBufferPtr) {
93       *CurBufferPtr++ = (uint8_t)(W >>  0);
94       *CurBufferPtr++ = (uint8_t)(W >>  8);
95       *CurBufferPtr++ = (uint8_t)(W >> 16);
96       *CurBufferPtr++ = (uint8_t)(W >> 24);
97     } else {
98       CurBufferPtr = BufferEnd;
99     }
100   }
101   
102   /// emitWordBE - This callback is invoked when a 32-bit word needs to be
103   /// written to the output stream in big-endian format.
104   ///
105   void emitWordBE(uint32_t W) {
106     if (4 <= BufferEnd-CurBufferPtr) {
107       *CurBufferPtr++ = (uint8_t)(W >> 24);
108       *CurBufferPtr++ = (uint8_t)(W >> 16);
109       *CurBufferPtr++ = (uint8_t)(W >>  8);
110       *CurBufferPtr++ = (uint8_t)(W >>  0);
111     } else {
112       CurBufferPtr = BufferEnd;
113     }
114   }
115
116   /// emitDWordLE - This callback is invoked when a 64-bit word needs to be
117   /// written to the output stream in little-endian format.
118   ///
119   void emitDWordLE(uint64_t W) {
120     if (8 <= BufferEnd-CurBufferPtr) {
121       *CurBufferPtr++ = (uint8_t)(W >>  0);
122       *CurBufferPtr++ = (uint8_t)(W >>  8);
123       *CurBufferPtr++ = (uint8_t)(W >> 16);
124       *CurBufferPtr++ = (uint8_t)(W >> 24);
125       *CurBufferPtr++ = (uint8_t)(W >> 32);
126       *CurBufferPtr++ = (uint8_t)(W >> 40);
127       *CurBufferPtr++ = (uint8_t)(W >> 48);
128       *CurBufferPtr++ = (uint8_t)(W >> 56);
129     } else {
130       CurBufferPtr = BufferEnd;
131     }
132   }
133   
134   /// emitDWordBE - This callback is invoked when a 64-bit word needs to be
135   /// written to the output stream in big-endian format.
136   ///
137   void emitDWordBE(uint64_t W) {
138     if (8 <= BufferEnd-CurBufferPtr) {
139       *CurBufferPtr++ = (uint8_t)(W >> 56);
140       *CurBufferPtr++ = (uint8_t)(W >> 48);
141       *CurBufferPtr++ = (uint8_t)(W >> 40);
142       *CurBufferPtr++ = (uint8_t)(W >> 32);
143       *CurBufferPtr++ = (uint8_t)(W >> 24);
144       *CurBufferPtr++ = (uint8_t)(W >> 16);
145       *CurBufferPtr++ = (uint8_t)(W >>  8);
146       *CurBufferPtr++ = (uint8_t)(W >>  0);
147     } else {
148       CurBufferPtr = BufferEnd;
149     }
150   }
151
152   /// emitAlignment - Move the CurBufferPtr pointer up the the specified
153   /// alignment (saturated to BufferEnd of course).
154   void emitAlignment(unsigned Alignment) {
155     if (Alignment == 0) Alignment = 1;
156
157     if(Alignment <= (uintptr_t)(BufferEnd-CurBufferPtr)) {
158       // Move the current buffer ptr up to the specified alignment.
159       CurBufferPtr =
160         (uint8_t*)(((uintptr_t)CurBufferPtr+Alignment-1) &
161                    ~(uintptr_t)(Alignment-1));
162     } else {
163       CurBufferPtr = BufferEnd;
164     }
165   }
166   
167
168   /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be
169   /// written to the output stream.
170   void emitULEB128Bytes(uint64_t Value) {
171     do {
172       uint8_t Byte = Value & 0x7f;
173       Value >>= 7;
174       if (Value) Byte |= 0x80;
175       emitByte(Byte);
176     } while (Value);
177   }
178   
179   /// emitSLEB128Bytes - This callback is invoked when a SLEB128 needs to be
180   /// written to the output stream.
181   void emitSLEB128Bytes(uint64_t Value) {
182     uint64_t Sign = Value >> (8 * sizeof(Value) - 1);
183     bool IsMore;
184   
185     do {
186       uint8_t Byte = Value & 0x7f;
187       Value >>= 7;
188       IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
189       if (IsMore) Byte |= 0x80;
190       emitByte(Byte);
191     } while (IsMore);
192   }
193
194   /// emitString - This callback is invoked when a String needs to be
195   /// written to the output stream.
196   void emitString(const std::string &String) {
197     for (unsigned i = 0, N = static_cast<unsigned>(String.size());
198          i < N; ++i) {
199       uint8_t C = String[i];
200       emitByte(C);
201     }
202     emitByte(0);
203   }
204   
205   /// emitInt32 - Emit a int32 directive.
206   void emitInt32(int32_t Value) {
207     if (4 <= BufferEnd-CurBufferPtr) {
208       *((uint32_t*)CurBufferPtr) = Value;
209       CurBufferPtr += 4;
210     } else {
211       CurBufferPtr = BufferEnd;
212     }
213   }
214
215   /// emitInt64 - Emit a int64 directive.
216   void emitInt64(uint64_t Value) {
217     if (8 <= BufferEnd-CurBufferPtr) {
218       *((uint64_t*)CurBufferPtr) = Value;
219       CurBufferPtr += 8;
220     } else {
221       CurBufferPtr = BufferEnd;
222     }
223   }
224   
225   /// emitInt32At - Emit the Int32 Value in Addr.
226   void emitInt32At(uintptr_t *Addr, uintptr_t Value) {
227     if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd)
228       (*(uint32_t*)Addr) = (uint32_t)Value;
229   }
230   
231   /// emitInt64At - Emit the Int64 Value in Addr.
232   void emitInt64At(uintptr_t *Addr, uintptr_t Value) {
233     if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd)
234       (*(uint64_t*)Addr) = (uint64_t)Value;
235   }
236   
237   
238   /// emitLabel - Emits a label
239   virtual void emitLabel(uint64_t LabelID) = 0;
240
241   /// allocateSpace - Allocate a block of space in the current output buffer,
242   /// returning null (and setting conditions to indicate buffer overflow) on
243   /// failure.  Alignment is the alignment in bytes of the buffer desired.
244   virtual void *allocateSpace(uintptr_t Size, unsigned Alignment) {
245     emitAlignment(Alignment);
246     void *Result;
247     
248     // Check for buffer overflow.
249     if (Size >= (uintptr_t)(BufferEnd-CurBufferPtr)) {
250       CurBufferPtr = BufferEnd;
251       Result = 0;
252     } else {
253       // Allocate the space.
254       Result = CurBufferPtr;
255       CurBufferPtr += Size;
256     }
257     
258     return Result;
259   }
260
261   /// StartMachineBasicBlock - This should be called by the target when a new
262   /// basic block is about to be emitted.  This way the MCE knows where the
263   /// start of the block is, and can implement getMachineBasicBlockAddress.
264   virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) = 0;
265   
266   /// getCurrentPCValue - This returns the address that the next emitted byte
267   /// will be output to.
268   ///
269   virtual uintptr_t getCurrentPCValue() const {
270     return (uintptr_t)CurBufferPtr;
271   }
272
273   /// getCurrentPCOffset - Return the offset from the start of the emitted
274   /// buffer that we are currently writing to.
275   virtual uintptr_t getCurrentPCOffset() const {
276     return CurBufferPtr-BufferBegin;
277   }
278
279   /// addRelocation - Whenever a relocatable address is needed, it should be
280   /// noted with this interface.
281   virtual void addRelocation(const MachineRelocation &MR) = 0;
282   
283   /// FIXME: These should all be handled with relocations!
284   
285   /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in
286   /// the constant pool that was last emitted with the emitConstantPool method.
287   ///
288   virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const = 0;
289
290   /// getJumpTableEntryAddress - Return the address of the jump table with index
291   /// 'Index' in the function that last called initJumpTableInfo.
292   ///
293   virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const = 0;
294   
295   /// getMachineBasicBlockAddress - Return the address of the specified
296   /// MachineBasicBlock, only usable after the label for the MBB has been
297   /// emitted.
298   ///
299   virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const= 0;
300
301   /// getLabelAddress - Return the address of the specified LabelID, only usable
302   /// after the LabelID has been emitted.
303   ///
304   virtual uintptr_t getLabelAddress(uint64_t LabelID) const = 0;
305   
306   /// Specifies the MachineModuleInfo object. This is used for exception handling
307   /// purposes.
308   virtual void setModuleInfo(MachineModuleInfo* Info) = 0;
309 };
310
311 } // End llvm namespace
312
313 #endif