{-# LANGUAGE CPP, MagicHash, ScopedTypeVariables #-}
{-# OPTIONS_GHC -optc-DNON_POSIX_SOURCE #-}
--
--  (c) The University of Glasgow 2002-2006
--

-- | Generate infotables for interpreter-made bytecodes
module GHC.ByteCode.InfoTable ( mkITbls ) where

#include "HsVersions.h"

import GHC.Prelude

import GHC.Platform
import GHC.ByteCode.Types
import GHC.Runtime.Interpreter
import GHC.Driver.Session
import GHC.Driver.Types
import GHC.Types.Name       ( Name, getName )
import GHC.Types.Name.Env
import GHC.Core.DataCon     ( DataCon, dataConRepArgTys, dataConIdentity )
import GHC.Core.TyCon       ( TyCon, tyConFamilySize, isDataTyCon, tyConDataCons )
import GHC.Core.Multiplicity     ( scaledThing )
import GHC.Types.RepType
import GHC.StgToCmm.Layout  ( mkVirtConstrSizes )
import GHC.StgToCmm.Closure ( tagForCon, NonVoid (..) )
import GHC.Utils.Misc
import GHC.Utils.Panic

{-
  Manufacturing of info tables for DataCons
-}

-- Make info tables for the data decls in this module
mkITbls :: HscEnv -> [TyCon] -> IO ItblEnv
mkITbls :: HscEnv -> [TyCon] -> IO ItblEnv
mkITbls HscEnv
hsc_env [TyCon]
tcs =
  (ItblEnv -> ItblEnv -> ItblEnv) -> ItblEnv -> [ItblEnv] -> ItblEnv
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr ItblEnv -> ItblEnv -> ItblEnv
forall a. NameEnv a -> NameEnv a -> NameEnv a
plusNameEnv ItblEnv
forall a. NameEnv a
emptyNameEnv ([ItblEnv] -> ItblEnv) -> IO [ItblEnv] -> IO ItblEnv
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
    (TyCon -> IO ItblEnv) -> [TyCon] -> IO [ItblEnv]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (HscEnv -> TyCon -> IO ItblEnv
mkITbl HscEnv
hsc_env) ((TyCon -> Bool) -> [TyCon] -> [TyCon]
forall a. (a -> Bool) -> [a] -> [a]
filter TyCon -> Bool
isDataTyCon [TyCon]
tcs)
 where
  mkITbl :: HscEnv -> TyCon -> IO ItblEnv
  mkITbl :: HscEnv -> TyCon -> IO ItblEnv
mkITbl HscEnv
hsc_env TyCon
tc
    | [DataCon]
dcs [DataCon] -> Int -> Bool
forall a. [a] -> Int -> Bool
`lengthIs` Int
n -- paranoia; this is an assertion.
    = HscEnv -> [DataCon] -> IO ItblEnv
make_constr_itbls HscEnv
hsc_env [DataCon]
dcs
       where
          dcs :: [DataCon]
dcs = TyCon -> [DataCon]
tyConDataCons TyCon
tc
          n :: Int
n   = TyCon -> Int
tyConFamilySize TyCon
tc
  mkITbl HscEnv
_ TyCon
_ = String -> IO ItblEnv
forall a. String -> a
panic String
"mkITbl"

mkItblEnv :: [(Name,ItblPtr)] -> ItblEnv
mkItblEnv :: [(Name, ItblPtr)] -> ItblEnv
mkItblEnv [(Name, ItblPtr)]
pairs = [(Name, (Name, ItblPtr))] -> ItblEnv
forall a. [(Name, a)] -> NameEnv a
mkNameEnv [(Name
n, (Name
n,ItblPtr
p)) | (Name
n,ItblPtr
p) <- [(Name, ItblPtr)]
pairs]

-- Assumes constructors are numbered from zero, not one
make_constr_itbls :: HscEnv -> [DataCon] -> IO ItblEnv
make_constr_itbls :: HscEnv -> [DataCon] -> IO ItblEnv
make_constr_itbls HscEnv
hsc_env [DataCon]
cons =
  [(Name, ItblPtr)] -> ItblEnv
mkItblEnv ([(Name, ItblPtr)] -> ItblEnv)
-> IO [(Name, ItblPtr)] -> IO ItblEnv
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((DataCon, Int) -> IO (Name, ItblPtr))
-> [(DataCon, Int)] -> IO [(Name, ItblPtr)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM ((DataCon -> Int -> IO (Name, ItblPtr))
-> (DataCon, Int) -> IO (Name, ItblPtr)
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry DataCon -> Int -> IO (Name, ItblPtr)
mk_itbl) ([DataCon] -> [Int] -> [(DataCon, Int)]
forall a b. [a] -> [b] -> [(a, b)]
zip [DataCon]
cons [Int
0..])
 where
  dflags :: DynFlags
dflags = HscEnv -> DynFlags
hsc_dflags HscEnv
hsc_env

  mk_itbl :: DataCon -> Int -> IO (Name,ItblPtr)
  mk_itbl :: DataCon -> Int -> IO (Name, ItblPtr)
mk_itbl DataCon
dcon Int
conNo = do
     let rep_args :: [NonVoid PrimRep]
rep_args = [ PrimRep -> NonVoid PrimRep
forall a. a -> NonVoid a
NonVoid PrimRep
prim_rep
                    | Scaled Type
arg <- DataCon -> [Scaled Type]
dataConRepArgTys DataCon
dcon
                    , PrimRep
prim_rep <- HasDebugCallStack => Type -> [PrimRep]
Type -> [PrimRep]
typePrimRep (Scaled Type -> Type
forall a. Scaled a -> a
scaledThing Scaled Type
arg) ]

         (Int
tot_wds, Int
ptr_wds) =
             DynFlags -> [NonVoid PrimRep] -> (Int, Int)
mkVirtConstrSizes DynFlags
dflags [NonVoid PrimRep]
rep_args

         ptrs' :: Int
ptrs'  = Int
ptr_wds
         nptrs' :: Int
nptrs' = Int
tot_wds Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
ptr_wds
         nptrs_really :: Int
nptrs_really
            | Int
ptrs' Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
nptrs' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= DynFlags -> Int
mIN_PAYLOAD_SIZE DynFlags
dflags = Int
nptrs'
            | Bool
otherwise = DynFlags -> Int
mIN_PAYLOAD_SIZE DynFlags
dflags Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
ptrs'

         descr :: ByteString
descr = DataCon -> ByteString
dataConIdentity DataCon
dcon

         platform :: Platform
platform = DynFlags -> Platform
targetPlatform DynFlags
dflags
         tables_next_to_code :: Bool
tables_next_to_code = Platform -> Bool
platformTablesNextToCode Platform
platform

     RemotePtr StgInfoTable
r <- HscEnv
-> Message (RemotePtr StgInfoTable) -> IO (RemotePtr StgInfoTable)
forall a. Binary a => HscEnv -> Message a -> IO a
iservCmd HscEnv
hsc_env (Bool
-> Int
-> Int
-> Int
-> Int
-> ByteString
-> Message (RemotePtr StgInfoTable)
MkConInfoTable Bool
tables_next_to_code Int
ptrs' Int
nptrs_really
                              Int
conNo (DynFlags -> DataCon -> Int
tagForCon DynFlags
dflags DataCon
dcon) ByteString
descr)
     (Name, ItblPtr) -> IO (Name, ItblPtr)
forall (m :: * -> *) a. Monad m => a -> m a
return (DataCon -> Name
forall a. NamedThing a => a -> Name
getName DataCon
dcon, RemotePtr StgInfoTable -> ItblPtr
ItblPtr RemotePtr StgInfoTable
r)