Groom comments related to StaticPointers.
[ghc.git] / compiler / deSugar / StaticPtrTable.hs
1 -- | Code generation for the Static Pointer Table
2 --
3 -- (c) 2014 I/O Tweag
4 --
5 -- Each module that uses 'static' keyword declares an initialization function of
6 -- the form hs_spt_init_<module>() which is emitted into the _stub.c file and
7 -- annotated with __attribute__((constructor)) so that it gets executed at
8 -- startup time.
9 --
10 -- The function's purpose is to call hs_spt_insert to insert the static
11 -- pointers of this module in the hashtable of the RTS, and it looks something
12 -- like this:
13 --
14 -- > static void hs_hpc_init_Main(void) __attribute__((constructor));
15 -- > static void hs_hpc_init_Main(void) {
16 -- >
17 -- > static StgWord64 k0[2] = {16252233372134256ULL,7370534374096082ULL};
18 -- > extern StgPtr Main_sptEntryZC0_closure;
19 -- > hs_spt_insert(k0, &Main_sptEntryZC0_closure);
20 -- >
21 -- > static StgWord64 k1[2] = {12545634534567898ULL,5409674567544151ULL};
22 -- > extern StgPtr Main_sptEntryZC1_closure;
23 -- > hs_spt_insert(k1, &Main_sptEntryZC1_closure);
24 -- >
25 -- > }
26 --
27 -- where the constants are fingerprints produced from the static forms.
28 --
29 module StaticPtrTable (sptInitCode) where
30
31 import CoreSyn
32 import Module
33 import Outputable
34 import Id
35 import CLabel
36 import GHC.Fingerprint
37
38
39 -- | @sptInitCode module statics@ is a C stub to insert the static entries
40 -- @statics@ of @module@ into the static pointer table.
41 --
42 -- Each entry contains the fingerprint used to locate the entry and the
43 -- top-level binding for the entry.
44 --
45 sptInitCode :: Module -> [(Fingerprint, (Id,CoreExpr))] -> SDoc
46 sptInitCode _ [] = Outputable.empty
47 sptInitCode this_mod entries = vcat
48 [ text "static void hs_spt_init_" <> ppr this_mod
49 <> text "(void) __attribute__((constructor));"
50 , text "static void hs_spt_init_" <> ppr this_mod <> text "(void)"
51 , braces $ vcat $
52 [ text "static StgWord64 k" <> int i <> text "[2] = "
53 <> pprFingerprint fp <> semi
54 $$ text "extern StgPtr "
55 <> (ppr $ mkClosureLabel (idName n) (idCafInfo n)) <> semi
56 $$ text "hs_spt_insert" <> parens
57 (hcat $ punctuate comma
58 [ char 'k' <> int i
59 , char '&' <> ppr (mkClosureLabel (idName n) (idCafInfo n))
60 ]
61 )
62 <> semi
63 | (i, (fp, (n, _))) <- zip [0..] entries
64 ]
65 ]
66
67 where
68
69 pprFingerprint :: Fingerprint -> SDoc
70 pprFingerprint (Fingerprint w1 w2) =
71 braces $ hcat $ punctuate comma
72 [ integer (fromIntegral w1) <> text "ULL"
73 , integer (fromIntegral w2) <> text "ULL"
74 ]