blob: cb9031fefc4b3f6daddc83abdd5d0dbd3d835966 [file] [log] [blame]
Georgia Kouveli8b57c862017-03-02 15:18:58 +00001// Copyright 2017, VIXL authors
armvixlc68cb642014-09-25 18:49:30 +01002// 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 Langlois78973f22016-08-10 14:35:56 +010030#include <cstring>
Alexandre Ramesb68bacb2016-05-24 08:56:23 +010031
Alexandre Rames1f9074d2016-05-23 15:50:01 +010032#include "globals-vixl.h"
Pierre Langlois88c46b82016-06-02 18:15:32 +010033#include "utils-vixl.h"
armvixlc68cb642014-09-25 18:49:30 +010034
35namespace vixl {
36
37class CodeBuffer {
38 public:
Alexandre Ramesf2f550c2016-07-01 14:52:14 +010039 static const size_t kDefaultCapacity = 4 * KBytes;
40
41 explicit CodeBuffer(size_t capacity = kDefaultCapacity);
Alexandre Rames919e3fe2016-10-14 09:07:54 +010042 CodeBuffer(byte* buffer, size_t capacity);
Pierre Langloisa5b3cef2019-01-28 11:30:38 +000043 ~CodeBuffer() VIXL_NEGATIVE_TESTING_ALLOW_EXCEPTION;
armvixlc68cb642014-09-25 18:49:30 +010044
45 void Reset();
46
Jacob Bramleycff5a2e2019-03-15 09:34:56 +000047 // 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 Gilday31dd2ae2016-07-05 16:34:41 +010051 void SetExecutable();
52 void SetWritable();
53
Pierre Langlois88c46b82016-06-02 18:15:32 +010054 ptrdiff_t GetOffsetFrom(ptrdiff_t offset) const {
armvixlc68cb642014-09-25 18:49:30 +010055 ptrdiff_t cursor_offset = cursor_ - buffer_;
56 VIXL_ASSERT((offset >= 0) && (offset <= cursor_offset));
57 return cursor_offset - offset;
58 }
Pierre Langlois88c46b82016-06-02 18:15:32 +010059 VIXL_DEPRECATED("GetOffsetFrom",
60 ptrdiff_t OffsetFrom(ptrdiff_t offset) const) {
61 return GetOffsetFrom(offset);
62 }
armvixlc68cb642014-09-25 18:49:30 +010063
Pierre Langlois88c46b82016-06-02 18:15:32 +010064 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 }
armvixlc68cb642014-09-25 18:49:30 +010074
75 template <typename T>
76 T GetOffsetAddress(ptrdiff_t offset) const {
Alexandre Rames6a049f92016-09-21 14:55:05 +010077 VIXL_STATIC_ASSERT(sizeof(T) >= sizeof(uintptr_t));
armvixlc68cb642014-09-25 18:49:30 +010078 VIXL_ASSERT((offset >= 0) && (offset <= (cursor_ - buffer_)));
79 return reinterpret_cast<T>(buffer_ + offset);
80 }
81
Jacob Bramleyab70ab92018-08-30 16:11:32 +010082 // Return the address of the start or end of the emitted code.
Alexandre Rames6a049f92016-09-21 14:55:05 +010083 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 Bramleyab70ab92018-08-30 16:11:32 +010091 return GetOffsetAddress<T>(GetSizeInBytes());
Alexandre Rames6a049f92016-09-21 14:55:05 +010092 }
93
Pierre Langlois88c46b82016-06-02 18:15:32 +010094 size_t GetRemainingBytes() const {
armvixlc68cb642014-09-25 18:49:30 +010095 VIXL_ASSERT((cursor_ >= buffer_) && (cursor_ <= (buffer_ + capacity_)));
96 return (buffer_ + capacity_) - cursor_;
97 }
Pierre Langlois88c46b82016-06-02 18:15:32 +010098 VIXL_DEPRECATED("GetRemainingBytes", size_t RemainingBytes() const) {
99 return GetRemainingBytes();
100 }
101
Pierre Langlois88c46b82016-06-02 18:15:32 +0100102 size_t GetSizeInBytes() const {
103 VIXL_ASSERT((cursor_ >= buffer_) && (cursor_ <= (buffer_ + capacity_)));
104 return cursor_ - buffer_;
105 }
armvixlc68cb642014-09-25 18:49:30 +0100106
107 // A code buffer can emit:
Vincent Belliard5d850182017-09-25 13:04:30 -0700108 // * 8, 16, 32 or 64-bit data: constant.
109 // * 16 or 32-bit data: instruction.
armvixlc68cb642014-09-25 18:49:30 +0100110 // * string: debug info.
Vincent Belliard5d850182017-09-25 13:04:30 -0700111 void Emit8(uint8_t data) { Emit(data); }
112
Pierre Langlois88c46b82016-06-02 18:15:32 +0100113 void Emit16(uint16_t data) { Emit(data); }
114
armvixlc68cb642014-09-25 18:49:30 +0100115 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 Langlois88c46b82016-06-02 18:15:32 +0100121 void EmitData(const void* data, size_t size);
122
Alexandre Rames86eb8872016-08-15 15:40:22 +0100123 template <typename T>
124 void Emit(T value) {
125 VIXL_ASSERT(HasSpaceFor(sizeof(value)));
126 dirty_ = true;
mmc28a64c25fe2023-05-09 14:32:48 +0100127 byte* c = cursor_;
128 memcpy(c, &value, sizeof(value));
129 cursor_ = c + sizeof(value);
Alexandre Rames86eb8872016-08-15 15:40:22 +0100130 }
131
Vincent Belliard3e1b8992016-07-13 16:02:19 -0700132 void UpdateData(size_t offset, const void* data, size_t size);
133
Pierre Langlois88c46b82016-06-02 18:15:32 +0100134 // Align to 32bit.
armvixlc68cb642014-09-25 18:49:30 +0100135 void Align();
136
Georgia Kouveli8b57c862017-03-02 15:18:58 +0000137 // Ensure there is enough space for and emit 'n' zero bytes.
138 void EmitZeroedBytes(int n);
139
Pierre Langlois88c46b82016-06-02 18:15:32 +0100140 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 }
armvixlc68cb642014-09-25 18:49:30 +0100148
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 Langlois88c46b82016-06-02 18:15:32 +0100157 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 Capewelld42989c2020-11-10 14:32:56 +0000168 bool placeholder;
169 EnsureSpaceFor(amount, &placeholder);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100170 }
171
armvixlc68cb642014-09-25 18:49:30 +0100172 private:
armvixlc68cb642014-09-25 18:49:30 +0100173 // 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