2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 % $Id: CLabel.lhs,v 1.53 2002/07/16 14:56:09 simonmar Exp $
6 \section[CLabel]{@CLabel@: Information to make C Labels}
10 CLabel, -- abstract type
18 mkStaticConEntryLabel,
21 mkStaticInfoTableLabel,
37 mkPlainModuleInitLabel,
48 mkMainCapabilityLabel,
49 mkCharlikeClosureLabel,
50 mkIntlikeClosureLabel,
51 mkMAP_FROZEN_infoLabel,
52 mkEMPTY_MVAR_infoLabel,
55 mkBlackHoleInfoTableLabel,
56 mkCAFBlackHoleInfoTableLabel,
57 mkSECAFBlackHoleInfoTableLabel,
67 mkCC_Label, mkCCS_Label,
69 needsCDecl, isAsmTemp, externallyVisibleCLabel,
71 CLabelType(..), labelType, labelDynamic,
77 #include "HsVersions.h"
79 #if ! OMIT_NATIVE_CODEGEN
80 import {-# SOURCE #-} MachMisc ( underscorePrefix, fmtAsmLbl )
83 import CmdLineOpts ( opt_Static, opt_DoTickyProfiling )
84 import CStrings ( pp_cSEP )
85 import DataCon ( ConTag )
86 import Module ( moduleName, moduleNameFS,
87 Module, isHomeModule )
88 import Name ( Name, getName, isDllName, isExternalName )
89 import TyCon ( TyCon )
90 import Unique ( pprUnique, Unique )
91 import PrimOp ( PrimOp )
92 import CostCentre ( CostCentre, CostCentreStack )
93 import BasicTypes ( Version )
98 things we want to find out:
100 * should the labelled things be declared "static" (visible only in this file)?
102 * should it be declared "const" (read-only text space)?
104 * does it need declarations at all? (v common Prelude things are pre-declared)
106 * what type does it have? (for generating accurate enough C declarations
107 so that the C compiler won't complain).
111 = IdLabel -- A family of labels related to the
112 Name -- definition of a particular Id
115 | DataConLabel -- Ditto data constructors
119 | CaseLabel -- A family of labels related to a particular case expression
120 Unique -- Unique says which case expression
123 | TyConLabel TyCon -- currently only one kind of TyconLabel,
124 -- a 'Closure Table'.
126 | AsmTempLabel Unique
129 Module -- the module name
130 Version -- its version (same as the interface file ver)
133 | PlainModuleInitLabel Module -- without the vesrion & way info
135 | RtsLabel RtsLabelInfo
137 | ForeignLabel FastString Bool -- a 'C' (or otherwise foreign) label
138 -- Bool <=> is dynamic
140 | CC_Label CostCentre
141 | CCS_Label CostCentreStack
148 = Closure -- Label for (static???) closure
150 | SRT -- Static reference table
152 | InfoTbl -- Info table for a closure; always read-only
154 | EntryStd -- Thunk, or "slow", code entry point
156 | EntryFast Int -- entry pt when no arg satisfaction chk needed;
157 -- Int is the arity of the function (to be
158 -- encoded into the name)
160 -- Ticky-ticky counting
161 | RednCounts -- Label of place to keep reduction-count info for
165 data DataConLabelInfo
166 = ConEntry -- the only kind of entry pt for constructors
167 | ConInfoTbl -- corresponding info table
168 | StaticConEntry -- static constructor entry point
169 | StaticInfoTbl -- corresponding info table
182 = RtsShouldNeverHappenCode
184 | RtsBlackHoleInfoTbl FastString -- black hole with info table name
186 | RtsUpdInfo -- upd_frame_info
187 | RtsSeqInfo -- seq_frame_info
188 | RtsGCEntryLabel String -- a heap check fail handler, eg stg_chk_2
189 | RtsMainCapability -- MainCapability
190 | Rts_Closure String -- misc rts closures, eg CHARLIKE_closure
191 | Rts_Info String -- misc rts itbls, eg MUT_ARR_PTRS_FROZEN_info
192 | Rts_Code String -- misc rts code
194 | RtsSelectorInfoTbl Bool{-updatable-} Int{-offset-} -- Selector thunks
195 | RtsSelectorEntry Bool{-updatable-} Int{-offset-}
197 | RtsApInfoTbl Bool{-updatable-} Int{-arity-} -- AP thunks
198 | RtsApEntry Bool{-updatable-} Int{-arity-}
208 -- Label Type: for generating C declarations.
220 mkClosureLabel id = IdLabel id Closure
221 mkSRTLabel id = IdLabel id SRT
222 mkInfoTableLabel id = IdLabel id InfoTbl
223 mkStdEntryLabel id = IdLabel id EntryStd
224 mkFastEntryLabel id arity = ASSERT(arity > 0)
225 IdLabel id (EntryFast arity)
227 mkRednCountsLabel id = IdLabel id RednCounts
229 mkStaticInfoTableLabel con = DataConLabel con StaticInfoTbl
230 mkConInfoTableLabel con = DataConLabel con ConInfoTbl
231 mkConEntryLabel con = DataConLabel con ConEntry
232 mkStaticConEntryLabel con = DataConLabel con StaticConEntry
235 mkReturnPtLabel uniq = CaseLabel uniq CaseReturnPt
236 mkReturnInfoLabel uniq = CaseLabel uniq CaseReturnInfo
237 mkVecTblLabel uniq = CaseLabel uniq CaseVecTbl
238 mkAltLabel uniq tag = CaseLabel uniq (CaseAlt tag)
239 mkDefaultLabel uniq = CaseLabel uniq CaseDefault
240 mkBitmapLabel uniq = CaseLabel uniq CaseBitmap
242 mkClosureTblLabel tycon = TyConLabel tycon
244 mkAsmTempLabel = AsmTempLabel
246 mkModuleInitLabel = ModuleInitLabel
247 mkPlainModuleInitLabel = PlainModuleInitLabel
249 -- Some fixed runtime system labels
251 mkErrorStdEntryLabel = RtsLabel RtsShouldNeverHappenCode
252 mkStgUpdatePAPLabel = RtsLabel (Rts_Code "stg_update_PAP")
253 mkSplitMarkerLabel = RtsLabel (Rts_Code "__stg_split_marker")
254 mkUpdInfoLabel = RtsLabel RtsUpdInfo
255 mkSeqInfoLabel = RtsLabel RtsSeqInfo
256 mkIndInfoLabel = RtsLabel (Rts_Info "stg_IND_info")
257 mkIndStaticInfoLabel = RtsLabel (Rts_Info "stg_IND_STATIC_info")
258 mkRtsGCEntryLabel str = RtsLabel (RtsGCEntryLabel str)
259 mkMainCapabilityLabel = RtsLabel RtsMainCapability
260 mkCharlikeClosureLabel = RtsLabel (Rts_Closure "stg_CHARLIKE_closure")
261 mkIntlikeClosureLabel = RtsLabel (Rts_Closure "stg_INTLIKE_closure")
262 mkMAP_FROZEN_infoLabel = RtsLabel (Rts_Info "stg_MUT_ARR_PTRS_FROZEN_info")
263 mkEMPTY_MVAR_infoLabel = RtsLabel (Rts_Info "stg_EMPTY_MVAR_info")
265 mkTopTickyCtrLabel = RtsLabel RtsTopTickyCtr
266 mkBlackHoleInfoTableLabel = RtsLabel (RtsBlackHoleInfoTbl FSLIT("stg_BLACKHOLE_info"))
267 mkCAFBlackHoleInfoTableLabel = RtsLabel (RtsBlackHoleInfoTbl FSLIT("stg_CAF_BLACKHOLE_info"))
268 mkSECAFBlackHoleInfoTableLabel = if opt_DoTickyProfiling then
269 RtsLabel (RtsBlackHoleInfoTbl FSLIT("stg_SE_CAF_BLACKHOLE_info"))
270 else -- RTS won't have info table unless -ticky is on
271 panic "mkSECAFBlackHoleInfoTableLabel requires -ticky"
272 mkRtsPrimOpLabel primop = RtsLabel (RtsPrimOp primop)
274 moduleRegdLabel = RtsLabel RtsModuleRegd
276 mkSelectorInfoLabel upd off = RtsLabel (RtsSelectorInfoTbl upd off)
277 mkSelectorEntryLabel upd off = RtsLabel (RtsSelectorEntry upd off)
279 mkApInfoTableLabel upd off = RtsLabel (RtsApInfoTbl upd off)
280 mkApEntryLabel upd off = RtsLabel (RtsApEntry upd off)
284 mkForeignLabel :: FastString -> Bool -> CLabel
285 mkForeignLabel str is_dynamic = ForeignLabel str is_dynamic
289 mkCC_Label cc = CC_Label cc
290 mkCCS_Label ccs = CCS_Label ccs
294 needsCDecl :: CLabel -> Bool -- False <=> it's pre-declared; don't bother
295 isAsmTemp :: CLabel -> Bool -- is a local temporary for native code generation
296 externallyVisibleCLabel :: CLabel -> Bool -- not C "static"
299 @needsCDecl@ is @True@ unless the thing is a deeply-@PreludeCore@-ish
300 object. {\em Also:} No need to spit out labels for things generated
301 by the flattener (in @AbsCUtils@)---it is careful to ensure references
302 to them are always backwards. These are return-point and vector-table
305 Declarations for (non-prelude) @Id@-based things are needed because of
308 Declarations for direct return points are needed, because they may be
309 let-no-escapes, which can be recursive.
312 needsCDecl (IdLabel _ _) = True
313 needsCDecl (CaseLabel _ CaseReturnPt) = True
314 needsCDecl (DataConLabel _ _) = True
315 needsCDecl (TyConLabel _) = True
316 needsCDecl (ModuleInitLabel _ _ _) = True
317 needsCDecl (PlainModuleInitLabel _) = True
319 needsCDecl (CaseLabel _ _) = False
320 needsCDecl (AsmTempLabel _) = False
321 needsCDecl (RtsLabel _) = False
322 needsCDecl (ForeignLabel _ _) = False
323 needsCDecl (CC_Label _) = False
324 needsCDecl (CCS_Label _) = False
327 Whether the label is an assembler temporary:
330 isAsmTemp (AsmTempLabel _) = True
334 C ``static'' or not...
335 From the point of view of the code generator, a name is
336 externally visible if it has to be declared as exported
337 in the .o file's symbol table; that is, made non-static.
340 externallyVisibleCLabel (DataConLabel _ _) = True
341 externallyVisibleCLabel (TyConLabel tc) = True
342 externallyVisibleCLabel (CaseLabel _ _) = False
343 externallyVisibleCLabel (AsmTempLabel _) = False
344 externallyVisibleCLabel (ModuleInitLabel _ _ _)= True
345 externallyVisibleCLabel (PlainModuleInitLabel _)= True
346 externallyVisibleCLabel (RtsLabel RtsModuleRegd) = False --hack
347 externallyVisibleCLabel (RtsLabel _) = True
348 externallyVisibleCLabel (ForeignLabel _ _) = True
349 externallyVisibleCLabel (IdLabel id _) = isExternalName id
350 externallyVisibleCLabel (CC_Label _) = False -- not strictly true
351 externallyVisibleCLabel (CCS_Label _) = False -- not strictly true
354 For generating correct types in label declarations...
357 labelType :: CLabel -> CLabelType
358 labelType (RtsLabel (RtsBlackHoleInfoTbl _)) = InfoTblType
359 labelType (RtsLabel (RtsSelectorInfoTbl _ _)) = InfoTblType
360 labelType (RtsLabel (RtsApInfoTbl _ _)) = InfoTblType
361 labelType (RtsLabel RtsUpdInfo) = InfoTblType
362 labelType (RtsLabel (Rts_Info _)) = InfoTblType
363 labelType (CaseLabel _ CaseReturnInfo) = InfoTblType
364 labelType (CaseLabel _ CaseReturnPt) = CodeType
365 labelType (CaseLabel _ CaseVecTbl) = VecTblType
366 labelType (TyConLabel _) = ClosureTblType
367 labelType (ModuleInitLabel _ _ _) = CodeType
368 labelType (PlainModuleInitLabel _) = CodeType
370 labelType (IdLabel _ info) =
372 InfoTbl -> InfoTblType
373 Closure -> ClosureType
376 labelType (DataConLabel _ info) =
378 ConInfoTbl -> InfoTblType
379 StaticInfoTbl -> InfoTblType
382 labelType _ = DataType
385 When referring to data in code, we need to know whether
386 that data resides in a DLL or not. [Win32 only.]
387 @labelDynamic@ returns @True@ if the label is located
388 in a DLL, be it a data reference or not.
391 labelDynamic :: CLabel -> Bool
394 -- The special case for RtsShouldNeverHappenCode is because the associated address is
395 -- NULL, i.e. not a DLL entry point
396 RtsLabel RtsShouldNeverHappenCode -> False
397 RtsLabel _ -> not opt_Static -- i.e., is the RTS in a DLL or not?
398 IdLabel n k -> isDllName n
399 DataConLabel n k -> isDllName n
400 TyConLabel tc -> isDllName (getName tc)
401 ForeignLabel _ d -> d
402 ModuleInitLabel m _ _ -> (not opt_Static) && (not (isHomeModule m))
403 PlainModuleInitLabel m -> (not opt_Static) && (not (isHomeModule m))
408 OLD?: These GRAN functions are needed for spitting out GRAN_FETCH() at the
409 right places. It is used to detect when the abstractC statement of an
410 CCodeBlock actually contains the code for a slow entry point. -- HWL
412 We need at least @Eq@ for @CLabels@, because we want to avoid
413 duplicate declarations in generating C (see @labelSeenTE@ in
416 -----------------------------------------------------------------------------
417 Printing out CLabels.
423 where <name> is <Module>_<name> for external names and <unique> for
424 internal names. <type> is one of the following:
427 srt Static reference table
429 ret Direct return address
431 <n>_alt Case alternative (tag n)
432 dflt Default case alternative
433 btm Large bitmap vector
434 closure Static closure
435 con_entry Dynamic Constructor entry code
436 con_info Dynamic Constructor info table
437 static_entry Static Constructor entry code
438 static_info Static Constructor info table
439 sel_info Selector info table
440 sel_entry Selector entry code
442 ccs Cost centre stack
445 pprCLabel :: CLabel -> SDoc
447 #if ! OMIT_NATIVE_CODEGEN
448 pprCLabel (AsmTempLabel u)
449 = text (fmtAsmLbl (show u))
453 #if ! OMIT_NATIVE_CODEGEN
454 getPprStyle $ \ sty ->
455 if asmStyle sty && underscorePrefix then
456 pp_cSEP <> pprCLbl lbl
461 pprCLbl (CaseLabel u CaseReturnPt)
462 = hcat [pprUnique u, pp_cSEP, ptext SLIT("ret")]
463 pprCLbl (CaseLabel u CaseReturnInfo)
464 = hcat [pprUnique u, pp_cSEP, ptext SLIT("info")]
465 pprCLbl (CaseLabel u CaseVecTbl)
466 = hcat [pprUnique u, pp_cSEP, ptext SLIT("vtbl")]
467 pprCLbl (CaseLabel u (CaseAlt tag))
468 = hcat [pprUnique u, pp_cSEP, int tag, pp_cSEP, ptext SLIT("alt")]
469 pprCLbl (CaseLabel u CaseDefault)
470 = hcat [pprUnique u, pp_cSEP, ptext SLIT("dflt")]
471 pprCLbl (CaseLabel u CaseBitmap)
472 = hcat [pprUnique u, pp_cSEP, ptext SLIT("btm")]
474 pprCLbl (RtsLabel RtsShouldNeverHappenCode) = ptext SLIT("NULL")
475 -- used to be stg_error_entry but Windows can't have DLL entry points as static
476 -- initialisers, and besides, this ShouldNeverHappen, right?
478 pprCLbl (RtsLabel RtsUpdInfo) = ptext SLIT("stg_upd_frame_info")
479 pprCLbl (RtsLabel RtsSeqInfo) = ptext SLIT("stg_seq_frame_info")
480 pprCLbl (RtsLabel RtsMainCapability) = ptext SLIT("MainCapability")
481 pprCLbl (RtsLabel (RtsGCEntryLabel str)) = text str
482 pprCLbl (RtsLabel (Rts_Closure str)) = text str
483 pprCLbl (RtsLabel (Rts_Info str)) = text str
484 pprCLbl (RtsLabel (Rts_Code str)) = text str
486 pprCLbl (RtsLabel RtsTopTickyCtr) = ptext SLIT("top_ct")
488 pprCLbl (RtsLabel (RtsBlackHoleInfoTbl info)) = ftext info
490 pprCLbl (RtsLabel (RtsSelectorInfoTbl upd_reqd offset))
491 = hcat [ptext SLIT("stg_sel_"), text (show offset),
493 then SLIT("_upd_info")
494 else SLIT("_noupd_info"))
497 pprCLbl (RtsLabel (RtsSelectorEntry upd_reqd offset))
498 = hcat [ptext SLIT("stg_sel_"), text (show offset),
500 then SLIT("_upd_entry")
501 else SLIT("_noupd_entry"))
504 pprCLbl (RtsLabel (RtsApInfoTbl upd_reqd arity))
505 = hcat [ptext SLIT("stg_ap_"), text (show arity),
507 then SLIT("_upd_info")
508 else SLIT("_noupd_info"))
511 pprCLbl (RtsLabel (RtsApEntry upd_reqd arity))
512 = hcat [ptext SLIT("stg_ap_"), text (show arity),
514 then SLIT("_upd_entry")
515 else SLIT("_noupd_entry"))
518 pprCLbl (RtsLabel (RtsPrimOp primop))
519 = ppr primop <> ptext SLIT("_fast")
521 pprCLbl (RtsLabel RtsModuleRegd)
522 = ptext SLIT("module_registered")
524 pprCLbl (ForeignLabel str _)
527 pprCLbl (TyConLabel tc)
528 = hcat [ppr tc, pp_cSEP, ptext SLIT("closure_tbl")]
530 pprCLbl (IdLabel id flavor) = ppr id <> ppIdFlavor flavor
531 pprCLbl (DataConLabel con flavor) = ppr con <> ppConFlavor flavor
533 pprCLbl (CC_Label cc) = ppr cc
534 pprCLbl (CCS_Label ccs) = ppr ccs
536 pprCLbl (ModuleInitLabel mod ver way)
537 = ptext SLIT("__stginit_") <> ftext (moduleNameFS (moduleName mod))
538 <> char '_' <> int ver <> char '_' <> text way
540 pprCLbl (PlainModuleInitLabel mod)
541 = ptext SLIT("__stginit_") <> ftext (moduleNameFS (moduleName mod))
543 ppIdFlavor :: IdLabelInfo -> SDoc
545 ppIdFlavor x = pp_cSEP <>
547 Closure -> ptext SLIT("closure")
548 SRT -> ptext SLIT("srt")
549 InfoTbl -> ptext SLIT("info")
550 EntryStd -> ptext SLIT("entry")
551 EntryFast arity -> --false:ASSERT (arity > 0)
552 (<>) (ptext SLIT("fast")) (int arity)
553 RednCounts -> ptext SLIT("ct")
556 ppConFlavor x = pp_cSEP <>
558 ConEntry -> ptext SLIT("con_entry")
559 ConInfoTbl -> ptext SLIT("con_info")
560 StaticConEntry -> ptext SLIT("static_entry")
561 StaticInfoTbl -> ptext SLIT("static_info")