2 * Copyright (C) 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef DFGByteCodeCache_h
27 #define DFGByteCodeCache_h
29 #include <wtf/Platform.h>
33 #include "CodeBlock.h"
34 #include "Executable.h"
35 #include "JSFunction.h"
36 #include <wtf/HashMap.h>
38 namespace JSC
{ namespace DFG
{
44 , m_kind(CodeForCall
) // CodeForCall = empty value
48 CodeBlockKey(WTF::HashTableDeletedValueType
)
50 , m_kind(CodeForConstruct
) // CodeForConstruct = deleted value
54 CodeBlockKey(FunctionExecutable
* executable
, CodeSpecializationKind kind
)
55 : m_executable(executable
)
60 bool operator==(const CodeBlockKey
& other
) const
62 return m_executable
== other
.m_executable
63 && m_kind
== other
.m_kind
;
68 return WTF::PtrHash
<FunctionExecutable
*>::hash(m_executable
) ^ static_cast<unsigned>(m_kind
);
71 FunctionExecutable
* executable() const { return m_executable
; }
72 CodeSpecializationKind
kind() const { return m_kind
; }
74 bool isHashTableDeletedValue() const
76 return !m_executable
&& m_kind
== CodeForConstruct
;
80 FunctionExecutable
* m_executable
;
81 CodeSpecializationKind m_kind
;
84 struct CodeBlockKeyHash
{
85 static unsigned hash(const CodeBlockKey
& key
) { return key
.hash(); }
86 static bool equal(const CodeBlockKey
& a
, const CodeBlockKey
& b
) { return a
== b
; }
88 static const bool safeToCompareToEmptyOrDeleted
= true;
91 } } // namespace JSC::DFG
95 template<typename T
> struct DefaultHash
;
96 template<> struct DefaultHash
<JSC::DFG::CodeBlockKey
> {
97 typedef JSC::DFG::CodeBlockKeyHash Hash
;
100 template<typename T
> struct HashTraits
;
101 template<> struct HashTraits
<JSC::DFG::CodeBlockKey
> : SimpleClassHashTraits
<JSC::DFG::CodeBlockKey
> { };
105 namespace JSC
{ namespace DFG
{
107 struct ByteCodeCacheValue
{
108 FunctionCodeBlock
* codeBlock
;
110 bool oldValueOfShouldDiscardBytecode
;
112 // All uses of this struct initialize everything manually. But gcc isn't
113 // smart enough to see that, so this constructor is just here to make the
118 , oldValueOfShouldDiscardBytecode(false)
123 template<bool (*filterFunction
)(CodeBlock
*, CodeSpecializationKind
)>
124 class ByteCodeCache
{
126 typedef HashMap
<CodeBlockKey
, ByteCodeCacheValue
> Map
;
132 Map::iterator begin
= m_map
.begin();
133 Map::iterator end
= m_map
.end();
134 for (Map::iterator iter
= begin
; iter
!= end
; ++iter
) {
135 if (!iter
->second
.codeBlock
)
137 if (iter
->second
.owned
) {
138 delete iter
->second
.codeBlock
;
144 CodeBlock
* get(const CodeBlockKey
& key
, ScopeChainNode
* scope
)
146 Map::iterator iter
= m_map
.find(key
);
147 if (iter
!= m_map
.end())
148 return iter
->second
.codeBlock
;
150 ByteCodeCacheValue value
;
152 // First see if there is already a parsed code block that still has some
154 value
.codeBlock
= key
.executable()->codeBlockWithBytecodeFor(key
.kind());
155 if (value
.codeBlock
) {
158 // Nope, so try to parse one.
161 value
.codeBlock
= key
.executable()->produceCodeBlockFor(scope
, OptimizingCompilation
, key
.kind(), exception
).leakPtr();
164 // Check if there is any reason to reject this from our cache. If so, then
166 if (!!value
.codeBlock
&& !filterFunction(value
.codeBlock
, key
.kind())) {
168 delete value
.codeBlock
;
172 m_map
.add(key
, value
);
174 return value
.codeBlock
;
181 } } // namespace JSC::DFG
183 #endif // ENABLE(DFG_JIT)
185 #endif // DFGByteCodeCache_h