Jan Voung | f76fd37 | 2014-10-16 15:39:22 -0700 | [diff] [blame] | 1 | //===- subzero/src/assembler.cpp - Assembler base class -------------------===// |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 2 | // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 3 | // for details. All rights reserved. Use of this source code is governed by a |
| 4 | // BSD-style license that can be found in the LICENSE file. |
| 5 | // |
| 6 | // Modified by the Subzero authors. |
| 7 | // |
Jan Voung | 33a5f41 | 2015-02-03 16:06:42 -0800 | [diff] [blame] | 8 | // This is forked from Dart revision 39313. |
| 9 | // Please update the revision if we merge back changes from Dart. |
| 10 | // https://code.google.com/p/dart/wiki/GettingTheSource |
| 11 | // |
Jan Voung | f76fd37 | 2014-10-16 15:39:22 -0700 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 13 | // |
| 14 | // The Subzero Code Generator |
| 15 | // |
| 16 | // This file is distributed under the University of Illinois Open Source |
| 17 | // License. See LICENSE.TXT for details. |
| 18 | // |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | // |
| 21 | // This file implements the Assembler class. |
| 22 | // |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
| 25 | #include "assembler.h" |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 26 | #include "IceGlobalContext.h" |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 27 | #include "IceOperand.h" |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 28 | |
| 29 | namespace Ice { |
| 30 | |
| 31 | static uintptr_t NewContents(Assembler &assembler, intptr_t capacity) { |
| 32 | uintptr_t result = assembler.AllocateBytes(capacity); |
| 33 | return result; |
| 34 | } |
| 35 | |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 36 | AssemblerFixup *AssemblerBuffer::createFixup(FixupKind Kind, |
| 37 | const Constant *Value) { |
| 38 | AssemblerFixup *F = |
| 39 | new (assembler_.Allocate<AssemblerFixup>()) AssemblerFixup(); |
| 40 | F->set_position(0); |
| 41 | F->set_kind(Kind); |
| 42 | F->set_value(Value); |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 43 | if (!assembler_.getPreliminary()) |
| 44 | fixups_.push_back(F); |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 45 | return F; |
| 46 | } |
| 47 | |
Jan Voung | f76fd37 | 2014-10-16 15:39:22 -0700 | [diff] [blame] | 48 | #ifndef NDEBUG |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 49 | AssemblerBuffer::EnsureCapacity::EnsureCapacity(AssemblerBuffer *buffer) { |
| 50 | if (buffer->cursor() >= buffer->limit()) |
| 51 | buffer->ExtendCapacity(); |
| 52 | // In debug mode, we save the assembler buffer along with the gap |
| 53 | // size before we start emitting to the buffer. This allows us to |
| 54 | // check that any single generated instruction doesn't overflow the |
| 55 | // limit implied by the minimum gap size. |
| 56 | buffer_ = buffer; |
| 57 | gap_ = ComputeGap(); |
| 58 | // Make sure that extending the capacity leaves a big enough gap |
| 59 | // for any kind of instruction. |
| 60 | assert(gap_ >= kMinimumGap); |
| 61 | // Mark the buffer as having ensured the capacity. |
| 62 | assert(!buffer->HasEnsuredCapacity()); // Cannot nest. |
| 63 | buffer->has_ensured_capacity_ = true; |
| 64 | } |
| 65 | |
| 66 | AssemblerBuffer::EnsureCapacity::~EnsureCapacity() { |
| 67 | // Unmark the buffer, so we cannot emit after this. |
| 68 | buffer_->has_ensured_capacity_ = false; |
| 69 | // Make sure the generated instruction doesn't take up more |
| 70 | // space than the minimum gap. |
| 71 | intptr_t delta = gap_ - ComputeGap(); |
| 72 | assert(delta <= kMinimumGap); |
| 73 | } |
Jan Voung | f76fd37 | 2014-10-16 15:39:22 -0700 | [diff] [blame] | 74 | #endif // !NDEBUG |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 75 | |
| 76 | AssemblerBuffer::AssemblerBuffer(Assembler &assembler) : assembler_(assembler) { |
| 77 | const intptr_t OneKB = 1024; |
| 78 | static const intptr_t kInitialBufferCapacity = 4 * OneKB; |
| 79 | contents_ = NewContents(assembler_, kInitialBufferCapacity); |
| 80 | cursor_ = contents_; |
| 81 | limit_ = ComputeLimit(contents_, kInitialBufferCapacity); |
Jan Voung | f76fd37 | 2014-10-16 15:39:22 -0700 | [diff] [blame] | 82 | #ifndef NDEBUG |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 83 | has_ensured_capacity_ = false; |
Jan Voung | f76fd37 | 2014-10-16 15:39:22 -0700 | [diff] [blame] | 84 | #endif // !NDEBUG |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 85 | |
| 86 | // Verify internal state. |
| 87 | assert(Capacity() == kInitialBufferCapacity); |
| 88 | assert(Size() == 0); |
| 89 | } |
| 90 | |
| 91 | AssemblerBuffer::~AssemblerBuffer() {} |
| 92 | |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 93 | void AssemblerBuffer::ExtendCapacity() { |
| 94 | intptr_t old_size = Size(); |
| 95 | intptr_t old_capacity = Capacity(); |
| 96 | const intptr_t OneMB = 1 << 20; |
| 97 | intptr_t new_capacity = std::min(old_capacity * 2, old_capacity + OneMB); |
| 98 | if (new_capacity < old_capacity) { |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 99 | llvm::report_fatal_error( |
| 100 | "Unexpected overflow in AssemblerBuffer::ExtendCapacity"); |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | // Allocate the new data area and copy contents of the old one to it. |
| 104 | uintptr_t new_contents = NewContents(assembler_, new_capacity); |
| 105 | memmove(reinterpret_cast<void *>(new_contents), |
| 106 | reinterpret_cast<void *>(contents_), old_size); |
| 107 | |
| 108 | // Compute the relocation delta and switch to the new contents area. |
| 109 | intptr_t delta = new_contents - contents_; |
| 110 | contents_ = new_contents; |
| 111 | |
| 112 | // Update the cursor and recompute the limit. |
| 113 | cursor_ += delta; |
| 114 | limit_ = ComputeLimit(new_contents, new_capacity); |
| 115 | |
| 116 | // Verify internal state. |
| 117 | assert(Capacity() == new_capacity); |
| 118 | assert(Size() == old_size); |
| 119 | } |
| 120 | |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 121 | llvm::StringRef Assembler::getBufferView() const { |
| 122 | return llvm::StringRef(reinterpret_cast<const char *>(buffer_.contents()), |
| 123 | buffer_.Size()); |
| 124 | } |
| 125 | |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 126 | void Assembler::emitIASBytes(GlobalContext *Ctx) const { |
| 127 | Ostream &Str = Ctx->getStrEmit(); |
| 128 | intptr_t EndPosition = buffer_.Size(); |
| 129 | intptr_t CurPosition = 0; |
| 130 | const intptr_t FixupSize = 4; |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 131 | for (const AssemblerFixup *NextFixup : fixups()) { |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 132 | intptr_t NextFixupLoc = NextFixup->position(); |
| 133 | for (intptr_t i = CurPosition; i < NextFixupLoc; ++i) { |
| 134 | Str << "\t.byte 0x"; |
| 135 | Str.write_hex(buffer_.Load<uint8_t>(i)); |
| 136 | Str << "\n"; |
| 137 | } |
| 138 | Str << "\t.long "; |
Jim Stichnoth | 927f7cc | 2015-03-19 23:23:00 -0700 | [diff] [blame] | 139 | NextFixup->emit(Ctx, buffer_.Load<RelocOffsetT>(NextFixupLoc)); |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 140 | if (fixupIsPCRel(NextFixup->kind())) |
Jim Stichnoth | 927f7cc | 2015-03-19 23:23:00 -0700 | [diff] [blame] | 141 | Str << " - ."; |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 142 | Str << "\n"; |
| 143 | CurPosition = NextFixupLoc + FixupSize; |
| 144 | assert(CurPosition <= EndPosition); |
| 145 | } |
| 146 | // Handle any bytes that are not prefixed by a fixup. |
| 147 | for (intptr_t i = CurPosition; i < EndPosition; ++i) { |
| 148 | Str << "\t.byte 0x"; |
| 149 | Str.write_hex(buffer_.Load<uint8_t>(i)); |
| 150 | Str << "\n"; |
| 151 | } |
| 152 | } |
| 153 | |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 154 | } // end of namespace Ice |