DFGDesiredWatchpoints.h 7.54 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * Copyright (C) 2013 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

#ifndef DFGDesiredWatchpoints_h
#define DFGDesiredWatchpoints_h

#include <wtf/Platform.h>

#if ENABLE(DFG_JIT)

33 34
#include "CodeOrigin.h"
#include "DFGCommonData.h"
35
#include "JSArrayBufferView.h"
36
#include "Watchpoint.h"
37
#include <wtf/HashMap.h>
38
#include <wtf/HashSet.h>
39 40 41 42 43 44 45 46
#include <wtf/Noncopyable.h>
#include <wtf/Vector.h>

namespace JSC { namespace DFG {

template<typename WatchpointSetType>
struct WatchpointForGenericWatchpointSet {
    WatchpointForGenericWatchpointSet()
47
        : m_exitKind(ExitKindUnset)
48 49 50 51
        , m_set(0)
    {
    }
    
52 53 54 55
    WatchpointForGenericWatchpointSet(
        CodeOrigin codeOrigin, ExitKind exitKind, WatchpointSetType* set)
        : m_codeOrigin(codeOrigin)
        , m_exitKind(exitKind)
56 57 58 59
        , m_set(set)
    {
    }
    
60 61
    CodeOrigin m_codeOrigin;
    ExitKind m_exitKind;
62 63 64
    WatchpointSetType* m_set;
};

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
template<typename T>
struct GenericSetAdaptor {
    static void add(CodeBlock*, T* set, Watchpoint* watchpoint)
    {
        return set->add(watchpoint);
    }
    static bool hasBeenInvalidated(T* set) { return set->hasBeenInvalidated(); }
};

struct ArrayBufferViewWatchpointAdaptor {
    static void add(CodeBlock*, JSArrayBufferView*, Watchpoint*);
    static bool hasBeenInvalidated(JSArrayBufferView* view)
    {
        bool result = !view->length();
        WTF::loadLoadFence();
        return result;
    }
};

template<typename WatchpointSetType, typename Adaptor = GenericSetAdaptor<WatchpointSetType>>
85 86
class GenericDesiredWatchpoints {
    WTF_MAKE_NONCOPYABLE(GenericDesiredWatchpoints);
87 88 89
#if !ASSERT_DISABLED
    typedef HashMap<WatchpointSetType*, bool> StateMap;
#endif
90 91 92 93 94 95
public:
    GenericDesiredWatchpoints()
        : m_reallyAdded(false)
    {
    }
    
96 97 98 99 100 101
    void addLazily(WatchpointSetType* set)
    {
        m_sets.add(set);
    }
    
    void addLazily(CodeOrigin codeOrigin, ExitKind exitKind, WatchpointSetType* set)
102
    {
103 104
        m_profiledWatchpoints.append(
            WatchpointForGenericWatchpointSet<WatchpointSetType>(codeOrigin, exitKind, set));
105 106
    }
    
107
    void reallyAdd(CodeBlock* codeBlock, CommonData& common)
108 109
    {
        RELEASE_ASSERT(!m_reallyAdded);
110 111 112 113 114
        
        typename HashSet<WatchpointSetType*>::iterator iter = m_sets.begin();
        typename HashSet<WatchpointSetType*>::iterator end = m_sets.end();
        for (; iter != end; ++iter) {
            common.watchpoints.append(CodeBlockJettisoningWatchpoint(codeBlock));
115
            Adaptor::add(codeBlock, *iter, &common.watchpoints.last());
116 117 118 119 120 121 122
        }
        
        for (unsigned i = m_profiledWatchpoints.size(); i--;) {
            WatchpointForGenericWatchpointSet<WatchpointSetType> watchpoint =
                m_profiledWatchpoints[i];
            common.profiledWatchpoints.append(
                ProfiledCodeBlockJettisoningWatchpoint(watchpoint.m_codeOrigin, watchpoint.m_exitKind, codeBlock));
123
            Adaptor::add(codeBlock, watchpoint.m_set, &common.profiledWatchpoints.last());
124 125
        }
        
126 127 128 129 130
        m_reallyAdded = true;
    }
    
