1 //===-- llvm/CodeGen/MachineCodeEmitter.h - Code emission -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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;
62 virtual ~MachineCodeEmitter() {}
64 /// startFunction - This callback is invoked when the specified function is
65 /// about to be code generated. This initializes the BufferBegin/End/Ptr
68 virtual void startFunction(MachineFunction &F) {}
70 /// finishFunction - This callback is invoked when the specified function has
71 /// finished code generation. If a buffer overflow has occurred, this method
72 /// returns true (the callee is required to try again), otherwise it returns
75 virtual bool finishFunction(MachineFunction &F) {
76 return CurBufferPtr == BufferEnd;
79 /// emitConstantPool - This callback is invoked to output the constant pool
81 virtual void emitConstantPool(MachineConstantPool *MCP) {}
83 /// initJumpTableInfo - This callback is invoked by the JIT to allocate the
84 /// necessary memory to hold the jump tables.
85 virtual void initJumpTableInfo(MachineJumpTableInfo *MJTI) {}
87 /// emitJumpTableInfo - This callback is invoked to output the jump tables
88 /// for the function. In addition to a pointer to the MachineJumpTableInfo,
89 /// this function also takes a map of MBBs to addresses, so that the final
90 /// addresses of the MBBs can be written to the jump tables.
91 virtual void emitJumpTableInfo(MachineJumpTableInfo *MJTI,
92 std::map<MachineBasicBlock*,uint64_t> &MBBM) {}
94 /// startFunctionStub - This callback is invoked when the JIT needs the
95 /// address of a function that has not been code generated yet. The StubSize
96 /// specifies the total size required by the stub. Stubs are not allowed to
97 /// have constant pools, the can only use the other emitByte*/emitWord*
100 virtual void startFunctionStub(unsigned StubSize) {}
102 /// finishFunctionStub - This callback is invoked to terminate a function
105 virtual void *finishFunctionStub(const Function *F) { return 0; }
107 /// emitByte - This callback is invoked when a byte needs to be written to the
110 void emitByte(unsigned char B) {
111 if (CurBufferPtr != BufferEnd)
115 /// emitWordLE - This callback is invoked when a 32-bit word needs to be
116 /// written to the output stream in little-endian format.
118 void emitWordLE(unsigned W) {
119 if (CurBufferPtr+4 <= BufferEnd) {
120 *CurBufferPtr++ = (unsigned char)(W >> 0);
121 *CurBufferPtr++ = (unsigned char)(W >> 8);
122 *CurBufferPtr++ = (unsigned char)(W >> 16);
123 *CurBufferPtr++ = (unsigned char)(W >> 24);
125 CurBufferPtr = BufferEnd;
129 /// emitWordBE - This callback is invoked when a 32-bit word needs to be
130 /// written to the output stream in big-endian format.
132 void emitWordBE(unsigned W) {
133 if (CurBufferPtr+4 <= BufferEnd) {
134 *CurBufferPtr++ = (unsigned char)(W >> 24);
135 *CurBufferPtr++ = (unsigned char)(W >> 16);
136 *CurBufferPtr++ = (unsigned char)(W >> 8);
137 *CurBufferPtr++ = (unsigned char)(W >> 0);
139 CurBufferPtr = BufferEnd;
143 /// allocateSpace - Allocate a block of space in the current output buffer,
144 /// returning null (and setting conditions to indicate buffer overflow) on
145 /// failure. Alignment is the alignment in bytes of the buffer desired.
146 void *allocateSpace(intptr_t Size, unsigned Alignment) {
147 if (Alignment == 0) Alignment = 1;
148 // Move the current buffer ptr up to the specified alignment.
150 (unsigned char*)(((intptr_t)CurBufferPtr+Alignment-1) & ~(Alignment-1));
151 void *Result = CurBufferPtr;
153 // Allocate the space.
154 CurBufferPtr += Size;
156 // Check for buffer overflow.
157 if (CurBufferPtr >= BufferEnd) {
158 CurBufferPtr = BufferEnd;
165 /// getCurrentPCValue - This returns the address that the next emitted byte
166 /// will be output to.
168 virtual intptr_t getCurrentPCValue() const {
169 return (intptr_t)CurBufferPtr;
172 /// getCurrentPCOffset - Return the offset from the start of the emitted
173 /// buffer that we are currently writing to.
174 intptr_t getCurrentPCOffset() const {
175 return CurBufferPtr-BufferBegin;
178 /// addRelocation - Whenever a relocatable address is needed, it should be
179 /// noted with this interface.
180 virtual void addRelocation(const MachineRelocation &MR) = 0;
182 /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in
183 /// the constant pool that was last emitted with the emitConstantPool method.
185 virtual uint64_t getConstantPoolEntryAddress(unsigned Index) = 0;
187 /// getJumpTableEntryAddress - Return the address of the jump table with index
188 /// 'Index' in the function that last called initJumpTableInfo.
190 virtual uint64_t getJumpTableEntryAddress(unsigned Index) = 0;
193 } // End llvm namespace