GetById list caching should use something object-oriented rather than PolymorphicAcce...
[WebKit-https.git] / Source / JavaScriptCore / bytecode / PolymorphicGetByIdList.cpp
1 /*
2  * Copyright (C) 2014 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 #include "config.h"
27 #include "PolymorphicGetByIdList.h"
28
29 #if ENABLE(JIT)
30
31 #include "CodeBlock.h"
32 #include "Heap.h"
33 #include "JSCInlines.h"
34 #include "StructureStubInfo.h"
35
36 namespace JSC {
37
38 GetByIdAccess::GetByIdAccess(
39     VM& vm, JSCell* owner, AccessType type, PassRefPtr<JITStubRoutine> stubRoutine,
40     Structure* structure, StructureChain* chain, unsigned chainCount)
41     : m_type(type)
42     , m_chainCount(chainCount)
43     , m_structure(vm, owner, structure)
44     , m_stubRoutine(stubRoutine)
45 {
46     if (chain)
47         m_chain.set(vm, owner, chain);
48 }
49
50 GetByIdAccess::~GetByIdAccess()
51 {
52 }
53
54 GetByIdAccess GetByIdAccess::fromStructureStubInfo(StructureStubInfo& stubInfo)
55 {
56     MacroAssemblerCodePtr initialSlowPath =
57         stubInfo.callReturnLocation.labelAtOffset(stubInfo.patch.deltaCallToSlowCase);
58     
59     GetByIdAccess result;
60     
61     switch (stubInfo.accessType) {
62     case access_get_by_id_self:
63         result.m_type = SimpleInline;
64         result.m_structure.copyFrom(stubInfo.u.getByIdSelf.baseObjectStructure);
65         result.m_stubRoutine = JITStubRoutine::createSelfManagedRoutine(initialSlowPath);
66         break;
67         
68     case access_get_by_id_chain:
69         result.m_structure.copyFrom(stubInfo.u.getByIdChain.baseObjectStructure);
70         result.m_chain.copyFrom(stubInfo.u.getByIdChain.chain);
71         result.m_chainCount = stubInfo.u.getByIdChain.count;
72         result.m_stubRoutine = stubInfo.stubRoutine;
73         if (stubInfo.u.getByIdChain.isDirect)
74             result.m_type = SimpleStub;
75         else
76             result.m_type = Getter;
77         break;
78         
79     default:
80         RELEASE_ASSERT_NOT_REACHED();
81     }
82     
83     return result;
84 }
85
86 bool GetByIdAccess::visitWeak() const
87 {
88     if (m_structure && !Heap::isMarked(m_structure.get()))
89         return false;
90     if (m_chain && !Heap::isMarked(m_chain.get()))
91         return false;
92     return true;
93 }
94
95 PolymorphicGetByIdList::PolymorphicGetByIdList(StructureStubInfo& stubInfo)
96 {
97     if (stubInfo.accessType == access_unset)
98         return;
99     
100     m_list.append(GetByIdAccess::fromStructureStubInfo(stubInfo));
101 }
102
103 PolymorphicGetByIdList* PolymorphicGetByIdList::from(StructureStubInfo& stubInfo)
104 {
105     if (stubInfo.accessType == access_get_by_id_list)
106         return stubInfo.u.getByIdList.list;
107     
108     ASSERT(
109         stubInfo.accessType == access_get_by_id_self
110         || stubInfo.accessType == access_get_by_id_chain
111         || stubInfo.accessType == access_unset);
112     
113     PolymorphicGetByIdList* result = new PolymorphicGetByIdList(stubInfo);
114     
115     stubInfo.initGetByIdList(result);
116     
117     return result;
118 }
119
120 PolymorphicGetByIdList::~PolymorphicGetByIdList() { }
121
122 MacroAssemblerCodePtr PolymorphicGetByIdList::currentSlowPathTarget(
123     StructureStubInfo& stubInfo) const
124 {
125     if (isEmpty())
126         return stubInfo.callReturnLocation.labelAtOffset(stubInfo.patch.deltaCallToSlowCase);
127     return m_list.last().stubRoutine()->code().code();
128 }
129
130 void PolymorphicGetByIdList::addAccess(const GetByIdAccess& access)
131 {
132     ASSERT(!isFull());
133     // Make sure that the resizing optimizes for space, not time.
134     m_list.resize(m_list.size() + 1);
135     m_list.last() = access;
136 }
137
138 bool PolymorphicGetByIdList::isFull() const
139 {
140     ASSERT(size() <= POLYMORPHIC_LIST_CACHE_SIZE);
141     return size() == POLYMORPHIC_LIST_CACHE_SIZE;
142 }
143
144 bool PolymorphicGetByIdList::isAlmostFull() const
145 {
146     ASSERT(size() <= POLYMORPHIC_LIST_CACHE_SIZE);
147     return size() >= POLYMORPHIC_LIST_CACHE_SIZE - 1;
148 }
149
150 bool PolymorphicGetByIdList::didSelfPatching() const
151 {
152     for (unsigned i = size(); i--;) {
153         if (at(i).type() == GetByIdAccess::SimpleInline)
154             return true;
155     }
156     return false;
157 }
158
159 bool PolymorphicGetByIdList::visitWeak() const
160 {
161     for (unsigned i = size(); i--;) {
162         if (!at(i).visitWeak())
163             return false;
164     }
165     return true;
166 }
167
168 } // namespace JSC
169
170 #endif // ENABLE(JIT)
171
172