assembler-mips64.cc 132 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
// Copyright (c) 1994-2006 Sun Microsystems Inc.
// All Rights Reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// - Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// - Redistribution in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// - Neither the name of Sun Microsystems or the names of contributors may
// be used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// The original source code covered by the above license above has been
// modified significantly by Google Inc.
// Copyright 2012 the V8 project authors. All rights reserved.

35
#include "src/codegen/mips64/assembler-mips64.h"
36

37 38 39
#if V8_TARGET_ARCH_MIPS64

#include "src/base/cpu.h"
40
#include "src/codegen/machine-type.h"
41
#include "src/codegen/mips64/assembler-mips64-inl.h"
42
#include "src/codegen/safepoint-table.h"
43
#include "src/codegen/string-constants.h"
44
#include "src/deoptimizer/deoptimizer.h"
45
#include "src/objects/heap-number-inl.h"
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

namespace v8 {
namespace internal {

// Get the CPU features enabled by the build. For cross compilation the
// preprocessor symbols CAN_USE_FPU_INSTRUCTIONS
// can be defined to enable FPU instructions when building the
// snapshot.
static unsigned CpuFeaturesImpliedByCompiler() {
  unsigned answer = 0;
#ifdef CAN_USE_FPU_INSTRUCTIONS
  answer |= 1u << FPU;
#endif  // def CAN_USE_FPU_INSTRUCTIONS

  // If the compiler is allowed to use FPU then we can use FPU too in our code
  // generation even when generating snapshots.  This won't work for cross
  // compilation.
#if defined(__mips__) && defined(__mips_hard_float) && __mips_hard_float != 0
  answer |= 1u << FPU;
#endif

  return answer;
}

70 71
bool CpuFeatures::SupportsWasmSimd128() { return IsSupported(MIPS_SIMD); }

72 73 74 75 76 77
void CpuFeatures::ProbeImpl(bool cross_compile) {
  supported_ |= CpuFeaturesImpliedByCompiler();

  // Only use statically determined features for cross compile (snapshot).
  if (cross_compile) return;

78 79
    // If the compiler is allowed to use fpu then we can use fpu too in our
    // code generation.
80 81 82
#ifndef __mips__
  // For the simulator build, use FPU.
  supported_ |= 1u << FPU;
83 84 85
#if defined(_MIPS_ARCH_MIPS64R6) && defined(_MIPS_MSA)
  supported_ |= 1u << MIPS_SIMD;
#endif
86 87 88 89
#else
  // Probe for additional features at runtime.
  base::CPU cpu;
  if (cpu.has_fpu()) supported_ |= 1u << FPU;
90 91 92
#if defined(_MIPS_MSA)
  supported_ |= 1u << MIPS_SIMD;
#else
93
  if (cpu.has_msa()) supported_ |= 1u << MIPS_SIMD;
94
#endif
95
#endif
96 97 98 99 100 101

  // Set a static value on whether Simd is supported.
  // This variable is only used for certain archs to query SupportWasmSimd128()
  // at runtime in builtins using an extern ref. Other callers should use
  // CpuFeatures::SupportWasmSimd128().
  CpuFeatures::supports_wasm_simd_128_ = CpuFeatures::SupportsWasmSimd128();
102 103
}

104 105
void CpuFeatures::PrintTarget() {}
void CpuFeatures::PrintFeatures() {}
106 107

int ToNumber(Register reg) {
108
  DCHECK(reg.is_valid());
109
  const int kNumbers[] = {
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
      0,   // zero_reg
      1,   // at
      2,   // v0
      3,   // v1
      4,   // a0
      5,   // a1
      6,   // a2
      7,   // a3
      8,   // a4
      9,   // a5
      10,  // a6
      11,  // a7
      12,  // t0
      13,  // t1
      14,  // t2
      15,  // t3
      16,  // s0
      17,  // s1
      18,  // s2
      19,  // s3
      20,  // s4
      21,  // s5
      22,  // s6
      23,  // s7
      24,  // t8
      25,  // t9
      26,  // k0
      27,  // k1
      28,  // gp
      29,  // sp
      30,  // fp
      31,  // ra
142 143 144 145 146
  };
  return kNumbers[reg.code()];
}

Register ToRegister(int num) {
147
  DCHECK(num >= 0 && num < kNumRegisters);
148
  const Register kRegisters[] = {
149 150
      zero_reg, at, v0, v1, a0, a1, a2, a3, a4, a5, a6, a7, t0, t1, t2, t3,
      s0,       s1, s2, s3, s4, s5, s6, s7, t8, t9, k0, k1, gp, sp, fp, ra};
151 152 153 154 155 156
  return kRegisters[num];
}

// -----------------------------------------------------------------------------
// Implementation of RelocInfo.

157 158 159
const int RelocInfo::kApplyMask =
    RelocInfo::ModeMask(RelocInfo::INTERNAL_REFERENCE) |
    RelocInfo::ModeMask(RelocInfo::INTERNAL_REFERENCE_ENCODED);
160 161 162 163 164 165 166 167

bool RelocInfo::IsCodedSpecially() {
  // The deserializer needs to know whether a pointer is specially coded.  Being
  // specially coded on MIPS means that it is a lui/ori instruction, and that is
  // always the case inside code objects.
  return true;
}

168
bool RelocInfo::IsInConstantPool() { return false; }
169

170 171
uint32_t RelocInfo::wasm_call_tag() const {
  DCHECK(rmode_ == WASM_CALL || rmode_ == WASM_STUB_CALL);
172 173 174 175
  return static_cast<uint32_t>(
      Assembler::target_address_at(pc_, constant_pool_));
}

176 177 178 179
// -----------------------------------------------------------------------------
// Implementation of Operand and MemOperand.
// See assembler-mips-inl.h for inlined constructors.

180
Operand::Operand(Handle<HeapObject> handle)
181
    : rm_(no_reg), rmode_(RelocInfo::FULL_EMBEDDED_OBJECT) {
182
  value_.immediate = static_cast<intptr_t>(handle.address());
183 184
}

185 186 187
Operand Operand::EmbeddedNumber(double value) {
  int32_t smi;
  if (DoubleToSmiInteger(value, &smi)) return Operand(Smi::FromInt(smi));
188
  Operand result(0, RelocInfo::FULL_EMBEDDED_OBJECT);
189 190 191 192 193
  result.is_heap_object_request_ = true;
  result.value_.heap_object_request = HeapObjectRequest(value);
  return result;
}

194
Operand Operand::EmbeddedStringConstant(const StringConstantBase* str) {
195
  Operand result(0, RelocInfo::FULL_EMBEDDED_OBJECT);
196 197 198 199 200
  result.is_heap_object_request_ = true;
  result.value_.heap_object_request = HeapObjectRequest(str);
  return result;
}

201
MemOperand::MemOperand(Register rm, int32_t offset) : Operand(rm) {
202 203 204
  offset_ = offset;
}

205 206 207
MemOperand::MemOperand(Register rm, int32_t unit, int32_t multiplier,
                       OffsetAddend offset_addend)
    : Operand(rm) {
208 209 210
  offset_ = unit * multiplier + offset_addend;
}

211
void Assembler::AllocateAndInstallRequestedHeapObjects(Isolate* isolate) {
212
  DCHECK_IMPLIES(isolate == nullptr, heap_object_requests_.empty());
213 214 215 216
  for (auto& request : heap_object_requests_) {
    Handle<HeapObject> object;
    switch (request.kind()) {
      case HeapObjectRequest::kHeapNumber:
217 218
        object = isolate->factory()->NewHeapNumber<AllocationType::kOld>(
            request.heap_number());
219
        break;
220 221 222 223 224
      case HeapObjectRequest::kStringConstant:
        const StringConstantBase* str = request.string();
        CHECK_NOT_NULL(str);
        object = str->AllocateStringConstant(isolate);
        break;
225
    }
226
    Address pc = reinterpret_cast<Address>(buffer_start_) + request.offset();
227
    set_target_value_at(pc, reinterpret_cast<uint64_t>(object.location()));
228 229
  }
}
230 231 232 233 234 235

// -----------------------------------------------------------------------------
// Specific instructions, constants, and masks.

// daddiu(sp, sp, 8) aka Pop() operation or part of Pop(r)
// operations as post-increment of sp.
236 237
const Instr kPopInstruction = DADDIU | (sp.code() << kRsShift) |
                              (sp.code() << kRtShift) |
238
                              (kPointerSize & kImm16Mask);
239
// daddiu(sp, sp, -8) part of Push(r) operation as pre-decrement of sp.
240 241
const Instr kPushInstruction = DADDIU | (sp.code() << kRsShift) |
                               (sp.code() << kRtShift) |
242
                               (-kPointerSize & kImm16Mask);
243
// Sd(r, MemOperand(sp, 0))
244
const Instr kPushRegPattern = SD | (sp.code() << kRsShift) | (0 & kImm16Mask);
245
//  Ld(r, MemOperand(sp, 0))
246
const Instr kPopRegPattern = LD | (sp.code() << kRsShift) | (0 & kImm16Mask);
247

248
const Instr kLwRegFpOffsetPattern =
249
    LW | (fp.code() << kRsShift) | (0 & kImm16Mask);
250

251
const Instr kSwRegFpOffsetPattern =
252
    SW | (fp.code() << kRsShift) | (0 & kImm16Mask);
253

254
const Instr kLwRegFpNegOffsetPattern =
255
    LW | (fp.code() << kRsShift) | (kNegOffset & kImm16Mask);
256

257
const Instr kSwRegFpNegOffsetPattern =
258
    SW | (fp.code() << kRsShift) | (kNegOffset & kImm16Mask);
259 260
// A mask for the Rt register for push, pop, lw, sw instructions.
const Instr kRtMask = kRtFieldMask;
261
const Instr kLwSwInstrTypeMask = 0xFFE00000;
262
const Instr kLwSwInstrArgumentMask = ~kLwSwInstrTypeMask;
263 264
const Instr kLwSwOffsetMask = kImm16Mask;

265 266 267
Assembler::Assembler(const AssemblerOptions& options,
                     std::unique_ptr<AssemblerBuffer> buffer)
    : AssemblerBase(options, std::move(buffer)),
268
      scratch_register_list_(at.bit() | s0.bit()) {
269 270 271
  if (CpuFeatures::IsSupported(MIPS_SIMD)) {
    EnableCpuFeature(MIPS_SIMD);
  }
272
  reloc_info_writer.Reposition(buffer_start_ + buffer_->size(), pc_);
273 274 275 276 277 278 279

  last_trampoline_pool_end_ = 0;
  no_trampoline_pool_before_ = 0;
  trampoline_pool_blocked_nesting_ = 0;
  // We leave space (16 * kTrampolineSlotsSize)
  // for BlockTrampolinePoolScope buffer.
  next_buffer_check_ = FLAG_force_long_branches
280 281
                           ? kMaxInt
                           : kMaxBranchOffset - kTrampolineSlotsSize * 16;
282 283 284 285 286 287 288 289
  internal_trampoline_exception_ = false;
  last_bound_pos_ = 0;

  trampoline_emitted_ = FLAG_force_long_branches;
  unbound_labels_count_ = 0;
  block_buffer_growth_ = false;
}

290 291 292
void Assembler::GetCode(Isolate* isolate, CodeDesc* desc,
                        SafepointTableBuilder* safepoint_table_builder,
                        int handler_table_offset) {
293 294 295 296 297 298 299 300 301
  // As a crutch to avoid having to add manual Align calls wherever we use a
  // raw workflow to create Code objects (mostly in tests), add another Align
  // call here. It does no harm - the end of the Code object is aligned to the
  // (larger) kCodeAlignment anyways.
  // TODO(jgruber): Consider moving responsibility for proper alignment to
  // metadata table builders (safepoint, handler, constant pool, code
  // comments).
  DataAlign(Code::kMetadataAlignment);

302
  EmitForbiddenSlotInstruction();
303 304 305

  int code_comments_size = WriteCodeComments();

306
  DCHECK(pc_ <= reloc_info_writer.pos());  // No overlap.
307 308 309

  AllocateAndInstallRequestedHeapObjects(isolate);

310
  // Set up code descriptor.
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
  // TODO(jgruber): Reconsider how these offsets and sizes are maintained up to
  // this point to make CodeDesc initialization less fiddly.

  static constexpr int kConstantPoolSize = 0;
  const int instruction_size = pc_offset();
  const int code_comments_offset = instruction_size - code_comments_size;
  const int constant_pool_offset = code_comments_offset - kConstantPoolSize;
  const int handler_table_offset2 = (handler_table_offset == kNoHandlerTable)
                                        ? constant_pool_offset
                                        : handler_table_offset;
  const int safepoint_table_offset =
      (safepoint_table_builder == kNoSafepointTable)
          ? handler_table_offset2
          : safepoint_table_builder->GetCodeOffset();
  const int reloc_info_offset =
      static_cast<int>(reloc_info_writer.pos() - buffer_->start());
  CodeDesc::Initialize(desc, this, safepoint_table_offset,
                       handler_table_offset2, constant_pool_offset,
                       code_comments_offset, reloc_info_offset);
330 331 332
}

void Assembler::Align(int m) {
333
  DCHECK(m >= 4 && base::bits::IsPowerOfTwo(m));
334
  EmitForbiddenSlotInstruction();
335 336 337 338 339 340 341 342 343 344 345 346
  while ((pc_offset() & (m - 1)) != 0) {
    nop();
  }
}

void Assembler::CodeTargetAlign() {
  // No advantage to aligning branch/call targets to more than
  // single instruction, that I am aware of.
  Align(4);
}

Register Assembler::GetRtReg(Instr instr) {
347
  return Register::from_code((instr & kRtFieldMask) >> kRtShift);
348 349 350
}

Register Assembler::GetRsReg(Instr instr) {
351
  return Register::from_code((instr & kRsFieldMask) >> kRsShift);
352 353 354
}

Register Assembler::GetRdReg(Instr instr) {
355
  return Register::from_code((instr & kRdFieldMask) >> kRdShift);
356 357 358 359 360 361
}

uint32_t Assembler::GetRt(Instr instr) {
  return (instr & kRtFieldMask) >> kRtShift;
}

362
uint32_t Assembler::GetRtField(Instr instr) { return instr & kRtFieldMask; }
363 364 365 366 367

uint32_t Assembler::GetRs(Instr instr) {
  return (instr & kRsFieldMask) >> kRsShift;
}

368
uint32_t Assembler::GetRsField(Instr instr) { return instr & kRsFieldMask; }
369 370

uint32_t Assembler::GetRd(Instr instr) {
371
  return (instr & kRdFieldMask) >> kRdShift;
372 373
}

374
uint32_t Assembler::GetRdField(Instr instr) { return instr & kRdFieldMask; }
375 376 377 378 379

uint32_t Assembler::GetSa(Instr instr) {
  return (instr & kSaFieldMask) >> kSaShift;
}

380
uint32_t Assembler::GetSaField(Instr instr) { return instr & kSaFieldMask; }
381

382
uint32_t Assembler::GetOpcodeField(Instr instr) { return instr & kOpcodeMask; }
383 384 385 386 387 388 389 390 391

uint32_t Assembler::GetFunction(Instr instr) {
  return (instr & kFunctionFieldMask) >> kFunctionShift;
}

uint32_t Assembler::GetFunctionField(Instr instr) {
  return instr & kFunctionFieldMask;
}

392
uint32_t Assembler::GetImmediate16(Instr instr) { return instr & kImm16Mask; }
393

394
uint32_t Assembler::GetLabelConst(Instr instr) { return instr & ~kImm16Mask; }
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440

bool Assembler::IsPop(Instr instr) {
  return (instr & ~kRtMask) == kPopRegPattern;
}

bool Assembler::IsPush(Instr instr) {
  return (instr & ~kRtMask) == kPushRegPattern;
}

bool Assembler::IsSwRegFpOffset(Instr instr) {
  return ((instr & kLwSwInstrTypeMask) == kSwRegFpOffsetPattern);
}

bool Assembler::IsLwRegFpOffset(Instr instr) {
  return ((instr & kLwSwInstrTypeMask) == kLwRegFpOffsetPattern);
}

bool Assembler::IsSwRegFpNegOffset(Instr instr) {
  return ((instr & (kLwSwInstrTypeMask | kNegOffset)) ==
          kSwRegFpNegOffsetPattern);
}

bool Assembler::IsLwRegFpNegOffset(Instr instr) {
  return ((instr & (kLwSwInstrTypeMask | kNegOffset)) ==
          kLwRegFpNegOffsetPattern);
}

// Labels refer to positions in the (to be) generated code.
// There are bound, linked, and unused labels.
//
// Bound labels refer to known positions in the already
// generated code. pos() is the position the label refers to.
//
// Linked labels refer to unknown positions in the code
// to be generated; pos() is the position of the last
// instruction using the label.

// The link chain is terminated by a value in the instruction of -1,
// which is an otherwise illegal value (branch -1 is inf loop).
// The instruction 16-bit offset field addresses 32-bit words, but in
// code is conv to an 18-bit value addressing bytes, hence the -4 value.

const int kEndOfChain = -4;
// Determines the end of the Jump chain (a subset of the label link chain).
const int kEndOfJumpChain = 0;

441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
bool Assembler::IsMsaBranch(Instr instr) {
  uint32_t opcode = GetOpcodeField(instr);
  uint32_t rs_field = GetRsField(instr);
  if (opcode == COP1) {
    switch (rs_field) {
      case BZ_V:
      case BZ_B:
      case BZ_H:
      case BZ_W:
      case BZ_D:
      case BNZ_V:
      case BNZ_B:
      case BNZ_H:
      case BNZ_W:
      case BNZ_D:
        return true;
      default:
        return false;
    }
  } else {
    return false;
  }
}
464 465

bool Assembler::IsBranch(Instr instr) {
466
  uint32_t opcode = GetOpcodeField(instr);
467 468 469
  uint32_t rt_field = GetRtField(instr);
  uint32_t rs_field = GetRsField(instr);
  // Checks if the instruction is a branch.
470 471 472
  bool isBranch =
      opcode == BEQ || opcode == BNE || opcode == BLEZ || opcode == BGTZ ||
      opcode == BEQL || opcode == BNEL || opcode == BLEZL || opcode == BGTZL ||
473 474
      (opcode == REGIMM && (rt_field == BLTZ || rt_field == BGEZ ||
                            rt_field == BLTZAL || rt_field == BGEZAL)) ||
475 476
      (opcode == COP1 && rs_field == BC1) ||  // Coprocessor branch.
      (opcode == COP1 && rs_field == BC1EQZ) ||
477
      (opcode == COP1 && rs_field == BC1NEZ) || IsMsaBranch(instr);
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
  if (!isBranch && kArchVariant == kMips64r6) {
    // All the 3 variants of POP10 (BOVC, BEQC, BEQZALC) and
    // POP30 (BNVC, BNEC, BNEZALC) are branch ops.
    isBranch |= opcode == POP10 || opcode == POP30 || opcode == BC ||
                opcode == BALC ||
                (opcode == POP66 && rs_field != 0) ||  // BEQZC
                (opcode == POP76 && rs_field != 0);    // BNEZC
  }
  return isBranch;
}

bool Assembler::IsBc(Instr instr) {
  uint32_t opcode = GetOpcodeField(instr);
  // Checks if the instruction is a BC or BALC.
  return opcode == BC || opcode == BALC;
}

495
bool Assembler::IsNal(Instr instr) {
496 497 498
  uint32_t opcode = GetOpcodeField(instr);
  uint32_t rt_field = GetRtField(instr);
  uint32_t rs_field = GetRsField(instr);
499
  return opcode == REGIMM && rt_field == BLTZAL && rs_field == 0;
500
}
501 502 503 504 505 506

bool Assembler::IsBzc(Instr instr) {
  uint32_t opcode = GetOpcodeField(instr);
  // Checks if the instruction is BEQZC or BNEZC.
  return (opcode == POP66 && GetRsField(instr) != 0) ||
         (opcode == POP76 && GetRsField(instr) != 0);
507 508 509 510 511 512 513
}

bool Assembler::IsEmittedConstant(Instr instr) {
  uint32_t label_constant = GetLabelConst(instr);
  return label_constant == 0;  // Emitted label const in reg-exp engine.
}

514
bool Assembler::IsBeq(Instr instr) { return GetOpcodeField(instr) == BEQ; }
515

516
bool Assembler::IsBne(Instr instr) { return GetOpcodeField(instr) == BNE; }
517

518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
bool Assembler::IsBeqzc(Instr instr) {
  uint32_t opcode = GetOpcodeField(instr);
  return opcode == POP66 && GetRsField(instr) != 0;
}

bool Assembler::IsBnezc(Instr instr) {
  uint32_t opcode = GetOpcodeField(instr);
  return opcode == POP76 && GetRsField(instr) != 0;
}

bool Assembler::IsBeqc(Instr instr) {
  uint32_t opcode = GetOpcodeField(instr);
  uint32_t rs = GetRsField(instr);
  uint32_t rt = GetRtField(instr);
  return opcode == POP10 && rs != 0 && rs < rt;  // && rt != 0
}

bool Assembler::IsBnec(Instr instr) {
  uint32_t opcode = GetOpcodeField(instr);
  uint32_t rs = GetRsField(instr);
  uint32_t rt = GetRtField(instr);
  return opcode == POP30 && rs != 0 && rs < rt;  // && rt != 0
}

542 543 544 545 546 547 548 549 550 551 552 553 554
bool Assembler::IsMov(Instr instr, Register rd, Register rs) {
  uint32_t opcode = GetOpcodeField(instr);
  uint32_t rd_field = GetRd(instr);
  uint32_t rs_field = GetRs(instr);
  uint32_t rt_field = GetRt(instr);
  uint32_t rd_reg = static_cast<uint32_t>(rd.code());
  uint32_t rs_reg = static_cast<uint32_t>(rs.code());
  uint32_t function_field = GetFunctionField(instr);
  // Checks if the instruction is a OR with zero_reg argument (aka MOV).
  bool res = opcode == SPECIAL && function_field == OR && rd_field == rd_reg &&
             rs_field == rs_reg && rt_field == 0;
  return res;
}
555

556
bool Assembler::IsJump(Instr instr) {
557
  uint32_t opcode = GetOpcodeField(instr);
558 559 560 561 562
  uint32_t rt_field = GetRtField(instr);
  uint32_t rd_field = GetRdField(instr);
  uint32_t function_field = GetFunctionField(instr);
  // Checks if the instruction is a jump.
  return opcode == J || opcode == JAL ||
563 564 565
         (opcode == SPECIAL && rt_field == 0 &&
          ((function_field == JALR) ||
           (rd_field == 0 && (function_field == JR))));
566 567 568 569 570 571 572 573
}

bool Assembler::IsJ(Instr instr) {
  uint32_t opcode = GetOpcodeField(instr);
  // Checks if the instruction is a jump.
  return opcode == J;
}

574
bool Assembler::IsJal(Instr instr) { return GetOpcodeField(instr) == JAL; }
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597

bool Assembler::IsJr(Instr instr) {
  return GetOpcodeField(instr) == SPECIAL && GetFunctionField(instr) == JR;
}

bool Assembler::IsJalr(Instr instr) {
  return GetOpcodeField(instr) == SPECIAL && GetFunctionField(instr) == JALR;
}

bool Assembler::IsLui(Instr instr) {
  uint32_t opcode = GetOpcodeField(instr);
  // Checks if the instruction is a load upper immediate.
  return opcode == LUI;
}

bool Assembler::IsOri(Instr instr) {
  uint32_t opcode = GetOpcodeField(instr);
  // Checks if the instruction is a load upper immediate.
  return opcode == ORI;
}

bool Assembler::IsNop(Instr instr, unsigned int type) {
  // See Assembler::nop(type).
598
  DCHECK_LT(type, 32);
599 600 601 602 603 604 605 606 607 608 609 610 611 612
  uint32_t opcode = GetOpcodeField(instr);
  uint32_t function = GetFunctionField(instr);
  uint32_t rt = GetRt(instr);
  uint32_t rd = GetRd(instr);
  uint32_t sa = GetSa(instr);

  // Traditional mips nop == sll(zero_reg, zero_reg, 0)
  // When marking non-zero type, use sll(zero_reg, at, type)
  // to avoid use of mips ssnop and ehb special encodings
  // of the sll instruction.

  Register nop_rt_reg = (type == 0) ? zero_reg : at;
  bool ret = (opcode == SPECIAL && function == SLL &&
              rd == static_cast<uint32_t>(ToNumber(zero_reg)) &&
613
              rt == static_cast<uint32_t>(ToNumber(nop_rt_reg)) && sa == type);
614 615 616 617 618

  return ret;
}

int32_t Assembler::GetBranchOffset(Instr instr) {
619
  DCHECK(IsBranch(instr));
620 621 622 623
  return (static_cast<int16_t>(instr & kImm16Mask)) << 2;
}

bool Assembler::IsLw(Instr instr) {
624
  return (static_cast<uint32_t>(instr & kOpcodeMask) == LW);
625 626 627
}

int16_t Assembler::GetLwOffset(Instr instr) {
628
  DCHECK(IsLw(instr));
629 630 631 632
  return ((instr & kImm16Mask));
}

Instr Assembler::SetLwOffset(Instr instr, int16_t offset) {
633
  DCHECK(IsLw(instr));
634 635

  // We actually create a new lw instruction based on the original one.
636 637
  Instr temp_instr = LW | (instr & kRsFieldMask) | (instr & kRtFieldMask) |
                     (offset & kImm16Mask);
638 639 640 641 642

  return temp_instr;
}

bool Assembler::IsSw(Instr instr) {
643
  return (static_cast<uint32_t>(instr & kOpcodeMask) == SW);
644 645 646
}

Instr Assembler::SetSwOffset(Instr instr, int16_t offset) {
647
  DCHECK(IsSw(instr));
648 649 650 651 652 653 654 655
  return ((instr & ~kImm16Mask) | (offset & kImm16Mask));
}

bool Assembler::IsAddImmediate(Instr instr) {
  return ((instr & kOpcodeMask) == ADDIU || (instr & kOpcodeMask) == DADDIU);
}

Instr Assembler::SetAddImmediateOffset(Instr instr, int16_t offset) {
656
  DCHECK(IsAddImmediate(instr));
657 658 659 660 661 662 663
  return ((instr & ~kImm16Mask) | (offset & kImm16Mask));
}

bool Assembler::IsAndImmediate(Instr instr) {
  return GetOpcodeField(instr) == ANDI;
}

664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
static Assembler::OffsetSize OffsetSizeInBits(Instr instr) {
  if (kArchVariant == kMips64r6) {
    if (Assembler::IsBc(instr)) {
      return Assembler::OffsetSize::kOffset26;
    } else if (Assembler::IsBzc(instr)) {
      return Assembler::OffsetSize::kOffset21;
    }
  }
  return Assembler::OffsetSize::kOffset16;
}

static inline int32_t AddBranchOffset(int pos, Instr instr) {
  int bits = OffsetSizeInBits(instr);
  const int32_t mask = (1 << bits) - 1;
  bits = 32 - bits;

  // Do NOT change this to <<2. We rely on arithmetic shifts here, assuming
  // the compiler uses arithmetic shifts for signed integers.
  int32_t imm = ((instr & mask) << bits) >> (bits - 2);

  if (imm == kEndOfChain) {
    // EndOfChain sentinel is returned directly, not relative to pc or pos.
    return kEndOfChain;
  } else {
    return pos + Assembler::kBranchPCOffset + imm;
  }
}

692
int Assembler::target_at(int pos, bool is_internal) {
693
  if (is_internal) {
694
    int64_t* p = reinterpret_cast<int64_t*>(buffer_start_ + pos);
695 696 697 698 699
    int64_t address = *p;
    if (address == kEndOfJumpChain) {
      return kEndOfChain;
    } else {
      int64_t instr_address = reinterpret_cast<int64_t>(p);
700 701
      DCHECK(instr_address - address < INT_MAX);
      int delta = static_cast<int>(instr_address - address);
702 703 704 705
      DCHECK(pos > delta);
      return pos - delta;
    }
  }
706 707 708 709
  Instr instr = instr_at(pos);
  if ((instr & ~kImm16Mask) == 0) {
    // Emitted label constant, not part of a branch.
    if (instr == 0) {
710 711 712 713 714
      return kEndOfChain;
    } else {
      int32_t imm18 = ((instr & static_cast<int32_t>(kImm16Mask)) << 16) >> 14;
      return (imm18 + pos);
    }
715 716
  }
  // Check we have a branch or jump instruction.
717 718
  DCHECK(IsBranch(instr) || IsJ(instr) || IsJal(instr) || IsLui(instr) ||
         IsMov(instr, t8, ra));
719 720 721
  // Do NOT change this to <<2. We rely on arithmetic shifts here, assuming
  // the compiler uses arithmetic shifts for signed integers.
  if (IsBranch(instr)) {
722
    return AddBranchOffset(pos, instr);
723 724
  } else if (IsMov(instr, t8, ra)) {
    int32_t imm32;
725 726
    Instr instr_lui = instr_at(pos + 2 * kInstrSize);
    Instr instr_ori = instr_at(pos + 3 * kInstrSize);
727
    DCHECK(IsLui(instr_lui));
728
    DCHECK(IsOri(instr_ori));
729 730 731
    imm32 = (instr_lui & static_cast<int32_t>(kImm16Mask)) << kLuiShift;
    imm32 |= (instr_ori & static_cast<int32_t>(kImm16Mask));
    if (imm32 == kEndOfJumpChain) {
732 733
      // EndOfChain sentinel is returned directly, not relative to pc or pos.
      return kEndOfChain;
734 735 736
    }
    return pos + Assembler::kLongBranchPCOffset + imm32;
  } else if (IsLui(instr)) {
737
    if (IsNal(instr_at(pos + kInstrSize))) {
738
      int32_t imm32;
739 740
      Instr instr_lui = instr_at(pos + 0 * kInstrSize);
      Instr instr_ori = instr_at(pos + 2 * kInstrSize);
741 742 743 744 745 746 747 748 749
      DCHECK(IsLui(instr_lui));
      DCHECK(IsOri(instr_ori));
      imm32 = (instr_lui & static_cast<int32_t>(kImm16Mask)) << kLuiShift;
      imm32 |= (instr_ori & static_cast<int32_t>(kImm16Mask));
      if (imm32 == kEndOfJumpChain) {
        // EndOfChain sentinel is returned directly, not relative to pc or pos.
        return kEndOfChain;
      }
      return pos + Assembler::kLongBranchPCOffset + imm32;
750
    } else {
751 752 753
      Instr instr_lui = instr_at(pos + 0 * kInstrSize);
      Instr instr_ori = instr_at(pos + 1 * kInstrSize);
      Instr instr_ori2 = instr_at(pos + 3 * kInstrSize);
754 755 756 757 758 759 760 761 762 763 764 765 766 767
      DCHECK(IsOri(instr_ori));
      DCHECK(IsOri(instr_ori2));

      // TODO(plind) create named constants for shift values.
      int64_t imm = static_cast<int64_t>(instr_lui & kImm16Mask) << 48;
      imm |= static_cast<int64_t>(instr_ori & kImm16Mask) << 32;
      imm |= static_cast<int64_t>(instr_ori2 & kImm16Mask) << 16;
      // Sign extend address;
      imm >>= 16;

      if (imm == kEndOfJumpChain) {
        // EndOfChain sentinel is returned directly, not relative to pc or pos.
        return kEndOfChain;
      } else {
768
        uint64_t instr_address = reinterpret_cast<int64_t>(buffer_start_ + pos);
769 770 771 772 773
        DCHECK(instr_address - imm < INT_MAX);
        int delta = static_cast<int>(instr_address - imm);
        DCHECK(pos > delta);
        return pos - delta;
      }
774 775
    }
  } else {
776 777 778 779 780 781
    DCHECK(IsJ(instr) || IsJal(instr));
    int32_t imm28 = (instr & static_cast<int32_t>(kImm26Mask)) << 2;
    if (imm28 == kEndOfJumpChain) {
      // EndOfChain sentinel is returned directly, not relative to pc or pos.
      return kEndOfChain;
    } else {
782 783 784
      // Sign extend 28-bit offset.
      int32_t delta = static_cast<int32_t>((imm28 << 4) >> 4);
      return pos + delta;
785
    }
786 787 788
  }
}

789 790 791 792
static inline Instr SetBranchOffset(int32_t pos, int32_t target_pos,
                                    Instr instr) {
  int32_t bits = OffsetSizeInBits(instr);
  int32_t imm = target_pos - (pos + Assembler::kBranchPCOffset);
793
  DCHECK_EQ(imm & 3, 0);
794 795 796 797 798 799 800 801 802
  imm >>= 2;

  const int32_t mask = (1 << bits) - 1;
  instr &= ~mask;
  DCHECK(is_intn(imm, bits));

  return instr | (imm & mask);
}

803
void Assembler::target_at_put(int pos, int target_pos, bool is_internal) {
804
  if (is_internal) {
805 806
    uint64_t imm = reinterpret_cast<uint64_t>(buffer_start_) + target_pos;
    *reinterpret_cast<uint64_t*>(buffer_start_ + pos) = imm;
807 808
    return;
  }
809 810
  Instr instr = instr_at(pos);
  if ((instr & ~kImm16Mask) == 0) {
811
    DCHECK(target_pos == kEndOfChain || target_pos >= 0);
812
    // Emitted label constant, not part of a branch.
813
    // Make label relative to Code pointer of generated Code object.
814 815 816 817 818
    instr_at_put(pos, target_pos + (Code::kHeaderSize - kHeapObjectTag));
    return;
  }

  if (IsBranch(instr)) {
819 820
    instr = SetBranchOffset(pos, target_pos, instr);
    instr_at_put(pos, instr);
821
  } else if (IsLui(instr)) {
822
    if (IsNal(instr_at(pos + kInstrSize))) {
823 824
      Instr instr_lui = instr_at(pos + 0 * kInstrSize);
      Instr instr_ori = instr_at(pos + 2 * kInstrSize);
825 826 827 828
      DCHECK(IsLui(instr_lui));
      DCHECK(IsOri(instr_ori));
      int32_t imm = target_pos - (pos + Assembler::kLongBranchPCOffset);
      DCHECK_EQ(imm & 3, 0);
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
      if (is_int16(imm + Assembler::kLongBranchPCOffset -
                   Assembler::kBranchPCOffset)) {
        // Optimize by converting to regular branch and link with 16-bit
        // offset.
        Instr instr_b = REGIMM | BGEZAL;  // Branch and link.
        instr_b = SetBranchOffset(pos, target_pos, instr_b);
        // Correct ra register to point to one instruction after jalr from
        // TurboAssembler::BranchAndLinkLong.
        Instr instr_a = DADDIU | ra.code() << kRsShift | ra.code() << kRtShift |
                        kOptimizedBranchAndLinkLongReturnOffset;

        instr_at_put(pos, instr_b);
        instr_at_put(pos + 1 * kInstrSize, instr_a);
      } else {
        instr_lui &= ~kImm16Mask;
        instr_ori &= ~kImm16Mask;
845

846 847 848 849
        instr_at_put(pos + 0 * kInstrSize,
                     instr_lui | ((imm >> kLuiShift) & kImm16Mask));
        instr_at_put(pos + 2 * kInstrSize, instr_ori | (imm & kImm16Mask));
      }
850
    } else {
851 852 853
      Instr instr_lui = instr_at(pos + 0 * kInstrSize);
      Instr instr_ori = instr_at(pos + 1 * kInstrSize);
      Instr instr_ori2 = instr_at(pos + 3 * kInstrSize);
854 855
      DCHECK(IsOri(instr_ori));
      DCHECK(IsOri(instr_ori2));
856

857
      uint64_t imm = reinterpret_cast<uint64_t>(buffer_start_) + target_pos;
858 859 860 861 862 863
      DCHECK_EQ(imm & 3, 0);

      instr_lui &= ~kImm16Mask;
      instr_ori &= ~kImm16Mask;
      instr_ori2 &= ~kImm16Mask;

864
      instr_at_put(pos + 0 * kInstrSize,
865
                   instr_lui | ((imm >> 32) & kImm16Mask));
866
      instr_at_put(pos + 1 * kInstrSize,
867
                   instr_ori | ((imm >> 16) & kImm16Mask));
868
      instr_at_put(pos + 3 * kInstrSize, instr_ori2 | (imm & kImm16Mask));
869
    }
870
  } else if (IsMov(instr, t8, ra)) {
871 872
    Instr instr_lui = instr_at(pos + 2 * kInstrSize);
    Instr instr_ori = instr_at(pos + 3 * kInstrSize);
873 874 875 876 877 878 879 880 881 882 883
    DCHECK(IsLui(instr_lui));
    DCHECK(IsOri(instr_ori));

    int32_t imm_short = target_pos - (pos + Assembler::kBranchPCOffset);

    if (is_int16(imm_short)) {
      // Optimize by converting to regular branch with 16-bit
      // offset
      Instr instr_b = BEQ;
      instr_b = SetBranchOffset(pos, target_pos, instr_b);

884 885 886 887
      Instr instr_j = instr_at(pos + 5 * kInstrSize);
      Instr instr_branch_delay;

      if (IsJump(instr_j)) {
888 889
        // Case when branch delay slot is protected.
        instr_branch_delay = nopInstr;
890
      } else {
891
        // Case when branch delay slot is used.
892 893
        instr_branch_delay = instr_at(pos + 7 * kInstrSize);
      }
894
      instr_at_put(pos, instr_b);
895
      instr_at_put(pos + 1 * kInstrSize, instr_branch_delay);
896 897 898 899 900 901 902
    } else {
      int32_t imm = target_pos - (pos + Assembler::kLongBranchPCOffset);
      DCHECK_EQ(imm & 3, 0);

      instr_lui &= ~kImm16Mask;
      instr_ori &= ~kImm16Mask;

903
      instr_at_put(pos + 2 * kInstrSize,
904
                   instr_lui | ((imm >> kLuiShift) & kImm16Mask));
905
      instr_at_put(pos + 3 * kInstrSize, instr_ori | (imm & kImm16Mask));
906
    }
907 908
  } else if (IsJ(instr) || IsJal(instr)) {
    int32_t imm28 = target_pos - pos;
909
    DCHECK_EQ(imm28 & 3, 0);
910

911
    uint32_t imm26 = static_cast<uint32_t>(imm28 >> 2);
912
    DCHECK(is_uint26(imm26));
913 914 915 916 917 918
    // Place 26-bit signed offset with markings.
    // When code is committed it will be resolved to j/jal.
    int32_t mark = IsJ(instr) ? kJRawMark : kJalRawMark;
    instr_at_put(pos, mark | (imm26 & kImm26Mask));
  } else {
    int32_t imm28 = target_pos - pos;
919
    DCHECK_EQ(imm28 & 3, 0);
920

921 922 923 924 925
    uint32_t imm26 = static_cast<uint32_t>(imm28 >> 2);
    DCHECK(is_uint26(imm26));
    // Place raw 26-bit signed offset.
    // When code is committed it will be resolved to j/jal.
    instr &= ~kImm26Mask;
926
    instr_at_put(pos, instr | (imm26 & kImm26Mask));
927 928 929
  }
}

930
void Assembler::print(const Label* L) {
931 932 933 934 935
  if (L->is_unused()) {
    PrintF("unused label\n");
  } else if (L->is_bound()) {
    PrintF("bound label to %d\n", L->pos());
  } else if (L->is_linked()) {
936 937
    Label l;
    l.link_to(L->pos());
938 939 940 941 942 943 944 945 946
    PrintF("unbound label");
    while (l.is_linked()) {
      PrintF("@ %d ", l.pos());
      Instr instr = instr_at(l.pos());
      if ((instr & ~kImm16Mask) == 0) {
        PrintF("value\n");
      } else {
        PrintF("%d\n", instr);
      }
947
      next(&l, is_internal_reference(&l));
948 949 950 951 952 953 954
    }
  } else {
    PrintF("label in inconsistent state (pos = %d)\n", L->pos_);
  }
}

void Assembler::bind_to(Label* L, int pos) {
955
  DCHECK(0 <= pos && pos <= pc_offset());  // Must have valid binding position.
956
  int trampoline_pos = kInvalidSlotPos;
957
  bool is_internal = false;
958 959
  if (L->is_linked() && !trampoline_emitted_) {
    unbound_labels_count_--;
960 961 962
    if (!is_internal_reference(L)) {
      next_buffer_check_ += kTrampolineSlotsSize;
    }
963 964 965
  }

  while (L->is_linked()) {
966 967
    int fixup_pos = L->pos();
    int dist = pos - fixup_pos;
968
    is_internal = is_internal_reference(L);
969 970
    next(L, is_internal);  // Call next before overwriting link with target at
                           // fixup_pos.
971
    Instr instr = instr_at(fixup_pos);
972 973
    if (is_internal) {
      target_at_put(fixup_pos, pos, is_internal);
974 975 976 977 978 979
    } else {
      if (IsBranch(instr)) {
        int branch_offset = BranchOffset(instr);
        if (dist > branch_offset) {
          if (trampoline_pos == kInvalidSlotPos) {
            trampoline_pos = get_trampoline_entry(fixup_pos);
980
            CHECK_NE(trampoline_pos, kInvalidSlotPos);
981 982 983 984
          }
          CHECK((trampoline_pos - fixup_pos) <= branch_offset);
          target_at_put(fixup_pos, trampoline_pos, false);
          fixup_pos = trampoline_pos;
985
        }
986 987 988
        target_at_put(fixup_pos, pos, false);
      } else {
        DCHECK(IsJ(instr) || IsJal(instr) || IsLui(instr) ||
989
               IsEmittedConstant(instr) || IsMov(instr, t8, ra));
990
        target_at_put(fixup_pos, pos, false);
991 992 993 994 995 996 997
      }
    }
  }
  L->bind_to(pos);

  // Keep track of the last bound label so we don't eliminate any instructions
  // before a bound label.
998
  if (pos > last_bound_pos_) last_bound_pos_ = pos;
999 1000 1001
}

void Assembler::bind(Label* L) {
1002
  DCHECK(!L->is_bound());  // Label can only be bound once.
1003 1004 1005
  bind_to(L, pc_offset());
}

1006
void Assembler::next(Label* L, bool is_internal) {
1007
  DCHECK(L->is_linked());
1008
  int link = target_at(L->pos(), is_internal);
1009 1010 1011
  if (link == kEndOfChain) {
    L->Unuse();
  } else {
1012
    DCHECK_GE(link, 0);
1013 1014 1015 1016 1017
    L->link_to(link);
  }
}

bool Assembler::is_near(Label* L) {
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
  DCHECK(L->is_bound());
  return pc_offset() - L->pos() < kMaxBranchOffset - 4 * kInstrSize;
}

bool Assembler::is_near(Label* L, OffsetSize bits) {
  if (L == nullptr || !L->is_bound()) return true;
  return ((pc_offset() - L->pos()) <
          (1 << (bits + 2 - 1)) - 1 - 5 * kInstrSize);
}

bool Assembler::is_near_branch(Label* L) {
  DCHECK(L->is_bound());
  return kArchVariant == kMips64r6 ? is_near_r6(L) : is_near_pre_r6(L);
}

int Assembler::BranchOffset(Instr instr) {
  // At pre-R6 and for other R6 branches the offset is 16 bits.
  int bits = OffsetSize::kOffset16;

  if (kArchVariant == kMips64r6) {
    uint32_t opcode = GetOpcodeField(instr);
    switch (opcode) {
      // Checks BC or BALC.
      case BC:
      case BALC:
        bits = OffsetSize::kOffset26;
        break;

      // Checks BEQZC or BNEZC.
      case POP66:
      case POP76:
        if (GetRsField(instr) != 0) bits = OffsetSize::kOffset21;
        break;
      default:
        break;
    }
1054
  }
1055 1056

  return (1 << (bits + 2 - 1)) - 1;
1057 1058 1059 1060 1061 1062 1063
}

// We have to use a temporary register for things that can be relocated even
// if they can be encoded in the MIPS's 16 bits of immediate-offset instruction
// space.  There is no guarantee that the relocated location can be similarly
// encoded.
bool Assembler::MustUseReg(RelocInfo::Mode rmode) {
1064
  return !RelocInfo::IsNoInfo(rmode);
1065 1066
}

1067 1068
void Assembler::GenInstrRegister(Opcode opcode, Register rs, Register rt,
                                 Register rd, uint16_t sa,
1069
                                 SecondaryField func) {
1070
  DCHECK(rd.is_valid() && rs.is_valid() && rt.is_valid() && is_uint5(sa));
1071 1072
  Instr instr = opcode | (rs.code() << kRsShift) | (rt.code() << kRtShift) |
                (rd.code() << kRdShift) | (sa << kSaShift) | func;
1073 1074 1075
  emit(instr);
}

1076 1077
void Assembler::GenInstrRegister(Opcode opcode, Register rs, Register rt,
                                 uint16_t msb, uint16_t lsb,
1078
                                 SecondaryField func) {
1079
  DCHECK(rs.is_valid() && rt.is_valid() && is_uint5(msb) && is_uint5(lsb));
1080 1081
  Instr instr = opcode | (rs.code() << kRsShift) | (rt.code() << kRtShift) |
                (msb << kRdShift) | (lsb << kSaShift) | func;
1082 1083 1084
  emit(instr);
}

1085 1086
void Assembler::GenInstrRegister(Opcode opcode, SecondaryField fmt,
                                 FPURegister ft, FPURegister fs, FPURegister fd,
1087
                                 SecondaryField func) {
1088
  DCHECK(fd.is_valid() && fs.is_valid() && ft.is_valid());
1089 1090
  Instr instr = opcode | fmt | (ft.code() << kFtShift) |
                (fs.code() << kFsShift) | (fd.code() << kFdShift) | func;
1091 1092 1093
  emit(instr);
}

1094 1095
void Assembler::GenInstrRegister(Opcode opcode, FPURegister fr, FPURegister ft,
                                 FPURegister fs, FPURegister fd,
1096
                                 SecondaryField func) {
1097
  DCHECK(fd.is_valid() && fr.is_valid() && fs.is_valid() && ft.is_valid());
1098 1099
  Instr instr = opcode | (fr.code() << kFrShift) | (ft.code() << kFtShift) |
                (fs.code() << kFsShift) | (fd.code() << kFdShift) | func;
1100 1101 1102
  emit(instr);
}

1103 1104
void Assembler::GenInstrRegister(Opcode opcode, SecondaryField fmt, Register rt,
                                 FPURegister fs, FPURegister fd,
1105
                                 SecondaryField func) {
1106
  DCHECK(fd.is_valid() && fs.is_valid() && rt.is_valid());
1107 1108
  Instr instr = opcode | fmt | (rt.code() << kRtShift) |
                (fs.code() << kFsShift) | (fd.code() << kFdShift) | func;
1109 1110 1111
  emit(instr);
}

1112 1113
void Assembler::GenInstrRegister(Opcode opcode, SecondaryField fmt, Register rt,
                                 FPUControlRegister fs, SecondaryField func) {
1114
  DCHECK(fs.is_valid() && rt.is_valid());
1115 1116 1117 1118 1119 1120 1121
  Instr instr =
      opcode | fmt | (rt.code() << kRtShift) | (fs.code() << kFsShift) | func;
  emit(instr);
}

// Instructions with immediate value.
// Registers are in the order of the instruction encoding, from left to right.
1122 1123 1124
void Assembler::GenInstrImmediate(Opcode opcode, Register rs, Register rt,
                                  int32_t j,
                                  CompactBranchType is_compact_branch) {
1125
  DCHECK(rs.is_valid() && rt.is_valid() && (is_int16(j) || is_uint16(j)));
1126 1127
  Instr instr = opcode | (rs.code() << kRsShift) | (rt.code() << kRtShift) |
                (j & kImm16Mask);
1128
  emit(instr, is_compact_branch);
1129 1130
}

1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
void Assembler::GenInstrImmediate(Opcode opcode, Register base, Register rt,
                                  int32_t offset9, int bit6,
                                  SecondaryField func) {
  DCHECK(base.is_valid() && rt.is_valid() && is_int9(offset9) &&
         is_uint1(bit6));
  Instr instr = opcode | (base.code() << kBaseShift) | (rt.code() << kRtShift) |
                ((offset9 << kImm9Shift) & kImm9Mask) | bit6 << kBit6Shift |
                func;
  emit(instr);
}
1141

1142 1143 1144
void Assembler::GenInstrImmediate(Opcode opcode, Register rs, SecondaryField SF,
                                  int32_t j,
                                  CompactBranchType is_compact_branch) {
1145
  DCHECK(rs.is_valid() && (is_int16(j) || is_uint16(j)));
1146
  Instr instr = opcode | (rs.code() << kRsShift) | SF | (j & kImm16Mask);
1147
  emit(instr, is_compact_branch);
1148 1149
}

1150 1151 1152
void Assembler::GenInstrImmediate(Opcode opcode, Register rs, FPURegister ft,
                                  int32_t j,
                                  CompactBranchType is_compact_branch) {
1153
  DCHECK(rs.is_valid() && ft.is_valid() && (is_int16(j) || is_uint16(j)));
1154 1155
  Instr instr = opcode | (rs.code() << kRsShift) | (ft.code() << kFtShift) |
                (j & kImm16Mask);
1156
  emit(instr, is_compact_branch);
1157 1158
}

1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
void Assembler::GenInstrImmediate(Opcode opcode, Register rs, int32_t offset21,
                                  CompactBranchType is_compact_branch) {
  DCHECK(rs.is_valid() && (is_int21(offset21)));
  Instr instr = opcode | (rs.code() << kRsShift) | (offset21 & kImm21Mask);
  emit(instr, is_compact_branch);
}

void Assembler::GenInstrImmediate(Opcode opcode, Register rs,
                                  uint32_t offset21) {
  DCHECK(rs.is_valid() && (is_uint21(offset21)));
  Instr instr = opcode | (rs.code() << kRsShift) | (offset21 & kImm21Mask);
1170 1171 1172
  emit(instr);
}

1173 1174
void Assembler::GenInstrImmediate(Opcode opcode, int32_t offset26,
                                  CompactBranchType is_compact_branch) {
1175 1176
  DCHECK(is_int26(offset26));
  Instr instr = opcode | (offset26 & kImm26Mask);
1177
  emit(instr, is_compact_branch);
1178 1179
}

1180
void Assembler::GenInstrJump(Opcode opcode, uint32_t address) {
1181
  BlockTrampolinePoolScope block_trampoline_pool(this);
1182
  DCHECK(is_uint26(address));
1183 1184 1185 1186 1187
  Instr instr = opcode | address;
  emit(instr);
  BlockTrampolinePoolFor(1);  // For associated delay slot.
}

1188 1189 1190
// MSA instructions
void Assembler::GenInstrMsaI8(SecondaryField operation, uint32_t imm8,
                              MSARegister ws, MSARegister wd) {
1191
  DCHECK(IsEnabled(MIPS_SIMD));
1192 1193 1194 1195 1196 1197 1198 1199
  DCHECK(ws.is_valid() && wd.is_valid() && is_uint8(imm8));
  Instr instr = MSA | operation | ((imm8 & kImm8Mask) << kWtShift) |
                (ws.code() << kWsShift) | (wd.code() << kWdShift);
  emit(instr);
}

void Assembler::GenInstrMsaI5(SecondaryField operation, SecondaryField df,
                              int32_t imm5, MSARegister ws, MSARegister wd) {
1200
  DCHECK(IsEnabled(MIPS_SIMD));
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
  DCHECK(ws.is_valid() && wd.is_valid());
  DCHECK((operation == MAXI_S) || (operation == MINI_S) ||
                 (operation == CEQI) || (operation == CLTI_S) ||
                 (operation == CLEI_S)
             ? is_int5(imm5)
             : is_uint5(imm5));
  Instr instr = MSA | operation | df | ((imm5 & kImm5Mask) << kWtShift) |
                (ws.code() << kWsShift) | (wd.code() << kWdShift);
  emit(instr);
}

void Assembler::GenInstrMsaBit(SecondaryField operation, SecondaryField df,
                               uint32_t m, MSARegister ws, MSARegister wd) {
1214
  DCHECK(IsEnabled(MIPS_SIMD));
1215 1216 1217 1218 1219 1220 1221 1222
  DCHECK(ws.is_valid() && wd.is_valid() && is_valid_msa_df_m(df, m));
  Instr instr = MSA | operation | df | (m << kWtShift) |
                (ws.code() << kWsShift) | (wd.code() << kWdShift);
  emit(instr);
}

void Assembler::GenInstrMsaI10(SecondaryField operation, SecondaryField df,
                               int32_t imm10, MSARegister wd) {
1223
  DCHECK(IsEnabled(MIPS_SIMD));
1224 1225 1226 1227 1228 1229 1230 1231 1232
  DCHECK(wd.is_valid() && is_int10(imm10));
  Instr instr = MSA | operation | df | ((imm10 & kImm10Mask) << kWsShift) |
                (wd.code() << kWdShift);
  emit(instr);
}

template <typename RegType>
void Assembler::GenInstrMsa3R(SecondaryField operation, SecondaryField df,
                              RegType t, MSARegister ws, MSARegister wd) {
1233
  DCHECK(IsEnabled(MIPS_SIMD));
1234 1235 1236 1237 1238 1239 1240 1241 1242
  DCHECK(t.is_valid() && ws.is_valid() && wd.is_valid());
  Instr instr = MSA | operation | df | (t.code() << kWtShift) |
                (ws.code() << kWsShift) | (wd.code() << kWdShift);
  emit(instr);
}

template <typename DstType, typename SrcType>
void Assembler::GenInstrMsaElm(SecondaryField operation, SecondaryField df,
                               uint32_t n, SrcType src, DstType dst) {
1243
  DCHECK(IsEnabled(MIPS_SIMD));
1244 1245 1246 1247 1248 1249 1250 1251 1252
  DCHECK(src.is_valid() && dst.is_valid() && is_valid_msa_df_n(df, n));
  Instr instr = MSA | operation | df | (n << kWtShift) |
                (src.code() << kWsShift) | (dst.code() << kWdShift) |
                MSA_ELM_MINOR;
  emit(instr);
}

void Assembler::GenInstrMsa3RF(SecondaryField operation, uint32_t df,
                               MSARegister wt, MSARegister ws, MSARegister wd) {
1253
  DCHECK(IsEnabled(MIPS_SIMD));
1254
  DCHECK(wt.is_valid() && ws.is_valid() && wd.is_valid());
1255
  DCHECK_LT(df, 2);
1256 1257 1258 1259 1260 1261 1262
  Instr instr = MSA | operation | (df << 21) | (wt.code() << kWtShift) |
                (ws.code() << kWsShift) | (wd.code() << kWdShift);
  emit(instr);
}

void Assembler::GenInstrMsaVec(SecondaryField operation, MSARegister wt,
                               MSARegister ws, MSARegister wd) {
1263
  DCHECK(IsEnabled(MIPS_SIMD));
1264 1265 1266 1267 1268 1269 1270 1271 1272
  DCHECK(wt.is_valid() && ws.is_valid() && wd.is_valid());
  Instr instr = MSA | operation | (wt.code() << kWtShift) |
                (ws.code() << kWsShift) | (wd.code() << kWdShift) |
                MSA_VEC_2R_2RF_MINOR;
  emit(instr);
}

void Assembler::GenInstrMsaMI10(SecondaryField operation, int32_t s10,
                                Register rs, MSARegister wd) {
1273
  DCHECK(IsEnabled(MIPS_SIMD));
1274 1275 1276 1277 1278 1279 1280 1281
  DCHECK(rs.is_valid() && wd.is_valid() && is_int10(s10));
  Instr instr = MSA | operation | ((s10 & kImm10Mask) << kWtShift) |
                (rs.code() << kWsShift) | (wd.code() << kWdShift);
  emit(instr);
}

void Assembler::GenInstrMsa2R(SecondaryField operation, SecondaryField df,
                              MSARegister ws, MSARegister wd) {
1282
  DCHECK(IsEnabled(MIPS_SIMD));
1283 1284 1285 1286 1287 1288 1289 1290
  DCHECK(ws.is_valid() && wd.is_valid());
  Instr instr = MSA | MSA_2R_FORMAT | operation | df | (ws.code() << kWsShift) |
                (wd.code() << kWdShift) | MSA_VEC_2R_2RF_MINOR;
  emit(instr);
}

void Assembler::GenInstrMsa2RF(SecondaryField operation, SecondaryField df,
                               MSARegister ws, MSARegister wd) {
1291
  DCHECK(IsEnabled(MIPS_SIMD));
1292 1293 1294 1295 1296 1297 1298 1299 1300
  DCHECK(ws.is_valid() && wd.is_valid());
  Instr instr = MSA | MSA_2RF_FORMAT | operation | df |
                (ws.code() << kWsShift) | (wd.code() << kWdShift) |
                MSA_VEC_2R_2RF_MINOR;
  emit(instr);
}

void Assembler::GenInstrMsaBranch(SecondaryField operation, MSARegister wt,
                                  int32_t offset16) {
1301
  DCHECK(IsEnabled(MIPS_SIMD));
1302 1303 1304 1305 1306 1307 1308
  DCHECK(wt.is_valid() && is_int16(offset16));
  BlockTrampolinePoolScope block_trampoline_pool(this);
  Instr instr =
      COP1 | operation | (wt.code() << kWtShift) | (offset16 & kImm16Mask);
  emit(instr);
  BlockTrampolinePoolFor(1);  // For associated delay slot.
}
1309 1310 1311 1312 1313 1314

// Returns the next free trampoline entry.
int32_t Assembler::get_trampoline_entry(int32_t pos) {
  int32_t trampoline_entry = kInvalidSlotPos;
  if (!internal_trampoline_exception_) {
    if (trampoline_.start() > pos) {
1315
      trampoline_entry = trampoline_.take_slot();
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
    }

    if (kInvalidSlotPos == trampoline_entry) {
      internal_trampoline_exception_ = true;
    }
  }
  return trampoline_entry;
}

uint64_t Assembler::jump_address(Label* L) {
  int64_t target_pos;
  if (L->is_bound()) {
    target_pos = L->pos();
  } else {
    if (L->is_linked()) {
      target_pos = L->pos();  // L's link.
      L->link_to(pc_offset());
    } else {
      L->link_to(pc_offset());
      return kEndOfJumpChain;
    }
  }
1338
  uint64_t imm = reinterpret_cast<uint64_t>(buffer_start_) + target_pos;
1339
  DCHECK_EQ(imm & 3, 0);
1340 1341 1342 1343

  return imm;
}

1344 1345
uint64_t Assembler::jump_offset(Label* L) {
  int64_t target_pos;
1346 1347
  int32_t pad = IsPrevInstrCompactBranch() ? kInstrSize : 0;

1348 1349 1350 1351 1352
  if (L->is_bound()) {
    target_pos = L->pos();
  } else {
    if (L->is_linked()) {
      target_pos = L->pos();  // L's link.
1353
      L->link_to(pc_offset() + pad);
1354
    } else {
1355
      L->link_to(pc_offset() + pad);
1356 1357 1358
      return kEndOfJumpChain;
    }
  }
1359
  int64_t imm = target_pos - (pc_offset() + pad);
1360
  DCHECK_EQ(imm & 3, 0);
1361 1362 1363 1364

  return static_cast<uint64_t>(imm);
}

1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
uint64_t Assembler::branch_long_offset(Label* L) {
  int64_t target_pos;

  if (L->is_bound()) {
    target_pos = L->pos();
  } else {
    if (L->is_linked()) {
      target_pos = L->pos();  // L's link.
      L->link_to(pc_offset());
    } else {
      L->link_to(pc_offset());
      return kEndOfJumpChain;
    }
  }
  int64_t offset = target_pos - (pc_offset() + kLongBranchPCOffset);
  DCHECK_EQ(offset & 3, 0);

  return static_cast<uint64_t>(offset);
}
1384

1385
int32_t Assembler::branch_offset_helper(Label* L, OffsetSize bits) {
1386
  int32_t target_pos;
1387
  int32_t pad = IsPrevInstrCompactBranch() ? kInstrSize : 0;
1388 1389 1390 1391 1392 1393

  if (L->is_bound()) {
    target_pos = L->pos();
  } else {
    if (L->is_linked()) {
      target_pos = L->pos();
1394
      L->link_to(pc_offset() + pad);
1395
    } else {
1396
      L->link_to(pc_offset() + pad);
1397 1398 1399 1400 1401 1402 1403 1404
      if (!trampoline_emitted_) {
        unbound_labels_count_++;
        next_buffer_check_ -= kTrampolineSlotsSize;
      }
      return kEndOfChain;
    }
  }

1405 1406
  int32_t offset = target_pos - (pc_offset() + kBranchPCOffset + pad);
  DCHECK(is_intn(offset, bits + 2));
1407
  DCHECK_EQ(offset & 3, 0);
1408 1409 1410 1411

  return offset;
}

1412 1413 1414 1415 1416 1417 1418 1419 1420
void Assembler::label_at_put(Label* L, int at_offset) {
  int target_pos;
  if (L->is_bound()) {
    target_pos = L->pos();
    instr_at_put(at_offset, target_pos + (Code::kHeaderSize - kHeapObjectTag));
  } else {
    if (L->is_linked()) {
      target_pos = L->pos();  // L's link.
      int32_t imm18 = target_pos - at_offset;
1421
      DCHECK_EQ(imm18 & 3, 0);
1422
      int32_t imm16 = imm18 >> 2;
1423
      DCHECK(is_int16(imm16));
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
      instr_at_put(at_offset, (imm16 & kImm16Mask));
    } else {
      target_pos = kEndOfChain;
      instr_at_put(at_offset, 0);
      if (!trampoline_emitted_) {
        unbound_labels_count_++;
        next_buffer_check_ -= kTrampolineSlotsSize;
      }
    }
    L->link_to(at_offset);
  }
}

//------- Branch and jump instructions --------

1439
void Assembler::b(int16_t offset) { beq(zero_reg, zero_reg, offset); }
1440

1441
void Assembler::bal(int16_t offset) { bgezal(zero_reg, offset); }
1442

1443
void Assembler::bc(int32_t offset) {
1444
  DCHECK_EQ(kArchVariant, kMips64r6);
1445
  GenInstrImmediate(BC, offset, CompactBranchType::COMPACT_BRANCH);
1446 1447 1448
}

void Assembler::balc(int32_t offset) {
1449
  DCHECK_EQ(kArchVariant, kMips64r6);
1450
  GenInstrImmediate(BALC, offset, CompactBranchType::COMPACT_BRANCH);
1451 1452
}

1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
void Assembler::beq(Register rs, Register rt, int16_t offset) {
  BlockTrampolinePoolScope block_trampoline_pool(this);
  GenInstrImmediate(BEQ, rs, rt, offset);
  BlockTrampolinePoolFor(1);  // For associated delay slot.
}

void Assembler::bgez(Register rs, int16_t offset) {
  BlockTrampolinePoolScope block_trampoline_pool(this);
  GenInstrImmediate(REGIMM, rs, BGEZ, offset);
  BlockTrampolinePoolFor(1);  // For associated delay slot.
}

1465
void Assembler::bgezc(Register rt, int16_t offset) {
1466
  DCHECK_EQ(kArchVariant, kMips64r6);
1467
  DCHECK(rt != zero_reg);
1468
  GenInstrImmediate(BLEZL, rt, rt, offset, CompactBranchType::COMPACT_BRANCH);
1469 1470 1471
}

void Assembler::bgeuc(Register rs, Register rt, int16_t offset) {
1472
  DCHECK_EQ(kArchVariant, kMips64r6);
1473 1474
  DCHECK(rs != zero_reg);
  DCHECK(rt != zero_reg);
1475
  DCHECK(rs.code() != rt.code());
1476
  GenInstrImmediate(BLEZ, rs, rt, offset, CompactBranchType::COMPACT_BRANCH);
1477 1478 1479
}

void Assembler::bgec(Register rs, Register rt, int16_t offset) {
1480
  DCHECK_EQ(kArchVariant, kMips64r6);
1481 1482
  DCHECK(rs != zero_reg);
  DCHECK(rt != zero_reg);
1483
  DCHECK(rs.code() != rt.code());
1484
  GenInstrImmediate(BLEZL, rs, rt, offset, CompactBranchType::COMPACT_BRANCH);
1485 1486
}

1487
void Assembler::bgezal(Register rs, int16_t offset) {
1488 1489
  DCHECK(kArchVariant != kMips64r6 || rs == zero_reg);
  DCHECK(rs != ra);
1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500
  BlockTrampolinePoolScope block_trampoline_pool(this);
  GenInstrImmediate(REGIMM, rs, BGEZAL, offset);
  BlockTrampolinePoolFor(1);  // For associated delay slot.
}

void Assembler::bgtz(Register rs, int16_t offset) {
  BlockTrampolinePoolScope block_trampoline_pool(this);
  GenInstrImmediate(BGTZ, rs, zero_reg, offset);
  BlockTrampolinePoolFor(1);  // For associated delay slot.
}

1501
void Assembler::bgtzc(Register rt, int16_t offset) {
1502
  DCHECK_EQ(kArchVariant, kMips64r6);
1503
  DCHECK(rt != zero_reg);
1504 1505
  GenInstrImmediate(BGTZL, zero_reg, rt, offset,
                    CompactBranchType::COMPACT_BRANCH);
1506 1507
}

1508 1509 1510 1511 1512 1513
void Assembler::blez(Register rs, int16_t offset) {
  BlockTrampolinePoolScope block_trampoline_pool(this);
  GenInstrImmediate(BLEZ, rs, zero_reg, offset);
  BlockTrampolinePoolFor(1);  // For associated delay slot.
}

1514
void Assembler::blezc(Register rt, int16_t offset) {
1515
  DCHECK_EQ(kArchVariant, kMips64r6);
1516
  DCHECK(rt != zero_reg);
1517 1518
  GenInstrImmediate(BLEZL, zero_reg, rt, offset,
                    CompactBranchType::COMPACT_BRANCH);
1519 1520 1521
}

void Assembler::bltzc(Register rt, int16_t offset) {
1522
  DCHECK_EQ(kArchVariant, kMips64r6);
1523
  DCHECK(rt != zero_reg);
1524
  GenInstrImmediate(BGTZL, rt, rt, offset, CompactBranchType::COMPACT_BRANCH);
1525 1526 1527
}

void Assembler::bltuc(Register rs, Register rt, int16_t offset) {
1528
  DCHECK_EQ(kArchVariant, kMips64r6);
1529 1530
  DCHECK(rs != zero_reg);
  DCHECK(rt != zero_reg);
1531
  DCHECK(rs.code() != rt.code());
1532
  GenInstrImmediate(BGTZ, rs, rt, offset, CompactBranchType::COMPACT_BRANCH);
1533 1534 1535
}

void Assembler::bltc(Register rs, Register rt, int16_t offset) {
1536
  DCHECK_EQ(kArchVariant, kMips64r6);
1537 1538
  DCHECK(rs != zero_reg);
  DCHECK(rt != zero_reg);
1539
  DCHECK(rs.code() != rt.code());
1540
  GenInstrImmediate(BGTZL, rs, rt, offset, CompactBranchType::COMPACT_BRANCH);
1541 1542
}

1543 1544 1545 1546 1547 1548 1549
void Assembler::bltz(Register rs, int16_t offset) {
  BlockTrampolinePoolScope block_trampoline_pool(this);
  GenInstrImmediate(REGIMM, rs, BLTZ, offset);
  BlockTrampolinePoolFor(1);  // For associated delay slot.
}

void Assembler::bltzal(Register rs, int16_t offset) {
1550 1551
  DCHECK(kArchVariant != kMips64r6 || rs == zero_reg);
  DCHECK(rs != ra);
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562
  BlockTrampolinePoolScope block_trampoline_pool(this);
  GenInstrImmediate(REGIMM, rs, BLTZAL, offset);
  BlockTrampolinePoolFor(1);  // For associated delay slot.
}

void Assembler::bne(Register rs, Register rt, int16_t offset) {
  BlockTrampolinePoolScope block_trampoline_pool(this);
  GenInstrImmediate(BNE, rs, rt, offset);
  BlockTrampolinePoolFor(1);  // For associated delay slot.
}

1563
void Assembler::bovc(Register rs, Register rt, int16_t offset) {
1564
  DCHECK_EQ(kArchVariant, kMips64r6);
1565 1566 1567 1568 1569
  if (rs.code() >= rt.code()) {
    GenInstrImmediate(ADDI, rs, rt, offset, CompactBranchType::COMPACT_BRANCH);
  } else {
    GenInstrImmediate(ADDI, rt, rs, offset, CompactBranchType::COMPACT_BRANCH);
  }
1570 1571 1572
}

void Assembler::bnvc(Register rs, Register rt, int16_t offset) {
1573
  DCHECK_EQ(kArchVariant, kMips64r6);
1574 1575 1576 1577 1578
  if (rs.code() >= rt.code()) {
    GenInstrImmediate(DADDI, rs, rt, offset, CompactBranchType::COMPACT_BRANCH);
  } else {
    GenInstrImmediate(DADDI, rt, rs, offset, CompactBranchType::COMPACT_BRANCH);
  }
1579 1580 1581
}

void Assembler::blezalc(Register rt, int16_t offset) {
1582
  DCHECK_EQ(kArchVariant, kMips64r6);
1583 1584
  DCHECK(rt != zero_reg);
  DCHECK(rt != ra);
1585 1586
  GenInstrImmediate(BLEZ, zero_reg, rt, offset,
                    CompactBranchType::COMPACT_BRANCH);
1587 1588 1589
}

void Assembler::bgezalc(Register rt, int16_t offset) {
1590
  DCHECK_EQ(kArchVariant, kMips64r6);
1591 1592
  DCHECK(rt != zero_reg);
  DCHECK(rt != ra);
1593
  GenInstrImmediate(BLEZ, rt, rt, offset, CompactBranchType::COMPACT_BRANCH);
1594 1595 1596
}

void Assembler::bgezall(Register rs, int16_t offset) {
1597
  DCHECK_NE(kArchVariant, kMips64r6);
1598 1599
  DCHECK(rs != zero_reg);
  DCHECK(rs != ra);
1600
  BlockTrampolinePoolScope block_trampoline_pool(this);
1601
  GenInstrImmediate(REGIMM, rs, BGEZALL, offset);
1602
  BlockTrampolinePoolFor(1);  // For associated delay slot.
1603 1604 1605
}

void Assembler::bltzalc(Register rt, int16_t offset) {
1606
  DCHECK_EQ(kArchVariant, kMips64r6);
1607 1608
  DCHECK(rt != zero_reg);
  DCHECK(rt != ra);
1609
  GenInstrImmediate(BGTZ, rt, rt, offset, CompactBranchType::COMPACT_BRANCH);
1610 1611 1612
}

void Assembler::bgtzalc(Register rt, int16_t offset) {
1613
  DCHECK_EQ(kArchVariant, kMips64r6);
1614 1615
  DCHECK(rt != zero_reg);
  DCHECK(rt != ra);
1616 1617
  GenInstrImmediate(BGTZ, zero_reg, rt, offset,
                    CompactBranchType::COMPACT_BRANCH);
1618 1619 1620
}

void Assembler::beqzalc(Register rt, int16_t offset) {
1621
  DCHECK_EQ(kArchVariant, kMips64r6);
1622 1623
  DCHECK(rt != zero_reg);
  DCHECK(rt != ra);
1624 1625
  GenInstrImmediate(ADDI, zero_reg, rt, offset,
                    CompactBranchType::COMPACT_BRANCH);
1626 1627 1628
}

void Assembler::bnezalc(Register rt, int16_t offset) {
1629
  DCHECK_EQ(kArchVariant, kMips64r6);
1630 1631
  DCHECK(rt != zero_reg);
  DCHECK(rt != ra);
1632 1633
  GenInstrImmediate(DADDI, zero_reg, rt, offset,
                    CompactBranchType::COMPACT_BRANCH);
1634 1635 1636
}

void Assembler::beqc(Register rs, Register rt, int16_t offset) {
1637
  DCHECK_EQ(kArchVariant, kMips64r6);
1638 1639 1640 1641 1642 1643
  DCHECK(rs.code() != rt.code() && rs.code() != 0 && rt.code() != 0);
  if (rs.code() < rt.code()) {
    GenInstrImmediate(ADDI, rs, rt, offset, CompactBranchType::COMPACT_BRANCH);
  } else {
    GenInstrImmediate(ADDI, rt, rs, offset, CompactBranchType::COMPACT_BRANCH);
  }
1644 1645 1646
}

void Assembler::beqzc(Register rs, int32_t offset) {
1647
  DCHECK_EQ(kArchVariant, kMips64r6);
1648
  DCHECK(rs != zero_reg);
1649
  GenInstrImmediate(POP66, rs, offset, CompactBranchType::COMPACT_BRANCH);
1650 1651 1652
}

void Assembler::bnec(Register rs, Register rt, int16_t offset) {
1653
  DCHECK_EQ(kArchVariant, kMips64r6);
1654 1655 1656 1657 1658 1659
  DCHECK(rs.code() != rt.code() && rs.code() != 0 && rt.code() != 0);
  if (rs.code() < rt.code()) {
    GenInstrImmediate(DADDI, rs, rt, offset, CompactBranchType::COMPACT_BRANCH);
  } else {
    GenInstrImmediate(DADDI, rt, rs, offset, CompactBranchType::COMPACT_BRANCH);
  }
1660 1661 1662
}

void Assembler::bnezc(Register rs, int32_t offset) {
1663
  DCHECK_EQ(kArchVariant, kMips64r6);
1664
  DCHECK(rs != zero_reg);
1665
  GenInstrImmediate(POP76, rs, offset, CompactBranchType::COMPACT_BRANCH);
1666 1667
}

1668
void Assembler::j(int64_t target) {
1669 1670
  // Deprecated. Use PC-relative jumps instead.
  UNREACHABLE();
1671 1672
}

1673
void Assembler::j(Label* target) {
1674 1675
  // Deprecated. Use PC-relative jumps instead.
  UNREACHABLE();
1676 1677 1678
}

void Assembler::jal(Label* target) {
1679 1680 1681 1682 1683 1684 1685
  // Deprecated. Use PC-relative jumps instead.
  UNREACHABLE();
}

void Assembler::jal(int64_t target) {
  // Deprecated. Use PC-relative jumps instead.
  UNREACHABLE();
1686 1687
}

1688
void Assembler::jr(Register rs) {
1689 1690 1691 1692 1693 1694
  if (kArchVariant != kMips64r6) {
    BlockTrampolinePoolScope block_trampoline_pool(this);
    GenInstrRegister(SPECIAL, rs, zero_reg, zero_reg, 0, JR);
    BlockTrampolinePoolFor(1);  // For associated delay slot.
  } else {
    jalr(rs, zero_reg);
1695 1696 1697 1698
  }
}

void Assembler::jalr(Register rs, Register rd) {
1699
  DCHECK(rs.code() != rd.code());
1700 1701 1702 1703 1704
  BlockTrampolinePoolScope block_trampoline_pool(this);
  GenInstrRegister(SPECIAL, rs, zero_reg, rd, 0, JALR);
  BlockTrampolinePoolFor(1);  // For associated delay slot.
}

1705
void Assembler::jic(Register rt, int16_t offset) {
1706
  DCHECK_EQ(kArchVariant, kMips64r6);
1707
  GenInstrImmediate(POP66, zero_reg, rt, offset);
1708 1709
}

1710
void Assembler::jialc(Register rt, int16_t offset) {
1711
  DCHECK_EQ(kArchVariant, kMips64r6);
1712
  GenInstrImmediate(POP76, zero_reg, rt, offset);
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731
}

// -------Data-processing-instructions---------

// Arithmetic.

void Assembler::addu(Register rd, Register rs, Register rt) {
  GenInstrRegister(SPECIAL, rs, rt, rd, 0, ADDU);
}

void Assembler::addiu(Register rd, Register rs, int32_t j) {
  GenInstrImmediate(ADDIU, rs, rd, j);
}

void Assembler::subu(Register rd, Register rs, Register rt) {
  GenInstrRegister(SPECIAL, rs, rt, rd, 0, SUBU);
}

void Assembler::mul(Register rd, Register rs, Register rt) {
1732
  if (kArchVariant == kMips64r6) {
1733
    GenInstrRegister(SPECIAL, rs, rt, rd, MUL_OP, MUL_MUH);
1734
  } else {
1735
    GenInstrRegister(SPECIAL2, rs, rt, rd, 0, MUL);
1736 1737 1738 1739
  }
}

void Assembler::muh(Register rd, Register rs, Register rt) {
1740
  DCHECK_EQ(kArchVariant, kMips64r6);
1741 1742 1743 1744
  GenInstrRegister(SPECIAL, rs, rt, rd, MUH_OP, MUL_MUH);
}

void Assembler::mulu(Register rd, Register rs, Register rt) {
1745
  DCHECK_EQ(kArchVariant, kMips64r6);
1746 1747 1748 1749
  GenInstrRegister(SPECIAL, rs, rt, rd, MUL_OP, MUL_MUH_U);
}

void Assembler::muhu(Register rd, Register rs, Register rt) {
1750
  DCHECK_EQ(kArchVariant, kMips64r6);
1751 1752 1753 1754
  GenInstrRegister(SPECIAL, rs, rt, rd, MUH_OP, MUL_MUH_U);
}

void Assembler::dmul(Register rd, Register rs, Register rt) {
1755
  DCHECK_EQ(kArchVariant, kMips64r6);
1756 1757 1758 1759
  GenInstrRegister(SPECIAL, rs, rt, rd, MUL_OP, D_MUL_MUH);
}

void Assembler::dmuh(Register rd, Register rs, Register rt) {
1760
  DCHECK_EQ(kArchVariant, kMips64r6);
1761 1762 1763 1764
  GenInstrRegister(SPECIAL, rs, rt, rd, MUH_OP, D_MUL_MUH);
}

void Assembler::dmulu(Register rd, Register rs, Register rt) {
1765
  DCHECK_EQ(kArchVariant, kMips64r6);
1766 1767 1768 1769
  GenInstrRegister(SPECIAL, rs, rt, rd, MUL_OP, D_MUL_MUH_U);
}

void Assembler::dmuhu(Register rd, Register rs, Register rt) {
1770
  DCHECK_EQ(kArchVariant, kMips64r6);
1771
  GenInstrRegister(SPECIAL, rs, rt, rd, MUH_OP, D_MUL_MUH_U);
1772 1773 1774
}

void Assembler::mult(Register rs, Register rt) {
1775
  DCHECK_NE(kArchVariant, kMips64r6);
1776 1777 1778 1779
  GenInstrRegister(SPECIAL, rs, rt, zero_reg, 0, MULT);
}

void Assembler::multu(Register rs, Register rt) {
1780
  DCHECK_NE(kArchVariant, kMips64r6);
1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791
  GenInstrRegister(SPECIAL, rs, rt, zero_reg, 0, MULTU);
}

void Assembler::daddiu(Register rd, Register rs, int32_t j) {
  GenInstrImmediate(DADDIU, rs, rd, j);
}

void Assembler::div(Register rs, Register rt) {
  GenInstrRegister(SPECIAL, rs, rt, zero_reg, 0, DIV);
}

1792
void Assembler::div(Register rd, Register rs, Register rt) {
1793
  DCHECK_EQ(kArchVariant, kMips64r6);
1794 1795 1796 1797
  GenInstrRegister(SPECIAL, rs, rt, rd, DIV_OP, DIV_MOD);
}

void Assembler::mod(Register rd, Register rs, Register rt) {
1798
  DCHECK_EQ(kArchVariant, kMips64r6);
1799 1800 1801
  GenInstrRegister(SPECIAL, rs, rt, rd, MOD_OP, DIV_MOD);
}

1802 1803 1804 1805
void Assembler::divu(Register rs, Register rt) {
  GenInstrRegister(SPECIAL, rs, rt, zero_reg, 0, DIVU);
}

1806
void Assembler::divu(Register rd, Register rs, Register rt) {
1807
  DCHECK_EQ(kArchVariant, kMips64r6);
1808 1809 1810 1811
  GenInstrRegister(SPECIAL, rs, rt, rd, DIV_OP, DIV_MOD_U);
}

void Assembler::modu(Register rd, Register rs, Register rt) {
1812
  DCHECK_EQ(kArchVariant, kMips64r6);
1813 1814 1815
  GenInstrRegister(SPECIAL, rs, rt, rd, MOD_OP, DIV_MOD_U);
}

1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835
void Assembler::daddu(Register rd, Register rs, Register rt) {
  GenInstrRegister(SPECIAL, rs, rt, rd, 0, DADDU);
}

void Assembler::dsubu(Register rd, Register rs, Register rt) {
  GenInstrRegister(SPECIAL, rs, rt, rd, 0, DSUBU);
}

void Assembler::dmult(Register rs, Register rt) {
  GenInstrRegister(SPECIAL, rs, rt, zero_reg, 0, DMULT);
}

void Assembler::dmultu(Register rs, Register rt) {
  GenInstrRegister(SPECIAL, rs, rt, zero_reg, 0, DMULTU);
}

void Assembler::ddiv(Register rs, Register rt) {
  GenInstrRegister(SPECIAL, rs, rt, zero_reg, 0, DDIV);
}

1836
void Assembler::ddiv(Register rd, Register rs, Register rt) {
1837
  DCHECK_EQ(kArchVariant, kMips64r6);
1838 1839 1840 1841
  GenInstrRegister(SPECIAL, rs, rt, rd, DIV_OP, D_DIV_MOD);
}

void Assembler::dmod(Register rd, Register rs, Register rt) {
1842
  DCHECK_EQ(kArchVariant, kMips64r6);
1843 1844 1845
  GenInstrRegister(SPECIAL, rs, rt, rd, MOD_OP, D_DIV_MOD);
}

1846 1847 1848 1849
void Assembler::ddivu(Register rs, Register rt) {
  GenInstrRegister(SPECIAL, rs, rt, zero_reg, 0, DDIVU);
}

1850
void Assembler::ddivu(Register rd, Register rs, Register rt) {
1851
  DCHECK_EQ(kArchVariant, kMips64r6);
1852 1853 1854 1855
  GenInstrRegister(SPECIAL, rs, rt, rd, DIV_OP, D_DIV_MOD_U);
}

void Assembler::dmodu(Register rd, Register rs, Register rt) {
1856
  DCHECK_EQ(kArchVariant, kMips64r6);
1857 1858 1859
  GenInstrRegister(SPECIAL, rs, rt, rd, MOD_OP, D_DIV_MOD_U);
}

1860 1861 1862 1863 1864 1865 1866
// Logical.

void Assembler::and_(Register rd, Register rs, Register rt) {
  GenInstrRegister(SPECIAL, rs, rt, rd, 0, AND);
}

void Assembler::andi(Register rt, Register rs, int32_t j) {
1867
  DCHECK(is_uint16(j));
1868 1869 1870 1871 1872 1873 1874 1875
  GenInstrImmediate(ANDI, rs, rt, j);
}

void Assembler::or_(Register rd, Register rs, Register rt) {
  GenInstrRegister(SPECIAL, rs, rt, rd, 0, OR);
}

void Assembler::ori(Register rt, Register rs, int32_t j) {
1876
  DCHECK(is_uint16(j));
1877 1878 1879 1880 1881 1882 1883 1884
  GenInstrImmediate(ORI, rs, rt, j);
}

void Assembler::xor_(Register rd, Register rs, Register rt) {
  GenInstrRegister(SPECIAL, rs, rt, rd, 0, XOR);
}

void Assembler::xori(Register rt, Register rs, int32_t j) {
1885
  DCHECK(is_uint16(j));
1886 1887 1888 1889 1890 1891 1892 1893
  GenInstrImmediate(XORI, rs, rt, j);
}

void Assembler::nor(Register rd, Register rs, Register rt) {
  GenInstrRegister(SPECIAL, rs, rt, rd, 0, NOR);
}

// Shifts.
1894
void Assembler::sll(Register rd, Register rt, uint16_t sa,
1895 1896 1897
                    bool coming_from_nop) {
  // Don't allow nop instructions in the form sll zero_reg, zero_reg to be
  // generated using the sll instruction. They must be generated using
1898
  // nop(int/NopMarkerTypes).
1899
  DCHECK(coming_from_nop || (rd != zero_reg && rt != zero_reg));
1900
  GenInstrRegister(SPECIAL, zero_reg, rt, rd, sa & 0x1F, SLL);
1901 1902 1903 1904 1905 1906 1907
}

void Assembler::sllv(Register rd, Register rt, Register rs) {
  GenInstrRegister(SPECIAL, rs, rt, rd, 0, SLLV);
}

void Assembler::srl(Register rd, Register rt, uint16_t sa) {
1908
  GenInstrRegister(SPECIAL, zero_reg, rt, rd, sa & 0x1F, SRL);
1909 1910 1911 1912 1913 1914 1915
}

void Assembler::srlv(Register rd, Register rt, Register rs) {
  GenInstrRegister(SPECIAL, rs, rt, rd, 0, SRLV);
}

void Assembler::sra(Register rd, Register rt, uint16_t sa) {
1916
  GenInstrRegister(SPECIAL, zero_reg, rt, rd, sa & 0x1F, SRA);
1917 1918 1919 1920 1921 1922 1923 1924
}

void Assembler::srav(Register rd, Register rt, Register rs) {
  GenInstrRegister(SPECIAL, rs, rt, rd, 0, SRAV);
}

void Assembler::rotr(Register rd, Register rt, uint16_t sa) {
  // Should be called via MacroAssembler::Ror.
1925
  DCHECK(rd.is_valid() && rt.is_valid() && is_uint5(sa));
1926
  DCHECK(kArchVariant == kMips64r2 || kArchVariant == kMips64r6);
1927 1928
  Instr instr = SPECIAL | (1 << kRsShift) | (rt.code() << kRtShift) |
                (rd.code() << kRdShift) | (sa << kSaShift) | SRL;
1929 1930 1931 1932 1933
  emit(instr);
}

void Assembler::rotrv(Register rd, Register rt, Register rs) {
  // Should be called via MacroAssembler::Ror.
1934
  DCHECK(rd.is_valid() && rt.is_valid() && rs.is_valid());
1935
  DCHECK(kArchVariant == kMips64r2 || kArchVariant == kMips64r6);
1936 1937
  Instr instr = SPECIAL | (rs.code() << kRsShift) | (rt.code() << kRtShift) |
                (rd.code() << kRdShift) | (1 << kSaShift) | SRLV;
1938 1939 1940 1941
  emit(instr);
}

void Assembler::dsll(Register rd, Register rt, uint16_t sa) {
1942
  GenInstrRegister(SPECIAL, zero_reg, rt, rd, sa & 0x1F, DSLL);
1943 1944 1945 1946 1947 1948 1949
}

void Assembler::dsllv(Register rd, Register rt, Register rs) {
  GenInstrRegister(SPECIAL, rs, rt, rd, 0, DSLLV);
}

void Assembler::dsrl(Register rd, Register rt, uint16_t sa) {
1950
  GenInstrRegister(SPECIAL, zero_reg, rt, rd, sa & 0x1F, DSRL);
1951 1952 1953 1954 1955 1956 1957
}

void Assembler::dsrlv(Register rd, Register rt, Register rs) {
  GenInstrRegister(SPECIAL, rs, rt, rd, 0, DSRLV);
}

void Assembler::drotr(Register rd, Register rt, uint16_t sa) {
1958
  DCHECK(rd.is_valid() && rt.is_valid() && is_uint5(sa));
1959 1960
  Instr instr = SPECIAL | (1 << kRsShift) | (rt.code() << kRtShift) |
                (rd.code() << kRdShift) | (sa << kSaShift) | DSRL;
1961 1962 1963
  emit(instr);
}

1964 1965 1966 1967 1968 1969
void Assembler::drotr32(Register rd, Register rt, uint16_t sa) {
  DCHECK(rd.is_valid() && rt.is_valid() && is_uint5(sa));
  Instr instr = SPECIAL | (1 << kRsShift) | (rt.code() << kRtShift) |
                (rd.code() << kRdShift) | (sa << kSaShift) | DSRL32;
  emit(instr);
}
1970 1971

void Assembler::drotrv(Register rd, Register rt, Register rs) {
1972 1973 1974
  DCHECK(rd.is_valid() && rt.is_valid() && rs.is_valid());
  Instr instr = SPECIAL | (rs.code() << kRsShift) | (rt.code() << kRtShift) |
                (rd.code() << kRdShift) | (1 << kSaShift) | DSRLV;
1975 1976 1977 1978
  emit(instr);
}

void Assembler::dsra(Register rd, Register rt, uint16_t sa) {
1979
  GenInstrRegister(SPECIAL, zero_reg, rt, rd, sa & 0x1F, DSRA);
1980 1981 1982 1983 1984 1985 1986
}

void Assembler::dsrav(Register rd, Register rt, Register rs) {
  GenInstrRegister(SPECIAL, rs, rt, rd, 0, DSRAV);
}

void Assembler::dsll32(Register rd, Register rt, uint16_t sa) {
1987
  GenInstrRegister(SPECIAL, zero_reg, rt, rd, sa & 0x1F, DSLL32);
1988 1989 1990
}

void Assembler::dsrl32(Register rd, Register rt, uint16_t sa) {
1991
  GenInstrRegister(SPECIAL, zero_reg, rt, rd, sa & 0x1F, DSRL32);
1992 1993 1994
}

void Assembler::dsra32(Register rd, Register rt, uint16_t sa) {
1995
  GenInstrRegister(SPECIAL, zero_reg, rt, rd, sa & 0x1F, DSRA32);
1996 1997
}

1998 1999
void Assembler::lsa(Register rd, Register rt, Register rs, uint8_t sa) {
  DCHECK(rd.is_valid() && rt.is_valid() && rs.is_valid());
2000 2001
  DCHECK_LE(sa, 3);
  DCHECK_EQ(kArchVariant, kMips64r6);
2002 2003
  Instr instr = SPECIAL | rs.code() << kRsShift | rt.code() << kRtShift |
                rd.code() << kRdShift | sa << kSaShift | LSA;
2004 2005 2006 2007 2008
  emit(instr);
}

void Assembler::dlsa(Register rd, Register rt, Register rs, uint8_t sa) {
  DCHECK(rd.is_valid() && rt.is_valid() && rs.is_valid());
2009 2010
  DCHECK_LE(sa, 3);
  DCHECK_EQ(kArchVariant, kMips64r6);
2011 2012
  Instr instr = SPECIAL | rs.code() << kRsShift | rt.code() << kRtShift |
                rd.code() << kRdShift | sa << kSaShift | DLSA;
2013 2014 2015
  emit(instr);
}

2016 2017
// ------------Memory-instructions-------------

2018
void Assembler::AdjustBaseAndOffset(MemOperand* src,
2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030
                                    OffsetAccessType access_type,
                                    int second_access_add_to_offset) {
  // This method is used to adjust the base register and offset pair
  // for a load/store when the offset doesn't fit into int16_t.
  // It is assumed that 'base + offset' is sufficiently aligned for memory
  // operands that are machine word in size or smaller. For doubleword-sized
  // operands it's assumed that 'base' is a multiple of 8, while 'offset'
  // may be a multiple of 4 (e.g. 4-byte-aligned long and double arguments
  // and spilled variables on the stack accessed relative to the stack
  // pointer register).
  // We preserve the "alignment" of 'offset' by adjusting it by a multiple of 8.

2031
  bool doubleword_aligned = (src->offset() & (kDoubleSize - 1)) == 0;
2032
  bool two_accesses = static_cast<bool>(access_type) || !doubleword_aligned;
2033
  DCHECK_LE(second_access_add_to_offset, 7);  // Must be <= 7.
2034 2035

  // is_int16 must be passed a signed value, hence the static cast below.
2036
  if (is_int16(src->offset()) &&
2037
      (!two_accesses || is_int16(static_cast<int32_t>(
2038
                            src->offset() + second_access_add_to_offset)))) {
2039 2040 2041 2042
    // Nothing to do: 'offset' (and, if needed, 'offset + 4', or other specified
    // value) fits into int16_t.
    return;
  }
2043

2044
  DCHECK(src->rm() !=
2045
         at);  // Must not overwrite the register 'base' while loading 'offset'.
2046 2047 2048

#ifdef DEBUG
  // Remember the "(mis)alignment" of 'offset', it will be checked at the end.
2049
  uint32_t misalignment = src->offset() & (kDoubleSize - 1);
2050 2051 2052 2053 2054 2055 2056 2057
#endif

  // Do not load the whole 32-bit 'offset' if it can be represented as
  // a sum of two 16-bit signed offsets. This can save an instruction or two.
  // To simplify matters, only do this for a symmetric range of offsets from
  // about -64KB to about +64KB, allowing further addition of 4 when accessing
  // 64-bit variables with two 32-bit accesses.
  constexpr int32_t kMinOffsetForSimpleAdjustment =
2058
      0x7FF8;  // Max int16_t that's a multiple of 8.
2059 2060 2061
  constexpr int32_t kMaxOffsetForSimpleAdjustment =
      2 * kMinOffsetForSimpleAdjustment;

2062 2063
  UseScratchRegisterScope temps(this);
  Register scratch = temps.Acquire();
2064 2065 2066 2067 2068 2069 2070
  if (0 <= src->offset() && src->offset() <= kMaxOffsetForSimpleAdjustment) {
    daddiu(scratch, src->rm(), kMinOffsetForSimpleAdjustment);
    src->offset_ -= kMinOffsetForSimpleAdjustment;
  } else if (-kMaxOffsetForSimpleAdjustment <= src->offset() &&
             src->offset() < 0) {
    daddiu(scratch, src->rm(), -kMinOffsetForSimpleAdjustment);
    src->offset_ += kMinOffsetForSimpleAdjustment;
2071 2072
  } else if (kArchVariant == kMips64r6) {
    // On r6 take advantage of the daui instruction, e.g.:
2073 2074 2075 2076
    //    daui   at, base, offset_high
    //   [dahi   at, 1]                       // When `offset` is close to +2GB.
    //    lw     reg_lo, offset_low(at)
    //   [lw     reg_hi, (offset_low+4)(at)]  // If misaligned 64-bit load.
2077
    // or when offset_low+4 overflows int16_t:
2078 2079 2080 2081
    //    daui   at, base, offset_high
    //    daddiu at, at, 8
    //    lw     reg_lo, (offset_low-8)(at)
    //    lw     reg_hi, (offset_low-4)(at)
2082
    int16_t offset_low = static_cast<uint16_t>(src->offset());
2083
    int32_t offset_low32 = offset_low;
2084
    int16_t offset_high = static_cast<uint16_t>(src->offset() >> 16);
2085 2086 2087 2088 2089 2090 2091
    bool increment_hi16 = offset_low < 0;
    bool overflow_hi16 = false;

    if (increment_hi16) {
      offset_high++;
      overflow_hi16 = (offset_high == -32768);
    }
2092
    daui(scratch, src->rm(), static_cast<uint16_t>(offset_high));
2093 2094

    if (overflow_hi16) {
2095
      dahi(scratch, 1);
2096
    }
2097

2098 2099 2100 2101
    if (two_accesses && !is_int16(static_cast<int32_t>(
                            offset_low32 + second_access_add_to_offset))) {
      // Avoid overflow in the 16-bit offset of the load/store instruction when
      // adding 4.
2102
      daddiu(scratch, scratch, kDoubleSize);
2103
      offset_low32 -= kDoubleSize;
2104 2105
    }

2106
    src->offset_ = offset_low32;
2107
  } else {
2108 2109 2110 2111 2112 2113 2114 2115 2116
    // Do not load the whole 32-bit 'offset' if it can be represented as
    // a sum of three 16-bit signed offsets. This can save an instruction.
    // To simplify matters, only do this for a symmetric range of offsets from
    // about -96KB to about +96KB, allowing further addition of 4 when accessing
    // 64-bit variables with two 32-bit accesses.
    constexpr int32_t kMinOffsetForMediumAdjustment =
        2 * kMinOffsetForSimpleAdjustment;
    constexpr int32_t kMaxOffsetForMediumAdjustment =
        3 * kMinOffsetForSimpleAdjustment;
2117 2118
    if (0 <= src->offset() && src->offset() <= kMaxOffsetForMediumAdjustment) {
      daddiu(scratch, src->rm(), kMinOffsetForMediumAdjustment / 2);
2119
      daddiu(scratch, scratch, kMinOffsetForMediumAdjustment / 2);
2120 2121 2122 2123
      src->offset_ -= kMinOffsetForMediumAdjustment;
    } else if (-kMaxOffsetForMediumAdjustment <= src->offset() &&
               src->offset() < 0) {
      daddiu(scratch, src->rm(), -kMinOffsetForMediumAdjustment / 2);
2124
      daddiu(scratch, scratch, -kMinOffsetForMediumAdjustment / 2);
2125
      src->offset_ += kMinOffsetForMediumAdjustment;
2126 2127 2128
    } else {
      // Now that all shorter options have been exhausted, load the full 32-bit
      // offset.
2129
      int32_t loaded_offset = RoundDown(src->offset(), kDoubleSize);
2130 2131
      lui(scratch, (loaded_offset >> kLuiShift) & kImm16Mask);
      ori(scratch, scratch, loaded_offset & kImm16Mask);  // Load 32-bit offset.
2132 2133
      daddu(scratch, scratch, src->rm());
      src->offset_ -= loaded_offset;
2134 2135
    }
  }
2136
  src->rm_ = scratch;
2137

2138
  DCHECK(is_int16(src->offset()));
2139 2140
  if (two_accesses) {
    DCHECK(is_int16(
2141
        static_cast<int32_t>(src->offset() + second_access_add_to_offset)));
2142
  }
2143
  DCHECK(misalignment == (src->offset() & (kDoubleSize - 1)));
2144 2145 2146
}

void Assembler::lb(Register rd, const MemOperand& rs) {
2147
  GenInstrImmediate(LB, rs.rm(), rd, rs.offset_);
2148 2149 2150
}

void Assembler::lbu(Register rd, const MemOperand& rs) {
2151
  GenInstrImmediate(LBU, rs.rm(), rd, rs.offset_);
2152 2153 2154
}

void Assembler::lh(Register rd, const MemOperand& rs) {
2155
  GenInstrImmediate(LH, rs.rm(), rd, rs.offset_);
2156 2157 2158
}

void Assembler::lhu(Register rd, const MemOperand& rs) {
2159
  GenInstrImmediate(LHU, rs.rm(), rd, rs.offset_);
2160 2161 2162
}

void Assembler::lw(Register rd, const MemOperand& rs) {
2163
  GenInstrImmediate(LW, rs.rm(), rd, rs.offset_);
2164 2165 2166
}

void Assembler::lwu(Register rd, const MemOperand& rs) {
2167
  GenInstrImmediate(LWU, rs.rm(), rd, rs.offset_);
2168 2169 2170
}

void Assembler::lwl(Register rd, const MemOperand& rs) {
2171
  DCHECK(is_int16(rs.offset_));
2172
  DCHECK_EQ(kArchVariant, kMips64r2);
2173 2174 2175 2176
  GenInstrImmediate(LWL, rs.rm(), rd, rs.offset_);
}

void Assembler::lwr(Register rd, const MemOperand& rs) {
2177
  DCHECK(is_int16(rs.offset_));
2178
  DCHECK_EQ(kArchVariant, kMips64r2);
2179 2180 2181 2182
  GenInstrImmediate(LWR, rs.rm(), rd, rs.offset_);
}

void Assembler::sb(Register rd, const MemOperand& rs) {
2183
  GenInstrImmediate(SB, rs.rm(), rd, rs.offset_);
2184 2185 2186
}

void Assembler::sh(Register rd, const MemOperand& rs) {
2187
  GenInstrImmediate(SH, rs.rm(), rd, rs.offset_);
2188 2189 2190
}

void Assembler::sw(Register rd, const MemOperand& rs) {
2191
  GenInstrImmediate(SW, rs.rm(), rd, rs.offset_);
2192 2193 2194
}

void Assembler::swl(Register rd, const MemOperand& rs) {
2195
  DCHECK(is_int16(rs.offset_));
2196
  DCHECK_EQ(kArchVariant, kMips64r2);
2197 2198 2199 2200
  GenInstrImmediate(SWL, rs.rm(), rd, rs.offset_);
}

void Assembler::swr(Register rd, const MemOperand& rs) {
2201
  DCHECK(is_int16(rs.offset_));
2202
  DCHECK_EQ(kArchVariant, kMips64r2);
2203 2204 2205
  GenInstrImmediate(SWR, rs.rm(), rd, rs.offset_);
}

2206 2207 2208 2209 2210
void Assembler::ll(Register rd, const MemOperand& rs) {
  if (kArchVariant == kMips64r6) {
    DCHECK(is_int9(rs.offset_));
    GenInstrImmediate(SPECIAL3, rs.rm(), rd, rs.offset_, 0, LL_R6);
  } else {
2211
    DCHECK_EQ(kArchVariant, kMips64r2);
2212 2213 2214 2215 2216 2217 2218 2219 2220 2221
    DCHECK(is_int16(rs.offset_));
    GenInstrImmediate(LL, rs.rm(), rd, rs.offset_);
  }
}

void Assembler::lld(Register rd, const MemOperand& rs) {
  if (kArchVariant == kMips64r6) {
    DCHECK(is_int9(rs.offset_));
    GenInstrImmediate(SPECIAL3, rs.rm(), rd, rs.offset_, 0, LLD_R6);
  } else {
2222
    DCHECK_EQ(kArchVariant, kMips64r2);
2223 2224 2225 2226 2227 2228 2229 2230 2231 2232
    DCHECK(is_int16(rs.offset_));
    GenInstrImmediate(LLD, rs.rm(), rd, rs.offset_);
  }
}

void Assembler::sc(Register rd, const MemOperand& rs) {
  if (kArchVariant == kMips64r6) {
    DCHECK(is_int9(rs.offset_));
    GenInstrImmediate(SPECIAL3, rs.rm(), rd, rs.offset_, 0, SC_R6);
  } else {
2233
    DCHECK_EQ(kArchVariant, kMips64r2);
2234 2235 2236 2237 2238 2239 2240 2241 2242
    GenInstrImmediate(SC, rs.rm(), rd, rs.offset_);
  }
}

void Assembler::scd(Register rd, const MemOperand& rs) {
  if (kArchVariant == kMips64r6) {
    DCHECK(is_int9(rs.offset_));
    GenInstrImmediate(SPECIAL3, rs.rm(), rd, rs.offset_, 0, SCD_R6);
  } else {
2243
    DCHECK_EQ(kArchVariant, kMips64r2);
2244 2245 2246
    GenInstrImmediate(SCD, rs.rm(), rd, rs.offset_);
  }
}
2247 2248

void Assembler::lui(Register rd, int32_t j) {
2249
  DCHECK(is_uint16(j) || is_int16(j));
2250 2251 2252
  GenInstrImmediate(LUI, zero_reg, rd, j);
}

2253
void Assembler::aui(Register rt, Register rs, int32_t j) {
2254 2255
  // This instruction uses same opcode as 'lui'. The difference in encoding is
  // 'lui' has zero reg. for rs field.
2256
  DCHECK(is_uint16(j));
2257 2258 2259
  GenInstrImmediate(LUI, rs, rt, j);
}

2260
void Assembler::daui(Register rt, Register rs, int32_t j) {
2261
  DCHECK(is_uint16(j));
2262
  DCHECK(rs != zero_reg);
2263 2264 2265 2266
  GenInstrImmediate(DAUI, rs, rt, j);
}

void Assembler::dahi(Register rs, int32_t j) {
2267
  DCHECK(is_uint16(j));
2268 2269 2270 2271
  GenInstrImmediate(REGIMM, rs, DAHI, j);
}

void Assembler::dati(Register rs, int32_t j) {
2272
  DCHECK(is_uint16(j));
2273 2274 2275
  GenInstrImmediate(REGIMM, rs, DATI, j);
}

2276
void Assembler::ldl(Register rd, const MemOperand& rs) {
2277
  DCHECK(is_int16(rs.offset_));
2278
  DCHECK_EQ(kArchVariant, kMips64r2);
2279 2280 2281 2282
  GenInstrImmediate(LDL, rs.rm(), rd, rs.offset_);
}

void Assembler::ldr(Register rd, const MemOperand& rs) {
2283
  DCHECK(is_int16(rs.offset_));
2284
  DCHECK_EQ(kArchVariant, kMips64r2);
2285 2286 2287 2288
  GenInstrImmediate(LDR, rs.rm(), rd, rs.offset_);
}

void Assembler::sdl(Register rd, const MemOperand& rs) {
2289
  DCHECK(is_int16(rs.offset_));
2290
  DCHECK_EQ(kArchVariant, kMips64r2);
2291 2292 2293 2294
  GenInstrImmediate(SDL, rs.rm(), rd, rs.offset_);
}

void Assembler::sdr(Register rd, const MemOperand& rs) {
2295
  DCHECK(is_int16(rs.offset_));
2296
  DCHECK_EQ(kArchVariant, kMips64r2);
2297 2298 2299 2300
  GenInstrImmediate(SDR, rs.rm(), rd, rs.offset_);
}

void Assembler::ld(Register rd, const MemOperand& rs) {
2301
  GenInstrImmediate(LD, rs.rm(), rd, rs.offset_);
2302 2303 2304
}

void Assembler::sd(Register rd, const MemOperand& rs) {
2305
  GenInstrImmediate(SD, rs.rm(), rd, rs.offset_);
2306 2307
}

2308 2309 2310
// ---------PC-Relative instructions-----------

void Assembler::addiupc(Register rs, int32_t imm19) {
2311
  DCHECK_EQ(kArchVariant, kMips64r6);
2312
  DCHECK(rs.is_valid() && is_int19(imm19));
2313
  uint32_t imm21 = ADDIUPC << kImm19Bits | (imm19 & kImm19Mask);
2314 2315 2316 2317
  GenInstrImmediate(PCREL, rs, imm21);
}

void Assembler::lwpc(Register rs, int32_t offset19) {
2318
  DCHECK_EQ(kArchVariant, kMips64r6);
2319
  DCHECK(rs.is_valid() && is_int19(offset19));
2320
  uint32_t imm21 = LWPC << kImm19Bits | (offset19 & kImm19Mask);
2321 2322 2323 2324
  GenInstrImmediate(PCREL, rs, imm21);
}

void Assembler::lwupc(Register rs, int32_t offset19) {
2325
  DCHECK_EQ(kArchVariant, kMips64r6);
2326
  DCHECK(rs.is_valid() && is_int19(offset19));
2327
  uint32_t imm21 = LWUPC << kImm19Bits | (offset19 & kImm19Mask);
2328 2329 2330 2331
  GenInstrImmediate(PCREL, rs, imm21);
}

void Assembler::ldpc(Register rs, int32_t offset18) {
2332
  DCHECK_EQ(kArchVariant, kMips64r6);
2333
  DCHECK(rs.is_valid() && is_int18(offset18));
2334
  uint32_t imm21 = LDPC << kImm18Bits | (offset18 & kImm18Mask);
2335 2336 2337 2338
  GenInstrImmediate(PCREL, rs, imm21);
}

void Assembler::auipc(Register rs, int16_t imm16) {
2339
  DCHECK_EQ(kArchVariant, kMips64r6);
2340 2341
  DCHECK(rs.is_valid());
  uint32_t imm21 = AUIPC << kImm16Bits | (imm16 & kImm16Mask);
2342 2343 2344 2345
  GenInstrImmediate(PCREL, rs, imm21);
}

void Assembler::aluipc(Register rs, int16_t imm16) {
2346
  DCHECK_EQ(kArchVariant, kMips64r6);
2347 2348
  DCHECK(rs.is_valid());
  uint32_t imm21 = ALUIPC << kImm16Bits | (imm16 & kImm16Mask);
2349 2350 2351
  GenInstrImmediate(PCREL, rs, imm21);
}

2352 2353 2354 2355
// -------------Misc-instructions--------------

// Break / Trap instructions.
void Assembler::break_(uint32_t code, bool break_as_stop) {
2356
  DCHECK_EQ(code & ~0xFFFFF, 0);
2357 2358 2359
  // We need to invalidate breaks that could be stops as well because the
  // simulator expects a char pointer after the stop instruction.
  // See constants-mips.h for explanation.
2360 2361 2362
  DCHECK(
      (break_as_stop && code <= kMaxStopCode && code > kMaxWatchpointCode) ||
      (!break_as_stop && (code > kMaxStopCode || code <= kMaxWatchpointCode)));
2363 2364 2365 2366
  Instr break_instr = SPECIAL | BREAK | (code << 6);
  emit(break_instr);
}

2367
void Assembler::stop(uint32_t code) {
2368 2369
  DCHECK_GT(code, kMaxWatchpointCode);
  DCHECK_LE(code, kMaxStopCode);
2370 2371 2372 2373 2374 2375 2376 2377
#if defined(V8_HOST_ARCH_MIPS) || defined(V8_HOST_ARCH_MIPS64)
  break_(0x54321);
#else  // V8_HOST_ARCH_MIPS
  break_(code, true);
#endif
}

void Assembler::tge(Register rs, Register rt, uint16_t code) {
2378
  DCHECK(is_uint10(code));
2379 2380
  Instr instr =
      SPECIAL | TGE | rs.code() << kRsShift | rt.code() << kRtShift | code << 6;
2381 2382 2383 2384
  emit(instr);
}

void Assembler::tgeu(Register rs, Register rt, uint16_t code) {
2385
  DCHECK(is_uint10(code));
2386 2387
  Instr instr = SPECIAL | TGEU | rs.code() << kRsShift | rt.code() << kRtShift |
                code << 6;
2388 2389 2390 2391
  emit(instr);
}

void Assembler::tlt(Register rs, Register rt, uint16_t code) {
2392
  DCHECK(is_uint10(code));
2393 2394 2395 2396 2397 2398
  Instr instr =
      SPECIAL | TLT | rs.code() << kRsShift | rt.code() << kRtShift | code << 6;
  emit(instr);
}

void Assembler::tltu(Register rs, Register rt, uint16_t code) {
2399
  DCHECK(is_uint10(code));
2400 2401
  Instr instr = SPECIAL | TLTU | rs.code() << kRsShift | rt.code() << kRtShift |
                code << 6;
2402 2403 2404 2405
  emit(instr);
}

void Assembler::teq(Register rs, Register rt, uint16_t code) {
2406
  DCHECK(is_uint10(code));
2407 2408 2409 2410 2411 2412
  Instr instr =
      SPECIAL | TEQ | rs.code() << kRsShift | rt.code() << kRtShift | code << 6;
  emit(instr);
}

void Assembler::tne(Register rs, Register rt, uint16_t code) {
2413
  DCHECK(is_uint10(code));
2414 2415 2416 2417 2418
  Instr instr =
      SPECIAL | TNE | rs.code() << kRsShift | rt.code() << kRtShift | code << 6;
  emit(instr);
}

2419 2420 2421 2422
void Assembler::sync() {
  Instr sync_instr = SPECIAL | SYNC;
  emit(sync_instr);
}
2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460

// Move from HI/LO register.

void Assembler::mfhi(Register rd) {
  GenInstrRegister(SPECIAL, zero_reg, zero_reg, rd, 0, MFHI);
}

void Assembler::mflo(Register rd) {
  GenInstrRegister(SPECIAL, zero_reg, zero_reg, rd, 0, MFLO);
}

// Set on less than instructions.
void Assembler::slt(Register rd, Register rs, Register rt) {
  GenInstrRegister(SPECIAL, rs, rt, rd, 0, SLT);
}

void Assembler::sltu(Register rd, Register rs, Register rt) {
  GenInstrRegister(SPECIAL, rs, rt, rd, 0, SLTU);
}

void Assembler::slti(Register rt, Register rs, int32_t j) {
  GenInstrImmediate(SLTI, rs, rt, j);
}

void Assembler::sltiu(Register rt, Register rs, int32_t j) {
  GenInstrImmediate(SLTIU, rs, rt, j);
}

// Conditional move.
void Assembler::movz(Register rd, Register rs, Register rt) {
  GenInstrRegister(SPECIAL, rs, rt, rd, 0, MOVZ);
}

void Assembler::movn(Register rd, Register rs, Register rt) {
  GenInstrRegister(SPECIAL, rs, rt, rd, 0, MOVN);
}

void Assembler::movt(Register rd, Register rs, uint16_t cc) {
2461
  Register rt = Register::from_code((cc & 0x0007) << 2 | 1);
2462 2463 2464 2465
  GenInstrRegister(SPECIAL, rs, rt, rd, 0, MOVCI);
}

void Assembler::movf(Register rd, Register rs, uint16_t cc) {
2466
  Register rt = Register::from_code((cc & 0x0007) << 2 | 0);
2467 2468 2469
  GenInstrRegister(SPECIAL, rs, rt, rd, 0, MOVCI);
}

2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501
void Assembler::min_s(FPURegister fd, FPURegister fs, FPURegister ft) {
  min(S, fd, fs, ft);
}

void Assembler::min_d(FPURegister fd, FPURegister fs, FPURegister ft) {
  min(D, fd, fs, ft);
}

void Assembler::max_s(FPURegister fd, FPURegister fs, FPURegister ft) {
  max(S, fd, fs, ft);
}

void Assembler::max_d(FPURegister fd, FPURegister fs, FPURegister ft) {
  max(D, fd, fs, ft);
}

void Assembler::mina_s(FPURegister fd, FPURegister fs, FPURegister ft) {
  mina(S, fd, fs, ft);
}

void Assembler::mina_d(FPURegister fd, FPURegister fs, FPURegister ft) {
  mina(D, fd, fs, ft);
}

void Assembler::maxa_s(FPURegister fd, FPURegister fs, FPURegister ft) {
  maxa(S, fd, fs, ft);
}

void Assembler::maxa_d(FPURegister fd, FPURegister fs, FPURegister ft) {
  maxa(D, fd, fs, ft);
}

2502 2503
void Assembler::max(SecondaryField fmt, FPURegister fd, FPURegister fs,
                    FPURegister ft) {
2504
  DCHECK_EQ(kArchVariant, kMips64r6);
2505 2506 2507 2508 2509 2510
  DCHECK((fmt == D) || (fmt == S));
  GenInstrRegister(COP1, fmt, ft, fs, fd, MAX);
}

void Assembler::min(SecondaryField fmt, FPURegister fd, FPURegister fs,
                    FPURegister ft) {
2511
  DCHECK_EQ(kArchVariant, kMips64r6);
2512 2513 2514 2515
  DCHECK((fmt == D) || (fmt == S));
  GenInstrRegister(COP1, fmt, ft, fs, fd, MIN);
}

2516
// GPR.
2517
void Assembler::seleqz(Register rd, Register rs, Register rt) {
2518
  DCHECK_EQ(kArchVariant, kMips64r6);
2519 2520 2521 2522
  GenInstrRegister(SPECIAL, rs, rt, rd, 0, SELEQZ_S);
}

// GPR.
2523
void Assembler::selnez(Register rd, Register rs, Register rt) {
2524
  DCHECK_EQ(kArchVariant, kMips64r6);
2525 2526 2527
  GenInstrRegister(SPECIAL, rs, rt, rd, 0, SELNEZ_S);
}

2528 2529
// Bit twiddling.
void Assembler::clz(Register rd, Register rs) {
2530
  if (kArchVariant != kMips64r6) {
2531
    // clz instr requires same GPR number in 'rd' and 'rt' fields.
2532 2533 2534 2535
    GenInstrRegister(SPECIAL2, rs, rd, rd, 0, CLZ);
  } else {
    GenInstrRegister(SPECIAL, rs, zero_reg, rd, 1, CLZ_R6);
  }
2536 2537
}

2538 2539 2540 2541 2542 2543 2544 2545 2546
void Assembler::dclz(Register rd, Register rs) {
  if (kArchVariant != kMips64r6) {
    // dclz instr requires same GPR number in 'rd' and 'rt' fields.
    GenInstrRegister(SPECIAL2, rs, rd, rd, 0, DCLZ);
  } else {
    GenInstrRegister(SPECIAL, rs, zero_reg, rd, 1, DCLZ_R6);
  }
}

2547 2548
void Assembler::ins_(Register rt, Register rs, uint16_t pos, uint16_t size) {
  // Should be called via MacroAssembler::Ins.
2549
  // ins instr has 'rt' field as dest, and two uint5: msb, lsb.
2550
  DCHECK((kArchVariant == kMips64r2) || (kArchVariant == kMips64r6));
2551 2552 2553
  GenInstrRegister(SPECIAL3, rs, rt, pos + size - 1, pos, INS);
}

2554 2555
void Assembler::dins_(Register rt, Register rs, uint16_t pos, uint16_t size) {
  // Should be called via MacroAssembler::Dins.
2556
  // dins instr has 'rt' field as dest, and two uint5: msb, lsb.
2557 2558 2559 2560
  DCHECK(kArchVariant == kMips64r2 || kArchVariant == kMips64r6);
  GenInstrRegister(SPECIAL3, rs, rt, pos + size - 1, pos, DINS);
}

2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573
void Assembler::dinsm_(Register rt, Register rs, uint16_t pos, uint16_t size) {
  // Should be called via MacroAssembler::Dins.
  // dinsm instr has 'rt' field as dest, and two uint5: msbminus32, lsb.
  DCHECK(kArchVariant == kMips64r2 || kArchVariant == kMips64r6);
  GenInstrRegister(SPECIAL3, rs, rt, pos + size - 1 - 32, pos, DINSM);
}

void Assembler::dinsu_(Register rt, Register rs, uint16_t pos, uint16_t size) {
  // Should be called via MacroAssembler::Dins.
  // dinsu instr has 'rt' field as dest, and two uint5: msbminus32, lsbminus32.
  DCHECK(kArchVariant == kMips64r2 || kArchVariant == kMips64r6);
  GenInstrRegister(SPECIAL3, rs, rt, pos + size - 1 - 32, pos - 32, DINSU);
}
2574

2575 2576
void Assembler::ext_(Register rt, Register rs, uint16_t pos, uint16_t size) {
  // Should be called via MacroAssembler::Ext.
2577
  // ext instr has 'rt' field as dest, and two uint5: msbd, lsb.
2578
  DCHECK(kArchVariant == kMips64r2 || kArchVariant == kMips64r6);
2579 2580 2581
  GenInstrRegister(SPECIAL3, rs, rt, size - 1, pos, EXT);
}

2582
void Assembler::dext_(Register rt, Register rs, uint16_t pos, uint16_t size) {
2583
  // Should be called via MacroAssembler::Dext.
2584
  // dext instr has 'rt' field as dest, and two uint5: msbd, lsb.
2585 2586 2587 2588
  DCHECK(kArchVariant == kMips64r2 || kArchVariant == kMips64r6);
  GenInstrRegister(SPECIAL3, rs, rt, size - 1, pos, DEXT);
}

2589
void Assembler::dextm_(Register rt, Register rs, uint16_t pos, uint16_t size) {
2590
  // Should be called via MacroAssembler::Dextm.
2591
  // dextm instr has 'rt' field as dest, and two uint5: msbdminus32, lsb.
2592 2593 2594 2595
  DCHECK(kArchVariant == kMips64r2 || kArchVariant == kMips64r6);
  GenInstrRegister(SPECIAL3, rs, rt, size - 1 - 32, pos, DEXTM);
}

2596
void Assembler::dextu_(Register rt, Register rs, uint16_t pos, uint16_t size) {
2597
  // Should be called via MacroAssembler::Dextu.
2598
  // dextu instr has 'rt' field as dest, and two uint5: msbd, lsbminus32.
2599 2600 2601 2602
  DCHECK(kArchVariant == kMips64r2 || kArchVariant == kMips64r6);
  GenInstrRegister(SPECIAL3, rs, rt, size - 1, pos - 32, DEXTU);
}

2603
void Assembler::bitswap(Register rd, Register rt) {
2604
  DCHECK_EQ(kArchVariant, kMips64r6);
2605
  GenInstrRegister(SPECIAL3, zero_reg, rt, rd, 0, BSHFL);
2606 2607 2608
}

void Assembler::dbitswap(Register rd, Register rt) {
2609
  DCHECK_EQ(kArchVariant, kMips64r6);
2610
  GenInstrRegister(SPECIAL3, zero_reg, rt, rd, 0, DBSHFL);
2611 2612
}

2613
void Assembler::pref(int32_t hint, const MemOperand& rs) {
2614
  DCHECK(is_uint5(hint) && is_uint16(rs.offset_));
2615 2616
  Instr instr =
      PREF | (rs.rm().code() << kRsShift) | (hint << kRtShift) | (rs.offset_);
2617 2618 2619
  emit(instr);
}

2620
void Assembler::align(Register rd, Register rs, Register rt, uint8_t bp) {
2621
  DCHECK_EQ(kArchVariant, kMips64r6);
2622 2623 2624 2625 2626 2627
  DCHECK(is_uint3(bp));
  uint16_t sa = (ALIGN << kBp2Bits) | bp;
  GenInstrRegister(SPECIAL3, rs, rt, rd, sa, BSHFL);
}

void Assembler::dalign(Register rd, Register rs, Register rt, uint8_t bp) {
2628
  DCHECK_EQ(kArchVariant, kMips64r6);
2629 2630 2631 2632 2633
  DCHECK(is_uint3(bp));
  uint16_t sa = (DALIGN << kBp3Bits) | bp;
  GenInstrRegister(SPECIAL3, rs, rt, rd, sa, DBSHFL);
}

2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657
void Assembler::wsbh(Register rd, Register rt) {
  DCHECK(kArchVariant == kMips64r2 || kArchVariant == kMips64r6);
  GenInstrRegister(SPECIAL3, zero_reg, rt, rd, WSBH, BSHFL);
}

void Assembler::dsbh(Register rd, Register rt) {
  DCHECK(kArchVariant == kMips64r2 || kArchVariant == kMips64r6);
  GenInstrRegister(SPECIAL3, zero_reg, rt, rd, DSBH, DBSHFL);
}

void Assembler::dshd(Register rd, Register rt) {
  DCHECK(kArchVariant == kMips64r2 || kArchVariant == kMips64r6);
  GenInstrRegister(SPECIAL3, zero_reg, rt, rd, DSHD, DBSHFL);
}

void Assembler::seh(Register rd, Register rt) {
  DCHECK(kArchVariant == kMips64r2 || kArchVariant == kMips64r6);
  GenInstrRegister(SPECIAL3, zero_reg, rt, rd, SEH, BSHFL);
}

void Assembler::seb(Register rd, Register rt) {
  DCHECK(kArchVariant == kMips64r2 || kArchVariant == kMips64r6);
  GenInstrRegister(SPECIAL3, zero_reg, rt, rd, SEB, BSHFL);
}
2658

2659 2660 2661 2662
// --------Coprocessor-instructions----------------

// Load, store, move.
void Assembler::lwc1(FPURegister fd, const MemOperand& src) {
2663
  GenInstrImmediate(LWC1, src.rm(), fd, src.offset_);
2664 2665 2666
}

void Assembler::ldc1(FPURegister fd, const MemOperand& src) {
2667
  GenInstrImmediate(LDC1, src.rm(), fd, src.offset_);
2668 2669
}

2670 2671
void Assembler::swc1(FPURegister fs, const MemOperand& src) {
  GenInstrImmediate(SWC1, src.rm(), fs, src.offset_);
2672 2673
}

2674 2675
void Assembler::sdc1(FPURegister fs, const MemOperand& src) {
  GenInstrImmediate(SDC1, src.rm(), fs, src.offset_);
2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709
}

void Assembler::mtc1(Register rt, FPURegister fs) {
  GenInstrRegister(COP1, MTC1, rt, fs, f0);
}

void Assembler::mthc1(Register rt, FPURegister fs) {
  GenInstrRegister(COP1, MTHC1, rt, fs, f0);
}

void Assembler::dmtc1(Register rt, FPURegister fs) {
  GenInstrRegister(COP1, DMTC1, rt, fs, f0);
}

void Assembler::mfc1(Register rt, FPURegister fs) {
  GenInstrRegister(COP1, MFC1, rt, fs, f0);
}

void Assembler::mfhc1(Register rt, FPURegister fs) {
  GenInstrRegister(COP1, MFHC1, rt, fs, f0);
}

void Assembler::dmfc1(Register rt, FPURegister fs) {
  GenInstrRegister(COP1, DMFC1, rt, fs, f0);
}

void Assembler::ctc1(Register rt, FPUControlRegister fs) {
  GenInstrRegister(COP1, CTC1, rt, fs);
}

void Assembler::cfc1(Register rt, FPUControlRegister fs) {
  GenInstrRegister(COP1, CFC1, rt, fs);
}

2710 2711
void Assembler::sel(SecondaryField fmt, FPURegister fd, FPURegister fs,
                    FPURegister ft) {
2712
  DCHECK_EQ(kArchVariant, kMips64r6);
2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749
  DCHECK((fmt == D) || (fmt == S));

  GenInstrRegister(COP1, fmt, ft, fs, fd, SEL);
}

void Assembler::sel_s(FPURegister fd, FPURegister fs, FPURegister ft) {
  sel(S, fd, fs, ft);
}

void Assembler::sel_d(FPURegister fd, FPURegister fs, FPURegister ft) {
  sel(D, fd, fs, ft);
}

// FPR.
void Assembler::seleqz(SecondaryField fmt, FPURegister fd, FPURegister fs,
                       FPURegister ft) {
  DCHECK((fmt == D) || (fmt == S));
  GenInstrRegister(COP1, fmt, ft, fs, fd, SELEQZ_C);
}

void Assembler::seleqz_d(FPURegister fd, FPURegister fs, FPURegister ft) {
  seleqz(D, fd, fs, ft);
}

void Assembler::seleqz_s(FPURegister fd, FPURegister fs, FPURegister ft) {
  seleqz(S, fd, fs, ft);
}

void Assembler::selnez_d(FPURegister fd, FPURegister fs, FPURegister ft) {
  selnez(D, fd, fs, ft);
}

void Assembler::selnez_s(FPURegister fd, FPURegister fs, FPURegister ft) {
  selnez(S, fd, fs, ft);
}

void Assembler::movz_s(FPURegister fd, FPURegister fs, Register rt) {
2750
  DCHECK_EQ(kArchVariant, kMips64r2);
2751 2752 2753 2754
  GenInstrRegister(COP1, S, rt, fs, fd, MOVZ_C);
}

void Assembler::movz_d(FPURegister fd, FPURegister fs, Register rt) {
2755
  DCHECK_EQ(kArchVariant, kMips64r2);
2756 2757 2758 2759
  GenInstrRegister(COP1, D, rt, fs, fd, MOVZ_C);
}

void Assembler::movt_s(FPURegister fd, FPURegister fs, uint16_t cc) {
2760
  DCHECK_EQ(kArchVariant, kMips64r2);
2761
  FPURegister ft = FPURegister::from_code((cc & 0x0007) << 2 | 1);
2762 2763 2764 2765
  GenInstrRegister(COP1, S, ft, fs, fd, MOVF);
}

void Assembler::movt_d(FPURegister fd, FPURegister fs, uint16_t cc) {
2766
  DCHECK_EQ(kArchVariant, kMips64r2);
2767
  FPURegister ft = FPURegister::from_code((cc & 0x0007) << 2 | 1);
2768 2769 2770 2771
  GenInstrRegister(COP1, D, ft, fs, fd, MOVF);
}

void Assembler::movf_s(FPURegister fd, FPURegister fs, uint16_t cc) {
2772
  DCHECK_EQ(kArchVariant, kMips64r2);
2773
  FPURegister ft = FPURegister::from_code((cc & 0x0007) << 2 | 0);
2774 2775 2776 2777
  GenInstrRegister(COP1, S, ft, fs, fd, MOVF);
}

void Assembler::movf_d(FPURegister fd, FPURegister fs, uint16_t cc) {
2778
  DCHECK_EQ(kArchVariant, kMips64r2);
2779
  FPURegister ft = FPURegister::from_code((cc & 0x0007) << 2 | 0);
2780 2781 2782 2783
  GenInstrRegister(COP1, D, ft, fs, fd, MOVF);
}

void Assembler::movn_s(FPURegister fd, FPURegister fs, Register rt) {
2784
  DCHECK_EQ(kArchVariant, kMips64r2);
2785 2786 2787 2788
  GenInstrRegister(COP1, S, rt, fs, fd, MOVN_C);
}

void Assembler::movn_d(FPURegister fd, FPURegister fs, Register rt) {
2789
  DCHECK_EQ(kArchVariant, kMips64r2);
2790 2791 2792 2793 2794 2795
  GenInstrRegister(COP1, D, rt, fs, fd, MOVN_C);
}

// FPR.
void Assembler::selnez(SecondaryField fmt, FPURegister fd, FPURegister fs,
                       FPURegister ft) {
2796
  DCHECK_EQ(kArchVariant, kMips64r6);
2797 2798 2799 2800
  DCHECK((fmt == D) || (fmt == S));
  GenInstrRegister(COP1, fmt, ft, fs, fd, SELNEZ_C);
}

2801 2802
// Arithmetic.

2803 2804 2805 2806
void Assembler::add_s(FPURegister fd, FPURegister fs, FPURegister ft) {
  GenInstrRegister(COP1, S, ft, fs, fd, ADD_D);
}

2807 2808 2809 2810
void Assembler::add_d(FPURegister fd, FPURegister fs, FPURegister ft) {
  GenInstrRegister(COP1, D, ft, fs, fd, ADD_D);
}

2811 2812 2813 2814
void Assembler::sub_s(FPURegister fd, FPURegister fs, FPURegister ft) {
  GenInstrRegister(COP1, S, ft, fs, fd, SUB_D);
}

2815 2816 2817 2818
void Assembler::sub_d(FPURegister fd, FPURegister fs, FPURegister ft) {
  GenInstrRegister(COP1, D, ft, fs, fd, SUB_D);
}

2819 2820 2821 2822
void Assembler::mul_s(FPURegister fd, FPURegister fs, FPURegister ft) {
  GenInstrRegister(COP1, S, ft, fs, fd, MUL_D);
}

2823 2824 2825 2826
void Assembler::mul_d(FPURegister fd, FPURegister fs, FPURegister ft) {
  GenInstrRegister(COP1, D, ft, fs, fd, MUL_D);
}

2827 2828
void Assembler::madd_s(FPURegister fd, FPURegister fr, FPURegister fs,
                       FPURegister ft) {
2829 2830 2831 2832
  // On Loongson 3A (MIPS64R2), MADD.S instruction is actually fused MADD.S and
  // this causes failure in some of the tests. Since this optimization is rarely
  // used, and not used at all on MIPS64R6, this isntruction is removed.
  UNREACHABLE();
2833
}
2834 2835

void Assembler::madd_d(FPURegister fd, FPURegister fr, FPURegister fs,
2836
                       FPURegister ft) {
2837 2838 2839 2840
  // On Loongson 3A (MIPS64R2), MADD.D instruction is actually fused MADD.D and
  // this causes failure in some of the tests. Since this optimization is rarely
  // used, and not used at all on MIPS64R6, this isntruction is removed.
  UNREACHABLE();
2841 2842
}

2843 2844
void Assembler::msub_s(FPURegister fd, FPURegister fr, FPURegister fs,
                       FPURegister ft) {
2845 2846
  // See explanation for instruction madd_s.
  UNREACHABLE();
2847 2848 2849 2850
}

void Assembler::msub_d(FPURegister fd, FPURegister fr, FPURegister fs,
                       FPURegister ft) {
2851 2852
  // See explanation for instruction madd_d.
  UNREACHABLE();
2853 2854 2855
}

void Assembler::maddf_s(FPURegister fd, FPURegister fs, FPURegister ft) {
2856
  DCHECK_EQ(kArchVariant, kMips64r6);
2857 2858 2859 2860
  GenInstrRegister(COP1, S, ft, fs, fd, MADDF_S);
}

void Assembler::maddf_d(FPURegister fd, FPURegister fs, FPURegister ft) {
2861
  DCHECK_EQ(kArchVariant, kMips64r6);
2862 2863 2864 2865
  GenInstrRegister(COP1, D, ft, fs, fd, MADDF_D);
}

void Assembler::msubf_s(FPURegister fd, FPURegister fs, FPURegister ft) {
2866
  DCHECK_EQ(kArchVariant, kMips64r6);
2867 2868 2869 2870
  GenInstrRegister(COP1, S, ft, fs, fd, MSUBF_S);
}

void Assembler::msubf_d(FPURegister fd, FPURegister fs, FPURegister ft) {
2871
  DCHECK_EQ(kArchVariant, kMips64r6);
2872 2873
  GenInstrRegister(COP1, D, ft, fs, fd, MSUBF_D);
}
2874

2875 2876 2877 2878
void Assembler::div_s(FPURegister fd, FPURegister fs, FPURegister ft) {
  GenInstrRegister(COP1, S, ft, fs, fd, DIV_D);
}

2879 2880 2881 2882
void Assembler::div_d(FPURegister fd, FPURegister fs, FPURegister ft) {
  GenInstrRegister(COP1, D, ft, fs, fd, DIV_D);
}

2883 2884 2885 2886
void Assembler::abs_s(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, S, f0, fs, fd, ABS_D);
}

2887 2888 2889 2890 2891 2892 2893 2894
void Assembler::abs_d(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, D, f0, fs, fd, ABS_D);
}

