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