OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/compiler/ast-graph-builder.h" | 5 #include "src/compiler/ast-graph-builder.h" |
6 | 6 |
7 #include "src/ast/scopes.h" | 7 #include "src/ast/scopes.h" |
8 #include "src/compiler.h" | 8 #include "src/compiler.h" |
9 #include "src/compiler/ast-loop-assignment-analyzer.h" | 9 #include "src/compiler/ast-loop-assignment-analyzer.h" |
10 #include "src/compiler/control-builders.h" | 10 #include "src/compiler/control-builders.h" |
(...skipping 1065 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1076 expr->Accept(this); | 1076 expr->Accept(this); |
1077 } else { | 1077 } else { |
1078 ast_context()->ProduceValue(jsgraph()->UndefinedConstant()); | 1078 ast_context()->ProduceValue(jsgraph()->UndefinedConstant()); |
1079 } | 1079 } |
1080 } | 1080 } |
1081 | 1081 |
1082 | 1082 |
1083 void AstGraphBuilder::VisitVariableDeclaration(VariableDeclaration* decl) { | 1083 void AstGraphBuilder::VisitVariableDeclaration(VariableDeclaration* decl) { |
1084 Variable* variable = decl->proxy()->var(); | 1084 Variable* variable = decl->proxy()->var(); |
1085 VariableMode mode = decl->mode(); | 1085 VariableMode mode = decl->mode(); |
1086 bool hole_init = mode == CONST || mode == CONST_LEGACY || mode == LET; | 1086 bool hole_init = mode == CONST || mode == LET; |
1087 switch (variable->location()) { | 1087 switch (variable->location()) { |
1088 case VariableLocation::GLOBAL: | 1088 case VariableLocation::GLOBAL: |
1089 case VariableLocation::UNALLOCATED: { | 1089 case VariableLocation::UNALLOCATED: { |
1090 Handle<Oddball> value = variable->binding_needs_init() | 1090 Handle<Oddball> value = variable->binding_needs_init() |
1091 ? isolate()->factory()->the_hole_value() | 1091 ? isolate()->factory()->the_hole_value() |
1092 : isolate()->factory()->undefined_value(); | 1092 : isolate()->factory()->undefined_value(); |
1093 globals()->push_back(variable->name()); | 1093 globals()->push_back(variable->name()); |
1094 globals()->push_back(value); | 1094 globals()->push_back(value); |
1095 break; | 1095 break; |
1096 } | 1096 } |
(...skipping 2189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3286 | 3286 |
3287 // Assign the object to the {new.target} variable. This should never lazy | 3287 // Assign the object to the {new.target} variable. This should never lazy |
3288 // deopt, so it is fine to send invalid bailout id. | 3288 // deopt, so it is fine to send invalid bailout id. |
3289 FrameStateBeforeAndAfter states(this, BailoutId::None()); | 3289 FrameStateBeforeAndAfter states(this, BailoutId::None()); |
3290 BuildVariableAssignment(new_target_var, object, Token::INIT, VectorSlotPair(), | 3290 BuildVariableAssignment(new_target_var, object, Token::INIT, VectorSlotPair(), |
3291 BailoutId::None(), states); | 3291 BailoutId::None(), states); |
3292 return object; | 3292 return object; |
3293 } | 3293 } |
3294 | 3294 |
3295 | 3295 |
3296 Node* AstGraphBuilder::BuildHoleCheckSilent(Node* value, Node* for_hole, | |
3297 Node* not_hole) { | |
3298 Node* the_hole = jsgraph()->TheHoleConstant(); | |
3299 Node* check = NewNode(javascript()->StrictEqual(), value, the_hole); | |
3300 return NewNode( | |
3301 common()->Select(MachineRepresentation::kTagged, BranchHint::kFalse), | |
3302 check, for_hole, not_hole); | |
3303 } | |
3304 | |
3305 | |
3306 Node* AstGraphBuilder::BuildHoleCheckThenThrow(Node* value, Variable* variable, | 3296 Node* AstGraphBuilder::BuildHoleCheckThenThrow(Node* value, Variable* variable, |
3307 Node* not_hole, | 3297 Node* not_hole, |
3308 BailoutId bailout_id) { | 3298 BailoutId bailout_id) { |
3309 IfBuilder hole_check(this); | 3299 IfBuilder hole_check(this); |
3310 Node* the_hole = jsgraph()->TheHoleConstant(); | 3300 Node* the_hole = jsgraph()->TheHoleConstant(); |
3311 Node* check = NewNode(javascript()->StrictEqual(), value, the_hole); | 3301 Node* check = NewNode(javascript()->StrictEqual(), value, the_hole); |
3312 hole_check.If(check); | 3302 hole_check.If(check); |
3313 hole_check.Then(); | 3303 hole_check.Then(); |
3314 Node* error = BuildThrowReferenceError(variable, bailout_id); | 3304 Node* error = BuildThrowReferenceError(variable, bailout_id); |
3315 environment()->Push(error); | 3305 environment()->Push(error); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3369 Handle<Name> name = variable->name(); | 3359 Handle<Name> name = variable->name(); |
3370 if (Node* node = TryLoadGlobalConstant(name)) return node; | 3360 if (Node* node = TryLoadGlobalConstant(name)) return node; |
3371 Node* value = BuildGlobalLoad(name, feedback, typeof_mode); | 3361 Node* value = BuildGlobalLoad(name, feedback, typeof_mode); |
3372 states.AddToNode(value, bailout_id, combine); | 3362 states.AddToNode(value, bailout_id, combine); |
3373 return value; | 3363 return value; |
3374 } | 3364 } |
3375 case VariableLocation::PARAMETER: | 3365 case VariableLocation::PARAMETER: |
3376 case VariableLocation::LOCAL: { | 3366 case VariableLocation::LOCAL: { |
3377 // Local var, const, or let variable. | 3367 // Local var, const, or let variable. |
3378 Node* value = environment()->Lookup(variable); | 3368 Node* value = environment()->Lookup(variable); |
3379 if (mode == CONST_LEGACY) { | 3369 if (mode == LET || mode == CONST) { |
3380 // Perform check for uninitialized legacy const variables. | |
3381 if (value->op() == the_hole->op()) { | |
3382 value = jsgraph()->UndefinedConstant(); | |
3383 } else if (value->opcode() == IrOpcode::kPhi) { | |
3384 Node* undefined = jsgraph()->UndefinedConstant(); | |
3385 value = BuildHoleCheckSilent(value, undefined, value); | |
3386 } | |
3387 } else if (mode == LET || mode == CONST) { | |
3388 // Perform check for uninitialized let/const variables. | 3370 // Perform check for uninitialized let/const variables. |
3389 if (value->op() == the_hole->op()) { | 3371 if (value->op() == the_hole->op()) { |
3390 value = BuildThrowReferenceError(variable, bailout_id); | 3372 value = BuildThrowReferenceError(variable, bailout_id); |
3391 } else if (value->opcode() == IrOpcode::kPhi) { | 3373 } else if (value->opcode() == IrOpcode::kPhi) { |
3392 value = BuildHoleCheckThenThrow(value, variable, value, bailout_id); | 3374 value = BuildHoleCheckThenThrow(value, variable, value, bailout_id); |
3393 } | 3375 } |
3394 } | 3376 } |
3395 return value; | 3377 return value; |
3396 } | 3378 } |
3397 case VariableLocation::CONTEXT: { | 3379 case VariableLocation::CONTEXT: { |
3398 // Context variable (potentially up the context chain). | 3380 // Context variable (potentially up the context chain). |
3399 int depth = current_scope()->ContextChainLength(variable->scope()); | 3381 int depth = current_scope()->ContextChainLength(variable->scope()); |
3400 bool immutable = variable->maybe_assigned() == kNotAssigned; | 3382 bool immutable = variable->maybe_assigned() == kNotAssigned; |
3401 const Operator* op = | 3383 const Operator* op = |
3402 javascript()->LoadContext(depth, variable->index(), immutable); | 3384 javascript()->LoadContext(depth, variable->index(), immutable); |
3403 Node* value = NewNode(op, current_context()); | 3385 Node* value = NewNode(op, current_context()); |
3404 // TODO(titzer): initialization checks are redundant for already | 3386 // TODO(titzer): initialization checks are redundant for already |
3405 // initialized immutable context loads, but only specialization knows. | 3387 // initialized immutable context loads, but only specialization knows. |
3406 // Maybe specializer should be a parameter to the graph builder? | 3388 // Maybe specializer should be a parameter to the graph builder? |
3407 if (mode == CONST_LEGACY) { | 3389 if (mode == LET || mode == CONST) { |
3408 // Perform check for uninitialized legacy const variables. | |
3409 Node* undefined = jsgraph()->UndefinedConstant(); | |
3410 value = BuildHoleCheckSilent(value, undefined, value); | |
3411 } else if (mode == LET || mode == CONST) { | |
3412 // Perform check for uninitialized let/const variables. | 3390 // Perform check for uninitialized let/const variables. |
3413 value = BuildHoleCheckThenThrow(value, variable, value, bailout_id); | 3391 value = BuildHoleCheckThenThrow(value, variable, value, bailout_id); |
3414 } | 3392 } |
3415 return value; | 3393 return value; |
3416 } | 3394 } |
3417 case VariableLocation::LOOKUP: { | 3395 case VariableLocation::LOOKUP: { |
3418 // Dynamic lookup of context variable (anywhere in the chain). | 3396 // Dynamic lookup of context variable (anywhere in the chain). |
3419 Handle<String> name = variable->name(); | 3397 Handle<String> name = variable->name(); |
3420 if (Node* node = | 3398 if (Node* node = |
3421 TryLoadDynamicVariable(variable, name, bailout_id, states, | 3399 TryLoadDynamicVariable(variable, name, bailout_id, states, |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3478 case VariableLocation::UNALLOCATED: { | 3456 case VariableLocation::UNALLOCATED: { |
3479 // Global var, const, or let variable. | 3457 // Global var, const, or let variable. |
3480 Handle<Name> name = variable->name(); | 3458 Handle<Name> name = variable->name(); |
3481 Node* store = BuildGlobalStore(name, value, feedback); | 3459 Node* store = BuildGlobalStore(name, value, feedback); |
3482 states.AddToNode(store, bailout_id, combine); | 3460 states.AddToNode(store, bailout_id, combine); |
3483 return store; | 3461 return store; |
3484 } | 3462 } |
3485 case VariableLocation::PARAMETER: | 3463 case VariableLocation::PARAMETER: |
3486 case VariableLocation::LOCAL: | 3464 case VariableLocation::LOCAL: |
3487 // Local var, const, or let variable. | 3465 // Local var, const, or let variable. |
3488 if (mode == CONST_LEGACY && op == Token::INIT) { | 3466 if (mode == CONST_LEGACY && op != Token::INIT) { |
3489 // Perform an initialization check for legacy const variables. | |
3490 Node* current = environment()->Lookup(variable); | |
3491 if (current->op() != the_hole->op()) { | |
3492 value = BuildHoleCheckSilent(current, value, current); | |
3493 } | |
3494 } else if (mode == CONST_LEGACY && op != Token::INIT) { | |
3495 // Non-initializing assignment to legacy const is | 3467 // Non-initializing assignment to legacy const is |
3496 // - exception in strict mode. | 3468 // - exception in strict mode. |
3497 // - ignored in sloppy mode. | 3469 // - ignored in sloppy mode. |
3498 if (is_strict(language_mode())) { | 3470 if (is_strict(language_mode())) { |
3499 return BuildThrowConstAssignError(bailout_id); | 3471 return BuildThrowConstAssignError(bailout_id); |
3500 } | 3472 } |
3501 return value; | 3473 return value; |
3502 } else if (mode == LET && op == Token::INIT) { | 3474 } else if (mode == LET && op == Token::INIT) { |
3503 // No initialization check needed because scoping guarantees it. Note | 3475 // No initialization check needed because scoping guarantees it. Note |
3504 // that we still perform a lookup to keep the variable live, because | 3476 // that we still perform a lookup to keep the variable live, because |
(...skipping 24 matching lines...) Expand all Loading... |
3529 } else if (current->opcode() == IrOpcode::kPhi) { | 3501 } else if (current->opcode() == IrOpcode::kPhi) { |
3530 BuildHoleCheckThenThrow(current, variable, value, bailout_id); | 3502 BuildHoleCheckThenThrow(current, variable, value, bailout_id); |
3531 } | 3503 } |
3532 return BuildThrowConstAssignError(bailout_id); | 3504 return BuildThrowConstAssignError(bailout_id); |
3533 } | 3505 } |
3534 environment()->Bind(variable, value); | 3506 environment()->Bind(variable, value); |
3535 return value; | 3507 return value; |
3536 case VariableLocation::CONTEXT: { | 3508 case VariableLocation::CONTEXT: { |
3537 // Context variable (potentially up the context chain). | 3509 // Context variable (potentially up the context chain). |
3538 int depth = current_scope()->ContextChainLength(variable->scope()); | 3510 int depth = current_scope()->ContextChainLength(variable->scope()); |
3539 if (mode == CONST_LEGACY && op == Token::INIT) { | 3511 if (mode == CONST_LEGACY && op != Token::INIT) { |
3540 // Perform an initialization check for legacy const variables. | |
3541 const Operator* op = | |
3542 javascript()->LoadContext(depth, variable->index(), false); | |
3543 Node* current = NewNode(op, current_context()); | |
3544 value = BuildHoleCheckSilent(current, value, current); | |
3545 } else if (mode == CONST_LEGACY && op != Token::INIT) { | |
3546 // Non-initializing assignment to legacy const is | 3512 // Non-initializing assignment to legacy const is |
3547 // - exception in strict mode. | 3513 // - exception in strict mode. |
3548 // - ignored in sloppy mode. | 3514 // - ignored in sloppy mode. |
3549 if (is_strict(language_mode())) { | 3515 if (is_strict(language_mode())) { |
3550 return BuildThrowConstAssignError(bailout_id); | 3516 return BuildThrowConstAssignError(bailout_id); |
3551 } | 3517 } |
3552 return value; | 3518 return value; |
3553 } else if (mode == LET && op != Token::INIT) { | 3519 } else if (mode == LET && op != Token::INIT) { |
3554 // Perform an initialization check for let declared variables. | 3520 // Perform an initialization check for let declared variables. |
3555 const Operator* op = | 3521 const Operator* op = |
(...skipping 17 matching lines...) Expand all Loading... |
3573 Node* current = NewNode(op, current_context()); | 3539 Node* current = NewNode(op, current_context()); |
3574 BuildHoleCheckThenThrow(current, variable, value, bailout_id); | 3540 BuildHoleCheckThenThrow(current, variable, value, bailout_id); |
3575 return BuildThrowConstAssignError(bailout_id); | 3541 return BuildThrowConstAssignError(bailout_id); |
3576 } | 3542 } |
3577 const Operator* op = javascript()->StoreContext(depth, variable->index()); | 3543 const Operator* op = javascript()->StoreContext(depth, variable->index()); |
3578 return NewNode(op, current_context(), value); | 3544 return NewNode(op, current_context(), value); |
3579 } | 3545 } |
3580 case VariableLocation::LOOKUP: { | 3546 case VariableLocation::LOOKUP: { |
3581 // Dynamic lookup of context variable (anywhere in the chain). | 3547 // Dynamic lookup of context variable (anywhere in the chain). |
3582 Handle<Name> name = variable->name(); | 3548 Handle<Name> name = variable->name(); |
3583 // TODO(mstarzinger): Use Runtime::kInitializeLegacyConstLookupSlot for | |
3584 // initializations of const declarations. | |
3585 Node* store = BuildDynamicStore(name, value); | 3549 Node* store = BuildDynamicStore(name, value); |
3586 PrepareFrameState(store, bailout_id, combine); | 3550 PrepareFrameState(store, bailout_id, combine); |
3587 return store; | 3551 return store; |
3588 } | 3552 } |
3589 } | 3553 } |
3590 UNREACHABLE(); | 3554 UNREACHABLE(); |
3591 return nullptr; | 3555 return nullptr; |
3592 } | 3556 } |
3593 | 3557 |
3594 | 3558 |
(...skipping 776 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4371 // Phi does not exist yet, introduce one. | 4335 // Phi does not exist yet, introduce one. |
4372 value = NewPhi(inputs, value, control); | 4336 value = NewPhi(inputs, value, control); |
4373 value->ReplaceInput(inputs - 1, other); | 4337 value->ReplaceInput(inputs - 1, other); |
4374 } | 4338 } |
4375 return value; | 4339 return value; |
4376 } | 4340 } |
4377 | 4341 |
4378 } // namespace compiler | 4342 } // namespace compiler |
4379 } // namespace internal | 4343 } // namespace internal |
4380 } // namespace v8 | 4344 } // namespace v8 |
OLD | NEW |