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