Goose  Artifact [81f863900e]

Artifact 81f863900e4ebf53d8b3c33e40cc0fe92d544a57fb68bfff8685c7d0d30f35fd:

  • File bs/codegen/module.h — part of check-in [c44aed7937] at 2019-08-05 19:02:51 on branch trunk — llr, execute, codegen: Implemented the And, Or, Shl, LShr and AShr instructions. (user: achavasse size: 4200)

#ifndef EMPATHY_CODEGEN_MODULE_H
#define EMPATHY_CODEGEN_MODULE_H

namespace llvm
{
    class TargetMachine;
}

namespace empathy::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::TargetMachine::CodeGenFileType type );

        private:
            struct Infos
            {
                using storage_type = llr::TempStorage< NullInit< llvm::Value* > >;
                ptr< storage_type > temporaries = make_shared< storage_type >();

                bool inlining = false;
                llvm::Value* inlineResult = nullptr;
            };

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

            llvm::BasicBlock* buildCFG( const Context& c, llvm::Function* llvmFunc, const ptr< llr::CFG >& pCFG );
            llvm::Value* inlineCFG( const Context& c, Infos& inf, const ptr< llr::CFG >& pCFG );

            llvm::BasicBlock* buildBasicBlock( const Context& c, Infos& inf, const ptr< llr::BasicBlock >& pBB );
            bool inlineBasicBlock( const Context& c, Infos& inf, const ptr< BasicBlock >& pBB );

            llvm::Value* buildValue( const Context& c, Infos& inf, const Value& val );

            llvm::Value* buildInstruction( const Context& c, Infos& inf, const llr::Instruction& instr );
            llvm::Value* buildInstruction( const Context& c, Infos& inf, const ptr< CFG >& ic );
            llvm::Value* buildInstruction( const Context& c, Infos& inf, const llr::GetArg& ga );
            llvm::Value* buildInstruction( const Context& c, Infos& inf, const llr::Call& call );
            llvm::Value* buildInstruction( const Context& c, Infos& inf, const llr::CreateTemporary& ct );
            llvm::Value* buildInstruction( const Context& c, Infos& inf, const llr::GetTemporary& gt );
            llvm::Value* buildInstruction( const Context& c, Infos& inf, const llr::Phi& p );
            llvm::Value* buildInstruction( const Context& c, Infos& inf, const llr::LoadConstInt& lci );
            llvm::Value* buildInstruction( const Context& c, Infos& inf, const llr::LoadConstStr& lcs );

            llvm::Value* buildInstruction( const Context& c, Infos& inf, const llr::And& bo );
            llvm::Value* buildInstruction( const Context& c, Infos& inf, const llr::Or& bo );
            llvm::Value* buildInstruction( const Context& c, Infos& inf, const llr::Xor& bo );
            llvm::Value* buildInstruction( const Context& c, Infos& inf, const llr::Shl& bo );
            llvm::Value* buildInstruction( const Context& c, Infos& inf, const llr::LShr& bo );
            llvm::Value* buildInstruction( const Context& c, Infos& inf, const llr::AShr& bo );

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

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

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

            unordered_map< string, llvm::GlobalVariable* > m_strings;
    };
}

#endif