{-# LANGUAGE ConstraintKinds      #-}
{-# LANGUAGE DataKinds            #-}
{-# LANGUAGE DeriveDataTypeable   #-}
{-# LANGUAGE FlexibleContexts     #-}
{-# LANGUAGE LambdaCase           #-}
{-# LANGUAGE TypeFamilies         #-}
{-# LANGUAGE NamedFieldPuns       #-}
{-# LANGUAGE UndecidableInstances #-}

{-
(c) The GRASP/AQUA Project, Glasgow University, 1992-1998

Shared term graph (STG) syntax for spineless-tagless code generation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This data type represents programs just before code generation (conversion to
@Cmm@): basically, what we have is a stylised form of Core syntax, the style
being one that happens to be ideally suited to spineless tagless code
generation.
-}

module GHC.Stg.Syntax (
        StgArg(..),

        GenStgTopBinding(..), GenStgBinding(..), GenStgExpr(..), GenStgRhs(..),
        GenStgAlt(..), AltType(..),

        StgPass(..), BinderP, XRhsClosure, XLet, XLetNoEscape,
        NoExtFieldSilent, noExtFieldSilent,
        OutputablePass,

        UpdateFlag(..), isUpdatable,

        ConstructorNumber(..),

        -- a set of synonyms for the vanilla parameterisation
        StgTopBinding, StgBinding, StgExpr, StgRhs, StgAlt,

        -- a set of synonyms for the code gen parameterisation
        CgStgTopBinding, CgStgBinding, CgStgExpr, CgStgRhs, CgStgAlt,

        -- Same for taggedness
        TgStgTopBinding, TgStgBinding, TgStgExpr, TgStgRhs, TgStgAlt,

        -- a set of synonyms for the lambda lifting parameterisation
        LlStgTopBinding, LlStgBinding, LlStgExpr, LlStgRhs, LlStgAlt,

        -- a set of synonyms to distinguish in- and out variants
        InStgArg,  InStgTopBinding,  InStgBinding,  InStgExpr,  InStgRhs,  InStgAlt,
        OutStgArg, OutStgTopBinding, OutStgBinding, OutStgExpr, OutStgRhs, OutStgAlt,

        -- StgOp
        StgOp(..),

        -- utils
        stgRhsArity, freeVarsOfRhs,
        isDllConApp,
        stgArgType,
        stgCaseBndrInScope,

        -- ppr
        StgPprOpts(..),
        panicStgPprOpts, shortStgPprOpts,
        pprStgArg, pprStgExpr, pprStgRhs, pprStgBinding, pprStgAlt,
        pprGenStgTopBinding, pprStgTopBinding,
        pprGenStgTopBindings, pprStgTopBindings
    ) where

import GHC.Prelude

import GHC.Core     ( AltCon )
import GHC.Types.CostCentre ( CostCentreStack )
import Data.ByteString ( ByteString )
import Data.Data   ( Data )
import Data.List   ( intersperse )
import GHC.Core.DataCon
import GHC.Types.ForeignCall ( ForeignCall )
import GHC.Types.Id
import GHC.Types.Name        ( isDynLinkName )
import GHC.Types.Tickish     ( StgTickish )
import GHC.Types.Var.Set
import GHC.Types.Literal     ( Literal, literalType )
import GHC.Unit.Module       ( Module )
import GHC.Utils.Outputable
import GHC.Platform
import GHC.Core.Ppr( {- instances -} )
import GHC.Builtin.PrimOps ( PrimOp, PrimCall )
import GHC.Core.TyCon    ( PrimRep(..), TyCon )
import GHC.Core.Type     ( Type )
import GHC.Types.RepType ( typePrimRep1, typePrimRep )
import GHC.Utils.Panic.Plain

{-
************************************************************************
*                                                                      *
GenStgBinding
*                                                                      *
************************************************************************

As usual, expressions are interesting; other things are boring. Here are the
boring things (except note the @GenStgRhs@), parameterised with respect to
binder and occurrence information (just as in @GHC.Core@):
-}

-- | A top-level binding.
data GenStgTopBinding pass
-- See Note [Core top-level string literals]
  = StgTopLifted (GenStgBinding pass)
  | StgTopStringLit Id ByteString

data GenStgBinding pass
  = StgNonRec (BinderP pass) (GenStgRhs pass)
  | StgRec    [(BinderP pass, GenStgRhs pass)]

{-
************************************************************************
*                                                                      *
StgArg
*                                                                      *
************************************************************************
-}

data StgArg
  = StgVarArg  Id
  | StgLitArg  Literal

-- | Does this constructor application refer to anything in a different
-- *Windows* DLL?
-- If so, we can't allocate it statically
isDllConApp
  :: Platform
  -> Bool          -- is Opt_ExternalDynamicRefs enabled?
  -> Module
  -> DataCon
  -> [StgArg]
  -> Bool
isDllConApp :: Platform -> Bool -> Module -> DataCon -> [StgArg] -> Bool
isDllConApp Platform
platform Bool
ext_dyn_refs Module
this_mod DataCon
con [StgArg]
args
 | Bool -> Bool
not Bool
ext_dyn_refs    = Bool
False
 | Platform -> OS
platformOS Platform
platform OS -> OS -> Bool
forall a. Eq a => a -> a -> Bool
== OS
OSMinGW32
    = Platform -> Module -> Name -> Bool
isDynLinkName Platform
platform Module
this_mod (DataCon -> Name
dataConName DataCon
con) Bool -> Bool -> Bool
|| (StgArg -> Bool) -> [StgArg] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any StgArg -> Bool
is_dll_arg [StgArg]
args
 | Bool
otherwise = Bool
False
  where
    -- NB: typePrimRep1 is legit because any free variables won't have
    -- unlifted type (there are no unlifted things at top level)
    is_dll_arg :: StgArg -> Bool
    is_dll_arg :: StgArg -> Bool
is_dll_arg (StgVarArg Id
v) =  PrimRep -> Bool
isAddrRep ((() :: Constraint) => UnaryType -> PrimRep
UnaryType -> PrimRep
typePrimRep1 (Id -> UnaryType
idType Id
v))
                             Bool -> Bool -> Bool
&& Platform -> Module -> Name -> Bool
isDynLinkName Platform
platform Module
this_mod (Id -> Name
idName Id
v)
    is_dll_arg StgArg
_             = Bool
False

-- True of machine addresses; these are the things that don't work across DLLs.
-- The key point here is that VoidRep comes out False, so that a top level
-- nullary GADT constructor is False for isDllConApp
--
--    data T a where
--      T1 :: T Int
--
-- gives
--
--    T1 :: forall a. (a~Int) -> T a
--
-- and hence the top-level binding
--
--    $WT1 :: T Int
--    $WT1 = T1 Int (Coercion (Refl Int))
--
-- The coercion argument here gets VoidRep
isAddrRep :: PrimRep -> Bool
isAddrRep :: PrimRep -> Bool
isAddrRep PrimRep
AddrRep     = Bool
True
isAddrRep PrimRep
LiftedRep   = Bool
True
isAddrRep PrimRep
UnliftedRep = Bool
True
isAddrRep PrimRep
_           = Bool
False

-- | Type of an @StgArg@
--
-- Very half baked because we have lost the type arguments.
stgArgType :: StgArg -> Type
stgArgType :: StgArg -> UnaryType
stgArgType (StgVarArg Id
v)   = Id -> UnaryType
idType Id
v
stgArgType (StgLitArg Literal
lit) = Literal -> UnaryType
literalType Literal
lit

-- | Given an alt type and whether the program is unarised, return whether the
-- case binder is in scope.
--
-- Case binders of unboxed tuple or unboxed sum type always dead after the
-- unariser has run. See Note [Post-unarisation invariants].
stgCaseBndrInScope :: AltType -> Bool {- ^ unarised? -} -> Bool
stgCaseBndrInScope :: AltType -> Bool -> Bool
stgCaseBndrInScope AltType
alt_ty Bool
unarised =
    case AltType
alt_ty of
      AlgAlt TyCon
_      -> Bool
True
      PrimAlt PrimRep
_     -> Bool
True
      MultiValAlt Int
_ -> Bool -> Bool
not Bool
unarised
      AltType
PolyAlt       -> Bool
True

{-
************************************************************************
*                                                                      *
STG expressions
*                                                                      *
************************************************************************

The @GenStgExpr@ data type is parameterised on binder and occurrence info, as
before.

************************************************************************
*                                                                      *
GenStgExpr
*                                                                      *
************************************************************************

An application is of a function to a list of atoms (not expressions).
Operationally, we want to push the arguments on the stack and call the function.
(If the arguments were expressions, we would have to build their closures
first.)

There is no constructor for a lone variable; it would appear as @StgApp var []@.
-}

data GenStgExpr pass
  = StgApp
        Id       -- function
        [StgArg] -- arguments; may be empty

{-
************************************************************************
*                                                                      *
StgConApp and StgPrimApp --- saturated applications
*                                                                      *
************************************************************************

There are specialised forms of application, for constructors, primitives, and
literals.
-}

  | StgLit      Literal

        -- StgConApp is vital for returning unboxed tuples or sums
        -- which can't be let-bound
  | StgConApp   DataCon
                ConstructorNumber
                [StgArg] -- Saturated. (After Unarisation, [NonVoid StgArg])
                [Type]   -- See Note [Types in StgConApp] in GHC.Stg.Unarise

  | StgOpApp    StgOp    -- Primitive op or foreign call
                [StgArg] -- Saturated.
                Type     -- Result type
                         -- We need to know this so that we can
                         -- assign result registers

{-
************************************************************************
*                                                                      *
GenStgExpr: case-expressions
*                                                                      *
************************************************************************

This has the same boxed/unboxed business as Core case expressions.
-}

  | StgCase
        (GenStgExpr pass) -- the thing to examine
        (BinderP pass) -- binds the result of evaluating the scrutinee
        AltType
        [GenStgAlt pass]
                    -- The DEFAULT case is always *first*
                    -- if it is there at all

{-
************************************************************************
*                                                                      *
GenStgExpr: let(rec)-expressions
*                                                                      *
************************************************************************

The various forms of let(rec)-expression encode most of the interesting things
we want to do.

-   let-closure x = [free-vars] [args] expr in e

  is equivalent to

    let x = (\free-vars -> \args -> expr) free-vars

  @args@ may be empty (and is for most closures). It isn't under circumstances
  like this:

    let x = (\y -> y+z)

  This gets mangled to

    let-closure x = [z] [y] (y+z)

  The idea is that we compile code for @(y+z)@ in an environment in which @z@ is
  bound to an offset from Node, and `y` is bound to an offset from the stack
  pointer.

  (A let-closure is an @StgLet@ with a @StgRhsClosure@ RHS.)

-   let-constructor x = Constructor [args] in e

  (A let-constructor is an @StgLet@ with a @StgRhsCon@ RHS.)

- Letrec-expressions are essentially the same deal as let-closure/
  let-constructor, so we use a common structure and distinguish between them
  with an @is_recursive@ boolean flag.

-   let-unboxed u = <an arbitrary arithmetic expression in unboxed values> in e

  All the stuff on the RHS must be fully evaluated. No function calls either!

  (We've backed away from this toward case-expressions with suitably-magical
  alts ...)

- Advanced stuff here! Not to start with, but makes pattern matching generate
  more efficient code.

    let-escapes-not fail = expr
    in e'

  Here the idea is that @e'@ guarantees not to put @fail@ in a data structure,
  or pass it to another function. All @e'@ will ever do is tail-call @fail@.
  Rather than build a closure for @fail@, all we need do is to record the stack
  level at the moment of the @let-escapes-not@; then entering @fail@ is just a
  matter of adjusting the stack pointer back down to that point and entering the
  code for it.

  Another example:

    f x y = let z = huge-expression in
            if y==1 then z else
            if y==2 then z else
            1

  (A let-escapes-not is an @StgLetNoEscape@.)

- We may eventually want:

    let-literal x = Literal in e

And so the code for let(rec)-things:
-}

  | StgLet
        (XLet pass)
        (GenStgBinding pass)    -- right hand sides (see below)
        (GenStgExpr pass)       -- body

  | StgLetNoEscape
        (XLetNoEscape pass)
        (GenStgBinding pass)    -- right hand sides (see below)
        (GenStgExpr pass)       -- body

{-
*************************************************************************
*                                                                      *
GenStgExpr: hpc, scc and other debug annotations
*                                                                      *
*************************************************************************

Finally for @hpc@ expressions we introduce a new STG construct.
-}

  | StgTick
    StgTickish
    (GenStgExpr pass)       -- sub expression

-- END of GenStgExpr

{-
************************************************************************
*                                                                      *
STG right-hand sides
*                                                                      *
************************************************************************

Here's the rest of the interesting stuff for @StgLet@s; the first flavour is for
closures:
-}

data GenStgRhs pass
  = StgRhsClosure
        (XRhsClosure pass) -- ^ Extension point for non-global free var
                           --   list just before 'CodeGen'.
        CostCentreStack    -- ^ CCS to be attached (default is CurrentCCS)
        !UpdateFlag        -- ^ 'ReEntrant' | 'Updatable' | 'SingleEntry'
        [BinderP pass]     -- ^ arguments; if empty, then not a function;
                           --   as above, order is important.
        (GenStgExpr pass)  -- ^ body

{-
An example may be in order.  Consider:

  let t = \x -> \y -> ... x ... y ... p ... q in e

Pulling out the free vars and stylising somewhat, we get the equivalent:

  let t = (\[p,q] -> \[x,y] -> ... x ... y ... p ...q) p q

Stg-operationally, the @[x,y]@ are on the stack, the @[p,q]@ are offsets from
@Node@ into the closure, and the code ptr for the closure will be exactly that
in parentheses above.

The second flavour of right-hand-side is for constructors (simple but
important):
-}

  | StgRhsCon
        CostCentreStack -- CCS to be attached (default is CurrentCCS).
                        -- Top-level (static) ones will end up with
                        -- DontCareCCS, because we don't count static
                        -- data in heap profiles, and we don't set CCCS
                        -- from static closure.
        DataCon         -- Constructor. Never an unboxed tuple or sum, as those
                        -- are not allocated.
        ConstructorNumber
        [StgTickish]
        [StgArg]        -- Args

-- | Like 'GHC.Hs.Extension.NoExtField', but with an 'Outputable' instance that
-- returns 'empty'.
data NoExtFieldSilent = NoExtFieldSilent
  deriving (Typeable NoExtFieldSilent
Typeable NoExtFieldSilent =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> NoExtFieldSilent -> c NoExtFieldSilent)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c NoExtFieldSilent)
-> (NoExtFieldSilent -> Constr)
-> (NoExtFieldSilent -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c NoExtFieldSilent))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c NoExtFieldSilent))
-> ((forall b. Data b => b -> b)
    -> NoExtFieldSilent -> NoExtFieldSilent)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> NoExtFieldSilent -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> NoExtFieldSilent -> r)
-> (forall u.
    (forall d. Data d => d -> u) -> NoExtFieldSilent -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> NoExtFieldSilent -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> NoExtFieldSilent -> m NoExtFieldSilent)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> NoExtFieldSilent -> m NoExtFieldSilent)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> NoExtFieldSilent -> m NoExtFieldSilent)
-> Data NoExtFieldSilent
NoExtFieldSilent -> Constr
NoExtFieldSilent -> DataType
(forall b. Data b => b -> b)
-> NoExtFieldSilent -> NoExtFieldSilent
forall a.
Typeable a =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u.
Int -> (forall d. Data d => d -> u) -> NoExtFieldSilent -> u
forall u. (forall d. Data d => d -> u) -> NoExtFieldSilent -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> NoExtFieldSilent -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> NoExtFieldSilent -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> NoExtFieldSilent -> m NoExtFieldSilent
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> NoExtFieldSilent -> m NoExtFieldSilent
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c NoExtFieldSilent
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> NoExtFieldSilent -> c NoExtFieldSilent
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c NoExtFieldSilent)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c NoExtFieldSilent)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> NoExtFieldSilent -> c NoExtFieldSilent
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> NoExtFieldSilent -> c NoExtFieldSilent
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c NoExtFieldSilent
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c NoExtFieldSilent
$ctoConstr :: NoExtFieldSilent -> Constr
toConstr :: NoExtFieldSilent -> Constr
$cdataTypeOf :: NoExtFieldSilent -> DataType
dataTypeOf :: NoExtFieldSilent -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c NoExtFieldSilent)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c NoExtFieldSilent)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c NoExtFieldSilent)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c NoExtFieldSilent)
$cgmapT :: (forall b. Data b => b -> b)
-> NoExtFieldSilent -> NoExtFieldSilent
gmapT :: (forall b. Data b => b -> b)
-> NoExtFieldSilent -> NoExtFieldSilent
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> NoExtFieldSilent -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> NoExtFieldSilent -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> NoExtFieldSilent -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> NoExtFieldSilent -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> NoExtFieldSilent -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> NoExtFieldSilent -> [u]
$cgmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> NoExtFieldSilent -> u
gmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> NoExtFieldSilent -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> NoExtFieldSilent -> m NoExtFieldSilent
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> NoExtFieldSilent -> m NoExtFieldSilent
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> NoExtFieldSilent -> m NoExtFieldSilent
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> NoExtFieldSilent -> m NoExtFieldSilent
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> NoExtFieldSilent -> m NoExtFieldSilent
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> NoExtFieldSilent -> m NoExtFieldSilent
Data, NoExtFieldSilent -> NoExtFieldSilent -> Bool
(NoExtFieldSilent -> NoExtFieldSilent -> Bool)
-> (NoExtFieldSilent -> NoExtFieldSilent -> Bool)
-> Eq NoExtFieldSilent
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: NoExtFieldSilent -> NoExtFieldSilent -> Bool
== :: NoExtFieldSilent -> NoExtFieldSilent -> Bool
$c/= :: NoExtFieldSilent -> NoExtFieldSilent -> Bool
/= :: NoExtFieldSilent -> NoExtFieldSilent -> Bool
Eq, Eq NoExtFieldSilent
Eq NoExtFieldSilent =>
(NoExtFieldSilent -> NoExtFieldSilent -> Ordering)
-> (NoExtFieldSilent -> NoExtFieldSilent -> Bool)
-> (NoExtFieldSilent -> NoExtFieldSilent -> Bool)
-> (NoExtFieldSilent -> NoExtFieldSilent -> Bool)
-> (NoExtFieldSilent -> NoExtFieldSilent -> Bool)
-> (NoExtFieldSilent -> NoExtFieldSilent -> NoExtFieldSilent)
-> (NoExtFieldSilent -> NoExtFieldSilent -> NoExtFieldSilent)
-> Ord NoExtFieldSilent
NoExtFieldSilent -> NoExtFieldSilent -> Bool
NoExtFieldSilent -> NoExtFieldSilent -> Ordering
NoExtFieldSilent -> NoExtFieldSilent -> NoExtFieldSilent
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: NoExtFieldSilent -> NoExtFieldSilent -> Ordering
compare :: NoExtFieldSilent -> NoExtFieldSilent -> Ordering
$c< :: NoExtFieldSilent -> NoExtFieldSilent -> Bool
< :: NoExtFieldSilent -> NoExtFieldSilent -> Bool
$c<= :: NoExtFieldSilent -> NoExtFieldSilent -> Bool
<= :: NoExtFieldSilent -> NoExtFieldSilent -> Bool
$c> :: NoExtFieldSilent -> NoExtFieldSilent -> Bool
> :: NoExtFieldSilent -> NoExtFieldSilent -> Bool
$c>= :: NoExtFieldSilent -> NoExtFieldSilent -> Bool
>= :: NoExtFieldSilent -> NoExtFieldSilent -> Bool
$cmax :: NoExtFieldSilent -> NoExtFieldSilent -> NoExtFieldSilent
max :: NoExtFieldSilent -> NoExtFieldSilent -> NoExtFieldSilent
$cmin :: NoExtFieldSilent -> NoExtFieldSilent -> NoExtFieldSilent
min :: NoExtFieldSilent -> NoExtFieldSilent -> NoExtFieldSilent
Ord)

