V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
assembler-ia32.cc
1 // Copyright (c) 1994-2006 Sun Microsystems Inc.
2 // All Rights Reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions
6 // are met:
7 //
8 // - Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 //
11 // - Redistribution in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the
14 // distribution.
15 //
16 // - Neither the name of Sun Microsystems or the names of contributors may
17 // be used to endorse or promote products derived from this software without
18 // specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 // OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 // The original source code covered by the above license above has been modified
34 // significantly by Google Inc.
35 // Copyright 2012 the V8 project authors. All rights reserved.
36 
37 #include "src/ia32/assembler-ia32.h"
38 
39 #include <cstring>
40 
41 #if V8_TARGET_ARCH_IA32
42 
43 #if V8_LIBC_MSVCRT
44 #include <intrin.h> // _xgetbv()
45 #endif
46 #if V8_OS_MACOSX
47 #include <sys/sysctl.h>
48 #endif
49 
50 #include "src/assembler-inl.h"
51 #include "src/base/bits.h"
52 #include "src/base/cpu.h"
53 #include "src/code-stubs.h"
54 #include "src/conversions-inl.h"
55 #include "src/deoptimizer.h"
56 #include "src/disassembler.h"
57 #include "src/macro-assembler.h"
58 #include "src/string-constants.h"
59 #include "src/v8.h"
60 
61 namespace v8 {
62 namespace internal {
63 
64 Immediate Immediate::EmbeddedNumber(double value) {
65  int32_t smi;
66  if (DoubleToSmiInteger(value, &smi)) return Immediate(Smi::FromInt(smi));
67  Immediate result(0, RelocInfo::EMBEDDED_OBJECT);
68  result.is_heap_object_request_ = true;
69  result.value_.heap_object_request = HeapObjectRequest(value);
70  return result;
71 }
72 
73 Immediate Immediate::EmbeddedCode(CodeStub* stub) {
74  Immediate result(0, RelocInfo::CODE_TARGET);
75  result.is_heap_object_request_ = true;
76  result.value_.heap_object_request = HeapObjectRequest(stub);
77  return result;
78 }
79 
80 Immediate Immediate::EmbeddedStringConstant(const StringConstantBase* str) {
81  Immediate result(0, RelocInfo::EMBEDDED_OBJECT);
82  result.is_heap_object_request_ = true;
83  result.value_.heap_object_request = HeapObjectRequest(str);
84  return result;
85 }
86 
87 // -----------------------------------------------------------------------------
88 // Implementation of CpuFeatures
89 
90 namespace {
91 
92 #if !V8_LIBC_MSVCRT
93 
94 V8_INLINE uint64_t _xgetbv(unsigned int xcr) {
95  unsigned eax, edx;
96  // Check xgetbv; this uses a .byte sequence instead of the instruction
97  // directly because older assemblers do not include support for xgetbv and
98  // there is no easy way to conditionally compile based on the assembler
99  // used.
100  __asm__ volatile(".byte 0x0F, 0x01, 0xD0" : "=a"(eax), "=d"(edx) : "c"(xcr));
101  return static_cast<uint64_t>(eax) | (static_cast<uint64_t>(edx) << 32);
102 }
103 
104 #define _XCR_XFEATURE_ENABLED_MASK 0
105 
106 #endif // !V8_LIBC_MSVCRT
107 
108 
109 bool OSHasAVXSupport() {
110 #if V8_OS_MACOSX
111  // Mac OS X up to 10.9 has a bug where AVX transitions were indeed being
112  // caused by ISRs, so we detect that here and disable AVX in that case.
113  char buffer[128];
114  size_t buffer_size = arraysize(buffer);
115  int ctl_name[] = {CTL_KERN, KERN_OSRELEASE};
116  if (sysctl(ctl_name, 2, buffer, &buffer_size, nullptr, 0) != 0) {
117  FATAL("V8 failed to get kernel version");
118  }
119  // The buffer now contains a string of the form XX.YY.ZZ, where
120  // XX is the major kernel version component.
121  char* period_pos = strchr(buffer, '.');
122  DCHECK_NOT_NULL(period_pos);
123  *period_pos = '\0';
124  long kernel_version_major = strtol(buffer, nullptr, 10); // NOLINT
125  if (kernel_version_major <= 13) return false;
126 #endif // V8_OS_MACOSX
127  // Check whether OS claims to support AVX.
128  uint64_t feature_mask = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
129  return (feature_mask & 0x6) == 0x6;
130 }
131 
132 } // namespace
133 
134 
135 void CpuFeatures::ProbeImpl(bool cross_compile) {
136  base::CPU cpu;
137  CHECK(cpu.has_sse2()); // SSE2 support is mandatory.
138  CHECK(cpu.has_cmov()); // CMOV support is mandatory.
139 
140  // Only use statically determined features for cross compile (snapshot).
141  if (cross_compile) return;
142 
143  if (cpu.has_sse41() && FLAG_enable_sse4_1) supported_ |= 1u << SSE4_1;
144  if (cpu.has_ssse3() && FLAG_enable_ssse3) supported_ |= 1u << SSSE3;
145  if (cpu.has_sse3() && FLAG_enable_sse3) supported_ |= 1u << SSE3;
146  if (cpu.has_avx() && FLAG_enable_avx && cpu.has_osxsave() &&
147  OSHasAVXSupport()) {
148  supported_ |= 1u << AVX;
149  }
150  if (cpu.has_fma3() && FLAG_enable_fma3 && cpu.has_osxsave() &&
151  OSHasAVXSupport()) {
152  supported_ |= 1u << FMA3;
153  }
154  if (cpu.has_bmi1() && FLAG_enable_bmi1) supported_ |= 1u << BMI1;
155  if (cpu.has_bmi2() && FLAG_enable_bmi2) supported_ |= 1u << BMI2;
156  if (cpu.has_lzcnt() && FLAG_enable_lzcnt) supported_ |= 1u << LZCNT;
157  if (cpu.has_popcnt() && FLAG_enable_popcnt) supported_ |= 1u << POPCNT;
158  if (strcmp(FLAG_mcpu, "auto") == 0) {
159  if (cpu.is_atom()) supported_ |= 1u << ATOM;
160  } else if (strcmp(FLAG_mcpu, "atom") == 0) {
161  supported_ |= 1u << ATOM;
162  }
163 }
164 
165 
166 void CpuFeatures::PrintTarget() { }
167 void CpuFeatures::PrintFeatures() {
168  printf(
169  "SSE3=%d SSSE3=%d SSE4_1=%d AVX=%d FMA3=%d BMI1=%d BMI2=%d LZCNT=%d "
170  "POPCNT=%d ATOM=%d\n",
171  CpuFeatures::IsSupported(SSE3), CpuFeatures::IsSupported(SSSE3),
172  CpuFeatures::IsSupported(SSE4_1), CpuFeatures::IsSupported(AVX),
173  CpuFeatures::IsSupported(FMA3), CpuFeatures::IsSupported(BMI1),
174  CpuFeatures::IsSupported(BMI2), CpuFeatures::IsSupported(LZCNT),
175  CpuFeatures::IsSupported(POPCNT), CpuFeatures::IsSupported(ATOM));
176 }
177 
178 
179 // -----------------------------------------------------------------------------
180 // Implementation of Displacement
181 
182 void Displacement::init(Label* L, Type type) {
183  DCHECK(!L->is_bound());
184  int next = 0;
185  if (L->is_linked()) {
186  next = L->pos();
187  DCHECK_GT(next, 0); // Displacements must be at positions > 0
188  }
189  // Ensure that we _never_ overflow the next field.
190  DCHECK(NextField::is_valid(Assembler::kMaximalBufferSize));
191  data_ = NextField::encode(next) | TypeField::encode(type);
192 }
193 
194 
195 // -----------------------------------------------------------------------------
196 // Implementation of RelocInfo
197 
198 const int RelocInfo::kApplyMask =
199  RelocInfo::ModeMask(RelocInfo::CODE_TARGET) |
200  RelocInfo::ModeMask(RelocInfo::INTERNAL_REFERENCE) |
201  RelocInfo::ModeMask(RelocInfo::OFF_HEAP_TARGET) |
202  RelocInfo::ModeMask(RelocInfo::RUNTIME_ENTRY);
203 
204 bool RelocInfo::IsCodedSpecially() {
205  // The deserializer needs to know whether a pointer is specially coded. Being
206  // specially coded on IA32 means that it is a relative address, as used by
207  // branch instructions. These are also the ones that need changing when a
208  // code object moves.
209  return RelocInfo::ModeMask(rmode_) & kApplyMask;
210 }
211 
212 
213 bool RelocInfo::IsInConstantPool() {
214  return false;
215 }
216 
217 int RelocInfo::GetDeoptimizationId(Isolate* isolate, DeoptimizeKind kind) {
218  DCHECK(IsRuntimeEntry(rmode_));
219  return Deoptimizer::GetDeoptimizationId(isolate, target_address(), kind);
220 }
221 
222 uint32_t RelocInfo::wasm_call_tag() const {
223  DCHECK(rmode_ == WASM_CALL || rmode_ == WASM_STUB_CALL);
224  return Memory<uint32_t>(pc_);
225 }
226 
227 // -----------------------------------------------------------------------------
228 // Implementation of Operand
229 
230 Operand::Operand(Register base, int32_t disp, RelocInfo::Mode rmode) {
231  // [base + disp/r]
232  if (disp == 0 && RelocInfo::IsNone(rmode) && base != ebp) {
233  // [base]
234  set_modrm(0, base);
235  if (base == esp) set_sib(times_1, esp, base);
236  } else if (is_int8(disp) && RelocInfo::IsNone(rmode)) {
237  // [base + disp8]
238  set_modrm(1, base);
239  if (base == esp) set_sib(times_1, esp, base);
240  set_disp8(disp);
241  } else {
242  // [base + disp/r]
243  set_modrm(2, base);
244  if (base == esp) set_sib(times_1, esp, base);
245  set_dispr(disp, rmode);
246  }
247 }
248 
249 
250 Operand::Operand(Register base,
251  Register index,
252  ScaleFactor scale,
253  int32_t disp,
254  RelocInfo::Mode rmode) {
255  DCHECK(index != esp); // illegal addressing mode
256  // [base + index*scale + disp/r]
257  if (disp == 0 && RelocInfo::IsNone(rmode) && base != ebp) {
258  // [base + index*scale]
259  set_modrm(0, esp);
260  set_sib(scale, index, base);
261  } else if (is_int8(disp) && RelocInfo::IsNone(rmode)) {
262  // [base + index*scale + disp8]
263  set_modrm(1, esp);
264  set_sib(scale, index, base);
265  set_disp8(disp);
266  } else {
267  // [base + index*scale + disp/r]
268  set_modrm(2, esp);
269  set_sib(scale, index, base);
270  set_dispr(disp, rmode);
271  }
272 }
273 
274 
275 Operand::Operand(Register index,
276  ScaleFactor scale,
277  int32_t disp,
278  RelocInfo::Mode rmode) {
279  DCHECK(index != esp); // illegal addressing mode
280  // [index*scale + disp/r]
281  set_modrm(0, esp);
282  set_sib(scale, index, ebp);
283  set_dispr(disp, rmode);
284 }
285 
286 
287 bool Operand::is_reg_only() const {
288  return (buf_[0] & 0xF8) == 0xC0; // Addressing mode is register only.
289 }
290 
291 
292 Register Operand::reg() const {
293  DCHECK(is_reg_only());
294  return Register::from_code(buf_[0] & 0x07);
295 }
296 
297 void Assembler::AllocateAndInstallRequestedHeapObjects(Isolate* isolate) {
298  DCHECK_IMPLIES(isolate == nullptr, heap_object_requests_.empty());
299  for (auto& request : heap_object_requests_) {
300  Handle<HeapObject> object;
301  switch (request.kind()) {
302  case HeapObjectRequest::kHeapNumber:
303  object =
304  isolate->factory()->NewHeapNumber(request.heap_number(), TENURED);
305  break;
306  case HeapObjectRequest::kCodeStub:
307  request.code_stub()->set_isolate(isolate);
308  object = request.code_stub()->GetCode();
309  break;
310  case HeapObjectRequest::kStringConstant: {
311  const StringConstantBase* str = request.string();
312  CHECK_NOT_NULL(str);
313  object = str->AllocateStringConstant(isolate);
314  break;
315  }
316  }
317  Address pc = reinterpret_cast<Address>(buffer_) + request.offset();
318  Memory<Handle<Object>>(pc) = object;
319  }
320 }
321 
322 // -----------------------------------------------------------------------------
323 // Implementation of Assembler.
324 
325 // Emit a single byte. Must always be inlined.
326 #define EMIT(x) \
327  *pc_++ = (x)
328 
329 Assembler::Assembler(const AssemblerOptions& options, void* buffer,
330  int buffer_size)
331  : AssemblerBase(options, buffer, buffer_size) {
332 // Clear the buffer in debug mode unless it was provided by the
333 // caller in which case we can't be sure it's okay to overwrite
334 // existing code in it.
335 #ifdef DEBUG
336  if (own_buffer_) ZapCode(reinterpret_cast<Address>(buffer_), buffer_size_);
337 #endif
338 
339  reloc_info_writer.Reposition(buffer_ + buffer_size_, pc_);
340 }
341 
342 void Assembler::GetCode(Isolate* isolate, CodeDesc* desc) {
343  // Finalize code (at this point overflow() may be true, but the gap ensures
344  // that we are still not overlapping instructions and relocation info).
345  DCHECK(pc_ <= reloc_info_writer.pos()); // No overlap.
346 
347  AllocateAndInstallRequestedHeapObjects(isolate);
348 
349  // Set up code descriptor.
350  desc->buffer = buffer_;
351  desc->buffer_size = buffer_size_;
352  desc->instr_size = pc_offset();
353  desc->reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos();
354  desc->origin = this;
355  desc->constant_pool_size = 0;
356  desc->unwinding_info_size = 0;
357  desc->unwinding_info = nullptr;
358 
359  // Collection stage
360  auto jump_opt = jump_optimization_info();
361  if (jump_opt && jump_opt->is_collecting()) {
362  auto& bitmap = jump_opt->farjmp_bitmap();
363  int num = static_cast<int>(farjmp_positions_.size());
364  if (num && bitmap.empty()) {
365  bool can_opt = false;
366 
367  bitmap.resize((num + 31) / 32, 0);
368  for (int i = 0; i < num; i++) {
369  int disp_pos = farjmp_positions_[i];
370  int disp = long_at(disp_pos);
371  if (is_int8(disp)) {
372  bitmap[i / 32] |= 1 << (i & 31);
373  can_opt = true;
374  }
375  }
376  if (can_opt) {
377  jump_opt->set_optimizable();
378  }
379  }
380  }
381 }
382 
383 
384 void Assembler::Align(int m) {
385  DCHECK(base::bits::IsPowerOfTwo(m));
386  int mask = m - 1;
387  int addr = pc_offset();
388  Nop((m - (addr & mask)) & mask);
389 }
390 
391 
392 bool Assembler::IsNop(Address addr) {
393  byte* a = reinterpret_cast<byte*>(addr);
394  while (*a == 0x66) a++;
395  if (*a == 0x90) return true;
396  if (a[0] == 0xF && a[1] == 0x1F) return true;
397  return false;
398 }
399 
400 
401 void Assembler::Nop(int bytes) {
402  EnsureSpace ensure_space(this);
403 
404  // Multi byte nops from http://support.amd.com/us/Processor_TechDocs/40546.pdf
405  while (bytes > 0) {
406  switch (bytes) {
407  case 2:
408  EMIT(0x66);
409  V8_FALLTHROUGH;
410  case 1:
411  EMIT(0x90);
412  return;
413  case 3:
414  EMIT(0xF);
415  EMIT(0x1F);
416  EMIT(0);
417  return;
418  case 4:
419  EMIT(0xF);
420  EMIT(0x1F);
421  EMIT(0x40);
422  EMIT(0);
423  return;
424  case 6:
425  EMIT(0x66);
426  V8_FALLTHROUGH;
427  case 5:
428  EMIT(0xF);
429  EMIT(0x1F);
430  EMIT(0x44);
431  EMIT(0);
432  EMIT(0);
433  return;
434  case 7:
435  EMIT(0xF);
436  EMIT(0x1F);
437  EMIT(0x80);
438  EMIT(0);
439  EMIT(0);
440  EMIT(0);
441  EMIT(0);
442  return;
443  default:
444  case 11:
445  EMIT(0x66);
446  bytes--;
447  V8_FALLTHROUGH;
448  case 10:
449  EMIT(0x66);
450  bytes--;
451  V8_FALLTHROUGH;
452  case 9:
453  EMIT(0x66);
454  bytes--;
455  V8_FALLTHROUGH;
456  case 8:
457  EMIT(0xF);
458  EMIT(0x1F);
459  EMIT(0x84);
460  EMIT(0);
461  EMIT(0);
462  EMIT(0);
463  EMIT(0);
464  EMIT(0);
465  bytes -= 8;
466  }
467  }
468 }
469 
470 
471 void Assembler::CodeTargetAlign() {
472  Align(16); // Preferred alignment of jump targets on ia32.
473 }
474 
475 
476 void Assembler::cpuid() {
477  EnsureSpace ensure_space(this);
478  EMIT(0x0F);
479  EMIT(0xA2);
480 }
481 
482 
483 void Assembler::pushad() {
484  EnsureSpace ensure_space(this);
485  EMIT(0x60);
486 }
487 
488 
489 void Assembler::popad() {
490  EnsureSpace ensure_space(this);
491  EMIT(0x61);
492 }
493 
494 
495 void Assembler::pushfd() {
496  EnsureSpace ensure_space(this);
497  EMIT(0x9C);
498 }
499 
500 
501 void Assembler::popfd() {
502  EnsureSpace ensure_space(this);
503  EMIT(0x9D);
504 }
505 
506 
507 void Assembler::push(const Immediate& x) {
508  EnsureSpace ensure_space(this);
509  if (x.is_int8()) {
510  EMIT(0x6A);
511  EMIT(x.immediate());
512  } else {
513  EMIT(0x68);
514  emit(x);
515  }
516 }
517 
518 
519 void Assembler::push_imm32(int32_t imm32) {
520  EnsureSpace ensure_space(this);
521  EMIT(0x68);
522  emit(imm32);
523 }
524 
525 
526 void Assembler::push(Register src) {
527  EnsureSpace ensure_space(this);
528  EMIT(0x50 | src.code());
529 }
530 
531 void Assembler::push(Operand src) {
532  EnsureSpace ensure_space(this);
533  EMIT(0xFF);
534  emit_operand(esi, src);
535 }
536 
537 
538 void Assembler::pop(Register dst) {
539  DCHECK_NOT_NULL(reloc_info_writer.last_pc());
540  EnsureSpace ensure_space(this);
541  EMIT(0x58 | dst.code());
542 }
543 
544 void Assembler::pop(Operand dst) {
545  EnsureSpace ensure_space(this);
546  EMIT(0x8F);
547  emit_operand(eax, dst);
548 }
549 
550 
551 void Assembler::enter(const Immediate& size) {
552  EnsureSpace ensure_space(this);
553  EMIT(0xC8);
554  emit_w(size);
555  EMIT(0);
556 }
557 
558 
559 void Assembler::leave() {
560  EnsureSpace ensure_space(this);
561  EMIT(0xC9);
562 }
563 
564 void Assembler::mov_b(Register dst, Operand src) {
565  CHECK(dst.is_byte_register());
566  EnsureSpace ensure_space(this);
567  EMIT(0x8A);
568  emit_operand(dst, src);
569 }
570 
571 void Assembler::mov_b(Operand dst, const Immediate& src) {
572  EnsureSpace ensure_space(this);
573  EMIT(0xC6);
574  emit_operand(eax, dst);
575  EMIT(static_cast<int8_t>(src.immediate()));
576 }
577 
578 void Assembler::mov_b(Operand dst, Register src) {
579  CHECK(src.is_byte_register());
580  EnsureSpace ensure_space(this);
581  EMIT(0x88);
582  emit_operand(src, dst);
583 }
584 
585 void Assembler::mov_w(Register dst, Operand src) {
586  EnsureSpace ensure_space(this);
587  EMIT(0x66);
588  EMIT(0x8B);
589  emit_operand(dst, src);
590 }
591 
592 void Assembler::mov_w(Operand dst, Register src) {
593  EnsureSpace ensure_space(this);
594  EMIT(0x66);
595  EMIT(0x89);
596  emit_operand(src, dst);
597 }
598 
599 void Assembler::mov_w(Operand dst, const Immediate& src) {
600  EnsureSpace ensure_space(this);
601  EMIT(0x66);
602  EMIT(0xC7);
603  emit_operand(eax, dst);
604  EMIT(static_cast<int8_t>(src.immediate() & 0xFF));
605  EMIT(static_cast<int8_t>(src.immediate() >> 8));
606 }
607 
608 
609 void Assembler::mov(Register dst, int32_t imm32) {
610  EnsureSpace ensure_space(this);
611  EMIT(0xB8 | dst.code());
612  emit(imm32);
613 }
614 
615 
616 void Assembler::mov(Register dst, const Immediate& x) {
617  EnsureSpace ensure_space(this);
618  EMIT(0xB8 | dst.code());
619  emit(x);
620 }
621 
622 void Assembler::mov(Register dst, Handle<HeapObject> handle) {
623  EnsureSpace ensure_space(this);
624  EMIT(0xB8 | dst.code());
625  emit(handle);
626 }
627 
628 void Assembler::mov(Register dst, Operand src) {
629  EnsureSpace ensure_space(this);
630  EMIT(0x8B);
631  emit_operand(dst, src);
632 }
633 
634 
635 void Assembler::mov(Register dst, Register src) {
636  EnsureSpace ensure_space(this);
637  EMIT(0x89);
638  EMIT(0xC0 | src.code() << 3 | dst.code());
639 }
640 
641 void Assembler::mov(Operand dst, const Immediate& x) {
642  EnsureSpace ensure_space(this);
643  EMIT(0xC7);
644  emit_operand(eax, dst);
645  emit(x);
646 }
647 
648 void Assembler::mov(Operand dst, Address src, RelocInfo::Mode rmode) {
649  EnsureSpace ensure_space(this);
650  EMIT(0xC7);
651  emit_operand(eax, dst);
652  emit(src, rmode);
653 }
654 
655 void Assembler::mov(Operand dst, Handle<HeapObject> handle) {
656  EnsureSpace ensure_space(this);
657  EMIT(0xC7);
658  emit_operand(eax, dst);
659  emit(handle);
660 }
661 
662 void Assembler::mov(Operand dst, Register src) {
663  EnsureSpace ensure_space(this);
664  EMIT(0x89);
665  emit_operand(src, dst);
666 }
667 
668 void Assembler::movsx_b(Register dst, Operand src) {
669  DCHECK_IMPLIES(src.is_reg_only(), src.reg().is_byte_register());
670  EnsureSpace ensure_space(this);
671  EMIT(0x0F);
672  EMIT(0xBE);
673  emit_operand(dst, src);
674 }
675 
676 void Assembler::movsx_w(Register dst, Operand src) {
677  EnsureSpace ensure_space(this);
678  EMIT(0x0F);
679  EMIT(0xBF);
680  emit_operand(dst, src);
681 }
682 
683 void Assembler::movzx_b(Register dst, Operand src) {
684  DCHECK_IMPLIES(src.is_reg_only(), src.reg().is_byte_register());
685  EnsureSpace ensure_space(this);
686  EMIT(0x0F);
687  EMIT(0xB6);
688  emit_operand(dst, src);
689 }
690 
691 void Assembler::movzx_w(Register dst, Operand src) {
692  EnsureSpace ensure_space(this);
693  EMIT(0x0F);
694  EMIT(0xB7);
695  emit_operand(dst, src);
696 }
697 
698 void Assembler::movq(XMMRegister dst, Operand src) {
699  EnsureSpace ensure_space(this);
700  EMIT(0xF3);
701  EMIT(0x0F);
702  EMIT(0x7E);
703  emit_operand(dst, src);
704 }
705 
706 void Assembler::cmov(Condition cc, Register dst, Operand src) {
707  EnsureSpace ensure_space(this);
708  // Opcode: 0f 40 + cc /r.
709  EMIT(0x0F);
710  EMIT(0x40 + cc);
711  emit_operand(dst, src);
712 }
713 
714 
715 void Assembler::cld() {
716  EnsureSpace ensure_space(this);
717  EMIT(0xFC);
718 }
719 
720 
721 void Assembler::rep_movs() {
722  EnsureSpace ensure_space(this);
723  EMIT(0xF3);
724  EMIT(0xA5);
725 }
726 
727 
728 void Assembler::rep_stos() {
729  EnsureSpace ensure_space(this);
730  EMIT(0xF3);
731  EMIT(0xAB);
732 }
733 
734 
735 void Assembler::stos() {
736  EnsureSpace ensure_space(this);
737  EMIT(0xAB);
738 }
739 
740 
741 void Assembler::xchg(Register dst, Register src) {
742  EnsureSpace ensure_space(this);
743  if (src == eax || dst == eax) { // Single-byte encoding.
744  EMIT(0x90 | (src == eax ? dst.code() : src.code()));
745  } else {
746  EMIT(0x87);
747  EMIT(0xC0 | src.code() << 3 | dst.code());
748  }
749 }
750 
751 void Assembler::xchg(Register dst, Operand src) {
752  EnsureSpace ensure_space(this);
753  EMIT(0x87);
754  emit_operand(dst, src);
755 }
756 
757 void Assembler::xchg_b(Register reg, Operand op) {
758  DCHECK(reg.is_byte_register());
759  EnsureSpace ensure_space(this);
760  EMIT(0x86);
761  emit_operand(reg, op);
762 }
763 
764 void Assembler::xchg_w(Register reg, Operand op) {
765  EnsureSpace ensure_space(this);
766  EMIT(0x66);
767  EMIT(0x87);
768  emit_operand(reg, op);
769 }
770 
771 void Assembler::lock() {
772  EnsureSpace ensure_space(this);
773  EMIT(0xF0);
774 }
775 
776 void Assembler::cmpxchg(Operand dst, Register src) {
777  EnsureSpace ensure_space(this);
778  EMIT(0x0F);
779  EMIT(0xB1);
780  emit_operand(src, dst);
781 }
782 
783 void Assembler::cmpxchg_b(Operand dst, Register src) {
784  DCHECK(src.is_byte_register());
785  EnsureSpace ensure_space(this);
786  EMIT(0x0F);
787  EMIT(0xB0);
788  emit_operand(src, dst);
789 }
790 
791 void Assembler::cmpxchg_w(Operand dst, Register src) {
792  EnsureSpace ensure_space(this);
793  EMIT(0x66);
794  EMIT(0x0F);
795  EMIT(0xB1);
796  emit_operand(src, dst);
797 }
798 
799 void Assembler::cmpxchg8b(Operand dst) {
800  EnsureSpace enure_space(this);
801  EMIT(0x0F);
802  EMIT(0xC7);
803  emit_operand(ecx, dst);
804 }
805 
806 void Assembler::lfence() {
807  EnsureSpace ensure_space(this);
808  EMIT(0x0F);
809  EMIT(0xAE);
810  EMIT(0xE8);
811 }
812 
813 void Assembler::pause() {
814  EnsureSpace ensure_space(this);
815  EMIT(0xF3);
816  EMIT(0x90);
817 }
818 
819 void Assembler::adc(Register dst, int32_t imm32) {
820  EnsureSpace ensure_space(this);
821  emit_arith(2, Operand(dst), Immediate(imm32));
822 }
823 
824 void Assembler::adc(Register dst, Operand src) {
825  EnsureSpace ensure_space(this);
826  EMIT(0x13);
827  emit_operand(dst, src);
828 }
829 
830 void Assembler::add(Register dst, Operand src) {
831  EnsureSpace ensure_space(this);
832  EMIT(0x03);
833  emit_operand(dst, src);
834 }
835 
836 void Assembler::add(Operand dst, Register src) {
837  EnsureSpace ensure_space(this);
838  EMIT(0x01);
839  emit_operand(src, dst);
840 }
841 
842 void Assembler::add(Operand dst, const Immediate& x) {
843  DCHECK_NOT_NULL(reloc_info_writer.last_pc());
844  EnsureSpace ensure_space(this);
845  emit_arith(0, dst, x);
846 }
847 
848 
849 void Assembler::and_(Register dst, int32_t imm32) {
850  and_(dst, Immediate(imm32));
851 }
852 
853 
854 void Assembler::and_(Register dst, const Immediate& x) {
855  EnsureSpace ensure_space(this);
856  emit_arith(4, Operand(dst), x);
857 }
858 
859 void Assembler::and_(Register dst, Operand src) {
860  EnsureSpace ensure_space(this);
861  EMIT(0x23);
862  emit_operand(dst, src);
863 }
864 
865 void Assembler::and_(Operand dst, const Immediate& x) {
866  EnsureSpace ensure_space(this);
867  emit_arith(4, dst, x);
868 }
869 
870 void Assembler::and_(Operand dst, Register src) {
871  EnsureSpace ensure_space(this);
872  EMIT(0x21);
873  emit_operand(src, dst);
874 }
875 
876 void Assembler::cmpb(Operand op, Immediate imm8) {
877  DCHECK(imm8.is_int8() || imm8.is_uint8());
878  EnsureSpace ensure_space(this);
879  if (op.is_reg(eax)) {
880  EMIT(0x3C);
881  } else {
882  EMIT(0x80);
883  emit_operand(edi, op); // edi == 7
884  }
885  emit_b(imm8);
886 }
887 
888 void Assembler::cmpb(Operand op, Register reg) {
889  CHECK(reg.is_byte_register());
890  EnsureSpace ensure_space(this);
891  EMIT(0x38);
892  emit_operand(reg, op);
893 }
894 
895 void Assembler::cmpb(Register reg, Operand op) {
896  CHECK(reg.is_byte_register());
897  EnsureSpace ensure_space(this);
898  EMIT(0x3A);
899  emit_operand(reg, op);
900 }
901 
902 void Assembler::cmpw(Operand op, Immediate imm16) {
903  DCHECK(imm16.is_int16() || imm16.is_uint16());
904  EnsureSpace ensure_space(this);
905  EMIT(0x66);
906  EMIT(0x81);
907  emit_operand(edi, op);
908  emit_w(imm16);
909 }
910 
911 void Assembler::cmpw(Register reg, Operand op) {
912  EnsureSpace ensure_space(this);
913  EMIT(0x66);
914  EMIT(0x3B);
915  emit_operand(reg, op);
916 }
917 
918 void Assembler::cmpw(Operand op, Register reg) {
919  EnsureSpace ensure_space(this);
920  EMIT(0x66);
921  EMIT(0x39);
922  emit_operand(reg, op);
923 }
924 
925 void Assembler::cmp(Register reg, int32_t imm32) {
926  EnsureSpace ensure_space(this);
927  emit_arith(7, Operand(reg), Immediate(imm32));
928 }
929 
930 void Assembler::cmp(Register reg, Handle<HeapObject> handle) {
931  EnsureSpace ensure_space(this);
932  emit_arith(7, Operand(reg), Immediate(handle));
933 }
934 
935 void Assembler::cmp(Register reg, Operand op) {
936  EnsureSpace ensure_space(this);
937  EMIT(0x3B);
938  emit_operand(reg, op);
939 }
940 
941 void Assembler::cmp(Operand op, Register reg) {
942  EnsureSpace ensure_space(this);
943  EMIT(0x39);
944  emit_operand(reg, op);
945 }
946 
947 void Assembler::cmp(Operand op, const Immediate& imm) {
948  EnsureSpace ensure_space(this);
949  emit_arith(7, op, imm);
950 }
951 
952 void Assembler::cmp(Operand op, Handle<HeapObject> handle) {
953  EnsureSpace ensure_space(this);
954  emit_arith(7, op, Immediate(handle));
955 }
956 
957 void Assembler::cmpb_al(Operand op) {
958  EnsureSpace ensure_space(this);
959  EMIT(0x38); // CMP r/m8, r8
960  emit_operand(eax, op); // eax has same code as register al.
961 }
962 
963 void Assembler::cmpw_ax(Operand op) {
964  EnsureSpace ensure_space(this);
965  EMIT(0x66);
966  EMIT(0x39); // CMP r/m16, r16
967  emit_operand(eax, op); // eax has same code as register ax.
968 }
969 
970 
971 void Assembler::dec_b(Register dst) {
972  CHECK(dst.is_byte_register());
973  EnsureSpace ensure_space(this);
974  EMIT(0xFE);
975  EMIT(0xC8 | dst.code());
976 }
977 
978 void Assembler::dec_b(Operand dst) {
979  EnsureSpace ensure_space(this);
980  EMIT(0xFE);
981  emit_operand(ecx, dst);
982 }
983 
984 
985 void Assembler::dec(Register dst) {
986  EnsureSpace ensure_space(this);
987  EMIT(0x48 | dst.code());
988 }
989 
990 void Assembler::dec(Operand dst) {
991  EnsureSpace ensure_space(this);
992  EMIT(0xFF);
993  emit_operand(ecx, dst);
994 }
995 
996 
997 void Assembler::cdq() {
998  EnsureSpace ensure_space(this);
999  EMIT(0x99);
1000 }
1001 
1002 void Assembler::idiv(Operand src) {
1003  EnsureSpace ensure_space(this);
1004  EMIT(0xF7);
1005  emit_operand(edi, src);
1006 }
1007 
1008 void Assembler::div(Operand src) {
1009  EnsureSpace ensure_space(this);
1010  EMIT(0xF7);
1011  emit_operand(esi, src);
1012 }
1013 
1014 
1015 void Assembler::imul(Register reg) {
1016  EnsureSpace ensure_space(this);
1017  EMIT(0xF7);
1018  EMIT(0xE8 | reg.code());
1019 }
1020 
1021 void Assembler::imul(Register dst, Operand src) {
1022  EnsureSpace ensure_space(this);
1023  EMIT(0x0F);
1024  EMIT(0xAF);
1025  emit_operand(dst, src);
1026 }
1027 
1028 
1029 void Assembler::imul(Register dst, Register src, int32_t imm32) {
1030  imul(dst, Operand(src), imm32);
1031 }
1032 
1033 void Assembler::imul(Register dst, Operand src, int32_t imm32) {
1034  EnsureSpace ensure_space(this);
1035  if (is_int8(imm32)) {
1036  EMIT(0x6B);
1037  emit_operand(dst, src);
1038  EMIT(imm32);
1039  } else {
1040  EMIT(0x69);
1041  emit_operand(dst, src);
1042  emit(imm32);
1043  }
1044 }
1045 
1046 
1047 void Assembler::inc(Register dst) {
1048  EnsureSpace ensure_space(this);
1049  EMIT(0x40 | dst.code());
1050 }
1051 
1052 void Assembler::inc(Operand dst) {
1053  EnsureSpace ensure_space(this);
1054  EMIT(0xFF);
1055  emit_operand(eax, dst);
1056 }
1057 
1058 void Assembler::lea(Register dst, Operand src) {
1059  EnsureSpace ensure_space(this);
1060  EMIT(0x8D);
1061  emit_operand(dst, src);
1062 }
1063 
1064 
1065 void Assembler::mul(Register src) {
1066  EnsureSpace ensure_space(this);
1067  EMIT(0xF7);
1068  EMIT(0xE0 | src.code());
1069 }
1070 
1071 
1072 void Assembler::neg(Register dst) {
1073  EnsureSpace ensure_space(this);
1074  EMIT(0xF7);
1075  EMIT(0xD8 | dst.code());
1076 }
1077 
1078 void Assembler::neg(Operand dst) {
1079  EnsureSpace ensure_space(this);
1080  EMIT(0xF7);
1081  emit_operand(ebx, dst);
1082 }
1083 
1084 
1085 void Assembler::not_(Register dst) {
1086  EnsureSpace ensure_space(this);
1087  EMIT(0xF7);
1088  EMIT(0xD0 | dst.code());
1089 }
1090 
1091 void Assembler::not_(Operand dst) {
1092  EnsureSpace ensure_space(this);
1093  EMIT(0xF7);
1094  emit_operand(edx, dst);
1095 }
1096 
1097 
1098 void Assembler::or_(Register dst, int32_t imm32) {
1099  EnsureSpace ensure_space(this);
1100  emit_arith(1, Operand(dst), Immediate(imm32));
1101 }
1102 
1103 void Assembler::or_(Register dst, Operand src) {
1104  EnsureSpace ensure_space(this);
1105  EMIT(0x0B);
1106  emit_operand(dst, src);
1107 }
1108 
1109 void Assembler::or_(Operand dst, const Immediate& x) {
1110  EnsureSpace ensure_space(this);
1111  emit_arith(1, dst, x);
1112 }
1113 
1114 void Assembler::or_(Operand dst, Register src) {
1115  EnsureSpace ensure_space(this);
1116  EMIT(0x09);
1117  emit_operand(src, dst);
1118 }
1119 
1120 
1121 void Assembler::rcl(Register dst, uint8_t imm8) {
1122  EnsureSpace ensure_space(this);
1123  DCHECK(is_uint5(imm8)); // illegal shift count
1124  if (imm8 == 1) {
1125  EMIT(0xD1);
1126  EMIT(0xD0 | dst.code());
1127  } else {
1128  EMIT(0xC1);
1129  EMIT(0xD0 | dst.code());
1130  EMIT(imm8);
1131  }
1132 }
1133 
1134 
1135 void Assembler::rcr(Register dst, uint8_t imm8) {
1136  EnsureSpace ensure_space(this);
1137  DCHECK(is_uint5(imm8)); // illegal shift count
1138  if (imm8 == 1) {
1139  EMIT(0xD1);
1140  EMIT(0xD8 | dst.code());
1141  } else {
1142  EMIT(0xC1);
1143  EMIT(0xD8 | dst.code());
1144  EMIT(imm8);
1145  }
1146 }
1147 
1148 void Assembler::ror(Operand dst, uint8_t imm8) {
1149  EnsureSpace ensure_space(this);
1150  DCHECK(is_uint5(imm8)); // illegal shift count
1151  if (imm8 == 1) {
1152  EMIT(0xD1);
1153  emit_operand(ecx, dst);
1154  } else {
1155  EMIT(0xC1);
1156  emit_operand(ecx, dst);
1157  EMIT(imm8);
1158  }
1159 }
1160 
1161 void Assembler::ror_cl(Operand dst) {
1162  EnsureSpace ensure_space(this);
1163  EMIT(0xD3);
1164  emit_operand(ecx, dst);
1165 }
1166 
1167 void Assembler::sar(Operand dst, uint8_t imm8) {
1168  EnsureSpace ensure_space(this);
1169  DCHECK(is_uint5(imm8)); // illegal shift count
1170  if (imm8 == 1) {
1171  EMIT(0xD1);
1172  emit_operand(edi, dst);
1173  } else {
1174  EMIT(0xC1);
1175  emit_operand(edi, dst);
1176  EMIT(imm8);
1177  }
1178 }
1179 
1180 void Assembler::sar_cl(Operand dst) {
1181  EnsureSpace ensure_space(this);
1182  EMIT(0xD3);
1183  emit_operand(edi, dst);
1184 }
1185 
1186 void Assembler::sbb(Register dst, Operand src) {
1187  EnsureSpace ensure_space(this);
1188  EMIT(0x1B);
1189  emit_operand(dst, src);
1190 }
1191 
1192 void Assembler::shld(Register dst, Register src, uint8_t shift) {
1193  DCHECK(is_uint5(shift));
1194  EnsureSpace ensure_space(this);
1195  EMIT(0x0F);
1196  EMIT(0xA4);
1197  emit_operand(src, Operand(dst));
1198  EMIT(shift);
1199 }
1200 
1201 void Assembler::shld_cl(Register dst, Register src) {
1202  EnsureSpace ensure_space(this);
1203  EMIT(0x0F);
1204  EMIT(0xA5);
1205  emit_operand(src, Operand(dst));
1206 }
1207 
1208 void Assembler::shl(Operand dst, uint8_t imm8) {
1209  EnsureSpace ensure_space(this);
1210  DCHECK(is_uint5(imm8)); // illegal shift count
1211  if (imm8 == 1) {
1212  EMIT(0xD1);
1213  emit_operand(esp, dst);
1214  } else {
1215  EMIT(0xC1);
1216  emit_operand(esp, dst);
1217  EMIT(imm8);
1218  }
1219 }
1220 
1221 void Assembler::shl_cl(Operand dst) {
1222  EnsureSpace ensure_space(this);
1223  EMIT(0xD3);
1224  emit_operand(esp, dst);
1225 }
1226 
1227 void Assembler::shr(Operand dst, uint8_t imm8) {
1228  EnsureSpace ensure_space(this);
1229  DCHECK(is_uint5(imm8)); // illegal shift count
1230  if (imm8 == 1) {
1231  EMIT(0xD1);
1232  emit_operand(ebp, dst);
1233  } else {
1234  EMIT(0xC1);
1235  emit_operand(ebp, dst);
1236  EMIT(imm8);
1237  }
1238 }
1239 
1240 void Assembler::shr_cl(Operand dst) {
1241  EnsureSpace ensure_space(this);
1242  EMIT(0xD3);
1243  emit_operand(ebp, dst);
1244 }
1245 
1246 void Assembler::shrd(Register dst, Register src, uint8_t shift) {
1247  DCHECK(is_uint5(shift));
1248  EnsureSpace ensure_space(this);
1249  EMIT(0x0F);
1250  EMIT(0xAC);
1251  emit_operand(dst, Operand(src));
1252  EMIT(shift);
1253 }
1254 
1255 void Assembler::shrd_cl(Operand dst, Register src) {
1256  EnsureSpace ensure_space(this);
1257  EMIT(0x0F);
1258  EMIT(0xAD);
1259  emit_operand(src, dst);
1260 }
1261 
1262 void Assembler::sub(Operand dst, const Immediate& x) {
1263  EnsureSpace ensure_space(this);
1264  emit_arith(5, dst, x);
1265 }
1266 
1267 void Assembler::sub(Register dst, Operand src) {
1268  EnsureSpace ensure_space(this);
1269  EMIT(0x2B);
1270  emit_operand(dst, src);
1271 }
1272 
1273 void Assembler::sub(Operand dst, Register src) {
1274  EnsureSpace ensure_space(this);
1275  EMIT(0x29);
1276  emit_operand(src, dst);
1277 }
1278 
1279 void Assembler::sub_sp_32(uint32_t imm) {
1280  EnsureSpace ensure_space(this);
1281  EMIT(0x81); // using a literal 32-bit immediate.
1282  static constexpr Register ireg = Register::from_code<5>();
1283  emit_operand(ireg, Operand(esp));
1284  emit(imm);
1285 }
1286 
1287 void Assembler::test(Register reg, const Immediate& imm) {
1288  if (imm.is_uint8()) {
1289  test_b(reg, imm);
1290  return;
1291  }
1292 
1293  EnsureSpace ensure_space(this);
1294  // This is not using emit_arith because test doesn't support
1295  // sign-extension of 8-bit operands.
1296  if (reg == eax) {
1297  EMIT(0xA9);
1298  } else {
1299  EMIT(0xF7);
1300  EMIT(0xC0 | reg.code());
1301  }
1302  emit(imm);
1303 }
1304 
1305 void Assembler::test(Register reg, Operand op) {
1306  EnsureSpace ensure_space(this);
1307  EMIT(0x85);
1308  emit_operand(reg, op);
1309 }
1310 
1311 void Assembler::test_b(Register reg, Operand op) {
1312  CHECK(reg.is_byte_register());
1313  EnsureSpace ensure_space(this);
1314  EMIT(0x84);
1315  emit_operand(reg, op);
1316 }
1317 
1318 void Assembler::test(Operand op, const Immediate& imm) {
1319  if (op.is_reg_only()) {
1320  test(op.reg(), imm);
1321  return;
1322  }
1323  if (imm.is_uint8()) {
1324  return test_b(op, imm);
1325  }
1326  EnsureSpace ensure_space(this);
1327  EMIT(0xF7);
1328  emit_operand(eax, op);
1329  emit(imm);
1330 }
1331 
1332 void Assembler::test_b(Register reg, Immediate imm8) {
1333  DCHECK(imm8.is_uint8());
1334  EnsureSpace ensure_space(this);
1335  // Only use test against byte for registers that have a byte
1336  // variant: eax, ebx, ecx, and edx.
1337  if (reg == eax) {
1338  EMIT(0xA8);
1339  emit_b(imm8);
1340  } else if (reg.is_byte_register()) {
1341  emit_arith_b(0xF6, 0xC0, reg, static_cast<uint8_t>(imm8.immediate()));
1342  } else {
1343  EMIT(0x66);
1344  EMIT(0xF7);
1345  EMIT(0xC0 | reg.code());
1346  emit_w(imm8);
1347  }
1348 }
1349 
1350 void Assembler::test_b(Operand op, Immediate imm8) {
1351  if (op.is_reg_only()) {
1352  test_b(op.reg(), imm8);
1353  return;
1354  }
1355  EnsureSpace ensure_space(this);
1356  EMIT(0xF6);
1357  emit_operand(eax, op);
1358  emit_b(imm8);
1359 }
1360 
1361 void Assembler::test_w(Register reg, Immediate imm16) {
1362  DCHECK(imm16.is_int16() || imm16.is_uint16());
1363  EnsureSpace ensure_space(this);
1364  if (reg == eax) {
1365  EMIT(0xA9);
1366  emit_w(imm16);
1367  } else {
1368  EMIT(0x66);
1369  EMIT(0xF7);
1370  EMIT(0xC0 | reg.code());
1371  emit_w(imm16);
1372  }
1373 }
1374 
1375 void Assembler::test_w(Register reg, Operand op) {
1376  EnsureSpace ensure_space(this);
1377  EMIT(0x66);
1378  EMIT(0x85);
1379  emit_operand(reg, op);
1380 }
1381 
1382 void Assembler::test_w(Operand op, Immediate imm16) {
1383  DCHECK(imm16.is_int16() || imm16.is_uint16());
1384  if (op.is_reg_only()) {
1385  test_w(op.reg(), imm16);
1386  return;
1387  }
1388  EnsureSpace ensure_space(this);
1389  EMIT(0x66);
1390  EMIT(0xF7);
1391  emit_operand(eax, op);
1392  emit_w(imm16);
1393 }
1394 
1395 void Assembler::xor_(Register dst, int32_t imm32) {
1396  EnsureSpace ensure_space(this);
1397  emit_arith(6, Operand(dst), Immediate(imm32));
1398 }
1399 
1400 void Assembler::xor_(Register dst, Operand src) {
1401  EnsureSpace ensure_space(this);
1402  EMIT(0x33);
1403  emit_operand(dst, src);
1404 }
1405 
1406 void Assembler::xor_(Operand dst, Register src) {
1407  EnsureSpace ensure_space(this);
1408  EMIT(0x31);
1409  emit_operand(src, dst);
1410 }
1411 
1412 void Assembler::xor_(Operand dst, const Immediate& x) {
1413  EnsureSpace ensure_space(this);
1414  emit_arith(6, dst, x);
1415 }
1416 
1417 void Assembler::bswap(Register dst) {
1418  EnsureSpace ensure_space(this);
1419  EMIT(0x0F);
1420  EMIT(0xC8 + dst.code());
1421 }
1422 
1423 void Assembler::bt(Operand dst, Register src) {
1424  EnsureSpace ensure_space(this);
1425  EMIT(0x0F);
1426  EMIT(0xA3);
1427  emit_operand(src, dst);
1428 }
1429 
1430 void Assembler::bts(Operand dst, Register src) {
1431  EnsureSpace ensure_space(this);
1432  EMIT(0x0F);
1433  EMIT(0xAB);
1434  emit_operand(src, dst);
1435 }
1436 
1437 void Assembler::bsr(Register dst, Operand src) {
1438  EnsureSpace ensure_space(this);
1439  EMIT(0x0F);
1440  EMIT(0xBD);
1441  emit_operand(dst, src);
1442 }
1443 
1444 void Assembler::bsf(Register dst, Operand src) {
1445  EnsureSpace ensure_space(this);
1446  EMIT(0x0F);
1447  EMIT(0xBC);
1448  emit_operand(dst, src);
1449 }
1450 
1451 
1452 void Assembler::hlt() {
1453  EnsureSpace ensure_space(this);
1454  EMIT(0xF4);
1455 }
1456 
1457 
1458 void Assembler::int3() {
1459  EnsureSpace ensure_space(this);
1460  EMIT(0xCC);
1461 }
1462 
1463 
1464 void Assembler::nop() {
1465  EnsureSpace ensure_space(this);
1466  EMIT(0x90);
1467 }
1468 
1469 
1470 void Assembler::ret(int imm16) {
1471  EnsureSpace ensure_space(this);
1472  DCHECK(is_uint16(imm16));
1473  if (imm16 == 0) {
1474  EMIT(0xC3);
1475  } else {
1476  EMIT(0xC2);
1477  EMIT(imm16 & 0xFF);
1478  EMIT((imm16 >> 8) & 0xFF);
1479  }
1480 }
1481 
1482 
1483 void Assembler::ud2() {
1484  EnsureSpace ensure_space(this);
1485  EMIT(0x0F);
1486  EMIT(0x0B);
1487 }
1488 
1489 
1490 // Labels refer to positions in the (to be) generated code.
1491 // There are bound, linked, and unused labels.
1492 //
1493 // Bound labels refer to known positions in the already
1494 // generated code. pos() is the position the label refers to.
1495 //
1496 // Linked labels refer to unknown positions in the code
1497 // to be generated; pos() is the position of the 32bit
1498 // Displacement of the last instruction using the label.
1499 
1500 void Assembler::print(const Label* L) {
1501  if (L->is_unused()) {
1502  PrintF("unused label\n");
1503  } else if (L->is_bound()) {
1504  PrintF("bound label to %d\n", L->pos());
1505  } else if (L->is_linked()) {
1506  Label l;
1507  l.link_to(L->pos());
1508  PrintF("unbound label");
1509  while (l.is_linked()) {
1510  Displacement disp = disp_at(&l);
1511  PrintF("@ %d ", l.pos());
1512  disp.print();
1513  PrintF("\n");
1514  disp.next(&l);
1515  }
1516  } else {
1517  PrintF("label in inconsistent state (pos = %d)\n", L->pos_);
1518  }
1519 }
1520 
1521 
1522 void Assembler::bind_to(Label* L, int pos) {
1523  EnsureSpace ensure_space(this);
1524  DCHECK(0 <= pos && pos <= pc_offset()); // must have a valid binding position
1525  while (L->is_linked()) {
1526  Displacement disp = disp_at(L);
1527  int fixup_pos = L->pos();
1528  if (disp.type() == Displacement::CODE_ABSOLUTE) {
1529  long_at_put(fixup_pos, reinterpret_cast<int>(buffer_ + pos));
1530  internal_reference_positions_.push_back(fixup_pos);
1531  } else if (disp.type() == Displacement::CODE_RELATIVE) {
1532  // Relative to Code heap object pointer.
1533  long_at_put(fixup_pos, pos + Code::kHeaderSize - kHeapObjectTag);
1534  } else {
1535  if (disp.type() == Displacement::UNCONDITIONAL_JUMP) {
1536  DCHECK_EQ(byte_at(fixup_pos - 1), 0xE9); // jmp expected
1537  }
1538  // Relative address, relative to point after address.
1539  int imm32 = pos - (fixup_pos + sizeof(int32_t));
1540  long_at_put(fixup_pos, imm32);
1541  }
1542  disp.next(L);
1543  }
1544  while (L->is_near_linked()) {
1545  int fixup_pos = L->near_link_pos();
1546  int offset_to_next =
1547  static_cast<int>(*reinterpret_cast<int8_t*>(addr_at(fixup_pos)));
1548  DCHECK_LE(offset_to_next, 0);
1549  // Relative address, relative to point after address.
1550  int disp = pos - fixup_pos - sizeof(int8_t);
1551  CHECK(0 <= disp && disp <= 127);
1552  set_byte_at(fixup_pos, disp);
1553  if (offset_to_next < 0) {
1554  L->link_to(fixup_pos + offset_to_next, Label::kNear);
1555  } else {
1556  L->UnuseNear();
1557  }
1558  }
1559 
1560  // Optimization stage
1561  auto jump_opt = jump_optimization_info();
1562  if (jump_opt && jump_opt->is_optimizing()) {
1563  auto it = label_farjmp_maps_.find(L);
1564  if (it != label_farjmp_maps_.end()) {
1565  auto& pos_vector = it->second;
1566  for (auto fixup_pos : pos_vector) {
1567  int disp = pos - (fixup_pos + sizeof(int8_t));
1568  CHECK(is_int8(disp));
1569  set_byte_at(fixup_pos, disp);
1570  }
1571  label_farjmp_maps_.erase(it);
1572  }
1573  }
1574  L->bind_to(pos);
1575 }
1576 
1577 
1578 void Assembler::bind(Label* L) {
1579  EnsureSpace ensure_space(this);
1580  DCHECK(!L->is_bound()); // label can only be bound once
1581  bind_to(L, pc_offset());
1582 }
1583 
1584 void Assembler::record_farjmp_position(Label* L, int pos) {
1585  auto& pos_vector = label_farjmp_maps_[L];
1586  pos_vector.push_back(pos);
1587 }
1588 
1589 bool Assembler::is_optimizable_farjmp(int idx) {
1590  if (predictable_code_size()) return false;
1591 
1592  auto jump_opt = jump_optimization_info();
1593  CHECK(jump_opt->is_optimizing());
1594 
1595  auto& bitmap = jump_opt->farjmp_bitmap();
1596  CHECK(idx < static_cast<int>(bitmap.size() * 32));
1597  return !!(bitmap[idx / 32] & (1 << (idx & 31)));
1598 }
1599 
1600 void Assembler::call(Label* L) {
1601  EnsureSpace ensure_space(this);
1602  if (L->is_bound()) {
1603  const int long_size = 5;
1604  int offs = L->pos() - pc_offset();
1605  DCHECK_LE(offs, 0);
1606  // 1110 1000 #32-bit disp.
1607  EMIT(0xE8);
1608  emit(offs - long_size);
1609  } else {
1610  // 1110 1000 #32-bit disp.
1611  EMIT(0xE8);
1612  emit_disp(L, Displacement::OTHER);
1613  }
1614 }
1615 
1616 void Assembler::call(Address entry, RelocInfo::Mode rmode) {
1617  EnsureSpace ensure_space(this);
1618  DCHECK(!RelocInfo::IsCodeTarget(rmode));
1619  EMIT(0xE8);
1620  if (RelocInfo::IsRuntimeEntry(rmode)) {
1621  emit(entry, rmode);
1622  } else {
1623  emit(entry - (reinterpret_cast<Address>(pc_) + sizeof(int32_t)), rmode);
1624  }
1625 }
1626 
1627 void Assembler::wasm_call(Address entry, RelocInfo::Mode rmode) {
1628  EnsureSpace ensure_space(this);
1629  EMIT(0xE8);
1630  emit(entry, rmode);
1631 }
1632 
1633 void Assembler::call(Operand adr) {
1634  EnsureSpace ensure_space(this);
1635  EMIT(0xFF);
1636  emit_operand(edx, adr);
1637 }
1638 
1639 void Assembler::call(Handle<Code> code, RelocInfo::Mode rmode) {
1640  EnsureSpace ensure_space(this);
1641  DCHECK(RelocInfo::IsCodeTarget(rmode));
1642  EMIT(0xE8);
1643  emit(code, rmode);
1644 }
1645 
1646 void Assembler::call(CodeStub* stub) {
1647  EnsureSpace ensure_space(this);
1648  EMIT(0xE8);
1649  emit(Immediate::EmbeddedCode(stub));
1650 }
1651 
1652 void Assembler::jmp_rel(int offset) {
1653  EnsureSpace ensure_space(this);
1654  const int short_size = 2;
1655  const int long_size = 5;
1656  if (is_int8(offset - short_size)) {
1657  // 1110 1011 #8-bit disp.
1658  EMIT(0xEB);
1659  EMIT((offset - short_size) & 0xFF);
1660  } else {
1661  // 1110 1001 #32-bit disp.
1662  EMIT(0xE9);
1663  emit(offset - long_size);
1664  }
1665 }
1666 
1667 void Assembler::jmp(Label* L, Label::Distance distance) {
1668  if (L->is_bound()) {
1669  int offset = L->pos() - pc_offset();
1670  DCHECK_LE(offset, 0); // backward jump.
1671  jmp_rel(offset);
1672  return;
1673  }
1674 
1675  EnsureSpace ensure_space(this);
1676  if (distance == Label::kNear) {
1677  EMIT(0xEB);
1678  emit_near_disp(L);
1679  } else {
1680  auto jump_opt = jump_optimization_info();
1681  if (V8_UNLIKELY(jump_opt)) {
1682  if (jump_opt->is_optimizing() && is_optimizable_farjmp(farjmp_num_++)) {
1683  EMIT(0xEB);
1684  record_farjmp_position(L, pc_offset());
1685  EMIT(0);
1686  return;
1687  }
1688  if (jump_opt->is_collecting()) {
1689  farjmp_positions_.push_back(pc_offset() + 1);
1690  }
1691  }
1692  // 1110 1001 #32-bit disp.
1693  EMIT(0xE9);
1694  emit_disp(L, Displacement::UNCONDITIONAL_JUMP);
1695  }
1696 }
1697 
1698 void Assembler::jmp(Address entry, RelocInfo::Mode rmode) {
1699  EnsureSpace ensure_space(this);
1700  DCHECK(!RelocInfo::IsCodeTarget(rmode));
1701  EMIT(0xE9);
1702  if (RelocInfo::IsRuntimeEntry(rmode)) {
1703  emit(entry, rmode);
1704  } else {
1705  emit(entry - (reinterpret_cast<Address>(pc_) + sizeof(int32_t)), rmode);
1706  }
1707 }
1708 
1709 void Assembler::jmp(Operand adr) {
1710  EnsureSpace ensure_space(this);
1711  EMIT(0xFF);
1712  emit_operand(esp, adr);
1713 }
1714 
1715 
1716 void Assembler::jmp(Handle<Code> code, RelocInfo::Mode rmode) {
1717  EnsureSpace ensure_space(this);
1718  DCHECK(RelocInfo::IsCodeTarget(rmode));
1719  EMIT(0xE9);
1720  emit(code, rmode);
1721 }
1722 
1723 
1724 void Assembler::j(Condition cc, Label* L, Label::Distance distance) {
1725  EnsureSpace ensure_space(this);
1726  DCHECK(0 <= cc && static_cast<int>(cc) < 16);
1727  if (L->is_bound()) {
1728  const int short_size = 2;
1729  const int long_size = 6;
1730  int offs = L->pos() - pc_offset();
1731  DCHECK_LE(offs, 0);
1732  if (is_int8(offs - short_size)) {
1733  // 0111 tttn #8-bit disp
1734  EMIT(0x70 | cc);
1735  EMIT((offs - short_size) & 0xFF);
1736  } else {
1737  // 0000 1111 1000 tttn #32-bit disp
1738  EMIT(0x0F);
1739  EMIT(0x80 | cc);
1740  emit(offs - long_size);
1741  }
1742  } else if (distance == Label::kNear) {
1743  EMIT(0x70 | cc);
1744  emit_near_disp(L);
1745  } else {
1746  auto jump_opt = jump_optimization_info();
1747  if (V8_UNLIKELY(jump_opt)) {
1748  if (jump_opt->is_optimizing() && is_optimizable_farjmp(farjmp_num_++)) {
1749  // 0111 tttn #8-bit disp
1750  EMIT(0x70 | cc);
1751  record_farjmp_position(L, pc_offset());
1752  EMIT(0);
1753  return;
1754  }
1755  if (jump_opt->is_collecting()) {
1756  farjmp_positions_.push_back(pc_offset() + 2);
1757  }
1758  }
1759  // 0000 1111 1000 tttn #32-bit disp
1760  // Note: could eliminate cond. jumps to this jump if condition
1761  // is the same however, seems to be rather unlikely case.
1762  EMIT(0x0F);
1763  EMIT(0x80 | cc);
1764  emit_disp(L, Displacement::OTHER);
1765  }
1766 }
1767 
1768 
1769 void Assembler::j(Condition cc, byte* entry, RelocInfo::Mode rmode) {
1770  EnsureSpace ensure_space(this);
1771  DCHECK((0 <= cc) && (static_cast<int>(cc) < 16));
1772  // 0000 1111 1000 tttn #32-bit disp.
1773  EMIT(0x0F);
1774  EMIT(0x80 | cc);
1775  if (RelocInfo::IsRuntimeEntry(rmode)) {
1776  emit(reinterpret_cast<uint32_t>(entry), rmode);
1777  } else {
1778  emit(entry - (pc_ + sizeof(int32_t)), rmode);
1779  }
1780 }
1781 
1782 
1783 void Assembler::j(Condition cc, Handle<Code> code, RelocInfo::Mode rmode) {
1784  EnsureSpace ensure_space(this);
1785  // 0000 1111 1000 tttn #32-bit disp
1786  EMIT(0x0F);
1787  EMIT(0x80 | cc);
1788  emit(code, rmode);
1789 }
1790 
1791 
1792 // FPU instructions.
1793 
1794 void Assembler::fld(int i) {
1795  EnsureSpace ensure_space(this);
1796  emit_farith(0xD9, 0xC0, i);
1797 }
1798 
1799 
1800 void Assembler::fstp(int i) {
1801  EnsureSpace ensure_space(this);
1802  emit_farith(0xDD, 0xD8, i);
1803 }
1804 
1805 
1806 void Assembler::fld1() {
1807  EnsureSpace ensure_space(this);
1808  EMIT(0xD9);
1809  EMIT(0xE8);
1810 }
1811 
1812 
1813 void Assembler::fldpi() {
1814  EnsureSpace ensure_space(this);
1815  EMIT(0xD9);
1816  EMIT(0xEB);
1817 }
1818 
1819 
1820 void Assembler::fldz() {
1821  EnsureSpace ensure_space(this);
1822  EMIT(0xD9);
1823  EMIT(0xEE);
1824 }
1825 
1826 
1827 void Assembler::fldln2() {
1828  EnsureSpace ensure_space(this);
1829  EMIT(0xD9);
1830  EMIT(0xED);
1831 }
1832 
1833 void Assembler::fld_s(Operand adr) {
1834  EnsureSpace ensure_space(this);
1835  EMIT(0xD9);
1836  emit_operand(eax, adr);
1837 }
1838 
1839 void Assembler::fld_d(Operand adr) {
1840  EnsureSpace ensure_space(this);
1841  EMIT(0xDD);
1842  emit_operand(eax, adr);
1843 }
1844 
1845 void Assembler::fstp_s(Operand adr) {
1846  EnsureSpace ensure_space(this);
1847  EMIT(0xD9);
1848  emit_operand(ebx, adr);
1849 }
1850 
1851 void Assembler::fst_s(Operand adr) {
1852  EnsureSpace ensure_space(this);
1853  EMIT(0xD9);
1854  emit_operand(edx, adr);
1855 }
1856 
1857 void Assembler::fstp_d(Operand adr) {
1858  EnsureSpace ensure_space(this);
1859  EMIT(0xDD);
1860  emit_operand(ebx, adr);
1861 }
1862 
1863 void Assembler::fst_d(Operand adr) {
1864  EnsureSpace ensure_space(this);
1865  EMIT(0xDD);
1866  emit_operand(edx, adr);
1867 }
1868 
1869 void Assembler::fild_s(Operand adr) {
1870  EnsureSpace ensure_space(this);
1871  EMIT(0xDB);
1872  emit_operand(eax, adr);
1873 }
1874 
1875 void Assembler::fild_d(Operand adr) {
1876  EnsureSpace ensure_space(this);
1877  EMIT(0xDF);
1878  emit_operand(ebp, adr);
1879 }
1880 
1881 void Assembler::fistp_s(Operand adr) {
1882  EnsureSpace ensure_space(this);
1883  EMIT(0xDB);
1884  emit_operand(ebx, adr);
1885 }
1886 
1887 void Assembler::fisttp_s(Operand adr) {
1888  DCHECK(IsEnabled(SSE3));
1889  EnsureSpace ensure_space(this);
1890  EMIT(0xDB);
1891  emit_operand(ecx, adr);
1892 }
1893 
1894 void Assembler::fisttp_d(Operand adr) {
1895  DCHECK(IsEnabled(SSE3));
1896  EnsureSpace ensure_space(this);
1897  EMIT(0xDD);
1898  emit_operand(ecx, adr);
1899 }
1900 
1901 void Assembler::fist_s(Operand adr) {
1902  EnsureSpace ensure_space(this);
1903  EMIT(0xDB);
1904  emit_operand(edx, adr);
1905 }
1906 
1907 void Assembler::fistp_d(Operand adr) {
1908  EnsureSpace ensure_space(this);
1909  EMIT(0xDF);
1910  emit_operand(edi, adr);
1911 }
1912 
1913 
1914 void Assembler::fabs() {
1915  EnsureSpace ensure_space(this);
1916  EMIT(0xD9);
1917  EMIT(0xE1);
1918 }
1919 
1920 
1921 void Assembler::fchs() {
1922  EnsureSpace ensure_space(this);
1923  EMIT(0xD9);
1924  EMIT(0xE0);
1925 }
1926 
1927 
1928 void Assembler::fcos() {
1929  EnsureSpace ensure_space(this);
1930  EMIT(0xD9);
1931  EMIT(0xFF);
1932 }
1933 
1934 
1935 void Assembler::fsin() {
1936  EnsureSpace ensure_space(this);
1937  EMIT(0xD9);
1938  EMIT(0xFE);
1939 }
1940 
1941 
1942 void Assembler::fptan() {
1943  EnsureSpace ensure_space(this);
1944  EMIT(0xD9);
1945  EMIT(0xF2);
1946 }
1947 
1948 
1949 void Assembler::fyl2x() {
1950  EnsureSpace ensure_space(this);
1951  EMIT(0xD9);
1952  EMIT(0xF1);
1953 }
1954 
1955 
1956 void Assembler::f2xm1() {
1957  EnsureSpace ensure_space(this);
1958  EMIT(0xD9);
1959  EMIT(0xF0);
1960 }
1961 
1962 
1963 void Assembler::fscale() {
1964  EnsureSpace ensure_space(this);
1965  EMIT(0xD9);
1966  EMIT(0xFD);
1967 }
1968 
1969 
1970 void Assembler::fninit() {
1971  EnsureSpace ensure_space(this);
1972  EMIT(0xDB);
1973  EMIT(0xE3);
1974 }
1975 
1976 
1977 void Assembler::fadd(int i) {
1978  EnsureSpace ensure_space(this);
1979  emit_farith(0xDC, 0xC0, i);
1980 }
1981 
1982 
1983 void Assembler::fadd_i(int i) {
1984  EnsureSpace ensure_space(this);
1985  emit_farith(0xD8, 0xC0, i);
1986 }
1987 
1988 
1989 void Assembler::fsub(int i) {
1990  EnsureSpace ensure_space(this);
1991  emit_farith(0xDC, 0xE8, i);
1992 }
1993 
1994 
1995 void Assembler::fsub_i(int i) {
1996  EnsureSpace ensure_space(this);
1997  emit_farith(0xD8, 0xE0, i);
1998 }
1999 
2000 void Assembler::fisub_s(Operand adr) {
2001  EnsureSpace ensure_space(this);
2002  EMIT(0xDA);
2003  emit_operand(esp, adr);
2004 }
2005 
2006 
2007 void Assembler::fmul_i(int i) {
2008  EnsureSpace ensure_space(this);
2009  emit_farith(0xD8, 0xC8, i);
2010 }
2011 
2012 
2013 void Assembler::fmul(int i) {
2014  EnsureSpace ensure_space(this);
2015  emit_farith(0xDC, 0xC8, i);
2016 }
2017 
2018 
2019 void Assembler::fdiv(int i) {
2020  EnsureSpace ensure_space(this);
2021  emit_farith(0xDC, 0xF8, i);
2022 }
2023 
2024 
2025 void Assembler::fdiv_i(int i) {
2026  EnsureSpace ensure_space(this);
2027  emit_farith(0xD8, 0xF0, i);
2028 }
2029 
2030 
2031 void Assembler::faddp(int i) {
2032  EnsureSpace ensure_space(this);
2033  emit_farith(0xDE, 0xC0, i);
2034 }
2035 
2036 
2037 void Assembler::fsubp(int i) {
2038  EnsureSpace ensure_space(this);
2039  emit_farith(0xDE, 0xE8, i);
2040 }
2041 
2042 
2043 void Assembler::fsubrp(int i) {
2044  EnsureSpace ensure_space(this);
2045  emit_farith(0xDE, 0xE0, i);
2046 }
2047 
2048 
2049 void Assembler::fmulp(int i) {
2050  EnsureSpace ensure_space(this);
2051  emit_farith(0xDE, 0xC8, i);
2052 }
2053 
2054 
2055 void Assembler::fdivp(int i) {
2056  EnsureSpace ensure_space(this);
2057  emit_farith(0xDE, 0xF8, i);
2058 }
2059 
2060 
2061 void Assembler::fprem() {
2062  EnsureSpace ensure_space(this);
2063  EMIT(0xD9);
2064  EMIT(0xF8);
2065 }
2066 
2067 
2068 void Assembler::fprem1() {
2069  EnsureSpace ensure_space(this);
2070  EMIT(0xD9);
2071  EMIT(0xF5);
2072 }
2073 
2074 
2075 void Assembler::fxch(int i) {
2076  EnsureSpace ensure_space(this);
2077  emit_farith(0xD9, 0xC8, i);
2078 }
2079 
2080 
2081 void Assembler::fincstp() {
2082  EnsureSpace ensure_space(this);
2083  EMIT(0xD9);
2084  EMIT(0xF7);
2085 }
2086 
2087 
2088 void Assembler::ffree(int i) {
2089  EnsureSpace ensure_space(this);
2090  emit_farith(0xDD, 0xC0, i);
2091 }
2092 
2093 
2094 void Assembler::ftst() {
2095  EnsureSpace ensure_space(this);
2096  EMIT(0xD9);
2097  EMIT(0xE4);
2098 }
2099 
2100 
2101 void Assembler::fucomp(int i) {
2102  EnsureSpace ensure_space(this);
2103  emit_farith(0xDD, 0xE8, i);
2104 }
2105 
2106 
2107 void Assembler::fucompp() {
2108  EnsureSpace ensure_space(this);
2109  EMIT(0xDA);
2110  EMIT(0xE9);
2111 }
2112 
2113 
2114 void Assembler::fucomi(int i) {
2115  EnsureSpace ensure_space(this);
2116  EMIT(0xDB);
2117  EMIT(0xE8 + i);
2118 }
2119 
2120 
2121 void Assembler::fucomip() {
2122  EnsureSpace ensure_space(this);
2123  EMIT(0xDF);
2124  EMIT(0xE9);
2125 }
2126 
2127 
2128 void Assembler::fcompp() {
2129  EnsureSpace ensure_space(this);
2130  EMIT(0xDE);
2131  EMIT(0xD9);
2132 }
2133 
2134 
2135 void Assembler::fnstsw_ax() {
2136  EnsureSpace ensure_space(this);
2137  EMIT(0xDF);
2138  EMIT(0xE0);
2139 }
2140 
2141 
2142 void Assembler::fwait() {
2143  EnsureSpace ensure_space(this);
2144  EMIT(0x9B);
2145 }
2146 
2147 
2148 void Assembler::frndint() {
2149  EnsureSpace ensure_space(this);
2150  EMIT(0xD9);
2151  EMIT(0xFC);
2152 }
2153 
2154 
2155 void Assembler::fnclex() {
2156  EnsureSpace ensure_space(this);
2157  EMIT(0xDB);
2158  EMIT(0xE2);
2159 }
2160 
2161 
2162 void Assembler::sahf() {
2163  EnsureSpace ensure_space(this);
2164  EMIT(0x9E);
2165 }
2166 
2167 
2168 void Assembler::setcc(Condition cc, Register reg) {
2169  DCHECK(reg.is_byte_register());
2170  EnsureSpace ensure_space(this);
2171  EMIT(0x0F);
2172  EMIT(0x90 | cc);
2173  EMIT(0xC0 | reg.code());
2174 }
2175 
2176 void Assembler::cvttss2si(Register dst, Operand src) {
2177  EnsureSpace ensure_space(this);
2178  // The [src] might contain ebx's register code, but in
2179  // this case, it refers to xmm3, so it is OK to emit.
2180  EMIT(0xF3);
2181  EMIT(0x0F);
2182  EMIT(0x2C);
2183  emit_operand(dst, src);
2184 }
2185 
2186 void Assembler::cvttsd2si(Register dst, Operand src) {
2187  EnsureSpace ensure_space(this);
2188  // The [src] might contain ebx's register code, but in
2189  // this case, it refers to xmm3, so it is OK to emit.
2190  EMIT(0xF2);
2191  EMIT(0x0F);
2192  EMIT(0x2C);
2193  emit_operand(dst, src);
2194 }
2195 
2196 
2197 void Assembler::cvtsd2si(Register dst, XMMRegister src) {
2198  EnsureSpace ensure_space(this);
2199  EMIT(0xF2);
2200  EMIT(0x0F);
2201  EMIT(0x2D);
2202  emit_sse_operand(dst, src);
2203 }
2204 
2205 void Assembler::cvtsi2ss(XMMRegister dst, Operand src) {
2206  EnsureSpace ensure_space(this);
2207  EMIT(0xF3);
2208  EMIT(0x0F);
2209  EMIT(0x2A);
2210  emit_sse_operand(dst, src);
2211 }
2212 
2213 void Assembler::cvtsi2sd(XMMRegister dst, Operand src) {
2214  EnsureSpace ensure_space(this);
2215  EMIT(0xF2);
2216  EMIT(0x0F);
2217  EMIT(0x2A);
2218  emit_sse_operand(dst, src);
2219 }
2220 
2221 void Assembler::cvtss2sd(XMMRegister dst, Operand src) {
2222  EnsureSpace ensure_space(this);
2223  EMIT(0xF3);
2224  EMIT(0x0F);
2225  EMIT(0x5A);
2226  emit_sse_operand(dst, src);
2227 }
2228 
2229 void Assembler::cvtsd2ss(XMMRegister dst, Operand src) {
2230  EnsureSpace ensure_space(this);
2231  EMIT(0xF2);
2232  EMIT(0x0F);
2233  EMIT(0x5A);
2234  emit_sse_operand(dst, src);
2235 }
2236 
2237 void Assembler::cvtdq2ps(XMMRegister dst, Operand src) {
2238  EnsureSpace ensure_space(this);
2239  EMIT(0x0F);
2240  EMIT(0x5B);
2241  emit_sse_operand(dst, src);
2242 }
2243 
2244 void Assembler::cvttps2dq(XMMRegister dst, Operand src) {
2245  EnsureSpace ensure_space(this);
2246  EMIT(0xF3);
2247  EMIT(0x0F);
2248  EMIT(0x5B);
2249  emit_sse_operand(dst, src);
2250 }
2251 
2252 void Assembler::addsd(XMMRegister dst, Operand src) {
2253  EnsureSpace ensure_space(this);
2254  EMIT(0xF2);
2255  EMIT(0x0F);
2256  EMIT(0x58);
2257  emit_sse_operand(dst, src);
2258 }
2259 
2260 void Assembler::mulsd(XMMRegister dst, Operand src) {
2261  EnsureSpace ensure_space(this);
2262  EMIT(0xF2);
2263  EMIT(0x0F);
2264  EMIT(0x59);
2265  emit_sse_operand(dst, src);
2266 }
2267 
2268 void Assembler::subsd(XMMRegister dst, Operand src) {
2269  EnsureSpace ensure_space(this);
2270  EMIT(0xF2);
2271  EMIT(0x0F);
2272  EMIT(0x5C);
2273  emit_sse_operand(dst, src);
2274 }
2275 
2276 void Assembler::divsd(XMMRegister dst, Operand src) {
2277  EnsureSpace ensure_space(this);
2278  EMIT(0xF2);
2279  EMIT(0x0F);
2280  EMIT(0x5E);
2281  emit_sse_operand(dst, src);
2282 }
2283 
2284 void Assembler::xorpd(XMMRegister dst, Operand src) {
2285  EnsureSpace ensure_space(this);
2286  EMIT(0x66);
2287  EMIT(0x0F);
2288  EMIT(0x57);
2289  emit_sse_operand(dst, src);
2290 }
2291 
2292 void Assembler::andps(XMMRegister dst, Operand src) {
2293  EnsureSpace ensure_space(this);
2294  EMIT(0x0F);
2295  EMIT(0x54);
2296  emit_sse_operand(dst, src);
2297 }
2298 
2299 void Assembler::orps(XMMRegister dst, Operand src) {
2300  EnsureSpace ensure_space(this);
2301  EMIT(0x0F);
2302  EMIT(0x56);
2303  emit_sse_operand(dst, src);
2304 }
2305 
2306 void Assembler::xorps(XMMRegister dst, Operand src) {
2307  EnsureSpace ensure_space(this);
2308  EMIT(0x0F);
2309  EMIT(0x57);
2310  emit_sse_operand(dst, src);
2311 }
2312 
2313 void Assembler::addps(XMMRegister dst, Operand src) {
2314  EnsureSpace ensure_space(this);
2315  EMIT(0x0F);
2316  EMIT(0x58);
2317  emit_sse_operand(dst, src);
2318 }
2319 
2320 void Assembler::subps(XMMRegister dst, Operand src) {
2321  EnsureSpace ensure_space(this);
2322  EMIT(0x0F);
2323  EMIT(0x5C);
2324  emit_sse_operand(dst, src);
2325 }
2326 
2327 void Assembler::mulps(XMMRegister dst, Operand src) {
2328  EnsureSpace ensure_space(this);
2329  EMIT(0x0F);
2330  EMIT(0x59);
2331  emit_sse_operand(dst, src);
2332 }
2333 
2334 void Assembler::divps(XMMRegister dst, Operand src) {
2335  EnsureSpace ensure_space(this);
2336  EMIT(0x0F);
2337  EMIT(0x5E);
2338  emit_sse_operand(dst, src);
2339 }
2340 
2341 void Assembler::rcpps(XMMRegister dst, Operand src) {
2342  EnsureSpace ensure_space(this);
2343  EMIT(0x0F);
2344  EMIT(0x53);
2345  emit_sse_operand(dst, src);
2346 }
2347 
2348 void Assembler::rsqrtps(XMMRegister dst, Operand src) {
2349  EnsureSpace ensure_space(this);
2350  EMIT(0x0F);
2351  EMIT(0x52);
2352  emit_sse_operand(dst, src);
2353 }
2354 
2355 void Assembler::minps(XMMRegister dst, Operand src) {
2356  EnsureSpace ensure_space(this);
2357  EMIT(0x0F);
2358  EMIT(0x5D);
2359  emit_sse_operand(dst, src);
2360 }
2361 
2362 void Assembler::maxps(XMMRegister dst, Operand src) {
2363  EnsureSpace ensure_space(this);
2364  EMIT(0x0F);
2365  EMIT(0x5F);
2366  emit_sse_operand(dst, src);
2367 }
2368 
2369 void Assembler::cmpps(XMMRegister dst, Operand src, uint8_t cmp) {
2370  EnsureSpace ensure_space(this);
2371  EMIT(0x0F);
2372  EMIT(0xC2);
2373  emit_sse_operand(dst, src);
2374  EMIT(cmp);
2375 }
2376 
2377 void Assembler::sqrtsd(XMMRegister dst, Operand src) {
2378  EnsureSpace ensure_space(this);
2379  EMIT(0xF2);
2380  EMIT(0x0F);
2381  EMIT(0x51);
2382  emit_sse_operand(dst, src);
2383 }
2384 
2385 void Assembler::haddps(XMMRegister dst, Operand src) {
2386  DCHECK(IsEnabled(SSE3));
2387  EnsureSpace ensure_space(this);
2388  EMIT(0xF2);
2389  EMIT(0x0F);
2390  EMIT(0x7C);
2391  emit_sse_operand(dst, src);
2392 }
2393 
2394 void Assembler::andpd(XMMRegister dst, Operand src) {
2395  EnsureSpace ensure_space(this);
2396  EMIT(0x66);
2397  EMIT(0x0F);
2398  EMIT(0x54);
2399  emit_sse_operand(dst, src);
2400 }
2401 
2402 void Assembler::orpd(XMMRegister dst, Operand src) {
2403  EnsureSpace ensure_space(this);
2404  EMIT(0x66);
2405  EMIT(0x0F);
2406  EMIT(0x56);
2407  emit_sse_operand(dst, src);
2408 }
2409 
2410 void Assembler::ucomisd(XMMRegister dst, Operand src) {
2411  EnsureSpace ensure_space(this);
2412  EMIT(0x66);
2413  EMIT(0x0F);
2414  EMIT(0x2E);
2415  emit_sse_operand(dst, src);
2416 }
2417 
2418 
2419 void Assembler::roundss(XMMRegister dst, XMMRegister src, RoundingMode mode) {
2420  DCHECK(IsEnabled(SSE4_1));
2421  EnsureSpace ensure_space(this);
2422  EMIT(0x66);
2423  EMIT(0x0F);
2424  EMIT(0x3A);
2425  EMIT(0x0A);
2426  emit_sse_operand(dst, src);
2427  // Mask precision exeption.
2428  EMIT(static_cast<byte>(mode) | 0x8);
2429 }
2430 
2431 
2432 void Assembler::roundsd(XMMRegister dst, XMMRegister src, RoundingMode mode) {
2433  DCHECK(IsEnabled(SSE4_1));
2434  EnsureSpace ensure_space(this);
2435  EMIT(0x66);
2436  EMIT(0x0F);
2437  EMIT(0x3A);
2438  EMIT(0x0B);
2439  emit_sse_operand(dst, src);
2440  // Mask precision exeption.
2441  EMIT(static_cast<byte>(mode) | 0x8);
2442 }
2443 
2444 
2445 void Assembler::movmskpd(Register dst, XMMRegister src) {
2446  EnsureSpace ensure_space(this);
2447  EMIT(0x66);
2448  EMIT(0x0F);
2449  EMIT(0x50);
2450  emit_sse_operand(dst, src);
2451 }
2452 
2453 
2454 void Assembler::movmskps(Register dst, XMMRegister src) {
2455  EnsureSpace ensure_space(this);
2456  EMIT(0x0F);
2457  EMIT(0x50);
2458  emit_sse_operand(dst, src);
2459 }
2460 
2461 void Assembler::maxsd(XMMRegister dst, Operand src) {
2462  EnsureSpace ensure_space(this);
2463  EMIT(0xF2);
2464  EMIT(0x0F);
2465  EMIT(0x5F);
2466  emit_sse_operand(dst, src);
2467 }
2468 
2469 void Assembler::minsd(XMMRegister dst, Operand src) {
2470  EnsureSpace ensure_space(this);
2471  EMIT(0xF2);
2472  EMIT(0x0F);
2473  EMIT(0x5D);
2474  emit_sse_operand(dst, src);
2475 }
2476 
2477 
2478 void Assembler::cmpltsd(XMMRegister dst, XMMRegister src) {
2479  EnsureSpace ensure_space(this);
2480  EMIT(0xF2);
2481  EMIT(0x0F);
2482  EMIT(0xC2);
2483  emit_sse_operand(dst, src);
2484  EMIT(1); // LT == 1
2485 }
2486 
2487 
2488 void Assembler::movaps(XMMRegister dst, XMMRegister src) {
2489  EnsureSpace ensure_space(this);
2490  EMIT(0x0F);
2491  EMIT(0x28);
2492  emit_sse_operand(dst, src);
2493 }
2494 
2495 void Assembler::movups(XMMRegister dst, XMMRegister src) {
2496  EnsureSpace ensure_space(this);
2497  EMIT(0x0F);
2498  EMIT(0x10);
2499  emit_sse_operand(dst, src);
2500 }
2501 
2502 void Assembler::movups(XMMRegister dst, Operand src) {
2503  EnsureSpace ensure_space(this);
2504  EMIT(0x0F);
2505  EMIT(0x10);
2506  emit_sse_operand(dst, src);
2507 }
2508 
2509 void Assembler::movups(Operand dst, XMMRegister src) {
2510  EnsureSpace ensure_space(this);
2511  EMIT(0x0F);
2512  EMIT(0x11);
2513  emit_sse_operand(src, dst);
2514 }
2515 
2516 void Assembler::shufps(XMMRegister dst, XMMRegister src, byte imm8) {
2517  DCHECK(is_uint8(imm8));
2518  EnsureSpace ensure_space(this);
2519  EMIT(0x0F);
2520  EMIT(0xC6);
2521  emit_sse_operand(dst, src);
2522  EMIT(imm8);
2523 }
2524 
2525 void Assembler::movdqa(Operand dst, XMMRegister src) {
2526  EnsureSpace ensure_space(this);
2527  EMIT(0x66);
2528  EMIT(0x0F);
2529  EMIT(0x7F);
2530  emit_sse_operand(src, dst);
2531 }
2532 
2533 void Assembler::movdqa(XMMRegister dst, Operand src) {
2534  EnsureSpace ensure_space(this);
2535  EMIT(0x66);
2536  EMIT(0x0F);
2537  EMIT(0x6F);
2538  emit_sse_operand(dst, src);
2539 }
2540 
2541 void Assembler::movdqu(Operand dst, XMMRegister src) {
2542  EnsureSpace ensure_space(this);
2543  EMIT(0xF3);
2544  EMIT(0x0F);
2545  EMIT(0x7F);
2546  emit_sse_operand(src, dst);
2547 }
2548 
2549 void Assembler::movdqu(XMMRegister dst, Operand src) {
2550  EnsureSpace ensure_space(this);
2551  EMIT(0xF3);
2552  EMIT(0x0F);
2553  EMIT(0x6F);
2554  emit_sse_operand(dst, src);
2555 }
2556 
2557 void Assembler::prefetch(Operand src, int level) {
2558  DCHECK(is_uint2(level));
2559  EnsureSpace ensure_space(this);
2560  EMIT(0x0F);
2561  EMIT(0x18);
2562  // Emit hint number in Reg position of RegR/M.
2563  XMMRegister code = XMMRegister::from_code(level);
2564  emit_sse_operand(code, src);
2565 }
2566 
2567 void Assembler::movsd(Operand dst, XMMRegister src) {
2568  EnsureSpace ensure_space(this);
2569  EMIT(0xF2); // double
2570  EMIT(0x0F);
2571  EMIT(0x11); // store
2572  emit_sse_operand(src, dst);
2573 }
2574 
2575 void Assembler::movsd(XMMRegister dst, Operand src) {
2576  EnsureSpace ensure_space(this);
2577  EMIT(0xF2); // double
2578  EMIT(0x0F);
2579  EMIT(0x10); // load
2580  emit_sse_operand(dst, src);
2581 }
2582 
2583 void Assembler::movss(Operand dst, XMMRegister src) {
2584  EnsureSpace ensure_space(this);
2585  EMIT(0xF3); // float
2586  EMIT(0x0F);
2587  EMIT(0x11); // store
2588  emit_sse_operand(src, dst);
2589 }
2590 
2591 void Assembler::movss(XMMRegister dst, Operand src) {
2592  EnsureSpace ensure_space(this);
2593  EMIT(0xF3); // float
2594  EMIT(0x0F);
2595  EMIT(0x10); // load
2596  emit_sse_operand(dst, src);
2597 }
2598 
2599 void Assembler::movd(XMMRegister dst, Operand src) {
2600  EnsureSpace ensure_space(this);
2601  EMIT(0x66);
2602  EMIT(0x0F);
2603  EMIT(0x6E);
2604  emit_sse_operand(dst, src);
2605 }
2606 
2607 void Assembler::movd(Operand dst, XMMRegister src) {
2608  EnsureSpace ensure_space(this);
2609  EMIT(0x66);
2610  EMIT(0x0F);
2611  EMIT(0x7E);
2612  emit_sse_operand(src, dst);
2613 }
2614 
2615 
2616 void Assembler::extractps(Register dst, XMMRegister src, byte imm8) {
2617  DCHECK(IsEnabled(SSE4_1));
2618  DCHECK(is_uint8(imm8));
2619  EnsureSpace ensure_space(this);
2620  EMIT(0x66);
2621  EMIT(0x0F);
2622  EMIT(0x3A);
2623  EMIT(0x17);
2624  emit_sse_operand(src, dst);
2625  EMIT(imm8);
2626 }
2627 
2628 void Assembler::psllw(XMMRegister reg, uint8_t shift) {
2629  EnsureSpace ensure_space(this);
2630  EMIT(0x66);
2631  EMIT(0x0F);
2632  EMIT(0x71);
2633  emit_sse_operand(esi, reg); // esi == 6
2634  EMIT(shift);
2635 }
2636 
2637 void Assembler::pslld(XMMRegister reg, uint8_t shift) {
2638  EnsureSpace ensure_space(this);
2639  EMIT(0x66);
2640  EMIT(0x0F);
2641  EMIT(0x72);
2642  emit_sse_operand(esi, reg); // esi == 6
2643  EMIT(shift);
2644 }
2645 
2646 void Assembler::psrlw(XMMRegister reg, uint8_t shift) {
2647  EnsureSpace ensure_space(this);
2648  EMIT(0x66);
2649  EMIT(0x0F);
2650  EMIT(0x71);
2651  emit_sse_operand(edx, reg); // edx == 2
2652  EMIT(shift);
2653 }
2654 
2655 void Assembler::psrld(XMMRegister reg, uint8_t shift) {
2656  EnsureSpace ensure_space(this);
2657  EMIT(0x66);
2658  EMIT(0x0F);
2659  EMIT(0x72);
2660  emit_sse_operand(edx, reg); // edx == 2
2661  EMIT(shift);
2662 }
2663 
2664 void Assembler::psraw(XMMRegister reg, uint8_t shift) {
2665  EnsureSpace ensure_space(this);
2666  EMIT(0x66);
2667  EMIT(0x0F);
2668  EMIT(0x71);
2669  emit_sse_operand(esp, reg); // esp == 4
2670  EMIT(shift);
2671 }
2672 
2673 void Assembler::psrad(XMMRegister reg, uint8_t shift) {
2674  EnsureSpace ensure_space(this);
2675  EMIT(0x66);
2676  EMIT(0x0F);
2677  EMIT(0x72);
2678  emit_sse_operand(esp, reg); // esp == 4
2679  EMIT(shift);
2680 }
2681 
2682 void Assembler::psllq(XMMRegister reg, uint8_t shift) {
2683  EnsureSpace ensure_space(this);
2684  EMIT(0x66);
2685  EMIT(0x0F);
2686  EMIT(0x73);
2687  emit_sse_operand(esi, reg); // esi == 6
2688  EMIT(shift);
2689 }
2690 
2691 
2692 void Assembler::psllq(XMMRegister dst, XMMRegister src) {
2693  EnsureSpace ensure_space(this);
2694  EMIT(0x66);
2695  EMIT(0x0F);
2696  EMIT(0xF3);
2697  emit_sse_operand(dst, src);
2698 }
2699 
2700 void Assembler::psrlq(XMMRegister reg, uint8_t shift) {
2701  EnsureSpace ensure_space(this);
2702  EMIT(0x66);
2703  EMIT(0x0F);
2704  EMIT(0x73);
2705  emit_sse_operand(edx, reg); // edx == 2
2706  EMIT(shift);
2707 }
2708 
2709 
2710 void Assembler::psrlq(XMMRegister dst, XMMRegister src) {
2711  EnsureSpace ensure_space(this);
2712  EMIT(0x66);
2713  EMIT(0x0F);
2714  EMIT(0xD3);
2715  emit_sse_operand(dst, src);
2716 }
2717 
2718 void Assembler::pshufhw(XMMRegister dst, Operand src, uint8_t shuffle) {
2719  EnsureSpace ensure_space(this);
2720  EMIT(0xF3);
2721  EMIT(0x0F);
2722  EMIT(0x70);
2723  emit_sse_operand(dst, src);
2724  EMIT(shuffle);
2725 }
2726 
2727 void Assembler::pshuflw(XMMRegister dst, Operand src, uint8_t shuffle) {
2728  EnsureSpace ensure_space(this);
2729  EMIT(0xF2);
2730  EMIT(0x0F);
2731  EMIT(0x70);
2732  emit_sse_operand(dst, src);
2733  EMIT(shuffle);
2734 }
2735 
2736 void Assembler::pshufd(XMMRegister dst, Operand src, uint8_t shuffle) {
2737  EnsureSpace ensure_space(this);
2738  EMIT(0x66);
2739  EMIT(0x0F);
2740  EMIT(0x70);
2741  emit_sse_operand(dst, src);
2742  EMIT(shuffle);
2743 }
2744 
2745 void Assembler::pblendw(XMMRegister dst, Operand src, uint8_t mask) {
2746  DCHECK(IsEnabled(SSE4_1));
2747  EnsureSpace ensure_space(this);
2748  EMIT(0x66);
2749  EMIT(0x0F);
2750  EMIT(0x3A);
2751  EMIT(0x0E);
2752  emit_sse_operand(dst, src);
2753  EMIT(mask);
2754 }
2755 
2756 void Assembler::palignr(XMMRegister dst, Operand src, uint8_t mask) {
2757  DCHECK(IsEnabled(SSSE3));
2758  EnsureSpace ensure_space(this);
2759  EMIT(0x66);
2760  EMIT(0x0F);
2761  EMIT(0x3A);
2762  EMIT(0x0F);
2763  emit_sse_operand(dst, src);
2764  EMIT(mask);
2765 }
2766 
2767 void Assembler::pextrb(Operand dst, XMMRegister src, uint8_t offset) {
2768  DCHECK(IsEnabled(SSE4_1));
2769  EnsureSpace ensure_space(this);
2770  EMIT(0x66);
2771  EMIT(0x0F);
2772  EMIT(0x3A);
2773  EMIT(0x14);
2774  emit_sse_operand(src, dst);
2775  EMIT(offset);
2776 }
2777 
2778 void Assembler::pextrw(Operand dst, XMMRegister src, uint8_t offset) {
2779  DCHECK(IsEnabled(SSE4_1));
2780  EnsureSpace ensure_space(this);
2781  EMIT(0x66);
2782  EMIT(0x0F);
2783  EMIT(0x3A);
2784  EMIT(0x15);
2785  emit_sse_operand(src, dst);
2786  EMIT(offset);
2787 }
2788 
2789 void Assembler::pextrd(Operand dst, XMMRegister src, uint8_t offset) {
2790  DCHECK(IsEnabled(SSE4_1));
2791  EnsureSpace ensure_space(this);
2792  EMIT(0x66);
2793  EMIT(0x0F);
2794  EMIT(0x3A);
2795  EMIT(0x16);
2796  emit_sse_operand(src, dst);
2797  EMIT(offset);
2798 }
2799 
2800 void Assembler::insertps(XMMRegister dst, Operand src, uint8_t offset) {
2801  DCHECK(IsEnabled(SSE4_1));
2802  EnsureSpace ensure_space(this);
2803  EMIT(0x66);
2804  EMIT(0x0F);
2805  EMIT(0x3A);
2806  EMIT(0x21);
2807  emit_sse_operand(dst, src);
2808  EMIT(offset);
2809 }
2810 
2811 void Assembler::pinsrb(XMMRegister dst, Operand src, uint8_t offset) {
2812  DCHECK(IsEnabled(SSE4_1));
2813  EnsureSpace ensure_space(this);
2814  EMIT(0x66);
2815  EMIT(0x0F);
2816  EMIT(0x3A);
2817  EMIT(0x20);
2818  emit_sse_operand(dst, src);
2819  EMIT(offset);
2820 }
2821 
2822 void Assembler::pinsrw(XMMRegister dst, Operand src, uint8_t offset) {
2823  DCHECK(is_uint8(offset));
2824  EnsureSpace ensure_space(this);
2825  EMIT(0x66);
2826  EMIT(0x0F);
2827  EMIT(0xC4);
2828  emit_sse_operand(dst, src);
2829  EMIT(offset);
2830 }
2831 
2832 void Assembler::pinsrd(XMMRegister dst, Operand src, uint8_t offset) {
2833  DCHECK(IsEnabled(SSE4_1));
2834  EnsureSpace ensure_space(this);
2835  EMIT(0x66);
2836  EMIT(0x0F);
2837  EMIT(0x3A);
2838  EMIT(0x22);
2839  emit_sse_operand(dst, src);
2840  EMIT(offset);
2841 }
2842 
2843 void Assembler::addss(XMMRegister dst, Operand src) {
2844  EnsureSpace ensure_space(this);
2845  EMIT(0xF3);
2846  EMIT(0x0F);
2847  EMIT(0x58);
2848  emit_sse_operand(dst, src);
2849 }
2850 
2851 void Assembler::subss(XMMRegister dst, Operand src) {
2852  EnsureSpace ensure_space(this);
2853  EMIT(0xF3);
2854  EMIT(0x0F);
2855  EMIT(0x5C);
2856  emit_sse_operand(dst, src);
2857 }
2858 
2859 void Assembler::mulss(XMMRegister dst, Operand src) {
2860  EnsureSpace ensure_space(this);
2861  EMIT(0xF3);
2862  EMIT(0x0F);
2863  EMIT(0x59);
2864  emit_sse_operand(dst, src);
2865 }
2866 
2867 void Assembler::divss(XMMRegister dst, Operand src) {
2868  EnsureSpace ensure_space(this);
2869  EMIT(0xF3);
2870  EMIT(0x0F);
2871  EMIT(0x5E);
2872  emit_sse_operand(dst, src);
2873 }
2874 
2875 void Assembler::sqrtss(XMMRegister dst, Operand src) {
2876  EnsureSpace ensure_space(this);
2877  EMIT(0xF3);
2878  EMIT(0x0F);
2879  EMIT(0x51);
2880  emit_sse_operand(dst, src);
2881 }
2882 
2883 void Assembler::ucomiss(XMMRegister dst, Operand src) {
2884  EnsureSpace ensure_space(this);
2885  EMIT(0x0F);
2886  EMIT(0x2E);
2887  emit_sse_operand(dst, src);
2888 }
2889 
2890 void Assembler::maxss(XMMRegister dst, Operand src) {
2891  EnsureSpace ensure_space(this);
2892  EMIT(0xF3);
2893  EMIT(0x0F);
2894  EMIT(0x5F);
2895  emit_sse_operand(dst, src);
2896 }
2897 
2898 void Assembler::minss(XMMRegister dst, Operand src) {
2899  EnsureSpace ensure_space(this);
2900  EMIT(0xF3);
2901  EMIT(0x0F);
2902  EMIT(0x5D);
2903  emit_sse_operand(dst, src);
2904 }
2905 
2906 
2907 // AVX instructions
2908 void Assembler::vfmasd(byte op, XMMRegister dst, XMMRegister src1,
2909  Operand src2) {
2910  DCHECK(IsEnabled(FMA3));
2911  EnsureSpace ensure_space(this);
2912  emit_vex_prefix(src1, kLIG, k66, k0F38, kW1);
2913  EMIT(op);
2914  emit_sse_operand(dst, src2);
2915 }
2916 
2917 void Assembler::vfmass(byte op, XMMRegister dst, XMMRegister src1,
2918  Operand src2) {
2919  DCHECK(IsEnabled(FMA3));
2920  EnsureSpace ensure_space(this);
2921  emit_vex_prefix(src1, kLIG, k66, k0F38, kW0);
2922  EMIT(op);
2923  emit_sse_operand(dst, src2);
2924 }
2925 
2926 void Assembler::vsd(byte op, XMMRegister dst, XMMRegister src1, Operand src2) {
2927  vinstr(op, dst, src1, src2, kF2, k0F, kWIG);
2928 }
2929 
2930 void Assembler::vss(byte op, XMMRegister dst, XMMRegister src1, Operand src2) {
2931  vinstr(op, dst, src1, src2, kF3, k0F, kWIG);
2932 }
2933 
2934 void Assembler::vps(byte op, XMMRegister dst, XMMRegister src1, Operand src2) {
2935  vinstr(op, dst, src1, src2, kNone, k0F, kWIG);
2936 }
2937 
2938 void Assembler::vpd(byte op, XMMRegister dst, XMMRegister src1, Operand src2) {
2939  vinstr(op, dst, src1, src2, k66, k0F, kWIG);
2940 }
2941 
2942 void Assembler::vcmpps(XMMRegister dst, XMMRegister src1, Operand src2,
2943  uint8_t cmp) {
2944  vps(0xC2, dst, src1, src2);
2945  EMIT(cmp);
2946 }
2947 
2948 void Assembler::vshufps(XMMRegister dst, XMMRegister src1, Operand src2,
2949  byte imm8) {
2950  DCHECK(is_uint8(imm8));
2951  vps(0xC6, dst, src1, src2);
2952  EMIT(imm8);
2953 }
2954 
2955 void Assembler::vpsllw(XMMRegister dst, XMMRegister src, uint8_t imm8) {
2956  XMMRegister iop = XMMRegister::from_code(6);
2957  vinstr(0x71, iop, dst, Operand(src), k66, k0F, kWIG);
2958  EMIT(imm8);
2959 }
2960 
2961 void Assembler::vpslld(XMMRegister dst, XMMRegister src, uint8_t imm8) {
2962  XMMRegister iop = XMMRegister::from_code(6);
2963  vinstr(0x72, iop, dst, Operand(src), k66, k0F, kWIG);
2964  EMIT(imm8);
2965 }
2966 
2967 void Assembler::vpsrlw(XMMRegister dst, XMMRegister src, uint8_t imm8) {
2968  XMMRegister iop = XMMRegister::from_code(2);
2969  vinstr(0x71, iop, dst, Operand(src), k66, k0F, kWIG);
2970  EMIT(imm8);
2971 }
2972 
2973 void Assembler::vpsrld(XMMRegister dst, XMMRegister src, uint8_t imm8) {
2974  XMMRegister iop = XMMRegister::from_code(2);
2975  vinstr(0x72, iop, dst, Operand(src), k66, k0F, kWIG);
2976  EMIT(imm8);
2977 }
2978 
2979 void Assembler::vpsraw(XMMRegister dst, XMMRegister src, uint8_t imm8) {
2980  XMMRegister iop = XMMRegister::from_code(4);
2981  vinstr(0x71, iop, dst, Operand(src), k66, k0F, kWIG);
2982  EMIT(imm8);
2983 }
2984 
2985 void Assembler::vpsrad(XMMRegister dst, XMMRegister src, uint8_t imm8) {
2986  XMMRegister iop = XMMRegister::from_code(4);
2987  vinstr(0x72, iop, dst, Operand(src), k66, k0F, kWIG);
2988  EMIT(imm8);
2989 }
2990 
2991 void Assembler::vpshufhw(XMMRegister dst, Operand src, uint8_t shuffle) {
2992  vinstr(0x70, dst, xmm0, src, kF3, k0F, kWIG);
2993  EMIT(shuffle);
2994 }
2995 
2996 void Assembler::vpshuflw(XMMRegister dst, Operand src, uint8_t shuffle) {
2997  vinstr(0x70, dst, xmm0, src, kF2, k0F, kWIG);
2998  EMIT(shuffle);
2999 }
3000 
3001 void Assembler::vpshufd(XMMRegister dst, Operand src, uint8_t shuffle) {
3002  vinstr(0x70, dst, xmm0, src, k66, k0F, kWIG);
3003  EMIT(shuffle);
3004 }
3005 
3006 void Assembler::vpblendw(XMMRegister dst, XMMRegister src1, Operand src2,
3007  uint8_t mask) {
3008  vinstr(0x0E, dst, src1, src2, k66, k0F3A, kWIG);
3009  EMIT(mask);
3010 }
3011 
3012 void Assembler::vpalignr(XMMRegister dst, XMMRegister src1, Operand src2,
3013  uint8_t mask) {
3014  vinstr(0x0F, dst, src1, src2, k66, k0F3A, kWIG);
3015  EMIT(mask);
3016 }
3017 
3018 void Assembler::vpextrb(Operand dst, XMMRegister src, uint8_t offset) {
3019  vinstr(0x14, src, xmm0, dst, k66, k0F3A, kWIG);
3020  EMIT(offset);
3021 }
3022 
3023 void Assembler::vpextrw(Operand dst, XMMRegister src, uint8_t offset) {
3024  vinstr(0x15, src, xmm0, dst, k66, k0F3A, kWIG);
3025  EMIT(offset);
3026 }
3027 
3028 void Assembler::vpextrd(Operand dst, XMMRegister src, uint8_t offset) {
3029  vinstr(0x16, src, xmm0, dst, k66, k0F3A, kWIG);
3030  EMIT(offset);
3031 }
3032 
3033 void Assembler::vinsertps(XMMRegister dst, XMMRegister src1, Operand src2,
3034  uint8_t offset) {
3035  vinstr(0x21, dst, src1, src2, k66, k0F3A, kWIG);
3036  EMIT(offset);
3037 }
3038 
3039 void Assembler::vpinsrb(XMMRegister dst, XMMRegister src1, Operand src2,
3040  uint8_t offset) {
3041  vinstr(0x20, dst, src1, src2, k66, k0F3A, kWIG);
3042  EMIT(offset);
3043 }
3044 
3045 void Assembler::vpinsrw(XMMRegister dst, XMMRegister src1, Operand src2,
3046  uint8_t offset) {
3047  vinstr(0xC4, dst, src1, src2, k66, k0F, kWIG);
3048  EMIT(offset);
3049 }
3050 
3051 void Assembler::vpinsrd(XMMRegister dst, XMMRegister src1, Operand src2,
3052  uint8_t offset) {
3053  vinstr(0x22, dst, src1, src2, k66, k0F3A, kWIG);
3054  EMIT(offset);
3055 }
3056 
3057 void Assembler::bmi1(byte op, Register reg, Register vreg, Operand rm) {
3058  DCHECK(IsEnabled(BMI1));
3059  EnsureSpace ensure_space(this);
3060  emit_vex_prefix(vreg, kLZ, kNone, k0F38, kW0);
3061  EMIT(op);
3062  emit_operand(reg, rm);
3063 }
3064 
3065 void Assembler::tzcnt(Register dst, Operand src) {
3066  DCHECK(IsEnabled(BMI1));
3067  EnsureSpace ensure_space(this);
3068  EMIT(0xF3);
3069  EMIT(0x0F);
3070  EMIT(0xBC);
3071  emit_operand(dst, src);
3072 }
3073 
3074 void Assembler::lzcnt(Register dst, Operand src) {
3075  DCHECK(IsEnabled(LZCNT));
3076  EnsureSpace ensure_space(this);
3077  EMIT(0xF3);
3078  EMIT(0x0F);
3079  EMIT(0xBD);
3080  emit_operand(dst, src);
3081 }
3082 
3083 void Assembler::popcnt(Register dst, Operand src) {
3084  DCHECK(IsEnabled(POPCNT));
3085  EnsureSpace ensure_space(this);
3086  EMIT(0xF3);
3087  EMIT(0x0F);
3088  EMIT(0xB8);
3089  emit_operand(dst, src);
3090 }
3091 
3092 void Assembler::bmi2(SIMDPrefix pp, byte op, Register reg, Register vreg,
3093  Operand rm) {
3094  DCHECK(IsEnabled(BMI2));
3095  EnsureSpace ensure_space(this);
3096  emit_vex_prefix(vreg, kLZ, pp, k0F38, kW0);
3097  EMIT(op);
3098  emit_operand(reg, rm);
3099 }
3100 
3101 void Assembler::rorx(Register dst, Operand src, byte imm8) {
3102  DCHECK(IsEnabled(BMI2));
3103  DCHECK(is_uint8(imm8));
3104  Register vreg = Register::from_code<0>(); // VEX.vvvv unused
3105  EnsureSpace ensure_space(this);
3106  emit_vex_prefix(vreg, kLZ, kF2, k0F3A, kW0);
3107  EMIT(0xF0);
3108  emit_operand(dst, src);
3109  EMIT(imm8);
3110 }
3111 
3112 void Assembler::sse2_instr(XMMRegister dst, Operand src, byte prefix,
3113  byte escape, byte opcode) {
3114  EnsureSpace ensure_space(this);
3115  EMIT(prefix);
3116  EMIT(escape);
3117  EMIT(opcode);
3118  emit_sse_operand(dst, src);
3119 }
3120 
3121 void Assembler::ssse3_instr(XMMRegister dst, Operand src, byte prefix,
3122  byte escape1, byte escape2, byte opcode) {
3123  DCHECK(IsEnabled(SSSE3));
3124  EnsureSpace ensure_space(this);
3125  EMIT(prefix);
3126  EMIT(escape1);
3127  EMIT(escape2);
3128  EMIT(opcode);
3129  emit_sse_operand(dst, src);
3130 }
3131 
3132 void Assembler::sse4_instr(XMMRegister dst, Operand src, byte prefix,
3133  byte escape1, byte escape2, byte opcode) {
3134  DCHECK(IsEnabled(SSE4_1));
3135  EnsureSpace ensure_space(this);
3136  EMIT(prefix);
3137  EMIT(escape1);
3138  EMIT(escape2);
3139  EMIT(opcode);
3140  emit_sse_operand(dst, src);
3141 }
3142 
3143 void Assembler::vinstr(byte op, XMMRegister dst, XMMRegister src1, Operand src2,
3144  SIMDPrefix pp, LeadingOpcode m, VexW w) {
3145  DCHECK(IsEnabled(AVX));
3146  EnsureSpace ensure_space(this);
3147  emit_vex_prefix(src1, kL128, pp, m, w);
3148  EMIT(op);
3149  emit_sse_operand(dst, src2);
3150 }
3151 
3152 void Assembler::emit_sse_operand(XMMRegister reg, Operand adr) {
3153  Register ireg = Register::from_code(reg.code());
3154  emit_operand(ireg, adr);
3155 }
3156 
3157 
3158 void Assembler::emit_sse_operand(XMMRegister dst, XMMRegister src) {
3159  EMIT(0xC0 | dst.code() << 3 | src.code());
3160 }
3161 
3162 
3163 void Assembler::emit_sse_operand(Register dst, XMMRegister src) {
3164  EMIT(0xC0 | dst.code() << 3 | src.code());
3165 }
3166 
3167 
3168 void Assembler::emit_sse_operand(XMMRegister dst, Register src) {
3169  EMIT(0xC0 | (dst.code() << 3) | src.code());
3170 }
3171 
3172 
3173 void Assembler::emit_vex_prefix(XMMRegister vreg, VectorLength l, SIMDPrefix pp,
3174  LeadingOpcode mm, VexW w) {
3175  if (mm != k0F || w != kW0) {
3176  EMIT(0xC4);
3177  // Change RXB from "110" to "111" to align with gdb disassembler.
3178  EMIT(0xE0 | mm);
3179  EMIT(w | ((~vreg.code() & 0xF) << 3) | l | pp);
3180  } else {
3181  EMIT(0xC5);
3182  EMIT(((~vreg.code()) << 3) | l | pp);
3183  }
3184 }
3185 
3186 
3187 void Assembler::emit_vex_prefix(Register vreg, VectorLength l, SIMDPrefix pp,
3188  LeadingOpcode mm, VexW w) {
3189  XMMRegister ivreg = XMMRegister::from_code(vreg.code());
3190  emit_vex_prefix(ivreg, l, pp, mm, w);
3191 }
3192 
3193 
3194 void Assembler::GrowBuffer() {
3195  DCHECK(buffer_overflow());
3196  if (!own_buffer_) FATAL("external code buffer is too small");
3197 
3198  // Compute new buffer size.
3199  CodeDesc desc; // the new buffer
3200  desc.buffer_size = 2 * buffer_size_;
3201 
3202  // Some internal data structures overflow for very large buffers,
3203  // they must ensure that kMaximalBufferSize is not too large.
3204  if (desc.buffer_size > kMaximalBufferSize) {
3205  V8::FatalProcessOutOfMemory(nullptr, "Assembler::GrowBuffer");
3206  }
3207 
3208  // Set up new buffer.
3209  desc.buffer = NewArray<byte>(desc.buffer_size);
3210  desc.origin = this;
3211  desc.instr_size = pc_offset();
3212  desc.reloc_size = (buffer_ + buffer_size_) - (reloc_info_writer.pos());
3213 
3214  // Clear the buffer in debug mode. Use 'int3' instructions to make
3215  // sure to get into problems if we ever run uninitialized code.
3216 #ifdef DEBUG
3217  ZapCode(reinterpret_cast<Address>(desc.buffer), desc.buffer_size);
3218 #endif
3219 
3220  // Copy the data.
3221  int pc_delta = desc.buffer - buffer_;
3222  int rc_delta = (desc.buffer + desc.buffer_size) - (buffer_ + buffer_size_);
3223  MemMove(desc.buffer, buffer_, desc.instr_size);
3224  MemMove(rc_delta + reloc_info_writer.pos(), reloc_info_writer.pos(),
3225  desc.reloc_size);
3226 
3227  // Switch buffers.
3228  DeleteArray(buffer_);
3229  buffer_ = desc.buffer;
3230  buffer_size_ = desc.buffer_size;
3231  pc_ += pc_delta;
3232  reloc_info_writer.Reposition(reloc_info_writer.pos() + rc_delta,
3233  reloc_info_writer.last_pc() + pc_delta);
3234 
3235  // Relocate internal references.
3236  for (auto pos : internal_reference_positions_) {
3237  int32_t* p = reinterpret_cast<int32_t*>(buffer_ + pos);
3238  *p += pc_delta;
3239  }
3240 
3241  // Relocate pc-relative references.
3242  int mode_mask = RelocInfo::ModeMask(RelocInfo::OFF_HEAP_TARGET);
3243  DCHECK_EQ(mode_mask, RelocInfo::kApplyMask & mode_mask);
3244  for (RelocIterator it(desc, mode_mask); !it.done(); it.next()) {
3245  it.rinfo()->apply(pc_delta);
3246  }
3247 
3248  DCHECK(!buffer_overflow());
3249 }
3250 
3251 
3252 void Assembler::emit_arith_b(int op1, int op2, Register dst, int imm8) {
3253  DCHECK(is_uint8(op1) && is_uint8(op2)); // wrong opcode
3254  DCHECK(is_uint8(imm8));
3255  DCHECK_EQ(op1 & 0x01, 0); // should be 8bit operation
3256  EMIT(op1);
3257  EMIT(op2 | dst.code());
3258  EMIT(imm8);
3259 }
3260 
3261 
3262 void Assembler::emit_arith(int sel, Operand dst, const Immediate& x) {
3263  DCHECK((0 <= sel) && (sel <= 7));
3264  Register ireg = Register::from_code(sel);
3265  if (x.is_int8()) {
3266  EMIT(0x83); // using a sign-extended 8-bit immediate.
3267  emit_operand(ireg, dst);
3268  EMIT(x.immediate() & 0xFF);
3269  } else if (dst.is_reg(eax)) {
3270  EMIT((sel << 3) | 0x05); // short form if the destination is eax.
3271  emit(x);
3272  } else {
3273  EMIT(0x81); // using a literal 32-bit immediate.
3274  emit_operand(ireg, dst);
3275  emit(x);
3276  }
3277 }
3278 
3279 void Assembler::emit_operand(Register reg, Operand adr) {
3280  emit_operand(reg.code(), adr);
3281 }
3282 
3283 void Assembler::emit_operand(XMMRegister reg, Operand adr) {
3284  Register ireg = Register::from_code(reg.code());
3285  emit_operand(ireg, adr);
3286 }
3287 
3288 void Assembler::emit_operand(int code, Operand adr) {
3289  // Isolate-independent code may not embed relocatable addresses.
3290  DCHECK(!options().isolate_independent_code ||
3291  adr.rmode_ != RelocInfo::CODE_TARGET);
3292  DCHECK(!options().isolate_independent_code ||
3293  adr.rmode_ != RelocInfo::EMBEDDED_OBJECT);
3294  DCHECK(!options().isolate_independent_code ||
3295  adr.rmode_ != RelocInfo::EXTERNAL_REFERENCE);
3296 
3297  const unsigned length = adr.len_;
3298  DCHECK_GT(length, 0);
3299 
3300  // Emit updated ModRM byte containing the given register.
3301  pc_[0] = (adr.buf_[0] & ~0x38) | (code << 3);
3302 
3303  // Emit the rest of the encoded operand.
3304  for (unsigned i = 1; i < length; i++) pc_[i] = adr.buf_[i];
3305  pc_ += length;
3306 
3307  // Emit relocation information if necessary.
3308  if (length >= sizeof(int32_t) && !RelocInfo::IsNone(adr.rmode_)) {
3309  pc_ -= sizeof(int32_t); // pc_ must be *at* disp32
3310  RecordRelocInfo(adr.rmode_);
3311  if (adr.rmode_ == RelocInfo::INTERNAL_REFERENCE) { // Fixup for labels
3312  emit_label(*reinterpret_cast<Label**>(pc_));
3313  } else {
3314  pc_ += sizeof(int32_t);
3315  }
3316  }
3317 }
3318 
3319 
3320 void Assembler::emit_label(Label* label) {
3321  if (label->is_bound()) {
3322  internal_reference_positions_.push_back(pc_offset());
3323  emit(reinterpret_cast<uint32_t>(buffer_ + label->pos()));
3324  } else {
3325  emit_disp(label, Displacement::CODE_ABSOLUTE);
3326  }
3327 }
3328 
3329 
3330 void Assembler::emit_farith(int b1, int b2, int i) {
3331  DCHECK(is_uint8(b1) && is_uint8(b2)); // wrong opcode
3332  DCHECK(0 <= i && i < 8); // illegal stack offset
3333  EMIT(b1);
3334  EMIT(b2 + i);
3335 }
3336 
3337 
3338 void Assembler::db(uint8_t data) {
3339  EnsureSpace ensure_space(this);
3340  EMIT(data);
3341 }
3342 
3343 
3344 void Assembler::dd(uint32_t data) {
3345  EnsureSpace ensure_space(this);
3346  emit(data);
3347 }
3348 
3349 
3350 void Assembler::dq(uint64_t data) {
3351  EnsureSpace ensure_space(this);
3352  emit_q(data);
3353 }
3354 
3355 
3356 void Assembler::dd(Label* label) {
3357  EnsureSpace ensure_space(this);
3358  RecordRelocInfo(RelocInfo::INTERNAL_REFERENCE);
3359  emit_label(label);
3360 }
3361 
3362 
3363 void Assembler::RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data) {
3364  if (!ShouldRecordRelocInfo(rmode)) return;
3365  RelocInfo rinfo(reinterpret_cast<Address>(pc_), rmode, data, Code());
3366  reloc_info_writer.Write(&rinfo);
3367 }
3368 
3369 } // namespace internal
3370 } // namespace v8
3371 
3372 #endif // V8_TARGET_ARCH_IA32
Definition: libplatform.h:13