1193323Sed//===-- llvm/CodeGen/JITCodeEmitter.h - Code emission ----------*- C++ -*-===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This file defines an abstract interface that is used by the machine code 11193323Sed// emission framework to output the code. This allows machine code emission to 12193323Sed// be separated from concerns such as resolution of call targets, and where the 13193323Sed// machine code will be written (memory or disk, f.e.). 14193323Sed// 15193323Sed//===----------------------------------------------------------------------===// 16193323Sed 17193323Sed#ifndef LLVM_CODEGEN_JITCODEEMITTER_H 18193323Sed#define LLVM_CODEGEN_JITCODEEMITTER_H 19193323Sed 20249423Sdim#include "llvm/ADT/DenseMap.h" 21249423Sdim#include "llvm/CodeGen/MachineCodeEmitter.h" 22218893Sdim#include "llvm/Support/DataTypes.h" 23198090Srdivacky#include "llvm/Support/MathExtras.h" 24249423Sdim#include <string> 25193323Sed 26193323Sednamespace llvm { 27193323Sed 28193323Sedclass MachineBasicBlock; 29193323Sedclass MachineConstantPool; 30193323Sedclass MachineJumpTableInfo; 31193323Sedclass MachineFunction; 32193323Sedclass MachineModuleInfo; 33193323Sedclass MachineRelocation; 34193323Sedclass Value; 35193323Sedclass GlobalValue; 36193323Sedclass Function; 37205218Srdivacky 38193323Sed/// JITCodeEmitter - This class defines two sorts of methods: those for 39221345Sdim/// emitting the actual bytes of machine code, and those for emitting auxiliary 40193323Sed/// structures, such as jump tables, relocations, etc. 41193323Sed/// 42193323Sed/// Emission of machine code is complicated by the fact that we don't (in 43193323Sed/// general) know the size of the machine code that we're about to emit before 44193323Sed/// we emit it. As such, we preallocate a certain amount of memory, and set the 45193323Sed/// BufferBegin/BufferEnd pointers to the start and end of the buffer. As we 46193323Sed/// emit machine instructions, we advance the CurBufferPtr to indicate the 47193323Sed/// location of the next byte to emit. In the case of a buffer overflow (we 48193323Sed/// need to emit more machine code than we have allocated space for), the 49193323Sed/// CurBufferPtr will saturate to BufferEnd and ignore stores. Once the entire 50193323Sed/// function has been emitted, the overflow condition is checked, and if it has 51193323Sed/// occurred, more memory is allocated, and we reemit the code into it. 52193323Sed/// 53193323Sedclass JITCodeEmitter : public MachineCodeEmitter { 54234353Sdim virtual void anchor(); 55193323Sedpublic: 56193323Sed virtual ~JITCodeEmitter() {} 57193323Sed 58193323Sed /// startFunction - This callback is invoked when the specified function is 59193323Sed /// about to be code generated. This initializes the BufferBegin/End/Ptr 60193323Sed /// fields. 61193323Sed /// 62193323Sed virtual void startFunction(MachineFunction &F) = 0; 63193323Sed 64193323Sed /// finishFunction - This callback is invoked when the specified function has 65193323Sed /// finished code generation. If a buffer overflow has occurred, this method 66193323Sed /// returns true (the callee is required to try again), otherwise it returns 67193323Sed /// false. 68193323Sed /// 69193323Sed virtual bool finishFunction(MachineFunction &F) = 0; 70193323Sed 71201360Srdivacky /// allocIndirectGV - Allocates and fills storage for an indirect 72201360Srdivacky /// GlobalValue, and returns the address. 73201360Srdivacky virtual void *allocIndirectGV(const GlobalValue *GV, 74201360Srdivacky const uint8_t *Buffer, size_t Size, 75201360Srdivacky unsigned Alignment) = 0; 76193323Sed 77193323Sed /// emitByte - This callback is invoked when a byte needs to be written to the 78193323Sed /// output stream. 79193323Sed /// 80193574Sed void emitByte(uint8_t B) { 81193323Sed if (CurBufferPtr != BufferEnd) 82193323Sed *CurBufferPtr++ = B; 83193323Sed } 84193323Sed 85193323Sed /// emitWordLE - This callback is invoked when a 32-bit word needs to be 86193323Sed /// written to the output stream in little-endian format. 87193323Sed /// 88194178Sed void emitWordLE(uint32_t W) { 89193323Sed if (4 <= BufferEnd-CurBufferPtr) { 90193574Sed *CurBufferPtr++ = (uint8_t)(W >> 0); 91193574Sed *CurBufferPtr++ = (uint8_t)(W >> 8); 92193574Sed *CurBufferPtr++ = (uint8_t)(W >> 16); 93193574Sed *CurBufferPtr++ = (uint8_t)(W >> 24); 94193323Sed } else { 95193323Sed CurBufferPtr = BufferEnd; 96193323Sed } 97193323Sed } 98193323Sed 99193323Sed /// emitWordBE - This callback is invoked when a 32-bit word needs to be 100193323Sed /// written to the output stream in big-endian format. 101193323Sed /// 102194178Sed void emitWordBE(uint32_t W) { 103193323Sed if (4 <= BufferEnd-CurBufferPtr) { 104193574Sed *CurBufferPtr++ = (uint8_t)(W >> 24); 105193574Sed *CurBufferPtr++ = (uint8_t)(W >> 16); 106193574Sed *CurBufferPtr++ = (uint8_t)(W >> 8); 107193574Sed *CurBufferPtr++ = (uint8_t)(W >> 0); 108193323Sed } else { 109193323Sed CurBufferPtr = BufferEnd; 110193323Sed } 111193323Sed } 112193323Sed 113193323Sed /// emitDWordLE - This callback is invoked when a 64-bit word needs to be 114193323Sed /// written to the output stream in little-endian format. 115193323Sed /// 116193323Sed void emitDWordLE(uint64_t W) { 117193323Sed if (8 <= BufferEnd-CurBufferPtr) { 118193574Sed *CurBufferPtr++ = (uint8_t)(W >> 0); 119193574Sed *CurBufferPtr++ = (uint8_t)(W >> 8); 120193574Sed *CurBufferPtr++ = (uint8_t)(W >> 16); 121193574Sed *CurBufferPtr++ = (uint8_t)(W >> 24); 122193574Sed *CurBufferPtr++ = (uint8_t)(W >> 32); 123193574Sed *CurBufferPtr++ = (uint8_t)(W >> 40); 124193574Sed *CurBufferPtr++ = (uint8_t)(W >> 48); 125193574Sed *CurBufferPtr++ = (uint8_t)(W >> 56); 126193323Sed } else { 127193323Sed CurBufferPtr = BufferEnd; 128193323Sed } 129193323Sed } 130193323Sed 131193323Sed /// emitDWordBE - This callback is invoked when a 64-bit word needs to be 132193323Sed /// written to the output stream in big-endian format. 133193323Sed /// 134193323Sed void emitDWordBE(uint64_t W) { 135193323Sed if (8 <= BufferEnd-CurBufferPtr) { 136193574Sed *CurBufferPtr++ = (uint8_t)(W >> 56); 137193574Sed *CurBufferPtr++ = (uint8_t)(W >> 48); 138193574Sed *CurBufferPtr++ = (uint8_t)(W >> 40); 139193574Sed *CurBufferPtr++ = (uint8_t)(W >> 32); 140193574Sed *CurBufferPtr++ = (uint8_t)(W >> 24); 141193574Sed *CurBufferPtr++ = (uint8_t)(W >> 16); 142193574Sed *CurBufferPtr++ = (uint8_t)(W >> 8); 143193574Sed *CurBufferPtr++ = (uint8_t)(W >> 0); 144193323Sed } else { 145193323Sed CurBufferPtr = BufferEnd; 146193323Sed } 147193323Sed } 148193323Sed 149203954Srdivacky /// emitAlignment - Move the CurBufferPtr pointer up to the specified 150193323Sed /// alignment (saturated to BufferEnd of course). 151193323Sed void emitAlignment(unsigned Alignment) { 152193323Sed if (Alignment == 0) Alignment = 1; 153198090Srdivacky uint8_t *NewPtr = (uint8_t*)RoundUpToAlignment((uintptr_t)CurBufferPtr, 154198090Srdivacky Alignment); 155198090Srdivacky CurBufferPtr = std::min(NewPtr, BufferEnd); 156198090Srdivacky } 157193323Sed 158198090Srdivacky /// emitAlignmentWithFill - Similar to emitAlignment, except that the 159198090Srdivacky /// extra bytes are filled with the provided byte. 160198090Srdivacky void emitAlignmentWithFill(unsigned Alignment, uint8_t Fill) { 161198090Srdivacky if (Alignment == 0) Alignment = 1; 162198090Srdivacky uint8_t *NewPtr = (uint8_t*)RoundUpToAlignment((uintptr_t)CurBufferPtr, 163198090Srdivacky Alignment); 164198090Srdivacky // Fail if we don't have room. 165198090Srdivacky if (NewPtr > BufferEnd) { 166193323Sed CurBufferPtr = BufferEnd; 167198090Srdivacky return; 168193323Sed } 169198090Srdivacky while (CurBufferPtr < NewPtr) { 170198090Srdivacky *CurBufferPtr++ = Fill; 171198090Srdivacky } 172193323Sed } 173193323Sed 174193323Sed /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be 175193323Sed /// written to the output stream. 176207618Srdivacky void emitULEB128Bytes(uint64_t Value, unsigned PadTo = 0) { 177193323Sed do { 178193574Sed uint8_t Byte = Value & 0x7f; 179193323Sed Value >>= 7; 180207618Srdivacky if (Value || PadTo != 0) Byte |= 0x80; 181193323Sed emitByte(Byte); 182193323Sed } while (Value); 183207618Srdivacky 184207618Srdivacky if (PadTo) { 185207618Srdivacky do { 186207618Srdivacky uint8_t Byte = (PadTo > 1) ? 0x80 : 0x0; 187207618Srdivacky emitByte(Byte); 188207618Srdivacky } while (--PadTo); 189207618Srdivacky } 190193323Sed } 191193323Sed 192193323Sed /// emitSLEB128Bytes - This callback is invoked when a SLEB128 needs to be 193193323Sed /// written to the output stream. 194194178Sed void emitSLEB128Bytes(int64_t Value) { 195193574Sed int32_t Sign = Value >> (8 * sizeof(Value) - 1); 196193323Sed bool IsMore; 197193323Sed 198193323Sed do { 199193574Sed uint8_t Byte = Value & 0x7f; 200193323Sed Value >>= 7; 201193323Sed IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0; 202193323Sed if (IsMore) Byte |= 0x80; 203193323Sed emitByte(Byte); 204193323Sed } while (IsMore); 205193323Sed } 206193323Sed 207193323Sed /// emitString - This callback is invoked when a String needs to be 208193323Sed /// written to the output stream. 209193323Sed void emitString(const std::string &String) { 210249423Sdim for (size_t i = 0, N = String.size(); i < N; ++i) { 211193574Sed uint8_t C = String[i]; 212193323Sed emitByte(C); 213193323Sed } 214193323Sed emitByte(0); 215193323Sed } 216193323Sed 217193323Sed /// emitInt32 - Emit a int32 directive. 218194178Sed void emitInt32(uint32_t Value) { 219193323Sed if (4 <= BufferEnd-CurBufferPtr) { 220193323Sed *((uint32_t*)CurBufferPtr) = Value; 221193323Sed CurBufferPtr += 4; 222193323Sed } else { 223193323Sed CurBufferPtr = BufferEnd; 224193323Sed } 225193323Sed } 226193323Sed 227193323Sed /// emitInt64 - Emit a int64 directive. 228193323Sed void emitInt64(uint64_t Value) { 229193323Sed if (8 <= BufferEnd-CurBufferPtr) { 230193323Sed *((uint64_t*)CurBufferPtr) = Value; 231193323Sed CurBufferPtr += 8; 232193323Sed } else { 233193323Sed CurBufferPtr = BufferEnd; 234193323Sed } 235193323Sed } 236193323Sed 237193323Sed /// emitInt32At - Emit the Int32 Value in Addr. 238193323Sed void emitInt32At(uintptr_t *Addr, uintptr_t Value) { 239193323Sed if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd) 240193323Sed (*(uint32_t*)Addr) = (uint32_t)Value; 241193323Sed } 242193323Sed 243193323Sed /// emitInt64At - Emit the Int64 Value in Addr. 244193323Sed void emitInt64At(uintptr_t *Addr, uintptr_t Value) { 245193323Sed if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd) 246193323Sed (*(uint64_t*)Addr) = (uint64_t)Value; 247193323Sed } 248193323Sed 249193323Sed 250193323Sed /// emitLabel - Emits a label 251205218Srdivacky virtual void emitLabel(MCSymbol *Label) = 0; 252193323Sed 253193323Sed /// allocateSpace - Allocate a block of space in the current output buffer, 254193323Sed /// returning null (and setting conditions to indicate buffer overflow) on 255193323Sed /// failure. Alignment is the alignment in bytes of the buffer desired. 256193323Sed virtual void *allocateSpace(uintptr_t Size, unsigned Alignment) { 257193323Sed emitAlignment(Alignment); 258193323Sed void *Result; 259193323Sed 260193323Sed // Check for buffer overflow. 261193323Sed if (Size >= (uintptr_t)(BufferEnd-CurBufferPtr)) { 262193323Sed CurBufferPtr = BufferEnd; 263193323Sed Result = 0; 264193323Sed } else { 265193323Sed // Allocate the space. 266193323Sed Result = CurBufferPtr; 267193323Sed CurBufferPtr += Size; 268193323Sed } 269193323Sed 270193323Sed return Result; 271193323Sed } 272193323Sed 273198090Srdivacky /// allocateGlobal - Allocate memory for a global. Unlike allocateSpace, 274198090Srdivacky /// this method does not allocate memory in the current output buffer, 275198090Srdivacky /// because a global may live longer than the current function. 276198090Srdivacky virtual void *allocateGlobal(uintptr_t Size, unsigned Alignment) = 0; 277198090Srdivacky 278193323Sed /// StartMachineBasicBlock - This should be called by the target when a new 279193323Sed /// basic block is about to be emitted. This way the MCE knows where the 280193323Sed /// start of the block is, and can implement getMachineBasicBlockAddress. 281193323Sed virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) = 0; 282193323Sed 283193323Sed /// getCurrentPCValue - This returns the address that the next emitted byte 284193323Sed /// will be output to. 285193323Sed /// 286193323Sed virtual uintptr_t getCurrentPCValue() const { 287193323Sed return (uintptr_t)CurBufferPtr; 288193323Sed } 289193323Sed 290193323Sed /// getCurrentPCOffset - Return the offset from the start of the emitted 291193323Sed /// buffer that we are currently writing to. 292193323Sed uintptr_t getCurrentPCOffset() const { 293193323Sed return CurBufferPtr-BufferBegin; 294193323Sed } 295193323Sed 296198090Srdivacky /// earlyResolveAddresses - True if the code emitter can use symbol addresses 297198090Srdivacky /// during code emission time. The JIT is capable of doing this because it 298198090Srdivacky /// creates jump tables or constant pools in memory on the fly while the 299198090Srdivacky /// object code emitters rely on a linker to have real addresses and should 300198090Srdivacky /// use relocations instead. 301198090Srdivacky bool earlyResolveAddresses() const { return true; } 302198090Srdivacky 303193323Sed /// addRelocation - Whenever a relocatable address is needed, it should be 304193323Sed /// noted with this interface. 305193323Sed virtual void addRelocation(const MachineRelocation &MR) = 0; 306193323Sed 307193323Sed /// FIXME: These should all be handled with relocations! 308193323Sed 309193323Sed /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in 310193323Sed /// the constant pool that was last emitted with the emitConstantPool method. 311193323Sed /// 312193323Sed virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const = 0; 313193323Sed 314193323Sed /// getJumpTableEntryAddress - Return the address of the jump table with index 315193323Sed /// 'Index' in the function that last called initJumpTableInfo. 316193323Sed /// 317193323Sed virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const = 0; 318193323Sed 319193323Sed /// getMachineBasicBlockAddress - Return the address of the specified 320193323Sed /// MachineBasicBlock, only usable after the label for the MBB has been 321193323Sed /// emitted. 322193323Sed /// 323193323Sed virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const= 0; 324193323Sed 325205218Srdivacky /// getLabelAddress - Return the address of the specified Label, only usable 326205218Srdivacky /// after the Label has been emitted. 327193323Sed /// 328205218Srdivacky virtual uintptr_t getLabelAddress(MCSymbol *Label) const = 0; 329193323Sed 330193323Sed /// Specifies the MachineModuleInfo object. This is used for exception handling 331193323Sed /// purposes. 332193323Sed virtual void setModuleInfo(MachineModuleInfo* Info) = 0; 333207618Srdivacky 334207618Srdivacky /// getLabelLocations - Return the label locations map of the label IDs to 335207618Srdivacky /// their address. 336207618Srdivacky virtual DenseMap<MCSymbol*, uintptr_t> *getLabelLocations() { return 0; } 337193323Sed}; 338193323Sed 339193323Sed} // End llvm namespace 340193323Sed 341193323Sed#endif 342