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