/*
 * Copyright (C) 2016 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 "WasmCallingConvention.h"

#if ENABLE(WEBASSEMBLY)

#include <wtf/NeverDestroyed.h>

namespace JSC::Wasm {

const JSCallingConvention& jsCallingConvention()
{
    static LazyNeverDestroyed<JSCallingConvention> staticJSCallingConvention;
    static std::once_flag staticJSCCallingConventionFlag;
    std::call_once(staticJSCCallingConventionFlag, [] () {
        RegisterSet callerSaveRegisters = RegisterSet::allRegisters();
        callerSaveRegisters.exclude(RegisterSet::calleeSaveRegisters());

        staticJSCallingConvention.construct(Vector<JSValueRegs>(), Vector<FPRReg>(), RegisterSet::calleeSaveRegisters(), WTFMove(callerSaveRegisters));
    });

    return staticJSCallingConvention;
}

const WasmCallingConvention& wasmCallingConvention()
{
    static LazyNeverDestroyed<WasmCallingConvention> staticWasmCallingConvention;
    static std::once_flag staticWasmCallingConventionFlag;
    std::call_once(staticWasmCallingConventionFlag, [] () {
#if USE(JSVALUE64) // One value per GPR
        constexpr unsigned numberOfArgumentJSRs = GPRInfo::numberOfArgumentRegisters;
#elif USE(JSVALUE32_64) // One value per consecutive GPR pair
        constexpr unsigned numberOfArgumentJSRs = GPRInfo::numberOfArgumentRegisters / 2;
#endif
        Vector<JSValueRegs> jsrArgumentRegisters(numberOfArgumentJSRs);
        for (unsigned i = 0; i < numberOfArgumentJSRs; ++i) {
#if USE(JSVALUE64)
            jsrArgumentRegisters[i] = JSValueRegs { GPRInfo::toArgumentRegister(i) };
#elif USE(JSVALUE32_64)
            jsrArgumentRegisters[i] = JSValueRegs { GPRInfo::toArgumentRegister(2 * i + 1), GPRInfo::toArgumentRegister(2 * i) };
#endif
        }

        Vector<FPRReg> fprArgumentRegisters(FPRInfo::numberOfArgumentRegisters);
        for (unsigned i = 0; i < FPRInfo::numberOfArgumentRegisters; ++i)
            fprArgumentRegisters[i] = FPRInfo::toArgumentRegister(i);

        RegisterSet scratch = RegisterSet::allGPRs();
        scratch.exclude(RegisterSet::vmCalleeSaveRegisters());
        scratch.exclude(RegisterSet::macroScratchRegisters());
        scratch.exclude(RegisterSet::reservedHardwareRegisters());
        scratch.exclude(RegisterSet::stackRegisters());
        for (JSValueRegs jsr : jsrArgumentRegisters) {
            scratch.clear(jsr.payloadGPR());
#if USE(JSVALUE32_64)
            scratch.clear(jsr.tagGPR());
#endif
        }

        Vector<GPRReg> scratchGPRs;
        for (Reg reg : scratch)
            scratchGPRs.append(reg.gpr());

        // Need at least one JSValue and an additional GPR
#if USE(JSVALUE64)
        RELEASE_ASSERT(scratchGPRs.size() >= 2);
#elif USE(JSVALUE32_64)
        RELEASE_ASSERT(scratchGPRs.size() >= 3);
#endif

        RegisterSet callerSaveRegisters = RegisterSet::allRegisters();
        callerSaveRegisters.exclude(RegisterSet::calleeSaveRegisters());

        staticWasmCallingConvention.construct(WTFMove(jsrArgumentRegisters), WTFMove(fprArgumentRegisters), WTFMove(scratchGPRs), RegisterSet::calleeSaveRegisters(), WTFMove(callerSaveRegisters));
    });

    return staticWasmCallingConvention;
}

} // namespace JSC::Wasm

#endif // ENABLE(B3_JIT)