instance Outputable NoExtFieldSilent where
  ppr :: NoExtFieldSilent -> SDoc
ppr NoExtFieldSilent
_ = SDoc
forall doc. IsOutput doc => doc
empty

-- | Used when constructing a term with an unused extension point that should
-- not appear in pretty-printed output at all.
noExtFieldSilent :: NoExtFieldSilent
noExtFieldSilent :: NoExtFieldSilent
noExtFieldSilent = NoExtFieldSilent
NoExtFieldSilent
-- TODO: Maybe move this to GHC.Hs.Extension? I'm not sure about the
-- implications on build time...

stgRhsArity :: StgRhs -> Int
stgRhsArity :: StgRhs -> Int
stgRhsArity (StgRhsClosure XRhsClosure 'Vanilla
_ CostCentreStack
_ UpdateFlag
_ [BinderP 'Vanilla]
bndrs GenStgExpr 'Vanilla
_)
  = Bool -> Int -> Int
forall a. HasCallStack => Bool -> a -> a
assert ((Id -> Bool) -> [Id] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Id -> Bool
isId [Id]
[BinderP 'Vanilla]
bndrs) (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ [Id] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Id]
[BinderP 'Vanilla]
bndrs
  -- The arity never includes type parameters, but they should have gone by now
stgRhsArity (StgRhsCon {}) = Int
0

freeVarsOfRhs :: (XRhsClosure pass ~ DIdSet) => GenStgRhs pass -> DIdSet
freeVarsOfRhs :: forall (pass :: StgPass).
(XRhsClosure pass ~ DIdSet) =>
GenStgRhs pass -> DIdSet
freeVarsOfRhs (StgRhsCon CostCentreStack
_ DataCon
_ ConstructorNumber
_ [StgTickish]
_ [StgArg]
args) = [Id] -> DIdSet
mkDVarSet [ Id
id | StgVarArg Id
id <- [StgArg]
args ]
freeVarsOfRhs (StgRhsClosure XRhsClosure pass
fvs CostCentreStack
_ UpdateFlag
_ [BinderP pass]
_ GenStgExpr pass
_) = DIdSet
XRhsClosure pass
fvs

{-
************************************************************************
*                                                                      *
STG case alternatives
*                                                                      *
************************************************************************

Very like in Core syntax (except no type-world stuff).

The type constructor is guaranteed not to be abstract; that is, we can see its
representation. This is important because the code generator uses it to
determine return conventions etc. But it's not trivial where there's a module
loop involved, because some versions of a type constructor might not have all
the constructors visible. So mkStgAlgAlts (in CoreToStg) ensures that it gets
the TyCon from the constructors or literals (which are guaranteed to have the
Real McCoy) rather than from the scrutinee type.
-}

data GenStgAlt pass = GenStgAlt
  { forall (pass :: StgPass). GenStgAlt pass -> AltCon
alt_con          :: !AltCon            -- alts: data constructor,
  , forall (pass :: StgPass). GenStgAlt pass -> [BinderP pass]
alt_bndrs        :: ![BinderP pass]    -- constructor's parameters,
  , forall (pass :: StgPass). GenStgAlt pass -> GenStgExpr pass
alt_rhs          :: !(GenStgExpr pass) -- right-hand side.
  }

data AltType
  = PolyAlt             -- Polymorphic (a boxed type variable, lifted or unlifted)
  | MultiValAlt Int     -- Multi value of this arity (unboxed tuple or sum)
                        -- the arity could indeed be 1 for unary unboxed tuple
                        -- or enum-like unboxed sums
  | AlgAlt      TyCon   -- Algebraic data type; the AltCons will be DataAlts
  | PrimAlt     PrimRep -- Primitive data type; the AltCons (if any) will be LitAlts

{-
************************************************************************
*                                                                      *
The Plain STG parameterisation
*                                                                      *
************************************************************************

  Note [STG Extension points]
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  We now make use of extension points in STG for different passes which want
  to associate information with AST nodes.

  Currently the pipeline is roughly:

  CoreToStg: Core -> Stg
  StgSimpl: Stg -> Stg
  CodeGen: Stg -> Cmm

    As part of StgSimpl we run late lambda lifting (Ll).
    Late lambda lift:
    Stg -> FvStg -> LlStg -> Stg

  CodeGen:
    As part of CodeGen we run tag inference.
    Tag Inference:
      Stg -> Stg 'InferTaggedBinders` -> Stg

    And at a last step we add the free Variables:
      Stg -> CgStg

  Which finally CgStg being used to generate Cmm.

-}

type StgTopBinding = GenStgTopBinding 'Vanilla
type StgBinding    = GenStgBinding    'Vanilla
type StgExpr       = GenStgExpr       'Vanilla
type StgRhs        = GenStgRhs        'Vanilla
type StgAlt        = GenStgAlt        'Vanilla

type LlStgTopBinding = GenStgTopBinding 'LiftLams
type LlStgBinding    = GenStgBinding    'LiftLams
type LlStgExpr       = GenStgExpr       'LiftLams
type LlStgRhs        = GenStgRhs        'LiftLams
type LlStgAlt        = GenStgAlt        'LiftLams

type CgStgTopBinding = GenStgTopBinding 'CodeGen
type CgStgBinding    = GenStgBinding    'CodeGen
type CgStgExpr       = GenStgExpr       'CodeGen
type CgStgRhs        = GenStgRhs        'CodeGen
type CgStgAlt        = GenStgAlt        'CodeGen

type TgStgTopBinding = GenStgTopBinding 'CodeGen
type TgStgBinding    = GenStgBinding    'CodeGen
type TgStgExpr       = GenStgExpr       'CodeGen
type TgStgRhs        = GenStgRhs        'CodeGen
type TgStgAlt        = GenStgAlt        'CodeGen

{- Many passes apply a substitution, and it's very handy to have type
   synonyms to remind us whether or not the substitution has been applied.
   See GHC.Core for precedence in Core land
-}

type InStgTopBinding  = StgTopBinding
type InStgBinding     = StgBinding
type InStgArg         = StgArg
type InStgExpr        = StgExpr
type InStgRhs         = StgRhs
type InStgAlt         = StgAlt
type OutStgTopBinding = StgTopBinding
type OutStgBinding    = StgBinding
type OutStgArg        = StgArg
type OutStgExpr       = StgExpr
type OutStgRhs        = StgRhs
type OutStgAlt        = StgAlt

-- | When `-fdistinct-constructor-tables` is turned on then
-- each usage of a constructor is given an unique number and
-- an info table is generated for each different constructor.
data ConstructorNumber =
      NoNumber | Numbered Int

instance Outputable ConstructorNumber where
  ppr :: ConstructorNumber -> SDoc
ppr ConstructorNumber
NoNumber = SDoc
forall doc. IsOutput doc => doc
empty
  ppr (Numbered Int
n) = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"#" SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<> Int -> SDoc
forall a. Outputable a => a -> SDoc
ppr Int
n

{-
Note Stg Passes
~~~~~~~~~~~~~~~
Here is a short summary of the STG pipeline and where we use the different
StgPass data type indexes:

  1. CoreToStg.Prep performs several transformations that prepare the desugared
     and simplified core to be converted to STG. One of these transformations is
     making it so that value lambdas only exist as the RHS of a binding.
     See Note [CorePrep Overview].

  2. CoreToStg converts the prepared core to STG, specifically GenStg*
     parameterised by 'Vanilla. See the GHC.CoreToStg Module.

  3. Stg.Pipeline does a number of passes on the generated STG. One of these is
     the lambda-lifting pass, which internally uses the 'LiftLams
     parameterisation to store information for deciding whether or not to lift
     each binding.
     See Note [Late lambda lifting in STG].

  4. Tag inference takes in 'Vanilla and produces 'InferTagged STG, while using
     the InferTaggedBinders annotated AST internally.
     See Note [Tag Inference].

  5. Stg.FVs annotates closures with their free variables. To store these
     annotations we use the 'CodeGen parameterisation.
     See the GHC.Stg.FVs module.

  6. The Module Stg.StgToCmm generates Cmm from the CodeGen annotated STG.
-}


-- | Used as a data type index for the stgSyn AST
data StgPass
  = Vanilla
  | LiftLams -- ^ Use internally by the lambda lifting pass
  | InferTaggedBinders -- ^ Tag inference information on binders.
                       -- See Note [Tag inference passes] in GHC.Stg.InferTags
  | InferTagged -- ^ Tag inference information put on relevant StgApp nodes
                -- See Note [Tag inference passes] in GHC.Stg.InferTags
  | CodeGen

type family BinderP (pass :: StgPass)
type instance BinderP 'Vanilla = Id
type instance BinderP 'CodeGen = Id
type instance BinderP 'InferTagged = Id

type family XRhsClosure (pass :: StgPass)
type instance XRhsClosure 'Vanilla = NoExtFieldSilent
type instance XRhsClosure 'InferTagged = NoExtFieldSilent
-- | Code gen needs to track non-global free vars
type instance XRhsClosure 'CodeGen = DIdSet

type family XLet (pass :: StgPass)
type instance XLet 'Vanilla = NoExtFieldSilent
type instance XLet 'InferTagged = NoExtFieldSilent
type instance XLet 'CodeGen = NoExtFieldSilent

type family XLetNoEscape (pass :: StgPass)
type instance XLetNoEscape 'Vanilla = NoExtFieldSilent
type instance XLetNoEscape 'InferTagged = NoExtFieldSilent
type instance XLetNoEscape 'CodeGen = NoExtFieldSilent

{-

************************************************************************
*                                                                      *
UpdateFlag
*                                                                      *
************************************************************************

This is also used in @LambdaFormInfo@ in the @ClosureInfo@ module.

A @ReEntrant@ closure may be entered multiple times, but should not be updated
or blackholed. An @Updatable@ closure should be updated after evaluation (and
may be blackholed during evaluation). A @SingleEntry@ closure will only be
entered once, and so need not be updated but may safely be blackholed.
-}

data UpdateFlag = ReEntrant | Updatable | SingleEntry

instance Outputable UpdateFlag where
    ppr :: UpdateFlag -> SDoc
ppr UpdateFlag
u = Char -> SDoc
forall doc. IsLine doc => Char -> doc
char (Char -> SDoc) -> Char -> SDoc
forall a b. (a -> b) -> a -> b
$ case UpdateFlag
u of
                       UpdateFlag
ReEntrant   -> Char
'r'
                       UpdateFlag
Updatable   -> Char
'u'
                       UpdateFlag
SingleEntry -> Char
's'

isUpdatable :: UpdateFlag -> Bool
isUpdatable :: UpdateFlag -> Bool
isUpdatable UpdateFlag
ReEntrant   = Bool
False
isUpdatable UpdateFlag
SingleEntry = Bool
False
isUpdatable UpdateFlag
Updatable   = Bool
True

{-
************************************************************************
*                                                                      *
StgOp
*                                                                      *
************************************************************************

An StgOp allows us to group together PrimOps and ForeignCalls. It's quite useful
to move these around together, notably in StgOpApp and COpStmt.
-}

data StgOp
  = StgPrimOp  PrimOp

  | StgPrimCallOp PrimCall

  | StgFCallOp ForeignCall Type
        -- The Type, which is obtained from the foreign import declaration
        -- itself, is needed by the stg-to-cmm pass to determine the offset to
        -- apply to unlifted boxed arguments in GHC.StgToCmm.Foreign. See Note
        -- [Unlifted boxed arguments to foreign calls]

{-
************************************************************************
*                                                                      *
Pretty-printing
*                                                                      *
************************************************************************

Robin Popplestone asked for semi-colon separators on STG binds; here's hoping he
likes terminators instead...  Ditto for case alternatives.
-}

type OutputablePass pass =
  ( Outputable (XLet pass)
  , Outputable (XLetNoEscape pass)
  , Outputable (XRhsClosure pass)
  , OutputableBndr (BinderP pass)
  )

-- | STG pretty-printing options
data StgPprOpts = StgPprOpts
   { StgPprOpts -> Bool
stgSccEnabled :: !Bool -- ^ Enable cost-centres
   }

-- | STG pretty-printing options used for panic messages
panicStgPprOpts :: StgPprOpts
panicStgPprOpts :: StgPprOpts
panicStgPprOpts = StgPprOpts
   { stgSccEnabled :: Bool
stgSccEnabled = Bool
True
   }

-- | STG pretty-printing options used for short messages
shortStgPprOpts :: StgPprOpts
shortStgPprOpts :: StgPprOpts
shortStgPprOpts = StgPprOpts
   { stgSccEnabled :: Bool
stgSccEnabled = Bool
False
   }


pprGenStgTopBinding
  :: OutputablePass pass => StgPprOpts -> GenStgTopBinding pass -> SDoc
pprGenStgTopBinding :: forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgTopBinding pass -> SDoc
pprGenStgTopBinding StgPprOpts
opts GenStgTopBinding pass
b = case GenStgTopBinding pass
b of
   StgTopStringLit Id
bndr ByteString
str -> SDoc -> Int -> SDoc -> SDoc
hang ([SDoc] -> SDoc
forall doc. IsLine doc => [doc] -> doc
hsep [BindingSite -> Id -> SDoc
forall a. OutputableBndr a => BindingSite -> a -> SDoc
pprBndr BindingSite
LetBind Id
bndr, SDoc
forall doc. IsLine doc => doc
equals]) Int
4 (ByteString -> SDoc
pprHsBytes ByteString
str SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<> SDoc
forall doc. IsLine doc => doc
semi)
   StgTopLifted GenStgBinding pass
bind        -> StgPprOpts -> GenStgBinding pass -> SDoc
forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgBinding pass -> SDoc
pprGenStgBinding StgPprOpts
opts GenStgBinding pass
bind

pprGenStgBinding :: OutputablePass pass => StgPprOpts -> GenStgBinding pass -> SDoc
pprGenStgBinding :: forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgBinding pass -> SDoc
pprGenStgBinding StgPprOpts
opts GenStgBinding pass
b = case GenStgBinding pass
b of
   StgNonRec BinderP pass
bndr GenStgRhs pass
rhs -> SDoc -> Int -> SDoc -> SDoc
hang ([SDoc] -> SDoc
forall doc. IsLine doc => [doc] -> doc
hsep [BindingSite -> BinderP pass -> SDoc
forall a. OutputableBndr a => BindingSite -> a -> SDoc
pprBndr BindingSite
LetBind BinderP pass
bndr, SDoc
forall doc. IsLine doc => doc
equals]) Int
4 (StgPprOpts -> GenStgRhs pass -> SDoc
forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgRhs pass -> SDoc
pprStgRhs StgPprOpts
opts GenStgRhs pass
rhs SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<> SDoc
forall doc. IsLine doc => doc
semi)
   StgRec [(BinderP pass, GenStgRhs pass)]
pairs       -> [SDoc] -> SDoc
forall doc. IsDoc doc => [doc] -> doc
vcat [ String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"Rec {"
                              , [SDoc] -> SDoc
forall doc. IsDoc doc => [doc] -> doc
vcat (SDoc -> [SDoc] -> [SDoc]
forall a. a -> [a] -> [a]
intersperse SDoc
blankLine (((BinderP pass, GenStgRhs pass) -> SDoc)
-> [(BinderP pass, GenStgRhs pass)] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map (BinderP pass, GenStgRhs pass) -> SDoc
ppr_bind [(BinderP pass, GenStgRhs pass)]
pairs))
                              , String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"end Rec }" ]
                         where
                           ppr_bind :: (BinderP pass, GenStgRhs pass) -> SDoc
ppr_bind (BinderP pass
bndr, GenStgRhs pass
expr)
                             = SDoc -> Int -> SDoc -> SDoc
hang ([SDoc] -> SDoc
forall doc. IsLine doc => [doc] -> doc
hsep [BindingSite -> BinderP pass -> SDoc
forall a. OutputableBndr a => BindingSite -> a -> SDoc
pprBndr BindingSite
LetBind BinderP pass
bndr, SDoc
forall doc. IsLine doc => doc
equals])
                                    Int
4 (StgPprOpts -> GenStgRhs pass -> SDoc
forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgRhs pass -> SDoc
pprStgRhs StgPprOpts
opts GenStgRhs pass
expr SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<> SDoc
forall doc. IsLine doc => doc
semi)

instance OutputablePass pass => Outputable  (GenStgBinding pass) where
  ppr :: GenStgBinding pass -> SDoc
ppr = StgPprOpts -> GenStgBinding pass -> SDoc
forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgBinding pass -> SDoc
pprGenStgBinding StgPprOpts
panicStgPprOpts

pprGenStgTopBindings :: (OutputablePass pass) => StgPprOpts -> [GenStgTopBinding pass] -> SDoc
pprGenStgTopBindings :: forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> [GenStgTopBinding pass] -> SDoc
pprGenStgTopBindings StgPprOpts
opts [GenStgTopBinding pass]
binds
  = [SDoc] -> SDoc
forall doc. IsDoc doc => [doc] -> doc
vcat ([SDoc] -> SDoc) -> [SDoc] -> SDoc
forall a b. (a -> b) -> a -> b
$ SDoc -> [SDoc] -> [SDoc]
forall a. a -> [a] -> [a]
intersperse SDoc
blankLine ((GenStgTopBinding pass -> SDoc)
-> [GenStgTopBinding pass] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map (StgPprOpts -> GenStgTopBinding pass -> SDoc
forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgTopBinding pass -> SDoc
pprGenStgTopBinding StgPprOpts
opts) [GenStgTopBinding pass]
binds)

pprStgBinding :: OutputablePass pass => StgPprOpts -> GenStgBinding pass -> SDoc
pprStgBinding :: forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgBinding pass -> SDoc
pprStgBinding = StgPprOpts -> GenStgBinding pass -> SDoc
forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgBinding pass -> SDoc
pprGenStgBinding

pprStgTopBinding :: OutputablePass pass => StgPprOpts -> GenStgTopBinding pass -> SDoc
pprStgTopBinding :: forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgTopBinding pass -> SDoc
pprStgTopBinding = StgPprOpts -> GenStgTopBinding pass -> SDoc
forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgTopBinding pass -> SDoc
pprGenStgTopBinding

pprStgTopBindings :: OutputablePass pass => StgPprOpts -> [GenStgTopBinding pass] -> SDoc
pprStgTopBindings :: forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> [GenStgTopBinding pass] -> SDoc
pprStgTopBindings = StgPprOpts -> [GenStgTopBinding pass] -> SDoc
forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> [GenStgTopBinding pass] -> SDoc
pprGenStgTopBindings

pprIdWithRep :: Id -> SDoc
pprIdWithRep :: Id -> SDoc
pprIdWithRep Id
v = Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Id
v SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<> UnaryType -> SDoc
pprTypeRep (Id -> UnaryType
idType Id
v)

pprTypeRep :: Type -> SDoc
pprTypeRep :: UnaryType -> SDoc
pprTypeRep UnaryType
ty =
    (SDocContext -> Bool) -> SDoc -> SDoc
forall doc. IsLine doc => (SDocContext -> Bool) -> doc -> doc
ppUnlessOption SDocContext -> Bool
sdocSuppressStgReps (SDoc -> SDoc) -> SDoc -> SDoc
forall a b. (a -> b) -> a -> b
$
    Char -> SDoc
forall doc. IsLine doc => Char -> doc
char Char
':' SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<> case (() :: Constraint) => UnaryType -> [PrimRep]
UnaryType -> [PrimRep]
typePrimRep UnaryType
ty of
                  [PrimRep
r] -> PrimRep -> SDoc
forall a. Outputable a => a -> SDoc
ppr PrimRep
r
                  [PrimRep]
r -> [PrimRep] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [PrimRep]
r


instance Outputable StgArg where
  ppr :: StgArg -> SDoc
ppr = StgArg -> SDoc
pprStgArg

pprStgArg :: StgArg -> SDoc
pprStgArg :: StgArg -> SDoc
pprStgArg (StgVarArg Id
var) = Id -> SDoc
pprIdWithRep Id
var
pprStgArg (StgLitArg Literal
con) = Literal -> SDoc
forall a. Outputable a => a -> SDoc
ppr Literal
con SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<> UnaryType -> SDoc
pprTypeRep (Literal -> UnaryType
literalType Literal
con)

instance OutputablePass pass => Outputable  (GenStgExpr pass) where
  ppr :: GenStgExpr pass -> SDoc
ppr = StgPprOpts -> GenStgExpr pass -> SDoc
forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgExpr pass -> SDoc
pprStgExpr StgPprOpts
panicStgPprOpts

pprStgExpr :: OutputablePass pass => StgPprOpts -> GenStgExpr pass -> SDoc
pprStgExpr :: forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgExpr pass -> SDoc
pprStgExpr StgPprOpts
opts GenStgExpr pass
e = case GenStgExpr pass
e of
                           -- special case
   StgLit Literal
lit           -> Literal -> SDoc
forall a. Outputable a => a -> SDoc
ppr Literal
lit
                           -- general case
   StgApp Id
func [StgArg]
args
      | [StgArg] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [StgArg]
args
      , Just TagSig
sig <- Id -> Maybe TagSig
idTagSig_maybe Id
func
      -> Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Id
func SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<> TagSig -> SDoc
forall a. Outputable a => a -> SDoc
ppr TagSig
sig
      | Bool
otherwise -> SDoc -> Int -> SDoc -> SDoc
hang (Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Id
func) Int
4 ([StgArg] -> SDoc
forall a. Outputable a => [a] -> SDoc
interppSP [StgArg]
args) -- TODO: Print taggedness
   StgConApp DataCon
con ConstructorNumber
n [StgArg]
args [UnaryType]
_ -> [SDoc] -> SDoc
forall doc. IsLine doc => [doc] -> doc
hsep [ DataCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr DataCon
con, ConstructorNumber -> SDoc
forall a. Outputable a => a -> SDoc
ppr ConstructorNumber
n, SDoc -> SDoc
forall doc. IsLine doc => doc -> doc
brackets ([StgArg] -> SDoc
forall a. Outputable a => [a] -> SDoc
interppSP [StgArg]
args) ]
   StgOpApp StgOp
op [StgArg]
args UnaryType
_   -> [SDoc] -> SDoc
forall doc. IsLine doc => [doc] -> doc
hsep [ StgOp -> SDoc
pprStgOp StgOp
op, SDoc -> SDoc
forall doc. IsLine doc => doc -> doc
brackets ([StgArg] -> SDoc
forall a. Outputable a => [a] -> SDoc
interppSP [StgArg]
args)]

-- special case: let v = <very specific thing>
--               in
--               let ...
--               in
--               ...
--
-- Very special!  Suspicious! (SLPJ)

{-
   StgLet srt (StgNonRec bndr (StgRhsClosure cc bi free_vars upd_flag args rhs))
                        expr@(StgLet _ _))
   -> ($$)
      (hang (hcat [text "let { ", ppr bndr, text " = ",
                          ppr cc,
                          pp_binder_info bi,
                          text " [", whenPprDebug (interppSP free_vars), text "] \\",
                          ppr upd_flag, text " [",
                          interppSP args, char ']'])
            8 (sep [hsep [ppr rhs, text "} in"]]))
      (ppr expr)
-}

   -- special case: let ... in let ...
   StgLet XLet pass
ext GenStgBinding pass
bind expr :: GenStgExpr pass
expr@StgLet{} -> SDoc -> SDoc -> SDoc
forall doc. IsDoc doc => doc -> doc -> doc
($$)
      ([SDoc] -> SDoc
forall doc. IsLine doc => [doc] -> doc
sep [SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"let" SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> XLet pass -> SDoc
forall a. Outputable a => a -> SDoc
ppr XLet pass
ext SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"{")
                Int
2 ([SDoc] -> SDoc
forall doc. IsLine doc => [doc] -> doc
hsep [StgPprOpts -> GenStgBinding pass -> SDoc
forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgBinding pass -> SDoc
pprGenStgBinding StgPprOpts
opts GenStgBinding pass
bind, String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"} in"])])
      (StgPprOpts -> GenStgExpr pass -> SDoc
forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgExpr pass -> SDoc
pprStgExpr StgPprOpts
opts GenStgExpr pass
expr)

   -- general case
   StgLet XLet pass
ext GenStgBinding pass
bind GenStgExpr pass
expr
      -> [SDoc] -> SDoc
forall doc. IsLine doc => [doc] -> doc
sep [ SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"let" SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> XLet pass -> SDoc
forall a. Outputable a => a -> SDoc
ppr XLet pass
ext SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"{")
                    Int
2 (StgPprOpts -> GenStgBinding pass -> SDoc
forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgBinding pass -> SDoc
pprGenStgBinding StgPprOpts
opts GenStgBinding pass
bind)
             , SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"} in ") Int
