OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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/v8.h" | 5 #include "src/v8.h" |
6 | 6 |
7 #include "src/compiler.h" | 7 #include "src/compiler.h" |
8 | 8 |
9 #include "src/bootstrapper.h" | 9 #include "src/bootstrapper.h" |
10 #include "src/codegen.h" | 10 #include "src/codegen.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 CopyBytes(copy, data, length); | 40 CopyBytes(copy, data, length); |
41 data_ = copy; | 41 data_ = copy; |
42 AcquireDataOwnership(); | 42 AcquireDataOwnership(); |
43 } | 43 } |
44 } | 44 } |
45 | 45 |
46 | 46 |
47 CompilationInfo::CompilationInfo(Handle<Script> script, Zone* zone) | 47 CompilationInfo::CompilationInfo(Handle<Script> script, Zone* zone) |
48 : flags_(kThisHasUses), | 48 : flags_(kThisHasUses), |
49 script_(script), | 49 script_(script), |
| 50 source_stream_(NULL), |
50 osr_ast_id_(BailoutId::None()), | 51 osr_ast_id_(BailoutId::None()), |
51 parameter_count_(0), | 52 parameter_count_(0), |
52 optimization_id_(-1), | 53 optimization_id_(-1), |
53 ast_value_factory_(NULL), | 54 ast_value_factory_(NULL), |
54 ast_value_factory_owned_(false) { | 55 ast_value_factory_owned_(false) { |
55 Initialize(script->GetIsolate(), BASE, zone); | 56 Initialize(script->GetIsolate(), BASE, zone); |
56 } | 57 } |
57 | 58 |
58 | 59 |
59 CompilationInfo::CompilationInfo(Isolate* isolate, Zone* zone) | 60 CompilationInfo::CompilationInfo(Isolate* isolate, Zone* zone) |
60 : flags_(kThisHasUses), | 61 : flags_(kThisHasUses), |
61 script_(Handle<Script>::null()), | 62 script_(Handle<Script>::null()), |
| 63 source_stream_(NULL), |
62 osr_ast_id_(BailoutId::None()), | 64 osr_ast_id_(BailoutId::None()), |
63 parameter_count_(0), | 65 parameter_count_(0), |
64 optimization_id_(-1), | 66 optimization_id_(-1), |
65 ast_value_factory_(NULL), | 67 ast_value_factory_(NULL), |
66 ast_value_factory_owned_(false) { | 68 ast_value_factory_owned_(false) { |
67 Initialize(isolate, STUB, zone); | 69 Initialize(isolate, STUB, zone); |
68 } | 70 } |
69 | 71 |
70 | 72 |
71 CompilationInfo::CompilationInfo(Handle<SharedFunctionInfo> shared_info, | 73 CompilationInfo::CompilationInfo(Handle<SharedFunctionInfo> shared_info, |
72 Zone* zone) | 74 Zone* zone) |
73 : flags_(kLazy | kThisHasUses), | 75 : flags_(kLazy | kThisHasUses), |
74 shared_info_(shared_info), | 76 shared_info_(shared_info), |
75 script_(Handle<Script>(Script::cast(shared_info->script()))), | 77 script_(Handle<Script>(Script::cast(shared_info->script()))), |
| 78 source_stream_(NULL), |
76 osr_ast_id_(BailoutId::None()), | 79 osr_ast_id_(BailoutId::None()), |
77 parameter_count_(0), | 80 parameter_count_(0), |
78 optimization_id_(-1), | 81 optimization_id_(-1), |
79 ast_value_factory_(NULL), | 82 ast_value_factory_(NULL), |
80 ast_value_factory_owned_(false) { | 83 ast_value_factory_owned_(false) { |
81 Initialize(script_->GetIsolate(), BASE, zone); | 84 Initialize(script_->GetIsolate(), BASE, zone); |
82 } | 85 } |
83 | 86 |
84 | 87 |
85 CompilationInfo::CompilationInfo(Handle<JSFunction> closure, Zone* zone) | 88 CompilationInfo::CompilationInfo(Handle<JSFunction> closure, Zone* zone) |
86 : flags_(kLazy | kThisHasUses), | 89 : flags_(kLazy | kThisHasUses), |
87 closure_(closure), | 90 closure_(closure), |
88 shared_info_(Handle<SharedFunctionInfo>(closure->shared())), | 91 shared_info_(Handle<SharedFunctionInfo>(closure->shared())), |
89 script_(Handle<Script>(Script::cast(shared_info_->script()))), | 92 script_(Handle<Script>(Script::cast(shared_info_->script()))), |
| 93 source_stream_(NULL), |
90 context_(closure->context()), | 94 context_(closure->context()), |
91 osr_ast_id_(BailoutId::None()), | 95 osr_ast_id_(BailoutId::None()), |
92 parameter_count_(0), | 96 parameter_count_(0), |
93 optimization_id_(-1), | 97 optimization_id_(-1), |
94 ast_value_factory_(NULL), | 98 ast_value_factory_(NULL), |
95 ast_value_factory_owned_(false) { | 99 ast_value_factory_owned_(false) { |
96 Initialize(script_->GetIsolate(), BASE, zone); | 100 Initialize(script_->GetIsolate(), BASE, zone); |
97 } | 101 } |
98 | 102 |
99 | 103 |
100 CompilationInfo::CompilationInfo(HydrogenCodeStub* stub, Isolate* isolate, | 104 CompilationInfo::CompilationInfo(HydrogenCodeStub* stub, Isolate* isolate, |
101 Zone* zone) | 105 Zone* zone) |
102 : flags_(kLazy | kThisHasUses), | 106 : flags_(kLazy | kThisHasUses), |
| 107 source_stream_(NULL), |
103 osr_ast_id_(BailoutId::None()), | 108 osr_ast_id_(BailoutId::None()), |
104 parameter_count_(0), | 109 parameter_count_(0), |
105 optimization_id_(-1), | 110 optimization_id_(-1), |
106 ast_value_factory_(NULL), | 111 ast_value_factory_(NULL), |
107 ast_value_factory_owned_(false) { | 112 ast_value_factory_owned_(false) { |
108 Initialize(isolate, STUB, zone); | 113 Initialize(isolate, STUB, zone); |
109 code_stub_ = stub; | 114 code_stub_ = stub; |
110 } | 115 } |
111 | 116 |
112 | 117 |
| 118 CompilationInfo::CompilationInfo( |
| 119 ScriptCompiler::ExternalSourceStream* stream, |
| 120 ScriptCompiler::StreamedSource::Encoding encoding, Isolate* isolate, |
| 121 Zone* zone) |
| 122 : flags_(kThisHasUses), |
| 123 source_stream_(stream), |
| 124 source_stream_encoding_(encoding), |
| 125 osr_ast_id_(BailoutId::None()), |
| 126 parameter_count_(0), |
| 127 optimization_id_(-1), |
| 128 ast_value_factory_(NULL), |
| 129 ast_value_factory_owned_(false) { |
| 130 Initialize(isolate, BASE, zone); |
| 131 } |
| 132 |
| 133 |
113 void CompilationInfo::Initialize(Isolate* isolate, | 134 void CompilationInfo::Initialize(Isolate* isolate, |
114 Mode mode, | 135 Mode mode, |
115 Zone* zone) { | 136 Zone* zone) { |
116 isolate_ = isolate; | 137 isolate_ = isolate; |
117 function_ = NULL; | 138 function_ = NULL; |
118 scope_ = NULL; | 139 scope_ = NULL; |
119 global_scope_ = NULL; | 140 global_scope_ = NULL; |
120 extension_ = NULL; | 141 extension_ = NULL; |
121 cached_data_ = NULL; | 142 cached_data_ = NULL; |
122 compile_options_ = ScriptCompiler::kNoCompileOptions; | 143 compile_options_ = ScriptCompiler::kNoCompileOptions; |
123 zone_ = zone; | 144 zone_ = zone; |
124 deferred_handles_ = NULL; | 145 deferred_handles_ = NULL; |
125 code_stub_ = NULL; | 146 code_stub_ = NULL; |
126 prologue_offset_ = Code::kPrologueOffsetNotSet; | 147 prologue_offset_ = Code::kPrologueOffsetNotSet; |
127 opt_count_ = shared_info().is_null() ? 0 : shared_info()->opt_count(); | 148 opt_count_ = shared_info().is_null() ? 0 : shared_info()->opt_count(); |
128 no_frame_ranges_ = isolate->cpu_profiler()->is_profiling() | 149 no_frame_ranges_ = isolate->cpu_profiler()->is_profiling() |
129 ? new List<OffsetRange>(2) : NULL; | 150 ? new List<OffsetRange>(2) : NULL; |
130 for (int i = 0; i < DependentCode::kGroupCount; i++) { | 151 for (int i = 0; i < DependentCode::kGroupCount; i++) { |
131 dependencies_[i] = NULL; | 152 dependencies_[i] = NULL; |
132 } | 153 } |
133 if (mode == STUB) { | 154 if (mode == STUB) { |
134 mode_ = STUB; | 155 mode_ = STUB; |
135 return; | 156 return; |
136 } | 157 } |
137 mode_ = mode; | 158 mode_ = mode; |
138 abort_due_to_dependency_ = false; | 159 abort_due_to_dependency_ = false; |
139 if (script_->type()->value() == Script::TYPE_NATIVE) MarkAsNative(); | 160 if (!script_.is_null() && script_->type()->value() == Script::TYPE_NATIVE) { |
| 161 MarkAsNative(); |
| 162 } |
140 if (isolate_->debug()->is_active()) MarkAsDebug(); | 163 if (isolate_->debug()->is_active()) MarkAsDebug(); |
141 if (FLAG_context_specialization) MarkAsContextSpecializing(); | 164 if (FLAG_context_specialization) MarkAsContextSpecializing(); |
142 if (FLAG_turbo_types) MarkAsTypingEnabled(); | 165 if (FLAG_turbo_types) MarkAsTypingEnabled(); |
143 | 166 |
144 if (!shared_info_.is_null()) { | 167 if (!shared_info_.is_null()) { |
145 DCHECK(strict_mode() == SLOPPY); | 168 DCHECK(strict_mode() == SLOPPY); |
146 SetStrictMode(shared_info_->strict_mode()); | 169 SetStrictMode(shared_info_->strict_mode()); |
147 } | 170 } |
148 set_bailout_reason(kUnknown); | 171 set_bailout_reason(kUnknown); |
149 | 172 |
(...skipping 653 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
803 if (!CompileUnoptimizedCode(&info)) return; | 826 if (!CompileUnoptimizedCode(&info)) return; |
804 if (!info.shared_info().is_null()) { | 827 if (!info.shared_info().is_null()) { |
805 Handle<ScopeInfo> scope_info = ScopeInfo::Create(info.scope(), | 828 Handle<ScopeInfo> scope_info = ScopeInfo::Create(info.scope(), |
806 info.zone()); | 829 info.zone()); |
807 info.shared_info()->set_scope_info(*scope_info); | 830 info.shared_info()->set_scope_info(*scope_info); |
808 } | 831 } |
809 tracker.RecordRootFunctionInfo(info.code()); | 832 tracker.RecordRootFunctionInfo(info.code()); |
810 } | 833 } |
811 | 834 |
812 | 835 |
813 static bool DebuggerWantsEagerCompilation(CompilationInfo* info, | |
814 bool allow_lazy_without_ctx = false) { | |
815 return LiveEditFunctionTracker::IsActive(info->isolate()) || | |
816 (info->isolate()->DebuggerHasBreakPoints() && !allow_lazy_without_ctx); | |
817 } | |
818 | |
819 | |
820 static Handle<SharedFunctionInfo> CompileToplevel(CompilationInfo* info) { | 836 static Handle<SharedFunctionInfo> CompileToplevel(CompilationInfo* info) { |
821 Isolate* isolate = info->isolate(); | 837 Isolate* isolate = info->isolate(); |
822 PostponeInterruptsScope postpone(isolate); | 838 PostponeInterruptsScope postpone(isolate); |
823 DCHECK(!isolate->native_context().is_null()); | 839 DCHECK(!isolate->native_context().is_null()); |
824 Handle<Script> script = info->script(); | 840 Handle<Script> script = info->script(); |
825 | 841 |
826 // TODO(svenpanne) Obscure place for this, perhaps move to OnBeforeCompile? | 842 // TODO(svenpanne) Obscure place for this, perhaps move to OnBeforeCompile? |
827 FixedArray* array = isolate->native_context()->embedder_data(); | 843 FixedArray* array = isolate->native_context()->embedder_data(); |
828 script->set_context_data(array->get(0)); | 844 script->set_context_data(array->get(0)); |
829 | 845 |
830 isolate->debug()->OnBeforeCompile(script); | 846 isolate->debug()->OnBeforeCompile(script); |
831 | 847 |
832 DCHECK(info->is_eval() || info->is_global()); | 848 DCHECK(info->is_eval() || info->is_global()); |
833 | 849 |
834 bool parse_allow_lazy = | |
835 (info->compile_options() == ScriptCompiler::kConsumeParserCache || | |
836 String::cast(script->source())->length() > FLAG_min_preparse_length) && | |
837 !DebuggerWantsEagerCompilation(info); | |
838 | |
839 if (!parse_allow_lazy && | |
840 (info->compile_options() == ScriptCompiler::kProduceParserCache || | |
841 info->compile_options() == ScriptCompiler::kConsumeParserCache)) { | |
842 // We are going to parse eagerly, but we either 1) have cached data produced | |
843 // by lazy parsing or 2) are asked to generate cached data. We cannot use | |
844 // the existing data, since it won't contain all the symbols we need for | |
845 // eager parsing. In addition, it doesn't make sense to produce the data | |
846 // when parsing eagerly. That data would contain all symbols, but no | |
847 // functions, so it cannot be used to aid lazy parsing later. | |
848 info->SetCachedData(NULL, ScriptCompiler::kNoCompileOptions); | |
849 } | |
850 | |
851 Handle<SharedFunctionInfo> result; | 850 Handle<SharedFunctionInfo> result; |
852 | 851 |
853 { VMState<COMPILER> state(info->isolate()); | 852 { VMState<COMPILER> state(info->isolate()); |
854 if (!Parser::Parse(info, parse_allow_lazy)) { | 853 if (info->function() == NULL) { |
855 return Handle<SharedFunctionInfo>::null(); | 854 // Parse the script if needed (if it's already parsed, function() is |
| 855 // non-NULL). |
| 856 bool parse_allow_lazy = |
| 857 (info->compile_options() == ScriptCompiler::kConsumeParserCache || |
| 858 String::cast(script->source())->length() > |
| 859 FLAG_min_preparse_length) && |
| 860 !Compiler::DebuggerWantsEagerCompilation(info); |
| 861 |
| 862 if (!parse_allow_lazy && |
| 863 (info->compile_options() == ScriptCompiler::kProduceParserCache || |
| 864 info->compile_options() == ScriptCompiler::kConsumeParserCache)) { |
| 865 // We are going to parse eagerly, but we either 1) have cached data |
| 866 // produced by lazy parsing or 2) are asked to generate cached data. |
| 867 // Eager parsing cannot benefit from cached data, and producing cached |
| 868 // data while parsing eagerly is not implemented. |
| 869 info->SetCachedData(NULL, ScriptCompiler::kNoCompileOptions); |
| 870 } |
| 871 if (!Parser::Parse(info, parse_allow_lazy)) { |
| 872 return Handle<SharedFunctionInfo>::null(); |
| 873 } |
856 } | 874 } |
857 | 875 |
858 FunctionLiteral* lit = info->function(); | 876 FunctionLiteral* lit = info->function(); |
859 LiveEditFunctionTracker live_edit_tracker(isolate, lit); | 877 LiveEditFunctionTracker live_edit_tracker(isolate, lit); |
860 | 878 |
861 // Measure how long it takes to do the compilation; only take the | 879 // Measure how long it takes to do the compilation; only take the |
862 // rest of the function into account to avoid overlap with the | 880 // rest of the function into account to avoid overlap with the |
863 // parsing statistics. | 881 // parsing statistics. |
864 HistogramTimer* rate = info->is_eval() | 882 HistogramTimer* rate = info->is_eval() |
865 ? info->isolate()->counters()->compile_eval() | 883 ? info->isolate()->counters()->compile_eval() |
(...skipping 26 matching lines...) Expand all Loading... |
892 PROFILE(isolate, CodeCreateEvent( | 910 PROFILE(isolate, CodeCreateEvent( |
893 log_tag, *info->code(), *result, info, *script_name)); | 911 log_tag, *info->code(), *result, info, *script_name)); |
894 GDBJIT(AddCode(script_name, script, info->code(), info)); | 912 GDBJIT(AddCode(script_name, script, info->code(), info)); |
895 | 913 |
896 // Hint to the runtime system used when allocating space for initial | 914 // Hint to the runtime system used when allocating space for initial |
897 // property space by setting the expected number of properties for | 915 // property space by setting the expected number of properties for |
898 // the instances of the function. | 916 // the instances of the function. |
899 SetExpectedNofPropertiesFromEstimate(result, | 917 SetExpectedNofPropertiesFromEstimate(result, |
900 lit->expected_property_count()); | 918 lit->expected_property_count()); |
901 | 919 |
902 script->set_compilation_state(Script::COMPILATION_STATE_COMPILED); | 920 if (!script.is_null()) |
| 921 script->set_compilation_state(Script::COMPILATION_STATE_COMPILED); |
903 | 922 |
904 live_edit_tracker.RecordFunctionInfo(result, lit, info->zone()); | 923 live_edit_tracker.RecordFunctionInfo(result, lit, info->zone()); |
905 } | 924 } |
906 | 925 |
907 isolate->debug()->OnAfterCompile(script); | 926 isolate->debug()->OnAfterCompile(script); |
908 | 927 |
909 return result; | 928 return result; |
910 } | 929 } |
911 | 930 |
912 | 931 |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1049 } | 1068 } |
1050 | 1069 |
1051 if (result.is_null()) isolate->ReportPendingMessages(); | 1070 if (result.is_null()) isolate->ReportPendingMessages(); |
1052 } else if (result->ic_age() != isolate->heap()->global_ic_age()) { | 1071 } else if (result->ic_age() != isolate->heap()->global_ic_age()) { |
1053 result->ResetForNewContext(isolate->heap()->global_ic_age()); | 1072 result->ResetForNewContext(isolate->heap()->global_ic_age()); |
1054 } | 1073 } |
1055 return result; | 1074 return result; |
1056 } | 1075 } |
1057 | 1076 |
1058 | 1077 |
| 1078 Handle<SharedFunctionInfo> Compiler::CompileStreamedScript( |
| 1079 CompilationInfo* info, int source_length) { |
| 1080 Isolate* isolate = info->isolate(); |
| 1081 isolate->counters()->total_load_size()->Increment(source_length); |
| 1082 isolate->counters()->total_compile_size()->Increment(source_length); |
| 1083 |
| 1084 if (FLAG_use_strict) info->SetStrictMode(STRICT); |
| 1085 // TODO(marja): FLAG_serialize_toplevel is not honoured and won't be; when the |
| 1086 // real code caching lands, streaming needs to be adapted to use it. |
| 1087 return CompileToplevel(info); |
| 1088 } |
| 1089 |
| 1090 |
1059 Handle<SharedFunctionInfo> Compiler::BuildFunctionInfo( | 1091 Handle<SharedFunctionInfo> Compiler::BuildFunctionInfo( |
1060 FunctionLiteral* literal, Handle<Script> script, | 1092 FunctionLiteral* literal, Handle<Script> script, |
1061 CompilationInfo* outer_info) { | 1093 CompilationInfo* outer_info) { |
1062 // Precondition: code has been parsed and scopes have been analyzed. | 1094 // Precondition: code has been parsed and scopes have been analyzed. |
1063 CompilationInfoWithZone info(script); | 1095 CompilationInfoWithZone info(script); |
1064 info.SetFunction(literal); | 1096 info.SetFunction(literal); |
1065 info.PrepareForCompilation(literal->scope()); | 1097 info.PrepareForCompilation(literal->scope()); |
1066 info.SetStrictMode(literal->scope()->strict_mode()); | 1098 info.SetStrictMode(literal->scope()->strict_mode()); |
1067 if (outer_info->will_serialize()) info.PrepareForSerializing(); | 1099 if (outer_info->will_serialize()) info.PrepareForSerializing(); |
1068 | 1100 |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1348 log_tag, *code, *shared, info, script_name, line_num, column_num)); | 1380 log_tag, *code, *shared, info, script_name, line_num, column_num)); |
1349 } | 1381 } |
1350 | 1382 |
1351 GDBJIT(AddCode(Handle<String>(shared->DebugName()), | 1383 GDBJIT(AddCode(Handle<String>(shared->DebugName()), |
1352 Handle<Script>(info->script()), | 1384 Handle<Script>(info->script()), |
1353 Handle<Code>(info->code()), | 1385 Handle<Code>(info->code()), |
1354 info)); | 1386 info)); |
1355 } | 1387 } |
1356 | 1388 |
1357 | 1389 |
| 1390 bool Compiler::DebuggerWantsEagerCompilation(CompilationInfo* info, |
| 1391 bool allow_lazy_without_ctx) { |
| 1392 return LiveEditFunctionTracker::IsActive(info->isolate()) || |
| 1393 (info->isolate()->DebuggerHasBreakPoints() && !allow_lazy_without_ctx); |
| 1394 } |
| 1395 |
| 1396 |
1358 CompilationPhase::CompilationPhase(const char* name, CompilationInfo* info) | 1397 CompilationPhase::CompilationPhase(const char* name, CompilationInfo* info) |
1359 : name_(name), info_(info), zone_(info->isolate()) { | 1398 : name_(name), info_(info), zone_(info->isolate()) { |
1360 if (FLAG_hydrogen_stats) { | 1399 if (FLAG_hydrogen_stats) { |
1361 info_zone_start_allocation_size_ = info->zone()->allocation_size(); | 1400 info_zone_start_allocation_size_ = info->zone()->allocation_size(); |
1362 timer_.Start(); | 1401 timer_.Start(); |
1363 } | 1402 } |
1364 } | 1403 } |
1365 | 1404 |
1366 | 1405 |
1367 CompilationPhase::~CompilationPhase() { | 1406 CompilationPhase::~CompilationPhase() { |
(...skipping 11 matching lines...) Expand all Loading... |
1379 AllowHandleDereference allow_deref; | 1418 AllowHandleDereference allow_deref; |
1380 bool tracing_on = info()->IsStub() | 1419 bool tracing_on = info()->IsStub() |
1381 ? FLAG_trace_hydrogen_stubs | 1420 ? FLAG_trace_hydrogen_stubs |
1382 : (FLAG_trace_hydrogen && | 1421 : (FLAG_trace_hydrogen && |
1383 info()->closure()->PassesFilter(FLAG_trace_hydrogen_filter)); | 1422 info()->closure()->PassesFilter(FLAG_trace_hydrogen_filter)); |
1384 return (tracing_on && | 1423 return (tracing_on && |
1385 base::OS::StrChr(const_cast<char*>(FLAG_trace_phase), name_[0]) != NULL); | 1424 base::OS::StrChr(const_cast<char*>(FLAG_trace_phase), name_[0]) != NULL); |
1386 } | 1425 } |
1387 | 1426 |
1388 } } // namespace v8::internal | 1427 } } // namespace v8::internal |
OLD | NEW |