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"
25 class MachineBasicBlock;
26 class MachineConstantPool;
27 class MachineJumpTableInfo;
28 class MachineFunction;
29 class MachineRelocation;
34 /// MachineCodeEmitter - This class defines two sorts of methods: those for
35 /// emitting the actual bytes of machine code, and those for emitting auxillary
36 /// structures, such as jump tables, relocations, etc.
38 /// Emission of machine code is complicated by the fact that we don't (in
39 /// general) know the size of the machine code that we're about to emit before
40 /// we emit it. As such, we preallocate a certain amount of memory, and set the
41 /// BufferBegin/BufferEnd pointers to the start and end of the buffer. As we
42 /// emit machine instructions, we advance the CurBufferPtr to indicate the
43 /// location of the next byte to emit. In the case of a buffer overflow (we
44 /// need to emit more machine code than we have allocated space for), the
45 /// CurBufferPtr will saturate to BufferEnd and ignore stores. Once the entire
46 /// function has been emitted, the overflow condition is checked, and if it has
47 /// occurred, more memory is allocated, and we reemit the code into it.
49 class MachineCodeEmitter {
51 /// BufferBegin/BufferEnd - Pointers to the start and end of the memory
52 /// allocated for this code buffer.
53 unsigned char *BufferBegin, *BufferEnd;
55 /// CurBufferPtr - Pointer to the next byte of memory to fill when emitting
56 /// code. This is guranteed to be in the range [BufferBegin,BufferEnd]. If
57 /// this pointer is at BufferEnd, it will never move due to code emission, and
58 /// all code emission requests will be ignored (this is the buffer overflow
60 unsigned char *CurBufferPtr;
63 virtual ~MachineCodeEmitter() {}
65 /// startFunction - This callback is invoked when the specified function is
66 /// about to be code generated. This initializes the BufferBegin/End/Ptr
69 virtual void startFunction(MachineFunction &F) = 0;
71 /// finishFunction - This callback is invoked when the specified function has
72 /// finished code generation. If a buffer overflow has occurred, this method
73 /// returns true (the callee is required to try again), otherwise it returns
76 virtual bool finishFunction(MachineFunction &F) = 0;
78 /// startFunctionStub - This callback is invoked when the JIT needs the
79 /// address of a function that has not been code generated yet. The StubSize
80 /// specifies the total size required by the stub. Stubs are not allowed to
81 /// have constant pools, the can only use the other emitByte*/emitWord*
84 virtual void startFunctionStub(unsigned StubSize, unsigned Alignment = 1) = 0;
86 /// finishFunctionStub - This callback is invoked to terminate a function
89 virtual void *finishFunctionStub(const Function *F) = 0;
91 /// emitByte - This callback is invoked when a byte needs to be written to the
94 void emitByte(unsigned char B) {
95 if (CurBufferPtr != BufferEnd)
99 /// emitWordLE - This callback is invoked when a 32-bit word needs to be
100 /// written to the output stream in little-endian format.
102 void emitWordLE(unsigned W) {
103 if (CurBufferPtr+4 <= BufferEnd) {
104 *CurBufferPtr++ = (unsigned char)(W >> 0);
105 *CurBufferPtr++ = (unsigned char)(W >> 8);
106 *CurBufferPtr++ = (unsigned char)(W >> 16);
107 *CurBufferPtr++ = (unsigned char)(W >> 24);
109 CurBufferPtr = BufferEnd;
113 /// emitWordBE - This callback is invoked when a 32-bit word needs to be
114 /// written to the output stream in big-endian format.
116 void emitWordBE(unsigned W) {
117 if (CurBufferPtr+4 <= BufferEnd) {
118 *CurBufferPtr++ = (unsigned char)(W >> 24);
119 *CurBufferPtr++ = (unsigned char)(W >> 16);
120 *CurBufferPtr++ = (unsigned char)(W >> 8);
121 *CurBufferPtr++ = (unsigned char)(W >> 0);
123 CurBufferPtr = BufferEnd;
127 /// emitAlignment - Move the CurBufferPtr pointer up the the specified
128 /// alignment (saturated to BufferEnd of course).
129 void emitAlignment(unsigned Alignment) {
130 if (Alignment == 0) Alignment = 1;
131 // Move the current buffer ptr up to the specified alignment.
133 (unsigned char*)(((intptr_t)CurBufferPtr+Alignment-1) &
134 ~(intptr_t)(Alignment-1));
135 if (CurBufferPtr > BufferEnd)
136 CurBufferPtr = BufferEnd;
139 /// allocateSpace - Allocate a block of space in the current output buffer,
140 /// returning null (and setting conditions to indicate buffer overflow) on
141 /// failure. Alignment is the alignment in bytes of the buffer desired.
142 void *allocateSpace(intptr_t Size, unsigned Alignment) {
143 emitAlignment(Alignment);
144 void *Result = CurBufferPtr;
146 // Allocate the space.
147 CurBufferPtr += Size;
149 // Check for buffer overflow.
150 if (CurBufferPtr >= BufferEnd) {
151 CurBufferPtr = BufferEnd;
157 /// StartMachineBasicBlock - This should be called by the target when a new
158 /// basic block is about to be emitted. This way the MCE knows where the
159 /// start of the block is, and can implement getMachineBasicBlockAddress.
160 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) = 0;
162 /// getCurrentPCValue - This returns the address that the next emitted byte
163 /// will be output to.
165 virtual intptr_t getCurrentPCValue() const {
166 return (intptr_t)CurBufferPtr;
169 /// getCurrentPCOffset - Return the offset from the start of the emitted
170 /// buffer that we are currently writing to.
171 intptr_t getCurrentPCOffset() const {
172 return CurBufferPtr-BufferBegin;
175 /// addRelocation - Whenever a relocatable address is needed, it should be
176 /// noted with this interface.
177 virtual void addRelocation(const MachineRelocation &MR) = 0;
180 /// FIXME: These should all be handled with relocations!
182 /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in
183 /// the constant pool that was last emitted with the emitConstantPool method.
185 virtual intptr_t getConstantPoolEntryAddress(unsigned Index) const = 0;
187 /// getJumpTableEntryAddress - Return the address of the jump table with index
188 /// 'Index' in the function that last called initJumpTableInfo.
190 virtual intptr_t getJumpTableEntryAddress(unsigned Index) const = 0;
192 /// getMachineBasicBlockAddress - Return the address of the specified
193 /// MachineBasicBlock, only usable after the label for the MBB has been
196 virtual intptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const= 0;
199 } // End llvm namespace