It should be possible to jettison JIT stub routines even if they are currently running
[WebKit-https.git] / Source / JavaScriptCore / bytecode / Instruction.h
1 /*
2  * Copyright (C) 2008 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  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #ifndef Instruction_h
30 #define Instruction_h
31
32 #include "JITStubRoutine.h"
33 #include "MacroAssembler.h"
34 #include "Opcode.h"
35 #include "PropertySlot.h"
36 #include "Structure.h"
37 #include "StructureChain.h"
38 #include <wtf/VectorTraits.h>
39
40 #define POLYMORPHIC_LIST_CACHE_SIZE 8
41
42 namespace JSC {
43
44     // *Sigh*, If the JIT is enabled we need to track the stubRountine (of type CodeLocationLabel),
45     // If the JIT is not in use we don't actually need the variable (that said, if the JIT is not in use we don't
46     // curently actually use PolymorphicAccessStructureLists, which we should).  Anyway, this seems like the best
47     // solution for now - will need to something smarter if/when we actually want mixed-mode operation.
48
49     class JSCell;
50     class Structure;
51     class StructureChain;
52     struct LLIntCallLinkInfo;
53     struct ValueProfile;
54
55 #if ENABLE(JIT)
56     // Structure used by op_get_by_id_self_list and op_get_by_id_proto_list instruction to hold data off the main opcode stream.
57     struct PolymorphicAccessStructureList {
58         WTF_MAKE_FAST_ALLOCATED;
59     public:
60         struct PolymorphicStubInfo {
61             bool isChain;
62             bool isDirect;
63             RefPtr<JITStubRoutine> stubRoutine;
64             WriteBarrier<Structure> base;
65             union {
66                 WriteBarrierBase<Structure> proto;
67                 WriteBarrierBase<StructureChain> chain;
68             } u;
69
70             PolymorphicStubInfo()
71             {
72                 u.proto.clear();
73             }
74
75             void set(JSGlobalData& globalData, JSCell* owner, PassRefPtr<JITStubRoutine> _stubRoutine, Structure* _base, bool isDirect)
76             {
77                 stubRoutine = _stubRoutine;
78                 base.set(globalData, owner, _base);
79                 u.proto.clear();
80                 isChain = false;
81                 this->isDirect = isDirect;
82             }
83             
84             void set(JSGlobalData& globalData, JSCell* owner, PassRefPtr<JITStubRoutine> _stubRoutine, Structure* _base, Structure* _proto, bool isDirect)
85             {
86                 stubRoutine = _stubRoutine;
87                 base.set(globalData, owner, _base);
88                 u.proto.set(globalData, owner, _proto);
89                 isChain = false;
90                 this->isDirect = isDirect;
91             }
92             
93             void set(JSGlobalData& globalData, JSCell* owner, PassRefPtr<JITStubRoutine> _stubRoutine, Structure* _base, StructureChain* _chain, bool isDirect)
94             {
95                 stubRoutine = _stubRoutine;
96                 base.set(globalData, owner, _base);
97                 u.chain.set(globalData, owner, _chain);
98                 isChain = true;
99                 this->isDirect = isDirect;
100             }
101         } list[POLYMORPHIC_LIST_CACHE_SIZE];
102         
103         PolymorphicAccessStructureList()
104         {
105         }
106         
107         PolymorphicAccessStructureList(JSGlobalData& globalData, JSCell* owner, PassRefPtr<JITStubRoutine> stubRoutine, Structure* firstBase, bool isDirect)
108         {
109             list[0].set(globalData, owner, stubRoutine, firstBase, isDirect);
110         }
111
112         PolymorphicAccessStructureList(JSGlobalData& globalData, JSCell* owner, PassRefPtr<JITStubRoutine> stubRoutine, Structure* firstBase, Structure* firstProto, bool isDirect)
113         {
114             list[0].set(globalData, owner, stubRoutine, firstBase, firstProto, isDirect);
115         }
116
117         PolymorphicAccessStructureList(JSGlobalData& globalData, JSCell* owner, PassRefPtr<JITStubRoutine> stubRoutine, Structure* firstBase, StructureChain* firstChain, bool isDirect)
118         {
119             list[0].set(globalData, owner, stubRoutine, firstBase, firstChain, isDirect);
120         }
121
122         bool visitWeak(int count)
123         {
124             for (int i = 0; i < count; ++i) {
125                 PolymorphicStubInfo& info = list[i];
126                 if (!info.base) {
127                     // We're being marked during initialisation of an entry
128                     ASSERT(!info.u.proto);
129                     continue;
130                 }
131                 
132                 if (!Heap::isMarked(info.base.get()))
133                     return false;
134                 if (info.u.proto && !info.isChain
135                     && !Heap::isMarked(info.u.proto.get()))
136                     return false;
137                 if (info.u.chain && info.isChain
138                     && !Heap::isMarked(info.u.chain.get()))
139                     return false;
140             }
141             
142             return true;
143         }
144     };
145
146 #endif
147
148     struct Instruction {
149         Instruction()
150         {
151             u.jsCell.clear();
152         }
153         
154         Instruction(Opcode opcode)
155         {
156 #if !ENABLE(COMPUTED_GOTO_CLASSIC_INTERPRETER)
157             // We have to initialize one of the pointer members to ensure that
158             // the entire struct is initialized, when opcode is not a pointer.
159             u.jsCell.clear();
160 #endif
161             u.opcode = opcode;
162         }
163
164         Instruction(int operand)
165         {
166             // We have to initialize one of the pointer members to ensure that
167             // the entire struct is initialized in 64-bit.
168             u.jsCell.clear();
169             u.operand = operand;
170         }
171
172         Instruction(JSGlobalData& globalData, JSCell* owner, Structure* structure)
173         {
174             u.structure.clear();
175             u.structure.set(globalData, owner, structure);
176         }
177         Instruction(JSGlobalData& globalData, JSCell* owner, StructureChain* structureChain)
178         {
179             u.structureChain.clear();
180             u.structureChain.set(globalData, owner, structureChain);
181         }
182         Instruction(JSGlobalData& globalData, JSCell* owner, JSCell* jsCell)
183         {
184             u.jsCell.clear();
185             u.jsCell.set(globalData, owner, jsCell);
186         }
187
188         Instruction(PropertySlot::GetValueFunc getterFunc) { u.getterFunc = getterFunc; }
189         
190         Instruction(LLIntCallLinkInfo* callLinkInfo) { u.callLinkInfo = callLinkInfo; }
191         
192         Instruction(ValueProfile* profile) { u.profile = profile; }
193         
194         Instruction(WriteBarrier<Unknown>* registerPointer) { u.registerPointer = registerPointer; }
195         
196         Instruction(bool* predicatePointer) { u.predicatePointer = predicatePointer; }
197
198         union {
199             Opcode opcode;
200             int operand;
201             WriteBarrierBase<Structure> structure;
202             WriteBarrierBase<StructureChain> structureChain;
203             WriteBarrierBase<JSCell> jsCell;
204             WriteBarrier<Unknown>* registerPointer;
205             PropertySlot::GetValueFunc getterFunc;
206             LLIntCallLinkInfo* callLinkInfo;
207             ValueProfile* profile;
208             void* pointer;
209             bool* predicatePointer;
210         } u;
211         
212     private:
213         Instruction(StructureChain*);
214         Instruction(Structure*);
215     };
216
217 } // namespace JSC
218
219 namespace WTF {
220
221     template<> struct VectorTraits<JSC::Instruction> : VectorTraitsBase<true, JSC::Instruction> { };
222
223 } // namespace WTF
224
225 #endif // Instruction_h