compilation-cache-table-inl.h 4.05 KB
Newer Older
1 2 3 4
// Copyright 2017 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.

5 6
#ifndef V8_OBJECTS_COMPILATION_CACHE_TABLE_INL_H_
#define V8_OBJECTS_COMPILATION_CACHE_TABLE_INL_H_
7

8
#include "src/objects/compilation-cache-table.h"
9 10 11
#include "src/objects/name-inl.h"
#include "src/objects/script-inl.h"
#include "src/objects/shared-function-info.h"
12
#include "src/objects/smi.h"
13 14
#include "src/objects/string.h"

15 16 17 18 19 20
// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"

namespace v8 {
namespace internal {

21 22 23 24 25 26
CompilationCacheTable::CompilationCacheTable(Address ptr)
    : HashTable<CompilationCacheTable, CompilationCacheShape>(ptr) {
  SLOW_DCHECK(IsCompilationCacheTable());
}

NEVER_READ_ONLY_SPACE_IMPL(CompilationCacheTable)
27
CAST_ACCESSOR(CompilationCacheTable)
28

29
uint32_t CompilationCacheShape::RegExpHash(String string, Smi flags) {
30
  return string.EnsureHash() + flags.value();
31 32
}

33
uint32_t CompilationCacheShape::StringSharedHash(String source,
34
                                                 SharedFunctionInfo shared,
35 36
                                                 LanguageMode language_mode,
                                                 int position) {
37
  uint32_t hash = source.EnsureHash();
38
  if (shared.HasSourceCode()) {
39 40 41 42 43
    // Instead of using the SharedFunctionInfo pointer in the hash
    // code computation, we use a combination of the hash of the
    // script source code and the start position of the calling scope.
    // We do this to ensure that the cache entries can survive garbage
    // collection.
44
    Script script(Script::cast(shared.script()));
45
    hash ^= String::cast(script.source()).EnsureHash();
46
  }
47 48 49 50 51 52 53 54 55 56 57
  STATIC_ASSERT(LanguageModeSize == 2);
  if (is_strict(language_mode)) hash ^= 0x8000;
  hash += position;
  return hash;
}

uint32_t CompilationCacheShape::StringSharedHash(String source,
                                                 LanguageMode language_mode) {
  uint32_t hash = source.EnsureHash();
  STATIC_ASSERT(LanguageModeSize == 2);
  if (is_strict(language_mode)) hash ^= 0x8000;
58 59 60
  return hash;
}

61 62
uint32_t CompilationCacheShape::HashForObject(ReadOnlyRoots roots,
                                              Object object) {
63
  // Eval: The key field contains the hash as a Number.
64
  if (object.IsNumber()) return static_cast<uint32_t>(object.Number());
65

66 67 68 69 70 71
  // Code: The key field contains the SFI key.
  if (object.IsSharedFunctionInfo()) {
    return SharedFunctionInfo::cast(object).Hash();
  }

  // Script: See StringSharedKey::ToHandle for the encoding.
72
  FixedArray val = FixedArray::cast(object);
73 74 75 76
  if (val.map() == roots.fixed_cow_array_map()) {
    DCHECK_EQ(4, val.length());
    String source = String::cast(val.get(1));
    int language_unchecked = Smi::ToInt(val.get(2));
77 78
    DCHECK(is_valid_language_mode(language_unchecked));
    LanguageMode language_mode = static_cast<LanguageMode>(language_unchecked);
79
    int position = Smi::ToInt(val.get(3));
80 81 82 83 84 85 86 87
    Object shared_or_smi = val.get(0);
    if (shared_or_smi.IsSmi()) {
      DCHECK_EQ(position, kNoSourcePosition);
      return StringSharedHash(source, language_mode);
    } else {
      return StringSharedHash(source, SharedFunctionInfo::cast(shared_or_smi),
                              language_mode, position);
    }
88
  }
89 90 91 92

  // RegExp: The key field (and the value field) contains the
  // JSRegExp::data fixed array.
  DCHECK_GE(val.length(), JSRegExp::kMinDataArrayLength);
93 94
  return RegExpHash(String::cast(val.get(JSRegExp::kSourceIndex)),
                    Smi::cast(val.get(JSRegExp::kFlagsIndex)));
95 96
}

97
InfoCellPair::InfoCellPair(Isolate* isolate, SharedFunctionInfo shared,
98
                           FeedbackCell feedback_cell)
99
    : is_compiled_scope_(!shared.is_null() ? shared.is_compiled_scope(isolate)
100 101 102 103
                                           : IsCompiledScope()),
      shared_(shared),
      feedback_cell_(feedback_cell) {}

104 105 106 107 108
}  // namespace internal
}  // namespace v8

#include "src/objects/object-macros-undef.h"

109
#endif  // V8_OBJECTS_COMPILATION_CACHE_TABLE_INL_H_