void Assembler::mov_d(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, D, f0, fs, fd, MOV_D);
}

2895
void Assembler::mov_s(FPURegister fd, FPURegister fs) {
2896
  GenInstrRegister(COP1, S, f0, fs, fd, MOV_S);
2897 2898
}

2899 2900 2901 2902
void Assembler::neg_s(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, S, f0, fs, fd, NEG_D);
}

2903 2904 2905 2906
void Assembler::neg_d(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, D, f0, fs, fd, NEG_D);
}

2907 2908 2909 2910
void Assembler::sqrt_s(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, S, f0, fs, fd, SQRT_D);
}

2911 2912 2913 2914
void Assembler::sqrt_d(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, D, f0, fs, fd, SQRT_D);
}

2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931
void Assembler::rsqrt_s(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, S, f0, fs, fd, RSQRT_S);
}

void Assembler::rsqrt_d(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, D, f0, fs, fd, RSQRT_D);
}

void Assembler::recip_d(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, D, f0, fs, fd, RECIP_D);
}

void Assembler::recip_s(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, S, f0, fs, fd, RECIP_S);
}

// Conversions.
2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971
void Assembler::cvt_w_s(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, S, f0, fs, fd, CVT_W_S);
}

void Assembler::cvt_w_d(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, D, f0, fs, fd, CVT_W_D);
}

