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_inlining) MarkAsInliningEnabled(); | 165 if (FLAG_turbo_inlining) MarkAsInliningEnabled(); |
143 if (FLAG_turbo_types) MarkAsTypingEnabled(); | 166 if (FLAG_turbo_types) MarkAsTypingEnabled(); |
144 | 167 |
145 if (!shared_info_.is_null()) { | 168 if (!shared_info_.is_null()) { |
146 DCHECK(strict_mode() == SLOPPY); | 169 DCHECK(strict_mode() == SLOPPY); |
147 SetStrictMode(shared_info_->strict_mode()); | 170 SetStrictMode(shared_info_->strict_mode()); |
148 } | 171 } |
149 set_bailout_reason(kUnknown); | 172 set_bailout_reason(kUnknown); |
(...skipping 650 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
800 if (!CompileUnoptimizedCode(&info)) return; | 823 if (!CompileUnoptimizedCode(&info)) return; |
801 if (!info.shared_info().is_null()) { | 824 if (!info.shared_info().is_null()) { |
802 Handle<ScopeInfo> scope_info = ScopeInfo::Create(info.scope(), | 825 Handle<ScopeInfo> scope_info = ScopeInfo::Create(info.scope(), |
803 info.zone()); | 826 info.zone()); |
804 info.shared_info()->set_scope_info(*scope_info); | 827 info.shared_info()->set_scope_info(*scope_info); |
805 } | 828 } |
806 tracker.RecordRootFunctionInfo(info.code()); | 829 tracker.RecordRootFunctionInfo(info.code()); |
807 } | 830 } |
808 | 831 |
809 | 832 |
810 static bool DebuggerWantsEagerCompilation(CompilationInfo* info, | |
811 bool allow_lazy_without_ctx = false) { | |
812 return LiveEditFunctionTracker::IsActive(info->isolate()) || | |
813 (info->isolate()->DebuggerHasBreakPoints() && !allow_lazy_without_ctx); | |
814 } | |
815 | |
816 | |
817 static Handle<SharedFunctionInfo> CompileToplevel(CompilationInfo* info) { | 833 static Handle<SharedFunctionInfo> CompileToplevel(CompilationInfo* info) { |
818 Isolate* isolate = info->isolate(); | 834 Isolate* isolate = info->isolate(); |
819 PostponeInterruptsScope postpone(isolate); | 835 PostponeInterruptsScope postpone(isolate); |
820 DCHECK(!isolate->native_context().is_null()); | 836 DCHECK(!isolate->native_context().is_null()); |
821 Handle<Script> script = info->script(); | 837 Handle<Script> script = info->script(); |
822 | 838 |
823 // TODO(svenpanne) Obscure place for this, perhaps move to OnBeforeCompile? | 839 // TODO(svenpanne) Obscure place for this, perhaps move to OnBeforeCompile? |
824 FixedArray* array = isolate->native_context()->embedder_data(); | 840 FixedArray* array = isolate->native_context()->embedder_data(); |
825 script->set_context_data(array->get(0)); | 841 script->set_context_data(array->get(0)); |
826 | 842 |
827 isolate->debug()->OnBeforeCompile(script); | 843 isolate->debug()->OnBeforeCompile(script); |
828 | 844 |
829 DCHECK(info->is_eval() || info->is_global()); | 845 DCHECK(info->is_eval() || info->is_global()); |
830 | 846 |
831 bool parse_allow_lazy = | |
832 (info->compile_options() == ScriptCompiler::kConsumeParserCache || | |
833 String::cast(script->source())->length() > FLAG_min_preparse_length) && | |
834 !DebuggerWantsEagerCompilation(info); | |
835 | |
836 if (!parse_allow_lazy && | |
837 (info->compile_options() == ScriptCompiler::kProduceParserCache || | |
838 info->compile_options() == ScriptCompiler::kConsumeParserCache)) { | |
839 // We are going to parse eagerly, but we either 1) have cached data produced | |
840 // by lazy parsing or 2) are asked to generate cached data. We cannot use | |
841 // the existing data, since it won't contain all the symbols we need for | |
842 // eager parsing. In addition, it doesn't make sense to produce the data | |
843 // when parsing eagerly. That data would contain all symbols, but no | |
844 // functions, so it cannot be used to aid lazy parsing later. | |
845 info->SetCachedData(NULL, ScriptCompiler::kNoCompileOptions); | |
846 } | |
847 | |
848 Handle<SharedFunctionInfo> result; | 847 Handle<SharedFunctionInfo> result; |
849 | 848 |
850 { VMState<COMPILER> state(info->isolate()); | 849 { VMState<COMPILER> state(info->isolate()); |
851 if (!Parser::Parse(info, parse_allow_lazy)) { | 850 if (info->function() == NULL) { |
852 return Handle<SharedFunctionInfo>::null(); | 851 // Parse the script if needed (if it's already parsed, function() is |
| 852 // non-NULL). |
| 853 bool parse_allow_lazy = |
| 854 (info->compile_options() == ScriptCompiler::kConsumeParserCache || |
| 855 String::cast(script->source())->length() > |
| 856 FLAG_min_preparse_length) && |
| 857 !Compiler::DebuggerWantsEagerCompilation(info); |
| 858 |
| 859 if (!parse_allow_lazy && |
| 860 (info->compile_options() == ScriptCompiler::kProduceParserCache || |
| 861 info->compile_options() == ScriptCompiler::kConsumeParserCache)) { |
| 862 // We are going to parse eagerly, but we either 1) have cached data |
| 863 // produced by lazy parsing or 2) are asked to generate cached data. |
| 864 // Eager parsing cannot benefit from cached data, and producing cached |
| 865 // data while parsing eagerly is not implemented. |
| 866 info->SetCachedData(NULL, ScriptCompiler::kNoCompileOptions); |
| 867 } |
| 868 if (!Parser::Parse(info, parse_allow_lazy)) { |
| 869 return Handle<SharedFunctionInfo>::null(); |
| 870 } |
853 } | 871 } |
854 | 872 |
855 FunctionLiteral* lit = info->function(); | 873 FunctionLiteral* lit = info->function(); |
856 LiveEditFunctionTracker live_edit_tracker(isolate, lit); | 874 LiveEditFunctionTracker live_edit_tracker(isolate, lit); |
857 | 875 |
858 // Measure how long it takes to do the compilation; only take the | 876 // Measure how long it takes to do the compilation; only take the |
859 // rest of the function into account to avoid overlap with the | 877 // rest of the function into account to avoid overlap with the |
860 // parsing statistics. | 878 // parsing statistics. |
861 HistogramTimer* rate = info->is_eval() | 879 HistogramTimer* rate = info->is_eval() |
862 ? info->isolate()->counters()->compile_eval() | 880 ? info->isolate()->counters()->compile_eval() |
(...skipping 25 matching lines...) Expand all Loading... |
888 PROFILE(isolate, CodeCreateEvent( | 906 PROFILE(isolate, CodeCreateEvent( |
889 log_tag, *info->code(), *result, info, *script_name)); | 907 log_tag, *info->code(), *result, info, *script_name)); |
890 GDBJIT(AddCode(script_name, script, info->code(), info)); | 908 GDBJIT(AddCode(script_name, script, info->code(), info)); |
891 | 909 |
892 // Hint to the runtime system used when allocating space for initial | 910 // Hint to the runtime system used when allocating space for initial |
893 // property space by setting the expected number of properties for | 911 // property space by setting the expected number of properties for |
894 // the instances of the function. | 912 // the instances of the function. |
895 SetExpectedNofPropertiesFromEstimate(result, | 913 SetExpectedNofPropertiesFromEstimate(result, |
896 lit->expected_property_count()); | 914 lit->expected_property_count()); |
897 | 915 |
898 script->set_compilation_state(Script::COMPILATION_STATE_COMPILED); | 916 if (!script.is_null()) |
| 917 script->set_compilation_state(Script::COMPILATION_STATE_COMPILED); |
899 | 918 |
900 live_edit_tracker.RecordFunctionInfo(result, lit, info->zone()); | 919 live_edit_tracker.RecordFunctionInfo(result, lit, info->zone()); |
901 } | 920 } |
902 | 921 |
903 isolate->debug()->OnAfterCompile(script); | 922 isolate->debug()->OnAfterCompile(script); |
904 | 923 |
905 return result; | 924 return result; |
906 } | 925 } |
907 | 926 |
908 | 927 |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1045 } | 1064 } |
1046 | 1065 |
1047 if (result.is_null()) isolate->ReportPendingMessages(); | 1066 if (result.is_null()) isolate->ReportPendingMessages(); |
1048 } else if (result->ic_age() != isolate->heap()->global_ic_age()) { | 1067 } else if (result->ic_age() != isolate->heap()->global_ic_age()) { |
1049 result->ResetForNewContext(isolate->heap()->global_ic_age()); | 1068 result->ResetForNewContext(isolate->heap()->global_ic_age()); |
1050 } | 1069 } |
1051 return result; | 1070 return result; |
1052 } | 1071 } |
1053 | 1072 |
1054 | 1073 |
| 1074 Handle<SharedFunctionInfo> Compiler::CompileStreamedScript( |
| 1075 CompilationInfo* info, int source_length) { |
| 1076 Isolate* isolate = info->isolate(); |
| 1077 isolate->counters()->total_load_size()->Increment(source_length); |
| 1078 isolate->counters()->total_compile_size()->Increment(source_length); |
| 1079 |
| 1080 if (FLAG_use_strict) info->SetStrictMode(STRICT); |
| 1081 // TODO(marja): FLAG_serialize_toplevel is not honoured and won't be; when the |
| 1082 // real code caching lands, streaming needs to be adapted to use it. |
| 1083 return CompileToplevel(info); |
| 1084 } |
| 1085 |
| 1086 |
1055 Handle<SharedFunctionInfo> Compiler::BuildFunctionInfo( | 1087 Handle<SharedFunctionInfo> Compiler::BuildFunctionInfo( |
1056 FunctionLiteral* literal, Handle<Script> script, | 1088 FunctionLiteral* literal, Handle<Script> script, |
1057 CompilationInfo* outer_info) { | 1089 CompilationInfo* outer_info) { |
1058 // Precondition: code has been parsed and scopes have been analyzed. | 1090 // Precondition: code has been parsed and scopes have been analyzed. |
1059 CompilationInfoWithZone info(script); | 1091 CompilationInfoWithZone info(script); |
1060 info.SetFunction(literal); | 1092 info.SetFunction(literal); |
1061 info.PrepareForCompilation(literal->scope()); | 1093 info.PrepareForCompilation(literal->scope()); |
1062 info.SetStrictMode(literal->scope()->strict_mode()); | 1094 info.SetStrictMode(literal->scope()->strict_mode()); |
1063 if (outer_info->will_serialize()) info.PrepareForSerializing(); | 1095 if (outer_info->will_serialize()) info.PrepareForSerializing(); |
1064 | 1096 |
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1352 log_tag, *code, *shared, info, script_name, line_num, column_num)); | 1384 log_tag, *code, *shared, info, script_name, line_num, column_num)); |
1353 } | 1385 } |
1354 | 1386 |
1355 GDBJIT(AddCode(Handle<String>(shared->DebugName()), | 1387 GDBJIT(AddCode(Handle<String>(shared->DebugName()), |
1356 Handle<Script>(info->script()), | 1388 Handle<Script>(info->script()), |
1357 Handle<Code>(info->code()), | 1389 Handle<Code>(info->code()), |
1358 info)); | 1390 info)); |
1359 } | 1391 } |
1360 | 1392 |
1361 | 1393 |
| 1394 bool Compiler::DebuggerWantsEagerCompilation(CompilationInfo* info, |
| 1395 bool allow_lazy_without_ctx) { |
| 1396 return LiveEditFunctionTracker::IsActive(info->isolate()) || |
| 1397 (info->isolate()->DebuggerHasBreakPoints() && !allow_lazy_without_ctx); |
| 1398 } |
| 1399 |
| 1400 |
1362 CompilationPhase::CompilationPhase(const char* name, CompilationInfo* info) | 1401 CompilationPhase::CompilationPhase(const char* name, CompilationInfo* info) |
1363 : name_(name), info_(info), zone_(info->isolate()) { | 1402 : name_(name), info_(info), zone_(info->isolate()) { |
1364 if (FLAG_hydrogen_stats) { | 1403 if (FLAG_hydrogen_stats) { |
1365 info_zone_start_allocation_size_ = info->zone()->allocation_size(); | 1404 info_zone_start_allocation_size_ = info->zone()->allocation_size(); |
1366 timer_.Start(); | 1405 timer_.Start(); |
1367 } | 1406 } |
1368 } | 1407 } |
1369 | 1408 |
1370 | 1409 |
1371 CompilationPhase::~CompilationPhase() { | 1410 CompilationPhase::~CompilationPhase() { |
(...skipping 11 matching lines...) Expand all Loading... |
1383 AllowHandleDereference allow_deref; | 1422 AllowHandleDereference allow_deref; |
1384 bool tracing_on = info()->IsStub() | 1423 bool tracing_on = info()->IsStub() |
1385 ? FLAG_trace_hydrogen_stubs | 1424 ? FLAG_trace_hydrogen_stubs |
1386 : (FLAG_trace_hydrogen && | 1425 : (FLAG_trace_hydrogen && |
1387 info()->closure()->PassesFilter(FLAG_trace_hydrogen_filter)); | 1426 info()->closure()->PassesFilter(FLAG_trace_hydrogen_filter)); |
1388 return (tracing_on && | 1427 return (tracing_on && |
1389 base::OS::StrChr(const_cast<char*>(FLAG_trace_phase), name_[0]) != NULL); | 1428 base::OS::StrChr(const_cast<char*>(FLAG_trace_phase), name_[0]) != NULL); |
1390 } | 1429 } |
1391 | 1430 |
1392 } } // namespace v8::internal | 1431 } } // namespace v8::internal |
OLD | NEW |