1193323Sed//===-- llvm/CodeGen/MachineCodeEmitter.h - Code emission -------*- C++ -*-===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This file defines an abstract interface that is used by the machine code
11193323Sed// emission framework to output the code.  This allows machine code emission to
12193323Sed// be separated from concerns such as resolution of call targets, and where the
13193323Sed// machine code will be written (memory or disk, f.e.).
14193323Sed//
15193323Sed//===----------------------------------------------------------------------===//
16193323Sed
17193323Sed#ifndef LLVM_CODEGEN_MACHINECODEEMITTER_H
18193323Sed#define LLVM_CODEGEN_MACHINECODEEMITTER_H
19193323Sed
20218893Sdim#include "llvm/Support/DataTypes.h"
21198090Srdivacky#include "llvm/Support/DebugLoc.h"
22234353Sdim#include <string>
23234353Sdim
24193323Sednamespace llvm {
25193323Sed
26193323Sedclass MachineBasicBlock;
27193323Sedclass MachineConstantPool;
28193323Sedclass MachineJumpTableInfo;
29193323Sedclass MachineFunction;
30193323Sedclass MachineModuleInfo;
31193323Sedclass MachineRelocation;
32193323Sedclass Value;
33193323Sedclass GlobalValue;
34193323Sedclass Function;
35205218Srdivackyclass MCSymbol;
36193323Sed
37193323Sed/// MachineCodeEmitter - This class defines two sorts of methods: those for
38221345Sdim/// emitting the actual bytes of machine code, and those for emitting auxiliary
39193323Sed/// structures, such as jump tables, relocations, etc.
40193323Sed///
41193323Sed/// Emission of machine code is complicated by the fact that we don't (in
42193323Sed/// general) know the size of the machine code that we're about to emit before
43193323Sed/// we emit it.  As such, we preallocate a certain amount of memory, and set the
44193323Sed/// BufferBegin/BufferEnd pointers to the start and end of the buffer.  As we
45193323Sed/// emit machine instructions, we advance the CurBufferPtr to indicate the
46193323Sed/// location of the next byte to emit.  In the case of a buffer overflow (we
47193323Sed/// need to emit more machine code than we have allocated space for), the
48193323Sed/// CurBufferPtr will saturate to BufferEnd and ignore stores.  Once the entire
49193323Sed/// function has been emitted, the overflow condition is checked, and if it has
50193323Sed/// occurred, more memory is allocated, and we reemit the code into it.
51193323Sed///
52193323Sedclass MachineCodeEmitter {
53234353Sdim  virtual void anchor();
54193323Sedprotected:
55201360Srdivacky  /// BufferBegin/BufferEnd - Pointers to the start and end of the memory
56201360Srdivacky  /// allocated for this code buffer.
57201360Srdivacky  uint8_t *BufferBegin, *BufferEnd;
58201360Srdivacky  /// CurBufferPtr - Pointer to the next byte of memory to fill when emitting
59221345Sdim  /// code.  This is guaranteed to be in the range [BufferBegin,BufferEnd].  If
60201360Srdivacky  /// this pointer is at BufferEnd, it will never move due to code emission, and
61201360Srdivacky  /// all code emission requests will be ignored (this is the buffer overflow
62201360Srdivacky  /// condition).
63201360Srdivacky  uint8_t *CurBufferPtr;
64193323Sed
65193323Sedpublic:
66193323Sed  virtual ~MachineCodeEmitter() {}
67193323Sed
68193323Sed  /// startFunction - This callback is invoked when the specified function is
69193323Sed  /// about to be code generated.  This initializes the BufferBegin/End/Ptr
70193323Sed  /// fields.
71193323Sed  ///
72193323Sed  virtual void startFunction(MachineFunction &F) = 0;
73193323Sed
74193323Sed  /// finishFunction - This callback is invoked when the specified function has
75193323Sed  /// finished code generation.  If a buffer overflow has occurred, this method
76193323Sed  /// returns true (the callee is required to try again), otherwise it returns
77193323Sed  /// false.
78193323Sed  ///
79193323Sed  virtual bool finishFunction(MachineFunction &F) = 0;
80193323Sed
81193323Sed  /// emitByte - This callback is invoked when a byte needs to be written to the
82193323Sed  /// output stream.
83193323Sed  ///
84193574Sed  void emitByte(uint8_t B) {
85193323Sed    if (CurBufferPtr != BufferEnd)
86193323Sed      *CurBufferPtr++ = B;
87193323Sed  }
88193323Sed
89193323Sed  /// emitWordLE - This callback is invoked when a 32-bit word needs to be
90193323Sed  /// written to the output stream in little-endian format.
91193323Sed  ///
92194178Sed  void emitWordLE(uint32_t W) {
93193323Sed    if (4 <= BufferEnd-CurBufferPtr) {
94201360Srdivacky      emitWordLEInto(CurBufferPtr, W);
95193323Sed    } else {
96193323Sed      CurBufferPtr = BufferEnd;
97193323Sed    }
98193323Sed  }
99201360Srdivacky
100201360Srdivacky  /// emitWordLEInto - This callback is invoked when a 32-bit word needs to be
101201360Srdivacky  /// written to an arbitrary buffer in little-endian format.  Buf must have at
102201360Srdivacky  /// least 4 bytes of available space.
103201360Srdivacky  ///
104201360Srdivacky  static void emitWordLEInto(uint8_t *&Buf, uint32_t W) {
105201360Srdivacky    *Buf++ = (uint8_t)(W >>  0);
106201360Srdivacky    *Buf++ = (uint8_t)(W >>  8);
107201360Srdivacky    *Buf++ = (uint8_t)(W >> 16);
108201360Srdivacky    *Buf++ = (uint8_t)(W >> 24);
109201360Srdivacky  }
110201360Srdivacky
111193323Sed  /// emitWordBE - This callback is invoked when a 32-bit word needs to be
112193323Sed  /// written to the output stream in big-endian format.
113193323Sed  ///
114194178Sed  void emitWordBE(uint32_t W) {
115193323Sed    if (4 <= BufferEnd-CurBufferPtr) {
116193574Sed      *CurBufferPtr++ = (uint8_t)(W >> 24);
117193574Sed      *CurBufferPtr++ = (uint8_t)(W >> 16);
118193574Sed      *CurBufferPtr++ = (uint8_t)(W >>  8);
119193574Sed      *CurBufferPtr++ = (uint8_t)(W >>  0);
120193323Sed    } else {
121193323Sed      CurBufferPtr = BufferEnd;
122193323Sed    }
123193323Sed  }
124193323Sed
125193323Sed  /// emitDWordLE - This callback is invoked when a 64-bit word needs to be
126193323Sed  /// written to the output stream in little-endian format.
127193323Sed  ///
128193323Sed  void emitDWordLE(uint64_t W) {
129193323Sed    if (8 <= BufferEnd-CurBufferPtr) {
130193574Sed      *CurBufferPtr++ = (uint8_t)(W >>  0);
131193574Sed      *CurBufferPtr++ = (uint8_t)(W >>  8);
132193574Sed      *CurBufferPtr++ = (uint8_t)(W >> 16);
133193574Sed      *CurBufferPtr++ = (uint8_t)(W >> 24);
134193574Sed      *CurBufferPtr++ = (uint8_t)(W >> 32);
135193574Sed      *CurBufferPtr++ = (uint8_t)(W >> 40);
136193574Sed      *CurBufferPtr++ = (uint8_t)(W >> 48);
137193574Sed      *CurBufferPtr++ = (uint8_t)(W >> 56);
138193323Sed    } else {
139193323Sed      CurBufferPtr = BufferEnd;
140193323Sed    }
141193323Sed  }
142193323Sed
143193323Sed  /// emitDWordBE - This callback is invoked when a 64-bit word needs to be
144193323Sed  /// written to the output stream in big-endian format.
145193323Sed  ///
146193323Sed  void emitDWordBE(uint64_t W) {
147193323Sed    if (8 <= BufferEnd-CurBufferPtr) {
148193574Sed      *CurBufferPtr++ = (uint8_t)(W >> 56);
149193574Sed      *CurBufferPtr++ = (uint8_t)(W >> 48);
150193574Sed      *CurBufferPtr++ = (uint8_t)(W >> 40);
151193574Sed      *CurBufferPtr++ = (uint8_t)(W >> 32);
152193574Sed      *CurBufferPtr++ = (uint8_t)(W >> 24);
153193574Sed      *CurBufferPtr++ = (uint8_t)(W >> 16);
154193574Sed      *CurBufferPtr++ = (uint8_t)(W >>  8);
155193574Sed      *CurBufferPtr++ = (uint8_t)(W >>  0);
156193323Sed    } else {
157193323Sed      CurBufferPtr = BufferEnd;
158193323Sed    }
159193323Sed  }
160193323Sed
161203954Srdivacky  /// emitAlignment - Move the CurBufferPtr pointer up to the specified
162193323Sed  /// alignment (saturated to BufferEnd of course).
163193323Sed  void emitAlignment(unsigned Alignment) {
164193323Sed    if (Alignment == 0) Alignment = 1;
165193323Sed
166193323Sed    if(Alignment <= (uintptr_t)(BufferEnd-CurBufferPtr)) {
167193323Sed      // Move the current buffer ptr up to the specified alignment.
168193323Sed      CurBufferPtr =
169193574Sed        (uint8_t*)(((uintptr_t)CurBufferPtr+Alignment-1) &
170193574Sed                   ~(uintptr_t)(Alignment-1));
171193323Sed    } else {
172193323Sed      CurBufferPtr = BufferEnd;
173193323Sed    }
174193323Sed  }
175193323Sed
176193323Sed
177193323Sed  /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be
178193323Sed  /// written to the output stream.
179194178Sed  void emitULEB128Bytes(uint64_t Value) {
180193323Sed    do {
181193574Sed      uint8_t Byte = Value & 0x7f;
182193323Sed      Value >>= 7;
183193323Sed      if (Value) Byte |= 0x80;
184193323Sed      emitByte(Byte);
185193323Sed    } while (Value);
186193323Sed  }
187193323Sed
188193323Sed  /// emitSLEB128Bytes - This callback is invoked when a SLEB128 needs to be
189193323Sed  /// written to the output stream.
190194178Sed  void emitSLEB128Bytes(uint64_t Value) {
191194178Sed    uint64_t Sign = Value >> (8 * sizeof(Value) - 1);
192193323Sed    bool IsMore;
193193323Sed
194193323Sed    do {
195193574Sed      uint8_t Byte = Value & 0x7f;
196193323Sed      Value >>= 7;
197193323Sed      IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
198193323Sed      if (IsMore) Byte |= 0x80;
199193323Sed      emitByte(Byte);
200193323Sed    } while (IsMore);
201193323Sed  }
202193323Sed
203193323Sed  /// emitString - This callback is invoked when a String needs to be
204193323Sed  /// written to the output stream.
205193323Sed  void emitString(const std::string &String) {
206193323Sed    for (unsigned i = 0, N = static_cast<unsigned>(String.size());
207193323Sed         i < N; ++i) {
208193574Sed      uint8_t C = String[i];
209193323Sed      emitByte(C);
210193323Sed    }
211193323Sed    emitByte(0);
212193323Sed  }
213193323Sed
214193323Sed  /// emitInt32 - Emit a int32 directive.
215193574Sed  void emitInt32(int32_t Value) {
216193323Sed    if (4 <= BufferEnd-CurBufferPtr) {
217193323Sed      *((uint32_t*)CurBufferPtr) = Value;
218193323Sed      CurBufferPtr += 4;
219193323Sed    } else {
220193323Sed      CurBufferPtr = BufferEnd;
221193323Sed    }
222193323Sed  }
223193323Sed
224193323Sed  /// emitInt64 - Emit a int64 directive.
225193323Sed  void emitInt64(uint64_t Value) {
226193323Sed    if (8 <= BufferEnd-CurBufferPtr) {
227193323Sed      *((uint64_t*)CurBufferPtr) = Value;
228193323Sed      CurBufferPtr += 8;
229193323Sed    } else {
230193323Sed      CurBufferPtr = BufferEnd;
231193323Sed    }
232193323Sed  }
233193323Sed
234193323Sed  /// emitInt32At - Emit the Int32 Value in Addr.
235193323Sed  void emitInt32At(uintptr_t *Addr, uintptr_t Value) {
236193323Sed    if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd)
237193323Sed      (*(uint32_t*)Addr) = (uint32_t)Value;
238193323Sed  }
239193323Sed
240193323Sed  /// emitInt64At - Emit the Int64 Value in Addr.
241193323Sed  void emitInt64At(uintptr_t *Addr, uintptr_t Value) {
242193323Sed    if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd)
243193323Sed      (*(uint64_t*)Addr) = (uint64_t)Value;
244193323Sed  }
245193323Sed
246198090Srdivacky  /// processDebugLoc - Records debug location information about a
247198090Srdivacky  /// MachineInstruction.  This is called before emitting any bytes associated
248198090Srdivacky  /// with the instruction.  Even if successive instructions have the same debug
249198090Srdivacky  /// location, this method will be called for each one.
250198090Srdivacky  virtual void processDebugLoc(DebugLoc DL, bool BeforePrintintInsn) {}
251198090Srdivacky
252193323Sed  /// emitLabel - Emits a label
253205218Srdivacky  virtual void emitLabel(MCSymbol *Label) = 0;
254193323Sed
255193323Sed  /// allocateSpace - Allocate a block of space in the current output buffer,
256193323Sed  /// returning null (and setting conditions to indicate buffer overflow) on
257193323Sed  /// failure.  Alignment is the alignment in bytes of the buffer desired.
258193323Sed  virtual void *allocateSpace(uintptr_t Size, unsigned Alignment) {
259193323Sed    emitAlignment(Alignment);
260193323Sed    void *Result;
261193323Sed
262193323Sed    // Check for buffer overflow.
263193323Sed    if (Size >= (uintptr_t)(BufferEnd-CurBufferPtr)) {
264193323Sed      CurBufferPtr = BufferEnd;
265193323Sed      Result = 0;
266193323Sed    } else {
267193323Sed      // Allocate the space.
268193323Sed      Result = CurBufferPtr;
269193323Sed      CurBufferPtr += Size;
270193323Sed    }
271193323Sed
272193323Sed    return Result;
273193323Sed  }
274193323Sed
275193323Sed  /// StartMachineBasicBlock - This should be called by the target when a new
276193323Sed  /// basic block is about to be emitted.  This way the MCE knows where the
277193323Sed  /// start of the block is, and can implement getMachineBasicBlockAddress.
278193323Sed  virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) = 0;
279193323Sed
280193323Sed  /// getCurrentPCValue - This returns the address that the next emitted byte
281193323Sed  /// will be output to.
282193323Sed  ///
283193323Sed  virtual uintptr_t getCurrentPCValue() const {
284193323Sed    return (uintptr_t)CurBufferPtr;
285193323Sed  }
286193323Sed
287193323Sed  /// getCurrentPCOffset - Return the offset from the start of the emitted
288193323Sed  /// buffer that we are currently writing to.
289198090Srdivacky  virtual uintptr_t getCurrentPCOffset() const {
290193323Sed    return CurBufferPtr-BufferBegin;
291193323Sed  }
292193323Sed
293198090Srdivacky  /// earlyResolveAddresses - True if the code emitter can use symbol addresses
294198090Srdivacky  /// during code emission time. The JIT is capable of doing this because it
295198090Srdivacky  /// creates jump tables or constant pools in memory on the fly while the
296198090Srdivacky  /// object code emitters rely on a linker to have real addresses and should
297198090Srdivacky  /// use relocations instead.
298198090Srdivacky  virtual bool earlyResolveAddresses() const = 0;
299198090Srdivacky
300193323Sed  /// addRelocation - Whenever a relocatable address is needed, it should be
301193323Sed  /// noted with this interface.
302193323Sed  virtual void addRelocation(const MachineRelocation &MR) = 0;
303193323Sed
304193323Sed  /// FIXME: These should all be handled with relocations!
305193323Sed
306193323Sed  /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in
307193323Sed  /// the constant pool that was last emitted with the emitConstantPool method.
308193323Sed  ///
309193323Sed  virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const = 0;
310193323Sed
311193323Sed  /// getJumpTableEntryAddress - Return the address of the jump table with index
312193323Sed  /// 'Index' in the function that last called initJumpTableInfo.
313193323Sed  ///
314193323Sed  virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const = 0;
315193323Sed
316193323Sed  /// getMachineBasicBlockAddress - Return the address of the specified
317193323Sed  /// MachineBasicBlock, only usable after the label for the MBB has been
318193323Sed  /// emitted.
319193323Sed  ///
320193323Sed  virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const= 0;
321193323Sed
322205218Srdivacky  /// getLabelAddress - Return the address of the specified Label, only usable
323193323Sed  /// after the LabelID has been emitted.
324193323Sed  ///
325205218Srdivacky  virtual uintptr_t getLabelAddress(MCSymbol *Label) const = 0;
326193323Sed
327193323Sed  /// Specifies the MachineModuleInfo object. This is used for exception handling
328193323Sed  /// purposes.
329193323Sed  virtual void setModuleInfo(MachineModuleInfo* Info) = 0;
330193323Sed};
331193323Sed
332193323Sed} // End llvm namespace
333193323Sed
334193323Sed#endif
335