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