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

Side by Side Diff: runtime/vm/code_generator_ia32.cc

Issue 9664062: Support allocating and calling closures in the new non-optimizing compiler. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Incorporated review comments. 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | runtime/vm/code_generator_x64.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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_IA32. 5 #include "vm/globals.h" // Needed here to get TARGET_ARCH_IA32.
6 #if defined(TARGET_ARCH_IA32) 6 #if defined(TARGET_ARCH_IA32)
7 7
8 #include "vm/code_generator.h" 8 #include "vm/code_generator.h"
9 9
10 #include "lib/error.h" 10 #include "lib/error.h"
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 ExternalLabel target_label("InlineCache", label_address); 331 ExternalLabel target_label("InlineCache", label_address);
332 332
333 __ call(&target_label); 333 __ call(&target_label);
334 AddCurrentDescriptor(PcDescriptors::kIcCall, 334 AddCurrentDescriptor(PcDescriptors::kIcCall,
335 node_id, 335 node_id,
336 token_index); 336 token_index);
337 __ addl(ESP, Immediate(num_arguments * kWordSize)); 337 __ addl(ESP, Immediate(num_arguments * kWordSize));
338 } 338 }
339 339
340 340
341 // Call to generate entry code:
342 // - compute frame size and setup frame.
343 // - allocate local variables on stack.
344 // - optionally check if number of arguments match.
345 // - initialize all non-argument locals to null.
346 //
347 // Input parameters: 341 // Input parameters:
348 // ESP : points to return address. 342 // ESP : points to return address.
349 // ESP + 4 : address of last argument (arg n-1). 343 // ESP + 4 : address of last argument (arg n-1).
350 // ESP + 4*n : address of first argument (arg 0). 344 // ESP + 4*n : address of first argument (arg 0).
351 // EDX : arguments descriptor array. 345 // EDX : arguments descriptor array.
352 void CodeGenerator::GenerateEntryCode() { 346 void CodeGenerator::GenerateEntryCode() {
353 const Immediate raw_null = 347 const Immediate raw_null =
354 Immediate(reinterpret_cast<intptr_t>(Object::null())); 348 Immediate(reinterpret_cast<intptr_t>(Object::null()));
355 const Function& function = parsed_function_.function(); 349 const Function& function = parsed_function_.function();
350
351 // 1. Compute the frame size and enter the frame (reserving local space
352 // for copied incoming and default arguments and stack-allocated local
353 // variables).
354 //
355 // TODO(regis): We may give up reserving space on stack for args/locals
356 // because pushes of initial values may be more effective than moves.
356 LocalScope* scope = parsed_function_.node_sequence()->scope(); 357 LocalScope* scope = parsed_function_.node_sequence()->scope();
357 const int num_fixed_params = function.num_fixed_parameters(); 358 const int num_fixed_params = function.num_fixed_parameters();
358 const int num_opt_params = function.num_optional_parameters(); 359 const int num_opt_params = function.num_optional_parameters();
359 const int num_params = num_fixed_params + num_opt_params; 360 const int num_copied_params = parsed_function_.copied_parameter_count();
360 int first_param_index; 361 const int stack_slot_count =
361 int first_local_index; 362 num_copied_params + parsed_function_.stack_local_count();
362 int num_copied_params; 363 set_locals_space_size(stack_slot_count * kWordSize);
363 // Assign indices to parameters and locals.
364 if (num_params == num_fixed_params) {
365 // No need to copy incoming arguments.
366 // The body of the function will access parameter i at fp[1 + num_fixed - i]
367 // and local variable j at fp[-1 - j].
368 first_param_index = 1 + num_params;
369 first_local_index = -1;
370 num_copied_params = 0;
371 } else {
372 // The body of the function will access copied parameter i at fp[-1 - i]
373 // and local j at fp[-1 - num_params - j].
374 first_param_index = -1;
375 first_local_index = -1 - num_params;
376 num_copied_params = num_params;
377 ASSERT(num_copied_params > 0);
378 }
379
380 // Allocate parameters and local variables, either in the local frame or in
381 // the context(s).
382 LocalScope* context_owner = NULL; // No context needed so far.
383 int first_free_frame_index =
384 scope->AllocateVariables(first_param_index,
385 num_params,
386 first_local_index,
387 scope, // Initial loop owner.
388 &context_owner);
389 // Frame indices are relative to the frame pointer and are decreasing.
390 ASSERT(first_free_frame_index <= first_local_index);
391 const int num_locals = first_local_index - first_free_frame_index;
392
393 // Reserve local space for copied incoming and default arguments and locals.
394 // TODO(regis): We may give up reserving space on stack for args/locals
395 // because pushes of initial values may be more effective than moves.
396 set_locals_space_size((num_copied_params + num_locals) * kWordSize);
397 __ EnterFrame(locals_space_size()); 364 __ EnterFrame(locals_space_size());
398 365
399 // We check the number of passed arguments when we have to copy them due to 366 // 2. Optionally check if the number of arguments matches. We check the
400 // the presence of optional named parameters. 367 // number of passed arguments when we have to copy them due to the
401 // No such checking code is generated if only fixed parameters are declared, 368 // presence of optional named parameters. No such checking code is
402 // unless we are debug mode or unless we are compiling a closure. 369 // generated if only fixed parameters are declared, unless we are in debug
370 // mode or unless we are compiling a closure.
403 if (num_copied_params == 0) { 371 if (num_copied_params == 0) {
404 #if defined(DEBUG) 372 #if defined(DEBUG)
405 const bool check_arguments = true; // Always check arguments in debug mode. 373 const bool check_arguments = true; // Always check arguments in debug mode.
406 #else 374 #else
407 // The number of arguments passed to closure functions must always be 375 // The number of arguments passed to closure functions must always be
408 // checked here, because no resolving stub (normally responsible for the 376 // checked here, because no resolving stub (normally responsible for the
409 // check) is involved in closure calls. 377 // check) is involved in closure calls.
410 const bool check_arguments = function.IsClosureFunction(); 378 const bool check_arguments = function.IsClosureFunction();
411 #endif 379 #endif
412 if (check_arguments) { 380 if (check_arguments) {
(...skipping 12 matching lines...) Expand all
425 if (function.IsClosureFunction()) { 393 if (function.IsClosureFunction()) {
426 GenerateCallRuntime(AstNode::kNoId, 394 GenerateCallRuntime(AstNode::kNoId,
427 0, 395 0,
428 kClosureArgumentMismatchRuntimeEntry); 396 kClosureArgumentMismatchRuntimeEntry);
429 } else { 397 } else {
430 __ Stop("Wrong number of arguments"); 398 __ Stop("Wrong number of arguments");
431 } 399 }
432 __ Bind(&argc_in_range); 400 __ Bind(&argc_in_range);
433 } 401 }
434 } else { 402 } else {
435 ASSERT(first_param_index == -1); 403 ASSERT(parsed_function_.first_parameter_index() == -1);
436 // Copy positional arguments. 404 // Copy positional arguments.
437 // Check that no fewer than num_fixed_params positional arguments are passed 405 // Check that no fewer than num_fixed_params positional arguments are passed
438 // in and that no more than num_params arguments are passed in. 406 // in and that no more than num_params arguments are passed in.
439 // Passed argument i at fp[1 + argc - i] copied to fp[-1 - i]. 407 // Passed argument i at fp[1 + argc - i] copied to fp[-1 - i].
408 const int num_params = num_fixed_params + num_opt_params;
440 409
441 // Total number of args is the first Smi in args descriptor array (EDX). 410 // Total number of args is the first Smi in args descriptor array (EDX).
442 __ movl(EBX, FieldAddress(EDX, Array::data_offset())); 411 __ movl(EBX, FieldAddress(EDX, Array::data_offset()));
443 // Check that num_args <= num_params. 412 // Check that num_args <= num_params.
444 Label wrong_num_arguments; 413 Label wrong_num_arguments;
445 __ cmpl(EBX, Immediate(Smi::RawValue(num_params))); 414 __ cmpl(EBX, Immediate(Smi::RawValue(num_params)));
446 __ j(GREATER, &wrong_num_arguments); 415 __ j(GREATER, &wrong_num_arguments);
447 // Number of positional args is the second Smi in descriptor array (EDX). 416 // Number of positional args is the second Smi in descriptor array (EDX).
448 __ movl(ECX, FieldAddress(EDX, Array::data_offset() + (1 * kWordSize))); 417 __ movl(ECX, FieldAddress(EDX, Array::data_offset() + (1 * kWordSize)));
449 // Check that num_pos_args >= num_fixed_params. 418 // Check that num_pos_args >= num_fixed_params.
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
595 Label null_args_loop, null_args_loop_condition; 564 Label null_args_loop, null_args_loop_condition;
596 __ jmp(&null_args_loop_condition, Assembler::kNearJump); 565 __ jmp(&null_args_loop_condition, Assembler::kNearJump);
597 const Address original_argument_addr(EBP, ECX, TIMES_4, 2 * kWordSize); 566 const Address original_argument_addr(EBP, ECX, TIMES_4, 2 * kWordSize);
598 __ Bind(&null_args_loop); 567 __ Bind(&null_args_loop);
599 __ movl(original_argument_addr, raw_null); 568 __ movl(original_argument_addr, raw_null);
600 __ Bind(&null_args_loop_condition); 569 __ Bind(&null_args_loop_condition);
601 __ decl(ECX); 570 __ decl(ECX);
602 __ j(POSITIVE, &null_args_loop, Assembler::kNearJump); 571 __ j(POSITIVE, &null_args_loop, Assembler::kNearJump);
603 } 572 }
604 573
605 // Initialize locals. 574 // 3. Initialize (non-argument) stack-allocated locals to null.
575 //
606 // TODO(regis): For now, always unroll the init loop. Decide later above 576 // TODO(regis): For now, always unroll the init loop. Decide later above
607 // which threshold to implement a loop. 577 // which threshold to implement a loop. Consider emitting pushes instead
608 // Consider emitting pushes instead of moves. 578 // of moves.
609 for (int index = first_local_index; index > first_free_frame_index; index--) { 579 const int base = parsed_function_.first_stack_local_index();
610 if (index == first_local_index) { 580 for (int index = 0; index < parsed_function_.stack_local_count(); ++index) {
581 if (index == 0) {
611 __ movl(EAX, raw_null); 582 __ movl(EAX, raw_null);
612 } 583 }
613 __ movl(Address(EBP, index * kWordSize), EAX); 584 __ movl(Address(EBP, (base - index) * kWordSize), EAX);
614 } 585 }
615 586
616 // Generate stack overflow check. 587 // 4. Generate the stack overflow check.
617 __ cmpl(ESP, 588 __ cmpl(ESP,
618 Address::Absolute(Isolate::Current()->stack_limit_address())); 589 Address::Absolute(Isolate::Current()->stack_limit_address()));
619 Label no_stack_overflow; 590 Label no_stack_overflow;
620 __ j(ABOVE, &no_stack_overflow); 591 __ j(ABOVE, &no_stack_overflow);
621 GenerateCallRuntime(AstNode::kNoId, 592 GenerateCallRuntime(AstNode::kNoId,
622 0, 593 0,
623 kStackOverflowRuntimeEntry); 594 kStackOverflowRuntimeEntry);
624 __ Bind(&no_stack_overflow); 595 __ Bind(&no_stack_overflow);
625 } 596 }
626 597
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
759 node->dst_name()); 730 node->dst_name());
760 if (IsResultNeeded(node)) { 731 if (IsResultNeeded(node)) {
761 __ pushl(EAX); 732 __ pushl(EAX);
762 } 733 }
763 } 734 }
764 735
765 736
766 void CodeGenerator::VisitClosureNode(ClosureNode* node) { 737 void CodeGenerator::VisitClosureNode(ClosureNode* node) {
767 const Function& function = node->function(); 738 const Function& function = node->function();
768 if (function.IsNonImplicitClosureFunction()) { 739 if (function.IsNonImplicitClosureFunction()) {
769 const int current_context_level = state()->context_level(); 740 // The context scope may have already been set by the new non-optimizing
770 const ContextScope& context_scope = ContextScope::ZoneHandle( 741 // compiler. If it was not, set it here.
771 node->scope()->PreserveOuterScope(current_context_level)); 742 if (function.context_scope() == ContextScope::null()) {
772 ASSERT(!function.HasCode()); 743 const int current_context_level = state()->context_level();
773 ASSERT(function.context_scope() == ContextScope::null()); 744 const ContextScope& context_scope = ContextScope::ZoneHandle(
774 function.set_context_scope(context_scope); 745 node->scope()->PreserveOuterScope(current_context_level));
775 } else { 746 ASSERT(!function.HasCode());
776 ASSERT(function.context_scope() != ContextScope::null()); 747 function.set_context_scope(context_scope);
777 if (function.IsImplicitInstanceClosureFunction()) {
778 node->receiver()->Visit(this);
779 } 748 }
749 } else if (function.IsImplicitInstanceClosureFunction()) {
750 node->receiver()->Visit(this);
780 } 751 }
752 ASSERT(function.context_scope() != ContextScope::null());
753
781 // The function type of a closure may have type arguments. In that case, pass 754 // The function type of a closure may have type arguments. In that case, pass
782 // the type arguments of the instantiator. 755 // the type arguments of the instantiator.
783 const Class& cls = Class::Handle(function.signature_class()); 756 const Class& cls = Class::Handle(function.signature_class());
784 ASSERT(!cls.IsNull()); 757 ASSERT(!cls.IsNull());
785 const bool requires_type_arguments = cls.HasTypeArguments(); 758 const bool requires_type_arguments = cls.HasTypeArguments();
786 if (requires_type_arguments) { 759 if (requires_type_arguments) {
787 ASSERT(!function.IsImplicitStaticClosureFunction()); 760 ASSERT(!function.IsImplicitStaticClosureFunction());
788 GenerateInstantiatorTypeArguments(node->token_index()); 761 GenerateInstantiatorTypeArguments(node->token_index());
789 } 762 }
790 const Code& stub = Code::Handle( 763 const Code& stub = Code::Handle(
(...skipping 1894 matching lines...) Expand 10 before | Expand all | Expand 10 after
2685 const Error& error = Error::Handle( 2658 const Error& error = Error::Handle(
2686 Parser::FormatError(script, token_index, "Error", format, args)); 2659 Parser::FormatError(script, token_index, "Error", format, args));
2687 va_end(args); 2660 va_end(args);
2688 Isolate::Current()->long_jump_base()->Jump(1, error); 2661 Isolate::Current()->long_jump_base()->Jump(1, error);
2689 UNREACHABLE(); 2662 UNREACHABLE();
2690 } 2663 }
2691 2664
2692 } // namespace dart 2665 } // namespace dart
2693 2666
2694 #endif // defined TARGET_ARCH_IA32 2667 #endif // defined TARGET_ARCH_IA32
OLDNEW
« no previous file with comments | « no previous file | runtime/vm/code_generator_x64.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698