Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 16 | |
Ian Rogers | 166db04 | 2013-07-26 12:05:57 -0700 | [diff] [blame] | 17 | #ifndef ART_COMPILER_UTILS_ASSEMBLER_H_ |
| 18 | #define ART_COMPILER_UTILS_ASSEMBLER_H_ |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 19 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 22 | #include "arch/instruction_set.h" |
Goran Jakovljevic | 8c434dc | 2015-08-26 14:39:44 +0200 | [diff] [blame] | 23 | #include "arch/instruction_set_features.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 24 | #include "arm/constants_arm.h" |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 25 | #include "base/arena_allocator.h" |
| 26 | #include "base/arena_object.h" |
David Brazdil | d9c9037 | 2016-09-14 16:53:55 +0100 | [diff] [blame] | 27 | #include "base/array_ref.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 28 | #include "base/enums.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 29 | #include "base/logging.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 30 | #include "base/macros.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 31 | #include "debug/dwarf/debug_frame_opcode_writer.h" |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 32 | #include "label.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 33 | #include "managed_register.h" |
| 34 | #include "memory_region.h" |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 35 | #include "mips/constants_mips.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 36 | #include "offsets.h" |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 37 | #include "x86/constants_x86.h" |
| 38 | #include "x86_64/constants_x86_64.h" |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 39 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 40 | namespace art { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 41 | |
| 42 | class Assembler; |
| 43 | class AssemblerBuffer; |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 44 | |
| 45 | // Assembler fixups are positions in generated code that require processing |
| 46 | // after the code has been copied to executable memory. This includes building |
| 47 | // relocation information. |
| 48 | class AssemblerFixup { |
| 49 | public: |
| 50 | virtual void Process(const MemoryRegion& region, int position) = 0; |
| 51 | virtual ~AssemblerFixup() {} |
| 52 | |
| 53 | private: |
| 54 | AssemblerFixup* previous_; |
| 55 | int position_; |
| 56 | |
| 57 | AssemblerFixup* previous() const { return previous_; } |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 58 | void set_previous(AssemblerFixup* previous_in) { previous_ = previous_in; } |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 59 | |
| 60 | int position() const { return position_; } |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 61 | void set_position(int position_in) { position_ = position_in; } |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 62 | |
| 63 | friend class AssemblerBuffer; |
| 64 | }; |
| 65 | |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 66 | // Parent of all queued slow paths, emitted during finalization |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 67 | class SlowPath : public DeletableArenaObject<kArenaAllocAssembler> { |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 68 | public: |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 69 | SlowPath() : next_(nullptr) {} |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 70 | virtual ~SlowPath() {} |
| 71 | |
| 72 | Label* Continuation() { return &continuation_; } |
| 73 | Label* Entry() { return &entry_; } |
| 74 | // Generate code for slow path |
| 75 | virtual void Emit(Assembler *sp_asm) = 0; |
| 76 | |
| 77 | protected: |
| 78 | // Entry branched to by fast path |
| 79 | Label entry_; |
| 80 | // Optional continuation that is branched to at the end of the slow path |
| 81 | Label continuation_; |
| 82 | // Next in linked list of slow paths |
| 83 | SlowPath *next_; |
| 84 | |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 85 | private: |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 86 | friend class AssemblerBuffer; |
| 87 | DISALLOW_COPY_AND_ASSIGN(SlowPath); |
| 88 | }; |
| 89 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 90 | class AssemblerBuffer { |
| 91 | public: |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 92 | explicit AssemblerBuffer(ArenaAllocator* arena); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 93 | ~AssemblerBuffer(); |
| 94 | |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 95 | ArenaAllocator* GetArena() { |
| 96 | return arena_; |
| 97 | } |
| 98 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 99 | // Basic support for emitting, loading, and storing. |
| 100 | template<typename T> void Emit(T value) { |
| 101 | CHECK(HasEnsuredCapacity()); |
| 102 | *reinterpret_cast<T*>(cursor_) = value; |
| 103 | cursor_ += sizeof(T); |
| 104 | } |
| 105 | |
| 106 | template<typename T> T Load(size_t position) { |
| 107 | CHECK_LE(position, Size() - static_cast<int>(sizeof(T))); |
| 108 | return *reinterpret_cast<T*>(contents_ + position); |
| 109 | } |
| 110 | |
| 111 | template<typename T> void Store(size_t position, T value) { |
| 112 | CHECK_LE(position, Size() - static_cast<int>(sizeof(T))); |
| 113 | *reinterpret_cast<T*>(contents_ + position) = value; |
| 114 | } |
| 115 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 116 | void Resize(size_t new_size) { |
| 117 | if (new_size > Capacity()) { |
| 118 | ExtendCapacity(new_size); |
| 119 | } |
| 120 | cursor_ = contents_ + new_size; |
| 121 | } |
| 122 | |
| 123 | void Move(size_t newposition, size_t oldposition, size_t size) { |
| 124 | // Move a chunk of the buffer from oldposition to newposition. |
| 125 | DCHECK_LE(oldposition + size, Size()); |
| 126 | DCHECK_LE(newposition + size, Size()); |
| 127 | memmove(contents_ + newposition, contents_ + oldposition, size); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 130 | // Emit a fixup at the current location. |
| 131 | void EmitFixup(AssemblerFixup* fixup) { |
| 132 | fixup->set_previous(fixup_); |
| 133 | fixup->set_position(Size()); |
| 134 | fixup_ = fixup; |
| 135 | } |
| 136 | |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 137 | void EnqueueSlowPath(SlowPath* slowpath) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 138 | if (slow_path_ == nullptr) { |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 139 | slow_path_ = slowpath; |
| 140 | } else { |
| 141 | SlowPath* cur = slow_path_; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 142 | for ( ; cur->next_ != nullptr ; cur = cur->next_) {} |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 143 | cur->next_ = slowpath; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void EmitSlowPaths(Assembler* sp_asm) { |
| 148 | SlowPath* cur = slow_path_; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 149 | SlowPath* next = nullptr; |
| 150 | slow_path_ = nullptr; |
| 151 | for ( ; cur != nullptr ; cur = next) { |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 152 | cur->Emit(sp_asm); |
| 153 | next = cur->next_; |
| 154 | delete cur; |
| 155 | } |
| 156 | } |
| 157 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 158 | // Get the size of the emitted code. |
| 159 | size_t Size() const { |
| 160 | CHECK_GE(cursor_, contents_); |
| 161 | return cursor_ - contents_; |
| 162 | } |
| 163 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 164 | uint8_t* contents() const { return contents_; } |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 165 | |
| 166 | // Copy the assembled instructions into the specified memory block |
| 167 | // and apply all fixups. |
| 168 | void FinalizeInstructions(const MemoryRegion& region); |
| 169 | |
| 170 | // To emit an instruction to the assembler buffer, the EnsureCapacity helper |
| 171 | // must be used to guarantee that the underlying data area is big enough to |
| 172 | // hold the emitted instruction. Usage: |
| 173 | // |
| 174 | // AssemblerBuffer buffer; |
| 175 | // AssemblerBuffer::EnsureCapacity ensured(&buffer); |
| 176 | // ... emit bytes for single instruction ... |
| 177 | |
Elliott Hughes | 31f1f4f | 2012-03-12 13:57:36 -0700 | [diff] [blame] | 178 | #ifndef NDEBUG |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 179 | |
| 180 | class EnsureCapacity { |
| 181 | public: |
| 182 | explicit EnsureCapacity(AssemblerBuffer* buffer) { |
Vladimir Marko | 9152fed | 2016-04-20 14:39:47 +0100 | [diff] [blame] | 183 | if (buffer->cursor() > buffer->limit()) { |
| 184 | buffer->ExtendCapacity(buffer->Size() + kMinimumGap); |
Elliott Hughes | 31f1f4f | 2012-03-12 13:57:36 -0700 | [diff] [blame] | 185 | } |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 186 | // In debug mode, we save the assembler buffer along with the gap |
| 187 | // size before we start emitting to the buffer. This allows us to |
| 188 | // check that any single generated instruction doesn't overflow the |
| 189 | // limit implied by the minimum gap size. |
| 190 | buffer_ = buffer; |
| 191 | gap_ = ComputeGap(); |
| 192 | // Make sure that extending the capacity leaves a big enough gap |
| 193 | // for any kind of instruction. |
| 194 | CHECK_GE(gap_, kMinimumGap); |
| 195 | // Mark the buffer as having ensured the capacity. |
| 196 | CHECK(!buffer->HasEnsuredCapacity()); // Cannot nest. |
| 197 | buffer->has_ensured_capacity_ = true; |
| 198 | } |
| 199 | |
| 200 | ~EnsureCapacity() { |
| 201 | // Unmark the buffer, so we cannot emit after this. |
| 202 | buffer_->has_ensured_capacity_ = false; |
| 203 | // Make sure the generated instruction doesn't take up more |
| 204 | // space than the minimum gap. |
| 205 | int delta = gap_ - ComputeGap(); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 206 | CHECK_LE(delta, kMinimumGap); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | private: |
| 210 | AssemblerBuffer* buffer_; |
| 211 | int gap_; |
| 212 | |
| 213 | int ComputeGap() { return buffer_->Capacity() - buffer_->Size(); } |
| 214 | }; |
| 215 | |
| 216 | bool has_ensured_capacity_; |
| 217 | bool HasEnsuredCapacity() const { return has_ensured_capacity_; } |
| 218 | |
| 219 | #else |
| 220 | |
| 221 | class EnsureCapacity { |
| 222 | public: |
| 223 | explicit EnsureCapacity(AssemblerBuffer* buffer) { |
Vladimir Marko | 9152fed | 2016-04-20 14:39:47 +0100 | [diff] [blame] | 224 | if (buffer->cursor() > buffer->limit()) { |
| 225 | buffer->ExtendCapacity(buffer->Size() + kMinimumGap); |
| 226 | } |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 227 | } |
| 228 | }; |
| 229 | |
| 230 | // When building the C++ tests, assertion code is enabled. To allow |
| 231 | // asserting that the user of the assembler buffer has ensured the |
| 232 | // capacity needed for emitting, we add a dummy method in non-debug mode. |
| 233 | bool HasEnsuredCapacity() const { return true; } |
| 234 | |
| 235 | #endif |
| 236 | |
| 237 | // Returns the position in the instruction stream. |
| 238 | int GetPosition() { return cursor_ - contents_; } |
| 239 | |
Vladimir Marko | 9152fed | 2016-04-20 14:39:47 +0100 | [diff] [blame] | 240 | size_t Capacity() const { |
| 241 | CHECK_GE(limit_, contents_); |
| 242 | return (limit_ - contents_) + kMinimumGap; |
| 243 | } |
| 244 | |
| 245 | // Unconditionally increase the capacity. |
| 246 | // The provided `min_capacity` must be higher than current `Capacity()`. |
| 247 | void ExtendCapacity(size_t min_capacity); |
Andreas Gampe | 7cffc3b | 2015-10-19 21:31:53 -0700 | [diff] [blame] | 248 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 249 | private: |
| 250 | // The limit is set to kMinimumGap bytes before the end of the data area. |
| 251 | // This leaves enough space for the longest possible instruction and allows |
| 252 | // for a single, fast space check per instruction. |
| 253 | static const int kMinimumGap = 32; |
| 254 | |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 255 | ArenaAllocator* arena_; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 256 | uint8_t* contents_; |
| 257 | uint8_t* cursor_; |
| 258 | uint8_t* limit_; |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 259 | AssemblerFixup* fixup_; |
Ian Rogers | b48b9eb | 2014-02-28 16:20:21 -0800 | [diff] [blame] | 260 | #ifndef NDEBUG |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 261 | bool fixups_processed_; |
Ian Rogers | b48b9eb | 2014-02-28 16:20:21 -0800 | [diff] [blame] | 262 | #endif |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 263 | |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 264 | // Head of linked list of slow paths |
| 265 | SlowPath* slow_path_; |
| 266 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 267 | uint8_t* cursor() const { return cursor_; } |
| 268 | uint8_t* limit() const { return limit_; } |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 269 | |
| 270 | // Process the fixup chain starting at the given fixup. The offset is |
| 271 | // non-zero for fixups in the body if the preamble is non-empty. |
| 272 | void ProcessFixups(const MemoryRegion& region); |
| 273 | |
| 274 | // Compute the limit based on the data area and the capacity. See |
| 275 | // description of kMinimumGap for the reasoning behind the value. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 276 | static uint8_t* ComputeLimit(uint8_t* data, size_t capacity) { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 277 | return data + capacity - kMinimumGap; |
| 278 | } |
| 279 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 280 | friend class AssemblerFixup; |
| 281 | }; |
| 282 | |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 283 | // The purpose of this class is to ensure that we do not have to explicitly |
| 284 | // call the AdvancePC method (which is good for convenience and correctness). |
| 285 | class DebugFrameOpCodeWriterForAssembler FINAL |
| 286 | : public dwarf::DebugFrameOpCodeWriter<> { |
| 287 | public: |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 288 | struct DelayedAdvancePC { |
| 289 | uint32_t stream_pos; |
| 290 | uint32_t pc; |
| 291 | }; |
| 292 | |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 293 | // This method is called the by the opcode writers. |
| 294 | virtual void ImplicitlyAdvancePC() FINAL; |
| 295 | |
| 296 | explicit DebugFrameOpCodeWriterForAssembler(Assembler* buffer) |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 297 | : dwarf::DebugFrameOpCodeWriter<>(false /* enabled */), |
| 298 | assembler_(buffer), |
| 299 | delay_emitting_advance_pc_(false), |
| 300 | delayed_advance_pcs_() { |
| 301 | } |
| 302 | |
| 303 | ~DebugFrameOpCodeWriterForAssembler() { |
| 304 | DCHECK(delayed_advance_pcs_.empty()); |
| 305 | } |
| 306 | |
| 307 | // Tell the writer to delay emitting advance PC info. |
| 308 | // The assembler must explicitly process all the delayed advances. |
| 309 | void DelayEmittingAdvancePCs() { |
| 310 | delay_emitting_advance_pc_ = true; |
| 311 | } |
| 312 | |
| 313 | // Override the last delayed PC. The new PC can be out of order. |
| 314 | void OverrideDelayedPC(size_t pc) { |
| 315 | DCHECK(delay_emitting_advance_pc_); |
Vladimir Marko | 6134ba1 | 2016-04-14 11:27:34 +0100 | [diff] [blame] | 316 | if (enabled_) { |
| 317 | DCHECK(!delayed_advance_pcs_.empty()); |
| 318 | delayed_advance_pcs_.back().pc = pc; |
| 319 | } |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | // Return the number of delayed advance PC entries. |
| 323 | size_t NumberOfDelayedAdvancePCs() const { |
| 324 | return delayed_advance_pcs_.size(); |
| 325 | } |
| 326 | |
| 327 | // Release the CFI stream and advance PC infos so that the assembler can patch it. |
| 328 | std::pair<std::vector<uint8_t>, std::vector<DelayedAdvancePC>> |
| 329 | ReleaseStreamAndPrepareForDelayedAdvancePC() { |
| 330 | DCHECK(delay_emitting_advance_pc_); |
| 331 | delay_emitting_advance_pc_ = false; |
| 332 | std::pair<std::vector<uint8_t>, std::vector<DelayedAdvancePC>> result; |
| 333 | result.first.swap(opcodes_); |
| 334 | result.second.swap(delayed_advance_pcs_); |
| 335 | return result; |
| 336 | } |
| 337 | |
| 338 | // Reserve space for the CFI stream. |
| 339 | void ReserveCFIStream(size_t capacity) { |
| 340 | opcodes_.reserve(capacity); |
| 341 | } |
| 342 | |
| 343 | // Append raw data to the CFI stream. |
| 344 | void AppendRawData(const std::vector<uint8_t>& raw_data, size_t first, size_t last) { |
| 345 | DCHECK_LE(0u, first); |
| 346 | DCHECK_LE(first, last); |
| 347 | DCHECK_LE(last, raw_data.size()); |
| 348 | opcodes_.insert(opcodes_.end(), raw_data.begin() + first, raw_data.begin() + last); |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | private: |
| 352 | Assembler* assembler_; |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 353 | bool delay_emitting_advance_pc_; |
| 354 | std::vector<DelayedAdvancePC> delayed_advance_pcs_; |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 355 | }; |
| 356 | |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 357 | class Assembler : public DeletableArenaObject<kArenaAllocAssembler> { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 358 | public: |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 359 | // Finalize the code; emit slow paths, fixup branches, add literal pool, etc. |
| 360 | virtual void FinalizeCode() { buffer_.EmitSlowPaths(this); } |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 361 | |
| 362 | // Size of generated code |
Serban Constantinescu | ed8dd49 | 2014-02-11 14:15:10 +0000 | [diff] [blame] | 363 | virtual size_t CodeSize() const { return buffer_.Size(); } |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 364 | virtual const uint8_t* CodeBufferBaseAddress() const { return buffer_.contents(); } |
Alexey Frunze | 57eb0f5 | 2016-07-29 22:04:46 -0700 | [diff] [blame] | 365 | // CodePosition() is a non-const method similar to CodeSize(), which is used to |
| 366 | // record positions within the code buffer for the purpose of signal handling |
| 367 | // (stack overflow checks and implicit null checks may trigger signals and the |
| 368 | // signal handlers expect them right before the recorded positions). |
| 369 | // On most architectures CodePosition() should be equivalent to CodeSize(), but |
| 370 | // the MIPS assembler needs to be aware of this recording, so it doesn't put |
| 371 | // the instructions that can trigger signals into branch delay slots. Handling |
| 372 | // signals from instructions in delay slots is a bit problematic and should be |
| 373 | // avoided. |
| 374 | virtual size_t CodePosition() { return CodeSize(); } |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 375 | |
| 376 | // Copy instructions out of assembly buffer into the given region of memory |
Serban Constantinescu | ed8dd49 | 2014-02-11 14:15:10 +0000 | [diff] [blame] | 377 | virtual void FinalizeInstructions(const MemoryRegion& region) { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 378 | buffer_.FinalizeInstructions(region); |
| 379 | } |
| 380 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 381 | // TODO: Implement with disassembler. |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 382 | virtual void Comment(const char* format ATTRIBUTE_UNUSED, ...) {} |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 383 | |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 384 | virtual void Bind(Label* label) = 0; |
| 385 | virtual void Jump(Label* label) = 0; |
| 386 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 387 | virtual ~Assembler() {} |
| 388 | |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 389 | /** |
| 390 | * @brief Buffer of DWARF's Call Frame Information opcodes. |
| 391 | * @details It is used by debuggers and other tools to unwind the call stack. |
| 392 | */ |
| 393 | DebugFrameOpCodeWriterForAssembler& cfi() { return cfi_; } |
| 394 | |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 395 | ArenaAllocator* GetArena() { |
| 396 | return buffer_.GetArena(); |
| 397 | } |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 398 | |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 399 | AssemblerBuffer* GetBuffer() { |
| 400 | return &buffer_; |
| 401 | } |
| 402 | |
| 403 | protected: |
| 404 | explicit Assembler(ArenaAllocator* arena) : buffer_(arena), cfi_(this) {} |
| 405 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 406 | AssemblerBuffer buffer_; |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 407 | |
| 408 | DebugFrameOpCodeWriterForAssembler cfi_; |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 409 | }; |
| 410 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 411 | } // namespace art |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 412 | |
Ian Rogers | 166db04 | 2013-07-26 12:05:57 -0700 | [diff] [blame] | 413 | #endif // ART_COMPILER_UTILS_ASSEMBLER_H_ |