#include "cir/cir.h"

namespace goose::cir
{
    bool CanBeExecuted( const Instruction& instr )
    {
        return visit(
            []< typename ET >( const ET& e )
            {
                if constexpr( is_same_v< ET, monostate > )
                    return false;
                else
                    return e.canBeExecuted();
            },
            instr );
    }

    bool CanBeEagerlyEvaluated( const Instruction& instr )
    {
        return visit(
            []< typename ET >( const ET& e )
            {
                if constexpr( is_same_v< ET, monostate > )
                    return false;
                else
                    return e.canBeEagerlyEvaluated();
            },
            instr );
    }

    bool HaveSideEffects( const Instruction& instr )
    {
        return visit(
            []< typename ET >( const ET& e )
            {
                if constexpr( is_same_v< ET, monostate > )
                    return false;
                else
                    return e.haveSideEffects();
            },
            instr );
    }

    ostream& operator<<( ostream& out, const Instruction& instr )
    {
        return visit(
            [&]< typename ET >( const ET& e ) -> ostream&
            {
                if constexpr( is_same_v< ET, monostate > )
                    return out << "NIL";
                else
                    return out << e;
            },
            instr );
    }

    ostream& operator<<( ostream& out, const Select& ins )
    {
        out << "SELECT(" << ins.m_memberIndex;
        return out << ')';
    }

    ostream& operator<<( ostream& out, const Load& ins )
    {
        out << "LOAD(";
        return out << ins.m_type.type() << ')';
    }

    ostream& operator<<( ostream& out, const Store& ins )
    {
        return out << "STORE";
    }

    ostream& operator<<( ostream& out, const PHOverrideSet& ins )
    {
        return out << "PHOVERRIDESET(" << ins.m_name << ')';
    }

    ostream& operator<<( ostream& out, const PHOverrideClear& ins )
    {
        return out << "PHOVERRIDECLEAR(" << ins.m_name << ')';
    }

    ostream& operator<<( ostream& out, const Phi& ins )
    {
        out << "PHI(" << ins.m_destIndex << ", " << ins.m_type.get();

        for( const auto& [bb, val] : ins.m_incomings )
            out << ", " << bb.lock()->index() << ':' << val;

        return out << " )";
    }

    bool Select::operator<( const Select& rhs ) const
    {
        return m_memberIndex < rhs.m_memberIndex;
    }

    bool Load::operator<( const Load& rhs ) const
    {
        return m_type < rhs.m_type.get();
    }

    bool Store::operator<( const Store& rhs ) const
    {
        return m_type < rhs.m_type.get();
    }

    bool PHOverrideSet::operator<( const PHOverrideSet& rhs ) const
    {
        return m_name < rhs.m_name;
    }

    bool PHOverrideClear::operator<( const PHOverrideClear& rhs ) const
    {
        return m_name < rhs.m_name;
    }
} // namespace goose::cir