2 (StgPprOpts -> GenStgExpr pass -> SDoc
forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgExpr pass -> SDoc
pprStgExpr StgPprOpts
opts GenStgExpr pass
expr)
             ]

   StgLetNoEscape XLetNoEscape pass
ext GenStgBinding pass
bind GenStgExpr pass
expr
      -> [SDoc] -> SDoc
forall doc. IsLine doc => [doc] -> doc
sep [ SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"let-no-escape" SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> XLetNoEscape pass -> SDoc
forall a. Outputable a => a -> SDoc
ppr XLetNoEscape pass
ext SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"{")
                    Int
2 (StgPprOpts -> GenStgBinding pass -> SDoc
forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgBinding pass -> SDoc
pprGenStgBinding StgPprOpts
opts GenStgBinding pass
bind)
             , SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"} in ") Int
2 (StgPprOpts -> GenStgExpr pass -> SDoc
forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgExpr pass -> SDoc
pprStgExpr StgPprOpts
opts GenStgExpr pass
expr)
             ]

   StgTick StgTickish
_tickish GenStgExpr pass
expr -> (SDocContext -> Bool) -> (Bool -> SDoc) -> SDoc
forall a. (SDocContext -> a) -> (a -> SDoc) -> SDoc
sdocOption SDocContext -> Bool
sdocSuppressTicks ((Bool -> SDoc) -> SDoc) -> (Bool -> SDoc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \case
      Bool
True  -> StgPprOpts -> GenStgExpr pass -> SDoc
forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgExpr pass -> SDoc
pprStgExpr StgPprOpts
opts GenStgExpr pass
expr
      Bool
False -> StgPprOpts -> GenStgExpr pass -> SDoc
forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgExpr pass -> SDoc
pprStgExpr StgPprOpts
opts GenStgExpr pass
expr
        -- XXX sep [ ppr tickish, pprStgExpr opts expr ]

   -- Don't indent for a single case alternative.
   StgCase GenStgExpr pass
expr BinderP pass
bndr AltType
alt_type [GenStgAlt pass
alt]
      -> [SDoc] -> SDoc
forall doc. IsLine doc => [doc] -> doc
sep [ [SDoc] -> SDoc
forall doc. IsLine doc => [doc] -> doc
sep [ String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"case"
                   , Int -> SDoc -> SDoc
nest Int
4 ([SDoc] -> SDoc
forall doc. IsLine doc => [doc] -> doc
hsep [ StgPprOpts -> GenStgExpr pass -> SDoc
forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgExpr pass -> SDoc
pprStgExpr StgPprOpts
opts GenStgExpr pass
expr
                                  , SDoc -> SDoc
forall doc. IsOutput doc => doc -> doc
whenPprDebug (SDoc
dcolon SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> AltType -> SDoc
forall a. Outputable a => a -> SDoc
ppr AltType
alt_type)
                                  ])
                   , String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"of"
                   , BindingSite -> BinderP pass -> SDoc
forall a. OutputableBndr a => BindingSite -> a -> SDoc
pprBndr BindingSite
CaseBind BinderP pass
bndr
                   , Char -> SDoc
forall doc. IsLine doc => Char -> doc
char Char
'{'
                   ]
             , StgPprOpts -> Bool -> GenStgAlt pass -> SDoc
forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> Bool -> GenStgAlt pass -> SDoc
pprStgAlt StgPprOpts
opts Bool
False GenStgAlt pass
alt
             , Char -> SDoc
forall doc. IsLine doc => Char -> doc
char Char
'}'
             ]

   StgCase GenStgExpr pass
expr BinderP pass
bndr AltType
alt_type [GenStgAlt pass]
alts
      -> [SDoc] -> SDoc
forall doc. IsLine doc => [doc] -> doc
sep [ [SDoc] -> SDoc
forall doc. IsLine doc => [doc] -> doc
sep [ String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"case"
                   , Int -> SDoc -> SDoc
nest Int
4 ([SDoc] -> SDoc
forall doc. IsLine doc => [doc] -> doc
hsep [ StgPprOpts -> GenStgExpr pass -> SDoc
forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgExpr pass -> SDoc
pprStgExpr StgPprOpts
opts GenStgExpr pass
expr
                                  , SDoc -> SDoc
forall doc. IsOutput doc => doc -> doc
whenPprDebug (SDoc
dcolon SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> AltType -> SDoc
forall a. Outputable a => a -> SDoc
ppr AltType
alt_type)
                                  ])
                   , String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"of"
                   , BindingSite -> BinderP pass -> SDoc
forall a. OutputableBndr a => BindingSite -> a -> SDoc
pprBndr BindingSite
CaseBind BinderP pass
bndr, Char -> SDoc
forall doc. IsLine doc => Char -> doc
char Char
'{'
                   ]
             , Int -> SDoc -> SDoc
nest Int
2 ([SDoc] -> SDoc
forall doc. IsDoc doc => [doc] -> doc
vcat ((GenStgAlt pass -> SDoc) -> [GenStgAlt pass] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map (StgPprOpts -> Bool -> GenStgAlt pass -> SDoc
forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> Bool -> GenStgAlt pass -> SDoc
pprStgAlt StgPprOpts
opts Bool
True) [GenStgAlt pass]
alts))
             , Char -> SDoc
forall doc. IsLine doc => Char -> doc
char Char
'}'
             ]


pprStgAlt :: OutputablePass pass => StgPprOpts -> Bool -> GenStgAlt pass -> SDoc
pprStgAlt :: forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> Bool -> GenStgAlt pass -> SDoc
pprStgAlt StgPprOpts
opts Bool
indent GenStgAlt{AltCon
alt_con :: forall (pass :: StgPass). GenStgAlt pass -> AltCon
alt_con :: AltCon
alt_con, [BinderP pass]
alt_bndrs :: forall (pass :: StgPass). GenStgAlt pass -> [BinderP pass]
alt_bndrs :: [BinderP pass]
alt_bndrs, GenStgExpr pass
alt_rhs :: forall (pass :: StgPass). GenStgAlt pass -> GenStgExpr pass
alt_rhs :: GenStgExpr pass
alt_rhs}
  | Bool
indent    = SDoc -> Int -> SDoc -> SDoc
hang SDoc
altPattern Int
4 (StgPprOpts -> GenStgExpr pass -> SDoc
forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgExpr pass -> SDoc
pprStgExpr StgPprOpts
opts GenStgExpr pass
alt_rhs SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<> SDoc
forall doc. IsLine doc => doc
semi)
  | Bool
otherwise = [SDoc] -> SDoc
forall doc. IsLine doc => [doc] -> doc
sep [SDoc
altPattern, StgPprOpts -> GenStgExpr pass -> SDoc
forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgExpr pass -> SDoc
pprStgExpr StgPprOpts
opts GenStgExpr pass
alt_rhs SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<> SDoc
forall doc. IsLine doc => doc
semi]
    where
      altPattern :: SDoc
altPattern = [SDoc] -> SDoc
forall doc. IsLine doc => [doc] -> doc
hsep [ AltCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr AltCon
alt_con
                        , [SDoc] -> SDoc
forall doc. IsLine doc => [doc] -> doc
sep ((BinderP pass -> SDoc) -> [BinderP pass] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map (BindingSite -> BinderP pass -> SDoc
forall a. OutputableBndr a => BindingSite -> a -> SDoc
pprBndr BindingSite
CasePatBind) [BinderP pass]
alt_bndrs)
                        , String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"->"
                        ]


pprStgOp :: StgOp -> SDoc
pprStgOp :: StgOp -> SDoc
pprStgOp (StgPrimOp  PrimOp
op)   = PrimOp -> SDoc
forall a. Outputable a => a -> SDoc
ppr PrimOp
op
pprStgOp (StgPrimCallOp PrimCall
op)= PrimCall -> SDoc
forall a. Outputable a => a -> SDoc
ppr PrimCall
op
pprStgOp (StgFCallOp ForeignCall
op UnaryType
_) = ForeignCall -> SDoc
forall a. Outputable a => a -> SDoc
ppr ForeignCall
op

instance Outputable StgOp where
  ppr :: StgOp -> SDoc
ppr = StgOp -> SDoc
pprStgOp

instance Outputable AltType where
  ppr :: AltType -> SDoc
ppr AltType
PolyAlt         = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"Polymorphic"
  ppr (MultiValAlt Int
n) = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"MultiAlt" SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> Int -> SDoc
forall a. Outputable a => a -> SDoc
ppr Int
n
  ppr (AlgAlt TyCon
tc)     = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"Alg"    SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> TyCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr TyCon
tc
  ppr (PrimAlt PrimRep
tc)    = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"Prim"   SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> PrimRep -> SDoc
forall a. Outputable a => a -> SDoc
ppr PrimRep
tc

pprStgRhs :: OutputablePass pass => StgPprOpts -> GenStgRhs pass -> SDoc
pprStgRhs :: forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgRhs pass -> SDoc
pprStgRhs StgPprOpts
opts GenStgRhs pass
rhs = case GenStgRhs pass
rhs of
   StgRhsClosure XRhsClosure pass
ext CostCentreStack
cc UpdateFlag
upd_flag [BinderP pass]
args GenStgExpr pass
body
      -> SDoc -> Int -> SDoc -> SDoc
hang ([SDoc] -> SDoc
forall doc. IsLine doc => [doc] -> doc
hsep [ if StgPprOpts -> Bool
stgSccEnabled StgPprOpts
opts then CostCentreStack -> SDoc
forall a. Outputable a => a -> SDoc
ppr CostCentreStack
cc else SDoc
forall doc. IsOutput doc => doc
empty
                    , (SDocContext -> Bool) -> SDoc -> SDoc
forall doc. IsLine doc => (SDocContext -> Bool) -> doc -> doc
ppUnlessOption SDocContext -> Bool
sdocSuppressStgExts (XRhsClosure pass -> SDoc
forall a. Outputable a => a -> SDoc
ppr XRhsClosure pass
ext)
                    , Char -> SDoc
forall doc. IsLine doc => Char -> doc
char Char
'\\' SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<> UpdateFlag -> SDoc
forall a. Outputable a => a -> SDoc
ppr UpdateFlag
upd_flag, SDoc -> SDoc
forall doc. IsLine doc => doc -> doc
brackets ([BinderP pass] -> SDoc
forall a. Outputable a => [a] -> SDoc
interppSP [BinderP pass]
args)
                    ])
              Int
4 (StgPprOpts -> GenStgExpr pass -> SDoc
forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgExpr pass -> SDoc
pprStgExpr StgPprOpts
opts GenStgExpr pass
body)

   StgRhsCon CostCentreStack
cc DataCon
con ConstructorNumber
mid [StgTickish]
_ticks [StgArg]
args
      -> [SDoc] -> SDoc
forall doc. IsLine doc => [doc] -> doc
hcat [ if StgPprOpts -> Bool
stgSccEnabled StgPprOpts
opts then CostCentreStack -> SDoc
forall a. Outputable a => a -> SDoc
ppr CostCentreStack
cc SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<> SDoc
forall doc. IsLine doc => doc
space else SDoc
forall doc. IsOutput doc => doc
empty
              , case ConstructorNumber
mid of
                  ConstructorNumber
NoNumber -> SDoc
forall doc. IsOutput doc => doc
empty
                  Numbered Int
n -> [SDoc] -> SDoc
forall doc. IsLine doc => [doc] -> doc
hcat [Int -> SDoc
forall a. Outputable a => a -> SDoc
ppr Int
n, SDoc
forall doc. IsLine doc => doc
space]
              -- The bang indicates this is an StgRhsCon instead of an StgConApp.
              , DataCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr DataCon
con, String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"! ", SDoc -> SDoc
forall doc. IsLine doc => doc -> doc
brackets ([SDoc] -> SDoc
forall doc. IsLine doc => [doc] -> doc
sep ((StgArg -> SDoc) -> [StgArg] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map StgArg -> SDoc
pprStgArg [StgArg]
args))]

instance OutputablePass pass => Outputable  (GenStgRhs pass) where
  ppr :: GenStgRhs pass -> SDoc
ppr = StgPprOpts -> GenStgRhs pass -> SDoc
forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgRhs pass -> SDoc
pprStgRhs StgPprOpts
panicStgPprOpts