PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/src/Gen2/StgAst.hs

http://github.com/ghcjs/ghcjs
Haskell | 155 lines | 122 code | 17 blank | 16 comment | 2 complexity | d2a389bde3c6b4402fbf13c933998647 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  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 Data.Char (isSpace)
  10. import qualified Data.Foldable as F
  11. import Data.Set (Set)
  12. import qualified Data.Set as S
  13. import DataCon
  14. import DynFlags
  15. import BasicTypes
  16. import Control.Lens
  17. import CoreSyn
  18. import CostCentre
  19. import ForeignCall
  20. import Id
  21. import Literal
  22. import Module
  23. import Name
  24. import Outputable hiding ((<>))
  25. import PrimOp
  26. import StgSyn
  27. import TyCon
  28. import Type
  29. import Unique
  30. import UniqFM
  31. import IdInfo
  32. import qualified Var
  33. import Coercion
  34. import CoAxiom
  35. import Gen2.Utils
  36. -- this is a hack to be able to use pprShow in a Show instance, should be removed
  37. {-# NOINLINE hackPprDflags #-}
  38. hackPprDflags :: DynFlags
  39. hackPprDflags = unsafeGlobalDynFlags
  40. -- | replace all whitespace with space
  41. fixSpace :: String -> String
  42. fixSpace xs = map f xs
  43. where
  44. f c | isSpace c = ' '
  45. | otherwise = c
  46. -- fixme make this more informative
  47. instance Show Type where
  48. show ty = fixSpace (showPpr hackPprDflags ty)
  49. instance Show CostCentre where show _ = "CostCentre"
  50. instance Show CostCentreStack where show _ = "CostCentreStack"
  51. instance Show StgBinderInfo where show _ = "StgBinderInfo"
  52. instance Show Module where show m = unitIdString (moduleUnitId m) ++ ":" ++ moduleNameString (moduleName m)
  53. -- instance Show (UniqFM Id) where show u = "[" ++ show (uniqSetToList u) ++ "]"
  54. instance Show TyCon where show = show . tyConName
  55. instance Show Name where
  56. show n = case nameModule_maybe n of
  57. Nothing -> show (nameOccName n)
  58. Just m -> show m ++ "." ++ show (nameOccName n)
  59. instance Show OccName where show = occNameString
  60. instance Show DataCon where show d = show (dataConName d)
  61. instance Show Var where show v = "(" ++ show (Var.varName v) ++ "[" ++
  62. encodeUnique (getKey (getUnique v)) ++
  63. "]" ++ if isGlobalId v then "G" else "L" ++
  64. " <" ++ show (idDetails v) ++ "> :: " ++
  65. show (Var.varType v) ++ ")"
  66. instance Show IdDetails where
  67. show VanillaId = "VanillaId"
  68. show (RecSelId {}) = "RecSelId"
  69. show (DataConWorkId dc) = "DataConWorkId " ++ show dc
  70. show (DataConWrapId dc) = "DataConWrapId " ++ show dc
  71. show (ClassOpId {}) = "ClassOpId"
  72. show (PrimOpId {}) = "PrimOpId"
  73. show (FCallId {}) = "FCallId"
  74. show (TickBoxOpId {}) = "VanillaId"
  75. show (DFunId {}) = "DFunId"
  76. show CoVarId = "CoVarId"
  77. show (JoinId {}) = "JoinId"
  78. deriving instance Show UpdateFlag
  79. deriving instance Show PrimOpVecCat
  80. deriving instance Show LitNumType
  81. deriving instance Show Literal
  82. deriving instance Show PrimOp
  83. deriving instance Show AltCon
  84. deriving instance Show AltType
  85. deriving instance Show PrimCall
  86. deriving instance Show ForeignCall
  87. deriving instance Show CCallTarget
  88. deriving instance Show CCallSpec
  89. deriving instance Show CCallConv
  90. deriving instance Show FunctionOrData
  91. deriving instance Show StgExpr
  92. deriving instance Show StgBinding
  93. deriving instance Show StgTopBinding
  94. deriving instance Show StgRhs
  95. deriving instance Show StgOp
  96. deriving instance Show a => Show (Tickish a)
  97. --
  98. instance Show Coercion where show co = showPpr hackPprDflags co
  99. deriving instance Show a => Show (Expr a)
  100. deriving instance Show a => Show (Bind a)
  101. instance Show CoAxiomRule where show _ = "CoAxiomRule"
  102. instance Show (CoAxiom a) where show _ = "CoAxiom"
  103. deriving instance Show LeftOrRight
  104. deriving instance Show Role
  105. instance Show (GenStgArg Var) where
  106. show a@(StgVarArg occ) = "StgVarArg " ++ show occ ++ " :: " ++ show (stgArgType a)
  107. show (StgLitArg l) = "StgLitArg " ++ show l
  108. deriving instance Show UnfoldingGuidance
  109. deriving instance Show UnfoldingSource
  110. deriving instance Show Unfolding
  111. s :: a -> Set a
  112. s = S.singleton
  113. l :: (a -> Set Id) -> [a] -> Set Id
  114. l = F.foldMap
  115. -- | collect Ids that this binding refers to
  116. -- (does not include the bindees themselves)
  117. -- first argument is Id -> StgExpr map for unfloated arguments
  118. bindingRefs :: UniqFM StgExpr -> StgBinding -> Set Id
  119. bindingRefs u (StgNonRec _ rhs) = rhsRefs u rhs
  120. bindingRefs u (StgRec bs) = l (rhsRefs u . snd) bs
  121. rhsRefs :: UniqFM StgExpr -> StgRhs -> Set Id
  122. rhsRefs u (StgRhsClosure _ _ _ _ _ body) = exprRefs u body
  123. rhsRefs u (StgRhsCon _ d args) = l s [ i | AnId i <- dataConImplicitTyThings d] <> l (argRefs u) args
  124. exprRefs :: UniqFM StgExpr -> StgExpr -> Set Id
  125. exprRefs u (StgApp f args) = s f <> l (argRefs u) args
  126. exprRefs u (StgConApp d args _) = l s [ i | AnId i <- dataConImplicitTyThings d] <> l (argRefs u) args
  127. exprRefs u (StgOpApp _ args _) = l (argRefs u) args
  128. exprRefs _ (StgLit {}) = mempty
  129. exprRefs _ (StgLam {}) = mempty
  130. exprRefs u (StgCase expr _ _ alts) = exprRefs u expr <> alts^.folded._3.to (exprRefs u)
  131. exprRefs u (StgLet bnd expr) = bindingRefs u bnd <> exprRefs u expr
  132. exprRefs u (StgLetNoEscape bnd expr) = bindingRefs u bnd <> exprRefs u expr
  133. exprRefs u (StgTick _ expr) = exprRefs u expr
  134. argRefs :: UniqFM StgExpr -> StgArg -> Set Id
  135. argRefs u (StgVarArg id)
  136. | Just e <- lookupUFM u id = exprRefs u e
  137. | otherwise = s id
  138. argRefs _ _ = mempty