summaryrefslogtreecommitdiff
path: root/ghc/compiler/basicTypes/CLabelInfo.lhs
blob: 0e490e256712e2a368762334acc92c0659300e1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
%
% (c) The GRASP/AQUA Project, Glasgow University, 1992-1995
%
\section[CLabelInfo]{@CLabelInfo@: Information to make C Labels}

\begin{code}
#include "HsVersions.h"

module CLabelInfo (
	CLabel,	-- abstract type

	mkClosureLabel,
	mkInfoTableLabel,
	mkStdEntryLabel,
	mkFastEntryLabel,
	mkConEntryLabel,
	mkStaticConEntryLabel,
	mkRednCountsLabel,
	mkPhantomInfoTableLabel,
	mkStaticInfoTableLabel,
	mkVapEntryLabel,
	mkVapInfoTableLabel,

--UNUSED: mkConUpdCodePtrUnvecLabel,
	mkConUpdCodePtrVecLabel,
	mkStdUpdCodePtrVecLabel,

	mkInfoTableVecTblLabel,
	mkStdUpdVecTblLabel,

	mkReturnPtLabel,
	mkVecTblLabel,
	mkAltLabel,
	mkDefaultLabel,

	mkAsmTempLabel,

	mkErrorStdEntryLabel,
	mkBlackHoleInfoTableLabel,
--UNUSED: mkSelectorInfoTableLabel,
--UNUSED: mkSelectorEntryLabel,

#ifdef DPH
	mkLocalLabel, isLocalLabel, isNestableBlockLabel,
	isGlobalDataLabel, isDataLabel, 
	needsApalDecl, isVectorTableLabel, isSlowFastLabelPair,
#endif {- Data Parallel Haskell -}

	needsCDecl, isReadOnly, isAsmTemp, externallyVisibleCLabel,

	cSEP, identToC, modnameToC, stringToC, charToC, charToEasyHaskell,
	pprCLabel,

#ifdef GRAN
	isSlowEntryCCodeBlock,
#endif

	-- and to make the interface self-sufficient...
	Id, TyCon, Unique
    ) where

import AbsUniType	( showTyCon, cmpTyCon, isBigTupleTyCon,
			  TyCon, Unique
			)
import Id		( externallyVisibleId, cmpId_withSpecDataCon,
			  DataCon(..), Id, fIRST_TAG, ConTag(..)
#ifdef DPH
			 ,isInventedTopLevId
#endif {- Data Parallel Haskell -}
			)
import Outputable
import Pretty		( ppNil, ppChar, ppStr, ppPStr, ppDouble, ppInt,
			  ppInteger, ppBeside, ppIntersperse, prettyToUn
			)
#ifdef USE_ATTACK_PRAGMAS
import CharSeq
#endif
import Unpretty		-- NOTE!! ********************
import Unique		( cmpUnique, showUnique, pprUnique, Unique )
import Util

#ifdef DPH
import AbsCSyn		( MagicId )
import PprAbsC		( pprMagicId )
#endif {- Data Parallel Haskell -}

-- Sigh...  Shouldn't this file (CLabelInfo) live in codeGen?
import CgRetConv    	( CtrlReturnConvention(..), ctrlReturnConvAlg )

\end{code}

things we want to find out:

* should the labelled things be declared "static" (visible only in this file)?

* should it be declared "const" (read-only text space)?

* does it need declarations at all? (v common Prelude things are pre-declared)

\begin{code}
data CLabel
  = IdLabel	    		-- A family of labels related to the 
	CLabelId		-- definition of a particular Id
	IdLabelInfo		-- Includes DataCon

  | TyConLabel			-- A family of labels related to the
	TyCon			-- definition of a data type
	TyConLabelInfo

  | CaseLabel			-- A family of labels related to a particular case expression
	Unique			-- Unique says which case expression
	CaseLabelInfo

  | AsmTempLabel    Unique

  | RtsLabel	    RtsLabelInfo

