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 |
Jim Stichnoth | 92a6e5b | 2015-12-02 16:52:44 -0800 | [diff] [blame] | 22 | /// \brief Implements the Assembler base class. |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 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 | |
Karl Schimpf | 67574d8 | 2015-12-08 15:37:00 -0800 | [diff] [blame] | 38 | void Label::linkTo(const Assembler &Asm, intptr_t Pos) { |
| 39 | // We must not set the link until the position is absolutely known. This means |
| 40 | // not during the preliminary (sandboxing) pass, and not when the instruction |
| 41 | // needs a text fixup (hybrid iasm mode). |
| 42 | if (Asm.getPreliminary() || Asm.needsTextFixup()) |
| 43 | return; |
| 44 | assert(!isBound()); |
| 45 | Position = Pos + kWordSize; |
| 46 | assert(isLinked()); |
| 47 | } |
| 48 | |
Karl Schimpf | 2fee2a2 | 2015-10-22 08:19:26 -0700 | [diff] [blame] | 49 | void AssemblerBuffer::installFixup(AssemblerFixup *F) { |
Karl Schimpf | 2fee2a2 | 2015-10-22 08:19:26 -0700 | [diff] [blame] | 50 | if (!Assemblr.getPreliminary()) |
| 51 | Fixups.push_back(F); |
| 52 | } |
| 53 | |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 54 | AssemblerFixup *AssemblerBuffer::createFixup(FixupKind Kind, |
| 55 | const Constant *Value) { |
| 56 | AssemblerFixup *F = |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 57 | new (Assemblr.allocate<AssemblerFixup>()) AssemblerFixup(); |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 58 | F->set_kind(Kind); |
| 59 | F->set_value(Value); |
Karl Schimpf | 2fee2a2 | 2015-10-22 08:19:26 -0700 | [diff] [blame] | 60 | installFixup(F); |
| 61 | return F; |
| 62 | } |
| 63 | |
| 64 | AssemblerTextFixup *AssemblerBuffer::createTextFixup(const std::string &Text, |
| 65 | size_t BytesUsed) { |
| 66 | AssemblerTextFixup *F = new (Assemblr.allocate<AssemblerTextFixup>()) |
| 67 | AssemblerTextFixup(Text, BytesUsed); |
| 68 | installFixup(F); |
Karl Schimpf | 82975ba | 2016-02-02 10:17:01 -0800 | [diff] [blame] | 69 | resetNeedsTextFixup(); |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 70 | return F; |
| 71 | } |
| 72 | |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 73 | void AssemblerBuffer::EnsureCapacity::validate(AssemblerBuffer *buffer) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 74 | // In debug mode, we save the assembler buffer along with the gap size before |
| 75 | // we start emitting to the buffer. This allows us to check that any single |
| 76 | // generated instruction doesn't overflow the limit implied by the minimum |
| 77 | // gap size. |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 78 | Gap = computeGap(); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 79 | // Make sure that extending the capacity leaves a big enough gap for any kind |
| 80 | // of instruction. |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 81 | assert(Gap >= kMinimumGap); |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 82 | // Mark the buffer as having ensured the capacity. |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 83 | assert(!buffer->hasEnsuredCapacity()); // Cannot nest. |
| 84 | buffer->HasEnsuredCapacity = true; |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | AssemblerBuffer::EnsureCapacity::~EnsureCapacity() { |
| 88 | // Unmark the buffer, so we cannot emit after this. |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 89 | Buffer->HasEnsuredCapacity = false; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 90 | // Make sure the generated instruction doesn't take up more space than the |
| 91 | // minimum gap. |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 92 | intptr_t delta = Gap - computeGap(); |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 93 | (void)delta; |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 94 | assert(delta <= kMinimumGap); |
| 95 | } |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 96 | |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 97 | AssemblerBuffer::AssemblerBuffer(Assembler &Asm) : Assemblr(Asm) { |
Karl Schimpf | 2fee2a2 | 2015-10-22 08:19:26 -0700 | [diff] [blame] | 98 | constexpr intptr_t OneKB = 1024; |
| 99 | static constexpr intptr_t kInitialBufferCapacity = 4 * OneKB; |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 100 | Contents = NewContents(Assemblr, kInitialBufferCapacity); |
| 101 | Cursor = Contents; |
| 102 | Limit = computeLimit(Contents, kInitialBufferCapacity); |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 103 | HasEnsuredCapacity = false; |
Karl Schimpf | 2fee2a2 | 2015-10-22 08:19:26 -0700 | [diff] [blame] | 104 | TextFixupNeeded = false; |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 105 | |
| 106 | // Verify internal state. |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 107 | assert(capacity() == kInitialBufferCapacity); |
| 108 | assert(size() == 0); |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 111 | AssemblerBuffer::~AssemblerBuffer() = default; |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 112 | |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 113 | void AssemblerBuffer::extendCapacity() { |
| 114 | intptr_t old_size = size(); |
| 115 | intptr_t old_capacity = capacity(); |
Karl Schimpf | 2fee2a2 | 2015-10-22 08:19:26 -0700 | [diff] [blame] | 116 | constexpr intptr_t OneMB = 1 << 20; |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 117 | intptr_t new_capacity = std::min(old_capacity * 2, old_capacity + OneMB); |
| 118 | if (new_capacity < old_capacity) { |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 119 | llvm::report_fatal_error( |
| 120 | "Unexpected overflow in AssemblerBuffer::ExtendCapacity"); |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | // 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] | 124 | uintptr_t new_contents = NewContents(Assemblr, new_capacity); |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 125 | memmove(reinterpret_cast<void *>(new_contents), |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 126 | reinterpret_cast<void *>(Contents), old_size); |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 127 | |
| 128 | // Compute the relocation delta and switch to the new contents area. |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 129 | intptr_t delta = new_contents - Contents; |
| 130 | Contents = new_contents; |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 131 | |
| 132 | // Update the cursor and recompute the limit. |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 133 | Cursor += delta; |
| 134 | Limit = computeLimit(new_contents, new_capacity); |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 135 | |
| 136 | // Verify internal state. |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 137 | assert(capacity() == new_capacity); |
| 138 | assert(size() == old_size); |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 141 | llvm::StringRef Assembler::getBufferView() const { |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 142 | return llvm::StringRef(reinterpret_cast<const char *>(Buffer.contents()), |
| 143 | Buffer.size()); |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 144 | } |
| 145 | |
John Porto | dc61925 | 2016-02-10 15:57:16 -0800 | [diff] [blame] | 146 | void Assembler::bindRelocOffset(RelocOffset *Offset) { |
| 147 | if (!getPreliminary()) { |
| 148 | Offset->setOffset(Buffer.getPosition()); |
| 149 | } |
| 150 | } |
| 151 | |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 152 | void Assembler::emitIASBytes(GlobalContext *Ctx) const { |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 153 | Ostream &Str = Ctx->getStrEmit(); |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 154 | intptr_t EndPosition = Buffer.size(); |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 155 | intptr_t CurPosition = 0; |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 156 | for (const AssemblerFixup *NextFixup : fixups()) { |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 157 | intptr_t NextFixupLoc = NextFixup->position(); |
| 158 | for (intptr_t i = CurPosition; i < NextFixupLoc; ++i) { |
| 159 | Str << "\t.byte 0x"; |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 160 | Str.write_hex(Buffer.load<uint8_t>(i)); |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 161 | Str << "\n"; |
| 162 | } |
Karl Schimpf | e4289e2 | 2015-10-27 15:16:27 -0700 | [diff] [blame] | 163 | CurPosition = NextFixupLoc + NextFixup->emit(Ctx, *this); |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 164 | assert(CurPosition <= EndPosition); |
| 165 | } |
| 166 | // Handle any bytes that are not prefixed by a fixup. |
| 167 | for (intptr_t i = CurPosition; i < EndPosition; ++i) { |
| 168 | Str << "\t.byte 0x"; |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 169 | Str.write_hex(Buffer.load<uint8_t>(i)); |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 170 | Str << "\n"; |
| 171 | } |
| 172 | } |
| 173 | |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 174 | } // end of namespace Ice |