1 ---------------------------------------------------------
2 -- The main program for the hpc-add tool, part of HPC.
4 ---------------------------------------------------------
6 module HpcCombine (combine_plugin) where
14 import qualified HpcSet as Set
15 import qualified HpcMap as Map
17 ------------------------------------------------------------------------------
19 [ excludeOpt,includeOpt,outputOpt,combineFunOpt, combineFunOptInfo, postInvertOpt ]
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
30 ------------------------------------------------------------------------------
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).
36 let f = case combineFun flags of
38 SUB -> \ l r -> max 0 (l - r)
39 DIFF -> \ g b -> if g > 0 then 0 else min 1 b
42 Just tix <- readTix first_file
44 tix' <- foldM (mergeTixFile flags f)
48 let (Tix inside_tix') = tix'
51 let tix'' = if postInvert flags
52 then Tix [ TixModule m p i (map inv t)
53 | TixModule m p i t <- inside_tix'
57 case outputFile flags of
58 "-" -> putStrLn (show tix'')
59 out -> writeTix out tix''
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)
66 -- could allow different numbering on the module info,
67 -- as long as the total is the same; will require normalization.
69 mergeTix :: (Integer -> Integer -> Integer) -> Tix -> Tix -> 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))
77 || length tix1 /= length tix2
78 || len1 /= length tix1
79 || len2 /= length tix2
80 -> error $ "mismatched in module " ++ m
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)
91 m1s = Set.fromList $ map tixModuleName t1
92 m2s = Set.fromList $ map tixModuleName t2
94 fm1 = Map.fromList [ (tixModuleName tix,tix)
97 fm2 = Map.fromList [ (tixModuleName tix,tix)
102 -- What I would give for a hyperstrict :-)
103 -- This makes things about 100 times faster.
107 instance Strict Integer where
110 instance Strict Int where
113 instance Strict Hash where -- should be fine, because Hash is a newtype round an Int
116 instance Strict Char where
119 instance Strict a => Strict [a] where
120 strict (a:as) = (((:) $! strict a) $! strict as)
123 instance (Strict a, Strict b) => Strict (a,b) where
124 strict (a,b) = (((,) $! strict a) $! strict b)
126 instance Strict Tix where
130 instance Strict TixModule where
131 strict (TixModule m1 p1 i1 t1) =
132 ((((TixModule $! strict m1) $! strict p1) $! strict i1) $! strict t1)