JITCodeEmitter.h revision 688ed8583eaa1544e8e53b039b2b8284d2e9268a
1//===-- llvm/CodeGen/JITCodeEmitter.h - Code emission ----------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 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.). 14// 15//===----------------------------------------------------------------------===// 16 17#ifndef LLVM_CODEGEN_JITCODEEMITTER_H 18#define LLVM_CODEGEN_JITCODEEMITTER_H 19 20#include <string> 21#include "llvm/Support/DataTypes.h" 22#include "llvm/CodeGen/MachineCodeEmitter.h" 23 24using namespace std; 25 26namespace llvm { 27 28class MachineBasicBlock; 29class MachineConstantPool; 30class MachineJumpTableInfo; 31class MachineFunction; 32class MachineModuleInfo; 33class MachineRelocation; 34class Value; 35class GlobalValue; 36class Function; 37 38/// JITCodeEmitter - This class defines two sorts of methods: those for 39/// emitting the actual bytes of machine code, and those for emitting auxillary 40/// structures, such as jump tables, relocations, etc. 41/// 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. 52/// 53class JITCodeEmitter : public MachineCodeEmitter { 54public: 55 virtual ~JITCodeEmitter() {} 56 57 /// startFunction - This callback is invoked when the specified function is 58 /// about to be code generated. This initializes the BufferBegin/End/Ptr 59 /// fields. 60 /// 61 virtual void startFunction(MachineFunction &F) = 0; 62 63 /// finishFunction - This callback is invoked when the specified function has 64 /// finished code generation. If a buffer overflow has occurred, this method 65 /// returns true (the callee is required to try again), otherwise it returns 66 /// false. 67 /// 68 virtual bool finishFunction(MachineFunction &F) = 0; 69 70 /// startGVStub - This callback is invoked when the JIT needs the 71 /// address of a GV (e.g. function) that has not been code generated yet. 72 /// The StubSize specifies the total size required by the stub. 73 /// 74 virtual void startGVStub(const GlobalValue* GV, unsigned StubSize, 75 unsigned Alignment = 1) = 0; 76 77 /// startGVStub - This callback is invoked when the JIT needs the address of a 78 /// GV (e.g. function) that has not been code generated yet. Buffer points to 79 /// memory already allocated for this stub. 80 /// 81 virtual void startGVStub(const GlobalValue* GV, void *Buffer, 82 unsigned StubSize) = 0; 83 84 /// finishGVStub - This callback is invoked to terminate a GV stub. 85 /// 86 virtual void *finishGVStub(const GlobalValue* F) = 0; 87 88 /// emitByte - This callback is invoked when a byte needs to be written to the 89 /// output stream. 90 /// 91 void emitByte(uint8_t B) { 92 if (CurBufferPtr != BufferEnd) 93 *CurBufferPtr++ = B; 94 } 95 96 /// emitWordLE - This callback is invoked when a 32-bit word needs to be 97 /// written to the output stream in little-endian format. 98 /// 99 void emitWordLE(uint32_t W) { 100 if (4 <= BufferEnd-CurBufferPtr) { 101 *CurBufferPtr++ = (uint8_t)(W >> 0); 102 *CurBufferPtr++ = (uint8_t)(W >> 8); 103 *CurBufferPtr++ = (uint8_t)(W >> 16); 104 *CurBufferPtr++ = (uint8_t)(W >> 24); 105 } else { 106 CurBufferPtr = BufferEnd; 107 } 108 } 109 110 /// emitWordBE - This callback is invoked when a 32-bit word needs to be 111 /// written to the output stream in big-endian format. 112 /// 113 void emitWordBE(uint32_t W) { 114 if (4 <= BufferEnd-CurBufferPtr) { 115 *CurBufferPtr++ = (uint8_t)(W >> 24); 116 *CurBufferPtr++ = (uint8_t)(W >> 16); 117 *CurBufferPtr++ = (uint8_t)(W >> 8); 118 *CurBufferPtr++ = (uint8_t)(W >> 0); 119 } else { 120 CurBufferPtr = BufferEnd; 121 } 122 } 123 124 /// emitDWordLE - This callback is invoked when a 64-bit word needs to be 125 /// written to the output stream in little-endian format. 126 /// 127 void emitDWordLE(uint64_t W) { 128 if (8 <= BufferEnd-CurBufferPtr) { 129 *CurBufferPtr++ = (uint8_t)(W >> 0); 130 *CurBufferPtr++ = (uint8_t)(W >> 8); 131 *CurBufferPtr++ = (uint8_t)(W >> 16); 132 *CurBufferPtr++ = (uint8_t)(W >> 24); 133 *CurBufferPtr++ = (uint8_t)(W >> 32); 134 *CurBufferPtr++ = (uint8_t)(W >> 40); 135 *CurBufferPtr++ = (uint8_t)(W >> 48); 136 *CurBufferPtr++ = (uint8_t)(W >> 56); 137 } else { 138 CurBufferPtr = BufferEnd; 139 } 140 } 141 142 /// emitDWordBE - This callback is invoked when a 64-bit word needs to be 143 /// written to the output stream in big-endian format. 144 /// 145 void emitDWordBE(uint64_t W) { 146 if (8 <= BufferEnd-CurBufferPtr) { 147 *CurBufferPtr++ = (uint8_t)(W >> 56); 148 *CurBufferPtr++ = (uint8_t)(W >> 48); 149 *CurBufferPtr++ = (uint8_t)(W >> 40); 150 *CurBufferPtr++ = (uint8_t)(W >> 32); 151 *CurBufferPtr++ = (uint8_t)(W >> 24); 152 *CurBufferPtr++ = (uint8_t)(W >> 16); 153 *CurBufferPtr++ = (uint8_t)(W >> 8); 154 *CurBufferPtr++ = (uint8_t)(W >> 0); 155 } else { 156 CurBufferPtr = BufferEnd; 157 } 158 } 159 160 /// emitAlignment - Move the CurBufferPtr pointer up the the specified 161 /// alignment (saturated to BufferEnd of course). 162 void emitAlignment(unsigned Alignment) { 163 if (Alignment == 0) Alignment = 1; 164 165 if(Alignment <= (uintptr_t)(BufferEnd-CurBufferPtr)) { 166 // Move the current buffer ptr up to the specified alignment. 167 CurBufferPtr = 168 (uint8_t*)(((uintptr_t)CurBufferPtr+Alignment-1) & 169 ~(uintptr_t)(Alignment-1)); 170 } else { 171 CurBufferPtr = BufferEnd; 172 } 173 } 174 175 176 /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be 177 /// written to the output stream. 178 void emitULEB128Bytes(uint64_t Value) { 179 do { 180 uint8_t Byte = Value & 0x7f; 181 Value >>= 7; 182 if (Value) Byte |= 0x80; 183 emitByte(Byte); 184 } while (Value); 185 } 186 187 /// emitSLEB128Bytes - This callback is invoked when a SLEB128 needs to be 188 /// written to the output stream. 189 void emitSLEB128Bytes(int64_t Value) { 190 int32_t Sign = Value >> (8 * sizeof(Value) - 1); 191 bool IsMore; 192 193 do { 194 uint8_t Byte = Value & 0x7f; 195 Value >>= 7; 196 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0; 197 if (IsMore) Byte |= 0x80; 198 emitByte(Byte); 199 } while (IsMore); 200 } 201 202 /// emitString - This callback is invoked when a String needs to be 203 /// written to the output stream. 204 void emitString(const std::string &String) { 205 for (unsigned i = 0, N = static_cast<unsigned>(String.size()); 206 i < N; ++i) { 207 uint8_t C = String[i]; 208 emitByte(C); 209 } 210 emitByte(0); 211 } 212 213 /// emitInt32 - Emit a int32 directive. 214 void emitInt32(uint32_t Value) { 215 if (4 <= BufferEnd-CurBufferPtr) { 216 *((uint32_t*)CurBufferPtr) = Value; 217 CurBufferPtr += 4; 218 } else { 219 CurBufferPtr = BufferEnd; 220 } 221 } 222 223 /// emitInt64 - Emit a int64 directive. 224 void emitInt64(uint64_t Value) { 225 if (8 <= BufferEnd-CurBufferPtr) { 226 *((uint64_t*)CurBufferPtr) = Value; 227 CurBufferPtr += 8; 228 } else { 229 CurBufferPtr = BufferEnd; 230 } 231 } 232 233 /// emitInt32At - Emit the Int32 Value in Addr. 234 void emitInt32At(uintptr_t *Addr, uintptr_t Value) { 235 if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd) 236 (*(uint32_t*)Addr) = (uint32_t)Value; 237 } 238 239 /// emitInt64At - Emit the Int64 Value in Addr. 240 void emitInt64At(uintptr_t *Addr, uintptr_t Value) { 241 if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd) 242 (*(uint64_t*)Addr) = (uint64_t)Value; 243 } 244 245 246 /// emitLabel - Emits a label 247 virtual void emitLabel(uint64_t LabelID) = 0; 248 249 /// allocateSpace - Allocate a block of space in the current output buffer, 250 /// returning null (and setting conditions to indicate buffer overflow) on 251 /// failure. Alignment is the alignment in bytes of the buffer desired. 252 virtual void *allocateSpace(uintptr_t Size, unsigned Alignment) { 253 emitAlignment(Alignment); 254 void *Result; 255 256 // Check for buffer overflow. 257 if (Size >= (uintptr_t)(BufferEnd-CurBufferPtr)) { 258 CurBufferPtr = BufferEnd; 259 Result = 0; 260 } else { 261 // Allocate the space. 262 Result = CurBufferPtr; 263 CurBufferPtr += Size; 264 } 265 266 return Result; 267 } 268 269 /// allocateGlobal - Allocate memory for a global. Unlike allocateSpace, 270 /// this method does not allocate memory in the current output buffer, 271 /// because a global may live longer than the current function. 272 virtual void *allocateGlobal(uintptr_t Size, unsigned Alignment) = 0; 273 274 /// StartMachineBasicBlock - This should be called by the target when a new 275 /// basic block is about to be emitted. This way the MCE knows where the 276 /// start of the block is, and can implement getMachineBasicBlockAddress. 277 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) = 0; 278 279 /// getCurrentPCValue - This returns the address that the next emitted byte 280 /// will be output to. 281 /// 282 virtual uintptr_t getCurrentPCValue() const { 283 return (uintptr_t)CurBufferPtr; 284 } 285 286 /// getCurrentPCOffset - Return the offset from the start of the emitted 287 /// buffer that we are currently writing to. 288 uintptr_t getCurrentPCOffset() const { 289 return CurBufferPtr-BufferBegin; 290 } 291 292 /// addRelocation - Whenever a relocatable address is needed, it should be 293 /// noted with this interface. 294 virtual void addRelocation(const MachineRelocation &MR) = 0; 295 296 /// FIXME: These should all be handled with relocations! 297 298 /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in 299 /// the constant pool that was last emitted with the emitConstantPool method. 300 /// 301 virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const = 0; 302 303 /// getJumpTableEntryAddress - Return the address of the jump table with index 304 /// 'Index' in the function that last called initJumpTableInfo. 305 /// 306 virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const = 0; 307 308 /// getMachineBasicBlockAddress - Return the address of the specified 309 /// MachineBasicBlock, only usable after the label for the MBB has been 310 /// emitted. 311 /// 312 virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const= 0; 313 314 /// getLabelAddress - Return the address of the specified LabelID, only usable 315 /// after the LabelID has been emitted. 316 /// 317 virtual uintptr_t getLabelAddress(uint64_t LabelID) const = 0; 318 319 /// Specifies the MachineModuleInfo object. This is used for exception handling 320 /// purposes. 321 virtual void setModuleInfo(MachineModuleInfo* Info) = 0; 322}; 323 324} // End llvm namespace 325 326#endif 327