#ifndef GOOSE_CODEGEN_MODULE_H
#define GOOSE_CODEGEN_MODULE_H

namespace llvm
{
    class TargetMachine;
}

namespace goose::codegen
{
    class Module
    {
        public:
            Module( const string& name );

            bool setupTarget();
            void dumpAsLlvmIr( const string& filename ) const;

            llvm::FunctionType* getOrCreateFuncType( const Context& c, const builtins::FuncType& ftype );

            llvm::Function* buildFuncProto( const Context& c, const builtins::Func& func, const string& name,
                llvm::Function::LinkageTypes linkageType );

            llvm::Function* getOrCreateFunc( const Context& c, const builtins::Func& func );
            llvm::Function* getOrCreateFunc( const Context& c, const builtins::Func& func, const string& name,
                llvm::Function::LinkageTypes linkageType );

            void runOptimizationPasses();
            bool emitToFile( const string& filename, llvm::CodeGenFileType type );

        private:
            struct Infos
            {
                Infos( const Context& c ) : context( c ) {}
                const Context& context;

                llvm::BasicBlock* allocaBasicBlock = nullptr;
                llvm::AllocaInst* lastEmittedAlloca = nullptr;

                using storage_type = cir::TempStorage< NullInit< llvm::Value* > >;
                ptr< storage_type > temporaries = make_shared< storage_type >();
            };

            llvm::Function* getCurrentFunction() const
            {
                return m_llvmBuilder.GetInsertBlock()->getParent();
            }

            llvm::BasicBlock* buildCFG( Infos& inf, llvm::Function* llvmFunc, const ptr< cir::CFG >& pCFG );
            llvm::BasicBlock* buildBasicBlock( Infos& inf, const ptr< cir::BasicBlock >& pBB );

            llvm::Value* buildValue( Infos& inf, const Value& val );
            llvm::Constant* buildConstant( Infos& inf, const Value& val );
            llvm::Constant* buildConstantPointer( Infos& inf, const builtins::PointerType& ptType, const Value& v );

            llvm::Value* buildInstruction( Infos& inf, const cir::Instruction& instr );
            llvm::Value* buildInstruction( Infos& inf, const monostate& );
            llvm::Value* buildInstruction( Infos& inf, const cir::Call& call );
            llvm::Value* buildInstruction( Infos& inf, const cir::VarAddr& va );
            llvm::Value* buildInstruction( Infos& inf, const cir::TempAddr& ta );
            llvm::Value* buildInstruction( Infos& inf, const cir::Select& s );
            llvm::Value* buildInstruction( Infos& inf, const cir::CreateTemporary& ct );
            llvm::Value* buildInstruction( Infos& inf, const cir::GetTemporary& gt );
            llvm::Value* buildInstruction( Infos& inf, const cir::AllocVar& av );
            llvm::Value* buildInstruction( Infos& inf, const cir::Load& load );
            llvm::Value* buildInstruction( Infos& inf, const cir::Store& store );
            llvm::Value* buildInstruction( Infos& inf, const cir::Phi& p );

            llvm::Value* buildInstruction( Infos& inf, const cir::Not& uo );
            llvm::Value* buildInstruction( Infos& inf, const cir::And& bo );
            llvm::Value* buildInstruction( Infos& inf, const cir::Or& bo );
            llvm::Value* buildInstruction( Infos& inf, const cir::Xor& bo );
            llvm::Value* buildInstruction( Infos& inf, const cir::Shl& bo );
            llvm::Value* buildInstruction( Infos& inf, const cir::LShr& bo );
            llvm::Value* buildInstruction( Infos& inf, const cir::AShr& bo );

            llvm::Value* buildInstruction( Infos& inf, const cir::Add& bo );
            llvm::Value* buildInstruction( Infos& inf, const cir::Sub& bo );
            llvm::Value* buildInstruction( Infos& inf, const cir::Mul& bo );
            llvm::Value* buildInstruction( Infos& inf, const cir::UDiv& bo );
            llvm::Value* buildInstruction( Infos& inf, const cir::SDiv& bo );
            llvm::Value* buildInstruction( Infos& inf, const cir::URem& bo );
            llvm::Value* buildInstruction( Infos& inf, const cir::SRem& bo );

            llvm::Value* buildInstruction( Infos& inf, const cir::Eq& bo );
            llvm::Value* buildInstruction( Infos& inf, const cir::Neq& bo );
            llvm::Value* buildInstruction( Infos& inf, const cir::UGT& bo );
            llvm::Value* buildInstruction( Infos& inf, const cir::UGE& bo );
            llvm::Value* buildInstruction( Infos& inf, const cir::ULT& bo );
            llvm::Value* buildInstruction( Infos& inf, const cir::ULE& bo );
            llvm::Value* buildInstruction( Infos& inf, const cir::SGT& bo );
            llvm::Value* buildInstruction( Infos& inf, const cir::SGE& bo );
            llvm::Value* buildInstruction( Infos& inf, const cir::SLT& bo );
            llvm::Value* buildInstruction( Infos& inf, const cir::SLE& bo );

            llvm::Value* buildInstruction( Infos& inf, const cir::Assert& ass );
            llvm::Value* buildInstruction( Infos& inf, const cir::Placeholder& ph );

            bool buildTerminator( Infos& inf, const cir::Terminator& terminator );
            bool buildTerminator( Infos& inf, const cir::Ret& r );
            bool buildTerminator( Infos& inf, const cir::Branch& b );
            bool buildTerminator( Infos& inf, const cir::CondBranch& cb );

            template< typename T >
            bool buildTerminator( Infos& inf, const T& t )
            {
                return false;
            }

            llvm::Value* buildAlloca( Infos& inf, llvm::Type* type );
            llvm::Value* createTemporary( Infos& inf, uint32_t index, llvm::Value* pValue ) const;

            llvm::Module m_llvmModule;
            llvm::DataLayout m_dataLayout;
            llvm::IRBuilder<> m_llvmBuilder;
            llvm::TargetMachine* m_targetMachine = nullptr;

            unordered_map< string, llvm::GlobalVariable* > m_strings;
            uint32_t m_nextAggregateID = 0;
    };
}

#endif
