Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | */ |
| 16 | |
| 17 | #include "assembler_thumb2.h" |
| 18 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 19 | #include "base/bit_utils.h" |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 20 | #include "base/logging.h" |
| 21 | #include "entrypoints/quick/quick_entrypoints.h" |
| 22 | #include "offsets.h" |
| 23 | #include "thread.h" |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | namespace arm { |
| 27 | |
Vladimir Marko | 6b756b5 | 2015-07-14 11:58:38 +0100 | [diff] [blame] | 28 | void Thumb2Assembler::Fixup::PrepareDependents(Thumb2Assembler* assembler) { |
| 29 | // For each Fixup, it's easy to find the Fixups that it depends on as they are either |
| 30 | // the following or the preceding Fixups until we find the target. However, for fixup |
| 31 | // adjustment we need the reverse lookup, i.e. what Fixups depend on a given Fixup. |
| 32 | // This function creates a compact representation of this relationship, where we have |
| 33 | // all the dependents in a single array and Fixups reference their ranges by start |
| 34 | // index and count. (Instead of having a per-fixup vector.) |
| 35 | |
| 36 | // Count the number of dependents of each Fixup. |
| 37 | const FixupId end_id = assembler->fixups_.size(); |
| 38 | Fixup* fixups = assembler->fixups_.data(); |
| 39 | for (FixupId fixup_id = 0u; fixup_id != end_id; ++fixup_id) { |
| 40 | uint32_t target = fixups[fixup_id].target_; |
| 41 | if (target > fixups[fixup_id].location_) { |
| 42 | for (FixupId id = fixup_id + 1u; id != end_id && fixups[id].location_ < target; ++id) { |
| 43 | fixups[id].dependents_count_ += 1u; |
| 44 | } |
| 45 | } else { |
| 46 | for (FixupId id = fixup_id; id != 0u && fixups[id - 1u].location_ >= target; --id) { |
| 47 | fixups[id - 1u].dependents_count_ += 1u; |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | // Assign index ranges in fixup_dependents_ to individual fixups. Record the end of the |
| 52 | // range in dependents_start_, we shall later decrement it as we fill in fixup_dependents_. |
| 53 | uint32_t number_of_dependents = 0u; |
| 54 | for (FixupId fixup_id = 0u; fixup_id != end_id; ++fixup_id) { |
| 55 | number_of_dependents += fixups[fixup_id].dependents_count_; |
| 56 | fixups[fixup_id].dependents_start_ = number_of_dependents; |
| 57 | } |
| 58 | if (number_of_dependents == 0u) { |
| 59 | return; |
| 60 | } |
| 61 | // Create and fill in the fixup_dependents_. |
| 62 | assembler->fixup_dependents_.reset(new FixupId[number_of_dependents]); |
| 63 | FixupId* dependents = assembler->fixup_dependents_.get(); |
| 64 | for (FixupId fixup_id = 0u; fixup_id != end_id; ++fixup_id) { |
| 65 | uint32_t target = fixups[fixup_id].target_; |
| 66 | if (target > fixups[fixup_id].location_) { |
| 67 | for (FixupId id = fixup_id + 1u; id != end_id && fixups[id].location_ < target; ++id) { |
| 68 | fixups[id].dependents_start_ -= 1u; |
| 69 | dependents[fixups[id].dependents_start_] = fixup_id; |
| 70 | } |
| 71 | } else { |
| 72 | for (FixupId id = fixup_id; id != 0u && fixups[id - 1u].location_ >= target; --id) { |
| 73 | fixups[id - 1u].dependents_start_ -= 1u; |
| 74 | dependents[fixups[id - 1u].dependents_start_] = fixup_id; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 80 | void Thumb2Assembler::BindLabel(Label* label, uint32_t bound_pc) { |
| 81 | CHECK(!label->IsBound()); |
| 82 | |
| 83 | while (label->IsLinked()) { |
| 84 | FixupId fixup_id = label->Position(); // The id for linked Fixup. |
| 85 | Fixup* fixup = GetFixup(fixup_id); // Get the Fixup at this id. |
| 86 | fixup->Resolve(bound_pc); // Fixup can be resolved now. |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 87 | uint32_t fixup_location = fixup->GetLocation(); |
| 88 | uint16_t next = buffer_.Load<uint16_t>(fixup_location); // Get next in chain. |
| 89 | buffer_.Store<int16_t>(fixup_location, 0); |
| 90 | label->position_ = next; // Move to next. |
| 91 | } |
| 92 | label->BindTo(bound_pc); |
| 93 | } |
| 94 | |
Andreas Gampe | 7cffc3b | 2015-10-19 21:31:53 -0700 | [diff] [blame] | 95 | uint32_t Thumb2Assembler::BindLiterals() { |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 96 | // We don't add the padding here, that's done only after adjusting the Fixup sizes. |
| 97 | uint32_t code_size = buffer_.Size(); |
| 98 | for (Literal& lit : literals_) { |
| 99 | Label* label = lit.GetLabel(); |
| 100 | BindLabel(label, code_size); |
| 101 | code_size += lit.GetSize(); |
| 102 | } |
Andreas Gampe | 7cffc3b | 2015-10-19 21:31:53 -0700 | [diff] [blame] | 103 | return code_size; |
| 104 | } |
| 105 | |
| 106 | void Thumb2Assembler::BindJumpTables(uint32_t code_size) { |
| 107 | for (JumpTable& table : jump_tables_) { |
| 108 | Label* label = table.GetLabel(); |
| 109 | BindLabel(label, code_size); |
| 110 | code_size += table.GetSize(); |
| 111 | } |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void Thumb2Assembler::AdjustFixupIfNeeded(Fixup* fixup, uint32_t* current_code_size, |
| 115 | std::deque<FixupId>* fixups_to_recalculate) { |
| 116 | uint32_t adjustment = fixup->AdjustSizeIfNeeded(*current_code_size); |
| 117 | if (adjustment != 0u) { |
| 118 | *current_code_size += adjustment; |
Vladimir Marko | 6b756b5 | 2015-07-14 11:58:38 +0100 | [diff] [blame] | 119 | for (FixupId dependent_id : fixup->Dependents(*this)) { |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 120 | Fixup* dependent = GetFixup(dependent_id); |
| 121 | dependent->IncreaseAdjustment(adjustment); |
| 122 | if (buffer_.Load<int16_t>(dependent->GetLocation()) == 0) { |
| 123 | buffer_.Store<int16_t>(dependent->GetLocation(), 1); |
| 124 | fixups_to_recalculate->push_back(dependent_id); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | uint32_t Thumb2Assembler::AdjustFixups() { |
Vladimir Marko | 6b756b5 | 2015-07-14 11:58:38 +0100 | [diff] [blame] | 131 | Fixup::PrepareDependents(this); |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 132 | uint32_t current_code_size = buffer_.Size(); |
| 133 | std::deque<FixupId> fixups_to_recalculate; |
| 134 | if (kIsDebugBuild) { |
| 135 | // We will use the placeholders in the buffer_ to mark whether the fixup has |
| 136 | // been added to the fixups_to_recalculate. Make sure we start with zeros. |
| 137 | for (Fixup& fixup : fixups_) { |
| 138 | CHECK_EQ(buffer_.Load<int16_t>(fixup.GetLocation()), 0); |
| 139 | } |
| 140 | } |
| 141 | for (Fixup& fixup : fixups_) { |
| 142 | AdjustFixupIfNeeded(&fixup, ¤t_code_size, &fixups_to_recalculate); |
| 143 | } |
| 144 | while (!fixups_to_recalculate.empty()) { |
Vladimir Marko | 663c934 | 2015-07-22 11:28:14 +0100 | [diff] [blame] | 145 | do { |
| 146 | // Pop the fixup. |
| 147 | FixupId fixup_id = fixups_to_recalculate.front(); |
| 148 | fixups_to_recalculate.pop_front(); |
| 149 | Fixup* fixup = GetFixup(fixup_id); |
| 150 | DCHECK_NE(buffer_.Load<int16_t>(fixup->GetLocation()), 0); |
| 151 | buffer_.Store<int16_t>(fixup->GetLocation(), 0); |
| 152 | // See if it needs adjustment. |
| 153 | AdjustFixupIfNeeded(fixup, ¤t_code_size, &fixups_to_recalculate); |
| 154 | } while (!fixups_to_recalculate.empty()); |
| 155 | |
Andreas Gampe | 7cffc3b | 2015-10-19 21:31:53 -0700 | [diff] [blame] | 156 | if ((current_code_size & 2) != 0 && (!literals_.empty() || !jump_tables_.empty())) { |
Vladimir Marko | 663c934 | 2015-07-22 11:28:14 +0100 | [diff] [blame] | 157 | // If we need to add padding before literals, this may just push some out of range, |
| 158 | // so recalculate all load literals. This makes up for the fact that we don't mark |
| 159 | // load literal as a dependency of all previous Fixups even though it actually is. |
| 160 | for (Fixup& fixup : fixups_) { |
| 161 | if (fixup.IsLoadLiteral()) { |
| 162 | AdjustFixupIfNeeded(&fixup, ¤t_code_size, &fixups_to_recalculate); |
| 163 | } |
| 164 | } |
| 165 | } |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 166 | } |
| 167 | if (kIsDebugBuild) { |
| 168 | // Check that no fixup is marked as being in fixups_to_recalculate anymore. |
| 169 | for (Fixup& fixup : fixups_) { |
| 170 | CHECK_EQ(buffer_.Load<int16_t>(fixup.GetLocation()), 0); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | // Adjust literal pool labels for padding. |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 175 | DCHECK_ALIGNED(current_code_size, 2); |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 176 | uint32_t literals_adjustment = current_code_size + (current_code_size & 2) - buffer_.Size(); |
| 177 | if (literals_adjustment != 0u) { |
| 178 | for (Literal& literal : literals_) { |
| 179 | Label* label = literal.GetLabel(); |
| 180 | DCHECK(label->IsBound()); |
| 181 | int old_position = label->Position(); |
| 182 | label->Reinitialize(); |
| 183 | label->BindTo(old_position + literals_adjustment); |
| 184 | } |
Andreas Gampe | 7cffc3b | 2015-10-19 21:31:53 -0700 | [diff] [blame] | 185 | for (JumpTable& table : jump_tables_) { |
| 186 | Label* label = table.GetLabel(); |
| 187 | DCHECK(label->IsBound()); |
| 188 | int old_position = label->Position(); |
| 189 | label->Reinitialize(); |
| 190 | label->BindTo(old_position + literals_adjustment); |
| 191 | } |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | return current_code_size; |
| 195 | } |
| 196 | |
| 197 | void Thumb2Assembler::EmitFixups(uint32_t adjusted_code_size) { |
| 198 | // Move non-fixup code to its final place and emit fixups. |
| 199 | // Process fixups in reverse order so that we don't repeatedly move the same data. |
| 200 | size_t src_end = buffer_.Size(); |
| 201 | size_t dest_end = adjusted_code_size; |
| 202 | buffer_.Resize(dest_end); |
| 203 | DCHECK_GE(dest_end, src_end); |
| 204 | for (auto i = fixups_.rbegin(), end = fixups_.rend(); i != end; ++i) { |
| 205 | Fixup* fixup = &*i; |
| 206 | if (fixup->GetOriginalSize() == fixup->GetSize()) { |
| 207 | // The size of this Fixup didn't change. To avoid moving the data |
| 208 | // in small chunks, emit the code to its original position. |
| 209 | fixup->Emit(&buffer_, adjusted_code_size); |
| 210 | fixup->Finalize(dest_end - src_end); |
| 211 | } else { |
| 212 | // Move the data between the end of the fixup and src_end to its final location. |
| 213 | size_t old_fixup_location = fixup->GetLocation(); |
| 214 | size_t src_begin = old_fixup_location + fixup->GetOriginalSizeInBytes(); |
| 215 | size_t data_size = src_end - src_begin; |
| 216 | size_t dest_begin = dest_end - data_size; |
| 217 | buffer_.Move(dest_begin, src_begin, data_size); |
| 218 | src_end = old_fixup_location; |
| 219 | dest_end = dest_begin - fixup->GetSizeInBytes(); |
| 220 | // Finalize the Fixup and emit the data to the new location. |
| 221 | fixup->Finalize(dest_end - src_end); |
| 222 | fixup->Emit(&buffer_, adjusted_code_size); |
| 223 | } |
| 224 | } |
| 225 | CHECK_EQ(src_end, dest_end); |
| 226 | } |
| 227 | |
| 228 | void Thumb2Assembler::EmitLiterals() { |
| 229 | if (!literals_.empty()) { |
| 230 | // Load literal instructions (LDR, LDRD, VLDR) require 4-byte alignment. |
| 231 | // We don't support byte and half-word literals. |
| 232 | uint32_t code_size = buffer_.Size(); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 233 | DCHECK_ALIGNED(code_size, 2); |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 234 | if ((code_size & 2u) != 0u) { |
| 235 | Emit16(0); |
| 236 | } |
| 237 | for (Literal& literal : literals_) { |
| 238 | AssemblerBuffer::EnsureCapacity ensured(&buffer_); |
| 239 | DCHECK_EQ(static_cast<size_t>(literal.GetLabel()->Position()), buffer_.Size()); |
| 240 | DCHECK(literal.GetSize() == 4u || literal.GetSize() == 8u); |
| 241 | for (size_t i = 0, size = literal.GetSize(); i != size; ++i) { |
| 242 | buffer_.Emit<uint8_t>(literal.GetData()[i]); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
Andreas Gampe | 7cffc3b | 2015-10-19 21:31:53 -0700 | [diff] [blame] | 248 | void Thumb2Assembler::EmitJumpTables() { |
| 249 | if (!jump_tables_.empty()) { |
| 250 | // Jump tables require 4 byte alignment. (We don't support byte and half-word jump tables.) |
| 251 | uint32_t code_size = buffer_.Size(); |
| 252 | DCHECK_ALIGNED(code_size, 2); |
| 253 | if ((code_size & 2u) != 0u) { |
| 254 | Emit16(0); |
| 255 | } |
| 256 | for (JumpTable& table : jump_tables_) { |
| 257 | // Bulk ensure capacity, as this may be large. |
| 258 | size_t orig_size = buffer_.Size(); |
| 259 | buffer_.ExtendCapacity(orig_size + table.GetSize()); |
| 260 | #ifndef NDEBUG |
| 261 | buffer_.has_ensured_capacity_ = true; |
| 262 | #endif |
| 263 | |
| 264 | DCHECK_EQ(static_cast<size_t>(table.GetLabel()->Position()), buffer_.Size()); |
| 265 | int32_t anchor_position = table.GetAnchorLabel()->Position() + 4; |
| 266 | |
| 267 | for (Label* target : table.GetData()) { |
| 268 | // Ensure that the label was tracked, so that it will have the right position. |
| 269 | DCHECK(std::find(tracked_labels_.begin(), tracked_labels_.end(), target) != |
| 270 | tracked_labels_.end()); |
| 271 | |
| 272 | int32_t offset = target->Position() - anchor_position; |
| 273 | buffer_.Emit<int32_t>(offset); |
| 274 | } |
| 275 | |
| 276 | #ifndef NDEBUG |
| 277 | buffer_.has_ensured_capacity_ = false; |
| 278 | #endif |
| 279 | size_t new_size = buffer_.Size(); |
| 280 | DCHECK_LE(new_size - orig_size, table.GetSize()); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 285 | void Thumb2Assembler::PatchCFI() { |
| 286 | if (cfi().NumberOfDelayedAdvancePCs() == 0u) { |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | typedef DebugFrameOpCodeWriterForAssembler::DelayedAdvancePC DelayedAdvancePC; |
| 291 | const auto data = cfi().ReleaseStreamAndPrepareForDelayedAdvancePC(); |
| 292 | const std::vector<uint8_t>& old_stream = data.first; |
| 293 | const std::vector<DelayedAdvancePC>& advances = data.second; |
| 294 | |
| 295 | // Refill our data buffer with patched opcodes. |
| 296 | cfi().ReserveCFIStream(old_stream.size() + advances.size() + 16); |
| 297 | size_t stream_pos = 0; |
| 298 | for (const DelayedAdvancePC& advance : advances) { |
| 299 | DCHECK_GE(advance.stream_pos, stream_pos); |
| 300 | // Copy old data up to the point where advance was issued. |
| 301 | cfi().AppendRawData(old_stream, stream_pos, advance.stream_pos); |
| 302 | stream_pos = advance.stream_pos; |
| 303 | // Insert the advance command with its final offset. |
| 304 | size_t final_pc = GetAdjustedPosition(advance.pc); |
| 305 | cfi().AdvancePC(final_pc); |
| 306 | } |
| 307 | // Copy the final segment if any. |
| 308 | cfi().AppendRawData(old_stream, stream_pos, old_stream.size()); |
| 309 | } |
| 310 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 311 | inline int16_t Thumb2Assembler::BEncoding16(int32_t offset, Condition cond) { |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 312 | DCHECK_ALIGNED(offset, 2); |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 313 | int16_t encoding = B15 | B14; |
| 314 | if (cond != AL) { |
| 315 | DCHECK(IsInt<9>(offset)); |
| 316 | encoding |= B12 | (static_cast<int32_t>(cond) << 8) | ((offset >> 1) & 0xff); |
| 317 | } else { |
| 318 | DCHECK(IsInt<12>(offset)); |
| 319 | encoding |= B13 | ((offset >> 1) & 0x7ff); |
| 320 | } |
| 321 | return encoding; |
| 322 | } |
| 323 | |
| 324 | inline int32_t Thumb2Assembler::BEncoding32(int32_t offset, Condition cond) { |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 325 | DCHECK_ALIGNED(offset, 2); |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 326 | int32_t s = (offset >> 31) & 1; // Sign bit. |
| 327 | int32_t encoding = B31 | B30 | B29 | B28 | B15 | |
| 328 | (s << 26) | // Sign bit goes to bit 26. |
| 329 | ((offset >> 1) & 0x7ff); // imm11 goes to bits 0-10. |
| 330 | if (cond != AL) { |
| 331 | DCHECK(IsInt<21>(offset)); |
| 332 | // Encode cond, move imm6 from bits 12-17 to bits 16-21 and move J1 and J2. |
| 333 | encoding |= (static_cast<int32_t>(cond) << 22) | ((offset & 0x3f000) << (16 - 12)) | |
| 334 | ((offset & (1 << 19)) >> (19 - 13)) | // Extract J1 from bit 19 to bit 13. |
| 335 | ((offset & (1 << 18)) >> (18 - 11)); // Extract J2 from bit 18 to bit 11. |
| 336 | } else { |
| 337 | DCHECK(IsInt<25>(offset)); |
| 338 | int32_t j1 = ((offset >> 23) ^ s ^ 1) & 1; // Calculate J1 from I1 extracted from bit 23. |
| 339 | int32_t j2 = ((offset >> 22)^ s ^ 1) & 1; // Calculate J2 from I2 extracted from bit 22. |
| 340 | // Move imm10 from bits 12-21 to bits 16-25 and add J1 and J2. |
| 341 | encoding |= B12 | ((offset & 0x3ff000) << (16 - 12)) | |
| 342 | (j1 << 13) | (j2 << 11); |
| 343 | } |
| 344 | return encoding; |
| 345 | } |
| 346 | |
| 347 | inline int16_t Thumb2Assembler::CbxzEncoding16(Register rn, int32_t offset, Condition cond) { |
| 348 | DCHECK(!IsHighRegister(rn)); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 349 | DCHECK_ALIGNED(offset, 2); |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 350 | DCHECK(IsUint<7>(offset)); |
| 351 | DCHECK(cond == EQ || cond == NE); |
| 352 | return B15 | B13 | B12 | B8 | (cond == NE ? B11 : 0) | static_cast<int32_t>(rn) | |
| 353 | ((offset & 0x3e) << (3 - 1)) | // Move imm5 from bits 1-5 to bits 3-7. |
| 354 | ((offset & 0x40) << (9 - 6)); // Move i from bit 6 to bit 11 |
| 355 | } |
| 356 | |
| 357 | inline int16_t Thumb2Assembler::CmpRnImm8Encoding16(Register rn, int32_t value) { |
| 358 | DCHECK(!IsHighRegister(rn)); |
| 359 | DCHECK(IsUint<8>(value)); |
| 360 | return B13 | B11 | (rn << 8) | value; |
| 361 | } |
| 362 | |
| 363 | inline int16_t Thumb2Assembler::AddRdnRmEncoding16(Register rdn, Register rm) { |
| 364 | // The high bit of rn is moved across 4-bit rm. |
| 365 | return B14 | B10 | (static_cast<int32_t>(rm) << 3) | |
| 366 | (static_cast<int32_t>(rdn) & 7) | ((static_cast<int32_t>(rdn) & 8) << 4); |
| 367 | } |
| 368 | |
| 369 | inline int32_t Thumb2Assembler::MovwEncoding32(Register rd, int32_t value) { |
| 370 | DCHECK(IsUint<16>(value)); |
| 371 | return B31 | B30 | B29 | B28 | B25 | B22 | |
| 372 | (static_cast<int32_t>(rd) << 8) | |
| 373 | ((value & 0xf000) << (16 - 12)) | // Move imm4 from bits 12-15 to bits 16-19. |
| 374 | ((value & 0x0800) << (26 - 11)) | // Move i from bit 11 to bit 26. |
| 375 | ((value & 0x0700) << (12 - 8)) | // Move imm3 from bits 8-10 to bits 12-14. |
| 376 | (value & 0xff); // Keep imm8 in bits 0-7. |
| 377 | } |
| 378 | |
| 379 | inline int32_t Thumb2Assembler::MovtEncoding32(Register rd, int32_t value) { |
| 380 | DCHECK_EQ(value & 0xffff, 0); |
| 381 | int32_t movw_encoding = MovwEncoding32(rd, (value >> 16) & 0xffff); |
| 382 | return movw_encoding | B25 | B23; |
| 383 | } |
| 384 | |
| 385 | inline int32_t Thumb2Assembler::MovModImmEncoding32(Register rd, int32_t value) { |
| 386 | uint32_t mod_imm = ModifiedImmediate(value); |
| 387 | DCHECK_NE(mod_imm, kInvalidModifiedImmediate); |
| 388 | return B31 | B30 | B29 | B28 | B22 | B19 | B18 | B17 | B16 | |
| 389 | (static_cast<int32_t>(rd) << 8) | static_cast<int32_t>(mod_imm); |
| 390 | } |
| 391 | |
| 392 | inline int16_t Thumb2Assembler::LdrLitEncoding16(Register rt, int32_t offset) { |
| 393 | DCHECK(!IsHighRegister(rt)); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 394 | DCHECK_ALIGNED(offset, 4); |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 395 | DCHECK(IsUint<10>(offset)); |
| 396 | return B14 | B11 | (static_cast<int32_t>(rt) << 8) | (offset >> 2); |
| 397 | } |
| 398 | |
| 399 | inline int32_t Thumb2Assembler::LdrLitEncoding32(Register rt, int32_t offset) { |
| 400 | // NOTE: We don't support negative offset, i.e. U=0 (B23). |
| 401 | return LdrRtRnImm12Encoding(rt, PC, offset); |
| 402 | } |
| 403 | |
| 404 | inline int32_t Thumb2Assembler::LdrdEncoding32(Register rt, Register rt2, Register rn, int32_t offset) { |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 405 | DCHECK_ALIGNED(offset, 4); |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 406 | CHECK(IsUint<10>(offset)); |
| 407 | return B31 | B30 | B29 | B27 | |
| 408 | B24 /* P = 1 */ | B23 /* U = 1 */ | B22 | 0 /* W = 0 */ | B20 | |
| 409 | (static_cast<int32_t>(rn) << 16) | (static_cast<int32_t>(rt) << 12) | |
| 410 | (static_cast<int32_t>(rt2) << 8) | (offset >> 2); |
| 411 | } |
| 412 | |
| 413 | inline int32_t Thumb2Assembler::VldrsEncoding32(SRegister sd, Register rn, int32_t offset) { |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 414 | DCHECK_ALIGNED(offset, 4); |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 415 | CHECK(IsUint<10>(offset)); |
| 416 | return B31 | B30 | B29 | B27 | B26 | B24 | |
| 417 | B23 /* U = 1 */ | B20 | B11 | B9 | |
| 418 | (static_cast<int32_t>(rn) << 16) | |
| 419 | ((static_cast<int32_t>(sd) & 0x01) << (22 - 0)) | // Move D from bit 0 to bit 22. |
| 420 | ((static_cast<int32_t>(sd) & 0x1e) << (12 - 1)) | // Move Vd from bits 1-4 to bits 12-15. |
| 421 | (offset >> 2); |
| 422 | } |
| 423 | |
| 424 | inline int32_t Thumb2Assembler::VldrdEncoding32(DRegister dd, Register rn, int32_t offset) { |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 425 | DCHECK_ALIGNED(offset, 4); |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 426 | CHECK(IsUint<10>(offset)); |
| 427 | return B31 | B30 | B29 | B27 | B26 | B24 | |
| 428 | B23 /* U = 1 */ | B20 | B11 | B9 | B8 | |
| 429 | (rn << 16) | |
| 430 | ((static_cast<int32_t>(dd) & 0x10) << (22 - 4)) | // Move D from bit 4 to bit 22. |
| 431 | ((static_cast<int32_t>(dd) & 0x0f) << (12 - 0)) | // Move Vd from bits 0-3 to bits 12-15. |
| 432 | (offset >> 2); |
| 433 | } |
| 434 | |
| 435 | inline int16_t Thumb2Assembler::LdrRtRnImm5Encoding16(Register rt, Register rn, int32_t offset) { |
| 436 | DCHECK(!IsHighRegister(rt)); |
| 437 | DCHECK(!IsHighRegister(rn)); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 438 | DCHECK_ALIGNED(offset, 4); |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 439 | DCHECK(IsUint<7>(offset)); |
| 440 | return B14 | B13 | B11 | |
| 441 | (static_cast<int32_t>(rn) << 3) | static_cast<int32_t>(rt) | |
| 442 | (offset << (6 - 2)); // Move imm5 from bits 2-6 to bits 6-10. |
| 443 | } |
| 444 | |
| 445 | int32_t Thumb2Assembler::Fixup::LoadWideOrFpEncoding(Register rbase, int32_t offset) const { |
| 446 | switch (type_) { |
| 447 | case kLoadLiteralWide: |
| 448 | return LdrdEncoding32(rn_, rt2_, rbase, offset); |
| 449 | case kLoadFPLiteralSingle: |
| 450 | return VldrsEncoding32(sd_, rbase, offset); |
| 451 | case kLoadFPLiteralDouble: |
| 452 | return VldrdEncoding32(dd_, rbase, offset); |
| 453 | default: |
| 454 | LOG(FATAL) << "Unexpected type: " << static_cast<int>(type_); |
| 455 | UNREACHABLE(); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | inline int32_t Thumb2Assembler::LdrRtRnImm12Encoding(Register rt, Register rn, int32_t offset) { |
| 460 | DCHECK(IsUint<12>(offset)); |
| 461 | return B31 | B30 | B29 | B28 | B27 | B23 | B22 | B20 | (rn << 16) | (rt << 12) | offset; |
| 462 | } |
| 463 | |
Andreas Gampe | 7cffc3b | 2015-10-19 21:31:53 -0700 | [diff] [blame] | 464 | inline int16_t Thumb2Assembler::AdrEncoding16(Register rd, int32_t offset) { |
| 465 | DCHECK(IsUint<10>(offset)); |
| 466 | DCHECK(IsAligned<4>(offset)); |
| 467 | DCHECK(!IsHighRegister(rd)); |
| 468 | return B15 | B13 | (rd << 8) | (offset >> 2); |
| 469 | } |
| 470 | |
| 471 | inline int32_t Thumb2Assembler::AdrEncoding32(Register rd, int32_t offset) { |
| 472 | DCHECK(IsUint<12>(offset)); |
| 473 | // Bit 26: offset[11] |
| 474 | // Bits 14-12: offset[10-8] |
| 475 | // Bits 7-0: offset[7-0] |
| 476 | int32_t immediate_mask = |
| 477 | ((offset & (1 << 11)) << (26 - 11)) | |
| 478 | ((offset & (7 << 8)) << (12 - 8)) | |
| 479 | (offset & 0xFF); |
| 480 | return B31 | B30 | B29 | B28 | B25 | B19 | B18 | B17 | B16 | (rd << 8) | immediate_mask; |
| 481 | } |
| 482 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 483 | void Thumb2Assembler::FinalizeCode() { |
| 484 | ArmAssembler::FinalizeCode(); |
Andreas Gampe | 7cffc3b | 2015-10-19 21:31:53 -0700 | [diff] [blame] | 485 | uint32_t size_after_literals = BindLiterals(); |
| 486 | BindJumpTables(size_after_literals); |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 487 | uint32_t adjusted_code_size = AdjustFixups(); |
| 488 | EmitFixups(adjusted_code_size); |
| 489 | EmitLiterals(); |
Andreas Gampe | 7cffc3b | 2015-10-19 21:31:53 -0700 | [diff] [blame] | 490 | FinalizeTrackedLabels(); |
| 491 | EmitJumpTables(); |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 492 | PatchCFI(); |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Nicolas Geoffray | 5bd05a5 | 2015-10-13 09:48:30 +0100 | [diff] [blame] | 495 | bool Thumb2Assembler::ShifterOperandCanAlwaysHold(uint32_t immediate) { |
| 496 | return ArmAssembler::ModifiedImmediate(immediate) != kInvalidModifiedImmediate; |
| 497 | } |
| 498 | |
Nicolas Geoffray | 3d1e788 | 2015-02-03 13:59:52 +0000 | [diff] [blame] | 499 | bool Thumb2Assembler::ShifterOperandCanHold(Register rd ATTRIBUTE_UNUSED, |
| 500 | Register rn ATTRIBUTE_UNUSED, |
Nicolas Geoffray | 3bcc8ea | 2014-11-28 15:00:02 +0000 | [diff] [blame] | 501 | Opcode opcode, |
| 502 | uint32_t immediate, |
| 503 | ShifterOperand* shifter_op) { |
| 504 | shifter_op->type_ = ShifterOperand::kImmediate; |
| 505 | shifter_op->immed_ = immediate; |
| 506 | shifter_op->is_shift_ = false; |
| 507 | shifter_op->is_rotate_ = false; |
| 508 | switch (opcode) { |
| 509 | case ADD: |
| 510 | case SUB: |
Nicolas Geoffray | 3bcc8ea | 2014-11-28 15:00:02 +0000 | [diff] [blame] | 511 | if (immediate < (1 << 12)) { // Less than (or equal to) 12 bits can always be done. |
| 512 | return true; |
| 513 | } |
| 514 | return ArmAssembler::ModifiedImmediate(immediate) != kInvalidModifiedImmediate; |
| 515 | |
| 516 | case MOV: |
| 517 | // TODO: Support less than or equal to 12bits. |
| 518 | return ArmAssembler::ModifiedImmediate(immediate) != kInvalidModifiedImmediate; |
Vladimir Marko | d2b4ca2 | 2015-09-14 15:13:26 +0100 | [diff] [blame] | 519 | |
Nicolas Geoffray | 3bcc8ea | 2014-11-28 15:00:02 +0000 | [diff] [blame] | 520 | case MVN: |
| 521 | default: |
| 522 | return ArmAssembler::ModifiedImmediate(immediate) != kInvalidModifiedImmediate; |
| 523 | } |
| 524 | } |
| 525 | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 526 | void Thumb2Assembler::and_(Register rd, Register rn, const ShifterOperand& so, |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 527 | Condition cond, SetCc set_cc) { |
| 528 | EmitDataProcessing(cond, AND, set_cc, rn, rd, so); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | |
| 532 | void Thumb2Assembler::eor(Register rd, Register rn, const ShifterOperand& so, |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 533 | Condition cond, SetCc set_cc) { |
| 534 | EmitDataProcessing(cond, EOR, set_cc, rn, rd, so); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | |
| 538 | void Thumb2Assembler::sub(Register rd, Register rn, const ShifterOperand& so, |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 539 | Condition cond, SetCc set_cc) { |
| 540 | EmitDataProcessing(cond, SUB, set_cc, rn, rd, so); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | |
| 544 | void Thumb2Assembler::rsb(Register rd, Register rn, const ShifterOperand& so, |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 545 | Condition cond, SetCc set_cc) { |
| 546 | EmitDataProcessing(cond, RSB, set_cc, rn, rd, so); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | |
| 550 | void Thumb2Assembler::add(Register rd, Register rn, const ShifterOperand& so, |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 551 | Condition cond, SetCc set_cc) { |
| 552 | EmitDataProcessing(cond, ADD, set_cc, rn, rd, so); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | |
| 556 | void Thumb2Assembler::adc(Register rd, Register rn, const ShifterOperand& so, |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 557 | Condition cond, SetCc set_cc) { |
| 558 | EmitDataProcessing(cond, ADC, set_cc, rn, rd, so); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | |
| 562 | void Thumb2Assembler::sbc(Register rd, Register rn, const ShifterOperand& so, |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 563 | Condition cond, SetCc set_cc) { |
| 564 | EmitDataProcessing(cond, SBC, set_cc, rn, rd, so); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | |
| 568 | void Thumb2Assembler::rsc(Register rd, Register rn, const ShifterOperand& so, |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 569 | Condition cond, SetCc set_cc) { |
| 570 | EmitDataProcessing(cond, RSC, set_cc, rn, rd, so); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | |
| 574 | void Thumb2Assembler::tst(Register rn, const ShifterOperand& so, Condition cond) { |
| 575 | CHECK_NE(rn, PC); // Reserve tst pc instruction for exception handler marker. |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 576 | EmitDataProcessing(cond, TST, kCcSet, rn, R0, so); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | |
| 580 | void Thumb2Assembler::teq(Register rn, const ShifterOperand& so, Condition cond) { |
| 581 | CHECK_NE(rn, PC); // Reserve teq pc instruction for exception handler marker. |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 582 | EmitDataProcessing(cond, TEQ, kCcSet, rn, R0, so); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | |
| 586 | void Thumb2Assembler::cmp(Register rn, const ShifterOperand& so, Condition cond) { |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 587 | EmitDataProcessing(cond, CMP, kCcSet, rn, R0, so); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | |
| 591 | void Thumb2Assembler::cmn(Register rn, const ShifterOperand& so, Condition cond) { |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 592 | EmitDataProcessing(cond, CMN, kCcSet, rn, R0, so); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 596 | void Thumb2Assembler::orr(Register rd, Register rn, const ShifterOperand& so, |
| 597 | Condition cond, SetCc set_cc) { |
| 598 | EmitDataProcessing(cond, ORR, set_cc, rn, rd, so); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | |
Vladimir Marko | d2b4ca2 | 2015-09-14 15:13:26 +0100 | [diff] [blame] | 602 | void Thumb2Assembler::orn(Register rd, Register rn, const ShifterOperand& so, |
| 603 | Condition cond, SetCc set_cc) { |
| 604 | EmitDataProcessing(cond, ORN, set_cc, rn, rd, so); |
| 605 | } |
| 606 | |
| 607 | |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 608 | void Thumb2Assembler::mov(Register rd, const ShifterOperand& so, |
| 609 | Condition cond, SetCc set_cc) { |
| 610 | EmitDataProcessing(cond, MOV, set_cc, R0, rd, so); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | |
| 614 | void Thumb2Assembler::bic(Register rd, Register rn, const ShifterOperand& so, |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 615 | Condition cond, SetCc set_cc) { |
| 616 | EmitDataProcessing(cond, BIC, set_cc, rn, rd, so); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 620 | void Thumb2Assembler::mvn(Register rd, const ShifterOperand& so, |
| 621 | Condition cond, SetCc set_cc) { |
| 622 | EmitDataProcessing(cond, MVN, set_cc, R0, rd, so); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | |
| 626 | void Thumb2Assembler::mul(Register rd, Register rn, Register rm, Condition cond) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 627 | CheckCondition(cond); |
| 628 | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 629 | if (rd == rm && !IsHighRegister(rd) && !IsHighRegister(rn) && !force_32bit_) { |
| 630 | // 16 bit. |
| 631 | int16_t encoding = B14 | B9 | B8 | B6 | |
| 632 | rn << 3 | rd; |
| 633 | Emit16(encoding); |
| 634 | } else { |
| 635 | // 32 bit. |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 636 | uint32_t op1 = 0U /* 0b000 */; |
| 637 | uint32_t op2 = 0U /* 0b00 */; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 638 | int32_t encoding = B31 | B30 | B29 | B28 | B27 | B25 | B24 | |
| 639 | op1 << 20 | |
| 640 | B15 | B14 | B13 | B12 | |
| 641 | op2 << 4 | |
| 642 | static_cast<uint32_t>(rd) << 8 | |
| 643 | static_cast<uint32_t>(rn) << 16 | |
| 644 | static_cast<uint32_t>(rm); |
| 645 | |
| 646 | Emit32(encoding); |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | |
| 651 | void Thumb2Assembler::mla(Register rd, Register rn, Register rm, Register ra, |
| 652 | Condition cond) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 653 | CheckCondition(cond); |
| 654 | |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 655 | uint32_t op1 = 0U /* 0b000 */; |
| 656 | uint32_t op2 = 0U /* 0b00 */; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 657 | int32_t encoding = B31 | B30 | B29 | B28 | B27 | B25 | B24 | |
| 658 | op1 << 20 | |
| 659 | op2 << 4 | |
| 660 | static_cast<uint32_t>(rd) << 8 | |
| 661 | static_cast<uint32_t>(ra) << 12 | |
| 662 | static_cast<uint32_t>(rn) << 16 | |
| 663 | static_cast<uint32_t>(rm); |
| 664 | |
| 665 | Emit32(encoding); |
| 666 | } |
| 667 | |
| 668 | |
| 669 | void Thumb2Assembler::mls(Register rd, Register rn, Register rm, Register ra, |
| 670 | Condition cond) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 671 | CheckCondition(cond); |
| 672 | |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 673 | uint32_t op1 = 0U /* 0b000 */; |
| 674 | uint32_t op2 = 01 /* 0b01 */; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 675 | int32_t encoding = B31 | B30 | B29 | B28 | B27 | B25 | B24 | |
| 676 | op1 << 20 | |
| 677 | op2 << 4 | |
| 678 | static_cast<uint32_t>(rd) << 8 | |
| 679 | static_cast<uint32_t>(ra) << 12 | |
| 680 | static_cast<uint32_t>(rn) << 16 | |
| 681 | static_cast<uint32_t>(rm); |
| 682 | |
| 683 | Emit32(encoding); |
| 684 | } |
| 685 | |
| 686 | |
Zheng Xu | c666710 | 2015-05-15 16:08:45 +0800 | [diff] [blame] | 687 | void Thumb2Assembler::smull(Register rd_lo, Register rd_hi, Register rn, |
| 688 | Register rm, Condition cond) { |
| 689 | CheckCondition(cond); |
| 690 | |
| 691 | uint32_t op1 = 0U /* 0b000; */; |
| 692 | uint32_t op2 = 0U /* 0b0000 */; |
| 693 | int32_t encoding = B31 | B30 | B29 | B28 | B27 | B25 | B24 | B23 | |
| 694 | op1 << 20 | |
| 695 | op2 << 4 | |
| 696 | static_cast<uint32_t>(rd_lo) << 12 | |
| 697 | static_cast<uint32_t>(rd_hi) << 8 | |
| 698 | static_cast<uint32_t>(rn) << 16 | |
| 699 | static_cast<uint32_t>(rm); |
| 700 | |
| 701 | Emit32(encoding); |
| 702 | } |
| 703 | |
| 704 | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 705 | void Thumb2Assembler::umull(Register rd_lo, Register rd_hi, Register rn, |
| 706 | Register rm, Condition cond) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 707 | CheckCondition(cond); |
| 708 | |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 709 | uint32_t op1 = 2U /* 0b010; */; |
| 710 | uint32_t op2 = 0U /* 0b0000 */; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 711 | int32_t encoding = B31 | B30 | B29 | B28 | B27 | B25 | B24 | B23 | |
| 712 | op1 << 20 | |
| 713 | op2 << 4 | |
| 714 | static_cast<uint32_t>(rd_lo) << 12 | |
| 715 | static_cast<uint32_t>(rd_hi) << 8 | |
| 716 | static_cast<uint32_t>(rn) << 16 | |
| 717 | static_cast<uint32_t>(rm); |
| 718 | |
| 719 | Emit32(encoding); |
| 720 | } |
| 721 | |
| 722 | |
| 723 | void Thumb2Assembler::sdiv(Register rd, Register rn, Register rm, Condition cond) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 724 | CheckCondition(cond); |
| 725 | |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 726 | uint32_t op1 = 1U /* 0b001 */; |
| 727 | uint32_t op2 = 15U /* 0b1111 */; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 728 | int32_t encoding = B31 | B30 | B29 | B28 | B27 | B25 | B24 | B23 | B20 | |
| 729 | op1 << 20 | |
| 730 | op2 << 4 | |
| 731 | 0xf << 12 | |
| 732 | static_cast<uint32_t>(rd) << 8 | |
| 733 | static_cast<uint32_t>(rn) << 16 | |
| 734 | static_cast<uint32_t>(rm); |
| 735 | |
| 736 | Emit32(encoding); |
| 737 | } |
| 738 | |
| 739 | |
| 740 | void Thumb2Assembler::udiv(Register rd, Register rn, Register rm, Condition cond) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 741 | CheckCondition(cond); |
| 742 | |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 743 | uint32_t op1 = 1U /* 0b001 */; |
| 744 | uint32_t op2 = 15U /* 0b1111 */; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 745 | int32_t encoding = B31 | B30 | B29 | B28 | B27 | B25 | B24 | B23 | B21 | B20 | |
| 746 | op1 << 20 | |
| 747 | op2 << 4 | |
| 748 | 0xf << 12 | |
| 749 | static_cast<uint32_t>(rd) << 8 | |
| 750 | static_cast<uint32_t>(rn) << 16 | |
| 751 | static_cast<uint32_t>(rm); |
| 752 | |
| 753 | Emit32(encoding); |
| 754 | } |
| 755 | |
| 756 | |
Roland Levillain | 51d3fc4 | 2014-11-13 14:11:42 +0000 | [diff] [blame] | 757 | void Thumb2Assembler::sbfx(Register rd, Register rn, uint32_t lsb, uint32_t width, Condition cond) { |
| 758 | CheckCondition(cond); |
| 759 | CHECK_LE(lsb, 31U); |
| 760 | CHECK(1U <= width && width <= 32U) << width; |
| 761 | uint32_t widthminus1 = width - 1; |
| 762 | uint32_t imm2 = lsb & (B1 | B0); // Bits 0-1 of `lsb`. |
| 763 | uint32_t imm3 = (lsb & (B4 | B3 | B2)) >> 2; // Bits 2-4 of `lsb`. |
| 764 | |
| 765 | uint32_t op = 20U /* 0b10100 */; |
| 766 | int32_t encoding = B31 | B30 | B29 | B28 | B25 | |
| 767 | op << 20 | |
| 768 | static_cast<uint32_t>(rn) << 16 | |
| 769 | imm3 << 12 | |
| 770 | static_cast<uint32_t>(rd) << 8 | |
| 771 | imm2 << 6 | |
| 772 | widthminus1; |
| 773 | |
| 774 | Emit32(encoding); |
| 775 | } |
| 776 | |
| 777 | |
Roland Levillain | 981e454 | 2014-11-14 11:47:14 +0000 | [diff] [blame] | 778 | void Thumb2Assembler::ubfx(Register rd, Register rn, uint32_t lsb, uint32_t width, Condition cond) { |
| 779 | CheckCondition(cond); |
| 780 | CHECK_LE(lsb, 31U); |
| 781 | CHECK(1U <= width && width <= 32U) << width; |
| 782 | uint32_t widthminus1 = width - 1; |
| 783 | uint32_t imm2 = lsb & (B1 | B0); // Bits 0-1 of `lsb`. |
| 784 | uint32_t imm3 = (lsb & (B4 | B3 | B2)) >> 2; // Bits 2-4 of `lsb`. |
| 785 | |
| 786 | uint32_t op = 28U /* 0b11100 */; |
| 787 | int32_t encoding = B31 | B30 | B29 | B28 | B25 | |
| 788 | op << 20 | |
| 789 | static_cast<uint32_t>(rn) << 16 | |
| 790 | imm3 << 12 | |
| 791 | static_cast<uint32_t>(rd) << 8 | |
| 792 | imm2 << 6 | |
| 793 | widthminus1; |
| 794 | |
| 795 | Emit32(encoding); |
| 796 | } |
| 797 | |
| 798 | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 799 | void Thumb2Assembler::ldr(Register rd, const Address& ad, Condition cond) { |
| 800 | EmitLoadStore(cond, true, false, false, false, rd, ad); |
| 801 | } |
| 802 | |
| 803 | |
| 804 | void Thumb2Assembler::str(Register rd, const Address& ad, Condition cond) { |
| 805 | EmitLoadStore(cond, false, false, false, false, rd, ad); |
| 806 | } |
| 807 | |
| 808 | |
| 809 | void Thumb2Assembler::ldrb(Register rd, const Address& ad, Condition cond) { |
| 810 | EmitLoadStore(cond, true, true, false, false, rd, ad); |
| 811 | } |
| 812 | |
| 813 | |
| 814 | void Thumb2Assembler::strb(Register rd, const Address& ad, Condition cond) { |
| 815 | EmitLoadStore(cond, false, true, false, false, rd, ad); |
| 816 | } |
| 817 | |
| 818 | |
| 819 | void Thumb2Assembler::ldrh(Register rd, const Address& ad, Condition cond) { |
| 820 | EmitLoadStore(cond, true, false, true, false, rd, ad); |
| 821 | } |
| 822 | |
| 823 | |
| 824 | void Thumb2Assembler::strh(Register rd, const Address& ad, Condition cond) { |
| 825 | EmitLoadStore(cond, false, false, true, false, rd, ad); |
| 826 | } |
| 827 | |
| 828 | |
| 829 | void Thumb2Assembler::ldrsb(Register rd, const Address& ad, Condition cond) { |
| 830 | EmitLoadStore(cond, true, true, false, true, rd, ad); |
| 831 | } |
| 832 | |
| 833 | |
| 834 | void Thumb2Assembler::ldrsh(Register rd, const Address& ad, Condition cond) { |
| 835 | EmitLoadStore(cond, true, false, true, true, rd, ad); |
| 836 | } |
| 837 | |
| 838 | |
| 839 | void Thumb2Assembler::ldrd(Register rd, const Address& ad, Condition cond) { |
Roland Levillain | 4af147e | 2015-04-07 13:54:49 +0100 | [diff] [blame] | 840 | ldrd(rd, Register(rd + 1), ad, cond); |
| 841 | } |
| 842 | |
| 843 | |
| 844 | void Thumb2Assembler::ldrd(Register rd, Register rd2, const Address& ad, Condition cond) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 845 | CheckCondition(cond); |
Roland Levillain | 4af147e | 2015-04-07 13:54:49 +0100 | [diff] [blame] | 846 | // Encoding T1. |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 847 | // This is different from other loads. The encoding is like ARM. |
| 848 | int32_t encoding = B31 | B30 | B29 | B27 | B22 | B20 | |
| 849 | static_cast<int32_t>(rd) << 12 | |
Roland Levillain | 4af147e | 2015-04-07 13:54:49 +0100 | [diff] [blame] | 850 | static_cast<int32_t>(rd2) << 8 | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 851 | ad.encodingThumbLdrdStrd(); |
| 852 | Emit32(encoding); |
| 853 | } |
| 854 | |
| 855 | |
| 856 | void Thumb2Assembler::strd(Register rd, const Address& ad, Condition cond) { |
Roland Levillain | 4af147e | 2015-04-07 13:54:49 +0100 | [diff] [blame] | 857 | strd(rd, Register(rd + 1), ad, cond); |
| 858 | } |
| 859 | |
| 860 | |
| 861 | void Thumb2Assembler::strd(Register rd, Register rd2, const Address& ad, Condition cond) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 862 | CheckCondition(cond); |
Roland Levillain | 4af147e | 2015-04-07 13:54:49 +0100 | [diff] [blame] | 863 | // Encoding T1. |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 864 | // This is different from other loads. The encoding is like ARM. |
| 865 | int32_t encoding = B31 | B30 | B29 | B27 | B22 | |
| 866 | static_cast<int32_t>(rd) << 12 | |
Roland Levillain | 4af147e | 2015-04-07 13:54:49 +0100 | [diff] [blame] | 867 | static_cast<int32_t>(rd2) << 8 | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 868 | ad.encodingThumbLdrdStrd(); |
| 869 | Emit32(encoding); |
| 870 | } |
| 871 | |
| 872 | |
| 873 | void Thumb2Assembler::ldm(BlockAddressMode am, |
| 874 | Register base, |
| 875 | RegList regs, |
| 876 | Condition cond) { |
Vladimir Marko | e8469c1 | 2014-11-26 18:09:30 +0000 | [diff] [blame] | 877 | CHECK_NE(regs, 0u); // Do not use ldm if there's nothing to load. |
| 878 | if (IsPowerOfTwo(regs)) { |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 879 | // Thumb doesn't support one reg in the list. |
| 880 | // Find the register number. |
Vladimir Marko | e8469c1 | 2014-11-26 18:09:30 +0000 | [diff] [blame] | 881 | int reg = CTZ(static_cast<uint32_t>(regs)); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 882 | CHECK_LT(reg, 16); |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 883 | CHECK(am == DB_W); // Only writeback is supported. |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 884 | ldr(static_cast<Register>(reg), Address(base, kRegisterSize, Address::PostIndex), cond); |
| 885 | } else { |
| 886 | EmitMultiMemOp(cond, am, true, base, regs); |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | |
| 891 | void Thumb2Assembler::stm(BlockAddressMode am, |
| 892 | Register base, |
| 893 | RegList regs, |
| 894 | Condition cond) { |
Vladimir Marko | e8469c1 | 2014-11-26 18:09:30 +0000 | [diff] [blame] | 895 | CHECK_NE(regs, 0u); // Do not use stm if there's nothing to store. |
| 896 | if (IsPowerOfTwo(regs)) { |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 897 | // Thumb doesn't support one reg in the list. |
| 898 | // Find the register number. |
Vladimir Marko | e8469c1 | 2014-11-26 18:09:30 +0000 | [diff] [blame] | 899 | int reg = CTZ(static_cast<uint32_t>(regs)); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 900 | CHECK_LT(reg, 16); |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 901 | CHECK(am == IA || am == IA_W); |
| 902 | Address::Mode strmode = am == IA ? Address::PreIndex : Address::Offset; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 903 | str(static_cast<Register>(reg), Address(base, -kRegisterSize, strmode), cond); |
| 904 | } else { |
| 905 | EmitMultiMemOp(cond, am, false, base, regs); |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | |
| 910 | bool Thumb2Assembler::vmovs(SRegister sd, float s_imm, Condition cond) { |
| 911 | uint32_t imm32 = bit_cast<uint32_t, float>(s_imm); |
| 912 | if (((imm32 & ((1 << 19) - 1)) == 0) && |
| 913 | ((((imm32 >> 25) & ((1 << 6) - 1)) == (1 << 5)) || |
| 914 | (((imm32 >> 25) & ((1 << 6) - 1)) == ((1 << 5) -1)))) { |
| 915 | uint8_t imm8 = ((imm32 >> 31) << 7) | (((imm32 >> 29) & 1) << 6) | |
| 916 | ((imm32 >> 19) & ((1 << 6) -1)); |
| 917 | EmitVFPsss(cond, B23 | B21 | B20 | ((imm8 >> 4)*B16) | (imm8 & 0xf), |
| 918 | sd, S0, S0); |
| 919 | return true; |
| 920 | } |
| 921 | return false; |
| 922 | } |
| 923 | |
| 924 | |
| 925 | bool Thumb2Assembler::vmovd(DRegister dd, double d_imm, Condition cond) { |
| 926 | uint64_t imm64 = bit_cast<uint64_t, double>(d_imm); |
| 927 | if (((imm64 & ((1LL << 48) - 1)) == 0) && |
| 928 | ((((imm64 >> 54) & ((1 << 9) - 1)) == (1 << 8)) || |
| 929 | (((imm64 >> 54) & ((1 << 9) - 1)) == ((1 << 8) -1)))) { |
| 930 | uint8_t imm8 = ((imm64 >> 63) << 7) | (((imm64 >> 61) & 1) << 6) | |
| 931 | ((imm64 >> 48) & ((1 << 6) -1)); |
| 932 | EmitVFPddd(cond, B23 | B21 | B20 | ((imm8 >> 4)*B16) | B8 | (imm8 & 0xf), |
| 933 | dd, D0, D0); |
| 934 | return true; |
| 935 | } |
| 936 | return false; |
| 937 | } |
| 938 | |
| 939 | |
| 940 | void Thumb2Assembler::vmovs(SRegister sd, SRegister sm, Condition cond) { |
| 941 | EmitVFPsss(cond, B23 | B21 | B20 | B6, sd, S0, sm); |
| 942 | } |
| 943 | |
| 944 | |
| 945 | void Thumb2Assembler::vmovd(DRegister dd, DRegister dm, Condition cond) { |
| 946 | EmitVFPddd(cond, B23 | B21 | B20 | B6, dd, D0, dm); |
| 947 | } |
| 948 | |
| 949 | |
| 950 | void Thumb2Assembler::vadds(SRegister sd, SRegister sn, SRegister sm, |
| 951 | Condition cond) { |
| 952 | EmitVFPsss(cond, B21 | B20, sd, sn, sm); |
| 953 | } |
| 954 | |
| 955 | |
| 956 | void Thumb2Assembler::vaddd(DRegister dd, DRegister dn, DRegister dm, |
| 957 | Condition cond) { |
| 958 | EmitVFPddd(cond, B21 | B20, dd, dn, dm); |
| 959 | } |
| 960 | |
| 961 | |
| 962 | void Thumb2Assembler::vsubs(SRegister sd, SRegister sn, SRegister sm, |
| 963 | Condition cond) { |
| 964 | EmitVFPsss(cond, B21 | B20 | B6, sd, sn, sm); |
| 965 | } |
| 966 | |
| 967 | |
| 968 | void Thumb2Assembler::vsubd(DRegister dd, DRegister dn, DRegister dm, |
| 969 | Condition cond) { |
| 970 | EmitVFPddd(cond, B21 | B20 | B6, dd, dn, dm); |
| 971 | } |
| 972 | |
| 973 | |
| 974 | void Thumb2Assembler::vmuls(SRegister sd, SRegister sn, SRegister sm, |
| 975 | Condition cond) { |
| 976 | EmitVFPsss(cond, B21, sd, sn, sm); |
| 977 | } |
| 978 | |
| 979 | |
| 980 | void Thumb2Assembler::vmuld(DRegister dd, DRegister dn, DRegister dm, |
| 981 | Condition cond) { |
| 982 | EmitVFPddd(cond, B21, dd, dn, dm); |
| 983 | } |
| 984 | |
| 985 | |
| 986 | void Thumb2Assembler::vmlas(SRegister sd, SRegister sn, SRegister sm, |
| 987 | Condition cond) { |
| 988 | EmitVFPsss(cond, 0, sd, sn, sm); |
| 989 | } |
| 990 | |
| 991 | |
| 992 | void Thumb2Assembler::vmlad(DRegister dd, DRegister dn, DRegister dm, |
| 993 | Condition cond) { |
| 994 | EmitVFPddd(cond, 0, dd, dn, dm); |
| 995 | } |
| 996 | |
| 997 | |
| 998 | void Thumb2Assembler::vmlss(SRegister sd, SRegister sn, SRegister sm, |
| 999 | Condition cond) { |
| 1000 | EmitVFPsss(cond, B6, sd, sn, sm); |
| 1001 | } |
| 1002 | |
| 1003 | |
| 1004 | void Thumb2Assembler::vmlsd(DRegister dd, DRegister dn, DRegister dm, |
| 1005 | Condition cond) { |
| 1006 | EmitVFPddd(cond, B6, dd, dn, dm); |
| 1007 | } |
| 1008 | |
| 1009 | |
| 1010 | void Thumb2Assembler::vdivs(SRegister sd, SRegister sn, SRegister sm, |
| 1011 | Condition cond) { |
| 1012 | EmitVFPsss(cond, B23, sd, sn, sm); |
| 1013 | } |
| 1014 | |
| 1015 | |
| 1016 | void Thumb2Assembler::vdivd(DRegister dd, DRegister dn, DRegister dm, |
| 1017 | Condition cond) { |
| 1018 | EmitVFPddd(cond, B23, dd, dn, dm); |
| 1019 | } |
| 1020 | |
| 1021 | |
| 1022 | void Thumb2Assembler::vabss(SRegister sd, SRegister sm, Condition cond) { |
| 1023 | EmitVFPsss(cond, B23 | B21 | B20 | B7 | B6, sd, S0, sm); |
| 1024 | } |
| 1025 | |
| 1026 | |
| 1027 | void Thumb2Assembler::vabsd(DRegister dd, DRegister dm, Condition cond) { |
| 1028 | EmitVFPddd(cond, B23 | B21 | B20 | B7 | B6, dd, D0, dm); |
| 1029 | } |
| 1030 | |
| 1031 | |
| 1032 | void Thumb2Assembler::vnegs(SRegister sd, SRegister sm, Condition cond) { |
| 1033 | EmitVFPsss(cond, B23 | B21 | B20 | B16 | B6, sd, S0, sm); |
| 1034 | } |
| 1035 | |
| 1036 | |
| 1037 | void Thumb2Assembler::vnegd(DRegister dd, DRegister dm, Condition cond) { |
| 1038 | EmitVFPddd(cond, B23 | B21 | B20 | B16 | B6, dd, D0, dm); |
| 1039 | } |
| 1040 | |
| 1041 | |
| 1042 | void Thumb2Assembler::vsqrts(SRegister sd, SRegister sm, Condition cond) { |
| 1043 | EmitVFPsss(cond, B23 | B21 | B20 | B16 | B7 | B6, sd, S0, sm); |
| 1044 | } |
| 1045 | |
| 1046 | void Thumb2Assembler::vsqrtd(DRegister dd, DRegister dm, Condition cond) { |
| 1047 | EmitVFPddd(cond, B23 | B21 | B20 | B16 | B7 | B6, dd, D0, dm); |
| 1048 | } |
| 1049 | |
| 1050 | |
| 1051 | void Thumb2Assembler::vcvtsd(SRegister sd, DRegister dm, Condition cond) { |
| 1052 | EmitVFPsd(cond, B23 | B21 | B20 | B18 | B17 | B16 | B8 | B7 | B6, sd, dm); |
| 1053 | } |
| 1054 | |
| 1055 | |
| 1056 | void Thumb2Assembler::vcvtds(DRegister dd, SRegister sm, Condition cond) { |
| 1057 | EmitVFPds(cond, B23 | B21 | B20 | B18 | B17 | B16 | B7 | B6, dd, sm); |
| 1058 | } |
| 1059 | |
| 1060 | |
| 1061 | void Thumb2Assembler::vcvtis(SRegister sd, SRegister sm, Condition cond) { |
| 1062 | EmitVFPsss(cond, B23 | B21 | B20 | B19 | B18 | B16 | B7 | B6, sd, S0, sm); |
| 1063 | } |
| 1064 | |
| 1065 | |
| 1066 | void Thumb2Assembler::vcvtid(SRegister sd, DRegister dm, Condition cond) { |
| 1067 | EmitVFPsd(cond, B23 | B21 | B20 | B19 | B18 | B16 | B8 | B7 | B6, sd, dm); |
| 1068 | } |
| 1069 | |
| 1070 | |
| 1071 | void Thumb2Assembler::vcvtsi(SRegister sd, SRegister sm, Condition cond) { |
| 1072 | EmitVFPsss(cond, B23 | B21 | B20 | B19 | B7 | B6, sd, S0, sm); |
| 1073 | } |
| 1074 | |
| 1075 | |
| 1076 | void Thumb2Assembler::vcvtdi(DRegister dd, SRegister sm, Condition cond) { |
| 1077 | EmitVFPds(cond, B23 | B21 | B20 | B19 | B8 | B7 | B6, dd, sm); |
| 1078 | } |
| 1079 | |
| 1080 | |
| 1081 | void Thumb2Assembler::vcvtus(SRegister sd, SRegister sm, Condition cond) { |
| 1082 | EmitVFPsss(cond, B23 | B21 | B20 | B19 | B18 | B7 | B6, sd, S0, sm); |
| 1083 | } |
| 1084 | |
| 1085 | |
| 1086 | void Thumb2Assembler::vcvtud(SRegister sd, DRegister dm, Condition cond) { |
| 1087 | EmitVFPsd(cond, B23 | B21 | B20 | B19 | B18 | B8 | B7 | B6, sd, dm); |
| 1088 | } |
| 1089 | |
| 1090 | |
| 1091 | void Thumb2Assembler::vcvtsu(SRegister sd, SRegister sm, Condition cond) { |
| 1092 | EmitVFPsss(cond, B23 | B21 | B20 | B19 | B6, sd, S0, sm); |
| 1093 | } |
| 1094 | |
| 1095 | |
| 1096 | void Thumb2Assembler::vcvtdu(DRegister dd, SRegister sm, Condition cond) { |
| 1097 | EmitVFPds(cond, B23 | B21 | B20 | B19 | B8 | B6, dd, sm); |
| 1098 | } |
| 1099 | |
| 1100 | |
| 1101 | void Thumb2Assembler::vcmps(SRegister sd, SRegister sm, Condition cond) { |
| 1102 | EmitVFPsss(cond, B23 | B21 | B20 | B18 | B6, sd, S0, sm); |
| 1103 | } |
| 1104 | |
| 1105 | |
| 1106 | void Thumb2Assembler::vcmpd(DRegister dd, DRegister dm, Condition cond) { |
| 1107 | EmitVFPddd(cond, B23 | B21 | B20 | B18 | B6, dd, D0, dm); |
| 1108 | } |
| 1109 | |
| 1110 | |
| 1111 | void Thumb2Assembler::vcmpsz(SRegister sd, Condition cond) { |
| 1112 | EmitVFPsss(cond, B23 | B21 | B20 | B18 | B16 | B6, sd, S0, S0); |
| 1113 | } |
| 1114 | |
| 1115 | |
| 1116 | void Thumb2Assembler::vcmpdz(DRegister dd, Condition cond) { |
| 1117 | EmitVFPddd(cond, B23 | B21 | B20 | B18 | B16 | B6, dd, D0, D0); |
| 1118 | } |
| 1119 | |
| 1120 | void Thumb2Assembler::b(Label* label, Condition cond) { |
agicsaki | e2142d25 | 2015-06-30 17:10:03 -0700 | [diff] [blame] | 1121 | DCHECK_EQ(next_condition_, AL); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1122 | EmitBranch(cond, label, false, false); |
| 1123 | } |
| 1124 | |
| 1125 | |
| 1126 | void Thumb2Assembler::bl(Label* label, Condition cond) { |
| 1127 | CheckCondition(cond); |
| 1128 | EmitBranch(cond, label, true, false); |
| 1129 | } |
| 1130 | |
| 1131 | |
| 1132 | void Thumb2Assembler::blx(Label* label) { |
| 1133 | EmitBranch(AL, label, true, true); |
| 1134 | } |
| 1135 | |
| 1136 | |
| 1137 | void Thumb2Assembler::MarkExceptionHandler(Label* label) { |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1138 | EmitDataProcessing(AL, TST, kCcSet, PC, R0, ShifterOperand(0)); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1139 | Label l; |
| 1140 | b(&l); |
| 1141 | EmitBranch(AL, label, false, false); |
| 1142 | Bind(&l); |
| 1143 | } |
| 1144 | |
| 1145 | |
| 1146 | void Thumb2Assembler::Emit32(int32_t value) { |
| 1147 | AssemblerBuffer::EnsureCapacity ensured(&buffer_); |
| 1148 | buffer_.Emit<int16_t>(value >> 16); |
| 1149 | buffer_.Emit<int16_t>(value & 0xffff); |
| 1150 | } |
| 1151 | |
| 1152 | |
| 1153 | void Thumb2Assembler::Emit16(int16_t value) { |
| 1154 | AssemblerBuffer::EnsureCapacity ensured(&buffer_); |
| 1155 | buffer_.Emit<int16_t>(value); |
| 1156 | } |
| 1157 | |
| 1158 | |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1159 | bool Thumb2Assembler::Is32BitDataProcessing(Condition cond, |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1160 | Opcode opcode, |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1161 | SetCc set_cc, |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1162 | Register rn, |
| 1163 | Register rd, |
| 1164 | const ShifterOperand& so) { |
| 1165 | if (force_32bit_) { |
| 1166 | return true; |
| 1167 | } |
| 1168 | |
Vladimir Marko | 5bc561c | 2014-12-16 17:41:59 +0000 | [diff] [blame] | 1169 | // Check special case for SP relative ADD and SUB immediate. |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1170 | if ((opcode == ADD || opcode == SUB) && rn == SP && so.IsImmediate() && set_cc != kCcSet) { |
Vladimir Marko | 5bc561c | 2014-12-16 17:41:59 +0000 | [diff] [blame] | 1171 | // If the immediate is in range, use 16 bit. |
| 1172 | if (rd == SP) { |
| 1173 | if (so.GetImmediate() < (1 << 9)) { // 9 bit immediate. |
| 1174 | return false; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1175 | } |
Vladimir Marko | 5bc561c | 2014-12-16 17:41:59 +0000 | [diff] [blame] | 1176 | } else if (!IsHighRegister(rd) && opcode == ADD) { |
| 1177 | if (so.GetImmediate() < (1 << 10)) { // 10 bit immediate. |
| 1178 | return false; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1179 | } |
| 1180 | } |
Vladimir Marko | 5bc561c | 2014-12-16 17:41:59 +0000 | [diff] [blame] | 1181 | } |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1182 | |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1183 | bool can_contain_high_register = |
| 1184 | (opcode == CMP) || |
| 1185 | (opcode == MOV && set_cc != kCcSet) || |
| 1186 | ((opcode == ADD) && (rn == rd) && set_cc != kCcSet); |
Vladimir Marko | 5bc561c | 2014-12-16 17:41:59 +0000 | [diff] [blame] | 1187 | |
| 1188 | if (IsHighRegister(rd) || IsHighRegister(rn)) { |
| 1189 | if (!can_contain_high_register) { |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1190 | return true; |
| 1191 | } |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 1192 | |
Vladimir Marko | 5bc561c | 2014-12-16 17:41:59 +0000 | [diff] [blame] | 1193 | // There are high register instructions available for this opcode. |
| 1194 | // However, there is no actual shift available, neither for ADD nor for MOV (ASR/LSR/LSL/ROR). |
| 1195 | if (so.IsShift() && (so.GetShift() == RRX || so.GetImmediate() != 0u)) { |
| 1196 | return true; |
| 1197 | } |
| 1198 | |
| 1199 | // The ADD and MOV instructions that work with high registers don't have 16-bit |
| 1200 | // immediate variants. |
| 1201 | if (so.IsImmediate()) { |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 1202 | return true; |
| 1203 | } |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | if (so.IsRegister() && IsHighRegister(so.GetRegister()) && !can_contain_high_register) { |
| 1207 | return true; |
| 1208 | } |
| 1209 | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1210 | bool rn_is_valid = true; |
| 1211 | |
| 1212 | // Check for single operand instructions and ADD/SUB. |
| 1213 | switch (opcode) { |
| 1214 | case CMP: |
| 1215 | case MOV: |
| 1216 | case TST: |
| 1217 | case MVN: |
| 1218 | rn_is_valid = false; // There is no Rn for these instructions. |
| 1219 | break; |
| 1220 | case TEQ: |
Vladimir Marko | d2b4ca2 | 2015-09-14 15:13:26 +0100 | [diff] [blame] | 1221 | case ORN: |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1222 | return true; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1223 | case ADD: |
| 1224 | case SUB: |
| 1225 | break; |
| 1226 | default: |
| 1227 | if (so.IsRegister() && rd != rn) { |
| 1228 | return true; |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | if (so.IsImmediate()) { |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1233 | if (opcode == RSB) { |
| 1234 | DCHECK(rn_is_valid); |
| 1235 | if (so.GetImmediate() != 0u) { |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1236 | return true; |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1237 | } |
| 1238 | } else if (rn_is_valid && rn != rd) { |
| 1239 | // The only thumb1 instructions with a register and an immediate are ADD and SUB |
| 1240 | // with a 3-bit immediate, and RSB with zero immediate. |
| 1241 | if (opcode == ADD || opcode == SUB) { |
| 1242 | if (!IsUint<3>(so.GetImmediate())) { |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1243 | return true; |
| 1244 | } |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1245 | } else { |
| 1246 | return true; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1247 | } |
| 1248 | } else { |
| 1249 | // ADD, SUB, CMP and MOV may be thumb1 only if the immediate is 8 bits. |
| 1250 | if (!(opcode == ADD || opcode == SUB || opcode == MOV || opcode == CMP)) { |
| 1251 | return true; |
| 1252 | } else { |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1253 | if (!IsUint<8>(so.GetImmediate())) { |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1254 | return true; |
| 1255 | } |
| 1256 | } |
| 1257 | } |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1258 | } else { |
| 1259 | DCHECK(so.IsRegister()); |
| 1260 | if (so.IsShift()) { |
| 1261 | // Shift operand - check if it is a MOV convertible to a 16-bit shift instruction. |
| 1262 | if (opcode != MOV) { |
Zheng Xu | c666710 | 2015-05-15 16:08:45 +0800 | [diff] [blame] | 1263 | return true; |
| 1264 | } |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1265 | // Check for MOV with an ROR/RRX. There is no 16-bit ROR immediate and no 16-bit RRX. |
| 1266 | if (so.GetShift() == ROR || so.GetShift() == RRX) { |
| 1267 | return true; |
| 1268 | } |
| 1269 | // 16-bit shifts set condition codes if and only if outside IT block, |
| 1270 | // i.e. if and only if cond == AL. |
| 1271 | if ((cond == AL) ? set_cc == kCcKeep : set_cc == kCcSet) { |
| 1272 | return true; |
| 1273 | } |
| 1274 | } else { |
| 1275 | // Register operand without shift. |
| 1276 | switch (opcode) { |
| 1277 | case ADD: |
| 1278 | // The 16-bit ADD that cannot contain high registers can set condition codes |
| 1279 | // if and only if outside IT block, i.e. if and only if cond == AL. |
| 1280 | if (!can_contain_high_register && |
| 1281 | ((cond == AL) ? set_cc == kCcKeep : set_cc == kCcSet)) { |
| 1282 | return true; |
| 1283 | } |
| 1284 | break; |
| 1285 | case AND: |
| 1286 | case BIC: |
| 1287 | case EOR: |
| 1288 | case ORR: |
| 1289 | case MVN: |
| 1290 | case ADC: |
| 1291 | case SUB: |
| 1292 | case SBC: |
| 1293 | // These 16-bit opcodes set condition codes if and only if outside IT block, |
| 1294 | // i.e. if and only if cond == AL. |
| 1295 | if ((cond == AL) ? set_cc == kCcKeep : set_cc == kCcSet) { |
| 1296 | return true; |
| 1297 | } |
| 1298 | break; |
| 1299 | case RSB: |
| 1300 | case RSC: |
| 1301 | // No 16-bit RSB/RSC Rd, Rm, Rn. It would be equivalent to SUB/SBC Rd, Rn, Rm. |
| 1302 | return true; |
| 1303 | case CMP: |
| 1304 | default: |
| 1305 | break; |
| 1306 | } |
Zheng Xu | c666710 | 2015-05-15 16:08:45 +0800 | [diff] [blame] | 1307 | } |
| 1308 | } |
| 1309 | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1310 | // The instruction can be encoded in 16 bits. |
| 1311 | return false; |
| 1312 | } |
| 1313 | |
| 1314 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1315 | void Thumb2Assembler::Emit32BitDataProcessing(Condition cond ATTRIBUTE_UNUSED, |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1316 | Opcode opcode, |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1317 | SetCc set_cc, |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1318 | Register rn, |
| 1319 | Register rd, |
| 1320 | const ShifterOperand& so) { |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1321 | uint8_t thumb_opcode = 255U /* 0b11111111 */; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1322 | switch (opcode) { |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1323 | case AND: thumb_opcode = 0U /* 0b0000 */; break; |
| 1324 | case EOR: thumb_opcode = 4U /* 0b0100 */; break; |
| 1325 | case SUB: thumb_opcode = 13U /* 0b1101 */; break; |
| 1326 | case RSB: thumb_opcode = 14U /* 0b1110 */; break; |
| 1327 | case ADD: thumb_opcode = 8U /* 0b1000 */; break; |
Andreas Gampe | 35c68e3 | 2014-09-30 08:39:37 -0700 | [diff] [blame] | 1328 | case ADC: thumb_opcode = 10U /* 0b1010 */; break; |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1329 | case SBC: thumb_opcode = 11U /* 0b1011 */; break; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1330 | case RSC: break; |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1331 | case TST: thumb_opcode = 0U /* 0b0000 */; DCHECK(set_cc == kCcSet); rd = PC; break; |
| 1332 | case TEQ: thumb_opcode = 4U /* 0b0100 */; DCHECK(set_cc == kCcSet); rd = PC; break; |
| 1333 | case CMP: thumb_opcode = 13U /* 0b1101 */; DCHECK(set_cc == kCcSet); rd = PC; break; |
| 1334 | case CMN: thumb_opcode = 8U /* 0b1000 */; DCHECK(set_cc == kCcSet); rd = PC; break; |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1335 | case ORR: thumb_opcode = 2U /* 0b0010 */; break; |
| 1336 | case MOV: thumb_opcode = 2U /* 0b0010 */; rn = PC; break; |
| 1337 | case BIC: thumb_opcode = 1U /* 0b0001 */; break; |
| 1338 | case MVN: thumb_opcode = 3U /* 0b0011 */; rn = PC; break; |
Vladimir Marko | d2b4ca2 | 2015-09-14 15:13:26 +0100 | [diff] [blame] | 1339 | case ORN: thumb_opcode = 3U /* 0b0011 */; break; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1340 | default: |
| 1341 | break; |
| 1342 | } |
| 1343 | |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1344 | if (thumb_opcode == 255U /* 0b11111111 */) { |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1345 | LOG(FATAL) << "Invalid thumb2 opcode " << opcode; |
Vladimir Marko | e8469c1 | 2014-11-26 18:09:30 +0000 | [diff] [blame] | 1346 | UNREACHABLE(); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | int32_t encoding = 0; |
| 1350 | if (so.IsImmediate()) { |
| 1351 | // Check special cases. |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 1352 | if ((opcode == SUB || opcode == ADD) && (so.GetImmediate() < (1u << 12))) { |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1353 | if (set_cc != kCcSet) { |
Guillaume "Vermeille" Sanchez | dc62c48 | 2015-03-11 14:30:31 +0000 | [diff] [blame] | 1354 | if (opcode == SUB) { |
| 1355 | thumb_opcode = 5U; |
| 1356 | } else if (opcode == ADD) { |
| 1357 | thumb_opcode = 0U; |
| 1358 | } |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1359 | } |
| 1360 | uint32_t imm = so.GetImmediate(); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1361 | |
| 1362 | uint32_t i = (imm >> 11) & 1; |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1363 | uint32_t imm3 = (imm >> 8) & 7U /* 0b111 */; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1364 | uint32_t imm8 = imm & 0xff; |
| 1365 | |
Guillaume "Vermeille" Sanchez | dc62c48 | 2015-03-11 14:30:31 +0000 | [diff] [blame] | 1366 | encoding = B31 | B30 | B29 | B28 | |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1367 | (set_cc == kCcSet ? B20 : B25) | |
Guillaume "Vermeille" Sanchez | dc62c48 | 2015-03-11 14:30:31 +0000 | [diff] [blame] | 1368 | thumb_opcode << 21 | |
| 1369 | rn << 16 | |
| 1370 | rd << 8 | |
| 1371 | i << 26 | |
| 1372 | imm3 << 12 | |
| 1373 | imm8; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1374 | } else { |
| 1375 | // Modified immediate. |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 1376 | uint32_t imm = ModifiedImmediate(so.encodingThumb()); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1377 | if (imm == kInvalidModifiedImmediate) { |
| 1378 | LOG(FATAL) << "Immediate value cannot fit in thumb2 modified immediate"; |
Vladimir Marko | e8469c1 | 2014-11-26 18:09:30 +0000 | [diff] [blame] | 1379 | UNREACHABLE(); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1380 | } |
| 1381 | encoding = B31 | B30 | B29 | B28 | |
| 1382 | thumb_opcode << 21 | |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1383 | (set_cc == kCcSet ? B20 : 0) | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1384 | rn << 16 | |
| 1385 | rd << 8 | |
| 1386 | imm; |
| 1387 | } |
| 1388 | } else if (so.IsRegister()) { |
Guillaume "Vermeille" Sanchez | dc62c48 | 2015-03-11 14:30:31 +0000 | [diff] [blame] | 1389 | // Register (possibly shifted) |
| 1390 | encoding = B31 | B30 | B29 | B27 | B25 | |
| 1391 | thumb_opcode << 21 | |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1392 | (set_cc == kCcSet ? B20 : 0) | |
Guillaume "Vermeille" Sanchez | dc62c48 | 2015-03-11 14:30:31 +0000 | [diff] [blame] | 1393 | rn << 16 | |
| 1394 | rd << 8 | |
| 1395 | so.encodingThumb(); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1396 | } |
| 1397 | Emit32(encoding); |
| 1398 | } |
| 1399 | |
| 1400 | |
| 1401 | void Thumb2Assembler::Emit16BitDataProcessing(Condition cond, |
| 1402 | Opcode opcode, |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1403 | SetCc set_cc, |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1404 | Register rn, |
| 1405 | Register rd, |
| 1406 | const ShifterOperand& so) { |
| 1407 | if (opcode == ADD || opcode == SUB) { |
| 1408 | Emit16BitAddSub(cond, opcode, set_cc, rn, rd, so); |
| 1409 | return; |
| 1410 | } |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1411 | uint8_t thumb_opcode = 255U /* 0b11111111 */; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1412 | // Thumb1. |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1413 | uint8_t dp_opcode = 1U /* 0b01 */; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1414 | uint8_t opcode_shift = 6; |
| 1415 | uint8_t rd_shift = 0; |
| 1416 | uint8_t rn_shift = 3; |
| 1417 | uint8_t immediate_shift = 0; |
| 1418 | bool use_immediate = false; |
| 1419 | uint8_t immediate = 0; |
| 1420 | |
| 1421 | if (opcode == MOV && so.IsRegister() && so.IsShift()) { |
| 1422 | // Convert shifted mov operand2 into 16 bit opcodes. |
| 1423 | dp_opcode = 0; |
| 1424 | opcode_shift = 11; |
| 1425 | |
| 1426 | use_immediate = true; |
| 1427 | immediate = so.GetImmediate(); |
| 1428 | immediate_shift = 6; |
| 1429 | |
| 1430 | rn = so.GetRegister(); |
| 1431 | |
| 1432 | switch (so.GetShift()) { |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1433 | case LSL: |
| 1434 | DCHECK_LE(immediate, 31u); |
| 1435 | thumb_opcode = 0U /* 0b00 */; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1436 | break; |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1437 | case LSR: |
| 1438 | DCHECK(1 <= immediate && immediate <= 32); |
| 1439 | immediate &= 31; // 32 is encoded as 0. |
| 1440 | thumb_opcode = 1U /* 0b01 */; |
| 1441 | break; |
| 1442 | case ASR: |
| 1443 | DCHECK(1 <= immediate && immediate <= 32); |
| 1444 | immediate &= 31; // 32 is encoded as 0. |
| 1445 | thumb_opcode = 2U /* 0b10 */; |
| 1446 | break; |
| 1447 | case ROR: // No 16-bit ROR immediate. |
| 1448 | case RRX: // No 16-bit RRX. |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1449 | default: |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1450 | LOG(FATAL) << "Unexpected shift: " << so.GetShift(); |
| 1451 | UNREACHABLE(); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1452 | } |
| 1453 | } else { |
| 1454 | if (so.IsImmediate()) { |
| 1455 | use_immediate = true; |
| 1456 | immediate = so.GetImmediate(); |
Andreas Gampe | 513ea0c | 2015-02-02 13:17:52 -0800 | [diff] [blame] | 1457 | } else { |
Guillaume "Vermeille" Sanchez | ab4a2f5 | 2015-03-11 14:00:30 +0000 | [diff] [blame] | 1458 | CHECK(!(so.IsRegister() && so.IsShift() && so.GetSecondRegister() != kNoRegister)) |
| 1459 | << "No register-shifted register instruction available in thumb"; |
Andreas Gampe | 513ea0c | 2015-02-02 13:17:52 -0800 | [diff] [blame] | 1460 | // Adjust rn and rd: only two registers will be emitted. |
| 1461 | switch (opcode) { |
| 1462 | case AND: |
| 1463 | case ORR: |
| 1464 | case EOR: |
| 1465 | case RSB: |
| 1466 | case ADC: |
| 1467 | case SBC: |
| 1468 | case BIC: { |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1469 | // Sets condition codes if and only if outside IT block, |
| 1470 | // check that it complies with set_cc. |
| 1471 | DCHECK((cond == AL) ? set_cc != kCcKeep : set_cc != kCcSet); |
Andreas Gampe | 513ea0c | 2015-02-02 13:17:52 -0800 | [diff] [blame] | 1472 | if (rn == rd) { |
| 1473 | rn = so.GetRegister(); |
| 1474 | } else { |
| 1475 | CHECK_EQ(rd, so.GetRegister()); |
| 1476 | } |
| 1477 | break; |
| 1478 | } |
| 1479 | case CMP: |
| 1480 | case CMN: { |
| 1481 | CHECK_EQ(rd, 0); |
| 1482 | rd = rn; |
| 1483 | rn = so.GetRegister(); |
| 1484 | break; |
| 1485 | } |
| 1486 | case MVN: { |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1487 | // Sets condition codes if and only if outside IT block, |
| 1488 | // check that it complies with set_cc. |
| 1489 | DCHECK((cond == AL) ? set_cc != kCcKeep : set_cc != kCcSet); |
| 1490 | CHECK_EQ(rn, 0); |
| 1491 | rn = so.GetRegister(); |
| 1492 | break; |
| 1493 | } |
| 1494 | case TST: |
| 1495 | case TEQ: { |
| 1496 | DCHECK(set_cc == kCcSet); |
Andreas Gampe | 513ea0c | 2015-02-02 13:17:52 -0800 | [diff] [blame] | 1497 | CHECK_EQ(rn, 0); |
| 1498 | rn = so.GetRegister(); |
| 1499 | break; |
| 1500 | } |
| 1501 | default: |
| 1502 | break; |
| 1503 | } |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | switch (opcode) { |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1507 | case AND: thumb_opcode = 0U /* 0b0000 */; break; |
Andreas Gampe | 513ea0c | 2015-02-02 13:17:52 -0800 | [diff] [blame] | 1508 | case ORR: thumb_opcode = 12U /* 0b1100 */; break; |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1509 | case EOR: thumb_opcode = 1U /* 0b0001 */; break; |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1510 | case RSB: thumb_opcode = 9U /* 0b1001 */; break; |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1511 | case ADC: thumb_opcode = 5U /* 0b0101 */; break; |
| 1512 | case SBC: thumb_opcode = 6U /* 0b0110 */; break; |
Andreas Gampe | 513ea0c | 2015-02-02 13:17:52 -0800 | [diff] [blame] | 1513 | case BIC: thumb_opcode = 14U /* 0b1110 */; break; |
| 1514 | case TST: thumb_opcode = 8U /* 0b1000 */; CHECK(!use_immediate); break; |
| 1515 | case MVN: thumb_opcode = 15U /* 0b1111 */; CHECK(!use_immediate); break; |
| 1516 | case CMP: { |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1517 | DCHECK(set_cc == kCcSet); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1518 | if (use_immediate) { |
| 1519 | // T2 encoding. |
Andreas Gampe | 513ea0c | 2015-02-02 13:17:52 -0800 | [diff] [blame] | 1520 | dp_opcode = 0; |
| 1521 | opcode_shift = 11; |
| 1522 | thumb_opcode = 5U /* 0b101 */; |
| 1523 | rd_shift = 8; |
| 1524 | rn_shift = 8; |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1525 | } else if (IsHighRegister(rd) || IsHighRegister(rn)) { |
| 1526 | // Special cmp for high registers. |
| 1527 | dp_opcode = 1U /* 0b01 */; |
| 1528 | opcode_shift = 7; |
| 1529 | // Put the top bit of rd into the bottom bit of the opcode. |
| 1530 | thumb_opcode = 10U /* 0b0001010 */ | static_cast<uint32_t>(rd) >> 3; |
| 1531 | rd = static_cast<Register>(static_cast<uint32_t>(rd) & 7U /* 0b111 */); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1532 | } else { |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1533 | thumb_opcode = 10U /* 0b1010 */; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | break; |
Andreas Gampe | 513ea0c | 2015-02-02 13:17:52 -0800 | [diff] [blame] | 1537 | } |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 1538 | case CMN: { |
Andreas Gampe | 513ea0c | 2015-02-02 13:17:52 -0800 | [diff] [blame] | 1539 | CHECK(!use_immediate); |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1540 | thumb_opcode = 11U /* 0b1011 */; |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 1541 | break; |
| 1542 | } |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1543 | case MOV: |
| 1544 | dp_opcode = 0; |
| 1545 | if (use_immediate) { |
| 1546 | // T2 encoding. |
| 1547 | opcode_shift = 11; |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1548 | thumb_opcode = 4U /* 0b100 */; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1549 | rd_shift = 8; |
| 1550 | rn_shift = 8; |
| 1551 | } else { |
| 1552 | rn = so.GetRegister(); |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1553 | if (set_cc != kCcSet) { |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1554 | // Special mov for high registers. |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1555 | dp_opcode = 1U /* 0b01 */; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1556 | opcode_shift = 7; |
| 1557 | // Put the top bit of rd into the bottom bit of the opcode. |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1558 | thumb_opcode = 12U /* 0b0001100 */ | static_cast<uint32_t>(rd) >> 3; |
| 1559 | rd = static_cast<Register>(static_cast<uint32_t>(rd) & 7U /* 0b111 */); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1560 | } else { |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1561 | DCHECK(!IsHighRegister(rn)); |
| 1562 | DCHECK(!IsHighRegister(rd)); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1563 | thumb_opcode = 0; |
| 1564 | } |
| 1565 | } |
| 1566 | break; |
Andreas Gampe | 513ea0c | 2015-02-02 13:17:52 -0800 | [diff] [blame] | 1567 | |
| 1568 | case TEQ: |
| 1569 | case RSC: |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1570 | default: |
Andreas Gampe | 513ea0c | 2015-02-02 13:17:52 -0800 | [diff] [blame] | 1571 | LOG(FATAL) << "Invalid thumb1 opcode " << opcode; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1572 | break; |
| 1573 | } |
| 1574 | } |
| 1575 | |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1576 | if (thumb_opcode == 255U /* 0b11111111 */) { |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1577 | LOG(FATAL) << "Invalid thumb1 opcode " << opcode; |
Vladimir Marko | e8469c1 | 2014-11-26 18:09:30 +0000 | [diff] [blame] | 1578 | UNREACHABLE(); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | int16_t encoding = dp_opcode << 14 | |
| 1582 | (thumb_opcode << opcode_shift) | |
| 1583 | rd << rd_shift | |
| 1584 | rn << rn_shift | |
| 1585 | (use_immediate ? (immediate << immediate_shift) : 0); |
| 1586 | |
| 1587 | Emit16(encoding); |
| 1588 | } |
| 1589 | |
| 1590 | |
| 1591 | // ADD and SUB are complex enough to warrant their own emitter. |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1592 | void Thumb2Assembler::Emit16BitAddSub(Condition cond, |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1593 | Opcode opcode, |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1594 | SetCc set_cc, |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1595 | Register rn, |
| 1596 | Register rd, |
| 1597 | const ShifterOperand& so) { |
| 1598 | uint8_t dp_opcode = 0; |
| 1599 | uint8_t opcode_shift = 6; |
| 1600 | uint8_t rd_shift = 0; |
| 1601 | uint8_t rn_shift = 3; |
| 1602 | uint8_t immediate_shift = 0; |
| 1603 | bool use_immediate = false; |
Vladimir Marko | ac0341e | 2014-12-18 19:56:49 +0000 | [diff] [blame] | 1604 | uint32_t immediate = 0; // Should be at most 9 bits but keep the full immediate for CHECKs. |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1605 | uint8_t thumb_opcode; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1606 | |
| 1607 | if (so.IsImmediate()) { |
| 1608 | use_immediate = true; |
| 1609 | immediate = so.GetImmediate(); |
| 1610 | } |
| 1611 | |
| 1612 | switch (opcode) { |
| 1613 | case ADD: |
| 1614 | if (so.IsRegister()) { |
| 1615 | Register rm = so.GetRegister(); |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1616 | if (rn == rd && set_cc != kCcSet) { |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1617 | // Can use T2 encoding (allows 4 bit registers) |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1618 | dp_opcode = 1U /* 0b01 */; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1619 | opcode_shift = 10; |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1620 | thumb_opcode = 1U /* 0b0001 */; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1621 | // Make Rn also contain the top bit of rd. |
| 1622 | rn = static_cast<Register>(static_cast<uint32_t>(rm) | |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1623 | (static_cast<uint32_t>(rd) & 8U /* 0b1000 */) << 1); |
| 1624 | rd = static_cast<Register>(static_cast<uint32_t>(rd) & 7U /* 0b111 */); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1625 | } else { |
| 1626 | // T1. |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1627 | DCHECK(!IsHighRegister(rd)); |
| 1628 | DCHECK(!IsHighRegister(rn)); |
| 1629 | DCHECK(!IsHighRegister(rm)); |
| 1630 | // Sets condition codes if and only if outside IT block, |
| 1631 | // check that it complies with set_cc. |
| 1632 | DCHECK((cond == AL) ? set_cc != kCcKeep : set_cc != kCcSet); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1633 | opcode_shift = 9; |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1634 | thumb_opcode = 12U /* 0b01100 */; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1635 | immediate = static_cast<uint32_t>(so.GetRegister()); |
| 1636 | use_immediate = true; |
| 1637 | immediate_shift = 6; |
| 1638 | } |
| 1639 | } else { |
| 1640 | // Immediate. |
| 1641 | if (rd == SP && rn == SP) { |
| 1642 | // ADD sp, sp, #imm |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1643 | dp_opcode = 2U /* 0b10 */; |
| 1644 | thumb_opcode = 3U /* 0b11 */; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1645 | opcode_shift = 12; |
Vladimir Marko | ac0341e | 2014-12-18 19:56:49 +0000 | [diff] [blame] | 1646 | CHECK_LT(immediate, (1u << 9)); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 1647 | CHECK_ALIGNED(immediate, 4); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1648 | |
| 1649 | // Remove rd and rn from instruction by orring it with immed and clearing bits. |
| 1650 | rn = R0; |
| 1651 | rd = R0; |
| 1652 | rd_shift = 0; |
| 1653 | rn_shift = 0; |
| 1654 | immediate >>= 2; |
| 1655 | } else if (rd != SP && rn == SP) { |
| 1656 | // ADD rd, SP, #imm |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1657 | dp_opcode = 2U /* 0b10 */; |
| 1658 | thumb_opcode = 5U /* 0b101 */; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1659 | opcode_shift = 11; |
Vladimir Marko | ac0341e | 2014-12-18 19:56:49 +0000 | [diff] [blame] | 1660 | CHECK_LT(immediate, (1u << 10)); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 1661 | CHECK_ALIGNED(immediate, 4); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1662 | |
| 1663 | // Remove rn from instruction. |
| 1664 | rn = R0; |
| 1665 | rn_shift = 0; |
| 1666 | rd_shift = 8; |
| 1667 | immediate >>= 2; |
| 1668 | } else if (rn != rd) { |
| 1669 | // Must use T1. |
| 1670 | opcode_shift = 9; |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1671 | thumb_opcode = 14U /* 0b01110 */; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1672 | immediate_shift = 6; |
| 1673 | } else { |
| 1674 | // T2 encoding. |
| 1675 | opcode_shift = 11; |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1676 | thumb_opcode = 6U /* 0b110 */; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1677 | rd_shift = 8; |
| 1678 | rn_shift = 8; |
| 1679 | } |
| 1680 | } |
| 1681 | break; |
| 1682 | |
| 1683 | case SUB: |
| 1684 | if (so.IsRegister()) { |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1685 | // T1. |
| 1686 | Register rm = so.GetRegister(); |
| 1687 | DCHECK(!IsHighRegister(rd)); |
| 1688 | DCHECK(!IsHighRegister(rn)); |
| 1689 | DCHECK(!IsHighRegister(rm)); |
| 1690 | // Sets condition codes if and only if outside IT block, |
| 1691 | // check that it complies with set_cc. |
| 1692 | DCHECK((cond == AL) ? set_cc != kCcKeep : set_cc != kCcSet); |
| 1693 | opcode_shift = 9; |
| 1694 | thumb_opcode = 13U /* 0b01101 */; |
| 1695 | immediate = static_cast<uint32_t>(rm); |
| 1696 | use_immediate = true; |
| 1697 | immediate_shift = 6; |
| 1698 | } else { |
| 1699 | if (rd == SP && rn == SP) { |
| 1700 | // SUB sp, sp, #imm |
| 1701 | dp_opcode = 2U /* 0b10 */; |
| 1702 | thumb_opcode = 0x61 /* 0b1100001 */; |
| 1703 | opcode_shift = 7; |
| 1704 | CHECK_LT(immediate, (1u << 9)); |
| 1705 | CHECK_ALIGNED(immediate, 4); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1706 | |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1707 | // Remove rd and rn from instruction by orring it with immed and clearing bits. |
| 1708 | rn = R0; |
| 1709 | rd = R0; |
| 1710 | rd_shift = 0; |
| 1711 | rn_shift = 0; |
| 1712 | immediate >>= 2; |
| 1713 | } else if (rn != rd) { |
| 1714 | // Must use T1. |
| 1715 | opcode_shift = 9; |
| 1716 | thumb_opcode = 15U /* 0b01111 */; |
| 1717 | immediate_shift = 6; |
| 1718 | } else { |
| 1719 | // T2 encoding. |
| 1720 | opcode_shift = 11; |
| 1721 | thumb_opcode = 7U /* 0b111 */; |
| 1722 | rd_shift = 8; |
| 1723 | rn_shift = 8; |
| 1724 | } |
| 1725 | } |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1726 | break; |
| 1727 | default: |
| 1728 | LOG(FATAL) << "This opcode is not an ADD or SUB: " << opcode; |
Vladimir Marko | e8469c1 | 2014-11-26 18:09:30 +0000 | [diff] [blame] | 1729 | UNREACHABLE(); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | int16_t encoding = dp_opcode << 14 | |
| 1733 | (thumb_opcode << opcode_shift) | |
| 1734 | rd << rd_shift | |
| 1735 | rn << rn_shift | |
| 1736 | (use_immediate ? (immediate << immediate_shift) : 0); |
| 1737 | |
| 1738 | Emit16(encoding); |
| 1739 | } |
| 1740 | |
| 1741 | |
| 1742 | void Thumb2Assembler::EmitDataProcessing(Condition cond, |
| 1743 | Opcode opcode, |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1744 | SetCc set_cc, |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1745 | Register rn, |
| 1746 | Register rd, |
| 1747 | const ShifterOperand& so) { |
| 1748 | CHECK_NE(rd, kNoRegister); |
| 1749 | CheckCondition(cond); |
| 1750 | |
| 1751 | if (Is32BitDataProcessing(cond, opcode, set_cc, rn, rd, so)) { |
| 1752 | Emit32BitDataProcessing(cond, opcode, set_cc, rn, rd, so); |
| 1753 | } else { |
| 1754 | Emit16BitDataProcessing(cond, opcode, set_cc, rn, rd, so); |
| 1755 | } |
| 1756 | } |
| 1757 | |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1758 | void Thumb2Assembler::EmitShift(Register rd, |
| 1759 | Register rm, |
| 1760 | Shift shift, |
| 1761 | uint8_t amount, |
| 1762 | Condition cond, |
| 1763 | SetCc set_cc) { |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 1764 | CHECK_LT(amount, (1 << 5)); |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1765 | if ((IsHighRegister(rd) || IsHighRegister(rm) || shift == ROR || shift == RRX) || |
| 1766 | ((cond == AL) ? set_cc == kCcKeep : set_cc == kCcSet)) { |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 1767 | uint16_t opcode = 0; |
| 1768 | switch (shift) { |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1769 | case LSL: opcode = 0U /* 0b00 */; break; |
| 1770 | case LSR: opcode = 1U /* 0b01 */; break; |
| 1771 | case ASR: opcode = 2U /* 0b10 */; break; |
| 1772 | case ROR: opcode = 3U /* 0b11 */; break; |
| 1773 | case RRX: opcode = 3U /* 0b11 */; amount = 0; break; |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 1774 | default: |
| 1775 | LOG(FATAL) << "Unsupported thumb2 shift opcode"; |
Vladimir Marko | e8469c1 | 2014-11-26 18:09:30 +0000 | [diff] [blame] | 1776 | UNREACHABLE(); |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 1777 | } |
| 1778 | // 32 bit. |
| 1779 | int32_t encoding = B31 | B30 | B29 | B27 | B25 | B22 | |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1780 | 0xf << 16 | (set_cc == kCcSet ? B20 : 0); |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 1781 | uint32_t imm3 = amount >> 2; |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1782 | uint32_t imm2 = amount & 3U /* 0b11 */; |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 1783 | encoding |= imm3 << 12 | imm2 << 6 | static_cast<int16_t>(rm) | |
| 1784 | static_cast<int16_t>(rd) << 8 | opcode << 4; |
| 1785 | Emit32(encoding); |
| 1786 | } else { |
| 1787 | // 16 bit shift |
| 1788 | uint16_t opcode = 0; |
| 1789 | switch (shift) { |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1790 | case LSL: opcode = 0U /* 0b00 */; break; |
| 1791 | case LSR: opcode = 1U /* 0b01 */; break; |
| 1792 | case ASR: opcode = 2U /* 0b10 */; break; |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 1793 | default: |
Vladimir Marko | e8469c1 | 2014-11-26 18:09:30 +0000 | [diff] [blame] | 1794 | LOG(FATAL) << "Unsupported thumb2 shift opcode"; |
| 1795 | UNREACHABLE(); |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 1796 | } |
| 1797 | int16_t encoding = opcode << 11 | amount << 6 | static_cast<int16_t>(rm) << 3 | |
| 1798 | static_cast<int16_t>(rd); |
| 1799 | Emit16(encoding); |
| 1800 | } |
| 1801 | } |
| 1802 | |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1803 | void Thumb2Assembler::EmitShift(Register rd, |
| 1804 | Register rn, |
| 1805 | Shift shift, |
| 1806 | Register rm, |
| 1807 | Condition cond, |
| 1808 | SetCc set_cc) { |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 1809 | CHECK_NE(shift, RRX); |
| 1810 | bool must_be_32bit = false; |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1811 | if (IsHighRegister(rd) || IsHighRegister(rm) || IsHighRegister(rn) || rd != rn || |
| 1812 | ((cond == AL) ? set_cc == kCcKeep : set_cc == kCcSet)) { |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 1813 | must_be_32bit = true; |
| 1814 | } |
| 1815 | |
| 1816 | if (must_be_32bit) { |
| 1817 | uint16_t opcode = 0; |
| 1818 | switch (shift) { |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1819 | case LSL: opcode = 0U /* 0b00 */; break; |
| 1820 | case LSR: opcode = 1U /* 0b01 */; break; |
| 1821 | case ASR: opcode = 2U /* 0b10 */; break; |
| 1822 | case ROR: opcode = 3U /* 0b11 */; break; |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 1823 | default: |
| 1824 | LOG(FATAL) << "Unsupported thumb2 shift opcode"; |
Vladimir Marko | e8469c1 | 2014-11-26 18:09:30 +0000 | [diff] [blame] | 1825 | UNREACHABLE(); |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 1826 | } |
| 1827 | // 32 bit. |
| 1828 | int32_t encoding = B31 | B30 | B29 | B28 | B27 | B25 | |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1829 | 0xf << 12 | (set_cc == kCcSet ? B20 : 0); |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 1830 | encoding |= static_cast<int16_t>(rn) << 16 | static_cast<int16_t>(rm) | |
| 1831 | static_cast<int16_t>(rd) << 8 | opcode << 21; |
| 1832 | Emit32(encoding); |
| 1833 | } else { |
| 1834 | uint16_t opcode = 0; |
| 1835 | switch (shift) { |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1836 | case LSL: opcode = 2U /* 0b0010 */; break; |
| 1837 | case LSR: opcode = 3U /* 0b0011 */; break; |
| 1838 | case ASR: opcode = 4U /* 0b0100 */; break; |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 1839 | case ROR: opcode = 7U /* 0b0111 */; break; |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 1840 | default: |
Vladimir Marko | e8469c1 | 2014-11-26 18:09:30 +0000 | [diff] [blame] | 1841 | LOG(FATAL) << "Unsupported thumb2 shift opcode"; |
| 1842 | UNREACHABLE(); |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 1843 | } |
| 1844 | int16_t encoding = B14 | opcode << 6 | static_cast<int16_t>(rm) << 3 | |
| 1845 | static_cast<int16_t>(rd); |
| 1846 | Emit16(encoding); |
| 1847 | } |
| 1848 | } |
| 1849 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 1850 | inline size_t Thumb2Assembler::Fixup::SizeInBytes(Size size) { |
| 1851 | switch (size) { |
| 1852 | case kBranch16Bit: |
| 1853 | return 2u; |
| 1854 | case kBranch32Bit: |
| 1855 | return 4u; |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 1856 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 1857 | case kCbxz16Bit: |
| 1858 | return 2u; |
| 1859 | case kCbxz32Bit: |
| 1860 | return 4u; |
| 1861 | case kCbxz48Bit: |
| 1862 | return 6u; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1863 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 1864 | case kLiteral1KiB: |
| 1865 | return 2u; |
| 1866 | case kLiteral4KiB: |
| 1867 | return 4u; |
| 1868 | case kLiteral64KiB: |
| 1869 | return 8u; |
| 1870 | case kLiteral1MiB: |
| 1871 | return 10u; |
| 1872 | case kLiteralFar: |
| 1873 | return 14u; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1874 | |
Andreas Gampe | 7cffc3b | 2015-10-19 21:31:53 -0700 | [diff] [blame] | 1875 | case kLiteralAddr1KiB: |
| 1876 | return 2u; |
| 1877 | case kLiteralAddr4KiB: |
| 1878 | return 4u; |
| 1879 | case kLiteralAddr64KiB: |
| 1880 | return 6u; |
| 1881 | case kLiteralAddrFar: |
| 1882 | return 10u; |
| 1883 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 1884 | case kLongOrFPLiteral1KiB: |
| 1885 | return 4u; |
| 1886 | case kLongOrFPLiteral256KiB: |
| 1887 | return 10u; |
| 1888 | case kLongOrFPLiteralFar: |
| 1889 | return 14u; |
| 1890 | } |
| 1891 | LOG(FATAL) << "Unexpected size: " << static_cast<int>(size); |
| 1892 | UNREACHABLE(); |
| 1893 | } |
| 1894 | |
| 1895 | inline uint32_t Thumb2Assembler::Fixup::GetOriginalSizeInBytes() const { |
| 1896 | return SizeInBytes(original_size_); |
| 1897 | } |
| 1898 | |
| 1899 | inline uint32_t Thumb2Assembler::Fixup::GetSizeInBytes() const { |
| 1900 | return SizeInBytes(size_); |
| 1901 | } |
| 1902 | |
| 1903 | inline size_t Thumb2Assembler::Fixup::LiteralPoolPaddingSize(uint32_t current_code_size) { |
| 1904 | // The code size must be a multiple of 2. |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 1905 | DCHECK_ALIGNED(current_code_size, 2); |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 1906 | // If it isn't a multiple of 4, we need to add a 2-byte padding before the literal pool. |
| 1907 | return current_code_size & 2; |
| 1908 | } |
| 1909 | |
| 1910 | inline int32_t Thumb2Assembler::Fixup::GetOffset(uint32_t current_code_size) const { |
| 1911 | static constexpr int32_t int32_min = std::numeric_limits<int32_t>::min(); |
| 1912 | static constexpr int32_t int32_max = std::numeric_limits<int32_t>::max(); |
| 1913 | DCHECK_LE(target_, static_cast<uint32_t>(int32_max)); |
| 1914 | DCHECK_LE(location_, static_cast<uint32_t>(int32_max)); |
| 1915 | DCHECK_LE(adjustment_, static_cast<uint32_t>(int32_max)); |
| 1916 | int32_t diff = static_cast<int32_t>(target_) - static_cast<int32_t>(location_); |
| 1917 | if (target_ > location_) { |
| 1918 | DCHECK_LE(adjustment_, static_cast<uint32_t>(int32_max - diff)); |
| 1919 | diff += static_cast<int32_t>(adjustment_); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1920 | } else { |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 1921 | DCHECK_LE(int32_min + static_cast<int32_t>(adjustment_), diff); |
| 1922 | diff -= static_cast<int32_t>(adjustment_); |
| 1923 | } |
| 1924 | // The default PC adjustment for Thumb2 is 4 bytes. |
| 1925 | DCHECK_GE(diff, int32_min + 4); |
| 1926 | diff -= 4; |
| 1927 | // Add additional adjustment for instructions preceding the PC usage, padding |
| 1928 | // before the literal pool and rounding down the PC for literal loads. |
| 1929 | switch (GetSize()) { |
| 1930 | case kBranch16Bit: |
| 1931 | case kBranch32Bit: |
| 1932 | break; |
| 1933 | |
| 1934 | case kCbxz16Bit: |
| 1935 | break; |
| 1936 | case kCbxz32Bit: |
| 1937 | case kCbxz48Bit: |
| 1938 | DCHECK_GE(diff, int32_min + 2); |
| 1939 | diff -= 2; // Extra CMP Rn, #0, 16-bit. |
| 1940 | break; |
| 1941 | |
| 1942 | case kLiteral1KiB: |
| 1943 | case kLiteral4KiB: |
| 1944 | case kLongOrFPLiteral1KiB: |
Andreas Gampe | 7cffc3b | 2015-10-19 21:31:53 -0700 | [diff] [blame] | 1945 | case kLiteralAddr1KiB: |
| 1946 | case kLiteralAddr4KiB: |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 1947 | DCHECK(diff >= 0 || (GetSize() == kLiteral1KiB && diff == -2)); |
| 1948 | diff += LiteralPoolPaddingSize(current_code_size); |
| 1949 | // Load literal instructions round down the PC+4 to a multiple of 4, so if the PC |
| 1950 | // isn't a multiple of 2, we need to adjust. Since we already adjusted for the target |
| 1951 | // being aligned, current PC alignment can be inferred from diff. |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 1952 | DCHECK_ALIGNED(diff, 2); |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 1953 | diff = diff + (diff & 2); |
| 1954 | DCHECK_GE(diff, 0); |
| 1955 | break; |
| 1956 | case kLiteral1MiB: |
| 1957 | case kLiteral64KiB: |
| 1958 | case kLongOrFPLiteral256KiB: |
Andreas Gampe | 7cffc3b | 2015-10-19 21:31:53 -0700 | [diff] [blame] | 1959 | case kLiteralAddr64KiB: |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 1960 | DCHECK_GE(diff, 4); // The target must be at least 4 bytes after the ADD rX, PC. |
| 1961 | diff -= 4; // One extra 32-bit MOV. |
| 1962 | diff += LiteralPoolPaddingSize(current_code_size); |
| 1963 | break; |
| 1964 | case kLiteralFar: |
| 1965 | case kLongOrFPLiteralFar: |
Andreas Gampe | 7cffc3b | 2015-10-19 21:31:53 -0700 | [diff] [blame] | 1966 | case kLiteralAddrFar: |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 1967 | DCHECK_GE(diff, 8); // The target must be at least 4 bytes after the ADD rX, PC. |
| 1968 | diff -= 8; // Extra MOVW+MOVT; both 32-bit. |
| 1969 | diff += LiteralPoolPaddingSize(current_code_size); |
| 1970 | break; |
| 1971 | } |
| 1972 | return diff; |
| 1973 | } |
| 1974 | |
| 1975 | inline size_t Thumb2Assembler::Fixup::IncreaseSize(Size new_size) { |
| 1976 | DCHECK_NE(target_, kUnresolved); |
| 1977 | Size old_size = size_; |
| 1978 | size_ = new_size; |
| 1979 | DCHECK_GT(SizeInBytes(new_size), SizeInBytes(old_size)); |
| 1980 | size_t adjustment = SizeInBytes(new_size) - SizeInBytes(old_size); |
| 1981 | if (target_ > location_) { |
| 1982 | adjustment_ += adjustment; |
| 1983 | } |
| 1984 | return adjustment; |
| 1985 | } |
| 1986 | |
| 1987 | uint32_t Thumb2Assembler::Fixup::AdjustSizeIfNeeded(uint32_t current_code_size) { |
| 1988 | uint32_t old_code_size = current_code_size; |
| 1989 | switch (GetSize()) { |
| 1990 | case kBranch16Bit: |
| 1991 | if (IsInt(cond_ != AL ? 9 : 12, GetOffset(current_code_size))) { |
| 1992 | break; |
Vladimir Marko | f38caa6 | 2015-05-29 15:50:18 +0100 | [diff] [blame] | 1993 | } |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 1994 | current_code_size += IncreaseSize(kBranch32Bit); |
| 1995 | FALLTHROUGH_INTENDED; |
| 1996 | case kBranch32Bit: |
| 1997 | // We don't support conditional branches beyond +-1MiB |
| 1998 | // or unconditional branches beyond +-16MiB. |
| 1999 | break; |
| 2000 | |
| 2001 | case kCbxz16Bit: |
| 2002 | if (IsUint<7>(GetOffset(current_code_size))) { |
| 2003 | break; |
| 2004 | } |
| 2005 | current_code_size += IncreaseSize(kCbxz32Bit); |
| 2006 | FALLTHROUGH_INTENDED; |
| 2007 | case kCbxz32Bit: |
| 2008 | if (IsInt<9>(GetOffset(current_code_size))) { |
| 2009 | break; |
| 2010 | } |
| 2011 | current_code_size += IncreaseSize(kCbxz48Bit); |
| 2012 | FALLTHROUGH_INTENDED; |
| 2013 | case kCbxz48Bit: |
| 2014 | // We don't support conditional branches beyond +-1MiB. |
| 2015 | break; |
| 2016 | |
| 2017 | case kLiteral1KiB: |
| 2018 | DCHECK(!IsHighRegister(rn_)); |
| 2019 | if (IsUint<10>(GetOffset(current_code_size))) { |
| 2020 | break; |
| 2021 | } |
| 2022 | current_code_size += IncreaseSize(kLiteral4KiB); |
| 2023 | FALLTHROUGH_INTENDED; |
| 2024 | case kLiteral4KiB: |
| 2025 | if (IsUint<12>(GetOffset(current_code_size))) { |
| 2026 | break; |
| 2027 | } |
| 2028 | current_code_size += IncreaseSize(kLiteral64KiB); |
| 2029 | FALLTHROUGH_INTENDED; |
| 2030 | case kLiteral64KiB: |
| 2031 | // Can't handle high register which we can encounter by fall-through from kLiteral4KiB. |
| 2032 | if (!IsHighRegister(rn_) && IsUint<16>(GetOffset(current_code_size))) { |
| 2033 | break; |
| 2034 | } |
| 2035 | current_code_size += IncreaseSize(kLiteral1MiB); |
| 2036 | FALLTHROUGH_INTENDED; |
| 2037 | case kLiteral1MiB: |
| 2038 | if (IsUint<20>(GetOffset(current_code_size))) { |
| 2039 | break; |
| 2040 | } |
| 2041 | current_code_size += IncreaseSize(kLiteralFar); |
| 2042 | FALLTHROUGH_INTENDED; |
| 2043 | case kLiteralFar: |
| 2044 | // This encoding can reach any target. |
| 2045 | break; |
| 2046 | |
Andreas Gampe | 7cffc3b | 2015-10-19 21:31:53 -0700 | [diff] [blame] | 2047 | case kLiteralAddr1KiB: |
| 2048 | DCHECK(!IsHighRegister(rn_)); |
| 2049 | if (IsUint<10>(GetOffset(current_code_size))) { |
| 2050 | break; |
| 2051 | } |
| 2052 | current_code_size += IncreaseSize(kLiteralAddr4KiB); |
| 2053 | FALLTHROUGH_INTENDED; |
| 2054 | case kLiteralAddr4KiB: |
| 2055 | if (IsUint<12>(GetOffset(current_code_size))) { |
| 2056 | break; |
| 2057 | } |
| 2058 | current_code_size += IncreaseSize(kLiteralAddr64KiB); |
| 2059 | FALLTHROUGH_INTENDED; |
| 2060 | case kLiteralAddr64KiB: |
| 2061 | if (IsUint<16>(GetOffset(current_code_size))) { |
| 2062 | break; |
| 2063 | } |
| 2064 | current_code_size += IncreaseSize(kLiteralAddrFar); |
| 2065 | FALLTHROUGH_INTENDED; |
| 2066 | case kLiteralAddrFar: |
| 2067 | // This encoding can reach any target. |
| 2068 | break; |
| 2069 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 2070 | case kLongOrFPLiteral1KiB: |
| 2071 | if (IsUint<10>(GetOffset(current_code_size))) { |
| 2072 | break; |
| 2073 | } |
| 2074 | current_code_size += IncreaseSize(kLongOrFPLiteral256KiB); |
| 2075 | FALLTHROUGH_INTENDED; |
| 2076 | case kLongOrFPLiteral256KiB: |
| 2077 | if (IsUint<18>(GetOffset(current_code_size))) { |
| 2078 | break; |
| 2079 | } |
| 2080 | current_code_size += IncreaseSize(kLongOrFPLiteralFar); |
| 2081 | FALLTHROUGH_INTENDED; |
| 2082 | case kLongOrFPLiteralFar: |
| 2083 | // This encoding can reach any target. |
| 2084 | break; |
| 2085 | } |
| 2086 | return current_code_size - old_code_size; |
| 2087 | } |
| 2088 | |
| 2089 | void Thumb2Assembler::Fixup::Emit(AssemblerBuffer* buffer, uint32_t code_size) const { |
| 2090 | switch (GetSize()) { |
| 2091 | case kBranch16Bit: { |
| 2092 | DCHECK(type_ == kUnconditional || type_ == kConditional); |
| 2093 | DCHECK_EQ(type_ == kConditional, cond_ != AL); |
| 2094 | int16_t encoding = BEncoding16(GetOffset(code_size), cond_); |
Vladimir Marko | f38caa6 | 2015-05-29 15:50:18 +0100 | [diff] [blame] | 2095 | buffer->Store<int16_t>(location_, encoding); |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 2096 | break; |
| 2097 | } |
| 2098 | case kBranch32Bit: { |
| 2099 | DCHECK(type_ == kConditional || type_ == kUnconditional || |
| 2100 | type_ == kUnconditionalLink || type_ == kUnconditionalLinkX); |
| 2101 | DCHECK_EQ(type_ == kConditional, cond_ != AL); |
| 2102 | int32_t encoding = BEncoding32(GetOffset(code_size), cond_); |
| 2103 | if (type_ == kUnconditionalLink) { |
| 2104 | DCHECK_NE(encoding & B12, 0); |
| 2105 | encoding |= B14; |
| 2106 | } else if (type_ == kUnconditionalLinkX) { |
| 2107 | DCHECK_NE(encoding & B12, 0); |
| 2108 | encoding ^= B14 | B12; |
| 2109 | } |
| 2110 | buffer->Store<int16_t>(location_, encoding >> 16); |
| 2111 | buffer->Store<int16_t>(location_ + 2u, static_cast<int16_t>(encoding & 0xffff)); |
| 2112 | break; |
| 2113 | } |
| 2114 | |
| 2115 | case kCbxz16Bit: { |
| 2116 | DCHECK(type_ == kCompareAndBranchXZero); |
| 2117 | int16_t encoding = CbxzEncoding16(rn_, GetOffset(code_size), cond_); |
| 2118 | buffer->Store<int16_t>(location_, encoding); |
| 2119 | break; |
| 2120 | } |
| 2121 | case kCbxz32Bit: { |
| 2122 | DCHECK(type_ == kCompareAndBranchXZero); |
| 2123 | DCHECK(cond_ == EQ || cond_ == NE); |
| 2124 | int16_t cmp_encoding = CmpRnImm8Encoding16(rn_, 0); |
| 2125 | int16_t b_encoding = BEncoding16(GetOffset(code_size), cond_); |
| 2126 | buffer->Store<int16_t>(location_, cmp_encoding); |
| 2127 | buffer->Store<int16_t>(location_ + 2, b_encoding); |
| 2128 | break; |
| 2129 | } |
| 2130 | case kCbxz48Bit: { |
| 2131 | DCHECK(type_ == kCompareAndBranchXZero); |
| 2132 | DCHECK(cond_ == EQ || cond_ == NE); |
| 2133 | int16_t cmp_encoding = CmpRnImm8Encoding16(rn_, 0); |
| 2134 | int32_t b_encoding = BEncoding32(GetOffset(code_size), cond_); |
| 2135 | buffer->Store<int16_t>(location_, cmp_encoding); |
| 2136 | buffer->Store<int16_t>(location_ + 2u, b_encoding >> 16); |
| 2137 | buffer->Store<int16_t>(location_ + 4u, static_cast<int16_t>(b_encoding & 0xffff)); |
| 2138 | break; |
| 2139 | } |
| 2140 | |
| 2141 | case kLiteral1KiB: { |
| 2142 | DCHECK(type_ == kLoadLiteralNarrow); |
| 2143 | int16_t encoding = LdrLitEncoding16(rn_, GetOffset(code_size)); |
| 2144 | buffer->Store<int16_t>(location_, encoding); |
| 2145 | break; |
| 2146 | } |
| 2147 | case kLiteral4KiB: { |
| 2148 | DCHECK(type_ == kLoadLiteralNarrow); |
| 2149 | // GetOffset() uses PC+4 but load literal uses AlignDown(PC+4, 4). Adjust offset accordingly. |
| 2150 | int32_t encoding = LdrLitEncoding32(rn_, GetOffset(code_size)); |
| 2151 | buffer->Store<int16_t>(location_, encoding >> 16); |
| 2152 | buffer->Store<int16_t>(location_ + 2u, static_cast<int16_t>(encoding & 0xffff)); |
| 2153 | break; |
| 2154 | } |
| 2155 | case kLiteral64KiB: { |
| 2156 | DCHECK(type_ == kLoadLiteralNarrow); |
| 2157 | int32_t mov_encoding = MovwEncoding32(rn_, GetOffset(code_size)); |
| 2158 | int16_t add_pc_encoding = AddRdnRmEncoding16(rn_, PC); |
| 2159 | int16_t ldr_encoding = LdrRtRnImm5Encoding16(rn_, rn_, 0); |
| 2160 | buffer->Store<int16_t>(location_, mov_encoding >> 16); |
| 2161 | buffer->Store<int16_t>(location_ + 2u, static_cast<int16_t>(mov_encoding & 0xffff)); |
| 2162 | buffer->Store<int16_t>(location_ + 4u, add_pc_encoding); |
| 2163 | buffer->Store<int16_t>(location_ + 6u, ldr_encoding); |
| 2164 | break; |
| 2165 | } |
| 2166 | case kLiteral1MiB: { |
| 2167 | DCHECK(type_ == kLoadLiteralNarrow); |
| 2168 | int32_t offset = GetOffset(code_size); |
| 2169 | int32_t mov_encoding = MovModImmEncoding32(rn_, offset & ~0xfff); |
| 2170 | int16_t add_pc_encoding = AddRdnRmEncoding16(rn_, PC); |
| 2171 | int32_t ldr_encoding = LdrRtRnImm12Encoding(rn_, rn_, offset & 0xfff); |
| 2172 | buffer->Store<int16_t>(location_, mov_encoding >> 16); |
| 2173 | buffer->Store<int16_t>(location_ + 2u, static_cast<int16_t>(mov_encoding & 0xffff)); |
| 2174 | buffer->Store<int16_t>(location_ + 4u, add_pc_encoding); |
| 2175 | buffer->Store<int16_t>(location_ + 6u, ldr_encoding >> 16); |
| 2176 | buffer->Store<int16_t>(location_ + 8u, static_cast<int16_t>(ldr_encoding & 0xffff)); |
| 2177 | break; |
| 2178 | } |
| 2179 | case kLiteralFar: { |
| 2180 | DCHECK(type_ == kLoadLiteralNarrow); |
| 2181 | int32_t offset = GetOffset(code_size); |
| 2182 | int32_t movw_encoding = MovwEncoding32(rn_, offset & 0xffff); |
| 2183 | int32_t movt_encoding = MovtEncoding32(rn_, offset & ~0xffff); |
| 2184 | int16_t add_pc_encoding = AddRdnRmEncoding16(rn_, PC); |
| 2185 | int32_t ldr_encoding = LdrRtRnImm12Encoding(rn_, rn_, 0); |
| 2186 | buffer->Store<int16_t>(location_, movw_encoding >> 16); |
| 2187 | buffer->Store<int16_t>(location_ + 2u, static_cast<int16_t>(movw_encoding & 0xffff)); |
| 2188 | buffer->Store<int16_t>(location_ + 4u, movt_encoding >> 16); |
| 2189 | buffer->Store<int16_t>(location_ + 6u, static_cast<int16_t>(movt_encoding & 0xffff)); |
| 2190 | buffer->Store<int16_t>(location_ + 8u, add_pc_encoding); |
| 2191 | buffer->Store<int16_t>(location_ + 10u, ldr_encoding >> 16); |
| 2192 | buffer->Store<int16_t>(location_ + 12u, static_cast<int16_t>(ldr_encoding & 0xffff)); |
| 2193 | break; |
| 2194 | } |
| 2195 | |
Andreas Gampe | 7cffc3b | 2015-10-19 21:31:53 -0700 | [diff] [blame] | 2196 | case kLiteralAddr1KiB: { |
| 2197 | DCHECK(type_ == kLoadLiteralAddr); |
| 2198 | int16_t encoding = AdrEncoding16(rn_, GetOffset(code_size)); |
| 2199 | buffer->Store<int16_t>(location_, encoding); |
| 2200 | break; |
| 2201 | } |
| 2202 | case kLiteralAddr4KiB: { |
| 2203 | DCHECK(type_ == kLoadLiteralAddr); |
| 2204 | int32_t encoding = AdrEncoding32(rn_, GetOffset(code_size)); |
| 2205 | buffer->Store<int16_t>(location_, encoding >> 16); |
| 2206 | buffer->Store<int16_t>(location_ + 2u, static_cast<int16_t>(encoding & 0xffff)); |
| 2207 | break; |
| 2208 | } |
| 2209 | case kLiteralAddr64KiB: { |
| 2210 | DCHECK(type_ == kLoadLiteralAddr); |
| 2211 | int32_t mov_encoding = MovwEncoding32(rn_, GetOffset(code_size)); |
| 2212 | int16_t add_pc_encoding = AddRdnRmEncoding16(rn_, PC); |
| 2213 | buffer->Store<int16_t>(location_, mov_encoding >> 16); |
| 2214 | buffer->Store<int16_t>(location_ + 2u, static_cast<int16_t>(mov_encoding & 0xffff)); |
| 2215 | buffer->Store<int16_t>(location_ + 4u, add_pc_encoding); |
| 2216 | break; |
| 2217 | } |
| 2218 | case kLiteralAddrFar: { |
| 2219 | DCHECK(type_ == kLoadLiteralAddr); |
| 2220 | int32_t offset = GetOffset(code_size); |
| 2221 | int32_t movw_encoding = MovwEncoding32(rn_, offset & 0xffff); |
| 2222 | int32_t movt_encoding = MovtEncoding32(rn_, offset & ~0xffff); |
| 2223 | int16_t add_pc_encoding = AddRdnRmEncoding16(rn_, PC); |
| 2224 | buffer->Store<int16_t>(location_, movw_encoding >> 16); |
| 2225 | buffer->Store<int16_t>(location_ + 2u, static_cast<int16_t>(movw_encoding & 0xffff)); |
| 2226 | buffer->Store<int16_t>(location_ + 4u, movt_encoding >> 16); |
| 2227 | buffer->Store<int16_t>(location_ + 6u, static_cast<int16_t>(movt_encoding & 0xffff)); |
| 2228 | buffer->Store<int16_t>(location_ + 8u, add_pc_encoding); |
| 2229 | break; |
| 2230 | } |
| 2231 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 2232 | case kLongOrFPLiteral1KiB: { |
| 2233 | int32_t encoding = LoadWideOrFpEncoding(PC, GetOffset(code_size)); // DCHECKs type_. |
| 2234 | buffer->Store<int16_t>(location_, encoding >> 16); |
| 2235 | buffer->Store<int16_t>(location_ + 2u, static_cast<int16_t>(encoding & 0xffff)); |
| 2236 | break; |
| 2237 | } |
| 2238 | case kLongOrFPLiteral256KiB: { |
| 2239 | int32_t offset = GetOffset(code_size); |
| 2240 | int32_t mov_encoding = MovModImmEncoding32(IP, offset & ~0x3ff); |
| 2241 | int16_t add_pc_encoding = AddRdnRmEncoding16(IP, PC); |
| 2242 | int32_t ldr_encoding = LoadWideOrFpEncoding(IP, offset & 0x3ff); // DCHECKs type_. |
| 2243 | buffer->Store<int16_t>(location_, mov_encoding >> 16); |
| 2244 | buffer->Store<int16_t>(location_ + 2u, static_cast<int16_t>(mov_encoding & 0xffff)); |
| 2245 | buffer->Store<int16_t>(location_ + 4u, add_pc_encoding); |
| 2246 | buffer->Store<int16_t>(location_ + 6u, ldr_encoding >> 16); |
| 2247 | buffer->Store<int16_t>(location_ + 8u, static_cast<int16_t>(ldr_encoding & 0xffff)); |
| 2248 | break; |
| 2249 | } |
| 2250 | case kLongOrFPLiteralFar: { |
| 2251 | int32_t offset = GetOffset(code_size); |
| 2252 | int32_t movw_encoding = MovwEncoding32(IP, offset & 0xffff); |
| 2253 | int32_t movt_encoding = MovtEncoding32(IP, offset & ~0xffff); |
| 2254 | int16_t add_pc_encoding = AddRdnRmEncoding16(IP, PC); |
| 2255 | int32_t ldr_encoding = LoadWideOrFpEncoding(IP, 0); // DCHECKs type_. |
| 2256 | buffer->Store<int16_t>(location_, movw_encoding >> 16); |
| 2257 | buffer->Store<int16_t>(location_ + 2u, static_cast<int16_t>(movw_encoding & 0xffff)); |
| 2258 | buffer->Store<int16_t>(location_ + 4u, movt_encoding >> 16); |
| 2259 | buffer->Store<int16_t>(location_ + 6u, static_cast<int16_t>(movt_encoding & 0xffff)); |
| 2260 | buffer->Store<int16_t>(location_ + 8u, add_pc_encoding); |
| 2261 | buffer->Store<int16_t>(location_ + 10u, ldr_encoding >> 16); |
| 2262 | buffer->Store<int16_t>(location_ + 12u, static_cast<int16_t>(ldr_encoding & 0xffff)); |
| 2263 | break; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2264 | } |
| 2265 | } |
| 2266 | } |
| 2267 | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2268 | uint16_t Thumb2Assembler::EmitCompareAndBranch(Register rn, uint16_t prev, bool n) { |
Nicolas Geoffray | d56376c | 2015-05-21 12:32:34 +0000 | [diff] [blame] | 2269 | CHECK(IsLowRegister(rn)); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2270 | uint32_t location = buffer_.Size(); |
| 2271 | |
| 2272 | // This is always unresolved as it must be a forward branch. |
| 2273 | Emit16(prev); // Previous link. |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 2274 | return AddFixup(Fixup::CompareAndBranch(location, rn, n ? NE : EQ)); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2275 | } |
| 2276 | |
| 2277 | |
| 2278 | // NOTE: this only support immediate offsets, not [rx,ry]. |
| 2279 | // TODO: support [rx,ry] instructions. |
| 2280 | void Thumb2Assembler::EmitLoadStore(Condition cond, |
| 2281 | bool load, |
| 2282 | bool byte, |
| 2283 | bool half, |
| 2284 | bool is_signed, |
| 2285 | Register rd, |
| 2286 | const Address& ad) { |
| 2287 | CHECK_NE(rd, kNoRegister); |
| 2288 | CheckCondition(cond); |
| 2289 | bool must_be_32bit = force_32bit_; |
| 2290 | if (IsHighRegister(rd)) { |
| 2291 | must_be_32bit = true; |
| 2292 | } |
| 2293 | |
| 2294 | Register rn = ad.GetRegister(); |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 2295 | if (IsHighRegister(rn) && rn != SP && rn != PC) { |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2296 | must_be_32bit = true; |
| 2297 | } |
| 2298 | |
| 2299 | if (is_signed || ad.GetOffset() < 0 || ad.GetMode() != Address::Offset) { |
| 2300 | must_be_32bit = true; |
| 2301 | } |
| 2302 | |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 2303 | if (ad.IsImmediate()) { |
| 2304 | // Immediate offset |
| 2305 | int32_t offset = ad.GetOffset(); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2306 | |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 2307 | // The 16 bit SP relative instruction can only have a 10 bit offset. |
Dave Allison | 0bb9ade | 2014-06-26 17:57:36 -0700 | [diff] [blame] | 2308 | if (rn == SP && offset >= (1 << 10)) { |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2309 | must_be_32bit = true; |
| 2310 | } |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2311 | |
| 2312 | if (byte) { |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 2313 | // 5 bit offset, no shift. |
Dave Allison | 0bb9ade | 2014-06-26 17:57:36 -0700 | [diff] [blame] | 2314 | if (offset >= (1 << 5)) { |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 2315 | must_be_32bit = true; |
| 2316 | } |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2317 | } else if (half) { |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 2318 | // 6 bit offset, shifted by 1. |
Dave Allison | 0bb9ade | 2014-06-26 17:57:36 -0700 | [diff] [blame] | 2319 | if (offset >= (1 << 6)) { |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 2320 | must_be_32bit = true; |
| 2321 | } |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2322 | } else { |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 2323 | // 7 bit offset, shifted by 2. |
Dave Allison | 0bb9ade | 2014-06-26 17:57:36 -0700 | [diff] [blame] | 2324 | if (offset >= (1 << 7)) { |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 2325 | must_be_32bit = true; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2326 | } |
| 2327 | } |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2328 | |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 2329 | if (must_be_32bit) { |
| 2330 | int32_t encoding = B31 | B30 | B29 | B28 | B27 | |
| 2331 | (load ? B20 : 0) | |
| 2332 | (is_signed ? B24 : 0) | |
| 2333 | static_cast<uint32_t>(rd) << 12 | |
| 2334 | ad.encodingThumb(true) | |
| 2335 | (byte ? 0 : half ? B21 : B22); |
| 2336 | Emit32(encoding); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2337 | } else { |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 2338 | // 16 bit thumb1. |
| 2339 | uint8_t opA = 0; |
| 2340 | bool sp_relative = false; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2341 | |
| 2342 | if (byte) { |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 2343 | opA = 7U /* 0b0111 */; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2344 | } else if (half) { |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 2345 | opA = 8U /* 0b1000 */; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2346 | } else { |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 2347 | if (rn == SP) { |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 2348 | opA = 9U /* 0b1001 */; |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 2349 | sp_relative = true; |
| 2350 | } else { |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 2351 | opA = 6U /* 0b0110 */; |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 2352 | } |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2353 | } |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 2354 | int16_t encoding = opA << 12 | |
| 2355 | (load ? B11 : 0); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2356 | |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 2357 | CHECK_GE(offset, 0); |
| 2358 | if (sp_relative) { |
| 2359 | // SP relative, 10 bit offset. |
Dave Allison | 0bb9ade | 2014-06-26 17:57:36 -0700 | [diff] [blame] | 2360 | CHECK_LT(offset, (1 << 10)); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 2361 | CHECK_ALIGNED(offset, 4); |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 2362 | encoding |= rd << 8 | offset >> 2; |
| 2363 | } else { |
| 2364 | // No SP relative. The offset is shifted right depending on |
| 2365 | // the size of the load/store. |
| 2366 | encoding |= static_cast<uint32_t>(rd); |
| 2367 | |
| 2368 | if (byte) { |
| 2369 | // 5 bit offset, no shift. |
Dave Allison | 0bb9ade | 2014-06-26 17:57:36 -0700 | [diff] [blame] | 2370 | CHECK_LT(offset, (1 << 5)); |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 2371 | } else if (half) { |
| 2372 | // 6 bit offset, shifted by 1. |
Dave Allison | 0bb9ade | 2014-06-26 17:57:36 -0700 | [diff] [blame] | 2373 | CHECK_LT(offset, (1 << 6)); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 2374 | CHECK_ALIGNED(offset, 2); |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 2375 | offset >>= 1; |
| 2376 | } else { |
| 2377 | // 7 bit offset, shifted by 2. |
Dave Allison | 0bb9ade | 2014-06-26 17:57:36 -0700 | [diff] [blame] | 2378 | CHECK_LT(offset, (1 << 7)); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 2379 | CHECK_ALIGNED(offset, 4); |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 2380 | offset >>= 2; |
| 2381 | } |
| 2382 | encoding |= rn << 3 | offset << 6; |
| 2383 | } |
| 2384 | |
| 2385 | Emit16(encoding); |
| 2386 | } |
| 2387 | } else { |
| 2388 | // Register shift. |
| 2389 | if (ad.GetRegister() == PC) { |
| 2390 | // PC relative literal encoding. |
| 2391 | int32_t offset = ad.GetOffset(); |
Dave Allison | 0bb9ade | 2014-06-26 17:57:36 -0700 | [diff] [blame] | 2392 | if (must_be_32bit || offset < 0 || offset >= (1 << 10) || !load) { |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 2393 | int32_t up = B23; |
| 2394 | if (offset < 0) { |
| 2395 | offset = -offset; |
| 2396 | up = 0; |
| 2397 | } |
| 2398 | CHECK_LT(offset, (1 << 12)); |
| 2399 | int32_t encoding = 0x1f << 27 | 0xf << 16 | B22 | (load ? B20 : 0) | |
| 2400 | offset | up | |
| 2401 | static_cast<uint32_t>(rd) << 12; |
| 2402 | Emit32(encoding); |
| 2403 | } else { |
| 2404 | // 16 bit literal load. |
| 2405 | CHECK_GE(offset, 0); |
| 2406 | CHECK_LT(offset, (1 << 10)); |
| 2407 | int32_t encoding = B14 | (load ? B11 : 0) | static_cast<uint32_t>(rd) << 8 | offset >> 2; |
| 2408 | Emit16(encoding); |
| 2409 | } |
| 2410 | } else { |
| 2411 | if (ad.GetShiftCount() != 0) { |
| 2412 | // If there is a shift count this must be 32 bit. |
| 2413 | must_be_32bit = true; |
| 2414 | } else if (IsHighRegister(ad.GetRegisterOffset())) { |
| 2415 | must_be_32bit = true; |
| 2416 | } |
| 2417 | |
| 2418 | if (must_be_32bit) { |
Nicolas Geoffray | 1a43dd7 | 2014-07-17 15:15:34 +0100 | [diff] [blame] | 2419 | int32_t encoding = 0x1f << 27 | (load ? B20 : 0) | static_cast<uint32_t>(rd) << 12 | |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 2420 | ad.encodingThumb(true); |
Nicolas Geoffray | 1a43dd7 | 2014-07-17 15:15:34 +0100 | [diff] [blame] | 2421 | if (half) { |
| 2422 | encoding |= B21; |
| 2423 | } else if (!byte) { |
| 2424 | encoding |= B22; |
| 2425 | } |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 2426 | Emit32(encoding); |
| 2427 | } else { |
| 2428 | // 16 bit register offset. |
| 2429 | int32_t encoding = B14 | B12 | (load ? B11 : 0) | static_cast<uint32_t>(rd) | |
| 2430 | ad.encodingThumb(false); |
Nicolas Geoffray | 1a43dd7 | 2014-07-17 15:15:34 +0100 | [diff] [blame] | 2431 | if (byte) { |
| 2432 | encoding |= B10; |
| 2433 | } else if (half) { |
| 2434 | encoding |= B9; |
| 2435 | } |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 2436 | Emit16(encoding); |
| 2437 | } |
| 2438 | } |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2439 | } |
| 2440 | } |
| 2441 | |
| 2442 | |
| 2443 | void Thumb2Assembler::EmitMultiMemOp(Condition cond, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2444 | BlockAddressMode bam, |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2445 | bool load, |
| 2446 | Register base, |
| 2447 | RegList regs) { |
| 2448 | CHECK_NE(base, kNoRegister); |
| 2449 | CheckCondition(cond); |
| 2450 | bool must_be_32bit = force_32bit_; |
| 2451 | |
Vladimir Marko | e8469c1 | 2014-11-26 18:09:30 +0000 | [diff] [blame] | 2452 | if (!must_be_32bit && base == SP && bam == (load ? IA_W : DB_W) && |
| 2453 | (regs & 0xff00 & ~(1 << (load ? PC : LR))) == 0) { |
| 2454 | // Use 16-bit PUSH/POP. |
| 2455 | int16_t encoding = B15 | B13 | B12 | (load ? B11 : 0) | B10 | |
| 2456 | ((regs & (1 << (load ? PC : LR))) != 0 ? B8 : 0) | (regs & 0x00ff); |
| 2457 | Emit16(encoding); |
| 2458 | return; |
| 2459 | } |
| 2460 | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2461 | if ((regs & 0xff00) != 0) { |
| 2462 | must_be_32bit = true; |
| 2463 | } |
| 2464 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2465 | bool w_bit = bam == IA_W || bam == DB_W || bam == DA_W || bam == IB_W; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2466 | // 16 bit always uses writeback. |
| 2467 | if (!w_bit) { |
| 2468 | must_be_32bit = true; |
| 2469 | } |
| 2470 | |
| 2471 | if (must_be_32bit) { |
| 2472 | uint32_t op = 0; |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2473 | switch (bam) { |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2474 | case IA: |
| 2475 | case IA_W: |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 2476 | op = 1U /* 0b01 */; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2477 | break; |
| 2478 | case DB: |
| 2479 | case DB_W: |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 2480 | op = 2U /* 0b10 */; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2481 | break; |
| 2482 | case DA: |
| 2483 | case IB: |
| 2484 | case DA_W: |
| 2485 | case IB_W: |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2486 | LOG(FATAL) << "LDM/STM mode not supported on thumb: " << bam; |
Vladimir Marko | e8469c1 | 2014-11-26 18:09:30 +0000 | [diff] [blame] | 2487 | UNREACHABLE(); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2488 | } |
| 2489 | if (load) { |
| 2490 | // Cannot have SP in the list. |
| 2491 | CHECK_EQ((regs & (1 << SP)), 0); |
| 2492 | } else { |
| 2493 | // Cannot have PC or SP in the list. |
| 2494 | CHECK_EQ((regs & (1 << PC | 1 << SP)), 0); |
| 2495 | } |
| 2496 | int32_t encoding = B31 | B30 | B29 | B27 | |
| 2497 | (op << 23) | |
| 2498 | (load ? B20 : 0) | |
| 2499 | base << 16 | |
| 2500 | regs | |
| 2501 | (w_bit << 21); |
| 2502 | Emit32(encoding); |
| 2503 | } else { |
| 2504 | int16_t encoding = B15 | B14 | |
| 2505 | (load ? B11 : 0) | |
| 2506 | base << 8 | |
| 2507 | regs; |
| 2508 | Emit16(encoding); |
| 2509 | } |
| 2510 | } |
| 2511 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 2512 | void Thumb2Assembler::EmitBranch(Condition cond, Label* label, bool link, bool x) { |
| 2513 | bool use32bit = IsForced32Bit() || !CanRelocateBranches(); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2514 | uint32_t pc = buffer_.Size(); |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 2515 | Fixup::Type branch_type; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2516 | if (cond == AL) { |
| 2517 | if (link) { |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 2518 | use32bit = true; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2519 | if (x) { |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 2520 | branch_type = Fixup::kUnconditionalLinkX; // BLX. |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2521 | } else { |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 2522 | branch_type = Fixup::kUnconditionalLink; // BX. |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2523 | } |
| 2524 | } else { |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 2525 | branch_type = Fixup::kUnconditional; // B. |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2526 | } |
| 2527 | } else { |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 2528 | branch_type = Fixup::kConditional; // B<cond>. |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2529 | } |
| 2530 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 2531 | Fixup::Size size = use32bit ? Fixup::kBranch32Bit : Fixup::kBranch16Bit; |
| 2532 | FixupId branch_id = AddFixup(Fixup::Branch(pc, branch_type, size, cond)); |
| 2533 | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2534 | if (label->IsBound()) { |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 2535 | // The branch is to a bound label which means that it's a backwards branch. |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 2536 | GetFixup(branch_id)->Resolve(label->Position()); |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 2537 | Emit16(0); |
Vladimir Marko | fbeb4ae | 2015-06-16 11:32:01 +0000 | [diff] [blame] | 2538 | } else { |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 2539 | // Branch target is an unbound label. Add it to a singly-linked list maintained within |
| 2540 | // the code with the label serving as the head. |
| 2541 | Emit16(static_cast<uint16_t>(label->position_)); |
| 2542 | label->LinkTo(branch_id); |
Vladimir Marko | f38caa6 | 2015-05-29 15:50:18 +0100 | [diff] [blame] | 2543 | } |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 2544 | |
| 2545 | if (use32bit) { |
| 2546 | Emit16(0); |
| 2547 | } |
| 2548 | DCHECK_EQ(buffer_.Size() - pc, GetFixup(branch_id)->GetSizeInBytes()); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2549 | } |
| 2550 | |
| 2551 | |
| 2552 | void Thumb2Assembler::clz(Register rd, Register rm, Condition cond) { |
| 2553 | CHECK_NE(rd, kNoRegister); |
| 2554 | CHECK_NE(rm, kNoRegister); |
| 2555 | CheckCondition(cond); |
| 2556 | CHECK_NE(rd, PC); |
| 2557 | CHECK_NE(rm, PC); |
| 2558 | int32_t encoding = B31 | B30 | B29 | B28 | B27 | |
| 2559 | B25 | B23 | B21 | B20 | |
| 2560 | static_cast<uint32_t>(rm) << 16 | |
| 2561 | 0xf << 12 | |
| 2562 | static_cast<uint32_t>(rd) << 8 | |
| 2563 | B7 | |
| 2564 | static_cast<uint32_t>(rm); |
| 2565 | Emit32(encoding); |
| 2566 | } |
| 2567 | |
| 2568 | |
| 2569 | void Thumb2Assembler::movw(Register rd, uint16_t imm16, Condition cond) { |
| 2570 | CheckCondition(cond); |
| 2571 | bool must_be_32bit = force_32bit_; |
| 2572 | if (IsHighRegister(rd)|| imm16 >= 256u) { |
| 2573 | must_be_32bit = true; |
| 2574 | } |
| 2575 | |
| 2576 | if (must_be_32bit) { |
| 2577 | // Use encoding T3. |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 2578 | uint32_t imm4 = (imm16 >> 12) & 15U /* 0b1111 */; |
| 2579 | uint32_t i = (imm16 >> 11) & 1U /* 0b1 */; |
| 2580 | uint32_t imm3 = (imm16 >> 8) & 7U /* 0b111 */; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2581 | uint32_t imm8 = imm16 & 0xff; |
| 2582 | int32_t encoding = B31 | B30 | B29 | B28 | |
| 2583 | B25 | B22 | |
| 2584 | static_cast<uint32_t>(rd) << 8 | |
| 2585 | i << 26 | |
| 2586 | imm4 << 16 | |
| 2587 | imm3 << 12 | |
| 2588 | imm8; |
| 2589 | Emit32(encoding); |
| 2590 | } else { |
| 2591 | int16_t encoding = B13 | static_cast<uint16_t>(rd) << 8 | |
| 2592 | imm16; |
| 2593 | Emit16(encoding); |
| 2594 | } |
| 2595 | } |
| 2596 | |
| 2597 | |
| 2598 | void Thumb2Assembler::movt(Register rd, uint16_t imm16, Condition cond) { |
| 2599 | CheckCondition(cond); |
| 2600 | // Always 32 bits. |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 2601 | uint32_t imm4 = (imm16 >> 12) & 15U /* 0b1111 */; |
| 2602 | uint32_t i = (imm16 >> 11) & 1U /* 0b1 */; |
| 2603 | uint32_t imm3 = (imm16 >> 8) & 7U /* 0b111 */; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2604 | uint32_t imm8 = imm16 & 0xff; |
| 2605 | int32_t encoding = B31 | B30 | B29 | B28 | |
| 2606 | B25 | B23 | B22 | |
| 2607 | static_cast<uint32_t>(rd) << 8 | |
| 2608 | i << 26 | |
| 2609 | imm4 << 16 | |
| 2610 | imm3 << 12 | |
| 2611 | imm8; |
| 2612 | Emit32(encoding); |
| 2613 | } |
| 2614 | |
| 2615 | |
Scott Wakeling | 9ee23f4 | 2015-07-23 10:44:35 +0100 | [diff] [blame] | 2616 | void Thumb2Assembler::rbit(Register rd, Register rm, Condition cond) { |
| 2617 | CHECK_NE(rd, kNoRegister); |
| 2618 | CHECK_NE(rm, kNoRegister); |
| 2619 | CheckCondition(cond); |
| 2620 | CHECK_NE(rd, PC); |
| 2621 | CHECK_NE(rm, PC); |
| 2622 | CHECK_NE(rd, SP); |
| 2623 | CHECK_NE(rm, SP); |
| 2624 | int32_t encoding = B31 | B30 | B29 | B28 | B27 | |
| 2625 | B25 | B23 | B20 | |
| 2626 | static_cast<uint32_t>(rm) << 16 | |
| 2627 | 0xf << 12 | |
| 2628 | static_cast<uint32_t>(rd) << 8 | |
| 2629 | B7 | B5 | |
| 2630 | static_cast<uint32_t>(rm); |
| 2631 | Emit32(encoding); |
| 2632 | } |
| 2633 | |
| 2634 | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2635 | void Thumb2Assembler::ldrex(Register rt, Register rn, uint16_t imm, Condition cond) { |
| 2636 | CHECK_NE(rn, kNoRegister); |
| 2637 | CHECK_NE(rt, kNoRegister); |
| 2638 | CheckCondition(cond); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2639 | CHECK_LT(imm, (1u << 10)); |
| 2640 | |
| 2641 | int32_t encoding = B31 | B30 | B29 | B27 | B22 | B20 | |
| 2642 | static_cast<uint32_t>(rn) << 16 | |
| 2643 | static_cast<uint32_t>(rt) << 12 | |
| 2644 | 0xf << 8 | |
| 2645 | imm >> 2; |
| 2646 | Emit32(encoding); |
| 2647 | } |
| 2648 | |
| 2649 | |
| 2650 | void Thumb2Assembler::ldrex(Register rt, Register rn, Condition cond) { |
| 2651 | ldrex(rt, rn, 0, cond); |
| 2652 | } |
| 2653 | |
| 2654 | |
| 2655 | void Thumb2Assembler::strex(Register rd, |
| 2656 | Register rt, |
| 2657 | Register rn, |
| 2658 | uint16_t imm, |
| 2659 | Condition cond) { |
| 2660 | CHECK_NE(rn, kNoRegister); |
| 2661 | CHECK_NE(rd, kNoRegister); |
| 2662 | CHECK_NE(rt, kNoRegister); |
| 2663 | CheckCondition(cond); |
| 2664 | CHECK_LT(imm, (1u << 10)); |
| 2665 | |
| 2666 | int32_t encoding = B31 | B30 | B29 | B27 | B22 | |
| 2667 | static_cast<uint32_t>(rn) << 16 | |
| 2668 | static_cast<uint32_t>(rt) << 12 | |
| 2669 | static_cast<uint32_t>(rd) << 8 | |
| 2670 | imm >> 2; |
| 2671 | Emit32(encoding); |
| 2672 | } |
| 2673 | |
| 2674 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2675 | void Thumb2Assembler::ldrexd(Register rt, Register rt2, Register rn, Condition cond) { |
| 2676 | CHECK_NE(rn, kNoRegister); |
| 2677 | CHECK_NE(rt, kNoRegister); |
| 2678 | CHECK_NE(rt2, kNoRegister); |
| 2679 | CHECK_NE(rt, rt2); |
| 2680 | CheckCondition(cond); |
| 2681 | |
| 2682 | int32_t encoding = B31 | B30 | B29 | B27 | B23 | B22 | B20 | |
| 2683 | static_cast<uint32_t>(rn) << 16 | |
| 2684 | static_cast<uint32_t>(rt) << 12 | |
| 2685 | static_cast<uint32_t>(rt2) << 8 | |
| 2686 | B6 | B5 | B4 | B3 | B2 | B1 | B0; |
| 2687 | Emit32(encoding); |
| 2688 | } |
| 2689 | |
| 2690 | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2691 | void Thumb2Assembler::strex(Register rd, |
| 2692 | Register rt, |
| 2693 | Register rn, |
| 2694 | Condition cond) { |
| 2695 | strex(rd, rt, rn, 0, cond); |
| 2696 | } |
| 2697 | |
| 2698 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2699 | void Thumb2Assembler::strexd(Register rd, Register rt, Register rt2, Register rn, Condition cond) { |
| 2700 | CHECK_NE(rd, kNoRegister); |
| 2701 | CHECK_NE(rn, kNoRegister); |
| 2702 | CHECK_NE(rt, kNoRegister); |
| 2703 | CHECK_NE(rt2, kNoRegister); |
| 2704 | CHECK_NE(rt, rt2); |
| 2705 | CHECK_NE(rd, rt); |
| 2706 | CHECK_NE(rd, rt2); |
| 2707 | CheckCondition(cond); |
| 2708 | |
| 2709 | int32_t encoding = B31 | B30 | B29 | B27 | B23 | B22 | |
| 2710 | static_cast<uint32_t>(rn) << 16 | |
| 2711 | static_cast<uint32_t>(rt) << 12 | |
| 2712 | static_cast<uint32_t>(rt2) << 8 | |
| 2713 | B6 | B5 | B4 | |
| 2714 | static_cast<uint32_t>(rd); |
| 2715 | Emit32(encoding); |
| 2716 | } |
| 2717 | |
| 2718 | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2719 | void Thumb2Assembler::clrex(Condition cond) { |
| 2720 | CheckCondition(cond); |
| 2721 | int32_t encoding = B31 | B30 | B29 | B27 | B28 | B25 | B24 | B23 | |
| 2722 | B21 | B20 | |
| 2723 | 0xf << 16 | |
| 2724 | B15 | |
| 2725 | 0xf << 8 | |
| 2726 | B5 | |
| 2727 | 0xf; |
| 2728 | Emit32(encoding); |
| 2729 | } |
| 2730 | |
| 2731 | |
| 2732 | void Thumb2Assembler::nop(Condition cond) { |
| 2733 | CheckCondition(cond); |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 2734 | uint16_t encoding = B15 | B13 | B12 | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2735 | B11 | B10 | B9 | B8; |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 2736 | Emit16(static_cast<int16_t>(encoding)); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2737 | } |
| 2738 | |
| 2739 | |
| 2740 | void Thumb2Assembler::vmovsr(SRegister sn, Register rt, Condition cond) { |
| 2741 | CHECK_NE(sn, kNoSRegister); |
| 2742 | CHECK_NE(rt, kNoRegister); |
| 2743 | CHECK_NE(rt, SP); |
| 2744 | CHECK_NE(rt, PC); |
| 2745 | CheckCondition(cond); |
| 2746 | int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) | |
| 2747 | B27 | B26 | B25 | |
| 2748 | ((static_cast<int32_t>(sn) >> 1)*B16) | |
| 2749 | (static_cast<int32_t>(rt)*B12) | B11 | B9 | |
| 2750 | ((static_cast<int32_t>(sn) & 1)*B7) | B4; |
| 2751 | Emit32(encoding); |
| 2752 | } |
| 2753 | |
| 2754 | |
| 2755 | void Thumb2Assembler::vmovrs(Register rt, SRegister sn, Condition cond) { |
| 2756 | CHECK_NE(sn, kNoSRegister); |
| 2757 | CHECK_NE(rt, kNoRegister); |
| 2758 | CHECK_NE(rt, SP); |
| 2759 | CHECK_NE(rt, PC); |
| 2760 | CheckCondition(cond); |
| 2761 | int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) | |
| 2762 | B27 | B26 | B25 | B20 | |
| 2763 | ((static_cast<int32_t>(sn) >> 1)*B16) | |
| 2764 | (static_cast<int32_t>(rt)*B12) | B11 | B9 | |
| 2765 | ((static_cast<int32_t>(sn) & 1)*B7) | B4; |
| 2766 | Emit32(encoding); |
| 2767 | } |
| 2768 | |
| 2769 | |
| 2770 | void Thumb2Assembler::vmovsrr(SRegister sm, Register rt, Register rt2, |
| 2771 | Condition cond) { |
| 2772 | CHECK_NE(sm, kNoSRegister); |
| 2773 | CHECK_NE(sm, S31); |
| 2774 | CHECK_NE(rt, kNoRegister); |
| 2775 | CHECK_NE(rt, SP); |
| 2776 | CHECK_NE(rt, PC); |
| 2777 | CHECK_NE(rt2, kNoRegister); |
| 2778 | CHECK_NE(rt2, SP); |
| 2779 | CHECK_NE(rt2, PC); |
| 2780 | CheckCondition(cond); |
| 2781 | int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) | |
| 2782 | B27 | B26 | B22 | |
| 2783 | (static_cast<int32_t>(rt2)*B16) | |
| 2784 | (static_cast<int32_t>(rt)*B12) | B11 | B9 | |
| 2785 | ((static_cast<int32_t>(sm) & 1)*B5) | B4 | |
| 2786 | (static_cast<int32_t>(sm) >> 1); |
| 2787 | Emit32(encoding); |
| 2788 | } |
| 2789 | |
| 2790 | |
| 2791 | void Thumb2Assembler::vmovrrs(Register rt, Register rt2, SRegister sm, |
| 2792 | Condition cond) { |
| 2793 | CHECK_NE(sm, kNoSRegister); |
| 2794 | CHECK_NE(sm, S31); |
| 2795 | CHECK_NE(rt, kNoRegister); |
| 2796 | CHECK_NE(rt, SP); |
| 2797 | CHECK_NE(rt, PC); |
| 2798 | CHECK_NE(rt2, kNoRegister); |
| 2799 | CHECK_NE(rt2, SP); |
| 2800 | CHECK_NE(rt2, PC); |
| 2801 | CHECK_NE(rt, rt2); |
| 2802 | CheckCondition(cond); |
| 2803 | int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) | |
| 2804 | B27 | B26 | B22 | B20 | |
| 2805 | (static_cast<int32_t>(rt2)*B16) | |
| 2806 | (static_cast<int32_t>(rt)*B12) | B11 | B9 | |
| 2807 | ((static_cast<int32_t>(sm) & 1)*B5) | B4 | |
| 2808 | (static_cast<int32_t>(sm) >> 1); |
| 2809 | Emit32(encoding); |
| 2810 | } |
| 2811 | |
| 2812 | |
| 2813 | void Thumb2Assembler::vmovdrr(DRegister dm, Register rt, Register rt2, |
| 2814 | Condition cond) { |
| 2815 | CHECK_NE(dm, kNoDRegister); |
| 2816 | CHECK_NE(rt, kNoRegister); |
| 2817 | CHECK_NE(rt, SP); |
| 2818 | CHECK_NE(rt, PC); |
| 2819 | CHECK_NE(rt2, kNoRegister); |
| 2820 | CHECK_NE(rt2, SP); |
| 2821 | CHECK_NE(rt2, PC); |
| 2822 | CheckCondition(cond); |
| 2823 | int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) | |
| 2824 | B27 | B26 | B22 | |
| 2825 | (static_cast<int32_t>(rt2)*B16) | |
| 2826 | (static_cast<int32_t>(rt)*B12) | B11 | B9 | B8 | |
| 2827 | ((static_cast<int32_t>(dm) >> 4)*B5) | B4 | |
| 2828 | (static_cast<int32_t>(dm) & 0xf); |
| 2829 | Emit32(encoding); |
| 2830 | } |
| 2831 | |
| 2832 | |
| 2833 | void Thumb2Assembler::vmovrrd(Register rt, Register rt2, DRegister dm, |
| 2834 | Condition cond) { |
| 2835 | CHECK_NE(dm, kNoDRegister); |
| 2836 | CHECK_NE(rt, kNoRegister); |
| 2837 | CHECK_NE(rt, SP); |
| 2838 | CHECK_NE(rt, PC); |
| 2839 | CHECK_NE(rt2, kNoRegister); |
| 2840 | CHECK_NE(rt2, SP); |
| 2841 | CHECK_NE(rt2, PC); |
| 2842 | CHECK_NE(rt, rt2); |
| 2843 | CheckCondition(cond); |
| 2844 | int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) | |
| 2845 | B27 | B26 | B22 | B20 | |
| 2846 | (static_cast<int32_t>(rt2)*B16) | |
| 2847 | (static_cast<int32_t>(rt)*B12) | B11 | B9 | B8 | |
| 2848 | ((static_cast<int32_t>(dm) >> 4)*B5) | B4 | |
| 2849 | (static_cast<int32_t>(dm) & 0xf); |
| 2850 | Emit32(encoding); |
| 2851 | } |
| 2852 | |
| 2853 | |
| 2854 | void Thumb2Assembler::vldrs(SRegister sd, const Address& ad, Condition cond) { |
| 2855 | const Address& addr = static_cast<const Address&>(ad); |
| 2856 | CHECK_NE(sd, kNoSRegister); |
| 2857 | CheckCondition(cond); |
| 2858 | int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) | |
| 2859 | B27 | B26 | B24 | B20 | |
| 2860 | ((static_cast<int32_t>(sd) & 1)*B22) | |
| 2861 | ((static_cast<int32_t>(sd) >> 1)*B12) | |
| 2862 | B11 | B9 | addr.vencoding(); |
| 2863 | Emit32(encoding); |
| 2864 | } |
| 2865 | |
| 2866 | |
| 2867 | void Thumb2Assembler::vstrs(SRegister sd, const Address& ad, Condition cond) { |
| 2868 | const Address& addr = static_cast<const Address&>(ad); |
| 2869 | CHECK_NE(static_cast<Register>(addr.encodingArm() & (0xf << kRnShift)), PC); |
| 2870 | CHECK_NE(sd, kNoSRegister); |
| 2871 | CheckCondition(cond); |
| 2872 | int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) | |
| 2873 | B27 | B26 | B24 | |
| 2874 | ((static_cast<int32_t>(sd) & 1)*B22) | |
| 2875 | ((static_cast<int32_t>(sd) >> 1)*B12) | |
| 2876 | B11 | B9 | addr.vencoding(); |
| 2877 | Emit32(encoding); |
| 2878 | } |
| 2879 | |
| 2880 | |
| 2881 | void Thumb2Assembler::vldrd(DRegister dd, const Address& ad, Condition cond) { |
| 2882 | const Address& addr = static_cast<const Address&>(ad); |
| 2883 | CHECK_NE(dd, kNoDRegister); |
| 2884 | CheckCondition(cond); |
| 2885 | int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) | |
| 2886 | B27 | B26 | B24 | B20 | |
| 2887 | ((static_cast<int32_t>(dd) >> 4)*B22) | |
| 2888 | ((static_cast<int32_t>(dd) & 0xf)*B12) | |
| 2889 | B11 | B9 | B8 | addr.vencoding(); |
| 2890 | Emit32(encoding); |
| 2891 | } |
| 2892 | |
| 2893 | |
| 2894 | void Thumb2Assembler::vstrd(DRegister dd, const Address& ad, Condition cond) { |
| 2895 | const Address& addr = static_cast<const Address&>(ad); |
| 2896 | CHECK_NE(static_cast<Register>(addr.encodingArm() & (0xf << kRnShift)), PC); |
| 2897 | CHECK_NE(dd, kNoDRegister); |
| 2898 | CheckCondition(cond); |
| 2899 | int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) | |
| 2900 | B27 | B26 | B24 | |
| 2901 | ((static_cast<int32_t>(dd) >> 4)*B22) | |
| 2902 | ((static_cast<int32_t>(dd) & 0xf)*B12) | |
| 2903 | B11 | B9 | B8 | addr.vencoding(); |
| 2904 | Emit32(encoding); |
| 2905 | } |
| 2906 | |
| 2907 | |
| 2908 | void Thumb2Assembler::vpushs(SRegister reg, int nregs, Condition cond) { |
| 2909 | EmitVPushPop(static_cast<uint32_t>(reg), nregs, true, false, cond); |
| 2910 | } |
| 2911 | |
| 2912 | |
| 2913 | void Thumb2Assembler::vpushd(DRegister reg, int nregs, Condition cond) { |
| 2914 | EmitVPushPop(static_cast<uint32_t>(reg), nregs, true, true, cond); |
| 2915 | } |
| 2916 | |
| 2917 | |
| 2918 | void Thumb2Assembler::vpops(SRegister reg, int nregs, Condition cond) { |
| 2919 | EmitVPushPop(static_cast<uint32_t>(reg), nregs, false, false, cond); |
| 2920 | } |
| 2921 | |
| 2922 | |
| 2923 | void Thumb2Assembler::vpopd(DRegister reg, int nregs, Condition cond) { |
| 2924 | EmitVPushPop(static_cast<uint32_t>(reg), nregs, false, true, cond); |
| 2925 | } |
| 2926 | |
| 2927 | |
| 2928 | void Thumb2Assembler::EmitVPushPop(uint32_t reg, int nregs, bool push, bool dbl, Condition cond) { |
| 2929 | CheckCondition(cond); |
| 2930 | |
| 2931 | uint32_t D; |
| 2932 | uint32_t Vd; |
| 2933 | if (dbl) { |
| 2934 | // Encoded as D:Vd. |
| 2935 | D = (reg >> 4) & 1; |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 2936 | Vd = reg & 15U /* 0b1111 */; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2937 | } else { |
| 2938 | // Encoded as Vd:D. |
| 2939 | D = reg & 1; |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 2940 | Vd = (reg >> 1) & 15U /* 0b1111 */; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2941 | } |
| 2942 | int32_t encoding = B27 | B26 | B21 | B19 | B18 | B16 | |
| 2943 | B11 | B9 | |
| 2944 | (dbl ? B8 : 0) | |
| 2945 | (push ? B24 : (B23 | B20)) | |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 2946 | 14U /* 0b1110 */ << 28 | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 2947 | nregs << (dbl ? 1 : 0) | |
| 2948 | D << 22 | |
| 2949 | Vd << 12; |
| 2950 | Emit32(encoding); |
| 2951 | } |
| 2952 | |
| 2953 | |
| 2954 | void Thumb2Assembler::EmitVFPsss(Condition cond, int32_t opcode, |
| 2955 | SRegister sd, SRegister sn, SRegister sm) { |
| 2956 | CHECK_NE(sd, kNoSRegister); |
| 2957 | CHECK_NE(sn, kNoSRegister); |
| 2958 | CHECK_NE(sm, kNoSRegister); |
| 2959 | CheckCondition(cond); |
| 2960 | int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) | |
| 2961 | B27 | B26 | B25 | B11 | B9 | opcode | |
| 2962 | ((static_cast<int32_t>(sd) & 1)*B22) | |
| 2963 | ((static_cast<int32_t>(sn) >> 1)*B16) | |
| 2964 | ((static_cast<int32_t>(sd) >> 1)*B12) | |
| 2965 | ((static_cast<int32_t>(sn) & 1)*B7) | |
| 2966 | ((static_cast<int32_t>(sm) & 1)*B5) | |
| 2967 | (static_cast<int32_t>(sm) >> 1); |
| 2968 | Emit32(encoding); |
| 2969 | } |
| 2970 | |
| 2971 | |
| 2972 | void Thumb2Assembler::EmitVFPddd(Condition cond, int32_t opcode, |
| 2973 | DRegister dd, DRegister dn, DRegister dm) { |
| 2974 | CHECK_NE(dd, kNoDRegister); |
| 2975 | CHECK_NE(dn, kNoDRegister); |
| 2976 | CHECK_NE(dm, kNoDRegister); |
| 2977 | CheckCondition(cond); |
| 2978 | int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) | |
| 2979 | B27 | B26 | B25 | B11 | B9 | B8 | opcode | |
| 2980 | ((static_cast<int32_t>(dd) >> 4)*B22) | |
| 2981 | ((static_cast<int32_t>(dn) & 0xf)*B16) | |
| 2982 | ((static_cast<int32_t>(dd) & 0xf)*B12) | |
| 2983 | ((static_cast<int32_t>(dn) >> 4)*B7) | |
| 2984 | ((static_cast<int32_t>(dm) >> 4)*B5) | |
| 2985 | (static_cast<int32_t>(dm) & 0xf); |
| 2986 | Emit32(encoding); |
| 2987 | } |
| 2988 | |
| 2989 | |
| 2990 | void Thumb2Assembler::EmitVFPsd(Condition cond, int32_t opcode, |
| 2991 | SRegister sd, DRegister dm) { |
| 2992 | CHECK_NE(sd, kNoSRegister); |
| 2993 | CHECK_NE(dm, kNoDRegister); |
| 2994 | CheckCondition(cond); |
| 2995 | int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) | |
| 2996 | B27 | B26 | B25 | B11 | B9 | opcode | |
| 2997 | ((static_cast<int32_t>(sd) & 1)*B22) | |
| 2998 | ((static_cast<int32_t>(sd) >> 1)*B12) | |
| 2999 | ((static_cast<int32_t>(dm) >> 4)*B5) | |
| 3000 | (static_cast<int32_t>(dm) & 0xf); |
| 3001 | Emit32(encoding); |
| 3002 | } |
| 3003 | |
| 3004 | |
| 3005 | void Thumb2Assembler::EmitVFPds(Condition cond, int32_t opcode, |
| 3006 | DRegister dd, SRegister sm) { |
| 3007 | CHECK_NE(dd, kNoDRegister); |
| 3008 | CHECK_NE(sm, kNoSRegister); |
| 3009 | CheckCondition(cond); |
| 3010 | int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) | |
| 3011 | B27 | B26 | B25 | B11 | B9 | opcode | |
| 3012 | ((static_cast<int32_t>(dd) >> 4)*B22) | |
| 3013 | ((static_cast<int32_t>(dd) & 0xf)*B12) | |
| 3014 | ((static_cast<int32_t>(sm) & 1)*B5) | |
| 3015 | (static_cast<int32_t>(sm) >> 1); |
| 3016 | Emit32(encoding); |
| 3017 | } |
| 3018 | |
| 3019 | |
| 3020 | void Thumb2Assembler::vmstat(Condition cond) { // VMRS APSR_nzcv, FPSCR. |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 3021 | CHECK_NE(cond, kNoCondition); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3022 | CheckCondition(cond); |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 3023 | int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) | |
| 3024 | B27 | B26 | B25 | B23 | B22 | B21 | B20 | B16 | |
| 3025 | (static_cast<int32_t>(PC)*B12) | |
| 3026 | B11 | B9 | B4; |
| 3027 | Emit32(encoding); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3028 | } |
| 3029 | |
| 3030 | |
| 3031 | void Thumb2Assembler::svc(uint32_t imm8) { |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 3032 | CHECK(IsUint<8>(imm8)) << imm8; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3033 | int16_t encoding = B15 | B14 | B12 | |
| 3034 | B11 | B10 | B9 | B8 | |
| 3035 | imm8; |
| 3036 | Emit16(encoding); |
| 3037 | } |
| 3038 | |
| 3039 | |
| 3040 | void Thumb2Assembler::bkpt(uint16_t imm8) { |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 3041 | CHECK(IsUint<8>(imm8)) << imm8; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3042 | int16_t encoding = B15 | B13 | B12 | |
| 3043 | B11 | B10 | B9 | |
| 3044 | imm8; |
| 3045 | Emit16(encoding); |
| 3046 | } |
| 3047 | |
| 3048 | // Convert the given IT state to a mask bit given bit 0 of the first |
| 3049 | // condition and a shift position. |
| 3050 | static uint8_t ToItMask(ItState s, uint8_t firstcond0, uint8_t shift) { |
| 3051 | switch (s) { |
| 3052 | case kItOmitted: return 1 << shift; |
| 3053 | case kItThen: return firstcond0 << shift; |
| 3054 | case kItElse: return !firstcond0 << shift; |
| 3055 | } |
| 3056 | return 0; |
| 3057 | } |
| 3058 | |
| 3059 | |
| 3060 | // Set the IT condition in the given position for the given state. This is used |
| 3061 | // to check that conditional instructions match the preceding IT statement. |
| 3062 | void Thumb2Assembler::SetItCondition(ItState s, Condition cond, uint8_t index) { |
| 3063 | switch (s) { |
| 3064 | case kItOmitted: it_conditions_[index] = AL; break; |
| 3065 | case kItThen: it_conditions_[index] = cond; break; |
| 3066 | case kItElse: |
| 3067 | it_conditions_[index] = static_cast<Condition>(static_cast<uint8_t>(cond) ^ 1); |
| 3068 | break; |
| 3069 | } |
| 3070 | } |
| 3071 | |
| 3072 | |
| 3073 | void Thumb2Assembler::it(Condition firstcond, ItState i1, ItState i2, ItState i3) { |
| 3074 | CheckCondition(AL); // Not allowed in IT block. |
| 3075 | uint8_t firstcond0 = static_cast<uint8_t>(firstcond) & 1; |
| 3076 | |
| 3077 | // All conditions to AL. |
| 3078 | for (uint8_t i = 0; i < 4; ++i) { |
| 3079 | it_conditions_[i] = AL; |
| 3080 | } |
| 3081 | |
| 3082 | SetItCondition(kItThen, firstcond, 0); |
| 3083 | uint8_t mask = ToItMask(i1, firstcond0, 3); |
| 3084 | SetItCondition(i1, firstcond, 1); |
| 3085 | |
| 3086 | if (i1 != kItOmitted) { |
| 3087 | mask |= ToItMask(i2, firstcond0, 2); |
| 3088 | SetItCondition(i2, firstcond, 2); |
| 3089 | if (i2 != kItOmitted) { |
| 3090 | mask |= ToItMask(i3, firstcond0, 1); |
| 3091 | SetItCondition(i3, firstcond, 3); |
| 3092 | if (i3 != kItOmitted) { |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 3093 | mask |= 1U /* 0b0001 */; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3094 | } |
| 3095 | } |
| 3096 | } |
| 3097 | |
| 3098 | // Start at first condition. |
| 3099 | it_cond_index_ = 0; |
| 3100 | next_condition_ = it_conditions_[0]; |
| 3101 | uint16_t encoding = B15 | B13 | B12 | |
| 3102 | B11 | B10 | B9 | B8 | |
| 3103 | firstcond << 4 | |
| 3104 | mask; |
| 3105 | Emit16(encoding); |
| 3106 | } |
| 3107 | |
| 3108 | |
| 3109 | void Thumb2Assembler::cbz(Register rn, Label* label) { |
| 3110 | CheckCondition(AL); |
| 3111 | if (label->IsBound()) { |
| 3112 | LOG(FATAL) << "cbz can only be used to branch forwards"; |
Vladimir Marko | e8469c1 | 2014-11-26 18:09:30 +0000 | [diff] [blame] | 3113 | UNREACHABLE(); |
Nicolas Geoffray | d56376c | 2015-05-21 12:32:34 +0000 | [diff] [blame] | 3114 | } else if (IsHighRegister(rn)) { |
| 3115 | LOG(FATAL) << "cbz can only be used with low registers"; |
| 3116 | UNREACHABLE(); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3117 | } else { |
| 3118 | uint16_t branchid = EmitCompareAndBranch(rn, static_cast<uint16_t>(label->position_), false); |
| 3119 | label->LinkTo(branchid); |
| 3120 | } |
| 3121 | } |
| 3122 | |
| 3123 | |
| 3124 | void Thumb2Assembler::cbnz(Register rn, Label* label) { |
| 3125 | CheckCondition(AL); |
| 3126 | if (label->IsBound()) { |
| 3127 | LOG(FATAL) << "cbnz can only be used to branch forwards"; |
Vladimir Marko | e8469c1 | 2014-11-26 18:09:30 +0000 | [diff] [blame] | 3128 | UNREACHABLE(); |
Nicolas Geoffray | d56376c | 2015-05-21 12:32:34 +0000 | [diff] [blame] | 3129 | } else if (IsHighRegister(rn)) { |
| 3130 | LOG(FATAL) << "cbnz can only be used with low registers"; |
| 3131 | UNREACHABLE(); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3132 | } else { |
| 3133 | uint16_t branchid = EmitCompareAndBranch(rn, static_cast<uint16_t>(label->position_), true); |
| 3134 | label->LinkTo(branchid); |
| 3135 | } |
| 3136 | } |
| 3137 | |
| 3138 | |
| 3139 | void Thumb2Assembler::blx(Register rm, Condition cond) { |
| 3140 | CHECK_NE(rm, kNoRegister); |
| 3141 | CheckCondition(cond); |
| 3142 | int16_t encoding = B14 | B10 | B9 | B8 | B7 | static_cast<int16_t>(rm) << 3; |
| 3143 | Emit16(encoding); |
| 3144 | } |
| 3145 | |
| 3146 | |
| 3147 | void Thumb2Assembler::bx(Register rm, Condition cond) { |
| 3148 | CHECK_NE(rm, kNoRegister); |
| 3149 | CheckCondition(cond); |
| 3150 | int16_t encoding = B14 | B10 | B9 | B8 | static_cast<int16_t>(rm) << 3; |
| 3151 | Emit16(encoding); |
| 3152 | } |
| 3153 | |
| 3154 | |
| 3155 | void Thumb2Assembler::Push(Register rd, Condition cond) { |
| 3156 | str(rd, Address(SP, -kRegisterSize, Address::PreIndex), cond); |
| 3157 | } |
| 3158 | |
| 3159 | |
| 3160 | void Thumb2Assembler::Pop(Register rd, Condition cond) { |
| 3161 | ldr(rd, Address(SP, kRegisterSize, Address::PostIndex), cond); |
| 3162 | } |
| 3163 | |
| 3164 | |
| 3165 | void Thumb2Assembler::PushList(RegList regs, Condition cond) { |
| 3166 | stm(DB_W, SP, regs, cond); |
| 3167 | } |
| 3168 | |
| 3169 | |
| 3170 | void Thumb2Assembler::PopList(RegList regs, Condition cond) { |
| 3171 | ldm(IA_W, SP, regs, cond); |
| 3172 | } |
| 3173 | |
| 3174 | |
| 3175 | void Thumb2Assembler::Mov(Register rd, Register rm, Condition cond) { |
| 3176 | if (cond != AL || rd != rm) { |
| 3177 | mov(rd, ShifterOperand(rm), cond); |
| 3178 | } |
| 3179 | } |
| 3180 | |
| 3181 | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3182 | void Thumb2Assembler::Bind(Label* label) { |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 3183 | BindLabel(label, buffer_.Size()); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3184 | } |
| 3185 | |
| 3186 | |
| 3187 | void Thumb2Assembler::Lsl(Register rd, Register rm, uint32_t shift_imm, |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 3188 | Condition cond, SetCc set_cc) { |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 3189 | CHECK_LE(shift_imm, 31u); |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 3190 | CheckCondition(cond); |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 3191 | EmitShift(rd, rm, LSL, shift_imm, cond, set_cc); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3192 | } |
| 3193 | |
| 3194 | |
| 3195 | void Thumb2Assembler::Lsr(Register rd, Register rm, uint32_t shift_imm, |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 3196 | Condition cond, SetCc set_cc) { |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 3197 | CHECK(1u <= shift_imm && shift_imm <= 32u); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3198 | if (shift_imm == 32) shift_imm = 0; // Comply to UAL syntax. |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 3199 | CheckCondition(cond); |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 3200 | EmitShift(rd, rm, LSR, shift_imm, cond, set_cc); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3201 | } |
| 3202 | |
| 3203 | |
| 3204 | void Thumb2Assembler::Asr(Register rd, Register rm, uint32_t shift_imm, |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 3205 | Condition cond, SetCc set_cc) { |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 3206 | CHECK(1u <= shift_imm && shift_imm <= 32u); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3207 | if (shift_imm == 32) shift_imm = 0; // Comply to UAL syntax. |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 3208 | CheckCondition(cond); |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 3209 | EmitShift(rd, rm, ASR, shift_imm, cond, set_cc); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3210 | } |
| 3211 | |
| 3212 | |
| 3213 | void Thumb2Assembler::Ror(Register rd, Register rm, uint32_t shift_imm, |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 3214 | Condition cond, SetCc set_cc) { |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 3215 | CHECK(1u <= shift_imm && shift_imm <= 31u); |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 3216 | CheckCondition(cond); |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 3217 | EmitShift(rd, rm, ROR, shift_imm, cond, set_cc); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3218 | } |
| 3219 | |
| 3220 | |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 3221 | void Thumb2Assembler::Rrx(Register rd, Register rm, Condition cond, SetCc set_cc) { |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 3222 | CheckCondition(cond); |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 3223 | EmitShift(rd, rm, RRX, rm, cond, set_cc); |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 3224 | } |
| 3225 | |
| 3226 | |
| 3227 | void Thumb2Assembler::Lsl(Register rd, Register rm, Register rn, |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 3228 | Condition cond, SetCc set_cc) { |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 3229 | CheckCondition(cond); |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 3230 | EmitShift(rd, rm, LSL, rn, cond, set_cc); |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 3231 | } |
| 3232 | |
| 3233 | |
| 3234 | void Thumb2Assembler::Lsr(Register rd, Register rm, Register rn, |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 3235 | Condition cond, SetCc set_cc) { |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 3236 | CheckCondition(cond); |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 3237 | EmitShift(rd, rm, LSR, rn, cond, set_cc); |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 3238 | } |
| 3239 | |
| 3240 | |
| 3241 | void Thumb2Assembler::Asr(Register rd, Register rm, Register rn, |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 3242 | Condition cond, SetCc set_cc) { |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 3243 | CheckCondition(cond); |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 3244 | EmitShift(rd, rm, ASR, rn, cond, set_cc); |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 3245 | } |
| 3246 | |
| 3247 | |
| 3248 | void Thumb2Assembler::Ror(Register rd, Register rm, Register rn, |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 3249 | Condition cond, SetCc set_cc) { |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 3250 | CheckCondition(cond); |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 3251 | EmitShift(rd, rm, ROR, rn, cond, set_cc); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3252 | } |
| 3253 | |
| 3254 | |
| 3255 | int32_t Thumb2Assembler::EncodeBranchOffset(int32_t offset, int32_t inst) { |
| 3256 | // The offset is off by 4 due to the way the ARM CPUs read PC. |
| 3257 | offset -= 4; |
| 3258 | offset >>= 1; |
| 3259 | |
| 3260 | uint32_t value = 0; |
| 3261 | // There are two different encodings depending on the value of bit 12. In one case |
| 3262 | // intermediate values are calculated using the sign bit. |
| 3263 | if ((inst & B12) == B12) { |
| 3264 | // 25 bits of offset. |
| 3265 | uint32_t signbit = (offset >> 31) & 0x1; |
| 3266 | uint32_t i1 = (offset >> 22) & 0x1; |
| 3267 | uint32_t i2 = (offset >> 21) & 0x1; |
| 3268 | uint32_t imm10 = (offset >> 11) & 0x03ff; |
| 3269 | uint32_t imm11 = offset & 0x07ff; |
| 3270 | uint32_t j1 = (i1 ^ signbit) ? 0 : 1; |
| 3271 | uint32_t j2 = (i2 ^ signbit) ? 0 : 1; |
| 3272 | value = (signbit << 26) | (j1 << 13) | (j2 << 11) | (imm10 << 16) | |
| 3273 | imm11; |
| 3274 | // Remove the offset from the current encoding. |
| 3275 | inst &= ~(0x3ff << 16 | 0x7ff); |
| 3276 | } else { |
| 3277 | uint32_t signbit = (offset >> 31) & 0x1; |
| 3278 | uint32_t imm6 = (offset >> 11) & 0x03f; |
| 3279 | uint32_t imm11 = offset & 0x07ff; |
| 3280 | uint32_t j1 = (offset >> 19) & 1; |
| 3281 | uint32_t j2 = (offset >> 17) & 1; |
| 3282 | value = (signbit << 26) | (j1 << 13) | (j2 << 11) | (imm6 << 16) | |
| 3283 | imm11; |
| 3284 | // Remove the offset from the current encoding. |
| 3285 | inst &= ~(0x3f << 16 | 0x7ff); |
| 3286 | } |
| 3287 | // Mask out offset bits in current instruction. |
| 3288 | inst &= ~(B26 | B13 | B11); |
| 3289 | inst |= value; |
| 3290 | return inst; |
| 3291 | } |
| 3292 | |
| 3293 | |
| 3294 | int Thumb2Assembler::DecodeBranchOffset(int32_t instr) { |
| 3295 | int32_t imm32; |
| 3296 | if ((instr & B12) == B12) { |
| 3297 | uint32_t S = (instr >> 26) & 1; |
| 3298 | uint32_t J2 = (instr >> 11) & 1; |
| 3299 | uint32_t J1 = (instr >> 13) & 1; |
| 3300 | uint32_t imm10 = (instr >> 16) & 0x3FF; |
| 3301 | uint32_t imm11 = instr & 0x7FF; |
| 3302 | |
| 3303 | uint32_t I1 = ~(J1 ^ S) & 1; |
| 3304 | uint32_t I2 = ~(J2 ^ S) & 1; |
| 3305 | imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1); |
| 3306 | imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate. |
| 3307 | } else { |
| 3308 | uint32_t S = (instr >> 26) & 1; |
| 3309 | uint32_t J2 = (instr >> 11) & 1; |
| 3310 | uint32_t J1 = (instr >> 13) & 1; |
| 3311 | uint32_t imm6 = (instr >> 16) & 0x3F; |
| 3312 | uint32_t imm11 = instr & 0x7FF; |
| 3313 | |
| 3314 | imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1); |
| 3315 | imm32 = (imm32 << 11) >> 11; // sign extend 21 bit immediate. |
| 3316 | } |
| 3317 | imm32 += 4; |
| 3318 | return imm32; |
| 3319 | } |
| 3320 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 3321 | uint32_t Thumb2Assembler::GetAdjustedPosition(uint32_t old_position) { |
| 3322 | // We can reconstruct the adjustment by going through all the fixups from the beginning |
| 3323 | // up to the old_position. Since we expect AdjustedPosition() to be called in a loop |
| 3324 | // with increasing old_position, we can use the data from last AdjustedPosition() to |
| 3325 | // continue where we left off and the whole loop should be O(m+n) where m is the number |
| 3326 | // of positions to adjust and n is the number of fixups. |
| 3327 | if (old_position < last_old_position_) { |
| 3328 | last_position_adjustment_ = 0u; |
| 3329 | last_old_position_ = 0u; |
| 3330 | last_fixup_id_ = 0u; |
| 3331 | } |
| 3332 | while (last_fixup_id_ != fixups_.size()) { |
| 3333 | Fixup* fixup = GetFixup(last_fixup_id_); |
| 3334 | if (fixup->GetLocation() >= old_position + last_position_adjustment_) { |
| 3335 | break; |
| 3336 | } |
| 3337 | if (fixup->GetSize() != fixup->GetOriginalSize()) { |
| 3338 | last_position_adjustment_ += fixup->GetSizeInBytes() - fixup->GetOriginalSizeInBytes(); |
| 3339 | } |
| 3340 | ++last_fixup_id_; |
| 3341 | } |
| 3342 | last_old_position_ = old_position; |
| 3343 | return old_position + last_position_adjustment_; |
| 3344 | } |
| 3345 | |
| 3346 | Literal* Thumb2Assembler::NewLiteral(size_t size, const uint8_t* data) { |
| 3347 | DCHECK(size == 4u || size == 8u) << size; |
| 3348 | literals_.emplace_back(size, data); |
| 3349 | return &literals_.back(); |
| 3350 | } |
| 3351 | |
| 3352 | void Thumb2Assembler::LoadLiteral(Register rt, Literal* literal) { |
| 3353 | DCHECK_EQ(literal->GetSize(), 4u); |
| 3354 | DCHECK(!literal->GetLabel()->IsBound()); |
| 3355 | bool use32bit = IsForced32Bit() || IsHighRegister(rt); |
| 3356 | uint32_t location = buffer_.Size(); |
| 3357 | Fixup::Size size = use32bit ? Fixup::kLiteral4KiB : Fixup::kLiteral1KiB; |
| 3358 | FixupId fixup_id = AddFixup(Fixup::LoadNarrowLiteral(location, rt, size)); |
| 3359 | Emit16(static_cast<uint16_t>(literal->GetLabel()->position_)); |
| 3360 | literal->GetLabel()->LinkTo(fixup_id); |
| 3361 | if (use32bit) { |
| 3362 | Emit16(0); |
| 3363 | } |
| 3364 | DCHECK_EQ(location + GetFixup(fixup_id)->GetSizeInBytes(), buffer_.Size()); |
| 3365 | } |
| 3366 | |
| 3367 | void Thumb2Assembler::LoadLiteral(Register rt, Register rt2, Literal* literal) { |
| 3368 | DCHECK_EQ(literal->GetSize(), 8u); |
| 3369 | DCHECK(!literal->GetLabel()->IsBound()); |
| 3370 | uint32_t location = buffer_.Size(); |
| 3371 | FixupId fixup_id = |
| 3372 | AddFixup(Fixup::LoadWideLiteral(location, rt, rt2, Fixup::kLongOrFPLiteral1KiB)); |
| 3373 | Emit16(static_cast<uint16_t>(literal->GetLabel()->position_)); |
| 3374 | literal->GetLabel()->LinkTo(fixup_id); |
| 3375 | Emit16(0); |
| 3376 | DCHECK_EQ(location + GetFixup(fixup_id)->GetSizeInBytes(), buffer_.Size()); |
| 3377 | } |
| 3378 | |
| 3379 | void Thumb2Assembler::LoadLiteral(SRegister sd, Literal* literal) { |
| 3380 | DCHECK_EQ(literal->GetSize(), 4u); |
| 3381 | DCHECK(!literal->GetLabel()->IsBound()); |
| 3382 | uint32_t location = buffer_.Size(); |
| 3383 | FixupId fixup_id = AddFixup(Fixup::LoadSingleLiteral(location, sd, Fixup::kLongOrFPLiteral1KiB)); |
| 3384 | Emit16(static_cast<uint16_t>(literal->GetLabel()->position_)); |
| 3385 | literal->GetLabel()->LinkTo(fixup_id); |
| 3386 | Emit16(0); |
| 3387 | DCHECK_EQ(location + GetFixup(fixup_id)->GetSizeInBytes(), buffer_.Size()); |
| 3388 | } |
| 3389 | |
| 3390 | void Thumb2Assembler::LoadLiteral(DRegister dd, Literal* literal) { |
| 3391 | DCHECK_EQ(literal->GetSize(), 8u); |
| 3392 | DCHECK(!literal->GetLabel()->IsBound()); |
| 3393 | uint32_t location = buffer_.Size(); |
| 3394 | FixupId fixup_id = AddFixup(Fixup::LoadDoubleLiteral(location, dd, Fixup::kLongOrFPLiteral1KiB)); |
| 3395 | Emit16(static_cast<uint16_t>(literal->GetLabel()->position_)); |
| 3396 | literal->GetLabel()->LinkTo(fixup_id); |
| 3397 | Emit16(0); |
| 3398 | DCHECK_EQ(location + GetFixup(fixup_id)->GetSizeInBytes(), buffer_.Size()); |
| 3399 | } |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3400 | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3401 | |
| 3402 | void Thumb2Assembler::AddConstant(Register rd, Register rn, int32_t value, |
Vladimir Marko | 449b109 | 2015-09-08 12:16:45 +0100 | [diff] [blame] | 3403 | Condition cond, SetCc set_cc) { |
| 3404 | if (value == 0 && set_cc != kCcSet) { |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3405 | if (rd != rn) { |
| 3406 | mov(rd, ShifterOperand(rn), cond); |
| 3407 | } |
| 3408 | return; |
| 3409 | } |
| 3410 | // We prefer to select the shorter code sequence rather than selecting add for |
| 3411 | // positive values and sub for negatives ones, which would slightly improve |
| 3412 | // the readability of generated code for some constants. |
| 3413 | ShifterOperand shifter_op; |
Nicolas Geoffray | 3bcc8ea | 2014-11-28 15:00:02 +0000 | [diff] [blame] | 3414 | if (ShifterOperandCanHold(rd, rn, ADD, value, &shifter_op)) { |
Vladimir Marko | 449b109 | 2015-09-08 12:16:45 +0100 | [diff] [blame] | 3415 | add(rd, rn, shifter_op, cond, set_cc); |
Nicolas Geoffray | 3bcc8ea | 2014-11-28 15:00:02 +0000 | [diff] [blame] | 3416 | } else if (ShifterOperandCanHold(rd, rn, SUB, -value, &shifter_op)) { |
Vladimir Marko | 449b109 | 2015-09-08 12:16:45 +0100 | [diff] [blame] | 3417 | sub(rd, rn, shifter_op, cond, set_cc); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3418 | } else { |
| 3419 | CHECK(rn != IP); |
Nicolas Geoffray | 3bcc8ea | 2014-11-28 15:00:02 +0000 | [diff] [blame] | 3420 | if (ShifterOperandCanHold(rd, rn, MVN, ~value, &shifter_op)) { |
Vladimir Marko | 449b109 | 2015-09-08 12:16:45 +0100 | [diff] [blame] | 3421 | mvn(IP, shifter_op, cond, kCcKeep); |
| 3422 | add(rd, rn, ShifterOperand(IP), cond, set_cc); |
Nicolas Geoffray | 3bcc8ea | 2014-11-28 15:00:02 +0000 | [diff] [blame] | 3423 | } else if (ShifterOperandCanHold(rd, rn, MVN, ~(-value), &shifter_op)) { |
Vladimir Marko | 449b109 | 2015-09-08 12:16:45 +0100 | [diff] [blame] | 3424 | mvn(IP, shifter_op, cond, kCcKeep); |
| 3425 | sub(rd, rn, ShifterOperand(IP), cond, set_cc); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3426 | } else { |
| 3427 | movw(IP, Low16Bits(value), cond); |
| 3428 | uint16_t value_high = High16Bits(value); |
| 3429 | if (value_high != 0) { |
| 3430 | movt(IP, value_high, cond); |
| 3431 | } |
Vladimir Marko | 449b109 | 2015-09-08 12:16:45 +0100 | [diff] [blame] | 3432 | add(rd, rn, ShifterOperand(IP), cond, set_cc); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3433 | } |
| 3434 | } |
| 3435 | } |
| 3436 | |
Andreas Gampe | 7cffc3b | 2015-10-19 21:31:53 -0700 | [diff] [blame] | 3437 | void Thumb2Assembler::CmpConstant(Register rn, int32_t value, Condition cond) { |
| 3438 | // We prefer to select the shorter code sequence rather than selecting add for |
| 3439 | // positive values and sub for negatives ones, which would slightly improve |
| 3440 | // the readability of generated code for some constants. |
| 3441 | ShifterOperand shifter_op; |
| 3442 | if (ShifterOperandCanHold(kNoRegister, rn, CMP, value, &shifter_op)) { |
| 3443 | cmp(rn, shifter_op, cond); |
| 3444 | } else if (ShifterOperandCanHold(kNoRegister, rn, CMN, ~value, &shifter_op)) { |
| 3445 | cmn(rn, shifter_op, cond); |
| 3446 | } else { |
| 3447 | CHECK(rn != IP); |
| 3448 | movw(IP, Low16Bits(value), cond); |
| 3449 | uint16_t value_high = High16Bits(value); |
| 3450 | if (value_high != 0) { |
| 3451 | movt(IP, value_high, cond); |
| 3452 | } |
| 3453 | cmp(rn, ShifterOperand(IP), cond); |
| 3454 | } |
| 3455 | } |
Nicolas Geoffray | 3bcc8ea | 2014-11-28 15:00:02 +0000 | [diff] [blame] | 3456 | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3457 | void Thumb2Assembler::LoadImmediate(Register rd, int32_t value, Condition cond) { |
| 3458 | ShifterOperand shifter_op; |
Nicolas Geoffray | 3bcc8ea | 2014-11-28 15:00:02 +0000 | [diff] [blame] | 3459 | if (ShifterOperandCanHold(rd, R0, MOV, value, &shifter_op)) { |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3460 | mov(rd, shifter_op, cond); |
Nicolas Geoffray | 3bcc8ea | 2014-11-28 15:00:02 +0000 | [diff] [blame] | 3461 | } else if (ShifterOperandCanHold(rd, R0, MVN, ~value, &shifter_op)) { |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3462 | mvn(rd, shifter_op, cond); |
| 3463 | } else { |
| 3464 | movw(rd, Low16Bits(value), cond); |
| 3465 | uint16_t value_high = High16Bits(value); |
| 3466 | if (value_high != 0) { |
| 3467 | movt(rd, value_high, cond); |
| 3468 | } |
| 3469 | } |
| 3470 | } |
| 3471 | |
Nicolas Geoffray | 3bcc8ea | 2014-11-28 15:00:02 +0000 | [diff] [blame] | 3472 | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3473 | // Implementation note: this method must emit at most one instruction when |
| 3474 | // Address::CanHoldLoadOffsetThumb. |
| 3475 | void Thumb2Assembler::LoadFromOffset(LoadOperandType type, |
| 3476 | Register reg, |
| 3477 | Register base, |
| 3478 | int32_t offset, |
| 3479 | Condition cond) { |
| 3480 | if (!Address::CanHoldLoadOffsetThumb(type, offset)) { |
Roland Levillain | 775ef49 | 2014-11-04 17:43:11 +0000 | [diff] [blame] | 3481 | CHECK_NE(base, IP); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3482 | LoadImmediate(IP, offset, cond); |
| 3483 | add(IP, IP, ShifterOperand(base), cond); |
| 3484 | base = IP; |
| 3485 | offset = 0; |
| 3486 | } |
| 3487 | CHECK(Address::CanHoldLoadOffsetThumb(type, offset)); |
| 3488 | switch (type) { |
| 3489 | case kLoadSignedByte: |
| 3490 | ldrsb(reg, Address(base, offset), cond); |
| 3491 | break; |
| 3492 | case kLoadUnsignedByte: |
| 3493 | ldrb(reg, Address(base, offset), cond); |
| 3494 | break; |
| 3495 | case kLoadSignedHalfword: |
| 3496 | ldrsh(reg, Address(base, offset), cond); |
| 3497 | break; |
| 3498 | case kLoadUnsignedHalfword: |
| 3499 | ldrh(reg, Address(base, offset), cond); |
| 3500 | break; |
| 3501 | case kLoadWord: |
| 3502 | ldr(reg, Address(base, offset), cond); |
| 3503 | break; |
| 3504 | case kLoadWordPair: |
| 3505 | ldrd(reg, Address(base, offset), cond); |
| 3506 | break; |
| 3507 | default: |
| 3508 | LOG(FATAL) << "UNREACHABLE"; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 3509 | UNREACHABLE(); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3510 | } |
| 3511 | } |
| 3512 | |
| 3513 | |
| 3514 | // Implementation note: this method must emit at most one instruction when |
| 3515 | // Address::CanHoldLoadOffsetThumb, as expected by JIT::GuardedLoadFromOffset. |
| 3516 | void Thumb2Assembler::LoadSFromOffset(SRegister reg, |
| 3517 | Register base, |
| 3518 | int32_t offset, |
| 3519 | Condition cond) { |
| 3520 | if (!Address::CanHoldLoadOffsetThumb(kLoadSWord, offset)) { |
| 3521 | CHECK_NE(base, IP); |
| 3522 | LoadImmediate(IP, offset, cond); |
| 3523 | add(IP, IP, ShifterOperand(base), cond); |
| 3524 | base = IP; |
| 3525 | offset = 0; |
| 3526 | } |
| 3527 | CHECK(Address::CanHoldLoadOffsetThumb(kLoadSWord, offset)); |
| 3528 | vldrs(reg, Address(base, offset), cond); |
| 3529 | } |
| 3530 | |
| 3531 | |
| 3532 | // Implementation note: this method must emit at most one instruction when |
| 3533 | // Address::CanHoldLoadOffsetThumb, as expected by JIT::GuardedLoadFromOffset. |
| 3534 | void Thumb2Assembler::LoadDFromOffset(DRegister reg, |
| 3535 | Register base, |
| 3536 | int32_t offset, |
| 3537 | Condition cond) { |
| 3538 | if (!Address::CanHoldLoadOffsetThumb(kLoadDWord, offset)) { |
| 3539 | CHECK_NE(base, IP); |
| 3540 | LoadImmediate(IP, offset, cond); |
| 3541 | add(IP, IP, ShifterOperand(base), cond); |
| 3542 | base = IP; |
| 3543 | offset = 0; |
| 3544 | } |
| 3545 | CHECK(Address::CanHoldLoadOffsetThumb(kLoadDWord, offset)); |
| 3546 | vldrd(reg, Address(base, offset), cond); |
| 3547 | } |
| 3548 | |
| 3549 | |
| 3550 | // Implementation note: this method must emit at most one instruction when |
| 3551 | // Address::CanHoldStoreOffsetThumb. |
| 3552 | void Thumb2Assembler::StoreToOffset(StoreOperandType type, |
| 3553 | Register reg, |
| 3554 | Register base, |
| 3555 | int32_t offset, |
| 3556 | Condition cond) { |
Roland Levillain | 775ef49 | 2014-11-04 17:43:11 +0000 | [diff] [blame] | 3557 | Register tmp_reg = kNoRegister; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3558 | if (!Address::CanHoldStoreOffsetThumb(type, offset)) { |
Roland Levillain | 775ef49 | 2014-11-04 17:43:11 +0000 | [diff] [blame] | 3559 | CHECK_NE(base, IP); |
Roland Levillain | 23f02f3 | 2015-08-25 18:23:20 +0100 | [diff] [blame] | 3560 | if ((reg != IP) && |
| 3561 | ((type != kStoreWordPair) || (reg + 1 != IP))) { |
Roland Levillain | 775ef49 | 2014-11-04 17:43:11 +0000 | [diff] [blame] | 3562 | tmp_reg = IP; |
| 3563 | } else { |
Roland Levillain | 4af147e | 2015-04-07 13:54:49 +0100 | [diff] [blame] | 3564 | // Be careful not to use IP twice (for `reg` (or `reg` + 1 in |
Roland Levillain | 23f02f3 | 2015-08-25 18:23:20 +0100 | [diff] [blame] | 3565 | // the case of a word-pair store) and `base`) to build the |
| 3566 | // Address object used by the store instruction(s) below. |
| 3567 | // Instead, save R5 on the stack (or R6 if R5 is already used by |
| 3568 | // `base`), use it as secondary temporary register, and restore |
| 3569 | // it after the store instruction has been emitted. |
| 3570 | tmp_reg = (base != R5) ? R5 : R6; |
Roland Levillain | 775ef49 | 2014-11-04 17:43:11 +0000 | [diff] [blame] | 3571 | Push(tmp_reg); |
| 3572 | if (base == SP) { |
| 3573 | offset += kRegisterSize; |
| 3574 | } |
| 3575 | } |
| 3576 | LoadImmediate(tmp_reg, offset, cond); |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 3577 | add(tmp_reg, tmp_reg, ShifterOperand(base), AL); |
Roland Levillain | 775ef49 | 2014-11-04 17:43:11 +0000 | [diff] [blame] | 3578 | base = tmp_reg; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3579 | offset = 0; |
| 3580 | } |
| 3581 | CHECK(Address::CanHoldStoreOffsetThumb(type, offset)); |
| 3582 | switch (type) { |
| 3583 | case kStoreByte: |
| 3584 | strb(reg, Address(base, offset), cond); |
| 3585 | break; |
| 3586 | case kStoreHalfword: |
| 3587 | strh(reg, Address(base, offset), cond); |
| 3588 | break; |
| 3589 | case kStoreWord: |
| 3590 | str(reg, Address(base, offset), cond); |
| 3591 | break; |
| 3592 | case kStoreWordPair: |
| 3593 | strd(reg, Address(base, offset), cond); |
| 3594 | break; |
| 3595 | default: |
| 3596 | LOG(FATAL) << "UNREACHABLE"; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 3597 | UNREACHABLE(); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3598 | } |
Roland Levillain | 23f02f3 | 2015-08-25 18:23:20 +0100 | [diff] [blame] | 3599 | if ((tmp_reg != kNoRegister) && (tmp_reg != IP)) { |
| 3600 | CHECK((tmp_reg == R5) || (tmp_reg == R6)); |
Roland Levillain | 775ef49 | 2014-11-04 17:43:11 +0000 | [diff] [blame] | 3601 | Pop(tmp_reg); |
| 3602 | } |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3603 | } |
| 3604 | |
| 3605 | |
| 3606 | // Implementation note: this method must emit at most one instruction when |
| 3607 | // Address::CanHoldStoreOffsetThumb, as expected by JIT::GuardedStoreToOffset. |
| 3608 | void Thumb2Assembler::StoreSToOffset(SRegister reg, |
| 3609 | Register base, |
| 3610 | int32_t offset, |
| 3611 | Condition cond) { |
| 3612 | if (!Address::CanHoldStoreOffsetThumb(kStoreSWord, offset)) { |
| 3613 | CHECK_NE(base, IP); |
| 3614 | LoadImmediate(IP, offset, cond); |
| 3615 | add(IP, IP, ShifterOperand(base), cond); |
| 3616 | base = IP; |
| 3617 | offset = 0; |
| 3618 | } |
| 3619 | CHECK(Address::CanHoldStoreOffsetThumb(kStoreSWord, offset)); |
| 3620 | vstrs(reg, Address(base, offset), cond); |
| 3621 | } |
| 3622 | |
| 3623 | |
| 3624 | // Implementation note: this method must emit at most one instruction when |
| 3625 | // Address::CanHoldStoreOffsetThumb, as expected by JIT::GuardedStoreSToOffset. |
| 3626 | void Thumb2Assembler::StoreDToOffset(DRegister reg, |
| 3627 | Register base, |
| 3628 | int32_t offset, |
| 3629 | Condition cond) { |
| 3630 | if (!Address::CanHoldStoreOffsetThumb(kStoreDWord, offset)) { |
| 3631 | CHECK_NE(base, IP); |
| 3632 | LoadImmediate(IP, offset, cond); |
| 3633 | add(IP, IP, ShifterOperand(base), cond); |
| 3634 | base = IP; |
| 3635 | offset = 0; |
| 3636 | } |
| 3637 | CHECK(Address::CanHoldStoreOffsetThumb(kStoreDWord, offset)); |
| 3638 | vstrd(reg, Address(base, offset), cond); |
| 3639 | } |
| 3640 | |
| 3641 | |
| 3642 | void Thumb2Assembler::MemoryBarrier(ManagedRegister mscratch) { |
| 3643 | CHECK_EQ(mscratch.AsArm().AsCoreRegister(), R12); |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3644 | dmb(SY); |
| 3645 | } |
| 3646 | |
| 3647 | |
| 3648 | void Thumb2Assembler::dmb(DmbOptions flavor) { |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3649 | int32_t encoding = 0xf3bf8f50; // dmb in T1 encoding. |
| 3650 | Emit32(encoding | flavor); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3651 | } |
| 3652 | |
| 3653 | |
| 3654 | void Thumb2Assembler::CompareAndBranchIfZero(Register r, Label* label) { |
Nicolas Geoffray | 2bcb431 | 2015-07-01 12:22:56 +0100 | [diff] [blame] | 3655 | if (CanRelocateBranches() && IsLowRegister(r) && !label->IsBound()) { |
Nicolas Geoffray | d56376c | 2015-05-21 12:32:34 +0000 | [diff] [blame] | 3656 | cbz(r, label); |
| 3657 | } else { |
| 3658 | cmp(r, ShifterOperand(0)); |
| 3659 | b(label, EQ); |
| 3660 | } |
| 3661 | } |
| 3662 | |
| 3663 | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3664 | void Thumb2Assembler::CompareAndBranchIfNonZero(Register r, Label* label) { |
Nicolas Geoffray | 2bcb431 | 2015-07-01 12:22:56 +0100 | [diff] [blame] | 3665 | if (CanRelocateBranches() && IsLowRegister(r) && !label->IsBound()) { |
Nicolas Geoffray | d126ba1 | 2015-05-20 11:25:27 +0100 | [diff] [blame] | 3666 | cbnz(r, label); |
| 3667 | } else { |
Nicolas Geoffray | 1a43dd7 | 2014-07-17 15:15:34 +0100 | [diff] [blame] | 3668 | cmp(r, ShifterOperand(0)); |
| 3669 | b(label, NE); |
Nicolas Geoffray | 1a43dd7 | 2014-07-17 15:15:34 +0100 | [diff] [blame] | 3670 | } |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3671 | } |
Andreas Gampe | 7cffc3b | 2015-10-19 21:31:53 -0700 | [diff] [blame] | 3672 | |
| 3673 | JumpTable* Thumb2Assembler::CreateJumpTable(std::vector<Label*>&& labels, Register base_reg) { |
| 3674 | jump_tables_.emplace_back(std::move(labels)); |
| 3675 | JumpTable* table = &jump_tables_.back(); |
| 3676 | DCHECK(!table->GetLabel()->IsBound()); |
| 3677 | |
| 3678 | bool use32bit = IsForced32Bit() || IsHighRegister(base_reg); |
| 3679 | uint32_t location = buffer_.Size(); |
| 3680 | Fixup::Size size = use32bit ? Fixup::kLiteralAddr4KiB : Fixup::kLiteralAddr1KiB; |
| 3681 | FixupId fixup_id = AddFixup(Fixup::LoadLiteralAddress(location, base_reg, size)); |
| 3682 | Emit16(static_cast<uint16_t>(table->GetLabel()->position_)); |
| 3683 | table->GetLabel()->LinkTo(fixup_id); |
| 3684 | if (use32bit) { |
| 3685 | Emit16(0); |
| 3686 | } |
| 3687 | DCHECK_EQ(location + GetFixup(fixup_id)->GetSizeInBytes(), buffer_.Size()); |
| 3688 | |
| 3689 | return table; |
| 3690 | } |
| 3691 | |
| 3692 | void Thumb2Assembler::EmitJumpTableDispatch(JumpTable* jump_table, Register displacement_reg) { |
| 3693 | CHECK(!IsForced32Bit()) << "Forced 32-bit dispatch not implemented yet"; |
| 3694 | // 32-bit ADD doesn't support PC as an input, so we need a two-instruction sequence: |
| 3695 | // SUB ip, ip, #0 |
| 3696 | // ADD pc, ip, reg |
| 3697 | // TODO: Implement. |
| 3698 | |
| 3699 | // The anchor's position needs to be fixed up before we can compute offsets - so make it a tracked |
| 3700 | // label. |
| 3701 | BindTrackedLabel(jump_table->GetAnchorLabel()); |
| 3702 | |
| 3703 | add(PC, PC, ShifterOperand(displacement_reg)); |
| 3704 | } |
| 3705 | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 3706 | } // namespace arm |
| 3707 | } // namespace art |