#ifdef DPH
  | ALocalLabel     Unique	-- Label within a code block.
		    String
#endif {- Data Parallel Haskell -}
  deriving (Eq, Ord)
\end{code}

The CLabelId is simply so we can declare alternative Eq and Ord
instances which use cmpId_SpecDataCon (instead of cmpId). This avoids
comparing the Uniques of two specialised data constructors (which have
the same as the uniques their respective unspecialised data
constructors). Instead, the specialising types and the uniques of the
unspecialised constructors are compared.

\begin{code}
data CLabelId = CLabelId Id

instance Eq CLabelId where
    CLabelId a == CLabelId b = case cmpId_withSpecDataCon a b of { EQ_ -> True;  _ -> False }
    CLabelId a /= CLabelId b = case cmpId_withSpecDataCon a b of { EQ_ -> False; _ -> True  }

instance Ord CLabelId where
    CLabelId a <= CLabelId b = case cmpId_withSpecDataCon a b
	 of { LT_ -> True;  EQ_ -> True;  GT__ -> False }
    CLabelId a <  CLabelId b = case cmpId_withSpecDataCon a b
	 of { LT_ -> True;  EQ_ -> False; GT__ -> False }
    CLabelId a >= CLabelId b = case cmpId_withSpecDataCon a b
	 of { LT_ -> False; EQ_ -> True;  GT__ -> True  }
    CLabelId a >  CLabelId b = case cmpId_withSpecDataCon a b
	 of { LT_ -> False; EQ_ -> False; GT__ -> True  }
#ifdef __GLASGOW_HASKELL__
    _tagCmp (CLabelId a) (CLabelId b) = case cmpId_withSpecDataCon a b
	 of { LT_ -> _LT; EQ_ -> _EQ; GT__ -> _GT }
#endif
\end{code}

\begin{code}
data IdLabelInfo
  = Closure		-- Label for (static???) closure

  | InfoTbl		-- Info table for a closure; always read-only

  | EntryStd		-- Thunk, or "slow", code entry point (requires arg satis check)
  | EntryFast Int	-- entry pt when no arg satisfaction chk needed;
			-- Int is the arity of the function (to be
			-- encoded into the name)

  | ConEntry		-- the only kind of entry pt for constructors
  | StaticConEntry  	-- static constructor entry point

  | StaticInfoTbl   	-- corresponding info table

  | PhantomInfoTbl  	-- for phantom constructors that only exist in regs

  | VapInfoTbl Bool	-- True <=> the update-reqd version; False <=> the no-update-reqd version
  | VapEntry Bool

	-- Ticky-ticky counting
  | RednCounts		-- Label of place to keep reduction-count info for this Id
  deriving (Eq, Ord)


data TyConLabelInfo
  = UnvecConUpdCode	 -- Update code for the data type if it's unvectored
			 
  | VecConUpdCode ConTag -- One for each constructor which returns in
		    	 -- regs; this code actually performs an update
			 
  | StdUpdCode ConTag	 -- Update code for all constructors which return
    	    	    	 -- in heap.  There are a small number of variants,
    	    	    	 -- so that the update code returns (vectored/n or
			 -- unvectored) in the right way.
			 -- ToDo: maybe replace TyCon/Int with return conv.
			 
  | InfoTblVecTbl	 -- For tables of info tables
			 
  | StdUpdVecTbl	 -- Labels the update code, or table of update codes,
			 -- for a particular type.
  deriving (Eq, Ord)

data CaseLabelInfo  
  = CaseReturnPt
  | CaseVecTbl
  | CaseAlt ConTag
  | CaseDefault
  deriving (Eq, Ord)

data RtsLabelInfo
  = RtsShouldNeverHappenCode

  | RtsBlackHoleInfoTbl

  | RtsSelectorInfoTbl	-- Selectors
	Bool		-- True <=> the update-reqd version;
			-- False <=> the no-update-reqd version
	Int		-- 0-indexed Offset from the "goods"

  | RtsSelectorEntry	-- Ditto entry code
	Bool
	Int
  deriving (Eq, Ord)
