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();
44 
45   void Reset();
46 
47 #ifdef VIXL_CODE_BUFFER_MMAP
48   void SetExecutable();
49   void SetWritable();
50 #else
51   // These require page-aligned memory blocks, which we can only guarantee with
52   // mmap.
SetExecutable()53   VIXL_NO_RETURN_IN_DEBUG_MODE void SetExecutable() { VIXL_UNIMPLEMENTED(); }
SetWritable()54   VIXL_NO_RETURN_IN_DEBUG_MODE void SetWritable() { VIXL_UNIMPLEMENTED(); }
55 #endif
56 
GetOffsetFrom(ptrdiff_t offset)57   ptrdiff_t GetOffsetFrom(ptrdiff_t offset) const {
58     ptrdiff_t cursor_offset = cursor_ - buffer_;
59     VIXL_ASSERT((offset >= 0) && (offset <= cursor_offset));
60     return cursor_offset - offset;
61   }
62   VIXL_DEPRECATED("GetOffsetFrom",
63                   ptrdiff_t OffsetFrom(ptrdiff_t offset) const) {
64     return GetOffsetFrom(offset);
65   }
66 
GetCursorOffset()67   ptrdiff_t GetCursorOffset() const { return GetOffsetFrom(0); }
68   VIXL_DEPRECATED("GetCursorOffset", ptrdiff_t CursorOffset() const) {
69     return GetCursorOffset();
70   }
71 
Rewind(ptrdiff_t offset)72   void Rewind(ptrdiff_t offset) {
73     byte* rewound_cursor = buffer_ + offset;
74     VIXL_ASSERT((buffer_ <= rewound_cursor) && (rewound_cursor <= cursor_));
75     cursor_ = rewound_cursor;
76   }
77 
78   template <typename T>
GetOffsetAddress(ptrdiff_t offset)79   T GetOffsetAddress(ptrdiff_t offset) const {
80     VIXL_STATIC_ASSERT(sizeof(T) >= sizeof(uintptr_t));
81     VIXL_ASSERT((offset >= 0) && (offset <= (cursor_ - buffer_)));
82     return reinterpret_cast<T>(buffer_ + offset);
83   }
84 
85   // Return the address of the start or end of the buffer.
86   template <typename T>
GetStartAddress()87   T GetStartAddress() const {
88     VIXL_STATIC_ASSERT(sizeof(T) >= sizeof(uintptr_t));
89     return GetOffsetAddress<T>(0);
90   }
91   template <typename T>
GetEndAddress()92   T GetEndAddress() const {
93     VIXL_STATIC_ASSERT(sizeof(T) >= sizeof(uintptr_t));
94     return GetOffsetAddress<T>(GetCapacity());
95   }
96 
GetRemainingBytes()97   size_t GetRemainingBytes() const {
98     VIXL_ASSERT((cursor_ >= buffer_) && (cursor_ <= (buffer_ + capacity_)));
99     return (buffer_ + capacity_) - cursor_;
100   }
101   VIXL_DEPRECATED("GetRemainingBytes", size_t RemainingBytes() const) {
102     return GetRemainingBytes();
103   }
104 
GetSizeInBytes()105   size_t GetSizeInBytes() const {
106     VIXL_ASSERT((cursor_ >= buffer_) && (cursor_ <= (buffer_ + capacity_)));
107     return cursor_ - buffer_;
108   }
109 
110   // A code buffer can emit:
111   //  * 8, 16, 32 or 64-bit data: constant.
112   //  * 16 or 32-bit data: instruction.
113   //  * string: debug info.
Emit8(uint8_t data)114   void Emit8(uint8_t data) { Emit(data); }
115 
Emit16(uint16_t data)116   void Emit16(uint16_t data) { Emit(data); }
117 
Emit32(uint32_t data)118   void Emit32(uint32_t data) { Emit(data); }
119 
Emit64(uint64_t data)120   void Emit64(uint64_t data) { Emit(data); }
121 
122   void EmitString(const char* string);
123 
124   void EmitData(const void* data, size_t size);
125 
126   template <typename T>
Emit(T value)127   void Emit(T value) {
128     VIXL_ASSERT(HasSpaceFor(sizeof(value)));
129     dirty_ = true;
130     memcpy(cursor_, &value, sizeof(value));
131     cursor_ += sizeof(value);
132   }
133 
134   void UpdateData(size_t offset, const void* data, size_t size);
135 
136   // Align to 32bit.
137   void Align();
138 
139   // Ensure there is enough space for and emit 'n' zero bytes.
140   void EmitZeroedBytes(int n);
141 
Is16bitAligned()142   bool Is16bitAligned() const { return IsAligned<2>(cursor_); }
143 
Is32bitAligned()144   bool Is32bitAligned() const { return IsAligned<4>(cursor_); }
145 
GetCapacity()146   size_t GetCapacity() const { return capacity_; }
147   VIXL_DEPRECATED("GetCapacity", size_t capacity() const) {
148     return GetCapacity();
149   }
150 
IsManaged()151   bool IsManaged() const { return managed_; }
152 
153   void Grow(size_t new_capacity);
154 
IsDirty()155   bool IsDirty() const { return dirty_; }
156 
SetClean()157   void SetClean() { dirty_ = false; }
158 
HasSpaceFor(size_t amount)159   bool HasSpaceFor(size_t amount) const {
160     return GetRemainingBytes() >= amount;
161   }
162 
EnsureSpaceFor(size_t amount,bool * has_grown)163   void EnsureSpaceFor(size_t amount, bool* has_grown) {
164     bool is_full = !HasSpaceFor(amount);
165     if (is_full) Grow(capacity_ * 2 + amount);
166     VIXL_ASSERT(has_grown != NULL);
167     *has_grown = is_full;
168   }
EnsureSpaceFor(size_t amount)169   void EnsureSpaceFor(size_t amount) {
170     bool dummy;
171     EnsureSpaceFor(amount, &dummy);
172   }
173 
174  private:
175   // Backing store of the buffer.
176   byte* buffer_;
177   // If true the backing store is allocated and deallocated by the buffer. The
178   // backing store can then grow on demand. If false the backing store is
179   // provided by the user and cannot be resized internally.
180   bool managed_;
181   // Pointer to the next location to be written.
182   byte* cursor_;
183   // True if there has been any write since the buffer was created or cleaned.
184   bool dirty_;
185   // Capacity in bytes of the backing store.
186   size_t capacity_;
187 };
188 
189 }  // namespace vixl
190 
191 #endif  // VIXL_CODE_BUFFER_H
192