CLabel.hs 46.1 KB
Newer Older
1 2 3 4
-----------------------------------------------------------------------------
--
-- Object-file symbols (called CLabel for histerical raisins).
--
Simon Marlow's avatar
Simon Marlow committed
5
-- (c) The University of Glasgow 2004-2006
6 7 8 9
--
-----------------------------------------------------------------------------

module CLabel (
Ian Lynagh's avatar
Ian Lynagh committed
10 11 12 13 14 15
        CLabel, -- abstract type
        ForeignLabelSource(..),
        pprDebugCLabel,

        mkClosureLabel,
        mkSRTLabel,
16
        mkTopSRTLabel,
Ian Lynagh's avatar
Ian Lynagh committed
17 18
        mkInfoTableLabel,
        mkEntryLabel,
19
        mkSlowEntryLabel,
Ian Lynagh's avatar
Ian Lynagh committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
        mkConEntryLabel,
        mkStaticConEntryLabel,
        mkRednCountsLabel,
        mkConInfoTableLabel,
        mkStaticInfoTableLabel,
        mkLargeSRTLabel,
        mkApEntryLabel,
        mkApInfoTableLabel,
        mkClosureTableLabel,

        mkLocalClosureLabel,
        mkLocalInfoTableLabel,
        mkLocalEntryLabel,
        mkLocalConEntryLabel,
        mkLocalStaticConEntryLabel,
        mkLocalConInfoTableLabel,
        mkLocalStaticInfoTableLabel,
        mkLocalClosureTableLabel,

        mkReturnPtLabel,
        mkReturnInfoLabel,
        mkAltLabel,
        mkDefaultLabel,
        mkBitmapLabel,
        mkStringLitLabel,

        mkAsmTempLabel,
47

48
        mkPlainModuleInitLabel,
49

Ian Lynagh's avatar
Ian Lynagh committed
50 51 52 53 54
        mkSplitMarkerLabel,
        mkDirty_MUT_VAR_Label,
        mkUpdInfoLabel,
        mkBHUpdInfoLabel,
        mkIndStaticInfoLabel,
55
        mkMainCapabilityLabel,
Ian Lynagh's avatar
Ian Lynagh committed
56 57
        mkMAP_FROZEN_infoLabel,
        mkMAP_DIRTY_infoLabel,
58 59
        mkEMPTY_MVAR_infoLabel,

Ian Lynagh's avatar
Ian Lynagh committed
60
        mkTopTickyCtrLabel,
61
        mkCAFBlackHoleInfoTableLabel,
batterseapower's avatar
batterseapower committed
62
        mkCAFBlackHoleEntryLabel,
Ian Lynagh's avatar
Ian Lynagh committed
63
        mkRtsPrimOpLabel,
nfrisby's avatar
nfrisby committed
64
        mkRtsSlowFastTickyCtrLabel,
65

66
        mkSelectorInfoLabel,
Ian Lynagh's avatar
Ian Lynagh committed
67
        mkSelectorEntryLabel,
68

Ian Lynagh's avatar
Ian Lynagh committed
69 70 71 72 73 74
        mkCmmInfoLabel,
        mkCmmEntryLabel,
        mkCmmRetInfoLabel,
        mkCmmRetLabel,
        mkCmmCodeLabel,
        mkCmmDataLabel,
75
        mkCmmClosureLabel,
76

Ian Lynagh's avatar
Ian Lynagh committed
77
        mkRtsApFastLabel,
78

79 80
        mkPrimCallLabel,

Ian Lynagh's avatar
Ian Lynagh committed
81
        mkForeignLabel,
82
        addLabelSize,
83
        foreignLabelStdcallInfo,
84

Ian Lynagh's avatar
Ian Lynagh committed
85
        mkCCLabel, mkCCSLabel,
86

87 88 89
        DynamicLinkerLabelInfo(..),
        mkDynamicLinkerLabel,
        dynamicLinkerLabelInfo,
Ian Lynagh's avatar
Ian Lynagh committed
90

91
        mkPicBaseLabel,
92
        mkDeadStripPreventer,
93

andy@galois.com's avatar
andy@galois.com committed
94 95
        mkHpcTicksLabel,

96
        hasCAF,
97
        needsCDecl, isAsmTemp, maybeAsmTemp, externallyVisibleCLabel,
98
        isMathFun,
Ian Lynagh's avatar
Ian Lynagh committed
99
        isCFunctionLabel, isGcPtrLabel, labelDynamic,
100 101

        -- * Conversions
nfrisby's avatar
nfrisby committed
102
        toClosureLbl, toSlowEntryLbl, toEntryLbl, toInfoLbl, toRednCountsLbl, hasHaskellName,
103

104
        pprCLabel
105 106
    ) where

107
import IdInfo
108
import BasicTypes
Simon Marlow's avatar
Simon Marlow committed
109 110 111 112 113 114 115
import Packages
import Module
import Name
import Unique
import PrimOp
import Config
import CostCentre
116 117
import Outputable
import FastString
118
import DynFlags
Ian Lynagh's avatar
Ian Lynagh committed
119
import Platform
120
import UniqSet
121 122 123 124 125

-- -----------------------------------------------------------------------------
-- The CLabel type

{-
126
  | CLabel is an abstract type that supports the following operations:
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

  - Pretty printing

  - In a C file, does it need to be declared before use?  (i.e. is it
    guaranteed to be already in scope in the places we need to refer to it?)

  - If it needs to be declared, what type (code or data) should it be
    declared to have?

  - Is it visible outside this object file or not?

  - Is it "dynamic" (see details below)

  - Eq and Ord, so that we can make sets of CLabels (currently only
    used in outputting C as far as I can tell, to avoid generating
    more than one declaration for any given label).

  - Converting an info table label into an entry label.
-}

data CLabel
148
  = -- | A label related to the definition of a particular Id or Con in a .hs file.
Ian Lynagh's avatar
Ian Lynagh committed
149 150
    IdLabel
        Name
151
        CafInfo
Ian Lynagh's avatar
Ian Lynagh committed
152 153
        IdLabelInfo             -- encodes the suffix of the label

154
  -- | A label from a .cmm file that is not associated with a .hs level Id.
Ian Lynagh's avatar
Ian Lynagh committed
155 156 157 158
  | CmmLabel
        PackageId               -- what package the label belongs to.
        FastString              -- identifier giving the prefix of the label
        CmmLabelInfo            -- encodes the suffix of the label
159 160 161

  -- | A label with a baked-in \/ algorithmically generated name that definitely
  --    comes from the RTS. The code for it must compile into libHSrts.a \/ libHSrts.so
Ian Lynagh's avatar
Ian Lynagh committed
162
  --    If it doesn't have an algorithmically generated name then use a CmmLabel
Ben.Lippmeier@anu.edu.au's avatar
Ben.Lippmeier@anu.edu.au committed
163
  --    instead and give it an appropriate PackageId argument.
