JITCodeEmitter.h revision 205218
1193323Sed//===-- llvm/CodeGen/JITCodeEmitter.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_JITCODEEMITTER_H
18193323Sed#define LLVM_CODEGEN_JITCODEEMITTER_H
19193323Sed
20193323Sed#include <string>
21198892Srdivacky#include "llvm/System/DataTypes.h"
22198090Srdivacky#include "llvm/Support/MathExtras.h"
23193323Sed#include "llvm/CodeGen/MachineCodeEmitter.h"
24193323Sed
25193323Sedusing namespace std;
26193323Sed
27193323Sednamespace llvm {
28193323Sed
29193323Sedclass MachineBasicBlock;
30193323Sedclass MachineConstantPool;
31193323Sedclass MachineJumpTableInfo;
32193323Sedclass MachineFunction;
33193323Sedclass MachineModuleInfo;
34193323Sedclass MachineRelocation;
35193323Sedclass Value;
36193323Sedclass GlobalValue;
37193323Sedclass Function;
38205218Srdivacky
39193323Sed/// JITCodeEmitter - This class defines two sorts of methods: those for
40193323Sed/// emitting the actual bytes of machine code, and those for emitting auxillary
41193323Sed/// structures, such as jump tables, relocations, etc.
42193323Sed///
43193323Sed/// Emission of machine code is complicated by the fact that we don't (in
44193323Sed/// general) know the size of the machine code that we're about to emit before
45193323Sed/// we emit it.  As such, we preallocate a certain amount of memory, and set the
46193323Sed/// BufferBegin/BufferEnd pointers to the start and end of the buffer.  As we
47193323Sed/// emit machine instructions, we advance the CurBufferPtr to indicate the
48193323Sed/// location of the next byte to emit.  In the case of a buffer overflow (we
49193323Sed/// need to emit more machine code than we have allocated space for), the
50193323Sed/// CurBufferPtr will saturate to BufferEnd and ignore stores.  Once the entire
51193323Sed/// function has been emitted, the overflow condition is checked, and if it has
52193323Sed/// occurred, more memory is allocated, and we reemit the code into it.
53193323Sed///
54193323Sedclass JITCodeEmitter : public MachineCodeEmitter {
55193323Sedpublic:
56193323Sed  virtual ~JITCodeEmitter() {}
57193323Sed
58193323Sed  /// startFunction - This callback is invoked when the specified function is
59193323Sed  /// about to be code generated.  This initializes the BufferBegin/End/Ptr
60193323Sed  /// fields.
61193323Sed  ///
62193323Sed  virtual void startFunction(MachineFunction &F) = 0;
63193323Sed
64193323Sed  /// finishFunction - This callback is invoked when the specified function has
65193323Sed  /// finished code generation.  If a buffer overflow has occurred, this method
66193323Sed  /// returns true (the callee is required to try again), otherwise it returns
67193323Sed  /// false.
68193323Sed  ///
69193323Sed  virtual bool finishFunction(MachineFunction &F) = 0;
70193323Sed
71201360Srdivacky  /// allocIndirectGV - Allocates and fills storage for an indirect
72201360Srdivacky  /// GlobalValue, and returns the address.
73201360Srdivacky  virtual void *allocIndirectGV(const GlobalValue *GV,
74201360Srdivacky                                const uint8_t *Buffer, size_t Size,
75201360Srdivacky                                unsigned Alignment) = 0;
76193323Sed
77193323Sed  /// emitByte - This callback is invoked when a byte needs to be written to the
78193323Sed  /// output stream.
79193323Sed  ///
80193574Sed  void emitByte(uint8_t B) {
81193323Sed    if (CurBufferPtr != BufferEnd)
82193323Sed      *CurBufferPtr++ = B;
83193323Sed  }
84193323Sed
85193323Sed  /// emitWordLE - This callback is invoked when a 32-bit word needs to be
86193323Sed  /// written to the output stream in little-endian format.
87193323Sed  ///
88194178Sed  void emitWordLE(uint32_t W) {
89193323Sed    if (4 <= BufferEnd-CurBufferPtr) {
90193574Sed      *CurBufferPtr++ = (uint8_t)(W >>  0);
91193574Sed      *CurBufferPtr++ = (uint8_t)(W >>  8);
92193574Sed      *CurBufferPtr++ = (uint8_t)(W >> 16);
93193574Sed      *CurBufferPtr++ = (uint8_t)(W >> 24);
94193323Sed    } else {
95193323Sed      CurBufferPtr = BufferEnd;
96193323Sed    }
97193323Sed  }
98193323Sed
99193323Sed  /// emitWordBE - This callback is invoked when a 32-bit word needs to be
100193323Sed  /// written to the output stream in big-endian format.
101193323Sed  ///
102194178Sed  void emitWordBE(uint32_t W) {
103193323Sed    if (4 <= BufferEnd-CurBufferPtr) {
104193574Sed      *CurBufferPtr++ = (uint8_t)(W >> 24);
105193574Sed      *CurBufferPtr++ = (uint8_t)(W >> 16);
106193574Sed      *CurBufferPtr++ = (uint8_t)(W >>  8);
107193574Sed      *CurBufferPtr++ = (uint8_t)(W >>  0);
108193323Sed    } else {
109193323Sed      CurBufferPtr = BufferEnd;
110193323Sed    }
111193323Sed  }
112193323Sed
113193323Sed  /// emitDWordLE - This callback is invoked when a 64-bit word needs to be
114193323Sed  /// written to the output stream in little-endian format.
115193323Sed  ///
116193323Sed  void emitDWordLE(uint64_t W) {
117193323Sed    if (8 <= BufferEnd-CurBufferPtr) {
118193574Sed      *CurBufferPtr++ = (uint8_t)(W >>  0);
119193574Sed      *CurBufferPtr++ = (uint8_t)(W >>  8);
120193574Sed      *CurBufferPtr++ = (uint8_t)(W >> 16);
121193574Sed      *CurBufferPtr++ = (uint8_t)(W >> 24);
122193574Sed      *CurBufferPtr++ = (uint8_t)(W >> 32);
123193574Sed      *CurBufferPtr++ = (uint8_t)(W >> 40);
124193574Sed      *CurBufferPtr++ = (uint8_t)(W >> 48);
125193574Sed      *CurBufferPtr++ = (uint8_t)(W >> 56);
126193323Sed    } else {
127193323Sed      CurBufferPtr = BufferEnd;
128193323Sed    }
129193323Sed  }
130193323Sed
131193323Sed  /// emitDWordBE - This callback is invoked when a 64-bit word needs to be
132193323Sed  /// written to the output stream in big-endian format.
133193323Sed  ///
134193323Sed  void emitDWordBE(uint64_t W) {
135193323Sed    if (8 <= BufferEnd-CurBufferPtr) {
136193574Sed      *CurBufferPtr++ = (uint8_t)(W >> 56);
137193574Sed      *CurBufferPtr++ = (uint8_t)(W >> 48);
138193574Sed      *CurBufferPtr++ = (uint8_t)(W >> 40);
139193574Sed      *CurBufferPtr++ = (uint8_t)(W >> 32);
140193574Sed      *CurBufferPtr++ = (uint8_t)(W >> 24);
141193574Sed      *CurBufferPtr++ = (uint8_t)(W >> 16);
142193574Sed      *CurBufferPtr++ = (uint8_t)(W >>  8);
143193574Sed      *CurBufferPtr++ = (uint8_t)(W >>  0);
144193323Sed    } else {
145193323Sed      CurBufferPtr = BufferEnd;
146193323Sed    }
147193323Sed  }
148193323Sed
149203954Srdivacky  /// emitAlignment - Move the CurBufferPtr pointer up to the specified
150193323Sed  /// alignment (saturated to BufferEnd of course).
151193323Sed  void emitAlignment(unsigned Alignment) {
152193323Sed    if (Alignment == 0) Alignment = 1;
153198090Srdivacky    uint8_t *NewPtr = (uint8_t*)RoundUpToAlignment((uintptr_t)CurBufferPtr,
154198090Srdivacky                                                   Alignment);
155198090Srdivacky    CurBufferPtr = std::min(NewPtr, BufferEnd);
156198090Srdivacky  }
157193323Sed
158198090Srdivacky  /// emitAlignmentWithFill - Similar to emitAlignment, except that the
159198090Srdivacky  /// extra bytes are filled with the provided byte.
160198090Srdivacky  void emitAlignmentWithFill(unsigned Alignment, uint8_t Fill) {
161198090Srdivacky    if (Alignment == 0) Alignment = 1;
162198090Srdivacky    uint8_t *NewPtr = (uint8_t*)RoundUpToAlignment((uintptr_t)CurBufferPtr,
163198090Srdivacky                                                   Alignment);
164198090Srdivacky    // Fail if we don't have room.
165198090Srdivacky    if (NewPtr > BufferEnd) {
166193323Sed      CurBufferPtr = BufferEnd;
167198090Srdivacky      return;
168193323Sed    }
169198090Srdivacky    while (CurBufferPtr < NewPtr) {
170198090Srdivacky      *CurBufferPtr++ = Fill;
171198090Srdivacky    }
172193323Sed  }
173193323Sed
174193323Sed  /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be
175193323Sed  /// written to the output stream.
176194178Sed  void emitULEB128Bytes(uint64_t Value) {
177193323Sed    do {
178193574Sed      uint8_t Byte = Value & 0x7f;
179193323Sed      Value >>= 7;
180193323Sed      if (Value) Byte |= 0x80;
181193323Sed      emitByte(Byte);
182193323Sed    } while (Value);
183193323Sed  }
184193323Sed
185193323Sed  /// emitSLEB128Bytes - This callback is invoked when a SLEB128 needs to be
186193323Sed  /// written to the output stream.
187194178Sed  void emitSLEB128Bytes(int64_t Value) {
188193574Sed    int32_t Sign = Value >> (8 * sizeof(Value) - 1);
189193323Sed    bool IsMore;
190193323Sed
191193323Sed    do {
192193574Sed      uint8_t Byte = Value & 0x7f;
193193323Sed      Value >>= 7;
194193323Sed      IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
195193323Sed      if (IsMore) Byte |= 0x80;
196193323Sed      emitByte(Byte);
197193323Sed    } while (IsMore);
198193323Sed  }
199193323Sed
200193323Sed  /// emitString - This callback is invoked when a String needs to be
201193323Sed  /// written to the output stream.
202193323Sed  void emitString(const std::string &String) {
203193323Sed    for (unsigned i = 0, N = static_cast<unsigned>(String.size());
204193323Sed         i < N; ++i) {
205193574Sed      uint8_t C = String[i];
206193323Sed      emitByte(C);
207193323Sed    }
208193323Sed    emitByte(0);
209193323Sed  }
210193323Sed
211193323Sed  /// emitInt32 - Emit a int32 directive.
212194178Sed  void emitInt32(uint32_t Value) {
213193323Sed    if (4 <= BufferEnd-CurBufferPtr) {
214193323Sed      *((uint32_t*)CurBufferPtr) = Value;
215193323Sed      CurBufferPtr += 4;
216193323Sed    } else {
217193323Sed      CurBufferPtr = BufferEnd;
218193323Sed    }
219193323Sed  }
220193323Sed
221193323Sed  /// emitInt64 - Emit a int64 directive.
222193323Sed  void emitInt64(uint64_t Value) {
223193323Sed    if (8 <= BufferEnd-CurBufferPtr) {
224193323Sed      *((uint64_t*)CurBufferPtr) = Value;
225193323Sed      CurBufferPtr += 8;
226193323Sed    } else {
227193323Sed      CurBufferPtr = BufferEnd;
228193323Sed    }
229193323Sed  }
230193323Sed
231193323Sed  /// emitInt32At - Emit the Int32 Value in Addr.
232193323Sed  void emitInt32At(uintptr_t *Addr, uintptr_t Value) {
233193323Sed    if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd)
234193323Sed      (*(uint32_t*)Addr) = (uint32_t)Value;
235193323Sed  }
236193323Sed
237193323Sed  /// emitInt64At - Emit the Int64 Value in Addr.
238193323Sed  void emitInt64At(uintptr_t *Addr, uintptr_t Value) {
239193323Sed    if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd)
240193323Sed      (*(uint64_t*)Addr) = (uint64_t)Value;
241193323Sed  }
242193323Sed
243193323Sed
244193323Sed  /// emitLabel - Emits a label
245205218Srdivacky  virtual void emitLabel(MCSymbol *Label) = 0;
246193323Sed
247193323Sed  /// allocateSpace - Allocate a block of space in the current output buffer,
248193323Sed  /// returning null (and setting conditions to indicate buffer overflow) on
249193323Sed  /// failure.  Alignment is the alignment in bytes of the buffer desired.
250193323Sed  virtual void *allocateSpace(uintptr_t Size, unsigned Alignment) {
251193323Sed    emitAlignment(Alignment);
252193323Sed    void *Result;
253193323Sed
254193323Sed    // Check for buffer overflow.
255193323Sed    if (Size >= (uintptr_t)(BufferEnd-CurBufferPtr)) {
256193323Sed      CurBufferPtr = BufferEnd;
257193323Sed      Result = 0;
258193323Sed    } else {
259193323Sed      // Allocate the space.
260193323Sed      Result = CurBufferPtr;
261193323Sed      CurBufferPtr += Size;
262193323Sed    }
263193323Sed
264193323Sed    return Result;
265193323Sed  }
266193323Sed
267198090Srdivacky  /// allocateGlobal - Allocate memory for a global.  Unlike allocateSpace,
268198090Srdivacky  /// this method does not allocate memory in the current output buffer,
269198090Srdivacky  /// because a global may live longer than the current function.
270198090Srdivacky  virtual void *allocateGlobal(uintptr_t Size, unsigned Alignment) = 0;
271198090Srdivacky
272193323Sed  /// StartMachineBasicBlock - This should be called by the target when a new
273193323Sed  /// basic block is about to be emitted.  This way the MCE knows where the
274193323Sed  /// start of the block is, and can implement getMachineBasicBlockAddress.
275193323Sed  virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) = 0;
276193323Sed
277193323Sed  /// getCurrentPCValue - This returns the address that the next emitted byte
278193323Sed  /// will be output to.
279193323Sed  ///
280193323Sed  virtual uintptr_t getCurrentPCValue() const {
281193323Sed    return (uintptr_t)CurBufferPtr;
282193323Sed  }
283193323Sed
284193323Sed  /// getCurrentPCOffset - Return the offset from the start of the emitted
285193323Sed  /// buffer that we are currently writing to.
286193323Sed  uintptr_t getCurrentPCOffset() const {
287193323Sed    return CurBufferPtr-BufferBegin;
288193323Sed  }
289193323Sed
290198090Srdivacky  /// earlyResolveAddresses - True if the code emitter can use symbol addresses
291198090Srdivacky  /// during code emission time. The JIT is capable of doing this because it
292198090Srdivacky  /// creates jump tables or constant pools in memory on the fly while the
293198090Srdivacky  /// object code emitters rely on a linker to have real addresses and should
294198090Srdivacky  /// use relocations instead.
295198090Srdivacky  bool earlyResolveAddresses() const { return true; }
296198090Srdivacky
297193323Sed  /// addRelocation - Whenever a relocatable address is needed, it should be
298193323Sed  /// noted with this interface.
299193323Sed  virtual void addRelocation(const MachineRelocation &MR) = 0;
300193323Sed
301193323Sed  /// FIXME: These should all be handled with relocations!
302193323Sed
303193323Sed  /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in
304193323Sed  /// the constant pool that was last emitted with the emitConstantPool method.
305193323Sed  ///
306193323Sed  virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const = 0;
307193323Sed
308193323Sed  /// getJumpTableEntryAddress - Return the address of the jump table with index
309193323Sed  /// 'Index' in the function that last called initJumpTableInfo.
310193323Sed  ///
311193323Sed  virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const = 0;
312193323Sed
313193323Sed  /// getMachineBasicBlockAddress - Return the address of the specified
314193323Sed  /// MachineBasicBlock, only usable after the label for the MBB has been
315193323Sed  /// emitted.
316193323Sed  ///
317193323Sed  virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const= 0;
318193323Sed
319205218Srdivacky  /// getLabelAddress - Return the address of the specified Label, only usable
320205218Srdivacky  /// after the Label has been emitted.
321193323Sed  ///
322205218Srdivacky  virtual uintptr_t getLabelAddress(MCSymbol *Label) const = 0;
323193323Sed
324193323Sed  /// Specifies the MachineModuleInfo object. This is used for exception handling
325193323Sed  /// purposes.
326193323Sed  virtual void setModuleInfo(MachineModuleInfo* Info) = 0;
327193323Sed};
328193323Sed
329193323Sed} // End llvm namespace
330193323Sed
331193323Sed#endif
332