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" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 23 | #include "base/logging.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 24 | #include "base/macros.h" |
Ian Rogers | 166db04 | 2013-07-26 12:05:57 -0700 | [diff] [blame] | 25 | #include "arm/constants_arm.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 26 | #include "managed_register.h" |
| 27 | #include "memory_region.h" |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 28 | #include "mips/constants_mips.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 29 | #include "offsets.h" |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 30 | #include "x86/constants_x86.h" |
| 31 | #include "x86_64/constants_x86_64.h" |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 32 | #include "dwarf/debug_frame_opcode_writer.h" |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 33 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 34 | namespace art { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 35 | |
| 36 | class Assembler; |
| 37 | class AssemblerBuffer; |
| 38 | class AssemblerFixup; |
| 39 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 40 | namespace arm { |
| 41 | class ArmAssembler; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 42 | class Arm32Assembler; |
| 43 | class Thumb2Assembler; |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 44 | } |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 45 | namespace arm64 { |
| 46 | class Arm64Assembler; |
| 47 | } |
jeffhao | 7fbee07 | 2012-08-24 17:56:54 -0700 | [diff] [blame] | 48 | namespace mips { |
| 49 | class MipsAssembler; |
| 50 | } |
Andreas Gampe | 57b3429 | 2015-01-14 15:45:59 -0800 | [diff] [blame] | 51 | namespace mips64 { |
| 52 | class Mips64Assembler; |
| 53 | } |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 54 | namespace x86 { |
| 55 | class X86Assembler; |
| 56 | } |
Dmitry Petrochenko | fca8220 | 2014-03-21 11:21:37 +0700 | [diff] [blame] | 57 | namespace x86_64 { |
| 58 | class X86_64Assembler; |
| 59 | } |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 60 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 61 | class ExternalLabel { |
| 62 | public: |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 63 | ExternalLabel(const char* name_in, uintptr_t address_in) |
| 64 | : name_(name_in), address_(address_in) { |
| 65 | DCHECK(name_in != nullptr); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | const char* name() const { return name_; } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 69 | uintptr_t address() const { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 70 | return address_; |
| 71 | } |
| 72 | |
| 73 | private: |
| 74 | const char* name_; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 75 | const uintptr_t address_; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 78 | class Label { |
| 79 | public: |
| 80 | Label() : position_(0) {} |
| 81 | |
| 82 | ~Label() { |
| 83 | // Assert if label is being destroyed with unresolved branches pending. |
| 84 | CHECK(!IsLinked()); |
| 85 | } |
| 86 | |
| 87 | // Returns the position for bound and linked labels. Cannot be used |
| 88 | // for unused labels. |
| 89 | int Position() const { |
| 90 | CHECK(!IsUnused()); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 91 | return IsBound() ? -position_ - sizeof(void*) : position_ - sizeof(void*); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | int LinkPosition() const { |
| 95 | CHECK(IsLinked()); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 96 | return position_ - sizeof(void*); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | bool IsBound() const { return position_ < 0; } |
| 100 | bool IsUnused() const { return position_ == 0; } |
| 101 | bool IsLinked() const { return position_ > 0; } |
| 102 | |
| 103 | private: |
| 104 | int position_; |
| 105 | |
| 106 | void Reinitialize() { |
| 107 | position_ = 0; |
| 108 | } |
| 109 | |
| 110 | void BindTo(int position) { |
| 111 | CHECK(!IsBound()); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 112 | position_ = -position - sizeof(void*); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 113 | CHECK(IsBound()); |
| 114 | } |
| 115 | |
| 116 | void LinkTo(int position) { |
| 117 | CHECK(!IsBound()); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 118 | position_ = position + sizeof(void*); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 119 | CHECK(IsLinked()); |
| 120 | } |
| 121 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 122 | friend class arm::ArmAssembler; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 123 | friend class arm::Arm32Assembler; |
| 124 | friend class arm::Thumb2Assembler; |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 125 | friend class arm64::Arm64Assembler; |
jeffhao | 7fbee07 | 2012-08-24 17:56:54 -0700 | [diff] [blame] | 126 | friend class mips::MipsAssembler; |
Andreas Gampe | 57b3429 | 2015-01-14 15:45:59 -0800 | [diff] [blame] | 127 | friend class mips64::Mips64Assembler; |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 128 | friend class x86::X86Assembler; |
Dmitry Petrochenko | fca8220 | 2014-03-21 11:21:37 +0700 | [diff] [blame] | 129 | friend class x86_64::X86_64Assembler; |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 130 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 131 | DISALLOW_COPY_AND_ASSIGN(Label); |
| 132 | }; |
| 133 | |
| 134 | |
| 135 | // Assembler fixups are positions in generated code that require processing |
| 136 | // after the code has been copied to executable memory. This includes building |
| 137 | // relocation information. |
| 138 | class AssemblerFixup { |
| 139 | public: |
| 140 | virtual void Process(const MemoryRegion& region, int position) = 0; |
| 141 | virtual ~AssemblerFixup() {} |
| 142 | |
| 143 | private: |
| 144 | AssemblerFixup* previous_; |
| 145 | int position_; |
| 146 | |
| 147 | AssemblerFixup* previous() const { return previous_; } |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 148 | void set_previous(AssemblerFixup* previous_in) { previous_ = previous_in; } |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 149 | |
| 150 | int position() const { return position_; } |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 151 | void set_position(int position_in) { position_ = position_in; } |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 152 | |
| 153 | friend class AssemblerBuffer; |
| 154 | }; |
| 155 | |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 156 | // Parent of all queued slow paths, emitted during finalization |
| 157 | class SlowPath { |
| 158 | public: |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 159 | SlowPath() : next_(nullptr) {} |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 160 | virtual ~SlowPath() {} |
| 161 | |
| 162 | Label* Continuation() { return &continuation_; } |
| 163 | Label* Entry() { return &entry_; } |
| 164 | // Generate code for slow path |
| 165 | virtual void Emit(Assembler *sp_asm) = 0; |
| 166 | |
| 167 | protected: |
| 168 | // Entry branched to by fast path |
| 169 | Label entry_; |
| 170 | // Optional continuation that is branched to at the end of the slow path |
| 171 | Label continuation_; |
| 172 | // Next in linked list of slow paths |
| 173 | SlowPath *next_; |
| 174 | |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 175 | private: |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 176 | friend class AssemblerBuffer; |
| 177 | DISALLOW_COPY_AND_ASSIGN(SlowPath); |
| 178 | }; |
| 179 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 180 | class AssemblerBuffer { |
| 181 | public: |
| 182 | AssemblerBuffer(); |
| 183 | ~AssemblerBuffer(); |
| 184 | |
| 185 | // Basic support for emitting, loading, and storing. |
| 186 | template<typename T> void Emit(T value) { |
| 187 | CHECK(HasEnsuredCapacity()); |
| 188 | *reinterpret_cast<T*>(cursor_) = value; |
| 189 | cursor_ += sizeof(T); |
| 190 | } |
| 191 | |
| 192 | template<typename T> T Load(size_t position) { |
| 193 | CHECK_LE(position, Size() - static_cast<int>(sizeof(T))); |
| 194 | return *reinterpret_cast<T*>(contents_ + position); |
| 195 | } |
| 196 | |
| 197 | template<typename T> void Store(size_t position, T value) { |
| 198 | CHECK_LE(position, Size() - static_cast<int>(sizeof(T))); |
| 199 | *reinterpret_cast<T*>(contents_ + position) = value; |
| 200 | } |
| 201 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame^] | 202 | void Resize(size_t new_size) { |
| 203 | if (new_size > Capacity()) { |
| 204 | ExtendCapacity(new_size); |
| 205 | } |
| 206 | cursor_ = contents_ + new_size; |
| 207 | } |
| 208 | |
| 209 | void Move(size_t newposition, size_t oldposition, size_t size) { |
| 210 | // Move a chunk of the buffer from oldposition to newposition. |
| 211 | DCHECK_LE(oldposition + size, Size()); |
| 212 | DCHECK_LE(newposition + size, Size()); |
| 213 | memmove(contents_ + newposition, contents_ + oldposition, size); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 216 | // Emit a fixup at the current location. |
| 217 | void EmitFixup(AssemblerFixup* fixup) { |
| 218 | fixup->set_previous(fixup_); |
| 219 | fixup->set_position(Size()); |
| 220 | fixup_ = fixup; |
| 221 | } |
| 222 | |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 223 | void EnqueueSlowPath(SlowPath* slowpath) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 224 | if (slow_path_ == nullptr) { |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 225 | slow_path_ = slowpath; |
| 226 | } else { |
| 227 | SlowPath* cur = slow_path_; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 228 | for ( ; cur->next_ != nullptr ; cur = cur->next_) {} |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 229 | cur->next_ = slowpath; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | void EmitSlowPaths(Assembler* sp_asm) { |
| 234 | SlowPath* cur = slow_path_; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 235 | SlowPath* next = nullptr; |
| 236 | slow_path_ = nullptr; |
| 237 | for ( ; cur != nullptr ; cur = next) { |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 238 | cur->Emit(sp_asm); |
| 239 | next = cur->next_; |
| 240 | delete cur; |
| 241 | } |
| 242 | } |
| 243 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 244 | // Get the size of the emitted code. |
| 245 | size_t Size() const { |
| 246 | CHECK_GE(cursor_, contents_); |
| 247 | return cursor_ - contents_; |
| 248 | } |
| 249 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 250 | uint8_t* contents() const { return contents_; } |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 251 | |
| 252 | // Copy the assembled instructions into the specified memory block |
| 253 | // and apply all fixups. |
| 254 | void FinalizeInstructions(const MemoryRegion& region); |
| 255 | |
| 256 | // To emit an instruction to the assembler buffer, the EnsureCapacity helper |
| 257 | // must be used to guarantee that the underlying data area is big enough to |
| 258 | // hold the emitted instruction. Usage: |
| 259 | // |
| 260 | // AssemblerBuffer buffer; |
| 261 | // AssemblerBuffer::EnsureCapacity ensured(&buffer); |
| 262 | // ... emit bytes for single instruction ... |
| 263 | |
Elliott Hughes | 31f1f4f | 2012-03-12 13:57:36 -0700 | [diff] [blame] | 264 | #ifndef NDEBUG |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 265 | |
| 266 | class EnsureCapacity { |
| 267 | public: |
| 268 | explicit EnsureCapacity(AssemblerBuffer* buffer) { |
Elliott Hughes | 31f1f4f | 2012-03-12 13:57:36 -0700 | [diff] [blame] | 269 | if (buffer->cursor() >= buffer->limit()) { |
| 270 | buffer->ExtendCapacity(); |
| 271 | } |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 272 | // In debug mode, we save the assembler buffer along with the gap |
| 273 | // size before we start emitting to the buffer. This allows us to |
| 274 | // check that any single generated instruction doesn't overflow the |
| 275 | // limit implied by the minimum gap size. |
| 276 | buffer_ = buffer; |
| 277 | gap_ = ComputeGap(); |
| 278 | // Make sure that extending the capacity leaves a big enough gap |
| 279 | // for any kind of instruction. |
| 280 | CHECK_GE(gap_, kMinimumGap); |
| 281 | // Mark the buffer as having ensured the capacity. |
| 282 | CHECK(!buffer->HasEnsuredCapacity()); // Cannot nest. |
| 283 | buffer->has_ensured_capacity_ = true; |
| 284 | } |
| 285 | |
| 286 | ~EnsureCapacity() { |
| 287 | // Unmark the buffer, so we cannot emit after this. |
| 288 | buffer_->has_ensured_capacity_ = false; |
| 289 | // Make sure the generated instruction doesn't take up more |
| 290 | // space than the minimum gap. |
| 291 | int delta = gap_ - ComputeGap(); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 292 | CHECK_LE(delta, kMinimumGap); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | private: |
| 296 | AssemblerBuffer* buffer_; |
| 297 | int gap_; |
| 298 | |
| 299 | int ComputeGap() { return buffer_->Capacity() - buffer_->Size(); } |
| 300 | }; |
| 301 | |
| 302 | bool has_ensured_capacity_; |
| 303 | bool HasEnsuredCapacity() const { return has_ensured_capacity_; } |
| 304 | |
| 305 | #else |
| 306 | |
| 307 | class EnsureCapacity { |
| 308 | public: |
| 309 | explicit EnsureCapacity(AssemblerBuffer* buffer) { |
| 310 | if (buffer->cursor() >= buffer->limit()) buffer->ExtendCapacity(); |
| 311 | } |
| 312 | }; |
| 313 | |
| 314 | // When building the C++ tests, assertion code is enabled. To allow |
| 315 | // asserting that the user of the assembler buffer has ensured the |
| 316 | // capacity needed for emitting, we add a dummy method in non-debug mode. |
| 317 | bool HasEnsuredCapacity() const { return true; } |
| 318 | |
| 319 | #endif |
| 320 | |
| 321 | // Returns the position in the instruction stream. |
| 322 | int GetPosition() { return cursor_ - contents_; } |
| 323 | |
| 324 | private: |
| 325 | // The limit is set to kMinimumGap bytes before the end of the data area. |
| 326 | // This leaves enough space for the longest possible instruction and allows |
| 327 | // for a single, fast space check per instruction. |
| 328 | static const int kMinimumGap = 32; |
| 329 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 330 | uint8_t* contents_; |
| 331 | uint8_t* cursor_; |
| 332 | uint8_t* limit_; |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 333 | AssemblerFixup* fixup_; |
Ian Rogers | b48b9eb | 2014-02-28 16:20:21 -0800 | [diff] [blame] | 334 | #ifndef NDEBUG |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 335 | bool fixups_processed_; |
Ian Rogers | b48b9eb | 2014-02-28 16:20:21 -0800 | [diff] [blame] | 336 | #endif |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 337 | |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 338 | // Head of linked list of slow paths |
| 339 | SlowPath* slow_path_; |
| 340 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 341 | uint8_t* cursor() const { return cursor_; } |
| 342 | uint8_t* limit() const { return limit_; } |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 343 | size_t Capacity() const { |
| 344 | CHECK_GE(limit_, contents_); |
| 345 | return (limit_ - contents_) + kMinimumGap; |
| 346 | } |
| 347 | |
| 348 | // Process the fixup chain starting at the given fixup. The offset is |
| 349 | // non-zero for fixups in the body if the preamble is non-empty. |
| 350 | void ProcessFixups(const MemoryRegion& region); |
| 351 | |
| 352 | // Compute the limit based on the data area and the capacity. See |
| 353 | // description of kMinimumGap for the reasoning behind the value. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 354 | static uint8_t* ComputeLimit(uint8_t* data, size_t capacity) { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 355 | return data + capacity - kMinimumGap; |
| 356 | } |
| 357 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame^] | 358 | void ExtendCapacity(size_t min_capacity = 0u); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 359 | |
| 360 | friend class AssemblerFixup; |
| 361 | }; |
| 362 | |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 363 | // The purpose of this class is to ensure that we do not have to explicitly |
| 364 | // call the AdvancePC method (which is good for convenience and correctness). |
| 365 | class DebugFrameOpCodeWriterForAssembler FINAL |
| 366 | : public dwarf::DebugFrameOpCodeWriter<> { |
| 367 | public: |
| 368 | // This method is called the by the opcode writers. |
| 369 | virtual void ImplicitlyAdvancePC() FINAL; |
| 370 | |
| 371 | explicit DebugFrameOpCodeWriterForAssembler(Assembler* buffer) |
| 372 | : dwarf::DebugFrameOpCodeWriter<>(), |
| 373 | assembler_(buffer) { |
| 374 | } |
| 375 | |
| 376 | private: |
| 377 | Assembler* assembler_; |
| 378 | }; |
| 379 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 380 | class Assembler { |
| 381 | public: |
| 382 | static Assembler* Create(InstructionSet instruction_set); |
| 383 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame^] | 384 | // Finalize the code; emit slow paths, fixup branches, add literal pool, etc. |
| 385 | virtual void FinalizeCode() { buffer_.EmitSlowPaths(this); } |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 386 | |
| 387 | // Size of generated code |
Serban Constantinescu | ed8dd49 | 2014-02-11 14:15:10 +0000 | [diff] [blame] | 388 | virtual size_t CodeSize() const { return buffer_.Size(); } |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 389 | |
| 390 | // Copy instructions out of assembly buffer into the given region of memory |
Serban Constantinescu | ed8dd49 | 2014-02-11 14:15:10 +0000 | [diff] [blame] | 391 | virtual void FinalizeInstructions(const MemoryRegion& region) { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 392 | buffer_.FinalizeInstructions(region); |
| 393 | } |
| 394 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 395 | // TODO: Implement with disassembler. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 396 | virtual void Comment(const char* format, ...) { UNUSED(format); } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 397 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 398 | // Emit code that will create an activation on the stack |
| 399 | virtual void BuildFrame(size_t frame_size, ManagedRegister method_reg, |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 400 | const std::vector<ManagedRegister>& callee_save_regs, |
Dmitry Petrochenko | fca8220 | 2014-03-21 11:21:37 +0700 | [diff] [blame] | 401 | const ManagedRegisterEntrySpills& entry_spills) = 0; |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 402 | |
| 403 | // Emit code that will remove an activation from the stack |
| 404 | virtual void RemoveFrame(size_t frame_size, |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 405 | const std::vector<ManagedRegister>& callee_save_regs) = 0; |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 406 | |
| 407 | virtual void IncreaseFrameSize(size_t adjust) = 0; |
| 408 | virtual void DecreaseFrameSize(size_t adjust) = 0; |
| 409 | |
| 410 | // Store routines |
| 411 | virtual void Store(FrameOffset offs, ManagedRegister src, size_t size) = 0; |
| 412 | virtual void StoreRef(FrameOffset dest, ManagedRegister src) = 0; |
| 413 | virtual void StoreRawPtr(FrameOffset dest, ManagedRegister src) = 0; |
| 414 | |
| 415 | virtual void StoreImmediateToFrame(FrameOffset dest, uint32_t imm, |
| 416 | ManagedRegister scratch) = 0; |
| 417 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 418 | virtual void StoreImmediateToThread32(ThreadOffset<4> dest, uint32_t imm, |
| 419 | ManagedRegister scratch); |
| 420 | virtual void StoreImmediateToThread64(ThreadOffset<8> dest, uint32_t imm, |
| 421 | ManagedRegister scratch); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 422 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 423 | virtual void StoreStackOffsetToThread32(ThreadOffset<4> thr_offs, |
| 424 | FrameOffset fr_offs, |
| 425 | ManagedRegister scratch); |
| 426 | virtual void StoreStackOffsetToThread64(ThreadOffset<8> thr_offs, |
| 427 | FrameOffset fr_offs, |
| 428 | ManagedRegister scratch); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 429 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 430 | virtual void StoreStackPointerToThread32(ThreadOffset<4> thr_offs); |
| 431 | virtual void StoreStackPointerToThread64(ThreadOffset<8> thr_offs); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 432 | |
| 433 | virtual void StoreSpanning(FrameOffset dest, ManagedRegister src, |
| 434 | FrameOffset in_off, ManagedRegister scratch) = 0; |
| 435 | |
| 436 | // Load routines |
| 437 | virtual void Load(ManagedRegister dest, FrameOffset src, size_t size) = 0; |
| 438 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 439 | virtual void LoadFromThread32(ManagedRegister dest, ThreadOffset<4> src, size_t size); |
| 440 | virtual void LoadFromThread64(ManagedRegister dest, ThreadOffset<8> src, size_t size); |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 441 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 442 | virtual void LoadRef(ManagedRegister dest, FrameOffset src) = 0; |
| 443 | // If poison_reference is true and kPoisonReference is true, then we negate the read reference. |
| 444 | virtual void LoadRef(ManagedRegister dest, ManagedRegister base, MemberOffset offs, |
| 445 | bool poison_reference) = 0; |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 446 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 447 | virtual void LoadRawPtr(ManagedRegister dest, ManagedRegister base, Offset offs) = 0; |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 448 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 449 | virtual void LoadRawPtrFromThread32(ManagedRegister dest, ThreadOffset<4> offs); |
| 450 | virtual void LoadRawPtrFromThread64(ManagedRegister dest, ThreadOffset<8> offs); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 451 | |
| 452 | // Copying routines |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 453 | virtual void Move(ManagedRegister dest, ManagedRegister src, size_t size) = 0; |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 454 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 455 | virtual void CopyRawPtrFromThread32(FrameOffset fr_offs, ThreadOffset<4> thr_offs, |
| 456 | ManagedRegister scratch); |
| 457 | virtual void CopyRawPtrFromThread64(FrameOffset fr_offs, ThreadOffset<8> thr_offs, |
| 458 | ManagedRegister scratch); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 459 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 460 | virtual void CopyRawPtrToThread32(ThreadOffset<4> thr_offs, FrameOffset fr_offs, |
| 461 | ManagedRegister scratch); |
| 462 | virtual void CopyRawPtrToThread64(ThreadOffset<8> thr_offs, FrameOffset fr_offs, |
| 463 | ManagedRegister scratch); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 464 | |
| 465 | virtual void CopyRef(FrameOffset dest, FrameOffset src, |
| 466 | ManagedRegister scratch) = 0; |
| 467 | |
Elliott Hughes | a09aea2 | 2012-01-06 18:58:27 -0800 | [diff] [blame] | 468 | virtual void Copy(FrameOffset dest, FrameOffset src, ManagedRegister scratch, size_t size) = 0; |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 469 | |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 470 | virtual void Copy(FrameOffset dest, ManagedRegister src_base, Offset src_offset, |
| 471 | ManagedRegister scratch, size_t size) = 0; |
| 472 | |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 473 | virtual void Copy(ManagedRegister dest_base, Offset dest_offset, FrameOffset src, |
| 474 | ManagedRegister scratch, size_t size) = 0; |
| 475 | |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 476 | virtual void Copy(FrameOffset dest, FrameOffset src_base, Offset src_offset, |
| 477 | ManagedRegister scratch, size_t size) = 0; |
| 478 | |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 479 | virtual void Copy(ManagedRegister dest, Offset dest_offset, |
| 480 | ManagedRegister src, Offset src_offset, |
| 481 | ManagedRegister scratch, size_t size) = 0; |
| 482 | |
| 483 | virtual void Copy(FrameOffset dest, Offset dest_offset, FrameOffset src, Offset src_offset, |
| 484 | ManagedRegister scratch, size_t size) = 0; |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 485 | |
Ian Rogers | e5de95b | 2011-09-18 20:31:38 -0700 | [diff] [blame] | 486 | virtual void MemoryBarrier(ManagedRegister scratch) = 0; |
| 487 | |
jeffhao | 58136ca | 2012-05-24 13:40:11 -0700 | [diff] [blame] | 488 | // Sign extension |
| 489 | virtual void SignExtend(ManagedRegister mreg, size_t size) = 0; |
| 490 | |
jeffhao | cee4d0c | 2012-06-15 14:42:01 -0700 | [diff] [blame] | 491 | // Zero extension |
| 492 | virtual void ZeroExtend(ManagedRegister mreg, size_t size) = 0; |
| 493 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 494 | // Exploit fast access in managed code to Thread::Current() |
| 495 | virtual void GetCurrentThread(ManagedRegister tr) = 0; |
| 496 | virtual void GetCurrentThread(FrameOffset dest_offset, |
| 497 | ManagedRegister scratch) = 0; |
| 498 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 499 | // Set up out_reg to hold a Object** into the handle scope, or to be null if the |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 500 | // value is null and null_allowed. in_reg holds a possibly stale reference |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 501 | // that can be used to avoid loading the handle scope entry to see if the value is |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 502 | // null. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 503 | virtual void CreateHandleScopeEntry(ManagedRegister out_reg, FrameOffset handlescope_offset, |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 504 | ManagedRegister in_reg, bool null_allowed) = 0; |
| 505 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 506 | // Set up out_off to hold a Object** into the handle scope, or to be null if the |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 507 | // value is null and null_allowed. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 508 | virtual void CreateHandleScopeEntry(FrameOffset out_off, FrameOffset handlescope_offset, |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 509 | ManagedRegister scratch, bool null_allowed) = 0; |
| 510 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 511 | // src holds a handle scope entry (Object**) load this into dst |
| 512 | virtual void LoadReferenceFromHandleScope(ManagedRegister dst, |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 513 | ManagedRegister src) = 0; |
| 514 | |
| 515 | // Heap::VerifyObject on src. In some cases (such as a reference to this) we |
| 516 | // know that src may not be null. |
| 517 | virtual void VerifyObject(ManagedRegister src, bool could_be_null) = 0; |
| 518 | virtual void VerifyObject(FrameOffset src, bool could_be_null) = 0; |
| 519 | |
| 520 | // Call to address held at [base+offset] |
| 521 | virtual void Call(ManagedRegister base, Offset offset, |
| 522 | ManagedRegister scratch) = 0; |
| 523 | virtual void Call(FrameOffset base, Offset offset, |
| 524 | ManagedRegister scratch) = 0; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 525 | virtual void CallFromThread32(ThreadOffset<4> offset, ManagedRegister scratch); |
| 526 | virtual void CallFromThread64(ThreadOffset<8> offset, ManagedRegister scratch); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 527 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 528 | // Generate code to check if Thread::Current()->exception_ is non-null |
| 529 | // and branch to a ExceptionSlowPath if it is. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 530 | virtual void ExceptionPoll(ManagedRegister scratch, size_t stack_adjust) = 0; |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 531 | |
| 532 | virtual ~Assembler() {} |
| 533 | |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 534 | /** |
| 535 | * @brief Buffer of DWARF's Call Frame Information opcodes. |
| 536 | * @details It is used by debuggers and other tools to unwind the call stack. |
| 537 | */ |
| 538 | DebugFrameOpCodeWriterForAssembler& cfi() { return cfi_; } |
| 539 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 540 | protected: |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 541 | Assembler() : buffer_(), cfi_(this) {} |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 542 | |
| 543 | AssemblerBuffer buffer_; |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 544 | |
| 545 | DebugFrameOpCodeWriterForAssembler cfi_; |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 546 | }; |
| 547 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 548 | } // namespace art |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 549 | |
Ian Rogers | 166db04 | 2013-07-26 12:05:57 -0700 | [diff] [blame] | 550 | #endif // ART_COMPILER_UTILS_ASSEMBLER_H_ |