2 % (c) The University of Glasgow, 1992-2002
4 \section[CLabel]{@CLabel@: Information to make C Labels}
8 CLabel, -- abstract type
17 mkStaticConEntryLabel,
20 mkStaticInfoTableLabel,
36 mkPlainModuleInitLabel,
47 mkMainCapabilityLabel,
48 mkCharlikeClosureLabel,
49 mkIntlikeClosureLabel,
50 mkMAP_FROZEN_infoLabel,
51 mkEMPTY_MVAR_infoLabel,
54 mkBlackHoleInfoTableLabel,
55 mkBlackHoleBQInfoTableLabel,
56 mkCAFBlackHoleInfoTableLabel,
57 mkSECAFBlackHoleInfoTableLabel,
70 mkCC_Label, mkCCS_Label,
72 needsCDecl, isAsmTemp, externallyVisibleCLabel,
74 CLabelType(..), labelType, labelDynamic,
80 #include "HsVersions.h"
82 #if ! OMIT_NATIVE_CODEGEN
83 import {-# SOURCE #-} MachMisc ( underscorePrefix, fmtAsmLbl )
86 import CmdLineOpts ( opt_Static, opt_DoTickyProfiling )
87 import CStrings ( pp_cSEP )
88 import DataCon ( ConTag )
89 import Module ( moduleName, moduleNameFS,
90 Module, isHomeModule )
91 import Name ( Name, getName, isDllName, isExternalName )
92 import TyCon ( TyCon )
93 import Unique ( pprUnique, Unique )
94 import PrimOp ( PrimOp )
95 import CostCentre ( CostCentre, CostCentreStack )
100 things we want to find out:
102 * should the labelled things be declared "static" (visible only in this file)?
104 * should it be declared "const" (read-only text space)?
106 * does it need declarations at all? (v common Prelude things are pre-declared)
108 * what type does it have? (for generating accurate enough C declarations
109 so that the C compiler won't complain).
113 = IdLabel -- A family of labels related to the
114 Name -- definition of a particular Id
117 | DataConLabel -- Ditto data constructors
121 | CaseLabel -- A family of labels related to a particular case expression
122 Unique -- Unique says which case expression
125 | TyConLabel TyCon -- currently only one kind of TyconLabel,
126 -- a 'Closure Table'.
128 | AsmTempLabel Unique
131 Module -- the module name
133 -- at some point we might want some kind of version number in
134 -- the module init label, to guard against compiling modules in
135 -- the wrong order. We can't use the interface file version however,
136 -- because we don't always recompile modules which depend on a module
137 -- whose version has changed.
139 | PlainModuleInitLabel Module -- without the vesrion & way info
141 | RtsLabel RtsLabelInfo
143 | ForeignLabel FastString Bool -- a 'C' (or otherwise foreign) label
144 -- Bool <=> is dynamic
146 | CC_Label CostCentre
147 | CCS_Label CostCentreStack
154 = Closure -- Label for (static???) closure
155 | SRT -- Static reference table
156 | SRTDesc -- Static reference table descriptor
157 | InfoTbl -- Info tables for closures; always read-only
158 | Entry -- entry point
159 | Slow -- slow entry point
161 -- Ticky-ticky counting
162 | RednCounts -- Label of place to keep reduction-count info for
165 | Bitmap -- A bitmap (function or case return)
169 data DataConLabelInfo
170 = ConEntry -- the only kind of entry pt for constructors
171 | ConInfoTbl -- corresponding info table
172 | StaticConEntry -- static constructor entry point
173 | StaticInfoTbl -- corresponding info table
185 = RtsShouldNeverHappenCode
187 | RtsBlackHoleInfoTbl LitString -- black hole with info table name
189 | RtsUpdInfo -- upd_frame_info
190 | RtsSeqInfo -- seq_frame_info
191 | RtsGCEntryLabel String -- a heap check fail handler, eg stg_chk_2
192 | RtsMainCapability -- MainCapability
193 | Rts_Closure String -- misc rts closures, eg CHARLIKE_closure
194 | Rts_Info String -- misc rts itbls, eg MUT_ARR_PTRS_FROZEN_info
195 | Rts_Code String -- misc rts code
197 | RtsSelectorInfoTbl Bool{-updatable-} Int{-offset-} -- Selector thunks
198 | RtsSelectorEntry Bool{-updatable-} Int{-offset-}
200 | RtsApInfoTbl Bool{-updatable-} Int{-arity-} -- AP thunks
201 | RtsApEntry Bool{-updatable-} Int{-arity-}
209 | RtsApplyInfoLabel LitString
210 | RtsApplyEntryLabel LitString
214 -- Label Type: for generating C declarations.
227 mkClosureLabel id = IdLabel id Closure
228 mkSRTLabel id = IdLabel id SRT
229 mkSRTDescLabel id = IdLabel id SRTDesc
230 mkInfoTableLabel id = IdLabel id InfoTbl
231 mkEntryLabel id = IdLabel id Entry
232 mkSlowEntryLabel id = IdLabel id Slow
233 mkBitmapLabel id = IdLabel id Bitmap
234 mkRednCountsLabel id = IdLabel id RednCounts
236 mkStaticInfoTableLabel con = DataConLabel con StaticInfoTbl
237 mkConInfoTableLabel con = DataConLabel con ConInfoTbl
238 mkConEntryLabel con = DataConLabel con ConEntry
239 mkStaticConEntryLabel con = DataConLabel con StaticConEntry
242 mkReturnPtLabel uniq = CaseLabel uniq CaseReturnPt
243 mkReturnInfoLabel uniq = CaseLabel uniq CaseReturnInfo
244 mkVecTblLabel uniq = CaseLabel uniq CaseVecTbl
245 mkAltLabel uniq tag = CaseLabel uniq (CaseAlt tag)
246 mkDefaultLabel uniq = CaseLabel uniq CaseDefault
249 mkClosureTblLabel tycon = TyConLabel tycon
251 mkAsmTempLabel = AsmTempLabel
253 mkModuleInitLabel = ModuleInitLabel
254 mkPlainModuleInitLabel = PlainModuleInitLabel
256 -- Some fixed runtime system labels
258 mkErrorStdEntryLabel = RtsLabel RtsShouldNeverHappenCode
259 mkStgUpdatePAPLabel = RtsLabel (Rts_Code "stg_update_PAP")
260 mkSplitMarkerLabel = RtsLabel (Rts_Code "__stg_split_marker")
261 mkUpdInfoLabel = RtsLabel RtsUpdInfo
262 mkSeqInfoLabel = RtsLabel RtsSeqInfo
263 mkIndInfoLabel = RtsLabel (Rts_Info "stg_IND_info")
264 mkIndStaticInfoLabel = RtsLabel (Rts_Info "stg_IND_STATIC_info")
265 mkRtsGCEntryLabel str = RtsLabel (RtsGCEntryLabel str)
266 mkMainCapabilityLabel = RtsLabel RtsMainCapability
267 mkCharlikeClosureLabel = RtsLabel (Rts_Closure "stg_CHARLIKE_closure")
268 mkIntlikeClosureLabel = RtsLabel (Rts_Closure "stg_INTLIKE_closure")
269 mkMAP_FROZEN_infoLabel = RtsLabel (Rts_Info "stg_MUT_ARR_PTRS_FROZEN_info")
270 mkEMPTY_MVAR_infoLabel = RtsLabel (Rts_Info "stg_EMPTY_MVAR_info")
272 mkTopTickyCtrLabel = RtsLabel RtsTopTickyCtr
273 mkBlackHoleInfoTableLabel = RtsLabel (RtsBlackHoleInfoTbl SLIT("stg_BLACKHOLE_info"))
274 mkBlackHoleBQInfoTableLabel = RtsLabel (RtsBlackHoleInfoTbl SLIT("stg_BLACKHOLE_BQ_info"))
275 mkCAFBlackHoleInfoTableLabel = RtsLabel (RtsBlackHoleInfoTbl SLIT("stg_CAF_BLACKHOLE_info"))
276 mkSECAFBlackHoleInfoTableLabel = if opt_DoTickyProfiling then
277 RtsLabel (RtsBlackHoleInfoTbl SLIT("stg_SE_CAF_BLACKHOLE_info"))
278 else -- RTS won't have info table unless -ticky is on
279 panic "mkSECAFBlackHoleInfoTableLabel requires -ticky"
280 mkRtsPrimOpLabel primop = RtsLabel (RtsPrimOp primop)
282 moduleRegdLabel = RtsLabel RtsModuleRegd
284 mkSelectorInfoLabel upd off = RtsLabel (RtsSelectorInfoTbl upd off)
285 mkSelectorEntryLabel upd off = RtsLabel (RtsSelectorEntry upd off)
287 mkApInfoTableLabel upd off = RtsLabel (RtsApInfoTbl upd off)
288 mkApEntryLabel upd off = RtsLabel (RtsApEntry upd off)
292 mkForeignLabel :: FastString -> Bool -> CLabel
293 mkForeignLabel str is_dynamic = ForeignLabel str is_dynamic
297 mkCC_Label cc = CC_Label cc
298 mkCCS_Label ccs = CCS_Label ccs
300 -- Std RTS application routines
302 mkRtsApplyInfoLabel = RtsLabel . RtsApplyInfoLabel
303 mkRtsApplyEntryLabel = RtsLabel . RtsApplyEntryLabel
307 needsCDecl :: CLabel -> Bool -- False <=> it's pre-declared; don't bother
308 isAsmTemp :: CLabel -> Bool -- is a local temporary for native code generation
309 externallyVisibleCLabel :: CLabel -> Bool -- not C "static"
312 @needsCDecl@ is @True@ unless the thing is a deeply-@PreludeCore@-ish
313 object. {\em Also:} No need to spit out labels for things generated
314 by the flattener (in @AbsCUtils@)---it is careful to ensure references
315 to them are always backwards. These are return-point and vector-table
318 Declarations for (non-prelude) @Id@-based things are needed because of
321 Declarations for direct return points are needed, because they may be
322 let-no-escapes, which can be recursive.
325 -- don't bother declaring SRT & Bitmap labels, we always make sure
326 -- they are defined before use.
327 needsCDecl (IdLabel _ SRT) = False
328 needsCDecl (IdLabel _ SRTDesc) = False
329 needsCDecl (IdLabel _ Bitmap) = False
330 needsCDecl (IdLabel _ _) = True
331 needsCDecl (CaseLabel _ CaseReturnPt) = True
332 needsCDecl (DataConLabel _ _) = True
333 needsCDecl (TyConLabel _) = True
334 needsCDecl (ModuleInitLabel _ _) = True
335 needsCDecl (PlainModuleInitLabel _) = True
337 needsCDecl (CaseLabel _ _) = False
338 needsCDecl (AsmTempLabel _) = False
339 needsCDecl (RtsLabel _) = False
340 needsCDecl (ForeignLabel _ _) = False
341 needsCDecl (CC_Label _) = False
342 needsCDecl (CCS_Label _) = False
345 Whether the label is an assembler temporary:
348 isAsmTemp (AsmTempLabel _) = True
352 C ``static'' or not...
353 From the point of view of the code generator, a name is
354 externally visible if it has to be declared as exported
355 in the .o file's symbol table; that is, made non-static.
358 externallyVisibleCLabel (DataConLabel _ _) = True
359 externallyVisibleCLabel (TyConLabel tc) = True
360 externallyVisibleCLabel (CaseLabel _ _) = False
361 externallyVisibleCLabel (AsmTempLabel _) = False
362 externallyVisibleCLabel (ModuleInitLabel _ _)= True
363 externallyVisibleCLabel (PlainModuleInitLabel _)= True
364 externallyVisibleCLabel (RtsLabel RtsModuleRegd) = False --hack
365 externallyVisibleCLabel (RtsLabel _) = True
366 externallyVisibleCLabel (ForeignLabel _ _) = True
367 externallyVisibleCLabel (IdLabel id _) = isExternalName id
368 externallyVisibleCLabel (CC_Label _) = False -- not strictly true
369 externallyVisibleCLabel (CCS_Label _) = False -- not strictly true
372 For generating correct types in label declarations, and also for
373 deciding whether the C compiler would like us to use '&' before the
374 label to get its address:
377 labelType :: CLabel -> CLabelType
378 labelType (RtsLabel (RtsBlackHoleInfoTbl _)) = InfoTblType
379 labelType (RtsLabel (RtsSelectorInfoTbl _ _)) = InfoTblType
380 labelType (RtsLabel (RtsApInfoTbl _ _)) = InfoTblType
381 labelType (RtsLabel RtsUpdInfo) = RetInfoTblType
382 labelType (RtsLabel RtsSeqInfo) = RetInfoTblType
383 labelType (RtsLabel RtsTopTickyCtr) = CodeType -- XXX
384 labelType (RtsLabel (Rts_Info _)) = InfoTblType
385 labelType (RtsLabel (RtsApplyInfoLabel _)) = RetInfoTblType
386 labelType (RtsLabel (RtsApplyEntryLabel _)) = CodeType
387 labelType (CaseLabel _ CaseReturnInfo) = RetInfoTblType
388 labelType (CaseLabel _ CaseReturnPt) = CodeType
389 labelType (CaseLabel _ CaseVecTbl) = VecTblType
390 labelType (TyConLabel _) = ClosureTblType
391 labelType (ModuleInitLabel _ _) = CodeType
392 labelType (PlainModuleInitLabel _) = CodeType
393 labelType (CC_Label _) = CodeType -- hack
394 labelType (CCS_Label _) = CodeType -- hack
396 labelType (IdLabel _ info) =
398 InfoTbl -> InfoTblType
399 Closure -> ClosureType
403 labelType (DataConLabel _ info) =
405 ConInfoTbl -> InfoTblType
406 StaticInfoTbl -> InfoTblType
409 labelType _ = DataType
412 When referring to data in code, we need to know whether
413 that data resides in a DLL or not. [Win32 only.]
414 @labelDynamic@ returns @True@ if the label is located
415 in a DLL, be it a data reference or not.
418 labelDynamic :: CLabel -> Bool
421 -- The special case for RtsShouldNeverHappenCode is because the associated address is
422 -- NULL, i.e. not a DLL entry point
423 RtsLabel RtsShouldNeverHappenCode -> False
424 RtsLabel _ -> not opt_Static -- i.e., is the RTS in a DLL or not?
425 IdLabel n k -> isDllName n
426 DataConLabel n k -> isDllName n
427 TyConLabel tc -> isDllName (getName tc)
428 ForeignLabel _ d -> d
429 ModuleInitLabel m _ -> (not opt_Static) && (not (isHomeModule m))
430 PlainModuleInitLabel m -> (not opt_Static) && (not (isHomeModule m))
435 OLD?: These GRAN functions are needed for spitting out GRAN_FETCH() at the
436 right places. It is used to detect when the abstractC statement of an
437 CCodeBlock actually contains the code for a slow entry point. -- HWL
439 We need at least @Eq@ for @CLabels@, because we want to avoid
440 duplicate declarations in generating C (see @labelSeenTE@ in
443 -----------------------------------------------------------------------------
444 Printing out CLabels.
450 where <name> is <Module>_<name> for external names and <unique> for
451 internal names. <type> is one of the following:
454 srt Static reference table
455 srtd Static reference table descriptor
457 slow Slow entry code (if any)
458 ret Direct return address
460 <n>_alt Case alternative (tag n)
461 dflt Default case alternative
462 btm Large bitmap vector
463 closure Static closure
464 con_entry Dynamic Constructor entry code
465 con_info Dynamic Constructor info table
466 static_entry Static Constructor entry code
467 static_info Static Constructor info table
468 sel_info Selector info table
469 sel_entry Selector entry code
471 ccs Cost centre stack
474 pprCLabel :: CLabel -> SDoc
476 #if ! OMIT_NATIVE_CODEGEN
477 pprCLabel (AsmTempLabel u)
478 = text (fmtAsmLbl (show u))
482 #if ! OMIT_NATIVE_CODEGEN
483 getPprStyle $ \ sty ->
484 if asmStyle sty && underscorePrefix then
485 pp_cSEP <> pprCLbl lbl
490 pprCLbl (CaseLabel u CaseReturnPt)
491 = hcat [pprUnique u, pp_cSEP, ptext SLIT("ret")]
492 pprCLbl (CaseLabel u CaseReturnInfo)
493 = hcat [pprUnique u, pp_cSEP, ptext SLIT("info")]
494 pprCLbl (CaseLabel u CaseVecTbl)
495 = hcat [pprUnique u, pp_cSEP, ptext SLIT("vtbl")]
496 pprCLbl (CaseLabel u (CaseAlt tag))
497 = hcat [pprUnique u, pp_cSEP, int tag, pp_cSEP, ptext SLIT("alt")]
498 pprCLbl (CaseLabel u CaseDefault)
499 = hcat [pprUnique u, pp_cSEP, ptext SLIT("dflt")]
501 pprCLbl (RtsLabel RtsShouldNeverHappenCode) = ptext SLIT("NULL")
502 -- used to be stg_error_entry but Windows can't have DLL entry points as static
503 -- initialisers, and besides, this ShouldNeverHappen, right?
505 pprCLbl (RtsLabel RtsUpdInfo) = ptext SLIT("stg_upd_frame_info")
506 pprCLbl (RtsLabel RtsSeqInfo) = ptext SLIT("stg_seq_frame_info")
507 pprCLbl (RtsLabel RtsMainCapability) = ptext SLIT("MainCapability")
508 pprCLbl (RtsLabel (RtsGCEntryLabel str)) = text str
509 pprCLbl (RtsLabel (Rts_Closure str)) = text str
510 pprCLbl (RtsLabel (Rts_Info str)) = text str
511 pprCLbl (RtsLabel (Rts_Code str)) = text str
513 pprCLbl (RtsLabel RtsTopTickyCtr) = ptext SLIT("top_ct")
515 pprCLbl (RtsLabel (RtsBlackHoleInfoTbl info)) = ptext info
517 pprCLbl (RtsLabel (RtsSelectorInfoTbl upd_reqd offset))
518 = hcat [ptext SLIT("stg_sel_"), text (show offset),
520 then SLIT("_upd_info")
521 else SLIT("_noupd_info"))
524 pprCLbl (RtsLabel (RtsSelectorEntry upd_reqd offset))
525 = hcat [ptext SLIT("stg_sel_"), text (show offset),
527 then SLIT("_upd_entry")
528 else SLIT("_noupd_entry"))
531 pprCLbl (RtsLabel (RtsApInfoTbl upd_reqd arity))
532 = hcat [ptext SLIT("stg_ap_"), text (show arity),
534 then SLIT("_upd_info")
535 else SLIT("_noupd_info"))
538 pprCLbl (RtsLabel (RtsApEntry upd_reqd arity))
539 = hcat [ptext SLIT("stg_ap_"), text (show arity),
541 then SLIT("_upd_entry")
542 else SLIT("_noupd_entry"))
545 pprCLbl (RtsLabel (RtsApplyInfoLabel fs))
546 = ptext SLIT("stg_ap_") <> ptext fs <> ptext SLIT("_info")
548 pprCLbl (RtsLabel (RtsApplyEntryLabel fs))
549 = ptext SLIT("stg_ap_") <> ptext fs <> ptext SLIT("_ret")
551 pprCLbl (RtsLabel (RtsPrimOp primop))
552 = ppr primop <> ptext SLIT("_fast")
554 pprCLbl (RtsLabel RtsModuleRegd)
555 = ptext SLIT("module_registered")
557 pprCLbl (ForeignLabel str _)
560 pprCLbl (TyConLabel tc)
561 = hcat [ppr tc, pp_cSEP, ptext SLIT("closure_tbl")]
563 pprCLbl (IdLabel id flavor) = ppr id <> ppIdFlavor flavor
564 pprCLbl (DataConLabel con flavor) = ppr con <> ppConFlavor flavor
566 pprCLbl (CC_Label cc) = ppr cc
567 pprCLbl (CCS_Label ccs) = ppr ccs
569 pprCLbl (ModuleInitLabel mod way)
570 = ptext SLIT("__stginit_") <> ftext (moduleNameFS (moduleName mod))
571 <> char '_' <> text way
572 pprCLbl (PlainModuleInitLabel mod)
573 = ptext SLIT("__stginit_") <> ftext (moduleNameFS (moduleName mod))
575 ppIdFlavor :: IdLabelInfo -> SDoc
577 ppIdFlavor x = pp_cSEP <>
579 Closure -> ptext SLIT("closure")
580 SRT -> ptext SLIT("srt")
581 SRTDesc -> ptext SLIT("srtd")
582 InfoTbl -> ptext SLIT("info")
583 Entry -> ptext SLIT("entry")
584 Slow -> ptext SLIT("slow")
585 RednCounts -> ptext SLIT("ct")
586 Bitmap -> ptext SLIT("btm")
589 ppConFlavor x = pp_cSEP <>
591 ConEntry -> ptext SLIT("con_entry")
592 ConInfoTbl -> ptext SLIT("con_info")
593 StaticConEntry -> ptext SLIT("static_entry")
594 StaticInfoTbl -> ptext SLIT("static_info")