void Assembler::trunc_w_s(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, S, f0, fs, fd, TRUNC_W_S);
}

void Assembler::trunc_w_d(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, D, f0, fs, fd, TRUNC_W_D);
}

void Assembler::round_w_s(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, S, f0, fs, fd, ROUND_W_S);
}

void Assembler::round_w_d(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, D, f0, fs, fd, ROUND_W_D);
}

void Assembler::floor_w_s(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, S, f0, fs, fd, FLOOR_W_S);
}

void Assembler::floor_w_d(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, D, f0, fs, fd, FLOOR_W_D);
}

void Assembler::ceil_w_s(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, S, f0, fs, fd, CEIL_W_S);
}

void Assembler::ceil_w_d(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, D, f0, fs, fd, CEIL_W_D);
}

2972 2973 2974 2975 2976
void Assembler::rint_s(FPURegister fd, FPURegister fs) { rint(S, fd, fs); }

void Assembler::rint_d(FPURegister fd, FPURegister fs) { rint(D, fd, fs); }

void Assembler::rint(SecondaryField fmt, FPURegister fd, FPURegister fs) {
2977
  DCHECK_EQ(kArchVariant, kMips64r6);
2978
  GenInstrRegister(COP1, fmt, f0, fs, fd, RINT);
2979 2980
}

