assembler-x64.cc 94.8 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6
#include "src/x64/assembler-x64.h"

7 8 9 10
#include <cstring>

#if V8_TARGET_ARCH_X64

11 12 13
#if V8_LIBC_MSVCRT
#include <intrin.h>  // _xgetbv()
#endif
14 15 16
#if V8_OS_MACOSX
#include <sys/sysctl.h>
#endif
17

18
#include "src/base/bits.h"
19
#include "src/macro-assembler.h"
20
#include "src/v8.h"
21

22 23
namespace v8 {
namespace internal {
24

25 26
// -----------------------------------------------------------------------------
// Implementation of CpuFeatures
27

28 29
namespace {

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#if !V8_LIBC_MSVCRT

V8_INLINE uint64_t _xgetbv(unsigned int xcr) {
  unsigned eax, edx;
  // Check xgetbv; this uses a .byte sequence instead of the instruction
  // directly because older assemblers do not include support for xgetbv and
  // there is no easy way to conditionally compile based on the assembler
  // used.
  __asm__ volatile(".byte 0x0f, 0x01, 0xd0" : "=a"(eax), "=d"(edx) : "c"(xcr));
  return static_cast<uint64_t>(eax) | (static_cast<uint64_t>(edx) << 32);
}

#define _XCR_XFEATURE_ENABLED_MASK 0

#endif  // !V8_LIBC_MSVCRT


bool OSHasAVXSupport() {
48
#if V8_OS_MACOSX
49 50
  // Mac OS X up to 10.9 has a bug where AVX transitions were indeed being
  // caused by ISRs, so we detect that here and disable AVX in that case.
51 52
  char buffer[128];
  size_t buffer_size = arraysize(buffer);
53
  int ctl_name[] = {CTL_KERN, KERN_OSRELEASE};
54 55 56 57
  if (sysctl(ctl_name, 2, buffer, &buffer_size, nullptr, 0) != 0) {
    V8_Fatal(__FILE__, __LINE__, "V8 failed to get kernel version");
  }
  // The buffer now contains a string of the form XX.YY.ZZ, where
58 59 60 61 62 63
  // XX is the major kernel version component.
  char* period_pos = strchr(buffer, '.');
  DCHECK_NOT_NULL(period_pos);
  *period_pos = '\0';
  long kernel_version_major = strtol(buffer, nullptr, 10);  // NOLINT
  if (kernel_version_major <= 13) return false;
64
#endif  // V8_OS_MACOSX
65 66 67
  // Check whether OS claims to support AVX.
  uint64_t feature_mask = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
  return (feature_mask & 0x6) == 0x6;
68 69 70 71 72
}

}  // namespace


73
void CpuFeatures::ProbeImpl(bool cross_compile) {
74
  base::CPU cpu;
75 76
  CHECK(cpu.has_sse2());  // SSE2 support is mandatory.
  CHECK(cpu.has_cmov());  // CMOV support is mandatory.
77

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

81 82
  if (cpu.has_sse41() && FLAG_enable_sse4_1) supported_ |= 1u << SSE4_1;
  if (cpu.has_sse3() && FLAG_enable_sse3) supported_ |= 1u << SSE3;
83
  // SAHF is not generally available in long mode.
84
  if (cpu.has_sahf() && FLAG_enable_sahf) supported_ |= 1u << SAHF;
85 86 87 88 89 90 91 92
  if (cpu.has_avx() && FLAG_enable_avx && cpu.has_osxsave() &&
      OSHasAVXSupport()) {
    supported_ |= 1u << AVX;
  }
  if (cpu.has_fma3() && FLAG_enable_fma3 && cpu.has_osxsave() &&
      OSHasAVXSupport()) {
    supported_ |= 1u << FMA3;
  }
93 94 95 96
  if (cpu.has_bmi1() && FLAG_enable_bmi1) supported_ |= 1u << BMI1;
  if (cpu.has_bmi2() && FLAG_enable_bmi2) supported_ |= 1u << BMI2;
  if (cpu.has_lzcnt() && FLAG_enable_lzcnt) supported_ |= 1u << LZCNT;
  if (cpu.has_popcnt() && FLAG_enable_popcnt) supported_ |= 1u << POPCNT;
97 98 99 100 101
  if (strcmp(FLAG_mcpu, "auto") == 0) {
    if (cpu.is_atom()) supported_ |= 1u << ATOM;
  } else if (strcmp(FLAG_mcpu, "atom") == 0) {
    supported_ |= 1u << ATOM;
  }
102 103
}

104

105
void CpuFeatures::PrintTarget() { }
106
void CpuFeatures::PrintFeatures() {
107 108 109 110 111 112 113 114
  printf(
      "SSE3=%d SSE4_1=%d SAHF=%d AVX=%d FMA3=%d BMI1=%d BMI2=%d LZCNT=%d "
      "POPCNT=%d ATOM=%d\n",
      CpuFeatures::IsSupported(SSE3), CpuFeatures::IsSupported(SSE4_1),
      CpuFeatures::IsSupported(SAHF), CpuFeatures::IsSupported(AVX),
      CpuFeatures::IsSupported(FMA3), CpuFeatures::IsSupported(BMI1),
      CpuFeatures::IsSupported(BMI2), CpuFeatures::IsSupported(LZCNT),
      CpuFeatures::IsSupported(POPCNT), CpuFeatures::IsSupported(ATOM));
115
}
116 117


118 119 120
// -----------------------------------------------------------------------------
// Implementation of Operand

121
Operand::Operand(Register base, int32_t disp) : rex_(0) {
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  len_ = 1;
  if (base.is(rsp) || base.is(r12)) {
    // SIB byte is needed to encode (rsp + offset) or (r12 + offset).
    set_sib(times_1, rsp, base);
  }

  if (disp == 0 && !base.is(rbp) && !base.is(r13)) {
    set_modrm(0, base);
  } else if (is_int8(disp)) {
    set_modrm(1, base);
    set_disp8(disp);
  } else {
    set_modrm(2, base);
    set_disp32(disp);
  }
}


Operand::Operand(Register base,
                 Register index,
                 ScaleFactor scale,
143
                 int32_t disp) : rex_(0) {
144
  DCHECK(!index.is(rsp));
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  len_ = 1;
  set_sib(scale, index, base);
  if (disp == 0 && !base.is(rbp) && !base.is(r13)) {
    // This call to set_modrm doesn't overwrite the REX.B (or REX.X) bits
    // possibly set by set_sib.
    set_modrm(0, rsp);
  } else if (is_int8(disp)) {
    set_modrm(1, rsp);
    set_disp8(disp);
  } else {
    set_modrm(2, rsp);
    set_disp32(disp);
  }
}


161 162 163
Operand::Operand(Register index,
                 ScaleFactor scale,
                 int32_t disp) : rex_(0) {
164
  DCHECK(!index.is(rsp));
165 166 167 168 169 170 171
  len_ = 1;
  set_modrm(0, rsp);
  set_sib(scale, index, rbp);
  set_disp32(disp);
}


172 173 174 175 176 177 178
Operand::Operand(Label* label) : rex_(0), len_(1) {
  DCHECK_NOT_NULL(label);
  set_modrm(0, rbp);
  set_disp64(reinterpret_cast<intptr_t>(label));
}


179
Operand::Operand(const Operand& operand, int32_t offset) {
180
  DCHECK(operand.len_ >= 1);
181 182
  // Operand encodes REX ModR/M [SIB] [Disp].
  byte modrm = operand.buf_[0];
183
  DCHECK(modrm < 0xC0);  // Disallow mode 3 (register target).
184 185 186 187 188 189 190 191 192 193
  bool has_sib = ((modrm & 0x07) == 0x04);
  byte mode = modrm & 0xC0;
  int disp_offset = has_sib ? 2 : 1;
  int base_reg = (has_sib ? operand.buf_[1] : modrm) & 0x07;
  // Mode 0 with rbp/r13 as ModR/M or SIB base register always has a 32-bit
  // displacement.
  bool is_baseless = (mode == 0) && (base_reg == 0x05);  // No base or RIP base.
  int32_t disp_value = 0;
  if (mode == 0x80 || is_baseless) {
    // Mode 2 or mode 0 with rbp/r13 as base: Word displacement.
194
    disp_value = *bit_cast<const int32_t*>(&operand.buf_[disp_offset]);
195 196 197 198 199 200
  } else if (mode == 0x40) {
    // Mode 1: Byte displacement.
    disp_value = static_cast<signed char>(operand.buf_[disp_offset]);
  }

  // Write new operand with same registers, but with modified displacement.
201
  DCHECK(offset >= 0 ? disp_value + offset > disp_value
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
                     : disp_value + offset < disp_value);  // No overflow.
  disp_value += offset;
  rex_ = operand.rex_;
  if (!is_int8(disp_value) || is_baseless) {
    // Need 32 bits of displacement, mode 2 or mode 1 with register rbp/r13.
    buf_[0] = (modrm & 0x3f) | (is_baseless ? 0x00 : 0x80);
    len_ = disp_offset + 4;
    Memory::int32_at(&buf_[disp_offset]) = disp_value;
  } else if (disp_value != 0 || (base_reg == 0x05)) {
    // Need 8 bits of displacement.
    buf_[0] = (modrm & 0x3f) | 0x40;  // Mode 1.
    len_ = disp_offset + 1;
    buf_[disp_offset] = static_cast<byte>(disp_value);
  } else {
    // Need no displacement.
    buf_[0] = (modrm & 0x3f);  // Mode 0.
    len_ = disp_offset;
  }
  if (has_sib) {
    buf_[1] = operand.buf_[1];
  }
}

225 226 227

bool Operand::AddressUsesRegister(Register reg) const {
  int code = reg.code();
228
  DCHECK((buf_[0] & 0xC0) != 0xC0);  // Always a memory operand.
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
  // Start with only low three bits of base register. Initial decoding doesn't
  // distinguish on the REX.B bit.
  int base_code = buf_[0] & 0x07;
  if (base_code == rsp.code()) {
    // SIB byte present in buf_[1].
    // Check the index register from the SIB byte + REX.X prefix.
    int index_code = ((buf_[1] >> 3) & 0x07) | ((rex_ & 0x02) << 2);
    // Index code (including REX.X) of 0x04 (rsp) means no index register.
    if (index_code != rsp.code() && index_code == code) return true;
    // Add REX.B to get the full base register code.
    base_code = (buf_[1] & 0x07) | ((rex_ & 0x01) << 3);
    // A base register of 0x05 (rbp) with mod = 0 means no base register.
    if (base_code == rbp.code() && ((buf_[0] & 0xC0) == 0)) return false;
    return code == base_code;
  } else {
    // A base register with low bits of 0x05 (rbp or r13) and mod = 0 means
    // no base register.
    if (base_code == rbp.code() && ((buf_[0] & 0xC0) == 0)) return false;
    base_code |= ((rex_ & 0x01) << 3);
    return code == base_code;
  }
}


253
// -----------------------------------------------------------------------------
254
// Implementation of Assembler.
255

256 257 258
#ifdef GENERATED_CODE_COVERAGE
static void InitCoverageLog();
#endif
259

260 261
Assembler::Assembler(Isolate* isolate, void* buffer, int buffer_size)
    : AssemblerBase(isolate, buffer, buffer_size),
262
      code_targets_(100),
263
      positions_recorder_(this) {
264 265
  // Clear the buffer in debug mode unless it was provided by the
  // caller in which case we can't be sure it's okay to overwrite
266
  // existing code in it.
267 268
#ifdef DEBUG
  if (own_buffer_) {
269
    memset(buffer_, 0xCC, buffer_size_);  // int3
270 271 272
  }
#endif

273
  reloc_info_writer.Reposition(buffer_ + buffer_size_, pc_);
274

275

276 277 278 279 280 281 282
#ifdef GENERATED_CODE_COVERAGE
  InitCoverageLog();
#endif
}


void Assembler::GetCode(CodeDesc* desc) {
283 284
  // Finalize code (at this point overflow() may be true, but the gap ensures
  // that we are still not overlapping instructions and relocation info).
285
  reloc_info_writer.Finish();
286
  DCHECK(pc_ <= reloc_info_writer.pos());  // No overlap.
287
  // Set up code descriptor.
288 289 290
  desc->buffer = buffer_;
  desc->buffer_size = buffer_size_;
  desc->instr_size = pc_offset();
291
  DCHECK(desc->instr_size > 0);  // Zero-size code objects upset the system.
292 293
  desc->reloc_size =
      static_cast<int>((buffer_ + buffer_size_) - reloc_info_writer.pos());
294
  desc->origin = this;
295
  desc->constant_pool_size = 0;
296 297 298 299
}


void Assembler::Align(int m) {
300
  DCHECK(base::bits::IsPowerOfTwo32(m));
301
  int delta = (m - (pc_offset() & (m - 1))) & (m - 1);
302
  Nop(delta);
303 304
}

305

306 307 308 309 310
void Assembler::CodeTargetAlign() {
  Align(16);  // Preferred alignment of jump targets on x64.
}


311 312 313 314 315 316 317 318 319
bool Assembler::IsNop(Address addr) {
  Address a = addr;
  while (*a == 0x66) a++;
  if (*a == 0x90) return true;
  if (a[0] == 0xf && a[1] == 0x1f) return true;
  return false;
}


320
void Assembler::bind_to(Label* L, int pos) {
321 322
  DCHECK(!L->is_bound());  // Label may only be bound once.
  DCHECK(0 <= pos && pos <= pc_offset());  // Position must be valid.
323 324 325 326
  if (L->is_linked()) {
    int current = L->pos();
    int next = long_at(current);
    while (next != current) {
327 328 329 330 331 332 333 334 335 336
      if (current >= 4 && long_at(current - 4) == 0) {
        // Absolute address.
        intptr_t imm64 = reinterpret_cast<intptr_t>(buffer_ + pos);
        *reinterpret_cast<intptr_t*>(addr_at(current - 4)) = imm64;
        internal_reference_positions_.push_back(current - 4);
      } else {
        // Relative address, relative to point after address.
        int imm32 = pos - (current + sizeof(int32_t));
        long_at_put(current, imm32);
      }
337 338 339 340
      current = next;
      next = long_at(next);
    }
    // Fix up last fixup on linked list.
341 342 343 344 345 346 347 348 349 350
    if (current >= 4 && long_at(current - 4) == 0) {
      // Absolute address.
      intptr_t imm64 = reinterpret_cast<intptr_t>(buffer_ + pos);
      *reinterpret_cast<intptr_t*>(addr_at(current - 4)) = imm64;
      internal_reference_positions_.push_back(current - 4);
    } else {
      // Relative address, relative to point after address.
      int imm32 = pos - (current + sizeof(int32_t));
      long_at_put(current, imm32);
    }
351
  }
352 353 354 355
  while (L->is_near_linked()) {
    int fixup_pos = L->near_link_pos();
    int offset_to_next =
        static_cast<int>(*reinterpret_cast<int8_t*>(addr_at(fixup_pos)));
356
    DCHECK(offset_to_next <= 0);
357
    int disp = pos - (fixup_pos + sizeof(int8_t));
358
    CHECK(is_int8(disp));
359 360 361 362 363 364 365
    set_byte_at(fixup_pos, disp);
    if (offset_to_next < 0) {
      L->link_to(fixup_pos + offset_to_next, Label::kNear);
    } else {
      L->UnuseNear();
    }
  }
366
  L->bind_to(pos);
367 368
}

369 370 371 372 373 374

void Assembler::bind(Label* L) {
  bind_to(L, pc_offset());
}


375
void Assembler::GrowBuffer() {
376
  DCHECK(buffer_overflow());
377
  if (!own_buffer_) FATAL("external code buffer is too small");
378

379
  // Compute new buffer size.
380
  CodeDesc desc;  // the new buffer
381 382
  desc.buffer_size = 2 * buffer_size_;

383 384 385
  // Some internal data structures overflow for very large buffers,
  // they must ensure that kMaximalBufferSize is not too large.
  if ((desc.buffer_size > kMaximalBufferSize) ||
386
      (desc.buffer_size > isolate()->heap()->MaxOldGenerationSize())) {
387 388
    V8::FatalProcessOutOfMemory("Assembler::GrowBuffer");
  }
389

390
  // Set up new buffer.
391
  desc.buffer = NewArray<byte>(desc.buffer_size);
jochen's avatar
jochen committed
392
  desc.origin = this;
393
  desc.instr_size = pc_offset();
394 395
  desc.reloc_size =
      static_cast<int>((buffer_ + buffer_size_) - (reloc_info_writer.pos()));
396

397 398 399 400 401
  // Clear the buffer in debug mode. Use 'int3' instructions to make
  // sure to get into problems if we ever run uninitialized code.
#ifdef DEBUG
  memset(desc.buffer, 0xCC, desc.buffer_size);
#endif
402

403
  // Copy the data.
404 405 406
  intptr_t pc_delta = desc.buffer - buffer_;
  intptr_t rc_delta = (desc.buffer + desc.buffer_size) -
      (buffer_ + buffer_size_);
407 408 409
  MemMove(desc.buffer, buffer_, desc.instr_size);
  MemMove(rc_delta + reloc_info_writer.pos(), reloc_info_writer.pos(),
          desc.reloc_size);
410

411
  // Switch buffers.
412
  DeleteArray(buffer_);
413 414 415 416 417 418
  buffer_ = desc.buffer;
  buffer_size_ = desc.buffer_size;
  pc_ += pc_delta;
  reloc_info_writer.Reposition(reloc_info_writer.pos() + rc_delta,
                               reloc_info_writer.last_pc() + pc_delta);

419 420 421 422
  // Relocate internal references.
  for (auto pos : internal_reference_positions_) {
    intptr_t* p = reinterpret_cast<intptr_t*>(buffer_ + pos);
    *p += pc_delta;
423
  }
424

425
  DCHECK(!buffer_overflow());
426 427 428
}


429
void Assembler::emit_operand(int code, const Operand& adr) {
430
  DCHECK(is_uint3(code));
431
  const unsigned length = adr.len_;
432
  DCHECK(length > 0);
433

434
  // Emit updated ModR/M byte containing the given register.
435
  DCHECK((adr.buf_[0] & 0x38) == 0);
436 437 438 439 440
  *pc_++ = adr.buf_[0] | code << 3;

  // Recognize RIP relative addressing.
  if (adr.buf_[0] == 5) {
    DCHECK_EQ(9u, length);
441
    Label* label = *bit_cast<Label* const*>(&adr.buf_[1]);
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
    if (label->is_bound()) {
      int offset = label->pos() - pc_offset() - sizeof(int32_t);
      DCHECK_GE(0, offset);
      emitl(offset);
    } else if (label->is_linked()) {
      emitl(label->pos());
      label->link_to(pc_offset() - sizeof(int32_t));
    } else {
      DCHECK(label->is_unused());
      int32_t current = pc_offset();
      emitl(current);
      label->link_to(current);
    }
  } else {
    // Emit the rest of the encoded operand.
    for (unsigned i = 1; i < length; i++) *pc_++ = adr.buf_[i];
  }
459 460 461
}


462
// Assembler Instruction implementations.
463

464 465 466 467
void Assembler::arithmetic_op(byte opcode,
                              Register reg,
                              const Operand& op,
                              int size) {
468
  EnsureSpace ensure_space(this);
469
  emit_rex(reg, op, size);
470
  emit(opcode);
471
  emit_operand(reg, op);
472 473 474
}


475 476 477 478
void Assembler::arithmetic_op(byte opcode,
                              Register reg,
                              Register rm_reg,
                              int size) {
479
  EnsureSpace ensure_space(this);
480
  DCHECK((opcode & 0xC6) == 2);
481 482
  if (rm_reg.low_bits() == 4)  {  // Forces SIB byte.
    // Swap reg and rm_reg and change opcode operand order.
483
    emit_rex(rm_reg, reg, size);
484 485 486
    emit(opcode ^ 0x02);
    emit_modrm(rm_reg, reg);
  } else {
487
    emit_rex(reg, rm_reg, size);
488 489 490
    emit(opcode);
    emit_modrm(reg, rm_reg);
  }
491 492
}

493

494
void Assembler::arithmetic_op_16(byte opcode, Register reg, Register rm_reg) {
495
  EnsureSpace ensure_space(this);
496
  DCHECK((opcode & 0xC6) == 2);
497 498 499 500 501 502 503 504 505 506 507 508
  if (rm_reg.low_bits() == 4) {  // Forces SIB byte.
    // Swap reg and rm_reg and change opcode operand order.
    emit(0x66);
    emit_optional_rex_32(rm_reg, reg);
    emit(opcode ^ 0x02);
    emit_modrm(rm_reg, reg);
  } else {
    emit(0x66);
    emit_optional_rex_32(reg, rm_reg);
    emit(opcode);
    emit_modrm(reg, rm_reg);
  }
509 510 511 512 513 514 515 516 517 518 519 520 521 522
}


void Assembler::arithmetic_op_16(byte opcode,
                                 Register reg,
                                 const Operand& rm_reg) {
  EnsureSpace ensure_space(this);
  emit(0x66);
  emit_optional_rex_32(reg, rm_reg);
  emit(opcode);
  emit_operand(reg, rm_reg);
}


523 524 525
void Assembler::arithmetic_op_8(byte opcode, Register reg, const Operand& op) {
  EnsureSpace ensure_space(this);
  if (!reg.is_byte_register()) {
526 527 528
    emit_rex_32(reg, op);
  } else {
    emit_optional_rex_32(reg, op);
529 530 531 532 533 534 535
  }
  emit(opcode);
  emit_operand(reg, op);
}


void Assembler::arithmetic_op_8(byte opcode, Register reg, Register rm_reg) {
536
  EnsureSpace ensure_space(this);
537
  DCHECK((opcode & 0xC6) == 2);
538
  if (rm_reg.low_bits() == 4)  {  // Forces SIB byte.
539
    // Swap reg and rm_reg and change opcode operand order.
540 541 542 543 544
    if (!rm_reg.is_byte_register() || !reg.is_byte_register()) {
      // Register is not one of al, bl, cl, dl.  Its encoding needs REX.
      emit_rex_32(rm_reg, reg);
    }
    emit(opcode ^ 0x02);
545 546
    emit_modrm(rm_reg, reg);
  } else {
547 548 549 550
    if (!reg.is_byte_register() || !rm_reg.is_byte_register()) {
      // Register is not one of al, bl, cl, dl.  Its encoding needs REX.
      emit_rex_32(reg, rm_reg);
    }
551 552 553
    emit(opcode);
    emit_modrm(reg, rm_reg);
  }
554 555 556
}


557 558
void Assembler::immediate_arithmetic_op(byte subcode,
                                        Register dst,
559 560
                                        Immediate src,
                                        int size) {
561
  EnsureSpace ensure_space(this);
562
  emit_rex(dst, size);
563
  if (is_int8(src.value_)) {
564
    emit(0x83);
565
    emit_modrm(subcode, dst);
566
    emit(src.value_);
567 568 569
  } else if (dst.is(rax)) {
    emit(0x05 | (subcode << 3));
    emitl(src.value_);
570
  } else {
571
    emit(0x81);
572
    emit_modrm(subcode, dst);
573 574 575 576 577 578
    emitl(src.value_);
  }
}

void Assembler::immediate_arithmetic_op(byte subcode,
                                        const Operand& dst,
579 580
                                        Immediate src,
                                        int size) {
581
  EnsureSpace ensure_space(this);
582
  emit_rex(dst, size);
583
  if (is_int8(src.value_)) {
584
    emit(0x83);
585
    emit_operand(subcode, dst);
586
    emit(src.value_);
587
  } else {
588
    emit(0x81);
589
    emit_operand(subcode, dst);
590 591 592 593
    emitl(src.value_);
  }
}

594

595 596 597 598 599 600 601 602 603 604 605 606
void Assembler::immediate_arithmetic_op_16(byte subcode,
                                           Register dst,
                                           Immediate src) {
  EnsureSpace ensure_space(this);
  emit(0x66);  // Operand size override prefix.
  emit_optional_rex_32(dst);
  if (is_int8(src.value_)) {
    emit(0x83);
    emit_modrm(subcode, dst);
    emit(src.value_);
  } else if (dst.is(rax)) {
    emit(0x05 | (subcode << 3));
607
    emitw(src.value_);
608 609 610
  } else {
    emit(0x81);
    emit_modrm(subcode, dst);
611
    emitw(src.value_);
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
  }
}


void Assembler::immediate_arithmetic_op_16(byte subcode,
                                           const Operand& dst,
                                           Immediate src) {
  EnsureSpace ensure_space(this);
  emit(0x66);  // Operand size override prefix.
  emit_optional_rex_32(dst);
  if (is_int8(src.value_)) {
    emit(0x83);
    emit_operand(subcode, dst);
    emit(src.value_);
  } else {
    emit(0x81);
    emit_operand(subcode, dst);
629
    emitw(src.value_);
630 631 632 633
  }
}


634
void Assembler::immediate_arithmetic_op_8(byte subcode,
635 636
                                          const Operand& dst,
                                          Immediate src) {
637 638
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(dst);
639
  DCHECK(is_int8(src.value_) || is_uint8(src.value_));
640 641 642 643 644 645
  emit(0x80);
  emit_operand(subcode, dst);
  emit(src.value_);
}


646 647 648 649
void Assembler::immediate_arithmetic_op_8(byte subcode,
                                          Register dst,
                                          Immediate src) {
  EnsureSpace ensure_space(this);
650
  if (!dst.is_byte_register()) {
651 652
    // Register is not one of al, bl, cl, dl.  Its encoding needs REX.
    emit_rex_32(dst);
653
  }
654
  DCHECK(is_int8(src.value_) || is_uint8(src.value_));
655 656 657 658 659 660
  emit(0x80);
  emit_modrm(subcode, dst);
  emit(src.value_);
}


661 662 663 664
void Assembler::shift(Register dst,
                      Immediate shift_amount,
                      int subcode,
                      int size) {
665
  EnsureSpace ensure_space(this);
666
  DCHECK(size == kInt64Size ? is_uint6(shift_amount.value_)
667
                            : is_uint5(shift_amount.value_));
668
  if (shift_amount.value_ == 1) {
669
    emit_rex(dst, size);
670
    emit(0xD1);
671
    emit_modrm(subcode, dst);
672
  } else {
673
    emit_rex(dst, size);
674
    emit(0xC1);
675
    emit_modrm(subcode, dst);
676 677 678 679 680
    emit(shift_amount.value_);
  }
}


681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
void Assembler::shift(Operand dst, Immediate shift_amount, int subcode,
                      int size) {
  EnsureSpace ensure_space(this);
  DCHECK(size == kInt64Size ? is_uint6(shift_amount.value_)
                            : is_uint5(shift_amount.value_));
  if (shift_amount.value_ == 1) {
    emit_rex(dst, size);
    emit(0xD1);
    emit_operand(subcode, dst);
  } else {
    emit_rex(dst, size);
    emit(0xC1);
    emit_operand(subcode, dst);
    emit(shift_amount.value_);
  }
}


699
void Assembler::shift(Register dst, int subcode, int size) {
700
  EnsureSpace ensure_space(this);
701
  emit_rex(dst, size);
702
  emit(0xD3);
703
  emit_modrm(subcode, dst);
704 705 706
}


707 708 709 710 711 712 713 714
void Assembler::shift(Operand dst, int subcode, int size) {
  EnsureSpace ensure_space(this);
  emit_rex(dst, size);
  emit(0xD3);
  emit_operand(subcode, dst);
}


715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
void Assembler::bt(const Operand& dst, Register src) {
  EnsureSpace ensure_space(this);
  emit_rex_64(src, dst);
  emit(0x0F);
  emit(0xA3);
  emit_operand(src, dst);
}


void Assembler::bts(const Operand& dst, Register src) {
  EnsureSpace ensure_space(this);
  emit_rex_64(src, dst);
  emit(0x0F);
  emit(0xAB);
  emit_operand(src, dst);
}


733 734 735 736 737 738 739 740 741
void Assembler::bsrl(Register dst, Register src) {
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0xBD);
  emit_modrm(dst, src);
}


742 743 744 745 746 747 748 749 750
void Assembler::bsrl(Register dst, const Operand& src) {
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0xBD);
  emit_operand(dst, src);
}


