Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(632)

Unified Diff: runtime/vm/flow_graph_compiler_x64.cc

Issue 9693020: Optional arguments in new compiler. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: runtime/vm/flow_graph_compiler_x64.cc
===================================================================
--- runtime/vm/flow_graph_compiler_x64.cc (revision 5369)
+++ runtime/vm/flow_graph_compiler_x64.cc (working copy)
@@ -30,8 +30,7 @@
blocks_(blocks),
block_info_(blocks->length()),
current_block_(NULL),
- pc_descriptors_list_(new CodeGenerator::DescriptorList()),
- stack_local_count_(0) {
+ pc_descriptors_list_(new CodeGenerator::DescriptorList()) {
for (int i = 0; i < blocks->length(); ++i) {
block_info_.Add(new BlockInfo());
}
@@ -165,6 +164,31 @@
}
+void FlowGraphCompiler::VisitCurrentContext(CurrentContextComp* comp) {
+ __ movq(RAX, CTX);
+}
+
+
+void FlowGraphCompiler::VisitClosureCall(ClosureCallComp* comp) {
+ ASSERT(comp->context()->IsTemp());
+ ASSERT(VerifyCallComputation(comp));
+ // The arguments to the stub include the closure. The arguments
+ // descriptor describes the closure's arguments (and so does not include
+ // the closure).
+ int argument_count = comp->ArgumentCount();
+ const Array& arguments_descriptor =
+ CodeGenerator::ArgumentsDescriptor(argument_count - 1,
+ comp->argument_names());
+ __ LoadObject(R10, arguments_descriptor);
+
+ GenerateCall(comp->token_index(),
+ &StubCode::CallClosureFunctionLabel(),
+ PcDescriptors::kOther);
+ __ addq(RSP, Immediate(argument_count * kWordSize));
+ __ popq(CTX);
+}
+
+
void FlowGraphCompiler::VisitInstanceCall(InstanceCallComp* comp) {
ASSERT(VerifyCallComputation(comp));
EmitInstanceCall(comp->node_id(),
@@ -542,6 +566,23 @@
}
+void FlowGraphCompiler::VisitCreateClosure(CreateClosureComp* comp) {
+ const Function& function = comp->function();
+ const Code& stub = Code::Handle(
+ StubCode::GetAllocationStubForClosure(function));
+ const ExternalLabel label(function.ToCString(), stub.EntryPoint());
+ GenerateCall(comp->token_index(), &label, PcDescriptors::kOther);
+
+ const Class& cls = Class::Handle(function.signature_class());
+ if (cls.HasTypeArguments()) {
+ __ popq(RCX); // Discard type arguments.
+ }
+ if (function.IsImplicitInstanceClosureFunction()) {
+ __ popq(RCX); // Discard receiver.
+ }
+}
+
+
void FlowGraphCompiler::VisitBlocks(
const GrowableArray<BlockEntryInstr*>& blocks) {
for (intptr_t i = blocks.length() - 1; i >= 0; --i) {
@@ -616,7 +657,7 @@
// Check that the entry stack size matches the exit stack size.
__ movq(R10, RBP);
__ subq(R10, RSP);
- __ cmpq(R10, Immediate(stack_local_count() * kWordSize));
+ __ cmpq(R10, Immediate(parsed_function_.local_count() * kWordSize));
Label stack_ok;
__ j(EQUAL, &stack_ok, Assembler::kNearJump);
__ Stop("Exit stack size does not match the entry stack size.");
@@ -675,54 +716,242 @@
}
+// Coped from CodeGenerator::CopyParameters (CodeGenerator will be deprecated).
+void FlowGraphCompiler::CopyParameters() {
+ const Function& function = parsed_function_.function();
+ LocalScope* scope = parsed_function_.node_sequence()->scope();
+ const int num_fixed_params = function.num_fixed_parameters();
+ const int num_opt_params = function.num_optional_parameters();
+ ASSERT(parsed_function_.first_parameter_index() == -1);
+ // Copy positional arguments.
+ // Check that no fewer than num_fixed_params positional arguments are passed
+ // in and that no more than num_params arguments are passed in.
+ // Passed argument i at fp[1 + argc - i] copied to fp[-1 - i].
+ const int num_params = num_fixed_params + num_opt_params;
+
+ // Total number of args is the first Smi in args descriptor array (R10).
+ __ movq(RBX, FieldAddress(R10, Array::data_offset()));
+ // Check that num_args <= num_params.
+ Label wrong_num_arguments;
+ __ cmpq(RBX, Immediate(Smi::RawValue(num_params)));
+ __ j(GREATER, &wrong_num_arguments);
+ // Number of positional args is the second Smi in descriptor array (R10).
+ __ movq(RCX, FieldAddress(R10, Array::data_offset() + (1 * kWordSize)));
+ // Check that num_pos_args >= num_fixed_params.
+ __ cmpq(RCX, Immediate(Smi::RawValue(num_fixed_params)));
+ __ j(LESS, &wrong_num_arguments);
+ // Since RBX and RCX are Smi, use TIMES_4 instead of TIMES_8.
+ // Let RBX point to the last passed positional argument, i.e. to
+ // fp[1 + num_args - (num_pos_args - 1)].
+ __ subq(RBX, RCX);
+ __ leaq(RBX, Address(RBP, RBX, TIMES_4, 2 * kWordSize));
+ // Let RDI point to the last copied positional argument, i.e. to
+ // fp[-1 - (num_pos_args - 1)].
+ __ SmiUntag(RCX);
+ __ movq(RAX, RCX);
+ __ negq(RAX);
+ __ leaq(RDI, Address(RBP, RAX, TIMES_8, 0));
+ Label loop, loop_condition;
+ __ jmp(&loop_condition, Assembler::kNearJump);
+ // We do not use the final allocation index of the variable here, i.e.
+ // scope->VariableAt(i)->index(), because captured variables still need
+ // to be copied to the context that is not yet allocated.
+ const Address argument_addr(RBX, RCX, TIMES_8, 0);
+ const Address copy_addr(RDI, RCX, TIMES_8, 0);
+ __ Bind(&loop);
+ __ movq(RAX, argument_addr);
+ __ movq(copy_addr, RAX);
+ __ Bind(&loop_condition);
+ __ decq(RCX);
+ __ j(POSITIVE, &loop, Assembler::kNearJump);
+
+ // Copy or initialize optional named arguments.
+ ASSERT(num_opt_params > 0); // Or we would not have to copy arguments.
+ // Start by alphabetically sorting the names of the optional parameters.
+ LocalVariable** opt_param = new LocalVariable*[num_opt_params];
+ int* opt_param_position = new int[num_opt_params];
+ for (int pos = num_fixed_params; pos < num_params; pos++) {
+ LocalVariable* parameter = scope->VariableAt(pos);
+ const String& opt_param_name = parameter->name();
+ int i = pos - num_fixed_params;
+ while (--i >= 0) {
+ LocalVariable* param_i = opt_param[i];
+ const intptr_t result = opt_param_name.CompareTo(param_i->name());
+ ASSERT(result != 0);
+ if (result > 0) break;
+ opt_param[i + 1] = opt_param[i];
+ opt_param_position[i + 1] = opt_param_position[i];
+ }
+ opt_param[i + 1] = parameter;
+ opt_param_position[i + 1] = pos;
+ }
+ // Generate code handling each optional parameter in alphabetical order.
+ // Total number of args is the first Smi in args descriptor array (R10).
+ __ movq(RBX, FieldAddress(R10, Array::data_offset()));
+ // Number of positional args is the second Smi in descriptor array (R10).
+ __ movq(RCX, FieldAddress(R10, Array::data_offset() + (1 * kWordSize)));
+ __ SmiUntag(RCX);
+ // Let RBX point to the first passed argument, i.e. to fp[1 + argc - 0].
+ __ leaq(RBX, Address(RBP, RBX, TIMES_4, kWordSize)); // RBX is Smi.
+ // Let EDI point to the name/pos pair of the first named argument.
+ __ leaq(RDI, FieldAddress(R10, Array::data_offset() + (2 * kWordSize)));
+ for (int i = 0; i < num_opt_params; i++) {
+ // Handle this optional parameter only if k or fewer positional arguments
+ // have been passed, where k is the position of this optional parameter in
+ // the formal parameter list.
+ Label load_default_value, assign_optional_parameter, next_parameter;
+ const int param_pos = opt_param_position[i];
+ __ cmpq(RCX, Immediate(param_pos));
+ __ j(GREATER, &next_parameter, Assembler::kNearJump);
+ // Check if this named parameter was passed in.
+ __ movq(RAX, Address(RDI, 0)); // Load RAX with the name of the argument.
+ __ CompareObject(RAX, opt_param[i]->name());
+ __ j(NOT_EQUAL, &load_default_value, Assembler::kNearJump);
+ // Load RAX with passed-in argument at provided arg_pos, i.e. at
+ // fp[1 + argc - arg_pos].
+ __ movq(RAX, Address(RDI, kWordSize)); // RAX is arg_pos as Smi.
+ __ addq(RDI, Immediate(2 * kWordSize)); // Point to next name/pos pair.
+ __ negq(RAX);
+ Address argument_addr(RBX, RAX, TIMES_4, 0); // RAX is a negative Smi.
+ __ movq(RAX, argument_addr);
+ __ jmp(&assign_optional_parameter, Assembler::kNearJump);
+ __ Bind(&load_default_value);
+ // Load RAX with default argument at pos.
+ const Object& value = Object::ZoneHandle(
+ parsed_function_.default_parameter_values().At(
+ param_pos - num_fixed_params));
+ __ LoadObject(RAX, value);
+ __ Bind(&assign_optional_parameter);
+ // Assign RAX to fp[-1 - param_pos].
+ // We do not use the final allocation index of the variable here, i.e.
+ // scope->VariableAt(i)->index(), because captured variables still need
+ // to be copied to the context that is not yet allocated.
+ const Address param_addr(RBP, (-1 - param_pos) * kWordSize);
+ __ movq(param_addr, RAX);
+ __ Bind(&next_parameter);
+ }
+ delete[] opt_param;
+ delete[] opt_param_position;
+ // Check that RDI now points to the null terminator in the array descriptor.
+ const Immediate raw_null =
+ Immediate(reinterpret_cast<intptr_t>(Object::null()));
+ Label all_arguments_processed;
+ __ cmpq(Address(RDI, 0), raw_null);
+ __ j(EQUAL, &all_arguments_processed, Assembler::kNearJump);
+
+ __ Bind(&wrong_num_arguments);
+ if (function.IsClosureFunction()) {
+ GenerateCallRuntime(AstNode::kNoId,
+ 0,
+ kClosureArgumentMismatchRuntimeEntry);
+ } else {
+ // Invoke noSuchMethod function.
+ const int kNumArgsChecked = 1;
+ ICData& ic_data = ICData::ZoneHandle();
+ ic_data = ICData::New(parsed_function_.function(),
+ String::Handle(function.name()),
+ AstNode::kNoId,
+ kNumArgsChecked);
+ __ LoadObject(RBX, ic_data);
+ // RBP : points to previous frame pointer.
+ // RBP + 8 : points to return address.
+ // RBP + 16 : address of last argument (arg n-1).
+ // RSP + 16 + 8*(n-1) : address of first argument (arg 0).
+ // RBX : ic-data.
+ // R10 : arguments descriptor array.
+ __ call(&StubCode::CallNoSuchMethodFunctionLabel());
+ }
+
+ if (FLAG_trace_functions) {
+ __ pushq(RAX); // Preserve result.
+ __ PushObject(Function::ZoneHandle(function.raw()));
+ GenerateCallRuntime(AstNode::kNoId,
+ 0,
+ kTraceFunctionExitRuntimeEntry);
+ __ popq(RAX); // Remove argument.
+ __ popq(RAX); // Restore result.
+ }
+ __ LeaveFrame();
+ __ ret();
+
+ __ Bind(&all_arguments_processed);
+ // Nullify originally passed arguments only after they have been copied and
+ // checked, otherwise noSuchMethod would not see their original values.
+ // This step can be skipped in case we decide that formal parameters are
+ // implicitly final, since garbage collecting the unmodified value is not
+ // an issue anymore.
+
+ // R10 : arguments descriptor array.
+ // Total number of args is the first Smi in args descriptor array (R10).
+ __ movq(RCX, FieldAddress(R10, Array::data_offset()));
+ __ SmiUntag(RCX);
+ Label null_args_loop, null_args_loop_condition;
+ __ jmp(&null_args_loop_condition, Assembler::kNearJump);
+ const Address original_argument_addr(RBP, RCX, TIMES_8, 2 * kWordSize);
+ __ Bind(&null_args_loop);
+ __ movq(original_argument_addr, raw_null);
+ __ Bind(&null_args_loop_condition);
+ __ decq(RCX);
+ __ j(POSITIVE, &null_args_loop, Assembler::kNearJump);
+}
+
+
+// TODO(srdjan): Investigate where to put the argument type checks for
+// checked mode.
void FlowGraphCompiler::CompileGraph() {
+ // Specialized version of entry code from CodeGenerator::GenerateEntryCode.
const Function& function = parsed_function_.function();
+
+ // We don't implement copied parameters yet and should have bailed out
+ // from the graph builder.
if ((function.num_optional_parameters() != 0)) {
- Bailout("function has optional parameters");
+ // Bailout("function has optional parameters");
}
- LocalScope* scope = parsed_function_.node_sequence()->scope();
- LocalScope* context_owner = NULL;
+
const int parameter_count = function.num_fixed_parameters();
- const int first_parameter_index = 1 + parameter_count;
- const int first_local_index = -1;
- int first_free_frame_index =
- scope->AllocateVariables(first_parameter_index,
- parameter_count,
- first_local_index,
- scope,
- &context_owner);
- set_stack_local_count(first_local_index - first_free_frame_index);
+ const int num_opt_params = function.num_optional_parameters();
+ const int num_copied_params = parsed_function_.copied_parameter_count();
- // Specialized version of entry code from CodeGenerator::GenerateEntryCode.
- __ EnterFrame(stack_local_count() * kWordSize);
+ const int local_count = parsed_function_.local_count();
+ __ EnterFrame(local_count * kWordSize);
+
+ // We check the number of passed arguments when we have to copy them due to
+ // the presence of optional named parameters.
+ // No such checking code is generated if only fixed parameters are declared,
+ // unless we are debug mode or unless we are compiling a closure.
+ if (num_copied_params == 0) {
#ifdef DEBUG
- const bool check_arguments = true;
+ const bool check_arguments = true;
#else
- const bool check_arguments = function.IsClosureFunction();
+ const bool check_arguments = function.IsClosureFunction();
#endif
- if (check_arguments) {
- // Check that num_fixed <= argc <= num_params.
- Label argc_in_range;
- // Total number of args is the first Smi in args descriptor array (R10).
- __ movq(RAX, FieldAddress(R10, Array::data_offset()));
- __ cmpq(RAX, Immediate(Smi::RawValue(parameter_count)));
- __ j(EQUAL, &argc_in_range, Assembler::kNearJump);
- if (function.IsClosureFunction()) {
- GenerateCallRuntime(AstNode::kNoId,
- function.token_index(),
- kClosureArgumentMismatchRuntimeEntry);
- } else {
- __ Stop("Wrong number of arguments");
+ if (check_arguments) {
+ // Check that num_fixed <= argc <= num_params.
+ Label argc_in_range;
+ // Total number of args is the first Smi in args descriptor array (R10).
+ __ movq(RAX, FieldAddress(R10, Array::data_offset()));
+ __ cmpq(RAX, Immediate(Smi::RawValue(parameter_count)));
+ __ j(EQUAL, &argc_in_range, Assembler::kNearJump);
+ if (function.IsClosureFunction()) {
+ GenerateCallRuntime(AstNode::kNoId,
+ function.token_index(),
+ kClosureArgumentMismatchRuntimeEntry);
+ } else {
+ __ Stop("Wrong number of arguments");
+ }
+ __ Bind(&argc_in_range);
}
- __ Bind(&argc_in_range);
+ } else {
+ CopyParameters();
}
// Initialize locals to null.
- if (stack_local_count() > 0) {
+ if (local_count > 0) {
__ movq(RAX, Immediate(reinterpret_cast<intptr_t>(Object::null())));
- for (int i = 0; i < stack_local_count(); ++i) {
+ const int base = parsed_function_.first_local_index();
+ for (int i = 0; i < local_count; ++i) {
// Subtract index i (locals lie at lower addresses than RBP).
- __ movq(Address(RBP, (first_local_index - i) * kWordSize), RAX);
+ __ movq(Address(RBP, (base - i) * kWordSize), RAX);
}
}

Powered by Google App Engine
This is Rietveld 408576698