Goose  Artifact [3b4f3ede86]

Artifact 3b4f3ede863b92d8504c3649b533e477e4a61d1e384ecf13e03f0ad08db5fb38:

  • File bs/codegen/module.h — part of check-in [9a68159d52] at 2019-08-25 01:54:54 on branch trunk —
    • Got rid of the LoadConstInt instruction and directly handle integer constants in codegen. The old system didn't work for compile-time evaluated runtime integers.
    • Added missing implementations for unsigned comparison, that were somehow forgotten.
    • Implemented the nullptr constant.
    • Made a test version of the mandelbrot sample that gets both compiled and interpreted and whose results are compared with the same reference file, as a sanity test that interpreted and compiled code behave identically.
    (user: achavasse size: 5612)

#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
            {
                Infos( const Context& c ) : context( c ) {}
                const Context& context;
                llvm::SmallVector< llvm::Value*, 8 > m_locals;

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

                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( Infos& inf, llvm::Function* llvmFunc, const ptr< llr::CFG >& pCFG );
            llvm::Value* inlineCFG( Infos& inf, const ptr< llr::CFG >& pCFG );

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

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

            llvm::Value* buildInstruction( Infos& inf, const llr::Instruction& instr );
            llvm::Value* buildInstruction( Infos& inf, const ptr< CFG >& ic );
            llvm::Value* buildInstruction( Infos& inf, const llr::Call& call );
            llvm::Value* buildInstruction( Infos& inf, const llr::CreateTemporary& ct );
            llvm::Value* buildInstruction( Infos& inf, const llr::GetTemporary& gt );
            llvm::Value* buildInstruction( Infos& inf, const llr::AllocVar& av );
            llvm::Value* buildInstruction( Infos& inf, const llr::GetVar& gv );
            llvm::Value* buildInstruction( Infos& inf, const llr::SetVar& sv );
            llvm::Value* buildInstruction( Infos& inf, const llr::Phi& p );
            llvm::Value* buildInstruction( Infos& inf, const llr::LoadConstStr& lcs );

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

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

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

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

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

            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