/**
 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "plugins/ets/compiler/optimizer/ets_codegen_extensions.h"

inline void CreateLoadObjectDynamic([[maybe_unused]] Inst *inst)
{

    LOG(WARNING, COMPILER) << "Cannot encode LoadObjectDynamic";
    GetEncoder()->SetFalseResult();
}

inline void CreateStoreObjectDynamic([[maybe_unused]] Inst *inst)
{

    LOG(WARNING, COMPILER) << "Cannot encode StoreObjectDynamic";
    GetEncoder()->SetFalseResult();
}

inline void ResolveCallByName([[maybe_unused]] ResolveVirtualInst *inst)
{
    if (ResolveCallByNameCodegen(inst)) {
        return;
    }

    LOG(WARNING, COMPILER) << "Cannot encode ResolveCallByName";
    GetEncoder()->SetFalseResult();
}

inline void GetNativeMethod([[maybe_unused]] IntrinsicInst *inst, [[maybe_unused]] Reg dst, [[maybe_unused]] SRCREGS src)
{
    [[maybe_unused]] auto *graph = inst->GetBasicBlock()->GetGraph();
    if (graph->GetLanguage() == SourceLanguage::ETS) {
        EtsGetNativeMethod(inst, dst, src);
        return;
    }
    LOG(WARNING, COMPILER) << "Cannot encode intrinsic GetNativeMethod";
    GetEncoder()->SetFalseResult();
}

inline void GetNativeMethodManagedClass([[maybe_unused]] IntrinsicInst *inst, [[maybe_unused]] Reg dst, [[maybe_unused]] SRCREGS src)
{
    [[maybe_unused]] auto *graph = inst->GetBasicBlock()->GetGraph();
    if (graph->GetLanguage() == SourceLanguage::ETS) {
        EtsGetNativeMethodManagedClass(inst, dst, src);
        return;
    }
    LOG(WARNING, COMPILER) << "Cannot encode intrinsic GetNativeMethodManagedClass";
    GetEncoder()->SetFalseResult();
}

inline void GetMethodNativePointer([[maybe_unused]] IntrinsicInst *inst, [[maybe_unused]] Reg dst, [[maybe_unused]] SRCREGS src)
{
    [[maybe_unused]] auto *graph = inst->GetBasicBlock()->GetGraph();
    if (graph->GetLanguage() == SourceLanguage::ETS) {
        EtsGetMethodNativePointer(inst, dst, src);
        return;
    }
    LOG(WARNING, COMPILER) << "Cannot encode intrinsic GetMethodNativePointer";
    GetEncoder()->SetFalseResult();
}

inline void GetNativeApiEnv([[maybe_unused]] IntrinsicInst *inst, [[maybe_unused]] Reg dst, [[maybe_unused]] SRCREGS src)
{
    [[maybe_unused]] auto *graph = inst->GetBasicBlock()->GetGraph();
    if (graph->GetLanguage() == SourceLanguage::ETS) {
        EtsGetNativeApiEnv(inst, dst, src);
        return;
    }
    LOG(WARNING, COMPILER) << "Cannot encode intrinsic GetNativeApiEnv";
    GetEncoder()->SetFalseResult();
}

inline void BeginNativeMethod([[maybe_unused]] IntrinsicInst *inst, [[maybe_unused]] Reg dst, [[maybe_unused]] SRCREGS src)
{
    [[maybe_unused]] auto *graph = inst->GetBasicBlock()->GetGraph();
    if (graph->GetLanguage() == SourceLanguage::ETS) {
        EtsBeginNativeMethod(inst, dst, src);
        return;
    }
    LOG(WARNING, COMPILER) << "Cannot encode intrinsic BeginNativeMethod";
    GetEncoder()->SetFalseResult();
}

inline void EndNativeMethodPrim([[maybe_unused]] IntrinsicInst *inst, [[maybe_unused]] Reg dst, [[maybe_unused]] SRCREGS src)
{
    [[maybe_unused]] auto *graph = inst->GetBasicBlock()->GetGraph();
    if (graph->GetLanguage() == SourceLanguage::ETS) {
        EtsEndNativeMethodPrim(inst, dst, src);
        return;
    }
    LOG(WARNING, COMPILER) << "Cannot encode intrinsic EndNativeMethodPrim";
    GetEncoder()->SetFalseResult();
}

inline void EndNativeMethodObj([[maybe_unused]] IntrinsicInst *inst, [[maybe_unused]] Reg dst, [[maybe_unused]] SRCREGS src)
{
    [[maybe_unused]] auto *graph = inst->GetBasicBlock()->GetGraph();
    if (graph->GetLanguage() == SourceLanguage::ETS) {
        EtsEndNativeMethodObj(inst, dst, src);
        return;
    }
    LOG(WARNING, COMPILER) << "Cannot encode intrinsic EndNativeMethodObj";
    GetEncoder()->SetFalseResult();
}

inline void CheckNativeException([[maybe_unused]] IntrinsicInst *inst, [[maybe_unused]] Reg dst, [[maybe_unused]] SRCREGS src)
{
    [[maybe_unused]] auto *graph = inst->GetBasicBlock()->GetGraph();
    if (graph->GetLanguage() == SourceLanguage::ETS) {
        EtsCheckNativeException(inst, dst, src);
        return;
    }
    LOG(WARNING, COMPILER) << "Cannot encode intrinsic CheckNativeException";
    GetEncoder()->SetFalseResult();
}

inline void WrapObjectNative([[maybe_unused]] WrapObjectNativeInst *inst)
{
    [[maybe_unused]] auto *graph = inst->GetBasicBlock()->GetGraph();
    if (graph->GetLanguage() == SourceLanguage::ETS) {
        EtsWrapObjectNative(inst);
        return;
    }
    LOG(WARNING, COMPILER) << "Cannot encode WrapObjectNative";
    GetEncoder()->SetFalseResult();
}
