/*
 * Copyright (C) 2011 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

#include "config.h"
#include "Repatch.h"

#if ENABLE(JIT)

#include "AccessorCallJITStubRoutine.h"
#include "BinarySwitch.h"
#include "CCallHelpers.h"
#include "CallFrameInlines.h"
#include "GCAwareJITStubRoutine.h"
#include "GetterSetter.h"
#include "LinkBuffer.h"
#include "ObjectPropertyConditionSet.h"
#include "Operations.h"
#include "PolymorphicGetByIdList.h"
#include "PolymorphicPutByIdList.h"
#include "ScratchRegisterAllocator.h"
#include "StructureRareDataInlines.h"
#include "StructureStubClearingWatchpoint.h"
#include "ThunkGenerators.h"
#include <wtf/MemoryMonitor.h>
#include <wtf/StringPrintStream.h>

namespace JSC {

// Beware: in this code, it is not safe to assume anything about the following registers
// that would ordinarily have well-known values:
// - tagTypeNumberRegister
// - tagMaskRegister
// - callFrameRegister **
//
// The callFrameRegister is a double-FIXME:
// - https://bugs.webkit.org/show_bug.cgi?id=123085
//   We're not yet done getting rid of past uses of callFrameRegister.
// - https://bugs.webkit.org/show_bug.cgi?id=113621
//   Once the calling convention work is done, we won't need the callFrameRegister anymore
//   since we'll just use framePointerRegister.

static FunctionPtr readCallTarget(CodeBlock* codeBlock, CodeLocationCall call)
{
    FunctionPtr result = MacroAssembler::readCallTarget(call);
#if ENABLE(FTL_JIT)
    if (codeBlock->jitType() == JITCode::FTLJIT) {
        return FunctionPtr(codeBlock->vm()->ftlThunks->keyForSlowPathCallThunk(
            MacroAssemblerCodePtr::createFromExecutableAddress(
                result.executableAddress())).callTarget());
    }
#else
    UNUSED_PARAM(codeBlock);
#endif // ENABLE(FTL_JIT)
    return result;
}

static void repatchCall(CodeBlock* codeBlock, CodeLocationCall call, FunctionPtr newCalleeFunction)
{
#if ENABLE(FTL_JIT)
    if (codeBlock->jitType() == JITCode::FTLJIT) {
        VM& vm = *codeBlock->vm();
        FTL::Thunks& thunks = *vm.ftlThunks;
        FTL::SlowPathCallKey key = thunks.keyForSlowPathCallThunk(
            MacroAssemblerCodePtr::createFromExecutableAddress(
                MacroAssembler::readCallTarget(call).executableAddress()));
        key = key.withCallTarget(newCalleeFunction.executableAddress());
        newCalleeFunction = FunctionPtr(
            thunks.getSlowPathCallThunk(vm, key).code().executableAddress());
    }
#else // ENABLE(FTL_JIT)
    UNUSED_PARAM(codeBlock);
#endif // ENABLE(FTL_JIT)
    MacroAssembler::repatchCall(call, newCalleeFunction);
}

static void repatchByIdSelfAccess(VM& vm, CodeBlock* codeBlock, StructureStubInfo& stubInfo, Structure* structure, const Identifier& propertyName, PropertyOffset offset,
	    const FunctionPtr &slowPathFunction, bool compact)
{
    if (structure->needImpurePropertyWatchpoint())
        vm.registerWatchpointForImpureProperty(propertyName, stubInfo.addWatchpoint(codeBlock));

    // Only optimize once!
    repatchCall(codeBlock, stubInfo.callReturnLocation, slowPathFunction);

    // Patch the structure check & the offset of the load.
    MacroAssembler::repatchPointer(
		stubInfo.callReturnLocation.dataLabelPtrAtOffset(-(intptr_t)stubInfo.patch.deltaCheckImmToCall),
		structure);
    CodeLocationConvertibleLoad convertibleLoad = stubInfo.callReturnLocation.convertibleLoadAtOffset(stubInfo.patch.deltaCallToStorageLoad);
    if (isOutOfLineOffset(offset))
        MacroAssembler::replaceWithLoad(convertibleLoad);
    else
        MacroAssembler::replaceWithAddressComputation(convertibleLoad);
#if USE(JSVALUE64)
    if (compact)
    	MacroAssembler::repatchCompact(stubInfo.callReturnLocation.dataLabelCompactAtOffset(stubInfo.patch.deltaCallToLoadOrStore), offsetRelativeToPatchedStorage(offset));
    else
    	MacroAssembler::repatchInt32(stubInfo.callReturnLocation.dataLabel32AtOffset(stubInfo.patch.deltaCallToLoadOrStore), offsetRelativeToPatchedStorage(offset));
#elif USE(JSVALUE32_64)
    if (compact) {
    	MacroAssembler::repatchCompact(stubInfo.callReturnLocation.dataLabelCompactAtOffset(stubInfo.patch.deltaCallToTagLoadOrStore), offsetRelativeToPatchedStorage(offset) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.tag));
    	MacroAssembler::repatchCompact(stubInfo.callReturnLocation.dataLabelCompactAtOffset(stubInfo.patch.deltaCallToPayloadLoadOrStore), offsetRelativeToPatchedStorage(offset) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.payload));
    } else {
    	MacroAssembler::repatchInt32(stubInfo.callReturnLocation.dataLabel32AtOffset(stubInfo.patch.deltaCallToTagLoadOrStore), offsetRelativeToPatchedStorage(offset) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.tag));
    	MacroAssembler::repatchInt32(stubInfo.callReturnLocation.dataLabel32AtOffset(stubInfo.patch.deltaCallToPayloadLoadOrStore), offsetRelativeToPatchedStorage(offset) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.payload));
    }
#endif
}

static void checkObjectPropertyCondition(
    const ObjectPropertyCondition& condition, CodeBlock* codeBlock, StructureStubInfo& stubInfo,
    MacroAssembler& jit, MacroAssembler::JumpList& failureCases, GPRReg scratchGPR)
{
    if (condition.isWatchableAssumingImpurePropertyWatchpoint(*codeBlock->vm())) {
        condition.object()->structure()->addTransitionWatchpoint(
            stubInfo.addWatchpoint(codeBlock, condition));
        return;
    }

    Structure* structure = condition.object()->structure();
    RELEASE_ASSERT(condition.structureEnsuresValidityAssumingImpurePropertyWatchpoint(structure));
    jit.move(MacroAssembler::TrustedImmPtr(condition.object()), scratchGPR);
    failureCases.append(
        jit.branchPtr(
            MacroAssembler::NotEqual,
            MacroAssembler::Address(scratchGPR, JSCell::structureOffset()),
            MacroAssembler::TrustedImmPtr(structure)));
}

static void checkObjectPropertyConditions(
    const ObjectPropertyConditionSet& set, CodeBlock* codeBlock, StructureStubInfo& stubInfo,
    MacroAssembler& jit, MacroAssembler::JumpList& failureCases, GPRReg scratchGPR)
{
    for (auto iter = set.begin(); iter != set.end(); ++iter) {
    	const ObjectPropertyCondition& condition = *iter;
        checkObjectPropertyCondition(
            condition, codeBlock, stubInfo, jit, failureCases, scratchGPR);
    }
}

static void replaceWithJump(StructureStubInfo& stubInfo, const MacroAssemblerCodePtr target)
{
    if (MacroAssembler::canJumpReplacePatchableBranchPtrWithPatch()) {
    	 MacroAssembler::replaceWithJump(
			MacroAssembler::startOfPatchableBranchPtrWithPatchOnAddress(
                stubInfo.callReturnLocation.dataLabelPtrAtOffset(
                    -(intptr_t)stubInfo.patch.deltaCheckImmToCall)),
            CodeLocationLabel(target));
        return;
    }

    MacroAssembler::repatchJump(
        stubInfo.callReturnLocation.jumpAtOffset(
            stubInfo.patch.deltaCallToJump),
        CodeLocationLabel(target));
}

static void emitRestoreScratch(MacroAssembler& stubJit, bool needToRestoreScratch, GPRReg scratchGPR, MacroAssembler::Jump& success, MacroAssembler::Jump& fail, MacroAssembler::JumpList failureCases)
{
    if (needToRestoreScratch) {
        stubJit.popToRestore(scratchGPR);
        
        success = stubJit.jump();
        
        // link failure cases here, so we can pop scratchGPR, and then jump back.
        failureCases.link(&stubJit);
        
        stubJit.popToRestore(scratchGPR);
        
        fail = stubJit.jump();
        return;
    }
    
    success = stubJit.jump();
}

static void linkRestoreScratch(LinkBuffer& patchBuffer, bool needToRestoreScratch, MacroAssembler::Jump success, MacroAssembler::Jump fail, MacroAssembler::JumpList failureCases, CodeLocationLabel successLabel, CodeLocationLabel slowCaseLabel)
{
    patchBuffer.link(success, successLabel);
        
    if (needToRestoreScratch) {
        patchBuffer.link(fail, slowCaseLabel);
        return;
    }
    
    // link failure cases directly back to normal path
    patchBuffer.link(failureCases, slowCaseLabel);
}

static void linkRestoreScratch(LinkBuffer& patchBuffer, bool needToRestoreScratch, StructureStubInfo& stubInfo, MacroAssembler::Jump success, MacroAssembler::Jump fail, MacroAssembler::JumpList failureCases)
{
    linkRestoreScratch(patchBuffer, needToRestoreScratch, success, fail, failureCases, stubInfo.callReturnLocation.labelAtOffset(stubInfo.patch.deltaCallToDone), stubInfo.callReturnLocation.labelAtOffset(stubInfo.patch.deltaCallToSlowCase));
}

static FunctionPtr customFor(const PropertySlot& slot)
{
    if (!slot.isCacheableCustom())
        return FunctionPtr();
    return FunctionPtr(slot.customGetter());
}

static FunctionPtr customFor(const PutPropertySlot& slot)
{
    if (!slot.isCacheableCustom())
        return FunctionPtr();
    return FunctionPtr(slot.customSetter());
}

// EncodedJSValue in JSVALUE32_64 is a 64-bit integer. When being compiled in ARM EABI, it must be aligned on an even-numbered register (r0, r2 or [sp]).
// To prevent the assembler from using wrong registers, let's occupy r1 or r3 with a dummy argument when necessary.
#if (COMPILER_SUPPORTS(EABI) && CPU(ARM)) || CPU(MIPS)
#define EABI_32BIT_DUMMY_ARG      CCallHelpers::TrustedImm32(0),
#else
#define EABI_32BIT_DUMMY_ARG
#endif

static bool generateByIdStub(
    ExecState* exec, AccessCase::AccessType accessType, const Identifier& propertyName,
    FunctionPtr custom, StructureStubInfo& stubInfo, const ObjectPropertyConditionSet& conditionSet,
	JSObject* alternateBase, PropertyOffset offset, Structure* structure, bool loadTargetFromProxy,
	WatchpointSet* watchpointSet, CodeLocationLabel successLabel, CodeLocationLabel slowCaseLabel,
	RefPtr<JITStubRoutine>& stubRoutine)
{
    ASSERT(conditionSet.structuresEnsureValidityAssumingImpurePropertyWatchpoint());

    VM* vm = &exec->vm();

    GPRReg baseGPR = static_cast<GPRReg>(stubInfo.patch.baseGPR);
    JSValueRegs valueRegs = JSValueRegs(
#if USE(JSVALUE32_64)
        static_cast<GPRReg>(stubInfo.patch.valueTagGPR),
#endif
        static_cast<GPRReg>(stubInfo.patch.valueGPR));

    ScratchRegisterAllocator allocator(stubInfo.patch.usedRegisters);
    allocator.lock(baseGPR);
    allocator.lock(valueRegs);

    GPRReg scratchGPR = allocator.allocateScratchGPR();

    RELEASE_ASSERT(accessType == AccessCase::WatchedStub || accessType == AccessCase::SimpleStub || accessType == AccessCase::SimpleMiss);

	CCallHelpers stubJit(vm, exec->codeBlock());

    allocator.preserveReusedRegistersByPushing(stubJit);
    
    MacroAssembler::JumpList failureCases;

    GPRReg baseForGetGPR;
    if (loadTargetFromProxy) {
        baseForGetGPR = valueRegs.payloadGPR();
        stubJit.loadPtr(MacroAssembler::Address(baseGPR, JSCell::structureOffset()), scratchGPR);
        failureCases.append(stubJit.branch8(
            MacroAssembler::NotEqual,
            MacroAssembler::Address(scratchGPR, Structure::typeInfoTypeOffset()),
            MacroAssembler::TrustedImm32(PureForwardingProxyType)));

        stubJit.loadPtr(MacroAssembler::Address(baseGPR, JSProxy::targetOffset()), scratchGPR);

        failureCases.append(stubJit.branchPtr(
            MacroAssembler::NotEqual,
            MacroAssembler::Address(scratchGPR, JSCell::structureOffset()),
			MacroAssembler::TrustedImmPtr(structure)));
    } else {
        baseForGetGPR = baseGPR;

		failureCases.append(stubJit.branchPtr(
			MacroAssembler::NotEqual,
			MacroAssembler::Address(baseForGetGPR, JSCell::structureOffset()),
			MacroAssembler::TrustedImmPtr(structure)));
    }

    CodeBlock* codeBlock = exec->codeBlock();
    if (structure->needImpurePropertyWatchpoint() || conditionSet.needImpurePropertyWatchpoint())
    	vm->registerWatchpointForImpureProperty(propertyName, stubInfo.addWatchpoint(codeBlock));

    if (watchpointSet)
        watchpointSet->add(stubInfo.addWatchpoint(codeBlock));

    checkObjectPropertyConditions(
        conditionSet, codeBlock, stubInfo, stubJit, failureCases, scratchGPR);

    if (isValidOffset(offset)) {
        Structure* currStructure;
        if (conditionSet.isEmpty())
            currStructure = structure;
        else
            currStructure = conditionSet.slotBaseCondition().object()->structure();
        currStructure->startWatchingPropertyForReplacements(*vm, offset);
    }

    GPRReg baseForAccessGPR = InvalidGPRReg;
    if (accessType != AccessCase::SimpleMiss) {
        if (!conditionSet.isEmpty()) {
			// We could have clobbered scratchGPR earlier, so we have to reload from baseGPR to get the target.
			if (loadTargetFromProxy)
				stubJit.loadPtr(MacroAssembler::Address(baseGPR, JSProxy::targetOffset()), baseForGetGPR);

			stubJit.move(MacroAssembler::TrustedImmPtr(alternateBase), scratchGPR);
			baseForAccessGPR = scratchGPR;
		} else {
			// For proxy objects, we need to do all the Structure checks before moving the baseGPR into
			// baseForGetGPR because if we fail any of the checks then we would have the wrong value in baseGPR
			// on the slow path.
			if (loadTargetFromProxy)
				stubJit.move(scratchGPR, baseForGetGPR);
			baseForAccessGPR = baseForGetGPR;
		}
    }

    GPRReg loadedValueGPR = InvalidGPRReg;
    if (accessType == AccessCase::SimpleMiss)
		stubJit.moveTrustedValue(jsUndefined(), valueRegs);
    else if (accessType != AccessCase::CustomValueGetter && accessType != AccessCase::CustomAccessorGetter && accessType != AccessCase::CustomValueSetter && accessType != AccessCase::CustomAccessorSetter) {
        if (accessType == AccessCase::WatchedStub || accessType == AccessCase::SimpleStub)
            loadedValueGPR = valueRegs.payloadGPR();
        else
            loadedValueGPR = scratchGPR;

        GPRReg storageGPR;
        if (isInlineOffset(offset))
            storageGPR = baseForAccessGPR;
        else {
            stubJit.loadPtr(MacroAssembler::Address(baseForAccessGPR, JSObject::butterflyOffset()), loadedValueGPR);
            storageGPR = loadedValueGPR;
        }

#if USE(JSVALUE64)
        stubJit.load64(MacroAssembler::Address(storageGPR, offsetRelativeToBase(offset)), loadedValueGPR);
#else
        stubJit.load32(MacroAssembler::Address(storageGPR, offsetRelativeToBase(offset) + TagOffset), valueRegs.tagGPR());
        stubJit.load32(MacroAssembler::Address(storageGPR, offsetRelativeToBase(offset) + PayloadOffset), loadedValueGPR);
#endif
    }

    // Stuff for custom getters.
    MacroAssembler::Call operationCall;
    MacroAssembler::Call handlerCall;

    // Stuff for JS getters.
    MacroAssembler::DataLabelPtr addressOfLinkFunctionCheck;
    MacroAssembler::Call fastPathCall;
    MacroAssembler::Call slowPathCall;
    OwnPtr<CallLinkInfo> callLinkInfo;

    MacroAssembler::Jump success, fail;
    if (accessType != AccessCase::WatchedStub && accessType != AccessCase::SimpleStub && accessType != AccessCase::SimpleMiss) {
        // Need to make sure that whenever this call is made in the future, we remember the
        // place that we made it from. It just so happens to be the place that we are at
        // right now!
        stubJit.store32(MacroAssembler::TrustedImm32(stubInfo.callSiteIndex.bits()),
            CCallHelpers::tagFor(static_cast<VirtualRegister>(JSStack::ArgumentCount)));

    	if (accessType == AccessCase::Getter || accessType == AccessCase::Setter){
            callLinkInfo = adoptPtr(new CallLinkInfo());

            // FIXME: If we generated a polymorphic call stub that jumped back to the getter
            // stub, which then jumped back to the main code, then we'd have a reachability
            // situation that the GC doesn't know about. The GC would ensure that the polymorphic
            // call stub stayed alive, and it would ensure that the main code stayed alive, but
            // it wouldn't know that the getter stub was alive. Ideally JIT stub routines would
            // be GC objects, and then we'd be able to say that the polymorphic call stub has a
            // reference to the getter stub.
            // https://bugs.webkit.org/show_bug.cgi?id=148914
            callLinkInfo->disallowStubs();

            callLinkInfo->setUpCall(CallLinkInfo::Call, stubInfo.codeOrigin, loadedValueGPR);

            if (accessType == AccessCase::Getter)
            	stubJit.setupArgumentsWithExecState(baseForGetGPR, loadedValueGPR);
		}
		else {
			// getter: EncodedJSValue (*GetValueFunc)(ExecState*, EncodedJSValue thisValue, PropertyName);
			// setter: void (*PutValueFunc)(ExecState*, EncodedJSValue thisObject, EncodedJSValue value);
            // Custom values are passed the slotBase (the property holder), custom accessors are passed the thisVaule (reciever).
            GPRReg baseForCustomValue = accessType == AccessCase::CustomValueGetter || accessType == AccessCase::CustomValueSetter ? baseForAccessGPR : baseForGetGPR;
#if USE(JSVALUE64)
            if (accessType == AccessCase::CustomValueGetter || accessType == AccessCase::CustomAccessorGetter) {
                stubJit.setupArgumentsWithExecState(
					baseForCustomValue,
					MacroAssembler::TrustedImmPtr(propertyName.impl()));
            } else
                stubJit.setupArgumentsWithExecState(baseForCustomValue, valueRegs.gpr());
#else
            if (accessType == AccessCase::CustomValueGetter || accessType == AccessCase::CustomAccessorGetter) {
                stubJit.setupArgumentsWithExecState(
					EABI_32BIT_DUMMY_ARG baseForCustomValue,
					MacroAssembler::TrustedImm32(JSValue::CellTag),
					MacroAssembler::TrustedImmPtr(propertyName.impl()));
            } else {
                stubJit.setupArgumentsWithExecState(
					EABI_32BIT_DUMMY_ARG baseForCustomValue,
					MacroAssembler::TrustedImm32(JSValue::CellTag),
					valueRegs.payloadGPR(), valueRegs.tagGPR());
            }
#endif
		}

		stubJit.storePtr(GPRInfo::callFrameRegister, &vm->topCallFrame);

		operationCall = stubJit.call();
        if (accessType == AccessCase::Getter || accessType == AccessCase::CustomValueGetter || accessType == AccessCase::CustomAccessorGetter)
            stubJit.setupResults(valueRegs);
		MacroAssembler::Jump noException = stubJit.emitExceptionCheck(CCallHelpers::InvertedExceptionCheck);

		stubJit.setupArguments(CCallHelpers::TrustedImmPtr(vm), GPRInfo::callFrameRegister);
		handlerCall = stubJit.call();
		stubJit.jumpToExceptionHandler();

		noException.link(&stubJit);
	}

    if (allocator.didReuseRegisters()) {
        allocator.restoreReusedRegistersByPopping(stubJit);
        success = stubJit.jump();

        failureCases.link(&stubJit);
        allocator.restoreReusedRegistersByPopping(stubJit);
        fail = stubJit.jump();
    } else
        success = stubJit.jump();
    
    LinkBuffer patchBuffer(*vm, &stubJit, exec->codeBlock(), JITCompilationCanFail, true);
    if (patchBuffer.didFailToAllocate())
		return false;

    patchBuffer.link(success, successLabel);
    if (allocator.didReuseRegisters())
        patchBuffer.link(fail, slowCaseLabel);
    else
        patchBuffer.link(failureCases, slowCaseLabel);

    if (accessType == AccessCase::CustomValueGetter || accessType == AccessCase::CustomAccessorGetter || accessType == AccessCase::CustomValueSetter || accessType == AccessCase::CustomAccessorSetter) {
		patchBuffer.link(operationCall, custom);
		patchBuffer.link(handlerCall, lookupExceptionHandler);
    } else if (accessType == AccessCase::Getter) {
		patchBuffer.link(operationCall, operationCallGetter);
		patchBuffer.link(handlerCall, lookupExceptionHandler);
    } else if (accessType == AccessCase::Setter){
    	//
    }

    MacroAssemblerCodeRef code = FINALIZE_CODE(
        patchBuffer,
        ("Get access stub for CodeBlock %p, return point %p",
         exec->codeBlock(), successLabel.executableAddress()));

    if (accessType == AccessCase::Getter || accessType == AccessCase::Setter)
        stubRoutine = adoptRef(new AccessorCallJITStubRoutine(code, *vm, callLinkInfo.release()));
    else
        stubRoutine = createJITStubRoutine(code, *vm, codeBlock->ownerExecutable(), true);

    return true;
}

enum InlineCacheAction {
    GiveUpOnCache,
    RetryCacheLater,
    AttemptToCache
};

static InlineCacheAction actionForCell(VM& vm, JSCell* cell)
{
    Structure* structure = cell->structure(vm);

    TypeInfo typeInfo = structure->typeInfo();
    if (typeInfo.prohibitsPropertyCaching())
        return GiveUpOnCache;

    if (structure->isUncacheableDictionary()) {
        if (structure->hasBeenFlattenedBefore())
            return GiveUpOnCache;
        // Flattening could have changed the offset, so return early for another try.
        asObject(cell)->flattenDictionaryObject(vm);
        return RetryCacheLater;
    }

    if (!structure->propertyAccessesAreCacheable())
        return GiveUpOnCache;

    return AttemptToCache;
}

static InlineCacheAction tryCacheGetByID(ExecState* exec, JSValue baseValue, const Identifier& propertyName, const PropertySlot& slot, StructureStubInfo& stubInfo)
{
	if(ExecutableAllocator::underMemoryPressure() || WTF::gLowMemory)
        return GiveUpOnCache;

    // FIXME: Write a test that proves we need to check for recursion here just
    // like the interpreter does, then add a check for recursion.

    CodeBlock* codeBlock = exec->codeBlock();
    VM& vm = exec->vm();
    
    if(Options::forceICFailure())
    	return GiveUpOnCache;

    // FIXME: Cache property access for immediates.
    if (!baseValue.isCell())
        return GiveUpOnCache;

    JSCell* baseCell = baseValue.asCell();
    Structure* structure = baseCell->structure(vm);

    if ((isJSArray(baseValue) || isJSString(baseValue)) && propertyName == vm.propertyNames->length) {
        GPRReg baseGPR = static_cast<GPRReg>(stubInfo.patch.baseGPR);
#if USE(JSVALUE32_64)
        GPRReg resultTagGPR = static_cast<GPRReg>(stubInfo.patch.valueTagGPR);
#endif
        GPRReg resultGPR = static_cast<GPRReg>(stubInfo.patch.valueGPR);
        
        MacroAssembler stubJit;

        if (isJSArray(baseValue)) {
			GPRReg scratchGPR = TempRegisterSet(stubInfo.patch.usedRegisters).getFreeGPR();
			bool needToRestoreScratch = false;

			if (scratchGPR == InvalidGPRReg) {
#if USE(JSVALUE64)
				scratchGPR = AssemblyHelpers::selectScratchGPR(baseGPR, resultGPR);
#else
				scratchGPR = AssemblyHelpers::selectScratchGPR(baseGPR, resultGPR, resultTagGPR);
#endif
				stubJit.pushToSave(scratchGPR);
				needToRestoreScratch = true;
			}
        
			MacroAssembler::JumpList failureCases;

			stubJit.loadPtr(MacroAssembler::Address(baseGPR, JSCell::structureOffset()), scratchGPR);
			stubJit.load8(MacroAssembler::Address(scratchGPR, Structure::indexingTypeOffset()), scratchGPR);
			failureCases.append(stubJit.branchTest32(MacroAssembler::Zero, scratchGPR, MacroAssembler::TrustedImm32(IsArray)));
			failureCases.append(stubJit.branchTest32(MacroAssembler::Zero, scratchGPR, MacroAssembler::TrustedImm32(IndexingShapeMask)));

			stubJit.loadPtr(MacroAssembler::Address(baseGPR, JSObject::butterflyOffset()), scratchGPR);
			stubJit.load32(MacroAssembler::Address(scratchGPR, ArrayStorage::lengthOffset()), scratchGPR);
			failureCases.append(stubJit.branch32(MacroAssembler::LessThan, scratchGPR, MacroAssembler::TrustedImm32(0)));

	        stubJit.move(scratchGPR, resultGPR);
#if USE(JSVALUE64)
	        stubJit.or64(AssemblyHelpers::TrustedImm64(TagTypeNumber), resultGPR);
#elif USE(JSVALUE32_64)
			stubJit.move(AssemblyHelpers::TrustedImm32(0xffffffff), resultTagGPR); // JSValue::Int32Tag
#endif

			MacroAssembler::Jump success, fail;

			emitRestoreScratch(stubJit, needToRestoreScratch, scratchGPR, success, fail, failureCases);

			LinkBuffer patchBuffer(vm, &stubJit, codeBlock, JITCompilationCanFail, true);
            if (patchBuffer.didFailToAllocate())
                return GiveUpOnCache;

			linkRestoreScratch(patchBuffer, needToRestoreScratch, stubInfo, success, fail, failureCases);

			stubInfo.stubRoutine = FINALIZE_CODE_FOR_DFG_STUB(
				patchBuffer,
				("DFG GetById array length stub for CodeBlock %p, return point %p",
				 exec->codeBlock(), stubInfo.callReturnLocation.labelAtOffset(
					 stubInfo.patch.deltaCallToDone).executableAddress()));

			replaceWithJump(stubInfo, stubInfo.stubRoutine->code().code());
			repatchCall(codeBlock, stubInfo.callReturnLocation, operationGetById);

			return RetryCacheLater;
		}

        // String.length case
		GPRReg scratchGPR = TempRegisterSet(stubInfo.patch.usedRegisters).getFreeGPR();
		bool needToRestoreScratch = false;

		if (scratchGPR == InvalidGPRReg) {
#if USE(JSVALUE64)
			scratchGPR = AssemblyHelpers::selectScratchGPR(baseGPR, resultGPR);
#else
			scratchGPR = AssemblyHelpers::selectScratchGPR(baseGPR, resultGPR, resultTagGPR);
#endif
			stubJit.push(scratchGPR);
			needToRestoreScratch = true;
		}

		MacroAssembler::JumpList failureCases;

		stubJit.loadPtr(MacroAssembler::Address(baseGPR, JSCell::structureOffset()), scratchGPR);
		failureCases.append(stubJit.branch8(MacroAssembler::NotEqual, MacroAssembler::Address(scratchGPR, Structure::typeInfoTypeOffset()), MacroAssembler::TrustedImm32(StringType)));

        stubJit.load32(MacroAssembler::Address(baseGPR, JSString::offsetOfLength()), scratchGPR);

#if USE(JSVALUE64)
		stubJit.or64(GPRInfo::tagTypeNumberRegister, scratchGPR, resultGPR);
#elif USE(JSVALUE32_64)
		stubJit.move(scratchGPR, resultGPR);
		stubJit.move(AssemblyHelpers::TrustedImm32(0xffffffff), resultTagGPR); // JSValue::Int32Tag
#endif

		MacroAssembler::Jump success, fail;

		emitRestoreScratch(stubJit, needToRestoreScratch, scratchGPR, success, fail, failureCases);

        LinkBuffer patchBuffer(vm, &stubJit, codeBlock, JITCompilationCanFail, true);
        if (patchBuffer.didFailToAllocate())
            return GiveUpOnCache;

		linkRestoreScratch(patchBuffer, needToRestoreScratch, stubInfo, success, fail, failureCases);

        stubInfo.stubRoutine = FINALIZE_CODE_FOR_STUB(
            patchBuffer,
            ("GetById string length stub for %s, return point %p",
                toCString(*exec->codeBlock()).data(), stubInfo.callReturnLocation.labelAtOffset(
                    stubInfo.patch.deltaCallToDone).executableAddress()));

        replaceWithJump(stubInfo, stubInfo.stubRoutine->code().code());
        repatchCall(codeBlock, stubInfo.callReturnLocation, operationGetById);

        return RetryCacheLater;
    }

#if 0
    JSFunction* getter = 0;
    ObjectPropertyConditionSet conditionSet;

    if (slot.isCacheableGetter())
        getter = jsDynamicCast<JSFunction*>(slot.getterSetter()->getter());

    if (getter && AccessCase::canEmitIntrinsicGetter(getter, structure)) {
		if (slot.slotBase() != baseValue) {
			conditionSet = generateConditionsForPrototypePropertyHit(vm, codeBlock->ownerExecutable(), exec, structure, slot.slotBase(), propertyName.impl());
			if (!conditionSet.isValid())
				return GiveUpOnCache;
		}

		newCase = AccessCase::getIntrinsic(vm, codeBlock, getter, slot.cachedOffset(), structure, conditionSet);

	}
#endif

    if (!slot.isCacheable() && !slot.isUnset())
        return GiveUpOnCache;

    if(parseIndex(propertyName))
        return GiveUpOnCache;

    InlineCacheAction action = actionForCell(vm, baseCell);
    if (action != AttemptToCache)
        return action;

    // Optimize self access.
    if (slot.isCacheableValue()
        && slot.slotBase() == baseValue
        && !slot.watchpointSet()
    	&& MacroAssembler::isCompactPtrAlignedAddressOffset(maxOffsetRelativeToPatchedStorage(slot.cachedOffset()))) {
        structure->startWatchingPropertyForReplacements(vm, slot.cachedOffset());
        repatchByIdSelfAccess(vm, codeBlock, stubInfo, structure, propertyName, slot.cachedOffset(), operationGetByIdBuildList, true);
        stubInfo.initGetByIdSelf(vm, codeBlock->ownerExecutable(), structure);
        return RetryCacheLater;
    }

    if (slot.isUnset() && structure->typeInfo().getOwnPropertySlotIsImpureForPropertyAbsence())
        return GiveUpOnCache;
    
    repatchCall(codeBlock, stubInfo.callReturnLocation, operationGetByIdBuildList);
    return RetryCacheLater;
}

void repatchGetByID(ExecState* exec, JSValue baseValue, const Identifier& propertyName, const PropertySlot& slot, StructureStubInfo& stubInfo)
{
    // ConcurrentJITLocker locker(exec->codeBlock()->m_lock);

    if(tryCacheGetByID(exec, baseValue, propertyName, slot, stubInfo) == GiveUpOnCache)
        repatchCall(exec->codeBlock(), stubInfo.callReturnLocation, operationGetById);
}

static void patchJumpToGetByIdStub(StructureStubInfo& stubInfo, JITStubRoutine* stubRoutine)
{
    ASSERT(stubInfo.accessType == access_get_by_id_list);
    if (stubInfo.u.getByIdList.list->didSelfPatching()) {
        MacroAssembler::repatchJump(
            stubInfo.callReturnLocation.jumpAtOffset(
                stubInfo.patch.deltaCallToJump),
            CodeLocationLabel(stubRoutine->code().code()));
        return;
    }

    replaceWithJump(stubInfo, stubRoutine->code().code());
}

static InlineCacheAction tryBuildGetByIDList(ExecState* exec, JSValue baseValue, const Identifier& propertyName, const PropertySlot& slot, StructureStubInfo& stubInfo)
{
	if(ExecutableAllocator::underMemoryPressure() || WTF::gLowMemory)
        return GiveUpOnCache;

    if (!baseValue.isCell()
		|| (!slot.isCacheable() && !slot.isUnset()))
        return GiveUpOnCache;

    JSCell* baseCell = baseValue.asCell();
	bool loadTargetFromProxy = false;
	if (baseCell->structure()->typeInfo().type() == PureForwardingProxyType) {
		baseValue = jsCast<JSProxy*>(baseCell)->target();
		baseCell = baseValue.asCell();
		loadTargetFromProxy = true;
	}

    CodeBlock* codeBlock = exec->codeBlock();
    VM& vm = exec->vm();

    InlineCacheAction action = actionForCell(vm, baseCell);
    if (action != AttemptToCache)
        return action;

    if(parseIndex(propertyName))
        return GiveUpOnCache;

    Structure* structure = baseCell->structure();
    TypeInfo typeInfo = structure->typeInfo();

    if (stubInfo.patch.spillMode == NeedToSpill) {
		// We cannot do as much inline caching if the registers were not flushed prior to this GetById. In particular,
		// non-Value cached properties require planting calls, which requires registers to have been flushed. Thus,
		// if registers were not flushed, don't do non-Value caching.
		if (!slot.isCacheableValue() && !slot.isUnset())
			return GiveUpOnCache;
	}

    PropertyOffset offset = slot.isUnset() ? invalidOffset : slot.cachedOffset();

    ObjectPropertyConditionSet conditionSet;
    if (slot.isUnset() || slot.slotBase() != baseValue) {
		if (typeInfo.prohibitsPropertyCaching())
			return GiveUpOnCache;

		if (structure->isDictionary()) {
			if (structure->hasBeenFlattenedBefore())
				return GiveUpOnCache;
			structure->flattenDictionaryStructure(vm, jsCast<JSObject*>(baseCell));
		}

        if (slot.isUnset() && structure->typeInfo().getOwnPropertySlotIsImpureForPropertyAbsence())
            return GiveUpOnCache;

        if (slot.isUnset())
            conditionSet = generateConditionsForPropertyMiss(vm, codeBlock->ownerExecutable(), exec, structure, propertyName.impl());
        else
            conditionSet = generateConditionsForPrototypePropertyHit(vm, codeBlock->ownerExecutable(), exec, structure, slot.slotBase(), propertyName.impl());

        if (!conditionSet.isValid())
            return GiveUpOnCache;

        offset = slot.isUnset() ? invalidOffset : conditionSet.slotBaseCondition().offset();
    }

    PolymorphicGetByIdList* list = PolymorphicGetByIdList::from(stubInfo);
    if (list->isFull()) {
        // We need this extra check because of recursion.
        return GiveUpOnCache;
    }
    
    AccessCase::AccessType accessType;
    if (slot.isCacheableValue())
        accessType = slot.watchpointSet() ? AccessCase::WatchedStub : AccessCase::SimpleStub;
    else if (slot.isUnset())
		accessType = AccessCase::SimpleMiss;
    else if (slot.isCacheableGetter())
        accessType = AccessCase::Getter;
    else if (slot.attributes() & CustomAccessor)
    	accessType = AccessCase::CustomAccessorGetter;
    else
    	accessType = AccessCase::CustomValueGetter;

    RefPtr<JITStubRoutine> stubRoutine;
    bool result = generateByIdStub(
        exec, accessType, propertyName, customFor(slot), stubInfo, conditionSet, slot.slotBase(),
		offset, structure, loadTargetFromProxy, slot.watchpointSet(),
        stubInfo.callReturnLocation.labelAtOffset(stubInfo.patch.deltaCallToDone),
        CodeLocationLabel(list->currentSlowPathTarget(stubInfo)), stubRoutine);
    if (!result)
        return GiveUpOnCache;

	list->addAccess(GetByIdAccess(
		vm, codeBlock->ownerExecutable(), accessType, stubRoutine, structure,
        conditionSet));

	patchJumpToGetByIdStub(stubInfo, stubRoutine.get());

	return list->isFull() ? GiveUpOnCache : RetryCacheLater;
}

void buildGetByIDList(ExecState* exec, JSValue baseValue, const Identifier& propertyName, const PropertySlot& slot, StructureStubInfo& stubInfo)
{
    // ConcurrentJITLocker locker(exec->codeBlock()->m_lock);

    if(tryBuildGetByIDList(exec, baseValue, propertyName, slot, stubInfo) == GiveUpOnCache)
        repatchCall(exec->codeBlock(), stubInfo.callReturnLocation, operationGetById);
}

static V_JITOperation_ESsiJJI appropriateGenericPutByIdFunction(const PutPropertySlot &slot, PutKind putKind)
{
    if (slot.isStrictMode()) {
        if (putKind == Direct)
            return operationPutByIdDirectStrict;
        return operationPutByIdStrict;
    }
    if (putKind == Direct)
        return operationPutByIdDirectNonStrict;
    return operationPutByIdNonStrict;
}

static V_JITOperation_ESsiJJI appropriateListBuildingPutByIdFunction(const PutPropertySlot &slot, PutKind putKind)
{
    if (slot.isStrictMode()) {
        if (putKind == Direct)
            return operationPutByIdDirectStrictBuildList;
        return operationPutByIdStrictBuildList;
    }
    if (putKind == Direct)
        return operationPutByIdDirectNonStrictBuildList;
    return operationPutByIdNonStrictBuildList;
}

static bool emitPutReplaceStub(
    ExecState* exec,
    const Identifier&,
    const PutPropertySlot& slot,
    StructureStubInfo& stubInfo,
    Structure* structure,
    CodeLocationLabel failureLabel,
    RefPtr<JITStubRoutine>& stubRoutine)
{
    VM& vm = exec->vm();
    GPRReg baseGPR = static_cast<GPRReg>(stubInfo.patch.baseGPR);
#if USE(JSVALUE32_64)
    GPRReg valueTagGPR = static_cast<GPRReg>(stubInfo.patch.valueTagGPR);
#endif
    GPRReg valueGPR = static_cast<GPRReg>(stubInfo.patch.valueGPR);
    GPRReg scratchGPR = TempRegisterSet(stubInfo.patch.usedRegisters).getFreeGPR();
    bool needToRestoreScratch = false;
#if ENABLE(GGC) || ENABLE(WRITE_BARRIER_PROFILING)
    GPRReg scratchGPR2;
    const bool writeBarrierNeeded = true;
#else
    const bool writeBarrierNeeded = false;
#endif
    
    MacroAssembler stubJit;
    
    if (scratchGPR == InvalidGPRReg && (writeBarrierNeeded || isOutOfLineOffset(slot.cachedOffset()))) {
#if USE(JSVALUE64)
        scratchGPR = AssemblyHelpers::selectScratchGPR(baseGPR, valueGPR);
#else
        scratchGPR = AssemblyHelpers::selectScratchGPR(baseGPR, valueGPR, valueTagGPR);
#endif
        needToRestoreScratch = true;
        stubJit.pushToSave(scratchGPR);
    }

    MacroAssembler::Jump badStructure = stubJit.branchPtr(
        MacroAssembler::NotEqual,
        MacroAssembler::Address(baseGPR, JSCell::structureOffset()),
        MacroAssembler::TrustedImmPtr(structure));
    
#if ENABLE(GGC) || ENABLE(WRITE_BARRIER_PROFILING)
#if USE(JSVALUE64)
    scratchGPR2 = AssemblyHelpers::selectScratchGPR(baseGPR, valueGPR, scratchGPR);
#else
    scratchGPR2 = AssemblyHelpers::selectScratchGPR(baseGPR, valueGPR, valueTagGPR, scratchGPR);
#endif
    stubJit.pushToSave(scratchGPR2);
    AssemblyHelpers::writeBarrier(stubJit, baseGPR, scratchGPR, scratchGPR2, WriteBarrierForPropertyAccess);
    stubJit.popToRestore(scratchGPR2);
#endif
    
#if USE(JSVALUE64)
    if (isInlineOffset(slot.cachedOffset()))
        stubJit.store64(valueGPR, MacroAssembler::Address(baseGPR, JSObject::offsetOfInlineStorage() + offsetInInlineStorage(slot.cachedOffset()) * sizeof(JSValue)));
    else {
        stubJit.loadPtr(MacroAssembler::Address(baseGPR, JSObject::butterflyOffset()), scratchGPR);
        stubJit.store64(valueGPR, MacroAssembler::Address(scratchGPR, offsetInButterfly(slot.cachedOffset()) * sizeof(JSValue)));
    }
#elif USE(JSVALUE32_64)
    if (isInlineOffset(slot.cachedOffset())) {
        stubJit.store32(valueGPR, MacroAssembler::Address(baseGPR, JSObject::offsetOfInlineStorage() + offsetInInlineStorage(slot.cachedOffset()) * sizeof(JSValue) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.payload)));
        stubJit.store32(valueTagGPR, MacroAssembler::Address(baseGPR, JSObject::offsetOfInlineStorage() + offsetInInlineStorage(slot.cachedOffset()) * sizeof(JSValue) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.tag)));
    } else {
        stubJit.loadPtr(MacroAssembler::Address(baseGPR, JSObject::butterflyOffset()), scratchGPR);
        stubJit.store32(valueGPR, MacroAssembler::Address(scratchGPR, offsetInButterfly(slot.cachedOffset()) * sizeof(JSValue) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.payload)));
        stubJit.store32(valueTagGPR, MacroAssembler::Address(scratchGPR, offsetInButterfly(slot.cachedOffset()) * sizeof(JSValue) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.tag)));
    }
#endif
    
    MacroAssembler::Jump success;
    MacroAssembler::Jump failure;
    
    if (needToRestoreScratch) {
        stubJit.popToRestore(scratchGPR);
        success = stubJit.jump();
        
        badStructure.link(&stubJit);
        stubJit.popToRestore(scratchGPR);
        failure = stubJit.jump();
    } else {
        success = stubJit.jump();
        failure = badStructure;
    }
    
    LinkBuffer patchBuffer(vm, &stubJit, exec->codeBlock(), JITCompilationCanFail, true);
    if (patchBuffer.didFailToAllocate())
        return false;

    patchBuffer.link(success, stubInfo.callReturnLocation.labelAtOffset(stubInfo.patch.deltaCallToDone));
    patchBuffer.link(failure, failureLabel);
            
    stubRoutine = FINALIZE_CODE_FOR_DFG_STUB(
        patchBuffer,
        ("DFG PutById replace stub for CodeBlock %p, return point %p",
         exec->codeBlock(), stubInfo.callReturnLocation.labelAtOffset(
             stubInfo.patch.deltaCallToDone).executableAddress()));

    return true;
}

static bool emitPutTransitionStub(
	ExecState* exec, VM* vm, JSCell* baseCell, Structure*& structure, const Identifier& propertyName,
    const PutPropertySlot& slot, StructureStubInfo& stubInfo, PutKind putKind,
	CodeLocationLabel failureLabel, Structure*& oldStructure, ObjectPropertyConditionSet& conditionSet)
{
    if (!structure->isObject() || parseIndex(propertyName))
        return false;

    if (structure->isDictionary()) {
        if (structure->hasBeenFlattenedBefore())
            return false;
        structure->flattenDictionaryStructure(*vm, jsCast<JSObject*>(baseCell));
    }

    oldStructure = structure;
    PropertyOffset propertyOffset;
    structure = Structure::addPropertyTransitionToExistingStructure(oldStructure, propertyName.impl(), 0, propertyOffset);

    if (!structure || !structure->propertyAccessesAreCacheable())
        return false;

    // Skip optimizing the case where we need a realloc, if we don't have
    // enough registers to make it happen.
    if (GPRInfo::numberOfRegisters < 6
        && oldStructure->outOfLineCapacity() != structure->outOfLineCapacity()
        && oldStructure->outOfLineCapacity()) {
        return false;
    }

    // Skip optimizing the case where we need realloc, and the structure has
    // indexing storage.
    // FIXME: We shouldn't skip this! Implement it!
    // https://bugs.webkit.org/show_bug.cgi?id=130914
    if (oldStructure->couldHaveIndexingHeader())
        return false;

    if (putKind == NotDirect) {
        conditionSet = generateConditionsForPropertySetterMiss(
            *vm, exec->codeBlock()->ownerExecutable(), exec, structure, propertyName.impl());
        if (!conditionSet.isValid())
            return false;
    }

    RefPtr<JITStubRoutine>& stubRoutine = stubInfo.stubRoutine;

    GPRReg callFrameRegister = static_cast<GPRReg>(stubInfo.patch.callFrameRegister);
    GPRReg baseGPR = static_cast<GPRReg>(stubInfo.patch.baseGPR);
#if USE(JSVALUE32_64)
    GPRReg valueTagGPR = static_cast<GPRReg>(stubInfo.patch.valueTagGPR);
#endif
    GPRReg valueGPR = static_cast<GPRReg>(stubInfo.patch.valueGPR);
    
    ScratchRegisterAllocator allocator(stubInfo.patch.usedRegisters);
    allocator.lock(baseGPR);
#if USE(JSVALUE32_64)
    allocator.lock(valueTagGPR);
#endif
    allocator.lock(valueGPR);
    
    CCallHelpers stubJit(vm);
            
    GPRReg scratchGPR1 = allocator.allocateScratchGPR();
    ASSERT(scratchGPR1 != baseGPR);
    ASSERT(scratchGPR1 != valueGPR);
    
    bool needSecondScratch = false;
    bool needThirdScratch = false;
#if ENABLE(GGC) || ENABLE(WRITE_BARRIER_PROFILING)
    needSecondScratch = true;
#endif
    if (structure->outOfLineCapacity() != oldStructure->outOfLineCapacity()
        && oldStructure->outOfLineCapacity()) {
        needSecondScratch = true;
        needThirdScratch = true;
    }

    GPRReg scratchGPR2;
    if (needSecondScratch) {
        scratchGPR2 = allocator.allocateScratchGPR();
        ASSERT(scratchGPR2 != baseGPR);
        ASSERT(scratchGPR2 != valueGPR);
        ASSERT(scratchGPR2 != scratchGPR1);
    } else
        scratchGPR2 = InvalidGPRReg;
    GPRReg scratchGPR3;
    if (needThirdScratch) {
        scratchGPR3 = allocator.allocateScratchGPR();
        ASSERT(scratchGPR3 != baseGPR);
        ASSERT(scratchGPR3 != valueGPR);
        ASSERT(scratchGPR3 != scratchGPR1);
        ASSERT(scratchGPR3 != scratchGPR2);
    } else
        scratchGPR3 = InvalidGPRReg;
            
    allocator.preserveReusedRegistersByPushing(stubJit);

    MacroAssembler::JumpList failureCases;
            
    ASSERT(oldStructure->transitionWatchpointSetHasBeenInvalidated());
    
    failureCases.append(stubJit.branchPtr(MacroAssembler::NotEqual, MacroAssembler::Address(baseGPR, JSCell::structureOffset()), MacroAssembler::TrustedImmPtr(oldStructure)));

    checkObjectPropertyConditions(
        conditionSet, exec->codeBlock(), stubInfo, stubJit, failureCases, scratchGPR1);

#if ENABLE(GGC) || ENABLE(WRITE_BARRIER_PROFILING)
    ASSERT(needSecondScratch);
    ASSERT(scratchGPR2 != InvalidGPRReg);
    // Must always emit this write barrier as the structure transition itself requires it
    AssemblyHelpers::writeBarrier(stubJit, baseGPR, scratchGPR1, scratchGPR2, WriteBarrierForPropertyAccess);
#endif
    
    MacroAssembler::JumpList slowPath;
    
    bool scratchGPR1HasStorage = false;
    
    if (structure->outOfLineCapacity() != oldStructure->outOfLineCapacity()) {
        size_t newSize = structure->outOfLineCapacity() * sizeof(JSValue);
        CopiedAllocator* copiedAllocator = &vm->heap.storageAllocator();
        
        if (!oldStructure->outOfLineCapacity()) {
            stubJit.loadPtr(&copiedAllocator->m_currentRemaining, scratchGPR1);
            slowPath.append(stubJit.branchSubPtr(MacroAssembler::Signed, MacroAssembler::TrustedImm32(newSize), scratchGPR1));
            stubJit.storePtr(scratchGPR1, &copiedAllocator->m_currentRemaining);
            stubJit.negPtr(scratchGPR1);
            stubJit.addPtr(MacroAssembler::AbsoluteAddress(&copiedAllocator->m_currentPayloadEnd), scratchGPR1);
            stubJit.addPtr(MacroAssembler::TrustedImm32(sizeof(JSValue)), scratchGPR1);
        } else {
            size_t oldSize = oldStructure->outOfLineCapacity() * sizeof(JSValue);
            ASSERT(newSize > oldSize);
            
            stubJit.loadPtr(MacroAssembler::Address(baseGPR, JSObject::butterflyOffset()), scratchGPR3);
            stubJit.loadPtr(&copiedAllocator->m_currentRemaining, scratchGPR1);
            slowPath.append(stubJit.branchSubPtr(MacroAssembler::Signed, MacroAssembler::TrustedImm32(newSize), scratchGPR1));
            stubJit.storePtr(scratchGPR1, &copiedAllocator->m_currentRemaining);
            stubJit.negPtr(scratchGPR1);
            stubJit.addPtr(MacroAssembler::AbsoluteAddress(&copiedAllocator->m_currentPayloadEnd), scratchGPR1);
            stubJit.addPtr(MacroAssembler::TrustedImm32(sizeof(JSValue)), scratchGPR1);
            // We have scratchGPR1 = new storage, scratchGPR3 = old storage, scratchGPR2 = available
            for (ptrdiff_t offset = 0; offset < static_cast<ptrdiff_t>(oldSize); offset += sizeof(void*)) {
                stubJit.loadPtr(MacroAssembler::Address(scratchGPR3, -(offset + sizeof(JSValue) + sizeof(void*))), scratchGPR2);
                stubJit.storePtr(scratchGPR2, MacroAssembler::Address(scratchGPR1, -(offset + sizeof(JSValue) + sizeof(void*))));
            }
        }
        
        stubJit.storePtr(scratchGPR1, MacroAssembler::Address(baseGPR, JSObject::butterflyOffset()));
        scratchGPR1HasStorage = true;
    }

    stubJit.storePtr(MacroAssembler::TrustedImmPtr(structure), MacroAssembler::Address(baseGPR, JSCell::structureOffset()));
#if USE(JSVALUE64)
    if (isInlineOffset(slot.cachedOffset()))
        stubJit.store64(valueGPR, MacroAssembler::Address(baseGPR, JSObject::offsetOfInlineStorage() + offsetInInlineStorage(slot.cachedOffset()) * sizeof(JSValue)));
    else {
        if (!scratchGPR1HasStorage)
            stubJit.loadPtr(MacroAssembler::Address(baseGPR, JSObject::butterflyOffset()), scratchGPR1);
        stubJit.store64(valueGPR, MacroAssembler::Address(scratchGPR1, offsetInButterfly(slot.cachedOffset()) * sizeof(JSValue)));
    }
#elif USE(JSVALUE32_64)
    if (isInlineOffset(slot.cachedOffset())) {
        stubJit.store32(valueGPR, MacroAssembler::Address(baseGPR, JSObject::offsetOfInlineStorage() + offsetInInlineStorage(slot.cachedOffset()) * sizeof(JSValue) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.payload)));
        stubJit.store32(valueTagGPR, MacroAssembler::Address(baseGPR, JSObject::offsetOfInlineStorage() + offsetInInlineStorage(slot.cachedOffset()) * sizeof(JSValue) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.tag)));
    } else {
        if (!scratchGPR1HasStorage)
            stubJit.loadPtr(MacroAssembler::Address(baseGPR, JSObject::butterflyOffset()), scratchGPR1);
        stubJit.store32(valueGPR, MacroAssembler::Address(scratchGPR1, offsetInButterfly(slot.cachedOffset()) * sizeof(JSValue) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.payload)));
        stubJit.store32(valueTagGPR, MacroAssembler::Address(scratchGPR1, offsetInButterfly(slot.cachedOffset()) * sizeof(JSValue) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.tag)));
    }
#endif
            
    MacroAssembler::Jump success;
    MacroAssembler::Jump failure;
            
    if (allocator.didReuseRegisters()) {
        allocator.restoreReusedRegistersByPopping(stubJit);
        success = stubJit.jump();

        failureCases.link(&stubJit);
        allocator.restoreReusedRegistersByPopping(stubJit);
        failure = stubJit.jump();
    } else
        success = stubJit.jump();
    
    MacroAssembler::Call operationCall;
    MacroAssembler::Jump successInSlowPath;
    
    if (structure->outOfLineCapacity() != oldStructure->outOfLineCapacity()) {
        slowPath.link(&stubJit);
        
        allocator.restoreReusedRegistersByPopping(stubJit);
        ScratchBuffer* scratchBuffer = vm->scratchBufferForSize(allocator.desiredScratchBufferSizeForCall());
        allocator.preserveUsedRegistersToScratchBufferForCall(stubJit, scratchBuffer, scratchGPR1);
#if USE(JSVALUE64)
        stubJit.setupArguments(callFrameRegister, baseGPR, MacroAssembler::TrustedImmPtr(structure), MacroAssembler::TrustedImm32(slot.cachedOffset()), valueGPR);
#else
        stubJit.setupArguments(callFrameRegister, baseGPR, MacroAssembler::TrustedImmPtr(structure), MacroAssembler::TrustedImm32(slot.cachedOffset()), valueGPR, valueTagGPR);
#endif
        operationCall = stubJit.call();
        allocator.restoreUsedRegistersFromScratchBufferForCall(stubJit, scratchBuffer, scratchGPR1);
        successInSlowPath = stubJit.jump();
    }
    
    LinkBuffer patchBuffer(*vm, &stubJit, exec->codeBlock(), JITCompilationCanFail, true);
    if (patchBuffer.didFailToAllocate())
        return false;

    patchBuffer.link(success, stubInfo.callReturnLocation.labelAtOffset(stubInfo.patch.deltaCallToDone));
    if (allocator.didReuseRegisters())
        patchBuffer.link(failure, failureLabel);
    else
        patchBuffer.link(failureCases, failureLabel);
    if (structure->outOfLineCapacity() != oldStructure->outOfLineCapacity()) {
        patchBuffer.link(operationCall, operationReallocateStorageAndFinishPut);
        patchBuffer.link(successInSlowPath, stubInfo.callReturnLocation.labelAtOffset(stubInfo.patch.deltaCallToDone));
    }
    
    stubRoutine =
        createJITStubRoutine(
        	FINALIZE_DFG_CODE(
                patchBuffer,
                ("DFG PutById %stransition stub (%p -> %p) for CodeBlock %p, return point %p",
                 structure->outOfLineCapacity() != oldStructure->outOfLineCapacity() ? "reallocating " : "",
                 oldStructure, structure,
                 exec->codeBlock(), stubInfo.callReturnLocation.labelAtOffset(
                     stubInfo.patch.deltaCallToDone).executableAddress())),
            *vm,
            exec->codeBlock()->ownerExecutable(),
            structure->outOfLineCapacity() != oldStructure->outOfLineCapacity(),
            structure);

    return true;
}

static InlineCacheAction tryCachePutByID(ExecState* exec, JSValue baseValue, Structure* structure, const Identifier& propertyName, const PutPropertySlot& slot, StructureStubInfo& stubInfo, PutKind putKind)
{
	if(ExecutableAllocator::underMemoryPressure() || WTF::gLowMemory)
        return GiveUpOnCache;

    CodeBlock* codeBlock = exec->codeBlock();
    VM& vm = exec->vm();

    if(Options::forceICFailure())
    	return GiveUpOnCache;

    if (!baseValue.isCell())
        return GiveUpOnCache;

    if (!slot.isCacheablePut() && !slot.isCacheableCustom())
        return GiveUpOnCache;

    if (!structure->propertyAccessesAreCacheable())
        return GiveUpOnCache;

    if(parseIndex(propertyName))
        return GiveUpOnCache;

    // Optimize self access.
    if (slot.base() == baseValue && slot.isCacheablePut()) {
        if (slot.type() == PutPropertySlot::NewProperty) {
            Structure* oldStructure;
            ObjectPropertyConditionSet conditionSet;
            if (!emitPutTransitionStub(exec, &vm, baseValue.asCell(), structure, propertyName, slot, stubInfo, putKind,
				stubInfo.callReturnLocation.labelAtOffset(stubInfo.patch.deltaCallToSlowCase), oldStructure, conditionSet))
                return GiveUpOnCache;

            MacroAssembler::repatchJump(
                stubInfo.callReturnLocation.jumpAtOffset(
                    stubInfo.patch.deltaCallToJump),
                CodeLocationLabel(stubInfo.stubRoutine->code().code()));
            repatchCall(codeBlock, stubInfo.callReturnLocation, appropriateListBuildingPutByIdFunction(slot, putKind));
            
            stubInfo.initPutByIdTransition(vm, codeBlock->ownerExecutable(), oldStructure, structure, conditionSet, putKind == Direct);
            
            return RetryCacheLater;
        }

        if (!MacroAssembler::isPtrAlignedAddressOffset(offsetRelativeToPatchedStorage(slot.cachedOffset())))
            return GiveUpOnCache;

        structure->didCachePropertyReplacement(vm, slot.cachedOffset());
        repatchByIdSelfAccess(vm, codeBlock, stubInfo, structure, propertyName, slot.cachedOffset(), appropriateListBuildingPutByIdFunction(slot, putKind), false);
        stubInfo.initPutByIdReplace(vm, codeBlock->ownerExecutable(), structure);
        return RetryCacheLater;
    }
    if (slot.isCacheableCustom() && stubInfo.patch.spillMode == DontSpill) {
        RefPtr<JITStubRoutine> stubRoutine;

        ObjectPropertyConditionSet conditionSet;
        PropertyOffset offset;
        if (slot.base() != baseValue) {
            if (slot.isCacheableCustom()) {
                conditionSet =
					generateConditionsForPrototypePropertyHit(
                        vm, codeBlock->ownerExecutable(), exec, structure, slot.base(),
						propertyName.impl());
            } else {
                conditionSet =
                    generateConditionsForPrototypePropertyHit(
                        vm, codeBlock->ownerExecutable(), exec, structure, slot.base(),
						propertyName.impl());
            }
            if (!conditionSet.isValid())
                return GiveUpOnCache;
            offset = slot.isCacheableCustom() ? invalidOffset : conditionSet.slotBaseCondition().offset();
        } else
            offset = slot.cachedOffset();

        PolymorphicPutByIdList* list;
        list = PolymorphicPutByIdList::from(putKind, stubInfo);

        AccessCase::AccessType accessType;
        if(slot.isCacheableSetter())
        	accessType = AccessCase::Setter;
        else if(slot.isCustomAccessor())
        	accessType = AccessCase::CustomAccessorSetter;
        else
        	accessType = AccessCase::CustomValueSetter;

        bool result = generateByIdStub(
            exec, accessType, propertyName, customFor(slot), stubInfo, conditionSet, slot.base(),
            offset, structure, false, 0,
            stubInfo.callReturnLocation.labelAtOffset(stubInfo.patch.deltaCallToDone),
            stubInfo.callReturnLocation.labelAtOffset(stubInfo.patch.deltaCallToSlowCase),
            stubRoutine);
        if (!result)
            return GiveUpOnCache;

        list->addAccess(PutByIdAccess::setter(
			vm, codeBlock->ownerExecutable(),
			accessType,
			structure, conditionSet, slot.customSetter(), stubRoutine));

        MacroAssembler::repatchJump(stubInfo.callReturnLocation.jumpAtOffset(stubInfo.patch.deltaCallToJump), CodeLocationLabel(stubRoutine->code().code()));
        repatchCall(codeBlock, stubInfo.callReturnLocation, appropriateListBuildingPutByIdFunction(slot, putKind));
        RELEASE_ASSERT(!list->isFull());
        return RetryCacheLater;
    }

    return GiveUpOnCache;
}

void repatchPutByID(ExecState* exec, JSValue baseValue, Structure* structure, const Identifier& propertyName, const PutPropertySlot& slot, StructureStubInfo& stubInfo, PutKind putKind)
{
    if(tryCachePutByID(exec, baseValue, structure, propertyName, slot, stubInfo, putKind) == GiveUpOnCache)
        repatchCall(exec->codeBlock(), stubInfo.callReturnLocation, appropriateGenericPutByIdFunction(slot, putKind));
}

static InlineCacheAction tryBuildPutByIdList(ExecState* exec, JSValue baseValue, Structure* structure, const Identifier& propertyName, const PutPropertySlot& slot, StructureStubInfo& stubInfo, PutKind putKind)
{
	if(ExecutableAllocator::underMemoryPressure() || WTF::gLowMemory)
        return GiveUpOnCache;

    CodeBlock* codeBlock = exec->codeBlock();
    VM* vm = &exec->vm();

    if (!baseValue.isCell())
        return GiveUpOnCache;

    if (!slot.isCacheablePut() && !slot.isCacheableCustom())
       return GiveUpOnCache;

    if (!structure->propertyAccessesAreCacheable())
        return GiveUpOnCache;

    if(parseIndex(propertyName))
        return GiveUpOnCache;

    // Optimize self access.
    if (slot.base() == baseValue && slot.isCacheablePut()) {
        PolymorphicPutByIdList* list;
        RefPtr<JITStubRoutine> stubRoutine;
        
        if (slot.type() == PutPropertySlot::NewProperty) {
            list = PolymorphicPutByIdList::from(putKind, stubInfo);
            if (list->isFull())
				return GiveUpOnCache; // Will get here due to recursion.

            Structure* oldStructure;
            ObjectPropertyConditionSet conditionSet;
            if (!emitPutTransitionStub(exec, vm, baseValue.asCell(), structure, propertyName, slot, stubInfo, putKind,
				stubInfo.callReturnLocation.labelAtOffset(stubInfo.patch.deltaCallToSlowCase), oldStructure, conditionSet))
                return GiveUpOnCache;
            
            stubRoutine = stubInfo.stubRoutine;
            
            list->addAccess(
                PutByIdAccess::transition(
                    *vm, codeBlock->ownerExecutable(),
                    oldStructure, structure, conditionSet,
                    stubRoutine));
        } else {
            list = PolymorphicPutByIdList::from(putKind, stubInfo);
            
            if (list->isFull())
				return GiveUpOnCache; // Will get here due to recursion.

            structure->didCachePropertyReplacement(*vm, slot.cachedOffset());

            bool result = emitPutReplaceStub(
                exec, propertyName, slot, stubInfo,
                structure, CodeLocationLabel(list->currentSlowPathTarget()), stubRoutine);
            if(!result)
            	return GiveUpOnCache;
            
            list->addAccess(
                PutByIdAccess::replace(
                    *vm, codeBlock->ownerExecutable(),
                    structure, stubRoutine));
        }
        
        MacroAssembler::repatchJump(stubInfo.callReturnLocation.jumpAtOffset(stubInfo.patch.deltaCallToJump), CodeLocationLabel(stubRoutine->code().code()));
        if (list->isFull())
        	repatchCall(codeBlock, stubInfo.callReturnLocation, appropriateGenericPutByIdFunction(slot, putKind));
        
        return RetryCacheLater;
    }

    if (slot.isCacheableCustom() && stubInfo.patch.spillMode == DontSpill) {
        RefPtr<JITStubRoutine> stubRoutine;

        ObjectPropertyConditionSet conditionSet;
        PropertyOffset offset;
        if (slot.base() != baseValue) {
            if (slot.isCacheableCustom()) {
                conditionSet =
					generateConditionsForPrototypePropertyHit(
                        *vm, codeBlock->ownerExecutable(), exec, structure, slot.base(),
                        propertyName.impl());
            } else {
                conditionSet =
                    generateConditionsForPrototypePropertyHit(
                        *vm, codeBlock->ownerExecutable(), exec, structure, slot.base(),
                        propertyName.impl());
            }
            if (!conditionSet.isValid())
                return GiveUpOnCache;
            offset = slot.isCacheableCustom() ? invalidOffset : conditionSet.slotBaseCondition().offset();
        } else
            offset = slot.cachedOffset();

        PolymorphicPutByIdList* list;
        list = PolymorphicPutByIdList::from(putKind, stubInfo);

        AccessCase::AccessType accessType;
        if(slot.isCacheableSetter())
        	accessType = AccessCase::Setter;
        else if(slot.isCustomAccessor())
        	accessType = AccessCase::CustomAccessorSetter;
        else
        	accessType = AccessCase::CustomValueSetter;

        bool result = generateByIdStub(
            exec, accessType, propertyName, customFor(slot), stubInfo, conditionSet, slot.base(),
            offset, structure, false, 0,
            stubInfo.callReturnLocation.labelAtOffset(stubInfo.patch.deltaCallToDone),
            CodeLocationLabel(list->currentSlowPathTarget()),
            stubRoutine);
        if (!result)
            return GiveUpOnCache;

        list->addAccess(PutByIdAccess::setter(
			*vm, codeBlock->ownerExecutable(),
			accessType,
			structure, conditionSet, slot.customSetter(), stubRoutine));

        MacroAssembler::repatchJump(stubInfo.callReturnLocation.jumpAtOffset(stubInfo.patch.deltaCallToJump), CodeLocationLabel(stubRoutine->code().code()));
        if (list->isFull())
            repatchCall(codeBlock, stubInfo.callReturnLocation, appropriateGenericPutByIdFunction(slot, putKind));

        return RetryCacheLater;
    }
    return GiveUpOnCache;
}

void buildPutByIdList(ExecState* exec, JSValue baseValue, Structure* structure, const Identifier& propertyName, const PutPropertySlot& slot, StructureStubInfo& stubInfo, PutKind putKind)
{
    if(tryBuildPutByIdList(exec, baseValue, structure, propertyName, slot, stubInfo, putKind) == GiveUpOnCache)
        repatchCall(exec->codeBlock(), stubInfo.callReturnLocation, appropriateGenericPutByIdFunction(slot, putKind));
}

static InlineCacheAction tryRepatchIn(
    ExecState* exec, JSCell* base, const Identifier& propertyName, bool wasFound,
    const PropertySlot& slot, StructureStubInfo& stubInfo)
{
	if(ExecutableAllocator::underMemoryPressure() || WTF::gLowMemory)
        return GiveUpOnCache;

    Structure* structure = base->structure();

    if (!structure->propertyAccessesAreCacheable()|| (!wasFound && !base->structure()->propertyAccessesAreCacheableForAbsence()))
        return GiveUpOnCache;

    if (wasFound) {
        if (!slot.isCacheable())
            return GiveUpOnCache;
    }

    CodeBlock* codeBlock = exec->codeBlock();
    VM& vm = exec->vm();

    TypeInfo typeInfo = structure->typeInfo();
    if (typeInfo.getOwnPropertySlotIsImpure() && !typeInfo.newImpurePropertyFiresWatchpoints())
        return GiveUpOnCache;

    if(parseIndex(propertyName))
        return GiveUpOnCache;

    ObjectPropertyConditionSet conditionSet;
    if (wasFound) {
        if (slot.slotBase() != base) {
            conditionSet = generateConditionsForPrototypePropertyHit(
                vm, codeBlock->ownerExecutable(), exec, structure, slot.slotBase(), propertyName.impl());
        }
    } else {
        conditionSet = generateConditionsForPropertyMiss(
            vm, codeBlock->ownerExecutable(), exec, structure, propertyName.impl());
    }
    if (!conditionSet.isValid())
        return GiveUpOnCache;

    PolymorphicAccessStructureList* polymorphicStructureList;
    int listIndex;

    CodeLocationLabel successLabel = stubInfo.callReturnLocation.labelAtOffset(stubInfo.patch.deltaCallToDone);
    CodeLocationLabel slowCaseLabel;

    if (stubInfo.accessType == access_unset) {
        polymorphicStructureList = new PolymorphicAccessStructureList();
        stubInfo.initInList(polymorphicStructureList, 0);
        slowCaseLabel = stubInfo.callReturnLocation.labelAtOffset(
            stubInfo.patch.deltaCallToSlowCase);
        listIndex = 0;
    } else {
        RELEASE_ASSERT(stubInfo.accessType == access_in_list);
        polymorphicStructureList = stubInfo.u.inList.structureList;
        listIndex = stubInfo.u.inList.listSize;
        slowCaseLabel = CodeLocationLabel(polymorphicStructureList->list[listIndex - 1].stubRoutine->code().code());

        if (listIndex == POLYMORPHIC_LIST_CACHE_SIZE)
            return GiveUpOnCache;
    }

    RefPtr<JITStubRoutine> stubRoutine;

    {
        GPRReg baseGPR = static_cast<GPRReg>(stubInfo.patch.baseGPR);
        GPRReg resultGPR = static_cast<GPRReg>(stubInfo.patch.valueGPR);
        GPRReg scratchGPR = TempRegisterSet(stubInfo.patch.usedRegisters).getFreeGPR();

        CCallHelpers stubJit(&vm);

        bool needToRestoreScratch;
        if (scratchGPR == InvalidGPRReg) {
            scratchGPR = AssemblyHelpers::selectScratchGPR(baseGPR, resultGPR);
            stubJit.pushToSave(scratchGPR);
            needToRestoreScratch = true;
        } else
            needToRestoreScratch = false;

        MacroAssembler::JumpList failureCases;
        failureCases.append(stubJit.branchPtr(
            MacroAssembler::NotEqual,
            MacroAssembler::Address(baseGPR, JSCell::structureOffset()),
            MacroAssembler::TrustedImmPtr(structure)));

        if (structure->typeInfo().newImpurePropertyFiresWatchpoints())
            vm.registerWatchpointForImpureProperty(propertyName, stubInfo.addWatchpoint(codeBlock));

        if (slot.watchpointSet())
            slot.watchpointSet()->add(stubInfo.addWatchpoint(codeBlock));

        checkObjectPropertyConditions(
            conditionSet, exec->codeBlock(), stubInfo, stubJit, failureCases, scratchGPR);

#if USE(JSVALUE64)
        stubJit.move(MacroAssembler::TrustedImm64(JSValue::encode(jsBoolean(wasFound))), resultGPR);
#else
        stubJit.move(MacroAssembler::TrustedImm32(wasFound), resultGPR);
#endif

        MacroAssembler::Jump success, fail;

        emitRestoreScratch(stubJit, needToRestoreScratch, scratchGPR, success, fail, failureCases);

        LinkBuffer patchBuffer(vm, &stubJit, exec->codeBlock(), JITCompilationCanFail, true);
        if (patchBuffer.didFailToAllocate())
            return GiveUpOnCache;

        linkRestoreScratch(patchBuffer, needToRestoreScratch, success, fail, failureCases, successLabel, slowCaseLabel);

        stubRoutine = FINALIZE_CODE_FOR_DFG_STUB(
            patchBuffer,
            ("DFG In (found = %s) stub for %s, return point %p",
                wasFound ? "yes" : "no", toCString(*exec->codeBlock()).data(),
                successLabel.executableAddress()));
    }

    polymorphicStructureList->list[listIndex].set(vm, codeBlock->ownerExecutable(), stubRoutine, structure, true);
    stubInfo.u.inList.listSize++;

    MacroAssembler::repatchJump(stubInfo.callReturnLocation.jumpAtOffset(stubInfo.patch.deltaCallToJump), CodeLocationLabel(stubRoutine->code().code()));

    return listIndex < (POLYMORPHIC_LIST_CACHE_SIZE - 1) ? RetryCacheLater : GiveUpOnCache;
}

void repatchIn(
    ExecState* exec, JSCell* base, const Identifier& propertyName, bool wasFound,
    const PropertySlot& slot, StructureStubInfo& stubInfo)
{
    if (tryRepatchIn(exec, base, propertyName, wasFound, slot, stubInfo) == GiveUpOnCache)
    	repatchCall(exec->codeBlock(), stubInfo.callReturnLocation, operationIn);
}

static void linkSlowFor(VM*, CallLinkInfo& callLinkInfo, MacroAssemblerCodeRef codeRef)
{
    MacroAssembler::repatchNearCall(callLinkInfo.callReturnLocation(), CodeLocationLabel(codeRef.code()));
}

static void linkSlowFor(VM* vm, CallLinkInfo& callLinkInfo, ThunkGenerator generator)
{
    linkSlowFor(vm, callLinkInfo, vm->getCTIStub(generator));
}

static void linkSlowFor(VM* vm, CallLinkInfo& callLinkInfo)
{
    MacroAssemblerCodeRef virtualThunk = virtualThunkFor(vm, callLinkInfo);
    linkSlowFor(vm, callLinkInfo, virtualThunk);
    callLinkInfo.setSlowStub(createJITStubRoutine(virtualThunk, *vm, 0, true));
}

void linkFor(
    ExecState* exec, CallLinkInfo& callLinkInfo, CodeBlock* calleeCodeBlock,
    JSFunction* callee, MacroAssemblerCodePtr codePtr)
{
    ASSERT(!callLinkInfo.stub());

    CodeBlock* callerCodeBlock = exec->callerFrame()->codeBlock();

    VM* vm = callerCodeBlock->vm();

    ASSERT(!callLinkInfo.isLinked());
    callLinkInfo.setCallee(exec->callerFrame()->vm(), callLinkInfo.hotPathBegin(), callerCodeBlock->ownerExecutable(), callee);
    callLinkInfo.setLastSeenCallee(exec->callerFrame()->vm(), callerCodeBlock->ownerExecutable(), callee);
    MacroAssembler::repatchNearCall(callLinkInfo.hotPathOther(), CodeLocationLabel(codePtr));

    if (calleeCodeBlock)
        calleeCodeBlock->linkIncomingCall(exec->callerFrame(), &callLinkInfo);
    
    if (callLinkInfo.specializationKind() == CodeForCall && callLinkInfo.allowStubs()) {
        linkSlowFor(vm, callLinkInfo, linkPolymorphicCallThunkGenerator);
        return;
    }

    ASSERT(callLinkInfo.specializationKind() == CodeForConstruct);
    linkSlowFor(vm, callLinkInfo);
}

void linkSlowFor(
    ExecState* exec, CallLinkInfo& callLinkInfo)
{
	CodeBlock* callerCodeBlock = exec->callerFrame()->codeBlock();
	VM* vm = callerCodeBlock->vm();

	linkSlowFor(vm, callLinkInfo);
}

static void revertCall(VM* vm, CallLinkInfo& callLinkInfo, MacroAssemblerCodeRef codeRef)
{
    MacroAssembler::revertJumpReplacementToBranchPtrWithPatch(
        MacroAssembler::startOfBranchPtrWithPatchOnRegister(callLinkInfo.hotPathBegin()),
        static_cast<MacroAssembler::RegisterID>(callLinkInfo.calleeGPR()), 0);
    linkSlowFor(vm, callLinkInfo, codeRef);
    callLinkInfo.clearSeen();
    callLinkInfo.clearCallee();
    callLinkInfo.clearStub();
    callLinkInfo.clearSlowStub();
    if (callLinkInfo.isOnList())
        callLinkInfo.remove();
}

void unlinkFor(VM& vm, CallLinkInfo& callLinkInfo)
{
    //if (Options::showDisassembly())
    //    dataLog("Unlinking call from ", callLinkInfo.callReturnLocation, " in request from ", pointerDump(repatchBuffer.codeBlock()), "\n");

    revertCall(&vm, callLinkInfo, vm.getCTIStub(linkCallThunkGenerator));
}

void linkVirtualFor(
    ExecState* exec, CallLinkInfo& callLinkInfo)
{
    CodeBlock* callerCodeBlock = exec->callerFrame()->codeBlock();
    VM* vm = callerCodeBlock->vm();

    //if (shouldShowDisassemblyFor(callerCodeBlock))
    //    dataLog("Linking virtual call at ", *callerCodeBlock, " ", exec->callerFrame()->codeOrigin(), "\n");

    MacroAssemblerCodeRef virtualThunk = virtualThunkFor(vm, callLinkInfo);
    revertCall(vm, callLinkInfo, virtualThunk);
    callLinkInfo.setSlowStub(createJITStubRoutine(virtualThunk, *vm, 0, true));
}

namespace {
struct CallToCodePtr {
    CCallHelpers::Call call;
    MacroAssemblerCodePtr codePtr;
};
} // annonymous namespace

void linkPolymorphicCall(
    ExecState* exec, CallLinkInfo& callLinkInfo, CallVariant newVariant)
{
    // Currently we can't do anything for non-function callees.
    // https://bugs.webkit.org/show_bug.cgi?id=140685
    if (!newVariant || !newVariant.executable()) {
        linkVirtualFor(exec, callLinkInfo);
        return;
    }

    RELEASE_ASSERT(callLinkInfo.allowStubs());

	ASSERT(!callLinkInfo.stub());

	CodeBlock* callerCodeBlock = exec->callerFrame()->codeBlock();
	VM* vm = callerCodeBlock->vm();

    CallVariantList list;
    if (PolymorphicCallStubRoutine* stub = callLinkInfo.stub())
        list = stub->variants();
    else if (JSFunction* oldCallee = callLinkInfo.callee())
        list.append(CallVariant(oldCallee));

    list = variantListWithVariant(list, newVariant);

    // If there are any closure calls then it makes sense to treat all of them as closure calls.
    // This makes switching on callee cheaper. It also produces profiling that's easier on the DFG;
    // the DFG doesn't really want to deal with a combination of closure and non-closure callees.
    bool isClosureCall = false;
    for(auto it = list.begin(); it != list.end(); ++it) {
    	CallVariant variant = *it;
        if (variant.isClosureCall()) {
            list = despecifiedVariantList(list);
            isClosureCall = true;
            break;
        }
    }

    if (isClosureCall)
        callLinkInfo.setHasSeenClosure();

    Vector<PolymorphicCallCase> callCases;

    // Figure out what our cases are.
    for(auto it = list.begin(); it != list.end(); ++it) {
    	CallVariant variant = *it;
        CodeBlock* codeBlock;
        if (variant.executable()->isHostFunction())
            codeBlock = 0;
        else {
            codeBlock = jsCast<FunctionExecutable*>(variant.executable())->generatedBytecodeFor(CodeForCall);

            // If we cannot handle a callee, either because we don't have a CodeBlock or because arity mismatch,
			// assume that it's better for this whole thing to be a virtual call.
			if (!codeBlock || exec->argumentCountIncludingThis() < static_cast<size_t>(codeBlock->numParameters()) || callLinkInfo.callType() == CallLinkInfo::CallVarargs || callLinkInfo.callType() == CallLinkInfo::ConstructVarargs) {
                linkVirtualFor(exec, callLinkInfo);
                return;
            }
        }

        callCases.append(PolymorphicCallCase(variant, codeBlock));
    }

    // If we are over the limit, just use a normal virtual call.
    unsigned maxPolymorphicCallVariantListSize;
    if (callerCodeBlock->jitType() == JITCode::topTierJIT())
        maxPolymorphicCallVariantListSize = Options::maxPolymorphicCallVariantListSizeForTopTier();
    else
        maxPolymorphicCallVariantListSize = Options::maxPolymorphicCallVariantListSize();
    if (list.size() > maxPolymorphicCallVariantListSize) {
        linkVirtualFor(exec, callLinkInfo);
        return;
    }

	GPRReg calleeGPR = static_cast<GPRReg>(callLinkInfo.calleeGPR());

	CCallHelpers stubJit(vm, callerCodeBlock);

	CCallHelpers::JumpList slowPath;

    GPRReg scratch = AssemblyHelpers::selectScratchGPR(calleeGPR);
    GPRReg comparisonValueGPR;

    if (isClosureCall) {
        // Verify that we have a function and stash the executable in scratch.

#if USE(JSVALUE64)
		// We can safely clobber everything except the calleeGPR. We can't rely on tagMaskRegister
		// being set. So we do this the hard way.
		stubJit.move(MacroAssembler::TrustedImm64(TagMask), scratch);
		slowPath.append(stubJit.branchTest64(CCallHelpers::NonZero, calleeGPR, scratch));
#else
		// We would have already checked that the callee is a cell.
#endif

		stubJit.loadPtr(MacroAssembler::Address(calleeGPR, JSCell::structureOffset()), scratch);
		slowPath.append(
			stubJit.branch8(
				CCallHelpers::NotEqual,
				CCallHelpers::Address(scratch, Structure::typeInfoTypeOffset()),
				CCallHelpers::TrustedImm32(JSFunctionType)));

		stubJit.loadPtr(
			CCallHelpers::Address(calleeGPR, JSFunction::offsetOfExecutable()),
			scratch);

		comparisonValueGPR = scratch;
	} else
		comparisonValueGPR = calleeGPR;

#if USE(JSVALUE64)
	stubJit.store64(
		AssemblyHelpers::TrustedImm64(ValueUndefined),
		CCallHelpers::Address(GPRInfo::callFrameRegister, static_cast<ptrdiff_t>(sizeof(Register) * JSStack::ScopeChain)));
#else
	stubJit.storePtr(
		AssemblyHelpers::TrustedImmPtr(0),
		CCallHelpers::Address(GPRInfo::callFrameRegister, static_cast<ptrdiff_t>(sizeof(Register) * JSStack::ScopeChain) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.payload)));
	stubJit.store32(
		CCallHelpers::TrustedImm32(JSValue::UndefinedTag),
		CCallHelpers::Address(GPRInfo::callFrameRegister, static_cast<ptrdiff_t>(sizeof(Register) * JSStack::ScopeChain) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.tag)));
#endif

    Vector<int64_t> caseValues(callCases.size());
    Vector<CallToCodePtr> calls(callCases.size());
    uint32_t* fastCounts = 0;

    if (callerCodeBlock->jitType() != JITCode::topTierJIT())
        fastCounts = reinterpret_cast<uint32_t*>(fastMalloc(sizeof(uint32_t) * callCases.size()));

    for (size_t i = callCases.size(); i--;) {
        if (fastCounts)
            fastCounts[i] = 0;

        CallVariant variant = callCases[i].variant();
        if (isClosureCall)
            caseValues[i] = bitwise_cast<intptr_t>(variant.executable());
        else
            caseValues[i] = bitwise_cast<intptr_t>(variant.function());
    }

    GPRReg fastCountsBaseGPR =
        AssemblyHelpers::selectScratchGPR(calleeGPR, comparisonValueGPR, GPRInfo::regT3);
    stubJit.move(CCallHelpers::TrustedImmPtr(fastCounts), fastCountsBaseGPR);

    BinarySwitch binarySwitch(comparisonValueGPR, caseValues, BinarySwitch::IntPtr);
    CCallHelpers::JumpList done;
    while (binarySwitch.advance(stubJit)) {
        size_t caseIndex = binarySwitch.caseIndex();

        CallVariant variant = callCases[caseIndex].variant();

        ASSERT(variant.executable()->hasJITCodeForCall());
        MacroAssemblerCodePtr codePtr =
            variant.executable()->generatedJITCodeForCall()->addressForCall(
                *vm, variant.executable(), ArityCheckNotRequired, callLinkInfo.registerPreservationMode());

        if (fastCounts) {
            stubJit.add32(
                CCallHelpers::TrustedImm32(1),
                CCallHelpers::Address(fastCountsBaseGPR, caseIndex * sizeof(uint32_t)));
        }
        calls[caseIndex].call = stubJit.nearCall();
        calls[caseIndex].codePtr = codePtr;
        done.append(stubJit.jump());
    }

	slowPath.link(&stubJit);
    binarySwitch.fallThrough().link(&stubJit);
	stubJit.move(calleeGPR, GPRInfo::regT0);
#if USE(JSVALUE32_64)
	stubJit.move(CCallHelpers::TrustedImm32(JSValue::CellTag), GPRInfo::regT1);
#endif
    stubJit.move(CCallHelpers::TrustedImmPtr(&callLinkInfo), GPRInfo::regT2);
    stubJit.move(CCallHelpers::TrustedImmPtr(callLinkInfo.callReturnLocation().executableAddress()), GPRInfo::regT4);

    stubJit.restoreReturnAddressBeforeReturn(GPRInfo::regT4);
	AssemblyHelpers::Jump slow = stubJit.jump();

	LinkBuffer patchBuffer(*vm, &stubJit, callerCodeBlock, JITCompilationCanFail, true);
    if (patchBuffer.didFailToAllocate()) {
        linkVirtualFor(exec, callLinkInfo);
        return;
    }

    RELEASE_ASSERT(callCases.size() == calls.size());
    for(int i = 0; i < calls.size(); ++i) {
    	CallToCodePtr callToCodePtr = calls[i];
        patchBuffer.link(
            callToCodePtr.call, FunctionPtr(callToCodePtr.codePtr.executableAddress()));
    }
	patchBuffer.link(done, callLinkInfo.callReturnLocation().labelAtOffset(0));
	patchBuffer.link(slow, CodeLocationLabel(vm->getCTIStub(linkPolymorphicCallThunkGenerator).code()));

	RefPtr<PolymorphicCallStubRoutine> stubRoutine = adoptRef(new PolymorphicCallStubRoutine(
		FINALIZE_DFG_CODE(
			patchBuffer,
			("Polymorphic call stub for %s, return point %p, targets %s",
				toCString(*callerCodeBlock).data(), callLinkInfo.callReturnLocation().labelAtOffset(0).executableAddress(),
				""/*toCString(listDump(callCases)).data()*/)),
		*vm, callerCodeBlock->ownerExecutable(), exec->callerFrame(), callLinkInfo, callCases,
        fastCounts));

    MacroAssembler::replaceWithJump(
        MacroAssembler::startOfBranchPtrWithPatchOnRegister(callLinkInfo.hotPathBegin()),
		CodeLocationLabel(stubRoutine->code().code()));
    // The original slow path is unreachable on 64-bits, but still
    // reachable on 32-bits since a non-cell callee will always
    // trigger the slow path
    linkSlowFor(vm, callLinkInfo);

    // If there had been a previous stub routine, that one will die as soon as the GC runs and sees
    // that it's no longer on stack.
	callLinkInfo.setStub(stubRoutine.release());

    // The call link info no longer has a call cache apart from the jump to the polymorphic call
    // stub.
    if (callLinkInfo.isOnList())
        callLinkInfo.remove();
}

