Merge RtsLabelInfo.Rts* with RtsLabelInfo.Rts*FS
[ghc-hetmet.git] / compiler / cmm / CLabel.hs
1 {-# OPTIONS -w #-}
2 -- The above warning supression flag is a temporary kludge.
3 -- While working on this module you are encouraged to remove it and fix
4 -- any warnings in the module. See
5 --     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
6 -- for details
7
8 -----------------------------------------------------------------------------
9 --
10 -- Object-file symbols (called CLabel for histerical raisins).
11 --
12 -- (c) The University of Glasgow 2004-2006
13 --
14 -----------------------------------------------------------------------------
15
16 module CLabel (
17         CLabel, -- abstract type
18
19         mkClosureLabel,
20         mkSRTLabel,
21         mkInfoTableLabel,
22         mkEntryLabel,
23         mkSlowEntryLabel,
24         mkConEntryLabel,
25         mkStaticConEntryLabel,
26         mkRednCountsLabel,
27         mkConInfoTableLabel,
28         mkStaticInfoTableLabel,
29         mkLargeSRTLabel,
30         mkApEntryLabel,
31         mkApInfoTableLabel,
32         mkClosureTableLabel,
33
34         mkLocalClosureLabel,
35         mkLocalInfoTableLabel,
36         mkLocalEntryLabel,
37         mkLocalConEntryLabel,
38         mkLocalStaticConEntryLabel,
39         mkLocalConInfoTableLabel,
40         mkLocalStaticInfoTableLabel,
41         mkLocalClosureTableLabel,
42
43         mkReturnPtLabel,
44         mkReturnInfoLabel,
45         mkAltLabel,
46         mkDefaultLabel,
47         mkBitmapLabel,
48         mkStringLitLabel,
49
50         mkAsmTempLabel,
51
52         mkModuleInitLabel,
53         mkPlainModuleInitLabel,
54         mkModuleInitTableLabel,
55
56         mkSplitMarkerLabel,
57         mkDirty_MUT_VAR_Label,
58         mkUpdInfoLabel,
59         mkIndStaticInfoLabel,
60         mkMainCapabilityLabel,
61         mkMAP_FROZEN_infoLabel,
62         mkMAP_DIRTY_infoLabel,
63         mkEMPTY_MVAR_infoLabel,
64
65         mkTopTickyCtrLabel,
66         mkCAFBlackHoleInfoTableLabel,
67         mkRtsPrimOpLabel,
68         mkRtsSlowTickyCtrLabel,
69
70         moduleRegdLabel,
71         moduleRegTableLabel,
72
73         mkSelectorInfoLabel,
74         mkSelectorEntryLabel,
75
76         mkRtsInfoLabel,
77         mkRtsEntryLabel,
78         mkRtsRetInfoLabel,
79         mkRtsRetLabel,
80         mkRtsCodeLabel,
81         mkRtsDataLabel,
82         mkRtsGcPtrLabel,
83
84         mkRtsApFastLabel,
85
86         mkPrimCallLabel,
87
88         mkForeignLabel,
89         addLabelSize,
90         foreignLabelStdcallInfo,
91
92         mkCCLabel, mkCCSLabel,
93
94         DynamicLinkerLabelInfo(..),
95         mkDynamicLinkerLabel,
96         dynamicLinkerLabelInfo,
97         
98         mkPicBaseLabel,
99         mkDeadStripPreventer,
100
101         mkHpcTicksLabel,
102         mkHpcModuleNameLabel,
103
104         hasCAF,
105         infoLblToEntryLbl, entryLblToInfoLbl, cvtToClosureLbl, cvtToSRTLbl,
106         needsCDecl, isAsmTemp, maybeAsmTemp, externallyVisibleCLabel,
107         isMathFun,
108         isCFunctionLabel, isGcPtrLabel, labelDynamic,
109
110         pprCLabel
111     ) where
112
113 #include "HsVersions.h"
114
115 import IdInfo
116 import StaticFlags
117 import BasicTypes
118 import Literal
119 import Packages
120 import DataCon
121 import PackageConfig
122 import Module
123 import Name
124 import Unique
125 import PrimOp
126 import Config
127 import CostCentre
128 import Outputable
129 import FastString
130 import DynFlags
131 import UniqSet
132
133 -- -----------------------------------------------------------------------------
134 -- The CLabel type
135
136 {-
137 CLabel is an abstract type that supports the following operations:
138
139   - Pretty printing
140
141   - In a C file, does it need to be declared before use?  (i.e. is it
142     guaranteed to be already in scope in the places we need to refer to it?)
143
144   - If it needs to be declared, what type (code or data) should it be
145     declared to have?
146
147   - Is it visible outside this object file or not?
148
149   - Is it "dynamic" (see details below)
150
151   - Eq and Ord, so that we can make sets of CLabels (currently only
152     used in outputting C as far as I can tell, to avoid generating
153     more than one declaration for any given label).
154
155   - Converting an info table label into an entry label.
156 -}
157
158 data CLabel
159   = IdLabel                     -- A family of labels related to the
160         Name                    -- definition of a particular Id or Con
161         CafInfo
162         IdLabelInfo
163
164   | CaseLabel                   -- A family of labels related to a particular
165                                 -- case expression.
166         {-# UNPACK #-} !Unique  -- Unique says which case expression
167         CaseLabelInfo
168
169   | AsmTempLabel 
170         {-# UNPACK #-} !Unique
171
172   | StringLitLabel
173         {-# UNPACK #-} !Unique
174
175   | ModuleInitLabel 
176         Module                  -- the module name
177         String                  -- its "way"
178         -- at some point we might want some kind of version number in
179         -- the module init label, to guard against compiling modules in
180         -- the wrong order.  We can't use the interface file version however,
181         -- because we don't always recompile modules which depend on a module
182         -- whose version has changed.
183
184   | PlainModuleInitLabel        -- without the version & way info
185         Module
186
187   | ModuleInitTableLabel        -- table of imported modules to init
188         Module
189
190   | ModuleRegdLabel
191
192   | RtsLabel RtsLabelInfo
193
194   | ForeignLabel FastString     -- a 'C' (or otherwise foreign) label
195         (Maybe Int)             -- possible '@n' suffix for stdcall functions
196                 -- When generating C, the '@n' suffix is omitted, but when
197                 -- generating assembler we must add it to the label.
198         Bool                    -- True <=> is dynamic
199         FunctionOrData
200
201   | CC_Label  CostCentre
202   | CCS_Label CostCentreStack
203
204       -- Dynamic Linking in the NCG:
205       -- generated and used inside the NCG only,
206       -- see module PositionIndependentCode for details.
207       
208   | DynamicLinkerLabel DynamicLinkerLabelInfo CLabel
209         -- special variants of a label used for dynamic linking
210
211   | PicBaseLabel                -- a label used as a base for PIC calculations
212                                 -- on some platforms.
213                                 -- It takes the form of a local numeric
214                                 -- assembler label '1'; it is pretty-printed
215                                 -- as 1b, referring to the previous definition
216                                 -- of 1: in the assembler source file.
217
218   | DeadStripPreventer CLabel
219     -- label before an info table to prevent excessive dead-stripping on darwin
220
221   | HpcTicksLabel Module       -- Per-module table of tick locations
222   | HpcModuleNameLabel         -- Per-module name of the module for Hpc
223
224   | LargeSRTLabel           -- Label of an StgLargeSRT
225         {-# UNPACK #-} !Unique
226
227   | LargeBitmapLabel        -- A bitmap (function or case return)
228         {-# UNPACK #-} !Unique
229
230   deriving (Eq, Ord)
231
232 data IdLabelInfo
233   = Closure             -- Label for closure
234   | SRT                 -- Static reference table
235   | InfoTable           -- Info tables for closures; always read-only
236   | Entry               -- entry point
237   | Slow                -- slow entry point
238
239   | RednCounts          -- Label of place to keep Ticky-ticky  info for 
240                         -- this Id
241
242   | ConEntry            -- constructor entry point
243   | ConInfoTable                -- corresponding info table
244   | StaticConEntry      -- static constructor entry point
245   | StaticInfoTable     -- corresponding info table
246
247   | ClosureTable        -- table of closures for Enum tycons
248
249   deriving (Eq, Ord)
250
251
252 data CaseLabelInfo
253   = CaseReturnPt
254   | CaseReturnInfo
255   | CaseAlt ConTag
256   | CaseDefault
257   deriving (Eq, Ord)
258
259
260 data RtsLabelInfo
261   = RtsSelectorInfoTable Bool{-updatable-} Int{-offset-}        -- Selector thunks
262   | RtsSelectorEntry   Bool{-updatable-} Int{-offset-}
263
264   | RtsApInfoTable Bool{-updatable-} Int{-arity-}               -- AP thunks
265   | RtsApEntry   Bool{-updatable-} Int{-arity-}
266
267   | RtsPrimOp PrimOp
268
269   | RtsInfo       FastString    -- misc rts info tables
270   | RtsEntry      FastString    -- misc rts entry points
271   | RtsRetInfo    FastString    -- misc rts ret info tables
272   | RtsRet        FastString    -- misc rts return points
273   | RtsData       FastString    -- misc rts data bits, eg CHARLIKE_closure
274   | RtsCode       FastString    -- misc rts code
275   | RtsGcPtr      FastString    -- GcPtrs eg CHARLIKE_closure  
276
277   | RtsApFast     FastString    -- _fast versions of generic apply
278
279   | RtsSlowTickyCtr String
280
281   deriving (Eq, Ord)
282         -- NOTE: Eq on LitString compares the pointer only, so this isn't
283         -- a real equality.
284
285 data DynamicLinkerLabelInfo
286   = CodeStub            -- MachO: Lfoo$stub, ELF: foo@plt
287   | SymbolPtr           -- MachO: Lfoo$non_lazy_ptr, Windows: __imp_foo
288   | GotSymbolPtr        -- ELF: foo@got
289   | GotSymbolOffset     -- ELF: foo@gotoff
290   
291   deriving (Eq, Ord)
292   
293 -- -----------------------------------------------------------------------------
294 -- Constructing CLabels
295
296 -- These are always local:
297 mkSRTLabel              name c  = IdLabel name  c SRT
298 mkSlowEntryLabel        name c  = IdLabel name  c Slow
299 mkRednCountsLabel       name c  = IdLabel name  c RednCounts
300
301 -- These have local & (possibly) external variants:
302 mkLocalClosureLabel     name c  = IdLabel name  c Closure
303 mkLocalInfoTableLabel   name c  = IdLabel name  c InfoTable
304 mkLocalEntryLabel       name c  = IdLabel name  c Entry
305 mkLocalClosureTableLabel name c = IdLabel name  c ClosureTable
306
307 mkClosureLabel name         c     = IdLabel name c Closure
308 mkInfoTableLabel name       c     = IdLabel name c InfoTable
309 mkEntryLabel name           c     = IdLabel name c Entry
310 mkClosureTableLabel name    c     = IdLabel name c ClosureTable
311 mkLocalConInfoTableLabel    c con = IdLabel con c ConInfoTable
312 mkLocalConEntryLabel        c con = IdLabel con c ConEntry
313 mkLocalStaticInfoTableLabel c con = IdLabel con c StaticInfoTable
314 mkLocalStaticConEntryLabel  c con = IdLabel con c StaticConEntry
315 mkConInfoTableLabel name    c     = IdLabel    name c ConInfoTable
316 mkStaticInfoTableLabel name c     = IdLabel    name c StaticInfoTable
317
318 mkConEntryLabel name        c     = IdLabel name c ConEntry
319 mkStaticConEntryLabel name  c     = IdLabel name c StaticConEntry
320
321 mkLargeSRTLabel uniq    = LargeSRTLabel uniq
322 mkBitmapLabel   uniq    = LargeBitmapLabel uniq
323
324 mkReturnPtLabel uniq            = CaseLabel uniq CaseReturnPt
325 mkReturnInfoLabel uniq          = CaseLabel uniq CaseReturnInfo
326 mkAltLabel      uniq tag        = CaseLabel uniq (CaseAlt tag)
327 mkDefaultLabel  uniq            = CaseLabel uniq CaseDefault
328
329 mkStringLitLabel                = StringLitLabel
330 mkAsmTempLabel :: Uniquable a => a -> CLabel
331 mkAsmTempLabel a                = AsmTempLabel (getUnique a)
332
333 mkModuleInitLabel :: Module -> String -> CLabel
334 mkModuleInitLabel mod way        = ModuleInitLabel mod way
335
336 mkPlainModuleInitLabel :: Module -> CLabel
337 mkPlainModuleInitLabel mod       = PlainModuleInitLabel mod
338
339 mkModuleInitTableLabel :: Module -> CLabel
340 mkModuleInitTableLabel mod       = ModuleInitTableLabel mod
341
342         -- Some fixed runtime system labels
343
344 mkSplitMarkerLabel              = RtsLabel (RtsCode (fsLit "__stg_split_marker"))
345 mkDirty_MUT_VAR_Label           = RtsLabel (RtsCode (fsLit "dirty_MUT_VAR"))
346 mkUpdInfoLabel                  = RtsLabel (RtsInfo (fsLit "stg_upd_frame"))
347 mkIndStaticInfoLabel            = RtsLabel (RtsInfo (fsLit "stg_IND_STATIC"))
348 mkMainCapabilityLabel           = RtsLabel (RtsData (fsLit "MainCapability"))
349 mkMAP_FROZEN_infoLabel          = RtsLabel (RtsInfo (fsLit "stg_MUT_ARR_PTRS_FROZEN0"))
350 mkMAP_DIRTY_infoLabel           = RtsLabel (RtsInfo (fsLit "stg_MUT_ARR_PTRS_DIRTY"))
351 mkEMPTY_MVAR_infoLabel          = RtsLabel (RtsInfo (fsLit "stg_EMPTY_MVAR"))
352
353 mkTopTickyCtrLabel              = RtsLabel (RtsData (fsLit "top_ct"))
354 mkCAFBlackHoleInfoTableLabel    = RtsLabel (RtsInfo (fsLit "stg_CAF_BLACKHOLE"))
355 mkRtsPrimOpLabel primop         = RtsLabel (RtsPrimOp primop)
356
357 moduleRegdLabel                 = ModuleRegdLabel
358 moduleRegTableLabel             = ModuleInitTableLabel  
359
360 mkSelectorInfoLabel  upd off    = RtsLabel (RtsSelectorInfoTable upd off)
361 mkSelectorEntryLabel upd off    = RtsLabel (RtsSelectorEntry   upd off)
362
363 mkApInfoTableLabel  upd off     = RtsLabel (RtsApInfoTable upd off)
364 mkApEntryLabel upd off          = RtsLabel (RtsApEntry   upd off)
365
366         -- Primitive / cmm call labels
367
368 mkPrimCallLabel :: PrimCall -> CLabel
369 mkPrimCallLabel (PrimCall str)  = ForeignLabel str Nothing False IsFunction
370
371         -- Foreign labels
372
373 mkForeignLabel :: FastString -> Maybe Int -> Bool -> FunctionOrData -> CLabel
374 mkForeignLabel str mb_sz is_dynamic fod
375     = ForeignLabel str mb_sz is_dynamic fod
376
377 addLabelSize :: CLabel -> Int -> CLabel
378 addLabelSize (ForeignLabel str _ is_dynamic fod) sz
379   = ForeignLabel str (Just sz) is_dynamic fod
380 addLabelSize label _
381   = label
382
383 foreignLabelStdcallInfo :: CLabel -> Maybe Int
384 foreignLabelStdcallInfo (ForeignLabel _ info _ _) = info
385 foreignLabelStdcallInfo _lbl = Nothing
386
387         -- Cost centres etc.
388
389 mkCCLabel       cc              = CC_Label cc
390 mkCCSLabel      ccs             = CCS_Label ccs
391
392 mkRtsInfoLabel      str = RtsLabel (RtsInfo      str)
393 mkRtsEntryLabel     str = RtsLabel (RtsEntry     str)
394 mkRtsRetInfoLabel   str = RtsLabel (RtsRetInfo   str)
395 mkRtsRetLabel       str = RtsLabel (RtsRet       str)
396 mkRtsCodeLabel      str = RtsLabel (RtsCode      str)
397 mkRtsDataLabel      str = RtsLabel (RtsData      str)
398 mkRtsGcPtrLabel     str = RtsLabel (RtsGcPtr     str)
399
400 mkRtsApFastLabel str = RtsLabel (RtsApFast str)
401
402 mkRtsSlowTickyCtrLabel :: String -> CLabel
403 mkRtsSlowTickyCtrLabel pat = RtsLabel (RtsSlowTickyCtr pat)
404
405         -- Coverage
406
407 mkHpcTicksLabel                = HpcTicksLabel
408 mkHpcModuleNameLabel           = HpcModuleNameLabel
409
410         -- Dynamic linking
411         
412 mkDynamicLinkerLabel :: DynamicLinkerLabelInfo -> CLabel -> CLabel
413 mkDynamicLinkerLabel = DynamicLinkerLabel
414
415 dynamicLinkerLabelInfo :: CLabel -> Maybe (DynamicLinkerLabelInfo, CLabel)
416 dynamicLinkerLabelInfo (DynamicLinkerLabel info lbl) = Just (info, lbl)
417 dynamicLinkerLabelInfo _ = Nothing
418
419         -- Position independent code
420         
421 mkPicBaseLabel :: CLabel
422 mkPicBaseLabel = PicBaseLabel
423
424 mkDeadStripPreventer :: CLabel -> CLabel
425 mkDeadStripPreventer lbl = DeadStripPreventer lbl
426
427 -- -----------------------------------------------------------------------------
428 -- Converting between info labels and entry/ret labels.
429
430 infoLblToEntryLbl :: CLabel -> CLabel 
431 infoLblToEntryLbl (IdLabel n c InfoTable)       = IdLabel n c Entry
432 infoLblToEntryLbl (IdLabel n c ConInfoTable)    = IdLabel n c ConEntry
433 infoLblToEntryLbl (IdLabel n c StaticInfoTable) = IdLabel n c StaticConEntry
434 infoLblToEntryLbl (CaseLabel n CaseReturnInfo)  = CaseLabel n CaseReturnPt
435 infoLblToEntryLbl (RtsLabel (RtsInfo s))      = RtsLabel (RtsEntry s)
436 infoLblToEntryLbl (RtsLabel (RtsRetInfo s))   = RtsLabel (RtsRet s)
437 infoLblToEntryLbl _ = panic "CLabel.infoLblToEntryLbl"
438
439 entryLblToInfoLbl :: CLabel -> CLabel 
440 entryLblToInfoLbl (IdLabel n c Entry)           = IdLabel n c InfoTable
441 entryLblToInfoLbl (IdLabel n c ConEntry)        = IdLabel n c ConInfoTable
442 entryLblToInfoLbl (IdLabel n c StaticConEntry)  = IdLabel n c StaticInfoTable
443 entryLblToInfoLbl (CaseLabel n CaseReturnPt)    = CaseLabel n CaseReturnInfo
444 entryLblToInfoLbl (RtsLabel (RtsEntry s))     = RtsLabel (RtsInfo s)
445 entryLblToInfoLbl (RtsLabel (RtsRet s))       = RtsLabel (RtsRetInfo s)
446 entryLblToInfoLbl l = pprPanic "CLabel.entryLblToInfoLbl" (pprCLabel l)
447
448 cvtToClosureLbl   (IdLabel n c InfoTable) = IdLabel n c Closure
449 cvtToClosureLbl   (IdLabel n c Entry)     = IdLabel n c Closure
450 cvtToClosureLbl   (IdLabel n c ConEntry)  = IdLabel n c Closure
451 cvtToClosureLbl l@(IdLabel n c Closure)   = l
452 cvtToClosureLbl l = pprPanic "cvtToClosureLbl" (pprCLabel l)
453
454 cvtToSRTLbl   (IdLabel n c InfoTable) = mkSRTLabel n c
455 cvtToSRTLbl   (IdLabel n c Entry)     = mkSRTLabel n c
456 cvtToSRTLbl   (IdLabel n c ConEntry)  = mkSRTLabel n c
457 cvtToSRTLbl l@(IdLabel n c Closure)   = mkSRTLabel n c
458 cvtToSRTLbl l = pprPanic "cvtToSRTLbl" (pprCLabel l)
459
460 -- -----------------------------------------------------------------------------
461 -- Does a CLabel refer to a CAF?
462 hasCAF :: CLabel -> Bool
463 hasCAF (IdLabel _ MayHaveCafRefs _) = True
464 hasCAF _                            = False
465
466 -- -----------------------------------------------------------------------------
467 -- Does a CLabel need declaring before use or not?
468 --
469 -- See wiki:Commentary/Compiler/Backends/PprC#Prototypes
470
471 needsCDecl :: CLabel -> Bool
472   -- False <=> it's pre-declared; don't bother
473   -- don't bother declaring SRT & Bitmap labels, we always make sure
474   -- they are defined before use.
475 needsCDecl (IdLabel _ _ SRT)            = False
476 needsCDecl (LargeSRTLabel _)            = False
477 needsCDecl (LargeBitmapLabel _)         = False
478 needsCDecl (IdLabel _ _ _)              = True
479 needsCDecl (CaseLabel _ _)              = True
480 needsCDecl (ModuleInitLabel _ _)        = True
481 needsCDecl (PlainModuleInitLabel _)     = True
482 needsCDecl (ModuleInitTableLabel _)     = True
483 needsCDecl ModuleRegdLabel              = False
484
485 needsCDecl (StringLitLabel _)           = False
486 needsCDecl (AsmTempLabel _)             = False
487 needsCDecl (RtsLabel _)                 = False
488 needsCDecl l@(ForeignLabel _ _ _ _)     = not (isMathFun l)
489 needsCDecl (CC_Label _)                 = True
490 needsCDecl (CCS_Label _)                = True
491 needsCDecl (HpcTicksLabel _)            = True
492 needsCDecl HpcModuleNameLabel           = False
493
494 -- Whether the label is an assembler temporary:
495
496 isAsmTemp  :: CLabel -> Bool    -- is a local temporary for native code generation
497 isAsmTemp (AsmTempLabel _) = True
498 isAsmTemp _                = False
499
500 maybeAsmTemp :: CLabel -> Maybe Unique
501 maybeAsmTemp (AsmTempLabel uq) = Just uq
502 maybeAsmTemp _                 = Nothing
503
504 -- some labels have C prototypes in scope when compiling via C, because
505 -- they are builtin to the C compiler.  For these labels we avoid
506 -- generating our own C prototypes.
507 isMathFun :: CLabel -> Bool
508 isMathFun (ForeignLabel fs _ _ _) = fs `elementOfUniqSet` math_funs
509 isMathFun _ = False
510
511 math_funs = mkUniqSet [
512         -- _ISOC99_SOURCE
513         (fsLit "acos"),         (fsLit "acosf"),        (fsLit "acosh"),
514         (fsLit "acoshf"),       (fsLit "acoshl"),       (fsLit "acosl"),
515         (fsLit "asin"),         (fsLit "asinf"),        (fsLit "asinl"),
516         (fsLit "asinh"),        (fsLit "asinhf"),       (fsLit "asinhl"),
517         (fsLit "atan"),         (fsLit "atanf"),        (fsLit "atanl"),
518         (fsLit "atan2"),        (fsLit "atan2f"),       (fsLit "atan2l"),
519         (fsLit "atanh"),        (fsLit "atanhf"),       (fsLit "atanhl"),
520         (fsLit "cbrt"),         (fsLit "cbrtf"),        (fsLit "cbrtl"),
521         (fsLit "ceil"),         (fsLit "ceilf"),        (fsLit "ceill"),
522         (fsLit "copysign"),     (fsLit "copysignf"),    (fsLit "copysignl"),
523         (fsLit "cos"),          (fsLit "cosf"),         (fsLit "cosl"),
524         (fsLit "cosh"),         (fsLit "coshf"),        (fsLit "coshl"),
525         (fsLit "erf"),          (fsLit "erff"),         (fsLit "erfl"),
526         (fsLit "erfc"),         (fsLit "erfcf"),        (fsLit "erfcl"),
527         (fsLit "exp"),          (fsLit "expf"),         (fsLit "expl"),
528         (fsLit "exp2"),         (fsLit "exp2f"),        (fsLit "exp2l"),
529         (fsLit "expm1"),        (fsLit "expm1f"),       (fsLit "expm1l"),
530         (fsLit "fabs"),         (fsLit "fabsf"),        (fsLit "fabsl"),
531         (fsLit "fdim"),         (fsLit "fdimf"),        (fsLit "fdiml"),
532         (fsLit "floor"),        (fsLit "floorf"),       (fsLit "floorl"),
533         (fsLit "fma"),          (fsLit "fmaf"),         (fsLit "fmal"),
534         (fsLit "fmax"),         (fsLit "fmaxf"),        (fsLit "fmaxl"),
535         (fsLit "fmin"),         (fsLit "fminf"),        (fsLit "fminl"),
536         (fsLit "fmod"),         (fsLit "fmodf"),        (fsLit "fmodl"),
537         (fsLit "frexp"),        (fsLit "frexpf"),       (fsLit "frexpl"),
538         (fsLit "hypot"),        (fsLit "hypotf"),       (fsLit "hypotl"),
539         (fsLit "ilogb"),        (fsLit "ilogbf"),       (fsLit "ilogbl"),
540         (fsLit "ldexp"),        (fsLit "ldexpf"),       (fsLit "ldexpl"),
541         (fsLit "lgamma"),       (fsLit "lgammaf"),      (fsLit "lgammal"),
542         (fsLit "llrint"),       (fsLit "llrintf"),      (fsLit "llrintl"),
543         (fsLit "llround"),      (fsLit "llroundf"),     (fsLit "llroundl"),
544         (fsLit "log"),          (fsLit "logf"),         (fsLit "logl"),
545         (fsLit "log10l"),       (fsLit "log10"),        (fsLit "log10f"),
546         (fsLit "log1pl"),       (fsLit "log1p"),        (fsLit "log1pf"),
547         (fsLit "log2"),         (fsLit "log2f"),        (fsLit "log2l"),
548         (fsLit "logb"),         (fsLit "logbf"),        (fsLit "logbl"),
549         (fsLit "lrint"),        (fsLit "lrintf"),       (fsLit "lrintl"),
550         (fsLit "lround"),       (fsLit "lroundf"),      (fsLit "lroundl"),
551         (fsLit "modf"),         (fsLit "modff"),        (fsLit "modfl"),
552         (fsLit "nan"),          (fsLit "nanf"),         (fsLit "nanl"),
553         (fsLit "nearbyint"),    (fsLit "nearbyintf"),   (fsLit "nearbyintl"),
554         (fsLit "nextafter"),    (fsLit "nextafterf"),   (fsLit "nextafterl"),
555         (fsLit "nexttoward"),   (fsLit "nexttowardf"),  (fsLit "nexttowardl"),
556         (fsLit "pow"),          (fsLit "powf"),         (fsLit "powl"),
557         (fsLit "remainder"),    (fsLit "remainderf"),   (fsLit "remainderl"),
558         (fsLit "remquo"),       (fsLit "remquof"),      (fsLit "remquol"),
559         (fsLit "rint"),         (fsLit "rintf"),        (fsLit "rintl"),
560         (fsLit "round"),        (fsLit "roundf"),       (fsLit "roundl"),
561         (fsLit "scalbln"),      (fsLit "scalblnf"),     (fsLit "scalblnl"),
562         (fsLit "scalbn"),       (fsLit "scalbnf"),      (fsLit "scalbnl"),
563         (fsLit "sin"),          (fsLit "sinf"),         (fsLit "sinl"),
564         (fsLit "sinh"),         (fsLit "sinhf"),        (fsLit "sinhl"),
565         (fsLit "sqrt"),         (fsLit "sqrtf"),        (fsLit "sqrtl"),
566         (fsLit "tan"),          (fsLit "tanf"),         (fsLit "tanl"),
567         (fsLit "tanh"),         (fsLit "tanhf"),        (fsLit "tanhl"),
568         (fsLit "tgamma"),       (fsLit "tgammaf"),      (fsLit "tgammal"),
569         (fsLit "trunc"),        (fsLit "truncf"),       (fsLit "truncl"),
570         -- ISO C 99 also defines these function-like macros in math.h:
571         -- fpclassify, isfinite, isinf, isnormal, signbit, isgreater,
572         -- isgreaterequal, isless, islessequal, islessgreater, isunordered
573
574         -- additional symbols from _BSD_SOURCE
575         (fsLit "drem"),         (fsLit "dremf"),        (fsLit "dreml"),
576         (fsLit "finite"),       (fsLit "finitef"),      (fsLit "finitel"),
577         (fsLit "gamma"),        (fsLit "gammaf"),       (fsLit "gammal"),
578         (fsLit "isinf"),        (fsLit "isinff"),       (fsLit "isinfl"),
579         (fsLit "isnan"),        (fsLit "isnanf"),       (fsLit "isnanl"),
580         (fsLit "j0"),           (fsLit "j0f"),          (fsLit "j0l"),
581         (fsLit "j1"),           (fsLit "j1f"),          (fsLit "j1l"),
582         (fsLit "jn"),           (fsLit "jnf"),          (fsLit "jnl"),
583         (fsLit "lgamma_r"),     (fsLit "lgammaf_r"),    (fsLit "lgammal_r"),
584         (fsLit "scalb"),        (fsLit "scalbf"),       (fsLit "scalbl"),
585         (fsLit "significand"),  (fsLit "significandf"), (fsLit "significandl"),
586         (fsLit "y0"),           (fsLit "y0f"),          (fsLit "y0l"),
587         (fsLit "y1"),           (fsLit "y1f"),          (fsLit "y1l"),
588         (fsLit "yn"),           (fsLit "ynf"),          (fsLit "ynl")
589     ]
590
591 -- -----------------------------------------------------------------------------
592 -- Is a CLabel visible outside this object file or not?
593
594 -- From the point of view of the code generator, a name is
595 -- externally visible if it has to be declared as exported
596 -- in the .o file's symbol table; that is, made non-static.
597
598 externallyVisibleCLabel :: CLabel -> Bool -- not C "static"
599 externallyVisibleCLabel (CaseLabel _ _)    = False
600 externallyVisibleCLabel (StringLitLabel _) = False
601 externallyVisibleCLabel (AsmTempLabel _)   = False
602 externallyVisibleCLabel (ModuleInitLabel _ _) = True
603 externallyVisibleCLabel (PlainModuleInitLabel _)= True
604 externallyVisibleCLabel (ModuleInitTableLabel _)= False
605 externallyVisibleCLabel ModuleRegdLabel    = False
606 externallyVisibleCLabel (RtsLabel _)       = True
607 externallyVisibleCLabel (ForeignLabel _ _ _ _) = True
608 externallyVisibleCLabel (IdLabel name _ _)     = isExternalName name
609 externallyVisibleCLabel (CC_Label _)       = True
610 externallyVisibleCLabel (CCS_Label _)      = True
611 externallyVisibleCLabel (DynamicLinkerLabel _ _)  = False
612 externallyVisibleCLabel (HpcTicksLabel _)   = True
613 externallyVisibleCLabel HpcModuleNameLabel      = False
614 externallyVisibleCLabel (LargeBitmapLabel _) = False
615 externallyVisibleCLabel (LargeSRTLabel _) = False
616
617 -- -----------------------------------------------------------------------------
618 -- Finding the "type" of a CLabel 
619
620 -- For generating correct types in label declarations:
621
622 data CLabelType
623   = CodeLabel   -- Address of some executable instructions
624   | DataLabel   -- Address of data, not a GC ptr
625   | GcPtrLabel  -- Address of a (presumably static) GC object
626
627 isCFunctionLabel :: CLabel -> Bool
628 isCFunctionLabel lbl = case labelType lbl of
629                         CodeLabel -> True
630                         _other    -> False
631
632 isGcPtrLabel :: CLabel -> Bool
633 isGcPtrLabel lbl = case labelType lbl of
634                         GcPtrLabel -> True
635                         _other     -> False
636
637 labelType :: CLabel -> CLabelType
638 labelType (RtsLabel (RtsSelectorInfoTable _ _)) = DataLabel
639 labelType (RtsLabel (RtsApInfoTable _ _))       = DataLabel
640 labelType (RtsLabel (RtsData _))              = DataLabel
641 labelType (RtsLabel (RtsGcPtr _))             = GcPtrLabel
642 labelType (RtsLabel (RtsCode _))              = CodeLabel
643 labelType (RtsLabel (RtsInfo _))              = DataLabel
644 labelType (RtsLabel (RtsEntry _))             = CodeLabel
645 labelType (RtsLabel (RtsRetInfo _))           = DataLabel
646 labelType (RtsLabel (RtsRet _))               = CodeLabel
647 labelType (RtsLabel (RtsApFast _))              = CodeLabel
648 labelType (CaseLabel _ CaseReturnInfo)          = DataLabel
649 labelType (CaseLabel _ _)                       = CodeLabel
650 labelType (ModuleInitLabel _ _)                 = CodeLabel
651 labelType (PlainModuleInitLabel _)              = CodeLabel
652 labelType (ModuleInitTableLabel _)              = DataLabel
653 labelType (LargeSRTLabel _)                     = DataLabel
654 labelType (LargeBitmapLabel _)                  = DataLabel
655 labelType (ForeignLabel _ _ _ IsFunction)       = CodeLabel
656 labelType (IdLabel _ _ info)                    = idInfoLabelType info
657 labelType _                                     = DataLabel
658
659 idInfoLabelType info =
660   case info of
661     InfoTable     -> DataLabel
662     Closure       -> GcPtrLabel
663     ConInfoTable  -> DataLabel
664     StaticInfoTable -> DataLabel
665     ClosureTable  -> DataLabel
666     RednCounts    -> DataLabel
667     _             -> CodeLabel
668
669
670 -- -----------------------------------------------------------------------------
671 -- Does a CLabel need dynamic linkage?
672
673 -- When referring to data in code, we need to know whether
674 -- that data resides in a DLL or not. [Win32 only.]
675 -- @labelDynamic@ returns @True@ if the label is located
676 -- in a DLL, be it a data reference or not.
677
678 labelDynamic :: PackageId -> CLabel -> Bool
679 labelDynamic this_pkg lbl =
680   case lbl of
681    RtsLabel _        -> not opt_Static && (this_pkg /= rtsPackageId) -- i.e., is the RTS in a DLL or not?
682    IdLabel n _ k       -> isDllName this_pkg n
683 #if mingw32_TARGET_OS
684    ForeignLabel _ _ d _ -> d
685 #else
686    -- On Mac OS X and on ELF platforms, false positives are OK,
687    -- so we claim that all foreign imports come from dynamic libraries
688    ForeignLabel _ _ _ _ -> True
689 #endif
690    ModuleInitLabel m _    -> not opt_Static && this_pkg /= (modulePackageId m)
691    PlainModuleInitLabel m -> not opt_Static && this_pkg /= (modulePackageId m)
692    ModuleInitTableLabel m -> not opt_Static && this_pkg /= (modulePackageId m)
693    
694    -- Note that DynamicLinkerLabels do NOT require dynamic linking themselves.
695    _                 -> False
696
697 {-
698 OLD?: These GRAN functions are needed for spitting out GRAN_FETCH() at the
699 right places. It is used to detect when the abstractC statement of an
700 CCodeBlock actually contains the code for a slow entry point.  -- HWL
701
702 We need at least @Eq@ for @CLabels@, because we want to avoid
703 duplicate declarations in generating C (see @labelSeenTE@ in
704 @PprAbsC@).
705 -}
706
707 -----------------------------------------------------------------------------
708 -- Printing out CLabels.
709
710 {-
711 Convention:
712
713       <name>_<type>
714
715 where <name> is <Module>_<name> for external names and <unique> for
716 internal names. <type> is one of the following:
717
718          info                   Info table
719          srt                    Static reference table
720          srtd                   Static reference table descriptor
721          entry                  Entry code (function, closure)
722          slow                   Slow entry code (if any)
723          ret                    Direct return address    
724          vtbl                   Vector table
725          <n>_alt                Case alternative (tag n)
726          dflt                   Default case alternative
727          btm                    Large bitmap vector
728          closure                Static closure
729          con_entry              Dynamic Constructor entry code
730          con_info               Dynamic Constructor info table
731          static_entry           Static Constructor entry code
732          static_info            Static Constructor info table
733          sel_info               Selector info table
734          sel_entry              Selector entry code
735          cc                     Cost centre
736          ccs                    Cost centre stack
737
738 Many of these distinctions are only for documentation reasons.  For
739 example, _ret is only distinguished from _entry to make it easy to
740 tell whether a code fragment is a return point or a closure/function
741 entry.
742 -}
743
744 instance Outputable CLabel where
745   ppr = pprCLabel
746
747 pprCLabel :: CLabel -> SDoc
748
749 #if ! OMIT_NATIVE_CODEGEN
750 pprCLabel (AsmTempLabel u)
751   =  getPprStyle $ \ sty ->
752      if asmStyle sty then 
753         ptext asmTempLabelPrefix <> pprUnique u
754      else
755         char '_' <> pprUnique u
756
757 pprCLabel (DynamicLinkerLabel info lbl)
758    = pprDynamicLinkerAsmLabel info lbl
759    
760 pprCLabel PicBaseLabel
761    = ptext (sLit "1b")
762    
763 pprCLabel (DeadStripPreventer lbl)
764    = pprCLabel lbl <> ptext (sLit "_dsp")
765 #endif
766
767 pprCLabel lbl = 
768 #if ! OMIT_NATIVE_CODEGEN
769     getPprStyle $ \ sty ->
770     if asmStyle sty then 
771         maybe_underscore (pprAsmCLbl lbl)
772     else
773 #endif
774        pprCLbl lbl
775
776 maybe_underscore doc
777   | underscorePrefix = pp_cSEP <> doc
778   | otherwise        = doc
779
780 #ifdef mingw32_TARGET_OS
781 -- In asm mode, we need to put the suffix on a stdcall ForeignLabel.
782 -- (The C compiler does this itself).
783 pprAsmCLbl (ForeignLabel fs (Just sz) _ _)
784    = ftext fs <> char '@' <> int sz
785 #endif
786 pprAsmCLbl lbl
787    = pprCLbl lbl
788
789 pprCLbl (StringLitLabel u)
790   = pprUnique u <> ptext (sLit "_str")
791
792 pprCLbl (CaseLabel u CaseReturnPt)
793   = hcat [pprUnique u, ptext (sLit "_ret")]
794 pprCLbl (CaseLabel u CaseReturnInfo)
795   = hcat [pprUnique u, ptext (sLit "_info")]
796 pprCLbl (CaseLabel u (CaseAlt tag))
797   = hcat [pprUnique u, pp_cSEP, int tag, ptext (sLit "_alt")]
798 pprCLbl (CaseLabel u CaseDefault)
799   = hcat [pprUnique u, ptext (sLit "_dflt")]
800
801 pprCLbl (LargeSRTLabel u)  = pprUnique u <> pp_cSEP <> ptext (sLit "srtd")
802 pprCLbl (LargeBitmapLabel u)  = text "b" <> pprUnique u <> pp_cSEP <> ptext (sLit "btm")
803 -- Some bitsmaps for tuple constructors have a numeric tag (e.g. '7')
804 -- until that gets resolved we'll just force them to start
805 -- with a letter so the label will be legal assmbly code.
806         
807
808 pprCLbl (RtsLabel (RtsCode str))   = ftext str
809 pprCLbl (RtsLabel (RtsData str))   = ftext str
810 pprCLbl (RtsLabel (RtsGcPtr str))  = ftext str
811
812 pprCLbl (RtsLabel (RtsApFast str))   = ftext str <> ptext (sLit "_fast")
813
814 pprCLbl (RtsLabel (RtsSelectorInfoTable upd_reqd offset))
815   = hcat [ptext (sLit "stg_sel_"), text (show offset),
816                 ptext (if upd_reqd 
817                         then (sLit "_upd_info") 
818                         else (sLit "_noupd_info"))
819         ]
820
821 pprCLbl (RtsLabel (RtsSelectorEntry upd_reqd offset))
822   = hcat [ptext (sLit "stg_sel_"), text (show offset),
823                 ptext (if upd_reqd 
824                         then (sLit "_upd_entry") 
825                         else (sLit "_noupd_entry"))
826         ]
827
828 pprCLbl (RtsLabel (RtsApInfoTable upd_reqd arity))
829   = hcat [ptext (sLit "stg_ap_"), text (show arity),
830                 ptext (if upd_reqd 
831                         then (sLit "_upd_info") 
832                         else (sLit "_noupd_info"))
833         ]
834
835 pprCLbl (RtsLabel (RtsApEntry upd_reqd arity))
836   = hcat [ptext (sLit "stg_ap_"), text (show arity),
837                 ptext (if upd_reqd 
838                         then (sLit "_upd_entry") 
839                         else (sLit "_noupd_entry"))
840         ]
841
842 pprCLbl (RtsLabel (RtsInfo fs))
843   = ftext fs <> ptext (sLit "_info")
844
845 pprCLbl (RtsLabel (RtsEntry fs))
846   = ftext fs <> ptext (sLit "_entry")
847
848 pprCLbl (RtsLabel (RtsRetInfo fs))
849   = ftext fs <> ptext (sLit "_info")
850
851 pprCLbl (RtsLabel (RtsRet fs))
852   = ftext fs <> ptext (sLit "_ret")
853
854 pprCLbl (RtsLabel (RtsPrimOp primop)) 
855   = ptext (sLit "stg_") <> ppr primop
856
857 pprCLbl (RtsLabel (RtsSlowTickyCtr pat)) 
858   = ptext (sLit "SLOW_CALL_") <> text pat <> ptext (sLit "_ctr")
859
860 pprCLbl ModuleRegdLabel
861   = ptext (sLit "_module_registered")
862
863 pprCLbl (ForeignLabel str _ _ _)
864   = ftext str
865
866 pprCLbl (IdLabel name cafs flavor) = ppr name <> ppIdFlavor flavor
867
868 pprCLbl (CC_Label cc)           = ppr cc
869 pprCLbl (CCS_Label ccs)         = ppr ccs
870
871 pprCLbl (ModuleInitLabel mod way)
872    = ptext (sLit "__stginit_") <> ppr mod
873         <> char '_' <> text way
874 pprCLbl (PlainModuleInitLabel mod)
875    = ptext (sLit "__stginit_") <> ppr mod
876 pprCLbl (ModuleInitTableLabel mod)
877    = ptext (sLit "__stginittable_") <> ppr mod
878
879 pprCLbl (HpcTicksLabel mod)
880   = ptext (sLit "_hpc_tickboxes_")  <> ppr mod <> ptext (sLit "_hpc")
881
882 pprCLbl HpcModuleNameLabel
883   = ptext (sLit "_hpc_module_name_str")
884
885 ppIdFlavor :: IdLabelInfo -> SDoc
886 ppIdFlavor x = pp_cSEP <>
887                (case x of
888                        Closure          -> ptext (sLit "closure")
889                        SRT              -> ptext (sLit "srt")
890                        InfoTable        -> ptext (sLit "info")
891                        Entry            -> ptext (sLit "entry")
892                        Slow             -> ptext (sLit "slow")
893                        RednCounts       -> ptext (sLit "ct")
894                        ConEntry         -> ptext (sLit "con_entry")
895                        ConInfoTable     -> ptext (sLit "con_info")
896                        StaticConEntry   -> ptext (sLit "static_entry")
897                        StaticInfoTable  -> ptext (sLit "static_info")
898                        ClosureTable     -> ptext (sLit "closure_tbl")
899                       )
900
901
902 pp_cSEP = char '_'
903
904 -- -----------------------------------------------------------------------------
905 -- Machine-dependent knowledge about labels.
906
907 underscorePrefix :: Bool   -- leading underscore on assembler labels?
908 underscorePrefix = (cLeadingUnderscore == "YES")
909
910 asmTempLabelPrefix :: LitString  -- for formatting labels
911 asmTempLabelPrefix =
912 #if alpha_TARGET_OS
913      {- The alpha assembler likes temporary labels to look like $L123
914         instead of L123.  (Don't toss the L, because then Lf28
915         turns into $f28.)
916      -}
917      (sLit "$")
918 #elif darwin_TARGET_OS
919      (sLit "L")
920 #else
921      (sLit ".L")
922 #endif
923
924 pprDynamicLinkerAsmLabel :: DynamicLinkerLabelInfo -> CLabel -> SDoc
925
926 #if x86_64_TARGET_ARCH && darwin_TARGET_OS
927 pprDynamicLinkerAsmLabel CodeStub lbl
928   = char 'L' <> pprCLabel lbl <> text "$stub"
929 pprDynamicLinkerAsmLabel SymbolPtr lbl
930   = char 'L' <> pprCLabel lbl <> text "$non_lazy_ptr"
931 pprDynamicLinkerAsmLabel GotSymbolPtr lbl
932   = pprCLabel lbl <> text "@GOTPCREL"
933 pprDynamicLinkerAsmLabel GotSymbolOffset lbl
934   = pprCLabel lbl
935 pprDynamicLinkerAsmLabel _ _
936   = panic "pprDynamicLinkerAsmLabel"
937 #elif darwin_TARGET_OS
938 pprDynamicLinkerAsmLabel CodeStub lbl
939   = char 'L' <> pprCLabel lbl <> text "$stub"
940 pprDynamicLinkerAsmLabel SymbolPtr lbl
941   = char 'L' <> pprCLabel lbl <> text "$non_lazy_ptr"
942 pprDynamicLinkerAsmLabel _ _
943   = panic "pprDynamicLinkerAsmLabel"
944 #elif powerpc_TARGET_ARCH && linux_TARGET_OS
945 pprDynamicLinkerAsmLabel CodeStub lbl
946   = pprCLabel lbl <> text "@plt"
947 pprDynamicLinkerAsmLabel SymbolPtr lbl
948   = text ".LC_" <> pprCLabel lbl
949 pprDynamicLinkerAsmLabel _ _
950   = panic "pprDynamicLinkerAsmLabel"
951 #elif x86_64_TARGET_ARCH && linux_TARGET_OS
952 pprDynamicLinkerAsmLabel CodeStub lbl
953   = pprCLabel lbl <> text "@plt"
954 pprDynamicLinkerAsmLabel GotSymbolPtr lbl
955   = pprCLabel lbl <> text "@gotpcrel"
956 pprDynamicLinkerAsmLabel GotSymbolOffset lbl
957   = pprCLabel lbl
958 pprDynamicLinkerAsmLabel SymbolPtr lbl
959   = text ".LC_" <> pprCLabel lbl
960 #elif linux_TARGET_OS
961 pprDynamicLinkerAsmLabel CodeStub lbl
962   = pprCLabel lbl <> text "@plt"
963 pprDynamicLinkerAsmLabel SymbolPtr lbl
964   = text ".LC_" <> pprCLabel lbl
965 pprDynamicLinkerAsmLabel GotSymbolPtr lbl
966   = pprCLabel lbl <> text "@got"
967 pprDynamicLinkerAsmLabel GotSymbolOffset lbl
968   = pprCLabel lbl <> text "@gotoff"
969 #elif mingw32_TARGET_OS
970 pprDynamicLinkerAsmLabel SymbolPtr lbl
971   = text "__imp_" <> pprCLabel lbl
972 pprDynamicLinkerAsmLabel _ _
973   = panic "pprDynamicLinkerAsmLabel"
974 #else
975 pprDynamicLinkerAsmLabel _ _
976   = panic "pprDynamicLinkerAsmLabel"
977 #endif