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