]> git.saurik.com Git - apple/javascriptcore.git/blob - dfg/DFGDesiredWatchpoints.h
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / dfg / DFGDesiredWatchpoints.h
1 /*
2 * Copyright (C) 2013-2015 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26 #ifndef DFGDesiredWatchpoints_h
27 #define DFGDesiredWatchpoints_h
28
29 #if ENABLE(DFG_JIT)
30
31 #include "CodeOrigin.h"
32 #include "DFGCommonData.h"
33 #include "InferredValue.h"
34 #include "JSArrayBufferView.h"
35 #include "Watchpoint.h"
36 #include <wtf/HashMap.h>
37 #include <wtf/HashSet.h>
38 #include <wtf/Noncopyable.h>
39 #include <wtf/Vector.h>
40
41 namespace JSC { namespace DFG {
42
43 class Graph;
44
45 template<typename T>
46 struct GenericSetAdaptor {
47 static void add(CodeBlock*, T* set, Watchpoint* watchpoint)
48 {
49 return set->add(watchpoint);
50 }
51 static bool hasBeenInvalidated(T* set) { return set->hasBeenInvalidated(); }
52 };
53
54 struct InferredValueAdaptor {
55 static void add(CodeBlock*, InferredValue*, Watchpoint*);
56 static bool hasBeenInvalidated(InferredValue* inferredValue)
57 {
58 return inferredValue->hasBeenInvalidated();
59 }
60 };
61
62 struct ArrayBufferViewWatchpointAdaptor {
63 static void add(CodeBlock*, JSArrayBufferView*, Watchpoint*);
64 static bool hasBeenInvalidated(JSArrayBufferView* view)
65 {
66 bool result = !view->length();
67 WTF::loadLoadFence();
68 return result;
69 }
70 };
71
72 template<typename WatchpointSetType, typename Adaptor = GenericSetAdaptor<WatchpointSetType>>
73 class GenericDesiredWatchpoints {
74 #if !ASSERT_DISABLED
75 typedef HashMap<WatchpointSetType*, bool> StateMap;
76 #endif
77 public:
78 GenericDesiredWatchpoints()
79 : m_reallyAdded(false)
80 {
81 }
82
83 void addLazily(WatchpointSetType* set)
84 {
85 m_sets.add(set);
86 }
87
88 void reallyAdd(CodeBlock* codeBlock, CommonData& common)
89 {
90 RELEASE_ASSERT(!m_reallyAdded);
91
92 typename HashSet<WatchpointSetType*>::iterator iter = m_sets.begin();
93 typename HashSet<WatchpointSetType*>::iterator end = m_sets.end();
94 for (; iter != end; ++iter) {
95 common.watchpoints.append(CodeBlockJettisoningWatchpoint(codeBlock));
96 Adaptor::add(codeBlock, *iter, &common.watchpoints.last());
97 }
98
99 m_reallyAdded = true;
100 }
101
102 bool areStillValid() const
103 {
104 typename HashSet<WatchpointSetType*>::iterator iter = m_sets.begin();
105 typename HashSet<WatchpointSetType*>::iterator end = m_sets.end();
106 for (; iter != end; ++iter) {
107 if (Adaptor::hasBeenInvalidated(*iter))
108 return false;
109 }
110
111 return true;
112 }
113
114 bool isWatched(WatchpointSetType* set) const
115 {
116 return m_sets.contains(set);
117 }
118
119 private:
120 HashSet<WatchpointSetType*> m_sets;
121 bool m_reallyAdded;
122 };
123
124 class DesiredWatchpoints {
125 public:
126 DesiredWatchpoints();
127 ~DesiredWatchpoints();
128
129 void addLazily(WatchpointSet*);
130 void addLazily(InlineWatchpointSet&);
131 void addLazily(InferredValue*);
132 void addLazily(JSArrayBufferView*);
133
134 bool consider(Structure*);
135
136 void reallyAdd(CodeBlock*, CommonData&);
137
138 bool areStillValid() const;
139
140 bool isWatched(WatchpointSet* set)
141 {
142 return m_sets.isWatched(set);
143 }
144 bool isWatched(InlineWatchpointSet& set)
145 {
146 return m_inlineSets.isWatched(&set);
147 }
148 bool isWatched(InferredValue* inferredValue)
149 {
150 return m_inferredValues.isWatched(inferredValue);
151 }
152 bool isWatched(JSArrayBufferView* view)
153 {
154 return m_bufferViews.isWatched(view);
155 }
156
157 private:
158 GenericDesiredWatchpoints<WatchpointSet> m_sets;
159 GenericDesiredWatchpoints<InlineWatchpointSet> m_inlineSets;
160 GenericDesiredWatchpoints<InferredValue, InferredValueAdaptor> m_inferredValues;
161 GenericDesiredWatchpoints<JSArrayBufferView, ArrayBufferViewWatchpointAdaptor> m_bufferViews;
162 };
163
164 } } // namespace JSC::DFG
165
166 #endif // ENABLE(DFG_JIT)
167
168 #endif // DFGDesiredWatchpoints_h
169