1 //===-- llvm/CodeGen/MachineCodeEmitter.h - Code emission -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.).
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_CODEGEN_MACHINECODEEMITTER_H
18 #define LLVM_CODEGEN_MACHINECODEEMITTER_H
20 #include "llvm/System/DataTypes.h"
21 #include "llvm/Support/DebugLoc.h"
25 class MachineBasicBlock;
26 class MachineConstantPool;
27 class MachineJumpTableInfo;
28 class MachineFunction;
29 class MachineModuleInfo;
30 class MachineRelocation;
36 /// MachineCodeEmitter - This class defines two sorts of methods: those for
37 /// emitting the actual bytes of machine code, and those for emitting auxillary
38 /// structures, such as jump tables, relocations, etc.
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.
51 class MachineCodeEmitter {
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 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
61 uint8_t *CurBufferPtr;
64 virtual ~MachineCodeEmitter() {}
66 /// startFunction - This callback is invoked when the specified function is
67 /// about to be code generated. This initializes the BufferBegin/End/Ptr
70 virtual void startFunction(MachineFunction &F) = 0;
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
77 virtual bool finishFunction(MachineFunction &F) = 0;
79 /// emitByte - This callback is invoked when a byte needs to be written to the
82 void emitByte(uint8_t B) {
83 if (CurBufferPtr != BufferEnd)
87 /// emitWordLE - This callback is invoked when a 32-bit word needs to be
88 /// written to the output stream in little-endian format.
90 void emitWordLE(uint32_t W) {
91 if (4 <= BufferEnd-CurBufferPtr) {
92 emitWordLEInto(CurBufferPtr, W);
94 CurBufferPtr = BufferEnd;
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.
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);
109 /// emitWordBE - This callback is invoked when a 32-bit word needs to be
110 /// written to the output stream in big-endian format.
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);
119 CurBufferPtr = BufferEnd;
123 /// emitDWordLE - This callback is invoked when a 64-bit word needs to be
124 /// written to the output stream in little-endian format.
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);
137 CurBufferPtr = BufferEnd;
141 /// emitDWordBE - This callback is invoked when a 64-bit word needs to be
142 /// written to the output stream in big-endian format.
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);
155 CurBufferPtr = BufferEnd;
159 /// emitAlignment - Move the CurBufferPtr pointer up to the specified
160 /// alignment (saturated to BufferEnd of course).
161 void emitAlignment(unsigned Alignment) {
162 if (Alignment == 0) Alignment = 1;
164 if(Alignment <= (uintptr_t)(BufferEnd-CurBufferPtr)) {
165 // Move the current buffer ptr up to the specified alignment.
167 (uint8_t*)(((uintptr_t)CurBufferPtr+Alignment-1) &
168 ~(uintptr_t)(Alignment-1));
170 CurBufferPtr = BufferEnd;
175 /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be
176 /// written to the output stream.
177 void emitULEB128Bytes(uint64_t Value) {
179 uint8_t Byte = Value & 0x7f;
181 if (Value) Byte |= 0x80;
186 /// emitSLEB128Bytes - This callback is invoked when a SLEB128 needs to be
187 /// written to the output stream.
188 void emitSLEB128Bytes(uint64_t Value) {
189 uint64_t Sign = Value >> (8 * sizeof(Value) - 1);
193 uint8_t Byte = Value & 0x7f;
195 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
196 if (IsMore) Byte |= 0x80;
201 /// emitString - This callback is invoked when a String needs to be
202 /// written to the output stream.
203 void emitString(const std::string &String) {
204 for (unsigned i = 0, N = static_cast<unsigned>(String.size());
206 uint8_t C = String[i];
212 /// emitInt32 - Emit a int32 directive.
213 void emitInt32(int32_t Value) {
214 if (4 <= BufferEnd-CurBufferPtr) {
215 *((uint32_t*)CurBufferPtr) = Value;
218 CurBufferPtr = BufferEnd;
222 /// emitInt64 - Emit a int64 directive.
223 void emitInt64(uint64_t Value) {
224 if (8 <= BufferEnd-CurBufferPtr) {
225 *((uint64_t*)CurBufferPtr) = Value;
228 CurBufferPtr = BufferEnd;
232 /// emitInt32At - Emit the Int32 Value in Addr.
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;
238 /// emitInt64At - Emit the Int64 Value in Addr.
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;
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.
248 virtual void processDebugLoc(DebugLoc DL, bool BeforePrintintInsn) {}
250 /// emitLabel - Emits a label
251 virtual void emitLabel(MCSymbol *Label) = 0;
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.
256 virtual void *allocateSpace(uintptr_t Size, unsigned Alignment) {
257 emitAlignment(Alignment);
260 // Check for buffer overflow.
261 if (Size >= (uintptr_t)(BufferEnd-CurBufferPtr)) {
262 CurBufferPtr = BufferEnd;
265 // Allocate the space.
266 Result = CurBufferPtr;
267 CurBufferPtr += Size;
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;
278 /// getCurrentPCValue - This returns the address that the next emitted byte
279 /// will be output to.
281 virtual uintptr_t getCurrentPCValue() const {
282 return (uintptr_t)CurBufferPtr;
285 /// getCurrentPCOffset - Return the offset from the start of the emitted
286 /// buffer that we are currently writing to.
287 virtual uintptr_t getCurrentPCOffset() const {
288 return CurBufferPtr-BufferBegin;
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;
298 /// addRelocation - Whenever a relocatable address is needed, it should be
299 /// noted with this interface.
300 virtual void addRelocation(const MachineRelocation &MR) = 0;
302 /// FIXME: These should all be handled with relocations!
304 /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in
305 /// the constant pool that was last emitted with the emitConstantPool method.
307 virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const = 0;
309 /// getJumpTableEntryAddress - Return the address of the jump table with index
310 /// 'Index' in the function that last called initJumpTableInfo.
312 virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const = 0;
314 /// getMachineBasicBlockAddress - Return the address of the specified
315 /// MachineBasicBlock, only usable after the label for the MBB has been
318 virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const= 0;
320 /// getLabelAddress - Return the address of the specified Label, only usable
321 /// after the LabelID has been emitted.
323 virtual uintptr_t getLabelAddress(MCSymbol *Label) const = 0;
325 /// Specifies the MachineModuleInfo object. This is used for exception handling
327 virtual void setModuleInfo(MachineModuleInfo* Info) = 0;
330 } // End llvm namespace