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

\section[StgSyn]{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 @CoreSyntax@, the style
being one that happens to be ideally suited to spineless tagless code
generation.
-}

{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ConstraintKinds #-}

module StgSyn (
        StgArg(..),

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

        StgPass(..), BinderP, XRhsClosure, XLet, XLetNoEscape,
        NoExtSilent, noExtSilent,
        OutputablePass,

        UpdateFlag(..), isUpdatable,

        -- 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,

        -- 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
        topStgBindHasCafRefs, stgArgHasCafRefs, stgRhsArity,
        isDllConApp,
        stgArgType,
        stripStgTicksTop,
        stgCaseBndrInScope,

        pprStgBinding, pprGenStgTopBindings, pprStgTopBindings
    ) where

#include "HsVersions.h"

import GhcPrelude

import CoreSyn     ( AltCon, Tickish )
import CostCentre  ( CostCentreStack )
import Data.ByteString ( ByteString )
import Data.Data   ( Data )
import Data.List   ( intersperse )
import DataCon
import DynFlags
import FastString
import ForeignCall ( ForeignCall )
import Id
import IdInfo      ( mayHaveCafRefs )
import VarSet
import Literal     ( Literal, literalType )
import Module      ( Module )
import Outputable
import Packages    ( isDllName )
import Platform
import PprCore     ( {- instances -} )
import PrimOp      ( PrimOp, PrimCall )
import TyCon       ( PrimRep(..), TyCon )
import Type        ( Type )
import RepType     ( typePrimRep1 )
import Unique      ( Unique )
import Util

import Data.List.NonEmpty ( NonEmpty, toList )

{-
************************************************************************
*                                                                      *
\subsection{@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
@CoreSyn@):
-}

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

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

{-
************************************************************************
*                                                                      *
\subsection{@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 :: DynFlags -> Module -> DataCon -> [StgArg] -> Bool
isDllConApp :: DynFlags -> Module -> DataCon -> [StgArg] -> Bool
isDllConApp dflags :: DynFlags
dflags this_mod :: Module
this_mod con :: DataCon
con args :: [StgArg]
args
 | Platform -> OS
platformOS (DynFlags -> Platform
targetPlatform DynFlags
dflags) OS -> OS -> Bool
forall a. Eq a => a -> a -> Bool
== OS
OSMinGW32
    = DynFlags -> Module -> Name -> Bool
isDllName DynFlags
dflags 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 v :: Id
v) =  PrimRep -> Bool
isAddrRep (HasDebugCallStack => UnaryType -> PrimRep
UnaryType -> PrimRep
typePrimRep1 (Id -> UnaryType
idType Id
v))
                             Bool -> Bool -> Bool
&& DynFlags -> Module -> Name -> Bool
isDllName DynFlags
dflags Module
this_mod (Id -> Name
idName Id
v)
    is_dll_arg _             = 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 AddrRep     = Bool
True
isAddrRep LiftedRep   = Bool
True
isAddrRep UnliftedRep = Bool
True
isAddrRep _           = Bool
False

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


-- | Strip ticks of a given type from an STG expression
stripStgTicksTop :: (Tickish Id -> Bool) -> GenStgExpr p -> ([Tickish Id], GenStgExpr p)
stripStgTicksTop :: (Tickish Id -> Bool)
-> GenStgExpr p -> ([Tickish Id], GenStgExpr p)
stripStgTicksTop p :: Tickish Id -> Bool
p = [Tickish Id] -> GenStgExpr p -> ([Tickish Id], GenStgExpr p)
go []
   where go :: [Tickish Id] -> GenStgExpr p -> ([Tickish Id], GenStgExpr p)
go ts :: [Tickish Id]
ts (StgTick t :: Tickish Id
t e :: GenStgExpr p
e) | Tickish Id -> Bool
p Tickish Id
t = [Tickish Id] -> GenStgExpr p -> ([Tickish Id], GenStgExpr p)
go (Tickish Id
tTickish Id -> [Tickish Id] -> [Tickish Id]
forall a. a -> [a] -> [a]
:[Tickish Id]
ts) GenStgExpr p
e
         go ts :: [Tickish Id]
ts other :: GenStgExpr p
other               = ([Tickish Id] -> [Tickish Id]
forall a. [a] -> [a]
reverse [Tickish Id]
ts, GenStgExpr p
other)

-- | 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 alt_ty :: AltType
alt_ty unarised :: Bool
unarised =
    case AltType
alt_ty of
      AlgAlt _      -> Bool
True
      PrimAlt _     -> Bool
True
      MultiValAlt _ -> Bool -> Bool
not Bool
unarised
      PolyAlt       -> Bool
True

{-
************************************************************************
*                                                                      *
\subsection{STG expressions}
*                                                                      *
************************************************************************

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

************************************************************************
*                                                                      *
\subsubsection{@GenStgExpr@ application}
*                                                                      *
************************************************************************

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

{-
************************************************************************
*                                                                      *
\subsubsection{@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 first
  | StgConApp   DataCon
                [StgArg] -- Saturated
                [Type]   -- See Note [Types in StgConApp] in UnariseStg

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

{-
************************************************************************
*                                                                      *
\subsubsection{@StgLam@}
*                                                                      *
************************************************************************

StgLam is used *only* during CoreToStg's work. Before CoreToStg has
finished it encodes (\x -> e) as (let f = \x -> e in f)
TODO: Encode this via an extension to GenStgExpr à la TTG.
-}

  | StgLam
        (NonEmpty (BinderP pass))
        StgExpr    -- Body of lambda

{-
************************************************************************
*                                                                      *
\subsubsection{@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

{-
************************************************************************
*                                                                      *
\subsubsection{@GenStgExpr@: @let(rec)@-expressions}
*                                                                      *
************************************************************************

The various forms of let(rec)-expression encode most of the
interesting things we want to do.
\begin{enumerate}
\item
\begin{verbatim}
let-closure x = [free-vars] [args] expr
in e
\end{verbatim}
is equivalent to
\begin{verbatim}
let x = (\free-vars -> \args -> expr) free-vars
\end{verbatim}
\tr{args} may be empty (and is for most closures).  It isn't under
circumstances like this:
\begin{verbatim}
let x = (\y -> y+z)
\end{verbatim}
This gets mangled to
\begin{verbatim}
let-closure x = [z] [y] (y+z)
\end{verbatim}
The idea is that we compile code for @(y+z)@ in an environment in which
@z@ is bound to an offset from \tr{Node}, and @y@ is bound to an
offset from the stack pointer.

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

\item
\begin{verbatim}
let-constructor x = Constructor [args]
in e
\end{verbatim}

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

\item
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.

\item
\begin{verbatim}
let-unboxed u = an arbitrary arithmetic expression in unboxed values
in e
\end{verbatim}
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 ...)

\item
~[Advanced stuff here! Not to start with, but makes pattern matching
generate more efficient code.]

\begin{verbatim}
let-escapes-not fail = expr
in e'
\end{verbatim}
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:
\begin{verbatim}
f x y = let z = huge-expression in
        if y==1 then z else
        if y==2 then z else
        1
\end{verbatim}

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

\item
We may eventually want:
\begin{verbatim}
let-literal x = Literal
in e
\end{verbatim}
\end{enumerate}

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

{-
%************************************************************************
%*                                                                      *
\subsubsection{@GenStgExpr@: @hpc@, @scc@ and other debug annotations}
%*                                                                      *
%************************************************************************

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

  | StgTick
    (Tickish Id)
    (GenStgExpr pass)       -- sub expression

-- END of GenStgExpr

{-
************************************************************************
*                                                                      *
\subsection{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:
\begin{verbatim}
let t = \x -> \y -> ... x ... y ... p ... q in e
\end{verbatim}
Pulling out the free vars and stylising somewhat, we get the equivalent:
\begin{verbatim}
let t = (\[p,q] -> \[x,y] -> ... x ... y ... p ...q) p q
\end{verbatim}
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.
        [StgArg]        -- Args

-- | Used as a data type index for the stgSyn AST
data StgPass
  = Vanilla
  | LiftLams
  | CodeGen

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

instance Outputable NoExtSilent where
  ppr :: NoExtSilent -> SDoc
ppr _ = SDoc
empty

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

-- TODO: Do we really want to the extension point type families to have a closed
-- domain?
type family BinderP (pass :: StgPass)
type instance BinderP 'Vanilla = Id
type instance BinderP 'CodeGen = Id

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

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

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

stgRhsArity :: StgRhs -> Int
stgRhsArity :: StgRhs -> Int
stgRhsArity (StgRhsClosure _ _ _ bndrs :: [BinderP 'Vanilla]
bndrs _)
  = ASSERT( all isId bndrs ) length bndrs
  -- The arity never includes type parameters, but they should have gone by now
stgRhsArity (StgRhsCon _ _ _) = 0

-- Note [CAF consistency]
-- ~~~~~~~~~~~~~~~~~~~~~~
--
-- `topStgBindHasCafRefs` is only used by an assert (`consistentCafInfo` in
-- `CoreToStg`) to make sure CAF-ness predicted by `TidyPgm` is consistent with
-- reality.
--
-- Specifically, if the RHS mentions any Id that itself is marked
-- `MayHaveCafRefs`; or if the binding is a top-level updateable thunk; then the
-- `Id` for the binding should be marked `MayHaveCafRefs`. The potential trouble
-- is that `TidyPgm` computed the CAF info on the `Id` but some transformations
-- have taken place since then.

topStgBindHasCafRefs :: GenStgTopBinding pass -> Bool
topStgBindHasCafRefs :: GenStgTopBinding pass -> Bool
topStgBindHasCafRefs (StgTopLifted (StgNonRec _ rhs :: GenStgRhs pass
rhs))
  = GenStgRhs pass -> Bool
forall (pass :: StgPass). GenStgRhs pass -> Bool
topRhsHasCafRefs GenStgRhs pass
rhs
topStgBindHasCafRefs (StgTopLifted (StgRec binds :: [(BinderP pass, GenStgRhs pass)]
binds))
  = (GenStgRhs pass -> Bool) -> [GenStgRhs pass] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any GenStgRhs pass -> Bool
forall (pass :: StgPass). GenStgRhs pass -> Bool
topRhsHasCafRefs (((BinderP pass, GenStgRhs pass) -> GenStgRhs pass)
-> [(BinderP pass, GenStgRhs pass)] -> [GenStgRhs pass]
forall a b. (a -> b) -> [a] -> [b]
map (BinderP pass, GenStgRhs pass) -> GenStgRhs pass
forall a b. (a, b) -> b
snd [(BinderP pass, GenStgRhs pass)]
binds)
topStgBindHasCafRefs StgTopStringLit{}
  = Bool
False

topRhsHasCafRefs :: GenStgRhs pass -> Bool
topRhsHasCafRefs :: GenStgRhs pass -> Bool
topRhsHasCafRefs (StgRhsClosure _ _ upd :: UpdateFlag
upd _ body :: GenStgExpr pass
body)
  = -- See Note [CAF consistency]
    UpdateFlag -> Bool
isUpdatable UpdateFlag
upd Bool -> Bool -> Bool
|| GenStgExpr pass -> Bool
forall (pass :: StgPass). GenStgExpr pass -> Bool
exprHasCafRefs GenStgExpr pass
body
topRhsHasCafRefs (StgRhsCon _ _ args :: [StgArg]
args)
  = (StgArg -> Bool) -> [StgArg] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any StgArg -> Bool
stgArgHasCafRefs [StgArg]
args

exprHasCafRefs :: GenStgExpr pass -> Bool
exprHasCafRefs :: GenStgExpr pass -> Bool
exprHasCafRefs (StgApp f :: Id
f args :: [StgArg]
args)
  = Id -> Bool
stgIdHasCafRefs Id
f Bool -> Bool -> Bool
|| (StgArg -> Bool) -> [StgArg] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any StgArg -> Bool
stgArgHasCafRefs [StgArg]
args
exprHasCafRefs StgLit{}
  = Bool
False
exprHasCafRefs (StgConApp _ args :: [StgArg]
args _)
  = (StgArg -> Bool) -> [StgArg] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any StgArg -> Bool
stgArgHasCafRefs [StgArg]
args
exprHasCafRefs (StgOpApp _ args :: [StgArg]
args _)
  = (StgArg -> Bool) -> [StgArg] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any StgArg -> Bool
stgArgHasCafRefs [StgArg]
args
exprHasCafRefs (StgLam _ body :: StgExpr
body)
  = StgExpr -> Bool
forall (pass :: StgPass). GenStgExpr pass -> Bool
exprHasCafRefs StgExpr
body
exprHasCafRefs (StgCase scrt :: GenStgExpr pass
scrt _ _ alts :: [GenStgAlt pass]
alts)
  = GenStgExpr pass -> Bool
forall (pass :: StgPass). GenStgExpr pass -> Bool
exprHasCafRefs GenStgExpr pass
scrt Bool -> Bool -> Bool
|| (GenStgAlt pass -> Bool) -> [GenStgAlt pass] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any GenStgAlt pass -> Bool
forall (pass :: StgPass). GenStgAlt pass -> Bool
altHasCafRefs [GenStgAlt pass]
alts
exprHasCafRefs (StgLet _ bind :: GenStgBinding pass
bind body :: GenStgExpr pass
body)
  = GenStgBinding pass -> Bool
forall (pass :: StgPass). GenStgBinding pass -> Bool
bindHasCafRefs GenStgBinding pass
bind Bool -> Bool -> Bool
|| GenStgExpr pass -> Bool
forall (pass :: StgPass). GenStgExpr pass -> Bool
exprHasCafRefs GenStgExpr pass
body
exprHasCafRefs (StgLetNoEscape _ bind :: GenStgBinding pass
bind body :: GenStgExpr pass
body)
  = GenStgBinding pass -> Bool
forall (pass :: StgPass). GenStgBinding pass -> Bool
bindHasCafRefs GenStgBinding pass
bind Bool -> Bool -> Bool
|| GenStgExpr pass -> Bool
forall (pass :: StgPass). GenStgExpr pass -> Bool
exprHasCafRefs GenStgExpr pass
body
exprHasCafRefs (StgTick _ expr :: GenStgExpr pass
expr)
  = GenStgExpr pass -> Bool
forall (pass :: StgPass). GenStgExpr pass -> Bool
exprHasCafRefs GenStgExpr pass
expr

bindHasCafRefs :: GenStgBinding pass -> Bool
bindHasCafRefs :: GenStgBinding pass -> Bool
bindHasCafRefs (StgNonRec _ rhs :: GenStgRhs pass
rhs)
  = GenStgRhs pass -> Bool
forall (pass :: StgPass). GenStgRhs pass -> Bool
rhsHasCafRefs GenStgRhs pass
rhs
bindHasCafRefs (StgRec binds :: [(BinderP pass, GenStgRhs pass)]
binds)
  = (GenStgRhs pass -> Bool) -> [GenStgRhs pass] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any GenStgRhs pass -> Bool
forall (pass :: StgPass). GenStgRhs pass -> Bool
rhsHasCafRefs (((BinderP pass, GenStgRhs pass) -> GenStgRhs pass)
-> [(BinderP pass, GenStgRhs pass)] -> [GenStgRhs pass]
forall a b. (a -> b) -> [a] -> [b]
map (BinderP pass, GenStgRhs pass) -> GenStgRhs pass
forall a b. (a, b) -> b
snd [(BinderP pass, GenStgRhs pass)]
binds)

rhsHasCafRefs :: GenStgRhs pass -> Bool
rhsHasCafRefs :: GenStgRhs pass -> Bool
rhsHasCafRefs (StgRhsClosure _ _ _ _ body :: GenStgExpr pass
body)
  = GenStgExpr pass -> Bool
forall (pass :: StgPass). GenStgExpr pass -> Bool
exprHasCafRefs GenStgExpr pass
body
rhsHasCafRefs (StgRhsCon _ _ args :: [StgArg]
args)
  = (StgArg -> Bool) -> [StgArg] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any StgArg -> Bool
stgArgHasCafRefs [StgArg]
args

altHasCafRefs :: GenStgAlt pass -> Bool
altHasCafRefs :: GenStgAlt pass -> Bool
altHasCafRefs (_, _, rhs :: GenStgExpr pass
rhs) = GenStgExpr pass -> Bool
forall (pass :: StgPass). GenStgExpr pass -> Bool
exprHasCafRefs GenStgExpr pass
rhs

stgArgHasCafRefs :: StgArg -> Bool
stgArgHasCafRefs :: StgArg -> Bool
stgArgHasCafRefs (StgVarArg id :: Id
id)
  = Id -> Bool
stgIdHasCafRefs Id
id
stgArgHasCafRefs _
  = Bool
False

stgIdHasCafRefs :: Id -> Bool
stgIdHasCafRefs :: Id -> Bool
stgIdHasCafRefs id :: Id
id =
  -- We are looking for occurrences of an Id that is bound at top level, and may
  -- have CAF refs. At this point (after TidyPgm) top-level Ids (whether
  -- imported or defined in this module) are GlobalIds, so the test is easy.
  Id -> Bool
isGlobalId Id
id Bool -> Bool -> Bool
&& CafInfo -> Bool
mayHaveCafRefs (Id -> CafInfo
idCafInfo Id
id)

{-
************************************************************************
*                                                                      *
\subsection[Stg-case-alternatives]{STG case alternatives}
*                                                                      *
************************************************************************

Very like in @CoreSyntax@ (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.
-}

type GenStgAlt pass
  = (AltCon,          -- alts: data constructor,
     [BinderP pass],  -- constructor's parameters,
     GenStgExpr pass) -- ...right-hand side.

data AltType
  = PolyAlt             -- Polymorphic (a lifted type variable)
  | 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

{-
************************************************************************
*                                                                      *
\subsection[Stg]{The Plain STG parameterisation}
*                                                                      *
************************************************************************

This happens to be the only one we use at the moment.
-}

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

{- 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 CoreSyn 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

{-

************************************************************************
*                                                                      *
\subsubsection[UpdateFlag-datatype]{@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 u :: UpdateFlag
u = Char -> SDoc
char (Char -> SDoc) -> Char -> SDoc
forall a b. (a -> b) -> a -> b
$ case UpdateFlag
u of
                       ReEntrant   -> 'r'
                       Updatable   -> 'u'
                       SingleEntry -> 's'

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

{-
************************************************************************
*                                                                      *
\subsubsection{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 Unique
        -- The Unique is occasionally needed by the C pretty-printer
        -- (which lacks a unique supply), notably when generating a
        -- typedef for foreign-export-dynamic

{-
************************************************************************
*                                                                      *
\subsection[Stg-pretty-printing]{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)
  )

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

pprGenStgBinding
  :: OutputablePass pass => GenStgBinding pass -> SDoc

pprGenStgBinding :: GenStgBinding pass -> SDoc
pprGenStgBinding (StgNonRec bndr :: BinderP pass
bndr rhs :: GenStgRhs pass
rhs)
  = SDoc -> Int -> SDoc -> SDoc
hang ([SDoc] -> SDoc
hsep [BindingSite -> BinderP pass -> SDoc
forall a. OutputableBndr a => BindingSite -> a -> SDoc
pprBndr BindingSite
LetBind BinderP pass
bndr, SDoc
equals])
        4 (GenStgRhs pass -> SDoc
forall a. Outputable a => a -> SDoc
ppr GenStgRhs pass
rhs SDoc -> SDoc -> SDoc
<> SDoc
semi)

pprGenStgBinding (StgRec pairs :: [(BinderP pass, GenStgRhs pass)]
pairs)
  = [SDoc] -> SDoc
vcat [ String -> SDoc
text "Rec {"
         , [SDoc] -> SDoc
vcat (((BinderP pass, GenStgRhs pass) -> SDoc)
-> [(BinderP pass, GenStgRhs pass)] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map (BinderP pass, GenStgRhs pass) -> SDoc
forall a a. (OutputableBndr a, Outputable a) => (a, a) -> SDoc
ppr_bind [(BinderP pass, GenStgRhs pass)]
pairs)
         , String -> SDoc
text "end Rec }" ]
  where
    ppr_bind :: (a, a) -> SDoc
ppr_bind (bndr :: a
bndr, expr :: a
expr)
      = SDoc -> Int -> SDoc -> SDoc
hang ([SDoc] -> SDoc
hsep [BindingSite -> a -> SDoc
forall a. OutputableBndr a => BindingSite -> a -> SDoc
pprBndr BindingSite
LetBind a
bndr, SDoc
equals])
             4 (a -> SDoc
forall a. Outputable a => a -> SDoc
ppr a
expr SDoc -> SDoc -> SDoc
<> SDoc
semi)

pprGenStgTopBindings
  :: (OutputablePass pass) => [GenStgTopBinding pass] -> SDoc
pprGenStgTopBindings :: [GenStgTopBinding pass] -> SDoc
pprGenStgTopBindings binds :: [GenStgTopBinding pass]
binds
  = [SDoc] -> SDoc
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 GenStgTopBinding pass -> SDoc
forall (pass :: StgPass).
OutputablePass pass =>
GenStgTopBinding pass -> SDoc
pprGenStgTopBinding [GenStgTopBinding pass]
binds)

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

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

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

instance OutputablePass pass => Outputable (GenStgTopBinding pass) where
    ppr :: GenStgTopBinding pass -> SDoc
ppr = GenStgTopBinding pass -> SDoc
forall (pass :: StgPass).
OutputablePass pass =>
GenStgTopBinding pass -> SDoc
pprGenStgTopBinding

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

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

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

pprStgArg :: StgArg -> SDoc
pprStgArg :: StgArg -> SDoc
pprStgArg (StgVarArg var :: Id
var) = Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Id
var
pprStgArg (StgLitArg con :: Literal
con) = Literal -> SDoc
forall a. Outputable a => a -> SDoc
ppr Literal
con

pprStgExpr :: OutputablePass pass => GenStgExpr pass -> SDoc
-- special case
pprStgExpr :: GenStgExpr pass -> SDoc
pprStgExpr (StgLit lit :: Literal
lit)     = Literal -> SDoc
forall a. Outputable a => a -> SDoc
ppr Literal
lit

-- general case
pprStgExpr (StgApp func :: Id
func args :: [StgArg]
args)
  = SDoc -> Int -> SDoc -> SDoc
hang (Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Id
func) 4 ([SDoc] -> SDoc
sep ((StgArg -> SDoc) -> [StgArg] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map (StgArg -> SDoc
forall a. Outputable a => a -> SDoc
ppr) [StgArg]
args))

pprStgExpr (StgConApp con :: DataCon
con args :: [StgArg]
args _)
  = [SDoc] -> SDoc
hsep [ DataCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr DataCon
con, SDoc -> SDoc
brackets ([StgArg] -> SDoc
forall a. Outputable a => [a] -> SDoc
interppSP [StgArg]
args) ]

pprStgExpr (StgOpApp op :: StgOp
op args :: [StgArg]
args _)
  = [SDoc] -> SDoc
hsep [ StgOp -> SDoc
pprStgOp StgOp
op, SDoc -> SDoc
brackets ([StgArg] -> SDoc
forall a. Outputable a => [a] -> SDoc
interppSP [StgArg]
args)]

pprStgExpr (StgLam bndrs :: NonEmpty (BinderP pass)
bndrs body :: StgExpr
body)
  = [SDoc] -> SDoc
sep [ Char -> SDoc
char '\\' SDoc -> SDoc -> SDoc
<+> [SDoc] -> SDoc
ppr_list ((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
LambdaBind) (NonEmpty (BinderP pass) -> [BinderP pass]
forall a. NonEmpty a -> [a]
toList NonEmpty (BinderP pass)
bndrs))
            SDoc -> SDoc -> SDoc
<+> String -> SDoc
text "->",
         StgExpr -> SDoc
forall (pass :: StgPass).
OutputablePass pass =>
GenStgExpr pass -> SDoc
pprStgExpr StgExpr
body ]
  where ppr_list :: [SDoc] -> SDoc
ppr_list = SDoc -> SDoc
brackets (SDoc -> SDoc) -> ([SDoc] -> SDoc) -> [SDoc] -> SDoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [SDoc] -> SDoc
fsep ([SDoc] -> SDoc) -> ([SDoc] -> [SDoc]) -> [SDoc] -> SDoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SDoc -> [SDoc] -> [SDoc]
punctuate SDoc
comma

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

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

-- special case: let ... in let ...

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

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

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

pprStgExpr (StgTick tickish :: Tickish Id
tickish expr :: GenStgExpr pass
expr)
  = (DynFlags -> SDoc) -> SDoc
sdocWithDynFlags ((DynFlags -> SDoc) -> SDoc) -> (DynFlags -> SDoc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \dflags :: DynFlags
dflags ->
    if GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_SuppressTicks DynFlags
dflags
    then GenStgExpr pass -> SDoc
forall (pass :: StgPass).
OutputablePass pass =>
GenStgExpr pass -> SDoc
pprStgExpr GenStgExpr pass
expr
    else [SDoc] -> SDoc
sep [ Tickish Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Tickish Id
tickish, GenStgExpr pass -> SDoc
forall (pass :: StgPass).
OutputablePass pass =>
GenStgExpr pass -> SDoc
pprStgExpr GenStgExpr pass
expr ]


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

pprStgAlt :: OutputablePass pass => GenStgAlt pass -> SDoc
pprStgAlt :: GenStgAlt pass -> SDoc
pprStgAlt (con :: AltCon
con, params :: [BinderP pass]
params, expr :: GenStgExpr pass
expr)
  = SDoc -> Int -> SDoc -> SDoc
hang ([SDoc] -> SDoc
hsep [AltCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr AltCon
con, [SDoc] -> SDoc
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]
params), String -> SDoc
text "->"])
         4 (GenStgExpr pass -> SDoc
forall a. Outputable a => a -> SDoc
ppr GenStgExpr pass
expr SDoc -> SDoc -> SDoc
<> SDoc
semi)

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

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

pprStgRhs :: OutputablePass pass => GenStgRhs pass -> SDoc

-- special case
pprStgRhs :: GenStgRhs pass -> SDoc
pprStgRhs (StgRhsClosure ext :: XRhsClosure pass
ext cc :: CostCentreStack
cc upd_flag :: UpdateFlag
upd_flag [{-no args-}] (StgApp func :: Id
func []))
  = (DynFlags -> SDoc) -> SDoc
sdocWithDynFlags ((DynFlags -> SDoc) -> SDoc) -> (DynFlags -> SDoc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \dflags :: DynFlags
dflags ->
    [SDoc] -> SDoc
hsep [ CostCentreStack -> SDoc
forall a. Outputable a => a -> SDoc
ppr CostCentreStack
cc,
           if Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_SuppressStgExts DynFlags
dflags
             then XRhsClosure pass -> SDoc
forall a. Outputable a => a -> SDoc
ppr XRhsClosure pass
ext else SDoc
empty,
           String -> SDoc
text " \\", UpdateFlag -> SDoc
forall a. Outputable a => a -> SDoc
ppr UpdateFlag
upd_flag, PtrString -> SDoc
ptext (String -> PtrString
sLit " [] "), Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Id
func ]

-- general case
pprStgRhs (StgRhsClosure ext :: XRhsClosure pass
ext cc :: CostCentreStack
cc upd_flag :: UpdateFlag
upd_flag args :: [BinderP pass]
args body :: GenStgExpr pass
body)
  = (DynFlags -> SDoc) -> SDoc
sdocWithDynFlags ((DynFlags -> SDoc) -> SDoc) -> (DynFlags -> SDoc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \dflags :: DynFlags
dflags ->
    SDoc -> Int -> SDoc -> SDoc
hang ([SDoc] -> SDoc
hsep [if GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_SccProfilingOn DynFlags
dflags then CostCentreStack -> SDoc
forall a. Outputable a => a -> SDoc
ppr CostCentreStack
cc else SDoc
empty,
                if Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_SuppressStgExts DynFlags
dflags
                  then XRhsClosure pass -> SDoc
forall a. Outputable a => a -> SDoc
ppr XRhsClosure pass
ext else SDoc
empty,
                Char -> SDoc
char '\\' SDoc -> SDoc -> SDoc
<> UpdateFlag -> SDoc
forall a. Outputable a => a -> SDoc
ppr UpdateFlag
upd_flag, SDoc -> SDoc
brackets ([BinderP pass] -> SDoc
forall a. Outputable a => [a] -> SDoc
interppSP [BinderP pass]
args)])
         4 (GenStgExpr pass -> SDoc
forall a. Outputable a => a -> SDoc
ppr GenStgExpr pass
body)

pprStgRhs (StgRhsCon cc :: CostCentreStack
cc con :: DataCon
con args :: [StgArg]
args)
  = [SDoc] -> SDoc
hcat [ CostCentreStack -> SDoc
forall a. Outputable a => a -> SDoc
ppr CostCentreStack
cc,
           SDoc
space, DataCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr DataCon
con, String -> SDoc
text "! ", SDoc -> SDoc
brackets ([StgArg] -> SDoc
forall a. Outputable a => [a] -> SDoc
interppSP [StgArg]
args)]