blob: d0a8aa4f24ceb3a65004220311cfe66b6b9c6e3e [file] [log] [blame]
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#include <algorithm>
4#include <vector>
5#include "src/assembler.h"
6#include "src/globals.h"
7#include "src/memory_region.h"
8
Carl Shapiro6b6b5f02011-06-21 15:05:09 -07009namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070010
11static byte* NewContents(size_t capacity) {
12 byte* result = new byte[capacity];
13#if defined(DEBUG)
14 // Initialize the buffer with kBreakPointInstruction to force a break
15 // point if we ever execute an uninitialized part of the code buffer.
16 Assembler::InitializeMemoryWithBreakpoints(result, capacity);
17#endif
18 return result;
19}
20
21
22#if defined(DEBUG)
23AssemblerBuffer::EnsureCapacity::EnsureCapacity(AssemblerBuffer* buffer) {
24 if (buffer->cursor() >= buffer->limit()) buffer->ExtendCapacity();
25 // In debug mode, we save the assembler buffer along with the gap
26 // size before we start emitting to the buffer. This allows us to
27 // check that any single generated instruction doesn't overflow the
28 // limit implied by the minimum gap size.
29 buffer_ = buffer;
30 gap_ = ComputeGap();
31 // Make sure that extending the capacity leaves a big enough gap
32 // for any kind of instruction.
33 CHECK(gap_ >= kMinimumGap);
34 // Mark the buffer as having ensured the capacity.
35 CHECK(!buffer->HasEnsuredCapacity()); // Cannot nest.
36 buffer->has_ensured_capacity_ = true;
37}
38
39
40AssemblerBuffer::EnsureCapacity::~EnsureCapacity() {
41 // Unmark the buffer, so we cannot emit after this.
42 buffer_->has_ensured_capacity_ = false;
43 // Make sure the generated instruction doesn't take up more
44 // space than the minimum gap.
45 int delta = gap_ - ComputeGap();
46 CHECK(delta <= kMinimumGap);
47}
48#endif
49
50
51AssemblerBuffer::AssemblerBuffer() {
52 static const size_t kInitialBufferCapacity = 4 * KB;
53 contents_ = NewContents(kInitialBufferCapacity);
54 cursor_ = contents_;
55 limit_ = ComputeLimit(contents_, kInitialBufferCapacity);
56 fixup_ = NULL;
57#if defined(DEBUG)
58 has_ensured_capacity_ = false;
59 fixups_processed_ = false;
60#endif
61
62 // Verify internal state.
63 CHECK_EQ(Capacity(), kInitialBufferCapacity);
64 CHECK_EQ(Size(), 0);
65}
66
67
68AssemblerBuffer::~AssemblerBuffer() {
69}
70
71
72void AssemblerBuffer::ProcessFixups(const MemoryRegion& region) {
73 AssemblerFixup* fixup = fixup_;
74 while (fixup != NULL) {
75 fixup->Process(region, fixup->position());
76 fixup = fixup->previous();
77 }
78}
79
80
81void AssemblerBuffer::FinalizeInstructions(const MemoryRegion& instructions) {
82 // Copy the instructions from the buffer.
83 MemoryRegion from(reinterpret_cast<void*>(contents()), Size());
84 instructions.CopyFrom(0, from);
85
86 // Process fixups in the instructions.
87 ProcessFixups(instructions);
88#if defined(DEBUG)
89 fixups_processed_ = true;
90#endif
91}
92
93
94void AssemblerBuffer::ExtendCapacity() {
95 size_t old_size = Size();
96 size_t old_capacity = Capacity();
97 size_t new_capacity = std::min(old_capacity * 2, old_capacity + 1 * MB);
98
99 // Allocate the new data area and copy contents of the old one to it.
100 byte* new_contents = NewContents(new_capacity);
101 memmove(reinterpret_cast<void*>(new_contents),
102 reinterpret_cast<void*>(contents_),
103 old_size);
104
105 // Compute the relocation delta and switch to the new contents area.
106 ptrdiff_t delta = new_contents - contents_;
107 contents_ = new_contents;
108
109 // Update the cursor and recompute the limit.
110 cursor_ += delta;
111 limit_ = ComputeLimit(new_contents, new_capacity);
112
113 // Verify internal state.
114 CHECK_EQ(Capacity(), new_capacity);
115 CHECK_EQ(Size(), old_size);
116}
117
118
119#if 0
120// Shared macros are implemented here.
121void Assembler::Unimplemented(const char* message) {
122 Stop("unimplemented");
123}
124
125
126void Assembler::Untested(const char* message) {
127 Stop("untested");
128}
129
130
131void Assembler::Unreachable(const char* message) {
132 Stop("unreachable");
133}
134#endif
135
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700136} // namespace art