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