v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
parse-info.h
Go to the documentation of this file.
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#ifndef V8_PARSING_PARSE_INFO_H_
6#define V8_PARSING_PARSE_INFO_H_
7
8#include <memory>
9
11#include "src/base/bit-field.h"
13#include "src/base/logging.h"
14#include "src/common/globals.h"
15#include "src/handles/handles.h"
18#include "src/objects/script.h"
21
22namespace v8 {
23
24class Extension;
25
26namespace internal {
27
28class AccountingAllocator;
29class AstRawString;
30class AstStringConstants;
31class AstValueFactory;
32class LazyCompileDispatcher;
33class DeclarationScope;
34class FunctionLiteral;
35class RuntimeCallStats;
36class V8FileLogger;
37class SourceRangeMap;
38class Utf16CharacterStream;
39class Zone;
40
41// The flags for a parse + unoptimized compile operation.
42#define FLAG_FIELDS(V, _) \
43 V(is_toplevel, bool, 1, _) \
44 V(is_eager, bool, 1, _) \
45 V(is_eval, bool, 1, _) \
46 V(is_reparse, bool, 1, _) \
47 V(outer_language_mode, LanguageMode, 1, _) \
48 V(parse_restriction, ParseRestriction, 1, _) \
49 V(is_module, bool, 1, _) \
50 V(allow_lazy_parsing, bool, 1, _) \
51 V(is_lazy_compile, bool, 1, _) \
52 V(coverage_enabled, bool, 1, _) \
53 V(block_coverage_enabled, bool, 1, _) \
54 V(is_asm_wasm_broken, bool, 1, _) \
55 V(class_scope_has_private_brand, bool, 1, _) \
56 V(private_name_lookup_skips_outer_class, bool, 1, _) \
57 V(requires_instance_members_initializer, bool, 1, _) \
58 V(has_static_private_methods_or_accessors, bool, 1, _) \
59 V(might_always_turbofan, bool, 1, _) \
60 V(allow_natives_syntax, bool, 1, _) \
61 V(allow_lazy_compile, bool, 1, _) \
62 V(post_parallel_compile_tasks_for_eager_toplevel, bool, 1, _) \
63 V(post_parallel_compile_tasks_for_lazy, bool, 1, _) \
64 V(collect_source_positions, bool, 1, _) \
65 V(is_repl_mode, bool, 1, _) \
66 V(produce_compile_hints, bool, 1, _) \
67 V(compile_hints_magic_enabled, bool, 1, _) \
68 V(compile_hints_per_function_magic_enabled, bool, 1, _)
69
71 public:
72 // Set-up flags for a toplevel compilation.
73 static UnoptimizedCompileFlags ForToplevelCompile(Isolate* isolate,
74 bool is_user_javascript,
75 LanguageMode language_mode,
76 REPLMode repl_mode,
77 ScriptType type, bool lazy);
78
79 // Set-up flags for a compiling a particular function (either a lazy compile
80 // or a recompile).
81 static UnoptimizedCompileFlags ForFunctionCompile(
82 Isolate* isolate, Tagged<SharedFunctionInfo> shared);
83
84 // Set-up flags for a full compilation of a given script.
85 static UnoptimizedCompileFlags ForScriptCompile(Isolate* isolate,
86 Tagged<Script> script);
87
88 // Set-up flags for a parallel toplevel function compilation, based on the
89 // flags of an existing toplevel compilation.
90 static UnoptimizedCompileFlags ForToplevelFunction(
91 const UnoptimizedCompileFlags toplevel_flags,
93
94 // Create flags for a test.
95 static UnoptimizedCompileFlags ForTest(Isolate* isolate);
96
97#define FLAG_GET_SET(NAME, TYPE, SIZE, _) \
98 TYPE NAME() const { return BitFields::NAME::decode(flags_); } \
99 UnoptimizedCompileFlags& set_##NAME(TYPE value) { \
100 flags_ = BitFields::NAME::update(flags_, value); \
101 return *this; \
102 }
103
105
106 int script_id() const { return script_id_; }
109 return *this;
110 }
111
112 FunctionKind function_kind() const { return function_kind_; }
114 function_kind_ = value;
115 return *this;
116 }
117
119 return function_syntax_kind_;
120 }
122 function_syntax_kind_ = value;
123 return *this;
124 }
125
127 return parsing_while_debugging_;
128 }
130 ParsingWhileDebugging value) {
131 parsing_while_debugging_ = value;
132 return *this;
133 }
134
135 private:
139
140 UnoptimizedCompileFlags(Isolate* isolate, int script_id);
141
142 // Set function info flags based on those in either FunctionLiteral or
143 // SharedFunctionInfo |function|
144 template <typename T>
145 void SetFlagsFromFunction(T function);
146 void SetFlagsForToplevelCompile(bool is_user_javascript,
147 LanguageMode language_mode,
148 REPLMode repl_mode, ScriptType type,
149 bool lazy);
150 void SetFlagsForFunctionFromScript(Tagged<Script> script);
151
152 uint32_t flags_;
157};
158
159#undef FLAG_FIELDS
160class ParseInfo;
161
162// The mutable state for a parse + unoptimized compile operation.
164 public:
166 return &pending_error_handler_;
167 }
169 return &pending_error_handler_;
170 }
171
172 private:
174};
175
176// A container for ParseInfo fields that are reusable across multiple parses and
177// unoptimized compiles.
178//
179// Note that this is different from UnoptimizedCompileState, which has mutable
180// state for a single compilation that is not reusable across multiple
181// compilations.
183 public:
184 explicit ReusableUnoptimizedCompileState(Isolate* isolate);
187
188 // The AstRawString Zone stores the AstRawStrings in the AstValueFactory that
189 // can be reused across parses, and thereforce should stay alive between
190 // parses that reuse this reusable state and its AstValueFactory.
191 Zone* ast_raw_string_zone() { return &ast_raw_string_zone_; }
192
193 // The single parse Zone stores the data of a single parse, and can be cleared
194 // when that parse completes.
195 //
196 // This is in "reusable" state despite being wiped per-parse, because it
197 // allows us to reuse the Zone itself, and e.g. keep the same single parse
198 // Zone pointer in the AstValueFactory.
199 Zone* single_parse_zone() { return &single_parse_zone_; }
200
201 void NotifySingleParseCompleted() { single_parse_zone_.Reset(); }
202
204 return ast_value_factory_.get();
205 }
206 uint64_t hash_seed() const { return hash_seed_; }
209 return ast_string_constants_;
210 }
211 // TODO(cbruni): Switch this back to the main logger.
212 V8FileLogger* v8_file_logger() const { return v8_file_logger_; }
213 LazyCompileDispatcher* dispatcher() const { return dispatcher_; }
214
215 private:
216 uint64_t hash_seed_;
223 std::unique_ptr<AstValueFactory> ast_value_factory_;
224};
225
226// A container for the inputs, configuration options, and outputs of parsing.
228 public:
229 ParseInfo(Isolate* isolate, const UnoptimizedCompileFlags flags,
231 ReusableUnoptimizedCompileState* reusable_state);
232 ParseInfo(LocalIsolate* isolate, const UnoptimizedCompileFlags flags,
234 ReusableUnoptimizedCompileState* reusable_state,
235 uintptr_t stack_limit);
236
237 ~ParseInfo();
238
239 template <typename IsolateT>
241 Handle<Script> CreateScript(
242 IsolateT* isolate, DirectHandle<String> source,
243 MaybeDirectHandle<FixedArray> maybe_wrapped_arguments,
244 ScriptOriginOptions origin_options,
245 NativesFlag natives = NOT_NATIVES_CODE);
246
247 Zone* zone() const { return reusable_state_->single_parse_zone(); }
248
249 const UnoptimizedCompileFlags& flags() const { return flags_; }
250
251 // Getters for reusable state.
252 uint64_t hash_seed() const { return reusable_state_->hash_seed(); }
254 return reusable_state_->allocator();
255 }
257 return reusable_state_->ast_string_constants();
258 }
260 return reusable_state_->v8_file_logger();
261 }
263 return reusable_state_->dispatcher();
264 }
265 const UnoptimizedCompileState* state() const { return state_; }
266
267 // Getters for state.
269 return state_->pending_error_handler();
270 }
271
272 // Accessors for per-thread state.
273 uintptr_t stack_limit() const { return stack_limit_; }
274 RuntimeCallStats* runtime_call_stats() const { return runtime_call_stats_; }
275
276 // Accessor methods for output flags.
277 bool allow_eval_cache() const { return allow_eval_cache_; }
278 void set_allow_eval_cache(bool value) { allow_eval_cache_ = value; }
279
280#if V8_ENABLE_WEBASSEMBLY
281 bool contains_asm_module() const { return contains_asm_module_; }
282 void set_contains_asm_module(bool value) { contains_asm_module_ = value; }
283#endif // V8_ENABLE_WEBASSEMBLY
284
287
289 return character_stream_.get();
290 }
291 void set_character_stream(
292 std::unique_ptr<Utf16CharacterStream> character_stream);
293 void ResetCharacterStream();
294
295 v8::Extension* extension() const { return extension_; }
297
298 void set_consumed_preparse_data(std::unique_ptr<ConsumedPreparseData> data) {
299 consumed_preparse_data_.swap(data);
300 }
302 return consumed_preparse_data_.get();
303 }
304
305 DeclarationScope* script_scope() const { return script_scope_; }
306 void set_script_scope(DeclarationScope* script_scope) {
307 script_scope_ = script_scope;
308 }
309
311 return reusable_state_->ast_value_factory();
312 }
313
314 const AstRawString* function_name() const { return function_name_; }
315 void set_function_name(const AstRawString* function_name) {
316 function_name_ = function_name;
317 }
318
319 FunctionLiteral* literal() const { return literal_; }
321
322 DeclarationScope* scope() const;
323
324 int parameters_end_pos() const { return parameters_end_pos_; }
325 void set_parameters_end_pos(int parameters_end_pos) {
326 parameters_end_pos_ = parameters_end_pos;
327 }
328
330 return flags().function_syntax_kind() == FunctionSyntaxKind::kWrapped;
331 }
332
333 int max_info_id() const { return max_info_id_; }
334 void set_max_info_id(int max_info_id) { max_info_id_ = max_info_id; }
335
337 SourceRangeMap* source_range_map() const { return source_range_map_; }
338 void set_source_range_map(SourceRangeMap* source_range_map) {
339 source_range_map_ = source_range_map;
340 }
341
342 void CheckFlagsForFunctionFromScript(Tagged<Script> script);
343
344 bool is_background_compilation() const { return is_background_compilation_; }
345
346 void set_is_background_compilation() { is_background_compilation_ = true; }
347
348 bool is_streaming_compilation() const { return is_streaming_compilation_; }
349
350 void set_is_streaming_compilation() { is_streaming_compilation_ = true; }
351
352 bool has_module_in_scope_chain() const { return has_module_in_scope_chain_; }
353 void set_has_module_in_scope_chain() { has_module_in_scope_chain_ = true; }
354
356 DCHECK_NULL(compile_hint_callback_);
357 DCHECK_NULL(compile_hint_callback_data_);
358 compile_hint_callback_ = callback;
359 compile_hint_callback_data_ = data;
360 }
361
363 return compile_hint_callback_;
364 }
365
367 return compile_hint_callback_data_;
368 }
369
370 private:
372 ReusableUnoptimizedCompileState* reusable_state,
373 uintptr_t stack_limit, RuntimeCallStats* runtime_call_stats);
374
375 void CheckFlagsForToplevelCompileFromScript(Tagged<Script> script);
376
377 //------------- Inputs to parsing and scope analysis -----------------------
381
384 uintptr_t stack_limit_;
387
388 v8::CompileHintCallback compile_hint_callback_ = nullptr;
389 void* compile_hint_callback_data_ = nullptr;
390
391 //----------- Inputs+Outputs of parsing and scope analysis -----------------
392 std::unique_ptr<Utf16CharacterStream> character_stream_;
393 std::unique_ptr<ConsumedPreparseData> consumed_preparse_data_;
396 SourceRangeMap* source_range_map_; // Used when block coverage is enabled.
397
398 //----------- Output of parsing and scope analysis ------------------------
401#if V8_ENABLE_WEBASSEMBLY
402 bool contains_asm_module_ : 1;
403#endif // V8_ENABLE_WEBASSEMBLY
408};
409
410} // namespace internal
411} // namespace v8
412
413#endif // V8_PARSING_PARSE_INFO_H_
friend Zone
Definition asm-types.cc:195
#define DEFINE_BIT_FIELDS(LIST_MACRO)
Definition bit-field.h:126
RegisterAllocator * allocator_
union v8::internal::@341::BuiltinMetadata::KindSpecificData data
V8FileLogger * v8_file_logger() const
Definition parse-info.h:259
DeclarationScope * script_scope_
Definition parse-info.h:383
const AstRawString * function_name() const
Definition parse-info.h:314
Zone * zone() const
Definition parse-info.h:247
std::unique_ptr< Utf16CharacterStream > character_stream_
Definition parse-info.h:392
void set_has_module_in_scope_chain()
Definition parse-info.h:353
RuntimeCallStats * runtime_call_stats() const
Definition parse-info.h:274
bool is_streaming_compilation() const
Definition parse-info.h:348
const UnoptimizedCompileState * state() const
Definition parse-info.h:265
void set_function_name(const AstRawString *function_name)
Definition parse-info.h:315
void set_extension(v8::Extension *extension)
Definition parse-info.h:296
DeclarationScope * script_scope() const
Definition parse-info.h:305
AccountingAllocator * allocator() const
Definition parse-info.h:253
void set_allow_eval_cache(bool value)
Definition parse-info.h:278
FunctionLiteral * literal_
Definition parse-info.h:399
SourceRangeMap * source_range_map_
Definition parse-info.h:396
LazyCompileDispatcher * dispatcher() const
Definition parse-info.h:262
bool has_module_in_scope_chain() const
Definition parse-info.h:352
PendingCompilationErrorHandler * pending_error_handler()
Definition parse-info.h:268
Utf16CharacterStream * character_stream() const
Definition parse-info.h:288
const AstStringConstants * ast_string_constants() const
Definition parse-info.h:256
bool is_background_compilation() const
Definition parse-info.h:344
void set_language_mode(LanguageMode value)
Definition parse-info.h:286
UnoptimizedCompileState * state_
Definition parse-info.h:379
ConsumedPreparseData * consumed_preparse_data()
Definition parse-info.h:301
bool allow_eval_cache() const
Definition parse-info.h:277
bool is_wrapped_as_function() const
Definition parse-info.h:329
LanguageMode language_mode_
Definition parse-info.h:404
const UnoptimizedCompileFlags & flags() const
Definition parse-info.h:249
uint64_t hash_seed() const
Definition parse-info.h:252
FunctionLiteral * literal() const
Definition parse-info.h:319
int parameters_end_pos() const
Definition parse-info.h:324
void set_consumed_preparse_data(std::unique_ptr< ConsumedPreparseData > data)
Definition parse-info.h:298
LanguageMode language_mode() const
Definition parse-info.h:285
void set_is_background_compilation()
Definition parse-info.h:346
void set_max_info_id(int max_info_id)
Definition parse-info.h:334
uintptr_t stack_limit() const
Definition parse-info.h:273
void SetCompileHintCallbackAndData(CompileHintCallback callback, void *data)
Definition parse-info.h:355
SourceRangeMap * source_range_map() const
Definition parse-info.h:337
const UnoptimizedCompileFlags flags_
Definition parse-info.h:378
void * compile_hint_callback_data() const
Definition parse-info.h:366
v8::Extension * extension() const
Definition parse-info.h:295
void set_is_streaming_compilation()
Definition parse-info.h:350
v8::Extension * extension_
Definition parse-info.h:382
ReusableUnoptimizedCompileState * reusable_state_
Definition parse-info.h:380
CompileHintCallback compile_hint_callback() const
Definition parse-info.h:362
AstValueFactory * ast_value_factory() const
Definition parse-info.h:310
void set_source_range_map(SourceRangeMap *source_range_map)
Definition parse-info.h:338
void set_script_scope(DeclarationScope *script_scope)
Definition parse-info.h:306
const AstRawString * function_name_
Definition parse-info.h:394
void set_literal(FunctionLiteral *literal)
Definition parse-info.h:320
RuntimeCallStats * runtime_call_stats_
Definition parse-info.h:395
std::unique_ptr< ConsumedPreparseData > consumed_preparse_data_
Definition parse-info.h:393
void set_parameters_end_pos(int parameters_end_pos)
Definition parse-info.h:325
AstValueFactory * ast_value_factory() const
Definition parse-info.h:203
AccountingAllocator * allocator() const
Definition parse-info.h:207
const AstStringConstants * ast_string_constants_
Definition parse-info.h:220
const AstStringConstants * ast_string_constants() const
Definition parse-info.h:208
std::unique_ptr< AstValueFactory > ast_value_factory_
Definition parse-info.h:223
LazyCompileDispatcher * dispatcher() const
Definition parse-info.h:213
UnoptimizedCompileFlags & set_function_kind(FunctionKind value)
Definition parse-info.h:113
FunctionSyntaxKind function_syntax_kind_
Definition parse-info.h:155
UnoptimizedCompileFlags & set_parsing_while_debugging(ParsingWhileDebugging value)
Definition parse-info.h:129
FunctionSyntaxKind function_syntax_kind() const
Definition parse-info.h:118
UnoptimizedCompileFlags & set_function_syntax_kind(FunctionSyntaxKind value)
Definition parse-info.h:121
ParsingWhileDebugging parsing_while_debugging_
Definition parse-info.h:156
UnoptimizedCompileFlags & set_script_id(int value)
Definition parse-info.h:107
ParsingWhileDebugging parsing_while_debugging() const
Definition parse-info.h:126
const PendingCompilationErrorHandler * pending_error_handler() const
Definition parse-info.h:165
PendingCompilationErrorHandler pending_error_handler_
Definition parse-info.h:173
PendingCompilationErrorHandler * pending_error_handler()
Definition parse-info.h:168
LanguageMode language_mode_
JSRegExp::Flags flags_
enum v8::internal::@1270::DeoptimizableCodeIterator::@67 state_
#define EXPORT_TEMPLATE_DECLARE(export)
other heap size flags(e.g. initial_heap_size) take precedence") DEFINE_SIZE_T( max_shared_heap_size
std::string extension
TNode< Object > callback
#define _
FunctionLiteral * literal
Definition liveedit.cc:294
bool(*)(int, void *) CompileHintCallback
ScriptType
Definition v8-script.h:397
#define FLAG_FIELDS(V, _)
Definition parse-info.h:42
#define FLAG_GET_SET(NAME, TYPE, SIZE, _)
Definition parse-info.h:97
const uintptr_t stack_limit_
#define DCHECK_NULL(val)
Definition logging.h:491
#define V8_EXPORT_PRIVATE
Definition macros.h:460
std::unique_ptr< ValueMirror > value
int script_id_