armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 1 | // Copyright 2015, ARM Limited |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are met: |
| 6 | // |
| 7 | // * Redistributions of source code must retain the above copyright notice, |
| 8 | // this list of conditions and the following disclaimer. |
| 9 | // * Redistributions in binary form must reproduce the above copyright notice, |
| 10 | // this list of conditions and the following disclaimer in the documentation |
| 11 | // and/or other materials provided with the distribution. |
| 12 | // * Neither the name of ARM Limited nor the names of its contributors may be |
| 13 | // used to endorse or promote products derived from this software without |
| 14 | // specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND |
| 17 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
| 20 | // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 21 | // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 22 | // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | |
armvixl | 684cd2a | 2015-10-23 13:38:33 +0100 | [diff] [blame] | 27 | #include <ctype.h> |
| 28 | |
Alexandre Rames | 39c32a6 | 2016-05-23 15:47:22 +0100 | [diff] [blame] | 29 | #include "a64/macro-assembler-a64.h" |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 30 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 31 | namespace vixl { |
| 32 | |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 33 | |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 34 | void Pool::Release() { |
| 35 | if (--monitor_ == 0) { |
| 36 | // Ensure the pool has not been blocked for too long. |
| 37 | VIXL_ASSERT(masm_->CursorOffset() < checkpoint_); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | |
| 42 | void Pool::SetNextCheckpoint(ptrdiff_t checkpoint) { |
| 43 | masm_->checkpoint_ = std::min(masm_->checkpoint_, checkpoint); |
| 44 | checkpoint_ = checkpoint; |
| 45 | } |
| 46 | |
| 47 | |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 48 | LiteralPool::LiteralPool(MacroAssembler* masm) |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 49 | : Pool(masm), |
| 50 | size_(0), |
| 51 | first_use_(-1), |
| 52 | recommended_checkpoint_(kNoCheckpointRequired) {} |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 53 | |
| 54 | |
| 55 | LiteralPool::~LiteralPool() { |
| 56 | VIXL_ASSERT(IsEmpty()); |
| 57 | VIXL_ASSERT(!IsBlocked()); |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 58 | for (std::vector<RawLiteral*>::iterator it = deleted_on_destruction_.begin(); |
| 59 | it != deleted_on_destruction_.end(); |
| 60 | it++) { |
| 61 | delete *it; |
| 62 | } |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | |
| 66 | void LiteralPool::Reset() { |
| 67 | std::vector<RawLiteral*>::iterator it, end; |
| 68 | for (it = entries_.begin(), end = entries_.end(); it != end; ++it) { |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 69 | RawLiteral* literal = *it; |
| 70 | if (literal->deletion_policy_ == RawLiteral::kDeletedOnPlacementByPool) { |
| 71 | delete literal; |
| 72 | } |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 73 | } |
| 74 | entries_.clear(); |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 75 | size_ = 0; |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 76 | first_use_ = -1; |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 77 | Pool::Reset(); |
| 78 | recommended_checkpoint_ = kNoCheckpointRequired; |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | |
| 82 | void LiteralPool::CheckEmitFor(size_t amount, EmitOption option) { |
| 83 | if (IsEmpty() || IsBlocked()) return; |
| 84 | |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 85 | ptrdiff_t distance = masm_->CursorOffset() + amount - first_use_; |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 86 | if (distance >= kRecommendedLiteralPoolRange) { |
| 87 | Emit(option); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | |
| 92 | void LiteralPool::Emit(EmitOption option) { |
| 93 | // There is an issue if we are asked to emit a blocked or empty pool. |
| 94 | VIXL_ASSERT(!IsBlocked()); |
| 95 | VIXL_ASSERT(!IsEmpty()); |
| 96 | |
| 97 | size_t pool_size = Size(); |
| 98 | size_t emit_size = pool_size; |
| 99 | if (option == kBranchRequired) emit_size += kInstructionSize; |
| 100 | Label end_of_pool; |
| 101 | |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 102 | VIXL_ASSERT(emit_size % kInstructionSize == 0); |
| 103 | InstructionAccurateScope guard(masm_, emit_size / kInstructionSize); |
| 104 | if (option == kBranchRequired) masm_->b(&end_of_pool); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 105 | |
| 106 | // Marker indicating the size of the literal pool in 32-bit words. |
| 107 | VIXL_ASSERT((pool_size % kWRegSizeInBytes) == 0); |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 108 | masm_->ldr(xzr, static_cast<int>(pool_size / kWRegSizeInBytes)); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 109 | |
| 110 | // Now populate the literal pool. |
| 111 | std::vector<RawLiteral*>::iterator it, end; |
| 112 | for (it = entries_.begin(), end = entries_.end(); it != end; ++it) { |
| 113 | VIXL_ASSERT((*it)->IsUsed()); |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 114 | masm_->place(*it); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 115 | } |
| 116 | |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 117 | if (option == kBranchRequired) masm_->bind(&end_of_pool); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 118 | |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 119 | Reset(); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 123 | void LiteralPool::AddEntry(RawLiteral* literal) { |
| 124 | // A literal must be registered immediately before its first use. Here we |
| 125 | // cannot control that it is its first use, but we check no code has been |
| 126 | // emitted since its last use. |
| 127 | VIXL_ASSERT(masm_->CursorOffset() == literal->last_use()); |
| 128 | |
| 129 | UpdateFirstUse(masm_->CursorOffset()); |
| 130 | VIXL_ASSERT(masm_->CursorOffset() >= first_use_); |
| 131 | entries_.push_back(literal); |
| 132 | size_ += literal->size(); |
| 133 | } |
| 134 | |
| 135 | |
| 136 | void LiteralPool::UpdateFirstUse(ptrdiff_t use_position) { |
| 137 | first_use_ = std::min(first_use_, use_position); |
| 138 | if (first_use_ == -1) { |
| 139 | first_use_ = use_position; |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 140 | SetNextRecommendedCheckpoint(NextRecommendedCheckpoint()); |
| 141 | SetNextCheckpoint(first_use_ + Instruction::kLoadLiteralRange); |
| 142 | } else { |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 143 | VIXL_ASSERT(use_position > first_use_); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 144 | } |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | |
| 148 | void VeneerPool::Reset() { |
| 149 | Pool::Reset(); |
| 150 | unresolved_branches_.Reset(); |
| 151 | } |
| 152 | |
| 153 | |
| 154 | void VeneerPool::Release() { |
| 155 | if (--monitor_ == 0) { |
| 156 | VIXL_ASSERT(IsEmpty() || |
| 157 | masm_->CursorOffset() < unresolved_branches_.FirstLimit()); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | |
| 162 | void VeneerPool::RegisterUnresolvedBranch(ptrdiff_t branch_pos, |
| 163 | Label* label, |
| 164 | ImmBranchType branch_type) { |
| 165 | VIXL_ASSERT(!label->IsBound()); |
| 166 | BranchInfo branch_info = BranchInfo(branch_pos, label, branch_type); |
| 167 | unresolved_branches_.insert(branch_info); |
| 168 | UpdateNextCheckPoint(); |
| 169 | // TODO: In debug mode register the label with the assembler to make sure it |
| 170 | // is bound with masm Bind and not asm bind. |
| 171 | } |
| 172 | |
| 173 | |
| 174 | void VeneerPool::DeleteUnresolvedBranchInfoForLabel(Label* label) { |
| 175 | if (IsEmpty()) { |
| 176 | VIXL_ASSERT(checkpoint_ == kNoCheckpointRequired); |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | if (label->IsLinked()) { |
| 181 | Label::LabelLinksIterator links_it(label); |
| 182 | for (; !links_it.Done(); links_it.Advance()) { |
| 183 | ptrdiff_t link_offset = *links_it.Current(); |
| 184 | Instruction* link = masm_->InstructionAt(link_offset); |
| 185 | |
| 186 | // ADR instructions are not handled. |
| 187 | if (BranchTypeUsesVeneers(link->BranchType())) { |
| 188 | BranchInfo branch_info(link_offset, label, link->BranchType()); |
| 189 | unresolved_branches_.erase(branch_info); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | UpdateNextCheckPoint(); |
| 195 | } |
| 196 | |
| 197 | |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 198 | bool VeneerPool::ShouldEmitVeneer(int64_t max_reachable_pc, size_t amount) { |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 199 | ptrdiff_t offset = |
| 200 | kPoolNonVeneerCodeSize + amount + MaxSize() + OtherPoolsMaxSize(); |
| 201 | return (masm_->CursorOffset() + offset) > max_reachable_pc; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | void VeneerPool::CheckEmitFor(size_t amount, EmitOption option) { |
| 206 | if (IsEmpty()) return; |
| 207 | |
| 208 | VIXL_ASSERT(masm_->CursorOffset() < unresolved_branches_.FirstLimit()); |
| 209 | |
| 210 | if (IsBlocked()) return; |
| 211 | |
| 212 | if (ShouldEmitVeneers(amount)) { |
| 213 | Emit(option, amount); |
| 214 | } else { |
| 215 | UpdateNextCheckPoint(); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | |
| 220 | void VeneerPool::Emit(EmitOption option, size_t amount) { |
| 221 | // There is an issue if we are asked to emit a blocked or empty pool. |
| 222 | VIXL_ASSERT(!IsBlocked()); |
| 223 | VIXL_ASSERT(!IsEmpty()); |
| 224 | |
| 225 | Label end; |
| 226 | if (option == kBranchRequired) { |
| 227 | InstructionAccurateScope scope(masm_, 1); |
| 228 | masm_->b(&end); |
| 229 | } |
| 230 | |
| 231 | // We want to avoid generating veneer pools too often, so generate veneers for |
| 232 | // branches that don't immediately require a veneer but will soon go out of |
| 233 | // range. |
| 234 | static const size_t kVeneerEmissionMargin = 1 * KBytes; |
| 235 | |
| 236 | for (BranchInfoSetIterator it(&unresolved_branches_); !it.Done();) { |
| 237 | BranchInfo* branch_info = it.Current(); |
| 238 | if (ShouldEmitVeneer(branch_info->max_reachable_pc_, |
| 239 | amount + kVeneerEmissionMargin)) { |
| 240 | InstructionAccurateScope scope(masm_, kVeneerCodeSize / kInstructionSize); |
| 241 | ptrdiff_t branch_pos = branch_info->pc_offset_; |
| 242 | Instruction* branch = masm_->InstructionAt(branch_pos); |
| 243 | Label* label = branch_info->label_; |
| 244 | |
| 245 | // Patch the branch to point to the current position, and emit a branch |
| 246 | // to the label. |
| 247 | Instruction* veneer = masm_->GetCursorAddress<Instruction*>(); |
| 248 | branch->SetImmPCOffsetTarget(veneer); |
| 249 | masm_->b(label); |
| 250 | |
| 251 | // Update the label. The branch patched does not point to it any longer. |
| 252 | label->DeleteLink(branch_pos); |
| 253 | |
| 254 | it.DeleteCurrentAndAdvance(); |
| 255 | } else { |
| 256 | it.AdvanceToNextType(); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | UpdateNextCheckPoint(); |
| 261 | |
| 262 | masm_->bind(&end); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 266 | EmissionCheckScope::EmissionCheckScope(MacroAssembler* masm, size_t size) |
| 267 | : masm_(masm) { |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 268 | if (masm_ == NULL) { |
| 269 | // Nothing to do. |
| 270 | // We may reach this point in a context of conditional code generation. See |
| 271 | // `MacroAssembler::MoveImmediateHelper()` for an example. |
| 272 | return; |
| 273 | } |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 274 | masm_->EnsureEmitFor(size); |
| 275 | masm_->BlockPools(); |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 276 | #ifdef VIXL_DEBUG |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 277 | masm_->Bind(&start_); |
| 278 | size_ = size; |
| 279 | masm_->AcquireBuffer(); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 280 | #endif |
| 281 | } |
| 282 | |
| 283 | |
| 284 | EmissionCheckScope::~EmissionCheckScope() { |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 285 | if (masm_ == NULL) { |
| 286 | // Nothing to do. |
| 287 | return; |
| 288 | } |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 289 | #ifdef VIXL_DEBUG |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 290 | masm_->ReleaseBuffer(); |
| 291 | VIXL_ASSERT(masm_->SizeOfCodeGeneratedSince(&start_) <= size_); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 292 | #endif |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 293 | masm_->ReleasePools(); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | |
| 297 | MacroAssembler::MacroAssembler(size_t capacity, |
| 298 | PositionIndependentCodeOption pic) |
| 299 | : Assembler(capacity, pic), |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 300 | #ifdef VIXL_DEBUG |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 301 | allow_macro_instructions_(true), |
| 302 | #endif |
armvixl | 684cd2a | 2015-10-23 13:38:33 +0100 | [diff] [blame] | 303 | allow_simulator_instructions_(VIXL_GENERATE_SIMULATOR_INSTRUCTIONS_VALUE), |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 304 | sp_(sp), |
| 305 | tmp_list_(ip0, ip1), |
| 306 | fptmp_list_(d31), |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 307 | literal_pool_(this), |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 308 | veneer_pool_(this), |
| 309 | recommended_checkpoint_(Pool::kNoCheckpointRequired) { |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 310 | checkpoint_ = NextCheckPoint(); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 314 | MacroAssembler::MacroAssembler(byte* buffer, |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 315 | size_t capacity, |
| 316 | PositionIndependentCodeOption pic) |
| 317 | : Assembler(buffer, capacity, pic), |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 318 | #ifdef VIXL_DEBUG |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 319 | allow_macro_instructions_(true), |
| 320 | #endif |
armvixl | 684cd2a | 2015-10-23 13:38:33 +0100 | [diff] [blame] | 321 | allow_simulator_instructions_(VIXL_GENERATE_SIMULATOR_INSTRUCTIONS_VALUE), |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 322 | sp_(sp), |
| 323 | tmp_list_(ip0, ip1), |
| 324 | fptmp_list_(d31), |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 325 | literal_pool_(this), |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 326 | veneer_pool_(this), |
| 327 | recommended_checkpoint_(Pool::kNoCheckpointRequired) { |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 328 | checkpoint_ = NextCheckPoint(); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 332 | MacroAssembler::~MacroAssembler() {} |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 333 | |
| 334 | |
| 335 | void MacroAssembler::Reset() { |
| 336 | Assembler::Reset(); |
| 337 | |
| 338 | VIXL_ASSERT(!literal_pool_.IsBlocked()); |
| 339 | literal_pool_.Reset(); |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 340 | veneer_pool_.Reset(); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 341 | |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 342 | checkpoint_ = NextCheckPoint(); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | |
| 346 | void MacroAssembler::FinalizeCode() { |
| 347 | if (!literal_pool_.IsEmpty()) literal_pool_.Emit(); |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 348 | VIXL_ASSERT(veneer_pool_.IsEmpty()); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 349 | |
| 350 | Assembler::FinalizeCode(); |
| 351 | } |
| 352 | |
| 353 | |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 354 | void MacroAssembler::CheckEmitFor(size_t amount) { |
| 355 | ptrdiff_t offset = amount; |
| 356 | |
| 357 | literal_pool_.CheckEmitFor(amount); |
| 358 | veneer_pool_.CheckEmitFor(amount); |
| 359 | // Ensure there's enough space for the emit, keep in mind the cursor will |
| 360 | // have moved if a pool was emitted. |
| 361 | if ((CursorOffset() + offset) > BufferEndOffset()) { |
| 362 | EnsureSpaceFor(amount); |
| 363 | } |
| 364 | |
| 365 | checkpoint_ = NextCheckPoint(); |
| 366 | } |
| 367 | |
| 368 | |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 369 | int MacroAssembler::MoveImmediateHelper(MacroAssembler* masm, |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 370 | const Register& rd, |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 371 | uint64_t imm) { |
| 372 | bool emit_code = (masm != NULL); |
| 373 | VIXL_ASSERT(is_uint32(imm) || is_int32(imm) || rd.Is64Bits()); |
| 374 | // The worst case for size is mov 64-bit immediate to sp: |
| 375 | // * up to 4 instructions to materialise the constant |
| 376 | // * 1 instruction to move to sp |
| 377 | MacroEmissionCheckScope guard(masm); |
| 378 | |
| 379 | // Immediates on Aarch64 can be produced using an initial value, and zero to |
| 380 | // three move keep operations. |
| 381 | // |
| 382 | // Initial values can be generated with: |
| 383 | // 1. 64-bit move zero (movz). |
| 384 | // 2. 32-bit move inverted (movn). |
| 385 | // 3. 64-bit move inverted. |
| 386 | // 4. 32-bit orr immediate. |
| 387 | // 5. 64-bit orr immediate. |
| 388 | // Move-keep may then be used to modify each of the 16-bit half words. |
| 389 | // |
| 390 | // The code below supports all five initial value generators, and |
| 391 | // applying move-keep operations to move-zero and move-inverted initial |
| 392 | // values. |
| 393 | |
| 394 | // Try to move the immediate in one instruction, and if that fails, switch to |
| 395 | // using multiple instructions. |
| 396 | if (OneInstrMoveImmediateHelper(masm, rd, imm)) { |
| 397 | return 1; |
| 398 | } else { |
| 399 | int instruction_count = 0; |
| 400 | unsigned reg_size = rd.size(); |
| 401 | |
| 402 | // Generic immediate case. Imm will be represented by |
| 403 | // [imm3, imm2, imm1, imm0], where each imm is 16 bits. |
| 404 | // A move-zero or move-inverted is generated for the first non-zero or |
| 405 | // non-0xffff immX, and a move-keep for subsequent non-zero immX. |
| 406 | |
| 407 | uint64_t ignored_halfword = 0; |
| 408 | bool invert_move = false; |
| 409 | // If the number of 0xffff halfwords is greater than the number of 0x0000 |
| 410 | // halfwords, it's more efficient to use move-inverted. |
| 411 | if (CountClearHalfWords(~imm, reg_size) > |
| 412 | CountClearHalfWords(imm, reg_size)) { |
| 413 | ignored_halfword = 0xffff; |
| 414 | invert_move = true; |
| 415 | } |
| 416 | |
| 417 | // Mov instructions can't move values into the stack pointer, so set up a |
| 418 | // temporary register, if needed. |
| 419 | UseScratchRegisterScope temps; |
| 420 | Register temp; |
| 421 | if (emit_code) { |
| 422 | temps.Open(masm); |
| 423 | temp = rd.IsSP() ? temps.AcquireSameSizeAs(rd) : rd; |
| 424 | } |
| 425 | |
| 426 | // Iterate through the halfwords. Use movn/movz for the first non-ignored |
| 427 | // halfword, and movk for subsequent halfwords. |
| 428 | VIXL_ASSERT((reg_size % 16) == 0); |
| 429 | bool first_mov_done = false; |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 430 | for (unsigned i = 0; i < (reg_size / 16); i++) { |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 431 | uint64_t imm16 = (imm >> (16 * i)) & 0xffff; |
| 432 | if (imm16 != ignored_halfword) { |
| 433 | if (!first_mov_done) { |
| 434 | if (invert_move) { |
| 435 | if (emit_code) masm->movn(temp, ~imm16 & 0xffff, 16 * i); |
| 436 | instruction_count++; |
| 437 | } else { |
| 438 | if (emit_code) masm->movz(temp, imm16, 16 * i); |
| 439 | instruction_count++; |
| 440 | } |
| 441 | first_mov_done = true; |
| 442 | } else { |
| 443 | // Construct a wider constant. |
| 444 | if (emit_code) masm->movk(temp, imm16, 16 * i); |
| 445 | instruction_count++; |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | VIXL_ASSERT(first_mov_done); |
| 451 | |
| 452 | // Move the temporary if the original destination register was the stack |
| 453 | // pointer. |
| 454 | if (rd.IsSP()) { |
| 455 | if (emit_code) masm->mov(rd, temp); |
| 456 | instruction_count++; |
| 457 | } |
| 458 | return instruction_count; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | |
| 463 | bool MacroAssembler::OneInstrMoveImmediateHelper(MacroAssembler* masm, |
| 464 | const Register& dst, |
| 465 | int64_t imm) { |
| 466 | bool emit_code = masm != NULL; |
| 467 | unsigned n, imm_s, imm_r; |
| 468 | int reg_size = dst.size(); |
| 469 | |
| 470 | if (IsImmMovz(imm, reg_size) && !dst.IsSP()) { |
| 471 | // Immediate can be represented in a move zero instruction. Movz can't write |
| 472 | // to the stack pointer. |
| 473 | if (emit_code) { |
| 474 | masm->movz(dst, imm); |
| 475 | } |
| 476 | return true; |
| 477 | } else if (IsImmMovn(imm, reg_size) && !dst.IsSP()) { |
| 478 | // Immediate can be represented in a move negative instruction. Movn can't |
| 479 | // write to the stack pointer. |
| 480 | if (emit_code) { |
| 481 | masm->movn(dst, dst.Is64Bits() ? ~imm : (~imm & kWRegMask)); |
| 482 | } |
| 483 | return true; |
| 484 | } else if (IsImmLogical(imm, reg_size, &n, &imm_s, &imm_r)) { |
| 485 | // Immediate can be represented in a logical orr instruction. |
| 486 | VIXL_ASSERT(!dst.IsZero()); |
| 487 | if (emit_code) { |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 488 | masm->LogicalImmediate(dst, |
| 489 | AppropriateZeroRegFor(dst), |
| 490 | n, |
| 491 | imm_s, |
| 492 | imm_r, |
| 493 | ORR); |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 494 | } |
| 495 | return true; |
| 496 | } |
| 497 | return false; |
| 498 | } |
| 499 | |
| 500 | |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 501 | void MacroAssembler::B(Label* label, BranchType type, Register reg, int bit) { |
| 502 | VIXL_ASSERT((reg.Is(NoReg) || (type >= kBranchTypeFirstUsingReg)) && |
| 503 | ((bit == -1) || (type >= kBranchTypeFirstUsingBit))); |
| 504 | if (kBranchTypeFirstCondition <= type && type <= kBranchTypeLastCondition) { |
| 505 | B(static_cast<Condition>(type), label); |
| 506 | } else { |
| 507 | switch (type) { |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 508 | case always: |
| 509 | B(label); |
| 510 | break; |
| 511 | case never: |
| 512 | break; |
| 513 | case reg_zero: |
| 514 | Cbz(reg, label); |
| 515 | break; |
| 516 | case reg_not_zero: |
| 517 | Cbnz(reg, label); |
| 518 | break; |
| 519 | case reg_bit_clear: |
| 520 | Tbz(reg, bit, label); |
| 521 | break; |
| 522 | case reg_bit_set: |
| 523 | Tbnz(reg, bit, label); |
| 524 | break; |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 525 | default: |
| 526 | VIXL_UNREACHABLE(); |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 531 | |
| 532 | void MacroAssembler::B(Label* label) { |
| 533 | SingleEmissionCheckScope guard(this); |
| 534 | b(label); |
| 535 | } |
| 536 | |
| 537 | |
| 538 | void MacroAssembler::B(Label* label, Condition cond) { |
| 539 | VIXL_ASSERT(allow_macro_instructions_); |
| 540 | VIXL_ASSERT((cond != al) && (cond != nv)); |
| 541 | EmissionCheckScope guard(this, 2 * kInstructionSize); |
| 542 | |
| 543 | if (label->IsBound() && LabelIsOutOfRange(label, CondBranchType)) { |
| 544 | Label done; |
| 545 | b(&done, InvertCondition(cond)); |
| 546 | b(label); |
| 547 | bind(&done); |
| 548 | } else { |
| 549 | if (!label->IsBound()) { |
| 550 | veneer_pool_.RegisterUnresolvedBranch(CursorOffset(), |
| 551 | label, |
| 552 | CondBranchType); |
| 553 | } |
| 554 | b(label, cond); |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | |
| 559 | void MacroAssembler::Cbnz(const Register& rt, Label* label) { |
| 560 | VIXL_ASSERT(allow_macro_instructions_); |
| 561 | VIXL_ASSERT(!rt.IsZero()); |
| 562 | EmissionCheckScope guard(this, 2 * kInstructionSize); |
| 563 | |
| 564 | if (label->IsBound() && LabelIsOutOfRange(label, CondBranchType)) { |
| 565 | Label done; |
| 566 | cbz(rt, &done); |
| 567 | b(label); |
| 568 | bind(&done); |
| 569 | } else { |
| 570 | if (!label->IsBound()) { |
| 571 | veneer_pool_.RegisterUnresolvedBranch(CursorOffset(), |
| 572 | label, |
| 573 | CompareBranchType); |
| 574 | } |
| 575 | cbnz(rt, label); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | |
| 580 | void MacroAssembler::Cbz(const Register& rt, Label* label) { |
| 581 | VIXL_ASSERT(allow_macro_instructions_); |
| 582 | VIXL_ASSERT(!rt.IsZero()); |
| 583 | EmissionCheckScope guard(this, 2 * kInstructionSize); |
| 584 | |
| 585 | if (label->IsBound() && LabelIsOutOfRange(label, CondBranchType)) { |
| 586 | Label done; |
| 587 | cbnz(rt, &done); |
| 588 | b(label); |
| 589 | bind(&done); |
| 590 | } else { |
| 591 | if (!label->IsBound()) { |
| 592 | veneer_pool_.RegisterUnresolvedBranch(CursorOffset(), |
| 593 | label, |
| 594 | CompareBranchType); |
| 595 | } |
| 596 | cbz(rt, label); |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | |
| 601 | void MacroAssembler::Tbnz(const Register& rt, unsigned bit_pos, Label* label) { |
| 602 | VIXL_ASSERT(allow_macro_instructions_); |
| 603 | VIXL_ASSERT(!rt.IsZero()); |
| 604 | EmissionCheckScope guard(this, 2 * kInstructionSize); |
| 605 | |
| 606 | if (label->IsBound() && LabelIsOutOfRange(label, TestBranchType)) { |
| 607 | Label done; |
| 608 | tbz(rt, bit_pos, &done); |
| 609 | b(label); |
| 610 | bind(&done); |
| 611 | } else { |
| 612 | if (!label->IsBound()) { |
| 613 | veneer_pool_.RegisterUnresolvedBranch(CursorOffset(), |
| 614 | label, |
| 615 | TestBranchType); |
| 616 | } |
| 617 | tbnz(rt, bit_pos, label); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | |
| 622 | void MacroAssembler::Tbz(const Register& rt, unsigned bit_pos, Label* label) { |
| 623 | VIXL_ASSERT(allow_macro_instructions_); |
| 624 | VIXL_ASSERT(!rt.IsZero()); |
| 625 | EmissionCheckScope guard(this, 2 * kInstructionSize); |
| 626 | |
| 627 | if (label->IsBound() && LabelIsOutOfRange(label, TestBranchType)) { |
| 628 | Label done; |
| 629 | tbnz(rt, bit_pos, &done); |
| 630 | b(label); |
| 631 | bind(&done); |
| 632 | } else { |
| 633 | if (!label->IsBound()) { |
| 634 | veneer_pool_.RegisterUnresolvedBranch(CursorOffset(), |
| 635 | label, |
| 636 | TestBranchType); |
| 637 | } |
| 638 | tbz(rt, bit_pos, label); |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | |
| 643 | void MacroAssembler::Bind(Label* label) { |
| 644 | VIXL_ASSERT(allow_macro_instructions_); |
| 645 | veneer_pool_.DeleteUnresolvedBranchInfoForLabel(label); |
| 646 | bind(label); |
| 647 | } |
| 648 | |
| 649 | |
| 650 | // Bind a label to a specified offset from the start of the buffer. |
| 651 | void MacroAssembler::BindToOffset(Label* label, ptrdiff_t offset) { |
| 652 | VIXL_ASSERT(allow_macro_instructions_); |
| 653 | veneer_pool_.DeleteUnresolvedBranchInfoForLabel(label); |
| 654 | Assembler::BindToOffset(label, offset); |
| 655 | } |
| 656 | |
| 657 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 658 | void MacroAssembler::And(const Register& rd, |
| 659 | const Register& rn, |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 660 | const Operand& operand) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 661 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 662 | LogicalMacro(rd, rn, operand, AND); |
| 663 | } |
| 664 | |
| 665 | |
| 666 | void MacroAssembler::Ands(const Register& rd, |
| 667 | const Register& rn, |
| 668 | const Operand& operand) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 669 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 670 | LogicalMacro(rd, rn, operand, ANDS); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 674 | void MacroAssembler::Tst(const Register& rn, const Operand& operand) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 675 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 676 | Ands(AppropriateZeroRegFor(rn), rn, operand); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | |
| 680 | void MacroAssembler::Bic(const Register& rd, |
| 681 | const Register& rn, |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 682 | const Operand& operand) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 683 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 684 | LogicalMacro(rd, rn, operand, BIC); |
| 685 | } |
| 686 | |
| 687 | |
| 688 | void MacroAssembler::Bics(const Register& rd, |
| 689 | const Register& rn, |
| 690 | const Operand& operand) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 691 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 692 | LogicalMacro(rd, rn, operand, BICS); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | |
| 696 | void MacroAssembler::Orr(const Register& rd, |
| 697 | const Register& rn, |
| 698 | const Operand& operand) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 699 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 700 | LogicalMacro(rd, rn, operand, ORR); |
| 701 | } |
| 702 | |
| 703 | |
| 704 | void MacroAssembler::Orn(const Register& rd, |
| 705 | const Register& rn, |
| 706 | const Operand& operand) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 707 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 708 | LogicalMacro(rd, rn, operand, ORN); |
| 709 | } |
| 710 | |
| 711 | |
| 712 | void MacroAssembler::Eor(const Register& rd, |
| 713 | const Register& rn, |
| 714 | const Operand& operand) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 715 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 716 | LogicalMacro(rd, rn, operand, EOR); |
| 717 | } |
| 718 | |
| 719 | |
| 720 | void MacroAssembler::Eon(const Register& rd, |
| 721 | const Register& rn, |
| 722 | const Operand& operand) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 723 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 724 | LogicalMacro(rd, rn, operand, EON); |
| 725 | } |
| 726 | |
| 727 | |
| 728 | void MacroAssembler::LogicalMacro(const Register& rd, |
| 729 | const Register& rn, |
| 730 | const Operand& operand, |
| 731 | LogicalOp op) { |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 732 | // The worst case for size is logical immediate to sp: |
| 733 | // * up to 4 instructions to materialise the constant |
| 734 | // * 1 instruction to do the operation |
| 735 | // * 1 instruction to move to sp |
| 736 | MacroEmissionCheckScope guard(this); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 737 | UseScratchRegisterScope temps(this); |
| 738 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 739 | if (operand.IsImmediate()) { |
| 740 | int64_t immediate = operand.immediate(); |
| 741 | unsigned reg_size = rd.size(); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 742 | |
| 743 | // If the operation is NOT, invert the operation and immediate. |
| 744 | if ((op & NOT) == NOT) { |
| 745 | op = static_cast<LogicalOp>(op & ~NOT); |
| 746 | immediate = ~immediate; |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 747 | } |
| 748 | |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 749 | // Ignore the top 32 bits of an immediate if we're moving to a W register. |
| 750 | if (rd.Is32Bits()) { |
| 751 | // Check that the top 32 bits are consistent. |
| 752 | VIXL_ASSERT(((immediate >> kWRegSize) == 0) || |
| 753 | ((immediate >> kWRegSize) == -1)); |
| 754 | immediate &= kWRegMask; |
| 755 | } |
| 756 | |
| 757 | VIXL_ASSERT(rd.Is64Bits() || is_uint32(immediate)); |
| 758 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 759 | // Special cases for all set or all clear immediates. |
| 760 | if (immediate == 0) { |
| 761 | switch (op) { |
| 762 | case AND: |
| 763 | Mov(rd, 0); |
| 764 | return; |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 765 | case ORR: |
| 766 | VIXL_FALLTHROUGH(); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 767 | case EOR: |
| 768 | Mov(rd, rn); |
| 769 | return; |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 770 | case ANDS: |
| 771 | VIXL_FALLTHROUGH(); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 772 | case BICS: |
| 773 | break; |
| 774 | default: |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 775 | VIXL_UNREACHABLE(); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 776 | } |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 777 | } else if ((rd.Is64Bits() && (immediate == -1)) || |
| 778 | (rd.Is32Bits() && (immediate == 0xffffffff))) { |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 779 | switch (op) { |
| 780 | case AND: |
| 781 | Mov(rd, rn); |
| 782 | return; |
| 783 | case ORR: |
| 784 | Mov(rd, immediate); |
| 785 | return; |
| 786 | case EOR: |
| 787 | Mvn(rd, rn); |
| 788 | return; |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 789 | case ANDS: |
| 790 | VIXL_FALLTHROUGH(); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 791 | case BICS: |
| 792 | break; |
| 793 | default: |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 794 | VIXL_UNREACHABLE(); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 795 | } |
| 796 | } |
| 797 | |
| 798 | unsigned n, imm_s, imm_r; |
| 799 | if (IsImmLogical(immediate, reg_size, &n, &imm_s, &imm_r)) { |
| 800 | // Immediate can be encoded in the instruction. |
| 801 | LogicalImmediate(rd, rn, n, imm_s, imm_r, op); |
| 802 | } else { |
| 803 | // Immediate can't be encoded: synthesize using move immediate. |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 804 | Register temp = temps.AcquireSameSizeAs(rn); |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 805 | Operand imm_operand = MoveImmediateForShiftedOp(temp, immediate); |
| 806 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 807 | if (rd.Is(sp)) { |
| 808 | // If rd is the stack pointer we cannot use it as the destination |
| 809 | // register so we use the temp register as an intermediate again. |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 810 | Logical(temp, rn, imm_operand, op); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 811 | Mov(sp, temp); |
| 812 | } else { |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 813 | Logical(rd, rn, imm_operand, op); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 814 | } |
| 815 | } |
| 816 | } else if (operand.IsExtendedRegister()) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 817 | VIXL_ASSERT(operand.reg().size() <= rd.size()); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 818 | // Add/sub extended supports shift <= 4. We want to support exactly the |
| 819 | // same modes here. |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 820 | VIXL_ASSERT(operand.shift_amount() <= 4); |
| 821 | VIXL_ASSERT(operand.reg().Is64Bits() || |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 822 | ((operand.extend() != UXTX) && (operand.extend() != SXTX))); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 823 | |
| 824 | temps.Exclude(operand.reg()); |
| 825 | Register temp = temps.AcquireSameSizeAs(rn); |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 826 | EmitExtendShift(temp, |
| 827 | operand.reg(), |
| 828 | operand.extend(), |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 829 | operand.shift_amount()); |
| 830 | Logical(rd, rn, Operand(temp), op); |
| 831 | } else { |
| 832 | // The operand can be encoded in the instruction. |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 833 | VIXL_ASSERT(operand.IsShiftedRegister()); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 834 | Logical(rd, rn, operand, op); |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 839 | void MacroAssembler::Mov(const Register& rd, |
| 840 | const Operand& operand, |
| 841 | DiscardMoveMode discard_mode) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 842 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 843 | // The worst case for size is mov immediate with up to 4 instructions. |
| 844 | MacroEmissionCheckScope guard(this); |
| 845 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 846 | if (operand.IsImmediate()) { |
| 847 | // Call the macro assembler for generic immediates. |
| 848 | Mov(rd, operand.immediate()); |
| 849 | } else if (operand.IsShiftedRegister() && (operand.shift_amount() != 0)) { |
| 850 | // Emit a shift instruction if moving a shifted register. This operation |
| 851 | // could also be achieved using an orr instruction (like orn used by Mvn), |
| 852 | // but using a shift instruction makes the disassembly clearer. |
| 853 | EmitShift(rd, operand.reg(), operand.shift(), operand.shift_amount()); |
| 854 | } else if (operand.IsExtendedRegister()) { |
| 855 | // Emit an extend instruction if moving an extended register. This handles |
| 856 | // extend with post-shift operations, too. |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 857 | EmitExtendShift(rd, |
| 858 | operand.reg(), |
| 859 | operand.extend(), |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 860 | operand.shift_amount()); |
| 861 | } else { |
| 862 | // Otherwise, emit a register move only if the registers are distinct, or |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 863 | // if they are not X registers. |
| 864 | // |
| 865 | // Note that mov(w0, w0) is not a no-op because it clears the top word of |
| 866 | // x0. A flag is provided (kDiscardForSameWReg) if a move between the same W |
| 867 | // registers is not required to clear the top word of the X register. In |
| 868 | // this case, the instruction is discarded. |
| 869 | // |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 870 | // If the sp is an operand, add #0 is emitted, otherwise, orr #0. |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 871 | if (!rd.Is(operand.reg()) || |
| 872 | (rd.Is32Bits() && (discard_mode == kDontDiscardForSameWReg))) { |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 873 | mov(rd, operand.reg()); |
| 874 | } |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 879 | void MacroAssembler::Movi16bitHelper(const VRegister& vd, uint64_t imm) { |
| 880 | VIXL_ASSERT(is_uint16(imm)); |
| 881 | int byte1 = (imm & 0xff); |
| 882 | int byte2 = ((imm >> 8) & 0xff); |
| 883 | if (byte1 == byte2) { |
| 884 | movi(vd.Is64Bits() ? vd.V8B() : vd.V16B(), byte1); |
| 885 | } else if (byte1 == 0) { |
| 886 | movi(vd, byte2, LSL, 8); |
| 887 | } else if (byte2 == 0) { |
| 888 | movi(vd, byte1); |
| 889 | } else if (byte1 == 0xff) { |
| 890 | mvni(vd, ~byte2 & 0xff, LSL, 8); |
| 891 | } else if (byte2 == 0xff) { |
| 892 | mvni(vd, ~byte1 & 0xff); |
| 893 | } else { |
| 894 | UseScratchRegisterScope temps(this); |
| 895 | Register temp = temps.AcquireW(); |
| 896 | movz(temp, imm); |
| 897 | dup(vd, temp); |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | |
| 902 | void MacroAssembler::Movi32bitHelper(const VRegister& vd, uint64_t imm) { |
| 903 | VIXL_ASSERT(is_uint32(imm)); |
| 904 | |
| 905 | uint8_t bytes[sizeof(imm)]; |
| 906 | memcpy(bytes, &imm, sizeof(imm)); |
| 907 | |
| 908 | // All bytes are either 0x00 or 0xff. |
| 909 | { |
| 910 | bool all0orff = true; |
| 911 | for (int i = 0; i < 4; ++i) { |
| 912 | if ((bytes[i] != 0) && (bytes[i] != 0xff)) { |
| 913 | all0orff = false; |
| 914 | break; |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | if (all0orff == true) { |
| 919 | movi(vd.Is64Bits() ? vd.V1D() : vd.V2D(), ((imm << 32) | imm)); |
| 920 | return; |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | // Of the 4 bytes, only one byte is non-zero. |
| 925 | for (int i = 0; i < 4; i++) { |
| 926 | if ((imm & (0xff << (i * 8))) == imm) { |
| 927 | movi(vd, bytes[i], LSL, i * 8); |
| 928 | return; |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | // Of the 4 bytes, only one byte is not 0xff. |
| 933 | for (int i = 0; i < 4; i++) { |
| 934 | uint32_t mask = ~(0xff << (i * 8)); |
| 935 | if ((imm & mask) == mask) { |
| 936 | mvni(vd, ~bytes[i] & 0xff, LSL, i * 8); |
| 937 | return; |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | // Immediate is of the form 0x00MMFFFF. |
| 942 | if ((imm & 0xff00ffff) == 0x0000ffff) { |
| 943 | movi(vd, bytes[2], MSL, 16); |
| 944 | return; |
| 945 | } |
| 946 | |
| 947 | // Immediate is of the form 0x0000MMFF. |
| 948 | if ((imm & 0xffff00ff) == 0x000000ff) { |
| 949 | movi(vd, bytes[1], MSL, 8); |
| 950 | return; |
| 951 | } |
| 952 | |
| 953 | // Immediate is of the form 0xFFMM0000. |
| 954 | if ((imm & 0xff00ffff) == 0xff000000) { |
| 955 | mvni(vd, ~bytes[2] & 0xff, MSL, 16); |
| 956 | return; |
| 957 | } |
| 958 | // Immediate is of the form 0xFFFFMM00. |
| 959 | if ((imm & 0xffff00ff) == 0xffff0000) { |
| 960 | mvni(vd, ~bytes[1] & 0xff, MSL, 8); |
| 961 | return; |
| 962 | } |
| 963 | |
| 964 | // Top and bottom 16-bits are equal. |
| 965 | if (((imm >> 16) & 0xffff) == (imm & 0xffff)) { |
| 966 | Movi16bitHelper(vd.Is64Bits() ? vd.V4H() : vd.V8H(), imm & 0xffff); |
| 967 | return; |
| 968 | } |
| 969 | |
| 970 | // Default case. |
| 971 | { |
| 972 | UseScratchRegisterScope temps(this); |
| 973 | Register temp = temps.AcquireW(); |
| 974 | Mov(temp, imm); |
| 975 | dup(vd, temp); |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | |
| 980 | void MacroAssembler::Movi64bitHelper(const VRegister& vd, uint64_t imm) { |
| 981 | // All bytes are either 0x00 or 0xff. |
| 982 | { |
| 983 | bool all0orff = true; |
| 984 | for (int i = 0; i < 8; ++i) { |
| 985 | int byteval = (imm >> (i * 8)) & 0xff; |
| 986 | if (byteval != 0 && byteval != 0xff) { |
| 987 | all0orff = false; |
| 988 | break; |
| 989 | } |
| 990 | } |
| 991 | if (all0orff == true) { |
| 992 | movi(vd, imm); |
| 993 | return; |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | // Top and bottom 32-bits are equal. |
| 998 | if (((imm >> 32) & 0xffffffff) == (imm & 0xffffffff)) { |
| 999 | Movi32bitHelper(vd.Is64Bits() ? vd.V2S() : vd.V4S(), imm & 0xffffffff); |
| 1000 | return; |
| 1001 | } |
| 1002 | |
| 1003 | // Default case. |
| 1004 | { |
| 1005 | UseScratchRegisterScope temps(this); |
| 1006 | Register temp = temps.AcquireX(); |
| 1007 | Mov(temp, imm); |
| 1008 | if (vd.Is1D()) { |
| 1009 | mov(vd.D(), 0, temp); |
| 1010 | } else { |
| 1011 | dup(vd.V2D(), temp); |
| 1012 | } |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | |
| 1017 | void MacroAssembler::Movi(const VRegister& vd, |
| 1018 | uint64_t imm, |
| 1019 | Shift shift, |
| 1020 | int shift_amount) { |
| 1021 | VIXL_ASSERT(allow_macro_instructions_); |
| 1022 | MacroEmissionCheckScope guard(this); |
| 1023 | if (shift_amount != 0 || shift != LSL) { |
| 1024 | movi(vd, imm, shift, shift_amount); |
| 1025 | } else if (vd.Is8B() || vd.Is16B()) { |
| 1026 | // 8-bit immediate. |
| 1027 | VIXL_ASSERT(is_uint8(imm)); |
| 1028 | movi(vd, imm); |
| 1029 | } else if (vd.Is4H() || vd.Is8H()) { |
| 1030 | // 16-bit immediate. |
| 1031 | Movi16bitHelper(vd, imm); |
| 1032 | } else if (vd.Is2S() || vd.Is4S()) { |
| 1033 | // 32-bit immediate. |
| 1034 | Movi32bitHelper(vd, imm); |
| 1035 | } else { |
| 1036 | // 64-bit immediate. |
| 1037 | Movi64bitHelper(vd, imm); |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 1042 | void MacroAssembler::Movi(const VRegister& vd, uint64_t hi, uint64_t lo) { |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 1043 | // TODO: Move 128-bit values in a more efficient way. |
| 1044 | VIXL_ASSERT(vd.Is128Bits()); |
| 1045 | UseScratchRegisterScope temps(this); |
| 1046 | Movi(vd.V2D(), lo); |
| 1047 | Register temp = temps.AcquireX(); |
| 1048 | Mov(temp, hi); |
| 1049 | Ins(vd.V2D(), 1, temp); |
| 1050 | } |
| 1051 | |
| 1052 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1053 | void MacroAssembler::Mvn(const Register& rd, const Operand& operand) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1054 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 1055 | // The worst case for size is mvn immediate with up to 4 instructions. |
| 1056 | MacroEmissionCheckScope guard(this); |
| 1057 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1058 | if (operand.IsImmediate()) { |
| 1059 | // Call the macro assembler for generic immediates. |
| 1060 | Mvn(rd, operand.immediate()); |
| 1061 | } else if (operand.IsExtendedRegister()) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1062 | UseScratchRegisterScope temps(this); |
| 1063 | temps.Exclude(operand.reg()); |
| 1064 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1065 | // Emit two instructions for the extend case. This differs from Mov, as |
| 1066 | // the extend and invert can't be achieved in one instruction. |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1067 | Register temp = temps.AcquireSameSizeAs(rd); |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 1068 | EmitExtendShift(temp, |
| 1069 | operand.reg(), |
| 1070 | operand.extend(), |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1071 | operand.shift_amount()); |
| 1072 | mvn(rd, Operand(temp)); |
| 1073 | } else { |
| 1074 | // Otherwise, register and shifted register cases can be handled by the |
| 1075 | // assembler directly, using orn. |
| 1076 | mvn(rd, operand); |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | |
| 1081 | void MacroAssembler::Mov(const Register& rd, uint64_t imm) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1082 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 1083 | MoveImmediateHelper(this, rd, imm); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | |
| 1087 | void MacroAssembler::Ccmp(const Register& rn, |
| 1088 | const Operand& operand, |
| 1089 | StatusFlags nzcv, |
| 1090 | Condition cond) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1091 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 1092 | if (operand.IsImmediate() && (operand.immediate() < 0)) { |
| 1093 | ConditionalCompareMacro(rn, -operand.immediate(), nzcv, cond, CCMN); |
| 1094 | } else { |
| 1095 | ConditionalCompareMacro(rn, operand, nzcv, cond, CCMP); |
| 1096 | } |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | |
| 1100 | void MacroAssembler::Ccmn(const Register& rn, |
| 1101 | const Operand& operand, |
| 1102 | StatusFlags nzcv, |
| 1103 | Condition cond) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1104 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 1105 | if (operand.IsImmediate() && (operand.immediate() < 0)) { |
| 1106 | ConditionalCompareMacro(rn, -operand.immediate(), nzcv, cond, CCMP); |
| 1107 | } else { |
| 1108 | ConditionalCompareMacro(rn, operand, nzcv, cond, CCMN); |
| 1109 | } |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | |
| 1113 | void MacroAssembler::ConditionalCompareMacro(const Register& rn, |
| 1114 | const Operand& operand, |
| 1115 | StatusFlags nzcv, |
| 1116 | Condition cond, |
| 1117 | ConditionalCompareOp op) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1118 | VIXL_ASSERT((cond != al) && (cond != nv)); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 1119 | // The worst case for size is ccmp immediate: |
| 1120 | // * up to 4 instructions to materialise the constant |
| 1121 | // * 1 instruction for ccmp |
| 1122 | MacroEmissionCheckScope guard(this); |
| 1123 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1124 | if ((operand.IsShiftedRegister() && (operand.shift_amount() == 0)) || |
| 1125 | (operand.IsImmediate() && IsImmConditionalCompare(operand.immediate()))) { |
| 1126 | // The immediate can be encoded in the instruction, or the operand is an |
| 1127 | // unshifted register: call the assembler. |
| 1128 | ConditionalCompare(rn, operand, nzcv, cond, op); |
| 1129 | } else { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1130 | UseScratchRegisterScope temps(this); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1131 | // The operand isn't directly supported by the instruction: perform the |
| 1132 | // operation on a temporary register. |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1133 | Register temp = temps.AcquireSameSizeAs(rn); |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 1134 | Mov(temp, operand); |
| 1135 | ConditionalCompare(rn, temp, nzcv, cond, op); |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 1140 | void MacroAssembler::CselHelper(MacroAssembler* masm, |
| 1141 | const Register& rd, |
| 1142 | Operand left, |
| 1143 | Operand right, |
| 1144 | Condition cond, |
| 1145 | bool* should_synthesise_left, |
| 1146 | bool* should_synthesise_right) { |
| 1147 | bool emit_code = (masm != NULL); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 1148 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 1149 | VIXL_ASSERT(!emit_code || masm->allow_macro_instructions_); |
| 1150 | VIXL_ASSERT((cond != al) && (cond != nv)); |
| 1151 | VIXL_ASSERT(!rd.IsZero() && !rd.IsSP()); |
| 1152 | VIXL_ASSERT(left.IsImmediate() || !left.reg().IsSP()); |
| 1153 | VIXL_ASSERT(right.IsImmediate() || !right.reg().IsSP()); |
| 1154 | |
| 1155 | if (should_synthesise_left != NULL) *should_synthesise_left = false; |
| 1156 | if (should_synthesise_right != NULL) *should_synthesise_right = false; |
| 1157 | |
| 1158 | // The worst case for size occurs when the inputs are two non encodable |
| 1159 | // constants: |
| 1160 | // * up to 4 instructions to materialise the left constant |
| 1161 | // * up to 4 instructions to materialise the right constant |
| 1162 | // * 1 instruction for csel |
| 1163 | EmissionCheckScope guard(masm, 9 * kInstructionSize); |
| 1164 | UseScratchRegisterScope temps; |
| 1165 | if (masm != NULL) { |
| 1166 | temps.Open(masm); |
| 1167 | } |
| 1168 | |
| 1169 | // Try to handle cases where both inputs are immediates. |
| 1170 | bool left_is_immediate = left.IsImmediate() || left.IsZero(); |
| 1171 | bool right_is_immediate = right.IsImmediate() || right.IsZero(); |
| 1172 | if (left_is_immediate && right_is_immediate && |
| 1173 | CselSubHelperTwoImmediates(masm, |
| 1174 | rd, |
| 1175 | left.GetEquivalentImmediate(), |
| 1176 | right.GetEquivalentImmediate(), |
| 1177 | cond, |
| 1178 | should_synthesise_left, |
| 1179 | should_synthesise_right)) { |
| 1180 | return; |
| 1181 | } |
| 1182 | |
| 1183 | // Handle cases where one of the two inputs is -1, 0, or 1. |
| 1184 | bool left_is_small_immediate = |
| 1185 | left_is_immediate && ((-1 <= left.GetEquivalentImmediate()) && |
| 1186 | (left.GetEquivalentImmediate() <= 1)); |
| 1187 | bool right_is_small_immediate = |
| 1188 | right_is_immediate && ((-1 <= right.GetEquivalentImmediate()) && |
| 1189 | (right.GetEquivalentImmediate() <= 1)); |
| 1190 | if (right_is_small_immediate || left_is_small_immediate) { |
| 1191 | bool swapped_inputs = false; |
| 1192 | if (!right_is_small_immediate) { |
| 1193 | std::swap(left, right); |
| 1194 | cond = InvertCondition(cond); |
| 1195 | swapped_inputs = true; |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1196 | } |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 1197 | CselSubHelperRightSmallImmediate(masm, |
| 1198 | &temps, |
| 1199 | rd, |
| 1200 | left, |
| 1201 | right, |
| 1202 | cond, |
| 1203 | swapped_inputs ? should_synthesise_right |
| 1204 | : should_synthesise_left); |
| 1205 | return; |
| 1206 | } |
| 1207 | |
| 1208 | // Otherwise both inputs need to be available in registers. Synthesise them |
| 1209 | // if necessary and emit the `csel`. |
| 1210 | if (!left.IsPlainRegister()) { |
| 1211 | if (emit_code) { |
| 1212 | Register temp = temps.AcquireSameSizeAs(rd); |
| 1213 | masm->Mov(temp, left); |
| 1214 | left = temp; |
| 1215 | } |
| 1216 | if (should_synthesise_left != NULL) *should_synthesise_left = true; |
| 1217 | } |
| 1218 | if (!right.IsPlainRegister()) { |
| 1219 | if (emit_code) { |
| 1220 | Register temp = temps.AcquireSameSizeAs(rd); |
| 1221 | masm->Mov(temp, right); |
| 1222 | right = temp; |
| 1223 | } |
| 1224 | if (should_synthesise_right != NULL) *should_synthesise_right = true; |
| 1225 | } |
| 1226 | if (emit_code) { |
| 1227 | VIXL_ASSERT(left.IsPlainRegister() && right.IsPlainRegister()); |
| 1228 | if (left.reg().Is(right.reg())) { |
| 1229 | masm->Mov(rd, left.reg()); |
| 1230 | } else { |
| 1231 | masm->csel(rd, left.reg(), right.reg(), cond); |
| 1232 | } |
| 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | |
| 1237 | bool MacroAssembler::CselSubHelperTwoImmediates(MacroAssembler* masm, |
| 1238 | const Register& rd, |
| 1239 | int64_t left, |
| 1240 | int64_t right, |
| 1241 | Condition cond, |
| 1242 | bool* should_synthesise_left, |
| 1243 | bool* should_synthesise_right) { |
| 1244 | bool emit_code = (masm != NULL); |
| 1245 | if (should_synthesise_left != NULL) *should_synthesise_left = false; |
| 1246 | if (should_synthesise_right != NULL) *should_synthesise_right = false; |
| 1247 | |
| 1248 | if (left == right) { |
| 1249 | if (emit_code) masm->Mov(rd, left); |
| 1250 | return true; |
| 1251 | } else if (left == -right) { |
| 1252 | if (should_synthesise_right != NULL) *should_synthesise_right = true; |
| 1253 | if (emit_code) { |
| 1254 | masm->Mov(rd, right); |
| 1255 | masm->Cneg(rd, rd, cond); |
| 1256 | } |
| 1257 | return true; |
| 1258 | } |
| 1259 | |
| 1260 | if (CselSubHelperTwoOrderedImmediates(masm, rd, left, right, cond)) { |
| 1261 | return true; |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 1262 | } else { |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 1263 | std::swap(left, right); |
| 1264 | if (CselSubHelperTwoOrderedImmediates(masm, |
| 1265 | rd, |
| 1266 | left, |
| 1267 | right, |
| 1268 | InvertCondition(cond))) { |
| 1269 | return true; |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | // TODO: Handle more situations. For example handle `csel rd, #5, #6, cond` |
| 1274 | // with `cinc`. |
| 1275 | return false; |
| 1276 | } |
| 1277 | |
| 1278 | |
| 1279 | bool MacroAssembler::CselSubHelperTwoOrderedImmediates(MacroAssembler* masm, |
| 1280 | const Register& rd, |
| 1281 | int64_t left, |
| 1282 | int64_t right, |
| 1283 | Condition cond) { |
| 1284 | bool emit_code = (masm != NULL); |
| 1285 | |
Alexandre Rames | 2fbea6c | 2016-06-07 09:21:11 +0100 | [diff] [blame^] | 1286 | if ((left == 1) && (right == 0)) { |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 1287 | if (emit_code) masm->cset(rd, cond); |
| 1288 | return true; |
Alexandre Rames | 2fbea6c | 2016-06-07 09:21:11 +0100 | [diff] [blame^] | 1289 | } else if ((left == -1) && (right == 0)) { |
| 1290 | if (emit_code) masm->csetm(rd, cond); |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 1291 | return true; |
| 1292 | } |
| 1293 | return false; |
| 1294 | } |
| 1295 | |
| 1296 | |
| 1297 | void MacroAssembler::CselSubHelperRightSmallImmediate( |
| 1298 | MacroAssembler* masm, |
| 1299 | UseScratchRegisterScope* temps, |
| 1300 | const Register& rd, |
| 1301 | const Operand& left, |
| 1302 | const Operand& right, |
| 1303 | Condition cond, |
| 1304 | bool* should_synthesise_left) { |
| 1305 | bool emit_code = (masm != NULL); |
| 1306 | VIXL_ASSERT((right.IsImmediate() || right.IsZero()) && |
| 1307 | (-1 <= right.GetEquivalentImmediate()) && |
| 1308 | (right.GetEquivalentImmediate() <= 1)); |
| 1309 | Register left_register; |
| 1310 | |
| 1311 | if (left.IsPlainRegister()) { |
| 1312 | left_register = left.reg(); |
| 1313 | } else { |
| 1314 | if (emit_code) { |
| 1315 | left_register = temps->AcquireSameSizeAs(rd); |
| 1316 | masm->Mov(left_register, left); |
| 1317 | } |
| 1318 | if (should_synthesise_left != NULL) *should_synthesise_left = true; |
| 1319 | } |
| 1320 | if (emit_code) { |
| 1321 | int64_t imm = right.GetEquivalentImmediate(); |
| 1322 | Register zr = AppropriateZeroRegFor(rd); |
| 1323 | if (imm == 0) { |
| 1324 | masm->csel(rd, left_register, zr, cond); |
| 1325 | } else if (imm == 1) { |
| 1326 | masm->csinc(rd, left_register, zr, cond); |
| 1327 | } else { |
| 1328 | VIXL_ASSERT(imm == -1); |
| 1329 | masm->csinv(rd, left_register, zr, cond); |
| 1330 | } |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | |
| 1335 | void MacroAssembler::Add(const Register& rd, |
| 1336 | const Register& rn, |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 1337 | const Operand& operand, |
| 1338 | FlagsUpdate S) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1339 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 1340 | if (operand.IsImmediate() && (operand.immediate() < 0) && |
| 1341 | IsImmAddSub(-operand.immediate())) { |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 1342 | AddSubMacro(rd, rn, -operand.immediate(), S, SUB); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1343 | } else { |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 1344 | AddSubMacro(rd, rn, operand, S, ADD); |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | |
| 1349 | void MacroAssembler::Adds(const Register& rd, |
| 1350 | const Register& rn, |
| 1351 | const Operand& operand) { |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 1352 | Add(rd, rn, operand, SetFlags); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | |
| 1356 | void MacroAssembler::Sub(const Register& rd, |
| 1357 | const Register& rn, |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 1358 | const Operand& operand, |
| 1359 | FlagsUpdate S) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1360 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 1361 | if (operand.IsImmediate() && (operand.immediate() < 0) && |
| 1362 | IsImmAddSub(-operand.immediate())) { |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 1363 | AddSubMacro(rd, rn, -operand.immediate(), S, ADD); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1364 | } else { |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 1365 | AddSubMacro(rd, rn, operand, S, SUB); |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | |
| 1370 | void MacroAssembler::Subs(const Register& rd, |
| 1371 | const Register& rn, |
| 1372 | const Operand& operand) { |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 1373 | Sub(rd, rn, operand, SetFlags); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | |
| 1377 | void MacroAssembler::Cmn(const Register& rn, const Operand& operand) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1378 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 1379 | Adds(AppropriateZeroRegFor(rn), rn, operand); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | |
| 1383 | void MacroAssembler::Cmp(const Register& rn, const Operand& operand) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1384 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 1385 | Subs(AppropriateZeroRegFor(rn), rn, operand); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 1389 | void MacroAssembler::Fcmp(const FPRegister& fn, |
| 1390 | double value, |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 1391 | FPTrapFlags trap) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1392 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 1393 | // The worst case for size is: |
| 1394 | // * 1 to materialise the constant, using literal pool if necessary |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 1395 | // * 1 instruction for fcmp{e} |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 1396 | MacroEmissionCheckScope guard(this); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1397 | if (value != 0.0) { |
| 1398 | UseScratchRegisterScope temps(this); |
| 1399 | FPRegister tmp = temps.AcquireSameSizeAs(fn); |
| 1400 | Fmov(tmp, value); |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 1401 | FPCompareMacro(fn, tmp, trap); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1402 | } else { |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 1403 | FPCompareMacro(fn, value, trap); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 1408 | void MacroAssembler::Fcmpe(const FPRegister& fn, double value) { |
| 1409 | Fcmp(fn, value, EnableTrap); |
| 1410 | } |
| 1411 | |
| 1412 | |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 1413 | void MacroAssembler::Fmov(VRegister vd, double imm) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1414 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 1415 | // Floating point immediates are loaded through the literal pool. |
| 1416 | MacroEmissionCheckScope guard(this); |
| 1417 | |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 1418 | if (vd.Is1S() || vd.Is2S() || vd.Is4S()) { |
| 1419 | Fmov(vd, static_cast<float>(imm)); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1420 | return; |
| 1421 | } |
| 1422 | |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 1423 | VIXL_ASSERT(vd.Is1D() || vd.Is2D()); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1424 | if (IsImmFP64(imm)) { |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 1425 | fmov(vd, imm); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1426 | } else { |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 1427 | uint64_t rawbits = double_to_rawbits(imm); |
| 1428 | if (vd.IsScalar()) { |
| 1429 | if (rawbits == 0) { |
| 1430 | fmov(vd, xzr); |
| 1431 | } else { |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 1432 | ldr(vd, |
| 1433 | new Literal<double>(imm, |
| 1434 | &literal_pool_, |
| 1435 | RawLiteral::kDeletedOnPlacementByPool)); |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 1436 | } |
| 1437 | } else { |
| 1438 | // TODO: consider NEON support for load literal. |
| 1439 | Movi(vd, rawbits); |
| 1440 | } |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1441 | } |
| 1442 | } |
| 1443 | |
| 1444 | |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 1445 | void MacroAssembler::Fmov(VRegister vd, float imm) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1446 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 1447 | // Floating point immediates are loaded through the literal pool. |
| 1448 | MacroEmissionCheckScope guard(this); |
| 1449 | |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 1450 | if (vd.Is1D() || vd.Is2D()) { |
| 1451 | Fmov(vd, static_cast<double>(imm)); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1452 | return; |
| 1453 | } |
| 1454 | |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 1455 | VIXL_ASSERT(vd.Is1S() || vd.Is2S() || vd.Is4S()); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1456 | if (IsImmFP32(imm)) { |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 1457 | fmov(vd, imm); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1458 | } else { |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 1459 | uint32_t rawbits = float_to_rawbits(imm); |
| 1460 | if (vd.IsScalar()) { |
| 1461 | if (rawbits == 0) { |
| 1462 | fmov(vd, wzr); |
| 1463 | } else { |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 1464 | ldr(vd, |
| 1465 | new Literal<float>(imm, |
| 1466 | &literal_pool_, |
| 1467 | RawLiteral::kDeletedOnPlacementByPool)); |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 1468 | } |
| 1469 | } else { |
| 1470 | // TODO: consider NEON support for load literal. |
| 1471 | Movi(vd, rawbits); |
| 1472 | } |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1473 | } |
| 1474 | } |
| 1475 | |
| 1476 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 1477 | void MacroAssembler::Neg(const Register& rd, const Operand& operand) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1478 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1479 | if (operand.IsImmediate()) { |
| 1480 | Mov(rd, -operand.immediate()); |
| 1481 | } else { |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 1482 | Sub(rd, AppropriateZeroRegFor(rd), operand); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1483 | } |
| 1484 | } |
| 1485 | |
| 1486 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 1487 | void MacroAssembler::Negs(const Register& rd, const Operand& operand) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1488 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 1489 | Subs(rd, AppropriateZeroRegFor(rd), operand); |
| 1490 | } |
| 1491 | |
| 1492 | |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 1493 | bool MacroAssembler::TryOneInstrMoveImmediate(const Register& dst, |
| 1494 | int64_t imm) { |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 1495 | return OneInstrMoveImmediateHelper(this, dst, imm); |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | |
| 1499 | Operand MacroAssembler::MoveImmediateForShiftedOp(const Register& dst, |
| 1500 | int64_t imm) { |
| 1501 | int reg_size = dst.size(); |
| 1502 | |
| 1503 | // Encode the immediate in a single move instruction, if possible. |
| 1504 | if (TryOneInstrMoveImmediate(dst, imm)) { |
| 1505 | // The move was successful; nothing to do here. |
| 1506 | } else { |
| 1507 | // Pre-shift the immediate to the least-significant bits of the register. |
| 1508 | int shift_low = CountTrailingZeros(imm, reg_size); |
| 1509 | int64_t imm_low = imm >> shift_low; |
| 1510 | |
| 1511 | // Pre-shift the immediate to the most-significant bits of the register, |
| 1512 | // inserting set bits in the least-significant bits. |
| 1513 | int shift_high = CountLeadingZeros(imm, reg_size); |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 1514 | int64_t imm_high = (imm << shift_high) | ((INT64_C(1) << shift_high) - 1); |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 1515 | |
| 1516 | if (TryOneInstrMoveImmediate(dst, imm_low)) { |
| 1517 | // The new immediate has been moved into the destination's low bits: |
| 1518 | // return a new leftward-shifting operand. |
| 1519 | return Operand(dst, LSL, shift_low); |
| 1520 | } else if (TryOneInstrMoveImmediate(dst, imm_high)) { |
| 1521 | // The new immediate has been moved into the destination's high bits: |
| 1522 | // return a new rightward-shifting operand. |
| 1523 | return Operand(dst, LSR, shift_high); |
| 1524 | } else { |
| 1525 | Mov(dst, imm); |
| 1526 | } |
| 1527 | } |
| 1528 | return Operand(dst); |
| 1529 | } |
| 1530 | |
| 1531 | |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 1532 | void MacroAssembler::ComputeAddress(const Register& dst, |
| 1533 | const MemOperand& mem_op) { |
| 1534 | // We cannot handle pre-indexing or post-indexing. |
| 1535 | VIXL_ASSERT(mem_op.addrmode() == Offset); |
| 1536 | Register base = mem_op.base(); |
| 1537 | if (mem_op.IsImmediateOffset()) { |
| 1538 | Add(dst, base, mem_op.offset()); |
| 1539 | } else { |
| 1540 | VIXL_ASSERT(mem_op.IsRegisterOffset()); |
| 1541 | Register reg_offset = mem_op.regoffset(); |
| 1542 | Shift shift = mem_op.shift(); |
| 1543 | Extend extend = mem_op.extend(); |
| 1544 | if (shift == NO_SHIFT) { |
| 1545 | VIXL_ASSERT(extend != NO_EXTEND); |
| 1546 | Add(dst, base, Operand(reg_offset, extend, mem_op.shift_amount())); |
| 1547 | } else { |
| 1548 | VIXL_ASSERT(extend == NO_EXTEND); |
| 1549 | Add(dst, base, Operand(reg_offset, shift, mem_op.shift_amount())); |
| 1550 | } |
| 1551 | } |
| 1552 | } |
| 1553 | |
| 1554 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1555 | void MacroAssembler::AddSubMacro(const Register& rd, |
| 1556 | const Register& rn, |
| 1557 | const Operand& operand, |
| 1558 | FlagsUpdate S, |
| 1559 | AddSubOp op) { |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 1560 | // Worst case is add/sub immediate: |
| 1561 | // * up to 4 instructions to materialise the constant |
| 1562 | // * 1 instruction for add/sub |
| 1563 | MacroEmissionCheckScope guard(this); |
| 1564 | |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 1565 | if (operand.IsZero() && rd.Is(rn) && rd.Is64Bits() && rn.Is64Bits() && |
| 1566 | (S == LeaveFlags)) { |
| 1567 | // The instruction would be a nop. Avoid generating useless code. |
| 1568 | return; |
| 1569 | } |
| 1570 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1571 | if ((operand.IsImmediate() && !IsImmAddSub(operand.immediate())) || |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 1572 | (rn.IsZero() && !operand.IsShiftedRegister()) || |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1573 | (operand.IsShiftedRegister() && (operand.shift() == ROR))) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1574 | UseScratchRegisterScope temps(this); |
| 1575 | Register temp = temps.AcquireSameSizeAs(rn); |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 1576 | if (operand.IsImmediate()) { |
| 1577 | Operand imm_operand = |
| 1578 | MoveImmediateForShiftedOp(temp, operand.immediate()); |
| 1579 | AddSub(rd, rn, imm_operand, S, op); |
| 1580 | } else { |
| 1581 | Mov(temp, operand); |
| 1582 | AddSub(rd, rn, temp, S, op); |
| 1583 | } |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1584 | } else { |
| 1585 | AddSub(rd, rn, operand, S, op); |
| 1586 | } |
| 1587 | } |
| 1588 | |
| 1589 | |
| 1590 | void MacroAssembler::Adc(const Register& rd, |
| 1591 | const Register& rn, |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 1592 | const Operand& operand) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1593 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 1594 | AddSubWithCarryMacro(rd, rn, operand, LeaveFlags, ADC); |
| 1595 | } |
| 1596 | |
| 1597 | |
| 1598 | void MacroAssembler::Adcs(const Register& rd, |
| 1599 | const Register& rn, |
| 1600 | const Operand& operand) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1601 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 1602 | AddSubWithCarryMacro(rd, rn, operand, SetFlags, ADC); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | |
| 1606 | void MacroAssembler::Sbc(const Register& rd, |
| 1607 | const Register& rn, |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 1608 | const Operand& operand) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1609 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 1610 | AddSubWithCarryMacro(rd, rn, operand, LeaveFlags, SBC); |
| 1611 | } |
| 1612 | |
| 1613 | |
| 1614 | void MacroAssembler::Sbcs(const Register& rd, |
| 1615 | const Register& rn, |
| 1616 | const Operand& operand) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1617 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 1618 | AddSubWithCarryMacro(rd, rn, operand, SetFlags, SBC); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1619 | } |
| 1620 | |
| 1621 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 1622 | void MacroAssembler::Ngc(const Register& rd, const Operand& operand) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1623 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1624 | Register zr = AppropriateZeroRegFor(rd); |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 1625 | Sbc(rd, zr, operand); |
| 1626 | } |
| 1627 | |
| 1628 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 1629 | void MacroAssembler::Ngcs(const Register& rd, const Operand& operand) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1630 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 1631 | Register zr = AppropriateZeroRegFor(rd); |
| 1632 | Sbcs(rd, zr, operand); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1633 | } |
| 1634 | |
| 1635 | |
| 1636 | void MacroAssembler::AddSubWithCarryMacro(const Register& rd, |
| 1637 | const Register& rn, |
| 1638 | const Operand& operand, |
| 1639 | FlagsUpdate S, |
| 1640 | AddSubWithCarryOp op) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1641 | VIXL_ASSERT(rd.size() == rn.size()); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 1642 | // Worst case is addc/subc immediate: |
| 1643 | // * up to 4 instructions to materialise the constant |
| 1644 | // * 1 instruction for add/sub |
| 1645 | MacroEmissionCheckScope guard(this); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1646 | UseScratchRegisterScope temps(this); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1647 | |
| 1648 | if (operand.IsImmediate() || |
| 1649 | (operand.IsShiftedRegister() && (operand.shift() == ROR))) { |
| 1650 | // Add/sub with carry (immediate or ROR shifted register.) |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1651 | Register temp = temps.AcquireSameSizeAs(rn); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1652 | Mov(temp, operand); |
| 1653 | AddSubWithCarry(rd, rn, Operand(temp), S, op); |
| 1654 | } else if (operand.IsShiftedRegister() && (operand.shift_amount() != 0)) { |
| 1655 | // Add/sub with carry (shifted register). |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1656 | VIXL_ASSERT(operand.reg().size() == rd.size()); |
| 1657 | VIXL_ASSERT(operand.shift() != ROR); |
| 1658 | VIXL_ASSERT(is_uintn(rd.size() == kXRegSize ? kXRegSizeLog2 : kWRegSizeLog2, |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 1659 | operand.shift_amount())); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1660 | temps.Exclude(operand.reg()); |
| 1661 | Register temp = temps.AcquireSameSizeAs(rn); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1662 | EmitShift(temp, operand.reg(), operand.shift(), operand.shift_amount()); |
| 1663 | AddSubWithCarry(rd, rn, Operand(temp), S, op); |
| 1664 | } else if (operand.IsExtendedRegister()) { |
| 1665 | // Add/sub with carry (extended register). |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1666 | VIXL_ASSERT(operand.reg().size() <= rd.size()); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1667 | // Add/sub extended supports a shift <= 4. We want to support exactly the |
| 1668 | // same modes. |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1669 | VIXL_ASSERT(operand.shift_amount() <= 4); |
| 1670 | VIXL_ASSERT(operand.reg().Is64Bits() || |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 1671 | ((operand.extend() != UXTX) && (operand.extend() != SXTX))); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1672 | temps.Exclude(operand.reg()); |
| 1673 | Register temp = temps.AcquireSameSizeAs(rn); |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 1674 | EmitExtendShift(temp, |
| 1675 | operand.reg(), |
| 1676 | operand.extend(), |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1677 | operand.shift_amount()); |
| 1678 | AddSubWithCarry(rd, rn, Operand(temp), S, op); |
| 1679 | } else { |
| 1680 | // The addressing mode is directly supported by the instruction. |
| 1681 | AddSubWithCarry(rd, rn, operand, S, op); |
| 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 1686 | #define DEFINE_FUNCTION(FN, REGTYPE, REG, OP) \ |
| 1687 | void MacroAssembler::FN(const REGTYPE REG, const MemOperand& addr) { \ |
| 1688 | VIXL_ASSERT(allow_macro_instructions_); \ |
| 1689 | LoadStoreMacro(REG, addr, OP); \ |
| 1690 | } |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1691 | LS_MACRO_LIST(DEFINE_FUNCTION) |
| 1692 | #undef DEFINE_FUNCTION |
| 1693 | |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 1694 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1695 | void MacroAssembler::LoadStoreMacro(const CPURegister& rt, |
| 1696 | const MemOperand& addr, |
| 1697 | LoadStoreOp op) { |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 1698 | // Worst case is ldr/str pre/post index: |
| 1699 | // * 1 instruction for ldr/str |
| 1700 | // * up to 4 instructions to materialise the constant |
| 1701 | // * 1 instruction to update the base |
| 1702 | MacroEmissionCheckScope guard(this); |
| 1703 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1704 | int64_t offset = addr.offset(); |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 1705 | unsigned access_size = CalcLSDataSize(op); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1706 | |
| 1707 | // Check if an immediate offset fits in the immediate field of the |
| 1708 | // appropriate instruction. If not, emit two instructions to perform |
| 1709 | // the operation. |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 1710 | if (addr.IsImmediateOffset() && !IsImmLSScaled(offset, access_size) && |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1711 | !IsImmLSUnscaled(offset)) { |
| 1712 | // Immediate offset that can't be encoded using unsigned or unscaled |
| 1713 | // addressing modes. |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1714 | UseScratchRegisterScope temps(this); |
| 1715 | Register temp = temps.AcquireSameSizeAs(addr.base()); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1716 | Mov(temp, addr.offset()); |
| 1717 | LoadStore(rt, MemOperand(addr.base(), temp), op); |
| 1718 | } else if (addr.IsPostIndex() && !IsImmLSUnscaled(offset)) { |
| 1719 | // Post-index beyond unscaled addressing range. |
| 1720 | LoadStore(rt, MemOperand(addr.base()), op); |
| 1721 | Add(addr.base(), addr.base(), Operand(offset)); |
| 1722 | } else if (addr.IsPreIndex() && !IsImmLSUnscaled(offset)) { |
| 1723 | // Pre-index beyond unscaled addressing range. |
| 1724 | Add(addr.base(), addr.base(), Operand(offset)); |
| 1725 | LoadStore(rt, MemOperand(addr.base()), op); |
| 1726 | } else { |
| 1727 | // Encodable in one load/store instruction. |
| 1728 | LoadStore(rt, addr, op); |
| 1729 | } |
| 1730 | } |
| 1731 | |
| 1732 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 1733 | #define DEFINE_FUNCTION(FN, REGTYPE, REG, REG2, OP) \ |
| 1734 | void MacroAssembler::FN(const REGTYPE REG, \ |
| 1735 | const REGTYPE REG2, \ |
| 1736 | const MemOperand& addr) { \ |
| 1737 | VIXL_ASSERT(allow_macro_instructions_); \ |
| 1738 | LoadStorePairMacro(REG, REG2, addr, OP); \ |
| 1739 | } |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 1740 | LSPAIR_MACRO_LIST(DEFINE_FUNCTION) |
| 1741 | #undef DEFINE_FUNCTION |
| 1742 | |
| 1743 | void MacroAssembler::LoadStorePairMacro(const CPURegister& rt, |
| 1744 | const CPURegister& rt2, |
| 1745 | const MemOperand& addr, |
| 1746 | LoadStorePairOp op) { |
| 1747 | // TODO(all): Should we support register offset for load-store-pair? |
| 1748 | VIXL_ASSERT(!addr.IsRegisterOffset()); |
| 1749 | // Worst case is ldp/stp immediate: |
| 1750 | // * 1 instruction for ldp/stp |
| 1751 | // * up to 4 instructions to materialise the constant |
| 1752 | // * 1 instruction to update the base |
| 1753 | MacroEmissionCheckScope guard(this); |
| 1754 | |
| 1755 | int64_t offset = addr.offset(); |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 1756 | unsigned access_size = CalcLSPairDataSize(op); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 1757 | |
| 1758 | // Check if the offset fits in the immediate field of the appropriate |
| 1759 | // instruction. If not, emit two instructions to perform the operation. |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 1760 | if (IsImmLSPair(offset, access_size)) { |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 1761 | // Encodable in one load/store pair instruction. |
| 1762 | LoadStorePair(rt, rt2, addr, op); |
| 1763 | } else { |
| 1764 | Register base = addr.base(); |
| 1765 | if (addr.IsImmediateOffset()) { |
| 1766 | UseScratchRegisterScope temps(this); |
| 1767 | Register temp = temps.AcquireSameSizeAs(base); |
| 1768 | Add(temp, base, offset); |
| 1769 | LoadStorePair(rt, rt2, MemOperand(temp), op); |
| 1770 | } else if (addr.IsPostIndex()) { |
| 1771 | LoadStorePair(rt, rt2, MemOperand(base), op); |
| 1772 | Add(base, base, offset); |
| 1773 | } else { |
| 1774 | VIXL_ASSERT(addr.IsPreIndex()); |
| 1775 | Add(base, base, offset); |
| 1776 | LoadStorePair(rt, rt2, MemOperand(base), op); |
| 1777 | } |
| 1778 | } |
| 1779 | } |
| 1780 | |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 1781 | |
| 1782 | void MacroAssembler::Prfm(PrefetchOperation op, const MemOperand& addr) { |
| 1783 | MacroEmissionCheckScope guard(this); |
| 1784 | |
| 1785 | // There are no pre- or post-index modes for prfm. |
| 1786 | VIXL_ASSERT(addr.IsImmediateOffset() || addr.IsRegisterOffset()); |
| 1787 | |
| 1788 | // The access size is implicitly 8 bytes for all prefetch operations. |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 1789 | unsigned size = kXRegSizeInBytesLog2; |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 1790 | |
| 1791 | // Check if an immediate offset fits in the immediate field of the |
| 1792 | // appropriate instruction. If not, emit two instructions to perform |
| 1793 | // the operation. |
| 1794 | if (addr.IsImmediateOffset() && !IsImmLSScaled(addr.offset(), size) && |
| 1795 | !IsImmLSUnscaled(addr.offset())) { |
| 1796 | // Immediate offset that can't be encoded using unsigned or unscaled |
| 1797 | // addressing modes. |
| 1798 | UseScratchRegisterScope temps(this); |
| 1799 | Register temp = temps.AcquireSameSizeAs(addr.base()); |
| 1800 | Mov(temp, addr.offset()); |
| 1801 | Prefetch(op, MemOperand(addr.base(), temp)); |
| 1802 | } else { |
| 1803 | // Simple register-offsets are encodable in one instruction. |
| 1804 | Prefetch(op, addr); |
| 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 1809 | void MacroAssembler::Push(const CPURegister& src0, |
| 1810 | const CPURegister& src1, |
| 1811 | const CPURegister& src2, |
| 1812 | const CPURegister& src3) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1813 | VIXL_ASSERT(allow_macro_instructions_); |
| 1814 | VIXL_ASSERT(AreSameSizeAndType(src0, src1, src2, src3)); |
| 1815 | VIXL_ASSERT(src0.IsValid()); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1816 | |
| 1817 | int count = 1 + src1.IsValid() + src2.IsValid() + src3.IsValid(); |
| 1818 | int size = src0.SizeInBytes(); |
| 1819 | |
| 1820 | PrepareForPush(count, size); |
| 1821 | PushHelper(count, size, src0, src1, src2, src3); |
| 1822 | } |
| 1823 | |
| 1824 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 1825 | void MacroAssembler::Pop(const CPURegister& dst0, |
| 1826 | const CPURegister& dst1, |
| 1827 | const CPURegister& dst2, |
| 1828 | const CPURegister& dst3) { |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1829 | // It is not valid to pop into the same register more than once in one |
| 1830 | // instruction, not even into the zero register. |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1831 | VIXL_ASSERT(allow_macro_instructions_); |
| 1832 | VIXL_ASSERT(!AreAliased(dst0, dst1, dst2, dst3)); |
| 1833 | VIXL_ASSERT(AreSameSizeAndType(dst0, dst1, dst2, dst3)); |
| 1834 | VIXL_ASSERT(dst0.IsValid()); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1835 | |
| 1836 | int count = 1 + dst1.IsValid() + dst2.IsValid() + dst3.IsValid(); |
| 1837 | int size = dst0.SizeInBytes(); |
| 1838 | |
| 1839 | PrepareForPop(count, size); |
| 1840 | PopHelper(count, size, dst0, dst1, dst2, dst3); |
| 1841 | } |
| 1842 | |
| 1843 | |
| 1844 | void MacroAssembler::PushCPURegList(CPURegList registers) { |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 1845 | VIXL_ASSERT(!registers.Overlaps(*TmpList())); |
| 1846 | VIXL_ASSERT(!registers.Overlaps(*FPTmpList())); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1847 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 1848 | |
| 1849 | int reg_size = registers.RegisterSizeInBytes(); |
| 1850 | PrepareForPush(registers.Count(), reg_size); |
| 1851 | |
| 1852 | // Bump the stack pointer and store two registers at the bottom. |
| 1853 | int size = registers.TotalSizeInBytes(); |
| 1854 | const CPURegister& bottom_0 = registers.PopLowestIndex(); |
| 1855 | const CPURegister& bottom_1 = registers.PopLowestIndex(); |
| 1856 | if (bottom_0.IsValid() && bottom_1.IsValid()) { |
| 1857 | Stp(bottom_0, bottom_1, MemOperand(StackPointer(), -size, PreIndex)); |
| 1858 | } else if (bottom_0.IsValid()) { |
| 1859 | Str(bottom_0, MemOperand(StackPointer(), -size, PreIndex)); |
| 1860 | } |
| 1861 | |
| 1862 | int offset = 2 * reg_size; |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1863 | while (!registers.IsEmpty()) { |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 1864 | const CPURegister& src0 = registers.PopLowestIndex(); |
| 1865 | const CPURegister& src1 = registers.PopLowestIndex(); |
| 1866 | if (src1.IsValid()) { |
| 1867 | Stp(src0, src1, MemOperand(StackPointer(), offset)); |
| 1868 | } else { |
| 1869 | Str(src0, MemOperand(StackPointer(), offset)); |
| 1870 | } |
| 1871 | offset += 2 * reg_size; |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1872 | } |
| 1873 | } |
| 1874 | |
| 1875 | |
| 1876 | void MacroAssembler::PopCPURegList(CPURegList registers) { |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 1877 | VIXL_ASSERT(!registers.Overlaps(*TmpList())); |
| 1878 | VIXL_ASSERT(!registers.Overlaps(*FPTmpList())); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1879 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 1880 | |
| 1881 | int reg_size = registers.RegisterSizeInBytes(); |
| 1882 | PrepareForPop(registers.Count(), reg_size); |
| 1883 | |
| 1884 | |
| 1885 | int size = registers.TotalSizeInBytes(); |
| 1886 | const CPURegister& bottom_0 = registers.PopLowestIndex(); |
| 1887 | const CPURegister& bottom_1 = registers.PopLowestIndex(); |
| 1888 | |
| 1889 | int offset = 2 * reg_size; |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1890 | while (!registers.IsEmpty()) { |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1891 | const CPURegister& dst0 = registers.PopLowestIndex(); |
| 1892 | const CPURegister& dst1 = registers.PopLowestIndex(); |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 1893 | if (dst1.IsValid()) { |
| 1894 | Ldp(dst0, dst1, MemOperand(StackPointer(), offset)); |
| 1895 | } else { |
| 1896 | Ldr(dst0, MemOperand(StackPointer(), offset)); |
| 1897 | } |
| 1898 | offset += 2 * reg_size; |
| 1899 | } |
| 1900 | |
| 1901 | // Load the two registers at the bottom and drop the stack pointer. |
| 1902 | if (bottom_0.IsValid() && bottom_1.IsValid()) { |
| 1903 | Ldp(bottom_0, bottom_1, MemOperand(StackPointer(), size, PostIndex)); |
| 1904 | } else if (bottom_0.IsValid()) { |
| 1905 | Ldr(bottom_0, MemOperand(StackPointer(), size, PostIndex)); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1906 | } |
| 1907 | } |
| 1908 | |
| 1909 | |
| 1910 | void MacroAssembler::PushMultipleTimes(int count, Register src) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1911 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1912 | int size = src.SizeInBytes(); |
| 1913 | |
| 1914 | PrepareForPush(count, size); |
| 1915 | // Push up to four registers at a time if possible because if the current |
| 1916 | // stack pointer is sp and the register size is 32, registers must be pushed |
| 1917 | // in blocks of four in order to maintain the 16-byte alignment for sp. |
| 1918 | while (count >= 4) { |
| 1919 | PushHelper(4, size, src, src, src, src); |
| 1920 | count -= 4; |
| 1921 | } |
| 1922 | if (count >= 2) { |
| 1923 | PushHelper(2, size, src, src, NoReg, NoReg); |
| 1924 | count -= 2; |
| 1925 | } |
| 1926 | if (count == 1) { |
| 1927 | PushHelper(1, size, src, NoReg, NoReg, NoReg); |
| 1928 | count -= 1; |
| 1929 | } |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1930 | VIXL_ASSERT(count == 0); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1931 | } |
| 1932 | |
| 1933 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 1934 | void MacroAssembler::PushHelper(int count, |
| 1935 | int size, |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1936 | const CPURegister& src0, |
| 1937 | const CPURegister& src1, |
| 1938 | const CPURegister& src2, |
| 1939 | const CPURegister& src3) { |
| 1940 | // Ensure that we don't unintentionally modify scratch or debug registers. |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 1941 | // Worst case for size is 2 stp. |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 1942 | InstructionAccurateScope scope(this, |
| 1943 | 2, |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 1944 | InstructionAccurateScope::kMaximumSize); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1945 | |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1946 | VIXL_ASSERT(AreSameSizeAndType(src0, src1, src2, src3)); |
| 1947 | VIXL_ASSERT(size == src0.SizeInBytes()); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1948 | |
| 1949 | // When pushing multiple registers, the store order is chosen such that |
| 1950 | // Push(a, b) is equivalent to Push(a) followed by Push(b). |
| 1951 | switch (count) { |
| 1952 | case 1: |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1953 | VIXL_ASSERT(src1.IsNone() && src2.IsNone() && src3.IsNone()); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1954 | str(src0, MemOperand(StackPointer(), -1 * size, PreIndex)); |
| 1955 | break; |
| 1956 | case 2: |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1957 | VIXL_ASSERT(src2.IsNone() && src3.IsNone()); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1958 | stp(src1, src0, MemOperand(StackPointer(), -2 * size, PreIndex)); |
| 1959 | break; |
| 1960 | case 3: |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1961 | VIXL_ASSERT(src3.IsNone()); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1962 | stp(src2, src1, MemOperand(StackPointer(), -3 * size, PreIndex)); |
| 1963 | str(src0, MemOperand(StackPointer(), 2 * size)); |
| 1964 | break; |
| 1965 | case 4: |
| 1966 | // Skip over 4 * size, then fill in the gap. This allows four W registers |
| 1967 | // to be pushed using sp, whilst maintaining 16-byte alignment for sp at |
| 1968 | // all times. |
| 1969 | stp(src3, src2, MemOperand(StackPointer(), -4 * size, PreIndex)); |
| 1970 | stp(src1, src0, MemOperand(StackPointer(), 2 * size)); |
| 1971 | break; |
| 1972 | default: |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1973 | VIXL_UNREACHABLE(); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1974 | } |
| 1975 | } |
| 1976 | |
| 1977 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 1978 | void MacroAssembler::PopHelper(int count, |
| 1979 | int size, |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1980 | const CPURegister& dst0, |
| 1981 | const CPURegister& dst1, |
| 1982 | const CPURegister& dst2, |
| 1983 | const CPURegister& dst3) { |
| 1984 | // Ensure that we don't unintentionally modify scratch or debug registers. |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 1985 | // Worst case for size is 2 ldp. |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 1986 | InstructionAccurateScope scope(this, |
| 1987 | 2, |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 1988 | InstructionAccurateScope::kMaximumSize); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1989 | |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1990 | VIXL_ASSERT(AreSameSizeAndType(dst0, dst1, dst2, dst3)); |
| 1991 | VIXL_ASSERT(size == dst0.SizeInBytes()); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1992 | |
| 1993 | // When popping multiple registers, the load order is chosen such that |
| 1994 | // Pop(a, b) is equivalent to Pop(a) followed by Pop(b). |
| 1995 | switch (count) { |
| 1996 | case 1: |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 1997 | VIXL_ASSERT(dst1.IsNone() && dst2.IsNone() && dst3.IsNone()); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1998 | ldr(dst0, MemOperand(StackPointer(), 1 * size, PostIndex)); |
| 1999 | break; |
| 2000 | case 2: |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2001 | VIXL_ASSERT(dst2.IsNone() && dst3.IsNone()); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2002 | ldp(dst0, dst1, MemOperand(StackPointer(), 2 * size, PostIndex)); |
| 2003 | break; |
| 2004 | case 3: |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2005 | VIXL_ASSERT(dst3.IsNone()); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2006 | ldr(dst2, MemOperand(StackPointer(), 2 * size)); |
| 2007 | ldp(dst0, dst1, MemOperand(StackPointer(), 3 * size, PostIndex)); |
| 2008 | break; |
| 2009 | case 4: |
| 2010 | // Load the higher addresses first, then load the lower addresses and skip |
| 2011 | // the whole block in the second instruction. This allows four W registers |
| 2012 | // to be popped using sp, whilst maintaining 16-byte alignment for sp at |
| 2013 | // all times. |
| 2014 | ldp(dst2, dst3, MemOperand(StackPointer(), 2 * size)); |
| 2015 | ldp(dst0, dst1, MemOperand(StackPointer(), 4 * size, PostIndex)); |
| 2016 | break; |
| 2017 | default: |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2018 | VIXL_UNREACHABLE(); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2019 | } |
| 2020 | } |
| 2021 | |
| 2022 | |
| 2023 | void MacroAssembler::PrepareForPush(int count, int size) { |
| 2024 | if (sp.Is(StackPointer())) { |
| 2025 | // If the current stack pointer is sp, then it must be aligned to 16 bytes |
| 2026 | // on entry and the total size of the specified registers must also be a |
| 2027 | // multiple of 16 bytes. |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2028 | VIXL_ASSERT((count * size) % 16 == 0); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2029 | } else { |
| 2030 | // Even if the current stack pointer is not the system stack pointer (sp), |
| 2031 | // the system stack pointer will still be modified in order to comply with |
| 2032 | // ABI rules about accessing memory below the system stack pointer. |
| 2033 | BumpSystemStackPointer(count * size); |
| 2034 | } |
| 2035 | } |
| 2036 | |
| 2037 | |
| 2038 | void MacroAssembler::PrepareForPop(int count, int size) { |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 2039 | USE(count, size); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2040 | if (sp.Is(StackPointer())) { |
| 2041 | // If the current stack pointer is sp, then it must be aligned to 16 bytes |
| 2042 | // on entry and the total size of the specified registers must also be a |
| 2043 | // multiple of 16 bytes. |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2044 | VIXL_ASSERT((count * size) % 16 == 0); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2045 | } |
| 2046 | } |
| 2047 | |
| 2048 | void MacroAssembler::Poke(const Register& src, const Operand& offset) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2049 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2050 | if (offset.IsImmediate()) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2051 | VIXL_ASSERT(offset.immediate() >= 0); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2052 | } |
| 2053 | |
| 2054 | Str(src, MemOperand(StackPointer(), offset)); |
| 2055 | } |
| 2056 | |
| 2057 | |
| 2058 | void MacroAssembler::Peek(const Register& dst, const Operand& offset) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2059 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2060 | if (offset.IsImmediate()) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2061 | VIXL_ASSERT(offset.immediate() >= 0); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2062 | } |
| 2063 | |
| 2064 | Ldr(dst, MemOperand(StackPointer(), offset)); |
| 2065 | } |
| 2066 | |
| 2067 | |
| 2068 | void MacroAssembler::Claim(const Operand& size) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2069 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 2070 | |
| 2071 | if (size.IsZero()) { |
| 2072 | return; |
| 2073 | } |
| 2074 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2075 | if (size.IsImmediate()) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2076 | VIXL_ASSERT(size.immediate() > 0); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2077 | if (sp.Is(StackPointer())) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2078 | VIXL_ASSERT((size.immediate() % 16) == 0); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2079 | } |
| 2080 | } |
| 2081 | |
| 2082 | if (!sp.Is(StackPointer())) { |
| 2083 | BumpSystemStackPointer(size); |
| 2084 | } |
| 2085 | |
| 2086 | Sub(StackPointer(), StackPointer(), size); |
| 2087 | } |
| 2088 | |
| 2089 | |
| 2090 | void MacroAssembler::Drop(const Operand& size) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2091 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 2092 | |
| 2093 | if (size.IsZero()) { |
| 2094 | return; |
| 2095 | } |
| 2096 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2097 | if (size.IsImmediate()) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2098 | VIXL_ASSERT(size.immediate() > 0); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2099 | if (sp.Is(StackPointer())) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2100 | VIXL_ASSERT((size.immediate() % 16) == 0); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2101 | } |
| 2102 | } |
| 2103 | |
| 2104 | Add(StackPointer(), StackPointer(), size); |
| 2105 | } |
| 2106 | |
| 2107 | |
| 2108 | void MacroAssembler::PushCalleeSavedRegisters() { |
| 2109 | // Ensure that the macro-assembler doesn't use any scratch registers. |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 2110 | // 10 stp will be emitted. |
| 2111 | // TODO(all): Should we use GetCalleeSaved and SavedFP. |
| 2112 | InstructionAccurateScope scope(this, 10); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2113 | |
| 2114 | // This method must not be called unless the current stack pointer is sp. |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2115 | VIXL_ASSERT(sp.Is(StackPointer())); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2116 | |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 2117 | MemOperand tos(sp, -2 * static_cast<int>(kXRegSizeInBytes), PreIndex); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2118 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2119 | stp(x29, x30, tos); |
| 2120 | stp(x27, x28, tos); |
| 2121 | stp(x25, x26, tos); |
| 2122 | stp(x23, x24, tos); |
| 2123 | stp(x21, x22, tos); |
| 2124 | stp(x19, x20, tos); |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2125 | |
| 2126 | stp(d14, d15, tos); |
| 2127 | stp(d12, d13, tos); |
| 2128 | stp(d10, d11, tos); |
| 2129 | stp(d8, d9, tos); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2130 | } |
| 2131 | |
| 2132 | |
| 2133 | void MacroAssembler::PopCalleeSavedRegisters() { |
| 2134 | // Ensure that the macro-assembler doesn't use any scratch registers. |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 2135 | // 10 ldp will be emitted. |
| 2136 | // TODO(all): Should we use GetCalleeSaved and SavedFP. |
| 2137 | InstructionAccurateScope scope(this, 10); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2138 | |
| 2139 | // This method must not be called unless the current stack pointer is sp. |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2140 | VIXL_ASSERT(sp.Is(StackPointer())); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2141 | |
| 2142 | MemOperand tos(sp, 2 * kXRegSizeInBytes, PostIndex); |
| 2143 | |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2144 | ldp(d8, d9, tos); |
| 2145 | ldp(d10, d11, tos); |
| 2146 | ldp(d12, d13, tos); |
| 2147 | ldp(d14, d15, tos); |
| 2148 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2149 | ldp(x19, x20, tos); |
| 2150 | ldp(x21, x22, tos); |
| 2151 | ldp(x23, x24, tos); |
| 2152 | ldp(x25, x26, tos); |
| 2153 | ldp(x27, x28, tos); |
| 2154 | ldp(x29, x30, tos); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2155 | } |
| 2156 | |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 2157 | void MacroAssembler::LoadCPURegList(CPURegList registers, |
| 2158 | const MemOperand& src) { |
| 2159 | LoadStoreCPURegListHelper(kLoad, registers, src); |
| 2160 | } |
| 2161 | |
| 2162 | void MacroAssembler::StoreCPURegList(CPURegList registers, |
| 2163 | const MemOperand& dst) { |
| 2164 | LoadStoreCPURegListHelper(kStore, registers, dst); |
| 2165 | } |
| 2166 | |
| 2167 | |
| 2168 | void MacroAssembler::LoadStoreCPURegListHelper(LoadStoreCPURegListAction op, |
| 2169 | CPURegList registers, |
| 2170 | const MemOperand& mem) { |
| 2171 | // We do not handle pre-indexing or post-indexing. |
| 2172 | VIXL_ASSERT(!(mem.IsPreIndex() || mem.IsPostIndex())); |
| 2173 | VIXL_ASSERT(!registers.Overlaps(tmp_list_)); |
| 2174 | VIXL_ASSERT(!registers.Overlaps(fptmp_list_)); |
| 2175 | VIXL_ASSERT(!registers.IncludesAliasOf(sp)); |
| 2176 | |
| 2177 | UseScratchRegisterScope temps(this); |
| 2178 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 2179 | MemOperand loc = BaseMemOperandForLoadStoreCPURegList(registers, mem, &temps); |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 2180 | |
| 2181 | while (registers.Count() >= 2) { |
| 2182 | const CPURegister& dst0 = registers.PopLowestIndex(); |
| 2183 | const CPURegister& dst1 = registers.PopLowestIndex(); |
| 2184 | if (op == kStore) { |
| 2185 | Stp(dst0, dst1, loc); |
| 2186 | } else { |
| 2187 | VIXL_ASSERT(op == kLoad); |
| 2188 | Ldp(dst0, dst1, loc); |
| 2189 | } |
| 2190 | loc.AddOffset(2 * registers.RegisterSizeInBytes()); |
| 2191 | } |
| 2192 | if (!registers.IsEmpty()) { |
| 2193 | if (op == kStore) { |
| 2194 | Str(registers.PopLowestIndex(), loc); |
| 2195 | } else { |
| 2196 | VIXL_ASSERT(op == kLoad); |
| 2197 | Ldr(registers.PopLowestIndex(), loc); |
| 2198 | } |
| 2199 | } |
| 2200 | } |
| 2201 | |
| 2202 | MemOperand MacroAssembler::BaseMemOperandForLoadStoreCPURegList( |
| 2203 | const CPURegList& registers, |
| 2204 | const MemOperand& mem, |
| 2205 | UseScratchRegisterScope* scratch_scope) { |
| 2206 | // If necessary, pre-compute the base address for the accesses. |
| 2207 | if (mem.IsRegisterOffset()) { |
| 2208 | Register reg_base = scratch_scope->AcquireX(); |
| 2209 | ComputeAddress(reg_base, mem); |
| 2210 | return MemOperand(reg_base); |
| 2211 | |
| 2212 | } else if (mem.IsImmediateOffset()) { |
| 2213 | int reg_size = registers.RegisterSizeInBytes(); |
| 2214 | int total_size = registers.TotalSizeInBytes(); |
| 2215 | int64_t min_offset = mem.offset(); |
| 2216 | int64_t max_offset = mem.offset() + std::max(0, total_size - 2 * reg_size); |
| 2217 | if ((registers.Count() >= 2) && |
| 2218 | (!Assembler::IsImmLSPair(min_offset, WhichPowerOf2(reg_size)) || |
| 2219 | !Assembler::IsImmLSPair(max_offset, WhichPowerOf2(reg_size)))) { |
| 2220 | Register reg_base = scratch_scope->AcquireX(); |
| 2221 | ComputeAddress(reg_base, mem); |
| 2222 | return MemOperand(reg_base); |
| 2223 | } |
| 2224 | } |
| 2225 | |
| 2226 | return mem; |
| 2227 | } |
| 2228 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2229 | void MacroAssembler::BumpSystemStackPointer(const Operand& space) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2230 | VIXL_ASSERT(!sp.Is(StackPointer())); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2231 | // TODO: Several callers rely on this not using scratch registers, so we use |
| 2232 | // the assembler directly here. However, this means that large immediate |
| 2233 | // values of 'space' cannot be handled. |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 2234 | InstructionAccurateScope scope(this, 1); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2235 | sub(sp, StackPointer(), space); |
| 2236 | } |
| 2237 | |
| 2238 | |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 2239 | // TODO(all): Fix printf for NEON registers, and resolve whether we should be |
| 2240 | // using FPRegister or VRegister here. |
| 2241 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2242 | // This is the main Printf implementation. All callee-saved registers are |
| 2243 | // preserved, but NZCV and the caller-saved registers may be clobbered. |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 2244 | void MacroAssembler::PrintfNoPreserve(const char* format, |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2245 | const CPURegister& arg0, |
| 2246 | const CPURegister& arg1, |
| 2247 | const CPURegister& arg2, |
| 2248 | const CPURegister& arg3) { |
| 2249 | // We cannot handle a caller-saved stack pointer. It doesn't make much sense |
| 2250 | // in most cases anyway, so this restriction shouldn't be too serious. |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2251 | VIXL_ASSERT(!kCallerSaved.IncludesAliasOf(StackPointer())); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2252 | |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2253 | // The provided arguments, and their proper PCS registers. |
| 2254 | CPURegister args[kPrintfMaxArgCount] = {arg0, arg1, arg2, arg3}; |
| 2255 | CPURegister pcs[kPrintfMaxArgCount]; |
| 2256 | |
| 2257 | int arg_count = kPrintfMaxArgCount; |
| 2258 | |
| 2259 | // The PCS varargs registers for printf. Note that x0 is used for the printf |
| 2260 | // format string. |
| 2261 | static const CPURegList kPCSVarargs = |
| 2262 | CPURegList(CPURegister::kRegister, kXRegSize, 1, arg_count); |
| 2263 | static const CPURegList kPCSVarargsFP = |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 2264 | CPURegList(CPURegister::kVRegister, kDRegSize, 0, arg_count - 1); |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2265 | |
| 2266 | // We can use caller-saved registers as scratch values, except for the |
| 2267 | // arguments and the PCS registers where they might need to go. |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2268 | UseScratchRegisterScope temps(this); |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2269 | temps.Include(kCallerSaved); |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 2270 | temps.Include(kCallerSavedV); |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2271 | temps.Exclude(kPCSVarargs); |
| 2272 | temps.Exclude(kPCSVarargsFP); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2273 | temps.Exclude(arg0, arg1, arg2, arg3); |
| 2274 | |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2275 | // Copies of the arg lists that we can iterate through. |
| 2276 | CPURegList pcs_varargs = kPCSVarargs; |
| 2277 | CPURegList pcs_varargs_fp = kPCSVarargsFP; |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2278 | |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2279 | // Place the arguments. There are lots of clever tricks and optimizations we |
| 2280 | // could use here, but Printf is a debug tool so instead we just try to keep |
| 2281 | // it simple: Move each input that isn't already in the right place to a |
| 2282 | // scratch register, then move everything back. |
| 2283 | for (unsigned i = 0; i < kPrintfMaxArgCount; i++) { |
| 2284 | // Work out the proper PCS register for this argument. |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2285 | if (args[i].IsRegister()) { |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2286 | pcs[i] = pcs_varargs.PopLowestIndex().X(); |
| 2287 | // We might only need a W register here. We need to know the size of the |
| 2288 | // argument so we can properly encode it for the simulator call. |
| 2289 | if (args[i].Is32Bits()) pcs[i] = pcs[i].W(); |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 2290 | } else if (args[i].IsVRegister()) { |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2291 | // In C, floats are always cast to doubles for varargs calls. |
| 2292 | pcs[i] = pcs_varargs_fp.PopLowestIndex().D(); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2293 | } else { |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2294 | VIXL_ASSERT(args[i].IsNone()); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2295 | arg_count = i; |
| 2296 | break; |
| 2297 | } |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2298 | |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2299 | // If the argument is already in the right place, leave it where it is. |
| 2300 | if (args[i].Aliases(pcs[i])) continue; |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2301 | |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2302 | // Otherwise, if the argument is in a PCS argument register, allocate an |
| 2303 | // appropriate scratch register and then move it out of the way. |
| 2304 | if (kPCSVarargs.IncludesAliasOf(args[i]) || |
| 2305 | kPCSVarargsFP.IncludesAliasOf(args[i])) { |
| 2306 | if (args[i].IsRegister()) { |
| 2307 | Register old_arg = Register(args[i]); |
| 2308 | Register new_arg = temps.AcquireSameSizeAs(old_arg); |
| 2309 | Mov(new_arg, old_arg); |
| 2310 | args[i] = new_arg; |
| 2311 | } else { |
| 2312 | FPRegister old_arg = FPRegister(args[i]); |
| 2313 | FPRegister new_arg = temps.AcquireSameSizeAs(old_arg); |
| 2314 | Fmov(new_arg, old_arg); |
| 2315 | args[i] = new_arg; |
| 2316 | } |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2317 | } |
| 2318 | } |
| 2319 | |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2320 | // Do a second pass to move values into their final positions and perform any |
| 2321 | // conversions that may be required. |
| 2322 | for (int i = 0; i < arg_count; i++) { |
| 2323 | VIXL_ASSERT(pcs[i].type() == args[i].type()); |
| 2324 | if (pcs[i].IsRegister()) { |
| 2325 | Mov(Register(pcs[i]), Register(args[i]), kDiscardForSameWReg); |
| 2326 | } else { |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 2327 | VIXL_ASSERT(pcs[i].IsVRegister()); |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2328 | if (pcs[i].size() == args[i].size()) { |
| 2329 | Fmov(FPRegister(pcs[i]), FPRegister(args[i])); |
| 2330 | } else { |
| 2331 | Fcvt(FPRegister(pcs[i]), FPRegister(args[i])); |
| 2332 | } |
| 2333 | } |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2334 | } |
| 2335 | |
| 2336 | // Load the format string into x0, as per the procedure-call standard. |
| 2337 | // |
| 2338 | // To make the code as portable as possible, the format string is encoded |
| 2339 | // directly in the instruction stream. It might be cleaner to encode it in a |
| 2340 | // literal pool, but since Printf is usually used for debugging, it is |
| 2341 | // beneficial for it to be minimally dependent on other features. |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2342 | temps.Exclude(x0); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2343 | Label format_address; |
| 2344 | Adr(x0, &format_address); |
| 2345 | |
| 2346 | // Emit the format string directly in the instruction stream. |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 2347 | { |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 2348 | BlockPoolsScope scope(this); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 2349 | // Data emitted: |
| 2350 | // branch |
| 2351 | // strlen(format) + 1 (includes null termination) |
| 2352 | // padding to next instruction |
| 2353 | // unreachable |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 2354 | EmissionCheckScope guard(this, |
| 2355 | AlignUp(strlen(format) + 1, kInstructionSize) + |
| 2356 | 2 * kInstructionSize); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2357 | Label after_data; |
| 2358 | B(&after_data); |
| 2359 | Bind(&format_address); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 2360 | EmitString(format); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2361 | Unreachable(); |
| 2362 | Bind(&after_data); |
| 2363 | } |
| 2364 | |
| 2365 | // We don't pass any arguments on the stack, but we still need to align the C |
| 2366 | // stack pointer to a 16-byte boundary for PCS compliance. |
| 2367 | if (!sp.Is(StackPointer())) { |
| 2368 | Bic(sp, StackPointer(), 0xf); |
| 2369 | } |
| 2370 | |
| 2371 | // Actually call printf. This part needs special handling for the simulator, |
| 2372 | // since the system printf function will use a different instruction set and |
| 2373 | // the procedure-call standard will not be compatible. |
armvixl | 684cd2a | 2015-10-23 13:38:33 +0100 | [diff] [blame] | 2374 | if (allow_simulator_instructions_) { |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 2375 | InstructionAccurateScope scope(this, kPrintfLength / kInstructionSize); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2376 | hlt(kPrintfOpcode); |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 2377 | dc32(arg_count); // kPrintfArgCountOffset |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2378 | |
| 2379 | // Determine the argument pattern. |
| 2380 | uint32_t arg_pattern_list = 0; |
| 2381 | for (int i = 0; i < arg_count; i++) { |
| 2382 | uint32_t arg_pattern; |
| 2383 | if (pcs[i].IsRegister()) { |
| 2384 | arg_pattern = pcs[i].Is32Bits() ? kPrintfArgW : kPrintfArgX; |
| 2385 | } else { |
| 2386 | VIXL_ASSERT(pcs[i].Is64Bits()); |
| 2387 | arg_pattern = kPrintfArgD; |
| 2388 | } |
| 2389 | VIXL_ASSERT(arg_pattern < (1 << kPrintfArgPatternBits)); |
| 2390 | arg_pattern_list |= (arg_pattern << (kPrintfArgPatternBits * i)); |
| 2391 | } |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 2392 | dc32(arg_pattern_list); // kPrintfArgPatternListOffset |
armvixl | 684cd2a | 2015-10-23 13:38:33 +0100 | [diff] [blame] | 2393 | } else { |
| 2394 | Register tmp = temps.AcquireX(); |
| 2395 | Mov(tmp, reinterpret_cast<uintptr_t>(printf)); |
| 2396 | Blr(tmp); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2397 | } |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2398 | } |
| 2399 | |
| 2400 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 2401 | void MacroAssembler::Printf(const char* format, |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2402 | CPURegister arg0, |
| 2403 | CPURegister arg1, |
| 2404 | CPURegister arg2, |
| 2405 | CPURegister arg3) { |
| 2406 | // We can only print sp if it is the current stack pointer. |
| 2407 | if (!sp.Is(StackPointer())) { |
| 2408 | VIXL_ASSERT(!sp.Aliases(arg0)); |
| 2409 | VIXL_ASSERT(!sp.Aliases(arg1)); |
| 2410 | VIXL_ASSERT(!sp.Aliases(arg2)); |
| 2411 | VIXL_ASSERT(!sp.Aliases(arg3)); |
| 2412 | } |
| 2413 | |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2414 | // Make sure that the macro assembler doesn't try to use any of our arguments |
| 2415 | // as scratch registers. |
| 2416 | UseScratchRegisterScope exclude_all(this); |
| 2417 | exclude_all.ExcludeAll(); |
| 2418 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2419 | // Preserve all caller-saved registers as well as NZCV. |
| 2420 | // If sp is the stack pointer, PushCPURegList asserts that the size of each |
| 2421 | // list is a multiple of 16 bytes. |
| 2422 | PushCPURegList(kCallerSaved); |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 2423 | PushCPURegList(kCallerSavedV); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2424 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 2425 | { |
| 2426 | UseScratchRegisterScope temps(this); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2427 | // We can use caller-saved registers as scratch values (except for argN). |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2428 | temps.Include(kCallerSaved); |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 2429 | temps.Include(kCallerSavedV); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2430 | temps.Exclude(arg0, arg1, arg2, arg3); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2431 | |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2432 | // If any of the arguments are the current stack pointer, allocate a new |
| 2433 | // register for them, and adjust the value to compensate for pushing the |
| 2434 | // caller-saved registers. |
| 2435 | bool arg0_sp = StackPointer().Aliases(arg0); |
| 2436 | bool arg1_sp = StackPointer().Aliases(arg1); |
| 2437 | bool arg2_sp = StackPointer().Aliases(arg2); |
| 2438 | bool arg3_sp = StackPointer().Aliases(arg3); |
| 2439 | if (arg0_sp || arg1_sp || arg2_sp || arg3_sp) { |
| 2440 | // Allocate a register to hold the original stack pointer value, to pass |
| 2441 | // to PrintfNoPreserve as an argument. |
| 2442 | Register arg_sp = temps.AcquireX(); |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 2443 | Add(arg_sp, |
| 2444 | StackPointer(), |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 2445 | kCallerSaved.TotalSizeInBytes() + kCallerSavedV.TotalSizeInBytes()); |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2446 | if (arg0_sp) arg0 = Register(arg_sp.code(), arg0.size()); |
| 2447 | if (arg1_sp) arg1 = Register(arg_sp.code(), arg1.size()); |
| 2448 | if (arg2_sp) arg2 = Register(arg_sp.code(), arg2.size()); |
| 2449 | if (arg3_sp) arg3 = Register(arg_sp.code(), arg3.size()); |
| 2450 | } |
| 2451 | |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2452 | // Preserve NZCV. |
| 2453 | Register tmp = temps.AcquireX(); |
| 2454 | Mrs(tmp, NZCV); |
| 2455 | Push(tmp, xzr); |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2456 | temps.Release(tmp); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2457 | |
| 2458 | PrintfNoPreserve(format, arg0, arg1, arg2, arg3); |
| 2459 | |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2460 | // Restore NZCV. |
| 2461 | tmp = temps.AcquireX(); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2462 | Pop(xzr, tmp); |
| 2463 | Msr(NZCV, tmp); |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2464 | temps.Release(tmp); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2465 | } |
| 2466 | |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 2467 | PopCPURegList(kCallerSavedV); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2468 | PopCPURegList(kCallerSaved); |
| 2469 | } |
| 2470 | |
| 2471 | void MacroAssembler::Trace(TraceParameters parameters, TraceCommand command) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2472 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2473 | |
armvixl | 684cd2a | 2015-10-23 13:38:33 +0100 | [diff] [blame] | 2474 | if (allow_simulator_instructions_) { |
| 2475 | // The arguments to the trace pseudo instruction need to be contiguous in |
| 2476 | // memory, so make sure we don't try to emit a literal pool. |
| 2477 | InstructionAccurateScope scope(this, kTraceLength / kInstructionSize); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2478 | |
armvixl | 684cd2a | 2015-10-23 13:38:33 +0100 | [diff] [blame] | 2479 | Label start; |
| 2480 | bind(&start); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2481 | |
armvixl | 684cd2a | 2015-10-23 13:38:33 +0100 | [diff] [blame] | 2482 | // Refer to simulator-a64.h for a description of the marker and its |
| 2483 | // arguments. |
| 2484 | hlt(kTraceOpcode); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2485 | |
armvixl | 684cd2a | 2015-10-23 13:38:33 +0100 | [diff] [blame] | 2486 | VIXL_ASSERT(SizeOfCodeGeneratedSince(&start) == kTraceParamsOffset); |
| 2487 | dc32(parameters); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2488 | |
armvixl | 684cd2a | 2015-10-23 13:38:33 +0100 | [diff] [blame] | 2489 | VIXL_ASSERT(SizeOfCodeGeneratedSince(&start) == kTraceCommandOffset); |
| 2490 | dc32(command); |
| 2491 | } else { |
| 2492 | // Emit nothing on real hardware. |
| 2493 | USE(parameters, command); |
| 2494 | } |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2495 | } |
| 2496 | |
| 2497 | |
| 2498 | void MacroAssembler::Log(TraceParameters parameters) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2499 | VIXL_ASSERT(allow_macro_instructions_); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2500 | |
armvixl | 684cd2a | 2015-10-23 13:38:33 +0100 | [diff] [blame] | 2501 | if (allow_simulator_instructions_) { |
| 2502 | // The arguments to the log pseudo instruction need to be contiguous in |
| 2503 | // memory, so make sure we don't try to emit a literal pool. |
| 2504 | InstructionAccurateScope scope(this, kLogLength / kInstructionSize); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2505 | |
armvixl | 684cd2a | 2015-10-23 13:38:33 +0100 | [diff] [blame] | 2506 | Label start; |
| 2507 | bind(&start); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2508 | |
armvixl | 684cd2a | 2015-10-23 13:38:33 +0100 | [diff] [blame] | 2509 | // Refer to simulator-a64.h for a description of the marker and its |
| 2510 | // arguments. |
| 2511 | hlt(kLogOpcode); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2512 | |
armvixl | 684cd2a | 2015-10-23 13:38:33 +0100 | [diff] [blame] | 2513 | VIXL_ASSERT(SizeOfCodeGeneratedSince(&start) == kLogParamsOffset); |
| 2514 | dc32(parameters); |
| 2515 | } else { |
| 2516 | // Emit nothing on real hardware. |
| 2517 | USE(parameters); |
| 2518 | } |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2519 | } |
| 2520 | |
armvixl | 578645f | 2013-08-15 17:21:42 +0100 | [diff] [blame] | 2521 | |
| 2522 | void MacroAssembler::EnableInstrumentation() { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2523 | VIXL_ASSERT(!isprint(InstrumentStateEnable)); |
armvixl | 578645f | 2013-08-15 17:21:42 +0100 | [diff] [blame] | 2524 | InstructionAccurateScope scope(this, 1); |
| 2525 | movn(xzr, InstrumentStateEnable); |
| 2526 | } |
| 2527 | |
| 2528 | |
| 2529 | void MacroAssembler::DisableInstrumentation() { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2530 | VIXL_ASSERT(!isprint(InstrumentStateDisable)); |
armvixl | 578645f | 2013-08-15 17:21:42 +0100 | [diff] [blame] | 2531 | InstructionAccurateScope scope(this, 1); |
| 2532 | movn(xzr, InstrumentStateDisable); |
| 2533 | } |
| 2534 | |
| 2535 | |
| 2536 | void MacroAssembler::AnnotateInstrumentation(const char* marker_name) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2537 | VIXL_ASSERT(strlen(marker_name) == 2); |
armvixl | 578645f | 2013-08-15 17:21:42 +0100 | [diff] [blame] | 2538 | |
| 2539 | // We allow only printable characters in the marker names. Unprintable |
| 2540 | // characters are reserved for controlling features of the instrumentation. |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2541 | VIXL_ASSERT(isprint(marker_name[0]) && isprint(marker_name[1])); |
armvixl | 578645f | 2013-08-15 17:21:42 +0100 | [diff] [blame] | 2542 | |
| 2543 | InstructionAccurateScope scope(this, 1); |
| 2544 | movn(xzr, (marker_name[1] << 8) | marker_name[0]); |
| 2545 | } |
| 2546 | |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2547 | |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 2548 | void UseScratchRegisterScope::Open(MacroAssembler* masm) { |
| 2549 | VIXL_ASSERT(!initialised_); |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 2550 | VIXL_ASSERT(masm != NULL); |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 2551 | available_ = masm->TmpList(); |
| 2552 | availablefp_ = masm->FPTmpList(); |
| 2553 | old_available_ = available_->list(); |
| 2554 | old_availablefp_ = availablefp_->list(); |
| 2555 | VIXL_ASSERT(available_->type() == CPURegister::kRegister); |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 2556 | VIXL_ASSERT(availablefp_->type() == CPURegister::kVRegister); |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 2557 | #ifdef VIXL_DEBUG |
| 2558 | initialised_ = true; |
| 2559 | #endif |
| 2560 | } |
| 2561 | |
| 2562 | |
| 2563 | void UseScratchRegisterScope::Close() { |
| 2564 | if (available_) { |
| 2565 | available_->set_list(old_available_); |
| 2566 | available_ = NULL; |
| 2567 | } |
| 2568 | if (availablefp_) { |
| 2569 | availablefp_->set_list(old_availablefp_); |
| 2570 | availablefp_ = NULL; |
| 2571 | } |
| 2572 | #ifdef VIXL_DEBUG |
| 2573 | initialised_ = false; |
| 2574 | #endif |
| 2575 | } |
| 2576 | |
| 2577 | |
| 2578 | UseScratchRegisterScope::UseScratchRegisterScope(MacroAssembler* masm) { |
| 2579 | #ifdef VIXL_DEBUG |
| 2580 | initialised_ = false; |
| 2581 | #endif |
| 2582 | Open(masm); |
| 2583 | } |
| 2584 | |
| 2585 | // This allows deferred (and optional) initialisation of the scope. |
| 2586 | UseScratchRegisterScope::UseScratchRegisterScope() |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 2587 | : available_(NULL), |
| 2588 | availablefp_(NULL), |
| 2589 | old_available_(0), |
| 2590 | old_availablefp_(0) { |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 2591 | #ifdef VIXL_DEBUG |
| 2592 | initialised_ = false; |
| 2593 | #endif |
| 2594 | } |
| 2595 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 2596 | UseScratchRegisterScope::~UseScratchRegisterScope() { Close(); } |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2597 | |
| 2598 | |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2599 | bool UseScratchRegisterScope::IsAvailable(const CPURegister& reg) const { |
| 2600 | return available_->IncludesAliasOf(reg) || availablefp_->IncludesAliasOf(reg); |
| 2601 | } |
| 2602 | |
| 2603 | |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2604 | Register UseScratchRegisterScope::AcquireSameSizeAs(const Register& reg) { |
| 2605 | int code = AcquireNextAvailable(available_).code(); |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 2606 | return Register(code, reg.size()); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2607 | } |
| 2608 | |
| 2609 | |
| 2610 | FPRegister UseScratchRegisterScope::AcquireSameSizeAs(const FPRegister& reg) { |
| 2611 | int code = AcquireNextAvailable(availablefp_).code(); |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 2612 | return FPRegister(code, reg.size()); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2613 | } |
| 2614 | |
| 2615 | |
| 2616 | void UseScratchRegisterScope::Release(const CPURegister& reg) { |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 2617 | VIXL_ASSERT(initialised_); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2618 | if (reg.IsRegister()) { |
| 2619 | ReleaseByCode(available_, reg.code()); |
| 2620 | } else if (reg.IsFPRegister()) { |
| 2621 | ReleaseByCode(availablefp_, reg.code()); |
| 2622 | } else { |
| 2623 | VIXL_ASSERT(reg.IsNone()); |
| 2624 | } |
| 2625 | } |
| 2626 | |
| 2627 | |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2628 | void UseScratchRegisterScope::Include(const CPURegList& list) { |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 2629 | VIXL_ASSERT(initialised_); |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2630 | if (list.type() == CPURegister::kRegister) { |
| 2631 | // Make sure that neither sp nor xzr are included the list. |
| 2632 | IncludeByRegList(available_, list.list() & ~(xzr.Bit() | sp.Bit())); |
| 2633 | } else { |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 2634 | VIXL_ASSERT(list.type() == CPURegister::kVRegister); |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2635 | IncludeByRegList(availablefp_, list.list()); |
| 2636 | } |
| 2637 | } |
| 2638 | |
| 2639 | |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2640 | void UseScratchRegisterScope::Include(const Register& reg1, |
| 2641 | const Register& reg2, |
| 2642 | const Register& reg3, |
| 2643 | const Register& reg4) { |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 2644 | VIXL_ASSERT(initialised_); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2645 | RegList include = reg1.Bit() | reg2.Bit() | reg3.Bit() | reg4.Bit(); |
| 2646 | // Make sure that neither sp nor xzr are included the list. |
| 2647 | include &= ~(xzr.Bit() | sp.Bit()); |
| 2648 | |
| 2649 | IncludeByRegList(available_, include); |
| 2650 | } |
| 2651 | |
| 2652 | |
| 2653 | void UseScratchRegisterScope::Include(const FPRegister& reg1, |
| 2654 | const FPRegister& reg2, |
| 2655 | const FPRegister& reg3, |
| 2656 | const FPRegister& reg4) { |
| 2657 | RegList include = reg1.Bit() | reg2.Bit() | reg3.Bit() | reg4.Bit(); |
| 2658 | IncludeByRegList(availablefp_, include); |
| 2659 | } |
| 2660 | |
| 2661 | |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2662 | void UseScratchRegisterScope::Exclude(const CPURegList& list) { |
| 2663 | if (list.type() == CPURegister::kRegister) { |
| 2664 | ExcludeByRegList(available_, list.list()); |
| 2665 | } else { |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 2666 | VIXL_ASSERT(list.type() == CPURegister::kVRegister); |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2667 | ExcludeByRegList(availablefp_, list.list()); |
| 2668 | } |
| 2669 | } |
| 2670 | |
| 2671 | |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 2672 | void UseScratchRegisterScope::Exclude(const Register& reg1, |
| 2673 | const Register& reg2, |
| 2674 | const Register& reg3, |
| 2675 | const Register& reg4) { |
| 2676 | RegList exclude = reg1.Bit() | reg2.Bit() | reg3.Bit() | reg4.Bit(); |
| 2677 | ExcludeByRegList(available_, exclude); |
| 2678 | } |
| 2679 | |
| 2680 | |
| 2681 | void UseScratchRegisterScope::Exclude(const FPRegister& reg1, |
| 2682 | const FPRegister& reg2, |
| 2683 | const FPRegister& reg3, |
| 2684 | const FPRegister& reg4) { |
| 2685 | RegList excludefp = reg1.Bit() | reg2.Bit() | reg3.Bit() | reg4.Bit(); |
| 2686 | ExcludeByRegList(availablefp_, excludefp); |
| 2687 | } |
| 2688 | |
| 2689 | |
| 2690 | void UseScratchRegisterScope::Exclude(const CPURegister& reg1, |
| 2691 | const CPURegister& reg2, |
| 2692 | const CPURegister& reg3, |
| 2693 | const CPURegister& reg4) { |
| 2694 | RegList exclude = 0; |
| 2695 | RegList excludefp = 0; |
| 2696 | |
| 2697 | const CPURegister regs[] = {reg1, reg2, reg3, reg4}; |
| 2698 | |
| 2699 | for (unsigned i = 0; i < (sizeof(regs) / sizeof(regs[0])); i++) { |
| 2700 | if (regs[i].IsRegister()) { |
| 2701 | exclude |= regs[i].Bit(); |
| 2702 | } else if (regs[i].IsFPRegister()) { |
| 2703 | excludefp |= regs[i].Bit(); |
| 2704 | } else { |
| 2705 | VIXL_ASSERT(regs[i].IsNone()); |
| 2706 | } |
| 2707 | } |
| 2708 | |
| 2709 | ExcludeByRegList(available_, exclude); |
| 2710 | ExcludeByRegList(availablefp_, excludefp); |
| 2711 | } |
| 2712 | |
| 2713 | |
| 2714 | void UseScratchRegisterScope::ExcludeAll() { |
| 2715 | ExcludeByRegList(available_, available_->list()); |
| 2716 | ExcludeByRegList(availablefp_, availablefp_->list()); |
| 2717 | } |
| 2718 | |
| 2719 | |
| 2720 | CPURegister UseScratchRegisterScope::AcquireNextAvailable( |
| 2721 | CPURegList* available) { |
| 2722 | VIXL_CHECK(!available->IsEmpty()); |
| 2723 | CPURegister result = available->PopLowestIndex(); |
| 2724 | VIXL_ASSERT(!AreAliased(result, xzr, sp)); |
| 2725 | return result; |
| 2726 | } |
| 2727 | |
| 2728 | |
| 2729 | void UseScratchRegisterScope::ReleaseByCode(CPURegList* available, int code) { |
| 2730 | ReleaseByRegList(available, static_cast<RegList>(1) << code); |
| 2731 | } |
| 2732 | |
| 2733 | |
| 2734 | void UseScratchRegisterScope::ReleaseByRegList(CPURegList* available, |
| 2735 | RegList regs) { |
| 2736 | available->set_list(available->list() | regs); |
| 2737 | } |
| 2738 | |
| 2739 | |
| 2740 | void UseScratchRegisterScope::IncludeByRegList(CPURegList* available, |
| 2741 | RegList regs) { |
| 2742 | available->set_list(available->list() | regs); |
| 2743 | } |
| 2744 | |
| 2745 | |
| 2746 | void UseScratchRegisterScope::ExcludeByRegList(CPURegList* available, |
| 2747 | RegList exclude) { |
| 2748 | available->set_list(available->list() & ~exclude); |
| 2749 | } |
| 2750 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2751 | } // namespace vixl |