Mark all signals as ports or states when appropriate.
[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 qualified ForSyDe.Backend.Ppr
11 import qualified ForSyDe.Backend.VHDL.AST as AST
12
13 import HsValueMap
14 import FlattenTypes
15 import TranslatorTypes
16 import VHDLTypes
17
18 -- | A version of the default pPrintList method, which uses a custom function
19 --   f instead of pPrint to print elements.
20 printList :: (a -> Doc) -> [a] -> Doc
21 printList f = brackets . fsep . punctuate comma . map f
22
23 instance Pretty HsFunction where
24   pPrint (HsFunction name args res) =
25     text name <> char ' ' <> parens (hsep $ punctuate comma args') <> text " -> " <> res'
26     where
27       args' = map pPrint args
28       res'  = pPrint res
29
30 instance Pretty x => Pretty (HsValueMap x) where
31   pPrint (Tuple maps) = parens (hsep $ punctuate comma (map pPrint maps))
32   pPrint (Single s)   = pPrint s
33
34 instance Pretty HsValueUse where
35   pPrint Port            = char 'P'
36   pPrint (State n)       = char 'C' <> int n
37   pPrint (HighOrder _ _) = text "Higher Order"
38
39 instance Pretty id => Pretty (FlatFunction' id) where
40   pPrint (FlatFunction args res apps conds sigs) =
41     (text "Args: ") $$ nest 10 (pPrint args)
42     $+$ (text "Result: ") $$ nest 10 (pPrint res)
43     $+$ (text "Apps: ") $$ nest 10 (vcat (map pPrint apps))
44     $+$ (text "Conds: ") $$ nest 10 (pPrint conds)
45     $+$ text "Signals: " $$ nest 10 (printList ppsig sigs)
46     where
47       ppsig (id, info) = pPrint id <> pPrint info
48
49 instance Pretty id => Pretty (FApp id) where
50   pPrint (FApp func args res) =
51     pPrint func <> text " : " <> pPrint args <> text " -> " <> pPrint res
52
53 instance Pretty id => Pretty (CondDef id) where
54   pPrint _ = text "TODO"
55
56 instance Pretty SignalInfo where
57   pPrint (SignalInfo name use ty) =
58     text ":" <> (pPrint use) <> (ppname name)
59     where
60       ppname Nothing = empty
61       ppname (Just name) = text ":" <> text name
62
63 instance Pretty SigUse where
64   pPrint SigPortIn   = text "PI"
65   pPrint SigPortOut  = text "PO"
66   pPrint SigInternal = text "I"
67   pPrint (SigStateOld n) = text "SO:" <> int n
68   pPrint (SigStateNew n) = text "SN:" <> int n
69   pPrint SigSubState = text "s"
70
71 instance Pretty VHDLSession where
72   pPrint (VHDLSession mod nameCount funcs) =
73     text "Module: " $$ nest 15 (text modname)
74     $+$ text "NameCount: " $$ nest 15 (int nameCount)
75     $+$ text "Functions: " $$ nest 15 (vcat (map ppfunc (Map.toList funcs)))
76     where
77       ppfunc (hsfunc, fdata) =
78         pPrint hsfunc $+$ nest 5 (pPrint fdata)
79       modname = showSDoc $ Module.pprModule (HscTypes.cm_module mod)
80
81 instance Pretty FuncData where
82   pPrint (FuncData flatfunc entity arch) =
83     text "Flattened: " $$ nest 15 (ppffunc flatfunc)
84     $+$ text "Entity" $$ nest 15 (ppent entity)
85     $+$ pparch arch
86     where
87       ppffunc (Just f) = pPrint f
88       ppffunc Nothing  = text "Nothing"
89       ppent (Just e)   = pPrint e
90       ppent Nothing    = text "Nothing"
91       pparch Nothing = text "VHDL architecture not present"
92       pparch (Just _) = text "VHDL architecture present"
93
94 instance Pretty Entity where
95   pPrint (Entity id args res decl) =
96     text "Entity: " $$ nest 10 (pPrint id)
97     $+$ text "Args: " $$ nest 10 (pPrint args)
98     $+$ text "Result: " $$ nest 10 (pPrint res)
99     $+$ ppdecl decl
100     where
101       ppdecl Nothing = text "VHDL entity not present"
102       ppdecl (Just _) = text "VHDL entity present"
103
104 instance (OutputableBndr b) => Pretty (CoreSyn.Bind b) where
105   pPrint (CoreSyn.NonRec b expr) =
106     text "NonRec: " $$ nest 10 (prettyBind (b, expr))
107   pPrint (CoreSyn.Rec binds) =
108     text "Rec: " $$ nest 10 (vcat $ map (prettyBind) binds)
109
110 instance Pretty AST.VHDLId where
111   pPrint id = ForSyDe.Backend.Ppr.ppr id
112
113 prettyBind :: (Outputable b, Outputable e) => (b, e) -> Doc
114 prettyBind (b, expr) =
115   text b' <> text " = " <> text expr'
116   where
117     b' = showSDoc $ ppr b
118     expr' = showSDoc $ ppr expr