751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
void Assembler::bsrq(Register dst, Register src) {
  EnsureSpace ensure_space(this);
  emit_rex_64(dst, src);
  emit(0x0F);
  emit(0xBD);
  emit_modrm(dst, src);
}


void Assembler::bsrq(Register dst, const Operand& src) {
  EnsureSpace ensure_space(this);
  emit_rex_64(dst, src);
  emit(0x0F);
  emit(0xBD);
  emit_operand(dst, src);
}


769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
void Assembler::bsfl(Register dst, Register src) {
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0xBC);
  emit_modrm(dst, src);
}


void Assembler::bsfl(Register dst, const Operand& src) {
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0xBC);
  emit_operand(dst, src);
}


787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
void Assembler::bsfq(Register dst, Register src) {
  EnsureSpace ensure_space(this);
  emit_rex_64(dst, src);
  emit(0x0F);
  emit(0xBC);
  emit_modrm(dst, src);
}


void Assembler::bsfq(Register dst, const Operand& src) {
  EnsureSpace ensure_space(this);
  emit_rex_64(dst, src);
  emit(0x0F);
  emit(0xBC);
  emit_operand(dst, src);
}


805
void Assembler::call(Label* L) {
806
  positions_recorder()->WriteRecordedPositions();
807
  EnsureSpace ensure_space(this);
808
  // 1110 1000 #32-bit disp.
809
  emit(0xE8);
810 811
  if (L->is_bound()) {
    int offset = L->pos() - pc_offset() - sizeof(int32_t);
812
    DCHECK(offset <= 0);
813
    emitl(offset);
814
  } else if (L->is_linked()) {
815
    emitl(L->pos());
816 817
    L->link_to(pc_offset() - sizeof(int32_t));
  } else {
818
    DCHECK(L->is_unused());
819
    int32_t current = pc_offset();
820
    emitl(current);
821 822
    L->link_to(current);
  }
823 824 825
}


826
void Assembler::call(Address entry, RelocInfo::Mode rmode) {
827
  DCHECK(RelocInfo::IsRuntimeEntry(rmode));
828 829 830 831 832 833 834 835
  positions_recorder()->WriteRecordedPositions();
  EnsureSpace ensure_space(this);
  // 1110 1000 #32-bit disp.
  emit(0xE8);
  emit_runtime_entry(entry, rmode);
}


836 837
void Assembler::call(Handle<Code> target,
                     RelocInfo::Mode rmode,
838
                     TypeFeedbackId ast_id) {
839
  positions_recorder()->WriteRecordedPositions();
840
  EnsureSpace ensure_space(this);
841
  // 1110 1000 #32-bit disp.
842
  emit(0xE8);
843
  emit_code_target(target, rmode, ast_id);
844 845 846
}


847
void Assembler::call(Register adr) {
848
  positions_recorder()->WriteRecordedPositions();
849
  EnsureSpace ensure_space(this);
850
  // Opcode: FF /2 r64.
851
  emit_optional_rex_32(adr);
852
  emit(0xFF);
853
  emit_modrm(0x2, adr);
854 855
}

856

lrn@chromium.org's avatar
lrn@chromium.org committed
857
void Assembler::call(const Operand& op) {
858
  positions_recorder()->WriteRecordedPositions();
lrn@chromium.org's avatar
lrn@chromium.org committed
859
  EnsureSpace ensure_space(this);
860
  // Opcode: FF /2 m64.
861
  emit_optional_rex_32(op);
lrn@chromium.org's avatar
lrn@chromium.org committed
862
  emit(0xFF);
863
  emit_operand(0x2, op);
lrn@chromium.org's avatar
lrn@chromium.org committed
864 865 866
}


867 868 869 870 871 872 873 874 875 876 877
// Calls directly to the given address using a relative offset.
// Should only ever be used in Code objects for calls within the
// same Code object. Should not be used when generating new code (use labels),
// but only when patching existing code.
void Assembler::call(Address target) {
  positions_recorder()->WriteRecordedPositions();
  EnsureSpace ensure_space(this);
  // 1110 1000 #32-bit disp.
  emit(0xE8);
  Address source = pc_ + 4;
  intptr_t displacement = target - source;
878
  DCHECK(is_int32(displacement));
879 880 881 882
  emitl(static_cast<int32_t>(displacement));
}


