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 global function with the given index.
266 void LoadGlobalFunction(int index, Register function);
268 // Load the initial map from the global function. The registers
269 // function and map can be the same.
270 void LoadGlobalFunctionInitialMap(Register function, Register map);
272 // Push and pop the registers that can hold pointers.
273 void PushSafepointRegisters() { pushad(); }
274 void PopSafepointRegisters() { popad(); }
275 // Store the value in register/immediate src in the safepoint
276 // register stack slot for register dst.
277 void StoreToSafepointRegisterSlot(Register dst, Register src);
278 void StoreToSafepointRegisterSlot(Register dst, Immediate src);
279 void LoadFromSafepointRegisterSlot(Register dst, Register src);
281 void LoadHeapObject(Register result, Handle<HeapObject> object);
282 void CmpHeapObject(Register reg, Handle<HeapObject> object);
283 void PushHeapObject(Handle<HeapObject> object);
285 void LoadObject(Register result, Handle<Object> object) {
286 AllowDeferredHandleDereference heap_object_check;
287 if (object->IsHeapObject()) {
288 LoadHeapObject(result, Handle<HeapObject>::cast(object));
290 Move(result, Immediate(object));
294 void CmpObject(Register reg, Handle<Object> object) {
295 AllowDeferredHandleDereference heap_object_check;
296 if (object->IsHeapObject()) {
297 CmpHeapObject(reg, Handle<HeapObject>::cast(object));
299 cmp(reg, Immediate(object));
303 // ---------------------------------------------------------------------------
304 // JavaScript invokes
306 // Invoke the JavaScript function code by either calling or jumping.
307 void InvokeCode(Register code,
308 const ParameterCount& expected,
309 const ParameterCount& actual,
311 const CallWrapper& call_wrapper) {
312 InvokeCode(Operand(code), expected, actual, flag, call_wrapper);
315 void InvokeCode(const Operand& code,
316 const ParameterCount& expected,
317 const ParameterCount& actual,
319 const CallWrapper& call_wrapper);
321 // Invoke the JavaScript function in the given register. Changes the
322 // current context to the context in the function before invoking.
323 void InvokeFunction(Register function,
324 const ParameterCount& actual,
326 const CallWrapper& call_wrapper);
328 void InvokeFunction(Register function,
329 const ParameterCount& expected,
330 const ParameterCount& actual,
332 const CallWrapper& call_wrapper);
334 void InvokeFunction(Handle<JSFunction> function,
335 const ParameterCount& expected,
336 const ParameterCount& actual,
338 const CallWrapper& call_wrapper);
340 // Invoke specified builtin JavaScript function. Adds an entry to
341 // the unresolved list if the name does not resolve.
342 void InvokeBuiltin(Builtins::JavaScript id,
344 const CallWrapper& call_wrapper = NullCallWrapper());
346 // Store the function for the given builtin in the target register.
347 void GetBuiltinFunction(Register target, Builtins::JavaScript id);
349 // Store the code object for the given builtin in the target register.
350 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
352 // Expression support
353 // cvtsi2sd instruction only writes to the low 64-bit of dst register, which
354 // hinders register renaming and makes dependence chains longer. So we use
355 // xorps to clear the dst register before cvtsi2sd to solve this issue.
356 void Cvtsi2sd(XMMRegister dst, Register src) { Cvtsi2sd(dst, Operand(src)); }
357 void Cvtsi2sd(XMMRegister dst, const Operand& src);
359 // Support for constant splitting.
360 bool IsUnsafeImmediate(const Immediate& x);
361 void SafeMove(Register dst, const Immediate& x);
362 void SafePush(const Immediate& x);
364 // Compare object type for heap object.
365 // Incoming register is heap_object and outgoing register is map.
366 void CmpObjectType(Register heap_object, InstanceType type, Register map);
368 // Compare instance type for map.
369 void CmpInstanceType(Register map, InstanceType type);
371 // Check if a map for a JSObject indicates that the object has fast elements.
372 // Jump to the specified label if it does not.
373 void CheckFastElements(Register map,
375 Label::Distance distance = Label::kFar);
377 // Check if a map for a JSObject indicates that the object can have both smi
378 // and HeapObject elements. Jump to the specified label if it does not.
379 void CheckFastObjectElements(Register map,
381 Label::Distance distance = Label::kFar);
383 // Check if a map for a JSObject indicates that the object has fast smi only
384 // elements. Jump to the specified label if it does not.
385 void CheckFastSmiElements(Register map,
387 Label::Distance distance = Label::kFar);
389 // Check to see if maybe_number can be stored as a double in
390 // FastDoubleElements. If it can, store it at the index specified by key in
391 // the FastDoubleElements array elements, otherwise jump to fail.
392 void StoreNumberToDoubleElements(Register maybe_number,
396 XMMRegister scratch2,
398 bool specialize_for_processor,
401 // Compare an object's map with the specified map.
402 void CompareMap(Register obj, Handle<Map> map);
404 // Check if the map of an object is equal to a specified map and branch to
405 // label if not. Skip the smi check if not required (object is known to be a
406 // heap object). If mode is ALLOW_ELEMENT_TRANSITION_MAPS, then also match
407 // against maps that are ElementsKind transition maps of the specified map.
408 void CheckMap(Register obj,
411 SmiCheckType smi_check_type);
413 // Check if the map of an object is equal to a specified map and branch to a
414 // specified target if equal. Skip the smi check if not required (object is
415 // known to be a heap object)
416 void DispatchMap(Register obj,
419 Handle<Code> success,
420 SmiCheckType smi_check_type);
422 // Check if the object in register heap_object is a string. Afterwards the
423 // register map contains the object map and the register instance_type
424 // contains the instance_type. The registers map and instance_type can be the
425 // same in which case it contains the instance type afterwards. Either of the
426 // registers map and instance_type can be the same as heap_object.
427 Condition IsObjectStringType(Register heap_object,
429 Register instance_type);
431 // Check if the object in register heap_object is a name. Afterwards the
432 // register map contains the object map and the register instance_type
433 // contains the instance_type. The registers map and instance_type can be the
434 // same in which case it contains the instance type afterwards. Either of the
435 // registers map and instance_type can be the same as heap_object.
436 Condition IsObjectNameType(Register heap_object,
438 Register instance_type);
440 // Check if a heap object's type is in the JSObject range, not including
441 // JSFunction. The object's map will be loaded in the map register.
442 // Any or all of the three registers may be the same.
443 // The contents of the scratch register will always be overwritten.
444 void IsObjectJSObjectType(Register heap_object,
449 // The contents of the scratch register will be overwritten.
450 void IsInstanceJSObjectType(Register map, Register scratch, Label* fail);
452 // FCmp is similar to integer cmp, but requires unsigned
453 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
456 void ClampUint8(Register reg);
458 void ClampDoubleToUint8(XMMRegister input_reg,
459 XMMRegister scratch_reg,
460 Register result_reg);
462 void SlowTruncateToI(Register result_reg, Register input_reg,
463 int offset = HeapNumber::kValueOffset - kHeapObjectTag);
465 void TruncateHeapNumberToI(Register result_reg, Register input_reg);
466 void TruncateDoubleToI(Register result_reg, XMMRegister input_reg);
467 void TruncateX87TOSToI(Register result_reg);
469 void DoubleToI(Register result_reg, XMMRegister input_reg,
470 XMMRegister scratch, MinusZeroMode minus_zero_mode,
471 Label* conversion_failed, Label::Distance dst = Label::kFar);
472 void X87TOSToI(Register result_reg, MinusZeroMode minus_zero_mode,
473 Label* conversion_failed, Label::Distance dst = Label::kFar);
475 void TaggedToI(Register result_reg, Register input_reg, XMMRegister temp,
476 MinusZeroMode minus_zero_mode, Label* lost_precision);
478 // Smi tagging support.
479 void SmiTag(Register reg) {
480 STATIC_ASSERT(kSmiTag == 0);
481 STATIC_ASSERT(kSmiTagSize == 1);
484 void SmiUntag(Register reg) {
485 sar(reg, kSmiTagSize);
488 // Modifies the register even if it does not contain a Smi!
489 void SmiUntag(Register reg, Label* is_smi) {
490 STATIC_ASSERT(kSmiTagSize == 1);
491 sar(reg, kSmiTagSize);
492 STATIC_ASSERT(kSmiTag == 0);
493 j(not_carry, is_smi);
496 void LoadUint32(XMMRegister dst, Register src, XMMRegister scratch);
497 void LoadUint32NoSSE2(Register 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);
525 template<typename Field>
526 void DecodeField(Register reg) {
527 static const int shift = Field::kShift;
528 static const int mask = (Field::kMask >> Field::kShift) << kSmiTagSize;
530 and_(reg, Immediate(mask));
532 void LoadPowerOf2(XMMRegister dst, Register scratch, int power);
534 // Abort execution if argument is not a number, enabled via --debug-code.
535 void AssertNumber(Register object);
537 // Abort execution if argument is not a smi, enabled via --debug-code.
538 void AssertSmi(Register object);
540 // Abort execution if argument is a smi, enabled via --debug-code.
541 void AssertNotSmi(Register object);
543 // Abort execution if argument is not a string, enabled via --debug-code.
544 void AssertString(Register object);
546 // Abort execution if argument is not a name, enabled via --debug-code.
547 void AssertName(Register object);
549 // Abort execution if argument is not undefined or an AllocationSite, enabled
551 void AssertUndefinedOrAllocationSite(Register object);
553 // ---------------------------------------------------------------------------
554 // Exception handling
556 // Push a new try handler and link it into try handler chain.
557 void PushTryHandler(StackHandler::Kind kind, int handler_index);
559 // Unlink the stack handler on top of the stack from the try handler chain.
560 void PopTryHandler();
562 // Throw to the top handler in the try hander chain.
563 void Throw(Register value);
565 // Throw past all JS frames to the top JS entry frame.
566 void ThrowUncatchable(Register value);
568 // Throw a message string as an exception.
569 void Throw(BailoutReason reason);
571 // Throw a message string as an exception if a condition is not true.
572 void ThrowIf(Condition cc, BailoutReason reason);
574 // ---------------------------------------------------------------------------
575 // Inline caching support
577 // Generate code for checking access rights - used for security checks
578 // on access to global objects across environments. The holder register
579 // is left untouched, but the scratch register is clobbered.
580 void CheckAccessGlobalProxy(Register holder_reg,
585 void GetNumberHash(Register r0, Register scratch);
587 void LoadFromNumberDictionary(Label* miss,
596 // ---------------------------------------------------------------------------
597 // Allocation support
599 // Allocate an object in new space or old pointer space. If the given space
600 // is exhausted control continues at the gc_required label. The allocated
601 // object is returned in result and end of the new object is returned in
602 // result_end. The register scratch can be passed as no_reg in which case
603 // an additional object reference will be added to the reloc info. The
604 // returned pointers in result and result_end have not yet been tagged as
605 // heap objects. If result_contains_top_on_entry is true the content of
606 // result is known to be the allocation top on entry (could be result_end
607 // from a previous call). If result_contains_top_on_entry is true scratch
608 // should be no_reg as it is never used.
609 void Allocate(int object_size,
614 AllocationFlags flags);
616 void Allocate(int header_size,
617 ScaleFactor element_size,
618 Register element_count,
619 RegisterValueType element_count_type,
624 AllocationFlags flags);
626 void Allocate(Register object_size,
631 AllocationFlags flags);
633 // Undo allocation in new space. The object passed and objects allocated after
634 // it will no longer be allocated. Make sure that no pointers are left to the
635 // object(s) no longer allocated as they would be invalid when allocation is
637 void UndoAllocationInNewSpace(Register object);
639 // Allocate a heap number in new space with undefined value. The
640 // register scratch2 can be passed as no_reg; the others must be
641 // valid registers. Returns tagged pointer in result register, or
642 // jumps to gc_required if new space is full.
643 void AllocateHeapNumber(Register result,
648 // Allocate a sequential string. All the header fields of the string object
650 void AllocateTwoByteString(Register result,
656 void AllocateAsciiString(Register result,
662 void AllocateAsciiString(Register result,
668 // Allocate a raw cons string object. Only the map field of the result is
670 void AllocateTwoByteConsString(Register result,
674 void AllocateAsciiConsString(Register result,
679 // Allocate a raw sliced string object. Only the map field of the result is
681 void AllocateTwoByteSlicedString(Register result,
685 void AllocateAsciiSlicedString(Register result,
690 // Copy memory, byte-by-byte, from source to destination. Not optimized for
691 // long or aligned copies.
692 // The contents of index and scratch are destroyed.
693 void CopyBytes(Register source,
694 Register destination,
698 // Initialize fields with filler values. Fields starting at |start_offset|
699 // not including end_offset are overwritten with the value in |filler|. At
700 // the end the loop, |start_offset| takes the value of |end_offset|.
701 void InitializeFieldsWithFiller(Register start_offset,
705 // ---------------------------------------------------------------------------
706 // Support functions.
708 // Check a boolean-bit of a Smi field.
709 void BooleanBitTest(Register object, int field_offset, int bit_index);
711 // Check if result is zero and op is negative.
712 void NegativeZeroTest(Register result, Register op, Label* then_label);
714 // Check if result is zero and any of op1 and op2 are negative.
715 // Register scratch is destroyed, and it must be different from op2.
716 void NegativeZeroTest(Register result, Register op1, Register op2,
717 Register scratch, Label* then_label);
719 // Try to get function prototype of a function and puts the value in
720 // the result register. Checks that the function really is a
721 // function and jumps to the miss label if the fast checks fail. The
722 // function register will be untouched; the other registers may be
724 void TryGetFunctionPrototype(Register function,
728 bool miss_on_bound_function = false);
730 // Generates code for reporting that an illegal operation has
732 void IllegalOperation(int num_arguments);
734 // Picks out an array index from the hash field.
736 // hash - holds the index's hash. Clobbered.
737 // index - holds the overwritten index on exit.
738 void IndexFromHash(Register hash, Register index);
740 // ---------------------------------------------------------------------------
743 // Call a code stub. Generate the code if necessary.
744 void CallStub(CodeStub* stub, TypeFeedbackId ast_id = TypeFeedbackId::None());
746 // Tail call a code stub (jump). Generate the code if necessary.
747 void TailCallStub(CodeStub* stub);
749 // Return from a code stub after popping its arguments.
750 void StubReturn(int argc);
752 // Call a runtime routine.
753 void CallRuntime(const Runtime::Function* f,
755 SaveFPRegsMode save_doubles = kDontSaveFPRegs);
756 void CallRuntimeSaveDoubles(Runtime::FunctionId id) {
757 const Runtime::Function* function = Runtime::FunctionForId(id);
758 CallRuntime(function, function->nargs, kSaveFPRegs);
761 // Convenience function: Same as above, but takes the fid instead.
762 void CallRuntime(Runtime::FunctionId id,
764 SaveFPRegsMode save_doubles = kDontSaveFPRegs) {
765 CallRuntime(Runtime::FunctionForId(id), num_arguments, save_doubles);
768 // Convenience function: call an external reference.
769 void CallExternalReference(ExternalReference ref, int num_arguments);
771 // Tail call of a runtime routine (jump).
772 // Like JumpToExternalReference, but also takes care of passing the number
774 void TailCallExternalReference(const ExternalReference& ext,
778 // Convenience function: tail call a runtime routine (jump).
779 void TailCallRuntime(Runtime::FunctionId fid,
783 // Before calling a C-function from generated code, align arguments on stack.
784 // After aligning the frame, arguments must be stored in esp[0], esp[4],
785 // etc., not pushed. The argument count assumes all arguments are word sized.
786 // Some compilers/platforms require the stack to be aligned when calling
788 // Needs a scratch register to do some arithmetic. This register will be
790 void PrepareCallCFunction(int num_arguments, Register scratch);
792 // Calls a C function and cleans up the space for arguments allocated
793 // by PrepareCallCFunction. The called function is not allowed to trigger a
794 // garbage collection, since that might move the code and invalidate the
795 // return address (unless this is somehow accounted for by the called
797 void CallCFunction(ExternalReference function, int num_arguments);
798 void CallCFunction(Register function, int num_arguments);
800 // Prepares stack to put arguments (aligns and so on). Reserves
801 // space for return value if needed (assumes the return value is a handle).
802 // Arguments must be stored in ApiParameterOperand(0), ApiParameterOperand(1)
803 // etc. Saves context (esi). If space was reserved for return value then
804 // stores the pointer to the reserved slot into esi.
805 void PrepareCallApiFunction(int argc);
807 // Calls an API function. Allocates HandleScope, extracts returned value
808 // from handle and propagates exceptions. Clobbers ebx, edi and
809 // caller-save registers. Restores context. On return removes
810 // stack_space * kPointerSize (GCed).
811 void CallApiFunctionAndReturn(Register function_address,
812 Address thunk_address,
813 Operand thunk_last_arg,
815 Operand return_value_operand,
816 Operand* context_restore_operand);
818 // Jump to a runtime routine.
819 void JumpToExternalReference(const ExternalReference& ext);
821 // ---------------------------------------------------------------------------
826 // Return and drop arguments from stack, where the number of arguments
827 // may be bigger than 2^16 - 1. Requires a scratch register.
828 void Ret(int bytes_dropped, Register scratch);
830 // Emit code to discard a non-negative number of pointer-sized elements
831 // from the stack, clobbering only the esp register.
832 void Drop(int element_count);
834 void Call(Label* target) { call(target); }
835 void Push(Register src) { push(src); }
836 void Pop(Register dst) { pop(dst); }
838 // Emit call to the code we are currently generating.
840 Handle<Code> self(reinterpret_cast<Code**>(CodeObject().location()));
841 call(self, RelocInfo::CODE_TARGET);
844 // Move if the registers are not identical.
845 void Move(Register target, Register source);
847 // Move a constant into a destination using the most efficient encoding.
848 void Move(Register dst, const Immediate& x);
849 void Move(const Operand& dst, const Immediate& x);
851 // Move an immediate into an XMM register.
852 void Move(XMMRegister dst, double val);
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 // Emit code for a truncating division by a constant. The dividend register is
867 // unchanged, the result is in edx, and eax gets clobbered.
868 void TruncatingDiv(Register dividend, int32_t divisor);
870 // ---------------------------------------------------------------------------
871 // StatsCounter support
873 void SetCounter(StatsCounter* counter, int value);
874 void IncrementCounter(StatsCounter* counter, int value);
875 void DecrementCounter(StatsCounter* counter, int value);
876 void IncrementCounter(Condition cc, StatsCounter* counter, int value);
877 void DecrementCounter(Condition cc, StatsCounter* counter, int value);
880 // ---------------------------------------------------------------------------
883 // Calls Abort(msg) if the condition cc is not satisfied.
884 // Use --debug_code to enable.
885 void Assert(Condition cc, BailoutReason reason);
887 void AssertFastElements(Register elements);
889 // Like Assert(), but always enabled.
890 void Check(Condition cc, BailoutReason reason);
892 // Print a message to stdout and abort execution.
893 void Abort(BailoutReason reason);
895 // Check that the stack is aligned.
896 void CheckStackAlignment();
898 // Verify restrictions about code generated in stubs.
899 void set_generating_stub(bool value) { generating_stub_ = value; }
900 bool generating_stub() { return generating_stub_; }
901 void set_has_frame(bool value) { has_frame_ = value; }
902 bool has_frame() { return has_frame_; }
903 inline bool AllowThisStubCall(CodeStub* stub);
905 // ---------------------------------------------------------------------------
908 // Generate code to do a lookup in the number string cache. If the number in
909 // the register object is found in the cache the generated code falls through
910 // with the result in the result register. The object and the result register
911 // can be the same. If the number is not found in the cache the code jumps to
912 // the label not_found with only the content of register object unchanged.
913 void LookupNumberStringCache(Register object,
919 // Check whether the instance type represents a flat ASCII string. Jump to the
920 // label if not. If the instance type can be scratched specify same register
921 // for both instance type and scratch.
922 void JumpIfInstanceTypeIsNotSequentialAscii(Register instance_type,
924 Label* on_not_flat_ascii_string);
926 // Checks if both objects are sequential ASCII strings, and jumps to label
928 void JumpIfNotBothSequentialAsciiStrings(Register object1,
932 Label* on_not_flat_ascii_strings);
934 // Checks if the given register or operand is a unique name
935 void JumpIfNotUniqueName(Register reg, Label* not_unique_name,
936 Label::Distance distance = Label::kFar) {
937 JumpIfNotUniqueName(Operand(reg), not_unique_name, distance);
940 void JumpIfNotUniqueName(Operand operand, Label* not_unique_name,
941 Label::Distance distance = Label::kFar);
943 void EmitSeqStringSetCharCheck(Register string,
946 uint32_t encoding_mask);
948 static int SafepointRegisterStackIndex(Register reg) {
949 return SafepointRegisterStackIndex(reg.code());
952 // Activation support.
953 void EnterFrame(StackFrame::Type type);
954 void LeaveFrame(StackFrame::Type type);
956 // Expects object in eax and returns map with validated enum cache
957 // in eax. Assumes that any other register can be used as a scratch.
958 void CheckEnumCache(Label* call_runtime);
960 // AllocationMemento support. Arrays may have an associated
961 // AllocationMemento object that can be checked for in order to pretransition
963 // On entry, receiver_reg should point to the array object.
964 // scratch_reg gets clobbered.
965 // If allocation info is present, conditional code is set to equal.
966 void TestJSArrayForAllocationMemento(Register receiver_reg,
967 Register scratch_reg,
968 Label* no_memento_found);
970 void JumpIfJSArrayHasAllocationMemento(Register receiver_reg,
971 Register scratch_reg,
972 Label* memento_found) {
973 Label no_memento_found;
974 TestJSArrayForAllocationMemento(receiver_reg, scratch_reg,
976 j(equal, memento_found);
977 bind(&no_memento_found);
980 // Jumps to found label if a prototype map has dictionary elements.
981 void JumpIfDictionaryInPrototypeChain(Register object, Register scratch0,
982 Register scratch1, Label* found);
985 bool generating_stub_;
987 // This handle will be patched with the code object on installation.
988 Handle<Object> code_object_;
990 // Helper functions for generating invokes.
991 void InvokePrologue(const ParameterCount& expected,
992 const ParameterCount& actual,
993 Handle<Code> code_constant,
994 const Operand& code_operand,
996 bool* definitely_mismatches,
998 Label::Distance done_distance,
999 const CallWrapper& call_wrapper = NullCallWrapper());
1001 void EnterExitFramePrologue();
1002 void EnterExitFrameEpilogue(int argc, bool save_doubles);
1004 void LeaveExitFrameEpilogue(bool restore_context);
1006 // Allocation support helpers.
1007 void LoadAllocationTopHelper(Register result,
1009 AllocationFlags flags);
1011 void UpdateAllocationTopHelper(Register result_end,
1013 AllocationFlags flags);
1015 // Helper for PopHandleScope. Allowed to perform a GC and returns
1016 // NULL if gc_allowed. Does not perform a GC if !gc_allowed, and
1017 // possibly returns a failure object indicating an allocation failure.
1018 MUST_USE_RESULT MaybeObject* PopHandleScopeHelper(Register saved,
1022 // Helper for implementing JumpIfNotInNewSpace and JumpIfInNewSpace.
1023 void InNewSpace(Register object,
1026 Label* condition_met,
1027 Label::Distance condition_met_distance = Label::kFar);
1029 // Helper for finding the mark bits for an address. Afterwards, the
1030 // bitmap register points at the word with the mark bits and the mask
1031 // the position of the first bit. Uses ecx as scratch and leaves addr_reg
1033 inline void GetMarkBits(Register addr_reg,
1034 Register bitmap_reg,
1037 // Helper for throwing exceptions. Compute a handler address and jump to
1038 // it. See the implementation for register usage.
1039 void JumpToHandlerEntry();
1041 // Compute memory operands for safepoint stack slots.
1042 Operand SafepointRegisterSlot(Register reg);
1043 static int SafepointRegisterStackIndex(int reg_code);
1045 // Needs access to SafepointRegisterStackIndex for compiled frame
1047 friend class StandardFrame;
1051 // The code patcher is used to patch (typically) small parts of code e.g. for
1052 // debugging and other types of instrumentation. When using the code patcher
1053 // the exact number of bytes specified must be emitted. Is not legal to emit
1054 // relocation information. If any of these constraints are violated it causes
1058 CodePatcher(byte* address, int size);
1059 virtual ~CodePatcher();
1061 // Macro assembler to emit code.
1062 MacroAssembler* masm() { return &masm_; }
1065 byte* address_; // The address of the code being patched.
1066 int size_; // Number of bytes of the expected patch size.
1067 MacroAssembler masm_; // Macro assembler used to generate the code.
1071 // -----------------------------------------------------------------------------
1072 // Static helper functions.
1074 // Generate an Operand for loading a field from an object.
1075 inline Operand FieldOperand(Register object, int offset) {
1076 return Operand(object, offset - kHeapObjectTag);
1080 // Generate an Operand for loading an indexed field from an object.
1081 inline Operand FieldOperand(Register object,
1085 return Operand(object, index, scale, offset - kHeapObjectTag);
1089 inline Operand FixedArrayElementOperand(Register array,
1090 Register index_as_smi,
1091 int additional_offset = 0) {
1092 int offset = FixedArray::kHeaderSize + additional_offset * kPointerSize;
1093 return FieldOperand(array, index_as_smi, times_half_pointer_size, offset);
1097 inline Operand ContextOperand(Register context, int index) {
1098 return Operand(context, Context::SlotOffset(index));
1102 inline Operand GlobalObjectOperand() {
1103 return ContextOperand(esi, Context::GLOBAL_OBJECT_INDEX);
1107 // Generates an Operand for saving parameters after PrepareCallApiFunction.
1108 Operand ApiParameterOperand(int index);
1111 #ifdef GENERATED_CODE_COVERAGE
1112 extern void LogGeneratedCodeCoverage(const char* file_line);
1113 #define CODE_COVERAGE_STRINGIFY(x) #x
1114 #define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
1115 #define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
1116 #define ACCESS_MASM(masm) { \
1117 byte* ia32_coverage_function = \
1118 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
1121 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
1122 masm->call(ia32_coverage_function, RelocInfo::RUNTIME_ENTRY); \
1129 #define ACCESS_MASM(masm) masm->
1133 } } // namespace v8::internal
1135 #endif // V8_IA32_MACRO_ASSEMBLER_IA32_H_