OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/compiler/code-generator.h" | 5 #include "src/compiler/code-generator.h" |
6 | 6 |
7 #include "src/ast/scopes.h" | 7 #include "src/ast/scopes.h" |
8 #include "src/compiler/code-generator-impl.h" | 8 #include "src/compiler/code-generator-impl.h" |
9 #include "src/compiler/gap-resolver.h" | 9 #include "src/compiler/gap-resolver.h" |
10 #include "src/compiler/node-matchers.h" | 10 #include "src/compiler/node-matchers.h" |
(...skipping 608 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
619 __ PrepareCallCFunction(1); \ | 619 __ PrepareCallCFunction(1); \ |
620 __ CallCFunction(ExternalReference::ieee754_##name##_function(isolate()), \ | 620 __ CallCFunction(ExternalReference::ieee754_##name##_function(isolate()), \ |
621 1); \ | 621 1); \ |
622 } while (false) | 622 } while (false) |
623 | 623 |
624 void CodeGenerator::AssembleDeconstructFrame() { | 624 void CodeGenerator::AssembleDeconstructFrame() { |
625 __ movq(rsp, rbp); | 625 __ movq(rsp, rbp); |
626 __ popq(rbp); | 626 __ popq(rbp); |
627 } | 627 } |
628 | 628 |
629 void CodeGenerator::AssembleDeconstructActivationRecord(int stack_param_delta) { | 629 void CodeGenerator::AssemblePrepareTailCall() { |
630 int sp_slot_delta = TailCallFrameStackSlotDelta(stack_param_delta); | |
631 if (sp_slot_delta > 0) { | |
632 __ addq(rsp, Immediate(sp_slot_delta * kPointerSize)); | |
633 } | |
634 frame_access_state()->SetFrameAccessToDefault(); | |
635 } | |
636 | |
637 | |
638 void CodeGenerator::AssemblePrepareTailCall(int stack_param_delta) { | |
639 int sp_slot_delta = TailCallFrameStackSlotDelta(stack_param_delta); | |
640 if (sp_slot_delta < 0) { | |
641 __ subq(rsp, Immediate(-sp_slot_delta * kPointerSize)); | |
642 frame_access_state()->IncreaseSPDelta(-sp_slot_delta); | |
643 } | |
644 if (frame_access_state()->has_frame()) { | 630 if (frame_access_state()->has_frame()) { |
645 __ movq(rbp, MemOperand(rbp, 0)); | 631 __ movq(rbp, MemOperand(rbp, 0)); |
646 } | 632 } |
647 frame_access_state()->SetFrameAccessToSP(); | 633 frame_access_state()->SetFrameAccessToSP(); |
648 } | 634 } |
649 | 635 |
650 void CodeGenerator::AssemblePopArgumentsAdaptorFrame(Register args_reg, | 636 void CodeGenerator::AssemblePopArgumentsAdaptorFrame(Register args_reg, |
651 Register scratch1, | 637 Register scratch1, |
652 Register scratch2, | 638 Register scratch2, |
653 Register scratch3) { | 639 Register scratch3) { |
(...skipping 11 matching lines...) Expand all Loading... |
665 __ SmiToInteger32( | 651 __ SmiToInteger32( |
666 caller_args_count_reg, | 652 caller_args_count_reg, |
667 Operand(rbp, ArgumentsAdaptorFrameConstants::kLengthOffset)); | 653 Operand(rbp, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
668 | 654 |
669 ParameterCount callee_args_count(args_reg); | 655 ParameterCount callee_args_count(args_reg); |
670 __ PrepareForTailCall(callee_args_count, caller_args_count_reg, scratch2, | 656 __ PrepareForTailCall(callee_args_count, caller_args_count_reg, scratch2, |
671 scratch3, ReturnAddressState::kOnStack); | 657 scratch3, ReturnAddressState::kOnStack); |
672 __ bind(&done); | 658 __ bind(&done); |
673 } | 659 } |
674 | 660 |
| 661 namespace { |
| 662 |
| 663 void AdjustStackPointerForTailCall(MacroAssembler* masm, |
| 664 FrameAccessState* state, |
| 665 int new_slot_above_sp, |
| 666 bool allow_shrinkage = true) { |
| 667 int current_sp_offset = state->GetSPToFPSlotCount() + |
| 668 StandardFrameConstants::kFixedSlotCountAboveFp; |
| 669 int stack_slot_delta = new_slot_above_sp - current_sp_offset; |
| 670 if (stack_slot_delta > 0) { |
| 671 masm->subq(rsp, Immediate(stack_slot_delta * kPointerSize)); |
| 672 state->IncreaseSPDelta(stack_slot_delta); |
| 673 } else if (allow_shrinkage && stack_slot_delta < 0) { |
| 674 masm->addq(rsp, Immediate(-stack_slot_delta * kPointerSize)); |
| 675 state->IncreaseSPDelta(stack_slot_delta); |
| 676 } |
| 677 } |
| 678 |
| 679 } // namespace |
| 680 |
| 681 void CodeGenerator::AssembleTailCallBeforeGap(Instruction* instr, |
| 682 int first_unused_stack_slot) { |
| 683 CodeGenerator::PushTypeFlags flags(kImmediatePush | kScalarPush); |
| 684 ZoneVector<MoveOperands*> pushes(zone()); |
| 685 GetPushCompatibleMoves(instr, flags, &pushes); |
| 686 |
| 687 if (!pushes.empty() && |
| 688 (LocationOperand::cast(pushes.back()->destination()).index() + 1 == |
| 689 first_unused_stack_slot)) { |
| 690 X64OperandConverter g(this, instr); |
| 691 for (auto move : pushes) { |
| 692 LocationOperand destination_location( |
| 693 LocationOperand::cast(move->destination())); |
| 694 InstructionOperand source(move->source()); |
| 695 AdjustStackPointerForTailCall(masm(), frame_access_state(), |
| 696 destination_location.index()); |
| 697 if (source.IsStackSlot()) { |
| 698 LocationOperand source_location(LocationOperand::cast(source)); |
| 699 __ Push(g.SlotToOperand(source_location.index())); |
| 700 } else if (source.IsRegister()) { |
| 701 LocationOperand source_location(LocationOperand::cast(source)); |
| 702 __ Push(source_location.GetRegister()); |
| 703 } else if (source.IsImmediate()) { |
| 704 __ Push(Immediate(ImmediateOperand::cast(source).inline_value())); |
| 705 } else { |
| 706 // Pushes of non-scalar data types is not supported. |
| 707 UNIMPLEMENTED(); |
| 708 } |
| 709 frame_access_state()->IncreaseSPDelta(1); |
| 710 move->Eliminate(); |
| 711 } |
| 712 } |
| 713 AdjustStackPointerForTailCall(masm(), frame_access_state(), |
| 714 first_unused_stack_slot, false); |
| 715 } |
| 716 |
| 717 void CodeGenerator::AssembleTailCallAfterGap(Instruction* instr, |
| 718 int first_unused_stack_slot) { |
| 719 AdjustStackPointerForTailCall(masm(), frame_access_state(), |
| 720 first_unused_stack_slot); |
| 721 } |
| 722 |
675 // Assembles an instruction after register allocation, producing machine code. | 723 // Assembles an instruction after register allocation, producing machine code. |
676 CodeGenerator::CodeGenResult CodeGenerator::AssembleArchInstruction( | 724 CodeGenerator::CodeGenResult CodeGenerator::AssembleArchInstruction( |
677 Instruction* instr) { | 725 Instruction* instr) { |
678 X64OperandConverter i(this, instr); | 726 X64OperandConverter i(this, instr); |
679 InstructionCode opcode = instr->opcode(); | 727 InstructionCode opcode = instr->opcode(); |
680 ArchOpcode arch_opcode = ArchOpcodeField::decode(opcode); | 728 ArchOpcode arch_opcode = ArchOpcodeField::decode(opcode); |
681 switch (arch_opcode) { | 729 switch (arch_opcode) { |
682 case kArchCallCodeObject: { | 730 case kArchCallCodeObject: { |
683 EnsureSpaceForLazyDeopt(); | 731 EnsureSpaceForLazyDeopt(); |
684 if (HasImmediateInput(instr, 0)) { | 732 if (HasImmediateInput(instr, 0)) { |
685 Handle<Code> code = Handle<Code>::cast(i.InputHeapObject(0)); | 733 Handle<Code> code = Handle<Code>::cast(i.InputHeapObject(0)); |
686 __ Call(code, RelocInfo::CODE_TARGET); | 734 __ Call(code, RelocInfo::CODE_TARGET); |
687 } else { | 735 } else { |
688 Register reg = i.InputRegister(0); | 736 Register reg = i.InputRegister(0); |
689 __ addp(reg, Immediate(Code::kHeaderSize - kHeapObjectTag)); | 737 __ addp(reg, Immediate(Code::kHeaderSize - kHeapObjectTag)); |
690 __ call(reg); | 738 __ call(reg); |
691 } | 739 } |
692 RecordCallPosition(instr); | 740 RecordCallPosition(instr); |
693 frame_access_state()->ClearSPDelta(); | 741 frame_access_state()->ClearSPDelta(); |
694 break; | 742 break; |
695 } | 743 } |
696 case kArchTailCallCodeObjectFromJSFunction: | 744 case kArchTailCallCodeObjectFromJSFunction: |
697 case kArchTailCallCodeObject: { | 745 case kArchTailCallCodeObject: { |
698 int stack_param_delta = i.InputInt32(instr->InputCount() - 1); | |
699 AssembleDeconstructActivationRecord(stack_param_delta); | |
700 if (arch_opcode == kArchTailCallCodeObjectFromJSFunction) { | 746 if (arch_opcode == kArchTailCallCodeObjectFromJSFunction) { |
701 AssemblePopArgumentsAdaptorFrame(kJavaScriptCallArgCountRegister, | 747 AssemblePopArgumentsAdaptorFrame(kJavaScriptCallArgCountRegister, |
702 i.TempRegister(0), i.TempRegister(1), | 748 i.TempRegister(0), i.TempRegister(1), |
703 i.TempRegister(2)); | 749 i.TempRegister(2)); |
704 } | 750 } |
705 if (HasImmediateInput(instr, 0)) { | 751 if (HasImmediateInput(instr, 0)) { |
706 Handle<Code> code = Handle<Code>::cast(i.InputHeapObject(0)); | 752 Handle<Code> code = Handle<Code>::cast(i.InputHeapObject(0)); |
707 __ jmp(code, RelocInfo::CODE_TARGET); | 753 __ jmp(code, RelocInfo::CODE_TARGET); |
708 } else { | 754 } else { |
709 Register reg = i.InputRegister(0); | 755 Register reg = i.InputRegister(0); |
710 __ addp(reg, Immediate(Code::kHeaderSize - kHeapObjectTag)); | 756 __ addp(reg, Immediate(Code::kHeaderSize - kHeapObjectTag)); |
711 __ jmp(reg); | 757 __ jmp(reg); |
712 } | 758 } |
713 frame_access_state()->ClearSPDelta(); | 759 frame_access_state()->ClearSPDelta(); |
| 760 frame_access_state()->SetFrameAccessToDefault(); |
714 break; | 761 break; |
715 } | 762 } |
716 case kArchTailCallAddress: { | 763 case kArchTailCallAddress: { |
717 int stack_param_delta = i.InputInt32(instr->InputCount() - 1); | |
718 AssembleDeconstructActivationRecord(stack_param_delta); | |
719 CHECK(!HasImmediateInput(instr, 0)); | 764 CHECK(!HasImmediateInput(instr, 0)); |
720 Register reg = i.InputRegister(0); | 765 Register reg = i.InputRegister(0); |
721 __ jmp(reg); | 766 __ jmp(reg); |
722 frame_access_state()->ClearSPDelta(); | 767 frame_access_state()->ClearSPDelta(); |
| 768 frame_access_state()->SetFrameAccessToDefault(); |
723 break; | 769 break; |
724 } | 770 } |
725 case kArchCallJSFunction: { | 771 case kArchCallJSFunction: { |
726 EnsureSpaceForLazyDeopt(); | 772 EnsureSpaceForLazyDeopt(); |
727 Register func = i.InputRegister(0); | 773 Register func = i.InputRegister(0); |
728 if (FLAG_debug_code) { | 774 if (FLAG_debug_code) { |
729 // Check the function's context matches the context argument. | 775 // Check the function's context matches the context argument. |
730 __ cmpp(rsi, FieldOperand(func, JSFunction::kContextOffset)); | 776 __ cmpp(rsi, FieldOperand(func, JSFunction::kContextOffset)); |
731 __ Assert(equal, kWrongFunctionContext); | 777 __ Assert(equal, kWrongFunctionContext); |
732 } | 778 } |
733 __ Call(FieldOperand(func, JSFunction::kCodeEntryOffset)); | 779 __ Call(FieldOperand(func, JSFunction::kCodeEntryOffset)); |
734 frame_access_state()->ClearSPDelta(); | 780 frame_access_state()->ClearSPDelta(); |
735 RecordCallPosition(instr); | 781 RecordCallPosition(instr); |
736 break; | 782 break; |
737 } | 783 } |
738 case kArchTailCallJSFunctionFromJSFunction: | 784 case kArchTailCallJSFunctionFromJSFunction: |
739 case kArchTailCallJSFunction: { | 785 case kArchTailCallJSFunction: { |
740 Register func = i.InputRegister(0); | 786 Register func = i.InputRegister(0); |
741 if (FLAG_debug_code) { | 787 if (FLAG_debug_code) { |
742 // Check the function's context matches the context argument. | 788 // Check the function's context matches the context argument. |
743 __ cmpp(rsi, FieldOperand(func, JSFunction::kContextOffset)); | 789 __ cmpp(rsi, FieldOperand(func, JSFunction::kContextOffset)); |
744 __ Assert(equal, kWrongFunctionContext); | 790 __ Assert(equal, kWrongFunctionContext); |
745 } | 791 } |
746 int stack_param_delta = i.InputInt32(instr->InputCount() - 1); | |
747 AssembleDeconstructActivationRecord(stack_param_delta); | |
748 if (arch_opcode == kArchTailCallJSFunctionFromJSFunction) { | 792 if (arch_opcode == kArchTailCallJSFunctionFromJSFunction) { |
749 AssemblePopArgumentsAdaptorFrame(kJavaScriptCallArgCountRegister, | 793 AssemblePopArgumentsAdaptorFrame(kJavaScriptCallArgCountRegister, |
750 i.TempRegister(0), i.TempRegister(1), | 794 i.TempRegister(0), i.TempRegister(1), |
751 i.TempRegister(2)); | 795 i.TempRegister(2)); |
752 } | 796 } |
753 __ jmp(FieldOperand(func, JSFunction::kCodeEntryOffset)); | 797 __ jmp(FieldOperand(func, JSFunction::kCodeEntryOffset)); |
754 frame_access_state()->ClearSPDelta(); | 798 frame_access_state()->ClearSPDelta(); |
| 799 frame_access_state()->SetFrameAccessToDefault(); |
755 break; | 800 break; |
756 } | 801 } |
757 case kArchPrepareCallCFunction: { | 802 case kArchPrepareCallCFunction: { |
758 // Frame alignment requires using FP-relative frame addressing. | 803 // Frame alignment requires using FP-relative frame addressing. |
759 frame_access_state()->SetFrameAccessToFP(); | 804 frame_access_state()->SetFrameAccessToFP(); |
760 int const num_parameters = MiscField::decode(instr->opcode()); | 805 int const num_parameters = MiscField::decode(instr->opcode()); |
761 __ PrepareCallCFunction(num_parameters); | 806 __ PrepareCallCFunction(num_parameters); |
762 break; | 807 break; |
763 } | 808 } |
764 case kArchPrepareTailCall: | 809 case kArchPrepareTailCall: |
765 AssemblePrepareTailCall(i.InputInt32(instr->InputCount() - 1)); | 810 AssemblePrepareTailCall(); |
766 break; | 811 break; |
767 case kArchCallCFunction: { | 812 case kArchCallCFunction: { |
768 int const num_parameters = MiscField::decode(instr->opcode()); | 813 int const num_parameters = MiscField::decode(instr->opcode()); |
769 if (HasImmediateInput(instr, 0)) { | 814 if (HasImmediateInput(instr, 0)) { |
770 ExternalReference ref = i.InputExternalReference(0); | 815 ExternalReference ref = i.InputExternalReference(0); |
771 __ CallCFunction(ref, num_parameters); | 816 __ CallCFunction(ref, num_parameters); |
772 } else { | 817 } else { |
773 Register func = i.InputRegister(0); | 818 Register func = i.InputRegister(0); |
774 __ CallCFunction(func, num_parameters); | 819 __ CallCFunction(func, num_parameters); |
775 } | 820 } |
(...skipping 1627 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2403 int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc; | 2448 int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc; |
2404 __ Nop(padding_size); | 2449 __ Nop(padding_size); |
2405 } | 2450 } |
2406 } | 2451 } |
2407 | 2452 |
2408 #undef __ | 2453 #undef __ |
2409 | 2454 |
2410 } // namespace compiler | 2455 } // namespace compiler |
2411 } // namespace internal | 2456 } // namespace internal |
2412 } // namespace v8 | 2457 } // namespace v8 |
OLD | NEW |