<commit_before>-- |
-- Module      : $Header$
-- Description : The compiler pipeline
-- Copyright   : (c) <NAME> and <NAME> 2017. All Rights Reserved.
-- License     : EPL-1.0
-- Maintainer  : <EMAIL>, <EMAIL>
-- Stability   : experimental
-- Portability : portable

-- This source code is licensed under the terms described in the associated LICENSE.TXT file
{-# LANGUAGE CPP             #-}
{-# LANGUAGE RecordWildCards #-}
module Ohua.Compile where


import           Control.DeepSeq
import           Control.Monad.Except
import           Data.Default.Class
import qualified Data.HashMap.Strict       as HM
import           Data.Monoid               ((<>))
import           Data.Text                 (Text, pack)
import           Ohua.ALang.Lang
import           Ohua.ALang.Optimizations
import           Ohua.ALang.Passes
import           Ohua.ALang.Passes.SSA
import           Ohua.DFGraph
import           Ohua.DFLang.Lang
import           Ohua.DFLang.Optimizations
import           Ohua.DFLang.Passes
import           Ohua.Monad
import           Ohua.Types
import qualified Ohua.Util.Str             as Str


data CustomPasses env = CustomPasses
  { passAfterDFLowering :: DFExpr -> OhuaM env DFExpr
  }

noCustomPasses :: CustomPasses env
noCustomPasses = CustomPasses pure

instance Default (CustomPasses env) where
  def = noCustomPasses


forceLog :: (MonadLogger m, NFData a) => Text -> a -> m ()
forceLog msg a = a `deepseq` logDebugN msg


-- | The canonical order of transformations and lowerings performed in a full compilation.
pipeline :: CustomPasses env -> Expression -> OhuaM env OutGraph
pipeline CustomPasses{..} e = do
    ssaE <- performSSA e
    normalizedE <- normalize ssaE
#ifdef DEBUG
    checkProgramValidity normalizedE
    checkHigherOrderFunctionSupport normalizedE
    Ohua.ALang.Passes.SSA.checkSSA normalizedE
#endif

    optimizedE <- Ohua.ALang.Optimizations.runOptimizations normalizedE

#ifdef DEBUG
    Ohua.ALang.Passes.SSA.checkSSA optimizedE
#endif

    dfE <- lowerALang optimizedE

    forceLog (pack $ Str.toString $ showDFExpr dfE) ()

#ifdef DEBUG
    Ohua.DFLang.Passes.checkSSAExpr dfE
#endif

    dfAfterCustom <- passAfterDFLowering dfE

    optimizedDfE <- Ohua.DFLang.Optimizations.runOptimizations dfAfterCustom<commit_msg>Adding a custom pass on ALang<commit_after>-- |
-- Module      : $Header$
-- Description : The compiler pipeline
-- Copyright   : (c) <NAME> and <NAME> 2017. All Rights Reserved.
-- License     : EPL-1.0
-- Maintainer  : <EMAIL>, <EMAIL>
-- Stability   : experimental
-- Portability : portable

-- This source code is licensed under the terms described in the associated LICENSE.TXT file
{-# LANGUAGE CPP             #-}
{-# LANGUAGE RecordWildCards #-}
module Ohua.Compile where


import           Control.DeepSeq
import           Control.Monad.Except
import           Data.Default.Class
import qualified Data.HashMap.Strict       as HM
import           Data.Monoid               ((<>))
import           Data.Text                 (Text, pack)
import           Ohua.ALang.Lang
import           Ohua.ALang.Optimizations
import           Ohua.ALang.Passes
import           Ohua.ALang.Passes.SSA
import           Ohua.DFGraph
import           Ohua.DFLang.Lang
import           Ohua.DFLang.Optimizations
import           Ohua.DFLang.Passes
import           Ohua.Monad
import           Ohua.Types
import qualified Ohua.Util.Str             as Str


data CustomPasses env = CustomPasses
  { passAfterDFLowering :: DFExpr -> OhuaM env DFExpr
  , passAfterNormalize  :: Expression -> OhuaM env Expression
  }

noCustomPasses :: CustomPasses env
noCustomPasses = CustomPasses pure pure

instance Default (CustomPasses env) where
  def = noCustomPasses


forceLog :: (MonadLogger m, NFData a) => Text -> a -> m ()
forceLog msg a = a `deepseq` logDebugN msg


-- | The canonical order of transformations and lowerings performed in a full compilation.
pipeline :: CustomPasses env -> Expression -> OhuaM env OutGraph
pipeline CustomPasses{..} e = do
    ssaE <- performSSA e
    normalizedE <- normalize ssaE
#ifdef DEBUG
    checkProgramValidity normalizedE
    checkHigherOrderFunctionSupport normalizedE
    Ohua.ALang.Passes.SSA.checkSSA normalizedE
#endif

    customAfterNorm <- normalize =<< passAfterNormalize normalizedE

    optimizedE <- Ohua.ALang.Optimizations.runOptimizations customAfterNorm

#ifdef DEBUG
    Ohua.ALang.Passes.SSA.checkSSA optimizedE
#endif

    dfE <- lowerALang optimizedE

    forceLog (pack $ Str.toString $ showDFExpr dfE) ()

#ifdef DEBUG
    Ohua.DFLang.Passes.checkSSAExpr dfE
#endif

    dfAfterCustom <- passAfterDFLowering dfE

    optimizedDfE <- Ohua.DFLang.Optimizations.runOptimizations dfAfterCustom
{'fraction_non_alphanumeric': 0.08015267175572519, 'fraction_numerical': 0.002544529262086514, 'mean_word_length': 2.676539360872954, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7277977', 'n_tokens_mistral': 1462, 'n_tokens_neox': 1403, 'n_words': 394}