Georgia Kouveli | 8b57c86 | 2017-03-02 15:18:58 +0000 | [diff] [blame] | 1 | // Copyright 2017, VIXL authors |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 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 | |
Pierre Langlois | 78973f2 | 2016-08-10 14:35:56 +0100 | [diff] [blame] | 30 | #include <cstring> |
Alexandre Rames | b68bacb | 2016-05-24 08:56:23 +0100 | [diff] [blame] | 31 | |
Alexandre Rames | 1f9074d | 2016-05-23 15:50:01 +0100 | [diff] [blame] | 32 | #include "globals-vixl.h" |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 33 | #include "utils-vixl.h" |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 34 | |
| 35 | namespace vixl { |
| 36 | |
| 37 | class CodeBuffer { |
| 38 | public: |
Alexandre Rames | f2f550c | 2016-07-01 14:52:14 +0100 | [diff] [blame] | 39 | static const size_t kDefaultCapacity = 4 * KBytes; |
| 40 | |
| 41 | explicit CodeBuffer(size_t capacity = kDefaultCapacity); |
Alexandre Rames | 919e3fe | 2016-10-14 09:07:54 +0100 | [diff] [blame] | 42 | CodeBuffer(byte* buffer, size_t capacity); |
Pierre Langlois | a5b3cef | 2019-01-28 11:30:38 +0000 | [diff] [blame] | 43 | ~CodeBuffer() VIXL_NEGATIVE_TESTING_ALLOW_EXCEPTION; |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 44 | |
| 45 | void Reset(); |
| 46 | |
Jacob Bramley | cff5a2e | 2019-03-15 09:34:56 +0000 | [diff] [blame] | 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. |
Alex Gilday | 31dd2ae | 2016-07-05 16:34:41 +0100 | [diff] [blame] | 51 | void SetExecutable(); |
| 52 | void SetWritable(); |
| 53 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 54 | ptrdiff_t GetOffsetFrom(ptrdiff_t offset) const { |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 55 | ptrdiff_t cursor_offset = cursor_ - buffer_; |
| 56 | VIXL_ASSERT((offset >= 0) && (offset <= cursor_offset)); |
| 57 | return cursor_offset - offset; |
| 58 | } |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 59 | VIXL_DEPRECATED("GetOffsetFrom", |
| 60 | ptrdiff_t OffsetFrom(ptrdiff_t offset) const) { |
| 61 | return GetOffsetFrom(offset); |
| 62 | } |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 63 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 64 | ptrdiff_t GetCursorOffset() const { return GetOffsetFrom(0); } |
| 65 | VIXL_DEPRECATED("GetCursorOffset", ptrdiff_t CursorOffset() const) { |
| 66 | return GetCursorOffset(); |
| 67 | } |
| 68 | |
| 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 | } |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 74 | |
| 75 | template <typename T> |
| 76 | T GetOffsetAddress(ptrdiff_t offset) const { |
Alexandre Rames | 6a049f9 | 2016-09-21 14:55:05 +0100 | [diff] [blame] | 77 | VIXL_STATIC_ASSERT(sizeof(T) >= sizeof(uintptr_t)); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 78 | VIXL_ASSERT((offset >= 0) && (offset <= (cursor_ - buffer_))); |
| 79 | return reinterpret_cast<T>(buffer_ + offset); |
| 80 | } |
| 81 | |
Jacob Bramley | ab70ab9 | 2018-08-30 16:11:32 +0100 | [diff] [blame] | 82 | // Return the address of the start or end of the emitted code. |
Alexandre Rames | 6a049f9 | 2016-09-21 14:55:05 +0100 | [diff] [blame] | 83 | template <typename T> |
| 84 | T GetStartAddress() const { |
| 85 | VIXL_STATIC_ASSERT(sizeof(T) >= sizeof(uintptr_t)); |
| 86 | return GetOffsetAddress<T>(0); |
| 87 | } |
| 88 | template <typename T> |
| 89 | T GetEndAddress() const { |
| 90 | VIXL_STATIC_ASSERT(sizeof(T) >= sizeof(uintptr_t)); |
Jacob Bramley | ab70ab9 | 2018-08-30 16:11:32 +0100 | [diff] [blame] | 91 | return GetOffsetAddress<T>(GetSizeInBytes()); |
Alexandre Rames | 6a049f9 | 2016-09-21 14:55:05 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 94 | size_t GetRemainingBytes() const { |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 95 | VIXL_ASSERT((cursor_ >= buffer_) && (cursor_ <= (buffer_ + capacity_))); |
| 96 | return (buffer_ + capacity_) - cursor_; |
| 97 | } |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 98 | VIXL_DEPRECATED("GetRemainingBytes", size_t RemainingBytes() const) { |
| 99 | return GetRemainingBytes(); |
| 100 | } |
| 101 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 102 | size_t GetSizeInBytes() const { |
| 103 | VIXL_ASSERT((cursor_ >= buffer_) && (cursor_ <= (buffer_ + capacity_))); |
| 104 | return cursor_ - buffer_; |
| 105 | } |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 106 | |
| 107 | // A code buffer can emit: |
Vincent Belliard | 5d85018 | 2017-09-25 13:04:30 -0700 | [diff] [blame] | 108 | // * 8, 16, 32 or 64-bit data: constant. |
| 109 | // * 16 or 32-bit data: instruction. |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 110 | // * string: debug info. |
Vincent Belliard | 5d85018 | 2017-09-25 13:04:30 -0700 | [diff] [blame] | 111 | void Emit8(uint8_t data) { Emit(data); } |
| 112 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 113 | void Emit16(uint16_t data) { Emit(data); } |
| 114 | |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 115 | void Emit32(uint32_t data) { Emit(data); } |
| 116 | |
| 117 | void Emit64(uint64_t data) { Emit(data); } |
| 118 | |
| 119 | void EmitString(const char* string); |
| 120 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 121 | void EmitData(const void* data, size_t size); |
| 122 | |
Alexandre Rames | 86eb887 | 2016-08-15 15:40:22 +0100 | [diff] [blame] | 123 | template <typename T> |
| 124 | void Emit(T value) { |
| 125 | VIXL_ASSERT(HasSpaceFor(sizeof(value))); |
| 126 | dirty_ = true; |
mmc28a | 64c25fe | 2023-05-09 14:32:48 +0100 | [diff] [blame] | 127 | byte* c = cursor_; |
| 128 | memcpy(c, &value, sizeof(value)); |
| 129 | cursor_ = c + sizeof(value); |
Alexandre Rames | 86eb887 | 2016-08-15 15:40:22 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Vincent Belliard | 3e1b899 | 2016-07-13 16:02:19 -0700 | [diff] [blame] | 132 | void UpdateData(size_t offset, const void* data, size_t size); |
| 133 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 134 | // Align to 32bit. |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 135 | void Align(); |
| 136 | |
Georgia Kouveli | 8b57c86 | 2017-03-02 15:18:58 +0000 | [diff] [blame] | 137 | // Ensure there is enough space for and emit 'n' zero bytes. |
| 138 | void EmitZeroedBytes(int n); |
| 139 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 140 | bool Is16bitAligned() const { return IsAligned<2>(cursor_); } |
| 141 | |
| 142 | bool Is32bitAligned() const { return IsAligned<4>(cursor_); } |
| 143 | |
| 144 | size_t GetCapacity() const { return capacity_; } |
| 145 | VIXL_DEPRECATED("GetCapacity", size_t capacity() const) { |
| 146 | return GetCapacity(); |
| 147 | } |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 148 | |
| 149 | bool IsManaged() const { return managed_; } |
| 150 | |
| 151 | void Grow(size_t new_capacity); |
| 152 | |
| 153 | bool IsDirty() const { return dirty_; } |
| 154 | |
| 155 | void SetClean() { dirty_ = false; } |
| 156 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 157 | bool HasSpaceFor(size_t amount) const { |
| 158 | return GetRemainingBytes() >= amount; |
| 159 | } |
| 160 | |
| 161 | void EnsureSpaceFor(size_t amount, bool* has_grown) { |
| 162 | bool is_full = !HasSpaceFor(amount); |
| 163 | if (is_full) Grow(capacity_ * 2 + amount); |
| 164 | VIXL_ASSERT(has_grown != NULL); |
| 165 | *has_grown = is_full; |
| 166 | } |
| 167 | void EnsureSpaceFor(size_t amount) { |
Martyn Capewell | d42989c | 2020-11-10 14:32:56 +0000 | [diff] [blame] | 168 | bool placeholder; |
| 169 | EnsureSpaceFor(amount, &placeholder); |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 170 | } |
| 171 | |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 172 | private: |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 173 | // Backing store of the buffer. |
| 174 | byte* buffer_; |
| 175 | // If true the backing store is allocated and deallocated by the buffer. The |
| 176 | // backing store can then grow on demand. If false the backing store is |
| 177 | // provided by the user and cannot be resized internally. |
| 178 | bool managed_; |
| 179 | // Pointer to the next location to be written. |
| 180 | byte* cursor_; |
| 181 | // True if there has been any write since the buffer was created or cleaned. |
| 182 | bool dirty_; |
| 183 | // Capacity in bytes of the backing store. |
| 184 | size_t capacity_; |
| 185 | }; |
| 186 | |
| 187 | } // namespace vixl |
| 188 | |
| 189 | #endif // VIXL_CODE_BUFFER_H |