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