| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 550 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 561 void FullCodeGenerator::DoTest(const TestContext* context) { | 561 void FullCodeGenerator::DoTest(const TestContext* context) { |
| 562 DoTest(context->condition(), | 562 DoTest(context->condition(), |
| 563 context->true_label(), | 563 context->true_label(), |
| 564 context->false_label(), | 564 context->false_label(), |
| 565 context->fall_through()); | 565 context->fall_through()); |
| 566 } | 566 } |
| 567 | 567 |
| 568 | 568 |
| 569 void FullCodeGenerator::VisitDeclarations( | 569 void FullCodeGenerator::VisitDeclarations( |
| 570 ZoneList<Declaration*>* declarations) { | 570 ZoneList<Declaration*>* declarations) { |
| 571 int save_global_count = global_count_; | 571 ZoneList<Handle<Object> >* saved_globals = globals_; |
| 572 global_count_ = 0; | 572 ZoneList<Handle<Object> > inner_globals(10); |
| 573 globals_ = &inner_globals; |
| 573 | 574 |
| 574 AstVisitor::VisitDeclarations(declarations); | 575 AstVisitor::VisitDeclarations(declarations); |
| 575 | 576 if (!globals_->is_empty()) { |
| 576 // Batch declare global functions and variables. | |
| 577 if (global_count_ > 0) { | |
| 578 Handle<FixedArray> array = | |
| 579 isolate()->factory()->NewFixedArray(2 * global_count_, TENURED); | |
| 580 int length = declarations->length(); | |
| 581 for (int j = 0, i = 0; i < length; i++) { | |
| 582 Declaration* decl = declarations->at(i); | |
| 583 Variable* var = decl->proxy()->var(); | |
| 584 | |
| 585 if (var->IsUnallocated()) { | |
| 586 array->set(j++, *(var->name())); | |
| 587 FunctionDeclaration* fun_decl = decl->AsFunctionDeclaration(); | |
| 588 if (fun_decl == NULL) { | |
| 589 if (var->binding_needs_init()) { | |
| 590 // In case this binding needs initialization use the hole. | |
| 591 array->set_the_hole(j++); | |
| 592 } else { | |
| 593 array->set_undefined(j++); | |
| 594 } | |
| 595 } else { | |
| 596 Handle<SharedFunctionInfo> function = | |
| 597 Compiler::BuildFunctionInfo(fun_decl->fun(), script()); | |
| 598 // Check for stack-overflow exception. | |
| 599 if (function.is_null()) { | |
| 600 SetStackOverflow(); | |
| 601 return; | |
| 602 } | |
| 603 array->set(j++, *function); | |
| 604 } | |
| 605 } | |
| 606 } | |
| 607 // Invoke the platform-dependent code generator to do the actual | 577 // Invoke the platform-dependent code generator to do the actual |
| 608 // declaration the global functions and variables. | 578 // declaration the global functions and variables. |
| 579 Handle<FixedArray> array = |
| 580 isolate()->factory()->NewFixedArray(globals_->length(), TENURED); |
| 581 for (int i = 0; i < globals_->length(); ++i) |
| 582 array->set(i, *globals_->at(i)); |
| 609 DeclareGlobals(array); | 583 DeclareGlobals(array); |
| 610 } | 584 } |
| 611 | 585 |
| 612 global_count_ = save_global_count; | 586 globals_ = saved_globals; |
| 613 } | |
| 614 | |
| 615 | |
| 616 void FullCodeGenerator::VisitVariableDeclaration(VariableDeclaration* decl) { | |
| 617 EmitDeclaration(decl->proxy(), decl->mode(), NULL); | |
| 618 } | |
| 619 | |
| 620 | |
| 621 void FullCodeGenerator::VisitFunctionDeclaration(FunctionDeclaration* decl) { | |
| 622 EmitDeclaration(decl->proxy(), decl->mode(), decl->fun()); | |
| 623 } | |
| 624 | |
| 625 | |
| 626 void FullCodeGenerator::VisitModuleDeclaration(ModuleDeclaration* decl) { | |
| 627 EmitDeclaration(decl->proxy(), decl->mode(), NULL); | |
| 628 } | |
| 629 | |
| 630 | |
| 631 void FullCodeGenerator::VisitImportDeclaration(ImportDeclaration* decl) { | |
| 632 EmitDeclaration(decl->proxy(), decl->mode(), NULL); | |
| 633 } | |
| 634 | |
| 635 | |
| 636 void FullCodeGenerator::VisitExportDeclaration(ExportDeclaration* decl) { | |
| 637 // TODO(rossberg) | |
| 638 } | 587 } |
| 639 | 588 |
| 640 | 589 |
| 641 void FullCodeGenerator::VisitModuleLiteral(ModuleLiteral* module) { | 590 void FullCodeGenerator::VisitModuleLiteral(ModuleLiteral* module) { |
| 642 // TODO(rossberg) | 591 Handle<JSModule> instance = module->interface()->Instance(); |
| 592 ASSERT(!instance.is_null()); |
| 593 |
| 594 // Allocate a module context statically. |
| 595 Block* block = module->body(); |
| 596 Scope* saved_scope = scope(); |
| 597 scope_ = block->scope(); |
| 598 Handle<ScopeInfo> scope_info = scope_->GetScopeInfo(); |
| 599 |
| 600 // Generate code for module creation and linking. |
| 601 Comment cmnt(masm_, "[ ModuleLiteral"); |
| 602 SetStatementPosition(block); |
| 603 |
| 604 if (scope_info->HasContext()) { |
| 605 // Set up module context. |
| 606 __ Push(scope_info); |
| 607 __ Push(instance); |
| 608 __ CallRuntime(Runtime::kPushModuleContext, 2); |
| 609 StoreToFrameField( |
| 610 StandardFrameConstants::kContextOffset, context_register()); |
| 611 } |
| 612 |
| 613 { |
| 614 Comment cmnt(masm_, "[ Declarations"); |
| 615 VisitDeclarations(scope_->declarations()); |
| 616 } |
| 617 |
| 618 scope_ = saved_scope; |
| 619 if (scope_info->HasContext()) { |
| 620 // Pop module context. |
| 621 LoadContextField(context_register(), Context::PREVIOUS_INDEX); |
| 622 // Update local stack frame context field. |
| 623 StoreToFrameField( |
| 624 StandardFrameConstants::kContextOffset, context_register()); |
| 625 } |
| 626 |
| 627 // Populate module instance object. |
| 628 const PropertyAttributes attr = |
| 629 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE | DONT_ENUM); |
| 630 for (Interface::Iterator it = module->interface()->iterator(); |
| 631 !it.done(); it.Advance()) { |
| 632 if (it.interface()->IsModule()) { |
| 633 Handle<Object> value = it.interface()->Instance(); |
| 634 ASSERT(!value.is_null()); |
| 635 JSReceiver::SetProperty(instance, it.name(), value, attr, kStrictMode); |
| 636 } else { |
| 637 // TODO(rossberg): set proper getters instead of undefined... |
| 638 // instance->DefineAccessor(*it.name(), ACCESSOR_GETTER, *getter, attr); |
| 639 Handle<Object> value(isolate()->heap()->undefined_value()); |
| 640 JSReceiver::SetProperty(instance, it.name(), value, attr, kStrictMode); |
| 641 } |
| 642 } |
| 643 USE(instance->PreventExtensions()); |
| 643 } | 644 } |
| 644 | 645 |
| 645 | 646 |
| 646 void FullCodeGenerator::VisitModuleVariable(ModuleVariable* module) { | 647 void FullCodeGenerator::VisitModuleVariable(ModuleVariable* module) { |
| 647 // TODO(rossberg) | 648 // Noting to do. |
| 649 // The instance object is resolved statically through the module's interface. |
| 648 } | 650 } |
| 649 | 651 |
| 650 | 652 |
| 651 void FullCodeGenerator::VisitModulePath(ModulePath* module) { | 653 void FullCodeGenerator::VisitModulePath(ModulePath* module) { |
| 652 // TODO(rossberg) | 654 // Noting to do. |
| 655 // The instance object is resolved statically through the module's interface. |
| 653 } | 656 } |
| 654 | 657 |
| 655 | 658 |
| 656 void FullCodeGenerator::VisitModuleUrl(ModuleUrl* decl) { | 659 void FullCodeGenerator::VisitModuleUrl(ModuleUrl* decl) { |
| 657 // TODO(rossberg) | 660 // TODO(rossberg) |
| 658 } | 661 } |
| 659 | 662 |
| 660 | 663 |
| 661 int FullCodeGenerator::DeclareGlobalsFlags() { | 664 int FullCodeGenerator::DeclareGlobalsFlags() { |
| 662 ASSERT(DeclareGlobalsLanguageMode::is_valid(language_mode())); | 665 ASSERT(DeclareGlobalsLanguageMode::is_valid(language_mode())); |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 904 } | 907 } |
| 905 | 908 |
| 906 | 909 |
| 907 void FullCodeGenerator::VisitBlock(Block* stmt) { | 910 void FullCodeGenerator::VisitBlock(Block* stmt) { |
| 908 Comment cmnt(masm_, "[ Block"); | 911 Comment cmnt(masm_, "[ Block"); |
| 909 NestedBlock nested_block(this, stmt); | 912 NestedBlock nested_block(this, stmt); |
| 910 SetStatementPosition(stmt); | 913 SetStatementPosition(stmt); |
| 911 | 914 |
| 912 Scope* saved_scope = scope(); | 915 Scope* saved_scope = scope(); |
| 913 // Push a block context when entering a block with block scoped variables. | 916 // Push a block context when entering a block with block scoped variables. |
| 914 if (stmt->block_scope() != NULL) { | 917 if (stmt->scope() != NULL) { |
| 915 { Comment cmnt(masm_, "[ Extend block context"); | 918 { Comment cmnt(masm_, "[ Extend block context"); |
| 916 scope_ = stmt->block_scope(); | 919 scope_ = stmt->scope(); |
| 917 Handle<ScopeInfo> scope_info = scope_->GetScopeInfo(); | 920 Handle<ScopeInfo> scope_info = scope_->GetScopeInfo(); |
| 918 int heap_slots = scope_info->ContextLength() - Context::MIN_CONTEXT_SLOTS; | 921 int heap_slots = scope_info->ContextLength() - Context::MIN_CONTEXT_SLOTS; |
| 919 __ Push(scope_info); | 922 __ Push(scope_info); |
| 920 PushFunctionArgumentForContextAllocation(); | 923 PushFunctionArgumentForContextAllocation(); |
| 921 if (heap_slots <= FastNewBlockContextStub::kMaximumSlots) { | 924 if (heap_slots <= FastNewBlockContextStub::kMaximumSlots) { |
| 922 FastNewBlockContextStub stub(heap_slots); | 925 FastNewBlockContextStub stub(heap_slots); |
| 923 __ CallStub(&stub); | 926 __ CallStub(&stub); |
| 924 } else { | 927 } else { |
| 925 __ CallRuntime(Runtime::kPushBlockContext, 2); | 928 __ CallRuntime(Runtime::kPushBlockContext, 2); |
| 926 } | 929 } |
| 927 | 930 |
| 928 // Replace the context stored in the frame. | 931 // Replace the context stored in the frame. |
| 929 StoreToFrameField(StandardFrameConstants::kContextOffset, | 932 StoreToFrameField(StandardFrameConstants::kContextOffset, |
| 930 context_register()); | 933 context_register()); |
| 931 } | 934 } |
| 932 { Comment cmnt(masm_, "[ Declarations"); | 935 { Comment cmnt(masm_, "[ Declarations"); |
| 933 VisitDeclarations(scope_->declarations()); | 936 VisitDeclarations(scope_->declarations()); |
| 934 } | 937 } |
| 935 } | 938 } |
| 936 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS); | 939 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS); |
| 937 VisitStatements(stmt->statements()); | 940 VisitStatements(stmt->statements()); |
| 938 scope_ = saved_scope; | 941 scope_ = saved_scope; |
| 939 __ bind(nested_block.break_label()); | 942 __ bind(nested_block.break_label()); |
| 940 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS); | 943 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS); |
| 941 | 944 |
| 942 // Pop block context if necessary. | 945 // Pop block context if necessary. |
| 943 if (stmt->block_scope() != NULL) { | 946 if (stmt->scope() != NULL) { |
| 944 LoadContextField(context_register(), Context::PREVIOUS_INDEX); | 947 LoadContextField(context_register(), Context::PREVIOUS_INDEX); |
| 945 // Update local stack frame context field. | 948 // Update local stack frame context field. |
| 946 StoreToFrameField(StandardFrameConstants::kContextOffset, | 949 StoreToFrameField(StandardFrameConstants::kContextOffset, |
| 947 context_register()); | 950 context_register()); |
| 948 } | 951 } |
| 949 } | 952 } |
| 950 | 953 |
| 951 | 954 |
| 952 void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) { | 955 void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) { |
| 953 Comment cmnt(masm_, "[ ExpressionStatement"); | 956 Comment cmnt(masm_, "[ ExpressionStatement"); |
| (...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1419 } | 1422 } |
| 1420 | 1423 |
| 1421 return false; | 1424 return false; |
| 1422 } | 1425 } |
| 1423 | 1426 |
| 1424 | 1427 |
| 1425 #undef __ | 1428 #undef __ |
| 1426 | 1429 |
| 1427 | 1430 |
| 1428 } } // namespace v8::internal | 1431 } } // namespace v8::internal |
| OLD | NEW |