1 -----------------------------------------------------------------------------
3 -- Building info tables.
5 -- (c) The University of Glasgow 2004
7 -----------------------------------------------------------------------------
10 emitClosureCodeAndInfoTable,
14 emitDirectReturnTarget, emitAlgReturnTarget,
15 emitDirectReturnInstr, emitVectoredReturnInstr,
19 mkFunGenInfoExtraBits,
20 entryCode, closureInfoPtr,
22 infoTable, infoTableClosureType,
23 infoTablePtrs, infoTableNonPtrs,
29 #include "HsVersions.h"
31 import ClosureInfo ( ClosureInfo, closureTypeDescr, closureName,
32 infoTableLabelFromCI, Liveness,
33 closureValDescr, closureSRT, closureSMRep,
34 closurePtrsSize, closureNonHdrSize, closureFunInfo,
35 C_SRT(..), needsSRT, isConstrClosure_maybe,
37 import SMRep ( StgHalfWord, hALF_WORD_SIZE_IN_BITS, hALF_WORD_SIZE,
39 smRepClosureTypeInt, tablesNextToCode,
40 rET_BIG, rET_SMALL, rET_VEC_BIG, rET_VEC_SMALL )
41 import CgBindery ( getLiveStackSlots )
42 import CgCallConv ( isBigLiveness, mkLivenessCLit, buildContLiveness,
43 argDescrType, getSequelAmode,
44 CtrlReturnConvention(..) )
45 import CgUtils ( mkStringCLit, packHalfWordsCLit, mkWordCLit,
46 cmmOffsetB, cmmOffsetExprW, cmmLabelOffW, cmmOffsetW,
47 emitDataLits, emitRODataLits, emitSwitch, cmmNegate,
51 import CmmUtils ( mkIntCLit, zeroCLit )
52 import Cmm ( CmmStmt(..), CmmExpr(..), CmmLit(..), LocalReg,
53 CmmBasicBlock, nodeReg )
54 import MachOp ( MachOp(..), wordRep, halfWordRep )
56 import StgSyn ( SRT(..) )
58 import DataCon ( DataCon, dataConTag, fIRST_TAG )
59 import Unique ( Uniquable(..) )
60 import DynFlags ( DynFlags(..), HscTarget(..) )
61 import StaticFlags ( opt_SccProfilingOn )
62 import ListSetOps ( assocDefault )
63 import Maybes ( isJust )
64 import Constants ( wORD_SIZE, sIZEOF_StgFunInfoExtraRev )
68 -------------------------------------------------------------------------
70 -- Generating the info table and code for a closure
72 -------------------------------------------------------------------------
74 -- Here we make a concrete info table, represented as a list of CmmAddr
75 -- (it can't be simply a list of Word, because the SRT field is
76 -- represented by a label+offset expression).
78 -- With tablesNextToCode, the layout is
79 -- <reversed variable part>
80 -- <normal forward StgInfoTable, but without
81 -- an entry point at the front>
84 -- Without tablesNextToCode, the layout of an info table is
86 -- <normal forward rest of StgInfoTable>
87 -- <forward variable part>
89 -- See includes/InfoTables.h
91 emitClosureCodeAndInfoTable :: ClosureInfo -> [LocalReg] -> CgStmts -> Code
92 emitClosureCodeAndInfoTable cl_info args body
93 = do { ty_descr_lit <-
95 then mkStringCLit (closureTypeDescr cl_info)
96 else return (mkIntCLit 0)
99 then mkStringCLit cl_descr_string
100 else return (mkIntCLit 0)
101 ; let std_info = mkStdInfoTable ty_descr_lit cl_descr_lit
102 cl_type srt_len layout_lit
104 ; blks <- cgStmtsToBlocks body
105 ; emitInfoTableAndCode info_lbl std_info extra_bits args blks }
107 info_lbl = infoTableLabelFromCI cl_info
109 cl_descr_string = closureValDescr cl_info
110 cl_type = smRepClosureTypeInt (closureSMRep cl_info)
112 srt = closureSRT cl_info
113 needs_srt = needsSRT srt
115 mb_con = isConstrClosure_maybe cl_info
116 is_con = isJust mb_con
120 Just con -> -- Constructors don't have an SRT
121 -- We keep the *zero-indexed* tag in the srt_len
122 -- field of the info table.
123 (mkIntCLit 0, fromIntegral (dataConTagZ con))
125 Nothing -> -- Not a constructor
126 srtLabelAndLength srt info_lbl
128 ptrs = closurePtrsSize cl_info
130 size = closureNonHdrSize cl_info
131 layout_lit = packHalfWordsCLit ptrs nptrs
134 | is_fun = fun_extra_bits
136 | needs_srt = [srt_label]
139 maybe_fun_stuff = closureFunInfo cl_info
140 is_fun = isJust maybe_fun_stuff
141 (Just (arity, arg_descr)) = maybe_fun_stuff
144 | ArgGen liveness <- arg_descr
147 makeRelativeRefTo info_lbl $ mkLivenessCLit liveness,
149 | needs_srt = [fun_amode, srt_label]
150 | otherwise = [fun_amode]
152 slow_entry = makeRelativeRefTo info_lbl (CmmLabel slow_entry_label)
153 slow_entry_label = mkSlowEntryLabel (closureName cl_info)
155 fun_amode = packHalfWordsCLit fun_type arity
156 fun_type = argDescrType arg_descr
158 -- We keep the *zero-indexed* tag in the srt_len field of the info
159 -- table of a data constructor.
160 dataConTagZ :: DataCon -> ConTagZ
161 dataConTagZ con = dataConTag con - fIRST_TAG
163 -- A low-level way to generate the variable part of a fun-style info table.
164 -- (must match fun_extra_bits above). Used by the C-- parser.
165 mkFunGenInfoExtraBits :: Int -> Int -> CmmLit -> CmmLit -> CmmLit -> [CmmLit]
166 mkFunGenInfoExtraBits fun_type arity srt_label liveness slow_entry
167 = [ packHalfWordsCLit fun_type arity,
172 -------------------------------------------------------------------------
174 -- Generating the info table and code for a return point
176 -------------------------------------------------------------------------
178 -- Here's the layout of a return-point info table
180 -- Tables next to code:
182 -- <reversed vector table>
184 -- <standard info table>
185 -- ret-addr --> <entry code (if any)>
187 -- Not tables-next-to-code:
189 -- ret-addr --> <ptr to entry code>
190 -- <standard info table>
192 -- <forward vector table>
194 -- * The vector table is only present for vectored returns
196 -- * The SRT slot is only there if either
197 -- (a) there is SRT info to record, OR
198 -- (b) if the return is vectored
199 -- The latter (b) is necessary so that the vector is in a
202 vectorSlot :: CmmExpr -> CmmExpr -> CmmExpr
203 -- Get the vector slot from the info pointer
204 vectorSlot info_amode zero_indexed_tag
206 = cmmOffsetExprW (cmmOffsetW info_amode (- (stdInfoTableSizeW + 2)))
207 (cmmNegate zero_indexed_tag)
208 -- The "2" is one for the SRT slot, and one more
209 -- to get to the first word of the vector
212 = cmmOffsetExprW (cmmOffsetW info_amode (stdInfoTableSizeW + 2))
214 -- The "2" is one for the entry-code slot and one for the SRT slot
216 retVec :: CmmExpr -> CmmExpr -> CmmExpr
217 -- Get a return vector from the info pointer
218 retVec info_amode zero_indexed_tag
219 = let slot = vectorSlot info_amode zero_indexed_tag
220 tableEntry = CmmLoad slot wordRep
221 in if tablesNextToCode
222 then CmmMachOp (MO_Add wordRep) [tableEntry, info_amode]
227 -> CgStmts -- The direct-return code (if any)
228 -- (empty for vectored returns)
229 -> [CmmLit] -- Vector of return points
230 -- (empty for non-vectored returns)
233 emitReturnTarget name stmts vector srt
234 = do { live_slots <- getLiveStackSlots
235 ; liveness <- buildContLiveness name live_slots
236 ; srt_info <- getSRTInfo name srt
239 cl_type = case (null vector, isBigLiveness liveness) of
240 (True, True) -> rET_BIG
241 (True, False) -> rET_SMALL
242 (False, True) -> rET_VEC_BIG
243 (False, False) -> rET_VEC_SMALL
245 (std_info, extra_bits) =
246 mkRetInfoTable info_lbl liveness srt_info cl_type vector
248 ; blks <- cgStmtsToBlocks stmts
249 ; emitInfoTableAndCode info_lbl std_info extra_bits args blks
252 args = trace "emitReturnTarget: missing args" []
253 uniq = getUnique name
254 info_lbl = mkReturnInfoLabel uniq
258 :: CLabel -- info label
259 -> Liveness -- liveness
261 -> Int -- type (eg. rET_SMALL)
262 -> [CmmLit] -- vector
263 -> ([CmmLit],[CmmLit])
264 mkRetInfoTable info_lbl liveness srt_info cl_type vector
265 = (std_info, extra_bits)
267 (srt_label, srt_len) = srtLabelAndLength srt_info info_lbl
269 srt_slot | need_srt = [srt_label]
272 need_srt = needsSRT srt_info || not (null vector)
273 -- If there's a vector table then we must allocate
274 -- an SRT slot, so that the vector table is at a
275 -- known offset from the info pointer
277 liveness_lit = makeRelativeRefTo info_lbl $ mkLivenessCLit liveness
278 std_info = mkStdInfoTable zeroCLit zeroCLit cl_type srt_len liveness_lit
279 extra_bits = srt_slot ++ map (makeRelativeRefTo info_lbl) vector
282 emitDirectReturnTarget
284 -> CgStmts -- The direct-return code
287 emitDirectReturnTarget name code srt
288 = emitReturnTarget name code [] srt
291 :: Name -- Just for its unique
292 -> [(ConTagZ, CgStmts)] -- Tagged branches
293 -> Maybe CgStmts -- Default branch (if any)
294 -> SRT -- Continuation's SRT
295 -> CtrlReturnConvention
296 -> FCode (CLabel, SemiTaggingStuff)
298 emitAlgReturnTarget name branches mb_deflt srt ret_conv
300 UnvectoredReturn fam_sz -> do
301 { blks <- getCgStmts $
302 emitSwitch tag_expr branches mb_deflt 0 (fam_sz - 1)
303 -- NB: tag_expr is zero-based
304 ; lbl <- emitDirectReturnTarget name blks srt
305 ; return (lbl, Nothing) }
306 -- Nothing: the internal branches in the switch don't have
307 -- global labels, so we can't use them at the 'call site'
309 VectoredReturn fam_sz -> do
310 { let tagged_lbls = zip (map fst branches) $
311 map (CmmLabel . mkAltLabel uniq . fst) branches
312 deflt_lbl | isJust mb_deflt = CmmLabel $ mkDefaultLabel uniq
313 | otherwise = mkIntCLit 0
314 ; let vector = [ assocDefault deflt_lbl tagged_lbls i
315 | i <- [0..fam_sz-1]]
316 ; lbl <- emitReturnTarget name noCgStmts vector srt
317 ; mapFCs emit_alt branches
318 ; emit_deflt mb_deflt
319 ; return (lbl, Just (tagged_lbls, deflt_lbl)) }
321 uniq = getUnique name
322 tag_expr = getConstrTag (CmmReg nodeReg)
324 emit_alt :: (Int, CgStmts) -> FCode (Int, CmmLit)
325 -- Emit the code for the alternative as a top-level
326 -- code block returning a label for it
327 emit_alt (tag, stmts) = do { let lbl = mkAltLabel uniq tag
328 ; blks <- cgStmtsToBlocks stmts
329 ; emitProc [] lbl [] blks
330 ; return (tag, CmmLabel lbl) }
332 emit_deflt (Just stmts) = do { let lbl = mkDefaultLabel uniq
333 ; blks <- cgStmtsToBlocks stmts
334 ; emitProc [] lbl [] blks
335 ; return (CmmLabel lbl) }
336 emit_deflt Nothing = return (mkIntCLit 0)
337 -- Nothing case: the simplifier might have eliminated a case
338 -- so we may have e.g. case xs of
340 -- In that situation the default should never be taken,
341 -- so we just use a NULL pointer
343 --------------------------------
344 emitDirectReturnInstr :: Code
345 emitDirectReturnInstr
346 = do { info_amode <- getSequelAmode
347 ; stmtC (CmmJump (entryCode info_amode) []) }
349 emitVectoredReturnInstr :: CmmExpr -- _Zero-indexed_ constructor tag
351 emitVectoredReturnInstr zero_indexed_tag
352 = do { info_amode <- getSequelAmode
353 -- HACK! assign info_amode to a temp, because retVec
354 -- uses it twice and the NCG doesn't have any CSE yet.
355 -- Only do this for the NCG, because gcc is too stupid
356 -- to optimise away the extra tmp (grrr).
357 ; dflags <- getDynFlags
358 ; x <- if hscTarget dflags == HscAsm
359 then do z <- newTemp wordRep
360 stmtC (CmmAssign z info_amode)
364 ; let target = retVec x zero_indexed_tag
365 ; stmtC (CmmJump target []) }
368 -------------------------------------------------------------------------
370 -- Generating a standard info table
372 -------------------------------------------------------------------------
374 -- The standard bits of an info table. This part of the info table
375 -- corresponds to the StgInfoTable type defined in InfoTables.h.
377 -- Its shape varies with ticky/profiling/tables next to code etc
378 -- so we can't use constant offsets from Constants
381 :: CmmLit -- closure type descr (profiling)
382 -> CmmLit -- closure descr (profiling)
383 -> Int -- closure type
384 -> StgHalfWord -- SRT length
385 -> CmmLit -- layout field
388 mkStdInfoTable type_descr closure_descr cl_type srt_len layout_lit
389 = -- Parallel revertible-black hole field
391 -- Ticky info (none at present)
392 -- Debug info (none at present)
393 ++ [layout_lit, type_lit]
397 | opt_SccProfilingOn = [type_descr, closure_descr]
400 type_lit = packHalfWordsCLit cl_type srt_len
402 stdInfoTableSizeW :: WordOff
403 -- The size of a standard info table varies with profiling/ticky etc,
404 -- so we can't get it from Constants
405 -- It must vary in sync with mkStdInfoTable
407 = size_fixed + size_prof
409 size_fixed = 2 -- layout, type
410 size_prof | opt_SccProfilingOn = 2
413 stdInfoTableSizeB = stdInfoTableSizeW * wORD_SIZE :: ByteOff
415 stdSrtBitmapOffset :: ByteOff
416 -- Byte offset of the SRT bitmap half-word which is
417 -- in the *higher-addressed* part of the type_lit
418 stdSrtBitmapOffset = stdInfoTableSizeB - hALF_WORD_SIZE
420 stdClosureTypeOffset :: ByteOff
421 -- Byte offset of the closure type half-word
422 stdClosureTypeOffset = stdInfoTableSizeB - wORD_SIZE
424 stdPtrsOffset, stdNonPtrsOffset :: ByteOff
425 stdPtrsOffset = stdInfoTableSizeB - 2*wORD_SIZE
426 stdNonPtrsOffset = stdInfoTableSizeB - 2*wORD_SIZE + hALF_WORD_SIZE
428 -------------------------------------------------------------------------
430 -- Accessing fields of an info table
432 -------------------------------------------------------------------------
434 closureInfoPtr :: CmmExpr -> CmmExpr
435 -- Takes a closure pointer and returns the info table pointer
436 closureInfoPtr e = CmmLoad e wordRep
438 entryCode :: CmmExpr -> CmmExpr
439 -- Takes an info pointer (the first word of a closure)
440 -- and returns its entry code
441 entryCode e | tablesNextToCode = e
442 | otherwise = CmmLoad e wordRep
444 getConstrTag :: CmmExpr -> CmmExpr
445 -- Takes a closure pointer, and return the *zero-indexed*
446 -- constructor tag obtained from the info table
447 -- This lives in the SRT field of the info table
448 -- (constructors don't need SRTs).
449 getConstrTag closure_ptr
450 = CmmMachOp (MO_U_Conv halfWordRep wordRep) [infoTableConstrTag info_table]
452 info_table = infoTable (closureInfoPtr closure_ptr)
454 infoTable :: CmmExpr -> CmmExpr
455 -- Takes an info pointer (the first word of a closure)
456 -- and returns a pointer to the first word of the standard-form
457 -- info table, excluding the entry-code word (if present)
459 | tablesNextToCode = cmmOffsetB info_ptr (- stdInfoTableSizeB)
460 | otherwise = cmmOffsetW info_ptr 1 -- Past the entry code pointer
462 infoTableConstrTag :: CmmExpr -> CmmExpr
463 -- Takes an info table pointer (from infoTable) and returns the constr tag
464 -- field of the info table (same as the srt_bitmap field)
465 infoTableConstrTag = infoTableSrtBitmap
467 infoTableSrtBitmap :: CmmExpr -> CmmExpr
468 -- Takes an info table pointer (from infoTable) and returns the srt_bitmap
469 -- field of the info table
470 infoTableSrtBitmap info_tbl
471 = CmmLoad (cmmOffsetB info_tbl stdSrtBitmapOffset) halfWordRep
473 infoTableClosureType :: CmmExpr -> CmmExpr
474 -- Takes an info table pointer (from infoTable) and returns the closure type
475 -- field of the info table.
476 infoTableClosureType info_tbl
477 = CmmLoad (cmmOffsetB info_tbl stdClosureTypeOffset) halfWordRep
479 infoTablePtrs :: CmmExpr -> CmmExpr
480 infoTablePtrs info_tbl
481 = CmmLoad (cmmOffsetB info_tbl stdPtrsOffset) halfWordRep
483 infoTableNonPtrs :: CmmExpr -> CmmExpr
484 infoTableNonPtrs info_tbl
485 = CmmLoad (cmmOffsetB info_tbl stdNonPtrsOffset) halfWordRep
487 funInfoTable :: CmmExpr -> CmmExpr
488 -- Takes the info pointer of a function,
489 -- and returns a pointer to the first word of the StgFunInfoExtra struct
490 -- in the info table.
491 funInfoTable info_ptr
493 = cmmOffsetB info_ptr (- stdInfoTableSizeB - sIZEOF_StgFunInfoExtraRev)
495 = cmmOffsetW info_ptr (1 + stdInfoTableSizeW)
496 -- Past the entry code pointer
498 -------------------------------------------------------------------------
500 -- Emit the code for a closure (or return address)
501 -- and its associated info table
503 -------------------------------------------------------------------------
505 -- The complication here concerns whether or not we can
506 -- put the info table next to the code
509 :: CLabel -- Label of info table
510 -> [CmmLit] -- ...its invariant part
511 -> [CmmLit] -- ...and its variant part
512 -> [LocalReg] -- ...args
513 -> [CmmBasicBlock] -- ...and body
516 emitInfoTableAndCode info_lbl std_info extra_bits args blocks
517 | tablesNextToCode -- Reverse the extra_bits; and emit the top-level proc
518 = emitProc (reverse extra_bits ++ std_info)
519 entry_lbl args blocks
520 -- NB: the info_lbl is discarded
522 | null blocks -- No actual code; only the info table is significant
523 = -- Use a zero place-holder in place of the
524 -- entry-label in the info table
525 emitRODataLits info_lbl (zeroCLit : std_info ++ extra_bits)
527 | otherwise -- Separately emit info table (with the function entry
528 = -- point as first entry) and the entry code
529 do { emitDataLits info_lbl (CmmLabel entry_lbl : std_info ++ extra_bits)
530 ; emitProc [] entry_lbl args blocks }
533 entry_lbl = infoLblToEntryLbl info_lbl
535 -------------------------------------------------------------------------
537 -- Static reference tables
539 -------------------------------------------------------------------------
541 -- There is just one SRT for each top level binding; all the nested
542 -- bindings use sub-sections of this SRT. The label is passed down to
543 -- the nested bindings via the monad.
545 getSRTInfo :: Name -> SRT -> FCode C_SRT
546 getSRTInfo id NoSRT = return NoC_SRT
547 getSRTInfo id (SRT off len bmp)
548 | len > hALF_WORD_SIZE_IN_BITS || bmp == [fromIntegral srt_escape]
549 = do { srt_lbl <- getSRTLabel
550 ; let srt_desc_lbl = mkSRTDescLabel id
551 ; emitRODataLits srt_desc_lbl
552 ( cmmLabelOffW srt_lbl off
553 : mkWordCLit (fromIntegral len)
554 : map mkWordCLit bmp)
555 ; return (C_SRT srt_desc_lbl 0 srt_escape) }
558 = do { srt_lbl <- getSRTLabel
559 ; return (C_SRT srt_lbl off (fromIntegral (head bmp))) }
560 -- The fromIntegral converts to StgHalfWord
562 srt_escape = (-1) :: StgHalfWord
564 srtLabelAndLength :: C_SRT -> CLabel -> (CmmLit, StgHalfWord)
565 srtLabelAndLength NoC_SRT _
567 srtLabelAndLength (C_SRT lbl off bitmap) info_lbl
568 = (makeRelativeRefTo info_lbl $ cmmLabelOffW lbl off, bitmap)
570 -------------------------------------------------------------------------
572 -- Position independent code
574 -------------------------------------------------------------------------
575 -- In order to support position independent code, we mustn't put absolute
576 -- references into read-only space. Info tables in the tablesNextToCode
577 -- case must be in .text, which is read-only, so we doctor the CmmLits
578 -- to use relative offsets instead.
580 -- Note that this is done even when the -fPIC flag is not specified,
581 -- as we want to keep binary compatibility between PIC and non-PIC.
583 makeRelativeRefTo :: CLabel -> CmmLit -> CmmLit
585 makeRelativeRefTo info_lbl (CmmLabel lbl)
587 = CmmLabelDiffOff lbl info_lbl 0
588 makeRelativeRefTo info_lbl (CmmLabelOff lbl off)
590 = CmmLabelDiffOff lbl info_lbl off
591 makeRelativeRefTo _ lit = lit