2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 % $Id: CLabel.lhs,v 1.28 1999/10/13 16:39:10 simonmar Exp $
6 \section[CLabel]{@CLabel@: Information to make C Labels}
10 CLabel, -- abstract type
18 mkStaticConEntryLabel,
22 mkStaticInfoTableLabel,
40 mkCAFBlackHoleInfoTableLabel,
41 mkSECAFBlackHoleInfoTableLabel,
47 mkCC_Label, mkCCS_Label,
49 needsCDecl, isAsmTemp, externallyVisibleCLabel,
51 CLabelType(..), labelType, labelDynamic,
54 #if ! OMIT_NATIVE_CODEGEN
60 #include "HsVersions.h"
62 #if ! OMIT_NATIVE_CODEGEN
63 import {-# SOURCE #-} MachMisc ( underscorePrefix, fmtAsmLbl )
66 import CmdLineOpts ( opt_Static, opt_DoTickyProfiling )
67 import CStrings ( pp_cSEP )
68 import DataCon ( ConTag, DataCon )
69 import Module ( isDynamicModule )
70 import Name ( Name, getName, isExternallyVisibleName, nameModule, isLocallyDefinedName )
71 import TyCon ( TyCon )
72 import Unique ( pprUnique, Unique )
73 import PrimOp ( PrimOp, pprPrimOp )
74 import CostCentre ( CostCentre, CostCentreStack )
79 things we want to find out:
81 * should the labelled things be declared "static" (visible only in this file)?
83 * should it be declared "const" (read-only text space)?
85 * does it need declarations at all? (v common Prelude things are pre-declared)
87 * what type does it have? (for generating accurate enough C declarations
88 so that the C compiler won't complain).
92 = IdLabel -- A family of labels related to the
93 Name -- definition of a particular Id
96 | DataConLabel -- Ditto data constructors
100 | CaseLabel -- A family of labels related to a particular case expression
101 Unique -- Unique says which case expression
104 | TyConLabel TyCon -- currently only one kind of TyconLabel,
105 -- a 'Closure Table'.
107 | AsmTempLabel Unique
109 | RtsLabel RtsLabelInfo
111 | CC_Label CostCentre
112 | CCS_Label CostCentreStack
119 = Closure -- Label for (static???) closure
121 | SRT -- Static reference table
123 | InfoTbl -- Info table for a closure; always read-only
125 | EntryStd -- Thunk, or "slow", code entry point
127 | EntryFast Int -- entry pt when no arg satisfaction chk needed;
128 -- Int is the arity of the function (to be
129 -- encoded into the name)
131 -- Ticky-ticky counting
132 | RednCounts -- Label of place to keep reduction-count info for
136 data DataConLabelInfo
137 = ConEntry -- the only kind of entry pt for constructors
138 | ConInfoTbl -- corresponding info table
140 | StaticClosure -- Static constructor closure
141 -- e.g., nullary constructor
142 | StaticConEntry -- static constructor entry point
143 | StaticInfoTbl -- corresponding info table
156 = RtsShouldNeverHappenCode
158 | RtsBlackHoleInfoTbl FAST_STRING -- black hole with info table name
162 | RtsSelectorInfoTbl Bool{-updatable-} Int{-offset-} -- Selector thunks
163 | RtsSelectorEntry Bool{-updatable-} Int{-offset-}
165 | RtsApInfoTbl Bool{-updatable-} Int{-arity-} -- AP thunks
166 | RtsApEntry Bool{-updatable-} Int{-arity-}
174 -- Label Type: for generating C declarations.
186 mkClosureLabel id = IdLabel id Closure
187 mkSRTLabel id = IdLabel id SRT
188 mkInfoTableLabel id = IdLabel id InfoTbl
189 mkStdEntryLabel id = IdLabel id EntryStd
190 mkFastEntryLabel id arity = ASSERT(arity > 0)
191 IdLabel id (EntryFast arity)
193 mkRednCountsLabel id = IdLabel id RednCounts
195 mkStaticClosureLabel con = DataConLabel con StaticClosure
196 mkStaticInfoTableLabel con = DataConLabel con StaticInfoTbl
197 mkConInfoTableLabel con = DataConLabel con ConInfoTbl
198 mkConEntryLabel con = DataConLabel con ConEntry
199 mkStaticConEntryLabel con = DataConLabel con StaticConEntry
202 mkReturnPtLabel uniq = CaseLabel uniq CaseReturnPt
203 mkReturnInfoLabel uniq = CaseLabel uniq CaseReturnInfo
204 mkVecTblLabel uniq = CaseLabel uniq CaseVecTbl
205 mkAltLabel uniq tag = CaseLabel uniq (CaseAlt tag)
206 mkDefaultLabel uniq = CaseLabel uniq CaseDefault
207 mkBitmapLabel uniq = CaseLabel uniq CaseBitmap
209 mkClosureTblLabel tycon = TyConLabel tycon
211 mkAsmTempLabel = AsmTempLabel
213 -- Some fixed runtime system labels
215 mkErrorStdEntryLabel = RtsLabel RtsShouldNeverHappenCode
216 mkUpdInfoLabel = RtsLabel RtsUpdInfo
217 mkTopTickyCtrLabel = RtsLabel RtsTopTickyCtr
218 mkCAFBlackHoleInfoTableLabel = RtsLabel (RtsBlackHoleInfoTbl SLIT("CAF_BLACKHOLE_info"))
219 mkSECAFBlackHoleInfoTableLabel = if opt_DoTickyProfiling then
220 RtsLabel (RtsBlackHoleInfoTbl SLIT("SE_CAF_BLACKHOLE_info"))
221 else -- RTS won't have info table unless -ticky is on
222 panic "mkSECAFBlackHoleInfoTableLabel requires -ticky"
223 mkRtsPrimOpLabel primop = RtsLabel (RtsPrimOp primop)
225 mkSelectorInfoLabel upd off = RtsLabel (RtsSelectorInfoTbl upd off)
226 mkSelectorEntryLabel upd off = RtsLabel (RtsSelectorEntry upd off)
228 mkApInfoTableLabel upd off = RtsLabel (RtsApInfoTbl upd off)
229 mkApEntryLabel upd off = RtsLabel (RtsApEntry upd off)
233 mkCC_Label cc = CC_Label cc
234 mkCCS_Label ccs = CCS_Label ccs
238 needsCDecl :: CLabel -> Bool -- False <=> it's pre-declared; don't bother
239 isAsmTemp :: CLabel -> Bool -- is a local temporary for native code generation
240 externallyVisibleCLabel :: CLabel -> Bool -- not C "static"
243 @needsCDecl@ is @True@ unless the thing is a deeply-@PreludeCore@-ish
244 object. {\em Also:} No need to spit out labels for things generated
245 by the flattener (in @AbsCUtils@)---it is careful to ensure references
246 to them are always backwards. These are return-point and vector-table
249 Declarations for (non-prelude) @Id@-based things are needed because of
252 Declarations for direct return points are needed, because they may be
253 let-no-escapes, which can be recursive.
256 needsCDecl (IdLabel _ _) = True
257 needsCDecl (CaseLabel _ CaseReturnPt) = True
258 needsCDecl (DataConLabel _ _) = True
259 needsCDecl (CaseLabel _ _) = False
260 needsCDecl (TyConLabel _) = True
262 needsCDecl (AsmTempLabel _) = False
263 needsCDecl (RtsLabel _) = False
264 needsCDecl (CC_Label _) = False
265 needsCDecl (CCS_Label _) = False
268 Whether the label is an assembler temporary:
271 isAsmTemp (AsmTempLabel _) = True
275 C ``static'' or not...
276 From the point of view of the code generator, a name is
277 externally visible if it has to be declared as exported
278 in the .o file's symbol table; that is, made non-static.
281 externallyVisibleCLabel (DataConLabel _ _) = True
282 externallyVisibleCLabel (TyConLabel tc) = True
283 externallyVisibleCLabel (CaseLabel _ _) = False
284 externallyVisibleCLabel (AsmTempLabel _) = False
285 externallyVisibleCLabel (RtsLabel _) = True
286 externallyVisibleCLabel (IdLabel id _) = isExternallyVisibleName id
287 externallyVisibleCLabel (CC_Label _) = False -- not strictly true
288 externallyVisibleCLabel (CCS_Label _) = False -- not strictly true
291 For generating correct types in label declarations...
294 labelType :: CLabel -> CLabelType
295 labelType (RtsLabel (RtsBlackHoleInfoTbl _)) = InfoTblType
296 labelType (RtsLabel (RtsSelectorInfoTbl _ _)) = InfoTblType
297 labelType (RtsLabel (RtsApInfoTbl _ _)) = InfoTblType
298 labelType (RtsLabel RtsUpdInfo) = InfoTblType
299 labelType (CaseLabel _ CaseReturnInfo) = InfoTblType
300 labelType (CaseLabel _ CaseReturnPt) = CodeType
301 labelType (CaseLabel _ CaseVecTbl) = VecTblType
302 labelType (TyConLabel _) = ClosureTblType
304 labelType (IdLabel _ info) =
306 InfoTbl -> InfoTblType
307 Closure -> ClosureType
310 labelType (DataConLabel _ info) =
312 ConInfoTbl -> InfoTblType
313 StaticInfoTbl -> InfoTblType
314 StaticClosure -> ClosureType
317 labelType _ = DataType
320 When referring to data in code, we need to know whether
321 that data resides in a DLL or not. [Win32 only.]
322 @labelDynamic@ returns @True@ if the label is located
323 in a DLL, be it a data reference or not.
326 labelDynamic :: CLabel -> Bool
329 RtsLabel _ -> not opt_Static -- i.e., is the RTS in a DLL or not?
330 IdLabel n k | not (isLocallyDefinedName n) -> isDynamicModule (nameModule n)
331 DataConLabel n k | not (isLocallyDefinedName n) -> isDynamicModule (nameModule n)
332 TyConLabel tc | not (isLocallyDefinedName (getName tc)) -> isDynamicModule (nameModule (getName tc))
338 OLD?: These GRAN functions are needed for spitting out GRAN_FETCH() at the
339 right places. It is used to detect when the abstractC statement of an
340 CCodeBlock actually contains the code for a slow entry point. -- HWL
342 We need at least @Eq@ for @CLabels@, because we want to avoid
343 duplicate declarations in generating C (see @labelSeenTE@ in
346 -----------------------------------------------------------------------------
347 Printing out CLabels.
353 where <name> is <Module>_<name> for external names and <unique> for
354 internal names. <type> is one of the following:
357 srt Static reference table
359 ret Direct return address
361 <n>_alt Case alternative (tag n)
362 dflt Default case alternative
363 btm Large bitmap vector
364 closure Static closure
365 static_closure Static closure (???)
366 con_entry Dynamic Constructor entry code
367 con_info Dynamic Constructor info table
368 static_entry Static Constructor entry code
369 static_info Static Constructor info table
370 sel_info Selector info table
371 sel_entry Selector entry code
374 -- specialised for PprAsm: saves lots of arg passing in NCG
375 #if ! OMIT_NATIVE_CODEGEN
376 pprCLabel_asm = pprCLabel
379 pprCLabel :: CLabel -> SDoc
381 #if ! OMIT_NATIVE_CODEGEN
382 pprCLabel (AsmTempLabel u)
383 = text (fmtAsmLbl (show u))
387 #if ! OMIT_NATIVE_CODEGEN
388 getPprStyle $ \ sty ->
389 if asmStyle sty && underscorePrefix then
390 pp_cSEP <> pprCLbl lbl
395 pprCLbl (CaseLabel u CaseReturnPt)
396 = hcat [pprUnique u, pp_cSEP, ptext SLIT("ret")]
397 pprCLbl (CaseLabel u CaseReturnInfo)
398 = hcat [pprUnique u, pp_cSEP, ptext SLIT("info")]
399 pprCLbl (CaseLabel u CaseVecTbl)
400 = hcat [pprUnique u, pp_cSEP, ptext SLIT("vtbl")]
401 pprCLbl (CaseLabel u (CaseAlt tag))
402 = hcat [pprUnique u, pp_cSEP, int tag, pp_cSEP, ptext SLIT("alt")]
403 pprCLbl (CaseLabel u CaseDefault)
404 = hcat [pprUnique u, pp_cSEP, ptext SLIT("dflt")]
405 pprCLbl (CaseLabel u CaseBitmap)
406 = hcat [pprUnique u, pp_cSEP, ptext SLIT("btm")]
408 pprCLbl (RtsLabel RtsShouldNeverHappenCode) = ptext SLIT("stg_error_entry")
410 pprCLbl (RtsLabel RtsUpdInfo) = ptext SLIT("Upd_frame_info")
412 pprCLbl (RtsLabel RtsTopTickyCtr) = ptext SLIT("top_ct")
414 pprCLbl (RtsLabel (RtsBlackHoleInfoTbl info)) = ptext info
416 pprCLbl (RtsLabel (RtsSelectorInfoTbl upd_reqd offset))
417 = hcat [ptext SLIT("__sel_"), text (show offset),
419 then SLIT("_upd_info")
420 else SLIT("_noupd_info"))
423 pprCLbl (RtsLabel (RtsSelectorEntry upd_reqd offset))
424 = hcat [ptext SLIT("__sel_"), text (show offset),
426 then SLIT("_upd_entry")
427 else SLIT("_noupd_entry"))
430 pprCLbl (RtsLabel (RtsApInfoTbl upd_reqd arity))
431 = hcat [ptext SLIT("__ap_"), text (show arity),
433 then SLIT("_upd_info")
434 else SLIT("_noupd_info"))
437 pprCLbl (RtsLabel (RtsApEntry upd_reqd arity))
438 = hcat [ptext SLIT("__ap_"), text (show arity),
440 then SLIT("_upd_entry")
441 else SLIT("_noupd_entry"))
444 pprCLbl (RtsLabel (RtsPrimOp primop))
445 = pprPrimOp primop <> ptext SLIT("_fast")
447 pprCLbl (TyConLabel tc)
448 = hcat [ppr tc, pp_cSEP, ptext SLIT("closure_tbl")]
450 pprCLbl (IdLabel id flavor) = ppr id <> ppIdFlavor flavor
451 pprCLbl (DataConLabel con flavor) = ppr con <> ppConFlavor flavor
453 pprCLbl (CC_Label cc) = ppr cc
454 pprCLbl (CCS_Label ccs) = ppr ccs
456 ppIdFlavor :: IdLabelInfo -> SDoc
458 ppIdFlavor x = pp_cSEP <>
460 Closure -> ptext SLIT("closure")
461 SRT -> ptext SLIT("srt")
462 InfoTbl -> ptext SLIT("info")
463 EntryStd -> ptext SLIT("entry")
464 EntryFast arity -> --false:ASSERT (arity > 0)
465 (<>) (ptext SLIT("fast")) (int arity)
466 RednCounts -> ptext SLIT("ct")
469 ppConFlavor x = pp_cSEP <>
471 StaticClosure -> ptext SLIT("static_closure")
472 ConEntry -> ptext SLIT("con_entry")
473 ConInfoTbl -> ptext SLIT("con_info")
474 StaticConEntry -> ptext SLIT("static_entry")
475 StaticInfoTbl -> ptext SLIT("static_info")