Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 1 | // 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 Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 9 | namespace art { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 10 | |
| 11 | static 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) |
| 23 | AssemblerBuffer::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 | |
| 40 | AssemblerBuffer::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 | |
| 51 | AssemblerBuffer::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 | |
| 68 | AssemblerBuffer::~AssemblerBuffer() { |
| 69 | } |
| 70 | |
| 71 | |
| 72 | void 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 | |
| 81 | void 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 | |
| 94 | void 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. |
| 121 | void Assembler::Unimplemented(const char* message) { |
| 122 | Stop("unimplemented"); |
| 123 | } |
| 124 | |
| 125 | |
| 126 | void Assembler::Untested(const char* message) { |
| 127 | Stop("untested"); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | void Assembler::Unreachable(const char* message) { |
| 132 | Stop("unreachable"); |
| 133 | } |
| 134 | #endif |
| 135 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 136 | } // namespace art |