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/ErrorHandling.h"
22 #include "llvm/Support/raw_ostream.h"
26 class MachineBasicBlock;
27 class MachineConstantPool;
28 class MachineJumpTableInfo;
29 class MachineFunction;
30 class MachineModuleInfo;
31 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;
57 /// CurBufferPtr - Pointer to the next byte of memory to fill when emitting
58 /// code. This is guranteed to be in the range [BufferBegin,BufferEnd]. If
59 /// this pointer is at BufferEnd, it will never move due to code emission, and
60 /// all code emission requests will be ignored (this is the buffer overflow
62 uint8_t *CurBufferPtr;
65 virtual ~MachineCodeEmitter() {}
67 /// startFunction - This callback is invoked when the specified function is
68 /// about to be code generated. This initializes the BufferBegin/End/Ptr
71 virtual void startFunction(MachineFunction &F) = 0;
73 /// finishFunction - This callback is invoked when the specified function has
74 /// finished code generation. If a buffer overflow has occurred, this method
75 /// returns true (the callee is required to try again), otherwise it returns
78 virtual bool finishFunction(MachineFunction &F) = 0;
80 /// emitByte - This callback is invoked when a byte needs to be written to the
83 void emitByte(uint8_t B) {
84 if (CurBufferPtr != BufferEnd)
88 /// emitWordLE - This callback is invoked when a 32-bit word needs to be
89 /// written to the output stream in little-endian format.
91 void emitWordLE(uint32_t W) {
92 if (4 <= BufferEnd-CurBufferPtr) {
93 *CurBufferPtr++ = (uint8_t)(W >> 0);
94 *CurBufferPtr++ = (uint8_t)(W >> 8);
95 *CurBufferPtr++ = (uint8_t)(W >> 16);
96 *CurBufferPtr++ = (uint8_t)(W >> 24);
98 CurBufferPtr = BufferEnd;
102 /// emitWordBE - This callback is invoked when a 32-bit word needs to be
103 /// written to the output stream in big-endian format.
105 void emitWordBE(uint32_t W) {
106 if (4 <= BufferEnd-CurBufferPtr) {
107 *CurBufferPtr++ = (uint8_t)(W >> 24);
108 *CurBufferPtr++ = (uint8_t)(W >> 16);
109 *CurBufferPtr++ = (uint8_t)(W >> 8);
110 *CurBufferPtr++ = (uint8_t)(W >> 0);
112 CurBufferPtr = BufferEnd;
116 /// emitDWordLE - This callback is invoked when a 64-bit word needs to be
117 /// written to the output stream in little-endian format.
119 void emitDWordLE(uint64_t W) {
120 if (8 <= BufferEnd-CurBufferPtr) {
121 *CurBufferPtr++ = (uint8_t)(W >> 0);
122 *CurBufferPtr++ = (uint8_t)(W >> 8);
123 *CurBufferPtr++ = (uint8_t)(W >> 16);
124 *CurBufferPtr++ = (uint8_t)(W >> 24);
125 *CurBufferPtr++ = (uint8_t)(W >> 32);
126 *CurBufferPtr++ = (uint8_t)(W >> 40);
127 *CurBufferPtr++ = (uint8_t)(W >> 48);
128 *CurBufferPtr++ = (uint8_t)(W >> 56);
130 CurBufferPtr = BufferEnd;
134 /// emitDWordBE - This callback is invoked when a 64-bit word needs to be
135 /// written to the output stream in big-endian format.
137 void emitDWordBE(uint64_t W) {
138 if (8 <= BufferEnd-CurBufferPtr) {
139 *CurBufferPtr++ = (uint8_t)(W >> 56);
140 *CurBufferPtr++ = (uint8_t)(W >> 48);
141 *CurBufferPtr++ = (uint8_t)(W >> 40);
142 *CurBufferPtr++ = (uint8_t)(W >> 32);
143 *CurBufferPtr++ = (uint8_t)(W >> 24);
144 *CurBufferPtr++ = (uint8_t)(W >> 16);
145 *CurBufferPtr++ = (uint8_t)(W >> 8);
146 *CurBufferPtr++ = (uint8_t)(W >> 0);
148 CurBufferPtr = BufferEnd;
152 /// emitAlignment - Move the CurBufferPtr pointer up the the specified
153 /// alignment (saturated to BufferEnd of course).
154 void emitAlignment(unsigned Alignment) {
155 if (Alignment == 0) Alignment = 1;
157 if(Alignment <= (uintptr_t)(BufferEnd-CurBufferPtr)) {
158 // Move the current buffer ptr up to the specified alignment.
160 (uint8_t*)(((uintptr_t)CurBufferPtr+Alignment-1) &
161 ~(uintptr_t)(Alignment-1));
163 CurBufferPtr = BufferEnd;
168 /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be
169 /// written to the output stream.
170 void emitULEB128Bytes(uint64_t Value) {
172 uint8_t Byte = Value & 0x7f;
174 if (Value) Byte |= 0x80;
179 /// emitSLEB128Bytes - This callback is invoked when a SLEB128 needs to be
180 /// written to the output stream.
181 void emitSLEB128Bytes(uint64_t Value) {
182 uint64_t Sign = Value >> (8 * sizeof(Value) - 1);
186 uint8_t Byte = Value & 0x7f;
188 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
189 if (IsMore) Byte |= 0x80;
194 /// emitString - This callback is invoked when a String needs to be
195 /// written to the output stream.
196 void emitString(const std::string &String) {
197 for (unsigned i = 0, N = static_cast<unsigned>(String.size());
199 uint8_t C = String[i];
205 /// emitInt32 - Emit a int32 directive.
206 void emitInt32(int32_t Value) {
207 if (4 <= BufferEnd-CurBufferPtr) {
208 *((uint32_t*)CurBufferPtr) = Value;
211 CurBufferPtr = BufferEnd;
215 /// emitInt64 - Emit a int64 directive.
216 void emitInt64(uint64_t Value) {
217 if (8 <= BufferEnd-CurBufferPtr) {
218 *((uint64_t*)CurBufferPtr) = Value;
221 CurBufferPtr = BufferEnd;
225 /// emitInt32At - Emit the Int32 Value in Addr.
226 void emitInt32At(uintptr_t *Addr, uintptr_t Value) {
227 if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd)
228 (*(uint32_t*)Addr) = (uint32_t)Value;
231 /// emitInt64At - Emit the Int64 Value in Addr.
232 void emitInt64At(uintptr_t *Addr, uintptr_t Value) {
233 if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd)
234 (*(uint64_t*)Addr) = (uint64_t)Value;
238 /// emitLabel - Emits a label
239 virtual void emitLabel(uint64_t LabelID) = 0;
241 /// allocateSpace - Allocate a block of space in the current output buffer,
242 /// returning null (and setting conditions to indicate buffer overflow) on
243 /// failure. Alignment is the alignment in bytes of the buffer desired.
244 virtual void *allocateSpace(uintptr_t Size, unsigned Alignment) {
245 emitAlignment(Alignment);
248 // Check for buffer overflow.
249 if (Size >= (uintptr_t)(BufferEnd-CurBufferPtr)) {
250 CurBufferPtr = BufferEnd;
253 // Allocate the space.
254 Result = CurBufferPtr;
255 CurBufferPtr += Size;
261 /// StartMachineBasicBlock - This should be called by the target when a new
262 /// basic block is about to be emitted. This way the MCE knows where the
263 /// start of the block is, and can implement getMachineBasicBlockAddress.
264 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) = 0;
266 /// getCurrentPCValue - This returns the address that the next emitted byte
267 /// will be output to.
269 virtual uintptr_t getCurrentPCValue() const {
270 return (uintptr_t)CurBufferPtr;
273 /// getCurrentPCOffset - Return the offset from the start of the emitted
274 /// buffer that we are currently writing to.
275 virtual uintptr_t getCurrentPCOffset() const {
276 return CurBufferPtr-BufferBegin;
279 /// addRelocation - Whenever a relocatable address is needed, it should be
280 /// noted with this interface.
281 virtual void addRelocation(const MachineRelocation &MR) = 0;
283 /// FIXME: These should all be handled with relocations!
285 /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in
286 /// the constant pool that was last emitted with the emitConstantPool method.
288 virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const = 0;
290 /// getJumpTableEntryAddress - Return the address of the jump table with index
291 /// 'Index' in the function that last called initJumpTableInfo.
293 virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const = 0;
295 /// getMachineBasicBlockAddress - Return the address of the specified
296 /// MachineBasicBlock, only usable after the label for the MBB has been
299 virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const= 0;
301 /// getLabelAddress - Return the address of the specified LabelID, only usable
302 /// after the LabelID has been emitted.
304 virtual uintptr_t getLabelAddress(uint64_t LabelID) const = 0;
306 /// Specifies the MachineModuleInfo object. This is used for exception handling
308 virtual void setModuleInfo(MachineModuleInfo* Info) = 0;
311 } // End llvm namespace