1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_X64_MACRO_ASSEMBLER_X64_H_
6 #define V8_X64_MACRO_ASSEMBLER_X64_H_
7 
8 #include "src/assembler.h"
9 #include "src/bailout-reason.h"
10 #include "src/base/flags.h"
11 #include "src/frames.h"
12 #include "src/globals.h"
13 #include "src/x64/frames-x64.h"
14 
15 namespace v8 {
16 namespace internal {
17 
18 // Give alias names to registers for calling conventions.
19 const Register kReturnRegister0 = {Register::kCode_rax};
20 const Register kReturnRegister1 = {Register::kCode_rdx};
21 const Register kJSFunctionRegister = {Register::kCode_rdi};
22 const Register kContextRegister = {Register::kCode_rsi};
23 const Register kInterpreterAccumulatorRegister = {Register::kCode_rax};
24 const Register kInterpreterRegisterFileRegister = {Register::kCode_r11};
25 const Register kInterpreterBytecodeOffsetRegister = {Register::kCode_r12};
26 const Register kInterpreterBytecodeArrayRegister = {Register::kCode_r14};
27 const Register kInterpreterDispatchTableRegister = {Register::kCode_r15};
28 const Register kJavaScriptCallArgCountRegister = {Register::kCode_rax};
29 const Register kJavaScriptCallNewTargetRegister = {Register::kCode_rdx};
30 const Register kRuntimeCallFunctionRegister = {Register::kCode_rbx};
31 const Register kRuntimeCallArgCountRegister = {Register::kCode_rax};
32 
33 // Default scratch register used by MacroAssembler (and other code that needs
34 // a spare register). The register isn't callee save, and not used by the
35 // function calling convention.
36 const Register kScratchRegister = { 10 };      // r10.
37 const Register kRootRegister = { 13 };         // r13 (callee save).
38 // Actual value of root register is offset from the root array's start
39 // to take advantage of negitive 8-bit displacement values.
40 const int kRootRegisterBias = 128;
41 
42 // Convenience for platform-independent signatures.
43 typedef Operand MemOperand;
44 
45 enum RememberedSetAction { EMIT_REMEMBERED_SET, OMIT_REMEMBERED_SET };
46 enum SmiCheck { INLINE_SMI_CHECK, OMIT_SMI_CHECK };
47 enum PointersToHereCheck {
48   kPointersToHereMaybeInteresting,
49   kPointersToHereAreAlwaysInteresting
50 };
51 
52 enum class SmiOperationConstraint {
53   kPreserveSourceRegister = 1 << 0,
54   kBailoutOnNoOverflow = 1 << 1,
55   kBailoutOnOverflow = 1 << 2
56 };
57 
58 typedef base::Flags<SmiOperationConstraint> SmiOperationConstraints;
59 
60 DEFINE_OPERATORS_FOR_FLAGS(SmiOperationConstraints)
61 
62 #ifdef DEBUG
63 bool AreAliased(Register reg1,
64                 Register reg2,
65                 Register reg3 = no_reg,
66                 Register reg4 = no_reg,
67                 Register reg5 = no_reg,
68                 Register reg6 = no_reg,
69                 Register reg7 = no_reg,
70                 Register reg8 = no_reg);
71 #endif
72 
73 // Forward declaration.
74 class JumpTarget;
75 
76 struct SmiIndex {
SmiIndexSmiIndex77   SmiIndex(Register index_register, ScaleFactor scale)
78       : reg(index_register),
79         scale(scale) {}
80   Register reg;
81   ScaleFactor scale;
82 };
83 
84 
85 // MacroAssembler implements a collection of frequently used macros.
86 class MacroAssembler: public Assembler {
87  public:
88   MacroAssembler(Isolate* isolate, void* buffer, int size,
89                  CodeObjectRequired create_code_object);
90 
91   // Prevent the use of the RootArray during the lifetime of this
92   // scope object.
93   class NoRootArrayScope BASE_EMBEDDED {
94    public:
NoRootArrayScope(MacroAssembler * assembler)95     explicit NoRootArrayScope(MacroAssembler* assembler)
96         : variable_(&assembler->root_array_available_),
97           old_value_(assembler->root_array_available_) {
98       assembler->root_array_available_ = false;
99     }
~NoRootArrayScope()100     ~NoRootArrayScope() {
101       *variable_ = old_value_;
102     }
103    private:
104     bool* variable_;
105     bool old_value_;
106   };
107 
108   // Operand pointing to an external reference.
109   // May emit code to set up the scratch register. The operand is
110   // only guaranteed to be correct as long as the scratch register
111   // isn't changed.
112   // If the operand is used more than once, use a scratch register
113   // that is guaranteed not to be clobbered.
114   Operand ExternalOperand(ExternalReference reference,
115                           Register scratch = kScratchRegister);
116   // Loads and stores the value of an external reference.
117   // Special case code for load and store to take advantage of
118   // load_rax/store_rax if possible/necessary.
119   // For other operations, just use:
120   //   Operand operand = ExternalOperand(extref);
121   //   operation(operand, ..);
122   void Load(Register destination, ExternalReference source);
123   void Store(ExternalReference destination, Register source);
124   // Loads the address of the external reference into the destination
125   // register.
126   void LoadAddress(Register destination, ExternalReference source);
127   // Returns the size of the code generated by LoadAddress.
128   // Used by CallSize(ExternalReference) to find the size of a call.
129   int LoadAddressSize(ExternalReference source);
130   // Pushes the address of the external reference onto the stack.
131   void PushAddress(ExternalReference source);
132 
133   // Operations on roots in the root-array.
134   void LoadRoot(Register destination, Heap::RootListIndex index);
LoadRoot(const Operand & destination,Heap::RootListIndex index)135   void LoadRoot(const Operand& destination, Heap::RootListIndex index) {
136     LoadRoot(kScratchRegister, index);
137     movp(destination, kScratchRegister);
138   }
139   void StoreRoot(Register source, Heap::RootListIndex index);
140   // Load a root value where the index (or part of it) is variable.
141   // The variable_offset register is added to the fixed_offset value
142   // to get the index into the root-array.
143   void LoadRootIndexed(Register destination,
144                        Register variable_offset,
145                        int fixed_offset);
146   void CompareRoot(Register with, Heap::RootListIndex index);
147   void CompareRoot(const Operand& with, Heap::RootListIndex index);
148   void PushRoot(Heap::RootListIndex index);
149 
150   // Compare the object in a register to a value and jump if they are equal.
151   void JumpIfRoot(Register with, Heap::RootListIndex index, Label* if_equal,
152                   Label::Distance if_equal_distance = Label::kFar) {
153     CompareRoot(with, index);
154     j(equal, if_equal, if_equal_distance);
155   }
156   void JumpIfRoot(const Operand& with, Heap::RootListIndex index,
157                   Label* if_equal,
158                   Label::Distance if_equal_distance = Label::kFar) {
159     CompareRoot(with, index);
160     j(equal, if_equal, if_equal_distance);
161   }
162 
163   // Compare the object in a register to a value and jump if they are not equal.
164   void JumpIfNotRoot(Register with, Heap::RootListIndex index,
165                      Label* if_not_equal,
166                      Label::Distance if_not_equal_distance = Label::kFar) {
167     CompareRoot(with, index);
168     j(not_equal, if_not_equal, if_not_equal_distance);
169   }
170   void JumpIfNotRoot(const Operand& with, Heap::RootListIndex index,
171                      Label* if_not_equal,
172                      Label::Distance if_not_equal_distance = Label::kFar) {
173     CompareRoot(with, index);
174     j(not_equal, if_not_equal, if_not_equal_distance);
175   }
176 
177   // These functions do not arrange the registers in any particular order so
178   // they are not useful for calls that can cause a GC.  The caller can
179   // exclude up to 3 registers that do not need to be saved and restored.
180   void PushCallerSaved(SaveFPRegsMode fp_mode,
181                        Register exclusion1 = no_reg,
182                        Register exclusion2 = no_reg,
183                        Register exclusion3 = no_reg);
184   void PopCallerSaved(SaveFPRegsMode fp_mode,
185                       Register exclusion1 = no_reg,
186                       Register exclusion2 = no_reg,
187                       Register exclusion3 = no_reg);
188 
189 // ---------------------------------------------------------------------------
190 // GC Support
191 
192 
193   enum RememberedSetFinalAction {
194     kReturnAtEnd,
195     kFallThroughAtEnd
196   };
197 
198   // Record in the remembered set the fact that we have a pointer to new space
199   // at the address pointed to by the addr register.  Only works if addr is not
200   // in new space.
201   void RememberedSetHelper(Register object,  // Used for debug code.
202                            Register addr,
203                            Register scratch,
204                            SaveFPRegsMode save_fp,
205                            RememberedSetFinalAction and_then);
206 
207   void CheckPageFlag(Register object,
208                      Register scratch,
209                      int mask,
210                      Condition cc,
211                      Label* condition_met,
212                      Label::Distance condition_met_distance = Label::kFar);
213 
214   // Check if object is in new space.  Jumps if the object is not in new space.
215   // The register scratch can be object itself, but scratch will be clobbered.
216   void JumpIfNotInNewSpace(Register object,
217                            Register scratch,
218                            Label* branch,
219                            Label::Distance distance = Label::kFar) {
220     InNewSpace(object, scratch, not_equal, branch, distance);
221   }
222 
223   // Check if object is in new space.  Jumps if the object is in new space.
224   // The register scratch can be object itself, but it will be clobbered.
225   void JumpIfInNewSpace(Register object,
226                         Register scratch,
227                         Label* branch,
228                         Label::Distance distance = Label::kFar) {
229     InNewSpace(object, scratch, equal, branch, distance);
230   }
231 
232   // Check if an object has the black incremental marking color.  Also uses rcx!
233   void JumpIfBlack(Register object, Register bitmap_scratch,
234                    Register mask_scratch, Label* on_black,
235                    Label::Distance on_black_distance);
236 
237   // Checks the color of an object.  If the object is white we jump to the
238   // incremental marker.
239   void JumpIfWhite(Register value, Register scratch1, Register scratch2,
240                    Label* value_is_white, Label::Distance distance);
241 
242   // Notify the garbage collector that we wrote a pointer into an object.
243   // |object| is the object being stored into, |value| is the object being
244   // stored.  value and scratch registers are clobbered by the operation.
245   // The offset is the offset from the start of the object, not the offset from
246   // the tagged HeapObject pointer.  For use with FieldOperand(reg, off).
247   void RecordWriteField(
248       Register object,
249       int offset,
250       Register value,
251       Register scratch,
252       SaveFPRegsMode save_fp,
253       RememberedSetAction remembered_set_action = EMIT_REMEMBERED_SET,
254       SmiCheck smi_check = INLINE_SMI_CHECK,
255       PointersToHereCheck pointers_to_here_check_for_value =
256           kPointersToHereMaybeInteresting);
257 
258   // As above, but the offset has the tag presubtracted.  For use with
259   // Operand(reg, off).
260   void RecordWriteContextSlot(
261       Register context,
262       int offset,
263       Register value,
264       Register scratch,
265       SaveFPRegsMode save_fp,
266       RememberedSetAction remembered_set_action = EMIT_REMEMBERED_SET,
267       SmiCheck smi_check = INLINE_SMI_CHECK,
268       PointersToHereCheck pointers_to_here_check_for_value =
269           kPointersToHereMaybeInteresting) {
270     RecordWriteField(context,
271                      offset + kHeapObjectTag,
272                      value,
273                      scratch,
274                      save_fp,
275                      remembered_set_action,
276                      smi_check,
277                      pointers_to_here_check_for_value);
278   }
279 
280   // Notify the garbage collector that we wrote a pointer into a fixed array.
281   // |array| is the array being stored into, |value| is the
282   // object being stored.  |index| is the array index represented as a non-smi.
283   // All registers are clobbered by the operation RecordWriteArray
284   // filters out smis so it does not update the write barrier if the
285   // value is a smi.
286   void RecordWriteArray(
287       Register array,
288       Register value,
289       Register index,
290       SaveFPRegsMode save_fp,
291       RememberedSetAction remembered_set_action = EMIT_REMEMBERED_SET,
292       SmiCheck smi_check = INLINE_SMI_CHECK,
293       PointersToHereCheck pointers_to_here_check_for_value =
294           kPointersToHereMaybeInteresting);
295 
296   void RecordWriteForMap(
297       Register object,
298       Register map,
299       Register dst,
300       SaveFPRegsMode save_fp);
301 
302   // For page containing |object| mark region covering |address|
303   // dirty. |object| is the object being stored into, |value| is the
304   // object being stored. The address and value registers are clobbered by the
305   // operation.  RecordWrite filters out smis so it does not update
306   // the write barrier if the value is a smi.
307   void RecordWrite(
308       Register object,
309       Register address,
310       Register value,
311       SaveFPRegsMode save_fp,
312       RememberedSetAction remembered_set_action = EMIT_REMEMBERED_SET,
313       SmiCheck smi_check = INLINE_SMI_CHECK,
314       PointersToHereCheck pointers_to_here_check_for_value =
315           kPointersToHereMaybeInteresting);
316 
317   // ---------------------------------------------------------------------------
318   // Debugger Support
319 
320   void DebugBreak();
321 
322   // Generates function and stub prologue code.
323   void StubPrologue();
324   void Prologue(bool code_pre_aging);
325 
326   // Enter specific kind of exit frame; either in normal or
327   // debug mode. Expects the number of arguments in register rax and
328   // sets up the number of arguments in register rdi and the pointer
329   // to the first argument in register rsi.
330   //
331   // Allocates arg_stack_space * kPointerSize memory (not GCed) on the stack
332   // accessible via StackSpaceOperand.
333   void EnterExitFrame(int arg_stack_space = 0, bool save_doubles = false);
334 
335   // Enter specific kind of exit frame. Allocates arg_stack_space * kPointerSize
336   // memory (not GCed) on the stack accessible via StackSpaceOperand.
337   void EnterApiExitFrame(int arg_stack_space);
338 
339   // Leave the current exit frame. Expects/provides the return value in
340   // register rax:rdx (untouched) and the pointer to the first
341   // argument in register rsi (if pop_arguments == true).
342   void LeaveExitFrame(bool save_doubles = false, bool pop_arguments = true);
343 
344   // Leave the current exit frame. Expects/provides the return value in
345   // register rax (untouched).
346   void LeaveApiExitFrame(bool restore_context);
347 
348   // Push and pop the registers that can hold pointers.
PushSafepointRegisters()349   void PushSafepointRegisters() { Pushad(); }
PopSafepointRegisters()350   void PopSafepointRegisters() { Popad(); }
351   // Store the value in register src in the safepoint register stack
352   // slot for register dst.
353   void StoreToSafepointRegisterSlot(Register dst, const Immediate& imm);
354   void StoreToSafepointRegisterSlot(Register dst, Register src);
355   void LoadFromSafepointRegisterSlot(Register dst, Register src);
356 
InitializeRootRegister()357   void InitializeRootRegister() {
358     ExternalReference roots_array_start =
359         ExternalReference::roots_array_start(isolate());
360     Move(kRootRegister, roots_array_start);
361     addp(kRootRegister, Immediate(kRootRegisterBias));
362   }
363 
364   // ---------------------------------------------------------------------------
365   // JavaScript invokes
366 
367   // Invoke the JavaScript function code by either calling or jumping.
368   void InvokeFunctionCode(Register function, Register new_target,
369                           const ParameterCount& expected,
370                           const ParameterCount& actual, InvokeFlag flag,
371                           const CallWrapper& call_wrapper);
372 
373   void FloodFunctionIfStepping(Register fun, Register new_target,
374                                const ParameterCount& expected,
375                                const ParameterCount& actual);
376 
377   // Invoke the JavaScript function in the given register. Changes the
378   // current context to the context in the function before invoking.
379   void InvokeFunction(Register function,
380                       Register new_target,
381                       const ParameterCount& actual,
382                       InvokeFlag flag,
383                       const CallWrapper& call_wrapper);
384 
385   void InvokeFunction(Register function,
386                       Register new_target,
387                       const ParameterCount& expected,
388                       const ParameterCount& actual,
389                       InvokeFlag flag,
390                       const CallWrapper& call_wrapper);
391 
392   void InvokeFunction(Handle<JSFunction> function,
393                       const ParameterCount& expected,
394                       const ParameterCount& actual,
395                       InvokeFlag flag,
396                       const CallWrapper& call_wrapper);
397 
398   // Invoke specified builtin JavaScript function.
399   void InvokeBuiltin(int native_context_index, InvokeFlag flag,
400                      const CallWrapper& call_wrapper = NullCallWrapper());
401 
402   // ---------------------------------------------------------------------------
403   // Smi tagging, untagging and operations on tagged smis.
404 
405   // Support for constant splitting.
406   bool IsUnsafeInt(const int32_t x);
407   void SafeMove(Register dst, Smi* src);
408   void SafePush(Smi* src);
409 
410   // Conversions between tagged smi values and non-tagged integer values.
411 
412   // Tag an integer value. The result must be known to be a valid smi value.
413   // Only uses the low 32 bits of the src register. Sets the N and Z flags
414   // based on the value of the resulting smi.
415   void Integer32ToSmi(Register dst, Register src);
416 
417   // Stores an integer32 value into a memory field that already holds a smi.
418   void Integer32ToSmiField(const Operand& dst, Register src);
419 
420   // Adds constant to src and tags the result as a smi.
421   // Result must be a valid smi.
422   void Integer64PlusConstantToSmi(Register dst, Register src, int constant);
423 
424   // Convert smi to 32-bit integer. I.e., not sign extended into
425   // high 32 bits of destination.
426   void SmiToInteger32(Register dst, Register src);
427   void SmiToInteger32(Register dst, const Operand& src);
428 
429   // Convert smi to 64-bit integer (sign extended if necessary).
430   void SmiToInteger64(Register dst, Register src);
431   void SmiToInteger64(Register dst, const Operand& src);
432 
433   // Multiply a positive smi's integer value by a power of two.
434   // Provides result as 64-bit integer value.
435   void PositiveSmiTimesPowerOfTwoToInteger64(Register dst,
436                                              Register src,
437                                              int power);
438 
439   // Divide a positive smi's integer value by a power of two.
440   // Provides result as 32-bit integer value.
441   void PositiveSmiDivPowerOfTwoToInteger32(Register dst,
442                                            Register src,
443                                            int power);
444 
445   // Perform the logical or of two smi values and return a smi value.
446   // If either argument is not a smi, jump to on_not_smis and retain
447   // the original values of source registers. The destination register
448   // may be changed if it's not one of the source registers.
449   void SmiOrIfSmis(Register dst,
450                    Register src1,
451                    Register src2,
452                    Label* on_not_smis,
453                    Label::Distance near_jump = Label::kFar);
454 
455 
456   // Simple comparison of smis.  Both sides must be known smis to use these,
457   // otherwise use Cmp.
458   void SmiCompare(Register smi1, Register smi2);
459   void SmiCompare(Register dst, Smi* src);
460   void SmiCompare(Register dst, const Operand& src);
461   void SmiCompare(const Operand& dst, Register src);
462   void SmiCompare(const Operand& dst, Smi* src);
463   // Compare the int32 in src register to the value of the smi stored at dst.
464   void SmiCompareInteger32(const Operand& dst, Register src);
465   // Sets sign and zero flags depending on value of smi in register.
466   void SmiTest(Register src);
467 
468   // Functions performing a check on a known or potential smi. Returns
469   // a condition that is satisfied if the check is successful.
470 
471   // Is the value a tagged smi.
472   Condition CheckSmi(Register src);
473   Condition CheckSmi(const Operand& src);
474 
475   // Is the value a non-negative tagged smi.
476   Condition CheckNonNegativeSmi(Register src);
477 
478   // Are both values tagged smis.
479   Condition CheckBothSmi(Register first, Register second);
480 
481   // Are both values non-negative tagged smis.
482   Condition CheckBothNonNegativeSmi(Register first, Register second);
483 
484   // Are either value a tagged smi.
485   Condition CheckEitherSmi(Register first,
486                            Register second,
487                            Register scratch = kScratchRegister);
488 
489   // Checks whether an 32-bit integer value is a valid for conversion
490   // to a smi.
491   Condition CheckInteger32ValidSmiValue(Register src);
492 
493   // Checks whether an 32-bit unsigned integer value is a valid for
494   // conversion to a smi.
495   Condition CheckUInteger32ValidSmiValue(Register src);
496 
497   // Check whether src is a Smi, and set dst to zero if it is a smi,
498   // and to one if it isn't.
499   void CheckSmiToIndicator(Register dst, Register src);
500   void CheckSmiToIndicator(Register dst, const Operand& src);
501 
502   // Test-and-jump functions. Typically combines a check function
503   // above with a conditional jump.
504 
505   // Jump if the value can be represented by a smi.
506   void JumpIfValidSmiValue(Register src, Label* on_valid,
507                            Label::Distance near_jump = Label::kFar);
508 
509   // Jump if the value cannot be represented by a smi.
510   void JumpIfNotValidSmiValue(Register src, Label* on_invalid,
511                               Label::Distance near_jump = Label::kFar);
512 
513   // Jump if the unsigned integer value can be represented by a smi.
514   void JumpIfUIntValidSmiValue(Register src, Label* on_valid,
515                                Label::Distance near_jump = Label::kFar);
516 
517   // Jump if the unsigned integer value cannot be represented by a smi.
518   void JumpIfUIntNotValidSmiValue(Register src, Label* on_invalid,
519                                   Label::Distance near_jump = Label::kFar);
520 
521   // Jump to label if the value is a tagged smi.
522   void JumpIfSmi(Register src,
523                  Label* on_smi,
524                  Label::Distance near_jump = Label::kFar);
525 
526   // Jump to label if the value is not a tagged smi.
527   void JumpIfNotSmi(Register src,
528                     Label* on_not_smi,
529                     Label::Distance near_jump = Label::kFar);
530 
531   // Jump to label if the value is not a non-negative tagged smi.
532   void JumpUnlessNonNegativeSmi(Register src,
533                                 Label* on_not_smi,
534                                 Label::Distance near_jump = Label::kFar);
535 
536   // Jump to label if the value, which must be a tagged smi, has value equal
537   // to the constant.
538   void JumpIfSmiEqualsConstant(Register src,
539                                Smi* constant,
540                                Label* on_equals,
541                                Label::Distance near_jump = Label::kFar);
542 
543   // Jump if either or both register are not smi values.
544   void JumpIfNotBothSmi(Register src1,
545                         Register src2,
546                         Label* on_not_both_smi,
547                         Label::Distance near_jump = Label::kFar);
548 
549   // Jump if either or both register are not non-negative smi values.
550   void JumpUnlessBothNonNegativeSmi(Register src1, Register src2,
551                                     Label* on_not_both_smi,
552                                     Label::Distance near_jump = Label::kFar);
553 
554   // Operations on tagged smi values.
555 
556   // Smis represent a subset of integers. The subset is always equivalent to
557   // a two's complement interpretation of a fixed number of bits.
558 
559   // Add an integer constant to a tagged smi, giving a tagged smi as result.
560   // No overflow testing on the result is done.
561   void SmiAddConstant(Register dst, Register src, Smi* constant);
562 
563   // Add an integer constant to a tagged smi, giving a tagged smi as result.
564   // No overflow testing on the result is done.
565   void SmiAddConstant(const Operand& dst, Smi* constant);
566 
567   // Add an integer constant to a tagged smi, giving a tagged smi as result,
568   // or jumping to a label if the result cannot be represented by a smi.
569   void SmiAddConstant(Register dst, Register src, Smi* constant,
570                       SmiOperationConstraints constraints, Label* bailout_label,
571                       Label::Distance near_jump = Label::kFar);
572 
573   // Subtract an integer constant from a tagged smi, giving a tagged smi as
574   // result. No testing on the result is done. Sets the N and Z flags
575   // based on the value of the resulting integer.
576   void SmiSubConstant(Register dst, Register src, Smi* constant);
577 
578   // Subtract an integer constant from a tagged smi, giving a tagged smi as
579   // result, or jumping to a label if the result cannot be represented by a smi.
580   void SmiSubConstant(Register dst, Register src, Smi* constant,
581                       SmiOperationConstraints constraints, Label* bailout_label,
582                       Label::Distance near_jump = Label::kFar);
583 
584   // Negating a smi can give a negative zero or too large positive value.
585   // NOTICE: This operation jumps on success, not failure!
586   void SmiNeg(Register dst,
587               Register src,
588               Label* on_smi_result,
589               Label::Distance near_jump = Label::kFar);
590 
591   // Adds smi values and return the result as a smi.
592   // If dst is src1, then src1 will be destroyed if the operation is
593   // successful, otherwise kept intact.
594   void SmiAdd(Register dst,
595               Register src1,
596               Register src2,
597               Label* on_not_smi_result,
598               Label::Distance near_jump = Label::kFar);
599   void SmiAdd(Register dst,
600               Register src1,
601               const Operand& src2,
602               Label* on_not_smi_result,
603               Label::Distance near_jump = Label::kFar);
604 
605   void SmiAdd(Register dst,
606               Register src1,
607               Register src2);
608 
609   // Subtracts smi values and return the result as a smi.
610   // If dst is src1, then src1 will be destroyed if the operation is
611   // successful, otherwise kept intact.
612   void SmiSub(Register dst,
613               Register src1,
614               Register src2,
615               Label* on_not_smi_result,
616               Label::Distance near_jump = Label::kFar);
617   void SmiSub(Register dst,
618               Register src1,
619               const Operand& src2,
620               Label* on_not_smi_result,
621               Label::Distance near_jump = Label::kFar);
622 
623   void SmiSub(Register dst,
624               Register src1,
625               Register src2);
626 
627   void SmiSub(Register dst,
628               Register src1,
629               const Operand& src2);
630 
631   // Multiplies smi values and return the result as a smi,
632   // if possible.
633   // If dst is src1, then src1 will be destroyed, even if
634   // the operation is unsuccessful.
635   void SmiMul(Register dst,
636               Register src1,
637               Register src2,
638               Label* on_not_smi_result,
639               Label::Distance near_jump = Label::kFar);
640 
641   // Divides one smi by another and returns the quotient.
642   // Clobbers rax and rdx registers.
643   void SmiDiv(Register dst,
644               Register src1,
645               Register src2,
646               Label* on_not_smi_result,
647               Label::Distance near_jump = Label::kFar);
648 
649   // Divides one smi by another and returns the remainder.
650   // Clobbers rax and rdx registers.
651   void SmiMod(Register dst,
652               Register src1,
653               Register src2,
654               Label* on_not_smi_result,
655               Label::Distance near_jump = Label::kFar);
656 
657   // Bitwise operations.
658   void SmiNot(Register dst, Register src);
659   void SmiAnd(Register dst, Register src1, Register src2);
660   void SmiOr(Register dst, Register src1, Register src2);
661   void SmiXor(Register dst, Register src1, Register src2);
662   void SmiAndConstant(Register dst, Register src1, Smi* constant);
663   void SmiOrConstant(Register dst, Register src1, Smi* constant);
664   void SmiXorConstant(Register dst, Register src1, Smi* constant);
665 
666   void SmiShiftLeftConstant(Register dst,
667                             Register src,
668                             int shift_value,
669                             Label* on_not_smi_result = NULL,
670                             Label::Distance near_jump = Label::kFar);
671   void SmiShiftLogicalRightConstant(Register dst,
672                                     Register src,
673                                     int shift_value,
674                                     Label* on_not_smi_result,
675                                     Label::Distance near_jump = Label::kFar);
676   void SmiShiftArithmeticRightConstant(Register dst,
677                                        Register src,
678                                        int shift_value);
679 
680   // Shifts a smi value to the left, and returns the result if that is a smi.
681   // Uses and clobbers rcx, so dst may not be rcx.
682   void SmiShiftLeft(Register dst,
683                     Register src1,
684                     Register src2,
685                     Label* on_not_smi_result = NULL,
686                     Label::Distance near_jump = Label::kFar);
687   // Shifts a smi value to the right, shifting in zero bits at the top, and
688   // returns the unsigned intepretation of the result if that is a smi.
689   // Uses and clobbers rcx, so dst may not be rcx.
690   void SmiShiftLogicalRight(Register dst,
691                             Register src1,
692                             Register src2,
693                             Label* on_not_smi_result,
694                             Label::Distance near_jump = Label::kFar);
695   // Shifts a smi value to the right, sign extending the top, and
696   // returns the signed intepretation of the result. That will always
697   // be a valid smi value, since it's numerically smaller than the
698   // original.
699   // Uses and clobbers rcx, so dst may not be rcx.
700   void SmiShiftArithmeticRight(Register dst,
701                                Register src1,
702                                Register src2);
703 
704   // Specialized operations
705 
706   // Select the non-smi register of two registers where exactly one is a
707   // smi. If neither are smis, jump to the failure label.
708   void SelectNonSmi(Register dst,
709                     Register src1,
710                     Register src2,
711                     Label* on_not_smis,
712                     Label::Distance near_jump = Label::kFar);
713 
714   // Converts, if necessary, a smi to a combination of number and
715   // multiplier to be used as a scaled index.
716   // The src register contains a *positive* smi value. The shift is the
717   // power of two to multiply the index value by (e.g.
718   // to index by smi-value * kPointerSize, pass the smi and kPointerSizeLog2).
719   // The returned index register may be either src or dst, depending
720   // on what is most efficient. If src and dst are different registers,
721   // src is always unchanged.
722   SmiIndex SmiToIndex(Register dst, Register src, int shift);
723 
724   // Converts a positive smi to a negative index.
725   SmiIndex SmiToNegativeIndex(Register dst, Register src, int shift);
726 
727   // Add the value of a smi in memory to an int32 register.
728   // Sets flags as a normal add.
729   void AddSmiField(Register dst, const Operand& src);
730 
731   // Basic Smi operations.
Move(Register dst,Smi * source)732   void Move(Register dst, Smi* source) {
733     LoadSmiConstant(dst, source);
734   }
735 
Move(const Operand & dst,Smi * source)736   void Move(const Operand& dst, Smi* source) {
737     Register constant = GetSmiConstant(source);
738     movp(dst, constant);
739   }
740 
741   void Push(Smi* smi);
742 
743   // Save away a raw integer with pointer size on the stack as two integers
744   // masquerading as smis so that the garbage collector skips visiting them.
745   void PushRegisterAsTwoSmis(Register src, Register scratch = kScratchRegister);
746   // Reconstruct a raw integer with pointer size from two integers masquerading
747   // as smis on the top of stack.
748   void PopRegisterAsTwoSmis(Register dst, Register scratch = kScratchRegister);
749 
750   void Test(const Operand& dst, Smi* source);
751 
752 
753   // ---------------------------------------------------------------------------
754   // String macros.
755 
756   // If object is a string, its map is loaded into object_map.
757   void JumpIfNotString(Register object,
758                        Register object_map,
759                        Label* not_string,
760                        Label::Distance near_jump = Label::kFar);
761 
762 
763   void JumpIfNotBothSequentialOneByteStrings(
764       Register first_object, Register second_object, Register scratch1,
765       Register scratch2, Label* on_not_both_flat_one_byte,
766       Label::Distance near_jump = Label::kFar);
767 
768   // Check whether the instance type represents a flat one-byte string. Jump
769   // to the label if not. If the instance type can be scratched specify same
770   // register for both instance type and scratch.
771   void JumpIfInstanceTypeIsNotSequentialOneByte(
772       Register instance_type, Register scratch,
773       Label* on_not_flat_one_byte_string,
774       Label::Distance near_jump = Label::kFar);
775 
776   void JumpIfBothInstanceTypesAreNotSequentialOneByte(
777       Register first_object_instance_type, Register second_object_instance_type,
778       Register scratch1, Register scratch2, Label* on_fail,
779       Label::Distance near_jump = Label::kFar);
780 
781   void EmitSeqStringSetCharCheck(Register string,
782                                  Register index,
783                                  Register value,
784                                  uint32_t encoding_mask);
785 
786   // Checks if the given register or operand is a unique name
787   void JumpIfNotUniqueNameInstanceType(Register reg, Label* not_unique_name,
788                                        Label::Distance distance = Label::kFar);
789   void JumpIfNotUniqueNameInstanceType(Operand operand, Label* not_unique_name,
790                                        Label::Distance distance = Label::kFar);
791 
792   // ---------------------------------------------------------------------------
793   // Macro instructions.
794 
795   // Load/store with specific representation.
796   void Load(Register dst, const Operand& src, Representation r);
797   void Store(const Operand& dst, Register src, Representation r);
798 
799   // Load a register with a long value as efficiently as possible.
800   void Set(Register dst, int64_t x);
801   void Set(const Operand& dst, intptr_t x);
802 
803   void Cvtss2sd(XMMRegister dst, XMMRegister src);
804   void Cvtss2sd(XMMRegister dst, const Operand& src);
805   void Cvtsd2ss(XMMRegister dst, XMMRegister src);
806   void Cvtsd2ss(XMMRegister dst, const Operand& src);
807 
808   // cvtsi2sd instruction only writes to the low 64-bit of dst register, which
809   // hinders register renaming and makes dependence chains longer. So we use
810   // xorpd to clear the dst register before cvtsi2sd to solve this issue.
811   void Cvtlsi2sd(XMMRegister dst, Register src);
812   void Cvtlsi2sd(XMMRegister dst, const Operand& src);
813 
814   void Cvtqsi2ss(XMMRegister dst, Register src);
815   void Cvtqsi2ss(XMMRegister dst, const Operand& src);
816 
817   void Cvtqsi2sd(XMMRegister dst, Register src);
818   void Cvtqsi2sd(XMMRegister dst, const Operand& src);
819 
820   void Cvtqui2ss(XMMRegister dst, Register src, Register tmp);
821   void Cvtqui2sd(XMMRegister dst, Register src, Register tmp);
822 
823   void Cvtsd2si(Register dst, XMMRegister src);
824 
825   void Cvttsd2si(Register dst, XMMRegister src);
826   void Cvttsd2si(Register dst, const Operand& src);
827   void Cvttss2siq(Register dst, XMMRegister src);
828   void Cvttss2siq(Register dst, const Operand& src);
829   void Cvttsd2siq(Register dst, XMMRegister src);
830   void Cvttsd2siq(Register dst, const Operand& src);
831 
832   // Move if the registers are not identical.
833   void Move(Register target, Register source);
834 
835   // TestBit and Load SharedFunctionInfo special field.
836   void TestBitSharedFunctionInfoSpecialField(Register base,
837                                              int offset,
838                                              int bit_index);
839   void LoadSharedFunctionInfoSpecialField(Register dst,
840                                           Register base,
841                                           int offset);
842 
843   // Handle support
844   void Move(Register dst, Handle<Object> source);
845   void Move(const Operand& dst, Handle<Object> source);
846   void Cmp(Register dst, Handle<Object> source);
847   void Cmp(const Operand& dst, Handle<Object> source);
848   void Cmp(Register dst, Smi* src);
849   void Cmp(const Operand& dst, Smi* src);
850   void Push(Handle<Object> source);
851 
852   // Load a heap object and handle the case of new-space objects by
853   // indirecting via a global cell.
854   void MoveHeapObject(Register result, Handle<Object> object);
855 
856   // Load a global cell into a register.
857   void LoadGlobalCell(Register dst, Handle<Cell> cell);
858 
859   // Compare the given value and the value of weak cell.
860   void CmpWeakValue(Register value, Handle<WeakCell> cell, Register scratch);
861 
862   void GetWeakValue(Register value, Handle<WeakCell> cell);
863 
864   // Load the value of the weak cell in the value register. Branch to the given
865   // miss label if the weak cell was cleared.
866   void LoadWeakValue(Register value, Handle<WeakCell> cell, Label* miss);
867 
868   // Emit code to discard a non-negative number of pointer-sized elements
869   // from the stack, clobbering only the rsp register.
870   void Drop(int stack_elements);
871   // Emit code to discard a positive number of pointer-sized elements
872   // from the stack under the return address which remains on the top,
873   // clobbering the rsp register.
874   void DropUnderReturnAddress(int stack_elements,
875                               Register scratch = kScratchRegister);
876 
Call(Label * target)877   void Call(Label* target) { call(target); }
878   void Push(Register src);
879   void Push(const Operand& src);
880   void PushQuad(const Operand& src);
881   void Push(Immediate value);
882   void PushImm32(int32_t imm32);
883   void Pop(Register dst);
884   void Pop(const Operand& dst);
885   void PopQuad(const Operand& dst);
PushReturnAddressFrom(Register src)886   void PushReturnAddressFrom(Register src) { pushq(src); }
PopReturnAddressTo(Register dst)887   void PopReturnAddressTo(Register dst) { popq(dst); }
Move(Register dst,ExternalReference ext)888   void Move(Register dst, ExternalReference ext) {
889     movp(dst, reinterpret_cast<void*>(ext.address()),
890          RelocInfo::EXTERNAL_REFERENCE);
891   }
892 
893   // Loads a pointer into a register with a relocation mode.
Move(Register dst,void * ptr,RelocInfo::Mode rmode)894   void Move(Register dst, void* ptr, RelocInfo::Mode rmode) {
895     // This method must not be used with heap object references. The stored
896     // address is not GC safe. Use the handle version instead.
897     DCHECK(rmode > RelocInfo::LAST_GCED_ENUM);
898     movp(dst, ptr, rmode);
899   }
900 
Move(Register dst,Handle<Object> value,RelocInfo::Mode rmode)901   void Move(Register dst, Handle<Object> value, RelocInfo::Mode rmode) {
902     AllowDeferredHandleDereference using_raw_address;
903     DCHECK(!RelocInfo::IsNone(rmode));
904     DCHECK(value->IsHeapObject());
905     DCHECK(!isolate()->heap()->InNewSpace(*value));
906     movp(dst, reinterpret_cast<void*>(value.location()), rmode);
907   }
908 
909   void Move(XMMRegister dst, uint32_t src);
910   void Move(XMMRegister dst, uint64_t src);
Move(XMMRegister dst,float src)911   void Move(XMMRegister dst, float src) { Move(dst, bit_cast<uint32_t>(src)); }
Move(XMMRegister dst,double src)912   void Move(XMMRegister dst, double src) { Move(dst, bit_cast<uint64_t>(src)); }
913 
914 #define AVX_OP2_WITH_TYPE(macro_name, name, src_type) \
915   void macro_name(XMMRegister dst, src_type src) {    \
916     if (CpuFeatures::IsSupported(AVX)) {              \
917       CpuFeatureScope scope(this, AVX);               \
918       v##name(dst, dst, src);                         \
919     } else {                                          \
920       name(dst, src);                                 \
921     }                                                 \
922   }
923 #define AVX_OP2_X(macro_name, name) \
924   AVX_OP2_WITH_TYPE(macro_name, name, XMMRegister)
925 #define AVX_OP2_O(macro_name, name) \
926   AVX_OP2_WITH_TYPE(macro_name, name, const Operand&)
927 #define AVX_OP2_XO(macro_name, name) \
928   AVX_OP2_X(macro_name, name)        \
929   AVX_OP2_O(macro_name, name)
930 
931   AVX_OP2_XO(Addsd, addsd)
932   AVX_OP2_XO(Subsd, subsd)
933   AVX_OP2_XO(Mulsd, mulsd)
934   AVX_OP2_XO(Divsd, divsd)
935   AVX_OP2_X(Andpd, andpd)
936   AVX_OP2_X(Orpd, orpd)
937   AVX_OP2_X(Xorpd, xorpd)
938   AVX_OP2_X(Pcmpeqd, pcmpeqd)
939   AVX_OP2_WITH_TYPE(Psllq, psllq, byte)
940   AVX_OP2_WITH_TYPE(Psrlq, psrlq, byte)
941 
942 #undef AVX_OP2_O
943 #undef AVX_OP2_X
944 #undef AVX_OP2_XO
945 #undef AVX_OP2_WITH_TYPE
946 
947   void Movsd(XMMRegister dst, XMMRegister src);
948   void Movsd(XMMRegister dst, const Operand& src);
949   void Movsd(const Operand& dst, XMMRegister src);
950   void Movss(XMMRegister dst, XMMRegister src);
951   void Movss(XMMRegister dst, const Operand& src);
952   void Movss(const Operand& dst, XMMRegister src);
953 
954   void Movd(XMMRegister dst, Register src);
955   void Movd(XMMRegister dst, const Operand& src);
956   void Movd(Register dst, XMMRegister src);
957   void Movq(XMMRegister dst, Register src);
958   void Movq(Register dst, XMMRegister src);
959 
960   void Movaps(XMMRegister dst, XMMRegister src);
961   void Movapd(XMMRegister dst, XMMRegister src);
962   void Movmskpd(Register dst, XMMRegister src);
963 
964   void Roundss(XMMRegister dst, XMMRegister src, RoundingMode mode);
965   void Roundsd(XMMRegister dst, XMMRegister src, RoundingMode mode);
966   void Sqrtsd(XMMRegister dst, XMMRegister src);
967   void Sqrtsd(XMMRegister dst, const Operand& src);
968 
969   void Ucomiss(XMMRegister src1, XMMRegister src2);
970   void Ucomiss(XMMRegister src1, const Operand& src2);
971   void Ucomisd(XMMRegister src1, XMMRegister src2);
972   void Ucomisd(XMMRegister src1, const Operand& src2);
973 
974   // Control Flow
975   void Jump(Address destination, RelocInfo::Mode rmode);
976   void Jump(ExternalReference ext);
977   void Jump(const Operand& op);
978   void Jump(Handle<Code> code_object, RelocInfo::Mode rmode);
979 
980   void Call(Address destination, RelocInfo::Mode rmode);
981   void Call(ExternalReference ext);
982   void Call(const Operand& op);
983   void Call(Handle<Code> code_object,
984             RelocInfo::Mode rmode,
985             TypeFeedbackId ast_id = TypeFeedbackId::None());
986 
987   // The size of the code generated for different call instructions.
CallSize(Address destination)988   int CallSize(Address destination) {
989     return kCallSequenceLength;
990   }
991   int CallSize(ExternalReference ext);
CallSize(Handle<Code> code_object)992   int CallSize(Handle<Code> code_object) {
993     // Code calls use 32-bit relative addressing.
994     return kShortCallInstructionLength;
995   }
CallSize(Register target)996   int CallSize(Register target) {
997     // Opcode: REX_opt FF /2 m64
998     return (target.high_bit() != 0) ? 3 : 2;
999   }
CallSize(const Operand & target)1000   int CallSize(const Operand& target) {
1001     // Opcode: REX_opt FF /2 m64
1002     return (target.requires_rex() ? 2 : 1) + target.operand_size();
1003   }
1004 
1005   // Emit call to the code we are currently generating.
CallSelf()1006   void CallSelf() {
1007     Handle<Code> self(reinterpret_cast<Code**>(CodeObject().location()));
1008     Call(self, RelocInfo::CODE_TARGET);
1009   }
1010 
1011   // Non-SSE2 instructions.
1012   void Pextrd(Register dst, XMMRegister src, int8_t imm8);
1013   void Pinsrd(XMMRegister dst, Register src, int8_t imm8);
1014   void Pinsrd(XMMRegister dst, const Operand& src, int8_t imm8);
1015 
1016   void Lzcntq(Register dst, Register src);
1017   void Lzcntq(Register dst, const Operand& src);
1018 
1019   void Lzcntl(Register dst, Register src);
1020   void Lzcntl(Register dst, const Operand& src);
1021 
1022   void Tzcntq(Register dst, Register src);
1023   void Tzcntq(Register dst, const Operand& src);
1024 
1025   void Tzcntl(Register dst, Register src);
1026   void Tzcntl(Register dst, const Operand& src);
1027 
1028   void Popcntl(Register dst, Register src);
1029   void Popcntl(Register dst, const Operand& src);
1030 
1031   void Popcntq(Register dst, Register src);
1032   void Popcntq(Register dst, const Operand& src);
1033 
1034   // Non-x64 instructions.
1035   // Push/pop all general purpose registers.
1036   // Does not push rsp/rbp nor any of the assembler's special purpose registers
1037   // (kScratchRegister, kRootRegister).
1038   void Pushad();
1039   void Popad();
1040   // Sets the stack as after performing Popad, without actually loading the
1041   // registers.
1042   void Dropad();
1043 
1044   // Compare object type for heap object.
1045   // Always use unsigned comparisons: above and below, not less and greater.
1046   // Incoming register is heap_object and outgoing register is map.
1047   // They may be the same register, and may be kScratchRegister.
1048   void CmpObjectType(Register heap_object, InstanceType type, Register map);
1049 
1050   // Compare instance type for map.
1051   // Always use unsigned comparisons: above and below, not less and greater.
1052   void CmpInstanceType(Register map, InstanceType type);
1053 
1054   // Check if a map for a JSObject indicates that the object has fast elements.
1055   // Jump to the specified label if it does not.
1056   void CheckFastElements(Register map,
1057                          Label* fail,
1058                          Label::Distance distance = Label::kFar);
1059 
1060   // Check if a map for a JSObject indicates that the object can have both smi
1061   // and HeapObject elements.  Jump to the specified label if it does not.
1062   void CheckFastObjectElements(Register map,
1063                                Label* fail,
1064                                Label::Distance distance = Label::kFar);
1065 
1066   // Check if a map for a JSObject indicates that the object has fast smi only
1067   // elements.  Jump to the specified label if it does not.
1068   void CheckFastSmiElements(Register map,
1069                             Label* fail,
1070                             Label::Distance distance = Label::kFar);
1071 
1072   // Check to see if maybe_number can be stored as a double in
1073   // FastDoubleElements. If it can, store it at the index specified by index in
1074   // the FastDoubleElements array elements, otherwise jump to fail.  Note that
1075   // index must not be smi-tagged.
1076   void StoreNumberToDoubleElements(Register maybe_number,
1077                                    Register elements,
1078                                    Register index,
1079                                    XMMRegister xmm_scratch,
1080                                    Label* fail,
1081                                    int elements_offset = 0);
1082 
1083   // Compare an object's map with the specified map.
1084   void CompareMap(Register obj, Handle<Map> map);
1085 
1086   // Check if the map of an object is equal to a specified map and branch to
1087   // label if not. Skip the smi check if not required (object is known to be a
1088   // heap object). If mode is ALLOW_ELEMENT_TRANSITION_MAPS, then also match
1089   // against maps that are ElementsKind transition maps of the specified map.
1090   void CheckMap(Register obj,
1091                 Handle<Map> map,
1092                 Label* fail,
1093                 SmiCheckType smi_check_type);
1094 
1095   // Check if the map of an object is equal to a specified weak map and branch
1096   // to a specified target if equal. Skip the smi check if not required
1097   // (object is known to be a heap object)
1098   void DispatchWeakMap(Register obj, Register scratch1, Register scratch2,
1099                        Handle<WeakCell> cell, Handle<Code> success,
1100                        SmiCheckType smi_check_type);
1101 
1102   // Check if the object in register heap_object is a string. Afterwards the
1103   // register map contains the object map and the register instance_type
1104   // contains the instance_type. The registers map and instance_type can be the
1105   // same in which case it contains the instance type afterwards. Either of the
1106   // registers map and instance_type can be the same as heap_object.
1107   Condition IsObjectStringType(Register heap_object,
1108                                Register map,
1109                                Register instance_type);
1110 
1111   // Check if the object in register heap_object is a name. Afterwards the
1112   // register map contains the object map and the register instance_type
1113   // contains the instance_type. The registers map and instance_type can be the
1114   // same in which case it contains the instance type afterwards. Either of the
1115   // registers map and instance_type can be the same as heap_object.
1116   Condition IsObjectNameType(Register heap_object,
1117                              Register map,
1118                              Register instance_type);
1119 
1120   // FCmp compares and pops the two values on top of the FPU stack.
1121   // The flag results are similar to integer cmp, but requires unsigned
1122   // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
1123   void FCmp();
1124 
1125   void ClampUint8(Register reg);
1126 
1127   void ClampDoubleToUint8(XMMRegister input_reg,
1128                           XMMRegister temp_xmm_reg,
1129                           Register result_reg);
1130 
1131   void SlowTruncateToI(Register result_reg, Register input_reg,
1132       int offset = HeapNumber::kValueOffset - kHeapObjectTag);
1133 
1134   void TruncateHeapNumberToI(Register result_reg, Register input_reg);
1135   void TruncateDoubleToI(Register result_reg, XMMRegister input_reg);
1136 
1137   void DoubleToI(Register result_reg, XMMRegister input_reg,
1138                  XMMRegister scratch, MinusZeroMode minus_zero_mode,
1139                  Label* lost_precision, Label* is_nan, Label* minus_zero,
1140                  Label::Distance dst = Label::kFar);
1141 
1142   void LoadUint32(XMMRegister dst, Register src);
1143 
1144   void LoadInstanceDescriptors(Register map, Register descriptors);
1145   void EnumLength(Register dst, Register map);
1146   void NumberOfOwnDescriptors(Register dst, Register map);
1147   void LoadAccessor(Register dst, Register holder, int accessor_index,
1148                     AccessorComponent accessor);
1149 
1150   template<typename Field>
DecodeField(Register reg)1151   void DecodeField(Register reg) {
1152     static const int shift = Field::kShift;
1153     static const int mask = Field::kMask >> Field::kShift;
1154     if (shift != 0) {
1155       shrp(reg, Immediate(shift));
1156     }
1157     andp(reg, Immediate(mask));
1158   }
1159 
1160   template<typename Field>
DecodeFieldToSmi(Register reg)1161   void DecodeFieldToSmi(Register reg) {
1162     if (SmiValuesAre32Bits()) {
1163       andp(reg, Immediate(Field::kMask));
1164       shlp(reg, Immediate(kSmiShift - Field::kShift));
1165     } else {
1166       static const int shift = Field::kShift;
1167       static const int mask = (Field::kMask >> Field::kShift) << kSmiTagSize;
1168       DCHECK(SmiValuesAre31Bits());
1169       DCHECK(kSmiShift == kSmiTagSize);
1170       DCHECK((mask & 0x80000000u) == 0);
1171       if (shift < kSmiShift) {
1172         shlp(reg, Immediate(kSmiShift - shift));
1173       } else if (shift > kSmiShift) {
1174         sarp(reg, Immediate(shift - kSmiShift));
1175       }
1176       andp(reg, Immediate(mask));
1177     }
1178   }
1179 
1180   // Abort execution if argument is not a number, enabled via --debug-code.
1181   void AssertNumber(Register object);
1182 
1183   // Abort execution if argument is a smi, enabled via --debug-code.
1184   void AssertNotSmi(Register object);
1185 
1186   // Abort execution if argument is not a smi, enabled via --debug-code.
1187   void AssertSmi(Register object);
1188   void AssertSmi(const Operand& object);
1189 
1190   // Abort execution if a 64 bit register containing a 32 bit payload does not
1191   // have zeros in the top 32 bits, enabled via --debug-code.
1192   void AssertZeroExtended(Register reg);
1193 
1194   // Abort execution if argument is not a string, enabled via --debug-code.
1195   void AssertString(Register object);
1196 
1197   // Abort execution if argument is not a name, enabled via --debug-code.
1198   void AssertName(Register object);
1199 
1200   // Abort execution if argument is not a JSFunction, enabled via --debug-code.
1201   void AssertFunction(Register object);
1202 
1203   // Abort execution if argument is not a JSBoundFunction,
1204   // enabled via --debug-code.
1205   void AssertBoundFunction(Register object);
1206 
1207   // Abort execution if argument is not undefined or an AllocationSite, enabled
1208   // via --debug-code.
1209   void AssertUndefinedOrAllocationSite(Register object);
1210 
1211   // Abort execution if argument is not the root value with the given index,
1212   // enabled via --debug-code.
1213   void AssertRootValue(Register src,
1214                        Heap::RootListIndex root_value_index,
1215                        BailoutReason reason);
1216 
1217   // ---------------------------------------------------------------------------
1218   // Exception handling
1219 
1220   // Push a new stack handler and link it into stack handler chain.
1221   void PushStackHandler();
1222 
1223   // Unlink the stack handler on top of the stack from the stack handler chain.
1224   void PopStackHandler();
1225 
1226   // ---------------------------------------------------------------------------
1227   // Inline caching support
1228 
1229   // Generate code for checking access rights - used for security checks
1230   // on access to global objects across environments. The holder register
1231   // is left untouched, but the scratch register and kScratchRegister,
1232   // which must be different, are clobbered.
1233   void CheckAccessGlobalProxy(Register holder_reg,
1234                               Register scratch,
1235                               Label* miss);
1236 
1237   void GetNumberHash(Register r0, Register scratch);
1238 
1239   void LoadFromNumberDictionary(Label* miss,
1240                                 Register elements,
1241                                 Register key,
1242                                 Register r0,
1243                                 Register r1,
1244                                 Register r2,
1245                                 Register result);
1246 
1247 
1248   // ---------------------------------------------------------------------------
1249   // Allocation support
1250 
1251   // Allocate an object in new space or old space. If the given space
1252   // is exhausted control continues at the gc_required label. The allocated
1253   // object is returned in result and end of the new object is returned in
1254   // result_end. The register scratch can be passed as no_reg in which case
1255   // an additional object reference will be added to the reloc info. The
1256   // returned pointers in result and result_end have not yet been tagged as
1257   // heap objects. If result_contains_top_on_entry is true the content of
1258   // result is known to be the allocation top on entry (could be result_end
1259   // from a previous call). If result_contains_top_on_entry is true scratch
1260   // should be no_reg as it is never used.
1261   void Allocate(int object_size,
1262                 Register result,
1263                 Register result_end,
1264                 Register scratch,
1265                 Label* gc_required,
1266                 AllocationFlags flags);
1267 
1268   void Allocate(int header_size,
1269                 ScaleFactor element_size,
1270                 Register element_count,
1271                 Register result,
1272                 Register result_end,
1273                 Register scratch,
1274                 Label* gc_required,
1275                 AllocationFlags flags);
1276 
1277   void Allocate(Register object_size,
1278                 Register result,
1279                 Register result_end,
1280                 Register scratch,
1281                 Label* gc_required,
1282                 AllocationFlags flags);
1283 
1284   // Allocate a heap number in new space with undefined value. Returns
1285   // tagged pointer in result register, or jumps to gc_required if new
1286   // space is full.
1287   void AllocateHeapNumber(Register result,
1288                           Register scratch,
1289                           Label* gc_required,
1290                           MutableMode mode = IMMUTABLE);
1291 
1292   // Allocate a sequential string. All the header fields of the string object
1293   // are initialized.
1294   void AllocateTwoByteString(Register result,
1295                              Register length,
1296                              Register scratch1,
1297                              Register scratch2,
1298                              Register scratch3,
1299                              Label* gc_required);
1300   void AllocateOneByteString(Register result, Register length,
1301                              Register scratch1, Register scratch2,
1302                              Register scratch3, Label* gc_required);
1303 
1304   // Allocate a raw cons string object. Only the map field of the result is
1305   // initialized.
1306   void AllocateTwoByteConsString(Register result,
1307                           Register scratch1,
1308                           Register scratch2,
1309                           Label* gc_required);
1310   void AllocateOneByteConsString(Register result, Register scratch1,
1311                                  Register scratch2, Label* gc_required);
1312 
1313   // Allocate a raw sliced string object. Only the map field of the result is
1314   // initialized.
1315   void AllocateTwoByteSlicedString(Register result,
1316                             Register scratch1,
1317                             Register scratch2,
1318                             Label* gc_required);
1319   void AllocateOneByteSlicedString(Register result, Register scratch1,
1320                                    Register scratch2, Label* gc_required);
1321 
1322   // Allocate and initialize a JSValue wrapper with the specified {constructor}
1323   // and {value}.
1324   void AllocateJSValue(Register result, Register constructor, Register value,
1325                        Register scratch, Label* gc_required);
1326 
1327   // ---------------------------------------------------------------------------
1328   // Support functions.
1329 
1330   // Check if result is zero and op is negative.
1331   void NegativeZeroTest(Register result, Register op, Label* then_label);
1332 
1333   // Check if result is zero and op is negative in code using jump targets.
1334   void NegativeZeroTest(CodeGenerator* cgen,
1335                         Register result,
1336                         Register op,
1337                         JumpTarget* then_target);
1338 
1339   // Check if result is zero and any of op1 and op2 are negative.
1340   // Register scratch is destroyed, and it must be different from op2.
1341   void NegativeZeroTest(Register result, Register op1, Register op2,
1342                         Register scratch, Label* then_label);
1343 
1344   // Machine code version of Map::GetConstructor().
1345   // |temp| holds |result|'s map when done.
1346   void GetMapConstructor(Register result, Register map, Register temp);
1347 
1348   // Try to get function prototype of a function and puts the value in
1349   // the result register. Checks that the function really is a
1350   // function and jumps to the miss label if the fast checks fail. The
1351   // function register will be untouched; the other register may be
1352   // clobbered.
1353   void TryGetFunctionPrototype(Register function, Register result, Label* miss);
1354 
1355   // Picks out an array index from the hash field.
1356   // Register use:
1357   //   hash - holds the index's hash. Clobbered.
1358   //   index - holds the overwritten index on exit.
1359   void IndexFromHash(Register hash, Register index);
1360 
1361   // Find the function context up the context chain.
1362   void LoadContext(Register dst, int context_chain_length);
1363 
1364   // Load the global object from the current context.
LoadGlobalObject(Register dst)1365   void LoadGlobalObject(Register dst) {
1366     LoadNativeContextSlot(Context::EXTENSION_INDEX, dst);
1367   }
1368 
1369   // Load the global proxy from the current context.
LoadGlobalProxy(Register dst)1370   void LoadGlobalProxy(Register dst) {
1371     LoadNativeContextSlot(Context::GLOBAL_PROXY_INDEX, dst);
1372   }
1373 
1374   // Conditionally load the cached Array transitioned map of type
1375   // transitioned_kind from the native context if the map in register
1376   // map_in_out is the cached Array map in the native context of
1377   // expected_kind.
1378   void LoadTransitionedArrayMapConditional(
1379       ElementsKind expected_kind,
1380       ElementsKind transitioned_kind,
1381       Register map_in_out,
1382       Register scratch,
1383       Label* no_map_match);
1384 
1385   // Load the native context slot with the current index.
1386   void LoadNativeContextSlot(int index, Register dst);
1387 
1388   // Load the initial map from the global function. The registers
1389   // function and map can be the same.
1390   void LoadGlobalFunctionInitialMap(Register function, Register map);
1391 
1392   // ---------------------------------------------------------------------------
1393   // Runtime calls
1394 
1395   // Call a code stub.
1396   void CallStub(CodeStub* stub, TypeFeedbackId ast_id = TypeFeedbackId::None());
1397 
1398   // Tail call a code stub (jump).
1399   void TailCallStub(CodeStub* stub);
1400 
1401   // Return from a code stub after popping its arguments.
1402   void StubReturn(int argc);
1403 
1404   // Call a runtime routine.
1405   void CallRuntime(const Runtime::Function* f,
1406                    int num_arguments,
1407                    SaveFPRegsMode save_doubles = kDontSaveFPRegs);
1408 
1409   // Call a runtime function and save the value of XMM registers.
CallRuntimeSaveDoubles(Runtime::FunctionId fid)1410   void CallRuntimeSaveDoubles(Runtime::FunctionId fid) {
1411     const Runtime::Function* function = Runtime::FunctionForId(fid);
1412     CallRuntime(function, function->nargs, kSaveFPRegs);
1413   }
1414 
1415   // Convenience function: Same as above, but takes the fid instead.
1416   void CallRuntime(Runtime::FunctionId fid,
1417                    SaveFPRegsMode save_doubles = kDontSaveFPRegs) {
1418     const Runtime::Function* function = Runtime::FunctionForId(fid);
1419     CallRuntime(function, function->nargs, save_doubles);
1420   }
1421 
1422   // Convenience function: Same as above, but takes the fid instead.
1423   void CallRuntime(Runtime::FunctionId fid, int num_arguments,
1424                    SaveFPRegsMode save_doubles = kDontSaveFPRegs) {
1425     CallRuntime(Runtime::FunctionForId(fid), num_arguments, save_doubles);
1426   }
1427 
1428   // Convenience function: call an external reference.
1429   void CallExternalReference(const ExternalReference& ext,
1430                              int num_arguments);
1431 
1432   // Convenience function: tail call a runtime routine (jump)
1433   void TailCallRuntime(Runtime::FunctionId fid);
1434 
1435   // Jump to a runtime routines
1436   void JumpToExternalReference(const ExternalReference& ext);
1437 
1438   // Before calling a C-function from generated code, align arguments on stack.
1439   // After aligning the frame, arguments must be stored in rsp[0], rsp[8],
1440   // etc., not pushed. The argument count assumes all arguments are word sized.
1441   // The number of slots reserved for arguments depends on platform. On Windows
1442   // stack slots are reserved for the arguments passed in registers. On other
1443   // platforms stack slots are only reserved for the arguments actually passed
1444   // on the stack.
1445   void PrepareCallCFunction(int num_arguments);
1446 
1447   // Calls a C function and cleans up the space for arguments allocated
1448   // by PrepareCallCFunction. The called function is not allowed to trigger a
1449   // garbage collection, since that might move the code and invalidate the
1450   // return address (unless this is somehow accounted for by the called
1451   // function).
1452   void CallCFunction(ExternalReference function, int num_arguments);
1453   void CallCFunction(Register function, int num_arguments);
1454 
1455   // Calculate the number of stack slots to reserve for arguments when calling a
1456   // C function.
1457   int ArgumentStackSlotsForCFunctionCall(int num_arguments);
1458 
1459   // ---------------------------------------------------------------------------
1460   // Utilities
1461 
1462   void Ret();
1463 
1464   // Return and drop arguments from stack, where the number of arguments
1465   // may be bigger than 2^16 - 1.  Requires a scratch register.
1466   void Ret(int bytes_dropped, Register scratch);
1467 
CodeObject()1468   Handle<Object> CodeObject() {
1469     DCHECK(!code_object_.is_null());
1470     return code_object_;
1471   }
1472 
1473   // Copy length bytes from source to destination.
1474   // Uses scratch register internally (if you have a low-eight register
1475   // free, do use it, otherwise kScratchRegister will be used).
1476   // The min_length is a minimum limit on the value that length will have.
1477   // The algorithm has some special cases that might be omitted if the string
1478   // is known to always be long.
1479   void CopyBytes(Register destination,
1480                  Register source,
1481                  Register length,
1482                  int min_length = 0,
1483                  Register scratch = kScratchRegister);
1484 
1485   // Initialize fields with filler values.  Fields starting at |current_address|
1486   // not including |end_address| are overwritten with the value in |filler|.  At
1487   // the end the loop, |current_address| takes the value of |end_address|.
1488   void InitializeFieldsWithFiller(Register current_address,
1489                                   Register end_address, Register filler);
1490 
1491 
1492   // Emit code for a truncating division by a constant. The dividend register is
1493   // unchanged, the result is in rdx, and rax gets clobbered.
1494   void TruncatingDiv(Register dividend, int32_t divisor);
1495 
1496   // ---------------------------------------------------------------------------
1497   // StatsCounter support
1498 
1499   void SetCounter(StatsCounter* counter, int value);
1500   void IncrementCounter(StatsCounter* counter, int value);
1501   void DecrementCounter(StatsCounter* counter, int value);
1502 
1503 
1504   // ---------------------------------------------------------------------------
1505   // Debugging
1506 
1507   // Calls Abort(msg) if the condition cc is not satisfied.
1508   // Use --debug_code to enable.
1509   void Assert(Condition cc, BailoutReason reason);
1510 
1511   void AssertFastElements(Register elements);
1512 
1513   // Like Assert(), but always enabled.
1514   void Check(Condition cc, BailoutReason reason);
1515 
1516   // Print a message to stdout and abort execution.
1517   void Abort(BailoutReason msg);
1518 
1519   // Check that the stack is aligned.
1520   void CheckStackAlignment();
1521 
1522   // Verify restrictions about code generated in stubs.
set_generating_stub(bool value)1523   void set_generating_stub(bool value) { generating_stub_ = value; }
generating_stub()1524   bool generating_stub() { return generating_stub_; }
set_has_frame(bool value)1525   void set_has_frame(bool value) { has_frame_ = value; }
has_frame()1526   bool has_frame() { return has_frame_; }
1527   inline bool AllowThisStubCall(CodeStub* stub);
1528 
SafepointRegisterStackIndex(Register reg)1529   static int SafepointRegisterStackIndex(Register reg) {
1530     return SafepointRegisterStackIndex(reg.code());
1531   }
1532 
1533   // Load the type feedback vector from a JavaScript frame.
1534   void EmitLoadTypeFeedbackVector(Register vector);
1535 
1536   // Activation support.
1537   void EnterFrame(StackFrame::Type type);
1538   void EnterFrame(StackFrame::Type type, bool load_constant_pool_pointer_reg);
1539   void LeaveFrame(StackFrame::Type type);
1540 
1541   // Expects object in rax and returns map with validated enum cache
1542   // in rax.  Assumes that any other register can be used as a scratch.
1543   void CheckEnumCache(Register null_value,
1544                       Label* call_runtime);
1545 
1546   // AllocationMemento support. Arrays may have an associated
1547   // AllocationMemento object that can be checked for in order to pretransition
1548   // to another type.
1549   // On entry, receiver_reg should point to the array object.
1550   // scratch_reg gets clobbered.
1551   // If allocation info is present, condition flags are set to equal.
1552   void TestJSArrayForAllocationMemento(Register receiver_reg,
1553                                        Register scratch_reg,
1554                                        Label* no_memento_found);
1555 
JumpIfJSArrayHasAllocationMemento(Register receiver_reg,Register scratch_reg,Label * memento_found)1556   void JumpIfJSArrayHasAllocationMemento(Register receiver_reg,
1557                                          Register scratch_reg,
1558                                          Label* memento_found) {
1559     Label no_memento_found;
1560     TestJSArrayForAllocationMemento(receiver_reg, scratch_reg,
1561                                     &no_memento_found);
1562     j(equal, memento_found);
1563     bind(&no_memento_found);
1564   }
1565 
1566   // Jumps to found label if a prototype map has dictionary elements.
1567   void JumpIfDictionaryInPrototypeChain(Register object, Register scratch0,
1568                                         Register scratch1, Label* found);
1569 
1570  private:
1571   // Order general registers are pushed by Pushad.
1572   // rax, rcx, rdx, rbx, rsi, rdi, r8, r9, r11, r12, r14, r15.
1573   static const int kSafepointPushRegisterIndices[Register::kNumRegisters];
1574   static const int kNumSafepointSavedRegisters = 12;
1575   static const int kSmiShift = kSmiTagSize + kSmiShiftSize;
1576 
1577   bool generating_stub_;
1578   bool has_frame_;
1579   bool root_array_available_;
1580 
1581   // Returns a register holding the smi value. The register MUST NOT be
1582   // modified. It may be the "smi 1 constant" register.
1583   Register GetSmiConstant(Smi* value);
1584 
1585   int64_t RootRegisterDelta(ExternalReference other);
1586 
1587   // Moves the smi value to the destination register.
1588   void LoadSmiConstant(Register dst, Smi* value);
1589 
1590   // This handle will be patched with the code object on installation.
1591   Handle<Object> code_object_;
1592 
1593   // Helper functions for generating invokes.
1594   void InvokePrologue(const ParameterCount& expected,
1595                       const ParameterCount& actual,
1596                       Label* done,
1597                       bool* definitely_mismatches,
1598                       InvokeFlag flag,
1599                       Label::Distance near_jump,
1600                       const CallWrapper& call_wrapper);
1601 
1602   void EnterExitFramePrologue(bool save_rax);
1603 
1604   // Allocates arg_stack_space * kPointerSize memory (not GCed) on the stack
1605   // accessible via StackSpaceOperand.
1606   void EnterExitFrameEpilogue(int arg_stack_space, bool save_doubles);
1607 
1608   void LeaveExitFrameEpilogue(bool restore_context);
1609 
1610   // Allocation support helpers.
1611   // Loads the top of new-space into the result register.
1612   // Otherwise the address of the new-space top is loaded into scratch (if
1613   // scratch is valid), and the new-space top is loaded into result.
1614   void LoadAllocationTopHelper(Register result,
1615                                Register scratch,
1616                                AllocationFlags flags);
1617 
1618   void MakeSureDoubleAlignedHelper(Register result,
1619                                    Register scratch,
1620                                    Label* gc_required,
1621                                    AllocationFlags flags);
1622 
1623   // Update allocation top with value in result_end register.
1624   // If scratch is valid, it contains the address of the allocation top.
1625   void UpdateAllocationTopHelper(Register result_end,
1626                                  Register scratch,
1627                                  AllocationFlags flags);
1628 
1629   // Helper for implementing JumpIfNotInNewSpace and JumpIfInNewSpace.
1630   void InNewSpace(Register object,
1631                   Register scratch,
1632                   Condition cc,
1633                   Label* branch,
1634                   Label::Distance distance = Label::kFar);
1635 
1636   // Helper for finding the mark bits for an address.  Afterwards, the
1637   // bitmap register points at the word with the mark bits and the mask
1638   // the position of the first bit.  Uses rcx as scratch and leaves addr_reg
1639   // unchanged.
1640   inline void GetMarkBits(Register addr_reg,
1641                           Register bitmap_reg,
1642                           Register mask_reg);
1643 
1644   // Compute memory operands for safepoint stack slots.
1645   Operand SafepointRegisterSlot(Register reg);
SafepointRegisterStackIndex(int reg_code)1646   static int SafepointRegisterStackIndex(int reg_code) {
1647     return kNumSafepointRegisters - kSafepointPushRegisterIndices[reg_code] - 1;
1648   }
1649 
1650   // Needs access to SafepointRegisterStackIndex for compiled frame
1651   // traversal.
1652   friend class StandardFrame;
1653 };
1654 
1655 
1656 // The code patcher is used to patch (typically) small parts of code e.g. for
1657 // debugging and other types of instrumentation. When using the code patcher
1658 // the exact number of bytes specified must be emitted. Is not legal to emit
1659 // relocation information. If any of these constraints are violated it causes
1660 // an assertion.
1661 class CodePatcher {
1662  public:
1663   CodePatcher(Isolate* isolate, byte* address, int size);
1664   ~CodePatcher();
1665 
1666   // Macro assembler to emit code.
masm()1667   MacroAssembler* masm() { return &masm_; }
1668 
1669  private:
1670   byte* address_;  // The address of the code being patched.
1671   int size_;  // Number of bytes of the expected patch size.
1672   MacroAssembler masm_;  // Macro assembler used to generate the code.
1673 };
1674 
1675 
1676 // -----------------------------------------------------------------------------
1677 // Static helper functions.
1678 
1679 // Generate an Operand for loading a field from an object.
FieldOperand(Register object,int offset)1680 inline Operand FieldOperand(Register object, int offset) {
1681   return Operand(object, offset - kHeapObjectTag);
1682 }
1683 
1684 
1685 // Generate an Operand for loading an indexed field from an object.
FieldOperand(Register object,Register index,ScaleFactor scale,int offset)1686 inline Operand FieldOperand(Register object,
1687                             Register index,
1688                             ScaleFactor scale,
1689                             int offset) {
1690   return Operand(object, index, scale, offset - kHeapObjectTag);
1691 }
1692 
1693 
ContextOperand(Register context,int index)1694 inline Operand ContextOperand(Register context, int index) {
1695   return Operand(context, Context::SlotOffset(index));
1696 }
1697 
1698 
ContextOperand(Register context,Register index)1699 inline Operand ContextOperand(Register context, Register index) {
1700   return Operand(context, index, times_pointer_size, Context::SlotOffset(0));
1701 }
1702 
1703 
NativeContextOperand()1704 inline Operand NativeContextOperand() {
1705   return ContextOperand(rsi, Context::NATIVE_CONTEXT_INDEX);
1706 }
1707 
1708 
1709 // Provides access to exit frame stack space (not GCed).
StackSpaceOperand(int index)1710 inline Operand StackSpaceOperand(int index) {
1711 #ifdef _WIN64
1712   const int kShaddowSpace = 4;
1713   return Operand(rsp, (index + kShaddowSpace) * kPointerSize);
1714 #else
1715   return Operand(rsp, index * kPointerSize);
1716 #endif
1717 }
1718 
1719 
StackOperandForReturnAddress(int32_t disp)1720 inline Operand StackOperandForReturnAddress(int32_t disp) {
1721   return Operand(rsp, disp);
1722 }
1723 
1724 
1725 #ifdef GENERATED_CODE_COVERAGE
1726 extern void LogGeneratedCodeCoverage(const char* file_line);
1727 #define CODE_COVERAGE_STRINGIFY(x) #x
1728 #define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
1729 #define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
1730 #define ACCESS_MASM(masm) {                                                  \
1731     Address x64_coverage_function = FUNCTION_ADDR(LogGeneratedCodeCoverage); \
1732     masm->pushfq();                                                          \
1733     masm->Pushad();                                                          \
1734     masm->Push(Immediate(reinterpret_cast<int>(&__FILE_LINE__)));            \
1735     masm->Call(x64_coverage_function, RelocInfo::EXTERNAL_REFERENCE);        \
1736     masm->Pop(rax);                                                          \
1737     masm->Popad();                                                           \
1738     masm->popfq();                                                           \
1739   }                                                                          \
1740   masm->
1741 #else
1742 #define ACCESS_MASM(masm) masm->
1743 #endif
1744 
1745 }  // namespace internal
1746 }  // namespace v8
1747 
1748 #endif  // V8_X64_MACRO_ASSEMBLER_X64_H_
1749