void resetGetByID(CodeBlock* codeBlock, StructureStubInfo& stubInfo)
{
	repatchCall(codeBlock, stubInfo.callReturnLocation, operationGetByIdOptimize);
    CodeLocationDataLabelPtr structureLabel = stubInfo.callReturnLocation.dataLabelPtrAtOffset(-(intptr_t)stubInfo.patch.deltaCheckImmToCall);
	if (MacroAssembler::canJumpReplacePatchableBranchPtrWithPatch()) {
		MacroAssembler::revertJumpReplacementToPatchableBranchPtrWithPatch(
			MacroAssembler::startOfPatchableBranchPtrWithPatchOnAddress(structureLabel),
			MacroAssembler::Address(
				static_cast<MacroAssembler::RegisterID>(stubInfo.patch.baseGPR),
				JSCell::structureOffset()),
			reinterpret_cast<void*>(unusedPointer));
	}
	MacroAssembler::repatchPointer(structureLabel, reinterpret_cast<void*>(unusedPointer));
#if USE(JSVALUE64)
	MacroAssembler::repatchCompact(stubInfo.callReturnLocation.dataLabelCompactAtOffset(stubInfo.patch.deltaCallToLoadOrStore), 0);
#else
	MacroAssembler::repatchCompact(stubInfo.callReturnLocation.dataLabelCompactAtOffset(stubInfo.patch.deltaCallToTagLoadOrStore), 0);
	MacroAssembler::repatchCompact(stubInfo.callReturnLocation.dataLabelCompactAtOffset(stubInfo.patch.deltaCallToPayloadLoadOrStore), 0);
#endif
	MacroAssembler::repatchJump(stubInfo.callReturnLocation.jumpAtOffset(stubInfo.patch.deltaCallToJump), stubInfo.callReturnLocation.labelAtOffset(stubInfo.patch.deltaCallToSlowCase));
}