\end{code}

\begin{code}
mkClosureLabel	      	id 		= IdLabel (CLabelId id) Closure
mkInfoTableLabel      	id 		= IdLabel (CLabelId id) InfoTbl
mkStdEntryLabel	      	id 		= IdLabel (CLabelId id) EntryStd
mkFastEntryLabel      	id arity	= --false:ASSERT(arity > 0)
					  IdLabel (CLabelId id) (EntryFast arity)
mkConEntryLabel	      	id		= IdLabel (CLabelId id) ConEntry
mkStaticConEntryLabel 	id		= IdLabel (CLabelId id) StaticConEntry
mkRednCountsLabel     	id		= IdLabel (CLabelId id) RednCounts
mkPhantomInfoTableLabel id		= IdLabel (CLabelId id) PhantomInfoTbl
mkStaticInfoTableLabel  id		= IdLabel (CLabelId id) StaticInfoTbl
mkVapEntryLabel		id upd_flag	= IdLabel (CLabelId id) (VapEntry upd_flag)
mkVapInfoTableLabel	id upd_flag	= IdLabel (CLabelId id) (VapInfoTbl upd_flag)

--UNUSED:mkConUpdCodePtrUnvecLabel tycon     = TyConLabel tycon UnvecConUpdCode
mkConUpdCodePtrVecLabel   tycon tag = TyConLabel tycon (VecConUpdCode tag)
mkStdUpdCodePtrVecLabel   tycon tag = TyConLabel tycon (StdUpdCode tag)

mkInfoTableVecTblLabel	  tycon     = TyConLabel tycon InfoTblVecTbl
mkStdUpdVecTblLabel	  tycon     = TyConLabel tycon StdUpdVecTbl

mkReturnPtLabel uniq		= CaseLabel uniq CaseReturnPt
mkVecTblLabel   uniq		= CaseLabel uniq CaseVecTbl
mkAltLabel      uniq tag	= CaseLabel uniq (CaseAlt tag)
mkDefaultLabel  uniq 		= CaseLabel uniq CaseDefault

mkAsmTempLabel 			= AsmTempLabel

	-- Some fixed runtime system labels

mkErrorStdEntryLabel		= RtsLabel RtsShouldNeverHappenCode
mkBlackHoleInfoTableLabel	= RtsLabel RtsBlackHoleInfoTbl
--UNUSED:mkSelectorInfoTableLabel upd_reqd offset = RtsLabel (RtsSelectorInfoTbl upd_reqd offset)
--UNUSED: mkSelectorEntryLabel upd_reqd offset     = RtsLabel (RtsSelectorEntry upd_reqd offset)

#ifdef DPH
mkLocalLabel = ALocalLabel
#endif {- Data Parallel Haskell -}
\end{code}

\begin{code}
needsCDecl :: CLabel -> Bool	-- False <=> it's pre-declared; don't bother
isReadOnly :: CLabel -> Bool	-- lives in C "text space"
isAsmTemp  :: CLabel -> Bool    -- is a local temporary for native code generation
externallyVisibleCLabel :: CLabel -> Bool -- not C "static"
\end{code}

@needsCDecl@ is @True@ unless the thing is a deeply-@PreludeCore@-ish
object.  {\em Also:} No need to spit out labels for things generated
by the flattener (in @AbsCFuns@)---it is careful to ensure references
to them are always backwards.  These are return-point and vector-table
labels.

Declarations for (non-prelude) @Id@-based things are needed because of
mutual recursion.
\begin{code}
needsCDecl (IdLabel _ _)	       = True -- OLD: not (fromPreludeCore id)
needsCDecl (CaseLabel _ _)	       = False

