4e1c828a4d53b8cdd6e3e318a822d7fd63b761f3
[ghc.git] / compiler / ghci / ByteCodeItbls.hs
1 {-# LANGUAGE CPP, MagicHash, ScopedTypeVariables #-}
2 {-# OPTIONS_GHC -optc-DNON_POSIX_SOURCE #-}
3 --
4 -- (c) The University of Glasgow 2002-2006
5 --
6
7 -- | ByteCodeItbls: Generate infotables for interpreter-made bytecodes
8 module ByteCodeItbls ( mkITbls ) where
9
10 #include "HsVersions.h"
11
12 import ByteCodeTypes
13 import GHCi
14 import DynFlags
15 import HscTypes
16 import Name ( Name, getName )
17 import NameEnv
18 import DataCon ( DataCon, dataConRepArgTys, dataConIdentity )
19 import TyCon ( TyCon, tyConFamilySize, isDataTyCon, tyConDataCons )
20 import Type ( flattenRepType, repType, typePrimRep )
21 import StgCmmLayout ( mkVirtHeapOffsets )
22 import Util
23 import Panic
24
25 {-
26 Manufacturing of info tables for DataCons
27 -}
28
29 -- Make info tables for the data decls in this module
30 mkITbls :: HscEnv -> [TyCon] -> IO ItblEnv
31 mkITbls hsc_env tcs =
32 foldr plusNameEnv emptyNameEnv <$>
33 mapM (mkITbl hsc_env) (filter isDataTyCon tcs)
34 where
35 mkITbl :: HscEnv -> TyCon -> IO ItblEnv
36 mkITbl hsc_env tc
37 | dcs `lengthIs` n -- paranoia; this is an assertion.
38 = make_constr_itbls hsc_env dcs
39 where
40 dcs = tyConDataCons tc
41 n = tyConFamilySize tc
42 mkITbl _ _ = panic "mkITbl"
43
44 mkItblEnv :: [(Name,ItblPtr)] -> ItblEnv
45 mkItblEnv pairs = mkNameEnv [(n, (n,p)) | (n,p) <- pairs]
46
47 -- Assumes constructors are numbered from zero, not one
48 make_constr_itbls :: HscEnv -> [DataCon] -> IO ItblEnv
49 make_constr_itbls hsc_env cons =
50 mkItblEnv <$> mapM (uncurry mk_itbl) (zip cons [0..])
51 where
52 dflags = hsc_dflags hsc_env
53
54 mk_itbl :: DataCon -> Int -> IO (Name,ItblPtr)
55 mk_itbl dcon conNo = do
56 let rep_args = [ (typePrimRep rep_arg,rep_arg)
57 | arg <- dataConRepArgTys dcon
58 , rep_arg <- flattenRepType (repType arg) ]
59
60 (tot_wds, ptr_wds, _) =
61 mkVirtHeapOffsets dflags False{-not a THUNK-} rep_args
62
63 ptrs' = ptr_wds
64 nptrs' = tot_wds - ptr_wds
65 nptrs_really
66 | ptrs' + nptrs' >= mIN_PAYLOAD_SIZE dflags = nptrs'
67 | otherwise = mIN_PAYLOAD_SIZE dflags - ptrs'
68
69 descr = dataConIdentity dcon
70
71 r <- iservCmd hsc_env (MkConInfoTable ptrs' nptrs_really conNo descr)
72 return (getName dcon, ItblPtr r)