It should be possible to jettison JIT stub routines even if they are currently running
[WebKit-https.git] / Source / JavaScriptCore / jit / JITPropertyAccess.cpp
1 /*
2  * Copyright (C) 2008, 2009 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 "GCAwareJITStubRoutine.h"
33 #include "GetterSetter.h"
34 #include "Interpreter.h"
35 #include "JITInlineMethods.h"
36 #include "JITStubCall.h"
37 #include "JSArray.h"
38 #include "JSFunction.h"
39 #include "JSPropertyNameIterator.h"
40 #include "JSVariableObject.h"
41 #include "LinkBuffer.h"
42 #include "RepatchBuffer.h"
43 #include "ResultType.h"
44 #include "SamplingTool.h"
45
46 #ifndef NDEBUG
47 #include <stdio.h>
48 #endif
49
50 using namespace std;
51
52 namespace JSC {
53 #if USE(JSVALUE64)
54
55 JIT::CodeRef JIT::stringGetByValStubGenerator(JSGlobalData* globalData)
56 {
57     JSInterfaceJIT jit;
58     JumpList failures;
59     failures.append(jit.branchPtr(NotEqual, Address(regT0, JSCell::classInfoOffset()), TrustedImmPtr(&JSString::s_info)));
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, ThunkHelpers::stringImplFlagsOffset()), regT2);
74     jit.loadPtr(Address(regT0, ThunkHelpers::stringImplDataOffset()), regT0);
75     is16Bit.append(jit.branchTest32(Zero, regT2, TrustedImm32(ThunkHelpers::stringImpl8BitFlag())));
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(globalData->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(*globalData, &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     unsigned dst = currentInstruction[1].u.operand;
98     unsigned base = currentInstruction[2].u.operand;
99     unsigned property = currentInstruction[3].u.operand;
100
101     emitGetVirtualRegisters(base, regT0, property, regT1);
102     emitJumpSlowCaseIfNotImmediateInteger(regT1);
103
104     // This is technically incorrect - we're zero-extending an int32.  On the hot path this doesn't matter.
105     // We check the value as if it was a uint32 against the m_vectorLength - which will always fail if
106     // number was signed since m_vectorLength is always less than intmax (since the total allocation
107     // size is always less than 4Gb).  As such zero extending wil have been correct (and extending the value
108     // to 64-bits is necessary since it's used in the address calculation.  We zero extend rather than sign
109     // extending since it makes it easier to re-tag the value in the slow case.
110     zeroExtend32ToPtr(regT1, regT1);
111
112     emitJumpSlowCaseIfNotJSCell(regT0, base);
113     addSlowCase(branchPtr(NotEqual, Address(regT0, JSCell::classInfoOffset()), TrustedImmPtr(&JSArray::s_info)));
114
115     loadPtr(Address(regT0, JSArray::storageOffset()), regT2);
116     addSlowCase(branch32(AboveOrEqual, regT1, Address(regT0, JSArray::vectorLengthOffset())));
117
118     loadPtr(BaseIndex(regT2, regT1, ScalePtr, OBJECT_OFFSETOF(ArrayStorage, m_vector[0])), regT0);
119     addSlowCase(branchTestPtr(Zero, regT0));
120
121     emitValueProfilingSite();
122     emitPutVirtualRegister(dst);
123 }
124
125 void JIT::emitSlow_op_get_by_val(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
126 {
127     unsigned dst = currentInstruction[1].u.operand;
128     unsigned base = currentInstruction[2].u.operand;
129     unsigned property = currentInstruction[3].u.operand;
130     
131     linkSlowCase(iter); // property int32 check
132     linkSlowCaseIfNotJSCell(iter, base); // base cell check
133     Jump nonCell = jump();
134     linkSlowCase(iter); // base array check
135     Jump notString = branchPtr(NotEqual, Address(regT0, JSCell::classInfoOffset()), TrustedImmPtr(&JSString::s_info));
136     emitNakedCall(CodeLocationLabel(m_globalData->getCTIStub(stringGetByValStubGenerator).code()));
137     Jump failed = branchTestPtr(Zero, regT0);
138     emitPutVirtualRegister(dst, regT0);
139     emitJumpSlowToHot(jump(), OPCODE_LENGTH(op_get_by_val));
140     failed.link(this);
141     notString.link(this);
142     nonCell.link(this);
143     
144     linkSlowCase(iter); // vector length check
145     linkSlowCase(iter); // empty value
146     
147     JITStubCall stubCall(this, cti_op_get_by_val);
148     stubCall.addArgument(base, regT2);
149     stubCall.addArgument(property, regT2);
150     stubCall.call(dst);
151
152     emitValueProfilingSite();
153 }
154
155 void JIT::compileGetDirectOffset(RegisterID base, RegisterID result, RegisterID offset, RegisterID scratch, FinalObjectMode finalObjectMode)
156 {
157     ASSERT(sizeof(JSValue) == 8);
158     
159     if (finalObjectMode == MayBeFinal) {
160         Jump isInline = branch32(LessThan, offset, TrustedImm32(inlineStorageCapacity));
161         loadPtr(Address(base, JSObject::offsetOfOutOfLineStorage()), scratch);
162         Jump done = jump();
163         isInline.link(this);
164         addPtr(TrustedImm32(JSObject::offsetOfInlineStorage() + inlineStorageCapacity * sizeof(EncodedJSValue)), base, scratch);
165         done.link(this);
166     } else {
167 #if !ASSERT_DISABLED
168         Jump isOutOfLine = branch32(GreaterThanOrEqual, offset, TrustedImm32(inlineStorageCapacity));
169         breakpoint();
170         isOutOfLine.link(this);
171 #endif
172         loadPtr(Address(base, JSObject::offsetOfOutOfLineStorage()), scratch);
173     }
174     loadPtr(BaseIndex(scratch, offset, ScalePtr, -inlineStorageCapacity * static_cast<ptrdiff_t>(sizeof(JSValue))), result);
175 }
176
177 void JIT::emit_op_get_by_pname(Instruction* currentInstruction)
178 {
179     unsigned dst = currentInstruction[1].u.operand;
180     unsigned base = currentInstruction[2].u.operand;
181     unsigned property = currentInstruction[3].u.operand;
182     unsigned expected = currentInstruction[4].u.operand;
183     unsigned iter = currentInstruction[5].u.operand;
184     unsigned i = currentInstruction[6].u.operand;
185
186     emitGetVirtualRegister(property, regT0);
187     addSlowCase(branchPtr(NotEqual, regT0, addressFor(expected)));
188     emitGetVirtualRegisters(base, regT0, iter, regT1);
189     emitJumpSlowCaseIfNotJSCell(regT0, base);
190
191     // Test base's structure
192     loadPtr(Address(regT0, JSCell::structureOffset()), regT2);
193     addSlowCase(branchPtr(NotEqual, regT2, Address(regT1, OBJECT_OFFSETOF(JSPropertyNameIterator, m_cachedStructure))));
194     load32(addressFor(i), regT3);
195     sub32(TrustedImm32(1), regT3);
196     addSlowCase(branch32(AboveOrEqual, regT3, Address(regT1, OBJECT_OFFSETOF(JSPropertyNameIterator, m_numCacheableSlots))));
197     add32(Address(regT1, OBJECT_OFFSETOF(JSPropertyNameIterator, m_offsetBase)), regT3);
198     compileGetDirectOffset(regT0, regT0, regT3, regT1);
199
200     emitPutVirtualRegister(dst, regT0);
201 }
202
203 void JIT::emitSlow_op_get_by_pname(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
204 {
205     unsigned dst = currentInstruction[1].u.operand;
206     unsigned base = currentInstruction[2].u.operand;
207     unsigned property = currentInstruction[3].u.operand;
208
209     linkSlowCase(iter);
210     linkSlowCaseIfNotJSCell(iter, base);
211     linkSlowCase(iter);
212     linkSlowCase(iter);
213
214     JITStubCall stubCall(this, cti_op_get_by_val);
215     stubCall.addArgument(base, regT2);
216     stubCall.addArgument(property, regT2);
217     stubCall.call(dst);
218 }
219
220 void JIT::emit_op_put_by_val(Instruction* currentInstruction)
221 {
222     unsigned base = currentInstruction[1].u.operand;
223     unsigned property = currentInstruction[2].u.operand;
224     unsigned value = currentInstruction[3].u.operand;
225
226     emitGetVirtualRegisters(base, regT0, property, regT1);
227     emitJumpSlowCaseIfNotImmediateInteger(regT1);
228     // See comment in op_get_by_val.
229     zeroExtend32ToPtr(regT1, regT1);
230     emitJumpSlowCaseIfNotJSCell(regT0, base);
231     addSlowCase(branchPtr(NotEqual, Address(regT0, JSCell::classInfoOffset()), TrustedImmPtr(&JSArray::s_info)));
232     addSlowCase(branch32(AboveOrEqual, regT1, Address(regT0, JSArray::vectorLengthOffset())));
233
234     loadPtr(Address(regT0, JSArray::storageOffset()), regT2);
235     Jump empty = branchTestPtr(Zero, BaseIndex(regT2, regT1, ScalePtr, OBJECT_OFFSETOF(ArrayStorage, m_vector[0])));
236
237     Label storeResult(this);
238     emitGetVirtualRegister(value, regT3);
239     storePtr(regT3, BaseIndex(regT2, regT1, ScalePtr, OBJECT_OFFSETOF(ArrayStorage, m_vector[0])));
240     Jump end = jump();
241     
242     empty.link(this);
243     add32(TrustedImm32(1), Address(regT2, OBJECT_OFFSETOF(ArrayStorage, m_numValuesInVector)));
244     branch32(Below, regT1, Address(regT2, OBJECT_OFFSETOF(ArrayStorage, m_length))).linkTo(storeResult, this);
245
246     add32(TrustedImm32(1), regT1);
247     store32(regT1, Address(regT2, OBJECT_OFFSETOF(ArrayStorage, m_length)));
248     sub32(TrustedImm32(1), regT1);
249     jump().linkTo(storeResult, this);
250
251     end.link(this);
252
253     emitWriteBarrier(regT0, regT3, regT1, regT3, ShouldFilterImmediates, WriteBarrierForPropertyAccess);
254 }
255
256 void JIT::emitSlow_op_put_by_val(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
257 {
258     unsigned base = currentInstruction[1].u.operand;
259     unsigned property = currentInstruction[2].u.operand;
260     unsigned value = currentInstruction[3].u.operand;
261
262     linkSlowCase(iter); // property int32 check
263     linkSlowCaseIfNotJSCell(iter, base); // base cell check
264     linkSlowCase(iter); // base not array check
265     linkSlowCase(iter); // in vector check
266
267     JITStubCall stubPutByValCall(this, cti_op_put_by_val);
268     stubPutByValCall.addArgument(regT0);
269     stubPutByValCall.addArgument(property, regT2);
270     stubPutByValCall.addArgument(value, regT2);
271     stubPutByValCall.call();
272 }
273
274 void JIT::emit_op_put_by_index(Instruction* currentInstruction)
275 {
276     JITStubCall stubCall(this, cti_op_put_by_index);
277     stubCall.addArgument(currentInstruction[1].u.operand, regT2);
278     stubCall.addArgument(TrustedImm32(currentInstruction[2].u.operand));
279     stubCall.addArgument(currentInstruction[3].u.operand, regT2);
280     stubCall.call();
281 }
282
283 void JIT::emit_op_put_getter_setter(Instruction* currentInstruction)
284 {
285     JITStubCall stubCall(this, cti_op_put_getter_setter);
286     stubCall.addArgument(currentInstruction[1].u.operand, regT2);
287     stubCall.addArgument(TrustedImmPtr(&m_codeBlock->identifier(currentInstruction[2].u.operand)));
288     stubCall.addArgument(currentInstruction[3].u.operand, regT2);
289     stubCall.addArgument(currentInstruction[4].u.operand, regT2);
290     stubCall.call();
291 }
292
293 void JIT::emit_op_del_by_id(Instruction* currentInstruction)
294 {
295     JITStubCall stubCall(this, cti_op_del_by_id);
296     stubCall.addArgument(currentInstruction[2].u.operand, regT2);
297     stubCall.addArgument(TrustedImmPtr(&m_codeBlock->identifier(currentInstruction[3].u.operand)));
298     stubCall.call(currentInstruction[1].u.operand);
299 }
300
301 void JIT::emit_op_method_check(Instruction* currentInstruction)
302 {
303     // Assert that the following instruction is a get_by_id.
304     ASSERT(m_interpreter->getOpcodeID((currentInstruction + OPCODE_LENGTH(op_method_check))->u.opcode) == op_get_by_id
305         || m_interpreter->getOpcodeID((currentInstruction + OPCODE_LENGTH(op_method_check))->u.opcode) == op_get_by_id_out_of_line);
306
307     currentInstruction += OPCODE_LENGTH(op_method_check);
308     unsigned resultVReg = currentInstruction[1].u.operand;
309     unsigned baseVReg = currentInstruction[2].u.operand;
310     Identifier* ident = &(m_codeBlock->identifier(currentInstruction[3].u.operand));
311
312     emitGetVirtualRegister(baseVReg, regT0);
313
314     // Do the method check - check the object & its prototype's structure inline (this is the common case).
315     m_methodCallCompilationInfo.append(MethodCallCompilationInfo(m_bytecodeOffset, m_propertyAccessCompilationInfo.size()));
316     MethodCallCompilationInfo& info = m_methodCallCompilationInfo.last();
317
318     Jump notCell = emitJumpIfNotJSCell(regT0);
319
320     BEGIN_UNINTERRUPTED_SEQUENCE(sequenceMethodCheck);
321
322     Jump structureCheck = branchPtrWithPatch(NotEqual, Address(regT0, JSCell::structureOffset()), info.structureToCompare, TrustedImmPtr(reinterpret_cast<void*>(patchGetByIdDefaultStructure)));
323     DataLabelPtr protoStructureToCompare, protoObj = moveWithPatch(TrustedImmPtr(0), regT1);
324     Jump protoStructureCheck = branchPtrWithPatch(NotEqual, Address(regT1, JSCell::structureOffset()), protoStructureToCompare, TrustedImmPtr(reinterpret_cast<void*>(patchGetByIdDefaultStructure)));
325
326     // This will be relinked to load the function without doing a load.
327     DataLabelPtr putFunction = moveWithPatch(TrustedImmPtr(0), regT0);
328
329     END_UNINTERRUPTED_SEQUENCE(sequenceMethodCheck);
330
331     Jump match = jump();
332
333     // Link the failure cases here.
334     notCell.link(this);
335     structureCheck.link(this);
336     protoStructureCheck.link(this);
337
338     // Do a regular(ish) get_by_id (the slow case will be link to
339     // cti_op_get_by_id_method_check instead of cti_op_get_by_id.
340     compileGetByIdHotPath(baseVReg, ident);
341
342     match.link(this);
343     emitValueProfilingSite(m_bytecodeOffset + OPCODE_LENGTH(op_method_check));
344     emitPutVirtualRegister(resultVReg);
345
346     // We've already generated the following get_by_id, so make sure it's skipped over.
347     m_bytecodeOffset += OPCODE_LENGTH(op_get_by_id);
348
349     m_propertyAccessCompilationInfo.last().addMethodCheckInfo(info.structureToCompare, protoObj, protoStructureToCompare, putFunction);
350 }
351
352 void JIT::emitSlow_op_method_check(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
353 {
354     currentInstruction += OPCODE_LENGTH(op_method_check);
355     unsigned resultVReg = currentInstruction[1].u.operand;
356     unsigned baseVReg = currentInstruction[2].u.operand;
357     Identifier* ident = &(m_codeBlock->identifier(currentInstruction[3].u.operand));
358
359     compileGetByIdSlowCase(resultVReg, baseVReg, ident, iter, true);
360     emitValueProfilingSite(m_bytecodeOffset + OPCODE_LENGTH(op_method_check));
361
362     // We've already generated the following get_by_id, so make sure it's skipped over.
363     m_bytecodeOffset += OPCODE_LENGTH(op_get_by_id);
364 }
365
366 void JIT::emit_op_get_by_id(Instruction* currentInstruction)
367 {
368     unsigned resultVReg = currentInstruction[1].u.operand;
369     unsigned baseVReg = currentInstruction[2].u.operand;
370     Identifier* ident = &(m_codeBlock->identifier(currentInstruction[3].u.operand));
371
372     emitGetVirtualRegister(baseVReg, regT0);
373     compileGetByIdHotPath(baseVReg, ident);
374     emitValueProfilingSite();
375     emitPutVirtualRegister(resultVReg);
376 }
377
378 void JIT::compileGetByIdHotPath(int baseVReg, Identifier*)
379 {
380     // As for put_by_id, get_by_id requires the offset of the Structure and the offset of the access to be patched.
381     // Additionally, for get_by_id we need patch the offset of the branch to the slow case (we patch this to jump
382     // to array-length / prototype access tranpolines, and finally we also the the property-map access offset as a label
383     // to jump back to if one of these trampolies finds a match.
384
385     emitJumpSlowCaseIfNotJSCell(regT0, baseVReg);
386
387     BEGIN_UNINTERRUPTED_SEQUENCE(sequenceGetByIdHotPath);
388
389     Label hotPathBegin(this);
390
391     DataLabelPtr structureToCompare;
392     PatchableJump structureCheck = patchableBranchPtrWithPatch(NotEqual, Address(regT0, JSCell::structureOffset()), structureToCompare, TrustedImmPtr(reinterpret_cast<void*>(patchGetByIdDefaultStructure)));
393     addSlowCase(structureCheck);
394
395     ConvertibleLoadLabel propertyStorageLoad = convertibleLoadPtr(Address(regT0, JSObject::offsetOfOutOfLineStorage()), regT0);
396     DataLabelCompact displacementLabel = loadPtrWithCompactAddressOffsetPatch(Address(regT0, patchGetByIdDefaultOffset), regT0);
397
398     Label putResult(this);
399
400     END_UNINTERRUPTED_SEQUENCE(sequenceGetByIdHotPath);
401
402     m_propertyAccessCompilationInfo.append(PropertyStubCompilationInfo(PropertyStubGetById, m_bytecodeOffset, hotPathBegin, structureToCompare, structureCheck, propertyStorageLoad, displacementLabel, putResult));
403 }
404
405 void JIT::emitSlow_op_get_by_id(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
406 {
407     unsigned resultVReg = currentInstruction[1].u.operand;
408     unsigned baseVReg = currentInstruction[2].u.operand;
409     Identifier* ident = &(m_codeBlock->identifier(currentInstruction[3].u.operand));
410
411     compileGetByIdSlowCase(resultVReg, baseVReg, ident, iter, false);
412     emitValueProfilingSite();
413 }
414
415 void JIT::compileGetByIdSlowCase(int resultVReg, int baseVReg, Identifier* ident, Vector<SlowCaseEntry>::iterator& iter, bool isMethodCheck)
416 {
417     // As for the hot path of get_by_id, above, we ensure that we can use an architecture specific offset
418     // so that we only need track one pointer into the slow case code - we track a pointer to the location
419     // of the call (which we can use to look up the patch information), but should a array-length or
420     // prototype access trampoline fail we want to bail out back to here.  To do so we can subtract back
421     // the distance from the call to the head of the slow case.
422
423     linkSlowCaseIfNotJSCell(iter, baseVReg);
424     linkSlowCase(iter);
425
426     BEGIN_UNINTERRUPTED_SEQUENCE(sequenceGetByIdSlowCase);
427
428     Label coldPathBegin(this);
429     JITStubCall stubCall(this, isMethodCheck ? cti_op_get_by_id_method_check : cti_op_get_by_id);
430     stubCall.addArgument(regT0);
431     stubCall.addArgument(TrustedImmPtr(ident));
432     Call call = stubCall.call(resultVReg);
433
434     END_UNINTERRUPTED_SEQUENCE(sequenceGetByIdSlowCase);
435
436     // Track the location of the call; this will be used to recover patch information.
437     m_propertyAccessCompilationInfo[m_propertyAccessInstructionIndex++].slowCaseInfo(PropertyStubGetById, coldPathBegin, call);
438 }
439
440 void JIT::emit_op_put_by_id(Instruction* currentInstruction)
441 {
442     unsigned baseVReg = currentInstruction[1].u.operand;
443     unsigned valueVReg = currentInstruction[3].u.operand;
444
445     // In order to be able to patch both the Structure, and the object offset, we store one pointer,
446     // to just after the arguments have been loaded into registers 'hotPathBegin', and we generate code
447     // such that the Structure & offset are always at the same distance from this.
448
449     emitGetVirtualRegisters(baseVReg, regT0, valueVReg, regT1);
450
451     // Jump to a slow case if either the base object is an immediate, or if the Structure does not match.
452     emitJumpSlowCaseIfNotJSCell(regT0, baseVReg);
453
454     BEGIN_UNINTERRUPTED_SEQUENCE(sequencePutById);
455
456     Label hotPathBegin(this);
457
458     // It is important that the following instruction plants a 32bit immediate, in order that it can be patched over.
459     DataLabelPtr structureToCompare;
460     addSlowCase(branchPtrWithPatch(NotEqual, Address(regT0, JSCell::structureOffset()), structureToCompare, TrustedImmPtr(reinterpret_cast<void*>(patchGetByIdDefaultStructure))));
461
462     ConvertibleLoadLabel propertyStorageLoad = convertibleLoadPtr(Address(regT0, JSObject::offsetOfOutOfLineStorage()), regT2);
463     DataLabel32 displacementLabel = storePtrWithAddressOffsetPatch(regT1, Address(regT2, patchPutByIdDefaultOffset));
464
465     END_UNINTERRUPTED_SEQUENCE(sequencePutById);
466
467     emitWriteBarrier(regT0, regT1, regT2, regT3, ShouldFilterImmediates, WriteBarrierForPropertyAccess);
468
469     m_propertyAccessCompilationInfo.append(PropertyStubCompilationInfo(PropertyStubPutById, m_bytecodeOffset, hotPathBegin, structureToCompare, propertyStorageLoad, displacementLabel));
470 }
471
472 void JIT::emitSlow_op_put_by_id(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
473 {
474     unsigned baseVReg = currentInstruction[1].u.operand;
475     Identifier* ident = &(m_codeBlock->identifier(currentInstruction[2].u.operand));
476     unsigned direct = currentInstruction[8].u.operand;
477
478     linkSlowCaseIfNotJSCell(iter, baseVReg);
479     linkSlowCase(iter);
480
481     JITStubCall stubCall(this, direct ? cti_op_put_by_id_direct : cti_op_put_by_id);
482     stubCall.addArgument(regT0);
483     stubCall.addArgument(TrustedImmPtr(ident));
484     stubCall.addArgument(regT1);
485     Call call = stubCall.call();
486
487     // Track the location of the call; this will be used to recover patch information.
488     m_propertyAccessCompilationInfo[m_propertyAccessInstructionIndex++].slowCaseInfo(PropertyStubPutById, call);
489 }
490
491 // Compile a store into an object's property storage.  May overwrite the
492 // value in objectReg.
493 void JIT::compilePutDirectOffset(RegisterID base, RegisterID value, PropertyOffset cachedOffset)
494 {
495     if (isInlineOffset(cachedOffset)) {
496         storePtr(value, Address(base, JSObject::offsetOfInlineStorage() + sizeof(JSValue) * offsetInInlineStorage(cachedOffset)));
497         return;
498     }
499     
500     loadPtr(Address(base, JSObject::offsetOfOutOfLineStorage()), base);
501     storePtr(value, Address(base, sizeof(JSValue) * offsetInOutOfLineStorage(cachedOffset)));
502 }
503
504 // Compile a load from an object's property storage.  May overwrite base.
505 void JIT::compileGetDirectOffset(RegisterID base, RegisterID result, PropertyOffset cachedOffset)
506 {
507     if (isInlineOffset(cachedOffset)) {
508         loadPtr(Address(base, JSObject::offsetOfInlineStorage() + sizeof(JSValue) * offsetInInlineStorage(cachedOffset)), result);
509         return;
510     }
511     
512     loadPtr(Address(base, JSObject::offsetOfOutOfLineStorage()), result);
513     loadPtr(Address(result, sizeof(JSValue) * offsetInOutOfLineStorage(cachedOffset)), result);
514 }
515
516 void JIT::compileGetDirectOffset(JSObject* base, RegisterID result, PropertyOffset cachedOffset)
517 {
518     if (isInlineOffset(cachedOffset)) {
519         loadPtr(base->locationForOffset(cachedOffset), result);
520         return;
521     }
522     
523     loadPtr(base->addressOfOutOfLineStorage(), result);
524     loadPtr(Address(result, offsetInOutOfLineStorage(cachedOffset) * sizeof(WriteBarrier<Unknown>)), result);
525 }
526
527 void JIT::privateCompilePutByIdTransition(StructureStubInfo* stubInfo, Structure* oldStructure, Structure* newStructure, PropertyOffset cachedOffset, StructureChain* chain, ReturnAddressPtr returnAddress, bool direct)
528 {
529     JumpList failureCases;
530     // Check eax is an object of the right Structure.
531     failureCases.append(emitJumpIfNotJSCell(regT0));
532     failureCases.append(branchPtr(NotEqual, Address(regT0, JSCell::structureOffset()), TrustedImmPtr(oldStructure)));
533     
534     testPrototype(oldStructure->storedPrototype(), failureCases);
535     
536     ASSERT(oldStructure->storedPrototype().isNull() || oldStructure->storedPrototype().asCell()->structure() == chain->head()->get());
537
538     // ecx = baseObject->m_structure
539     if (!direct) {
540         for (WriteBarrier<Structure>* it = chain->head(); *it; ++it) {
541             ASSERT((*it)->storedPrototype().isNull() || (*it)->storedPrototype().asCell()->structure() == it[1].get());
542             testPrototype((*it)->storedPrototype(), failureCases);
543         }
544     }
545
546     // If we succeed in all of our checks, and the code was optimizable, then make sure we
547     // decrement the rare case counter.
548 #if ENABLE(VALUE_PROFILER)
549     if (m_codeBlock->canCompileWithDFG() >= DFG::ShouldProfile) {
550         sub32(
551             TrustedImm32(1),
552             AbsoluteAddress(&m_codeBlock->rareCaseProfileForBytecodeOffset(stubInfo->bytecodeIndex)->m_counter));
553     }
554 #endif
555     
556     // emit a call only if storage realloc is needed
557     bool willNeedStorageRealloc = oldStructure->outOfLineCapacity() != newStructure->outOfLineCapacity();
558     if (willNeedStorageRealloc) {
559         // This trampoline was called to like a JIT stub; before we can can call again we need to
560         // remove the return address from the stack, to prevent the stack from becoming misaligned.
561         preserveReturnAddressAfterCall(regT3);
562  
563         JITStubCall stubCall(this, cti_op_put_by_id_transition_realloc);
564         stubCall.skipArgument(); // base
565         stubCall.skipArgument(); // ident
566         stubCall.skipArgument(); // value
567         stubCall.addArgument(TrustedImm32(oldStructure->outOfLineCapacity()));
568         stubCall.addArgument(TrustedImmPtr(newStructure));
569         stubCall.call(regT0);
570         emitGetJITStubArg(2, regT1);
571
572         restoreReturnAddressBeforeReturn(regT3);
573     }
574
575     // Planting the new structure triggers the write barrier so we need
576     // an unconditional barrier here.
577     emitWriteBarrier(regT0, regT1, regT2, regT3, UnconditionalWriteBarrier, WriteBarrierForPropertyAccess);
578
579     ASSERT(newStructure->classInfo() == oldStructure->classInfo());
580     storePtr(TrustedImmPtr(newStructure), Address(regT0, JSCell::structureOffset()));
581     compilePutDirectOffset(regT0, regT1, cachedOffset);
582
583     ret();
584     
585     ASSERT(!failureCases.empty());
586     failureCases.link(this);
587     restoreArgumentReferenceForTrampoline();
588     Call failureCall = tailRecursiveCall();
589
590     LinkBuffer patchBuffer(*m_globalData, this, m_codeBlock);
591
592     patchBuffer.link(failureCall, FunctionPtr(direct ? cti_op_put_by_id_direct_fail : cti_op_put_by_id_fail));
593
594     if (willNeedStorageRealloc) {
595         ASSERT(m_calls.size() == 1);
596         patchBuffer.link(m_calls[0].from, FunctionPtr(cti_op_put_by_id_transition_realloc));
597     }
598     
599     stubInfo->stubRoutine = createJITStubRoutine(
600         FINALIZE_CODE(
601             patchBuffer,
602             ("Baseline put_by_id transition for CodeBlock %p, return point %p",
603              m_codeBlock, returnAddress.value())),
604         *m_globalData,
605         m_codeBlock->ownerExecutable(),
606         willNeedStorageRealloc,
607         newStructure);
608     RepatchBuffer repatchBuffer(m_codeBlock);
609     repatchBuffer.relinkCallerToTrampoline(returnAddress, CodeLocationLabel(stubInfo->stubRoutine->code().code()));
610 }
611
612 void JIT::patchGetByIdSelf(CodeBlock* codeBlock, StructureStubInfo* stubInfo, Structure* structure, PropertyOffset cachedOffset, ReturnAddressPtr returnAddress)
613 {
614     RepatchBuffer repatchBuffer(codeBlock);
615
616     // We don't want to patch more than once - in future go to cti_op_get_by_id_generic.
617     // Should probably go to cti_op_get_by_id_fail, but that doesn't do anything interesting right now.
618     repatchBuffer.relinkCallerToFunction(returnAddress, FunctionPtr(cti_op_get_by_id_self_fail));
619
620     // Patch the offset into the propoerty map to load from, then patch the Structure to look for.
621     repatchBuffer.repatch(stubInfo->hotPathBegin.dataLabelPtrAtOffset(stubInfo->patch.baseline.u.get.structureToCompare), structure);
622     repatchBuffer.setLoadInstructionIsActive(stubInfo->hotPathBegin.convertibleLoadAtOffset(stubInfo->patch.baseline.u.get.propertyStorageLoad), isOutOfLineOffset(cachedOffset));
623     repatchBuffer.repatch(stubInfo->hotPathBegin.dataLabelCompactAtOffset(stubInfo->patch.baseline.u.get.displacementLabel), offsetRelativeToPatchedStorage(cachedOffset));
624 }
625
626 void JIT::patchPutByIdReplace(CodeBlock* codeBlock, StructureStubInfo* stubInfo, Structure* structure, PropertyOffset cachedOffset, ReturnAddressPtr returnAddress, bool direct)
627 {
628     RepatchBuffer repatchBuffer(codeBlock);
629
630     // We don't want to patch more than once - in future go to cti_op_put_by_id_generic.
631     // Should probably go to cti_op_put_by_id_fail, but that doesn't do anything interesting right now.
632     repatchBuffer.relinkCallerToFunction(returnAddress, FunctionPtr(direct ? cti_op_put_by_id_direct_generic : cti_op_put_by_id_generic));
633
634     // Patch the offset into the propoerty map to load from, then patch the Structure to look for.
635     repatchBuffer.repatch(stubInfo->hotPathBegin.dataLabelPtrAtOffset(stubInfo->patch.baseline.u.put.structureToCompare), structure);
636     repatchBuffer.setLoadInstructionIsActive(stubInfo->hotPathBegin.convertibleLoadAtOffset(stubInfo->patch.baseline.u.put.propertyStorageLoad), isOutOfLineOffset(cachedOffset));
637     repatchBuffer.repatch(stubInfo->hotPathBegin.dataLabel32AtOffset(stubInfo->patch.baseline.u.put.displacementLabel), offsetRelativeToPatchedStorage(cachedOffset));
638 }
639
640 void JIT::privateCompilePatchGetArrayLength(ReturnAddressPtr returnAddress)
641 {
642     StructureStubInfo* stubInfo = &m_codeBlock->getStubInfo(returnAddress);
643
644     // Check eax is an array
645     Jump failureCases1 = branchPtr(NotEqual, Address(regT0, JSCell::classInfoOffset()), TrustedImmPtr(&JSArray::s_info));
646
647     // Checks out okay! - get the length from the storage
648     loadPtr(Address(regT0, JSArray::storageOffset()), regT3);
649     load32(Address(regT3, OBJECT_OFFSETOF(ArrayStorage, m_length)), regT2);
650     Jump failureCases2 = branch32(LessThan, regT2, TrustedImm32(0));
651
652     emitFastArithIntToImmNoCheck(regT2, regT0);
653     Jump success = jump();
654
655     LinkBuffer patchBuffer(*m_globalData, this, m_codeBlock);
656
657     // Use the patch information to link the failure cases back to the original slow case routine.
658     CodeLocationLabel slowCaseBegin = stubInfo->callReturnLocation.labelAtOffset(-stubInfo->patch.baseline.u.get.coldPathBegin);
659     patchBuffer.link(failureCases1, slowCaseBegin);
660     patchBuffer.link(failureCases2, slowCaseBegin);
661
662     // On success return back to the hot patch code, at a point it will perform the store to dest for us.
663     patchBuffer.link(success, stubInfo->hotPathBegin.labelAtOffset(stubInfo->patch.baseline.u.get.putResult));
664
665     // Track the stub we have created so that it will be deleted later.
666     stubInfo->stubRoutine = FINALIZE_CODE_FOR_STUB(
667         patchBuffer,
668         ("Basline JIT get_by_id array length stub for CodeBlock %p, return point %p",
669          m_codeBlock, stubInfo->hotPathBegin.labelAtOffset(
670              stubInfo->patch.baseline.u.get.putResult).executableAddress()));
671
672     // Finally patch the jump to slow case back in the hot path to jump here instead.
673     CodeLocationJump jumpLocation = stubInfo->hotPathBegin.jumpAtOffset(stubInfo->patch.baseline.u.get.structureCheck);
674     RepatchBuffer repatchBuffer(m_codeBlock);
675     repatchBuffer.relink(jumpLocation, CodeLocationLabel(stubInfo->stubRoutine->code().code()));
676
677     // We don't want to patch more than once - in future go to cti_op_put_by_id_generic.
678     repatchBuffer.relinkCallerToFunction(returnAddress, FunctionPtr(cti_op_get_by_id_array_fail));
679 }
680
681 void JIT::privateCompileGetByIdProto(StructureStubInfo* stubInfo, Structure* structure, Structure* prototypeStructure, const Identifier& ident, const PropertySlot& slot, PropertyOffset cachedOffset, ReturnAddressPtr returnAddress, CallFrame* callFrame)
682 {
683     // The prototype object definitely exists (if this stub exists the CodeBlock is referencing a Structure that is
684     // referencing the prototype object - let's speculatively load it's table nice and early!)
685     JSObject* protoObject = asObject(structure->prototypeForLookup(callFrame));
686
687     // Check eax is an object of the right Structure.
688     Jump failureCases1 = checkStructure(regT0, structure);
689
690     // Check the prototype object's Structure had not changed.
691     move(TrustedImmPtr(protoObject), regT3);
692     Jump failureCases2 = branchPtr(NotEqual, Address(regT3, JSCell::structureOffset()), TrustedImmPtr(prototypeStructure));
693
694     bool needsStubLink = false;
695     
696     // Checks out okay!
697     if (slot.cachedPropertyType() == PropertySlot::Getter) {
698         needsStubLink = true;
699         compileGetDirectOffset(protoObject, regT1, cachedOffset);
700         JITStubCall stubCall(this, cti_op_get_by_id_getter_stub);
701         stubCall.addArgument(regT1);
702         stubCall.addArgument(regT0);
703         stubCall.addArgument(TrustedImmPtr(stubInfo->callReturnLocation.executableAddress()));
704         stubCall.call();
705     } else if (slot.cachedPropertyType() == PropertySlot::Custom) {
706         needsStubLink = true;
707         JITStubCall stubCall(this, cti_op_get_by_id_custom_stub);
708         stubCall.addArgument(TrustedImmPtr(protoObject));
709         stubCall.addArgument(TrustedImmPtr(FunctionPtr(slot.customGetter()).executableAddress()));
710         stubCall.addArgument(TrustedImmPtr(const_cast<Identifier*>(&ident)));
711         stubCall.addArgument(TrustedImmPtr(stubInfo->callReturnLocation.executableAddress()));
712         stubCall.call();
713     } else
714         compileGetDirectOffset(protoObject, regT0, cachedOffset);
715     Jump success = jump();
716     LinkBuffer patchBuffer(*m_globalData, this, m_codeBlock);
717
718     // Use the patch information to link the failure cases back to the original slow case routine.
719     CodeLocationLabel slowCaseBegin = stubInfo->callReturnLocation.labelAtOffset(-stubInfo->patch.baseline.u.get.coldPathBegin);
720     patchBuffer.link(failureCases1, slowCaseBegin);
721     patchBuffer.link(failureCases2, slowCaseBegin);
722
723     // On success return back to the hot patch code, at a point it will perform the store to dest for us.
724     patchBuffer.link(success, stubInfo->hotPathBegin.labelAtOffset(stubInfo->patch.baseline.u.get.putResult));
725
726     if (needsStubLink) {
727         for (Vector<CallRecord>::iterator iter = m_calls.begin(); iter != m_calls.end(); ++iter) {
728             if (iter->to)
729                 patchBuffer.link(iter->from, FunctionPtr(iter->to));
730         }
731     }
732     // Track the stub we have created so that it will be deleted later.
733     stubInfo->stubRoutine = createJITStubRoutine(
734         FINALIZE_CODE(
735             patchBuffer,
736             ("Baseline JIT get_by_id proto stub for CodeBlock %p, return point %p",
737              m_codeBlock, stubInfo->hotPathBegin.labelAtOffset(
738                  stubInfo->patch.baseline.u.get.putResult).executableAddress())),
739         *m_globalData,
740         m_codeBlock->ownerExecutable(),
741         needsStubLink);
742
743     // Finally patch the jump to slow case back in the hot path to jump here instead.
744     CodeLocationJump jumpLocation = stubInfo->hotPathBegin.jumpAtOffset(stubInfo->patch.baseline.u.get.structureCheck);
745     RepatchBuffer repatchBuffer(m_codeBlock);
746     repatchBuffer.relink(jumpLocation, CodeLocationLabel(stubInfo->stubRoutine->code().code()));
747
748     // We don't want to patch more than once - in future go to cti_op_put_by_id_generic.
749     repatchBuffer.relinkCallerToFunction(returnAddress, FunctionPtr(cti_op_get_by_id_proto_list));
750 }
751
752 void JIT::privateCompileGetByIdSelfList(StructureStubInfo* stubInfo, PolymorphicAccessStructureList* polymorphicStructures, int currentIndex, Structure* structure, const Identifier& ident, const PropertySlot& slot, PropertyOffset cachedOffset)
753 {
754     Jump failureCase = checkStructure(regT0, structure);
755     bool needsStubLink = false;
756     bool isDirect = false;
757     if (slot.cachedPropertyType() == PropertySlot::Getter) {
758         needsStubLink = true;
759         compileGetDirectOffset(regT0, regT1, cachedOffset);
760         JITStubCall stubCall(this, cti_op_get_by_id_getter_stub);
761         stubCall.addArgument(regT1);
762         stubCall.addArgument(regT0);
763         stubCall.addArgument(TrustedImmPtr(stubInfo->callReturnLocation.executableAddress()));
764         stubCall.call();
765     } else if (slot.cachedPropertyType() == PropertySlot::Custom) {
766         needsStubLink = true;
767         JITStubCall stubCall(this, cti_op_get_by_id_custom_stub);
768         stubCall.addArgument(regT0);
769         stubCall.addArgument(TrustedImmPtr(FunctionPtr(slot.customGetter()).executableAddress()));
770         stubCall.addArgument(TrustedImmPtr(const_cast<Identifier*>(&ident)));
771         stubCall.addArgument(TrustedImmPtr(stubInfo->callReturnLocation.executableAddress()));
772         stubCall.call();
773     } else {
774         isDirect = true;
775         compileGetDirectOffset(regT0, regT0, cachedOffset);
776     }
777     Jump success = jump();
778
779     LinkBuffer patchBuffer(*m_globalData, this, m_codeBlock);
780
781     if (needsStubLink) {
782         for (Vector<CallRecord>::iterator iter = m_calls.begin(); iter != m_calls.end(); ++iter) {
783             if (iter->to)
784                 patchBuffer.link(iter->from, FunctionPtr(iter->to));
785         }
786     }
787
788     // Use the patch information to link the failure cases back to the original slow case routine.
789     CodeLocationLabel lastProtoBegin = CodeLocationLabel(JITStubRoutine::asCodePtr(polymorphicStructures->list[currentIndex - 1].stubRoutine));
790     if (!lastProtoBegin)
791         lastProtoBegin = stubInfo->callReturnLocation.labelAtOffset(-stubInfo->patch.baseline.u.get.coldPathBegin);
792
793     patchBuffer.link(failureCase, lastProtoBegin);
794
795     // On success return back to the hot patch code, at a point it will perform the store to dest for us.
796     patchBuffer.link(success, stubInfo->hotPathBegin.labelAtOffset(stubInfo->patch.baseline.u.get.putResult));
797
798     RefPtr<JITStubRoutine> stubCode = createJITStubRoutine(
799         FINALIZE_CODE(
800             patchBuffer,
801             ("Baseline JIT get_by_id list stub for CodeBlock %p, return point %p",
802              m_codeBlock, stubInfo->hotPathBegin.labelAtOffset(
803                  stubInfo->patch.baseline.u.get.putResult).executableAddress())),
804         *m_globalData,
805         m_codeBlock->ownerExecutable(),
806         needsStubLink);
807
808     polymorphicStructures->list[currentIndex].set(*m_globalData, m_codeBlock->ownerExecutable(), stubCode, structure, isDirect);
809
810     // Finally patch the jump to slow case back in the hot path to jump here instead.
811     CodeLocationJump jumpLocation = stubInfo->hotPathBegin.jumpAtOffset(stubInfo->patch.baseline.u.get.structureCheck);
812     RepatchBuffer repatchBuffer(m_codeBlock);
813     repatchBuffer.relink(jumpLocation, CodeLocationLabel(stubCode->code().code()));
814 }
815
816 void JIT::privateCompileGetByIdProtoList(StructureStubInfo* stubInfo, PolymorphicAccessStructureList* prototypeStructures, int currentIndex, Structure* structure, Structure* prototypeStructure, const Identifier& ident, const PropertySlot& slot, PropertyOffset cachedOffset, CallFrame* callFrame)
817 {
818     // The prototype object definitely exists (if this stub exists the CodeBlock is referencing a Structure that is
819     // referencing the prototype object - let's speculatively load it's table nice and early!)
820     JSObject* protoObject = asObject(structure->prototypeForLookup(callFrame));
821
822     // Check eax is an object of the right Structure.
823     Jump failureCases1 = checkStructure(regT0, structure);
824
825     // Check the prototype object's Structure had not changed.
826     move(TrustedImmPtr(protoObject), regT3);
827     Jump failureCases2 = branchPtr(NotEqual, Address(regT3, JSCell::structureOffset()), TrustedImmPtr(prototypeStructure));
828
829     // Checks out okay!
830     bool needsStubLink = false;
831     bool isDirect = false;
832     if (slot.cachedPropertyType() == PropertySlot::Getter) {
833         needsStubLink = true;
834         compileGetDirectOffset(protoObject, regT1, cachedOffset);
835         JITStubCall stubCall(this, cti_op_get_by_id_getter_stub);
836         stubCall.addArgument(regT1);
837         stubCall.addArgument(regT0);
838         stubCall.addArgument(TrustedImmPtr(stubInfo->callReturnLocation.executableAddress()));
839         stubCall.call();
840     } else if (slot.cachedPropertyType() == PropertySlot::Custom) {
841         needsStubLink = true;
842         JITStubCall stubCall(this, cti_op_get_by_id_custom_stub);
843         stubCall.addArgument(TrustedImmPtr(protoObject));
844         stubCall.addArgument(TrustedImmPtr(FunctionPtr(slot.customGetter()).executableAddress()));
845         stubCall.addArgument(TrustedImmPtr(const_cast<Identifier*>(&ident)));
846         stubCall.addArgument(TrustedImmPtr(stubInfo->callReturnLocation.executableAddress()));
847         stubCall.call();
848     } else {
849         isDirect = true;
850         compileGetDirectOffset(protoObject, regT0, cachedOffset);
851     }
852
853     Jump success = jump();
854
855     LinkBuffer patchBuffer(*m_globalData, this, m_codeBlock);
856
857     if (needsStubLink) {
858         for (Vector<CallRecord>::iterator iter = m_calls.begin(); iter != m_calls.end(); ++iter) {
859             if (iter->to)
860                 patchBuffer.link(iter->from, FunctionPtr(iter->to));
861         }
862     }
863
864     // Use the patch information to link the failure cases back to the original slow case routine.
865     CodeLocationLabel lastProtoBegin = CodeLocationLabel(JITStubRoutine::asCodePtr(prototypeStructures->list[currentIndex - 1].stubRoutine));
866     patchBuffer.link(failureCases1, lastProtoBegin);
867     patchBuffer.link(failureCases2, lastProtoBegin);
868
869     // On success return back to the hot patch code, at a point it will perform the store to dest for us.
870     patchBuffer.link(success, stubInfo->hotPathBegin.labelAtOffset(stubInfo->patch.baseline.u.get.putResult));
871
872     RefPtr<JITStubRoutine> stubCode = createJITStubRoutine(
873         FINALIZE_CODE(
874             patchBuffer,
875             ("Baseline JIT get_by_id proto list stub for CodeBlock %p, return point %p",
876              m_codeBlock, stubInfo->hotPathBegin.labelAtOffset(
877                  stubInfo->patch.baseline.u.get.putResult).executableAddress())),
878         *m_globalData,
879         m_codeBlock->ownerExecutable(),
880         needsStubLink);
881     prototypeStructures->list[currentIndex].set(*m_globalData, m_codeBlock->ownerExecutable(), stubCode, structure, prototypeStructure, isDirect);
882
883     // Finally patch the jump to slow case back in the hot path to jump here instead.
884     CodeLocationJump jumpLocation = stubInfo->hotPathBegin.jumpAtOffset(stubInfo->patch.baseline.u.get.structureCheck);
885     RepatchBuffer repatchBuffer(m_codeBlock);
886     repatchBuffer.relink(jumpLocation, CodeLocationLabel(stubCode->code().code()));
887 }
888
889 void JIT::privateCompileGetByIdChainList(StructureStubInfo* stubInfo, PolymorphicAccessStructureList* prototypeStructures, int currentIndex, Structure* structure, StructureChain* chain, size_t count, const Identifier& ident, const PropertySlot& slot, PropertyOffset cachedOffset, CallFrame* callFrame)
890 {
891     ASSERT(count);
892     JumpList bucketsOfFail;
893
894     // Check eax is an object of the right Structure.
895     Jump baseObjectCheck = checkStructure(regT0, structure);
896     bucketsOfFail.append(baseObjectCheck);
897
898     Structure* currStructure = structure;
899     WriteBarrier<Structure>* it = chain->head();
900     JSObject* protoObject = 0;
901     for (unsigned i = 0; i < count; ++i, ++it) {
902         protoObject = asObject(currStructure->prototypeForLookup(callFrame));
903         currStructure = it->get();
904         testPrototype(protoObject, bucketsOfFail);
905     }
906     ASSERT(protoObject);
907     
908     bool needsStubLink = false;
909     bool isDirect = false;
910     if (slot.cachedPropertyType() == PropertySlot::Getter) {
911         needsStubLink = true;
912         compileGetDirectOffset(protoObject, regT1, cachedOffset);
913         JITStubCall stubCall(this, cti_op_get_by_id_getter_stub);
914         stubCall.addArgument(regT1);
915         stubCall.addArgument(regT0);
916         stubCall.addArgument(TrustedImmPtr(stubInfo->callReturnLocation.executableAddress()));
917         stubCall.call();
918     } else if (slot.cachedPropertyType() == PropertySlot::Custom) {
919         needsStubLink = true;
920         JITStubCall stubCall(this, cti_op_get_by_id_custom_stub);
921         stubCall.addArgument(TrustedImmPtr(protoObject));
922         stubCall.addArgument(TrustedImmPtr(FunctionPtr(slot.customGetter()).executableAddress()));
923         stubCall.addArgument(TrustedImmPtr(const_cast<Identifier*>(&ident)));
924         stubCall.addArgument(TrustedImmPtr(stubInfo->callReturnLocation.executableAddress()));
925         stubCall.call();
926     } else {
927         isDirect = true;
928         compileGetDirectOffset(protoObject, regT0, cachedOffset);
929     }
930     Jump success = jump();
931
932     LinkBuffer patchBuffer(*m_globalData, this, m_codeBlock);
933     
934     if (needsStubLink) {
935         for (Vector<CallRecord>::iterator iter = m_calls.begin(); iter != m_calls.end(); ++iter) {
936             if (iter->to)
937                 patchBuffer.link(iter->from, FunctionPtr(iter->to));
938         }
939     }
940
941     // Use the patch information to link the failure cases back to the original slow case routine.
942     CodeLocationLabel lastProtoBegin = CodeLocationLabel(JITStubRoutine::asCodePtr(prototypeStructures->list[currentIndex - 1].stubRoutine));
943
944     patchBuffer.link(bucketsOfFail, lastProtoBegin);
945
946     // On success return back to the hot patch code, at a point it will perform the store to dest for us.
947     patchBuffer.link(success, stubInfo->hotPathBegin.labelAtOffset(stubInfo->patch.baseline.u.get.putResult));
948
949     RefPtr<JITStubRoutine> stubRoutine = createJITStubRoutine(
950         FINALIZE_CODE(
951             patchBuffer,
952             ("Baseline JIT get_by_id chain list stub for CodeBlock %p, return point %p",
953              m_codeBlock, stubInfo->hotPathBegin.labelAtOffset(
954                  stubInfo->patch.baseline.u.get.putResult).executableAddress())),
955         *m_globalData,
956         m_codeBlock->ownerExecutable(),
957         needsStubLink);
958
959     // Track the stub we have created so that it will be deleted later.
960     prototypeStructures->list[currentIndex].set(callFrame->globalData(), m_codeBlock->ownerExecutable(), stubRoutine, structure, chain, isDirect);
961
962     // Finally patch the jump to slow case back in the hot path to jump here instead.
963     CodeLocationJump jumpLocation = stubInfo->hotPathBegin.jumpAtOffset(stubInfo->patch.baseline.u.get.structureCheck);
964     RepatchBuffer repatchBuffer(m_codeBlock);
965     repatchBuffer.relink(jumpLocation, CodeLocationLabel(stubRoutine->code().code()));
966 }
967
968 void JIT::privateCompileGetByIdChain(StructureStubInfo* stubInfo, Structure* structure, StructureChain* chain, size_t count, const Identifier& ident, const PropertySlot& slot, PropertyOffset cachedOffset, ReturnAddressPtr returnAddress, CallFrame* callFrame)
969 {
970     ASSERT(count);
971
972     JumpList bucketsOfFail;
973
974     // Check eax is an object of the right Structure.
975     bucketsOfFail.append(checkStructure(regT0, structure));
976
977     Structure* currStructure = structure;
978     WriteBarrier<Structure>* it = chain->head();
979     JSObject* protoObject = 0;
980     for (unsigned i = 0; i < count; ++i, ++it) {
981         protoObject = asObject(currStructure->prototypeForLookup(callFrame));
982         currStructure = it->get();
983         testPrototype(protoObject, bucketsOfFail);
984     }
985     ASSERT(protoObject);
986
987     bool needsStubLink = false;
988     if (slot.cachedPropertyType() == PropertySlot::Getter) {
989         needsStubLink = true;
990         compileGetDirectOffset(protoObject, regT1, cachedOffset);
991         JITStubCall stubCall(this, cti_op_get_by_id_getter_stub);
992         stubCall.addArgument(regT1);
993         stubCall.addArgument(regT0);
994         stubCall.addArgument(TrustedImmPtr(stubInfo->callReturnLocation.executableAddress()));
995         stubCall.call();
996     } else if (slot.cachedPropertyType() == PropertySlot::Custom) {
997         needsStubLink = true;
998         JITStubCall stubCall(this, cti_op_get_by_id_custom_stub);
999         stubCall.addArgument(TrustedImmPtr(protoObject));
1000         stubCall.addArgument(TrustedImmPtr(FunctionPtr(slot.customGetter()).executableAddress()));
1001         stubCall.addArgument(TrustedImmPtr(const_cast<Identifier*>(&ident)));
1002         stubCall.addArgument(TrustedImmPtr(stubInfo->callReturnLocation.executableAddress()));
1003         stubCall.call();
1004     } else
1005         compileGetDirectOffset(protoObject, regT0, cachedOffset);
1006     Jump success = jump();
1007
1008     LinkBuffer patchBuffer(*m_globalData, this, m_codeBlock);
1009
1010     if (needsStubLink) {
1011         for (Vector<CallRecord>::iterator iter = m_calls.begin(); iter != m_calls.end(); ++iter) {
1012             if (iter->to)
1013                 patchBuffer.link(iter->from, FunctionPtr(iter->to));
1014         }
1015     }
1016
1017     // Use the patch information to link the failure cases back to the original slow case routine.
1018     patchBuffer.link(bucketsOfFail, stubInfo->callReturnLocation.labelAtOffset(-stubInfo->patch.baseline.u.get.coldPathBegin));
1019
1020     // On success return back to the hot patch code, at a point it will perform the store to dest for us.
1021     patchBuffer.link(success, stubInfo->hotPathBegin.labelAtOffset(stubInfo->patch.baseline.u.get.putResult));
1022
1023     // Track the stub we have created so that it will be deleted later.
1024     RefPtr<JITStubRoutine> stubRoutine = createJITStubRoutine(
1025         FINALIZE_CODE(
1026             patchBuffer,
1027             ("Baseline JIT get_by_id chain stub for CodeBlock %p, return point %p",
1028              m_codeBlock, stubInfo->hotPathBegin.labelAtOffset(
1029                  stubInfo->patch.baseline.u.get.putResult).executableAddress())),
1030         *m_globalData,
1031         m_codeBlock->ownerExecutable(),
1032         needsStubLink);
1033     stubInfo->stubRoutine = stubRoutine;
1034
1035     // Finally patch the jump to slow case back in the hot path to jump here instead.
1036     CodeLocationJump jumpLocation = stubInfo->hotPathBegin.jumpAtOffset(stubInfo->patch.baseline.u.get.structureCheck);
1037     RepatchBuffer repatchBuffer(m_codeBlock);
1038     repatchBuffer.relink(jumpLocation, CodeLocationLabel(stubRoutine->code().code()));
1039
1040     // We don't want to patch more than once - in future go to cti_op_put_by_id_generic.
1041     repatchBuffer.relinkCallerToFunction(returnAddress, FunctionPtr(cti_op_get_by_id_proto_list));
1042 }
1043
1044 void JIT::emit_op_get_scoped_var(Instruction* currentInstruction)
1045 {
1046     int skip = currentInstruction[3].u.operand;
1047
1048     emitGetFromCallFrameHeaderPtr(RegisterFile::ScopeChain, regT0);
1049     bool checkTopLevel = m_codeBlock->codeType() == FunctionCode && m_codeBlock->needsFullScopeChain();
1050     ASSERT(skip || !checkTopLevel);
1051     if (checkTopLevel && skip--) {
1052         Jump activationNotCreated;
1053         if (checkTopLevel)
1054             activationNotCreated = branchTestPtr(Zero, addressFor(m_codeBlock->activationRegister()));
1055         loadPtr(Address(regT0, OBJECT_OFFSETOF(ScopeChainNode, next)), regT0);
1056         activationNotCreated.link(this);
1057     }
1058     while (skip--)
1059         loadPtr(Address(regT0, OBJECT_OFFSETOF(ScopeChainNode, next)), regT0);
1060
1061     loadPtr(Address(regT0, OBJECT_OFFSETOF(ScopeChainNode, object)), regT0);
1062     loadPtr(Address(regT0, JSVariableObject::offsetOfRegisters()), regT0);
1063     loadPtr(Address(regT0, currentInstruction[2].u.operand * sizeof(Register)), regT0);
1064     emitValueProfilingSite();
1065     emitPutVirtualRegister(currentInstruction[1].u.operand);
1066 }
1067
1068 void JIT::emit_op_put_scoped_var(Instruction* currentInstruction)
1069 {
1070     int skip = currentInstruction[2].u.operand;
1071
1072     emitGetVirtualRegister(currentInstruction[3].u.operand, regT0);
1073
1074     emitGetFromCallFrameHeaderPtr(RegisterFile::ScopeChain, regT1);
1075     bool checkTopLevel = m_codeBlock->codeType() == FunctionCode && m_codeBlock->needsFullScopeChain();
1076     ASSERT(skip || !checkTopLevel);
1077     if (checkTopLevel && skip--) {
1078         Jump activationNotCreated;
1079         if (checkTopLevel)
1080             activationNotCreated = branchTestPtr(Zero, addressFor(m_codeBlock->activationRegister()));
1081         loadPtr(Address(regT1, OBJECT_OFFSETOF(ScopeChainNode, next)), regT1);
1082         activationNotCreated.link(this);
1083     }
1084     while (skip--)
1085         loadPtr(Address(regT1, OBJECT_OFFSETOF(ScopeChainNode, next)), regT1);
1086     loadPtr(Address(regT1, OBJECT_OFFSETOF(ScopeChainNode, object)), regT1);
1087
1088     emitWriteBarrier(regT1, regT0, regT2, regT3, ShouldFilterImmediates, WriteBarrierForVariableAccess);
1089
1090     loadPtr(Address(regT1, JSVariableObject::offsetOfRegisters()), regT1);
1091     storePtr(regT0, Address(regT1, currentInstruction[1].u.operand * sizeof(Register)));
1092 }
1093
1094 void JIT::emit_op_get_global_var(Instruction* currentInstruction)
1095 {
1096     loadPtr(currentInstruction[2].u.registerPointer, regT0);
1097     emitValueProfilingSite();
1098     emitPutVirtualRegister(currentInstruction[1].u.operand);
1099 }
1100
1101 void JIT::emit_op_put_global_var(Instruction* currentInstruction)
1102 {
1103     JSGlobalObject* globalObject = m_codeBlock->globalObject();
1104
1105     emitGetVirtualRegister(currentInstruction[2].u.operand, regT0);
1106     
1107     storePtr(regT0, currentInstruction[1].u.registerPointer);
1108     if (Heap::isWriteBarrierEnabled())
1109         emitWriteBarrier(globalObject, regT0, regT2, ShouldFilterImmediates, WriteBarrierForVariableAccess);
1110 }
1111
1112 void JIT::emit_op_put_global_var_check(Instruction* currentInstruction)
1113 {
1114     emitGetVirtualRegister(currentInstruction[2].u.operand, regT0);
1115     
1116     addSlowCase(branchTest8(NonZero, AbsoluteAddress(currentInstruction[3].u.predicatePointer)));
1117
1118     JSGlobalObject* globalObject = m_codeBlock->globalObject();
1119     
1120     storePtr(regT0, currentInstruction[1].u.registerPointer);
1121     if (Heap::isWriteBarrierEnabled())
1122         emitWriteBarrier(globalObject, regT0, regT2, ShouldFilterImmediates, WriteBarrierForVariableAccess);
1123 }
1124
1125 void JIT::emitSlow_op_put_global_var_check(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
1126 {
1127     linkSlowCase(iter);
1128     
1129     JITStubCall stubCall(this, cti_op_put_global_var_check);
1130     stubCall.addArgument(regT0);
1131     stubCall.addArgument(TrustedImm32(currentInstruction[4].u.operand));
1132     stubCall.call();
1133 }
1134
1135 void JIT::resetPatchGetById(RepatchBuffer& repatchBuffer, StructureStubInfo* stubInfo)
1136 {
1137     repatchBuffer.relink(stubInfo->callReturnLocation, cti_op_get_by_id);
1138     repatchBuffer.repatch(stubInfo->hotPathBegin.dataLabelPtrAtOffset(stubInfo->patch.baseline.u.get.structureToCompare), reinterpret_cast<void*>(-1));
1139     repatchBuffer.repatch(stubInfo->hotPathBegin.dataLabelCompactAtOffset(stubInfo->patch.baseline.u.get.displacementLabel), 0);
1140     repatchBuffer.relink(stubInfo->hotPathBegin.jumpAtOffset(stubInfo->patch.baseline.u.get.structureCheck), stubInfo->callReturnLocation.labelAtOffset(-stubInfo->patch.baseline.u.get.coldPathBegin));
1141 }
1142
1143 void JIT::resetPatchPutById(RepatchBuffer& repatchBuffer, StructureStubInfo* stubInfo)
1144 {
1145     if (isDirectPutById(stubInfo))
1146         repatchBuffer.relink(stubInfo->callReturnLocation, cti_op_put_by_id_direct);
1147     else
1148         repatchBuffer.relink(stubInfo->callReturnLocation, cti_op_put_by_id);
1149     repatchBuffer.repatch(stubInfo->hotPathBegin.dataLabelPtrAtOffset(stubInfo->patch.baseline.u.put.structureToCompare), reinterpret_cast<void*>(-1));
1150     repatchBuffer.repatch(stubInfo->hotPathBegin.dataLabelCompactAtOffset(stubInfo->patch.baseline.u.put.displacementLabel), 0);
1151 }
1152
1153 #endif // USE(JSVALUE64)
1154
1155 void JIT::emitWriteBarrier(RegisterID owner, RegisterID value, RegisterID scratch, RegisterID scratch2, WriteBarrierMode mode, WriteBarrierUseKind useKind)
1156 {
1157     UNUSED_PARAM(owner);
1158     UNUSED_PARAM(scratch);
1159     UNUSED_PARAM(scratch2);
1160     UNUSED_PARAM(useKind);
1161     UNUSED_PARAM(value);
1162     UNUSED_PARAM(mode);
1163     ASSERT(owner != scratch);
1164     ASSERT(owner != scratch2);
1165     
1166 #if ENABLE(WRITE_BARRIER_PROFILING)
1167     emitCount(WriteBarrierCounters::jitCounterFor(useKind));
1168 #endif
1169     
1170 #if ENABLE(GGC)
1171     Jump filterCells;
1172     if (mode == ShouldFilterImmediates)
1173         filterCells = emitJumpIfNotJSCell(value);
1174     move(owner, scratch);
1175     andPtr(TrustedImm32(static_cast<int32_t>(MarkedBlock::blockMask)), scratch);
1176     move(owner, scratch2);
1177     // consume additional 8 bits as we're using an approximate filter
1178     rshift32(TrustedImm32(MarkedBlock::atomShift + 8), scratch2);
1179     andPtr(TrustedImm32(MarkedBlock::atomMask >> 8), scratch2);
1180     Jump filter = branchTest8(Zero, BaseIndex(scratch, scratch2, TimesOne, MarkedBlock::offsetOfMarks()));
1181     move(owner, scratch2);
1182     rshift32(TrustedImm32(MarkedBlock::cardShift), scratch2);
1183     andPtr(TrustedImm32(MarkedBlock::cardMask), scratch2);
1184     store8(TrustedImm32(1), BaseIndex(scratch, scratch2, TimesOne, MarkedBlock::offsetOfCards()));
1185     filter.link(this);
1186     if (mode == ShouldFilterImmediates)
1187         filterCells.link(this);
1188 #endif
1189 }
1190
1191 void JIT::emitWriteBarrier(JSCell* owner, RegisterID value, RegisterID scratch, WriteBarrierMode mode, WriteBarrierUseKind useKind)
1192 {
1193     UNUSED_PARAM(owner);
1194     UNUSED_PARAM(scratch);
1195     UNUSED_PARAM(useKind);
1196     UNUSED_PARAM(value);
1197     UNUSED_PARAM(mode);
1198     
1199 #if ENABLE(WRITE_BARRIER_PROFILING)
1200     emitCount(WriteBarrierCounters::jitCounterFor(useKind));
1201 #endif
1202     
1203 #if ENABLE(GGC)
1204     Jump filterCells;
1205     if (mode == ShouldFilterImmediates)
1206         filterCells = emitJumpIfNotJSCell(value);
1207     uint8_t* cardAddress = Heap::addressOfCardFor(owner);
1208     move(TrustedImmPtr(cardAddress), scratch);
1209     store8(TrustedImm32(1), Address(scratch));
1210     if (mode == ShouldFilterImmediates)
1211         filterCells.link(this);
1212 #endif
1213 }
1214
1215 void JIT::testPrototype(JSValue prototype, JumpList& failureCases)
1216 {
1217     if (prototype.isNull())
1218         return;
1219
1220     ASSERT(prototype.isCell());
1221     move(TrustedImmPtr(prototype.asCell()), regT3);
1222     failureCases.append(branchPtr(NotEqual, Address(regT3, JSCell::structureOffset()), TrustedImmPtr(prototype.asCell()->structure())));
1223 }
1224
1225 void JIT::patchMethodCallProto(JSGlobalData& globalData, CodeBlock* codeBlock, MethodCallLinkInfo& methodCallLinkInfo, StructureStubInfo& stubInfo, JSObject* callee, Structure* structure, JSObject* proto, ReturnAddressPtr returnAddress)
1226 {
1227     RepatchBuffer repatchBuffer(codeBlock);
1228     
1229     CodeLocationDataLabelPtr structureLocation = methodCallLinkInfo.cachedStructure.location();
1230     methodCallLinkInfo.cachedStructure.set(globalData, structureLocation, codeBlock->ownerExecutable(), structure);
1231     
1232     Structure* prototypeStructure = proto->structure();
1233     methodCallLinkInfo.cachedPrototypeStructure.set(globalData, structureLocation.dataLabelPtrAtOffset(stubInfo.patch.baseline.methodCheckProtoStructureToCompare), codeBlock->ownerExecutable(), prototypeStructure);
1234     methodCallLinkInfo.cachedPrototype.set(globalData, structureLocation.dataLabelPtrAtOffset(stubInfo.patch.baseline.methodCheckProtoObj), codeBlock->ownerExecutable(), proto);
1235     methodCallLinkInfo.cachedFunction.set(globalData, structureLocation.dataLabelPtrAtOffset(stubInfo.patch.baseline.methodCheckPutFunction), codeBlock->ownerExecutable(), callee);
1236     repatchBuffer.relinkCallerToFunction(returnAddress, FunctionPtr(cti_op_get_by_id_method_check_update));
1237 }
1238
1239 bool JIT::isDirectPutById(StructureStubInfo* stubInfo)
1240 {
1241     switch (stubInfo->accessType) {
1242     case access_put_by_id_transition_normal:
1243         return false;
1244     case access_put_by_id_transition_direct:
1245         return true;
1246     case access_put_by_id_replace:
1247     case access_put_by_id_generic: {
1248         void* oldCall = MacroAssembler::readCallTarget(stubInfo->callReturnLocation).executableAddress();
1249         if (oldCall == bitwise_cast<void*>(cti_op_put_by_id_direct)
1250             || oldCall == bitwise_cast<void*>(cti_op_put_by_id_direct_generic)
1251             || oldCall == bitwise_cast<void*>(cti_op_put_by_id_direct_fail))
1252             return true;
1253         ASSERT(oldCall == bitwise_cast<void*>(cti_op_put_by_id)
1254                || oldCall == bitwise_cast<void*>(cti_op_put_by_id_generic)
1255                || oldCall == bitwise_cast<void*>(cti_op_put_by_id_fail));
1256         return false;
1257     }
1258     default:
1259         ASSERT_NOT_REACHED();
1260         return false;
1261     }
1262 }
1263
1264 } // namespace JSC
1265
1266 #endif // ENABLE(JIT)