armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 1 | // Copyright 2014, ARM Limited |
| 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 <string.h> |
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); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 42 | CodeBuffer(void* buffer, size_t capacity); |
| 43 | ~CodeBuffer(); |
| 44 | |
| 45 | void Reset(); |
| 46 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 47 | ptrdiff_t GetOffsetFrom(ptrdiff_t offset) const { |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 48 | ptrdiff_t cursor_offset = cursor_ - buffer_; |
| 49 | VIXL_ASSERT((offset >= 0) && (offset <= cursor_offset)); |
| 50 | return cursor_offset - offset; |
| 51 | } |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 52 | VIXL_DEPRECATED("GetOffsetFrom", |
| 53 | ptrdiff_t OffsetFrom(ptrdiff_t offset) const) { |
| 54 | return GetOffsetFrom(offset); |
| 55 | } |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 56 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 57 | ptrdiff_t GetCursorOffset() const { return GetOffsetFrom(0); } |
| 58 | VIXL_DEPRECATED("GetCursorOffset", ptrdiff_t CursorOffset() const) { |
| 59 | return GetCursorOffset(); |
| 60 | } |
| 61 | |
| 62 | void Rewind(ptrdiff_t offset) { |
| 63 | byte* rewound_cursor = buffer_ + offset; |
| 64 | VIXL_ASSERT((buffer_ <= rewound_cursor) && (rewound_cursor <= cursor_)); |
| 65 | cursor_ = rewound_cursor; |
| 66 | } |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 67 | |
| 68 | template <typename T> |
| 69 | T GetOffsetAddress(ptrdiff_t offset) const { |
| 70 | VIXL_ASSERT((offset >= 0) && (offset <= (cursor_ - buffer_))); |
| 71 | return reinterpret_cast<T>(buffer_ + offset); |
| 72 | } |
| 73 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 74 | size_t GetRemainingBytes() const { |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 75 | VIXL_ASSERT((cursor_ >= buffer_) && (cursor_ <= (buffer_ + capacity_))); |
| 76 | return (buffer_ + capacity_) - cursor_; |
| 77 | } |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 78 | VIXL_DEPRECATED("GetRemainingBytes", size_t RemainingBytes() const) { |
| 79 | return GetRemainingBytes(); |
| 80 | } |
| 81 | |
| 82 | byte* GetBuffer() const { return GetOffsetAddress<byte*>(0); } |
| 83 | |
| 84 | size_t GetSizeInBytes() const { |
| 85 | VIXL_ASSERT((cursor_ >= buffer_) && (cursor_ <= (buffer_ + capacity_))); |
| 86 | return cursor_ - buffer_; |
| 87 | } |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 88 | |
| 89 | // A code buffer can emit: |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 90 | // * 16 or 32-bit data: instruction and constant. |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 91 | // * 64-bit data: constant. |
| 92 | // * string: debug info. |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 93 | void Emit16(uint16_t data) { Emit(data); } |
| 94 | |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 95 | void Emit32(uint32_t data) { Emit(data); } |
| 96 | |
| 97 | void Emit64(uint64_t data) { Emit(data); } |
| 98 | |
| 99 | void EmitString(const char* string); |
| 100 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 101 | void EmitData(const void* data, size_t size); |
| 102 | |
| 103 | // Align to 32bit. |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 104 | void Align(); |
| 105 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 106 | bool Is16bitAligned() const { return IsAligned<2>(cursor_); } |
| 107 | |
| 108 | bool Is32bitAligned() const { return IsAligned<4>(cursor_); } |
| 109 | |
| 110 | size_t GetCapacity() const { return capacity_; } |
| 111 | VIXL_DEPRECATED("GetCapacity", size_t capacity() const) { |
| 112 | return GetCapacity(); |
| 113 | } |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 114 | |
| 115 | bool IsManaged() const { return managed_; } |
| 116 | |
| 117 | void Grow(size_t new_capacity); |
| 118 | |
| 119 | bool IsDirty() const { return dirty_; } |
| 120 | |
| 121 | void SetClean() { dirty_ = false; } |
| 122 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 123 | bool HasSpaceFor(size_t amount) const { |
| 124 | return GetRemainingBytes() >= amount; |
| 125 | } |
| 126 | |
| 127 | void EnsureSpaceFor(size_t amount, bool* has_grown) { |
| 128 | bool is_full = !HasSpaceFor(amount); |
| 129 | if (is_full) Grow(capacity_ * 2 + amount); |
| 130 | VIXL_ASSERT(has_grown != NULL); |
| 131 | *has_grown = is_full; |
| 132 | } |
| 133 | void EnsureSpaceFor(size_t amount) { |
| 134 | bool dummy; |
| 135 | EnsureSpaceFor(amount, &dummy); |
| 136 | } |
| 137 | |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 138 | private: |
| 139 | template <typename T> |
| 140 | void Emit(T value) { |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 141 | VIXL_ASSERT(HasSpaceFor(sizeof(value))); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 142 | dirty_ = true; |
| 143 | memcpy(cursor_, &value, sizeof(value)); |
| 144 | cursor_ += sizeof(value); |
| 145 | } |
| 146 | |
| 147 | // Backing store of the buffer. |
| 148 | byte* buffer_; |
| 149 | // If true the backing store is allocated and deallocated by the buffer. The |
| 150 | // backing store can then grow on demand. If false the backing store is |
| 151 | // provided by the user and cannot be resized internally. |
| 152 | bool managed_; |
| 153 | // Pointer to the next location to be written. |
| 154 | byte* cursor_; |
| 155 | // True if there has been any write since the buffer was created or cleaned. |
| 156 | bool dirty_; |
| 157 | // Capacity in bytes of the backing store. |
| 158 | size_t capacity_; |
| 159 | }; |
| 160 | |
| 161 | } // namespace vixl |
| 162 | |
| 163 | #endif // VIXL_CODE_BUFFER_H |