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 inline bool IsSIMD128LoadStoreOp(BuiltinFunctionId op) {
23 return (op == kFloat32ArrayGetFloat32x4XYZW ||
24 op == kFloat32ArrayGetFloat32x4X ||
25 op == kFloat32ArrayGetFloat32x4XY ||
26 op == kFloat32ArrayGetFloat32x4XYZ ||
27 op == kFloat64ArrayGetFloat64x2XY ||
28 op == kFloat64ArrayGetFloat64x2X ||
29 op == kInt32ArrayGetInt32x4XYZW ||
30 op == kInt32ArrayGetInt32x4X ||
31 op == kInt32ArrayGetInt32x4XY ||
32 op == kInt32ArrayGetInt32x4XYZ ||
33 op == kInt8ArrayGetFloat32x4XYZW ||
34 op == kInt8ArrayGetFloat32x4X ||
35 op == kInt8ArrayGetFloat32x4XY ||
36 op == kInt8ArrayGetFloat32x4XYZ ||
37 op == kInt8ArrayGetFloat64x2XY ||
38 op == kInt8ArrayGetFloat64x2X ||
39 op == kInt8ArrayGetInt32x4XYZW ||
40 op == kInt8ArrayGetInt32x4X ||
41 op == kInt8ArrayGetInt32x4XY ||
42 op == kInt8ArrayGetInt32x4XYZ ||
43 op == kFloat32ArraySetFloat32x4XYZW ||
44 op == kFloat32ArraySetFloat32x4X ||
45 op == kFloat32ArraySetFloat32x4XY ||
46 op == kFloat32ArraySetFloat32x4XYZ ||
47 op == kFloat64ArraySetFloat64x2XY ||
48 op == kFloat64ArraySetFloat64x2X ||
49 op == kInt32ArraySetInt32x4XYZW ||
50 op == kInt32ArraySetInt32x4X ||
51 op == kInt32ArraySetInt32x4XY ||
52 op == kInt32ArraySetInt32x4XYZ ||
53 op == kInt8ArraySetFloat32x4XYZW ||
54 op == kInt8ArraySetFloat32x4X ||
55 op == kInt8ArraySetFloat32x4XY ||
56 op == kInt8ArraySetFloat32x4XYZ ||
57 op == kInt8ArraySetFloat64x2XY ||
58 op == kInt8ArraySetFloat64x2X ||
59 op == kInt8ArraySetInt32x4XYZW ||
60 op == kInt8ArraySetInt32x4X ||
61 op == kInt8ArraySetInt32x4XY ||
62 op == kInt8ArraySetInt32x4XYZ);
66 int GetSIMD128LoadStoreBytes(BuiltinFunctionId op) {
67 if (op == kFloat32ArrayGetFloat32x4XYZW ||
68 op == kFloat64ArrayGetFloat64x2XY ||
69 op == kInt32ArrayGetInt32x4XYZW ||
70 op == kInt8ArrayGetFloat32x4XYZW ||
71 op == kInt8ArrayGetFloat64x2XY ||
72 op == kInt8ArrayGetInt32x4XYZW ||
73 op == kFloat32ArraySetFloat32x4XYZW ||
74 op == kFloat64ArraySetFloat64x2XY ||
75 op == kInt32ArraySetInt32x4XYZW ||
76 op == kInt8ArraySetFloat32x4XYZW ||
77 op == kInt8ArraySetFloat64x2XY ||
78 op == kInt8ArraySetInt32x4XYZW) {
80 } else if (op == kFloat32ArrayGetFloat32x4X ||
81 op == kInt32ArrayGetInt32x4X ||
82 op == kInt8ArrayGetFloat32x4X ||
83 op == kInt8ArrayGetInt32x4X ||
84 op == kFloat32ArraySetFloat32x4X ||
85 op == kInt32ArraySetInt32x4X ||
86 op == kInt8ArraySetFloat32x4X ||
87 op == kInt8ArraySetInt32x4X) {
89 } else if (op == kFloat32ArrayGetFloat32x4XY ||
90 op == kFloat64ArrayGetFloat64x2X ||
91 op == kInt32ArrayGetInt32x4XY ||
92 op == kInt8ArrayGetFloat32x4XY ||
93 op == kInt8ArrayGetFloat64x2X ||
94 op == kInt8ArrayGetInt32x4XY ||
95 op == kFloat32ArraySetFloat32x4XY ||
96 op == kFloat64ArraySetFloat64x2X ||
97 op == kInt32ArraySetInt32x4XY ||
98 op == kInt8ArraySetFloat32x4XY ||
99 op == kInt8ArraySetFloat64x2X ||
100 op == kInt8ArraySetInt32x4XY) {
102 } else if (op == kFloat32ArrayGetFloat32x4XYZ ||
103 op == kInt32ArrayGetInt32x4XYZ ||
104 op == kInt8ArrayGetFloat32x4XYZ ||
105 op == kInt8ArrayGetInt32x4XYZ ||
106 op == kFloat32ArraySetFloat32x4XYZ ||
107 op == kInt32ArraySetInt32x4XYZ ||
108 op == kInt8ArraySetFloat32x4XYZ ||
109 op == kInt8ArraySetInt32x4XYZ) {
118 // When invoking builtins, we need to record the safepoint in the middle of
119 // the invoke instruction sequence generated by the macro assembler.
120 class SafepointGenerator FINAL : public CallWrapper {
122 SafepointGenerator(LCodeGen* codegen,
123 LPointerMap* pointers,
124 Safepoint::DeoptMode mode)
128 virtual ~SafepointGenerator() {}
130 virtual void BeforeCall(int call_size) const OVERRIDE {}
132 virtual void AfterCall() const OVERRIDE {
133 codegen_->RecordSafepoint(pointers_, deopt_mode_);
138 LPointerMap* pointers_;
139 Safepoint::DeoptMode deopt_mode_;
145 bool LCodeGen::GenerateCode() {
146 LPhase phase("Z_Code generation", chunk());
148 status_ = GENERATING;
150 // Open a frame scope to indicate that there is a frame on the stack. The
151 // MANUAL indicates that the scope shouldn't actually generate code to set up
152 // the frame (that is done in GeneratePrologue).
153 FrameScope frame_scope(masm_, StackFrame::MANUAL);
155 support_aligned_spilled_doubles_ = info()->IsOptimizing();
157 dynamic_frame_alignment_ = info()->IsOptimizing() &&
158 ((chunk()->num_double_slots() > 2 &&
159 !chunk()->graph()->is_recursive()) ||
160 !info()->osr_ast_id().IsNone());
162 return GeneratePrologue() &&
164 GenerateDeferredCode() &&
165 GenerateJumpTable() &&
166 GenerateSafepointTable();
170 void LCodeGen::FinishCode(Handle<Code> code) {
172 code->set_stack_slots(GetStackSlotCount());
173 code->set_safepoint_table_offset(safepoints_.GetCodeOffset());
174 if (code->is_optimized_code()) RegisterWeakObjectsInOptimizedCode(code);
175 PopulateDeoptimizationData(code);
176 if (!info()->IsStub()) {
177 Deoptimizer::EnsureRelocSpaceForLazyDeoptimization(code);
183 void LCodeGen::MakeSureStackPagesMapped(int offset) {
184 const int kPageSize = 4 * KB;
185 for (offset -= kPageSize; offset > 0; offset -= kPageSize) {
186 __ mov(Operand(esp, offset), eax);
192 void LCodeGen::SaveCallerDoubles() {
193 DCHECK(info()->saves_caller_doubles());
194 DCHECK(NeedsEagerFrame());
195 Comment(";;; Save clobbered callee double registers");
197 BitVector* doubles = chunk()->allocated_double_registers();
198 BitVector::Iterator save_iterator(doubles);
199 while (!save_iterator.Done()) {
200 __ movsd(MemOperand(esp, count * kDoubleSize),
201 XMMRegister::FromAllocationIndex(save_iterator.Current()));
202 save_iterator.Advance();
208 void LCodeGen::RestoreCallerDoubles() {
209 DCHECK(info()->saves_caller_doubles());
210 DCHECK(NeedsEagerFrame());
211 Comment(";;; Restore clobbered callee double registers");
212 BitVector* doubles = chunk()->allocated_double_registers();
213 BitVector::Iterator save_iterator(doubles);
215 while (!save_iterator.Done()) {
216 __ movsd(XMMRegister::FromAllocationIndex(save_iterator.Current()),
217 MemOperand(esp, count * kDoubleSize));
218 save_iterator.Advance();
224 bool LCodeGen::GeneratePrologue() {
225 DCHECK(is_generating());
227 if (info()->IsOptimizing()) {
228 ProfileEntryHookStub::MaybeCallEntryHook(masm_);
231 if (strlen(FLAG_stop_at) > 0 &&
232 info_->function()->name()->IsUtf8EqualTo(CStrVector(FLAG_stop_at))) {
237 // Sloppy mode functions and builtins need to replace the receiver with the
238 // global proxy when called as functions (without an explicit receiver
240 if (info_->this_has_uses() &&
241 info_->strict_mode() == SLOPPY &&
242 !info_->is_native()) {
244 // +1 for return address.
245 int receiver_offset = (scope()->num_parameters() + 1) * kPointerSize;
246 __ mov(ecx, Operand(esp, receiver_offset));
248 __ cmp(ecx, isolate()->factory()->undefined_value());
249 __ j(not_equal, &ok, Label::kNear);
251 __ mov(ecx, GlobalObjectOperand());
252 __ mov(ecx, FieldOperand(ecx, GlobalObject::kGlobalProxyOffset));
254 __ mov(Operand(esp, receiver_offset), ecx);
259 if (support_aligned_spilled_doubles_ && dynamic_frame_alignment_) {
260 // Move state of dynamic frame alignment into edx.
261 __ Move(edx, Immediate(kNoAlignmentPadding));
263 Label do_not_pad, align_loop;
264 STATIC_ASSERT(kDoubleSize == 2 * kPointerSize);
265 // Align esp + 4 to a multiple of 2 * kPointerSize.
266 __ test(esp, Immediate(kPointerSize));
267 __ j(not_zero, &do_not_pad, Label::kNear);
268 __ push(Immediate(0));
270 __ mov(edx, Immediate(kAlignmentPaddingPushed));
271 // Copy arguments, receiver, and return address.
272 __ mov(ecx, Immediate(scope()->num_parameters() + 2));
274 __ bind(&align_loop);
275 __ mov(eax, Operand(ebx, 1 * kPointerSize));
276 __ mov(Operand(ebx, 0), eax);
277 __ add(Operand(ebx), Immediate(kPointerSize));
279 __ j(not_zero, &align_loop, Label::kNear);
280 __ mov(Operand(ebx, 0), Immediate(kAlignmentZapValue));
281 __ bind(&do_not_pad);
285 info()->set_prologue_offset(masm_->pc_offset());
286 if (NeedsEagerFrame()) {
287 DCHECK(!frame_is_built_);
288 frame_is_built_ = true;
289 if (info()->IsStub()) {
292 __ Prologue(info()->IsCodePreAgingActive());
294 info()->AddNoFrameRange(0, masm_->pc_offset());
297 if (info()->IsOptimizing() &&
298 dynamic_frame_alignment_ &&
300 __ test(esp, Immediate(kPointerSize));
301 __ Assert(zero, kFrameIsExpectedToBeAligned);
304 // Reserve space for the stack slots needed by the code.
305 int slots = GetStackSlotCount();
306 DCHECK(slots != 0 || !info()->IsOptimizing());
309 if (dynamic_frame_alignment_) {
312 __ push(Immediate(kNoAlignmentPadding));
315 if (FLAG_debug_code) {
316 __ sub(Operand(esp), Immediate(slots * kPointerSize));
318 MakeSureStackPagesMapped(slots * kPointerSize);
321 __ mov(Operand(eax), Immediate(slots));
324 __ mov(MemOperand(esp, eax, times_4, 0),
325 Immediate(kSlotsZapValue));
327 __ j(not_zero, &loop);
330 __ sub(Operand(esp), Immediate(slots * kPointerSize));
332 MakeSureStackPagesMapped(slots * kPointerSize);
336 if (support_aligned_spilled_doubles_) {
337 Comment(";;; Store dynamic frame alignment tag for spilled doubles");
338 // Store dynamic frame alignment state in the first local.
339 int offset = JavaScriptFrameConstants::kDynamicAlignmentStateOffset;
340 if (dynamic_frame_alignment_) {
341 __ mov(Operand(ebp, offset), edx);
343 __ mov(Operand(ebp, offset), Immediate(kNoAlignmentPadding));
348 if (info()->saves_caller_doubles()) SaveCallerDoubles();
351 // Possibly allocate a local context.
352 int heap_slots = info_->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
353 if (heap_slots > 0) {
354 Comment(";;; Allocate local context");
355 bool need_write_barrier = true;
356 // Argument to NewContext is the function, which is still in edi.
357 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
358 FastNewContextStub stub(isolate(), heap_slots);
360 // Result of FastNewContextStub is always in new space.
361 need_write_barrier = false;
364 __ CallRuntime(Runtime::kNewFunctionContext, 1);
366 RecordSafepoint(Safepoint::kNoLazyDeopt);
367 // Context is returned in eax. It replaces the context passed to us.
368 // It's saved in the stack and kept live in esi.
370 __ mov(Operand(ebp, StandardFrameConstants::kContextOffset), eax);
372 // Copy parameters into context if necessary.
373 int num_parameters = scope()->num_parameters();
374 for (int i = 0; i < num_parameters; i++) {
375 Variable* var = scope()->parameter(i);
376 if (var->IsContextSlot()) {
377 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
378 (num_parameters - 1 - i) * kPointerSize;
379 // Load parameter from stack.
380 __ mov(eax, Operand(ebp, parameter_offset));
381 // Store it in the context.
382 int context_offset = Context::SlotOffset(var->index());
383 __ mov(Operand(esi, context_offset), eax);
384 // Update the write barrier. This clobbers eax and ebx.
385 if (need_write_barrier) {
386 __ RecordWriteContextSlot(esi,
391 } else if (FLAG_debug_code) {
393 __ JumpIfInNewSpace(esi, eax, &done, Label::kNear);
394 __ Abort(kExpectedNewSpaceObject);
399 Comment(";;; End allocate local context");
403 if (FLAG_trace && info()->IsOptimizing()) {
404 // We have not executed any compiled code yet, so esi still holds the
406 __ CallRuntime(Runtime::kTraceEnter, 0);
408 return !is_aborted();
412 void LCodeGen::GenerateOsrPrologue() {
413 // Generate the OSR entry prologue at the first unknown OSR value, or if there
414 // are none, at the OSR entrypoint instruction.
415 if (osr_pc_offset_ >= 0) return;
417 osr_pc_offset_ = masm()->pc_offset();
419 // Move state of dynamic frame alignment into edx.
420 __ Move(edx, Immediate(kNoAlignmentPadding));
422 if (support_aligned_spilled_doubles_ && dynamic_frame_alignment_) {
423 Label do_not_pad, align_loop;
424 // Align ebp + 4 to a multiple of 2 * kPointerSize.
425 __ test(ebp, Immediate(kPointerSize));
426 __ j(zero, &do_not_pad, Label::kNear);
427 __ push(Immediate(0));
429 __ mov(edx, Immediate(kAlignmentPaddingPushed));
431 // Move all parts of the frame over one word. The frame consists of:
432 // unoptimized frame slots, alignment state, context, frame pointer, return
433 // address, receiver, and the arguments.
434 __ mov(ecx, Immediate(scope()->num_parameters() +
435 5 + graph()->osr()->UnoptimizedFrameSlots()));
437 __ bind(&align_loop);
438 __ mov(eax, Operand(ebx, 1 * kPointerSize));
439 __ mov(Operand(ebx, 0), eax);
440 __ add(Operand(ebx), Immediate(kPointerSize));
442 __ j(not_zero, &align_loop, Label::kNear);
443 __ mov(Operand(ebx, 0), Immediate(kAlignmentZapValue));
444 __ sub(Operand(ebp), Immediate(kPointerSize));
445 __ bind(&do_not_pad);
448 // Save the first local, which is overwritten by the alignment state.
449 Operand alignment_loc = MemOperand(ebp, -3 * kPointerSize);
450 __ push(alignment_loc);
452 // Set the dynamic frame alignment state.
453 __ mov(alignment_loc, edx);
455 // Adjust the frame size, subsuming the unoptimized frame into the
457 int slots = GetStackSlotCount() - graph()->osr()->UnoptimizedFrameSlots();
459 __ sub(esp, Immediate((slots - 1) * kPointerSize));
463 void LCodeGen::GenerateBodyInstructionPre(LInstruction* instr) {
464 if (instr->IsCall()) {
465 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
467 if (!instr->IsLazyBailout() && !instr->IsGap()) {
468 safepoints_.BumpLastLazySafepointIndex();
473 void LCodeGen::GenerateBodyInstructionPost(LInstruction* instr) { }
476 bool LCodeGen::GenerateJumpTable() {
478 if (jump_table_.length() > 0) {
479 Comment(";;; -------------------- Jump table --------------------");
481 for (int i = 0; i < jump_table_.length(); i++) {
482 Deoptimizer::JumpTableEntry* table_entry = &jump_table_[i];
483 __ bind(&table_entry->label);
484 Address entry = table_entry->address;
485 DeoptComment(table_entry->reason);
486 if (table_entry->needs_frame) {
487 DCHECK(!info()->saves_caller_doubles());
488 __ push(Immediate(ExternalReference::ForDeoptEntry(entry)));
489 if (needs_frame.is_bound()) {
490 __ jmp(&needs_frame);
492 __ bind(&needs_frame);
493 __ push(MemOperand(ebp, StandardFrameConstants::kContextOffset));
494 // This variant of deopt can only be used with stubs. Since we don't
495 // have a function pointer to install in the stack frame that we're
496 // building, install a special marker there instead.
497 DCHECK(info()->IsStub());
498 __ push(Immediate(Smi::FromInt(StackFrame::STUB)));
499 // Push a PC inside the function so that the deopt code can find where
500 // the deopt comes from. It doesn't have to be the precise return
501 // address of a "calling" LAZY deopt, it only has to be somewhere
502 // inside the code body.
503 Label push_approx_pc;
504 __ call(&push_approx_pc);
505 __ bind(&push_approx_pc);
506 // Push the continuation which was stashed were the ebp should
507 // be. Replace it with the saved ebp.
508 __ push(MemOperand(esp, 3 * kPointerSize));
509 __ mov(MemOperand(esp, 4 * kPointerSize), ebp);
510 __ lea(ebp, MemOperand(esp, 4 * kPointerSize));
511 __ ret(0); // Call the continuation without clobbering registers.
514 if (info()->saves_caller_doubles()) RestoreCallerDoubles();
515 __ call(entry, RelocInfo::RUNTIME_ENTRY);
518 return !is_aborted();
522 bool LCodeGen::GenerateDeferredCode() {
523 DCHECK(is_generating());
524 if (deferred_.length() > 0) {
525 for (int i = 0; !is_aborted() && i < deferred_.length(); i++) {
526 LDeferredCode* code = deferred_[i];
529 instructions_->at(code->instruction_index())->hydrogen_value();
530 RecordAndWritePosition(
531 chunk()->graph()->SourcePositionToScriptPosition(value->position()));
533 Comment(";;; <@%d,#%d> "
534 "-------------------- Deferred %s --------------------",
535 code->instruction_index(),
536 code->instr()->hydrogen_value()->id(),
537 code->instr()->Mnemonic());
538 __ bind(code->entry());
539 if (NeedsDeferredFrame()) {
540 Comment(";;; Build frame");
541 DCHECK(!frame_is_built_);
542 DCHECK(info()->IsStub());
543 frame_is_built_ = true;
544 // Build the frame in such a way that esi isn't trashed.
545 __ push(ebp); // Caller's frame pointer.
546 __ push(Operand(ebp, StandardFrameConstants::kContextOffset));
547 __ push(Immediate(Smi::FromInt(StackFrame::STUB)));
548 __ lea(ebp, Operand(esp, 2 * kPointerSize));
549 Comment(";;; Deferred code");
552 if (NeedsDeferredFrame()) {
553 __ bind(code->done());
554 Comment(";;; Destroy frame");
555 DCHECK(frame_is_built_);
556 frame_is_built_ = false;
560 __ jmp(code->exit());
564 // Deferred code is the last part of the instruction sequence. Mark
565 // the generated code as done unless we bailed out.
566 if (!is_aborted()) status_ = DONE;
567 return !is_aborted();
571 bool LCodeGen::GenerateSafepointTable() {
573 if (!info()->IsStub()) {
574 // For lazy deoptimization we need space to patch a call after every call.
575 // Ensure there is always space for such patching, even if the code ends
577 int target_offset = masm()->pc_offset() + Deoptimizer::patch_size();
578 while (masm()->pc_offset() < target_offset) {
582 safepoints_.Emit(masm(), GetStackSlotCount());
583 return !is_aborted();
587 Register LCodeGen::ToRegister(int index) const {
588 return Register::FromAllocationIndex(index);
592 XMMRegister LCodeGen::ToDoubleRegister(int index) const {
593 return XMMRegister::FromAllocationIndex(index);
597 XMMRegister LCodeGen::ToSIMD128Register(int index) const {
598 return XMMRegister::FromAllocationIndex(index);
602 Register LCodeGen::ToRegister(LOperand* op) const {
603 DCHECK(op->IsRegister());
604 return ToRegister(op->index());
608 XMMRegister LCodeGen::ToDoubleRegister(LOperand* op) const {
609 DCHECK(op->IsDoubleRegister());
610 return ToDoubleRegister(op->index());
614 XMMRegister LCodeGen::ToFloat32x4Register(LOperand* op) const {
615 DCHECK(op->IsFloat32x4Register());
616 return ToSIMD128Register(op->index());
620 XMMRegister LCodeGen::ToFloat64x2Register(LOperand* op) const {
621 DCHECK(op->IsFloat64x2Register());
622 return ToSIMD128Register(op->index());
626 XMMRegister LCodeGen::ToInt32x4Register(LOperand* op) const {
627 DCHECK(op->IsInt32x4Register());
628 return ToSIMD128Register(op->index());
632 XMMRegister LCodeGen::ToSIMD128Register(LOperand* op) const {
633 DCHECK(op->IsFloat32x4Register() || op->IsFloat64x2Register() ||
634 op->IsInt32x4Register());
635 return ToSIMD128Register(op->index());
639 int32_t LCodeGen::ToInteger32(LConstantOperand* op) const {
640 return ToRepresentation(op, Representation::Integer32());
644 int32_t LCodeGen::ToRepresentation(LConstantOperand* op,
645 const Representation& r) const {
646 HConstant* constant = chunk_->LookupConstant(op);
647 int32_t value = constant->Integer32Value();
648 if (r.IsInteger32()) return value;
649 DCHECK(r.IsSmiOrTagged());
650 return reinterpret_cast<int32_t>(Smi::FromInt(value));
654 Handle<Object> LCodeGen::ToHandle(LConstantOperand* op) const {
655 HConstant* constant = chunk_->LookupConstant(op);
656 DCHECK(chunk_->LookupLiteralRepresentation(op).IsSmiOrTagged());
657 return constant->handle(isolate());
661 double LCodeGen::ToDouble(LConstantOperand* op) const {
662 HConstant* constant = chunk_->LookupConstant(op);
663 DCHECK(constant->HasDoubleValue());
664 return constant->DoubleValue();
668 ExternalReference LCodeGen::ToExternalReference(LConstantOperand* op) const {
669 HConstant* constant = chunk_->LookupConstant(op);
670 DCHECK(constant->HasExternalReferenceValue());
671 return constant->ExternalReferenceValue();
675 bool LCodeGen::IsInteger32(LConstantOperand* op) const {
676 return chunk_->LookupLiteralRepresentation(op).IsSmiOrInteger32();
680 bool LCodeGen::IsSmi(LConstantOperand* op) const {
681 return chunk_->LookupLiteralRepresentation(op).IsSmi();
685 static int ArgumentsOffsetWithoutFrame(int index) {
687 return -(index + 1) * kPointerSize + kPCOnStackSize;
691 Operand LCodeGen::ToOperand(LOperand* op) const {
692 if (op->IsRegister()) return Operand(ToRegister(op));
693 if (op->IsDoubleRegister()) return Operand(ToDoubleRegister(op));
694 if (op->IsFloat32x4Register()) return Operand(ToFloat32x4Register(op));
695 if (op->IsFloat64x2Register()) return Operand(ToFloat64x2Register(op));
696 if (op->IsInt32x4Register()) return Operand(ToInt32x4Register(op));
697 DCHECK(op->IsStackSlot() || op->IsDoubleStackSlot() ||
698 op->IsFloat32x4StackSlot() || op->IsFloat64x2StackSlot() ||
699 op->IsInt32x4StackSlot());
700 if (NeedsEagerFrame()) {
701 return Operand(ebp, StackSlotOffset(op->index()));
703 // Retrieve parameter without eager stack-frame relative to the
705 return Operand(esp, ArgumentsOffsetWithoutFrame(op->index()));
710 Operand LCodeGen::HighOperand(LOperand* op) {
711 DCHECK(op->IsDoubleStackSlot());
712 if (NeedsEagerFrame()) {
713 return Operand(ebp, StackSlotOffset(op->index()) + kPointerSize);
715 // Retrieve parameter without eager stack-frame relative to the
718 esp, ArgumentsOffsetWithoutFrame(op->index()) + kPointerSize);
723 void LCodeGen::WriteTranslation(LEnvironment* environment,
724 Translation* translation) {
725 if (environment == NULL) return;
727 // The translation includes one command per value in the environment.
728 int translation_size = environment->translation_size();
729 // The output frame height does not include the parameters.
730 int height = translation_size - environment->parameter_count();
732 WriteTranslation(environment->outer(), translation);
733 bool has_closure_id = !info()->closure().is_null() &&
734 !info()->closure().is_identical_to(environment->closure());
735 int closure_id = has_closure_id
736 ? DefineDeoptimizationLiteral(environment->closure())
737 : Translation::kSelfLiteralId;
738 switch (environment->frame_type()) {
740 translation->BeginJSFrame(environment->ast_id(), closure_id, height);
743 translation->BeginConstructStubFrame(closure_id, translation_size);
746 DCHECK(translation_size == 1);
748 translation->BeginGetterStubFrame(closure_id);
751 DCHECK(translation_size == 2);
753 translation->BeginSetterStubFrame(closure_id);
755 case ARGUMENTS_ADAPTOR:
756 translation->BeginArgumentsAdaptorFrame(closure_id, translation_size);
759 translation->BeginCompiledStubFrame();
765 int object_index = 0;
766 int dematerialized_index = 0;
767 for (int i = 0; i < translation_size; ++i) {
768 LOperand* value = environment->values()->at(i);
769 AddToTranslation(environment,
772 environment->HasTaggedValueAt(i),
773 environment->HasUint32ValueAt(i),
775 &dematerialized_index);
780 void LCodeGen::AddToTranslation(LEnvironment* environment,
781 Translation* translation,
785 int* object_index_pointer,
786 int* dematerialized_index_pointer) {
787 if (op == LEnvironment::materialization_marker()) {
788 int object_index = (*object_index_pointer)++;
789 if (environment->ObjectIsDuplicateAt(object_index)) {
790 int dupe_of = environment->ObjectDuplicateOfAt(object_index);
791 translation->DuplicateObject(dupe_of);
794 int object_length = environment->ObjectLengthAt(object_index);
795 if (environment->ObjectIsArgumentsAt(object_index)) {
796 translation->BeginArgumentsObject(object_length);
798 translation->BeginCapturedObject(object_length);
800 int dematerialized_index = *dematerialized_index_pointer;
801 int env_offset = environment->translation_size() + dematerialized_index;
802 *dematerialized_index_pointer += object_length;
803 for (int i = 0; i < object_length; ++i) {
804 LOperand* value = environment->values()->at(env_offset + i);
805 AddToTranslation(environment,
808 environment->HasTaggedValueAt(env_offset + i),
809 environment->HasUint32ValueAt(env_offset + i),
810 object_index_pointer,
811 dematerialized_index_pointer);
816 if (op->IsStackSlot()) {
818 translation->StoreStackSlot(op->index());
819 } else if (is_uint32) {
820 translation->StoreUint32StackSlot(op->index());
822 translation->StoreInt32StackSlot(op->index());
824 } else if (op->IsDoubleStackSlot()) {
825 translation->StoreDoubleStackSlot(op->index());
826 } else if (op->IsFloat32x4StackSlot()) {
827 translation->StoreSIMD128StackSlot(op->index(),
828 Translation::FLOAT32x4_STACK_SLOT);
829 } else if (op->IsFloat64x2StackSlot()) {
830 translation->StoreSIMD128StackSlot(op->index(),
831 Translation::FLOAT64x2_STACK_SLOT);
832 } else if (op->IsInt32x4StackSlot()) {
833 translation->StoreSIMD128StackSlot(op->index(),
834 Translation::INT32x4_STACK_SLOT);
835 } else if (op->IsRegister()) {
836 Register reg = ToRegister(op);
838 translation->StoreRegister(reg);
839 } else if (is_uint32) {
840 translation->StoreUint32Register(reg);
842 translation->StoreInt32Register(reg);
844 } else if (op->IsDoubleRegister()) {
845 XMMRegister reg = ToDoubleRegister(op);
846 translation->StoreDoubleRegister(reg);
847 } else if (op->IsFloat32x4Register()) {
848 XMMRegister reg = ToFloat32x4Register(op);
849 translation->StoreSIMD128Register(reg, Translation::FLOAT32x4_REGISTER);
850 } else if (op->IsFloat64x2Register()) {
851 XMMRegister reg = ToFloat64x2Register(op);
852 translation->StoreSIMD128Register(reg, Translation::FLOAT64x2_REGISTER);
853 } else if (op->IsInt32x4Register()) {
854 XMMRegister reg = ToInt32x4Register(op);
855 translation->StoreSIMD128Register(reg, Translation::INT32x4_REGISTER);
856 } else if (op->IsConstantOperand()) {
857 HConstant* constant = chunk()->LookupConstant(LConstantOperand::cast(op));
858 int src_index = DefineDeoptimizationLiteral(constant->handle(isolate()));
859 translation->StoreLiteral(src_index);
866 void LCodeGen::CallCodeGeneric(Handle<Code> code,
867 RelocInfo::Mode mode,
869 SafepointMode safepoint_mode) {
870 DCHECK(instr != NULL);
872 RecordSafepointWithLazyDeopt(instr, safepoint_mode);
874 // Signal that we don't inline smi code before these stubs in the
875 // optimizing code generator.
876 if (code->kind() == Code::BINARY_OP_IC ||
877 code->kind() == Code::COMPARE_IC) {
883 void LCodeGen::CallCode(Handle<Code> code,
884 RelocInfo::Mode mode,
885 LInstruction* instr) {
886 CallCodeGeneric(code, mode, instr, RECORD_SIMPLE_SAFEPOINT);
890 void LCodeGen::CallRuntime(const Runtime::Function* fun,
893 SaveFPRegsMode save_doubles) {
894 DCHECK(instr != NULL);
895 DCHECK(instr->HasPointerMap());
897 __ CallRuntime(fun, argc, save_doubles);
899 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
901 DCHECK(info()->is_calling());
905 void LCodeGen::LoadContextFromDeferred(LOperand* context) {
906 if (context->IsRegister()) {
907 if (!ToRegister(context).is(esi)) {
908 __ mov(esi, ToRegister(context));
910 } else if (context->IsStackSlot()) {
911 __ mov(esi, ToOperand(context));
912 } else if (context->IsConstantOperand()) {
913 HConstant* constant =
914 chunk_->LookupConstant(LConstantOperand::cast(context));
915 __ LoadObject(esi, Handle<Object>::cast(constant->handle(isolate())));
921 void LCodeGen::CallRuntimeFromDeferred(Runtime::FunctionId id,
925 LoadContextFromDeferred(context);
927 __ CallRuntimeSaveDoubles(id);
928 RecordSafepointWithRegisters(
929 instr->pointer_map(), argc, Safepoint::kNoLazyDeopt);
931 DCHECK(info()->is_calling());
935 void LCodeGen::RegisterEnvironmentForDeoptimization(
936 LEnvironment* environment, Safepoint::DeoptMode mode) {
937 environment->set_has_been_used();
938 if (!environment->HasBeenRegistered()) {
939 // Physical stack frame layout:
940 // -x ............. -4 0 ..................................... y
941 // [incoming arguments] [spill slots] [pushed outgoing arguments]
943 // Layout of the environment:
944 // 0 ..................................................... size-1
945 // [parameters] [locals] [expression stack including arguments]
947 // Layout of the translation:
948 // 0 ........................................................ size - 1 + 4
949 // [expression stack including arguments] [locals] [4 words] [parameters]
950 // |>------------ translation_size ------------<|
953 int jsframe_count = 0;
954 for (LEnvironment* e = environment; e != NULL; e = e->outer()) {
956 if (e->frame_type() == JS_FUNCTION) {
960 Translation translation(&translations_, frame_count, jsframe_count, zone());
961 WriteTranslation(environment, &translation);
962 int deoptimization_index = deoptimizations_.length();
963 int pc_offset = masm()->pc_offset();
964 environment->Register(deoptimization_index,
966 (mode == Safepoint::kLazyDeopt) ? pc_offset : -1);
967 deoptimizations_.Add(environment, zone());
972 void LCodeGen::DeoptimizeIf(Condition cc, LInstruction* instr,
974 Deoptimizer::BailoutType bailout_type) {
975 LEnvironment* environment = instr->environment();
976 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
977 DCHECK(environment->HasBeenRegistered());
978 int id = environment->deoptimization_index();
979 DCHECK(info()->IsOptimizing() || info()->IsStub());
981 Deoptimizer::GetDeoptimizationEntry(isolate(), id, bailout_type);
983 Abort(kBailoutWasNotPrepared);
987 if (DeoptEveryNTimes()) {
988 ExternalReference count = ExternalReference::stress_deopt_count(isolate());
992 __ mov(eax, Operand::StaticVariable(count));
993 __ sub(eax, Immediate(1));
994 __ j(not_zero, &no_deopt, Label::kNear);
995 if (FLAG_trap_on_deopt) __ int3();
996 __ mov(eax, Immediate(FLAG_deopt_every_n_times));
997 __ mov(Operand::StaticVariable(count), eax);
1000 DCHECK(frame_is_built_);
1001 __ call(entry, RelocInfo::RUNTIME_ENTRY);
1003 __ mov(Operand::StaticVariable(count), eax);
1008 if (info()->ShouldTrapOnDeopt()) {
1010 if (cc != no_condition) __ j(NegateCondition(cc), &done, Label::kNear);
1015 Deoptimizer::Reason reason(instr->hydrogen_value()->position().raw(),
1016 instr->Mnemonic(), detail);
1017 DCHECK(info()->IsStub() || frame_is_built_);
1018 if (cc == no_condition && frame_is_built_) {
1019 DeoptComment(reason);
1020 __ call(entry, RelocInfo::RUNTIME_ENTRY);
1022 Deoptimizer::JumpTableEntry table_entry(entry, reason, bailout_type,
1024 // We often have several deopts to the same entry, reuse the last
1025 // jump entry if this is the case.
1026 if (jump_table_.is_empty() ||
1027 !table_entry.IsEquivalentTo(jump_table_.last())) {
1028 jump_table_.Add(table_entry, zone());
1030 if (cc == no_condition) {
1031 __ jmp(&jump_table_.last().label);
1033 __ j(cc, &jump_table_.last().label);
1039 void LCodeGen::DeoptimizeIf(Condition cc, LInstruction* instr,
1040 const char* detail) {
1041 Deoptimizer::BailoutType bailout_type = info()->IsStub()
1043 : Deoptimizer::EAGER;
1044 DeoptimizeIf(cc, instr, detail, bailout_type);
1048 void LCodeGen::PopulateDeoptimizationData(Handle<Code> code) {
1049 int length = deoptimizations_.length();
1050 if (length == 0) return;
1051 Handle<DeoptimizationInputData> data =
1052 DeoptimizationInputData::New(isolate(), length, TENURED);
1054 Handle<ByteArray> translations =
1055 translations_.CreateByteArray(isolate()->factory());
1056 data->SetTranslationByteArray(*translations);
1057 data->SetInlinedFunctionCount(Smi::FromInt(inlined_function_count_));
1058 data->SetOptimizationId(Smi::FromInt(info_->optimization_id()));
1059 if (info_->IsOptimizing()) {
1060 // Reference to shared function info does not change between phases.
1061 AllowDeferredHandleDereference allow_handle_dereference;
1062 data->SetSharedFunctionInfo(*info_->shared_info());
1064 data->SetSharedFunctionInfo(Smi::FromInt(0));
1067 Handle<FixedArray> literals =
1068 factory()->NewFixedArray(deoptimization_literals_.length(), TENURED);
1069 { AllowDeferredHandleDereference copy_handles;
1070 for (int i = 0; i < deoptimization_literals_.length(); i++) {
1071 literals->set(i, *deoptimization_literals_[i]);
1073 data->SetLiteralArray(*literals);
1076 data->SetOsrAstId(Smi::FromInt(info_->osr_ast_id().ToInt()));
1077 data->SetOsrPcOffset(Smi::FromInt(osr_pc_offset_));
1079 // Populate the deoptimization entries.
1080 for (int i = 0; i < length; i++) {
1081 LEnvironment* env = deoptimizations_[i];
1082 data->SetAstId(i, env->ast_id());
1083 data->SetTranslationIndex(i, Smi::FromInt(env->translation_index()));
1084 data->SetArgumentsStackHeight(i,
1085 Smi::FromInt(env->arguments_stack_height()));
1086 data->SetPc(i, Smi::FromInt(env->pc_offset()));
1088 code->set_deoptimization_data(*data);
1092 int LCodeGen::DefineDeoptimizationLiteral(Handle<Object> literal) {
1093 int result = deoptimization_literals_.length();
1094 for (int i = 0; i < deoptimization_literals_.length(); ++i) {
1095 if (deoptimization_literals_[i].is_identical_to(literal)) return i;
1097 deoptimization_literals_.Add(literal, zone());
1102 void LCodeGen::PopulateDeoptimizationLiteralsWithInlinedFunctions() {
1103 DCHECK(deoptimization_literals_.length() == 0);
1105 const ZoneList<Handle<JSFunction> >* inlined_closures =
1106 chunk()->inlined_closures();
1108 for (int i = 0, length = inlined_closures->length();
1111 DefineDeoptimizationLiteral(inlined_closures->at(i));
1114 inlined_function_count_ = deoptimization_literals_.length();
1118 void LCodeGen::RecordSafepointWithLazyDeopt(
1119 LInstruction* instr, SafepointMode safepoint_mode) {
1120 if (safepoint_mode == RECORD_SIMPLE_SAFEPOINT) {
1121 RecordSafepoint(instr->pointer_map(), Safepoint::kLazyDeopt);
1123 DCHECK(safepoint_mode == RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
1124 RecordSafepointWithRegisters(
1125 instr->pointer_map(), 0, Safepoint::kLazyDeopt);
1130 void LCodeGen::RecordSafepoint(
1131 LPointerMap* pointers,
1132 Safepoint::Kind kind,
1134 Safepoint::DeoptMode deopt_mode) {
1135 DCHECK(kind == expected_safepoint_kind_);
1136 const ZoneList<LOperand*>* operands = pointers->GetNormalizedOperands();
1137 Safepoint safepoint =
1138 safepoints_.DefineSafepoint(masm(), kind, arguments, deopt_mode);
1139 for (int i = 0; i < operands->length(); i++) {
1140 LOperand* pointer = operands->at(i);
1141 if (pointer->IsStackSlot()) {
1142 safepoint.DefinePointerSlot(pointer->index(), zone());
1143 } else if (pointer->IsRegister() && (kind & Safepoint::kWithRegisters)) {
1144 safepoint.DefinePointerRegister(ToRegister(pointer), zone());
1150 void LCodeGen::RecordSafepoint(LPointerMap* pointers,
1151 Safepoint::DeoptMode mode) {
1152 RecordSafepoint(pointers, Safepoint::kSimple, 0, mode);
1156 void LCodeGen::RecordSafepoint(Safepoint::DeoptMode mode) {
1157 LPointerMap empty_pointers(zone());
1158 RecordSafepoint(&empty_pointers, mode);
1162 void LCodeGen::RecordSafepointWithRegisters(LPointerMap* pointers,
1164 Safepoint::DeoptMode mode) {
1165 RecordSafepoint(pointers, Safepoint::kWithRegisters, arguments, mode);
1169 void LCodeGen::RecordAndWritePosition(int position) {
1170 if (position == RelocInfo::kNoPosition) return;
1171 masm()->positions_recorder()->RecordPosition(position);
1172 masm()->positions_recorder()->WriteRecordedPositions();
1176 static const char* LabelType(LLabel* label) {
1177 if (label->is_loop_header()) return " (loop header)";
1178 if (label->is_osr_entry()) return " (OSR entry)";
1183 void LCodeGen::DoLabel(LLabel* label) {
1184 Comment(";;; <@%d,#%d> -------------------- B%d%s --------------------",
1185 current_instruction_,
1186 label->hydrogen_value()->id(),
1189 __ bind(label->label());
1190 current_block_ = label->block_id();
1195 void LCodeGen::DoParallelMove(LParallelMove* move) {
1196 resolver_.Resolve(move);
1200 void LCodeGen::DoGap(LGap* gap) {
1201 for (int i = LGap::FIRST_INNER_POSITION;
1202 i <= LGap::LAST_INNER_POSITION;
1204 LGap::InnerPosition inner_pos = static_cast<LGap::InnerPosition>(i);
1205 LParallelMove* move = gap->GetParallelMove(inner_pos);
1206 if (move != NULL) DoParallelMove(move);
1211 void LCodeGen::DoInstructionGap(LInstructionGap* instr) {
1216 void LCodeGen::DoParameter(LParameter* instr) {
1221 void LCodeGen::DoCallStub(LCallStub* instr) {
1222 DCHECK(ToRegister(instr->context()).is(esi));
1223 DCHECK(ToRegister(instr->result()).is(eax));
1224 switch (instr->hydrogen()->major_key()) {
1225 case CodeStub::RegExpExec: {
1226 RegExpExecStub stub(isolate());
1227 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1230 case CodeStub::SubString: {
1231 SubStringStub stub(isolate());
1232 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1235 case CodeStub::StringCompare: {
1236 StringCompareStub stub(isolate());
1237 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1246 void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) {
1247 GenerateOsrPrologue();
1251 void LCodeGen::DoModByPowerOf2I(LModByPowerOf2I* instr) {
1252 Register dividend = ToRegister(instr->dividend());
1253 int32_t divisor = instr->divisor();
1254 DCHECK(dividend.is(ToRegister(instr->result())));
1256 // Theoretically, a variation of the branch-free code for integer division by
1257 // a power of 2 (calculating the remainder via an additional multiplication
1258 // (which gets simplified to an 'and') and subtraction) should be faster, and
1259 // this is exactly what GCC and clang emit. Nevertheless, benchmarks seem to
1260 // indicate that positive dividends are heavily favored, so the branching
1261 // version performs better.
1262 HMod* hmod = instr->hydrogen();
1263 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
1264 Label dividend_is_not_negative, done;
1265 if (hmod->CheckFlag(HValue::kLeftCanBeNegative)) {
1266 __ test(dividend, dividend);
1267 __ j(not_sign, ÷nd_is_not_negative, Label::kNear);
1268 // Note that this is correct even for kMinInt operands.
1270 __ and_(dividend, mask);
1272 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1273 DeoptimizeIf(zero, instr, "minus zero");
1275 __ jmp(&done, Label::kNear);
1278 __ bind(÷nd_is_not_negative);
1279 __ and_(dividend, mask);
1284 void LCodeGen::DoModByConstI(LModByConstI* instr) {
1285 Register dividend = ToRegister(instr->dividend());
1286 int32_t divisor = instr->divisor();
1287 DCHECK(ToRegister(instr->result()).is(eax));
1290 DeoptimizeIf(no_condition, instr, "division by zero");
1294 __ TruncatingDiv(dividend, Abs(divisor));
1295 __ imul(edx, edx, Abs(divisor));
1296 __ mov(eax, dividend);
1299 // Check for negative zero.
1300 HMod* hmod = instr->hydrogen();
1301 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1302 Label remainder_not_zero;
1303 __ j(not_zero, &remainder_not_zero, Label::kNear);
1304 __ cmp(dividend, Immediate(0));
1305 DeoptimizeIf(less, instr, "minus zero");
1306 __ bind(&remainder_not_zero);
1311 void LCodeGen::DoModI(LModI* instr) {
1312 HMod* hmod = instr->hydrogen();
1314 Register left_reg = ToRegister(instr->left());
1315 DCHECK(left_reg.is(eax));
1316 Register right_reg = ToRegister(instr->right());
1317 DCHECK(!right_reg.is(eax));
1318 DCHECK(!right_reg.is(edx));
1319 Register result_reg = ToRegister(instr->result());
1320 DCHECK(result_reg.is(edx));
1323 // Check for x % 0, idiv would signal a divide error. We have to
1324 // deopt in this case because we can't return a NaN.
1325 if (hmod->CheckFlag(HValue::kCanBeDivByZero)) {
1326 __ test(right_reg, Operand(right_reg));
1327 DeoptimizeIf(zero, instr, "division by zero");
1330 // Check for kMinInt % -1, idiv would signal a divide error. We
1331 // have to deopt if we care about -0, because we can't return that.
1332 if (hmod->CheckFlag(HValue::kCanOverflow)) {
1333 Label no_overflow_possible;
1334 __ cmp(left_reg, kMinInt);
1335 __ j(not_equal, &no_overflow_possible, Label::kNear);
1336 __ cmp(right_reg, -1);
1337 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1338 DeoptimizeIf(equal, instr, "minus zero");
1340 __ j(not_equal, &no_overflow_possible, Label::kNear);
1341 __ Move(result_reg, Immediate(0));
1342 __ jmp(&done, Label::kNear);
1344 __ bind(&no_overflow_possible);
1347 // Sign extend dividend in eax into edx:eax.
1350 // If we care about -0, test if the dividend is <0 and the result is 0.
1351 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1352 Label positive_left;
1353 __ test(left_reg, Operand(left_reg));
1354 __ j(not_sign, &positive_left, Label::kNear);
1356 __ test(result_reg, Operand(result_reg));
1357 DeoptimizeIf(zero, instr, "minus zero");
1358 __ jmp(&done, Label::kNear);
1359 __ bind(&positive_left);
1366 void LCodeGen::DoDivByPowerOf2I(LDivByPowerOf2I* instr) {
1367 Register dividend = ToRegister(instr->dividend());
1368 int32_t divisor = instr->divisor();
1369 Register result = ToRegister(instr->result());
1370 DCHECK(divisor == kMinInt || base::bits::IsPowerOfTwo32(Abs(divisor)));
1371 DCHECK(!result.is(dividend));
1373 // Check for (0 / -x) that will produce negative zero.
1374 HDiv* hdiv = instr->hydrogen();
1375 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1376 __ test(dividend, dividend);
1377 DeoptimizeIf(zero, instr, "minus zero");
1379 // Check for (kMinInt / -1).
1380 if (hdiv->CheckFlag(HValue::kCanOverflow) && divisor == -1) {
1381 __ cmp(dividend, kMinInt);
1382 DeoptimizeIf(zero, instr, "overflow");
1384 // Deoptimize if remainder will not be 0.
1385 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32) &&
1386 divisor != 1 && divisor != -1) {
1387 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
1388 __ test(dividend, Immediate(mask));
1389 DeoptimizeIf(not_zero, instr, "lost precision");
1391 __ Move(result, dividend);
1392 int32_t shift = WhichPowerOf2Abs(divisor);
1394 // The arithmetic shift is always OK, the 'if' is an optimization only.
1395 if (shift > 1) __ sar(result, 31);
1396 __ shr(result, 32 - shift);
1397 __ add(result, dividend);
1398 __ sar(result, shift);
1400 if (divisor < 0) __ neg(result);
1404 void LCodeGen::DoDivByConstI(LDivByConstI* instr) {
1405 Register dividend = ToRegister(instr->dividend());
1406 int32_t divisor = instr->divisor();
1407 DCHECK(ToRegister(instr->result()).is(edx));
1410 DeoptimizeIf(no_condition, instr, "division by zero");
1414 // Check for (0 / -x) that will produce negative zero.
1415 HDiv* hdiv = instr->hydrogen();
1416 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1417 __ test(dividend, dividend);
1418 DeoptimizeIf(zero, instr, "minus zero");
1421 __ TruncatingDiv(dividend, Abs(divisor));
1422 if (divisor < 0) __ neg(edx);
1424 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32)) {
1426 __ imul(eax, eax, divisor);
1427 __ sub(eax, dividend);
1428 DeoptimizeIf(not_equal, instr, "lost precision");
1433 // TODO(svenpanne) Refactor this to avoid code duplication with DoFlooringDivI.
1434 void LCodeGen::DoDivI(LDivI* instr) {
1435 HBinaryOperation* hdiv = instr->hydrogen();
1436 Register dividend = ToRegister(instr->dividend());
1437 Register divisor = ToRegister(instr->divisor());
1438 Register remainder = ToRegister(instr->temp());
1439 DCHECK(dividend.is(eax));
1440 DCHECK(remainder.is(edx));
1441 DCHECK(ToRegister(instr->result()).is(eax));
1442 DCHECK(!divisor.is(eax));
1443 DCHECK(!divisor.is(edx));
1446 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
1447 __ test(divisor, divisor);
1448 DeoptimizeIf(zero, instr, "division by zero");
1451 // Check for (0 / -x) that will produce negative zero.
1452 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
1453 Label dividend_not_zero;
1454 __ test(dividend, dividend);
1455 __ j(not_zero, ÷nd_not_zero, Label::kNear);
1456 __ test(divisor, divisor);
1457 DeoptimizeIf(sign, instr, "minus zero");
1458 __ bind(÷nd_not_zero);
1461 // Check for (kMinInt / -1).
1462 if (hdiv->CheckFlag(HValue::kCanOverflow)) {
1463 Label dividend_not_min_int;
1464 __ cmp(dividend, kMinInt);
1465 __ j(not_zero, ÷nd_not_min_int, Label::kNear);
1466 __ cmp(divisor, -1);
1467 DeoptimizeIf(zero, instr, "overflow");
1468 __ bind(÷nd_not_min_int);
1471 // Sign extend to edx (= remainder).
1475 if (!hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
1476 // Deoptimize if remainder is not 0.
1477 __ test(remainder, remainder);
1478 DeoptimizeIf(not_zero, instr, "lost precision");
1483 void LCodeGen::DoFlooringDivByPowerOf2I(LFlooringDivByPowerOf2I* instr) {
1484 Register dividend = ToRegister(instr->dividend());
1485 int32_t divisor = instr->divisor();
1486 DCHECK(dividend.is(ToRegister(instr->result())));
1488 // If the divisor is positive, things are easy: There can be no deopts and we
1489 // can simply do an arithmetic right shift.
1490 if (divisor == 1) return;
1491 int32_t shift = WhichPowerOf2Abs(divisor);
1493 __ sar(dividend, shift);
1497 // If the divisor is negative, we have to negate and handle edge cases.
1499 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1500 DeoptimizeIf(zero, instr, "minus zero");
1503 // Dividing by -1 is basically negation, unless we overflow.
1504 if (divisor == -1) {
1505 if (instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
1506 DeoptimizeIf(overflow, instr, "overflow");
1511 // If the negation could not overflow, simply shifting is OK.
1512 if (!instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
1513 __ sar(dividend, shift);
1517 Label not_kmin_int, done;
1518 __ j(no_overflow, ¬_kmin_int, Label::kNear);
1519 __ mov(dividend, Immediate(kMinInt / divisor));
1520 __ jmp(&done, Label::kNear);
1521 __ bind(¬_kmin_int);
1522 __ sar(dividend, shift);
1527 void LCodeGen::DoFlooringDivByConstI(LFlooringDivByConstI* instr) {
1528 Register dividend = ToRegister(instr->dividend());
1529 int32_t divisor = instr->divisor();
1530 DCHECK(ToRegister(instr->result()).is(edx));
1533 DeoptimizeIf(no_condition, instr, "division by zero");
1537 // Check for (0 / -x) that will produce negative zero.
1538 HMathFloorOfDiv* hdiv = instr->hydrogen();
1539 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1540 __ test(dividend, dividend);
1541 DeoptimizeIf(zero, instr, "minus zero");
1544 // Easy case: We need no dynamic check for the dividend and the flooring
1545 // division is the same as the truncating division.
1546 if ((divisor > 0 && !hdiv->CheckFlag(HValue::kLeftCanBeNegative)) ||
1547 (divisor < 0 && !hdiv->CheckFlag(HValue::kLeftCanBePositive))) {
1548 __ TruncatingDiv(dividend, Abs(divisor));
1549 if (divisor < 0) __ neg(edx);
1553 // In the general case we may need to adjust before and after the truncating
1554 // division to get a flooring division.
1555 Register temp = ToRegister(instr->temp3());
1556 DCHECK(!temp.is(dividend) && !temp.is(eax) && !temp.is(edx));
1557 Label needs_adjustment, done;
1558 __ cmp(dividend, Immediate(0));
1559 __ j(divisor > 0 ? less : greater, &needs_adjustment, Label::kNear);
1560 __ TruncatingDiv(dividend, Abs(divisor));
1561 if (divisor < 0) __ neg(edx);
1562 __ jmp(&done, Label::kNear);
1563 __ bind(&needs_adjustment);
1564 __ lea(temp, Operand(dividend, divisor > 0 ? 1 : -1));
1565 __ TruncatingDiv(temp, Abs(divisor));
1566 if (divisor < 0) __ neg(edx);
1572 // TODO(svenpanne) Refactor this to avoid code duplication with DoDivI.
1573 void LCodeGen::DoFlooringDivI(LFlooringDivI* instr) {
1574 HBinaryOperation* hdiv = instr->hydrogen();
1575 Register dividend = ToRegister(instr->dividend());
1576 Register divisor = ToRegister(instr->divisor());
1577 Register remainder = ToRegister(instr->temp());
1578 Register result = ToRegister(instr->result());
1579 DCHECK(dividend.is(eax));
1580 DCHECK(remainder.is(edx));
1581 DCHECK(result.is(eax));
1582 DCHECK(!divisor.is(eax));
1583 DCHECK(!divisor.is(edx));
1586 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
1587 __ test(divisor, divisor);
1588 DeoptimizeIf(zero, instr, "division by zero");
1591 // Check for (0 / -x) that will produce negative zero.
1592 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
1593 Label dividend_not_zero;
1594 __ test(dividend, dividend);
1595 __ j(not_zero, ÷nd_not_zero, Label::kNear);
1596 __ test(divisor, divisor);
1597 DeoptimizeIf(sign, instr, "minus zero");
1598 __ bind(÷nd_not_zero);
1601 // Check for (kMinInt / -1).
1602 if (hdiv->CheckFlag(HValue::kCanOverflow)) {
1603 Label dividend_not_min_int;
1604 __ cmp(dividend, kMinInt);
1605 __ j(not_zero, ÷nd_not_min_int, Label::kNear);
1606 __ cmp(divisor, -1);
1607 DeoptimizeIf(zero, instr, "overflow");
1608 __ bind(÷nd_not_min_int);
1611 // Sign extend to edx (= remainder).
1616 __ test(remainder, remainder);
1617 __ j(zero, &done, Label::kNear);
1618 __ xor_(remainder, divisor);
1619 __ sar(remainder, 31);
1620 __ add(result, remainder);
1625 void LCodeGen::DoMulI(LMulI* instr) {
1626 Register left = ToRegister(instr->left());
1627 LOperand* right = instr->right();
1629 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1630 __ mov(ToRegister(instr->temp()), left);
1633 if (right->IsConstantOperand()) {
1634 // Try strength reductions on the multiplication.
1635 // All replacement instructions are at most as long as the imul
1636 // and have better latency.
1637 int constant = ToInteger32(LConstantOperand::cast(right));
1638 if (constant == -1) {
1640 } else if (constant == 0) {
1641 __ xor_(left, Operand(left));
1642 } else if (constant == 2) {
1643 __ add(left, Operand(left));
1644 } else if (!instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
1645 // If we know that the multiplication can't overflow, it's safe to
1646 // use instructions that don't set the overflow flag for the
1653 __ lea(left, Operand(left, left, times_2, 0));
1659 __ lea(left, Operand(left, left, times_4, 0));
1665 __ lea(left, Operand(left, left, times_8, 0));
1671 __ imul(left, left, constant);
1675 __ imul(left, left, constant);
1678 if (instr->hydrogen()->representation().IsSmi()) {
1681 __ imul(left, ToOperand(right));
1684 if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
1685 DeoptimizeIf(overflow, instr, "overflow");
1688 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1689 // Bail out if the result is supposed to be negative zero.
1691 __ test(left, Operand(left));
1692 __ j(not_zero, &done, Label::kNear);
1693 if (right->IsConstantOperand()) {
1694 if (ToInteger32(LConstantOperand::cast(right)) < 0) {
1695 DeoptimizeIf(no_condition, instr, "minus zero");
1696 } else if (ToInteger32(LConstantOperand::cast(right)) == 0) {
1697 __ cmp(ToRegister(instr->temp()), Immediate(0));
1698 DeoptimizeIf(less, instr, "minus zero");
1701 // Test the non-zero operand for negative sign.
1702 __ or_(ToRegister(instr->temp()), ToOperand(right));
1703 DeoptimizeIf(sign, instr, "minus zero");
1710 void LCodeGen::DoBitI(LBitI* instr) {
1711 LOperand* left = instr->left();
1712 LOperand* right = instr->right();
1713 DCHECK(left->Equals(instr->result()));
1714 DCHECK(left->IsRegister());
1716 if (right->IsConstantOperand()) {
1717 int32_t right_operand =
1718 ToRepresentation(LConstantOperand::cast(right),
1719 instr->hydrogen()->representation());
1720 switch (instr->op()) {
1721 case Token::BIT_AND:
1722 __ and_(ToRegister(left), right_operand);
1725 __ or_(ToRegister(left), right_operand);
1727 case Token::BIT_XOR:
1728 if (right_operand == int32_t(~0)) {
1729 __ not_(ToRegister(left));
1731 __ xor_(ToRegister(left), right_operand);
1739 switch (instr->op()) {
1740 case Token::BIT_AND:
1741 __ and_(ToRegister(left), ToOperand(right));
1744 __ or_(ToRegister(left), ToOperand(right));
1746 case Token::BIT_XOR:
1747 __ xor_(ToRegister(left), ToOperand(right));
1757 void LCodeGen::DoShiftI(LShiftI* instr) {
1758 LOperand* left = instr->left();
1759 LOperand* right = instr->right();
1760 DCHECK(left->Equals(instr->result()));
1761 DCHECK(left->IsRegister());
1762 if (right->IsRegister()) {
1763 DCHECK(ToRegister(right).is(ecx));
1765 switch (instr->op()) {
1767 __ ror_cl(ToRegister(left));
1770 __ sar_cl(ToRegister(left));
1773 __ shr_cl(ToRegister(left));
1774 if (instr->can_deopt()) {
1775 __ test(ToRegister(left), ToRegister(left));
1776 DeoptimizeIf(sign, instr, "negative value");
1780 __ shl_cl(ToRegister(left));
1787 int value = ToInteger32(LConstantOperand::cast(right));
1788 uint8_t shift_count = static_cast<uint8_t>(value & 0x1F);
1789 switch (instr->op()) {
1791 if (shift_count == 0 && instr->can_deopt()) {
1792 __ test(ToRegister(left), ToRegister(left));
1793 DeoptimizeIf(sign, instr, "negative value");
1795 __ ror(ToRegister(left), shift_count);
1799 if (shift_count != 0) {
1800 __ sar(ToRegister(left), shift_count);
1804 if (shift_count != 0) {
1805 __ shr(ToRegister(left), shift_count);
1806 } else if (instr->can_deopt()) {
1807 __ test(ToRegister(left), ToRegister(left));
1808 DeoptimizeIf(sign, instr, "negative value");
1812 if (shift_count != 0) {
1813 if (instr->hydrogen_value()->representation().IsSmi() &&
1814 instr->can_deopt()) {
1815 if (shift_count != 1) {
1816 __ shl(ToRegister(left), shift_count - 1);
1818 __ SmiTag(ToRegister(left));
1819 DeoptimizeIf(overflow, instr, "overflow");
1821 __ shl(ToRegister(left), shift_count);
1833 void LCodeGen::DoSubI(LSubI* instr) {
1834 LOperand* left = instr->left();
1835 LOperand* right = instr->right();
1836 DCHECK(left->Equals(instr->result()));
1838 if (right->IsConstantOperand()) {
1839 __ sub(ToOperand(left),
1840 ToImmediate(right, instr->hydrogen()->representation()));
1842 __ sub(ToRegister(left), ToOperand(right));
1844 if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
1845 DeoptimizeIf(overflow, instr, "overflow");
1850 void LCodeGen::DoConstantI(LConstantI* instr) {
1851 __ Move(ToRegister(instr->result()), Immediate(instr->value()));
1855 void LCodeGen::DoConstantS(LConstantS* instr) {
1856 __ Move(ToRegister(instr->result()), Immediate(instr->value()));
1860 void LCodeGen::DoConstantD(LConstantD* instr) {
1861 double v = instr->value();
1862 uint64_t int_val = bit_cast<uint64_t, double>(v);
1863 int32_t lower = static_cast<int32_t>(int_val);
1864 int32_t upper = static_cast<int32_t>(int_val >> (kBitsPerInt));
1865 DCHECK(instr->result()->IsDoubleRegister());
1867 XMMRegister res = ToDoubleRegister(instr->result());
1871 Register temp = ToRegister(instr->temp());
1872 if (CpuFeatures::IsSupported(SSE4_1)) {
1873 CpuFeatureScope scope2(masm(), SSE4_1);
1875 __ Move(temp, Immediate(lower));
1876 __ movd(res, Operand(temp));
1877 __ Move(temp, Immediate(upper));
1878 __ pinsrd(res, Operand(temp), 1);
1881 __ Move(temp, Immediate(upper));
1882 __ pinsrd(res, Operand(temp), 1);
1885 __ Move(temp, Immediate(upper));
1886 __ movd(res, Operand(temp));
1889 XMMRegister xmm_scratch = double_scratch0();
1890 __ Move(temp, Immediate(lower));
1891 __ movd(xmm_scratch, Operand(temp));
1892 __ orps(res, xmm_scratch);
1899 void LCodeGen::DoConstantE(LConstantE* instr) {
1900 __ lea(ToRegister(instr->result()), Operand::StaticVariable(instr->value()));
1904 void LCodeGen::DoConstantT(LConstantT* instr) {
1905 Register reg = ToRegister(instr->result());
1906 Handle<Object> object = instr->value(isolate());
1907 AllowDeferredHandleDereference smi_check;
1908 __ LoadObject(reg, object);
1912 void LCodeGen::DoMapEnumLength(LMapEnumLength* instr) {
1913 Register result = ToRegister(instr->result());
1914 Register map = ToRegister(instr->value());
1915 __ EnumLength(result, map);
1919 void LCodeGen::DoDateField(LDateField* instr) {
1920 Register object = ToRegister(instr->date());
1921 Register result = ToRegister(instr->result());
1922 Register scratch = ToRegister(instr->temp());
1923 Smi* index = instr->index();
1924 Label runtime, done;
1925 DCHECK(object.is(result));
1926 DCHECK(object.is(eax));
1928 __ test(object, Immediate(kSmiTagMask));
1929 DeoptimizeIf(zero, instr, "Smi");
1930 __ CmpObjectType(object, JS_DATE_TYPE, scratch);
1931 DeoptimizeIf(not_equal, instr, "not a date object");
1933 if (index->value() == 0) {
1934 __ mov(result, FieldOperand(object, JSDate::kValueOffset));
1936 if (index->value() < JSDate::kFirstUncachedField) {
1937 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
1938 __ mov(scratch, Operand::StaticVariable(stamp));
1939 __ cmp(scratch, FieldOperand(object, JSDate::kCacheStampOffset));
1940 __ j(not_equal, &runtime, Label::kNear);
1941 __ mov(result, FieldOperand(object, JSDate::kValueOffset +
1942 kPointerSize * index->value()));
1943 __ jmp(&done, Label::kNear);
1946 __ PrepareCallCFunction(2, scratch);
1947 __ mov(Operand(esp, 0), object);
1948 __ mov(Operand(esp, 1 * kPointerSize), Immediate(index));
1949 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
1955 Operand LCodeGen::BuildSeqStringOperand(Register string,
1957 String::Encoding encoding) {
1958 if (index->IsConstantOperand()) {
1959 int offset = ToRepresentation(LConstantOperand::cast(index),
1960 Representation::Integer32());
1961 if (encoding == String::TWO_BYTE_ENCODING) {
1962 offset *= kUC16Size;
1964 STATIC_ASSERT(kCharSize == 1);
1965 return FieldOperand(string, SeqString::kHeaderSize + offset);
1967 return FieldOperand(
1968 string, ToRegister(index),
1969 encoding == String::ONE_BYTE_ENCODING ? times_1 : times_2,
1970 SeqString::kHeaderSize);
1974 void LCodeGen::DoSeqStringGetChar(LSeqStringGetChar* instr) {
1975 String::Encoding encoding = instr->hydrogen()->encoding();
1976 Register result = ToRegister(instr->result());
1977 Register string = ToRegister(instr->string());
1979 if (FLAG_debug_code) {
1981 __ mov(string, FieldOperand(string, HeapObject::kMapOffset));
1982 __ movzx_b(string, FieldOperand(string, Map::kInstanceTypeOffset));
1984 __ and_(string, Immediate(kStringRepresentationMask | kStringEncodingMask));
1985 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
1986 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
1987 __ cmp(string, Immediate(encoding == String::ONE_BYTE_ENCODING
1988 ? one_byte_seq_type : two_byte_seq_type));
1989 __ Check(equal, kUnexpectedStringType);
1993 Operand operand = BuildSeqStringOperand(string, instr->index(), encoding);
1994 if (encoding == String::ONE_BYTE_ENCODING) {
1995 __ movzx_b(result, operand);
1997 __ movzx_w(result, operand);
2002 void LCodeGen::DoSeqStringSetChar(LSeqStringSetChar* instr) {
2003 String::Encoding encoding = instr->hydrogen()->encoding();
2004 Register string = ToRegister(instr->string());
2006 if (FLAG_debug_code) {
2007 Register value = ToRegister(instr->value());
2008 Register index = ToRegister(instr->index());
2009 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
2010 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
2012 instr->hydrogen()->encoding() == String::ONE_BYTE_ENCODING
2013 ? one_byte_seq_type : two_byte_seq_type;
2014 __ EmitSeqStringSetCharCheck(string, index, value, encoding_mask);
2017 Operand operand = BuildSeqStringOperand(string, instr->index(), encoding);
2018 if (instr->value()->IsConstantOperand()) {
2019 int value = ToRepresentation(LConstantOperand::cast(instr->value()),
2020 Representation::Integer32());
2021 DCHECK_LE(0, value);
2022 if (encoding == String::ONE_BYTE_ENCODING) {
2023 DCHECK_LE(value, String::kMaxOneByteCharCode);
2024 __ mov_b(operand, static_cast<int8_t>(value));
2026 DCHECK_LE(value, String::kMaxUtf16CodeUnit);
2027 __ mov_w(operand, static_cast<int16_t>(value));
2030 Register value = ToRegister(instr->value());
2031 if (encoding == String::ONE_BYTE_ENCODING) {
2032 __ mov_b(operand, value);
2034 __ mov_w(operand, value);
2040 void LCodeGen::DoAddI(LAddI* instr) {
2041 LOperand* left = instr->left();
2042 LOperand* right = instr->right();
2044 if (LAddI::UseLea(instr->hydrogen()) && !left->Equals(instr->result())) {
2045 if (right->IsConstantOperand()) {
2046 int32_t offset = ToRepresentation(LConstantOperand::cast(right),
2047 instr->hydrogen()->representation());
2048 __ lea(ToRegister(instr->result()), MemOperand(ToRegister(left), offset));
2050 Operand address(ToRegister(left), ToRegister(right), times_1, 0);
2051 __ lea(ToRegister(instr->result()), address);
2054 if (right->IsConstantOperand()) {
2055 __ add(ToOperand(left),
2056 ToImmediate(right, instr->hydrogen()->representation()));
2058 __ add(ToRegister(left), ToOperand(right));
2060 if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
2061 DeoptimizeIf(overflow, instr, "overflow");
2067 void LCodeGen::DoMathMinMax(LMathMinMax* instr) {
2068 LOperand* left = instr->left();
2069 LOperand* right = instr->right();
2070 DCHECK(left->Equals(instr->result()));
2071 HMathMinMax::Operation operation = instr->hydrogen()->operation();
2072 if (instr->hydrogen()->representation().IsSmiOrInteger32()) {
2074 Condition condition = (operation == HMathMinMax::kMathMin)
2077 if (right->IsConstantOperand()) {
2078 Operand left_op = ToOperand(left);
2079 Immediate immediate = ToImmediate(LConstantOperand::cast(instr->right()),
2080 instr->hydrogen()->representation());
2081 __ cmp(left_op, immediate);
2082 __ j(condition, &return_left, Label::kNear);
2083 __ mov(left_op, immediate);
2085 Register left_reg = ToRegister(left);
2086 Operand right_op = ToOperand(right);
2087 __ cmp(left_reg, right_op);
2088 __ j(condition, &return_left, Label::kNear);
2089 __ mov(left_reg, right_op);
2091 __ bind(&return_left);
2093 DCHECK(instr->hydrogen()->representation().IsDouble());
2094 Label check_nan_left, check_zero, return_left, return_right;
2095 Condition condition = (operation == HMathMinMax::kMathMin) ? below : above;
2096 XMMRegister left_reg = ToDoubleRegister(left);
2097 XMMRegister right_reg = ToDoubleRegister(right);
2098 __ ucomisd(left_reg, right_reg);
2099 __ j(parity_even, &check_nan_left, Label::kNear); // At least one NaN.
2100 __ j(equal, &check_zero, Label::kNear); // left == right.
2101 __ j(condition, &return_left, Label::kNear);
2102 __ jmp(&return_right, Label::kNear);
2104 __ bind(&check_zero);
2105 XMMRegister xmm_scratch = double_scratch0();
2106 __ xorps(xmm_scratch, xmm_scratch);
2107 __ ucomisd(left_reg, xmm_scratch);
2108 __ j(not_equal, &return_left, Label::kNear); // left == right != 0.
2109 // At this point, both left and right are either 0 or -0.
2110 if (operation == HMathMinMax::kMathMin) {
2111 __ orpd(left_reg, right_reg);
2113 // Since we operate on +0 and/or -0, addsd and andsd have the same effect.
2114 __ addsd(left_reg, right_reg);
2116 __ jmp(&return_left, Label::kNear);
2118 __ bind(&check_nan_left);
2119 __ ucomisd(left_reg, left_reg); // NaN check.
2120 __ j(parity_even, &return_left, Label::kNear); // left == NaN.
2121 __ bind(&return_right);
2122 __ movaps(left_reg, right_reg);
2124 __ bind(&return_left);
2129 void LCodeGen::DoArithmeticD(LArithmeticD* instr) {
2130 XMMRegister left = ToDoubleRegister(instr->left());
2131 XMMRegister right = ToDoubleRegister(instr->right());
2132 XMMRegister result = ToDoubleRegister(instr->result());
2133 switch (instr->op()) {
2135 __ addsd(left, right);
2138 __ subsd(left, right);
2141 __ mulsd(left, right);
2144 __ divsd(left, right);
2145 // Don't delete this mov. It may improve performance on some CPUs,
2146 // when there is a mulsd depending on the result
2147 __ movaps(left, left);
2150 // Pass two doubles as arguments on the stack.
2151 __ PrepareCallCFunction(4, eax);
2152 __ movsd(Operand(esp, 0 * kDoubleSize), left);
2153 __ movsd(Operand(esp, 1 * kDoubleSize), right);
2155 ExternalReference::mod_two_doubles_operation(isolate()),
2158 // Return value is in st(0) on ia32.
2159 // Store it into the result register.
2160 __ sub(Operand(esp), Immediate(kDoubleSize));
2161 __ fstp_d(Operand(esp, 0));
2162 __ movsd(result, Operand(esp, 0));
2163 __ add(Operand(esp), Immediate(kDoubleSize));
2173 void LCodeGen::DoArithmeticT(LArithmeticT* instr) {
2174 DCHECK(ToRegister(instr->context()).is(esi));
2175 DCHECK(ToRegister(instr->left()).is(edx));
2176 DCHECK(ToRegister(instr->right()).is(eax));
2177 DCHECK(ToRegister(instr->result()).is(eax));
2180 CodeFactory::BinaryOpIC(isolate(), instr->op(), NO_OVERWRITE).code();
2181 CallCode(code, RelocInfo::CODE_TARGET, instr);
2185 template<class InstrType>
2186 void LCodeGen::EmitBranch(InstrType instr, Condition cc) {
2187 int left_block = instr->TrueDestination(chunk_);
2188 int right_block = instr->FalseDestination(chunk_);
2190 int next_block = GetNextEmittedBlock();
2192 if (right_block == left_block || cc == no_condition) {
2193 EmitGoto(left_block);
2194 } else if (left_block == next_block) {
2195 __ j(NegateCondition(cc), chunk_->GetAssemblyLabel(right_block));
2196 } else if (right_block == next_block) {
2197 __ j(cc, chunk_->GetAssemblyLabel(left_block));
2199 __ j(cc, chunk_->GetAssemblyLabel(left_block));
2200 __ jmp(chunk_->GetAssemblyLabel(right_block));
2205 template<class InstrType>
2206 void LCodeGen::EmitFalseBranch(InstrType instr, Condition cc) {
2207 int false_block = instr->FalseDestination(chunk_);
2208 if (cc == no_condition) {
2209 __ jmp(chunk_->GetAssemblyLabel(false_block));
2211 __ j(cc, chunk_->GetAssemblyLabel(false_block));
2216 void LCodeGen::DoBranch(LBranch* instr) {
2217 Representation r = instr->hydrogen()->value()->representation();
2218 if (r.IsSmiOrInteger32()) {
2219 Register reg = ToRegister(instr->value());
2220 __ test(reg, Operand(reg));
2221 EmitBranch(instr, not_zero);
2222 } else if (r.IsDouble()) {
2223 DCHECK(!info()->IsStub());
2224 XMMRegister reg = ToDoubleRegister(instr->value());
2225 XMMRegister xmm_scratch = double_scratch0();
2226 __ xorps(xmm_scratch, xmm_scratch);
2227 __ ucomisd(reg, xmm_scratch);
2228 EmitBranch(instr, not_equal);
2229 } else if (r.IsSIMD128()) {
2230 DCHECK(!info()->IsStub());
2231 EmitBranch(instr, no_condition);
2233 DCHECK(r.IsTagged());
2234 Register reg = ToRegister(instr->value());
2235 HType type = instr->hydrogen()->value()->type();
2236 if (type.IsBoolean()) {
2237 DCHECK(!info()->IsStub());
2238 __ cmp(reg, factory()->true_value());
2239 EmitBranch(instr, equal);
2240 } else if (type.IsSmi()) {
2241 DCHECK(!info()->IsStub());
2242 __ test(reg, Operand(reg));
2243 EmitBranch(instr, not_equal);
2244 } else if (type.IsJSArray()) {
2245 DCHECK(!info()->IsStub());
2246 EmitBranch(instr, no_condition);
2247 } else if (type.IsHeapNumber()) {
2248 DCHECK(!info()->IsStub());
2249 XMMRegister xmm_scratch = double_scratch0();
2250 __ xorps(xmm_scratch, xmm_scratch);
2251 __ ucomisd(xmm_scratch, FieldOperand(reg, HeapNumber::kValueOffset));
2252 EmitBranch(instr, not_equal);
2253 } else if (type.IsString()) {
2254 DCHECK(!info()->IsStub());
2255 __ cmp(FieldOperand(reg, String::kLengthOffset), Immediate(0));
2256 EmitBranch(instr, not_equal);
2258 ToBooleanStub::Types expected = instr->hydrogen()->expected_input_types();
2259 if (expected.IsEmpty()) expected = ToBooleanStub::Types::Generic();
2261 if (expected.Contains(ToBooleanStub::UNDEFINED)) {
2262 // undefined -> false.
2263 __ cmp(reg, factory()->undefined_value());
2264 __ j(equal, instr->FalseLabel(chunk_));
2266 if (expected.Contains(ToBooleanStub::BOOLEAN)) {
2268 __ cmp(reg, factory()->true_value());
2269 __ j(equal, instr->TrueLabel(chunk_));
2271 __ cmp(reg, factory()->false_value());
2272 __ j(equal, instr->FalseLabel(chunk_));
2274 if (expected.Contains(ToBooleanStub::NULL_TYPE)) {
2276 __ cmp(reg, factory()->null_value());
2277 __ j(equal, instr->FalseLabel(chunk_));
2280 if (expected.Contains(ToBooleanStub::SMI)) {
2281 // Smis: 0 -> false, all other -> true.
2282 __ test(reg, Operand(reg));
2283 __ j(equal, instr->FalseLabel(chunk_));
2284 __ JumpIfSmi(reg, instr->TrueLabel(chunk_));
2285 } else if (expected.NeedsMap()) {
2286 // If we need a map later and have a Smi -> deopt.
2287 __ test(reg, Immediate(kSmiTagMask));
2288 DeoptimizeIf(zero, instr, "Smi");
2291 Register map = no_reg; // Keep the compiler happy.
2292 if (expected.NeedsMap()) {
2293 map = ToRegister(instr->temp());
2294 DCHECK(!map.is(reg));
2295 __ mov(map, FieldOperand(reg, HeapObject::kMapOffset));
2297 if (expected.CanBeUndetectable()) {
2298 // Undetectable -> false.
2299 __ test_b(FieldOperand(map, Map::kBitFieldOffset),
2300 1 << Map::kIsUndetectable);
2301 __ j(not_zero, instr->FalseLabel(chunk_));
2305 if (expected.Contains(ToBooleanStub::SPEC_OBJECT)) {
2306 // spec object -> true.
2307 __ CmpInstanceType(map, FIRST_SPEC_OBJECT_TYPE);
2308 __ j(above_equal, instr->TrueLabel(chunk_));
2311 if (expected.Contains(ToBooleanStub::STRING)) {
2312 // String value -> false iff empty.
2314 __ CmpInstanceType(map, FIRST_NONSTRING_TYPE);
2315 __ j(above_equal, ¬_string, Label::kNear);
2316 __ cmp(FieldOperand(reg, String::kLengthOffset), Immediate(0));
2317 __ j(not_zero, instr->TrueLabel(chunk_));
2318 __ jmp(instr->FalseLabel(chunk_));
2319 __ bind(¬_string);
2322 if (expected.Contains(ToBooleanStub::SYMBOL)) {
2323 // Symbol value -> true.
2324 __ CmpInstanceType(map, SYMBOL_TYPE);
2325 __ j(equal, instr->TrueLabel(chunk_));
2328 if (expected.Contains(ToBooleanStub::HEAP_NUMBER)) {
2329 // heap number -> false iff +0, -0, or NaN.
2330 Label not_heap_number;
2331 __ cmp(FieldOperand(reg, HeapObject::kMapOffset),
2332 factory()->heap_number_map());
2333 __ j(not_equal, ¬_heap_number, Label::kNear);
2334 XMMRegister xmm_scratch = double_scratch0();
2335 __ xorps(xmm_scratch, xmm_scratch);
2336 __ ucomisd(xmm_scratch, FieldOperand(reg, HeapNumber::kValueOffset));
2337 __ j(zero, instr->FalseLabel(chunk_));
2338 __ jmp(instr->TrueLabel(chunk_));
2339 __ bind(¬_heap_number);
2342 if (!expected.IsGeneric()) {
2343 // We've seen something for the first time -> deopt.
2344 // This can only happen if we are not generic already.
2345 DeoptimizeIf(no_condition, instr, "unexpected object");
2352 void LCodeGen::EmitGoto(int block) {
2353 if (!IsNextEmittedBlock(block)) {
2354 __ jmp(chunk_->GetAssemblyLabel(LookupDestination(block)));
2359 void LCodeGen::DoGoto(LGoto* instr) {
2360 EmitGoto(instr->block_id());
2364 Condition LCodeGen::TokenToCondition(Token::Value op, bool is_unsigned) {
2365 Condition cond = no_condition;
2368 case Token::EQ_STRICT:
2372 case Token::NE_STRICT:
2376 cond = is_unsigned ? below : less;
2379 cond = is_unsigned ? above : greater;
2382 cond = is_unsigned ? below_equal : less_equal;
2385 cond = is_unsigned ? above_equal : greater_equal;
2388 case Token::INSTANCEOF:
2396 void LCodeGen::DoCompareNumericAndBranch(LCompareNumericAndBranch* instr) {
2397 LOperand* left = instr->left();
2398 LOperand* right = instr->right();
2400 instr->is_double() ||
2401 instr->hydrogen()->left()->CheckFlag(HInstruction::kUint32) ||
2402 instr->hydrogen()->right()->CheckFlag(HInstruction::kUint32);
2403 Condition cc = TokenToCondition(instr->op(), is_unsigned);
2405 if (left->IsConstantOperand() && right->IsConstantOperand()) {
2406 // We can statically evaluate the comparison.
2407 double left_val = ToDouble(LConstantOperand::cast(left));
2408 double right_val = ToDouble(LConstantOperand::cast(right));
2409 int next_block = EvalComparison(instr->op(), left_val, right_val) ?
2410 instr->TrueDestination(chunk_) : instr->FalseDestination(chunk_);
2411 EmitGoto(next_block);
2413 if (instr->is_double()) {
2414 __ ucomisd(ToDoubleRegister(left), ToDoubleRegister(right));
2415 // Don't base result on EFLAGS when a NaN is involved. Instead
2416 // jump to the false block.
2417 __ j(parity_even, instr->FalseLabel(chunk_));
2419 if (right->IsConstantOperand()) {
2420 __ cmp(ToOperand(left),
2421 ToImmediate(right, instr->hydrogen()->representation()));
2422 } else if (left->IsConstantOperand()) {
2423 __ cmp(ToOperand(right),
2424 ToImmediate(left, instr->hydrogen()->representation()));
2425 // We commuted the operands, so commute the condition.
2426 cc = CommuteCondition(cc);
2428 __ cmp(ToRegister(left), ToOperand(right));
2431 EmitBranch(instr, cc);
2436 void LCodeGen::DoCmpObjectEqAndBranch(LCmpObjectEqAndBranch* instr) {
2437 Register left = ToRegister(instr->left());
2439 if (instr->right()->IsConstantOperand()) {
2440 Handle<Object> right = ToHandle(LConstantOperand::cast(instr->right()));
2441 __ CmpObject(left, right);
2443 Operand right = ToOperand(instr->right());
2444 __ cmp(left, right);
2446 EmitBranch(instr, equal);
2450 void LCodeGen::DoCmpHoleAndBranch(LCmpHoleAndBranch* instr) {
2451 if (instr->hydrogen()->representation().IsTagged()) {
2452 Register input_reg = ToRegister(instr->object());
2453 __ cmp(input_reg, factory()->the_hole_value());
2454 EmitBranch(instr, equal);
2458 XMMRegister input_reg = ToDoubleRegister(instr->object());
2459 __ ucomisd(input_reg, input_reg);
2460 EmitFalseBranch(instr, parity_odd);
2462 __ sub(esp, Immediate(kDoubleSize));
2463 __ movsd(MemOperand(esp, 0), input_reg);
2465 __ add(esp, Immediate(kDoubleSize));
2466 int offset = sizeof(kHoleNanUpper32);
2467 __ cmp(MemOperand(esp, -offset), Immediate(kHoleNanUpper32));
2468 EmitBranch(instr, equal);
2472 void LCodeGen::DoCompareMinusZeroAndBranch(LCompareMinusZeroAndBranch* instr) {
2473 Representation rep = instr->hydrogen()->value()->representation();
2474 DCHECK(!rep.IsInteger32());
2475 Register scratch = ToRegister(instr->temp());
2477 if (rep.IsDouble()) {
2478 XMMRegister value = ToDoubleRegister(instr->value());
2479 XMMRegister xmm_scratch = double_scratch0();
2480 __ xorps(xmm_scratch, xmm_scratch);
2481 __ ucomisd(xmm_scratch, value);
2482 EmitFalseBranch(instr, not_equal);
2483 __ movmskpd(scratch, value);
2484 __ test(scratch, Immediate(1));
2485 EmitBranch(instr, not_zero);
2487 Register value = ToRegister(instr->value());
2488 Handle<Map> map = masm()->isolate()->factory()->heap_number_map();
2489 __ CheckMap(value, map, instr->FalseLabel(chunk()), DO_SMI_CHECK);
2490 __ cmp(FieldOperand(value, HeapNumber::kExponentOffset),
2492 EmitFalseBranch(instr, no_overflow);
2493 __ cmp(FieldOperand(value, HeapNumber::kMantissaOffset),
2494 Immediate(0x00000000));
2495 EmitBranch(instr, equal);
2500 Condition LCodeGen::EmitIsObject(Register input,
2502 Label* is_not_object,
2504 __ JumpIfSmi(input, is_not_object);
2506 __ cmp(input, isolate()->factory()->null_value());
2507 __ j(equal, is_object);
2509 __ mov(temp1, FieldOperand(input, HeapObject::kMapOffset));
2510 // Undetectable objects behave like undefined.
2511 __ test_b(FieldOperand(temp1, Map::kBitFieldOffset),
2512 1 << Map::kIsUndetectable);
2513 __ j(not_zero, is_not_object);
2515 __ movzx_b(temp1, FieldOperand(temp1, Map::kInstanceTypeOffset));
2516 __ cmp(temp1, FIRST_NONCALLABLE_SPEC_OBJECT_TYPE);
2517 __ j(below, is_not_object);
2518 __ cmp(temp1, LAST_NONCALLABLE_SPEC_OBJECT_TYPE);
2523 void LCodeGen::DoIsObjectAndBranch(LIsObjectAndBranch* instr) {
2524 Register reg = ToRegister(instr->value());
2525 Register temp = ToRegister(instr->temp());
2527 Condition true_cond = EmitIsObject(
2528 reg, temp, instr->FalseLabel(chunk_), instr->TrueLabel(chunk_));
2530 EmitBranch(instr, true_cond);
2534 Condition LCodeGen::EmitIsString(Register input,
2536 Label* is_not_string,
2537 SmiCheck check_needed = INLINE_SMI_CHECK) {
2538 if (check_needed == INLINE_SMI_CHECK) {
2539 __ JumpIfSmi(input, is_not_string);
2542 Condition cond = masm_->IsObjectStringType(input, temp1, temp1);
2548 void LCodeGen::DoIsStringAndBranch(LIsStringAndBranch* instr) {
2549 Register reg = ToRegister(instr->value());
2550 Register temp = ToRegister(instr->temp());
2552 SmiCheck check_needed =
2553 instr->hydrogen()->value()->type().IsHeapObject()
2554 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
2556 Condition true_cond = EmitIsString(
2557 reg, temp, instr->FalseLabel(chunk_), check_needed);
2559 EmitBranch(instr, true_cond);
2563 void LCodeGen::DoIsSmiAndBranch(LIsSmiAndBranch* instr) {
2564 Operand input = ToOperand(instr->value());
2566 __ test(input, Immediate(kSmiTagMask));
2567 EmitBranch(instr, zero);
2571 void LCodeGen::DoIsUndetectableAndBranch(LIsUndetectableAndBranch* instr) {
2572 Register input = ToRegister(instr->value());
2573 Register temp = ToRegister(instr->temp());
2575 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2576 STATIC_ASSERT(kSmiTag == 0);
2577 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2579 __ mov(temp, FieldOperand(input, HeapObject::kMapOffset));
2580 __ test_b(FieldOperand(temp, Map::kBitFieldOffset),
2581 1 << Map::kIsUndetectable);
2582 EmitBranch(instr, not_zero);
2586 static Condition ComputeCompareCondition(Token::Value op) {
2588 case Token::EQ_STRICT:
2598 return greater_equal;
2601 return no_condition;
2606 void LCodeGen::DoStringCompareAndBranch(LStringCompareAndBranch* instr) {
2607 Token::Value op = instr->op();
2609 Handle<Code> ic = CodeFactory::CompareIC(isolate(), op).code();
2610 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2612 Condition condition = ComputeCompareCondition(op);
2613 __ test(eax, Operand(eax));
2615 EmitBranch(instr, condition);
2619 static InstanceType TestType(HHasInstanceTypeAndBranch* instr) {
2620 InstanceType from = instr->from();
2621 InstanceType to = instr->to();
2622 if (from == FIRST_TYPE) return to;
2623 DCHECK(from == to || to == LAST_TYPE);
2628 static Condition BranchCondition(HHasInstanceTypeAndBranch* instr) {
2629 InstanceType from = instr->from();
2630 InstanceType to = instr->to();
2631 if (from == to) return equal;
2632 if (to == LAST_TYPE) return above_equal;
2633 if (from == FIRST_TYPE) return below_equal;
2639 void LCodeGen::DoHasInstanceTypeAndBranch(LHasInstanceTypeAndBranch* instr) {
2640 Register input = ToRegister(instr->value());
2641 Register temp = ToRegister(instr->temp());
2643 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2644 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2647 __ CmpObjectType(input, TestType(instr->hydrogen()), temp);
2648 EmitBranch(instr, BranchCondition(instr->hydrogen()));
2652 void LCodeGen::DoGetCachedArrayIndex(LGetCachedArrayIndex* instr) {
2653 Register input = ToRegister(instr->value());
2654 Register result = ToRegister(instr->result());
2656 __ AssertString(input);
2658 __ mov(result, FieldOperand(input, String::kHashFieldOffset));
2659 __ IndexFromHash(result, result);
2663 void LCodeGen::DoHasCachedArrayIndexAndBranch(
2664 LHasCachedArrayIndexAndBranch* instr) {
2665 Register input = ToRegister(instr->value());
2667 __ test(FieldOperand(input, String::kHashFieldOffset),
2668 Immediate(String::kContainsCachedArrayIndexMask));
2669 EmitBranch(instr, equal);
2673 // Branches to a label or falls through with the answer in the z flag. Trashes
2674 // the temp registers, but not the input.
2675 void LCodeGen::EmitClassOfTest(Label* is_true,
2677 Handle<String>class_name,
2681 DCHECK(!input.is(temp));
2682 DCHECK(!input.is(temp2));
2683 DCHECK(!temp.is(temp2));
2684 __ JumpIfSmi(input, is_false);
2686 if (String::Equals(isolate()->factory()->Function_string(), class_name)) {
2687 // Assuming the following assertions, we can use the same compares to test
2688 // for both being a function type and being in the object type range.
2689 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
2690 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2691 FIRST_SPEC_OBJECT_TYPE + 1);
2692 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2693 LAST_SPEC_OBJECT_TYPE - 1);
2694 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
2695 __ CmpObjectType(input, FIRST_SPEC_OBJECT_TYPE, temp);
2696 __ j(below, is_false);
2697 __ j(equal, is_true);
2698 __ CmpInstanceType(temp, LAST_SPEC_OBJECT_TYPE);
2699 __ j(equal, is_true);
2701 // Faster code path to avoid two compares: subtract lower bound from the
2702 // actual type and do a signed compare with the width of the type range.
2703 __ mov(temp, FieldOperand(input, HeapObject::kMapOffset));
2704 __ movzx_b(temp2, FieldOperand(temp, Map::kInstanceTypeOffset));
2705 __ sub(Operand(temp2), Immediate(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2706 __ cmp(Operand(temp2), Immediate(LAST_NONCALLABLE_SPEC_OBJECT_TYPE -
2707 FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2708 __ j(above, is_false);
2711 // Now we are in the FIRST-LAST_NONCALLABLE_SPEC_OBJECT_TYPE range.
2712 // Check if the constructor in the map is a function.
2713 __ mov(temp, FieldOperand(temp, Map::kConstructorOffset));
2714 // Objects with a non-function constructor have class 'Object'.
2715 __ CmpObjectType(temp, JS_FUNCTION_TYPE, temp2);
2716 if (String::Equals(class_name, isolate()->factory()->Object_string())) {
2717 __ j(not_equal, is_true);
2719 __ j(not_equal, is_false);
2722 // temp now contains the constructor function. Grab the
2723 // instance class name from there.
2724 __ mov(temp, FieldOperand(temp, JSFunction::kSharedFunctionInfoOffset));
2725 __ mov(temp, FieldOperand(temp,
2726 SharedFunctionInfo::kInstanceClassNameOffset));
2727 // The class name we are testing against is internalized since it's a literal.
2728 // The name in the constructor is internalized because of the way the context
2729 // is booted. This routine isn't expected to work for random API-created
2730 // classes and it doesn't have to because you can't access it with natives
2731 // syntax. Since both sides are internalized it is sufficient to use an
2732 // identity comparison.
2733 __ cmp(temp, class_name);
2734 // End with the answer in the z flag.
2738 void LCodeGen::DoClassOfTestAndBranch(LClassOfTestAndBranch* instr) {
2739 Register input = ToRegister(instr->value());
2740 Register temp = ToRegister(instr->temp());
2741 Register temp2 = ToRegister(instr->temp2());
2743 Handle<String> class_name = instr->hydrogen()->class_name();
2745 EmitClassOfTest(instr->TrueLabel(chunk_), instr->FalseLabel(chunk_),
2746 class_name, input, temp, temp2);
2748 EmitBranch(instr, equal);
2752 void LCodeGen::DoCmpMapAndBranch(LCmpMapAndBranch* instr) {
2753 Register reg = ToRegister(instr->value());
2754 __ cmp(FieldOperand(reg, HeapObject::kMapOffset), instr->map());
2755 EmitBranch(instr, equal);
2759 void LCodeGen::DoInstanceOf(LInstanceOf* instr) {
2760 // Object and function are in fixed registers defined by the stub.
2761 DCHECK(ToRegister(instr->context()).is(esi));
2762 InstanceofStub stub(isolate(), InstanceofStub::kArgsInRegisters);
2763 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
2765 Label true_value, done;
2766 __ test(eax, Operand(eax));
2767 __ j(zero, &true_value, Label::kNear);
2768 __ mov(ToRegister(instr->result()), factory()->false_value());
2769 __ jmp(&done, Label::kNear);
2770 __ bind(&true_value);
2771 __ mov(ToRegister(instr->result()), factory()->true_value());
2776 void LCodeGen::DoInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr) {
2777 class DeferredInstanceOfKnownGlobal FINAL : public LDeferredCode {
2779 DeferredInstanceOfKnownGlobal(LCodeGen* codegen,
2780 LInstanceOfKnownGlobal* instr)
2781 : LDeferredCode(codegen), instr_(instr) { }
2782 virtual void Generate() OVERRIDE {
2783 codegen()->DoDeferredInstanceOfKnownGlobal(instr_, &map_check_);
2785 virtual LInstruction* instr() OVERRIDE { return instr_; }
2786 Label* map_check() { return &map_check_; }
2788 LInstanceOfKnownGlobal* instr_;
2792 DeferredInstanceOfKnownGlobal* deferred;
2793 deferred = new(zone()) DeferredInstanceOfKnownGlobal(this, instr);
2795 Label done, false_result;
2796 Register object = ToRegister(instr->value());
2797 Register temp = ToRegister(instr->temp());
2799 // A Smi is not an instance of anything.
2800 __ JumpIfSmi(object, &false_result, Label::kNear);
2802 // This is the inlined call site instanceof cache. The two occurences of the
2803 // hole value will be patched to the last map/result pair generated by the
2806 Register map = ToRegister(instr->temp());
2807 __ mov(map, FieldOperand(object, HeapObject::kMapOffset));
2808 __ bind(deferred->map_check()); // Label for calculating code patching.
2809 Handle<Cell> cache_cell = factory()->NewCell(factory()->the_hole_value());
2810 __ cmp(map, Operand::ForCell(cache_cell)); // Patched to cached map.
2811 __ j(not_equal, &cache_miss, Label::kNear);
2812 __ mov(eax, factory()->the_hole_value()); // Patched to either true or false.
2813 __ jmp(&done, Label::kNear);
2815 // The inlined call site cache did not match. Check for null and string
2816 // before calling the deferred code.
2817 __ bind(&cache_miss);
2818 // Null is not an instance of anything.
2819 __ cmp(object, factory()->null_value());
2820 __ j(equal, &false_result, Label::kNear);
2822 // String values are not instances of anything.
2823 Condition is_string = masm_->IsObjectStringType(object, temp, temp);
2824 __ j(is_string, &false_result, Label::kNear);
2826 // Go to the deferred code.
2827 __ jmp(deferred->entry());
2829 __ bind(&false_result);
2830 __ mov(ToRegister(instr->result()), factory()->false_value());
2832 // Here result has either true or false. Deferred code also produces true or
2834 __ bind(deferred->exit());
2839 void LCodeGen::DoDeferredInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr,
2841 PushSafepointRegistersScope scope(this);
2843 InstanceofStub::Flags flags = InstanceofStub::kNoFlags;
2844 flags = static_cast<InstanceofStub::Flags>(
2845 flags | InstanceofStub::kArgsInRegisters);
2846 flags = static_cast<InstanceofStub::Flags>(
2847 flags | InstanceofStub::kCallSiteInlineCheck);
2848 flags = static_cast<InstanceofStub::Flags>(
2849 flags | InstanceofStub::kReturnTrueFalseObject);
2850 InstanceofStub stub(isolate(), flags);
2852 // Get the temp register reserved by the instruction. This needs to be a
2853 // register which is pushed last by PushSafepointRegisters as top of the
2854 // stack is used to pass the offset to the location of the map check to
2856 Register temp = ToRegister(instr->temp());
2857 DCHECK(MacroAssembler::SafepointRegisterStackIndex(temp) == 0);
2858 __ LoadHeapObject(InstanceofStub::right(), instr->function());
2859 static const int kAdditionalDelta = 13;
2860 int delta = masm_->SizeOfCodeGeneratedSince(map_check) + kAdditionalDelta;
2861 __ mov(temp, Immediate(delta));
2862 __ StoreToSafepointRegisterSlot(temp, temp);
2863 CallCodeGeneric(stub.GetCode(),
2864 RelocInfo::CODE_TARGET,
2866 RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
2867 // Get the deoptimization index of the LLazyBailout-environment that
2868 // corresponds to this instruction.
2869 LEnvironment* env = instr->GetDeferredLazyDeoptimizationEnvironment();
2870 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
2872 // Put the result value into the eax slot and restore all registers.
2873 __ StoreToSafepointRegisterSlot(eax, eax);
2877 void LCodeGen::DoCmpT(LCmpT* instr) {
2878 Token::Value op = instr->op();
2880 Handle<Code> ic = CodeFactory::CompareIC(isolate(), op).code();
2881 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2883 Condition condition = ComputeCompareCondition(op);
2884 Label true_value, done;
2885 __ test(eax, Operand(eax));
2886 __ j(condition, &true_value, Label::kNear);
2887 __ mov(ToRegister(instr->result()), factory()->false_value());
2888 __ jmp(&done, Label::kNear);
2889 __ bind(&true_value);
2890 __ mov(ToRegister(instr->result()), factory()->true_value());
2895 void LCodeGen::EmitReturn(LReturn* instr, bool dynamic_frame_alignment) {
2896 int extra_value_count = dynamic_frame_alignment ? 2 : 1;
2898 if (instr->has_constant_parameter_count()) {
2899 int parameter_count = ToInteger32(instr->constant_parameter_count());
2900 if (dynamic_frame_alignment && FLAG_debug_code) {
2902 (parameter_count + extra_value_count) * kPointerSize),
2903 Immediate(kAlignmentZapValue));
2904 __ Assert(equal, kExpectedAlignmentMarker);
2906 __ Ret((parameter_count + extra_value_count) * kPointerSize, ecx);
2908 Register reg = ToRegister(instr->parameter_count());
2909 // The argument count parameter is a smi
2911 Register return_addr_reg = reg.is(ecx) ? ebx : ecx;
2912 if (dynamic_frame_alignment && FLAG_debug_code) {
2913 DCHECK(extra_value_count == 2);
2914 __ cmp(Operand(esp, reg, times_pointer_size,
2915 extra_value_count * kPointerSize),
2916 Immediate(kAlignmentZapValue));
2917 __ Assert(equal, kExpectedAlignmentMarker);
2920 // emit code to restore stack based on instr->parameter_count()
2921 __ pop(return_addr_reg); // save return address
2922 if (dynamic_frame_alignment) {
2923 __ inc(reg); // 1 more for alignment
2925 __ shl(reg, kPointerSizeLog2);
2927 __ jmp(return_addr_reg);
2932 void LCodeGen::DoReturn(LReturn* instr) {
2933 if (FLAG_trace && info()->IsOptimizing()) {
2934 // Preserve the return value on the stack and rely on the runtime call
2935 // to return the value in the same register. We're leaving the code
2936 // managed by the register allocator and tearing down the frame, it's
2937 // safe to write to the context register.
2939 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
2940 __ CallRuntime(Runtime::kTraceExit, 1);
2942 if (info()->saves_caller_doubles()) RestoreCallerDoubles();
2943 if (dynamic_frame_alignment_) {
2944 // Fetch the state of the dynamic frame alignment.
2945 __ mov(edx, Operand(ebp,
2946 JavaScriptFrameConstants::kDynamicAlignmentStateOffset));
2948 int no_frame_start = -1;
2949 if (NeedsEagerFrame()) {
2952 no_frame_start = masm_->pc_offset();
2954 if (dynamic_frame_alignment_) {
2956 __ cmp(edx, Immediate(kNoAlignmentPadding));
2957 __ j(equal, &no_padding, Label::kNear);
2959 EmitReturn(instr, true);
2960 __ bind(&no_padding);
2963 EmitReturn(instr, false);
2964 if (no_frame_start != -1) {
2965 info()->AddNoFrameRange(no_frame_start, masm_->pc_offset());
2970 void LCodeGen::DoLoadGlobalCell(LLoadGlobalCell* instr) {
2971 Register result = ToRegister(instr->result());
2972 __ mov(result, Operand::ForCell(instr->hydrogen()->cell().handle()));
2973 if (instr->hydrogen()->RequiresHoleCheck()) {
2974 __ cmp(result, factory()->the_hole_value());
2975 DeoptimizeIf(equal, instr, "hole");
2981 void LCodeGen::EmitVectorLoadICRegisters(T* instr) {
2982 DCHECK(FLAG_vector_ics);
2983 Register vector_register = ToRegister(instr->temp_vector());
2984 DCHECK(vector_register.is(VectorLoadICDescriptor::VectorRegister()));
2985 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
2986 __ mov(vector_register, vector);
2987 // No need to allocate this register.
2988 DCHECK(VectorLoadICDescriptor::SlotRegister().is(eax));
2989 int index = vector->GetIndex(instr->hydrogen()->slot());
2990 __ mov(VectorLoadICDescriptor::SlotRegister(),
2991 Immediate(Smi::FromInt(index)));
2995 void LCodeGen::DoLoadGlobalGeneric(LLoadGlobalGeneric* instr) {
2996 DCHECK(ToRegister(instr->context()).is(esi));
2997 DCHECK(ToRegister(instr->global_object())
2998 .is(LoadDescriptor::ReceiverRegister()));
2999 DCHECK(ToRegister(instr->result()).is(eax));
3001 __ mov(LoadDescriptor::NameRegister(), instr->name());
3002 if (FLAG_vector_ics) {
3003 EmitVectorLoadICRegisters<LLoadGlobalGeneric>(instr);
3005 ContextualMode mode = instr->for_typeof() ? NOT_CONTEXTUAL : CONTEXTUAL;
3006 Handle<Code> ic = CodeFactory::LoadICInOptimizedCode(isolate(), mode).code();
3007 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3011 void LCodeGen::DoStoreGlobalCell(LStoreGlobalCell* instr) {
3012 Register value = ToRegister(instr->value());
3013 Handle<PropertyCell> cell_handle = instr->hydrogen()->cell().handle();
3015 // If the cell we are storing to contains the hole it could have
3016 // been deleted from the property dictionary. In that case, we need
3017 // to update the property details in the property dictionary to mark
3018 // it as no longer deleted. We deoptimize in that case.
3019 if (instr->hydrogen()->RequiresHoleCheck()) {
3020 __ cmp(Operand::ForCell(cell_handle), factory()->the_hole_value());
3021 DeoptimizeIf(equal, instr, "hole");
3025 __ mov(Operand::ForCell(cell_handle), value);
3026 // Cells are always rescanned, so no write barrier here.
3030 void LCodeGen::DoLoadContextSlot(LLoadContextSlot* instr) {
3031 Register context = ToRegister(instr->context());
3032 Register result = ToRegister(instr->result());
3033 __ mov(result, ContextOperand(context, instr->slot_index()));
3035 if (instr->hydrogen()->RequiresHoleCheck()) {
3036 __ cmp(result, factory()->the_hole_value());
3037 if (instr->hydrogen()->DeoptimizesOnHole()) {
3038 DeoptimizeIf(equal, instr, "hole");
3041 __ j(not_equal, &is_not_hole, Label::kNear);
3042 __ mov(result, factory()->undefined_value());
3043 __ bind(&is_not_hole);
3049 void LCodeGen::DoStoreContextSlot(LStoreContextSlot* instr) {
3050 Register context = ToRegister(instr->context());
3051 Register value = ToRegister(instr->value());
3053 Label skip_assignment;
3055 Operand target = ContextOperand(context, instr->slot_index());
3056 if (instr->hydrogen()->RequiresHoleCheck()) {
3057 __ cmp(target, factory()->the_hole_value());
3058 if (instr->hydrogen()->DeoptimizesOnHole()) {
3059 DeoptimizeIf(equal, instr, "hole");
3061 __ j(not_equal, &skip_assignment, Label::kNear);
3065 __ mov(target, value);
3066 if (instr->hydrogen()->NeedsWriteBarrier()) {
3067 SmiCheck check_needed =
3068 instr->hydrogen()->value()->type().IsHeapObject()
3069 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
3070 Register temp = ToRegister(instr->temp());
3071 int offset = Context::SlotOffset(instr->slot_index());
3072 __ RecordWriteContextSlot(context,
3077 EMIT_REMEMBERED_SET,
3081 __ bind(&skip_assignment);
3085 void LCodeGen::DoLoadNamedField(LLoadNamedField* instr) {
3086 HObjectAccess access = instr->hydrogen()->access();
3087 int offset = access.offset();
3089 if (access.IsExternalMemory()) {
3090 Register result = ToRegister(instr->result());
3091 MemOperand operand = instr->object()->IsConstantOperand()
3092 ? MemOperand::StaticVariable(ToExternalReference(
3093 LConstantOperand::cast(instr->object())))
3094 : MemOperand(ToRegister(instr->object()), offset);
3095 __ Load(result, operand, access.representation());
3099 Register object = ToRegister(instr->object());
3100 if (instr->hydrogen()->representation().IsDouble()) {
3101 XMMRegister result = ToDoubleRegister(instr->result());
3102 __ movsd(result, FieldOperand(object, offset));
3106 Register result = ToRegister(instr->result());
3107 if (!access.IsInobject()) {
3108 __ mov(result, FieldOperand(object, JSObject::kPropertiesOffset));
3111 __ Load(result, FieldOperand(object, offset), access.representation());
3115 void LCodeGen::EmitPushTaggedOperand(LOperand* operand) {
3116 DCHECK(!operand->IsDoubleRegister());
3117 if (operand->IsConstantOperand()) {
3118 Handle<Object> object = ToHandle(LConstantOperand::cast(operand));
3119 AllowDeferredHandleDereference smi_check;
3120 if (object->IsSmi()) {
3121 __ Push(Handle<Smi>::cast(object));
3123 __ PushHeapObject(Handle<HeapObject>::cast(object));
3125 } else if (operand->IsRegister()) {
3126 __ push(ToRegister(operand));
3128 __ push(ToOperand(operand));
3133 void LCodeGen::DoLoadNamedGeneric(LLoadNamedGeneric* instr) {
3134 DCHECK(ToRegister(instr->context()).is(esi));
3135 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
3136 DCHECK(ToRegister(instr->result()).is(eax));
3138 __ mov(LoadDescriptor::NameRegister(), instr->name());
3139 if (FLAG_vector_ics) {
3140 EmitVectorLoadICRegisters<LLoadNamedGeneric>(instr);
3143 CodeFactory::LoadICInOptimizedCode(isolate(), NOT_CONTEXTUAL).code();
3144 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3148 void LCodeGen::DoLoadFunctionPrototype(LLoadFunctionPrototype* instr) {
3149 Register function = ToRegister(instr->function());
3150 Register temp = ToRegister(instr->temp());
3151 Register result = ToRegister(instr->result());
3153 // Get the prototype or initial map from the function.
3155 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
3157 // Check that the function has a prototype or an initial map.
3158 __ cmp(Operand(result), Immediate(factory()->the_hole_value()));
3159 DeoptimizeIf(equal, instr, "hole");
3161 // If the function does not have an initial map, we're done.
3163 __ CmpObjectType(result, MAP_TYPE, temp);
3164 __ j(not_equal, &done, Label::kNear);
3166 // Get the prototype from the initial map.
3167 __ mov(result, FieldOperand(result, Map::kPrototypeOffset));
3174 void LCodeGen::DoLoadRoot(LLoadRoot* instr) {
3175 Register result = ToRegister(instr->result());
3176 __ LoadRoot(result, instr->index());
3180 void LCodeGen::DoAccessArgumentsAt(LAccessArgumentsAt* instr) {
3181 Register arguments = ToRegister(instr->arguments());
3182 Register result = ToRegister(instr->result());
3183 if (instr->length()->IsConstantOperand() &&
3184 instr->index()->IsConstantOperand()) {
3185 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
3186 int const_length = ToInteger32(LConstantOperand::cast(instr->length()));
3187 int index = (const_length - const_index) + 1;
3188 __ mov(result, Operand(arguments, index * kPointerSize));
3190 Register length = ToRegister(instr->length());
3191 Operand index = ToOperand(instr->index());
3192 // There are two words between the frame pointer and the last argument.
3193 // Subtracting from length accounts for one of them add one more.
3194 __ sub(length, index);
3195 __ mov(result, Operand(arguments, length, times_4, kPointerSize));
3200 void LCodeGen::DoDeferredSIMD128ToTagged(LInstruction* instr,
3201 Runtime::FunctionId id) {
3202 // TODO(3095996): Get rid of this. For now, we need to make the
3203 // result register contain a valid pointer because it is already
3204 // contained in the register pointer map.
3205 Register reg = ToRegister(instr->result());
3206 __ Move(reg, Immediate(0));
3208 PushSafepointRegistersScope scope(this);
3209 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
3210 __ CallRuntimeSaveDoubles(id);
3211 RecordSafepointWithRegisters(
3212 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
3213 __ StoreToSafepointRegisterSlot(reg, eax);
3217 void LCodeGen::HandleExternalArrayOpRequiresTemp(
3219 Representation key_representation,
3220 ElementsKind elements_kind) {
3221 if (ExternalArrayOpRequiresPreScale(key_representation, elements_kind)) {
3222 int pre_shift_size = ElementsKindToShiftSize(elements_kind) -
3223 static_cast<int>(maximal_scale_factor);
3224 if (key_representation.IsSmi()) {
3225 pre_shift_size -= kSmiTagSize;
3227 DCHECK(pre_shift_size > 0);
3228 __ shl(ToRegister(key), pre_shift_size);
3230 __ SmiUntag(ToRegister(key));
3235 void LCodeGen::DoLoadKeyedExternalArray(LLoadKeyed* instr) {
3236 ElementsKind elements_kind = instr->elements_kind();
3237 LOperand* key = instr->key();
3238 if (!key->IsConstantOperand() &&
3239 ExternalArrayOpRequiresTemp(
3240 instr->hydrogen()->key()->representation(), elements_kind)) {
3241 HandleExternalArrayOpRequiresTemp(
3242 key, instr->hydrogen()->key()->representation(), elements_kind);
3245 Operand operand(BuildFastArrayOperand(
3248 instr->hydrogen()->key()->representation(),
3250 instr->base_offset()));
3251 BuiltinFunctionId op = instr->hydrogen()->op();
3252 if (IsSIMD128LoadStoreOp(op)) {
3253 if (GetSIMD128LoadStoreBytes(op) == 16) {
3254 __ movups(ToSIMD128Register(instr->result()), operand);
3255 } else if (GetSIMD128LoadStoreBytes(op) == 4) {
3256 __ movss(ToSIMD128Register(instr->result()), operand);
3257 } else if (GetSIMD128LoadStoreBytes(op) == 8) {
3258 __ movq(ToSIMD128Register(instr->result()), operand);
3259 } else if (GetSIMD128LoadStoreBytes(op) == 12) {
3260 XMMRegister result(ToSIMD128Register(instr->result()));
3261 XMMRegister xmm_scratch = double_scratch0();
3262 __ movq(result, operand);
3263 Operand operand2(BuildFastArrayOperand(
3266 instr->hydrogen()->key()->representation(),
3268 instr->base_offset() + 8));
3269 __ movss(xmm_scratch, operand2);
3270 __ movlhps(result, xmm_scratch);
3272 } else if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
3273 elements_kind == FLOAT32_ELEMENTS) {
3274 XMMRegister result(ToDoubleRegister(instr->result()));
3275 __ movss(result, operand);
3276 __ cvtss2sd(result, result);
3277 } else if (elements_kind == EXTERNAL_FLOAT64_ELEMENTS ||
3278 elements_kind == FLOAT64_ELEMENTS) {
3279 __ movsd(ToDoubleRegister(instr->result()), operand);
3280 } else if (IsSIMD128ElementsKind(elements_kind)) {
3281 __ movups(ToSIMD128Register(instr->result()), operand);
3283 Register result(ToRegister(instr->result()));
3284 switch (elements_kind) {
3285 case EXTERNAL_INT8_ELEMENTS:
3287 __ movsx_b(result, operand);
3289 case EXTERNAL_UINT8_CLAMPED_ELEMENTS:
3290 case EXTERNAL_UINT8_ELEMENTS:
3291 case UINT8_ELEMENTS:
3292 case UINT8_CLAMPED_ELEMENTS:
3293 __ movzx_b(result, operand);
3295 case EXTERNAL_INT16_ELEMENTS:
3296 case INT16_ELEMENTS:
3297 __ movsx_w(result, operand);
3299 case EXTERNAL_UINT16_ELEMENTS:
3300 case UINT16_ELEMENTS:
3301 __ movzx_w(result, operand);
3303 case EXTERNAL_INT32_ELEMENTS:
3304 case INT32_ELEMENTS:
3305 __ mov(result, operand);
3307 case EXTERNAL_UINT32_ELEMENTS:
3308 case UINT32_ELEMENTS:
3309 __ mov(result, operand);
3310 if (!instr->hydrogen()->CheckFlag(HInstruction::kUint32)) {
3311 __ test(result, Operand(result));
3312 DeoptimizeIf(negative, instr, "negative value");
3315 case EXTERNAL_FLOAT32_ELEMENTS:
3316 case EXTERNAL_FLOAT64_ELEMENTS:
3317 case EXTERNAL_FLOAT32x4_ELEMENTS:
3318 case EXTERNAL_FLOAT64x2_ELEMENTS:
3319 case EXTERNAL_INT32x4_ELEMENTS:
3320 case FLOAT32_ELEMENTS:
3321 case FLOAT64_ELEMENTS:
3322 case FLOAT32x4_ELEMENTS:
3323 case FLOAT64x2_ELEMENTS:
3324 case INT32x4_ELEMENTS:
3325 case FAST_SMI_ELEMENTS:
3327 case FAST_DOUBLE_ELEMENTS:
3328 case FAST_HOLEY_SMI_ELEMENTS:
3329 case FAST_HOLEY_ELEMENTS:
3330 case FAST_HOLEY_DOUBLE_ELEMENTS:
3331 case DICTIONARY_ELEMENTS:
3332 case SLOPPY_ARGUMENTS_ELEMENTS:
3340 void LCodeGen::DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr) {
3341 if (instr->hydrogen()->RequiresHoleCheck()) {
3342 Operand hole_check_operand = BuildFastArrayOperand(
3343 instr->elements(), instr->key(),
3344 instr->hydrogen()->key()->representation(),
3345 FAST_DOUBLE_ELEMENTS,
3346 instr->base_offset() + sizeof(kHoleNanLower32));
3347 __ cmp(hole_check_operand, Immediate(kHoleNanUpper32));
3348 DeoptimizeIf(equal, instr, "hole");
3351 Operand double_load_operand = BuildFastArrayOperand(
3354 instr->hydrogen()->key()->representation(),
3355 FAST_DOUBLE_ELEMENTS,
3356 instr->base_offset());
3357 XMMRegister result = ToDoubleRegister(instr->result());
3358 __ movsd(result, double_load_operand);
3362 void LCodeGen::DoLoadKeyedFixedArray(LLoadKeyed* instr) {
3363 Register result = ToRegister(instr->result());
3367 BuildFastArrayOperand(instr->elements(), instr->key(),
3368 instr->hydrogen()->key()->representation(),
3369 FAST_ELEMENTS, instr->base_offset()));
3371 // Check for the hole value.
3372 if (instr->hydrogen()->RequiresHoleCheck()) {
3373 if (IsFastSmiElementsKind(instr->hydrogen()->elements_kind())) {
3374 __ test(result, Immediate(kSmiTagMask));
3375 DeoptimizeIf(not_equal, instr, "not a Smi");
3377 __ cmp(result, factory()->the_hole_value());
3378 DeoptimizeIf(equal, instr, "hole");
3384 void LCodeGen::DoLoadKeyed(LLoadKeyed* instr) {
3385 if (instr->is_typed_elements()) {
3386 DoLoadKeyedExternalArray(instr);
3387 } else if (instr->hydrogen()->representation().IsDouble()) {
3388 DoLoadKeyedFixedDoubleArray(instr);
3390 DoLoadKeyedFixedArray(instr);
3395 Operand LCodeGen::BuildFastArrayOperand(
3396 LOperand* elements_pointer,
3398 Representation key_representation,
3399 ElementsKind elements_kind,
3400 uint32_t base_offset) {
3401 Register elements_pointer_reg = ToRegister(elements_pointer);
3402 int element_shift_size = ElementsKindToShiftSize(elements_kind);
3403 int shift_size = element_shift_size;
3404 if (key->IsConstantOperand()) {
3405 int constant_value = ToInteger32(LConstantOperand::cast(key));
3406 if (constant_value & 0xF0000000) {
3407 Abort(kArrayIndexConstantValueTooBig);
3409 return Operand(elements_pointer_reg,
3410 ((constant_value) << shift_size)
3413 if (ExternalArrayOpRequiresPreScale(key_representation, elements_kind)) {
3414 // Make sure the key is pre-scaled against maximal_scale_factor.
3415 shift_size = static_cast<int>(maximal_scale_factor);
3416 } else if (key_representation.IsSmi() && (shift_size >= 1)) {
3417 // Take the tag bit into account while computing the shift size.
3418 shift_size -= kSmiTagSize;
3420 ScaleFactor scale_factor = static_cast<ScaleFactor>(shift_size);
3421 return Operand(elements_pointer_reg,
3429 void LCodeGen::DoLoadKeyedGeneric(LLoadKeyedGeneric* instr) {
3430 DCHECK(ToRegister(instr->context()).is(esi));
3431 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
3432 DCHECK(ToRegister(instr->key()).is(LoadDescriptor::NameRegister()));
3434 if (FLAG_vector_ics) {
3435 EmitVectorLoadICRegisters<LLoadKeyedGeneric>(instr);
3438 Handle<Code> ic = CodeFactory::KeyedLoadICInOptimizedCode(isolate()).code();
3439 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3443 void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) {
3444 Register result = ToRegister(instr->result());
3446 if (instr->hydrogen()->from_inlined()) {
3447 __ lea(result, Operand(esp, -2 * kPointerSize));
3449 // Check for arguments adapter frame.
3450 Label done, adapted;
3451 __ mov(result, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
3452 __ mov(result, Operand(result, StandardFrameConstants::kContextOffset));
3453 __ cmp(Operand(result),
3454 Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3455 __ j(equal, &adapted, Label::kNear);
3457 // No arguments adaptor frame.
3458 __ mov(result, Operand(ebp));
3459 __ jmp(&done, Label::kNear);
3461 // Arguments adaptor frame present.
3463 __ mov(result, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
3465 // Result is the frame pointer for the frame if not adapted and for the real
3466 // frame below the adaptor frame if adapted.
3472 void LCodeGen::DoArgumentsLength(LArgumentsLength* instr) {
3473 Operand elem = ToOperand(instr->elements());
3474 Register result = ToRegister(instr->result());
3478 // If no arguments adaptor frame the number of arguments is fixed.
3480 __ mov(result, Immediate(scope()->num_parameters()));
3481 __ j(equal, &done, Label::kNear);
3483 // Arguments adaptor frame present. Get argument length from there.
3484 __ mov(result, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
3485 __ mov(result, Operand(result,
3486 ArgumentsAdaptorFrameConstants::kLengthOffset));
3487 __ SmiUntag(result);
3489 // Argument length is in result register.
3494 void LCodeGen::DoWrapReceiver(LWrapReceiver* instr) {
3495 Register receiver = ToRegister(instr->receiver());
3496 Register function = ToRegister(instr->function());
3498 // If the receiver is null or undefined, we have to pass the global
3499 // object as a receiver to normal functions. Values have to be
3500 // passed unchanged to builtins and strict-mode functions.
3501 Label receiver_ok, global_object;
3502 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
3503 Register scratch = ToRegister(instr->temp());
3505 if (!instr->hydrogen()->known_function()) {
3506 // Do not transform the receiver to object for strict mode
3509 FieldOperand(function, JSFunction::kSharedFunctionInfoOffset));
3510 __ test_b(FieldOperand(scratch, SharedFunctionInfo::kStrictModeByteOffset),
3511 1 << SharedFunctionInfo::kStrictModeBitWithinByte);
3512 __ j(not_equal, &receiver_ok, dist);
3514 // Do not transform the receiver to object for builtins.
3515 __ test_b(FieldOperand(scratch, SharedFunctionInfo::kNativeByteOffset),
3516 1 << SharedFunctionInfo::kNativeBitWithinByte);
3517 __ j(not_equal, &receiver_ok, dist);
3520 // Normal function. Replace undefined or null with global receiver.
3521 __ cmp(receiver, factory()->null_value());
3522 __ j(equal, &global_object, Label::kNear);
3523 __ cmp(receiver, factory()->undefined_value());
3524 __ j(equal, &global_object, Label::kNear);
3526 // The receiver should be a JS object.
3527 __ test(receiver, Immediate(kSmiTagMask));
3528 DeoptimizeIf(equal, instr, "Smi");
3529 __ CmpObjectType(receiver, FIRST_SPEC_OBJECT_TYPE, scratch);
3530 DeoptimizeIf(below, instr, "not a JavaScript object");
3532 __ jmp(&receiver_ok, Label::kNear);
3533 __ bind(&global_object);
3534 __ mov(receiver, FieldOperand(function, JSFunction::kContextOffset));
3535 const int global_offset = Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX);
3536 __ mov(receiver, Operand(receiver, global_offset));
3537 const int proxy_offset = GlobalObject::kGlobalProxyOffset;
3538 __ mov(receiver, FieldOperand(receiver, proxy_offset));
3539 __ bind(&receiver_ok);
3543 void LCodeGen::DoApplyArguments(LApplyArguments* instr) {
3544 Register receiver = ToRegister(instr->receiver());
3545 Register function = ToRegister(instr->function());
3546 Register length = ToRegister(instr->length());
3547 Register elements = ToRegister(instr->elements());
3548 DCHECK(receiver.is(eax)); // Used for parameter count.
3549 DCHECK(function.is(edi)); // Required by InvokeFunction.
3550 DCHECK(ToRegister(instr->result()).is(eax));
3552 // Copy the arguments to this function possibly from the
3553 // adaptor frame below it.
3554 const uint32_t kArgumentsLimit = 1 * KB;
3555 __ cmp(length, kArgumentsLimit);
3556 DeoptimizeIf(above, instr, "too many arguments");
3559 __ mov(receiver, length);
3561 // Loop through the arguments pushing them onto the execution
3564 // length is a small non-negative integer, due to the test above.
3565 __ test(length, Operand(length));
3566 __ j(zero, &invoke, Label::kNear);
3568 __ push(Operand(elements, length, times_pointer_size, 1 * kPointerSize));
3570 __ j(not_zero, &loop);
3572 // Invoke the function.
3574 DCHECK(instr->HasPointerMap());
3575 LPointerMap* pointers = instr->pointer_map();
3576 SafepointGenerator safepoint_generator(
3577 this, pointers, Safepoint::kLazyDeopt);
3578 ParameterCount actual(eax);
3579 __ InvokeFunction(function, actual, CALL_FUNCTION, safepoint_generator);
3583 void LCodeGen::DoDebugBreak(LDebugBreak* instr) {
3588 void LCodeGen::DoPushArgument(LPushArgument* instr) {
3589 LOperand* argument = instr->value();
3590 EmitPushTaggedOperand(argument);
3594 void LCodeGen::DoDrop(LDrop* instr) {
3595 __ Drop(instr->count());
3599 void LCodeGen::DoThisFunction(LThisFunction* instr) {
3600 Register result = ToRegister(instr->result());
3601 __ mov(result, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
3605 void LCodeGen::DoContext(LContext* instr) {
3606 Register result = ToRegister(instr->result());
3607 if (info()->IsOptimizing()) {
3608 __ mov(result, Operand(ebp, StandardFrameConstants::kContextOffset));
3610 // If there is no frame, the context must be in esi.
3611 DCHECK(result.is(esi));
3616 void LCodeGen::DoDeclareGlobals(LDeclareGlobals* instr) {
3617 DCHECK(ToRegister(instr->context()).is(esi));
3618 __ push(esi); // The context is the first argument.
3619 __ push(Immediate(instr->hydrogen()->pairs()));
3620 __ push(Immediate(Smi::FromInt(instr->hydrogen()->flags())));
3621 CallRuntime(Runtime::kDeclareGlobals, 3, instr);
3625 void LCodeGen::CallKnownFunction(Handle<JSFunction> function,
3626 int formal_parameter_count,
3628 LInstruction* instr,
3629 EDIState edi_state) {
3630 bool dont_adapt_arguments =
3631 formal_parameter_count == SharedFunctionInfo::kDontAdaptArgumentsSentinel;
3632 bool can_invoke_directly =
3633 dont_adapt_arguments || formal_parameter_count == arity;
3635 if (can_invoke_directly) {
3636 if (edi_state == EDI_UNINITIALIZED) {
3637 __ LoadHeapObject(edi, function);
3641 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
3643 // Set eax to arguments count if adaption is not needed. Assumes that eax
3644 // is available to write to at this point.
3645 if (dont_adapt_arguments) {
3649 // Invoke function directly.
3650 if (function.is_identical_to(info()->closure())) {
3653 __ call(FieldOperand(edi, JSFunction::kCodeEntryOffset));
3655 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
3657 // We need to adapt arguments.
3658 LPointerMap* pointers = instr->pointer_map();
3659 SafepointGenerator generator(
3660 this, pointers, Safepoint::kLazyDeopt);
3661 ParameterCount count(arity);
3662 ParameterCount expected(formal_parameter_count);
3663 __ InvokeFunction(function, expected, count, CALL_FUNCTION, generator);
3668 void LCodeGen::DoTailCallThroughMegamorphicCache(
3669 LTailCallThroughMegamorphicCache* instr) {
3670 Register receiver = ToRegister(instr->receiver());
3671 Register name = ToRegister(instr->name());
3672 DCHECK(receiver.is(LoadDescriptor::ReceiverRegister()));
3673 DCHECK(name.is(LoadDescriptor::NameRegister()));
3675 Register scratch = ebx;
3676 Register extra = eax;
3677 DCHECK(!scratch.is(receiver) && !scratch.is(name));
3678 DCHECK(!extra.is(receiver) && !extra.is(name));
3680 // Important for the tail-call.
3681 bool must_teardown_frame = NeedsEagerFrame();
3683 // The probe will tail call to a handler if found.
3684 isolate()->stub_cache()->GenerateProbe(masm(), instr->hydrogen()->flags(),
3685 must_teardown_frame, receiver, name,
3688 // Tail call to miss if we ended up here.
3689 if (must_teardown_frame) __ leave();
3690 LoadIC::GenerateMiss(masm());
3694 void LCodeGen::DoCallWithDescriptor(LCallWithDescriptor* instr) {
3695 DCHECK(ToRegister(instr->result()).is(eax));
3697 LPointerMap* pointers = instr->pointer_map();
3698 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3700 if (instr->target()->IsConstantOperand()) {
3701 LConstantOperand* target = LConstantOperand::cast(instr->target());
3702 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
3703 generator.BeforeCall(__ CallSize(code, RelocInfo::CODE_TARGET));
3704 __ call(code, RelocInfo::CODE_TARGET);
3706 DCHECK(instr->target()->IsRegister());
3707 Register target = ToRegister(instr->target());
3708 generator.BeforeCall(__ CallSize(Operand(target)));
3709 __ add(target, Immediate(Code::kHeaderSize - kHeapObjectTag));
3712 generator.AfterCall();
3716 void LCodeGen::DoCallJSFunction(LCallJSFunction* instr) {
3717 DCHECK(ToRegister(instr->function()).is(edi));
3718 DCHECK(ToRegister(instr->result()).is(eax));
3720 if (instr->hydrogen()->pass_argument_count()) {
3721 __ mov(eax, instr->arity());
3725 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
3727 bool is_self_call = false;
3728 if (instr->hydrogen()->function()->IsConstant()) {
3729 HConstant* fun_const = HConstant::cast(instr->hydrogen()->function());
3730 Handle<JSFunction> jsfun =
3731 Handle<JSFunction>::cast(fun_const->handle(isolate()));
3732 is_self_call = jsfun.is_identical_to(info()->closure());
3738 __ call(FieldOperand(edi, JSFunction::kCodeEntryOffset));
3741 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
3745 void LCodeGen::DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr) {
3746 Register input_reg = ToRegister(instr->value());
3747 __ cmp(FieldOperand(input_reg, HeapObject::kMapOffset),
3748 factory()->heap_number_map());
3749 DeoptimizeIf(not_equal, instr, "not a heap number");
3751 Label slow, allocated, done;
3752 Register tmp = input_reg.is(eax) ? ecx : eax;
3753 Register tmp2 = tmp.is(ecx) ? edx : input_reg.is(ecx) ? edx : ecx;
3755 // Preserve the value of all registers.
3756 PushSafepointRegistersScope scope(this);
3758 __ mov(tmp, FieldOperand(input_reg, HeapNumber::kExponentOffset));
3759 // Check the sign of the argument. If the argument is positive, just
3760 // return it. We do not need to patch the stack since |input| and
3761 // |result| are the same register and |input| will be restored
3762 // unchanged by popping safepoint registers.
3763 __ test(tmp, Immediate(HeapNumber::kSignMask));
3764 __ j(zero, &done, Label::kNear);
3766 __ AllocateHeapNumber(tmp, tmp2, no_reg, &slow);
3767 __ jmp(&allocated, Label::kNear);
3769 // Slow case: Call the runtime system to do the number allocation.
3771 CallRuntimeFromDeferred(Runtime::kAllocateHeapNumber, 0,
3772 instr, instr->context());
3773 // Set the pointer to the new heap number in tmp.
3774 if (!tmp.is(eax)) __ mov(tmp, eax);
3775 // Restore input_reg after call to runtime.
3776 __ LoadFromSafepointRegisterSlot(input_reg, input_reg);
3778 __ bind(&allocated);
3779 __ mov(tmp2, FieldOperand(input_reg, HeapNumber::kExponentOffset));
3780 __ and_(tmp2, ~HeapNumber::kSignMask);
3781 __ mov(FieldOperand(tmp, HeapNumber::kExponentOffset), tmp2);
3782 __ mov(tmp2, FieldOperand(input_reg, HeapNumber::kMantissaOffset));
3783 __ mov(FieldOperand(tmp, HeapNumber::kMantissaOffset), tmp2);
3784 __ StoreToSafepointRegisterSlot(input_reg, tmp);
3790 void LCodeGen::EmitIntegerMathAbs(LMathAbs* instr) {
3791 Register input_reg = ToRegister(instr->value());
3792 __ test(input_reg, Operand(input_reg));
3794 __ j(not_sign, &is_positive, Label::kNear);
3795 __ neg(input_reg); // Sets flags.
3796 DeoptimizeIf(negative, instr, "overflow");
3797 __ bind(&is_positive);
3801 void LCodeGen::DoMathAbs(LMathAbs* instr) {
3802 // Class for deferred case.
3803 class DeferredMathAbsTaggedHeapNumber FINAL : public LDeferredCode {
3805 DeferredMathAbsTaggedHeapNumber(LCodeGen* codegen,
3807 : LDeferredCode(codegen), instr_(instr) { }
3808 virtual void Generate() OVERRIDE {
3809 codegen()->DoDeferredMathAbsTaggedHeapNumber(instr_);
3811 virtual LInstruction* instr() OVERRIDE { return instr_; }
3816 DCHECK(instr->value()->Equals(instr->result()));
3817 Representation r = instr->hydrogen()->value()->representation();
3820 XMMRegister scratch = double_scratch0();
3821 XMMRegister input_reg = ToDoubleRegister(instr->value());
3822 __ xorps(scratch, scratch);
3823 __ subsd(scratch, input_reg);
3824 __ andps(input_reg, scratch);
3825 } else if (r.IsSmiOrInteger32()) {
3826 EmitIntegerMathAbs(instr);
3827 } else { // Tagged case.
3828 DeferredMathAbsTaggedHeapNumber* deferred =
3829 new(zone()) DeferredMathAbsTaggedHeapNumber(this, instr);
3830 Register input_reg = ToRegister(instr->value());
3832 __ JumpIfNotSmi(input_reg, deferred->entry());
3833 EmitIntegerMathAbs(instr);
3834 __ bind(deferred->exit());
3839 void LCodeGen::DoMathFloor(LMathFloor* instr) {
3840 XMMRegister xmm_scratch = double_scratch0();
3841 Register output_reg = ToRegister(instr->result());
3842 XMMRegister input_reg = ToDoubleRegister(instr->value());
3844 if (CpuFeatures::IsSupported(SSE4_1)) {
3845 CpuFeatureScope scope(masm(), SSE4_1);
3846 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3847 // Deoptimize on negative zero.
3849 __ xorps(xmm_scratch, xmm_scratch); // Zero the register.
3850 __ ucomisd(input_reg, xmm_scratch);
3851 __ j(not_equal, &non_zero, Label::kNear);
3852 __ movmskpd(output_reg, input_reg);
3853 __ test(output_reg, Immediate(1));
3854 DeoptimizeIf(not_zero, instr, "minus zero");
3857 __ roundsd(xmm_scratch, input_reg, Assembler::kRoundDown);
3858 __ cvttsd2si(output_reg, Operand(xmm_scratch));
3859 // Overflow is signalled with minint.
3860 __ cmp(output_reg, 0x1);
3861 DeoptimizeIf(overflow, instr, "overflow");
3863 Label negative_sign, done;
3864 // Deoptimize on unordered.
3865 __ xorps(xmm_scratch, xmm_scratch); // Zero the register.
3866 __ ucomisd(input_reg, xmm_scratch);
3867 DeoptimizeIf(parity_even, instr, "NaN");
3868 __ j(below, &negative_sign, Label::kNear);
3870 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3871 // Check for negative zero.
3872 Label positive_sign;
3873 __ j(above, &positive_sign, Label::kNear);
3874 __ movmskpd(output_reg, input_reg);
3875 __ test(output_reg, Immediate(1));
3876 DeoptimizeIf(not_zero, instr, "minus zero");
3877 __ Move(output_reg, Immediate(0));
3878 __ jmp(&done, Label::kNear);
3879 __ bind(&positive_sign);
3882 // Use truncating instruction (OK because input is positive).
3883 __ cvttsd2si(output_reg, Operand(input_reg));
3884 // Overflow is signalled with minint.
3885 __ cmp(output_reg, 0x1);
3886 DeoptimizeIf(overflow, instr, "overflow");
3887 __ jmp(&done, Label::kNear);
3889 // Non-zero negative reaches here.
3890 __ bind(&negative_sign);
3891 // Truncate, then compare and compensate.
3892 __ cvttsd2si(output_reg, Operand(input_reg));
3893 __ Cvtsi2sd(xmm_scratch, output_reg);
3894 __ ucomisd(input_reg, xmm_scratch);
3895 __ j(equal, &done, Label::kNear);
3896 __ sub(output_reg, Immediate(1));
3897 DeoptimizeIf(overflow, instr, "overflow");
3904 void LCodeGen::DoMathRound(LMathRound* instr) {
3905 Register output_reg = ToRegister(instr->result());
3906 XMMRegister input_reg = ToDoubleRegister(instr->value());
3907 XMMRegister xmm_scratch = double_scratch0();
3908 XMMRegister input_temp = ToDoubleRegister(instr->temp());
3909 ExternalReference one_half = ExternalReference::address_of_one_half();
3910 ExternalReference minus_one_half =
3911 ExternalReference::address_of_minus_one_half();
3913 Label done, round_to_zero, below_one_half, do_not_compensate;
3914 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
3916 __ movsd(xmm_scratch, Operand::StaticVariable(one_half));
3917 __ ucomisd(xmm_scratch, input_reg);
3918 __ j(above, &below_one_half, Label::kNear);
3920 // CVTTSD2SI rounds towards zero, since 0.5 <= x, we use floor(0.5 + x).
3921 __ addsd(xmm_scratch, input_reg);
3922 __ cvttsd2si(output_reg, Operand(xmm_scratch));
3923 // Overflow is signalled with minint.
3924 __ cmp(output_reg, 0x1);
3925 DeoptimizeIf(overflow, instr, "overflow");
3926 __ jmp(&done, dist);
3928 __ bind(&below_one_half);
3929 __ movsd(xmm_scratch, Operand::StaticVariable(minus_one_half));
3930 __ ucomisd(xmm_scratch, input_reg);
3931 __ j(below_equal, &round_to_zero, Label::kNear);
3933 // CVTTSD2SI rounds towards zero, we use ceil(x - (-0.5)) and then
3934 // compare and compensate.
3935 __ movaps(input_temp, input_reg); // Do not alter input_reg.
3936 __ subsd(input_temp, xmm_scratch);
3937 __ cvttsd2si(output_reg, Operand(input_temp));
3938 // Catch minint due to overflow, and to prevent overflow when compensating.
3939 __ cmp(output_reg, 0x1);
3940 DeoptimizeIf(overflow, instr, "overflow");
3942 __ Cvtsi2sd(xmm_scratch, output_reg);
3943 __ ucomisd(xmm_scratch, input_temp);
3944 __ j(equal, &done, dist);
3945 __ sub(output_reg, Immediate(1));
3946 // No overflow because we already ruled out minint.
3947 __ jmp(&done, dist);
3949 __ bind(&round_to_zero);
3950 // We return 0 for the input range [+0, 0.5[, or [-0.5, 0.5[ if
3951 // we can ignore the difference between a result of -0 and +0.
3952 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3953 // If the sign is positive, we return +0.
3954 __ movmskpd(output_reg, input_reg);
3955 __ test(output_reg, Immediate(1));
3956 DeoptimizeIf(not_zero, instr, "minus zero");
3958 __ Move(output_reg, Immediate(0));
3963 void LCodeGen::DoMathFround(LMathFround* instr) {
3964 XMMRegister input_reg = ToDoubleRegister(instr->value());
3965 XMMRegister output_reg = ToDoubleRegister(instr->result());
3966 __ cvtsd2ss(output_reg, input_reg);
3967 __ cvtss2sd(output_reg, output_reg);
3971 void LCodeGen::DoMathSqrt(LMathSqrt* instr) {
3972 Operand input = ToOperand(instr->value());
3973 XMMRegister output = ToDoubleRegister(instr->result());
3974 __ sqrtsd(output, input);
3978 void LCodeGen::DoMathPowHalf(LMathPowHalf* instr) {
3979 XMMRegister xmm_scratch = double_scratch0();
3980 XMMRegister input_reg = ToDoubleRegister(instr->value());
3981 Register scratch = ToRegister(instr->temp());
3982 DCHECK(ToDoubleRegister(instr->result()).is(input_reg));
3984 // Note that according to ECMA-262 15.8.2.13:
3985 // Math.pow(-Infinity, 0.5) == Infinity
3986 // Math.sqrt(-Infinity) == NaN
3988 // Check base for -Infinity. According to IEEE-754, single-precision
3989 // -Infinity has the highest 9 bits set and the lowest 23 bits cleared.
3990 __ mov(scratch, 0xFF800000);
3991 __ movd(xmm_scratch, scratch);
3992 __ cvtss2sd(xmm_scratch, xmm_scratch);
3993 __ ucomisd(input_reg, xmm_scratch);
3994 // Comparing -Infinity with NaN results in "unordered", which sets the
3995 // zero flag as if both were equal. However, it also sets the carry flag.
3996 __ j(not_equal, &sqrt, Label::kNear);
3997 __ j(carry, &sqrt, Label::kNear);
3998 // If input is -Infinity, return Infinity.
3999 __ xorps(input_reg, input_reg);
4000 __ subsd(input_reg, xmm_scratch);
4001 __ jmp(&done, Label::kNear);
4005 __ xorps(xmm_scratch, xmm_scratch);
4006 __ addsd(input_reg, xmm_scratch); // Convert -0 to +0.
4007 __ sqrtsd(input_reg, input_reg);
4012 void LCodeGen::DoPower(LPower* instr) {
4013 Representation exponent_type = instr->hydrogen()->right()->representation();
4014 // Having marked this as a call, we can use any registers.
4015 // Just make sure that the input/output registers are the expected ones.
4016 Register tagged_exponent = MathPowTaggedDescriptor::exponent();
4017 DCHECK(!instr->right()->IsDoubleRegister() ||
4018 ToDoubleRegister(instr->right()).is(xmm1));
4019 DCHECK(!instr->right()->IsRegister() ||
4020 ToRegister(instr->right()).is(tagged_exponent));
4021 DCHECK(ToDoubleRegister(instr->left()).is(xmm2));
4022 DCHECK(ToDoubleRegister(instr->result()).is(xmm3));
4024 if (exponent_type.IsSmi()) {
4025 MathPowStub stub(isolate(), MathPowStub::TAGGED);
4027 } else if (exponent_type.IsTagged()) {
4029 __ JumpIfSmi(tagged_exponent, &no_deopt);
4030 DCHECK(!ecx.is(tagged_exponent));
4031 __ CmpObjectType(tagged_exponent, HEAP_NUMBER_TYPE, ecx);
4032 DeoptimizeIf(not_equal, instr, "not a heap number");
4034 MathPowStub stub(isolate(), MathPowStub::TAGGED);
4036 } else if (exponent_type.IsInteger32()) {
4037 MathPowStub stub(isolate(), MathPowStub::INTEGER);
4040 DCHECK(exponent_type.IsDouble());
4041 MathPowStub stub(isolate(), MathPowStub::DOUBLE);
4047 void LCodeGen::DoMathLog(LMathLog* instr) {
4048 DCHECK(instr->value()->Equals(instr->result()));
4049 XMMRegister input_reg = ToDoubleRegister(instr->value());
4050 XMMRegister xmm_scratch = double_scratch0();
4051 Label positive, done, zero;
4052 __ xorps(xmm_scratch, xmm_scratch);
4053 __ ucomisd(input_reg, xmm_scratch);
4054 __ j(above, &positive, Label::kNear);
4055 __ j(not_carry, &zero, Label::kNear);
4056 ExternalReference nan =
4057 ExternalReference::address_of_canonical_non_hole_nan();
4058 __ movsd(input_reg, Operand::StaticVariable(nan));
4059 __ jmp(&done, Label::kNear);
4061 ExternalReference ninf =
4062 ExternalReference::address_of_negative_infinity();
4063 __ movsd(input_reg, Operand::StaticVariable(ninf));
4064 __ jmp(&done, Label::kNear);
4067 __ sub(Operand(esp), Immediate(kDoubleSize));
4068 __ movsd(Operand(esp, 0), input_reg);
4069 __ fld_d(Operand(esp, 0));
4071 __ fstp_d(Operand(esp, 0));
4072 __ movsd(input_reg, Operand(esp, 0));
4073 __ add(Operand(esp), Immediate(kDoubleSize));
4078 void LCodeGen::DoMathClz32(LMathClz32* instr) {
4079 Register input = ToRegister(instr->value());
4080 Register result = ToRegister(instr->result());
4081 Label not_zero_input;
4082 __ bsr(result, input);
4084 __ j(not_zero, ¬_zero_input);
4085 __ Move(result, Immediate(63)); // 63^31 == 32
4087 __ bind(¬_zero_input);
4088 __ xor_(result, Immediate(31)); // for x in [0..31], 31^x == 31-x.
4092 void LCodeGen::DoMathExp(LMathExp* instr) {
4093 XMMRegister input = ToDoubleRegister(instr->value());
4094 XMMRegister result = ToDoubleRegister(instr->result());
4095 XMMRegister temp0 = double_scratch0();
4096 Register temp1 = ToRegister(instr->temp1());
4097 Register temp2 = ToRegister(instr->temp2());
4099 MathExpGenerator::EmitMathExp(masm(), input, result, temp0, temp1, temp2);
4103 void LCodeGen::DoInvokeFunction(LInvokeFunction* instr) {
4104 DCHECK(ToRegister(instr->context()).is(esi));
4105 DCHECK(ToRegister(instr->function()).is(edi));
4106 DCHECK(instr->HasPointerMap());
4108 Handle<JSFunction> known_function = instr->hydrogen()->known_function();
4109 if (known_function.is_null()) {
4110 LPointerMap* pointers = instr->pointer_map();
4111 SafepointGenerator generator(
4112 this, pointers, Safepoint::kLazyDeopt);
4113 ParameterCount count(instr->arity());
4114 __ InvokeFunction(edi, count, CALL_FUNCTION, generator);
4116 CallKnownFunction(known_function,
4117 instr->hydrogen()->formal_parameter_count(),
4120 EDI_CONTAINS_TARGET);
4125 void LCodeGen::DoCallFunction(LCallFunction* instr) {
4126 DCHECK(ToRegister(instr->context()).is(esi));
4127 DCHECK(ToRegister(instr->function()).is(edi));
4128 DCHECK(ToRegister(instr->result()).is(eax));
4130 int arity = instr->arity();
4131 CallFunctionStub stub(isolate(), arity, instr->hydrogen()->function_flags());
4132 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
4136 void LCodeGen::DoCallNew(LCallNew* instr) {
4137 DCHECK(ToRegister(instr->context()).is(esi));
4138 DCHECK(ToRegister(instr->constructor()).is(edi));
4139 DCHECK(ToRegister(instr->result()).is(eax));
4141 // No cell in ebx for construct type feedback in optimized code
4142 __ mov(ebx, isolate()->factory()->undefined_value());
4143 CallConstructStub stub(isolate(), NO_CALL_CONSTRUCTOR_FLAGS);
4144 __ Move(eax, Immediate(instr->arity()));
4145 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4149 void LCodeGen::DoCallNewArray(LCallNewArray* instr) {
4150 DCHECK(ToRegister(instr->context()).is(esi));
4151 DCHECK(ToRegister(instr->constructor()).is(edi));
4152 DCHECK(ToRegister(instr->result()).is(eax));
4154 __ Move(eax, Immediate(instr->arity()));
4155 __ mov(ebx, isolate()->factory()->undefined_value());
4156 ElementsKind kind = instr->hydrogen()->elements_kind();
4157 AllocationSiteOverrideMode override_mode =
4158 (AllocationSite::GetMode(kind) == TRACK_ALLOCATION_SITE)
4159 ? DISABLE_ALLOCATION_SITES
4162 if (instr->arity() == 0) {
4163 ArrayNoArgumentConstructorStub stub(isolate(), kind, override_mode);
4164 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4165 } else if (instr->arity() == 1) {
4167 if (IsFastPackedElementsKind(kind)) {
4169 // We might need a change here
4170 // look at the first argument
4171 __ mov(ecx, Operand(esp, 0));
4173 __ j(zero, &packed_case, Label::kNear);
4175 ElementsKind holey_kind = GetHoleyElementsKind(kind);
4176 ArraySingleArgumentConstructorStub stub(isolate(),
4179 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4180 __ jmp(&done, Label::kNear);
4181 __ bind(&packed_case);
4184 ArraySingleArgumentConstructorStub stub(isolate(), kind, override_mode);
4185 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4188 ArrayNArgumentsConstructorStub stub(isolate(), kind, override_mode);
4189 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4194 void LCodeGen::DoCallRuntime(LCallRuntime* instr) {
4195 DCHECK(ToRegister(instr->context()).is(esi));
4196 CallRuntime(instr->function(), instr->arity(), instr, instr->save_doubles());
4200 void LCodeGen::DoStoreCodeEntry(LStoreCodeEntry* instr) {
4201 Register function = ToRegister(instr->function());
4202 Register code_object = ToRegister(instr->code_object());
4203 __ lea(code_object, FieldOperand(code_object, Code::kHeaderSize));
4204 __ mov(FieldOperand(function, JSFunction::kCodeEntryOffset), code_object);
4208 void LCodeGen::DoInnerAllocatedObject(LInnerAllocatedObject* instr) {
4209 Register result = ToRegister(instr->result());
4210 Register base = ToRegister(instr->base_object());
4211 if (instr->offset()->IsConstantOperand()) {
4212 LConstantOperand* offset = LConstantOperand::cast(instr->offset());
4213 __ lea(result, Operand(base, ToInteger32(offset)));
4215 Register offset = ToRegister(instr->offset());
4216 __ lea(result, Operand(base, offset, times_1, 0));
4221 void LCodeGen::DoStoreNamedField(LStoreNamedField* instr) {
4222 Representation representation = instr->hydrogen()->field_representation();
4224 HObjectAccess access = instr->hydrogen()->access();
4225 int offset = access.offset();
4227 if (access.IsExternalMemory()) {
4228 DCHECK(!instr->hydrogen()->NeedsWriteBarrier());
4229 MemOperand operand = instr->object()->IsConstantOperand()
4230 ? MemOperand::StaticVariable(
4231 ToExternalReference(LConstantOperand::cast(instr->object())))
4232 : MemOperand(ToRegister(instr->object()), offset);
4233 if (instr->value()->IsConstantOperand()) {
4234 LConstantOperand* operand_value = LConstantOperand::cast(instr->value());
4235 __ mov(operand, Immediate(ToInteger32(operand_value)));
4237 Register value = ToRegister(instr->value());
4238 __ Store(value, operand, representation);
4243 Register object = ToRegister(instr->object());
4244 __ AssertNotSmi(object);
4246 DCHECK(!representation.IsSmi() ||
4247 !instr->value()->IsConstantOperand() ||
4248 IsSmi(LConstantOperand::cast(instr->value())));
4249 if (representation.IsDouble()) {
4250 DCHECK(access.IsInobject());
4251 DCHECK(!instr->hydrogen()->has_transition());
4252 DCHECK(!instr->hydrogen()->NeedsWriteBarrier());
4253 XMMRegister value = ToDoubleRegister(instr->value());
4254 __ movsd(FieldOperand(object, offset), value);
4258 if (instr->hydrogen()->has_transition()) {
4259 Handle<Map> transition = instr->hydrogen()->transition_map();
4260 AddDeprecationDependency(transition);
4261 __ mov(FieldOperand(object, HeapObject::kMapOffset), transition);
4262 if (instr->hydrogen()->NeedsWriteBarrierForMap()) {
4263 Register temp = ToRegister(instr->temp());
4264 Register temp_map = ToRegister(instr->temp_map());
4265 // Update the write barrier for the map field.
4266 __ RecordWriteForMap(object, transition, temp_map, temp, kSaveFPRegs);
4271 Register write_register = object;
4272 if (!access.IsInobject()) {
4273 write_register = ToRegister(instr->temp());
4274 __ mov(write_register, FieldOperand(object, JSObject::kPropertiesOffset));
4277 MemOperand operand = FieldOperand(write_register, offset);
4278 if (instr->value()->IsConstantOperand()) {
4279 LConstantOperand* operand_value = LConstantOperand::cast(instr->value());
4280 if (operand_value->IsRegister()) {
4281 Register value = ToRegister(operand_value);
4282 __ Store(value, operand, representation);
4283 } else if (representation.IsInteger32()) {
4284 Immediate immediate = ToImmediate(operand_value, representation);
4285 DCHECK(!instr->hydrogen()->NeedsWriteBarrier());
4286 __ mov(operand, immediate);
4288 Handle<Object> handle_value = ToHandle(operand_value);
4289 DCHECK(!instr->hydrogen()->NeedsWriteBarrier());
4290 __ mov(operand, handle_value);
4293 Register value = ToRegister(instr->value());
4294 __ Store(value, operand, representation);
4297 if (instr->hydrogen()->NeedsWriteBarrier()) {
4298 Register value = ToRegister(instr->value());
4299 Register temp = access.IsInobject() ? ToRegister(instr->temp()) : object;
4300 // Update the write barrier for the object for in-object properties.
4301 __ RecordWriteField(write_register,
4306 EMIT_REMEMBERED_SET,
4307 instr->hydrogen()->SmiCheckForWriteBarrier(),
4308 instr->hydrogen()->PointersToHereCheckForValue());
4313 void LCodeGen::DoStoreNamedGeneric(LStoreNamedGeneric* instr) {
4314 DCHECK(ToRegister(instr->context()).is(esi));
4315 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4316 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
4318 __ mov(StoreDescriptor::NameRegister(), instr->name());
4319 Handle<Code> ic = StoreIC::initialize_stub(isolate(), instr->strict_mode());
4320 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4324 void LCodeGen::DoBoundsCheck(LBoundsCheck* instr) {
4325 Condition cc = instr->hydrogen()->allow_equality() ? above : above_equal;
4326 BuiltinFunctionId op = instr->hydrogen()->op();
4327 if (IsSIMD128LoadStoreOp(op)) {
4329 Register index_in_bytes = ToRegister(instr->temp0());
4330 Register length_in_bytes = ToRegister(instr->temp1());
4331 if (instr->index()->IsConstantOperand())
4332 __ mov(index_in_bytes, ToImmediate(LConstantOperand::cast(instr->index()),
4333 instr->hydrogen()->index()->representation()));
4335 __ mov(index_in_bytes, ToOperand(instr->index()));
4336 int index_shift_size =
4337 ElementsKindToShiftSize(instr->hydrogen()->element_kind());
4338 DCHECK(index_shift_size >= 0);
4339 if (index_shift_size > 0)
4340 __ shl(index_in_bytes, index_shift_size);
4341 int bytes = GetSIMD128LoadStoreBytes(op);
4342 __ add(index_in_bytes, Immediate(bytes));
4343 if (instr->length()->IsConstantOperand())
4344 __ mov(length_in_bytes,
4345 ToImmediate(LConstantOperand::cast(instr->length()),
4346 instr->hydrogen()->length()->representation()));
4348 __ mov(length_in_bytes, ToOperand(instr->length()));
4349 int length_shift_size =
4350 ElementsKindToShiftSize(instr->hydrogen()->element_kind());
4351 DCHECK(length_shift_size >= 0);
4352 if (length_shift_size > 0)
4353 __ shl(length_in_bytes, length_shift_size);
4354 __ cmp(index_in_bytes, length_in_bytes);
4356 if (instr->index()->IsConstantOperand()) {
4357 __ cmp(ToOperand(instr->length()),
4358 ToImmediate(LConstantOperand::cast(instr->index()),
4359 instr->hydrogen()->length()->representation()));
4360 cc = CommuteCondition(cc);
4361 } else if (instr->length()->IsConstantOperand()) {
4362 __ cmp(ToOperand(instr->index()),
4363 ToImmediate(LConstantOperand::cast(instr->length()),
4364 instr->hydrogen()->index()->representation()));
4366 __ cmp(ToRegister(instr->index()), ToOperand(instr->length()));
4369 if (FLAG_debug_code && instr->hydrogen()->skip_check()) {
4371 __ j(NegateCondition(cc), &done, Label::kNear);
4375 DeoptimizeIf(cc, instr, "out of bounds");
4380 void LCodeGen::DoStoreKeyedExternalArray(LStoreKeyed* instr) {
4381 ElementsKind elements_kind = instr->elements_kind();
4382 LOperand* key = instr->key();
4383 if (!key->IsConstantOperand() &&
4384 ExternalArrayOpRequiresTemp(instr->hydrogen()->key()->representation(),
4386 HandleExternalArrayOpRequiresTemp(
4387 key, instr->hydrogen()->key()->representation(), elements_kind);
4390 Operand operand(BuildFastArrayOperand(
4393 instr->hydrogen()->key()->representation(),
4395 instr->base_offset()));
4396 BuiltinFunctionId op = instr->hydrogen()->op();
4397 if (IsSIMD128LoadStoreOp(op)) {
4398 if (GetSIMD128LoadStoreBytes(op) == 16) {
4399 __ movups(operand, ToSIMD128Register(instr->value()));
4400 } else if (GetSIMD128LoadStoreBytes(op) == 4) {
4401 __ movss(operand, ToSIMD128Register(instr->value()));
4402 } else if (GetSIMD128LoadStoreBytes(op) == 8) {
4403 __ movq(operand, ToSIMD128Register(instr->value()));
4404 } else if (GetSIMD128LoadStoreBytes(op) == 12) {
4405 XMMRegister value(ToSIMD128Register(instr->value()));
4406 XMMRegister xmm_scratch = double_scratch0();
4407 __ movq(operand, value);
4408 Operand operand2(BuildFastArrayOperand(
4411 instr->hydrogen()->key()->representation(),
4413 instr->base_offset() + 8));
4414 __ movhlps(xmm_scratch, value);
4415 __ movss(operand2, xmm_scratch);
4417 } else if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
4418 elements_kind == FLOAT32_ELEMENTS) {
4419 XMMRegister xmm_scratch = double_scratch0();
4420 __ cvtsd2ss(xmm_scratch, ToDoubleRegister(instr->value()));
4421 __ movss(operand, xmm_scratch);
4422 } else if (elements_kind == EXTERNAL_FLOAT64_ELEMENTS ||
4423 elements_kind == FLOAT64_ELEMENTS) {
4424 __ movsd(operand, ToDoubleRegister(instr->value()));
4425 } else if (IsSIMD128ElementsKind(elements_kind)) {
4426 __ movups(operand, ToSIMD128Register(instr->value()));
4428 Register value = ToRegister(instr->value());
4429 switch (elements_kind) {
4430 case EXTERNAL_UINT8_CLAMPED_ELEMENTS:
4431 case EXTERNAL_UINT8_ELEMENTS:
4432 case EXTERNAL_INT8_ELEMENTS:
4433 case UINT8_ELEMENTS:
4435 case UINT8_CLAMPED_ELEMENTS:
4436 __ mov_b(operand, value);
4438 case EXTERNAL_INT16_ELEMENTS:
4439 case EXTERNAL_UINT16_ELEMENTS:
4440 case UINT16_ELEMENTS:
4441 case INT16_ELEMENTS:
4442 __ mov_w(operand, value);
4444 case EXTERNAL_INT32_ELEMENTS:
4445 case EXTERNAL_UINT32_ELEMENTS:
4446 case UINT32_ELEMENTS:
4447 case INT32_ELEMENTS:
4448 __ mov(operand, value);
4450 case EXTERNAL_FLOAT32_ELEMENTS:
4451 case EXTERNAL_FLOAT64_ELEMENTS:
4452 case EXTERNAL_FLOAT32x4_ELEMENTS:
4453 case EXTERNAL_FLOAT64x2_ELEMENTS:
4454 case EXTERNAL_INT32x4_ELEMENTS:
4455 case FLOAT32_ELEMENTS:
4456 case FLOAT64_ELEMENTS:
4457 case FLOAT32x4_ELEMENTS:
4458 case FLOAT64x2_ELEMENTS:
4459 case INT32x4_ELEMENTS:
4460 case FAST_SMI_ELEMENTS:
4462 case FAST_DOUBLE_ELEMENTS:
4463 case FAST_HOLEY_SMI_ELEMENTS:
4464 case FAST_HOLEY_ELEMENTS:
4465 case FAST_HOLEY_DOUBLE_ELEMENTS:
4466 case DICTIONARY_ELEMENTS:
4467 case SLOPPY_ARGUMENTS_ELEMENTS:
4475 void LCodeGen::DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr) {
4476 ExternalReference canonical_nan_reference =
4477 ExternalReference::address_of_canonical_non_hole_nan();
4478 Operand double_store_operand = BuildFastArrayOperand(
4481 instr->hydrogen()->key()->representation(),
4482 FAST_DOUBLE_ELEMENTS,
4483 instr->base_offset());
4485 XMMRegister value = ToDoubleRegister(instr->value());
4487 if (instr->NeedsCanonicalization()) {
4490 __ ucomisd(value, value);
4491 __ j(parity_odd, &have_value, Label::kNear); // NaN.
4493 __ movsd(value, Operand::StaticVariable(canonical_nan_reference));
4494 __ bind(&have_value);
4497 __ movsd(double_store_operand, value);
4501 void LCodeGen::DoStoreKeyedFixedArray(LStoreKeyed* instr) {
4502 Register elements = ToRegister(instr->elements());
4503 Register key = instr->key()->IsRegister() ? ToRegister(instr->key()) : no_reg;
4505 Operand operand = BuildFastArrayOperand(
4508 instr->hydrogen()->key()->representation(),
4510 instr->base_offset());
4511 if (instr->value()->IsRegister()) {
4512 __ mov(operand, ToRegister(instr->value()));
4514 LConstantOperand* operand_value = LConstantOperand::cast(instr->value());
4515 if (IsSmi(operand_value)) {
4516 Immediate immediate = ToImmediate(operand_value, Representation::Smi());
4517 __ mov(operand, immediate);
4519 DCHECK(!IsInteger32(operand_value));
4520 Handle<Object> handle_value = ToHandle(operand_value);
4521 __ mov(operand, handle_value);
4525 if (instr->hydrogen()->NeedsWriteBarrier()) {
4526 DCHECK(instr->value()->IsRegister());
4527 Register value = ToRegister(instr->value());
4528 DCHECK(!instr->key()->IsConstantOperand());
4529 SmiCheck check_needed =
4530 instr->hydrogen()->value()->type().IsHeapObject()
4531 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
4532 // Compute address of modified element and store it into key register.
4533 __ lea(key, operand);
4534 __ RecordWrite(elements,
4538 EMIT_REMEMBERED_SET,
4540 instr->hydrogen()->PointersToHereCheckForValue());
4545 void LCodeGen::DoStoreKeyed(LStoreKeyed* instr) {
4546 // By cases...external, fast-double, fast
4547 if (instr->is_typed_elements()) {
4548 DoStoreKeyedExternalArray(instr);
4549 } else if (instr->hydrogen()->value()->representation().IsDouble()) {
4550 DoStoreKeyedFixedDoubleArray(instr);
4552 DoStoreKeyedFixedArray(instr);
4557 void LCodeGen::DoStoreKeyedGeneric(LStoreKeyedGeneric* instr) {
4558 DCHECK(ToRegister(instr->context()).is(esi));
4559 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4560 DCHECK(ToRegister(instr->key()).is(StoreDescriptor::NameRegister()));
4561 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
4564 CodeFactory::KeyedStoreIC(isolate(), instr->strict_mode()).code();
4565 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4569 void LCodeGen::DoTrapAllocationMemento(LTrapAllocationMemento* instr) {
4570 Register object = ToRegister(instr->object());
4571 Register temp = ToRegister(instr->temp());
4572 Label no_memento_found;
4573 __ TestJSArrayForAllocationMemento(object, temp, &no_memento_found);
4574 DeoptimizeIf(equal, instr, "memento found");
4575 __ bind(&no_memento_found);
4579 void LCodeGen::DoTransitionElementsKind(LTransitionElementsKind* instr) {
4580 Register object_reg = ToRegister(instr->object());
4582 Handle<Map> from_map = instr->original_map();
4583 Handle<Map> to_map = instr->transitioned_map();
4584 ElementsKind from_kind = instr->from_kind();
4585 ElementsKind to_kind = instr->to_kind();
4587 Label not_applicable;
4588 bool is_simple_map_transition =
4589 IsSimpleMapChangeTransition(from_kind, to_kind);
4590 Label::Distance branch_distance =
4591 is_simple_map_transition ? Label::kNear : Label::kFar;
4592 __ cmp(FieldOperand(object_reg, HeapObject::kMapOffset), from_map);
4593 __ j(not_equal, ¬_applicable, branch_distance);
4594 if (is_simple_map_transition) {
4595 Register new_map_reg = ToRegister(instr->new_map_temp());
4596 __ mov(FieldOperand(object_reg, HeapObject::kMapOffset),
4599 DCHECK_NE(instr->temp(), NULL);
4600 __ RecordWriteForMap(object_reg, to_map, new_map_reg,
4601 ToRegister(instr->temp()),
4604 DCHECK(ToRegister(instr->context()).is(esi));
4605 DCHECK(object_reg.is(eax));
4606 PushSafepointRegistersScope scope(this);
4607 __ mov(ebx, to_map);
4608 bool is_js_array = from_map->instance_type() == JS_ARRAY_TYPE;
4609 TransitionElementsKindStub stub(isolate(), from_kind, to_kind, is_js_array);
4611 RecordSafepointWithLazyDeopt(instr,
4612 RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
4614 __ bind(¬_applicable);
4618 void LCodeGen::DoStringCharCodeAt(LStringCharCodeAt* instr) {
4619 class DeferredStringCharCodeAt FINAL : public LDeferredCode {
4621 DeferredStringCharCodeAt(LCodeGen* codegen,
4622 LStringCharCodeAt* instr)
4623 : LDeferredCode(codegen), instr_(instr) { }
4624 virtual void Generate() OVERRIDE {
4625 codegen()->DoDeferredStringCharCodeAt(instr_);
4627 virtual LInstruction* instr() OVERRIDE { return instr_; }
4629 LStringCharCodeAt* instr_;
4632 DeferredStringCharCodeAt* deferred =
4633 new(zone()) DeferredStringCharCodeAt(this, instr);
4635 StringCharLoadGenerator::Generate(masm(),
4637 ToRegister(instr->string()),
4638 ToRegister(instr->index()),
4639 ToRegister(instr->result()),
4641 __ bind(deferred->exit());
4645 void LCodeGen::DoDeferredStringCharCodeAt(LStringCharCodeAt* instr) {
4646 Register string = ToRegister(instr->string());
4647 Register result = ToRegister(instr->result());
4649 // TODO(3095996): Get rid of this. For now, we need to make the
4650 // result register contain a valid pointer because it is already
4651 // contained in the register pointer map.
4652 __ Move(result, Immediate(0));
4654 PushSafepointRegistersScope scope(this);
4656 // Push the index as a smi. This is safe because of the checks in
4657 // DoStringCharCodeAt above.
4658 STATIC_ASSERT(String::kMaxLength <= Smi::kMaxValue);
4659 if (instr->index()->IsConstantOperand()) {
4660 Immediate immediate = ToImmediate(LConstantOperand::cast(instr->index()),
4661 Representation::Smi());
4664 Register index = ToRegister(instr->index());
4668 CallRuntimeFromDeferred(Runtime::kStringCharCodeAtRT, 2,
4669 instr, instr->context());
4672 __ StoreToSafepointRegisterSlot(result, eax);
4676 void LCodeGen::DoStringCharFromCode(LStringCharFromCode* instr) {
4677 class DeferredStringCharFromCode FINAL : public LDeferredCode {
4679 DeferredStringCharFromCode(LCodeGen* codegen,
4680 LStringCharFromCode* instr)
4681 : LDeferredCode(codegen), instr_(instr) { }
4682 virtual void Generate() OVERRIDE {
4683 codegen()->DoDeferredStringCharFromCode(instr_);
4685 virtual LInstruction* instr() OVERRIDE { return instr_; }
4687 LStringCharFromCode* instr_;
4690 DeferredStringCharFromCode* deferred =
4691 new(zone()) DeferredStringCharFromCode(this, instr);
4693 DCHECK(instr->hydrogen()->value()->representation().IsInteger32());
4694 Register char_code = ToRegister(instr->char_code());
4695 Register result = ToRegister(instr->result());
4696 DCHECK(!char_code.is(result));
4698 __ cmp(char_code, String::kMaxOneByteCharCode);
4699 __ j(above, deferred->entry());
4700 __ Move(result, Immediate(factory()->single_character_string_cache()));
4701 __ mov(result, FieldOperand(result,
4702 char_code, times_pointer_size,
4703 FixedArray::kHeaderSize));
4704 __ cmp(result, factory()->undefined_value());
4705 __ j(equal, deferred->entry());
4706 __ bind(deferred->exit());
4710 void LCodeGen::DoDeferredStringCharFromCode(LStringCharFromCode* instr) {
4711 Register char_code = ToRegister(instr->char_code());
4712 Register result = ToRegister(instr->result());
4714 // TODO(3095996): Get rid of this. For now, we need to make the
4715 // result register contain a valid pointer because it is already
4716 // contained in the register pointer map.
4717 __ Move(result, Immediate(0));
4719 PushSafepointRegistersScope scope(this);
4720 __ SmiTag(char_code);
4722 CallRuntimeFromDeferred(Runtime::kCharFromCode, 1, instr, instr->context());
4723 __ StoreToSafepointRegisterSlot(result, eax);
4727 void LCodeGen::DoStringAdd(LStringAdd* instr) {
4728 DCHECK(ToRegister(instr->context()).is(esi));
4729 DCHECK(ToRegister(instr->left()).is(edx));
4730 DCHECK(ToRegister(instr->right()).is(eax));
4731 StringAddStub stub(isolate(),
4732 instr->hydrogen()->flags(),
4733 instr->hydrogen()->pretenure_flag());
4734 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
4738 void LCodeGen::DoInteger32ToDouble(LInteger32ToDouble* instr) {
4739 LOperand* input = instr->value();
4740 LOperand* output = instr->result();
4741 DCHECK(input->IsRegister() || input->IsStackSlot());
4742 DCHECK(output->IsDoubleRegister());
4743 __ Cvtsi2sd(ToDoubleRegister(output), ToOperand(input));
4747 void LCodeGen::DoUint32ToDouble(LUint32ToDouble* instr) {
4748 LOperand* input = instr->value();
4749 LOperand* output = instr->result();
4750 __ LoadUint32(ToDoubleRegister(output), ToRegister(input));
4754 void LCodeGen::DoNumberTagI(LNumberTagI* instr) {
4755 class DeferredNumberTagI FINAL : public LDeferredCode {
4757 DeferredNumberTagI(LCodeGen* codegen,
4759 : LDeferredCode(codegen), instr_(instr) { }
4760 virtual void Generate() OVERRIDE {
4761 codegen()->DoDeferredNumberTagIU(
4762 instr_, instr_->value(), instr_->temp(), SIGNED_INT32);
4764 virtual LInstruction* instr() OVERRIDE { return instr_; }
4766 LNumberTagI* instr_;
4769 LOperand* input = instr->value();
4770 DCHECK(input->IsRegister() && input->Equals(instr->result()));
4771 Register reg = ToRegister(input);
4773 DeferredNumberTagI* deferred =
4774 new(zone()) DeferredNumberTagI(this, instr);
4776 __ j(overflow, deferred->entry());
4777 __ bind(deferred->exit());
4781 void LCodeGen::DoNumberTagU(LNumberTagU* instr) {
4782 class DeferredNumberTagU FINAL : public LDeferredCode {
4784 DeferredNumberTagU(LCodeGen* codegen, LNumberTagU* instr)
4785 : LDeferredCode(codegen), instr_(instr) { }
4786 virtual void Generate() OVERRIDE {
4787 codegen()->DoDeferredNumberTagIU(
4788 instr_, instr_->value(), instr_->temp(), UNSIGNED_INT32);
4790 virtual LInstruction* instr() OVERRIDE { return instr_; }
4792 LNumberTagU* instr_;
4795 LOperand* input = instr->value();
4796 DCHECK(input->IsRegister() && input->Equals(instr->result()));
4797 Register reg = ToRegister(input);
4799 DeferredNumberTagU* deferred =
4800 new(zone()) DeferredNumberTagU(this, instr);
4801 __ cmp(reg, Immediate(Smi::kMaxValue));
4802 __ j(above, deferred->entry());
4804 __ bind(deferred->exit());
4808 void LCodeGen::DoDeferredNumberTagIU(LInstruction* instr,
4811 IntegerSignedness signedness) {
4813 Register reg = ToRegister(value);
4814 Register tmp = ToRegister(temp);
4815 XMMRegister xmm_scratch = double_scratch0();
4817 if (signedness == SIGNED_INT32) {
4818 // There was overflow, so bits 30 and 31 of the original integer
4819 // disagree. Try to allocate a heap number in new space and store
4820 // the value in there. If that fails, call the runtime system.
4822 __ xor_(reg, 0x80000000);
4823 __ Cvtsi2sd(xmm_scratch, Operand(reg));
4825 __ LoadUint32(xmm_scratch, reg);
4828 if (FLAG_inline_new) {
4829 __ AllocateHeapNumber(reg, tmp, no_reg, &slow);
4830 __ jmp(&done, Label::kNear);
4833 // Slow case: Call the runtime system to do the number allocation.
4836 // TODO(3095996): Put a valid pointer value in the stack slot where the
4837 // result register is stored, as this register is in the pointer map, but
4838 // contains an integer value.
4839 __ Move(reg, Immediate(0));
4841 // Preserve the value of all registers.
4842 PushSafepointRegistersScope scope(this);
4844 // NumberTagI and NumberTagD use the context from the frame, rather than
4845 // the environment's HContext or HInlinedContext value.
4846 // They only call Runtime::kAllocateHeapNumber.
4847 // The corresponding HChange instructions are added in a phase that does
4848 // not have easy access to the local context.
4849 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
4850 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4851 RecordSafepointWithRegisters(
4852 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4853 __ StoreToSafepointRegisterSlot(reg, eax);
4856 // Done. Put the value in xmm_scratch into the value of the allocated heap
4859 __ movsd(FieldOperand(reg, HeapNumber::kValueOffset), xmm_scratch);
4863 void LCodeGen::DoNumberTagD(LNumberTagD* instr) {
4864 class DeferredNumberTagD FINAL : public LDeferredCode {
4866 DeferredNumberTagD(LCodeGen* codegen, LNumberTagD* instr)
4867 : LDeferredCode(codegen), instr_(instr) { }
4868 virtual void Generate() OVERRIDE {
4869 codegen()->DoDeferredNumberTagD(instr_);
4871 virtual LInstruction* instr() OVERRIDE { return instr_; }
4873 LNumberTagD* instr_;
4876 Register reg = ToRegister(instr->result());
4878 DeferredNumberTagD* deferred =
4879 new(zone()) DeferredNumberTagD(this, instr);
4880 if (FLAG_inline_new) {
4881 Register tmp = ToRegister(instr->temp());
4882 __ AllocateHeapNumber(reg, tmp, no_reg, deferred->entry());
4884 __ jmp(deferred->entry());
4886 __ bind(deferred->exit());
4887 XMMRegister input_reg = ToDoubleRegister(instr->value());
4888 __ movsd(FieldOperand(reg, HeapNumber::kValueOffset), input_reg);
4892 void LCodeGen::DoDeferredNumberTagD(LNumberTagD* instr) {
4893 // TODO(3095996): Get rid of this. For now, we need to make the
4894 // result register contain a valid pointer because it is already
4895 // contained in the register pointer map.
4896 Register reg = ToRegister(instr->result());
4897 __ Move(reg, Immediate(0));
4899 PushSafepointRegistersScope scope(this);
4900 // NumberTagI and NumberTagD use the context from the frame, rather than
4901 // the environment's HContext or HInlinedContext value.
4902 // They only call Runtime::kAllocateHeapNumber.
4903 // The corresponding HChange instructions are added in a phase that does
4904 // not have easy access to the local context.
4905 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
4906 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4907 RecordSafepointWithRegisters(
4908 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4909 __ StoreToSafepointRegisterSlot(reg, eax);
4913 void LCodeGen::DoSmiTag(LSmiTag* instr) {
4914 HChange* hchange = instr->hydrogen();
4915 Register input = ToRegister(instr->value());
4916 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4917 hchange->value()->CheckFlag(HValue::kUint32)) {
4918 __ test(input, Immediate(0xc0000000));
4919 DeoptimizeIf(not_zero, instr, "overflow");
4922 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4923 !hchange->value()->CheckFlag(HValue::kUint32)) {
4924 DeoptimizeIf(overflow, instr, "overflow");
4929 void LCodeGen::DoSmiUntag(LSmiUntag* instr) {
4930 LOperand* input = instr->value();
4931 Register result = ToRegister(input);
4932 DCHECK(input->IsRegister() && input->Equals(instr->result()));
4933 if (instr->needs_check()) {
4934 __ test(result, Immediate(kSmiTagMask));
4935 DeoptimizeIf(not_zero, instr, "not a Smi");
4937 __ AssertSmi(result);
4939 __ SmiUntag(result);
4943 void LCodeGen::EmitNumberUntagD(LNumberUntagD* instr, Register input_reg,
4944 Register temp_reg, XMMRegister result_reg,
4945 NumberUntagDMode mode) {
4946 bool can_convert_undefined_to_nan =
4947 instr->hydrogen()->can_convert_undefined_to_nan();
4948 bool deoptimize_on_minus_zero = instr->hydrogen()->deoptimize_on_minus_zero();
4950 Label convert, load_smi, done;
4952 if (mode == NUMBER_CANDIDATE_IS_ANY_TAGGED) {
4954 __ JumpIfSmi(input_reg, &load_smi, Label::kNear);
4956 // Heap number map check.
4957 __ cmp(FieldOperand(input_reg, HeapObject::kMapOffset),
4958 factory()->heap_number_map());
4959 if (can_convert_undefined_to_nan) {
4960 __ j(not_equal, &convert, Label::kNear);
4962 DeoptimizeIf(not_equal, instr, "not a heap number");
4965 // Heap number to XMM conversion.
4966 __ movsd(result_reg, FieldOperand(input_reg, HeapNumber::kValueOffset));
4968 if (deoptimize_on_minus_zero) {
4969 XMMRegister xmm_scratch = double_scratch0();
4970 __ xorps(xmm_scratch, xmm_scratch);
4971 __ ucomisd(result_reg, xmm_scratch);
4972 __ j(not_zero, &done, Label::kNear);
4973 __ movmskpd(temp_reg, result_reg);
4974 __ test_b(temp_reg, 1);
4975 DeoptimizeIf(not_zero, instr, "minus zero");
4977 __ jmp(&done, Label::kNear);
4979 if (can_convert_undefined_to_nan) {
4982 // Convert undefined (and hole) to NaN.
4983 __ cmp(input_reg, factory()->undefined_value());
4984 DeoptimizeIf(not_equal, instr, "not a heap number/undefined");
4986 ExternalReference nan =
4987 ExternalReference::address_of_canonical_non_hole_nan();
4988 __ movsd(result_reg, Operand::StaticVariable(nan));
4989 __ jmp(&done, Label::kNear);
4992 DCHECK(mode == NUMBER_CANDIDATE_IS_SMI);
4996 // Smi to XMM conversion. Clobbering a temp is faster than re-tagging the
4997 // input register since we avoid dependencies.
4998 __ mov(temp_reg, input_reg);
4999 __ SmiUntag(temp_reg); // Untag smi before converting to float.
5000 __ Cvtsi2sd(result_reg, Operand(temp_reg));
5005 void LCodeGen::DoDeferredTaggedToI(LTaggedToI* instr, Label* done) {
5006 Register input_reg = ToRegister(instr->value());
5008 // The input was optimistically untagged; revert it.
5009 STATIC_ASSERT(kSmiTagSize == 1);
5010 __ lea(input_reg, Operand(input_reg, times_2, kHeapObjectTag));
5012 if (instr->truncating()) {
5013 Label no_heap_number, check_bools, check_false;
5015 // Heap number map check.
5016 __ cmp(FieldOperand(input_reg, HeapObject::kMapOffset),
5017 factory()->heap_number_map());
5018 __ j(not_equal, &no_heap_number, Label::kNear);
5019 __ TruncateHeapNumberToI(input_reg, input_reg);
5022 __ bind(&no_heap_number);
5023 // Check for Oddballs. Undefined/False is converted to zero and True to one
5024 // for truncating conversions.
5025 __ cmp(input_reg, factory()->undefined_value());
5026 __ j(not_equal, &check_bools, Label::kNear);
5027 __ Move(input_reg, Immediate(0));
5030 __ bind(&check_bools);
5031 __ cmp(input_reg, factory()->true_value());
5032 __ j(not_equal, &check_false, Label::kNear);
5033 __ Move(input_reg, Immediate(1));
5036 __ bind(&check_false);
5037 __ cmp(input_reg, factory()->false_value());
5038 DeoptimizeIf(not_equal, instr, "not a heap number/undefined/true/false");
5039 __ Move(input_reg, Immediate(0));
5041 XMMRegister scratch = ToDoubleRegister(instr->temp());
5042 DCHECK(!scratch.is(xmm0));
5043 __ cmp(FieldOperand(input_reg, HeapObject::kMapOffset),
5044 isolate()->factory()->heap_number_map());
5045 DeoptimizeIf(not_equal, instr, "not a heap number");
5046 __ movsd(xmm0, FieldOperand(input_reg, HeapNumber::kValueOffset));
5047 __ cvttsd2si(input_reg, Operand(xmm0));
5048 __ Cvtsi2sd(scratch, Operand(input_reg));
5049 __ ucomisd(xmm0, scratch);
5050 DeoptimizeIf(not_equal, instr, "lost precision");
5051 DeoptimizeIf(parity_even, instr, "NaN");
5052 if (instr->hydrogen()->GetMinusZeroMode() == FAIL_ON_MINUS_ZERO) {
5053 __ test(input_reg, Operand(input_reg));
5054 __ j(not_zero, done);
5055 __ movmskpd(input_reg, xmm0);
5056 __ and_(input_reg, 1);
5057 DeoptimizeIf(not_zero, instr, "minus zero");
5063 void LCodeGen::DoTaggedToI(LTaggedToI* instr) {
5064 class DeferredTaggedToI FINAL : public LDeferredCode {
5066 DeferredTaggedToI(LCodeGen* codegen, LTaggedToI* instr)
5067 : LDeferredCode(codegen), instr_(instr) { }
5068 virtual void Generate() OVERRIDE {
5069 codegen()->DoDeferredTaggedToI(instr_, done());
5071 virtual LInstruction* instr() OVERRIDE { return instr_; }
5076 LOperand* input = instr->value();
5077 DCHECK(input->IsRegister());
5078 Register input_reg = ToRegister(input);
5079 DCHECK(input_reg.is(ToRegister(instr->result())));
5081 if (instr->hydrogen()->value()->representation().IsSmi()) {
5082 __ SmiUntag(input_reg);
5084 DeferredTaggedToI* deferred =
5085 new(zone()) DeferredTaggedToI(this, instr);
5086 // Optimistically untag the input.
5087 // If the input is a HeapObject, SmiUntag will set the carry flag.
5088 STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
5089 __ SmiUntag(input_reg);
5090 // Branch to deferred code if the input was tagged.
5091 // The deferred code will take care of restoring the tag.
5092 __ j(carry, deferred->entry());
5093 __ bind(deferred->exit());
5098 void LCodeGen::DoNumberUntagD(LNumberUntagD* instr) {
5099 LOperand* input = instr->value();
5100 DCHECK(input->IsRegister());
5101 LOperand* temp = instr->temp();
5102 DCHECK(temp->IsRegister());
5103 LOperand* result = instr->result();
5104 DCHECK(result->IsDoubleRegister());
5106 Register input_reg = ToRegister(input);
5107 Register temp_reg = ToRegister(temp);
5109 HValue* value = instr->hydrogen()->value();
5110 NumberUntagDMode mode = value->representation().IsSmi()
5111 ? NUMBER_CANDIDATE_IS_SMI : NUMBER_CANDIDATE_IS_ANY_TAGGED;
5113 XMMRegister result_reg = ToDoubleRegister(result);
5114 EmitNumberUntagD(instr, input_reg, temp_reg, result_reg, mode);
5118 void LCodeGen::DoDoubleToI(LDoubleToI* instr) {
5119 LOperand* input = instr->value();
5120 DCHECK(input->IsDoubleRegister());
5121 LOperand* result = instr->result();
5122 DCHECK(result->IsRegister());
5123 Register result_reg = ToRegister(result);
5125 if (instr->truncating()) {
5126 XMMRegister input_reg = ToDoubleRegister(input);
5127 __ TruncateDoubleToI(result_reg, input_reg);
5129 Label lost_precision, is_nan, minus_zero, done;
5130 XMMRegister input_reg = ToDoubleRegister(input);
5131 XMMRegister xmm_scratch = double_scratch0();
5132 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
5133 __ DoubleToI(result_reg, input_reg, xmm_scratch,
5134 instr->hydrogen()->GetMinusZeroMode(), &lost_precision,
5135 &is_nan, &minus_zero, dist);
5136 __ jmp(&done, dist);
5137 __ bind(&lost_precision);
5138 DeoptimizeIf(no_condition, instr, "lost precision");
5140 DeoptimizeIf(no_condition, instr, "NaN");
5141 __ bind(&minus_zero);
5142 DeoptimizeIf(no_condition, instr, "minus zero");
5148 void LCodeGen::DoDoubleToSmi(LDoubleToSmi* instr) {
5149 LOperand* input = instr->value();
5150 DCHECK(input->IsDoubleRegister());
5151 LOperand* result = instr->result();
5152 DCHECK(result->IsRegister());
5153 Register result_reg = ToRegister(result);
5155 Label lost_precision, is_nan, minus_zero, done;
5156 XMMRegister input_reg = ToDoubleRegister(input);
5157 XMMRegister xmm_scratch = double_scratch0();
5158 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
5159 __ DoubleToI(result_reg, input_reg, xmm_scratch,
5160 instr->hydrogen()->GetMinusZeroMode(), &lost_precision, &is_nan,
5162 __ jmp(&done, dist);
5163 __ bind(&lost_precision);
5164 DeoptimizeIf(no_condition, instr, "lost precision");
5166 DeoptimizeIf(no_condition, instr, "NaN");
5167 __ bind(&minus_zero);
5168 DeoptimizeIf(no_condition, instr, "minus zero");
5170 __ SmiTag(result_reg);
5171 DeoptimizeIf(overflow, instr, "overflow");
5175 void LCodeGen::DoCheckSmi(LCheckSmi* instr) {
5176 LOperand* input = instr->value();
5177 __ test(ToOperand(input), Immediate(kSmiTagMask));
5178 DeoptimizeIf(not_zero, instr, "not a Smi");
5182 void LCodeGen::DoCheckNonSmi(LCheckNonSmi* instr) {
5183 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
5184 LOperand* input = instr->value();
5185 __ test(ToOperand(input), Immediate(kSmiTagMask));
5186 DeoptimizeIf(zero, instr, "Smi");
5191 void LCodeGen::DoCheckInstanceType(LCheckInstanceType* instr) {
5192 Register input = ToRegister(instr->value());
5193 Register temp = ToRegister(instr->temp());
5195 __ mov(temp, FieldOperand(input, HeapObject::kMapOffset));
5197 if (instr->hydrogen()->is_interval_check()) {
5200 instr->hydrogen()->GetCheckInterval(&first, &last);
5202 __ cmpb(FieldOperand(temp, Map::kInstanceTypeOffset),
5203 static_cast<int8_t>(first));
5205 // If there is only one type in the interval check for equality.
5206 if (first == last) {
5207 DeoptimizeIf(not_equal, instr, "wrong instance type");
5209 DeoptimizeIf(below, instr, "wrong instance type");
5210 // Omit check for the last type.
5211 if (last != LAST_TYPE) {
5212 __ cmpb(FieldOperand(temp, Map::kInstanceTypeOffset),
5213 static_cast<int8_t>(last));
5214 DeoptimizeIf(above, instr, "wrong instance type");
5220 instr->hydrogen()->GetCheckMaskAndTag(&mask, &tag);
5222 if (base::bits::IsPowerOfTwo32(mask)) {
5223 DCHECK(tag == 0 || base::bits::IsPowerOfTwo32(tag));
5224 __ test_b(FieldOperand(temp, Map::kInstanceTypeOffset), mask);
5225 DeoptimizeIf(tag == 0 ? not_zero : zero, instr, "wrong instance type");
5227 __ movzx_b(temp, FieldOperand(temp, Map::kInstanceTypeOffset));
5228 __ and_(temp, mask);
5230 DeoptimizeIf(not_equal, instr, "wrong instance type");
5236 void LCodeGen::DoCheckValue(LCheckValue* instr) {
5237 Handle<HeapObject> object = instr->hydrogen()->object().handle();
5238 if (instr->hydrogen()->object_in_new_space()) {
5239 Register reg = ToRegister(instr->value());
5240 Handle<Cell> cell = isolate()->factory()->NewCell(object);
5241 __ cmp(reg, Operand::ForCell(cell));
5243 Operand operand = ToOperand(instr->value());
5244 __ cmp(operand, object);
5246 DeoptimizeIf(not_equal, instr, "value mismatch");
5250 void LCodeGen::DoDeferredInstanceMigration(LCheckMaps* instr, Register object) {
5252 PushSafepointRegistersScope scope(this);
5255 __ CallRuntimeSaveDoubles(Runtime::kTryMigrateInstance);
5256 RecordSafepointWithRegisters(
5257 instr->pointer_map(), 1, Safepoint::kNoLazyDeopt);
5259 __ test(eax, Immediate(kSmiTagMask));
5261 DeoptimizeIf(zero, instr, "instance migration failed");
5265 void LCodeGen::DoCheckMaps(LCheckMaps* instr) {
5266 class DeferredCheckMaps FINAL : public LDeferredCode {
5268 DeferredCheckMaps(LCodeGen* codegen, LCheckMaps* instr, Register object)
5269 : LDeferredCode(codegen), instr_(instr), object_(object) {
5270 SetExit(check_maps());
5272 virtual void Generate() OVERRIDE {
5273 codegen()->DoDeferredInstanceMigration(instr_, object_);
5275 Label* check_maps() { return &check_maps_; }
5276 virtual LInstruction* instr() OVERRIDE { return instr_; }
5283 if (instr->hydrogen()->IsStabilityCheck()) {
5284 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5285 for (int i = 0; i < maps->size(); ++i) {
5286 AddStabilityDependency(maps->at(i).handle());
5291 LOperand* input = instr->value();
5292 DCHECK(input->IsRegister());
5293 Register reg = ToRegister(input);
5295 DeferredCheckMaps* deferred = NULL;
5296 if (instr->hydrogen()->HasMigrationTarget()) {
5297 deferred = new(zone()) DeferredCheckMaps(this, instr, reg);
5298 __ bind(deferred->check_maps());
5301 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5303 for (int i = 0; i < maps->size() - 1; i++) {
5304 Handle<Map> map = maps->at(i).handle();
5305 __ CompareMap(reg, map);
5306 __ j(equal, &success, Label::kNear);
5309 Handle<Map> map = maps->at(maps->size() - 1).handle();
5310 __ CompareMap(reg, map);
5311 if (instr->hydrogen()->HasMigrationTarget()) {
5312 __ j(not_equal, deferred->entry());
5314 DeoptimizeIf(not_equal, instr, "wrong map");
5321 void LCodeGen::DoClampDToUint8(LClampDToUint8* instr) {
5322 XMMRegister value_reg = ToDoubleRegister(instr->unclamped());
5323 XMMRegister xmm_scratch = double_scratch0();
5324 Register result_reg = ToRegister(instr->result());
5325 __ ClampDoubleToUint8(value_reg, xmm_scratch, result_reg);
5329 void LCodeGen::DoClampIToUint8(LClampIToUint8* instr) {
5330 DCHECK(instr->unclamped()->Equals(instr->result()));
5331 Register value_reg = ToRegister(instr->result());
5332 __ ClampUint8(value_reg);
5336 void LCodeGen::DoClampTToUint8(LClampTToUint8* instr) {
5337 DCHECK(instr->unclamped()->Equals(instr->result()));
5338 Register input_reg = ToRegister(instr->unclamped());
5339 XMMRegister temp_xmm_reg = ToDoubleRegister(instr->temp_xmm());
5340 XMMRegister xmm_scratch = double_scratch0();
5341 Label is_smi, done, heap_number;
5343 __ JumpIfSmi(input_reg, &is_smi);
5345 // Check for heap number
5346 __ cmp(FieldOperand(input_reg, HeapObject::kMapOffset),
5347 factory()->heap_number_map());
5348 __ j(equal, &heap_number, Label::kNear);
5350 // Check for undefined. Undefined is converted to zero for clamping
5352 __ cmp(input_reg, factory()->undefined_value());
5353 DeoptimizeIf(not_equal, instr, "not a heap number/undefined");
5354 __ mov(input_reg, 0);
5355 __ jmp(&done, Label::kNear);
5358 __ bind(&heap_number);
5359 __ movsd(xmm_scratch, FieldOperand(input_reg, HeapNumber::kValueOffset));
5360 __ ClampDoubleToUint8(xmm_scratch, temp_xmm_reg, input_reg);
5361 __ jmp(&done, Label::kNear);
5365 __ SmiUntag(input_reg);
5366 __ ClampUint8(input_reg);
5371 void LCodeGen::DoDoubleBits(LDoubleBits* instr) {
5372 XMMRegister value_reg = ToDoubleRegister(instr->value());
5373 Register result_reg = ToRegister(instr->result());
5374 if (instr->hydrogen()->bits() == HDoubleBits::HIGH) {
5375 if (CpuFeatures::IsSupported(SSE4_1)) {
5376 CpuFeatureScope scope2(masm(), SSE4_1);
5377 __ pextrd(result_reg, value_reg, 1);
5379 XMMRegister xmm_scratch = double_scratch0();
5380 __ pshufd(xmm_scratch, value_reg, 1);
5381 __ movd(result_reg, xmm_scratch);
5384 __ movd(result_reg, value_reg);
5389 void LCodeGen::DoConstructDouble(LConstructDouble* instr) {
5390 Register hi_reg = ToRegister(instr->hi());
5391 Register lo_reg = ToRegister(instr->lo());
5392 XMMRegister result_reg = ToDoubleRegister(instr->result());
5394 if (CpuFeatures::IsSupported(SSE4_1)) {
5395 CpuFeatureScope scope2(masm(), SSE4_1);
5396 __ movd(result_reg, lo_reg);
5397 __ pinsrd(result_reg, hi_reg, 1);
5399 XMMRegister xmm_scratch = double_scratch0();
5400 __ movd(result_reg, hi_reg);
5401 __ psllq(result_reg, 32);
5402 __ movd(xmm_scratch, lo_reg);
5403 __ orps(result_reg, xmm_scratch);
5408 void LCodeGen::DoAllocate(LAllocate* instr) {
5409 class DeferredAllocate FINAL : public LDeferredCode {
5411 DeferredAllocate(LCodeGen* codegen, LAllocate* instr)
5412 : LDeferredCode(codegen), instr_(instr) { }
5413 virtual void Generate() OVERRIDE {
5414 codegen()->DoDeferredAllocate(instr_);
5416 virtual LInstruction* instr() OVERRIDE { return instr_; }
5421 DeferredAllocate* deferred = new(zone()) DeferredAllocate(this, instr);
5423 Register result = ToRegister(instr->result());
5424 Register temp = ToRegister(instr->temp());
5426 // Allocate memory for the object.
5427 AllocationFlags flags = TAG_OBJECT;
5428 if (instr->hydrogen()->MustAllocateDoubleAligned()) {
5429 flags = static_cast<AllocationFlags>(flags | DOUBLE_ALIGNMENT);
5431 if (instr->hydrogen()->IsOldPointerSpaceAllocation()) {
5432 DCHECK(!instr->hydrogen()->IsOldDataSpaceAllocation());
5433 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5434 flags = static_cast<AllocationFlags>(flags | PRETENURE_OLD_POINTER_SPACE);
5435 } else if (instr->hydrogen()->IsOldDataSpaceAllocation()) {
5436 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5437 flags = static_cast<AllocationFlags>(flags | PRETENURE_OLD_DATA_SPACE);
5440 if (instr->size()->IsConstantOperand()) {
5441 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5442 if (size <= Page::kMaxRegularHeapObjectSize) {
5443 __ Allocate(size, result, temp, no_reg, deferred->entry(), flags);
5445 __ jmp(deferred->entry());
5448 Register size = ToRegister(instr->size());
5449 __ Allocate(size, result, temp, no_reg, deferred->entry(), flags);
5452 __ bind(deferred->exit());
5454 if (instr->hydrogen()->MustPrefillWithFiller()) {
5455 if (instr->size()->IsConstantOperand()) {
5456 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5457 __ mov(temp, (size / kPointerSize) - 1);
5459 temp = ToRegister(instr->size());
5460 __ shr(temp, kPointerSizeLog2);
5465 __ mov(FieldOperand(result, temp, times_pointer_size, 0),
5466 isolate()->factory()->one_pointer_filler_map());
5468 __ j(not_zero, &loop);
5473 void LCodeGen::DoDeferredAllocate(LAllocate* instr) {
5474 Register result = ToRegister(instr->result());
5476 // TODO(3095996): Get rid of this. For now, we need to make the
5477 // result register contain a valid pointer because it is already
5478 // contained in the register pointer map.
5479 __ Move(result, Immediate(Smi::FromInt(0)));
5481 PushSafepointRegistersScope scope(this);
5482 if (instr->size()->IsRegister()) {
5483 Register size = ToRegister(instr->size());
5484 DCHECK(!size.is(result));
5485 __ SmiTag(ToRegister(instr->size()));
5488 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5489 if (size >= 0 && size <= Smi::kMaxValue) {
5490 __ push(Immediate(Smi::FromInt(size)));
5492 // We should never get here at runtime => abort
5498 int flags = AllocateDoubleAlignFlag::encode(
5499 instr->hydrogen()->MustAllocateDoubleAligned());
5500 if (instr->hydrogen()->IsOldPointerSpaceAllocation()) {
5501 DCHECK(!instr->hydrogen()->IsOldDataSpaceAllocation());
5502 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5503 flags = AllocateTargetSpace::update(flags, OLD_POINTER_SPACE);
5504 } else if (instr->hydrogen()->IsOldDataSpaceAllocation()) {
5505 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5506 flags = AllocateTargetSpace::update(flags, OLD_DATA_SPACE);
5508 flags = AllocateTargetSpace::update(flags, NEW_SPACE);
5510 __ push(Immediate(Smi::FromInt(flags)));
5512 CallRuntimeFromDeferred(
5513 Runtime::kAllocateInTargetSpace, 2, instr, instr->context());
5514 __ StoreToSafepointRegisterSlot(result, eax);
5518 void LCodeGen::DoToFastProperties(LToFastProperties* instr) {
5519 DCHECK(ToRegister(instr->value()).is(eax));
5521 CallRuntime(Runtime::kToFastProperties, 1, instr);
5525 void LCodeGen::DoRegExpLiteral(LRegExpLiteral* instr) {
5526 DCHECK(ToRegister(instr->context()).is(esi));
5528 // Registers will be used as follows:
5529 // ecx = literals array.
5530 // ebx = regexp literal.
5531 // eax = regexp literal clone.
5533 int literal_offset =
5534 FixedArray::OffsetOfElementAt(instr->hydrogen()->literal_index());
5535 __ LoadHeapObject(ecx, instr->hydrogen()->literals());
5536 __ mov(ebx, FieldOperand(ecx, literal_offset));
5537 __ cmp(ebx, factory()->undefined_value());
5538 __ j(not_equal, &materialized, Label::kNear);
5540 // Create regexp literal using runtime function
5541 // Result will be in eax.
5543 __ push(Immediate(Smi::FromInt(instr->hydrogen()->literal_index())));
5544 __ push(Immediate(instr->hydrogen()->pattern()));
5545 __ push(Immediate(instr->hydrogen()->flags()));
5546 CallRuntime(Runtime::kMaterializeRegExpLiteral, 4, instr);
5549 __ bind(&materialized);
5550 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
5551 Label allocated, runtime_allocate;
5552 __ Allocate(size, eax, ecx, edx, &runtime_allocate, TAG_OBJECT);
5553 __ jmp(&allocated, Label::kNear);
5555 __ bind(&runtime_allocate);
5557 __ push(Immediate(Smi::FromInt(size)));
5558 CallRuntime(Runtime::kAllocateInNewSpace, 1, instr);
5561 __ bind(&allocated);
5562 // Copy the content into the newly allocated memory.
5563 // (Unroll copy loop once for better throughput).
5564 for (int i = 0; i < size - kPointerSize; i += 2 * kPointerSize) {
5565 __ mov(edx, FieldOperand(ebx, i));
5566 __ mov(ecx, FieldOperand(ebx, i + kPointerSize));
5567 __ mov(FieldOperand(eax, i), edx);
5568 __ mov(FieldOperand(eax, i + kPointerSize), ecx);
5570 if ((size % (2 * kPointerSize)) != 0) {
5571 __ mov(edx, FieldOperand(ebx, size - kPointerSize));
5572 __ mov(FieldOperand(eax, size - kPointerSize), edx);
5577 void LCodeGen::DoFunctionLiteral(LFunctionLiteral* instr) {
5578 DCHECK(ToRegister(instr->context()).is(esi));
5579 // Use the fast case closure allocation code that allocates in new
5580 // space for nested functions that don't need literals cloning.
5581 bool pretenure = instr->hydrogen()->pretenure();
5582 if (!pretenure && instr->hydrogen()->has_no_literals()) {
5583 FastNewClosureStub stub(isolate(), instr->hydrogen()->strict_mode(),
5584 instr->hydrogen()->kind());
5585 __ mov(ebx, Immediate(instr->hydrogen()->shared_info()));
5586 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
5589 __ push(Immediate(instr->hydrogen()->shared_info()));
5590 __ push(Immediate(pretenure ? factory()->true_value()
5591 : factory()->false_value()));
5592 CallRuntime(Runtime::kNewClosure, 3, instr);
5597 void LCodeGen::DoTypeof(LTypeof* instr) {
5598 DCHECK(ToRegister(instr->context()).is(esi));
5599 LOperand* input = instr->value();
5600 EmitPushTaggedOperand(input);
5601 CallRuntime(Runtime::kTypeof, 1, instr);
5605 void LCodeGen::DoTypeofIsAndBranch(LTypeofIsAndBranch* instr) {
5606 Register input = ToRegister(instr->value());
5607 Condition final_branch_condition = EmitTypeofIs(instr, input);
5608 if (final_branch_condition != no_condition) {
5609 EmitBranch(instr, final_branch_condition);
5614 Condition LCodeGen::EmitTypeofIs(LTypeofIsAndBranch* instr, Register input) {
5615 Label* true_label = instr->TrueLabel(chunk_);
5616 Label* false_label = instr->FalseLabel(chunk_);
5617 Handle<String> type_name = instr->type_literal();
5618 int left_block = instr->TrueDestination(chunk_);
5619 int right_block = instr->FalseDestination(chunk_);
5620 int next_block = GetNextEmittedBlock();
5622 Label::Distance true_distance = left_block == next_block ? Label::kNear
5624 Label::Distance false_distance = right_block == next_block ? Label::kNear
5626 Condition final_branch_condition = no_condition;
5627 if (String::Equals(type_name, factory()->number_string())) {
5628 __ JumpIfSmi(input, true_label, true_distance);
5629 __ cmp(FieldOperand(input, HeapObject::kMapOffset),
5630 factory()->heap_number_map());
5631 final_branch_condition = equal;
5633 } else if (String::Equals(type_name, factory()->string_string())) {
5634 __ JumpIfSmi(input, false_label, false_distance);
5635 __ CmpObjectType(input, FIRST_NONSTRING_TYPE, input);
5636 __ j(above_equal, false_label, false_distance);
5637 __ test_b(FieldOperand(input, Map::kBitFieldOffset),
5638 1 << Map::kIsUndetectable);
5639 final_branch_condition = zero;
5641 } else if (String::Equals(type_name, factory()->symbol_string())) {
5642 __ JumpIfSmi(input, false_label, false_distance);
5643 __ CmpObjectType(input, SYMBOL_TYPE, input);
5644 final_branch_condition = equal;
5646 } else if (String::Equals(type_name, factory()->boolean_string())) {
5647 __ cmp(input, factory()->true_value());
5648 __ j(equal, true_label, true_distance);
5649 __ cmp(input, factory()->false_value());
5650 final_branch_condition = equal;
5652 } else if (String::Equals(type_name, factory()->undefined_string())) {
5653 __ cmp(input, factory()->undefined_value());
5654 __ j(equal, true_label, true_distance);
5655 __ JumpIfSmi(input, false_label, false_distance);
5656 // Check for undetectable objects => true.
5657 __ mov(input, FieldOperand(input, HeapObject::kMapOffset));
5658 __ test_b(FieldOperand(input, Map::kBitFieldOffset),
5659 1 << Map::kIsUndetectable);
5660 final_branch_condition = not_zero;
5662 } else if (String::Equals(type_name, factory()->function_string())) {
5663 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
5664 __ JumpIfSmi(input, false_label, false_distance);
5665 __ CmpObjectType(input, JS_FUNCTION_TYPE, input);
5666 __ j(equal, true_label, true_distance);
5667 __ CmpInstanceType(input, JS_FUNCTION_PROXY_TYPE);
5668 final_branch_condition = equal;
5670 } else if (String::Equals(type_name, factory()->object_string())) {
5671 __ JumpIfSmi(input, false_label, false_distance);
5672 __ cmp(input, factory()->null_value());
5673 __ j(equal, true_label, true_distance);
5674 __ CmpObjectType(input, FIRST_NONCALLABLE_SPEC_OBJECT_TYPE, input);
5675 __ j(below, false_label, false_distance);
5676 __ CmpInstanceType(input, LAST_NONCALLABLE_SPEC_OBJECT_TYPE);
5677 __ j(above, false_label, false_distance);
5678 // Check for undetectable objects => false.
5679 __ test_b(FieldOperand(input, Map::kBitFieldOffset),
5680 1 << Map::kIsUndetectable);
5681 final_branch_condition = zero;
5684 __ jmp(false_label, false_distance);
5686 return final_branch_condition;
5690 void LCodeGen::DoIsConstructCallAndBranch(LIsConstructCallAndBranch* instr) {
5691 Register temp = ToRegister(instr->temp());
5693 EmitIsConstructCall(temp);
5694 EmitBranch(instr, equal);
5698 void LCodeGen::EmitIsConstructCall(Register temp) {
5699 // Get the frame pointer for the calling frame.
5700 __ mov(temp, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
5702 // Skip the arguments adaptor frame if it exists.
5703 Label check_frame_marker;
5704 __ cmp(Operand(temp, StandardFrameConstants::kContextOffset),
5705 Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
5706 __ j(not_equal, &check_frame_marker, Label::kNear);
5707 __ mov(temp, Operand(temp, StandardFrameConstants::kCallerFPOffset));
5709 // Check the marker in the calling frame.
5710 __ bind(&check_frame_marker);
5711 __ cmp(Operand(temp, StandardFrameConstants::kMarkerOffset),
5712 Immediate(Smi::FromInt(StackFrame::CONSTRUCT)));
5716 void LCodeGen::EnsureSpaceForLazyDeopt(int space_needed) {
5717 if (!info()->IsStub()) {
5718 // Ensure that we have enough space after the previous lazy-bailout
5719 // instruction for patching the code here.
5720 int current_pc = masm()->pc_offset();
5721 if (current_pc < last_lazy_deopt_pc_ + space_needed) {
5722 int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
5723 __ Nop(padding_size);
5726 last_lazy_deopt_pc_ = masm()->pc_offset();
5730 void LCodeGen::DoLazyBailout(LLazyBailout* instr) {
5731 last_lazy_deopt_pc_ = masm()->pc_offset();
5732 DCHECK(instr->HasEnvironment());
5733 LEnvironment* env = instr->environment();
5734 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5735 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5739 void LCodeGen::DoDeoptimize(LDeoptimize* instr) {
5740 Deoptimizer::BailoutType type = instr->hydrogen()->type();
5741 // TODO(danno): Stubs expect all deopts to be lazy for historical reasons (the
5742 // needed return address), even though the implementation of LAZY and EAGER is
5743 // now identical. When LAZY is eventually completely folded into EAGER, remove
5744 // the special case below.
5745 if (info()->IsStub() && type == Deoptimizer::EAGER) {
5746 type = Deoptimizer::LAZY;
5748 DeoptimizeIf(no_condition, instr, instr->hydrogen()->reason(), type);
5752 void LCodeGen::DoDummy(LDummy* instr) {
5753 // Nothing to see here, move on!
5757 void LCodeGen::DoDummyUse(LDummyUse* instr) {
5758 // Nothing to see here, move on!
5762 void LCodeGen::DoDeferredStackCheck(LStackCheck* instr) {
5763 PushSafepointRegistersScope scope(this);
5764 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
5765 __ CallRuntimeSaveDoubles(Runtime::kStackGuard);
5766 RecordSafepointWithLazyDeopt(
5767 instr, RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
5768 DCHECK(instr->HasEnvironment());
5769 LEnvironment* env = instr->environment();
5770 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5774 void LCodeGen::DoStackCheck(LStackCheck* instr) {
5775 class DeferredStackCheck FINAL : public LDeferredCode {
5777 DeferredStackCheck(LCodeGen* codegen, LStackCheck* instr)
5778 : LDeferredCode(codegen), instr_(instr) { }
5779 virtual void Generate() OVERRIDE {
5780 codegen()->DoDeferredStackCheck(instr_);
5782 virtual LInstruction* instr() OVERRIDE { return instr_; }
5784 LStackCheck* instr_;
5787 DCHECK(instr->HasEnvironment());
5788 LEnvironment* env = instr->environment();
5789 // There is no LLazyBailout instruction for stack-checks. We have to
5790 // prepare for lazy deoptimization explicitly here.
5791 if (instr->hydrogen()->is_function_entry()) {
5792 // Perform stack overflow check.
5794 ExternalReference stack_limit =
5795 ExternalReference::address_of_stack_limit(isolate());
5796 __ cmp(esp, Operand::StaticVariable(stack_limit));
5797 __ j(above_equal, &done, Label::kNear);
5799 DCHECK(instr->context()->IsRegister());
5800 DCHECK(ToRegister(instr->context()).is(esi));
5801 CallCode(isolate()->builtins()->StackCheck(),
5802 RelocInfo::CODE_TARGET,
5806 DCHECK(instr->hydrogen()->is_backwards_branch());
5807 // Perform stack overflow check if this goto needs it before jumping.
5808 DeferredStackCheck* deferred_stack_check =
5809 new(zone()) DeferredStackCheck(this, instr);
5810 ExternalReference stack_limit =
5811 ExternalReference::address_of_stack_limit(isolate());
5812 __ cmp(esp, Operand::StaticVariable(stack_limit));
5813 __ j(below, deferred_stack_check->entry());
5814 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
5815 __ bind(instr->done_label());
5816 deferred_stack_check->SetExit(instr->done_label());
5817 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5818 // Don't record a deoptimization index for the safepoint here.
5819 // This will be done explicitly when emitting call and the safepoint in
5820 // the deferred code.
5825 void LCodeGen::DoOsrEntry(LOsrEntry* instr) {
5826 // This is a pseudo-instruction that ensures that the environment here is
5827 // properly registered for deoptimization and records the assembler's PC
5829 LEnvironment* environment = instr->environment();
5831 // If the environment were already registered, we would have no way of
5832 // backpatching it with the spill slot operands.
5833 DCHECK(!environment->HasBeenRegistered());
5834 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
5836 GenerateOsrPrologue();
5840 void LCodeGen::DoForInPrepareMap(LForInPrepareMap* instr) {
5841 DCHECK(ToRegister(instr->context()).is(esi));
5842 __ cmp(eax, isolate()->factory()->undefined_value());
5843 DeoptimizeIf(equal, instr, "undefined");
5845 __ cmp(eax, isolate()->factory()->null_value());
5846 DeoptimizeIf(equal, instr, "null");
5848 __ test(eax, Immediate(kSmiTagMask));
5849 DeoptimizeIf(zero, instr, "Smi");
5851 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
5852 __ CmpObjectType(eax, LAST_JS_PROXY_TYPE, ecx);
5853 DeoptimizeIf(below_equal, instr, "wrong instance type");
5855 Label use_cache, call_runtime;
5856 __ CheckEnumCache(&call_runtime);
5858 __ mov(eax, FieldOperand(eax, HeapObject::kMapOffset));
5859 __ jmp(&use_cache, Label::kNear);
5861 // Get the set of properties to enumerate.
5862 __ bind(&call_runtime);
5864 CallRuntime(Runtime::kGetPropertyNamesFast, 1, instr);
5866 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
5867 isolate()->factory()->meta_map());
5868 DeoptimizeIf(not_equal, instr, "wrong map");
5869 __ bind(&use_cache);
5873 void LCodeGen::DoForInCacheArray(LForInCacheArray* instr) {
5874 Register map = ToRegister(instr->map());
5875 Register result = ToRegister(instr->result());
5876 Label load_cache, done;
5877 __ EnumLength(result, map);
5878 __ cmp(result, Immediate(Smi::FromInt(0)));
5879 __ j(not_equal, &load_cache, Label::kNear);
5880 __ mov(result, isolate()->factory()->empty_fixed_array());
5881 __ jmp(&done, Label::kNear);
5883 __ bind(&load_cache);
5884 __ LoadInstanceDescriptors(map, result);
5886 FieldOperand(result, DescriptorArray::kEnumCacheOffset));
5888 FieldOperand(result, FixedArray::SizeFor(instr->idx())));
5890 __ test(result, result);
5891 DeoptimizeIf(equal, instr, "no cache");
5895 void LCodeGen::DoCheckMapValue(LCheckMapValue* instr) {
5896 Register object = ToRegister(instr->value());
5897 __ cmp(ToRegister(instr->map()),
5898 FieldOperand(object, HeapObject::kMapOffset));
5899 DeoptimizeIf(not_equal, instr, "wrong map");
5903 void LCodeGen::DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
5906 PushSafepointRegistersScope scope(this);
5910 __ CallRuntimeSaveDoubles(Runtime::kLoadMutableDouble);
5911 RecordSafepointWithRegisters(
5912 instr->pointer_map(), 2, Safepoint::kNoLazyDeopt);
5913 __ StoreToSafepointRegisterSlot(object, eax);
5917 void LCodeGen::DoLoadFieldByIndex(LLoadFieldByIndex* instr) {
5918 class DeferredLoadMutableDouble FINAL : public LDeferredCode {
5920 DeferredLoadMutableDouble(LCodeGen* codegen,
5921 LLoadFieldByIndex* instr,
5924 : LDeferredCode(codegen),
5929 virtual void Generate() OVERRIDE {
5930 codegen()->DoDeferredLoadMutableDouble(instr_, object_, index_);
5932 virtual LInstruction* instr() OVERRIDE { return instr_; }
5934 LLoadFieldByIndex* instr_;
5939 Register object = ToRegister(instr->object());
5940 Register index = ToRegister(instr->index());
5942 DeferredLoadMutableDouble* deferred;
5943 deferred = new(zone()) DeferredLoadMutableDouble(
5944 this, instr, object, index);
5946 Label out_of_object, done;
5947 __ test(index, Immediate(Smi::FromInt(1)));
5948 __ j(not_zero, deferred->entry());
5952 __ cmp(index, Immediate(0));
5953 __ j(less, &out_of_object, Label::kNear);
5954 __ mov(object, FieldOperand(object,
5956 times_half_pointer_size,
5957 JSObject::kHeaderSize));
5958 __ jmp(&done, Label::kNear);
5960 __ bind(&out_of_object);
5961 __ mov(object, FieldOperand(object, JSObject::kPropertiesOffset));
5963 // Index is now equal to out of object property index plus 1.
5964 __ mov(object, FieldOperand(object,
5966 times_half_pointer_size,
5967 FixedArray::kHeaderSize - kPointerSize));
5968 __ bind(deferred->exit());
5973 void LCodeGen::DoStoreFrameContext(LStoreFrameContext* instr) {
5974 Register context = ToRegister(instr->context());
5975 __ mov(Operand(ebp, StandardFrameConstants::kContextOffset), context);
5979 void LCodeGen::DoAllocateBlockContext(LAllocateBlockContext* instr) {
5980 Handle<ScopeInfo> scope_info = instr->scope_info();
5981 __ Push(scope_info);
5982 __ push(ToRegister(instr->function()));
5983 CallRuntime(Runtime::kPushBlockContext, 2, instr);
5984 RecordSafepoint(Safepoint::kNoLazyDeopt);
5989 void LCodeGen::HandleSIMD128ToTagged(LSIMD128ToTagged* instr) {
5990 class DeferredSIMD128ToTagged FINAL : public LDeferredCode {
5992 DeferredSIMD128ToTagged(LCodeGen* codegen,
5993 LInstruction* instr,
5994 Runtime::FunctionId id)
5995 : LDeferredCode(codegen), instr_(instr), id_(id) { }
5996 virtual void Generate() OVERRIDE {
5997 codegen()->DoDeferredSIMD128ToTagged(instr_, id_);
5999 virtual LInstruction* instr() OVERRIDE { return instr_; }
6001 LInstruction* instr_;
6002 Runtime::FunctionId id_;
6005 XMMRegister input_reg = ToSIMD128Register(instr->value());
6006 Register reg = ToRegister(instr->result());
6007 Register tmp = ToRegister(instr->temp());
6008 Register tmp2 = ToRegister(instr->temp2());
6010 DeferredSIMD128ToTagged* deferred = new(zone()) DeferredSIMD128ToTagged(
6011 this, instr, static_cast<Runtime::FunctionId>(T::kRuntimeAllocatorId()));
6013 if (FLAG_inline_new) {
6014 if (T::kInstanceType == FLOAT32x4_TYPE) {
6015 __ AllocateFloat32x4(reg, tmp, tmp2, deferred->entry());
6016 } else if (T::kInstanceType == INT32x4_TYPE) {
6017 __ AllocateInt32x4(reg, tmp, tmp2, deferred->entry());
6018 } else if (T::kInstanceType == FLOAT64x2_TYPE) {
6019 __ AllocateFloat64x2(reg, tmp, tmp2, deferred->entry());
6022 __ jmp(deferred->entry());
6024 __ bind(deferred->exit());
6026 // Load the inner FixedTypedArray object.
6027 __ mov(tmp, FieldOperand(reg, T::kValueOffset));
6029 __ movups(FieldOperand(tmp, FixedTypedArrayBase::kDataOffset), input_reg);
6033 void LCodeGen::DoSIMD128ToTagged(LSIMD128ToTagged* instr) {
6034 if (instr->value()->IsFloat32x4Register()) {
6035 HandleSIMD128ToTagged<Float32x4>(instr);
6036 } else if (instr->value()->IsFloat64x2Register()) {
6037 HandleSIMD128ToTagged<Float64x2>(instr);
6039 DCHECK(instr->value()->IsInt32x4Register());
6040 HandleSIMD128ToTagged<Int32x4>(instr);
6046 void LCodeGen::HandleTaggedToSIMD128(LTaggedToSIMD128* instr) {
6047 LOperand* input = instr->value();
6048 DCHECK(input->IsRegister());
6049 LOperand* result = instr->result();
6050 DCHECK(result->IsSIMD128Register());
6052 Register input_reg = ToRegister(input);
6053 Register temp_reg = ToRegister(instr->temp());
6054 XMMRegister result_reg = ToSIMD128Register(result);
6056 __ test(input_reg, Immediate(kSmiTagMask));
6057 DeoptimizeIf(zero, instr, "value is smi");
6058 __ CmpObjectType(input_reg, T::kInstanceType, temp_reg);
6059 DeoptimizeIf(not_equal, instr, "value is not simd128");
6061 // Load the inner FixedTypedArray object.
6062 __ mov(temp_reg, FieldOperand(input_reg, T::kValueOffset));
6065 result_reg, FieldOperand(temp_reg, FixedTypedArrayBase::kDataOffset));
6069 void LCodeGen::DoTaggedToSIMD128(LTaggedToSIMD128* instr) {
6070 if (instr->representation().IsFloat32x4()) {
6071 HandleTaggedToSIMD128<Float32x4>(instr);
6072 } else if (instr->representation().IsFloat64x2()) {
6073 HandleTaggedToSIMD128<Float64x2>(instr);
6075 DCHECK(instr->representation().IsInt32x4());
6076 HandleTaggedToSIMD128<Int32x4>(instr);
6081 void LCodeGen::DoNullarySIMDOperation(LNullarySIMDOperation* instr) {
6082 switch (instr->op()) {
6083 case kFloat32x4Zero: {
6084 XMMRegister result_reg = ToFloat32x4Register(instr->result());
6085 __ xorps(result_reg, result_reg);
6088 case kFloat64x2Zero: {
6089 XMMRegister result_reg = ToFloat64x2Register(instr->result());
6090 __ xorpd(result_reg, result_reg);
6093 case kInt32x4Zero: {
6094 XMMRegister result_reg = ToInt32x4Register(instr->result());
6095 __ xorps(result_reg, result_reg);
6105 void LCodeGen::DoUnarySIMDOperation(LUnarySIMDOperation* instr) {
6107 switch (instr->op()) {
6108 case kFloat32x4Coercion: {
6109 XMMRegister input_reg = ToFloat32x4Register(instr->value());
6110 XMMRegister result_reg = ToFloat32x4Register(instr->result());
6111 if (!result_reg.is(input_reg)) {
6112 __ movaps(result_reg, input_reg);
6116 case kFloat64x2Coercion: {
6117 XMMRegister input_reg = ToFloat64x2Register(instr->value());
6118 XMMRegister result_reg = ToFloat64x2Register(instr->result());
6119 if (!result_reg.is(input_reg)) {
6120 __ movaps(result_reg, input_reg);
6124 case kInt32x4Coercion: {
6125 XMMRegister input_reg = ToInt32x4Register(instr->value());
6126 XMMRegister result_reg = ToInt32x4Register(instr->result());
6127 if (!result_reg.is(input_reg)) {
6128 __ movaps(result_reg, input_reg);
6132 case kSIMD128Change: {
6133 Comment(";;; deoptimize: can not perform representation change"
6134 "for float32x4 or int32x4");
6135 DeoptimizeIf(no_condition, instr, "cannot perform representation change"
6136 "for float32x4 or int32x4");
6141 case kFloat32x4Reciprocal:
6142 case kFloat32x4ReciprocalSqrt:
6143 case kFloat32x4Sqrt: {
6144 DCHECK(instr->value()->Equals(instr->result()));
6145 DCHECK(instr->hydrogen()->value()->representation().IsFloat32x4());
6146 XMMRegister input_reg = ToFloat32x4Register(instr->value());
6147 switch (instr->op()) {
6149 __ absps(input_reg);
6152 __ negateps(input_reg);
6154 case kFloat32x4Reciprocal:
6155 __ rcpps(input_reg, input_reg);
6157 case kFloat32x4ReciprocalSqrt:
6158 __ rsqrtps(input_reg, input_reg);
6160 case kFloat32x4Sqrt:
6161 __ sqrtps(input_reg, input_reg);
6171 case kFloat64x2Sqrt: {
6172 DCHECK(instr->value()->Equals(instr->result()));
6173 DCHECK(instr->hydrogen()->value()->representation().IsFloat64x2());
6174 XMMRegister input_reg = ToFloat64x2Register(instr->value());
6175 switch (instr->op()) {
6177 __ abspd(input_reg);
6180 __ negatepd(input_reg);
6182 case kFloat64x2Sqrt:
6183 __ sqrtpd(input_reg, input_reg);
6193 DCHECK(instr->hydrogen()->value()->representation().IsInt32x4());
6194 XMMRegister input_reg = ToInt32x4Register(instr->value());
6195 switch (instr->op()) {
6197 __ notps(input_reg);
6200 __ pnegd(input_reg);
6208 case kFloat32x4BitsToInt32x4:
6209 case kFloat32x4ToInt32x4: {
6210 DCHECK(instr->hydrogen()->value()->representation().IsFloat32x4());
6211 XMMRegister input_reg = ToFloat32x4Register(instr->value());
6212 XMMRegister result_reg = ToInt32x4Register(instr->result());
6213 if (instr->op() == kFloat32x4BitsToInt32x4) {
6214 if (!result_reg.is(input_reg)) {
6215 __ movaps(result_reg, input_reg);
6218 DCHECK(instr->op() == kFloat32x4ToInt32x4);
6219 __ cvtps2dq(result_reg, input_reg);
6223 case kInt32x4BitsToFloat32x4:
6224 case kInt32x4ToFloat32x4: {
6225 DCHECK(instr->hydrogen()->value()->representation().IsInt32x4());
6226 XMMRegister input_reg = ToInt32x4Register(instr->value());
6227 XMMRegister result_reg = ToFloat32x4Register(instr->result());
6228 if (instr->op() == kInt32x4BitsToFloat32x4) {
6229 if (!result_reg.is(input_reg)) {
6230 __ movaps(result_reg, input_reg);
6233 DCHECK(instr->op() == kInt32x4ToFloat32x4);
6234 __ cvtdq2ps(result_reg, input_reg);
6238 case kFloat32x4Splat: {
6239 DCHECK(instr->hydrogen()->value()->representation().IsDouble());
6240 XMMRegister input_reg = ToDoubleRegister(instr->value());
6241 XMMRegister result_reg = ToFloat32x4Register(instr->result());
6242 XMMRegister xmm_scratch = xmm0;
6243 __ xorps(xmm_scratch, xmm_scratch);
6244 __ cvtsd2ss(xmm_scratch, input_reg);
6245 __ shufps(xmm_scratch, xmm_scratch, 0x0);
6246 __ movaps(result_reg, xmm_scratch);
6249 case kInt32x4Splat: {
6250 DCHECK(instr->hydrogen()->value()->representation().IsInteger32());
6251 Register input_reg = ToRegister(instr->value());
6252 XMMRegister result_reg = ToInt32x4Register(instr->result());
6253 __ movd(result_reg, input_reg);
6254 __ shufps(result_reg, result_reg, 0x0);
6257 case kInt32x4GetSignMask: {
6258 DCHECK(instr->hydrogen()->value()->representation().IsInt32x4());
6259 XMMRegister input_reg = ToInt32x4Register(instr->value());
6260 Register result = ToRegister(instr->result());
6261 __ movmskps(result, input_reg);
6264 case kFloat32x4GetSignMask: {
6265 DCHECK(instr->hydrogen()->value()->representation().IsFloat32x4());
6266 XMMRegister input_reg = ToFloat32x4Register(instr->value());
6267 Register result = ToRegister(instr->result());
6268 __ movmskps(result, input_reg);
6271 case kFloat32x4GetW:
6273 case kFloat32x4GetZ:
6275 case kFloat32x4GetY:
6277 case kFloat32x4GetX: {
6278 DCHECK(instr->hydrogen()->value()->representation().IsFloat32x4());
6279 XMMRegister input_reg = ToFloat32x4Register(instr->value());
6280 XMMRegister result = ToDoubleRegister(instr->result());
6281 XMMRegister xmm_scratch = result.is(input_reg) ? xmm0 : result;
6283 if (select == 0x0) {
6284 __ xorps(xmm_scratch, xmm_scratch);
6285 __ cvtss2sd(xmm_scratch, input_reg);
6286 if (!xmm_scratch.is(result)) {
6287 __ movaps(result, xmm_scratch);
6290 __ pshufd(xmm_scratch, input_reg, select);
6291 if (!xmm_scratch.is(result)) {
6292 __ xorps(result, result);
6294 __ cvtss2sd(result, xmm_scratch);
6298 case kFloat64x2GetSignMask: {
6299 DCHECK(instr->hydrogen()->value()->representation().IsFloat64x2());
6300 XMMRegister input_reg = ToFloat64x2Register(instr->value());
6301 Register result = ToRegister(instr->result());
6302 __ movmskpd(result, input_reg);
6305 case kFloat64x2GetX: {
6306 DCHECK(instr->hydrogen()->value()->representation().IsFloat64x2());
6307 XMMRegister input_reg = ToFloat64x2Register(instr->value());
6308 XMMRegister result = ToDoubleRegister(instr->result());
6310 if (!input_reg.is(result)) {
6311 __ movaps(result, input_reg);
6315 case kFloat64x2GetY: {
6316 DCHECK(instr->hydrogen()->value()->representation().IsFloat64x2());
6317 XMMRegister input_reg = ToFloat64x2Register(instr->value());
6318 XMMRegister result = ToDoubleRegister(instr->result());
6320 if (!input_reg.is(result)) {
6321 __ movaps(result, input_reg);
6323 __ shufpd(result, input_reg, 0x1);
6330 case kInt32x4GetFlagX:
6331 case kInt32x4GetFlagY:
6332 case kInt32x4GetFlagZ:
6333 case kInt32x4GetFlagW: {
6334 DCHECK(instr->hydrogen()->value()->representation().IsInt32x4());
6336 switch (instr->op()) {
6337 case kInt32x4GetFlagX:
6341 case kInt32x4GetFlagY:
6346 case kInt32x4GetFlagZ:
6351 case kInt32x4GetFlagW:
6360 XMMRegister input_reg = ToInt32x4Register(instr->value());
6361 Register result = ToRegister(instr->result());
6362 if (select == 0x0) {
6363 __ movd(result, input_reg);
6365 if (CpuFeatures::IsSupported(SSE4_1)) {
6366 CpuFeatureScope scope(masm(), SSE4_1);
6367 __ extractps(result, input_reg, select);
6369 XMMRegister xmm_scratch = xmm0;
6370 __ pshufd(xmm_scratch, input_reg, select);
6371 __ movd(result, xmm_scratch);
6376 Label false_value, done;
6377 __ test(result, result);
6378 __ j(zero, &false_value, Label::kNear);
6379 __ LoadRoot(result, Heap::kTrueValueRootIndex);
6380 __ jmp(&done, Label::kNear);
6381 __ bind(&false_value);
6382 __ LoadRoot(result, Heap::kFalseValueRootIndex);
6394 void LCodeGen::DoBinarySIMDOperation(LBinarySIMDOperation* instr) {
6395 uint8_t imm8 = 0; // for with operation
6396 switch (instr->op()) {
6402 case kFloat32x4Max: {
6403 DCHECK(instr->left()->Equals(instr->result()));
6404 DCHECK(instr->hydrogen()->left()->representation().IsFloat32x4());
6405 DCHECK(instr->hydrogen()->right()->representation().IsFloat32x4());
6406 XMMRegister left_reg = ToFloat32x4Register(instr->left());
6407 XMMRegister right_reg = ToFloat32x4Register(instr->right());
6408 switch (instr->op()) {
6410 __ addps(left_reg, right_reg);
6413 __ subps(left_reg, right_reg);
6416 __ mulps(left_reg, right_reg);
6419 __ divps(left_reg, right_reg);
6422 __ minps(left_reg, right_reg);
6425 __ maxps(left_reg, right_reg);
6433 case kFloat32x4Scale: {
6434 DCHECK(instr->left()->Equals(instr->result()));
6435 DCHECK(instr->hydrogen()->left()->representation().IsFloat32x4());
6436 DCHECK(instr->hydrogen()->right()->representation().IsDouble());
6437 XMMRegister left_reg = ToFloat32x4Register(instr->left());
6438 XMMRegister right_reg = ToDoubleRegister(instr->right());
6439 XMMRegister scratch_reg = xmm0;
6440 __ xorps(scratch_reg, scratch_reg);
6441 __ cvtsd2ss(scratch_reg, right_reg);
6442 __ shufps(scratch_reg, scratch_reg, 0x0);
6443 __ mulps(left_reg, scratch_reg);
6451 case kFloat64x2Max: {
6452 DCHECK(instr->left()->Equals(instr->result()));
6453 DCHECK(instr->hydrogen()->left()->representation().IsFloat64x2());
6454 DCHECK(instr->hydrogen()->right()->representation().IsFloat64x2());
6455 XMMRegister left_reg = ToFloat64x2Register(instr->left());
6456 XMMRegister right_reg = ToFloat64x2Register(instr->right());
6457 switch (instr->op()) {
6459 __ addpd(left_reg, right_reg);
6462 __ subpd(left_reg, right_reg);
6465 __ mulpd(left_reg, right_reg);
6468 __ divpd(left_reg, right_reg);
6471 __ minpd(left_reg, right_reg);
6474 __ maxpd(left_reg, right_reg);
6482 case kFloat64x2Scale: {
6483 DCHECK(instr->left()->Equals(instr->result()));
6484 DCHECK(instr->hydrogen()->left()->representation().IsFloat64x2());
6485 DCHECK(instr->hydrogen()->right()->representation().IsDouble());
6486 XMMRegister left_reg = ToFloat64x2Register(instr->left());
6487 XMMRegister right_reg = ToDoubleRegister(instr->right());
6488 __ shufpd(right_reg, right_reg, 0x0);
6489 __ mulpd(left_reg, right_reg);
6492 case kFloat32x4Shuffle: {
6493 DCHECK(instr->left()->Equals(instr->result()));
6494 DCHECK(instr->hydrogen()->left()->representation().IsFloat32x4());
6495 if (instr->hydrogen()->right()->IsConstant() &&
6496 HConstant::cast(instr->hydrogen()->right())->HasInteger32Value()) {
6497 int32_t value = ToInteger32(LConstantOperand::cast(instr->right()));
6498 uint8_t select = static_cast<uint8_t>(value & 0xFF);
6499 XMMRegister left_reg = ToFloat32x4Register(instr->left());
6500 __ shufps(left_reg, left_reg, select);
6503 Comment(";;; deoptimize: non-constant selector for shuffle");
6504 DeoptimizeIf(no_condition, instr, "non-constant selector for shuffle");
6508 case kInt32x4Shuffle: {
6509 DCHECK(instr->left()->Equals(instr->result()));
6510 DCHECK(instr->hydrogen()->left()->representation().IsInt32x4());
6511 if (instr->hydrogen()->right()->IsConstant() &&
6512 HConstant::cast(instr->hydrogen()->right())->HasInteger32Value()) {
6513 int32_t value = ToInteger32(LConstantOperand::cast(instr->right()));
6514 uint8_t select = static_cast<uint8_t>(value & 0xFF);
6515 XMMRegister left_reg = ToInt32x4Register(instr->left());
6516 __ pshufd(left_reg, left_reg, select);
6519 Comment(";;; deoptimize: non-constant selector for shuffle");
6520 DeoptimizeIf(no_condition, instr, "non-constant selector for shuffle");
6524 case kInt32x4ShiftLeft:
6525 case kInt32x4ShiftRight:
6526 case kInt32x4ShiftRightArithmetic: {
6527 DCHECK(instr->left()->Equals(instr->result()));
6528 DCHECK(instr->hydrogen()->left()->representation().IsInt32x4());
6529 if (instr->hydrogen()->right()->IsConstant() &&
6530 HConstant::cast(instr->hydrogen()->right())->HasInteger32Value()) {
6531 int32_t value = ToInteger32(LConstantOperand::cast(instr->right()));
6532 uint8_t shift = static_cast<uint8_t>(value & 0xFF);
6533 XMMRegister left_reg = ToInt32x4Register(instr->left());
6534 switch (instr->op()) {
6535 case kInt32x4ShiftLeft:
6536 __ pslld(left_reg, shift);
6538 case kInt32x4ShiftRight:
6539 __ psrld(left_reg, shift);
6541 case kInt32x4ShiftRightArithmetic:
6542 __ psrad(left_reg, shift);
6549 XMMRegister left_reg = ToInt32x4Register(instr->left());
6550 Register shift = ToRegister(instr->right());
6551 XMMRegister xmm_scratch = double_scratch0();
6552 __ movd(xmm_scratch, shift);
6553 switch (instr->op()) {
6554 case kInt32x4ShiftLeft:
6555 __ pslld(left_reg, xmm_scratch);
6557 case kInt32x4ShiftRight:
6558 __ psrld(left_reg, xmm_scratch);
6560 case kInt32x4ShiftRightArithmetic:
6561 __ psrad(left_reg, xmm_scratch);
6569 case kFloat32x4LessThan:
6570 case kFloat32x4LessThanOrEqual:
6571 case kFloat32x4Equal:
6572 case kFloat32x4NotEqual:
6573 case kFloat32x4GreaterThanOrEqual:
6574 case kFloat32x4GreaterThan: {
6575 DCHECK(instr->hydrogen()->left()->representation().IsFloat32x4());
6576 DCHECK(instr->hydrogen()->right()->representation().IsFloat32x4());
6577 XMMRegister left_reg = ToFloat32x4Register(instr->left());
6578 XMMRegister right_reg = ToFloat32x4Register(instr->right());
6579 XMMRegister result_reg = ToInt32x4Register(instr->result());
6580 switch (instr->op()) {
6581 case kFloat32x4LessThan:
6582 if (result_reg.is(left_reg)) {
6583 __ cmpltps(result_reg, right_reg);
6584 } else if (result_reg.is(right_reg)) {
6585 __ cmpnltps(result_reg, left_reg);
6587 __ movaps(result_reg, left_reg);
6588 __ cmpltps(result_reg, right_reg);
6591 case kFloat32x4LessThanOrEqual:
6592 if (result_reg.is(left_reg)) {
6593 __ cmpleps(result_reg, right_reg);
6594 } else if (result_reg.is(right_reg)) {
6595 __ cmpnleps(result_reg, left_reg);
6597 __ movaps(result_reg, left_reg);
6598 __ cmpleps(result_reg, right_reg);
6601 case kFloat32x4Equal:
6602 if (result_reg.is(left_reg)) {
6603 __ cmpeqps(result_reg, right_reg);
6604 } else if (result_reg.is(right_reg)) {
6605 __ cmpeqps(result_reg, left_reg);
6607 __ movaps(result_reg, left_reg);
6608 __ cmpeqps(result_reg, right_reg);
6611 case kFloat32x4NotEqual:
6612 if (result_reg.is(left_reg)) {
6613 __ cmpneqps(result_reg, right_reg);
6614 } else if (result_reg.is(right_reg)) {
6615 __ cmpneqps(result_reg, left_reg);
6617 __ movaps(result_reg, left_reg);
6618 __ cmpneqps(result_reg, right_reg);
6621 case kFloat32x4GreaterThanOrEqual:
6622 if (result_reg.is(left_reg)) {
6623 __ cmpnltps(result_reg, right_reg);
6624 } else if (result_reg.is(right_reg)) {
6625 __ cmpltps(result_reg, left_reg);
6627 __ movaps(result_reg, left_reg);
6628 __ cmpnltps(result_reg, right_reg);
6631 case kFloat32x4GreaterThan:
6632 if (result_reg.is(left_reg)) {
6633 __ cmpnleps(result_reg, right_reg);
6634 } else if (result_reg.is(right_reg)) {
6635 __ cmpleps(result_reg, left_reg);
6637 __ movaps(result_reg, left_reg);
6638 __ cmpnleps(result_reg, right_reg);
6653 case kInt32x4GreaterThan:
6655 case kInt32x4LessThan: {
6656 DCHECK(instr->left()->Equals(instr->result()));
6657 DCHECK(instr->hydrogen()->left()->representation().IsInt32x4());
6658 DCHECK(instr->hydrogen()->right()->representation().IsInt32x4());
6659 XMMRegister left_reg = ToInt32x4Register(instr->left());
6660 XMMRegister right_reg = ToInt32x4Register(instr->right());
6661 switch (instr->op()) {
6663 __ andps(left_reg, right_reg);
6666 __ orps(left_reg, right_reg);
6669 __ xorps(left_reg, right_reg);
6672 __ paddd(left_reg, right_reg);
6675 __ psubd(left_reg, right_reg);
6678 if (CpuFeatures::IsSupported(SSE4_1)) {
6679 CpuFeatureScope scope(masm(), SSE4_1);
6680 __ pmulld(left_reg, right_reg);
6682 // The algorithm is from http://stackoverflow.com/questions/10500766/sse-multiplication-of-4-32-bit-integers
6683 XMMRegister xmm_scratch = xmm0;
6684 __ movaps(xmm_scratch, left_reg);
6685 __ pmuludq(left_reg, right_reg);
6686 __ psrldq(xmm_scratch, 4);
6687 __ psrldq(right_reg, 4);
6688 __ pmuludq(xmm_scratch, right_reg);
6689 __ pshufd(left_reg, left_reg, 8);
6690 __ pshufd(xmm_scratch, xmm_scratch, 8);
6691 __ punpackldq(left_reg, xmm_scratch);
6694 case kInt32x4GreaterThan:
6695 __ pcmpgtd(left_reg, right_reg);
6698 __ pcmpeqd(left_reg, right_reg);
6700 case kInt32x4LessThan: {
6701 XMMRegister xmm_scratch = xmm0;
6702 __ movaps(xmm_scratch, right_reg);
6703 __ pcmpgtd(xmm_scratch, left_reg);
6704 __ movaps(left_reg, xmm_scratch);
6713 case kFloat32x4WithW:
6715 case kFloat32x4WithZ:
6717 case kFloat32x4WithY:
6719 case kFloat32x4WithX: {
6720 DCHECK(instr->left()->Equals(instr->result()));
6721 DCHECK(instr->hydrogen()->left()->representation().IsFloat32x4());
6722 DCHECK(instr->hydrogen()->right()->representation().IsDouble());
6723 XMMRegister left_reg = ToFloat32x4Register(instr->left());
6724 XMMRegister right_reg = ToDoubleRegister(instr->right());
6725 XMMRegister xmm_scratch = xmm0;
6726 __ xorps(xmm_scratch, xmm_scratch);
6727 __ cvtsd2ss(xmm_scratch, right_reg);
6728 if (CpuFeatures::IsSupported(SSE4_1)) {
6730 CpuFeatureScope scope(masm(), SSE4_1);
6731 __ insertps(left_reg, xmm_scratch, imm8);
6733 __ sub(esp, Immediate(kFloat32x4Size));
6734 __ movups(Operand(esp, 0), left_reg);
6735 __ movss(Operand(esp, imm8 * kFloatSize), xmm_scratch);
6736 __ movups(left_reg, Operand(esp, 0));
6737 __ add(esp, Immediate(kFloat32x4Size));
6741 case kFloat64x2WithX: {
6742 DCHECK(instr->left()->Equals(instr->result()));
6743 DCHECK(instr->hydrogen()->left()->representation().IsFloat64x2());
6744 DCHECK(instr->hydrogen()->right()->representation().IsDouble());
6745 XMMRegister left_reg = ToFloat64x2Register(instr->left());
6746 XMMRegister right_reg = ToDoubleRegister(instr->right());
6747 __ sub(esp, Immediate(kFloat64x2Size));
6748 __ movups(Operand(esp, 0), left_reg);
6749 __ movsd(Operand(esp, 0 * kDoubleSize), right_reg);
6750 __ movups(left_reg, Operand(esp, 0));
6751 __ add(esp, Immediate(kFloat64x2Size));
6754 case kFloat64x2WithY: {
6755 DCHECK(instr->left()->Equals(instr->result()));
6756 DCHECK(instr->hydrogen()->left()->representation().IsFloat64x2());
6757 DCHECK(instr->hydrogen()->right()->representation().IsDouble());
6758 XMMRegister left_reg = ToFloat64x2Register(instr->left());
6759 XMMRegister right_reg = ToDoubleRegister(instr->right());
6760 __ sub(esp, Immediate(kFloat64x2Size));
6761 __ movups(Operand(esp, 0), left_reg);
6762 __ movsd(Operand(esp, 1 * kDoubleSize), right_reg);
6763 __ movups(left_reg, Operand(esp, 0));
6764 __ add(esp, Immediate(kFloat64x2Size));
6767 case kFloat64x2Constructor: {
6768 DCHECK(instr->hydrogen()->left()->representation().IsDouble());
6769 DCHECK(instr->hydrogen()->right()->representation().IsDouble());
6770 XMMRegister left_reg = ToDoubleRegister(instr->left());
6771 XMMRegister right_reg = ToDoubleRegister(instr->right());
6772 XMMRegister result_reg = ToFloat64x2Register(instr->result());
6773 __ sub(esp, Immediate(kFloat64x2Size));
6774 __ movsd(Operand(esp, 0 * kDoubleSize), left_reg);
6775 __ movsd(Operand(esp, 1 * kDoubleSize), right_reg);
6776 __ movups(result_reg, Operand(esp, 0));
6777 __ add(esp, Immediate(kFloat64x2Size));
6786 case kInt32x4WithX: {
6787 DCHECK(instr->left()->Equals(instr->result()));
6788 DCHECK(instr->hydrogen()->left()->representation().IsInt32x4());
6789 DCHECK(instr->hydrogen()->right()->representation().IsInteger32());
6790 XMMRegister left_reg = ToInt32x4Register(instr->left());
6791 Register right_reg = ToRegister(instr->right());
6792 if (CpuFeatures::IsSupported(SSE4_1)) {
6793 CpuFeatureScope scope(masm(), SSE4_1);
6794 __ pinsrd(left_reg, right_reg, imm8);
6796 __ sub(esp, Immediate(kInt32x4Size));
6797 __ movdqu(Operand(esp, 0), left_reg);
6798 __ mov(Operand(esp, imm8 * kFloatSize), right_reg);
6799 __ movdqu(left_reg, Operand(esp, 0));
6800 __ add(esp, Immediate(kInt32x4Size));
6804 case kInt32x4WithFlagW:
6806 case kInt32x4WithFlagZ:
6808 case kInt32x4WithFlagY:
6810 case kInt32x4WithFlagX: {
6811 DCHECK(instr->left()->Equals(instr->result()));
6812 DCHECK(instr->hydrogen()->left()->representation().IsInt32x4());
6813 DCHECK(instr->hydrogen()->right()->representation().IsTagged());
6814 HType type = instr->hydrogen()->right()->type();
6815 XMMRegister left_reg = ToInt32x4Register(instr->left());
6816 Register right_reg = ToRegister(instr->right());
6817 Label load_false_value, done;
6818 if (type.IsBoolean()) {
6819 __ sub(esp, Immediate(kInt32x4Size));
6820 __ movups(Operand(esp, 0), left_reg);
6821 __ CompareRoot(right_reg, Heap::kTrueValueRootIndex);
6822 __ j(not_equal, &load_false_value, Label::kNear);
6824 Comment(";;; deoptimize: other types for int32x4.withFlagX/Y/Z/W.");
6825 DeoptimizeIf(no_condition, instr,
6826 "other types for int32x4.withFlagX/Y/Z/W");
6830 __ mov(Operand(esp, imm8 * kFloatSize), Immediate(0xFFFFFFFF));
6831 __ jmp(&done, Label::kNear);
6832 __ bind(&load_false_value);
6833 __ mov(Operand(esp, imm8 * kFloatSize), Immediate(0x0));
6835 __ movups(left_reg, Operand(esp, 0));
6836 __ add(esp, Immediate(kInt32x4Size));
6846 void LCodeGen::DoTernarySIMDOperation(LTernarySIMDOperation* instr) {
6847 switch (instr->op()) {
6848 case kFloat32x4Select: {
6849 DCHECK(instr->hydrogen()->first()->representation().IsInt32x4());
6850 DCHECK(instr->hydrogen()->second()->representation().IsFloat32x4());
6851 DCHECK(instr->hydrogen()->third()->representation().IsFloat32x4());
6853 XMMRegister mask_reg = ToInt32x4Register(instr->first());
6854 XMMRegister left_reg = ToFloat32x4Register(instr->second());
6855 XMMRegister right_reg = ToFloat32x4Register(instr->third());
6856 XMMRegister result_reg = ToFloat32x4Register(instr->result());
6857 XMMRegister temp_reg = xmm0;
6860 __ movaps(temp_reg, mask_reg);
6863 // temp_reg = temp_reg & falseValue.
6864 __ andps(temp_reg, right_reg);
6866 if (!result_reg.is(mask_reg)) {
6867 if (result_reg.is(left_reg)) {
6868 // result_reg = result_reg & trueValue.
6869 __ andps(result_reg, mask_reg);
6870 // out = result_reg | temp_reg.
6871 __ orps(result_reg, temp_reg);
6873 __ movaps(result_reg, mask_reg);
6874 // result_reg = result_reg & trueValue.
6875 __ andps(result_reg, left_reg);
6876 // out = result_reg | temp_reg.
6877 __ orps(result_reg, temp_reg);
6880 // result_reg = result_reg & trueValue.
6881 __ andps(result_reg, left_reg);
6882 // out = result_reg | temp_reg.
6883 __ orps(result_reg, temp_reg);
6887 case kInt32x4Select: {
6888 DCHECK(instr->hydrogen()->first()->representation().IsInt32x4());
6889 DCHECK(instr->hydrogen()->second()->representation().IsInt32x4());
6890 DCHECK(instr->hydrogen()->third()->representation().IsInt32x4());
6892 XMMRegister mask_reg = ToInt32x4Register(instr->first());
6893 XMMRegister left_reg = ToInt32x4Register(instr->second());
6894 XMMRegister right_reg = ToInt32x4Register(instr->third());
6895 XMMRegister result_reg = ToInt32x4Register(instr->result());
6896 XMMRegister temp_reg = xmm0;
6899 __ movaps(temp_reg, mask_reg);
6902 // temp_reg = temp_reg & falseValue.
6903 __ andps(temp_reg, right_reg);
6905 if (!result_reg.is(mask_reg)) {
6906 if (result_reg.is(left_reg)) {
6907 // result_reg = result_reg & trueValue.
6908 __ andps(result_reg, mask_reg);
6909 // out = result_reg | temp_reg.
6910 __ orps(result_reg, temp_reg);
6912 __ movaps(result_reg, mask_reg);
6913 // result_reg = result_reg & trueValue.
6914 __ andps(result_reg, left_reg);
6915 // out = result_reg | temp_reg.
6916 __ orps(result_reg, temp_reg);
6919 // result_reg = result_reg & trueValue.
6920 __ andps(result_reg, left_reg);
6921 // out = result_reg | temp_reg.
6922 __ orps(result_reg, temp_reg);
6926 case kFloat32x4ShuffleMix: {
6927 DCHECK(instr->first()->Equals(instr->result()));
6928 DCHECK(instr->hydrogen()->first()->representation().IsFloat32x4());
6929 DCHECK(instr->hydrogen()->second()->representation().IsFloat32x4());
6930 DCHECK(instr->hydrogen()->third()->representation().IsInteger32());
6931 if (instr->hydrogen()->third()->IsConstant() &&
6932 HConstant::cast(instr->hydrogen()->third())->HasInteger32Value()) {
6933 int32_t value = ToInteger32(LConstantOperand::cast(instr->third()));
6934 uint8_t select = static_cast<uint8_t>(value & 0xFF);
6935 XMMRegister first_reg = ToFloat32x4Register(instr->first());
6936 XMMRegister second_reg = ToFloat32x4Register(instr->second());
6937 __ shufps(first_reg, second_reg, select);
6940 Comment(";;; deoptimize: non-constant selector for shuffle");
6941 DeoptimizeIf(no_condition, instr, "non-constant selector for shuffle");
6945 case kFloat32x4Clamp: {
6946 DCHECK(instr->first()->Equals(instr->result()));
6947 DCHECK(instr->hydrogen()->first()->representation().IsFloat32x4());
6948 DCHECK(instr->hydrogen()->second()->representation().IsFloat32x4());
6949 DCHECK(instr->hydrogen()->third()->representation().IsFloat32x4());
6951 XMMRegister value_reg = ToFloat32x4Register(instr->first());
6952 XMMRegister lower_reg = ToFloat32x4Register(instr->second());
6953 XMMRegister upper_reg = ToFloat32x4Register(instr->third());
6954 __ minps(value_reg, upper_reg);
6955 __ maxps(value_reg, lower_reg);
6958 case kFloat64x2Clamp: {
6959 DCHECK(instr->first()->Equals(instr->result()));
6960 DCHECK(instr->hydrogen()->first()->representation().IsFloat64x2());
6961 DCHECK(instr->hydrogen()->second()->representation().IsFloat64x2());
6962 DCHECK(instr->hydrogen()->third()->representation().IsFloat64x2());
6964 XMMRegister value_reg = ToFloat64x2Register(instr->first());
6965 XMMRegister lower_reg = ToFloat64x2Register(instr->second());
6966 XMMRegister upper_reg = ToFloat64x2Register(instr->third());
6967 __ minpd(value_reg, upper_reg);
6968 __ maxpd(value_reg, lower_reg);
6978 void LCodeGen::DoQuarternarySIMDOperation(LQuarternarySIMDOperation* instr) {
6979 switch (instr->op()) {
6980 case kFloat32x4Constructor: {
6981 DCHECK(instr->hydrogen()->x()->representation().IsDouble());
6982 DCHECK(instr->hydrogen()->y()->representation().IsDouble());
6983 DCHECK(instr->hydrogen()->z()->representation().IsDouble());
6984 DCHECK(instr->hydrogen()->w()->representation().IsDouble());
6985 XMMRegister x_reg = ToDoubleRegister(instr->x());
6986 XMMRegister y_reg = ToDoubleRegister(instr->y());
6987 XMMRegister z_reg = ToDoubleRegister(instr->z());
6988 XMMRegister w_reg = ToDoubleRegister(instr->w());
6989 XMMRegister result_reg = ToFloat32x4Register(instr->result());
6990 __ sub(esp, Immediate(kFloat32x4Size));
6991 __ xorps(xmm0, xmm0);
6992 __ cvtsd2ss(xmm0, x_reg);
6993 __ movss(Operand(esp, 0 * kFloatSize), xmm0);
6994 __ xorps(xmm0, xmm0);
6995 __ cvtsd2ss(xmm0, y_reg);
6996 __ movss(Operand(esp, 1 * kFloatSize), xmm0);
6997 __ xorps(xmm0, xmm0);
6998 __ cvtsd2ss(xmm0, z_reg);
6999 __ movss(Operand(esp, 2 * kFloatSize), xmm0);
7000 __ xorps(xmm0, xmm0);
7001 __ cvtsd2ss(xmm0, w_reg);
7002 __ movss(Operand(esp, 3 * kFloatSize), xmm0);
7003 __ movups(result_reg, Operand(esp, 0 * kFloatSize));
7004 __ add(esp, Immediate(kFloat32x4Size));
7007 case kInt32x4Constructor: {
7008 DCHECK(instr->hydrogen()->x()->representation().IsInteger32());
7009 DCHECK(instr->hydrogen()->y()->representation().IsInteger32());
7010 DCHECK(instr->hydrogen()->z()->representation().IsInteger32());
7011 DCHECK(instr->hydrogen()->w()->representation().IsInteger32());
7012 Register x_reg = ToRegister(instr->x());
7013 Register y_reg = ToRegister(instr->y());
7014 Register z_reg = ToRegister(instr->z());
7015 Register w_reg = ToRegister(instr->w());
7016 XMMRegister result_reg = ToInt32x4Register(instr->result());
7017 __ sub(esp, Immediate(kInt32x4Size));
7018 __ mov(Operand(esp, 0 * kInt32Size), x_reg);
7019 __ mov(Operand(esp, 1 * kInt32Size), y_reg);
7020 __ mov(Operand(esp, 2 * kInt32Size), z_reg);
7021 __ mov(Operand(esp, 3 * kInt32Size), w_reg);
7022 __ movups(result_reg, Operand(esp, 0 * kInt32Size));
7023 __ add(esp, Immediate(kInt32x4Size));
7026 case kInt32x4Bool: {
7027 DCHECK(instr->hydrogen()->x()->representation().IsTagged());
7028 DCHECK(instr->hydrogen()->y()->representation().IsTagged());
7029 DCHECK(instr->hydrogen()->z()->representation().IsTagged());
7030 DCHECK(instr->hydrogen()->w()->representation().IsTagged());
7031 HType x_type = instr->hydrogen()->x()->type();
7032 HType y_type = instr->hydrogen()->y()->type();
7033 HType z_type = instr->hydrogen()->z()->type();
7034 HType w_type = instr->hydrogen()->w()->type();
7035 if (!x_type.IsBoolean() || !y_type.IsBoolean() ||
7036 !z_type.IsBoolean() || !w_type.IsBoolean()) {
7037 Comment(";;; deoptimize: other types for int32x4.bool.");
7038 DeoptimizeIf(no_condition, instr, "other types for int32x4.bool");
7041 XMMRegister result_reg = ToInt32x4Register(instr->result());
7042 Register x_reg = ToRegister(instr->x());
7043 Register y_reg = ToRegister(instr->y());
7044 Register z_reg = ToRegister(instr->z());
7045 Register w_reg = ToRegister(instr->w());
7046 Label load_false_x, done_x, load_false_y, done_y,
7047 load_false_z, done_z, load_false_w, done_w;
7048 __ sub(esp, Immediate(kInt32x4Size));
7050 __ CompareRoot(x_reg, Heap::kTrueValueRootIndex);
7051 __ j(not_equal, &load_false_x, Label::kNear);
7052 __ mov(Operand(esp, 0 * kInt32Size), Immediate(-1));
7053 __ jmp(&done_x, Label::kNear);
7054 __ bind(&load_false_x);
7055 __ mov(Operand(esp, 0 * kInt32Size), Immediate(0x0));
7058 __ CompareRoot(y_reg, Heap::kTrueValueRootIndex);
7059 __ j(not_equal, &load_false_y, Label::kNear);
7060 __ mov(Operand(esp, 1 * kInt32Size), Immediate(-1));
7061 __ jmp(&done_y, Label::kNear);
7062 __ bind(&load_false_y);
7063 __ mov(Operand(esp, 1 * kInt32Size), Immediate(0x0));
7066 __ CompareRoot(z_reg, Heap::kTrueValueRootIndex);
7067 __ j(not_equal, &load_false_z, Label::kNear);
7068 __ mov(Operand(esp, 2 * kInt32Size), Immediate(-1));
7069 __ jmp(&done_z, Label::kNear);
7070 __ bind(&load_false_z);
7071 __ mov(Operand(esp, 2 * kInt32Size), Immediate(0x0));
7074 __ CompareRoot(w_reg, Heap::kTrueValueRootIndex);
7075 __ j(not_equal, &load_false_w, Label::kNear);
7076 __ mov(Operand(esp, 3 * kInt32Size), Immediate(-1));
7077 __ jmp(&done_w, Label::kNear);
7078 __ bind(&load_false_w);
7079 __ mov(Operand(esp, 3 * kInt32Size), Immediate(0x0));
7082 __ movups(result_reg, Operand(esp, 0));
7083 __ add(esp, Immediate(kInt32x4Size));
7095 } } // namespace v8::internal
7097 #endif // V8_TARGET_ARCH_IA32