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