1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/parsing/parse-info.h"
6
7 #include "src/ast/ast-source-ranges.h"
8 #include "src/ast/ast-value-factory.h"
9 #include "src/ast/ast.h"
10 #include "src/base/logging.h"
11 #include "src/common/globals.h"
12 #include "src/compiler-dispatcher/lazy-compile-dispatcher.h"
13 #include "src/heap/heap-inl.h"
14 #include "src/logging/counters.h"
15 #include "src/logging/log.h"
16 #include "src/numbers/hash-seed-inl.h"
17 #include "src/objects/objects-inl.h"
18 #include "src/objects/scope-info.h"
19 #include "src/zone/zone.h"
20
21 namespace v8 {
22 namespace internal {
23
UnoptimizedCompileFlags(Isolate * isolate,int script_id)24 UnoptimizedCompileFlags::UnoptimizedCompileFlags(Isolate* isolate,
25 int script_id)
26 : flags_(0),
27 script_id_(script_id),
28 function_kind_(FunctionKind::kNormalFunction),
29 function_syntax_kind_(FunctionSyntaxKind::kDeclaration),
30 parsing_while_debugging_(ParsingWhileDebugging::kNo) {
31 set_collect_type_profile(isolate->is_collecting_type_profile());
32 set_coverage_enabled(!isolate->is_best_effort_code_coverage());
33 set_block_coverage_enabled(isolate->is_block_code_coverage());
34 set_might_always_opt(FLAG_always_opt || FLAG_prepare_always_opt);
35 set_allow_natives_syntax(FLAG_allow_natives_syntax);
36 set_allow_lazy_compile(true);
37 set_collect_source_positions(!FLAG_enable_lazy_source_positions ||
38 isolate->NeedsDetailedOptimizedCodeLineInfo());
39 set_post_parallel_compile_tasks_for_eager_toplevel(
40 FLAG_parallel_compile_tasks_for_eager_toplevel);
41 set_post_parallel_compile_tasks_for_lazy(
42 FLAG_parallel_compile_tasks_for_lazy);
43 }
44
45 // static
ForFunctionCompile(Isolate * isolate,SharedFunctionInfo shared)46 UnoptimizedCompileFlags UnoptimizedCompileFlags::ForFunctionCompile(
47 Isolate* isolate, SharedFunctionInfo shared) {
48 Script script = Script::cast(shared.script());
49
50 UnoptimizedCompileFlags flags(isolate, script.id());
51
52 flags.SetFlagsFromFunction(&shared);
53 flags.SetFlagsForFunctionFromScript(script);
54 flags.set_allow_lazy_parsing(true);
55 flags.set_is_lazy_compile(true);
56
57 #if V8_ENABLE_WEBASSEMBLY
58 flags.set_is_asm_wasm_broken(shared.is_asm_wasm_broken());
59 #endif // V8_ENABLE_WEBASSEMBLY
60 flags.set_is_repl_mode(shared.is_repl_mode());
61
62 // CollectTypeProfile uses its own feedback slots. If we have existing
63 // FeedbackMetadata, we can only collect type profile if the feedback vector
64 // has the appropriate slots.
65 flags.set_collect_type_profile(
66 isolate->is_collecting_type_profile() &&
67 (shared.HasFeedbackMetadata()
68 ? shared.feedback_metadata().HasTypeProfileSlot()
69 : script.IsUserJavaScript()));
70
71 // Do not support re-parsing top-level function of a wrapped script.
72 DCHECK_IMPLIES(flags.is_toplevel(), !script.is_wrapped());
73
74 return flags;
75 }
76
77 // static
ForScriptCompile(Isolate * isolate,Script script)78 UnoptimizedCompileFlags UnoptimizedCompileFlags::ForScriptCompile(
79 Isolate* isolate, Script script) {
80 UnoptimizedCompileFlags flags(isolate, script.id());
81
82 flags.SetFlagsForFunctionFromScript(script);
83 flags.SetFlagsForToplevelCompile(
84 isolate->is_collecting_type_profile(), script.IsUserJavaScript(),
85 flags.outer_language_mode(), construct_repl_mode(script.is_repl_mode()),
86 script.origin_options().IsModule() ? ScriptType::kModule
87 : ScriptType::kClassic,
88 FLAG_lazy);
89 if (script.is_wrapped()) {
90 flags.set_function_syntax_kind(FunctionSyntaxKind::kWrapped);
91 }
92
93 return flags;
94 }
95
96 // static
ForToplevelCompile(Isolate * isolate,bool is_user_javascript,LanguageMode language_mode,REPLMode repl_mode,ScriptType type,bool lazy)97 UnoptimizedCompileFlags UnoptimizedCompileFlags::ForToplevelCompile(
98 Isolate* isolate, bool is_user_javascript, LanguageMode language_mode,
99 REPLMode repl_mode, ScriptType type, bool lazy) {
100 UnoptimizedCompileFlags flags(isolate, isolate->GetNextScriptId());
101 flags.SetFlagsForToplevelCompile(isolate->is_collecting_type_profile(),
102 is_user_javascript, language_mode, repl_mode,
103 type, lazy);
104
105 LOG(isolate,
106 ScriptEvent(Logger::ScriptEventType::kReserveId, flags.script_id()));
107 return flags;
108 }
109
110 // static
ForToplevelFunction(const UnoptimizedCompileFlags toplevel_flags,const FunctionLiteral * literal)111 UnoptimizedCompileFlags UnoptimizedCompileFlags::ForToplevelFunction(
112 const UnoptimizedCompileFlags toplevel_flags,
113 const FunctionLiteral* literal) {
114 DCHECK(toplevel_flags.is_toplevel());
115 DCHECK(!literal->is_toplevel());
116
117 // Replicate the toplevel flags, then setup the function-specific flags.
118 UnoptimizedCompileFlags flags = toplevel_flags;
119 flags.SetFlagsFromFunction(literal);
120
121 return flags;
122 }
123
124 // static
ForTest(Isolate * isolate)125 UnoptimizedCompileFlags UnoptimizedCompileFlags::ForTest(Isolate* isolate) {
126 return UnoptimizedCompileFlags(isolate, Script::kTemporaryScriptId);
127 }
128
129 template <typename T>
SetFlagsFromFunction(T function)130 void UnoptimizedCompileFlags::SetFlagsFromFunction(T function) {
131 set_outer_language_mode(function->language_mode());
132 set_function_kind(function->kind());
133 set_function_syntax_kind(function->syntax_kind());
134 set_requires_instance_members_initializer(
135 function->requires_instance_members_initializer());
136 set_class_scope_has_private_brand(function->class_scope_has_private_brand());
137 set_has_static_private_methods_or_accessors(
138 function->has_static_private_methods_or_accessors());
139 set_private_name_lookup_skips_outer_class(
140 function->private_name_lookup_skips_outer_class());
141 set_is_toplevel(function->is_toplevel());
142 }
143
SetFlagsForToplevelCompile(bool is_collecting_type_profile,bool is_user_javascript,LanguageMode language_mode,REPLMode repl_mode,ScriptType type,bool lazy)144 void UnoptimizedCompileFlags::SetFlagsForToplevelCompile(
145 bool is_collecting_type_profile, bool is_user_javascript,
146 LanguageMode language_mode, REPLMode repl_mode, ScriptType type,
147 bool lazy) {
148 set_is_toplevel(true);
149 set_allow_lazy_parsing(lazy);
150 set_allow_lazy_compile(lazy);
151 set_collect_type_profile(is_user_javascript && is_collecting_type_profile);
152 set_outer_language_mode(
153 stricter_language_mode(outer_language_mode(), language_mode));
154 set_is_repl_mode((repl_mode == REPLMode::kYes));
155 set_is_module(type == ScriptType::kModule);
156 DCHECK_IMPLIES(is_eval(), !is_module());
157
158 set_block_coverage_enabled(block_coverage_enabled() && is_user_javascript);
159 }
160
SetFlagsForFunctionFromScript(Script script)161 void UnoptimizedCompileFlags::SetFlagsForFunctionFromScript(Script script) {
162 DCHECK_EQ(script_id(), script.id());
163
164 set_is_eval(script.compilation_type() == Script::COMPILATION_TYPE_EVAL);
165 set_is_module(script.origin_options().IsModule());
166 DCHECK_IMPLIES(is_eval(), !is_module());
167
168 set_block_coverage_enabled(block_coverage_enabled() &&
169 script.IsUserJavaScript());
170 }
171
ReusableUnoptimizedCompileState(Isolate * isolate)172 ReusableUnoptimizedCompileState::ReusableUnoptimizedCompileState(
173 Isolate* isolate)
174 : hash_seed_(HashSeed(isolate)),
175 allocator_(isolate->allocator()),
176 logger_(isolate->logger()),
177 dispatcher_(isolate->lazy_compile_dispatcher()),
178 ast_string_constants_(isolate->ast_string_constants()),
179 ast_raw_string_zone_(allocator_,
180 "unoptimized-compile-ast-raw-string-zone"),
181 single_parse_zone_(allocator_, "unoptimized-compile-parse-zone"),
182 ast_value_factory_(
183 new AstValueFactory(ast_raw_string_zone(), single_parse_zone(),
184 ast_string_constants(), hash_seed())) {}
185
ReusableUnoptimizedCompileState(LocalIsolate * isolate)186 ReusableUnoptimizedCompileState::ReusableUnoptimizedCompileState(
187 LocalIsolate* isolate)
188 : hash_seed_(HashSeed(isolate)),
189 allocator_(isolate->allocator()),
190 logger_(isolate->main_thread_logger()),
191 dispatcher_(isolate->lazy_compile_dispatcher()),
192 ast_string_constants_(isolate->ast_string_constants()),
193 ast_raw_string_zone_(allocator_,
194 "unoptimized-compile-ast-raw-string-zone"),
195 single_parse_zone_(allocator_, "unoptimized-compile-parse-zone"),
196 ast_value_factory_(
197 new AstValueFactory(ast_raw_string_zone(), single_parse_zone(),
198 ast_string_constants(), hash_seed())) {}
199
200 ReusableUnoptimizedCompileState::~ReusableUnoptimizedCompileState() = default;
201
ParseInfo(const UnoptimizedCompileFlags flags,UnoptimizedCompileState * state,ReusableUnoptimizedCompileState * reusable_state,uintptr_t stack_limit,RuntimeCallStats * runtime_call_stats)202 ParseInfo::ParseInfo(const UnoptimizedCompileFlags flags,
203 UnoptimizedCompileState* state,
204 ReusableUnoptimizedCompileState* reusable_state,
205 uintptr_t stack_limit,
206 RuntimeCallStats* runtime_call_stats)
207 : flags_(flags),
208 state_(state),
209 reusable_state_(reusable_state),
210 extension_(nullptr),
211 script_scope_(nullptr),
212 stack_limit_(stack_limit),
213 parameters_end_pos_(kNoSourcePosition),
214 max_function_literal_id_(kFunctionLiteralIdInvalid),
215 character_stream_(nullptr),
216 function_name_(nullptr),
217 runtime_call_stats_(runtime_call_stats),
218 source_range_map_(nullptr),
219 literal_(nullptr),
220 allow_eval_cache_(false),
221 #if V8_ENABLE_WEBASSEMBLY
222 contains_asm_module_(false),
223 #endif // V8_ENABLE_WEBASSEMBLY
224 language_mode_(flags.outer_language_mode()) {
225 if (flags.block_coverage_enabled()) {
226 AllocateSourceRangeMap();
227 }
228 }
229
ParseInfo(Isolate * isolate,const UnoptimizedCompileFlags flags,UnoptimizedCompileState * state,ReusableUnoptimizedCompileState * reusable_state)230 ParseInfo::ParseInfo(Isolate* isolate, const UnoptimizedCompileFlags flags,
231 UnoptimizedCompileState* state,
232 ReusableUnoptimizedCompileState* reusable_state)
233 : ParseInfo(flags, state, reusable_state,
234 isolate->stack_guard()->real_climit(),
235 isolate->counters()->runtime_call_stats()) {}
236
ParseInfo(LocalIsolate * isolate,const UnoptimizedCompileFlags flags,UnoptimizedCompileState * state,ReusableUnoptimizedCompileState * reusable_state,uintptr_t stack_limit)237 ParseInfo::ParseInfo(LocalIsolate* isolate, const UnoptimizedCompileFlags flags,
238 UnoptimizedCompileState* state,
239 ReusableUnoptimizedCompileState* reusable_state,
240 uintptr_t stack_limit)
241 : ParseInfo(flags, state, reusable_state, stack_limit,
242 isolate->runtime_call_stats()) {}
243
~ParseInfo()244 ParseInfo::~ParseInfo() { reusable_state_->NotifySingleParseCompleted(); }
245
scope() const246 DeclarationScope* ParseInfo::scope() const { return literal()->scope(); }
247
248 template <typename IsolateT>
CreateScript(IsolateT * isolate,Handle<String> source,MaybeHandle<FixedArray> maybe_wrapped_arguments,ScriptOriginOptions origin_options,NativesFlag natives)249 Handle<Script> ParseInfo::CreateScript(
250 IsolateT* isolate, Handle<String> source,
251 MaybeHandle<FixedArray> maybe_wrapped_arguments,
252 ScriptOriginOptions origin_options, NativesFlag natives) {
253 // Create a script object describing the script to be compiled.
254 DCHECK(flags().script_id() >= 0 ||
255 flags().script_id() == Script::kTemporaryScriptId);
256 Handle<Script> script =
257 isolate->factory()->NewScriptWithId(source, flags().script_id());
258 switch (natives) {
259 case EXTENSION_CODE:
260 script->set_type(Script::TYPE_EXTENSION);
261 break;
262 case INSPECTOR_CODE:
263 script->set_type(Script::TYPE_INSPECTOR);
264 break;
265 case NOT_NATIVES_CODE:
266 break;
267 }
268 script->set_origin_options(origin_options);
269 script->set_is_repl_mode(flags().is_repl_mode());
270
271 DCHECK_EQ(is_wrapped_as_function(), !maybe_wrapped_arguments.is_null());
272 if (is_wrapped_as_function()) {
273 script->set_wrapped_arguments(*maybe_wrapped_arguments.ToHandleChecked());
274 } else if (flags().is_eval()) {
275 script->set_compilation_type(Script::COMPILATION_TYPE_EVAL);
276 }
277
278 CheckFlagsForToplevelCompileFromScript(*script,
279 isolate->is_collecting_type_profile());
280 return script;
281 }
282
283 template EXPORT_TEMPLATE_DEFINE(V8_EXPORT_PRIVATE)
284 Handle<Script> ParseInfo::CreateScript(
285 Isolate* isolate, Handle<String> source,
286 MaybeHandle<FixedArray> maybe_wrapped_arguments,
287 ScriptOriginOptions origin_options, NativesFlag natives);
288 template EXPORT_TEMPLATE_DEFINE(V8_EXPORT_PRIVATE)
289 Handle<Script> ParseInfo::CreateScript(
290 LocalIsolate* isolate, Handle<String> source,
291 MaybeHandle<FixedArray> maybe_wrapped_arguments,
292 ScriptOriginOptions origin_options, NativesFlag natives);
293
AllocateSourceRangeMap()294 void ParseInfo::AllocateSourceRangeMap() {
295 DCHECK(flags().block_coverage_enabled());
296 DCHECK_NULL(source_range_map());
297 set_source_range_map(zone()->New<SourceRangeMap>(zone()));
298 }
299
ResetCharacterStream()300 void ParseInfo::ResetCharacterStream() { character_stream_.reset(); }
301
set_character_stream(std::unique_ptr<Utf16CharacterStream> character_stream)302 void ParseInfo::set_character_stream(
303 std::unique_ptr<Utf16CharacterStream> character_stream) {
304 DCHECK_NULL(character_stream_);
305 character_stream_.swap(character_stream);
306 }
307
CheckFlagsForToplevelCompileFromScript(Script script,bool is_collecting_type_profile)308 void ParseInfo::CheckFlagsForToplevelCompileFromScript(
309 Script script, bool is_collecting_type_profile) {
310 CheckFlagsForFunctionFromScript(script);
311 DCHECK(flags().is_toplevel());
312 DCHECK_EQ(flags().collect_type_profile(),
313 is_collecting_type_profile && script.IsUserJavaScript());
314 DCHECK_EQ(flags().is_repl_mode(), script.is_repl_mode());
315
316 if (script.is_wrapped()) {
317 DCHECK_EQ(flags().function_syntax_kind(), FunctionSyntaxKind::kWrapped);
318 }
319 }
320
CheckFlagsForFunctionFromScript(Script script)321 void ParseInfo::CheckFlagsForFunctionFromScript(Script script) {
322 DCHECK_EQ(flags().script_id(), script.id());
323 // We set "is_eval" for wrapped scripts to get an outer declaration scope.
324 // This is a bit hacky, but ok since we can't be both eval and wrapped.
325 DCHECK_EQ(flags().is_eval() && !script.is_wrapped(),
326 script.compilation_type() == Script::COMPILATION_TYPE_EVAL);
327 DCHECK_EQ(flags().is_module(), script.origin_options().IsModule());
328 DCHECK_IMPLIES(flags().block_coverage_enabled() && script.IsUserJavaScript(),
329 source_range_map() != nullptr);
330 }
331
332 } // namespace internal
333 } // namespace v8
334