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(); | |
rossberg
2013/12/16 09:57:26
This should go into the Run method. It is not cons
| |
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()) { | |
rossberg
2013/12/16 09:57:26
Why is the type None for undefined?
| |
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 Handle<Type> result = list->at(index); | |
135 #ifdef OBJECT_PRINT | |
136 if (FLAG_trace_osr && FLAG_print_scopes) { | |
137 PrintF(" observed %s : ", var->IsParameter() ? "param" : "local"); | |
138 var->name()->Print(); | |
139 PrintF(" -> "); | |
140 result->TypePrint(); | |
141 } | |
142 #endif | |
143 return result; | |
144 } | |
145 | |
146 | |
71 #define RECURSE(call) \ | 147 #define RECURSE(call) \ |
72 do { \ | 148 do { \ |
73 ASSERT(!HasStackOverflow()); \ | 149 ASSERT(!HasStackOverflow()); \ |
74 call; \ | 150 call; \ |
75 if (HasStackOverflow()) return; \ | 151 if (HasStackOverflow()) return; \ |
76 } while (false) | 152 } while (false) |
77 | 153 |
78 | 154 |
79 void AstTyper::VisitStatements(ZoneList<Statement*>* stmts) { | 155 void AstTyper::VisitStatements(ZoneList<Statement*>* stmts) { |
80 for (int i = 0; i < stmts->length(); ++i) { | 156 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( | 408 NarrowType(expr, Bounds::Either( |
333 expr->then_expression()->bounds(), | 409 expr->then_expression()->bounds(), |
334 expr->else_expression()->bounds(), isolate_)); | 410 expr->else_expression()->bounds(), isolate_)); |
335 } | 411 } |
336 | 412 |
337 | 413 |
338 void AstTyper::VisitVariableProxy(VariableProxy* expr) { | 414 void AstTyper::VisitVariableProxy(VariableProxy* expr) { |
339 Variable* var = expr->var(); | 415 Variable* var = expr->var(); |
340 if (var->IsStackAllocated()) { | 416 if (var->IsStackAllocated()) { |
341 NarrowType(expr, store_.LookupBounds(variable_index(var))); | 417 NarrowType(expr, store_.LookupBounds(variable_index(var))); |
418 if (info_->is_osr()) NarrowLowerType(expr, GetObservedType(var)); | |
342 } | 419 } |
343 } | 420 } |
344 | 421 |
345 | 422 |
346 void AstTyper::VisitLiteral(Literal* expr) { | 423 void AstTyper::VisitLiteral(Literal* expr) { |
347 Type* type = Type::Constant(expr->value(), isolate_); | 424 Type* type = Type::Constant(expr->value(), isolate_); |
348 NarrowType(expr, Bounds(type, isolate_)); | 425 NarrowType(expr, Bounds(type, isolate_)); |
349 } | 426 } |
350 | 427 |
351 | 428 |
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
732 void AstTyper::VisitModuleUrl(ModuleUrl* module) { | 809 void AstTyper::VisitModuleUrl(ModuleUrl* module) { |
733 } | 810 } |
734 | 811 |
735 | 812 |
736 void AstTyper::VisitModuleStatement(ModuleStatement* stmt) { | 813 void AstTyper::VisitModuleStatement(ModuleStatement* stmt) { |
737 RECURSE(Visit(stmt->body())); | 814 RECURSE(Visit(stmt->body())); |
738 } | 815 } |
739 | 816 |
740 | 817 |
741 } } // namespace v8::internal | 818 } } // namespace v8::internal |
OLD | NEW |