OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 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 14 matching lines...) Expand all Loading... |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 #include "v8.h" | 28 #include "v8.h" |
29 | 29 |
30 #include "bootstrapper.h" | 30 #include "bootstrapper.h" |
31 #include "codegen-inl.h" | 31 #include "codegen-inl.h" |
32 #include "debug.h" | 32 #include "debug.h" |
33 #include "scopes.h" | 33 #include "scopes.h" |
34 #include "runtime.h" | 34 #include "runtime.h" |
35 #include "parser.h" | |
36 | 35 |
37 namespace v8 { namespace internal { | 36 namespace v8 { namespace internal { |
38 | 37 |
39 #define __ masm_-> | 38 #define __ masm_-> |
40 | 39 |
41 // ------------------------------------------------------------------------- | 40 // ------------------------------------------------------------------------- |
42 // CodeGenState implementation. | 41 // CodeGenState implementation. |
43 | 42 |
44 CodeGenState::CodeGenState(CodeGenerator* owner) | 43 CodeGenState::CodeGenState(CodeGenerator* owner) |
45 : owner_(owner), | 44 : owner_(owner), |
(...skipping 3448 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3494 deferred->enter()->Branch(equal, &literals, not_taken); | 3493 deferred->enter()->Branch(equal, &literals, not_taken); |
3495 | 3494 |
3496 literals.Unuse(); | 3495 literals.Unuse(); |
3497 // The deferred code returns the boilerplate object. | 3496 // The deferred code returns the boilerplate object. |
3498 deferred->BindExit(&boilerplate); | 3497 deferred->BindExit(&boilerplate); |
3499 | 3498 |
3500 // Push the boilerplate object. | 3499 // Push the boilerplate object. |
3501 frame_->Push(&boilerplate); | 3500 frame_->Push(&boilerplate); |
3502 // Clone the boilerplate object. | 3501 // Clone the boilerplate object. |
3503 Result clone = | 3502 Result clone = |
3504 frame_->CallRuntime(Runtime::kCloneLiteralBoilerplate, 1); | 3503 frame_->CallRuntime(Runtime::kCloneObjectLiteralBoilerplate, 1); |
3505 // Push the newly cloned literal object as the result. | 3504 // Push the newly cloned literal object as the result. |
3506 frame_->Push(&clone); | 3505 frame_->Push(&clone); |
3507 | 3506 |
3508 for (int i = 0; i < node->properties()->length(); i++) { | 3507 for (int i = 0; i < node->properties()->length(); i++) { |
3509 ObjectLiteral::Property* property = node->properties()->at(i); | 3508 ObjectLiteral::Property* property = node->properties()->at(i); |
3510 switch (property->kind()) { | 3509 switch (property->kind()) { |
3511 case ObjectLiteral::Property::CONSTANT: | 3510 case ObjectLiteral::Property::CONSTANT: break; |
3512 break; | |
3513 case ObjectLiteral::Property::MATERIALIZED_LITERAL: | |
3514 if (CompileTimeValue::IsCompileTimeValue(property->value())) break; | |
3515 // else fall through. | |
3516 case ObjectLiteral::Property::COMPUTED: { | 3511 case ObjectLiteral::Property::COMPUTED: { |
3517 Handle<Object> key(property->key()->handle()); | 3512 Handle<Object> key(property->key()->handle()); |
3518 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); | 3513 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
3519 if (key->IsSymbol()) { | 3514 if (key->IsSymbol()) { |
3520 // Duplicate the object as the IC receiver. | 3515 // Duplicate the object as the IC receiver. |
3521 frame_->Dup(); | 3516 frame_->Dup(); |
3522 Load(property->value()); | 3517 Load(property->value()); |
3523 Result value = frame_->Pop(); | 3518 Result value = frame_->Pop(); |
3524 value.ToRegister(eax); | 3519 value.ToRegister(eax); |
3525 | 3520 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3563 Result ignored = frame_->CallRuntime(Runtime::kDefineAccessor, 4); | 3558 Result ignored = frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
3564 // Ignore the result. | 3559 // Ignore the result. |
3565 break; | 3560 break; |
3566 } | 3561 } |
3567 default: UNREACHABLE(); | 3562 default: UNREACHABLE(); |
3568 } | 3563 } |
3569 } | 3564 } |
3570 } | 3565 } |
3571 | 3566 |
3572 | 3567 |
3573 // This deferred code stub will be used for creating the boilerplate | |
3574 // by calling Runtime_CreateArrayLiteralBoilerplate. | |
3575 // Each created boilerplate is stored in the JSFunction and they are | |
3576 // therefore context dependent. | |
3577 class DeferredArrayLiteral: public DeferredCode { | |
3578 public: | |
3579 DeferredArrayLiteral(CodeGenerator* generator, | |
3580 ArrayLiteral* node) | |
3581 : DeferredCode(generator), node_(node) { | |
3582 set_comment("[ DeferredArrayLiteral"); | |
3583 } | |
3584 | |
3585 virtual void Generate(); | |
3586 | |
3587 private: | |
3588 ArrayLiteral* node_; | |
3589 }; | |
3590 | |
3591 | |
3592 void DeferredArrayLiteral::Generate() { | |
3593 Result literals(generator()); | |
3594 enter()->Bind(&literals); | |
3595 // Since the entry is undefined we call the runtime system to | |
3596 // compute the literal. | |
3597 | |
3598 VirtualFrame* frame = generator()->frame(); | |
3599 // Literal array (0). | |
3600 frame->Push(&literals); | |
3601 // Literal index (1). | |
3602 frame->Push(Smi::FromInt(node_->literal_index())); | |
3603 // Constant properties (2). | |
3604 frame->Push(node_->literals()); | |
3605 Result boilerplate = | |
3606 frame->CallRuntime(Runtime::kCreateArrayLiteralBoilerplate, 3); | |
3607 exit_.Jump(&boilerplate); | |
3608 } | |
3609 | |
3610 | |
3611 void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) { | 3568 void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) { |
3612 Comment cmnt(masm_, "[ ArrayLiteral"); | 3569 Comment cmnt(masm_, "[ ArrayLiteral"); |
3613 DeferredArrayLiteral* deferred = new DeferredArrayLiteral(this, node); | |
3614 | 3570 |
3615 // Retrieve the literals array and check the allocated entry. Begin | 3571 // Call the runtime to create the array literal. |
3616 // with a writable copy of the function of this activation in a | 3572 frame_->Push(node->literals()); |
3617 // register. | 3573 // Load the literals array of the current function. |
3618 frame_->PushFunction(); | 3574 frame_->PushFunction(); |
3619 Result literals = frame_->Pop(); | 3575 Result literals = frame_->Pop(); |
3620 literals.ToRegister(); | 3576 literals.ToRegister(); |
3621 frame_->Spill(literals.reg()); | 3577 frame_->Spill(literals.reg()); // Make it writable. |
3622 | |
3623 // Load the literals array of the function. | |
3624 __ mov(literals.reg(), | 3578 __ mov(literals.reg(), |
3625 FieldOperand(literals.reg(), JSFunction::kLiteralsOffset)); | 3579 FieldOperand(literals.reg(), JSFunction::kLiteralsOffset)); |
3626 | 3580 frame_->Push(&literals); |
3627 // Load the literal at the ast saved index. | 3581 Result array = frame_->CallRuntime(Runtime::kCreateArrayLiteral, 2); |
3628 int literal_offset = | |
3629 FixedArray::kHeaderSize + node->literal_index() * kPointerSize; | |
3630 Result boilerplate = allocator_->Allocate(); | |
3631 ASSERT(boilerplate.is_valid()); | |
3632 __ mov(boilerplate.reg(), FieldOperand(literals.reg(), literal_offset)); | |
3633 | |
3634 // Check whether we need to materialize the object literal boilerplate. | |
3635 // If so, jump to the deferred code passing the literals array. | |
3636 __ cmp(boilerplate.reg(), Factory::undefined_value()); | |
3637 deferred->enter()->Branch(equal, &literals, not_taken); | |
3638 | |
3639 literals.Unuse(); | |
3640 // The deferred code returns the boilerplate object. | |
3641 deferred->BindExit(&boilerplate); | |
3642 | 3582 |
3643 // Push the resulting array literal on the stack. | 3583 // Push the resulting array literal on the stack. |
3644 frame_->Push(&boilerplate); | 3584 frame_->Push(&array); |
3645 | |
3646 // Clone the boilerplate object. | |
3647 Result clone = | |
3648 frame_->CallRuntime(Runtime::kCloneLiteralBoilerplate, 1); | |
3649 // Push the newly cloned literal object as the result. | |
3650 frame_->Push(&clone); | |
3651 | 3585 |
3652 // Generate code to set the elements in the array that are not | 3586 // Generate code to set the elements in the array that are not |
3653 // literals. | 3587 // literals. |
3654 for (int i = 0; i < node->values()->length(); i++) { | 3588 for (int i = 0; i < node->values()->length(); i++) { |
3655 Expression* value = node->values()->at(i); | 3589 Expression* value = node->values()->at(i); |
3656 | 3590 |
3657 // If value is a literal the property value is already set in the | 3591 // If value is literal the property value is already set in the |
3658 // boilerplate object. | 3592 // boilerplate object. |
3659 if (value->AsLiteral() != NULL) continue; | 3593 if (value->AsLiteral() == NULL) { |
3660 // If value is a materialized literal the property value is already set | 3594 // The property must be set by generated code. |
3661 // in the boilerplate object if it is simple. | 3595 Load(value); |
3662 if (CompileTimeValue::IsCompileTimeValue(value)) continue; | |
3663 | 3596 |
3664 // The property must be set by generated code. | 3597 // Get the property value off the stack. |
3665 Load(value); | 3598 Result prop_value = frame_->Pop(); |
| 3599 prop_value.ToRegister(); |
3666 | 3600 |
3667 // Get the property value off the stack. | 3601 // Fetch the array literal while leaving a copy on the stack and |
3668 Result prop_value = frame_->Pop(); | 3602 // use it to get the elements array. |
3669 prop_value.ToRegister(); | 3603 frame_->Dup(); |
| 3604 Result elements = frame_->Pop(); |
| 3605 elements.ToRegister(); |
| 3606 frame_->Spill(elements.reg()); |
| 3607 // Get the elements array. |
| 3608 __ mov(elements.reg(), |
| 3609 FieldOperand(elements.reg(), JSObject::kElementsOffset)); |
3670 | 3610 |
3671 // Fetch the array literal while leaving a copy on the stack and | 3611 // Write to the indexed properties array. |
3672 // use it to get the elements array. | 3612 int offset = i * kPointerSize + Array::kHeaderSize; |
3673 frame_->Dup(); | 3613 __ mov(FieldOperand(elements.reg(), offset), prop_value.reg()); |
3674 Result elements = frame_->Pop(); | |
3675 elements.ToRegister(); | |
3676 frame_->Spill(elements.reg()); | |
3677 // Get the elements array. | |
3678 __ mov(elements.reg(), | |
3679 FieldOperand(elements.reg(), JSObject::kElementsOffset)); | |
3680 | 3614 |
3681 // Write to the indexed properties array. | 3615 // Update the write barrier for the array address. |
3682 int offset = i * kPointerSize + Array::kHeaderSize; | 3616 frame_->Spill(prop_value.reg()); // Overwritten by the write barrier. |
3683 __ mov(FieldOperand(elements.reg(), offset), prop_value.reg()); | 3617 Result scratch = allocator_->Allocate(); |
3684 | 3618 ASSERT(scratch.is_valid()); |
3685 // Update the write barrier for the array address. | 3619 __ RecordWrite(elements.reg(), offset, prop_value.reg(), scratch.reg()); |
3686 frame_->Spill(prop_value.reg()); // Overwritten by the write barrier. | 3620 } |
3687 Result scratch = allocator_->Allocate(); | |
3688 ASSERT(scratch.is_valid()); | |
3689 __ RecordWrite(elements.reg(), offset, prop_value.reg(), scratch.reg()); | |
3690 } | 3621 } |
3691 } | 3622 } |
3692 | 3623 |
3693 | 3624 |
3694 void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) { | 3625 void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) { |
3695 ASSERT(!in_spilled_code()); | 3626 ASSERT(!in_spilled_code()); |
3696 // Call runtime routine to allocate the catch extension object and | 3627 // Call runtime routine to allocate the catch extension object and |
3697 // assign the exception value to the catch variable. | 3628 // assign the exception value to the catch variable. |
3698 Comment cmnt(masm_, "[ CatchExtensionObject"); | 3629 Comment cmnt(masm_, "[ CatchExtensionObject"); |
3699 Load(node->key()); | 3630 Load(node->key()); |
(...skipping 3252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6952 | 6883 |
6953 // Slow-case: Go through the JavaScript implementation. | 6884 // Slow-case: Go through the JavaScript implementation. |
6954 __ bind(&slow); | 6885 __ bind(&slow); |
6955 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION); | 6886 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION); |
6956 } | 6887 } |
6957 | 6888 |
6958 | 6889 |
6959 #undef __ | 6890 #undef __ |
6960 | 6891 |
6961 } } // namespace v8::internal | 6892 } } // namespace v8::internal |
OLD | NEW |