2981
void Assembler::cvt_l_s(FPURegister fd, FPURegister fs) {
2982
  DCHECK(kArchVariant == kMips64r2 || kArchVariant == kMips64r6);
2983 2984 2985 2986
  GenInstrRegister(COP1, S, f0, fs, fd, CVT_L_S);
}

void Assembler::cvt_l_d(FPURegister fd, FPURegister fs) {
2987
  DCHECK(kArchVariant == kMips64r2 || kArchVariant == kMips64r6);
2988 2989 2990 2991
  GenInstrRegister(COP1, D, f0, fs, fd, CVT_L_D);
}

void Assembler::trunc_l_s(FPURegister fd, FPURegister fs) {
2992
  DCHECK(kArchVariant == kMips64r2 || kArchVariant == kMips64r6);
2993 2994 2995 2996
  GenInstrRegister(COP1, S, f0, fs, fd, TRUNC_L_S);
}

void Assembler::trunc_l_d(FPURegister fd, FPURegister fs) {
2997
  DCHECK(kArchVariant == kMips64r2 || kArchVariant == kMips64r6);
2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024
  GenInstrRegister(COP1, D, f0, fs, fd, TRUNC_L_D);
}

void Assembler::round_l_s(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, S, f0, fs, fd, ROUND_L_S);
}