883 884 885 886 887
void Assembler::clc() {
  EnsureSpace ensure_space(this);
  emit(0xF8);
}

888

889 890 891 892 893
void Assembler::cld() {
  EnsureSpace ensure_space(this);
  emit(0xFC);
}

894

895 896 897 898 899 900
void Assembler::cdq() {
  EnsureSpace ensure_space(this);
  emit(0x99);
}


901
void Assembler::cmovq(Condition cc, Register dst, Register src) {
902 903 904 905 906
  if (cc == always) {
    movq(dst, src);
  } else if (cc == never) {
    return;
  }
907 908
  // No need to check CpuInfo for CMOV support, it's a required part of the
  // 64-bit architecture.
909
  DCHECK(cc >= 0);  // Use mov for unconditional moves.
910
  EnsureSpace ensure_space(this);
911
  // Opcode: REX.W 0f 40 + cc /r.
912 913 914 915 916 917 918 919
  emit_rex_64(dst, src);
  emit(0x0f);
  emit(0x40 + cc);
  emit_modrm(dst, src);
}


void Assembler::cmovq(Condition cc, Register dst, const Operand& src) {
920 921 922 923 924
  if (cc == always) {
    movq(dst, src);
  } else if (cc == never) {
    return;
  }
925
  DCHECK(cc >= 0);
926
  EnsureSpace ensure_space(this);
927
  // Opcode: REX.W 0f 40 + cc /r.
928 929 930 931 932 933 934 935
  emit_rex_64(dst, src);
  emit(0x0f);
  emit(0x40 + cc);
  emit_operand(dst, src);
}


void Assembler::cmovl(Condition cc, Register dst, Register src) {
936 937 938 939 940
  if (cc == always) {
    movl(dst, src);
  } else if (cc == never) {
    return;
  }
941
  DCHECK(cc >= 0);
942
  EnsureSpace ensure_space(this);
943
  // Opcode: 0f 40 + cc /r.
944 945 946 947 948 949 950 951
  emit_optional_rex_32(dst, src);
  emit(0x0f);
  emit(0x40 + cc);
  emit_modrm(dst, src);
}


void Assembler::cmovl(Condition cc, Register dst, const Operand& src) {
952 953 954 955 956
  if (cc == always) {
    movl(dst, src);
  } else if (cc == never) {
    return;
  }
957
  DCHECK(cc >= 0);
958
  EnsureSpace ensure_space(this);
959
  // Opcode: 0f 40 + cc /r.
960 961 962 963 964 965 966
  emit_optional_rex_32(dst, src);
  emit(0x0f);
  emit(0x40 + cc);
  emit_operand(dst, src);
}


967
void Assembler::cmpb_al(Immediate imm8) {
968
  DCHECK(is_int8(imm8.value_) || is_uint8(imm8.value_));
969 970 971 972 973
  EnsureSpace ensure_space(this);
  emit(0x3c);
  emit(imm8.value_);
}

974

975 976 977 978 979 980 981
void Assembler::cpuid() {
  EnsureSpace ensure_space(this);
  emit(0x0F);
  emit(0xA2);
}


982 983 984 985 986 987 988
void Assembler::cqo() {
  EnsureSpace ensure_space(this);
  emit_rex_64();
  emit(0x99);
}


989
void Assembler::emit_dec(Register dst, int size) {
990
  EnsureSpace ensure_space(this);
991
  emit_rex(dst, size);
992 993 994 995 996
  emit(0xFF);
  emit_modrm(0x1, dst);
}


997
void Assembler::emit_dec(const Operand& dst, int size) {
998
  EnsureSpace ensure_space(this);
999
  emit_rex(dst, size);
1000 1001 1002 1003 1004
  emit(0xFF);
  emit_operand(1, dst);
}


1005 1006
void Assembler::decb(Register dst) {
  EnsureSpace ensure_space(this);
1007
  if (!dst.is_byte_register()) {
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
    // Register is not one of al, bl, cl, dl.  Its encoding needs REX.
    emit_rex_32(dst);
  }
  emit(0xFE);
  emit_modrm(0x1, dst);
}


void Assembler::decb(const Operand& dst) {
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(dst);
  emit(0xFE);
  emit_operand(1, dst);
}


1024 1025 1026 1027 1028 1029 1030 1031
void Assembler::enter(Immediate size) {
  EnsureSpace ensure_space(this);
  emit(0xC8);
  emitw(size.value_);  // 16 bit operand, always.
  emit(0);
}


1032 1033
void Assembler::hlt() {
  EnsureSpace ensure_space(this);
1034
  emit(0xF4);
1035 1036 1037
}


1038
void Assembler::emit_idiv(Register src, int size) {
1039
  EnsureSpace ensure_space(this);
1040
  emit_rex(src, size);
1041 1042 1043 1044 1045
  emit(0xF7);
  emit_modrm(0x7, src);
}


1046 1047 1048 1049 1050 1051 1052 1053
void Assembler::emit_div(Register src, int size) {
  EnsureSpace ensure_space(this);
  emit_rex(src, size);
  emit(0xF7);
  emit_modrm(0x6, src);
}


1054
void Assembler::emit_imul(Register src, int size) {
1055
  EnsureSpace ensure_space(this);
1056
  emit_rex(src, size);
1057 1058 1059 1060 1061
  emit(0xF7);
  emit_modrm(0x5, src);
}


1062 1063 1064 1065 1066 1067 1068 1069
void Assembler::emit_imul(const Operand& src, int size) {
  EnsureSpace ensure_space(this);
  emit_rex(src, size);
  emit(0xF7);
  emit_operand(0x5, src);
}


1070
void Assembler::emit_imul(Register dst, Register src, int size) {
1071
  EnsureSpace ensure_space(this);
1072
  emit_rex(dst, src, size);
1073 1074 1075 1076 1077 1078
  emit(0x0F);
  emit(0xAF);
  emit_modrm(dst, src);
}


1079
void Assembler::emit_imul(Register dst, const Operand& src, int size) {
1080
  EnsureSpace ensure_space(this);
1081
  emit_rex(dst, src, size);
1082 1083 1084 1085 1086 1087
  emit(0x0F);
  emit(0xAF);
  emit_operand(dst, src);
}


1088
void Assembler::emit_imul(Register dst, Register src, Immediate imm, int size) {
1089
  EnsureSpace ensure_space(this);
1090
  emit_rex(dst, src, size);
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
  if (is_int8(imm.value_)) {
    emit(0x6B);
    emit_modrm(dst, src);
    emit(imm.value_);
  } else {
    emit(0x69);
    emit_modrm(dst, src);
    emitl(imm.value_);
  }
}


1103 1104 1105 1106 1107 1108
void Assembler::emit_imul(Register dst, const Operand& src, Immediate imm,
                          int size) {
  EnsureSpace ensure_space(this);
  emit_rex(dst, src, size);
  if (is_int8(imm.value_)) {
    emit(0x6B);
1109 1110
    emit_operand(dst, src);
    emit(imm.value_);
1111 1112
  } else {
    emit(0x69);
1113 1114
    emit_operand(dst, src);
    emitl(imm.value_);
1115 1116 1117 1118
  }
}


1119
void Assembler::emit_inc(Register dst, int size) {
1120
  EnsureSpace ensure_space(this);
1121
  emit_rex(dst, size);
1122
  emit(0xFF);
1123
  emit_modrm(0x0, dst);
1124 1125 1126
}


1127
void Assembler::emit_inc(const Operand& dst, int size) {
1128
  EnsureSpace ensure_space(this);
1129
  emit_rex(dst, size);
1130 1131 1132 1133 1134
  emit(0xFF);
  emit_operand(0, dst);
}


1135 1136
void Assembler::int3() {
  EnsureSpace ensure_space(this);
1137
  emit(0xCC);
1138 1139 1140
}


1141
void Assembler::j(Condition cc, Label* L, Label::Distance distance) {
1142 1143 1144 1145 1146 1147
  if (cc == always) {
    jmp(L);
    return;
  } else if (cc == never) {
    return;
  }
1148
  EnsureSpace ensure_space(this);
1149
  DCHECK(is_uint4(cc));
1150 1151 1152 1153
  if (L->is_bound()) {
    const int short_size = 2;
    const int long_size  = 6;
    int offs = L->pos() - pc_offset();
1154
    DCHECK(offs <= 0);
1155 1156 1157
    // Determine whether we can use 1-byte offsets for backwards branches,
    // which have a max range of 128 bytes.

1158 1159 1160 1161 1162 1163 1164
    // We also need to check predictable_code_size() flag here, because on x64,
    // when the full code generator recompiles code for debugging, some places
    // need to be padded out to a certain size. The debugger is keeping track of
    // how often it did this so that it can adjust return addresses on the
    // stack, but if the size of jump instructions can also change, that's not
    // enough and the calculated offsets would be incorrect.
    if (is_int8(offs - short_size) && !predictable_code_size()) {
1165
      // 0111 tttn #8-bit disp.
1166 1167
      emit(0x70 | cc);
      emit((offs - short_size) & 0xFF);
1168
    } else {
1169
      // 0000 1111 1000 tttn #32-bit disp.
1170 1171
      emit(0x0F);
      emit(0x80 | cc);
1172
      emitl(offs - long_size);
1173
    }
1174 1175 1176 1177 1178 1179
  } else if (distance == Label::kNear) {
    // 0111 tttn #8-bit disp
    emit(0x70 | cc);
    byte disp = 0x00;
    if (L->is_near_linked()) {
      int offset = L->near_link_pos() - pc_offset();
1180
      DCHECK(is_int8(offset));
1181 1182 1183 1184
      disp = static_cast<byte>(offset & 0xFF);
    }
    L->link_to(pc_offset(), Label::kNear);
    emit(disp);
1185
  } else if (L->is_linked()) {
1186
    // 0000 1111 1000 tttn #32-bit disp.
1187 1188
    emit(0x0F);
    emit(0x80 | cc);
1189
    emitl(L->pos());
1190 1191
    L->link_to(pc_offset() - sizeof(int32_t));
  } else {
1192
    DCHECK(L->is_unused());
1193 1194
    emit(0x0F);
    emit(0x80 | cc);
1195
    int32_t current = pc_offset();
1196
    emitl(current);
1197 1198 1199 1200 1201
    L->link_to(current);
  }
}


1202
void Assembler::j(Condition cc, Address entry, RelocInfo::Mode rmode) {
1203
  DCHECK(RelocInfo::IsRuntimeEntry(rmode));
1204
  EnsureSpace ensure_space(this);
1205
  DCHECK(is_uint4(cc));
1206 1207 1208 1209 1210 1211
  emit(0x0F);
  emit(0x80 | cc);
  emit_runtime_entry(entry, rmode);
}


1212 1213 1214 1215
void Assembler::j(Condition cc,
                  Handle<Code> target,
                  RelocInfo::Mode rmode) {
  EnsureSpace ensure_space(this);
1216
  DCHECK(is_uint4(cc));
1217
  // 0000 1111 1000 tttn #32-bit disp.
1218 1219 1220 1221 1222 1223
  emit(0x0F);
  emit(0x80 | cc);
  emit_code_target(target, rmode);
}


1224
void Assembler::jmp(Label* L, Label::Distance distance) {
1225
  EnsureSpace ensure_space(this);
1226 1227
  const int short_size = sizeof(int8_t);
  const int long_size = sizeof(int32_t);
1228 1229
  if (L->is_bound()) {
    int offs = L->pos() - pc_offset() - 1;
1230
    DCHECK(offs <= 0);
1231
    if (is_int8(offs - short_size) && !predictable_code_size()) {
1232
      // 1110 1011 #8-bit disp.
1233
      emit(0xEB);
1234
      emit((offs - short_size) & 0xFF);
1235
    } else {
1236
      // 1110 1001 #32-bit disp.
1237
      emit(0xE9);
1238
      emitl(offs - long_size);
1239
    }
1240 1241 1242 1243 1244
  } else if (distance == Label::kNear) {
    emit(0xEB);
    byte disp = 0x00;
    if (L->is_near_linked()) {
      int offset = L->near_link_pos() - pc_offset();
1245
      DCHECK(is_int8(offset));
1246 1247 1248 1249 1250
      disp = static_cast<byte>(offset & 0xFF);
    }
    L->link_to(pc_offset(), Label::kNear);
    emit(disp);
  } else if (L->is_linked()) {
1251
    // 1110 1001 #32-bit disp.
1252
    emit(0xE9);
1253
    emitl(L->pos());
1254
    L->link_to(pc_offset() - long_size);
1255
  } else {
1256
    // 1110 1001 #32-bit disp.
1257
    DCHECK(L->is_unused());
1258
    emit(0xE9);
1259
    int32_t current = pc_offset();
1260
    emitl(current);
1261 1262 1263 1264 1265
    L->link_to(current);
  }
}


1266 1267
void Assembler::jmp(Handle<Code> target, RelocInfo::Mode rmode) {
  EnsureSpace ensure_space(this);
1268
  // 1110 1001 #32-bit disp.
1269 1270 1271 1272 1273
  emit(0xE9);
  emit_code_target(target, rmode);
}


1274
void Assembler::jmp(Address entry, RelocInfo::Mode rmode) {
1275
  DCHECK(RelocInfo::IsRuntimeEntry(rmode));
1276
  EnsureSpace ensure_space(this);
1277
  DCHECK(RelocInfo::IsRuntimeEntry(rmode));
1278 1279 1280 1281 1282
  emit(0xE9);
  emit_runtime_entry(entry, rmode);
}


1283 1284
void Assembler::jmp(Register target) {
  EnsureSpace ensure_space(this);
1285
  // Opcode FF/4 r64.
1286
  emit_optional_rex_32(target);
1287
  emit(0xFF);
1288
  emit_modrm(0x4, target);
1289 1290 1291
}


1292 1293
void Assembler::jmp(const Operand& src) {
  EnsureSpace ensure_space(this);
1294
  // Opcode FF/4 m64.
1295 1296 1297 1298 1299 1300
  emit_optional_rex_32(src);
  emit(0xFF);
  emit_operand(0x4, src);
}


1301
void Assembler::emit_lea(Register dst, const Operand& src, int size) {
1302
  EnsureSpace ensure_space(this);
1303
  emit_rex(dst, src, size);
1304 1305 1306 1307 1308
  emit(0x8D);
  emit_operand(dst, src);
}


lrn@chromium.org's avatar
lrn@chromium.org committed
1309 1310
void Assembler::load_rax(void* value, RelocInfo::Mode mode) {
  EnsureSpace ensure_space(this);
1311 1312 1313 1314 1315
  if (kPointerSize == kInt64Size) {
    emit(0x48);  // REX.W
    emit(0xA1);
    emitp(value, mode);
  } else {
1316
    DCHECK(kPointerSize == kInt32Size);
1317 1318 1319 1320 1321 1322 1323
    emit(0xA1);
    emitp(value, mode);
    // In 64-bit mode, need to zero extend the operand to 8 bytes.
    // See 2.2.1.4 in Intel64 and IA32 Architectures Software
    // Developer's Manual Volume 2.
    emitl(0);
  }
lrn@chromium.org's avatar
lrn@chromium.org committed
1324 1325 1326 1327 1328 1329 1330 1331
}


void Assembler::load_rax(ExternalReference ref) {
  load_rax(ref.address(), RelocInfo::EXTERNAL_REFERENCE);
}


1332 1333 1334 1335 1336 1337
void Assembler::leave() {
  EnsureSpace ensure_space(this);
  emit(0xC9);
}


1338 1339
void Assembler::movb(Register dst, const Operand& src) {
  EnsureSpace ensure_space(this);
1340
  if (!dst.is_byte_register()) {
1341 1342 1343 1344 1345
    // Register is not one of al, bl, cl, dl.  Its encoding needs REX.
    emit_rex_32(dst, src);
  } else {
    emit_optional_rex_32(dst, src);
  }
1346 1347 1348 1349
  emit(0x8A);
  emit_operand(dst, src);
}

1350

1351 1352
void Assembler::movb(Register dst, Immediate imm) {
  EnsureSpace ensure_space(this);
1353
  if (!dst.is_byte_register()) {
1354
    // Register is not one of al, bl, cl, dl.  Its encoding needs REX.
1355 1356 1357
    emit_rex_32(dst);
  }
  emit(0xB0 + dst.low_bits());
1358 1359 1360
  emit(imm.value_);
}

1361

