The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / v8 / src / x64 / assembler-x64.h @ f230a1cf

History | View | Annotate | Download (53.3 KB)

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 are
6
// 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 distribution.
14
//
15
// - Neither the name of Sun Microsystems or the names of contributors may
16
// be used to endorse or promote products derived from this software without
17
// specific prior written permission.
18
//
19
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30

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

    
35
// A lightweight X64 Assembler.
36

    
37
#ifndef V8_X64_ASSEMBLER_X64_H_
38
#define V8_X64_ASSEMBLER_X64_H_
39

    
40
#include "serialize.h"
41

    
42
namespace v8 {
43
namespace internal {
44

    
45
// Utility functions
46

    
47
// Test whether a 64-bit value is in a specific range.
48
inline bool is_uint32(int64_t x) {
49
  static const uint64_t kMaxUInt32 = V8_UINT64_C(0xffffffff);
50
  return static_cast<uint64_t>(x) <= kMaxUInt32;
51
}
52

    
53
inline bool is_int32(int64_t x) {
54
  static const int64_t kMinInt32 = -V8_INT64_C(0x80000000);
55
  return is_uint32(x - kMinInt32);
56
}
57

    
58
inline bool uint_is_int32(uint64_t x) {
59
  static const uint64_t kMaxInt32 = V8_UINT64_C(0x7fffffff);
60
  return x <= kMaxInt32;
61
}
62

    
63
inline bool is_uint32(uint64_t x) {
64
  static const uint64_t kMaxUInt32 = V8_UINT64_C(0xffffffff);
65
  return x <= kMaxUInt32;
66
}
67

    
68
// CPU Registers.
69
//
70
// 1) We would prefer to use an enum, but enum values are assignment-
71
// compatible with int, which has caused code-generation bugs.
72
//
73
// 2) We would prefer to use a class instead of a struct but we don't like
74
// the register initialization to depend on the particular initialization
75
// order (which appears to be different on OS X, Linux, and Windows for the
76
// installed versions of C++ we tried). Using a struct permits C-style
77
// "initialization". Also, the Register objects cannot be const as this
78
// forces initialization stubs in MSVC, making us dependent on initialization
79
// order.
80
//
81
// 3) By not using an enum, we are possibly preventing the compiler from
82
// doing certain constant folds, which may significantly reduce the
83
// code generated for some assembly instructions (because they boil down
84
// to a few constants). If this is a problem, we could change the code
85
// such that we use an enum in optimized mode, and the struct in debug
86
// mode. This way we get the compile-time error checking in debug mode
87
// and best performance in optimized code.
88
//
89

    
90
struct Register {
91
  // The non-allocatable registers are:
92
  //  rsp - stack pointer
93
  //  rbp - frame pointer
94
  //  rsi - context register
95
  //  r10 - fixed scratch register
96
  //  r12 - smi constant register
97
  //  r13 - root register
98
  static const int kMaxNumAllocatableRegisters = 10;
99
  static int NumAllocatableRegisters() {
100
    return kMaxNumAllocatableRegisters;
101
  }
102
  static const int kNumRegisters = 16;
103

    
104
  static int ToAllocationIndex(Register reg) {
105
    return kAllocationIndexByRegisterCode[reg.code()];
106
  }
107

    
108
  static Register FromAllocationIndex(int index) {
109
    ASSERT(index >= 0 && index < kMaxNumAllocatableRegisters);
110
    Register result = { kRegisterCodeByAllocationIndex[index] };
111
    return result;
112
  }
113

    
114
  static const char* AllocationIndexToString(int index) {
115
    ASSERT(index >= 0 && index < kMaxNumAllocatableRegisters);
116
    const char* const names[] = {
117
      "rax",
118
      "rbx",
119
      "rdx",
120
      "rcx",
121
      "rdi",
122
      "r8",
123
      "r9",
124
      "r11",
125
      "r14",
126
      "r15"
127
    };
128
    return names[index];
129
  }
130

    
131
  static Register from_code(int code) {
132
    Register r = { code };
133
    return r;
134
  }
135
  bool is_valid() const { return 0 <= code_ && code_ < kNumRegisters; }
136
  bool is(Register reg) const { return code_ == reg.code_; }
137
  // rax, rbx, rcx and rdx are byte registers, the rest are not.
138
  bool is_byte_register() const { return code_ <= 3; }
139
  int code() const {
140
    ASSERT(is_valid());
141
    return code_;
142
  }
143
  int bit() const {
144
    return 1 << code_;
145
  }
146

    
147
  // Return the high bit of the register code as a 0 or 1.  Used often
148
  // when constructing the REX prefix byte.
149
  int high_bit() const {
150
    return code_ >> 3;
151
  }
152
  // Return the 3 low bits of the register code.  Used when encoding registers
153
  // in modR/M, SIB, and opcode bytes.
154
  int low_bits() const {
155
    return code_ & 0x7;
156
  }
157

    
158
  // Unfortunately we can't make this private in a struct when initializing
159
  // by assignment.
160
  int code_;
161

    
162
 private:
163
  static const int kRegisterCodeByAllocationIndex[kMaxNumAllocatableRegisters];
164
  static const int kAllocationIndexByRegisterCode[kNumRegisters];
165
};
166

    
167
const int kRegister_rax_Code = 0;
168
const int kRegister_rcx_Code = 1;
169
const int kRegister_rdx_Code = 2;
170
const int kRegister_rbx_Code = 3;
171
const int kRegister_rsp_Code = 4;
172
const int kRegister_rbp_Code = 5;
173
const int kRegister_rsi_Code = 6;
174
const int kRegister_rdi_Code = 7;
175
const int kRegister_r8_Code = 8;
176
const int kRegister_r9_Code = 9;
177
const int kRegister_r10_Code = 10;
178
const int kRegister_r11_Code = 11;
179
const int kRegister_r12_Code = 12;
180
const int kRegister_r13_Code = 13;
181
const int kRegister_r14_Code = 14;
182
const int kRegister_r15_Code = 15;
183
const int kRegister_no_reg_Code = -1;
184

    
185
const Register rax = { kRegister_rax_Code };
186
const Register rcx = { kRegister_rcx_Code };
187
const Register rdx = { kRegister_rdx_Code };
188
const Register rbx = { kRegister_rbx_Code };
189
const Register rsp = { kRegister_rsp_Code };
190
const Register rbp = { kRegister_rbp_Code };
191
const Register rsi = { kRegister_rsi_Code };
192
const Register rdi = { kRegister_rdi_Code };
193
const Register r8 = { kRegister_r8_Code };
194
const Register r9 = { kRegister_r9_Code };
195
const Register r10 = { kRegister_r10_Code };
196
const Register r11 = { kRegister_r11_Code };
197
const Register r12 = { kRegister_r12_Code };
198
const Register r13 = { kRegister_r13_Code };
199
const Register r14 = { kRegister_r14_Code };
200
const Register r15 = { kRegister_r15_Code };
201
const Register no_reg = { kRegister_no_reg_Code };
202

    
203
#ifdef _WIN64
204
  // Windows calling convention
205
  const Register arg_reg_1 = { kRegister_rcx_Code };
206
  const Register arg_reg_2 = { kRegister_rdx_Code };
207
  const Register arg_reg_3 = { kRegister_r8_Code };
208
  const Register arg_reg_4 = { kRegister_r9_Code };
209
#else
210
  // AMD64 calling convention
211
  const Register arg_reg_1 = { kRegister_rdi_Code };
212
  const Register arg_reg_2 = { kRegister_rsi_Code };
213
  const Register arg_reg_3 = { kRegister_rdx_Code };
214
  const Register arg_reg_4 = { kRegister_rcx_Code };
215
#endif  // _WIN64
216

    
217
struct XMMRegister {
218
  static const int kMaxNumRegisters = 16;
219
  static const int kMaxNumAllocatableRegisters = 15;
220
  static int NumAllocatableRegisters() {
221
    return kMaxNumAllocatableRegisters;
222
  }
223

    
224
  static int ToAllocationIndex(XMMRegister reg) {
225
    ASSERT(reg.code() != 0);
226
    return reg.code() - 1;
227
  }
228

    
229
  static XMMRegister FromAllocationIndex(int index) {
230
    ASSERT(0 <= index && index < kMaxNumAllocatableRegisters);
231
    XMMRegister result = { index + 1 };
232
    return result;
233
  }
234

    
235
  static const char* AllocationIndexToString(int index) {
236
    ASSERT(index >= 0 && index < kMaxNumAllocatableRegisters);
237
    const char* const names[] = {
238
      "xmm1",
239
      "xmm2",
240
      "xmm3",
241
      "xmm4",
242
      "xmm5",
243
      "xmm6",
244
      "xmm7",
245
      "xmm8",
246
      "xmm9",
247
      "xmm10",
248
      "xmm11",
249
      "xmm12",
250
      "xmm13",
251
      "xmm14",
252
      "xmm15"
253
    };
254
    return names[index];
255
  }
256

    
257
  static XMMRegister from_code(int code) {
258
    ASSERT(code >= 0);
259
    ASSERT(code < kMaxNumRegisters);
260
    XMMRegister r = { code };
261
    return r;
262
  }
263
  bool is_valid() const { return 0 <= code_ && code_ < kMaxNumRegisters; }
264
  bool is(XMMRegister reg) const { return code_ == reg.code_; }
265
  int code() const {
266
    ASSERT(is_valid());
267
    return code_;
268
  }
269

    
270
  // Return the high bit of the register code as a 0 or 1.  Used often
271
  // when constructing the REX prefix byte.
272
  int high_bit() const {
273
    return code_ >> 3;
274
  }
275
  // Return the 3 low bits of the register code.  Used when encoding registers
276
  // in modR/M, SIB, and opcode bytes.
277
  int low_bits() const {
278
    return code_ & 0x7;
279
  }
280

    
281
  int code_;
282
};
283

    
284
const XMMRegister xmm0 = { 0 };
285
const XMMRegister xmm1 = { 1 };
286
const XMMRegister xmm2 = { 2 };
287
const XMMRegister xmm3 = { 3 };
288
const XMMRegister xmm4 = { 4 };
289
const XMMRegister xmm5 = { 5 };
290
const XMMRegister xmm6 = { 6 };
291
const XMMRegister xmm7 = { 7 };
292
const XMMRegister xmm8 = { 8 };
293
const XMMRegister xmm9 = { 9 };
294
const XMMRegister xmm10 = { 10 };
295
const XMMRegister xmm11 = { 11 };
296
const XMMRegister xmm12 = { 12 };
297
const XMMRegister xmm13 = { 13 };
298
const XMMRegister xmm14 = { 14 };
299
const XMMRegister xmm15 = { 15 };
300

    
301

    
302
typedef XMMRegister DoubleRegister;
303

    
304

    
305
enum Condition {
306
  // any value < 0 is considered no_condition
307
  no_condition  = -1,
308

    
309
  overflow      =  0,
310
  no_overflow   =  1,
311
  below         =  2,
312
  above_equal   =  3,
313
  equal         =  4,
314
  not_equal     =  5,
315
  below_equal   =  6,
316
  above         =  7,
317
  negative      =  8,
318
  positive      =  9,
319
  parity_even   = 10,
320
  parity_odd    = 11,
321
  less          = 12,
322
  greater_equal = 13,
323
  less_equal    = 14,
324
  greater       = 15,
325

    
326
  // Fake conditions that are handled by the
327
  // opcodes using them.
328
  always        = 16,
329
  never         = 17,
330
  // aliases
331
  carry         = below,
332
  not_carry     = above_equal,
333
  zero          = equal,
334
  not_zero      = not_equal,
335
  sign          = negative,
336
  not_sign      = positive,
337
  last_condition = greater
338
};
339

    
340

    
341
// Returns the equivalent of !cc.
342
// Negation of the default no_condition (-1) results in a non-default
343
// no_condition value (-2). As long as tests for no_condition check
344
// for condition < 0, this will work as expected.
345
inline Condition NegateCondition(Condition cc) {
346
  return static_cast<Condition>(cc ^ 1);
347
}
348

    
349

    
350
// Corresponds to transposing the operands of a comparison.
351
inline Condition ReverseCondition(Condition cc) {
352
  switch (cc) {
353
    case below:
354
      return above;
355
    case above:
356
      return below;
357
    case above_equal:
358
      return below_equal;
359
    case below_equal:
360
      return above_equal;
361
    case less:
362
      return greater;
363
    case greater:
364
      return less;
365
    case greater_equal:
366
      return less_equal;
367
    case less_equal:
368
      return greater_equal;
369
    default:
370
      return cc;
371
  };
372
}
373

    
374

    
375
// -----------------------------------------------------------------------------
376
// Machine instruction Immediates
377

    
378
class Immediate BASE_EMBEDDED {
379
 public:
380
  explicit Immediate(int32_t value) : value_(value) {}
381

    
382
 private:
383
  int32_t value_;
384

    
385
  friend class Assembler;
386
};
387

    
388

    
389
// -----------------------------------------------------------------------------
390
// Machine instruction Operands
391

    
392
enum ScaleFactor {
393
  times_1 = 0,
394
  times_2 = 1,
395
  times_4 = 2,
396
  times_8 = 3,
397
  times_int_size = times_4,
398
  times_pointer_size = times_8
399
};
400

    
401

    
402
class Operand BASE_EMBEDDED {
403
 public:
404
  // [base + disp/r]
405
  Operand(Register base, int32_t disp);
406

    
407
  // [base + index*scale + disp/r]
408
  Operand(Register base,
409
          Register index,
410
          ScaleFactor scale,
411
          int32_t disp);
412

    
413
  // [index*scale + disp/r]
414
  Operand(Register index,
415
          ScaleFactor scale,
416
          int32_t disp);
417

    
418
  // Offset from existing memory operand.
419
  // Offset is added to existing displacement as 32-bit signed values and
420
  // this must not overflow.
421
  Operand(const Operand& base, int32_t offset);
422

    
423
  // Checks whether either base or index register is the given register.
424
  // Does not check the "reg" part of the Operand.
425
  bool AddressUsesRegister(Register reg) const;
426

    
427
  // Queries related to the size of the generated instruction.
428
  // Whether the generated instruction will have a REX prefix.
429
  bool requires_rex() const { return rex_ != 0; }
430
  // Size of the ModR/M, SIB and displacement parts of the generated
431
  // instruction.
432
  int operand_size() const { return len_; }
433

    
434
 private:
435
  byte rex_;
436
  byte buf_[6];
437
  // The number of bytes of buf_ in use.
438
  byte len_;
439

    
440
  // Set the ModR/M byte without an encoded 'reg' register. The
441
  // register is encoded later as part of the emit_operand operation.
442
  // set_modrm can be called before or after set_sib and set_disp*.
443
  inline void set_modrm(int mod, Register rm);
444

    
445
  // Set the SIB byte if one is needed. Sets the length to 2 rather than 1.
446
  inline void set_sib(ScaleFactor scale, Register index, Register base);
447

    
448
  // Adds operand displacement fields (offsets added to the memory address).
449
  // Needs to be called after set_sib, not before it.
450
  inline void set_disp8(int disp);
451
  inline void set_disp32(int disp);
452

    
453
  friend class Assembler;
454
};
455

    
456

    
457
// CpuFeatures keeps track of which features are supported by the target CPU.
458
// Supported features must be enabled by a CpuFeatureScope before use.
459
// Example:
460
//   if (assembler->IsSupported(SSE3)) {
461
//     CpuFeatureScope fscope(assembler, SSE3);
462
//     // Generate SSE3 floating point code.
463
//   } else {
464
//     // Generate standard SSE2 floating point code.
465
//   }
466
class CpuFeatures : public AllStatic {
467
 public:
468
  // Detect features of the target CPU. Set safe defaults if the serializer
469
  // is enabled (snapshots must be portable).
470
  static void Probe();
471

    
472
  // Check whether a feature is supported by the target CPU.
473
  static bool IsSupported(CpuFeature f) {
474
    if (Check(f, cross_compile_)) return true;
475
    ASSERT(initialized_);
476
    if (f == SSE3 && !FLAG_enable_sse3) return false;
477
    if (f == SSE4_1 && !FLAG_enable_sse4_1) return false;
478
    if (f == CMOV && !FLAG_enable_cmov) return false;
479
    if (f == SAHF && !FLAG_enable_sahf) return false;
480
    return Check(f, supported_);
481
  }
482

    
483
  static bool IsFoundByRuntimeProbingOnly(CpuFeature f) {
484
    ASSERT(initialized_);
485
    return Check(f, found_by_runtime_probing_only_);
486
  }
487

    
488
  static bool IsSafeForSnapshot(CpuFeature f) {
489
    return Check(f, cross_compile_) ||
490
           (IsSupported(f) &&
491
            (!Serializer::enabled() || !IsFoundByRuntimeProbingOnly(f)));
492
  }
493

    
494
  static bool VerifyCrossCompiling() {
495
    return cross_compile_ == 0;
496
  }
497

    
498
  static bool VerifyCrossCompiling(CpuFeature f) {
499
    uint64_t mask = flag2set(f);
500
    return cross_compile_ == 0 ||
501
           (cross_compile_ & mask) == mask;
502
  }
503

    
504
 private:
505
  static bool Check(CpuFeature f, uint64_t set) {
506
    return (set & flag2set(f)) != 0;
507
  }
508

    
509
  static uint64_t flag2set(CpuFeature f) {
510
    return static_cast<uint64_t>(1) << f;
511
  }
512

    
513
  // Safe defaults include CMOV for X64. It is always available, if
514
  // anyone checks, but they shouldn't need to check.
515
  // The required user mode extensions in X64 are (from AMD64 ABI Table A.1):
516
  //   fpu, tsc, cx8, cmov, mmx, sse, sse2, fxsr, syscall
517
  static const uint64_t kDefaultCpuFeatures = (1 << CMOV);
518

    
519
#ifdef DEBUG
520
  static bool initialized_;
521
#endif
522
  static uint64_t supported_;
523
  static uint64_t found_by_runtime_probing_only_;
524

    
525
  static uint64_t cross_compile_;
526

    
527
  friend class ExternalReference;
528
  friend class PlatformFeatureScope;
529
  DISALLOW_COPY_AND_ASSIGN(CpuFeatures);
530
};
531

    
532

    
533
class Assembler : public AssemblerBase {
534
 private:
535
  // We check before assembling an instruction that there is sufficient
536
  // space to write an instruction and its relocation information.
537
  // The relocation writer's position must be kGap bytes above the end of
538
  // the generated instructions. This leaves enough space for the
539
  // longest possible x64 instruction, 15 bytes, and the longest possible
540
  // relocation information encoding, RelocInfoWriter::kMaxLength == 16.
541
  // (There is a 15 byte limit on x64 instruction length that rules out some
542
  // otherwise valid instructions.)
543
  // This allows for a single, fast space check per instruction.
544
  static const int kGap = 32;
545

    
546
 public:
547
  // Create an assembler. Instructions and relocation information are emitted
548
  // into a buffer, with the instructions starting from the beginning and the
549
  // relocation information starting from the end of the buffer. See CodeDesc
550
  // for a detailed comment on the layout (globals.h).
551
  //
552
  // If the provided buffer is NULL, the assembler allocates and grows its own
553
  // buffer, and buffer_size determines the initial buffer size. The buffer is
554
  // owned by the assembler and deallocated upon destruction of the assembler.
555
  //
556
  // If the provided buffer is not NULL, the assembler uses the provided buffer
557
  // for code generation and assumes its size to be buffer_size. If the buffer
558
  // is too small, a fatal error occurs. No deallocation of the buffer is done
559
  // upon destruction of the assembler.
560
  Assembler(Isolate* isolate, void* buffer, int buffer_size);
561
  virtual ~Assembler() { }
562

    
563
  // GetCode emits any pending (non-emitted) code and fills the descriptor
564
  // desc. GetCode() is idempotent; it returns the same result if no other
565
  // Assembler functions are invoked in between GetCode() calls.
566
  void GetCode(CodeDesc* desc);
567

    
568
  // Read/Modify the code target in the relative branch/call instruction at pc.
569
  // On the x64 architecture, we use relative jumps with a 32-bit displacement
570
  // to jump to other Code objects in the Code space in the heap.
571
  // Jumps to C functions are done indirectly through a 64-bit register holding
572
  // the absolute address of the target.
573
  // These functions convert between absolute Addresses of Code objects and
574
  // the relative displacements stored in the code.
575
  static inline Address target_address_at(Address pc);
576
  static inline void set_target_address_at(Address pc, Address target);
577

    
578
  // Return the code target address at a call site from the return address
579
  // of that call in the instruction stream.
580
  static inline Address target_address_from_return_address(Address pc);
581

    
582
  // This sets the branch destination (which is in the instruction on x64).
583
  // This is for calls and branches within generated code.
584
  inline static void deserialization_set_special_target_at(
585
      Address instruction_payload, Address target) {
586
    set_target_address_at(instruction_payload, target);
587
  }
588

    
589
  // This sets the branch destination (which is a load instruction on x64).
590
  // This is for calls and branches to runtime code.
591
  inline static void set_external_target_at(Address instruction_payload,
592
                                            Address target) {
593
    *reinterpret_cast<Address*>(instruction_payload) = target;
594
  }
595

    
596
  inline Handle<Object> code_target_object_handle_at(Address pc);
597
  inline Address runtime_entry_at(Address pc);
598
  // Number of bytes taken up by the branch target in the code.
599
  static const int kSpecialTargetSize = 4;  // Use 32-bit displacement.
600
  // Distance between the address of the code target in the call instruction
601
  // and the return address pushed on the stack.
602
  static const int kCallTargetAddressOffset = 4;  // Use 32-bit displacement.
603
  // The length of call(kScratchRegister).
604
  static const int kCallScratchRegisterInstructionLength = 3;
605
  // The length of call(Immediate32).
606
  static const int kShortCallInstructionLength = 5;
607
  // The length of movq(kScratchRegister, address).
608
  static const int kMoveAddressIntoScratchRegisterInstructionLength =
609
      2 + kPointerSize;
610
  // The length of movq(kScratchRegister, address) and call(kScratchRegister).
611
  static const int kCallSequenceLength =
612
      kMoveAddressIntoScratchRegisterInstructionLength +
613
      kCallScratchRegisterInstructionLength;
614

    
615
  // The js return and debug break slot must be able to contain an indirect
616
  // call sequence, some x64 JS code is padded with int3 to make it large
617
  // enough to hold an instruction when the debugger patches it.
618
  static const int kJSReturnSequenceLength = kCallSequenceLength;
619
  static const int kDebugBreakSlotLength = kCallSequenceLength;
620
  static const int kPatchDebugBreakSlotReturnOffset = kCallTargetAddressOffset;
621
  // Distance between the start of the JS return sequence and where the
622
  // 32-bit displacement of a short call would be. The short call is from
623
  // SetDebugBreakAtIC from debug-x64.cc.
624
  static const int kPatchReturnSequenceAddressOffset =
625
      kJSReturnSequenceLength - kPatchDebugBreakSlotReturnOffset;
626
  // Distance between the start of the JS return sequence and where the
627
  // 32-bit displacement of a short call would be. The short call is from
628
  // SetDebugBreakAtIC from debug-x64.cc.
629
  static const int kPatchDebugBreakSlotAddressOffset =
630
      kDebugBreakSlotLength - kPatchDebugBreakSlotReturnOffset;
631
  static const int kRealPatchReturnSequenceAddressOffset =
632
      kMoveAddressIntoScratchRegisterInstructionLength - kPointerSize;
633

    
634
  // One byte opcode for test eax,0xXXXXXXXX.
635
  static const byte kTestEaxByte = 0xA9;
636
  // One byte opcode for test al, 0xXX.
637
  static const byte kTestAlByte = 0xA8;
638
  // One byte opcode for nop.
639
  static const byte kNopByte = 0x90;
640

    
641
  // One byte prefix for a short conditional jump.
642
  static const byte kJccShortPrefix = 0x70;
643
  static const byte kJncShortOpcode = kJccShortPrefix | not_carry;
644
  static const byte kJcShortOpcode = kJccShortPrefix | carry;
645
  static const byte kJnzShortOpcode = kJccShortPrefix | not_zero;
646
  static const byte kJzShortOpcode = kJccShortPrefix | zero;
647

    
648

    
649
  // ---------------------------------------------------------------------------
650
  // Code generation
651
  //
652
  // Function names correspond one-to-one to x64 instruction mnemonics.
653
  // Unless specified otherwise, instructions operate on 64-bit operands.
654
  //
655
  // If we need versions of an assembly instruction that operate on different
656
  // width arguments, we add a single-letter suffix specifying the width.
657
  // This is done for the following instructions: mov, cmp, inc, dec,
658
  // add, sub, and test.
659
  // There are no versions of these instructions without the suffix.
660
  // - Instructions on 8-bit (byte) operands/registers have a trailing 'b'.
661
  // - Instructions on 16-bit (word) operands/registers have a trailing 'w'.
662
  // - Instructions on 32-bit (doubleword) operands/registers use 'l'.
663
  // - Instructions on 64-bit (quadword) operands/registers use 'q'.
664
  //
665
  // Some mnemonics, such as "and", are the same as C++ keywords.
666
  // Naming conflicts with C++ keywords are resolved by adding a trailing '_'.
667

    
668
  // Insert the smallest number of nop instructions
669
  // possible to align the pc offset to a multiple
670
  // of m, where m must be a power of 2.
671
  void Align(int m);
672
  void Nop(int bytes = 1);
673
  // Aligns code to something that's optimal for a jump target for the platform.
674
  void CodeTargetAlign();
675

    
676
  // Stack
677
  void pushfq();
678
  void popfq();
679

    
680
  void push(Immediate value);
681
  // Push a 32 bit integer, and guarantee that it is actually pushed as a
682
  // 32 bit value, the normal push will optimize the 8 bit case.
683
  void push_imm32(int32_t imm32);
684
  void push(Register src);
685
  void push(const Operand& src);
686

    
687
  void pop(Register dst);
688
  void pop(const Operand& dst);
689

    
690
  void enter(Immediate size);
691
  void leave();
692

    
693
  // Moves
694
  void movb(Register dst, const Operand& src);
695
  void movb(Register dst, Immediate imm);
696
  void movb(const Operand& dst, Register src);
697

    
698
  // Move the low 16 bits of a 64-bit register value to a 16-bit
699
  // memory location.
700
  void movw(const Operand& dst, Register src);
701

    
702
  void movl(Register dst, Register src);
703
  void movl(Register dst, const Operand& src);
704
  void movl(const Operand& dst, Register src);
705
  void movl(const Operand& dst, Immediate imm);
706
  // Load a 32-bit immediate value, zero-extended to 64 bits.
707
  void movl(Register dst, Immediate imm32);
708

    
709
  // Move 64 bit register value to 64-bit memory location.
710
  void movq(const Operand& dst, Register src);
711
  // Move 64 bit memory location to 64-bit register value.
712
  void movq(Register dst, const Operand& src);
713
  void movq(Register dst, Register src);
714
  // Sign extends immediate 32-bit value to 64 bits.
715
  void movq(Register dst, Immediate x);
716
  // Move the offset of the label location relative to the current
717
  // position (after the move) to the destination.
718
  void movl(const Operand& dst, Label* src);
719

    
720
  // Move sign extended immediate to memory location.
721
  void movq(const Operand& dst, Immediate value);
722
  // Instructions to load a 64-bit immediate into a register.
723
  // All 64-bit immediates must have a relocation mode.
724
  void movq(Register dst, void* ptr, RelocInfo::Mode rmode);
725
  void movq(Register dst, int64_t value, RelocInfo::Mode rmode);
726
  // Moves the address of the external reference into the register.
727
  void movq(Register dst, ExternalReference ext);
728
  void movq(Register dst, Handle<Object> handle, RelocInfo::Mode rmode);
729

    
730
  void movsxbq(Register dst, const Operand& src);
731
  void movsxwq(Register dst, const Operand& src);
732
  void movsxlq(Register dst, Register src);
733
  void movsxlq(Register dst, const Operand& src);
734
  void movzxbq(Register dst, const Operand& src);
735
  void movzxbl(Register dst, const Operand& src);
736
  void movzxwq(Register dst, const Operand& src);
737
  void movzxwl(Register dst, const Operand& src);
738
  void movzxwl(Register dst, Register src);
739

    
740
  // Repeated moves.
741

    
742
  void repmovsb();
743
  void repmovsw();
744
  void repmovsl();
745
  void repmovsq();
746

    
747
  // Instruction to load from an immediate 64-bit pointer into RAX.
748
  void load_rax(void* ptr, RelocInfo::Mode rmode);
749
  void load_rax(ExternalReference ext);
750

    
751
  // Conditional moves.
752
  void cmovq(Condition cc, Register dst, Register src);
753
  void cmovq(Condition cc, Register dst, const Operand& src);
754
  void cmovl(Condition cc, Register dst, Register src);
755
  void cmovl(Condition cc, Register dst, const Operand& src);
756

    
757
  // Exchange two registers
758
  void xchgq(Register dst, Register src);
759
  void xchgl(Register dst, Register src);
760

    
761
  // Arithmetics
762
  void addl(Register dst, Register src) {
763
    arithmetic_op_32(0x03, dst, src);
764
  }
765

    
766
  void addl(Register dst, Immediate src) {
767
    immediate_arithmetic_op_32(0x0, dst, src);
768
  }
769

    
770
  void addl(Register dst, const Operand& src) {
771
    arithmetic_op_32(0x03, dst, src);
772
  }
773

    
774
  void addl(const Operand& dst, Immediate src) {
775
    immediate_arithmetic_op_32(0x0, dst, src);
776
  }
777

    
778
  void addl(const Operand& dst, Register src) {
779
    arithmetic_op_32(0x01, src, dst);
780
  }
781

    
782
  void addq(Register dst, Register src) {
783
    arithmetic_op(0x03, dst, src);
784
  }
785

    
786
  void addq(Register dst, const Operand& src) {
787
    arithmetic_op(0x03, dst, src);
788
  }
789

    
790
  void addq(const Operand& dst, Register src) {
791
    arithmetic_op(0x01, src, dst);
792
  }
793

    
794
  void addq(Register dst, Immediate src) {
795
    immediate_arithmetic_op(0x0, dst, src);
796
  }
797

    
798
  void addq(const Operand& dst, Immediate src) {
799
    immediate_arithmetic_op(0x0, dst, src);
800
  }
801

    
802
  void sbbl(Register dst, Register src) {
803
    arithmetic_op_32(0x1b, dst, src);
804
  }
805

    
806
  void sbbq(Register dst, Register src) {
807
    arithmetic_op(0x1b, dst, src);
808
  }
809

    
810
  void cmpb(Register dst, Immediate src) {
811
    immediate_arithmetic_op_8(0x7, dst, src);
812
  }
813

    
814
  void cmpb_al(Immediate src);
815

    
816
  void cmpb(Register dst, Register src) {
817
    arithmetic_op(0x3A, dst, src);
818
  }
819

    
820
  void cmpb(Register dst, const Operand& src) {
821
    arithmetic_op(0x3A, dst, src);
822
  }
823

    
824
  void cmpb(const Operand& dst, Register src) {
825
    arithmetic_op(0x38, src, dst);
826
  }
827

    
828
  void cmpb(const Operand& dst, Immediate src) {
829
    immediate_arithmetic_op_8(0x7, dst, src);
830
  }
831

    
832
  void cmpw(const Operand& dst, Immediate src) {
833
    immediate_arithmetic_op_16(0x7, dst, src);
834
  }
835

    
836
  void cmpw(Register dst, Immediate src) {
837
    immediate_arithmetic_op_16(0x7, dst, src);
838
  }
839

    
840
  void cmpw(Register dst, const Operand& src) {
841
    arithmetic_op_16(0x3B, dst, src);
842
  }
843

    
844
  void cmpw(Register dst, Register src) {
845
    arithmetic_op_16(0x3B, dst, src);
846
  }
847

    
848
  void cmpw(const Operand& dst, Register src) {
849
    arithmetic_op_16(0x39, src, dst);
850
  }
851

    
852
  void cmpl(Register dst, Register src) {
853
    arithmetic_op_32(0x3B, dst, src);
854
  }
855

    
856
  void cmpl(Register dst, const Operand& src) {
857
    arithmetic_op_32(0x3B, dst, src);
858
  }
859

    
860
  void cmpl(const Operand& dst, Register src) {
861
    arithmetic_op_32(0x39, src, dst);
862
  }
863

    
864
  void cmpl(Register dst, Immediate src) {
865
    immediate_arithmetic_op_32(0x7, dst, src);
866
  }
867

    
868
  void cmpl(const Operand& dst, Immediate src) {
869
    immediate_arithmetic_op_32(0x7, dst, src);
870
  }
871

    
872
  void cmpq(Register dst, Register src) {
873
    arithmetic_op(0x3B, dst, src);
874
  }
875

    
876
  void cmpq(Register dst, const Operand& src) {
877
    arithmetic_op(0x3B, dst, src);
878
  }
879

    
880
  void cmpq(const Operand& dst, Register src) {
881
    arithmetic_op(0x39, src, dst);
882
  }
883

    
884
  void cmpq(Register dst, Immediate src) {
885
    immediate_arithmetic_op(0x7, dst, src);
886
  }
887

    
888
  void cmpq(const Operand& dst, Immediate src) {
889
    immediate_arithmetic_op(0x7, dst, src);
890
  }
891

    
892
  void and_(Register dst, Register src) {
893
    arithmetic_op(0x23, dst, src);
894
  }
895

    
896
  void and_(Register dst, const Operand& src) {
897
    arithmetic_op(0x23, dst, src);
898
  }
899

    
900
  void and_(const Operand& dst, Register src) {
901
    arithmetic_op(0x21, src, dst);
902
  }
903

    
904
  void and_(Register dst, Immediate src) {
905
    immediate_arithmetic_op(0x4, dst, src);
906
  }
907

    
908
  void and_(const Operand& dst, Immediate src) {
909
    immediate_arithmetic_op(0x4, dst, src);
910
  }
911

    
912
  void andl(Register dst, Immediate src) {
913
    immediate_arithmetic_op_32(0x4, dst, src);
914
  }
915

    
916
  void andl(Register dst, Register src) {
917
    arithmetic_op_32(0x23, dst, src);
918
  }
919

    
920
  void andl(Register dst, const Operand& src) {
921
    arithmetic_op_32(0x23, dst, src);
922
  }
923

    
924
  void andb(Register dst, Immediate src) {
925
    immediate_arithmetic_op_8(0x4, dst, src);
926
  }
927

    
928
  void decq(Register dst);
929
  void decq(const Operand& dst);
930
  void decl(Register dst);
931
  void decl(const Operand& dst);
932
  void decb(Register dst);
933
  void decb(const Operand& dst);
934

    
935
  // Sign-extends rax into rdx:rax.
936
  void cqo();
937
  // Sign-extends eax into edx:eax.
938
  void cdq();
939

    
940
  // Divide rdx:rax by src.  Quotient in rax, remainder in rdx.
941
  void idivq(Register src);
942
  // Divide edx:eax by lower 32 bits of src.  Quotient in eax, rem. in edx.
943
  void idivl(Register src);
944

    
945
  // Signed multiply instructions.
946
  void imul(Register src);                               // rdx:rax = rax * src.
947
  void imul(Register dst, Register src);                 // dst = dst * src.
948
  void imul(Register dst, const Operand& src);           // dst = dst * src.
949
  void imul(Register dst, Register src, Immediate imm);  // dst = src * imm.
950
  // Signed 32-bit multiply instructions.
951
  void imull(Register dst, Register src);                 // dst = dst * src.
952
  void imull(Register dst, const Operand& src);           // dst = dst * src.
953
  void imull(Register dst, Register src, Immediate imm);  // dst = src * imm.
954

    
955
  void incq(Register dst);
956
  void incq(const Operand& dst);
957
  void incl(Register dst);
958
  void incl(const Operand& dst);
959

    
960
  void lea(Register dst, const Operand& src);
961
  void leal(Register dst, const Operand& src);
962

    
963
  // Multiply rax by src, put the result in rdx:rax.
964
  void mul(Register src);
965

    
966
  void neg(Register dst);
967
  void neg(const Operand& dst);
968
  void negl(Register dst);
969

    
970
  void not_(Register dst);
971
  void not_(const Operand& dst);
972
  void notl(Register dst);
973

    
974
  void or_(Register dst, Register src) {
975
    arithmetic_op(0x0B, dst, src);
976
  }
977

    
978
  void orl(Register dst, Register src) {
979
    arithmetic_op_32(0x0B, dst, src);
980
  }
981

    
982
  void or_(Register dst, const Operand& src) {
983
    arithmetic_op(0x0B, dst, src);
984
  }
985

    
986
  void orl(Register dst, const Operand& src) {
987
    arithmetic_op_32(0x0B, dst, src);
988
  }
989

    
990
  void or_(const Operand& dst, Register src) {
991
    arithmetic_op(0x09, src, dst);
992
  }
993

    
994
  void orl(const Operand& dst, Register src) {
995
    arithmetic_op_32(0x09, src, dst);
996
  }
997

    
998
  void or_(Register dst, Immediate src) {
999
    immediate_arithmetic_op(0x1, dst, src);
1000
  }
1001

    
1002
  void orl(Register dst, Immediate src) {
1003
    immediate_arithmetic_op_32(0x1, dst, src);
1004
  }
1005

    
1006
  void or_(const Operand& dst, Immediate src) {
1007
    immediate_arithmetic_op(0x1, dst, src);
1008
  }
1009

    
1010
  void orl(const Operand& dst, Immediate src) {
1011
    immediate_arithmetic_op_32(0x1, dst, src);
1012
  }
1013

    
1014

    
1015
  void rcl(Register dst, Immediate imm8) {
1016
    shift(dst, imm8, 0x2);
1017
  }
1018

    
1019
  void rol(Register dst, Immediate imm8) {
1020
    shift(dst, imm8, 0x0);
1021
  }
1022

    
1023
  void roll(Register dst, Immediate imm8) {
1024
    shift_32(dst, imm8, 0x0);
1025
  }
1026

    
1027
  void rcr(Register dst, Immediate imm8) {
1028
    shift(dst, imm8, 0x3);
1029
  }
1030

    
1031
  void ror(Register dst, Immediate imm8) {
1032
    shift(dst, imm8, 0x1);
1033
  }
1034

    
1035
  void rorl(Register dst, Immediate imm8) {
1036
    shift_32(dst, imm8, 0x1);
1037
  }
1038

    
1039
  void rorl_cl(Register dst) {
1040
    shift_32(dst, 0x1);
1041
  }
1042

    
1043
  // Shifts dst:src left by cl bits, affecting only dst.
1044
  void shld(Register dst, Register src);
1045

    
1046
  // Shifts src:dst right by cl bits, affecting only dst.
1047
  void shrd(Register dst, Register src);
1048

    
1049
  // Shifts dst right, duplicating sign bit, by shift_amount bits.
1050
  // Shifting by 1 is handled efficiently.
1051
  void sar(Register dst, Immediate shift_amount) {
1052
    shift(dst, shift_amount, 0x7);
1053
  }
1054

    
1055
  // Shifts dst right, duplicating sign bit, by shift_amount bits.
1056
  // Shifting by 1 is handled efficiently.
1057
  void sarl(Register dst, Immediate shift_amount) {
1058
    shift_32(dst, shift_amount, 0x7);
1059
  }
1060

    
1061
  // Shifts dst right, duplicating sign bit, by cl % 64 bits.
1062
  void sar_cl(Register dst) {
1063
    shift(dst, 0x7);
1064
  }
1065

    
1066
  // Shifts dst right, duplicating sign bit, by cl % 64 bits.
1067
  void sarl_cl(Register dst) {
1068
    shift_32(dst, 0x7);
1069
  }
1070

    
1071
  void shl(Register dst, Immediate shift_amount) {
1072
    shift(dst, shift_amount, 0x4);
1073
  }
1074

    
1075
  void shl_cl(Register dst) {
1076
    shift(dst, 0x4);
1077
  }
1078

    
1079
  void shll_cl(Register dst) {
1080
    shift_32(dst, 0x4);
1081
  }
1082

    
1083
  void shll(Register dst, Immediate shift_amount) {
1084
    shift_32(dst, shift_amount, 0x4);
1085
  }
1086

    
1087
  void shr(Register dst, Immediate shift_amount) {
1088
    shift(dst, shift_amount, 0x5);
1089
  }
1090

    
1091
  void shr_cl(Register dst) {
1092
    shift(dst, 0x5);
1093
  }
1094

    
1095
  void shrl_cl(Register dst) {
1096
    shift_32(dst, 0x5);
1097
  }
1098

    
1099
  void shrl(Register dst, Immediate shift_amount) {
1100
    shift_32(dst, shift_amount, 0x5);
1101
  }
1102

    
1103
  void store_rax(void* dst, RelocInfo::Mode mode);
1104
  void store_rax(ExternalReference ref);
1105

    
1106
  void subq(Register dst, Register src) {
1107
    arithmetic_op(0x2B, dst, src);
1108
  }
1109

    
1110
  void subq(Register dst, const Operand& src) {
1111
    arithmetic_op(0x2B, dst, src);
1112
  }
1113

    
1114
  void subq(const Operand& dst, Register src) {
1115
    arithmetic_op(0x29, src, dst);
1116
  }
1117

    
1118
  void subq(Register dst, Immediate src) {
1119
    immediate_arithmetic_op(0x5, dst, src);
1120
  }
1121

    
1122
  void subq(const Operand& dst, Immediate src) {
1123
    immediate_arithmetic_op(0x5, dst, src);
1124
  }
1125

    
1126
  void subl(Register dst, Register src) {
1127
    arithmetic_op_32(0x2B, dst, src);
1128
  }
1129

    
1130
  void subl(Register dst, const Operand& src) {
1131
    arithmetic_op_32(0x2B, dst, src);
1132
  }
1133

    
1134
  void subl(const Operand& dst, Register src) {
1135
    arithmetic_op_32(0x29, src, dst);
1136
  }
1137

    
1138
  void subl(const Operand& dst, Immediate src) {
1139
    immediate_arithmetic_op_32(0x5, dst, src);
1140
  }
1141

    
1142
  void subl(Register dst, Immediate src) {
1143
    immediate_arithmetic_op_32(0x5, dst, src);
1144
  }
1145

    
1146
  void subb(Register dst, Immediate src) {
1147
    immediate_arithmetic_op_8(0x5, dst, src);
1148
  }
1149

    
1150
  void testb(Register dst, Register src);
1151
  void testb(Register reg, Immediate mask);
1152
  void testb(const Operand& op, Immediate mask);
1153
  void testb(const Operand& op, Register reg);
1154
  void testl(Register dst, Register src);
1155
  void testl(Register reg, Immediate mask);
1156
  void testl(const Operand& op, Register reg);
1157
  void testl(const Operand& op, Immediate mask);
1158
  void testq(const Operand& op, Register reg);
1159
  void testq(Register dst, Register src);
1160
  void testq(Register dst, Immediate mask);
1161

    
1162
  void xor_(Register dst, Register src) {
1163
    if (dst.code() == src.code()) {
1164
      arithmetic_op_32(0x33, dst, src);
1165
    } else {
1166
      arithmetic_op(0x33, dst, src);
1167
    }
1168
  }
1169

    
1170
  void xorl(Register dst, Register src) {
1171
    arithmetic_op_32(0x33, dst, src);
1172
  }
1173

    
1174
  void xorl(Register dst, const Operand& src) {
1175
    arithmetic_op_32(0x33, dst, src);
1176
  }
1177

    
1178
  void xorl(Register dst, Immediate src) {
1179
    immediate_arithmetic_op_32(0x6, dst, src);
1180
  }
1181

    
1182
  void xorl(const Operand& dst, Register src) {
1183
    arithmetic_op_32(0x31, src, dst);
1184
  }
1185

    
1186
  void xorl(const Operand& dst, Immediate src) {
1187
    immediate_arithmetic_op_32(0x6, dst, src);
1188
  }
1189

    
1190
  void xor_(Register dst, const Operand& src) {
1191
    arithmetic_op(0x33, dst, src);
1192
  }
1193

    
1194
  void xor_(const Operand& dst, Register src) {
1195
    arithmetic_op(0x31, src, dst);
1196
  }
1197

    
1198
  void xor_(Register dst, Immediate src) {
1199
    immediate_arithmetic_op(0x6, dst, src);
1200
  }
1201

    
1202
  void xor_(const Operand& dst, Immediate src) {
1203
    immediate_arithmetic_op(0x6, dst, src);
1204
  }
1205

    
1206
  // Bit operations.
1207
  void bt(const Operand& dst, Register src);
1208
  void bts(const Operand& dst, Register src);
1209

    
1210
  // Miscellaneous
1211
  void clc();
1212
  void cld();
1213
  void cpuid();
1214
  void hlt();
1215
  void int3();
1216
  void nop();
1217
  void ret(int imm16);
1218
  void setcc(Condition cc, Register reg);
1219

    
1220
  // Label operations & relative jumps (PPUM Appendix D)
1221
  //
1222
  // Takes a branch opcode (cc) and a label (L) and generates
1223
  // either a backward branch or a forward branch and links it
1224
  // to the label fixup chain. Usage:
1225
  //
1226
  // Label L;    // unbound label
1227
  // j(cc, &L);  // forward branch to unbound label
1228
  // bind(&L);   // bind label to the current pc
1229
  // j(cc, &L);  // backward branch to bound label
1230
  // bind(&L);   // illegal: a label may be bound only once
1231
  //
1232
  // Note: The same Label can be used for forward and backward branches
1233
  // but it may be bound only once.
1234

    
1235
  void bind(Label* L);  // binds an unbound label L to the current code position
1236

    
1237
  // Calls
1238
  // Call near relative 32-bit displacement, relative to next instruction.
1239
  void call(Label* L);
1240
  void call(Address entry, RelocInfo::Mode rmode);
1241
  void call(Handle<Code> target,
1242
            RelocInfo::Mode rmode = RelocInfo::CODE_TARGET,
1243
            TypeFeedbackId ast_id = TypeFeedbackId::None());
1244

    
1245
  // Calls directly to the given address using a relative offset.
1246
  // Should only ever be used in Code objects for calls within the
1247
  // same Code object. Should not be used when generating new code (use labels),
1248
  // but only when patching existing code.
1249
  void call(Address target);
1250

    
1251
  // Call near absolute indirect, address in register
1252
  void call(Register adr);
1253

    
1254
  // Call near indirect
1255
  void call(const Operand& operand);
1256

    
1257
  // Jumps
1258
  // Jump short or near relative.
1259
  // Use a 32-bit signed displacement.
1260
  // Unconditional jump to L
1261
  void jmp(Label* L, Label::Distance distance = Label::kFar);
1262
  void jmp(Address entry, RelocInfo::Mode rmode);
1263
  void jmp(Handle<Code> target, RelocInfo::Mode rmode);
1264

    
1265
  // Jump near absolute indirect (r64)
1266
  void jmp(Register adr);
1267

    
1268
  // Jump near absolute indirect (m64)
1269
  void jmp(const Operand& src);
1270

    
1271
  // Conditional jumps
1272
  void j(Condition cc,
1273
         Label* L,
1274
         Label::Distance distance = Label::kFar);
1275
  void j(Condition cc, Address entry, RelocInfo::Mode rmode);
1276
  void j(Condition cc, Handle<Code> target, RelocInfo::Mode rmode);
1277

    
1278
  // Floating-point operations
1279
  void fld(int i);
1280

    
1281
  void fld1();
1282
  void fldz();
1283
  void fldpi();
1284
  void fldln2();
1285

    
1286
  void fld_s(const Operand& adr);
1287
  void fld_d(const Operand& adr);
1288

    
1289
  void fstp_s(const Operand& adr);
1290
  void fstp_d(const Operand& adr);
1291
  void fstp(int index);
1292

    
1293
  void fild_s(const Operand& adr);
1294
  void fild_d(const Operand& adr);
1295

    
1296
  void fist_s(const Operand& adr);
1297

    
1298
  void fistp_s(const Operand& adr);
1299
  void fistp_d(const Operand& adr);
1300

    
1301
  void fisttp_s(const Operand& adr);
1302
  void fisttp_d(const Operand& adr);
1303

    
1304
  void fabs();
1305
  void fchs();
1306

    
1307
  void fadd(int i);
1308
  void fsub(int i);
1309
  void fmul(int i);
1310
  void fdiv(int i);
1311

    
1312
  void fisub_s(const Operand& adr);
1313

    
1314
  void faddp(int i = 1);
1315
  void fsubp(int i = 1);
1316
  void fsubrp(int i = 1);
1317
  void fmulp(int i = 1);
1318
  void fdivp(int i = 1);
1319
  void fprem();
1320
  void fprem1();
1321

    
1322
  void fxch(int i = 1);
1323
  void fincstp();
1324
  void ffree(int i = 0);
1325

    
1326
  void ftst();
1327
  void fucomp(int i);
1328
  void fucompp();
1329
  void fucomi(int i);
1330
  void fucomip();
1331

    
1332
  void fcompp();
1333
  void fnstsw_ax();
1334
  void fwait();
1335
  void fnclex();
1336

    
1337
  void fsin();
1338
  void fcos();
1339
  void fptan();
1340
  void fyl2x();
1341
  void f2xm1();
1342
  void fscale();
1343
  void fninit();
1344

    
1345
  void frndint();
1346

    
1347
  void sahf();
1348

    
1349
  // SSE instructions
1350
  void movaps(XMMRegister dst, XMMRegister src);
1351
  void movss(XMMRegister dst, const Operand& src);
1352
  void movss(const Operand& dst, XMMRegister src);
1353

    
1354
  void cvttss2si(Register dst, const Operand& src);
1355
  void cvttss2si(Register dst, XMMRegister src);
1356
  void cvtlsi2ss(XMMRegister dst, Register src);
1357

    
1358
  void xorps(XMMRegister dst, XMMRegister src);
1359
  void andps(XMMRegister dst, XMMRegister src);
1360

    
1361
  void movmskps(Register dst, XMMRegister src);
1362

    
1363
  // SSE2 instructions
1364
  void movd(XMMRegister dst, Register src);
1365
  void movd(Register dst, XMMRegister src);
1366
  void movq(XMMRegister dst, Register src);
1367
  void movq(Register dst, XMMRegister src);
1368
  void movq(XMMRegister dst, XMMRegister src);
1369

    
1370
  // Don't use this unless it's important to keep the
1371
  // top half of the destination register unchanged.
1372
  // Used movaps when moving double values and movq for integer
1373
  // values in xmm registers.
1374
  void movsd(XMMRegister dst, XMMRegister src);
1375

    
1376
  void movsd(const Operand& dst, XMMRegister src);
1377
  void movsd(XMMRegister dst, const Operand& src);
1378

    
1379
  void movdqa(const Operand& dst, XMMRegister src);
1380
  void movdqa(XMMRegister dst, const Operand& src);
1381

    
1382
  void movdqu(const Operand& dst, XMMRegister src);
1383
  void movdqu(XMMRegister dst, const Operand& src);
1384

    
1385
  void movapd(XMMRegister dst, XMMRegister src);
1386

    
1387
  void cvttsd2si(Register dst, const Operand& src);
1388
  void cvttsd2si(Register dst, XMMRegister src);
1389
  void cvttsd2siq(Register dst, XMMRegister src);
1390

    
1391
  void cvtlsi2sd(XMMRegister dst, const Operand& src);
1392
  void cvtlsi2sd(XMMRegister dst, Register src);
1393
  void cvtqsi2sd(XMMRegister dst, const Operand& src);
1394
  void cvtqsi2sd(XMMRegister dst, Register src);
1395

    
1396

    
1397
  void cvtss2sd(XMMRegister dst, XMMRegister src);
1398
  void cvtss2sd(XMMRegister dst, const Operand& src);
1399
  void cvtsd2ss(XMMRegister dst, XMMRegister src);
1400

    
1401
  void cvtsd2si(Register dst, XMMRegister src);
1402
  void cvtsd2siq(Register dst, XMMRegister src);
1403

    
1404
  void addsd(XMMRegister dst, XMMRegister src);
1405
  void addsd(XMMRegister dst, const Operand& src);
1406
  void subsd(XMMRegister dst, XMMRegister src);
1407
  void mulsd(XMMRegister dst, XMMRegister src);
1408
  void mulsd(XMMRegister dst, const Operand& src);
1409
  void divsd(XMMRegister dst, XMMRegister src);
1410

    
1411
  void andpd(XMMRegister dst, XMMRegister src);
1412
  void orpd(XMMRegister dst, XMMRegister src);
1413
  void xorpd(XMMRegister dst, XMMRegister src);
1414
  void sqrtsd(XMMRegister dst, XMMRegister src);
1415

    
1416
  void ucomisd(XMMRegister dst, XMMRegister src);
1417
  void ucomisd(XMMRegister dst, const Operand& src);
1418
  void cmpltsd(XMMRegister dst, XMMRegister src);
1419

    
1420
  void movmskpd(Register dst, XMMRegister src);
1421

    
1422
  // SSE 4.1 instruction
1423
  void extractps(Register dst, XMMRegister src, byte imm8);
1424

    
1425
  enum RoundingMode {
1426
    kRoundToNearest = 0x0,
1427
    kRoundDown      = 0x1,
1428
    kRoundUp        = 0x2,
1429
    kRoundToZero    = 0x3
1430
  };
1431

    
1432
  void roundsd(XMMRegister dst, XMMRegister src, RoundingMode mode);
1433

    
1434
  // Debugging
1435
  void Print();
1436

    
1437
  // Check the code size generated from label to here.
1438
  int SizeOfCodeGeneratedSince(Label* label) {
1439
    return pc_offset() - label->pos();
1440
  }
1441

    
1442
  // Mark address of the ExitJSFrame code.
1443
  void RecordJSReturn();
1444

    
1445
  // Mark address of a debug break slot.
1446
  void RecordDebugBreakSlot();
1447

    
1448
  // Record a comment relocation entry that can be used by a disassembler.
1449
  // Use --code-comments to enable.
1450
  void RecordComment(const char* msg, bool force = false);
1451

    
1452
  // Writes a single word of data in the code stream.
1453
  // Used for inline tables, e.g., jump-tables.
1454
  void db(uint8_t data);
1455
  void dd(uint32_t data);
1456

    
1457
  PositionsRecorder* positions_recorder() { return &positions_recorder_; }
1458

    
1459
  // Check if there is less than kGap bytes available in the buffer.
1460
  // If this is the case, we need to grow the buffer before emitting
1461
  // an instruction or relocation information.
1462
  inline bool buffer_overflow() const {
1463
    return pc_ >= reloc_info_writer.pos() - kGap;
1464
  }
1465

    
1466
  // Get the number of bytes available in the buffer.
1467
  inline int available_space() const {
1468
    return static_cast<int>(reloc_info_writer.pos() - pc_);
1469
  }
1470

    
1471
  static bool IsNop(Address addr);
1472

    
1473
  // Avoid overflows for displacements etc.
1474
  static const int kMaximalBufferSize = 512*MB;
1475

    
1476
  byte byte_at(int pos)  { return buffer_[pos]; }
1477
  void set_byte_at(int pos, byte value) { buffer_[pos] = value; }
1478

    
1479
 private:
1480
  byte* addr_at(int pos)  { return buffer_ + pos; }
1481
  uint32_t long_at(int pos)  {
1482
    return *reinterpret_cast<uint32_t*>(addr_at(pos));
1483
  }
1484
  void long_at_put(int pos, uint32_t x)  {
1485
    *reinterpret_cast<uint32_t*>(addr_at(pos)) = x;
1486
  }
1487

    
1488
  // code emission
1489
  void GrowBuffer();
1490

    
1491
  void emit(byte x) { *pc_++ = x; }
1492
  inline void emitl(uint32_t x);
1493
  inline void emitp(void* x, RelocInfo::Mode rmode);
1494
  inline void emitq(uint64_t x);
1495
  inline void emitw(uint16_t x);
1496
  inline void emit_code_target(Handle<Code> target,
1497
                               RelocInfo::Mode rmode,
1498
                               TypeFeedbackId ast_id = TypeFeedbackId::None());
1499
  inline void emit_runtime_entry(Address entry, RelocInfo::Mode rmode);
1500
  void emit(Immediate x) { emitl(x.value_); }
1501

    
1502
  // Emits a REX prefix that encodes a 64-bit operand size and
1503
  // the top bit of both register codes.
1504
  // High bit of reg goes to REX.R, high bit of rm_reg goes to REX.B.
1505
  // REX.W is set.
1506
  inline void emit_rex_64(XMMRegister reg, Register rm_reg);
1507
  inline void emit_rex_64(Register reg, XMMRegister rm_reg);
1508
  inline void emit_rex_64(Register reg, Register rm_reg);
1509

    
1510
  // Emits a REX prefix that encodes a 64-bit operand size and
1511
  // the top bit of the destination, index, and base register codes.
1512
  // The high bit of reg is used for REX.R, the high bit of op's base
1513
  // register is used for REX.B, and the high bit of op's index register
1514
  // is used for REX.X.  REX.W is set.
1515
  inline void emit_rex_64(Register reg, const Operand& op);
1516
  inline void emit_rex_64(XMMRegister reg, const Operand& op);
1517

    
1518
  // Emits a REX prefix that encodes a 64-bit operand size and
1519
  // the top bit of the register code.
1520
  // The high bit of register is used for REX.B.
1521
  // REX.W is set and REX.R and REX.X are clear.
1522
  inline void emit_rex_64(Register rm_reg);
1523

    
1524
  // Emits a REX prefix that encodes a 64-bit operand size and
1525
  // the top bit of the index and base register codes.
1526
  // The high bit of op's base register is used for REX.B, and the high
1527
  // bit of op's index register is used for REX.X.
1528
  // REX.W is set and REX.R clear.
1529
  inline void emit_rex_64(const Operand& op);
1530

    
1531
  // Emit a REX prefix that only sets REX.W to choose a 64-bit operand size.
1532
  void emit_rex_64() { emit(0x48); }
1533

    
1534
  // High bit of reg goes to REX.R, high bit of rm_reg goes to REX.B.
1535
  // REX.W is clear.
1536
  inline void emit_rex_32(Register reg, Register rm_reg);
1537

    
1538
  // The high bit of reg is used for REX.R, the high bit of op's base
1539
  // register is used for REX.B, and the high bit of op's index register
1540
  // is used for REX.X.  REX.W is cleared.
1541
  inline void emit_rex_32(Register reg, const Operand& op);
1542

    
1543
  // High bit of rm_reg goes to REX.B.
1544
  // REX.W, REX.R and REX.X are clear.
1545
  inline void emit_rex_32(Register rm_reg);
1546

    
1547
  // High bit of base goes to REX.B and high bit of index to REX.X.
1548
  // REX.W and REX.R are clear.
1549
  inline void emit_rex_32(const Operand& op);
1550

    
1551
  // High bit of reg goes to REX.R, high bit of rm_reg goes to REX.B.
1552
  // REX.W is cleared.  If no REX bits are set, no byte is emitted.
1553
  inline void emit_optional_rex_32(Register reg, Register rm_reg);
1554

    
1555
  // The high bit of reg is used for REX.R, the high bit of op's base
1556
  // register is used for REX.B, and the high bit of op's index register
1557
  // is used for REX.X.  REX.W is cleared.  If no REX bits are set, nothing
1558
  // is emitted.
1559
  inline void emit_optional_rex_32(Register reg, const Operand& op);
1560

    
1561
  // As for emit_optional_rex_32(Register, Register), except that
1562
  // the registers are XMM registers.
1563
  inline void emit_optional_rex_32(XMMRegister reg, XMMRegister base);
1564

    
1565
  // As for emit_optional_rex_32(Register, Register), except that
1566
  // one of the registers is an XMM registers.
1567
  inline void emit_optional_rex_32(XMMRegister reg, Register base);
1568

    
1569
  // As for emit_optional_rex_32(Register, Register), except that
1570
  // one of the registers is an XMM registers.
1571
  inline void emit_optional_rex_32(Register reg, XMMRegister base);
1572

    
1573
  // As for emit_optional_rex_32(Register, const Operand&), except that
1574
  // the register is an XMM register.
1575
  inline void emit_optional_rex_32(XMMRegister reg, const Operand& op);
1576

    
1577
  // Optionally do as emit_rex_32(Register) if the register number has
1578
  // the high bit set.
1579
  inline void emit_optional_rex_32(Register rm_reg);
1580

    
1581
  // Optionally do as emit_rex_32(const Operand&) if the operand register
1582
  // numbers have a high bit set.
1583
  inline void emit_optional_rex_32(const Operand& op);
1584

    
1585

    
1586
  // Emit the ModR/M byte, and optionally the SIB byte and
1587
  // 1- or 4-byte offset for a memory operand.  Also encodes
1588
  // the second operand of the operation, a register or operation
1589
  // subcode, into the reg field of the ModR/M byte.
1590
  void emit_operand(Register reg, const Operand& adr) {
1591
    emit_operand(reg.low_bits(), adr);
1592
  }
1593

    
1594
  // Emit the ModR/M byte, and optionally the SIB byte and
1595
  // 1- or 4-byte offset for a memory operand.  Also used to encode
1596
  // a three-bit opcode extension into the ModR/M byte.
1597
  void emit_operand(int rm, const Operand& adr);
1598

    
1599
  // Emit a ModR/M byte with registers coded in the reg and rm_reg fields.
1600
  void emit_modrm(Register reg, Register rm_reg) {
1601
    emit(0xC0 | reg.low_bits() << 3 | rm_reg.low_bits());
1602
  }
1603

    
1604
  // Emit a ModR/M byte with an operation subcode in the reg field and
1605
  // a register in the rm_reg field.
1606
  void emit_modrm(int code, Register rm_reg) {
1607
    ASSERT(is_uint3(code));
1608
    emit(0xC0 | code << 3 | rm_reg.low_bits());
1609
  }
1610

    
1611
  // Emit the code-object-relative offset of the label's position
1612
  inline void emit_code_relative_offset(Label* label);
1613

    
1614
  // The first argument is the reg field, the second argument is the r/m field.
1615
  void emit_sse_operand(XMMRegister dst, XMMRegister src);
1616
  void emit_sse_operand(XMMRegister reg, const Operand& adr);
1617
  void emit_sse_operand(XMMRegister dst, Register src);
1618
  void emit_sse_operand(Register dst, XMMRegister src);
1619

    
1620
  // Emit machine code for one of the operations ADD, ADC, SUB, SBC,
1621
  // AND, OR, XOR, or CMP.  The encodings of these operations are all
1622
  // similar, differing just in the opcode or in the reg field of the
1623
  // ModR/M byte.
1624
  void arithmetic_op_16(byte opcode, Register reg, Register rm_reg);
1625
  void arithmetic_op_16(byte opcode, Register reg, const Operand& rm_reg);
1626
  void arithmetic_op_32(byte opcode, Register reg, Register rm_reg);
1627
  void arithmetic_op_32(byte opcode, Register reg, const Operand& rm_reg);
1628
  void arithmetic_op(byte opcode, Register reg, Register rm_reg);
1629
  void arithmetic_op(byte opcode, Register reg, const Operand& rm_reg);
1630
  void immediate_arithmetic_op(byte subcode, Register dst, Immediate src);
1631
  void immediate_arithmetic_op(byte subcode, const Operand& dst, Immediate src);
1632
  // Operate on a byte in memory or register.
1633
  void immediate_arithmetic_op_8(byte subcode,
1634
                                 Register dst,
1635
                                 Immediate src);
1636
  void immediate_arithmetic_op_8(byte subcode,
1637
                                 const Operand& dst,
1638
                                 Immediate src);
1639
  // Operate on a word in memory or register.
1640
  void immediate_arithmetic_op_16(byte subcode,
1641
                                  Register dst,
1642
                                  Immediate src);
1643
  void immediate_arithmetic_op_16(byte subcode,
1644
                                  const Operand& dst,
1645
                                  Immediate src);
1646
  // Operate on a 32-bit word in memory or register.
1647
  void immediate_arithmetic_op_32(byte subcode,
1648
                                  Register dst,
1649
                                  Immediate src);
1650
  void immediate_arithmetic_op_32(byte subcode,
1651
                                  const Operand& dst,
1652
                                  Immediate src);
1653

    
1654
  // Emit machine code for a shift operation.
1655
  void shift(Register dst, Immediate shift_amount, int subcode);
1656
  void shift_32(Register dst, Immediate shift_amount, int subcode);
1657
  // Shift dst by cl % 64 bits.
1658
  void shift(Register dst, int subcode);
1659
  void shift_32(Register dst, int subcode);
1660

    
1661
  void emit_farith(int b1, int b2, int i);
1662

    
1663
  // labels
1664
  // void print(Label* L);
1665
  void bind_to(Label* L, int pos);
1666

    
1667
  // record reloc info for current pc_
1668
  void RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data = 0);
1669

    
1670
  friend class CodePatcher;
1671
  friend class EnsureSpace;
1672
  friend class RegExpMacroAssemblerX64;
1673

    
1674
  // code generation
1675
  RelocInfoWriter reloc_info_writer;
1676

    
1677
  List< Handle<Code> > code_targets_;
1678

    
1679
  PositionsRecorder positions_recorder_;
1680
  friend class PositionsRecorder;
1681
};
1682

    
1683

    
1684
// Helper class that ensures that there is enough space for generating
1685
// instructions and relocation information.  The constructor makes
1686
// sure that there is enough space and (in debug mode) the destructor
1687
// checks that we did not generate too much.
1688
class EnsureSpace BASE_EMBEDDED {
1689
 public:
1690
  explicit EnsureSpace(Assembler* assembler) : assembler_(assembler) {
1691
    if (assembler_->buffer_overflow()) assembler_->GrowBuffer();
1692
#ifdef DEBUG
1693
    space_before_ = assembler_->available_space();
1694
#endif
1695
  }
1696

    
1697
#ifdef DEBUG
1698
  ~EnsureSpace() {
1699
    int bytes_generated = space_before_ - assembler_->available_space();
1700
    ASSERT(bytes_generated < assembler_->kGap);
1701
  }
1702
#endif
1703

    
1704
 private:
1705
  Assembler* assembler_;
1706
#ifdef DEBUG
1707
  int space_before_;
1708
#endif
1709
};
1710

    
1711
} }  // namespace v8::internal
1712

    
1713
#endif  // V8_X64_ASSEMBLER_X64_H_