2 -- The above warning supression flag is a temporary kludge.
3 -- While working on this module you are encouraged to remove it and fix
4 -- any warnings in the module. See
5 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
8 -----------------------------------------------------------------------------
10 -- Object-file symbols (called CLabel for histerical raisins).
12 -- (c) The University of Glasgow 2004-2006
14 -----------------------------------------------------------------------------
17 CLabel, -- abstract type
25 mkStaticConEntryLabel,
28 mkStaticInfoTableLabel,
35 mkLocalInfoTableLabel,
38 mkLocalStaticConEntryLabel,
39 mkLocalConInfoTableLabel,
40 mkLocalStaticInfoTableLabel,
41 mkLocalClosureTableLabel,
53 mkPlainModuleInitLabel,
54 mkModuleInitTableLabel,
57 mkDirty_MUT_VAR_Label,
60 mkMainCapabilityLabel,
61 mkMAP_FROZEN_infoLabel,
62 mkMAP_DIRTY_infoLabel,
63 mkEMPTY_MVAR_infoLabel,
66 mkCAFBlackHoleInfoTableLabel,
68 mkRtsSlowTickyCtrLabel,
90 foreignLabelStdcallInfo,
92 mkCCLabel, mkCCSLabel,
94 DynamicLinkerLabelInfo(..),
96 dynamicLinkerLabelInfo,
102 mkHpcModuleNameLabel,
105 infoLblToEntryLbl, entryLblToInfoLbl, cvtToClosureLbl, cvtToSRTLbl,
106 needsCDecl, isAsmTemp, maybeAsmTemp, externallyVisibleCLabel,
108 isCFunctionLabel, isGcPtrLabel, labelDynamic,
113 #include "HsVersions.h"
133 -- -----------------------------------------------------------------------------
137 CLabel is an abstract type that supports the following operations:
141 - In a C file, does it need to be declared before use? (i.e. is it
142 guaranteed to be already in scope in the places we need to refer to it?)
144 - If it needs to be declared, what type (code or data) should it be
147 - Is it visible outside this object file or not?
149 - Is it "dynamic" (see details below)
151 - Eq and Ord, so that we can make sets of CLabels (currently only
152 used in outputting C as far as I can tell, to avoid generating
153 more than one declaration for any given label).
155 - Converting an info table label into an entry label.
159 = IdLabel -- A family of labels related to the
160 Name -- definition of a particular Id or Con
164 | CaseLabel -- A family of labels related to a particular
166 {-# UNPACK #-} !Unique -- Unique says which case expression
170 {-# UNPACK #-} !Unique
173 {-# UNPACK #-} !Unique
176 Module -- the module name
178 -- at some point we might want some kind of version number in
179 -- the module init label, to guard against compiling modules in
180 -- the wrong order. We can't use the interface file version however,
181 -- because we don't always recompile modules which depend on a module
182 -- whose version has changed.
184 | PlainModuleInitLabel -- without the version & way info
187 | ModuleInitTableLabel -- table of imported modules to init
192 | RtsLabel RtsLabelInfo
194 | ForeignLabel FastString -- a 'C' (or otherwise foreign) label
195 (Maybe Int) -- possible '@n' suffix for stdcall functions
196 -- When generating C, the '@n' suffix is omitted, but when
197 -- generating assembler we must add it to the label.
198 Bool -- True <=> is dynamic
201 | CC_Label CostCentre
202 | CCS_Label CostCentreStack
204 -- Dynamic Linking in the NCG:
205 -- generated and used inside the NCG only,
206 -- see module PositionIndependentCode for details.
208 | DynamicLinkerLabel DynamicLinkerLabelInfo CLabel
209 -- special variants of a label used for dynamic linking
211 | PicBaseLabel -- a label used as a base for PIC calculations
212 -- on some platforms.
213 -- It takes the form of a local numeric
214 -- assembler label '1'; it is pretty-printed
215 -- as 1b, referring to the previous definition
216 -- of 1: in the assembler source file.
218 | DeadStripPreventer CLabel
219 -- label before an info table to prevent excessive dead-stripping on darwin
221 | HpcTicksLabel Module -- Per-module table of tick locations
222 | HpcModuleNameLabel -- Per-module name of the module for Hpc
224 | LargeSRTLabel -- Label of an StgLargeSRT
225 {-# UNPACK #-} !Unique
227 | LargeBitmapLabel -- A bitmap (function or case return)
228 {-# UNPACK #-} !Unique
233 = Closure -- Label for closure
234 | SRT -- Static reference table
235 | InfoTable -- Info tables for closures; always read-only
236 | Entry -- entry point
237 | Slow -- slow entry point
239 | RednCounts -- Label of place to keep Ticky-ticky info for
242 | ConEntry -- constructor entry point
243 | ConInfoTable -- corresponding info table
244 | StaticConEntry -- static constructor entry point
245 | StaticInfoTable -- corresponding info table
247 | ClosureTable -- table of closures for Enum tycons
261 = RtsSelectorInfoTable Bool{-updatable-} Int{-offset-} -- Selector thunks
262 | RtsSelectorEntry Bool{-updatable-} Int{-offset-}
264 | RtsApInfoTable Bool{-updatable-} Int{-arity-} -- AP thunks
265 | RtsApEntry Bool{-updatable-} Int{-arity-}
269 | RtsInfo FastString -- misc rts info tables
270 | RtsEntry FastString -- misc rts entry points
271 | RtsRetInfo FastString -- misc rts ret info tables
272 | RtsRet FastString -- misc rts return points
273 | RtsData FastString -- misc rts data bits, eg CHARLIKE_closure
274 | RtsCode FastString -- misc rts code
275 | RtsGcPtr FastString -- GcPtrs eg CHARLIKE_closure
277 | RtsApFast FastString -- _fast versions of generic apply
279 | RtsSlowTickyCtr String
282 -- NOTE: Eq on LitString compares the pointer only, so this isn't
285 data DynamicLinkerLabelInfo
286 = CodeStub -- MachO: Lfoo$stub, ELF: foo@plt
287 | SymbolPtr -- MachO: Lfoo$non_lazy_ptr, Windows: __imp_foo
288 | GotSymbolPtr -- ELF: foo@got
289 | GotSymbolOffset -- ELF: foo@gotoff
293 -- -----------------------------------------------------------------------------
294 -- Constructing CLabels
296 -- These are always local:
297 mkSRTLabel name c = IdLabel name c SRT
298 mkSlowEntryLabel name c = IdLabel name c Slow
299 mkRednCountsLabel name c = IdLabel name c RednCounts
301 -- These have local & (possibly) external variants:
302 mkLocalClosureLabel name c = IdLabel name c Closure
303 mkLocalInfoTableLabel name c = IdLabel name c InfoTable
304 mkLocalEntryLabel name c = IdLabel name c Entry
305 mkLocalClosureTableLabel name c = IdLabel name c ClosureTable
307 mkClosureLabel name c = IdLabel name c Closure
308 mkInfoTableLabel name c = IdLabel name c InfoTable
309 mkEntryLabel name c = IdLabel name c Entry
310 mkClosureTableLabel name c = IdLabel name c ClosureTable
311 mkLocalConInfoTableLabel c con = IdLabel con c ConInfoTable
312 mkLocalConEntryLabel c con = IdLabel con c ConEntry
313 mkLocalStaticInfoTableLabel c con = IdLabel con c StaticInfoTable
314 mkLocalStaticConEntryLabel c con = IdLabel con c StaticConEntry
315 mkConInfoTableLabel name c = IdLabel name c ConInfoTable
316 mkStaticInfoTableLabel name c = IdLabel name c StaticInfoTable
318 mkConEntryLabel name c = IdLabel name c ConEntry
319 mkStaticConEntryLabel name c = IdLabel name c StaticConEntry
321 mkLargeSRTLabel uniq = LargeSRTLabel uniq
322 mkBitmapLabel uniq = LargeBitmapLabel uniq
324 mkReturnPtLabel uniq = CaseLabel uniq CaseReturnPt
325 mkReturnInfoLabel uniq = CaseLabel uniq CaseReturnInfo
326 mkAltLabel uniq tag = CaseLabel uniq (CaseAlt tag)
327 mkDefaultLabel uniq = CaseLabel uniq CaseDefault
329 mkStringLitLabel = StringLitLabel
330 mkAsmTempLabel :: Uniquable a => a -> CLabel
331 mkAsmTempLabel a = AsmTempLabel (getUnique a)
333 mkModuleInitLabel :: Module -> String -> CLabel
334 mkModuleInitLabel mod way = ModuleInitLabel mod way
336 mkPlainModuleInitLabel :: Module -> CLabel
337 mkPlainModuleInitLabel mod = PlainModuleInitLabel mod
339 mkModuleInitTableLabel :: Module -> CLabel
340 mkModuleInitTableLabel mod = ModuleInitTableLabel mod
342 -- Some fixed runtime system labels
344 mkSplitMarkerLabel = RtsLabel (RtsCode (fsLit "__stg_split_marker"))
345 mkDirty_MUT_VAR_Label = RtsLabel (RtsCode (fsLit "dirty_MUT_VAR"))
346 mkUpdInfoLabel = RtsLabel (RtsInfo (fsLit "stg_upd_frame"))
347 mkIndStaticInfoLabel = RtsLabel (RtsInfo (fsLit "stg_IND_STATIC"))
348 mkMainCapabilityLabel = RtsLabel (RtsData (fsLit "MainCapability"))
349 mkMAP_FROZEN_infoLabel = RtsLabel (RtsInfo (fsLit "stg_MUT_ARR_PTRS_FROZEN0"))
350 mkMAP_DIRTY_infoLabel = RtsLabel (RtsInfo (fsLit "stg_MUT_ARR_PTRS_DIRTY"))
351 mkEMPTY_MVAR_infoLabel = RtsLabel (RtsInfo (fsLit "stg_EMPTY_MVAR"))
353 mkTopTickyCtrLabel = RtsLabel (RtsData (fsLit "top_ct"))
354 mkCAFBlackHoleInfoTableLabel = RtsLabel (RtsInfo (fsLit "stg_CAF_BLACKHOLE"))
355 mkRtsPrimOpLabel primop = RtsLabel (RtsPrimOp primop)
357 moduleRegdLabel = ModuleRegdLabel
358 moduleRegTableLabel = ModuleInitTableLabel
360 mkSelectorInfoLabel upd off = RtsLabel (RtsSelectorInfoTable upd off)
361 mkSelectorEntryLabel upd off = RtsLabel (RtsSelectorEntry upd off)
363 mkApInfoTableLabel upd off = RtsLabel (RtsApInfoTable upd off)
364 mkApEntryLabel upd off = RtsLabel (RtsApEntry upd off)
366 -- Primitive / cmm call labels
368 mkPrimCallLabel :: PrimCall -> CLabel
369 mkPrimCallLabel (PrimCall str) = ForeignLabel str Nothing False IsFunction
373 mkForeignLabel :: FastString -> Maybe Int -> Bool -> FunctionOrData -> CLabel
374 mkForeignLabel str mb_sz is_dynamic fod
375 = ForeignLabel str mb_sz is_dynamic fod
377 addLabelSize :: CLabel -> Int -> CLabel
378 addLabelSize (ForeignLabel str _ is_dynamic fod) sz
379 = ForeignLabel str (Just sz) is_dynamic fod
383 foreignLabelStdcallInfo :: CLabel -> Maybe Int
384 foreignLabelStdcallInfo (ForeignLabel _ info _ _) = info
385 foreignLabelStdcallInfo _lbl = Nothing
389 mkCCLabel cc = CC_Label cc
390 mkCCSLabel ccs = CCS_Label ccs
392 mkRtsInfoLabel str = RtsLabel (RtsInfo str)
393 mkRtsEntryLabel str = RtsLabel (RtsEntry str)
394 mkRtsRetInfoLabel str = RtsLabel (RtsRetInfo str)
395 mkRtsRetLabel str = RtsLabel (RtsRet str)
396 mkRtsCodeLabel str = RtsLabel (RtsCode str)
397 mkRtsDataLabel str = RtsLabel (RtsData str)
398 mkRtsGcPtrLabel str = RtsLabel (RtsGcPtr str)
400 mkRtsApFastLabel str = RtsLabel (RtsApFast str)
402 mkRtsSlowTickyCtrLabel :: String -> CLabel
403 mkRtsSlowTickyCtrLabel pat = RtsLabel (RtsSlowTickyCtr pat)
407 mkHpcTicksLabel = HpcTicksLabel
408 mkHpcModuleNameLabel = HpcModuleNameLabel
412 mkDynamicLinkerLabel :: DynamicLinkerLabelInfo -> CLabel -> CLabel
413 mkDynamicLinkerLabel = DynamicLinkerLabel
415 dynamicLinkerLabelInfo :: CLabel -> Maybe (DynamicLinkerLabelInfo, CLabel)
416 dynamicLinkerLabelInfo (DynamicLinkerLabel info lbl) = Just (info, lbl)
417 dynamicLinkerLabelInfo _ = Nothing
419 -- Position independent code
421 mkPicBaseLabel :: CLabel
422 mkPicBaseLabel = PicBaseLabel
424 mkDeadStripPreventer :: CLabel -> CLabel
425 mkDeadStripPreventer lbl = DeadStripPreventer lbl
427 -- -----------------------------------------------------------------------------
428 -- Converting between info labels and entry/ret labels.
430 infoLblToEntryLbl :: CLabel -> CLabel
431 infoLblToEntryLbl (IdLabel n c InfoTable) = IdLabel n c Entry
432 infoLblToEntryLbl (IdLabel n c ConInfoTable) = IdLabel n c ConEntry
433 infoLblToEntryLbl (IdLabel n c StaticInfoTable) = IdLabel n c StaticConEntry
434 infoLblToEntryLbl (CaseLabel n CaseReturnInfo) = CaseLabel n CaseReturnPt
435 infoLblToEntryLbl (RtsLabel (RtsInfo s)) = RtsLabel (RtsEntry s)
436 infoLblToEntryLbl (RtsLabel (RtsRetInfo s)) = RtsLabel (RtsRet s)
437 infoLblToEntryLbl _ = panic "CLabel.infoLblToEntryLbl"
439 entryLblToInfoLbl :: CLabel -> CLabel
440 entryLblToInfoLbl (IdLabel n c Entry) = IdLabel n c InfoTable
441 entryLblToInfoLbl (IdLabel n c ConEntry) = IdLabel n c ConInfoTable
442 entryLblToInfoLbl (IdLabel n c StaticConEntry) = IdLabel n c StaticInfoTable
443 entryLblToInfoLbl (CaseLabel n CaseReturnPt) = CaseLabel n CaseReturnInfo
444 entryLblToInfoLbl (RtsLabel (RtsEntry s)) = RtsLabel (RtsInfo s)
445 entryLblToInfoLbl (RtsLabel (RtsRet s)) = RtsLabel (RtsRetInfo s)
446 entryLblToInfoLbl l = pprPanic "CLabel.entryLblToInfoLbl" (pprCLabel l)
448 cvtToClosureLbl (IdLabel n c InfoTable) = IdLabel n c Closure
449 cvtToClosureLbl (IdLabel n c Entry) = IdLabel n c Closure
450 cvtToClosureLbl (IdLabel n c ConEntry) = IdLabel n c Closure
451 cvtToClosureLbl l@(IdLabel n c Closure) = l
452 cvtToClosureLbl l = pprPanic "cvtToClosureLbl" (pprCLabel l)
454 cvtToSRTLbl (IdLabel n c InfoTable) = mkSRTLabel n c
455 cvtToSRTLbl (IdLabel n c Entry) = mkSRTLabel n c
456 cvtToSRTLbl (IdLabel n c ConEntry) = mkSRTLabel n c
457 cvtToSRTLbl l@(IdLabel n c Closure) = mkSRTLabel n c
458 cvtToSRTLbl l = pprPanic "cvtToSRTLbl" (pprCLabel l)
460 -- -----------------------------------------------------------------------------
461 -- Does a CLabel refer to a CAF?
462 hasCAF :: CLabel -> Bool
463 hasCAF (IdLabel _ MayHaveCafRefs _) = True
466 -- -----------------------------------------------------------------------------
467 -- Does a CLabel need declaring before use or not?
469 -- See wiki:Commentary/Compiler/Backends/PprC#Prototypes
471 needsCDecl :: CLabel -> Bool
472 -- False <=> it's pre-declared; don't bother
473 -- don't bother declaring SRT & Bitmap labels, we always make sure
474 -- they are defined before use.
475 needsCDecl (IdLabel _ _ SRT) = False
476 needsCDecl (LargeSRTLabel _) = False
477 needsCDecl (LargeBitmapLabel _) = False
478 needsCDecl (IdLabel _ _ _) = True
479 needsCDecl (CaseLabel _ _) = True
480 needsCDecl (ModuleInitLabel _ _) = True
481 needsCDecl (PlainModuleInitLabel _) = True
482 needsCDecl (ModuleInitTableLabel _) = True
483 needsCDecl ModuleRegdLabel = False
485 needsCDecl (StringLitLabel _) = False
486 needsCDecl (AsmTempLabel _) = False
487 needsCDecl (RtsLabel _) = False
488 needsCDecl l@(ForeignLabel _ _ _ _) = not (isMathFun l)
489 needsCDecl (CC_Label _) = True
490 needsCDecl (CCS_Label _) = True
491 needsCDecl (HpcTicksLabel _) = True
492 needsCDecl HpcModuleNameLabel = False
494 -- Whether the label is an assembler temporary:
496 isAsmTemp :: CLabel -> Bool -- is a local temporary for native code generation
497 isAsmTemp (AsmTempLabel _) = True
500 maybeAsmTemp :: CLabel -> Maybe Unique
501 maybeAsmTemp (AsmTempLabel uq) = Just uq
502 maybeAsmTemp _ = Nothing
504 -- some labels have C prototypes in scope when compiling via C, because
505 -- they are builtin to the C compiler. For these labels we avoid
506 -- generating our own C prototypes.
507 isMathFun :: CLabel -> Bool
508 isMathFun (ForeignLabel fs _ _ _) = fs `elementOfUniqSet` math_funs
511 math_funs = mkUniqSet [
513 (fsLit "acos"), (fsLit "acosf"), (fsLit "acosh"),
514 (fsLit "acoshf"), (fsLit "acoshl"), (fsLit "acosl"),
515 (fsLit "asin"), (fsLit "asinf"), (fsLit "asinl"),
516 (fsLit "asinh"), (fsLit "asinhf"), (fsLit "asinhl"),
517 (fsLit "atan"), (fsLit "atanf"), (fsLit "atanl"),
518 (fsLit "atan2"), (fsLit "atan2f"), (fsLit "atan2l"),
519 (fsLit "atanh"), (fsLit "atanhf"), (fsLit "atanhl"),
520 (fsLit "cbrt"), (fsLit "cbrtf"), (fsLit "cbrtl"),
521 (fsLit "ceil"), (fsLit "ceilf"), (fsLit "ceill"),
522 (fsLit "copysign"), (fsLit "copysignf"), (fsLit "copysignl"),
523 (fsLit "cos"), (fsLit "cosf"), (fsLit "cosl"),
524 (fsLit "cosh"), (fsLit "coshf"), (fsLit "coshl"),
525 (fsLit "erf"), (fsLit "erff"), (fsLit "erfl"),
526 (fsLit "erfc"), (fsLit "erfcf"), (fsLit "erfcl"),
527 (fsLit "exp"), (fsLit "expf"), (fsLit "expl"),
528 (fsLit "exp2"), (fsLit "exp2f"), (fsLit "exp2l"),
529 (fsLit "expm1"), (fsLit "expm1f"), (fsLit "expm1l"),
530 (fsLit "fabs"), (fsLit "fabsf"), (fsLit "fabsl"),
531 (fsLit "fdim"), (fsLit "fdimf"), (fsLit "fdiml"),
532 (fsLit "floor"), (fsLit "floorf"), (fsLit "floorl"),
533 (fsLit "fma"), (fsLit "fmaf"), (fsLit "fmal"),
534 (fsLit "fmax"), (fsLit "fmaxf"), (fsLit "fmaxl"),
535 (fsLit "fmin"), (fsLit "fminf"), (fsLit "fminl"),
536 (fsLit "fmod"), (fsLit "fmodf"), (fsLit "fmodl"),
537 (fsLit "frexp"), (fsLit "frexpf"), (fsLit "frexpl"),
538 (fsLit "hypot"), (fsLit "hypotf"), (fsLit "hypotl"),
539 (fsLit "ilogb"), (fsLit "ilogbf"), (fsLit "ilogbl"),
540 (fsLit "ldexp"), (fsLit "ldexpf"), (fsLit "ldexpl"),
541 (fsLit "lgamma"), (fsLit "lgammaf"), (fsLit "lgammal"),
542 (fsLit "llrint"), (fsLit "llrintf"), (fsLit "llrintl"),
543 (fsLit "llround"), (fsLit "llroundf"), (fsLit "llroundl"),
544 (fsLit "log"), (fsLit "logf"), (fsLit "logl"),
545 (fsLit "log10l"), (fsLit "log10"), (fsLit "log10f"),
546 (fsLit "log1pl"), (fsLit "log1p"), (fsLit "log1pf"),
547 (fsLit "log2"), (fsLit "log2f"), (fsLit "log2l"),
548 (fsLit "logb"), (fsLit "logbf"), (fsLit "logbl"),
549 (fsLit "lrint"), (fsLit "lrintf"), (fsLit "lrintl"),
550 (fsLit "lround"), (fsLit "lroundf"), (fsLit "lroundl"),
551 (fsLit "modf"), (fsLit "modff"), (fsLit "modfl"),
552 (fsLit "nan"), (fsLit "nanf"), (fsLit "nanl"),
553 (fsLit "nearbyint"), (fsLit "nearbyintf"), (fsLit "nearbyintl"),
554 (fsLit "nextafter"), (fsLit "nextafterf"), (fsLit "nextafterl"),
555 (fsLit "nexttoward"), (fsLit "nexttowardf"), (fsLit "nexttowardl"),
556 (fsLit "pow"), (fsLit "powf"), (fsLit "powl"),
557 (fsLit "remainder"), (fsLit "remainderf"), (fsLit "remainderl"),
558 (fsLit "remquo"), (fsLit "remquof"), (fsLit "remquol"),
559 (fsLit "rint"), (fsLit "rintf"), (fsLit "rintl"),
560 (fsLit "round"), (fsLit "roundf"), (fsLit "roundl"),
561 (fsLit "scalbln"), (fsLit "scalblnf"), (fsLit "scalblnl"),
562 (fsLit "scalbn"), (fsLit "scalbnf"), (fsLit "scalbnl"),
563 (fsLit "sin"), (fsLit "sinf"), (fsLit "sinl"),
564 (fsLit "sinh"), (fsLit "sinhf"), (fsLit "sinhl"),
565 (fsLit "sqrt"), (fsLit "sqrtf"), (fsLit "sqrtl"),
566 (fsLit "tan"), (fsLit "tanf"), (fsLit "tanl"),
567 (fsLit "tanh"), (fsLit "tanhf"), (fsLit "tanhl"),
568 (fsLit "tgamma"), (fsLit "tgammaf"), (fsLit "tgammal"),
569 (fsLit "trunc"), (fsLit "truncf"), (fsLit "truncl"),
570 -- ISO C 99 also defines these function-like macros in math.h:
571 -- fpclassify, isfinite, isinf, isnormal, signbit, isgreater,
572 -- isgreaterequal, isless, islessequal, islessgreater, isunordered
574 -- additional symbols from _BSD_SOURCE
575 (fsLit "drem"), (fsLit "dremf"), (fsLit "dreml"),
576 (fsLit "finite"), (fsLit "finitef"), (fsLit "finitel"),
577 (fsLit "gamma"), (fsLit "gammaf"), (fsLit "gammal"),
578 (fsLit "isinf"), (fsLit "isinff"), (fsLit "isinfl"),
579 (fsLit "isnan"), (fsLit "isnanf"), (fsLit "isnanl"),
580 (fsLit "j0"), (fsLit "j0f"), (fsLit "j0l"),
581 (fsLit "j1"), (fsLit "j1f"), (fsLit "j1l"),
582 (fsLit "jn"), (fsLit "jnf"), (fsLit "jnl"),
583 (fsLit "lgamma_r"), (fsLit "lgammaf_r"), (fsLit "lgammal_r"),
584 (fsLit "scalb"), (fsLit "scalbf"), (fsLit "scalbl"),
585 (fsLit "significand"), (fsLit "significandf"), (fsLit "significandl"),
586 (fsLit "y0"), (fsLit "y0f"), (fsLit "y0l"),
587 (fsLit "y1"), (fsLit "y1f"), (fsLit "y1l"),
588 (fsLit "yn"), (fsLit "ynf"), (fsLit "ynl")
591 -- -----------------------------------------------------------------------------
592 -- Is a CLabel visible outside this object file or not?
594 -- From the point of view of the code generator, a name is
595 -- externally visible if it has to be declared as exported
596 -- in the .o file's symbol table; that is, made non-static.
598 externallyVisibleCLabel :: CLabel -> Bool -- not C "static"
599 externallyVisibleCLabel (CaseLabel _ _) = False
600 externallyVisibleCLabel (StringLitLabel _) = False
601 externallyVisibleCLabel (AsmTempLabel _) = False
602 externallyVisibleCLabel (ModuleInitLabel _ _) = True
603 externallyVisibleCLabel (PlainModuleInitLabel _)= True
604 externallyVisibleCLabel (ModuleInitTableLabel _)= False
605 externallyVisibleCLabel ModuleRegdLabel = False
606 externallyVisibleCLabel (RtsLabel _) = True
607 externallyVisibleCLabel (ForeignLabel _ _ _ _) = True
608 externallyVisibleCLabel (IdLabel name _ _) = isExternalName name
609 externallyVisibleCLabel (CC_Label _) = True
610 externallyVisibleCLabel (CCS_Label _) = True
611 externallyVisibleCLabel (DynamicLinkerLabel _ _) = False
612 externallyVisibleCLabel (HpcTicksLabel _) = True
613 externallyVisibleCLabel HpcModuleNameLabel = False
614 externallyVisibleCLabel (LargeBitmapLabel _) = False
615 externallyVisibleCLabel (LargeSRTLabel _) = False
617 -- -----------------------------------------------------------------------------
618 -- Finding the "type" of a CLabel
620 -- For generating correct types in label declarations:
623 = CodeLabel -- Address of some executable instructions
624 | DataLabel -- Address of data, not a GC ptr
625 | GcPtrLabel -- Address of a (presumably static) GC object
627 isCFunctionLabel :: CLabel -> Bool
628 isCFunctionLabel lbl = case labelType lbl of
632 isGcPtrLabel :: CLabel -> Bool
633 isGcPtrLabel lbl = case labelType lbl of
637 labelType :: CLabel -> CLabelType
638 labelType (RtsLabel (RtsSelectorInfoTable _ _)) = DataLabel
639 labelType (RtsLabel (RtsApInfoTable _ _)) = DataLabel
640 labelType (RtsLabel (RtsData _)) = DataLabel
641 labelType (RtsLabel (RtsGcPtr _)) = GcPtrLabel
642 labelType (RtsLabel (RtsCode _)) = CodeLabel
643 labelType (RtsLabel (RtsInfo _)) = DataLabel
644 labelType (RtsLabel (RtsEntry _)) = CodeLabel
645 labelType (RtsLabel (RtsRetInfo _)) = DataLabel
646 labelType (RtsLabel (RtsRet _)) = CodeLabel
647 labelType (RtsLabel (RtsApFast _)) = CodeLabel
648 labelType (CaseLabel _ CaseReturnInfo) = DataLabel
649 labelType (CaseLabel _ _) = CodeLabel
650 labelType (ModuleInitLabel _ _) = CodeLabel
651 labelType (PlainModuleInitLabel _) = CodeLabel
652 labelType (ModuleInitTableLabel _) = DataLabel
653 labelType (LargeSRTLabel _) = DataLabel
654 labelType (LargeBitmapLabel _) = DataLabel
655 labelType (ForeignLabel _ _ _ IsFunction) = CodeLabel
656 labelType (IdLabel _ _ info) = idInfoLabelType info
657 labelType _ = DataLabel
659 idInfoLabelType info =
661 InfoTable -> DataLabel
662 Closure -> GcPtrLabel
663 ConInfoTable -> DataLabel
664 StaticInfoTable -> DataLabel
665 ClosureTable -> DataLabel
666 RednCounts -> DataLabel
670 -- -----------------------------------------------------------------------------
671 -- Does a CLabel need dynamic linkage?
673 -- When referring to data in code, we need to know whether
674 -- that data resides in a DLL or not. [Win32 only.]
675 -- @labelDynamic@ returns @True@ if the label is located
676 -- in a DLL, be it a data reference or not.
678 labelDynamic :: PackageId -> CLabel -> Bool
679 labelDynamic this_pkg lbl =
681 RtsLabel _ -> not opt_Static && (this_pkg /= rtsPackageId) -- i.e., is the RTS in a DLL or not?
682 IdLabel n _ k -> isDllName this_pkg n
683 #if mingw32_TARGET_OS
684 ForeignLabel _ _ d _ -> d
686 -- On Mac OS X and on ELF platforms, false positives are OK,
687 -- so we claim that all foreign imports come from dynamic libraries
688 ForeignLabel _ _ _ _ -> True
690 ModuleInitLabel m _ -> not opt_Static && this_pkg /= (modulePackageId m)
691 PlainModuleInitLabel m -> not opt_Static && this_pkg /= (modulePackageId m)
692 ModuleInitTableLabel m -> not opt_Static && this_pkg /= (modulePackageId m)
694 -- Note that DynamicLinkerLabels do NOT require dynamic linking themselves.
698 OLD?: These GRAN functions are needed for spitting out GRAN_FETCH() at the
699 right places. It is used to detect when the abstractC statement of an
700 CCodeBlock actually contains the code for a slow entry point. -- HWL
702 We need at least @Eq@ for @CLabels@, because we want to avoid
703 duplicate declarations in generating C (see @labelSeenTE@ in
707 -----------------------------------------------------------------------------
708 -- Printing out CLabels.
715 where <name> is <Module>_<name> for external names and <unique> for
716 internal names. <type> is one of the following:
719 srt Static reference table
720 srtd Static reference table descriptor
721 entry Entry code (function, closure)
722 slow Slow entry code (if any)
723 ret Direct return address
725 <n>_alt Case alternative (tag n)
726 dflt Default case alternative
727 btm Large bitmap vector
728 closure Static closure
729 con_entry Dynamic Constructor entry code
730 con_info Dynamic Constructor info table
731 static_entry Static Constructor entry code
732 static_info Static Constructor info table
733 sel_info Selector info table
734 sel_entry Selector entry code
736 ccs Cost centre stack
738 Many of these distinctions are only for documentation reasons. For
739 example, _ret is only distinguished from _entry to make it easy to
740 tell whether a code fragment is a return point or a closure/function
744 instance Outputable CLabel where
747 pprCLabel :: CLabel -> SDoc
749 #if ! OMIT_NATIVE_CODEGEN
750 pprCLabel (AsmTempLabel u)
751 = getPprStyle $ \ sty ->
753 ptext asmTempLabelPrefix <> pprUnique u
755 char '_' <> pprUnique u
757 pprCLabel (DynamicLinkerLabel info lbl)
758 = pprDynamicLinkerAsmLabel info lbl
760 pprCLabel PicBaseLabel
763 pprCLabel (DeadStripPreventer lbl)
764 = pprCLabel lbl <> ptext (sLit "_dsp")
768 #if ! OMIT_NATIVE_CODEGEN
769 getPprStyle $ \ sty ->
771 maybe_underscore (pprAsmCLbl lbl)
777 | underscorePrefix = pp_cSEP <> doc
780 #ifdef mingw32_TARGET_OS
781 -- In asm mode, we need to put the suffix on a stdcall ForeignLabel.
782 -- (The C compiler does this itself).
783 pprAsmCLbl (ForeignLabel fs (Just sz) _ _)
784 = ftext fs <> char '@' <> int sz
789 pprCLbl (StringLitLabel u)
790 = pprUnique u <> ptext (sLit "_str")
792 pprCLbl (CaseLabel u CaseReturnPt)
793 = hcat [pprUnique u, ptext (sLit "_ret")]
794 pprCLbl (CaseLabel u CaseReturnInfo)
795 = hcat [pprUnique u, ptext (sLit "_info")]
796 pprCLbl (CaseLabel u (CaseAlt tag))
797 = hcat [pprUnique u, pp_cSEP, int tag, ptext (sLit "_alt")]
798 pprCLbl (CaseLabel u CaseDefault)
799 = hcat [pprUnique u, ptext (sLit "_dflt")]
801 pprCLbl (LargeSRTLabel u) = pprUnique u <> pp_cSEP <> ptext (sLit "srtd")
802 pprCLbl (LargeBitmapLabel u) = text "b" <> pprUnique u <> pp_cSEP <> ptext (sLit "btm")
803 -- Some bitsmaps for tuple constructors have a numeric tag (e.g. '7')
804 -- until that gets resolved we'll just force them to start
805 -- with a letter so the label will be legal assmbly code.
808 pprCLbl (RtsLabel (RtsCode str)) = ftext str
809 pprCLbl (RtsLabel (RtsData str)) = ftext str
810 pprCLbl (RtsLabel (RtsGcPtr str)) = ftext str
812 pprCLbl (RtsLabel (RtsApFast str)) = ftext str <> ptext (sLit "_fast")
814 pprCLbl (RtsLabel (RtsSelectorInfoTable upd_reqd offset))
815 = hcat [ptext (sLit "stg_sel_"), text (show offset),
817 then (sLit "_upd_info")
818 else (sLit "_noupd_info"))
821 pprCLbl (RtsLabel (RtsSelectorEntry upd_reqd offset))
822 = hcat [ptext (sLit "stg_sel_"), text (show offset),
824 then (sLit "_upd_entry")
825 else (sLit "_noupd_entry"))
828 pprCLbl (RtsLabel (RtsApInfoTable upd_reqd arity))
829 = hcat [ptext (sLit "stg_ap_"), text (show arity),
831 then (sLit "_upd_info")
832 else (sLit "_noupd_info"))
835 pprCLbl (RtsLabel (RtsApEntry upd_reqd arity))
836 = hcat [ptext (sLit "stg_ap_"), text (show arity),
838 then (sLit "_upd_entry")
839 else (sLit "_noupd_entry"))
842 pprCLbl (RtsLabel (RtsInfo fs))
843 = ftext fs <> ptext (sLit "_info")
845 pprCLbl (RtsLabel (RtsEntry fs))
846 = ftext fs <> ptext (sLit "_entry")
848 pprCLbl (RtsLabel (RtsRetInfo fs))
849 = ftext fs <> ptext (sLit "_info")
851 pprCLbl (RtsLabel (RtsRet fs))
852 = ftext fs <> ptext (sLit "_ret")
854 pprCLbl (RtsLabel (RtsPrimOp primop))
855 = ptext (sLit "stg_") <> ppr primop
857 pprCLbl (RtsLabel (RtsSlowTickyCtr pat))
858 = ptext (sLit "SLOW_CALL_") <> text pat <> ptext (sLit "_ctr")
860 pprCLbl ModuleRegdLabel
861 = ptext (sLit "_module_registered")
863 pprCLbl (ForeignLabel str _ _ _)
866 pprCLbl (IdLabel name cafs flavor) = ppr name <> ppIdFlavor flavor
868 pprCLbl (CC_Label cc) = ppr cc
869 pprCLbl (CCS_Label ccs) = ppr ccs
871 pprCLbl (ModuleInitLabel mod way)
872 = ptext (sLit "__stginit_") <> ppr mod
873 <> char '_' <> text way
874 pprCLbl (PlainModuleInitLabel mod)
875 = ptext (sLit "__stginit_") <> ppr mod
876 pprCLbl (ModuleInitTableLabel mod)
877 = ptext (sLit "__stginittable_") <> ppr mod
879 pprCLbl (HpcTicksLabel mod)
880 = ptext (sLit "_hpc_tickboxes_") <> ppr mod <> ptext (sLit "_hpc")
882 pprCLbl HpcModuleNameLabel
883 = ptext (sLit "_hpc_module_name_str")
885 ppIdFlavor :: IdLabelInfo -> SDoc
886 ppIdFlavor x = pp_cSEP <>
888 Closure -> ptext (sLit "closure")
889 SRT -> ptext (sLit "srt")
890 InfoTable -> ptext (sLit "info")
891 Entry -> ptext (sLit "entry")
892 Slow -> ptext (sLit "slow")
893 RednCounts -> ptext (sLit "ct")
894 ConEntry -> ptext (sLit "con_entry")
895 ConInfoTable -> ptext (sLit "con_info")
896 StaticConEntry -> ptext (sLit "static_entry")
897 StaticInfoTable -> ptext (sLit "static_info")
898 ClosureTable -> ptext (sLit "closure_tbl")
904 -- -----------------------------------------------------------------------------
905 -- Machine-dependent knowledge about labels.
907 underscorePrefix :: Bool -- leading underscore on assembler labels?
908 underscorePrefix = (cLeadingUnderscore == "YES")
910 asmTempLabelPrefix :: LitString -- for formatting labels
913 {- The alpha assembler likes temporary labels to look like $L123
914 instead of L123. (Don't toss the L, because then Lf28
918 #elif darwin_TARGET_OS
924 pprDynamicLinkerAsmLabel :: DynamicLinkerLabelInfo -> CLabel -> SDoc
926 #if x86_64_TARGET_ARCH && darwin_TARGET_OS
927 pprDynamicLinkerAsmLabel CodeStub lbl
928 = char 'L' <> pprCLabel lbl <> text "$stub"
929 pprDynamicLinkerAsmLabel SymbolPtr lbl
930 = char 'L' <> pprCLabel lbl <> text "$non_lazy_ptr"
931 pprDynamicLinkerAsmLabel GotSymbolPtr lbl
932 = pprCLabel lbl <> text "@GOTPCREL"
933 pprDynamicLinkerAsmLabel GotSymbolOffset lbl
935 pprDynamicLinkerAsmLabel _ _
936 = panic "pprDynamicLinkerAsmLabel"
937 #elif darwin_TARGET_OS
938 pprDynamicLinkerAsmLabel CodeStub lbl
939 = char 'L' <> pprCLabel lbl <> text "$stub"
940 pprDynamicLinkerAsmLabel SymbolPtr lbl
941 = char 'L' <> pprCLabel lbl <> text "$non_lazy_ptr"
942 pprDynamicLinkerAsmLabel _ _
943 = panic "pprDynamicLinkerAsmLabel"
944 #elif powerpc_TARGET_ARCH && linux_TARGET_OS
945 pprDynamicLinkerAsmLabel CodeStub lbl
946 = pprCLabel lbl <> text "@plt"
947 pprDynamicLinkerAsmLabel SymbolPtr lbl
948 = text ".LC_" <> pprCLabel lbl
949 pprDynamicLinkerAsmLabel _ _
950 = panic "pprDynamicLinkerAsmLabel"
951 #elif x86_64_TARGET_ARCH && linux_TARGET_OS
952 pprDynamicLinkerAsmLabel CodeStub lbl
953 = pprCLabel lbl <> text "@plt"
954 pprDynamicLinkerAsmLabel GotSymbolPtr lbl
955 = pprCLabel lbl <> text "@gotpcrel"
956 pprDynamicLinkerAsmLabel GotSymbolOffset lbl
958 pprDynamicLinkerAsmLabel SymbolPtr lbl
959 = text ".LC_" <> pprCLabel lbl
960 #elif linux_TARGET_OS
961 pprDynamicLinkerAsmLabel CodeStub lbl
962 = pprCLabel lbl <> text "@plt"
963 pprDynamicLinkerAsmLabel SymbolPtr lbl
964 = text ".LC_" <> pprCLabel lbl
965 pprDynamicLinkerAsmLabel GotSymbolPtr lbl
966 = pprCLabel lbl <> text "@got"
967 pprDynamicLinkerAsmLabel GotSymbolOffset lbl
968 = pprCLabel lbl <> text "@gotoff"
969 #elif mingw32_TARGET_OS
970 pprDynamicLinkerAsmLabel SymbolPtr lbl
971 = text "__imp_" <> pprCLabel lbl
972 pprDynamicLinkerAsmLabel _ _
973 = panic "pprDynamicLinkerAsmLabel"
975 pprDynamicLinkerAsmLabel _ _
976 = panic "pprDynamicLinkerAsmLabel"