From 979eec927ab0c54e99a709f744263bc56ff79904 Mon Sep 17 00:00:00 2001 From: Neil Brown Date: Wed, 4 Feb 2009 11:30:57 +0000 Subject: [PATCH] Added a pass that removes replicators with a count of 0, and turns replicators with a count of 1 into non-replicated things (with the appropriate abbreviation) --- pass/PassList.hs | 1 + transformations/SimplifyProcs.hs | 42 +++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/pass/PassList.hs b/pass/PassList.hs index 202d6ad..4d3d2e8 100644 --- a/pass/PassList.hs +++ b/pass/PassList.hs @@ -50,6 +50,7 @@ commonPasses :: CompState -> [Pass] commonPasses opts = concat $ -- Rain does simplifyTypes separately: [ enablePassesWhen ((== FrontendOccam) . csFrontend) simplifyTypes + , [fixLowReplicators] , enablePassesWhen csUsageChecking [pass "Usage checking" Prop.agg_namesDone [Prop.parUsageChecked] (passOnlyOnAST "usageCheckPass" usageCheckPass)] diff --git a/transformations/SimplifyProcs.hs b/transformations/SimplifyProcs.hs index d7870f8..4e9df11 100644 --- a/transformations/SimplifyProcs.hs +++ b/transformations/SimplifyProcs.hs @@ -17,7 +17,7 @@ with this program. If not, see . -} -- | Simplify processes. -module SimplifyProcs (simplifyProcs) where +module SimplifyProcs (simplifyProcs, fixLowReplicators) where import Control.Monad.State import Data.Generics @@ -25,11 +25,14 @@ import qualified Data.Set as Set import qualified AST as A import CompState +import EvalConstants +import EvalLiterals import Metadata import Pass import qualified Properties as Prop import Traversal import Types +import Utils simplifyProcs :: [Pass] simplifyProcs = @@ -172,3 +175,40 @@ flattenAssign = pass "Flatten assignment" return (A.Spec m (A.Specification m n' proc)) + +-- | Removes replicators with a replicator count of zero, +-- and transforms replicators with a replicator count of one. +-- +-- We don't currently transform replicators in array constructors, +-- just replicators in SEQ, PAR, ALT, IF. +-- +-- This pass is primarily to make sure that PAR replicators with 0 or 1 counts +-- pass the usage checking, but it doesn't hurt to remove any redundant code (or +-- simplify code) in the other replicators. +fixLowReplicators :: Pass +fixLowReplicators = pass "Fix low-count (0, 1) replicators" [] [] + (applyDepthM doProcess) + where + doProcess :: Transform A.Process + doProcess (A.Seq m s) = doStructured s >>* A.Seq m + doProcess (A.Par m p s) = doStructured s >>* A.Par m p + doProcess (A.If m s) = doStructured s >>* A.If m + doProcess (A.Alt m p s) = doStructured s >>* A.Alt m p + doProcess p = return p + + doStructured :: Data a => Transform (A.Structured a) + doStructured s@(A.Only {}) = return s + doStructured (A.Several m ss) = mapM doStructured ss >>* A.Several m + doStructured (A.ProcThen m p s) = doStructured s >>* A.ProcThen m p + doStructured (A.Spec m sp@(A.Specification m' n (A.Rep m'' + (A.For m''' begin end _))) s) + | isConstant end + = do endVal <- evalIntExpression end + case endVal of + 0 -> return $ A.Several m [] + 1 -> doStructured s >>* + A.Spec m (A.Specification m' n + (A.IsExpr m'' A.ValAbbrev A.Int begin)) + _ -> doStructured s >>* A.Spec m sp + doStructured (A.Spec m sp s) = doStructured s >>* A.Spec m sp +