blob: d2c046c42f1b95d1bcc3d0a7b6d6cb71dce39888 [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
Alexandre Rames1f9074d2016-05-23 15:50:01 +010027#include "code-buffer-vixl.h"
28#include "utils-vixl.h"
armvixlc68cb642014-09-25 18:49:30 +010029
30namespace vixl {
31
32
33CodeBuffer::CodeBuffer(size_t capacity) : managed_(true), capacity_(capacity) {
34 VIXL_CHECK(capacity_ != 0);
35 buffer_ = reinterpret_cast<byte*>(malloc(capacity_));
36 VIXL_CHECK(buffer_ != NULL);
37 // A64 instructions must be word aligned, we assert the default allocator
38 // always returns word align memory.
39 VIXL_ASSERT(IsWordAligned(buffer_));
40
41 cursor_ = buffer_;
42 dirty_ = false;
43}
44
45
46CodeBuffer::CodeBuffer(void* buffer, size_t capacity)
47 : buffer_(reinterpret_cast<byte*>(buffer)),
48 managed_(false),
49 cursor_(reinterpret_cast<byte*>(buffer)),
50 dirty_(false),
51 capacity_(capacity) {
52 VIXL_ASSERT(buffer_ != NULL);
53}
54
55
56CodeBuffer::~CodeBuffer() {
57 VIXL_ASSERT(!IsDirty());
58 if (managed_) {
59 free(buffer_);
60 }
61}
62
63
64void CodeBuffer::EmitString(const char* string) {
65 VIXL_ASSERT(RemainingBytes() > strlen(string));
66 char* dst = reinterpret_cast<char*>(cursor_);
67 dirty_ = true;
68 char* null_char = stpcpy(dst, string);
69 cursor_ = reinterpret_cast<byte*>(null_char) + 1;
70}
71
72
73void CodeBuffer::Align() {
74 byte* end = AlignUp(cursor_, 4);
75 VIXL_ASSERT(end >= cursor_);
76 const size_t padding_size = end - cursor_;
77 VIXL_ASSERT(RemainingBytes() >= padding_size);
78 VIXL_ASSERT(padding_size <= 4);
armvixl0f35e362016-05-10 13:57:58 +010079 const byte padding[] = {0, 0, 0, 0};
armvixlc68cb642014-09-25 18:49:30 +010080 dirty_ = true;
81 memcpy(cursor_, padding, padding_size);
82 cursor_ = end;
83}
84
85
86void CodeBuffer::Reset() {
armvixl330dc712014-11-25 10:38:32 +000087#ifdef VIXL_DEBUG
armvixlc68cb642014-09-25 18:49:30 +010088 if (managed_) {
89 // TODO(all): Consider allowing for custom default values, e.g. HLT.
90 memset(buffer_, 0, capacity_);
91 }
92#endif
93 cursor_ = buffer_;
94 SetClean();
95}
96
97
98void CodeBuffer::Grow(size_t new_capacity) {
99 VIXL_ASSERT(managed_);
100 VIXL_ASSERT(new_capacity > capacity_);
101 size_t size = CursorOffset();
102 buffer_ = static_cast<byte*>(realloc(buffer_, new_capacity));
103 VIXL_CHECK(buffer_ != NULL);
104
105 cursor_ = buffer_ + size;
106 capacity_ = new_capacity;
107}
108
109
110} // namespace vixl