1362 1363
void Assembler::movb(const Operand& dst, Register src) {
  EnsureSpace ensure_space(this);
1364
  if (!src.is_byte_register()) {
1365
    // Register is not one of al, bl, cl, dl.  Its encoding needs REX.
1366 1367 1368 1369
    emit_rex_32(src, dst);
  } else {
    emit_optional_rex_32(src, dst);
  }
1370 1371 1372 1373
  emit(0x88);
  emit_operand(src, dst);
}

1374

1375 1376 1377 1378 1379 1380 1381 1382 1383
void Assembler::movb(const Operand& dst, Immediate imm) {
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(dst);
  emit(0xC6);
  emit_operand(0x0, dst);
  emit(static_cast<byte>(imm.value_));
}


1384 1385 1386 1387 1388 1389 1390 1391 1392
void Assembler::movw(Register dst, const Operand& src) {
  EnsureSpace ensure_space(this);
  emit(0x66);
  emit_optional_rex_32(dst, src);
  emit(0x8B);
  emit_operand(dst, src);
}


1393 1394 1395 1396 1397 1398 1399 1400
void Assembler::movw(const Operand& dst, Register src) {
  EnsureSpace ensure_space(this);
  emit(0x66);
  emit_optional_rex_32(src, dst);
  emit(0x89);
  emit_operand(src, dst);
}

1401

1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
void Assembler::movw(const Operand& dst, Immediate imm) {
  EnsureSpace ensure_space(this);
  emit(0x66);
  emit_optional_rex_32(dst);
  emit(0xC7);
  emit_operand(0x0, dst);
  emit(static_cast<byte>(imm.value_ & 0xff));
  emit(static_cast<byte>(imm.value_ >> 8));
}


1413
void Assembler::emit_mov(Register dst, const Operand& src, int size) {
1414
  EnsureSpace ensure_space(this);
1415
  emit_rex(dst, src, size);
1416 1417 1418 1419 1420
  emit(0x8B);
  emit_operand(dst, src);
}


1421
void Assembler::emit_mov(Register dst, Register src, int size) {
1422
  EnsureSpace ensure_space(this);
1423
  if (src.low_bits() == 4) {
1424
    emit_rex(src, dst, size);
1425 1426 1427
    emit(0x89);
    emit_modrm(src, dst);
  } else {
1428
    emit_rex(dst, src, size);
1429 1430 1431
    emit(0x8B);
    emit_modrm(dst, src);
  }
1432 1433 1434
}


1435
void Assembler::emit_mov(const Operand& dst, Register src, int size) {
1436
  EnsureSpace ensure_space(this);
1437
  emit_rex(src, dst, size);
1438 1439 1440 1441 1442
  emit(0x89);
  emit_operand(src, dst);
}


1443
void Assembler::emit_mov(Register dst, Immediate value, int size) {
1444
  EnsureSpace ensure_space(this);
1445 1446 1447 1448
  emit_rex(dst, size);
  if (size == kInt64Size) {
    emit(0xC7);
    emit_modrm(0x0, dst);
1449
  } else {
1450
    DCHECK(size == kInt32Size);
1451
    emit(0xB8 + dst.low_bits());
1452
  }
1453
  emit(value);
1454 1455
}

1456

1457
void Assembler::emit_mov(const Operand& dst, Immediate value, int size) {
1458
  EnsureSpace ensure_space(this);
1459
  emit_rex(dst, size);
1460
  emit(0xC7);
1461 1462
  emit_operand(0x0, dst);
  emit(value);
lrn@chromium.org's avatar
lrn@chromium.org committed
1463 1464 1465
}


1466 1467 1468 1469 1470
void Assembler::movp(Register dst, void* value, RelocInfo::Mode rmode) {
  EnsureSpace ensure_space(this);
  emit_rex(dst, kPointerSize);
  emit(0xB8 | dst.low_bits());
  emitp(value, rmode);
1471 1472
}

1473
void Assembler::movq(Register dst, int64_t value, RelocInfo::Mode rmode) {
1474 1475 1476
  EnsureSpace ensure_space(this);
  emit_rex_64(dst);
  emit(0xB8 | dst.low_bits());
1477 1478 1479
  if (!RelocInfo::IsNone(rmode)) {
    RecordRelocInfo(rmode, value);
  }
1480 1481 1482
  emitq(value);
}

1483 1484
void Assembler::movq(Register dst, uint64_t value, RelocInfo::Mode rmode) {
  movq(dst, static_cast<int64_t>(value), rmode);
1485 1486
}

1487 1488
// Loads the ip-relative location of the src label into the target location
// (as a 32-bit offset sign extended to 64-bit).
1489 1490 1491 1492 1493 1494 1495
void Assembler::movl(const Operand& dst, Label* src) {
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(dst);
  emit(0xC7);
  emit_operand(0, dst);
  if (src->is_bound()) {
    int offset = src->pos() - pc_offset() - sizeof(int32_t);
1496
    DCHECK(offset <= 0);
1497 1498 1499 1500 1501
    emitl(offset);
  } else if (src->is_linked()) {
    emitl(src->pos());
    src->link_to(pc_offset() - sizeof(int32_t));
  } else {
1502
    DCHECK(src->is_unused());
1503 1504 1505 1506 1507 1508 1509
    int32_t current = pc_offset();
    emitl(current);
    src->link_to(current);
  }
}


1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
void Assembler::movsxbl(Register dst, Register src) {
  EnsureSpace ensure_space(this);
  if (!src.is_byte_register()) {
    // Register is not one of al, bl, cl, dl.  Its encoding needs REX.
    emit_rex_32(dst, src);
  } else {
    emit_optional_rex_32(dst, src);
  }
  emit(0x0F);
  emit(0xBE);
  emit_modrm(dst, src);
}


1524 1525 1526 1527 1528 1529 1530 1531 1532
void Assembler::movsxbl(Register dst, const Operand& src) {
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0xBE);
  emit_operand(dst, src);
}


1533 1534
void Assembler::movsxbq(Register dst, const Operand& src) {
  EnsureSpace ensure_space(this);
1535
  emit_rex_64(dst, src);
1536 1537 1538 1539 1540 1541
  emit(0x0F);
  emit(0xBE);
  emit_operand(dst, src);
}


1542 1543 1544 1545 1546 1547 1548 1549 1550
void Assembler::movsxwl(Register dst, Register src) {
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0xBF);
  emit_modrm(dst, src);
}


1551 1552 1553 1554 1555 1556 1557 1558 1559
void Assembler::movsxwl(Register dst, const Operand& src) {
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0xBF);
  emit_operand(dst, src);
}


1560 1561 1562 1563 1564 1565 1566 1567 1568
void Assembler::movsxwq(Register dst, const Operand& src) {
  EnsureSpace ensure_space(this);
  emit_rex_64(dst, src);
  emit(0x0F);
  emit(0xBF);
  emit_operand(dst, src);
}


1569 1570 1571 1572 1573
void Assembler::movsxlq(Register dst, Register src) {
  EnsureSpace ensure_space(this);
  emit_rex_64(dst, src);
  emit(0x63);
  emit_modrm(dst, src);
1574 1575 1576 1577 1578 1579 1580 1581
}


void Assembler::movsxlq(Register dst, const Operand& src) {
  EnsureSpace ensure_space(this);
  emit_rex_64(dst, src);
  emit(0x63);
  emit_operand(dst, src);
1582 1583 1584
}


1585
void Assembler::emit_movzxb(Register dst, const Operand& src, int size) {
1586
  EnsureSpace ensure_space(this);
1587 1588
  // 32 bit operations zero the top 32 bits of 64 bit registers.  Therefore
  // there is no need to make this a 64 bit operation.
1589
  emit_optional_rex_32(dst, src);
1590 1591 1592 1593 1594 1595
  emit(0x0F);
  emit(0xB6);
  emit_operand(dst, src);
}


1596 1597 1598 1599
void Assembler::emit_movzxb(Register dst, Register src, int size) {
  EnsureSpace ensure_space(this);
  // 32 bit operations zero the top 32 bits of 64 bit registers.  Therefore
  // there is no need to make this a 64 bit operation.
1600 1601 1602 1603 1604 1605
  if (!src.is_byte_register()) {
    // Register is not one of al, bl, cl, dl.  Its encoding needs REX.
    emit_rex_32(dst, src);
  } else {
    emit_optional_rex_32(dst, src);
  }
1606 1607 1608 1609 1610 1611
  emit(0x0F);
  emit(0xB6);
  emit_modrm(dst, src);
}


1612
void Assembler::emit_movzxw(Register dst, const Operand& src, int size) {
1613
  EnsureSpace ensure_space(this);
1614 1615
  // 32 bit operations zero the top 32 bits of 64 bit registers.  Therefore
  // there is no need to make this a 64 bit operation.
1616 1617 1618 1619 1620 1621 1622
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0xB7);
  emit_operand(dst, src);
}


1623
void Assembler::emit_movzxw(Register dst, Register src, int size) {
1624
  EnsureSpace ensure_space(this);
1625 1626
  // 32 bit operations zero the top 32 bits of 64 bit registers.  Therefore
  // there is no need to make this a 64 bit operation.
1627 1628 1629 1630 1631 1632 1633
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0xB7);
  emit_modrm(dst, src);
}


1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648
void Assembler::repmovsb() {
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit(0xA4);
}


void Assembler::repmovsw() {
  EnsureSpace ensure_space(this);
  emit(0x66);  // Operand size override.
  emit(0xF3);
  emit(0xA4);
}


1649
void Assembler::emit_repmovs(int size) {
1650 1651
  EnsureSpace ensure_space(this);
  emit(0xF3);
1652
  emit_rex(size);
1653 1654 1655 1656
  emit(0xA5);
}


1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
void Assembler::mull(Register src) {
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(src);
  emit(0xF7);
  emit_modrm(0x4, src);
}


void Assembler::mull(const Operand& src) {
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(src);
  emit(0xF7);
  emit_operand(0x4, src);
}


void Assembler::mulq(Register src) {
1674 1675 1676 1677 1678 1679 1680
  EnsureSpace ensure_space(this);
  emit_rex_64(src);
  emit(0xF7);
  emit_modrm(0x4, src);
}


1681
void Assembler::emit_neg(Register dst, int size) {
1682
  EnsureSpace ensure_space(this);
1683
  emit_rex(dst, size);
1684 1685 1686 1687 1688
  emit(0xF7);
  emit_modrm(0x3, dst);
}


1689
void Assembler::emit_neg(const Operand& dst, int size) {
1690 1691 1692 1693 1694 1695 1696
  EnsureSpace ensure_space(this);
  emit_rex_64(dst);
  emit(0xF7);
  emit_operand(3, dst);
}


1697 1698
void Assembler::nop() {
  EnsureSpace ensure_space(this);
1699
  emit(0x90);
1700 1701
}

1702

1703
void Assembler::emit_not(Register dst, int size) {
1704
  EnsureSpace ensure_space(this);
1705
  emit_rex(dst, size);
1706
  emit(0xF7);
1707
  emit_modrm(0x2, dst);
1708 1709 1710
}


1711
void Assembler::emit_not(const Operand& dst, int size) {
1712
  EnsureSpace ensure_space(this);
1713
  emit_rex(dst, size);
1714 1715 1716 1717 1718
  emit(0xF7);
  emit_operand(2, dst);
}


1719
void Assembler::Nop(int n) {
1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734
  // The recommended muti-byte sequences of NOP instructions from the Intel 64
  // and IA-32 Architectures Software Developer's Manual.
  //
  // Length   Assembly                                Byte Sequence
  // 2 bytes  66 NOP                                  66 90H
  // 3 bytes  NOP DWORD ptr [EAX]                     0F 1F 00H
  // 4 bytes  NOP DWORD ptr [EAX + 00H]               0F 1F 40 00H
  // 5 bytes  NOP DWORD ptr [EAX + EAX*1 + 00H]       0F 1F 44 00 00H
  // 6 bytes  66 NOP DWORD ptr [EAX + EAX*1 + 00H]    66 0F 1F 44 00 00H
  // 7 bytes  NOP DWORD ptr [EAX + 00000000H]         0F 1F 80 00 00 00 00H
  // 8 bytes  NOP DWORD ptr [EAX + EAX*1 + 00000000H] 0F 1F 84 00 00 00 00 00H
  // 9 bytes  66 NOP DWORD ptr [EAX + EAX*1 +         66 0F 1F 84 00 00 00 00
  //          00000000H]                              00H

  EnsureSpace ensure_space(this);
1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791
  while (n > 0) {
    switch (n) {
      case 2:
        emit(0x66);
      case 1:
        emit(0x90);
        return;
      case 3:
        emit(0x0f);
        emit(0x1f);
        emit(0x00);
        return;
      case 4:
        emit(0x0f);
        emit(0x1f);
        emit(0x40);
        emit(0x00);
        return;
      case 6:
        emit(0x66);
      case 5:
        emit(0x0f);
        emit(0x1f);
        emit(0x44);
        emit(0x00);
        emit(0x00);
        return;
      case 7:
        emit(0x0f);
        emit(0x1f);
        emit(0x80);
        emit(0x00);
        emit(0x00);
        emit(0x00);
        emit(0x00);
        return;
      default:
      case 11:
        emit(0x66);
        n--;
      case 10:
        emit(0x66);
        n--;
      case 9:
        emit(0x66);
        n--;
      case 8:
        emit(0x0f);
        emit(0x1f);
        emit(0x84);
        emit(0x00);
        emit(0x00);
        emit(0x00);
        emit(0x00);
        emit(0x00);
        n -= 8;
    }
1792 1793 1794 1795
  }
}


1796
void Assembler::popq(Register dst) {
1797
  EnsureSpace ensure_space(this);
1798
  emit_optional_rex_32(dst);
1799
  emit(0x58 | dst.low_bits());
1800 1801 1802
}


1803
void Assembler::popq(const Operand& dst) {
1804
  EnsureSpace ensure_space(this);
1805
  emit_optional_rex_32(dst);
1806
  emit(0x8F);
1807
  emit_operand(0, dst);
1808 1809 1810
}


1811 1812 1813 1814 1815 1816
void Assembler::popfq() {
  EnsureSpace ensure_space(this);
  emit(0x9D);
}


1817
void Assembler::pushq(Register src) {
1818
  EnsureSpace ensure_space(this);
1819
  emit_optional_rex_32(src);
1820
  emit(0x50 | src.low_bits());
1821 1822 1823
}


1824
void Assembler::pushq(const Operand& src) {
1825
  EnsureSpace ensure_space(this);
1826
  emit_optional_rex_32(src);
1827
  emit(0xFF);
1828
  emit_operand(6, src);
1829 1830 1831
}


1832
void Assembler::pushq(Immediate value) {
1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843
  EnsureSpace ensure_space(this);
  if (is_int8(value.value_)) {
    emit(0x6A);
    emit(value.value_);  // Emit low byte of value.
  } else {
    emit(0x68);
    emitl(value.value_);
  }
}


1844
void Assembler::pushq_imm32(int32_t imm32) {
1845 1846 1847 1848 1849 1850
  EnsureSpace ensure_space(this);
  emit(0x68);
  emitl(imm32);
}


1851 1852 1853
void Assembler::pushfq() {
  EnsureSpace ensure_space(this);
  emit(0x9C);
1854 1855
}

1856

1857 1858
void Assembler::ret(int imm16) {
  EnsureSpace ensure_space(this);
1859
  DCHECK(is_uint16(imm16));
1860
  if (imm16 == 0) {
1861
    emit(0xC3);
1862
  } else {
1863 1864 1865
    emit(0xC2);
    emit(imm16 & 0xFF);
    emit((imm16 >> 8) & 0xFF);
1866 1867 1868
  }
}

1869

1870 1871 1872 1873 1874 1875 1876
void Assembler::ud2() {
  EnsureSpace ensure_space(this);
  emit(0x0F);
  emit(0x0B);
}


1877
void Assembler::setcc(Condition cc, Register reg) {
1878 1879 1880 1881
  if (cc > last_condition) {
    movb(reg, Immediate(cc == always ? 1 : 0));
    return;
  }
1882
  EnsureSpace ensure_space(this);
1883
  DCHECK(is_uint4(cc));
1884 1885
  if (!reg.is_byte_register()) {
    // Register is not one of al, bl, cl, dl.  Its encoding needs REX.
1886 1887 1888 1889 1890 1891 1892 1893
    emit_rex_32(reg);
  }
  emit(0x0F);
  emit(0x90 | cc);
  emit_modrm(0x0, reg);
}