void resetPutByID(CodeBlock* codeBlock, StructureStubInfo& stubInfo)
{
	V_JITOperation_ESsiJJI unoptimizedFunction = bitwise_cast<V_JITOperation_ESsiJJI>(readCallTarget(codeBlock, stubInfo.callReturnLocation).executableAddress());
	V_JITOperation_ESsiJJI optimizedFunction;
    if (unoptimizedFunction == operationPutByIdStrict || unoptimizedFunction == operationPutByIdStrictBuildList)
        optimizedFunction = operationPutByIdStrictOptimize;
    else if (unoptimizedFunction == operationPutByIdNonStrict || unoptimizedFunction == operationPutByIdNonStrictBuildList)
        optimizedFunction = operationPutByIdNonStrictOptimize;
    else if (unoptimizedFunction == operationPutByIdDirectStrict || unoptimizedFunction == operationPutByIdDirectStrictBuildList)
        optimizedFunction = operationPutByIdDirectStrictOptimize;
    else {
        ASSERT(unoptimizedFunction == operationPutByIdDirectNonStrict || unoptimizedFunction == operationPutByIdDirectNonStrictBuildList);
        optimizedFunction = operationPutByIdDirectNonStrictOptimize;
    }
    repatchCall(codeBlock, stubInfo.callReturnLocation, optimizedFunction);
    CodeLocationDataLabelPtr structureLabel = stubInfo.callReturnLocation.dataLabelPtrAtOffset(-(intptr_t)stubInfo.patch.deltaCheckImmToCall);
    if (MacroAssembler::canJumpReplacePatchableBranchPtrWithPatch()) {
    	MacroAssembler::revertJumpReplacementToPatchableBranchPtrWithPatch(
			MacroAssembler::startOfPatchableBranchPtrWithPatchOnAddress(structureLabel),
            MacroAssembler::Address(
                static_cast<MacroAssembler::RegisterID>(stubInfo.patch.baseGPR),
                JSCell::structureOffset()),
            reinterpret_cast<void*>(unusedPointer));
    }
    MacroAssembler::repatchPointer(structureLabel, reinterpret_cast<void*>(unusedPointer));
#if USE(JSVALUE64)
    MacroAssembler::repatchInt32(stubInfo.callReturnLocation.dataLabel32AtOffset(stubInfo.patch.deltaCallToLoadOrStore), 0);
#else
    MacroAssembler::repatchInt32(stubInfo.callReturnLocation.dataLabel32AtOffset(stubInfo.patch.deltaCallToTagLoadOrStore), 0);
    MacroAssembler::repatchInt32(stubInfo.callReturnLocation.dataLabel32AtOffset(stubInfo.patch.deltaCallToPayloadLoadOrStore), 0);
#endif
    MacroAssembler::repatchJump(stubInfo.callReturnLocation.jumpAtOffset(stubInfo.patch.deltaCallToJump), stubInfo.callReturnLocation.labelAtOffset(stubInfo.patch.deltaCallToSlowCase));
}

void resetIn(CodeBlock*, StructureStubInfo& stubInfo)
{
	MacroAssembler::repatchJump(stubInfo.callReturnLocation.jumpAtOffset(stubInfo.patch.deltaCallToJump), stubInfo.callReturnLocation.labelAtOffset(stubInfo.patch.deltaCallToSlowCase));
}

} // namespace JSC

#endif
