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.
7 #if V8_TARGET_ARCH_IA32
9 #include "src/base/bits.h"
10 #include "src/code-factory.h"
11 #include "src/code-stubs.h"
12 #include "src/codegen.h"
13 #include "src/deoptimizer.h"
14 #include "src/hydrogen-osr.h"
15 #include "src/ia32/lithium-codegen-ia32.h"
16 #include "src/ic/ic.h"
17 #include "src/ic/stub-cache.h"
22 // When invoking builtins, we need to record the safepoint in the middle of
23 // the invoke instruction sequence generated by the macro assembler.
24 class SafepointGenerator FINAL : public CallWrapper {
26 SafepointGenerator(LCodeGen* codegen,
27 LPointerMap* pointers,
28 Safepoint::DeoptMode mode)
32 virtual ~SafepointGenerator() {}
34 virtual void BeforeCall(int call_size) const OVERRIDE {}
36 virtual void AfterCall() const OVERRIDE {
37 codegen_->RecordSafepoint(pointers_, deopt_mode_);
42 LPointerMap* pointers_;
43 Safepoint::DeoptMode deopt_mode_;
49 bool LCodeGen::GenerateCode() {
50 LPhase phase("Z_Code generation", chunk());
54 // Open a frame scope to indicate that there is a frame on the stack. The
55 // MANUAL indicates that the scope shouldn't actually generate code to set up
56 // the frame (that is done in GeneratePrologue).
57 FrameScope frame_scope(masm_, StackFrame::MANUAL);
59 support_aligned_spilled_doubles_ = info()->IsOptimizing();
61 dynamic_frame_alignment_ = info()->IsOptimizing() &&
62 ((chunk()->num_double_slots() > 2 &&
63 !chunk()->graph()->is_recursive()) ||
64 !info()->osr_ast_id().IsNone());
66 return GeneratePrologue() &&
68 GenerateDeferredCode() &&
69 GenerateJumpTable() &&
70 GenerateSafepointTable();
74 void LCodeGen::FinishCode(Handle<Code> code) {
76 code->set_stack_slots(GetStackSlotCount());
77 code->set_safepoint_table_offset(safepoints_.GetCodeOffset());
78 if (code->is_optimized_code()) RegisterWeakObjectsInOptimizedCode(code);
79 PopulateDeoptimizationData(code);
80 if (!info()->IsStub()) {
81 Deoptimizer::EnsureRelocSpaceForLazyDeoptimization(code);
87 void LCodeGen::MakeSureStackPagesMapped(int offset) {
88 const int kPageSize = 4 * KB;
89 for (offset -= kPageSize; offset > 0; offset -= kPageSize) {
90 __ mov(Operand(esp, offset), eax);
96 void LCodeGen::SaveCallerDoubles() {
97 DCHECK(info()->saves_caller_doubles());
98 DCHECK(NeedsEagerFrame());
99 Comment(";;; Save clobbered callee double registers");
101 BitVector* doubles = chunk()->allocated_double_registers();
102 BitVector::Iterator save_iterator(doubles);
103 while (!save_iterator.Done()) {
104 __ movsd(MemOperand(esp, count * kDoubleSize),
105 XMMRegister::FromAllocationIndex(save_iterator.Current()));
106 save_iterator.Advance();
112 void LCodeGen::RestoreCallerDoubles() {
113 DCHECK(info()->saves_caller_doubles());
114 DCHECK(NeedsEagerFrame());
115 Comment(";;; Restore clobbered callee double registers");
116 BitVector* doubles = chunk()->allocated_double_registers();
117 BitVector::Iterator save_iterator(doubles);
119 while (!save_iterator.Done()) {
120 __ movsd(XMMRegister::FromAllocationIndex(save_iterator.Current()),
121 MemOperand(esp, count * kDoubleSize));
122 save_iterator.Advance();
128 bool LCodeGen::GeneratePrologue() {
129 DCHECK(is_generating());
131 if (info()->IsOptimizing()) {
132 ProfileEntryHookStub::MaybeCallEntryHook(masm_);
135 if (strlen(FLAG_stop_at) > 0 &&
136 info_->function()->name()->IsUtf8EqualTo(CStrVector(FLAG_stop_at))) {
141 // Sloppy mode functions and builtins need to replace the receiver with the
142 // global proxy when called as functions (without an explicit receiver
144 if (info_->this_has_uses() &&
145 info_->strict_mode() == SLOPPY &&
146 !info_->is_native()) {
148 // +1 for return address.
149 int receiver_offset = (scope()->num_parameters() + 1) * kPointerSize;
150 __ mov(ecx, Operand(esp, receiver_offset));
152 __ cmp(ecx, isolate()->factory()->undefined_value());
153 __ j(not_equal, &ok, Label::kNear);
155 __ mov(ecx, GlobalObjectOperand());
156 __ mov(ecx, FieldOperand(ecx, GlobalObject::kGlobalProxyOffset));
158 __ mov(Operand(esp, receiver_offset), ecx);
163 if (support_aligned_spilled_doubles_ && dynamic_frame_alignment_) {
164 // Move state of dynamic frame alignment into edx.
165 __ Move(edx, Immediate(kNoAlignmentPadding));
167 Label do_not_pad, align_loop;
168 STATIC_ASSERT(kDoubleSize == 2 * kPointerSize);
169 // Align esp + 4 to a multiple of 2 * kPointerSize.
170 __ test(esp, Immediate(kPointerSize));
171 __ j(not_zero, &do_not_pad, Label::kNear);
172 __ push(Immediate(0));
174 __ mov(edx, Immediate(kAlignmentPaddingPushed));
175 // Copy arguments, receiver, and return address.
176 __ mov(ecx, Immediate(scope()->num_parameters() + 2));
178 __ bind(&align_loop);
179 __ mov(eax, Operand(ebx, 1 * kPointerSize));
180 __ mov(Operand(ebx, 0), eax);
181 __ add(Operand(ebx), Immediate(kPointerSize));
183 __ j(not_zero, &align_loop, Label::kNear);
184 __ mov(Operand(ebx, 0), Immediate(kAlignmentZapValue));
185 __ bind(&do_not_pad);
189 info()->set_prologue_offset(masm_->pc_offset());
190 if (NeedsEagerFrame()) {
191 DCHECK(!frame_is_built_);
192 frame_is_built_ = true;
193 if (info()->IsStub()) {
196 __ Prologue(info()->IsCodePreAgingActive());
198 info()->AddNoFrameRange(0, masm_->pc_offset());
201 if (info()->IsOptimizing() &&
202 dynamic_frame_alignment_ &&
204 __ test(esp, Immediate(kPointerSize));
205 __ Assert(zero, kFrameIsExpectedToBeAligned);
208 // Reserve space for the stack slots needed by the code.
209 int slots = GetStackSlotCount();
210 DCHECK(slots != 0 || !info()->IsOptimizing());
213 if (dynamic_frame_alignment_) {
216 __ push(Immediate(kNoAlignmentPadding));
219 if (FLAG_debug_code) {
220 __ sub(Operand(esp), Immediate(slots * kPointerSize));
222 MakeSureStackPagesMapped(slots * kPointerSize);
225 __ mov(Operand(eax), Immediate(slots));
228 __ mov(MemOperand(esp, eax, times_4, 0),
229 Immediate(kSlotsZapValue));
231 __ j(not_zero, &loop);
234 __ sub(Operand(esp), Immediate(slots * kPointerSize));
236 MakeSureStackPagesMapped(slots * kPointerSize);
240 if (support_aligned_spilled_doubles_) {
241 Comment(";;; Store dynamic frame alignment tag for spilled doubles");
242 // Store dynamic frame alignment state in the first local.
243 int offset = JavaScriptFrameConstants::kDynamicAlignmentStateOffset;
244 if (dynamic_frame_alignment_) {
245 __ mov(Operand(ebp, offset), edx);
247 __ mov(Operand(ebp, offset), Immediate(kNoAlignmentPadding));
252 if (info()->saves_caller_doubles()) SaveCallerDoubles();
255 // Possibly allocate a local context.
256 int heap_slots = info_->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
257 if (heap_slots > 0) {
258 Comment(";;; Allocate local context");
259 bool need_write_barrier = true;
260 // Argument to NewContext is the function, which is still in edi.
261 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
262 FastNewContextStub stub(isolate(), heap_slots);
264 // Result of FastNewContextStub is always in new space.
265 need_write_barrier = false;
268 __ CallRuntime(Runtime::kNewFunctionContext, 1);
270 RecordSafepoint(Safepoint::kNoLazyDeopt);
271 // Context is returned in eax. It replaces the context passed to us.
272 // It's saved in the stack and kept live in esi.
274 __ mov(Operand(ebp, StandardFrameConstants::kContextOffset), eax);
276 // Copy parameters into context if necessary.
277 int num_parameters = scope()->num_parameters();
278 for (int i = 0; i < num_parameters; i++) {
279 Variable* var = scope()->parameter(i);
280 if (var->IsContextSlot()) {
281 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
282 (num_parameters - 1 - i) * kPointerSize;
283 // Load parameter from stack.
284 __ mov(eax, Operand(ebp, parameter_offset));
285 // Store it in the context.
286 int context_offset = Context::SlotOffset(var->index());
287 __ mov(Operand(esi, context_offset), eax);
288 // Update the write barrier. This clobbers eax and ebx.
289 if (need_write_barrier) {
290 __ RecordWriteContextSlot(esi,
295 } else if (FLAG_debug_code) {
297 __ JumpIfInNewSpace(esi, eax, &done, Label::kNear);
298 __ Abort(kExpectedNewSpaceObject);
303 Comment(";;; End allocate local context");
307 if (FLAG_trace && info()->IsOptimizing()) {
308 // We have not executed any compiled code yet, so esi still holds the
310 __ CallRuntime(Runtime::kTraceEnter, 0);
312 return !is_aborted();
316 void LCodeGen::GenerateOsrPrologue() {
317 // Generate the OSR entry prologue at the first unknown OSR value, or if there
318 // are none, at the OSR entrypoint instruction.
319 if (osr_pc_offset_ >= 0) return;
321 osr_pc_offset_ = masm()->pc_offset();
323 // Move state of dynamic frame alignment into edx.
324 __ Move(edx, Immediate(kNoAlignmentPadding));
326 if (support_aligned_spilled_doubles_ && dynamic_frame_alignment_) {
327 Label do_not_pad, align_loop;
328 // Align ebp + 4 to a multiple of 2 * kPointerSize.
329 __ test(ebp, Immediate(kPointerSize));
330 __ j(zero, &do_not_pad, Label::kNear);
331 __ push(Immediate(0));
333 __ mov(edx, Immediate(kAlignmentPaddingPushed));
335 // Move all parts of the frame over one word. The frame consists of:
336 // unoptimized frame slots, alignment state, context, frame pointer, return
337 // address, receiver, and the arguments.
338 __ mov(ecx, Immediate(scope()->num_parameters() +
339 5 + graph()->osr()->UnoptimizedFrameSlots()));
341 __ bind(&align_loop);
342 __ mov(eax, Operand(ebx, 1 * kPointerSize));
343 __ mov(Operand(ebx, 0), eax);
344 __ add(Operand(ebx), Immediate(kPointerSize));
346 __ j(not_zero, &align_loop, Label::kNear);
347 __ mov(Operand(ebx, 0), Immediate(kAlignmentZapValue));
348 __ sub(Operand(ebp), Immediate(kPointerSize));
349 __ bind(&do_not_pad);
352 // Save the first local, which is overwritten by the alignment state.
353 Operand alignment_loc = MemOperand(ebp, -3 * kPointerSize);
354 __ push(alignment_loc);
356 // Set the dynamic frame alignment state.
357 __ mov(alignment_loc, edx);
359 // Adjust the frame size, subsuming the unoptimized frame into the
361 int slots = GetStackSlotCount() - graph()->osr()->UnoptimizedFrameSlots();
363 __ sub(esp, Immediate((slots - 1) * kPointerSize));
367 void LCodeGen::GenerateBodyInstructionPre(LInstruction* instr) {
368 if (instr->IsCall()) {
369 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
371 if (!instr->IsLazyBailout() && !instr->IsGap()) {
372 safepoints_.BumpLastLazySafepointIndex();
377 void LCodeGen::GenerateBodyInstructionPost(LInstruction* instr) { }
380 bool LCodeGen::GenerateJumpTable() {
382 if (jump_table_.length() > 0) {
383 Comment(";;; -------------------- Jump table --------------------");
385 for (int i = 0; i < jump_table_.length(); i++) {
386 Deoptimizer::JumpTableEntry* table_entry = &jump_table_[i];
387 __ bind(&table_entry->label);
388 Address entry = table_entry->address;
389 DeoptComment(table_entry->reason);
390 if (table_entry->needs_frame) {
391 DCHECK(!info()->saves_caller_doubles());
392 __ push(Immediate(ExternalReference::ForDeoptEntry(entry)));
393 if (needs_frame.is_bound()) {
394 __ jmp(&needs_frame);
396 __ bind(&needs_frame);
397 __ push(MemOperand(ebp, StandardFrameConstants::kContextOffset));
398 // This variant of deopt can only be used with stubs. Since we don't
399 // have a function pointer to install in the stack frame that we're
400 // building, install a special marker there instead.
401 DCHECK(info()->IsStub());
402 __ push(Immediate(Smi::FromInt(StackFrame::STUB)));
403 // Push a PC inside the function so that the deopt code can find where
404 // the deopt comes from. It doesn't have to be the precise return
405 // address of a "calling" LAZY deopt, it only has to be somewhere
406 // inside the code body.
407 Label push_approx_pc;
408 __ call(&push_approx_pc);
409 __ bind(&push_approx_pc);
410 // Push the continuation which was stashed were the ebp should
411 // be. Replace it with the saved ebp.
412 __ push(MemOperand(esp, 3 * kPointerSize));
413 __ mov(MemOperand(esp, 4 * kPointerSize), ebp);
414 __ lea(ebp, MemOperand(esp, 4 * kPointerSize));
415 __ ret(0); // Call the continuation without clobbering registers.
418 if (info()->saves_caller_doubles()) RestoreCallerDoubles();
419 __ call(entry, RelocInfo::RUNTIME_ENTRY);
422 return !is_aborted();
426 bool LCodeGen::GenerateDeferredCode() {
427 DCHECK(is_generating());
428 if (deferred_.length() > 0) {
429 for (int i = 0; !is_aborted() && i < deferred_.length(); i++) {
430 LDeferredCode* code = deferred_[i];
433 instructions_->at(code->instruction_index())->hydrogen_value();
434 RecordAndWritePosition(
435 chunk()->graph()->SourcePositionToScriptPosition(value->position()));
437 Comment(";;; <@%d,#%d> "
438 "-------------------- Deferred %s --------------------",
439 code->instruction_index(),
440 code->instr()->hydrogen_value()->id(),
441 code->instr()->Mnemonic());
442 __ bind(code->entry());
443 if (NeedsDeferredFrame()) {
444 Comment(";;; Build frame");
445 DCHECK(!frame_is_built_);
446 DCHECK(info()->IsStub());
447 frame_is_built_ = true;
448 // Build the frame in such a way that esi isn't trashed.
449 __ push(ebp); // Caller's frame pointer.
450 __ push(Operand(ebp, StandardFrameConstants::kContextOffset));
451 __ push(Immediate(Smi::FromInt(StackFrame::STUB)));
452 __ lea(ebp, Operand(esp, 2 * kPointerSize));
453 Comment(";;; Deferred code");
456 if (NeedsDeferredFrame()) {
457 __ bind(code->done());
458 Comment(";;; Destroy frame");
459 DCHECK(frame_is_built_);
460 frame_is_built_ = false;
464 __ jmp(code->exit());
468 // Deferred code is the last part of the instruction sequence. Mark
469 // the generated code as done unless we bailed out.
470 if (!is_aborted()) status_ = DONE;
471 return !is_aborted();
475 bool LCodeGen::GenerateSafepointTable() {
477 if (!info()->IsStub()) {
478 // For lazy deoptimization we need space to patch a call after every call.
479 // Ensure there is always space for such patching, even if the code ends
481 int target_offset = masm()->pc_offset() + Deoptimizer::patch_size();
482 while (masm()->pc_offset() < target_offset) {
486 safepoints_.Emit(masm(), GetStackSlotCount());
487 return !is_aborted();
491 Register LCodeGen::ToRegister(int index) const {
492 return Register::FromAllocationIndex(index);
496 XMMRegister LCodeGen::ToDoubleRegister(int index) const {
497 return XMMRegister::FromAllocationIndex(index);
501 Register LCodeGen::ToRegister(LOperand* op) const {
502 DCHECK(op->IsRegister());
503 return ToRegister(op->index());
507 XMMRegister LCodeGen::ToDoubleRegister(LOperand* op) const {
508 DCHECK(op->IsDoubleRegister());
509 return ToDoubleRegister(op->index());
513 int32_t LCodeGen::ToInteger32(LConstantOperand* op) const {
514 return ToRepresentation(op, Representation::Integer32());
518 int32_t LCodeGen::ToRepresentation(LConstantOperand* op,
519 const Representation& r) const {
520 HConstant* constant = chunk_->LookupConstant(op);
521 int32_t value = constant->Integer32Value();
522 if (r.IsInteger32()) return value;
523 DCHECK(r.IsSmiOrTagged());
524 return reinterpret_cast<int32_t>(Smi::FromInt(value));
528 Handle<Object> LCodeGen::ToHandle(LConstantOperand* op) const {
529 HConstant* constant = chunk_->LookupConstant(op);
530 DCHECK(chunk_->LookupLiteralRepresentation(op).IsSmiOrTagged());
531 return constant->handle(isolate());
535 double LCodeGen::ToDouble(LConstantOperand* op) const {
536 HConstant* constant = chunk_->LookupConstant(op);
537 DCHECK(constant->HasDoubleValue());
538 return constant->DoubleValue();
542 ExternalReference LCodeGen::ToExternalReference(LConstantOperand* op) const {
543 HConstant* constant = chunk_->LookupConstant(op);
544 DCHECK(constant->HasExternalReferenceValue());
545 return constant->ExternalReferenceValue();
549 bool LCodeGen::IsInteger32(LConstantOperand* op) const {
550 return chunk_->LookupLiteralRepresentation(op).IsSmiOrInteger32();
554 bool LCodeGen::IsSmi(LConstantOperand* op) const {
555 return chunk_->LookupLiteralRepresentation(op).IsSmi();
559 static int ArgumentsOffsetWithoutFrame(int index) {
561 return -(index + 1) * kPointerSize + kPCOnStackSize;
565 Operand LCodeGen::ToOperand(LOperand* op) const {
566 if (op->IsRegister()) return Operand(ToRegister(op));
567 if (op->IsDoubleRegister()) return Operand(ToDoubleRegister(op));
568 DCHECK(op->IsStackSlot() || op->IsDoubleStackSlot());
569 if (NeedsEagerFrame()) {
570 return Operand(ebp, StackSlotOffset(op->index()));
572 // Retrieve parameter without eager stack-frame relative to the
574 return Operand(esp, ArgumentsOffsetWithoutFrame(op->index()));
579 Operand LCodeGen::HighOperand(LOperand* op) {
580 DCHECK(op->IsDoubleStackSlot());
581 if (NeedsEagerFrame()) {
582 return Operand(ebp, StackSlotOffset(op->index()) + kPointerSize);
584 // Retrieve parameter without eager stack-frame relative to the
587 esp, ArgumentsOffsetWithoutFrame(op->index()) + kPointerSize);
592 void LCodeGen::WriteTranslation(LEnvironment* environment,
593 Translation* translation) {
594 if (environment == NULL) return;
596 // The translation includes one command per value in the environment.
597 int translation_size = environment->translation_size();
598 // The output frame height does not include the parameters.
599 int height = translation_size - environment->parameter_count();
601 WriteTranslation(environment->outer(), translation);
602 bool has_closure_id = !info()->closure().is_null() &&
603 !info()->closure().is_identical_to(environment->closure());
604 int closure_id = has_closure_id
605 ? DefineDeoptimizationLiteral(environment->closure())
606 : Translation::kSelfLiteralId;
607 switch (environment->frame_type()) {
609 translation->BeginJSFrame(environment->ast_id(), closure_id, height);
612 translation->BeginConstructStubFrame(closure_id, translation_size);
615 DCHECK(translation_size == 1);
617 translation->BeginGetterStubFrame(closure_id);
620 DCHECK(translation_size == 2);
622 translation->BeginSetterStubFrame(closure_id);
624 case ARGUMENTS_ADAPTOR:
625 translation->BeginArgumentsAdaptorFrame(closure_id, translation_size);
628 translation->BeginCompiledStubFrame();
634 int object_index = 0;
635 int dematerialized_index = 0;
636 for (int i = 0; i < translation_size; ++i) {
637 LOperand* value = environment->values()->at(i);
638 AddToTranslation(environment,
641 environment->HasTaggedValueAt(i),
642 environment->HasUint32ValueAt(i),
644 &dematerialized_index);
649 void LCodeGen::AddToTranslation(LEnvironment* environment,
650 Translation* translation,
654 int* object_index_pointer,
655 int* dematerialized_index_pointer) {
656 if (op == LEnvironment::materialization_marker()) {
657 int object_index = (*object_index_pointer)++;
658 if (environment->ObjectIsDuplicateAt(object_index)) {
659 int dupe_of = environment->ObjectDuplicateOfAt(object_index);
660 translation->DuplicateObject(dupe_of);
663 int object_length = environment->ObjectLengthAt(object_index);
664 if (environment->ObjectIsArgumentsAt(object_index)) {
665 translation->BeginArgumentsObject(object_length);
667 translation->BeginCapturedObject(object_length);
669 int dematerialized_index = *dematerialized_index_pointer;
670 int env_offset = environment->translation_size() + dematerialized_index;
671 *dematerialized_index_pointer += object_length;
672 for (int i = 0; i < object_length; ++i) {
673 LOperand* value = environment->values()->at(env_offset + i);
674 AddToTranslation(environment,
677 environment->HasTaggedValueAt(env_offset + i),
678 environment->HasUint32ValueAt(env_offset + i),
679 object_index_pointer,
680 dematerialized_index_pointer);
685 if (op->IsStackSlot()) {
687 translation->StoreStackSlot(op->index());
688 } else if (is_uint32) {
689 translation->StoreUint32StackSlot(op->index());
691 translation->StoreInt32StackSlot(op->index());
693 } else if (op->IsDoubleStackSlot()) {
694 translation->StoreDoubleStackSlot(op->index());
695 } else if (op->IsRegister()) {
696 Register reg = ToRegister(op);
698 translation->StoreRegister(reg);
699 } else if (is_uint32) {
700 translation->StoreUint32Register(reg);
702 translation->StoreInt32Register(reg);
704 } else if (op->IsDoubleRegister()) {
705 XMMRegister reg = ToDoubleRegister(op);
706 translation->StoreDoubleRegister(reg);
707 } else if (op->IsConstantOperand()) {
708 HConstant* constant = chunk()->LookupConstant(LConstantOperand::cast(op));
709 int src_index = DefineDeoptimizationLiteral(constant->handle(isolate()));
710 translation->StoreLiteral(src_index);
717 void LCodeGen::CallCodeGeneric(Handle<Code> code,
718 RelocInfo::Mode mode,
720 SafepointMode safepoint_mode) {
721 DCHECK(instr != NULL);
723 RecordSafepointWithLazyDeopt(instr, safepoint_mode);
725 // Signal that we don't inline smi code before these stubs in the
726 // optimizing code generator.
727 if (code->kind() == Code::BINARY_OP_IC ||
728 code->kind() == Code::COMPARE_IC) {
734 void LCodeGen::CallCode(Handle<Code> code,
735 RelocInfo::Mode mode,
736 LInstruction* instr) {
737 CallCodeGeneric(code, mode, instr, RECORD_SIMPLE_SAFEPOINT);
741 void LCodeGen::CallRuntime(const Runtime::Function* fun,
744 SaveFPRegsMode save_doubles) {
745 DCHECK(instr != NULL);
746 DCHECK(instr->HasPointerMap());
748 __ CallRuntime(fun, argc, save_doubles);
750 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
752 DCHECK(info()->is_calling());
756 void LCodeGen::LoadContextFromDeferred(LOperand* context) {
757 if (context->IsRegister()) {
758 if (!ToRegister(context).is(esi)) {
759 __ mov(esi, ToRegister(context));
761 } else if (context->IsStackSlot()) {
762 __ mov(esi, ToOperand(context));
763 } else if (context->IsConstantOperand()) {
764 HConstant* constant =
765 chunk_->LookupConstant(LConstantOperand::cast(context));
766 __ LoadObject(esi, Handle<Object>::cast(constant->handle(isolate())));
772 void LCodeGen::CallRuntimeFromDeferred(Runtime::FunctionId id,
776 LoadContextFromDeferred(context);
778 __ CallRuntimeSaveDoubles(id);
779 RecordSafepointWithRegisters(
780 instr->pointer_map(), argc, Safepoint::kNoLazyDeopt);
782 DCHECK(info()->is_calling());
786 void LCodeGen::RegisterEnvironmentForDeoptimization(
787 LEnvironment* environment, Safepoint::DeoptMode mode) {
788 environment->set_has_been_used();
789 if (!environment->HasBeenRegistered()) {
790 // Physical stack frame layout:
791 // -x ............. -4 0 ..................................... y
792 // [incoming arguments] [spill slots] [pushed outgoing arguments]
794 // Layout of the environment:
795 // 0 ..................................................... size-1
796 // [parameters] [locals] [expression stack including arguments]
798 // Layout of the translation:
799 // 0 ........................................................ size - 1 + 4
800 // [expression stack including arguments] [locals] [4 words] [parameters]
801 // |>------------ translation_size ------------<|
804 int jsframe_count = 0;
805 for (LEnvironment* e = environment; e != NULL; e = e->outer()) {
807 if (e->frame_type() == JS_FUNCTION) {
811 Translation translation(&translations_, frame_count, jsframe_count, zone());
812 WriteTranslation(environment, &translation);
813 int deoptimization_index = deoptimizations_.length();
814 int pc_offset = masm()->pc_offset();
815 environment->Register(deoptimization_index,
817 (mode == Safepoint::kLazyDeopt) ? pc_offset : -1);
818 deoptimizations_.Add(environment, zone());
823 void LCodeGen::DeoptimizeIf(Condition cc, LInstruction* instr,
825 Deoptimizer::BailoutType bailout_type) {
826 LEnvironment* environment = instr->environment();
827 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
828 DCHECK(environment->HasBeenRegistered());
829 int id = environment->deoptimization_index();
830 DCHECK(info()->IsOptimizing() || info()->IsStub());
832 Deoptimizer::GetDeoptimizationEntry(isolate(), id, bailout_type);
834 Abort(kBailoutWasNotPrepared);
838 if (DeoptEveryNTimes()) {
839 ExternalReference count = ExternalReference::stress_deopt_count(isolate());
843 __ mov(eax, Operand::StaticVariable(count));
844 __ sub(eax, Immediate(1));
845 __ j(not_zero, &no_deopt, Label::kNear);
846 if (FLAG_trap_on_deopt) __ int3();
847 __ mov(eax, Immediate(FLAG_deopt_every_n_times));
848 __ mov(Operand::StaticVariable(count), eax);
851 DCHECK(frame_is_built_);
852 __ call(entry, RelocInfo::RUNTIME_ENTRY);
854 __ mov(Operand::StaticVariable(count), eax);
859 if (info()->ShouldTrapOnDeopt()) {
861 if (cc != no_condition) __ j(NegateCondition(cc), &done, Label::kNear);
866 Deoptimizer::Reason reason(instr->hydrogen_value()->position().raw(),
867 instr->Mnemonic(), detail);
868 DCHECK(info()->IsStub() || frame_is_built_);
869 if (cc == no_condition && frame_is_built_) {
870 DeoptComment(reason);
871 __ call(entry, RelocInfo::RUNTIME_ENTRY);
873 Deoptimizer::JumpTableEntry table_entry(entry, reason, bailout_type,
875 // We often have several deopts to the same entry, reuse the last
876 // jump entry if this is the case.
877 if (jump_table_.is_empty() ||
878 !table_entry.IsEquivalentTo(jump_table_.last())) {
879 jump_table_.Add(table_entry, zone());
881 if (cc == no_condition) {
882 __ jmp(&jump_table_.last().label);
884 __ j(cc, &jump_table_.last().label);
890 void LCodeGen::DeoptimizeIf(Condition cc, LInstruction* instr,
891 const char* detail) {
892 Deoptimizer::BailoutType bailout_type = info()->IsStub()
894 : Deoptimizer::EAGER;
895 DeoptimizeIf(cc, instr, detail, bailout_type);
899 void LCodeGen::PopulateDeoptimizationData(Handle<Code> code) {
900 int length = deoptimizations_.length();
901 if (length == 0) return;
902 Handle<DeoptimizationInputData> data =
903 DeoptimizationInputData::New(isolate(), length, TENURED);
905 Handle<ByteArray> translations =
906 translations_.CreateByteArray(isolate()->factory());
907 data->SetTranslationByteArray(*translations);
908 data->SetInlinedFunctionCount(Smi::FromInt(inlined_function_count_));
909 data->SetOptimizationId(Smi::FromInt(info_->optimization_id()));
910 if (info_->IsOptimizing()) {
911 // Reference to shared function info does not change between phases.
912 AllowDeferredHandleDereference allow_handle_dereference;
913 data->SetSharedFunctionInfo(*info_->shared_info());
915 data->SetSharedFunctionInfo(Smi::FromInt(0));
918 Handle<FixedArray> literals =
919 factory()->NewFixedArray(deoptimization_literals_.length(), TENURED);
920 { AllowDeferredHandleDereference copy_handles;
921 for (int i = 0; i < deoptimization_literals_.length(); i++) {
922 literals->set(i, *deoptimization_literals_[i]);
924 data->SetLiteralArray(*literals);
927 data->SetOsrAstId(Smi::FromInt(info_->osr_ast_id().ToInt()));
928 data->SetOsrPcOffset(Smi::FromInt(osr_pc_offset_));
930 // Populate the deoptimization entries.
931 for (int i = 0; i < length; i++) {
932 LEnvironment* env = deoptimizations_[i];
933 data->SetAstId(i, env->ast_id());
934 data->SetTranslationIndex(i, Smi::FromInt(env->translation_index()));
935 data->SetArgumentsStackHeight(i,
936 Smi::FromInt(env->arguments_stack_height()));
937 data->SetPc(i, Smi::FromInt(env->pc_offset()));
939 code->set_deoptimization_data(*data);
943 int LCodeGen::DefineDeoptimizationLiteral(Handle<Object> literal) {
944 int result = deoptimization_literals_.length();
945 for (int i = 0; i < deoptimization_literals_.length(); ++i) {
946 if (deoptimization_literals_[i].is_identical_to(literal)) return i;
948 deoptimization_literals_.Add(literal, zone());
953 void LCodeGen::PopulateDeoptimizationLiteralsWithInlinedFunctions() {
954 DCHECK(deoptimization_literals_.length() == 0);
956 const ZoneList<Handle<JSFunction> >* inlined_closures =
957 chunk()->inlined_closures();
959 for (int i = 0, length = inlined_closures->length();
962 DefineDeoptimizationLiteral(inlined_closures->at(i));
965 inlined_function_count_ = deoptimization_literals_.length();
969 void LCodeGen::RecordSafepointWithLazyDeopt(
970 LInstruction* instr, SafepointMode safepoint_mode) {
971 if (safepoint_mode == RECORD_SIMPLE_SAFEPOINT) {
972 RecordSafepoint(instr->pointer_map(), Safepoint::kLazyDeopt);
974 DCHECK(safepoint_mode == RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
975 RecordSafepointWithRegisters(
976 instr->pointer_map(), 0, Safepoint::kLazyDeopt);
981 void LCodeGen::RecordSafepoint(
982 LPointerMap* pointers,
983 Safepoint::Kind kind,
985 Safepoint::DeoptMode deopt_mode) {
986 DCHECK(kind == expected_safepoint_kind_);
987 const ZoneList<LOperand*>* operands = pointers->GetNormalizedOperands();
988 Safepoint safepoint =
989 safepoints_.DefineSafepoint(masm(), kind, arguments, deopt_mode);
990 for (int i = 0; i < operands->length(); i++) {
991 LOperand* pointer = operands->at(i);
992 if (pointer->IsStackSlot()) {
993 safepoint.DefinePointerSlot(pointer->index(), zone());
994 } else if (pointer->IsRegister() && (kind & Safepoint::kWithRegisters)) {
995 safepoint.DefinePointerRegister(ToRegister(pointer), zone());
1001 void LCodeGen::RecordSafepoint(LPointerMap* pointers,
1002 Safepoint::DeoptMode mode) {
1003 RecordSafepoint(pointers, Safepoint::kSimple, 0, mode);
1007 void LCodeGen::RecordSafepoint(Safepoint::DeoptMode mode) {
1008 LPointerMap empty_pointers(zone());
1009 RecordSafepoint(&empty_pointers, mode);
1013 void LCodeGen::RecordSafepointWithRegisters(LPointerMap* pointers,
1015 Safepoint::DeoptMode mode) {
1016 RecordSafepoint(pointers, Safepoint::kWithRegisters, arguments, mode);
1020 void LCodeGen::RecordAndWritePosition(int position) {
1021 if (position == RelocInfo::kNoPosition) return;
1022 masm()->positions_recorder()->RecordPosition(position);
1023 masm()->positions_recorder()->WriteRecordedPositions();
1027 static const char* LabelType(LLabel* label) {
1028 if (label->is_loop_header()) return " (loop header)";
1029 if (label->is_osr_entry()) return " (OSR entry)";
1034 void LCodeGen::DoLabel(LLabel* label) {
1035 Comment(";;; <@%d,#%d> -------------------- B%d%s --------------------",
1036 current_instruction_,
1037 label->hydrogen_value()->id(),
1040 __ bind(label->label());
1041 current_block_ = label->block_id();
1046 void LCodeGen::DoParallelMove(LParallelMove* move) {
1047 resolver_.Resolve(move);
1051 void LCodeGen::DoGap(LGap* gap) {
1052 for (int i = LGap::FIRST_INNER_POSITION;
1053 i <= LGap::LAST_INNER_POSITION;
1055 LGap::InnerPosition inner_pos = static_cast<LGap::InnerPosition>(i);
1056 LParallelMove* move = gap->GetParallelMove(inner_pos);
1057 if (move != NULL) DoParallelMove(move);
1062 void LCodeGen::DoInstructionGap(LInstructionGap* instr) {
1067 void LCodeGen::DoParameter(LParameter* instr) {
1072 void LCodeGen::DoCallStub(LCallStub* instr) {
1073 DCHECK(ToRegister(instr->context()).is(esi));
1074 DCHECK(ToRegister(instr->result()).is(eax));
1075 switch (instr->hydrogen()->major_key()) {
1076 case CodeStub::RegExpExec: {
1077 RegExpExecStub stub(isolate());
1078 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1081 case CodeStub::SubString: {
1082 SubStringStub stub(isolate());
1083 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1086 case CodeStub::StringCompare: {
1087 StringCompareStub stub(isolate());
1088 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1097 void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) {
1098 GenerateOsrPrologue();
1102 void LCodeGen::DoModByPowerOf2I(LModByPowerOf2I* instr) {
1103 Register dividend = ToRegister(instr->dividend());
1104 int32_t divisor = instr->divisor();
1105 DCHECK(dividend.is(ToRegister(instr->result())));
1107 // Theoretically, a variation of the branch-free code for integer division by
1108 // a power of 2 (calculating the remainder via an additional multiplication
1109 // (which gets simplified to an 'and') and subtraction) should be faster, and
1110 // this is exactly what GCC and clang emit. Nevertheless, benchmarks seem to
1111 // indicate that positive dividends are heavily favored, so the branching
1112 // version performs better.
1113 HMod* hmod = instr->hydrogen();
1114 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
1115 Label dividend_is_not_negative, done;
1116 if (hmod->CheckFlag(HValue::kLeftCanBeNegative)) {
1117 __ test(dividend, dividend);
1118 __ j(not_sign, ÷nd_is_not_negative, Label::kNear);
1119 // Note that this is correct even for kMinInt operands.
1121 __ and_(dividend, mask);
1123 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1124 DeoptimizeIf(zero, instr, "minus zero");
1126 __ jmp(&done, Label::kNear);
1129 __ bind(÷nd_is_not_negative);
1130 __ and_(dividend, mask);
1135 void LCodeGen::DoModByConstI(LModByConstI* instr) {
1136 Register dividend = ToRegister(instr->dividend());
1137 int32_t divisor = instr->divisor();
1138 DCHECK(ToRegister(instr->result()).is(eax));
1141 DeoptimizeIf(no_condition, instr, "division by zero");
1145 __ TruncatingDiv(dividend, Abs(divisor));
1146 __ imul(edx, edx, Abs(divisor));
1147 __ mov(eax, dividend);
1150 // Check for negative zero.
1151 HMod* hmod = instr->hydrogen();
1152 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1153 Label remainder_not_zero;
1154 __ j(not_zero, &remainder_not_zero, Label::kNear);
1155 __ cmp(dividend, Immediate(0));
1156 DeoptimizeIf(less, instr, "minus zero");
1157 __ bind(&remainder_not_zero);
1162 void LCodeGen::DoModI(LModI* instr) {
1163 HMod* hmod = instr->hydrogen();
1165 Register left_reg = ToRegister(instr->left());
1166 DCHECK(left_reg.is(eax));
1167 Register right_reg = ToRegister(instr->right());
1168 DCHECK(!right_reg.is(eax));
1169 DCHECK(!right_reg.is(edx));
1170 Register result_reg = ToRegister(instr->result());
1171 DCHECK(result_reg.is(edx));
1174 // Check for x % 0, idiv would signal a divide error. We have to
1175 // deopt in this case because we can't return a NaN.
1176 if (hmod->CheckFlag(HValue::kCanBeDivByZero)) {
1177 __ test(right_reg, Operand(right_reg));
1178 DeoptimizeIf(zero, instr, "division by zero");
1181 // Check for kMinInt % -1, idiv would signal a divide error. We
1182 // have to deopt if we care about -0, because we can't return that.
1183 if (hmod->CheckFlag(HValue::kCanOverflow)) {
1184 Label no_overflow_possible;
1185 __ cmp(left_reg, kMinInt);
1186 __ j(not_equal, &no_overflow_possible, Label::kNear);
1187 __ cmp(right_reg, -1);
1188 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1189 DeoptimizeIf(equal, instr, "minus zero");
1191 __ j(not_equal, &no_overflow_possible, Label::kNear);
1192 __ Move(result_reg, Immediate(0));
1193 __ jmp(&done, Label::kNear);
1195 __ bind(&no_overflow_possible);
1198 // Sign extend dividend in eax into edx:eax.
1201 // If we care about -0, test if the dividend is <0 and the result is 0.
1202 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1203 Label positive_left;
1204 __ test(left_reg, Operand(left_reg));
1205 __ j(not_sign, &positive_left, Label::kNear);
1207 __ test(result_reg, Operand(result_reg));
1208 DeoptimizeIf(zero, instr, "minus zero");
1209 __ jmp(&done, Label::kNear);
1210 __ bind(&positive_left);
1217 void LCodeGen::DoDivByPowerOf2I(LDivByPowerOf2I* instr) {
1218 Register dividend = ToRegister(instr->dividend());
1219 int32_t divisor = instr->divisor();
1220 Register result = ToRegister(instr->result());
1221 DCHECK(divisor == kMinInt || base::bits::IsPowerOfTwo32(Abs(divisor)));
1222 DCHECK(!result.is(dividend));
1224 // Check for (0 / -x) that will produce negative zero.
1225 HDiv* hdiv = instr->hydrogen();
1226 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1227 __ test(dividend, dividend);
1228 DeoptimizeIf(zero, instr, "minus zero");
1230 // Check for (kMinInt / -1).
1231 if (hdiv->CheckFlag(HValue::kCanOverflow) && divisor == -1) {
1232 __ cmp(dividend, kMinInt);
1233 DeoptimizeIf(zero, instr, "overflow");
1235 // Deoptimize if remainder will not be 0.
1236 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32) &&
1237 divisor != 1 && divisor != -1) {
1238 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
1239 __ test(dividend, Immediate(mask));
1240 DeoptimizeIf(not_zero, instr, "lost precision");
1242 __ Move(result, dividend);
1243 int32_t shift = WhichPowerOf2Abs(divisor);
1245 // The arithmetic shift is always OK, the 'if' is an optimization only.
1246 if (shift > 1) __ sar(result, 31);
1247 __ shr(result, 32 - shift);
1248 __ add(result, dividend);
1249 __ sar(result, shift);
1251 if (divisor < 0) __ neg(result);
1255 void LCodeGen::DoDivByConstI(LDivByConstI* instr) {
1256 Register dividend = ToRegister(instr->dividend());
1257 int32_t divisor = instr->divisor();
1258 DCHECK(ToRegister(instr->result()).is(edx));
1261 DeoptimizeIf(no_condition, instr, "division by zero");
1265 // Check for (0 / -x) that will produce negative zero.
1266 HDiv* hdiv = instr->hydrogen();
1267 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1268 __ test(dividend, dividend);
1269 DeoptimizeIf(zero, instr, "minus zero");
1272 __ TruncatingDiv(dividend, Abs(divisor));
1273 if (divisor < 0) __ neg(edx);
1275 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32)) {
1277 __ imul(eax, eax, divisor);
1278 __ sub(eax, dividend);
1279 DeoptimizeIf(not_equal, instr, "lost precision");
1284 // TODO(svenpanne) Refactor this to avoid code duplication with DoFlooringDivI.
1285 void LCodeGen::DoDivI(LDivI* instr) {
1286 HBinaryOperation* hdiv = instr->hydrogen();
1287 Register dividend = ToRegister(instr->dividend());
1288 Register divisor = ToRegister(instr->divisor());
1289 Register remainder = ToRegister(instr->temp());
1290 DCHECK(dividend.is(eax));
1291 DCHECK(remainder.is(edx));
1292 DCHECK(ToRegister(instr->result()).is(eax));
1293 DCHECK(!divisor.is(eax));
1294 DCHECK(!divisor.is(edx));
1297 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
1298 __ test(divisor, divisor);
1299 DeoptimizeIf(zero, instr, "division by zero");
1302 // Check for (0 / -x) that will produce negative zero.
1303 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
1304 Label dividend_not_zero;
1305 __ test(dividend, dividend);
1306 __ j(not_zero, ÷nd_not_zero, Label::kNear);
1307 __ test(divisor, divisor);
1308 DeoptimizeIf(sign, instr, "minus zero");
1309 __ bind(÷nd_not_zero);
1312 // Check for (kMinInt / -1).
1313 if (hdiv->CheckFlag(HValue::kCanOverflow)) {
1314 Label dividend_not_min_int;
1315 __ cmp(dividend, kMinInt);
1316 __ j(not_zero, ÷nd_not_min_int, Label::kNear);
1317 __ cmp(divisor, -1);
1318 DeoptimizeIf(zero, instr, "overflow");
1319 __ bind(÷nd_not_min_int);
1322 // Sign extend to edx (= remainder).
1326 if (!hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
1327 // Deoptimize if remainder is not 0.
1328 __ test(remainder, remainder);
1329 DeoptimizeIf(not_zero, instr, "lost precision");
1334 void LCodeGen::DoFlooringDivByPowerOf2I(LFlooringDivByPowerOf2I* instr) {
1335 Register dividend = ToRegister(instr->dividend());
1336 int32_t divisor = instr->divisor();
1337 DCHECK(dividend.is(ToRegister(instr->result())));
1339 // If the divisor is positive, things are easy: There can be no deopts and we
1340 // can simply do an arithmetic right shift.
1341 if (divisor == 1) return;
1342 int32_t shift = WhichPowerOf2Abs(divisor);
1344 __ sar(dividend, shift);
1348 // If the divisor is negative, we have to negate and handle edge cases.
1350 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1351 DeoptimizeIf(zero, instr, "minus zero");
1354 // Dividing by -1 is basically negation, unless we overflow.
1355 if (divisor == -1) {
1356 if (instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
1357 DeoptimizeIf(overflow, instr, "overflow");
1362 // If the negation could not overflow, simply shifting is OK.
1363 if (!instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
1364 __ sar(dividend, shift);
1368 Label not_kmin_int, done;
1369 __ j(no_overflow, ¬_kmin_int, Label::kNear);
1370 __ mov(dividend, Immediate(kMinInt / divisor));
1371 __ jmp(&done, Label::kNear);
1372 __ bind(¬_kmin_int);
1373 __ sar(dividend, shift);
1378 void LCodeGen::DoFlooringDivByConstI(LFlooringDivByConstI* instr) {
1379 Register dividend = ToRegister(instr->dividend());
1380 int32_t divisor = instr->divisor();
1381 DCHECK(ToRegister(instr->result()).is(edx));
1384 DeoptimizeIf(no_condition, instr, "division by zero");
1388 // Check for (0 / -x) that will produce negative zero.
1389 HMathFloorOfDiv* hdiv = instr->hydrogen();
1390 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1391 __ test(dividend, dividend);
1392 DeoptimizeIf(zero, instr, "minus zero");
1395 // Easy case: We need no dynamic check for the dividend and the flooring
1396 // division is the same as the truncating division.
1397 if ((divisor > 0 && !hdiv->CheckFlag(HValue::kLeftCanBeNegative)) ||
1398 (divisor < 0 && !hdiv->CheckFlag(HValue::kLeftCanBePositive))) {
1399 __ TruncatingDiv(dividend, Abs(divisor));
1400 if (divisor < 0) __ neg(edx);
1404 // In the general case we may need to adjust before and after the truncating
1405 // division to get a flooring division.
1406 Register temp = ToRegister(instr->temp3());
1407 DCHECK(!temp.is(dividend) && !temp.is(eax) && !temp.is(edx));
1408 Label needs_adjustment, done;
1409 __ cmp(dividend, Immediate(0));
1410 __ j(divisor > 0 ? less : greater, &needs_adjustment, Label::kNear);
1411 __ TruncatingDiv(dividend, Abs(divisor));
1412 if (divisor < 0) __ neg(edx);
1413 __ jmp(&done, Label::kNear);
1414 __ bind(&needs_adjustment);
1415 __ lea(temp, Operand(dividend, divisor > 0 ? 1 : -1));
1416 __ TruncatingDiv(temp, Abs(divisor));
1417 if (divisor < 0) __ neg(edx);
1423 // TODO(svenpanne) Refactor this to avoid code duplication with DoDivI.
1424 void LCodeGen::DoFlooringDivI(LFlooringDivI* instr) {
1425 HBinaryOperation* hdiv = instr->hydrogen();
1426 Register dividend = ToRegister(instr->dividend());
1427 Register divisor = ToRegister(instr->divisor());
1428 Register remainder = ToRegister(instr->temp());
1429 Register result = ToRegister(instr->result());
1430 DCHECK(dividend.is(eax));
1431 DCHECK(remainder.is(edx));
1432 DCHECK(result.is(eax));
1433 DCHECK(!divisor.is(eax));
1434 DCHECK(!divisor.is(edx));
1437 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
1438 __ test(divisor, divisor);
1439 DeoptimizeIf(zero, instr, "division by zero");
1442 // Check for (0 / -x) that will produce negative zero.
1443 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
1444 Label dividend_not_zero;
1445 __ test(dividend, dividend);
1446 __ j(not_zero, ÷nd_not_zero, Label::kNear);
1447 __ test(divisor, divisor);
1448 DeoptimizeIf(sign, instr, "minus zero");
1449 __ bind(÷nd_not_zero);
1452 // Check for (kMinInt / -1).
1453 if (hdiv->CheckFlag(HValue::kCanOverflow)) {
1454 Label dividend_not_min_int;
1455 __ cmp(dividend, kMinInt);
1456 __ j(not_zero, ÷nd_not_min_int, Label::kNear);
1457 __ cmp(divisor, -1);
1458 DeoptimizeIf(zero, instr, "overflow");
1459 __ bind(÷nd_not_min_int);
1462 // Sign extend to edx (= remainder).
1467 __ test(remainder, remainder);
1468 __ j(zero, &done, Label::kNear);
1469 __ xor_(remainder, divisor);
1470 __ sar(remainder, 31);
1471 __ add(result, remainder);
1476 void LCodeGen::DoMulI(LMulI* instr) {
1477 Register left = ToRegister(instr->left());
1478 LOperand* right = instr->right();
1480 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1481 __ mov(ToRegister(instr->temp()), left);
1484 if (right->IsConstantOperand()) {
1485 // Try strength reductions on the multiplication.
1486 // All replacement instructions are at most as long as the imul
1487 // and have better latency.
1488 int constant = ToInteger32(LConstantOperand::cast(right));
1489 if (constant == -1) {
1491 } else if (constant == 0) {
1492 __ xor_(left, Operand(left));
1493 } else if (constant == 2) {
1494 __ add(left, Operand(left));
1495 } else if (!instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
1496 // If we know that the multiplication can't overflow, it's safe to
1497 // use instructions that don't set the overflow flag for the
1504 __ lea(left, Operand(left, left, times_2, 0));
1510 __ lea(left, Operand(left, left, times_4, 0));
1516 __ lea(left, Operand(left, left, times_8, 0));
1522 __ imul(left, left, constant);
1526 __ imul(left, left, constant);
1529 if (instr->hydrogen()->representation().IsSmi()) {
1532 __ imul(left, ToOperand(right));
1535 if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
1536 DeoptimizeIf(overflow, instr, "overflow");
1539 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1540 // Bail out if the result is supposed to be negative zero.
1542 __ test(left, Operand(left));
1543 __ j(not_zero, &done, Label::kNear);
1544 if (right->IsConstantOperand()) {
1545 if (ToInteger32(LConstantOperand::cast(right)) < 0) {
1546 DeoptimizeIf(no_condition, instr, "minus zero");
1547 } else if (ToInteger32(LConstantOperand::cast(right)) == 0) {
1548 __ cmp(ToRegister(instr->temp()), Immediate(0));
1549 DeoptimizeIf(less, instr, "minus zero");
1552 // Test the non-zero operand for negative sign.
1553 __ or_(ToRegister(instr->temp()), ToOperand(right));
1554 DeoptimizeIf(sign, instr, "minus zero");
1561 void LCodeGen::DoBitI(LBitI* instr) {
1562 LOperand* left = instr->left();
1563 LOperand* right = instr->right();
1564 DCHECK(left->Equals(instr->result()));
1565 DCHECK(left->IsRegister());
1567 if (right->IsConstantOperand()) {
1568 int32_t right_operand =
1569 ToRepresentation(LConstantOperand::cast(right),
1570 instr->hydrogen()->representation());
1571 switch (instr->op()) {
1572 case Token::BIT_AND:
1573 __ and_(ToRegister(left), right_operand);
1576 __ or_(ToRegister(left), right_operand);
1578 case Token::BIT_XOR:
1579 if (right_operand == int32_t(~0)) {
1580 __ not_(ToRegister(left));
1582 __ xor_(ToRegister(left), right_operand);
1590 switch (instr->op()) {
1591 case Token::BIT_AND:
1592 __ and_(ToRegister(left), ToOperand(right));
1595 __ or_(ToRegister(left), ToOperand(right));
1597 case Token::BIT_XOR:
1598 __ xor_(ToRegister(left), ToOperand(right));
1608 void LCodeGen::DoShiftI(LShiftI* instr) {
1609 LOperand* left = instr->left();
1610 LOperand* right = instr->right();
1611 DCHECK(left->Equals(instr->result()));
1612 DCHECK(left->IsRegister());
1613 if (right->IsRegister()) {
1614 DCHECK(ToRegister(right).is(ecx));
1616 switch (instr->op()) {
1618 __ ror_cl(ToRegister(left));
1621 __ sar_cl(ToRegister(left));
1624 __ shr_cl(ToRegister(left));
1625 if (instr->can_deopt()) {
1626 __ test(ToRegister(left), ToRegister(left));
1627 DeoptimizeIf(sign, instr, "negative value");
1631 __ shl_cl(ToRegister(left));
1638 int value = ToInteger32(LConstantOperand::cast(right));
1639 uint8_t shift_count = static_cast<uint8_t>(value & 0x1F);
1640 switch (instr->op()) {
1642 if (shift_count == 0 && instr->can_deopt()) {
1643 __ test(ToRegister(left), ToRegister(left));
1644 DeoptimizeIf(sign, instr, "negative value");
1646 __ ror(ToRegister(left), shift_count);
1650 if (shift_count != 0) {
1651 __ sar(ToRegister(left), shift_count);
1655 if (shift_count != 0) {
1656 __ shr(ToRegister(left), shift_count);
1657 } else if (instr->can_deopt()) {
1658 __ test(ToRegister(left), ToRegister(left));
1659 DeoptimizeIf(sign, instr, "negative value");
1663 if (shift_count != 0) {
1664 if (instr->hydrogen_value()->representation().IsSmi() &&
1665 instr->can_deopt()) {
1666 if (shift_count != 1) {
1667 __ shl(ToRegister(left), shift_count - 1);
1669 __ SmiTag(ToRegister(left));
1670 DeoptimizeIf(overflow, instr, "overflow");
1672 __ shl(ToRegister(left), shift_count);
1684 void LCodeGen::DoSubI(LSubI* instr) {
1685 LOperand* left = instr->left();
1686 LOperand* right = instr->right();
1687 DCHECK(left->Equals(instr->result()));
1689 if (right->IsConstantOperand()) {
1690 __ sub(ToOperand(left),
1691 ToImmediate(right, instr->hydrogen()->representation()));
1693 __ sub(ToRegister(left), ToOperand(right));
1695 if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
1696 DeoptimizeIf(overflow, instr, "overflow");
1701 void LCodeGen::DoConstantI(LConstantI* instr) {
1702 __ Move(ToRegister(instr->result()), Immediate(instr->value()));
1706 void LCodeGen::DoConstantS(LConstantS* instr) {
1707 __ Move(ToRegister(instr->result()), Immediate(instr->value()));
1711 void LCodeGen::DoConstantD(LConstantD* instr) {
1712 double v = instr->value();
1713 uint64_t int_val = bit_cast<uint64_t, double>(v);
1714 int32_t lower = static_cast<int32_t>(int_val);
1715 int32_t upper = static_cast<int32_t>(int_val >> (kBitsPerInt));
1716 DCHECK(instr->result()->IsDoubleRegister());
1718 XMMRegister res = ToDoubleRegister(instr->result());
1722 Register temp = ToRegister(instr->temp());
1723 if (CpuFeatures::IsSupported(SSE4_1)) {
1724 CpuFeatureScope scope2(masm(), SSE4_1);
1726 __ Move(temp, Immediate(lower));
1727 __ movd(res, Operand(temp));
1728 __ Move(temp, Immediate(upper));
1729 __ pinsrd(res, Operand(temp), 1);
1732 __ Move(temp, Immediate(upper));
1733 __ pinsrd(res, Operand(temp), 1);
1736 __ Move(temp, Immediate(upper));
1737 __ movd(res, Operand(temp));
1740 XMMRegister xmm_scratch = double_scratch0();
1741 __ Move(temp, Immediate(lower));
1742 __ movd(xmm_scratch, Operand(temp));
1743 __ orps(res, xmm_scratch);
1750 void LCodeGen::DoConstantE(LConstantE* instr) {
1751 __ lea(ToRegister(instr->result()), Operand::StaticVariable(instr->value()));
1755 void LCodeGen::DoConstantT(LConstantT* instr) {
1756 Register reg = ToRegister(instr->result());
1757 Handle<Object> object = instr->value(isolate());
1758 AllowDeferredHandleDereference smi_check;
1759 __ LoadObject(reg, object);
1763 void LCodeGen::DoMapEnumLength(LMapEnumLength* instr) {
1764 Register result = ToRegister(instr->result());
1765 Register map = ToRegister(instr->value());
1766 __ EnumLength(result, map);
1770 void LCodeGen::DoDateField(LDateField* instr) {
1771 Register object = ToRegister(instr->date());
1772 Register result = ToRegister(instr->result());
1773 Register scratch = ToRegister(instr->temp());
1774 Smi* index = instr->index();
1775 Label runtime, done;
1776 DCHECK(object.is(result));
1777 DCHECK(object.is(eax));
1779 __ test(object, Immediate(kSmiTagMask));
1780 DeoptimizeIf(zero, instr, "Smi");
1781 __ CmpObjectType(object, JS_DATE_TYPE, scratch);
1782 DeoptimizeIf(not_equal, instr, "not a date object");
1784 if (index->value() == 0) {
1785 __ mov(result, FieldOperand(object, JSDate::kValueOffset));
1787 if (index->value() < JSDate::kFirstUncachedField) {
1788 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
1789 __ mov(scratch, Operand::StaticVariable(stamp));
1790 __ cmp(scratch, FieldOperand(object, JSDate::kCacheStampOffset));
1791 __ j(not_equal, &runtime, Label::kNear);
1792 __ mov(result, FieldOperand(object, JSDate::kValueOffset +
1793 kPointerSize * index->value()));
1794 __ jmp(&done, Label::kNear);
1797 __ PrepareCallCFunction(2, scratch);
1798 __ mov(Operand(esp, 0), object);
1799 __ mov(Operand(esp, 1 * kPointerSize), Immediate(index));
1800 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
1806 Operand LCodeGen::BuildSeqStringOperand(Register string,
1808 String::Encoding encoding) {
1809 if (index->IsConstantOperand()) {
1810 int offset = ToRepresentation(LConstantOperand::cast(index),
1811 Representation::Integer32());
1812 if (encoding == String::TWO_BYTE_ENCODING) {
1813 offset *= kUC16Size;
1815 STATIC_ASSERT(kCharSize == 1);
1816 return FieldOperand(string, SeqString::kHeaderSize + offset);
1818 return FieldOperand(
1819 string, ToRegister(index),
1820 encoding == String::ONE_BYTE_ENCODING ? times_1 : times_2,
1821 SeqString::kHeaderSize);
1825 void LCodeGen::DoSeqStringGetChar(LSeqStringGetChar* instr) {
1826 String::Encoding encoding = instr->hydrogen()->encoding();
1827 Register result = ToRegister(instr->result());
1828 Register string = ToRegister(instr->string());
1830 if (FLAG_debug_code) {
1832 __ mov(string, FieldOperand(string, HeapObject::kMapOffset));
1833 __ movzx_b(string, FieldOperand(string, Map::kInstanceTypeOffset));
1835 __ and_(string, Immediate(kStringRepresentationMask | kStringEncodingMask));
1836 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
1837 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
1838 __ cmp(string, Immediate(encoding == String::ONE_BYTE_ENCODING
1839 ? one_byte_seq_type : two_byte_seq_type));
1840 __ Check(equal, kUnexpectedStringType);
1844 Operand operand = BuildSeqStringOperand(string, instr->index(), encoding);
1845 if (encoding == String::ONE_BYTE_ENCODING) {
1846 __ movzx_b(result, operand);
1848 __ movzx_w(result, operand);
1853 void LCodeGen::DoSeqStringSetChar(LSeqStringSetChar* instr) {
1854 String::Encoding encoding = instr->hydrogen()->encoding();
1855 Register string = ToRegister(instr->string());
1857 if (FLAG_debug_code) {
1858 Register value = ToRegister(instr->value());
1859 Register index = ToRegister(instr->index());
1860 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
1861 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
1863 instr->hydrogen()->encoding() == String::ONE_BYTE_ENCODING
1864 ? one_byte_seq_type : two_byte_seq_type;
1865 __ EmitSeqStringSetCharCheck(string, index, value, encoding_mask);
1868 Operand operand = BuildSeqStringOperand(string, instr->index(), encoding);
1869 if (instr->value()->IsConstantOperand()) {
1870 int value = ToRepresentation(LConstantOperand::cast(instr->value()),
1871 Representation::Integer32());
1872 DCHECK_LE(0, value);
1873 if (encoding == String::ONE_BYTE_ENCODING) {
1874 DCHECK_LE(value, String::kMaxOneByteCharCode);
1875 __ mov_b(operand, static_cast<int8_t>(value));
1877 DCHECK_LE(value, String::kMaxUtf16CodeUnit);
1878 __ mov_w(operand, static_cast<int16_t>(value));
1881 Register value = ToRegister(instr->value());
1882 if (encoding == String::ONE_BYTE_ENCODING) {
1883 __ mov_b(operand, value);
1885 __ mov_w(operand, value);
1891 void LCodeGen::DoAddI(LAddI* instr) {
1892 LOperand* left = instr->left();
1893 LOperand* right = instr->right();
1895 if (LAddI::UseLea(instr->hydrogen()) && !left->Equals(instr->result())) {
1896 if (right->IsConstantOperand()) {
1897 int32_t offset = ToRepresentation(LConstantOperand::cast(right),
1898 instr->hydrogen()->representation());
1899 __ lea(ToRegister(instr->result()), MemOperand(ToRegister(left), offset));
1901 Operand address(ToRegister(left), ToRegister(right), times_1, 0);
1902 __ lea(ToRegister(instr->result()), address);
1905 if (right->IsConstantOperand()) {
1906 __ add(ToOperand(left),
1907 ToImmediate(right, instr->hydrogen()->representation()));
1909 __ add(ToRegister(left), ToOperand(right));
1911 if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
1912 DeoptimizeIf(overflow, instr, "overflow");
1918 void LCodeGen::DoMathMinMax(LMathMinMax* instr) {
1919 LOperand* left = instr->left();
1920 LOperand* right = instr->right();
1921 DCHECK(left->Equals(instr->result()));
1922 HMathMinMax::Operation operation = instr->hydrogen()->operation();
1923 if (instr->hydrogen()->representation().IsSmiOrInteger32()) {
1925 Condition condition = (operation == HMathMinMax::kMathMin)
1928 if (right->IsConstantOperand()) {
1929 Operand left_op = ToOperand(left);
1930 Immediate immediate = ToImmediate(LConstantOperand::cast(instr->right()),
1931 instr->hydrogen()->representation());
1932 __ cmp(left_op, immediate);
1933 __ j(condition, &return_left, Label::kNear);
1934 __ mov(left_op, immediate);
1936 Register left_reg = ToRegister(left);
1937 Operand right_op = ToOperand(right);
1938 __ cmp(left_reg, right_op);
1939 __ j(condition, &return_left, Label::kNear);
1940 __ mov(left_reg, right_op);
1942 __ bind(&return_left);
1944 DCHECK(instr->hydrogen()->representation().IsDouble());
1945 Label check_nan_left, check_zero, return_left, return_right;
1946 Condition condition = (operation == HMathMinMax::kMathMin) ? below : above;
1947 XMMRegister left_reg = ToDoubleRegister(left);
1948 XMMRegister right_reg = ToDoubleRegister(right);
1949 __ ucomisd(left_reg, right_reg);
1950 __ j(parity_even, &check_nan_left, Label::kNear); // At least one NaN.
1951 __ j(equal, &check_zero, Label::kNear); // left == right.
1952 __ j(condition, &return_left, Label::kNear);
1953 __ jmp(&return_right, Label::kNear);
1955 __ bind(&check_zero);
1956 XMMRegister xmm_scratch = double_scratch0();
1957 __ xorps(xmm_scratch, xmm_scratch);
1958 __ ucomisd(left_reg, xmm_scratch);
1959 __ j(not_equal, &return_left, Label::kNear); // left == right != 0.
1960 // At this point, both left and right are either 0 or -0.
1961 if (operation == HMathMinMax::kMathMin) {
1962 __ orpd(left_reg, right_reg);
1964 // Since we operate on +0 and/or -0, addsd and andsd have the same effect.
1965 __ addsd(left_reg, right_reg);
1967 __ jmp(&return_left, Label::kNear);
1969 __ bind(&check_nan_left);
1970 __ ucomisd(left_reg, left_reg); // NaN check.
1971 __ j(parity_even, &return_left, Label::kNear); // left == NaN.
1972 __ bind(&return_right);
1973 __ movaps(left_reg, right_reg);
1975 __ bind(&return_left);
1980 void LCodeGen::DoArithmeticD(LArithmeticD* instr) {
1981 XMMRegister left = ToDoubleRegister(instr->left());
1982 XMMRegister right = ToDoubleRegister(instr->right());
1983 XMMRegister result = ToDoubleRegister(instr->result());
1984 switch (instr->op()) {
1986 __ addsd(left, right);
1989 __ subsd(left, right);
1992 __ mulsd(left, right);
1995 __ divsd(left, right);
1996 // Don't delete this mov. It may improve performance on some CPUs,
1997 // when there is a mulsd depending on the result
1998 __ movaps(left, left);
2001 // Pass two doubles as arguments on the stack.
2002 __ PrepareCallCFunction(4, eax);
2003 __ movsd(Operand(esp, 0 * kDoubleSize), left);
2004 __ movsd(Operand(esp, 1 * kDoubleSize), right);
2006 ExternalReference::mod_two_doubles_operation(isolate()),
2009 // Return value is in st(0) on ia32.
2010 // Store it into the result register.
2011 __ sub(Operand(esp), Immediate(kDoubleSize));
2012 __ fstp_d(Operand(esp, 0));
2013 __ movsd(result, Operand(esp, 0));
2014 __ add(Operand(esp), Immediate(kDoubleSize));
2024 void LCodeGen::DoArithmeticT(LArithmeticT* instr) {
2025 DCHECK(ToRegister(instr->context()).is(esi));
2026 DCHECK(ToRegister(instr->left()).is(edx));
2027 DCHECK(ToRegister(instr->right()).is(eax));
2028 DCHECK(ToRegister(instr->result()).is(eax));
2031 CodeFactory::BinaryOpIC(isolate(), instr->op(), NO_OVERWRITE).code();
2032 CallCode(code, RelocInfo::CODE_TARGET, instr);
2036 template<class InstrType>
2037 void LCodeGen::EmitBranch(InstrType instr, Condition cc) {
2038 int left_block = instr->TrueDestination(chunk_);
2039 int right_block = instr->FalseDestination(chunk_);
2041 int next_block = GetNextEmittedBlock();
2043 if (right_block == left_block || cc == no_condition) {
2044 EmitGoto(left_block);
2045 } else if (left_block == next_block) {
2046 __ j(NegateCondition(cc), chunk_->GetAssemblyLabel(right_block));
2047 } else if (right_block == next_block) {
2048 __ j(cc, chunk_->GetAssemblyLabel(left_block));
2050 __ j(cc, chunk_->GetAssemblyLabel(left_block));
2051 __ jmp(chunk_->GetAssemblyLabel(right_block));
2056 template<class InstrType>
2057 void LCodeGen::EmitFalseBranch(InstrType instr, Condition cc) {
2058 int false_block = instr->FalseDestination(chunk_);
2059 if (cc == no_condition) {
2060 __ jmp(chunk_->GetAssemblyLabel(false_block));
2062 __ j(cc, chunk_->GetAssemblyLabel(false_block));
2067 void LCodeGen::DoBranch(LBranch* instr) {
2068 Representation r = instr->hydrogen()->value()->representation();
2069 if (r.IsSmiOrInteger32()) {
2070 Register reg = ToRegister(instr->value());
2071 __ test(reg, Operand(reg));
2072 EmitBranch(instr, not_zero);
2073 } else if (r.IsDouble()) {
2074 DCHECK(!info()->IsStub());
2075 XMMRegister reg = ToDoubleRegister(instr->value());
2076 XMMRegister xmm_scratch = double_scratch0();
2077 __ xorps(xmm_scratch, xmm_scratch);
2078 __ ucomisd(reg, xmm_scratch);
2079 EmitBranch(instr, not_equal);
2081 DCHECK(r.IsTagged());
2082 Register reg = ToRegister(instr->value());
2083 HType type = instr->hydrogen()->value()->type();
2084 if (type.IsBoolean()) {
2085 DCHECK(!info()->IsStub());
2086 __ cmp(reg, factory()->true_value());
2087 EmitBranch(instr, equal);
2088 } else if (type.IsSmi()) {
2089 DCHECK(!info()->IsStub());
2090 __ test(reg, Operand(reg));
2091 EmitBranch(instr, not_equal);
2092 } else if (type.IsJSArray()) {
2093 DCHECK(!info()->IsStub());
2094 EmitBranch(instr, no_condition);
2095 } else if (type.IsHeapNumber()) {
2096 DCHECK(!info()->IsStub());
2097 XMMRegister xmm_scratch = double_scratch0();
2098 __ xorps(xmm_scratch, xmm_scratch);
2099 __ ucomisd(xmm_scratch, FieldOperand(reg, HeapNumber::kValueOffset));
2100 EmitBranch(instr, not_equal);
2101 } else if (type.IsString()) {
2102 DCHECK(!info()->IsStub());
2103 __ cmp(FieldOperand(reg, String::kLengthOffset), Immediate(0));
2104 EmitBranch(instr, not_equal);
2106 ToBooleanStub::Types expected = instr->hydrogen()->expected_input_types();
2107 if (expected.IsEmpty()) expected = ToBooleanStub::Types::Generic();
2109 if (expected.Contains(ToBooleanStub::UNDEFINED)) {
2110 // undefined -> false.
2111 __ cmp(reg, factory()->undefined_value());
2112 __ j(equal, instr->FalseLabel(chunk_));
2114 if (expected.Contains(ToBooleanStub::BOOLEAN)) {
2116 __ cmp(reg, factory()->true_value());
2117 __ j(equal, instr->TrueLabel(chunk_));
2119 __ cmp(reg, factory()->false_value());
2120 __ j(equal, instr->FalseLabel(chunk_));
2122 if (expected.Contains(ToBooleanStub::NULL_TYPE)) {
2124 __ cmp(reg, factory()->null_value());
2125 __ j(equal, instr->FalseLabel(chunk_));
2128 if (expected.Contains(ToBooleanStub::SMI)) {
2129 // Smis: 0 -> false, all other -> true.
2130 __ test(reg, Operand(reg));
2131 __ j(equal, instr->FalseLabel(chunk_));
2132 __ JumpIfSmi(reg, instr->TrueLabel(chunk_));
2133 } else if (expected.NeedsMap()) {
2134 // If we need a map later and have a Smi -> deopt.
2135 __ test(reg, Immediate(kSmiTagMask));
2136 DeoptimizeIf(zero, instr, "Smi");
2139 Register map = no_reg; // Keep the compiler happy.
2140 if (expected.NeedsMap()) {
2141 map = ToRegister(instr->temp());
2142 DCHECK(!map.is(reg));
2143 __ mov(map, FieldOperand(reg, HeapObject::kMapOffset));
2145 if (expected.CanBeUndetectable()) {
2146 // Undetectable -> false.
2147 __ test_b(FieldOperand(map, Map::kBitFieldOffset),
2148 1 << Map::kIsUndetectable);
2149 __ j(not_zero, instr->FalseLabel(chunk_));
2153 if (expected.Contains(ToBooleanStub::SPEC_OBJECT)) {
2154 // spec object -> true.
2155 __ CmpInstanceType(map, FIRST_SPEC_OBJECT_TYPE);
2156 __ j(above_equal, instr->TrueLabel(chunk_));
2159 if (expected.Contains(ToBooleanStub::STRING)) {
2160 // String value -> false iff empty.
2162 __ CmpInstanceType(map, FIRST_NONSTRING_TYPE);
2163 __ j(above_equal, ¬_string, Label::kNear);
2164 __ cmp(FieldOperand(reg, String::kLengthOffset), Immediate(0));
2165 __ j(not_zero, instr->TrueLabel(chunk_));
2166 __ jmp(instr->FalseLabel(chunk_));
2167 __ bind(¬_string);
2170 if (expected.Contains(ToBooleanStub::SYMBOL)) {
2171 // Symbol value -> true.
2172 __ CmpInstanceType(map, SYMBOL_TYPE);
2173 __ j(equal, instr->TrueLabel(chunk_));
2176 if (expected.Contains(ToBooleanStub::HEAP_NUMBER)) {
2177 // heap number -> false iff +0, -0, or NaN.
2178 Label not_heap_number;
2179 __ cmp(FieldOperand(reg, HeapObject::kMapOffset),
2180 factory()->heap_number_map());
2181 __ j(not_equal, ¬_heap_number, Label::kNear);
2182 XMMRegister xmm_scratch = double_scratch0();
2183 __ xorps(xmm_scratch, xmm_scratch);
2184 __ ucomisd(xmm_scratch, FieldOperand(reg, HeapNumber::kValueOffset));
2185 __ j(zero, instr->FalseLabel(chunk_));
2186 __ jmp(instr->TrueLabel(chunk_));
2187 __ bind(¬_heap_number);
2190 if (!expected.IsGeneric()) {
2191 // We've seen something for the first time -> deopt.
2192 // This can only happen if we are not generic already.
2193 DeoptimizeIf(no_condition, instr, "unexpected object");
2200 void LCodeGen::EmitGoto(int block) {
2201 if (!IsNextEmittedBlock(block)) {
2202 __ jmp(chunk_->GetAssemblyLabel(LookupDestination(block)));
2207 void LCodeGen::DoGoto(LGoto* instr) {
2208 EmitGoto(instr->block_id());
2212 Condition LCodeGen::TokenToCondition(Token::Value op, bool is_unsigned) {
2213 Condition cond = no_condition;
2216 case Token::EQ_STRICT:
2220 case Token::NE_STRICT:
2224 cond = is_unsigned ? below : less;
2227 cond = is_unsigned ? above : greater;
2230 cond = is_unsigned ? below_equal : less_equal;
2233 cond = is_unsigned ? above_equal : greater_equal;
2236 case Token::INSTANCEOF:
2244 void LCodeGen::DoCompareNumericAndBranch(LCompareNumericAndBranch* instr) {
2245 LOperand* left = instr->left();
2246 LOperand* right = instr->right();
2248 instr->is_double() ||
2249 instr->hydrogen()->left()->CheckFlag(HInstruction::kUint32) ||
2250 instr->hydrogen()->right()->CheckFlag(HInstruction::kUint32);
2251 Condition cc = TokenToCondition(instr->op(), is_unsigned);
2253 if (left->IsConstantOperand() && right->IsConstantOperand()) {
2254 // We can statically evaluate the comparison.
2255 double left_val = ToDouble(LConstantOperand::cast(left));
2256 double right_val = ToDouble(LConstantOperand::cast(right));
2257 int next_block = EvalComparison(instr->op(), left_val, right_val) ?
2258 instr->TrueDestination(chunk_) : instr->FalseDestination(chunk_);
2259 EmitGoto(next_block);
2261 if (instr->is_double()) {
2262 __ ucomisd(ToDoubleRegister(left), ToDoubleRegister(right));
2263 // Don't base result on EFLAGS when a NaN is involved. Instead
2264 // jump to the false block.
2265 __ j(parity_even, instr->FalseLabel(chunk_));
2267 if (right->IsConstantOperand()) {
2268 __ cmp(ToOperand(left),
2269 ToImmediate(right, instr->hydrogen()->representation()));
2270 } else if (left->IsConstantOperand()) {
2271 __ cmp(ToOperand(right),
2272 ToImmediate(left, instr->hydrogen()->representation()));
2273 // We commuted the operands, so commute the condition.
2274 cc = CommuteCondition(cc);
2276 __ cmp(ToRegister(left), ToOperand(right));
2279 EmitBranch(instr, cc);
2284 void LCodeGen::DoCmpObjectEqAndBranch(LCmpObjectEqAndBranch* instr) {
2285 Register left = ToRegister(instr->left());
2287 if (instr->right()->IsConstantOperand()) {
2288 Handle<Object> right = ToHandle(LConstantOperand::cast(instr->right()));
2289 __ CmpObject(left, right);
2291 Operand right = ToOperand(instr->right());
2292 __ cmp(left, right);
2294 EmitBranch(instr, equal);
2298 void LCodeGen::DoCmpHoleAndBranch(LCmpHoleAndBranch* instr) {
2299 if (instr->hydrogen()->representation().IsTagged()) {
2300 Register input_reg = ToRegister(instr->object());
2301 __ cmp(input_reg, factory()->the_hole_value());
2302 EmitBranch(instr, equal);
2306 XMMRegister input_reg = ToDoubleRegister(instr->object());
2307 __ ucomisd(input_reg, input_reg);
2308 EmitFalseBranch(instr, parity_odd);
2310 __ sub(esp, Immediate(kDoubleSize));
2311 __ movsd(MemOperand(esp, 0), input_reg);
2313 __ add(esp, Immediate(kDoubleSize));
2314 int offset = sizeof(kHoleNanUpper32);
2315 __ cmp(MemOperand(esp, -offset), Immediate(kHoleNanUpper32));
2316 EmitBranch(instr, equal);
2320 void LCodeGen::DoCompareMinusZeroAndBranch(LCompareMinusZeroAndBranch* instr) {
2321 Representation rep = instr->hydrogen()->value()->representation();
2322 DCHECK(!rep.IsInteger32());
2323 Register scratch = ToRegister(instr->temp());
2325 if (rep.IsDouble()) {
2326 XMMRegister value = ToDoubleRegister(instr->value());
2327 XMMRegister xmm_scratch = double_scratch0();
2328 __ xorps(xmm_scratch, xmm_scratch);
2329 __ ucomisd(xmm_scratch, value);
2330 EmitFalseBranch(instr, not_equal);
2331 __ movmskpd(scratch, value);
2332 __ test(scratch, Immediate(1));
2333 EmitBranch(instr, not_zero);
2335 Register value = ToRegister(instr->value());
2336 Handle<Map> map = masm()->isolate()->factory()->heap_number_map();
2337 __ CheckMap(value, map, instr->FalseLabel(chunk()), DO_SMI_CHECK);
2338 __ cmp(FieldOperand(value, HeapNumber::kExponentOffset),
2340 EmitFalseBranch(instr, no_overflow);
2341 __ cmp(FieldOperand(value, HeapNumber::kMantissaOffset),
2342 Immediate(0x00000000));
2343 EmitBranch(instr, equal);
2348 Condition LCodeGen::EmitIsObject(Register input,
2350 Label* is_not_object,
2352 __ JumpIfSmi(input, is_not_object);
2354 __ cmp(input, isolate()->factory()->null_value());
2355 __ j(equal, is_object);
2357 __ mov(temp1, FieldOperand(input, HeapObject::kMapOffset));
2358 // Undetectable objects behave like undefined.
2359 __ test_b(FieldOperand(temp1, Map::kBitFieldOffset),
2360 1 << Map::kIsUndetectable);
2361 __ j(not_zero, is_not_object);
2363 __ movzx_b(temp1, FieldOperand(temp1, Map::kInstanceTypeOffset));
2364 __ cmp(temp1, FIRST_NONCALLABLE_SPEC_OBJECT_TYPE);
2365 __ j(below, is_not_object);
2366 __ cmp(temp1, LAST_NONCALLABLE_SPEC_OBJECT_TYPE);
2371 void LCodeGen::DoIsObjectAndBranch(LIsObjectAndBranch* instr) {
2372 Register reg = ToRegister(instr->value());
2373 Register temp = ToRegister(instr->temp());
2375 Condition true_cond = EmitIsObject(
2376 reg, temp, instr->FalseLabel(chunk_), instr->TrueLabel(chunk_));
2378 EmitBranch(instr, true_cond);
2382 Condition LCodeGen::EmitIsString(Register input,
2384 Label* is_not_string,
2385 SmiCheck check_needed = INLINE_SMI_CHECK) {
2386 if (check_needed == INLINE_SMI_CHECK) {
2387 __ JumpIfSmi(input, is_not_string);
2390 Condition cond = masm_->IsObjectStringType(input, temp1, temp1);
2396 void LCodeGen::DoIsStringAndBranch(LIsStringAndBranch* instr) {
2397 Register reg = ToRegister(instr->value());
2398 Register temp = ToRegister(instr->temp());
2400 SmiCheck check_needed =
2401 instr->hydrogen()->value()->type().IsHeapObject()
2402 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
2404 Condition true_cond = EmitIsString(
2405 reg, temp, instr->FalseLabel(chunk_), check_needed);
2407 EmitBranch(instr, true_cond);
2411 void LCodeGen::DoIsSmiAndBranch(LIsSmiAndBranch* instr) {
2412 Operand input = ToOperand(instr->value());
2414 __ test(input, Immediate(kSmiTagMask));
2415 EmitBranch(instr, zero);
2419 void LCodeGen::DoIsUndetectableAndBranch(LIsUndetectableAndBranch* instr) {
2420 Register input = ToRegister(instr->value());
2421 Register temp = ToRegister(instr->temp());
2423 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2424 STATIC_ASSERT(kSmiTag == 0);
2425 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2427 __ mov(temp, FieldOperand(input, HeapObject::kMapOffset));
2428 __ test_b(FieldOperand(temp, Map::kBitFieldOffset),
2429 1 << Map::kIsUndetectable);
2430 EmitBranch(instr, not_zero);
2434 static Condition ComputeCompareCondition(Token::Value op) {
2436 case Token::EQ_STRICT:
2446 return greater_equal;
2449 return no_condition;
2454 void LCodeGen::DoStringCompareAndBranch(LStringCompareAndBranch* instr) {
2455 Token::Value op = instr->op();
2457 Handle<Code> ic = CodeFactory::CompareIC(isolate(), op).code();
2458 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2460 Condition condition = ComputeCompareCondition(op);
2461 __ test(eax, Operand(eax));
2463 EmitBranch(instr, condition);
2467 static InstanceType TestType(HHasInstanceTypeAndBranch* instr) {
2468 InstanceType from = instr->from();
2469 InstanceType to = instr->to();
2470 if (from == FIRST_TYPE) return to;
2471 DCHECK(from == to || to == LAST_TYPE);
2476 static Condition BranchCondition(HHasInstanceTypeAndBranch* instr) {
2477 InstanceType from = instr->from();
2478 InstanceType to = instr->to();
2479 if (from == to) return equal;
2480 if (to == LAST_TYPE) return above_equal;
2481 if (from == FIRST_TYPE) return below_equal;
2487 void LCodeGen::DoHasInstanceTypeAndBranch(LHasInstanceTypeAndBranch* instr) {
2488 Register input = ToRegister(instr->value());
2489 Register temp = ToRegister(instr->temp());
2491 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2492 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2495 __ CmpObjectType(input, TestType(instr->hydrogen()), temp);
2496 EmitBranch(instr, BranchCondition(instr->hydrogen()));
2500 void LCodeGen::DoGetCachedArrayIndex(LGetCachedArrayIndex* instr) {
2501 Register input = ToRegister(instr->value());
2502 Register result = ToRegister(instr->result());
2504 __ AssertString(input);
2506 __ mov(result, FieldOperand(input, String::kHashFieldOffset));
2507 __ IndexFromHash(result, result);
2511 void LCodeGen::DoHasCachedArrayIndexAndBranch(
2512 LHasCachedArrayIndexAndBranch* instr) {
2513 Register input = ToRegister(instr->value());
2515 __ test(FieldOperand(input, String::kHashFieldOffset),
2516 Immediate(String::kContainsCachedArrayIndexMask));
2517 EmitBranch(instr, equal);
2521 // Branches to a label or falls through with the answer in the z flag. Trashes
2522 // the temp registers, but not the input.
2523 void LCodeGen::EmitClassOfTest(Label* is_true,
2525 Handle<String>class_name,
2529 DCHECK(!input.is(temp));
2530 DCHECK(!input.is(temp2));
2531 DCHECK(!temp.is(temp2));
2532 __ JumpIfSmi(input, is_false);
2534 if (String::Equals(isolate()->factory()->Function_string(), class_name)) {
2535 // Assuming the following assertions, we can use the same compares to test
2536 // for both being a function type and being in the object type range.
2537 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
2538 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2539 FIRST_SPEC_OBJECT_TYPE + 1);
2540 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2541 LAST_SPEC_OBJECT_TYPE - 1);
2542 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
2543 __ CmpObjectType(input, FIRST_SPEC_OBJECT_TYPE, temp);
2544 __ j(below, is_false);
2545 __ j(equal, is_true);
2546 __ CmpInstanceType(temp, LAST_SPEC_OBJECT_TYPE);
2547 __ j(equal, is_true);
2549 // Faster code path to avoid two compares: subtract lower bound from the
2550 // actual type and do a signed compare with the width of the type range.
2551 __ mov(temp, FieldOperand(input, HeapObject::kMapOffset));
2552 __ movzx_b(temp2, FieldOperand(temp, Map::kInstanceTypeOffset));
2553 __ sub(Operand(temp2), Immediate(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2554 __ cmp(Operand(temp2), Immediate(LAST_NONCALLABLE_SPEC_OBJECT_TYPE -
2555 FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2556 __ j(above, is_false);
2559 // Now we are in the FIRST-LAST_NONCALLABLE_SPEC_OBJECT_TYPE range.
2560 // Check if the constructor in the map is a function.
2561 __ mov(temp, FieldOperand(temp, Map::kConstructorOffset));
2562 // Objects with a non-function constructor have class 'Object'.
2563 __ CmpObjectType(temp, JS_FUNCTION_TYPE, temp2);
2564 if (String::Equals(class_name, isolate()->factory()->Object_string())) {
2565 __ j(not_equal, is_true);
2567 __ j(not_equal, is_false);
2570 // temp now contains the constructor function. Grab the
2571 // instance class name from there.
2572 __ mov(temp, FieldOperand(temp, JSFunction::kSharedFunctionInfoOffset));
2573 __ mov(temp, FieldOperand(temp,
2574 SharedFunctionInfo::kInstanceClassNameOffset));
2575 // The class name we are testing against is internalized since it's a literal.
2576 // The name in the constructor is internalized because of the way the context
2577 // is booted. This routine isn't expected to work for random API-created
2578 // classes and it doesn't have to because you can't access it with natives
2579 // syntax. Since both sides are internalized it is sufficient to use an
2580 // identity comparison.
2581 __ cmp(temp, class_name);
2582 // End with the answer in the z flag.
2586 void LCodeGen::DoClassOfTestAndBranch(LClassOfTestAndBranch* instr) {
2587 Register input = ToRegister(instr->value());
2588 Register temp = ToRegister(instr->temp());
2589 Register temp2 = ToRegister(instr->temp2());
2591 Handle<String> class_name = instr->hydrogen()->class_name();
2593 EmitClassOfTest(instr->TrueLabel(chunk_), instr->FalseLabel(chunk_),
2594 class_name, input, temp, temp2);
2596 EmitBranch(instr, equal);
2600 void LCodeGen::DoCmpMapAndBranch(LCmpMapAndBranch* instr) {
2601 Register reg = ToRegister(instr->value());
2602 __ cmp(FieldOperand(reg, HeapObject::kMapOffset), instr->map());
2603 EmitBranch(instr, equal);
2607 void LCodeGen::DoInstanceOf(LInstanceOf* instr) {
2608 // Object and function are in fixed registers defined by the stub.
2609 DCHECK(ToRegister(instr->context()).is(esi));
2610 InstanceofStub stub(isolate(), InstanceofStub::kArgsInRegisters);
2611 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
2613 Label true_value, done;
2614 __ test(eax, Operand(eax));
2615 __ j(zero, &true_value, Label::kNear);
2616 __ mov(ToRegister(instr->result()), factory()->false_value());
2617 __ jmp(&done, Label::kNear);
2618 __ bind(&true_value);
2619 __ mov(ToRegister(instr->result()), factory()->true_value());
2624 void LCodeGen::DoInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr) {
2625 class DeferredInstanceOfKnownGlobal FINAL : public LDeferredCode {
2627 DeferredInstanceOfKnownGlobal(LCodeGen* codegen,
2628 LInstanceOfKnownGlobal* instr)
2629 : LDeferredCode(codegen), instr_(instr) { }
2630 virtual void Generate() OVERRIDE {
2631 codegen()->DoDeferredInstanceOfKnownGlobal(instr_, &map_check_);
2633 virtual LInstruction* instr() OVERRIDE { return instr_; }
2634 Label* map_check() { return &map_check_; }
2636 LInstanceOfKnownGlobal* instr_;
2640 DeferredInstanceOfKnownGlobal* deferred;
2641 deferred = new(zone()) DeferredInstanceOfKnownGlobal(this, instr);
2643 Label done, false_result;
2644 Register object = ToRegister(instr->value());
2645 Register temp = ToRegister(instr->temp());
2647 // A Smi is not an instance of anything.
2648 __ JumpIfSmi(object, &false_result, Label::kNear);
2650 // This is the inlined call site instanceof cache. The two occurences of the
2651 // hole value will be patched to the last map/result pair generated by the
2654 Register map = ToRegister(instr->temp());
2655 __ mov(map, FieldOperand(object, HeapObject::kMapOffset));
2656 __ bind(deferred->map_check()); // Label for calculating code patching.
2657 Handle<Cell> cache_cell = factory()->NewCell(factory()->the_hole_value());
2658 __ cmp(map, Operand::ForCell(cache_cell)); // Patched to cached map.
2659 __ j(not_equal, &cache_miss, Label::kNear);
2660 __ mov(eax, factory()->the_hole_value()); // Patched to either true or false.
2661 __ jmp(&done, Label::kNear);
2663 // The inlined call site cache did not match. Check for null and string
2664 // before calling the deferred code.
2665 __ bind(&cache_miss);
2666 // Null is not an instance of anything.
2667 __ cmp(object, factory()->null_value());
2668 __ j(equal, &false_result, Label::kNear);
2670 // String values are not instances of anything.
2671 Condition is_string = masm_->IsObjectStringType(object, temp, temp);
2672 __ j(is_string, &false_result, Label::kNear);
2674 // Go to the deferred code.
2675 __ jmp(deferred->entry());
2677 __ bind(&false_result);
2678 __ mov(ToRegister(instr->result()), factory()->false_value());
2680 // Here result has either true or false. Deferred code also produces true or
2682 __ bind(deferred->exit());
2687 void LCodeGen::DoDeferredInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr,
2689 PushSafepointRegistersScope scope(this);
2691 InstanceofStub::Flags flags = InstanceofStub::kNoFlags;
2692 flags = static_cast<InstanceofStub::Flags>(
2693 flags | InstanceofStub::kArgsInRegisters);
2694 flags = static_cast<InstanceofStub::Flags>(
2695 flags | InstanceofStub::kCallSiteInlineCheck);
2696 flags = static_cast<InstanceofStub::Flags>(
2697 flags | InstanceofStub::kReturnTrueFalseObject);
2698 InstanceofStub stub(isolate(), flags);
2700 // Get the temp register reserved by the instruction. This needs to be a
2701 // register which is pushed last by PushSafepointRegisters as top of the
2702 // stack is used to pass the offset to the location of the map check to
2704 Register temp = ToRegister(instr->temp());
2705 DCHECK(MacroAssembler::SafepointRegisterStackIndex(temp) == 0);
2706 __ LoadHeapObject(InstanceofStub::right(), instr->function());
2707 static const int kAdditionalDelta = 13;
2708 int delta = masm_->SizeOfCodeGeneratedSince(map_check) + kAdditionalDelta;
2709 __ mov(temp, Immediate(delta));
2710 __ StoreToSafepointRegisterSlot(temp, temp);
2711 CallCodeGeneric(stub.GetCode(),
2712 RelocInfo::CODE_TARGET,
2714 RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
2715 // Get the deoptimization index of the LLazyBailout-environment that
2716 // corresponds to this instruction.
2717 LEnvironment* env = instr->GetDeferredLazyDeoptimizationEnvironment();
2718 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
2720 // Put the result value into the eax slot and restore all registers.
2721 __ StoreToSafepointRegisterSlot(eax, eax);
2725 void LCodeGen::DoCmpT(LCmpT* instr) {
2726 Token::Value op = instr->op();
2728 Handle<Code> ic = CodeFactory::CompareIC(isolate(), op).code();
2729 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2731 Condition condition = ComputeCompareCondition(op);
2732 Label true_value, done;
2733 __ test(eax, Operand(eax));
2734 __ j(condition, &true_value, Label::kNear);
2735 __ mov(ToRegister(instr->result()), factory()->false_value());
2736 __ jmp(&done, Label::kNear);
2737 __ bind(&true_value);
2738 __ mov(ToRegister(instr->result()), factory()->true_value());
2743 void LCodeGen::EmitReturn(LReturn* instr, bool dynamic_frame_alignment) {
2744 int extra_value_count = dynamic_frame_alignment ? 2 : 1;
2746 if (instr->has_constant_parameter_count()) {
2747 int parameter_count = ToInteger32(instr->constant_parameter_count());
2748 if (dynamic_frame_alignment && FLAG_debug_code) {
2750 (parameter_count + extra_value_count) * kPointerSize),
2751 Immediate(kAlignmentZapValue));
2752 __ Assert(equal, kExpectedAlignmentMarker);
2754 __ Ret((parameter_count + extra_value_count) * kPointerSize, ecx);
2756 Register reg = ToRegister(instr->parameter_count());
2757 // The argument count parameter is a smi
2759 Register return_addr_reg = reg.is(ecx) ? ebx : ecx;
2760 if (dynamic_frame_alignment && FLAG_debug_code) {
2761 DCHECK(extra_value_count == 2);
2762 __ cmp(Operand(esp, reg, times_pointer_size,
2763 extra_value_count * kPointerSize),
2764 Immediate(kAlignmentZapValue));
2765 __ Assert(equal, kExpectedAlignmentMarker);
2768 // emit code to restore stack based on instr->parameter_count()
2769 __ pop(return_addr_reg); // save return address
2770 if (dynamic_frame_alignment) {
2771 __ inc(reg); // 1 more for alignment
2773 __ shl(reg, kPointerSizeLog2);
2775 __ jmp(return_addr_reg);
2780 void LCodeGen::DoReturn(LReturn* instr) {
2781 if (FLAG_trace && info()->IsOptimizing()) {
2782 // Preserve the return value on the stack and rely on the runtime call
2783 // to return the value in the same register. We're leaving the code
2784 // managed by the register allocator and tearing down the frame, it's
2785 // safe to write to the context register.
2787 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
2788 __ CallRuntime(Runtime::kTraceExit, 1);
2790 if (info()->saves_caller_doubles()) RestoreCallerDoubles();
2791 if (dynamic_frame_alignment_) {
2792 // Fetch the state of the dynamic frame alignment.
2793 __ mov(edx, Operand(ebp,
2794 JavaScriptFrameConstants::kDynamicAlignmentStateOffset));
2796 int no_frame_start = -1;
2797 if (NeedsEagerFrame()) {
2800 no_frame_start = masm_->pc_offset();
2802 if (dynamic_frame_alignment_) {
2804 __ cmp(edx, Immediate(kNoAlignmentPadding));
2805 __ j(equal, &no_padding, Label::kNear);
2807 EmitReturn(instr, true);
2808 __ bind(&no_padding);
2811 EmitReturn(instr, false);
2812 if (no_frame_start != -1) {
2813 info()->AddNoFrameRange(no_frame_start, masm_->pc_offset());
2818 void LCodeGen::DoLoadGlobalCell(LLoadGlobalCell* instr) {
2819 Register result = ToRegister(instr->result());
2820 __ mov(result, Operand::ForCell(instr->hydrogen()->cell().handle()));
2821 if (instr->hydrogen()->RequiresHoleCheck()) {
2822 __ cmp(result, factory()->the_hole_value());
2823 DeoptimizeIf(equal, instr, "hole");
2829 void LCodeGen::EmitVectorLoadICRegisters(T* instr) {
2830 DCHECK(FLAG_vector_ics);
2831 Register vector_register = ToRegister(instr->temp_vector());
2832 DCHECK(vector_register.is(VectorLoadICDescriptor::VectorRegister()));
2833 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
2834 __ mov(vector_register, vector);
2835 // No need to allocate this register.
2836 DCHECK(VectorLoadICDescriptor::SlotRegister().is(eax));
2837 int index = vector->GetIndex(instr->hydrogen()->slot());
2838 __ mov(VectorLoadICDescriptor::SlotRegister(),
2839 Immediate(Smi::FromInt(index)));
2843 void LCodeGen::DoLoadGlobalGeneric(LLoadGlobalGeneric* instr) {
2844 DCHECK(ToRegister(instr->context()).is(esi));
2845 DCHECK(ToRegister(instr->global_object())
2846 .is(LoadDescriptor::ReceiverRegister()));
2847 DCHECK(ToRegister(instr->result()).is(eax));
2849 __ mov(LoadDescriptor::NameRegister(), instr->name());
2850 if (FLAG_vector_ics) {
2851 EmitVectorLoadICRegisters<LLoadGlobalGeneric>(instr);
2853 ContextualMode mode = instr->for_typeof() ? NOT_CONTEXTUAL : CONTEXTUAL;
2854 Handle<Code> ic = CodeFactory::LoadICInOptimizedCode(isolate(), mode).code();
2855 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2859 void LCodeGen::DoStoreGlobalCell(LStoreGlobalCell* instr) {
2860 Register value = ToRegister(instr->value());
2861 Handle<PropertyCell> cell_handle = instr->hydrogen()->cell().handle();
2863 // If the cell we are storing to contains the hole it could have
2864 // been deleted from the property dictionary. In that case, we need
2865 // to update the property details in the property dictionary to mark
2866 // it as no longer deleted. We deoptimize in that case.
2867 if (instr->hydrogen()->RequiresHoleCheck()) {
2868 __ cmp(Operand::ForCell(cell_handle), factory()->the_hole_value());
2869 DeoptimizeIf(equal, instr, "hole");
2873 __ mov(Operand::ForCell(cell_handle), value);
2874 // Cells are always rescanned, so no write barrier here.
2878 void LCodeGen::DoLoadContextSlot(LLoadContextSlot* instr) {
2879 Register context = ToRegister(instr->context());
2880 Register result = ToRegister(instr->result());
2881 __ mov(result, ContextOperand(context, instr->slot_index()));
2883 if (instr->hydrogen()->RequiresHoleCheck()) {
2884 __ cmp(result, factory()->the_hole_value());
2885 if (instr->hydrogen()->DeoptimizesOnHole()) {
2886 DeoptimizeIf(equal, instr, "hole");
2889 __ j(not_equal, &is_not_hole, Label::kNear);
2890 __ mov(result, factory()->undefined_value());
2891 __ bind(&is_not_hole);
2897 void LCodeGen::DoStoreContextSlot(LStoreContextSlot* instr) {
2898 Register context = ToRegister(instr->context());
2899 Register value = ToRegister(instr->value());
2901 Label skip_assignment;
2903 Operand target = ContextOperand(context, instr->slot_index());
2904 if (instr->hydrogen()->RequiresHoleCheck()) {
2905 __ cmp(target, factory()->the_hole_value());
2906 if (instr->hydrogen()->DeoptimizesOnHole()) {
2907 DeoptimizeIf(equal, instr, "hole");
2909 __ j(not_equal, &skip_assignment, Label::kNear);
2913 __ mov(target, value);
2914 if (instr->hydrogen()->NeedsWriteBarrier()) {
2915 SmiCheck check_needed =
2916 instr->hydrogen()->value()->type().IsHeapObject()
2917 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
2918 Register temp = ToRegister(instr->temp());
2919 int offset = Context::SlotOffset(instr->slot_index());
2920 __ RecordWriteContextSlot(context,
2925 EMIT_REMEMBERED_SET,
2929 __ bind(&skip_assignment);
2933 void LCodeGen::DoLoadNamedField(LLoadNamedField* instr) {
2934 HObjectAccess access = instr->hydrogen()->access();
2935 int offset = access.offset();
2937 if (access.IsExternalMemory()) {
2938 Register result = ToRegister(instr->result());
2939 MemOperand operand = instr->object()->IsConstantOperand()
2940 ? MemOperand::StaticVariable(ToExternalReference(
2941 LConstantOperand::cast(instr->object())))
2942 : MemOperand(ToRegister(instr->object()), offset);
2943 __ Load(result, operand, access.representation());
2947 Register object = ToRegister(instr->object());
2948 if (instr->hydrogen()->representation().IsDouble()) {
2949 XMMRegister result = ToDoubleRegister(instr->result());
2950 __ movsd(result, FieldOperand(object, offset));
2954 Register result = ToRegister(instr->result());
2955 if (!access.IsInobject()) {
2956 __ mov(result, FieldOperand(object, JSObject::kPropertiesOffset));
2959 __ Load(result, FieldOperand(object, offset), access.representation());
2963 void LCodeGen::EmitPushTaggedOperand(LOperand* operand) {
2964 DCHECK(!operand->IsDoubleRegister());
2965 if (operand->IsConstantOperand()) {
2966 Handle<Object> object = ToHandle(LConstantOperand::cast(operand));
2967 AllowDeferredHandleDereference smi_check;
2968 if (object->IsSmi()) {
2969 __ Push(Handle<Smi>::cast(object));
2971 __ PushHeapObject(Handle<HeapObject>::cast(object));
2973 } else if (operand->IsRegister()) {
2974 __ push(ToRegister(operand));
2976 __ push(ToOperand(operand));
2981 void LCodeGen::DoLoadNamedGeneric(LLoadNamedGeneric* instr) {
2982 DCHECK(ToRegister(instr->context()).is(esi));
2983 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
2984 DCHECK(ToRegister(instr->result()).is(eax));
2986 __ mov(LoadDescriptor::NameRegister(), instr->name());
2987 if (FLAG_vector_ics) {
2988 EmitVectorLoadICRegisters<LLoadNamedGeneric>(instr);
2991 CodeFactory::LoadICInOptimizedCode(isolate(), NOT_CONTEXTUAL).code();
2992 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2996 void LCodeGen::DoLoadFunctionPrototype(LLoadFunctionPrototype* instr) {
2997 Register function = ToRegister(instr->function());
2998 Register temp = ToRegister(instr->temp());
2999 Register result = ToRegister(instr->result());
3001 // Get the prototype or initial map from the function.
3003 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
3005 // Check that the function has a prototype or an initial map.
3006 __ cmp(Operand(result), Immediate(factory()->the_hole_value()));
3007 DeoptimizeIf(equal, instr, "hole");
3009 // If the function does not have an initial map, we're done.
3011 __ CmpObjectType(result, MAP_TYPE, temp);
3012 __ j(not_equal, &done, Label::kNear);
3014 // Get the prototype from the initial map.
3015 __ mov(result, FieldOperand(result, Map::kPrototypeOffset));
3022 void LCodeGen::DoLoadRoot(LLoadRoot* instr) {
3023 Register result = ToRegister(instr->result());
3024 __ LoadRoot(result, instr->index());
3028 void LCodeGen::DoAccessArgumentsAt(LAccessArgumentsAt* instr) {
3029 Register arguments = ToRegister(instr->arguments());
3030 Register result = ToRegister(instr->result());
3031 if (instr->length()->IsConstantOperand() &&
3032 instr->index()->IsConstantOperand()) {
3033 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
3034 int const_length = ToInteger32(LConstantOperand::cast(instr->length()));
3035 int index = (const_length - const_index) + 1;
3036 __ mov(result, Operand(arguments, index * kPointerSize));
3038 Register length = ToRegister(instr->length());
3039 Operand index = ToOperand(instr->index());
3040 // There are two words between the frame pointer and the last argument.
3041 // Subtracting from length accounts for one of them add one more.
3042 __ sub(length, index);
3043 __ mov(result, Operand(arguments, length, times_4, kPointerSize));
3048 void LCodeGen::DoLoadKeyedExternalArray(LLoadKeyed* instr) {
3049 ElementsKind elements_kind = instr->elements_kind();
3050 LOperand* key = instr->key();
3051 if (!key->IsConstantOperand() &&
3052 ExternalArrayOpRequiresTemp(instr->hydrogen()->key()->representation(),
3054 __ SmiUntag(ToRegister(key));
3056 Operand operand(BuildFastArrayOperand(
3059 instr->hydrogen()->key()->representation(),
3061 instr->base_offset()));
3062 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
3063 elements_kind == FLOAT32_ELEMENTS) {
3064 XMMRegister result(ToDoubleRegister(instr->result()));
3065 __ movss(result, operand);
3066 __ cvtss2sd(result, result);
3067 } else if (elements_kind == EXTERNAL_FLOAT64_ELEMENTS ||
3068 elements_kind == FLOAT64_ELEMENTS) {
3069 __ movsd(ToDoubleRegister(instr->result()), operand);
3071 Register result(ToRegister(instr->result()));
3072 switch (elements_kind) {
3073 case EXTERNAL_INT8_ELEMENTS:
3075 __ movsx_b(result, operand);
3077 case EXTERNAL_UINT8_CLAMPED_ELEMENTS:
3078 case EXTERNAL_UINT8_ELEMENTS:
3079 case UINT8_ELEMENTS:
3080 case UINT8_CLAMPED_ELEMENTS:
3081 __ movzx_b(result, operand);
3083 case EXTERNAL_INT16_ELEMENTS:
3084 case INT16_ELEMENTS:
3085 __ movsx_w(result, operand);
3087 case EXTERNAL_UINT16_ELEMENTS:
3088 case UINT16_ELEMENTS:
3089 __ movzx_w(result, operand);
3091 case EXTERNAL_INT32_ELEMENTS:
3092 case INT32_ELEMENTS:
3093 __ mov(result, operand);
3095 case EXTERNAL_UINT32_ELEMENTS:
3096 case UINT32_ELEMENTS:
3097 __ mov(result, operand);
3098 if (!instr->hydrogen()->CheckFlag(HInstruction::kUint32)) {
3099 __ test(result, Operand(result));
3100 DeoptimizeIf(negative, instr, "negative value");
3103 case EXTERNAL_FLOAT32_ELEMENTS:
3104 case EXTERNAL_FLOAT64_ELEMENTS:
3105 case FLOAT32_ELEMENTS:
3106 case FLOAT64_ELEMENTS:
3107 case FAST_SMI_ELEMENTS:
3109 case FAST_DOUBLE_ELEMENTS:
3110 case FAST_HOLEY_SMI_ELEMENTS:
3111 case FAST_HOLEY_ELEMENTS:
3112 case FAST_HOLEY_DOUBLE_ELEMENTS:
3113 case DICTIONARY_ELEMENTS:
3114 case SLOPPY_ARGUMENTS_ELEMENTS:
3122 void LCodeGen::DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr) {
3123 if (instr->hydrogen()->RequiresHoleCheck()) {
3124 Operand hole_check_operand = BuildFastArrayOperand(
3125 instr->elements(), instr->key(),
3126 instr->hydrogen()->key()->representation(),
3127 FAST_DOUBLE_ELEMENTS,
3128 instr->base_offset() + sizeof(kHoleNanLower32));
3129 __ cmp(hole_check_operand, Immediate(kHoleNanUpper32));
3130 DeoptimizeIf(equal, instr, "hole");
3133 Operand double_load_operand = BuildFastArrayOperand(
3136 instr->hydrogen()->key()->representation(),
3137 FAST_DOUBLE_ELEMENTS,
3138 instr->base_offset());
3139 XMMRegister result = ToDoubleRegister(instr->result());
3140 __ movsd(result, double_load_operand);
3144 void LCodeGen::DoLoadKeyedFixedArray(LLoadKeyed* instr) {
3145 Register result = ToRegister(instr->result());
3149 BuildFastArrayOperand(instr->elements(), instr->key(),
3150 instr->hydrogen()->key()->representation(),
3151 FAST_ELEMENTS, instr->base_offset()));
3153 // Check for the hole value.
3154 if (instr->hydrogen()->RequiresHoleCheck()) {
3155 if (IsFastSmiElementsKind(instr->hydrogen()->elements_kind())) {
3156 __ test(result, Immediate(kSmiTagMask));
3157 DeoptimizeIf(not_equal, instr, "not a Smi");
3159 __ cmp(result, factory()->the_hole_value());
3160 DeoptimizeIf(equal, instr, "hole");
3166 void LCodeGen::DoLoadKeyed(LLoadKeyed* instr) {
3167 if (instr->is_typed_elements()) {
3168 DoLoadKeyedExternalArray(instr);
3169 } else if (instr->hydrogen()->representation().IsDouble()) {
3170 DoLoadKeyedFixedDoubleArray(instr);
3172 DoLoadKeyedFixedArray(instr);
3177 Operand LCodeGen::BuildFastArrayOperand(
3178 LOperand* elements_pointer,
3180 Representation key_representation,
3181 ElementsKind elements_kind,
3182 uint32_t base_offset) {
3183 Register elements_pointer_reg = ToRegister(elements_pointer);
3184 int element_shift_size = ElementsKindToShiftSize(elements_kind);
3185 int shift_size = element_shift_size;
3186 if (key->IsConstantOperand()) {
3187 int constant_value = ToInteger32(LConstantOperand::cast(key));
3188 if (constant_value & 0xF0000000) {
3189 Abort(kArrayIndexConstantValueTooBig);
3191 return Operand(elements_pointer_reg,
3192 ((constant_value) << shift_size)
3195 // Take the tag bit into account while computing the shift size.
3196 if (key_representation.IsSmi() && (shift_size >= 1)) {
3197 shift_size -= kSmiTagSize;
3199 ScaleFactor scale_factor = static_cast<ScaleFactor>(shift_size);
3200 return Operand(elements_pointer_reg,
3208 void LCodeGen::DoLoadKeyedGeneric(LLoadKeyedGeneric* instr) {
3209 DCHECK(ToRegister(instr->context()).is(esi));
3210 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
3211 DCHECK(ToRegister(instr->key()).is(LoadDescriptor::NameRegister()));
3213 if (FLAG_vector_ics) {
3214 EmitVectorLoadICRegisters<LLoadKeyedGeneric>(instr);
3217 Handle<Code> ic = CodeFactory::KeyedLoadICInOptimizedCode(isolate()).code();
3218 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3222 void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) {
3223 Register result = ToRegister(instr->result());
3225 if (instr->hydrogen()->from_inlined()) {
3226 __ lea(result, Operand(esp, -2 * kPointerSize));
3228 // Check for arguments adapter frame.
3229 Label done, adapted;
3230 __ mov(result, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
3231 __ mov(result, Operand(result, StandardFrameConstants::kContextOffset));
3232 __ cmp(Operand(result),
3233 Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3234 __ j(equal, &adapted, Label::kNear);
3236 // No arguments adaptor frame.
3237 __ mov(result, Operand(ebp));
3238 __ jmp(&done, Label::kNear);
3240 // Arguments adaptor frame present.
3242 __ mov(result, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
3244 // Result is the frame pointer for the frame if not adapted and for the real
3245 // frame below the adaptor frame if adapted.
3251 void LCodeGen::DoArgumentsLength(LArgumentsLength* instr) {
3252 Operand elem = ToOperand(instr->elements());
3253 Register result = ToRegister(instr->result());
3257 // If no arguments adaptor frame the number of arguments is fixed.
3259 __ mov(result, Immediate(scope()->num_parameters()));
3260 __ j(equal, &done, Label::kNear);
3262 // Arguments adaptor frame present. Get argument length from there.
3263 __ mov(result, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
3264 __ mov(result, Operand(result,
3265 ArgumentsAdaptorFrameConstants::kLengthOffset));
3266 __ SmiUntag(result);
3268 // Argument length is in result register.
3273 void LCodeGen::DoWrapReceiver(LWrapReceiver* instr) {
3274 Register receiver = ToRegister(instr->receiver());
3275 Register function = ToRegister(instr->function());
3277 // If the receiver is null or undefined, we have to pass the global
3278 // object as a receiver to normal functions. Values have to be
3279 // passed unchanged to builtins and strict-mode functions.
3280 Label receiver_ok, global_object;
3281 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
3282 Register scratch = ToRegister(instr->temp());
3284 if (!instr->hydrogen()->known_function()) {
3285 // Do not transform the receiver to object for strict mode
3288 FieldOperand(function, JSFunction::kSharedFunctionInfoOffset));
3289 __ test_b(FieldOperand(scratch, SharedFunctionInfo::kStrictModeByteOffset),
3290 1 << SharedFunctionInfo::kStrictModeBitWithinByte);
3291 __ j(not_equal, &receiver_ok, dist);
3293 // Do not transform the receiver to object for builtins.
3294 __ test_b(FieldOperand(scratch, SharedFunctionInfo::kNativeByteOffset),
3295 1 << SharedFunctionInfo::kNativeBitWithinByte);
3296 __ j(not_equal, &receiver_ok, dist);
3299 // Normal function. Replace undefined or null with global receiver.
3300 __ cmp(receiver, factory()->null_value());
3301 __ j(equal, &global_object, Label::kNear);
3302 __ cmp(receiver, factory()->undefined_value());
3303 __ j(equal, &global_object, Label::kNear);
3305 // The receiver should be a JS object.
3306 __ test(receiver, Immediate(kSmiTagMask));
3307 DeoptimizeIf(equal, instr, "Smi");
3308 __ CmpObjectType(receiver, FIRST_SPEC_OBJECT_TYPE, scratch);
3309 DeoptimizeIf(below, instr, "not a JavaScript object");
3311 __ jmp(&receiver_ok, Label::kNear);
3312 __ bind(&global_object);
3313 __ mov(receiver, FieldOperand(function, JSFunction::kContextOffset));
3314 const int global_offset = Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX);
3315 __ mov(receiver, Operand(receiver, global_offset));
3316 const int proxy_offset = GlobalObject::kGlobalProxyOffset;
3317 __ mov(receiver, FieldOperand(receiver, proxy_offset));
3318 __ bind(&receiver_ok);
3322 void LCodeGen::DoApplyArguments(LApplyArguments* instr) {
3323 Register receiver = ToRegister(instr->receiver());
3324 Register function = ToRegister(instr->function());
3325 Register length = ToRegister(instr->length());
3326 Register elements = ToRegister(instr->elements());
3327 DCHECK(receiver.is(eax)); // Used for parameter count.
3328 DCHECK(function.is(edi)); // Required by InvokeFunction.
3329 DCHECK(ToRegister(instr->result()).is(eax));
3331 // Copy the arguments to this function possibly from the
3332 // adaptor frame below it.
3333 const uint32_t kArgumentsLimit = 1 * KB;
3334 __ cmp(length, kArgumentsLimit);
3335 DeoptimizeIf(above, instr, "too many arguments");
3338 __ mov(receiver, length);
3340 // Loop through the arguments pushing them onto the execution
3343 // length is a small non-negative integer, due to the test above.
3344 __ test(length, Operand(length));
3345 __ j(zero, &invoke, Label::kNear);
3347 __ push(Operand(elements, length, times_pointer_size, 1 * kPointerSize));
3349 __ j(not_zero, &loop);
3351 // Invoke the function.
3353 DCHECK(instr->HasPointerMap());
3354 LPointerMap* pointers = instr->pointer_map();
3355 SafepointGenerator safepoint_generator(
3356 this, pointers, Safepoint::kLazyDeopt);
3357 ParameterCount actual(eax);
3358 __ InvokeFunction(function, actual, CALL_FUNCTION, safepoint_generator);
3362 void LCodeGen::DoDebugBreak(LDebugBreak* instr) {
3367 void LCodeGen::DoPushArgument(LPushArgument* instr) {
3368 LOperand* argument = instr->value();
3369 EmitPushTaggedOperand(argument);
3373 void LCodeGen::DoDrop(LDrop* instr) {
3374 __ Drop(instr->count());
3378 void LCodeGen::DoThisFunction(LThisFunction* instr) {
3379 Register result = ToRegister(instr->result());
3380 __ mov(result, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
3384 void LCodeGen::DoContext(LContext* instr) {
3385 Register result = ToRegister(instr->result());
3386 if (info()->IsOptimizing()) {
3387 __ mov(result, Operand(ebp, StandardFrameConstants::kContextOffset));
3389 // If there is no frame, the context must be in esi.
3390 DCHECK(result.is(esi));
3395 void LCodeGen::DoDeclareGlobals(LDeclareGlobals* instr) {
3396 DCHECK(ToRegister(instr->context()).is(esi));
3397 __ push(esi); // The context is the first argument.
3398 __ push(Immediate(instr->hydrogen()->pairs()));
3399 __ push(Immediate(Smi::FromInt(instr->hydrogen()->flags())));
3400 CallRuntime(Runtime::kDeclareGlobals, 3, instr);
3404 void LCodeGen::CallKnownFunction(Handle<JSFunction> function,
3405 int formal_parameter_count,
3407 LInstruction* instr,
3408 EDIState edi_state) {
3409 bool dont_adapt_arguments =
3410 formal_parameter_count == SharedFunctionInfo::kDontAdaptArgumentsSentinel;
3411 bool can_invoke_directly =
3412 dont_adapt_arguments || formal_parameter_count == arity;
3414 if (can_invoke_directly) {
3415 if (edi_state == EDI_UNINITIALIZED) {
3416 __ LoadHeapObject(edi, function);
3420 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
3422 // Set eax to arguments count if adaption is not needed. Assumes that eax
3423 // is available to write to at this point.
3424 if (dont_adapt_arguments) {
3428 // Invoke function directly.
3429 if (function.is_identical_to(info()->closure())) {
3432 __ call(FieldOperand(edi, JSFunction::kCodeEntryOffset));
3434 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
3436 // We need to adapt arguments.
3437 LPointerMap* pointers = instr->pointer_map();
3438 SafepointGenerator generator(
3439 this, pointers, Safepoint::kLazyDeopt);
3440 ParameterCount count(arity);
3441 ParameterCount expected(formal_parameter_count);
3442 __ InvokeFunction(function, expected, count, CALL_FUNCTION, generator);
3447 void LCodeGen::DoTailCallThroughMegamorphicCache(
3448 LTailCallThroughMegamorphicCache* instr) {
3449 Register receiver = ToRegister(instr->receiver());
3450 Register name = ToRegister(instr->name());
3451 DCHECK(receiver.is(LoadDescriptor::ReceiverRegister()));
3452 DCHECK(name.is(LoadDescriptor::NameRegister()));
3454 Register scratch = ebx;
3455 Register extra = eax;
3456 DCHECK(!scratch.is(receiver) && !scratch.is(name));
3457 DCHECK(!extra.is(receiver) && !extra.is(name));
3459 // Important for the tail-call.
3460 bool must_teardown_frame = NeedsEagerFrame();
3462 // The probe will tail call to a handler if found.
3463 isolate()->stub_cache()->GenerateProbe(masm(), instr->hydrogen()->flags(),
3464 must_teardown_frame, receiver, name,
3467 // Tail call to miss if we ended up here.
3468 if (must_teardown_frame) __ leave();
3469 LoadIC::GenerateMiss(masm());
3473 void LCodeGen::DoCallWithDescriptor(LCallWithDescriptor* instr) {
3474 DCHECK(ToRegister(instr->result()).is(eax));
3476 LPointerMap* pointers = instr->pointer_map();
3477 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3479 if (instr->target()->IsConstantOperand()) {
3480 LConstantOperand* target = LConstantOperand::cast(instr->target());
3481 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
3482 generator.BeforeCall(__ CallSize(code, RelocInfo::CODE_TARGET));
3483 __ call(code, RelocInfo::CODE_TARGET);
3485 DCHECK(instr->target()->IsRegister());
3486 Register target = ToRegister(instr->target());
3487 generator.BeforeCall(__ CallSize(Operand(target)));
3488 __ add(target, Immediate(Code::kHeaderSize - kHeapObjectTag));
3491 generator.AfterCall();
3495 void LCodeGen::DoCallJSFunction(LCallJSFunction* instr) {
3496 DCHECK(ToRegister(instr->function()).is(edi));
3497 DCHECK(ToRegister(instr->result()).is(eax));
3499 if (instr->hydrogen()->pass_argument_count()) {
3500 __ mov(eax, instr->arity());
3504 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
3506 bool is_self_call = false;
3507 if (instr->hydrogen()->function()->IsConstant()) {
3508 HConstant* fun_const = HConstant::cast(instr->hydrogen()->function());
3509 Handle<JSFunction> jsfun =
3510 Handle<JSFunction>::cast(fun_const->handle(isolate()));
3511 is_self_call = jsfun.is_identical_to(info()->closure());
3517 __ call(FieldOperand(edi, JSFunction::kCodeEntryOffset));
3520 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
3524 void LCodeGen::DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr) {
3525 Register input_reg = ToRegister(instr->value());
3526 __ cmp(FieldOperand(input_reg, HeapObject::kMapOffset),
3527 factory()->heap_number_map());
3528 DeoptimizeIf(not_equal, instr, "not a heap number");
3530 Label slow, allocated, done;
3531 Register tmp = input_reg.is(eax) ? ecx : eax;
3532 Register tmp2 = tmp.is(ecx) ? edx : input_reg.is(ecx) ? edx : ecx;
3534 // Preserve the value of all registers.
3535 PushSafepointRegistersScope scope(this);
3537 __ mov(tmp, FieldOperand(input_reg, HeapNumber::kExponentOffset));
3538 // Check the sign of the argument. If the argument is positive, just
3539 // return it. We do not need to patch the stack since |input| and
3540 // |result| are the same register and |input| will be restored
3541 // unchanged by popping safepoint registers.
3542 __ test(tmp, Immediate(HeapNumber::kSignMask));
3543 __ j(zero, &done, Label::kNear);
3545 __ AllocateHeapNumber(tmp, tmp2, no_reg, &slow);
3546 __ jmp(&allocated, Label::kNear);
3548 // Slow case: Call the runtime system to do the number allocation.
3550 CallRuntimeFromDeferred(Runtime::kAllocateHeapNumber, 0,
3551 instr, instr->context());
3552 // Set the pointer to the new heap number in tmp.
3553 if (!tmp.is(eax)) __ mov(tmp, eax);
3554 // Restore input_reg after call to runtime.
3555 __ LoadFromSafepointRegisterSlot(input_reg, input_reg);
3557 __ bind(&allocated);
3558 __ mov(tmp2, FieldOperand(input_reg, HeapNumber::kExponentOffset));
3559 __ and_(tmp2, ~HeapNumber::kSignMask);
3560 __ mov(FieldOperand(tmp, HeapNumber::kExponentOffset), tmp2);
3561 __ mov(tmp2, FieldOperand(input_reg, HeapNumber::kMantissaOffset));
3562 __ mov(FieldOperand(tmp, HeapNumber::kMantissaOffset), tmp2);
3563 __ StoreToSafepointRegisterSlot(input_reg, tmp);
3569 void LCodeGen::EmitIntegerMathAbs(LMathAbs* instr) {
3570 Register input_reg = ToRegister(instr->value());
3571 __ test(input_reg, Operand(input_reg));
3573 __ j(not_sign, &is_positive, Label::kNear);
3574 __ neg(input_reg); // Sets flags.
3575 DeoptimizeIf(negative, instr, "overflow");
3576 __ bind(&is_positive);
3580 void LCodeGen::DoMathAbs(LMathAbs* instr) {
3581 // Class for deferred case.
3582 class DeferredMathAbsTaggedHeapNumber FINAL : public LDeferredCode {
3584 DeferredMathAbsTaggedHeapNumber(LCodeGen* codegen,
3586 : LDeferredCode(codegen), instr_(instr) { }
3587 virtual void Generate() OVERRIDE {
3588 codegen()->DoDeferredMathAbsTaggedHeapNumber(instr_);
3590 virtual LInstruction* instr() OVERRIDE { return instr_; }
3595 DCHECK(instr->value()->Equals(instr->result()));
3596 Representation r = instr->hydrogen()->value()->representation();
3599 XMMRegister scratch = double_scratch0();
3600 XMMRegister input_reg = ToDoubleRegister(instr->value());
3601 __ xorps(scratch, scratch);
3602 __ subsd(scratch, input_reg);
3603 __ andps(input_reg, scratch);
3604 } else if (r.IsSmiOrInteger32()) {
3605 EmitIntegerMathAbs(instr);
3606 } else { // Tagged case.
3607 DeferredMathAbsTaggedHeapNumber* deferred =
3608 new(zone()) DeferredMathAbsTaggedHeapNumber(this, instr);
3609 Register input_reg = ToRegister(instr->value());
3611 __ JumpIfNotSmi(input_reg, deferred->entry());
3612 EmitIntegerMathAbs(instr);
3613 __ bind(deferred->exit());
3618 void LCodeGen::DoMathFloor(LMathFloor* instr) {
3619 XMMRegister xmm_scratch = double_scratch0();
3620 Register output_reg = ToRegister(instr->result());
3621 XMMRegister input_reg = ToDoubleRegister(instr->value());
3623 if (CpuFeatures::IsSupported(SSE4_1)) {
3624 CpuFeatureScope scope(masm(), SSE4_1);
3625 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3626 // Deoptimize on negative zero.
3628 __ xorps(xmm_scratch, xmm_scratch); // Zero the register.
3629 __ ucomisd(input_reg, xmm_scratch);
3630 __ j(not_equal, &non_zero, Label::kNear);
3631 __ movmskpd(output_reg, input_reg);
3632 __ test(output_reg, Immediate(1));
3633 DeoptimizeIf(not_zero, instr, "minus zero");
3636 __ roundsd(xmm_scratch, input_reg, Assembler::kRoundDown);
3637 __ cvttsd2si(output_reg, Operand(xmm_scratch));
3638 // Overflow is signalled with minint.
3639 __ cmp(output_reg, 0x1);
3640 DeoptimizeIf(overflow, instr, "overflow");
3642 Label negative_sign, done;
3643 // Deoptimize on unordered.
3644 __ xorps(xmm_scratch, xmm_scratch); // Zero the register.
3645 __ ucomisd(input_reg, xmm_scratch);
3646 DeoptimizeIf(parity_even, instr, "NaN");
3647 __ j(below, &negative_sign, Label::kNear);
3649 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3650 // Check for negative zero.
3651 Label positive_sign;
3652 __ j(above, &positive_sign, Label::kNear);
3653 __ movmskpd(output_reg, input_reg);
3654 __ test(output_reg, Immediate(1));
3655 DeoptimizeIf(not_zero, instr, "minus zero");
3656 __ Move(output_reg, Immediate(0));
3657 __ jmp(&done, Label::kNear);
3658 __ bind(&positive_sign);
3661 // Use truncating instruction (OK because input is positive).
3662 __ cvttsd2si(output_reg, Operand(input_reg));
3663 // Overflow is signalled with minint.
3664 __ cmp(output_reg, 0x1);
3665 DeoptimizeIf(overflow, instr, "overflow");
3666 __ jmp(&done, Label::kNear);
3668 // Non-zero negative reaches here.
3669 __ bind(&negative_sign);
3670 // Truncate, then compare and compensate.
3671 __ cvttsd2si(output_reg, Operand(input_reg));
3672 __ Cvtsi2sd(xmm_scratch, output_reg);
3673 __ ucomisd(input_reg, xmm_scratch);
3674 __ j(equal, &done, Label::kNear);
3675 __ sub(output_reg, Immediate(1));
3676 DeoptimizeIf(overflow, instr, "overflow");
3683 void LCodeGen::DoMathRound(LMathRound* instr) {
3684 Register output_reg = ToRegister(instr->result());
3685 XMMRegister input_reg = ToDoubleRegister(instr->value());
3686 XMMRegister xmm_scratch = double_scratch0();
3687 XMMRegister input_temp = ToDoubleRegister(instr->temp());
3688 ExternalReference one_half = ExternalReference::address_of_one_half();
3689 ExternalReference minus_one_half =
3690 ExternalReference::address_of_minus_one_half();
3692 Label done, round_to_zero, below_one_half, do_not_compensate;
3693 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
3695 __ movsd(xmm_scratch, Operand::StaticVariable(one_half));
3696 __ ucomisd(xmm_scratch, input_reg);
3697 __ j(above, &below_one_half, Label::kNear);
3699 // CVTTSD2SI rounds towards zero, since 0.5 <= x, we use floor(0.5 + x).
3700 __ addsd(xmm_scratch, input_reg);
3701 __ cvttsd2si(output_reg, Operand(xmm_scratch));
3702 // Overflow is signalled with minint.
3703 __ cmp(output_reg, 0x1);
3704 DeoptimizeIf(overflow, instr, "overflow");
3705 __ jmp(&done, dist);
3707 __ bind(&below_one_half);
3708 __ movsd(xmm_scratch, Operand::StaticVariable(minus_one_half));
3709 __ ucomisd(xmm_scratch, input_reg);
3710 __ j(below_equal, &round_to_zero, Label::kNear);
3712 // CVTTSD2SI rounds towards zero, we use ceil(x - (-0.5)) and then
3713 // compare and compensate.
3714 __ movaps(input_temp, input_reg); // Do not alter input_reg.
3715 __ subsd(input_temp, xmm_scratch);
3716 __ cvttsd2si(output_reg, Operand(input_temp));
3717 // Catch minint due to overflow, and to prevent overflow when compensating.
3718 __ cmp(output_reg, 0x1);
3719 DeoptimizeIf(overflow, instr, "overflow");
3721 __ Cvtsi2sd(xmm_scratch, output_reg);
3722 __ ucomisd(xmm_scratch, input_temp);
3723 __ j(equal, &done, dist);
3724 __ sub(output_reg, Immediate(1));
3725 // No overflow because we already ruled out minint.
3726 __ jmp(&done, dist);
3728 __ bind(&round_to_zero);
3729 // We return 0 for the input range [+0, 0.5[, or [-0.5, 0.5[ if
3730 // we can ignore the difference between a result of -0 and +0.
3731 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3732 // If the sign is positive, we return +0.
3733 __ movmskpd(output_reg, input_reg);
3734 __ test(output_reg, Immediate(1));
3735 DeoptimizeIf(not_zero, instr, "minus zero");
3737 __ Move(output_reg, Immediate(0));
3742 void LCodeGen::DoMathFround(LMathFround* instr) {
3743 XMMRegister input_reg = ToDoubleRegister(instr->value());
3744 XMMRegister output_reg = ToDoubleRegister(instr->result());
3745 __ cvtsd2ss(output_reg, input_reg);
3746 __ cvtss2sd(output_reg, output_reg);
3750 void LCodeGen::DoMathSqrt(LMathSqrt* instr) {
3751 Operand input = ToOperand(instr->value());
3752 XMMRegister output = ToDoubleRegister(instr->result());
3753 __ sqrtsd(output, input);
3757 void LCodeGen::DoMathPowHalf(LMathPowHalf* instr) {
3758 XMMRegister xmm_scratch = double_scratch0();
3759 XMMRegister input_reg = ToDoubleRegister(instr->value());
3760 Register scratch = ToRegister(instr->temp());
3761 DCHECK(ToDoubleRegister(instr->result()).is(input_reg));
3763 // Note that according to ECMA-262 15.8.2.13:
3764 // Math.pow(-Infinity, 0.5) == Infinity
3765 // Math.sqrt(-Infinity) == NaN
3767 // Check base for -Infinity. According to IEEE-754, single-precision
3768 // -Infinity has the highest 9 bits set and the lowest 23 bits cleared.
3769 __ mov(scratch, 0xFF800000);
3770 __ movd(xmm_scratch, scratch);
3771 __ cvtss2sd(xmm_scratch, xmm_scratch);
3772 __ ucomisd(input_reg, xmm_scratch);
3773 // Comparing -Infinity with NaN results in "unordered", which sets the
3774 // zero flag as if both were equal. However, it also sets the carry flag.
3775 __ j(not_equal, &sqrt, Label::kNear);
3776 __ j(carry, &sqrt, Label::kNear);
3777 // If input is -Infinity, return Infinity.
3778 __ xorps(input_reg, input_reg);
3779 __ subsd(input_reg, xmm_scratch);
3780 __ jmp(&done, Label::kNear);
3784 __ xorps(xmm_scratch, xmm_scratch);
3785 __ addsd(input_reg, xmm_scratch); // Convert -0 to +0.
3786 __ sqrtsd(input_reg, input_reg);
3791 void LCodeGen::DoPower(LPower* instr) {
3792 Representation exponent_type = instr->hydrogen()->right()->representation();
3793 // Having marked this as a call, we can use any registers.
3794 // Just make sure that the input/output registers are the expected ones.
3795 Register tagged_exponent = MathPowTaggedDescriptor::exponent();
3796 DCHECK(!instr->right()->IsDoubleRegister() ||
3797 ToDoubleRegister(instr->right()).is(xmm1));
3798 DCHECK(!instr->right()->IsRegister() ||
3799 ToRegister(instr->right()).is(tagged_exponent));
3800 DCHECK(ToDoubleRegister(instr->left()).is(xmm2));
3801 DCHECK(ToDoubleRegister(instr->result()).is(xmm3));
3803 if (exponent_type.IsSmi()) {
3804 MathPowStub stub(isolate(), MathPowStub::TAGGED);
3806 } else if (exponent_type.IsTagged()) {
3808 __ JumpIfSmi(tagged_exponent, &no_deopt);
3809 DCHECK(!ecx.is(tagged_exponent));
3810 __ CmpObjectType(tagged_exponent, HEAP_NUMBER_TYPE, ecx);
3811 DeoptimizeIf(not_equal, instr, "not a heap number");
3813 MathPowStub stub(isolate(), MathPowStub::TAGGED);
3815 } else if (exponent_type.IsInteger32()) {
3816 MathPowStub stub(isolate(), MathPowStub::INTEGER);
3819 DCHECK(exponent_type.IsDouble());
3820 MathPowStub stub(isolate(), MathPowStub::DOUBLE);
3826 void LCodeGen::DoMathLog(LMathLog* instr) {
3827 DCHECK(instr->value()->Equals(instr->result()));
3828 XMMRegister input_reg = ToDoubleRegister(instr->value());
3829 XMMRegister xmm_scratch = double_scratch0();
3830 Label positive, done, zero;
3831 __ xorps(xmm_scratch, xmm_scratch);
3832 __ ucomisd(input_reg, xmm_scratch);
3833 __ j(above, &positive, Label::kNear);
3834 __ j(not_carry, &zero, Label::kNear);
3835 ExternalReference nan =
3836 ExternalReference::address_of_canonical_non_hole_nan();
3837 __ movsd(input_reg, Operand::StaticVariable(nan));
3838 __ jmp(&done, Label::kNear);
3840 ExternalReference ninf =
3841 ExternalReference::address_of_negative_infinity();
3842 __ movsd(input_reg, Operand::StaticVariable(ninf));
3843 __ jmp(&done, Label::kNear);
3846 __ sub(Operand(esp), Immediate(kDoubleSize));
3847 __ movsd(Operand(esp, 0), input_reg);
3848 __ fld_d(Operand(esp, 0));
3850 __ fstp_d(Operand(esp, 0));
3851 __ movsd(input_reg, Operand(esp, 0));
3852 __ add(Operand(esp), Immediate(kDoubleSize));
3857 void LCodeGen::DoMathClz32(LMathClz32* instr) {
3858 Register input = ToRegister(instr->value());
3859 Register result = ToRegister(instr->result());
3860 Label not_zero_input;
3861 __ bsr(result, input);
3863 __ j(not_zero, ¬_zero_input);
3864 __ Move(result, Immediate(63)); // 63^31 == 32
3866 __ bind(¬_zero_input);
3867 __ xor_(result, Immediate(31)); // for x in [0..31], 31^x == 31-x.
3871 void LCodeGen::DoMathExp(LMathExp* instr) {
3872 XMMRegister input = ToDoubleRegister(instr->value());
3873 XMMRegister result = ToDoubleRegister(instr->result());
3874 XMMRegister temp0 = double_scratch0();
3875 Register temp1 = ToRegister(instr->temp1());
3876 Register temp2 = ToRegister(instr->temp2());
3878 MathExpGenerator::EmitMathExp(masm(), input, result, temp0, temp1, temp2);
3882 void LCodeGen::DoInvokeFunction(LInvokeFunction* instr) {
3883 DCHECK(ToRegister(instr->context()).is(esi));
3884 DCHECK(ToRegister(instr->function()).is(edi));
3885 DCHECK(instr->HasPointerMap());
3887 Handle<JSFunction> known_function = instr->hydrogen()->known_function();
3888 if (known_function.is_null()) {
3889 LPointerMap* pointers = instr->pointer_map();
3890 SafepointGenerator generator(
3891 this, pointers, Safepoint::kLazyDeopt);
3892 ParameterCount count(instr->arity());
3893 __ InvokeFunction(edi, count, CALL_FUNCTION, generator);
3895 CallKnownFunction(known_function,
3896 instr->hydrogen()->formal_parameter_count(),
3899 EDI_CONTAINS_TARGET);
3904 void LCodeGen::DoCallFunction(LCallFunction* instr) {
3905 DCHECK(ToRegister(instr->context()).is(esi));
3906 DCHECK(ToRegister(instr->function()).is(edi));
3907 DCHECK(ToRegister(instr->result()).is(eax));
3909 int arity = instr->arity();
3910 CallFunctionStub stub(isolate(), arity, instr->hydrogen()->function_flags());
3911 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3915 void LCodeGen::DoCallNew(LCallNew* instr) {
3916 DCHECK(ToRegister(instr->context()).is(esi));
3917 DCHECK(ToRegister(instr->constructor()).is(edi));
3918 DCHECK(ToRegister(instr->result()).is(eax));
3920 // No cell in ebx for construct type feedback in optimized code
3921 __ mov(ebx, isolate()->factory()->undefined_value());
3922 CallConstructStub stub(isolate(), NO_CALL_CONSTRUCTOR_FLAGS);
3923 __ Move(eax, Immediate(instr->arity()));
3924 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
3928 void LCodeGen::DoCallNewArray(LCallNewArray* instr) {
3929 DCHECK(ToRegister(instr->context()).is(esi));
3930 DCHECK(ToRegister(instr->constructor()).is(edi));
3931 DCHECK(ToRegister(instr->result()).is(eax));
3933 __ Move(eax, Immediate(instr->arity()));
3934 __ mov(ebx, isolate()->factory()->undefined_value());
3935 ElementsKind kind = instr->hydrogen()->elements_kind();
3936 AllocationSiteOverrideMode override_mode =
3937 (AllocationSite::GetMode(kind) == TRACK_ALLOCATION_SITE)
3938 ? DISABLE_ALLOCATION_SITES
3941 if (instr->arity() == 0) {
3942 ArrayNoArgumentConstructorStub stub(isolate(), kind, override_mode);
3943 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
3944 } else if (instr->arity() == 1) {
3946 if (IsFastPackedElementsKind(kind)) {
3948 // We might need a change here
3949 // look at the first argument
3950 __ mov(ecx, Operand(esp, 0));
3952 __ j(zero, &packed_case, Label::kNear);
3954 ElementsKind holey_kind = GetHoleyElementsKind(kind);
3955 ArraySingleArgumentConstructorStub stub(isolate(),
3958 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
3959 __ jmp(&done, Label::kNear);
3960 __ bind(&packed_case);
3963 ArraySingleArgumentConstructorStub stub(isolate(), kind, override_mode);
3964 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
3967 ArrayNArgumentsConstructorStub stub(isolate(), kind, override_mode);
3968 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
3973 void LCodeGen::DoCallRuntime(LCallRuntime* instr) {
3974 DCHECK(ToRegister(instr->context()).is(esi));
3975 CallRuntime(instr->function(), instr->arity(), instr, instr->save_doubles());
3979 void LCodeGen::DoStoreCodeEntry(LStoreCodeEntry* instr) {
3980 Register function = ToRegister(instr->function());
3981 Register code_object = ToRegister(instr->code_object());
3982 __ lea(code_object, FieldOperand(code_object, Code::kHeaderSize));
3983 __ mov(FieldOperand(function, JSFunction::kCodeEntryOffset), code_object);
3987 void LCodeGen::DoInnerAllocatedObject(LInnerAllocatedObject* instr) {
3988 Register result = ToRegister(instr->result());
3989 Register base = ToRegister(instr->base_object());
3990 if (instr->offset()->IsConstantOperand()) {
3991 LConstantOperand* offset = LConstantOperand::cast(instr->offset());
3992 __ lea(result, Operand(base, ToInteger32(offset)));
3994 Register offset = ToRegister(instr->offset());
3995 __ lea(result, Operand(base, offset, times_1, 0));
4000 void LCodeGen::DoStoreNamedField(LStoreNamedField* instr) {
4001 Representation representation = instr->hydrogen()->field_representation();
4003 HObjectAccess access = instr->hydrogen()->access();
4004 int offset = access.offset();
4006 if (access.IsExternalMemory()) {
4007 DCHECK(!instr->hydrogen()->NeedsWriteBarrier());
4008 MemOperand operand = instr->object()->IsConstantOperand()
4009 ? MemOperand::StaticVariable(
4010 ToExternalReference(LConstantOperand::cast(instr->object())))
4011 : MemOperand(ToRegister(instr->object()), offset);
4012 if (instr->value()->IsConstantOperand()) {
4013 LConstantOperand* operand_value = LConstantOperand::cast(instr->value());
4014 __ mov(operand, Immediate(ToInteger32(operand_value)));
4016 Register value = ToRegister(instr->value());
4017 __ Store(value, operand, representation);
4022 Register object = ToRegister(instr->object());
4023 __ AssertNotSmi(object);
4025 DCHECK(!representation.IsSmi() ||
4026 !instr->value()->IsConstantOperand() ||
4027 IsSmi(LConstantOperand::cast(instr->value())));
4028 if (representation.IsDouble()) {
4029 DCHECK(access.IsInobject());
4030 DCHECK(!instr->hydrogen()->has_transition());
4031 DCHECK(!instr->hydrogen()->NeedsWriteBarrier());
4032 XMMRegister value = ToDoubleRegister(instr->value());
4033 __ movsd(FieldOperand(object, offset), value);
4037 if (instr->hydrogen()->has_transition()) {
4038 Handle<Map> transition = instr->hydrogen()->transition_map();
4039 AddDeprecationDependency(transition);
4040 __ mov(FieldOperand(object, HeapObject::kMapOffset), transition);
4041 if (instr->hydrogen()->NeedsWriteBarrierForMap()) {
4042 Register temp = ToRegister(instr->temp());
4043 Register temp_map = ToRegister(instr->temp_map());
4044 // Update the write barrier for the map field.
4045 __ RecordWriteForMap(object, transition, temp_map, temp, kSaveFPRegs);
4050 Register write_register = object;
4051 if (!access.IsInobject()) {
4052 write_register = ToRegister(instr->temp());
4053 __ mov(write_register, FieldOperand(object, JSObject::kPropertiesOffset));
4056 MemOperand operand = FieldOperand(write_register, offset);
4057 if (instr->value()->IsConstantOperand()) {
4058 LConstantOperand* operand_value = LConstantOperand::cast(instr->value());
4059 if (operand_value->IsRegister()) {
4060 Register value = ToRegister(operand_value);
4061 __ Store(value, operand, representation);
4062 } else if (representation.IsInteger32()) {
4063 Immediate immediate = ToImmediate(operand_value, representation);
4064 DCHECK(!instr->hydrogen()->NeedsWriteBarrier());
4065 __ mov(operand, immediate);
4067 Handle<Object> handle_value = ToHandle(operand_value);
4068 DCHECK(!instr->hydrogen()->NeedsWriteBarrier());
4069 __ mov(operand, handle_value);
4072 Register value = ToRegister(instr->value());
4073 __ Store(value, operand, representation);
4076 if (instr->hydrogen()->NeedsWriteBarrier()) {
4077 Register value = ToRegister(instr->value());
4078 Register temp = access.IsInobject() ? ToRegister(instr->temp()) : object;
4079 // Update the write barrier for the object for in-object properties.
4080 __ RecordWriteField(write_register,
4085 EMIT_REMEMBERED_SET,
4086 instr->hydrogen()->SmiCheckForWriteBarrier(),
4087 instr->hydrogen()->PointersToHereCheckForValue());
4092 void LCodeGen::DoStoreNamedGeneric(LStoreNamedGeneric* instr) {
4093 DCHECK(ToRegister(instr->context()).is(esi));
4094 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4095 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
4097 __ mov(StoreDescriptor::NameRegister(), instr->name());
4098 Handle<Code> ic = StoreIC::initialize_stub(isolate(), instr->strict_mode());
4099 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4103 void LCodeGen::DoBoundsCheck(LBoundsCheck* instr) {
4104 Condition cc = instr->hydrogen()->allow_equality() ? above : above_equal;
4105 if (instr->index()->IsConstantOperand()) {
4106 __ cmp(ToOperand(instr->length()),
4107 ToImmediate(LConstantOperand::cast(instr->index()),
4108 instr->hydrogen()->length()->representation()));
4109 cc = CommuteCondition(cc);
4110 } else if (instr->length()->IsConstantOperand()) {
4111 __ cmp(ToOperand(instr->index()),
4112 ToImmediate(LConstantOperand::cast(instr->length()),
4113 instr->hydrogen()->index()->representation()));
4115 __ cmp(ToRegister(instr->index()), ToOperand(instr->length()));
4117 if (FLAG_debug_code && instr->hydrogen()->skip_check()) {
4119 __ j(NegateCondition(cc), &done, Label::kNear);
4123 DeoptimizeIf(cc, instr, "out of bounds");
4128 void LCodeGen::DoStoreKeyedExternalArray(LStoreKeyed* instr) {
4129 ElementsKind elements_kind = instr->elements_kind();
4130 LOperand* key = instr->key();
4131 if (!key->IsConstantOperand() &&
4132 ExternalArrayOpRequiresTemp(instr->hydrogen()->key()->representation(),
4134 __ SmiUntag(ToRegister(key));
4136 Operand operand(BuildFastArrayOperand(
4139 instr->hydrogen()->key()->representation(),
4141 instr->base_offset()));
4142 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
4143 elements_kind == FLOAT32_ELEMENTS) {
4144 XMMRegister xmm_scratch = double_scratch0();
4145 __ cvtsd2ss(xmm_scratch, ToDoubleRegister(instr->value()));
4146 __ movss(operand, xmm_scratch);
4147 } else if (elements_kind == EXTERNAL_FLOAT64_ELEMENTS ||
4148 elements_kind == FLOAT64_ELEMENTS) {
4149 __ movsd(operand, ToDoubleRegister(instr->value()));
4151 Register value = ToRegister(instr->value());
4152 switch (elements_kind) {
4153 case EXTERNAL_UINT8_CLAMPED_ELEMENTS:
4154 case EXTERNAL_UINT8_ELEMENTS:
4155 case EXTERNAL_INT8_ELEMENTS:
4156 case UINT8_ELEMENTS:
4158 case UINT8_CLAMPED_ELEMENTS:
4159 __ mov_b(operand, value);
4161 case EXTERNAL_INT16_ELEMENTS:
4162 case EXTERNAL_UINT16_ELEMENTS:
4163 case UINT16_ELEMENTS:
4164 case INT16_ELEMENTS:
4165 __ mov_w(operand, value);
4167 case EXTERNAL_INT32_ELEMENTS:
4168 case EXTERNAL_UINT32_ELEMENTS:
4169 case UINT32_ELEMENTS:
4170 case INT32_ELEMENTS:
4171 __ mov(operand, value);
4173 case EXTERNAL_FLOAT32_ELEMENTS:
4174 case EXTERNAL_FLOAT64_ELEMENTS:
4175 case FLOAT32_ELEMENTS:
4176 case FLOAT64_ELEMENTS:
4177 case FAST_SMI_ELEMENTS:
4179 case FAST_DOUBLE_ELEMENTS:
4180 case FAST_HOLEY_SMI_ELEMENTS:
4181 case FAST_HOLEY_ELEMENTS:
4182 case FAST_HOLEY_DOUBLE_ELEMENTS:
4183 case DICTIONARY_ELEMENTS:
4184 case SLOPPY_ARGUMENTS_ELEMENTS:
4192 void LCodeGen::DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr) {
4193 ExternalReference canonical_nan_reference =
4194 ExternalReference::address_of_canonical_non_hole_nan();
4195 Operand double_store_operand = BuildFastArrayOperand(
4198 instr->hydrogen()->key()->representation(),
4199 FAST_DOUBLE_ELEMENTS,
4200 instr->base_offset());
4202 XMMRegister value = ToDoubleRegister(instr->value());
4204 if (instr->NeedsCanonicalization()) {
4207 __ ucomisd(value, value);
4208 __ j(parity_odd, &have_value, Label::kNear); // NaN.
4210 __ movsd(value, Operand::StaticVariable(canonical_nan_reference));
4211 __ bind(&have_value);
4214 __ movsd(double_store_operand, value);
4218 void LCodeGen::DoStoreKeyedFixedArray(LStoreKeyed* instr) {
4219 Register elements = ToRegister(instr->elements());
4220 Register key = instr->key()->IsRegister() ? ToRegister(instr->key()) : no_reg;
4222 Operand operand = BuildFastArrayOperand(
4225 instr->hydrogen()->key()->representation(),
4227 instr->base_offset());
4228 if (instr->value()->IsRegister()) {
4229 __ mov(operand, ToRegister(instr->value()));
4231 LConstantOperand* operand_value = LConstantOperand::cast(instr->value());
4232 if (IsSmi(operand_value)) {
4233 Immediate immediate = ToImmediate(operand_value, Representation::Smi());
4234 __ mov(operand, immediate);
4236 DCHECK(!IsInteger32(operand_value));
4237 Handle<Object> handle_value = ToHandle(operand_value);
4238 __ mov(operand, handle_value);
4242 if (instr->hydrogen()->NeedsWriteBarrier()) {
4243 DCHECK(instr->value()->IsRegister());
4244 Register value = ToRegister(instr->value());
4245 DCHECK(!instr->key()->IsConstantOperand());
4246 SmiCheck check_needed =
4247 instr->hydrogen()->value()->type().IsHeapObject()
4248 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
4249 // Compute address of modified element and store it into key register.
4250 __ lea(key, operand);
4251 __ RecordWrite(elements,
4255 EMIT_REMEMBERED_SET,
4257 instr->hydrogen()->PointersToHereCheckForValue());
4262 void LCodeGen::DoStoreKeyed(LStoreKeyed* instr) {
4263 // By cases...external, fast-double, fast
4264 if (instr->is_typed_elements()) {
4265 DoStoreKeyedExternalArray(instr);
4266 } else if (instr->hydrogen()->value()->representation().IsDouble()) {
4267 DoStoreKeyedFixedDoubleArray(instr);
4269 DoStoreKeyedFixedArray(instr);
4274 void LCodeGen::DoStoreKeyedGeneric(LStoreKeyedGeneric* instr) {
4275 DCHECK(ToRegister(instr->context()).is(esi));
4276 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4277 DCHECK(ToRegister(instr->key()).is(StoreDescriptor::NameRegister()));
4278 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
4281 CodeFactory::KeyedStoreIC(isolate(), instr->strict_mode()).code();
4282 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4286 void LCodeGen::DoTrapAllocationMemento(LTrapAllocationMemento* instr) {
4287 Register object = ToRegister(instr->object());
4288 Register temp = ToRegister(instr->temp());
4289 Label no_memento_found;
4290 __ TestJSArrayForAllocationMemento(object, temp, &no_memento_found);
4291 DeoptimizeIf(equal, instr, "memento found");
4292 __ bind(&no_memento_found);
4296 void LCodeGen::DoTransitionElementsKind(LTransitionElementsKind* instr) {
4297 Register object_reg = ToRegister(instr->object());
4299 Handle<Map> from_map = instr->original_map();
4300 Handle<Map> to_map = instr->transitioned_map();
4301 ElementsKind from_kind = instr->from_kind();
4302 ElementsKind to_kind = instr->to_kind();
4304 Label not_applicable;
4305 bool is_simple_map_transition =
4306 IsSimpleMapChangeTransition(from_kind, to_kind);
4307 Label::Distance branch_distance =
4308 is_simple_map_transition ? Label::kNear : Label::kFar;
4309 __ cmp(FieldOperand(object_reg, HeapObject::kMapOffset), from_map);
4310 __ j(not_equal, ¬_applicable, branch_distance);
4311 if (is_simple_map_transition) {
4312 Register new_map_reg = ToRegister(instr->new_map_temp());
4313 __ mov(FieldOperand(object_reg, HeapObject::kMapOffset),
4316 DCHECK_NE(instr->temp(), NULL);
4317 __ RecordWriteForMap(object_reg, to_map, new_map_reg,
4318 ToRegister(instr->temp()),
4321 DCHECK(ToRegister(instr->context()).is(esi));
4322 DCHECK(object_reg.is(eax));
4323 PushSafepointRegistersScope scope(this);
4324 __ mov(ebx, to_map);
4325 bool is_js_array = from_map->instance_type() == JS_ARRAY_TYPE;
4326 TransitionElementsKindStub stub(isolate(), from_kind, to_kind, is_js_array);
4328 RecordSafepointWithLazyDeopt(instr,
4329 RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
4331 __ bind(¬_applicable);
4335 void LCodeGen::DoStringCharCodeAt(LStringCharCodeAt* instr) {
4336 class DeferredStringCharCodeAt FINAL : public LDeferredCode {
4338 DeferredStringCharCodeAt(LCodeGen* codegen,
4339 LStringCharCodeAt* instr)
4340 : LDeferredCode(codegen), instr_(instr) { }
4341 virtual void Generate() OVERRIDE {
4342 codegen()->DoDeferredStringCharCodeAt(instr_);
4344 virtual LInstruction* instr() OVERRIDE { return instr_; }
4346 LStringCharCodeAt* instr_;
4349 DeferredStringCharCodeAt* deferred =
4350 new(zone()) DeferredStringCharCodeAt(this, instr);
4352 StringCharLoadGenerator::Generate(masm(),
4354 ToRegister(instr->string()),
4355 ToRegister(instr->index()),
4356 ToRegister(instr->result()),
4358 __ bind(deferred->exit());
4362 void LCodeGen::DoDeferredStringCharCodeAt(LStringCharCodeAt* instr) {
4363 Register string = ToRegister(instr->string());
4364 Register result = ToRegister(instr->result());
4366 // TODO(3095996): Get rid of this. For now, we need to make the
4367 // result register contain a valid pointer because it is already
4368 // contained in the register pointer map.
4369 __ Move(result, Immediate(0));
4371 PushSafepointRegistersScope scope(this);
4373 // Push the index as a smi. This is safe because of the checks in
4374 // DoStringCharCodeAt above.
4375 STATIC_ASSERT(String::kMaxLength <= Smi::kMaxValue);
4376 if (instr->index()->IsConstantOperand()) {
4377 Immediate immediate = ToImmediate(LConstantOperand::cast(instr->index()),
4378 Representation::Smi());
4381 Register index = ToRegister(instr->index());
4385 CallRuntimeFromDeferred(Runtime::kStringCharCodeAtRT, 2,
4386 instr, instr->context());
4389 __ StoreToSafepointRegisterSlot(result, eax);
4393 void LCodeGen::DoStringCharFromCode(LStringCharFromCode* instr) {
4394 class DeferredStringCharFromCode FINAL : public LDeferredCode {
4396 DeferredStringCharFromCode(LCodeGen* codegen,
4397 LStringCharFromCode* instr)
4398 : LDeferredCode(codegen), instr_(instr) { }
4399 virtual void Generate() OVERRIDE {
4400 codegen()->DoDeferredStringCharFromCode(instr_);
4402 virtual LInstruction* instr() OVERRIDE { return instr_; }
4404 LStringCharFromCode* instr_;
4407 DeferredStringCharFromCode* deferred =
4408 new(zone()) DeferredStringCharFromCode(this, instr);
4410 DCHECK(instr->hydrogen()->value()->representation().IsInteger32());
4411 Register char_code = ToRegister(instr->char_code());
4412 Register result = ToRegister(instr->result());
4413 DCHECK(!char_code.is(result));
4415 __ cmp(char_code, String::kMaxOneByteCharCode);
4416 __ j(above, deferred->entry());
4417 __ Move(result, Immediate(factory()->single_character_string_cache()));
4418 __ mov(result, FieldOperand(result,
4419 char_code, times_pointer_size,
4420 FixedArray::kHeaderSize));
4421 __ cmp(result, factory()->undefined_value());
4422 __ j(equal, deferred->entry());
4423 __ bind(deferred->exit());
4427 void LCodeGen::DoDeferredStringCharFromCode(LStringCharFromCode* instr) {
4428 Register char_code = ToRegister(instr->char_code());
4429 Register result = ToRegister(instr->result());
4431 // TODO(3095996): Get rid of this. For now, we need to make the
4432 // result register contain a valid pointer because it is already
4433 // contained in the register pointer map.
4434 __ Move(result, Immediate(0));
4436 PushSafepointRegistersScope scope(this);
4437 __ SmiTag(char_code);
4439 CallRuntimeFromDeferred(Runtime::kCharFromCode, 1, instr, instr->context());
4440 __ StoreToSafepointRegisterSlot(result, eax);
4444 void LCodeGen::DoStringAdd(LStringAdd* instr) {
4445 DCHECK(ToRegister(instr->context()).is(esi));
4446 DCHECK(ToRegister(instr->left()).is(edx));
4447 DCHECK(ToRegister(instr->right()).is(eax));
4448 StringAddStub stub(isolate(),
4449 instr->hydrogen()->flags(),
4450 instr->hydrogen()->pretenure_flag());
4451 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
4455 void LCodeGen::DoInteger32ToDouble(LInteger32ToDouble* instr) {
4456 LOperand* input = instr->value();
4457 LOperand* output = instr->result();
4458 DCHECK(input->IsRegister() || input->IsStackSlot());
4459 DCHECK(output->IsDoubleRegister());
4460 __ Cvtsi2sd(ToDoubleRegister(output), ToOperand(input));
4464 void LCodeGen::DoUint32ToDouble(LUint32ToDouble* instr) {
4465 LOperand* input = instr->value();
4466 LOperand* output = instr->result();
4467 __ LoadUint32(ToDoubleRegister(output), ToRegister(input));
4471 void LCodeGen::DoNumberTagI(LNumberTagI* instr) {
4472 class DeferredNumberTagI FINAL : public LDeferredCode {
4474 DeferredNumberTagI(LCodeGen* codegen,
4476 : LDeferredCode(codegen), instr_(instr) { }
4477 virtual void Generate() OVERRIDE {
4478 codegen()->DoDeferredNumberTagIU(
4479 instr_, instr_->value(), instr_->temp(), SIGNED_INT32);
4481 virtual LInstruction* instr() OVERRIDE { return instr_; }
4483 LNumberTagI* instr_;
4486 LOperand* input = instr->value();
4487 DCHECK(input->IsRegister() && input->Equals(instr->result()));
4488 Register reg = ToRegister(input);
4490 DeferredNumberTagI* deferred =
4491 new(zone()) DeferredNumberTagI(this, instr);
4493 __ j(overflow, deferred->entry());
4494 __ bind(deferred->exit());
4498 void LCodeGen::DoNumberTagU(LNumberTagU* instr) {
4499 class DeferredNumberTagU FINAL : public LDeferredCode {
4501 DeferredNumberTagU(LCodeGen* codegen, LNumberTagU* instr)
4502 : LDeferredCode(codegen), instr_(instr) { }
4503 virtual void Generate() OVERRIDE {
4504 codegen()->DoDeferredNumberTagIU(
4505 instr_, instr_->value(), instr_->temp(), UNSIGNED_INT32);
4507 virtual LInstruction* instr() OVERRIDE { return instr_; }
4509 LNumberTagU* instr_;
4512 LOperand* input = instr->value();
4513 DCHECK(input->IsRegister() && input->Equals(instr->result()));
4514 Register reg = ToRegister(input);
4516 DeferredNumberTagU* deferred =
4517 new(zone()) DeferredNumberTagU(this, instr);
4518 __ cmp(reg, Immediate(Smi::kMaxValue));
4519 __ j(above, deferred->entry());
4521 __ bind(deferred->exit());
4525 void LCodeGen::DoDeferredNumberTagIU(LInstruction* instr,
4528 IntegerSignedness signedness) {
4530 Register reg = ToRegister(value);
4531 Register tmp = ToRegister(temp);
4532 XMMRegister xmm_scratch = double_scratch0();
4534 if (signedness == SIGNED_INT32) {
4535 // There was overflow, so bits 30 and 31 of the original integer
4536 // disagree. Try to allocate a heap number in new space and store
4537 // the value in there. If that fails, call the runtime system.
4539 __ xor_(reg, 0x80000000);
4540 __ Cvtsi2sd(xmm_scratch, Operand(reg));
4542 __ LoadUint32(xmm_scratch, reg);
4545 if (FLAG_inline_new) {
4546 __ AllocateHeapNumber(reg, tmp, no_reg, &slow);
4547 __ jmp(&done, Label::kNear);
4550 // Slow case: Call the runtime system to do the number allocation.
4553 // TODO(3095996): Put a valid pointer value in the stack slot where the
4554 // result register is stored, as this register is in the pointer map, but
4555 // contains an integer value.
4556 __ Move(reg, Immediate(0));
4558 // Preserve the value of all registers.
4559 PushSafepointRegistersScope scope(this);
4561 // NumberTagI and NumberTagD use the context from the frame, rather than
4562 // the environment's HContext or HInlinedContext value.
4563 // They only call Runtime::kAllocateHeapNumber.
4564 // The corresponding HChange instructions are added in a phase that does
4565 // not have easy access to the local context.
4566 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
4567 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4568 RecordSafepointWithRegisters(
4569 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4570 __ StoreToSafepointRegisterSlot(reg, eax);
4573 // Done. Put the value in xmm_scratch into the value of the allocated heap
4576 __ movsd(FieldOperand(reg, HeapNumber::kValueOffset), xmm_scratch);
4580 void LCodeGen::DoNumberTagD(LNumberTagD* instr) {
4581 class DeferredNumberTagD FINAL : public LDeferredCode {
4583 DeferredNumberTagD(LCodeGen* codegen, LNumberTagD* instr)
4584 : LDeferredCode(codegen), instr_(instr) { }
4585 virtual void Generate() OVERRIDE {
4586 codegen()->DoDeferredNumberTagD(instr_);
4588 virtual LInstruction* instr() OVERRIDE { return instr_; }
4590 LNumberTagD* instr_;
4593 Register reg = ToRegister(instr->result());
4595 DeferredNumberTagD* deferred =
4596 new(zone()) DeferredNumberTagD(this, instr);
4597 if (FLAG_inline_new) {
4598 Register tmp = ToRegister(instr->temp());
4599 __ AllocateHeapNumber(reg, tmp, no_reg, deferred->entry());
4601 __ jmp(deferred->entry());
4603 __ bind(deferred->exit());
4604 XMMRegister input_reg = ToDoubleRegister(instr->value());
4605 __ movsd(FieldOperand(reg, HeapNumber::kValueOffset), input_reg);
4609 void LCodeGen::DoDeferredNumberTagD(LNumberTagD* instr) {
4610 // TODO(3095996): Get rid of this. For now, we need to make the
4611 // result register contain a valid pointer because it is already
4612 // contained in the register pointer map.
4613 Register reg = ToRegister(instr->result());
4614 __ Move(reg, Immediate(0));
4616 PushSafepointRegistersScope scope(this);
4617 // NumberTagI and NumberTagD use the context from the frame, rather than
4618 // the environment's HContext or HInlinedContext value.
4619 // They only call Runtime::kAllocateHeapNumber.
4620 // The corresponding HChange instructions are added in a phase that does
4621 // not have easy access to the local context.
4622 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
4623 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4624 RecordSafepointWithRegisters(
4625 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4626 __ StoreToSafepointRegisterSlot(reg, eax);
4630 void LCodeGen::DoSmiTag(LSmiTag* instr) {
4631 HChange* hchange = instr->hydrogen();
4632 Register input = ToRegister(instr->value());
4633 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4634 hchange->value()->CheckFlag(HValue::kUint32)) {
4635 __ test(input, Immediate(0xc0000000));
4636 DeoptimizeIf(not_zero, instr, "overflow");
4639 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4640 !hchange->value()->CheckFlag(HValue::kUint32)) {
4641 DeoptimizeIf(overflow, instr, "overflow");
4646 void LCodeGen::DoSmiUntag(LSmiUntag* instr) {
4647 LOperand* input = instr->value();
4648 Register result = ToRegister(input);
4649 DCHECK(input->IsRegister() && input->Equals(instr->result()));
4650 if (instr->needs_check()) {
4651 __ test(result, Immediate(kSmiTagMask));
4652 DeoptimizeIf(not_zero, instr, "not a Smi");
4654 __ AssertSmi(result);
4656 __ SmiUntag(result);
4660 void LCodeGen::EmitNumberUntagD(LNumberUntagD* instr, Register input_reg,
4661 Register temp_reg, XMMRegister result_reg,
4662 NumberUntagDMode mode) {
4663 bool can_convert_undefined_to_nan =
4664 instr->hydrogen()->can_convert_undefined_to_nan();
4665 bool deoptimize_on_minus_zero = instr->hydrogen()->deoptimize_on_minus_zero();
4667 Label convert, load_smi, done;
4669 if (mode == NUMBER_CANDIDATE_IS_ANY_TAGGED) {
4671 __ JumpIfSmi(input_reg, &load_smi, Label::kNear);
4673 // Heap number map check.
4674 __ cmp(FieldOperand(input_reg, HeapObject::kMapOffset),
4675 factory()->heap_number_map());
4676 if (can_convert_undefined_to_nan) {
4677 __ j(not_equal, &convert, Label::kNear);
4679 DeoptimizeIf(not_equal, instr, "not a heap number");
4682 // Heap number to XMM conversion.
4683 __ movsd(result_reg, FieldOperand(input_reg, HeapNumber::kValueOffset));
4685 if (deoptimize_on_minus_zero) {
4686 XMMRegister xmm_scratch = double_scratch0();
4687 __ xorps(xmm_scratch, xmm_scratch);
4688 __ ucomisd(result_reg, xmm_scratch);
4689 __ j(not_zero, &done, Label::kNear);
4690 __ movmskpd(temp_reg, result_reg);
4691 __ test_b(temp_reg, 1);
4692 DeoptimizeIf(not_zero, instr, "minus zero");
4694 __ jmp(&done, Label::kNear);
4696 if (can_convert_undefined_to_nan) {
4699 // Convert undefined (and hole) to NaN.
4700 __ cmp(input_reg, factory()->undefined_value());
4701 DeoptimizeIf(not_equal, instr, "not a heap number/undefined");
4703 ExternalReference nan =
4704 ExternalReference::address_of_canonical_non_hole_nan();
4705 __ movsd(result_reg, Operand::StaticVariable(nan));
4706 __ jmp(&done, Label::kNear);
4709 DCHECK(mode == NUMBER_CANDIDATE_IS_SMI);
4713 // Smi to XMM conversion. Clobbering a temp is faster than re-tagging the
4714 // input register since we avoid dependencies.
4715 __ mov(temp_reg, input_reg);
4716 __ SmiUntag(temp_reg); // Untag smi before converting to float.
4717 __ Cvtsi2sd(result_reg, Operand(temp_reg));
4722 void LCodeGen::DoDeferredTaggedToI(LTaggedToI* instr, Label* done) {
4723 Register input_reg = ToRegister(instr->value());
4725 // The input was optimistically untagged; revert it.
4726 STATIC_ASSERT(kSmiTagSize == 1);
4727 __ lea(input_reg, Operand(input_reg, times_2, kHeapObjectTag));
4729 if (instr->truncating()) {
4730 Label no_heap_number, check_bools, check_false;
4732 // Heap number map check.
4733 __ cmp(FieldOperand(input_reg, HeapObject::kMapOffset),
4734 factory()->heap_number_map());
4735 __ j(not_equal, &no_heap_number, Label::kNear);
4736 __ TruncateHeapNumberToI(input_reg, input_reg);
4739 __ bind(&no_heap_number);
4740 // Check for Oddballs. Undefined/False is converted to zero and True to one
4741 // for truncating conversions.
4742 __ cmp(input_reg, factory()->undefined_value());
4743 __ j(not_equal, &check_bools, Label::kNear);
4744 __ Move(input_reg, Immediate(0));
4747 __ bind(&check_bools);
4748 __ cmp(input_reg, factory()->true_value());
4749 __ j(not_equal, &check_false, Label::kNear);
4750 __ Move(input_reg, Immediate(1));
4753 __ bind(&check_false);
4754 __ cmp(input_reg, factory()->false_value());
4755 DeoptimizeIf(not_equal, instr, "not a heap number/undefined/true/false");
4756 __ Move(input_reg, Immediate(0));
4758 XMMRegister scratch = ToDoubleRegister(instr->temp());
4759 DCHECK(!scratch.is(xmm0));
4760 __ cmp(FieldOperand(input_reg, HeapObject::kMapOffset),
4761 isolate()->factory()->heap_number_map());
4762 DeoptimizeIf(not_equal, instr, "not a heap number");
4763 __ movsd(xmm0, FieldOperand(input_reg, HeapNumber::kValueOffset));
4764 __ cvttsd2si(input_reg, Operand(xmm0));
4765 __ Cvtsi2sd(scratch, Operand(input_reg));
4766 __ ucomisd(xmm0, scratch);
4767 DeoptimizeIf(not_equal, instr, "lost precision");
4768 DeoptimizeIf(parity_even, instr, "NaN");
4769 if (instr->hydrogen()->GetMinusZeroMode() == FAIL_ON_MINUS_ZERO) {
4770 __ test(input_reg, Operand(input_reg));
4771 __ j(not_zero, done);
4772 __ movmskpd(input_reg, xmm0);
4773 __ and_(input_reg, 1);
4774 DeoptimizeIf(not_zero, instr, "minus zero");
4780 void LCodeGen::DoTaggedToI(LTaggedToI* instr) {
4781 class DeferredTaggedToI FINAL : public LDeferredCode {
4783 DeferredTaggedToI(LCodeGen* codegen, LTaggedToI* instr)
4784 : LDeferredCode(codegen), instr_(instr) { }
4785 virtual void Generate() OVERRIDE {
4786 codegen()->DoDeferredTaggedToI(instr_, done());
4788 virtual LInstruction* instr() OVERRIDE { return instr_; }
4793 LOperand* input = instr->value();
4794 DCHECK(input->IsRegister());
4795 Register input_reg = ToRegister(input);
4796 DCHECK(input_reg.is(ToRegister(instr->result())));
4798 if (instr->hydrogen()->value()->representation().IsSmi()) {
4799 __ SmiUntag(input_reg);
4801 DeferredTaggedToI* deferred =
4802 new(zone()) DeferredTaggedToI(this, instr);
4803 // Optimistically untag the input.
4804 // If the input is a HeapObject, SmiUntag will set the carry flag.
4805 STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
4806 __ SmiUntag(input_reg);
4807 // Branch to deferred code if the input was tagged.
4808 // The deferred code will take care of restoring the tag.
4809 __ j(carry, deferred->entry());
4810 __ bind(deferred->exit());
4815 void LCodeGen::DoNumberUntagD(LNumberUntagD* instr) {
4816 LOperand* input = instr->value();
4817 DCHECK(input->IsRegister());
4818 LOperand* temp = instr->temp();
4819 DCHECK(temp->IsRegister());
4820 LOperand* result = instr->result();
4821 DCHECK(result->IsDoubleRegister());
4823 Register input_reg = ToRegister(input);
4824 Register temp_reg = ToRegister(temp);
4826 HValue* value = instr->hydrogen()->value();
4827 NumberUntagDMode mode = value->representation().IsSmi()
4828 ? NUMBER_CANDIDATE_IS_SMI : NUMBER_CANDIDATE_IS_ANY_TAGGED;
4830 XMMRegister result_reg = ToDoubleRegister(result);
4831 EmitNumberUntagD(instr, input_reg, temp_reg, result_reg, mode);
4835 void LCodeGen::DoDoubleToI(LDoubleToI* instr) {
4836 LOperand* input = instr->value();
4837 DCHECK(input->IsDoubleRegister());
4838 LOperand* result = instr->result();
4839 DCHECK(result->IsRegister());
4840 Register result_reg = ToRegister(result);
4842 if (instr->truncating()) {
4843 XMMRegister input_reg = ToDoubleRegister(input);
4844 __ TruncateDoubleToI(result_reg, input_reg);
4846 Label lost_precision, is_nan, minus_zero, done;
4847 XMMRegister input_reg = ToDoubleRegister(input);
4848 XMMRegister xmm_scratch = double_scratch0();
4849 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
4850 __ DoubleToI(result_reg, input_reg, xmm_scratch,
4851 instr->hydrogen()->GetMinusZeroMode(), &lost_precision,
4852 &is_nan, &minus_zero, dist);
4853 __ jmp(&done, dist);
4854 __ bind(&lost_precision);
4855 DeoptimizeIf(no_condition, instr, "lost precision");
4857 DeoptimizeIf(no_condition, instr, "NaN");
4858 __ bind(&minus_zero);
4859 DeoptimizeIf(no_condition, instr, "minus zero");
4865 void LCodeGen::DoDoubleToSmi(LDoubleToSmi* instr) {
4866 LOperand* input = instr->value();
4867 DCHECK(input->IsDoubleRegister());
4868 LOperand* result = instr->result();
4869 DCHECK(result->IsRegister());
4870 Register result_reg = ToRegister(result);
4872 Label lost_precision, is_nan, minus_zero, done;
4873 XMMRegister input_reg = ToDoubleRegister(input);
4874 XMMRegister xmm_scratch = double_scratch0();
4875 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
4876 __ DoubleToI(result_reg, input_reg, xmm_scratch,
4877 instr->hydrogen()->GetMinusZeroMode(), &lost_precision, &is_nan,
4879 __ jmp(&done, dist);
4880 __ bind(&lost_precision);
4881 DeoptimizeIf(no_condition, instr, "lost precision");
4883 DeoptimizeIf(no_condition, instr, "NaN");
4884 __ bind(&minus_zero);
4885 DeoptimizeIf(no_condition, instr, "minus zero");
4887 __ SmiTag(result_reg);
4888 DeoptimizeIf(overflow, instr, "overflow");
4892 void LCodeGen::DoCheckSmi(LCheckSmi* instr) {
4893 LOperand* input = instr->value();
4894 __ test(ToOperand(input), Immediate(kSmiTagMask));
4895 DeoptimizeIf(not_zero, instr, "not a Smi");
4899 void LCodeGen::DoCheckNonSmi(LCheckNonSmi* instr) {
4900 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
4901 LOperand* input = instr->value();
4902 __ test(ToOperand(input), Immediate(kSmiTagMask));
4903 DeoptimizeIf(zero, instr, "Smi");
4908 void LCodeGen::DoCheckInstanceType(LCheckInstanceType* instr) {
4909 Register input = ToRegister(instr->value());
4910 Register temp = ToRegister(instr->temp());
4912 __ mov(temp, FieldOperand(input, HeapObject::kMapOffset));
4914 if (instr->hydrogen()->is_interval_check()) {
4917 instr->hydrogen()->GetCheckInterval(&first, &last);
4919 __ cmpb(FieldOperand(temp, Map::kInstanceTypeOffset),
4920 static_cast<int8_t>(first));
4922 // If there is only one type in the interval check for equality.
4923 if (first == last) {
4924 DeoptimizeIf(not_equal, instr, "wrong instance type");
4926 DeoptimizeIf(below, instr, "wrong instance type");
4927 // Omit check for the last type.
4928 if (last != LAST_TYPE) {
4929 __ cmpb(FieldOperand(temp, Map::kInstanceTypeOffset),
4930 static_cast<int8_t>(last));
4931 DeoptimizeIf(above, instr, "wrong instance type");
4937 instr->hydrogen()->GetCheckMaskAndTag(&mask, &tag);
4939 if (base::bits::IsPowerOfTwo32(mask)) {
4940 DCHECK(tag == 0 || base::bits::IsPowerOfTwo32(tag));
4941 __ test_b(FieldOperand(temp, Map::kInstanceTypeOffset), mask);
4942 DeoptimizeIf(tag == 0 ? not_zero : zero, instr, "wrong instance type");
4944 __ movzx_b(temp, FieldOperand(temp, Map::kInstanceTypeOffset));
4945 __ and_(temp, mask);
4947 DeoptimizeIf(not_equal, instr, "wrong instance type");
4953 void LCodeGen::DoCheckValue(LCheckValue* instr) {
4954 Handle<HeapObject> object = instr->hydrogen()->object().handle();
4955 if (instr->hydrogen()->object_in_new_space()) {
4956 Register reg = ToRegister(instr->value());
4957 Handle<Cell> cell = isolate()->factory()->NewCell(object);
4958 __ cmp(reg, Operand::ForCell(cell));
4960 Operand operand = ToOperand(instr->value());
4961 __ cmp(operand, object);
4963 DeoptimizeIf(not_equal, instr, "value mismatch");
4967 void LCodeGen::DoDeferredInstanceMigration(LCheckMaps* instr, Register object) {
4969 PushSafepointRegistersScope scope(this);
4972 __ CallRuntimeSaveDoubles(Runtime::kTryMigrateInstance);
4973 RecordSafepointWithRegisters(
4974 instr->pointer_map(), 1, Safepoint::kNoLazyDeopt);
4976 __ test(eax, Immediate(kSmiTagMask));
4978 DeoptimizeIf(zero, instr, "instance migration failed");
4982 void LCodeGen::DoCheckMaps(LCheckMaps* instr) {
4983 class DeferredCheckMaps FINAL : public LDeferredCode {
4985 DeferredCheckMaps(LCodeGen* codegen, LCheckMaps* instr, Register object)
4986 : LDeferredCode(codegen), instr_(instr), object_(object) {
4987 SetExit(check_maps());
4989 virtual void Generate() OVERRIDE {
4990 codegen()->DoDeferredInstanceMigration(instr_, object_);
4992 Label* check_maps() { return &check_maps_; }
4993 virtual LInstruction* instr() OVERRIDE { return instr_; }
5000 if (instr->hydrogen()->IsStabilityCheck()) {
5001 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5002 for (int i = 0; i < maps->size(); ++i) {
5003 AddStabilityDependency(maps->at(i).handle());
5008 LOperand* input = instr->value();
5009 DCHECK(input->IsRegister());
5010 Register reg = ToRegister(input);
5012 DeferredCheckMaps* deferred = NULL;
5013 if (instr->hydrogen()->HasMigrationTarget()) {
5014 deferred = new(zone()) DeferredCheckMaps(this, instr, reg);
5015 __ bind(deferred->check_maps());
5018 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5020 for (int i = 0; i < maps->size() - 1; i++) {
5021 Handle<Map> map = maps->at(i).handle();
5022 __ CompareMap(reg, map);
5023 __ j(equal, &success, Label::kNear);
5026 Handle<Map> map = maps->at(maps->size() - 1).handle();
5027 __ CompareMap(reg, map);
5028 if (instr->hydrogen()->HasMigrationTarget()) {
5029 __ j(not_equal, deferred->entry());
5031 DeoptimizeIf(not_equal, instr, "wrong map");
5038 void LCodeGen::DoClampDToUint8(LClampDToUint8* instr) {
5039 XMMRegister value_reg = ToDoubleRegister(instr->unclamped());
5040 XMMRegister xmm_scratch = double_scratch0();
5041 Register result_reg = ToRegister(instr->result());
5042 __ ClampDoubleToUint8(value_reg, xmm_scratch, result_reg);
5046 void LCodeGen::DoClampIToUint8(LClampIToUint8* instr) {
5047 DCHECK(instr->unclamped()->Equals(instr->result()));
5048 Register value_reg = ToRegister(instr->result());
5049 __ ClampUint8(value_reg);
5053 void LCodeGen::DoClampTToUint8(LClampTToUint8* instr) {
5054 DCHECK(instr->unclamped()->Equals(instr->result()));
5055 Register input_reg = ToRegister(instr->unclamped());
5056 XMMRegister temp_xmm_reg = ToDoubleRegister(instr->temp_xmm());
5057 XMMRegister xmm_scratch = double_scratch0();
5058 Label is_smi, done, heap_number;
5060 __ JumpIfSmi(input_reg, &is_smi);
5062 // Check for heap number
5063 __ cmp(FieldOperand(input_reg, HeapObject::kMapOffset),
5064 factory()->heap_number_map());
5065 __ j(equal, &heap_number, Label::kNear);
5067 // Check for undefined. Undefined is converted to zero for clamping
5069 __ cmp(input_reg, factory()->undefined_value());
5070 DeoptimizeIf(not_equal, instr, "not a heap number/undefined");
5071 __ mov(input_reg, 0);
5072 __ jmp(&done, Label::kNear);
5075 __ bind(&heap_number);
5076 __ movsd(xmm_scratch, FieldOperand(input_reg, HeapNumber::kValueOffset));
5077 __ ClampDoubleToUint8(xmm_scratch, temp_xmm_reg, input_reg);
5078 __ jmp(&done, Label::kNear);
5082 __ SmiUntag(input_reg);
5083 __ ClampUint8(input_reg);
5088 void LCodeGen::DoDoubleBits(LDoubleBits* instr) {
5089 XMMRegister value_reg = ToDoubleRegister(instr->value());
5090 Register result_reg = ToRegister(instr->result());
5091 if (instr->hydrogen()->bits() == HDoubleBits::HIGH) {
5092 if (CpuFeatures::IsSupported(SSE4_1)) {
5093 CpuFeatureScope scope2(masm(), SSE4_1);
5094 __ pextrd(result_reg, value_reg, 1);
5096 XMMRegister xmm_scratch = double_scratch0();
5097 __ pshufd(xmm_scratch, value_reg, 1);
5098 __ movd(result_reg, xmm_scratch);
5101 __ movd(result_reg, value_reg);
5106 void LCodeGen::DoConstructDouble(LConstructDouble* instr) {
5107 Register hi_reg = ToRegister(instr->hi());
5108 Register lo_reg = ToRegister(instr->lo());
5109 XMMRegister result_reg = ToDoubleRegister(instr->result());
5111 if (CpuFeatures::IsSupported(SSE4_1)) {
5112 CpuFeatureScope scope2(masm(), SSE4_1);
5113 __ movd(result_reg, lo_reg);
5114 __ pinsrd(result_reg, hi_reg, 1);
5116 XMMRegister xmm_scratch = double_scratch0();
5117 __ movd(result_reg, hi_reg);
5118 __ psllq(result_reg, 32);
5119 __ movd(xmm_scratch, lo_reg);
5120 __ orps(result_reg, xmm_scratch);
5125 void LCodeGen::DoAllocate(LAllocate* instr) {
5126 class DeferredAllocate FINAL : public LDeferredCode {
5128 DeferredAllocate(LCodeGen* codegen, LAllocate* instr)
5129 : LDeferredCode(codegen), instr_(instr) { }
5130 virtual void Generate() OVERRIDE {
5131 codegen()->DoDeferredAllocate(instr_);
5133 virtual LInstruction* instr() OVERRIDE { return instr_; }
5138 DeferredAllocate* deferred = new(zone()) DeferredAllocate(this, instr);
5140 Register result = ToRegister(instr->result());
5141 Register temp = ToRegister(instr->temp());
5143 // Allocate memory for the object.
5144 AllocationFlags flags = TAG_OBJECT;
5145 if (instr->hydrogen()->MustAllocateDoubleAligned()) {
5146 flags = static_cast<AllocationFlags>(flags | DOUBLE_ALIGNMENT);
5148 if (instr->hydrogen()->IsOldPointerSpaceAllocation()) {
5149 DCHECK(!instr->hydrogen()->IsOldDataSpaceAllocation());
5150 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5151 flags = static_cast<AllocationFlags>(flags | PRETENURE_OLD_POINTER_SPACE);
5152 } else if (instr->hydrogen()->IsOldDataSpaceAllocation()) {
5153 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5154 flags = static_cast<AllocationFlags>(flags | PRETENURE_OLD_DATA_SPACE);
5157 if (instr->size()->IsConstantOperand()) {
5158 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5159 if (size <= Page::kMaxRegularHeapObjectSize) {
5160 __ Allocate(size, result, temp, no_reg, deferred->entry(), flags);
5162 __ jmp(deferred->entry());
5165 Register size = ToRegister(instr->size());
5166 __ Allocate(size, result, temp, no_reg, deferred->entry(), flags);
5169 __ bind(deferred->exit());
5171 if (instr->hydrogen()->MustPrefillWithFiller()) {
5172 if (instr->size()->IsConstantOperand()) {
5173 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5174 __ mov(temp, (size / kPointerSize) - 1);
5176 temp = ToRegister(instr->size());
5177 __ shr(temp, kPointerSizeLog2);
5182 __ mov(FieldOperand(result, temp, times_pointer_size, 0),
5183 isolate()->factory()->one_pointer_filler_map());
5185 __ j(not_zero, &loop);
5190 void LCodeGen::DoDeferredAllocate(LAllocate* instr) {
5191 Register result = ToRegister(instr->result());
5193 // TODO(3095996): Get rid of this. For now, we need to make the
5194 // result register contain a valid pointer because it is already
5195 // contained in the register pointer map.
5196 __ Move(result, Immediate(Smi::FromInt(0)));
5198 PushSafepointRegistersScope scope(this);
5199 if (instr->size()->IsRegister()) {
5200 Register size = ToRegister(instr->size());
5201 DCHECK(!size.is(result));
5202 __ SmiTag(ToRegister(instr->size()));
5205 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5206 if (size >= 0 && size <= Smi::kMaxValue) {
5207 __ push(Immediate(Smi::FromInt(size)));
5209 // We should never get here at runtime => abort
5215 int flags = AllocateDoubleAlignFlag::encode(
5216 instr->hydrogen()->MustAllocateDoubleAligned());
5217 if (instr->hydrogen()->IsOldPointerSpaceAllocation()) {
5218 DCHECK(!instr->hydrogen()->IsOldDataSpaceAllocation());
5219 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5220 flags = AllocateTargetSpace::update(flags, OLD_POINTER_SPACE);
5221 } else if (instr->hydrogen()->IsOldDataSpaceAllocation()) {
5222 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5223 flags = AllocateTargetSpace::update(flags, OLD_DATA_SPACE);
5225 flags = AllocateTargetSpace::update(flags, NEW_SPACE);
5227 __ push(Immediate(Smi::FromInt(flags)));
5229 CallRuntimeFromDeferred(
5230 Runtime::kAllocateInTargetSpace, 2, instr, instr->context());
5231 __ StoreToSafepointRegisterSlot(result, eax);
5235 void LCodeGen::DoToFastProperties(LToFastProperties* instr) {
5236 DCHECK(ToRegister(instr->value()).is(eax));
5238 CallRuntime(Runtime::kToFastProperties, 1, instr);
5242 void LCodeGen::DoRegExpLiteral(LRegExpLiteral* instr) {
5243 DCHECK(ToRegister(instr->context()).is(esi));
5245 // Registers will be used as follows:
5246 // ecx = literals array.
5247 // ebx = regexp literal.
5248 // eax = regexp literal clone.
5250 int literal_offset =
5251 FixedArray::OffsetOfElementAt(instr->hydrogen()->literal_index());
5252 __ LoadHeapObject(ecx, instr->hydrogen()->literals());
5253 __ mov(ebx, FieldOperand(ecx, literal_offset));
5254 __ cmp(ebx, factory()->undefined_value());
5255 __ j(not_equal, &materialized, Label::kNear);
5257 // Create regexp literal using runtime function
5258 // Result will be in eax.
5260 __ push(Immediate(Smi::FromInt(instr->hydrogen()->literal_index())));
5261 __ push(Immediate(instr->hydrogen()->pattern()));
5262 __ push(Immediate(instr->hydrogen()->flags()));
5263 CallRuntime(Runtime::kMaterializeRegExpLiteral, 4, instr);
5266 __ bind(&materialized);
5267 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
5268 Label allocated, runtime_allocate;
5269 __ Allocate(size, eax, ecx, edx, &runtime_allocate, TAG_OBJECT);
5270 __ jmp(&allocated, Label::kNear);
5272 __ bind(&runtime_allocate);
5274 __ push(Immediate(Smi::FromInt(size)));
5275 CallRuntime(Runtime::kAllocateInNewSpace, 1, instr);
5278 __ bind(&allocated);
5279 // Copy the content into the newly allocated memory.
5280 // (Unroll copy loop once for better throughput).
5281 for (int i = 0; i < size - kPointerSize; i += 2 * kPointerSize) {
5282 __ mov(edx, FieldOperand(ebx, i));
5283 __ mov(ecx, FieldOperand(ebx, i + kPointerSize));
5284 __ mov(FieldOperand(eax, i), edx);
5285 __ mov(FieldOperand(eax, i + kPointerSize), ecx);
5287 if ((size % (2 * kPointerSize)) != 0) {
5288 __ mov(edx, FieldOperand(ebx, size - kPointerSize));
5289 __ mov(FieldOperand(eax, size - kPointerSize), edx);
5294 void LCodeGen::DoFunctionLiteral(LFunctionLiteral* instr) {
5295 DCHECK(ToRegister(instr->context()).is(esi));
5296 // Use the fast case closure allocation code that allocates in new
5297 // space for nested functions that don't need literals cloning.
5298 bool pretenure = instr->hydrogen()->pretenure();
5299 if (!pretenure && instr->hydrogen()->has_no_literals()) {
5300 FastNewClosureStub stub(isolate(), instr->hydrogen()->strict_mode(),
5301 instr->hydrogen()->kind());
5302 __ mov(ebx, Immediate(instr->hydrogen()->shared_info()));
5303 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
5306 __ push(Immediate(instr->hydrogen()->shared_info()));
5307 __ push(Immediate(pretenure ? factory()->true_value()
5308 : factory()->false_value()));
5309 CallRuntime(Runtime::kNewClosure, 3, instr);
5314 void LCodeGen::DoTypeof(LTypeof* instr) {
5315 DCHECK(ToRegister(instr->context()).is(esi));
5316 LOperand* input = instr->value();
5317 EmitPushTaggedOperand(input);
5318 CallRuntime(Runtime::kTypeof, 1, instr);
5322 void LCodeGen::DoTypeofIsAndBranch(LTypeofIsAndBranch* instr) {
5323 Register input = ToRegister(instr->value());
5324 Condition final_branch_condition = EmitTypeofIs(instr, input);
5325 if (final_branch_condition != no_condition) {
5326 EmitBranch(instr, final_branch_condition);
5331 Condition LCodeGen::EmitTypeofIs(LTypeofIsAndBranch* instr, Register input) {
5332 Label* true_label = instr->TrueLabel(chunk_);
5333 Label* false_label = instr->FalseLabel(chunk_);
5334 Handle<String> type_name = instr->type_literal();
5335 int left_block = instr->TrueDestination(chunk_);
5336 int right_block = instr->FalseDestination(chunk_);
5337 int next_block = GetNextEmittedBlock();
5339 Label::Distance true_distance = left_block == next_block ? Label::kNear
5341 Label::Distance false_distance = right_block == next_block ? Label::kNear
5343 Condition final_branch_condition = no_condition;
5344 if (String::Equals(type_name, factory()->number_string())) {
5345 __ JumpIfSmi(input, true_label, true_distance);
5346 __ cmp(FieldOperand(input, HeapObject::kMapOffset),
5347 factory()->heap_number_map());
5348 final_branch_condition = equal;
5350 } else if (String::Equals(type_name, factory()->string_string())) {
5351 __ JumpIfSmi(input, false_label, false_distance);
5352 __ CmpObjectType(input, FIRST_NONSTRING_TYPE, input);
5353 __ j(above_equal, false_label, false_distance);
5354 __ test_b(FieldOperand(input, Map::kBitFieldOffset),
5355 1 << Map::kIsUndetectable);
5356 final_branch_condition = zero;
5358 } else if (String::Equals(type_name, factory()->symbol_string())) {
5359 __ JumpIfSmi(input, false_label, false_distance);
5360 __ CmpObjectType(input, SYMBOL_TYPE, input);
5361 final_branch_condition = equal;
5363 } else if (String::Equals(type_name, factory()->boolean_string())) {
5364 __ cmp(input, factory()->true_value());
5365 __ j(equal, true_label, true_distance);
5366 __ cmp(input, factory()->false_value());
5367 final_branch_condition = equal;
5369 } else if (String::Equals(type_name, factory()->undefined_string())) {
5370 __ cmp(input, factory()->undefined_value());
5371 __ j(equal, true_label, true_distance);
5372 __ JumpIfSmi(input, false_label, false_distance);
5373 // Check for undetectable objects => true.
5374 __ mov(input, FieldOperand(input, HeapObject::kMapOffset));
5375 __ test_b(FieldOperand(input, Map::kBitFieldOffset),
5376 1 << Map::kIsUndetectable);
5377 final_branch_condition = not_zero;
5379 } else if (String::Equals(type_name, factory()->function_string())) {
5380 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
5381 __ JumpIfSmi(input, false_label, false_distance);
5382 __ CmpObjectType(input, JS_FUNCTION_TYPE, input);
5383 __ j(equal, true_label, true_distance);
5384 __ CmpInstanceType(input, JS_FUNCTION_PROXY_TYPE);
5385 final_branch_condition = equal;
5387 } else if (String::Equals(type_name, factory()->object_string())) {
5388 __ JumpIfSmi(input, false_label, false_distance);
5389 __ cmp(input, factory()->null_value());
5390 __ j(equal, true_label, true_distance);
5391 __ CmpObjectType(input, FIRST_NONCALLABLE_SPEC_OBJECT_TYPE, input);
5392 __ j(below, false_label, false_distance);
5393 __ CmpInstanceType(input, LAST_NONCALLABLE_SPEC_OBJECT_TYPE);
5394 __ j(above, false_label, false_distance);
5395 // Check for undetectable objects => false.
5396 __ test_b(FieldOperand(input, Map::kBitFieldOffset),
5397 1 << Map::kIsUndetectable);
5398 final_branch_condition = zero;
5401 __ jmp(false_label, false_distance);
5403 return final_branch_condition;
5407 void LCodeGen::DoIsConstructCallAndBranch(LIsConstructCallAndBranch* instr) {
5408 Register temp = ToRegister(instr->temp());
5410 EmitIsConstructCall(temp);
5411 EmitBranch(instr, equal);
5415 void LCodeGen::EmitIsConstructCall(Register temp) {
5416 // Get the frame pointer for the calling frame.
5417 __ mov(temp, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
5419 // Skip the arguments adaptor frame if it exists.
5420 Label check_frame_marker;
5421 __ cmp(Operand(temp, StandardFrameConstants::kContextOffset),
5422 Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
5423 __ j(not_equal, &check_frame_marker, Label::kNear);
5424 __ mov(temp, Operand(temp, StandardFrameConstants::kCallerFPOffset));
5426 // Check the marker in the calling frame.
5427 __ bind(&check_frame_marker);
5428 __ cmp(Operand(temp, StandardFrameConstants::kMarkerOffset),
5429 Immediate(Smi::FromInt(StackFrame::CONSTRUCT)));
5433 void LCodeGen::EnsureSpaceForLazyDeopt(int space_needed) {
5434 if (!info()->IsStub()) {
5435 // Ensure that we have enough space after the previous lazy-bailout
5436 // instruction for patching the code here.
5437 int current_pc = masm()->pc_offset();
5438 if (current_pc < last_lazy_deopt_pc_ + space_needed) {
5439 int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
5440 __ Nop(padding_size);
5443 last_lazy_deopt_pc_ = masm()->pc_offset();
5447 void LCodeGen::DoLazyBailout(LLazyBailout* instr) {
5448 last_lazy_deopt_pc_ = masm()->pc_offset();
5449 DCHECK(instr->HasEnvironment());
5450 LEnvironment* env = instr->environment();
5451 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5452 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5456 void LCodeGen::DoDeoptimize(LDeoptimize* instr) {
5457 Deoptimizer::BailoutType type = instr->hydrogen()->type();
5458 // TODO(danno): Stubs expect all deopts to be lazy for historical reasons (the
5459 // needed return address), even though the implementation of LAZY and EAGER is
5460 // now identical. When LAZY is eventually completely folded into EAGER, remove
5461 // the special case below.
5462 if (info()->IsStub() && type == Deoptimizer::EAGER) {
5463 type = Deoptimizer::LAZY;
5465 DeoptimizeIf(no_condition, instr, instr->hydrogen()->reason(), type);
5469 void LCodeGen::DoDummy(LDummy* instr) {
5470 // Nothing to see here, move on!
5474 void LCodeGen::DoDummyUse(LDummyUse* instr) {
5475 // Nothing to see here, move on!
5479 void LCodeGen::DoDeferredStackCheck(LStackCheck* instr) {
5480 PushSafepointRegistersScope scope(this);
5481 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
5482 __ CallRuntimeSaveDoubles(Runtime::kStackGuard);
5483 RecordSafepointWithLazyDeopt(
5484 instr, RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
5485 DCHECK(instr->HasEnvironment());
5486 LEnvironment* env = instr->environment();
5487 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5491 void LCodeGen::DoStackCheck(LStackCheck* instr) {
5492 class DeferredStackCheck FINAL : public LDeferredCode {
5494 DeferredStackCheck(LCodeGen* codegen, LStackCheck* instr)
5495 : LDeferredCode(codegen), instr_(instr) { }
5496 virtual void Generate() OVERRIDE {
5497 codegen()->DoDeferredStackCheck(instr_);
5499 virtual LInstruction* instr() OVERRIDE { return instr_; }
5501 LStackCheck* instr_;
5504 DCHECK(instr->HasEnvironment());
5505 LEnvironment* env = instr->environment();
5506 // There is no LLazyBailout instruction for stack-checks. We have to
5507 // prepare for lazy deoptimization explicitly here.
5508 if (instr->hydrogen()->is_function_entry()) {
5509 // Perform stack overflow check.
5511 ExternalReference stack_limit =
5512 ExternalReference::address_of_stack_limit(isolate());
5513 __ cmp(esp, Operand::StaticVariable(stack_limit));
5514 __ j(above_equal, &done, Label::kNear);
5516 DCHECK(instr->context()->IsRegister());
5517 DCHECK(ToRegister(instr->context()).is(esi));
5518 CallCode(isolate()->builtins()->StackCheck(),
5519 RelocInfo::CODE_TARGET,
5523 DCHECK(instr->hydrogen()->is_backwards_branch());
5524 // Perform stack overflow check if this goto needs it before jumping.
5525 DeferredStackCheck* deferred_stack_check =
5526 new(zone()) DeferredStackCheck(this, instr);
5527 ExternalReference stack_limit =
5528 ExternalReference::address_of_stack_limit(isolate());
5529 __ cmp(esp, Operand::StaticVariable(stack_limit));
5530 __ j(below, deferred_stack_check->entry());
5531 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
5532 __ bind(instr->done_label());
5533 deferred_stack_check->SetExit(instr->done_label());
5534 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5535 // Don't record a deoptimization index for the safepoint here.
5536 // This will be done explicitly when emitting call and the safepoint in
5537 // the deferred code.
5542 void LCodeGen::DoOsrEntry(LOsrEntry* instr) {
5543 // This is a pseudo-instruction that ensures that the environment here is
5544 // properly registered for deoptimization and records the assembler's PC
5546 LEnvironment* environment = instr->environment();
5548 // If the environment were already registered, we would have no way of
5549 // backpatching it with the spill slot operands.
5550 DCHECK(!environment->HasBeenRegistered());
5551 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
5553 GenerateOsrPrologue();
5557 void LCodeGen::DoForInPrepareMap(LForInPrepareMap* instr) {
5558 DCHECK(ToRegister(instr->context()).is(esi));
5559 __ cmp(eax, isolate()->factory()->undefined_value());
5560 DeoptimizeIf(equal, instr, "undefined");
5562 __ cmp(eax, isolate()->factory()->null_value());
5563 DeoptimizeIf(equal, instr, "null");
5565 __ test(eax, Immediate(kSmiTagMask));
5566 DeoptimizeIf(zero, instr, "Smi");
5568 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
5569 __ CmpObjectType(eax, LAST_JS_PROXY_TYPE, ecx);
5570 DeoptimizeIf(below_equal, instr, "wrong instance type");
5572 Label use_cache, call_runtime;
5573 __ CheckEnumCache(&call_runtime);
5575 __ mov(eax, FieldOperand(eax, HeapObject::kMapOffset));
5576 __ jmp(&use_cache, Label::kNear);
5578 // Get the set of properties to enumerate.
5579 __ bind(&call_runtime);
5581 CallRuntime(Runtime::kGetPropertyNamesFast, 1, instr);
5583 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
5584 isolate()->factory()->meta_map());
5585 DeoptimizeIf(not_equal, instr, "wrong map");
5586 __ bind(&use_cache);
5590 void LCodeGen::DoForInCacheArray(LForInCacheArray* instr) {
5591 Register map = ToRegister(instr->map());
5592 Register result = ToRegister(instr->result());
5593 Label load_cache, done;
5594 __ EnumLength(result, map);
5595 __ cmp(result, Immediate(Smi::FromInt(0)));
5596 __ j(not_equal, &load_cache, Label::kNear);
5597 __ mov(result, isolate()->factory()->empty_fixed_array());
5598 __ jmp(&done, Label::kNear);
5600 __ bind(&load_cache);
5601 __ LoadInstanceDescriptors(map, result);
5603 FieldOperand(result, DescriptorArray::kEnumCacheOffset));
5605 FieldOperand(result, FixedArray::SizeFor(instr->idx())));
5607 __ test(result, result);
5608 DeoptimizeIf(equal, instr, "no cache");
5612 void LCodeGen::DoCheckMapValue(LCheckMapValue* instr) {
5613 Register object = ToRegister(instr->value());
5614 __ cmp(ToRegister(instr->map()),
5615 FieldOperand(object, HeapObject::kMapOffset));
5616 DeoptimizeIf(not_equal, instr, "wrong map");
5620 void LCodeGen::DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
5623 PushSafepointRegistersScope scope(this);
5627 __ CallRuntimeSaveDoubles(Runtime::kLoadMutableDouble);
5628 RecordSafepointWithRegisters(
5629 instr->pointer_map(), 2, Safepoint::kNoLazyDeopt);
5630 __ StoreToSafepointRegisterSlot(object, eax);
5634 void LCodeGen::DoLoadFieldByIndex(LLoadFieldByIndex* instr) {
5635 class DeferredLoadMutableDouble FINAL : public LDeferredCode {
5637 DeferredLoadMutableDouble(LCodeGen* codegen,
5638 LLoadFieldByIndex* instr,
5641 : LDeferredCode(codegen),
5646 virtual void Generate() OVERRIDE {
5647 codegen()->DoDeferredLoadMutableDouble(instr_, object_, index_);
5649 virtual LInstruction* instr() OVERRIDE { return instr_; }
5651 LLoadFieldByIndex* instr_;
5656 Register object = ToRegister(instr->object());
5657 Register index = ToRegister(instr->index());
5659 DeferredLoadMutableDouble* deferred;
5660 deferred = new(zone()) DeferredLoadMutableDouble(
5661 this, instr, object, index);
5663 Label out_of_object, done;
5664 __ test(index, Immediate(Smi::FromInt(1)));
5665 __ j(not_zero, deferred->entry());
5669 __ cmp(index, Immediate(0));
5670 __ j(less, &out_of_object, Label::kNear);
5671 __ mov(object, FieldOperand(object,
5673 times_half_pointer_size,
5674 JSObject::kHeaderSize));
5675 __ jmp(&done, Label::kNear);
5677 __ bind(&out_of_object);
5678 __ mov(object, FieldOperand(object, JSObject::kPropertiesOffset));
5680 // Index is now equal to out of object property index plus 1.
5681 __ mov(object, FieldOperand(object,
5683 times_half_pointer_size,
5684 FixedArray::kHeaderSize - kPointerSize));
5685 __ bind(deferred->exit());
5690 void LCodeGen::DoStoreFrameContext(LStoreFrameContext* instr) {
5691 Register context = ToRegister(instr->context());
5692 __ mov(Operand(ebp, StandardFrameConstants::kContextOffset), context);
5696 void LCodeGen::DoAllocateBlockContext(LAllocateBlockContext* instr) {
5697 Handle<ScopeInfo> scope_info = instr->scope_info();
5698 __ Push(scope_info);
5699 __ push(ToRegister(instr->function()));
5700 CallRuntime(Runtime::kPushBlockContext, 2, instr);
5701 RecordSafepoint(Safepoint::kNoLazyDeopt);
5707 } } // namespace v8::internal
5709 #endif // V8_TARGET_ARCH_IA32