John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 1 | //===- subzero/src/IceAssembler.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 | //===----------------------------------------------------------------------===// |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 20 | /// |
| 21 | /// \file |
| 22 | /// This file implements the Assembler base class. |
| 23 | /// |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
| 25 | |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 26 | #include "IceAssembler.h" |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 27 | |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 28 | #include "IceGlobalContext.h" |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 29 | #include "IceOperand.h" |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 30 | |
| 31 | namespace Ice { |
| 32 | |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 33 | static uintptr_t NewContents(Assembler &Assemblr, intptr_t Capacity) { |
| 34 | uintptr_t Result = Assemblr.allocateBytes(Capacity); |
| 35 | return Result; |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 38 | AssemblerFixup *AssemblerBuffer::createFixup(FixupKind Kind, |
| 39 | const Constant *Value) { |
| 40 | AssemblerFixup *F = |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 41 | new (Assemblr.allocate<AssemblerFixup>()) AssemblerFixup(); |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 42 | F->set_position(0); |
| 43 | F->set_kind(Kind); |
| 44 | F->set_value(Value); |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 45 | if (!Assemblr.getPreliminary()) |
| 46 | Fixups.push_back(F); |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 47 | return F; |
| 48 | } |
| 49 | |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 50 | void AssemblerBuffer::EnsureCapacity::validate(AssemblerBuffer *buffer) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 51 | // In debug mode, we save the assembler buffer along with the gap size before |
| 52 | // we start emitting to the buffer. This allows us to check that any single |
| 53 | // generated instruction doesn't overflow the limit implied by the minimum |
| 54 | // gap size. |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 55 | Gap = computeGap(); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 56 | // Make sure that extending the capacity leaves a big enough gap for any kind |
| 57 | // of instruction. |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 58 | assert(Gap >= kMinimumGap); |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 59 | // Mark the buffer as having ensured the capacity. |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 60 | assert(!buffer->hasEnsuredCapacity()); // Cannot nest. |
| 61 | buffer->HasEnsuredCapacity = true; |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | AssemblerBuffer::EnsureCapacity::~EnsureCapacity() { |
| 65 | // Unmark the buffer, so we cannot emit after this. |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 66 | Buffer->HasEnsuredCapacity = false; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 67 | // Make sure the generated instruction doesn't take up more space than the |
| 68 | // minimum gap. |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 69 | intptr_t delta = Gap - computeGap(); |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 70 | (void)delta; |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 71 | assert(delta <= kMinimumGap); |
| 72 | } |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 73 | |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 74 | AssemblerBuffer::AssemblerBuffer(Assembler &Asm) : Assemblr(Asm) { |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 75 | const intptr_t OneKB = 1024; |
| 76 | static const intptr_t kInitialBufferCapacity = 4 * OneKB; |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 77 | Contents = NewContents(Assemblr, kInitialBufferCapacity); |
| 78 | Cursor = Contents; |
| 79 | Limit = computeLimit(Contents, kInitialBufferCapacity); |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 80 | HasEnsuredCapacity = false; |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 81 | |
| 82 | // Verify internal state. |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 83 | assert(capacity() == kInitialBufferCapacity); |
| 84 | assert(size() == 0); |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 87 | AssemblerBuffer::~AssemblerBuffer() = default; |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 88 | |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 89 | void AssemblerBuffer::extendCapacity() { |
| 90 | intptr_t old_size = size(); |
| 91 | intptr_t old_capacity = capacity(); |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 92 | const intptr_t OneMB = 1 << 20; |
| 93 | intptr_t new_capacity = std::min(old_capacity * 2, old_capacity + OneMB); |
| 94 | if (new_capacity < old_capacity) { |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 95 | llvm::report_fatal_error( |
| 96 | "Unexpected overflow in AssemblerBuffer::ExtendCapacity"); |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | // Allocate the new data area and copy contents of the old one to it. |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 100 | uintptr_t new_contents = NewContents(Assemblr, new_capacity); |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 101 | memmove(reinterpret_cast<void *>(new_contents), |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 102 | reinterpret_cast<void *>(Contents), old_size); |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 103 | |
| 104 | // Compute the relocation delta and switch to the new contents area. |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 105 | intptr_t delta = new_contents - Contents; |
| 106 | Contents = new_contents; |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 107 | |
| 108 | // Update the cursor and recompute the limit. |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 109 | Cursor += delta; |
| 110 | Limit = computeLimit(new_contents, new_capacity); |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 111 | |
| 112 | // Verify internal state. |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 113 | assert(capacity() == new_capacity); |
| 114 | assert(size() == old_size); |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 117 | llvm::StringRef Assembler::getBufferView() const { |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 118 | return llvm::StringRef(reinterpret_cast<const char *>(Buffer.contents()), |
| 119 | Buffer.size()); |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 122 | void Assembler::emitIASBytes(GlobalContext *Ctx) const { |
| 123 | Ostream &Str = Ctx->getStrEmit(); |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 124 | intptr_t EndPosition = Buffer.size(); |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 125 | intptr_t CurPosition = 0; |
| 126 | const intptr_t FixupSize = 4; |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 127 | for (const AssemblerFixup *NextFixup : fixups()) { |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 128 | intptr_t NextFixupLoc = NextFixup->position(); |
| 129 | for (intptr_t i = CurPosition; i < NextFixupLoc; ++i) { |
| 130 | Str << "\t.byte 0x"; |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 131 | Str.write_hex(Buffer.load<uint8_t>(i)); |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 132 | Str << "\n"; |
| 133 | } |
| 134 | Str << "\t.long "; |
Jan Voung | b7db1a5 | 2015-07-21 09:39:01 -0700 | [diff] [blame] | 135 | // For PCRel fixups, we write the pc-offset from a symbol into the Buffer |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 136 | // (e.g., -4), but we don't represent that in the fixup's offset. Otherwise |
| 137 | // the fixup holds the true offset, and so does the Buffer. Just load the |
| 138 | // offset from the buffer. |
John Porto | aff4ccf | 2015-06-10 16:35:06 -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"; |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 149 | Str.write_hex(Buffer.load<uint8_t>(i)); |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 150 | Str << "\n"; |
| 151 | } |
| 152 | } |
| 153 | |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 154 | } // end of namespace Ice |