1/*
2 * Copyright (C) 2012-2018 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#pragma once
27
28#include "CallLinkStatus.h"
29#include "ExitFlag.h"
30#include "ICStatusMap.h"
31#include "PutByIdVariant.h"
32#include "StubInfoSummary.h"
33
34namespace JSC {
35
36class CodeBlock;
37class VM;
38class JSGlobalObject;
39class Structure;
40class StructureChain;
41class StructureStubInfo;
42
43typedef HashMap<CodeOrigin, StructureStubInfo*, CodeOriginApproximateHash> StubInfoMap;
44
45class PutByIdStatus {
46 WTF_MAKE_FAST_ALLOCATED;
47public:
48 enum State {
49 // It's uncached so we have no information.
50 NoInformation,
51 // It's cached as a simple store of some kind.
52 Simple,
53 // It's known to often take slow path.
54 TakesSlowPath,
55 // It's known to take paths that make calls.
56 MakesCalls
57 };
58
59 PutByIdStatus()
60 : m_state(NoInformation)
61 {
62 }
63
64 explicit PutByIdStatus(State state)
65 : m_state(state)
66 {
67 ASSERT(m_state == NoInformation || m_state == TakesSlowPath || m_state == MakesCalls);
68 }
69
70 explicit PutByIdStatus(StubInfoSummary summary)
71 {
72 switch (summary) {
73 case StubInfoSummary::NoInformation:
74 m_state = NoInformation;
75 return;
76 case StubInfoSummary::Simple:
77 case StubInfoSummary::MakesCalls:
78 RELEASE_ASSERT_NOT_REACHED();
79 return;
80 case StubInfoSummary::TakesSlowPath:
81 m_state = TakesSlowPath;
82 return;
83 case StubInfoSummary::TakesSlowPathAndMakesCalls:
84 m_state = MakesCalls;
85 return;
86 }
87 RELEASE_ASSERT_NOT_REACHED();
88 }
89
90 PutByIdStatus(const PutByIdVariant& variant)
91 : m_state(Simple)
92 {
93 m_variants.append(variant);
94 }
95
96 static PutByIdStatus computeFor(CodeBlock*, ICStatusMap&, BytecodeIndex, UniquedStringImpl* uid, ExitFlag, CallLinkStatus::ExitSiteData);
97 static PutByIdStatus computeFor(JSGlobalObject*, const StructureSet&, UniquedStringImpl* uid, bool isDirect);
98
99 static PutByIdStatus computeFor(CodeBlock* baselineBlock, ICStatusMap& baselineMap, ICStatusContextStack& contextStack, CodeOrigin, UniquedStringImpl* uid);
100
101#if ENABLE(JIT)
102 static PutByIdStatus computeForStubInfo(const ConcurrentJSLocker&, CodeBlock* baselineBlock, StructureStubInfo*, CodeOrigin, UniquedStringImpl* uid);
103#endif
104
105 State state() const { return m_state; }
106
107 bool isSet() const { return m_state != NoInformation; }
108 bool operator!() const { return m_state == NoInformation; }
109 bool isSimple() const { return m_state == Simple; }
110 bool takesSlowPath() const { return m_state == TakesSlowPath || m_state == MakesCalls; }
111 bool makesCalls() const;
112 PutByIdStatus slowVersion() const;
113
114 size_t numVariants() const { return m_variants.size(); }
115 const Vector<PutByIdVariant, 1>& variants() const { return m_variants; }
116 const PutByIdVariant& at(size_t index) const { return m_variants[index]; }
117 const PutByIdVariant& operator[](size_t index) const { return at(index); }
118
119 void markIfCheap(SlotVisitor&);
120 bool finalize(VM&);
121
122 void merge(const PutByIdStatus&);
123
124 void filter(const StructureSet&);
125
126 void dump(PrintStream&) const;
127
128private:
129#if ENABLE(JIT)
130 static PutByIdStatus computeForStubInfo(
131 const ConcurrentJSLocker&, CodeBlock*, StructureStubInfo*, UniquedStringImpl* uid,
132 CallLinkStatus::ExitSiteData);
133#endif
134 static PutByIdStatus computeFromLLInt(CodeBlock*, BytecodeIndex, UniquedStringImpl* uid);
135
136 bool appendVariant(const PutByIdVariant&);
137
138 State m_state;
139 Vector<PutByIdVariant, 1> m_variants;
140};
141
142} // namespace JSC
143