Warning Police: Unused imports
[ghc-hetmet.git] / utils / hpc / HpcCombine.hs
1 ---------------------------------------------------------
2 -- The main program for the hpc-add tool, part of HPC.
3 -- Andy Gill, Oct 2006
4 ---------------------------------------------------------
5
6 module HpcCombine (combine_plugin) where 
7
8 import Trace.Hpc.Tix
9 import Trace.Hpc.Util
10
11 import HpcFlags
12
13 import Control.Monad
14 import qualified HpcSet as Set
15 import qualified HpcMap as Map
16
17 ------------------------------------------------------------------------------
18 combine_options = 
19   [ excludeOpt,includeOpt,outputOpt,combineFunOpt, combineFunOptInfo, postInvertOpt ]
20          
21 combine_plugin = Plugin { name = "combine"
22                        , usage = "[OPTION] .. <TIX_FILE> [<TIX_FILE> [<TIX_FILE> ..]]" 
23                        , options = combine_options 
24                        , summary = "Combine multiple .tix files in a single .tix files"
25                        , implementation = combine_main
26                        , init_flags = default_flags
27                        , final_flags = default_final_flags
28                        }
29
30 ------------------------------------------------------------------------------
31
32 combine_main :: Flags -> [String] -> IO ()
33 combine_main flags (first_file:more_files) = do
34   -- combine does not expand out the .tix filenames (by design).
35
36   let f = case combineFun flags of
37             ADD  -> \ l r -> l + r
38             SUB  -> \ l r -> max 0 (l - r)
39             DIFF -> \ g b -> if g > 0 then 0 else min 1 b
40             ZERO -> \ _ _ -> 0
41
42   Just tix <- readTix first_file
43
44   tix' <- foldM (mergeTixFile flags f) 
45                 (filterTix flags tix)
46                 more_files
47
48   let (Tix inside_tix') = tix'
49   let inv 0 = 1
50       inv n = 0
51   let tix'' = if postInvert flags
52               then Tix [ TixModule m p i (map inv t)
53                        | TixModule m p i t <- inside_tix'
54                        ]
55               else tix'
56
57   case outputFile flags of
58     "-" -> putStrLn (show tix'')
59     out -> writeTix out tix''
60
61 mergeTixFile :: Flags -> (Integer -> Integer -> Integer) -> Tix -> String -> IO Tix
62 mergeTixFile flags fn tix file_name = do
63   Just new_tix <- readTix file_name
64   return $! strict $ mergeTix fn tix (filterTix flags new_tix)
65
66 -- could allow different numbering on the module info, 
67 -- as long as the total is the same; will require normalization.
68
69 mergeTix :: (Integer -> Integer -> Integer) -> Tix -> Tix -> Tix 
70 mergeTix f
71          (Tix t1)
72          (Tix t2)  = Tix 
73          [ case (Map.lookup m fm1,Map.lookup m fm2) of
74            -- todo, revisit the semantics of this combination
75             (Just (TixModule _ hash1 len1 tix1),Just (TixModule _ hash2 len2 tix2)) 
76                | hash1 /= hash2 
77                || length tix1 /= length tix2
78                || len1 /= length tix1
79                || len2 /= length tix2
80                      -> error $ "mismatched in module " ++ m
81                | otherwise      -> 
82                      TixModule m hash1 len1 (zipWith f tix1 tix2) 
83             (Just (TixModule _ hash1 len1 tix1),Nothing) -> 
84                   error $ "rogue module " ++ show m
85             (Nothing,Just (TixModule _ hash2 len2 tix2)) -> 
86                   error $ "rogue module " ++ show m
87             _ -> error "impossible"
88          | m <- Set.toList (m1s `Set.intersection` m2s)
89          ]
90   where 
91    m1s = Set.fromList $ map tixModuleName t1 
92    m2s = Set.fromList $ map tixModuleName t2
93
94    fm1 = Map.fromList [ (tixModuleName tix,tix) 
95                       | tix <- t1
96                       ]
97    fm2 = Map.fromList [ (tixModuleName tix,tix) 
98                       | tix <- t2
99                       ]
100
101
102 -- What I would give for a hyperstrict :-)
103 -- This makes things about 100 times faster.
104 class Strict a where
105    strict :: a -> a
106
107 instance Strict Integer where
108    strict i = i
109
110 instance Strict Int where
111    strict i = i
112
113 instance Strict Hash where      -- should be fine, because Hash is a newtype round an Int
114    strict i = i
115
116 instance Strict Char where
117    strict i = i
118
119 instance Strict a => Strict [a] where
120    strict (a:as) = (((:) $! strict a) $! strict as)
121    strict []     = []
122
123 instance (Strict a, Strict b) => Strict (a,b) where
124    strict (a,b) = (((,) $! strict a) $! strict b)
125
126 instance Strict Tix where
127   strict (Tix t1) = 
128             Tix $! strict t1
129
130 instance Strict TixModule where
131   strict (TixModule m1 p1 i1 t1) = 
132             ((((TixModule $! strict m1) $! strict p1) $! strict i1) $! strict t1)
133