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 AdjustStackPointerForGap(MacroAssembler* masm, FrameAccessState* state, |
| 664 int new_slot_above_sp, |
| 665 bool allow_shrinkage = true) { |
| 666 int current_sp_offset = state->GetSPToFPSlotCount() + |
| 667 StandardFrameConstants::kFixedSlotCountAboveFp; |
| 668 int stack_slot_delta = new_slot_above_sp - current_sp_offset; |
| 669 if (stack_slot_delta > 0) { |
| 670 masm->subq(rsp, Immediate(stack_slot_delta * kPointerSize)); |
| 671 state->IncreaseSPDelta(stack_slot_delta); |
| 672 } else if (allow_shrinkage && stack_slot_delta < 0) { |
| 673 masm->addq(rsp, Immediate(-stack_slot_delta * kPointerSize)); |
| 674 state->IncreaseSPDelta(stack_slot_delta); |
| 675 } |
| 676 } |
| 677 |
| 678 } // namespace |
| 679 |
| 680 void CodeGenerator::AssemblePreGaps(Instruction* instr) { |
| 681 int first_unused_stack_slot; |
| 682 if (!GetSlotAboveSPAfterGap(instr, &first_unused_stack_slot)) return; |
| 683 |
| 684 GapResolver::PushTypeFlags flags(GapResolver::kImmediatePush | |
| 685 GapResolver::kScalarPush); |
| 686 ZoneVector<MoveOperands*> pushes(zone()); |
| 687 resolver()->GetPushCompatibleMoves(zone(), instr, flags, &pushes); |
| 688 |
| 689 if (!pushes.empty() && |
| 690 (LocationOperand::cast(pushes.back()->destination()).index() + 1 == |
| 691 first_unused_stack_slot)) { |
| 692 X64OperandConverter g(this, instr); |
| 693 for (auto move : pushes) { |
| 694 LocationOperand destination_location( |
| 695 LocationOperand::cast(move->destination())); |
| 696 InstructionOperand source(move->source()); |
| 697 AdjustStackPointerForGap(masm(), frame_access_state(), |
| 698 destination_location.index()); |
| 699 if (source.IsStackSlot()) { |
| 700 LocationOperand source_location(LocationOperand::cast(source)); |
| 701 __ Push(g.SlotToOperand(source_location.index())); |
| 702 } else if (source.IsRegister()) { |
| 703 LocationOperand source_location(LocationOperand::cast(source)); |
| 704 __ Push(source_location.GetRegister()); |
| 705 } else if (source.IsImmediate()) { |
| 706 __ Push(Immediate(ImmediateOperand::cast(source).inline_value())); |
| 707 } else { |
| 708 // Pushes of non-scalar data types is not supported. |
| 709 UNIMPLEMENTED(); |
| 710 } |
| 711 frame_access_state()->IncreaseSPDelta(1); |
| 712 move->Eliminate(); |
| 713 } |
| 714 } |
| 715 AdjustStackPointerForGap(masm(), frame_access_state(), |
| 716 first_unused_stack_slot, false); |
| 717 } |
| 718 |
| 719 void CodeGenerator::AssemblePostGaps(Instruction* instr) { |
| 720 int first_unused_stack_slot; |
| 721 if (!GetSlotAboveSPAfterGap(instr, &first_unused_stack_slot)) return; |
| 722 AdjustStackPointerForGap(masm(), frame_access_state(), |
| 723 first_unused_stack_slot); |
| 724 } |
| 725 |
675 // Assembles an instruction after register allocation, producing machine code. | 726 // Assembles an instruction after register allocation, producing machine code. |
676 CodeGenerator::CodeGenResult CodeGenerator::AssembleArchInstruction( | 727 CodeGenerator::CodeGenResult CodeGenerator::AssembleArchInstruction( |
677 Instruction* instr) { | 728 Instruction* instr) { |
678 X64OperandConverter i(this, instr); | 729 X64OperandConverter i(this, instr); |
679 InstructionCode opcode = instr->opcode(); | 730 InstructionCode opcode = instr->opcode(); |
680 ArchOpcode arch_opcode = ArchOpcodeField::decode(opcode); | 731 ArchOpcode arch_opcode = ArchOpcodeField::decode(opcode); |
681 switch (arch_opcode) { | 732 switch (arch_opcode) { |
682 case kArchCallCodeObject: { | 733 case kArchCallCodeObject: { |
683 EnsureSpaceForLazyDeopt(); | 734 EnsureSpaceForLazyDeopt(); |
684 if (HasImmediateInput(instr, 0)) { | 735 if (HasImmediateInput(instr, 0)) { |
685 Handle<Code> code = Handle<Code>::cast(i.InputHeapObject(0)); | 736 Handle<Code> code = Handle<Code>::cast(i.InputHeapObject(0)); |
686 __ Call(code, RelocInfo::CODE_TARGET); | 737 __ Call(code, RelocInfo::CODE_TARGET); |
687 } else { | 738 } else { |
688 Register reg = i.InputRegister(0); | 739 Register reg = i.InputRegister(0); |
689 __ addp(reg, Immediate(Code::kHeaderSize - kHeapObjectTag)); | 740 __ addp(reg, Immediate(Code::kHeaderSize - kHeapObjectTag)); |
690 __ call(reg); | 741 __ call(reg); |
691 } | 742 } |
692 RecordCallPosition(instr); | 743 RecordCallPosition(instr); |
693 frame_access_state()->ClearSPDelta(); | 744 frame_access_state()->ClearSPDelta(); |
694 break; | 745 break; |
695 } | 746 } |
696 case kArchTailCallCodeObjectFromJSFunction: | 747 case kArchTailCallCodeObjectFromJSFunction: |
697 case kArchTailCallCodeObject: { | 748 case kArchTailCallCodeObject: { |
698 int stack_param_delta = i.InputInt32(instr->InputCount() - 1); | |
699 AssembleDeconstructActivationRecord(stack_param_delta); | |
700 if (arch_opcode == kArchTailCallCodeObjectFromJSFunction) { | 749 if (arch_opcode == kArchTailCallCodeObjectFromJSFunction) { |
701 AssemblePopArgumentsAdaptorFrame(kJavaScriptCallArgCountRegister, | 750 AssemblePopArgumentsAdaptorFrame(kJavaScriptCallArgCountRegister, |
702 i.TempRegister(0), i.TempRegister(1), | 751 i.TempRegister(0), i.TempRegister(1), |
703 i.TempRegister(2)); | 752 i.TempRegister(2)); |
704 } | 753 } |
705 if (HasImmediateInput(instr, 0)) { | 754 if (HasImmediateInput(instr, 0)) { |
706 Handle<Code> code = Handle<Code>::cast(i.InputHeapObject(0)); | 755 Handle<Code> code = Handle<Code>::cast(i.InputHeapObject(0)); |
707 __ jmp(code, RelocInfo::CODE_TARGET); | 756 __ jmp(code, RelocInfo::CODE_TARGET); |
708 } else { | 757 } else { |
709 Register reg = i.InputRegister(0); | 758 Register reg = i.InputRegister(0); |
710 __ addp(reg, Immediate(Code::kHeaderSize - kHeapObjectTag)); | 759 __ addp(reg, Immediate(Code::kHeaderSize - kHeapObjectTag)); |
711 __ jmp(reg); | 760 __ jmp(reg); |
712 } | 761 } |
713 frame_access_state()->ClearSPDelta(); | 762 frame_access_state()->ClearSPDelta(); |
| 763 frame_access_state()->SetFrameAccessToDefault(); |
714 break; | 764 break; |
715 } | 765 } |
716 case kArchTailCallAddress: { | 766 case kArchTailCallAddress: { |
717 int stack_param_delta = i.InputInt32(instr->InputCount() - 1); | |
718 AssembleDeconstructActivationRecord(stack_param_delta); | |
719 CHECK(!HasImmediateInput(instr, 0)); | 767 CHECK(!HasImmediateInput(instr, 0)); |
720 Register reg = i.InputRegister(0); | 768 Register reg = i.InputRegister(0); |
721 __ jmp(reg); | 769 __ jmp(reg); |
722 frame_access_state()->ClearSPDelta(); | 770 frame_access_state()->ClearSPDelta(); |
| 771 frame_access_state()->SetFrameAccessToDefault(); |
723 break; | 772 break; |
724 } | 773 } |
725 case kArchCallJSFunction: { | 774 case kArchCallJSFunction: { |
726 EnsureSpaceForLazyDeopt(); | 775 EnsureSpaceForLazyDeopt(); |
727 Register func = i.InputRegister(0); | 776 Register func = i.InputRegister(0); |
728 if (FLAG_debug_code) { | 777 if (FLAG_debug_code) { |
729 // Check the function's context matches the context argument. | 778 // Check the function's context matches the context argument. |
730 __ cmpp(rsi, FieldOperand(func, JSFunction::kContextOffset)); | 779 __ cmpp(rsi, FieldOperand(func, JSFunction::kContextOffset)); |
731 __ Assert(equal, kWrongFunctionContext); | 780 __ Assert(equal, kWrongFunctionContext); |
732 } | 781 } |
733 __ Call(FieldOperand(func, JSFunction::kCodeEntryOffset)); | 782 __ Call(FieldOperand(func, JSFunction::kCodeEntryOffset)); |
734 frame_access_state()->ClearSPDelta(); | 783 frame_access_state()->ClearSPDelta(); |
735 RecordCallPosition(instr); | 784 RecordCallPosition(instr); |
736 break; | 785 break; |
737 } | 786 } |
738 case kArchTailCallJSFunctionFromJSFunction: | 787 case kArchTailCallJSFunctionFromJSFunction: |
739 case kArchTailCallJSFunction: { | 788 case kArchTailCallJSFunction: { |
740 Register func = i.InputRegister(0); | 789 Register func = i.InputRegister(0); |
741 if (FLAG_debug_code) { | 790 if (FLAG_debug_code) { |
742 // Check the function's context matches the context argument. | 791 // Check the function's context matches the context argument. |
743 __ cmpp(rsi, FieldOperand(func, JSFunction::kContextOffset)); | 792 __ cmpp(rsi, FieldOperand(func, JSFunction::kContextOffset)); |
744 __ Assert(equal, kWrongFunctionContext); | 793 __ Assert(equal, kWrongFunctionContext); |
745 } | 794 } |
746 int stack_param_delta = i.InputInt32(instr->InputCount() - 1); | |
747 AssembleDeconstructActivationRecord(stack_param_delta); | |
748 if (arch_opcode == kArchTailCallJSFunctionFromJSFunction) { | 795 if (arch_opcode == kArchTailCallJSFunctionFromJSFunction) { |
749 AssemblePopArgumentsAdaptorFrame(kJavaScriptCallArgCountRegister, | 796 AssemblePopArgumentsAdaptorFrame(kJavaScriptCallArgCountRegister, |
750 i.TempRegister(0), i.TempRegister(1), | 797 i.TempRegister(0), i.TempRegister(1), |
751 i.TempRegister(2)); | 798 i.TempRegister(2)); |
752 } | 799 } |
753 __ jmp(FieldOperand(func, JSFunction::kCodeEntryOffset)); | 800 __ jmp(FieldOperand(func, JSFunction::kCodeEntryOffset)); |
754 frame_access_state()->ClearSPDelta(); | 801 frame_access_state()->ClearSPDelta(); |
| 802 frame_access_state()->SetFrameAccessToDefault(); |
755 break; | 803 break; |
756 } | 804 } |
757 case kArchPrepareCallCFunction: { | 805 case kArchPrepareCallCFunction: { |
758 // Frame alignment requires using FP-relative frame addressing. | 806 // Frame alignment requires using FP-relative frame addressing. |
759 frame_access_state()->SetFrameAccessToFP(); | 807 frame_access_state()->SetFrameAccessToFP(); |
760 int const num_parameters = MiscField::decode(instr->opcode()); | 808 int const num_parameters = MiscField::decode(instr->opcode()); |
761 __ PrepareCallCFunction(num_parameters); | 809 __ PrepareCallCFunction(num_parameters); |
762 break; | 810 break; |
763 } | 811 } |
764 case kArchPrepareTailCall: | 812 case kArchPrepareTailCall: |
765 AssemblePrepareTailCall(i.InputInt32(instr->InputCount() - 1)); | 813 AssemblePrepareTailCall(); |
766 break; | 814 break; |
767 case kArchCallCFunction: { | 815 case kArchCallCFunction: { |
768 int const num_parameters = MiscField::decode(instr->opcode()); | 816 int const num_parameters = MiscField::decode(instr->opcode()); |
769 if (HasImmediateInput(instr, 0)) { | 817 if (HasImmediateInput(instr, 0)) { |
770 ExternalReference ref = i.InputExternalReference(0); | 818 ExternalReference ref = i.InputExternalReference(0); |
771 __ CallCFunction(ref, num_parameters); | 819 __ CallCFunction(ref, num_parameters); |
772 } else { | 820 } else { |
773 Register func = i.InputRegister(0); | 821 Register func = i.InputRegister(0); |
774 __ CallCFunction(func, num_parameters); | 822 __ CallCFunction(func, num_parameters); |
775 } | 823 } |
(...skipping 1618 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2394 int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc; | 2442 int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc; |
2395 __ Nop(padding_size); | 2443 __ Nop(padding_size); |
2396 } | 2444 } |
2397 } | 2445 } |
2398 | 2446 |
2399 #undef __ | 2447 #undef __ |
2400 | 2448 |
2401 } // namespace compiler | 2449 } // namespace compiler |
2402 } // namespace internal | 2450 } // namespace internal |
2403 } // namespace v8 | 2451 } // namespace v8 |
OLD | NEW |