void Assembler::round_l_d(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, D, f0, fs, fd, ROUND_L_D);
}

void Assembler::floor_l_s(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, S, f0, fs, fd, FLOOR_L_S);
}

void Assembler::floor_l_d(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, D, f0, fs, fd, FLOOR_L_D);
}

void Assembler::ceil_l_s(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, S, f0, fs, fd, CEIL_L_S);
}

void Assembler::ceil_l_d(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, D, f0, fs, fd, CEIL_L_D);
}

3025
void Assembler::class_s(FPURegister fd, FPURegister fs) {
3026
  DCHECK_EQ(kArchVariant, kMips64r6);
3027 3028 3029 3030
  GenInstrRegister(COP1, S, f0, fs, fd, CLASS_S);
}

void Assembler::class_d(FPURegister fd, FPURegister fs) {
3031
  DCHECK_EQ(kArchVariant, kMips64r6);
3032 3033 3034
  GenInstrRegister(COP1, D, f0, fs, fd, CLASS_D);
}

3035 3036
void Assembler::mina(SecondaryField fmt, FPURegister fd, FPURegister fs,
                     FPURegister ft) {
3037
  DCHECK_EQ(kArchVariant, kMips64r6);
3038
  DCHECK((fmt == D) || (fmt == S));
3039 3040 3041
  GenInstrRegister(COP1, fmt, ft, fs, fd, MINA);
}