1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911
void Assembler::shld(Register dst, Register src) {
  EnsureSpace ensure_space(this);
  emit_rex_64(src, dst);
  emit(0x0F);
  emit(0xA5);
  emit_modrm(src, dst);
}


void Assembler::shrd(Register dst, Register src) {
  EnsureSpace ensure_space(this);
  emit_rex_64(src, dst);
  emit(0x0F);
  emit(0xAD);
  emit_modrm(src, dst);
}


1912
void Assembler::emit_xchg(Register dst, Register src, int size) {
1913 1914 1915
  EnsureSpace ensure_space(this);
  if (src.is(rax) || dst.is(rax)) {  // Single-byte encoding
    Register other = src.is(rax) ? dst : src;
1916
    emit_rex(other, size);
1917
    emit(0x90 | other.low_bits());
1918
  } else if (dst.low_bits() == 4) {
1919
    emit_rex(dst, src, size);
1920 1921 1922
    emit(0x87);
    emit_modrm(dst, src);
  } else {
1923
    emit_rex(src, dst, size);
1924 1925 1926 1927 1928 1929
    emit(0x87);
    emit_modrm(src, dst);
  }
}


1930 1931 1932 1933 1934 1935 1936 1937
void Assembler::emit_xchg(Register dst, const Operand& src, int size) {
  EnsureSpace ensure_space(this);
  emit_rex(dst, src, size);
  emit(0x87);
  emit_operand(dst, src);
}


lrn@chromium.org's avatar
lrn@chromium.org committed
1938 1939
void Assembler::store_rax(void* dst, RelocInfo::Mode mode) {
  EnsureSpace ensure_space(this);
1940 1941 1942 1943 1944
  if (kPointerSize == kInt64Size) {
    emit(0x48);  // REX.W
    emit(0xA3);
    emitp(dst, mode);
  } else {
1945
    DCHECK(kPointerSize == kInt32Size);
1946 1947 1948 1949 1950 1951 1952
    emit(0xA3);
    emitp(dst, mode);
    // In 64-bit mode, need to zero extend the operand to 8 bytes.
    // See 2.2.1.4 in Intel64 and IA32 Architectures Software
    // Developer's Manual Volume 2.
    emitl(0);
  }
lrn@chromium.org's avatar
lrn@chromium.org committed
1953 1954 1955 1956 1957 1958 1959 1960
}


void Assembler::store_rax(ExternalReference ref) {
  store_rax(ref.address(), RelocInfo::EXTERNAL_REFERENCE);
}


1961 1962
void Assembler::testb(Register dst, Register src) {
  EnsureSpace ensure_space(this);
1963 1964 1965 1966 1967
  if (src.low_bits() == 4) {
    emit_rex_32(src, dst);
    emit(0x84);
    emit_modrm(src, dst);
  } else {
1968
    if (!dst.is_byte_register() || !src.is_byte_register()) {
1969 1970 1971 1972 1973
      // Register is not one of al, bl, cl, dl.  Its encoding needs REX.
      emit_rex_32(dst, src);
    }
    emit(0x84);
    emit_modrm(dst, src);
1974 1975 1976 1977
  }
}


1978
void Assembler::testb(Register reg, Immediate mask) {
1979
  DCHECK(is_int8(mask.value_) || is_uint8(mask.value_));
1980 1981 1982
  EnsureSpace ensure_space(this);
  if (reg.is(rax)) {
    emit(0xA8);
1983
    emit(mask.value_);  // Low byte emitted.
1984
  } else {
1985
    if (!reg.is_byte_register()) {
1986 1987
      // Register is not one of al, bl, cl, dl.  Its encoding needs REX.
      emit_rex_32(reg);
1988 1989
    }
    emit(0xF6);
1990
    emit_modrm(0x0, reg);
1991 1992 1993 1994 1995 1996
    emit(mask.value_);  // Low byte emitted.
  }
}


void Assembler::testb(const Operand& op, Immediate mask) {
1997
  DCHECK(is_int8(mask.value_) || is_uint8(mask.value_));
1998 1999 2000 2001 2002 2003 2004 2005
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(rax, op);
  emit(0xF6);
  emit_operand(rax, op);  // Operation code 0
  emit(mask.value_);  // Low byte emitted.
}


2006 2007
void Assembler::testb(const Operand& op, Register reg) {
  EnsureSpace ensure_space(this);
2008
  if (!reg.is_byte_register()) {
2009 2010 2011 2012 2013 2014 2015 2016 2017
    // Register is not one of al, bl, cl, dl.  Its encoding needs REX.
    emit_rex_32(reg, op);
  } else {
    emit_optional_rex_32(reg, op);
  }
  emit(0x84);
  emit_operand(reg, op);
}

2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061
void Assembler::testw(Register dst, Register src) {
  EnsureSpace ensure_space(this);
  emit(0x66);
  if (src.low_bits() == 4) {
    emit_rex_32(src, dst);
  }
  emit(0x85);
  emit_modrm(src, dst);
}

void Assembler::testw(Register reg, Immediate mask) {
  DCHECK(is_int16(mask.value_) || is_uint16(mask.value_));
  EnsureSpace ensure_space(this);
  emit(0x66);
  if (reg.is(rax)) {
    emit(0xA9);
    emit(mask.value_);
  } else {
    if (reg.low_bits() == 4) {
      emit_rex_32(reg);
    }
    emit(0xF7);
    emit_modrm(0x0, reg);
    emit(mask.value_);
  }
}

void Assembler::testw(const Operand& op, Immediate mask) {
  DCHECK(is_int16(mask.value_) || is_uint16(mask.value_));
  EnsureSpace ensure_space(this);
  emit(0x66);
  emit_optional_rex_32(rax, op);
  emit(0xF7);
  emit_operand(rax, op);
  emit(mask.value_);
}

void Assembler::testw(const Operand& op, Register reg) {
  EnsureSpace ensure_space(this);
  emit(0x66);
  emit_optional_rex_32(reg, op);
  emit(0x85);
  emit_operand(rax, op);
}
2062

2063
void Assembler::emit_test(Register dst, Register src, int size) {
2064
  EnsureSpace ensure_space(this);
2065
  if (src.low_bits() == 4) {
2066
    emit_rex(src, dst, size);
2067 2068 2069
    emit(0x85);
    emit_modrm(src, dst);
  } else {
2070
    emit_rex(dst, src, size);
2071 2072 2073
    emit(0x85);
    emit_modrm(dst, src);
  }
2074 2075 2076
}


2077
void Assembler::emit_test(Register reg, Immediate mask, int size) {
2078 2079 2080 2081 2082
  // testl with a mask that fits in the low byte is exactly testb.
  if (is_uint8(mask.value_)) {
    testb(reg, mask);
    return;
  }
2083 2084
  EnsureSpace ensure_space(this);
  if (reg.is(rax)) {
2085
    emit_rex(rax, size);
2086 2087 2088
    emit(0xA9);
    emit(mask);
  } else {
2089
    emit_rex(reg, size);
2090
    emit(0xF7);
2091
    emit_modrm(0x0, reg);
2092 2093 2094 2095 2096
    emit(mask);
  }
}


2097
void Assembler::emit_test(const Operand& op, Immediate mask, int size) {
2098 2099 2100 2101 2102
  // testl with a mask that fits in the low byte is exactly testb.
  if (is_uint8(mask.value_)) {
    testb(op, mask);
    return;
  }
2103
  EnsureSpace ensure_space(this);
2104
  emit_rex(rax, op, size);
2105 2106 2107 2108 2109 2110
  emit(0xF7);
  emit_operand(rax, op);  // Operation code 0
  emit(mask);
}


2111
void Assembler::emit_test(const Operand& op, Register reg, int size) {
2112
  EnsureSpace ensure_space(this);
2113
  emit_rex(reg, op, size);
2114 2115 2116 2117 2118
  emit(0x85);
  emit_operand(reg, op);
}


2119
// FPU instructions.
2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141


void Assembler::fld(int i) {
  EnsureSpace ensure_space(this);
  emit_farith(0xD9, 0xC0, i);
}


void Assembler::fld1() {
  EnsureSpace ensure_space(this);
  emit(0xD9);
  emit(0xE8);
}


void Assembler::fldz() {
  EnsureSpace ensure_space(this);
  emit(0xD9);
  emit(0xEE);
}


2142 2143 2144 2145 2146 2147 2148
void Assembler::fldpi() {
  EnsureSpace ensure_space(this);
  emit(0xD9);
  emit(0xEB);
}


2149 2150 2151 2152 2153 2154 2155
void Assembler::fldln2() {
  EnsureSpace ensure_space(this);
  emit(0xD9);
  emit(0xED);
}


2156 2157
void Assembler::fld_s(const Operand& adr) {
  EnsureSpace ensure_space(this);
2158
  emit_optional_rex_32(adr);
2159 2160 2161 2162 2163 2164 2165
  emit(0xD9);
  emit_operand(0, adr);
}


void Assembler::fld_d(const Operand& adr) {
  EnsureSpace ensure_space(this);
2166
  emit_optional_rex_32(adr);
2167 2168 2169 2170 2171 2172 2173
  emit(0xDD);
  emit_operand(0, adr);
}


void Assembler::fstp_s(const Operand& adr) {
  EnsureSpace ensure_space(this);
2174
  emit_optional_rex_32(adr);
2175 2176 2177 2178 2179 2180 2181
  emit(0xD9);
  emit_operand(3, adr);
}


void Assembler::fstp_d(const Operand& adr) {
  EnsureSpace ensure_space(this);
2182
  emit_optional_rex_32(adr);
2183 2184 2185 2186 2187
  emit(0xDD);
  emit_operand(3, adr);
}


2188
void Assembler::fstp(int index) {
2189
  DCHECK(is_uint3(index));
2190 2191 2192 2193 2194
  EnsureSpace ensure_space(this);
  emit_farith(0xDD, 0xD8, index);
}


2195 2196
void Assembler::fild_s(const Operand& adr) {
  EnsureSpace ensure_space(this);
2197
  emit_optional_rex_32(adr);
2198 2199 2200 2201 2202 2203 2204
  emit(0xDB);
  emit_operand(0, adr);
}


void Assembler::fild_d(const Operand& adr) {
  EnsureSpace ensure_space(this);
2205
  emit_optional_rex_32(adr);
2206 2207 2208 2209 2210 2211 2212
  emit(0xDF);
  emit_operand(5, adr);
}


void Assembler::fistp_s(const Operand& adr) {
  EnsureSpace ensure_space(this);
2213
  emit_optional_rex_32(adr);
2214 2215 2216 2217 2218 2219
  emit(0xDB);
  emit_operand(3, adr);
}


void Assembler::fisttp_s(const Operand& adr) {
2220
  DCHECK(IsEnabled(SSE3));
2221
  EnsureSpace ensure_space(this);
2222
  emit_optional_rex_32(adr);
2223 2224 2225 2226 2227
  emit(0xDB);
  emit_operand(1, adr);
}


2228
void Assembler::fisttp_d(const Operand& adr) {
2229
  DCHECK(IsEnabled(SSE3));
2230 2231 2232 2233 2234 2235 2236
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(adr);
  emit(0xDD);
  emit_operand(1, adr);
}


2237 2238
void Assembler::fist_s(const Operand& adr) {
  EnsureSpace ensure_space(this);
2239
  emit_optional_rex_32(adr);
2240 2241 2242 2243 2244 2245 2246
  emit(0xDB);
  emit_operand(2, adr);
}


void Assembler::fistp_d(const Operand& adr) {
  EnsureSpace ensure_space(this);
2247
  emit_optional_rex_32(adr);
2248
  emit(0xDF);
2249
  emit_operand(7, adr);
2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280
}


void Assembler::fabs() {
  EnsureSpace ensure_space(this);
  emit(0xD9);
  emit(0xE1);
}


void Assembler::fchs() {
  EnsureSpace ensure_space(this);
  emit(0xD9);
  emit(0xE0);
}


void Assembler::fcos() {
  EnsureSpace ensure_space(this);
  emit(0xD9);
  emit(0xFF);
}


void Assembler::fsin() {
  EnsureSpace ensure_space(this);
  emit(0xD9);
  emit(0xFE);
}


2281 2282 2283 2284 2285 2286 2287
void Assembler::fptan() {
  EnsureSpace ensure_space(this);
  emit(0xD9);
  emit(0xF2);
}


2288 2289 2290 2291 2292 2293 2294
void Assembler::fyl2x() {
  EnsureSpace ensure_space(this);
  emit(0xD9);
  emit(0xF1);
}


2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315
void Assembler::f2xm1() {
  EnsureSpace ensure_space(this);
  emit(0xD9);
  emit(0xF0);
}


void Assembler::fscale() {
  EnsureSpace ensure_space(this);
  emit(0xD9);
  emit(0xFD);
}


void Assembler::fninit() {
  EnsureSpace ensure_space(this);
  emit(0xDB);
  emit(0xE3);
}


2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329
void Assembler::fadd(int i) {
  EnsureSpace ensure_space(this);
  emit_farith(0xDC, 0xC0, i);
}


void Assembler::fsub(int i) {
  EnsureSpace ensure_space(this);
  emit_farith(0xDC, 0xE8, i);
}


void Assembler::fisub_s(const Operand& adr) {
  EnsureSpace ensure_space(this);
2330
  emit_optional_rex_32(adr);
2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430
  emit(0xDA);
  emit_operand(4, adr);
}


void Assembler::fmul(int i) {
  EnsureSpace ensure_space(this);
  emit_farith(0xDC, 0xC8, i);
}


void Assembler::fdiv(int i) {
  EnsureSpace ensure_space(this);
  emit_farith(0xDC, 0xF8, i);
}


void Assembler::faddp(int i) {
  EnsureSpace ensure_space(this);
  emit_farith(0xDE, 0xC0, i);
}


void Assembler::fsubp(int i) {
  EnsureSpace ensure_space(this);
  emit_farith(0xDE, 0xE8, i);
}


void Assembler::fsubrp(int i) {
  EnsureSpace ensure_space(this);
  emit_farith(0xDE, 0xE0, i);
}


void Assembler::fmulp(int i) {
  EnsureSpace ensure_space(this);
  emit_farith(0xDE, 0xC8, i);
}


void Assembler::fdivp(int i) {
  EnsureSpace ensure_space(this);
  emit_farith(0xDE, 0xF8, i);
}


void Assembler::fprem() {
  EnsureSpace ensure_space(this);
  emit(0xD9);
  emit(0xF8);
}


void Assembler::fprem1() {
  EnsureSpace ensure_space(this);
  emit(0xD9);
  emit(0xF5);
}


void Assembler::fxch(int i) {
  EnsureSpace ensure_space(this);
  emit_farith(0xD9, 0xC8, i);
}


void Assembler::fincstp() {
  EnsureSpace ensure_space(this);
  emit(0xD9);
  emit(0xF7);
}


void Assembler::ffree(int i) {
  EnsureSpace ensure_space(this);
  emit_farith(0xDD, 0xC0, i);
}


void Assembler::ftst() {
  EnsureSpace ensure_space(this);
  emit(0xD9);
  emit(0xE4);
}


void Assembler::fucomp(int i) {
  EnsureSpace ensure_space(this);
  emit_farith(0xDD, 0xE8, i);
}


void Assembler::fucompp() {
  EnsureSpace ensure_space(this);
  emit(0xDA);
  emit(0xE9);
}


2431 2432 2433 2434 2435 2436 2437
void Assembler::fucomi(int i) {
  EnsureSpace ensure_space(this);
  emit(0xDB);
  emit(0xE8 + i);
}


2438 2439 2440 2441 2442 2443 2444
void Assembler::fucomip() {
  EnsureSpace ensure_space(this);
  emit(0xDF);
  emit(0xE9);
}


2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478
void Assembler::fcompp() {
  EnsureSpace ensure_space(this);
  emit(0xDE);
  emit(0xD9);
}


void Assembler::fnstsw_ax() {
  EnsureSpace ensure_space(this);
  emit(0xDF);
  emit(0xE0);
}


void Assembler::fwait() {
  EnsureSpace ensure_space(this);
  emit(0x9B);
}


