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