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