Goose  Artifact [f0895f33e4]

Artifact f0895f33e45388b929a3ff6fb364eb4dafd0373e26394db6ae372ea6f3c799c2:

  • File bs/cir/helpers.h — part of check-in [a0a57b5e22] at 2022-06-22 20:06:00 on branch cir-stack-language — Re-implemented "addr modified by loop" stuff (except ghost func related stuff) and re-enabled loop verification tests (user: zlodo size: 3346)

#ifndef GOOSE_CIR_HELPERS_H
#define GOOSE_CIR_HELPERS_H

namespace goose::cir
{
    class Instruction;
    using InstrSeq = list< Instruction >;

    extern bool IsValueConstantOrExecutable( const eir::Value& val );
    extern bool CanValueBeEagerlyEvaluated( const eir::Value& val );

    extern bool DoesInstrSeqHaveSideEffects( const InstrSeq& is );

    extern void AppendInstrSeq( InstrSeq& is, InstrSeq&& isToAppend );
    extern void AppendInstrSeq( InstrSeq& is, const InstrSeq& isToAppend );

    extern void AppendValue( InstrSeq& is, Value&& v );
    extern void AppendValue( InstrSeq& is, const Value& v );

    template< typename I >
    void AppendToInstrSeq( InstrSeq& is, I&& instr )
    {
        using II = remove_cvref_t< I >;

        if constexpr( is_same_v< II, InstrSeq > )
        {
            AppendInstrSeq( is, forward< I >( instr ) );
        }
        else if constexpr( is_same_v< II, Value > )
        {
            AppendValue( is, forward< I >( instr ) );
        }
        else
        {
            is.emplace_back( Instruction( forward< I >( instr ) ) );
        }
    }

    static inline void AppendToInstrSeq( InstrSeq& is, const ptr< InstrSeq >& instr )
    {
        AppendToInstrSeq( is, *instr );
    }

    static inline void AppendToInstrSeq( InstrSeq& is, ptr< InstrSeq >&& instr )
    {
        AppendToInstrSeq( is, move( *instr ) );
    }

    template< typename HI, typename... TI >
    void AppendToInstrSeq( InstrSeq& is, HI&& headInstr, TI&&... tailInstrs )
    {
        AppendToInstrSeq( is, forward< HI >( headInstr ) );
        AppendToInstrSeq( is, forward< TI >( tailInstrs )... );
    }

    template< typename T, typename... I >
    auto BuildComputedValue( T&& type, I&&... instrs )
    {
        auto is = make_shared< InstrSeq >();
        AppendToInstrSeq( *is, forward< I >( instrs )... );

        return eir::Value( forward< T >( type ), move( is ) );
    }

    template< size_t popCount, size_t pushCount >
    class BaseInstr
    {
        public:
            BaseInstr( LocationId loc ) :
                m_loc( loc )
            {}

            void setLocationId( LocationId loc ) { m_loc = loc; }

            auto locationId() const { return m_loc; }

            static constexpr size_t PopCount = popCount;
            static constexpr size_t PushCount = pushCount;

        private:
            LocationId m_loc;
    };

    template< typename T >
    class TempStorage
    {
        public:
            TempStorage( size_t size ) :
                m_storage( size )
            {}

            template< typename TT >
            T& set( uint32_t index, TT&& x )
            {
                if( index >= m_storage.size() )
                    m_storage.resize( index + 1 );
                m_storage[index] = forward< TT >( x );
                return m_storage[index];
            }

            const T* get( uint32_t index ) const
            {
                if( index < m_storage.size() )
                    return &m_storage[index];
                return nullptr;
            }

            T* get( uint32_t index )
            {
                if( index < m_storage.size() )
                    return &m_storage[index];
                return nullptr;
            }

        private:
            llvm::SmallVector< T, 16 > m_storage;
    };
}

#endif