Make it cheap to #include "JITOperations.h"
[WebKit-https.git] / Source / JavaScriptCore / runtime / CommonSlowPaths.h
1 /*
2  * Copyright (C) 2011-2013, 2015 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 #ifndef CommonSlowPaths_h
27 #define CommonSlowPaths_h
28
29 #include "CodeBlock.h"
30 #include "CodeSpecializationKind.h"
31 #include "ExceptionHelpers.h"
32 #include "JSStackInlines.h"
33 #include "SlowPathReturnType.h"
34 #include "StackAlignment.h"
35 #include "Symbol.h"
36 #include "VM.h"
37 #include <wtf/StdLibExtras.h>
38
39 namespace JSC {
40
41 // The purpose of this namespace is to include slow paths that are shared
42 // between the interpreter and baseline JIT. They are written to be agnostic
43 // with respect to the slow-path calling convention, but they do rely on the
44 // JS code being executed more-or-less directly from bytecode (so the call
45 // frame layout is unmodified, making it potentially awkward to use these
46 // from any optimizing JIT, like the DFG).
47
48 namespace CommonSlowPaths {
49
50 struct ArityCheckData {
51     unsigned paddedStackSpace;
52     void* thunkToCall;
53 };
54
55 ALWAYS_INLINE int arityCheckFor(ExecState* exec, JSStack* stack, CodeSpecializationKind kind)
56 {
57     JSFunction* callee = jsCast<JSFunction*>(exec->callee());
58     ASSERT(!callee->isHostFunction());
59     CodeBlock* newCodeBlock = callee->jsExecutable()->codeBlockFor(kind);
60     int argumentCountIncludingThis = exec->argumentCountIncludingThis();
61     
62     ASSERT(argumentCountIncludingThis < newCodeBlock->numParameters());
63     int frameSize = argumentCountIncludingThis + JSStack::CallFrameHeaderSize;
64     int alignedFrameSizeForParameters = WTF::roundUpToMultipleOf(stackAlignmentRegisters(),
65         newCodeBlock->numParameters() + JSStack::CallFrameHeaderSize);
66     int paddedStackSpace = alignedFrameSizeForParameters - frameSize;
67
68     if (!stack->ensureCapacityFor(exec->registers() - paddedStackSpace % stackAlignmentRegisters()))
69         return -1;
70     return paddedStackSpace;
71 }
72
73 inline bool opIn(ExecState* exec, JSValue propName, JSValue baseVal)
74 {
75     if (!baseVal.isObject()) {
76         exec->vm().throwException(exec, createInvalidInParameterError(exec, baseVal));
77         return false;
78     }
79
80     JSObject* baseObj = asObject(baseVal);
81
82     uint32_t i;
83     if (propName.getUInt32(i))
84         return baseObj->hasProperty(exec, i);
85
86     auto property = propName.toPropertyKey(exec);
87     if (exec->vm().exception())
88         return false;
89     return baseObj->hasProperty(exec, property);
90 }
91
92 inline void tryCachePutToScopeGlobal(
93     ExecState* exec, CodeBlock* codeBlock, Instruction* pc, JSObject* scope,
94     GetPutInfo getPutInfo, PutPropertySlot& slot, const Identifier& ident)
95 {
96     // Covers implicit globals. Since they don't exist until they first execute, we didn't know how to cache them at compile time.
97     ResolveType resolveType = getPutInfo.resolveType();
98     if (resolveType != GlobalProperty && resolveType != GlobalPropertyWithVarInjectionChecks 
99         && resolveType != UnresolvedProperty && resolveType != UnresolvedPropertyWithVarInjectionChecks)
100         return;
101
102     if (resolveType == UnresolvedProperty || resolveType == UnresolvedPropertyWithVarInjectionChecks) {
103         if (JSGlobalLexicalEnvironment* globalLexicalEnvironment = jsDynamicCast<JSGlobalLexicalEnvironment*>(scope)) {
104             ResolveType newResolveType = resolveType == UnresolvedProperty ? GlobalLexicalVar : GlobalLexicalVarWithVarInjectionChecks;
105             pc[4].u.operand = GetPutInfo(getPutInfo.resolveMode(), newResolveType, getPutInfo.initializationMode()).operand();
106             SymbolTableEntry entry = globalLexicalEnvironment->symbolTable()->get(ident.impl());
107             ASSERT(!entry.isNull());
108             pc[5].u.watchpointSet = entry.watchpointSet();
109             pc[6].u.pointer = static_cast<void*>(globalLexicalEnvironment->variableAt(entry.scopeOffset()).slot());
110         } else if (jsDynamicCast<JSGlobalObject*>(scope)) {
111             ResolveType newResolveType = resolveType == UnresolvedProperty ? GlobalProperty : GlobalPropertyWithVarInjectionChecks;
112             resolveType = newResolveType;
113             getPutInfo = GetPutInfo(getPutInfo.resolveMode(), newResolveType, getPutInfo.initializationMode());
114             pc[4].u.operand = getPutInfo.operand();
115         }
116     }
117     
118     if (resolveType == GlobalProperty || resolveType == GlobalPropertyWithVarInjectionChecks) {
119         if (!slot.isCacheablePut()
120             || slot.base() != scope
121             || !scope->structure()->propertyAccessesAreCacheable())
122             return;
123         
124         if (slot.type() == PutPropertySlot::NewProperty) {
125             // Don't cache if we've done a transition. We want to detect the first replace so that we
126             // can invalidate the watchpoint.
127             return;
128         }
129         
130         scope->structure()->didCachePropertyReplacement(exec->vm(), slot.cachedOffset());
131
132         ConcurrentJITLocker locker(codeBlock->m_lock);
133         pc[5].u.structure.set(exec->vm(), codeBlock, scope->structure());
134         pc[6].u.operand = slot.cachedOffset();
135     }
136 }
137
138 inline void tryCacheGetFromScopeGlobal(
139     ExecState* exec, VM& vm, Instruction* pc, JSObject* scope, PropertySlot& slot, const Identifier& ident)
140 {
141     GetPutInfo getPutInfo(pc[4].u.operand);
142     ResolveType resolveType = getPutInfo.resolveType();
143
144     if (resolveType == UnresolvedProperty || resolveType == UnresolvedPropertyWithVarInjectionChecks) {
145         if (JSGlobalLexicalEnvironment* globalLexicalEnvironment = jsDynamicCast<JSGlobalLexicalEnvironment*>(scope)) {
146             ResolveType newResolveType = resolveType == UnresolvedProperty ? GlobalLexicalVar : GlobalLexicalVarWithVarInjectionChecks;
147             pc[4].u.operand = GetPutInfo(getPutInfo.resolveMode(), newResolveType, getPutInfo.initializationMode()).operand();
148             SymbolTableEntry entry = globalLexicalEnvironment->symbolTable()->get(ident.impl());
149             ASSERT(!entry.isNull());
150             pc[5].u.watchpointSet = entry.watchpointSet();
151             pc[6].u.pointer = static_cast<void*>(globalLexicalEnvironment->variableAt(entry.scopeOffset()).slot());
152         } else if (jsDynamicCast<JSGlobalObject*>(scope)) {
153             ResolveType newResolveType = resolveType == UnresolvedProperty ? GlobalProperty : GlobalPropertyWithVarInjectionChecks;
154             resolveType = newResolveType; // Allow below caching mechanism to kick in.
155             pc[4].u.operand = GetPutInfo(getPutInfo.resolveMode(), newResolveType, getPutInfo.initializationMode()).operand();
156         }
157     }
158
159     // Covers implicit globals. Since they don't exist until they first execute, we didn't know how to cache them at compile time.
160     if (slot.isCacheableValue() && slot.slotBase() == scope && scope->structure()->propertyAccessesAreCacheable()) {
161         if (resolveType == GlobalProperty || resolveType == GlobalPropertyWithVarInjectionChecks) {
162             CodeBlock* codeBlock = exec->codeBlock();
163             Structure* structure = scope->structure(vm);
164             {
165                 ConcurrentJITLocker locker(codeBlock->m_lock);
166                 pc[5].u.structure.set(exec->vm(), codeBlock, structure);
167                 pc[6].u.operand = slot.cachedOffset();
168             }
169             structure->startWatchingPropertyForReplacements(vm, slot.cachedOffset());
170         }
171     }
172 }
173
174 } // namespace CommonSlowPaths
175
176 class ExecState;
177 struct Instruction;
178
179 #define SLOW_PATH
180     
181 #define SLOW_PATH_DECL(name) \
182 extern "C" SlowPathReturnType SLOW_PATH name(ExecState* exec, Instruction* pc)
183     
184 #define SLOW_PATH_HIDDEN_DECL(name) \
185 SLOW_PATH_DECL(name) WTF_INTERNAL
186     
187 SLOW_PATH_HIDDEN_DECL(slow_path_call_arityCheck);
188 SLOW_PATH_HIDDEN_DECL(slow_path_construct_arityCheck);
189 SLOW_PATH_HIDDEN_DECL(slow_path_create_direct_arguments);
190 SLOW_PATH_HIDDEN_DECL(slow_path_create_scoped_arguments);
191 SLOW_PATH_HIDDEN_DECL(slow_path_create_out_of_band_arguments);
192 SLOW_PATH_HIDDEN_DECL(slow_path_create_this);
193 SLOW_PATH_HIDDEN_DECL(slow_path_enter);
194 SLOW_PATH_HIDDEN_DECL(slow_path_get_callee);
195 SLOW_PATH_HIDDEN_DECL(slow_path_to_this);
196 SLOW_PATH_HIDDEN_DECL(slow_path_throw_tdz_error);
197 SLOW_PATH_HIDDEN_DECL(slow_path_throw_strict_mode_readonly_property_write_error);
198 SLOW_PATH_HIDDEN_DECL(slow_path_not);
199 SLOW_PATH_HIDDEN_DECL(slow_path_eq);
200 SLOW_PATH_HIDDEN_DECL(slow_path_neq);
201 SLOW_PATH_HIDDEN_DECL(slow_path_stricteq);
202 SLOW_PATH_HIDDEN_DECL(slow_path_nstricteq);
203 SLOW_PATH_HIDDEN_DECL(slow_path_less);
204 SLOW_PATH_HIDDEN_DECL(slow_path_lesseq);
205 SLOW_PATH_HIDDEN_DECL(slow_path_greater);
206 SLOW_PATH_HIDDEN_DECL(slow_path_greatereq);
207 SLOW_PATH_HIDDEN_DECL(slow_path_inc);
208 SLOW_PATH_HIDDEN_DECL(slow_path_dec);
209 SLOW_PATH_HIDDEN_DECL(slow_path_to_number);
210 SLOW_PATH_HIDDEN_DECL(slow_path_to_string);
211 SLOW_PATH_HIDDEN_DECL(slow_path_negate);
212 SLOW_PATH_HIDDEN_DECL(slow_path_add);
213 SLOW_PATH_HIDDEN_DECL(slow_path_mul);
214 SLOW_PATH_HIDDEN_DECL(slow_path_sub);
215 SLOW_PATH_HIDDEN_DECL(slow_path_div);
216 SLOW_PATH_HIDDEN_DECL(slow_path_mod);
217 SLOW_PATH_HIDDEN_DECL(slow_path_lshift);
218 SLOW_PATH_HIDDEN_DECL(slow_path_rshift);
219 SLOW_PATH_HIDDEN_DECL(slow_path_urshift);
220 SLOW_PATH_HIDDEN_DECL(slow_path_unsigned);
221 SLOW_PATH_HIDDEN_DECL(slow_path_bitand);
222 SLOW_PATH_HIDDEN_DECL(slow_path_bitor);
223 SLOW_PATH_HIDDEN_DECL(slow_path_bitxor);
224 SLOW_PATH_HIDDEN_DECL(slow_path_typeof);
225 SLOW_PATH_HIDDEN_DECL(slow_path_is_object);
226 SLOW_PATH_HIDDEN_DECL(slow_path_is_object_or_null);
227 SLOW_PATH_HIDDEN_DECL(slow_path_is_function);
228 SLOW_PATH_HIDDEN_DECL(slow_path_in);
229 SLOW_PATH_HIDDEN_DECL(slow_path_del_by_val);
230 SLOW_PATH_HIDDEN_DECL(slow_path_strcat);
231 SLOW_PATH_HIDDEN_DECL(slow_path_to_primitive);
232 SLOW_PATH_HIDDEN_DECL(slow_path_get_enumerable_length);
233 SLOW_PATH_HIDDEN_DECL(slow_path_has_generic_property);
234 SLOW_PATH_HIDDEN_DECL(slow_path_has_structure_property);
235 SLOW_PATH_HIDDEN_DECL(slow_path_has_indexed_property);
236 SLOW_PATH_HIDDEN_DECL(slow_path_get_direct_pname);
237 SLOW_PATH_HIDDEN_DECL(slow_path_get_property_enumerator);
238 SLOW_PATH_HIDDEN_DECL(slow_path_next_structure_enumerator_pname);
239 SLOW_PATH_HIDDEN_DECL(slow_path_next_generic_enumerator_pname);
240 SLOW_PATH_HIDDEN_DECL(slow_path_to_index_string);
241 SLOW_PATH_HIDDEN_DECL(slow_path_profile_type_clear_log);
242 SLOW_PATH_HIDDEN_DECL(slow_path_assert);
243 SLOW_PATH_HIDDEN_DECL(slow_path_save);
244 SLOW_PATH_HIDDEN_DECL(slow_path_resume);
245 SLOW_PATH_HIDDEN_DECL(slow_path_create_lexical_environment);
246 SLOW_PATH_HIDDEN_DECL(slow_path_push_with_scope);
247 SLOW_PATH_HIDDEN_DECL(slow_path_resolve_scope);
248 SLOW_PATH_HIDDEN_DECL(slow_path_copy_rest);
249
250 } // namespace JSC
251
252 #endif // CommonSlowPaths_h