/* -------------------------------------------------------------------------
 * This file is part of the MindStudio project.
 * Copyright (c) 2025 Huawei Technologies Co.,Ltd.
 *
 * MindStudio is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *
 *          http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 * ------------------------------------------------------------------------- */

#include "StubFuncContext.h"
#include "acl_rt_impl/AscendclImplOrigin.h"

using namespace std;

StubFuncContext::StubFuncContext(RegisterContextSP regCtx, const char *kernelName, void *funcHandle) : FuncContext(regCtx, funcHandle)
{
    kernelName_ = kernelName;
}

uint64_t StubFuncContext::GetKernelPC() const
{
    if (funcHandle_) {
        return FuncContext::GetKernelPC();
    }
    return 0;
}

uint64_t StubFuncContext::GetStartPC() const
{
    if (funcHandle_) {
        return FuncContext::GetStartPC();
    }
    return 0;
}

FuncContextSP StubFuncContext::Clone(const RegisterContextSP &regCtx) const
{
    if (funcHandle_) {
        aclrtBinHandle binHandle = regCtx->GetHandle();
        aclrtFuncHandle funcHandle;
        auto ret = aclrtBinaryGetFunctionImplOrigin(binHandle, kernelName_.c_str(), &funcHandle);
        if (ret != ACL_SUCCESS) {
            return nullptr;
        }
        FuncContextSP newFuncContext = make_shared<StubFuncContext>(regCtx, kernelName_.c_str(), funcHandle);
        return newFuncContext;
    }
    return nullptr;
}