2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 % $Id: CLabel.lhs,v 1.30 1999/12/02 17:57:13 simonmar Exp $
6 \section[CLabel]{@CLabel@: Information to make C Labels}
10 CLabel, -- abstract type
18 mkStaticConEntryLabel,
22 mkStaticInfoTableLabel,
40 mkBlackHoleInfoTableLabel,
41 mkCAFBlackHoleInfoTableLabel,
42 mkSECAFBlackHoleInfoTableLabel,
48 mkCC_Label, mkCCS_Label,
50 needsCDecl, isAsmTemp, externallyVisibleCLabel,
52 CLabelType(..), labelType, labelDynamic,
55 #if ! OMIT_NATIVE_CODEGEN
61 #include "HsVersions.h"
63 #if ! OMIT_NATIVE_CODEGEN
64 import {-# SOURCE #-} MachMisc ( underscorePrefix, fmtAsmLbl )
67 import CmdLineOpts ( opt_Static, opt_DoTickyProfiling )
68 import CStrings ( pp_cSEP )
69 import DataCon ( ConTag, DataCon )
70 import Module ( isDynamicModule )
71 import Name ( Name, getName, isExternallyVisibleName, nameModule, isLocallyDefinedName )
72 import TyCon ( TyCon )
73 import Unique ( pprUnique, Unique )
74 import PrimOp ( PrimOp, pprPrimOp )
75 import CostCentre ( CostCentre, CostCentreStack )
80 things we want to find out:
82 * should the labelled things be declared "static" (visible only in this file)?
84 * should it be declared "const" (read-only text space)?
86 * does it need declarations at all? (v common Prelude things are pre-declared)
88 * what type does it have? (for generating accurate enough C declarations
89 so that the C compiler won't complain).
93 = IdLabel -- A family of labels related to the
94 Name -- definition of a particular Id
97 | DataConLabel -- Ditto data constructors
101 | CaseLabel -- A family of labels related to a particular case expression
102 Unique -- Unique says which case expression
105 | TyConLabel TyCon -- currently only one kind of TyconLabel,
106 -- a 'Closure Table'.
108 | AsmTempLabel Unique
110 | RtsLabel RtsLabelInfo
112 | CC_Label CostCentre
113 | CCS_Label CostCentreStack
120 = Closure -- Label for (static???) closure
122 | SRT -- Static reference table
124 | InfoTbl -- Info table for a closure; always read-only
126 | EntryStd -- Thunk, or "slow", code entry point
128 | EntryFast Int -- entry pt when no arg satisfaction chk needed;
129 -- Int is the arity of the function (to be
130 -- encoded into the name)
132 -- Ticky-ticky counting
133 | RednCounts -- Label of place to keep reduction-count info for
137 data DataConLabelInfo
138 = ConEntry -- the only kind of entry pt for constructors
139 | ConInfoTbl -- corresponding info table
141 | StaticClosure -- Static constructor closure
142 -- e.g., nullary constructor
143 | StaticConEntry -- static constructor entry point
144 | StaticInfoTbl -- corresponding info table
157 = RtsShouldNeverHappenCode
159 | RtsBlackHoleInfoTbl FAST_STRING -- black hole with info table name
163 | RtsSelectorInfoTbl Bool{-updatable-} Int{-offset-} -- Selector thunks
164 | RtsSelectorEntry Bool{-updatable-} Int{-offset-}
166 | RtsApInfoTbl Bool{-updatable-} Int{-arity-} -- AP thunks
167 | RtsApEntry Bool{-updatable-} Int{-arity-}
175 -- Label Type: for generating C declarations.
187 mkClosureLabel id = IdLabel id Closure
188 mkSRTLabel id = IdLabel id SRT
189 mkInfoTableLabel id = IdLabel id InfoTbl
190 mkStdEntryLabel id = IdLabel id EntryStd
191 mkFastEntryLabel id arity = ASSERT(arity > 0)
192 IdLabel id (EntryFast arity)
194 mkRednCountsLabel id = IdLabel id RednCounts
196 mkStaticClosureLabel con = DataConLabel con StaticClosure
197 mkStaticInfoTableLabel con = DataConLabel con StaticInfoTbl
198 mkConInfoTableLabel con = DataConLabel con ConInfoTbl
199 mkConEntryLabel con = DataConLabel con ConEntry
200 mkStaticConEntryLabel con = DataConLabel con StaticConEntry
203 mkReturnPtLabel uniq = CaseLabel uniq CaseReturnPt
204 mkReturnInfoLabel uniq = CaseLabel uniq CaseReturnInfo
205 mkVecTblLabel uniq = CaseLabel uniq CaseVecTbl
206 mkAltLabel uniq tag = CaseLabel uniq (CaseAlt tag)
207 mkDefaultLabel uniq = CaseLabel uniq CaseDefault
208 mkBitmapLabel uniq = CaseLabel uniq CaseBitmap
210 mkClosureTblLabel tycon = TyConLabel tycon
212 mkAsmTempLabel = AsmTempLabel
214 -- Some fixed runtime system labels
216 mkErrorStdEntryLabel = RtsLabel RtsShouldNeverHappenCode
217 mkUpdInfoLabel = RtsLabel RtsUpdInfo
218 mkTopTickyCtrLabel = RtsLabel RtsTopTickyCtr
219 mkBlackHoleInfoTableLabel = RtsLabel (RtsBlackHoleInfoTbl SLIT("BLACKHOLE_info"))
220 mkCAFBlackHoleInfoTableLabel = RtsLabel (RtsBlackHoleInfoTbl SLIT("CAF_BLACKHOLE_info"))
221 mkSECAFBlackHoleInfoTableLabel = if opt_DoTickyProfiling then
222 RtsLabel (RtsBlackHoleInfoTbl SLIT("SE_CAF_BLACKHOLE_info"))
223 else -- RTS won't have info table unless -ticky is on
224 panic "mkSECAFBlackHoleInfoTableLabel requires -ticky"
225 mkRtsPrimOpLabel primop = RtsLabel (RtsPrimOp primop)
227 mkSelectorInfoLabel upd off = RtsLabel (RtsSelectorInfoTbl upd off)
228 mkSelectorEntryLabel upd off = RtsLabel (RtsSelectorEntry upd off)
230 mkApInfoTableLabel upd off = RtsLabel (RtsApInfoTbl upd off)
231 mkApEntryLabel upd off = RtsLabel (RtsApEntry upd off)
235 mkCC_Label cc = CC_Label cc
236 mkCCS_Label ccs = CCS_Label ccs
240 needsCDecl :: CLabel -> Bool -- False <=> it's pre-declared; don't bother
241 isAsmTemp :: CLabel -> Bool -- is a local temporary for native code generation
242 externallyVisibleCLabel :: CLabel -> Bool -- not C "static"
245 @needsCDecl@ is @True@ unless the thing is a deeply-@PreludeCore@-ish
246 object. {\em Also:} No need to spit out labels for things generated
247 by the flattener (in @AbsCUtils@)---it is careful to ensure references
248 to them are always backwards. These are return-point and vector-table
251 Declarations for (non-prelude) @Id@-based things are needed because of
254 Declarations for direct return points are needed, because they may be
255 let-no-escapes, which can be recursive.
258 needsCDecl (IdLabel _ _) = True
259 needsCDecl (CaseLabel _ CaseReturnPt) = True
260 needsCDecl (DataConLabel _ _) = True
261 needsCDecl (CaseLabel _ _) = False
262 needsCDecl (TyConLabel _) = True
264 needsCDecl (AsmTempLabel _) = False
265 needsCDecl (RtsLabel _) = False
266 needsCDecl (CC_Label _) = False
267 needsCDecl (CCS_Label _) = False
270 Whether the label is an assembler temporary:
273 isAsmTemp (AsmTempLabel _) = True
277 C ``static'' or not...
278 From the point of view of the code generator, a name is
279 externally visible if it has to be declared as exported
280 in the .o file's symbol table; that is, made non-static.
283 externallyVisibleCLabel (DataConLabel _ _) = True
284 externallyVisibleCLabel (TyConLabel tc) = True
285 externallyVisibleCLabel (CaseLabel _ _) = False
286 externallyVisibleCLabel (AsmTempLabel _) = False
287 externallyVisibleCLabel (RtsLabel _) = True
288 externallyVisibleCLabel (IdLabel id _) = isExternallyVisibleName id
289 externallyVisibleCLabel (CC_Label _) = False -- not strictly true
290 externallyVisibleCLabel (CCS_Label _) = False -- not strictly true
293 For generating correct types in label declarations...
296 labelType :: CLabel -> CLabelType
297 labelType (RtsLabel (RtsBlackHoleInfoTbl _)) = InfoTblType
298 labelType (RtsLabel (RtsSelectorInfoTbl _ _)) = InfoTblType
299 labelType (RtsLabel (RtsApInfoTbl _ _)) = InfoTblType
300 labelType (RtsLabel RtsUpdInfo) = InfoTblType
301 labelType (CaseLabel _ CaseReturnInfo) = InfoTblType
302 labelType (CaseLabel _ CaseReturnPt) = CodeType
303 labelType (CaseLabel _ CaseVecTbl) = VecTblType
304 labelType (TyConLabel _) = ClosureTblType
306 labelType (IdLabel _ info) =
308 InfoTbl -> InfoTblType
309 Closure -> ClosureType
312 labelType (DataConLabel _ info) =
314 ConInfoTbl -> InfoTblType
315 StaticInfoTbl -> InfoTblType
316 StaticClosure -> ClosureType
319 labelType _ = DataType
322 When referring to data in code, we need to know whether
323 that data resides in a DLL or not. [Win32 only.]
324 @labelDynamic@ returns @True@ if the label is located
325 in a DLL, be it a data reference or not.
328 labelDynamic :: CLabel -> Bool
331 RtsLabel _ -> not opt_Static -- i.e., is the RTS in a DLL or not?
332 IdLabel n k | not (isLocallyDefinedName n) -> isDynamicModule (nameModule n)
333 DataConLabel n k | not (isLocallyDefinedName n) -> isDynamicModule (nameModule n)
334 TyConLabel tc | not (isLocallyDefinedName (getName tc)) -> isDynamicModule (nameModule (getName tc))
340 OLD?: These GRAN functions are needed for spitting out GRAN_FETCH() at the
341 right places. It is used to detect when the abstractC statement of an
342 CCodeBlock actually contains the code for a slow entry point. -- HWL
344 We need at least @Eq@ for @CLabels@, because we want to avoid
345 duplicate declarations in generating C (see @labelSeenTE@ in
348 -----------------------------------------------------------------------------
349 Printing out CLabels.
355 where <name> is <Module>_<name> for external names and <unique> for
356 internal names. <type> is one of the following:
359 srt Static reference table
361 ret Direct return address
363 <n>_alt Case alternative (tag n)
364 dflt Default case alternative
365 btm Large bitmap vector
366 closure Static closure
367 static_closure Static closure (???)
368 con_entry Dynamic Constructor entry code
369 con_info Dynamic Constructor info table
370 static_entry Static Constructor entry code
371 static_info Static Constructor info table
372 sel_info Selector info table
373 sel_entry Selector entry code
375 ccs Cost centre stack
378 -- specialised for PprAsm: saves lots of arg passing in NCG
379 #if ! OMIT_NATIVE_CODEGEN
380 pprCLabel_asm = pprCLabel
383 pprCLabel :: CLabel -> SDoc
385 #if ! OMIT_NATIVE_CODEGEN
386 pprCLabel (AsmTempLabel u)
387 = text (fmtAsmLbl (show u))
391 #if ! OMIT_NATIVE_CODEGEN
392 getPprStyle $ \ sty ->
393 if asmStyle sty && underscorePrefix then
394 pp_cSEP <> pprCLbl lbl
399 pprCLbl (CaseLabel u CaseReturnPt)
400 = hcat [pprUnique u, pp_cSEP, ptext SLIT("ret")]
401 pprCLbl (CaseLabel u CaseReturnInfo)
402 = hcat [pprUnique u, pp_cSEP, ptext SLIT("info")]
403 pprCLbl (CaseLabel u CaseVecTbl)
404 = hcat [pprUnique u, pp_cSEP, ptext SLIT("vtbl")]
405 pprCLbl (CaseLabel u (CaseAlt tag))
406 = hcat [pprUnique u, pp_cSEP, int tag, pp_cSEP, ptext SLIT("alt")]
407 pprCLbl (CaseLabel u CaseDefault)
408 = hcat [pprUnique u, pp_cSEP, ptext SLIT("dflt")]
409 pprCLbl (CaseLabel u CaseBitmap)
410 = hcat [pprUnique u, pp_cSEP, ptext SLIT("btm")]
412 pprCLbl (RtsLabel RtsShouldNeverHappenCode) = ptext SLIT("stg_error_entry")
414 pprCLbl (RtsLabel RtsUpdInfo) = ptext SLIT("Upd_frame_info")
416 pprCLbl (RtsLabel RtsTopTickyCtr) = ptext SLIT("top_ct")
418 pprCLbl (RtsLabel (RtsBlackHoleInfoTbl info)) = ptext info
420 pprCLbl (RtsLabel (RtsSelectorInfoTbl upd_reqd offset))
421 = hcat [ptext SLIT("__sel_"), text (show offset),
423 then SLIT("_upd_info")
424 else SLIT("_noupd_info"))
427 pprCLbl (RtsLabel (RtsSelectorEntry upd_reqd offset))
428 = hcat [ptext SLIT("__sel_"), text (show offset),
430 then SLIT("_upd_entry")
431 else SLIT("_noupd_entry"))
434 pprCLbl (RtsLabel (RtsApInfoTbl upd_reqd arity))
435 = hcat [ptext SLIT("__ap_"), text (show arity),
437 then SLIT("_upd_info")
438 else SLIT("_noupd_info"))
441 pprCLbl (RtsLabel (RtsApEntry upd_reqd arity))
442 = hcat [ptext SLIT("__ap_"), text (show arity),
444 then SLIT("_upd_entry")
445 else SLIT("_noupd_entry"))
448 pprCLbl (RtsLabel (RtsPrimOp primop))
449 = pprPrimOp primop <> ptext SLIT("_fast")
451 pprCLbl (TyConLabel tc)
452 = hcat [ppr tc, pp_cSEP, ptext SLIT("closure_tbl")]
454 pprCLbl (IdLabel id flavor) = ppr id <> ppIdFlavor flavor
455 pprCLbl (DataConLabel con flavor) = ppr con <> ppConFlavor flavor
457 pprCLbl (CC_Label cc) = ppr cc
458 pprCLbl (CCS_Label ccs) = ppr ccs
460 ppIdFlavor :: IdLabelInfo -> SDoc
462 ppIdFlavor x = pp_cSEP <>
464 Closure -> ptext SLIT("closure")
465 SRT -> ptext SLIT("srt")
466 InfoTbl -> ptext SLIT("info")
467 EntryStd -> ptext SLIT("entry")
468 EntryFast arity -> --false:ASSERT (arity > 0)
469 (<>) (ptext SLIT("fast")) (int arity)
470 RednCounts -> ptext SLIT("ct")
473 ppConFlavor x = pp_cSEP <>
475 StaticClosure -> ptext SLIT("static_closure")
476 ConEntry -> ptext SLIT("con_entry")
477 ConInfoTbl -> ptext SLIT("con_info")
478 StaticConEntry -> ptext SLIT("static_entry")
479 StaticInfoTbl -> ptext SLIT("static_info")