JITCodeEmitter.h revision 5c2a079d3cfa2090cca855beeb6916b645d8047c
1a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes//===-- llvm/CodeGen/JITCodeEmitter.h - Code emission ----------*- C++ -*-===// 2a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes// 3a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes// The LLVM Compiler Infrastructure 4a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes// 5a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes// This file is distributed under the University of Illinois Open Source 6a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes// License. See LICENSE.TXT for details. 7a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes// 8a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes//===----------------------------------------------------------------------===// 9a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes// 10a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes// This file defines an abstract interface that is used by the machine code 11a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes// emission framework to output the code. This allows machine code emission to 12a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes// be separated from concerns such as resolution of call targets, and where the 13a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes// machine code will be written (memory or disk, f.e.). 14a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes// 15a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes//===----------------------------------------------------------------------===// 16a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 17a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes#ifndef LLVM_CODEGEN_JITCODEEMITTER_H 18a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes#define LLVM_CODEGEN_JITCODEEMITTER_H 19a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 20255f89faee13dc491cb64fbeae3c763e7e2ea4e6Chandler Carruth#include "llvm/ADT/DenseMap.h" 21255f89faee13dc491cb64fbeae3c763e7e2ea4e6Chandler Carruth#include "llvm/CodeGen/MachineCodeEmitter.h" 221f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/DataTypes.h" 2301248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner#include "llvm/Support/MathExtras.h" 24255f89faee13dc491cb64fbeae3c763e7e2ea4e6Chandler Carruth#include <string> 25a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 26a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopesnamespace llvm { 27a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 28a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopesclass MachineBasicBlock; 29a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopesclass MachineConstantPool; 30a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopesclass MachineJumpTableInfo; 31a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopesclass MachineFunction; 32a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopesclass MachineModuleInfo; 33a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopesclass MachineRelocation; 34a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopesclass Value; 35a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopesclass GlobalValue; 36a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopesclass Function; 371611273351d75b5cbe2a67485bb9831d5916fe26Chris Lattner 38a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes/// JITCodeEmitter - This class defines two sorts of methods: those for 397a2bdde0a0eebcd2125055e0eacaca040f0b766cChris Lattner/// emitting the actual bytes of machine code, and those for emitting auxiliary 40a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes/// structures, such as jump tables, relocations, etc. 41a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes/// 42a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes/// Emission of machine code is complicated by the fact that we don't (in 43a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes/// general) know the size of the machine code that we're about to emit before 44a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes/// we emit it. As such, we preallocate a certain amount of memory, and set the 45a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes/// BufferBegin/BufferEnd pointers to the start and end of the buffer. As we 46a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes/// emit machine instructions, we advance the CurBufferPtr to indicate the 47a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes/// location of the next byte to emit. In the case of a buffer overflow (we 48a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes/// need to emit more machine code than we have allocated space for), the 49a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes/// CurBufferPtr will saturate to BufferEnd and ignore stores. Once the entire 50a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes/// function has been emitted, the overflow condition is checked, and if it has 51a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes/// occurred, more memory is allocated, and we reemit the code into it. 52a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes/// 53a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopesclass JITCodeEmitter : public MachineCodeEmitter { 542d24e2a396a1d211baaeedf32148a3b657240170David Blaikie virtual void anchor(); 55a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopespublic: 56a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes virtual ~JITCodeEmitter() {} 57a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 58a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// startFunction - This callback is invoked when the specified function is 59a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// about to be code generated. This initializes the BufferBegin/End/Ptr 60a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// fields. 61a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// 62a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes virtual void startFunction(MachineFunction &F) = 0; 63a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 64a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// finishFunction - This callback is invoked when the specified function has 65a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// finished code generation. If a buffer overflow has occurred, this method 66a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// returns true (the callee is required to try again), otherwise it returns 67a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// false. 68a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// 69a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes virtual bool finishFunction(MachineFunction &F) = 0; 70a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 7132d7e6ebde29faeea75ecb718b4281414b0eea0bJeffrey Yasskin /// allocIndirectGV - Allocates and fills storage for an indirect 7232d7e6ebde29faeea75ecb718b4281414b0eea0bJeffrey Yasskin /// GlobalValue, and returns the address. 7332d7e6ebde29faeea75ecb718b4281414b0eea0bJeffrey Yasskin virtual void *allocIndirectGV(const GlobalValue *GV, 7432d7e6ebde29faeea75ecb718b4281414b0eea0bJeffrey Yasskin const uint8_t *Buffer, size_t Size, 7532d7e6ebde29faeea75ecb718b4281414b0eea0bJeffrey Yasskin unsigned Alignment) = 0; 76a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 77a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// emitByte - This callback is invoked when a byte needs to be written to the 78a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// output stream. 79a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// 80186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes void emitByte(uint8_t B) { 81a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes if (CurBufferPtr != BufferEnd) 82a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes *CurBufferPtr++ = B; 83a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } 84a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 85a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// emitWordLE - This callback is invoked when a 32-bit word needs to be 86a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// written to the output stream in little-endian format. 87a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// 887e0b551e2aeefd9b32810e14b8b27b6da3ec441eBruno Cardoso Lopes void emitWordLE(uint32_t W) { 89a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes if (4 <= BufferEnd-CurBufferPtr) { 90186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 0); 91186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 8); 92186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 16); 93186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 24); 94a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } else { 95a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes CurBufferPtr = BufferEnd; 96a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } 97a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } 98a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 99a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// emitWordBE - This callback is invoked when a 32-bit word needs to be 100a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// written to the output stream in big-endian format. 101a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// 1027e0b551e2aeefd9b32810e14b8b27b6da3ec441eBruno Cardoso Lopes void emitWordBE(uint32_t W) { 103a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes if (4 <= BufferEnd-CurBufferPtr) { 104186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 24); 105186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 16); 106186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 8); 107186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 0); 108a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } else { 109a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes CurBufferPtr = BufferEnd; 110a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } 111a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } 112a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 113a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// emitDWordLE - This callback is invoked when a 64-bit word needs to be 114a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// written to the output stream in little-endian format. 115a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// 116a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes void emitDWordLE(uint64_t W) { 117a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes if (8 <= BufferEnd-CurBufferPtr) { 118186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 0); 119186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 8); 120186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 16); 121186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 24); 122186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 32); 123186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 40); 124186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 48); 125186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 56); 126a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } else { 127a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes CurBufferPtr = BufferEnd; 128a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } 129a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } 130a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 131a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// emitDWordBE - This callback is invoked when a 64-bit word needs to be 132a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// written to the output stream in big-endian format. 133a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// 134a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes void emitDWordBE(uint64_t W) { 135a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes if (8 <= BufferEnd-CurBufferPtr) { 136186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 56); 137186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 48); 138186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 40); 139186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 32); 140186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 24); 141186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 16); 142186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 8); 143186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes *CurBufferPtr++ = (uint8_t)(W >> 0); 144a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } else { 145a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes CurBufferPtr = BufferEnd; 146a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } 147a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } 148a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 149f3b11aa6a72e0c31066a60c2e888e7a5eb5f2399Dan Gohman /// emitAlignment - Move the CurBufferPtr pointer up to the specified 150a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// alignment (saturated to BufferEnd of course). 151a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes void emitAlignment(unsigned Alignment) { 152a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes if (Alignment == 0) Alignment = 1; 15301248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner uint8_t *NewPtr = (uint8_t*)RoundUpToAlignment((uintptr_t)CurBufferPtr, 15401248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner Alignment); 15501248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner CurBufferPtr = std::min(NewPtr, BufferEnd); 15601248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner } 157a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 15801248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner /// emitAlignmentWithFill - Similar to emitAlignment, except that the 15901248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner /// extra bytes are filled with the provided byte. 16001248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner void emitAlignmentWithFill(unsigned Alignment, uint8_t Fill) { 16101248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner if (Alignment == 0) Alignment = 1; 16201248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner uint8_t *NewPtr = (uint8_t*)RoundUpToAlignment((uintptr_t)CurBufferPtr, 16301248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner Alignment); 16401248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner // Fail if we don't have room. 16501248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner if (NewPtr > BufferEnd) { 166a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes CurBufferPtr = BufferEnd; 16701248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner return; 16801248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner } 16901248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner while (CurBufferPtr < NewPtr) { 17001248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner *CurBufferPtr++ = Fill; 171a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } 172a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } 173a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 174a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be 175a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// written to the output stream. 17621739c1c72c3eadd5635309a8c68b7677f2a32f6Bill Wendling void emitULEB128Bytes(uint64_t Value, unsigned PadTo = 0) { 177a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes do { 178186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes uint8_t Byte = Value & 0x7f; 179a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes Value >>= 7; 18021739c1c72c3eadd5635309a8c68b7677f2a32f6Bill Wendling if (Value || PadTo != 0) Byte |= 0x80; 181a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes emitByte(Byte); 182a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } while (Value); 18321739c1c72c3eadd5635309a8c68b7677f2a32f6Bill Wendling 18421739c1c72c3eadd5635309a8c68b7677f2a32f6Bill Wendling if (PadTo) { 18521739c1c72c3eadd5635309a8c68b7677f2a32f6Bill Wendling do { 18621739c1c72c3eadd5635309a8c68b7677f2a32f6Bill Wendling uint8_t Byte = (PadTo > 1) ? 0x80 : 0x0; 18721739c1c72c3eadd5635309a8c68b7677f2a32f6Bill Wendling emitByte(Byte); 18821739c1c72c3eadd5635309a8c68b7677f2a32f6Bill Wendling } while (--PadTo); 18921739c1c72c3eadd5635309a8c68b7677f2a32f6Bill Wendling } 190a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } 191a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 192a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// emitSLEB128Bytes - This callback is invoked when a SLEB128 needs to be 193a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// written to the output stream. 1947e0b551e2aeefd9b32810e14b8b27b6da3ec441eBruno Cardoso Lopes void emitSLEB128Bytes(int64_t Value) { 195186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes int32_t Sign = Value >> (8 * sizeof(Value) - 1); 196a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes bool IsMore; 197a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 198a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes do { 199186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes uint8_t Byte = Value & 0x7f; 200a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes Value >>= 7; 201a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0; 202a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes if (IsMore) Byte |= 0x80; 203a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes emitByte(Byte); 204a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } while (IsMore); 205a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } 206a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 207a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// emitString - This callback is invoked when a String needs to be 208a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// written to the output stream. 209a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes void emitString(const std::string &String) { 2105c2a079d3cfa2090cca855beeb6916b645d8047cJakub Staszak for (size_t i = 0, N = String.size(); i < N; ++i) { 211186c670e15828327960d05a652ec43ae768c9b8eBruno Cardoso Lopes uint8_t C = String[i]; 212a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes emitByte(C); 213a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } 214a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes emitByte(0); 215a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } 216a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 217a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// emitInt32 - Emit a int32 directive. 2187e0b551e2aeefd9b32810e14b8b27b6da3ec441eBruno Cardoso Lopes void emitInt32(uint32_t Value) { 219a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes if (4 <= BufferEnd-CurBufferPtr) { 220a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes *((uint32_t*)CurBufferPtr) = Value; 221a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes CurBufferPtr += 4; 222a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } else { 223a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes CurBufferPtr = BufferEnd; 224a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } 225a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } 226a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 227a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// emitInt64 - Emit a int64 directive. 228a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes void emitInt64(uint64_t Value) { 229a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes if (8 <= BufferEnd-CurBufferPtr) { 230a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes *((uint64_t*)CurBufferPtr) = Value; 231a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes CurBufferPtr += 8; 232a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } else { 233a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes CurBufferPtr = BufferEnd; 234a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } 235a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } 236a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 237a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// emitInt32At - Emit the Int32 Value in Addr. 238a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes void emitInt32At(uintptr_t *Addr, uintptr_t Value) { 239a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd) 240a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes (*(uint32_t*)Addr) = (uint32_t)Value; 241a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } 242a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 243a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// emitInt64At - Emit the Int64 Value in Addr. 244a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes void emitInt64At(uintptr_t *Addr, uintptr_t Value) { 245a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd) 246a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes (*(uint64_t*)Addr) = (uint64_t)Value; 247a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } 248a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 249a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 250a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// emitLabel - Emits a label 2511611273351d75b5cbe2a67485bb9831d5916fe26Chris Lattner virtual void emitLabel(MCSymbol *Label) = 0; 252a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 253a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// allocateSpace - Allocate a block of space in the current output buffer, 254a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// returning null (and setting conditions to indicate buffer overflow) on 255a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// failure. Alignment is the alignment in bytes of the buffer desired. 256a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes virtual void *allocateSpace(uintptr_t Size, unsigned Alignment) { 257a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes emitAlignment(Alignment); 258a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes void *Result; 259a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 260a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes // Check for buffer overflow. 261a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes if (Size >= (uintptr_t)(BufferEnd-CurBufferPtr)) { 262a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes CurBufferPtr = BufferEnd; 263a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes Result = 0; 264a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } else { 265a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes // Allocate the space. 266a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes Result = CurBufferPtr; 267a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes CurBufferPtr += Size; 268a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } 269a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 270a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes return Result; 271a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } 272a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 273489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin /// allocateGlobal - Allocate memory for a global. Unlike allocateSpace, 274489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin /// this method does not allocate memory in the current output buffer, 275489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin /// because a global may live longer than the current function. 276489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin virtual void *allocateGlobal(uintptr_t Size, unsigned Alignment) = 0; 277489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin 278a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// StartMachineBasicBlock - This should be called by the target when a new 279a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// basic block is about to be emitted. This way the MCE knows where the 280a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// start of the block is, and can implement getMachineBasicBlockAddress. 281a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) = 0; 282a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 283a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// getCurrentPCValue - This returns the address that the next emitted byte 284a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// will be output to. 285a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// 286a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes virtual uintptr_t getCurrentPCValue() const { 287a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes return (uintptr_t)CurBufferPtr; 288a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } 289a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 290a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// getCurrentPCOffset - Return the offset from the start of the emitted 291a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// buffer that we are currently writing to. 292a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes uintptr_t getCurrentPCOffset() const { 293a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes return CurBufferPtr-BufferBegin; 294a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes } 295a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 296e55fef36a93badf6c4925ea2455a1ca6b361357eBruno Cardoso Lopes /// earlyResolveAddresses - True if the code emitter can use symbol addresses 297e55fef36a93badf6c4925ea2455a1ca6b361357eBruno Cardoso Lopes /// during code emission time. The JIT is capable of doing this because it 298e55fef36a93badf6c4925ea2455a1ca6b361357eBruno Cardoso Lopes /// creates jump tables or constant pools in memory on the fly while the 299e55fef36a93badf6c4925ea2455a1ca6b361357eBruno Cardoso Lopes /// object code emitters rely on a linker to have real addresses and should 300e55fef36a93badf6c4925ea2455a1ca6b361357eBruno Cardoso Lopes /// use relocations instead. 301e55fef36a93badf6c4925ea2455a1ca6b361357eBruno Cardoso Lopes bool earlyResolveAddresses() const { return true; } 302e55fef36a93badf6c4925ea2455a1ca6b361357eBruno Cardoso Lopes 303a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// addRelocation - Whenever a relocatable address is needed, it should be 304a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// noted with this interface. 305a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes virtual void addRelocation(const MachineRelocation &MR) = 0; 306a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 307a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// FIXME: These should all be handled with relocations! 308a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 309a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in 310a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// the constant pool that was last emitted with the emitConstantPool method. 311a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// 312a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const = 0; 313a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 314a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// getJumpTableEntryAddress - Return the address of the jump table with index 315a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// 'Index' in the function that last called initJumpTableInfo. 316a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// 317a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const = 0; 318a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 319a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// getMachineBasicBlockAddress - Return the address of the specified 320a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// MachineBasicBlock, only usable after the label for the MBB has been 321a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// emitted. 322a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// 323a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const= 0; 324a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 3251611273351d75b5cbe2a67485bb9831d5916fe26Chris Lattner /// getLabelAddress - Return the address of the specified Label, only usable 3261611273351d75b5cbe2a67485bb9831d5916fe26Chris Lattner /// after the Label has been emitted. 327a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// 3281611273351d75b5cbe2a67485bb9831d5916fe26Chris Lattner virtual uintptr_t getLabelAddress(MCSymbol *Label) const = 0; 329a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 330a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// Specifies the MachineModuleInfo object. This is used for exception handling 331a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// purposes. 332a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes virtual void setModuleInfo(MachineModuleInfo* Info) = 0; 33347639fc5be0b8f4873d076a9ed24b9a3c0682b15Bill Wendling 33447639fc5be0b8f4873d076a9ed24b9a3c0682b15Bill Wendling /// getLabelLocations - Return the label locations map of the label IDs to 33547639fc5be0b8f4873d076a9ed24b9a3c0682b15Bill Wendling /// their address. 33647639fc5be0b8f4873d076a9ed24b9a3c0682b15Bill Wendling virtual DenseMap<MCSymbol*, uintptr_t> *getLabelLocations() { return 0; } 337a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes}; 338a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 339a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes} // End llvm namespace 340a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 341a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes#endif 342