Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / v8 / src / ia32 / macro-assembler-ia32.h
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_IA32_MACRO_ASSEMBLER_IA32_H_
6 #define V8_IA32_MACRO_ASSEMBLER_IA32_H_
7
8 #include "src/assembler.h"
9 #include "src/bailout-reason.h"
10 #include "src/frames.h"
11 #include "src/globals.h"
12
13 namespace v8 {
14 namespace internal {
15
16 // Convenience for platform-independent signatures.  We do not normally
17 // distinguish memory operands from other operands on ia32.
18 typedef Operand MemOperand;
19
20 enum RememberedSetAction { EMIT_REMEMBERED_SET, OMIT_REMEMBERED_SET };
21 enum SmiCheck { INLINE_SMI_CHECK, OMIT_SMI_CHECK };
22 enum PointersToHereCheck {
23   kPointersToHereMaybeInteresting,
24   kPointersToHereAreAlwaysInteresting
25 };
26
27
28 enum RegisterValueType {
29   REGISTER_VALUE_IS_SMI,
30   REGISTER_VALUE_IS_INT32
31 };
32
33
34 #ifdef DEBUG
35 bool AreAliased(Register reg1,
36                 Register reg2,
37                 Register reg3 = no_reg,
38                 Register reg4 = no_reg,
39                 Register reg5 = no_reg,
40                 Register reg6 = no_reg,
41                 Register reg7 = no_reg,
42                 Register reg8 = no_reg);
43 #endif
44
45
46 // MacroAssembler implements a collection of frequently used macros.
47 class MacroAssembler: public Assembler {
48  public:
49   // The isolate parameter can be NULL if the macro assembler should
50   // not use isolate-dependent functionality. In this case, it's the
51   // responsibility of the caller to never invoke such function on the
52   // macro assembler.
53   MacroAssembler(Isolate* isolate, void* buffer, int size);
54
55   void Load(Register dst, const Operand& src, Representation r);
56   void Store(Register src, const Operand& dst, Representation r);
57
58   // Operations on roots in the root-array.
59   void LoadRoot(Register destination, Heap::RootListIndex index);
60   void StoreRoot(Register source, Register scratch, Heap::RootListIndex index);
61   void CompareRoot(Register with, Register scratch, Heap::RootListIndex index);
62   // These methods can only be used with constant roots (i.e. non-writable
63   // and not in new space).
64   void CompareRoot(Register with, Heap::RootListIndex index);
65   void CompareRoot(const Operand& with, Heap::RootListIndex index);
66
67   // ---------------------------------------------------------------------------
68   // GC Support
69   enum RememberedSetFinalAction {
70     kReturnAtEnd,
71     kFallThroughAtEnd
72   };
73
74   // Record in the remembered set the fact that we have a pointer to new space
75   // at the address pointed to by the addr register.  Only works if addr is not
76   // in new space.
77   void RememberedSetHelper(Register object,  // Used for debug code.
78                            Register addr,
79                            Register scratch,
80                            SaveFPRegsMode save_fp,
81                            RememberedSetFinalAction and_then);
82
83   void CheckPageFlag(Register object,
84                      Register scratch,
85                      int mask,
86                      Condition cc,
87                      Label* condition_met,
88                      Label::Distance condition_met_distance = Label::kFar);
89
90   void CheckPageFlagForMap(
91       Handle<Map> map,
92       int mask,
93       Condition cc,
94       Label* condition_met,
95       Label::Distance condition_met_distance = Label::kFar);
96
97   void CheckMapDeprecated(Handle<Map> map,
98                           Register scratch,
99                           Label* if_deprecated);
100
101   // Check if object is in new space.  Jumps if the object is not in new space.
102   // The register scratch can be object itself, but scratch will be clobbered.
103   void JumpIfNotInNewSpace(Register object,
104                            Register scratch,
105                            Label* branch,
106                            Label::Distance distance = Label::kFar) {
107     InNewSpace(object, scratch, zero, branch, distance);
108   }
109
110   // Check if object is in new space.  Jumps if the object is in new space.
111   // The register scratch can be object itself, but it will be clobbered.
112   void JumpIfInNewSpace(Register object,
113                         Register scratch,
114                         Label* branch,
115                         Label::Distance distance = Label::kFar) {
116     InNewSpace(object, scratch, not_zero, branch, distance);
117   }
118
119   // Check if an object has a given incremental marking color.  Also uses ecx!
120   void HasColor(Register object,
121                 Register scratch0,
122                 Register scratch1,
123                 Label* has_color,
124                 Label::Distance has_color_distance,
125                 int first_bit,
126                 int second_bit);
127
128   void JumpIfBlack(Register object,
129                    Register scratch0,
130                    Register scratch1,
131                    Label* on_black,
132                    Label::Distance on_black_distance = Label::kFar);
133
134   // Checks the color of an object.  If the object is already grey or black
135   // then we just fall through, since it is already live.  If it is white and
136   // we can determine that it doesn't need to be scanned, then we just mark it
137   // black and fall through.  For the rest we jump to the label so the
138   // incremental marker can fix its assumptions.
139   void EnsureNotWhite(Register object,
140                       Register scratch1,
141                       Register scratch2,
142                       Label* object_is_white_and_not_data,
143                       Label::Distance distance);
144
145   // Notify the garbage collector that we wrote a pointer into an object.
146   // |object| is the object being stored into, |value| is the object being
147   // stored.  value and scratch registers are clobbered by the operation.
148   // The offset is the offset from the start of the object, not the offset from
149   // the tagged HeapObject pointer.  For use with FieldOperand(reg, off).
150   void RecordWriteField(
151       Register object,
152       int offset,
153       Register value,
154       Register scratch,
155       SaveFPRegsMode save_fp,
156       RememberedSetAction remembered_set_action = EMIT_REMEMBERED_SET,
157       SmiCheck smi_check = INLINE_SMI_CHECK,
158       PointersToHereCheck pointers_to_here_check_for_value =
159           kPointersToHereMaybeInteresting);
160
161   // As above, but the offset has the tag presubtracted.  For use with
162   // Operand(reg, off).
163   void RecordWriteContextSlot(
164       Register context,
165       int offset,
166       Register value,
167       Register scratch,
168       SaveFPRegsMode save_fp,
169       RememberedSetAction remembered_set_action = EMIT_REMEMBERED_SET,
170       SmiCheck smi_check = INLINE_SMI_CHECK,
171       PointersToHereCheck pointers_to_here_check_for_value =
172           kPointersToHereMaybeInteresting) {
173     RecordWriteField(context,
174                      offset + kHeapObjectTag,
175                      value,
176                      scratch,
177                      save_fp,
178                      remembered_set_action,
179                      smi_check,
180                      pointers_to_here_check_for_value);
181   }
182
183   // Notify the garbage collector that we wrote a pointer into a fixed array.
184   // |array| is the array being stored into, |value| is the
185   // object being stored.  |index| is the array index represented as a
186   // Smi. All registers are clobbered by the operation RecordWriteArray
187   // filters out smis so it does not update the write barrier if the
188   // value is a smi.
189   void RecordWriteArray(
190       Register array,
191       Register value,
192       Register index,
193       SaveFPRegsMode save_fp,
194       RememberedSetAction remembered_set_action = EMIT_REMEMBERED_SET,
195       SmiCheck smi_check = INLINE_SMI_CHECK,
196       PointersToHereCheck pointers_to_here_check_for_value =
197           kPointersToHereMaybeInteresting);
198
199   // For page containing |object| mark region covering |address|
200   // dirty. |object| is the object being stored into, |value| is the
201   // object being stored. The address and value registers are clobbered by the
202   // operation. RecordWrite filters out smis so it does not update the
203   // write barrier if the value is a smi.
204   void RecordWrite(
205       Register object,
206       Register address,
207       Register value,
208       SaveFPRegsMode save_fp,
209       RememberedSetAction remembered_set_action = EMIT_REMEMBERED_SET,
210       SmiCheck smi_check = INLINE_SMI_CHECK,
211       PointersToHereCheck pointers_to_here_check_for_value =
212           kPointersToHereMaybeInteresting);
213
214   // For page containing |object| mark the region covering the object's map
215   // dirty. |object| is the object being stored into, |map| is the Map object
216   // that was stored.
217   void RecordWriteForMap(
218       Register object,
219       Handle<Map> map,
220       Register scratch1,
221       Register scratch2,
222       SaveFPRegsMode save_fp);
223
224   // ---------------------------------------------------------------------------
225   // Debugger Support
226
227   void DebugBreak();
228
229   // Generates function and stub prologue code.
230   void StubPrologue();
231   void Prologue(bool code_pre_aging);
232
233   // Enter specific kind of exit frame. Expects the number of
234   // arguments in register eax and sets up the number of arguments in
235   // register edi and the pointer to the first argument in register
236   // esi.
237   void EnterExitFrame(bool save_doubles);
238
239   void EnterApiExitFrame(int argc);
240
241   // Leave the current exit frame. Expects the return value in
242   // register eax:edx (untouched) and the pointer to the first
243   // argument in register esi.
244   void LeaveExitFrame(bool save_doubles);
245
246   // Leave the current exit frame. Expects the return value in
247   // register eax (untouched).
248   void LeaveApiExitFrame(bool restore_context);
249
250   // Find the function context up the context chain.
251   void LoadContext(Register dst, int context_chain_length);
252
253   // Conditionally load the cached Array transitioned map of type
254   // transitioned_kind from the native context if the map in register
255   // map_in_out is the cached Array map in the native context of
256   // expected_kind.
257   void LoadTransitionedArrayMapConditional(
258       ElementsKind expected_kind,
259       ElementsKind transitioned_kind,
260       Register map_in_out,
261       Register scratch,
262       Label* no_map_match);
263
264   // Load the global function with the given index.
265   void LoadGlobalFunction(int index, Register function);
266
267   // Load the initial map from the global function. The registers
268   // function and map can be the same.
269   void LoadGlobalFunctionInitialMap(Register function, Register map);
270
271   // Push and pop the registers that can hold pointers.
272   void PushSafepointRegisters() { pushad(); }
273   void PopSafepointRegisters() { popad(); }
274   // Store the value in register/immediate src in the safepoint
275   // register stack slot for register dst.
276   void StoreToSafepointRegisterSlot(Register dst, Register src);
277   void StoreToSafepointRegisterSlot(Register dst, Immediate src);
278   void LoadFromSafepointRegisterSlot(Register dst, Register src);
279
280   void LoadHeapObject(Register result, Handle<HeapObject> object);
281   void CmpHeapObject(Register reg, Handle<HeapObject> object);
282   void PushHeapObject(Handle<HeapObject> object);
283
284   void LoadObject(Register result, Handle<Object> object) {
285     AllowDeferredHandleDereference heap_object_check;
286     if (object->IsHeapObject()) {
287       LoadHeapObject(result, Handle<HeapObject>::cast(object));
288     } else {
289       Move(result, Immediate(object));
290     }
291   }
292
293   void CmpObject(Register reg, Handle<Object> object) {
294     AllowDeferredHandleDereference heap_object_check;
295     if (object->IsHeapObject()) {
296       CmpHeapObject(reg, Handle<HeapObject>::cast(object));
297     } else {
298       cmp(reg, Immediate(object));
299     }
300   }
301
302   // ---------------------------------------------------------------------------
303   // JavaScript invokes
304
305   // Invoke the JavaScript function code by either calling or jumping.
306   void InvokeCode(Register code,
307                   const ParameterCount& expected,
308                   const ParameterCount& actual,
309                   InvokeFlag flag,
310                   const CallWrapper& call_wrapper) {
311     InvokeCode(Operand(code), expected, actual, flag, call_wrapper);
312   }
313
314   void InvokeCode(const Operand& code,
315                   const ParameterCount& expected,
316                   const ParameterCount& actual,
317                   InvokeFlag flag,
318                   const CallWrapper& call_wrapper);
319
320   // Invoke the JavaScript function in the given register. Changes the
321   // current context to the context in the function before invoking.
322   void InvokeFunction(Register function,
323                       const ParameterCount& actual,
324                       InvokeFlag flag,
325                       const CallWrapper& call_wrapper);
326
327   void InvokeFunction(Register function,
328                       const ParameterCount& expected,
329                       const ParameterCount& actual,
330                       InvokeFlag flag,
331                       const CallWrapper& call_wrapper);
332
333   void InvokeFunction(Handle<JSFunction> function,
334                       const ParameterCount& expected,
335                       const ParameterCount& actual,
336                       InvokeFlag flag,
337                       const CallWrapper& call_wrapper);
338
339   // Invoke specified builtin JavaScript function. Adds an entry to
340   // the unresolved list if the name does not resolve.
341   void InvokeBuiltin(Builtins::JavaScript id,
342                      InvokeFlag flag,
343                      const CallWrapper& call_wrapper = NullCallWrapper());
344
345   // Store the function for the given builtin in the target register.
346   void GetBuiltinFunction(Register target, Builtins::JavaScript id);
347
348   // Store the code object for the given builtin in the target register.
349   void GetBuiltinEntry(Register target, Builtins::JavaScript id);
350
351   // Expression support
352   // cvtsi2sd instruction only writes to the low 64-bit of dst register, which
353   // hinders register renaming and makes dependence chains longer. So we use
354   // xorps to clear the dst register before cvtsi2sd to solve this issue.
355   void Cvtsi2sd(XMMRegister dst, Register src) { Cvtsi2sd(dst, Operand(src)); }
356   void Cvtsi2sd(XMMRegister dst, const Operand& src);
357
358   // Support for constant splitting.
359   bool IsUnsafeImmediate(const Immediate& x);
360   void SafeMove(Register dst, const Immediate& x);
361   void SafePush(const Immediate& x);
362
363   // Compare object type for heap object.
364   // Incoming register is heap_object and outgoing register is map.
365   void CmpObjectType(Register heap_object, InstanceType type, Register map);
366
367   // Compare instance type for map.
368   void CmpInstanceType(Register map, InstanceType type);
369
370   // Check if a map for a JSObject indicates that the object has fast elements.
371   // Jump to the specified label if it does not.
372   void CheckFastElements(Register map,
373                          Label* fail,
374                          Label::Distance distance = Label::kFar);
375
376   // Check if a map for a JSObject indicates that the object can have both smi
377   // and HeapObject elements.  Jump to the specified label if it does not.
378   void CheckFastObjectElements(Register map,
379                                Label* fail,
380                                Label::Distance distance = Label::kFar);
381
382   // Check if a map for a JSObject indicates that the object has fast smi only
383   // elements.  Jump to the specified label if it does not.
384   void CheckFastSmiElements(Register map,
385                             Label* fail,
386                             Label::Distance distance = Label::kFar);
387
388   // Check to see if maybe_number can be stored as a double in
389   // FastDoubleElements. If it can, store it at the index specified by key in
390   // the FastDoubleElements array elements, otherwise jump to fail.
391   void StoreNumberToDoubleElements(Register maybe_number,
392                                    Register elements,
393                                    Register key,
394                                    Register scratch1,
395                                    XMMRegister scratch2,
396                                    Label* fail,
397                                    int offset = 0);
398
399   // Compare an object's map with the specified map.
400   void CompareMap(Register obj, Handle<Map> map);
401
402   // Check if the map of an object is equal to a specified map and branch to
403   // label if not. Skip the smi check if not required (object is known to be a
404   // heap object). If mode is ALLOW_ELEMENT_TRANSITION_MAPS, then also match
405   // against maps that are ElementsKind transition maps of the specified map.
406   void CheckMap(Register obj,
407                 Handle<Map> map,
408                 Label* fail,
409                 SmiCheckType smi_check_type);
410
411   // Check if the map of an object is equal to a specified map and branch to a
412   // specified target if equal. Skip the smi check if not required (object is
413   // known to be a heap object)
414   void DispatchMap(Register obj,
415                    Register unused,
416                    Handle<Map> map,
417                    Handle<Code> success,
418                    SmiCheckType smi_check_type);
419
420   // Check if the object in register heap_object is a string. Afterwards the
421   // register map contains the object map and the register instance_type
422   // contains the instance_type. The registers map and instance_type can be the
423   // same in which case it contains the instance type afterwards. Either of the
424   // registers map and instance_type can be the same as heap_object.
425   Condition IsObjectStringType(Register heap_object,
426                                Register map,
427                                Register instance_type);
428
429   // Check if the object in register heap_object is a name. Afterwards the
430   // register map contains the object map and the register instance_type
431   // contains the instance_type. The registers map and instance_type can be the
432   // same in which case it contains the instance type afterwards. Either of the
433   // registers map and instance_type can be the same as heap_object.
434   Condition IsObjectNameType(Register heap_object,
435                              Register map,
436                              Register instance_type);
437
438   // Check if a heap object's type is in the JSObject range, not including
439   // JSFunction.  The object's map will be loaded in the map register.
440   // Any or all of the three registers may be the same.
441   // The contents of the scratch register will always be overwritten.
442   void IsObjectJSObjectType(Register heap_object,
443                             Register map,
444                             Register scratch,
445                             Label* fail);
446
447   // The contents of the scratch register will be overwritten.
448   void IsInstanceJSObjectType(Register map, Register scratch, Label* fail);
449
450   // FCmp is similar to integer cmp, but requires unsigned
451   // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
452   void FCmp();
453
454   void ClampUint8(Register reg);
455
456   void ClampDoubleToUint8(XMMRegister input_reg,
457                           XMMRegister scratch_reg,
458                           Register result_reg);
459
460   void SlowTruncateToI(Register result_reg, Register input_reg,
461       int offset = HeapNumber::kValueOffset - kHeapObjectTag);
462
463   void TruncateHeapNumberToI(Register result_reg, Register input_reg);
464   void TruncateDoubleToI(Register result_reg, XMMRegister input_reg);
465
466   void DoubleToI(Register result_reg, XMMRegister input_reg,
467                  XMMRegister scratch, MinusZeroMode minus_zero_mode,
468                  Label* lost_precision, Label* is_nan, Label* minus_zero,
469                  Label::Distance dst = Label::kFar);
470
471   // Smi tagging support.
472   void SmiTag(Register reg) {
473     STATIC_ASSERT(kSmiTag == 0);
474     STATIC_ASSERT(kSmiTagSize == 1);
475     add(reg, reg);
476   }
477   void SmiUntag(Register reg) {
478     sar(reg, kSmiTagSize);
479   }
480
481   // Modifies the register even if it does not contain a Smi!
482   void SmiUntag(Register reg, Label* is_smi) {
483     STATIC_ASSERT(kSmiTagSize == 1);
484     sar(reg, kSmiTagSize);
485     STATIC_ASSERT(kSmiTag == 0);
486     j(not_carry, is_smi);
487   }
488
489   void LoadUint32(XMMRegister dst, Register src) {
490     LoadUint32(dst, Operand(src));
491   }
492   void LoadUint32(XMMRegister dst, const Operand& src);
493
494   // Jump the register contains a smi.
495   inline void JumpIfSmi(Register value,
496                         Label* smi_label,
497                         Label::Distance distance = Label::kFar) {
498     test(value, Immediate(kSmiTagMask));
499     j(zero, smi_label, distance);
500   }
501   // Jump if the operand is a smi.
502   inline void JumpIfSmi(Operand value,
503                         Label* smi_label,
504                         Label::Distance distance = Label::kFar) {
505     test(value, Immediate(kSmiTagMask));
506     j(zero, smi_label, distance);
507   }
508   // Jump if register contain a non-smi.
509   inline void JumpIfNotSmi(Register value,
510                            Label* not_smi_label,
511                            Label::Distance distance = Label::kFar) {
512     test(value, Immediate(kSmiTagMask));
513     j(not_zero, not_smi_label, distance);
514   }
515
516   void LoadInstanceDescriptors(Register map, Register descriptors);
517   void EnumLength(Register dst, Register map);
518   void NumberOfOwnDescriptors(Register dst, Register map);
519
520   template<typename Field>
521   void DecodeField(Register reg) {
522     static const int shift = Field::kShift;
523     static const int mask = Field::kMask >> Field::kShift;
524     if (shift != 0) {
525       sar(reg, shift);
526     }
527     and_(reg, Immediate(mask));
528   }
529
530   template<typename Field>
531   void DecodeFieldToSmi(Register reg) {
532     static const int shift = Field::kShift;
533     static const int mask = (Field::kMask >> Field::kShift) << kSmiTagSize;
534     STATIC_ASSERT((mask & (0x80000000u >> (kSmiTagSize - 1))) == 0);
535     STATIC_ASSERT(kSmiTag == 0);
536     if (shift < kSmiTagSize) {
537       shl(reg, kSmiTagSize - shift);
538     } else if (shift > kSmiTagSize) {
539       sar(reg, shift - kSmiTagSize);
540     }
541     and_(reg, Immediate(mask));
542   }
543
544   void LoadPowerOf2(XMMRegister dst, Register scratch, int power);
545
546   // Abort execution if argument is not a number, enabled via --debug-code.
547   void AssertNumber(Register object);
548
549   // Abort execution if argument is not a smi, enabled via --debug-code.
550   void AssertSmi(Register object);
551
552   // Abort execution if argument is a smi, enabled via --debug-code.
553   void AssertNotSmi(Register object);
554
555   // Abort execution if argument is not a string, enabled via --debug-code.
556   void AssertString(Register object);
557
558   // Abort execution if argument is not a name, enabled via --debug-code.
559   void AssertName(Register object);
560
561   // Abort execution if argument is not undefined or an AllocationSite, enabled
562   // via --debug-code.
563   void AssertUndefinedOrAllocationSite(Register object);
564
565   // ---------------------------------------------------------------------------
566   // Exception handling
567
568   // Push a new try handler and link it into try handler chain.
569   void PushTryHandler(StackHandler::Kind kind, int handler_index);
570
571   // Unlink the stack handler on top of the stack from the try handler chain.
572   void PopTryHandler();
573
574   // Throw to the top handler in the try hander chain.
575   void Throw(Register value);
576
577   // Throw past all JS frames to the top JS entry frame.
578   void ThrowUncatchable(Register value);
579
580   // ---------------------------------------------------------------------------
581   // Inline caching support
582
583   // Generate code for checking access rights - used for security checks
584   // on access to global objects across environments. The holder register
585   // is left untouched, but the scratch register is clobbered.
586   void CheckAccessGlobalProxy(Register holder_reg,
587                               Register scratch1,
588                               Register scratch2,
589                               Label* miss);
590
591   void GetNumberHash(Register r0, Register scratch);
592
593   void LoadFromNumberDictionary(Label* miss,
594                                 Register elements,
595                                 Register key,
596                                 Register r0,
597                                 Register r1,
598                                 Register r2,
599                                 Register result);
600
601
602   // ---------------------------------------------------------------------------
603   // Allocation support
604
605   // Allocate an object in new space or old pointer space. If the given space
606   // is exhausted control continues at the gc_required label. The allocated
607   // object is returned in result and end of the new object is returned in
608   // result_end. The register scratch can be passed as no_reg in which case
609   // an additional object reference will be added to the reloc info. The
610   // returned pointers in result and result_end have not yet been tagged as
611   // heap objects. If result_contains_top_on_entry is true the content of
612   // result is known to be the allocation top on entry (could be result_end
613   // from a previous call). If result_contains_top_on_entry is true scratch
614   // should be no_reg as it is never used.
615   void Allocate(int object_size,
616                 Register result,
617                 Register result_end,
618                 Register scratch,
619                 Label* gc_required,
620                 AllocationFlags flags);
621
622   void Allocate(int header_size,
623                 ScaleFactor element_size,
624                 Register element_count,
625                 RegisterValueType element_count_type,
626                 Register result,
627                 Register result_end,
628                 Register scratch,
629                 Label* gc_required,
630                 AllocationFlags flags);
631
632   void Allocate(Register object_size,
633                 Register result,
634                 Register result_end,
635                 Register scratch,
636                 Label* gc_required,
637                 AllocationFlags flags);
638
639   // Undo allocation in new space. The object passed and objects allocated after
640   // it will no longer be allocated. Make sure that no pointers are left to the
641   // object(s) no longer allocated as they would be invalid when allocation is
642   // un-done.
643   void UndoAllocationInNewSpace(Register object);
644
645   // Allocate a heap number in new space with undefined value. The
646   // register scratch2 can be passed as no_reg; the others must be
647   // valid registers. Returns tagged pointer in result register, or
648   // jumps to gc_required if new space is full.
649   void AllocateHeapNumber(Register result,
650                           Register scratch1,
651                           Register scratch2,
652                           Label* gc_required,
653                           MutableMode mode = IMMUTABLE);
654
655   // Allocate a sequential string. All the header fields of the string object
656   // are initialized.
657   void AllocateTwoByteString(Register result,
658                              Register length,
659                              Register scratch1,
660                              Register scratch2,
661                              Register scratch3,
662                              Label* gc_required);
663   void AllocateOneByteString(Register result, Register length,
664                              Register scratch1, Register scratch2,
665                              Register scratch3, Label* gc_required);
666   void AllocateOneByteString(Register result, int length, Register scratch1,
667                              Register scratch2, Label* gc_required);
668
669   // Allocate a raw cons string object. Only the map field of the result is
670   // initialized.
671   void AllocateTwoByteConsString(Register result,
672                           Register scratch1,
673                           Register scratch2,
674                           Label* gc_required);
675   void AllocateOneByteConsString(Register result, Register scratch1,
676                                  Register scratch2, Label* gc_required);
677
678   // Allocate a raw sliced string object. Only the map field of the result is
679   // initialized.
680   void AllocateTwoByteSlicedString(Register result,
681                             Register scratch1,
682                             Register scratch2,
683                             Label* gc_required);
684   void AllocateOneByteSlicedString(Register result, Register scratch1,
685                                    Register scratch2, Label* gc_required);
686
687   // Copy memory, byte-by-byte, from source to destination.  Not optimized for
688   // long or aligned copies.
689   // The contents of index and scratch are destroyed.
690   void CopyBytes(Register source,
691                  Register destination,
692                  Register length,
693                  Register scratch);
694
695   // Initialize fields with filler values.  Fields starting at |start_offset|
696   // not including end_offset are overwritten with the value in |filler|.  At
697   // the end the loop, |start_offset| takes the value of |end_offset|.
698   void InitializeFieldsWithFiller(Register start_offset,
699                                   Register end_offset,
700                                   Register filler);
701
702   // ---------------------------------------------------------------------------
703   // Support functions.
704
705   // Check a boolean-bit of a Smi field.
706   void BooleanBitTest(Register object, int field_offset, int bit_index);
707
708   // Check if result is zero and op is negative.
709   void NegativeZeroTest(Register result, Register op, Label* then_label);
710
711   // Check if result is zero and any of op1 and op2 are negative.
712   // Register scratch is destroyed, and it must be different from op2.
713   void NegativeZeroTest(Register result, Register op1, Register op2,
714                         Register scratch, Label* then_label);
715
716   // Try to get function prototype of a function and puts the value in
717   // the result register. Checks that the function really is a
718   // function and jumps to the miss label if the fast checks fail. The
719   // function register will be untouched; the other registers may be
720   // clobbered.
721   void TryGetFunctionPrototype(Register function,
722                                Register result,
723                                Register scratch,
724                                Label* miss,
725                                bool miss_on_bound_function = false);
726
727   // Picks out an array index from the hash field.
728   // Register use:
729   //   hash - holds the index's hash. Clobbered.
730   //   index - holds the overwritten index on exit.
731   void IndexFromHash(Register hash, Register index);
732
733   // ---------------------------------------------------------------------------
734   // Runtime calls
735
736   // Call a code stub.  Generate the code if necessary.
737   void CallStub(CodeStub* stub, TypeFeedbackId ast_id = TypeFeedbackId::None());
738
739   // Tail call a code stub (jump).  Generate the code if necessary.
740   void TailCallStub(CodeStub* stub);
741
742   // Return from a code stub after popping its arguments.
743   void StubReturn(int argc);
744
745   // Call a runtime routine.
746   void CallRuntime(const Runtime::Function* f,
747                    int num_arguments,
748                    SaveFPRegsMode save_doubles = kDontSaveFPRegs);
749   void CallRuntimeSaveDoubles(Runtime::FunctionId id) {
750     const Runtime::Function* function = Runtime::FunctionForId(id);
751     CallRuntime(function, function->nargs, kSaveFPRegs);
752   }
753
754   // Convenience function: Same as above, but takes the fid instead.
755   void CallRuntime(Runtime::FunctionId id,
756                    int num_arguments,
757                    SaveFPRegsMode save_doubles = kDontSaveFPRegs) {
758     CallRuntime(Runtime::FunctionForId(id), num_arguments, save_doubles);
759   }
760
761   // Convenience function: call an external reference.
762   void CallExternalReference(ExternalReference ref, int num_arguments);
763
764   // Tail call of a runtime routine (jump).
765   // Like JumpToExternalReference, but also takes care of passing the number
766   // of parameters.
767   void TailCallExternalReference(const ExternalReference& ext,
768                                  int num_arguments,
769                                  int result_size);
770
771   // Convenience function: tail call a runtime routine (jump).
772   void TailCallRuntime(Runtime::FunctionId fid,
773                        int num_arguments,
774                        int result_size);
775
776   // Before calling a C-function from generated code, align arguments on stack.
777   // After aligning the frame, arguments must be stored in esp[0], esp[4],
778   // etc., not pushed. The argument count assumes all arguments are word sized.
779   // Some compilers/platforms require the stack to be aligned when calling
780   // C++ code.
781   // Needs a scratch register to do some arithmetic. This register will be
782   // trashed.
783   void PrepareCallCFunction(int num_arguments, Register scratch);
784
785   // Calls a C function and cleans up the space for arguments allocated
786   // by PrepareCallCFunction. The called function is not allowed to trigger a
787   // garbage collection, since that might move the code and invalidate the
788   // return address (unless this is somehow accounted for by the called
789   // function).
790   void CallCFunction(ExternalReference function, int num_arguments);
791   void CallCFunction(Register function, int num_arguments);
792
793   // Prepares stack to put arguments (aligns and so on). Reserves
794   // space for return value if needed (assumes the return value is a handle).
795   // Arguments must be stored in ApiParameterOperand(0), ApiParameterOperand(1)
796   // etc. Saves context (esi). If space was reserved for return value then
797   // stores the pointer to the reserved slot into esi.
798   void PrepareCallApiFunction(int argc);
799
800   // Calls an API function.  Allocates HandleScope, extracts returned value
801   // from handle and propagates exceptions.  Clobbers ebx, edi and
802   // caller-save registers.  Restores context.  On return removes
803   // stack_space * kPointerSize (GCed).
804   void CallApiFunctionAndReturn(Register function_address,
805                                 ExternalReference thunk_ref,
806                                 Operand thunk_last_arg,
807                                 int stack_space,
808                                 Operand return_value_operand,
809                                 Operand* context_restore_operand);
810
811   // Jump to a runtime routine.
812   void JumpToExternalReference(const ExternalReference& ext);
813
814   // ---------------------------------------------------------------------------
815   // Utilities
816
817   void Ret();
818
819   // Return and drop arguments from stack, where the number of arguments
820   // may be bigger than 2^16 - 1.  Requires a scratch register.
821   void Ret(int bytes_dropped, Register scratch);
822
823   // Emit code to discard a non-negative number of pointer-sized elements
824   // from the stack, clobbering only the esp register.
825   void Drop(int element_count);
826
827   void Call(Label* target) { call(target); }
828   void Push(Register src) { push(src); }
829   void Pop(Register dst) { pop(dst); }
830
831   // Emit call to the code we are currently generating.
832   void CallSelf() {
833     Handle<Code> self(reinterpret_cast<Code**>(CodeObject().location()));
834     call(self, RelocInfo::CODE_TARGET);
835   }
836
837   // Move if the registers are not identical.
838   void Move(Register target, Register source);
839
840   // Move a constant into a destination using the most efficient encoding.
841   void Move(Register dst, const Immediate& x);
842   void Move(const Operand& dst, const Immediate& x);
843
844   // Move an immediate into an XMM register.
845   void Move(XMMRegister dst, uint32_t src);
846   void Move(XMMRegister dst, uint64_t src);
847   void Move(XMMRegister dst, double src) { Move(dst, bit_cast<uint64_t>(src)); }
848
849   // Push a handle value.
850   void Push(Handle<Object> handle) { push(Immediate(handle)); }
851   void Push(Smi* smi) { Push(Handle<Smi>(smi, isolate())); }
852
853   Handle<Object> CodeObject() {
854     DCHECK(!code_object_.is_null());
855     return code_object_;
856   }
857
858   // Emit code for a truncating division by a constant. The dividend register is
859   // unchanged, the result is in edx, and eax gets clobbered.
860   void TruncatingDiv(Register dividend, int32_t divisor);
861
862   // ---------------------------------------------------------------------------
863   // StatsCounter support
864
865   void SetCounter(StatsCounter* counter, int value);
866   void IncrementCounter(StatsCounter* counter, int value);
867   void DecrementCounter(StatsCounter* counter, int value);
868   void IncrementCounter(Condition cc, StatsCounter* counter, int value);
869   void DecrementCounter(Condition cc, StatsCounter* counter, int value);
870
871
872   // ---------------------------------------------------------------------------
873   // Debugging
874
875   // Calls Abort(msg) if the condition cc is not satisfied.
876   // Use --debug_code to enable.
877   void Assert(Condition cc, BailoutReason reason);
878
879   void AssertFastElements(Register elements);
880
881   // Like Assert(), but always enabled.
882   void Check(Condition cc, BailoutReason reason);
883
884   // Print a message to stdout and abort execution.
885   void Abort(BailoutReason reason);
886
887   // Check that the stack is aligned.
888   void CheckStackAlignment();
889
890   // Verify restrictions about code generated in stubs.
891   void set_generating_stub(bool value) { generating_stub_ = value; }
892   bool generating_stub() { return generating_stub_; }
893   void set_has_frame(bool value) { has_frame_ = value; }
894   bool has_frame() { return has_frame_; }
895   inline bool AllowThisStubCall(CodeStub* stub);
896
897   // ---------------------------------------------------------------------------
898   // String utilities.
899
900   // Generate code to do a lookup in the number string cache. If the number in
901   // the register object is found in the cache the generated code falls through
902   // with the result in the result register. The object and the result register
903   // can be the same. If the number is not found in the cache the code jumps to
904   // the label not_found with only the content of register object unchanged.
905   void LookupNumberStringCache(Register object,
906                                Register result,
907                                Register scratch1,
908                                Register scratch2,
909                                Label* not_found);
910
911   // Check whether the instance type represents a flat one-byte string. Jump to
912   // the label if not. If the instance type can be scratched specify same
913   // register for both instance type and scratch.
914   void JumpIfInstanceTypeIsNotSequentialOneByte(
915       Register instance_type, Register scratch,
916       Label* on_not_flat_one_byte_string);
917
918   // Checks if both objects are sequential one-byte strings, and jumps to label
919   // if either is not.
920   void JumpIfNotBothSequentialOneByteStrings(
921       Register object1, Register object2, Register scratch1, Register scratch2,
922       Label* on_not_flat_one_byte_strings);
923
924   // Checks if the given register or operand is a unique name
925   void JumpIfNotUniqueNameInstanceType(Register reg, Label* not_unique_name,
926                                        Label::Distance distance = Label::kFar) {
927     JumpIfNotUniqueNameInstanceType(Operand(reg), not_unique_name, distance);
928   }
929
930   void JumpIfNotUniqueNameInstanceType(Operand operand, Label* not_unique_name,
931                                        Label::Distance distance = Label::kFar);
932
933   void EmitSeqStringSetCharCheck(Register string,
934                                  Register index,
935                                  Register value,
936                                  uint32_t encoding_mask);
937
938   static int SafepointRegisterStackIndex(Register reg) {
939     return SafepointRegisterStackIndex(reg.code());
940   }
941
942   // Activation support.
943   void EnterFrame(StackFrame::Type type);
944   void EnterFrame(StackFrame::Type type, bool load_constant_pool_pointer_reg);
945   void LeaveFrame(StackFrame::Type type);
946
947   // Expects object in eax and returns map with validated enum cache
948   // in eax.  Assumes that any other register can be used as a scratch.
949   void CheckEnumCache(Label* call_runtime);
950
951   // AllocationMemento support. Arrays may have an associated
952   // AllocationMemento object that can be checked for in order to pretransition
953   // to another type.
954   // On entry, receiver_reg should point to the array object.
955   // scratch_reg gets clobbered.
956   // If allocation info is present, conditional code is set to equal.
957   void TestJSArrayForAllocationMemento(Register receiver_reg,
958                                        Register scratch_reg,
959                                        Label* no_memento_found);
960
961   void JumpIfJSArrayHasAllocationMemento(Register receiver_reg,
962                                          Register scratch_reg,
963                                          Label* memento_found) {
964     Label no_memento_found;
965     TestJSArrayForAllocationMemento(receiver_reg, scratch_reg,
966                                     &no_memento_found);
967     j(equal, memento_found);
968     bind(&no_memento_found);
969   }
970
971   // Jumps to found label if a prototype map has dictionary elements.
972   void JumpIfDictionaryInPrototypeChain(Register object, Register scratch0,
973                                         Register scratch1, Label* found);
974
975  private:
976   bool generating_stub_;
977   bool has_frame_;
978   // This handle will be patched with the code object on installation.
979   Handle<Object> code_object_;
980
981   // Helper functions for generating invokes.
982   void InvokePrologue(const ParameterCount& expected,
983                       const ParameterCount& actual,
984                       Handle<Code> code_constant,
985                       const Operand& code_operand,
986                       Label* done,
987                       bool* definitely_mismatches,
988                       InvokeFlag flag,
989                       Label::Distance done_distance,
990                       const CallWrapper& call_wrapper = NullCallWrapper());
991
992   void EnterExitFramePrologue();
993   void EnterExitFrameEpilogue(int argc, bool save_doubles);
994
995   void LeaveExitFrameEpilogue(bool restore_context);
996
997   // Allocation support helpers.
998   void LoadAllocationTopHelper(Register result,
999                                Register scratch,
1000                                AllocationFlags flags);
1001
1002   void UpdateAllocationTopHelper(Register result_end,
1003                                  Register scratch,
1004                                  AllocationFlags flags);
1005
1006   // Helper for implementing JumpIfNotInNewSpace and JumpIfInNewSpace.
1007   void InNewSpace(Register object,
1008                   Register scratch,
1009                   Condition cc,
1010                   Label* condition_met,
1011                   Label::Distance condition_met_distance = Label::kFar);
1012
1013   // Helper for finding the mark bits for an address.  Afterwards, the
1014   // bitmap register points at the word with the mark bits and the mask
1015   // the position of the first bit.  Uses ecx as scratch and leaves addr_reg
1016   // unchanged.
1017   inline void GetMarkBits(Register addr_reg,
1018                           Register bitmap_reg,
1019                           Register mask_reg);
1020
1021   // Helper for throwing exceptions.  Compute a handler address and jump to
1022   // it.  See the implementation for register usage.
1023   void JumpToHandlerEntry();
1024
1025   // Compute memory operands for safepoint stack slots.
1026   Operand SafepointRegisterSlot(Register reg);
1027   static int SafepointRegisterStackIndex(int reg_code);
1028
1029   // Needs access to SafepointRegisterStackIndex for compiled frame
1030   // traversal.
1031   friend class StandardFrame;
1032 };
1033
1034
1035 // The code patcher is used to patch (typically) small parts of code e.g. for
1036 // debugging and other types of instrumentation. When using the code patcher
1037 // the exact number of bytes specified must be emitted. Is not legal to emit
1038 // relocation information. If any of these constraints are violated it causes
1039 // an assertion.
1040 class CodePatcher {
1041  public:
1042   CodePatcher(byte* address, int size);
1043   virtual ~CodePatcher();
1044
1045   // Macro assembler to emit code.
1046   MacroAssembler* masm() { return &masm_; }
1047
1048  private:
1049   byte* address_;  // The address of the code being patched.
1050   int size_;  // Number of bytes of the expected patch size.
1051   MacroAssembler masm_;  // Macro assembler used to generate the code.
1052 };
1053
1054
1055 // -----------------------------------------------------------------------------
1056 // Static helper functions.
1057
1058 // Generate an Operand for loading a field from an object.
1059 inline Operand FieldOperand(Register object, int offset) {
1060   return Operand(object, offset - kHeapObjectTag);
1061 }
1062
1063
1064 // Generate an Operand for loading an indexed field from an object.
1065 inline Operand FieldOperand(Register object,
1066                             Register index,
1067                             ScaleFactor scale,
1068                             int offset) {
1069   return Operand(object, index, scale, offset - kHeapObjectTag);
1070 }
1071
1072
1073 inline Operand FixedArrayElementOperand(Register array,
1074                                         Register index_as_smi,
1075                                         int additional_offset = 0) {
1076   int offset = FixedArray::kHeaderSize + additional_offset * kPointerSize;
1077   return FieldOperand(array, index_as_smi, times_half_pointer_size, offset);
1078 }
1079
1080
1081 inline Operand ContextOperand(Register context, int index) {
1082   return Operand(context, Context::SlotOffset(index));
1083 }
1084
1085
1086 inline Operand GlobalObjectOperand() {
1087   return ContextOperand(esi, Context::GLOBAL_OBJECT_INDEX);
1088 }
1089
1090
1091 // Generates an Operand for saving parameters after PrepareCallApiFunction.
1092 Operand ApiParameterOperand(int index);
1093
1094
1095 #ifdef GENERATED_CODE_COVERAGE
1096 extern void LogGeneratedCodeCoverage(const char* file_line);
1097 #define CODE_COVERAGE_STRINGIFY(x) #x
1098 #define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
1099 #define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
1100 #define ACCESS_MASM(masm) {                                               \
1101     byte* ia32_coverage_function =                                        \
1102         reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
1103     masm->pushfd();                                                       \
1104     masm->pushad();                                                       \
1105     masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__)));         \
1106     masm->call(ia32_coverage_function, RelocInfo::RUNTIME_ENTRY);         \
1107     masm->pop(eax);                                                       \
1108     masm->popad();                                                        \
1109     masm->popfd();                                                        \
1110   }                                                                       \
1111   masm->
1112 #else
1113 #define ACCESS_MASM(masm) masm->
1114 #endif
1115
1116
1117 } }  // namespace v8::internal
1118
1119 #endif  // V8_IA32_MACRO_ASSEMBLER_IA32_H_