blob: d917f08c402f6870ba3816d96ca55a42d0365422 [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"
armvixlc68cb642014-09-25 18:49:30 +010033
34namespace vixl {
35
36class CodeBuffer {
37 public:
38 explicit CodeBuffer(size_t capacity = 4 * KBytes);
39 CodeBuffer(void* buffer, size_t capacity);
40 ~CodeBuffer();
41
42 void Reset();
43
44 ptrdiff_t OffsetFrom(ptrdiff_t offset) const {
45 ptrdiff_t cursor_offset = cursor_ - buffer_;
46 VIXL_ASSERT((offset >= 0) && (offset <= cursor_offset));
47 return cursor_offset - offset;
48 }
49
armvixl0f35e362016-05-10 13:57:58 +010050 ptrdiff_t CursorOffset() const { return OffsetFrom(0); }
armvixlc68cb642014-09-25 18:49:30 +010051
52 template <typename T>
53 T GetOffsetAddress(ptrdiff_t offset) const {
54 VIXL_ASSERT((offset >= 0) && (offset <= (cursor_ - buffer_)));
55 return reinterpret_cast<T>(buffer_ + offset);
56 }
57
58 size_t RemainingBytes() const {
59 VIXL_ASSERT((cursor_ >= buffer_) && (cursor_ <= (buffer_ + capacity_)));
60 return (buffer_ + capacity_) - cursor_;
61 }
62
63 // A code buffer can emit:
64 // * 32-bit data: instruction and constant.
65 // * 64-bit data: constant.
66 // * string: debug info.
67 void Emit32(uint32_t data) { Emit(data); }
68
69 void Emit64(uint64_t data) { Emit(data); }
70
71 void EmitString(const char* string);
72
73 // Align to kInstructionSize.
74 void Align();
75
76 size_t capacity() const { return capacity_; }
77
78 bool IsManaged() const { return managed_; }
79
80 void Grow(size_t new_capacity);
81
82 bool IsDirty() const { return dirty_; }
83
84 void SetClean() { dirty_ = false; }
85
86 private:
87 template <typename T>
88 void Emit(T value) {
89 VIXL_ASSERT(RemainingBytes() >= sizeof(value));
90 dirty_ = true;
91 memcpy(cursor_, &value, sizeof(value));
92 cursor_ += sizeof(value);
93 }
94
95 // Backing store of the buffer.
96 byte* buffer_;
97 // If true the backing store is allocated and deallocated by the buffer. The
98 // backing store can then grow on demand. If false the backing store is
99 // provided by the user and cannot be resized internally.
100 bool managed_;
101 // Pointer to the next location to be written.
102 byte* cursor_;
103 // True if there has been any write since the buffer was created or cleaned.
104 bool dirty_;
105 // Capacity in bytes of the backing store.
106 size_t capacity_;
107};
108
109} // namespace vixl
110
111#endif // VIXL_CODE_BUFFER_H