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