2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 % $Id: CLabel.lhs,v 1.37 2000/07/03 14:59:25 simonmar Exp $
6 \section[CLabel]{@CLabel@: Information to make C Labels}
10 CLabel, -- abstract type
18 mkStaticConEntryLabel,
21 mkStaticInfoTableLabel,
48 mkCharlikeClosureLabel,
49 mkIntlikeClosureLabel,
50 mkMAP_FROZEN_infoLabel,
53 mkBlackHoleInfoTableLabel,
54 mkCAFBlackHoleInfoTableLabel,
55 mkSECAFBlackHoleInfoTableLabel,
65 mkCC_Label, mkCCS_Label,
67 needsCDecl, isAsmTemp, externallyVisibleCLabel,
69 CLabelType(..), labelType, labelDynamic,
72 #if ! OMIT_NATIVE_CODEGEN
78 #include "HsVersions.h"
80 #if ! OMIT_NATIVE_CODEGEN
81 import {-# SOURCE #-} MachMisc ( underscorePrefix, fmtAsmLbl )
84 import CmdLineOpts ( opt_Static, opt_DoTickyProfiling )
85 import CStrings ( pp_cSEP )
86 import DataCon ( ConTag, DataCon )
87 import Module ( ModuleName )
88 import Name ( Name, getName, isDllName, isExternallyVisibleName )
89 import TyCon ( TyCon )
90 import Unique ( pprUnique, Unique )
91 import PrimOp ( PrimOp, pprPrimOp )
92 import CostCentre ( CostCentre, CostCentreStack )
97 things we want to find out:
99 * should the labelled things be declared "static" (visible only in this file)?
101 * should it be declared "const" (read-only text space)?
103 * does it need declarations at all? (v common Prelude things are pre-declared)
105 * what type does it have? (for generating accurate enough C declarations
106 so that the C compiler won't complain).
110 = IdLabel -- A family of labels related to the
111 Name -- definition of a particular Id
114 | DataConLabel -- Ditto data constructors
118 | CaseLabel -- A family of labels related to a particular case expression
119 Unique -- Unique says which case expression
122 | TyConLabel TyCon -- currently only one kind of TyconLabel,
123 -- a 'Closure Table'.
125 | AsmTempLabel Unique
127 | ModuleInitLabel ModuleName
129 | RtsLabel RtsLabelInfo
131 | ForeignLabel FAST_STRING Bool -- a 'C' (or otherwise foreign) label
132 -- Bool <=> is dynamic
134 | CC_Label CostCentre
135 | CCS_Label CostCentreStack
142 = Closure -- Label for (static???) closure
144 | SRT -- Static reference table
146 | InfoTbl -- Info table for a closure; always read-only
148 | EntryStd -- Thunk, or "slow", code entry point
150 | EntryFast Int -- entry pt when no arg satisfaction chk needed;
151 -- Int is the arity of the function (to be
152 -- encoded into the name)
154 -- Ticky-ticky counting
155 | RednCounts -- Label of place to keep reduction-count info for
159 data DataConLabelInfo
160 = ConEntry -- the only kind of entry pt for constructors
161 | ConInfoTbl -- corresponding info table
162 | StaticConEntry -- static constructor entry point
163 | StaticInfoTbl -- corresponding info table
176 = RtsShouldNeverHappenCode
178 | RtsBlackHoleInfoTbl FAST_STRING -- black hole with info table name
180 | RtsUpdInfo -- upd_frame_info
181 | RtsSeqInfo -- seq_frame_info
182 | RtsGCEntryLabel String -- a heap check fail handler, eg stg_chk_2
183 | RtsMainRegTable -- MainRegTable (??? Capabilities wurble ???)
184 | Rts_Closure String -- misc rts closures, eg CHARLIKE_closure
185 | Rts_Info String -- misc rts itbls, eg MUT_ARR_PTRS_FROZEN_info
186 | Rts_Code String -- misc rts code
188 | RtsSelectorInfoTbl Bool{-updatable-} Int{-offset-} -- Selector thunks
189 | RtsSelectorEntry Bool{-updatable-} Int{-offset-}
191 | RtsApInfoTbl Bool{-updatable-} Int{-arity-} -- AP thunks
192 | RtsApEntry Bool{-updatable-} Int{-arity-}
202 -- Label Type: for generating C declarations.
214 mkClosureLabel id = IdLabel id Closure
215 mkSRTLabel id = IdLabel id SRT
216 mkInfoTableLabel id = IdLabel id InfoTbl
217 mkStdEntryLabel id = IdLabel id EntryStd
218 mkFastEntryLabel id arity = ASSERT(arity > 0)
219 IdLabel id (EntryFast arity)
221 mkRednCountsLabel id = IdLabel id RednCounts
223 mkStaticInfoTableLabel con = DataConLabel con StaticInfoTbl
224 mkConInfoTableLabel con = DataConLabel con ConInfoTbl
225 mkConEntryLabel con = DataConLabel con ConEntry
226 mkStaticConEntryLabel con = DataConLabel con StaticConEntry
229 mkReturnPtLabel uniq = CaseLabel uniq CaseReturnPt
230 mkReturnInfoLabel uniq = CaseLabel uniq CaseReturnInfo
231 mkVecTblLabel uniq = CaseLabel uniq CaseVecTbl
232 mkAltLabel uniq tag = CaseLabel uniq (CaseAlt tag)
233 mkDefaultLabel uniq = CaseLabel uniq CaseDefault
234 mkBitmapLabel uniq = CaseLabel uniq CaseBitmap
236 mkClosureTblLabel tycon = TyConLabel tycon
238 mkAsmTempLabel = AsmTempLabel
240 mkModuleInitLabel = ModuleInitLabel
242 -- Some fixed runtime system labels
244 mkErrorStdEntryLabel = RtsLabel RtsShouldNeverHappenCode
246 mkStgUpdatePAPLabel = RtsLabel (Rts_Code "stg_update_PAP")
247 mkSplitMarkerLabel = RtsLabel (Rts_Code "__stg_split_marker")
248 mkUpdInfoLabel = RtsLabel RtsUpdInfo
249 mkSeqInfoLabel = RtsLabel RtsSeqInfo
250 mkIndInfoLabel = RtsLabel (Rts_Info "IND_info")
251 mkIndStaticInfoLabel = RtsLabel (Rts_Info "IND_STATIC_info")
252 mkRtsGCEntryLabel str = RtsLabel (RtsGCEntryLabel str)
253 mkMainRegTableLabel = RtsLabel RtsMainRegTable
254 mkCharlikeClosureLabel = RtsLabel (Rts_Closure "CHARLIKE_closure")
255 mkIntlikeClosureLabel = RtsLabel (Rts_Closure "INTLIKE_closure")
256 mkMAP_FROZEN_infoLabel = RtsLabel (Rts_Info "MUT_ARR_PTRS_FROZEN_info")
258 mkTopTickyCtrLabel = RtsLabel RtsTopTickyCtr
259 mkBlackHoleInfoTableLabel = RtsLabel (RtsBlackHoleInfoTbl SLIT("BLACKHOLE_info"))
260 mkCAFBlackHoleInfoTableLabel = RtsLabel (RtsBlackHoleInfoTbl SLIT("CAF_BLACKHOLE_info"))
261 mkSECAFBlackHoleInfoTableLabel = if opt_DoTickyProfiling then
262 RtsLabel (RtsBlackHoleInfoTbl SLIT("SE_CAF_BLACKHOLE_info"))
263 else -- RTS won't have info table unless -ticky is on
264 panic "mkSECAFBlackHoleInfoTableLabel requires -ticky"
265 mkRtsPrimOpLabel primop = RtsLabel (RtsPrimOp primop)
267 moduleRegdLabel = RtsLabel RtsModuleRegd
269 mkSelectorInfoLabel upd off = RtsLabel (RtsSelectorInfoTbl upd off)
270 mkSelectorEntryLabel upd off = RtsLabel (RtsSelectorEntry upd off)
272 mkApInfoTableLabel upd off = RtsLabel (RtsApInfoTbl upd off)
273 mkApEntryLabel upd off = RtsLabel (RtsApEntry upd off)
277 mkForeignLabel :: FAST_STRING -> Bool -> CLabel
278 mkForeignLabel str is_dynamic = ForeignLabel str is_dynamic
282 mkCC_Label cc = CC_Label cc
283 mkCCS_Label ccs = CCS_Label ccs
287 needsCDecl :: CLabel -> Bool -- False <=> it's pre-declared; don't bother
288 isAsmTemp :: CLabel -> Bool -- is a local temporary for native code generation
289 externallyVisibleCLabel :: CLabel -> Bool -- not C "static"
292 @needsCDecl@ is @True@ unless the thing is a deeply-@PreludeCore@-ish
293 object. {\em Also:} No need to spit out labels for things generated
294 by the flattener (in @AbsCUtils@)---it is careful to ensure references
295 to them are always backwards. These are return-point and vector-table
298 Declarations for (non-prelude) @Id@-based things are needed because of
301 Declarations for direct return points are needed, because they may be
302 let-no-escapes, which can be recursive.
305 needsCDecl (IdLabel _ _) = True
306 needsCDecl (CaseLabel _ CaseReturnPt) = True
307 needsCDecl (DataConLabel _ _) = True
308 needsCDecl (CaseLabel _ _) = False
309 needsCDecl (TyConLabel _) = True
311 needsCDecl (AsmTempLabel _) = False
312 needsCDecl (ModuleInitLabel _) = False
313 needsCDecl (RtsLabel _) = False
314 needsCDecl (ForeignLabel _ _) = False
315 needsCDecl (CC_Label _) = False
316 needsCDecl (CCS_Label _) = False
319 Whether the label is an assembler temporary:
322 isAsmTemp (AsmTempLabel _) = True
326 C ``static'' or not...
327 From the point of view of the code generator, a name is
328 externally visible if it has to be declared as exported
329 in the .o file's symbol table; that is, made non-static.
332 externallyVisibleCLabel (DataConLabel _ _) = True
333 externallyVisibleCLabel (TyConLabel tc) = True
334 externallyVisibleCLabel (CaseLabel _ _) = False
335 externallyVisibleCLabel (AsmTempLabel _) = False
336 externallyVisibleCLabel (ModuleInitLabel _)= True
337 externallyVisibleCLabel (RtsLabel RtsModuleRegd) = False --hack
338 externallyVisibleCLabel (RtsLabel _) = True
339 externallyVisibleCLabel (ForeignLabel _ _) = True
340 externallyVisibleCLabel (IdLabel id _) = isExternallyVisibleName id
341 externallyVisibleCLabel (CC_Label _) = False -- not strictly true
342 externallyVisibleCLabel (CCS_Label _) = False -- not strictly true
345 For generating correct types in label declarations...
348 labelType :: CLabel -> CLabelType
349 labelType (RtsLabel (RtsBlackHoleInfoTbl _)) = InfoTblType
350 labelType (RtsLabel (RtsSelectorInfoTbl _ _)) = InfoTblType
351 labelType (RtsLabel (RtsApInfoTbl _ _)) = InfoTblType
352 labelType (RtsLabel RtsUpdInfo) = InfoTblType
353 labelType (CaseLabel _ CaseReturnInfo) = InfoTblType
354 labelType (CaseLabel _ CaseReturnPt) = CodeType
355 labelType (CaseLabel _ CaseVecTbl) = VecTblType
356 labelType (TyConLabel _) = ClosureTblType
358 labelType (IdLabel _ info) =
360 InfoTbl -> InfoTblType
361 Closure -> ClosureType
364 labelType (DataConLabel _ info) =
366 ConInfoTbl -> InfoTblType
367 StaticInfoTbl -> InfoTblType
370 labelType _ = DataType
373 When referring to data in code, we need to know whether
374 that data resides in a DLL or not. [Win32 only.]
375 @labelDynamic@ returns @True@ if the label is located
376 in a DLL, be it a data reference or not.
379 labelDynamic :: CLabel -> Bool
382 RtsLabel _ -> not opt_Static -- i.e., is the RTS in a DLL or not?
383 IdLabel n k -> isDllName n
384 DataConLabel n k -> isDllName n
385 TyConLabel tc -> isDllName (getName tc)
386 ForeignLabel _ d -> d
391 OLD?: These GRAN functions are needed for spitting out GRAN_FETCH() at the
392 right places. It is used to detect when the abstractC statement of an
393 CCodeBlock actually contains the code for a slow entry point. -- HWL
395 We need at least @Eq@ for @CLabels@, because we want to avoid
396 duplicate declarations in generating C (see @labelSeenTE@ in
399 -----------------------------------------------------------------------------
400 Printing out CLabels.
406 where <name> is <Module>_<name> for external names and <unique> for
407 internal names. <type> is one of the following:
410 srt Static reference table
412 ret Direct return address
414 <n>_alt Case alternative (tag n)
415 dflt Default case alternative
416 btm Large bitmap vector
417 closure Static closure
418 con_entry Dynamic Constructor entry code
419 con_info Dynamic Constructor info table
420 static_entry Static Constructor entry code
421 static_info Static Constructor info table
422 sel_info Selector info table
423 sel_entry Selector entry code
425 ccs Cost centre stack
428 -- specialised for PprAsm: saves lots of arg passing in NCG
429 #if ! OMIT_NATIVE_CODEGEN
430 pprCLabel_asm = pprCLabel
433 pprCLabel :: CLabel -> SDoc
435 #if ! OMIT_NATIVE_CODEGEN
436 pprCLabel (AsmTempLabel u)
437 = text (fmtAsmLbl (show u))
441 #if ! OMIT_NATIVE_CODEGEN
442 getPprStyle $ \ sty ->
443 if asmStyle sty && underscorePrefix then
444 pp_cSEP <> pprCLbl lbl
449 pprCLbl (CaseLabel u CaseReturnPt)
450 = hcat [pprUnique u, pp_cSEP, ptext SLIT("ret")]
451 pprCLbl (CaseLabel u CaseReturnInfo)
452 = hcat [pprUnique u, pp_cSEP, ptext SLIT("info")]
453 pprCLbl (CaseLabel u CaseVecTbl)
454 = hcat [pprUnique u, pp_cSEP, ptext SLIT("vtbl")]
455 pprCLbl (CaseLabel u (CaseAlt tag))
456 = hcat [pprUnique u, pp_cSEP, int tag, pp_cSEP, ptext SLIT("alt")]
457 pprCLbl (CaseLabel u CaseDefault)
458 = hcat [pprUnique u, pp_cSEP, ptext SLIT("dflt")]
459 pprCLbl (CaseLabel u CaseBitmap)
460 = hcat [pprUnique u, pp_cSEP, ptext SLIT("btm")]
462 pprCLbl (RtsLabel RtsShouldNeverHappenCode) = ptext SLIT("stg_error_entry")
464 pprCLbl (RtsLabel RtsUpdInfo) = ptext SLIT("upd_frame_info")
465 pprCLbl (RtsLabel RtsSeqInfo) = ptext SLIT("seq_frame_info")
466 pprCLbl (RtsLabel RtsMainRegTable) = ptext SLIT("MainRegTable")
467 pprCLbl (RtsLabel (RtsGCEntryLabel str)) = text str
468 pprCLbl (RtsLabel (Rts_Closure str)) = text str
469 pprCLbl (RtsLabel (Rts_Info str)) = text str
470 pprCLbl (RtsLabel (Rts_Code str)) = text str
472 pprCLbl (RtsLabel RtsTopTickyCtr) = ptext SLIT("top_ct")
474 pprCLbl (RtsLabel (RtsBlackHoleInfoTbl info)) = ptext info
476 pprCLbl (RtsLabel (RtsSelectorInfoTbl upd_reqd offset))
477 = hcat [ptext SLIT("__sel_"), text (show offset),
479 then SLIT("_upd_info")
480 else SLIT("_noupd_info"))
483 pprCLbl (RtsLabel (RtsSelectorEntry upd_reqd offset))
484 = hcat [ptext SLIT("__sel_"), text (show offset),
486 then SLIT("_upd_entry")
487 else SLIT("_noupd_entry"))
490 pprCLbl (RtsLabel (RtsApInfoTbl upd_reqd arity))
491 = hcat [ptext SLIT("__ap_"), text (show arity),
493 then SLIT("_upd_info")
494 else SLIT("_noupd_info"))
497 pprCLbl (RtsLabel (RtsApEntry upd_reqd arity))
498 = hcat [ptext SLIT("__ap_"), text (show arity),
500 then SLIT("_upd_entry")
501 else SLIT("_noupd_entry"))
504 pprCLbl (RtsLabel (RtsPrimOp primop))
505 = pprPrimOp primop <> ptext SLIT("_fast")
507 pprCLbl (RtsLabel RtsModuleRegd)
508 = ptext SLIT("module_registered")
510 pprCLbl (ForeignLabel str _)
513 pprCLbl (TyConLabel tc)
514 = hcat [ppr tc, pp_cSEP, ptext SLIT("closure_tbl")]
516 pprCLbl (IdLabel id flavor) = ppr id <> ppIdFlavor flavor
517 pprCLbl (DataConLabel con flavor) = ppr con <> ppConFlavor flavor
519 pprCLbl (CC_Label cc) = ppr cc
520 pprCLbl (CCS_Label ccs) = ppr ccs
522 pprCLbl (ModuleInitLabel mod) = ptext SLIT("__init_") <> ptext mod
524 ppIdFlavor :: IdLabelInfo -> SDoc
526 ppIdFlavor x = pp_cSEP <>
528 Closure -> ptext SLIT("closure")
529 SRT -> ptext SLIT("srt")
530 InfoTbl -> ptext SLIT("info")
531 EntryStd -> ptext SLIT("entry")
532 EntryFast arity -> --false:ASSERT (arity > 0)
533 (<>) (ptext SLIT("fast")) (int arity)
534 RednCounts -> ptext SLIT("ct")
537 ppConFlavor x = pp_cSEP <>
539 ConEntry -> ptext SLIT("con_entry")
540 ConInfoTbl -> ptext SLIT("con_info")
541 StaticConEntry -> ptext SLIT("static_entry")
542 StaticInfoTbl -> ptext SLIT("static_info")