needsCDecl (TyConLabel _ (StdUpdCode _)) = False
needsCDecl (TyConLabel _ InfoTblVecTbl)  = False
needsCDecl (TyConLabel _ other)          = True

needsCDecl (AsmTempLabel _)            = False
needsCDecl (RtsLabel _)                = False

#ifdef DPH
needsCDecl (ALocalLabel _ _)           = panic "needsCDecl: Shouldn't call"
#endif {- Data Parallel Haskell -}

needsCDecl other		       = True
\end{code}

Whether the labelled thing can be put in C "text space":
\begin{code}
isReadOnly (IdLabel _ InfoTbl)	       = True  -- info-tables: yes
isReadOnly (IdLabel _ StaticInfoTbl)   = True  -- and so on, for other
isReadOnly (IdLabel _ PhantomInfoTbl)  = True
isReadOnly (IdLabel _ (VapInfoTbl _))  = True
isReadOnly (IdLabel _ other)	       = False -- others: pessimistically, no

isReadOnly (TyConLabel _ _)    = True
isReadOnly (CaseLabel _ _)     = True
isReadOnly (AsmTempLabel _)    = True
isReadOnly (RtsLabel _)        = True

#ifdef DPH
isReadOnly (ALocalLabel _ _)   = panic "isReadOnly: Shouldn't call"
#endif {- Data Parallel Haskell -}
\end{code}

Whether the label is an assembler temporary:
\begin{code}
isAsmTemp (AsmTempLabel _) = True
isAsmTemp _ 	    	   = False
\end{code}

C ``static'' or not...
\begin{code}
externallyVisibleCLabel (TyConLabel tc _) = not (isBigTupleTyCon tc)
					    -- i.e. not generated for
					    -- purely-local use...
externallyVisibleCLabel (CaseLabel _ _)	  = False
externallyVisibleCLabel (AsmTempLabel _)  = False
externallyVisibleCLabel (RtsLabel _)	  = True

#ifndef DPH

externallyVisibleCLabel (IdLabel (CLabelId id) _)   = externallyVisibleId id

