module Camfort.Analysis.Loops where
import Data.Data
import Data.List
import Data.Ord
import Language.Fortran
import Language.Fortran.Pretty
import Data.Generics.Uniplate.Operations
import Control.Monad.State.Lazy
import Debug.Trace
import Camfort.Analysis.LVA
import Camfort.Analysis.Annotations
import Camfort.Analysis.Syntax
import Camfort.Analysis.Types
import Camfort.Helpers
import Camfort.Traverse
import Camfort.Transformation.Syntax
import qualified Data.Map.Lazy as Map hiding (map, (\\))
loopAnalyse :: Program a -> Program Annotation
loopAnalyse p = map ((descendBi arrayIndices) . ix . lvaOnUnit . (transformBi reassociate) . (fmap (const unitAnnotation))) p
analyse' :: Program Annotation -> Program Annotation
analyse' p = map ((descendBi arrayIndices) . ix . lvaOnUnit . (transformBi reassociate)) p
collect :: (Eq a, Ord k) => [(k, a)] -> Map.Map k [a]
collect = Map.fromListWith union . map (fmap (:[]))
arrayIndices :: Block Annotation -> Block Annotation
arrayIndices x =
let tenv = typeEnv x
arrIxsF :: Fortran Annotation -> Annotation
arrIxsF y = let readIxs = [(v, mfmap (const ()) e) |
(Var _ _ [(VarName _ v, e)]) <- rhsExpr y,
length e > 0,
isArrayType tenv v]
writeIxs = [(v, mfmap (const ()) e) |
(Var _ _ [(VarName _ v, e)]) <- lhsExpr y,
length e > 0,
isArrayType tenv v]
in (tag y) { arrsRead = (collect readIxs), arrsWrite = (collect writeIxs) }
in extendBi arrIxsF x
ix :: ProgUnit Annotation -> ProgUnit Annotation
ix = let ixF :: Fortran Annotation -> Annotation
ixF f = (tag f) { indices = (nub [v | (For _ _ (VarName _ v) _ _ _ _) <- ((universeBi f)::[Fortran Annotation])])}
in extendBi ixF
loopVariables :: ProgUnit Annotation -> [String]
loopVariables f = (nub [v | (For _ _ (VarName _ v) _ _ _ _) <- ((universeBi f)::[Fortran Annotation])])