    bool areStillValid() const
    {
131 132 133
        typename HashSet<WatchpointSetType*>::iterator iter = m_sets.begin();
        typename HashSet<WatchpointSetType*>::iterator end = m_sets.end();
        for (; iter != end; ++iter) {
134
            if (Adaptor::hasBeenInvalidated(*iter))
135 136 137 138
                return false;
        }
        
        for (unsigned i = m_profiledWatchpoints.size(); i--;) {
139
            if (Adaptor::hasBeenInvalidated(m_profiledWatchpoints[i].m_set))
140 141
                return false;
        }
142
        
143 144
        return true;
    }
145 146 147 148
    
#if ASSERT_DISABLED
    bool isStillValid(WatchpointSetType* set)
    {
149
        return !Adaptor::hasBeenInvalidated(set);
150 151 152 153 154 155 156 157 158
    }
    
    bool shouldAssumeMixedState(WatchpointSetType*)
    {
        return true;
    }
#else
    bool isStillValid(WatchpointSetType* set)
    {
159
        bool result = !Adaptor::hasBeenInvalidated(set);
160 161 162 163 164 165 166 167 168 169
        m_firstKnownState.add(set, result);
        return result;
    }
    
    bool shouldAssumeMixedState(WatchpointSetType* set)
    {
        typename StateMap::iterator iter = m_firstKnownState.find(set);
        if (iter == m_firstKnownState.end())
            return false;
        
170
        return iter->value != !Adaptor::hasBeenInvalidated(set);
171 172 173 174 175 176 177
    }
#endif
    
    bool isValidOrMixed(WatchpointSetType* set)
    {
        return isStillValid(set) || shouldAssumeMixedState(set);
    }
178 179

private:
180 181
    Vector<WatchpointForGenericWatchpointSet<WatchpointSetType>> m_profiledWatchpoints;
    HashSet<WatchpointSetType*> m_sets;
182 183 184
#if !ASSERT_DISABLED
    StateMap m_firstKnownState;
#endif
185 186 187 188 189 190 191 192
    bool m_reallyAdded;
};

class DesiredWatchpoints {
public:
    DesiredWatchpoints();
    ~DesiredWatchpoints();
    
193 194
    void addLazily(WatchpointSet*);
    void addLazily(InlineWatchpointSet&);
195
    void addLazily(JSArrayBufferView*);
196 197
    void addLazily(CodeOrigin, ExitKind, WatchpointSet*);
    void addLazily(CodeOrigin, ExitKind, InlineWatchpointSet&);
198
    
199
    void reallyAdd(CodeBlock*, CommonData&);
200 201 202
    
    bool areStillValid() const;
    
203 204 205 206 207 208 209 210
    bool isStillValid(WatchpointSet* set)
    {
        return m_sets.isStillValid(set);
    }
    bool isStillValid(InlineWatchpointSet& set)
    {
        return m_inlineSets.isStillValid(&set);
    }
211 212 213 214
    bool isStillValid(JSArrayBufferView* view)
    {
        return m_bufferViews.isStillValid(view);
    }
215 216 217 218 219 220 221 222
    bool shouldAssumeMixedState(WatchpointSet* set)
    {
        return m_sets.shouldAssumeMixedState(set);
    }
    bool shouldAssumeMixedState(InlineWatchpointSet& set)
    {
        return m_inlineSets.shouldAssumeMixedState(&set);
    }
223 224 225 226
    bool shouldAssumeMixedState(JSArrayBufferView* view)
    {
        return m_bufferViews.shouldAssumeMixedState(view);
    }
227 228 229 230 231 232 233 234
    bool isValidOrMixed(WatchpointSet* set)
    {
        return m_sets.isValidOrMixed(set);
    }
    bool isValidOrMixed(InlineWatchpointSet& set)
    {
        return m_inlineSets.isValidOrMixed(&set);
    }
235 236 237 238
    bool isValidOrMixed(JSArrayBufferView* view)
    {
        return m_bufferViews.isValidOrMixed(view);
    }
239
    
240 241 242
private:
    GenericDesiredWatchpoints<WatchpointSet> m_sets;
    GenericDesiredWatchpoints<InlineWatchpointSet> m_inlineSets;
243
    GenericDesiredWatchpoints<JSArrayBufferView, ArrayBufferViewWatchpointAdaptor> m_bufferViews;
244 245 246 247 248 249 250 251
};

} } // namespace JSC::DFG

#endif // ENABLE(DFG_JIT)

#endif // DFGDesiredWatchpoints_h