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/Support/DataTypes.h"
21 #include "llvm/Support/DebugLoc.h"
27 class MachineBasicBlock;
28 class MachineConstantPool;
29 class MachineJumpTableInfo;
30 class MachineFunction;
31 class MachineModuleInfo;
32 class MachineRelocation;
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.
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.
53 class MachineCodeEmitter {
54 virtual void anchor();
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
64 uint8_t *CurBufferPtr;
67 virtual ~MachineCodeEmitter() {}
69 /// startFunction - This callback is invoked when the specified function is
70 /// about to be code generated. This initializes the BufferBegin/End/Ptr
73 virtual void startFunction(MachineFunction &F) = 0;
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
80 virtual bool finishFunction(MachineFunction &F) = 0;
82 /// emitByte - This callback is invoked when a byte needs to be written to the
85 void emitByte(uint8_t B) {
86 if (CurBufferPtr != BufferEnd)
90 /// emitWordLE - This callback is invoked when a 32-bit word needs to be
91 /// written to the output stream in little-endian format.
93 void emitWordLE(uint32_t W) {
94 if (4 <= BufferEnd-CurBufferPtr) {
95 emitWordLEInto(CurBufferPtr, W);
97 CurBufferPtr = BufferEnd;
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.
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);
112 /// emitWordBE - This callback is invoked when a 32-bit word needs to be
113 /// written to the output stream in big-endian format.
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);
122 CurBufferPtr = BufferEnd;
126 /// emitDWordLE - This callback is invoked when a 64-bit word needs to be
127 /// written to the output stream in little-endian format.
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);
140 CurBufferPtr = BufferEnd;
144 /// emitDWordBE - This callback is invoked when a 64-bit word needs to be
145 /// written to the output stream in big-endian format.
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);
158 CurBufferPtr = BufferEnd;
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;
167 if(Alignment <= (uintptr_t)(BufferEnd-CurBufferPtr)) {
168 // Move the current buffer ptr up to the specified alignment.
170 (uint8_t*)(((uintptr_t)CurBufferPtr+Alignment-1) &
171 ~(uintptr_t)(Alignment-1));
173 CurBufferPtr = BufferEnd;
178 /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be
179 /// written to the output stream.
180 void emitULEB128Bytes(uint64_t Value) {
182 uint8_t Byte = Value & 0x7f;
184 if (Value) Byte |= 0x80;
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);
196 uint8_t Byte = Value & 0x7f;
198 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
199 if (IsMore) Byte |= 0x80;
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());
209 uint8_t C = String[i];
215 /// emitInt32 - Emit a int32 directive.
216 void emitInt32(int32_t Value) {
217 if (4 <= BufferEnd-CurBufferPtr) {
218 *((uint32_t*)CurBufferPtr) = Value;
221 CurBufferPtr = BufferEnd;
225 /// emitInt64 - Emit a int64 directive.
226 void emitInt64(uint64_t Value) {
227 if (8 <= BufferEnd-CurBufferPtr) {
228 *((uint64_t*)CurBufferPtr) = Value;
231 CurBufferPtr = BufferEnd;
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;
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;
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) {}
253 /// emitLabel - Emits a label
254 virtual void emitLabel(MCSymbol *Label) = 0;
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);
263 // Check for buffer overflow.
264 if (Size >= (uintptr_t)(BufferEnd-CurBufferPtr)) {
265 CurBufferPtr = BufferEnd;
268 // Allocate the space.
269 Result = CurBufferPtr;
270 CurBufferPtr += Size;
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;
281 /// getCurrentPCValue - This returns the address that the next emitted byte
282 /// will be output to.
284 virtual uintptr_t getCurrentPCValue() const {
285 return (uintptr_t)CurBufferPtr;
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;
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;
301 /// addRelocation - Whenever a relocatable address is needed, it should be
302 /// noted with this interface.
303 virtual void addRelocation(const MachineRelocation &MR) = 0;
305 /// FIXME: These should all be handled with relocations!
307 /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in
308 /// the constant pool that was last emitted with the emitConstantPool method.
310 virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const = 0;
312 /// getJumpTableEntryAddress - Return the address of the jump table with index
313 /// 'Index' in the function that last called initJumpTableInfo.
315 virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const = 0;
317 /// getMachineBasicBlockAddress - Return the address of the specified
318 /// MachineBasicBlock, only usable after the label for the MBB has been
321 virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const= 0;
323 /// getLabelAddress - Return the address of the specified Label, only usable
324 /// after the LabelID has been emitted.
326 virtual uintptr_t getLabelAddress(MCSymbol *Label) const = 0;
328 /// Specifies the MachineModuleInfo object. This is used for exception handling
330 virtual void setModuleInfo(MachineModuleInfo* Info) = 0;
333 } // End llvm namespace