#else
-- DPH pays a big price for exported identifiers. For example with
-- a statically allocated closure, if it is local to a file it will
-- only take up 1 word of storage; exported closures have to go
-- in a data section of their own, which gets padded out to a plane size---
-- on the DAP510 this is 32 words, DAP610 128 words, DAP710 512 words :-(
-- NOTE:16/07/93 Used isInvented (these worker things are globally visible).
-- Local labels (i.e ones within a code block) are not visible outside
-- a file.

externallyVisibleCLabel (IdLabel (CLabelId id) _) = isInventedTopLevId id || isExported id
externallyVisibleCLabel (ALocalLabel _ _)	  = False
#endif {- Data Parallel Haskell -}
\end{code}

@isLocalLabel@ determines if a label is local to a block---a different
machine code jump is generated.

Note(hack after 0.16): Blocks with direct entry points can appear
		       within blocks labelled with a direct entry
		       point --- something todo with let-no-escape.
		       Fast entry blocks arent nestable, however we
		       special case fall through.
\begin{code}
#ifdef DPH
isLocalLabel::CLabel -> Bool
isLocalLabel (ALocalLabel _ _) = True
isLocalLabel _		       = False

isNestableBlockLabel (ALocalLabel _ _)          = True
isNestableBlockLabel (IdLabel _ EntryStd)       = True
isNestableBlockLabel (IdLabel _ ConEntry)       = True
isNestableBlockLabel (IdLabel _ StaticConEntry) = True
isNestableBlockLabel _                          = False

isSlowFastLabelPair :: CLabel -> CLabel -> Bool
isSlowFastLabelPair (IdLabel clid EntryStd) (IdLabel clid' (EntryFast _)) = clid == clid'
isSlowFastLabelPair _                       _			          = False
#endif {- Data Parallel Haskell -}
\end{code}

We need to determine if a label represents a code entity, an ordinary 
data entity, or a special global data entity (placed at an absolute
address by the runtime system that ensures fast loading of variable
contents---global ``registers'' such as SuA are placed here as well)
(different instructions are used in the DAP machine code). 
\begin{code}
#ifdef DPH
isGlobalDataLabel _ = False

isDataLabel :: CLabel -> Bool
isDataLabel (IdLabel _ Closure) = True
isDataLabel _			= False

isVectorTableLabel :: CLabel -> Bool
isVectorTableLabel (VecTblCLabel _)   = True
isVectorTableLabel _                  = False
#endif {- Data Parallel Haskell -}
\end{code}

Sort of like the needsCDecl, we need to stop the assembler from complaining
about various data sections :-)
\begin{code}
#ifdef DPH
needsApalDecl :: CLabel -> Bool
needsApalDecl (IdLabel (CLabelId id) Closure)  = not (isLocallyDefined id)
needsApalDecl _		                       = False
#endif {- Data Parallel Haskell -}
\end{code}

These GRAN functions are needed for spitting out GRAN_FETCH() at the
right places. It is used to detect when the abstractC statement of an
CCodeBlock actually contains the code for a slow entry point.  -- HWL

\begin{code}
#ifdef GRAN

isSlowEntryCCodeBlock :: CLabel -> Bool
isSlowEntryCCodeBlock _ = False
-- Worth keeping?  ToDo (WDP)

#endif {-GRAN-}
\end{code}

We need at least @Eq@ for @CLabels@, because we want to avoid
duplicate declarations in generating C (see @labelSeenTE@ in
@PprAbsC@).

\begin{code}
pprCLabel :: PprStyle -> CLabel -> Unpretty

pprCLabel (PprForAsm _ _ fmtAsmLbl) (AsmTempLabel u) 
  = uppStr (fmtAsmLbl (_UNPK_ (showUnique u)))

pprCLabel (PprForAsm sw_chker prepend_cSEP _) lbl
  = if prepend_cSEP
    then uppBeside pp_cSEP prLbl
    else prLbl
  where
    prLbl = pprCLabel (PprForC sw_chker) lbl

pprCLabel sty (TyConLabel tc UnvecConUpdCode)
  = uppBesides [uppPStr SLIT("ret"), pp_cSEP, uppStr (showTyCon sty tc), 
               pp_cSEP, uppPStr SLIT("upd")]

pprCLabel sty (TyConLabel tc (VecConUpdCode tag))
  = uppBesides [uppPStr SLIT("ret"), pp_cSEP, uppStr (showTyCon sty tc), pp_cSEP,
		     uppInt tag, pp_cSEP, uppPStr SLIT("upd")]

pprCLabel sty (TyConLabel tc (StdUpdCode tag))
  = case (ctrlReturnConvAlg tc) of
        UnvectoredReturn _ -> uppPStr SLIT("IndUpdRetDir")
    	VectoredReturn _ -> uppBeside (uppPStr SLIT("IndUpdRetV")) (uppInt (tag - fIRST_TAG))

pprCLabel sty (TyConLabel tc InfoTblVecTbl)
  = uppBesides [uppStr (showTyCon sty tc), pp_cSEP, uppPStr SLIT("itblvtbl")]

pprCLabel sty (TyConLabel tc StdUpdVecTbl)
  = uppBesides [uppPStr SLIT("vtbl"), pp_cSEP, uppStr (showTyCon sty tc), 
    	       pp_cSEP, uppPStr SLIT("upd")]

pprCLabel sty (CaseLabel u CaseReturnPt)
  = uppBesides [uppPStr SLIT("ret"), pp_cSEP, ppr_u u]
pprCLabel sty (CaseLabel u CaseVecTbl)	
  = uppBesides [uppPStr SLIT("vtbl"), pp_cSEP, ppr_u u]
pprCLabel sty (CaseLabel u (CaseAlt tag))
  = uppBesides [uppPStr SLIT("djn"), pp_cSEP, ppr_u u, pp_cSEP, uppInt tag]
pprCLabel sty (CaseLabel u CaseDefault)	
  = uppBesides [uppPStr SLIT("djn"), pp_cSEP, ppr_u u]

pprCLabel sty (RtsLabel RtsShouldNeverHappenCode) = uppPStr SLIT("StdErrorCode")

pprCLabel sty (RtsLabel RtsBlackHoleInfoTbl) = uppPStr SLIT("BH_UPD_info")

pprCLabel sty (RtsLabel (RtsSelectorInfoTbl upd_reqd offset)) 
  = uppBesides [uppPStr SLIT("__sel_info_"), uppStr (show offset), 
		uppStr (if upd_reqd then "upd" else "noupd"),
		uppPStr SLIT("__")]

pprCLabel sty (RtsLabel (RtsSelectorEntry upd_reqd offset)) 
  = uppBesides [uppPStr SLIT("__sel_entry_"), uppStr (show offset), 
		uppStr (if upd_reqd then "upd" else "noupd"),
		uppPStr SLIT("__")]

pprCLabel sty (IdLabel (CLabelId id) flavor)
  = uppBeside (prettyToUn (ppr sty id)) (ppFlavor flavor)

#ifdef DPH
pprCLabel sty (ALocalLabel u str) = uppBeside (uppStr str) (ppr_u u)
#endif {- Data Parallel Haskell -}

ppr_u u = prettyToUn (pprUnique u)

ppFlavor :: IdLabelInfo -> Unpretty
#ifndef DPH
ppFlavor x = uppBeside pp_cSEP
             	      (case x of
		       Closure	    	-> uppPStr SLIT("closure")
		       InfoTbl	    	-> uppPStr SLIT("info")
		       EntryStd	    	-> uppPStr SLIT("entry")
		       EntryFast arity	-> --false:ASSERT (arity > 0)
					   uppBeside (uppPStr SLIT("fast")) (uppInt arity)
		       ConEntry	    	-> uppPStr SLIT("entry")
		       StaticConEntry  	-> uppPStr SLIT("static_entry")
		       StaticInfoTbl 	-> uppPStr SLIT("static_info")
		       PhantomInfoTbl 	-> uppPStr SLIT("inregs_info")
		       VapInfoTbl True  -> uppPStr SLIT("vap_info")
		       VapInfoTbl False -> uppPStr SLIT("vap_noupd_info")
		       VapEntry True    -> uppPStr SLIT("vap_entry")
		       VapEntry False   -> uppPStr SLIT("vap_noupd_entry")
		       RednCounts	-> uppPStr SLIT("ct")
		      )
#else
ppFlavor x = uppStr (case x of
		       Closure		-> "_clos"
		       InfoTbl		-> "_info"
		       EntryStd		-> "_entry"
		       EntryFast arity  -> "_fast" ++ show arity
		       ConEntry		-> "_entry"
		       StaticConEntry  	-> "_statentr"
		       StaticInfoTbl 	-> "_statinfo"
		       PhantomInfoTbl 	-> "_irinfo"
		       -- ToDo: add more
		    )
#endif {- Data Parallel Haskell -}

\end{code}

ToDo:
use Z as escape char
\begin{verbatim}
_	main separator

orig		becomes
****		*******
_		Zu
'		Zq (etc for ops ??)
<funny char>	Z[hex-digit][hex-digit]
Prelude<x>	ZP<x>
<std class>	ZC<?>
<std tycon>	ZT<?>
\end{verbatim}

\begin{code}
cSEP = SLIT("_")	-- official C separator
pp_cSEP = uppChar '_'

identToC    :: FAST_STRING -> Pretty
modnameToC  :: FAST_STRING -> FAST_STRING
stringToC   :: String -> String
charToC, charToEasyHaskell :: Char -> String

-- stringToC: the hassle is what to do w/ strings like "ESC 0"...

stringToC ""  = "" 
stringToC [c] = charToC c
stringToC (c:cs)
    -- if we have something "octifiable" in "c", we'd better "octify"
    -- the rest of the string, too.
  = if (c < ' ' || c > '~')
    then (charToC c) ++ (concat (map char_to_C cs))
    else (charToC c) ++ (stringToC cs)
  where
    char_to_C c | c == '\n' = "\\n"	-- use C escapes when we can
		| c == '\a' = "\\a"
		| c == '\b' = "\\b"	-- ToDo: chk some of these...
		| c == '\r' = "\\r"
		| c == '\t' = "\\t"
		| c == '\f' = "\\f"
		| c == '\v' = "\\v"
		| otherwise = '\\' : (octify (ord c))

-- OLD?: stringToC str = concat (map charToC str)

charToC c = if (c >= ' ' && c <= '~')	-- non-portable...
	    then case c of
		  '\'' -> "\\'"
		  '\\' -> "\\\\"
		  '"'  -> "\\\""
		  '\n' -> "\\n"
		  '\a' -> "\\a"
		  '\b' -> "\\b"
		  '\r' -> "\\r"
		  '\t' -> "\\t"
		  '\f' -> "\\f"
		  '\v' -> "\\v"
		  _    -> [c]
	    else '\\' : (octify (ord c))

-- really: charToSimpleHaskell

charToEasyHaskell c
  = if (c >= 'a' && c <= 'z')
    || (c >= 'A' && c <= 'Z')
    || (c >= '0' && c <= '9')
    then [c]
    else case c of
	  _    -> '\\' : 'o' : (octify (ord c))

octify :: Int -> String
octify n
  = if n < 8 then
	[chr (n + ord '0')]
    else 
	octify (n `quot` 8) ++ [chr (n `rem` 8 + ord '0')]

identToC ps
  = let
	str = _UNPK_ ps
    in
    ppBeside
	(case str of
	   's':'t':'d':_ -> -- avoid "stdin", "stdout", and "stderr"...
			    ppChar 'Z'
	   _	         -> ppNil)

	(if (all isAlphanum str) -- we gamble that this test will succeed...
	 then ppPStr ps
	 else ppIntersperse ppNil (map char_to_c str))
  where
    char_to_c 'Z'  = ppPStr SLIT("ZZ")
    char_to_c '&'  = ppPStr SLIT("Za")
    char_to_c '|'  = ppPStr SLIT("Zb")
    char_to_c ':'  = ppPStr SLIT("Zc")
    char_to_c '/'  = ppPStr SLIT("Zd")
    char_to_c '='  = ppPStr SLIT("Ze")
    char_to_c '>'  = ppPStr SLIT("Zg")
    char_to_c '#'  = ppPStr SLIT("Zh")
    char_to_c '<'  = ppPStr SLIT("Zl")
    char_to_c '-'  = ppPStr SLIT("Zm")
    char_to_c '!'  = ppPStr SLIT("Zn")
    char_to_c '.'  = ppPStr SLIT("Zo")
    char_to_c '+'  = ppPStr SLIT("Zp")
    char_to_c '\'' = ppPStr SLIT("Zq")
    char_to_c '*'  = ppPStr SLIT("Zt")
    char_to_c '_'  = ppPStr SLIT("Zu")

    char_to_c c    = if isAlphanum c
		     then ppChar c
		     else ppBeside (ppChar 'Z') (ppInt (ord c))
\end{code}

For \tr{modnameToC}, we really only have to worry about \tr{'}s (quote
chars) in the name.  Rare.
\begin{code}
modnameToC ps
  = let
	str = _UNPK_ ps
    in
    if not (any quote_here str) then
	ps
    else
	_PK_ (concat (map char_to_c str))
  where
    quote_here '\'' = True
    quote_here _    = False

    char_to_c c
      = if isAlphanum c then [c] else 'Z' : (show (ord c))
\end{code}