John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 1 | //===- subzero/src/IceAssembler.h - Integrated assembler --------*- C++ -*-===// |
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 | f76fd37 | 2014-10-16 15:39:22 -0700 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 9 | // |
| 10 | // The Subzero Code Generator |
| 11 | // |
| 12 | // This file is distributed under the University of Illinois Open Source |
| 13 | // License. See LICENSE.TXT for details. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 16 | /// |
| 17 | /// \file |
Jim Stichnoth | 92a6e5b | 2015-12-02 16:52:44 -0800 | [diff] [blame] | 18 | /// \brief Declares the Assembler base class. |
| 19 | /// |
| 20 | /// Instructions are assembled by architecture-specific assemblers that derive |
| 21 | /// from this base class. This base class manages buffers and fixups for |
| 22 | /// emitting code, etc. |
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 | #ifndef SUBZERO_SRC_ICEASSEMBLER_H |
| 27 | #define SUBZERO_SRC_ICEASSEMBLER_H |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 28 | |
| 29 | #include "IceDefs.h" |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 30 | #include "IceFixups.h" |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 31 | #include "IceStringPool.h" |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 32 | #include "IceUtils.h" |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 33 | |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 34 | #include "llvm/Support/Allocator.h" |
| 35 | |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 36 | namespace Ice { |
| 37 | |
Karl Schimpf | 67574d8 | 2015-12-08 15:37:00 -0800 | [diff] [blame] | 38 | class Assembler; |
| 39 | |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 40 | /// A Label can be in one of three states: |
| 41 | /// - Unused. |
| 42 | /// - Linked, unplaced and tracking the position of branches to the label. |
| 43 | /// - Bound, placed and tracking its position. |
| 44 | class Label { |
| 45 | Label(const Label &) = delete; |
| 46 | Label &operator=(const Label &) = delete; |
| 47 | |
| 48 | public: |
| 49 | Label() = default; |
Sean Klein | fc707ff | 2016-02-29 16:44:07 -0800 | [diff] [blame] | 50 | virtual ~Label() = default; |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 51 | |
| 52 | virtual void finalCheck() const { |
| 53 | // Assert if label is being destroyed with unresolved branches pending. |
| 54 | assert(!isLinked()); |
| 55 | } |
| 56 | |
Karl Schimpf | 137e62b | 2015-10-27 07:28:09 -0700 | [diff] [blame] | 57 | /// Returns the encoded position stored in the label. |
| 58 | intptr_t getEncodedPosition() const { return Position; } |
| 59 | |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 60 | /// Returns the position for bound labels (branches that come after this are |
| 61 | /// considered backward branches). Cannot be used for unused or linked labels. |
| 62 | intptr_t getPosition() const { |
| 63 | assert(isBound()); |
| 64 | return -Position - kWordSize; |
| 65 | } |
| 66 | |
| 67 | /// Returns the position of an earlier branch instruction that was linked to |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 68 | /// this label (branches that use this are considered forward branches). The |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 69 | /// linked instructions form a linked list, of sorts, using the instruction's |
| 70 | /// displacement field for the location of the next instruction that is also |
| 71 | /// linked to this label. |
| 72 | intptr_t getLinkPosition() const { |
| 73 | assert(isLinked()); |
| 74 | return Position - kWordSize; |
| 75 | } |
| 76 | |
Karl Schimpf | c5abdc1 | 2015-10-09 13:29:13 -0700 | [diff] [blame] | 77 | void setPosition(intptr_t NewValue) { Position = NewValue; } |
| 78 | |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 79 | bool isBound() const { return Position < 0; } |
| 80 | bool isLinked() const { return Position > 0; } |
Karl Schimpf | c5abdc1 | 2015-10-09 13:29:13 -0700 | [diff] [blame] | 81 | |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 82 | virtual bool isUnused() const { return Position == 0; } |
| 83 | |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 84 | void bindTo(intptr_t position) { |
| 85 | assert(!isBound()); |
| 86 | Position = -position - kWordSize; |
| 87 | assert(isBound()); |
| 88 | } |
| 89 | |
Karl Schimpf | 67574d8 | 2015-12-08 15:37:00 -0800 | [diff] [blame] | 90 | void linkTo(const Assembler &Asm, intptr_t position); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 91 | |
Karl Schimpf | 137e62b | 2015-10-27 07:28:09 -0700 | [diff] [blame] | 92 | protected: |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 93 | intptr_t Position = 0; |
| 94 | |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 95 | // TODO(jvoung): why are labels offset by this? |
| 96 | static constexpr uint32_t kWordSize = sizeof(uint32_t); |
| 97 | }; |
| 98 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 99 | /// Assembler buffers are used to emit binary code. They grow on demand. |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 100 | class AssemblerBuffer { |
Jan Voung | f76fd37 | 2014-10-16 15:39:22 -0700 | [diff] [blame] | 101 | AssemblerBuffer(const AssemblerBuffer &) = delete; |
| 102 | AssemblerBuffer &operator=(const AssemblerBuffer &) = delete; |
| 103 | |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 104 | public: |
| 105 | AssemblerBuffer(Assembler &); |
| 106 | ~AssemblerBuffer(); |
| 107 | |
Andrew Scull | 6ef7949 | 2015-09-09 15:50:42 -0700 | [diff] [blame] | 108 | /// \name Basic support for emitting, loading, and storing. |
| 109 | /// @{ |
| 110 | // These use memcpy instead of assignment to avoid undefined behaviour of |
| 111 | // assigning to unaligned addresses. Since the size of the copy is known the |
| 112 | // compiler can inline the memcpy with simple moves. |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 113 | template <typename T> void emit(T Value) { |
| 114 | assert(hasEnsuredCapacity()); |
Andrew Scull | 6ef7949 | 2015-09-09 15:50:42 -0700 | [diff] [blame] | 115 | memcpy(reinterpret_cast<void *>(Cursor), &Value, sizeof(T)); |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 116 | Cursor += sizeof(T); |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 117 | } |
| 118 | |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 119 | template <typename T> T load(intptr_t Position) const { |
| 120 | assert(Position >= 0 && |
| 121 | Position <= (size() - static_cast<intptr_t>(sizeof(T)))); |
Andrew Scull | 6ef7949 | 2015-09-09 15:50:42 -0700 | [diff] [blame] | 122 | T Value; |
| 123 | memcpy(&Value, reinterpret_cast<void *>(Contents + Position), sizeof(T)); |
| 124 | return Value; |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 125 | } |
| 126 | |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 127 | template <typename T> void store(intptr_t Position, T Value) { |
| 128 | assert(Position >= 0 && |
| 129 | Position <= (size() - static_cast<intptr_t>(sizeof(T)))); |
Andrew Scull | 6ef7949 | 2015-09-09 15:50:42 -0700 | [diff] [blame] | 130 | memcpy(reinterpret_cast<void *>(Contents + Position), &Value, sizeof(T)); |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 131 | } |
Andrew Scull | 6ef7949 | 2015-09-09 15:50:42 -0700 | [diff] [blame] | 132 | /// @{ |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 133 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 134 | /// Emit a fixup at the current location. |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 135 | void emitFixup(AssemblerFixup *Fixup) { Fixup->set_position(size()); } |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 136 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 137 | /// Get the size of the emitted code. |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 138 | intptr_t size() const { return Cursor - Contents; } |
| 139 | uintptr_t contents() const { return Contents; } |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 140 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 141 | /// To emit an instruction to the assembler buffer, the EnsureCapacity helper |
| 142 | /// must be used to guarantee that the underlying data area is big enough to |
| 143 | /// hold the emitted instruction. Usage: |
| 144 | /// |
| 145 | /// AssemblerBuffer buffer; |
| 146 | /// AssemblerBuffer::EnsureCapacity ensured(&buffer); |
| 147 | /// ... emit bytes for single instruction ... |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 148 | class EnsureCapacity { |
Jan Voung | f76fd37 | 2014-10-16 15:39:22 -0700 | [diff] [blame] | 149 | EnsureCapacity(const EnsureCapacity &) = delete; |
| 150 | EnsureCapacity &operator=(const EnsureCapacity &) = delete; |
| 151 | |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 152 | public: |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 153 | explicit EnsureCapacity(AssemblerBuffer *Buffer) : Buffer(Buffer) { |
| 154 | if (Buffer->cursor() >= Buffer->limit()) |
| 155 | Buffer->extendCapacity(); |
| 156 | if (BuildDefs::asserts()) |
| 157 | validate(Buffer); |
| 158 | } |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 159 | ~EnsureCapacity(); |
| 160 | |
| 161 | private: |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 162 | AssemblerBuffer *Buffer; |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 163 | intptr_t Gap = 0; |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 164 | |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 165 | void validate(AssemblerBuffer *Buffer); |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 166 | intptr_t computeGap() { return Buffer->capacity() - Buffer->size(); } |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 167 | }; |
| 168 | |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 169 | bool HasEnsuredCapacity; |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 170 | bool hasEnsuredCapacity() const { |
| 171 | if (BuildDefs::asserts()) |
| 172 | return HasEnsuredCapacity; |
| 173 | // Disable the actual check in non-debug mode. |
| 174 | return true; |
| 175 | } |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 176 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 177 | /// Returns the position in the instruction stream. |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 178 | intptr_t getPosition() const { return Cursor - Contents; } |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 179 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 180 | /// Create and track a fixup in the current function. |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 181 | AssemblerFixup *createFixup(FixupKind Kind, const Constant *Value); |
| 182 | |
Karl Schimpf | 2fee2a2 | 2015-10-22 08:19:26 -0700 | [diff] [blame] | 183 | /// Create and track a textual fixup in the current function. |
| 184 | AssemblerTextFixup *createTextFixup(const std::string &Text, |
| 185 | size_t BytesUsed); |
| 186 | |
| 187 | /// Mark that an attempt was made to emit, but failed. Hence, in order to |
| 188 | /// continue, one must emit a text fixup. |
| 189 | void setNeedsTextFixup() { TextFixupNeeded = true; } |
Karl Schimpf | 82975ba | 2016-02-02 10:17:01 -0800 | [diff] [blame] | 190 | void resetNeedsTextFixup() { TextFixupNeeded = false; } |
Karl Schimpf | 2fee2a2 | 2015-10-22 08:19:26 -0700 | [diff] [blame] | 191 | |
| 192 | /// Returns true if last emit failed and needs a text fixup. |
| 193 | bool needsTextFixup() const { return TextFixupNeeded; } |
| 194 | |
| 195 | /// Installs a created fixup, after it has been allocated. |
| 196 | void installFixup(AssemblerFixup *F); |
| 197 | |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 198 | const FixupRefList &fixups() const { return Fixups; } |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 199 | |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 200 | void setSize(intptr_t NewSize) { |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 201 | assert(NewSize <= size()); |
| 202 | Cursor = Contents + NewSize; |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 205 | private: |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 206 | /// The limit is set to kMinimumGap bytes before the end of the data area. |
| 207 | /// This leaves enough space for the longest possible instruction and allows |
| 208 | /// for a single, fast space check per instruction. |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 209 | static constexpr intptr_t kMinimumGap = 32; |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 210 | |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 211 | uintptr_t Contents; |
| 212 | uintptr_t Cursor; |
| 213 | uintptr_t Limit; |
| 214 | // The member variable is named Assemblr to avoid hiding the class Assembler. |
| 215 | Assembler &Assemblr; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 216 | /// List of pool-allocated fixups relative to the current function. |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 217 | FixupRefList Fixups; |
Karl Schimpf | 2fee2a2 | 2015-10-22 08:19:26 -0700 | [diff] [blame] | 218 | // True if a textual fixup is needed, because the assembler was unable to |
| 219 | // emit the last request. |
| 220 | bool TextFixupNeeded; |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 221 | |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 222 | uintptr_t cursor() const { return Cursor; } |
| 223 | uintptr_t limit() const { return Limit; } |
| 224 | intptr_t capacity() const { |
| 225 | assert(Limit >= Contents); |
| 226 | return (Limit - Contents) + kMinimumGap; |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 229 | /// Compute the limit based on the data area and the capacity. See description |
| 230 | /// of kMinimumGap for the reasoning behind the value. |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 231 | static uintptr_t computeLimit(uintptr_t Data, intptr_t Capacity) { |
| 232 | return Data + Capacity - kMinimumGap; |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 233 | } |
| 234 | |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 235 | void extendCapacity(); |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 236 | }; |
| 237 | |
| 238 | class Assembler { |
John Porto | 2da710c | 2015-06-29 07:57:02 -0700 | [diff] [blame] | 239 | Assembler() = delete; |
Jan Voung | f76fd37 | 2014-10-16 15:39:22 -0700 | [diff] [blame] | 240 | Assembler(const Assembler &) = delete; |
| 241 | Assembler &operator=(const Assembler &) = delete; |
| 242 | |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 243 | public: |
John Porto | 2da710c | 2015-06-29 07:57:02 -0700 | [diff] [blame] | 244 | enum AssemblerKind { |
| 245 | Asm_ARM32, |
| 246 | Asm_MIPS32, |
| 247 | Asm_X8632, |
| 248 | Asm_X8664, |
| 249 | }; |
| 250 | |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 251 | virtual ~Assembler() = default; |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 252 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 253 | /// Allocate a chunk of bytes using the per-Assembler allocator. |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 254 | uintptr_t allocateBytes(size_t bytes) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 255 | // For now, alignment is not related to NaCl bundle alignment, since the |
| 256 | // buffer's GetPosition is relative to the base. So NaCl bundle alignment |
| 257 | // checks can be relative to that base. Later, the buffer will be copied |
| 258 | // out to a ".text" section (or an in memory-buffer that can be mprotect'ed |
| 259 | // with executable permission), and that second buffer should be aligned |
| 260 | // for NaCl. |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 261 | const size_t Alignment = 16; |
| 262 | return reinterpret_cast<uintptr_t>(Allocator.Allocate(bytes, Alignment)); |
| 263 | } |
| 264 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 265 | /// Allocate data of type T using the per-Assembler allocator. |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 266 | template <typename T> T *allocate() { return Allocator.Allocate<T>(); } |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 267 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 268 | /// Align the tail end of the function to the required target alignment. |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 269 | virtual void alignFunction() = 0; |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 270 | /// Align the tail end of the basic block to the required target alignment. |
| 271 | void alignCfgNode() { |
| 272 | const SizeT Align = 1 << getBundleAlignLog2Bytes(); |
| 273 | padWithNop(Utils::OffsetToAlignment(Buffer.getPosition(), Align)); |
| 274 | } |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 275 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 276 | /// Add nop padding of a particular width to the current bundle. |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 277 | virtual void padWithNop(intptr_t Padding) = 0; |
| 278 | |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 279 | virtual SizeT getBundleAlignLog2Bytes() const = 0; |
| 280 | |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 281 | virtual const char *getAlignDirective() const = 0; |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 282 | virtual llvm::ArrayRef<uint8_t> getNonExecBundlePadding() const = 0; |
| 283 | |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 284 | /// Get the label for a CfgNode. |
Jan Voung | c2ec581 | 2015-08-05 09:35:18 -0700 | [diff] [blame] | 285 | virtual Label *getCfgNodeLabel(SizeT NodeNumber) = 0; |
Karl Schimpf | 50a3331 | 2015-10-23 09:19:48 -0700 | [diff] [blame] | 286 | /// Mark the current text location as the start of a CFG node. |
| 287 | virtual void bindCfgNodeLabel(const CfgNode *Node) = 0; |
Jan Voung | 7e1e485 | 2014-10-24 10:29:30 -0700 | [diff] [blame] | 288 | |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 289 | virtual bool fixupIsPCRel(FixupKind Kind) const = 0; |
| 290 | |
Karl Schimpf | e4289e2 | 2015-10-27 15:16:27 -0700 | [diff] [blame] | 291 | /// Return a view of all the bytes of code for the current function. |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 292 | llvm::StringRef getBufferView() const; |
| 293 | |
Karl Schimpf | e4289e2 | 2015-10-27 15:16:27 -0700 | [diff] [blame] | 294 | /// Return the value of the given type in the corresponding buffer. |
| 295 | template <typename T> T load(intptr_t Position) const { |
| 296 | return Buffer.load<T>(Position); |
| 297 | } |
| 298 | |
John Porto | 6e8d3fa | 2016-02-04 10:35:20 -0800 | [diff] [blame] | 299 | template <typename T> void store(intptr_t Position, T Value) { |
| 300 | Buffer.store(Position, Value); |
| 301 | } |
| 302 | |
Karl Schimpf | 2fee2a2 | 2015-10-22 08:19:26 -0700 | [diff] [blame] | 303 | /// Emit a fixup at the current location. |
| 304 | void emitFixup(AssemblerFixup *Fixup) { Buffer.emitFixup(Fixup); } |
| 305 | |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 306 | const FixupRefList &fixups() const { return Buffer.fixups(); } |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 307 | |
| 308 | AssemblerFixup *createFixup(FixupKind Kind, const Constant *Value) { |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 309 | return Buffer.createFixup(Kind, Value); |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Karl Schimpf | 2fee2a2 | 2015-10-22 08:19:26 -0700 | [diff] [blame] | 312 | AssemblerTextFixup *createTextFixup(const std::string &Text, |
| 313 | size_t BytesUsed) { |
| 314 | return Buffer.createTextFixup(Text, BytesUsed); |
| 315 | } |
| 316 | |
John Porto | dc61925 | 2016-02-10 15:57:16 -0800 | [diff] [blame] | 317 | void bindRelocOffset(RelocOffset *Offset); |
| 318 | |
Karl Schimpf | 2fee2a2 | 2015-10-22 08:19:26 -0700 | [diff] [blame] | 319 | void setNeedsTextFixup() { Buffer.setNeedsTextFixup(); } |
Karl Schimpf | 82975ba | 2016-02-02 10:17:01 -0800 | [diff] [blame] | 320 | void resetNeedsTextFixup() { Buffer.resetNeedsTextFixup(); } |
Karl Schimpf | 2fee2a2 | 2015-10-22 08:19:26 -0700 | [diff] [blame] | 321 | |
| 322 | bool needsTextFixup() const { return Buffer.needsTextFixup(); } |
| 323 | |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 324 | void emitIASBytes(GlobalContext *Ctx) const; |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 325 | bool getInternal() const { return IsInternal; } |
| 326 | void setInternal(bool Internal) { IsInternal = Internal; } |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 327 | GlobalString getFunctionName() const { return FunctionName; } |
| 328 | void setFunctionName(GlobalString NewName) { FunctionName = NewName; } |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 329 | intptr_t getBufferSize() const { return Buffer.size(); } |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 330 | /// Roll back to a (smaller) size. |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 331 | void setBufferSize(intptr_t NewSize) { Buffer.setSize(NewSize); } |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 332 | void setPreliminary(bool Value) { Preliminary = Value; } |
| 333 | bool getPreliminary() const { return Preliminary; } |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 334 | |
John Porto | 2da710c | 2015-06-29 07:57:02 -0700 | [diff] [blame] | 335 | AssemblerKind getKind() const { return Kind; } |
| 336 | |
| 337 | protected: |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 338 | explicit Assembler(AssemblerKind Kind) |
| 339 | : Kind(Kind), Allocator(), Buffer(*this) {} |
John Porto | 2da710c | 2015-06-29 07:57:02 -0700 | [diff] [blame] | 340 | |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 341 | private: |
John Porto | 2da710c | 2015-06-29 07:57:02 -0700 | [diff] [blame] | 342 | const AssemblerKind Kind; |
| 343 | |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 344 | using AssemblerAllocator = |
| 345 | llvm::BumpPtrAllocatorImpl<llvm::MallocAllocator, /*SlabSize=*/32 * 1024>; |
| 346 | AssemblerAllocator Allocator; |
| 347 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 348 | /// FunctionName and IsInternal are transferred from the original Cfg object, |
| 349 | /// since the Cfg object may be deleted by the time the assembler buffer is |
| 350 | /// emitted. |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 351 | GlobalString FunctionName; |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 352 | bool IsInternal = false; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 353 | /// Preliminary indicates whether a preliminary pass is being made for |
| 354 | /// calculating bundle padding (Preliminary=true), versus the final pass where |
| 355 | /// all changes to label bindings, label links, and relocation fixups are |
| 356 | /// fully committed (Preliminary=false). |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 357 | bool Preliminary = false; |
John Porto | 1a9043e | 2015-06-11 09:35:18 -0700 | [diff] [blame] | 358 | |
Karl Schimpf | 2fee2a2 | 2015-10-22 08:19:26 -0700 | [diff] [blame] | 359 | /// Installs a created fixup, after it has been allocated. |
| 360 | void installFixup(AssemblerFixup *F) { Buffer.installFixup(F); } |
| 361 | |
John Porto | 1a9043e | 2015-06-11 09:35:18 -0700 | [diff] [blame] | 362 | protected: |
| 363 | // Buffer's constructor uses the Allocator, so it needs to appear after it. |
| 364 | // TODO(jpp): dependencies on construction order are a nice way of shooting |
| 365 | // yourself in the foot. Fix this. |
| 366 | AssemblerBuffer Buffer; |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 367 | }; |
| 368 | |
| 369 | } // end of namespace Ice |
| 370 | |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 371 | #endif // SUBZERO_SRC_ICEASSEMBLER_H_ |