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.
5 #ifndef V8_IA32_MACRO_ASSEMBLER_IA32_H_
6 #define V8_IA32_MACRO_ASSEMBLER_IA32_H_
8 #include "src/assembler.h"
9 #include "src/bailout-reason.h"
10 #include "src/frames.h"
11 #include "src/globals.h"
16 // Give alias names to registers for calling conventions.
17 const Register kReturnRegister0 = {kRegister_eax_Code};
18 const Register kReturnRegister1 = {kRegister_edx_Code};
19 const Register kJSFunctionRegister = {kRegister_edi_Code};
20 const Register kContextRegister = {kRegister_esi_Code};
21 const Register kInterpreterAccumulatorRegister = {kRegister_eax_Code};
22 const Register kInterpreterRegisterFileRegister = {kRegister_edx_Code};
23 const Register kInterpreterBytecodeOffsetRegister = {kRegister_ecx_Code};
24 const Register kInterpreterBytecodeArrayRegister = {kRegister_edi_Code};
25 const Register kInterpreterDispatchTableRegister = {kRegister_ebx_Code};
26 const Register kRuntimeCallFunctionRegister = {kRegister_ebx_Code};
27 const Register kRuntimeCallArgCountRegister = {kRegister_eax_Code};
29 // Spill slots used by interpreter dispatch calling convention.
30 const int kInterpreterContextSpillSlot = -1;
32 // Convenience for platform-independent signatures. We do not normally
33 // distinguish memory operands from other operands on ia32.
34 typedef Operand MemOperand;
36 enum RememberedSetAction { EMIT_REMEMBERED_SET, OMIT_REMEMBERED_SET };
37 enum SmiCheck { INLINE_SMI_CHECK, OMIT_SMI_CHECK };
38 enum PointersToHereCheck {
39 kPointersToHereMaybeInteresting,
40 kPointersToHereAreAlwaysInteresting
44 enum RegisterValueType {
45 REGISTER_VALUE_IS_SMI,
46 REGISTER_VALUE_IS_INT32
51 bool AreAliased(Register reg1,
53 Register reg3 = no_reg,
54 Register reg4 = no_reg,
55 Register reg5 = no_reg,
56 Register reg6 = no_reg,
57 Register reg7 = no_reg,
58 Register reg8 = no_reg);
62 // MacroAssembler implements a collection of frequently used macros.
63 class MacroAssembler: public Assembler {
65 // The isolate parameter can be NULL if the macro assembler should
66 // not use isolate-dependent functionality. In this case, it's the
67 // responsibility of the caller to never invoke such function on the
69 MacroAssembler(Isolate* isolate, void* buffer, int size);
71 void Load(Register dst, const Operand& src, Representation r);
72 void Store(Register src, const Operand& dst, Representation r);
74 // Operations on roots in the root-array.
75 void LoadRoot(Register destination, Heap::RootListIndex index);
76 void StoreRoot(Register source, Register scratch, Heap::RootListIndex index);
77 void CompareRoot(Register with, Register scratch, Heap::RootListIndex index);
78 // These methods can only be used with constant roots (i.e. non-writable
79 // and not in new space).
80 void CompareRoot(Register with, Heap::RootListIndex index);
81 void CompareRoot(const Operand& with, Heap::RootListIndex index);
83 // ---------------------------------------------------------------------------
85 enum RememberedSetFinalAction {
90 // Record in the remembered set the fact that we have a pointer to new space
91 // at the address pointed to by the addr register. Only works if addr is not
93 void RememberedSetHelper(Register object, // Used for debug code.
96 SaveFPRegsMode save_fp,
97 RememberedSetFinalAction and_then);
99 void CheckPageFlag(Register object,
103 Label* condition_met,
104 Label::Distance condition_met_distance = Label::kFar);
106 void CheckPageFlagForMap(
110 Label* condition_met,
111 Label::Distance condition_met_distance = Label::kFar);
113 // Check if object is in new space. Jumps if the object is not in new space.
114 // The register scratch can be object itself, but scratch will be clobbered.
115 void JumpIfNotInNewSpace(Register object,
118 Label::Distance distance = Label::kFar) {
119 InNewSpace(object, scratch, zero, branch, distance);
122 // Check if object is in new space. Jumps if the object is in new space.
123 // The register scratch can be object itself, but it will be clobbered.
124 void JumpIfInNewSpace(Register object,
127 Label::Distance distance = Label::kFar) {
128 InNewSpace(object, scratch, not_zero, branch, distance);
131 // Check if an object has a given incremental marking color. Also uses ecx!
132 void HasColor(Register object,
136 Label::Distance has_color_distance,
140 void JumpIfBlack(Register object,
144 Label::Distance on_black_distance = Label::kFar);
146 // Checks the color of an object. If the object is already grey or black
147 // then we just fall through, since it is already live. If it is white and
148 // we can determine that it doesn't need to be scanned, then we just mark it
149 // black and fall through. For the rest we jump to the label so the
150 // incremental marker can fix its assumptions.
151 void EnsureNotWhite(Register object,
154 Label* object_is_white_and_not_data,
155 Label::Distance distance);
157 // Notify the garbage collector that we wrote a pointer into an object.
158 // |object| is the object being stored into, |value| is the object being
159 // stored. value and scratch registers are clobbered by the operation.
160 // The offset is the offset from the start of the object, not the offset from
161 // the tagged HeapObject pointer. For use with FieldOperand(reg, off).
162 void RecordWriteField(
167 SaveFPRegsMode save_fp,
168 RememberedSetAction remembered_set_action = EMIT_REMEMBERED_SET,
169 SmiCheck smi_check = INLINE_SMI_CHECK,
170 PointersToHereCheck pointers_to_here_check_for_value =
171 kPointersToHereMaybeInteresting);
173 // As above, but the offset has the tag presubtracted. For use with
174 // Operand(reg, off).
175 void RecordWriteContextSlot(
180 SaveFPRegsMode save_fp,
181 RememberedSetAction remembered_set_action = EMIT_REMEMBERED_SET,
182 SmiCheck smi_check = INLINE_SMI_CHECK,
183 PointersToHereCheck pointers_to_here_check_for_value =
184 kPointersToHereMaybeInteresting) {
185 RecordWriteField(context,
186 offset + kHeapObjectTag,
190 remembered_set_action,
192 pointers_to_here_check_for_value);
195 // Notify the garbage collector that we wrote a pointer into a fixed array.
196 // |array| is the array being stored into, |value| is the
197 // object being stored. |index| is the array index represented as a
198 // Smi. All registers are clobbered by the operation RecordWriteArray
199 // filters out smis so it does not update the write barrier if the
201 void RecordWriteArray(
205 SaveFPRegsMode save_fp,
206 RememberedSetAction remembered_set_action = EMIT_REMEMBERED_SET,
207 SmiCheck smi_check = INLINE_SMI_CHECK,
208 PointersToHereCheck pointers_to_here_check_for_value =
209 kPointersToHereMaybeInteresting);
211 // For page containing |object| mark region covering |address|
212 // dirty. |object| is the object being stored into, |value| is the
213 // object being stored. The address and value registers are clobbered by the
214 // operation. RecordWrite filters out smis so it does not update the
215 // write barrier if the value is a smi.
220 SaveFPRegsMode save_fp,
221 RememberedSetAction remembered_set_action = EMIT_REMEMBERED_SET,
222 SmiCheck smi_check = INLINE_SMI_CHECK,
223 PointersToHereCheck pointers_to_here_check_for_value =
224 kPointersToHereMaybeInteresting);
226 // For page containing |object| mark the region covering the object's map
227 // dirty. |object| is the object being stored into, |map| is the Map object
229 void RecordWriteForMap(
234 SaveFPRegsMode save_fp);
236 // ---------------------------------------------------------------------------
241 // Generates function and stub prologue code.
243 void Prologue(bool code_pre_aging);
245 // Enter specific kind of exit frame. Expects the number of
246 // arguments in register eax and sets up the number of arguments in
247 // register edi and the pointer to the first argument in register
249 void EnterExitFrame(bool save_doubles);
251 void EnterApiExitFrame(int argc);
253 // Leave the current exit frame. Expects the return value in
254 // register eax:edx (untouched) and the pointer to the first
255 // argument in register esi.
256 void LeaveExitFrame(bool save_doubles);
258 // Leave the current exit frame. Expects the return value in
259 // register eax (untouched).
260 void LeaveApiExitFrame(bool restore_context);
262 // Find the function context up the context chain.
263 void LoadContext(Register dst, int context_chain_length);
265 // Conditionally load the cached Array transitioned map of type
266 // transitioned_kind from the native context if the map in register
267 // map_in_out is the cached Array map in the native context of
269 void LoadTransitionedArrayMapConditional(
270 ElementsKind expected_kind,
271 ElementsKind transitioned_kind,
274 Label* no_map_match);
276 // Load the global function with the given index.
277 void LoadGlobalFunction(int index, Register function);
279 // Load the initial map from the global function. The registers
280 // function and map can be the same.
281 void LoadGlobalFunctionInitialMap(Register function, Register map);
283 // Push and pop the registers that can hold pointers.
284 void PushSafepointRegisters() { pushad(); }
285 void PopSafepointRegisters() { popad(); }
286 // Store the value in register/immediate src in the safepoint
287 // register stack slot for register dst.
288 void StoreToSafepointRegisterSlot(Register dst, Register src);
289 void StoreToSafepointRegisterSlot(Register dst, Immediate src);
290 void LoadFromSafepointRegisterSlot(Register dst, Register src);
292 void LoadHeapObject(Register result, Handle<HeapObject> object);
293 void CmpHeapObject(Register reg, Handle<HeapObject> object);
294 void PushHeapObject(Handle<HeapObject> object);
296 void LoadObject(Register result, Handle<Object> object) {
297 AllowDeferredHandleDereference heap_object_check;
298 if (object->IsHeapObject()) {
299 LoadHeapObject(result, Handle<HeapObject>::cast(object));
301 Move(result, Immediate(object));
305 void CmpObject(Register reg, Handle<Object> object) {
306 AllowDeferredHandleDereference heap_object_check;
307 if (object->IsHeapObject()) {
308 CmpHeapObject(reg, Handle<HeapObject>::cast(object));
310 cmp(reg, Immediate(object));
314 // Compare the given value and the value of weak cell.
315 void CmpWeakValue(Register value, Handle<WeakCell> cell, Register scratch);
317 void GetWeakValue(Register value, Handle<WeakCell> cell);
319 // Load the value of the weak cell in the value register. Branch to the given
320 // miss label if the weak cell was cleared.
321 void LoadWeakValue(Register value, Handle<WeakCell> cell, Label* miss);
323 // ---------------------------------------------------------------------------
324 // JavaScript invokes
326 // Invoke the JavaScript function code by either calling or jumping.
327 void InvokeCode(Register code,
328 const ParameterCount& expected,
329 const ParameterCount& actual,
331 const CallWrapper& call_wrapper) {
332 InvokeCode(Operand(code), expected, actual, flag, call_wrapper);
335 void InvokeCode(const Operand& code,
336 const ParameterCount& expected,
337 const ParameterCount& actual,
339 const CallWrapper& call_wrapper);
341 // Invoke the JavaScript function in the given register. Changes the
342 // current context to the context in the function before invoking.
343 void InvokeFunction(Register function,
344 const ParameterCount& actual,
346 const CallWrapper& call_wrapper);
348 void InvokeFunction(Register function,
349 const ParameterCount& expected,
350 const ParameterCount& actual,
352 const CallWrapper& call_wrapper);
354 void InvokeFunction(Handle<JSFunction> function,
355 const ParameterCount& expected,
356 const ParameterCount& actual,
358 const CallWrapper& call_wrapper);
360 // Invoke specified builtin JavaScript function.
361 void InvokeBuiltin(int native_context_index, InvokeFlag flag,
362 const CallWrapper& call_wrapper = NullCallWrapper());
364 // Store the function for the given builtin in the target register.
365 void GetBuiltinFunction(Register target, int native_context_index);
367 // Store the code object for the given builtin in the target register.
368 void GetBuiltinEntry(Register target, int native_context_index);
370 // Expression support
371 // cvtsi2sd instruction only writes to the low 64-bit of dst register, which
372 // hinders register renaming and makes dependence chains longer. So we use
373 // xorps to clear the dst register before cvtsi2sd to solve this issue.
374 void Cvtsi2sd(XMMRegister dst, Register src) { Cvtsi2sd(dst, Operand(src)); }
375 void Cvtsi2sd(XMMRegister dst, const Operand& src);
377 // Support for constant splitting.
378 bool IsUnsafeImmediate(const Immediate& x);
379 void SafeMove(Register dst, const Immediate& x);
380 void SafePush(const Immediate& x);
382 // Compare object type for heap object.
383 // Incoming register is heap_object and outgoing register is map.
384 void CmpObjectType(Register heap_object, InstanceType type, Register map);
386 // Compare instance type for map.
387 void CmpInstanceType(Register map, InstanceType type);
389 // Check if a map for a JSObject indicates that the object has fast elements.
390 // Jump to the specified label if it does not.
391 void CheckFastElements(Register map,
393 Label::Distance distance = Label::kFar);
395 // Check if a map for a JSObject indicates that the object can have both smi
396 // and HeapObject elements. Jump to the specified label if it does not.
397 void CheckFastObjectElements(Register map,
399 Label::Distance distance = Label::kFar);
401 // Check if a map for a JSObject indicates that the object has fast smi only
402 // elements. Jump to the specified label if it does not.
403 void CheckFastSmiElements(Register map,
405 Label::Distance distance = Label::kFar);
407 // Check to see if maybe_number can be stored as a double in
408 // FastDoubleElements. If it can, store it at the index specified by key in
409 // the FastDoubleElements array elements, otherwise jump to fail.
410 void StoreNumberToDoubleElements(Register maybe_number,
414 XMMRegister scratch2,
418 // Compare an object's map with the specified map.
419 void CompareMap(Register obj, Handle<Map> map);
421 // Check if the map of an object is equal to a specified map and branch to
422 // label if not. Skip the smi check if not required (object is known to be a
423 // heap object). If mode is ALLOW_ELEMENT_TRANSITION_MAPS, then also match
424 // against maps that are ElementsKind transition maps of the specified map.
425 void CheckMap(Register obj,
428 SmiCheckType smi_check_type);
430 // Check if the map of an object is equal to a specified weak map and branch
431 // to a specified target if equal. Skip the smi check if not required
432 // (object is known to be a heap object)
433 void DispatchWeakMap(Register obj, Register scratch1, Register scratch2,
434 Handle<WeakCell> cell, Handle<Code> success,
435 SmiCheckType smi_check_type);
437 // Check if the object in register heap_object is a string. Afterwards the
438 // register map contains the object map and the register instance_type
439 // contains the instance_type. The registers map and instance_type can be the
440 // same in which case it contains the instance type afterwards. Either of the
441 // registers map and instance_type can be the same as heap_object.
442 Condition IsObjectStringType(Register heap_object,
444 Register instance_type);
446 // Check if the object in register heap_object is a name. Afterwards the
447 // register map contains the object map and the register instance_type
448 // contains the instance_type. The registers map and instance_type can be the
449 // same in which case it contains the instance type afterwards. Either of the
450 // registers map and instance_type can be the same as heap_object.
451 Condition IsObjectNameType(Register heap_object,
453 Register instance_type);
455 // FCmp is similar to integer cmp, but requires unsigned
456 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
459 void ClampUint8(Register reg);
461 void ClampDoubleToUint8(XMMRegister input_reg,
462 XMMRegister scratch_reg,
463 Register result_reg);
465 void SlowTruncateToI(Register result_reg, Register input_reg,
466 int offset = HeapNumber::kValueOffset - kHeapObjectTag);
468 void TruncateHeapNumberToI(Register result_reg, Register input_reg);
469 void TruncateDoubleToI(Register result_reg, XMMRegister input_reg);
471 void DoubleToI(Register result_reg, XMMRegister input_reg,
472 XMMRegister scratch, MinusZeroMode minus_zero_mode,
473 Label* lost_precision, Label* is_nan, Label* minus_zero,
474 Label::Distance dst = Label::kFar);
476 // Smi tagging support.
477 void SmiTag(Register reg) {
478 STATIC_ASSERT(kSmiTag == 0);
479 STATIC_ASSERT(kSmiTagSize == 1);
482 void SmiUntag(Register reg) {
483 sar(reg, kSmiTagSize);
486 // Modifies the register even if it does not contain a Smi!
487 void SmiUntag(Register reg, Label* is_smi) {
488 STATIC_ASSERT(kSmiTagSize == 1);
489 sar(reg, kSmiTagSize);
490 STATIC_ASSERT(kSmiTag == 0);
491 j(not_carry, is_smi);
494 void LoadUint32(XMMRegister dst, Register src) {
495 LoadUint32(dst, Operand(src));
497 void LoadUint32(XMMRegister dst, const Operand& src);
499 // Jump the register contains a smi.
500 inline void JumpIfSmi(Register value,
502 Label::Distance distance = Label::kFar) {
503 test(value, Immediate(kSmiTagMask));
504 j(zero, smi_label, distance);
506 // Jump if the operand is a smi.
507 inline void JumpIfSmi(Operand value,
509 Label::Distance distance = Label::kFar) {
510 test(value, Immediate(kSmiTagMask));
511 j(zero, smi_label, distance);
513 // Jump if register contain a non-smi.
514 inline void JumpIfNotSmi(Register value,
515 Label* not_smi_label,
516 Label::Distance distance = Label::kFar) {
517 test(value, Immediate(kSmiTagMask));
518 j(not_zero, not_smi_label, distance);
521 void LoadInstanceDescriptors(Register map, Register descriptors);
522 void EnumLength(Register dst, Register map);
523 void NumberOfOwnDescriptors(Register dst, Register map);
524 void LoadAccessor(Register dst, Register holder, int accessor_index,
525 AccessorComponent accessor);
527 template<typename Field>
528 void DecodeField(Register reg) {
529 static const int shift = Field::kShift;
530 static const int mask = Field::kMask >> Field::kShift;
534 and_(reg, Immediate(mask));
537 template<typename Field>
538 void DecodeFieldToSmi(Register reg) {
539 static const int shift = Field::kShift;
540 static const int mask = (Field::kMask >> Field::kShift) << kSmiTagSize;
541 STATIC_ASSERT((mask & (0x80000000u >> (kSmiTagSize - 1))) == 0);
542 STATIC_ASSERT(kSmiTag == 0);
543 if (shift < kSmiTagSize) {
544 shl(reg, kSmiTagSize - shift);
545 } else if (shift > kSmiTagSize) {
546 sar(reg, shift - kSmiTagSize);
548 and_(reg, Immediate(mask));
551 void LoadPowerOf2(XMMRegister dst, Register scratch, int power);
553 // Abort execution if argument is not a number, enabled via --debug-code.
554 void AssertNumber(Register object);
556 // Abort execution if argument is not a smi, enabled via --debug-code.
557 void AssertSmi(Register object);
559 // Abort execution if argument is a smi, enabled via --debug-code.
560 void AssertNotSmi(Register object);
562 // Abort execution if argument is not a string, enabled via --debug-code.
563 void AssertString(Register object);
565 // Abort execution if argument is not a name, enabled via --debug-code.
566 void AssertName(Register object);
568 // Abort execution if argument is not undefined or an AllocationSite, enabled
570 void AssertUndefinedOrAllocationSite(Register object);
572 // ---------------------------------------------------------------------------
573 // Exception handling
575 // Push a new stack handler and link it into stack handler chain.
576 void PushStackHandler();
578 // Unlink the stack handler on top of the stack from the stack handler chain.
579 void PopStackHandler();
581 // ---------------------------------------------------------------------------
582 // Inline caching support
584 // Generate code for checking access rights - used for security checks
585 // on access to global objects across environments. The holder register
586 // is left untouched, but the scratch register is clobbered.
587 void CheckAccessGlobalProxy(Register holder_reg,
592 void GetNumberHash(Register r0, Register scratch);
594 void LoadFromNumberDictionary(Label* miss,
603 // ---------------------------------------------------------------------------
604 // Allocation support
606 // Allocate an object in new space or old space. If the given space
607 // is exhausted control continues at the gc_required label. The allocated
608 // object is returned in result and end of the new object is returned in
609 // result_end. The register scratch can be passed as no_reg in which case
610 // an additional object reference will be added to the reloc info. The
611 // returned pointers in result and result_end have not yet been tagged as
612 // heap objects. If result_contains_top_on_entry is true the content of
613 // result is known to be the allocation top on entry (could be result_end
614 // from a previous call). If result_contains_top_on_entry is true scratch
615 // should be no_reg as it is never used.
616 void Allocate(int object_size,
621 AllocationFlags flags);
623 void Allocate(int header_size,
624 ScaleFactor element_size,
625 Register element_count,
626 RegisterValueType element_count_type,
631 AllocationFlags flags);
633 void Allocate(Register object_size,
638 AllocationFlags flags);
640 // Allocate a heap number in new space with undefined value. The
641 // register scratch2 can be passed as no_reg; the others must be
642 // valid registers. Returns tagged pointer in result register, or
643 // jumps to gc_required if new space is full.
644 void AllocateHeapNumber(Register result,
648 MutableMode mode = IMMUTABLE);
650 // Allocate a sequential string. All the header fields of the string object
652 void AllocateTwoByteString(Register result,
658 void AllocateOneByteString(Register result, Register length,
659 Register scratch1, Register scratch2,
660 Register scratch3, Label* gc_required);
661 void AllocateOneByteString(Register result, int length, Register scratch1,
662 Register scratch2, Label* gc_required);
664 // Allocate a raw cons string object. Only the map field of the result is
666 void AllocateTwoByteConsString(Register result,
670 void AllocateOneByteConsString(Register result, Register scratch1,
671 Register scratch2, Label* gc_required);
673 // Allocate a raw sliced string object. Only the map field of the result is
675 void AllocateTwoByteSlicedString(Register result,
679 void AllocateOneByteSlicedString(Register result, Register scratch1,
680 Register scratch2, Label* gc_required);
682 // Copy memory, byte-by-byte, from source to destination. Not optimized for
683 // long or aligned copies.
684 // The contents of index and scratch are destroyed.
685 void CopyBytes(Register source,
686 Register destination,
690 // Initialize fields with filler values. Fields starting at |start_offset|
691 // not including end_offset are overwritten with the value in |filler|. At
692 // the end the loop, |start_offset| takes the value of |end_offset|.
693 void InitializeFieldsWithFiller(Register start_offset,
697 // ---------------------------------------------------------------------------
698 // Support functions.
700 // Check a boolean-bit of a Smi field.
701 void BooleanBitTest(Register object, int field_offset, int bit_index);
703 // Check if result is zero and op is negative.
704 void NegativeZeroTest(Register result, Register op, Label* then_label);
706 // Check if result is zero and any of op1 and op2 are negative.
707 // Register scratch is destroyed, and it must be different from op2.
708 void NegativeZeroTest(Register result, Register op1, Register op2,
709 Register scratch, Label* then_label);
711 // Machine code version of Map::GetConstructor().
712 // |temp| holds |result|'s map when done.
713 void GetMapConstructor(Register result, Register map, Register temp);
715 // Try to get function prototype of a function and puts the value in
716 // the result register. Checks that the function really is a
717 // function and jumps to the miss label if the fast checks fail. The
718 // function register will be untouched; the other registers may be
720 void TryGetFunctionPrototype(Register function, Register result,
721 Register scratch, Label* miss);
723 // Picks out an array index from the hash field.
725 // hash - holds the index's hash. Clobbered.
726 // index - holds the overwritten index on exit.
727 void IndexFromHash(Register hash, Register index);
729 // ---------------------------------------------------------------------------
732 // Call a code stub. Generate the code if necessary.
733 void CallStub(CodeStub* stub, TypeFeedbackId ast_id = TypeFeedbackId::None());
735 // Tail call a code stub (jump). Generate the code if necessary.
736 void TailCallStub(CodeStub* stub);
738 // Return from a code stub after popping its arguments.
739 void StubReturn(int argc);
741 // Call a runtime routine.
742 void CallRuntime(const Runtime::Function* f,
744 SaveFPRegsMode save_doubles = kDontSaveFPRegs);
745 void CallRuntimeSaveDoubles(Runtime::FunctionId id) {
746 const Runtime::Function* function = Runtime::FunctionForId(id);
747 CallRuntime(function, function->nargs, kSaveFPRegs);
750 // Convenience function: Same as above, but takes the fid instead.
751 void CallRuntime(Runtime::FunctionId id,
753 SaveFPRegsMode save_doubles = kDontSaveFPRegs) {
754 CallRuntime(Runtime::FunctionForId(id), num_arguments, save_doubles);
757 // Convenience function: call an external reference.
758 void CallExternalReference(ExternalReference ref, int num_arguments);
760 // Tail call of a runtime routine (jump).
761 // Like JumpToExternalReference, but also takes care of passing the number
763 void TailCallExternalReference(const ExternalReference& ext,
767 // Convenience function: tail call a runtime routine (jump).
768 void TailCallRuntime(Runtime::FunctionId fid,
772 // Before calling a C-function from generated code, align arguments on stack.
773 // After aligning the frame, arguments must be stored in esp[0], esp[4],
774 // etc., not pushed. The argument count assumes all arguments are word sized.
775 // Some compilers/platforms require the stack to be aligned when calling
777 // Needs a scratch register to do some arithmetic. This register will be
779 void PrepareCallCFunction(int num_arguments, Register scratch);
781 // Calls a C function and cleans up the space for arguments allocated
782 // by PrepareCallCFunction. The called function is not allowed to trigger a
783 // garbage collection, since that might move the code and invalidate the
784 // return address (unless this is somehow accounted for by the called
786 void CallCFunction(ExternalReference function, int num_arguments);
787 void CallCFunction(Register function, int num_arguments);
789 // Jump to a runtime routine.
790 void JumpToExternalReference(const ExternalReference& ext);
792 // ---------------------------------------------------------------------------
797 // Return and drop arguments from stack, where the number of arguments
798 // may be bigger than 2^16 - 1. Requires a scratch register.
799 void Ret(int bytes_dropped, Register scratch);
801 // Emit code to discard a non-negative number of pointer-sized elements
802 // from the stack, clobbering only the esp register.
803 void Drop(int element_count);
805 void Call(Label* target) { call(target); }
806 void Push(Register src) { push(src); }
807 void Pop(Register dst) { pop(dst); }
809 // Non-SSE2 instructions.
810 void Pextrd(Register dst, XMMRegister src, int8_t imm8);
811 void Pinsrd(XMMRegister dst, Register src, int8_t imm8) {
812 Pinsrd(dst, Operand(src), imm8);
814 void Pinsrd(XMMRegister dst, const Operand& src, int8_t imm8);
816 void Lzcnt(Register dst, Register src) { Lzcnt(dst, Operand(src)); }
817 void Lzcnt(Register dst, const Operand& src);
819 // Emit call to the code we are currently generating.
821 Handle<Code> self(reinterpret_cast<Code**>(CodeObject().location()));
822 call(self, RelocInfo::CODE_TARGET);
825 // Move if the registers are not identical.
826 void Move(Register target, Register source);
828 // Move a constant into a destination using the most efficient encoding.
829 void Move(Register dst, const Immediate& x);
830 void Move(const Operand& dst, const Immediate& x);
832 // Move an immediate into an XMM register.
833 void Move(XMMRegister dst, uint32_t src);
834 void Move(XMMRegister dst, uint64_t src);
835 void Move(XMMRegister dst, double src) { Move(dst, bit_cast<uint64_t>(src)); }
837 // Push a handle value.
838 void Push(Handle<Object> handle) { push(Immediate(handle)); }
839 void Push(Smi* smi) { Push(Handle<Smi>(smi, isolate())); }
841 Handle<Object> CodeObject() {
842 DCHECK(!code_object_.is_null());
846 // Emit code for a truncating division by a constant. The dividend register is
847 // unchanged, the result is in edx, and eax gets clobbered.
848 void TruncatingDiv(Register dividend, int32_t divisor);
850 // ---------------------------------------------------------------------------
851 // StatsCounter support
853 void SetCounter(StatsCounter* counter, int value);
854 void IncrementCounter(StatsCounter* counter, int value);
855 void DecrementCounter(StatsCounter* counter, int value);
856 void IncrementCounter(Condition cc, StatsCounter* counter, int value);
857 void DecrementCounter(Condition cc, StatsCounter* counter, int value);
860 // ---------------------------------------------------------------------------
863 // Calls Abort(msg) if the condition cc is not satisfied.
864 // Use --debug_code to enable.
865 void Assert(Condition cc, BailoutReason reason);
867 void AssertFastElements(Register elements);
869 // Like Assert(), but always enabled.
870 void Check(Condition cc, BailoutReason reason);
872 // Print a message to stdout and abort execution.
873 void Abort(BailoutReason reason);
875 // Check that the stack is aligned.
876 void CheckStackAlignment();
878 // Verify restrictions about code generated in stubs.
879 void set_generating_stub(bool value) { generating_stub_ = value; }
880 bool generating_stub() { return generating_stub_; }
881 void set_has_frame(bool value) { has_frame_ = value; }
882 bool has_frame() { return has_frame_; }
883 inline bool AllowThisStubCall(CodeStub* stub);
885 // ---------------------------------------------------------------------------
888 // Generate code to do a lookup in the number string cache. If the number in
889 // the register object is found in the cache the generated code falls through
890 // with the result in the result register. The object and the result register
891 // can be the same. If the number is not found in the cache the code jumps to
892 // the label not_found with only the content of register object unchanged.
893 void LookupNumberStringCache(Register object,
899 // Check whether the instance type represents a flat one-byte string. Jump to
900 // the label if not. If the instance type can be scratched specify same
901 // register for both instance type and scratch.
902 void JumpIfInstanceTypeIsNotSequentialOneByte(
903 Register instance_type, Register scratch,
904 Label* on_not_flat_one_byte_string);
906 // Checks if both objects are sequential one-byte strings, and jumps to label
908 void JumpIfNotBothSequentialOneByteStrings(
909 Register object1, Register object2, Register scratch1, Register scratch2,
910 Label* on_not_flat_one_byte_strings);
912 // Checks if the given register or operand is a unique name
913 void JumpIfNotUniqueNameInstanceType(Register reg, Label* not_unique_name,
914 Label::Distance distance = Label::kFar) {
915 JumpIfNotUniqueNameInstanceType(Operand(reg), not_unique_name, distance);
918 void JumpIfNotUniqueNameInstanceType(Operand operand, Label* not_unique_name,
919 Label::Distance distance = Label::kFar);
921 void EmitSeqStringSetCharCheck(Register string,
924 uint32_t encoding_mask);
926 static int SafepointRegisterStackIndex(Register reg) {
927 return SafepointRegisterStackIndex(reg.code());
930 // Activation support.
931 void EnterFrame(StackFrame::Type type);
932 void EnterFrame(StackFrame::Type type, bool load_constant_pool_pointer_reg);
933 void LeaveFrame(StackFrame::Type type);
935 // Expects object in eax and returns map with validated enum cache
936 // in eax. Assumes that any other register can be used as a scratch.
937 void CheckEnumCache(Label* call_runtime);
939 // AllocationMemento support. Arrays may have an associated
940 // AllocationMemento object that can be checked for in order to pretransition
942 // On entry, receiver_reg should point to the array object.
943 // scratch_reg gets clobbered.
944 // If allocation info is present, conditional code is set to equal.
945 void TestJSArrayForAllocationMemento(Register receiver_reg,
946 Register scratch_reg,
947 Label* no_memento_found);
949 void JumpIfJSArrayHasAllocationMemento(Register receiver_reg,
950 Register scratch_reg,
951 Label* memento_found) {
952 Label no_memento_found;
953 TestJSArrayForAllocationMemento(receiver_reg, scratch_reg,
955 j(equal, memento_found);
956 bind(&no_memento_found);
959 // Jumps to found label if a prototype map has dictionary elements.
960 void JumpIfDictionaryInPrototypeChain(Register object, Register scratch0,
961 Register scratch1, Label* found);
964 bool generating_stub_;
966 // This handle will be patched with the code object on installation.
967 Handle<Object> code_object_;
969 // Helper functions for generating invokes.
970 void InvokePrologue(const ParameterCount& expected,
971 const ParameterCount& actual,
972 Handle<Code> code_constant,
973 const Operand& code_operand,
975 bool* definitely_mismatches,
977 Label::Distance done_distance,
978 const CallWrapper& call_wrapper = NullCallWrapper());
980 void EnterExitFramePrologue();
981 void EnterExitFrameEpilogue(int argc, bool save_doubles);
983 void LeaveExitFrameEpilogue(bool restore_context);
985 // Allocation support helpers.
986 void LoadAllocationTopHelper(Register result,
988 AllocationFlags flags);
990 void UpdateAllocationTopHelper(Register result_end,
992 AllocationFlags flags);
994 // Helper for implementing JumpIfNotInNewSpace and JumpIfInNewSpace.
995 void InNewSpace(Register object,
998 Label* condition_met,
999 Label::Distance condition_met_distance = Label::kFar);
1001 // Helper for finding the mark bits for an address. Afterwards, the
1002 // bitmap register points at the word with the mark bits and the mask
1003 // the position of the first bit. Uses ecx as scratch and leaves addr_reg
1005 inline void GetMarkBits(Register addr_reg,
1006 Register bitmap_reg,
1009 // Compute memory operands for safepoint stack slots.
1010 Operand SafepointRegisterSlot(Register reg);
1011 static int SafepointRegisterStackIndex(int reg_code);
1013 // Needs access to SafepointRegisterStackIndex for compiled frame
1015 friend class StandardFrame;
1019 // The code patcher is used to patch (typically) small parts of code e.g. for
1020 // debugging and other types of instrumentation. When using the code patcher
1021 // the exact number of bytes specified must be emitted. Is not legal to emit
1022 // relocation information. If any of these constraints are violated it causes
1026 CodePatcher(byte* address, int size);
1029 // Macro assembler to emit code.
1030 MacroAssembler* masm() { return &masm_; }
1033 byte* address_; // The address of the code being patched.
1034 int size_; // Number of bytes of the expected patch size.
1035 MacroAssembler masm_; // Macro assembler used to generate the code.
1039 // -----------------------------------------------------------------------------
1040 // Static helper functions.
1042 // Generate an Operand for loading a field from an object.
1043 inline Operand FieldOperand(Register object, int offset) {
1044 return Operand(object, offset - kHeapObjectTag);
1048 // Generate an Operand for loading an indexed field from an object.
1049 inline Operand FieldOperand(Register object,
1053 return Operand(object, index, scale, offset - kHeapObjectTag);
1057 inline Operand FixedArrayElementOperand(Register array,
1058 Register index_as_smi,
1059 int additional_offset = 0) {
1060 int offset = FixedArray::kHeaderSize + additional_offset * kPointerSize;
1061 return FieldOperand(array, index_as_smi, times_half_pointer_size, offset);
1065 inline Operand ContextOperand(Register context, int index) {
1066 return Operand(context, Context::SlotOffset(index));
1070 inline Operand ContextOperand(Register context, Register index) {
1071 return Operand(context, index, times_pointer_size, Context::SlotOffset(0));
1075 inline Operand GlobalObjectOperand() {
1076 return ContextOperand(esi, Context::GLOBAL_OBJECT_INDEX);
1080 #ifdef GENERATED_CODE_COVERAGE
1081 extern void LogGeneratedCodeCoverage(const char* file_line);
1082 #define CODE_COVERAGE_STRINGIFY(x) #x
1083 #define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
1084 #define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
1085 #define ACCESS_MASM(masm) { \
1086 byte* ia32_coverage_function = \
1087 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
1090 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
1091 masm->call(ia32_coverage_function, RelocInfo::RUNTIME_ENTRY); \
1098 #define ACCESS_MASM(masm) masm->
1102 } } // namespace v8::internal
1104 #endif // V8_IA32_MACRO_ASSEMBLER_IA32_H_