1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef V8_IA32_MACRO_ASSEMBLER_IA32_H_
29 #define V8_IA32_MACRO_ASSEMBLER_IA32_H_
31 #include "assembler.h"
33 #include "v8globals.h"
38 // Convenience for platform-independent signatures. We do not normally
39 // distinguish memory operands from other operands on ia32.
40 typedef Operand MemOperand;
42 enum RememberedSetAction { EMIT_REMEMBERED_SET, OMIT_REMEMBERED_SET };
43 enum SmiCheck { INLINE_SMI_CHECK, OMIT_SMI_CHECK };
46 enum RegisterValueType {
47 REGISTER_VALUE_IS_SMI,
48 REGISTER_VALUE_IS_INT32
52 bool AreAliased(Register r1, Register r2, Register r3, Register r4);
55 // MacroAssembler implements a collection of frequently used macros.
56 class MacroAssembler: public Assembler {
58 // The isolate parameter can be NULL if the macro assembler should
59 // not use isolate-dependent functionality. In this case, it's the
60 // responsibility of the caller to never invoke such function on the
62 MacroAssembler(Isolate* isolate, void* buffer, int size);
64 void Load(Register dst, const Operand& src, Representation r);
65 void Store(Register src, const Operand& dst, Representation r);
67 // Operations on roots in the root-array.
68 void LoadRoot(Register destination, Heap::RootListIndex index);
69 void StoreRoot(Register source, Register scratch, Heap::RootListIndex index);
70 void CompareRoot(Register with, Register scratch, Heap::RootListIndex index);
71 // These methods can only be used with constant roots (i.e. non-writable
72 // and not in new space).
73 void CompareRoot(Register with, Heap::RootListIndex index);
74 void CompareRoot(const Operand& with, Heap::RootListIndex index);
76 // ---------------------------------------------------------------------------
78 enum RememberedSetFinalAction {
83 // Record in the remembered set the fact that we have a pointer to new space
84 // at the address pointed to by the addr register. Only works if addr is not
86 void RememberedSetHelper(Register object, // Used for debug code.
89 SaveFPRegsMode save_fp,
90 RememberedSetFinalAction and_then);
92 void CheckPageFlag(Register object,
97 Label::Distance condition_met_distance = Label::kFar);
99 void CheckPageFlagForMap(
103 Label* condition_met,
104 Label::Distance condition_met_distance = Label::kFar);
106 void CheckMapDeprecated(Handle<Map> map,
108 Label* if_deprecated);
110 // Check if object is in new space. Jumps if the object is not in new space.
111 // The register scratch can be object itself, but scratch will be clobbered.
112 void JumpIfNotInNewSpace(Register object,
115 Label::Distance distance = Label::kFar) {
116 InNewSpace(object, scratch, zero, branch, distance);
119 // Check if object is in new space. Jumps if the object is in new space.
120 // The register scratch can be object itself, but it will be clobbered.
121 void JumpIfInNewSpace(Register object,
124 Label::Distance distance = Label::kFar) {
125 InNewSpace(object, scratch, not_zero, branch, distance);
128 // Check if an object has a given incremental marking color. Also uses ecx!
129 void HasColor(Register object,
133 Label::Distance has_color_distance,
137 void JumpIfBlack(Register object,
141 Label::Distance on_black_distance = Label::kFar);
143 // Checks the color of an object. If the object is already grey or black
144 // then we just fall through, since it is already live. If it is white and
145 // we can determine that it doesn't need to be scanned, then we just mark it
146 // black and fall through. For the rest we jump to the label so the
147 // incremental marker can fix its assumptions.
148 void EnsureNotWhite(Register object,
151 Label* object_is_white_and_not_data,
152 Label::Distance distance);
154 // Notify the garbage collector that we wrote a pointer into an object.
155 // |object| is the object being stored into, |value| is the object being
156 // stored. value and scratch registers are clobbered by the operation.
157 // The offset is the offset from the start of the object, not the offset from
158 // the tagged HeapObject pointer. For use with FieldOperand(reg, off).
159 void RecordWriteField(
164 SaveFPRegsMode save_fp,
165 RememberedSetAction remembered_set_action = EMIT_REMEMBERED_SET,
166 SmiCheck smi_check = INLINE_SMI_CHECK);
168 // As above, but the offset has the tag presubtracted. For use with
169 // Operand(reg, off).
170 void RecordWriteContextSlot(
175 SaveFPRegsMode save_fp,
176 RememberedSetAction remembered_set_action = EMIT_REMEMBERED_SET,
177 SmiCheck smi_check = INLINE_SMI_CHECK) {
178 RecordWriteField(context,
179 offset + kHeapObjectTag,
183 remembered_set_action,
187 // Notify the garbage collector that we wrote a pointer into a fixed array.
188 // |array| is the array being stored into, |value| is the
189 // object being stored. |index| is the array index represented as a
190 // Smi. All registers are clobbered by the operation RecordWriteArray
191 // filters out smis so it does not update the write barrier if the
193 void RecordWriteArray(
197 SaveFPRegsMode save_fp,
198 RememberedSetAction remembered_set_action = EMIT_REMEMBERED_SET,
199 SmiCheck smi_check = INLINE_SMI_CHECK);
201 // For page containing |object| mark region covering |address|
202 // dirty. |object| is the object being stored into, |value| is the
203 // object being stored. The address and value registers are clobbered by the
204 // operation. RecordWrite filters out smis so it does not update the
205 // write barrier if the value is a smi.
210 SaveFPRegsMode save_fp,
211 RememberedSetAction remembered_set_action = EMIT_REMEMBERED_SET,
212 SmiCheck smi_check = INLINE_SMI_CHECK);
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
217 void RecordWriteForMap(
222 SaveFPRegsMode save_fp);
224 #ifdef ENABLE_DEBUGGER_SUPPORT
225 // ---------------------------------------------------------------------------
231 // Generates function and stub prologue code.
232 void Prologue(PrologueFrameMode frame_mode);
234 // Enter specific kind of exit frame. Expects the number of
235 // arguments in register eax and sets up the number of arguments in
236 // register edi and the pointer to the first argument in register
238 void EnterExitFrame(bool save_doubles);
240 void EnterApiExitFrame(int argc);
242 // Leave the current exit frame. Expects the return value in
243 // register eax:edx (untouched) and the pointer to the first
244 // argument in register esi.
245 void LeaveExitFrame(bool save_doubles);
247 // Leave the current exit frame. Expects the return value in
248 // register eax (untouched).
249 void LeaveApiExitFrame(bool restore_context);
251 // Find the function context up the context chain.
252 void LoadContext(Register dst, int context_chain_length);
254 // Conditionally load the cached Array transitioned map of type
255 // transitioned_kind from the native context if the map in register
256 // map_in_out is the cached Array map in the native context of
258 void LoadTransitionedArrayMapConditional(
259 ElementsKind expected_kind,
260 ElementsKind transitioned_kind,
263 Label* no_map_match);
265 // Load the initial map for new Arrays from a JSFunction.
266 void LoadInitialArrayMap(Register function_in,
269 bool can_have_holes);
271 void LoadGlobalContext(Register global_context);
273 // Load the global function with the given index.
274 void LoadGlobalFunction(int index, Register function);
276 // Load the initial map from the global function. The registers
277 // function and map can be the same.
278 void LoadGlobalFunctionInitialMap(Register function, Register map);
280 // Push and pop the registers that can hold pointers.
281 void PushSafepointRegisters() { pushad(); }
282 void PopSafepointRegisters() { popad(); }
283 // Store the value in register/immediate src in the safepoint
284 // register stack slot for register dst.
285 void StoreToSafepointRegisterSlot(Register dst, Register src);
286 void StoreToSafepointRegisterSlot(Register dst, Immediate src);
287 void LoadFromSafepointRegisterSlot(Register dst, Register src);
289 void LoadHeapObject(Register result, Handle<HeapObject> object);
290 void CmpHeapObject(Register reg, Handle<HeapObject> object);
291 void PushHeapObject(Handle<HeapObject> object);
293 void LoadObject(Register result, Handle<Object> object) {
294 AllowDeferredHandleDereference heap_object_check;
295 if (object->IsHeapObject()) {
296 LoadHeapObject(result, Handle<HeapObject>::cast(object));
298 Set(result, Immediate(object));
302 void CmpObject(Register reg, Handle<Object> object) {
303 AllowDeferredHandleDereference heap_object_check;
304 if (object->IsHeapObject()) {
305 CmpHeapObject(reg, Handle<HeapObject>::cast(object));
307 cmp(reg, Immediate(object));
311 // ---------------------------------------------------------------------------
312 // JavaScript invokes
314 // Invoke the JavaScript function code by either calling or jumping.
315 void InvokeCode(Register code,
316 const ParameterCount& expected,
317 const ParameterCount& actual,
319 const CallWrapper& call_wrapper) {
320 InvokeCode(Operand(code), expected, actual, flag, call_wrapper);
323 void InvokeCode(const Operand& code,
324 const ParameterCount& expected,
325 const ParameterCount& actual,
327 const CallWrapper& call_wrapper);
329 // Invoke the JavaScript function in the given register. Changes the
330 // current context to the context in the function before invoking.
331 void InvokeFunction(Register function,
332 const ParameterCount& actual,
334 const CallWrapper& call_wrapper);
336 void InvokeFunction(Register function,
337 const ParameterCount& expected,
338 const ParameterCount& actual,
340 const CallWrapper& call_wrapper);
342 void InvokeFunction(Handle<JSFunction> function,
343 const ParameterCount& expected,
344 const ParameterCount& actual,
346 const CallWrapper& call_wrapper);
348 // Invoke specified builtin JavaScript function. Adds an entry to
349 // the unresolved list if the name does not resolve.
350 void InvokeBuiltin(Builtins::JavaScript id,
352 const CallWrapper& call_wrapper = NullCallWrapper());
354 // Store the function for the given builtin in the target register.
355 void GetBuiltinFunction(Register target, Builtins::JavaScript id);
357 // Store the code object for the given builtin in the target register.
358 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
360 // Expression support
361 void Set(Register dst, const Immediate& x);
362 void Set(const Operand& dst, const Immediate& x);
364 // cvtsi2sd instruction only writes to the low 64-bit of dst register, which
365 // hinders register renaming and makes dependence chains longer. So we use
366 // xorps to clear the dst register before cvtsi2sd to solve this issue.
367 void Cvtsi2sd(XMMRegister dst, Register src) { Cvtsi2sd(dst, Operand(src)); }
368 void Cvtsi2sd(XMMRegister dst, const Operand& src);
370 // Support for constant splitting.
371 bool IsUnsafeImmediate(const Immediate& x);
372 void SafeSet(Register dst, const Immediate& x);
373 void SafePush(const Immediate& x);
375 // Compare object type for heap object.
376 // Incoming register is heap_object and outgoing register is map.
377 void CmpObjectType(Register heap_object, InstanceType type, Register map);
379 // Compare instance type for map.
380 void CmpInstanceType(Register map, InstanceType type);
382 // Check if a map for a JSObject indicates that the object has fast elements.
383 // Jump to the specified label if it does not.
384 void CheckFastElements(Register map,
386 Label::Distance distance = Label::kFar);
388 // Check if a map for a JSObject indicates that the object can have both smi
389 // and HeapObject elements. Jump to the specified label if it does not.
390 void CheckFastObjectElements(Register map,
392 Label::Distance distance = Label::kFar);
394 // Check if a map for a JSObject indicates that the object has fast smi only
395 // elements. Jump to the specified label if it does not.
396 void CheckFastSmiElements(Register map,
398 Label::Distance distance = Label::kFar);
400 // Check to see if maybe_number can be stored as a double in
401 // FastDoubleElements. If it can, store it at the index specified by key in
402 // the FastDoubleElements array elements, otherwise jump to fail.
403 void StoreNumberToDoubleElements(Register maybe_number,
407 XMMRegister scratch2,
409 bool specialize_for_processor,
412 // Compare an object's map with the specified map.
413 void CompareMap(Register obj, Handle<Map> map);
415 // Check if the map of an object is equal to a specified map and branch to
416 // label if not. Skip the smi check if not required (object is known to be a
417 // heap object). If mode is ALLOW_ELEMENT_TRANSITION_MAPS, then also match
418 // against maps that are ElementsKind transition maps of the specified map.
419 void CheckMap(Register obj,
422 SmiCheckType smi_check_type);
424 // Check if the map of an object is equal to a specified map and branch to a
425 // specified target if equal. Skip the smi check if not required (object is
426 // known to be a heap object)
427 void DispatchMap(Register obj,
430 Handle<Code> success,
431 SmiCheckType smi_check_type);
433 // Check if the object in register heap_object is a string. Afterwards the
434 // register map contains the object map and the register instance_type
435 // contains the instance_type. The registers map and instance_type can be the
436 // same in which case it contains the instance type afterwards. Either of the
437 // registers map and instance_type can be the same as heap_object.
438 Condition IsObjectStringType(Register heap_object,
440 Register instance_type);
442 // Check if the object in register heap_object is a name. Afterwards the
443 // register map contains the object map and the register instance_type
444 // contains the instance_type. The registers map and instance_type can be the
445 // same in which case it contains the instance type afterwards. Either of the
446 // registers map and instance_type can be the same as heap_object.
447 Condition IsObjectNameType(Register heap_object,
449 Register instance_type);
451 // Check if a heap object's type is in the JSObject range, not including
452 // JSFunction. The object's map will be loaded in the map register.
453 // Any or all of the three registers may be the same.
454 // The contents of the scratch register will always be overwritten.
455 void IsObjectJSObjectType(Register heap_object,
460 // The contents of the scratch register will be overwritten.
461 void IsInstanceJSObjectType(Register map, Register scratch, Label* fail);
463 // FCmp is similar to integer cmp, but requires unsigned
464 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
467 void ClampUint8(Register reg);
469 void ClampDoubleToUint8(XMMRegister input_reg,
470 XMMRegister scratch_reg,
471 Register result_reg);
473 void SlowTruncateToI(Register result_reg, Register input_reg,
474 int offset = HeapNumber::kValueOffset - kHeapObjectTag);
476 void TruncateHeapNumberToI(Register result_reg, Register input_reg);
477 void TruncateDoubleToI(Register result_reg, XMMRegister input_reg);
478 void TruncateX87TOSToI(Register result_reg);
480 void DoubleToI(Register result_reg, XMMRegister input_reg,
481 XMMRegister scratch, MinusZeroMode minus_zero_mode,
482 Label* conversion_failed, Label::Distance dst = Label::kFar);
483 void X87TOSToI(Register result_reg, MinusZeroMode minus_zero_mode,
484 Label* conversion_failed, Label::Distance dst = Label::kFar);
486 void TaggedToI(Register result_reg, Register input_reg, XMMRegister temp,
487 MinusZeroMode minus_zero_mode, Label* lost_precision);
489 // Smi tagging support.
490 void SmiTag(Register reg) {
491 STATIC_ASSERT(kSmiTag == 0);
492 STATIC_ASSERT(kSmiTagSize == 1);
495 void SmiUntag(Register reg) {
496 sar(reg, kSmiTagSize);
499 // Modifies the register even if it does not contain a Smi!
500 void SmiUntag(Register reg, Label* is_smi) {
501 STATIC_ASSERT(kSmiTagSize == 1);
502 sar(reg, kSmiTagSize);
503 STATIC_ASSERT(kSmiTag == 0);
504 j(not_carry, is_smi);
507 void LoadUint32(XMMRegister dst, Register src, XMMRegister scratch);
508 void LoadUint32NoSSE2(Register src);
510 // Jump the register contains a smi.
511 inline void JumpIfSmi(Register value,
513 Label::Distance distance = Label::kFar) {
514 test(value, Immediate(kSmiTagMask));
515 j(zero, smi_label, distance);
517 // Jump if the operand is a smi.
518 inline void JumpIfSmi(Operand value,
520 Label::Distance distance = Label::kFar) {
521 test(value, Immediate(kSmiTagMask));
522 j(zero, smi_label, distance);
524 // Jump if register contain a non-smi.
525 inline void JumpIfNotSmi(Register value,
526 Label* not_smi_label,
527 Label::Distance distance = Label::kFar) {
528 test(value, Immediate(kSmiTagMask));
529 j(not_zero, not_smi_label, distance);
532 void LoadInstanceDescriptors(Register map, Register descriptors);
533 void EnumLength(Register dst, Register map);
534 void NumberOfOwnDescriptors(Register dst, Register map);
536 template<typename Field>
537 void DecodeField(Register reg) {
538 static const int shift = Field::kShift;
539 static const int mask = (Field::kMask >> Field::kShift) << kSmiTagSize;
541 and_(reg, Immediate(mask));
543 void LoadPowerOf2(XMMRegister dst, Register scratch, int power);
545 // Abort execution if argument is not a number, enabled via --debug-code.
546 void AssertNumber(Register object);
548 // Abort execution if argument is not a smi, enabled via --debug-code.
549 void AssertSmi(Register object);
551 // Abort execution if argument is a smi, enabled via --debug-code.
552 void AssertNotSmi(Register object);
554 // Abort execution if argument is not a string, enabled via --debug-code.
555 void AssertString(Register object);
557 // Abort execution if argument is not a name, enabled via --debug-code.
558 void AssertName(Register object);
560 // ---------------------------------------------------------------------------
561 // Exception handling
563 // Push a new try handler and link it into try handler chain.
564 void PushTryHandler(StackHandler::Kind kind, int handler_index);
566 // Unlink the stack handler on top of the stack from the try handler chain.
567 void PopTryHandler();
569 // Throw to the top handler in the try hander chain.
570 void Throw(Register value);
572 // Throw past all JS frames to the top JS entry frame.
573 void ThrowUncatchable(Register value);
575 // Throw a message string as an exception.
576 void Throw(BailoutReason reason);
578 // Throw a message string as an exception if a condition is not true.
579 void ThrowIf(Condition cc, BailoutReason reason);
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 pointer 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 // Undo allocation in new space. The object passed and objects allocated after
641 // it will no longer be allocated. Make sure that no pointers are left to the
642 // object(s) no longer allocated as they would be invalid when allocation is
644 void UndoAllocationInNewSpace(Register object);
646 // Allocate a heap number in new space with undefined value. The
647 // register scratch2 can be passed as no_reg; the others must be
648 // valid registers. Returns tagged pointer in result register, or
649 // jumps to gc_required if new space is full.
650 void AllocateHeapNumber(Register result,
655 // Allocate a sequential string. All the header fields of the string object
657 void AllocateTwoByteString(Register result,
663 void AllocateAsciiString(Register result,
669 void AllocateAsciiString(Register result,
675 // Allocate a raw cons string object. Only the map field of the result is
677 void AllocateTwoByteConsString(Register result,
681 void AllocateAsciiConsString(Register result,
686 // Allocate a raw sliced string object. Only the map field of the result is
688 void AllocateTwoByteSlicedString(Register result,
692 void AllocateAsciiSlicedString(Register result,
697 // Copy memory, byte-by-byte, from source to destination. Not optimized for
698 // long or aligned copies.
699 // The contents of index and scratch are destroyed.
700 void CopyBytes(Register source,
701 Register destination,
705 // Initialize fields with filler values. Fields starting at |start_offset|
706 // not including end_offset are overwritten with the value in |filler|. At
707 // the end the loop, |start_offset| takes the value of |end_offset|.
708 void InitializeFieldsWithFiller(Register start_offset,
712 // ---------------------------------------------------------------------------
713 // Support functions.
715 // Check a boolean-bit of a Smi field.
716 void BooleanBitTest(Register object, int field_offset, int bit_index);
718 // Check if result is zero and op is negative.
719 void NegativeZeroTest(Register result, Register op, Label* then_label);
721 // Check if result is zero and any of op1 and op2 are negative.
722 // Register scratch is destroyed, and it must be different from op2.
723 void NegativeZeroTest(Register result, Register op1, Register op2,
724 Register scratch, Label* then_label);
726 // Try to get function prototype of a function and puts the value in
727 // the result register. Checks that the function really is a
728 // function and jumps to the miss label if the fast checks fail. The
729 // function register will be untouched; the other registers may be
731 void TryGetFunctionPrototype(Register function,
735 bool miss_on_bound_function = false);
737 // Generates code for reporting that an illegal operation has
739 void IllegalOperation(int num_arguments);
741 // Picks out an array index from the hash field.
743 // hash - holds the index's hash. Clobbered.
744 // index - holds the overwritten index on exit.
745 void IndexFromHash(Register hash, Register index);
747 // ---------------------------------------------------------------------------
750 // Call a code stub. Generate the code if necessary.
751 void CallStub(CodeStub* stub, TypeFeedbackId ast_id = TypeFeedbackId::None());
753 // Tail call a code stub (jump). Generate the code if necessary.
754 void TailCallStub(CodeStub* stub);
756 // Return from a code stub after popping its arguments.
757 void StubReturn(int argc);
759 // Call a runtime routine.
760 void CallRuntime(const Runtime::Function* f,
762 SaveFPRegsMode save_doubles = kDontSaveFPRegs);
763 void CallRuntimeSaveDoubles(Runtime::FunctionId id) {
764 const Runtime::Function* function = Runtime::FunctionForId(id);
765 CallRuntime(function, function->nargs, kSaveFPRegs);
768 // Convenience function: Same as above, but takes the fid instead.
769 void CallRuntime(Runtime::FunctionId id,
771 SaveFPRegsMode save_doubles = kDontSaveFPRegs) {
772 CallRuntime(Runtime::FunctionForId(id), num_arguments, save_doubles);
775 // Convenience function: call an external reference.
776 void CallExternalReference(ExternalReference ref, int num_arguments);
778 // Tail call of a runtime routine (jump).
779 // Like JumpToExternalReference, but also takes care of passing the number
781 void TailCallExternalReference(const ExternalReference& ext,
785 // Convenience function: tail call a runtime routine (jump).
786 void TailCallRuntime(Runtime::FunctionId fid,
790 // Before calling a C-function from generated code, align arguments on stack.
791 // After aligning the frame, arguments must be stored in esp[0], esp[4],
792 // etc., not pushed. The argument count assumes all arguments are word sized.
793 // Some compilers/platforms require the stack to be aligned when calling
795 // Needs a scratch register to do some arithmetic. This register will be
797 void PrepareCallCFunction(int num_arguments, Register scratch);
799 // Calls a C function and cleans up the space for arguments allocated
800 // by PrepareCallCFunction. The called function is not allowed to trigger a
801 // garbage collection, since that might move the code and invalidate the
802 // return address (unless this is somehow accounted for by the called
804 void CallCFunction(ExternalReference function, int num_arguments);
805 void CallCFunction(Register function, int num_arguments);
807 // Prepares stack to put arguments (aligns and so on). Reserves
808 // space for return value if needed (assumes the return value is a handle).
809 // Arguments must be stored in ApiParameterOperand(0), ApiParameterOperand(1)
810 // etc. Saves context (esi). If space was reserved for return value then
811 // stores the pointer to the reserved slot into esi.
812 void PrepareCallApiFunction(int argc);
814 // Calls an API function. Allocates HandleScope, extracts returned value
815 // from handle and propagates exceptions. Clobbers ebx, edi and
816 // caller-save registers. Restores context. On return removes
817 // stack_space * kPointerSize (GCed).
818 void CallApiFunctionAndReturn(Address function_address,
819 Address thunk_address,
820 Operand thunk_last_arg,
822 Operand return_value_operand,
823 Operand* context_restore_operand);
825 // Jump to a runtime routine.
826 void JumpToExternalReference(const ExternalReference& ext);
828 // ---------------------------------------------------------------------------
833 // Return and drop arguments from stack, where the number of arguments
834 // may be bigger than 2^16 - 1. Requires a scratch register.
835 void Ret(int bytes_dropped, Register scratch);
837 // Emit code to discard a non-negative number of pointer-sized elements
838 // from the stack, clobbering only the esp register.
839 void Drop(int element_count);
841 void Call(Label* target) { call(target); }
842 void Push(Register src) { push(src); }
843 void Pop(Register dst) { pop(dst); }
845 // Emit call to the code we are currently generating.
847 Handle<Code> self(reinterpret_cast<Code**>(CodeObject().location()));
848 call(self, RelocInfo::CODE_TARGET);
851 // Move if the registers are not identical.
852 void Move(Register target, Register source);
854 // Push a handle value.
855 void Push(Handle<Object> handle) { push(Immediate(handle)); }
856 void Push(Smi* smi) { Push(Handle<Smi>(smi, isolate())); }
858 Handle<Object> CodeObject() {
859 ASSERT(!code_object_.is_null());
863 // Insert code to verify that the x87 stack has the specified depth (0-7)
864 void VerifyX87StackDepth(uint32_t depth);
866 // ---------------------------------------------------------------------------
867 // StatsCounter support
869 void SetCounter(StatsCounter* counter, int value);
870 void IncrementCounter(StatsCounter* counter, int value);
871 void DecrementCounter(StatsCounter* counter, int value);
872 void IncrementCounter(Condition cc, StatsCounter* counter, int value);
873 void DecrementCounter(Condition cc, StatsCounter* counter, int value);
876 // ---------------------------------------------------------------------------
879 // Calls Abort(msg) if the condition cc is not satisfied.
880 // Use --debug_code to enable.
881 void Assert(Condition cc, BailoutReason reason);
883 void AssertFastElements(Register elements);
885 // Like Assert(), but always enabled.
886 void Check(Condition cc, BailoutReason reason);
888 // Print a message to stdout and abort execution.
889 void Abort(BailoutReason reason);
891 // Check that the stack is aligned.
892 void CheckStackAlignment();
894 // Verify restrictions about code generated in stubs.
895 void set_generating_stub(bool value) { generating_stub_ = value; }
896 bool generating_stub() { return generating_stub_; }
897 void set_has_frame(bool value) { has_frame_ = value; }
898 bool has_frame() { return has_frame_; }
899 inline bool AllowThisStubCall(CodeStub* stub);
901 // ---------------------------------------------------------------------------
904 // Generate code to do a lookup in the number string cache. If the number in
905 // the register object is found in the cache the generated code falls through
906 // with the result in the result register. The object and the result register
907 // can be the same. If the number is not found in the cache the code jumps to
908 // the label not_found with only the content of register object unchanged.
909 void LookupNumberStringCache(Register object,
915 // Check whether the instance type represents a flat ASCII string. Jump to the
916 // label if not. If the instance type can be scratched specify same register
917 // for both instance type and scratch.
918 void JumpIfInstanceTypeIsNotSequentialAscii(Register instance_type,
920 Label* on_not_flat_ascii_string);
922 // Checks if both objects are sequential ASCII strings, and jumps to label
924 void JumpIfNotBothSequentialAsciiStrings(Register object1,
928 Label* on_not_flat_ascii_strings);
930 // Checks if the given register or operand is a unique name
931 void JumpIfNotUniqueName(Register reg, Label* not_unique_name,
932 Label::Distance distance = Label::kFar) {
933 JumpIfNotUniqueName(Operand(reg), not_unique_name, distance);
936 void JumpIfNotUniqueName(Operand operand, Label* not_unique_name,
937 Label::Distance distance = Label::kFar);
939 void EmitSeqStringSetCharCheck(Register string,
942 uint32_t encoding_mask);
944 static int SafepointRegisterStackIndex(Register reg) {
945 return SafepointRegisterStackIndex(reg.code());
948 // Activation support.
949 void EnterFrame(StackFrame::Type type);
950 void LeaveFrame(StackFrame::Type type);
952 // Expects object in eax and returns map with validated enum cache
953 // in eax. Assumes that any other register can be used as a scratch.
954 void CheckEnumCache(Label* call_runtime);
956 // AllocationMemento support. Arrays may have an associated
957 // AllocationMemento object that can be checked for in order to pretransition
959 // On entry, receiver_reg should point to the array object.
960 // scratch_reg gets clobbered.
961 // If allocation info is present, conditional code is set to equal.
962 void TestJSArrayForAllocationMemento(Register receiver_reg,
963 Register scratch_reg,
964 Label* no_memento_found);
966 void JumpIfJSArrayHasAllocationMemento(Register receiver_reg,
967 Register scratch_reg,
968 Label* memento_found) {
969 Label no_memento_found;
970 TestJSArrayForAllocationMemento(receiver_reg, scratch_reg,
972 j(equal, memento_found);
973 bind(&no_memento_found);
976 // Jumps to found label if a prototype map has dictionary elements.
977 void JumpIfDictionaryInPrototypeChain(Register object, Register scratch0,
978 Register scratch1, Label* found);
981 bool generating_stub_;
983 // This handle will be patched with the code object on installation.
984 Handle<Object> code_object_;
986 // Helper functions for generating invokes.
987 void InvokePrologue(const ParameterCount& expected,
988 const ParameterCount& actual,
989 Handle<Code> code_constant,
990 const Operand& code_operand,
992 bool* definitely_mismatches,
994 Label::Distance done_distance,
995 const CallWrapper& call_wrapper = NullCallWrapper());
997 void EnterExitFramePrologue();
998 void EnterExitFrameEpilogue(int argc, bool save_doubles);
1000 void LeaveExitFrameEpilogue(bool restore_context);
1002 // Allocation support helpers.
1003 void LoadAllocationTopHelper(Register result,
1005 AllocationFlags flags);
1007 void UpdateAllocationTopHelper(Register result_end,
1009 AllocationFlags flags);
1011 // Helper for PopHandleScope. Allowed to perform a GC and returns
1012 // NULL if gc_allowed. Does not perform a GC if !gc_allowed, and
1013 // possibly returns a failure object indicating an allocation failure.
1014 MUST_USE_RESULT MaybeObject* PopHandleScopeHelper(Register saved,
1018 // Helper for implementing JumpIfNotInNewSpace and JumpIfInNewSpace.
1019 void InNewSpace(Register object,
1022 Label* condition_met,
1023 Label::Distance condition_met_distance = Label::kFar);
1025 // Helper for finding the mark bits for an address. Afterwards, the
1026 // bitmap register points at the word with the mark bits and the mask
1027 // the position of the first bit. Uses ecx as scratch and leaves addr_reg
1029 inline void GetMarkBits(Register addr_reg,
1030 Register bitmap_reg,
1033 // Helper for throwing exceptions. Compute a handler address and jump to
1034 // it. See the implementation for register usage.
1035 void JumpToHandlerEntry();
1037 // Compute memory operands for safepoint stack slots.
1038 Operand SafepointRegisterSlot(Register reg);
1039 static int SafepointRegisterStackIndex(int reg_code);
1041 // Needs access to SafepointRegisterStackIndex for compiled frame
1043 friend class StandardFrame;
1047 // The code patcher is used to patch (typically) small parts of code e.g. for
1048 // debugging and other types of instrumentation. When using the code patcher
1049 // the exact number of bytes specified must be emitted. Is not legal to emit
1050 // relocation information. If any of these constraints are violated it causes
1054 CodePatcher(byte* address, int size);
1055 virtual ~CodePatcher();
1057 // Macro assembler to emit code.
1058 MacroAssembler* masm() { return &masm_; }
1061 byte* address_; // The address of the code being patched.
1062 int size_; // Number of bytes of the expected patch size.
1063 MacroAssembler masm_; // Macro assembler used to generate the code.
1067 // -----------------------------------------------------------------------------
1068 // Static helper functions.
1070 // Generate an Operand for loading a field from an object.
1071 inline Operand FieldOperand(Register object, int offset) {
1072 return Operand(object, offset - kHeapObjectTag);
1076 // Generate an Operand for loading an indexed field from an object.
1077 inline Operand FieldOperand(Register object,
1081 return Operand(object, index, scale, offset - kHeapObjectTag);
1085 inline Operand ContextOperand(Register context, int index) {
1086 return Operand(context, Context::SlotOffset(index));
1090 inline Operand GlobalObjectOperand() {
1091 return ContextOperand(esi, Context::GLOBAL_OBJECT_INDEX);
1095 // Generates an Operand for saving parameters after PrepareCallApiFunction.
1096 Operand ApiParameterOperand(int index);
1099 #ifdef GENERATED_CODE_COVERAGE
1100 extern void LogGeneratedCodeCoverage(const char* file_line);
1101 #define CODE_COVERAGE_STRINGIFY(x) #x
1102 #define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
1103 #define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
1104 #define ACCESS_MASM(masm) { \
1105 byte* ia32_coverage_function = \
1106 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
1109 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
1110 masm->call(ia32_coverage_function, RelocInfo::RUNTIME_ENTRY); \
1117 #define ACCESS_MASM(masm) masm->
1121 } } // namespace v8::internal
1123 #endif // V8_IA32_MACRO_ASSEMBLER_IA32_H_