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
16 import System.Environment
18 ------------------------------------------------------------------------------
27 combine_plugin = Plugin { name = "combine"
28 , usage = "[OPTION] .. <TIX_FILE> [<TIX_FILE> [<TIX_FILE> ..]]"
29 , options = combine_options
30 , summary = "Combine multiple .tix files in a single .tix files"
31 , implementation = combine_main
32 , init_flags = default_flags
33 , final_flags = default_final_flags
36 ------------------------------------------------------------------------------
38 combine_main :: Flags -> [String] -> IO ()
39 combine_main flags (first_file:more_files) = do
40 -- combine does not expand out the .tix filenames (by design).
42 let f = case combineFun flags of
44 SUB -> \ l r -> max 0 (l - r)
45 DIFF -> \ g b -> if g > 0 then 0 else min 1 b
48 Just tix <- readTix first_file
50 tix' <- foldM (mergeTixFile flags f)
54 let (Tix inside_tix') = tix'
57 let tix'' = if postInvert flags
58 then Tix [ TixModule m p i (map inv t)
59 | TixModule m p i t <- inside_tix'
63 case outputFile flags of
64 "-" -> putStrLn (show tix'')
65 out -> writeTix out tix''
67 mergeTixFile :: Flags -> (Integer -> Integer -> Integer) -> Tix -> String -> IO Tix
68 mergeTixFile flags fn tix file_name = do
69 Just new_tix <- readTix file_name
70 return $! strict $ mergeTix fn tix (filterTix flags new_tix)
72 -- could allow different numbering on the module info,
73 -- as long as the total is the same; will require normalization.
75 mergeTix :: (Integer -> Integer -> Integer) -> Tix -> Tix -> Tix
79 [ case (Map.lookup m fm1,Map.lookup m fm2) of
80 -- todo, revisit the semantics of this combination
81 (Just (TixModule _ hash1 len1 tix1),Just (TixModule _ hash2 len2 tix2))
83 || length tix1 /= length tix2
84 || len1 /= length tix1
85 || len2 /= length tix2
86 -> error $ "mismatched in module " ++ m
88 TixModule m hash1 len1 (zipWith f tix1 tix2)
89 (Just (TixModule _ hash1 len1 tix1),Nothing) ->
90 error $ "rogue module " ++ show m
91 (Nothing,Just (TixModule _ hash2 len2 tix2)) ->
92 error $ "rogue module " ++ show m
93 _ -> error "impossible"
94 | m <- Set.toList (m1s `Set.intersection` m2s)
97 m1s = Set.fromList $ map tixModuleName t1
98 m2s = Set.fromList $ map tixModuleName t2
100 fm1 = Map.fromList [ (tixModuleName tix,tix)
103 fm2 = Map.fromList [ (tixModuleName tix,tix)
108 -- What I would give for a hyperstrict :-)
109 -- This makes things about 100 times faster.
113 instance Strict Integer where
116 instance Strict Int where
119 instance Strict Hash where -- should be fine, because Hash is a newtype round an Int
122 instance Strict Char where
125 instance Strict a => Strict [a] where
126 strict (a:as) = (((:) $! strict a) $! strict as)
129 instance (Strict a, Strict b) => Strict (a,b) where
130 strict (a,b) = (((,) $! strict a) $! strict b)
132 instance Strict Tix where
136 instance Strict TixModule where
137 strict (TixModule m1 p1 i1 t1) =
138 ((((TixModule $! strict m1) $! strict p1) $! strict i1) $! strict t1)