Roll out r108309, r108323, and r108326
[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 "MacroAssembler.h"
33 #include "Opcode.h"
34 #include "PropertySlot.h"
35 #include "Structure.h"
36 #include "StructureChain.h"
37 #include <wtf/VectorTraits.h>
38
39 #define POLYMORPHIC_LIST_CACHE_SIZE 8
40
41 namespace JSC {
42
43     // *Sigh*, If the JIT is enabled we need to track the stubRountine (of type CodeLocationLabel),
44     // 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
45     // curently actually use PolymorphicAccessStructureLists, which we should).  Anyway, this seems like the best
46     // solution for now - will need to something smarter if/when we actually want mixed-mode operation.
47
48     class JSCell;
49     class Structure;
50     class StructureChain;
51     struct ValueProfile;
52
53 #if ENABLE(JIT)
54     typedef MacroAssemblerCodeRef PolymorphicAccessStructureListStubRoutineType;
55
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             PolymorphicAccessStructureListStubRoutineType 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, PolymorphicAccessStructureListStubRoutineType _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, PolymorphicAccessStructureListStubRoutineType _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, PolymorphicAccessStructureListStubRoutineType _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, PolymorphicAccessStructureListStubRoutineType stubRoutine, Structure* firstBase, bool isDirect)
108         {
109             list[0].set(globalData, owner, stubRoutine, firstBase, isDirect);
110         }
111
112         PolymorphicAccessStructureList(JSGlobalData& globalData, JSCell* owner, PolymorphicAccessStructureListStubRoutineType 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, PolymorphicAccessStructureListStubRoutineType 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(Opcode opcode)
150         {
151 #if !ENABLE(COMPUTED_GOTO_CLASSIC_INTERPRETER)
152             // We have to initialize one of the pointer members to ensure that
153             // the entire struct is initialized, when opcode is not a pointer.
154             u.jsCell.clear();
155 #endif
156             u.opcode = opcode;
157         }
158
159         Instruction(int operand)
160         {
161             // We have to initialize one of the pointer members to ensure that
162             // the entire struct is initialized in 64-bit.
163             u.jsCell.clear();
164             u.operand = operand;
165         }
166
167         Instruction(JSGlobalData& globalData, JSCell* owner, Structure* structure)
168         {
169             u.structure.clear();
170             u.structure.set(globalData, owner, structure);
171         }
172         Instruction(JSGlobalData& globalData, JSCell* owner, StructureChain* structureChain)
173         {
174             u.structureChain.clear();
175             u.structureChain.set(globalData, owner, structureChain);
176         }
177         Instruction(JSGlobalData& globalData, JSCell* owner, JSCell* jsCell)
178         {
179             u.jsCell.clear();
180             u.jsCell.set(globalData, owner, jsCell);
181         }
182
183         Instruction(PropertySlot::GetValueFunc getterFunc) { u.getterFunc = getterFunc; }
184         
185         Instruction(ValueProfile* profile) { u.profile = profile; }
186
187         union {
188             Opcode opcode;
189             int operand;
190             WriteBarrierBase<Structure> structure;
191             WriteBarrierBase<StructureChain> structureChain;
192             WriteBarrierBase<JSCell> jsCell;
193             PropertySlot::GetValueFunc getterFunc;
194             ValueProfile* profile;
195         } u;
196         
197     private:
198         Instruction(StructureChain*);
199         Instruction(Structure*);
200     };
201
202 } // namespace JSC
203
204 namespace WTF {
205
206     template<> struct VectorTraits<JSC::Instruction> : VectorTraitsBase<true, JSC::Instruction> { };
207
208 } // namespace WTF
209
210 #endif // Instruction_h