Implement Proxy [[Get]]
[WebKit-https.git] / Source / JavaScriptCore / jit / JITOperations.cpp
1 /*
2  * Copyright (C) 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 #include "config.h"
27 #include "JITOperations.h"
28
29 #if ENABLE(JIT)
30
31 #include "ArrayConstructor.h"
32 #include "DFGCompilationMode.h"
33 #include "DFGDriver.h"
34 #include "DFGOSREntry.h"
35 #include "DFGThunks.h"
36 #include "DFGWorklist.h"
37 #include "Debugger.h"
38 #include "DirectArguments.h"
39 #include "Error.h"
40 #include "ErrorHandlingScope.h"
41 #include "ExceptionFuzz.h"
42 #include "GetterSetter.h"
43 #include "HostCallReturnValue.h"
44 #include "JIT.h"
45 #include "JITToDFGDeferredCompilationCallback.h"
46 #include "JSCInlines.h"
47 #include "JSGeneratorFunction.h"
48 #include "JSGlobalObjectFunctions.h"
49 #include "JSLexicalEnvironment.h"
50 #include "JSPropertyNameEnumerator.h"
51 #include "JSStackInlines.h"
52 #include "JSWithScope.h"
53 #include "LegacyProfiler.h"
54 #include "ObjectConstructor.h"
55 #include "PropertyName.h"
56 #include "Repatch.h"
57 #include "ScopedArguments.h"
58 #include "TestRunnerUtils.h"
59 #include "TypeProfilerLog.h"
60 #include "VMInlines.h"
61 #include <wtf/InlineASM.h>
62
63 namespace JSC {
64
65 extern "C" {
66
67 #if COMPILER(MSVC)
68 void * _ReturnAddress(void);
69 #pragma intrinsic(_ReturnAddress)
70
71 #define OUR_RETURN_ADDRESS _ReturnAddress()
72 #else
73 #define OUR_RETURN_ADDRESS __builtin_return_address(0)
74 #endif
75
76 #if ENABLE(OPCODE_SAMPLING)
77 #define CTI_SAMPLER vm->interpreter->sampler()
78 #else
79 #define CTI_SAMPLER 0
80 #endif
81
82
83 void JIT_OPERATION operationThrowStackOverflowError(ExecState* exec, CodeBlock* codeBlock)
84 {
85     // We pass in our own code block, because the callframe hasn't been populated.
86     VM* vm = codeBlock->vm();
87
88     VMEntryFrame* vmEntryFrame = vm->topVMEntryFrame;
89     CallFrame* callerFrame = exec->callerFrame(vmEntryFrame);
90     if (!callerFrame)
91         callerFrame = exec;
92
93     NativeCallFrameTracerWithRestore tracer(vm, vmEntryFrame, callerFrame);
94     throwStackOverflowError(callerFrame);
95 }
96
97 #if ENABLE(WEBASSEMBLY)
98 void JIT_OPERATION operationThrowDivideError(ExecState* exec)
99 {
100     VM* vm = &exec->vm();
101     VMEntryFrame* vmEntryFrame = vm->topVMEntryFrame;
102     CallFrame* callerFrame = exec->callerFrame(vmEntryFrame);
103
104     NativeCallFrameTracerWithRestore tracer(vm, vmEntryFrame, callerFrame);
105     ErrorHandlingScope errorScope(*vm);
106     vm->throwException(callerFrame, createError(callerFrame, ASCIILiteral("Division by zero or division overflow.")));
107 }
108
109 void JIT_OPERATION operationThrowOutOfBoundsAccessError(ExecState* exec)
110 {
111     VM* vm = &exec->vm();
112     VMEntryFrame* vmEntryFrame = vm->topVMEntryFrame;
113     CallFrame* callerFrame = exec->callerFrame(vmEntryFrame);
114
115     NativeCallFrameTracerWithRestore tracer(vm, vmEntryFrame, callerFrame);
116     ErrorHandlingScope errorScope(*vm);
117     vm->throwException(callerFrame, createError(callerFrame, ASCIILiteral("Out-of-bounds access.")));
118 }
119 #endif
120
121 int32_t JIT_OPERATION operationCallArityCheck(ExecState* exec)
122 {
123     VM* vm = &exec->vm();
124     JSStack& stack = vm->interpreter->stack();
125
126     int32_t missingArgCount = CommonSlowPaths::arityCheckFor(exec, &stack, CodeForCall);
127     if (missingArgCount < 0) {
128         VMEntryFrame* vmEntryFrame = vm->topVMEntryFrame;
129         CallFrame* callerFrame = exec->callerFrame(vmEntryFrame);
130         NativeCallFrameTracerWithRestore tracer(vm, vmEntryFrame, callerFrame);
131         throwStackOverflowError(callerFrame);
132     }
133
134     return missingArgCount;
135 }
136
137 int32_t JIT_OPERATION operationConstructArityCheck(ExecState* exec)
138 {
139     VM* vm = &exec->vm();
140     JSStack& stack = vm->interpreter->stack();
141
142     int32_t missingArgCount = CommonSlowPaths::arityCheckFor(exec, &stack, CodeForConstruct);
143     if (missingArgCount < 0) {
144         VMEntryFrame* vmEntryFrame = vm->topVMEntryFrame;
145         CallFrame* callerFrame = exec->callerFrame(vmEntryFrame);
146         NativeCallFrameTracerWithRestore tracer(vm, vmEntryFrame, callerFrame);
147         throwStackOverflowError(callerFrame);
148     }
149
150     return missingArgCount;
151 }
152
153 EncodedJSValue JIT_OPERATION operationGetById(ExecState* exec, StructureStubInfo* stubInfo, EncodedJSValue base, UniquedStringImpl* uid)
154 {
155     VM* vm = &exec->vm();
156     NativeCallFrameTracer tracer(vm, exec);
157     
158     stubInfo->tookSlowPath = true;
159     
160     JSValue baseValue = JSValue::decode(base);
161     PropertySlot slot(baseValue, PropertySlot::InternalMethodType::Get);
162     Identifier ident = Identifier::fromUid(vm, uid);
163     return JSValue::encode(baseValue.get(exec, ident, slot));
164 }
165
166 EncodedJSValue JIT_OPERATION operationGetByIdGeneric(ExecState* exec, EncodedJSValue base, UniquedStringImpl* uid)
167 {
168     VM* vm = &exec->vm();
169     NativeCallFrameTracer tracer(vm, exec);
170     
171     JSValue baseValue = JSValue::decode(base);
172     PropertySlot slot(baseValue, PropertySlot::InternalMethodType::Get);
173     Identifier ident = Identifier::fromUid(vm, uid);
174     return JSValue::encode(baseValue.get(exec, ident, slot));
175 }
176
177 EncodedJSValue JIT_OPERATION operationGetByIdOptimize(ExecState* exec, StructureStubInfo* stubInfo, EncodedJSValue base, UniquedStringImpl* uid)
178 {
179     VM* vm = &exec->vm();
180     NativeCallFrameTracer tracer(vm, exec);
181     Identifier ident = Identifier::fromUid(vm, uid);
182
183     JSValue baseValue = JSValue::decode(base);
184     PropertySlot slot(baseValue, PropertySlot::InternalMethodType::Get);
185     
186     bool hasResult = baseValue.getPropertySlot(exec, ident, slot);
187     if (stubInfo->considerCaching())
188         repatchGetByID(exec, baseValue, ident, slot, *stubInfo);
189     
190     return JSValue::encode(hasResult? slot.getValue(exec, ident) : jsUndefined());
191 }
192
193 EncodedJSValue JIT_OPERATION operationInOptimize(ExecState* exec, StructureStubInfo* stubInfo, JSCell* base, UniquedStringImpl* key)
194 {
195     VM* vm = &exec->vm();
196     NativeCallFrameTracer tracer(vm, exec);
197     
198     if (!base->isObject()) {
199         vm->throwException(exec, createInvalidInParameterError(exec, base));
200         return JSValue::encode(jsUndefined());
201     }
202     
203     AccessType accessType = static_cast<AccessType>(stubInfo->accessType);
204
205     Identifier ident = Identifier::fromUid(vm, key);
206     PropertySlot slot(base, PropertySlot::InternalMethodType::HasProperty);
207     bool result = asObject(base)->getPropertySlot(exec, ident, slot);
208     
209     RELEASE_ASSERT(accessType == stubInfo->accessType);
210     
211     if (stubInfo->considerCaching())
212         repatchIn(exec, base, ident, result, slot, *stubInfo);
213     
214     return JSValue::encode(jsBoolean(result));
215 }
216
217 EncodedJSValue JIT_OPERATION operationIn(ExecState* exec, StructureStubInfo* stubInfo, JSCell* base, UniquedStringImpl* key)
218 {
219     VM* vm = &exec->vm();
220     NativeCallFrameTracer tracer(vm, exec);
221     
222     stubInfo->tookSlowPath = true;
223
224     if (!base->isObject()) {
225         vm->throwException(exec, createInvalidInParameterError(exec, base));
226         return JSValue::encode(jsUndefined());
227     }
228
229     Identifier ident = Identifier::fromUid(vm, key);
230     return JSValue::encode(jsBoolean(asObject(base)->hasProperty(exec, ident)));
231 }
232
233 EncodedJSValue JIT_OPERATION operationGenericIn(ExecState* exec, JSCell* base, EncodedJSValue key)
234 {
235     VM* vm = &exec->vm();
236     NativeCallFrameTracer tracer(vm, exec);
237
238     return JSValue::encode(jsBoolean(CommonSlowPaths::opIn(exec, JSValue::decode(key), base)));
239 }
240
241 void JIT_OPERATION operationPutByIdStrict(ExecState* exec, StructureStubInfo* stubInfo, EncodedJSValue encodedValue, EncodedJSValue encodedBase, UniquedStringImpl* uid)
242 {
243     VM* vm = &exec->vm();
244     NativeCallFrameTracer tracer(vm, exec);
245     
246     stubInfo->tookSlowPath = true;
247     
248     Identifier ident = Identifier::fromUid(vm, uid);
249     PutPropertySlot slot(JSValue::decode(encodedBase), true, exec->codeBlock()->putByIdContext());
250     JSValue::decode(encodedBase).putInline(exec, ident, JSValue::decode(encodedValue), slot);
251 }
252
253 void JIT_OPERATION operationPutByIdNonStrict(ExecState* exec, StructureStubInfo* stubInfo, EncodedJSValue encodedValue, EncodedJSValue encodedBase, UniquedStringImpl* uid)
254 {
255     VM* vm = &exec->vm();
256     NativeCallFrameTracer tracer(vm, exec);
257     
258     stubInfo->tookSlowPath = true;
259     
260     Identifier ident = Identifier::fromUid(vm, uid);
261     PutPropertySlot slot(JSValue::decode(encodedBase), false, exec->codeBlock()->putByIdContext());
262     JSValue::decode(encodedBase).putInline(exec, ident, JSValue::decode(encodedValue), slot);
263 }
264
265 void JIT_OPERATION operationPutByIdDirectStrict(ExecState* exec, StructureStubInfo* stubInfo, EncodedJSValue encodedValue, EncodedJSValue encodedBase, UniquedStringImpl* uid)
266 {
267     VM* vm = &exec->vm();
268     NativeCallFrameTracer tracer(vm, exec);
269     
270     stubInfo->tookSlowPath = true;
271     
272     Identifier ident = Identifier::fromUid(vm, uid);
273     PutPropertySlot slot(JSValue::decode(encodedBase), true, exec->codeBlock()->putByIdContext());
274     asObject(JSValue::decode(encodedBase))->putDirect(exec->vm(), ident, JSValue::decode(encodedValue), slot);
275 }
276
277 void JIT_OPERATION operationPutByIdDirectNonStrict(ExecState* exec, StructureStubInfo* stubInfo, EncodedJSValue encodedValue, EncodedJSValue encodedBase, UniquedStringImpl* uid)
278 {
279     VM* vm = &exec->vm();
280     NativeCallFrameTracer tracer(vm, exec);
281     
282     stubInfo->tookSlowPath = true;
283     
284     Identifier ident = Identifier::fromUid(vm, uid);
285     PutPropertySlot slot(JSValue::decode(encodedBase), false, exec->codeBlock()->putByIdContext());
286     asObject(JSValue::decode(encodedBase))->putDirect(exec->vm(), ident, JSValue::decode(encodedValue), slot);
287 }
288
289 void JIT_OPERATION operationPutByIdStrictOptimize(ExecState* exec, StructureStubInfo* stubInfo, EncodedJSValue encodedValue, EncodedJSValue encodedBase, UniquedStringImpl* uid)
290 {
291     VM* vm = &exec->vm();
292     NativeCallFrameTracer tracer(vm, exec);
293     
294     Identifier ident = Identifier::fromUid(vm, uid);
295     AccessType accessType = static_cast<AccessType>(stubInfo->accessType);
296
297     JSValue value = JSValue::decode(encodedValue);
298     JSValue baseValue = JSValue::decode(encodedBase);
299     PutPropertySlot slot(baseValue, true, exec->codeBlock()->putByIdContext());
300
301     Structure* structure = baseValue.isCell() ? baseValue.asCell()->structure(*vm) : nullptr;
302     baseValue.putInline(exec, ident, value, slot);
303     
304     if (accessType != static_cast<AccessType>(stubInfo->accessType))
305         return;
306     
307     if (stubInfo->considerCaching())
308         repatchPutByID(exec, baseValue, structure, ident, slot, *stubInfo, NotDirect);
309 }
310
311 void JIT_OPERATION operationPutByIdNonStrictOptimize(ExecState* exec, StructureStubInfo* stubInfo, EncodedJSValue encodedValue, EncodedJSValue encodedBase, UniquedStringImpl* uid)
312 {
313     VM* vm = &exec->vm();
314     NativeCallFrameTracer tracer(vm, exec);
315     
316     Identifier ident = Identifier::fromUid(vm, uid);
317     AccessType accessType = static_cast<AccessType>(stubInfo->accessType);
318
319     JSValue value = JSValue::decode(encodedValue);
320     JSValue baseValue = JSValue::decode(encodedBase);
321     PutPropertySlot slot(baseValue, false, exec->codeBlock()->putByIdContext());
322
323     Structure* structure = baseValue.isCell() ? baseValue.asCell()->structure(*vm) : nullptr;    
324     baseValue.putInline(exec, ident, value, slot);
325     
326     if (accessType != static_cast<AccessType>(stubInfo->accessType))
327         return;
328     
329     if (stubInfo->considerCaching())
330         repatchPutByID(exec, baseValue, structure, ident, slot, *stubInfo, NotDirect);
331 }
332
333 void JIT_OPERATION operationPutByIdDirectStrictOptimize(ExecState* exec, StructureStubInfo* stubInfo, EncodedJSValue encodedValue, EncodedJSValue encodedBase, UniquedStringImpl* uid)
334 {
335     VM* vm = &exec->vm();
336     NativeCallFrameTracer tracer(vm, exec);
337     
338     Identifier ident = Identifier::fromUid(vm, uid);
339     AccessType accessType = static_cast<AccessType>(stubInfo->accessType);
340
341     JSValue value = JSValue::decode(encodedValue);
342     JSObject* baseObject = asObject(JSValue::decode(encodedBase));
343     PutPropertySlot slot(baseObject, true, exec->codeBlock()->putByIdContext());
344     
345     Structure* structure = baseObject->structure(*vm);
346     baseObject->putDirect(exec->vm(), ident, value, slot);
347     
348     if (accessType != static_cast<AccessType>(stubInfo->accessType))
349         return;
350     
351     if (stubInfo->considerCaching())
352         repatchPutByID(exec, baseObject, structure, ident, slot, *stubInfo, Direct);
353 }
354
355 void JIT_OPERATION operationPutByIdDirectNonStrictOptimize(ExecState* exec, StructureStubInfo* stubInfo, EncodedJSValue encodedValue, EncodedJSValue encodedBase, UniquedStringImpl* uid)
356 {
357     VM* vm = &exec->vm();
358     NativeCallFrameTracer tracer(vm, exec);
359     
360     Identifier ident = Identifier::fromUid(vm, uid);
361     AccessType accessType = static_cast<AccessType>(stubInfo->accessType);
362
363     JSValue value = JSValue::decode(encodedValue);
364     JSObject* baseObject = asObject(JSValue::decode(encodedBase));
365     PutPropertySlot slot(baseObject, false, exec->codeBlock()->putByIdContext());
366     
367     Structure* structure = baseObject->structure(*vm);
368     baseObject->putDirect(exec->vm(), ident, value, slot);
369     
370     if (accessType != static_cast<AccessType>(stubInfo->accessType))
371         return;
372     
373     if (stubInfo->considerCaching())
374         repatchPutByID(exec, baseObject, structure, ident, slot, *stubInfo, Direct);
375 }
376
377 void JIT_OPERATION operationReallocateStorageAndFinishPut(ExecState* exec, JSObject* base, Structure* structure, PropertyOffset offset, EncodedJSValue value)
378 {
379     VM& vm = exec->vm();
380     NativeCallFrameTracer tracer(&vm, exec);
381
382     ASSERT(structure->outOfLineCapacity() > base->structure(vm)->outOfLineCapacity());
383     ASSERT(!vm.heap.storageAllocator().fastPathShouldSucceed(structure->outOfLineCapacity() * sizeof(JSValue)));
384     base->setStructureAndReallocateStorageIfNecessary(vm, structure);
385     base->putDirect(vm, offset, JSValue::decode(value));
386 }
387
388 ALWAYS_INLINE static bool isStringOrSymbol(JSValue value)
389 {
390     return value.isString() || value.isSymbol();
391 }
392
393 static void putByVal(CallFrame* callFrame, JSValue baseValue, JSValue subscript, JSValue value, ByValInfo* byValInfo)
394 {
395     VM& vm = callFrame->vm();
396     if (LIKELY(subscript.isUInt32())) {
397         byValInfo->tookSlowPath = true;
398         uint32_t i = subscript.asUInt32();
399         if (baseValue.isObject()) {
400             JSObject* object = asObject(baseValue);
401             if (object->canSetIndexQuickly(i))
402                 object->setIndexQuickly(callFrame->vm(), i, value);
403             else {
404                 // FIXME: This will make us think that in-bounds typed array accesses are actually
405                 // out-of-bounds.
406                 // https://bugs.webkit.org/show_bug.cgi?id=149886
407                 byValInfo->arrayProfile->setOutOfBounds();
408                 object->methodTable(vm)->putByIndex(object, callFrame, i, value, callFrame->codeBlock()->isStrictMode());
409             }
410         } else
411             baseValue.putByIndex(callFrame, i, value, callFrame->codeBlock()->isStrictMode());
412         return;
413     }
414
415     auto property = subscript.toPropertyKey(callFrame);
416     // Don't put to an object if toString threw an exception.
417     if (callFrame->vm().exception())
418         return;
419
420     if (byValInfo->stubInfo && (!isStringOrSymbol(subscript) || byValInfo->cachedId != property))
421         byValInfo->tookSlowPath = true;
422
423     PutPropertySlot slot(baseValue, callFrame->codeBlock()->isStrictMode());
424     baseValue.putInline(callFrame, property, value, slot);
425 }
426
427 static void directPutByVal(CallFrame* callFrame, JSObject* baseObject, JSValue subscript, JSValue value, ByValInfo* byValInfo)
428 {
429     bool isStrictMode = callFrame->codeBlock()->isStrictMode();
430     if (LIKELY(subscript.isUInt32())) {
431         // Despite its name, JSValue::isUInt32 will return true only for positive boxed int32_t; all those values are valid array indices.
432         byValInfo->tookSlowPath = true;
433         uint32_t index = subscript.asUInt32();
434         ASSERT(isIndex(index));
435         if (baseObject->canSetIndexQuicklyForPutDirect(index)) {
436             baseObject->setIndexQuickly(callFrame->vm(), index, value);
437             return;
438         }
439
440         // FIXME: This will make us think that in-bounds typed array accesses are actually
441         // out-of-bounds.
442         // https://bugs.webkit.org/show_bug.cgi?id=149886
443         byValInfo->arrayProfile->setOutOfBounds();
444         baseObject->putDirectIndex(callFrame, index, value, 0, isStrictMode ? PutDirectIndexShouldThrow : PutDirectIndexShouldNotThrow);
445         return;
446     }
447
448     if (subscript.isDouble()) {
449         double subscriptAsDouble = subscript.asDouble();
450         uint32_t subscriptAsUInt32 = static_cast<uint32_t>(subscriptAsDouble);
451         if (subscriptAsDouble == subscriptAsUInt32 && isIndex(subscriptAsUInt32)) {
452             byValInfo->tookSlowPath = true;
453             baseObject->putDirectIndex(callFrame, subscriptAsUInt32, value, 0, isStrictMode ? PutDirectIndexShouldThrow : PutDirectIndexShouldNotThrow);
454             return;
455         }
456     }
457
458     // Don't put to an object if toString threw an exception.
459     auto property = subscript.toPropertyKey(callFrame);
460     if (callFrame->vm().exception())
461         return;
462
463     if (Optional<uint32_t> index = parseIndex(property)) {
464         byValInfo->tookSlowPath = true;
465         baseObject->putDirectIndex(callFrame, index.value(), value, 0, isStrictMode ? PutDirectIndexShouldThrow : PutDirectIndexShouldNotThrow);
466         return;
467     }
468
469     if (byValInfo->stubInfo && (!isStringOrSymbol(subscript) || byValInfo->cachedId != property))
470         byValInfo->tookSlowPath = true;
471
472     PutPropertySlot slot(baseObject, isStrictMode);
473     baseObject->putDirect(callFrame->vm(), property, value, slot);
474 }
475
476 enum class OptimizationResult {
477     NotOptimized,
478     SeenOnce,
479     Optimized,
480     GiveUp,
481 };
482
483 static OptimizationResult tryPutByValOptimize(ExecState* exec, JSValue baseValue, JSValue subscript, ByValInfo* byValInfo, ReturnAddressPtr returnAddress)
484 {
485     // See if it's worth optimizing at all.
486     OptimizationResult optimizationResult = OptimizationResult::NotOptimized;
487
488     VM& vm = exec->vm();
489
490     if (baseValue.isObject() && subscript.isInt32()) {
491         JSObject* object = asObject(baseValue);
492
493         ASSERT(exec->bytecodeOffset());
494         ASSERT(!byValInfo->stubRoutine);
495
496         Structure* structure = object->structure(vm);
497         if (hasOptimizableIndexing(structure)) {
498             // Attempt to optimize.
499             JITArrayMode arrayMode = jitArrayModeForStructure(structure);
500             if (jitArrayModePermitsPut(arrayMode) && arrayMode != byValInfo->arrayMode) {
501                 CodeBlock* codeBlock = exec->codeBlock();
502                 ConcurrentJITLocker locker(codeBlock->m_lock);
503                 byValInfo->arrayProfile->computeUpdatedPrediction(locker, codeBlock, structure);
504
505                 JIT::compilePutByVal(&vm, exec->codeBlock(), byValInfo, returnAddress, arrayMode);
506                 optimizationResult = OptimizationResult::Optimized;
507             }
508         }
509
510         // If we failed to patch and we have some object that intercepts indexed get, then don't even wait until 10 times.
511         if (optimizationResult != OptimizationResult::Optimized && object->structure(vm)->typeInfo().interceptsGetOwnPropertySlotByIndexEvenWhenLengthIsNotZero())
512             optimizationResult = OptimizationResult::GiveUp;
513     }
514
515     if (baseValue.isObject() && isStringOrSymbol(subscript)) {
516         const Identifier propertyName = subscript.toPropertyKey(exec);
517         if (!subscript.isString() || !parseIndex(propertyName)) {
518             ASSERT(exec->bytecodeOffset());
519             ASSERT(!byValInfo->stubRoutine);
520             if (byValInfo->seen) {
521                 if (byValInfo->cachedId == propertyName) {
522                     JIT::compilePutByValWithCachedId(&vm, exec->codeBlock(), byValInfo, returnAddress, NotDirect, propertyName);
523                     optimizationResult = OptimizationResult::Optimized;
524                 } else {
525                     // Seem like a generic property access site.
526                     optimizationResult = OptimizationResult::GiveUp;
527                 }
528             } else {
529                 byValInfo->seen = true;
530                 byValInfo->cachedId = propertyName;
531                 optimizationResult = OptimizationResult::SeenOnce;
532             }
533         }
534     }
535
536     if (optimizationResult != OptimizationResult::Optimized && optimizationResult != OptimizationResult::SeenOnce) {
537         // If we take slow path more than 10 times without patching then make sure we
538         // never make that mistake again. For cases where we see non-index-intercepting
539         // objects, this gives 10 iterations worth of opportunity for us to observe
540         // that the put_by_val may be polymorphic. We count up slowPathCount even if
541         // the result is GiveUp.
542         if (++byValInfo->slowPathCount >= 10)
543             optimizationResult = OptimizationResult::GiveUp;
544     }
545
546     return optimizationResult;
547 }
548
549 void JIT_OPERATION operationPutByValOptimize(ExecState* exec, EncodedJSValue encodedBaseValue, EncodedJSValue encodedSubscript, EncodedJSValue encodedValue, ByValInfo* byValInfo)
550 {
551     VM& vm = exec->vm();
552     NativeCallFrameTracer tracer(&vm, exec);
553
554     JSValue baseValue = JSValue::decode(encodedBaseValue);
555     JSValue subscript = JSValue::decode(encodedSubscript);
556     JSValue value = JSValue::decode(encodedValue);
557     if (tryPutByValOptimize(exec, baseValue, subscript, byValInfo, ReturnAddressPtr(OUR_RETURN_ADDRESS)) == OptimizationResult::GiveUp) {
558         // Don't ever try to optimize.
559         byValInfo->tookSlowPath = true;
560         ctiPatchCallByReturnAddress(ReturnAddressPtr(OUR_RETURN_ADDRESS), FunctionPtr(operationPutByValGeneric));
561     }
562     putByVal(exec, baseValue, subscript, value, byValInfo);
563 }
564
565 static OptimizationResult tryDirectPutByValOptimize(ExecState* exec, JSObject* object, JSValue subscript, ByValInfo* byValInfo, ReturnAddressPtr returnAddress)
566 {
567     // See if it's worth optimizing at all.
568     OptimizationResult optimizationResult = OptimizationResult::NotOptimized;
569
570     VM& vm = exec->vm();
571
572     if (subscript.isInt32()) {
573         ASSERT(exec->bytecodeOffset());
574         ASSERT(!byValInfo->stubRoutine);
575
576         Structure* structure = object->structure(vm);
577         if (hasOptimizableIndexing(structure)) {
578             // Attempt to optimize.
579             JITArrayMode arrayMode = jitArrayModeForStructure(structure);
580             if (jitArrayModePermitsPut(arrayMode) && arrayMode != byValInfo->arrayMode) {
581                 CodeBlock* codeBlock = exec->codeBlock();
582                 ConcurrentJITLocker locker(codeBlock->m_lock);
583                 byValInfo->arrayProfile->computeUpdatedPrediction(locker, codeBlock, structure);
584
585                 JIT::compileDirectPutByVal(&vm, exec->codeBlock(), byValInfo, returnAddress, arrayMode);
586                 optimizationResult = OptimizationResult::Optimized;
587             }
588         }
589
590         // If we failed to patch and we have some object that intercepts indexed get, then don't even wait until 10 times.
591         if (optimizationResult != OptimizationResult::Optimized && object->structure(vm)->typeInfo().interceptsGetOwnPropertySlotByIndexEvenWhenLengthIsNotZero())
592             optimizationResult = OptimizationResult::GiveUp;
593     } else if (isStringOrSymbol(subscript)) {
594         const Identifier propertyName = subscript.toPropertyKey(exec);
595         Optional<uint32_t> index = parseIndex(propertyName);
596
597         if (!subscript.isString() || !index) {
598             ASSERT(exec->bytecodeOffset());
599             ASSERT(!byValInfo->stubRoutine);
600             if (byValInfo->seen) {
601                 if (byValInfo->cachedId == propertyName) {
602                     JIT::compilePutByValWithCachedId(&vm, exec->codeBlock(), byValInfo, returnAddress, Direct, propertyName);
603                     optimizationResult = OptimizationResult::Optimized;
604                 } else {
605                     // Seem like a generic property access site.
606                     optimizationResult = OptimizationResult::GiveUp;
607                 }
608             } else {
609                 byValInfo->seen = true;
610                 byValInfo->cachedId = propertyName;
611                 optimizationResult = OptimizationResult::SeenOnce;
612             }
613         }
614     }
615
616     if (optimizationResult != OptimizationResult::Optimized && optimizationResult != OptimizationResult::SeenOnce) {
617         // If we take slow path more than 10 times without patching then make sure we
618         // never make that mistake again. For cases where we see non-index-intercepting
619         // objects, this gives 10 iterations worth of opportunity for us to observe
620         // that the get_by_val may be polymorphic. We count up slowPathCount even if
621         // the result is GiveUp.
622         if (++byValInfo->slowPathCount >= 10)
623             optimizationResult = OptimizationResult::GiveUp;
624     }
625
626     return optimizationResult;
627 }
628
629 void JIT_OPERATION operationDirectPutByValOptimize(ExecState* exec, EncodedJSValue encodedBaseValue, EncodedJSValue encodedSubscript, EncodedJSValue encodedValue, ByValInfo* byValInfo)
630 {
631     VM& vm = exec->vm();
632     NativeCallFrameTracer tracer(&vm, exec);
633
634     JSValue baseValue = JSValue::decode(encodedBaseValue);
635     JSValue subscript = JSValue::decode(encodedSubscript);
636     JSValue value = JSValue::decode(encodedValue);
637     RELEASE_ASSERT(baseValue.isObject());
638     JSObject* object = asObject(baseValue);
639     if (tryDirectPutByValOptimize(exec, object, subscript, byValInfo, ReturnAddressPtr(OUR_RETURN_ADDRESS)) == OptimizationResult::GiveUp) {
640         // Don't ever try to optimize.
641         byValInfo->tookSlowPath = true;
642         ctiPatchCallByReturnAddress(ReturnAddressPtr(OUR_RETURN_ADDRESS), FunctionPtr(operationDirectPutByValGeneric));
643     }
644
645     directPutByVal(exec, object, subscript, value, byValInfo);
646 }
647
648 void JIT_OPERATION operationPutByValGeneric(ExecState* exec, EncodedJSValue encodedBaseValue, EncodedJSValue encodedSubscript, EncodedJSValue encodedValue, ByValInfo* byValInfo)
649 {
650     VM& vm = exec->vm();
651     NativeCallFrameTracer tracer(&vm, exec);
652     
653     JSValue baseValue = JSValue::decode(encodedBaseValue);
654     JSValue subscript = JSValue::decode(encodedSubscript);
655     JSValue value = JSValue::decode(encodedValue);
656
657     putByVal(exec, baseValue, subscript, value, byValInfo);
658 }
659
660
661 void JIT_OPERATION operationDirectPutByValGeneric(ExecState* exec, EncodedJSValue encodedBaseValue, EncodedJSValue encodedSubscript, EncodedJSValue encodedValue, ByValInfo* byValInfo)
662 {
663     VM& vm = exec->vm();
664     NativeCallFrameTracer tracer(&vm, exec);
665     
666     JSValue baseValue = JSValue::decode(encodedBaseValue);
667     JSValue subscript = JSValue::decode(encodedSubscript);
668     JSValue value = JSValue::decode(encodedValue);
669     RELEASE_ASSERT(baseValue.isObject());
670     directPutByVal(exec, asObject(baseValue), subscript, value, byValInfo);
671 }
672
673 EncodedJSValue JIT_OPERATION operationCallEval(ExecState* exec, ExecState* execCallee)
674 {
675     UNUSED_PARAM(exec);
676
677     execCallee->setCodeBlock(0);
678
679     if (!isHostFunction(execCallee->calleeAsValue(), globalFuncEval))
680         return JSValue::encode(JSValue());
681
682     VM* vm = &execCallee->vm();
683     JSValue result = eval(execCallee);
684     if (vm->exception())
685         return EncodedJSValue();
686     
687     return JSValue::encode(result);
688 }
689
690 static SlowPathReturnType handleHostCall(ExecState* execCallee, JSValue callee, CallLinkInfo* callLinkInfo)
691 {
692     ExecState* exec = execCallee->callerFrame();
693     VM* vm = &exec->vm();
694
695     execCallee->setCodeBlock(0);
696
697     if (callLinkInfo->specializationKind() == CodeForCall) {
698         CallData callData;
699         CallType callType = getCallData(callee, callData);
700     
701         ASSERT(callType != CallTypeJS);
702     
703         if (callType == CallTypeHost) {
704             NativeCallFrameTracer tracer(vm, execCallee);
705             execCallee->setCallee(asObject(callee));
706             vm->hostCallReturnValue = JSValue::decode(callData.native.function(execCallee));
707             if (vm->exception()) {
708                 return encodeResult(
709                     vm->getCTIStub(throwExceptionFromCallSlowPathGenerator).code().executableAddress(),
710                     reinterpret_cast<void*>(KeepTheFrame));
711             }
712
713             return encodeResult(
714                 bitwise_cast<void*>(getHostCallReturnValue),
715                 reinterpret_cast<void*>(callLinkInfo->callMode() == CallMode::Tail ? ReuseTheFrame : KeepTheFrame));
716         }
717     
718         ASSERT(callType == CallTypeNone);
719         exec->vm().throwException(exec, createNotAFunctionError(exec, callee));
720         return encodeResult(
721             vm->getCTIStub(throwExceptionFromCallSlowPathGenerator).code().executableAddress(),
722             reinterpret_cast<void*>(KeepTheFrame));
723     }
724
725     ASSERT(callLinkInfo->specializationKind() == CodeForConstruct);
726     
727     ConstructData constructData;
728     ConstructType constructType = getConstructData(callee, constructData);
729     
730     ASSERT(constructType != ConstructTypeJS);
731     
732     if (constructType == ConstructTypeHost) {
733         NativeCallFrameTracer tracer(vm, execCallee);
734         execCallee->setCallee(asObject(callee));
735         vm->hostCallReturnValue = JSValue::decode(constructData.native.function(execCallee));
736         if (vm->exception()) {
737             return encodeResult(
738                 vm->getCTIStub(throwExceptionFromCallSlowPathGenerator).code().executableAddress(),
739                 reinterpret_cast<void*>(KeepTheFrame));
740         }
741
742         return encodeResult(bitwise_cast<void*>(getHostCallReturnValue), reinterpret_cast<void*>(KeepTheFrame));
743     }
744     
745     ASSERT(constructType == ConstructTypeNone);
746     exec->vm().throwException(exec, createNotAConstructorError(exec, callee));
747     return encodeResult(
748         vm->getCTIStub(throwExceptionFromCallSlowPathGenerator).code().executableAddress(),
749         reinterpret_cast<void*>(KeepTheFrame));
750 }
751
752 SlowPathReturnType JIT_OPERATION operationLinkCall(ExecState* execCallee, CallLinkInfo* callLinkInfo)
753 {
754     ExecState* exec = execCallee->callerFrame();
755     VM* vm = &exec->vm();
756     CodeSpecializationKind kind = callLinkInfo->specializationKind();
757     NativeCallFrameTracer tracer(vm, exec);
758     
759     JSValue calleeAsValue = execCallee->calleeAsValue();
760     JSCell* calleeAsFunctionCell = getJSFunction(calleeAsValue);
761     if (!calleeAsFunctionCell) {
762         // FIXME: We should cache these kinds of calls. They can be common and currently they are
763         // expensive.
764         // https://bugs.webkit.org/show_bug.cgi?id=144458
765         return handleHostCall(execCallee, calleeAsValue, callLinkInfo);
766     }
767
768     JSFunction* callee = jsCast<JSFunction*>(calleeAsFunctionCell);
769     JSScope* scope = callee->scopeUnchecked();
770     ExecutableBase* executable = callee->executable();
771
772     MacroAssemblerCodePtr codePtr;
773     CodeBlock* codeBlock = 0;
774     if (executable->isHostFunction()) {
775         codePtr = executable->entrypointFor(kind, MustCheckArity);
776 #if ENABLE(WEBASSEMBLY)
777     } else if (executable->isWebAssemblyExecutable()) {
778         WebAssemblyExecutable* webAssemblyExecutable = static_cast<WebAssemblyExecutable*>(executable);
779         webAssemblyExecutable->prepareForExecution(execCallee);
780         codeBlock = webAssemblyExecutable->codeBlockForCall();
781         ASSERT(codeBlock);
782         ArityCheckMode arity;
783         if (execCallee->argumentCountIncludingThis() < static_cast<size_t>(codeBlock->numParameters()))
784             arity = MustCheckArity;
785         else
786             arity = ArityCheckNotRequired;
787         codePtr = webAssemblyExecutable->entrypointFor(kind, arity);
788 #endif
789     } else {
790         FunctionExecutable* functionExecutable = static_cast<FunctionExecutable*>(executable);
791
792         if (!isCall(kind) && functionExecutable->constructAbility() == ConstructAbility::CannotConstruct) {
793             exec->vm().throwException(exec, createNotAConstructorError(exec, callee));
794             return encodeResult(
795                 vm->getCTIStub(throwExceptionFromCallSlowPathGenerator).code().executableAddress(),
796                 reinterpret_cast<void*>(KeepTheFrame));
797         }
798
799         JSObject* error = functionExecutable->prepareForExecution(execCallee, callee, scope, kind);
800         if (error) {
801             exec->vm().throwException(exec, error);
802             return encodeResult(
803                 vm->getCTIStub(throwExceptionFromCallSlowPathGenerator).code().executableAddress(),
804                 reinterpret_cast<void*>(KeepTheFrame));
805         }
806         codeBlock = functionExecutable->codeBlockFor(kind);
807         ArityCheckMode arity;
808         if (execCallee->argumentCountIncludingThis() < static_cast<size_t>(codeBlock->numParameters()) || callLinkInfo->isVarargs())
809             arity = MustCheckArity;
810         else
811             arity = ArityCheckNotRequired;
812         codePtr = functionExecutable->entrypointFor(kind, arity);
813     }
814     if (!callLinkInfo->seenOnce())
815         callLinkInfo->setSeen();
816     else
817         linkFor(execCallee, *callLinkInfo, codeBlock, callee, codePtr);
818     
819     return encodeResult(codePtr.executableAddress(), reinterpret_cast<void*>(callLinkInfo->callMode() == CallMode::Tail ? ReuseTheFrame : KeepTheFrame));
820 }
821
822 inline SlowPathReturnType virtualForWithFunction(
823     ExecState* execCallee, CallLinkInfo* callLinkInfo, JSCell*& calleeAsFunctionCell)
824 {
825     ExecState* exec = execCallee->callerFrame();
826     VM* vm = &exec->vm();
827     CodeSpecializationKind kind = callLinkInfo->specializationKind();
828     NativeCallFrameTracer tracer(vm, exec);
829
830     JSValue calleeAsValue = execCallee->calleeAsValue();
831     calleeAsFunctionCell = getJSFunction(calleeAsValue);
832     if (UNLIKELY(!calleeAsFunctionCell))
833         return handleHostCall(execCallee, calleeAsValue, callLinkInfo);
834     
835     JSFunction* function = jsCast<JSFunction*>(calleeAsFunctionCell);
836     JSScope* scope = function->scopeUnchecked();
837     ExecutableBase* executable = function->executable();
838     if (UNLIKELY(!executable->hasJITCodeFor(kind))) {
839         bool isWebAssemblyExecutable = false;
840 #if ENABLE(WEBASSEMBLY)
841         isWebAssemblyExecutable = executable->isWebAssemblyExecutable();
842 #endif
843         if (!isWebAssemblyExecutable) {
844             FunctionExecutable* functionExecutable = static_cast<FunctionExecutable*>(executable);
845
846             if (!isCall(kind) && functionExecutable->constructAbility() == ConstructAbility::CannotConstruct) {
847                 exec->vm().throwException(exec, createNotAConstructorError(exec, function));
848                 return encodeResult(
849                     vm->getCTIStub(throwExceptionFromCallSlowPathGenerator).code().executableAddress(),
850                     reinterpret_cast<void*>(KeepTheFrame));
851             }
852
853             JSObject* error = functionExecutable->prepareForExecution(execCallee, function, scope, kind);
854             if (error) {
855                 exec->vm().throwException(exec, error);
856                 return encodeResult(
857                     vm->getCTIStub(throwExceptionFromCallSlowPathGenerator).code().executableAddress(),
858                     reinterpret_cast<void*>(KeepTheFrame));
859             }
860         } else {
861 #if ENABLE(WEBASSEMBLY)
862             if (!isCall(kind)) {
863                 exec->vm().throwException(exec, createNotAConstructorError(exec, function));
864                 return encodeResult(
865                     vm->getCTIStub(throwExceptionFromCallSlowPathGenerator).code().executableAddress(),
866                     reinterpret_cast<void*>(KeepTheFrame));
867             }
868
869             WebAssemblyExecutable* webAssemblyExecutable = static_cast<WebAssemblyExecutable*>(executable);
870             webAssemblyExecutable->prepareForExecution(execCallee);
871 #endif
872         }
873     }
874     return encodeResult(executable->entrypointFor(
875         kind, MustCheckArity).executableAddress(),
876         reinterpret_cast<void*>(callLinkInfo->callMode() == CallMode::Tail ? ReuseTheFrame : KeepTheFrame));
877 }
878
879 SlowPathReturnType JIT_OPERATION operationLinkPolymorphicCall(ExecState* execCallee, CallLinkInfo* callLinkInfo)
880 {
881     ASSERT(callLinkInfo->specializationKind() == CodeForCall);
882     JSCell* calleeAsFunctionCell;
883     SlowPathReturnType result = virtualForWithFunction(execCallee, callLinkInfo, calleeAsFunctionCell);
884
885     linkPolymorphicCall(execCallee, *callLinkInfo, CallVariant(calleeAsFunctionCell));
886     
887     return result;
888 }
889
890 SlowPathReturnType JIT_OPERATION operationVirtualCall(ExecState* execCallee, CallLinkInfo* callLinkInfo)
891 {
892     JSCell* calleeAsFunctionCellIgnored;
893     return virtualForWithFunction(execCallee, callLinkInfo, calleeAsFunctionCellIgnored);
894 }
895
896 size_t JIT_OPERATION operationCompareLess(ExecState* exec, EncodedJSValue encodedOp1, EncodedJSValue encodedOp2)
897 {
898     VM* vm = &exec->vm();
899     NativeCallFrameTracer tracer(vm, exec);
900     
901     return jsLess<true>(exec, JSValue::decode(encodedOp1), JSValue::decode(encodedOp2));
902 }
903
904 size_t JIT_OPERATION operationCompareLessEq(ExecState* exec, EncodedJSValue encodedOp1, EncodedJSValue encodedOp2)
905 {
906     VM* vm = &exec->vm();
907     NativeCallFrameTracer tracer(vm, exec);
908
909     return jsLessEq<true>(exec, JSValue::decode(encodedOp1), JSValue::decode(encodedOp2));
910 }
911
912 size_t JIT_OPERATION operationCompareGreater(ExecState* exec, EncodedJSValue encodedOp1, EncodedJSValue encodedOp2)
913 {
914     VM* vm = &exec->vm();
915     NativeCallFrameTracer tracer(vm, exec);
916
917     return jsLess<false>(exec, JSValue::decode(encodedOp2), JSValue::decode(encodedOp1));
918 }
919
920 size_t JIT_OPERATION operationCompareGreaterEq(ExecState* exec, EncodedJSValue encodedOp1, EncodedJSValue encodedOp2)
921 {
922     VM* vm = &exec->vm();
923     NativeCallFrameTracer tracer(vm, exec);
924
925     return jsLessEq<false>(exec, JSValue::decode(encodedOp2), JSValue::decode(encodedOp1));
926 }
927
928 size_t JIT_OPERATION operationConvertJSValueToBoolean(ExecState* exec, EncodedJSValue encodedOp)
929 {
930     VM* vm = &exec->vm();
931     NativeCallFrameTracer tracer(vm, exec);
932     
933     return JSValue::decode(encodedOp).toBoolean(exec);
934 }
935
936 size_t JIT_OPERATION operationCompareEq(ExecState* exec, EncodedJSValue encodedOp1, EncodedJSValue encodedOp2)
937 {
938     VM* vm = &exec->vm();
939     NativeCallFrameTracer tracer(vm, exec);
940
941     return JSValue::equalSlowCaseInline(exec, JSValue::decode(encodedOp1), JSValue::decode(encodedOp2));
942 }
943
944 #if USE(JSVALUE64)
945 EncodedJSValue JIT_OPERATION operationCompareStringEq(ExecState* exec, JSCell* left, JSCell* right)
946 #else
947 size_t JIT_OPERATION operationCompareStringEq(ExecState* exec, JSCell* left, JSCell* right)
948 #endif
949 {
950     VM* vm = &exec->vm();
951     NativeCallFrameTracer tracer(vm, exec);
952
953     bool result = WTF::equal(*asString(left)->value(exec).impl(), *asString(right)->value(exec).impl());
954 #if USE(JSVALUE64)
955     return JSValue::encode(jsBoolean(result));
956 #else
957     return result;
958 #endif
959 }
960
961 size_t JIT_OPERATION operationHasProperty(ExecState* exec, JSObject* base, JSString* property)
962 {
963     int result = base->hasProperty(exec, property->toIdentifier(exec));
964     return result;
965 }
966     
967
968 EncodedJSValue JIT_OPERATION operationNewArrayWithProfile(ExecState* exec, ArrayAllocationProfile* profile, const JSValue* values, int size)
969 {
970     VM* vm = &exec->vm();
971     NativeCallFrameTracer tracer(vm, exec);
972     return JSValue::encode(constructArrayNegativeIndexed(exec, profile, values, size));
973 }
974
975 EncodedJSValue JIT_OPERATION operationNewArrayBufferWithProfile(ExecState* exec, ArrayAllocationProfile* profile, const JSValue* values, int size)
976 {
977     VM* vm = &exec->vm();
978     NativeCallFrameTracer tracer(vm, exec);
979     return JSValue::encode(constructArray(exec, profile, values, size));
980 }
981
982 EncodedJSValue JIT_OPERATION operationNewArrayWithSizeAndProfile(ExecState* exec, ArrayAllocationProfile* profile, EncodedJSValue size)
983 {
984     VM* vm = &exec->vm();
985     NativeCallFrameTracer tracer(vm, exec);
986     JSValue sizeValue = JSValue::decode(size);
987     return JSValue::encode(constructArrayWithSizeQuirk(exec, profile, exec->lexicalGlobalObject(), sizeValue));
988 }
989
990 }
991
992 template<typename FunctionType>
993 static EncodedJSValue operationNewFunctionCommon(ExecState* exec, JSScope* scope, JSCell* functionExecutable, bool isInvalidated)
994 {
995     ASSERT(functionExecutable->inherits(FunctionExecutable::info()));
996     VM& vm = exec->vm();
997     NativeCallFrameTracer tracer(&vm, exec);
998     if (isInvalidated)
999         return JSValue::encode(FunctionType::createWithInvalidatedReallocationWatchpoint(vm, static_cast<FunctionExecutable*>(functionExecutable), scope));
1000     return JSValue::encode(FunctionType::create(vm, static_cast<FunctionExecutable*>(functionExecutable), scope));
1001 }
1002
1003 extern "C" {
1004
1005 EncodedJSValue JIT_OPERATION operationNewFunction(ExecState* exec, JSScope* scope, JSCell* functionExecutable)
1006 {
1007     return operationNewFunctionCommon<JSFunction>(exec, scope, functionExecutable, false);
1008 }
1009
1010 EncodedJSValue JIT_OPERATION operationNewFunctionWithInvalidatedReallocationWatchpoint(ExecState* exec, JSScope* scope, JSCell* functionExecutable)
1011 {
1012     return operationNewFunctionCommon<JSFunction>(exec, scope, functionExecutable, true);
1013 }
1014
1015 EncodedJSValue JIT_OPERATION operationNewGeneratorFunction(ExecState* exec, JSScope* scope, JSCell* functionExecutable)
1016 {
1017     return operationNewFunctionCommon<JSGeneratorFunction>(exec, scope, functionExecutable, false);
1018 }
1019
1020 EncodedJSValue JIT_OPERATION operationNewGeneratorFunctionWithInvalidatedReallocationWatchpoint(ExecState* exec, JSScope* scope, JSCell* functionExecutable)
1021 {
1022     return operationNewFunctionCommon<JSGeneratorFunction>(exec, scope, functionExecutable, true);
1023 }
1024
1025 JSCell* JIT_OPERATION operationNewObject(ExecState* exec, Structure* structure)
1026 {
1027     VM* vm = &exec->vm();
1028     NativeCallFrameTracer tracer(vm, exec);
1029
1030     return constructEmptyObject(exec, structure);
1031 }
1032
1033 EncodedJSValue JIT_OPERATION operationNewRegexp(ExecState* exec, void* regexpPtr)
1034 {
1035     VM& vm = exec->vm();
1036     NativeCallFrameTracer tracer(&vm, exec);
1037     RegExp* regexp = static_cast<RegExp*>(regexpPtr);
1038     if (!regexp->isValid()) {
1039         vm.throwException(exec, createSyntaxError(exec, ASCIILiteral("Invalid flags supplied to RegExp constructor.")));
1040         return JSValue::encode(jsUndefined());
1041     }
1042
1043     return JSValue::encode(RegExpObject::create(vm, exec->lexicalGlobalObject()->regExpStructure(), regexp));
1044 }
1045
1046 // The only reason for returning an UnusedPtr (instead of void) is so that we can reuse the
1047 // existing DFG slow path generator machinery when creating the slow path for CheckWatchdogTimer
1048 // in the DFG. If a DFG slow path generator that supports a void return type is added in the
1049 // future, we can switch to using that then.
1050 UnusedPtr JIT_OPERATION operationHandleWatchdogTimer(ExecState* exec)
1051 {
1052     VM& vm = exec->vm();
1053     NativeCallFrameTracer tracer(&vm, exec);
1054
1055     if (UNLIKELY(vm.shouldTriggerTermination(exec)))
1056         vm.throwException(exec, createTerminatedExecutionException(&vm));
1057
1058     return nullptr;
1059 }
1060
1061 void JIT_OPERATION operationThrowStaticError(ExecState* exec, EncodedJSValue encodedValue, int32_t referenceErrorFlag)
1062 {
1063     VM& vm = exec->vm();
1064     NativeCallFrameTracer tracer(&vm, exec);
1065     JSValue errorMessageValue = JSValue::decode(encodedValue);
1066     RELEASE_ASSERT(errorMessageValue.isString());
1067     String errorMessage = asString(errorMessageValue)->value(exec);
1068     if (referenceErrorFlag)
1069         vm.throwException(exec, createReferenceError(exec, errorMessage));
1070     else
1071         vm.throwException(exec, createTypeError(exec, errorMessage));
1072 }
1073
1074 void JIT_OPERATION operationDebug(ExecState* exec, int32_t debugHookID)
1075 {
1076     VM& vm = exec->vm();
1077     NativeCallFrameTracer tracer(&vm, exec);
1078
1079     vm.interpreter->debug(exec, static_cast<DebugHookID>(debugHookID));
1080 }
1081
1082 #if ENABLE(DFG_JIT)
1083 static void updateAllPredictionsAndOptimizeAfterWarmUp(CodeBlock* codeBlock)
1084 {
1085     codeBlock->updateAllPredictions();
1086     codeBlock->optimizeAfterWarmUp();
1087 }
1088
1089 SlowPathReturnType JIT_OPERATION operationOptimize(ExecState* exec, int32_t bytecodeIndex)
1090 {
1091     VM& vm = exec->vm();
1092     NativeCallFrameTracer tracer(&vm, exec);
1093
1094     // Defer GC for a while so that it doesn't run between when we enter into this
1095     // slow path and when we figure out the state of our code block. This prevents
1096     // a number of awkward reentrancy scenarios, including:
1097     //
1098     // - The optimized version of our code block being jettisoned by GC right after
1099     //   we concluded that we wanted to use it, but have not planted it into the JS
1100     //   stack yet.
1101     //
1102     // - An optimized version of our code block being installed just as we decided
1103     //   that it wasn't ready yet.
1104     //
1105     // Note that jettisoning won't happen if we already initiated OSR, because in
1106     // that case we would have already planted the optimized code block into the JS
1107     // stack.
1108     DeferGCForAWhile deferGC(vm.heap);
1109     
1110     CodeBlock* codeBlock = exec->codeBlock();
1111     if (codeBlock->jitType() != JITCode::BaselineJIT) {
1112         dataLog("Unexpected code block in Baseline->DFG tier-up: ", *codeBlock, "\n");
1113         RELEASE_ASSERT_NOT_REACHED();
1114     }
1115     
1116     if (bytecodeIndex) {
1117         // If we're attempting to OSR from a loop, assume that this should be
1118         // separately optimized.
1119         codeBlock->m_shouldAlwaysBeInlined = false;
1120     }
1121
1122     if (Options::verboseOSR()) {
1123         dataLog(
1124             *codeBlock, ": Entered optimize with bytecodeIndex = ", bytecodeIndex,
1125             ", executeCounter = ", codeBlock->jitExecuteCounter(),
1126             ", optimizationDelayCounter = ", codeBlock->reoptimizationRetryCounter(),
1127             ", exitCounter = ");
1128         if (codeBlock->hasOptimizedReplacement())
1129             dataLog(codeBlock->replacement()->osrExitCounter());
1130         else
1131             dataLog("N/A");
1132         dataLog("\n");
1133     }
1134
1135     if (!codeBlock->checkIfOptimizationThresholdReached()) {
1136         codeBlock->updateAllPredictions();
1137         if (Options::verboseOSR())
1138             dataLog("Choosing not to optimize ", *codeBlock, " yet, because the threshold hasn't been reached.\n");
1139         return encodeResult(0, 0);
1140     }
1141     
1142     if (vm.enabledProfiler()) {
1143         updateAllPredictionsAndOptimizeAfterWarmUp(codeBlock);
1144         return encodeResult(0, 0);
1145     }
1146
1147     Debugger* debugger = codeBlock->globalObject()->debugger();
1148     if (debugger && (debugger->isStepping() || codeBlock->baselineAlternative()->hasDebuggerRequests())) {
1149         updateAllPredictionsAndOptimizeAfterWarmUp(codeBlock);
1150         return encodeResult(0, 0);
1151     }
1152
1153     if (codeBlock->m_shouldAlwaysBeInlined) {
1154         updateAllPredictionsAndOptimizeAfterWarmUp(codeBlock);
1155         if (Options::verboseOSR())
1156             dataLog("Choosing not to optimize ", *codeBlock, " yet, because m_shouldAlwaysBeInlined == true.\n");
1157         return encodeResult(0, 0);
1158     }
1159
1160     // We cannot be in the process of asynchronous compilation and also have an optimized
1161     // replacement.
1162     DFG::Worklist* worklist = DFG::existingGlobalDFGWorklistOrNull();
1163     ASSERT(
1164         !worklist
1165         || !(worklist->compilationState(DFG::CompilationKey(codeBlock, DFG::DFGMode)) != DFG::Worklist::NotKnown
1166         && codeBlock->hasOptimizedReplacement()));
1167
1168     DFG::Worklist::State worklistState;
1169     if (worklist) {
1170         // The call to DFG::Worklist::completeAllReadyPlansForVM() will complete all ready
1171         // (i.e. compiled) code blocks. But if it completes ours, we also need to know
1172         // what the result was so that we don't plow ahead and attempt OSR or immediate
1173         // reoptimization. This will have already also set the appropriate JIT execution
1174         // count threshold depending on what happened, so if the compilation was anything
1175         // but successful we just want to return early. See the case for worklistState ==
1176         // DFG::Worklist::Compiled, below.
1177         
1178         // Note that we could have alternatively just called Worklist::compilationState()
1179         // here, and if it returned Compiled, we could have then called
1180         // completeAndScheduleOSR() below. But that would have meant that it could take
1181         // longer for code blocks to be completed: they would only complete when *their*
1182         // execution count trigger fired; but that could take a while since the firing is
1183         // racy. It could also mean that code blocks that never run again after being
1184         // compiled would sit on the worklist until next GC. That's fine, but it's
1185         // probably a waste of memory. Our goal here is to complete code blocks as soon as
1186         // possible in order to minimize the chances of us executing baseline code after
1187         // optimized code is already available.
1188         worklistState = worklist->completeAllReadyPlansForVM(
1189             vm, DFG::CompilationKey(codeBlock, DFG::DFGMode));
1190     } else
1191         worklistState = DFG::Worklist::NotKnown;
1192
1193     if (worklistState == DFG::Worklist::Compiling) {
1194         // We cannot be in the process of asynchronous compilation and also have an optimized
1195         // replacement.
1196         RELEASE_ASSERT(!codeBlock->hasOptimizedReplacement());
1197         codeBlock->setOptimizationThresholdBasedOnCompilationResult(CompilationDeferred);
1198         return encodeResult(0, 0);
1199     }
1200
1201     if (worklistState == DFG::Worklist::Compiled) {
1202         // If we don't have an optimized replacement but we did just get compiled, then
1203         // the compilation failed or was invalidated, in which case the execution count
1204         // thresholds have already been set appropriately by
1205         // CodeBlock::setOptimizationThresholdBasedOnCompilationResult() and we have
1206         // nothing left to do.
1207         if (!codeBlock->hasOptimizedReplacement()) {
1208             codeBlock->updateAllPredictions();
1209             if (Options::verboseOSR())
1210                 dataLog("Code block ", *codeBlock, " was compiled but it doesn't have an optimized replacement.\n");
1211             return encodeResult(0, 0);
1212         }
1213     } else if (codeBlock->hasOptimizedReplacement()) {
1214         if (Options::verboseOSR())
1215             dataLog("Considering OSR ", *codeBlock, " -> ", *codeBlock->replacement(), ".\n");
1216         // If we have an optimized replacement, then it must be the case that we entered
1217         // cti_optimize from a loop. That's because if there's an optimized replacement,
1218         // then all calls to this function will be relinked to the replacement and so
1219         // the prologue OSR will never fire.
1220         
1221         // This is an interesting threshold check. Consider that a function OSR exits
1222         // in the middle of a loop, while having a relatively low exit count. The exit
1223         // will reset the execution counter to some target threshold, meaning that this
1224         // code won't be reached until that loop heats up for >=1000 executions. But then
1225         // we do a second check here, to see if we should either reoptimize, or just
1226         // attempt OSR entry. Hence it might even be correct for
1227         // shouldReoptimizeFromLoopNow() to always return true. But we make it do some
1228         // additional checking anyway, to reduce the amount of recompilation thrashing.
1229         if (codeBlock->replacement()->shouldReoptimizeFromLoopNow()) {
1230             if (Options::verboseOSR()) {
1231                 dataLog(
1232                     "Triggering reoptimization of ", *codeBlock,
1233                     "(", *codeBlock->replacement(), ") (in loop).\n");
1234             }
1235             codeBlock->replacement()->jettison(Profiler::JettisonDueToBaselineLoopReoptimizationTrigger, CountReoptimization);
1236             return encodeResult(0, 0);
1237         }
1238     } else {
1239         if (!codeBlock->shouldOptimizeNow()) {
1240             if (Options::verboseOSR()) {
1241                 dataLog(
1242                     "Delaying optimization for ", *codeBlock,
1243                     " because of insufficient profiling.\n");
1244             }
1245             return encodeResult(0, 0);
1246         }
1247
1248         if (Options::verboseOSR())
1249             dataLog("Triggering optimized compilation of ", *codeBlock, "\n");
1250
1251         unsigned numVarsWithValues;
1252         if (bytecodeIndex)
1253             numVarsWithValues = codeBlock->m_numVars;
1254         else
1255             numVarsWithValues = 0;
1256         Operands<JSValue> mustHandleValues(codeBlock->numParameters(), numVarsWithValues);
1257         int localsUsedForCalleeSaves = static_cast<int>(CodeBlock::llintBaselineCalleeSaveSpaceAsVirtualRegisters());
1258         for (size_t i = 0; i < mustHandleValues.size(); ++i) {
1259             int operand = mustHandleValues.operandForIndex(i);
1260             if (operandIsLocal(operand) && VirtualRegister(operand).toLocal() < localsUsedForCalleeSaves)
1261                 continue;
1262             mustHandleValues[i] = exec->uncheckedR(operand).jsValue();
1263         }
1264
1265         CodeBlock* replacementCodeBlock = codeBlock->newReplacement();
1266         CompilationResult result = DFG::compile(
1267             vm, replacementCodeBlock, nullptr, DFG::DFGMode, bytecodeIndex,
1268             mustHandleValues, JITToDFGDeferredCompilationCallback::create());
1269         
1270         if (result != CompilationSuccessful)
1271             return encodeResult(0, 0);
1272     }
1273     
1274     CodeBlock* optimizedCodeBlock = codeBlock->replacement();
1275     ASSERT(JITCode::isOptimizingJIT(optimizedCodeBlock->jitType()));
1276     
1277     if (void* dataBuffer = DFG::prepareOSREntry(exec, optimizedCodeBlock, bytecodeIndex)) {
1278         if (Options::verboseOSR()) {
1279             dataLog(
1280                 "Performing OSR ", *codeBlock, " -> ", *optimizedCodeBlock, ".\n");
1281         }
1282
1283         codeBlock->optimizeSoon();
1284         return encodeResult(vm.getCTIStub(DFG::osrEntryThunkGenerator).code().executableAddress(), dataBuffer);
1285     }
1286
1287     if (Options::verboseOSR()) {
1288         dataLog(
1289             "Optimizing ", *codeBlock, " -> ", *codeBlock->replacement(),
1290             " succeeded, OSR failed, after a delay of ",
1291             codeBlock->optimizationDelayCounter(), ".\n");
1292     }
1293
1294     // Count the OSR failure as a speculation failure. If this happens a lot, then
1295     // reoptimize.
1296     optimizedCodeBlock->countOSRExit();
1297
1298     // We are a lot more conservative about triggering reoptimization after OSR failure than
1299     // before it. If we enter the optimize_from_loop trigger with a bucket full of fail
1300     // already, then we really would like to reoptimize immediately. But this case covers
1301     // something else: there weren't many (or any) speculation failures before, but we just
1302     // failed to enter the speculative code because some variable had the wrong value or
1303     // because the OSR code decided for any spurious reason that it did not want to OSR
1304     // right now. So, we only trigger reoptimization only upon the more conservative (non-loop)
1305     // reoptimization trigger.
1306     if (optimizedCodeBlock->shouldReoptimizeNow()) {
1307         if (Options::verboseOSR()) {
1308             dataLog(
1309                 "Triggering reoptimization of ", *codeBlock, " -> ",
1310                 *codeBlock->replacement(), " (after OSR fail).\n");
1311         }
1312         optimizedCodeBlock->jettison(Profiler::JettisonDueToBaselineLoopReoptimizationTriggerOnOSREntryFail, CountReoptimization);
1313         return encodeResult(0, 0);
1314     }
1315
1316     // OSR failed this time, but it might succeed next time! Let the code run a bit
1317     // longer and then try again.
1318     codeBlock->optimizeAfterWarmUp();
1319     
1320     return encodeResult(0, 0);
1321 }
1322 #endif
1323
1324 void JIT_OPERATION operationPutByIndex(ExecState* exec, EncodedJSValue encodedArrayValue, int32_t index, EncodedJSValue encodedValue)
1325 {
1326     VM& vm = exec->vm();
1327     NativeCallFrameTracer tracer(&vm, exec);
1328
1329     JSValue arrayValue = JSValue::decode(encodedArrayValue);
1330     ASSERT(isJSArray(arrayValue));
1331     asArray(arrayValue)->putDirectIndex(exec, index, JSValue::decode(encodedValue));
1332 }
1333
1334 enum class AccessorType {
1335     Getter,
1336     Setter
1337 };
1338
1339 static void putAccessorByVal(ExecState* exec, JSObject* base, JSValue subscript, int32_t attribute, JSObject* accessor, AccessorType accessorType)
1340 {
1341     auto propertyKey = subscript.toPropertyKey(exec);
1342     if (exec->hadException())
1343         return;
1344
1345     if (accessorType == AccessorType::Getter)
1346         base->putGetter(exec, propertyKey, accessor, attribute);
1347     else
1348         base->putSetter(exec, propertyKey, accessor, attribute);
1349 }
1350
1351 void JIT_OPERATION operationPutGetterById(ExecState* exec, JSCell* object, UniquedStringImpl* uid, int32_t options, JSCell* getter)
1352 {
1353     VM& vm = exec->vm();
1354     NativeCallFrameTracer tracer(&vm, exec);
1355
1356     ASSERT(object && object->isObject());
1357     JSObject* baseObj = object->getObject();
1358
1359     ASSERT(getter->isObject());
1360     baseObj->putGetter(exec, uid, getter, options);
1361 }
1362
1363 void JIT_OPERATION operationPutSetterById(ExecState* exec, JSCell* object, UniquedStringImpl* uid, int32_t options, JSCell* setter)
1364 {
1365     VM& vm = exec->vm();
1366     NativeCallFrameTracer tracer(&vm, exec);
1367
1368     ASSERT(object && object->isObject());
1369     JSObject* baseObj = object->getObject();
1370
1371     ASSERT(setter->isObject());
1372     baseObj->putSetter(exec, uid, setter, options);
1373 }
1374
1375 void JIT_OPERATION operationPutGetterByVal(ExecState* exec, JSCell* base, EncodedJSValue encodedSubscript, int32_t attribute, JSCell* getter)
1376 {
1377     VM& vm = exec->vm();
1378     NativeCallFrameTracer tracer(&vm, exec);
1379
1380     putAccessorByVal(exec, asObject(base), JSValue::decode(encodedSubscript), attribute, asObject(getter), AccessorType::Getter);
1381 }
1382
1383 void JIT_OPERATION operationPutSetterByVal(ExecState* exec, JSCell* base, EncodedJSValue encodedSubscript, int32_t attribute, JSCell* setter)
1384 {
1385     VM& vm = exec->vm();
1386     NativeCallFrameTracer tracer(&vm, exec);
1387
1388     putAccessorByVal(exec, asObject(base), JSValue::decode(encodedSubscript), attribute, asObject(setter), AccessorType::Setter);
1389 }
1390
1391 #if USE(JSVALUE64)
1392 void JIT_OPERATION operationPutGetterSetter(ExecState* exec, JSCell* object, UniquedStringImpl* uid, int32_t attribute, EncodedJSValue encodedGetterValue, EncodedJSValue encodedSetterValue)
1393 {
1394     VM& vm = exec->vm();
1395     NativeCallFrameTracer tracer(&vm, exec);
1396
1397     ASSERT(object && object->isObject());
1398     JSObject* baseObj = asObject(object);
1399
1400     GetterSetter* accessor = GetterSetter::create(vm, exec->lexicalGlobalObject());
1401
1402     JSValue getter = JSValue::decode(encodedGetterValue);
1403     JSValue setter = JSValue::decode(encodedSetterValue);
1404     ASSERT(getter.isObject() || getter.isUndefined());
1405     ASSERT(setter.isObject() || setter.isUndefined());
1406     ASSERT(getter.isObject() || setter.isObject());
1407
1408     if (!getter.isUndefined())
1409         accessor->setGetter(vm, exec->lexicalGlobalObject(), asObject(getter));
1410     if (!setter.isUndefined())
1411         accessor->setSetter(vm, exec->lexicalGlobalObject(), asObject(setter));
1412     baseObj->putDirectAccessor(exec, uid, accessor, attribute);
1413 }
1414
1415 #else
1416 void JIT_OPERATION operationPutGetterSetter(ExecState* exec, JSCell* object, UniquedStringImpl* uid, int32_t attribute, JSCell* getter, JSCell* setter)
1417 {
1418     VM& vm = exec->vm();
1419     NativeCallFrameTracer tracer(&vm, exec);
1420
1421     ASSERT(object && object->isObject());
1422     JSObject* baseObj = asObject(object);
1423
1424     GetterSetter* accessor = GetterSetter::create(vm, exec->lexicalGlobalObject());
1425
1426     ASSERT(!getter || getter->isObject());
1427     ASSERT(!setter || setter->isObject());
1428     ASSERT(getter || setter);
1429
1430     if (getter)
1431         accessor->setGetter(vm, exec->lexicalGlobalObject(), getter->getObject());
1432     if (setter)
1433         accessor->setSetter(vm, exec->lexicalGlobalObject(), setter->getObject());
1434     baseObj->putDirectAccessor(exec, uid, accessor, attribute);
1435 }
1436 #endif
1437
1438 void JIT_OPERATION operationPopScope(ExecState* exec, int32_t scopeReg)
1439 {
1440     VM& vm = exec->vm();
1441     NativeCallFrameTracer tracer(&vm, exec);
1442
1443     JSScope* scope = exec->uncheckedR(scopeReg).Register::scope();
1444     exec->uncheckedR(scopeReg) = scope->next();
1445 }
1446
1447 void JIT_OPERATION operationProfileDidCall(ExecState* exec, EncodedJSValue encodedValue)
1448 {
1449     VM& vm = exec->vm();
1450     NativeCallFrameTracer tracer(&vm, exec);
1451
1452     if (LegacyProfiler* profiler = vm.enabledProfiler())
1453         profiler->didExecute(exec, JSValue::decode(encodedValue));
1454 }
1455
1456 void JIT_OPERATION operationProfileWillCall(ExecState* exec, EncodedJSValue encodedValue)
1457 {
1458     VM& vm = exec->vm();
1459     NativeCallFrameTracer tracer(&vm, exec);
1460
1461     if (LegacyProfiler* profiler = vm.enabledProfiler())
1462         profiler->willExecute(exec, JSValue::decode(encodedValue));
1463 }
1464
1465 int32_t JIT_OPERATION operationInstanceOfCustom(ExecState* exec, EncodedJSValue encodedValue, JSObject* constructor, EncodedJSValue encodedHasInstance)
1466 {
1467     VM& vm = exec->vm();
1468     NativeCallFrameTracer tracer(&vm, exec);
1469
1470     JSValue value = JSValue::decode(encodedValue);
1471     JSValue hasInstanceValue = JSValue::decode(encodedHasInstance);
1472
1473     ASSERT(hasInstanceValue != exec->lexicalGlobalObject()->functionProtoHasInstanceSymbolFunction() || !constructor->structure()->typeInfo().implementsDefaultHasInstance());
1474
1475     if (constructor->hasInstance(exec, value, hasInstanceValue))
1476         return 1;
1477     return 0;
1478 }
1479
1480 }
1481
1482 static bool canAccessArgumentIndexQuickly(JSObject& object, uint32_t index)
1483 {
1484     switch (object.structure()->typeInfo().type()) {
1485     case DirectArgumentsType: {
1486         DirectArguments* directArguments = jsCast<DirectArguments*>(&object);
1487         if (directArguments->canAccessArgumentIndexQuicklyInDFG(index))
1488             return true;
1489         break;
1490     }
1491     case ScopedArgumentsType: {
1492         ScopedArguments* scopedArguments = jsCast<ScopedArguments*>(&object);
1493         if (scopedArguments->canAccessArgumentIndexQuicklyInDFG(index))
1494             return true;
1495         break;
1496     }
1497     default:
1498         break;
1499     }
1500     return false;
1501 }
1502
1503 static JSValue getByVal(ExecState* exec, JSValue baseValue, JSValue subscript, ByValInfo* byValInfo, ReturnAddressPtr returnAddress)
1504 {
1505     if (LIKELY(baseValue.isCell() && subscript.isString())) {
1506         VM& vm = exec->vm();
1507         Structure& structure = *baseValue.asCell()->structure(vm);
1508         if (JSCell::canUseFastGetOwnProperty(structure)) {
1509             if (RefPtr<AtomicStringImpl> existingAtomicString = asString(subscript)->toExistingAtomicString(exec)) {
1510                 if (JSValue result = baseValue.asCell()->fastGetOwnProperty(vm, structure, existingAtomicString.get())) {
1511                     ASSERT(exec->bytecodeOffset());
1512                     if (byValInfo->stubInfo && byValInfo->cachedId.impl() != existingAtomicString)
1513                         byValInfo->tookSlowPath = true;
1514                     return result;
1515                 }
1516             }
1517         }
1518     }
1519
1520     if (subscript.isUInt32()) {
1521         ASSERT(exec->bytecodeOffset());
1522         byValInfo->tookSlowPath = true;
1523
1524         uint32_t i = subscript.asUInt32();
1525         if (isJSString(baseValue)) {
1526             if (asString(baseValue)->canGetIndex(i)) {
1527                 ctiPatchCallByReturnAddress(returnAddress, FunctionPtr(operationGetByValString));
1528                 return asString(baseValue)->getIndex(exec, i);
1529             }
1530             byValInfo->arrayProfile->setOutOfBounds();
1531         } else if (baseValue.isObject()) {
1532             JSObject* object = asObject(baseValue);
1533             if (object->canGetIndexQuickly(i))
1534                 return object->getIndexQuickly(i);
1535
1536             if (!canAccessArgumentIndexQuickly(*object, i)) {
1537                 // FIXME: This will make us think that in-bounds typed array accesses are actually
1538                 // out-of-bounds.
1539                 // https://bugs.webkit.org/show_bug.cgi?id=149886
1540                 byValInfo->arrayProfile->setOutOfBounds();
1541             }
1542         }
1543
1544         return baseValue.get(exec, i);
1545     }
1546
1547     baseValue.requireObjectCoercible(exec);
1548     if (exec->hadException())
1549         return jsUndefined();
1550     auto property = subscript.toPropertyKey(exec);
1551     if (exec->hadException())
1552         return jsUndefined();
1553
1554     ASSERT(exec->bytecodeOffset());
1555     if (byValInfo->stubInfo && (!isStringOrSymbol(subscript) || byValInfo->cachedId != property))
1556         byValInfo->tookSlowPath = true;
1557
1558     return baseValue.get(exec, property);
1559 }
1560
1561 static OptimizationResult tryGetByValOptimize(ExecState* exec, JSValue baseValue, JSValue subscript, ByValInfo* byValInfo, ReturnAddressPtr returnAddress)
1562 {
1563     // See if it's worth optimizing this at all.
1564     OptimizationResult optimizationResult = OptimizationResult::NotOptimized;
1565
1566     VM& vm = exec->vm();
1567
1568     if (baseValue.isObject() && subscript.isInt32()) {
1569         JSObject* object = asObject(baseValue);
1570
1571         ASSERT(exec->bytecodeOffset());
1572         ASSERT(!byValInfo->stubRoutine);
1573
1574         if (hasOptimizableIndexing(object->structure(vm))) {
1575             // Attempt to optimize.
1576             Structure* structure = object->structure(vm);
1577             JITArrayMode arrayMode = jitArrayModeForStructure(structure);
1578             if (arrayMode != byValInfo->arrayMode) {
1579                 // If we reached this case, we got an interesting array mode we did not expect when we compiled.
1580                 // Let's update the profile to do better next time.
1581                 CodeBlock* codeBlock = exec->codeBlock();
1582                 ConcurrentJITLocker locker(codeBlock->m_lock);
1583                 byValInfo->arrayProfile->computeUpdatedPrediction(locker, codeBlock, structure);
1584
1585                 JIT::compileGetByVal(&vm, exec->codeBlock(), byValInfo, returnAddress, arrayMode);
1586                 optimizationResult = OptimizationResult::Optimized;
1587             }
1588         }
1589
1590         // If we failed to patch and we have some object that intercepts indexed get, then don't even wait until 10 times.
1591         if (optimizationResult != OptimizationResult::Optimized && object->structure(vm)->typeInfo().interceptsGetOwnPropertySlotByIndexEvenWhenLengthIsNotZero())
1592             optimizationResult = OptimizationResult::GiveUp;
1593     }
1594
1595     if (baseValue.isObject() && isStringOrSymbol(subscript)) {
1596         const Identifier propertyName = subscript.toPropertyKey(exec);
1597         if (!subscript.isString() || !parseIndex(propertyName)) {
1598             ASSERT(exec->bytecodeOffset());
1599             ASSERT(!byValInfo->stubRoutine);
1600             if (byValInfo->seen) {
1601                 if (byValInfo->cachedId == propertyName) {
1602                     JIT::compileGetByValWithCachedId(&vm, exec->codeBlock(), byValInfo, returnAddress, propertyName);
1603                     optimizationResult = OptimizationResult::Optimized;
1604                 } else {
1605                     // Seem like a generic property access site.
1606                     optimizationResult = OptimizationResult::GiveUp;
1607                 }
1608             } else {
1609                 byValInfo->seen = true;
1610                 byValInfo->cachedId = propertyName;
1611                 optimizationResult = OptimizationResult::SeenOnce;
1612             }
1613
1614         }
1615     }
1616
1617     if (optimizationResult != OptimizationResult::Optimized && optimizationResult != OptimizationResult::SeenOnce) {
1618         // If we take slow path more than 10 times without patching then make sure we
1619         // never make that mistake again. For cases where we see non-index-intercepting
1620         // objects, this gives 10 iterations worth of opportunity for us to observe
1621         // that the get_by_val may be polymorphic. We count up slowPathCount even if
1622         // the result is GiveUp.
1623         if (++byValInfo->slowPathCount >= 10)
1624             optimizationResult = OptimizationResult::GiveUp;
1625     }
1626
1627     return optimizationResult;
1628 }
1629
1630 extern "C" {
1631
1632 EncodedJSValue JIT_OPERATION operationGetByValGeneric(ExecState* exec, EncodedJSValue encodedBase, EncodedJSValue encodedSubscript, ByValInfo* byValInfo)
1633 {
1634     VM& vm = exec->vm();
1635     NativeCallFrameTracer tracer(&vm, exec);
1636     JSValue baseValue = JSValue::decode(encodedBase);
1637     JSValue subscript = JSValue::decode(encodedSubscript);
1638
1639     JSValue result = getByVal(exec, baseValue, subscript, byValInfo, ReturnAddressPtr(OUR_RETURN_ADDRESS));
1640     return JSValue::encode(result);
1641 }
1642
1643 EncodedJSValue JIT_OPERATION operationGetByValOptimize(ExecState* exec, EncodedJSValue encodedBase, EncodedJSValue encodedSubscript, ByValInfo* byValInfo)
1644 {
1645     VM& vm = exec->vm();
1646     NativeCallFrameTracer tracer(&vm, exec);
1647
1648     JSValue baseValue = JSValue::decode(encodedBase);
1649     JSValue subscript = JSValue::decode(encodedSubscript);
1650     ReturnAddressPtr returnAddress = ReturnAddressPtr(OUR_RETURN_ADDRESS);
1651     if (tryGetByValOptimize(exec, baseValue, subscript, byValInfo, returnAddress) == OptimizationResult::GiveUp) {
1652         // Don't ever try to optimize.
1653         byValInfo->tookSlowPath = true;
1654         ctiPatchCallByReturnAddress(returnAddress, FunctionPtr(operationGetByValGeneric));
1655     }
1656
1657     return JSValue::encode(getByVal(exec, baseValue, subscript, byValInfo, returnAddress));
1658 }
1659
1660 EncodedJSValue JIT_OPERATION operationHasIndexedPropertyDefault(ExecState* exec, EncodedJSValue encodedBase, EncodedJSValue encodedSubscript, ByValInfo* byValInfo)
1661 {
1662     VM& vm = exec->vm();
1663     NativeCallFrameTracer tracer(&vm, exec);
1664     JSValue baseValue = JSValue::decode(encodedBase);
1665     JSValue subscript = JSValue::decode(encodedSubscript);
1666     
1667     ASSERT(baseValue.isObject());
1668     ASSERT(subscript.isUInt32());
1669
1670     JSObject* object = asObject(baseValue);
1671     bool didOptimize = false;
1672
1673     ASSERT(exec->bytecodeOffset());
1674     ASSERT(!byValInfo->stubRoutine);
1675     
1676     if (hasOptimizableIndexing(object->structure(vm))) {
1677         // Attempt to optimize.
1678         JITArrayMode arrayMode = jitArrayModeForStructure(object->structure(vm));
1679         if (arrayMode != byValInfo->arrayMode) {
1680             JIT::compileHasIndexedProperty(&vm, exec->codeBlock(), byValInfo, ReturnAddressPtr(OUR_RETURN_ADDRESS), arrayMode);
1681             didOptimize = true;
1682         }
1683     }
1684     
1685     if (!didOptimize) {
1686         // If we take slow path more than 10 times without patching then make sure we
1687         // never make that mistake again. Or, if we failed to patch and we have some object
1688         // that intercepts indexed get, then don't even wait until 10 times. For cases
1689         // where we see non-index-intercepting objects, this gives 10 iterations worth of
1690         // opportunity for us to observe that the get_by_val may be polymorphic.
1691         if (++byValInfo->slowPathCount >= 10
1692             || object->structure(vm)->typeInfo().interceptsGetOwnPropertySlotByIndexEvenWhenLengthIsNotZero()) {
1693             // Don't ever try to optimize.
1694             ctiPatchCallByReturnAddress(ReturnAddressPtr(OUR_RETURN_ADDRESS), FunctionPtr(operationHasIndexedPropertyGeneric));
1695         }
1696     }
1697
1698     uint32_t index = subscript.asUInt32();
1699     if (object->canGetIndexQuickly(index))
1700         return JSValue::encode(JSValue(JSValue::JSTrue));
1701
1702     if (!canAccessArgumentIndexQuickly(*object, index)) {
1703         // FIXME: This will make us think that in-bounds typed array accesses are actually
1704         // out-of-bounds.
1705         // https://bugs.webkit.org/show_bug.cgi?id=149886
1706         byValInfo->arrayProfile->setOutOfBounds();
1707     }
1708     return JSValue::encode(jsBoolean(object->hasProperty(exec, index)));
1709 }
1710     
1711 EncodedJSValue JIT_OPERATION operationHasIndexedPropertyGeneric(ExecState* exec, EncodedJSValue encodedBase, EncodedJSValue encodedSubscript, ByValInfo* byValInfo)
1712 {
1713     VM& vm = exec->vm();
1714     NativeCallFrameTracer tracer(&vm, exec);
1715     JSValue baseValue = JSValue::decode(encodedBase);
1716     JSValue subscript = JSValue::decode(encodedSubscript);
1717     
1718     ASSERT(baseValue.isObject());
1719     ASSERT(subscript.isUInt32());
1720
1721     JSObject* object = asObject(baseValue);
1722     uint32_t index = subscript.asUInt32();
1723     if (object->canGetIndexQuickly(index))
1724         return JSValue::encode(JSValue(JSValue::JSTrue));
1725
1726     if (!canAccessArgumentIndexQuickly(*object, index)) {
1727         // FIXME: This will make us think that in-bounds typed array accesses are actually
1728         // out-of-bounds.
1729         // https://bugs.webkit.org/show_bug.cgi?id=149886
1730         byValInfo->arrayProfile->setOutOfBounds();
1731     }
1732     return JSValue::encode(jsBoolean(object->hasProperty(exec, subscript.asUInt32())));
1733 }
1734     
1735 EncodedJSValue JIT_OPERATION operationGetByValString(ExecState* exec, EncodedJSValue encodedBase, EncodedJSValue encodedSubscript, ByValInfo* byValInfo)
1736 {
1737     VM& vm = exec->vm();
1738     NativeCallFrameTracer tracer(&vm, exec);
1739     JSValue baseValue = JSValue::decode(encodedBase);
1740     JSValue subscript = JSValue::decode(encodedSubscript);
1741     
1742     JSValue result;
1743     if (LIKELY(subscript.isUInt32())) {
1744         uint32_t i = subscript.asUInt32();
1745         if (isJSString(baseValue) && asString(baseValue)->canGetIndex(i))
1746             result = asString(baseValue)->getIndex(exec, i);
1747         else {
1748             result = baseValue.get(exec, i);
1749             if (!isJSString(baseValue)) {
1750                 ASSERT(exec->bytecodeOffset());
1751                 ctiPatchCallByReturnAddress(ReturnAddressPtr(OUR_RETURN_ADDRESS), FunctionPtr(byValInfo->stubRoutine ? operationGetByValGeneric : operationGetByValOptimize));
1752             }
1753         }
1754     } else {
1755         baseValue.requireObjectCoercible(exec);
1756         if (exec->hadException())
1757             return JSValue::encode(jsUndefined());
1758         auto property = subscript.toPropertyKey(exec);
1759         if (exec->hadException())
1760             return JSValue::encode(jsUndefined());
1761         result = baseValue.get(exec, property);
1762     }
1763
1764     return JSValue::encode(result);
1765 }
1766
1767 EncodedJSValue JIT_OPERATION operationDeleteById(ExecState* exec, EncodedJSValue encodedBase, const Identifier* identifier)
1768 {
1769     VM& vm = exec->vm();
1770     NativeCallFrameTracer tracer(&vm, exec);
1771
1772     JSObject* baseObj = JSValue::decode(encodedBase).toObject(exec);
1773     bool couldDelete = baseObj->methodTable(vm)->deleteProperty(baseObj, exec, *identifier);
1774     JSValue result = jsBoolean(couldDelete);
1775     if (!couldDelete && exec->codeBlock()->isStrictMode())
1776         vm.throwException(exec, createTypeError(exec, ASCIILiteral("Unable to delete property.")));
1777     return JSValue::encode(result);
1778 }
1779
1780 EncodedJSValue JIT_OPERATION operationInstanceOf(ExecState* exec, EncodedJSValue encodedValue, EncodedJSValue encodedProto)
1781 {
1782     VM& vm = exec->vm();
1783     NativeCallFrameTracer tracer(&vm, exec);
1784     JSValue value = JSValue::decode(encodedValue);
1785     JSValue proto = JSValue::decode(encodedProto);
1786     
1787     ASSERT(!value.isObject() || !proto.isObject());
1788
1789     bool result = JSObject::defaultHasInstance(exec, value, proto);
1790     return JSValue::encode(jsBoolean(result));
1791 }
1792
1793 int32_t JIT_OPERATION operationSizeFrameForVarargs(ExecState* exec, EncodedJSValue encodedArguments, int32_t numUsedStackSlots, int32_t firstVarArgOffset)
1794 {
1795     VM& vm = exec->vm();
1796     NativeCallFrameTracer tracer(&vm, exec);
1797     JSStack* stack = &exec->interpreter()->stack();
1798     JSValue arguments = JSValue::decode(encodedArguments);
1799     return sizeFrameForVarargs(exec, stack, arguments, numUsedStackSlots, firstVarArgOffset);
1800 }
1801
1802 CallFrame* JIT_OPERATION operationSetupVarargsFrame(ExecState* exec, CallFrame* newCallFrame, EncodedJSValue encodedArguments, int32_t firstVarArgOffset, int32_t length)
1803 {
1804     VM& vm = exec->vm();
1805     NativeCallFrameTracer tracer(&vm, exec);
1806     JSValue arguments = JSValue::decode(encodedArguments);
1807     setupVarargsFrame(exec, newCallFrame, arguments, firstVarArgOffset, length);
1808     return newCallFrame;
1809 }
1810
1811 EncodedJSValue JIT_OPERATION operationToObject(ExecState* exec, EncodedJSValue value)
1812 {
1813     VM& vm = exec->vm();
1814     NativeCallFrameTracer tracer(&vm, exec);
1815     return JSValue::encode(JSValue::decode(value).toObject(exec));
1816 }
1817
1818 char* JIT_OPERATION operationSwitchCharWithUnknownKeyType(ExecState* exec, EncodedJSValue encodedKey, size_t tableIndex)
1819 {
1820     VM& vm = exec->vm();
1821     NativeCallFrameTracer tracer(&vm, exec);
1822     JSValue key = JSValue::decode(encodedKey);
1823     CodeBlock* codeBlock = exec->codeBlock();
1824
1825     SimpleJumpTable& jumpTable = codeBlock->switchJumpTable(tableIndex);
1826     void* result = jumpTable.ctiDefault.executableAddress();
1827
1828     if (key.isString()) {
1829         StringImpl* value = asString(key)->value(exec).impl();
1830         if (value->length() == 1)
1831             result = jumpTable.ctiForValue((*value)[0]).executableAddress();
1832     }
1833
1834     return reinterpret_cast<char*>(result);
1835 }
1836
1837 char* JIT_OPERATION operationSwitchImmWithUnknownKeyType(ExecState* exec, EncodedJSValue encodedKey, size_t tableIndex)
1838 {
1839     VM& vm = exec->vm();
1840     NativeCallFrameTracer tracer(&vm, exec);
1841     JSValue key = JSValue::decode(encodedKey);
1842     CodeBlock* codeBlock = exec->codeBlock();
1843
1844     SimpleJumpTable& jumpTable = codeBlock->switchJumpTable(tableIndex);
1845     void* result;
1846     if (key.isInt32())
1847         result = jumpTable.ctiForValue(key.asInt32()).executableAddress();
1848     else if (key.isDouble() && key.asDouble() == static_cast<int32_t>(key.asDouble()))
1849         result = jumpTable.ctiForValue(static_cast<int32_t>(key.asDouble())).executableAddress();
1850     else
1851         result = jumpTable.ctiDefault.executableAddress();
1852     return reinterpret_cast<char*>(result);
1853 }
1854
1855 char* JIT_OPERATION operationSwitchStringWithUnknownKeyType(ExecState* exec, EncodedJSValue encodedKey, size_t tableIndex)
1856 {
1857     VM& vm = exec->vm();
1858     NativeCallFrameTracer tracer(&vm, exec);
1859     JSValue key = JSValue::decode(encodedKey);
1860     CodeBlock* codeBlock = exec->codeBlock();
1861
1862     void* result;
1863     StringJumpTable& jumpTable = codeBlock->stringSwitchJumpTable(tableIndex);
1864
1865     if (key.isString()) {
1866         StringImpl* value = asString(key)->value(exec).impl();
1867         result = jumpTable.ctiForValue(value).executableAddress();
1868     } else
1869         result = jumpTable.ctiDefault.executableAddress();
1870
1871     return reinterpret_cast<char*>(result);
1872 }
1873
1874 EncodedJSValue JIT_OPERATION operationGetFromScope(ExecState* exec, Instruction* bytecodePC)
1875 {
1876     VM& vm = exec->vm();
1877     NativeCallFrameTracer tracer(&vm, exec);
1878     CodeBlock* codeBlock = exec->codeBlock();
1879     Instruction* pc = bytecodePC;
1880
1881     const Identifier& ident = codeBlock->identifier(pc[3].u.operand);
1882     JSObject* scope = jsCast<JSObject*>(exec->uncheckedR(pc[2].u.operand).jsValue());
1883     GetPutInfo getPutInfo(pc[4].u.operand);
1884
1885     // ModuleVar is always converted to ClosureVar for get_from_scope.
1886     ASSERT(getPutInfo.resolveType() != ModuleVar);
1887
1888     PropertySlot slot(scope, PropertySlot::InternalMethodType::Get);
1889     if (!scope->getPropertySlot(exec, ident, slot)) {
1890         if (getPutInfo.resolveMode() == ThrowIfNotFound)
1891             vm.throwException(exec, createUndefinedVariableError(exec, ident));
1892         return JSValue::encode(jsUndefined());
1893     }
1894
1895     JSValue result = JSValue();
1896     if (jsDynamicCast<JSGlobalLexicalEnvironment*>(scope)) {
1897         // When we can't statically prove we need a TDZ check, we must perform the check on the slow path.
1898         result = slot.getValue(exec, ident);
1899         if (result == jsTDZValue()) {
1900             exec->vm().throwException(exec, createTDZError(exec));
1901             return JSValue::encode(jsUndefined());
1902         }
1903     }
1904
1905     CommonSlowPaths::tryCacheGetFromScopeGlobal(exec, vm, pc, scope, slot, ident);
1906
1907     if (!result)
1908         result = slot.getValue(exec, ident);
1909     return JSValue::encode(result);
1910 }
1911
1912 void JIT_OPERATION operationPutToScope(ExecState* exec, Instruction* bytecodePC)
1913 {
1914     VM& vm = exec->vm();
1915     NativeCallFrameTracer tracer(&vm, exec);
1916     Instruction* pc = bytecodePC;
1917
1918     CodeBlock* codeBlock = exec->codeBlock();
1919     const Identifier& ident = codeBlock->identifier(pc[2].u.operand);
1920     JSObject* scope = jsCast<JSObject*>(exec->uncheckedR(pc[1].u.operand).jsValue());
1921     JSValue value = exec->r(pc[3].u.operand).jsValue();
1922     GetPutInfo getPutInfo = GetPutInfo(pc[4].u.operand);
1923
1924     // ModuleVar does not keep the scope register value alive in DFG.
1925     ASSERT(getPutInfo.resolveType() != ModuleVar);
1926
1927     if (getPutInfo.resolveType() == LocalClosureVar) {
1928         JSLexicalEnvironment* environment = jsCast<JSLexicalEnvironment*>(scope);
1929         environment->variableAt(ScopeOffset(pc[6].u.operand)).set(vm, environment, value);
1930         if (WatchpointSet* set = pc[5].u.watchpointSet)
1931             set->touch("Executed op_put_scope<LocalClosureVar>");
1932         return;
1933     }
1934
1935     bool hasProperty = scope->hasProperty(exec, ident);
1936     if (hasProperty
1937         && jsDynamicCast<JSGlobalLexicalEnvironment*>(scope)
1938         && getPutInfo.initializationMode() != Initialization) {
1939         // When we can't statically prove we need a TDZ check, we must perform the check on the slow path.
1940         PropertySlot slot(scope, PropertySlot::InternalMethodType::Get);
1941         JSGlobalLexicalEnvironment::getOwnPropertySlot(scope, exec, ident, slot);
1942         if (slot.getValue(exec, ident) == jsTDZValue()) {
1943             exec->vm().throwException(exec, createTDZError(exec));
1944             return;
1945         }
1946     }
1947
1948     if (getPutInfo.resolveMode() == ThrowIfNotFound && !hasProperty) {
1949         exec->vm().throwException(exec, createUndefinedVariableError(exec, ident));
1950         return;
1951     }
1952
1953     PutPropertySlot slot(scope, codeBlock->isStrictMode(), PutPropertySlot::UnknownContext, getPutInfo.initializationMode() == Initialization);
1954     scope->methodTable()->put(scope, exec, ident, value, slot);
1955     
1956     if (exec->vm().exception())
1957         return;
1958
1959     CommonSlowPaths::tryCachePutToScopeGlobal(exec, codeBlock, pc, scope, getPutInfo, slot, ident);
1960 }
1961
1962 void JIT_OPERATION operationThrow(ExecState* exec, EncodedJSValue encodedExceptionValue)
1963 {
1964     VM* vm = &exec->vm();
1965     NativeCallFrameTracer tracer(vm, exec);
1966
1967     JSValue exceptionValue = JSValue::decode(encodedExceptionValue);
1968     vm->throwException(exec, exceptionValue);
1969
1970     // Results stored out-of-band in vm.targetMachinePCForThrow & vm.callFrameForCatch
1971     genericUnwind(vm, exec);
1972 }
1973
1974 void JIT_OPERATION operationFlushWriteBarrierBuffer(ExecState* exec, JSCell* cell)
1975 {
1976     VM* vm = &exec->vm();
1977     NativeCallFrameTracer tracer(vm, exec);
1978     vm->heap.flushWriteBarrierBuffer(cell);
1979 }
1980
1981 void JIT_OPERATION operationOSRWriteBarrier(ExecState* exec, JSCell* cell)
1982 {
1983     VM* vm = &exec->vm();
1984     NativeCallFrameTracer tracer(vm, exec);
1985     vm->heap.writeBarrier(cell);
1986 }
1987
1988 // NB: We don't include the value as part of the barrier because the write barrier elision
1989 // phase in the DFG only tracks whether the object being stored to has been barriered. It 
1990 // would be much more complicated to try to model the value being stored as well.
1991 void JIT_OPERATION operationUnconditionalWriteBarrier(ExecState* exec, JSCell* cell)
1992 {
1993     VM* vm = &exec->vm();
1994     NativeCallFrameTracer tracer(vm, exec);
1995     vm->heap.writeBarrier(cell);
1996 }
1997
1998 void JIT_OPERATION operationInitGlobalConst(ExecState* exec, Instruction* pc)
1999 {
2000     VM* vm = &exec->vm();
2001     NativeCallFrameTracer tracer(vm, exec);
2002
2003     JSValue value = exec->r(pc[2].u.operand).jsValue();
2004     pc[1].u.variablePointer->set(*vm, exec->codeBlock()->globalObject(), value);
2005 }
2006
2007 void JIT_OPERATION lookupExceptionHandler(VM* vm, ExecState* exec)
2008 {
2009     NativeCallFrameTracer tracer(vm, exec);
2010     genericUnwind(vm, exec);
2011     ASSERT(vm->targetMachinePCForThrow);
2012 }
2013
2014 void JIT_OPERATION lookupExceptionHandlerFromCallerFrame(VM* vm, ExecState* exec)
2015 {
2016     NativeCallFrameTracer tracer(vm, exec);
2017     genericUnwind(vm, exec, UnwindFromCallerFrame);
2018     ASSERT(vm->targetMachinePCForThrow);
2019 }
2020
2021 void JIT_OPERATION operationVMHandleException(ExecState* exec)
2022 {
2023     VM* vm = &exec->vm();
2024     NativeCallFrameTracer tracer(vm, exec);
2025     genericUnwind(vm, exec);
2026 }
2027
2028 // This function "should" just take the ExecState*, but doing so would make it more difficult
2029 // to call from exception check sites. So, unlike all of our other functions, we allow
2030 // ourselves to play some gnarly ABI tricks just to simplify the calling convention. This is
2031 // particularly safe here since this is never called on the critical path - it's only for
2032 // testing.
2033 void JIT_OPERATION operationExceptionFuzz(ExecState* exec)
2034 {
2035     VM* vm = &exec->vm();
2036     NativeCallFrameTracer tracer(vm, exec);
2037 #if COMPILER(GCC_OR_CLANG)
2038     void* returnPC = __builtin_return_address(0);
2039     doExceptionFuzzing(exec, "JITOperations", returnPC);
2040 #endif // COMPILER(GCC_OR_CLANG)
2041 }
2042
2043 EncodedJSValue JIT_OPERATION operationHasGenericProperty(ExecState* exec, EncodedJSValue encodedBaseValue, JSCell* propertyName)
2044 {
2045     VM& vm = exec->vm();
2046     NativeCallFrameTracer tracer(&vm, exec);
2047     JSValue baseValue = JSValue::decode(encodedBaseValue);
2048     if (baseValue.isUndefinedOrNull())
2049         return JSValue::encode(jsBoolean(false));
2050
2051     JSObject* base = baseValue.toObject(exec);
2052     return JSValue::encode(jsBoolean(base->hasProperty(exec, asString(propertyName)->toIdentifier(exec))));
2053 }
2054
2055 EncodedJSValue JIT_OPERATION operationHasIndexedProperty(ExecState* exec, JSCell* baseCell, int32_t subscript)
2056 {
2057     VM& vm = exec->vm();
2058     NativeCallFrameTracer tracer(&vm, exec);
2059     JSObject* object = baseCell->toObject(exec, exec->lexicalGlobalObject());
2060     return JSValue::encode(jsBoolean(object->hasProperty(exec, subscript)));
2061 }
2062     
2063 JSCell* JIT_OPERATION operationGetPropertyEnumerator(ExecState* exec, JSCell* cell)
2064 {
2065     VM& vm = exec->vm();
2066     NativeCallFrameTracer tracer(&vm, exec);
2067
2068     JSObject* base = cell->toObject(exec, exec->lexicalGlobalObject());
2069
2070     return propertyNameEnumerator(exec, base);
2071 }
2072
2073 EncodedJSValue JIT_OPERATION operationNextEnumeratorPname(ExecState* exec, JSCell* enumeratorCell, int32_t index)
2074 {
2075     VM& vm = exec->vm();
2076     NativeCallFrameTracer tracer(&vm, exec);
2077     JSPropertyNameEnumerator* enumerator = jsCast<JSPropertyNameEnumerator*>(enumeratorCell);
2078     JSString* propertyName = enumerator->propertyNameAtIndex(index);
2079     return JSValue::encode(propertyName ? propertyName : jsNull());
2080 }
2081
2082 JSCell* JIT_OPERATION operationToIndexString(ExecState* exec, int32_t index)
2083 {
2084     VM& vm = exec->vm();
2085     NativeCallFrameTracer tracer(&vm, exec);
2086     return jsString(exec, Identifier::from(exec, index).string());
2087 }
2088
2089 void JIT_OPERATION operationProcessTypeProfilerLog(ExecState* exec)
2090 {
2091     exec->vm().typeProfilerLog()->processLogEntries(ASCIILiteral("Log Full, called from inside baseline JIT"));
2092 }
2093
2094 int32_t JIT_OPERATION operationCheckIfExceptionIsUncatchableAndNotifyProfiler(ExecState* exec)
2095 {
2096     VM& vm = exec->vm();
2097     NativeCallFrameTracer tracer(&vm, exec);
2098     RELEASE_ASSERT(!!vm.exception());
2099
2100     if (LegacyProfiler* profiler = vm.enabledProfiler())
2101         profiler->exceptionUnwind(exec);
2102
2103     if (isTerminatedExecutionException(vm.exception())) {
2104         genericUnwind(&vm, exec);
2105         return 1;
2106     } else
2107         return 0;
2108 }
2109
2110 } // extern "C"
2111
2112 // Note: getHostCallReturnValueWithExecState() needs to be placed before the
2113 // definition of getHostCallReturnValue() below because the Windows build
2114 // requires it.
2115 extern "C" EncodedJSValue HOST_CALL_RETURN_VALUE_OPTION getHostCallReturnValueWithExecState(ExecState* exec)
2116 {
2117     if (!exec)
2118         return JSValue::encode(JSValue());
2119     return JSValue::encode(exec->vm().hostCallReturnValue);
2120 }
2121
2122 #if COMPILER(GCC_OR_CLANG) && CPU(X86_64)
2123 asm (
2124 ".globl " SYMBOL_STRING(getHostCallReturnValue) "\n"
2125 HIDE_SYMBOL(getHostCallReturnValue) "\n"
2126 SYMBOL_STRING(getHostCallReturnValue) ":" "\n"
2127     "lea -8(%rsp), %rdi\n"
2128     "jmp " LOCAL_REFERENCE(getHostCallReturnValueWithExecState) "\n"
2129 );
2130
2131 #elif COMPILER(GCC_OR_CLANG) && CPU(X86)
2132 asm (
2133 ".text" "\n" \
2134 ".globl " SYMBOL_STRING(getHostCallReturnValue) "\n"
2135 HIDE_SYMBOL(getHostCallReturnValue) "\n"
2136 SYMBOL_STRING(getHostCallReturnValue) ":" "\n"
2137     "push %ebp\n"
2138     "mov %esp, %eax\n"
2139     "leal -4(%esp), %esp\n"
2140     "push %eax\n"
2141     "call " LOCAL_REFERENCE(getHostCallReturnValueWithExecState) "\n"
2142     "leal 8(%esp), %esp\n"
2143     "pop %ebp\n"
2144     "ret\n"
2145 );
2146
2147 #elif COMPILER(GCC_OR_CLANG) && CPU(ARM_THUMB2)
2148 asm (
2149 ".text" "\n"
2150 ".align 2" "\n"
2151 ".globl " SYMBOL_STRING(getHostCallReturnValue) "\n"
2152 HIDE_SYMBOL(getHostCallReturnValue) "\n"
2153 ".thumb" "\n"
2154 ".thumb_func " THUMB_FUNC_PARAM(getHostCallReturnValue) "\n"
2155 SYMBOL_STRING(getHostCallReturnValue) ":" "\n"
2156     "sub r0, sp, #8" "\n"
2157     "b " LOCAL_REFERENCE(getHostCallReturnValueWithExecState) "\n"
2158 );
2159
2160 #elif COMPILER(GCC_OR_CLANG) && CPU(ARM_TRADITIONAL)
2161 asm (
2162 ".text" "\n"
2163 ".globl " SYMBOL_STRING(getHostCallReturnValue) "\n"
2164 HIDE_SYMBOL(getHostCallReturnValue) "\n"
2165 INLINE_ARM_FUNCTION(getHostCallReturnValue)
2166 SYMBOL_STRING(getHostCallReturnValue) ":" "\n"
2167     "sub r0, sp, #8" "\n"
2168     "b " LOCAL_REFERENCE(getHostCallReturnValueWithExecState) "\n"
2169 );
2170
2171 #elif CPU(ARM64)
2172 asm (
2173 ".text" "\n"
2174 ".align 2" "\n"
2175 ".globl " SYMBOL_STRING(getHostCallReturnValue) "\n"
2176 HIDE_SYMBOL(getHostCallReturnValue) "\n"
2177 SYMBOL_STRING(getHostCallReturnValue) ":" "\n"
2178      "sub x0, sp, #16" "\n"
2179      "b " LOCAL_REFERENCE(getHostCallReturnValueWithExecState) "\n"
2180 );
2181
2182 #elif COMPILER(GCC_OR_CLANG) && CPU(MIPS)
2183
2184 #if WTF_MIPS_PIC
2185 #define LOAD_FUNCTION_TO_T9(function) \
2186         ".set noreorder" "\n" \
2187         ".cpload $25" "\n" \
2188         ".set reorder" "\n" \
2189         "la $t9, " LOCAL_REFERENCE(function) "\n"
2190 #else
2191 #define LOAD_FUNCTION_TO_T9(function) "" "\n"
2192 #endif
2193
2194 asm (
2195 ".text" "\n"
2196 ".globl " SYMBOL_STRING(getHostCallReturnValue) "\n"
2197 HIDE_SYMBOL(getHostCallReturnValue) "\n"
2198 SYMBOL_STRING(getHostCallReturnValue) ":" "\n"
2199     LOAD_FUNCTION_TO_T9(getHostCallReturnValueWithExecState)
2200     "addi $a0, $sp, -8" "\n"
2201     "b " LOCAL_REFERENCE(getHostCallReturnValueWithExecState) "\n"
2202 );
2203
2204 #elif COMPILER(GCC_OR_CLANG) && CPU(SH4)
2205
2206 #define SH4_SCRATCH_REGISTER "r11"
2207
2208 asm (
2209 ".text" "\n"
2210 ".globl " SYMBOL_STRING(getHostCallReturnValue) "\n"
2211 HIDE_SYMBOL(getHostCallReturnValue) "\n"
2212 SYMBOL_STRING(getHostCallReturnValue) ":" "\n"
2213     "mov r15, r4" "\n"
2214     "add -8, r4" "\n"
2215     "mov.l 2f, " SH4_SCRATCH_REGISTER "\n"
2216     "braf " SH4_SCRATCH_REGISTER "\n"
2217     "nop" "\n"
2218     "1: .balign 4" "\n"
2219     "2: .long " LOCAL_REFERENCE(getHostCallReturnValueWithExecState) "-1b\n"
2220 );
2221
2222 #elif COMPILER(MSVC) && CPU(X86)
2223 extern "C" {
2224     __declspec(naked) EncodedJSValue HOST_CALL_RETURN_VALUE_OPTION getHostCallReturnValue()
2225     {
2226         __asm lea eax, [esp - 4]
2227         __asm mov [esp + 4], eax;
2228         __asm jmp getHostCallReturnValueWithExecState
2229     }
2230 }
2231 #endif
2232
2233 } // namespace JSC
2234
2235 #endif // ENABLE(JIT)