2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[CLabel]{@CLabel@: Information to make C Labels}
7 #include "HsVersions.h"
10 CLabel, -- abstract type
17 mkStaticConEntryLabel,
19 mkPhantomInfoTableLabel,
20 mkStaticInfoTableLabel,
24 mkConUpdCodePtrVecLabel,
25 mkStdUpdCodePtrVecLabel,
27 mkInfoTableVecTblLabel,
38 mkBlackHoleInfoTableLabel,
40 needsCDecl, isReadOnly, isAsmTemp, externallyVisibleCLabel,
45 , isSlowEntryCCodeBlock
48 -- and to make the interface self-sufficient...
53 import Id ( externallyVisibleId, cmpId_withSpecDataCon,
54 isDataCon, isDictFunId,
55 isConstMethodId_maybe, isClassOpId,
56 isDefaultMethodId_maybe,
57 isSuperDictSelId_maybe, fIRST_TAG,
58 DataCon(..), ConTag(..), Id
60 import Maybes ( maybeToBool )
61 import Unpretty -- NOTE!! ********************
64 import Pretty ( ppNil, ppChar, ppStr, ppPStr, ppDouble, ppInt,
65 ppInteger, ppBeside, ppIntersperse, prettyToUn
67 #ifdef USE_ATTACK_PRAGMAS
70 import Unique ( pprUnique, showUnique, Unique )
73 -- Sigh... Shouldn't this file (CLabel) live in codeGen?
74 import CgRetConv ( CtrlReturnConvention(..), ctrlReturnConvAlg )
78 things we want to find out:
80 * should the labelled things be declared "static" (visible only in this file)?
82 * should it be declared "const" (read-only text space)?
84 * does it need declarations at all? (v common Prelude things are pre-declared)
88 = IdLabel -- A family of labels related to the
89 CLabelId -- definition of a particular Id
90 IdLabelInfo -- Includes DataCon
92 | TyConLabel -- A family of labels related to the
93 TyCon -- definition of a data type
96 | CaseLabel -- A family of labels related to a particular case expression
97 Unique -- Unique says which case expression
100 | AsmTempLabel Unique
102 | RtsLabel RtsLabelInfo
107 The CLabelId is simply so we can declare alternative Eq and Ord
108 instances which use cmpId_SpecDataCon (instead of cmpId). This avoids
109 comparing the Uniques of two specialised data constructors (which have
110 the same as the uniques their respective unspecialised data
111 constructors). Instead, the specialising types and the uniques of the
112 unspecialised constructors are compared.
115 data CLabelId = CLabelId Id
117 instance Eq CLabelId where
118 CLabelId a == CLabelId b = case cmpId_withSpecDataCon a b of { EQ_ -> True; _ -> False }
119 CLabelId a /= CLabelId b = case cmpId_withSpecDataCon a b of { EQ_ -> False; _ -> True }
121 instance Ord CLabelId where
122 CLabelId a <= CLabelId b = case cmpId_withSpecDataCon a b
123 of { LT_ -> True; EQ_ -> True; GT__ -> False }
124 CLabelId a < CLabelId b = case cmpId_withSpecDataCon a b
125 of { LT_ -> True; EQ_ -> False; GT__ -> False }
126 CLabelId a >= CLabelId b = case cmpId_withSpecDataCon a b
127 of { LT_ -> False; EQ_ -> True; GT__ -> True }
128 CLabelId a > CLabelId b = case cmpId_withSpecDataCon a b
129 of { LT_ -> False; EQ_ -> False; GT__ -> True }
130 _tagCmp (CLabelId a) (CLabelId b) = case cmpId_withSpecDataCon a b
131 of { LT_ -> _LT; EQ_ -> _EQ; GT__ -> _GT }
136 = Closure -- Label for (static???) closure
138 | InfoTbl -- Info table for a closure; always read-only
140 | EntryStd -- Thunk, or "slow", code entry point (requires arg satis check)
141 | EntryFast Int -- entry pt when no arg satisfaction chk needed;
142 -- Int is the arity of the function (to be
143 -- encoded into the name)
145 | ConEntry -- the only kind of entry pt for constructors
146 | StaticConEntry -- static constructor entry point
148 | StaticInfoTbl -- corresponding info table
150 | PhantomInfoTbl -- for phantom constructors that only exist in regs
152 | VapInfoTbl Bool -- True <=> the update-reqd version; False <=> the no-update-reqd version
155 -- Ticky-ticky counting
156 | RednCounts -- Label of place to keep reduction-count info for this Id
161 = UnvecConUpdCode -- Update code for the data type if it's unvectored
163 | VecConUpdCode ConTag -- One for each constructor which returns in
164 -- regs; this code actually performs an update
166 | StdUpdCode ConTag -- Update code for all constructors which return
167 -- in heap. There are a small number of variants,
168 -- so that the update code returns (vectored/n or
169 -- unvectored) in the right way.
170 -- ToDo: maybe replace TyCon/Int with return conv.
172 | InfoTblVecTbl -- For tables of info tables
174 | StdUpdVecTbl -- Labels the update code, or table of update codes,
175 -- for a particular type.
186 = RtsShouldNeverHappenCode
188 | RtsBlackHoleInfoTbl
190 | RtsSelectorInfoTbl -- Selectors
191 Bool -- True <=> the update-reqd version;
192 -- False <=> the no-update-reqd version
193 Int -- 0-indexed Offset from the "goods"
195 | RtsSelectorEntry -- Ditto entry code
202 mkClosureLabel id = IdLabel (CLabelId id) Closure
203 mkInfoTableLabel id = IdLabel (CLabelId id) InfoTbl
204 mkStdEntryLabel id = IdLabel (CLabelId id) EntryStd
205 mkFastEntryLabel id arity = ASSERT(arity > 0)
206 IdLabel (CLabelId id) (EntryFast arity)
207 mkConEntryLabel id = IdLabel (CLabelId id) ConEntry
208 mkStaticConEntryLabel id = IdLabel (CLabelId id) StaticConEntry
209 mkRednCountsLabel id = IdLabel (CLabelId id) RednCounts
210 mkPhantomInfoTableLabel id = IdLabel (CLabelId id) PhantomInfoTbl
211 mkStaticInfoTableLabel id = IdLabel (CLabelId id) StaticInfoTbl
212 mkVapEntryLabel id upd_flag = IdLabel (CLabelId id) (VapEntry upd_flag)
213 mkVapInfoTableLabel id upd_flag = IdLabel (CLabelId id) (VapInfoTbl upd_flag)
215 mkConUpdCodePtrVecLabel tycon tag = TyConLabel tycon (VecConUpdCode tag)
216 mkStdUpdCodePtrVecLabel tycon tag = TyConLabel tycon (StdUpdCode tag)
218 mkInfoTableVecTblLabel tycon = TyConLabel tycon InfoTblVecTbl
219 mkStdUpdVecTblLabel tycon = TyConLabel tycon StdUpdVecTbl
221 mkReturnPtLabel uniq = CaseLabel uniq CaseReturnPt
222 mkVecTblLabel uniq = CaseLabel uniq CaseVecTbl
223 mkAltLabel uniq tag = CaseLabel uniq (CaseAlt tag)
224 mkDefaultLabel uniq = CaseLabel uniq CaseDefault
226 mkAsmTempLabel = AsmTempLabel
228 -- Some fixed runtime system labels
230 mkErrorStdEntryLabel = RtsLabel RtsShouldNeverHappenCode
231 mkBlackHoleInfoTableLabel = RtsLabel RtsBlackHoleInfoTbl
235 needsCDecl :: CLabel -> Bool -- False <=> it's pre-declared; don't bother
236 isReadOnly :: CLabel -> Bool -- lives in C "text space"
237 isAsmTemp :: CLabel -> Bool -- is a local temporary for native code generation
238 externallyVisibleCLabel :: CLabel -> Bool -- not C "static"
241 @needsCDecl@ is @True@ unless the thing is a deeply-@PreludeCore@-ish
242 object. {\em Also:} No need to spit out labels for things generated
243 by the flattener (in @AbsCUtils@)---it is careful to ensure references
244 to them are always backwards. These are return-point and vector-table
247 Declarations for (non-prelude) @Id@-based things are needed because of
250 needsCDecl (IdLabel _ _) = True
251 needsCDecl (CaseLabel _ _) = False
253 needsCDecl (TyConLabel _ (StdUpdCode _)) = False
254 needsCDecl (TyConLabel _ InfoTblVecTbl) = False
255 needsCDecl (TyConLabel _ other) = True
257 needsCDecl (AsmTempLabel _) = False
258 needsCDecl (RtsLabel _) = False
260 needsCDecl other = True
263 Whether the labelled thing can be put in C "text space":
265 isReadOnly (IdLabel _ InfoTbl) = True -- info-tables: yes
266 isReadOnly (IdLabel _ StaticInfoTbl) = True -- and so on, for other
267 isReadOnly (IdLabel _ PhantomInfoTbl) = True
268 isReadOnly (IdLabel _ (VapInfoTbl _)) = True
269 isReadOnly (IdLabel _ other) = False -- others: pessimistically, no
271 isReadOnly (TyConLabel _ _) = True
272 isReadOnly (CaseLabel _ _) = True
273 isReadOnly (AsmTempLabel _) = True
274 isReadOnly (RtsLabel _) = True
277 Whether the label is an assembler temporary:
279 isAsmTemp (AsmTempLabel _) = True
283 C ``static'' or not...
285 externallyVisibleCLabel (TyConLabel tc _) = True
286 externallyVisibleCLabel (CaseLabel _ _) = False
287 externallyVisibleCLabel (AsmTempLabel _) = False
288 externallyVisibleCLabel (RtsLabel _) = True
289 externallyVisibleCLabel (IdLabel (CLabelId id) _)
290 | isDataCon id = True
291 | is_ConstMethodId id = True -- These are here to ensure splitting works
292 | isDictFunId id = True -- when these values have not been exported
293 | isClassOpId id = True
294 | is_DefaultMethodId id = True
295 | is_SuperDictSelId id = True
296 | otherwise = externallyVisibleId id
298 is_ConstMethodId id = maybeToBool (isConstMethodId_maybe id)
299 is_DefaultMethodId id = maybeToBool (isDefaultMethodId_maybe id)
300 is_SuperDictSelId id = maybeToBool (isSuperDictSelId_maybe id)
303 These GRAN functions are needed for spitting out GRAN_FETCH() at the
304 right places. It is used to detect when the abstractC statement of an
305 CCodeBlock actually contains the code for a slow entry point. -- HWL
310 isSlowEntryCCodeBlock :: CLabel -> Bool
311 isSlowEntryCCodeBlock _ = False
312 -- Worth keeping? ToDo (WDP)
317 We need at least @Eq@ for @CLabels@, because we want to avoid
318 duplicate declarations in generating C (see @labelSeenTE@ in
322 pprCLabel :: PprStyle -> CLabel -> Unpretty
324 pprCLabel (PprForAsm _ _ fmtAsmLbl) (AsmTempLabel u)
325 = uppStr (fmtAsmLbl (_UNPK_ (showUnique u)))
327 pprCLabel (PprForAsm sw_chker prepend_cSEP _) lbl
329 then uppBeside pp_cSEP prLbl
332 prLbl = pprCLabel (PprForC sw_chker) lbl
334 pprCLabel sty (TyConLabel tc UnvecConUpdCode)
335 = uppBesides [uppPStr SLIT("ret"), pp_cSEP, uppStr (showTyCon sty tc),
336 pp_cSEP, uppPStr SLIT("upd")]
338 pprCLabel sty (TyConLabel tc (VecConUpdCode tag))
339 = uppBesides [uppPStr SLIT("ret"), pp_cSEP, uppStr (showTyCon sty tc), pp_cSEP,
340 uppInt tag, pp_cSEP, uppPStr SLIT("upd")]
342 pprCLabel sty (TyConLabel tc (StdUpdCode tag))
343 = case (ctrlReturnConvAlg tc) of
344 UnvectoredReturn _ -> uppPStr SLIT("IndUpdRetDir")
345 VectoredReturn _ -> uppBeside (uppPStr SLIT("IndUpdRetV")) (uppInt (tag - fIRST_TAG))
347 pprCLabel sty (TyConLabel tc InfoTblVecTbl)
348 = uppBesides [uppStr (showTyCon sty tc), pp_cSEP, uppPStr SLIT("itblvtbl")]
350 pprCLabel sty (TyConLabel tc StdUpdVecTbl)
351 = uppBesides [uppPStr SLIT("vtbl"), pp_cSEP, uppStr (showTyCon sty tc),
352 pp_cSEP, uppPStr SLIT("upd")]
354 pprCLabel sty (CaseLabel u CaseReturnPt)
355 = uppBesides [uppPStr SLIT("ret"), pp_cSEP, ppr_u u]
356 pprCLabel sty (CaseLabel u CaseVecTbl)
357 = uppBesides [uppPStr SLIT("vtbl"), pp_cSEP, ppr_u u]
358 pprCLabel sty (CaseLabel u (CaseAlt tag))
359 = uppBesides [uppPStr SLIT("djn"), pp_cSEP, ppr_u u, pp_cSEP, uppInt tag]
360 pprCLabel sty (CaseLabel u CaseDefault)
361 = uppBesides [uppPStr SLIT("djn"), pp_cSEP, ppr_u u]
363 pprCLabel sty (RtsLabel RtsShouldNeverHappenCode) = uppPStr SLIT("StdErrorCode")
365 pprCLabel sty (RtsLabel RtsBlackHoleInfoTbl) = uppPStr SLIT("BH_UPD_info")
367 pprCLabel sty (RtsLabel (RtsSelectorInfoTbl upd_reqd offset))
368 = uppBesides [uppPStr SLIT("__sel_info_"), uppStr (show offset),
369 uppStr (if upd_reqd then "upd" else "noupd"),
372 pprCLabel sty (RtsLabel (RtsSelectorEntry upd_reqd offset))
373 = uppBesides [uppPStr SLIT("__sel_entry_"), uppStr (show offset),
374 uppStr (if upd_reqd then "upd" else "noupd"),
377 pprCLabel sty (IdLabel (CLabelId id) flavor)
378 = uppBeside (prettyToUn (ppr sty id)) (ppFlavor flavor)
380 ppr_u u = prettyToUn (pprUnique u)
382 ppFlavor :: IdLabelInfo -> Unpretty
384 ppFlavor x = uppBeside pp_cSEP
386 Closure -> uppPStr SLIT("closure")
387 InfoTbl -> uppPStr SLIT("info")
388 EntryStd -> uppPStr SLIT("entry")
389 EntryFast arity -> --false:ASSERT (arity > 0)
390 uppBeside (uppPStr SLIT("fast")) (uppInt arity)
391 ConEntry -> uppPStr SLIT("entry")
392 StaticConEntry -> uppPStr SLIT("static_entry")
393 StaticInfoTbl -> uppPStr SLIT("static_info")
394 PhantomInfoTbl -> uppPStr SLIT("inregs_info")
395 VapInfoTbl True -> uppPStr SLIT("vap_info")
396 VapInfoTbl False -> uppPStr SLIT("vap_noupd_info")
397 VapEntry True -> uppPStr SLIT("vap_entry")
398 VapEntry False -> uppPStr SLIT("vap_noupd_entry")
399 RednCounts -> uppPStr SLIT("ct")