void Assembler::frndint() {
  EnsureSpace ensure_space(this);
  emit(0xD9);
  emit(0xFC);
}


void Assembler::fnclex() {
  EnsureSpace ensure_space(this);
  emit(0xDB);
  emit(0xE2);
}


2479 2480 2481
void Assembler::sahf() {
  // TODO(X64): Test for presence. Not all 64-bit intel CPU's have sahf
  // in 64-bit mode. Test CpuID.
2482
  DCHECK(IsEnabled(SAHF));
2483 2484 2485 2486 2487
  EnsureSpace ensure_space(this);
  emit(0x9E);
}


2488
void Assembler::emit_farith(int b1, int b2, int i) {
2489 2490
  DCHECK(is_uint8(b1) && is_uint8(b2));  // wrong opcode
  DCHECK(is_uint3(i));  // illegal stack offset
2491 2492 2493 2494
  emit(b1);
  emit(b2 + i);
}

2495

2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506
// SSE operations.

void Assembler::andps(XMMRegister dst, XMMRegister src) {
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x54);
  emit_sse_operand(dst, src);
}


2507 2508 2509 2510 2511 2512 2513 2514 2515
void Assembler::andps(XMMRegister dst, const Operand& src) {
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x54);
  emit_sse_operand(dst, src);
}


2516 2517 2518 2519 2520 2521 2522 2523 2524
void Assembler::orps(XMMRegister dst, XMMRegister src) {
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x56);
  emit_sse_operand(dst, src);
}


2525 2526 2527 2528 2529 2530 2531 2532 2533
void Assembler::orps(XMMRegister dst, const Operand& src) {
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x56);
  emit_sse_operand(dst, src);
}


2534
void Assembler::xorps(XMMRegister dst, XMMRegister src) {
2535
  DCHECK(!IsEnabled(AVX));
2536 2537 2538 2539 2540 2541 2542 2543
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x57);
  emit_sse_operand(dst, src);
}


2544
void Assembler::xorps(XMMRegister dst, const Operand& src) {
2545
  DCHECK(!IsEnabled(AVX));
2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x57);
  emit_sse_operand(dst, src);
}


void Assembler::addps(XMMRegister dst, XMMRegister src) {
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x58);
  emit_sse_operand(dst, src);
}


void Assembler::addps(XMMRegister dst, const Operand& src) {
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x58);
  emit_sse_operand(dst, src);
}


void Assembler::subps(XMMRegister dst, XMMRegister src) {
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x5C);
  emit_sse_operand(dst, src);
}


void Assembler::subps(XMMRegister dst, const Operand& src) {
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x5C);
  emit_sse_operand(dst, src);
}


void Assembler::mulps(XMMRegister dst, XMMRegister src) {
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x59);
  emit_sse_operand(dst, src);
}


void Assembler::mulps(XMMRegister dst, const Operand& src) {
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x59);
  emit_sse_operand(dst, src);
}


void Assembler::divps(XMMRegister dst, XMMRegister src) {
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x5E);
  emit_sse_operand(dst, src);
}


void Assembler::divps(XMMRegister dst, const Operand& src) {
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x5E);
  emit_sse_operand(dst, src);
}


2626
// SSE 2 operations.
2627

2628
void Assembler::movd(XMMRegister dst, Register src) {
2629
  DCHECK(!IsEnabled(AVX));
2630 2631 2632 2633 2634 2635 2636 2637 2638
  EnsureSpace ensure_space(this);
  emit(0x66);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x6E);
  emit_sse_operand(dst, src);
}


2639
void Assembler::movd(XMMRegister dst, const Operand& src) {
2640
  DCHECK(!IsEnabled(AVX));
2641 2642 2643 2644 2645 2646 2647 2648 2649
  EnsureSpace ensure_space(this);
  emit(0x66);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x6E);
  emit_sse_operand(dst, src);
}


2650
void Assembler::movd(Register dst, XMMRegister src) {
2651
  DCHECK(!IsEnabled(AVX));
2652 2653
  EnsureSpace ensure_space(this);
  emit(0x66);
2654
  emit_optional_rex_32(src, dst);
2655 2656
  emit(0x0F);
  emit(0x7E);
2657
  emit_sse_operand(src, dst);
2658 2659 2660 2661
}


void Assembler::movq(XMMRegister dst, Register src) {
2662
  DCHECK(!IsEnabled(AVX));
2663 2664 2665 2666 2667 2668 2669 2670 2671 2672
  EnsureSpace ensure_space(this);
  emit(0x66);
  emit_rex_64(dst, src);
  emit(0x0F);
  emit(0x6E);
  emit_sse_operand(dst, src);
}


void Assembler::movq(Register dst, XMMRegister src) {
2673
  DCHECK(!IsEnabled(AVX));
2674 2675
  EnsureSpace ensure_space(this);
  emit(0x66);
2676
  emit_rex_64(src, dst);
2677 2678
  emit(0x0F);
  emit(0x7E);
2679
  emit_sse_operand(src, dst);
2680 2681 2682
}


lrn@chromium.org's avatar
lrn@chromium.org committed
2683
void Assembler::movq(XMMRegister dst, XMMRegister src) {
2684
  DCHECK(!IsEnabled(AVX));
lrn@chromium.org's avatar
lrn@chromium.org committed
2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701
  EnsureSpace ensure_space(this);
  if (dst.low_bits() == 4) {
    // Avoid unnecessary SIB byte.
    emit(0xf3);
    emit_optional_rex_32(dst, src);
    emit(0x0F);
    emit(0x7e);
    emit_sse_operand(dst, src);
  } else {
    emit(0x66);
    emit_optional_rex_32(src, dst);
    emit(0x0F);
    emit(0xD6);
    emit_sse_operand(src, dst);
  }
}

2702

2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722
void Assembler::movdqa(const Operand& dst, XMMRegister src) {
  EnsureSpace ensure_space(this);
  emit(0x66);
  emit_rex_64(src, dst);
  emit(0x0F);
  emit(0x7F);
  emit_sse_operand(src, dst);
}


void Assembler::movdqa(XMMRegister dst, const Operand& src) {
  EnsureSpace ensure_space(this);
  emit(0x66);
  emit_rex_64(dst, src);
  emit(0x0F);
  emit(0x6F);
  emit_sse_operand(dst, src);
}


2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742
void Assembler::movdqu(const Operand& dst, XMMRegister src) {
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_rex_64(src, dst);
  emit(0x0F);
  emit(0x7F);
  emit_sse_operand(src, dst);
}


void Assembler::movdqu(XMMRegister dst, const Operand& src) {
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_rex_64(dst, src);
  emit(0x0F);
  emit(0x6F);
  emit_sse_operand(dst, src);
}


2743
void Assembler::extractps(Register dst, XMMRegister src, byte imm8) {
2744 2745
  DCHECK(IsEnabled(SSE4_1));
  DCHECK(is_uint8(imm8));
2746 2747
  EnsureSpace ensure_space(this);
  emit(0x66);
2748
  emit_optional_rex_32(src, dst);
2749 2750 2751
  emit(0x0F);
  emit(0x3A);
  emit(0x17);
2752
  emit_sse_operand(src, dst);
2753 2754 2755 2756
  emit(imm8);
}


2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795
void Assembler::pextrd(Register dst, XMMRegister src, int8_t imm8) {
  DCHECK(IsEnabled(SSE4_1));
  EnsureSpace ensure_space(this);
  emit(0x66);
  emit_optional_rex_32(src, dst);
  emit(0x0F);
  emit(0x3A);
  emit(0x16);
  emit_sse_operand(src, dst);
  emit(imm8);
}


void Assembler::pinsrd(XMMRegister dst, Register src, int8_t imm8) {
  DCHECK(IsEnabled(SSE4_1));
  EnsureSpace ensure_space(this);
  emit(0x66);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x3A);
  emit(0x22);
  emit_sse_operand(dst, src);
  emit(imm8);
}


void Assembler::pinsrd(XMMRegister dst, const Operand& src, int8_t imm8) {
  DCHECK(IsEnabled(SSE4_1));
  EnsureSpace ensure_space(this);
  emit(0x66);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x3A);
  emit(0x22);
  emit_sse_operand(dst, src);
  emit(imm8);
}


2796
void Assembler::movsd(const Operand& dst, XMMRegister src) {
2797
  DCHECK(!IsEnabled(AVX));
2798 2799 2800 2801 2802 2803 2804 2805 2806
  EnsureSpace ensure_space(this);
  emit(0xF2);  // double
  emit_optional_rex_32(src, dst);
  emit(0x0F);
  emit(0x11);  // store
  emit_sse_operand(src, dst);
}


2807
void Assembler::movsd(XMMRegister dst, XMMRegister src) {
2808
  DCHECK(!IsEnabled(AVX));
2809 2810 2811 2812 2813 2814 2815 2816 2817 2818
  EnsureSpace ensure_space(this);
  emit(0xF2);  // double
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x10);  // load
  emit_sse_operand(dst, src);
}


void Assembler::movsd(XMMRegister dst, const Operand& src) {
2819
  DCHECK(!IsEnabled(AVX));
2820 2821 2822 2823 2824 2825 2826 2827 2828
  EnsureSpace ensure_space(this);
  emit(0xF2);  // double
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x10);  // load
  emit_sse_operand(dst, src);
}


lrn@chromium.org's avatar
lrn@chromium.org committed
2829
void Assembler::movaps(XMMRegister dst, XMMRegister src) {
2830
  DCHECK(!IsEnabled(AVX));
lrn@chromium.org's avatar
lrn@chromium.org committed
2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846
  EnsureSpace ensure_space(this);
  if (src.low_bits() == 4) {
    // Try to avoid an unnecessary SIB byte.
    emit_optional_rex_32(src, dst);
    emit(0x0F);
    emit(0x29);
    emit_sse_operand(src, dst);
  } else {
    emit_optional_rex_32(dst, src);
    emit(0x0F);
    emit(0x28);
    emit_sse_operand(dst, src);
  }
}


2847
void Assembler::shufps(XMMRegister dst, XMMRegister src, byte imm8) {
2848
  DCHECK(is_uint8(imm8));
2849 2850 2851 2852 2853 2854 2855 2856 2857
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(src, dst);
  emit(0x0F);
  emit(0xC6);
  emit_sse_operand(dst, src);
  emit(imm8);
}


lrn@chromium.org's avatar
lrn@chromium.org committed
2858
void Assembler::movapd(XMMRegister dst, XMMRegister src) {
2859
  DCHECK(!IsEnabled(AVX));
lrn@chromium.org's avatar
lrn@chromium.org committed
2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877
  EnsureSpace ensure_space(this);
  if (src.low_bits() == 4) {
    // Try to avoid an unnecessary SIB byte.
    emit(0x66);
    emit_optional_rex_32(src, dst);
    emit(0x0F);
    emit(0x29);
    emit_sse_operand(src, dst);
  } else {
    emit(0x66);
    emit_optional_rex_32(dst, src);
    emit(0x0F);
    emit(0x28);
    emit_sse_operand(dst, src);
  }
}


2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 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
void Assembler::addss(XMMRegister dst, XMMRegister src) {
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x58);
  emit_sse_operand(dst, src);
}


void Assembler::addss(XMMRegister dst, const Operand& src) {
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x58);
  emit_sse_operand(dst, src);
}


void Assembler::subss(XMMRegister dst, XMMRegister src) {
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x5C);
  emit_sse_operand(dst, src);
}


void Assembler::subss(XMMRegister dst, const Operand& src) {
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x5C);
  emit_sse_operand(dst, src);
}


void Assembler::mulss(XMMRegister dst, XMMRegister src) {
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x59);
  emit_sse_operand(dst, src);
}


void Assembler::mulss(XMMRegister dst, const Operand& src) {
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x59);
  emit_sse_operand(dst, src);
}


void Assembler::divss(XMMRegister dst, XMMRegister src) {
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x5E);
  emit_sse_operand(dst, src);
}


void Assembler::divss(XMMRegister dst, const Operand& src) {
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x5E);
  emit_sse_operand(dst, src);
}


2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017
void Assembler::maxss(XMMRegister dst, XMMRegister src) {
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x5F);
  emit_sse_operand(dst, src);
}


void Assembler::maxss(XMMRegister dst, const Operand& src) {
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x5F);
  emit_sse_operand(dst, src);
}


void Assembler::minss(XMMRegister dst, XMMRegister src) {
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x5D);
  emit_sse_operand(dst, src);
}


void Assembler::minss(XMMRegister dst, const Operand& src) {
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x5D);
  emit_sse_operand(dst, src);
}


void Assembler::sqrtss(XMMRegister dst, XMMRegister src) {
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x51);
  emit_sse_operand(dst, src);
}


void Assembler::sqrtss(XMMRegister dst, const Operand& src) {
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x51);
  emit_sse_operand(dst, src);
}


3018
void Assembler::ucomiss(XMMRegister dst, XMMRegister src) {
3019
  DCHECK(!IsEnabled(AVX));
3020 3021 3022 3023 3024 3025 3026 3027 3028
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(dst, src);
  emit(0x0f);
  emit(0x2e);
  emit_sse_operand(dst, src);
}


void Assembler::ucomiss(XMMRegister dst, const Operand& src) {
3029
  DCHECK(!IsEnabled(AVX));
3030 3031 3032 3033 3034
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(dst, src);
  emit(0x0f);
  emit(0x2e);
  emit_sse_operand(dst, src);
3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045
}


void Assembler::movss(XMMRegister dst, XMMRegister src) {
  DCHECK(!IsEnabled(AVX));
  EnsureSpace ensure_space(this);
  emit(0xF3);  // single
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x10);  // load
  emit_sse_operand(dst, src);
3046 3047 3048
}


3049
void Assembler::movss(XMMRegister dst, const Operand& src) {
3050
  DCHECK(!IsEnabled(AVX));
3051 3052 3053 3054 3055 3056 3057 3058 3059 3060
  EnsureSpace ensure_space(this);
  emit(0xF3);  // single
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x10);  // load
  emit_sse_operand(dst, src);
}


void Assembler::movss(const Operand& src, XMMRegister dst) {
3061
  DCHECK(!IsEnabled(AVX));
3062 3063 3064 3065 3066 3067 3068 3069 3070
  EnsureSpace ensure_space(this);
  emit(0xF3);  // single
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x11);  // store
  emit_sse_operand(dst, src);
}


3071
void Assembler::psllq(XMMRegister reg, byte imm8) {
3072
  DCHECK(!IsEnabled(AVX));
3073 3074
  EnsureSpace ensure_space(this);
  emit(0x66);
3075
  emit_optional_rex_32(reg);
3076 3077 3078 3079 3080 3081 3082
  emit(0x0F);
  emit(0x73);
  emit_sse_operand(rsi, reg);  // rsi == 6
  emit(imm8);
}


3083
void Assembler::psrlq(XMMRegister reg, byte imm8) {
3084
  DCHECK(!IsEnabled(AVX));
3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116
  EnsureSpace ensure_space(this);
  emit(0x66);
  emit_optional_rex_32(reg);
  emit(0x0F);
  emit(0x73);
  emit_sse_operand(rdx, reg);  // rdx == 2
  emit(imm8);
}


void Assembler::pslld(XMMRegister reg, byte imm8) {
  EnsureSpace ensure_space(this);
  emit(0x66);
  emit_optional_rex_32(reg);
  emit(0x0F);
  emit(0x72);
  emit_sse_operand(rsi, reg);  // rsi == 6
  emit(imm8);
}


void Assembler::psrld(XMMRegister reg, byte imm8) {
  EnsureSpace ensure_space(this);
  emit(0x66);
  emit_optional_rex_32(reg);
  emit(0x0F);
  emit(0x72);
  emit_sse_operand(rdx, reg);  // rdx == 2
  emit(imm8);
}


3117
void Assembler::cvttss2si(Register dst, const Operand& src) {
3118
  DCHECK(!IsEnabled(AVX));
3119 3120 3121 3122 3123 3124 3125 3126 3127
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x2C);
  emit_operand(dst, src);
}


3128
void Assembler::cvttss2si(Register dst, XMMRegister src) {
3129
  DCHECK(!IsEnabled(AVX));
3130 3131 3132 3133 3134 3135 3136 3137 3138
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x2C);
  emit_sse_operand(dst, src);
}