3042 3043
void Assembler::maxa(SecondaryField fmt, FPURegister fd, FPURegister fs,
                     FPURegister ft) {
3044
  DCHECK_EQ(kArchVariant, kMips64r6);
3045
  DCHECK((fmt == D) || (fmt == S));
3046 3047 3048
  GenInstrRegister(COP1, fmt, ft, fs, fd, MAXA);
}

3049 3050 3051 3052 3053
void Assembler::cvt_s_w(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, W, f0, fs, fd, CVT_S_W);
}

void Assembler::cvt_s_l(FPURegister fd, FPURegister fs) {
3054
  DCHECK(kArchVariant == kMips64r2 || kArchVariant == kMips64r6);
3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066
  GenInstrRegister(COP1, L, f0, fs, fd, CVT_S_L);
}

void Assembler::cvt_s_d(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, D, f0, fs, fd, CVT_S_D);
}

void Assembler::cvt_d_w(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, W, f0, fs, fd, CVT_D_W);
}

void Assembler::cvt_d_l(FPURegister fd, FPURegister fs) {
3067
  DCHECK(kArchVariant == kMips64r2 || kArchVariant == kMips64r6);
3068 3069 3070 3071 3072 3073 3074
  GenInstrRegister(COP1, L, f0, fs, fd, CVT_D_L);
}

