parse-info.cc 12.6 KB
Newer Older
1 2 3 4 5 6
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/parsing/parse-info.h"

7
#include "src/ast/ast-source-ranges.h"
8 9
#include "src/ast/ast-value-factory.h"
#include "src/ast/ast.h"
10
#include "src/base/logging.h"
11
#include "src/common/globals.h"
12
#include "src/compiler-dispatcher/compiler-dispatcher.h"
13
#include "src/heap/heap-inl.h"
14 15
#include "src/logging/counters.h"
#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 22 23

namespace v8 {
namespace internal {

24 25 26
UnoptimizedCompileFlags::UnoptimizedCompileFlags(Isolate* isolate,
                                                 int script_id)
    : flags_(0),
27
      script_id_(script_id),
28 29 30 31 32
      function_kind_(FunctionKind::kNormalFunction),
      function_syntax_kind_(FunctionSyntaxKind::kDeclaration) {
  set_collect_type_profile(isolate->is_collecting_type_profile());
  set_coverage_enabled(!isolate->is_best_effort_code_coverage());
  set_block_coverage_enabled(isolate->is_block_code_coverage());
33 34
  set_might_always_opt(FLAG_always_opt || FLAG_prepare_always_opt);
  set_allow_natives_syntax(FLAG_allow_natives_syntax);
35
  set_allow_lazy_compile(true);
36 37
  set_collect_source_positions(!FLAG_enable_lazy_source_positions ||
                               isolate->NeedsDetailedOptimizedCodeLineInfo());
38 39 40
  set_allow_harmony_top_level_await(FLAG_harmony_top_level_await);
}

41 42 43 44 45 46 47 48 49 50
// static
UnoptimizedCompileFlags UnoptimizedCompileFlags::ForFunctionCompile(
    Isolate* isolate, SharedFunctionInfo shared) {
  Script script = Script::cast(shared.script());

  UnoptimizedCompileFlags flags(isolate, script.id());

  flags.SetFlagsFromFunction(&shared);
  flags.SetFlagsForFunctionFromScript(script);
  flags.set_allow_lazy_parsing(true);
51 52
  flags.set_is_lazy_compile(true);

53
#if V8_ENABLE_WEBASSEMBLY
54
  flags.set_is_asm_wasm_broken(shared.is_asm_wasm_broken());
55
#endif  // V8_ENABLE_WEBASSEMBLY
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  flags.set_is_repl_mode(shared.is_repl_mode());

  // CollectTypeProfile uses its own feedback slots. If we have existing
  // FeedbackMetadata, we can only collect type profile if the feedback vector
  // has the appropriate slots.
  flags.set_collect_type_profile(
      isolate->is_collecting_type_profile() &&
      (shared.HasFeedbackMetadata()
           ? shared.feedback_metadata().HasTypeProfileSlot()
           : script.IsUserJavaScript()));

  // Do not support re-parsing top-level function of a wrapped script.
  DCHECK_IMPLIES(flags.is_toplevel(), !script.is_wrapped());

  return flags;
}

// static
UnoptimizedCompileFlags UnoptimizedCompileFlags::ForScriptCompile(
    Isolate* isolate, Script script) {
  UnoptimizedCompileFlags flags(isolate, script.id());

  flags.SetFlagsForFunctionFromScript(script);
  flags.SetFlagsForToplevelCompile(
      isolate->is_collecting_type_profile(), script.IsUserJavaScript(),
81 82
      flags.outer_language_mode(), construct_repl_mode(script.is_repl_mode()),
      script.origin_options().IsModule() ? ScriptType::kModule
83 84
                                         : ScriptType::kClassic,
      FLAG_lazy);
85 86 87 88 89 90 91 92 93 94
  if (script.is_wrapped()) {
    flags.set_function_syntax_kind(FunctionSyntaxKind::kWrapped);
  }

  return flags;
}

// static
UnoptimizedCompileFlags UnoptimizedCompileFlags::ForToplevelCompile(
    Isolate* isolate, bool is_user_javascript, LanguageMode language_mode,
95
    REPLMode repl_mode, ScriptType type, bool lazy) {
96 97
  UnoptimizedCompileFlags flags(isolate, isolate->GetNextScriptId());
  flags.SetFlagsForToplevelCompile(isolate->is_collecting_type_profile(),
98
                                   is_user_javascript, language_mode, repl_mode,
99
                                   type, lazy);
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

  LOG(isolate,
      ScriptEvent(Logger::ScriptEventType::kReserveId, flags.script_id()));
  return flags;
}

// static
UnoptimizedCompileFlags UnoptimizedCompileFlags::ForToplevelFunction(
    const UnoptimizedCompileFlags toplevel_flags,
    const FunctionLiteral* literal) {
  DCHECK(toplevel_flags.is_toplevel());
  DCHECK(!literal->is_toplevel());

  // Replicate the toplevel flags, then setup the function-specific flags.
  UnoptimizedCompileFlags flags = toplevel_flags;
  flags.SetFlagsFromFunction(literal);

  return flags;
}

// static
UnoptimizedCompileFlags UnoptimizedCompileFlags::ForTest(Isolate* isolate) {
122
  return UnoptimizedCompileFlags(isolate, Script::kTemporaryScriptId);
123 124 125
}

template <typename T>
126 127
void UnoptimizedCompileFlags::SetFlagsFromFunction(T function) {
  set_outer_language_mode(function->language_mode());
128 129 130 131 132 133 134
  set_function_kind(function->kind());
  set_function_syntax_kind(function->syntax_kind());
  set_requires_instance_members_initializer(
      function->requires_instance_members_initializer());
  set_class_scope_has_private_brand(function->class_scope_has_private_brand());
  set_has_static_private_methods_or_accessors(
      function->has_static_private_methods_or_accessors());
135
  set_is_toplevel(function->is_toplevel());
136 137
}

138 139
void UnoptimizedCompileFlags::SetFlagsForToplevelCompile(
    bool is_collecting_type_profile, bool is_user_javascript,
140 141
    LanguageMode language_mode, REPLMode repl_mode, ScriptType type,
    bool lazy) {
142
  set_is_toplevel(true);
143 144
  set_allow_lazy_parsing(lazy);
  set_allow_lazy_compile(lazy);
145 146 147 148
  set_collect_type_profile(is_user_javascript && is_collecting_type_profile);
  set_outer_language_mode(
      stricter_language_mode(outer_language_mode(), language_mode));
  set_is_repl_mode((repl_mode == REPLMode::kYes));
149 150
  set_is_module(type == ScriptType::kModule);
  DCHECK_IMPLIES(is_eval(), !is_module());
151

152 153
  set_block_coverage_enabled(block_coverage_enabled() && is_user_javascript);
}
154

155 156
void UnoptimizedCompileFlags::SetFlagsForFunctionFromScript(Script script) {
  DCHECK_EQ(script_id(), script.id());
157

158 159
  set_is_eval(script.compilation_type() == Script::COMPILATION_TYPE_EVAL);
  set_is_module(script.origin_options().IsModule());
160
  DCHECK_IMPLIES(is_eval(), !is_module());
161

162 163 164 165
  set_block_coverage_enabled(block_coverage_enabled() &&
                             script.IsUserJavaScript());
}

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
UnoptimizedCompileState::UnoptimizedCompileState(Isolate* isolate)
    : hash_seed_(HashSeed(isolate)),
      allocator_(isolate->allocator()),
      ast_string_constants_(isolate->ast_string_constants()),
      logger_(isolate->logger()),
      parallel_tasks_(isolate->compiler_dispatcher()->IsEnabled()
                          ? new ParallelTasks(isolate->compiler_dispatcher())
                          : nullptr) {}

UnoptimizedCompileState::UnoptimizedCompileState(
    const UnoptimizedCompileState& other) V8_NOEXCEPT
    : hash_seed_(other.hash_seed()),
      allocator_(other.allocator()),
      ast_string_constants_(other.ast_string_constants()),
      logger_(other.logger()),
      // TODO(leszeks): Should this create a new ParallelTasks instance?
      parallel_tasks_(nullptr) {}

ParseInfo::ParseInfo(const UnoptimizedCompileFlags flags,
                     UnoptimizedCompileState* state)
186
    : flags_(flags),
187
      state_(state),
188
      zone_(std::make_unique<Zone>(state->allocator(), "parser-zone")),
189 190 191 192 193 194 195 196 197 198 199 200
      extension_(nullptr),
      script_scope_(nullptr),
      stack_limit_(0),
      parameters_end_pos_(kNoSourcePosition),
      max_function_literal_id_(kFunctionLiteralIdInvalid),
      character_stream_(nullptr),
      ast_value_factory_(nullptr),
      function_name_(nullptr),
      runtime_call_stats_(nullptr),
      source_range_map_(nullptr),
      literal_(nullptr),
      allow_eval_cache_(false),
201
#if V8_ENABLE_WEBASSEMBLY
202
      contains_asm_module_(false),
203
#endif  // V8_ENABLE_WEBASSEMBLY
204 205 206 207
      language_mode_(flags.outer_language_mode()) {
  if (flags.block_coverage_enabled()) {
    AllocateSourceRangeMap();
  }
208 209
}

210 211 212 213 214
ParseInfo::ParseInfo(Isolate* isolate, const UnoptimizedCompileFlags flags,
                     UnoptimizedCompileState* state)
    : ParseInfo(flags, state) {
  SetPerThreadState(isolate->stack_guard()->real_climit(),
                    isolate->counters()->runtime_call_stats());
215 216
}

217
// static
218 219 220 221
std::unique_ptr<ParseInfo> ParseInfo::ForToplevelFunction(
    const UnoptimizedCompileFlags flags, UnoptimizedCompileState* compile_state,
    const FunctionLiteral* literal, const AstRawString* function_name) {
  std::unique_ptr<ParseInfo> result(new ParseInfo(flags, compile_state));
222 223 224 225 226 227 228 229 230 231 232 233 234 235

  // Clone the function_name AstRawString into the ParseInfo's own
  // AstValueFactory.
  const AstRawString* cloned_function_name =
      result->GetOrCreateAstValueFactory()->CloneFromOtherFactory(
          function_name);

  // Setup function specific details.
  DCHECK(!literal->is_toplevel());
  result->set_function_name(cloned_function_name);

  return result;
}

236
ParseInfo::~ParseInfo() = default;
237

238 239
DeclarationScope* ParseInfo::scope() const { return literal()->scope(); }

240
template <typename IsolateT>
241
Handle<Script> ParseInfo::CreateScript(
242
    IsolateT* isolate, Handle<String> source,
243 244
    MaybeHandle<FixedArray> maybe_wrapped_arguments,
    ScriptOriginOptions origin_options, NativesFlag natives) {
245
  // Create a script object describing the script to be compiled.
246 247
  DCHECK(flags().script_id() >= 0 ||
         flags().script_id() == Script::kTemporaryScriptId);
248
  Handle<Script> script =
249
      isolate->factory()->NewScriptWithId(source, flags().script_id());
250 251 252 253 254 255 256 257 258 259 260
  switch (natives) {
    case EXTENSION_CODE:
      script->set_type(Script::TYPE_EXTENSION);
      break;
    case INSPECTOR_CODE:
      script->set_type(Script::TYPE_INSPECTOR);
      break;
    case NOT_NATIVES_CODE:
      break;
  }
  script->set_origin_options(origin_options);
261 262 263 264 265 266
  script->set_is_repl_mode(flags().is_repl_mode());

  DCHECK_EQ(is_wrapped_as_function(), !maybe_wrapped_arguments.is_null());
  if (is_wrapped_as_function()) {
    script->set_wrapped_arguments(*maybe_wrapped_arguments.ToHandleChecked());
  } else if (flags().is_eval()) {
267 268
    script->set_compilation_type(Script::COMPILATION_TYPE_EVAL);
  }
269

270 271
  CheckFlagsForToplevelCompileFromScript(*script,
                                         isolate->is_collecting_type_profile());
272 273 274
  return script;
}

275
template EXPORT_TEMPLATE_DEFINE(V8_EXPORT_PRIVATE)
276 277 278 279
    Handle<Script> ParseInfo::CreateScript(
        Isolate* isolate, Handle<String> source,
        MaybeHandle<FixedArray> maybe_wrapped_arguments,
        ScriptOriginOptions origin_options, NativesFlag natives);
280
template EXPORT_TEMPLATE_DEFINE(V8_EXPORT_PRIVATE)
281
    Handle<Script> ParseInfo::CreateScript(
282
        LocalIsolate* isolate, Handle<String> source,
283 284
        MaybeHandle<FixedArray> maybe_wrapped_arguments,
        ScriptOriginOptions origin_options, NativesFlag natives);
285

286 287 288 289 290 291 292 293
AstValueFactory* ParseInfo::GetOrCreateAstValueFactory() {
  if (!ast_value_factory_.get()) {
    ast_value_factory_.reset(
        new AstValueFactory(zone(), ast_string_constants(), hash_seed()));
  }
  return ast_value_factory();
}

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
}

300 301 302
void ParseInfo::ResetCharacterStream() { character_stream_.reset(); }

void ParseInfo::set_character_stream(
303
    std::unique_ptr<Utf16CharacterStream> character_stream) {
304
  DCHECK_NULL(character_stream_);
305 306 307
  character_stream_.swap(character_stream);
}

308 309 310
void ParseInfo::CheckFlagsForToplevelCompileFromScript(
    Script script, bool is_collecting_type_profile) {
  CheckFlagsForFunctionFromScript(script);
311 312
  DCHECK(flags().is_toplevel());
  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
  }
}

321
void ParseInfo::CheckFlagsForFunctionFromScript(Script script) {
322 323
  DCHECK_EQ(flags().script_id(), script.id());
  // 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 328
  DCHECK_EQ(flags().is_module(), script.origin_options().IsModule());
  DCHECK_IMPLIES(flags().block_coverage_enabled() && script.IsUserJavaScript(),
329 330 331
                 source_range_map() != nullptr);
}

332 333 334
void UnoptimizedCompileState::ParallelTasks::Enqueue(
    ParseInfo* outer_parse_info, const AstRawString* function_name,
    FunctionLiteral* literal) {
335 336 337 338 339 340 341
  base::Optional<CompilerDispatcher::JobId> job_id =
      dispatcher_->Enqueue(outer_parse_info, function_name, literal);
  if (job_id) {
    enqueued_jobs_.emplace_front(std::make_pair(literal, *job_id));
  }
}

342 343
}  // namespace internal
}  // namespace v8