| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "vm/globals.h" // Needed here to get TARGET_ARCH_X64. | 5 #include "vm/globals.h" // Needed here to get TARGET_ARCH_X64. |
| 6 #if defined(TARGET_ARCH_X64) | 6 #if defined(TARGET_ARCH_X64) |
| 7 | 7 |
| 8 #include "vm/flow_graph_compiler.h" | 8 #include "vm/flow_graph_compiler.h" |
| 9 | 9 |
| 10 #include "lib/error.h" | 10 #include "lib/error.h" |
| (...skipping 630 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 641 Location loc = locs->in(i); | 641 Location loc = locs->in(i); |
| 642 ASSERT(loc.kind() == Location::kRegister); | 642 ASSERT(loc.kind() == Location::kRegister); |
| 643 __ popq(loc.reg()); | 643 __ popq(loc.reg()); |
| 644 } | 644 } |
| 645 } | 645 } |
| 646 | 646 |
| 647 | 647 |
| 648 // Copied from CodeGenerator::CopyParameters (CodeGenerator will be deprecated). | 648 // Copied from CodeGenerator::CopyParameters (CodeGenerator will be deprecated). |
| 649 void FlowGraphCompiler::CopyParameters() { | 649 void FlowGraphCompiler::CopyParameters() { |
| 650 const Function& function = parsed_function().function(); | 650 const Function& function = parsed_function().function(); |
| 651 const bool is_native_instance_closure = |
| 652 function.is_native() && function.IsImplicitInstanceClosureFunction(); |
| 651 LocalScope* scope = parsed_function().node_sequence()->scope(); | 653 LocalScope* scope = parsed_function().node_sequence()->scope(); |
| 652 const int num_fixed_params = function.num_fixed_parameters(); | 654 const int num_fixed_params = function.num_fixed_parameters(); |
| 653 const int num_opt_params = function.num_optional_parameters(); | 655 const int num_opt_params = function.num_optional_parameters(); |
| 654 ASSERT(parsed_function().first_parameter_index() == | 656 ASSERT(parsed_function().first_parameter_index() == |
| 655 ParsedFunction::kFirstLocalSlotIndex); | 657 ParsedFunction::kFirstLocalSlotIndex); |
| 656 // Copy positional arguments. | 658 // Copy positional arguments. |
| 657 // Check that no fewer than num_fixed_params positional arguments are passed | 659 // Check that no fewer than num_fixed_params positional arguments are passed |
| 658 // in and that no more than num_params arguments are passed in. | 660 // in and that no more than num_params arguments are passed in. |
| 659 // Passed argument i at fp[1 + argc - i] | 661 // Passed argument i at fp[1 + argc - i] |
| 660 // copied to fp[ParsedFunction::kFirstLocalSlotIndex - i]. | 662 // copied to fp[ParsedFunction::kFirstLocalSlotIndex - i]. |
| 661 const int num_params = num_fixed_params + num_opt_params; | 663 const int num_params = num_fixed_params + num_opt_params; |
| 662 | 664 |
| 663 // Total number of args is the first Smi in args descriptor array (R10). | 665 // Total number of args is the first Smi in args descriptor array (R10). |
| 664 __ movq(RBX, FieldAddress(R10, Array::data_offset())); | 666 __ movq(RBX, FieldAddress(R10, Array::data_offset())); |
| 665 // Check that num_args <= num_params. | 667 // Check that num_args <= num_params. |
| 666 Label wrong_num_arguments; | 668 Label wrong_num_arguments; |
| 667 __ cmpq(RBX, Immediate(Smi::RawValue(num_params))); | 669 __ cmpq(RBX, Immediate(Smi::RawValue(num_params))); |
| 668 __ j(GREATER, &wrong_num_arguments); | 670 __ j(GREATER, &wrong_num_arguments); |
| 669 // Number of positional args is the second Smi in descriptor array (R10). | 671 // Number of positional args is the second Smi in descriptor array (R10). |
| 670 __ movq(RCX, FieldAddress(R10, Array::data_offset() + (1 * kWordSize))); | 672 __ movq(RCX, FieldAddress(R10, Array::data_offset() + (1 * kWordSize))); |
| 671 // Check that num_pos_args >= num_fixed_params. | 673 // Check that num_pos_args >= num_fixed_params. |
| 672 __ cmpq(RCX, Immediate(Smi::RawValue(num_fixed_params))); | 674 __ cmpq(RCX, Immediate(Smi::RawValue(num_fixed_params))); |
| 673 __ j(LESS, &wrong_num_arguments); | 675 __ j(LESS, &wrong_num_arguments); |
| 676 |
| 674 // Since RBX and RCX are Smi, use TIMES_4 instead of TIMES_8. | 677 // Since RBX and RCX are Smi, use TIMES_4 instead of TIMES_8. |
| 675 // Let RBX point to the last passed positional argument, i.e. to | 678 // Let RBX point to the last passed positional argument, i.e. to |
| 676 // fp[1 + num_args - (num_pos_args - 1)]. | 679 // fp[1 + num_args - (num_pos_args - 1)]. |
| 677 __ subq(RBX, RCX); | 680 __ subq(RBX, RCX); |
| 678 __ leaq(RBX, Address(RBP, RBX, TIMES_4, 2 * kWordSize)); | 681 __ leaq(RBX, Address(RBP, RBX, TIMES_4, 2 * kWordSize)); |
| 679 // Let RDI point to the last copied positional argument, i.e. to | 682 // Let RDI point to the last copied positional argument, i.e. to |
| 680 // fp[ParsedFunction::kFirstLocalSlotIndex - (num_pos_args - 1)]. | 683 // fp[ParsedFunction::kFirstLocalSlotIndex - (num_pos_args - 1)]. |
| 684 int implicit_this_param_pos = is_native_instance_closure ? -1 : 0; |
| 685 const int index = |
| 686 ParsedFunction::kFirstLocalSlotIndex + 1 + implicit_this_param_pos; |
| 687 // First copy captured receiver if function is an implicit native closure. |
| 688 if (is_native_instance_closure) { |
| 689 __ movq(RAX, FieldAddress(CTX, Context::variable_offset(0))); |
| 690 __ movq(Address(RBP, (index * kWordSize)), RAX); |
| 691 } |
| 681 __ SmiUntag(RCX); | 692 __ SmiUntag(RCX); |
| 682 __ movq(RAX, RCX); | 693 __ movq(RAX, RCX); |
| 683 __ negq(RAX); | 694 __ negq(RAX); |
| 684 const int index = ParsedFunction::kFirstLocalSlotIndex + 1; | |
| 685 // -num_pos_args is in RAX. | 695 // -num_pos_args is in RAX. |
| 686 // (ParsedFunction::kFirstLocalSlotIndex + 1) is in index. | 696 // (ParsedFunction::kFirstLocalSlotIndex + 1 + implicit_this_param_pos) |
| 697 // is in index. |
| 687 __ leaq(RDI, Address(RBP, RAX, TIMES_8, (index * kWordSize))); | 698 __ leaq(RDI, Address(RBP, RAX, TIMES_8, (index * kWordSize))); |
| 688 Label loop, loop_condition; | 699 Label loop, loop_condition; |
| 689 __ jmp(&loop_condition, Assembler::kNearJump); | 700 __ jmp(&loop_condition, Assembler::kNearJump); |
| 690 // We do not use the final allocation index of the variable here, i.e. | 701 // We do not use the final allocation index of the variable here, i.e. |
| 691 // scope->VariableAt(i)->index(), because captured variables still need | 702 // scope->VariableAt(i)->index(), because captured variables still need |
| 692 // to be copied to the context that is not yet allocated. | 703 // to be copied to the context that is not yet allocated. |
| 693 const Address argument_addr(RBX, RCX, TIMES_8, 0); | 704 const Address argument_addr(RBX, RCX, TIMES_8, 0); |
| 694 const Address copy_addr(RDI, RCX, TIMES_8, 0); | 705 const Address copy_addr(RDI, RCX, TIMES_8, 0); |
| 695 __ Bind(&loop); | 706 __ Bind(&loop); |
| 696 __ movq(RAX, argument_addr); | 707 __ movq(RAX, argument_addr); |
| 697 __ movq(copy_addr, RAX); | 708 __ movq(copy_addr, RAX); |
| 698 __ Bind(&loop_condition); | 709 __ Bind(&loop_condition); |
| 699 __ decq(RCX); | 710 __ decq(RCX); |
| 700 __ j(POSITIVE, &loop, Assembler::kNearJump); | 711 __ j(POSITIVE, &loop, Assembler::kNearJump); |
| 701 | 712 |
| 702 // Copy or initialize optional named arguments. | 713 // Copy or initialize optional named arguments. |
| 703 ASSERT(num_opt_params > 0); // Or we would not have to copy arguments. | |
| 704 // Start by alphabetically sorting the names of the optional parameters. | |
| 705 LocalVariable** opt_param = new LocalVariable*[num_opt_params]; | |
| 706 int* opt_param_position = new int[num_opt_params]; | |
| 707 for (int pos = num_fixed_params; pos < num_params; pos++) { | |
| 708 LocalVariable* parameter = scope->VariableAt(pos); | |
| 709 const String& opt_param_name = parameter->name(); | |
| 710 int i = pos - num_fixed_params; | |
| 711 while (--i >= 0) { | |
| 712 LocalVariable* param_i = opt_param[i]; | |
| 713 const intptr_t result = opt_param_name.CompareTo(param_i->name()); | |
| 714 ASSERT(result != 0); | |
| 715 if (result > 0) break; | |
| 716 opt_param[i + 1] = opt_param[i]; | |
| 717 opt_param_position[i + 1] = opt_param_position[i]; | |
| 718 } | |
| 719 opt_param[i + 1] = parameter; | |
| 720 opt_param_position[i + 1] = pos; | |
| 721 } | |
| 722 // Generate code handling each optional parameter in alphabetical order. | |
| 723 // Total number of args is the first Smi in args descriptor array (R10). | |
| 724 __ movq(RBX, FieldAddress(R10, Array::data_offset())); | |
| 725 // Number of positional args is the second Smi in descriptor array (R10). | |
| 726 __ movq(RCX, FieldAddress(R10, Array::data_offset() + (1 * kWordSize))); | |
| 727 __ SmiUntag(RCX); | |
| 728 // Let RBX point to the first passed argument, i.e. to fp[1 + argc - 0]. | |
| 729 __ leaq(RBX, Address(RBP, RBX, TIMES_4, kWordSize)); // RBX is Smi. | |
| 730 // Let EDI point to the name/pos pair of the first named argument. | |
| 731 __ leaq(RDI, FieldAddress(R10, Array::data_offset() + (2 * kWordSize))); | |
| 732 for (int i = 0; i < num_opt_params; i++) { | |
| 733 // Handle this optional parameter only if k or fewer positional arguments | |
| 734 // have been passed, where k is the position of this optional parameter in | |
| 735 // the formal parameter list. | |
| 736 Label load_default_value, assign_optional_parameter, next_parameter; | |
| 737 const int param_pos = opt_param_position[i]; | |
| 738 __ cmpq(RCX, Immediate(param_pos)); | |
| 739 __ j(GREATER, &next_parameter, Assembler::kNearJump); | |
| 740 // Check if this named parameter was passed in. | |
| 741 __ movq(RAX, Address(RDI, 0)); // Load RAX with the name of the argument. | |
| 742 __ CompareObject(RAX, opt_param[i]->name()); | |
| 743 __ j(NOT_EQUAL, &load_default_value, Assembler::kNearJump); | |
| 744 // Load RAX with passed-in argument at provided arg_pos, i.e. at | |
| 745 // fp[1 + argc - arg_pos]. | |
| 746 __ movq(RAX, Address(RDI, kWordSize)); // RAX is arg_pos as Smi. | |
| 747 __ addq(RDI, Immediate(2 * kWordSize)); // Point to next name/pos pair. | |
| 748 __ negq(RAX); | |
| 749 Address argument_addr(RBX, RAX, TIMES_4, 0); // RAX is a negative Smi. | |
| 750 __ movq(RAX, argument_addr); | |
| 751 __ jmp(&assign_optional_parameter, Assembler::kNearJump); | |
| 752 __ Bind(&load_default_value); | |
| 753 // Load RAX with default argument at pos. | |
| 754 const Object& value = Object::ZoneHandle( | |
| 755 parsed_function().default_parameter_values().At( | |
| 756 param_pos - num_fixed_params)); | |
| 757 __ LoadObject(RAX, value); | |
| 758 __ Bind(&assign_optional_parameter); | |
| 759 // Assign RAX to fp[ParsedFunction::kFirstLocalSlotIndex - param_pos]. | |
| 760 // We do not use the final allocation index of the variable here, i.e. | |
| 761 // scope->VariableAt(i)->index(), because captured variables still need | |
| 762 // to be copied to the context that is not yet allocated. | |
| 763 const Address param_addr( | |
| 764 RBP, (ParsedFunction::kFirstLocalSlotIndex - param_pos) * kWordSize); | |
| 765 __ movq(param_addr, RAX); | |
| 766 __ Bind(&next_parameter); | |
| 767 } | |
| 768 delete[] opt_param; | |
| 769 delete[] opt_param_position; | |
| 770 // Check that RDI now points to the null terminator in the array descriptor. | |
| 771 const Immediate raw_null = | 714 const Immediate raw_null = |
| 772 Immediate(reinterpret_cast<intptr_t>(Object::null())); | 715 Immediate(reinterpret_cast<intptr_t>(Object::null())); |
| 773 Label all_arguments_processed; | 716 Label all_arguments_processed; |
| 774 __ cmpq(Address(RDI, 0), raw_null); | 717 if (num_opt_params > 0) { |
| 775 __ j(EQUAL, &all_arguments_processed, Assembler::kNearJump); | 718 // Start by alphabetically sorting the names of the optional parameters. |
| 719 LocalVariable** opt_param = new LocalVariable*[num_opt_params]; |
| 720 int* opt_param_position = new int[num_opt_params]; |
| 721 for (int pos = num_fixed_params; pos < num_params; pos++) { |
| 722 LocalVariable* parameter = scope->VariableAt(pos); |
| 723 const String& opt_param_name = parameter->name(); |
| 724 int i = pos - num_fixed_params; |
| 725 while (--i >= 0) { |
| 726 LocalVariable* param_i = opt_param[i]; |
| 727 const intptr_t result = opt_param_name.CompareTo(param_i->name()); |
| 728 ASSERT(result != 0); |
| 729 if (result > 0) break; |
| 730 opt_param[i + 1] = opt_param[i]; |
| 731 opt_param_position[i + 1] = opt_param_position[i]; |
| 732 } |
| 733 opt_param[i + 1] = parameter; |
| 734 opt_param_position[i + 1] = pos; |
| 735 } |
| 736 // Generate code handling each optional parameter in alphabetical order. |
| 737 // Total number of args is the first Smi in args descriptor array (R10). |
| 738 __ movq(RBX, FieldAddress(R10, Array::data_offset())); |
| 739 // Number of positional args is the second Smi in descriptor array (R10). |
| 740 __ movq(RCX, FieldAddress(R10, Array::data_offset() + (1 * kWordSize))); |
| 741 __ SmiUntag(RCX); |
| 742 // Let RBX point to the first passed argument, i.e. to fp[1 + argc - 0]. |
| 743 __ leaq(RBX, Address(RBP, RBX, TIMES_4, kWordSize)); // RBX is Smi. |
| 744 // Let EDI point to the name/pos pair of the first named argument. |
| 745 __ leaq(RDI, FieldAddress(R10, Array::data_offset() + (2 * kWordSize))); |
| 746 for (int i = 0; i < num_opt_params; i++) { |
| 747 // Handle this optional parameter only if k or fewer positional arguments |
| 748 // have been passed, where k is the position of this optional parameter in |
| 749 // the formal parameter list. |
| 750 Label load_default_value, assign_optional_parameter, next_parameter; |
| 751 const int param_pos = opt_param_position[i]; |
| 752 __ cmpq(RCX, Immediate(param_pos)); |
| 753 __ j(GREATER, &next_parameter, Assembler::kNearJump); |
| 754 // Check if this named parameter was passed in. |
| 755 __ movq(RAX, Address(RDI, 0)); // Load RAX with the name of the argument. |
| 756 __ CompareObject(RAX, opt_param[i]->name()); |
| 757 __ j(NOT_EQUAL, &load_default_value, Assembler::kNearJump); |
| 758 // Load RAX with passed-in argument at provided arg_pos, i.e. at |
| 759 // fp[1 + argc - arg_pos]. |
| 760 __ movq(RAX, Address(RDI, kWordSize)); // RAX is arg_pos as Smi. |
| 761 __ addq(RDI, Immediate(2 * kWordSize)); // Point to next name/pos pair. |
| 762 __ negq(RAX); |
| 763 Address argument_addr(RBX, RAX, TIMES_4, 0); // RAX is a negative Smi. |
| 764 __ movq(RAX, argument_addr); |
| 765 __ jmp(&assign_optional_parameter, Assembler::kNearJump); |
| 766 __ Bind(&load_default_value); |
| 767 // Load RAX with default argument at pos. |
| 768 const Object& value = Object::ZoneHandle( |
| 769 parsed_function().default_parameter_values().At( |
| 770 param_pos - num_fixed_params)); |
| 771 __ LoadObject(RAX, value); |
| 772 __ Bind(&assign_optional_parameter); |
| 773 // Assign RAX to fp[ParsedFunction::kFirstLocalSlotIndex - param_pos]. |
| 774 // We do not use the final allocation index of the variable here, i.e. |
| 775 // scope->VariableAt(i)->index(), because captured variables still need |
| 776 // to be copied to the context that is not yet allocated. |
| 777 intptr_t computed_param_pos = (ParsedFunction::kFirstLocalSlotIndex - |
| 778 param_pos + implicit_this_param_pos); |
| 779 const Address param_addr(RBP, (computed_param_pos * kWordSize)); |
| 780 __ movq(param_addr, RAX); |
| 781 __ Bind(&next_parameter); |
| 782 } |
| 783 delete[] opt_param; |
| 784 delete[] opt_param_position; |
| 785 // Check that RDI now points to the null terminator in the array descriptor. |
| 786 __ cmpq(Address(RDI, 0), raw_null); |
| 787 __ j(EQUAL, &all_arguments_processed, Assembler::kNearJump); |
| 788 } else { |
| 789 ASSERT(is_native_instance_closure); |
| 790 __ jmp(&all_arguments_processed, Assembler::kNearJump); |
| 791 } |
| 776 | 792 |
| 777 __ Bind(&wrong_num_arguments); | 793 __ Bind(&wrong_num_arguments); |
| 778 if (StackSize() != 0) { | 794 if (StackSize() != 0) { |
| 779 // We need to unwind the space we reserved for locals and copied parameters. | 795 // We need to unwind the space we reserved for locals and copied parameters. |
| 780 // The NoSuchMethodFunction stub does not expect to see that area on the | 796 // The NoSuchMethodFunction stub does not expect to see that area on the |
| 781 // stack. | 797 // stack. |
| 782 __ addq(RSP, Immediate(StackSize() * kWordSize)); | 798 __ addq(RSP, Immediate(StackSize() * kWordSize)); |
| 783 } | 799 } |
| 784 if (function.IsClosureFunction()) { | 800 if (function.IsClosureFunction()) { |
| 785 GenerateCallRuntime(AstNode::kNoId, | 801 GenerateCallRuntime(AstNode::kNoId, |
| (...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1050 __ cvtsi2sd(result, temp); | 1066 __ cvtsi2sd(result, temp); |
| 1051 __ Bind(&done); | 1067 __ Bind(&done); |
| 1052 } | 1068 } |
| 1053 | 1069 |
| 1054 | 1070 |
| 1055 #undef __ | 1071 #undef __ |
| 1056 | 1072 |
| 1057 } // namespace dart | 1073 } // namespace dart |
| 1058 | 1074 |
| 1059 #endif // defined TARGET_ARCH_X64 | 1075 #endif // defined TARGET_ARCH_X64 |
| OLD | NEW |