void Assembler::cvt_d_s(FPURegister fd, FPURegister fs) {
  GenInstrRegister(COP1, S, f0, fs, fd, CVT_D_S);
}

3075
// Conditions for >= MIPSr6.
3076 3077
void Assembler::cmp(FPUCondition cond, SecondaryField fmt, FPURegister fd,
                    FPURegister fs, FPURegister ft) {
3078 3079
  DCHECK_EQ(kArchVariant, kMips64r6);
  DCHECK_EQ(fmt & ~(31 << kRsShift), 0);
3080 3081
  Instr instr = COP1 | fmt | ft.code() << kFtShift | fs.code() << kFsShift |
                fd.code() << kFdShift | (0 << 5) | cond;
3082 3083 3084
  emit(instr);
}

3085 3086 3087 3088 3089 3090 3091 3092 3093 3094
void Assembler::cmp_s(FPUCondition cond, FPURegister fd, FPURegister fs,
                      FPURegister ft) {
  cmp(cond, W, fd, fs, ft);
}

void Assembler::cmp_d(FPUCondition cond, FPURegister fd, FPURegister fs,
                      FPURegister ft) {
  cmp(cond, L, fd, fs, ft);
}

3095
void Assembler::bc1eqz(int16_t offset, FPURegister ft) {
3096
  DCHECK_EQ(kArchVariant, kMips64r6);
3097
  BlockTrampolinePoolScope block_trampoline_pool(this);
3098
  Instr instr = COP1 | BC1EQZ | ft.code() << kFtShift | (offset & kImm16Mask);
3099 3100
  emit(instr);
  BlockTrampolinePoolFor(1);  // For associated delay slot.
3101 3102 3103
}

void Assembler::bc1nez(int16_t offset, FPURegister ft) {
3104
  DCHECK_EQ(kArchVariant, kMips64r6);
3105
  BlockTrampolinePoolScope block_trampoline_pool(this);
3106
  Instr instr = COP1 | BC1NEZ | ft.code() << kFtShift | (offset & kImm16Mask);
3107 3108
  emit(instr);
  BlockTrampolinePoolFor(1);  // For associated delay slot.
3109 3110 3111
}

// Conditions for < MIPSr6.
3112 3113
void Assembler::c(FPUCondition cond, SecondaryField fmt, FPURegister fs,
                  FPURegister ft, uint16_t cc) {
3114
  DCHECK_NE(kArchVariant, kMips64r6);
3115
  DCHECK(is_uint3(cc));
3116
  DCHECK(fmt == S || fmt == D);
3117
  DCHECK_EQ(fmt & ~(31 << kRsShift), 0);
3118 3119
  Instr instr = COP1 | fmt | ft.code() << kFtShift | fs.code() << kFsShift |
                cc << 8 | 3 << 4 | cond;
3120 3121 3122
  emit(instr);
}

3123 3124 3125 3126 3127 3128 3129 3130 3131 3132
void Assembler::c_s(FPUCondition cond, FPURegister fs, FPURegister ft,
                    uint16_t cc) {
  c(cond, S, fs, ft, cc);
}

void Assembler::c_d(FPUCondition cond, FPURegister fs, FPURegister ft,
                    uint16_t cc) {
  c(cond, D, fs, ft, cc);
}

3133
void Assembler::fcmp(FPURegister src1, const double src2, FPUCondition cond) {
3134
  DCHECK_EQ(src2, 0.0);
3135 3136 3137 3138 3139 3140
  mtc1(zero_reg, f14);
  cvt_d_w(f14, f14);
  c(cond, D, src1, f14, 0);
}

void Assembler::bc1f(int16_t offset, uint16_t cc) {
3141
  BlockTrampolinePoolScope block_trampoline_pool(this);
3142
  DCHECK(is_uint3(cc));
3143 3144
  Instr instr = COP1 | BC1 | cc << 18 | 0 << 16 | (offset & kImm16Mask);
  emit(instr);
3145
  BlockTrampolinePoolFor(1);  // For associated delay slot.
3146 3147 3148
}

void Assembler::bc1t(int16_t offset, uint16_t cc) {
3149
  BlockTrampolinePoolScope block_trampoline_pool(this);
3150
  DCHECK(is_uint3(cc));
3151 3152
  Instr instr = COP1 | BC1 | cc << 18 | 1 << 16 | (offset & kImm16Mask);
  emit(instr);
3153
  BlockTrampolinePoolFor(1);  // For associated delay slot.
3154 3155
}

3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178
// ---------- MSA instructions ------------
#define MSA_BRANCH_LIST(V) \
  V(bz_v, BZ_V)            \
  V(bz_b, BZ_B)            \
  V(bz_h, BZ_H)            \
  V(bz_w, BZ_W)            \
  V(bz_d, BZ_D)            \
  V(bnz_v, BNZ_V)          \
  V(bnz_b, BNZ_B)          \
  V(bnz_h, BNZ_H)          \
  V(bnz_w, BNZ_W)          \
  V(bnz_d, BNZ_D)

#define MSA_BRANCH(name, opcode)                         \
  void Assembler::name(MSARegister wt, int16_t offset) { \
    GenInstrMsaBranch(opcode, wt, offset);               \
  }

MSA_BRANCH_LIST(MSA_BRANCH)
#undef MSA_BRANCH
#undef MSA_BRANCH_LIST

#define MSA_LD_ST_LIST(V) \
3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201
  V(ld_b, LD_B, 1)        \
  V(ld_h, LD_H, 2)        \
  V(ld_w, LD_W, 4)        \
  V(ld_d, LD_D, 8)        \
  V(st_b, ST_B, 1)        \
  V(st_h, ST_H, 2)        \
  V(st_w, ST_W, 4)        \
  V(st_d, ST_D, 8)

#define MSA_LD_ST(name, opcode, b)                                   \
  void Assembler::name(MSARegister wd, const MemOperand& rs) {       \
    MemOperand source = rs;                                          \
    AdjustBaseAndOffset(&source);                                    \
    if (is_int10(source.offset())) {                                 \
      DCHECK_EQ(source.offset() % b, 0);                             \
      GenInstrMsaMI10(opcode, source.offset() / b, source.rm(), wd); \
    } else {                                                         \
      UseScratchRegisterScope temps(this);                           \
      Register scratch = temps.Acquire();                            \
      DCHECK_NE(rs.rm(), scratch);                                   \
      daddiu(scratch, source.rm(), source.offset());                 \
      GenInstrMsaMI10(opcode, 0, scratch, wd);                       \
    }                                                                \
3202 3203 3204 3205
  }

MSA_LD_ST_LIST(MSA_LD_ST)
#undef MSA_LD_ST
3206
#undef MSA_LD_ST_LIST
3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313

#define MSA_I10_LIST(V) \
  V(ldi_b, I5_DF_b)     \
  V(ldi_h, I5_DF_h)     \
  V(ldi_w, I5_DF_w)     \
  V(ldi_d, I5_DF_d)

#define MSA_I10(name, format)                           \
  void Assembler::name(MSARegister wd, int32_t imm10) { \
    GenInstrMsaI10(LDI, format, imm10, wd);             \
  }
MSA_I10_LIST(MSA_I10)
#undef MSA_I10
#undef MSA_I10_LIST

#define MSA_I5_LIST(V) \
  V(addvi, ADDVI)      \
  V(subvi, SUBVI)      \
  V(maxi_s, MAXI_S)    \
  V(maxi_u, MAXI_U)    \
  V(mini_s, MINI_S)    \
  V(mini_u, MINI_U)    \
  V(ceqi, CEQI)        \
  V(clti_s, CLTI_S)    \
  V(clti_u, CLTI_U)    \
  V(clei_s, CLEI_S)    \
  V(clei_u, CLEI_U)

#define MSA_I5_FORMAT(name, opcode, format)                       \
  void Assembler::name##_##format(MSARegister wd, MSARegister ws, \
                                  uint32_t imm5) {                \
    GenInstrMsaI5(opcode, I5_DF_##format, imm5, ws, wd);          \
  }

#define MSA_I5(name, opcode)     \
  MSA_I5_FORMAT(name, opcode, b) \
  MSA_I5_FORMAT(name, opcode, h) \
  MSA_I5_FORMAT(name, opcode, w) \
  MSA_I5_FORMAT(name, opcode, d)

MSA_I5_LIST(MSA_I5)
#undef MSA_I5
#undef MSA_I5_FORMAT
#undef MSA_I5_LIST

#define MSA_I8_LIST(V) \
  V(andi_b, ANDI_B)    \
  V(ori_b, ORI_B)      \
  V(nori_b, NORI_B)    \
  V(xori_b, XORI_B)    \
  V(bmnzi_b, BMNZI_B)  \
  V(bmzi_b, BMZI_B)    \
  V(bseli_b, BSELI_B)  \
  V(shf_b, SHF_B)      \
  V(shf_h, SHF_H)      \
  V(shf_w, SHF_W)

#define MSA_I8(name, opcode)                                            \
  void Assembler::name(MSARegister wd, MSARegister ws, uint32_t imm8) { \
    GenInstrMsaI8(opcode, imm8, ws, wd);                                \
  }

MSA_I8_LIST(MSA_I8)
#undef MSA_I8
#undef MSA_I8_LIST

#define MSA_VEC_LIST(V) \
  V(and_v, AND_V)       \
  V(or_v, OR_V)         \
  V(nor_v, NOR_V)       \
  V(xor_v, XOR_V)       \
  V(bmnz_v, BMNZ_V)     \
  V(bmz_v, BMZ_V)       \
  V(bsel_v, BSEL_V)

#define MSA_VEC(name, opcode)                                            \
  void Assembler::name(MSARegister wd, MSARegister ws, MSARegister wt) { \
    GenInstrMsaVec(opcode, wt, ws, wd);                                  \
  }

MSA_VEC_LIST(MSA_VEC)
#undef MSA_VEC
#undef MSA_VEC_LIST

#define MSA_2R_LIST(V) \
  V(pcnt, PCNT)        \
  V(nloc, NLOC)        \
  V(nlzc, NLZC)

#define MSA_2R_FORMAT(name, opcode, format)                         \
  void Assembler::name##_##format(MSARegister wd, MSARegister ws) { \
    GenInstrMsa2R(opcode, MSA_2R_DF_##format, ws, wd);              \
  }

#define MSA_2R(name, opcode)     \
  MSA_2R_FORMAT(name, opcode, b) \
  MSA_2R_FORMAT(name, opcode, h) \
  MSA_2R_FORMAT(name, opcode, w) \
  MSA_2R_FORMAT(name, opcode, d)

MSA_2R_LIST(MSA_2R)
#undef MSA_2R
#undef MSA_2R_FORMAT
#undef MSA_2R_LIST

#define MSA_FILL(format)                                              \
  void Assembler::fill_##format(MSARegister wd, Register rs) {        \
3314
    DCHECK(IsEnabled(MIPS_SIMD));                                     \
3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616
    DCHECK(rs.is_valid() && wd.is_valid());                           \
    Instr instr = MSA | MSA_2R_FORMAT | FILL | MSA_2R_DF_##format |   \
                  (rs.code() << kWsShift) | (wd.code() << kWdShift) | \
                  MSA_VEC_2R_2RF_MINOR;                               \
    emit(instr);                                                      \
  }

MSA_FILL(b)
MSA_FILL(h)
MSA_FILL(w)
MSA_FILL(d)
#undef MSA_FILL

#define MSA_2RF_LIST(V) \
  V(fclass, FCLASS)     \
  V(ftrunc_s, FTRUNC_S) \
  V(ftrunc_u, FTRUNC_U) \
  V(fsqrt, FSQRT)       \
  V(frsqrt, FRSQRT)     \
  V(frcp, FRCP)         \
  V(frint, FRINT)       \
  V(flog2, FLOG2)       \
  V(fexupl, FEXUPL)     \
  V(fexupr, FEXUPR)     \
  V(ffql, FFQL)         \
  V(ffqr, FFQR)         \
  V(ftint_s, FTINT_S)   \
  V(ftint_u, FTINT_U)   \
  V(ffint_s, FFINT_S)   \
  V(ffint_u, FFINT_U)

#define MSA_2RF_FORMAT(name, opcode, format)                        \
  void Assembler::name##_##format(MSARegister wd, MSARegister ws) { \
    GenInstrMsa2RF(opcode, MSA_2RF_DF_##format, ws, wd);            \
  }

#define MSA_2RF(name, opcode)     \
  MSA_2RF_FORMAT(name, opcode, w) \
  MSA_2RF_FORMAT(name, opcode, d)

MSA_2RF_LIST(MSA_2RF)
#undef MSA_2RF
#undef MSA_2RF_FORMAT
#undef MSA_2RF_LIST

#define MSA_3R_LIST(V)  \
  V(sll, SLL_MSA)       \
  V(sra, SRA_MSA)       \
  V(srl, SRL_MSA)       \
  V(bclr, BCLR)         \
  V(bset, BSET)         \
  V(bneg, BNEG)         \
  V(binsl, BINSL)       \
  V(binsr, BINSR)       \
  V(addv, ADDV)         \
  V(subv, SUBV)         \
  V(max_s, MAX_S)       \
  V(max_u, MAX_U)       \
  V(min_s, MIN_S)       \
  V(min_u, MIN_U)       \
  V(max_a, MAX_A)       \
  V(min_a, MIN_A)       \
  V(ceq, CEQ)           \
  V(clt_s, CLT_S)       \
  V(clt_u, CLT_U)       \
  V(cle_s, CLE_S)       \
  V(cle_u, CLE_U)       \
  V(add_a, ADD_A)       \
  V(adds_a, ADDS_A)     \
  V(adds_s, ADDS_S)     \
  V(adds_u, ADDS_U)     \
  V(ave_s, AVE_S)       \
  V(ave_u, AVE_U)       \
  V(aver_s, AVER_S)     \
  V(aver_u, AVER_U)     \
  V(subs_s, SUBS_S)     \
  V(subs_u, SUBS_U)     \
  V(subsus_u, SUBSUS_U) \
  V(subsuu_s, SUBSUU_S) \
  V(asub_s, ASUB_S)     \
  V(asub_u, ASUB_U)     \
  V(mulv, MULV)         \
  V(maddv, MADDV)       \
  V(msubv, MSUBV)       \
  V(div_s, DIV_S_MSA)   \
  V(div_u, DIV_U)       \
  V(mod_s, MOD_S)       \
  V(mod_u, MOD_U)       \
  V(dotp_s, DOTP_S)     \
  V(dotp_u, DOTP_U)     \
  V(dpadd_s, DPADD_S)   \
  V(dpadd_u, DPADD_U)   \
  V(dpsub_s, DPSUB_S)   \
  V(dpsub_u, DPSUB_U)   \
  V(pckev, PCKEV)       \
  V(pckod, PCKOD)       \
  V(ilvl, ILVL)         \
  V(ilvr, ILVR)         \
  V(ilvev, ILVEV)       \
  V(ilvod, ILVOD)       \
  V(vshf, VSHF)         \
  V(srar, SRAR)         \
  V(srlr, SRLR)         \
  V(hadd_s, HADD_S)     \
  V(hadd_u, HADD_U)     \
  V(hsub_s, HSUB_S)     \
  V(hsub_u, HSUB_U)

#define MSA_3R_FORMAT(name, opcode, format)                             \
  void Assembler::name##_##format(MSARegister wd, MSARegister ws,       \
                                  MSARegister wt) {                     \
    GenInstrMsa3R<MSARegister>(opcode, MSA_3R_DF_##format, wt, ws, wd); \
  }

#define MSA_3R_FORMAT_SLD_SPLAT(name, opcode, format)                \
  void Assembler::name##_##format(MSARegister wd, MSARegister ws,    \
                                  Register rt) {                     \
    GenInstrMsa3R<Register>(opcode, MSA_3R_DF_##format, rt, ws, wd); \
  }

#define MSA_3R(name, opcode)     \
  MSA_3R_FORMAT(name, opcode, b) \
  MSA_3R_FORMAT(name, opcode, h) \
  MSA_3R_FORMAT(name, opcode, w) \
  MSA_3R_FORMAT(name, opcode, d)

#define MSA_3R_SLD_SPLAT(name, opcode)     \
  MSA_3R_FORMAT_SLD_SPLAT(name, opcode, b) \
  MSA_3R_FORMAT_SLD_SPLAT(name, opcode, h) \
  MSA_3R_FORMAT_SLD_SPLAT(name, opcode, w) \
  MSA_3R_FORMAT_SLD_SPLAT(name, opcode, d)

MSA_3R_LIST(MSA_3R)
MSA_3R_SLD_SPLAT(sld, SLD)
MSA_3R_SLD_SPLAT(splat, SPLAT)

#undef MSA_3R
#undef MSA_3R_FORMAT
#undef MSA_3R_FORMAT_SLD_SPLAT
#undef MSA_3R_SLD_SPLAT
#undef MSA_3R_LIST

#define MSA_3RF_LIST1(V) \
  V(fcaf, FCAF)          \
  V(fcun, FCUN)          \
  V(fceq, FCEQ)          \
  V(fcueq, FCUEQ)        \
  V(fclt, FCLT)          \
  V(fcult, FCULT)        \
  V(fcle, FCLE)          \
  V(fcule, FCULE)        \
  V(fsaf, FSAF)          \
  V(fsun, FSUN)          \
  V(fseq, FSEQ)          \
  V(fsueq, FSUEQ)        \
  V(fslt, FSLT)          \
  V(fsult, FSULT)        \
  V(fsle, FSLE)          \
  V(fsule, FSULE)        \
  V(fadd, FADD)          \
  V(fsub, FSUB)          \
  V(fmul, FMUL)          \
  V(fdiv, FDIV)          \
  V(fmadd, FMADD)        \
  V(fmsub, FMSUB)        \
  V(fexp2, FEXP2)        \
  V(fmin, FMIN)          \
  V(fmin_a, FMIN_A)      \
  V(fmax, FMAX)          \
  V(fmax_a, FMAX_A)      \
  V(fcor, FCOR)          \
  V(fcune, FCUNE)        \
  V(fcne, FCNE)          \
  V(fsor, FSOR)          \
  V(fsune, FSUNE)        \
  V(fsne, FSNE)

#define MSA_3RF_LIST2(V) \
  V(fexdo, FEXDO)        \
  V(ftq, FTQ)            \
  V(mul_q, MUL_Q)        \
  V(madd_q, MADD_Q)      \
  V(msub_q, MSUB_Q)      \
  V(mulr_q, MULR_Q)      \
  V(maddr_q, MADDR_Q)    \
  V(msubr_q, MSUBR_Q)

#define MSA_3RF_FORMAT(name, opcode, df, df_c)                \
  void Assembler::name##_##df(MSARegister wd, MSARegister ws, \
                              MSARegister wt) {               \
    GenInstrMsa3RF(opcode, df_c, wt, ws, wd);                 \
  }

#define MSA_3RF_1(name, opcode)      \
  MSA_3RF_FORMAT(name, opcode, w, 0) \
  MSA_3RF_FORMAT(name, opcode, d, 1)

#define MSA_3RF_2(name, opcode)      \
  MSA_3RF_FORMAT(name, opcode, h, 0) \
  MSA_3RF_FORMAT(name, opcode, w, 1)

MSA_3RF_LIST1(MSA_3RF_1)
MSA_3RF_LIST2(MSA_3RF_2)
#undef MSA_3RF_1
#undef MSA_3RF_2
#undef MSA_3RF_FORMAT
#undef MSA_3RF_LIST1
#undef MSA_3RF_LIST2

void Assembler::sldi_b(MSARegister wd, MSARegister ws, uint32_t n) {
  GenInstrMsaElm<MSARegister, MSARegister>(SLDI, ELM_DF_B, n, ws, wd);
}

void Assembler::sldi_h(MSARegister wd, MSARegister ws, uint32_t n) {
  GenInstrMsaElm<MSARegister, MSARegister>(SLDI, ELM_DF_H, n, ws, wd);
}

void Assembler::sldi_w(MSARegister wd, MSARegister ws, uint32_t n) {
  GenInstrMsaElm<MSARegister, MSARegister>(SLDI, ELM_DF_W, n, ws, wd);
}

void Assembler::sldi_d(MSARegister wd, MSARegister ws, uint32_t n) {
  GenInstrMsaElm<MSARegister, MSARegister>(SLDI, ELM_DF_D, n, ws, wd);
}

void Assembler::splati_b(MSARegister wd, MSARegister ws, uint32_t n) {
  GenInstrMsaElm<MSARegister, MSARegister>(SPLATI, ELM_DF_B, n, ws, wd);
}

