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