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