Ian Lynagh's avatar
Ian Lynagh committed
164 165
  | RtsLabel
        RtsLabelInfo
166

167 168
  -- | A 'C' (or otherwise foreign) label.
  --
Ian Lynagh's avatar
Ian Lynagh committed
169 170 171 172 173 174
  | ForeignLabel
        FastString              -- name of the imported label.

        (Maybe Int)             -- possible '@n' suffix for stdcall functions
                                -- When generating C, the '@n' suffix is omitted, but when
                                -- generating assembler we must add it to the label.
175

Ian Lynagh's avatar
Ian Lynagh committed
176
        ForeignLabelSource      -- what package the foreign label is in.
177

178 179 180
        FunctionOrData

  -- | A family of labels related to a particular case expression.
Ian Lynagh's avatar
Ian Lynagh committed
181 182 183
  | CaseLabel
        {-# UNPACK #-} !Unique  -- Unique says which case expression
        CaseLabelInfo
184

Ian Lynagh's avatar
Ian Lynagh committed
185 186
  | AsmTempLabel
        {-# UNPACK #-} !Unique
187

188
  | StringLitLabel
Ian Lynagh's avatar
Ian Lynagh committed
189
        {-# UNPACK #-} !Unique
190

191
  | PlainModuleInitLabel        -- without the version & way info
Ian Lynagh's avatar
Ian Lynagh committed
192
        Module
193

194 195 196
  | CC_Label  CostCentre
  | CCS_Label CostCentreStack

Ian Lynagh's avatar
Ian Lynagh committed
197 198 199

  -- | These labels are generated and used inside the NCG only.
  --    They are special variants of a label used for dynamic linking
200
  --    see module PositionIndependentCode for details.
201
  | DynamicLinkerLabel DynamicLinkerLabelInfo CLabel
Ian Lynagh's avatar
Ian Lynagh committed
202 203 204 205

  -- | This label is generated and used inside the NCG only.
  --    It is used as a base for PIC calculations on some platforms.
  --    It takes the form of a local numeric assembler label '1'; and
206 207
  --    is pretty-printed as 1b, referring to the previous definition
  --    of 1: in the assembler source file.
Ian Lynagh's avatar
Ian Lynagh committed
208 209
  | PicBaseLabel

210 211
  -- | A label before an info table to prevent excessive dead-stripping on darwin
  | DeadStripPreventer CLabel
212

213

214 215
  -- | Per-module table of tick locations
  | HpcTicksLabel Module
216

217
  -- | Static reference table
218
  | SRTLabel !Unique
219

220 221
  -- | Label of an StgLargeSRT
  | LargeSRTLabel
222 223
        {-# UNPACK #-} !Unique

224 225
  -- | A bitmap (function or case return)
  | LargeBitmapLabel
226 227
        {-# UNPACK #-} !Unique

228 229
  deriving (Eq, Ord)

230 231 232 233 234

-- | Record where a foreign label is stored.
data ForeignLabelSource

   -- | Label is in a named package
Ian Lynagh's avatar
Ian Lynagh committed
235 236
   = ForeignLabelInPackage      PackageId

237
   -- | Label is in some external, system package that doesn't also
Ian Lynagh's avatar
Ian Lynagh committed
238 239 240 241
   --   contain compiled Haskell code, and is not associated with any .hi files.
   --   We don't have to worry about Haskell code being inlined from
   --   external packages. It is safe to treat the RTS package as "external".
   | ForeignLabelInExternalPackage
242 243

   -- | Label is in the package currenly being compiled.
Ian Lynagh's avatar
Ian Lynagh committed
244 245 246 247
   --   This is only used for creating hacky tmp labels during code generation.
   --   Don't use it in any code that might be inlined across a package boundary
   --   (ie, core code) else the information will be wrong relative to the
   --   destination module.
248
   | ForeignLabelInThisPackage
Ian Lynagh's avatar
Ian Lynagh committed
249 250

   deriving (Eq, Ord)
251 252 253


-- | For debugging problems with the CLabel representation.
Ian Lynagh's avatar
Ian Lynagh committed
254 255
--      We can't make a Show instance for CLabel because lots of its components don't have instances.
--      The regular Outputable instance only shows the label name, and not its other info.
256
--
257 258
pprDebugCLabel :: CLabel -> SDoc
pprDebugCLabel lbl
259
 = case lbl of
Ian Lynagh's avatar
Ian Lynagh committed
260
        IdLabel{}       -> ppr lbl <> (parens $ text "IdLabel")
Ian Lynagh's avatar
Ian Lynagh committed
261
        CmmLabel pkg _name _info
Ian Lynagh's avatar
Ian Lynagh committed
262
         -> ppr lbl <> (parens $ text "CmmLabel" <+> ppr pkg)
263

Ian Lynagh's avatar
Ian Lynagh committed
264
        RtsLabel{}      -> ppr lbl <> (parens $ text "RtsLabel")
265

Ian Lynagh's avatar
Ian Lynagh committed
266
        ForeignLabel _name mSuffix src funOrData
Ian Lynagh's avatar
Ian Lynagh committed
267
            -> ppr lbl <> (parens $ text "ForeignLabel"
Ian Lynagh's avatar
Ian Lynagh committed
268 269 270
                                <+> ppr mSuffix
                                <+> ppr src
                                <+> ppr funOrData)
271

Ian Lynagh's avatar
Ian Lynagh committed
272
        _               -> ppr lbl <> (parens $ text "other CLabel)")
273 274


275
data IdLabelInfo
Ian Lynagh's avatar
Ian Lynagh committed
276
  = Closure             -- ^ Label for closure
277 278 279
  | SRT                 -- ^ Static reference table (TODO: could be removed
                        -- with the old code generator, but might be needed
                        -- when we implement the New SRT Plan)
280
  | InfoTable           -- ^ Info tables for closures; always read-only
Ian Lynagh's avatar
Ian Lynagh committed
281
  | Entry               -- ^ Entry point
282
  | Slow                -- ^ Slow entry point
283

284 285 286 287
  | LocalInfoTable      -- ^ Like InfoTable but not externally visible
  | LocalEntry          -- ^ Like Entry but not externally visible

  | RednCounts          -- ^ Label of place to keep Ticky-ticky  info for this Id
288

Ian Lynagh's avatar
Ian Lynagh committed
289 290 291 292
  | ConEntry            -- ^ Constructor entry point
  | ConInfoTable        -- ^ Corresponding info table
  | StaticConEntry      -- ^ Static constructor entry point
  | StaticInfoTable     -- ^ Corresponding info table
293

Ian Lynagh's avatar
Ian Lynagh committed
294
  | ClosureTable        -- ^ Table of closures for Enum tycons
295 296 297 298 299 300 301 302 303 304 305 306 307

  deriving (Eq, Ord)


data CaseLabelInfo
  = CaseReturnPt
  | CaseReturnInfo
  | CaseAlt ConTag
  | CaseDefault
  deriving (Eq, Ord)


data RtsLabelInfo
308 309
  = RtsSelectorInfoTable Bool{-updatable-} Int{-offset-}  -- ^ Selector thunks
  | RtsSelectorEntry     Bool{-updatable-} Int{-offset-}
310

311 312
  | RtsApInfoTable       Bool{-updatable-} Int{-arity-}    -- ^ AP thunks
  | RtsApEntry           Bool{-updatable-} Int{-arity-}
313 314

  | RtsPrimOp PrimOp
Ian Lynagh's avatar
Ian Lynagh committed
315
  | RtsApFast     FastString    -- ^ _fast versions of generic apply
nfrisby's avatar
nfrisby committed
316
  | RtsSlowFastTickyCtr String
317 318

  deriving (Eq, Ord)
319 320
  -- NOTE: Eq on LitString compares the pointer only, so this isn't
  -- a real equality.
321

322 323

-- | What type of Cmm label we're dealing with.
Ian Lynagh's avatar
Ian Lynagh committed
324 325
--      Determines the suffix appended to the name when a CLabel.CmmLabel
--      is pretty printed.
326
data CmmLabelInfo
Ian Lynagh's avatar
Ian Lynagh committed
327 328 329 330 331 332
  = CmmInfo                     -- ^ misc rts info tabless,     suffix _info
  | CmmEntry                    -- ^ misc rts entry points,     suffix _entry
  | CmmRetInfo                  -- ^ misc rts ret info tables,  suffix _info
  | CmmRet                      -- ^ misc rts return points,    suffix _ret
  | CmmData                     -- ^ misc rts data bits, eg CHARLIKE_closure
  | CmmCode                     -- ^ misc rts code
333
  | CmmClosure                  -- ^ closures eg CHARLIKE_closure
Ian Lynagh's avatar
Ian Lynagh committed
334
  | CmmPrimCall                 -- ^ a prim call to some hand written Cmm code
335 336
  deriving (Eq, Ord)

337
data DynamicLinkerLabelInfo
Ian Lynagh's avatar
Ian Lynagh committed
338 339 340 341 342
  = CodeStub                    -- MachO: Lfoo$stub, ELF: foo@plt
  | SymbolPtr                   -- MachO: Lfoo$non_lazy_ptr, Windows: __imp_foo
  | GotSymbolPtr                -- ELF: foo@got
  | GotSymbolOffset             -- ELF: foo@gotoff

343
  deriving (Eq, Ord)
Ian Lynagh's avatar
Ian Lynagh committed
344

345

346 347
-- -----------------------------------------------------------------------------
-- Constructing CLabels
348
-- -----------------------------------------------------------------------------
349

Ian Lynagh's avatar
Ian Lynagh committed
350
-- Constructing IdLabels
351
-- These are always local:
Ian Lynagh's avatar
Ian Lynagh committed
352
mkSlowEntryLabel :: Name -> CafInfo -> CLabel
Ian Lynagh's avatar
Ian Lynagh committed
353
mkSlowEntryLabel        name c         = IdLabel name  c Slow
354

355 356
mkTopSRTLabel     :: Unique -> CLabel
mkTopSRTLabel u = SRTLabel u
357

Ian Lynagh's avatar
Ian Lynagh committed
358
mkSRTLabel        :: Name -> CafInfo -> CLabel
nfrisby's avatar
nfrisby committed
359
mkRednCountsLabel :: Name -> CLabel
Ian Lynagh's avatar
Ian Lynagh committed
360
mkSRTLabel              name c  = IdLabel name  c SRT
nfrisby's avatar
nfrisby committed
361 362
mkRednCountsLabel       name    =
  IdLabel name NoCafRefs RednCounts  -- Note [ticky for LNE]
363 364

-- These have local & (possibly) external variants:
Ian Lynagh's avatar
Ian Lynagh committed
365 366 367 368
mkLocalClosureLabel      :: Name -> CafInfo -> CLabel
mkLocalInfoTableLabel    :: Name -> CafInfo -> CLabel
mkLocalEntryLabel        :: Name -> CafInfo -> CLabel
mkLocalClosureTableLabel :: Name -> CafInfo -> CLabel
Ian Lynagh's avatar
Ian Lynagh committed
369
mkLocalClosureLabel     name c  = IdLabel name  c Closure
370 371
mkLocalInfoTableLabel   name c  = IdLabel name  c LocalInfoTable
mkLocalEntryLabel       name c  = IdLabel name  c LocalEntry
372 373
mkLocalClosureTableLabel name c = IdLabel name  c ClosureTable

Ian Lynagh's avatar
Ian Lynagh committed
374 375 376 377 378 379 380 381 382 383
mkClosureLabel              :: Name -> CafInfo -> CLabel
mkInfoTableLabel            :: Name -> CafInfo -> CLabel
mkEntryLabel                :: Name -> CafInfo -> CLabel
mkClosureTableLabel         :: Name -> CafInfo -> CLabel
mkLocalConInfoTableLabel    :: CafInfo -> Name -> CLabel
mkLocalConEntryLabel        :: CafInfo -> Name -> CLabel
mkLocalStaticInfoTableLabel :: CafInfo -> Name -> CLabel
mkLocalStaticConEntryLabel  :: CafInfo -> Name -> CLabel
mkConInfoTableLabel         :: Name -> CafInfo -> CLabel
mkStaticInfoTableLabel      :: Name -> CafInfo -> CLabel
384
mkClosureLabel name         c     = IdLabel name c Closure
385
mkInfoTableLabel name       c     = IdLabel name c InfoTable
386
mkEntryLabel name           c     = IdLabel name c Entry
387 388
mkClosureTableLabel name    c     = IdLabel name c ClosureTable
mkLocalConInfoTableLabel    c con = IdLabel con c ConInfoTable
Ian Lynagh's avatar
Ian Lynagh committed
389
mkLocalConEntryLabel        c con = IdLabel con c ConEntry
390 391
mkLocalStaticInfoTableLabel c con = IdLabel con c StaticInfoTable
mkLocalStaticConEntryLabel  c con = IdLabel con c StaticConEntry
392 393
mkConInfoTableLabel name    c     = IdLabel name c ConInfoTable
mkStaticInfoTableLabel name c     = IdLabel name c StaticInfoTable
394

Ian Lynagh's avatar
Ian Lynagh committed
395 396
mkConEntryLabel       :: Name -> CafInfo -> CLabel
mkStaticConEntryLabel :: Name -> CafInfo -> CLabel
397 398
mkConEntryLabel name        c     = IdLabel name c ConEntry
mkStaticConEntryLabel name  c     = IdLabel name c StaticConEntry
399

400
-- Constructing Cmm Labels
401
mkDirty_MUT_VAR_Label, mkSplitMarkerLabel, mkUpdInfoLabel,
Ian Lynagh's avatar
Ian Lynagh committed
402 403 404 405
    mkBHUpdInfoLabel, mkIndStaticInfoLabel, mkMainCapabilityLabel,
    mkMAP_FROZEN_infoLabel, mkMAP_DIRTY_infoLabel,
    mkEMPTY_MVAR_infoLabel, mkTopTickyCtrLabel,
    mkCAFBlackHoleInfoTableLabel, mkCAFBlackHoleEntryLabel :: CLabel
406
mkDirty_MUT_VAR_Label           = mkForeignLabel (fsLit "dirty_MUT_VAR") Nothing ForeignLabelInExternalPackage IsFunction
Ian Lynagh's avatar
Ian Lynagh committed
407 408 409 410 411 412 413 414 415 416 417
mkSplitMarkerLabel              = CmmLabel rtsPackageId (fsLit "__stg_split_marker")    CmmCode
mkUpdInfoLabel                  = CmmLabel rtsPackageId (fsLit "stg_upd_frame")         CmmInfo
mkBHUpdInfoLabel                = CmmLabel rtsPackageId (fsLit "stg_bh_upd_frame" )     CmmInfo
mkIndStaticInfoLabel            = CmmLabel rtsPackageId (fsLit "stg_IND_STATIC")        CmmInfo
mkMainCapabilityLabel           = CmmLabel rtsPackageId (fsLit "MainCapability")        CmmData
mkMAP_FROZEN_infoLabel          = CmmLabel rtsPackageId (fsLit "stg_MUT_ARR_PTRS_FROZEN0") CmmInfo
mkMAP_DIRTY_infoLabel           = CmmLabel rtsPackageId (fsLit "stg_MUT_ARR_PTRS_DIRTY") CmmInfo
mkEMPTY_MVAR_infoLabel          = CmmLabel rtsPackageId (fsLit "stg_EMPTY_MVAR")        CmmInfo
mkTopTickyCtrLabel              = CmmLabel rtsPackageId (fsLit "top_ct")                CmmData
mkCAFBlackHoleInfoTableLabel    = CmmLabel rtsPackageId (fsLit "stg_CAF_BLACKHOLE")     CmmInfo
mkCAFBlackHoleEntryLabel        = CmmLabel rtsPackageId (fsLit "stg_CAF_BLACKHOLE")     CmmEntry
418 419

-----
420
mkCmmInfoLabel,   mkCmmEntryLabel, mkCmmRetInfoLabel, mkCmmRetLabel,
421
  mkCmmCodeLabel, mkCmmDataLabel,  mkCmmClosureLabel
Ian Lynagh's avatar
Ian Lynagh committed
422
        :: PackageId -> FastString -> CLabel
423

Ian Lynagh's avatar
Ian Lynagh committed
424 425 426 427 428 429
mkCmmInfoLabel      pkg str     = CmmLabel pkg str CmmInfo
mkCmmEntryLabel     pkg str     = CmmLabel pkg str CmmEntry
mkCmmRetInfoLabel   pkg str     = CmmLabel pkg str CmmRetInfo
mkCmmRetLabel       pkg str     = CmmLabel pkg str CmmRet
mkCmmCodeLabel      pkg str     = CmmLabel pkg str CmmCode
mkCmmDataLabel      pkg str     = CmmLabel pkg str CmmData
430
mkCmmClosureLabel   pkg str     = CmmLabel pkg str CmmClosure
431 432 433


-- Constructing RtsLabels
Ian Lynagh's avatar
Ian Lynagh committed
434
mkRtsPrimOpLabel :: PrimOp -> CLabel
Ian Lynagh's avatar
Ian Lynagh committed
435
mkRtsPrimOpLabel primop         = RtsLabel (RtsPrimOp primop)
436

Ian Lynagh's avatar
Ian Lynagh committed
437 438
mkSelectorInfoLabel  :: Bool -> Int -> CLabel
mkSelectorEntryLabel :: Bool -> Int -> CLabel
Ian Lynagh's avatar
Ian Lynagh committed
439 440
mkSelectorInfoLabel  upd off    = RtsLabel (RtsSelectorInfoTable upd off)
mkSelectorEntryLabel upd off    = RtsLabel (RtsSelectorEntry     upd off)
441

Ian Lynagh's avatar
Ian Lynagh committed
442 443
mkApInfoTableLabel :: Bool -> Int -> CLabel
mkApEntryLabel     :: Bool -> Int -> CLabel
Ian Lynagh's avatar
Ian Lynagh committed
444 445
mkApInfoTableLabel   upd off    = RtsLabel (RtsApInfoTable       upd off)
mkApEntryLabel       upd off    = RtsLabel (RtsApEntry           upd off)
446

447

448
-- A call to some primitive hand written Cmm code
449
mkPrimCallLabel :: PrimCall -> CLabel
Ian Lynagh's avatar
Ian Lynagh committed
450 451
mkPrimCallLabel (PrimCall str pkg)
        = CmmLabel pkg str CmmPrimCall
452

453

454
-- Constructing ForeignLabels
455

456
-- | Make a foreign label
Ian Lynagh's avatar
Ian Lynagh committed
457 458 459 460 461 462
mkForeignLabel
        :: FastString           -- name
        -> Maybe Int            -- size prefix
        -> ForeignLabelSource   -- what package it's in
        -> FunctionOrData
        -> CLabel
463 464 465 466 467 468

mkForeignLabel str mb_sz src fod
    = ForeignLabel str mb_sz src  fod


-- | Update the label size field in a ForeignLabel
469
addLabelSize :: CLabel -> Int -> CLabel
470 471
addLabelSize (ForeignLabel str _ src  fod) sz
    = ForeignLabel str (Just sz) src fod
472
addLabelSize label _
473
    = label
474

475
-- | Get the label size field from a ForeignLabel
476
foreignLabelStdcallInfo :: CLabel -> Maybe Int
477
foreignLabelStdcallInfo (ForeignLabel _ info _ _) = info
478 479
foreignLabelStdcallInfo _lbl = Nothing

480

481
-- Constructing Large*Labels
Ian Lynagh's avatar
Ian Lynagh committed
482 483
mkLargeSRTLabel :: Unique -> CLabel
mkBitmapLabel   :: Unique -> CLabel
Ian Lynagh's avatar
Ian Lynagh committed
484 485
mkLargeSRTLabel uniq            = LargeSRTLabel uniq
mkBitmapLabel   uniq            = LargeBitmapLabel uniq
486 487 488


-- Constructin CaseLabels
Ian Lynagh's avatar
Ian Lynagh committed
489 490 491 492
mkReturnPtLabel   :: Unique -> CLabel
mkReturnInfoLabel :: Unique -> CLabel
mkAltLabel        :: Unique -> ConTag -> CLabel
mkDefaultLabel    :: Unique -> CLabel
Ian Lynagh's avatar
Ian Lynagh committed
493 494 495 496
mkReturnPtLabel uniq            = CaseLabel uniq CaseReturnPt
mkReturnInfoLabel uniq          = CaseLabel uniq CaseReturnInfo
mkAltLabel      uniq tag        = CaseLabel uniq (CaseAlt tag)
mkDefaultLabel  uniq            = CaseLabel uniq CaseDefault
497

498
-- Constructing Cost Center Labels
Ian Lynagh's avatar
Ian Lynagh committed
499 500
mkCCLabel  :: CostCentre      -> CLabel
mkCCSLabel :: CostCentreStack -> CLabel
Ian Lynagh's avatar
Ian Lynagh committed
501 502
mkCCLabel           cc          = CC_Label cc
mkCCSLabel          ccs         = CCS_Label ccs
503

Ian Lynagh's avatar
Ian Lynagh committed
504
mkRtsApFastLabel :: FastString -> CLabel
505 506
mkRtsApFastLabel str = RtsLabel (RtsApFast str)

nfrisby's avatar
nfrisby committed
507 508
mkRtsSlowFastTickyCtrLabel :: String -> CLabel
mkRtsSlowFastTickyCtrLabel pat = RtsLabel (RtsSlowFastTickyCtr pat)
509

andy@galois.com's avatar
andy@galois.com committed
510

511
-- Constructing Code Coverage Labels
Ian Lynagh's avatar
Ian Lynagh committed
512
mkHpcTicksLabel :: Module -> CLabel
andy@galois.com's avatar
andy@galois.com committed
513 514
mkHpcTicksLabel                = HpcTicksLabel

515 516

-- Constructing labels used for dynamic linking
517
mkDynamicLinkerLabel :: DynamicLinkerLabelInfo -> CLabel -> CLabel
Ian Lynagh's avatar
Ian Lynagh committed
518
mkDynamicLinkerLabel            = DynamicLinkerLabel
519 520 521

dynamicLinkerLabelInfo :: CLabel -> Maybe (DynamicLinkerLabelInfo, CLabel)
dynamicLinkerLabelInfo (DynamicLinkerLabel info lbl) = Just (info, lbl)
Ian Lynagh's avatar
Ian Lynagh committed
522 523
dynamicLinkerLabelInfo _        = Nothing

524
mkPicBaseLabel :: CLabel
Ian Lynagh's avatar
Ian Lynagh committed
525
mkPicBaseLabel                  = PicBaseLabel
526

527 528

-- Constructing miscellaneous other labels
529
mkDeadStripPreventer :: CLabel -> CLabel
Ian Lynagh's avatar
Ian Lynagh committed
530
mkDeadStripPreventer lbl        = DeadStripPreventer lbl
531 532

mkStringLitLabel :: Unique -> CLabel
Ian Lynagh's avatar
Ian Lynagh committed
533
mkStringLitLabel                = StringLitLabel
534 535

mkAsmTempLabel :: Uniquable a => a -> CLabel
Ian Lynagh's avatar
Ian Lynagh committed
536
mkAsmTempLabel a                = AsmTempLabel (getUnique a)
537 538

mkPlainModuleInitLabel :: Module -> CLabel
Ian Lynagh's avatar
Ian Lynagh committed
539
mkPlainModuleInitLabel mod      = PlainModuleInitLabel mod
540

541
-- -----------------------------------------------------------------------------
542 543
-- Convert between different kinds of label

544 545
toClosureLbl :: CLabel -> CLabel
toClosureLbl (IdLabel n c _) = IdLabel n c Closure
546
toClosureLbl (CmmLabel m str _) = CmmLabel m str CmmClosure
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
toClosureLbl l = pprPanic "toClosureLbl" (ppr l)

toSlowEntryLbl :: CLabel -> CLabel
toSlowEntryLbl (IdLabel n c _) = IdLabel n c Slow
toSlowEntryLbl l = pprPanic "toSlowEntryLbl" (ppr l)

toEntryLbl :: CLabel -> CLabel
toEntryLbl (IdLabel n c LocalInfoTable)  = IdLabel n c LocalEntry
toEntryLbl (IdLabel n c ConInfoTable)    = IdLabel n c ConEntry
toEntryLbl (IdLabel n c StaticInfoTable) = IdLabel n c StaticConEntry
toEntryLbl (IdLabel n c _)               = IdLabel n c Entry
toEntryLbl (CaseLabel n CaseReturnInfo)  = CaseLabel n CaseReturnPt
toEntryLbl (CmmLabel m str CmmInfo)      = CmmLabel m str CmmEntry
toEntryLbl (CmmLabel m str CmmRetInfo)   = CmmLabel m str CmmRet
toEntryLbl l = pprPanic "toEntryLbl" (ppr l)

toInfoLbl :: CLabel -> CLabel
toInfoLbl (IdLabel n c Entry)          = IdLabel n c InfoTable
toInfoLbl (IdLabel n c LocalEntry)     = IdLabel n c LocalInfoTable
toInfoLbl (IdLabel n c ConEntry)       = IdLabel n c ConInfoTable
toInfoLbl (IdLabel n c StaticConEntry) = IdLabel n c StaticInfoTable
toInfoLbl (IdLabel n c _)              = IdLabel n c InfoTable
toInfoLbl (CaseLabel n CaseReturnPt)   = CaseLabel n CaseReturnInfo
toInfoLbl (CmmLabel m str CmmEntry)    = CmmLabel m str CmmInfo
toInfoLbl (CmmLabel m str CmmRet)      = CmmLabel m str CmmRetInfo
toInfoLbl l = pprPanic "CLabel.toInfoLbl" (ppr l)
573

nfrisby's avatar
nfrisby committed
574 575 576 577 578 579 580
toRednCountsLbl :: CLabel -> Maybe CLabel
toRednCountsLbl = fmap mkRednCountsLabel . hasHaskellName

hasHaskellName :: CLabel -> Maybe Name
hasHaskellName (IdLabel n _ _) = Just n
hasHaskellName _               = Nothing

581
-- -----------------------------------------------------------------------------
nfrisby's avatar
nfrisby committed
582
-- Does a CLabel's referent itself refer to a CAF?
583
hasCAF :: CLabel -> Bool
nfrisby's avatar
nfrisby committed
584
hasCAF (IdLabel _ _ RednCounts) = False -- Note [ticky for LNE]
585 586
hasCAF (IdLabel _ MayHaveCafRefs _) = True
hasCAF _                            = False
587

nfrisby's avatar
nfrisby committed
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
-- Note [ticky for LNE]
-- ~~~~~~~~~~~~~~~~~~~~~

-- Until 14 Feb 2013, every ticky counter was associated with a
-- closure. Thus, ticky labels used IdLabel. It is odd that
-- CmmBuildInfoTables.cafTransfers would consider such a ticky label
-- reason to add the name to the CAFEnv (and thus eventually the SRT),
-- but it was harmless because the ticky was only used if the closure
-- was also.
--
-- Since we now have ticky counters for LNEs, it is no longer the case
-- that every ticky counter has an actual closure. So I changed the
-- generation of ticky counters' CLabels to not result in their
-- associated id ending up in the SRT.
--
-- NB IdLabel is still appropriate for ticky ids (as opposed to
-- CmmLabel) because the LNE's counter is still related to an .hs Id,
-- that Id just isn't for a proper closure.
606

607 608
-- -----------------------------------------------------------------------------
-- Does a CLabel need declaring before use or not?
609 610
--
-- See wiki:Commentary/Compiler/Backends/PprC#Prototypes
611 612 613

needsCDecl :: CLabel -> Bool
  -- False <=> it's pre-declared; don't bother
614
  -- don't bother declaring Bitmap labels, we always make sure
615
  -- they are defined before use.
616
needsCDecl (SRTLabel _)                 = True
Ian Lynagh's avatar
Ian Lynagh committed
617 618 619 620
needsCDecl (LargeSRTLabel _)            = False
needsCDecl (LargeBitmapLabel _)         = False
needsCDecl (IdLabel _ _ _)              = True
needsCDecl (CaseLabel _ _)              = True
621
needsCDecl (PlainModuleInitLabel _)     = True
622

Ian Lynagh's avatar
Ian Lynagh committed
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
needsCDecl (StringLitLabel _)           = False
needsCDecl (AsmTempLabel _)             = False
needsCDecl (RtsLabel _)                 = False

needsCDecl (CmmLabel pkgId _ _)
        -- Prototypes for labels defined in the runtime system are imported
        --      into HC files via includes/Stg.h.
        | pkgId == rtsPackageId         = False

        -- For other labels we inline one into the HC file directly.
        | otherwise                     = True

needsCDecl l@(ForeignLabel{})           = not (isMathFun l)
needsCDecl (CC_Label _)                 = True
needsCDecl (CCS_Label _)                = True
andy@galois.com's avatar
andy@galois.com committed
638
needsCDecl (HpcTicksLabel _)            = True
Ian Lynagh's avatar
Ian Lynagh committed
639 640 641
needsCDecl (DynamicLinkerLabel {})      = panic "needsCDecl DynamicLinkerLabel"
needsCDecl PicBaseLabel                 = panic "needsCDecl PicBaseLabel"
needsCDecl (DeadStripPreventer {})      = panic "needsCDecl DeadStripPreventer"
642

643
-- | Check whether a label is a local temporary for native code generation
Ian Lynagh's avatar
Ian Lynagh committed
644 645 646
isAsmTemp  :: CLabel -> Bool
isAsmTemp (AsmTempLabel _)              = True
isAsmTemp _                             = False
647

648 649 650

-- | If a label is a local temporary used for native code generation
--      then return just its unique, otherwise nothing.
651
maybeAsmTemp :: CLabel -> Maybe Unique
Ian Lynagh's avatar
Ian Lynagh committed
652 653
maybeAsmTemp (AsmTempLabel uq)          = Just uq
maybeAsmTemp _                          = Nothing
654

655

Ian Lynagh's avatar
Ian Lynagh committed
656
-- | Check whether a label corresponds to a C function that has
657
--      a prototype in a system header somehere, or is built-in
658
--      to the C compiler. For these labels we avoid generating our
659
--      own C prototypes.
660
isMathFun :: CLabel -> Bool
Ian Lynagh's avatar
Ian Lynagh committed
661
isMathFun (ForeignLabel fs _ _ _)       = fs `elementOfUniqSet` math_funs
662 663
isMathFun _ = False

Ian Lynagh's avatar
Ian Lynagh committed
664
math_funs :: UniqSet FastString
665
math_funs = mkUniqSet [
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
        -- _ISOC99_SOURCE
        (fsLit "acos"),         (fsLit "acosf"),        (fsLit "acosh"),
        (fsLit "acoshf"),       (fsLit "acoshl"),       (fsLit "acosl"),
        (fsLit "asin"),         (fsLit "asinf"),        (fsLit "asinl"),
        (fsLit "asinh"),        (fsLit "asinhf"),       (fsLit "asinhl"),
        (fsLit "atan"),         (fsLit "atanf"),        (fsLit "atanl"),
        (fsLit "atan2"),        (fsLit "atan2f"),       (fsLit "atan2l"),
        (fsLit "atanh"),        (fsLit "atanhf"),       (fsLit "atanhl"),
        (fsLit "cbrt"),         (fsLit "cbrtf"),        (fsLit "cbrtl"),
        (fsLit "ceil"),         (fsLit "ceilf"),        (fsLit "ceill"),
        (fsLit "copysign"),     (fsLit "copysignf"),    (fsLit "copysignl"),
        (fsLit "cos"),          (fsLit "cosf"),         (fsLit "cosl"),
        (fsLit "cosh"),         (fsLit "coshf"),        (fsLit "coshl"),
        (fsLit "erf"),          (fsLit "erff"),         (fsLit "erfl"),
        (fsLit "erfc"),         (fsLit "erfcf"),        (fsLit "erfcl"),
        (fsLit "exp"),          (fsLit "expf"),         (fsLit "expl"),
        (fsLit "exp2"),         (fsLit "exp2f"),        (fsLit "exp2l"),
        (fsLit "expm1"),        (fsLit "expm1f"),       (fsLit "expm1l"),
        (fsLit "fabs"),         (fsLit "fabsf"),        (fsLit "fabsl"),
        (fsLit "fdim"),         (fsLit "fdimf"),        (fsLit "fdiml"),
        (fsLit "floor"),        (fsLit "floorf"),       (fsLit "floorl"),
        (fsLit "fma"),          (fsLit "fmaf"),         (fsLit "fmal"),
        (fsLit "fmax"),         (fsLit "fmaxf"),        (fsLit "fmaxl"),
        (fsLit "fmin"),         (fsLit "fminf"),        (fsLit "fminl"),
        (fsLit "fmod"),         (fsLit "fmodf"),        (fsLit "fmodl"),
        (fsLit "frexp"),        (fsLit "frexpf"),       (fsLit "frexpl"),
        (fsLit "hypot"),        (fsLit "hypotf"),       (fsLit "hypotl"),
        (fsLit "ilogb"),        (fsLit "ilogbf"),       (fsLit "ilogbl"),
        (fsLit "ldexp"),        (fsLit "ldexpf"),       (fsLit "ldexpl"),
        (fsLit "lgamma"),       (fsLit "lgammaf"),      (fsLit "lgammal"),
        (fsLit "llrint"),       (fsLit "llrintf"),      (fsLit "llrintl"),
        (fsLit "llround"),      (fsLit "llroundf"),     (fsLit "llroundl"),
        (fsLit "log"),          (fsLit "logf"),         (fsLit "logl"),
        (fsLit "log10l"),       (fsLit "log10"),        (fsLit "log10f"),
        (fsLit "log1pl"),       (fsLit "log1p"),        (fsLit "log1pf"),
        (fsLit "log2"),         (fsLit "log2f"),        (fsLit "log2l"),
        (fsLit "logb"),         (fsLit "logbf"),        (fsLit "logbl"),
        (fsLit "lrint"),        (fsLit "lrintf"),       (fsLit "lrintl"),
        (fsLit "lround"),       (fsLit "lroundf"),      (fsLit "lroundl"),
        (fsLit "modf"),         (fsLit "modff"),        (fsLit "modfl"),
        (fsLit "nan"),          (fsLit "nanf"),         (fsLit "nanl"),
        (fsLit "nearbyint"),    (fsLit "nearbyintf"),   (fsLit "nearbyintl"),
        (fsLit "nextafter"),    (fsLit "nextafterf"),   (fsLit "nextafterl"),
        (fsLit "nexttoward"),   (fsLit "nexttowardf"),  (fsLit "nexttowardl"),
        (fsLit "pow"),          (fsLit "powf"),         (fsLit "powl"),
        (fsLit "remainder"),    (fsLit "remainderf"),   (fsLit "remainderl"),
        (fsLit "remquo"),       (fsLit "remquof"),      (fsLit "remquol"),
        (fsLit "rint"),         (fsLit "rintf"),        (fsLit "rintl"),
        (fsLit "round"),        (fsLit "roundf"),       (fsLit "roundl"),
        (fsLit "scalbln"),      (fsLit "scalblnf"),     (fsLit "scalblnl"),
        (fsLit "scalbn"),       (fsLit "scalbnf"),      (fsLit "scalbnl"),
        (fsLit "sin"),          (fsLit "sinf"),         (fsLit "sinl"),
        (fsLit "sinh"),         (fsLit "sinhf"),        (fsLit "sinhl"),
        (fsLit "sqrt"),         (fsLit "sqrtf"),        (fsLit "sqrtl"),
        (fsLit "tan"),          (fsLit "tanf"),         (fsLit "tanl"),
        (fsLit "tanh"),         (fsLit "tanhf"),        (fsLit "tanhl"),
        (fsLit "tgamma"),       (fsLit "tgammaf"),      (fsLit "tgammal"),
        (fsLit "trunc"),        (fsLit "truncf"),       (fsLit "truncl"),
        -- ISO C 99 also defines these function-like macros in math.h:
        -- fpclassify, isfinite, isinf, isnormal, signbit, isgreater,
        -- isgreaterequal, isless, islessequal, islessgreater, isunordered

        -- additional symbols from _BSD_SOURCE
        (fsLit "drem"),         (fsLit "dremf"),        (fsLit "dreml"),
        (fsLit "finite"),       (fsLit "finitef"),      (fsLit "finitel"),
        (fsLit "gamma"),        (fsLit "gammaf"),       (fsLit "gammal"),
        (fsLit "isinf"),        (fsLit "isinff"),       (fsLit "isinfl"),
        (fsLit "isnan"),        (fsLit "isnanf"),       (fsLit "isnanl"),
        (fsLit "j0"),           (fsLit "j0f"),          (fsLit "j0l"),
        (fsLit "j1"),           (fsLit "j1f"),          (fsLit "j1l"),
        (fsLit "jn"),           (fsLit "jnf"),          (fsLit "jnl"),
        (fsLit "lgamma_r"),     (fsLit "lgammaf_r"),    (fsLit "lgammal_r"),
        (fsLit "scalb"),        (fsLit "scalbf"),       (fsLit "scalbl"),
        (fsLit "significand"),  (fsLit "significandf"), (fsLit "significandl"),
        (fsLit "y0"),           (fsLit "y0f"),          (fsLit "y0l"),
        (fsLit "y1"),           (fsLit "y1f"),          (fsLit "y1l"),
        (fsLit "yn"),           (fsLit "ynf"),          (fsLit "ynl")
743
    ]
744

745
-- -----------------------------------------------------------------------------
746
-- | Is a CLabel visible outside this object file or not?
Ian Lynagh's avatar
Ian Lynagh committed
747 748 749
--      From the point of view of the code generator, a name is
--      externally visible if it has to be declared as exported
--      in the .o file's symbol table; that is, made non-static.
750
externallyVisibleCLabel :: CLabel -> Bool -- not C "static"
Ian Lynagh's avatar
Ian Lynagh committed
751 752 753
externallyVisibleCLabel (CaseLabel _ _)         = False
externallyVisibleCLabel (StringLitLabel _)      = False
externallyVisibleCLabel (AsmTempLabel _)        = False
754
externallyVisibleCLabel (PlainModuleInitLabel _)= True
755
externallyVisibleCLabel (RtsLabel _)            = True
Ian Lynagh's avatar
Ian Lynagh committed
756 757 758 759 760
externallyVisibleCLabel (CmmLabel _ _ _)        = True
externallyVisibleCLabel (ForeignLabel{})        = True
externallyVisibleCLabel (IdLabel name _ info)   = isExternalName name && externallyVisibleIdLabel info
externallyVisibleCLabel (CC_Label _)            = True
externallyVisibleCLabel (CCS_Label _)           = True
761
externallyVisibleCLabel (DynamicLinkerLabel _ _)  = False
Ian Lynagh's avatar
Ian Lynagh committed
762
externallyVisibleCLabel (HpcTicksLabel _)       = True
763
externallyVisibleCLabel (LargeBitmapLabel _)    = False
764
externallyVisibleCLabel (SRTLabel _)            = False
Ian Lynagh's avatar
Ian Lynagh committed
765
externallyVisibleCLabel (LargeSRTLabel _)       = False
Ian Lynagh's avatar
Ian Lynagh committed
766 767
externallyVisibleCLabel (PicBaseLabel {}) = panic "externallyVisibleCLabel PicBaseLabel"
externallyVisibleCLabel (DeadStripPreventer {}) = panic "externallyVisibleCLabel DeadStripPreventer"
768

batterseapower's avatar
batterseapower committed
769
externallyVisibleIdLabel :: IdLabelInfo -> Bool
770
externallyVisibleIdLabel SRT             = False
771 772
externallyVisibleIdLabel LocalInfoTable  = False
externallyVisibleIdLabel LocalEntry      = False
773
externallyVisibleIdLabel _               = True
batterseapower's avatar
batterseapower committed
774

775
-- -----------------------------------------------------------------------------
Ian Lynagh's avatar
Ian Lynagh committed
776
-- Finding the "type" of a CLabel
777 778 779 780

-- For generating correct types in label declarations:

data CLabelType
Ian Lynagh's avatar
Ian Lynagh committed
781 782 783
  = CodeLabel   -- Address of some executable instructions
  | DataLabel   -- Address of data, not a GC ptr
  | GcPtrLabel  -- Address of a (presumably static) GC object
784 785 786

isCFunctionLabel :: CLabel -> Bool
isCFunctionLabel lbl = case labelType lbl of
Ian Lynagh's avatar
Ian Lynagh committed
787 788
                        CodeLabel -> True
                        _other    -> False
789 790 791

isGcPtrLabel :: CLabel -> Bool
isGcPtrLabel lbl = case labelType lbl of
Ian Lynagh's avatar
Ian Lynagh committed
792 793
                        GcPtrLabel -> True
                        _other     -> False
794

795 796 797

-- | Work out the general type of data at the address of this label
--    whether it be code, data, or static GC object.
798
labelType :: CLabel -> CLabelType
Ian Lynagh's avatar
Ian Lynagh committed
799
labelType (CmmLabel _ _ CmmData)                = DataLabel
800
labelType (CmmLabel _ _ CmmClosure)             = GcPtrLabel
Ian Lynagh's avatar
Ian Lynagh committed
801 802 803 804 805
labelType (CmmLabel _ _ CmmCode)                = CodeLabel
labelType (CmmLabel _ _ CmmInfo)                = DataLabel
labelType (CmmLabel _ _ CmmEntry)               = CodeLabel
labelType (CmmLabel _ _ CmmRetInfo)             = DataLabel
labelType (CmmLabel _ _ CmmRet)                 = CodeLabel
806 807
labelType (RtsLabel (RtsSelectorInfoTable _ _)) = DataLabel
labelType (RtsLabel (RtsApInfoTable _ _))       = DataLabel
808 809
labelType (RtsLabel (RtsApFast _))              = CodeLabel
labelType (CaseLabel _ CaseReturnInfo)          = DataLabel
Ian Lynagh's avatar
Ian Lynagh committed
810
labelType (CaseLabel _ _)                       = CodeLabel
811
labelType (PlainModuleInitLabel _)              = CodeLabel
812
labelType (SRTLabel _)                          = DataLabel
813 814
labelType (LargeSRTLabel _)                     = DataLabel
labelType (LargeBitmapLabel _)                  = DataLabel
Ian Lynagh's avatar
Ian Lynagh committed
815
labelType (ForeignLabel _ _ _ IsFunction)       = CodeLabel
816 817
labelType (IdLabel _ _ info)                    = idInfoLabelType info
labelType _                                     = DataLabel
818

Ian Lynagh's avatar
Ian Lynagh committed
819
idInfoLabelType :: IdLabelInfo -> CLabelType
820
idInfoLabelType info =
821
  case info of
822 823 824
    InfoTable     -> DataLabel
    LocalInfoTable -> DataLabel
    Closure       -> GcPtrLabel
825 826
    ConInfoTable  -> DataLabel
    StaticInfoTable -> DataLabel
827
    ClosureTable  -> DataLabel
828
    RednCounts    -> DataLabel
Ian Lynagh's avatar
Ian Lynagh committed
829
    _             -> CodeLabel
830 831 832 833 834 835 836 837 838 839


-- -----------------------------------------------------------------------------
-- Does a CLabel need dynamic linkage?

-- When referring to data in code, we need to know whether
-- that data resides in a DLL or not. [Win32 only.]
-- @labelDynamic@ returns @True@ if the label is located
-- in a DLL, be it a data reference or not.

840 841
labelDynamic :: DynFlags -> PackageId -> Module -> CLabel -> Bool
labelDynamic dflags this_pkg _this_mod lbl =
842
  case lbl of
843
   -- is the RTS in a DLL or not?
ian@well-typed.com's avatar
ian@well-typed.com committed
844
   RtsLabel _           -> not (gopt Opt_Static dflags) && (this_pkg /= rtsPackageId)
845

ian@well-typed.com's avatar
ian@well-typed.com committed
846
   IdLabel n _ _        -> isDllName dflags this_pkg n
847

daniel.is.fischer's avatar
daniel.is.fischer committed
848
   -- When compiling in the "dyn" way, each package is to be linked into
Ian Lynagh's avatar
Ian Lynagh committed
849
   -- its own shared library.
850
   CmmLabel pkg _ _
Ian Lynagh's avatar
Ian Lynagh committed
851
    | os == OSMinGW32 ->
ian@well-typed.com's avatar
ian@well-typed.com committed
852
       not (gopt Opt_Static dflags) && (this_pkg /= pkg)
Ian Lynagh's avatar
Ian Lynagh committed
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
    | otherwise ->
       True

   ForeignLabel _ _ source _  ->
       if os == OSMinGW32
       then case source of
            -- Foreign label is in some un-named foreign package (or DLL).
            ForeignLabelInExternalPackage -> True

            -- Foreign label is linked into the same package as the
            -- source file currently being compiled.
            ForeignLabelInThisPackage -> False

            -- Foreign label is in some named package.
            -- When compiling in the "dyn" way, each package is to be
            -- linked into its own DLL.
            ForeignLabelInPackage pkgId ->
ian@well-typed.com's avatar
ian@well-typed.com committed
870
                (not (gopt Opt_Static dflags)) && (this_pkg /= pkgId)
Ian Lynagh's avatar
Ian Lynagh committed
871 872 873 874 875

       else -- On Mac OS X and on ELF platforms, false positives are OK,
            -- so we claim that all foreign imports come from dynamic
            -- libraries
            True
876

ian@well-typed.com's avatar
ian@well-typed.com committed
877
   PlainModuleInitLabel m -> not (gopt Opt_Static dflags) && this_pkg /= (modulePackageId m)
878

879
   -- Note that DynamicLinkerLabels do NOT require dynamic linking themselves.
Ian Lynagh's avatar
Ian Lynagh committed
880
   _                 -> False
Ian Lynagh's avatar
Ian Lynagh committed
881
  where os = platformOS (targetPlatform dflags)
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903

{-
OLD?: These GRAN functions are needed for spitting out GRAN_FETCH() at the
right places. It is used to detect when the abstractC statement of an
CCodeBlock actually contains the code for a slow entry point.  -- HWL

We need at least @Eq@ for @CLabels@, because we want to avoid
duplicate declarations in generating C (see @labelSeenTE@ in
@PprAbsC@).
-}

-----------------------------------------------------------------------------
-- Printing out CLabels.

{-
Convention:

      <name>_<type>

where <name> is <Module>_<name> for external names and <unique> for
internal names. <type> is one of the following:

Ian Lynagh's avatar
Ian Lynagh committed
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
         info                   Info table
         srt                    Static reference table
         srtd                   Static reference table descriptor
         entry                  Entry code (function, closure)
         slow                   Slow entry code (if any)
         ret                    Direct return address
         vtbl                   Vector table
         <n>_alt                Case alternative (tag n)
         dflt                   Default case alternative
         btm                    Large bitmap vector
         closure                Static closure
         con_entry              Dynamic Constructor entry code
         con_info               Dynamic Constructor info table
         static_entry           Static Constructor entry code
         static_info            Static Constructor info table
         sel_info               Selector info table
         sel_entry              Selector entry code
         cc                     Cost centre
         ccs                    Cost centre stack