| OLD | NEW |
| 1 //===- subzero/src/assembler.cpp - Assembler base class -------------------===// |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 2 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 3 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 4 // BSD-style license that can be found in the LICENSE file. |
| 4 // | 5 // |
| 5 // Modified by the Subzero authors. | 6 // Modified by the Subzero authors. |
| 6 // | 7 // |
| 7 //===- subzero/src/assembler.cpp - Assembler base class -------------------===// | 8 //===----------------------------------------------------------------------===// |
| 8 // | 9 // |
| 9 // The Subzero Code Generator | 10 // The Subzero Code Generator |
| 10 // | 11 // |
| 11 // This file is distributed under the University of Illinois Open Source | 12 // This file is distributed under the University of Illinois Open Source |
| 12 // License. See LICENSE.TXT for details. | 13 // License. See LICENSE.TXT for details. |
| 13 // | 14 // |
| 14 //===----------------------------------------------------------------------===// | 15 //===----------------------------------------------------------------------===// |
| 15 // | 16 // |
| 16 // This file implements the Assembler class. | 17 // This file implements the Assembler class. |
| 17 // | 18 // |
| 18 //===----------------------------------------------------------------------===// | 19 //===----------------------------------------------------------------------===// |
| 19 | 20 |
| 20 #include "assembler.h" | 21 #include "assembler.h" |
| 21 #include "IceMemoryRegion.h" | 22 #include "IceMemoryRegion.h" |
| 22 | 23 |
| 23 namespace Ice { | 24 namespace Ice { |
| 24 | 25 |
| 25 static uintptr_t NewContents(Assembler &assembler, intptr_t capacity) { | 26 static uintptr_t NewContents(Assembler &assembler, intptr_t capacity) { |
| 26 uintptr_t result = assembler.AllocateBytes(capacity); | 27 uintptr_t result = assembler.AllocateBytes(capacity); |
| 27 return result; | 28 return result; |
| 28 } | 29 } |
| 29 | 30 |
| 30 #if defined(DEBUG) | 31 #ifndef NDEBUG |
| 31 AssemblerBuffer::EnsureCapacity::EnsureCapacity(AssemblerBuffer *buffer) { | 32 AssemblerBuffer::EnsureCapacity::EnsureCapacity(AssemblerBuffer *buffer) { |
| 32 if (buffer->cursor() >= buffer->limit()) | 33 if (buffer->cursor() >= buffer->limit()) |
| 33 buffer->ExtendCapacity(); | 34 buffer->ExtendCapacity(); |
| 34 // In debug mode, we save the assembler buffer along with the gap | 35 // In debug mode, we save the assembler buffer along with the gap |
| 35 // size before we start emitting to the buffer. This allows us to | 36 // size before we start emitting to the buffer. This allows us to |
| 36 // check that any single generated instruction doesn't overflow the | 37 // check that any single generated instruction doesn't overflow the |
| 37 // limit implied by the minimum gap size. | 38 // limit implied by the minimum gap size. |
| 38 buffer_ = buffer; | 39 buffer_ = buffer; |
| 39 gap_ = ComputeGap(); | 40 gap_ = ComputeGap(); |
| 40 // Make sure that extending the capacity leaves a big enough gap | 41 // Make sure that extending the capacity leaves a big enough gap |
| 41 // for any kind of instruction. | 42 // for any kind of instruction. |
| 42 assert(gap_ >= kMinimumGap); | 43 assert(gap_ >= kMinimumGap); |
| 43 // Mark the buffer as having ensured the capacity. | 44 // Mark the buffer as having ensured the capacity. |
| 44 assert(!buffer->HasEnsuredCapacity()); // Cannot nest. | 45 assert(!buffer->HasEnsuredCapacity()); // Cannot nest. |
| 45 buffer->has_ensured_capacity_ = true; | 46 buffer->has_ensured_capacity_ = true; |
| 46 } | 47 } |
| 47 | 48 |
| 48 AssemblerBuffer::EnsureCapacity::~EnsureCapacity() { | 49 AssemblerBuffer::EnsureCapacity::~EnsureCapacity() { |
| 49 // Unmark the buffer, so we cannot emit after this. | 50 // Unmark the buffer, so we cannot emit after this. |
| 50 buffer_->has_ensured_capacity_ = false; | 51 buffer_->has_ensured_capacity_ = false; |
| 51 // Make sure the generated instruction doesn't take up more | 52 // Make sure the generated instruction doesn't take up more |
| 52 // space than the minimum gap. | 53 // space than the minimum gap. |
| 53 intptr_t delta = gap_ - ComputeGap(); | 54 intptr_t delta = gap_ - ComputeGap(); |
| 54 assert(delta <= kMinimumGap); | 55 assert(delta <= kMinimumGap); |
| 55 } | 56 } |
| 56 #endif | 57 #endif // !NDEBUG |
| 57 | 58 |
| 58 AssemblerBuffer::AssemblerBuffer(Assembler &assembler) : assembler_(assembler) { | 59 AssemblerBuffer::AssemblerBuffer(Assembler &assembler) : assembler_(assembler) { |
| 59 const intptr_t OneKB = 1024; | 60 const intptr_t OneKB = 1024; |
| 60 static const intptr_t kInitialBufferCapacity = 4 * OneKB; | 61 static const intptr_t kInitialBufferCapacity = 4 * OneKB; |
| 61 contents_ = NewContents(assembler_, kInitialBufferCapacity); | 62 contents_ = NewContents(assembler_, kInitialBufferCapacity); |
| 62 cursor_ = contents_; | 63 cursor_ = contents_; |
| 63 limit_ = ComputeLimit(contents_, kInitialBufferCapacity); | 64 limit_ = ComputeLimit(contents_, kInitialBufferCapacity); |
| 64 #if defined(DEBUG) | 65 #ifndef NDEBUG |
| 65 has_ensured_capacity_ = false; | 66 has_ensured_capacity_ = false; |
| 66 fixups_processed_ = false; | 67 fixups_processed_ = false; |
| 67 #endif | 68 #endif // !NDEBUG |
| 68 | 69 |
| 69 // Verify internal state. | 70 // Verify internal state. |
| 70 assert(Capacity() == kInitialBufferCapacity); | 71 assert(Capacity() == kInitialBufferCapacity); |
| 71 assert(Size() == 0); | 72 assert(Size() == 0); |
| 72 } | 73 } |
| 73 | 74 |
| 74 AssemblerBuffer::~AssemblerBuffer() {} | 75 AssemblerBuffer::~AssemblerBuffer() {} |
| 75 | 76 |
| 76 AssemblerFixup *AssemblerBuffer::GetLatestFixup() const { | 77 AssemblerFixup *AssemblerBuffer::GetLatestFixup() const { |
| 77 if (fixups_.empty()) | 78 if (fixups_.empty()) |
| 78 return NULL; | 79 return NULL; |
| 79 return fixups_.back(); | 80 return fixups_.back(); |
| 80 } | 81 } |
| 81 | 82 |
| 82 void AssemblerBuffer::ProcessFixups(const MemoryRegion ®ion) { | 83 void AssemblerBuffer::ProcessFixups(const MemoryRegion ®ion) { |
| 83 for (SizeT I = 0; I < fixups_.size(); ++I) { | 84 for (SizeT I = 0; I < fixups_.size(); ++I) { |
| 84 AssemblerFixup *fixup = fixups_[I]; | 85 AssemblerFixup *fixup = fixups_[I]; |
| 85 fixup->Process(region, fixup->position()); | 86 fixup->Process(region, fixup->position()); |
| 86 } | 87 } |
| 87 } | 88 } |
| 88 | 89 |
| 89 void AssemblerBuffer::FinalizeInstructions(const MemoryRegion &instructions) { | 90 void AssemblerBuffer::FinalizeInstructions(const MemoryRegion &instructions) { |
| 90 // Copy the instructions from the buffer. | 91 // Copy the instructions from the buffer. |
| 91 MemoryRegion from(reinterpret_cast<void *>(contents()), Size()); | 92 MemoryRegion from(reinterpret_cast<void *>(contents()), Size()); |
| 92 instructions.CopyFrom(0, from); | 93 instructions.CopyFrom(0, from); |
| 93 | 94 |
| 94 // Process fixups in the instructions. | 95 // Process fixups in the instructions. |
| 95 ProcessFixups(instructions); | 96 ProcessFixups(instructions); |
| 96 #if defined(DEBUG) | 97 #ifndef NDEBUG |
| 97 fixups_processed_ = true; | 98 fixups_processed_ = true; |
| 98 #endif | 99 #endif // !NDEBUG |
| 99 } | 100 } |
| 100 | 101 |
| 101 void AssemblerBuffer::ExtendCapacity() { | 102 void AssemblerBuffer::ExtendCapacity() { |
| 102 intptr_t old_size = Size(); | 103 intptr_t old_size = Size(); |
| 103 intptr_t old_capacity = Capacity(); | 104 intptr_t old_capacity = Capacity(); |
| 104 const intptr_t OneMB = 1 << 20; | 105 const intptr_t OneMB = 1 << 20; |
| 105 intptr_t new_capacity = std::min(old_capacity * 2, old_capacity + OneMB); | 106 intptr_t new_capacity = std::min(old_capacity * 2, old_capacity + OneMB); |
| 106 if (new_capacity < old_capacity) { | 107 if (new_capacity < old_capacity) { |
| 107 // FATAL | 108 // FATAL |
| 108 llvm_unreachable("Unexpected overflow in AssemblerBuffer::ExtendCapacity"); | 109 llvm_unreachable("Unexpected overflow in AssemblerBuffer::ExtendCapacity"); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 120 // Update the cursor and recompute the limit. | 121 // Update the cursor and recompute the limit. |
| 121 cursor_ += delta; | 122 cursor_ += delta; |
| 122 limit_ = ComputeLimit(new_contents, new_capacity); | 123 limit_ = ComputeLimit(new_contents, new_capacity); |
| 123 | 124 |
| 124 // Verify internal state. | 125 // Verify internal state. |
| 125 assert(Capacity() == new_capacity); | 126 assert(Capacity() == new_capacity); |
| 126 assert(Size() == old_size); | 127 assert(Size() == old_size); |
| 127 } | 128 } |
| 128 | 129 |
| 129 } // end of namespace Ice | 130 } // end of namespace Ice |
| OLD | NEW |