@@ -23,15 +23,13 @@ import Control.Monad.Reader
2323import Control.Monad.Writer hiding (Alt )
2424import Control.Monad.State
2525import Control.Monad.RWS hiding (Alt )
26+ import Data.Maybe
2627import Data.Foldable
2728import Data.String (IsString (.. ))
2829import Data.Text (Text )
2930import qualified Data.Text as T
3031import qualified Data.Text.IO as TIO
3132
32- import Data.Map (Map )
33- import qualified Data.Map as Map
34-
3533import Stg.Syntax
3634import Stg.IRLocation
3735
104102-}
105103
106104getStgPoint :: DocM StgPoint
107- getStgPoint = askEnv >>= \ case
105+ getStgPoint = (speStgPoint <$> askEnv) >>= \ case
108106 Nothing -> error " missing stg point"
109107 Just sp -> pure sp
110108---------------------------------------------------------
@@ -125,21 +123,25 @@ state0 = PState
125123 { curLine = []
126124 }
127125
128- {-
126+ data Config
127+ = Config
128+ { cfgPrintTickish :: Bool
129+ }
130+
129131data SPEnv
130132 = SPEnv
131- { speParent :: Maybe StgPoint
132- , speBinderName :: Maybe Name
133- , speScrutineeName :: Maybe Name
134- } Maybe StgPoint
135- -}
136- type SPEnv = Maybe StgPoint
133+ { speStgPoint :: Maybe StgPoint
134+ , speConfig :: Config
135+ }
137136
138137withStgPoint :: StgPoint -> Doc -> Doc
139- withStgPoint sp = localEnv (const $ Just sp)
138+ withStgPoint sp = localEnv (\ env -> env {speStgPoint = Just sp} )
140139
141- spEnv0 :: SPEnv
142- spEnv0 = Nothing
140+ spEnv0 :: Config -> SPEnv
141+ spEnv0 cfg = SPEnv
142+ { speStgPoint = Nothing
143+ , speConfig = cfg
144+ }
143145
144146
145147-- For plain text pretty printing
@@ -161,9 +163,9 @@ instance IsString (DocM ()) where
161163runDocM :: PEnv Int StgPoint () -> SPEnv -> PState Int () -> DocM a -> Maybe (PState Int () , POut Int StgPoint , a )
162164runDocM e spe s d = (\ (a,s',o) -> (s',o,a)) <$> runRWST (runEnvT spe $ unDocM d) e s
163165
164- execDoc :: Doc -> POut Int StgPoint
165- execDoc d =
166- let rM = runDocM env0 spEnv0 state0 d
166+ execDoc :: Config -> Doc -> POut Int StgPoint
167+ execDoc cfg d =
168+ let rM = runDocM env0 ( spEnv0 cfg) state0 d
167169 in case rM of
168170 Nothing -> PAtom $ AChunk $ CText " <internal pretty printing error>"
169171 Just (_, o, () ) -> o
@@ -319,10 +321,6 @@ instance Pretty AltType where
319321 PrimAlt r -> text " PrimAlt" <+> ppPrimRep r
320322 AlgAlt tc -> text " AlgAlt" <+> ppTyConName tc
321323
322- instance Pretty Binder where
323- pretty = pprBinder
324-
325-
326324pprAlt :: Id -> Int -> Alt -> Doc
327325pprAlt scrutId idx (Alt con bndrs rhs) =
328326 (hsep (pretty con : map (pprBinder) bndrs) <+> text " -> do" ) <$$>
@@ -388,6 +386,23 @@ putDefaultLast :: [Alt] -> [Doc] -> [Doc]
388386putDefaultLast (Alt AltDefault _ _ : _) (first : rest) = rest ++ [first]
389387putDefaultLast _ l = l
390388
389+ pprRealSrcSpan :: RealSrcSpan -> Doc
390+ pprRealSrcSpan RealSrcSpan' {.. } = pretty srcSpanFile <+> pprPos srcSpanSLine srcSpanSCol <> text " -" <> pprPos srcSpanELine srcSpanECol
391+ where pprPos line col = parens $ pretty line <> text " :" <> pretty col
392+
393+ instance Pretty RealSrcSpan where
394+ pretty = pprRealSrcSpan
395+
396+ pprTickish :: Tickish -> Doc
397+ pprTickish = \ case
398+ ProfNote -> text " -- ProfNote"
399+ HpcTick -> text " -- HpcTick"
400+ Breakpoint -> text " -- Breakpoint"
401+ SourceNote {.. } -> text " -- SourceNote for" <+> pretty sourceName <+> pretty sourceSpan
402+
403+ instance Pretty Tickish where
404+ pretty = pprTickish
405+
391406pprExpr :: Expr -> Doc
392407pprExpr exp = do
393408 stgPoint <- getStgPoint
@@ -419,7 +434,11 @@ pprExpr exp = do
419434 [ text " -- stack allocating let"
420435 , text " let" <+> (align $ pprBinding b) <$$> align (withStgPoint (SP_LetNoEscapeExpr stgPoint) $ pprExpr e)
421436 ]
422- StgTick tickish e -> pprExpr e
437+ StgTick tickish e -> do
438+ Config {.. } <- speConfig <$> askEnv
439+ if cfgPrintTickish
440+ then vsep [pretty tickish, pprExpr e]
441+ else pprExpr e
423442
424443instance Pretty Expr where
425444 pretty = pprExpr
@@ -428,10 +447,17 @@ addUnboxedCommentIfNecessary :: DataCon -> Doc -> Doc
428447addUnboxedCommentIfNecessary DataCon {.. } doc = case dcRep of
429448 UnboxedTupleCon {} -> doc -- vsep [text "-- stack allocated unboxed tuple", doc]
430449 _ -> doc
431-
450+ {-
451+ pprSrcSpan :: SrcSpan -> Doc
452+ pprSrcSpan = \case
453+ UnhelpfulSpan UnhelpfulNoLocationInfo -> mempty
454+ UnhelpfulSpan (UnhelpfulOther s) -> text "-- src-loc:" <+> pretty s
455+ UnhelpfulSpan sr -> text "-- src-loc:" <+> text (T.pack $ show sr)
456+ RealSrcSpan sp _ -> text "-- src-loc:" <+> pretty sp
457+ -}
432458pprRhs :: Id -> Rhs -> Doc
433459pprRhs rhsId@ (Id rhsBinder) = \ case
434- StgRhsClosure _ u bs e -> pprBinder rhsBinder <+> hsep (map pprBinder bs) <+> text " = do" <+ > (newline <> (indent 2 $ withStgPoint (SP_RhsClosureExpr rhsId) $ pprExpr e))
460+ StgRhsClosure _ u bs e -> pprBinder rhsBinder <+> hsep (map pprBinder bs) <+> text " = do" <> (newline <> (indent 2 $ withStgPoint (SP_RhsClosureExpr rhsId) $ pprExpr e))
435461 StgRhsCon dc vs -> annotate (SP_RhsCon rhsId) $ do
436462 pprBinder rhsBinder <+> text " =" <+> addUnboxedCommentIfNecessary dc (pprDataConName dc <+> (hsep $ map (pprArg) vs))
437463
@@ -481,6 +507,7 @@ instance Pretty DataCon where
481507pprModule :: Module -> Doc
482508pprModule Module {.. } = vsep
483509 [ text " -- package:" <+> pretty moduleUnitId
510+ , text " -- source-file-path:" <+> pretty (fromMaybe " <empty>" moduleSourceFilePath)
484511 , text " module" <+> pretty moduleName
485512 , indent 2 $ pprExportList moduleTopBindings
486513 , " ) where"
@@ -573,9 +600,12 @@ getPos :: M SrcPos
573600getPos = (,) <$> gets spsRow <*> gets spsCol
574601
575602pShow :: Doc -> (Text , [(StgPoint , SrcRange )])
576- pShow doc = (T. concat . reverse $ spsOutput result, spsStgPoints result)
603+ pShow = pShowWithConfig Config {cfgPrintTickish = False }
604+
605+ pShowWithConfig :: Config -> Doc -> (Text , [(StgPoint , SrcRange )])
606+ pShowWithConfig cfg doc = (T. concat . reverse $ spsOutput result, spsStgPoints result)
577607 where
578- result = execState (renderPOut $ execDoc doc) emptyStgPointState
608+ result = execState (renderPOut $ execDoc cfg doc) emptyStgPointState
579609
580610 renderChunk :: Chunk Int -> M ()
581611 renderChunk = \ case
0 commit comments