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