blob: b9fc1ce4e36e64879ecf1e06dc65b80be6b810bd [file] [log] [blame]
armvixlc68cb642014-09-25 18:49:30 +01001// 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 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);
armvixlc68cb642014-09-25 18:49:30 +010042 CodeBuffer(void* buffer, size_t capacity);
43 ~CodeBuffer();
44
45 void Reset();
46
Pierre Langlois88c46b82016-06-02 18:15:32 +010047 ptrdiff_t GetOffsetFrom(ptrdiff_t offset) const {
armvixlc68cb642014-09-25 18:49:30 +010048 ptrdiff_t cursor_offset = cursor_ - buffer_;
49 VIXL_ASSERT((offset >= 0) && (offset <= cursor_offset));
50 return cursor_offset - offset;
51 }
Pierre Langlois88c46b82016-06-02 18:15:32 +010052 VIXL_DEPRECATED("GetOffsetFrom",
53 ptrdiff_t OffsetFrom(ptrdiff_t offset) const) {
54 return GetOffsetFrom(offset);
55 }
armvixlc68cb642014-09-25 18:49:30 +010056
Pierre Langlois88c46b82016-06-02 18:15:32 +010057 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 }
armvixlc68cb642014-09-25 18:49:30 +010067
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 Langlois88c46b82016-06-02 18:15:32 +010074 size_t GetRemainingBytes() const {
armvixlc68cb642014-09-25 18:49:30 +010075 VIXL_ASSERT((cursor_ >= buffer_) && (cursor_ <= (buffer_ + capacity_)));
76 return (buffer_ + capacity_) - cursor_;
77 }
Pierre Langlois88c46b82016-06-02 18:15:32 +010078 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 }
armvixlc68cb642014-09-25 18:49:30 +010088
89 // A code buffer can emit:
Pierre Langlois88c46b82016-06-02 18:15:32 +010090 // * 16 or 32-bit data: instruction and constant.
armvixlc68cb642014-09-25 18:49:30 +010091 // * 64-bit data: constant.
92 // * string: debug info.
Pierre Langlois88c46b82016-06-02 18:15:32 +010093 void Emit16(uint16_t data) { Emit(data); }
94
armvixlc68cb642014-09-25 18:49:30 +010095 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 Langlois88c46b82016-06-02 18:15:32 +0100101 void EmitData(const void* data, size_t size);
102
103 // Align to 32bit.
armvixlc68cb642014-09-25 18:49:30 +0100104 void Align();
105
Pierre Langlois88c46b82016-06-02 18:15:32 +0100106 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 }
armvixlc68cb642014-09-25 18:49:30 +0100114
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 Langlois88c46b82016-06-02 18:15:32 +0100123 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
armvixlc68cb642014-09-25 18:49:30 +0100138 private:
139 template <typename T>
140 void Emit(T value) {
Pierre Langlois88c46b82016-06-02 18:15:32 +0100141 VIXL_ASSERT(HasSpaceFor(sizeof(value)));
armvixlc68cb642014-09-25 18:49:30 +0100142 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