8 #include "HsVersions.h"
31 -- When we split at proc points, we need an empty info table.
32 emptyContInfoTable :: CmmInfo
34 CmmInfo Nothing Nothing (CmmInfoTable False (ProfilingInfo zero zero) rET_SMALL
35 (ContInfo [] NoC_SRT))
36 where zero = CmmInt 0 wordWidth
38 cmmToRawCmm :: [Cmm] -> IO [RawCmm]
40 info_tbl_uniques <- mkSplitUniqSupply 'i'
41 return $ zipWith raw_cmm (listSplitUniqSupply info_tbl_uniques) cmm
43 raw_cmm uniq_supply (Cmm procs) =
44 Cmm $ concat $ zipWith mkInfoTable (uniqsFromSupply uniq_supply) procs
46 -- Make a concrete info table, represented as a list of CmmStatic
47 -- (it can't be simply a list of Word, because the SRT field is
48 -- represented by a label+offset expression).
50 -- With tablesNextToCode, the layout is
51 -- <reversed variable part>
52 -- <normal forward StgInfoTable, but without
53 -- an entry point at the front>
56 -- Without tablesNextToCode, the layout of an info table is
58 -- <normal forward rest of StgInfoTable>
59 -- <forward variable part>
61 -- See includes/InfoTables.h
63 -- For return-points these are as follows
65 -- Tables next to code:
68 -- <standard info table>
69 -- ret-addr --> <entry code (if any)>
71 -- Not tables-next-to-code:
73 -- ret-addr --> <ptr to entry code>
74 -- <standard info table>
77 -- * The SRT slot is only there if there is SRT info to record
79 mkInfoTable :: Unique -> CmmTop -> [RawCmmTop]
80 mkInfoTable _ (CmmData sec dat) = [CmmData sec dat]
81 mkInfoTable uniq (CmmProc (CmmInfo _ _ info) entry_label arguments blocks) =
83 -- Code without an info table. Easy.
84 CmmNonInfoTable -> [CmmProc [] entry_label arguments blocks]
86 CmmInfoTable _ (ProfilingInfo ty_prof cl_prof) type_tag type_info ->
87 let info_label = entryLblToInfoLbl entry_label
88 ty_prof' = makeRelativeRefTo info_label ty_prof
89 cl_prof' = makeRelativeRefTo info_label cl_prof
91 -- A function entry point.
92 FunInfo (ptrs, nptrs) srt fun_arity pap_bitmap slow_entry ->
93 mkInfoTableAndCode info_label std_info fun_extra_bits entry_label
96 fun_type = argDescrType pap_bitmap
98 [packHalfWordsCLit fun_type fun_arity] ++
101 (if null srt_label then [mkIntCLit 0] else srt_label) ++
102 [makeRelativeRefTo info_label $ mkLivenessCLit liveness,
103 makeRelativeRefTo info_label slow_entry]
105 std_info = mkStdInfoTable ty_prof' cl_prof' type_tag srt_bitmap
107 (srt_label, srt_bitmap) = mkSRTLit info_label srt
108 layout = packHalfWordsCLit ptrs nptrs
111 ConstrInfo (ptrs, nptrs) con_tag descr ->
112 mkInfoTableAndCode info_label std_info [con_name] entry_label
115 std_info = mkStdInfoTable ty_prof' cl_prof' type_tag con_tag layout
116 con_name = makeRelativeRefTo info_label descr
117 layout = packHalfWordsCLit ptrs nptrs
119 ThunkInfo (ptrs, nptrs) srt ->
120 mkInfoTableAndCode info_label std_info srt_label entry_label
123 std_info = mkStdInfoTable ty_prof' cl_prof' type_tag srt_bitmap layout
124 (srt_label, srt_bitmap) = mkSRTLit info_label srt
125 layout = packHalfWordsCLit ptrs nptrs
128 ThunkSelectorInfo offset _srt ->
129 mkInfoTableAndCode info_label std_info [{- no SRT -}] entry_label
132 std_info = mkStdInfoTable ty_prof' cl_prof' type_tag 0 (mkWordCLit offset)
134 -- A continuation/return-point.
135 ContInfo stack_layout srt ->
137 mkInfoTableAndCode info_label std_info srt_label entry_label
140 std_info = mkStdInfoTable ty_prof' cl_prof' maybe_big_type_tag srt_bitmap
141 (makeRelativeRefTo info_label liveness_lit)
142 (liveness_lit, liveness_data, liveness_tag) =
143 mkLiveness uniq stack_layout
144 maybe_big_type_tag = if type_tag == rET_SMALL
147 (srt_label, srt_bitmap) = mkSRTLit info_label srt
149 -- Generate a bare info table, not attached to any procedure.
150 mkBareInfoTable :: CLabel -> Unique -> CmmInfoTable -> [CmmTopZ]
151 mkBareInfoTable lbl uniq info =
152 case mkInfoTable uniq (CmmProc (CmmInfo Nothing Nothing info) lbl [] (ListGraph [])) of
154 ASSERT (tablesNextToCode)
155 [CmmData Data (d ++ [CmmDataLabel (entryLblToInfoLbl lbl)])]
156 [CmmData d s] -> [CmmData d s]
157 _ -> panic "mkBareInfoTable expected to produce only data"
160 -- Handle the differences between tables-next-to-code
161 -- and not tables-next-to-code
162 mkInfoTableAndCode :: CLabel
169 mkInfoTableAndCode info_lbl std_info extra_bits entry_lbl args blocks
170 | tablesNextToCode -- Reverse the extra_bits; and emit the top-level proc
171 = [CmmProc (map CmmStaticLit (reverse extra_bits ++ std_info))
172 entry_lbl args blocks]
174 | ListGraph [] <- blocks -- No code; only the info table is significant
175 = -- Use a zero place-holder in place of the
176 -- entry-label in the info table
177 [mkRODataLits info_lbl (zeroCLit : std_info ++ extra_bits)]
179 | otherwise -- Separately emit info table (with the function entry
180 = -- point as first entry) and the entry code
181 [CmmProc [] entry_lbl args blocks,
182 mkDataLits info_lbl (CmmLabel entry_lbl : std_info ++ extra_bits)]
186 -> ([CmmLit], -- srt_label
187 StgHalfWord) -- srt_bitmap
188 mkSRTLit _ NoC_SRT = ([], 0)
189 mkSRTLit info_label (C_SRT lbl off bitmap) =
190 ([makeRelativeRefTo info_label (cmmLabelOffW lbl off)], bitmap)
192 -------------------------------------------------------------------------
194 -- Build a liveness mask for the stack layout
196 -------------------------------------------------------------------------
198 -- There are four kinds of things on the stack:
200 -- - pointer variables (bound in the environment)
201 -- - non-pointer variables (bound in the environment)
202 -- - free slots (recorded in the stack free list)
203 -- - non-pointer data slots (recorded in the stack free list)
205 -- The first two are represented with a 'Just' of a 'LocalReg'.
206 -- The last two with one or more 'Nothing' constructors.
207 -- Each 'Nothing' represents one used word.
209 -- The head of the stack layout is the top of the stack and
210 -- the least-significant bit.
212 -- TODO: refactor to use utility functions
213 -- TODO: combine with CgCallConv.mkLiveness (see comment there)
216 -> (CmmLit, [RawCmmTop], ClosureTypeTag)
218 -- 1. The bitmap (literal value or label)
219 -- 2. Large bitmap CmmData if needed
220 -- 3. rET_SMALL or rET_BIG
221 mkLiveness uniq live =
222 if length bits > mAX_SMALL_BITMAP_SIZE
223 -- does not fit in one word
224 then (CmmLabel big_liveness, [data_lits], rET_BIG)
226 else (mkWordCLit small_liveness, [], rET_SMALL)
229 mkBits (reg:regs) = take sizeW bits ++ mkBits regs where
232 Just r -> (widthInBytes (typeWidth (localRegType r)) + wORD_SIZE - 1)
234 -- number of words, rounded up
235 bits = repeat $ is_non_ptr reg -- True <=> Non Ptr
237 is_non_ptr Nothing = True
238 is_non_ptr (Just reg) = not $ isGcPtrType (localRegType reg)
244 bitmap = mkBitmap bits
246 small_bitmap = case bitmap of
248 [b] -> fromIntegral b
249 _ -> panic "mkLiveness"
251 fromIntegral (length bits) .|. (small_bitmap `shiftL` bITMAP_BITS_SHIFT)
253 big_liveness = mkBitmapLabel uniq
254 lits = mkWordCLit (fromIntegral (length bits)) : map mkWordCLit bitmap
255 data_lits = mkRODataLits big_liveness lits
257 -------------------------------------------------------------------------
259 -- Generating a standard info table
261 -------------------------------------------------------------------------
263 -- The standard bits of an info table. This part of the info table
264 -- corresponds to the StgInfoTable type defined in InfoTables.h.
266 -- Its shape varies with ticky/profiling/tables next to code etc
267 -- so we can't use constant offsets from Constants
270 :: CmmLit -- closure type descr (profiling)
271 -> CmmLit -- closure descr (profiling)
272 -> StgHalfWord -- closure type
273 -> StgHalfWord -- SRT length
274 -> CmmLit -- layout field
277 mkStdInfoTable type_descr closure_descr cl_type srt_len layout_lit
278 = -- Parallel revertible-black hole field
280 -- Ticky info (none at present)
281 -- Debug info (none at present)
282 ++ [layout_lit, type_lit]
286 | opt_SccProfilingOn = [type_descr, closure_descr]
289 type_lit = packHalfWordsCLit cl_type srt_len