2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 % $Id: CLabel.lhs,v 1.31 2000/03/16 12:37:06 simonmar Exp $
6 \section[CLabel]{@CLabel@: Information to make C Labels}
10 CLabel, -- abstract type
18 mkStaticConEntryLabel,
22 mkStaticInfoTableLabel,
42 mkBlackHoleInfoTableLabel,
43 mkCAFBlackHoleInfoTableLabel,
44 mkSECAFBlackHoleInfoTableLabel,
52 mkCC_Label, mkCCS_Label,
54 needsCDecl, isAsmTemp, externallyVisibleCLabel,
56 CLabelType(..), labelType, labelDynamic,
59 #if ! OMIT_NATIVE_CODEGEN
65 #include "HsVersions.h"
67 #if ! OMIT_NATIVE_CODEGEN
68 import {-# SOURCE #-} MachMisc ( underscorePrefix, fmtAsmLbl )
71 import CmdLineOpts ( opt_Static, opt_DoTickyProfiling )
72 import CStrings ( pp_cSEP )
73 import DataCon ( ConTag, DataCon )
74 import Module ( isDynamicModule, ModuleName, moduleNameString )
75 import Name ( Name, getName, isExternallyVisibleName, nameModule, isLocallyDefinedName )
76 import TyCon ( TyCon )
77 import Unique ( pprUnique, Unique )
78 import PrimOp ( PrimOp, pprPrimOp )
79 import CostCentre ( CostCentre, CostCentreStack )
84 things we want to find out:
86 * should the labelled things be declared "static" (visible only in this file)?
88 * should it be declared "const" (read-only text space)?
90 * does it need declarations at all? (v common Prelude things are pre-declared)
92 * what type does it have? (for generating accurate enough C declarations
93 so that the C compiler won't complain).
97 = IdLabel -- A family of labels related to the
98 Name -- definition of a particular Id
101 | DataConLabel -- Ditto data constructors
105 | CaseLabel -- A family of labels related to a particular case expression
106 Unique -- Unique says which case expression
109 | TyConLabel TyCon -- currently only one kind of TyconLabel,
110 -- a 'Closure Table'.
112 | AsmTempLabel Unique
114 | ModuleInitLabel ModuleName
116 | RtsLabel RtsLabelInfo
118 | CC_Label CostCentre
119 | CCS_Label CostCentreStack
126 = Closure -- Label for (static???) closure
128 | SRT -- Static reference table
130 | InfoTbl -- Info table for a closure; always read-only
132 | EntryStd -- Thunk, or "slow", code entry point
134 | EntryFast Int -- entry pt when no arg satisfaction chk needed;
135 -- Int is the arity of the function (to be
136 -- encoded into the name)
138 -- Ticky-ticky counting
139 | RednCounts -- Label of place to keep reduction-count info for
143 data DataConLabelInfo
144 = ConEntry -- the only kind of entry pt for constructors
145 | ConInfoTbl -- corresponding info table
147 | StaticClosure -- Static constructor closure
148 -- e.g., nullary constructor
149 | StaticConEntry -- static constructor entry point
150 | StaticInfoTbl -- corresponding info table
163 = RtsShouldNeverHappenCode
165 | RtsBlackHoleInfoTbl FAST_STRING -- black hole with info table name
169 | RtsSelectorInfoTbl Bool{-updatable-} Int{-offset-} -- Selector thunks
170 | RtsSelectorEntry Bool{-updatable-} Int{-offset-}
172 | RtsApInfoTbl Bool{-updatable-} Int{-arity-} -- AP thunks
173 | RtsApEntry Bool{-updatable-} Int{-arity-}
183 -- Label Type: for generating C declarations.
195 mkClosureLabel id = IdLabel id Closure
196 mkSRTLabel id = IdLabel id SRT
197 mkInfoTableLabel id = IdLabel id InfoTbl
198 mkStdEntryLabel id = IdLabel id EntryStd
199 mkFastEntryLabel id arity = ASSERT(arity > 0)
200 IdLabel id (EntryFast arity)
202 mkRednCountsLabel id = IdLabel id RednCounts
204 mkStaticClosureLabel con = DataConLabel con StaticClosure
205 mkStaticInfoTableLabel con = DataConLabel con StaticInfoTbl
206 mkConInfoTableLabel con = DataConLabel con ConInfoTbl
207 mkConEntryLabel con = DataConLabel con ConEntry
208 mkStaticConEntryLabel con = DataConLabel con StaticConEntry
211 mkReturnPtLabel uniq = CaseLabel uniq CaseReturnPt
212 mkReturnInfoLabel uniq = CaseLabel uniq CaseReturnInfo
213 mkVecTblLabel uniq = CaseLabel uniq CaseVecTbl
214 mkAltLabel uniq tag = CaseLabel uniq (CaseAlt tag)
215 mkDefaultLabel uniq = CaseLabel uniq CaseDefault
216 mkBitmapLabel uniq = CaseLabel uniq CaseBitmap
218 mkClosureTblLabel tycon = TyConLabel tycon
220 mkAsmTempLabel = AsmTempLabel
222 mkModuleInitLabel = ModuleInitLabel
224 -- Some fixed runtime system labels
226 mkErrorStdEntryLabel = RtsLabel RtsShouldNeverHappenCode
227 mkUpdInfoLabel = RtsLabel RtsUpdInfo
228 mkTopTickyCtrLabel = RtsLabel RtsTopTickyCtr
229 mkBlackHoleInfoTableLabel = RtsLabel (RtsBlackHoleInfoTbl SLIT("BLACKHOLE_info"))
230 mkCAFBlackHoleInfoTableLabel = RtsLabel (RtsBlackHoleInfoTbl SLIT("CAF_BLACKHOLE_info"))
231 mkSECAFBlackHoleInfoTableLabel = if opt_DoTickyProfiling then
232 RtsLabel (RtsBlackHoleInfoTbl SLIT("SE_CAF_BLACKHOLE_info"))
233 else -- RTS won't have info table unless -ticky is on
234 panic "mkSECAFBlackHoleInfoTableLabel requires -ticky"
235 mkRtsPrimOpLabel primop = RtsLabel (RtsPrimOp primop)
237 moduleRegdLabel = RtsLabel RtsModuleRegd
239 mkSelectorInfoLabel upd off = RtsLabel (RtsSelectorInfoTbl upd off)
240 mkSelectorEntryLabel upd off = RtsLabel (RtsSelectorEntry upd off)
242 mkApInfoTableLabel upd off = RtsLabel (RtsApInfoTbl upd off)
243 mkApEntryLabel upd off = RtsLabel (RtsApEntry upd off)
247 mkCC_Label cc = CC_Label cc
248 mkCCS_Label ccs = CCS_Label ccs
252 needsCDecl :: CLabel -> Bool -- False <=> it's pre-declared; don't bother
253 isAsmTemp :: CLabel -> Bool -- is a local temporary for native code generation
254 externallyVisibleCLabel :: CLabel -> Bool -- not C "static"
257 @needsCDecl@ is @True@ unless the thing is a deeply-@PreludeCore@-ish
258 object. {\em Also:} No need to spit out labels for things generated
259 by the flattener (in @AbsCUtils@)---it is careful to ensure references
260 to them are always backwards. These are return-point and vector-table
263 Declarations for (non-prelude) @Id@-based things are needed because of
266 Declarations for direct return points are needed, because they may be
267 let-no-escapes, which can be recursive.
270 needsCDecl (IdLabel _ _) = True
271 needsCDecl (CaseLabel _ CaseReturnPt) = True
272 needsCDecl (DataConLabel _ _) = True
273 needsCDecl (CaseLabel _ _) = False
274 needsCDecl (TyConLabel _) = True
276 needsCDecl (AsmTempLabel _) = False
277 needsCDecl (ModuleInitLabel _) = False
278 needsCDecl (RtsLabel _) = False
279 needsCDecl (CC_Label _) = False
280 needsCDecl (CCS_Label _) = False
283 Whether the label is an assembler temporary:
286 isAsmTemp (AsmTempLabel _) = True
290 C ``static'' or not...
291 From the point of view of the code generator, a name is
292 externally visible if it has to be declared as exported
293 in the .o file's symbol table; that is, made non-static.
296 externallyVisibleCLabel (DataConLabel _ _) = True
297 externallyVisibleCLabel (TyConLabel tc) = True
298 externallyVisibleCLabel (CaseLabel _ _) = False
299 externallyVisibleCLabel (AsmTempLabel _) = False
300 externallyVisibleCLabel (ModuleInitLabel _)= True
301 externallyVisibleCLabel (RtsLabel RtsModuleRegd) = False --hack
302 externallyVisibleCLabel (RtsLabel _) = True
303 externallyVisibleCLabel (IdLabel id _) = isExternallyVisibleName id
304 externallyVisibleCLabel (CC_Label _) = False -- not strictly true
305 externallyVisibleCLabel (CCS_Label _) = False -- not strictly true
308 For generating correct types in label declarations...
311 labelType :: CLabel -> CLabelType
312 labelType (RtsLabel (RtsBlackHoleInfoTbl _)) = InfoTblType
313 labelType (RtsLabel (RtsSelectorInfoTbl _ _)) = InfoTblType
314 labelType (RtsLabel (RtsApInfoTbl _ _)) = InfoTblType
315 labelType (RtsLabel RtsUpdInfo) = InfoTblType
316 labelType (CaseLabel _ CaseReturnInfo) = InfoTblType
317 labelType (CaseLabel _ CaseReturnPt) = CodeType
318 labelType (CaseLabel _ CaseVecTbl) = VecTblType
319 labelType (TyConLabel _) = ClosureTblType
321 labelType (IdLabel _ info) =
323 InfoTbl -> InfoTblType
324 Closure -> ClosureType
327 labelType (DataConLabel _ info) =
329 ConInfoTbl -> InfoTblType
330 StaticInfoTbl -> InfoTblType
331 StaticClosure -> ClosureType
334 labelType _ = DataType
337 When referring to data in code, we need to know whether
338 that data resides in a DLL or not. [Win32 only.]
339 @labelDynamic@ returns @True@ if the label is located
340 in a DLL, be it a data reference or not.
343 labelDynamic :: CLabel -> Bool
346 RtsLabel _ -> not opt_Static -- i.e., is the RTS in a DLL or not?
347 IdLabel n k | not (isLocallyDefinedName n) -> isDynamicModule (nameModule n)
348 DataConLabel n k | not (isLocallyDefinedName n) -> isDynamicModule (nameModule n)
349 TyConLabel tc | not (isLocallyDefinedName (getName tc)) -> isDynamicModule (nameModule (getName tc))
355 OLD?: These GRAN functions are needed for spitting out GRAN_FETCH() at the
356 right places. It is used to detect when the abstractC statement of an
357 CCodeBlock actually contains the code for a slow entry point. -- HWL
359 We need at least @Eq@ for @CLabels@, because we want to avoid
360 duplicate declarations in generating C (see @labelSeenTE@ in
363 -----------------------------------------------------------------------------
364 Printing out CLabels.
370 where <name> is <Module>_<name> for external names and <unique> for
371 internal names. <type> is one of the following:
374 srt Static reference table
376 ret Direct return address
378 <n>_alt Case alternative (tag n)
379 dflt Default case alternative
380 btm Large bitmap vector
381 closure Static closure
382 static_closure Static closure (???)
383 con_entry Dynamic Constructor entry code
384 con_info Dynamic Constructor info table
385 static_entry Static Constructor entry code
386 static_info Static Constructor info table
387 sel_info Selector info table
388 sel_entry Selector entry code
390 ccs Cost centre stack
393 -- specialised for PprAsm: saves lots of arg passing in NCG
394 #if ! OMIT_NATIVE_CODEGEN
395 pprCLabel_asm = pprCLabel
398 pprCLabel :: CLabel -> SDoc
400 #if ! OMIT_NATIVE_CODEGEN
401 pprCLabel (AsmTempLabel u)
402 = text (fmtAsmLbl (show u))
406 #if ! OMIT_NATIVE_CODEGEN
407 getPprStyle $ \ sty ->
408 if asmStyle sty && underscorePrefix then
409 pp_cSEP <> pprCLbl lbl
414 pprCLbl (CaseLabel u CaseReturnPt)
415 = hcat [pprUnique u, pp_cSEP, ptext SLIT("ret")]
416 pprCLbl (CaseLabel u CaseReturnInfo)
417 = hcat [pprUnique u, pp_cSEP, ptext SLIT("info")]
418 pprCLbl (CaseLabel u CaseVecTbl)
419 = hcat [pprUnique u, pp_cSEP, ptext SLIT("vtbl")]
420 pprCLbl (CaseLabel u (CaseAlt tag))
421 = hcat [pprUnique u, pp_cSEP, int tag, pp_cSEP, ptext SLIT("alt")]
422 pprCLbl (CaseLabel u CaseDefault)
423 = hcat [pprUnique u, pp_cSEP, ptext SLIT("dflt")]
424 pprCLbl (CaseLabel u CaseBitmap)
425 = hcat [pprUnique u, pp_cSEP, ptext SLIT("btm")]
427 pprCLbl (RtsLabel RtsShouldNeverHappenCode) = ptext SLIT("stg_error_entry")
429 pprCLbl (RtsLabel RtsUpdInfo) = ptext SLIT("Upd_frame_info")
431 pprCLbl (RtsLabel RtsTopTickyCtr) = ptext SLIT("top_ct")
433 pprCLbl (RtsLabel (RtsBlackHoleInfoTbl info)) = ptext info
435 pprCLbl (RtsLabel (RtsSelectorInfoTbl upd_reqd offset))
436 = hcat [ptext SLIT("__sel_"), text (show offset),
438 then SLIT("_upd_info")
439 else SLIT("_noupd_info"))
442 pprCLbl (RtsLabel (RtsSelectorEntry upd_reqd offset))
443 = hcat [ptext SLIT("__sel_"), text (show offset),
445 then SLIT("_upd_entry")
446 else SLIT("_noupd_entry"))
449 pprCLbl (RtsLabel (RtsApInfoTbl upd_reqd arity))
450 = hcat [ptext SLIT("__ap_"), text (show arity),
452 then SLIT("_upd_info")
453 else SLIT("_noupd_info"))
456 pprCLbl (RtsLabel (RtsApEntry upd_reqd arity))
457 = hcat [ptext SLIT("__ap_"), text (show arity),
459 then SLIT("_upd_entry")
460 else SLIT("_noupd_entry"))
463 pprCLbl (RtsLabel (RtsPrimOp primop))
464 = pprPrimOp primop <> ptext SLIT("_fast")
466 pprCLbl (RtsLabel RtsModuleRegd)
467 = ptext SLIT("module_registered")
469 pprCLbl (TyConLabel tc)
470 = hcat [ppr tc, pp_cSEP, ptext SLIT("closure_tbl")]
472 pprCLbl (IdLabel id flavor) = ppr id <> ppIdFlavor flavor
473 pprCLbl (DataConLabel con flavor) = ppr con <> ppConFlavor flavor
475 pprCLbl (CC_Label cc) = ppr cc
476 pprCLbl (CCS_Label ccs) = ppr ccs
478 pprCLbl (ModuleInitLabel mod) = ptext SLIT("__init_") <> ptext mod
480 ppIdFlavor :: IdLabelInfo -> SDoc
482 ppIdFlavor x = pp_cSEP <>
484 Closure -> ptext SLIT("closure")
485 SRT -> ptext SLIT("srt")
486 InfoTbl -> ptext SLIT("info")
487 EntryStd -> ptext SLIT("entry")
488 EntryFast arity -> --false:ASSERT (arity > 0)
489 (<>) (ptext SLIT("fast")) (int arity)
490 RednCounts -> ptext SLIT("ct")
493 ppConFlavor x = pp_cSEP <>
495 StaticClosure -> ptext SLIT("static_closure")
496 ConEntry -> ptext SLIT("con_entry")
497 ConInfoTbl -> ptext SLIT("con_info")
498 StaticConEntry -> ptext SLIT("static_entry")
499 StaticInfoTbl -> ptext SLIT("static_info")