diff options
Diffstat (limited to 'deps/v8/src/x64/lithium-codegen-x64.cc')
-rw-r--r-- | deps/v8/src/x64/lithium-codegen-x64.cc | 1611 |
1 files changed, 1125 insertions, 486 deletions
diff --git a/deps/v8/src/x64/lithium-codegen-x64.cc b/deps/v8/src/x64/lithium-codegen-x64.cc index 48844a5bf..92c889126 100644 --- a/deps/v8/src/x64/lithium-codegen-x64.cc +++ b/deps/v8/src/x64/lithium-codegen-x64.cc @@ -39,7 +39,7 @@ namespace internal { // When invoking builtins, we need to record the safepoint in the middle of // the invoke instruction sequence generated by the macro assembler. -class SafepointGenerator : public PostCallGenerator { +class SafepointGenerator : public CallWrapper { public: SafepointGenerator(LCodeGen* codegen, LPointerMap* pointers, @@ -49,14 +49,26 @@ class SafepointGenerator : public PostCallGenerator { deoptimization_index_(deoptimization_index) { } virtual ~SafepointGenerator() { } - virtual void Generate() { - // Ensure that we have enough space in the reloc info to patch - // this with calls when doing deoptimization. - codegen_->masm()->RecordComment(RelocInfo::kFillerCommentString, true); + virtual void BeforeCall(int call_size) const { + ASSERT(call_size >= 0); + // Ensure that we have enough space after the previous safepoint position + // for the jump generated there. + int call_end = codegen_->masm()->pc_offset() + call_size; + int prev_jump_end = codegen_->LastSafepointEnd() + kMinSafepointSize; + if (call_end < prev_jump_end) { + int padding_size = prev_jump_end - call_end; + STATIC_ASSERT(kMinSafepointSize <= 9); // One multibyte nop is enough. + codegen_->masm()->nop(padding_size); + } + } + + virtual void AfterCall() const { codegen_->RecordSafepoint(pointers_, deoptimization_index_); } private: + static const int kMinSafepointSize = + MacroAssembler::kShortCallInstructionLength; LCodeGen* codegen_; LPointerMap* pointers_; int deoptimization_index_; @@ -79,7 +91,7 @@ bool LCodeGen::GenerateCode() { void LCodeGen::FinishCode(Handle<Code> code) { ASSERT(is_done()); - code->set_stack_slots(StackSlotCount()); + code->set_stack_slots(GetStackSlotCount()); code->set_safepoint_table_offset(safepoints_.GetCodeOffset()); PopulateDeoptimizationData(code); Deoptimizer::EnsureRelocSpaceForLazyDeoptimization(code); @@ -88,8 +100,8 @@ void LCodeGen::FinishCode(Handle<Code> code) { void LCodeGen::Abort(const char* format, ...) { if (FLAG_trace_bailout) { - SmartPointer<char> debug_name = graph()->debug_name()->ToCString(); - PrintF("Aborting LCodeGen in @\"%s\": ", *debug_name); + SmartPointer<char> name(info()->shared_info()->DebugName()->ToCString()); + PrintF("Aborting LCodeGen in @\"%s\": ", *name); va_list arguments; va_start(arguments, format); OS::VPrint(format, arguments); @@ -128,16 +140,31 @@ bool LCodeGen::GeneratePrologue() { } #endif + // Strict mode functions need to replace the receiver with undefined + // when called as functions (without an explicit receiver + // object). rcx is zero for method calls and non-zero for function + // calls. + if (info_->is_strict_mode() || info_->is_native()) { + Label ok; + __ testq(rcx, rcx); + __ j(zero, &ok, Label::kNear); + // +1 for return address. + int receiver_offset = (scope()->num_parameters() + 1) * kPointerSize; + __ LoadRoot(kScratchRegister, Heap::kUndefinedValueRootIndex); + __ movq(Operand(rsp, receiver_offset), kScratchRegister); + __ bind(&ok); + } + __ push(rbp); // Caller's frame pointer. __ movq(rbp, rsp); __ push(rsi); // Callee's context. __ push(rdi); // Callee's JS function. // Reserve space for the stack slots needed by the code. - int slots = StackSlotCount(); + int slots = GetStackSlotCount(); if (slots > 0) { if (FLAG_debug_code) { - __ movl(rax, Immediate(slots)); + __ Set(rax, slots); __ movq(kScratchRegister, kSlotsZapValue, RelocInfo::NONE); Label loop; __ bind(&loop); @@ -170,7 +197,7 @@ bool LCodeGen::GeneratePrologue() { FastNewContextStub stub(heap_slots); __ CallStub(&stub); } else { - __ CallRuntime(Runtime::kNewContext, 1); + __ CallRuntime(Runtime::kNewFunctionContext, 1); } RecordSafepoint(Safepoint::kNoDeoptimizationIndex); // Context is returned in both rax and rsi. It replaces the context @@ -239,9 +266,8 @@ LInstruction* LCodeGen::GetNextInstruction() { bool LCodeGen::GenerateJumpTable() { for (int i = 0; i < jump_table_.length(); i++) { - JumpTableEntry* info = jump_table_[i]; - __ bind(&(info->label_)); - __ Jump(info->address_, RelocInfo::RUNTIME_ENTRY); + __ bind(&jump_table_[i].label); + __ Jump(jump_table_[i].address, RelocInfo::RUNTIME_ENTRY); } return !is_aborted(); } @@ -279,7 +305,7 @@ bool LCodeGen::GenerateSafepointTable() { while (byte_count-- > 0) { __ int3(); } - safepoints_.Emit(masm(), StackSlotCount()); + safepoints_.Emit(masm(), GetStackSlotCount()); return !is_aborted(); } @@ -407,7 +433,7 @@ void LCodeGen::AddToTranslation(Translation* translation, translation->StoreDoubleStackSlot(op->index()); } else if (op->IsArgument()) { ASSERT(is_tagged); - int src_index = StackSlotCount() + op->index(); + int src_index = GetStackSlotCount() + op->index(); translation->StoreStackSlot(src_index); } else if (op->IsRegister()) { Register reg = ToRegister(op); @@ -442,7 +468,7 @@ void LCodeGen::CallCodeGeneric(Handle<Code> code, // Signal that we don't inline smi code before these stubs in the // optimizing code generator. - if (code->kind() == Code::TYPE_RECORDING_BINARY_OP_IC || + if (code->kind() == Code::BINARY_OP_IC || code->kind() == Code::COMPARE_IC) { __ nop(); } @@ -456,7 +482,7 @@ void LCodeGen::CallCode(Handle<Code> code, } -void LCodeGen::CallRuntime(Runtime::Function* function, +void LCodeGen::CallRuntime(const Runtime::Function* function, int num_arguments, LInstruction* instr) { ASSERT(instr != NULL); @@ -549,17 +575,13 @@ void LCodeGen::DeoptimizeIf(Condition cc, LEnvironment* environment) { if (cc == no_condition) { __ Jump(entry, RelocInfo::RUNTIME_ENTRY); } else { - JumpTableEntry* jump_info = NULL; // We often have several deopts to the same entry, reuse the last // jump entry if this is the case. - if (jump_table_.length() > 0 && - jump_table_[jump_table_.length() - 1]->address_ == entry) { - jump_info = jump_table_[jump_table_.length() - 1]; - } else { - jump_info = new JumpTableEntry(entry); - jump_table_.Add(jump_info); + if (jump_table_.is_empty() || + jump_table_.last().address != entry) { + jump_table_.Add(JumpTableEntry(entry)); } - __ j(cc, &jump_info->label_); + __ j(cc, &jump_table_.last().label); } } @@ -569,14 +591,14 @@ void LCodeGen::PopulateDeoptimizationData(Handle<Code> code) { if (length == 0) return; ASSERT(FLAG_deopt); Handle<DeoptimizationInputData> data = - Factory::NewDeoptimizationInputData(length, TENURED); + factory()->NewDeoptimizationInputData(length, TENURED); Handle<ByteArray> translations = translations_.CreateByteArray(); data->SetTranslationByteArray(*translations); data->SetInlinedFunctionCount(Smi::FromInt(inlined_function_count_)); Handle<FixedArray> literals = - Factory::NewFixedArray(deoptimization_literals_.length(), TENURED); + factory()->NewFixedArray(deoptimization_literals_.length(), TENURED); for (int i = 0; i < deoptimization_literals_.length(); i++) { literals->set(i, *deoptimization_literals_[i]); } @@ -670,7 +692,7 @@ void LCodeGen::RecordSafepointWithRegisters(LPointerMap* pointers, void LCodeGen::RecordPosition(int position) { - if (!FLAG_debug_info || position == RelocInfo::kNoPosition) return; + if (position == RelocInfo::kNoPosition) return; masm()->positions_recorder()->RecordPosition(position); } @@ -683,7 +705,7 @@ void LCodeGen::DoLabel(LLabel* label) { } __ bind(label->label()); current_block_ = label->block_id(); - LCodeGen::DoGap(label); + DoGap(label); } @@ -709,6 +731,11 @@ void LCodeGen::DoGap(LGap* gap) { } +void LCodeGen::DoInstructionGap(LInstructionGap* instr) { + DoGap(instr); +} + + void LCodeGen::DoParameter(LParameter* instr) { // Nothing to do. } @@ -732,16 +759,6 @@ void LCodeGen::DoCallStub(LCallStub* instr) { CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); break; } - case CodeStub::StringCharAt: { - StringCharAtStub stub; - CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); - break; - } - case CodeStub::MathPow: { - MathPowStub stub; - CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); - break; - } case CodeStub::NumberToString: { NumberToStringStub stub; CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); @@ -775,41 +792,114 @@ void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) { void LCodeGen::DoModI(LModI* instr) { - LOperand* right = instr->InputAt(1); - ASSERT(ToRegister(instr->result()).is(rdx)); - ASSERT(ToRegister(instr->InputAt(0)).is(rax)); - ASSERT(!ToRegister(instr->InputAt(1)).is(rax)); - ASSERT(!ToRegister(instr->InputAt(1)).is(rdx)); + if (instr->hydrogen()->HasPowerOf2Divisor()) { + Register dividend = ToRegister(instr->InputAt(0)); - Register right_reg = ToRegister(right); + int32_t divisor = + HConstant::cast(instr->hydrogen()->right())->Integer32Value(); + + if (divisor < 0) divisor = -divisor; + + Label positive_dividend, done; + __ testl(dividend, dividend); + __ j(not_sign, &positive_dividend, Label::kNear); + __ negl(dividend); + __ andl(dividend, Immediate(divisor - 1)); + __ negl(dividend); + if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { + __ j(not_zero, &done, Label::kNear); + DeoptimizeIf(no_condition, instr->environment()); + } else { + __ jmp(&done, Label::kNear); + } + __ bind(&positive_dividend); + __ andl(dividend, Immediate(divisor - 1)); + __ bind(&done); + } else { + Label done, remainder_eq_dividend, slow, do_subtraction, both_positive; + Register left_reg = ToRegister(instr->InputAt(0)); + Register right_reg = ToRegister(instr->InputAt(1)); + Register result_reg = ToRegister(instr->result()); + + ASSERT(left_reg.is(rax)); + ASSERT(result_reg.is(rdx)); + ASSERT(!right_reg.is(rax)); + ASSERT(!right_reg.is(rdx)); + + // Check for x % 0. + if (instr->hydrogen()->CheckFlag(HValue::kCanBeDivByZero)) { + __ testl(right_reg, right_reg); + DeoptimizeIf(zero, instr->environment()); + } + + __ testl(left_reg, left_reg); + __ j(zero, &remainder_eq_dividend, Label::kNear); + __ j(sign, &slow, Label::kNear); - // Check for x % 0. - if (instr->hydrogen()->CheckFlag(HValue::kCanBeDivByZero)) { __ testl(right_reg, right_reg); - DeoptimizeIf(zero, instr->environment()); - } + __ j(not_sign, &both_positive, Label::kNear); + // The sign of the divisor doesn't matter. + __ neg(right_reg); - // Sign extend eax to edx. (We are using only the low 32 bits of the values.) - __ cdq(); + __ bind(&both_positive); + // If the dividend is smaller than the nonnegative + // divisor, the dividend is the result. + __ cmpl(left_reg, right_reg); + __ j(less, &remainder_eq_dividend, Label::kNear); + + // Check if the divisor is a PowerOfTwo integer. + Register scratch = ToRegister(instr->TempAt(0)); + __ movl(scratch, right_reg); + __ subl(scratch, Immediate(1)); + __ testl(scratch, right_reg); + __ j(not_zero, &do_subtraction, Label::kNear); + __ andl(left_reg, scratch); + __ jmp(&remainder_eq_dividend, Label::kNear); + + __ bind(&do_subtraction); + const int kUnfolds = 3; + // Try a few subtractions of the dividend. + __ movl(scratch, left_reg); + for (int i = 0; i < kUnfolds; i++) { + // Reduce the dividend by the divisor. + __ subl(left_reg, right_reg); + // Check if the dividend is less than the divisor. + __ cmpl(left_reg, right_reg); + __ j(less, &remainder_eq_dividend, Label::kNear); + } + __ movl(left_reg, scratch); + + // Slow case, using idiv instruction. + __ bind(&slow); + // Sign extend eax to edx. + // (We are using only the low 32 bits of the values.) + __ cdq(); + + // Check for (0 % -x) that will produce negative zero. + if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { + Label positive_left; + Label done; + __ testl(left_reg, left_reg); + __ j(not_sign, &positive_left, Label::kNear); + __ idivl(right_reg); + + // Test the remainder for 0, because then the result would be -0. + __ testl(result_reg, result_reg); + __ j(not_zero, &done, Label::kNear); + + DeoptimizeIf(no_condition, instr->environment()); + __ bind(&positive_left); + __ idivl(right_reg); + __ bind(&done); + } else { + __ idivl(right_reg); + } + __ jmp(&done, Label::kNear); + + __ bind(&remainder_eq_dividend); + __ movl(result_reg, left_reg); - // Check for (0 % -x) that will produce negative zero. - if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { - NearLabel positive_left; - NearLabel done; - __ testl(rax, rax); - __ j(not_sign, &positive_left); - __ idivl(right_reg); - - // Test the remainder for 0, because then the result would be -0. - __ testl(rdx, rdx); - __ j(not_zero, &done); - - DeoptimizeIf(no_condition, instr->environment()); - __ bind(&positive_left); - __ idivl(right_reg); __ bind(&done); - } else { - __ idivl(right_reg); } } @@ -832,9 +922,9 @@ void LCodeGen::DoDivI(LDivI* instr) { // Check for (0 / -x) that will produce negative zero. if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { - NearLabel left_not_zero; + Label left_not_zero; __ testl(left_reg, left_reg); - __ j(not_zero, &left_not_zero); + __ j(not_zero, &left_not_zero, Label::kNear); __ testl(right_reg, right_reg); DeoptimizeIf(sign, instr->environment()); __ bind(&left_not_zero); @@ -842,9 +932,9 @@ void LCodeGen::DoDivI(LDivI* instr) { // Check for (-kMinInt / -1). if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) { - NearLabel left_not_min_int; + Label left_not_min_int; __ cmpl(left_reg, Immediate(kMinInt)); - __ j(not_zero, &left_not_min_int); + __ j(not_zero, &left_not_min_int, Label::kNear); __ cmpl(right_reg, Immediate(-1)); DeoptimizeIf(zero, instr->environment()); __ bind(&left_not_min_int); @@ -868,24 +958,64 @@ void LCodeGen::DoMulI(LMulI* instr) { __ movl(kScratchRegister, left); } + bool can_overflow = + instr->hydrogen()->CheckFlag(HValue::kCanOverflow); if (right->IsConstantOperand()) { int right_value = ToInteger32(LConstantOperand::cast(right)); - __ imull(left, left, Immediate(right_value)); + if (right_value == -1) { + __ negl(left); + } else if (right_value == 0) { + __ xorl(left, left); + } else if (right_value == 2) { + __ addl(left, left); + } else if (!can_overflow) { + // If the multiplication is known to not overflow, we + // can use operations that don't set the overflow flag + // correctly. + switch (right_value) { + case 1: + // Do nothing. + break; + case 3: + __ leal(left, Operand(left, left, times_2, 0)); + break; + case 4: + __ shll(left, Immediate(2)); + break; + case 5: + __ leal(left, Operand(left, left, times_4, 0)); + break; + case 8: + __ shll(left, Immediate(3)); + break; + case 9: + __ leal(left, Operand(left, left, times_8, 0)); + break; + case 16: + __ shll(left, Immediate(4)); + break; + default: + __ imull(left, left, Immediate(right_value)); + break; + } + } else { + __ imull(left, left, Immediate(right_value)); + } } else if (right->IsStackSlot()) { __ imull(left, ToOperand(right)); } else { __ imull(left, ToRegister(right)); } - if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) { + if (can_overflow) { DeoptimizeIf(overflow, instr->environment()); } if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { // Bail out if the result is supposed to be negative zero. - NearLabel done; + Label done; __ testl(left, left); - __ j(not_zero, &done); + __ j(not_zero, &done, Label::kNear); if (right->IsConstantOperand()) { if (ToInteger32(LConstantOperand::cast(right)) <= 0) { DeoptimizeIf(no_condition, instr->environment()); @@ -1038,7 +1168,7 @@ void LCodeGen::DoSubI(LSubI* instr) { void LCodeGen::DoConstantI(LConstantI* instr) { ASSERT(instr->result()->IsRegister()); - __ movl(ToRegister(instr->result()), Immediate(instr->value())); + __ Set(ToRegister(instr->result()), instr->value()); } @@ -1050,7 +1180,7 @@ void LCodeGen::DoConstantD(LConstantD* instr) { // Use xor to produce +0.0 in a fast and compact way, but avoid to // do so if the constant is -0.0. if (int_val == 0) { - __ xorpd(res, res); + __ xorps(res, res); } else { Register tmp = ToRegister(instr->TempAt(0)); __ Set(tmp, int_val); @@ -1079,10 +1209,24 @@ void LCodeGen::DoFixedArrayLength(LFixedArrayLength* instr) { } -void LCodeGen::DoPixelArrayLength(LPixelArrayLength* instr) { +void LCodeGen::DoExternalArrayLength(LExternalArrayLength* instr) { Register result = ToRegister(instr->result()); Register array = ToRegister(instr->InputAt(0)); - __ movq(result, FieldOperand(array, PixelArray::kLengthOffset)); + __ movl(result, FieldOperand(array, ExternalPixelArray::kLengthOffset)); +} + + +void LCodeGen::DoElementsKind(LElementsKind* instr) { + Register result = ToRegister(instr->result()); + Register input = ToRegister(instr->InputAt(0)); + + // Load map into |result|. + __ movq(result, FieldOperand(input, HeapObject::kMapOffset)); + // Load the map's "bit field 2" into |result|. We only need the first byte. + __ movzxbq(result, FieldOperand(result, Map::kBitField2Offset)); + // Retrieve elements_kind from bit field 2. + __ and_(result, Immediate(Map::kElementsKindMask)); + __ shr(result, Immediate(Map::kElementsKindShift)); } @@ -1090,13 +1234,13 @@ void LCodeGen::DoValueOf(LValueOf* instr) { Register input = ToRegister(instr->InputAt(0)); Register result = ToRegister(instr->result()); ASSERT(input.is(result)); - NearLabel done; + Label done; // If the object is a smi return the object. - __ JumpIfSmi(input, &done); + __ JumpIfSmi(input, &done, Label::kNear); // If the object is not a value type, return the object. __ CmpObjectType(input, JS_VALUE_TYPE, kScratchRegister); - __ j(not_equal, &done); + __ j(not_equal, &done, Label::kNear); __ movq(result, FieldOperand(input, JSValue::kValueOffset)); __ bind(&done); @@ -1142,25 +1286,32 @@ void LCodeGen::DoAddI(LAddI* instr) { void LCodeGen::DoArithmeticD(LArithmeticD* instr) { - LOperand* left = instr->InputAt(0); - LOperand* right = instr->InputAt(1); + XMMRegister left = ToDoubleRegister(instr->InputAt(0)); + XMMRegister right = ToDoubleRegister(instr->InputAt(1)); + XMMRegister result = ToDoubleRegister(instr->result()); // All operations except MOD are computed in-place. - ASSERT(instr->op() == Token::MOD || left->Equals(instr->result())); + ASSERT(instr->op() == Token::MOD || left.is(result)); switch (instr->op()) { case Token::ADD: - __ addsd(ToDoubleRegister(left), ToDoubleRegister(right)); + __ addsd(left, right); break; case Token::SUB: - __ subsd(ToDoubleRegister(left), ToDoubleRegister(right)); + __ subsd(left, right); break; case Token::MUL: - __ mulsd(ToDoubleRegister(left), ToDoubleRegister(right)); + __ mulsd(left, right); break; case Token::DIV: - __ divsd(ToDoubleRegister(left), ToDoubleRegister(right)); + __ divsd(left, right); break; case Token::MOD: - Abort("Unimplemented: %s", "DoArithmeticD MOD"); + __ PrepareCallCFunction(2); + __ movaps(xmm0, left); + ASSERT(right.is(xmm1)); + __ CallCFunction( + ExternalReference::double_fp_operation(Token::MOD, isolate()), 2); + __ movq(rsi, Operand(rbp, StandardFrameConstants::kContextOffset)); + __ movaps(result, xmm0); break; default: UNREACHABLE(); @@ -1174,7 +1325,7 @@ void LCodeGen::DoArithmeticT(LArithmeticT* instr) { ASSERT(ToRegister(instr->InputAt(1)).is(rax)); ASSERT(ToRegister(instr->result()).is(rax)); - TypeRecordingBinaryOpStub stub(instr->op(), NO_OVERWRITE); + BinaryOpStub stub(instr->op(), NO_OVERWRITE); CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); } @@ -1219,7 +1370,7 @@ void LCodeGen::DoBranch(LBranch* instr) { EmitBranch(true_block, false_block, not_zero); } else if (r.IsDouble()) { XMMRegister reg = ToDoubleRegister(instr->InputAt(0)); - __ xorpd(xmm0, xmm0); + __ xorps(xmm0, xmm0); __ ucomisd(reg, xmm0); EmitBranch(true_block, false_block, not_equal); } else { @@ -1227,7 +1378,7 @@ void LCodeGen::DoBranch(LBranch* instr) { Register reg = ToRegister(instr->InputAt(0)); HType type = instr->hydrogen()->type(); if (type.IsBoolean()) { - __ Cmp(reg, Factory::true_value()); + __ CompareRoot(reg, Heap::kTrueValueRootIndex); EmitBranch(true_block, false_block, equal); } else if (type.IsSmi()) { __ SmiCompare(reg, Smi::FromInt(0)); @@ -1242,19 +1393,19 @@ void LCodeGen::DoBranch(LBranch* instr) { __ j(equal, true_label); __ CompareRoot(reg, Heap::kFalseValueRootIndex); __ j(equal, false_label); - __ SmiCompare(reg, Smi::FromInt(0)); + __ Cmp(reg, Smi::FromInt(0)); __ j(equal, false_label); __ JumpIfSmi(reg, true_label); // Test for double values. Plus/minus zero and NaN are false. - NearLabel call_stub; + Label call_stub; __ CompareRoot(FieldOperand(reg, HeapObject::kMapOffset), Heap::kHeapNumberMapRootIndex); - __ j(not_equal, &call_stub); + __ j(not_equal, &call_stub, Label::kNear); // HeapNumber => false iff +0, -0, or NaN. These three cases set the // zero flag when compared to zero using ucomisd. - __ xorpd(xmm0, xmm0); + __ xorps(xmm0, xmm0); __ ucomisd(xmm0, FieldOperand(reg, HeapNumber::kValueOffset)); __ j(zero, false_label); __ jmp(true_label); @@ -1262,7 +1413,7 @@ void LCodeGen::DoBranch(LBranch* instr) { // The conversion stub doesn't cause garbage collections so it's // safe to not record a safepoint after the call. __ bind(&call_stub); - ToBooleanStub stub; + ToBooleanStub stub(rax); __ Pushad(); __ push(reg); __ CallStub(&stub); @@ -1274,44 +1425,17 @@ void LCodeGen::DoBranch(LBranch* instr) { } -void LCodeGen::EmitGoto(int block, LDeferredCode* deferred_stack_check) { +void LCodeGen::EmitGoto(int block) { block = chunk_->LookupDestination(block); int next_block = GetNextEmittedBlock(current_block_); if (block != next_block) { - // Perform stack overflow check if this goto needs it before jumping. - if (deferred_stack_check != NULL) { - __ CompareRoot(rsp, Heap::kStackLimitRootIndex); - __ j(above_equal, chunk_->GetAssemblyLabel(block)); - __ jmp(deferred_stack_check->entry()); - deferred_stack_check->SetExit(chunk_->GetAssemblyLabel(block)); - } else { - __ jmp(chunk_->GetAssemblyLabel(block)); - } + __ jmp(chunk_->GetAssemblyLabel(block)); } } -void LCodeGen::DoDeferredStackCheck(LGoto* instr) { - PushSafepointRegistersScope scope(this); - CallRuntimeFromDeferred(Runtime::kStackGuard, 0, instr); -} - - void LCodeGen::DoGoto(LGoto* instr) { - class DeferredStackCheck: public LDeferredCode { - public: - DeferredStackCheck(LCodeGen* codegen, LGoto* instr) - : LDeferredCode(codegen), instr_(instr) { } - virtual void Generate() { codegen()->DoDeferredStackCheck(instr_); } - private: - LGoto* instr_; - }; - - DeferredStackCheck* deferred = NULL; - if (instr->include_stack_check()) { - deferred = new DeferredStackCheck(this, instr); - } - EmitGoto(instr->block_id(), deferred); + EmitGoto(instr->block_id()); } @@ -1364,20 +1488,20 @@ void LCodeGen::DoCmpID(LCmpID* instr) { LOperand* right = instr->InputAt(1); LOperand* result = instr->result(); - NearLabel unordered; + Label unordered; if (instr->is_double()) { // Don't base result on EFLAGS when a NaN is involved. Instead // jump to the unordered case, which produces a false value. __ ucomisd(ToDoubleRegister(left), ToDoubleRegister(right)); - __ j(parity_even, &unordered); + __ j(parity_even, &unordered, Label::kNear); } else { EmitCmpI(left, right); } - NearLabel done; + Label done; Condition cc = TokenToCondition(instr->op(), instr->is_double()); __ LoadRoot(ToRegister(result), Heap::kTrueValueRootIndex); - __ j(cc, &done); + __ j(cc, &done, Label::kNear); __ bind(&unordered); __ LoadRoot(ToRegister(result), Heap::kFalseValueRootIndex); @@ -1405,23 +1529,23 @@ void LCodeGen::DoCmpIDAndBranch(LCmpIDAndBranch* instr) { } -void LCodeGen::DoCmpJSObjectEq(LCmpJSObjectEq* instr) { +void LCodeGen::DoCmpObjectEq(LCmpObjectEq* instr) { Register left = ToRegister(instr->InputAt(0)); Register right = ToRegister(instr->InputAt(1)); Register result = ToRegister(instr->result()); - NearLabel different, done; + Label different, done; __ cmpq(left, right); - __ j(not_equal, &different); + __ j(not_equal, &different, Label::kNear); __ LoadRoot(result, Heap::kTrueValueRootIndex); - __ jmp(&done); + __ jmp(&done, Label::kNear); __ bind(&different); __ LoadRoot(result, Heap::kFalseValueRootIndex); __ bind(&done); } -void LCodeGen::DoCmpJSObjectEqAndBranch(LCmpJSObjectEqAndBranch* instr) { +void LCodeGen::DoCmpObjectEqAndBranch(LCmpObjectEqAndBranch* instr) { Register left = ToRegister(instr->InputAt(0)); Register right = ToRegister(instr->InputAt(1)); int false_block = chunk_->LookupDestination(instr->false_block_id()); @@ -1432,6 +1556,29 @@ void LCodeGen::DoCmpJSObjectEqAndBranch(LCmpJSObjectEqAndBranch* instr) { } +void LCodeGen::DoCmpConstantEq(LCmpConstantEq* instr) { + Register left = ToRegister(instr->InputAt(0)); + Register result = ToRegister(instr->result()); + + Label done; + __ cmpq(left, Immediate(instr->hydrogen()->right())); + __ LoadRoot(result, Heap::kTrueValueRootIndex); + __ j(equal, &done, Label::kNear); + __ LoadRoot(result, Heap::kFalseValueRootIndex); + __ bind(&done); +} + + +void LCodeGen::DoCmpConstantEqAndBranch(LCmpConstantEqAndBranch* instr) { + Register left = ToRegister(instr->InputAt(0)); + int true_block = chunk_->LookupDestination(instr->true_block_id()); + int false_block = chunk_->LookupDestination(instr->false_block_id()); + + __ cmpq(left, Immediate(instr->hydrogen()->right())); + EmitBranch(true_block, false_block, equal); +} + + void LCodeGen::DoIsNull(LIsNull* instr) { Register reg = ToRegister(instr->InputAt(0)); Register result = ToRegister(instr->result()); @@ -1446,28 +1593,29 @@ void LCodeGen::DoIsNull(LIsNull* instr) { __ CompareRoot(reg, Heap::kNullValueRootIndex); if (instr->is_strict()) { + ASSERT(Heap::kTrueValueRootIndex >= 0); __ movl(result, Immediate(Heap::kTrueValueRootIndex)); - NearLabel load; - __ j(equal, &load); - __ movl(result, Immediate(Heap::kFalseValueRootIndex)); + Label load; + __ j(equal, &load, Label::kNear); + __ Set(result, Heap::kFalseValueRootIndex); __ bind(&load); - __ movq(result, Operand(kRootRegister, result, times_pointer_size, 0)); + __ LoadRootIndexed(result, result, 0); } else { - NearLabel true_value, false_value, done; - __ j(equal, &true_value); + Label false_value, true_value, done; + __ j(equal, &true_value, Label::kNear); __ CompareRoot(reg, Heap::kUndefinedValueRootIndex); - __ j(equal, &true_value); - __ JumpIfSmi(reg, &false_value); + __ j(equal, &true_value, Label::kNear); + __ JumpIfSmi(reg, &false_value, Label::kNear); // Check for undetectable objects by looking in the bit field in // the map. The object has already been smi checked. Register scratch = result; __ movq(scratch, FieldOperand(reg, HeapObject::kMapOffset)); __ testb(FieldOperand(scratch, Map::kBitFieldOffset), Immediate(1 << Map::kIsUndetectable)); - __ j(not_zero, &true_value); + __ j(not_zero, &true_value, Label::kNear); __ bind(&false_value); __ LoadRoot(result, Heap::kFalseValueRootIndex); - __ jmp(&done); + __ jmp(&done, Label::kNear); __ bind(&true_value); __ LoadRoot(result, Heap::kTrueValueRootIndex); __ bind(&done); @@ -1491,14 +1639,14 @@ void LCodeGen::DoIsNullAndBranch(LIsNullAndBranch* instr) { int true_block = chunk_->LookupDestination(instr->true_block_id()); - __ Cmp(reg, Factory::null_value()); + __ CompareRoot(reg, Heap::kNullValueRootIndex); if (instr->is_strict()) { EmitBranch(true_block, false_block, equal); } else { Label* true_label = chunk_->GetAssemblyLabel(true_block); Label* false_label = chunk_->GetAssemblyLabel(false_block); __ j(equal, true_label); - __ Cmp(reg, Factory::undefined_value()); + __ CompareRoot(reg, Heap::kUndefinedValueRootIndex); __ j(equal, true_label); __ JumpIfSmi(reg, false_label); // Check for undetectable objects by looking in the bit field in @@ -1530,9 +1678,9 @@ Condition LCodeGen::EmitIsObject(Register input, __ movzxbl(kScratchRegister, FieldOperand(kScratchRegister, Map::kInstanceTypeOffset)); - __ cmpb(kScratchRegister, Immediate(FIRST_JS_OBJECT_TYPE)); + __ cmpb(kScratchRegister, Immediate(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE)); __ j(below, is_not_object); - __ cmpb(kScratchRegister, Immediate(LAST_JS_OBJECT_TYPE)); + __ cmpb(kScratchRegister, Immediate(LAST_NONCALLABLE_SPEC_OBJECT_TYPE)); return below_equal; } @@ -1582,8 +1730,7 @@ void LCodeGen::DoIsSmi(LIsSmi* instr) { } // result is zero if input is a smi, and one otherwise. ASSERT(Heap::kFalseValueRootIndex == Heap::kTrueValueRootIndex + 1); - __ movq(result, Operand(kRootRegister, result, times_pointer_size, - Heap::kTrueValueRootIndex * kPointerSize)); + __ LoadRootIndexed(result, result, Heap::kTrueValueRootIndex); } @@ -1603,6 +1750,40 @@ void LCodeGen::DoIsSmiAndBranch(LIsSmiAndBranch* instr) { } +void LCodeGen::DoIsUndetectable(LIsUndetectable* instr) { + Register input = ToRegister(instr->InputAt(0)); + Register result = ToRegister(instr->result()); + + ASSERT(instr->hydrogen()->value()->representation().IsTagged()); + Label false_label, done; + __ JumpIfSmi(input, &false_label); + __ movq(result, FieldOperand(input, HeapObject::kMapOffset)); + __ testb(FieldOperand(result, Map::kBitFieldOffset), + Immediate(1 << Map::kIsUndetectable)); + __ j(zero, &false_label); + __ LoadRoot(result, Heap::kTrueValueRootIndex); + __ jmp(&done); + __ bind(&false_label); + __ LoadRoot(result, Heap::kFalseValueRootIndex); + __ bind(&done); +} + + +void LCodeGen::DoIsUndetectableAndBranch(LIsUndetectableAndBranch* instr) { + Register input = ToRegister(instr->InputAt(0)); + Register temp = ToRegister(instr->TempAt(0)); + + int true_block = chunk_->LookupDestination(instr->true_block_id()); + int false_block = chunk_->LookupDestination(instr->false_block_id()); + + __ JumpIfSmi(input, chunk_->GetAssemblyLabel(false_block)); + __ movq(temp, FieldOperand(input, HeapObject::kMapOffset)); + __ testb(FieldOperand(temp, Map::kBitFieldOffset), + Immediate(1 << Map::kIsUndetectable)); + EmitBranch(true_block, false_block, not_zero); +} + + static InstanceType TestType(HHasInstanceType* instr) { InstanceType from = instr->from(); InstanceType to = instr->to(); @@ -1629,12 +1810,13 @@ void LCodeGen::DoHasInstanceType(LHasInstanceType* instr) { ASSERT(instr->hydrogen()->value()->representation().IsTagged()); __ testl(input, Immediate(kSmiTagMask)); - NearLabel done, is_false; + Label done, is_false; __ j(zero, &is_false); __ CmpObjectType(input, TestType(instr->hydrogen()), result); - __ j(NegateCondition(BranchCondition(instr->hydrogen())), &is_false); + __ j(NegateCondition(BranchCondition(instr->hydrogen())), + &is_false, Label::kNear); __ LoadRoot(result, Heap::kTrueValueRootIndex); - __ jmp(&done); + __ jmp(&done, Label::kNear); __ bind(&is_false); __ LoadRoot(result, Heap::kFalseValueRootIndex); __ bind(&done); @@ -1656,6 +1838,20 @@ void LCodeGen::DoHasInstanceTypeAndBranch(LHasInstanceTypeAndBranch* instr) { } +void LCodeGen::DoGetCachedArrayIndex(LGetCachedArrayIndex* instr) { + Register input = ToRegister(instr->InputAt(0)); + Register result = ToRegister(instr->result()); + + if (FLAG_debug_code) { + __ AbortIfNotString(input); + } + + __ movl(result, FieldOperand(input, String::kHashFieldOffset)); + ASSERT(String::kHashShift >= kSmiTagSize); + __ IndexFromHash(result, result); +} + + void LCodeGen::DoHasCachedArrayIndex(LHasCachedArrayIndex* instr) { Register input = ToRegister(instr->InputAt(0)); Register result = ToRegister(instr->result()); @@ -1664,8 +1860,8 @@ void LCodeGen::DoHasCachedArrayIndex(LHasCachedArrayIndex* instr) { __ LoadRoot(result, Heap::kTrueValueRootIndex); __ testl(FieldOperand(input, String::kHashFieldOffset), Immediate(String::kContainsCachedArrayIndexMask)); - NearLabel done; - __ j(not_zero, &done); + Label done; + __ j(zero, &done, Label::kNear); __ LoadRoot(result, Heap::kFalseValueRootIndex); __ bind(&done); } @@ -1680,7 +1876,7 @@ void LCodeGen::DoHasCachedArrayIndexAndBranch( __ testl(FieldOperand(input, String::kHashFieldOffset), Immediate(String::kContainsCachedArrayIndexMask)); - EmitBranch(true_block, false_block, not_equal); + EmitBranch(true_block, false_block, equal); } @@ -1692,26 +1888,27 @@ void LCodeGen::EmitClassOfTest(Label* is_true, Register input, Register temp) { __ JumpIfSmi(input, is_false); - __ CmpObjectType(input, FIRST_JS_OBJECT_TYPE, temp); + __ CmpObjectType(input, FIRST_SPEC_OBJECT_TYPE, temp); __ j(below, is_false); // Map is now in temp. // Functions have class 'Function'. - __ CmpInstanceType(temp, JS_FUNCTION_TYPE); + __ CmpInstanceType(temp, FIRST_CALLABLE_SPEC_OBJECT_TYPE); if (class_name->IsEqualTo(CStrVector("Function"))) { - __ j(equal, is_true); + __ j(above_equal, is_true); } else { - __ j(equal, is_false); + __ j(above_equal, is_false); } // Check if the constructor in the map is a function. __ movq(temp, FieldOperand(temp, Map::kConstructorOffset)); - // As long as JS_FUNCTION_TYPE is the last instance type and it is - // right after LAST_JS_OBJECT_TYPE, we can avoid checking for - // LAST_JS_OBJECT_TYPE. - ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); - ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1); + // As long as LAST_CALLABLE_SPEC_OBJECT_TYPE is the last type and + // FIRST_CALLABLE_SPEC_OBJECT_TYPE comes right after + // LAST_NONCALLABLE_SPEC_OBJECT_TYPE, we can avoid checking for the latter. + STATIC_ASSERT(LAST_TYPE == LAST_CALLABLE_SPEC_OBJECT_TYPE); + STATIC_ASSERT(FIRST_CALLABLE_SPEC_OBJECT_TYPE == + LAST_NONCALLABLE_SPEC_OBJECT_TYPE + 1); // Objects with a non-function constructor have class 'Object'. __ CmpObjectType(temp, JS_FUNCTION_TYPE, kScratchRegister); @@ -1744,7 +1941,7 @@ void LCodeGen::DoClassOfTest(LClassOfTest* instr) { ASSERT(input.is(result)); Register temp = ToRegister(instr->TempAt(0)); Handle<String> class_name = instr->hydrogen()->class_name(); - NearLabel done; + Label done; Label is_true, is_false; EmitClassOfTest(&is_true, &is_false, class_name, input, temp); @@ -1753,7 +1950,7 @@ void LCodeGen::DoClassOfTest(LClassOfTest* instr) { __ bind(&is_true); __ LoadRoot(result, Heap::kTrueValueRootIndex); - __ jmp(&done); + __ jmp(&done, Label::kNear); __ bind(&is_false); __ LoadRoot(result, Heap::kFalseValueRootIndex); @@ -1793,30 +1990,17 @@ void LCodeGen::DoInstanceOf(LInstanceOf* instr) { __ push(ToRegister(instr->InputAt(0))); __ push(ToRegister(instr->InputAt(1))); CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); - NearLabel true_value, done; + Label true_value, done; __ testq(rax, rax); - __ j(zero, &true_value); + __ j(zero, &true_value, Label::kNear); __ LoadRoot(ToRegister(instr->result()), Heap::kFalseValueRootIndex); - __ jmp(&done); + __ jmp(&done, Label::kNear); __ bind(&true_value); __ LoadRoot(ToRegister(instr->result()), Heap::kTrueValueRootIndex); __ bind(&done); } -void LCodeGen::DoInstanceOfAndBranch(LInstanceOfAndBranch* instr) { - int true_block = chunk_->LookupDestination(instr->true_block_id()); - int false_block = chunk_->LookupDestination(instr->false_block_id()); - - InstanceofStub stub(InstanceofStub::kNoFlags); - __ push(ToRegister(instr->InputAt(0))); - __ push(ToRegister(instr->InputAt(1))); - CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); - __ testq(rax, rax); - EmitBranch(true_block, false_block, zero); -} - - void LCodeGen::DoInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr) { class DeferredInstanceOfKnownGlobal: public LDeferredCode { public: @@ -1824,26 +2008,53 @@ void LCodeGen::DoInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr) { LInstanceOfKnownGlobal* instr) : LDeferredCode(codegen), instr_(instr) { } virtual void Generate() { - codegen()->DoDeferredLInstanceOfKnownGlobal(instr_); + codegen()->DoDeferredLInstanceOfKnownGlobal(instr_, &map_check_); } + Label* map_check() { return &map_check_; } + private: LInstanceOfKnownGlobal* instr_; + Label map_check_; }; DeferredInstanceOfKnownGlobal* deferred; deferred = new DeferredInstanceOfKnownGlobal(this, instr); - Label false_result; + Label done, false_result; Register object = ToRegister(instr->InputAt(0)); // A Smi is not an instance of anything. __ JumpIfSmi(object, &false_result); - // Null is not an instance of anything. + // This is the inlined call site instanceof cache. The two occurences of the + // hole value will be patched to the last map/result pair generated by the + // instanceof stub. + Label cache_miss; + // Use a temp register to avoid memory operands with variable lengths. + Register map = ToRegister(instr->TempAt(0)); + __ movq(map, FieldOperand(object, HeapObject::kMapOffset)); + __ bind(deferred->map_check()); // Label for calculating code patching. + __ movq(kScratchRegister, factory()->the_hole_value(), + RelocInfo::EMBEDDED_OBJECT); + __ cmpq(map, kScratchRegister); // Patched to cached map. + __ j(not_equal, &cache_miss, Label::kNear); + // Patched to load either true or false. + __ LoadRoot(ToRegister(instr->result()), Heap::kTheHoleValueRootIndex); +#ifdef DEBUG + // Check that the code size between patch label and patch sites is invariant. + Label end_of_patched_code; + __ bind(&end_of_patched_code); + ASSERT(true); +#endif + __ jmp(&done); + + // The inlined call site cache did not match. Check for null and string + // before calling the deferred code. + __ bind(&cache_miss); // Null is not an instance of anything. __ CompareRoot(object, Heap::kNullValueRootIndex); - __ j(equal, &false_result); + __ j(equal, &false_result, Label::kNear); // String values are not instances of anything. __ JumpIfNotString(object, kScratchRegister, deferred->entry()); @@ -1852,23 +2063,40 @@ void LCodeGen::DoInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr) { __ LoadRoot(ToRegister(instr->result()), Heap::kFalseValueRootIndex); __ bind(deferred->exit()); + __ bind(&done); } -void LCodeGen::DoDeferredLInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr) { +void LCodeGen::DoDeferredLInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr, + Label* map_check) { { PushSafepointRegistersScope scope(this); - - InstanceofStub stub(InstanceofStub::kNoFlags); + InstanceofStub::Flags flags = static_cast<InstanceofStub::Flags>( + InstanceofStub::kNoFlags | InstanceofStub::kCallSiteInlineCheck); + InstanceofStub stub(flags); __ push(ToRegister(instr->InputAt(0))); __ Push(instr->function()); - __ movq(rsi, Operand(rbp, StandardFrameConstants::kContextOffset)); + + Register temp = ToRegister(instr->TempAt(0)); + static const int kAdditionalDelta = 10; + int delta = + masm_->SizeOfCodeGeneratedSince(map_check) + kAdditionalDelta; + ASSERT(delta >= 0); + __ push_imm32(delta); + + // We are pushing three values on the stack but recording a + // safepoint with two arguments because stub is going to + // remove the third argument from the stack before jumping + // to instanceof builtin on the slow path. CallCodeGeneric(stub.GetCode(), RelocInfo::CODE_TARGET, instr, RECORD_SAFEPOINT_WITH_REGISTERS, 2); + ASSERT(delta == masm_->SizeOfCodeGeneratedSince(map_check)); + // Move result to a register that survives the end of the + // PushSafepointRegisterScope. __ movq(kScratchRegister, rax); } __ testq(kScratchRegister, kScratchRegister); @@ -1893,36 +2121,17 @@ void LCodeGen::DoCmpT(LCmpT* instr) { if (op == Token::GT || op == Token::LTE) { condition = ReverseCondition(condition); } - NearLabel true_value, done; + Label true_value, done; __ testq(rax, rax); - __ j(condition, &true_value); + __ j(condition, &true_value, Label::kNear); __ LoadRoot(ToRegister(instr->result()), Heap::kFalseValueRootIndex); - __ jmp(&done); + __ jmp(&done, Label::kNear); __ bind(&true_value); __ LoadRoot(ToRegister(instr->result()), Heap::kTrueValueRootIndex); __ bind(&done); } -void LCodeGen::DoCmpTAndBranch(LCmpTAndBranch* instr) { - Token::Value op = instr->op(); - int true_block = chunk_->LookupDestination(instr->true_block_id()); - int false_block = chunk_->LookupDestination(instr->false_block_id()); - - Handle<Code> ic = CompareIC::GetUninitialized(op); - CallCode(ic, RelocInfo::CODE_TARGET, instr); - - // The compare stub expects compare condition and the input operands - // reversed for GT and LTE. - Condition condition = TokenToCondition(op, false); - if (op == Token::GT || op == Token::LTE) { - condition = ReverseCondition(condition); - } - __ testq(rax, rax); - EmitBranch(true_block, false_block, condition); -} - - void LCodeGen::DoReturn(LReturn* instr) { if (FLAG_trace) { // Preserve the return value on the stack and rely on the runtime @@ -1932,11 +2141,11 @@ void LCodeGen::DoReturn(LReturn* instr) { } __ movq(rsp, rbp); __ pop(rbp); - __ Ret((ParameterCount() + 1) * kPointerSize, rcx); + __ Ret((GetParameterCount() + 1) * kPointerSize, rcx); } -void LCodeGen::DoLoadGlobal(LLoadGlobal* instr) { +void LCodeGen::DoLoadGlobalCell(LLoadGlobalCell* instr) { Register result = ToRegister(instr->result()); if (result.is(rax)) { __ load_rax(instr->hydrogen()->cell().location(), @@ -1952,7 +2161,19 @@ void LCodeGen::DoLoadGlobal(LLoadGlobal* instr) { } -void LCodeGen::DoStoreGlobal(LStoreGlobal* instr) { +void LCodeGen::DoLoadGlobalGeneric(LLoadGlobalGeneric* instr) { + ASSERT(ToRegister(instr->global_object()).is(rax)); + ASSERT(ToRegister(instr->result()).is(rax)); + + __ Move(rcx, instr->name()); + RelocInfo::Mode mode = instr->for_typeof() ? RelocInfo::CODE_TARGET : + RelocInfo::CODE_TARGET_CONTEXT; + Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize(); + CallCode(ic, mode, instr); +} + + +void LCodeGen::DoStoreGlobalCell(LStoreGlobalCell* instr) { Register value = ToRegister(instr->InputAt(0)); Register temp = ToRegister(instr->TempAt(0)); ASSERT(!value.is(temp)); @@ -1975,6 +2196,18 @@ void LCodeGen::DoStoreGlobal(LStoreGlobal* instr) { } +void LCodeGen::DoStoreGlobalGeneric(LStoreGlobalGeneric* instr) { + ASSERT(ToRegister(instr->global_object()).is(rdx)); + ASSERT(ToRegister(instr->value()).is(rax)); + + __ Move(rcx, instr->name()); + Handle<Code> ic = instr->strict_mode() + ? isolate()->builtins()->StoreIC_Initialize_Strict() + : isolate()->builtins()->StoreIC_Initialize(); + CallCode(ic, RelocInfo::CODE_TARGET_CONTEXT, instr); +} + + void LCodeGen::DoLoadContextSlot(LLoadContextSlot* instr) { Register context = ToRegister(instr->context()); Register result = ToRegister(instr->result()); @@ -1988,7 +2221,8 @@ void LCodeGen::DoStoreContextSlot(LStoreContextSlot* instr) { __ movq(ContextOperand(context, instr->slot_index()), value); if (instr->needs_write_barrier()) { int offset = Context::SlotOffset(instr->slot_index()); - __ RecordWrite(context, offset, value, kScratchRegister); + Register scratch = ToRegister(instr->TempAt(0)); + __ RecordWrite(context, offset, value, scratch); } } @@ -2005,12 +2239,82 @@ void LCodeGen::DoLoadNamedField(LLoadNamedField* instr) { } +void LCodeGen::EmitLoadFieldOrConstantFunction(Register result, + Register object, + Handle<Map> type, + Handle<String> name) { + LookupResult lookup; + type->LookupInDescriptors(NULL, *name, &lookup); + ASSERT(lookup.IsProperty() && + (lookup.type() == FIELD || lookup.type() == CONSTANT_FUNCTION)); + if (lookup.type() == FIELD) { + int index = lookup.GetLocalFieldIndexFromMap(*type); + int offset = index * kPointerSize; + if (index < 0) { + // Negative property indices are in-object properties, indexed + // from the end of the fixed part of the object. + __ movq(result, FieldOperand(object, offset + type->instance_size())); + } else { + // Non-negative property indices are in the properties array. + __ movq(result, FieldOperand(object, JSObject::kPropertiesOffset)); + __ movq(result, FieldOperand(result, offset + FixedArray::kHeaderSize)); + } + } else { + Handle<JSFunction> function(lookup.GetConstantFunctionFromMap(*type)); + LoadHeapObject(result, Handle<HeapObject>::cast(function)); + } +} + + +void LCodeGen::DoLoadNamedFieldPolymorphic(LLoadNamedFieldPolymorphic* instr) { + Register object = ToRegister(instr->object()); + Register result = ToRegister(instr->result()); + + int map_count = instr->hydrogen()->types()->length(); + Handle<String> name = instr->hydrogen()->name(); + + if (map_count == 0) { + ASSERT(instr->hydrogen()->need_generic()); + __ Move(rcx, instr->hydrogen()->name()); + Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize(); + CallCode(ic, RelocInfo::CODE_TARGET, instr); + } else { + Label done; + for (int i = 0; i < map_count - 1; ++i) { + Handle<Map> map = instr->hydrogen()->types()->at(i); + Label next; + __ Cmp(FieldOperand(object, HeapObject::kMapOffset), map); + __ j(not_equal, &next, Label::kNear); + EmitLoadFieldOrConstantFunction(result, object, map, name); + __ jmp(&done, Label::kNear); + __ bind(&next); + } + Handle<Map> map = instr->hydrogen()->types()->last(); + __ Cmp(FieldOperand(object, HeapObject::kMapOffset), map); + if (instr->hydrogen()->need_generic()) { + Label generic; + __ j(not_equal, &generic, Label::kNear); + EmitLoadFieldOrConstantFunction(result, object, map, name); + __ jmp(&done, Label::kNear); + __ bind(&generic); + __ Move(rcx, instr->hydrogen()->name()); + Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize(); + CallCode(ic, RelocInfo::CODE_TARGET, instr); + } else { + DeoptimizeIf(not_equal, instr->environment()); + EmitLoadFieldOrConstantFunction(result, object, map, name); + } + __ bind(&done); + } +} + + void LCodeGen::DoLoadNamedGeneric(LLoadNamedGeneric* instr) { ASSERT(ToRegister(instr->object()).is(rax)); ASSERT(ToRegister(instr->result()).is(rax)); __ Move(rcx, instr->name()); - Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); + Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize(); CallCode(ic, RelocInfo::CODE_TARGET, instr); } @@ -2024,10 +2328,10 @@ void LCodeGen::DoLoadFunctionPrototype(LLoadFunctionPrototype* instr) { DeoptimizeIf(not_equal, instr->environment()); // Check whether the function has an instance prototype. - NearLabel non_instance; + Label non_instance; __ testb(FieldOperand(result, Map::kBitFieldOffset), Immediate(1 << Map::kHasNonInstancePrototype)); - __ j(not_zero, &non_instance); + __ j(not_zero, &non_instance, Label::kNear); // Get the prototype or initial map from the function. __ movq(result, @@ -2038,13 +2342,13 @@ void LCodeGen::DoLoadFunctionPrototype(LLoadFunctionPrototype* instr) { DeoptimizeIf(equal, instr->environment()); // If the function does not have an initial map, we're done. - NearLabel done; + Label done; __ CmpObjectType(result, MAP_TYPE, kScratchRegister); - __ j(not_equal, &done); + __ j(not_equal, &done, Label::kNear); // Get the prototype from the initial map. __ movq(result, FieldOperand(result, Map::kPrototypeOffset)); - __ jmp(&done); + __ jmp(&done, Label::kNear); // Non-instance prototype: Fetch prototype from constructor field // in the function's map. @@ -2061,26 +2365,40 @@ void LCodeGen::DoLoadElements(LLoadElements* instr) { Register input = ToRegister(instr->InputAt(0)); __ movq(result, FieldOperand(input, JSObject::kElementsOffset)); if (FLAG_debug_code) { - NearLabel done; - __ Cmp(FieldOperand(result, HeapObject::kMapOffset), - Factory::fixed_array_map()); - __ j(equal, &done); - __ Cmp(FieldOperand(result, HeapObject::kMapOffset), - Factory::pixel_array_map()); - __ j(equal, &done); - __ Cmp(FieldOperand(result, HeapObject::kMapOffset), - Factory::fixed_cow_array_map()); - __ Check(equal, "Check for fast elements failed."); + Label done, ok, fail; + __ CompareRoot(FieldOperand(result, HeapObject::kMapOffset), + Heap::kFixedArrayMapRootIndex); + __ j(equal, &done, Label::kNear); + __ CompareRoot(FieldOperand(result, HeapObject::kMapOffset), + Heap::kFixedCOWArrayMapRootIndex); + __ j(equal, &done, Label::kNear); + Register temp((result.is(rax)) ? rbx : rax); + __ push(temp); + __ movq(temp, FieldOperand(result, HeapObject::kMapOffset)); + __ movzxbq(temp, FieldOperand(temp, Map::kBitField2Offset)); + __ and_(temp, Immediate(Map::kElementsKindMask)); + __ shr(temp, Immediate(Map::kElementsKindShift)); + __ cmpl(temp, Immediate(JSObject::FAST_ELEMENTS)); + __ j(equal, &ok, Label::kNear); + __ cmpl(temp, Immediate(JSObject::FIRST_EXTERNAL_ARRAY_ELEMENTS_KIND)); + __ j(less, &fail, Label::kNear); + __ cmpl(temp, Immediate(JSObject::LAST_EXTERNAL_ARRAY_ELEMENTS_KIND)); + __ j(less_equal, &ok, Label::kNear); + __ bind(&fail); + __ Abort("Check for fast or external elements failed"); + __ bind(&ok); + __ pop(temp); __ bind(&done); } } -void LCodeGen::DoLoadPixelArrayExternalPointer( - LLoadPixelArrayExternalPointer* instr) { +void LCodeGen::DoLoadExternalArrayPointer( + LLoadExternalArrayPointer* instr) { Register result = ToRegister(instr->result()); Register input = ToRegister(instr->InputAt(0)); - __ movq(result, FieldOperand(input, PixelArray::kExternalPointerOffset)); + __ movq(result, FieldOperand(input, + ExternalPixelArray::kExternalPointerOffset)); } @@ -2115,19 +2433,80 @@ void LCodeGen::DoLoadKeyedFastElement(LLoadKeyedFastElement* instr) { FixedArray::kHeaderSize)); // Check for the hole value. - __ Cmp(result, Factory::the_hole_value()); - DeoptimizeIf(equal, instr->environment()); + if (instr->hydrogen()->RequiresHoleCheck()) { + __ CompareRoot(result, Heap::kTheHoleValueRootIndex); + DeoptimizeIf(equal, instr->environment()); + } } -void LCodeGen::DoLoadPixelArrayElement(LLoadPixelArrayElement* instr) { - Register external_elements = ToRegister(instr->external_pointer()); - Register key = ToRegister(instr->key()); - Register result = ToRegister(instr->result()); - ASSERT(result.is(external_elements)); +Operand LCodeGen::BuildExternalArrayOperand( + LOperand* external_pointer, + LOperand* key, + JSObject::ElementsKind elements_kind) { + Register external_pointer_reg = ToRegister(external_pointer); + int shift_size = ElementsKindToShiftSize(elements_kind); + if (key->IsConstantOperand()) { + int constant_value = ToInteger32(LConstantOperand::cast(key)); + if (constant_value & 0xF0000000) { + Abort("array index constant value too big"); + } + return Operand(external_pointer_reg, constant_value * (1 << shift_size)); + } else { + ScaleFactor scale_factor = static_cast<ScaleFactor>(shift_size); + return Operand(external_pointer_reg, ToRegister(key), scale_factor, 0); + } +} - // Load the result. - __ movzxbq(result, Operand(external_elements, key, times_1, 0)); + +void LCodeGen::DoLoadKeyedSpecializedArrayElement( + LLoadKeyedSpecializedArrayElement* instr) { + JSObject::ElementsKind elements_kind = instr->elements_kind(); + Operand operand(BuildExternalArrayOperand(instr->external_pointer(), + instr->key(), elements_kind)); + if (elements_kind == JSObject::EXTERNAL_FLOAT_ELEMENTS) { + XMMRegister result(ToDoubleRegister(instr->result())); + __ movss(result, operand); + __ cvtss2sd(result, result); + } else if (elements_kind == JSObject::EXTERNAL_DOUBLE_ELEMENTS) { + __ movsd(ToDoubleRegister(instr->result()), operand); + } else { + Register result(ToRegister(instr->result())); + switch (elements_kind) { + case JSObject::EXTERNAL_BYTE_ELEMENTS: + __ movsxbq(result, operand); + break; + case JSObject::EXTERNAL_UNSIGNED_BYTE_ELEMENTS: + case JSObject::EXTERNAL_PIXEL_ELEMENTS: + __ movzxbq(result, operand); + break; + case JSObject::EXTERNAL_SHORT_ELEMENTS: + __ movsxwq(result, operand); + break; + case JSObject::EXTERNAL_UNSIGNED_SHORT_ELEMENTS: + __ movzxwq(result, operand); + break; + case JSObject::EXTERNAL_INT_ELEMENTS: + __ movsxlq(result, operand); + break; + case JSObject::EXTERNAL_UNSIGNED_INT_ELEMENTS: + __ movl(result, operand); + __ testl(result, result); + // TODO(danno): we could be more clever here, perhaps having a special + // version of the stub that detects if the overflow case actually + // happens, and generate code that returns a double rather than int. + DeoptimizeIf(negative, instr->environment()); + break; + case JSObject::EXTERNAL_FLOAT_ELEMENTS: + case JSObject::EXTERNAL_DOUBLE_ELEMENTS: + case JSObject::FAST_ELEMENTS: + case JSObject::FAST_DOUBLE_ELEMENTS: + case JSObject::DICTIONARY_ELEMENTS: + case JSObject::NON_STRICT_ARGUMENTS_ELEMENTS: + UNREACHABLE(); + break; + } + } } @@ -2135,7 +2514,7 @@ void LCodeGen::DoLoadKeyedGeneric(LLoadKeyedGeneric* instr) { ASSERT(ToRegister(instr->object()).is(rdx)); ASSERT(ToRegister(instr->key()).is(rax)); - Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize)); + Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize(); CallCode(ic, RelocInfo::CODE_TARGET, instr); } @@ -2144,15 +2523,15 @@ void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) { Register result = ToRegister(instr->result()); // Check for arguments adapter frame. - NearLabel done, adapted; + Label done, adapted; __ movq(result, Operand(rbp, StandardFrameConstants::kCallerFPOffset)); - __ SmiCompare(Operand(result, StandardFrameConstants::kContextOffset), - Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)); - __ j(equal, &adapted); + __ Cmp(Operand(result, StandardFrameConstants::kContextOffset), + Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)); + __ j(equal, &adapted, Label::kNear); // No arguments adaptor frame. __ movq(result, rbp); - __ jmp(&done); + __ jmp(&done, Label::kNear); // Arguments adaptor frame present. __ bind(&adapted); @@ -2167,7 +2546,7 @@ void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) { void LCodeGen::DoArgumentsLength(LArgumentsLength* instr) { Register result = ToRegister(instr->result()); - NearLabel done; + Label done; // If no arguments adaptor frame the number of arguments is fixed. if (instr->InputAt(0)->IsRegister()) { @@ -2175,14 +2554,14 @@ void LCodeGen::DoArgumentsLength(LArgumentsLength* instr) { } else { __ cmpq(rbp, ToOperand(instr->InputAt(0))); } - __ movq(result, Immediate(scope()->num_parameters())); - __ j(equal, &done); + __ movl(result, Immediate(scope()->num_parameters())); + __ j(equal, &done, Label::kNear); // Arguments adaptor frame present. Get argument length from there. __ movq(result, Operand(rbp, StandardFrameConstants::kCallerFPOffset)); - __ movq(result, Operand(result, - ArgumentsAdaptorFrameConstants::kLengthOffset)); - __ SmiToInteger32(result, result); + __ SmiToInteger32(result, + Operand(result, + ArgumentsAdaptorFrameConstants::kLengthOffset)); // Argument length is in result register. __ bind(&done); @@ -2198,27 +2577,46 @@ void LCodeGen::DoApplyArguments(LApplyArguments* instr) { ASSERT(function.is(rdi)); // Required by InvokeFunction. ASSERT(ToRegister(instr->result()).is(rax)); - // If the receiver is null or undefined, we have to pass the global object - // as a receiver. - NearLabel global_object, receiver_ok; + // If the receiver is null or undefined, we have to pass the global + // object as a receiver to normal functions. Values have to be + // passed unchanged to builtins and strict-mode functions. + Label global_object, receiver_ok; + + // Do not transform the receiver to object for strict mode + // functions. + __ movq(kScratchRegister, + FieldOperand(function, JSFunction::kSharedFunctionInfoOffset)); + __ testb(FieldOperand(kScratchRegister, + SharedFunctionInfo::kStrictModeByteOffset), + Immediate(1 << SharedFunctionInfo::kStrictModeBitWithinByte)); + __ j(not_equal, &receiver_ok, Label::kNear); + + // Do not transform the receiver to object for builtins. + __ testb(FieldOperand(kScratchRegister, + SharedFunctionInfo::kNativeByteOffset), + Immediate(1 << SharedFunctionInfo::kNativeBitWithinByte)); + __ j(not_equal, &receiver_ok, Label::kNear); + + // Normal function. Replace undefined or null with global receiver. __ CompareRoot(receiver, Heap::kNullValueRootIndex); - __ j(equal, &global_object); + __ j(equal, &global_object, Label::kNear); __ CompareRoot(receiver, Heap::kUndefinedValueRootIndex); - __ j(equal, &global_object); + __ j(equal, &global_object, Label::kNear); // The receiver should be a JS object. Condition is_smi = __ CheckSmi(receiver); DeoptimizeIf(is_smi, instr->environment()); - __ CmpObjectType(receiver, FIRST_JS_OBJECT_TYPE, kScratchRegister); + __ CmpObjectType(receiver, FIRST_SPEC_OBJECT_TYPE, kScratchRegister); DeoptimizeIf(below, instr->environment()); - __ jmp(&receiver_ok); + __ jmp(&receiver_ok, Label::kNear); __ bind(&global_object); // TODO(kmillikin): We have a hydrogen value for the global object. See // if it's better to use it than to explicitly fetch it from the context // here. - __ movq(receiver, Operand(rbp, StandardFrameConstants::kContextOffset)); - __ movq(receiver, ContextOperand(receiver, Context::GLOBAL_INDEX)); + __ movq(receiver, ContextOperand(rsi, Context::GLOBAL_INDEX)); + __ movq(receiver, + FieldOperand(receiver, JSGlobalObject::kGlobalReceiverOffset)); __ bind(&receiver_ok); // Copy the arguments to this function possibly from the @@ -2232,10 +2630,10 @@ void LCodeGen::DoApplyArguments(LApplyArguments* instr) { // Loop through the arguments pushing them onto the execution // stack. - NearLabel invoke, loop; + Label invoke, loop; // length is a small non-negative integer, due to the test above. __ testl(length, length); - __ j(zero, &invoke); + __ j(zero, &invoke, Label::kNear); __ bind(&loop); __ push(Operand(elements, length, times_pointer_size, 1 * kPointerSize)); __ decl(length); @@ -2252,26 +2650,27 @@ void LCodeGen::DoApplyArguments(LApplyArguments* instr) { pointers, env->deoptimization_index()); v8::internal::ParameterCount actual(rax); - __ InvokeFunction(function, actual, CALL_FUNCTION, &safepoint_generator); + __ InvokeFunction(function, actual, CALL_FUNCTION, + safepoint_generator, CALL_AS_METHOD); + __ movq(rsi, Operand(rbp, StandardFrameConstants::kContextOffset)); } void LCodeGen::DoPushArgument(LPushArgument* instr) { LOperand* argument = instr->InputAt(0); - if (argument->IsConstantOperand()) { - EmitPushConstantOperand(argument); - } else if (argument->IsRegister()) { - __ push(ToRegister(argument)); - } else { - ASSERT(!argument->IsDoubleRegister()); - __ push(ToOperand(argument)); - } + EmitPushTaggedOperand(argument); +} + + +void LCodeGen::DoThisFunction(LThisFunction* instr) { + Register result = ToRegister(instr->result()); + __ movq(result, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset)); } void LCodeGen::DoContext(LContext* instr) { Register result = ToRegister(instr->result()); - __ movq(result, Operand(rbp, StandardFrameConstants::kContextOffset)); + __ movq(result, rsi); } @@ -2279,8 +2678,7 @@ void LCodeGen::DoOuterContext(LOuterContext* instr) { Register context = ToRegister(instr->context()); Register result = ToRegister(instr->result()); __ movq(result, - Operand(context, Context::SlotOffset(Context::CLOSURE_INDEX))); - __ movq(result, FieldOperand(result, JSFunction::kContextOffset)); + Operand(context, Context::SlotOffset(Context::PREVIOUS_INDEX))); } @@ -2291,18 +2689,19 @@ void LCodeGen::DoGlobalObject(LGlobalObject* instr) { void LCodeGen::DoGlobalReceiver(LGlobalReceiver* instr) { + Register global = ToRegister(instr->global()); Register result = ToRegister(instr->result()); - __ movq(result, Operand(rsi, Context::SlotOffset(Context::GLOBAL_INDEX))); - __ movq(result, FieldOperand(result, GlobalObject::kGlobalReceiverOffset)); + __ movq(result, FieldOperand(global, GlobalObject::kGlobalReceiverOffset)); } void LCodeGen::CallKnownFunction(Handle<JSFunction> function, int arity, - LInstruction* instr) { + LInstruction* instr, + CallKind call_kind) { // Change context if needed. bool change_context = - (graph()->info()->closure()->context() != function->context()) || + (info()->closure()->context() != function->context()) || scope()->contains_with() || (scope()->num_heap_slots() > 0); if (change_context) { @@ -2319,7 +2718,8 @@ void LCodeGen::CallKnownFunction(Handle<JSFunction> function, RecordPosition(pointers->position()); // Invoke function. - if (*function == *graph()->info()->closure()) { + __ SetCallKind(rcx, call_kind); + if (*function == *info()->closure()) { __ CallSelf(); } else { __ call(FieldOperand(rdi, JSFunction::kCodeEntryOffset)); @@ -2336,7 +2736,10 @@ void LCodeGen::CallKnownFunction(Handle<JSFunction> function, void LCodeGen::DoCallConstantFunction(LCallConstantFunction* instr) { ASSERT(ToRegister(instr->result()).is(rax)); __ Move(rdi, instr->function()); - CallKnownFunction(instr->function(), instr->arity(), instr); + CallKnownFunction(instr->function(), + instr->arity(), + instr, + CALL_AS_METHOD); } @@ -2423,7 +2826,7 @@ void LCodeGen::DoMathAbs(LUnaryMathOperation* instr) { if (r.IsDouble()) { XMMRegister scratch = xmm0; XMMRegister input_reg = ToDoubleRegister(instr->InputAt(0)); - __ xorpd(scratch, scratch); + __ xorps(scratch, scratch); __ subsd(scratch, input_reg); __ andpd(input_reg, scratch); } else if (r.IsInteger32()) { @@ -2434,7 +2837,9 @@ void LCodeGen::DoMathAbs(LUnaryMathOperation* instr) { Register input_reg = ToRegister(instr->InputAt(0)); // Smi check. __ JumpIfNotSmi(input_reg, deferred->entry()); + __ SmiToInteger32(input_reg, input_reg); EmitIntegerMathAbs(instr); + __ Integer32ToSmi(input_reg, input_reg); __ bind(deferred->exit()); } } @@ -2444,21 +2849,36 @@ void LCodeGen::DoMathFloor(LUnaryMathOperation* instr) { XMMRegister xmm_scratch = xmm0; Register output_reg = ToRegister(instr->result()); XMMRegister input_reg = ToDoubleRegister(instr->InputAt(0)); - __ xorpd(xmm_scratch, xmm_scratch); // Zero the register. - __ ucomisd(input_reg, xmm_scratch); - if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { - DeoptimizeIf(below_equal, instr->environment()); + if (CpuFeatures::IsSupported(SSE4_1)) { + CpuFeatures::Scope scope(SSE4_1); + if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { + // Deoptimize if minus zero. + __ movq(output_reg, input_reg); + __ subq(output_reg, Immediate(1)); + DeoptimizeIf(overflow, instr->environment()); + } + __ roundsd(xmm_scratch, input_reg, Assembler::kRoundDown); + __ cvttsd2si(output_reg, xmm_scratch); + __ cmpl(output_reg, Immediate(0x80000000)); + DeoptimizeIf(equal, instr->environment()); } else { - DeoptimizeIf(below, instr->environment()); - } + __ xorps(xmm_scratch, xmm_scratch); // Zero the register. + __ ucomisd(input_reg, xmm_scratch); - // Use truncating instruction (OK because input is positive). - __ cvttsd2si(output_reg, input_reg); + if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { + DeoptimizeIf(below_equal, instr->environment()); + } else { + DeoptimizeIf(below, instr->environment()); + } - // Overflow is signalled with minint. - __ cmpl(output_reg, Immediate(0x80000000)); - DeoptimizeIf(equal, instr->environment()); + // Use truncating instruction (OK because input is positive). + __ cvttsd2si(output_reg, input_reg); + + // Overflow is signalled with minint. + __ cmpl(output_reg, Immediate(0x80000000)); + DeoptimizeIf(equal, instr->environment()); + } } @@ -2467,33 +2887,45 @@ void LCodeGen::DoMathRound(LUnaryMathOperation* instr) { Register output_reg = ToRegister(instr->result()); XMMRegister input_reg = ToDoubleRegister(instr->InputAt(0)); + Label done; // xmm_scratch = 0.5 __ movq(kScratchRegister, V8_INT64_C(0x3FE0000000000000), RelocInfo::NONE); __ movq(xmm_scratch, kScratchRegister); - + Label below_half; + __ ucomisd(xmm_scratch, input_reg); + // If input_reg is NaN, this doesn't jump. + __ j(above, &below_half, Label::kNear); // input = input + 0.5 + // This addition might give a result that isn't the correct for + // rounding, due to loss of precision, but only for a number that's + // so big that the conversion below will overflow anyway. __ addsd(input_reg, xmm_scratch); + // Compute Math.floor(input). + // Use truncating instruction (OK because input is positive). + __ cvttsd2si(output_reg, input_reg); + // Overflow is signalled with minint. + __ cmpl(output_reg, Immediate(0x80000000)); + DeoptimizeIf(equal, instr->environment()); + __ jmp(&done); - // We need to return -0 for the input range [-0.5, 0[, otherwise - // compute Math.floor(value + 0.5). + __ bind(&below_half); if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { - __ ucomisd(input_reg, xmm_scratch); - DeoptimizeIf(below_equal, instr->environment()); + // Bailout if negative (including -0). + __ movq(output_reg, input_reg); + __ testq(output_reg, output_reg); + DeoptimizeIf(negative, instr->environment()); } else { - // If we don't need to bailout on -0, we check only bailout - // on negative inputs. - __ xorpd(xmm_scratch, xmm_scratch); // Zero the register. + // Bailout if below -0.5, otherwise round to (positive) zero, even + // if negative. + // xmm_scrach = -0.5 + __ movq(kScratchRegister, V8_INT64_C(0xBFE0000000000000), RelocInfo::NONE); + __ movq(xmm_scratch, kScratchRegister); __ ucomisd(input_reg, xmm_scratch); DeoptimizeIf(below, instr->environment()); } + __ xorl(output_reg, output_reg); - // Compute Math.floor(value + 0.5). - // Use truncating instruction (OK because input is positive). - __ cvttsd2si(output_reg, input_reg); - - // Overflow is signalled with minint. - __ cmpl(output_reg, Immediate(0x80000000)); - DeoptimizeIf(equal, instr->environment()); + __ bind(&done); } @@ -2508,7 +2940,7 @@ void LCodeGen::DoMathPowHalf(LUnaryMathOperation* instr) { XMMRegister xmm_scratch = xmm0; XMMRegister input_reg = ToDoubleRegister(instr->InputAt(0)); ASSERT(ToDoubleRegister(instr->result()).is(input_reg)); - __ xorpd(xmm_scratch, xmm_scratch); + __ xorps(xmm_scratch, xmm_scratch); __ addsd(input_reg, xmm_scratch); // Convert -0 to +0. __ sqrtsd(input_reg, input_reg); } @@ -2524,23 +2956,24 @@ void LCodeGen::DoPower(LPower* instr) { if (exponent_type.IsDouble()) { __ PrepareCallCFunction(2); // Move arguments to correct registers - __ movsd(xmm0, left_reg); + __ movaps(xmm0, left_reg); ASSERT(ToDoubleRegister(right).is(xmm1)); - __ CallCFunction(ExternalReference::power_double_double_function(), 2); + __ CallCFunction( + ExternalReference::power_double_double_function(isolate()), 2); } else if (exponent_type.IsInteger32()) { __ PrepareCallCFunction(2); // Move arguments to correct registers: xmm0 and edi (not rdi). // On Windows, the registers are xmm0 and edx. - __ movsd(xmm0, left_reg); + __ movaps(xmm0, left_reg); #ifdef _WIN64 ASSERT(ToRegister(right).is(rdx)); #else ASSERT(ToRegister(right).is(rdi)); #endif - __ CallCFunction(ExternalReference::power_double_int_function(), 2); + __ CallCFunction( + ExternalReference::power_double_int_function(isolate()), 2); } else { ASSERT(exponent_type.IsTagged()); - CpuFeatures::Scope scope(SSE2); Register right_reg = ToRegister(right); Label non_smi, call; @@ -2557,12 +2990,15 @@ void LCodeGen::DoPower(LPower* instr) { __ bind(&call); __ PrepareCallCFunction(2); // Move arguments to correct registers xmm0 and xmm1. - __ movsd(xmm0, left_reg); + __ movaps(xmm0, left_reg); // Right argument is already in xmm1. - __ CallCFunction(ExternalReference::power_double_double_function(), 2); + __ CallCFunction( + ExternalReference::power_double_double_function(isolate()), 2); } // Return value is in xmm0. - __ movsd(result_reg, xmm0); + __ movaps(result_reg, xmm0); + // Restore context register. + __ movq(rsi, Operand(rbp, StandardFrameConstants::kContextOffset)); } @@ -2576,7 +3012,7 @@ void LCodeGen::DoMathLog(LUnaryMathOperation* instr) { void LCodeGen::DoMathCos(LUnaryMathOperation* instr) { ASSERT(ToDoubleRegister(instr->result()).is(xmm1)); - TranscendentalCacheStub stub(TranscendentalCache::LOG, + TranscendentalCacheStub stub(TranscendentalCache::COS, TranscendentalCacheStub::UNTAGGED); CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); } @@ -2584,7 +3020,7 @@ void LCodeGen::DoMathCos(LUnaryMathOperation* instr) { void LCodeGen::DoMathSin(LUnaryMathOperation* instr) { ASSERT(ToDoubleRegister(instr->result()).is(xmm1)); - TranscendentalCacheStub stub(TranscendentalCache::LOG, + TranscendentalCacheStub stub(TranscendentalCache::SIN, TranscendentalCacheStub::UNTAGGED); CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); } @@ -2623,12 +3059,28 @@ void LCodeGen::DoUnaryMathOperation(LUnaryMathOperation* instr) { } +void LCodeGen::DoInvokeFunction(LInvokeFunction* instr) { + ASSERT(ToRegister(instr->function()).is(rdi)); + ASSERT(instr->HasPointerMap()); + ASSERT(instr->HasDeoptimizationEnvironment()); + LPointerMap* pointers = instr->pointer_map(); + LEnvironment* env = instr->deoptimization_environment(); + RecordPosition(pointers->position()); + RegisterEnvironmentForDeoptimization(env); + SafepointGenerator generator(this, pointers, env->deoptimization_index()); + ParameterCount count(instr->arity()); + __ InvokeFunction(rdi, count, CALL_FUNCTION, generator, CALL_AS_METHOD); + __ movq(rsi, Operand(rbp, StandardFrameConstants::kContextOffset)); +} + + void LCodeGen::DoCallKeyed(LCallKeyed* instr) { ASSERT(ToRegister(instr->key()).is(rcx)); ASSERT(ToRegister(instr->result()).is(rax)); int arity = instr->arity(); - Handle<Code> ic = StubCache::ComputeKeyedCallInitialize(arity, NOT_IN_LOOP); + Handle<Code> ic = isolate()->stub_cache()->ComputeKeyedCallInitialize( + arity, NOT_IN_LOOP); CallCode(ic, RelocInfo::CODE_TARGET, instr); __ movq(rsi, Operand(rbp, StandardFrameConstants::kContextOffset)); } @@ -2638,9 +3090,11 @@ void LCodeGen::DoCallNamed(LCallNamed* instr) { ASSERT(ToRegister(instr->result()).is(rax)); int arity = instr->arity(); - Handle<Code> ic = StubCache::ComputeCallInitialize(arity, NOT_IN_LOOP); + RelocInfo::Mode mode = RelocInfo::CODE_TARGET; + Handle<Code> ic = + isolate()->stub_cache()->ComputeCallInitialize(arity, NOT_IN_LOOP, mode); __ Move(rcx, instr->name()); - CallCode(ic, RelocInfo::CODE_TARGET, instr); + CallCode(ic, mode, instr); __ movq(rsi, Operand(rbp, StandardFrameConstants::kContextOffset)); } @@ -2649,7 +3103,7 @@ void LCodeGen::DoCallFunction(LCallFunction* instr) { ASSERT(ToRegister(instr->result()).is(rax)); int arity = instr->arity(); - CallFunctionStub stub(arity, NOT_IN_LOOP, RECEIVER_MIGHT_BE_VALUE); + CallFunctionStub stub(arity, NOT_IN_LOOP, RECEIVER_MIGHT_BE_IMPLICIT); CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); __ movq(rsi, Operand(rbp, StandardFrameConstants::kContextOffset)); __ Drop(1); @@ -2659,9 +3113,11 @@ void LCodeGen::DoCallFunction(LCallFunction* instr) { void LCodeGen::DoCallGlobal(LCallGlobal* instr) { ASSERT(ToRegister(instr->result()).is(rax)); int arity = instr->arity(); - Handle<Code> ic = StubCache::ComputeCallInitialize(arity, NOT_IN_LOOP); + RelocInfo::Mode mode = RelocInfo::CODE_TARGET_CONTEXT; + Handle<Code> ic = + isolate()->stub_cache()->ComputeCallInitialize(arity, NOT_IN_LOOP, mode); __ Move(rcx, instr->name()); - CallCode(ic, RelocInfo::CODE_TARGET_CONTEXT, instr); + CallCode(ic, mode, instr); __ movq(rsi, Operand(rbp, StandardFrameConstants::kContextOffset)); } @@ -2669,7 +3125,7 @@ void LCodeGen::DoCallGlobal(LCallGlobal* instr) { void LCodeGen::DoCallKnownGlobal(LCallKnownGlobal* instr) { ASSERT(ToRegister(instr->result()).is(rax)); __ Move(rdi, instr->target()); - CallKnownFunction(instr->target(), instr->arity(), instr); + CallKnownFunction(instr->target(), instr->arity(), instr, CALL_AS_FUNCTION); } @@ -2677,7 +3133,7 @@ void LCodeGen::DoCallNew(LCallNew* instr) { ASSERT(ToRegister(instr->InputAt(0)).is(rdi)); ASSERT(ToRegister(instr->result()).is(rax)); - Handle<Code> builtin(Builtins::builtin(Builtins::JSConstructCall)); + Handle<Code> builtin = isolate()->builtins()->JSConstructCall(); __ Set(rax, instr->arity()); CallCode(builtin, RelocInfo::CONSTRUCT_CALL, instr); } @@ -2723,28 +3179,50 @@ void LCodeGen::DoStoreNamedGeneric(LStoreNamedGeneric* instr) { ASSERT(ToRegister(instr->value()).is(rax)); __ Move(rcx, instr->hydrogen()->name()); - Handle<Code> ic(Builtins::builtin( - info_->is_strict() ? Builtins::StoreIC_Initialize_Strict - : Builtins::StoreIC_Initialize)); + Handle<Code> ic = instr->strict_mode() + ? isolate()->builtins()->StoreIC_Initialize_Strict() + : isolate()->builtins()->StoreIC_Initialize(); CallCode(ic, RelocInfo::CODE_TARGET, instr); } -void LCodeGen::DoStorePixelArrayElement(LStorePixelArrayElement* instr) { - Register external_pointer = ToRegister(instr->external_pointer()); - Register key = ToRegister(instr->key()); - Register value = ToRegister(instr->value()); - - { // Clamp the value to [0..255]. - NearLabel done; - __ testl(value, Immediate(0xFFFFFF00)); - __ j(zero, &done); - __ setcc(negative, value); // 1 if negative, 0 if positive. - __ decb(value); // 0 if negative, 255 if positive. - __ bind(&done); +void LCodeGen::DoStoreKeyedSpecializedArrayElement( + LStoreKeyedSpecializedArrayElement* instr) { + JSObject::ElementsKind elements_kind = instr->elements_kind(); + Operand operand(BuildExternalArrayOperand(instr->external_pointer(), + instr->key(), elements_kind)); + if (elements_kind == JSObject::EXTERNAL_FLOAT_ELEMENTS) { + XMMRegister value(ToDoubleRegister(instr->value())); + __ cvtsd2ss(value, value); + __ movss(operand, value); + } else if (elements_kind == JSObject::EXTERNAL_DOUBLE_ELEMENTS) { + __ movsd(operand, ToDoubleRegister(instr->value())); + } else { + Register value(ToRegister(instr->value())); + switch (elements_kind) { + case JSObject::EXTERNAL_PIXEL_ELEMENTS: + case JSObject::EXTERNAL_BYTE_ELEMENTS: + case JSObject::EXTERNAL_UNSIGNED_BYTE_ELEMENTS: + __ movb(operand, value); + break; + case JSObject::EXTERNAL_SHORT_ELEMENTS: + case JSObject::EXTERNAL_UNSIGNED_SHORT_ELEMENTS: + __ movw(operand, value); + break; + case JSObject::EXTERNAL_INT_ELEMENTS: + case JSObject::EXTERNAL_UNSIGNED_INT_ELEMENTS: + __ movl(operand, value); + break; + case JSObject::EXTERNAL_FLOAT_ELEMENTS: + case JSObject::EXTERNAL_DOUBLE_ELEMENTS: + case JSObject::FAST_ELEMENTS: + case JSObject::FAST_DOUBLE_ELEMENTS: + case JSObject::DICTIONARY_ELEMENTS: + case JSObject::NON_STRICT_ARGUMENTS_ELEMENTS: + UNREACHABLE(); + break; + } } - - __ movb(Operand(external_pointer, key, times_1, 0), value); } @@ -2794,13 +3272,21 @@ void LCodeGen::DoStoreKeyedGeneric(LStoreKeyedGeneric* instr) { ASSERT(ToRegister(instr->key()).is(rcx)); ASSERT(ToRegister(instr->value()).is(rax)); - Handle<Code> ic(Builtins::builtin( - info_->is_strict() ? Builtins::KeyedStoreIC_Initialize_Strict - : Builtins::KeyedStoreIC_Initialize)); + Handle<Code> ic = instr->strict_mode() + ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict() + : isolate()->builtins()->KeyedStoreIC_Initialize(); CallCode(ic, RelocInfo::CODE_TARGET, instr); } +void LCodeGen::DoStringAdd(LStringAdd* instr) { + EmitPushTaggedOperand(instr->left()); + EmitPushTaggedOperand(instr->right()); + StringAddStub stub(NO_STRING_CHECK_IN_STUB); + CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); +} + + void LCodeGen::DoStringCharCodeAt(LStringCharCodeAt* instr) { class DeferredStringCharCodeAt: public LDeferredCode { public: @@ -2835,7 +3321,7 @@ void LCodeGen::DoStringCharCodeAt(LStringCharCodeAt* instr) { DeferredStringCharCodeAt* deferred = new DeferredStringCharCodeAt(this, instr); - NearLabel flat_string, ascii_string, done; + Label flat_string, ascii_string, done; // Fetch the instance type of the receiver into result register. __ movq(result, FieldOperand(string, HeapObject::kMapOffset)); @@ -2844,7 +3330,7 @@ void LCodeGen::DoStringCharCodeAt(LStringCharCodeAt* instr) { // We need special handling for non-sequential strings. STATIC_ASSERT(kSeqStringTag == 0); __ testb(result, Immediate(kStringRepresentationMask)); - __ j(zero, &flat_string); + __ j(zero, &flat_string, Label::kNear); // Handle cons strings and go to deferred code for the rest. __ testb(result, Immediate(kIsConsStringMask)); @@ -2871,7 +3357,7 @@ void LCodeGen::DoStringCharCodeAt(LStringCharCodeAt* instr) { __ bind(&flat_string); STATIC_ASSERT(kAsciiStringTag != 0); __ testb(result, Immediate(kStringEncodingMask)); - __ j(not_zero, &ascii_string); + __ j(not_zero, &ascii_string, Label::kNear); // Two-byte string. // Load the two-byte character code into the result register. @@ -2887,7 +3373,7 @@ void LCodeGen::DoStringCharCodeAt(LStringCharCodeAt* instr) { times_2, SeqTwoByteString::kHeaderSize)); } - __ jmp(&done); + __ jmp(&done, Label::kNear); // ASCII string. // Load the byte into the result register. @@ -2937,6 +3423,53 @@ void LCodeGen::DoDeferredStringCharCodeAt(LStringCharCodeAt* instr) { } +void LCodeGen::DoStringCharFromCode(LStringCharFromCode* instr) { + class DeferredStringCharFromCode: public LDeferredCode { + public: + DeferredStringCharFromCode(LCodeGen* codegen, LStringCharFromCode* instr) + : LDeferredCode(codegen), instr_(instr) { } + virtual void Generate() { codegen()->DoDeferredStringCharFromCode(instr_); } + private: + LStringCharFromCode* instr_; + }; + + DeferredStringCharFromCode* deferred = + new DeferredStringCharFromCode(this, instr); + + ASSERT(instr->hydrogen()->value()->representation().IsInteger32()); + Register char_code = ToRegister(instr->char_code()); + Register result = ToRegister(instr->result()); + ASSERT(!char_code.is(result)); + + __ cmpl(char_code, Immediate(String::kMaxAsciiCharCode)); + __ j(above, deferred->entry()); + __ LoadRoot(result, Heap::kSingleCharacterStringCacheRootIndex); + __ movq(result, FieldOperand(result, + char_code, times_pointer_size, + FixedArray::kHeaderSize)); + __ CompareRoot(result, Heap::kUndefinedValueRootIndex); + __ j(equal, deferred->entry()); + __ bind(deferred->exit()); +} + + +void LCodeGen::DoDeferredStringCharFromCode(LStringCharFromCode* instr) { + Register char_code = ToRegister(instr->char_code()); + Register result = ToRegister(instr->result()); + + // TODO(3095996): Get rid of this. For now, we need to make the + // result register contain a valid pointer because it is already + // contained in the register pointer map. + __ Set(result, 0); + + PushSafepointRegistersScope scope(this); + __ Integer32ToSmi(char_code, char_code); + __ push(char_code); + CallRuntimeFromDeferred(Runtime::kCharFromCode, 1, instr); + __ StoreToSafepointRegisterSlot(result, rax); +} + + void LCodeGen::DoStringLength(LStringLength* instr) { Register string = ToRegister(instr->string()); Register result = ToRegister(instr->result()); @@ -3029,29 +3562,35 @@ void LCodeGen::DoSmiUntag(LSmiUntag* instr) { void LCodeGen::EmitNumberUntagD(Register input_reg, XMMRegister result_reg, + bool deoptimize_on_undefined, LEnvironment* env) { - NearLabel load_smi, heap_number, done; + Label load_smi, done; // Smi check. - __ JumpIfSmi(input_reg, &load_smi); + __ JumpIfSmi(input_reg, &load_smi, Label::kNear); // Heap number map check. __ CompareRoot(FieldOperand(input_reg, HeapObject::kMapOffset), Heap::kHeapNumberMapRootIndex); - __ j(equal, &heap_number); + if (deoptimize_on_undefined) { + DeoptimizeIf(not_equal, env); + } else { + Label heap_number; + __ j(equal, &heap_number, Label::kNear); - __ CompareRoot(input_reg, Heap::kUndefinedValueRootIndex); - DeoptimizeIf(not_equal, env); + __ CompareRoot(input_reg, Heap::kUndefinedValueRootIndex); + DeoptimizeIf(not_equal, env); - // Convert undefined to NaN. Compute NaN as 0/0. - __ xorpd(result_reg, result_reg); - __ divsd(result_reg, result_reg); - __ jmp(&done); + // Convert undefined to NaN. Compute NaN as 0/0. + __ xorps(result_reg, result_reg); + __ divsd(result_reg, result_reg); + __ jmp(&done, Label::kNear); + __ bind(&heap_number); + } // Heap number to XMM conversion. - __ bind(&heap_number); __ movsd(result_reg, FieldOperand(input_reg, HeapNumber::kValueOffset)); - __ jmp(&done); + __ jmp(&done, Label::kNear); // Smi to XMM conversion __ bind(&load_smi); @@ -3072,7 +3611,7 @@ class DeferredTaggedToI: public LDeferredCode { void LCodeGen::DoDeferredTaggedToI(LTaggedToI* instr) { - NearLabel done, heap_number; + Label done, heap_number; Register input_reg = ToRegister(instr->InputAt(0)); // Heap number map check. @@ -3080,20 +3619,20 @@ void LCodeGen::DoDeferredTaggedToI(LTaggedToI* instr) { Heap::kHeapNumberMapRootIndex); if (instr->truncating()) { - __ j(equal, &heap_number); + __ j(equal, &heap_number, Label::kNear); // Check for undefined. Undefined is converted to zero for truncating // conversions. __ CompareRoot(input_reg, Heap::kUndefinedValueRootIndex); DeoptimizeIf(not_equal, instr->environment()); - __ movl(input_reg, Immediate(0)); - __ jmp(&done); + __ Set(input_reg, 0); + __ jmp(&done, Label::kNear); __ bind(&heap_number); __ movsd(xmm0, FieldOperand(input_reg, HeapNumber::kValueOffset)); __ cvttsd2siq(input_reg, xmm0); __ Set(kScratchRegister, V8_UINT64_C(0x8000000000000000)); - __ cmpl(input_reg, kScratchRegister); + __ cmpq(input_reg, kScratchRegister); DeoptimizeIf(equal, instr->environment()); } else { // Deoptimize if we don't have a heap number. @@ -3140,7 +3679,9 @@ void LCodeGen::DoNumberUntagD(LNumberUntagD* instr) { Register input_reg = ToRegister(input); XMMRegister result_reg = ToDoubleRegister(result); - EmitNumberUntagD(input_reg, result_reg, instr->environment()); + EmitNumberUntagD(input_reg, result_reg, + instr->hydrogen()->deoptimize_on_undefined(), + instr->environment()); } @@ -3158,8 +3699,8 @@ void LCodeGen::DoDoubleToI(LDoubleToI* instr) { // the JS bitwise operations. __ cvttsd2siq(result_reg, input_reg); __ movq(kScratchRegister, V8_INT64_C(0x8000000000000000), RelocInfo::NONE); - __ cmpl(result_reg, kScratchRegister); - DeoptimizeIf(equal, instr->environment()); + __ cmpq(result_reg, kScratchRegister); + DeoptimizeIf(equal, instr->environment()); } else { __ cvttsd2si(result_reg, input_reg); __ cvtlsi2sd(xmm0, result_reg); @@ -3167,11 +3708,11 @@ void LCodeGen::DoDoubleToI(LDoubleToI* instr) { DeoptimizeIf(not_equal, instr->environment()); DeoptimizeIf(parity_even, instr->environment()); // NaN. if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { - NearLabel done; + Label done; // The integer converted back is equal to the original. We // only have to test if we got -0 as an input. __ testl(result_reg, result_reg); - __ j(not_zero, &done); + __ j(not_zero, &done, Label::kNear); __ movmskpd(result_reg, input_reg); // Bit 0 contains the sign of the double in input_reg. // If input was positive, we are ok and return 0, otherwise @@ -3186,41 +3727,59 @@ void LCodeGen::DoDoubleToI(LDoubleToI* instr) { void LCodeGen::DoCheckSmi(LCheckSmi* instr) { LOperand* input = instr->InputAt(0); - ASSERT(input->IsRegister()); Condition cc = masm()->CheckSmi(ToRegister(input)); - if (instr->condition() != equal) { - cc = NegateCondition(cc); - } + DeoptimizeIf(NegateCondition(cc), instr->environment()); +} + + +void LCodeGen::DoCheckNonSmi(LCheckNonSmi* instr) { + LOperand* input = instr->InputAt(0); + Condition cc = masm()->CheckSmi(ToRegister(input)); DeoptimizeIf(cc, instr->environment()); } void LCodeGen::DoCheckInstanceType(LCheckInstanceType* instr) { Register input = ToRegister(instr->InputAt(0)); - InstanceType first = instr->hydrogen()->first(); - InstanceType last = instr->hydrogen()->last(); __ movq(kScratchRegister, FieldOperand(input, HeapObject::kMapOffset)); - // If there is only one type in the interval check for equality. - if (first == last) { + if (instr->hydrogen()->is_interval_check()) { + InstanceType first; + InstanceType last; + instr->hydrogen()->GetCheckInterval(&first, &last); + __ cmpb(FieldOperand(kScratchRegister, Map::kInstanceTypeOffset), Immediate(static_cast<int8_t>(first))); - DeoptimizeIf(not_equal, instr->environment()); - } else if (first == FIRST_STRING_TYPE && last == LAST_STRING_TYPE) { - // String has a dedicated bit in instance type. - __ testb(FieldOperand(kScratchRegister, Map::kInstanceTypeOffset), - Immediate(kIsNotStringMask)); - DeoptimizeIf(not_zero, instr->environment()); + + // If there is only one type in the interval check for equality. + if (first == last) { + DeoptimizeIf(not_equal, instr->environment()); + } else { + DeoptimizeIf(below, instr->environment()); + // Omit check for the last type. + if (last != LAST_TYPE) { + __ cmpb(FieldOperand(kScratchRegister, Map::kInstanceTypeOffset), + Immediate(static_cast<int8_t>(last))); + DeoptimizeIf(above, instr->environment()); + } + } } else { - __ cmpb(FieldOperand(kScratchRegister, Map::kInstanceTypeOffset), - Immediate(static_cast<int8_t>(first))); - DeoptimizeIf(below, instr->environment()); - // Omit check for the last type. - if (last != LAST_TYPE) { - __ cmpb(FieldOperand(kScratchRegister, Map::kInstanceTypeOffset), - Immediate(static_cast<int8_t>(last))); - DeoptimizeIf(above, instr->environment()); + uint8_t mask; + uint8_t tag; + instr->hydrogen()->GetCheckMaskAndTag(&mask, &tag); + + if (IsPowerOf2(mask)) { + ASSERT(tag == 0 || IsPowerOf2(tag)); + __ testb(FieldOperand(kScratchRegister, Map::kInstanceTypeOffset), + Immediate(mask)); + DeoptimizeIf(tag == 0 ? not_zero : zero, instr->environment()); + } else { + __ movzxbl(kScratchRegister, + FieldOperand(kScratchRegister, Map::kInstanceTypeOffset)); + __ andb(kScratchRegister, Immediate(mask)); + __ cmpb(kScratchRegister, Immediate(tag)); + DeoptimizeIf(not_equal, instr->environment()); } } } @@ -3244,10 +3803,61 @@ void LCodeGen::DoCheckMap(LCheckMap* instr) { } +void LCodeGen::DoClampDToUint8(LClampDToUint8* instr) { + XMMRegister value_reg = ToDoubleRegister(instr->unclamped()); + Register result_reg = ToRegister(instr->result()); + Register temp_reg = ToRegister(instr->TempAt(0)); + __ ClampDoubleToUint8(value_reg, xmm0, result_reg, temp_reg); +} + + +void LCodeGen::DoClampIToUint8(LClampIToUint8* instr) { + ASSERT(instr->unclamped()->Equals(instr->result())); + Register value_reg = ToRegister(instr->result()); + __ ClampUint8(value_reg); +} + + +void LCodeGen::DoClampTToUint8(LClampTToUint8* instr) { + ASSERT(instr->unclamped()->Equals(instr->result())); + Register input_reg = ToRegister(instr->unclamped()); + Register temp_reg = ToRegister(instr->TempAt(0)); + XMMRegister temp_xmm_reg = ToDoubleRegister(instr->TempAt(1)); + Label is_smi, done, heap_number; + + __ JumpIfSmi(input_reg, &is_smi); + + // Check for heap number + __ Cmp(FieldOperand(input_reg, HeapObject::kMapOffset), + factory()->heap_number_map()); + __ j(equal, &heap_number, Label::kNear); + + // Check for undefined. Undefined is converted to zero for clamping + // conversions. + __ Cmp(input_reg, factory()->undefined_value()); + DeoptimizeIf(not_equal, instr->environment()); + __ movq(input_reg, Immediate(0)); + __ jmp(&done, Label::kNear); + + // Heap number + __ bind(&heap_number); + __ movsd(xmm0, FieldOperand(input_reg, HeapNumber::kValueOffset)); + __ ClampDoubleToUint8(xmm0, temp_xmm_reg, input_reg, temp_reg); + __ jmp(&done, Label::kNear); + + // smi + __ bind(&is_smi); + __ SmiToInteger32(input_reg, input_reg); + __ ClampUint8(input_reg); + + __ bind(&done); +} + + void LCodeGen::LoadHeapObject(Register result, Handle<HeapObject> object) { - if (Heap::InNewSpace(*object)) { + if (heap()->InNewSpace(*object)) { Handle<JSGlobalPropertyCell> cell = - Factory::NewJSGlobalPropertyCell(object); + factory()->NewJSGlobalPropertyCell(object); __ movq(result, cell, RelocInfo::GLOBAL_PROPERTY_CELL); __ movq(result, Operand(result, 0)); } else { @@ -3328,8 +3938,15 @@ void LCodeGen::DoObjectLiteral(LObjectLiteral* instr) { } +void LCodeGen::DoToFastProperties(LToFastProperties* instr) { + ASSERT(ToRegister(instr->InputAt(0)).is(rax)); + __ push(rax); + CallRuntime(Runtime::kToFastProperties, 1, instr); +} + + void LCodeGen::DoRegExpLiteral(LRegExpLiteral* instr) { - NearLabel materialized; + Label materialized; // Registers will be used as follows: // rdi = JS function. // rcx = literals array. @@ -3341,7 +3958,7 @@ void LCodeGen::DoRegExpLiteral(LRegExpLiteral* instr) { instr->hydrogen()->literal_index() * kPointerSize; __ movq(rbx, FieldOperand(rcx, literal_offset)); __ CompareRoot(rbx, Heap::kUndefinedValueRootIndex); - __ j(not_equal, &materialized); + __ j(not_equal, &materialized, Label::kNear); // Create regexp literal using runtime function // Result will be in rax. @@ -3385,14 +4002,17 @@ void LCodeGen::DoFunctionLiteral(LFunctionLiteral* instr) { // space for nested functions that don't need literals cloning. Handle<SharedFunctionInfo> shared_info = instr->shared_info(); bool pretenure = instr->hydrogen()->pretenure(); - if (shared_info->num_literals() == 0 && !pretenure) { - FastNewClosureStub stub; + if (!pretenure && shared_info->num_literals() == 0) { + FastNewClosureStub stub( + shared_info->strict_mode() ? kStrictMode : kNonStrictMode); __ Push(shared_info); CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); } else { __ push(rsi); __ Push(shared_info); - __ Push(pretenure ? Factory::true_value() : Factory::false_value()); + __ PushRoot(pretenure ? + Heap::kTrueValueRootIndex : + Heap::kFalseValueRootIndex); CallRuntime(Runtime::kNewClosure, 3, instr); } } @@ -3400,14 +4020,7 @@ void LCodeGen::DoFunctionLiteral(LFunctionLiteral* instr) { void LCodeGen::DoTypeof(LTypeof* instr) { LOperand* input = instr->InputAt(0); - if (input->IsConstantOperand()) { - __ Push(ToHandle(LConstantOperand::cast(input))); - } else if (input->IsRegister()) { - __ push(ToRegister(input)); - } else { - ASSERT(input->IsStackSlot()); - __ push(ToOperand(input)); - } + EmitPushTaggedOperand(input); CallRuntime(Runtime::kTypeof, 1, instr); } @@ -3417,7 +4030,7 @@ void LCodeGen::DoTypeofIs(LTypeofIs* instr) { Register result = ToRegister(instr->result()); Label true_label; Label false_label; - NearLabel done; + Label done; Condition final_branch_condition = EmitTypeofIs(&true_label, &false_label, @@ -3426,7 +4039,7 @@ void LCodeGen::DoTypeofIs(LTypeofIs* instr) { __ j(final_branch_condition, &true_label); __ bind(&false_label); __ LoadRoot(result, Heap::kFalseValueRootIndex); - __ jmp(&done); + __ jmp(&done, Label::kNear); __ bind(&true_label); __ LoadRoot(result, Heap::kTrueValueRootIndex); @@ -3435,19 +4048,14 @@ void LCodeGen::DoTypeofIs(LTypeofIs* instr) { } -void LCodeGen::EmitPushConstantOperand(LOperand* operand) { - ASSERT(operand->IsConstantOperand()); - LConstantOperand* const_op = LConstantOperand::cast(operand); - Handle<Object> literal = chunk_->LookupLiteral(const_op); - Representation r = chunk_->LookupLiteralRepresentation(const_op); - if (r.IsInteger32()) { - ASSERT(literal->IsNumber()); - __ push(Immediate(static_cast<int32_t>(literal->Number()))); - } else if (r.IsDouble()) { - Abort("unsupported double immediate"); +void LCodeGen::EmitPushTaggedOperand(LOperand* operand) { + ASSERT(!operand->IsDoubleRegister()); + if (operand->IsConstantOperand()) { + __ Push(ToHandle(LConstantOperand::cast(operand))); + } else if (operand->IsRegister()) { + __ push(ToRegister(operand)); } else { - ASSERT(r.IsTagged()); - __ Push(literal); + __ push(ToOperand(operand)); } } @@ -3473,28 +4081,28 @@ Condition LCodeGen::EmitTypeofIs(Label* true_label, Register input, Handle<String> type_name) { Condition final_branch_condition = no_condition; - if (type_name->Equals(Heap::number_symbol())) { + if (type_name->Equals(heap()->number_symbol())) { __ JumpIfSmi(input, true_label); - __ Cmp(FieldOperand(input, HeapObject::kMapOffset), - Factory::heap_number_map()); + __ CompareRoot(FieldOperand(input, HeapObject::kMapOffset), + Heap::kHeapNumberMapRootIndex); + final_branch_condition = equal; - } else if (type_name->Equals(Heap::string_symbol())) { + } else if (type_name->Equals(heap()->string_symbol())) { __ JumpIfSmi(input, false_label); - __ movq(input, FieldOperand(input, HeapObject::kMapOffset)); + __ CmpObjectType(input, FIRST_NONSTRING_TYPE, input); + __ j(above_equal, false_label); __ testb(FieldOperand(input, Map::kBitFieldOffset), Immediate(1 << Map::kIsUndetectable)); - __ j(not_zero, false_label); - __ CmpInstanceType(input, FIRST_NONSTRING_TYPE); - final_branch_condition = below; + final_branch_condition = zero; - } else if (type_name->Equals(Heap::boolean_symbol())) { + } else if (type_name->Equals(heap()->boolean_symbol())) { __ CompareRoot(input, Heap::kTrueValueRootIndex); __ j(equal, true_label); __ CompareRoot(input, Heap::kFalseValueRootIndex); final_branch_condition = equal; - } else if (type_name->Equals(Heap::undefined_symbol())) { + } else if (type_name->Equals(heap()->undefined_symbol())) { __ CompareRoot(input, Heap::kUndefinedValueRootIndex); __ j(equal, true_label); __ JumpIfSmi(input, false_label); @@ -3504,24 +4112,23 @@ Condition LCodeGen::EmitTypeofIs(Label* true_label, Immediate(1 << Map::kIsUndetectable)); final_branch_condition = not_zero; - } else if (type_name->Equals(Heap::function_symbol())) { + } else if (type_name->Equals(heap()->function_symbol())) { __ JumpIfSmi(input, false_label); - __ CmpObjectType(input, FIRST_FUNCTION_CLASS_TYPE, input); + __ CmpObjectType(input, FIRST_CALLABLE_SPEC_OBJECT_TYPE, input); final_branch_condition = above_equal; - } else if (type_name->Equals(Heap::object_symbol())) { + } else if (type_name->Equals(heap()->object_symbol())) { __ JumpIfSmi(input, false_label); - __ Cmp(input, Factory::null_value()); + __ CompareRoot(input, Heap::kNullValueRootIndex); __ j(equal, true_label); + __ CmpObjectType(input, FIRST_NONCALLABLE_SPEC_OBJECT_TYPE, input); + __ j(below, false_label); + __ CmpInstanceType(input, LAST_NONCALLABLE_SPEC_OBJECT_TYPE); + __ j(above, false_label); // Check for undetectable objects => false. __ testb(FieldOperand(input, Map::kBitFieldOffset), Immediate(1 << Map::kIsUndetectable)); - __ j(not_zero, false_label); - // Check for JS objects that are not RegExp or Function => true. - __ CmpInstanceType(input, FIRST_JS_OBJECT_TYPE); - __ j(below, false_label); - __ CmpInstanceType(input, FIRST_FUNCTION_CLASS_TYPE); - final_branch_condition = below_equal; + final_branch_condition = zero; } else { final_branch_condition = never; @@ -3534,15 +4141,14 @@ Condition LCodeGen::EmitTypeofIs(Label* true_label, void LCodeGen::DoIsConstructCall(LIsConstructCall* instr) { Register result = ToRegister(instr->result()); - NearLabel true_label; - NearLabel false_label; - NearLabel done; + Label true_label; + Label done; EmitIsConstructCall(result); - __ j(equal, &true_label); + __ j(equal, &true_label, Label::kNear); __ LoadRoot(result, Heap::kFalseValueRootIndex); - __ jmp(&done); + __ jmp(&done, Label::kNear); __ bind(&true_label); __ LoadRoot(result, Heap::kTrueValueRootIndex); @@ -3567,16 +4173,16 @@ void LCodeGen::EmitIsConstructCall(Register temp) { __ movq(temp, Operand(rbp, StandardFrameConstants::kCallerFPOffset)); // Skip the arguments adaptor frame if it exists. - NearLabel check_frame_marker; - __ SmiCompare(Operand(temp, StandardFrameConstants::kContextOffset), - Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)); - __ j(not_equal, &check_frame_marker); + Label check_frame_marker; + __ Cmp(Operand(temp, StandardFrameConstants::kContextOffset), + Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)); + __ j(not_equal, &check_frame_marker, Label::kNear); __ movq(temp, Operand(rax, StandardFrameConstants::kCallerFPOffset)); // Check the marker in the calling frame. __ bind(&check_frame_marker); - __ SmiCompare(Operand(temp, StandardFrameConstants::kMarkerOffset), - Smi::FromInt(StackFrame::CONSTRUCT)); + __ Cmp(Operand(temp, StandardFrameConstants::kMarkerOffset), + Smi::FromInt(StackFrame::CONSTRUCT)); } @@ -3594,20 +4200,8 @@ void LCodeGen::DoDeoptimize(LDeoptimize* instr) { void LCodeGen::DoDeleteProperty(LDeleteProperty* instr) { LOperand* obj = instr->object(); LOperand* key = instr->key(); - // Push object. - if (obj->IsRegister()) { - __ push(ToRegister(obj)); - } else { - __ push(ToOperand(obj)); - } - // Push key. - if (key->IsConstantOperand()) { - EmitPushConstantOperand(key); - } else if (key->IsRegister()) { - __ push(ToRegister(key)); - } else { - __ push(ToOperand(key)); - } + EmitPushTaggedOperand(obj); + EmitPushTaggedOperand(key); ASSERT(instr->HasPointerMap() && instr->HasDeoptimizationEnvironment()); LPointerMap* pointers = instr->pointer_map(); LEnvironment* env = instr->deoptimization_environment(); @@ -3620,19 +4214,64 @@ void LCodeGen::DoDeleteProperty(LDeleteProperty* instr) { pointers, env->deoptimization_index()); __ Push(Smi::FromInt(strict_mode_flag())); - __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION, &safepoint_generator); + __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION, safepoint_generator); +} + + +void LCodeGen::DoIn(LIn* instr) { + LOperand* obj = instr->object(); + LOperand* key = instr->key(); + EmitPushTaggedOperand(key); + EmitPushTaggedOperand(obj); + ASSERT(instr->HasPointerMap() && instr->HasDeoptimizationEnvironment()); + LPointerMap* pointers = instr->pointer_map(); + LEnvironment* env = instr->deoptimization_environment(); + RecordPosition(pointers->position()); + RegisterEnvironmentForDeoptimization(env); + // Create safepoint generator that will also ensure enough space in the + // reloc info for patching in deoptimization (since this is invoking a + // builtin) + SafepointGenerator safepoint_generator(this, + pointers, + env->deoptimization_index()); + __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION, safepoint_generator); +} + + +void LCodeGen::DoDeferredStackCheck(LStackCheck* instr) { + PushSafepointRegistersScope scope(this); + CallRuntimeFromDeferred(Runtime::kStackGuard, 0, instr); } void LCodeGen::DoStackCheck(LStackCheck* instr) { - // Perform stack overflow check. - NearLabel done; - __ CompareRoot(rsp, Heap::kStackLimitRootIndex); - __ j(above_equal, &done); + class DeferredStackCheck: public LDeferredCode { + public: + DeferredStackCheck(LCodeGen* codegen, LStackCheck* instr) + : LDeferredCode(codegen), instr_(instr) { } + virtual void Generate() { codegen()->DoDeferredStackCheck(instr_); } + private: + LStackCheck* instr_; + }; - StackCheckStub stub; - CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); - __ bind(&done); + if (instr->hydrogen()->is_function_entry()) { + // Perform stack overflow check. + Label done; + __ CompareRoot(rsp, Heap::kStackLimitRootIndex); + __ j(above_equal, &done, Label::kNear); + StackCheckStub stub; + CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); + __ bind(&done); + } else { + ASSERT(instr->hydrogen()->is_backwards_branch()); + // Perform stack overflow check if this goto needs it before jumping. + DeferredStackCheck* deferred_stack_check = + new DeferredStackCheck(this, instr); + __ CompareRoot(rsp, Heap::kStackLimitRootIndex); + __ j(below, deferred_stack_check->entry()); + __ bind(instr->done_label()); + deferred_stack_check->SetExit(instr->done_label()); + } } |