1 // Copyright 2017 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_OBJECTS_COMPILATION_CACHE_TABLE_INL_H_
6 #define V8_OBJECTS_COMPILATION_CACHE_TABLE_INL_H_
7
8 #include "src/objects/compilation-cache-table.h"
9 #include "src/objects/name-inl.h"
10 #include "src/objects/script-inl.h"
11 #include "src/objects/shared-function-info.h"
12 #include "src/objects/smi.h"
13 #include "src/objects/string.h"
14
15 // Has to be the last include (doesn't have include guards):
16 #include "src/objects/object-macros.h"
17
18 namespace v8 {
19 namespace internal {
20
CompilationCacheTable(Address ptr)21 CompilationCacheTable::CompilationCacheTable(Address ptr)
22 : HashTable<CompilationCacheTable, CompilationCacheShape>(ptr) {
23 SLOW_DCHECK(IsCompilationCacheTable());
24 }
25
26 NEVER_READ_ONLY_SPACE_IMPL(CompilationCacheTable)
CAST_ACCESSOR(CompilationCacheTable)27 CAST_ACCESSOR(CompilationCacheTable)
28
29 uint32_t CompilationCacheShape::RegExpHash(String string, Smi flags) {
30 return string.EnsureHash() + flags.value();
31 }
32
StringSharedHash(String source,SharedFunctionInfo shared,LanguageMode language_mode,int position)33 uint32_t CompilationCacheShape::StringSharedHash(String source,
34 SharedFunctionInfo shared,
35 LanguageMode language_mode,
36 int position) {
37 uint32_t hash = source.EnsureHash();
38 if (shared.HasSourceCode()) {
39 // Instead of using the SharedFunctionInfo pointer in the hash
40 // code computation, we use a combination of the hash of the
41 // script source code and the start position of the calling scope.
42 // We do this to ensure that the cache entries can survive garbage
43 // collection.
44 Script script(Script::cast(shared.script()));
45 hash ^= String::cast(script.source()).EnsureHash();
46 }
47 STATIC_ASSERT(LanguageModeSize == 2);
48 if (is_strict(language_mode)) hash ^= 0x8000;
49 hash += position;
50 return hash;
51 }
52
StringSharedHash(String source,LanguageMode language_mode)53 uint32_t CompilationCacheShape::StringSharedHash(String source,
54 LanguageMode language_mode) {
55 uint32_t hash = source.EnsureHash();
56 STATIC_ASSERT(LanguageModeSize == 2);
57 if (is_strict(language_mode)) hash ^= 0x8000;
58 return hash;
59 }
60
HashForObject(ReadOnlyRoots roots,Object object)61 uint32_t CompilationCacheShape::HashForObject(ReadOnlyRoots roots,
62 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 // Code: The key field contains the SFI key.
67 if (object.IsSharedFunctionInfo()) {
68 return SharedFunctionInfo::cast(object).Hash();
69 }
70
71 // Script: See StringSharedKey::ToHandle for the encoding.
72 FixedArray val = FixedArray::cast(object);
73 if (val.map() == roots.fixed_cow_array_map()) {
74 DCHECK_EQ(4, val.length());
75 String source = String::cast(val.get(1));
76 int language_unchecked = Smi::ToInt(val.get(2));
77 DCHECK(is_valid_language_mode(language_unchecked));
78 LanguageMode language_mode = static_cast<LanguageMode>(language_unchecked);
79 int position = Smi::ToInt(val.get(3));
80 Object shared_or_smi = val.get(0);
81 if (shared_or_smi.IsSmi()) {
82 DCHECK_EQ(position, kNoSourcePosition);
83 return StringSharedHash(source, language_mode);
84 } else {
85 return StringSharedHash(source, SharedFunctionInfo::cast(shared_or_smi),
86 language_mode, position);
87 }
88 }
89
90 // RegExp: The key field (and the value field) contains the
91 // JSRegExp::data fixed array.
92 DCHECK_GE(val.length(), JSRegExp::kMinDataArrayLength);
93 return RegExpHash(String::cast(val.get(JSRegExp::kSourceIndex)),
94 Smi::cast(val.get(JSRegExp::kFlagsIndex)));
95 }
96
InfoCellPair(Isolate * isolate,SharedFunctionInfo shared,FeedbackCell feedback_cell)97 InfoCellPair::InfoCellPair(Isolate* isolate, SharedFunctionInfo shared,
98 FeedbackCell feedback_cell)
99 : is_compiled_scope_(!shared.is_null() ? shared.is_compiled_scope(isolate)
100 : IsCompiledScope()),
101 shared_(shared),
102 feedback_cell_(feedback_cell) {}
103
104 } // namespace internal
105 } // namespace v8
106
107 #include "src/objects/object-macros-undef.h"
108
109 #endif // V8_OBJECTS_COMPILATION_CACHE_TABLE_INL_H_
110