Remove pointer caging for double arrays
[WebKit-https.git] / Source / JavaScriptCore / jit / JITPropertyAccess.cpp
1 /*
2  * Copyright (C) 2008-2017 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27
28 #if ENABLE(JIT)
29 #include "JIT.h"
30
31 #include "CodeBlock.h"
32 #include "DirectArguments.h"
33 #include "GCAwareJITStubRoutine.h"
34 #include "GetterSetter.h"
35 #include "InterpreterInlines.h"
36 #include "JITInlines.h"
37 #include "JSArray.h"
38 #include "JSFunction.h"
39 #include "JSLexicalEnvironment.h"
40 #include "LinkBuffer.h"
41 #include "ResultType.h"
42 #include "ScopedArguments.h"
43 #include "ScopedArgumentsTable.h"
44 #include "SlowPathCall.h"
45 #include "StructureStubInfo.h"
46 #include <wtf/StringPrintStream.h>
47
48
49 namespace JSC {
50 #if USE(JSVALUE64)
51
52 JIT::CodeRef JIT::stringGetByValStubGenerator(VM* vm)
53 {
54     JSInterfaceJIT jit(vm);
55     JumpList failures;
56     failures.append(jit.branchStructure(
57         NotEqual, 
58         Address(regT0, JSCell::structureIDOffset()), 
59         vm->stringStructure.get()));
60
61     // Load string length to regT2, and start the process of loading the data pointer into regT0
62     jit.load32(Address(regT0, ThunkHelpers::jsStringLengthOffset()), regT2);
63     jit.loadPtr(Address(regT0, ThunkHelpers::jsStringValueOffset()), regT0);
64     failures.append(jit.branchTest32(Zero, regT0));
65
66     // Do an unsigned compare to simultaneously filter negative indices as well as indices that are too large
67     failures.append(jit.branch32(AboveOrEqual, regT1, regT2));
68     
69     // Load the character
70     JumpList is16Bit;
71     JumpList cont8Bit;
72     // Load the string flags
73     jit.loadPtr(Address(regT0, StringImpl::flagsOffset()), regT2);
74     jit.loadPtr(Address(regT0, StringImpl::dataOffset()), regT0);
75     is16Bit.append(jit.branchTest32(Zero, regT2, TrustedImm32(StringImpl::flagIs8Bit())));
76     jit.load8(BaseIndex(regT0, regT1, TimesOne, 0), regT0);
77     cont8Bit.append(jit.jump());
78     is16Bit.link(&jit);
79     jit.load16(BaseIndex(regT0, regT1, TimesTwo, 0), regT0);
80     cont8Bit.link(&jit);
81
82     failures.append(jit.branch32(AboveOrEqual, regT0, TrustedImm32(0x100)));
83     jit.move(TrustedImmPtr(vm->smallStrings.singleCharacterStrings()), regT1);
84     jit.loadPtr(BaseIndex(regT1, regT0, ScalePtr, 0), regT0);
85     jit.ret();
86     
87     failures.link(&jit);
88     jit.move(TrustedImm32(0), regT0);
89     jit.ret();
90     
91     LinkBuffer patchBuffer(jit, GLOBAL_THUNK_ID);
92     return FINALIZE_CODE(patchBuffer, ("String get_by_val stub"));
93 }
94
95 void JIT::emit_op_get_by_val(Instruction* currentInstruction)
96 {
97     int dst = currentInstruction[1].u.operand;
98     int base = currentInstruction[2].u.operand;
99     int property = currentInstruction[3].u.operand;
100     ArrayProfile* profile = currentInstruction[4].u.arrayProfile;
101     ByValInfo* byValInfo = m_codeBlock->addByValInfo();
102
103     emitGetVirtualRegister(base, regT0);
104     bool propertyNameIsIntegerConstant = isOperandConstantInt(property);
105     if (propertyNameIsIntegerConstant)
106         move(Imm32(getOperandConstantInt(property)), regT1);
107     else
108         emitGetVirtualRegister(property, regT1);
109
110     emitJumpSlowCaseIfNotJSCell(regT0, base);
111
112     PatchableJump notIndex;
113     if (!propertyNameIsIntegerConstant) {
114         notIndex = emitPatchableJumpIfNotInt(regT1);
115         addSlowCase(notIndex);
116
117         // This is technically incorrect - we're zero-extending an int32. On the hot path this doesn't matter.
118         // We check the value as if it was a uint32 against the m_vectorLength - which will always fail if
119         // number was signed since m_vectorLength is always less than intmax (since the total allocation
120         // size is always less than 4Gb). As such zero extending will have been correct (and extending the value
121         // to 64-bits is necessary since it's used in the address calculation). We zero extend rather than sign
122         // extending since it makes it easier to re-tag the value in the slow case.
123         zeroExtend32ToPtr(regT1, regT1);
124     }
125
126     emitArrayProfilingSiteWithCell(regT0, regT2, profile);
127     and32(TrustedImm32(IndexingShapeMask), regT2);
128
129     PatchableJump badType;
130     JumpList slowCases;
131
132     JITArrayMode mode = chooseArrayMode(profile);
133     switch (mode) {
134     case JITInt32:
135         slowCases = emitInt32GetByVal(currentInstruction, badType);
136         break;
137     case JITDouble:
138         slowCases = emitDoubleGetByVal(currentInstruction, badType);
139         break;
140     case JITContiguous:
141         slowCases = emitContiguousGetByVal(currentInstruction, badType);
142         break;
143     case JITArrayStorage:
144         slowCases = emitArrayStorageGetByVal(currentInstruction, badType);
145         break;
146     default:
147         CRASH();
148         break;
149     }
150     
151     addSlowCase(badType);
152     addSlowCase(slowCases);
153     
154     Label done = label();
155     
156     if (!ASSERT_DISABLED) {
157         Jump resultOK = branchTest64(NonZero, regT0);
158         abortWithReason(JITGetByValResultIsNotEmpty);
159         resultOK.link(this);
160     }
161
162     emitValueProfilingSite();
163     emitPutVirtualRegister(dst);
164
165     Label nextHotPath = label();
166
167     m_byValCompilationInfo.append(ByValCompilationInfo(byValInfo, m_bytecodeOffset, notIndex, badType, mode, profile, done, nextHotPath));
168 }
169
170 JIT::JumpList JIT::emitDoubleLoad(Instruction*, PatchableJump& badType)
171 {
172     JumpList slowCases;
173     
174     badType = patchableBranch32(NotEqual, regT2, TrustedImm32(DoubleShape));
175     loadPtr(Address(regT0, JSObject::butterflyOffset()), regT2);
176     slowCases.append(branch32(AboveOrEqual, regT1, Address(regT2, Butterfly::offsetOfPublicLength())));
177     loadDouble(BaseIndex(regT2, regT1, TimesEight), fpRegT0);
178     slowCases.append(branchDouble(DoubleNotEqualOrUnordered, fpRegT0, fpRegT0));
179     
180     return slowCases;
181 }
182
183 JIT::JumpList JIT::emitContiguousLoad(Instruction*, PatchableJump& badType, IndexingType expectedShape)
184 {
185     JumpList slowCases;
186     
187     badType = patchableBranch32(NotEqual, regT2, TrustedImm32(expectedShape));
188     loadPtr(Address(regT0, JSObject::butterflyOffset()), regT2);
189     slowCases.append(branch32(AboveOrEqual, regT1, Address(regT2, Butterfly::offsetOfPublicLength())));
190     load64(BaseIndex(regT2, regT1, TimesEight), regT0);
191     slowCases.append(branchTest64(Zero, regT0));
192     
193     return slowCases;
194 }
195
196 JIT::JumpList JIT::emitArrayStorageLoad(Instruction*, PatchableJump& badType)
197 {
198     JumpList slowCases;
199
200     add32(TrustedImm32(-ArrayStorageShape), regT2, regT3);
201     badType = patchableBranch32(Above, regT3, TrustedImm32(SlowPutArrayStorageShape - ArrayStorageShape));
202
203     loadPtr(Address(regT0, JSObject::butterflyOffset()), regT2);
204     slowCases.append(branch32(AboveOrEqual, regT1, Address(regT2, ArrayStorage::vectorLengthOffset())));
205
206     load64(BaseIndex(regT2, regT1, TimesEight, ArrayStorage::vectorOffset()), regT0);
207     slowCases.append(branchTest64(Zero, regT0));
208     
209     return slowCases;
210 }
211
212 JITGetByIdGenerator JIT::emitGetByValWithCachedId(ByValInfo* byValInfo, Instruction* currentInstruction, const Identifier& propertyName, Jump& fastDoneCase, Jump& slowDoneCase, JumpList& slowCases)
213 {
214     // base: regT0
215     // property: regT1
216     // scratch: regT3
217
218     int dst = currentInstruction[1].u.operand;
219
220     slowCases.append(emitJumpIfNotJSCell(regT1));
221     emitByValIdentifierCheck(byValInfo, regT1, regT3, propertyName, slowCases);
222
223     JITGetByIdGenerator gen(
224         m_codeBlock, CodeOrigin(m_bytecodeOffset), CallSiteIndex(m_bytecodeOffset), RegisterSet::stubUnavailableRegisters(),
225         propertyName.impl(), JSValueRegs(regT0), JSValueRegs(regT0), AccessType::Get);
226     gen.generateFastPath(*this);
227
228     fastDoneCase = jump();
229
230     Label coldPathBegin = label();
231     gen.slowPathJump().link(this);
232
233     Call call = callOperation(WithProfile, operationGetByIdOptimize, dst, gen.stubInfo(), regT0, propertyName.impl());
234     gen.reportSlowPathCall(coldPathBegin, call);
235     slowDoneCase = jump();
236
237     return gen;
238 }
239
240 void JIT::emitSlow_op_get_by_val(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
241 {
242     int dst = currentInstruction[1].u.operand;
243     int base = currentInstruction[2].u.operand;
244     int property = currentInstruction[3].u.operand;
245     ByValInfo* byValInfo = m_byValCompilationInfo[m_byValInstructionIndex].byValInfo;
246     
247     linkSlowCaseIfNotJSCell(iter, base); // base cell check
248
249     if (!isOperandConstantInt(property))
250         linkSlowCase(iter); // property int32 check
251     Jump nonCell = jump();
252     linkSlowCase(iter); // base array check
253     Jump notString = branchStructure(NotEqual, 
254         Address(regT0, JSCell::structureIDOffset()), 
255         m_vm->stringStructure.get());
256     emitNakedCall(CodeLocationLabel(m_vm->getCTIStub(stringGetByValStubGenerator).code()));
257     Jump failed = branchTest64(Zero, regT0);
258     emitPutVirtualRegister(dst, regT0);
259     emitJumpSlowToHot(jump(), OPCODE_LENGTH(op_get_by_val));
260     failed.link(this);
261     notString.link(this);
262     nonCell.link(this);
263     
264     linkSlowCase(iter); // vector length check
265     linkSlowCase(iter); // empty value
266     
267     Label slowPath = label();
268     
269     emitGetVirtualRegister(base, regT0);
270     emitGetVirtualRegister(property, regT1);
271     Call call = callOperation(operationGetByValOptimize, dst, regT0, regT1, byValInfo);
272
273     m_byValCompilationInfo[m_byValInstructionIndex].slowPathTarget = slowPath;
274     m_byValCompilationInfo[m_byValInstructionIndex].returnAddress = call;
275     m_byValInstructionIndex++;
276
277     emitValueProfilingSite();
278 }
279
280 void JIT::emit_op_put_by_val(Instruction* currentInstruction)
281 {
282     int base = currentInstruction[1].u.operand;
283     int property = currentInstruction[2].u.operand;
284     ArrayProfile* profile = currentInstruction[4].u.arrayProfile;
285     ByValInfo* byValInfo = m_codeBlock->addByValInfo();
286
287     emitGetVirtualRegister(base, regT0);
288     bool propertyNameIsIntegerConstant = isOperandConstantInt(property);
289     if (propertyNameIsIntegerConstant)
290         move(Imm32(getOperandConstantInt(property)), regT1);
291     else
292         emitGetVirtualRegister(property, regT1);
293
294     emitJumpSlowCaseIfNotJSCell(regT0, base);
295     PatchableJump notIndex;
296     if (!propertyNameIsIntegerConstant) {
297         notIndex = emitPatchableJumpIfNotInt(regT1);
298         addSlowCase(notIndex);
299         // See comment in op_get_by_val.
300         zeroExtend32ToPtr(regT1, regT1);
301     }
302     emitArrayProfilingSiteWithCell(regT0, regT2, profile);
303     and32(TrustedImm32(IndexingShapeMask), regT2);
304     
305     PatchableJump badType;
306     JumpList slowCases;
307     
308     JITArrayMode mode = chooseArrayMode(profile);
309     switch (mode) {
310     case JITInt32:
311         slowCases = emitInt32PutByVal(currentInstruction, badType);
312         break;
313     case JITDouble:
314         slowCases = emitDoublePutByVal(currentInstruction, badType);
315         break;
316     case JITContiguous:
317         slowCases = emitContiguousPutByVal(currentInstruction, badType);
318         break;
319     case JITArrayStorage:
320         slowCases = emitArrayStoragePutByVal(currentInstruction, badType);
321         break;
322     default:
323         CRASH();
324         break;
325     }
326     
327     addSlowCase(badType);
328     addSlowCase(slowCases);
329     
330     Label done = label();
331     
332     m_byValCompilationInfo.append(ByValCompilationInfo(byValInfo, m_bytecodeOffset, notIndex, badType, mode, profile, done, done));
333 }
334
335 JIT::JumpList JIT::emitGenericContiguousPutByVal(Instruction* currentInstruction, PatchableJump& badType, IndexingType indexingShape)
336 {
337     int value = currentInstruction[3].u.operand;
338     ArrayProfile* profile = currentInstruction[4].u.arrayProfile;
339     
340     JumpList slowCases;
341
342     badType = patchableBranch32(NotEqual, regT2, TrustedImm32(indexingShape));
343     
344     loadPtr(Address(regT0, JSObject::butterflyOffset()), regT2);
345     Jump outOfBounds = branch32(AboveOrEqual, regT1, Address(regT2, Butterfly::offsetOfPublicLength()));
346
347     Label storeResult = label();
348     emitGetVirtualRegister(value, regT3);
349     switch (indexingShape) {
350     case Int32Shape:
351         slowCases.append(emitJumpIfNotInt(regT3));
352         store64(regT3, BaseIndex(regT2, regT1, TimesEight));
353         break;
354     case DoubleShape: {
355         Jump notInt = emitJumpIfNotInt(regT3);
356         convertInt32ToDouble(regT3, fpRegT0);
357         Jump ready = jump();
358         notInt.link(this);
359         add64(tagTypeNumberRegister, regT3);
360         move64ToDouble(regT3, fpRegT0);
361         slowCases.append(branchDouble(DoubleNotEqualOrUnordered, fpRegT0, fpRegT0));
362         ready.link(this);
363         storeDouble(fpRegT0, BaseIndex(regT2, regT1, TimesEight));
364         break;
365     }
366     case ContiguousShape:
367         store64(regT3, BaseIndex(regT2, regT1, TimesEight));
368         emitWriteBarrier(currentInstruction[1].u.operand, value, ShouldFilterValue);
369         break;
370     default:
371         CRASH();
372         break;
373     }
374     
375     Jump done = jump();
376     outOfBounds.link(this);
377     
378     slowCases.append(branch32(AboveOrEqual, regT1, Address(regT2, Butterfly::offsetOfVectorLength())));
379     
380     emitArrayProfileStoreToHoleSpecialCase(profile);
381     
382     add32(TrustedImm32(1), regT1, regT3);
383     store32(regT3, Address(regT2, Butterfly::offsetOfPublicLength()));
384     jump().linkTo(storeResult, this);
385     
386     done.link(this);
387     
388     return slowCases;
389 }
390
391 JIT::JumpList JIT::emitArrayStoragePutByVal(Instruction* currentInstruction, PatchableJump& badType)
392 {
393     int value = currentInstruction[3].u.operand;
394     ArrayProfile* profile = currentInstruction[4].u.arrayProfile;
395     
396     JumpList slowCases;
397     
398     badType = patchableBranch32(NotEqual, regT2, TrustedImm32(ArrayStorageShape));
399     loadPtr(Address(regT0, JSObject::butterflyOffset()), regT2);
400     slowCases.append(branch32(AboveOrEqual, regT1, Address(regT2, ArrayStorage::vectorLengthOffset())));
401
402     Jump empty = branchTest64(Zero, BaseIndex(regT2, regT1, TimesEight, ArrayStorage::vectorOffset()));
403
404     Label storeResult(this);
405     emitGetVirtualRegister(value, regT3);
406     store64(regT3, BaseIndex(regT2, regT1, TimesEight, ArrayStorage::vectorOffset()));
407     emitWriteBarrier(currentInstruction[1].u.operand, value, ShouldFilterValue);
408     Jump end = jump();
409     
410     empty.link(this);
411     emitArrayProfileStoreToHoleSpecialCase(profile);
412     add32(TrustedImm32(1), Address(regT2, ArrayStorage::numValuesInVectorOffset()));
413     branch32(Below, regT1, Address(regT2, ArrayStorage::lengthOffset())).linkTo(storeResult, this);
414
415     add32(TrustedImm32(1), regT1);
416     store32(regT1, Address(regT2, ArrayStorage::lengthOffset()));
417     sub32(TrustedImm32(1), regT1);
418     jump().linkTo(storeResult, this);
419
420     end.link(this);
421     
422     return slowCases;
423 }
424
425 JITPutByIdGenerator JIT::emitPutByValWithCachedId(ByValInfo* byValInfo, Instruction* currentInstruction, PutKind putKind, const Identifier& propertyName, JumpList& doneCases, JumpList& slowCases)
426 {
427     // base: regT0
428     // property: regT1
429     // scratch: regT2
430
431     int base = currentInstruction[1].u.operand;
432     int value = currentInstruction[3].u.operand;
433
434     slowCases.append(emitJumpIfNotJSCell(regT1));
435     emitByValIdentifierCheck(byValInfo, regT1, regT1, propertyName, slowCases);
436
437     // Write barrier breaks the registers. So after issuing the write barrier,
438     // reload the registers.
439     emitGetVirtualRegisters(base, regT0, value, regT1);
440
441     JITPutByIdGenerator gen(
442         m_codeBlock, CodeOrigin(m_bytecodeOffset), CallSiteIndex(m_bytecodeOffset), RegisterSet::stubUnavailableRegisters(),
443         JSValueRegs(regT0), JSValueRegs(regT1), regT2, m_codeBlock->ecmaMode(), putKind);
444     gen.generateFastPath(*this);
445     emitWriteBarrier(base, value, ShouldFilterBase);
446     doneCases.append(jump());
447
448     Label coldPathBegin = label();
449     gen.slowPathJump().link(this);
450
451     Call call = callOperation(gen.slowPathFunction(), gen.stubInfo(), regT1, regT0, propertyName.impl());
452     gen.reportSlowPathCall(coldPathBegin, call);
453     doneCases.append(jump());
454
455     return gen;
456 }
457
458 void JIT::emitSlow_op_put_by_val(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
459 {
460     int base = currentInstruction[1].u.operand;
461     int property = currentInstruction[2].u.operand;
462     int value = currentInstruction[3].u.operand;
463     JITArrayMode mode = m_byValCompilationInfo[m_byValInstructionIndex].arrayMode;
464     ByValInfo* byValInfo = m_byValCompilationInfo[m_byValInstructionIndex].byValInfo;
465
466     linkSlowCaseIfNotJSCell(iter, base); // base cell check
467     if (!isOperandConstantInt(property))
468         linkSlowCase(iter); // property int32 check
469     linkSlowCase(iter); // base not array check
470     
471     linkSlowCase(iter); // out of bounds
472     
473     switch (mode) {
474     case JITInt32:
475     case JITDouble:
476         linkSlowCase(iter); // value type check
477         break;
478     default:
479         break;
480     }
481     
482     Label slowPath = label();
483
484     emitGetVirtualRegister(base, regT0);
485     emitGetVirtualRegister(property, regT1);
486     emitGetVirtualRegister(value, regT2);
487     bool isDirect = Interpreter::getOpcodeID(currentInstruction->u.opcode) == op_put_by_val_direct;
488     Call call = callOperation(isDirect ? operationDirectPutByValOptimize : operationPutByValOptimize, regT0, regT1, regT2, byValInfo);
489
490     m_byValCompilationInfo[m_byValInstructionIndex].slowPathTarget = slowPath;
491     m_byValCompilationInfo[m_byValInstructionIndex].returnAddress = call;
492     m_byValInstructionIndex++;
493 }
494
495 void JIT::emit_op_put_by_index(Instruction* currentInstruction)
496 {
497     emitGetVirtualRegister(currentInstruction[1].u.operand, regT0);
498     emitGetVirtualRegister(currentInstruction[3].u.operand, regT1);
499     callOperation(operationPutByIndex, regT0, currentInstruction[2].u.operand, regT1);
500 }
501
502 void JIT::emit_op_put_getter_by_id(Instruction* currentInstruction)
503 {
504     emitGetVirtualRegister(currentInstruction[1].u.operand, regT0);
505     int32_t options = currentInstruction[3].u.operand;
506     emitGetVirtualRegister(currentInstruction[4].u.operand, regT1);
507     callOperation(operationPutGetterById, regT0, m_codeBlock->identifier(currentInstruction[2].u.operand).impl(), options, regT1);
508 }
509
510 void JIT::emit_op_put_setter_by_id(Instruction* currentInstruction)
511 {
512     emitGetVirtualRegister(currentInstruction[1].u.operand, regT0);
513     int32_t options = currentInstruction[3].u.operand;
514     emitGetVirtualRegister(currentInstruction[4].u.operand, regT1);
515     callOperation(operationPutSetterById, regT0, m_codeBlock->identifier(currentInstruction[2].u.operand).impl(), options, regT1);
516 }
517
518 void JIT::emit_op_put_getter_setter_by_id(Instruction* currentInstruction)
519 {
520     emitGetVirtualRegister(currentInstruction[1].u.operand, regT0);
521     int32_t attribute = currentInstruction[3].u.operand;
522     emitGetVirtualRegister(currentInstruction[4].u.operand, regT1);
523     emitGetVirtualRegister(currentInstruction[5].u.operand, regT2);
524     callOperation(operationPutGetterSetter, regT0, m_codeBlock->identifier(currentInstruction[2].u.operand).impl(), attribute, regT1, regT2);
525 }
526
527 void JIT::emit_op_put_getter_by_val(Instruction* currentInstruction)
528 {
529     emitGetVirtualRegister(currentInstruction[1].u.operand, regT0);
530     emitGetVirtualRegister(currentInstruction[2].u.operand, regT1);
531     int32_t attributes = currentInstruction[3].u.operand;
532     emitGetVirtualRegister(currentInstruction[4].u.operand, regT2);
533     callOperation(operationPutGetterByVal, regT0, regT1, attributes, regT2);
534 }
535
536 void JIT::emit_op_put_setter_by_val(Instruction* currentInstruction)
537 {
538     emitGetVirtualRegister(currentInstruction[1].u.operand, regT0);
539     emitGetVirtualRegister(currentInstruction[2].u.operand, regT1);
540     int32_t attributes = currentInstruction[3].u.operand;
541     emitGetVirtualRegister(currentInstruction[4].u.operand, regT2);
542     callOperation(operationPutSetterByVal, regT0, regT1, attributes, regT2);
543 }
544
545 void JIT::emit_op_del_by_id(Instruction* currentInstruction)
546 {
547     int dst = currentInstruction[1].u.operand;
548     int base = currentInstruction[2].u.operand;
549     int property = currentInstruction[3].u.operand;
550     emitGetVirtualRegister(base, regT0);
551     callOperation(operationDeleteByIdJSResult, dst, regT0, m_codeBlock->identifier(property).impl());
552 }
553
554 void JIT::emit_op_del_by_val(Instruction* currentInstruction)
555 {
556     int dst = currentInstruction[1].u.operand;
557     int base = currentInstruction[2].u.operand;
558     int property = currentInstruction[3].u.operand;
559     emitGetVirtualRegister(base, regT0);
560     emitGetVirtualRegister(property, regT1);
561     callOperation(operationDeleteByValJSResult, dst, regT0, regT1);
562 }
563
564 void JIT::emit_op_try_get_by_id(Instruction* currentInstruction)
565 {
566     int resultVReg = currentInstruction[1].u.operand;
567     int baseVReg = currentInstruction[2].u.operand;
568     const Identifier* ident = &(m_codeBlock->identifier(currentInstruction[3].u.operand));
569
570     emitGetVirtualRegister(baseVReg, regT0);
571
572     emitJumpSlowCaseIfNotJSCell(regT0, baseVReg);
573
574     JITGetByIdGenerator gen(
575         m_codeBlock, CodeOrigin(m_bytecodeOffset), CallSiteIndex(m_bytecodeOffset), RegisterSet::stubUnavailableRegisters(),
576         ident->impl(), JSValueRegs(regT0), JSValueRegs(regT0), AccessType::TryGet);
577     gen.generateFastPath(*this);
578     addSlowCase(gen.slowPathJump());
579     m_getByIds.append(gen);
580     
581     emitValueProfilingSite();
582     emitPutVirtualRegister(resultVReg);
583 }
584
585 void JIT::emitSlow_op_try_get_by_id(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
586 {
587     linkAllSlowCases(iter);
588
589     int resultVReg = currentInstruction[1].u.operand;
590     const Identifier* ident = &(m_codeBlock->identifier(currentInstruction[3].u.operand));
591
592     JITGetByIdGenerator& gen = m_getByIds[m_getByIdIndex++];
593
594     Label coldPathBegin = label();
595
596     Call call = callOperation(operationTryGetByIdOptimize, resultVReg, gen.stubInfo(), regT0, ident->impl());
597     
598     gen.reportSlowPathCall(coldPathBegin, call);
599 }
600
601 void JIT::emit_op_get_by_id(Instruction* currentInstruction)
602 {
603     int resultVReg = currentInstruction[1].u.operand;
604     int baseVReg = currentInstruction[2].u.operand;
605     const Identifier* ident = &(m_codeBlock->identifier(currentInstruction[3].u.operand));
606
607     emitGetVirtualRegister(baseVReg, regT0);
608     
609     emitJumpSlowCaseIfNotJSCell(regT0, baseVReg);
610     
611     if (*ident == m_vm->propertyNames->length && shouldEmitProfiling())
612         emitArrayProfilingSiteForBytecodeIndexWithCell(regT0, regT1, m_bytecodeOffset);
613
614     JITGetByIdGenerator gen(
615         m_codeBlock, CodeOrigin(m_bytecodeOffset), CallSiteIndex(m_bytecodeOffset), RegisterSet::stubUnavailableRegisters(),
616         ident->impl(), JSValueRegs(regT0), JSValueRegs(regT0), AccessType::Get);
617     gen.generateFastPath(*this);
618     addSlowCase(gen.slowPathJump());
619     m_getByIds.append(gen);
620
621     emitValueProfilingSite();
622     emitPutVirtualRegister(resultVReg);
623 }
624
625 void JIT::emit_op_get_by_id_with_this(Instruction* currentInstruction)
626 {
627     int resultVReg = currentInstruction[1].u.operand;
628     int baseVReg = currentInstruction[2].u.operand;
629     int thisVReg = currentInstruction[3].u.operand;
630     const Identifier* ident = &(m_codeBlock->identifier(currentInstruction[4].u.operand));
631
632     emitGetVirtualRegister(baseVReg, regT0);
633     emitGetVirtualRegister(thisVReg, regT1);
634     emitJumpSlowCaseIfNotJSCell(regT0, baseVReg);
635     emitJumpSlowCaseIfNotJSCell(regT1, thisVReg);
636
637     JITGetByIdWithThisGenerator gen(
638         m_codeBlock, CodeOrigin(m_bytecodeOffset), CallSiteIndex(m_bytecodeOffset), RegisterSet::stubUnavailableRegisters(),
639         ident->impl(), JSValueRegs(regT0), JSValueRegs(regT0), JSValueRegs(regT1), AccessType::GetWithThis);
640     gen.generateFastPath(*this);
641     addSlowCase(gen.slowPathJump());
642     m_getByIdsWithThis.append(gen);
643
644     emitValueProfilingSite();
645     emitPutVirtualRegister(resultVReg);
646 }
647
648 void JIT::emitSlow_op_get_by_id(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
649 {
650     linkAllSlowCases(iter);
651
652     int resultVReg = currentInstruction[1].u.operand;
653     const Identifier* ident = &(m_codeBlock->identifier(currentInstruction[3].u.operand));
654
655     JITGetByIdGenerator& gen = m_getByIds[m_getByIdIndex++];
656     
657     Label coldPathBegin = label();
658     
659     Call call = callOperation(WithProfile, operationGetByIdOptimize, resultVReg, gen.stubInfo(), regT0, ident->impl());
660
661     gen.reportSlowPathCall(coldPathBegin, call);
662 }
663
664 void JIT::emitSlow_op_get_by_id_with_this(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
665 {
666     linkAllSlowCases(iter);
667
668     int resultVReg = currentInstruction[1].u.operand;
669     const Identifier* ident = &(m_codeBlock->identifier(currentInstruction[4].u.operand));
670
671     JITGetByIdWithThisGenerator& gen = m_getByIdsWithThis[m_getByIdWithThisIndex++];
672     
673     Label coldPathBegin = label();
674     
675     Call call = callOperation(WithProfile, operationGetByIdWithThisOptimize, resultVReg, gen.stubInfo(), regT0, regT1, ident->impl());
676
677     gen.reportSlowPathCall(coldPathBegin, call);
678 }
679
680 void JIT::emit_op_put_by_id(Instruction* currentInstruction)
681 {
682     int baseVReg = currentInstruction[1].u.operand;
683     int valueVReg = currentInstruction[3].u.operand;
684     unsigned direct = currentInstruction[8].u.putByIdFlags & PutByIdIsDirect;
685
686     // In order to be able to patch both the Structure, and the object offset, we store one pointer,
687     // to just after the arguments have been loaded into registers 'hotPathBegin', and we generate code
688     // such that the Structure & offset are always at the same distance from this.
689
690     emitGetVirtualRegisters(baseVReg, regT0, valueVReg, regT1);
691
692     emitJumpSlowCaseIfNotJSCell(regT0, baseVReg);
693
694     JITPutByIdGenerator gen(
695         m_codeBlock, CodeOrigin(m_bytecodeOffset), CallSiteIndex(m_bytecodeOffset), RegisterSet::stubUnavailableRegisters(),
696         JSValueRegs(regT0), JSValueRegs(regT1), regT2, m_codeBlock->ecmaMode(),
697         direct ? Direct : NotDirect);
698     
699     gen.generateFastPath(*this);
700     addSlowCase(gen.slowPathJump());
701     
702     emitWriteBarrier(baseVReg, valueVReg, ShouldFilterBase);
703
704     m_putByIds.append(gen);
705 }
706
707 void JIT::emitSlow_op_put_by_id(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
708 {
709     linkAllSlowCases(iter);
710
711     const Identifier* ident = &(m_codeBlock->identifier(currentInstruction[2].u.operand));
712
713     Label coldPathBegin(this);
714     
715     JITPutByIdGenerator& gen = m_putByIds[m_putByIdIndex++];
716
717     Call call = callOperation(
718         gen.slowPathFunction(), gen.stubInfo(), regT1, regT0, ident->impl());
719
720     gen.reportSlowPathCall(coldPathBegin, call);
721 }
722
723 void JIT::emitVarInjectionCheck(bool needsVarInjectionChecks)
724 {
725     if (!needsVarInjectionChecks)
726         return;
727     addSlowCase(branch8(Equal, AbsoluteAddress(m_codeBlock->globalObject()->varInjectionWatchpoint()->addressOfState()), TrustedImm32(IsInvalidated)));
728 }
729
730 void JIT::emitResolveClosure(int dst, int scope, bool needsVarInjectionChecks, unsigned depth)
731 {
732     emitVarInjectionCheck(needsVarInjectionChecks);
733     emitGetVirtualRegister(scope, regT0);
734     for (unsigned i = 0; i < depth; ++i)
735         loadPtr(Address(regT0, JSScope::offsetOfNext()), regT0);
736     emitPutVirtualRegister(dst);
737 }
738
739 void JIT::emit_op_resolve_scope(Instruction* currentInstruction)
740 {
741     int dst = currentInstruction[1].u.operand;
742     int scope = currentInstruction[2].u.operand;
743     ResolveType resolveType = static_cast<ResolveType>(copiedInstruction(currentInstruction)[4].u.operand);
744     unsigned depth = currentInstruction[5].u.operand;
745
746     auto emitCode = [&] (ResolveType resolveType) {
747         switch (resolveType) {
748         case GlobalProperty:
749         case GlobalVar:
750         case GlobalPropertyWithVarInjectionChecks:
751         case GlobalVarWithVarInjectionChecks:
752         case GlobalLexicalVar:
753         case GlobalLexicalVarWithVarInjectionChecks: {
754             JSScope* constantScope = JSScope::constantScopeForCodeBlock(resolveType, m_codeBlock);
755             RELEASE_ASSERT(constantScope);
756             emitVarInjectionCheck(needsVarInjectionChecks(resolveType));
757             move(TrustedImmPtr(constantScope), regT0);
758             emitPutVirtualRegister(dst);
759             break;
760         }
761         case ClosureVar:
762         case ClosureVarWithVarInjectionChecks:
763             emitResolveClosure(dst, scope, needsVarInjectionChecks(resolveType), depth);
764             break;
765         case ModuleVar:
766             move(TrustedImmPtr(currentInstruction[6].u.jsCell.get()), regT0);
767             emitPutVirtualRegister(dst);
768             break;
769         case Dynamic:
770             addSlowCase(jump());
771             break;
772         case LocalClosureVar:
773         case UnresolvedProperty:
774         case UnresolvedPropertyWithVarInjectionChecks:
775             RELEASE_ASSERT_NOT_REACHED();
776         }
777     };
778
779     switch (resolveType) {
780     case UnresolvedProperty:
781     case UnresolvedPropertyWithVarInjectionChecks: {
782         JumpList skipToEnd;
783         load32(&currentInstruction[4], regT0);
784
785         Jump notGlobalProperty = branch32(NotEqual, regT0, TrustedImm32(GlobalProperty));
786         emitCode(GlobalProperty);
787         skipToEnd.append(jump());
788         notGlobalProperty.link(this);
789
790         Jump notGlobalPropertyWithVarInjections = branch32(NotEqual, regT0, TrustedImm32(GlobalPropertyWithVarInjectionChecks));
791         emitCode(GlobalPropertyWithVarInjectionChecks);
792         skipToEnd.append(jump());
793         notGlobalPropertyWithVarInjections.link(this);
794
795         Jump notGlobalLexicalVar = branch32(NotEqual, regT0, TrustedImm32(GlobalLexicalVar));
796         emitCode(GlobalLexicalVar);
797         skipToEnd.append(jump());
798         notGlobalLexicalVar.link(this);
799
800         Jump notGlobalLexicalVarWithVarInjections = branch32(NotEqual, regT0, TrustedImm32(GlobalLexicalVarWithVarInjectionChecks));
801         emitCode(GlobalLexicalVarWithVarInjectionChecks);
802         skipToEnd.append(jump());
803         notGlobalLexicalVarWithVarInjections.link(this);
804
805         addSlowCase(jump());
806         skipToEnd.link(this);
807         break;
808     }
809
810     default:
811         emitCode(resolveType);
812         break;
813     }
814 }
815
816 void JIT::emitLoadWithStructureCheck(int scope, Structure** structureSlot)
817 {
818     loadPtr(structureSlot, regT1);
819     emitGetVirtualRegister(scope, regT0);
820     addSlowCase(branchTestPtr(Zero, regT1));
821     load32(Address(regT1, Structure::structureIDOffset()), regT1);
822     addSlowCase(branch32(NotEqual, Address(regT0, JSCell::structureIDOffset()), regT1));
823 }
824
825 void JIT::emitGetVarFromPointer(JSValue* operand, GPRReg reg)
826 {
827     loadPtr(operand, reg);
828 }
829
830 void JIT::emitGetVarFromIndirectPointer(JSValue** operand, GPRReg reg)
831 {
832     loadPtr(operand, reg);
833     loadPtr(reg, reg);
834 }
835
836 void JIT::emitGetClosureVar(int scope, uintptr_t operand)
837 {
838     emitGetVirtualRegister(scope, regT0);
839     loadPtr(Address(regT0, JSLexicalEnvironment::offsetOfVariables() + operand * sizeof(Register)), regT0);
840 }
841
842 void JIT::emit_op_get_from_scope(Instruction* currentInstruction)
843 {
844     int dst = currentInstruction[1].u.operand;
845     int scope = currentInstruction[2].u.operand;
846     ResolveType resolveType = GetPutInfo(copiedInstruction(currentInstruction)[4].u.operand).resolveType();
847     Structure** structureSlot = currentInstruction[5].u.structure.slot();
848     uintptr_t* operandSlot = reinterpret_cast<uintptr_t*>(&currentInstruction[6].u.pointer);
849
850     auto emitCode = [&] (ResolveType resolveType, bool indirectLoadForOperand) {
851         switch (resolveType) {
852         case GlobalProperty:
853         case GlobalPropertyWithVarInjectionChecks: {
854             emitLoadWithStructureCheck(scope, structureSlot); // Structure check covers var injection.
855             GPRReg base = regT0;
856             GPRReg result = regT0;
857             GPRReg offset = regT1;
858             GPRReg scratch = regT2;
859             
860             load32(operandSlot, offset);
861             if (!ASSERT_DISABLED) {
862                 Jump isOutOfLine = branch32(GreaterThanOrEqual, offset, TrustedImm32(firstOutOfLineOffset));
863                 abortWithReason(JITOffsetIsNotOutOfLine);
864                 isOutOfLine.link(this);
865             }
866             loadPtr(Address(base, JSObject::butterflyOffset()), scratch);
867             neg32(offset);
868             signExtend32ToPtr(offset, offset);
869             load64(BaseIndex(scratch, offset, TimesEight, (firstOutOfLineOffset - 2) * sizeof(EncodedJSValue)), result);
870             break;
871         }
872         case GlobalVar:
873         case GlobalVarWithVarInjectionChecks:
874         case GlobalLexicalVar:
875         case GlobalLexicalVarWithVarInjectionChecks:
876             emitVarInjectionCheck(needsVarInjectionChecks(resolveType));
877             if (indirectLoadForOperand)
878                 emitGetVarFromIndirectPointer(bitwise_cast<JSValue**>(operandSlot), regT0);
879             else
880                 emitGetVarFromPointer(bitwise_cast<JSValue*>(*operandSlot), regT0);
881             if (resolveType == GlobalLexicalVar || resolveType == GlobalLexicalVarWithVarInjectionChecks) // TDZ check.
882                 addSlowCase(branchTest64(Zero, regT0));
883             break;
884         case ClosureVar:
885         case ClosureVarWithVarInjectionChecks:
886             emitVarInjectionCheck(needsVarInjectionChecks(resolveType));
887             emitGetClosureVar(scope, *operandSlot);
888             break;
889         case Dynamic:
890             addSlowCase(jump());
891             break;
892         case LocalClosureVar:
893         case ModuleVar:
894         case UnresolvedProperty:
895         case UnresolvedPropertyWithVarInjectionChecks:
896             RELEASE_ASSERT_NOT_REACHED();
897         }
898     };
899
900     switch (resolveType) {
901     case UnresolvedProperty:
902     case UnresolvedPropertyWithVarInjectionChecks: {
903         JumpList skipToEnd;
904         load32(&currentInstruction[4], regT0);
905         and32(TrustedImm32(GetPutInfo::typeBits), regT0); // Load ResolveType into T0
906
907         Jump isGlobalProperty = branch32(Equal, regT0, TrustedImm32(GlobalProperty));
908         Jump notGlobalPropertyWithVarInjections = branch32(NotEqual, regT0, TrustedImm32(GlobalPropertyWithVarInjectionChecks));
909         isGlobalProperty.link(this);
910         emitCode(GlobalProperty, false);
911         skipToEnd.append(jump());
912         notGlobalPropertyWithVarInjections.link(this);
913
914         Jump notGlobalLexicalVar = branch32(NotEqual, regT0, TrustedImm32(GlobalLexicalVar));
915         emitCode(GlobalLexicalVar, true);
916         skipToEnd.append(jump());
917         notGlobalLexicalVar.link(this);
918
919         Jump notGlobalLexicalVarWithVarInjections = branch32(NotEqual, regT0, TrustedImm32(GlobalLexicalVarWithVarInjectionChecks));
920         emitCode(GlobalLexicalVarWithVarInjectionChecks, true);
921         skipToEnd.append(jump());
922         notGlobalLexicalVarWithVarInjections.link(this);
923
924         addSlowCase(jump());
925
926         skipToEnd.link(this);
927         break;
928     }
929
930     default:
931         emitCode(resolveType, false);
932         break;
933     }
934     emitPutVirtualRegister(dst);
935     emitValueProfilingSite();
936 }
937
938 void JIT::emitSlow_op_get_from_scope(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
939 {
940     linkAllSlowCases(iter);
941
942     int dst = currentInstruction[1].u.operand;
943     callOperation(WithProfile, operationGetFromScope, dst, currentInstruction);
944 }
945
946 void JIT::emitPutGlobalVariable(JSValue* operand, int value, WatchpointSet* set)
947 {
948     emitGetVirtualRegister(value, regT0);
949     emitNotifyWrite(set);
950     storePtr(regT0, operand);
951 }
952 void JIT::emitPutGlobalVariableIndirect(JSValue** addressOfOperand, int value, WatchpointSet** indirectWatchpointSet)
953 {
954     emitGetVirtualRegister(value, regT0);
955     loadPtr(indirectWatchpointSet, regT1);
956     emitNotifyWrite(regT1);
957     loadPtr(addressOfOperand, regT1);
958     storePtr(regT0, regT1);
959 }
960
961 void JIT::emitPutClosureVar(int scope, uintptr_t operand, int value, WatchpointSet* set)
962 {
963     emitGetVirtualRegister(value, regT1);
964     emitGetVirtualRegister(scope, regT0);
965     emitNotifyWrite(set);
966     storePtr(regT1, Address(regT0, JSLexicalEnvironment::offsetOfVariables() + operand * sizeof(Register)));
967 }
968
969 void JIT::emit_op_put_to_scope(Instruction* currentInstruction)
970 {
971     int scope = currentInstruction[1].u.operand;
972     int value = currentInstruction[3].u.operand;
973     GetPutInfo getPutInfo = GetPutInfo(copiedInstruction(currentInstruction)[4].u.operand);
974     ResolveType resolveType = getPutInfo.resolveType();
975     Structure** structureSlot = currentInstruction[5].u.structure.slot();
976     uintptr_t* operandSlot = reinterpret_cast<uintptr_t*>(&currentInstruction[6].u.pointer);
977
978     auto emitCode = [&] (ResolveType resolveType, bool indirectLoadForOperand) {
979         switch (resolveType) {
980         case GlobalProperty:
981         case GlobalPropertyWithVarInjectionChecks: {
982             emitLoadWithStructureCheck(scope, structureSlot); // Structure check covers var injection.
983             emitGetVirtualRegister(value, regT2);
984             
985             loadPtr(Address(regT0, JSObject::butterflyOffset()), regT0);
986             loadPtr(operandSlot, regT1);
987             negPtr(regT1);
988             storePtr(regT2, BaseIndex(regT0, regT1, TimesEight, (firstOutOfLineOffset - 2) * sizeof(EncodedJSValue)));
989             emitWriteBarrier(m_codeBlock->globalObject(), value, ShouldFilterValue);
990             break;
991         }
992         case GlobalVar:
993         case GlobalVarWithVarInjectionChecks:
994         case GlobalLexicalVar:
995         case GlobalLexicalVarWithVarInjectionChecks: {
996             JSScope* constantScope = JSScope::constantScopeForCodeBlock(resolveType, m_codeBlock);
997             RELEASE_ASSERT(constantScope);
998             emitVarInjectionCheck(needsVarInjectionChecks(resolveType));
999             if (!isInitialization(getPutInfo.initializationMode()) && (resolveType == GlobalLexicalVar || resolveType == GlobalLexicalVarWithVarInjectionChecks)) {
1000                 // We need to do a TDZ check here because we can't always prove we need to emit TDZ checks statically.
1001                 if (indirectLoadForOperand)
1002                     emitGetVarFromIndirectPointer(bitwise_cast<JSValue**>(operandSlot), regT0);
1003                 else
1004                     emitGetVarFromPointer(bitwise_cast<JSValue*>(*operandSlot), regT0);
1005                 addSlowCase(branchTest64(Zero, regT0));
1006             }
1007             if (indirectLoadForOperand)
1008                 emitPutGlobalVariableIndirect(bitwise_cast<JSValue**>(operandSlot), value, bitwise_cast<WatchpointSet**>(&currentInstruction[5]));
1009             else
1010                 emitPutGlobalVariable(bitwise_cast<JSValue*>(*operandSlot), value, currentInstruction[5].u.watchpointSet);
1011             emitWriteBarrier(constantScope, value, ShouldFilterValue);
1012             break;
1013         }
1014         case LocalClosureVar:
1015         case ClosureVar:
1016         case ClosureVarWithVarInjectionChecks:
1017             emitVarInjectionCheck(needsVarInjectionChecks(resolveType));
1018             emitPutClosureVar(scope, *operandSlot, value, currentInstruction[5].u.watchpointSet);
1019             emitWriteBarrier(scope, value, ShouldFilterValue);
1020             break;
1021         case ModuleVar:
1022         case Dynamic:
1023             addSlowCase(jump());
1024             break;
1025         case UnresolvedProperty:
1026         case UnresolvedPropertyWithVarInjectionChecks:
1027             RELEASE_ASSERT_NOT_REACHED();
1028             break;
1029         }
1030     };
1031
1032     switch (resolveType) {
1033     case UnresolvedProperty:
1034     case UnresolvedPropertyWithVarInjectionChecks: {
1035         JumpList skipToEnd;
1036         load32(&currentInstruction[4], regT0);
1037         and32(TrustedImm32(GetPutInfo::typeBits), regT0); // Load ResolveType into T0
1038
1039         Jump isGlobalProperty = branch32(Equal, regT0, TrustedImm32(GlobalProperty));
1040         Jump notGlobalPropertyWithVarInjections = branch32(NotEqual, regT0, TrustedImm32(GlobalPropertyWithVarInjectionChecks));
1041         isGlobalProperty.link(this);
1042         emitCode(GlobalProperty, false);
1043         skipToEnd.append(jump());
1044         notGlobalPropertyWithVarInjections.link(this);
1045
1046         Jump notGlobalLexicalVar = branch32(NotEqual, regT0, TrustedImm32(GlobalLexicalVar));
1047         emitCode(GlobalLexicalVar, true);
1048         skipToEnd.append(jump());
1049         notGlobalLexicalVar.link(this);
1050
1051         Jump notGlobalLexicalVarWithVarInjections = branch32(NotEqual, regT0, TrustedImm32(GlobalLexicalVarWithVarInjectionChecks));
1052         emitCode(GlobalLexicalVarWithVarInjectionChecks, true);
1053         skipToEnd.append(jump());
1054         notGlobalLexicalVarWithVarInjections.link(this);
1055
1056         addSlowCase(jump());
1057
1058         skipToEnd.link(this);
1059         break;
1060     }
1061
1062     default:
1063         emitCode(resolveType, false);
1064         break;
1065     }
1066 }
1067
1068 void JIT::emitSlow_op_put_to_scope(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
1069 {
1070     linkAllSlowCases(iter);
1071
1072     GetPutInfo getPutInfo = GetPutInfo(copiedInstruction(currentInstruction)[4].u.operand);
1073     ResolveType resolveType = getPutInfo.resolveType();
1074     if (resolveType == ModuleVar) {
1075         JITSlowPathCall slowPathCall(this, currentInstruction, slow_path_throw_strict_mode_readonly_property_write_error);
1076         slowPathCall.call();
1077     } else
1078         callOperation(operationPutToScope, currentInstruction);
1079 }
1080
1081 void JIT::emit_op_get_from_arguments(Instruction* currentInstruction)
1082 {
1083     int dst = currentInstruction[1].u.operand;
1084     int arguments = currentInstruction[2].u.operand;
1085     int index = currentInstruction[3].u.operand;
1086     
1087     emitGetVirtualRegister(arguments, regT0);
1088     load64(Address(regT0, DirectArguments::storageOffset() + index * sizeof(WriteBarrier<Unknown>)), regT0);
1089     emitValueProfilingSite();
1090     emitPutVirtualRegister(dst);
1091 }
1092
1093 void JIT::emit_op_put_to_arguments(Instruction* currentInstruction)
1094 {
1095     int arguments = currentInstruction[1].u.operand;
1096     int index = currentInstruction[2].u.operand;
1097     int value = currentInstruction[3].u.operand;
1098     
1099     emitGetVirtualRegister(arguments, regT0);
1100     emitGetVirtualRegister(value, regT1);
1101     store64(regT1, Address(regT0, DirectArguments::storageOffset() + index * sizeof(WriteBarrier<Unknown>)));
1102
1103     emitWriteBarrier(arguments, value, ShouldFilterValue);
1104 }
1105
1106 #endif // USE(JSVALUE64)
1107
1108 #if USE(JSVALUE64)
1109 void JIT::emitWriteBarrier(unsigned owner, unsigned value, WriteBarrierMode mode)
1110 {
1111     Jump valueNotCell;
1112     if (mode == ShouldFilterValue || mode == ShouldFilterBaseAndValue) {
1113         emitGetVirtualRegister(value, regT0);
1114         valueNotCell = branchTest64(NonZero, regT0, tagMaskRegister);
1115     }
1116     
1117     emitGetVirtualRegister(owner, regT0);
1118     Jump ownerNotCell;
1119     if (mode == ShouldFilterBaseAndValue || mode == ShouldFilterBase)
1120         ownerNotCell = branchTest64(NonZero, regT0, tagMaskRegister);
1121
1122     Jump ownerIsRememberedOrInEden = barrierBranch(*vm(), regT0, regT1);
1123     callOperation(operationWriteBarrierSlowPath, regT0);
1124     ownerIsRememberedOrInEden.link(this);
1125
1126     if (mode == ShouldFilterBaseAndValue || mode == ShouldFilterBase)
1127         ownerNotCell.link(this);
1128     if (mode == ShouldFilterValue || mode == ShouldFilterBaseAndValue) 
1129         valueNotCell.link(this);
1130 }
1131
1132 void JIT::emitWriteBarrier(JSCell* owner, unsigned value, WriteBarrierMode mode)
1133 {
1134     emitGetVirtualRegister(value, regT0);
1135     Jump valueNotCell;
1136     if (mode == ShouldFilterValue)
1137         valueNotCell = branchTest64(NonZero, regT0, tagMaskRegister);
1138
1139     emitWriteBarrier(owner);
1140
1141     if (mode == ShouldFilterValue) 
1142         valueNotCell.link(this);
1143 }
1144
1145 #else // USE(JSVALUE64)
1146
1147 void JIT::emitWriteBarrier(unsigned owner, unsigned value, WriteBarrierMode mode)
1148 {
1149     Jump valueNotCell;
1150     if (mode == ShouldFilterValue || mode == ShouldFilterBaseAndValue) {
1151         emitLoadTag(value, regT0);
1152         valueNotCell = branch32(NotEqual, regT0, TrustedImm32(JSValue::CellTag));
1153     }
1154     
1155     emitLoad(owner, regT0, regT1);
1156     Jump ownerNotCell;
1157     if (mode == ShouldFilterBase || mode == ShouldFilterBaseAndValue)
1158         ownerNotCell = branch32(NotEqual, regT0, TrustedImm32(JSValue::CellTag));
1159
1160     Jump ownerIsRememberedOrInEden = barrierBranch(*vm(), regT1, regT2);
1161     callOperation(operationWriteBarrierSlowPath, regT1);
1162     ownerIsRememberedOrInEden.link(this);
1163
1164     if (mode == ShouldFilterBase || mode == ShouldFilterBaseAndValue)
1165         ownerNotCell.link(this);
1166     if (mode == ShouldFilterValue || mode == ShouldFilterBaseAndValue) 
1167         valueNotCell.link(this);
1168 }
1169
1170 void JIT::emitWriteBarrier(JSCell* owner, unsigned value, WriteBarrierMode mode)
1171 {
1172     Jump valueNotCell;
1173     if (mode == ShouldFilterValue) {
1174         emitLoadTag(value, regT0);
1175         valueNotCell = branch32(NotEqual, regT0, TrustedImm32(JSValue::CellTag));
1176     }
1177
1178     emitWriteBarrier(owner);
1179
1180     if (mode == ShouldFilterValue) 
1181         valueNotCell.link(this);
1182 }
1183
1184 #endif // USE(JSVALUE64)
1185
1186 void JIT::emitWriteBarrier(JSCell* owner)
1187 {
1188     Jump ownerIsRememberedOrInEden = barrierBranch(*vm(), owner, regT0);
1189     callOperation(operationWriteBarrierSlowPath, owner);
1190     ownerIsRememberedOrInEden.link(this);
1191 }
1192
1193 void JIT::emitByValIdentifierCheck(ByValInfo* byValInfo, RegisterID cell, RegisterID scratch, const Identifier& propertyName, JumpList& slowCases)
1194 {
1195     if (propertyName.isSymbol())
1196         slowCases.append(branchPtr(NotEqual, cell, TrustedImmPtr(byValInfo->cachedSymbol.get())));
1197     else {
1198         slowCases.append(branchStructure(NotEqual, Address(cell, JSCell::structureIDOffset()), m_vm->stringStructure.get()));
1199         loadPtr(Address(cell, JSString::offsetOfValue()), scratch);
1200         slowCases.append(branchPtr(NotEqual, scratch, TrustedImmPtr(propertyName.impl())));
1201     }
1202 }
1203
1204 void JIT::privateCompileGetByVal(ByValInfo* byValInfo, ReturnAddressPtr returnAddress, JITArrayMode arrayMode)
1205 {
1206     Instruction* currentInstruction = m_codeBlock->instructions().begin() + byValInfo->bytecodeIndex;
1207     
1208     PatchableJump badType;
1209     JumpList slowCases;
1210     
1211     switch (arrayMode) {
1212     case JITInt32:
1213         slowCases = emitInt32GetByVal(currentInstruction, badType);
1214         break;
1215     case JITDouble:
1216         slowCases = emitDoubleGetByVal(currentInstruction, badType);
1217         break;
1218     case JITContiguous:
1219         slowCases = emitContiguousGetByVal(currentInstruction, badType);
1220         break;
1221     case JITArrayStorage:
1222         slowCases = emitArrayStorageGetByVal(currentInstruction, badType);
1223         break;
1224     case JITDirectArguments:
1225         slowCases = emitDirectArgumentsGetByVal(currentInstruction, badType);
1226         break;
1227     case JITScopedArguments:
1228         slowCases = emitScopedArgumentsGetByVal(currentInstruction, badType);
1229         break;
1230     default:
1231         TypedArrayType type = typedArrayTypeForJITArrayMode(arrayMode);
1232         if (isInt(type))
1233             slowCases = emitIntTypedArrayGetByVal(currentInstruction, badType, type);
1234         else 
1235             slowCases = emitFloatTypedArrayGetByVal(currentInstruction, badType, type);
1236         break;
1237     }
1238     
1239     Jump done = jump();
1240
1241     LinkBuffer patchBuffer(*this, m_codeBlock);
1242     
1243     patchBuffer.link(badType, CodeLocationLabel(MacroAssemblerCodePtr::createFromExecutableAddress(returnAddress.value())).labelAtOffset(byValInfo->returnAddressToSlowPath));
1244     patchBuffer.link(slowCases, CodeLocationLabel(MacroAssemblerCodePtr::createFromExecutableAddress(returnAddress.value())).labelAtOffset(byValInfo->returnAddressToSlowPath));
1245     
1246     patchBuffer.link(done, byValInfo->badTypeJump.labelAtOffset(byValInfo->badTypeJumpToDone));
1247     
1248     byValInfo->stubRoutine = FINALIZE_CODE_FOR_STUB(
1249         m_codeBlock, patchBuffer,
1250         ("Baseline get_by_val stub for %s, return point %p", toCString(*m_codeBlock).data(), returnAddress.value()));
1251     
1252     MacroAssembler::repatchJump(byValInfo->badTypeJump, CodeLocationLabel(byValInfo->stubRoutine->code().code()));
1253     MacroAssembler::repatchCall(CodeLocationCall(MacroAssemblerCodePtr(returnAddress)), FunctionPtr(operationGetByValGeneric));
1254 }
1255
1256 void JIT::privateCompileGetByValWithCachedId(ByValInfo* byValInfo, ReturnAddressPtr returnAddress, const Identifier& propertyName)
1257 {
1258     Instruction* currentInstruction = m_codeBlock->instructions().begin() + byValInfo->bytecodeIndex;
1259
1260     Jump fastDoneCase;
1261     Jump slowDoneCase;
1262     JumpList slowCases;
1263
1264     JITGetByIdGenerator gen = emitGetByValWithCachedId(byValInfo, currentInstruction, propertyName, fastDoneCase, slowDoneCase, slowCases);
1265
1266     ConcurrentJSLocker locker(m_codeBlock->m_lock);
1267     LinkBuffer patchBuffer(*this, m_codeBlock);
1268     patchBuffer.link(slowCases, CodeLocationLabel(MacroAssemblerCodePtr::createFromExecutableAddress(returnAddress.value())).labelAtOffset(byValInfo->returnAddressToSlowPath));
1269     patchBuffer.link(fastDoneCase, byValInfo->badTypeJump.labelAtOffset(byValInfo->badTypeJumpToDone));
1270     patchBuffer.link(slowDoneCase, byValInfo->badTypeJump.labelAtOffset(byValInfo->badTypeJumpToNextHotPath));
1271     if (!m_exceptionChecks.empty())
1272         patchBuffer.link(m_exceptionChecks, byValInfo->exceptionHandler);
1273
1274     for (const auto& callSite : m_calls) {
1275         if (callSite.to)
1276             patchBuffer.link(callSite.from, FunctionPtr(callSite.to));
1277     }
1278     gen.finalize(patchBuffer);
1279
1280     byValInfo->stubRoutine = FINALIZE_CODE_FOR_STUB(
1281         m_codeBlock, patchBuffer,
1282         ("Baseline get_by_val with cached property name '%s' stub for %s, return point %p", propertyName.impl()->utf8().data(), toCString(*m_codeBlock).data(), returnAddress.value()));
1283     byValInfo->stubInfo = gen.stubInfo();
1284
1285     MacroAssembler::repatchJump(byValInfo->notIndexJump, CodeLocationLabel(byValInfo->stubRoutine->code().code()));
1286     MacroAssembler::repatchCall(CodeLocationCall(MacroAssemblerCodePtr(returnAddress)), FunctionPtr(operationGetByValGeneric));
1287 }
1288
1289 void JIT::privateCompilePutByVal(ByValInfo* byValInfo, ReturnAddressPtr returnAddress, JITArrayMode arrayMode)
1290 {
1291     Instruction* currentInstruction = m_codeBlock->instructions().begin() + byValInfo->bytecodeIndex;
1292     
1293     PatchableJump badType;
1294     JumpList slowCases;
1295
1296     bool needsLinkForWriteBarrier = false;
1297
1298     switch (arrayMode) {
1299     case JITInt32:
1300         slowCases = emitInt32PutByVal(currentInstruction, badType);
1301         break;
1302     case JITDouble:
1303         slowCases = emitDoublePutByVal(currentInstruction, badType);
1304         break;
1305     case JITContiguous:
1306         slowCases = emitContiguousPutByVal(currentInstruction, badType);
1307         needsLinkForWriteBarrier = true;
1308         break;
1309     case JITArrayStorage:
1310         slowCases = emitArrayStoragePutByVal(currentInstruction, badType);
1311         needsLinkForWriteBarrier = true;
1312         break;
1313     default:
1314         TypedArrayType type = typedArrayTypeForJITArrayMode(arrayMode);
1315         if (isInt(type))
1316             slowCases = emitIntTypedArrayPutByVal(currentInstruction, badType, type);
1317         else 
1318             slowCases = emitFloatTypedArrayPutByVal(currentInstruction, badType, type);
1319         break;
1320     }
1321     
1322     Jump done = jump();
1323
1324     LinkBuffer patchBuffer(*this, m_codeBlock);
1325     patchBuffer.link(badType, CodeLocationLabel(MacroAssemblerCodePtr::createFromExecutableAddress(returnAddress.value())).labelAtOffset(byValInfo->returnAddressToSlowPath));
1326     patchBuffer.link(slowCases, CodeLocationLabel(MacroAssemblerCodePtr::createFromExecutableAddress(returnAddress.value())).labelAtOffset(byValInfo->returnAddressToSlowPath));
1327     patchBuffer.link(done, byValInfo->badTypeJump.labelAtOffset(byValInfo->badTypeJumpToDone));
1328     if (needsLinkForWriteBarrier) {
1329         ASSERT(m_calls.last().to == operationWriteBarrierSlowPath);
1330         patchBuffer.link(m_calls.last().from, operationWriteBarrierSlowPath);
1331     }
1332     
1333     bool isDirect = Interpreter::getOpcodeID(currentInstruction->u.opcode) == op_put_by_val_direct;
1334     if (!isDirect) {
1335         byValInfo->stubRoutine = FINALIZE_CODE_FOR_STUB(
1336             m_codeBlock, patchBuffer,
1337             ("Baseline put_by_val stub for %s, return point %p", toCString(*m_codeBlock).data(), returnAddress.value()));
1338         
1339     } else {
1340         byValInfo->stubRoutine = FINALIZE_CODE_FOR_STUB(
1341             m_codeBlock, patchBuffer,
1342             ("Baseline put_by_val_direct stub for %s, return point %p", toCString(*m_codeBlock).data(), returnAddress.value()));
1343     }
1344     MacroAssembler::repatchJump(byValInfo->badTypeJump, CodeLocationLabel(byValInfo->stubRoutine->code().code()));
1345     MacroAssembler::repatchCall(CodeLocationCall(MacroAssemblerCodePtr(returnAddress)), FunctionPtr(isDirect ? operationDirectPutByValGeneric : operationPutByValGeneric));
1346 }
1347
1348 void JIT::privateCompilePutByValWithCachedId(ByValInfo* byValInfo, ReturnAddressPtr returnAddress, PutKind putKind, const Identifier& propertyName)
1349 {
1350     Instruction* currentInstruction = m_codeBlock->instructions().begin() + byValInfo->bytecodeIndex;
1351
1352     JumpList doneCases;
1353     JumpList slowCases;
1354
1355     JITPutByIdGenerator gen = emitPutByValWithCachedId(byValInfo, currentInstruction, putKind, propertyName, doneCases, slowCases);
1356
1357     ConcurrentJSLocker locker(m_codeBlock->m_lock);
1358     LinkBuffer patchBuffer(*this, m_codeBlock);
1359     patchBuffer.link(slowCases, CodeLocationLabel(MacroAssemblerCodePtr::createFromExecutableAddress(returnAddress.value())).labelAtOffset(byValInfo->returnAddressToSlowPath));
1360     patchBuffer.link(doneCases, byValInfo->badTypeJump.labelAtOffset(byValInfo->badTypeJumpToDone));
1361     if (!m_exceptionChecks.empty())
1362         patchBuffer.link(m_exceptionChecks, byValInfo->exceptionHandler);
1363
1364     for (const auto& callSite : m_calls) {
1365         if (callSite.to)
1366             patchBuffer.link(callSite.from, FunctionPtr(callSite.to));
1367     }
1368     gen.finalize(patchBuffer);
1369
1370     byValInfo->stubRoutine = FINALIZE_CODE_FOR_STUB(
1371         m_codeBlock, patchBuffer,
1372         ("Baseline put_by_val%s with cached property name '%s' stub for %s, return point %p", (putKind == Direct) ? "_direct" : "", propertyName.impl()->utf8().data(), toCString(*m_codeBlock).data(), returnAddress.value()));
1373     byValInfo->stubInfo = gen.stubInfo();
1374
1375     MacroAssembler::repatchJump(byValInfo->notIndexJump, CodeLocationLabel(byValInfo->stubRoutine->code().code()));
1376     MacroAssembler::repatchCall(CodeLocationCall(MacroAssemblerCodePtr(returnAddress)), FunctionPtr(putKind == Direct ? operationDirectPutByValGeneric : operationPutByValGeneric));
1377 }
1378
1379
1380 JIT::JumpList JIT::emitDirectArgumentsGetByVal(Instruction*, PatchableJump& badType)
1381 {
1382     JumpList slowCases;
1383     
1384 #if USE(JSVALUE64)
1385     RegisterID base = regT0;
1386     RegisterID property = regT1;
1387     JSValueRegs result = JSValueRegs(regT0);
1388     RegisterID scratch = regT3;
1389 #else
1390     RegisterID base = regT0;
1391     RegisterID property = regT2;
1392     JSValueRegs result = JSValueRegs(regT1, regT0);
1393     RegisterID scratch = regT3;
1394 #endif
1395
1396     load8(Address(base, JSCell::typeInfoTypeOffset()), scratch);
1397     badType = patchableBranch32(NotEqual, scratch, TrustedImm32(DirectArgumentsType));
1398     
1399     slowCases.append(branch32(AboveOrEqual, property, Address(base, DirectArguments::offsetOfLength())));
1400     slowCases.append(branchTestPtr(NonZero, Address(base, DirectArguments::offsetOfMappedArguments())));
1401     
1402     zeroExtend32ToPtr(property, scratch);
1403     loadValue(BaseIndex(base, scratch, TimesEight, DirectArguments::storageOffset()), result);
1404     
1405     return slowCases;
1406 }
1407
1408 JIT::JumpList JIT::emitScopedArgumentsGetByVal(Instruction*, PatchableJump& badType)
1409 {
1410     JumpList slowCases;
1411     
1412 #if USE(JSVALUE64)
1413     RegisterID base = regT0;
1414     RegisterID property = regT1;
1415     JSValueRegs result = JSValueRegs(regT0);
1416     RegisterID scratch = regT3;
1417     RegisterID scratch2 = regT4;
1418 #else
1419     RegisterID base = regT0;
1420     RegisterID property = regT2;
1421     JSValueRegs result = JSValueRegs(regT1, regT0);
1422     RegisterID scratch = regT3;
1423     RegisterID scratch2 = regT4;
1424 #endif
1425
1426     load8(Address(base, JSCell::typeInfoTypeOffset()), scratch);
1427     badType = patchableBranch32(NotEqual, scratch, TrustedImm32(ScopedArgumentsType));
1428     slowCases.append(branch32(AboveOrEqual, property, Address(base, ScopedArguments::offsetOfTotalLength())));
1429     
1430     loadPtr(Address(base, ScopedArguments::offsetOfTable()), scratch);
1431     load32(Address(scratch, ScopedArgumentsTable::offsetOfLength()), scratch2);
1432     Jump overflowCase = branch32(AboveOrEqual, property, scratch2);
1433     loadPtr(Address(base, ScopedArguments::offsetOfScope()), scratch2);
1434     loadPtr(Address(scratch, ScopedArgumentsTable::offsetOfArguments()), scratch);
1435     load32(BaseIndex(scratch, property, TimesFour), scratch);
1436     slowCases.append(branch32(Equal, scratch, TrustedImm32(ScopeOffset::invalidOffset)));
1437     loadValue(BaseIndex(scratch2, scratch, TimesEight, JSLexicalEnvironment::offsetOfVariables()), result);
1438     Jump done = jump();
1439     overflowCase.link(this);
1440     sub32(property, scratch2);
1441     neg32(scratch2);
1442     loadValue(BaseIndex(base, scratch2, TimesEight, ScopedArguments::overflowStorageOffset()), result);
1443     slowCases.append(branchIfEmpty(result));
1444     done.link(this);
1445     
1446     return slowCases;
1447 }
1448
1449 JIT::JumpList JIT::emitIntTypedArrayGetByVal(Instruction*, PatchableJump& badType, TypedArrayType type)
1450 {
1451     ASSERT(isInt(type));
1452     
1453     // The best way to test the array type is to use the classInfo. We need to do so without
1454     // clobbering the register that holds the indexing type, base, and property.
1455
1456 #if USE(JSVALUE64)
1457     RegisterID base = regT0;
1458     RegisterID property = regT1;
1459     RegisterID resultPayload = regT0;
1460     RegisterID scratch = regT3;
1461     RegisterID scratch2 = regT4;
1462 #else
1463     RegisterID base = regT0;
1464     RegisterID property = regT2;
1465     RegisterID resultPayload = regT0;
1466     RegisterID resultTag = regT1;
1467     RegisterID scratch = regT3;
1468     RegisterID scratch2 = regT4;
1469 #endif
1470     
1471     JumpList slowCases;
1472     
1473     load8(Address(base, JSCell::typeInfoTypeOffset()), scratch);
1474     badType = patchableBranch32(NotEqual, scratch, TrustedImm32(typeForTypedArrayType(type)));
1475     slowCases.append(branch32(AboveOrEqual, property, Address(base, JSArrayBufferView::offsetOfLength())));
1476     loadPtr(Address(base, JSArrayBufferView::offsetOfVector()), scratch);
1477     cageConditionally(Gigacage::Primitive, scratch, scratch2);
1478     
1479     switch (elementSize(type)) {
1480     case 1:
1481         if (JSC::isSigned(type))
1482             load8SignedExtendTo32(BaseIndex(scratch, property, TimesOne), resultPayload);
1483         else
1484             load8(BaseIndex(scratch, property, TimesOne), resultPayload);
1485         break;
1486     case 2:
1487         if (JSC::isSigned(type))
1488             load16SignedExtendTo32(BaseIndex(scratch, property, TimesTwo), resultPayload);
1489         else
1490             load16(BaseIndex(scratch, property, TimesTwo), resultPayload);
1491         break;
1492     case 4:
1493         load32(BaseIndex(scratch, property, TimesFour), resultPayload);
1494         break;
1495     default:
1496         CRASH();
1497     }
1498     
1499     Jump done;
1500     if (type == TypeUint32) {
1501         Jump canBeInt = branch32(GreaterThanOrEqual, resultPayload, TrustedImm32(0));
1502         
1503         convertInt32ToDouble(resultPayload, fpRegT0);
1504         addDouble(AbsoluteAddress(&twoToThe32), fpRegT0);
1505 #if USE(JSVALUE64)
1506         moveDoubleTo64(fpRegT0, resultPayload);
1507         sub64(tagTypeNumberRegister, resultPayload);
1508 #else
1509         moveDoubleToInts(fpRegT0, resultPayload, resultTag);
1510 #endif
1511         
1512         done = jump();
1513         canBeInt.link(this);
1514     }
1515
1516 #if USE(JSVALUE64)
1517     or64(tagTypeNumberRegister, resultPayload);
1518 #else
1519     move(TrustedImm32(JSValue::Int32Tag), resultTag);
1520 #endif
1521     if (done.isSet())
1522         done.link(this);
1523     return slowCases;
1524 }
1525
1526 JIT::JumpList JIT::emitFloatTypedArrayGetByVal(Instruction*, PatchableJump& badType, TypedArrayType type)
1527 {
1528     ASSERT(isFloat(type));
1529     
1530 #if USE(JSVALUE64)
1531     RegisterID base = regT0;
1532     RegisterID property = regT1;
1533     RegisterID resultPayload = regT0;
1534     RegisterID scratch = regT3;
1535     RegisterID scratch2 = regT4;
1536 #else
1537     RegisterID base = regT0;
1538     RegisterID property = regT2;
1539     RegisterID resultPayload = regT0;
1540     RegisterID resultTag = regT1;
1541     RegisterID scratch = regT3;
1542     RegisterID scratch2 = regT4;
1543 #endif
1544     
1545     JumpList slowCases;
1546
1547     load8(Address(base, JSCell::typeInfoTypeOffset()), scratch);
1548     badType = patchableBranch32(NotEqual, scratch, TrustedImm32(typeForTypedArrayType(type)));
1549     slowCases.append(branch32(AboveOrEqual, property, Address(base, JSArrayBufferView::offsetOfLength())));
1550     loadPtr(Address(base, JSArrayBufferView::offsetOfVector()), scratch);
1551     cageConditionally(Gigacage::Primitive, scratch, scratch2);
1552     
1553     switch (elementSize(type)) {
1554     case 4:
1555         loadFloat(BaseIndex(scratch, property, TimesFour), fpRegT0);
1556         convertFloatToDouble(fpRegT0, fpRegT0);
1557         break;
1558     case 8: {
1559         loadDouble(BaseIndex(scratch, property, TimesEight), fpRegT0);
1560         break;
1561     }
1562     default:
1563         CRASH();
1564     }
1565     
1566     Jump notNaN = branchDouble(DoubleEqual, fpRegT0, fpRegT0);
1567     static const double NaN = PNaN;
1568     loadDouble(TrustedImmPtr(&NaN), fpRegT0);
1569     notNaN.link(this);
1570     
1571 #if USE(JSVALUE64)
1572     moveDoubleTo64(fpRegT0, resultPayload);
1573     sub64(tagTypeNumberRegister, resultPayload);
1574 #else
1575     moveDoubleToInts(fpRegT0, resultPayload, resultTag);
1576 #endif
1577     return slowCases;    
1578 }
1579
1580 JIT::JumpList JIT::emitIntTypedArrayPutByVal(Instruction* currentInstruction, PatchableJump& badType, TypedArrayType type)
1581 {
1582     ArrayProfile* profile = currentInstruction[4].u.arrayProfile;
1583     ASSERT(isInt(type));
1584     
1585     int value = currentInstruction[3].u.operand;
1586
1587 #if USE(JSVALUE64)
1588     RegisterID base = regT0;
1589     RegisterID property = regT1;
1590     RegisterID earlyScratch = regT3;
1591     RegisterID lateScratch = regT2;
1592     RegisterID lateScratch2 = regT4;
1593 #else
1594     RegisterID base = regT0;
1595     RegisterID property = regT2;
1596     RegisterID earlyScratch = regT3;
1597     RegisterID lateScratch = regT1;
1598     RegisterID lateScratch2 = regT4;
1599 #endif
1600     
1601     JumpList slowCases;
1602     
1603     load8(Address(base, JSCell::typeInfoTypeOffset()), earlyScratch);
1604     badType = patchableBranch32(NotEqual, earlyScratch, TrustedImm32(typeForTypedArrayType(type)));
1605     Jump inBounds = branch32(Below, property, Address(base, JSArrayBufferView::offsetOfLength()));
1606     emitArrayProfileOutOfBoundsSpecialCase(profile);
1607     slowCases.append(jump());
1608     inBounds.link(this);
1609     
1610 #if USE(JSVALUE64)
1611     emitGetVirtualRegister(value, earlyScratch);
1612     slowCases.append(emitJumpIfNotInt(earlyScratch));
1613 #else
1614     emitLoad(value, lateScratch, earlyScratch);
1615     slowCases.append(branch32(NotEqual, lateScratch, TrustedImm32(JSValue::Int32Tag)));
1616 #endif
1617     
1618     // We would be loading this into base as in get_by_val, except that the slow
1619     // path expects the base to be unclobbered.
1620     loadPtr(Address(base, JSArrayBufferView::offsetOfVector()), lateScratch);
1621     cageConditionally(Gigacage::Primitive, lateScratch, lateScratch2);
1622     
1623     if (isClamped(type)) {
1624         ASSERT(elementSize(type) == 1);
1625         ASSERT(!JSC::isSigned(type));
1626         Jump inBounds = branch32(BelowOrEqual, earlyScratch, TrustedImm32(0xff));
1627         Jump tooBig = branch32(GreaterThan, earlyScratch, TrustedImm32(0xff));
1628         xor32(earlyScratch, earlyScratch);
1629         Jump clamped = jump();
1630         tooBig.link(this);
1631         move(TrustedImm32(0xff), earlyScratch);
1632         clamped.link(this);
1633         inBounds.link(this);
1634     }
1635     
1636     switch (elementSize(type)) {
1637     case 1:
1638         store8(earlyScratch, BaseIndex(lateScratch, property, TimesOne));
1639         break;
1640     case 2:
1641         store16(earlyScratch, BaseIndex(lateScratch, property, TimesTwo));
1642         break;
1643     case 4:
1644         store32(earlyScratch, BaseIndex(lateScratch, property, TimesFour));
1645         break;
1646     default:
1647         CRASH();
1648     }
1649     
1650     return slowCases;
1651 }
1652
1653 JIT::JumpList JIT::emitFloatTypedArrayPutByVal(Instruction* currentInstruction, PatchableJump& badType, TypedArrayType type)
1654 {
1655     ArrayProfile* profile = currentInstruction[4].u.arrayProfile;
1656     ASSERT(isFloat(type));
1657     
1658     int value = currentInstruction[3].u.operand;
1659
1660 #if USE(JSVALUE64)
1661     RegisterID base = regT0;
1662     RegisterID property = regT1;
1663     RegisterID earlyScratch = regT3;
1664     RegisterID lateScratch = regT2;
1665     RegisterID lateScratch2 = regT4;
1666 #else
1667     RegisterID base = regT0;
1668     RegisterID property = regT2;
1669     RegisterID earlyScratch = regT3;
1670     RegisterID lateScratch = regT1;
1671     RegisterID lateScratch2 = regT4;
1672 #endif
1673     
1674     JumpList slowCases;
1675     
1676     load8(Address(base, JSCell::typeInfoTypeOffset()), earlyScratch);
1677     badType = patchableBranch32(NotEqual, earlyScratch, TrustedImm32(typeForTypedArrayType(type)));
1678     Jump inBounds = branch32(Below, property, Address(base, JSArrayBufferView::offsetOfLength()));
1679     emitArrayProfileOutOfBoundsSpecialCase(profile);
1680     slowCases.append(jump());
1681     inBounds.link(this);
1682     
1683 #if USE(JSVALUE64)
1684     emitGetVirtualRegister(value, earlyScratch);
1685     Jump doubleCase = emitJumpIfNotInt(earlyScratch);
1686     convertInt32ToDouble(earlyScratch, fpRegT0);
1687     Jump ready = jump();
1688     doubleCase.link(this);
1689     slowCases.append(emitJumpIfNotNumber(earlyScratch));
1690     add64(tagTypeNumberRegister, earlyScratch);
1691     move64ToDouble(earlyScratch, fpRegT0);
1692     ready.link(this);
1693 #else
1694     emitLoad(value, lateScratch, earlyScratch);
1695     Jump doubleCase = branch32(NotEqual, lateScratch, TrustedImm32(JSValue::Int32Tag));
1696     convertInt32ToDouble(earlyScratch, fpRegT0);
1697     Jump ready = jump();
1698     doubleCase.link(this);
1699     slowCases.append(branch32(Above, lateScratch, TrustedImm32(JSValue::LowestTag)));
1700     moveIntsToDouble(earlyScratch, lateScratch, fpRegT0, fpRegT1);
1701     ready.link(this);
1702 #endif
1703     
1704     // We would be loading this into base as in get_by_val, except that the slow
1705     // path expects the base to be unclobbered.
1706     loadPtr(Address(base, JSArrayBufferView::offsetOfVector()), lateScratch);
1707     cageConditionally(Gigacage::Primitive, lateScratch, lateScratch2);
1708     
1709     switch (elementSize(type)) {
1710     case 4:
1711         convertDoubleToFloat(fpRegT0, fpRegT0);
1712         storeFloat(fpRegT0, BaseIndex(lateScratch, property, TimesFour));
1713         break;
1714     case 8:
1715         storeDouble(fpRegT0, BaseIndex(lateScratch, property, TimesEight));
1716         break;
1717     default:
1718         CRASH();
1719     }
1720     
1721     return slowCases;
1722 }
1723
1724 } // namespace JSC
1725
1726 #endif // ENABLE(JIT)