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