1 -----------------------------------------------------------------------------
3 -- Object-file symbols (called CLabel for histerical raisins).
5 -- (c) The University of Glasgow 2004
7 -----------------------------------------------------------------------------
10 CLabel, -- abstract type
19 mkStaticConEntryLabel,
22 mkStaticInfoTableLabel,
37 mkPlainModuleInitLabel,
44 mkMainCapabilityLabel,
45 mkMAP_FROZEN_infoLabel,
46 mkEMPTY_MVAR_infoLabel,
49 mkCAFBlackHoleInfoTableLabel,
50 mkSECAFBlackHoleInfoTableLabel,
52 mkRtsSlowTickyCtrLabel,
75 mkCCLabel, mkCCSLabel,
77 infoLblToEntryLbl, entryLblToInfoLbl,
78 needsCDecl, isAsmTemp, externallyVisibleCLabel,
79 CLabelType(..), labelType, labelDynamic, labelCouldBeDynamic,
85 #include "HsVersions.h"
86 #include "../includes/ghcconfig.h"
88 import CmdLineOpts ( opt_Static, opt_DoTickyProfiling )
89 import DataCon ( ConTag )
90 import Module ( moduleName, moduleNameFS,
91 Module, isHomeModule )
92 import Name ( Name, isDllName, isExternalName )
93 import Unique ( pprUnique, Unique )
94 import PrimOp ( PrimOp )
95 import Config ( cLeadingUnderscore )
96 import CostCentre ( CostCentre, CostCentreStack )
101 -- -----------------------------------------------------------------------------
105 CLabel is an abstract type that supports the following operations:
109 - In a C file, does it need to be declared before use? (i.e. is it
110 guaranteed to be already in scope in the places we need to refer to it?)
112 - If it needs to be declared, what type (code or data) should it be
115 - Is it visible outside this object file or not?
117 - Is it "dynamic" (see details below)
119 - Eq and Ord, so that we can make sets of CLabels (currently only
120 used in outputting C as far as I can tell, to avoid generating
121 more than one declaration for any given label).
123 - Converting an info table label into an entry label.
127 = IdLabel -- A family of labels related to the
128 Name -- definition of a particular Id or Con
131 | CaseLabel -- A family of labels related to a particular
133 {-# UNPACK #-} !Unique -- Unique says which case expression
137 {-# UNPACK #-} !Unique
140 Module -- the module name
142 -- at some point we might want some kind of version number in
143 -- the module init label, to guard against compiling modules in
144 -- the wrong order. We can't use the interface file version however,
145 -- because we don't always recompile modules which depend on a module
146 -- whose version has changed.
148 | PlainModuleInitLabel Module -- without the vesrion & way info
152 | RtsLabel RtsLabelInfo
154 | ForeignLabel FastString -- a 'C' (or otherwise foreign) label
155 (Maybe Int) -- possible '@n' suffix for stdcall functions
156 -- When generating C, the '@n' suffix is omitted, but when
157 -- generating assembler we must add it to the label.
158 Bool -- True <=> is dynamic
160 | CC_Label CostCentre
161 | CCS_Label CostCentreStack
167 = Closure -- Label for closure
168 | SRT -- Static reference table
169 | SRTDesc -- Static reference table descriptor
170 | InfoTbl -- Info tables for closures; always read-only
171 | Entry -- entry point
172 | Slow -- slow entry point
174 | RednCounts -- Label of place to keep Ticky-ticky info for
177 | Bitmap -- A bitmap (function or case return)
179 | ConEntry -- constructor entry point
180 | ConInfoTbl -- corresponding info table
181 | StaticConEntry -- static constructor entry point
182 | StaticInfoTbl -- corresponding info table
184 | ClosureTable -- table of closures for Enum tycons
198 = RtsShouldNeverHappenCode
200 | RtsSelectorInfoTbl Bool{-updatable-} Int{-offset-} -- Selector thunks
201 | RtsSelectorEntry Bool{-updatable-} Int{-offset-}
203 | RtsApInfoTbl Bool{-updatable-} Int{-arity-} -- AP thunks
204 | RtsApEntry Bool{-updatable-} Int{-arity-}
208 | RtsInfo LitString -- misc rts info tables
209 | RtsEntry LitString -- misc rts entry points
210 | RtsRetInfo LitString -- misc rts ret info tables
211 | RtsRet LitString -- misc rts return points
212 | RtsData LitString -- misc rts data bits, eg CHARLIKE_closure
213 | RtsCode LitString -- misc rts code
215 | RtsInfoFS FastString -- misc rts info tables
216 | RtsEntryFS FastString -- misc rts entry points
217 | RtsRetInfoFS FastString -- misc rts ret info tables
218 | RtsRetFS FastString -- misc rts return points
219 | RtsDataFS FastString -- misc rts data bits, eg CHARLIKE_closure
220 | RtsCodeFS FastString -- misc rts code
222 | RtsSlowTickyCtr String
225 -- NOTE: Eq on LitString compares the pointer only, so this isn't
228 -- -----------------------------------------------------------------------------
229 -- Constructing CLabels
231 mkClosureLabel id = IdLabel id Closure
232 mkSRTLabel id = IdLabel id SRT
233 mkSRTDescLabel id = IdLabel id SRTDesc
234 mkInfoTableLabel id = IdLabel id InfoTbl
235 mkEntryLabel id = IdLabel id Entry
236 mkSlowEntryLabel id = IdLabel id Slow
237 mkBitmapLabel id = IdLabel id Bitmap
238 mkRednCountsLabel id = IdLabel id RednCounts
240 mkConInfoTableLabel con = IdLabel con ConInfoTbl
241 mkConEntryLabel con = IdLabel con ConEntry
242 mkStaticInfoTableLabel con = IdLabel con StaticInfoTbl
243 mkStaticConEntryLabel con = IdLabel con StaticConEntry
245 mkClosureTblLabel id = IdLabel id ClosureTable
247 mkReturnPtLabel uniq = CaseLabel uniq CaseReturnPt
248 mkReturnInfoLabel uniq = CaseLabel uniq CaseReturnInfo
249 mkAltLabel uniq tag = CaseLabel uniq (CaseAlt tag)
250 mkDefaultLabel uniq = CaseLabel uniq CaseDefault
252 mkAsmTempLabel = AsmTempLabel
254 mkModuleInitLabel = ModuleInitLabel
255 mkPlainModuleInitLabel = PlainModuleInitLabel
257 -- Some fixed runtime system labels
259 mkErrorStdEntryLabel = RtsLabel RtsShouldNeverHappenCode
260 mkSplitMarkerLabel = RtsLabel (RtsCode SLIT("__stg_split_marker"))
261 mkUpdInfoLabel = RtsLabel (RtsInfo SLIT("stg_upd_frame"))
262 mkSeqInfoLabel = RtsLabel (RtsInfo SLIT("stg_seq_frame"))
263 mkIndStaticInfoLabel = RtsLabel (RtsInfo SLIT("stg_IND_STATIC"))
264 mkMainCapabilityLabel = RtsLabel (RtsData SLIT("MainCapability"))
265 mkMAP_FROZEN_infoLabel = RtsLabel (RtsInfo SLIT("stg_MUT_ARR_PTRS_FROZEN"))
266 mkEMPTY_MVAR_infoLabel = RtsLabel (RtsInfo SLIT("stg_EMPTY_MVAR"))
268 mkTopTickyCtrLabel = RtsLabel (RtsData SLIT("top_ct"))
269 mkCAFBlackHoleInfoTableLabel = RtsLabel (RtsInfo SLIT("stg_CAF_BLACKHOLE"))
270 mkSECAFBlackHoleInfoTableLabel = if opt_DoTickyProfiling then
271 RtsLabel (RtsInfo SLIT("stg_SE_CAF_BLACKHOLE"))
272 else -- RTS won't have info table unless -ticky is on
273 panic "mkSECAFBlackHoleInfoTableLabel requires -ticky"
274 mkRtsPrimOpLabel primop = RtsLabel (RtsPrimOp primop)
276 moduleRegdLabel = ModuleRegdLabel
278 mkSelectorInfoLabel upd off = RtsLabel (RtsSelectorInfoTbl upd off)
279 mkSelectorEntryLabel upd off = RtsLabel (RtsSelectorEntry upd off)
281 mkApInfoTableLabel upd off = RtsLabel (RtsApInfoTbl upd off)
282 mkApEntryLabel upd off = RtsLabel (RtsApEntry upd off)
286 mkForeignLabel :: FastString -> Maybe Int -> Bool -> CLabel
287 mkForeignLabel str mb_sz is_dynamic = ForeignLabel str mb_sz is_dynamic
291 mkCCLabel cc = CC_Label cc
292 mkCCSLabel ccs = CCS_Label ccs
294 mkRtsInfoLabel str = RtsLabel (RtsInfo str)
295 mkRtsEntryLabel str = RtsLabel (RtsEntry str)
296 mkRtsRetInfoLabel str = RtsLabel (RtsRetInfo str)
297 mkRtsRetLabel str = RtsLabel (RtsRet str)
298 mkRtsCodeLabel str = RtsLabel (RtsCode str)
299 mkRtsDataLabel str = RtsLabel (RtsData str)
301 mkRtsInfoLabelFS str = RtsLabel (RtsInfoFS str)
302 mkRtsEntryLabelFS str = RtsLabel (RtsEntryFS str)
303 mkRtsRetInfoLabelFS str = RtsLabel (RtsRetInfoFS str)
304 mkRtsRetLabelFS str = RtsLabel (RtsRetFS str)
305 mkRtsCodeLabelFS str = RtsLabel (RtsCodeFS str)
306 mkRtsDataLabelFS str = RtsLabel (RtsDataFS str)
308 mkRtsSlowTickyCtrLabel :: String -> CLabel
309 mkRtsSlowTickyCtrLabel pat = RtsLabel (RtsSlowTickyCtr pat)
311 -- -----------------------------------------------------------------------------
312 -- Converting info labels to entry labels.
314 infoLblToEntryLbl :: CLabel -> CLabel
315 infoLblToEntryLbl (IdLabel n InfoTbl) = IdLabel n Entry
316 infoLblToEntryLbl (IdLabel n ConInfoTbl) = IdLabel n ConEntry
317 infoLblToEntryLbl (IdLabel n StaticInfoTbl) = IdLabel n StaticConEntry
318 infoLblToEntryLbl (CaseLabel n CaseReturnInfo) = CaseLabel n CaseReturnPt
319 infoLblToEntryLbl (RtsLabel (RtsInfo s)) = RtsLabel (RtsEntry s)
320 infoLblToEntryLbl (RtsLabel (RtsRetInfo s)) = RtsLabel (RtsRet s)
321 infoLblToEntryLbl (RtsLabel (RtsInfoFS s)) = RtsLabel (RtsEntryFS s)
322 infoLblToEntryLbl (RtsLabel (RtsRetInfoFS s)) = RtsLabel (RtsRetFS s)
323 infoLblToEntryLbl _ = panic "CLabel.infoLblToEntryLbl"
325 entryLblToInfoLbl :: CLabel -> CLabel
326 entryLblToInfoLbl (IdLabel n Entry) = IdLabel n InfoTbl
327 entryLblToInfoLbl (IdLabel n ConEntry) = IdLabel n ConInfoTbl
328 entryLblToInfoLbl (IdLabel n StaticConEntry) = IdLabel n StaticInfoTbl
329 entryLblToInfoLbl (CaseLabel n CaseReturnPt) = CaseLabel n CaseReturnInfo
330 entryLblToInfoLbl (RtsLabel (RtsEntry s)) = RtsLabel (RtsInfo s)
331 entryLblToInfoLbl (RtsLabel (RtsRet s)) = RtsLabel (RtsRetInfo s)
332 entryLblToInfoLbl (RtsLabel (RtsEntryFS s)) = RtsLabel (RtsInfoFS s)
333 entryLblToInfoLbl (RtsLabel (RtsRetFS s)) = RtsLabel (RtsRetInfoFS s)
334 entryLblToInfoLbl l = pprPanic "CLabel.entryLblToInfoLbl" (pprCLabel l)
336 -- -----------------------------------------------------------------------------
337 -- Does a CLabel need declaring before use or not?
339 needsCDecl :: CLabel -> Bool
340 -- False <=> it's pre-declared; don't bother
341 -- don't bother declaring SRT & Bitmap labels, we always make sure
342 -- they are defined before use.
343 needsCDecl (IdLabel _ SRT) = False
344 needsCDecl (IdLabel _ SRTDesc) = False
345 needsCDecl (IdLabel _ Bitmap) = False
346 needsCDecl (IdLabel _ _) = True
347 needsCDecl (CaseLabel _ CaseReturnPt) = True
348 needsCDecl (CaseLabel _ CaseReturnInfo) = True
349 needsCDecl (ModuleInitLabel _ _) = True
350 needsCDecl (PlainModuleInitLabel _) = True
351 needsCDecl ModuleRegdLabel = False
353 needsCDecl (CaseLabel _ _) = False
354 needsCDecl (AsmTempLabel _) = False
355 needsCDecl (RtsLabel _) = False
356 needsCDecl (ForeignLabel _ _ _) = False
357 needsCDecl (CC_Label _) = True
358 needsCDecl (CCS_Label _) = True
360 -- Whether the label is an assembler temporary:
362 isAsmTemp :: CLabel -> Bool -- is a local temporary for native code generation
363 isAsmTemp (AsmTempLabel _) = True
366 -- -----------------------------------------------------------------------------
367 -- Is a CLabel visible outside this object file or not?
369 -- From the point of view of the code generator, a name is
370 -- externally visible if it has to be declared as exported
371 -- in the .o file's symbol table; that is, made non-static.
373 externallyVisibleCLabel :: CLabel -> Bool -- not C "static"
374 externallyVisibleCLabel (CaseLabel _ _) = False
375 externallyVisibleCLabel (AsmTempLabel _) = False
376 externallyVisibleCLabel (ModuleInitLabel _ _)= True
377 externallyVisibleCLabel (PlainModuleInitLabel _)= True
378 externallyVisibleCLabel ModuleRegdLabel = False
379 externallyVisibleCLabel (RtsLabel _) = True
380 externallyVisibleCLabel (ForeignLabel _ _ _) = True
381 externallyVisibleCLabel (IdLabel id _) = isExternalName id
382 externallyVisibleCLabel (CC_Label _) = True
383 externallyVisibleCLabel (CCS_Label _) = True
386 -- -----------------------------------------------------------------------------
387 -- Finding the "type" of a CLabel
389 -- For generating correct types in label declarations:
395 labelType :: CLabel -> CLabelType
396 labelType (RtsLabel (RtsSelectorInfoTbl _ _)) = DataLabel
397 labelType (RtsLabel (RtsApInfoTbl _ _)) = DataLabel
398 labelType (RtsLabel (RtsData _)) = DataLabel
399 labelType (RtsLabel (RtsCode _)) = CodeLabel
400 labelType (RtsLabel (RtsInfo _)) = DataLabel
401 labelType (RtsLabel (RtsEntry _)) = CodeLabel
402 labelType (RtsLabel (RtsRetInfo _)) = DataLabel
403 labelType (RtsLabel (RtsRet _)) = CodeLabel
404 labelType (RtsLabel (RtsDataFS _)) = DataLabel
405 labelType (RtsLabel (RtsCodeFS _)) = CodeLabel
406 labelType (RtsLabel (RtsInfoFS _)) = DataLabel
407 labelType (RtsLabel (RtsEntryFS _)) = CodeLabel
408 labelType (RtsLabel (RtsRetInfoFS _)) = DataLabel
409 labelType (RtsLabel (RtsRetFS _)) = CodeLabel
410 labelType (CaseLabel _ CaseReturnInfo) = DataLabel
411 labelType (CaseLabel _ CaseReturnPt) = CodeLabel
412 labelType (ModuleInitLabel _ _) = CodeLabel
413 labelType (PlainModuleInitLabel _) = CodeLabel
415 labelType (IdLabel _ info) =
420 ConInfoTbl -> DataLabel
421 StaticInfoTbl -> DataLabel
422 ClosureTable -> DataLabel
425 labelType _ = DataLabel
428 -- -----------------------------------------------------------------------------
429 -- Does a CLabel need dynamic linkage?
431 -- When referring to data in code, we need to know whether
432 -- that data resides in a DLL or not. [Win32 only.]
433 -- @labelDynamic@ returns @True@ if the label is located
434 -- in a DLL, be it a data reference or not.
436 labelDynamic :: CLabel -> Bool
439 -- The special case for RtsShouldNeverHappenCode is because the associated address is
440 -- NULL, i.e. not a DLL entry point
441 RtsLabel RtsShouldNeverHappenCode -> False
442 RtsLabel _ -> not opt_Static -- i.e., is the RTS in a DLL or not?
443 IdLabel n k -> isDllName n
444 ForeignLabel _ _ d -> d
445 ModuleInitLabel m _ -> (not opt_Static) && (not (isHomeModule m))
446 PlainModuleInitLabel m -> (not opt_Static) && (not (isHomeModule m))
449 -- Basically the same as above, but this time for Darwin only.
450 -- The things that GHC does when labelDynamic returns true are not quite right
451 -- for Darwin. Also, every ForeignLabel might possibly be from a dynamic library,
452 -- and a 'false positive' doesn't really hurt on Darwin, so this just returns
453 -- True for every ForeignLabel.
455 -- ToDo: Clean up DLL-related code so we can do away with the distinction
456 -- between this and labelDynamic above.
458 labelCouldBeDynamic (ForeignLabel _ _ _) = True
459 labelCouldBeDynamic lbl = labelDynamic lbl
462 OLD?: These GRAN functions are needed for spitting out GRAN_FETCH() at the
463 right places. It is used to detect when the abstractC statement of an
464 CCodeBlock actually contains the code for a slow entry point. -- HWL
466 We need at least @Eq@ for @CLabels@, because we want to avoid
467 duplicate declarations in generating C (see @labelSeenTE@ in
471 -----------------------------------------------------------------------------
472 -- Printing out CLabels.
479 where <name> is <Module>_<name> for external names and <unique> for
480 internal names. <type> is one of the following:
483 srt Static reference table
484 srtd Static reference table descriptor
485 entry Entry code (function, closure)
486 slow Slow entry code (if any)
487 ret Direct return address
489 <n>_alt Case alternative (tag n)
490 dflt Default case alternative
491 btm Large bitmap vector
492 closure Static closure
493 con_entry Dynamic Constructor entry code
494 con_info Dynamic Constructor info table
495 static_entry Static Constructor entry code
496 static_info Static Constructor info table
497 sel_info Selector info table
498 sel_entry Selector entry code
500 ccs Cost centre stack
502 Many of these distinctions are only for documentation reasons. For
503 example, _ret is only distinguished from _entry to make it easy to
504 tell whether a code fragment is a return point or a closure/function
508 pprCLabel :: CLabel -> SDoc
510 #if ! OMIT_NATIVE_CODEGEN
511 pprCLabel (AsmTempLabel u)
512 = getPprStyle $ \ sty ->
514 ptext asmTempLabelPrefix <> pprUnique u
516 char '_' <> pprUnique u
520 #if ! OMIT_NATIVE_CODEGEN
521 getPprStyle $ \ sty ->
523 maybe_underscore (pprAsmCLbl lbl)
529 | underscorePrefix = pp_cSEP <> doc
532 -- In asm mode, we need to put the suffix on a stdcall ForeignLabel.
533 -- (The C compiler does this itself).
534 pprAsmCLbl (ForeignLabel fs (Just sz) _)
535 = ftext fs <> char '@' <> int sz
539 pprCLbl (CaseLabel u CaseReturnPt)
540 = hcat [pprUnique u, ptext SLIT("_ret")]
541 pprCLbl (CaseLabel u CaseReturnInfo)
542 = hcat [pprUnique u, ptext SLIT("_info")]
543 pprCLbl (CaseLabel u (CaseAlt tag))
544 = hcat [pprUnique u, pp_cSEP, int tag, ptext SLIT("_alt")]
545 pprCLbl (CaseLabel u CaseDefault)
546 = hcat [pprUnique u, ptext SLIT("_dflt")]
548 pprCLbl (RtsLabel RtsShouldNeverHappenCode) = ptext SLIT("0")
549 -- used to be stg_error_entry but Windows can't have DLL entry points as static
550 -- initialisers, and besides, this ShouldNeverHappen, right?
552 pprCLbl (RtsLabel (RtsCode str)) = ptext str
553 pprCLbl (RtsLabel (RtsData str)) = ptext str
554 pprCLbl (RtsLabel (RtsCodeFS str)) = ftext str
555 pprCLbl (RtsLabel (RtsDataFS str)) = ftext str
557 pprCLbl (RtsLabel (RtsSelectorInfoTbl upd_reqd offset))
558 = hcat [ptext SLIT("stg_sel_"), text (show offset),
560 then SLIT("_upd_info")
561 else SLIT("_noupd_info"))
564 pprCLbl (RtsLabel (RtsSelectorEntry upd_reqd offset))
565 = hcat [ptext SLIT("stg_sel_"), text (show offset),
567 then SLIT("_upd_entry")
568 else SLIT("_noupd_entry"))
571 pprCLbl (RtsLabel (RtsApInfoTbl upd_reqd arity))
572 = hcat [ptext SLIT("stg_ap_"), text (show arity),
574 then SLIT("_upd_info")
575 else SLIT("_noupd_info"))
578 pprCLbl (RtsLabel (RtsApEntry upd_reqd arity))
579 = hcat [ptext SLIT("stg_ap_"), text (show arity),
581 then SLIT("_upd_entry")
582 else SLIT("_noupd_entry"))
585 pprCLbl (RtsLabel (RtsInfo fs))
586 = ptext fs <> ptext SLIT("_info")
588 pprCLbl (RtsLabel (RtsEntry fs))
589 = ptext fs <> ptext SLIT("_entry")
591 pprCLbl (RtsLabel (RtsRetInfo fs))
592 = ptext fs <> ptext SLIT("_info")
594 pprCLbl (RtsLabel (RtsRet fs))
595 = ptext fs <> ptext SLIT("_ret")
597 pprCLbl (RtsLabel (RtsInfoFS fs))
598 = ftext fs <> ptext SLIT("_info")
600 pprCLbl (RtsLabel (RtsEntryFS fs))
601 = ftext fs <> ptext SLIT("_entry")
603 pprCLbl (RtsLabel (RtsRetInfoFS fs))
604 = ftext fs <> ptext SLIT("_info")
606 pprCLbl (RtsLabel (RtsRetFS fs))
607 = ftext fs <> ptext SLIT("_ret")
609 pprCLbl (RtsLabel (RtsPrimOp primop))
610 = ppr primop <> ptext SLIT("_fast")
612 pprCLbl (RtsLabel (RtsSlowTickyCtr pat))
613 = ptext SLIT("SLOW_CALL_") <> text pat <> ptext SLIT("_ctr")
615 pprCLbl ModuleRegdLabel
616 = ptext SLIT("_module_registered")
618 pprCLbl (ForeignLabel str _ _)
621 pprCLbl (IdLabel id flavor) = ppr id <> ppIdFlavor flavor
623 pprCLbl (CC_Label cc) = ppr cc
624 pprCLbl (CCS_Label ccs) = ppr ccs
626 pprCLbl (ModuleInitLabel mod way)
627 = ptext SLIT("__stginit_") <> ftext (moduleNameFS (moduleName mod))
628 <> char '_' <> text way
629 pprCLbl (PlainModuleInitLabel mod)
630 = ptext SLIT("__stginit_") <> ftext (moduleNameFS (moduleName mod))
632 ppIdFlavor :: IdLabelInfo -> SDoc
633 ppIdFlavor x = pp_cSEP <>
635 Closure -> ptext SLIT("closure")
636 SRT -> ptext SLIT("srt")
637 SRTDesc -> ptext SLIT("srtd")
638 InfoTbl -> ptext SLIT("info")
639 Entry -> ptext SLIT("entry")
640 Slow -> ptext SLIT("slow")
641 RednCounts -> ptext SLIT("ct")
642 Bitmap -> ptext SLIT("btm")
643 ConEntry -> ptext SLIT("con_entry")
644 ConInfoTbl -> ptext SLIT("con_info")
645 StaticConEntry -> ptext SLIT("static_entry")
646 StaticInfoTbl -> ptext SLIT("static_info")
647 ClosureTable -> ptext SLIT("closure_tbl")
653 -- -----------------------------------------------------------------------------
654 -- Machine-dependent knowledge about labels.
656 underscorePrefix :: Bool -- leading underscore on assembler labels?
657 underscorePrefix = (cLeadingUnderscore == "YES")
659 asmTempLabelPrefix :: LitString -- for formatting labels
662 {- The alpha assembler likes temporary labels to look like $L123
663 instead of L123. (Don't toss the L, because then Lf28
667 #elif darwin_TARGET_OS