[project @ 1996-01-08 20:28:12 by partain]
[ghc-hetmet.git] / ghc / compiler / prelude / AbsPrel.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1995
3 %
4 \section[AbsPrel]{The @AbsPrel@ interface to the compiler's prelude knowledge}
5
6 \begin{code}
7 #include "HsVersions.h"
8
9 module AbsPrel (
10
11 -- unlike most export lists, this one is actually interesting :-)
12
13         -- re-export some PrimOp stuff:
14         PrimOp(..), typeOfPrimOp, primOpNameInfo,
15         HeapRequirement(..), primOpHeapReq, primOpCanTriggerGC, 
16         primOpNeedsWrapper, primOpOkForSpeculation, primOpIsCheap,
17         fragilePrimOp,
18         PrimOpResultInfo(..), getPrimOpResultInfo,
19         pprPrimOp, showPrimOp, isCompareOp,
20         readUnfoldingPrimOp,  -- actually, defined herein
21
22         pRELUDE, pRELUDE_BUILTIN, pRELUDE_CORE, pRELUDE_RATIO,
23         pRELUDE_LIST, pRELUDE_TEXT, --OLD: pRELUDE_ARRAY, pRELUDE_COMPLEX,
24         pRELUDE_PRIMIO, pRELUDE_IO, pRELUDE_PS,
25         gLASGOW_ST, {-gLASGOW_IO,-} gLASGOW_MISC,
26
27         -- lookup functions for built-in names, for the renamer:
28         builtinNameInfo,
29
30         -- *odd* values that need to be reached out and grabbed:
31         eRROR_ID, pAT_ERROR_ID, aBSENT_ERROR_ID,
32         unpackCStringId, packStringForCId, unpackCStringAppendId,
33         integerZeroId, integerPlusOneId, integerMinusOneId,
34
35 #ifdef DPH
36         -- ProcessorClass
37         toPodId,
38
39         -- Pid Class
40         fromDomainId, toDomainId,
41 #endif {- Data Parallel Haskell -}
42
43         -----------------------------------------------------
44         -- the rest of the export list is organised by *type*
45         -----------------------------------------------------
46
47         -- "type": functions ("arrow" type constructor)
48         mkFunTy,
49
50         -- type: Bool
51         boolTyCon, boolTy, falseDataCon, trueDataCon,
52
53         -- types: Char#, Char, String (= [Char])
54         charPrimTy, charTy, stringTy,
55         charPrimTyCon, charTyCon, charDataCon,
56
57         -- type: CMP_TAG (used in deriving)
58         cmpTagTy, ltPrimDataCon, eqPrimDataCon, gtPrimDataCon,
59
60         -- types: Double#, Double
61         doublePrimTy, doubleTy,
62         doublePrimTyCon, doubleTyCon, doubleDataCon,
63
64         -- types: Float#, Float
65         floatPrimTy, floatTy,
66         floatPrimTyCon, floatTyCon, floatDataCon,
67
68         -- types: Glasgow *primitive* arrays, sequencing and I/O
69         mkPrimIoTy, -- to typecheck "mainIO", "mainPrimIO" & for _ccall_s
70         realWorldStatePrimTy, realWorldStateTy{-boxed-},
71         realWorldTy, realWorldTyCon, realWorldPrimId,
72         stateDataCon, getStatePairingConInfo,
73
74         -- types: Void# (only used within the compiler)
75         voidPrimTy, voidPrimId,
76
77         -- types: Addr#, Int#, Word#, Int
78         intPrimTy, intTy, intPrimTyCon, intTyCon, intDataCon,
79         wordPrimTyCon, wordPrimTy, wordTy, wordDataCon,
80         addrPrimTyCon, addrPrimTy, addrTy, addrDataCon,
81
82         -- types: Integer, Rational (= Ratio Integer)
83         integerTy, rationalTy,
84         integerTyCon, rationalTyCon, ratioDataCon,
85
86         -- type: Lift
87         liftTyCon, liftDataCon, mkLiftTy,
88
89         -- type: List
90         listTyCon, mkListTy, nilDataCon, consDataCon,
91         -- NOT USED: buildDataCon,
92
93         -- type: tuples
94         mkTupleTy, unitTy,
95
96         -- packed Strings
97 --      packedStringTyCon, packedStringTy, psDataCon, cpsDataCon,
98
99         -- for compilation of List Comprehensions and foldr
100         foldlId, foldrId, mkFoldl, mkFoldr, mkBuild, buildId,
101
102 #ifdef DPH
103         mkProcessorTy,
104         mkPodTy, mkPodNTy, podTyCon,                         -- user model
105         mkPodizedPodNTy,                                     -- podized model
106         mkInterfacePodNTy, interfacePodTyCon, mKINTERPOD_ID, -- interface model
107
108         -- Misc used during podization
109         primIfromPodNSelectorId,
110 #endif {- Data Parallel Haskell -}
111
112         -- and, finally, we must put in some (abstract) data types,
113         -- to make the interface self-sufficient
114         GlobalSwitch, Id, Maybe, Name, PprStyle, PrimKind, HeapOffset,
115         TyCon, UniType, TauType(..), Unique, CoreExpr, PlainCoreExpr(..)
116
117         IF_ATTACK_PRAGMAS(COMMA tagOf_PrimOp)
118         IF_ATTACK_PRAGMAS(COMMA mkStatePrimTy)
119
120 #ifndef __GLASGOW_HASKELL__
121         ,TAG_
122 #endif
123     ) where
124
125 #ifdef DPH
126 import TyPod
127 import TyProcs
128 #endif {- Data Parallel Haskell -}
129
130 import PrelFuns         -- help functions, types and things
131 import PrimKind
132
133 import TysPrim          -- TYPES
134 import TysWiredIn
135 import PrelVals         -- VALUES
136 import PrimOps          -- PRIMITIVE OPS
137
138 import AbsUniType       ( getTyConDataCons, TyCon
139                           IF_ATTACK_PRAGMAS(COMMA cmpTyCon)
140                         )
141 import CmdLineOpts      ( GlobalSwitch(..) )
142 import FiniteMap
143 import Id               ( Id )
144 --OLD:import NameEnv
145 import Maybes
146 import Unique           -- *Key stuff
147 import Util
148 \end{code}
149
150 This little devil is too small to merit its own ``TyFun'' module:
151
152 \begin{code}
153 mkFunTy = UniFun
154 \end{code}
155
156 %************************************************************************
157 %*                                                                      *
158 \subsection[builtinNameInfo]{Lookup built-in names}
159 %*                                                                      *
160 %************************************************************************
161
162 We have two ``builtin name funs,'' one to look up @TyCons@ and
163 @Classes@, the other to look up values.
164
165 \begin{code}
166 builtinNameInfo :: (GlobalSwitch -> Bool)       -- access to global cmd-line flags
167                 -> (FAST_STRING -> Maybe Name,  -- name lookup fn for values
168                     FAST_STRING -> Maybe Name)  -- name lookup fn for tycons/classes
169
170 builtinNameInfo switch_is_on
171   = (init_val_lookup_fn, init_tc_lookup_fn)
172   where
173     --
174     -- values (including data constructors)
175     --
176     init_val_lookup_fn
177       = if      switch_is_on HideBuiltinNames then
178                 (\ x -> Nothing)
179         else if switch_is_on HideMostBuiltinNames then
180                 lookupFM (listToFM min_val_assoc_list)
181                 -- OLD: mkStringLookupFn min_val_assoc_list False{-not pre-sorted-}
182         else
183                 lookupFM (listToFM (concat list_of_val_assoc_lists))
184                 -- mkStringLookupFn (concat list_of_val_assoc_lists) False{-not pre-sorted-}
185
186     min_val_assoc_list          -- this is an ad-hoc list; what "happens"
187         =  totally_wired_in_Ids -- to be needed (when compiling bits of
188         ++ unboxed_ops          -- Prelude).
189         ++ (concat (map pcDataConNameInfo min_nonprim_tycon_list))
190
191     -- We let a lot of "non-standard" values be visible, so that we
192     -- can make sense of them in interface pragmas.  It's cool, though
193     -- -- they all have "non-standard" names, so they won't get past
194     -- the parser in user code.
195     list_of_val_assoc_lists
196         = [ -- each list is empty or all there
197
198             totally_wired_in_Ids,
199
200             concat (map pcDataConNameInfo data_tycons),
201
202             unboxed_ops,
203
204             if switch_is_on ForConcurrent then parallel_vals else []
205           ]
206
207     --
208     -- type constructors and classes
209     --
210     init_tc_lookup_fn
211       = if      switch_is_on HideBuiltinNames then
212                 (\ x -> Nothing)
213         else if switch_is_on HideMostBuiltinNames then
214                 lookupFM (listToFM min_tc_assoc_list)
215                 --OLD: mkStringLookupFn min_tc_assoc_list False{-not pre-sorted-}
216         else
217                 lookupFM (listToFM (
218                 -- OLD: mkStringLookupFn
219                     map pcTyConNameInfo (data_tycons ++ synonym_tycons)
220                     ++ std_tycon_list -- TyCons not quite so wired in
221                     ++ std_class_list
222                     ++ prim_tys))
223                     -- The prim_tys,etc., are OK, because they all
224                     -- have "non-standard" names (and we really
225                     -- want them for interface pragmas).
226                   --OLD: False{-not pre-sorted-}
227
228     min_tc_assoc_list   -- again, pretty ad-hoc
229         = prim_tys ++ (map pcTyConNameInfo min_nonprim_tycon_list)
230 --HA!     ++ std_class_list -- no harm in this
231
232 min_nonprim_tycon_list -- used w/ HideMostBuiltinNames
233   = [ boolTyCon,
234       cmpTagTyCon,
235       charTyCon,
236       intTyCon,
237       floatTyCon,
238       doubleTyCon,
239       integerTyCon,
240       ratioTyCon,
241       return2GMPsTyCon, -- ADR asked for these last two (WDP 94/11)
242       returnIntAndGMPTyCon ]
243
244 -- sigh: I (WDP) think these should be local defns
245 -- but you cannot imagine how bad it is for speed (w/ GHC)
246 prim_tys    = map pcTyConNameInfo prim_tycons
247
248 -- values
249
250 totally_wired_in_Ids
251   = [(SLIT(":"),                WiredInVal consDataCon),
252      (SLIT("error"),            WiredInVal eRROR_ID),
253      (SLIT("patError#"),        WiredInVal pAT_ERROR_ID), -- occurs in i/faces
254      (SLIT("parError#"),        WiredInVal pAR_ERROR_ID), -- ditto
255      (SLIT("_trace"),           WiredInVal tRACE_ID),
256
257      -- now the build  / foldr Id, which needs to be built in
258      (SLIT("_build"),           WiredInVal buildId),
259      (SLIT("foldl"),            WiredInVal foldlId),
260      (SLIT("foldr"),            WiredInVal foldrId),
261      (SLIT("_runST"),           WiredInVal runSTId),
262      (SLIT("realWorld#"),       WiredInVal realWorldPrimId)
263     ]
264
265 parallel_vals
266   =[(SLIT("_seq_"),             WiredInVal seqId),
267     (SLIT("_par_"),             WiredInVal parId),
268     (SLIT("_fork_"),            WiredInVal forkId)
269 #ifdef GRAN
270     ,
271     (SLIT("_parLocal_"),        WiredInVal parLocalId),
272     (SLIT("_parGlobal_"),       WiredInVal parGlobalId)
273     -- Add later:
274     -- (SLIT("_parAt_"),        WiredInVal parAtId)
275     -- (SLIT("_parAtForNow_"),  WiredInVal parAtForNowId)
276     -- (SLIT("_copyable_"),     WiredInVal copyableId)
277     -- (SLIT("_noFollow_"),     WiredInVal noFollowId)
278 #endif {-GRAN-}
279    ]
280
281 unboxed_ops
282   = (map primOpNameInfo lots_of_primops)
283    ++
284     -- plus some of the same ones but w/ different names
285    [case (primOpNameInfo IntAddOp)      of (_,n) -> (SLIT("+#"),   n),
286     case (primOpNameInfo IntSubOp)      of (_,n) -> (SLIT("-#"),   n),
287     case (primOpNameInfo IntMulOp)      of (_,n) -> (SLIT("*#"),   n),
288     case (primOpNameInfo IntGtOp)       of (_,n) -> (SLIT(">#"),   n),
289     case (primOpNameInfo IntGeOp)       of (_,n) -> (SLIT(">=#"),  n),
290     case (primOpNameInfo IntEqOp)       of (_,n) -> (SLIT("==#"),  n),
291     case (primOpNameInfo IntNeOp)       of (_,n) -> (SLIT("/=#"),  n),
292     case (primOpNameInfo IntLtOp)       of (_,n) -> (SLIT("<#"),   n),
293     case (primOpNameInfo IntLeOp)       of (_,n) -> (SLIT("<=#"),  n),
294     case (primOpNameInfo DoubleAddOp)   of (_,n) -> (SLIT("+##"),  n),
295     case (primOpNameInfo DoubleSubOp)   of (_,n) -> (SLIT("-##"),  n),
296     case (primOpNameInfo DoubleMulOp)   of (_,n) -> (SLIT("*##"),  n),
297     case (primOpNameInfo DoubleDivOp)   of (_,n) -> (SLIT("/##"),  n),
298     case (primOpNameInfo DoublePowerOp) of (_,n) -> (SLIT("**##"), n),
299     case (primOpNameInfo DoubleGtOp)    of (_,n) -> (SLIT(">##"),  n),
300     case (primOpNameInfo DoubleGeOp)    of (_,n) -> (SLIT(">=##"), n),
301     case (primOpNameInfo DoubleEqOp)    of (_,n) -> (SLIT("==##"), n),
302     case (primOpNameInfo DoubleNeOp)    of (_,n) -> (SLIT("/=##"), n),
303     case (primOpNameInfo DoubleLtOp)    of (_,n) -> (SLIT("<##"),  n),
304     case (primOpNameInfo DoubleLeOp)    of (_,n) -> (SLIT("<=##"), n)]
305
306 prim_tycons
307   = [addrPrimTyCon,
308      arrayPrimTyCon,
309      byteArrayPrimTyCon,
310      charPrimTyCon,
311      doublePrimTyCon,
312      floatPrimTyCon,
313      intPrimTyCon,
314      mallocPtrPrimTyCon,
315      mutableArrayPrimTyCon,
316      mutableByteArrayPrimTyCon,
317      synchVarPrimTyCon,
318      realWorldTyCon,
319      stablePtrPrimTyCon,
320      statePrimTyCon,
321      wordPrimTyCon
322     ]
323
324 std_tycon_list
325   = let
326         swizzle_over (mod, nm, key, arity, is_data)
327           = let
328                 fname = mkPreludeCoreName mod nm
329             in
330             (nm, PreludeTyCon key fname arity is_data)
331     in
332     map swizzle_over
333         [--(pRELUDE_IO,    SLIT("Request"),  requestTyConKey,  0, True),
334 --OLD:   (pRELUDE_IO,      SLIT("Response"), responseTyConKey, 0, True),
335          (pRELUDE_IO,      SLIT("Dialogue"), dialogueTyConKey, 0, False),
336          (SLIT("PreludeMonadicIO"), SLIT("IO"), iOTyConKey,    1, False)
337         ]
338
339 -- Several of these are non-std, but they have non-std
340 -- names, so they won't get past the parser in user code
341 -- (but will be visible for interface-pragma purposes).
342
343 data_tycons
344   = [addrTyCon,
345      boolTyCon,
346 --   byteArrayTyCon,
347      charTyCon,
348      cmpTagTyCon,
349      doubleTyCon,
350      floatTyCon,
351      intTyCon,
352      integerTyCon,
353      liftTyCon,
354      mallocPtrTyCon,
355 --   mutableArrayTyCon,
356 --   mutableByteArrayTyCon,
357      ratioTyCon,
358      return2GMPsTyCon,
359      returnIntAndGMPTyCon,
360      stablePtrTyCon,
361      stateAndAddrPrimTyCon,
362      stateAndArrayPrimTyCon,
363      stateAndByteArrayPrimTyCon,
364      stateAndCharPrimTyCon,
365      stateAndDoublePrimTyCon,
366      stateAndFloatPrimTyCon,
367      stateAndIntPrimTyCon,
368      stateAndMallocPtrPrimTyCon,
369      stateAndMutableArrayPrimTyCon,
370      stateAndMutableByteArrayPrimTyCon,
371      stateAndSynchVarPrimTyCon,
372      stateAndPtrPrimTyCon,
373      stateAndStablePtrPrimTyCon,
374      stateAndWordPrimTyCon,
375      stateTyCon,
376      wordTyCon
377 #ifdef DPH
378      ,podTyCon
379 #endif {- Data Parallel Haskell -}
380     ]
381
382 synonym_tycons
383   = [primIoTyCon,
384      rationalTyCon,
385      stTyCon,
386      stringTyCon]
387
388 std_class_list
389   = let
390         swizzle_over (str, key)
391           = (str, PreludeClass key (mkPreludeCoreName pRELUDE_CORE str))
392     in
393     map swizzle_over
394         [(SLIT("Eq"),           eqClassKey),
395          (SLIT("Ord"),          ordClassKey),
396          (SLIT("Num"),          numClassKey),
397          (SLIT("Real"),         realClassKey),
398          (SLIT("Integral"),     integralClassKey),
399          (SLIT("Fractional"),   fractionalClassKey),
400          (SLIT("Floating"),     floatingClassKey),
401          (SLIT("RealFrac"),     realFracClassKey),
402          (SLIT("RealFloat"),    realFloatClassKey),
403          (SLIT("Ix"),           ixClassKey),
404          (SLIT("Enum"),         enumClassKey),
405          (SLIT("Text"),         textClassKey),
406          (SLIT("_CCallable"),   cCallableClassKey),
407          (SLIT("_CReturnable"), cReturnableClassKey),
408          (SLIT("Binary"),       binaryClassKey)
409 #ifdef DPH
410          , (SLIT("Pid"),        pidClassKey)
411          , (SLIT("Processor"),processorClassKey)
412 #endif {- Data Parallel Haskell -}
413         ]
414
415 lots_of_primops
416   = [   CharGtOp,
417         CharGeOp,
418         CharEqOp,
419         CharNeOp,
420         CharLtOp,
421         CharLeOp,
422         IntGtOp,
423         IntGeOp,
424         IntEqOp,
425         IntNeOp,
426         IntLtOp,
427         IntLeOp,
428         WordGtOp,
429         WordGeOp,
430         WordEqOp,
431         WordNeOp,
432         WordLtOp,
433         WordLeOp,
434         AddrGtOp,
435         AddrGeOp,
436         AddrEqOp,
437         AddrNeOp,
438         AddrLtOp,
439         AddrLeOp,
440         FloatGtOp,
441         FloatGeOp,
442         FloatEqOp,
443         FloatNeOp,
444         FloatLtOp,
445         FloatLeOp,
446         DoubleGtOp,
447         DoubleGeOp,
448         DoubleEqOp,
449         DoubleNeOp,
450         DoubleLtOp,
451         DoubleLeOp,
452         OrdOp,
453         ChrOp,
454         IntAddOp,
455         IntSubOp,
456         IntMulOp,
457         IntQuotOp,
458         IntDivOp,
459         IntRemOp,
460         IntNegOp,
461         AndOp,
462         OrOp,
463         NotOp,
464         SllOp,
465         SraOp,
466         SrlOp,
467         ISllOp,
468         ISraOp,
469         ISrlOp,
470         Int2WordOp,
471         Word2IntOp,
472         Int2AddrOp,
473         Addr2IntOp,
474         FloatAddOp,
475         FloatSubOp,
476         FloatMulOp,
477         FloatDivOp,
478         FloatNegOp,
479         Float2IntOp,
480         Int2FloatOp,
481         FloatExpOp,
482         FloatLogOp,
483         FloatSqrtOp,
484         FloatSinOp,
485         FloatCosOp,
486         FloatTanOp,
487         FloatAsinOp,
488         FloatAcosOp,
489         FloatAtanOp,
490         FloatSinhOp,
491         FloatCoshOp,
492         FloatTanhOp,
493         FloatPowerOp,
494         DoubleAddOp,
495         DoubleSubOp,
496         DoubleMulOp,
497         DoubleDivOp,
498         DoubleNegOp,
499         Double2IntOp,
500         Int2DoubleOp,
501         Double2FloatOp,
502         Float2DoubleOp,
503         DoubleExpOp,
504         DoubleLogOp,
505         DoubleSqrtOp,
506         DoubleSinOp,
507         DoubleCosOp,
508         DoubleTanOp,
509         DoubleAsinOp,
510         DoubleAcosOp,
511         DoubleAtanOp,
512         DoubleSinhOp,
513         DoubleCoshOp,
514         DoubleTanhOp,
515         DoublePowerOp,
516         IntegerAddOp,
517         IntegerSubOp,
518         IntegerMulOp,
519         IntegerQuotRemOp,
520         IntegerDivModOp,
521         IntegerNegOp,
522         IntegerCmpOp,
523         Integer2IntOp,
524         Int2IntegerOp,
525         Word2IntegerOp,
526         Addr2IntegerOp,
527         FloatEncodeOp,
528         FloatDecodeOp,
529         DoubleEncodeOp,
530         DoubleDecodeOp,
531         NewArrayOp,
532         NewByteArrayOp CharKind,
533         NewByteArrayOp IntKind,
534         NewByteArrayOp AddrKind,
535         NewByteArrayOp FloatKind,
536         NewByteArrayOp DoubleKind,
537         SameMutableArrayOp,
538         SameMutableByteArrayOp,
539         ReadArrayOp,
540         WriteArrayOp,
541         IndexArrayOp,
542         ReadByteArrayOp CharKind,
543         ReadByteArrayOp IntKind,
544         ReadByteArrayOp AddrKind,
545         ReadByteArrayOp FloatKind,
546         ReadByteArrayOp DoubleKind,
547         WriteByteArrayOp CharKind,
548         WriteByteArrayOp IntKind,
549         WriteByteArrayOp AddrKind,
550         WriteByteArrayOp FloatKind,
551         WriteByteArrayOp DoubleKind,
552         IndexByteArrayOp CharKind,
553         IndexByteArrayOp IntKind,
554         IndexByteArrayOp AddrKind,
555         IndexByteArrayOp FloatKind,
556         IndexByteArrayOp DoubleKind,
557         IndexOffAddrOp CharKind,
558         IndexOffAddrOp IntKind,
559         IndexOffAddrOp AddrKind,
560         IndexOffAddrOp FloatKind,
561         IndexOffAddrOp DoubleKind,
562         UnsafeFreezeArrayOp,
563         UnsafeFreezeByteArrayOp,
564         NewSynchVarOp,
565         ReadArrayOp,
566         TakeMVarOp,
567         PutMVarOp,
568         ReadIVarOp,
569         WriteIVarOp,
570         MakeStablePtrOp,
571         DeRefStablePtrOp,
572         ReallyUnsafePtrEqualityOp,
573         ErrorIOPrimOp,
574 #ifdef GRAN
575         ParGlobalOp,
576         ParLocalOp,
577 #endif {-GRAN-}
578         SeqOp,
579         ParOp,
580         ForkOp,
581         DelayOp,
582         WaitOp
583     ]
584 \end{code}
585
586 \begin{code}
587 readUnfoldingPrimOp :: FAST_STRING -> PrimOp
588
589 readUnfoldingPrimOp
590   = let
591         -- "reverse" lookup table
592         tbl = map (\ o -> let { (str,_) = primOpNameInfo o } in (str, o)) lots_of_primops
593     in
594     \ str -> case [ op | (s, op) <- tbl, s == str ] of
595                (op:_) -> op
596 #ifdef DEBUG
597                [] -> panic "readUnfoldingPrimOp" -- ++ _UNPK_ str ++"::"++show (map fst tbl))
598 #endif
599 \end{code}
600
601 Make table entries for various things:
602 \begin{code}
603 pcTyConNameInfo :: TyCon -> (FAST_STRING, Name)
604 pcTyConNameInfo tycon
605   = (getOccurrenceName tycon, WiredInTyCon tycon)
606
607 pcDataConNameInfo :: TyCon -> [(FAST_STRING, Name)]
608 pcDataConNameInfo tycon
609   = -- slurp out its data constructors...
610     [(getOccurrenceName con, WiredInVal con) | con <- getTyConDataCons tycon]
611 \end{code}