2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[PrelVals]{Prelude values the compiler ``knows about''}
9 #include "HsVersions.h"
11 import {-# SOURCE #-} CoreUnfold ( UnfoldingGuidance(..), mkUnfolding )
13 import Id ( Id, mkImported, mkTemplateLocals )
14 import SpecEnv ( SpecEnv, emptySpecEnv )
22 import CmdLineOpts ( maybe_CompilingGhcInternals )
23 import CoreSyn -- quite a bit
24 import IdInfo -- quite a bit
25 import Literal ( mkMachInt )
26 import Name ( mkWiredInIdName, Module )
28 import PrimOp ( PrimOp(..) )
30 import TyVar ( openAlphaTyVar, alphaTyVar, betaTyVar, gammaTyVar, TyVar )
31 import Unique -- lots of *Keys
38 mk_inline_unfolding = mkUnfolding IWantToBeINLINEd
40 pcMiscPrelId :: Unique{-IdKey-} -> Module -> FAST_STRING -> Type -> IdInfo -> Id
42 pcMiscPrelId key mod occ ty info
44 name = mkWiredInIdName key mod occ imp
45 imp = mkImported name ty info -- the usual case...
48 -- We lie and say the thing is imported; otherwise, we get into
49 -- a mess with dependency analysis; e.g., core2stg may heave in
50 -- random calls to GHCbase.unpackPS__. If GHCbase is the module
51 -- being compiled, then it's just a matter of luck if the definition
52 -- will be in "the right place" to be in scope.
55 %************************************************************************
57 \subsection[PrelVals-error-related]{@error@ and friends; @trace@}
59 %************************************************************************
61 GHC randomly injects these into the code.
63 @patError@ is just a version of @error@ for pattern-matching
64 failures. It knows various ``codes'' which expand to longer
65 strings---this saves space!
67 @absentErr@ is a thing we put in for ``absent'' arguments. They jolly
68 well shouldn't be yanked on, but if one is, then you will get a
69 friendly message from @absentErr@ (rather a totally random crash).
71 @parError@ is a special version of @error@ which the compiler does
72 not know to be a bottoming Id. It is used in the @_par_@ and @_seq_@
73 templates, but we don't ever expect to generate code for it.
76 pc_bottoming_Id key mod name ty
77 = pcMiscPrelId key mod name ty bottoming_info
79 bottoming_info = noIdInfo `addStrictnessInfo` mkBottomStrictnessInfo
80 -- these "bottom" out, no matter what their arguments
83 = pc_bottoming_Id errorIdKey pREL_ERR SLIT("error") errorTy
86 = pc_bottoming_Id u pREL_ERR n errorTy
89 = generic_ERROR_ID patErrorIdKey SLIT("patError")
91 = generic_ERROR_ID recConErrorIdKey SLIT("recConError")
93 = generic_ERROR_ID recUpdErrorIdKey SLIT("recUpdError")
95 = generic_ERROR_ID irrefutPatErrorIdKey SLIT("irrefutPatError")
96 nON_EXHAUSTIVE_GUARDS_ERROR_ID
97 = generic_ERROR_ID nonExhaustiveGuardsErrorIdKey SLIT("nonExhaustiveGuardsError")
98 nO_METHOD_BINDING_ERROR_ID
99 = generic_ERROR_ID noMethodBindingErrorIdKey SLIT("noMethodBindingError")
102 = pc_bottoming_Id absentErrorIdKey pREL_ERR SLIT("absentErr")
103 (mkSigmaTy [openAlphaTyVar] [] openAlphaTy)
106 = pcMiscPrelId parErrorIdKey pREL_ERR SLIT("parError")
107 (mkSigmaTy [openAlphaTyVar] [] openAlphaTy) noIdInfo
109 openAlphaTy = mkTyVarTy openAlphaTyVar
112 errorTy = mkSigmaTy [openAlphaTyVar] [] (mkFunTys [mkListTy charTy] openAlphaTy)
113 -- Notice the openAlphaTyVar. It says that "error" can be applied
114 -- to unboxed as well as boxed types. This is OK because it never
115 -- returns, so the return type is irrelevant.
118 We want \tr{GHCbase.trace} to be wired in
119 because we don't want the strictness analyser to get ahold of it,
120 decide that the second argument is strict, evaluate that first (!!),
121 and make a jolly old mess.
124 = pcMiscPrelId traceIdKey pREL_IO_BASE SLIT("trace") traceTy
125 (noIdInfo `addSpecInfo` pcGenerateSpecs traceIdKey tRACE_ID noIdInfo traceTy)
127 traceTy = mkSigmaTy [alphaTyVar] [] (mkFunTys [mkListTy charTy, alphaTy] alphaTy)
130 %************************************************************************
132 \subsection[PrelVals-Integer-support]{To support @Integer@ and @String@ literals}
134 %************************************************************************
138 = pcMiscPrelId packCStringIdKey{-ToDo:rename-} pREL_PACK SLIT("packCString#")
139 (mkFunTys [stringTy] byteArrayPrimTy) noIdInfo
141 --------------------------------------------------------------------
144 = pcMiscPrelId unpackCStringIdKey pREL_PACK SLIT("unpackCString#")
145 (mkFunTys [addrPrimTy{-a char *-}] stringTy) noIdInfo
147 -- (FunTy addrPrimTy{-a char *-} stringTy) (noIdInfo `addInfo` exactArity 1)
148 -- but I don't like wired-in IdInfos (WDP)
150 unpackCString2Id -- for cases when a string has a NUL in it
151 = pcMiscPrelId unpackCString2IdKey pREL_PACK SLIT("unpackNBytes#")
152 (mkFunTys [addrPrimTy{-a char *-}, intPrimTy{-length-}] stringTy)
155 --------------------------------------------------------------------
156 unpackCStringAppendId
157 = pcMiscPrelId unpackCStringAppendIdKey pREL_PACK SLIT("unpackAppendCString#")
158 (mkFunTys [addrPrimTy{-a "char *" pointer-},stringTy] stringTy)
160 {-LATER:`addUnfoldInfo` mkMagicUnfolding unpackCStringAppendIdKey-})
161 `addArityInfo` exactArity 2)
164 = pcMiscPrelId unpackCStringFoldrIdKey pREL_PACK SLIT("unpackFoldrCString#")
165 (mkSigmaTy [alphaTyVar] []
166 (mkFunTys [addrPrimTy{-a "char *" pointer-},
167 mkFunTys [charTy, alphaTy] alphaTy,
171 {-LATER:`addUnfoldInfo` mkMagicUnfolding unpackCStringFoldrIdKey-})
172 `addArityInfo` exactArity 3)
175 OK, this is Will's idea: we should have magic values for Integers 0,
176 +1, +2, and -1 (go ahead, fire me):
180 = pcMiscPrelId integerZeroIdKey pREL_NUM SLIT("integer_0") integerTy noIdInfo
182 = pcMiscPrelId integerPlusOneIdKey pREL_NUM SLIT("integer_1") integerTy noIdInfo
184 = pcMiscPrelId integerPlusTwoIdKey pREL_NUM SLIT("integer_2") integerTy noIdInfo
186 = pcMiscPrelId integerMinusOneIdKey pREL_NUM SLIT("integer_m1") integerTy noIdInfo
189 %************************************************************************
191 \subsection[PrelVals-parallel]{@seq@ and @par@: for parallel operation (only)}
193 %************************************************************************
197 --------------------------------------------------------------------
198 -- seqId :: "seq", used w/ GRIP, etc., is really quite similar to
202 seq = /\ a b -> \ x y -> case x of { _ -> y }
205 seq = /\ a b -> \ x y -> case seq# x y of { _Lift y' -> y' }
208 seq = /\ a b -> \ x::a y::b -> case seq# x of { 0# -> parError#; _ -> y; }
212 seqId = pcMiscPrelId seqIdKey pRELUDE SLIT("seq")
213 (mkSigmaTy [alphaTyVar, betaTyVar] []
214 (mkFunTys [alphaTy, betaTy] betaTy))
215 (noIdInfo `addUnfoldInfo` (mk_inline_unfolding seq_template))
225 = mkLam [alphaTyVar, betaTyVar] [x, y] (
226 Case (Prim SeqOp [TyArg alphaTy, VarArg x]) (
228 [(mkMachInt 0, mkTyApp (Var pAR_ERROR_ID) [betaTy])]
229 (BindDefault z (Var y))))
231 --------------------------------------------------------------------
232 -- parId :: "par", also used w/ GRIP, etc.
236 par = /\ a b -> \ x y -> case (par# (case x of { _ -> () })) of { _ -> y }
240 par = /\ a b -> \ x y -> case par# x y of { _Lift y' -> y' }
244 par = /\ a b -> \ x::a y::b -> case par# x of { 0# -> parError#; _ -> y; }
247 parId = pcMiscPrelId parIdKey cONC_BASE SLIT("par")
248 (mkSigmaTy [alphaTyVar, betaTyVar] []
249 (mkFunTys [alphaTy, betaTy] betaTy))
250 (noIdInfo `addUnfoldInfo` (mk_inline_unfolding par_template))
260 = mkLam [alphaTyVar, betaTyVar] [x, y] (
261 Case (Prim ParOp [TyArg alphaTy, VarArg x]) (
263 [(mkMachInt 0, mkTyApp (Var pAR_ERROR_ID) [betaTy])]
264 (BindDefault z (Var y))))
266 -- forkId :: "fork", for *required* concurrent threads
268 _fork_ = /\ a b -> \ x::a y::b -> case fork# x of { 0# -> parError#; _ -> y; }
270 forkId = pcMiscPrelId forkIdKey cONC_BASE SLIT("fork")
271 (mkSigmaTy [alphaTyVar, betaTyVar] []
272 (mkFunTys [alphaTy, betaTy] betaTy))
273 (noIdInfo `addUnfoldInfo` (mk_inline_unfolding fork_template))
283 = mkLam [alphaTyVar, betaTyVar] [x, y] (
284 Case (Prim ForkOp [TyArg alphaTy, VarArg x]) (
286 [(mkMachInt 0, mkTyApp (Var pAR_ERROR_ID) [betaTy])]
287 (BindDefault z (Var y))))
294 parLocalId = pcMiscPrelId parLocalIdKey cONC_BASE SLIT("parLocal")
295 (mkSigmaTy [alphaTyVar, betaTyVar] []
296 (mkFunTys [intPrimTy, intPrimTy, intPrimTy, intPrimTy, alphaTy, betaTy] betaTy))
297 (noIdInfo `addUnfoldInfo` (mk_inline_unfolding parLocal_template))
299 -- Annotations: w: name, g: gran. info, s: size info, p: par info -- HWL
300 [w, g, s, p, x, y, z]
312 = mkLam [alphaTyVar, betaTyVar] [w, g, s, p, x, y] (
313 Case (Prim ParLocalOp [TyArg alphaTy, TyArg betaTy, VarArg x, VarArg w, VarArg g, VarArg s, VarArg p, VarArg y]) (
315 [(mkMachInt 0, mkTyApp (Var pAR_ERROR_ID) [betaTy])]
316 (BindDefault z (Var y))))
318 parGlobalId = pcMiscPrelId parGlobalIdKey cONC_BASE SLIT("parGlobal")
319 (mkSigmaTy [alphaTyVar, betaTyVar] []
320 (mkFunTys [intPrimTy, intPrimTy, intPrimTy, intPrimTy, alphaTy, betaTy] betaTy))
321 (noIdInfo `addUnfoldInfo` (mk_inline_unfolding parGlobal_template))
323 -- Annotations: w: name, g: gran. info, s: size info, p: par info -- HWL
324 [w, g, s, p, x, y, z]
336 = mkLam [alphaTyVar, betaTyVar] [w, g, s, p, x, y] (
337 Case (Prim ParGlobalOp [TyArg alphaTy, TyArg betaTy, VarArg x, VarArg w, VarArg g, VarArg s, VarArg p, VarArg y]) (
339 [(mkMachInt 0, mkTyApp (Var pAR_ERROR_ID) [betaTy])]
340 (BindDefault z (Var y))))
343 parAtId = pcMiscPrelId parAtIdKey cONC_BASE SLIT("parAt")
344 (mkSigmaTy [alphaTyVar, betaTyVar] []
345 (mkFunTys [intPrimTy, intPrimTy, intPrimTy, intPrimTy,
346 alphaTy, betaTy, gammaTy] gammaTy))
347 (noIdInfo `addUnfoldInfo` (mk_inline_unfolding parAt_template))
349 -- Annotations: w: name, g: gran. info, s: size info, p: par info -- HWL
350 [w, g, s, p, v, x, y, z]
363 = mkLam [alphaTyVar, betaTyVar, gammaTyVar] [w, g, s, p, v, x, y] (
364 Case (Prim ParAtOp [TyArg alphaTy, TyArg betaTy, TyArg gammaTy, VarArg x, VarArg v, VarArg w, VarArg g, VarArg s, VarArg p, VarArg y]) (
366 [(mkMachInt 0, mkTyApp (Var pAR_ERROR_ID) [gammaTy])]
367 (BindDefault z (Var y))))
369 parAtAbsId = pcMiscPrelId parAtAbsIdKey cONC_BASE SLIT("parAtAbs")
370 (mkSigmaTy [alphaTyVar, betaTyVar] []
371 (mkFunTys [intPrimTy, intPrimTy, intPrimTy, intPrimTy, intPrimTy, alphaTy, betaTy] betaTy))
372 (noIdInfo `addUnfoldInfo` (mk_inline_unfolding parAtAbs_template))
374 -- Annotations: w: name, g: gran. info, s: size info, p: par info -- HWL
375 [w, g, s, p, v, x, y, z]
388 = mkLam [alphaTyVar, betaTyVar] [w, g, s, p, v, x, y] (
389 Case (Prim ParAtAbsOp [TyArg alphaTy, TyArg betaTy, VarArg x, VarArg v, VarArg w, VarArg g, VarArg s, VarArg p, VarArg y]) (
391 [(mkMachInt 0, mkTyApp (Var pAR_ERROR_ID) [betaTy])]
392 (BindDefault z (Var y))))
394 parAtRelId = pcMiscPrelId parAtRelIdKey cONC_BASE SLIT("parAtRel")
395 (mkSigmaTy [alphaTyVar, betaTyVar] []
396 (mkFunTys [intPrimTy, intPrimTy, intPrimTy, intPrimTy, intPrimTy, alphaTy, betaTy] betaTy))
397 (noIdInfo `addUnfoldInfo` (mk_inline_unfolding parAtRel_template))
399 -- Annotations: w: name, g: gran. info, s: size info, p: par info -- HWL
400 [w, g, s, p, v, x, y, z]
413 = mkLam [alphaTyVar, betaTyVar] [w, g, s, p, v, x, y] (
414 Case (Prim ParAtRelOp [TyArg alphaTy, TyArg betaTy, VarArg x, VarArg v, VarArg w, VarArg g, VarArg s, VarArg p, VarArg y]) (
416 [(mkMachInt 0, mkTyApp (Var pAR_ERROR_ID) [betaTy])]
417 (BindDefault z (Var y))))
419 parAtForNowId = pcMiscPrelId parAtForNowIdKey cONC_BASE SLIT("parAtForNow")
420 (mkSigmaTy [alphaTyVar, betaTyVar] []
421 (mkFunTys [intPrimTy, intPrimTy, intPrimTy, intPrimTy,
422 alphaTy, betaTy, gammaTy] gammaTy))
423 (noIdInfo `addUnfoldInfo` (mk_inline_unfolding parAtForNow_template))
425 -- Annotations: w: name, g: gran. info, s: size info, p: par info -- HWL
426 [w, g, s, p, v, x, y, z]
439 = mkLam [alphaTyVar, betaTyVar, gammaTyVar] [w, g, s, p, v, x, y] (
440 Case (Prim ParAtForNowOp [TyArg alphaTy, TyArg betaTy, TyArg gammaTy, VarArg x, VarArg v, VarArg w, VarArg g, VarArg s, VarArg p, VarArg y]) (
442 [(mkMachInt 0, mkTyApp (Var pAR_ERROR_ID) [gammaTy])]
443 (BindDefault z (Var y))))
445 -- copyable and noFollow are currently merely hooks: they are translated into
446 -- calls to the macros COPYABLE and NOFOLLOW -- HWL
448 copyableId = pcMiscPrelId copyableIdKey cONC_BASE SLIT("copyable")
449 (mkSigmaTy [alphaTyVar] []
451 (noIdInfo `addUnfoldInfo` (mk_inline_unfolding copyable_template))
453 -- Annotations: x: closure that's tagged to by copyable
461 = mkLam [alphaTyVar] [x] ( Prim CopyableOp [TyArg alphaTy, VarArg x] )
463 noFollowId = pcMiscPrelId noFollowIdKey cONC_BASE SLIT("noFollow")
464 (mkSigmaTy [alphaTyVar] []
466 (noIdInfo `addUnfoldInfo` (mk_inline_unfolding noFollow_template))
468 -- Annotations: x: closure that's tagged to not follow
476 = mkLam [alphaTyVar] [x] ( Prim NoFollowOp [TyArg alphaTy, VarArg x] )
480 @realWorld#@ used to be a magic literal, \tr{void#}. If things get
481 nasty as-is, change it back to a literal (@Literal@).
484 = pcMiscPrelId realWorldPrimIdKey pREL_GHC SLIT("realWorld#")
490 voidId = pc_bottoming_Id voidIdKey pREL_BASE SLIT("void") voidTy
493 %************************************************************************
495 \subsection[PrelVals-foldr-build]{Values known about for ``foldr/build''}
497 %************************************************************************
501 = pcMiscPrelId buildIdKey pREL_ERR SLIT("build") buildTy
503 {-LATER:`addUnfoldInfo` mkMagicUnfolding buildIdKey-})
504 `addStrictnessInfo` mkStrictnessInfo [WwStrict] False)
505 `addArgUsageInfo` mkArgUsageInfo [ArgUsage 2])
506 `addSpecInfo` pcGenerateSpecs buildIdKey buildId noIdInfo{-ToDo-} buildTy)
507 -- cheating, but since _build never actually exists ...
509 -- The type of this strange object is:
510 -- \/ a . (\/ b . (a -> b -> b) -> b -> b) -> [a]
512 buildTy = mkSigmaTy [alphaTyVar] [] (mkFunTys [build_ty] (mkListTy alphaTy))
514 build_ty = mkSigmaTy [betaTyVar] []
515 (mkFunTys [mkFunTys [alphaTy, betaTy] betaTy, betaTy] betaTy)
518 @mkBuild@ is sugar for building a build!
520 @mkbuild ty tv c n e@ $Rightarrow$ @build ty (/\ tv -> \ c n -> e)@
521 @ty@ is the type of the list.
522 @tv@ is always a new type variable.
523 @c,n@ are Id's for the abstract cons and nil, @g@ for let binding the argument argument.
526 v :: (\/ b . (a -> b -> b) -> b -> b) -> [a]
527 -- \/ a . (\/ b . (a -> b -> b) -> b -> b) -> [a]
528 @e@ is the object right inside the @build@
536 -> CoreExpr -- template
537 -> CoreExpr -- template
539 mkBuild ty tv c n g expr
540 = Let (NonRec g (mkLam [tv] [c,n] expr))
541 (App (mkTyApp (Var buildId) [ty]) (VarArg g))
546 = pcMiscPrelId augmentIdKey pREL_ERR SLIT("augment") augmentTy
548 {-LATER:`addUnfoldInfo` mkMagicUnfolding augmentIdKey-})
549 `addStrictnessInfo` mkStrictnessInfo [WwStrict,WwLazy False] False)
550 `addArgUsageInfo` mkArgUsageInfo [ArgUsage 2,UnknownArgUsage])
551 -- cheating, but since _augment never actually exists ...
553 -- The type of this strange object is:
554 -- \/ a . (\/ b . (a -> b -> b) -> b -> b) -> [a] -> [a]
556 augmentTy = mkSigmaTy [alphaTyVar] [] (mkFunTys [aug_ty, mkListTy alphaTy] (mkListTy alphaTy))
558 aug_ty = mkSigmaTy [betaTyVar] []
559 (mkFunTys [mkFunTys [alphaTy, betaTy] betaTy, betaTy] betaTy)
563 foldrId = pcMiscPrelId foldrIdKey pREL_BASE SLIT("foldr")
567 mkSigmaTy [alphaTyVar, betaTyVar] []
568 (mkFunTys [mkFunTys [alphaTy, betaTy] betaTy, betaTy, mkListTy alphaTy] betaTy)
570 idInfo = (((((noIdInfo
571 {-LATER:`addUnfoldInfo` mkMagicUnfolding foldrIdKey-})
572 `addStrictnessInfo` mkStrictnessInfo [WwLazy False,WwLazy False,WwStrict] False)
573 `addArityInfo` exactArity 3)
574 `addUpdateInfo` mkUpdateInfo [2,2,1])
575 `addSpecInfo` pcGenerateSpecs foldrIdKey foldrId noIdInfo{-ToDo-} foldrTy)
577 foldlId = pcMiscPrelId foldlIdKey pREL_LIST SLIT("foldl")
581 mkSigmaTy [alphaTyVar, betaTyVar] []
582 (mkFunTys [mkFunTys [alphaTy, betaTy] alphaTy, alphaTy, mkListTy betaTy] alphaTy)
584 idInfo = (((((noIdInfo
585 {-LATER:`addUnfoldInfo` mkMagicUnfolding foldlIdKey-})
586 `addStrictnessInfo` mkStrictnessInfo [WwLazy False,WwLazy False,WwStrict] False)
587 `addArityInfo` exactArity 3)
588 `addUpdateInfo` mkUpdateInfo [2,2,1])
589 `addSpecInfo` pcGenerateSpecs foldlIdKey foldlId noIdInfo{-ToDo-} foldlTy)
591 -- A bit of magic goes no here. We translate appendId into ++,
592 -- you have to be carefull when you actually compile append:
593 -- xs ++ ys = augment (\ c n -> foldr c n xs) ys
594 -- {- unfold augment -}
596 -- {- fold foldr to append -}
597 -- = ys `appendId` xs
598 -- = ys ++ xs -- ugg!
599 -- *BUT* you want (++) and not _append in your interfaces.
601 -- So you have to turn *off* unfolding of foldr inside FoldrBuild.hs inside
604 {- OLD: doesn't apply with 1.3
606 = pcMiscPrelId appendIdKey mONAD SLIT("++") appendTy idInfo
609 (mkSigmaTy [alphaTyVar] []
610 (mkFunTys [mkListTy alphaTy, mkListTy alphaTy] (mkListTy alphaTy)))
612 `addStrictnessInfo` mkStrictnessInfo [WwStrict,WwLazy False] False)
613 `addArityInfo` exactArity 2)
614 `addUpdateInfo` mkUpdateInfo [1,2])
618 %************************************************************************
620 \subsection[PrelUtils-specialisations]{Specialisations for builtin values}
622 %************************************************************************
624 The specialisations which exist for the builtin values must be recorded in
627 NOTE: THE USES OF THE pcGenerate... FUNCTIONS MUST CORRESPOND
628 TO THE SPECIALISATIONS DECLARED IN THE PRELUDE !!!
630 HACK: We currently use the same unique for the specialised Ids.
632 The list @specing_types@ determines the types for which specialised
633 versions are created. Note: This should correspond with the
634 types passed to the pre-processor with the -genSPECS arg (see ghc.lprl).
636 ToDo: Create single mkworld definition which is grabbed here and in ghc.lprl
639 pcGenerateSpecs :: Unique -> Id -> IdInfo -> Type -> IdSpecEnv
640 pcGenerateSpecs key id info ty
645 pc_gen_specs True key id info ty
647 pc_gen_specs is_id key id info ty
648 = mkSpecEnv spec_infos
650 spec_infos = [ let spec_ty = specialiseTy ty spec_tys 0
652 then mkSpecId key {- HACK WARNING: same unique! -}
653 id spec_tys spec_ty info
654 else panic "SpecData:SpecInfo:SpecId"
656 SpecInfo spec_tys (length ctxts) spec_id
657 | spec_tys <- specialisations ]
659 (tyvars, ctxts, _) = splitSigmaTy ty
660 no_tyvars = length tyvars
662 specialisations = if no_tyvars == 0
664 else tail (cross_product no_tyvars specing_types)
666 -- N.B. tail removes fully polymorphic specialisation
668 cross_product 0 tys = []
669 cross_product 1 tys = map (:[]) tys
670 cross_product n tys = concat [map (:cp) tys | cp <- cross_product (n-1) tys]
673 specing_types = [Nothing,