PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Gen2/StgAst.hs

https://bitbucket.org/bathtub/ghcjs
Haskell | 103 lines | 78 code | 15 blank | 10 comment | 0 complexity | 1aa16ebd9acac56ee3b62f49481d46ec MD5 | raw file
  1. {-
  2. some instances for printing the StgSyn AST in Haskell syntax.
  3. -}
  4. {-# LANGUAGE CPP #-}
  5. {-# LANGUAGE DeriveDataTypeable #-}
  6. {-# LANGUAGE FlexibleInstances #-}
  7. {-# LANGUAGE StandaloneDeriving #-}
  8. module Gen2.StgAst where
  9. import BasicTypes
  10. import CoreSyn
  11. import CostCentre
  12. import DataCon
  13. import DynFlags
  14. import ForeignCall
  15. import Id
  16. import Literal
  17. import Module
  18. import Outputable hiding ((<>))
  19. import PrimOp
  20. import StgSyn
  21. import Type
  22. import UniqFM
  23. import UniqSet
  24. import Control.Lens
  25. import qualified Data.Foldable as F
  26. import Data.Monoid
  27. import Data.Set (Set)
  28. import qualified Data.Set as S
  29. #if __GLASGOW_HASKELL__ >= 706
  30. showPpr' a = showPpr (defaultDynFlags undefined) a
  31. showSDoc' a = showSDoc (defaultDynFlags undefined) a
  32. #else
  33. showPpr' a = showPpr a
  34. showSDoc' a = showSDoc a
  35. #endif
  36. instance Show CostCentre where show _ = "CostCentre"
  37. instance Show CostCentreStack where show _ = "CostCentreStack"
  38. instance Show StgBinderInfo where show _ = "StgBinderInfo"
  39. instance Show Module where show = showPpr'
  40. instance Show (UniqFM a) where show _ = "UniqSet"
  41. instance Show Type where show = showPpr'
  42. instance Show AltType where show = showPpr'
  43. instance Show SRT where show _ = "SRT"
  44. instance Show PrimCall where show = showPpr'
  45. instance Show ForeignCall where show = showPpr'
  46. #if __GLASGOW_HASKELL__ >= 706
  47. instance Show DataCon where show = showPpr'
  48. instance Show Var where show = showPpr'
  49. #endif
  50. deriving instance Show UpdateFlag
  51. #if __GLASGOW_HASKELL__ >= 706
  52. deriving instance Show Literal
  53. deriving instance Show PrimOp
  54. deriving instance Show AltCon
  55. #endif
  56. deriving instance Show FunctionOrData
  57. deriving instance Show StgExpr
  58. deriving instance Show StgBinding
  59. deriving instance Show StgRhs
  60. deriving instance Show StgOp
  61. instance Show (GenStgArg Var) where
  62. show a@(StgVarArg occ) = "StgVarArg " ++ show occ ++ " :: " ++ show (stgArgType a)
  63. show (StgLitArg l) = "StgLitArg " ++ show l
  64. -- show (StgTypeArg t) = "StgTypeArg " ++ showPpr t
  65. s = S.singleton
  66. l = F.foldMap
  67. -- | collect Ids that this binding refers to
  68. -- (does not include the bindees themselves)
  69. bindingRefs :: StgBinding -> Set Id
  70. bindingRefs (StgNonRec _ rhs) = rhsRefs rhs
  71. bindingRefs (StgRec bs) = l (rhsRefs . snd) bs
  72. rhsRefs :: StgRhs -> Set Id
  73. rhsRefs (StgRhsClosure _ _ _ _ _ _ body) = exprRefs body
  74. rhsRefs (StgRhsCon _ d args) = l s (dataConImplicitIds d) <> l argRefs args
  75. exprRefs :: StgExpr -> Set Id
  76. exprRefs (StgApp f args) = s f <> l argRefs args
  77. exprRefs (StgConApp d args) = l s (dataConImplicitIds d) <> l argRefs args
  78. exprRefs (StgOpApp _ args _) = l argRefs args
  79. exprRefs (StgLit {}) = mempty
  80. exprRefs (StgLam {}) = mempty
  81. exprRefs (StgCase expr _ _ _ _ _ alts) = exprRefs expr <> alts^.folded._4.to exprRefs
  82. exprRefs (StgLet bnd expr) = bindingRefs bnd <> exprRefs expr
  83. exprRefs (StgLetNoEscape _ _ bnd expr) = bindingRefs bnd <> exprRefs expr
  84. exprRefs (StgSCC _ _ _ expr) = exprRefs expr
  85. exprRefs (StgTick _ _ expr) = exprRefs expr
  86. argRefs :: StgArg -> Set Id
  87. argRefs (StgVarArg id) = s id
  88. argRefs _ = mempty