2 * Copyright (C) 2008, 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
31 #include "CodeBlock.h"
32 #include "GCAwareJITStubRoutine.h"
33 #include "GetterSetter.h"
34 #include "Interpreter.h"
35 #include "JITInlineMethods.h"
36 #include "JITStubCall.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"
55 JIT::CodeRef JIT::stringGetByValStubGenerator(JSGlobalData* globalData)
59 failures.append(jit.branchPtr(NotEqual, Address(regT0, JSCell::structureOffset()), TrustedImmPtr(globalData->stringStructure.get())));
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));
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));
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());
79 jit.load16(BaseIndex(regT0, regT1, TimesTwo, 0), regT0);
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);
88 jit.move(TrustedImm32(0), regT0);
91 LinkBuffer patchBuffer(*globalData, &jit, GLOBAL_THUNK_ID);
92 return FINALIZE_CODE(patchBuffer, ("String get_by_val stub"));
95 void JIT::emit_op_get_by_val(Instruction* currentInstruction)
97 unsigned dst = currentInstruction[1].u.operand;
98 unsigned base = currentInstruction[2].u.operand;
99 unsigned property = currentInstruction[3].u.operand;
101 emitGetVirtualRegisters(base, regT0, property, regT1);
102 emitJumpSlowCaseIfNotImmediateInteger(regT1);
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);
112 emitJumpSlowCaseIfNotJSCell(regT0, base);
113 addSlowCase(branchPtr(NotEqual, Address(regT0, JSCell::classInfoOffset()), TrustedImmPtr(&JSArray::s_info)));
115 loadPtr(Address(regT0, JSArray::storageOffset()), regT2);
116 addSlowCase(branch32(AboveOrEqual, regT1, Address(regT0, JSArray::vectorLengthOffset())));
118 loadPtr(BaseIndex(regT2, regT1, ScalePtr, OBJECT_OFFSETOF(ArrayStorage, m_vector[0])), regT0);
119 addSlowCase(branchTestPtr(Zero, regT0));
121 emitValueProfilingSite();
122 emitPutVirtualRegister(dst);
125 void JIT::emitSlow_op_get_by_val(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
127 unsigned dst = currentInstruction[1].u.operand;
128 unsigned base = currentInstruction[2].u.operand;
129 unsigned property = currentInstruction[3].u.operand;
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));
141 notString.link(this);
144 linkSlowCase(iter); // vector length check
145 linkSlowCase(iter); // empty value
147 JITStubCall stubCall(this, cti_op_get_by_val);
148 stubCall.addArgument(base, regT2);
149 stubCall.addArgument(property, regT2);
152 emitValueProfilingSite();
155 void JIT::compileGetDirectOffset(RegisterID base, RegisterID result, RegisterID offset, RegisterID scratch, FinalObjectMode finalObjectMode)
157 ASSERT(sizeof(JSValue) == 8);
159 if (finalObjectMode == MayBeFinal) {
160 Jump isInline = branch32(LessThan, offset, TrustedImm32(inlineStorageCapacity));
161 loadPtr(Address(base, JSObject::offsetOfOutOfLineStorage()), scratch);
165 addPtr(TrustedImm32(JSObject::offsetOfInlineStorage() - (inlineStorageCapacity - 2) * sizeof(EncodedJSValue)), base, scratch);
169 Jump isOutOfLine = branch32(GreaterThanOrEqual, offset, TrustedImm32(inlineStorageCapacity));
171 isOutOfLine.link(this);
173 loadPtr(Address(base, JSObject::offsetOfOutOfLineStorage()), scratch);
176 signExtend32ToPtr(offset, offset);
177 loadPtr(BaseIndex(scratch, offset, ScalePtr, (inlineStorageCapacity - 2) * static_cast<ptrdiff_t>(sizeof(JSValue))), result);
180 void JIT::emit_op_get_by_pname(Instruction* currentInstruction)
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;
189 emitGetVirtualRegister(property, regT0);
190 addSlowCase(branchPtr(NotEqual, regT0, addressFor(expected)));
191 emitGetVirtualRegisters(base, regT0, iter, regT1);
192 emitJumpSlowCaseIfNotJSCell(regT0, base);
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);
203 emitPutVirtualRegister(dst, regT0);
206 void JIT::emitSlow_op_get_by_pname(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
208 unsigned dst = currentInstruction[1].u.operand;
209 unsigned base = currentInstruction[2].u.operand;
210 unsigned property = currentInstruction[3].u.operand;
213 linkSlowCaseIfNotJSCell(iter, base);
217 JITStubCall stubCall(this, cti_op_get_by_val);
218 stubCall.addArgument(base, regT2);
219 stubCall.addArgument(property, regT2);
223 void JIT::emit_op_put_by_val(Instruction* currentInstruction)
225 unsigned base = currentInstruction[1].u.operand;
226 unsigned property = currentInstruction[2].u.operand;
227 unsigned value = currentInstruction[3].u.operand;
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())));
237 loadPtr(Address(regT0, JSArray::storageOffset()), regT2);
238 Jump empty = branchTestPtr(Zero, BaseIndex(regT2, regT1, ScalePtr, OBJECT_OFFSETOF(ArrayStorage, m_vector[0])));
240 Label storeResult(this);
241 emitGetVirtualRegister(value, regT3);
242 storePtr(regT3, BaseIndex(regT2, regT1, ScalePtr, OBJECT_OFFSETOF(ArrayStorage, m_vector[0])));
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);
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);
256 emitWriteBarrier(regT0, regT3, regT1, regT3, ShouldFilterImmediates, WriteBarrierForPropertyAccess);
259 void JIT::emitSlow_op_put_by_val(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
261 unsigned base = currentInstruction[1].u.operand;
262 unsigned property = currentInstruction[2].u.operand;
263 unsigned value = currentInstruction[3].u.operand;
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
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();
277 void JIT::emit_op_put_by_index(Instruction* currentInstruction)
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);
286 void JIT::emit_op_put_getter_setter(Instruction* currentInstruction)
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);
296 void JIT::emit_op_del_by_id(Instruction* currentInstruction)
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);
304 void JIT::emit_op_method_check(Instruction* currentInstruction)
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);
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));
315 emitGetVirtualRegister(baseVReg, regT0);
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();
321 Jump notCell = emitJumpIfNotJSCell(regT0);
323 BEGIN_UNINTERRUPTED_SEQUENCE(sequenceMethodCheck);
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)));
329 // This will be relinked to load the function without doing a load.
330 DataLabelPtr putFunction = moveWithPatch(TrustedImmPtr(0), regT0);
332 END_UNINTERRUPTED_SEQUENCE(sequenceMethodCheck);
336 // Link the failure cases here.
338 structureCheck.link(this);
339 protoStructureCheck.link(this);
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);
346 emitValueProfilingSite(m_bytecodeOffset + OPCODE_LENGTH(op_method_check));
347 emitPutVirtualRegister(resultVReg);
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);
352 m_propertyAccessCompilationInfo.last().addMethodCheckInfo(info.structureToCompare, protoObj, protoStructureToCompare, putFunction);
355 void JIT::emitSlow_op_method_check(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
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));
362 compileGetByIdSlowCase(resultVReg, baseVReg, ident, iter, true);
363 emitValueProfilingSite(m_bytecodeOffset + OPCODE_LENGTH(op_method_check));
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);
369 void JIT::emit_op_get_by_id(Instruction* currentInstruction)
371 unsigned resultVReg = currentInstruction[1].u.operand;
372 unsigned baseVReg = currentInstruction[2].u.operand;
373 Identifier* ident = &(m_codeBlock->identifier(currentInstruction[3].u.operand));
375 emitGetVirtualRegister(baseVReg, regT0);
376 compileGetByIdHotPath(baseVReg, ident);
377 emitValueProfilingSite();
378 emitPutVirtualRegister(resultVReg);
381 void JIT::compileGetByIdHotPath(int baseVReg, Identifier*)
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.
388 emitJumpSlowCaseIfNotJSCell(regT0, baseVReg);
390 BEGIN_UNINTERRUPTED_SEQUENCE(sequenceGetByIdHotPath);
392 Label hotPathBegin(this);
394 DataLabelPtr structureToCompare;
395 PatchableJump structureCheck = patchableBranchPtrWithPatch(NotEqual, Address(regT0, JSCell::structureOffset()), structureToCompare, TrustedImmPtr(reinterpret_cast<void*>(patchGetByIdDefaultStructure)));
396 addSlowCase(structureCheck);
398 ConvertibleLoadLabel propertyStorageLoad = convertibleLoadPtr(Address(regT0, JSObject::offsetOfOutOfLineStorage()), regT0);
399 DataLabelCompact displacementLabel = loadPtrWithCompactAddressOffsetPatch(Address(regT0, patchGetByIdDefaultOffset), regT0);
401 Label putResult(this);
403 END_UNINTERRUPTED_SEQUENCE(sequenceGetByIdHotPath);
405 m_propertyAccessCompilationInfo.append(PropertyStubCompilationInfo(PropertyStubGetById, m_bytecodeOffset, hotPathBegin, structureToCompare, structureCheck, propertyStorageLoad, displacementLabel, putResult));
408 void JIT::emitSlow_op_get_by_id(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
410 unsigned resultVReg = currentInstruction[1].u.operand;
411 unsigned baseVReg = currentInstruction[2].u.operand;
412 Identifier* ident = &(m_codeBlock->identifier(currentInstruction[3].u.operand));
414 compileGetByIdSlowCase(resultVReg, baseVReg, ident, iter, false);
415 emitValueProfilingSite();
418 void JIT::compileGetByIdSlowCase(int resultVReg, int baseVReg, Identifier* ident, Vector<SlowCaseEntry>::iterator& iter, bool isMethodCheck)
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.
426 linkSlowCaseIfNotJSCell(iter, baseVReg);
429 BEGIN_UNINTERRUPTED_SEQUENCE(sequenceGetByIdSlowCase);
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);
437 END_UNINTERRUPTED_SEQUENCE(sequenceGetByIdSlowCase);
439 // Track the location of the call; this will be used to recover patch information.
440 m_propertyAccessCompilationInfo[m_propertyAccessInstructionIndex++].slowCaseInfo(PropertyStubGetById, coldPathBegin, call);
443 void JIT::emit_op_put_by_id(Instruction* currentInstruction)
445 unsigned baseVReg = currentInstruction[1].u.operand;
446 unsigned valueVReg = currentInstruction[3].u.operand;
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.
452 emitGetVirtualRegisters(baseVReg, regT0, valueVReg, regT1);
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);
457 BEGIN_UNINTERRUPTED_SEQUENCE(sequencePutById);
459 Label hotPathBegin(this);
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))));
465 ConvertibleLoadLabel propertyStorageLoad = convertibleLoadPtr(Address(regT0, JSObject::offsetOfOutOfLineStorage()), regT2);
466 DataLabel32 displacementLabel = storePtrWithAddressOffsetPatch(regT1, Address(regT2, patchPutByIdDefaultOffset));
468 END_UNINTERRUPTED_SEQUENCE(sequencePutById);
470 emitWriteBarrier(regT0, regT1, regT2, regT3, ShouldFilterImmediates, WriteBarrierForPropertyAccess);
472 m_propertyAccessCompilationInfo.append(PropertyStubCompilationInfo(PropertyStubPutById, m_bytecodeOffset, hotPathBegin, structureToCompare, propertyStorageLoad, displacementLabel));
475 void JIT::emitSlow_op_put_by_id(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
477 unsigned baseVReg = currentInstruction[1].u.operand;
478 Identifier* ident = &(m_codeBlock->identifier(currentInstruction[2].u.operand));
479 unsigned direct = currentInstruction[8].u.operand;
481 linkSlowCaseIfNotJSCell(iter, baseVReg);
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();
490 // Track the location of the call; this will be used to recover patch information.
491 m_propertyAccessCompilationInfo[m_propertyAccessInstructionIndex++].slowCaseInfo(PropertyStubPutById, call);
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)
498 if (isInlineOffset(cachedOffset)) {
499 storePtr(value, Address(base, JSObject::offsetOfInlineStorage() + sizeof(JSValue) * offsetInInlineStorage(cachedOffset)));
503 loadPtr(Address(base, JSObject::offsetOfOutOfLineStorage()), base);
504 storePtr(value, Address(base, sizeof(JSValue) * offsetInOutOfLineStorage(cachedOffset)));
507 // Compile a load from an object's property storage. May overwrite base.
508 void JIT::compileGetDirectOffset(RegisterID base, RegisterID result, PropertyOffset cachedOffset)
510 if (isInlineOffset(cachedOffset)) {
511 loadPtr(Address(base, JSObject::offsetOfInlineStorage() + sizeof(JSValue) * offsetInInlineStorage(cachedOffset)), result);
515 loadPtr(Address(base, JSObject::offsetOfOutOfLineStorage()), result);
516 loadPtr(Address(result, sizeof(JSValue) * offsetInOutOfLineStorage(cachedOffset)), result);
519 void JIT::compileGetDirectOffset(JSObject* base, RegisterID result, PropertyOffset cachedOffset)
521 if (isInlineOffset(cachedOffset)) {
522 loadPtr(base->locationForOffset(cachedOffset), result);
526 loadPtr(base->addressOfOutOfLineStorage(), result);
527 loadPtr(Address(result, offsetInOutOfLineStorage(cachedOffset) * sizeof(WriteBarrier<Unknown>)), result);
530 void JIT::privateCompilePutByIdTransition(StructureStubInfo* stubInfo, Structure* oldStructure, Structure* newStructure, PropertyOffset cachedOffset, StructureChain* chain, ReturnAddressPtr returnAddress, bool direct)
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)));
537 testPrototype(oldStructure->storedPrototype(), failureCases);
539 ASSERT(oldStructure->storedPrototype().isNull() || oldStructure->storedPrototype().asCell()->structure() == chain->head()->get());
541 // ecx = baseObject->m_structure
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);
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) {
555 AbsoluteAddress(&m_codeBlock->rareCaseProfileForBytecodeOffset(stubInfo->bytecodeIndex)->m_counter));
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);
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);
575 restoreReturnAddressBeforeReturn(regT3);
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);
582 ASSERT(newStructure->classInfo() == oldStructure->classInfo());
583 storePtr(TrustedImmPtr(newStructure), Address(regT0, JSCell::structureOffset()));
584 compilePutDirectOffset(regT0, regT1, cachedOffset);
588 ASSERT(!failureCases.empty());
589 failureCases.link(this);
590 restoreArgumentReferenceForTrampoline();
591 Call failureCall = tailRecursiveCall();
593 LinkBuffer patchBuffer(*m_globalData, this, m_codeBlock);
595 patchBuffer.link(failureCall, FunctionPtr(direct ? cti_op_put_by_id_direct_fail : cti_op_put_by_id_fail));
597 if (willNeedStorageRealloc) {
598 ASSERT(m_calls.size() == 1);
599 patchBuffer.link(m_calls[0].from, FunctionPtr(cti_op_put_by_id_transition_realloc));
602 stubInfo->stubRoutine = createJITStubRoutine(
605 ("Baseline put_by_id transition for CodeBlock %p, return point %p",
606 m_codeBlock, returnAddress.value())),
608 m_codeBlock->ownerExecutable(),
609 willNeedStorageRealloc,
611 RepatchBuffer repatchBuffer(m_codeBlock);
612 repatchBuffer.relinkCallerToTrampoline(returnAddress, CodeLocationLabel(stubInfo->stubRoutine->code().code()));
615 void JIT::patchGetByIdSelf(CodeBlock* codeBlock, StructureStubInfo* stubInfo, Structure* structure, PropertyOffset cachedOffset, ReturnAddressPtr returnAddress)
617 RepatchBuffer repatchBuffer(codeBlock);
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));
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));
629 void JIT::patchPutByIdReplace(CodeBlock* codeBlock, StructureStubInfo* stubInfo, Structure* structure, PropertyOffset cachedOffset, ReturnAddressPtr returnAddress, bool direct)
631 RepatchBuffer repatchBuffer(codeBlock);
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));
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));
643 void JIT::privateCompilePatchGetArrayLength(ReturnAddressPtr returnAddress)
645 StructureStubInfo* stubInfo = &m_codeBlock->getStubInfo(returnAddress);
647 // Check eax is an array
648 Jump failureCases1 = branchPtr(NotEqual, Address(regT0, JSCell::classInfoOffset()), TrustedImmPtr(&JSArray::s_info));
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));
655 emitFastArithIntToImmNoCheck(regT2, regT0);
656 Jump success = jump();
658 LinkBuffer patchBuffer(*m_globalData, this, m_codeBlock);
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);
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));
668 // Track the stub we have created so that it will be deleted later.
669 stubInfo->stubRoutine = FINALIZE_CODE_FOR_STUB(
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()));
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()));
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));
684 void JIT::privateCompileGetByIdProto(StructureStubInfo* stubInfo, Structure* structure, Structure* prototypeStructure, const Identifier& ident, const PropertySlot& slot, PropertyOffset cachedOffset, ReturnAddressPtr returnAddress, CallFrame* callFrame)
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));
690 // Check eax is an object of the right Structure.
691 Jump failureCases1 = checkStructure(regT0, structure);
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));
697 bool needsStubLink = false;
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()));
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()));
717 compileGetDirectOffset(protoObject, regT0, cachedOffset);
718 Jump success = jump();
719 LinkBuffer patchBuffer(*m_globalData, this, m_codeBlock);
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);
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));
730 for (Vector<CallRecord>::iterator iter = m_calls.begin(); iter != m_calls.end(); ++iter) {
732 patchBuffer.link(iter->from, FunctionPtr(iter->to));
735 // Track the stub we have created so that it will be deleted later.
736 stubInfo->stubRoutine = createJITStubRoutine(
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())),
743 m_codeBlock->ownerExecutable(),
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()));
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));
755 void JIT::privateCompileGetByIdSelfList(StructureStubInfo* stubInfo, PolymorphicAccessStructureList* polymorphicStructures, int currentIndex, Structure* structure, const Identifier& ident, const PropertySlot& slot, PropertyOffset cachedOffset)
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()));
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()));
778 compileGetDirectOffset(regT0, regT0, cachedOffset);
780 Jump success = jump();
782 LinkBuffer patchBuffer(*m_globalData, this, m_codeBlock);
785 for (Vector<CallRecord>::iterator iter = m_calls.begin(); iter != m_calls.end(); ++iter) {
787 patchBuffer.link(iter->from, FunctionPtr(iter->to));
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));
794 lastProtoBegin = stubInfo->callReturnLocation.labelAtOffset(-stubInfo->patch.baseline.u.get.coldPathBegin);
796 patchBuffer.link(failureCase, lastProtoBegin);
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));
801 RefPtr<JITStubRoutine> stubCode = createJITStubRoutine(
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())),
808 m_codeBlock->ownerExecutable(),
811 polymorphicStructures->list[currentIndex].set(*m_globalData, m_codeBlock->ownerExecutable(), stubCode, structure, isDirect);
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()));
819 void JIT::privateCompileGetByIdProtoList(StructureStubInfo* stubInfo, PolymorphicAccessStructureList* prototypeStructures, int currentIndex, Structure* structure, Structure* prototypeStructure, const Identifier& ident, const PropertySlot& slot, PropertyOffset cachedOffset, CallFrame* callFrame)
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));
825 // Check eax is an object of the right Structure.
826 Jump failureCases1 = checkStructure(regT0, structure);
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));
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()));
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()));
853 compileGetDirectOffset(protoObject, regT0, cachedOffset);
856 Jump success = jump();
858 LinkBuffer patchBuffer(*m_globalData, this, m_codeBlock);
861 for (Vector<CallRecord>::iterator iter = m_calls.begin(); iter != m_calls.end(); ++iter) {
863 patchBuffer.link(iter->from, FunctionPtr(iter->to));
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);
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));
875 RefPtr<JITStubRoutine> stubCode = createJITStubRoutine(
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())),
882 m_codeBlock->ownerExecutable(),
884 prototypeStructures->list[currentIndex].set(*m_globalData, m_codeBlock->ownerExecutable(), stubCode, structure, prototypeStructure, isDirect);
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()));
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)
895 JumpList bucketsOfFail;
897 // Check eax is an object of the right Structure.
898 Jump baseObjectCheck = checkStructure(regT0, structure);
899 bucketsOfFail.append(baseObjectCheck);
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);
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()));
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()));
931 compileGetDirectOffset(protoObject, regT0, cachedOffset);
933 Jump success = jump();
935 LinkBuffer patchBuffer(*m_globalData, this, m_codeBlock);
938 for (Vector<CallRecord>::iterator iter = m_calls.begin(); iter != m_calls.end(); ++iter) {
940 patchBuffer.link(iter->from, FunctionPtr(iter->to));
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));
947 patchBuffer.link(bucketsOfFail, lastProtoBegin);
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));
952 RefPtr<JITStubRoutine> stubRoutine = createJITStubRoutine(
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())),
959 m_codeBlock->ownerExecutable(),
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);
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()));
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)
975 JumpList bucketsOfFail;
977 // Check eax is an object of the right Structure.
978 bucketsOfFail.append(checkStructure(regT0, structure));
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);
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()));
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()));
1008 compileGetDirectOffset(protoObject, regT0, cachedOffset);
1009 Jump success = jump();
1011 LinkBuffer patchBuffer(*m_globalData, this, m_codeBlock);
1013 if (needsStubLink) {
1014 for (Vector<CallRecord>::iterator iter = m_calls.begin(); iter != m_calls.end(); ++iter) {
1016 patchBuffer.link(iter->from, FunctionPtr(iter->to));
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));
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));
1026 // Track the stub we have created so that it will be deleted later.
1027 RefPtr<JITStubRoutine> stubRoutine = createJITStubRoutine(
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())),
1034 m_codeBlock->ownerExecutable(),
1036 stubInfo->stubRoutine = stubRoutine;
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()));
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));
1047 void JIT::emit_op_get_scoped_var(Instruction* currentInstruction)
1049 int skip = currentInstruction[3].u.operand;
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;
1057 activationNotCreated = branchTestPtr(Zero, addressFor(m_codeBlock->activationRegister()));
1058 loadPtr(Address(regT0, OBJECT_OFFSETOF(ScopeChainNode, next)), regT0);
1059 activationNotCreated.link(this);
1062 loadPtr(Address(regT0, OBJECT_OFFSETOF(ScopeChainNode, next)), regT0);
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);
1071 void JIT::emit_op_put_scoped_var(Instruction* currentInstruction)
1073 int skip = currentInstruction[2].u.operand;
1075 emitGetVirtualRegister(currentInstruction[3].u.operand, regT0);
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;
1083 activationNotCreated = branchTestPtr(Zero, addressFor(m_codeBlock->activationRegister()));
1084 loadPtr(Address(regT1, OBJECT_OFFSETOF(ScopeChainNode, next)), regT1);
1085 activationNotCreated.link(this);
1088 loadPtr(Address(regT1, OBJECT_OFFSETOF(ScopeChainNode, next)), regT1);
1089 loadPtr(Address(regT1, OBJECT_OFFSETOF(ScopeChainNode, object)), regT1);
1091 emitWriteBarrier(regT1, regT0, regT2, regT3, ShouldFilterImmediates, WriteBarrierForVariableAccess);
1093 loadPtr(Address(regT1, JSVariableObject::offsetOfRegisters()), regT1);
1094 storePtr(regT0, Address(regT1, currentInstruction[1].u.operand * sizeof(Register)));
1097 void JIT::emit_op_get_global_var(Instruction* currentInstruction)
1099 loadPtr(currentInstruction[2].u.registerPointer, regT0);
1100 emitValueProfilingSite();
1101 emitPutVirtualRegister(currentInstruction[1].u.operand);
1104 void JIT::emit_op_put_global_var(Instruction* currentInstruction)
1106 JSGlobalObject* globalObject = m_codeBlock->globalObject();
1108 emitGetVirtualRegister(currentInstruction[2].u.operand, regT0);
1110 storePtr(regT0, currentInstruction[1].u.registerPointer);
1111 if (Heap::isWriteBarrierEnabled())
1112 emitWriteBarrier(globalObject, regT0, regT2, ShouldFilterImmediates, WriteBarrierForVariableAccess);
1115 void JIT::emit_op_put_global_var_check(Instruction* currentInstruction)
1117 emitGetVirtualRegister(currentInstruction[2].u.operand, regT0);
1119 addSlowCase(branchTest8(NonZero, AbsoluteAddress(currentInstruction[3].u.predicatePointer)));
1121 JSGlobalObject* globalObject = m_codeBlock->globalObject();
1123 storePtr(regT0, currentInstruction[1].u.registerPointer);
1124 if (Heap::isWriteBarrierEnabled())
1125 emitWriteBarrier(globalObject, regT0, regT2, ShouldFilterImmediates, WriteBarrierForVariableAccess);
1128 void JIT::emitSlow_op_put_global_var_check(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
1132 JITStubCall stubCall(this, cti_op_put_global_var_check);
1133 stubCall.addArgument(regT0);
1134 stubCall.addArgument(TrustedImm32(currentInstruction[4].u.operand));
1138 void JIT::resetPatchGetById(RepatchBuffer& repatchBuffer, StructureStubInfo* stubInfo)
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));
1146 void JIT::resetPatchPutById(RepatchBuffer& repatchBuffer, StructureStubInfo* stubInfo)
1148 if (isDirectPutById(stubInfo))
1149 repatchBuffer.relink(stubInfo->callReturnLocation, cti_op_put_by_id_direct);
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);
1156 #endif // USE(JSVALUE64)
1158 void JIT::emitWriteBarrier(RegisterID owner, RegisterID value, RegisterID scratch, RegisterID scratch2, WriteBarrierMode mode, WriteBarrierUseKind useKind)
1160 UNUSED_PARAM(owner);
1161 UNUSED_PARAM(scratch);
1162 UNUSED_PARAM(scratch2);
1163 UNUSED_PARAM(useKind);
1164 UNUSED_PARAM(value);
1166 ASSERT(owner != scratch);
1167 ASSERT(owner != scratch2);
1169 #if ENABLE(WRITE_BARRIER_PROFILING)
1170 emitCount(WriteBarrierCounters::jitCounterFor(useKind));
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()));
1189 if (mode == ShouldFilterImmediates)
1190 filterCells.link(this);
1194 void JIT::emitWriteBarrier(JSCell* owner, RegisterID value, RegisterID scratch, WriteBarrierMode mode, WriteBarrierUseKind useKind)
1196 UNUSED_PARAM(owner);
1197 UNUSED_PARAM(scratch);
1198 UNUSED_PARAM(useKind);
1199 UNUSED_PARAM(value);
1202 #if ENABLE(WRITE_BARRIER_PROFILING)
1203 emitCount(WriteBarrierCounters::jitCounterFor(useKind));
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);
1218 void JIT::testPrototype(JSValue prototype, JumpList& failureCases)
1220 if (prototype.isNull())
1223 ASSERT(prototype.isCell());
1224 move(TrustedImmPtr(prototype.asCell()), regT3);
1225 failureCases.append(branchPtr(NotEqual, Address(regT3, JSCell::structureOffset()), TrustedImmPtr(prototype.asCell()->structure())));
1228 void JIT::patchMethodCallProto(JSGlobalData& globalData, CodeBlock* codeBlock, MethodCallLinkInfo& methodCallLinkInfo, StructureStubInfo& stubInfo, JSObject* callee, Structure* structure, JSObject* proto, ReturnAddressPtr returnAddress)
1230 RepatchBuffer repatchBuffer(codeBlock);
1232 CodeLocationDataLabelPtr structureLocation = methodCallLinkInfo.cachedStructure.location();
1233 methodCallLinkInfo.cachedStructure.set(globalData, structureLocation, codeBlock->ownerExecutable(), structure);
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));
1242 bool JIT::isDirectPutById(StructureStubInfo* stubInfo)
1244 switch (stubInfo->accessType) {
1245 case access_put_by_id_transition_normal:
1247 case access_put_by_id_transition_direct:
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))
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));
1262 ASSERT_NOT_REACHED();
1269 #endif // ENABLE(JIT)