|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * vim: set ts=8 sts=4 et sw=4 tw=99: |
|
3 * This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #ifndef jit_PcScriptCache_h |
|
8 #define jit_PcScriptCache_h |
|
9 |
|
10 // Defines a fixed-size hash table solely for the purpose of caching jit::GetPcScript(). |
|
11 // One cache is attached to each JSRuntime; it functions as if cleared on GC. |
|
12 |
|
13 struct JSRuntime; |
|
14 |
|
15 namespace js { |
|
16 namespace jit { |
|
17 |
|
18 struct PcScriptCacheEntry |
|
19 { |
|
20 uint8_t *returnAddress; // Key into the hash table. |
|
21 jsbytecode *pc; // Cached PC. |
|
22 JSScript *script; // Cached script. |
|
23 }; |
|
24 |
|
25 struct PcScriptCache |
|
26 { |
|
27 static const uint32_t Length = 73; |
|
28 |
|
29 // GC number at the time the cache was filled or created. |
|
30 // Storing and checking against this number allows us to not bother |
|
31 // clearing this cache on every GC -- only when actually necessary. |
|
32 uint64_t gcNumber; |
|
33 |
|
34 // List of cache entries. |
|
35 mozilla::Array<PcScriptCacheEntry, Length> entries; |
|
36 |
|
37 void clear(uint64_t gcNumber) { |
|
38 for (uint32_t i = 0; i < Length; i++) |
|
39 entries[i].returnAddress = nullptr; |
|
40 this->gcNumber = gcNumber; |
|
41 } |
|
42 |
|
43 // Get a value from the cache. May perform lazy allocation. |
|
44 bool get(JSRuntime *rt, uint32_t hash, uint8_t *addr, |
|
45 JSScript **scriptRes, jsbytecode **pcRes) |
|
46 { |
|
47 // If a GC occurred, lazily clear the cache now. |
|
48 if (gcNumber != rt->gcNumber) { |
|
49 clear(rt->gcNumber); |
|
50 return false; |
|
51 } |
|
52 |
|
53 if (entries[hash].returnAddress != addr) |
|
54 return false; |
|
55 |
|
56 *scriptRes = entries[hash].script; |
|
57 if (pcRes) |
|
58 *pcRes = entries[hash].pc; |
|
59 |
|
60 return true; |
|
61 } |
|
62 |
|
63 void add(uint32_t hash, uint8_t *addr, jsbytecode *pc, JSScript *script) { |
|
64 entries[hash].returnAddress = addr; |
|
65 entries[hash].pc = pc; |
|
66 entries[hash].script = script; |
|
67 } |
|
68 |
|
69 static uint32_t Hash(uint8_t *addr) { |
|
70 uint32_t key = (uint32_t)((uintptr_t)addr); |
|
71 return ((key >> 3) * 2654435761u) % Length; |
|
72 } |
|
73 }; |
|
74 |
|
75 } // namespace jit |
|
76 } // namespace js |
|
77 |
|
78 #endif /* jit_PcScriptCache_h */ |