Name signals in a function after flattening it.
[matthijs/master-project/cλash.git] / Pretty.hs
1 module Pretty (prettyShow) where
2
3 import qualified Data.Map as Map
4 import qualified CoreSyn
5 import qualified Module
6 import qualified HscTypes
7 import Text.PrettyPrint.HughesPJClass
8 import Outputable ( showSDoc, ppr, Outputable, OutputableBndr)
9
10 import HsValueMap
11 import FlattenTypes
12 import TranslatorTypes
13
14 instance Pretty HsFunction where
15   pPrint (HsFunction name args res) =
16     text name <> char ' ' <> parens (hsep $ punctuate comma args') <> text " -> " <> res'
17     where
18       args' = map pPrint args
19       res'  = pPrint res
20
21 instance Pretty x => Pretty (HsValueMap x) where
22   pPrint (Tuple maps) = parens (hsep $ punctuate comma (map pPrint maps))
23   pPrint (Single s)   = pPrint s
24
25 instance Pretty HsValueUse where
26   pPrint Port            = char 'P'
27   pPrint (State n)       = char 'C' <> int n
28   pPrint (HighOrder _ _) = text "Higher Order"
29
30 instance Pretty id => Pretty (FlatFunction' id) where
31   pPrint (FlatFunction args res apps conds sigs) =
32     (text "Args: ") $$ nest 10 (pPrint args)
33     $+$ (text "Result: ") $$ nest 10 (pPrint res)
34     $+$ (text "Apps: ") $$ nest 10 (vcat (map pPrint apps))
35     $+$ (text "Conds: ") $$ nest 10 (pPrint conds)
36     $+$ text "Signals: " $$ nest 10 (pPrint sigs)
37
38 instance Pretty id => Pretty (FApp id) where
39   pPrint (FApp func args res) =
40     pPrint func <> text " : " <> pPrint args <> text " -> " <> pPrint res
41
42 instance Pretty id => Pretty (CondDef id) where
43   pPrint _ = text "TODO"
44
45 instance Pretty id => Pretty (Signal id) where
46   pPrint (Signal id Nothing) = pPrint id
47   pPrint (Signal id (Just name)) = pPrint id <> text ":" <> text name
48
49 instance Pretty VHDLSession where
50   pPrint (VHDLSession mod nameCount funcs) =
51     text "Module: " $$ nest 15 (text modname)
52     $+$ text "NameCount: " $$ nest 15 (int nameCount)
53     $+$ text "Functions: " $$ nest 15 (vcat (map ppfunc (Map.toList funcs)))
54     where
55       ppfunc (hsfunc, (FuncData flatfunc)) =
56         pPrint hsfunc $+$ (text "Flattened: " $$ nest 15 (ppffunc flatfunc))
57       ppffunc (Just f) = pPrint f
58       ppffunc Nothing  = text "Nothing"
59       modname = showSDoc $ Module.pprModule (HscTypes.cm_module mod)
60
61 instance (OutputableBndr b) => Pretty (CoreSyn.Bind b) where
62   pPrint (CoreSyn.NonRec b expr) =
63     text "NonRec: " $$ nest 10 (prettyBind (b, expr))
64   pPrint (CoreSyn.Rec binds) =
65     text "Rec: " $$ nest 10 (vcat $ map (prettyBind) binds)
66
67 prettyBind :: (Outputable b, Outputable e) => (b, e) -> Doc
68 prettyBind (b, expr) =
69   text b' <> text " = " <> text expr'
70   where
71     b' = showSDoc $ ppr b
72     expr' = showSDoc $ ppr expr