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/frames.h"
10 #include "src/globals.h"
15 // Convenience for platform-independent signatures. We do not normally
16 // distinguish memory operands from other operands on ia32.
17 typedef Operand MemOperand;
19 enum RememberedSetAction { EMIT_REMEMBERED_SET, OMIT_REMEMBERED_SET };
20 enum SmiCheck { INLINE_SMI_CHECK, OMIT_SMI_CHECK };
21 enum PointersToHereCheck {
22 kPointersToHereMaybeInteresting,
23 kPointersToHereAreAlwaysInteresting
27 enum RegisterValueType {
28 REGISTER_VALUE_IS_SMI,
29 REGISTER_VALUE_IS_INT32
34 bool AreAliased(Register reg1,
36 Register reg3 = no_reg,
37 Register reg4 = no_reg,
38 Register reg5 = no_reg,
39 Register reg6 = no_reg,
40 Register reg7 = no_reg,
41 Register reg8 = no_reg);
45 // MacroAssembler implements a collection of frequently used macros.
46 class MacroAssembler: public Assembler {
48 // The isolate parameter can be NULL if the macro assembler should
49 // not use isolate-dependent functionality. In this case, it's the
50 // responsibility of the caller to never invoke such function on the
52 MacroAssembler(Isolate* isolate, void* buffer, int size);
54 void Load(Register dst, const Operand& src, Representation r);
55 void Store(Register src, const Operand& dst, Representation r);
57 // Operations on roots in the root-array.
58 void LoadRoot(Register destination, Heap::RootListIndex index);
59 void StoreRoot(Register source, Register scratch, Heap::RootListIndex index);
60 void CompareRoot(Register with, Register scratch, Heap::RootListIndex index);
61 // These methods can only be used with constant roots (i.e. non-writable
62 // and not in new space).
63 void CompareRoot(Register with, Heap::RootListIndex index);
64 void CompareRoot(const Operand& with, Heap::RootListIndex index);
66 // ---------------------------------------------------------------------------
68 enum RememberedSetFinalAction {
73 // Record in the remembered set the fact that we have a pointer to new space
74 // at the address pointed to by the addr register. Only works if addr is not
76 void RememberedSetHelper(Register object, // Used for debug code.
79 SaveFPRegsMode save_fp,
80 RememberedSetFinalAction and_then);
82 void CheckPageFlag(Register object,
87 Label::Distance condition_met_distance = Label::kFar);
89 void CheckPageFlagForMap(
94 Label::Distance condition_met_distance = Label::kFar);
96 void CheckMapDeprecated(Handle<Map> map,
98 Label* if_deprecated);
100 // Check if object is in new space. Jumps if the object is not in new space.
101 // The register scratch can be object itself, but scratch will be clobbered.
102 void JumpIfNotInNewSpace(Register object,
105 Label::Distance distance = Label::kFar) {
106 InNewSpace(object, scratch, zero, branch, distance);
109 // Check if object is in new space. Jumps if the object is in new space.
110 // The register scratch can be object itself, but it will be clobbered.
111 void JumpIfInNewSpace(Register object,
114 Label::Distance distance = Label::kFar) {
115 InNewSpace(object, scratch, not_zero, branch, distance);
118 // Check if an object has a given incremental marking color. Also uses ecx!
119 void HasColor(Register object,
123 Label::Distance has_color_distance,
127 void JumpIfBlack(Register object,
131 Label::Distance on_black_distance = Label::kFar);
133 // Checks the color of an object. If the object is already grey or black
134 // then we just fall through, since it is already live. If it is white and
135 // we can determine that it doesn't need to be scanned, then we just mark it
136 // black and fall through. For the rest we jump to the label so the
137 // incremental marker can fix its assumptions.
138 void EnsureNotWhite(Register object,
141 Label* object_is_white_and_not_data,
142 Label::Distance distance);
144 // Notify the garbage collector that we wrote a pointer into an object.
145 // |object| is the object being stored into, |value| is the object being
146 // stored. value and scratch registers are clobbered by the operation.
147 // The offset is the offset from the start of the object, not the offset from
148 // the tagged HeapObject pointer. For use with FieldOperand(reg, off).
149 void RecordWriteField(
154 SaveFPRegsMode save_fp,
155 RememberedSetAction remembered_set_action = EMIT_REMEMBERED_SET,
156 SmiCheck smi_check = INLINE_SMI_CHECK,
157 PointersToHereCheck pointers_to_here_check_for_value =
158 kPointersToHereMaybeInteresting);
160 // As above, but the offset has the tag presubtracted. For use with
161 // Operand(reg, off).
162 void RecordWriteContextSlot(
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) {
172 RecordWriteField(context,
173 offset + kHeapObjectTag,
177 remembered_set_action,
179 pointers_to_here_check_for_value);
182 // Notify the garbage collector that we wrote a pointer into a fixed array.
183 // |array| is the array being stored into, |value| is the
184 // object being stored. |index| is the array index represented as a
185 // Smi. All registers are clobbered by the operation RecordWriteArray
186 // filters out smis so it does not update the write barrier if the
188 void RecordWriteArray(
192 SaveFPRegsMode save_fp,
193 RememberedSetAction remembered_set_action = EMIT_REMEMBERED_SET,
194 SmiCheck smi_check = INLINE_SMI_CHECK,
195 PointersToHereCheck pointers_to_here_check_for_value =
196 kPointersToHereMaybeInteresting);
198 // For page containing |object| mark region covering |address|
199 // dirty. |object| is the object being stored into, |value| is the
200 // object being stored. The address and value registers are clobbered by the
201 // operation. RecordWrite filters out smis so it does not update the
202 // write barrier if the value is a smi.
207 SaveFPRegsMode save_fp,
208 RememberedSetAction remembered_set_action = EMIT_REMEMBERED_SET,
209 SmiCheck smi_check = INLINE_SMI_CHECK,
210 PointersToHereCheck pointers_to_here_check_for_value =
211 kPointersToHereMaybeInteresting);
213 // For page containing |object| mark the region covering the object's map
214 // dirty. |object| is the object being stored into, |map| is the Map object
216 void RecordWriteForMap(
221 SaveFPRegsMode save_fp);
223 // ---------------------------------------------------------------------------
228 // Generates function and stub prologue code.
230 void Prologue(bool code_pre_aging);
232 // Enter specific kind of exit frame. Expects the number of
233 // arguments in register eax and sets up the number of arguments in
234 // register edi and the pointer to the first argument in register
236 void EnterExitFrame(bool save_doubles);
238 void EnterApiExitFrame(int argc);
240 // Leave the current exit frame. Expects the return value in
241 // register eax:edx (untouched) and the pointer to the first
242 // argument in register esi.
243 void LeaveExitFrame(bool save_doubles);
245 // Leave the current exit frame. Expects the return value in
246 // register eax (untouched).
247 void LeaveApiExitFrame(bool restore_context);
249 // Find the function context up the context chain.
250 void LoadContext(Register dst, int context_chain_length);
252 // Conditionally load the cached Array transitioned map of type
253 // transitioned_kind from the native context if the map in register
254 // map_in_out is the cached Array map in the native context of
256 void LoadTransitionedArrayMapConditional(
257 ElementsKind expected_kind,
258 ElementsKind transitioned_kind,
261 Label* no_map_match);
263 // Load the global function with the given index.
264 void LoadGlobalFunction(int index, Register function);
266 // Load the initial map from the global function. The registers
267 // function and map can be the same.
268 void LoadGlobalFunctionInitialMap(Register function, Register map);
270 // Push and pop the registers that can hold pointers.
271 void PushSafepointRegisters() { pushad(); }
272 void PopSafepointRegisters() { popad(); }
273 // Store the value in register/immediate src in the safepoint
274 // register stack slot for register dst.
275 void StoreToSafepointRegisterSlot(Register dst, Register src);
276 void StoreToSafepointRegisterSlot(Register dst, Immediate src);
277 void LoadFromSafepointRegisterSlot(Register dst, Register src);
279 void LoadHeapObject(Register result, Handle<HeapObject> object);
280 void CmpHeapObject(Register reg, Handle<HeapObject> object);
281 void PushHeapObject(Handle<HeapObject> object);
283 void LoadObject(Register result, Handle<Object> object) {
284 AllowDeferredHandleDereference heap_object_check;
285 if (object->IsHeapObject()) {
286 LoadHeapObject(result, Handle<HeapObject>::cast(object));
288 Move(result, Immediate(object));
292 void CmpObject(Register reg, Handle<Object> object) {
293 AllowDeferredHandleDereference heap_object_check;
294 if (object->IsHeapObject()) {
295 CmpHeapObject(reg, Handle<HeapObject>::cast(object));
297 cmp(reg, Immediate(object));
301 // ---------------------------------------------------------------------------
302 // JavaScript invokes
304 // Invoke the JavaScript function code by either calling or jumping.
305 void InvokeCode(Register code,
306 const ParameterCount& expected,
307 const ParameterCount& actual,
309 const CallWrapper& call_wrapper) {
310 InvokeCode(Operand(code), expected, actual, flag, call_wrapper);
313 void InvokeCode(const Operand& code,
314 const ParameterCount& expected,
315 const ParameterCount& actual,
317 const CallWrapper& call_wrapper);
319 // Invoke the JavaScript function in the given register. Changes the
320 // current context to the context in the function before invoking.
321 void InvokeFunction(Register function,
322 const ParameterCount& actual,
324 const CallWrapper& call_wrapper);
326 void InvokeFunction(Register function,
327 const ParameterCount& expected,
328 const ParameterCount& actual,
330 const CallWrapper& call_wrapper);
332 void InvokeFunction(Handle<JSFunction> function,
333 const ParameterCount& expected,
334 const ParameterCount& actual,
336 const CallWrapper& call_wrapper);
338 // Invoke specified builtin JavaScript function. Adds an entry to
339 // the unresolved list if the name does not resolve.
340 void InvokeBuiltin(Builtins::JavaScript id,
342 const CallWrapper& call_wrapper = NullCallWrapper());
344 // Store the function for the given builtin in the target register.
345 void GetBuiltinFunction(Register target, Builtins::JavaScript id);
347 // Store the code object for the given builtin in the target register.
348 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
350 // Expression support
351 // cvtsi2sd instruction only writes to the low 64-bit of dst register, which
352 // hinders register renaming and makes dependence chains longer. So we use
353 // xorps to clear the dst register before cvtsi2sd to solve this issue.
354 void Cvtsi2sd(XMMRegister dst, Register src) { Cvtsi2sd(dst, Operand(src)); }
355 void Cvtsi2sd(XMMRegister dst, const Operand& src);
357 // Support for constant splitting.
358 bool IsUnsafeImmediate(const Immediate& x);
359 void SafeMove(Register dst, const Immediate& x);
360 void SafePush(const Immediate& x);
362 // Compare object type for heap object.
363 // Incoming register is heap_object and outgoing register is map.
364 void CmpObjectType(Register heap_object, InstanceType type, Register map);
366 // Compare instance type for map.
367 void CmpInstanceType(Register map, InstanceType type);
369 // Check if a map for a JSObject indicates that the object has fast elements.
370 // Jump to the specified label if it does not.
371 void CheckFastElements(Register map,
373 Label::Distance distance = Label::kFar);
375 // Check if a map for a JSObject indicates that the object can have both smi
376 // and HeapObject elements. Jump to the specified label if it does not.
377 void CheckFastObjectElements(Register map,
379 Label::Distance distance = Label::kFar);
381 // Check if a map for a JSObject indicates that the object has fast smi only
382 // elements. Jump to the specified label if it does not.
383 void CheckFastSmiElements(Register map,
385 Label::Distance distance = Label::kFar);
387 // Check to see if maybe_number can be stored as a double in
388 // FastDoubleElements. If it can, store it at the index specified by key in
389 // the FastDoubleElements array elements, otherwise jump to fail.
390 void StoreNumberToDoubleElements(Register maybe_number,
394 XMMRegister scratch2,
398 // Compare an object's map with the specified map.
399 void CompareMap(Register obj, Handle<Map> map);
401 // Check if the map of an object is equal to a specified map and branch to
402 // label if not. Skip the smi check if not required (object is known to be a
403 // heap object). If mode is ALLOW_ELEMENT_TRANSITION_MAPS, then also match
404 // against maps that are ElementsKind transition maps of the specified map.
405 void CheckMap(Register obj,
408 SmiCheckType smi_check_type);
410 // Check if the map of an object is equal to a specified map and branch to a
411 // specified target if equal. Skip the smi check if not required (object is
412 // known to be a heap object)
413 void DispatchMap(Register obj,
416 Handle<Code> success,
417 SmiCheckType smi_check_type);
419 // Check if the object in register heap_object is a string. Afterwards the
420 // register map contains the object map and the register instance_type
421 // contains the instance_type. The registers map and instance_type can be the
422 // same in which case it contains the instance type afterwards. Either of the
423 // registers map and instance_type can be the same as heap_object.
424 Condition IsObjectStringType(Register heap_object,
426 Register instance_type);
428 // Check if the object in register heap_object is a name. Afterwards the
429 // register map contains the object map and the register instance_type
430 // contains the instance_type. The registers map and instance_type can be the
431 // same in which case it contains the instance type afterwards. Either of the
432 // registers map and instance_type can be the same as heap_object.
433 Condition IsObjectNameType(Register heap_object,
435 Register instance_type);
437 // Check if a heap object's type is in the JSObject range, not including
438 // JSFunction. The object's map will be loaded in the map register.
439 // Any or all of the three registers may be the same.
440 // The contents of the scratch register will always be overwritten.
441 void IsObjectJSObjectType(Register heap_object,
446 // The contents of the scratch register will be overwritten.
447 void IsInstanceJSObjectType(Register map, Register scratch, Label* fail);
449 // FCmp is similar to integer cmp, but requires unsigned
450 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
453 void ClampUint8(Register reg);
455 void ClampDoubleToUint8(XMMRegister input_reg,
456 XMMRegister scratch_reg,
457 Register result_reg);
459 void SlowTruncateToI(Register result_reg, Register input_reg,
460 int offset = HeapNumber::kValueOffset - kHeapObjectTag);
462 void TruncateHeapNumberToI(Register result_reg, Register input_reg);
463 void TruncateDoubleToI(Register result_reg, XMMRegister input_reg);
465 void DoubleToI(Register result_reg, XMMRegister input_reg,
466 XMMRegister scratch, MinusZeroMode minus_zero_mode,
467 Label* conversion_failed, Label::Distance dst = Label::kFar);
469 void TaggedToI(Register result_reg, Register input_reg, XMMRegister temp,
470 MinusZeroMode minus_zero_mode, Label* lost_precision);
472 // Smi tagging support.
473 void SmiTag(Register reg) {
474 STATIC_ASSERT(kSmiTag == 0);
475 STATIC_ASSERT(kSmiTagSize == 1);
478 void SmiUntag(Register reg) {
479 sar(reg, kSmiTagSize);
482 // Modifies the register even if it does not contain a Smi!
483 void SmiUntag(Register reg, Label* is_smi) {
484 STATIC_ASSERT(kSmiTagSize == 1);
485 sar(reg, kSmiTagSize);
486 STATIC_ASSERT(kSmiTag == 0);
487 j(not_carry, is_smi);
490 void LoadUint32(XMMRegister dst, Register src);
492 // Jump the register contains a smi.
493 inline void JumpIfSmi(Register value,
495 Label::Distance distance = Label::kFar) {
496 test(value, Immediate(kSmiTagMask));
497 j(zero, smi_label, distance);
499 // Jump if the operand is a smi.
500 inline void JumpIfSmi(Operand value,
502 Label::Distance distance = Label::kFar) {
503 test(value, Immediate(kSmiTagMask));
504 j(zero, smi_label, distance);
506 // Jump if register contain a non-smi.
507 inline void JumpIfNotSmi(Register value,
508 Label* not_smi_label,
509 Label::Distance distance = Label::kFar) {
510 test(value, Immediate(kSmiTagMask));
511 j(not_zero, not_smi_label, distance);
514 void LoadInstanceDescriptors(Register map, Register descriptors);
515 void EnumLength(Register dst, Register map);
516 void NumberOfOwnDescriptors(Register dst, Register map);
518 template<typename Field>
519 void DecodeField(Register reg) {
520 static const int shift = Field::kShift;
521 static const int mask = Field::kMask >> Field::kShift;
525 and_(reg, Immediate(mask));
528 template<typename Field>
529 void DecodeFieldToSmi(Register reg) {
530 static const int shift = Field::kShift;
531 static const int mask = (Field::kMask >> Field::kShift) << kSmiTagSize;
532 STATIC_ASSERT((mask & (0x80000000u >> (kSmiTagSize - 1))) == 0);
533 STATIC_ASSERT(kSmiTag == 0);
534 if (shift < kSmiTagSize) {
535 shl(reg, kSmiTagSize - shift);
536 } else if (shift > kSmiTagSize) {
537 sar(reg, shift - kSmiTagSize);
539 and_(reg, Immediate(mask));
542 void LoadPowerOf2(XMMRegister dst, Register scratch, int power);
544 // Abort execution if argument is not a number, enabled via --debug-code.
545 void AssertNumber(Register object);
547 // Abort execution if argument is not a smi, enabled via --debug-code.
548 void AssertSmi(Register object);
550 // Abort execution if argument is a smi, enabled via --debug-code.
551 void AssertNotSmi(Register object);
553 // Abort execution if argument is not a string, enabled via --debug-code.
554 void AssertString(Register object);
556 // Abort execution if argument is not a name, enabled via --debug-code.
557 void AssertName(Register object);
559 // Abort execution if argument is not undefined or an AllocationSite, enabled
561 void AssertUndefinedOrAllocationSite(Register object);
563 // ---------------------------------------------------------------------------
564 // Exception handling
566 // Push a new try handler and link it into try handler chain.
567 void PushTryHandler(StackHandler::Kind kind, int handler_index);
569 // Unlink the stack handler on top of the stack from the try handler chain.
570 void PopTryHandler();
572 // Throw to the top handler in the try hander chain.
573 void Throw(Register value);
575 // Throw past all JS frames to the top JS entry frame.
576 void ThrowUncatchable(Register value);
578 // ---------------------------------------------------------------------------
579 // Inline caching support
581 // Generate code for checking access rights - used for security checks
582 // on access to global objects across environments. The holder register
583 // is left untouched, but the scratch register is clobbered.
584 void CheckAccessGlobalProxy(Register holder_reg,
589 void GetNumberHash(Register r0, Register scratch);
591 void LoadFromNumberDictionary(Label* miss,
600 // ---------------------------------------------------------------------------
601 // Allocation support
603 // Allocate an object in new space or old pointer space. If the given space
604 // is exhausted control continues at the gc_required label. The allocated
605 // object is returned in result and end of the new object is returned in
606 // result_end. The register scratch can be passed as no_reg in which case
607 // an additional object reference will be added to the reloc info. The
608 // returned pointers in result and result_end have not yet been tagged as
609 // heap objects. If result_contains_top_on_entry is true the content of
610 // result is known to be the allocation top on entry (could be result_end
611 // from a previous call). If result_contains_top_on_entry is true scratch
612 // should be no_reg as it is never used.
613 void Allocate(int object_size,
618 AllocationFlags flags);
620 void Allocate(int header_size,
621 ScaleFactor element_size,
622 Register element_count,
623 RegisterValueType element_count_type,
628 AllocationFlags flags);
630 void Allocate(Register object_size,
635 AllocationFlags flags);
637 // Undo allocation in new space. The object passed and objects allocated after
638 // it will no longer be allocated. Make sure that no pointers are left to the
639 // object(s) no longer allocated as they would be invalid when allocation is
641 void UndoAllocationInNewSpace(Register object);
643 // Allocate a heap number in new space with undefined value. The
644 // register scratch2 can be passed as no_reg; the others must be
645 // valid registers. Returns tagged pointer in result register, or
646 // jumps to gc_required if new space is full.
647 void AllocateHeapNumber(Register result,
651 MutableMode mode = IMMUTABLE);
653 // Allocate a float32x4, float64x2 and int32x4 object in new space with
655 // Returns tagged pointer in result register, or jumps to gc_required if new
657 void AllocateFloat32x4(Register result,
662 void AllocateFloat64x2(Register result,
667 void AllocateInt32x4(Register result,
672 // Allocate a sequential string. All the header fields of the string object
674 void AllocateTwoByteString(Register result,
680 void AllocateAsciiString(Register result,
686 void AllocateAsciiString(Register result,
692 // Allocate a raw cons string object. Only the map field of the result is
694 void AllocateTwoByteConsString(Register result,
698 void AllocateAsciiConsString(Register result,
703 // Allocate a raw sliced string object. Only the map field of the result is
705 void AllocateTwoByteSlicedString(Register result,
709 void AllocateAsciiSlicedString(Register result,
714 // Copy memory, byte-by-byte, from source to destination. Not optimized for
715 // long or aligned copies.
716 // The contents of index and scratch are destroyed.
717 void CopyBytes(Register source,
718 Register destination,
722 // Initialize fields with filler values. Fields starting at |start_offset|
723 // not including end_offset are overwritten with the value in |filler|. At
724 // the end the loop, |start_offset| takes the value of |end_offset|.
725 void InitializeFieldsWithFiller(Register start_offset,
729 // ---------------------------------------------------------------------------
730 // Support functions.
732 // Check a boolean-bit of a Smi field.
733 void BooleanBitTest(Register object, int field_offset, int bit_index);
735 // Check if result is zero and op is negative.
736 void NegativeZeroTest(Register result, Register op, Label* then_label);
738 // Check if result is zero and any of op1 and op2 are negative.
739 // Register scratch is destroyed, and it must be different from op2.
740 void NegativeZeroTest(Register result, Register op1, Register op2,
741 Register scratch, Label* then_label);
743 // Try to get function prototype of a function and puts the value in
744 // the result register. Checks that the function really is a
745 // function and jumps to the miss label if the fast checks fail. The
746 // function register will be untouched; the other registers may be
748 void TryGetFunctionPrototype(Register function,
752 bool miss_on_bound_function = false);
754 // Picks out an array index from the hash field.
756 // hash - holds the index's hash. Clobbered.
757 // index - holds the overwritten index on exit.
758 void IndexFromHash(Register hash, Register index);
760 // ---------------------------------------------------------------------------
763 // Call a code stub. Generate the code if necessary.
764 void CallStub(CodeStub* stub, TypeFeedbackId ast_id = TypeFeedbackId::None());
766 // Tail call a code stub (jump). Generate the code if necessary.
767 void TailCallStub(CodeStub* stub);
769 // Return from a code stub after popping its arguments.
770 void StubReturn(int argc);
772 // Call a runtime routine.
773 void CallRuntime(const Runtime::Function* f,
775 SaveFPRegsMode save_doubles = kDontSaveFPRegs);
776 void CallRuntimeSaveDoubles(Runtime::FunctionId id) {
777 const Runtime::Function* function = Runtime::FunctionForId(id);
778 CallRuntime(function, function->nargs, kSaveFPRegs);
781 // Convenience function: Same as above, but takes the fid instead.
782 void CallRuntime(Runtime::FunctionId id,
784 SaveFPRegsMode save_doubles = kDontSaveFPRegs) {
785 CallRuntime(Runtime::FunctionForId(id), num_arguments, save_doubles);
788 // Convenience function: call an external reference.
789 void CallExternalReference(ExternalReference ref, int num_arguments);
791 // Tail call of a runtime routine (jump).
792 // Like JumpToExternalReference, but also takes care of passing the number
794 void TailCallExternalReference(const ExternalReference& ext,
798 // Convenience function: tail call a runtime routine (jump).
799 void TailCallRuntime(Runtime::FunctionId fid,
803 // Before calling a C-function from generated code, align arguments on stack.
804 // After aligning the frame, arguments must be stored in esp[0], esp[4],
805 // etc., not pushed. The argument count assumes all arguments are word sized.
806 // Some compilers/platforms require the stack to be aligned when calling
808 // Needs a scratch register to do some arithmetic. This register will be
810 void PrepareCallCFunction(int num_arguments, Register scratch);
812 // Calls a C function and cleans up the space for arguments allocated
813 // by PrepareCallCFunction. The called function is not allowed to trigger a
814 // garbage collection, since that might move the code and invalidate the
815 // return address (unless this is somehow accounted for by the called
817 void CallCFunction(ExternalReference function, int num_arguments);
818 void CallCFunction(Register function, int num_arguments);
820 // Prepares stack to put arguments (aligns and so on). Reserves
821 // space for return value if needed (assumes the return value is a handle).
822 // Arguments must be stored in ApiParameterOperand(0), ApiParameterOperand(1)
823 // etc. Saves context (esi). If space was reserved for return value then
824 // stores the pointer to the reserved slot into esi.
825 void PrepareCallApiFunction(int argc);
827 // Calls an API function. Allocates HandleScope, extracts returned value
828 // from handle and propagates exceptions. Clobbers ebx, edi and
829 // caller-save registers. Restores context. On return removes
830 // stack_space * kPointerSize (GCed).
831 void CallApiFunctionAndReturn(Register function_address,
832 ExternalReference thunk_ref,
833 Operand thunk_last_arg,
835 Operand return_value_operand,
836 Operand* context_restore_operand);
838 // Jump to a runtime routine.
839 void JumpToExternalReference(const ExternalReference& ext);
841 // ---------------------------------------------------------------------------
846 // Return and drop arguments from stack, where the number of arguments
847 // may be bigger than 2^16 - 1. Requires a scratch register.
848 void Ret(int bytes_dropped, Register scratch);
850 // Emit code to discard a non-negative number of pointer-sized elements
851 // from the stack, clobbering only the esp register.
852 void Drop(int element_count);
854 void Call(Label* target) { call(target); }
855 void Push(Register src) { push(src); }
856 void Pop(Register dst) { pop(dst); }
858 // Emit call to the code we are currently generating.
860 Handle<Code> self(reinterpret_cast<Code**>(CodeObject().location()));
861 call(self, RelocInfo::CODE_TARGET);
864 // Move if the registers are not identical.
865 void Move(Register target, Register source);
867 // Move a constant into a destination using the most efficient encoding.
868 void Move(Register dst, const Immediate& x);
869 void Move(const Operand& dst, const Immediate& x);
871 // Move an immediate into an XMM register.
872 void Move(XMMRegister dst, double val);
874 // Push a handle value.
875 void Push(Handle<Object> handle) { push(Immediate(handle)); }
876 void Push(Smi* smi) { Push(Handle<Smi>(smi, isolate())); }
878 Handle<Object> CodeObject() {
879 DCHECK(!code_object_.is_null());
883 // Emit code for a truncating division by a constant. The dividend register is
884 // unchanged, the result is in edx, and eax gets clobbered.
885 void TruncatingDiv(Register dividend, int32_t divisor);
887 // ---------------------------------------------------------------------------
888 // StatsCounter support
890 void SetCounter(StatsCounter* counter, int value);
891 void IncrementCounter(StatsCounter* counter, int value);
892 void DecrementCounter(StatsCounter* counter, int value);
893 void IncrementCounter(Condition cc, StatsCounter* counter, int value);
894 void DecrementCounter(Condition cc, StatsCounter* counter, int value);
897 // ---------------------------------------------------------------------------
900 // Calls Abort(msg) if the condition cc is not satisfied.
901 // Use --debug_code to enable.
902 void Assert(Condition cc, BailoutReason reason);
904 void AssertFastElements(Register elements);
906 // Like Assert(), but always enabled.
907 void Check(Condition cc, BailoutReason reason);
909 // Print a message to stdout and abort execution.
910 void Abort(BailoutReason reason);
912 // Check that the stack is aligned.
913 void CheckStackAlignment();
915 // Verify restrictions about code generated in stubs.
916 void set_generating_stub(bool value) { generating_stub_ = value; }
917 bool generating_stub() { return generating_stub_; }
918 void set_has_frame(bool value) { has_frame_ = value; }
919 bool has_frame() { return has_frame_; }
920 inline bool AllowThisStubCall(CodeStub* stub);
922 // ---------------------------------------------------------------------------
924 void absps(XMMRegister dst);
925 void abspd(XMMRegister dst);
926 void negateps(XMMRegister dst);
927 void negatepd(XMMRegister dst);
928 void notps(XMMRegister dst);
929 void pnegd(XMMRegister dst);
931 // ---------------------------------------------------------------------------
934 // Generate code to do a lookup in the number string cache. If the number in
935 // the register object is found in the cache the generated code falls through
936 // with the result in the result register. The object and the result register
937 // can be the same. If the number is not found in the cache the code jumps to
938 // the label not_found with only the content of register object unchanged.
939 void LookupNumberStringCache(Register object,
945 // Check whether the instance type represents a flat ASCII string. Jump to the
946 // label if not. If the instance type can be scratched specify same register
947 // for both instance type and scratch.
948 void JumpIfInstanceTypeIsNotSequentialAscii(Register instance_type,
950 Label* on_not_flat_ascii_string);
952 // Checks if both objects are sequential ASCII strings, and jumps to label
954 void JumpIfNotBothSequentialAsciiStrings(Register object1,
958 Label* on_not_flat_ascii_strings);
960 // Checks if the given register or operand is a unique name
961 void JumpIfNotUniqueName(Register reg, Label* not_unique_name,
962 Label::Distance distance = Label::kFar) {
963 JumpIfNotUniqueName(Operand(reg), not_unique_name, distance);
966 void JumpIfNotUniqueName(Operand operand, Label* not_unique_name,
967 Label::Distance distance = Label::kFar);
969 void EmitSeqStringSetCharCheck(Register string,
972 uint32_t encoding_mask);
974 static int SafepointRegisterStackIndex(Register reg) {
975 return SafepointRegisterStackIndex(reg.code());
978 // Activation support.
979 void EnterFrame(StackFrame::Type type);
980 void LeaveFrame(StackFrame::Type type);
982 // Expects object in eax and returns map with validated enum cache
983 // in eax. Assumes that any other register can be used as a scratch.
984 void CheckEnumCache(Label* call_runtime);
986 // AllocationMemento support. Arrays may have an associated
987 // AllocationMemento object that can be checked for in order to pretransition
989 // On entry, receiver_reg should point to the array object.
990 // scratch_reg gets clobbered.
991 // If allocation info is present, conditional code is set to equal.
992 void TestJSArrayForAllocationMemento(Register receiver_reg,
993 Register scratch_reg,
994 Label* no_memento_found);
996 void JumpIfJSArrayHasAllocationMemento(Register receiver_reg,
997 Register scratch_reg,
998 Label* memento_found) {
999 Label no_memento_found;
1000 TestJSArrayForAllocationMemento(receiver_reg, scratch_reg,
1002 j(equal, memento_found);
1003 bind(&no_memento_found);
1006 // Jumps to found label if a prototype map has dictionary elements.
1007 void JumpIfDictionaryInPrototypeChain(Register object, Register scratch0,
1008 Register scratch1, Label* found);
1011 bool generating_stub_;
1013 // This handle will be patched with the code object on installation.
1014 Handle<Object> code_object_;
1016 // Helper functions for generating invokes.
1017 void InvokePrologue(const ParameterCount& expected,
1018 const ParameterCount& actual,
1019 Handle<Code> code_constant,
1020 const Operand& code_operand,
1022 bool* definitely_mismatches,
1024 Label::Distance done_distance,
1025 const CallWrapper& call_wrapper = NullCallWrapper());
1027 void EnterExitFramePrologue();
1028 void EnterExitFrameEpilogue(int argc, bool save_doubles);
1030 void LeaveExitFrameEpilogue(bool restore_context);
1032 // Allocation support helpers.
1033 void LoadAllocationTopHelper(Register result,
1035 AllocationFlags flags);
1037 void UpdateAllocationTopHelper(Register result_end,
1039 AllocationFlags flags);
1041 // Helper for implementing JumpIfNotInNewSpace and JumpIfInNewSpace.
1042 void InNewSpace(Register object,
1045 Label* condition_met,
1046 Label::Distance condition_met_distance = Label::kFar);
1048 // Helper for finding the mark bits for an address. Afterwards, the
1049 // bitmap register points at the word with the mark bits and the mask
1050 // the position of the first bit. Uses ecx as scratch and leaves addr_reg
1052 inline void GetMarkBits(Register addr_reg,
1053 Register bitmap_reg,
1056 // Helper for throwing exceptions. Compute a handler address and jump to
1057 // it. See the implementation for register usage.
1058 void JumpToHandlerEntry();
1060 // Compute memory operands for safepoint stack slots.
1061 Operand SafepointRegisterSlot(Register reg);
1062 static int SafepointRegisterStackIndex(int reg_code);
1064 // Needs access to SafepointRegisterStackIndex for compiled frame
1066 friend class StandardFrame;
1070 // The code patcher is used to patch (typically) small parts of code e.g. for
1071 // debugging and other types of instrumentation. When using the code patcher
1072 // the exact number of bytes specified must be emitted. Is not legal to emit
1073 // relocation information. If any of these constraints are violated it causes
1077 CodePatcher(byte* address, int size);
1078 virtual ~CodePatcher();
1080 // Macro assembler to emit code.
1081 MacroAssembler* masm() { return &masm_; }
1084 byte* address_; // The address of the code being patched.
1085 int size_; // Number of bytes of the expected patch size.
1086 MacroAssembler masm_; // Macro assembler used to generate the code.
1090 // -----------------------------------------------------------------------------
1091 // Static helper functions.
1093 // Generate an Operand for loading a field from an object.
1094 inline Operand FieldOperand(Register object, int offset) {
1095 return Operand(object, offset - kHeapObjectTag);
1099 // Generate an Operand for loading an indexed field from an object.
1100 inline Operand FieldOperand(Register object,
1104 return Operand(object, index, scale, offset - kHeapObjectTag);
1108 inline Operand FixedArrayElementOperand(Register array,
1109 Register index_as_smi,
1110 int additional_offset = 0) {
1111 int offset = FixedArray::kHeaderSize + additional_offset * kPointerSize;
1112 return FieldOperand(array, index_as_smi, times_half_pointer_size, offset);
1116 inline Operand ContextOperand(Register context, int index) {
1117 return Operand(context, Context::SlotOffset(index));
1121 inline Operand GlobalObjectOperand() {
1122 return ContextOperand(esi, Context::GLOBAL_OBJECT_INDEX);
1126 // Generates an Operand for saving parameters after PrepareCallApiFunction.
1127 Operand ApiParameterOperand(int index);
1130 #ifdef GENERATED_CODE_COVERAGE
1131 extern void LogGeneratedCodeCoverage(const char* file_line);
1132 #define CODE_COVERAGE_STRINGIFY(x) #x
1133 #define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
1134 #define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
1135 #define ACCESS_MASM(masm) { \
1136 byte* ia32_coverage_function = \
1137 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
1140 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
1141 masm->call(ia32_coverage_function, RelocInfo::RUNTIME_ENTRY); \
1148 #define ACCESS_MASM(masm) masm->
1152 } } // namespace v8::internal
1154 #endif // V8_IA32_MACRO_ASSEMBLER_IA32_H_