3139
void Assembler::cvttsd2si(Register dst, const Operand& src) {
3140
  DCHECK(!IsEnabled(AVX));
3141 3142 3143 3144 3145 3146 3147 3148 3149
  EnsureSpace ensure_space(this);
  emit(0xF2);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x2C);
  emit_operand(dst, src);
}


3150
void Assembler::cvttsd2si(Register dst, XMMRegister src) {
3151
  DCHECK(!IsEnabled(AVX));
3152 3153 3154 3155 3156 3157 3158 3159 3160
  EnsureSpace ensure_space(this);
  emit(0xF2);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x2C);
  emit_sse_operand(dst, src);
}


3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182
void Assembler::cvttss2siq(Register dst, XMMRegister src) {
  DCHECK(!IsEnabled(AVX));
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_rex_64(dst, src);
  emit(0x0F);
  emit(0x2C);
  emit_sse_operand(dst, src);
}


void Assembler::cvttss2siq(Register dst, const Operand& src) {
  DCHECK(!IsEnabled(AVX));
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_rex_64(dst, src);
  emit(0x0F);
  emit(0x2C);
  emit_sse_operand(dst, src);
}


3183
void Assembler::cvttsd2siq(Register dst, XMMRegister src) {
3184
  DCHECK(!IsEnabled(AVX));
3185 3186 3187 3188 3189 3190 3191 3192 3193
  EnsureSpace ensure_space(this);
  emit(0xF2);
  emit_rex_64(dst, src);
  emit(0x0F);
  emit(0x2C);
  emit_sse_operand(dst, src);
}


3194
void Assembler::cvttsd2siq(Register dst, const Operand& src) {
3195
  DCHECK(!IsEnabled(AVX));
3196 3197 3198 3199 3200 3201 3202 3203 3204
  EnsureSpace ensure_space(this);
  emit(0xF2);
  emit_rex_64(dst, src);
  emit(0x0F);
  emit(0x2C);
  emit_sse_operand(dst, src);
}


3205
void Assembler::cvtlsi2sd(XMMRegister dst, const Operand& src) {
3206
  DCHECK(!IsEnabled(AVX));
3207 3208 3209 3210 3211 3212 3213 3214 3215 3216
  EnsureSpace ensure_space(this);
  emit(0xF2);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x2A);
  emit_sse_operand(dst, src);
}


void Assembler::cvtlsi2sd(XMMRegister dst, Register src) {
3217
  DCHECK(!IsEnabled(AVX));
3218 3219 3220 3221 3222 3223 3224 3225 3226
  EnsureSpace ensure_space(this);
  emit(0xF2);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x2A);
  emit_sse_operand(dst, src);
}


3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237
void Assembler::cvtlsi2ss(XMMRegister dst, const Operand& src) {
  DCHECK(!IsEnabled(AVX));
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x2A);
  emit_sse_operand(dst, src);
}


3238 3239 3240 3241 3242 3243 3244 3245 3246 3247
void Assembler::cvtlsi2ss(XMMRegister dst, Register src) {
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x2A);
  emit_sse_operand(dst, src);
}


3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269
void Assembler::cvtqsi2ss(XMMRegister dst, const Operand& src) {
  DCHECK(!IsEnabled(AVX));
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_rex_64(dst, src);
  emit(0x0F);
  emit(0x2A);
  emit_sse_operand(dst, src);
}


void Assembler::cvtqsi2ss(XMMRegister dst, Register src) {
  DCHECK(!IsEnabled(AVX));
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_rex_64(dst, src);
  emit(0x0F);
  emit(0x2A);
  emit_sse_operand(dst, src);
}


3270
void Assembler::cvtqsi2sd(XMMRegister dst, const Operand& src) {
3271
  DCHECK(!IsEnabled(AVX));
3272 3273 3274 3275 3276 3277 3278 3279 3280
  EnsureSpace ensure_space(this);
  emit(0xF2);
  emit_rex_64(dst, src);
  emit(0x0F);
  emit(0x2A);
  emit_sse_operand(dst, src);
}


3281
void Assembler::cvtqsi2sd(XMMRegister dst, Register src) {
3282
  DCHECK(!IsEnabled(AVX));
3283 3284 3285 3286 3287 3288 3289 3290 3291
  EnsureSpace ensure_space(this);
  emit(0xF2);
  emit_rex_64(dst, src);
  emit(0x0F);
  emit(0x2A);
  emit_sse_operand(dst, src);
}


3292
void Assembler::cvtss2sd(XMMRegister dst, XMMRegister src) {
3293
  DCHECK(!IsEnabled(AVX));
3294 3295 3296 3297 3298 3299 3300 3301 3302
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x5A);
  emit_sse_operand(dst, src);
}


3303
void Assembler::cvtss2sd(XMMRegister dst, const Operand& src) {
3304
  DCHECK(!IsEnabled(AVX));
3305 3306 3307 3308 3309 3310 3311 3312 3313 3314
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x5A);
  emit_sse_operand(dst, src);
}


void Assembler::cvtsd2ss(XMMRegister dst, XMMRegister src) {
3315
  DCHECK(!IsEnabled(AVX));
3316 3317 3318 3319 3320 3321 3322 3323 3324
  EnsureSpace ensure_space(this);
  emit(0xF2);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x5A);
  emit_sse_operand(dst, src);
}


3325
void Assembler::cvtsd2ss(XMMRegister dst, const Operand& src) {
3326
  DCHECK(!IsEnabled(AVX));
3327 3328 3329 3330 3331 3332 3333 3334 3335
  EnsureSpace ensure_space(this);
  emit(0xF2);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x5A);
  emit_sse_operand(dst, src);
}


3336
void Assembler::cvtsd2si(Register dst, XMMRegister src) {
3337
  DCHECK(!IsEnabled(AVX));
3338 3339 3340 3341 3342 3343 3344 3345 3346 3347
  EnsureSpace ensure_space(this);
  emit(0xF2);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x2D);
  emit_sse_operand(dst, src);
}


void Assembler::cvtsd2siq(Register dst, XMMRegister src) {
3348
  DCHECK(!IsEnabled(AVX));
3349 3350 3351 3352 3353 3354 3355 3356 3357
  EnsureSpace ensure_space(this);
  emit(0xF2);
  emit_rex_64(dst, src);
  emit(0x0F);
  emit(0x2D);
  emit_sse_operand(dst, src);
}


3358 3359 3360 3361 3362 3363 3364 3365 3366 3367
void Assembler::addsd(XMMRegister dst, XMMRegister src) {
  EnsureSpace ensure_space(this);
  emit(0xF2);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x58);
  emit_sse_operand(dst, src);
}


3368 3369 3370 3371 3372 3373 3374 3375 3376 3377
void Assembler::addsd(XMMRegister dst, const Operand& src) {
  EnsureSpace ensure_space(this);
  emit(0xF2);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x58);
  emit_sse_operand(dst, src);
}


3378 3379 3380 3381 3382 3383 3384 3385 3386 3387
void Assembler::mulsd(XMMRegister dst, XMMRegister src) {
  EnsureSpace ensure_space(this);
  emit(0xF2);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x59);
  emit_sse_operand(dst, src);
}


3388 3389 3390 3391 3392 3393 3394 3395 3396 3397
void Assembler::mulsd(XMMRegister dst, const Operand& src) {
  EnsureSpace ensure_space(this);
  emit(0xF2);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x59);
  emit_sse_operand(dst, src);
}


3398 3399 3400 3401 3402 3403 3404 3405 3406 3407
void Assembler::subsd(XMMRegister dst, XMMRegister src) {
  EnsureSpace ensure_space(this);
  emit(0xF2);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x5C);
  emit_sse_operand(dst, src);
}


3408 3409 3410 3411 3412 3413 3414 3415 3416 3417
void Assembler::subsd(XMMRegister dst, const Operand& src) {
  EnsureSpace ensure_space(this);
  emit(0xF2);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x5C);
  emit_sse_operand(dst, src);
}


3418 3419 3420 3421 3422 3423 3424 3425 3426 3427
void Assembler::divsd(XMMRegister dst, XMMRegister src) {
  EnsureSpace ensure_space(this);
  emit(0xF2);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x5E);
  emit_sse_operand(dst, src);
}


3428 3429 3430 3431 3432 3433 3434 3435 3436 3437
void Assembler::divsd(XMMRegister dst, const Operand& src) {
  EnsureSpace ensure_space(this);
  emit(0xF2);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x5E);
  emit_sse_operand(dst, src);
}


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
void Assembler::maxsd(XMMRegister dst, XMMRegister src) {
  EnsureSpace ensure_space(this);
  emit(0xF2);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x5F);
  emit_sse_operand(dst, src);
}


void Assembler::maxsd(XMMRegister dst, const Operand& src) {
  EnsureSpace ensure_space(this);
  emit(0xF2);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x5F);
  emit_sse_operand(dst, src);
}


void Assembler::minsd(XMMRegister dst, XMMRegister src) {
  EnsureSpace ensure_space(this);
  emit(0xF2);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x5D);
  emit_sse_operand(dst, src);
}


void Assembler::minsd(XMMRegister dst, const Operand& src) {
  EnsureSpace ensure_space(this);
  emit(0xF2);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x5D);
  emit_sse_operand(dst, src);
}


3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497
void Assembler::andpd(XMMRegister dst, XMMRegister src) {
  EnsureSpace ensure_space(this);
  emit(0x66);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x54);
  emit_sse_operand(dst, src);
}


void Assembler::orpd(XMMRegister dst, XMMRegister src) {
  EnsureSpace ensure_space(this);
  emit(0x66);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x56);
  emit_sse_operand(dst, src);
}


3498
void Assembler::xorpd(XMMRegister dst, XMMRegister src) {
3499
  DCHECK(!IsEnabled(AVX));
3500 3501 3502
  EnsureSpace ensure_space(this);
  emit(0x66);
  emit_optional_rex_32(dst, src);
3503
  emit(0x0F);
lrn@chromium.org's avatar
lrn@chromium.org committed
3504 3505 3506 3507 3508
  emit(0x57);
  emit_sse_operand(dst, src);
}


3509
void Assembler::sqrtsd(XMMRegister dst, XMMRegister src) {
3510
  DCHECK(!IsEnabled(AVX));
3511 3512 3513 3514 3515 3516 3517 3518 3519
  EnsureSpace ensure_space(this);
  emit(0xF2);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x51);
  emit_sse_operand(dst, src);
}


3520
void Assembler::sqrtsd(XMMRegister dst, const Operand& src) {
3521
  DCHECK(!IsEnabled(AVX));
3522 3523 3524 3525 3526 3527 3528 3529 3530
  EnsureSpace ensure_space(this);
  emit(0xF2);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x51);
  emit_sse_operand(dst, src);
}


3531
void Assembler::ucomisd(XMMRegister dst, XMMRegister src) {
3532
  DCHECK(!IsEnabled(AVX));
3533 3534 3535 3536 3537 3538 3539 3540
  EnsureSpace ensure_space(this);
  emit(0x66);
  emit_optional_rex_32(dst, src);
  emit(0x0f);
  emit(0x2e);
  emit_sse_operand(dst, src);
}

3541

3542
void Assembler::ucomisd(XMMRegister dst, const Operand& src) {
3543
  DCHECK(!IsEnabled(AVX));
3544 3545 3546 3547 3548 3549 3550 3551 3552
  EnsureSpace ensure_space(this);
  emit(0x66);
  emit_optional_rex_32(dst, src);
  emit(0x0f);
  emit(0x2e);
  emit_sse_operand(dst, src);
}


3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563
void Assembler::cmpltsd(XMMRegister dst, XMMRegister src) {
  EnsureSpace ensure_space(this);
  emit(0xF2);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0xC2);
  emit_sse_operand(dst, src);
  emit(0x01);  // LT == 1
}


3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578
void Assembler::roundss(XMMRegister dst, XMMRegister src, RoundingMode mode) {
  DCHECK(!IsEnabled(AVX));
  DCHECK(IsEnabled(SSE4_1));
  EnsureSpace ensure_space(this);
  emit(0x66);
  emit_optional_rex_32(dst, src);
  emit(0x0f);
  emit(0x3a);
  emit(0x0a);
  emit_sse_operand(dst, src);
  // Mask precision exception.
  emit(static_cast<byte>(mode) | 0x8);
}


3579
void Assembler::roundsd(XMMRegister dst, XMMRegister src, RoundingMode mode) {
3580
  DCHECK(!IsEnabled(AVX));
3581
  DCHECK(IsEnabled(SSE4_1));
3582 3583 3584 3585 3586 3587 3588
  EnsureSpace ensure_space(this);
  emit(0x66);
  emit_optional_rex_32(dst, src);
  emit(0x0f);
  emit(0x3a);
  emit(0x0b);
  emit_sse_operand(dst, src);
3589
  // Mask precision exception.
3590 3591 3592 3593
  emit(static_cast<byte>(mode) | 0x8);
}


3594 3595 3596 3597 3598 3599 3600 3601 3602
void Assembler::movmskpd(Register dst, XMMRegister src) {
  EnsureSpace ensure_space(this);
  emit(0x66);
  emit_optional_rex_32(dst, src);
  emit(0x0f);
  emit(0x50);
  emit_sse_operand(dst, src);
}

3603

3604 3605 3606 3607 3608 3609 3610 3611 3612
void Assembler::movmskps(Register dst, XMMRegister src) {
  EnsureSpace ensure_space(this);
  emit_optional_rex_32(dst, src);
  emit(0x0f);
  emit(0x50);
  emit_sse_operand(dst, src);
}


3613
void Assembler::pcmpeqd(XMMRegister dst, XMMRegister src) {
3614
  DCHECK(!IsEnabled(AVX));
3615 3616 3617 3618 3619 3620 3621 3622 3623
  EnsureSpace ensure_space(this);
  emit(0x66);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x76);
  emit_sse_operand(dst, src);
}


3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643
void Assembler::punpckldq(XMMRegister dst, XMMRegister src) {
  EnsureSpace ensure_space(this);
  emit(0x66);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x62);
  emit_sse_operand(dst, src);
}


void Assembler::punpckhdq(XMMRegister dst, XMMRegister src) {
  EnsureSpace ensure_space(this);
  emit(0x66);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0x6A);
  emit_sse_operand(dst, src);
}


3644
// AVX instructions
3645 3646 3647 3648
void Assembler::vfmasd(byte op, XMMRegister dst, XMMRegister src1,
                       XMMRegister src2) {
  DCHECK(IsEnabled(FMA3));
  EnsureSpace ensure_space(this);
3649
  emit_vex_prefix(dst, src1, src2, kLIG, k66, k0F38, kW1);
3650 3651 3652 3653 3654 3655 3656 3657 3658
  emit(op);
  emit_sse_operand(dst, src2);
}


void Assembler::vfmasd(byte op, XMMRegister dst, XMMRegister src1,
                       const Operand& src2) {
  DCHECK(IsEnabled(FMA3));
  EnsureSpace ensure_space(this);
3659
  emit_vex_prefix(dst, src1, src2, kLIG, k66, k0F38, kW1);
3660 3661 3662 3663 3664 3665 3666 3667 3668
  emit(op);
  emit_sse_operand(dst, src2);
}


void Assembler::vfmass(byte op, XMMRegister dst, XMMRegister src1,
                       XMMRegister src2) {
  DCHECK(IsEnabled(FMA3));
  EnsureSpace ensure_space(this);
3669
  emit_vex_prefix(dst, src1, src2, kLIG, k66, k0F38, kW0);
3670 3671 3672 3673 3674 3675 3676 3677 3678
  emit(op);
  emit_sse_operand(dst, src2);
}


void Assembler::vfmass(byte op, XMMRegister dst, XMMRegister src1,
                       const Operand& src2) {
  DCHECK(IsEnabled(FMA3));
  EnsureSpace ensure_space(this);
3679 3680 3681 3682 3683 3684
  emit_vex_prefix(dst, src1, src2, kLIG, k66, k0F38, kW0);
  emit(op);
  emit_sse_operand(dst, src2);
}


3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742
void Assembler::vmovd(XMMRegister dst, Register src) {
  DCHECK(IsEnabled(AVX));
  EnsureSpace ensure_space(this);
  XMMRegister isrc = {src.code()};
  emit_vex_prefix(dst, xmm0, isrc, kL128, k66, k0F, kW0);
  emit(0x6e);
  emit_sse_operand(dst, src);
}


