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