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,
38 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 {-# UNPACK #-} !Unique
143 Module -- the module name
145 -- at some point we might want some kind of version number in
146 -- the module init label, to guard against compiling modules in
147 -- the wrong order. We can't use the interface file version however,
148 -- because we don't always recompile modules which depend on a module
149 -- whose version has changed.
151 | PlainModuleInitLabel Module -- without the vesrion & way info
155 | RtsLabel RtsLabelInfo
157 | ForeignLabel FastString -- a 'C' (or otherwise foreign) label
158 (Maybe Int) -- possible '@n' suffix for stdcall functions
159 -- When generating C, the '@n' suffix is omitted, but when
160 -- generating assembler we must add it to the label.
161 Bool -- True <=> is dynamic
163 | CC_Label CostCentre
164 | CCS_Label CostCentreStack
170 = Closure -- Label for closure
171 | SRT -- Static reference table
172 | SRTDesc -- Static reference table descriptor
173 | InfoTbl -- Info tables for closures; always read-only
174 | Entry -- entry point
175 | Slow -- slow entry point
177 | RednCounts -- Label of place to keep Ticky-ticky info for
180 | Bitmap -- A bitmap (function or case return)
182 | ConEntry -- constructor entry point
183 | ConInfoTbl -- corresponding info table
184 | StaticConEntry -- static constructor entry point
185 | StaticInfoTbl -- corresponding info table
187 | ClosureTable -- table of closures for Enum tycons
201 = RtsSelectorInfoTbl Bool{-updatable-} Int{-offset-} -- Selector thunks
202 | RtsSelectorEntry Bool{-updatable-} Int{-offset-}
204 | RtsApInfoTbl Bool{-updatable-} Int{-arity-} -- AP thunks
205 | RtsApEntry Bool{-updatable-} Int{-arity-}
209 | RtsInfo LitString -- misc rts info tables
210 | RtsEntry LitString -- misc rts entry points
211 | RtsRetInfo LitString -- misc rts ret info tables
212 | RtsRet LitString -- misc rts return points
213 | RtsData LitString -- misc rts data bits, eg CHARLIKE_closure
214 | RtsCode LitString -- misc rts code
216 | RtsInfoFS FastString -- misc rts info tables
217 | RtsEntryFS FastString -- misc rts entry points
218 | RtsRetInfoFS FastString -- misc rts ret info tables
219 | RtsRetFS FastString -- misc rts return points
220 | RtsDataFS FastString -- misc rts data bits, eg CHARLIKE_closure
221 | RtsCodeFS FastString -- misc rts code
223 | RtsSlowTickyCtr String
226 -- NOTE: Eq on LitString compares the pointer only, so this isn't
229 -- -----------------------------------------------------------------------------
230 -- Constructing CLabels
232 mkClosureLabel id = IdLabel id Closure
233 mkSRTLabel id = IdLabel id SRT
234 mkSRTDescLabel id = IdLabel id SRTDesc
235 mkInfoTableLabel id = IdLabel id InfoTbl
236 mkEntryLabel id = IdLabel id Entry
237 mkSlowEntryLabel id = IdLabel id Slow
238 mkBitmapLabel id = IdLabel id Bitmap
239 mkRednCountsLabel id = IdLabel id RednCounts
241 mkConInfoTableLabel con = IdLabel con ConInfoTbl
242 mkConEntryLabel con = IdLabel con ConEntry
243 mkStaticInfoTableLabel con = IdLabel con StaticInfoTbl
244 mkStaticConEntryLabel con = IdLabel con StaticConEntry
246 mkClosureTblLabel id = IdLabel id ClosureTable
248 mkReturnPtLabel uniq = CaseLabel uniq CaseReturnPt
249 mkReturnInfoLabel uniq = CaseLabel uniq CaseReturnInfo
250 mkAltLabel uniq tag = CaseLabel uniq (CaseAlt tag)
251 mkDefaultLabel uniq = CaseLabel uniq CaseDefault
253 mkStringLitLabel = StringLitLabel
254 mkAsmTempLabel = AsmTempLabel
256 mkModuleInitLabel = ModuleInitLabel
257 mkPlainModuleInitLabel = PlainModuleInitLabel
259 -- Some fixed runtime system labels
261 mkSplitMarkerLabel = RtsLabel (RtsCode SLIT("__stg_split_marker"))
262 mkUpdInfoLabel = RtsLabel (RtsInfo SLIT("stg_upd_frame"))
263 mkSeqInfoLabel = RtsLabel (RtsInfo SLIT("stg_seq_frame"))
264 mkIndStaticInfoLabel = RtsLabel (RtsInfo SLIT("stg_IND_STATIC"))
265 mkMainCapabilityLabel = RtsLabel (RtsData SLIT("MainCapability"))
266 mkMAP_FROZEN_infoLabel = RtsLabel (RtsInfo SLIT("stg_MUT_ARR_PTRS_FROZEN"))
267 mkEMPTY_MVAR_infoLabel = RtsLabel (RtsInfo SLIT("stg_EMPTY_MVAR"))
269 mkTopTickyCtrLabel = RtsLabel (RtsData SLIT("top_ct"))
270 mkCAFBlackHoleInfoTableLabel = RtsLabel (RtsInfo SLIT("stg_CAF_BLACKHOLE"))
271 mkSECAFBlackHoleInfoTableLabel = if opt_DoTickyProfiling then
272 RtsLabel (RtsInfo SLIT("stg_SE_CAF_BLACKHOLE"))
273 else -- RTS won't have info table unless -ticky is on
274 panic "mkSECAFBlackHoleInfoTableLabel requires -ticky"
275 mkRtsPrimOpLabel primop = RtsLabel (RtsPrimOp primop)
277 moduleRegdLabel = ModuleRegdLabel
279 mkSelectorInfoLabel upd off = RtsLabel (RtsSelectorInfoTbl upd off)
280 mkSelectorEntryLabel upd off = RtsLabel (RtsSelectorEntry upd off)
282 mkApInfoTableLabel upd off = RtsLabel (RtsApInfoTbl upd off)
283 mkApEntryLabel upd off = RtsLabel (RtsApEntry upd off)
287 mkForeignLabel :: FastString -> Maybe Int -> Bool -> CLabel
288 mkForeignLabel str mb_sz is_dynamic = ForeignLabel str mb_sz is_dynamic
292 mkCCLabel cc = CC_Label cc
293 mkCCSLabel ccs = CCS_Label ccs
295 mkRtsInfoLabel str = RtsLabel (RtsInfo str)
296 mkRtsEntryLabel str = RtsLabel (RtsEntry str)
297 mkRtsRetInfoLabel str = RtsLabel (RtsRetInfo str)
298 mkRtsRetLabel str = RtsLabel (RtsRet str)
299 mkRtsCodeLabel str = RtsLabel (RtsCode str)
300 mkRtsDataLabel str = RtsLabel (RtsData str)
302 mkRtsInfoLabelFS str = RtsLabel (RtsInfoFS str)
303 mkRtsEntryLabelFS str = RtsLabel (RtsEntryFS str)
304 mkRtsRetInfoLabelFS str = RtsLabel (RtsRetInfoFS str)
305 mkRtsRetLabelFS str = RtsLabel (RtsRetFS str)
306 mkRtsCodeLabelFS str = RtsLabel (RtsCodeFS str)
307 mkRtsDataLabelFS str = RtsLabel (RtsDataFS str)
309 mkRtsSlowTickyCtrLabel :: String -> CLabel
310 mkRtsSlowTickyCtrLabel pat = RtsLabel (RtsSlowTickyCtr pat)
312 -- -----------------------------------------------------------------------------
313 -- Converting info labels to entry labels.
315 infoLblToEntryLbl :: CLabel -> CLabel
316 infoLblToEntryLbl (IdLabel n InfoTbl) = IdLabel n Entry
317 infoLblToEntryLbl (IdLabel n ConInfoTbl) = IdLabel n ConEntry
318 infoLblToEntryLbl (IdLabel n StaticInfoTbl) = IdLabel n StaticConEntry
319 infoLblToEntryLbl (CaseLabel n CaseReturnInfo) = CaseLabel n CaseReturnPt
320 infoLblToEntryLbl (RtsLabel (RtsInfo s)) = RtsLabel (RtsEntry s)
321 infoLblToEntryLbl (RtsLabel (RtsRetInfo s)) = RtsLabel (RtsRet s)
322 infoLblToEntryLbl (RtsLabel (RtsInfoFS s)) = RtsLabel (RtsEntryFS s)
323 infoLblToEntryLbl (RtsLabel (RtsRetInfoFS s)) = RtsLabel (RtsRetFS s)
324 infoLblToEntryLbl _ = panic "CLabel.infoLblToEntryLbl"
326 entryLblToInfoLbl :: CLabel -> CLabel
327 entryLblToInfoLbl (IdLabel n Entry) = IdLabel n InfoTbl
328 entryLblToInfoLbl (IdLabel n ConEntry) = IdLabel n ConInfoTbl
329 entryLblToInfoLbl (IdLabel n StaticConEntry) = IdLabel n StaticInfoTbl
330 entryLblToInfoLbl (CaseLabel n CaseReturnPt) = CaseLabel n CaseReturnInfo
331 entryLblToInfoLbl (RtsLabel (RtsEntry s)) = RtsLabel (RtsInfo s)
332 entryLblToInfoLbl (RtsLabel (RtsRet s)) = RtsLabel (RtsRetInfo s)
333 entryLblToInfoLbl (RtsLabel (RtsEntryFS s)) = RtsLabel (RtsInfoFS s)
334 entryLblToInfoLbl (RtsLabel (RtsRetFS s)) = RtsLabel (RtsRetInfoFS s)
335 entryLblToInfoLbl l = pprPanic "CLabel.entryLblToInfoLbl" (pprCLabel l)
337 -- -----------------------------------------------------------------------------
338 -- Does a CLabel need declaring before use or not?
340 needsCDecl :: CLabel -> Bool
341 -- False <=> it's pre-declared; don't bother
342 -- don't bother declaring SRT & Bitmap labels, we always make sure
343 -- they are defined before use.
344 needsCDecl (IdLabel _ SRT) = False
345 needsCDecl (IdLabel _ SRTDesc) = False
346 needsCDecl (IdLabel _ Bitmap) = False
347 needsCDecl (IdLabel _ _) = True
348 needsCDecl (CaseLabel _ CaseReturnPt) = True
349 needsCDecl (CaseLabel _ CaseReturnInfo) = True
350 needsCDecl (ModuleInitLabel _ _) = True
351 needsCDecl (PlainModuleInitLabel _) = True
352 needsCDecl ModuleRegdLabel = False
354 needsCDecl (CaseLabel _ _) = False
355 needsCDecl (StringLitLabel _) = False
356 needsCDecl (AsmTempLabel _) = False
357 needsCDecl (RtsLabel _) = False
358 needsCDecl (ForeignLabel _ _ _) = False
359 needsCDecl (CC_Label _) = True
360 needsCDecl (CCS_Label _) = True
362 -- Whether the label is an assembler temporary:
364 isAsmTemp :: CLabel -> Bool -- is a local temporary for native code generation
365 isAsmTemp (AsmTempLabel _) = True
368 -- -----------------------------------------------------------------------------
369 -- Is a CLabel visible outside this object file or not?
371 -- From the point of view of the code generator, a name is
372 -- externally visible if it has to be declared as exported
373 -- in the .o file's symbol table; that is, made non-static.
375 externallyVisibleCLabel :: CLabel -> Bool -- not C "static"
376 externallyVisibleCLabel (CaseLabel _ _) = False
377 externallyVisibleCLabel (StringLitLabel _) = False
378 externallyVisibleCLabel (AsmTempLabel _) = False
379 externallyVisibleCLabel (ModuleInitLabel _ _)= True
380 externallyVisibleCLabel (PlainModuleInitLabel _)= True
381 externallyVisibleCLabel ModuleRegdLabel = False
382 externallyVisibleCLabel (RtsLabel _) = True
383 externallyVisibleCLabel (ForeignLabel _ _ _) = True
384 externallyVisibleCLabel (IdLabel id _) = isExternalName id
385 externallyVisibleCLabel (CC_Label _) = True
386 externallyVisibleCLabel (CCS_Label _) = True
389 -- -----------------------------------------------------------------------------
390 -- Finding the "type" of a CLabel
392 -- For generating correct types in label declarations:
398 labelType :: CLabel -> CLabelType
399 labelType (RtsLabel (RtsSelectorInfoTbl _ _)) = DataLabel
400 labelType (RtsLabel (RtsApInfoTbl _ _)) = DataLabel
401 labelType (RtsLabel (RtsData _)) = DataLabel
402 labelType (RtsLabel (RtsCode _)) = CodeLabel
403 labelType (RtsLabel (RtsInfo _)) = DataLabel
404 labelType (RtsLabel (RtsEntry _)) = CodeLabel
405 labelType (RtsLabel (RtsRetInfo _)) = DataLabel
406 labelType (RtsLabel (RtsRet _)) = CodeLabel
407 labelType (RtsLabel (RtsDataFS _)) = DataLabel
408 labelType (RtsLabel (RtsCodeFS _)) = CodeLabel
409 labelType (RtsLabel (RtsInfoFS _)) = DataLabel
410 labelType (RtsLabel (RtsEntryFS _)) = CodeLabel
411 labelType (RtsLabel (RtsRetInfoFS _)) = DataLabel
412 labelType (RtsLabel (RtsRetFS _)) = CodeLabel
413 labelType (CaseLabel _ CaseReturnInfo) = DataLabel
414 labelType (CaseLabel _ CaseReturnPt) = CodeLabel
415 labelType (ModuleInitLabel _ _) = CodeLabel
416 labelType (PlainModuleInitLabel _) = CodeLabel
418 labelType (IdLabel _ info) =
423 ConInfoTbl -> DataLabel
424 StaticInfoTbl -> DataLabel
425 ClosureTable -> DataLabel
428 labelType _ = DataLabel
431 -- -----------------------------------------------------------------------------
432 -- Does a CLabel need dynamic linkage?
434 -- When referring to data in code, we need to know whether
435 -- that data resides in a DLL or not. [Win32 only.]
436 -- @labelDynamic@ returns @True@ if the label is located
437 -- in a DLL, be it a data reference or not.
439 labelDynamic :: CLabel -> Bool
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 (StringLitLabel u)
540 = pprUnique u <> ptext SLIT("_str")
542 pprCLbl (CaseLabel u CaseReturnPt)
543 = hcat [pprUnique u, ptext SLIT("_ret")]
544 pprCLbl (CaseLabel u CaseReturnInfo)
545 = hcat [pprUnique u, ptext SLIT("_info")]
546 pprCLbl (CaseLabel u (CaseAlt tag))
547 = hcat [pprUnique u, pp_cSEP, int tag, ptext SLIT("_alt")]
548 pprCLbl (CaseLabel u CaseDefault)
549 = hcat [pprUnique u, ptext SLIT("_dflt")]
551 pprCLbl (RtsLabel (RtsCode str)) = ptext str
552 pprCLbl (RtsLabel (RtsData str)) = ptext str
553 pprCLbl (RtsLabel (RtsCodeFS str)) = ftext str
554 pprCLbl (RtsLabel (RtsDataFS str)) = ftext str
556 pprCLbl (RtsLabel (RtsSelectorInfoTbl upd_reqd offset))
557 = hcat [ptext SLIT("stg_sel_"), text (show offset),
559 then SLIT("_upd_info")
560 else SLIT("_noupd_info"))
563 pprCLbl (RtsLabel (RtsSelectorEntry upd_reqd offset))
564 = hcat [ptext SLIT("stg_sel_"), text (show offset),
566 then SLIT("_upd_entry")
567 else SLIT("_noupd_entry"))
570 pprCLbl (RtsLabel (RtsApInfoTbl upd_reqd arity))
571 = hcat [ptext SLIT("stg_ap_"), text (show arity),
573 then SLIT("_upd_info")
574 else SLIT("_noupd_info"))
577 pprCLbl (RtsLabel (RtsApEntry upd_reqd arity))
578 = hcat [ptext SLIT("stg_ap_"), text (show arity),
580 then SLIT("_upd_entry")
581 else SLIT("_noupd_entry"))
584 pprCLbl (RtsLabel (RtsInfo fs))
585 = ptext fs <> ptext SLIT("_info")
587 pprCLbl (RtsLabel (RtsEntry fs))
588 = ptext fs <> ptext SLIT("_entry")
590 pprCLbl (RtsLabel (RtsRetInfo fs))
591 = ptext fs <> ptext SLIT("_info")
593 pprCLbl (RtsLabel (RtsRet fs))
594 = ptext fs <> ptext SLIT("_ret")
596 pprCLbl (RtsLabel (RtsInfoFS fs))
597 = ftext fs <> ptext SLIT("_info")
599 pprCLbl (RtsLabel (RtsEntryFS fs))
600 = ftext fs <> ptext SLIT("_entry")
602 pprCLbl (RtsLabel (RtsRetInfoFS fs))
603 = ftext fs <> ptext SLIT("_info")
605 pprCLbl (RtsLabel (RtsRetFS fs))
606 = ftext fs <> ptext SLIT("_ret")
608 pprCLbl (RtsLabel (RtsPrimOp primop))
609 = ppr primop <> ptext SLIT("_fast")
611 pprCLbl (RtsLabel (RtsSlowTickyCtr pat))
612 = ptext SLIT("SLOW_CALL_") <> text pat <> ptext SLIT("_ctr")
614 pprCLbl ModuleRegdLabel
615 = ptext SLIT("_module_registered")
617 pprCLbl (ForeignLabel str _ _)
620 pprCLbl (IdLabel id flavor) = ppr id <> ppIdFlavor flavor
622 pprCLbl (CC_Label cc) = ppr cc
623 pprCLbl (CCS_Label ccs) = ppr ccs
625 pprCLbl (ModuleInitLabel mod way)
626 = ptext SLIT("__stginit_") <> ftext (moduleNameFS (moduleName mod))
627 <> char '_' <> text way
628 pprCLbl (PlainModuleInitLabel mod)
629 = ptext SLIT("__stginit_") <> ftext (moduleNameFS (moduleName mod))
631 ppIdFlavor :: IdLabelInfo -> SDoc
632 ppIdFlavor x = pp_cSEP <>
634 Closure -> ptext SLIT("closure")
635 SRT -> ptext SLIT("srt")
636 SRTDesc -> ptext SLIT("srtd")
637 InfoTbl -> ptext SLIT("info")
638 Entry -> ptext SLIT("entry")
639 Slow -> ptext SLIT("slow")
640 RednCounts -> ptext SLIT("ct")
641 Bitmap -> ptext SLIT("btm")
642 ConEntry -> ptext SLIT("con_entry")
643 ConInfoTbl -> ptext SLIT("con_info")
644 StaticConEntry -> ptext SLIT("static_entry")
645 StaticInfoTbl -> ptext SLIT("static_info")
646 ClosureTable -> ptext SLIT("closure_tbl")
652 -- -----------------------------------------------------------------------------
653 -- Machine-dependent knowledge about labels.
655 underscorePrefix :: Bool -- leading underscore on assembler labels?
656 underscorePrefix = (cLeadingUnderscore == "YES")
658 asmTempLabelPrefix :: LitString -- for formatting labels
661 {- The alpha assembler likes temporary labels to look like $L123
662 instead of L123. (Don't toss the L, because then Lf28
666 #elif darwin_TARGET_OS