Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 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 |
| 11 // with the distribution. | 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
| 15 // | 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 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 "typing.h" | 28 #include "typing.h" |
| 29 | 29 |
| 30 #include "frames.h" | |
| 31 #include "frames-inl.h" | |
| 30 #include "parser.h" // for CompileTimeValue; TODO(rossberg): should move | 32 #include "parser.h" // for CompileTimeValue; TODO(rossberg): should move |
| 31 #include "scopes.h" | 33 #include "scopes.h" |
| 32 | 34 |
| 33 namespace v8 { | 35 namespace v8 { |
| 34 namespace internal { | 36 namespace internal { |
| 35 | 37 |
| 36 | 38 |
| 37 AstTyper::AstTyper(CompilationInfo* info) | 39 AstTyper::AstTyper(CompilationInfo* info) |
| 38 : info_(info), | 40 : info_(info), |
| 39 oracle_( | 41 oracle_( |
| 40 Handle<Code>(info->closure()->shared()->code()), | 42 Handle<Code>(info->closure()->shared()->code()), |
| 41 Handle<Context>(info->closure()->context()->native_context()), | 43 Handle<Context>(info->closure()->context()->native_context()), |
| 42 info->isolate(), | 44 info->isolate(), |
| 43 info->zone()), | 45 info->zone()), |
| 44 store_(info->zone()) { | 46 store_(info->zone()), |
| 47 parameter_types_(NULL), | |
| 48 stack_local_types_(NULL) { | |
| 45 InitializeAstVisitor(info->isolate()); | 49 InitializeAstVisitor(info->isolate()); |
| 50 if (info_->is_osr()) ObserveTypesOnStack(); | |
| 46 } | 51 } |
| 47 | 52 |
| 48 | 53 |
| 49 #define RECURSE(call) \ | 54 #define RECURSE(call) \ |
| 50 do { \ | 55 do { \ |
| 51 ASSERT(!visitor->HasStackOverflow()); \ | 56 ASSERT(!visitor->HasStackOverflow()); \ |
| 52 call; \ | 57 call; \ |
| 53 if (visitor->HasStackOverflow()) return; \ | 58 if (visitor->HasStackOverflow()) return; \ |
| 54 } while (false) | 59 } while (false) |
| 55 | 60 |
| 56 void AstTyper::Run(CompilationInfo* info) { | 61 void AstTyper::Run(CompilationInfo* info) { |
| 57 AstTyper* visitor = new(info->zone()) AstTyper(info); | 62 AstTyper* visitor = new(info->zone()) AstTyper(info); |
| 58 Scope* scope = info->scope(); | 63 Scope* scope = info->scope(); |
| 59 | 64 |
| 60 // Handle implicit declaration of the function name in named function | 65 // Handle implicit declaration of the function name in named function |
| 61 // expressions before other declarations. | 66 // expressions before other declarations. |
| 62 if (scope->is_function_scope() && scope->function() != NULL) { | 67 if (scope->is_function_scope() && scope->function() != NULL) { |
| 63 RECURSE(visitor->VisitVariableDeclaration(scope->function())); | 68 RECURSE(visitor->VisitVariableDeclaration(scope->function())); |
| 64 } | 69 } |
| 65 RECURSE(visitor->VisitDeclarations(scope->declarations())); | 70 RECURSE(visitor->VisitDeclarations(scope->declarations())); |
| 66 RECURSE(visitor->VisitStatements(info->function()->body())); | 71 RECURSE(visitor->VisitStatements(info->function()->body())); |
| 67 } | 72 } |
| 68 | 73 |
| 69 #undef RECURSE | 74 #undef RECURSE |
| 70 | 75 |
| 76 | |
| 77 Handle<Type> AstTyper::ObserveType(Object* value) { | |
| 78 if (value->IsUndefined() || value->IsTheHole()) { | |
| 79 return Handle<Type>(Type::None(), isolate()); | |
| 80 } else { | |
| 81 return Handle<Type>(Type::OfCurrently(Handle<Object>(value, isolate())), | |
| 82 isolate()); | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 | |
| 87 void AstTyper::ObserveTypesOnStack() { | |
| 88 DisallowHeapAllocation no_gc; | |
| 89 JavaScriptFrameIterator it(isolate()); | |
| 90 JavaScriptFrame* frame = it.frame(); | |
| 91 Scope* scope = info_->scope(); | |
| 92 | |
| 93 // Assert that the frame on the stack belongs to the function we want to OSR. | |
| 94 ASSERT_EQ(*info_->closure(), frame->function()); | |
| 95 | |
| 96 int params = scope->num_parameters(); | |
| 97 int locals = scope->num_stack_slots(); | |
| 98 | |
| 99 parameter_types_ = new(zone()) ZoneList<Handle<Type> >(params + 1, zone()); | |
| 100 stack_local_types_ = new(zone()) ZoneList<Handle<Type> >(locals, zone()); | |
| 101 | |
| 102 // The receiver ('this') is a parameter with index -1. We store it as | |
| 103 // element 0 and shift other params up by 1. | |
| 104 parameter_types_->Add(ObserveType(frame->receiver()), zone()); | |
| 105 for (int i = 0; i < params; i++) { | |
| 106 parameter_types_->Add(ObserveType(frame->GetParameter(i)), zone()); | |
| 107 } | |
| 108 | |
| 109 // This includes the function var, if it is stored as a stack local. | |
| 110 for (int i = 0; i < locals; i++) { | |
| 111 stack_local_types_->Add(ObserveType(frame->GetExpression(i)), zone()); | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 | |
| 116 Handle<Type> AstTyper::GetObservedType(Variable* var) { | |
| 117 ASSERT(info_->is_osr()); | |
| 118 ZoneList<Handle<Type> >* list; | |
| 119 int index = var->index(); | |
| 120 | |
| 121 if (var->IsParameter()) { | |
| 122 index += 1; | |
| 123 list = parameter_types_; | |
| 124 } else { | |
| 125 ASSERT(var->IsStackLocal()); | |
| 126 list = stack_local_types_; | |
| 127 } | |
| 128 | |
| 129 // We could also observe the type of context allocated values. We would go | |
| 130 // up the function's scope chain until we find the variable's scope, go up | |
| 131 // the context chain at the same time, and load the value from the context | |
| 132 // slot stored as variable index. But this seems not to be worth the effort. | |
| 133 | |
| 134 ASSERT(0 <= index && index < list->length()); | |
|
Jakob Kummerow
2013/12/12 13:46:46
Unnecessary -- List::at() contains the same check.
| |
| 135 Handle<Type> result = list->at(index); | |
| 136 | |
| 137 if (FLAG_trace_osr && FLAG_print_scopes) { | |
| 138 PrintF(" observed %s : ", var->IsParameter() ? "param" : "local"); | |
| 139 var->name()->Print(); | |
| 140 PrintF(" -> "); | |
| 141 result->TypePrint(); | |
| 142 } | |
| 143 | |
| 144 return result; | |
| 145 } | |
| 146 | |
| 147 | |
| 71 #define RECURSE(call) \ | 148 #define RECURSE(call) \ |
| 72 do { \ | 149 do { \ |
| 73 ASSERT(!HasStackOverflow()); \ | 150 ASSERT(!HasStackOverflow()); \ |
| 74 call; \ | 151 call; \ |
| 75 if (HasStackOverflow()) return; \ | 152 if (HasStackOverflow()) return; \ |
| 76 } while (false) | 153 } while (false) |
| 77 | 154 |
| 78 | 155 |
| 79 void AstTyper::VisitStatements(ZoneList<Statement*>* stmts) { | 156 void AstTyper::VisitStatements(ZoneList<Statement*>* stmts) { |
| 80 for (int i = 0; i < stmts->length(); ++i) { | 157 for (int i = 0; i < stmts->length(); ++i) { |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 332 NarrowType(expr, Bounds::Either( | 409 NarrowType(expr, Bounds::Either( |
| 333 expr->then_expression()->bounds(), | 410 expr->then_expression()->bounds(), |
| 334 expr->else_expression()->bounds(), isolate_)); | 411 expr->else_expression()->bounds(), isolate_)); |
| 335 } | 412 } |
| 336 | 413 |
| 337 | 414 |
| 338 void AstTyper::VisitVariableProxy(VariableProxy* expr) { | 415 void AstTyper::VisitVariableProxy(VariableProxy* expr) { |
| 339 Variable* var = expr->var(); | 416 Variable* var = expr->var(); |
| 340 if (var->IsStackAllocated()) { | 417 if (var->IsStackAllocated()) { |
| 341 NarrowType(expr, store_.LookupBounds(variable_index(var))); | 418 NarrowType(expr, store_.LookupBounds(variable_index(var))); |
| 419 if (info_->is_osr()) NarrowLowerType(expr, GetObservedType(var)); | |
| 342 } | 420 } |
| 343 } | 421 } |
| 344 | 422 |
| 345 | 423 |
| 346 void AstTyper::VisitLiteral(Literal* expr) { | 424 void AstTyper::VisitLiteral(Literal* expr) { |
| 347 Type* type = Type::Constant(expr->value(), isolate_); | 425 Type* type = Type::Constant(expr->value(), isolate_); |
| 348 NarrowType(expr, Bounds(type, isolate_)); | 426 NarrowType(expr, Bounds(type, isolate_)); |
| 349 } | 427 } |
| 350 | 428 |
| 351 | 429 |
| (...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 735 void AstTyper::VisitModuleUrl(ModuleUrl* module) { | 813 void AstTyper::VisitModuleUrl(ModuleUrl* module) { |
| 736 } | 814 } |
| 737 | 815 |
| 738 | 816 |
| 739 void AstTyper::VisitModuleStatement(ModuleStatement* stmt) { | 817 void AstTyper::VisitModuleStatement(ModuleStatement* stmt) { |
| 740 RECURSE(Visit(stmt->body())); | 818 RECURSE(Visit(stmt->body())); |
| 741 } | 819 } |
| 742 | 820 |
| 743 | 821 |
| 744 } } // namespace v8::internal | 822 } } // namespace v8::internal |
| OLD | NEW |