1 // Copyright 2017, VIXL authors 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are met: 6 // 7 // * Redistributions of source code must retain the above copyright notice, 8 // this list of conditions and the following disclaimer. 9 // * Redistributions in binary form must reproduce the above copyright notice, 10 // this list of conditions and the following disclaimer in the documentation 11 // and/or other materials provided with the distribution. 12 // * Neither the name of ARM Limited nor the names of its contributors may be 13 // used to endorse or promote products derived from this software without 14 // specific prior written permission. 15 // 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND 17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 20 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 27 #ifndef VIXL_CODE_BUFFER_H 28 #define VIXL_CODE_BUFFER_H 29 30 #include <cstring> 31 32 #include "globals-vixl.h" 33 #include "utils-vixl.h" 34 35 namespace vixl { 36 37 class CodeBuffer { 38 public: 39 static const size_t kDefaultCapacity = 4 * KBytes; 40 41 explicit CodeBuffer(size_t capacity = kDefaultCapacity); 42 CodeBuffer(byte* buffer, size_t capacity); 43 ~CodeBuffer() VIXL_NEGATIVE_TESTING_ALLOW_EXCEPTION; 44 45 void Reset(); 46 47 // Make the buffer executable or writable. These states are mutually 48 // exclusive. 49 // Note that these require page-aligned memory blocks, which we can only 50 // guarantee with VIXL_CODE_BUFFER_MMAP. 51 void SetExecutable(); 52 void SetWritable(); 53 GetOffsetFrom(ptrdiff_t offset)54 ptrdiff_t GetOffsetFrom(ptrdiff_t offset) const { 55 ptrdiff_t cursor_offset = cursor_ - buffer_; 56 VIXL_ASSERT((offset >= 0) && (offset <= cursor_offset)); 57 return cursor_offset - offset; 58 } 59 VIXL_DEPRECATED("GetOffsetFrom", 60 ptrdiff_t OffsetFrom(ptrdiff_t offset) const) { 61 return GetOffsetFrom(offset); 62 } 63 GetCursorOffset()64 ptrdiff_t GetCursorOffset() const { return GetOffsetFrom(0); } 65 VIXL_DEPRECATED("GetCursorOffset", ptrdiff_t CursorOffset() const) { 66 return GetCursorOffset(); 67 } 68 Rewind(ptrdiff_t offset)69 void Rewind(ptrdiff_t offset) { 70 byte* rewound_cursor = buffer_ + offset; 71 VIXL_ASSERT((buffer_ <= rewound_cursor) && (rewound_cursor <= cursor_)); 72 cursor_ = rewound_cursor; 73 } 74 75 template <typename T> GetOffsetAddress(ptrdiff_t offset)76 T GetOffsetAddress(ptrdiff_t offset) const { 77 VIXL_STATIC_ASSERT(sizeof(T) >= sizeof(uintptr_t)); 78 VIXL_ASSERT((offset >= 0) && (offset <= (cursor_ - buffer_))); 79 return reinterpret_cast<T>(buffer_ + offset); 80 } 81 82 // Return the address of the start or end of the emitted code. 83 template <typename T> GetStartAddress()84 T GetStartAddress() const { 85 VIXL_STATIC_ASSERT(sizeof(T) >= sizeof(uintptr_t)); 86 return GetOffsetAddress<T>(0); 87 } 88 template <typename T> GetEndAddress()89 T GetEndAddress() const { 90 VIXL_STATIC_ASSERT(sizeof(T) >= sizeof(uintptr_t)); 91 return GetOffsetAddress<T>(GetSizeInBytes()); 92 } 93 GetRemainingBytes()94 size_t GetRemainingBytes() const { 95 VIXL_ASSERT((cursor_ >= buffer_) && (cursor_ <= (buffer_ + capacity_))); 96 return (buffer_ + capacity_) - cursor_; 97 } 98 VIXL_DEPRECATED("GetRemainingBytes", size_t RemainingBytes() const) { 99 return GetRemainingBytes(); 100 } 101 GetSizeInBytes()102 size_t GetSizeInBytes() const { 103 VIXL_ASSERT((cursor_ >= buffer_) && (cursor_ <= (buffer_ + capacity_))); 104 return cursor_ - buffer_; 105 } 106 107 // A code buffer can emit: 108 // * 8, 16, 32 or 64-bit data: constant. 109 // * 16 or 32-bit data: instruction. 110 // * string: debug info. Emit8(uint8_t data)111 void Emit8(uint8_t data) { Emit(data); } 112 Emit16(uint16_t data)113 void Emit16(uint16_t data) { Emit(data); } 114 Emit32(uint32_t data)115 void Emit32(uint32_t data) { Emit(data); } 116 Emit64(uint64_t data)117 void Emit64(uint64_t data) { Emit(data); } 118 119 void EmitString(const char* string); 120 121 void EmitData(const void* data, size_t size); 122 123 template <typename T> Emit(T value)124 void Emit(T value) { 125 VIXL_ASSERT(HasSpaceFor(sizeof(value))); 126 dirty_ = true; 127 memcpy(cursor_, &value, sizeof(value)); 128 cursor_ += sizeof(value); 129 } 130 131 void UpdateData(size_t offset, const void* data, size_t size); 132 133 // Align to 32bit. 134 void Align(); 135 136 // Ensure there is enough space for and emit 'n' zero bytes. 137 void EmitZeroedBytes(int n); 138 Is16bitAligned()139 bool Is16bitAligned() const { return IsAligned<2>(cursor_); } 140 Is32bitAligned()141 bool Is32bitAligned() const { return IsAligned<4>(cursor_); } 142 GetCapacity()143 size_t GetCapacity() const { return capacity_; } 144 VIXL_DEPRECATED("GetCapacity", size_t capacity() const) { 145 return GetCapacity(); 146 } 147 IsManaged()148 bool IsManaged() const { return managed_; } 149 150 void Grow(size_t new_capacity); 151 IsDirty()152 bool IsDirty() const { return dirty_; } 153 SetClean()154 void SetClean() { dirty_ = false; } 155 HasSpaceFor(size_t amount)156 bool HasSpaceFor(size_t amount) const { 157 return GetRemainingBytes() >= amount; 158 } 159 EnsureSpaceFor(size_t amount,bool * has_grown)160 void EnsureSpaceFor(size_t amount, bool* has_grown) { 161 bool is_full = !HasSpaceFor(amount); 162 if (is_full) Grow(capacity_ * 2 + amount); 163 VIXL_ASSERT(has_grown != NULL); 164 *has_grown = is_full; 165 } EnsureSpaceFor(size_t amount)166 void EnsureSpaceFor(size_t amount) { 167 bool placeholder; 168 EnsureSpaceFor(amount, &placeholder); 169 } 170 171 private: 172 // Backing store of the buffer. 173 byte* buffer_; 174 // If true the backing store is allocated and deallocated by the buffer. The 175 // backing store can then grow on demand. If false the backing store is 176 // provided by the user and cannot be resized internally. 177 bool managed_; 178 // Pointer to the next location to be written. 179 byte* cursor_; 180 // True if there has been any write since the buffer was created or cleaned. 181 bool dirty_; 182 // Capacity in bytes of the backing store. 183 size_t capacity_; 184 }; 185 186 } // namespace vixl 187 188 #endif // VIXL_CODE_BUFFER_H 189