Goose  Artifact [5d55fd20a8]

Artifact 5d55fd20a8dbb70713f6d158437dac4a1ffe6537994bdff13da09a03fab4bfaa:

  • File bs/codegen/stack.h — part of check-in [2a03dd67e5] at 2022-06-28 22:23:50 on branch cir-stack-language — codegen: improved the translation of address computations into llvm GEP instructions (user: zlodo size: 1792) [more...]

#ifndef GOOSE_CODEGEN_STACK_H
#define GOOSE_CODEGEN_STACK_H

namespace goose::codegen
{
    class Stack
    {
        public:
            using Slot = variant< llvm::Value*, codegen::Address >;

            template< typename T = llvm::Value* >
            optional< T > pop( llvm::IRBuilder<>& builder )
            {
                if( m_stack.empty() )
                    return nullopt;

                auto result = m_stack.top();
                m_stack.pop();

                if constexpr( is_same_v< T, codegen::Address > )
                {
                    if( !holds_alternative< codegen::Address >( result ) )
                        return nullopt;
                    return get< codegen::Address >( result );
                }
                else if constexpr( is_same_v< T, llvm::Value* > )
                {
                    if( holds_alternative< codegen::Address >( result ) )
                        return AddressToGEP( builder, get< codegen::Address >( result ) );
                    else
                        return get< llvm::Value* >( result );
                }
                else if( holds_alternative< llvm::Value* >( result ) )
                    return llvm::dyn_cast_or_null< remove_pointer_t< T > >( get< llvm::Value* >( result ) );

                return nullopt;
            }

            optional< Slot > pop()
            {
                if( m_stack.empty() )
                    return nullopt;

                auto result = m_stack.top();
                m_stack.pop();
                return result;
            }

            template< typename T >
            void push( T&& v )
            {
                m_stack.push( forward< T >( v ) );
            }

        private:
            stack< Slot > m_stack;
    };
}

#endif