void Assembler::splati_h(MSARegister wd, MSARegister ws, uint32_t n) {
  GenInstrMsaElm<MSARegister, MSARegister>(SPLATI, ELM_DF_H, n, ws, wd);
}

void Assembler::splati_w(MSARegister wd, MSARegister ws, uint32_t n) {
  GenInstrMsaElm<MSARegister, MSARegister>(SPLATI, ELM_DF_W, n, ws, wd);
}

void Assembler::splati_d(MSARegister wd, MSARegister ws, uint32_t n) {
  GenInstrMsaElm<MSARegister, MSARegister>(SPLATI, ELM_DF_D, n, ws, wd);
}

void Assembler::copy_s_b(Register rd, MSARegister ws, uint32_t n) {
  GenInstrMsaElm<Register, MSARegister>(COPY_S, ELM_DF_B, n, ws, rd);
}

void Assembler::copy_s_h(Register rd, MSARegister ws, uint32_t n) {
  GenInstrMsaElm<Register, MSARegister>(COPY_S, ELM_DF_H, n, ws, rd);
}

void Assembler::copy_s_w(Register rd, MSARegister ws, uint32_t n) {
  GenInstrMsaElm<Register, MSARegister>(COPY_S, ELM_DF_W, n, ws, rd);
}

void Assembler::copy_s_d(Register rd, MSARegister ws, uint32_t n) {
  GenInstrMsaElm<Register, MSARegister>(COPY_S, ELM_DF_D, n, ws, rd);
}

void Assembler::copy_u_b(Register rd, MSARegister ws, uint32_t n) {
  GenInstrMsaElm<Register, MSARegister>(COPY_U, ELM_DF_B, n, ws, rd);
}

void Assembler::copy_u_h(Register rd, MSARegister ws, uint32_t n) {
  GenInstrMsaElm<Register, MSARegister>(COPY_U, ELM_DF_H, n, ws, rd);
}

void Assembler::copy_u_w(Register rd, MSARegister ws, uint32_t n) {
  GenInstrMsaElm<Register, MSARegister>(COPY_U, ELM_DF_W, n, ws, rd);
}

void Assembler::insert_b(MSARegister wd, uint32_t n, Register rs) {
  GenInstrMsaElm<MSARegister, Register>(INSERT, ELM_DF_B, n, rs, wd);
}

void Assembler::insert_h(MSARegister wd, uint32_t n, Register rs) {
  GenInstrMsaElm<MSARegister, Register>(INSERT, ELM_DF_H, n, rs, wd);
}

void Assembler::insert_w(MSARegister wd, uint32_t n, Register rs) {
  GenInstrMsaElm<MSARegister, Register>(INSERT, ELM_DF_W, n, rs, wd);
}

void Assembler::insert_d(MSARegister wd, uint32_t n, Register rs) {
  GenInstrMsaElm<MSARegister, Register>(INSERT, ELM_DF_D, n, rs, wd);
}

void Assembler::insve_b(MSARegister wd, uint32_t n, MSARegister ws) {
  GenInstrMsaElm<MSARegister, MSARegister>(INSVE, ELM_DF_B, n, ws, wd);
}

void Assembler::insve_h(MSARegister wd, uint32_t n, MSARegister ws) {
  GenInstrMsaElm<MSARegister, MSARegister>(INSVE, ELM_DF_H, n, ws, wd);
}

void Assembler::insve_w(MSARegister wd, uint32_t n, MSARegister ws) {
  GenInstrMsaElm<MSARegister, MSARegister>(INSVE, ELM_DF_W, n, ws, wd);
}

void Assembler::insve_d(MSARegister wd, uint32_t n, MSARegister ws) {
  GenInstrMsaElm<MSARegister, MSARegister>(INSVE, ELM_DF_D, n, ws, wd);
}

void Assembler::move_v(MSARegister wd, MSARegister ws) {
3617
  DCHECK(IsEnabled(MIPS_SIMD));
3618 3619 3620 3621 3622 3623 3624
  DCHECK(ws.is_valid() && wd.is_valid());
  Instr instr = MSA | MOVE_V | (ws.code() << kWsShift) |
                (wd.code() << kWdShift) | MSA_ELM_MINOR;
  emit(instr);
}

void Assembler::ctcmsa(MSAControlRegister cd, Register rs) {
3625
  DCHECK(IsEnabled(MIPS_SIMD));
3626 3627 3628 3629 3630 3631 3632
  DCHECK(cd.is_valid() && rs.is_valid());
  Instr instr = MSA | CTCMSA | (rs.code() << kWsShift) |
                (cd.code() << kWdShift) | MSA_ELM_MINOR;
  emit(instr);
}

void Assembler::cfcmsa(Register rd, MSAControlRegister cs) {
3633
  DCHECK(IsEnabled(MIPS_SIMD));
3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669
  DCHECK(rd.is_valid() && cs.is_valid());
  Instr instr = MSA | CFCMSA | (cs.code() << kWsShift) |
                (rd.code() << kWdShift) | MSA_ELM_MINOR;
  emit(instr);
}

#define MSA_BIT_LIST(V) \
  V(slli, SLLI)         \
  V(srai, SRAI)         \
  V(srli, SRLI)         \
  V(bclri, BCLRI)       \
  V(bseti, BSETI)       \
  V(bnegi, BNEGI)       \
  V(binsli, BINSLI)     \
  V(binsri, BINSRI)     \
  V(sat_s, SAT_S)       \
  V(sat_u, SAT_U)       \
  V(srari, SRARI)       \
  V(srlri, SRLRI)

#define MSA_BIT_FORMAT(name, opcode, format)                      \
  void Assembler::name##_##format(MSARegister wd, MSARegister ws, \
                                  uint32_t m) {                   \
    GenInstrMsaBit(opcode, BIT_DF_##format, m, ws, wd);           \
  }

#define MSA_BIT(name, opcode)     \
  MSA_BIT_FORMAT(name, opcode, b) \
  MSA_BIT_FORMAT(name, opcode, h) \
  MSA_BIT_FORMAT(name, opcode, w) \
  MSA_BIT_FORMAT(name, opcode, d)

MSA_BIT_LIST(MSA_BIT)
#undef MSA_BIT
#undef MSA_BIT_FORMAT
#undef MSA_BIT_LIST
3670

3671
int Assembler::RelocateInternalReference(RelocInfo::Mode rmode, Address pc,
3672 3673 3674 3675 3676 3677 3678 3679 3680
                                         intptr_t pc_delta) {
  if (RelocInfo::IsInternalReference(rmode)) {
    int64_t* p = reinterpret_cast<int64_t*>(pc);
    if (*p == kEndOfJumpChain) {
      return 0;  // Number of instructions patched.
    }
    *p += pc_delta;
    return 2;  // Number of instructions patched.
  }
3681
  Instr instr = instr_at(pc);
3682
  DCHECK(RelocInfo::IsInternalReferenceEncoded(rmode));
3683
  if (IsLui(instr)) {
3684 3685 3686
    Instr instr_lui = instr_at(pc + 0 * kInstrSize);
    Instr instr_ori = instr_at(pc + 1 * kInstrSize);
    Instr instr_ori2 = instr_at(pc + 3 * kInstrSize);
3687 3688
    DCHECK(IsOri(instr_ori));
    DCHECK(IsOri(instr_ori2));
3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699
    // TODO(plind): symbolic names for the shifts.
    int64_t imm = (instr_lui & static_cast<int64_t>(kImm16Mask)) << 48;
    imm |= (instr_ori & static_cast<int64_t>(kImm16Mask)) << 32;
    imm |= (instr_ori2 & static_cast<int64_t>(kImm16Mask)) << 16;
    // Sign extend address.
    imm >>= 16;

    if (imm == kEndOfJumpChain) {
      return 0;  // Number of instructions patched.
    }
    imm += pc_delta;
3700
    DCHECK_EQ(imm & 3, 0);
3701 3702 3703 3704 3705

    instr_lui &= ~kImm16Mask;
    instr_ori &= ~kImm16Mask;
    instr_ori2 &= ~kImm16Mask;

3706 3707 3708
    instr_at_put(pc + 0 * kInstrSize, instr_lui | ((imm >> 32) & kImm16Mask));
    instr_at_put(pc + 1 * kInstrSize, instr_ori | (imm >> 16 & kImm16Mask));
    instr_at_put(pc + 3 * kInstrSize, instr_ori2 | (imm & kImm16Mask));
3709
    return 4;  // Number of instructions patched.
3710 3711
  } else if (IsJ(instr) || IsJal(instr)) {
    // Regular j/jal relocation.
3712 3713 3714 3715
    uint32_t imm28 = (instr & static_cast<int32_t>(kImm26Mask)) << 2;
    imm28 += pc_delta;
    imm28 &= kImm28Mask;
    instr &= ~kImm26Mask;
3716
    DCHECK_EQ(imm28 & 3, 0);
3717
    uint32_t imm26 = static_cast<uint32_t>(imm28 >> 2);
3718 3719
    instr_at_put(pc, instr | (imm26 & kImm26Mask));
    return 1;  // Number of instructions patched.
3720
  } else {
3721 3722
    DCHECK(((instr & kJumpRawMask) == kJRawMark) ||
           ((instr & kJumpRawMask) == kJalRawMark));
3723 3724 3725 3726 3727 3728 3729
    // Unbox raw offset and emit j/jal.
    int32_t imm28 = (instr & static_cast<int32_t>(kImm26Mask)) << 2;
    // Sign extend 28-bit offset to 32-bit.
    imm28 = (imm28 << 4) >> 4;
    uint64_t target =
        static_cast<int64_t>(imm28) + reinterpret_cast<uint64_t>(pc);
    target &= kImm28Mask;
3730
    DCHECK_EQ(imm28 & 3, 0);
3731 3732 3733 3734 3735
    uint32_t imm26 = static_cast<uint32_t>(target >> 2);
    // Check markings whether to emit j or jal.
    uint32_t unbox = (instr & kJRawMark) ? J : JAL;
    instr_at_put(pc, unbox | (imm26 & kImm26Mask));
    return 1;  // Number of instructions patched.
3736 3737 3738 3739 3740
  }
}

void Assembler::GrowBuffer() {
  // Compute new buffer size.
3741 3742
  int old_size = buffer_->size();
  int new_size = std::min(2 * old_size, old_size + 1 * MB);
3743 3744 3745

  // Some internal data structures overflow for very large buffers,
  // they must ensure that kMaximalBufferSize is not too large.
3746
  if (new_size > kMaximalBufferSize) {
3747
    V8::FatalProcessOutOfMemory(nullptr, "Assembler::GrowBuffer");
3748
  }
3749 3750

  // Set up new buffer.
3751 3752 3753
  std::unique_ptr<AssemblerBuffer> new_buffer = buffer_->Grow(new_size);
  DCHECK_EQ(new_size, new_buffer->size());
  byte* new_start = new_buffer->start();
3754 3755

  // Copy the data.
3756 3757 3758 3759 3760 3761
  intptr_t pc_delta = new_start - buffer_start_;
  intptr_t rc_delta = (new_start + new_size) - (buffer_start_ + old_size);
  size_t reloc_size = (buffer_start_ + old_size) - reloc_info_writer.pos();
  MemMove(new_start, buffer_start_, pc_offset());
  MemMove(reloc_info_writer.pos() + rc_delta, reloc_info_writer.pos(),
          reloc_size);
3762 3763

  // Switch buffers.
3764 3765
  buffer_ = std::move(new_buffer);
  buffer_start_ = new_start;
3766
  pc_ += pc_delta;
3767
  pc_for_safepoint_ += pc_delta;
3768 3769 3770 3771
  reloc_info_writer.Reposition(reloc_info_writer.pos() + rc_delta,
                               reloc_info_writer.last_pc() + pc_delta);

  // Relocate runtime entries.
Liu Yu's avatar
Liu Yu committed
3772 3773
  base::Vector<byte> instructions{buffer_start_,
                                  static_cast<size_t>(pc_offset())};
3774
  base::Vector<const byte> reloc_info{reloc_info_writer.pos(), reloc_size};
3775
  for (RelocIterator it(instructions, reloc_info, 0); !it.done(); it.next()) {
3776
    RelocInfo::Mode rmode = it.rinfo()->rmode();
3777
    if (rmode == RelocInfo::INTERNAL_REFERENCE) {
3778
      RelocateInternalReference(rmode, it.rinfo()->pc(), pc_delta);
3779 3780
    }
  }
3781 3782

  DCHECK(!overflow());
3783 3784 3785
}

void Assembler::db(uint8_t data) {
3786
  CheckForEmitInForbiddenSlot();
3787 3788
  *reinterpret_cast<uint8_t*>(pc_) = data;
  pc_ += sizeof(uint8_t);
3789 3790
}

3791
void Assembler::dd(uint32_t data, RelocInfo::Mode rmode) {
3792
  CheckForEmitInForbiddenSlot();
3793
  if (!RelocInfo::IsNoInfo(rmode)) {
3794 3795
    DCHECK(RelocInfo::IsDataEmbeddedObject(rmode) ||
           RelocInfo::IsLiteralConstant(rmode));
3796 3797
    RecordRelocInfo(rmode);
  }
3798 3799
  *reinterpret_cast<uint32_t*>(pc_) = data;
  pc_ += sizeof(uint32_t);
3800 3801
}

3802
void Assembler::dq(uint64_t data, RelocInfo::Mode rmode) {
3803
  CheckForEmitInForbiddenSlot();
3804
  if (!RelocInfo::IsNoInfo(rmode)) {
3805 3806
    DCHECK(RelocInfo::IsDataEmbeddedObject(rmode) ||
           RelocInfo::IsLiteralConstant(rmode));
3807 3808
    RecordRelocInfo(rmode);
  }
3809 3810
  *reinterpret_cast<uint64_t*>(pc_) = data;
  pc_ += sizeof(uint64_t);
3811 3812
}

3813
void Assembler::dd(Label* label) {
3814
  uint64_t data;
3815
  CheckForEmitInForbiddenSlot();
3816
  if (label->is_bound()) {
3817
    data = reinterpret_cast<uint64_t>(buffer_start_ + label->pos());
3818
  } else {
3819
    data = jump_address(label);
3820
    unbound_labels_count_++;
3821 3822
    internal_reference_positions_.insert(label->pos());
  }
3823 3824
  RecordRelocInfo(RelocInfo::INTERNAL_REFERENCE);
  EmitHelper(data);
3825 3826 3827
}

void Assembler::RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data) {
3828
  if (!ShouldRecordRelocInfo(rmode)) return;
3829
  // We do not try to reuse pool constants.
3830
  RelocInfo rinfo(reinterpret_cast<Address>(pc_), rmode, data, Code());
3831 3832
  DCHECK_GE(buffer_space(), kMaxRelocSize);  // Too late to grow buffer here.
  reloc_info_writer.Write(&rinfo);
3833 3834 3835
}

void Assembler::BlockTrampolinePoolFor(int instructions) {
3836
  CheckTrampolinePoolQuick(instructions);
3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857
  BlockTrampolinePoolBefore(pc_offset() + instructions * kInstrSize);
}

void Assembler::CheckTrampolinePool() {
  // Some small sequences of instructions must not be broken up by the
  // insertion of a trampoline pool; such sequences are protected by setting
  // either trampoline_pool_blocked_nesting_ or no_trampoline_pool_before_,
  // which are both checked here. Also, recursive calls to CheckTrampolinePool
  // are blocked by trampoline_pool_blocked_nesting_.
  if ((trampoline_pool_blocked_nesting_ > 0) ||
      (pc_offset() < no_trampoline_pool_before_)) {
    // Emission is currently blocked; make sure we try again as soon as
    // possible.
    if (trampoline_pool_blocked_nesting_ > 0) {
      next_buffer_check_ = pc_offset() + kInstrSize;
    } else {
      next_buffer_check_ = no_trampoline_pool_before_;
    }
    return;
  }

3858
  DCHECK(!trampoline_emitted_);
3859
  DCHECK_GE(unbound_labels_count_, 0);
3860 3861
  if (unbound_labels_count_ > 0) {
    // First we emit jump (2 instructions), then we emit trampoline pool.
3862 3863
    {
      BlockTrampolinePoolScope block_trampoline_pool(this);
3864
      Label after_pool;
3865 3866 3867 3868 3869
      if (kArchVariant == kMips64r6) {
        bc(&after_pool);
      } else {
        b(&after_pool);
      }
3870
      nop();
3871 3872 3873

      int pool_start = pc_offset();
      for (int i = 0; i < unbound_labels_count_; i++) {
3874
        {
3875 3876 3877 3878 3879
          if (kArchVariant == kMips64r6) {
            bc(&after_pool);
            nop();
          } else {
            or_(t8, ra, zero_reg);
3880 3881
            nal();       // Read PC into ra register.
            lui(t9, 0);  // Branch delay slot.
3882 3883
            ori(t9, t9, 0);
            daddu(t9, ra, t9);
3884 3885 3886 3887
            or_(ra, t8, zero_reg);
            // Instruction jr will take or_ from the next trampoline.
            // in its branch delay slot. This is the expected behavior
            // in order to decrease size of trampoline pool.
3888 3889
            jr(t9);
          }
3890 3891
        }
      }
3892
      nop();
3893 3894 3895 3896
      // If unbound_labels_count_ is big enough, label after_pool will
      // need a trampoline too, so we must create the trampoline before
      // the bind operation to make sure function 'bind' can get this
      // information.
3897
      trampoline_ = Trampoline(pool_start, unbound_labels_count_);
3898
      bind(&after_pool);
3899 3900 3901 3902 3903 3904 3905 3906 3907

      trampoline_emitted_ = true;
      // As we are only going to emit trampoline once, we need to prevent any
      // further emission.
      next_buffer_check_ = kMaxInt;
    }
  } else {
    // Number of branches to unbound label at this point is zero, so we can
    // move next buffer check to maximum.
3908 3909
    next_buffer_check_ =
        pc_offset() + kMaxBranchOffset - kTrampolineSlotsSize * 16;
3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923
  }
  return;
}

Address Assembler::target_address_at(Address pc) {
  Instr instr0 = instr_at(pc);
  Instr instr1 = instr_at(pc + 1 * kInstrSize);
  Instr instr3 = instr_at(pc + 3 * kInstrSize);

  // Interpret 4 instructions for address generated by li: See listing in
  // Assembler::set_target_address_at() just below.
  if ((GetOpcodeField(instr0) == LUI) && (GetOpcodeField(instr1) == ORI) &&
      (GetOpcodeField(instr3) == ORI)) {
    // Assemble the 48 bit value.
3924 3925 3926 3927
    int64_t addr =
        static_cast<int64_t>(((uint64_t)(GetImmediate16(instr0)) << 32) |
                             ((uint64_t)(GetImmediate16(instr1)) << 16) |
                             ((uint64_t)(GetImmediate16(instr3))));
3928 3929 3930

    // Sign extend to get canonical address.
    addr = (addr << 16) >> 16;
3931
    return static_cast<Address>(addr);
3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948
  }
  // We should never get here, force a bad address if we do.
  UNREACHABLE();
}

// On Mips64, a target address is stored in a 4-instruction sequence:
//    0: lui(rd, (j.imm64_ >> 32) & kImm16Mask);
//    1: ori(rd, rd, (j.imm64_ >> 16) & kImm16Mask);
//    2: dsll(rd, rd, 16);
//    3: ori(rd, rd, j.imm32_ & kImm16Mask);
//
// Patching the address must replace all the lui & ori instructions,
// and flush the i-cache.
//
// There is an optimization below, which emits a nop when the address
// fits in just 16 bits. This is unlikely to help, and should be benchmarked,
// and possibly removed.
3949
void Assembler::set_target_value_at(Address pc, uint64_t target,
3950 3951 3952 3953 3954 3955
                                    ICacheFlushMode icache_flush_mode) {
  // There is an optimization where only 4 instructions are used to load address
  // in code on MIP64 because only 48-bits of address is effectively used.
  // It relies on fact the upper [63:48] bits are not used for virtual address
  // translation and they have to be set according to value of bit 47 in order
  // get canonical address.
3956 3957 3958 3959 3960 3961 3962 3963
  Instr instr1 = instr_at(pc + kInstrSize);
  uint32_t rt_code = GetRt(instr1);
  uint32_t* p = reinterpret_cast<uint32_t*>(pc);

#ifdef DEBUG
  // Check we have the result from a li macro-instruction.
  Instr instr0 = instr_at(pc);
  Instr instr3 = instr_at(pc + kInstrSize * 3);
3964 3965
  DCHECK((GetOpcodeField(instr0) == LUI && GetOpcodeField(instr1) == ORI &&
          GetOpcodeField(instr3) == ORI));
3966 3967 3968 3969 3970 3971 3972
#endif

  // Must use 4 instructions to insure patchable code.
  // lui rt, upper-16.
  // ori rt, rt, lower-16.
  // dsll rt, rt, 16.
  // ori rt rt, lower-16.
3973 3974 3975 3976 3977
  *p = LUI | (rt_code << kRtShift) | ((target >> 32) & kImm16Mask);
  *(p + 1) = ORI | (rt_code << kRtShift) | (rt_code << kRsShift) |
             ((target >> 16) & kImm16Mask);
  *(p + 3) = ORI | (rt_code << kRsShift) | (rt_code << kRtShift) |
             (target & kImm16Mask);
3978 3979

  if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
3980
    FlushInstructionCache(pc, 4 * kInstrSize);
3981 3982 3983
  }
}

3984 3985 3986 3987 3988 3989 3990 3991 3992
UseScratchRegisterScope::UseScratchRegisterScope(Assembler* assembler)
    : available_(assembler->GetScratchRegisterList()),
      old_available_(*available_) {}

UseScratchRegisterScope::~UseScratchRegisterScope() {
  *available_ = old_available_;
}

Register UseScratchRegisterScope::Acquire() {
3993 3994
  DCHECK_NOT_NULL(available_);
  DCHECK_NE(*available_, 0);
3995 3996 3997 3998 3999 4000 4001 4002
  int index = static_cast<int>(base::bits::CountTrailingZeros32(*available_));
  *available_ &= ~(1UL << index);

  return Register::from_code(index);
}

bool UseScratchRegisterScope::hasAvailable() const { return *available_ != 0; }

4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022
LoadStoreLaneParams::LoadStoreLaneParams(MachineRepresentation rep,
                                         uint8_t laneidx) {
  switch (rep) {
    case MachineRepresentation::kWord8:
      *this = LoadStoreLaneParams(laneidx, MSA_B, 16);
      break;
    case MachineRepresentation::kWord16:
      *this = LoadStoreLaneParams(laneidx, MSA_H, 8);
      break;
    case MachineRepresentation::kWord32:
      *this = LoadStoreLaneParams(laneidx, MSA_W, 4);
      break;
    case MachineRepresentation::kWord64:
      *this = LoadStoreLaneParams(laneidx, MSA_D, 2);
      break;
    default:
      UNREACHABLE();
  }
}

4023 4024
}  // namespace internal
}  // namespace v8
4025 4026

#endif  // V8_TARGET_ARCH_MIPS64