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 "CodeOrigin.h"
30#include "ConcurrentJSLock.h"
31#include "ExitFlag.h"
32#include "GetByIdVariant.h"
33#include "ICStatusMap.h"
34#include "ScopeOffset.h"
35#include "StubInfoSummary.h"
36
37namespace JSC {
38
39class AccessCase;
40class CodeBlock;
41class JSModuleEnvironment;
42class JSModuleNamespaceObject;
43class ModuleNamespaceAccessCase;
44class StructureStubInfo;
45
46class GetByIdStatus {
47public:
48 enum State : uint8_t {
49 // It's uncached so we have no information.
50 NoInformation,
51 // It's cached for a simple access to a known object property with
52 // a possible structure chain and a possible specific value.
53 Simple,
54 // It's cached for a custom accessor with a possible structure chain.
55 Custom,
56 // It's cached for an access to a module namespace object's binding.
57 ModuleNamespace,
58 // It's known to often take slow path.
59 TakesSlowPath,
60 // It's known to take paths that make calls.
61 MakesCalls,
62 };
63
64 GetByIdStatus()
65 : m_state(NoInformation)
66 {
67 }
68
69 explicit GetByIdStatus(State state)
70 : m_state(state)
71 {
72 ASSERT(state == NoInformation || state == TakesSlowPath || state == MakesCalls);
73 }
74
75 explicit GetByIdStatus(StubInfoSummary summary)
76 : m_wasSeenInJIT(true)
77 {
78 switch (summary) {
79 case StubInfoSummary::NoInformation:
80 m_state = NoInformation;
81 return;
82 case StubInfoSummary::Simple:
83 case StubInfoSummary::MakesCalls:
84 RELEASE_ASSERT_NOT_REACHED();
85 return;
86 case StubInfoSummary::TakesSlowPath:
87 m_state = TakesSlowPath;
88 return;
89 case StubInfoSummary::TakesSlowPathAndMakesCalls:
90 m_state = MakesCalls;
91 return;
92 }
93 RELEASE_ASSERT_NOT_REACHED();
94 }
95
96 GetByIdStatus(
97 State state, bool wasSeenInJIT, const GetByIdVariant& variant = GetByIdVariant())
98 : m_state(state)
99 , m_wasSeenInJIT(wasSeenInJIT)
100 {
101 ASSERT((state == Simple || state == Custom) == variant.isSet());
102 m_variants.append(variant);
103 }
104
105 static GetByIdStatus computeFor(CodeBlock*, ICStatusMap&, unsigned bytecodeIndex, UniquedStringImpl* uid, ExitFlag, CallLinkStatus::ExitSiteData);
106 static GetByIdStatus computeFor(const StructureSet&, UniquedStringImpl* uid);
107
108 static GetByIdStatus computeFor(CodeBlock* baselineBlock, ICStatusMap& baselineMap, ICStatusContextStack& dfgContextStack, CodeOrigin, UniquedStringImpl* uid);
109
110#if ENABLE(DFG_JIT)
111 static GetByIdStatus computeForStubInfo(const ConcurrentJSLocker&, CodeBlock* baselineBlock, StructureStubInfo*, CodeOrigin, UniquedStringImpl* uid);
112#endif
113
114 State state() const { return m_state; }
115
116 bool isSet() const { return m_state != NoInformation; }
117 explicit operator bool() const { return isSet(); }
118 bool isSimple() const { return m_state == Simple; }
119 bool isCustom() const { return m_state == Custom; }
120 bool isModuleNamespace() const { return m_state == ModuleNamespace; }
121
122 size_t numVariants() const { return m_variants.size(); }
123 const Vector<GetByIdVariant, 1>& variants() const { return m_variants; }
124 const GetByIdVariant& at(size_t index) const { return m_variants[index]; }
125 const GetByIdVariant& operator[](size_t index) const { return at(index); }
126
127 bool takesSlowPath() const { return m_state == TakesSlowPath || m_state == MakesCalls || m_state == Custom || m_state == ModuleNamespace; }
128 bool makesCalls() const;
129
130 GetByIdStatus slowVersion() const;
131
132 bool wasSeenInJIT() const { return m_wasSeenInJIT; }
133
134 void merge(const GetByIdStatus&);
135
136 // Attempts to reduce the set of variants to fit the given structure set. This may be approximate.
137 void filter(const StructureSet&);
138
139 JSModuleNamespaceObject* moduleNamespaceObject() const { return m_moduleNamespaceObject; }
140 JSModuleEnvironment* moduleEnvironment() const { return m_moduleEnvironment; }
141 ScopeOffset scopeOffset() const { return m_scopeOffset; }
142
143 void markIfCheap(SlotVisitor&);
144 bool finalize(VM&); // Return true if this gets to live.
145
146 void dump(PrintStream&) const;
147
148private:
149#if ENABLE(JIT)
150 GetByIdStatus(const ModuleNamespaceAccessCase&);
151 static GetByIdStatus computeForStubInfoWithoutExitSiteFeedback(
152 const ConcurrentJSLocker&, CodeBlock* profiledBlock, StructureStubInfo*,
153 UniquedStringImpl* uid, CallLinkStatus::ExitSiteData);
154#endif
155 static GetByIdStatus computeFromLLInt(CodeBlock*, unsigned bytecodeIndex, UniquedStringImpl* uid);
156
157 bool appendVariant(const GetByIdVariant&);
158
159
160 Vector<GetByIdVariant, 1> m_variants;
161 JSModuleNamespaceObject* m_moduleNamespaceObject { nullptr };
162 JSModuleEnvironment* m_moduleEnvironment { nullptr };
163 ScopeOffset m_scopeOffset { };
164 State m_state;
165 bool m_wasSeenInJIT { false };
166};
167
168} // namespace JSC
169