CLabel: Refactor pprDynamicLinkerAsmLabel
[ghc.git] / compiler / cmm / CLabel.hs
1 -----------------------------------------------------------------------------
2 --
3 -- Object-file symbols (called CLabel for histerical raisins).
4 --
5 -- (c) The University of Glasgow 2004-2006
6 --
7 -----------------------------------------------------------------------------
8
9 {-# LANGUAGE CPP #-}
10
11 module CLabel (
12 CLabel, -- abstract type
13 ForeignLabelSource(..),
14 pprDebugCLabel,
15
16 mkClosureLabel,
17 mkTopSRTLabel,
18 mkInfoTableLabel,
19 mkEntryLabel,
20 mkRednCountsLabel,
21 mkConInfoTableLabel,
22 mkLargeSRTLabel,
23 mkApEntryLabel,
24 mkApInfoTableLabel,
25 mkClosureTableLabel,
26 mkBytesLabel,
27
28 mkLocalBlockLabel,
29 mkLocalClosureLabel,
30 mkLocalInfoTableLabel,
31 mkLocalClosureTableLabel,
32
33 mkBlockInfoTableLabel,
34
35 mkBitmapLabel,
36 mkStringLitLabel,
37
38 mkAsmTempLabel,
39 mkAsmTempDerivedLabel,
40 mkAsmTempEndLabel,
41 mkAsmTempDieLabel,
42
43 mkSplitMarkerLabel,
44 mkDirty_MUT_VAR_Label,
45 mkUpdInfoLabel,
46 mkBHUpdInfoLabel,
47 mkIndStaticInfoLabel,
48 mkMainCapabilityLabel,
49 mkMAP_FROZEN_infoLabel,
50 mkMAP_FROZEN0_infoLabel,
51 mkMAP_DIRTY_infoLabel,
52 mkSMAP_FROZEN_infoLabel,
53 mkSMAP_FROZEN0_infoLabel,
54 mkSMAP_DIRTY_infoLabel,
55 mkBadAlignmentLabel,
56 mkArrWords_infoLabel,
57
58 mkTopTickyCtrLabel,
59 mkCAFBlackHoleInfoTableLabel,
60 mkRtsPrimOpLabel,
61 mkRtsSlowFastTickyCtrLabel,
62
63 mkSelectorInfoLabel,
64 mkSelectorEntryLabel,
65
66 mkCmmInfoLabel,
67 mkCmmEntryLabel,
68 mkCmmRetInfoLabel,
69 mkCmmRetLabel,
70 mkCmmCodeLabel,
71 mkCmmDataLabel,
72 mkCmmClosureLabel,
73
74 mkRtsApFastLabel,
75
76 mkPrimCallLabel,
77
78 mkForeignLabel,
79 addLabelSize,
80
81 foreignLabelStdcallInfo,
82 isBytesLabel,
83 isForeignLabel,
84 isSomeRODataLabel,
85 isStaticClosureLabel,
86 mkCCLabel, mkCCSLabel,
87
88 DynamicLinkerLabelInfo(..),
89 mkDynamicLinkerLabel,
90 dynamicLinkerLabelInfo,
91
92 mkPicBaseLabel,
93 mkDeadStripPreventer,
94
95 mkHpcTicksLabel,
96
97 hasCAF,
98 needsCDecl, maybeLocalBlockLabel, externallyVisibleCLabel,
99 isMathFun,
100 isCFunctionLabel, isGcPtrLabel, labelDynamic,
101
102 -- * Conversions
103 toClosureLbl, toSlowEntryLbl, toEntryLbl, toInfoLbl, hasHaskellName,
104
105 pprCLabel
106 ) where
107
108 #include "HsVersions.h"
109
110 import GhcPrelude
111
112 import IdInfo
113 import BasicTypes
114 import {-# SOURCE #-} BlockId (BlockId, mkBlockId)
115 import Packages
116 import Module
117 import Name
118 import Unique
119 import PrimOp
120 import Config
121 import CostCentre
122 import Outputable
123 import FastString
124 import DynFlags
125 import Platform
126 import UniqSet
127 import Util
128 import PprCore ( {- instances -} )
129
130 -- -----------------------------------------------------------------------------
131 -- The CLabel type
132
133 {-
134 | CLabel is an abstract type that supports the following operations:
135
136 - Pretty printing
137
138 - In a C file, does it need to be declared before use? (i.e. is it
139 guaranteed to be already in scope in the places we need to refer to it?)
140
141 - If it needs to be declared, what type (code or data) should it be
142 declared to have?
143
144 - Is it visible outside this object file or not?
145
146 - Is it "dynamic" (see details below)
147
148 - Eq and Ord, so that we can make sets of CLabels (currently only
149 used in outputting C as far as I can tell, to avoid generating
150 more than one declaration for any given label).
151
152 - Converting an info table label into an entry label.
153 -}
154
155 data CLabel
156 = -- | A label related to the definition of a particular Id or Con in a .hs file.
157 IdLabel
158 Name
159 CafInfo
160 IdLabelInfo -- encodes the suffix of the label
161
162 -- | A label from a .cmm file that is not associated with a .hs level Id.
163 | CmmLabel
164 UnitId -- what package the label belongs to.
165 FastString -- identifier giving the prefix of the label
166 CmmLabelInfo -- encodes the suffix of the label
167
168 -- | A label with a baked-in \/ algorithmically generated name that definitely
169 -- comes from the RTS. The code for it must compile into libHSrts.a \/ libHSrts.so
170 -- If it doesn't have an algorithmically generated name then use a CmmLabel
171 -- instead and give it an appropriate UnitId argument.
172 | RtsLabel
173 RtsLabelInfo
174
175 -- | A label associated with a block. These aren't visible outside of the
176 -- compilation unit in which they are defined. These are generally used to
177 -- name blocks produced by Cmm-to-Cmm passes and the native code generator,
178 -- where we don't have a 'Name' to associate the label to and therefore can't
179 -- use 'IdLabel'.
180 | LocalBlockLabel
181 {-# UNPACK #-} !Unique
182
183 -- | A 'C' (or otherwise foreign) label.
184 --
185 | ForeignLabel
186 FastString -- name of the imported label.
187
188 (Maybe Int) -- possible '@n' suffix for stdcall functions
189 -- When generating C, the '@n' suffix is omitted, but when
190 -- generating assembler we must add it to the label.
191
192 ForeignLabelSource -- what package the foreign label is in.
193
194 FunctionOrData
195
196 -- | Local temporary label used for native (or LLVM) code generation
197 | AsmTempLabel
198 {-# UNPACK #-} !Unique
199
200 | AsmTempDerivedLabel
201 CLabel
202 FastString -- suffix
203
204 | StringLitLabel
205 {-# UNPACK #-} !Unique
206
207 | CC_Label CostCentre
208 | CCS_Label CostCentreStack
209
210
211 -- | These labels are generated and used inside the NCG only.
212 -- They are special variants of a label used for dynamic linking
213 -- see module PositionIndependentCode for details.
214 | DynamicLinkerLabel DynamicLinkerLabelInfo CLabel
215
216 -- | This label is generated and used inside the NCG only.
217 -- It is used as a base for PIC calculations on some platforms.
218 -- It takes the form of a local numeric assembler label '1'; and
219 -- is pretty-printed as 1b, referring to the previous definition
220 -- of 1: in the assembler source file.
221 | PicBaseLabel
222
223 -- | A label before an info table to prevent excessive dead-stripping on darwin
224 | DeadStripPreventer CLabel
225
226
227 -- | Per-module table of tick locations
228 | HpcTicksLabel Module
229
230 -- | Static reference table
231 | SRTLabel !Unique
232
233 -- | Label of an StgLargeSRT
234 | LargeSRTLabel
235 {-# UNPACK #-} !Unique
236
237 -- | A bitmap (function or case return)
238 | LargeBitmapLabel
239 {-# UNPACK #-} !Unique
240
241 deriving Eq
242
243 -- This is laborious, but necessary. We can't derive Ord because
244 -- Unique doesn't have an Ord instance. Note nonDetCmpUnique in the
245 -- implementation. See Note [No Ord for Unique]
246 -- This is non-deterministic but we do not currently support deterministic
247 -- code-generation. See Note [Unique Determinism and code generation]
248 instance Ord CLabel where
249 compare (IdLabel a1 b1 c1) (IdLabel a2 b2 c2) =
250 compare a1 a2 `thenCmp`
251 compare b1 b2 `thenCmp`
252 compare c1 c2
253 compare (CmmLabel a1 b1 c1) (CmmLabel a2 b2 c2) =
254 compare a1 a2 `thenCmp`
255 compare b1 b2 `thenCmp`
256 compare c1 c2
257 compare (RtsLabel a1) (RtsLabel a2) = compare a1 a2
258 compare (LocalBlockLabel u1) (LocalBlockLabel u2) = nonDetCmpUnique u1 u2
259 compare (ForeignLabel a1 b1 c1 d1) (ForeignLabel a2 b2 c2 d2) =
260 compare a1 a2 `thenCmp`
261 compare b1 b2 `thenCmp`
262 compare c1 c2 `thenCmp`
263 compare d1 d2
264 compare (AsmTempLabel u1) (AsmTempLabel u2) = nonDetCmpUnique u1 u2
265 compare (AsmTempDerivedLabel a1 b1) (AsmTempDerivedLabel a2 b2) =
266 compare a1 a2 `thenCmp`
267 compare b1 b2
268 compare (StringLitLabel u1) (StringLitLabel u2) =
269 nonDetCmpUnique u1 u2
270 compare (CC_Label a1) (CC_Label a2) =
271 compare a1 a2
272 compare (CCS_Label a1) (CCS_Label a2) =
273 compare a1 a2
274 compare (DynamicLinkerLabel a1 b1) (DynamicLinkerLabel a2 b2) =
275 compare a1 a2 `thenCmp`
276 compare b1 b2
277 compare PicBaseLabel PicBaseLabel = EQ
278 compare (DeadStripPreventer a1) (DeadStripPreventer a2) =
279 compare a1 a2
280 compare (HpcTicksLabel a1) (HpcTicksLabel a2) =
281 compare a1 a2
282 compare (SRTLabel u1) (SRTLabel u2) =
283 nonDetCmpUnique u1 u2
284 compare (LargeSRTLabel u1) (LargeSRTLabel u2) =
285 nonDetCmpUnique u1 u2
286 compare (LargeBitmapLabel u1) (LargeBitmapLabel u2) =
287 nonDetCmpUnique u1 u2
288 compare IdLabel{} _ = LT
289 compare _ IdLabel{} = GT
290 compare CmmLabel{} _ = LT
291 compare _ CmmLabel{} = GT
292 compare RtsLabel{} _ = LT
293 compare _ RtsLabel{} = GT
294 compare LocalBlockLabel{} _ = LT
295 compare _ LocalBlockLabel{} = GT
296 compare ForeignLabel{} _ = LT
297 compare _ ForeignLabel{} = GT
298 compare AsmTempLabel{} _ = LT
299 compare _ AsmTempLabel{} = GT
300 compare AsmTempDerivedLabel{} _ = LT
301 compare _ AsmTempDerivedLabel{} = GT
302 compare StringLitLabel{} _ = LT
303 compare _ StringLitLabel{} = GT
304 compare CC_Label{} _ = LT
305 compare _ CC_Label{} = GT
306 compare CCS_Label{} _ = LT
307 compare _ CCS_Label{} = GT
308 compare DynamicLinkerLabel{} _ = LT
309 compare _ DynamicLinkerLabel{} = GT
310 compare PicBaseLabel{} _ = LT
311 compare _ PicBaseLabel{} = GT
312 compare DeadStripPreventer{} _ = LT
313 compare _ DeadStripPreventer{} = GT
314 compare HpcTicksLabel{} _ = LT
315 compare _ HpcTicksLabel{} = GT
316 compare SRTLabel{} _ = LT
317 compare _ SRTLabel{} = GT
318 compare LargeSRTLabel{} _ = LT
319 compare _ LargeSRTLabel{} = GT
320
321 -- | Record where a foreign label is stored.
322 data ForeignLabelSource
323
324 -- | Label is in a named package
325 = ForeignLabelInPackage UnitId
326
327 -- | Label is in some external, system package that doesn't also
328 -- contain compiled Haskell code, and is not associated with any .hi files.
329 -- We don't have to worry about Haskell code being inlined from
330 -- external packages. It is safe to treat the RTS package as "external".
331 | ForeignLabelInExternalPackage
332
333 -- | Label is in the package currenly being compiled.
334 -- This is only used for creating hacky tmp labels during code generation.
335 -- Don't use it in any code that might be inlined across a package boundary
336 -- (ie, core code) else the information will be wrong relative to the
337 -- destination module.
338 | ForeignLabelInThisPackage
339
340 deriving (Eq, Ord)
341
342
343 -- | For debugging problems with the CLabel representation.
344 -- We can't make a Show instance for CLabel because lots of its components don't have instances.
345 -- The regular Outputable instance only shows the label name, and not its other info.
346 --
347 pprDebugCLabel :: CLabel -> SDoc
348 pprDebugCLabel lbl
349 = case lbl of
350 IdLabel{} -> ppr lbl <> (parens $ text "IdLabel")
351 CmmLabel pkg _name _info
352 -> ppr lbl <> (parens $ text "CmmLabel" <+> ppr pkg)
353
354 RtsLabel{} -> ppr lbl <> (parens $ text "RtsLabel")
355
356 ForeignLabel _name mSuffix src funOrData
357 -> ppr lbl <> (parens $ text "ForeignLabel"
358 <+> ppr mSuffix
359 <+> ppr src
360 <+> ppr funOrData)
361
362 _ -> ppr lbl <> (parens $ text "other CLabel")
363
364
365 data IdLabelInfo
366 = Closure -- ^ Label for closure
367 | SRT -- ^ Static reference table (TODO: could be removed
368 -- with the old code generator, but might be needed
369 -- when we implement the New SRT Plan)
370 | InfoTable -- ^ Info tables for closures; always read-only
371 | Entry -- ^ Entry point
372 | Slow -- ^ Slow entry point
373
374 | LocalInfoTable -- ^ Like InfoTable but not externally visible
375 | LocalEntry -- ^ Like Entry but not externally visible
376
377 | RednCounts -- ^ Label of place to keep Ticky-ticky info for this Id
378
379 | ConEntry -- ^ Constructor entry point
380 | ConInfoTable -- ^ Corresponding info table
381
382 | ClosureTable -- ^ Table of closures for Enum tycons
383
384 | Bytes -- ^ Content of a string literal. See
385 -- Note [Bytes label].
386 | BlockInfoTable -- ^ Like LocalInfoTable but for a proc-point block
387 -- instead of a closure entry-point.
388 -- See Note [Proc-point local block entry-point].
389
390 deriving (Eq, Ord)
391
392
393 data RtsLabelInfo
394 = RtsSelectorInfoTable Bool{-updatable-} Int{-offset-} -- ^ Selector thunks
395 | RtsSelectorEntry Bool{-updatable-} Int{-offset-}
396
397 | RtsApInfoTable Bool{-updatable-} Int{-arity-} -- ^ AP thunks
398 | RtsApEntry Bool{-updatable-} Int{-arity-}
399
400 | RtsPrimOp PrimOp
401 | RtsApFast FastString -- ^ _fast versions of generic apply
402 | RtsSlowFastTickyCtr String
403
404 deriving (Eq, Ord)
405 -- NOTE: Eq on LitString compares the pointer only, so this isn't
406 -- a real equality.
407
408
409 -- | What type of Cmm label we're dealing with.
410 -- Determines the suffix appended to the name when a CLabel.CmmLabel
411 -- is pretty printed.
412 data CmmLabelInfo
413 = CmmInfo -- ^ misc rts info tables, suffix _info
414 | CmmEntry -- ^ misc rts entry points, suffix _entry
415 | CmmRetInfo -- ^ misc rts ret info tables, suffix _info
416 | CmmRet -- ^ misc rts return points, suffix _ret
417 | CmmData -- ^ misc rts data bits, eg CHARLIKE_closure
418 | CmmCode -- ^ misc rts code
419 | CmmClosure -- ^ closures eg CHARLIKE_closure
420 | CmmPrimCall -- ^ a prim call to some hand written Cmm code
421 deriving (Eq, Ord)
422
423 data DynamicLinkerLabelInfo
424 = CodeStub -- MachO: Lfoo$stub, ELF: foo@plt
425 | SymbolPtr -- MachO: Lfoo$non_lazy_ptr, Windows: __imp_foo
426 | GotSymbolPtr -- ELF: foo@got
427 | GotSymbolOffset -- ELF: foo@gotoff
428
429 deriving (Eq, Ord)
430
431
432 -- -----------------------------------------------------------------------------
433 -- Constructing CLabels
434 -- -----------------------------------------------------------------------------
435
436 -- Constructing IdLabels
437 -- These are always local:
438
439 mkTopSRTLabel :: Unique -> CLabel
440 mkTopSRTLabel u = SRTLabel u
441
442 mkRednCountsLabel :: Name -> CLabel
443 mkRednCountsLabel name =
444 IdLabel name NoCafRefs RednCounts -- Note [ticky for LNE]
445
446 -- These have local & (possibly) external variants:
447 mkLocalClosureLabel :: Name -> CafInfo -> CLabel
448 mkLocalInfoTableLabel :: Name -> CafInfo -> CLabel
449 mkLocalClosureTableLabel :: Name -> CafInfo -> CLabel
450 mkLocalClosureLabel name c = IdLabel name c Closure
451 mkLocalInfoTableLabel name c = IdLabel name c LocalInfoTable
452 mkLocalClosureTableLabel name c = IdLabel name c ClosureTable
453
454 mkClosureLabel :: Name -> CafInfo -> CLabel
455 mkInfoTableLabel :: Name -> CafInfo -> CLabel
456 mkEntryLabel :: Name -> CafInfo -> CLabel
457 mkClosureTableLabel :: Name -> CafInfo -> CLabel
458 mkConInfoTableLabel :: Name -> CafInfo -> CLabel
459 mkBytesLabel :: Name -> CLabel
460 mkClosureLabel name c = IdLabel name c Closure
461 mkInfoTableLabel name c = IdLabel name c InfoTable
462 mkEntryLabel name c = IdLabel name c Entry
463 mkClosureTableLabel name c = IdLabel name c ClosureTable
464 mkConInfoTableLabel name c = IdLabel name c ConInfoTable
465 mkBytesLabel name = IdLabel name NoCafRefs Bytes
466
467 mkBlockInfoTableLabel :: Name -> CafInfo -> CLabel
468 mkBlockInfoTableLabel name c = IdLabel name c BlockInfoTable
469 -- See Note [Proc-point local block entry-point].
470
471 -- Constructing Cmm Labels
472 mkDirty_MUT_VAR_Label, mkSplitMarkerLabel, mkUpdInfoLabel,
473 mkBHUpdInfoLabel, mkIndStaticInfoLabel, mkMainCapabilityLabel,
474 mkMAP_FROZEN_infoLabel, mkMAP_FROZEN0_infoLabel, mkMAP_DIRTY_infoLabel,
475 mkArrWords_infoLabel,
476 mkTopTickyCtrLabel,
477 mkCAFBlackHoleInfoTableLabel,
478 mkSMAP_FROZEN_infoLabel, mkSMAP_FROZEN0_infoLabel,
479 mkSMAP_DIRTY_infoLabel, mkBadAlignmentLabel :: CLabel
480 mkDirty_MUT_VAR_Label = mkForeignLabel (fsLit "dirty_MUT_VAR") Nothing ForeignLabelInExternalPackage IsFunction
481 mkSplitMarkerLabel = CmmLabel rtsUnitId (fsLit "__stg_split_marker") CmmCode
482 mkUpdInfoLabel = CmmLabel rtsUnitId (fsLit "stg_upd_frame") CmmInfo
483 mkBHUpdInfoLabel = CmmLabel rtsUnitId (fsLit "stg_bh_upd_frame" ) CmmInfo
484 mkIndStaticInfoLabel = CmmLabel rtsUnitId (fsLit "stg_IND_STATIC") CmmInfo
485 mkMainCapabilityLabel = CmmLabel rtsUnitId (fsLit "MainCapability") CmmData
486 mkMAP_FROZEN_infoLabel = CmmLabel rtsUnitId (fsLit "stg_MUT_ARR_PTRS_FROZEN") CmmInfo
487 mkMAP_FROZEN0_infoLabel = CmmLabel rtsUnitId (fsLit "stg_MUT_ARR_PTRS_FROZEN0") CmmInfo
488 mkMAP_DIRTY_infoLabel = CmmLabel rtsUnitId (fsLit "stg_MUT_ARR_PTRS_DIRTY") CmmInfo
489 mkTopTickyCtrLabel = CmmLabel rtsUnitId (fsLit "top_ct") CmmData
490 mkCAFBlackHoleInfoTableLabel = CmmLabel rtsUnitId (fsLit "stg_CAF_BLACKHOLE") CmmInfo
491 mkArrWords_infoLabel = CmmLabel rtsUnitId (fsLit "stg_ARR_WORDS") CmmInfo
492 mkSMAP_FROZEN_infoLabel = CmmLabel rtsUnitId (fsLit "stg_SMALL_MUT_ARR_PTRS_FROZEN") CmmInfo
493 mkSMAP_FROZEN0_infoLabel = CmmLabel rtsUnitId (fsLit "stg_SMALL_MUT_ARR_PTRS_FROZEN0") CmmInfo
494 mkSMAP_DIRTY_infoLabel = CmmLabel rtsUnitId (fsLit "stg_SMALL_MUT_ARR_PTRS_DIRTY") CmmInfo
495 mkBadAlignmentLabel = CmmLabel rtsUnitId (fsLit "stg_badAlignment") CmmEntry
496
497 -----
498 mkCmmInfoLabel, mkCmmEntryLabel, mkCmmRetInfoLabel, mkCmmRetLabel,
499 mkCmmCodeLabel, mkCmmDataLabel, mkCmmClosureLabel
500 :: UnitId -> FastString -> CLabel
501
502 mkCmmInfoLabel pkg str = CmmLabel pkg str CmmInfo
503 mkCmmEntryLabel pkg str = CmmLabel pkg str CmmEntry
504 mkCmmRetInfoLabel pkg str = CmmLabel pkg str CmmRetInfo
505 mkCmmRetLabel pkg str = CmmLabel pkg str CmmRet
506 mkCmmCodeLabel pkg str = CmmLabel pkg str CmmCode
507 mkCmmDataLabel pkg str = CmmLabel pkg str CmmData
508 mkCmmClosureLabel pkg str = CmmLabel pkg str CmmClosure
509
510 mkLocalBlockLabel :: Unique -> CLabel
511 mkLocalBlockLabel u = LocalBlockLabel u
512
513 -- Constructing RtsLabels
514 mkRtsPrimOpLabel :: PrimOp -> CLabel
515 mkRtsPrimOpLabel primop = RtsLabel (RtsPrimOp primop)
516
517 mkSelectorInfoLabel :: Bool -> Int -> CLabel
518 mkSelectorEntryLabel :: Bool -> Int -> CLabel
519 mkSelectorInfoLabel upd off = RtsLabel (RtsSelectorInfoTable upd off)
520 mkSelectorEntryLabel upd off = RtsLabel (RtsSelectorEntry upd off)
521
522 mkApInfoTableLabel :: Bool -> Int -> CLabel
523 mkApEntryLabel :: Bool -> Int -> CLabel
524 mkApInfoTableLabel upd off = RtsLabel (RtsApInfoTable upd off)
525 mkApEntryLabel upd off = RtsLabel (RtsApEntry upd off)
526
527
528 -- A call to some primitive hand written Cmm code
529 mkPrimCallLabel :: PrimCall -> CLabel
530 mkPrimCallLabel (PrimCall str pkg)
531 = CmmLabel pkg str CmmPrimCall
532
533
534 -- Constructing ForeignLabels
535
536 -- | Make a foreign label
537 mkForeignLabel
538 :: FastString -- name
539 -> Maybe Int -- size prefix
540 -> ForeignLabelSource -- what package it's in
541 -> FunctionOrData
542 -> CLabel
543
544 mkForeignLabel str mb_sz src fod
545 = ForeignLabel str mb_sz src fod
546
547
548 -- | Update the label size field in a ForeignLabel
549 addLabelSize :: CLabel -> Int -> CLabel
550 addLabelSize (ForeignLabel str _ src fod) sz
551 = ForeignLabel str (Just sz) src fod
552 addLabelSize label _
553 = label
554
555 -- | Whether label is a top-level string literal
556 isBytesLabel :: CLabel -> Bool
557 isBytesLabel (IdLabel _ _ Bytes) = True
558 isBytesLabel _lbl = False
559
560 -- | Whether label is a non-haskell label (defined in C code)
561 isForeignLabel :: CLabel -> Bool
562 isForeignLabel (ForeignLabel _ _ _ _) = True
563 isForeignLabel _lbl = False
564
565 -- | Whether label is a static closure label (can come from haskell or cmm)
566 isStaticClosureLabel :: CLabel -> Bool
567 -- Closure defined in haskell (.hs)
568 isStaticClosureLabel (IdLabel _ _ Closure) = True
569 -- Closure defined in cmm
570 isStaticClosureLabel (CmmLabel _ _ CmmClosure) = True
571 isStaticClosureLabel _lbl = False
572
573 -- | Whether label is a .rodata label
574 isSomeRODataLabel :: CLabel -> Bool
575 -- info table defined in haskell (.hs)
576 isSomeRODataLabel (IdLabel _ _ ClosureTable) = True
577 isSomeRODataLabel (IdLabel _ _ ConInfoTable) = True
578 isSomeRODataLabel (IdLabel _ _ InfoTable) = True
579 isSomeRODataLabel (IdLabel _ _ LocalInfoTable) = True
580 -- static reference tables defined in haskell (.hs)
581 isSomeRODataLabel (IdLabel _ _ SRT) = True
582 isSomeRODataLabel (SRTLabel _) = True
583 -- info table defined in cmm (.cmm)
584 isSomeRODataLabel (CmmLabel _ _ CmmInfo) = True
585 isSomeRODataLabel _lbl = False
586
587 -- | Get the label size field from a ForeignLabel
588 foreignLabelStdcallInfo :: CLabel -> Maybe Int
589 foreignLabelStdcallInfo (ForeignLabel _ info _ _) = info
590 foreignLabelStdcallInfo _lbl = Nothing
591
592
593 -- Constructing Large*Labels
594 mkLargeSRTLabel :: Unique -> CLabel
595 mkBitmapLabel :: Unique -> CLabel
596 mkLargeSRTLabel uniq = LargeSRTLabel uniq
597 mkBitmapLabel uniq = LargeBitmapLabel uniq
598
599 -- Constructing Cost Center Labels
600 mkCCLabel :: CostCentre -> CLabel
601 mkCCSLabel :: CostCentreStack -> CLabel
602 mkCCLabel cc = CC_Label cc
603 mkCCSLabel ccs = CCS_Label ccs
604
605 mkRtsApFastLabel :: FastString -> CLabel
606 mkRtsApFastLabel str = RtsLabel (RtsApFast str)
607
608 mkRtsSlowFastTickyCtrLabel :: String -> CLabel
609 mkRtsSlowFastTickyCtrLabel pat = RtsLabel (RtsSlowFastTickyCtr pat)
610
611
612 -- Constructing Code Coverage Labels
613 mkHpcTicksLabel :: Module -> CLabel
614 mkHpcTicksLabel = HpcTicksLabel
615
616
617 -- Constructing labels used for dynamic linking
618 mkDynamicLinkerLabel :: DynamicLinkerLabelInfo -> CLabel -> CLabel
619 mkDynamicLinkerLabel = DynamicLinkerLabel
620
621 dynamicLinkerLabelInfo :: CLabel -> Maybe (DynamicLinkerLabelInfo, CLabel)
622 dynamicLinkerLabelInfo (DynamicLinkerLabel info lbl) = Just (info, lbl)
623 dynamicLinkerLabelInfo _ = Nothing
624
625 mkPicBaseLabel :: CLabel
626 mkPicBaseLabel = PicBaseLabel
627
628
629 -- Constructing miscellaneous other labels
630 mkDeadStripPreventer :: CLabel -> CLabel
631 mkDeadStripPreventer lbl = DeadStripPreventer lbl
632
633 mkStringLitLabel :: Unique -> CLabel
634 mkStringLitLabel = StringLitLabel
635
636 mkAsmTempLabel :: Uniquable a => a -> CLabel
637 mkAsmTempLabel a = AsmTempLabel (getUnique a)
638
639 mkAsmTempDerivedLabel :: CLabel -> FastString -> CLabel
640 mkAsmTempDerivedLabel = AsmTempDerivedLabel
641
642 mkAsmTempEndLabel :: CLabel -> CLabel
643 mkAsmTempEndLabel l = mkAsmTempDerivedLabel l (fsLit "_end")
644
645 -- | Construct a label for a DWARF Debug Information Entity (DIE)
646 -- describing another symbol.
647 mkAsmTempDieLabel :: CLabel -> CLabel
648 mkAsmTempDieLabel l = mkAsmTempDerivedLabel l (fsLit "_die")
649
650 -- -----------------------------------------------------------------------------
651 -- Convert between different kinds of label
652
653 toClosureLbl :: CLabel -> CLabel
654 toClosureLbl (IdLabel n _ BlockInfoTable)
655 = pprPanic "toClosureLbl: BlockInfoTable" (ppr n)
656 toClosureLbl (IdLabel n c _) = IdLabel n c Closure
657 toClosureLbl (CmmLabel m str _) = CmmLabel m str CmmClosure
658 toClosureLbl l = pprPanic "toClosureLbl" (ppr l)
659
660 toSlowEntryLbl :: CLabel -> CLabel
661 toSlowEntryLbl (IdLabel n _ BlockInfoTable)
662 = pprPanic "toSlowEntryLbl" (ppr n)
663 toSlowEntryLbl (IdLabel n c _) = IdLabel n c Slow
664 toSlowEntryLbl l = pprPanic "toSlowEntryLbl" (ppr l)
665
666 toEntryLbl :: CLabel -> CLabel
667 toEntryLbl (IdLabel n c LocalInfoTable) = IdLabel n c LocalEntry
668 toEntryLbl (IdLabel n c ConInfoTable) = IdLabel n c ConEntry
669 toEntryLbl (IdLabel n _ BlockInfoTable) = mkLocalBlockLabel (nameUnique n)
670 -- See Note [Proc-point local block entry-point].
671 toEntryLbl (IdLabel n c _) = IdLabel n c Entry
672 toEntryLbl (CmmLabel m str CmmInfo) = CmmLabel m str CmmEntry
673 toEntryLbl (CmmLabel m str CmmRetInfo) = CmmLabel m str CmmRet
674 toEntryLbl l = pprPanic "toEntryLbl" (ppr l)
675
676 toInfoLbl :: CLabel -> CLabel
677 toInfoLbl (IdLabel n c LocalEntry) = IdLabel n c LocalInfoTable
678 toInfoLbl (IdLabel n c ConEntry) = IdLabel n c ConInfoTable
679 toInfoLbl (IdLabel n c _) = IdLabel n c InfoTable
680 toInfoLbl (CmmLabel m str CmmEntry) = CmmLabel m str CmmInfo
681 toInfoLbl (CmmLabel m str CmmRet) = CmmLabel m str CmmRetInfo
682 toInfoLbl l = pprPanic "CLabel.toInfoLbl" (ppr l)
683
684 hasHaskellName :: CLabel -> Maybe Name
685 hasHaskellName (IdLabel n _ _) = Just n
686 hasHaskellName _ = Nothing
687
688 -- -----------------------------------------------------------------------------
689 -- Does a CLabel's referent itself refer to a CAF?
690 hasCAF :: CLabel -> Bool
691 hasCAF (IdLabel _ _ RednCounts) = False -- Note [ticky for LNE]
692 hasCAF (IdLabel _ MayHaveCafRefs _) = True
693 hasCAF _ = False
694
695 -- Note [ticky for LNE]
696 -- ~~~~~~~~~~~~~~~~~~~~~
697
698 -- Until 14 Feb 2013, every ticky counter was associated with a
699 -- closure. Thus, ticky labels used IdLabel. It is odd that
700 -- CmmBuildInfoTables.cafTransfers would consider such a ticky label
701 -- reason to add the name to the CAFEnv (and thus eventually the SRT),
702 -- but it was harmless because the ticky was only used if the closure
703 -- was also.
704 --
705 -- Since we now have ticky counters for LNEs, it is no longer the case
706 -- that every ticky counter has an actual closure. So I changed the
707 -- generation of ticky counters' CLabels to not result in their
708 -- associated id ending up in the SRT.
709 --
710 -- NB IdLabel is still appropriate for ticky ids (as opposed to
711 -- CmmLabel) because the LNE's counter is still related to an .hs Id,
712 -- that Id just isn't for a proper closure.
713
714 -- -----------------------------------------------------------------------------
715 -- Does a CLabel need declaring before use or not?
716 --
717 -- See wiki:Commentary/Compiler/Backends/PprC#Prototypes
718
719 needsCDecl :: CLabel -> Bool
720 -- False <=> it's pre-declared; don't bother
721 -- don't bother declaring Bitmap labels, we always make sure
722 -- they are defined before use.
723 needsCDecl (SRTLabel _) = True
724 needsCDecl (LargeSRTLabel _) = False
725 needsCDecl (LargeBitmapLabel _) = False
726 needsCDecl (IdLabel _ _ _) = True
727 needsCDecl (LocalBlockLabel _) = True
728
729 needsCDecl (StringLitLabel _) = False
730 needsCDecl (AsmTempLabel _) = False
731 needsCDecl (AsmTempDerivedLabel _ _) = False
732 needsCDecl (RtsLabel _) = False
733
734 needsCDecl (CmmLabel pkgId _ _)
735 -- Prototypes for labels defined in the runtime system are imported
736 -- into HC files via includes/Stg.h.
737 | pkgId == rtsUnitId = False
738
739 -- For other labels we inline one into the HC file directly.
740 | otherwise = True
741
742 needsCDecl l@(ForeignLabel{}) = not (isMathFun l)
743 needsCDecl (CC_Label _) = True
744 needsCDecl (CCS_Label _) = True
745 needsCDecl (HpcTicksLabel _) = True
746 needsCDecl (DynamicLinkerLabel {}) = panic "needsCDecl DynamicLinkerLabel"
747 needsCDecl PicBaseLabel = panic "needsCDecl PicBaseLabel"
748 needsCDecl (DeadStripPreventer {}) = panic "needsCDecl DeadStripPreventer"
749
750 -- | If a label is a local block label then return just its 'BlockId', otherwise
751 -- 'Nothing'.
752 maybeLocalBlockLabel :: CLabel -> Maybe BlockId
753 maybeLocalBlockLabel (LocalBlockLabel uq) = Just $ mkBlockId uq
754 maybeLocalBlockLabel _ = Nothing
755
756
757 -- | Check whether a label corresponds to a C function that has
758 -- a prototype in a system header somehere, or is built-in
759 -- to the C compiler. For these labels we avoid generating our
760 -- own C prototypes.
761 isMathFun :: CLabel -> Bool
762 isMathFun (ForeignLabel fs _ _ _) = fs `elementOfUniqSet` math_funs
763 isMathFun _ = False
764
765 math_funs :: UniqSet FastString
766 math_funs = mkUniqSet [
767 -- _ISOC99_SOURCE
768 (fsLit "acos"), (fsLit "acosf"), (fsLit "acosh"),
769 (fsLit "acoshf"), (fsLit "acoshl"), (fsLit "acosl"),
770 (fsLit "asin"), (fsLit "asinf"), (fsLit "asinl"),
771 (fsLit "asinh"), (fsLit "asinhf"), (fsLit "asinhl"),
772 (fsLit "atan"), (fsLit "atanf"), (fsLit "atanl"),
773 (fsLit "atan2"), (fsLit "atan2f"), (fsLit "atan2l"),
774 (fsLit "atanh"), (fsLit "atanhf"), (fsLit "atanhl"),
775 (fsLit "cbrt"), (fsLit "cbrtf"), (fsLit "cbrtl"),
776 (fsLit "ceil"), (fsLit "ceilf"), (fsLit "ceill"),
777 (fsLit "copysign"), (fsLit "copysignf"), (fsLit "copysignl"),
778 (fsLit "cos"), (fsLit "cosf"), (fsLit "cosl"),
779 (fsLit "cosh"), (fsLit "coshf"), (fsLit "coshl"),
780 (fsLit "erf"), (fsLit "erff"), (fsLit "erfl"),
781 (fsLit "erfc"), (fsLit "erfcf"), (fsLit "erfcl"),
782 (fsLit "exp"), (fsLit "expf"), (fsLit "expl"),
783 (fsLit "exp2"), (fsLit "exp2f"), (fsLit "exp2l"),
784 (fsLit "expm1"), (fsLit "expm1f"), (fsLit "expm1l"),
785 (fsLit "fabs"), (fsLit "fabsf"), (fsLit "fabsl"),
786 (fsLit "fdim"), (fsLit "fdimf"), (fsLit "fdiml"),
787 (fsLit "floor"), (fsLit "floorf"), (fsLit "floorl"),
788 (fsLit "fma"), (fsLit "fmaf"), (fsLit "fmal"),
789 (fsLit "fmax"), (fsLit "fmaxf"), (fsLit "fmaxl"),
790 (fsLit "fmin"), (fsLit "fminf"), (fsLit "fminl"),
791 (fsLit "fmod"), (fsLit "fmodf"), (fsLit "fmodl"),
792 (fsLit "frexp"), (fsLit "frexpf"), (fsLit "frexpl"),
793 (fsLit "hypot"), (fsLit "hypotf"), (fsLit "hypotl"),
794 (fsLit "ilogb"), (fsLit "ilogbf"), (fsLit "ilogbl"),
795 (fsLit "ldexp"), (fsLit "ldexpf"), (fsLit "ldexpl"),
796 (fsLit "lgamma"), (fsLit "lgammaf"), (fsLit "lgammal"),
797 (fsLit "llrint"), (fsLit "llrintf"), (fsLit "llrintl"),
798 (fsLit "llround"), (fsLit "llroundf"), (fsLit "llroundl"),
799 (fsLit "log"), (fsLit "logf"), (fsLit "logl"),
800 (fsLit "log10l"), (fsLit "log10"), (fsLit "log10f"),
801 (fsLit "log1pl"), (fsLit "log1p"), (fsLit "log1pf"),
802 (fsLit "log2"), (fsLit "log2f"), (fsLit "log2l"),
803 (fsLit "logb"), (fsLit "logbf"), (fsLit "logbl"),
804 (fsLit "lrint"), (fsLit "lrintf"), (fsLit "lrintl"),
805 (fsLit "lround"), (fsLit "lroundf"), (fsLit "lroundl"),
806 (fsLit "modf"), (fsLit "modff"), (fsLit "modfl"),
807 (fsLit "nan"), (fsLit "nanf"), (fsLit "nanl"),
808 (fsLit "nearbyint"), (fsLit "nearbyintf"), (fsLit "nearbyintl"),
809 (fsLit "nextafter"), (fsLit "nextafterf"), (fsLit "nextafterl"),
810 (fsLit "nexttoward"), (fsLit "nexttowardf"), (fsLit "nexttowardl"),
811 (fsLit "pow"), (fsLit "powf"), (fsLit "powl"),
812 (fsLit "remainder"), (fsLit "remainderf"), (fsLit "remainderl"),
813 (fsLit "remquo"), (fsLit "remquof"), (fsLit "remquol"),
814 (fsLit "rint"), (fsLit "rintf"), (fsLit "rintl"),
815 (fsLit "round"), (fsLit "roundf"), (fsLit "roundl"),
816 (fsLit "scalbln"), (fsLit "scalblnf"), (fsLit "scalblnl"),
817 (fsLit "scalbn"), (fsLit "scalbnf"), (fsLit "scalbnl"),
818 (fsLit "sin"), (fsLit "sinf"), (fsLit "sinl"),
819 (fsLit "sinh"), (fsLit "sinhf"), (fsLit "sinhl"),
820 (fsLit "sqrt"), (fsLit "sqrtf"), (fsLit "sqrtl"),
821 (fsLit "tan"), (fsLit "tanf"), (fsLit "tanl"),
822 (fsLit "tanh"), (fsLit "tanhf"), (fsLit "tanhl"),
823 (fsLit "tgamma"), (fsLit "tgammaf"), (fsLit "tgammal"),
824 (fsLit "trunc"), (fsLit "truncf"), (fsLit "truncl"),
825 -- ISO C 99 also defines these function-like macros in math.h:
826 -- fpclassify, isfinite, isinf, isnormal, signbit, isgreater,
827 -- isgreaterequal, isless, islessequal, islessgreater, isunordered
828
829 -- additional symbols from _BSD_SOURCE
830 (fsLit "drem"), (fsLit "dremf"), (fsLit "dreml"),
831 (fsLit "finite"), (fsLit "finitef"), (fsLit "finitel"),
832 (fsLit "gamma"), (fsLit "gammaf"), (fsLit "gammal"),
833 (fsLit "isinf"), (fsLit "isinff"), (fsLit "isinfl"),
834 (fsLit "isnan"), (fsLit "isnanf"), (fsLit "isnanl"),
835 (fsLit "j0"), (fsLit "j0f"), (fsLit "j0l"),
836 (fsLit "j1"), (fsLit "j1f"), (fsLit "j1l"),
837 (fsLit "jn"), (fsLit "jnf"), (fsLit "jnl"),
838 (fsLit "lgamma_r"), (fsLit "lgammaf_r"), (fsLit "lgammal_r"),
839 (fsLit "scalb"), (fsLit "scalbf"), (fsLit "scalbl"),
840 (fsLit "significand"), (fsLit "significandf"), (fsLit "significandl"),
841 (fsLit "y0"), (fsLit "y0f"), (fsLit "y0l"),
842 (fsLit "y1"), (fsLit "y1f"), (fsLit "y1l"),
843 (fsLit "yn"), (fsLit "ynf"), (fsLit "ynl"),
844
845 -- These functions are described in IEEE Std 754-2008 -
846 -- Standard for Floating-Point Arithmetic and ISO/IEC TS 18661
847 (fsLit "nextup"), (fsLit "nextupf"), (fsLit "nextupl"),
848 (fsLit "nextdown"), (fsLit "nextdownf"), (fsLit "nextdownl")
849 ]
850
851 -- -----------------------------------------------------------------------------
852 -- | Is a CLabel visible outside this object file or not?
853 -- From the point of view of the code generator, a name is
854 -- externally visible if it has to be declared as exported
855 -- in the .o file's symbol table; that is, made non-static.
856 externallyVisibleCLabel :: CLabel -> Bool -- not C "static"
857 externallyVisibleCLabel (StringLitLabel _) = False
858 externallyVisibleCLabel (AsmTempLabel _) = False
859 externallyVisibleCLabel (AsmTempDerivedLabel _ _)= False
860 externallyVisibleCLabel (RtsLabel _) = True
861 externallyVisibleCLabel (LocalBlockLabel _) = False
862 externallyVisibleCLabel (CmmLabel _ _ _) = True
863 externallyVisibleCLabel (ForeignLabel{}) = True
864 externallyVisibleCLabel (IdLabel name _ info) = isExternalName name && externallyVisibleIdLabel info
865 externallyVisibleCLabel (CC_Label _) = True
866 externallyVisibleCLabel (CCS_Label _) = True
867 externallyVisibleCLabel (DynamicLinkerLabel _ _) = False
868 externallyVisibleCLabel (HpcTicksLabel _) = True
869 externallyVisibleCLabel (LargeBitmapLabel _) = False
870 externallyVisibleCLabel (SRTLabel _) = False
871 externallyVisibleCLabel (LargeSRTLabel _) = False
872 externallyVisibleCLabel (PicBaseLabel {}) = panic "externallyVisibleCLabel PicBaseLabel"
873 externallyVisibleCLabel (DeadStripPreventer {}) = panic "externallyVisibleCLabel DeadStripPreventer"
874
875 externallyVisibleIdLabel :: IdLabelInfo -> Bool
876 externallyVisibleIdLabel SRT = False
877 externallyVisibleIdLabel LocalInfoTable = False
878 externallyVisibleIdLabel LocalEntry = False
879 externallyVisibleIdLabel BlockInfoTable = False
880 externallyVisibleIdLabel _ = True
881
882 -- -----------------------------------------------------------------------------
883 -- Finding the "type" of a CLabel
884
885 -- For generating correct types in label declarations:
886
887 data CLabelType
888 = CodeLabel -- Address of some executable instructions
889 | DataLabel -- Address of data, not a GC ptr
890 | GcPtrLabel -- Address of a (presumably static) GC object
891
892 isCFunctionLabel :: CLabel -> Bool
893 isCFunctionLabel lbl = case labelType lbl of
894 CodeLabel -> True
895 _other -> False
896
897 isGcPtrLabel :: CLabel -> Bool
898 isGcPtrLabel lbl = case labelType lbl of
899 GcPtrLabel -> True
900 _other -> False
901
902
903 -- | Work out the general type of data at the address of this label
904 -- whether it be code, data, or static GC object.
905 labelType :: CLabel -> CLabelType
906 labelType (IdLabel _ _ info) = idInfoLabelType info
907 labelType (CmmLabel _ _ CmmData) = DataLabel
908 labelType (CmmLabel _ _ CmmClosure) = GcPtrLabel
909 labelType (CmmLabel _ _ CmmCode) = CodeLabel
910 labelType (CmmLabel _ _ CmmInfo) = DataLabel
911 labelType (CmmLabel _ _ CmmEntry) = CodeLabel
912 labelType (CmmLabel _ _ CmmPrimCall) = CodeLabel
913 labelType (CmmLabel _ _ CmmRetInfo) = DataLabel
914 labelType (CmmLabel _ _ CmmRet) = CodeLabel
915 labelType (RtsLabel (RtsSelectorInfoTable _ _)) = DataLabel
916 labelType (RtsLabel (RtsApInfoTable _ _)) = DataLabel
917 labelType (RtsLabel (RtsApFast _)) = CodeLabel
918 labelType (RtsLabel _) = DataLabel
919 labelType (LocalBlockLabel _) = CodeLabel
920 labelType (SRTLabel _) = DataLabel
921 labelType (ForeignLabel _ _ _ IsFunction) = CodeLabel
922 labelType (ForeignLabel _ _ _ IsData) = DataLabel
923 labelType (AsmTempLabel _) = panic "labelType(AsmTempLabel)"
924 labelType (AsmTempDerivedLabel _ _) = panic "labelType(AsmTempDerivedLabel)"
925 labelType (StringLitLabel _) = DataLabel
926 labelType (CC_Label _) = DataLabel
927 labelType (CCS_Label _) = DataLabel
928 labelType (DynamicLinkerLabel _ _) = DataLabel -- Is this right?
929 labelType PicBaseLabel = DataLabel
930 labelType (DeadStripPreventer _) = DataLabel
931 labelType (HpcTicksLabel _) = DataLabel
932 labelType (LargeSRTLabel _) = DataLabel
933 labelType (LargeBitmapLabel _) = DataLabel
934
935 idInfoLabelType :: IdLabelInfo -> CLabelType
936 idInfoLabelType info =
937 case info of
938 InfoTable -> DataLabel
939 LocalInfoTable -> DataLabel
940 Closure -> GcPtrLabel
941 ConInfoTable -> DataLabel
942 ClosureTable -> DataLabel
943 RednCounts -> DataLabel
944 Bytes -> DataLabel
945 _ -> CodeLabel
946
947
948 -- -----------------------------------------------------------------------------
949 -- Does a CLabel need dynamic linkage?
950
951 -- When referring to data in code, we need to know whether
952 -- that data resides in a DLL or not. [Win32 only.]
953 -- @labelDynamic@ returns @True@ if the label is located
954 -- in a DLL, be it a data reference or not.
955
956 labelDynamic :: DynFlags -> Module -> CLabel -> Bool
957 labelDynamic dflags this_mod lbl =
958 case lbl of
959 -- is the RTS in a DLL or not?
960 RtsLabel _ -> (WayDyn `elem` ways dflags) && (this_pkg /= rtsUnitId)
961
962 IdLabel n _ _ -> isDllName dflags this_mod n
963
964 -- When compiling in the "dyn" way, each package is to be linked into
965 -- its own shared library.
966 CmmLabel pkg _ _
967 | os == OSMinGW32 ->
968 (WayDyn `elem` ways dflags) && (this_pkg /= pkg)
969 | otherwise ->
970 True
971
972 LocalBlockLabel _ -> False
973
974 ForeignLabel _ _ source _ ->
975 if os == OSMinGW32
976 then case source of
977 -- Foreign label is in some un-named foreign package (or DLL).
978 ForeignLabelInExternalPackage -> True
979
980 -- Foreign label is linked into the same package as the
981 -- source file currently being compiled.
982 ForeignLabelInThisPackage -> False
983
984 -- Foreign label is in some named package.
985 -- When compiling in the "dyn" way, each package is to be
986 -- linked into its own DLL.
987 ForeignLabelInPackage pkgId ->
988 (WayDyn `elem` ways dflags) && (this_pkg /= pkgId)
989
990 else -- On Mac OS X and on ELF platforms, false positives are OK,
991 -- so we claim that all foreign imports come from dynamic
992 -- libraries
993 True
994
995 HpcTicksLabel m -> (WayDyn `elem` ways dflags) && this_mod /= m
996
997 -- Note that DynamicLinkerLabels do NOT require dynamic linking themselves.
998 _ -> False
999 where
1000 os = platformOS (targetPlatform dflags)
1001 this_pkg = moduleUnitId this_mod
1002
1003
1004 -----------------------------------------------------------------------------
1005 -- Printing out CLabels.
1006
1007 {-
1008 Convention:
1009
1010 <name>_<type>
1011
1012 where <name> is <Module>_<name> for external names and <unique> for
1013 internal names. <type> is one of the following:
1014
1015 info Info table
1016 srt Static reference table
1017 srtd Static reference table descriptor
1018 entry Entry code (function, closure)
1019 slow Slow entry code (if any)
1020 ret Direct return address
1021 vtbl Vector table
1022 <n>_alt Case alternative (tag n)
1023 dflt Default case alternative
1024 btm Large bitmap vector
1025 closure Static closure
1026 con_entry Dynamic Constructor entry code
1027 con_info Dynamic Constructor info table
1028 static_entry Static Constructor entry code
1029 static_info Static Constructor info table
1030 sel_info Selector info table
1031 sel_entry Selector entry code
1032 cc Cost centre
1033 ccs Cost centre stack
1034
1035 Many of these distinctions are only for documentation reasons. For
1036 example, _ret is only distinguished from _entry to make it easy to
1037 tell whether a code fragment is a return point or a closure/function
1038 entry.
1039
1040 Note [Closure and info labels]
1041 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1042 For a function 'foo, we have:
1043 foo_info : Points to the info table describing foo's closure
1044 (and entry code for foo with tables next to code)
1045 foo_closure : Static (no-free-var) closure only:
1046 points to the statically-allocated closure
1047
1048 For a data constructor (such as Just or Nothing), we have:
1049 Just_con_info: Info table for the data constructor itself
1050 the first word of a heap-allocated Just
1051 Just_info: Info table for the *worker function*, an
1052 ordinary Haskell function of arity 1 that
1053 allocates a (Just x) box:
1054 Just = \x -> Just x
1055 Just_closure: The closure for this worker
1056
1057 Nothing_closure: a statically allocated closure for Nothing
1058 Nothing_static_info: info table for Nothing_closure
1059
1060 All these must be exported symbol, EXCEPT Just_info. We don't need to
1061 export this because in other modules we either have
1062 * A reference to 'Just'; use Just_closure
1063 * A saturated call 'Just x'; allocate using Just_con_info
1064 Not exporting these Just_info labels reduces the number of symbols
1065 somewhat.
1066
1067 Note [Bytes label]
1068 ~~~~~~~~~~~~~~~~~~
1069 For a top-level string literal 'foo', we have just one symbol 'foo_bytes', which
1070 points to a static data block containing the content of the literal.
1071
1072 Note [Proc-point local block entry-points]
1073 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1074 A label for a proc-point local block entry-point has no "_entry" suffix. With
1075 `infoTblLbl` we derive an info table label from a proc-point block ID. If
1076 we convert such an info table label into an entry label we must produce
1077 the label without an "_entry" suffix. So an info table label records
1078 the fact that it was derived from a block ID in `IdLabelInfo` as
1079 `BlockInfoTable`.
1080
1081 The info table label and the local block label are both local labels
1082 and are not externally visible.
1083 -}
1084
1085 instance Outputable CLabel where
1086 ppr c = sdocWithPlatform $ \platform -> pprCLabel platform c
1087
1088 pprCLabel :: Platform -> CLabel -> SDoc
1089
1090 pprCLabel platform (LocalBlockLabel u)
1091 = getPprStyle $ \ sty ->
1092 if asmStyle sty then
1093 ptext (asmTempLabelPrefix platform) <> pprUniqueAlways u
1094 else
1095 char '_' <> pprUniqueAlways u
1096
1097 pprCLabel platform (AsmTempLabel u)
1098 | not (platformUnregisterised platform)
1099 = getPprStyle $ \ sty ->
1100 if asmStyle sty then
1101 ptext (asmTempLabelPrefix platform) <> pprUniqueAlways u
1102 else
1103 char '_' <> pprUniqueAlways u
1104
1105 pprCLabel platform (AsmTempDerivedLabel l suf)
1106 | cGhcWithNativeCodeGen == "YES"
1107 = ptext (asmTempLabelPrefix platform)
1108 <> case l of AsmTempLabel u -> pprUniqueAlways u
1109 LocalBlockLabel u -> pprUniqueAlways u
1110 _other -> pprCLabel platform l
1111 <> ftext suf
1112
1113 pprCLabel platform (DynamicLinkerLabel info lbl)
1114 | cGhcWithNativeCodeGen == "YES"
1115 = pprDynamicLinkerAsmLabel platform info lbl
1116
1117 pprCLabel _ PicBaseLabel
1118 | cGhcWithNativeCodeGen == "YES"
1119 = text "1b"
1120
1121 pprCLabel platform (DeadStripPreventer lbl)
1122 | cGhcWithNativeCodeGen == "YES"
1123 = pprCLabel platform lbl <> text "_dsp"
1124
1125 pprCLabel _ (StringLitLabel u)
1126 | cGhcWithNativeCodeGen == "YES"
1127 = pprUniqueAlways u <> ptext (sLit "_str")
1128
1129 pprCLabel platform lbl
1130 = getPprStyle $ \ sty ->
1131 if cGhcWithNativeCodeGen == "YES" && asmStyle sty
1132 then maybe_underscore (pprAsmCLbl platform lbl)
1133 else pprCLbl lbl
1134
1135 maybe_underscore :: SDoc -> SDoc
1136 maybe_underscore doc
1137 | underscorePrefix = pp_cSEP <> doc
1138 | otherwise = doc
1139
1140 pprAsmCLbl :: Platform -> CLabel -> SDoc
1141 pprAsmCLbl platform (ForeignLabel fs (Just sz) _ _)
1142 | platformOS platform == OSMinGW32
1143 -- In asm mode, we need to put the suffix on a stdcall ForeignLabel.
1144 -- (The C compiler does this itself).
1145 = ftext fs <> char '@' <> int sz
1146 pprAsmCLbl _ lbl
1147 = pprCLbl lbl
1148
1149 pprCLbl :: CLabel -> SDoc
1150 pprCLbl (StringLitLabel u)
1151 = pprUniqueAlways u <> text "_str"
1152
1153 pprCLbl (SRTLabel u)
1154 = pprUniqueAlways u <> pp_cSEP <> text "srt"
1155
1156 pprCLbl (LargeSRTLabel u) = pprUniqueAlways u <> pp_cSEP <> text "srtd"
1157 pprCLbl (LargeBitmapLabel u) = text "b" <> pprUniqueAlways u <> pp_cSEP <> text "btm"
1158 -- Some bitsmaps for tuple constructors have a numeric tag (e.g. '7')
1159 -- until that gets resolved we'll just force them to start
1160 -- with a letter so the label will be legal assmbly code.
1161
1162
1163 pprCLbl (CmmLabel _ str CmmCode) = ftext str
1164 pprCLbl (CmmLabel _ str CmmData) = ftext str
1165 pprCLbl (CmmLabel _ str CmmPrimCall) = ftext str
1166
1167 pprCLbl (LocalBlockLabel u) = text "blk_" <> pprUniqueAlways u
1168
1169 pprCLbl (RtsLabel (RtsApFast str)) = ftext str <> text "_fast"
1170
1171 pprCLbl (RtsLabel (RtsSelectorInfoTable upd_reqd offset))
1172 = sdocWithDynFlags $ \dflags ->
1173 ASSERT(offset >= 0 && offset <= mAX_SPEC_SELECTEE_SIZE dflags)
1174 hcat [text "stg_sel_", text (show offset),
1175 ptext (if upd_reqd
1176 then (sLit "_upd_info")
1177 else (sLit "_noupd_info"))
1178 ]
1179
1180 pprCLbl (RtsLabel (RtsSelectorEntry upd_reqd offset))
1181 = sdocWithDynFlags $ \dflags ->
1182 ASSERT(offset >= 0 && offset <= mAX_SPEC_SELECTEE_SIZE dflags)
1183 hcat [text "stg_sel_", text (show offset),
1184 ptext (if upd_reqd
1185 then (sLit "_upd_entry")
1186 else (sLit "_noupd_entry"))
1187 ]
1188
1189 pprCLbl (RtsLabel (RtsApInfoTable upd_reqd arity))
1190 = sdocWithDynFlags $ \dflags ->
1191 ASSERT(arity > 0 && arity <= mAX_SPEC_AP_SIZE dflags)
1192 hcat [text "stg_ap_", text (show arity),
1193 ptext (if upd_reqd
1194 then (sLit "_upd_info")
1195 else (sLit "_noupd_info"))
1196 ]
1197
1198 pprCLbl (RtsLabel (RtsApEntry upd_reqd arity))
1199 = sdocWithDynFlags $ \dflags ->
1200 ASSERT(arity > 0 && arity <= mAX_SPEC_AP_SIZE dflags)
1201 hcat [text "stg_ap_", text (show arity),
1202 ptext (if upd_reqd
1203 then (sLit "_upd_entry")
1204 else (sLit "_noupd_entry"))
1205 ]
1206
1207 pprCLbl (CmmLabel _ fs CmmInfo)
1208 = ftext fs <> text "_info"
1209
1210 pprCLbl (CmmLabel _ fs CmmEntry)
1211 = ftext fs <> text "_entry"
1212
1213 pprCLbl (CmmLabel _ fs CmmRetInfo)
1214 = ftext fs <> text "_info"
1215
1216 pprCLbl (CmmLabel _ fs CmmRet)
1217 = ftext fs <> text "_ret"
1218
1219 pprCLbl (CmmLabel _ fs CmmClosure)
1220 = ftext fs <> text "_closure"
1221
1222 pprCLbl (RtsLabel (RtsPrimOp primop))
1223 = text "stg_" <> ppr primop
1224
1225 pprCLbl (RtsLabel (RtsSlowFastTickyCtr pat))
1226 = text "SLOW_CALL_fast_" <> text pat <> ptext (sLit "_ctr")
1227
1228 pprCLbl (ForeignLabel str _ _ _)
1229 = ftext str
1230
1231 pprCLbl (IdLabel name _cafs flavor) = ppr name <> ppIdFlavor flavor
1232
1233 pprCLbl (CC_Label cc) = ppr cc
1234 pprCLbl (CCS_Label ccs) = ppr ccs
1235
1236 pprCLbl (HpcTicksLabel mod)
1237 = text "_hpc_tickboxes_" <> ppr mod <> ptext (sLit "_hpc")
1238
1239 pprCLbl (AsmTempLabel {}) = panic "pprCLbl AsmTempLabel"
1240 pprCLbl (AsmTempDerivedLabel {})= panic "pprCLbl AsmTempDerivedLabel"
1241 pprCLbl (DynamicLinkerLabel {}) = panic "pprCLbl DynamicLinkerLabel"
1242 pprCLbl (PicBaseLabel {}) = panic "pprCLbl PicBaseLabel"
1243 pprCLbl (DeadStripPreventer {}) = panic "pprCLbl DeadStripPreventer"
1244
1245 ppIdFlavor :: IdLabelInfo -> SDoc
1246 ppIdFlavor x = pp_cSEP <>
1247 (case x of
1248 Closure -> text "closure"
1249 SRT -> text "srt"
1250 InfoTable -> text "info"
1251 LocalInfoTable -> text "info"
1252 Entry -> text "entry"
1253 LocalEntry -> text "entry"
1254 Slow -> text "slow"
1255 RednCounts -> text "ct"
1256 ConEntry -> text "con_entry"
1257 ConInfoTable -> text "con_info"
1258 ClosureTable -> text "closure_tbl"
1259 Bytes -> text "bytes"
1260 BlockInfoTable -> text "info"
1261 )
1262
1263
1264 pp_cSEP :: SDoc
1265 pp_cSEP = char '_'
1266
1267
1268 instance Outputable ForeignLabelSource where
1269 ppr fs
1270 = case fs of
1271 ForeignLabelInPackage pkgId -> parens $ text "package: " <> ppr pkgId
1272 ForeignLabelInThisPackage -> parens $ text "this package"
1273 ForeignLabelInExternalPackage -> parens $ text "external package"
1274
1275 -- -----------------------------------------------------------------------------
1276 -- Machine-dependent knowledge about labels.
1277
1278 underscorePrefix :: Bool -- leading underscore on assembler labels?
1279 underscorePrefix = (cLeadingUnderscore == "YES")
1280
1281 asmTempLabelPrefix :: Platform -> LitString -- for formatting labels
1282 asmTempLabelPrefix platform = case platformOS platform of
1283 OSDarwin -> sLit "L"
1284 OSAIX -> sLit "__L" -- follow IBM XL C's convention
1285 _ -> sLit ".L"
1286
1287 pprDynamicLinkerAsmLabel :: Platform -> DynamicLinkerLabelInfo -> CLabel -> SDoc
1288 pprDynamicLinkerAsmLabel platform dllInfo lbl =
1289 case platformOS platform of
1290 OSDarwin
1291 | platformArch platform == ArchX86_64 ->
1292 case dllInfo of
1293 CodeStub -> char 'L' <> ppr lbl <> text "$stub"
1294 SymbolPtr -> char 'L' <> ppr lbl <> text "$non_lazy_ptr"
1295 GotSymbolPtr -> ppr lbl <> text "@GOTPCREL"
1296 GotSymbolOffset -> ppr lbl
1297 | otherwise ->
1298 case dllInfo of
1299 CodeStub -> char 'L' <> ppr lbl <> text "$stub"
1300 SymbolPtr -> char 'L' <> ppr lbl <> text "$non_lazy_ptr"
1301 _ -> panic "pprDynamicLinkerAsmLabel"
1302
1303 OSAIX ->
1304 case dllInfo of
1305 SymbolPtr -> text "LC.." <> ppr lbl -- GCC's naming convention
1306 _ -> panic "pprDynamicLinkerAsmLabel"
1307
1308 _ | osElfTarget (platformOS platform) -> elfLabel
1309
1310 OSMinGW32 ->
1311 case dllInfo of
1312 SymbolPtr -> text "__imp_" <> ppr lbl
1313 _ -> panic "pprDynamicLinkerAsmLabel"
1314
1315 _ -> panic "pprDynamicLinkerAsmLabel"
1316 where
1317 elfLabel
1318 | platformArch platform == ArchPPC
1319 = case dllInfo of
1320 CodeStub -> -- See Note [.LCTOC1 in PPC PIC code]
1321 ppr lbl <> text "+32768@plt"
1322 SymbolPtr -> text ".LC_" <> ppr lbl
1323 _ -> panic "pprDynamicLinkerAsmLabel"
1324
1325 | platformArch platform == ArchX86_64
1326 = case dllInfo of
1327 CodeStub -> ppr lbl <> text "@plt"
1328 GotSymbolPtr -> ppr lbl <> text "@gotpcrel"
1329 GotSymbolOffset -> ppr lbl
1330 SymbolPtr -> text ".LC_" <> ppr lbl
1331
1332 | platformArch platform == ArchPPC_64 ELF_V1
1333 || platformArch platform == ArchPPC_64 ELF_V2
1334 = case dllInfo of
1335 GotSymbolPtr -> text ".LC_" <> ppr lbl
1336 <> text "@toc"
1337 GotSymbolOffset -> ppr lbl
1338 SymbolPtr -> text ".LC_" <> ppr lbl
1339 _ -> panic "pprDynamicLinkerAsmLabel"
1340
1341 | otherwise
1342 = case dllInfo of
1343 CodeStub -> ppr lbl <> text "@plt"
1344 SymbolPtr -> text ".LC_" <> ppr lbl
1345 GotSymbolPtr -> ppr lbl <> text "@got"
1346 GotSymbolOffset -> ppr lbl <> text "@gotoff"
1347