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