void Assembler::vmovd(XMMRegister dst, const Operand& src) {
  DCHECK(IsEnabled(AVX));
  EnsureSpace ensure_space(this);
  emit_vex_prefix(dst, xmm0, src, kL128, k66, k0F, kW0);
  emit(0x6e);
  emit_sse_operand(dst, src);
}


void Assembler::vmovd(Register dst, XMMRegister src) {
  DCHECK(IsEnabled(AVX));
  EnsureSpace ensure_space(this);
  XMMRegister idst = {dst.code()};
  emit_vex_prefix(src, xmm0, idst, kL128, k66, k0F, kW0);
  emit(0x7e);
  emit_sse_operand(src, dst);
}


void Assembler::vmovq(XMMRegister dst, Register src) {
  DCHECK(IsEnabled(AVX));
  EnsureSpace ensure_space(this);
  XMMRegister isrc = {src.code()};
  emit_vex_prefix(dst, xmm0, isrc, kL128, k66, k0F, kW1);
  emit(0x6e);
  emit_sse_operand(dst, src);
}


void Assembler::vmovq(XMMRegister dst, const Operand& src) {
  DCHECK(IsEnabled(AVX));
  EnsureSpace ensure_space(this);
  emit_vex_prefix(dst, xmm0, src, kL128, k66, k0F, kW1);
  emit(0x6e);
  emit_sse_operand(dst, src);
}


void Assembler::vmovq(Register dst, XMMRegister src) {
  DCHECK(IsEnabled(AVX));
  EnsureSpace ensure_space(this);
  XMMRegister idst = {dst.code()};
  emit_vex_prefix(src, xmm0, idst, kL128, k66, k0F, kW1);
  emit(0x7e);
  emit_sse_operand(src, dst);
}


3743
void Assembler::vsd(byte op, XMMRegister dst, XMMRegister src1,
3744
                    XMMRegister src2, SIMDPrefix pp, LeadingOpcode m, VexW w) {
3745 3746
  DCHECK(IsEnabled(AVX));
  EnsureSpace ensure_space(this);
3747
  emit_vex_prefix(dst, src1, src2, kLIG, pp, m, w);
3748 3749 3750 3751 3752 3753
  emit(op);
  emit_sse_operand(dst, src2);
}


void Assembler::vsd(byte op, XMMRegister dst, XMMRegister src1,
3754 3755
                    const Operand& src2, SIMDPrefix pp, LeadingOpcode m,
                    VexW w) {
3756 3757
  DCHECK(IsEnabled(AVX));
  EnsureSpace ensure_space(this);
3758
  emit_vex_prefix(dst, src1, src2, kLIG, pp, m, w);
3759 3760 3761 3762 3763
  emit(op);
  emit_sse_operand(dst, src2);
}


3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803
void Assembler::vps(byte op, XMMRegister dst, XMMRegister src1,
                    XMMRegister src2) {
  DCHECK(IsEnabled(AVX));
  EnsureSpace ensure_space(this);
  emit_vex_prefix(dst, src1, src2, kL128, kNone, k0F, kWIG);
  emit(op);
  emit_sse_operand(dst, src2);
}


void Assembler::vps(byte op, XMMRegister dst, XMMRegister src1,
                    const Operand& src2) {
  DCHECK(IsEnabled(AVX));
  EnsureSpace ensure_space(this);
  emit_vex_prefix(dst, src1, src2, kL128, kNone, k0F, kWIG);
  emit(op);
  emit_sse_operand(dst, src2);
}


void Assembler::vpd(byte op, XMMRegister dst, XMMRegister src1,
                    XMMRegister src2) {
  DCHECK(IsEnabled(AVX));
  EnsureSpace ensure_space(this);
  emit_vex_prefix(dst, src1, src2, kL128, k66, k0F, kWIG);
  emit(op);
  emit_sse_operand(dst, src2);
}


void Assembler::vpd(byte op, XMMRegister dst, XMMRegister src1,
                    const Operand& src2) {
  DCHECK(IsEnabled(AVX));
  EnsureSpace ensure_space(this);
  emit_vex_prefix(dst, src1, src2, kL128, k66, k0F, kWIG);
  emit(op);
  emit_sse_operand(dst, src2);
}


3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841
void Assembler::vucomiss(XMMRegister dst, XMMRegister src) {
  DCHECK(IsEnabled(AVX));
  EnsureSpace ensure_space(this);
  emit_vex_prefix(dst, xmm0, src, kLIG, kNone, k0F, kWIG);
  emit(0x2e);
  emit_sse_operand(dst, src);
}


void Assembler::vucomiss(XMMRegister dst, const Operand& src) {
  DCHECK(IsEnabled(AVX));
  EnsureSpace ensure_space(this);
  emit_vex_prefix(dst, xmm0, src, kLIG, kNone, k0F, kWIG);
  emit(0x2e);
  emit_sse_operand(dst, src);
}


void Assembler::vss(byte op, XMMRegister dst, XMMRegister src1,
                    XMMRegister src2) {
  DCHECK(IsEnabled(AVX));
  EnsureSpace ensure_space(this);
  emit_vex_prefix(dst, src1, src2, kLIG, kF3, k0F, kWIG);
  emit(op);
  emit_sse_operand(dst, src2);
}


void Assembler::vss(byte op, XMMRegister dst, XMMRegister src1,
                    const Operand& src2) {
  DCHECK(IsEnabled(AVX));
  EnsureSpace ensure_space(this);
  emit_vex_prefix(dst, src1, src2, kLIG, kF3, k0F, kWIG);
  emit(op);
  emit_sse_operand(dst, src2);
}


3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097
void Assembler::bmi1q(byte op, Register reg, Register vreg, Register rm) {
  DCHECK(IsEnabled(BMI1));
  EnsureSpace ensure_space(this);
  emit_vex_prefix(reg, vreg, rm, kLZ, kNone, k0F38, kW1);
  emit(op);
  emit_modrm(reg, rm);
}


void Assembler::bmi1q(byte op, Register reg, Register vreg, const Operand& rm) {
  DCHECK(IsEnabled(BMI1));
  EnsureSpace ensure_space(this);
  emit_vex_prefix(reg, vreg, rm, kLZ, kNone, k0F38, kW1);
  emit(op);
  emit_operand(reg, rm);
}


void Assembler::bmi1l(byte op, Register reg, Register vreg, Register rm) {
  DCHECK(IsEnabled(BMI1));
  EnsureSpace ensure_space(this);
  emit_vex_prefix(reg, vreg, rm, kLZ, kNone, k0F38, kW0);
  emit(op);
  emit_modrm(reg, rm);
}


void Assembler::bmi1l(byte op, Register reg, Register vreg, const Operand& rm) {
  DCHECK(IsEnabled(BMI1));
  EnsureSpace ensure_space(this);
  emit_vex_prefix(reg, vreg, rm, kLZ, kNone, k0F38, kW0);
  emit(op);
  emit_operand(reg, rm);
}


void Assembler::tzcntq(Register dst, Register src) {
  DCHECK(IsEnabled(BMI1));
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_rex_64(dst, src);
  emit(0x0F);
  emit(0xBC);
  emit_modrm(dst, src);
}


void Assembler::tzcntq(Register dst, const Operand& src) {
  DCHECK(IsEnabled(BMI1));
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_rex_64(dst, src);
  emit(0x0F);
  emit(0xBC);
  emit_operand(dst, src);
}


void Assembler::tzcntl(Register dst, Register src) {
  DCHECK(IsEnabled(BMI1));
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0xBC);
  emit_modrm(dst, src);
}


void Assembler::tzcntl(Register dst, const Operand& src) {
  DCHECK(IsEnabled(BMI1));
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0xBC);
  emit_operand(dst, src);
}


void Assembler::lzcntq(Register dst, Register src) {
  DCHECK(IsEnabled(LZCNT));
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_rex_64(dst, src);
  emit(0x0F);
  emit(0xBD);
  emit_modrm(dst, src);
}


void Assembler::lzcntq(Register dst, const Operand& src) {
  DCHECK(IsEnabled(LZCNT));
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_rex_64(dst, src);
  emit(0x0F);
  emit(0xBD);
  emit_operand(dst, src);
}


void Assembler::lzcntl(Register dst, Register src) {
  DCHECK(IsEnabled(LZCNT));
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0xBD);
  emit_modrm(dst, src);
}


void Assembler::lzcntl(Register dst, const Operand& src) {
  DCHECK(IsEnabled(LZCNT));
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0xBD);
  emit_operand(dst, src);
}


void Assembler::popcntq(Register dst, Register src) {
  DCHECK(IsEnabled(POPCNT));
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_rex_64(dst, src);
  emit(0x0F);
  emit(0xB8);
  emit_modrm(dst, src);
}


void Assembler::popcntq(Register dst, const Operand& src) {
  DCHECK(IsEnabled(POPCNT));
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_rex_64(dst, src);
  emit(0x0F);
  emit(0xB8);
  emit_operand(dst, src);
}


void Assembler::popcntl(Register dst, Register src) {
  DCHECK(IsEnabled(POPCNT));
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0xB8);
  emit_modrm(dst, src);
}


void Assembler::popcntl(Register dst, const Operand& src) {
  DCHECK(IsEnabled(POPCNT));
  EnsureSpace ensure_space(this);
  emit(0xF3);
  emit_optional_rex_32(dst, src);
  emit(0x0F);
  emit(0xB8);
  emit_operand(dst, src);
}


void Assembler::bmi2q(SIMDPrefix pp, byte op, Register reg, Register vreg,
                      Register rm) {
  DCHECK(IsEnabled(BMI2));
  EnsureSpace ensure_space(this);
  emit_vex_prefix(reg, vreg, rm, kLZ, pp, k0F38, kW1);
  emit(op);
  emit_modrm(reg, rm);
}


void Assembler::bmi2q(SIMDPrefix pp, byte op, Register reg, Register vreg,
                      const Operand& rm) {
  DCHECK(IsEnabled(BMI2));
  EnsureSpace ensure_space(this);
  emit_vex_prefix(reg, vreg, rm, kLZ, pp, k0F38, kW1);
  emit(op);
  emit_operand(reg, rm);
}


void Assembler::bmi2l(SIMDPrefix pp, byte op, Register reg, Register vreg,
                      Register rm) {
  DCHECK(IsEnabled(BMI2));
  EnsureSpace ensure_space(this);
  emit_vex_prefix(reg, vreg, rm, kLZ, pp, k0F38, kW0);
  emit(op);
  emit_modrm(reg, rm);
}


void Assembler::bmi2l(SIMDPrefix pp, byte op, Register reg, Register vreg,
                      const Operand& rm) {
  DCHECK(IsEnabled(BMI2));
  EnsureSpace ensure_space(this);
  emit_vex_prefix(reg, vreg, rm, kLZ, pp, k0F38, kW0);
  emit(op);
  emit_operand(reg, rm);
}


void Assembler::rorxq(Register dst, Register src, byte imm8) {
  DCHECK(IsEnabled(BMI2));
  DCHECK(is_uint8(imm8));
  Register vreg = {0};  // VEX.vvvv unused
  EnsureSpace ensure_space(this);
  emit_vex_prefix(dst, vreg, src, kLZ, kF2, k0F3A, kW1);
  emit(0xF0);
  emit_modrm(dst, src);
  emit(imm8);
}


void Assembler::rorxq(Register dst, const Operand& src, byte imm8) {
  DCHECK(IsEnabled(BMI2));
  DCHECK(is_uint8(imm8));
  Register vreg = {0};  // VEX.vvvv unused
  EnsureSpace ensure_space(this);
  emit_vex_prefix(dst, vreg, src, kLZ, kF2, k0F3A, kW1);
  emit(0xF0);
  emit_operand(dst, src);
  emit(imm8);
}


void Assembler::rorxl(Register dst, Register src, byte imm8) {
  DCHECK(IsEnabled(BMI2));
  DCHECK(is_uint8(imm8));
  Register vreg = {0};  // VEX.vvvv unused
  EnsureSpace ensure_space(this);
  emit_vex_prefix(dst, vreg, src, kLZ, kF2, k0F3A, kW0);
  emit(0xF0);
  emit_modrm(dst, src);
  emit(imm8);
}


void Assembler::rorxl(Register dst, const Operand& src, byte imm8) {
  DCHECK(IsEnabled(BMI2));
  DCHECK(is_uint8(imm8));
  Register vreg = {0};  // VEX.vvvv unused
  EnsureSpace ensure_space(this);
  emit_vex_prefix(dst, vreg, src, kLZ, kF2, k0F3A, kW0);
  emit(0xF0);
  emit_operand(dst, src);
  emit(imm8);
}


4098 4099 4100 4101 4102 4103
void Assembler::emit_sse_operand(XMMRegister reg, const Operand& adr) {
  Register ireg = { reg.code() };
  emit_operand(ireg, adr);
}


4104 4105 4106 4107 4108 4109
void Assembler::emit_sse_operand(Register reg, const Operand& adr) {
  Register ireg = {reg.code()};
  emit_operand(ireg, adr);
}


4110
void Assembler::emit_sse_operand(XMMRegister dst, XMMRegister src) {
4111
  emit(0xC0 | (dst.low_bits() << 3) | src.low_bits());
4112 4113
}

4114

4115
void Assembler::emit_sse_operand(XMMRegister dst, Register src) {
4116
  emit(0xC0 | (dst.low_bits() << 3) | src.low_bits());
4117 4118
}

4119

4120 4121 4122 4123
void Assembler::emit_sse_operand(Register dst, XMMRegister src) {
  emit(0xC0 | (dst.low_bits() << 3) | src.low_bits());
}

4124

4125 4126 4127 4128 4129 4130
void Assembler::db(uint8_t data) {
  EnsureSpace ensure_space(this);
  emit(data);
}


4131 4132 4133 4134 4135 4136
void Assembler::dd(uint32_t data) {
  EnsureSpace ensure_space(this);
  emitl(data);
}


4137 4138 4139 4140 4141 4142
void Assembler::dq(uint64_t data) {
  EnsureSpace ensure_space(this);
  emitq(data);
}


4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163
void Assembler::dq(Label* label) {
  EnsureSpace ensure_space(this);
  if (label->is_bound()) {
    internal_reference_positions_.push_back(pc_offset());
    emitp(buffer_ + label->pos(), RelocInfo::INTERNAL_REFERENCE);
  } else {
    RecordRelocInfo(RelocInfo::INTERNAL_REFERENCE);
    emitl(0);  // Zero for the first 32bit marks it as 64bit absolute address.
    if (label->is_linked()) {
      emitl(label->pos());
      label->link_to(pc_offset() - sizeof(int32_t));
    } else {
      DCHECK(label->is_unused());
      int32_t current = pc_offset();
      emitl(current);
      label->link_to(current);
    }
  }
}


4164
// Relocation information implementations.
4165 4166

void Assembler::RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data) {
4167
  DCHECK(!RelocInfo::IsNone(rmode));
4168 4169 4170 4171
  // Don't record external references unless the heap will be serialized.
  if (rmode == RelocInfo::EXTERNAL_REFERENCE &&
      !serializer_enabled() && !emit_debug_code()) {
    return;
4172 4173 4174
  } else if (rmode == RelocInfo::CODE_AGE_SEQUENCE) {
    // Don't record psuedo relocation info for code age sequence mode.
    return;
4175
  }
jochen's avatar
jochen committed
4176
  RelocInfo rinfo(isolate(), pc_, rmode, data, NULL);
4177 4178 4179
  reloc_info_writer.Write(&rinfo);
}

4180

4181
const int RelocInfo::kApplyMask = RelocInfo::kCodeTargetMask |
4182
    1 << RelocInfo::RUNTIME_ENTRY |
4183 4184
    1 << RelocInfo::INTERNAL_REFERENCE |
    1 << RelocInfo::CODE_AGE_SEQUENCE;
4185

4186 4187 4188 4189 4190 4191 4192 4193

bool RelocInfo::IsCodedSpecially() {
  // The deserializer needs to know whether a pointer is specially coded.  Being
  // specially coded on x64 means that it is a relative 32 bit address, as used
  // by branch instructions.
  return (1 << rmode_) & kApplyMask;
}

4194 4195 4196 4197 4198 4199

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


4200 4201
}  // namespace internal
}  // namespace v8
4202 4203

#endif  // V8_TARGET_ARCH_X64