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