d0013f66b10d7f7fba0b4c6aa6fe663a51845382
[ghc-hetmet.git] / ghc / interpreter / storage.h
1
2 /* --------------------------------------------------------------------------
3  * Defines storage datatypes: Text, Name, Module, Tycon, Cell, List, Pair,
4  * Triple, ...
5  *
6  * The Hugs 98 system is Copyright (c) Mark P Jones, Alastair Reid, the
7  * Yale Haskell Group, and the Oregon Graduate Institute of Science and
8  * Technology, 1994-1999, All rights reserved.  It is distributed as
9  * free software under the license in the file "License", which is
10  * included in the distribution.
11  *
12  * $RCSfile: storage.h,v $
13  * $Revision: 1.37 $
14  * $Date: 2000/03/28 10:20:56 $
15  * ------------------------------------------------------------------------*/
16
17 #define DEBUG_STORAGE
18
19 /* --------------------------------------------------------------------------
20  * Typedefs for main data types:
21  * Many of these type names are used to indicate the intended us of a data
22  * item, rather than for type checking purposes.  Sadly (although sometimes,
23  * fortunately), the C compiler cannot distinguish between the use of two
24  * different names defined to be synonyms for the same types.
25  * ------------------------------------------------------------------------*/
26
27 typedef Int          Text;                       /* text string            */
28 typedef Unsigned     Syntax;                     /* syntax (assoc,preced)  */
29 typedef Int          Cell;                       /* general cell value     */
30 typedef Cell far     *Heap;                      /* storage of heap        */
31 typedef Cell         Pair;                       /* pair cell              */
32 typedef Int          StackPtr;                   /* stack pointer          */
33 typedef Cell         Offset;                     /* offset/generic variable*/
34 typedef Int          Module;                     /* module                 */
35 typedef Cell         Tycon;                      /* type constructor       */
36 typedef Cell         Type;                       /* type expression        */
37 typedef Cell         Kind;                       /* kind expression        */
38 typedef Cell         Kinds;                      /* list of kinds          */
39 typedef Cell         Constr;                     /* constructor expression */
40 typedef Cell         Name;                       /* named value            */
41 typedef Cell         Class;                      /* type class             */
42 typedef Cell         Inst;                       /* instance of type class */
43 typedef Cell         Triple;                     /* triple of cell values  */
44 typedef Cell         List;                       /* list of cells          */
45 typedef Cell         Bignum;                     /* bignum integer         */
46 typedef Cell         Float;                      /* floating pt literal    */
47 #if TREX
48 typedef Cell         Ext;                        /* extension label        */
49 #endif
50
51 typedef Cell         ConId;
52 typedef Cell         VarId;
53 typedef Cell         QualId;
54 typedef Cell         ConVarId;
55
56 /* --------------------------------------------------------------------------
57  * Address ranges.
58  * 
59  * -heapSize .. -1                                    cells in the heap
60  * 0                                                  NIL
61  *
62  * TAG_NONPTR_MIN(100) .. TAG_NONPTR_MAX(115)         non pointer tags
63  * TAG_PTR_MIN(200)    .. TAG_PTR_MAX(298)            pointer tags
64  * TAG_SPEC_MIN(400)   .. TAG_SPEC_MAX(425)           special tags
65  * OFF_MIN(1,000)      .. OFF_MAX(1,999)              offsets
66  * CHARR_MIN(3,000)    .. CHARR_MAX(3,255)            chars
67  *
68  * SMALL_INT_MIN(100,000) .. SMALL_INT_MAX(499,999)   smallish ints
69  *              (300,000 denotes 0)
70  *
71  * NAME_BASE_ADDR   (1,000,000 .. 1,899,999)          names
72  * TYCON_BASE_ADDR  (2,000,000 .. 2,899,999)          tycons
73  * CCLASS_BASE_ADDR (3,000,000 .. 3,899,999)          classes
74  * INST_BASE_ADDR   (4,000,000 .. 4,899,999)          instances
75  * MODULE_BASE_ADDR (5,000,000 .. 5,899,999)          modules
76  * INVAR_BASE_ADDR  (6,000,000 .. 6,899,999)          invented var names
77  * INDVAR_BASE_ADDR (7,000,000 .. 7,899,999)          invented dict var names
78  * TEXT_BASE_ADDR   (8,000,000 .. 8M +TEXT_SIZE-1)    text
79  * ------------------------------------------------------------------------*/
80
81 /* --------------------------------------------------------------------------
82  * Text storage:
83  * provides storage for the characters making up identifier and symbol
84  * names, string literals, character constants etc...
85  * ------------------------------------------------------------------------*/
86
87 extern  String       textToStr            ( Text );
88 extern  Text         findText             ( String );
89 extern  Text         inventText           ( Void );
90 extern  Text         inventDictText       ( Void );
91 extern  Bool         inventedText         ( Text );
92 extern  Text         enZcodeThenFindText  ( String );
93 extern  Text         unZcodeThenFindText  ( String );
94
95 /* Variants of textToStr and syntaxOf which work for idents, ops whether
96  * qualified or unqualified.
97  */
98 extern  String       identToStr         ( Cell );
99 extern  Text         fixLitText         ( Text );
100 extern  Syntax       identSyntax        ( Cell );
101 extern  Syntax       defaultSyntax      ( Text );
102
103 #define INVAR_BASE_ADDR  6000000
104 #define INVAR_MAX_AVAIL  900000
105 #define isInventedVar(c) (INVAR_BASE_ADDR<=(c) \
106                           && (c)<INVAR_BASE_ADDR+INVAR_MAX_AVAIL)
107
108 #define INDVAR_BASE_ADDR 7000000
109 #define INDVAR_MAX_AVAIL 900000
110 #define isInventedDictVar(c) (INDVAR_BASE_ADDR<=(c) \
111                               && (c)<INDVAR_BASE_ADDR+INDVAR_MAX_AVAIL)
112
113 #define TEXT_BASE_ADDR   8000000
114 #define isText(c) (TEXT_BASE_ADDR<=(c) \
115                   && (c)<TEXT_BASE_ADDR+TEXT_SIZE)
116
117 /* --------------------------------------------------------------------------
118  * Specification of syntax (i.e. default written form of application)
119  * ------------------------------------------------------------------------*/
120
121 #define MIN_PREC  0                    /* weakest binding operator         */
122 #define MAX_PREC  9                    /* strongest binding operator       */
123 #define FUN_PREC  (MAX_PREC+2)         /* binding of function symbols      */
124 #define DEF_PREC  MAX_PREC
125 #define APPLIC    0                    /* written applicatively            */
126 #define LEFT_ASS  1                    /* left associative infix           */
127 #define RIGHT_ASS 2                    /* right associative infix          */
128 #define NON_ASS   3                    /* non associative infix            */
129 #define DEF_ASS   LEFT_ASS
130
131 #define UMINUS_PREC  6                  /* Change these settings at your   */
132 #define UMINUS_ASSOC LEFT_ASS           /* own risk; they may not work!    */
133
134 #define assocOf(x)      ((x)&NON_ASS)
135 #define precOf(x)       ((x)>>2)
136 #define mkSyntax(a,p)   ((a)|((p)<<2))
137 #define DEF_OPSYNTAX    mkSyntax(DEF_ASS,DEF_PREC)
138 #define NO_SYNTAX       (-1)
139
140 extern  Void   addSyntax  ( Int,Text,Syntax );
141 extern  Syntax syntaxOf   ( Text );
142
143 /* --------------------------------------------------------------------------
144  * Heap storage:
145  * Provides a garbage collectable heap for storage of expressions etc.
146  * ------------------------------------------------------------------------*/
147
148 #define heapAlloc(s) (Heap)(farCalloc(s,sizeof(Cell)))
149 extern  Int          heapSize;
150 extern  Heap         heapFst, heapSnd;
151 extern  Heap         heapTopFst;
152 extern  Heap         heapTopSnd;
153 extern  Bool         consGC;            /* Set to FALSE to turn off gc from*/
154                                         /* C stack; use with extreme care! */
155 extern  Int          cellsRecovered;    /* cells recovered by last gc      */
156
157 #define fst(c)       heapTopFst[c]
158 #define snd(c)       heapTopSnd[c]
159
160 extern  Pair         pair            ( Cell,Cell );
161 extern  Void         garbageCollect  ( Void );
162
163 extern  Void         overwrite       ( Pair,Pair );
164 extern  Cell         markExpr        ( Cell );
165 extern  Void         markWithoutMove ( Cell );
166
167 #define mark(v)      v=markExpr(v)
168
169 #define isPair(c)    ((c)<0)
170 #define isGenPair(c) ((c)<0 && -heapSize<=(c))
171
172 extern  Cell         whatIs    ( Cell );
173
174 /* --------------------------------------------------------------------------
175  * Pairs in the heap fall into three categories.
176  *
177  * pair(TAG_NONPTR,y)
178  *    used to denote that the second element of the pair is to be treated
179  *    in some special way (eg is a integer or Text), and specifically is not
180  *    a heap pointer
181  * 
182  * pair(TAG_PTR,y)
183  *    to indicate that the second element of the pair is a normal 
184  *    heap pointer, which should be followed at GC time
185  * 
186  * pair(x,y)
187  *    is a genuine pair, where both components are heap pointers.
188  * ------------------------------------------------------------------------*/
189
190 #if !defined(SIZEOF_VOID_P) || !defined(SIZEOF_INT)
191 #error SIZEOF_VOID_P or SIZEOF_INT is not defined
192 #endif
193
194 #define isTagNonPtr(c) (TAG_NONPTR_MIN<=(c) && (c)<=TAG_NONPTR_MAX)
195 #define isTagPtr(c)    (TAG_PTR_MIN<=(c) && (c)<=TAG_PTR_MAX)
196 #define isTag(c)       (isTagNonPtr(c) || isTagPtr(c))
197
198 /* --------------------------------------------------------------------------
199  * Tags for non-pointer cells.
200  * ------------------------------------------------------------------------*/
201
202 #define TAG_NONPTR_MIN 100
203 #define TAG_NONPTR_MAX 115
204
205 #define FREECELL     100          /* Free list cell:          snd :: Cell  */
206 #define VARIDCELL    101          /* Identifier variable:     snd :: Text  */
207 #define VAROPCELL    102          /* Operator variable:       snd :: Text  */
208 #define DICTVAR      103          /* Dictionary variable:     snd :: Text  */
209 #define CONIDCELL    104          /* Identifier constructor:  snd :: Text  */
210 #define CONOPCELL    105          /* Operator constructor:    snd :: Text  */
211 #define STRCELL      106          /* String literal:          snd :: Text  */
212 #define INTCELL      107          /* Int literal:             snd :: Int   */
213 #define ADDPAT       108          /* (_+k) pattern discr:     snd :: Int   */
214 #define FLOATCELL    109          /* Floating Pt literal:     snd :: Text  */
215 #define BIGCELL      110          /* Integer literal:         snd :: Text  */
216 #define PTRCELL      111          /* C Heap Pointer           snd :: Ptr   */
217 #define CPTRCELL     112          /* Native code pointer      snd :: Ptr   */
218
219 #if IPARAM
220 #define IPCELL       113          /* Imp Param Cell:          snd :: Text  */
221 #define IPVAR        114          /* ?x:                      snd :: Text  */
222 #endif
223
224 #if TREX
225 #define EXTCOPY      115          /* Copy of an Ext:          snd :: Text  */
226 #endif
227
228 #define qmodOf(c)       (textOf(fst(snd(c))))    /* c ::  QUALIDENT        */
229 #define qtextOf(c)      (textOf(snd(snd(c))))    /* c ::  QUALIDENT        */
230 #define mkVar(t)        ap(VARIDCELL,t)
231 #define mkVarop(t)      ap(VAROPCELL,t)
232 #define mkCon(t)        ap(CONIDCELL,t)
233 #define mkConop(t)      ap(CONOPCELL,t)
234 #define mkQVar(m,t)     ap(QUALIDENT,pair(mkCon(m),mkVar(t)))
235 #define mkQCon(m,t)     ap(QUALIDENT,pair(mkCon(m),mkCon(t)))
236 #define mkQVarOp(m,t)   ap(QUALIDENT,pair(mkCon(m),mkVarop(t)))
237 #define mkQConOp(m,t)   ap(QUALIDENT,pair(mkCon(m),mkConop(t)))
238 #define mkQualId(m,t)   ap(QUALIDENT,pair(m,t))
239 #define intValOf(c)     (snd(c))
240 #define inventVar()     mkVar(inventText())
241 #define mkDictVar(t)    ap(DICTVAR,t)
242 #define inventDictVar() mkDictVar(inventDictText())
243 #define mkStr(t)        ap(STRCELL,t)
244 #if IPARAM
245 #define mkIParam(c)     ap(IPCELL,snd(c))
246 #define isIP(p)         (whatIs(p) == IPCELL)
247 #define ipMatch(pi, t)  (isIP(fun(pi)) && textOf(fun(pi)) == t)
248 #define ipVar(pi)       textOf(fun(pi))
249 #else
250 #define isIP(p)         FALSE
251 #endif
252
253 extern  Bool            isVar        ( Cell );
254 extern  Bool            isCon        ( Cell );
255 extern  Bool            isQVar       ( Cell );
256 extern  Bool            isQCon       ( Cell );
257 extern  Bool            isQualIdent  ( Cell );
258 extern  Bool            eqQualIdent  ( QualId c1, QualId c2 );
259 extern  Bool            isIdent      ( Cell );
260 extern  String          stringNegate ( String );
261 extern  Text            textOf       ( Cell );
262
263 #define isFloat(c)       (isPair(c) && fst(c)==FLOATCELL)
264 #define stringToFloat(s) pair(FLOATCELL,findText(s))
265 #define floatToString(f) textToStr(snd(f))
266 #define floatOf(f)       atof(floatToString(f))
267 #define mkFloat(f)       (f)  /* ToDo: is this right? */
268 #define floatNegate(f)   stringToFloat(stringNegate(floatToString(f)))
269
270 #define stringToBignum(s) pair(BIGCELL,findText(s))
271 #define bignumToString(b) textToStr(snd(b))
272
273 #define isPtr(c)        (isPair(c) && fst(c)==PTRCELL)
274 extern  Cell            mkPtr           ( Ptr );
275 extern  Ptr             ptrOf           ( Cell );
276 #define isCPtr(c)       (isPair(c) && fst(c)==CPTRCELL)
277 extern  Cell            mkCPtr          ( Ptr );
278 extern  Ptr             cptrOf          ( Cell );
279
280 /* --------------------------------------------------------------------------
281  * Tags for pointer cells.
282  * ------------------------------------------------------------------------*/
283
284 #define TAG_PTR_MIN 200
285 #define TAG_PTR_MAX 298
286
287 #define LETREC       200          /* LETREC     snd :: ([Decl],Exp)        */
288 #define COND         201          /* COND       snd :: (Exp,Exp,Exp)       */
289 #define LAMBDA       202          /* LAMBDA     snd :: Alt                 */
290 #define FINLIST      203          /* FINLIST    snd :: [Exp]               */
291 #define DOCOMP       204          /* DOCOMP     snd :: (Exp,[Qual])        */
292 #define BANG         205          /* BANG       snd :: Type                */
293 #define COMP         206          /* COMP       snd :: (Exp,[Qual])        */
294 #define ASPAT        207          /* ASPAT      snd :: (Var,Exp)           */
295 #define ESIGN        208          /* ESIGN      snd :: (Exp,Type)          */
296 #define RSIGN        209          /* RSIGN      snd :: (Rhs,Type)          */
297 #define CASE         210          /* CASE       snd :: (Exp,[Alt])         */
298 #define NUMCASE      211          /* NUMCASE    snd :: (Exp,Disc,Rhs)      */
299 #define FATBAR       212          /* FATBAR     snd :: (Exp,Exp)           */
300 #define LAZYPAT      213          /* LAZYPAT    snd :: Exp                 */
301 #define DERIVE       214          /* DERIVE     snd :: Cell                */
302 #define BOOLQUAL     215          /* BOOLQUAL   snd :: Exp                 */
303 #define QWHERE       216          /* QWHERE     snd :: [Decl]              */
304 #define FROMQUAL     217          /* FROMQUAL   snd :: (Exp,Exp)           */
305 #define DOQUAL       218          /* DOQUAL     snd :: Exp                 */
306 #define MONADCOMP    219          /* MONADCOMP  snd :: ((m,m0),(Exp,[Qual])*/
307 #define GUARDED      220          /* GUARDED    snd :: [guarded exprs]     */
308 #define ARRAY        221          /* Array      snd :: (Bounds,[Values])   */
309 #define MUTVAR       222          /* Mutvar     snd :: Cell                */
310 #define HUGSOBJECT   223          /* HUGSOBJECT snd :: Cell                */
311
312 #if IPARAM
313 #define WITHEXP      224          /* WITHEXP    snd :: [(Var,Exp)]         */
314 #endif
315
316 #define POLYTYPE     225          /* POLYTYPE   snd :: (Kind,Type)         */
317 #define QUAL         226          /* QUAL       snd :: ([Classes],Type)    */
318 #define RANK2        227          /* RANK2      snd :: (Int,Type)          */
319 #define EXIST        228          /* EXIST      snd :: (Int,Type)          */
320 #define POLYREC      229          /* POLYREC    snd :: (Int,Type)          */
321 #define BIGLAM       230          /* BIGLAM     snd :: (vars,patterns)     */
322 #define CDICTS       231          /* CDICTS     snd :: ([Pred],Type)       */
323
324 #define LABC         232          /* LABC       snd :: (con,[(Vars,Type)]) */
325 #define CONFLDS      233          /* CONFLDS    snd :: (con,[Field])       */
326 #define UPDFLDS      234          /* UPDFLDS    snd :: (Exp,[con],[Field]) */
327 #if TREX
328 #define RECORD       235          /* RECORD     snd :: [Val]               */
329 #define EXTCASE      236          /* EXTCASE    snd :: (Exp,Disc,Rhs)      */
330 #define RECSEL       237          /* RECSEL     snd :: Ext                 */
331 #endif
332 #define IMPDEPS      238          /* IMPDEPS    snd :: [Binding]           */
333
334 #define QUALIDENT    239          /* Qualified identifier  snd :: (Id,Id)  */
335 #define HIDDEN       240          /* hiding import list    snd :: [Entity] */
336 #define MODULEENT    241          /* module in export list snd :: con      */
337
338 #define INFIX        242          /* INFIX      snd :: (see tidyInfix)     */
339 #define ONLY         243          /* ONLY       snd :: Exp                 */
340 #define NEG          244          /* NEG        snd :: Exp                 */
341
342 /* Used when parsing GHC interface files */
343 #define DICTAP       245          /* DICTAP     snd :: (QClassId,[Type])   */
344 #define UNBOXEDTUP   246          /* UNBOXEDTUP snd :: [Type]              */
345
346 #if SIZEOF_VOID_P != SIZEOF_INT
347 #define PTRCELL      247          /* C Heap Pointer snd :: (Int,Int)       */
348 #endif
349
350 /* STG syntax */
351 #define STGVAR       248          /* STGVAR     snd :: (StgRhs,info)       */
352 #define STGAPP       249          /* STGAPP     snd :: (StgVar,[Arg])      */
353 #define STGPRIM      250          /* STGPRIM    snd :: (PrimOp,[Arg])      */
354 #define STGCON       251          /* STGCON     snd :: (StgCon,[Arg])      */
355 #define PRIMCASE     252          /* PRIMCASE   snd :: (Expr,[PrimAlt])    */
356 #define DEEFALT      253          /* DEEFALT    snd :: (Var,Expr)          */
357 #define CASEALT      254          /* CASEALT    snd :: (Con,[Var],Expr)    */
358 #define PRIMALT      255          /* PRIMALT    snd :: ([Var],Expr)        */
359
360 /* Module groups */
361 #define GRP_REC      256          /* GRP_REC    snd :: [CONID]             */
362 #define GRP_NONREC   257          /* GRP_NONREC snd :: CONID               */
363
364
365 /* 
366    Top-level interface entities 
367    type Line             = Int  -- a line number 
368    type ConVarId         = CONIDCELL | VARIDCELL
369    type ExportListEntry  = ConVarId | (ConId, [ConVarId]) 
370    type Associativity    = mkInt of LEFT_ASS | RIGHT_ASS | NON_ASS
371    type Constr           = ((ConId, [((Type,VarId,Int))]))
372                ((constr name, [((type, field name if any, strictness))]))
373                strictness: 0 => none, 1 => !, 2 => !! (unpacked)
374    All 2/3/4/5 tuples in the interface abstract syntax are done with
375    z-tuples.
376 */
377
378 #define I_INTERFACE  260  /* snd :: ((ConId, [I_IMPORT..I_VALUE])) 
379                                     interface name, list of iface entities */
380
381 #define I_IMPORT     261  /* snd :: ((ConId, [ConVarId]))
382                                     module name, list of entities          */
383
384 #define I_INSTIMPORT 262  /* snd :: NIL    -- not used at present          */
385
386 #define I_EXPORT     263  /* snd :: ((ConId, [ExportListEntry]))
387                                     this module name?, entities to export  */
388
389 #define I_FIXDECL    264  /* snd :: ((NIL|Int, Associativity, ConVarId))   
390                                     fixity, associativity, name            */
391
392 #define I_INSTANCE   265 /* snd :: ((Line, 
393                                      [((VarId,Kind))], 
394                                      Type, VarId, Inst))
395                    lineno, 
396                    forall-y bit (eg __forall [a b] =>),
397                    other bit, eg { C a1 } -> { C2 a2 } -> ... -> { Cn an },
398                    name of dictionary builder,
399                    (after startGHCInstance) the instance table location    */
400
401 #define I_TYPE       266 /* snd :: ((Line, ConId, [((VarId,Kind))], Type))
402                             lineno, tycon, kinded tyvars, the type expr    */
403
404 #define I_DATA       267 /* snd :: ((Line, [((QConId,VarId))], ConId, 
405                                           [((VarId,Kind))], [Constr]) 
406                             lineno, context, tycon, kinded tyvars, constrs 
407                            An empty constr list means exported abstractly. */
408
409 #define I_NEWTYPE    268 /* snd :: ((Line, [((QConId,VarId))], ConId,
410                                     [((VarId,Kind))], ((ConId,Type)) ))
411                              lineno, context, tycon, kinded tyvars, constr 
412                                     constr==NIL means exported abstractly. */
413
414 #define I_CLASS      269 /* snd :: ((Line, [((QConId,VarId))], ConId,
415                                     [((VarId,Kind))], [((VarId,Type))]))
416                             lineno, context, classname, 
417                                       kinded tyvars, method sigs           */
418
419 #define I_VALUE      270 /* snd :: ((Line, VarId, Type))                   */
420
421 /*
422    Top-level module entities.
423
424    type Export = ?
425 */
426 #define M_MODULE     280 /* snd :: ((ConId, [Export], 
427                                      M_IMPORT_Q .. M_VALUE]))
428                             module name, export spec, top level entities   */
429
430 #define M_IMPORT_Q   281 /* snd :: ((?,?)) */
431 #define M_IMPORT_UNQ 282 /* snd :: ((?,?)) */
432 #define M_TYCON      283 /* snd :: ((Line,?,?,?)) */
433 #define M_CLASS      284 /* snd :: ((Line,?,?,?)) */
434 #define M_INST       285 /* snd :: ((Line,?,?)) */
435 #define M_DEFAULT    286 /* snd :: ((Line,?)) */
436 #define M_FOREIGN_EX 289 /* snd :: ((Line,?,?,?,?)) */
437 #define M_FOREIGN_IM 290 /* snd :: ((Line,?,?,?,?)) */
438 #define M_VALUE      291 /* snd :: ? */
439
440
441
442
443 /* 
444    Tagged tuples.
445 */
446 #define ZTUP2        295          /* snd :: (Cell,Cell)                    */
447 #define ZTUP3        296          /* snd :: (Cell,(Cell,Cell))             */
448 #define ZTUP4        297          /* snd :: (Cell,(Cell,(Cell,Cell)))      */
449 #define ZTUP5        298       /* snd :: (Cell,(Cell,(Cell,(Cell,Cell))))  */
450
451
452
453 /* --------------------------------------------------------------------------
454  * Special cell values.
455  * ------------------------------------------------------------------------*/
456
457 #define TAG_SPEC_MIN 400
458 #define TAG_SPEC_MAX 428
459
460 #define isSpec(c) (TAG_SPEC_MIN<=(c) && (c)<=TAG_SPEC_MAX)
461
462 #define NONE         400          /* Dummy stub                            */
463 #define STAR         401          /* Representing the kind of types        */
464 #if TREX
465 #define ROW          402          /* Representing the kind of rows         */
466 #endif
467 #define WILDCARD     403          /* Wildcard pattern                      */
468 #define SKOLEM       404          /* Skolem constant                       */
469
470 #define DOTDOT       405          /* ".." in import/export list            */
471
472 #define NAME         406          /* whatIs code for isName                */
473 #define TYCON        407          /* whatIs code for isTycon               */
474 #define CLASS        408          /* whatIs code for isClass               */
475 #define MODULE       409          /* whatIs code for isModule              */
476 #define INSTANCE     410          /* whatIs code for isInst                */
477 #define TUPLE        411          /* whatIs code for tuple constructor     */
478 #define OFFSET       412          /* whatis code for offset                */
479 #define AP           413          /* whatIs code for application node      */
480 #define CHARCELL     414          /* whatIs code for isChar                */
481 #if TREX
482 #define EXT          415          /* whatIs code for isExt                 */
483 #endif
484
485 #define SIGDECL      416          /* Signature declaration                 */
486 #define FIXDECL      417          /* Fixity declaration                    */
487 #define FUNBIND      418          /* Function binding                      */
488 #define PATBIND      419          /* Pattern binding                       */
489
490 #define DATATYPE     420          /* Datatype type constructor             */
491 #define NEWTYPE      421          /* Newtype type constructor              */
492 #define SYNONYM      422          /* Synonym type constructor              */
493 #define RESTRICTSYN  423          /* Synonym with restricted scope         */
494
495 #define NODEPENDS    424          /* Stop calculation of deps in type check*/
496 #define PREDEFINED   425          /* Predefined name, not yet filled       */
497 #define TEXTCELL     426          /* whatIs code for isText                */
498 #define INVAR        427          /* whatIs code for isInventedVar         */
499 #define INDVAR       428          /* whatIs code for isInventedDictVar     */
500
501
502 /* --------------------------------------------------------------------------
503  * Tuple data/type constructors:
504  * ------------------------------------------------------------------------*/
505
506 extern Text ghcTupleText    ( Tycon );
507 extern Text ghcTupleText_n  ( Int );
508
509
510
511 #if TREX
512 #error TREX not supported
513 #define EXTMIN       301
514 #define isExt(c)     (EXTMIN<=(c) && (c)<OFFMIN)
515 #define extText(e)   tabExt[(e)-EXTMIN]
516 #define extField(c)  arg(fun(c))
517 #define extRow(c)    arg(c)
518
519 extern Text          DECTABLE(tabExt);
520 extern Ext           mkExt ( Text );
521 #else
522 #define mkExt(t) NIL
523 #endif
524
525 extern Module        findFakeModule ( Text t );
526 extern Tycon         addTupleTycon ( Int n );
527 extern Name          addWiredInBoxingTycon
528                         ( String modNm, String typeNm, String constrNm,
529                           Int rep, Kind kind );
530 extern Tycon         addWiredInEnumTycon 
531                         ( String modNm, String typeNm, 
532                           List /*of Text*/ constrs );
533
534 /* --------------------------------------------------------------------------
535  * Offsets: (generic types/stack offsets)
536  * ------------------------------------------------------------------------*/
537
538 #define OFF_MIN 1000
539 #define OFF_MAX 1999
540
541 #define isOffset(c)  (OFF_MIN<=(c) && (c)<=OFF_MAX)
542 #define offsetOf(c)  ((c)-OFF_MIN)
543 #define mkOffset(o)  (OFF_MIN+(o))
544
545
546 /* --------------------------------------------------------------------------
547  * Modules:
548  * ------------------------------------------------------------------------*/
549
550 #define MODULE_BASE_ADDR     5000000
551 #define MODULE_MAX_SIZE      900000
552 #define MODULE_INIT_SIZE     4
553
554 #ifdef DEBUG_STORAGE
555 extern struct strModule* generate_module_ref ( Cell );
556 #define module(mod)  (*generate_module_ref(mod))
557 #else
558 #define module(mod)   tabModule[(mod)-MODULE_BASE_ADDR]
559 #endif
560
561 #define mkModule(n)   (MODULE_BASE_ADDR+(n))
562 #define isModule(c)   (MODULE_BASE_ADDR<=(c)                  \
563                        && (c)<MODULE_BASE_ADDR+tabModuleSz    \
564                        && tabModule[(c)-MODULE_BASE_ADDR].inUse)
565
566
567 /* Import defns for the ObjectCode struct in Module. */
568 #include "object.h"
569
570 /* Import a machine-dependent definition of Time, for module timestamps. */
571 #include "machdep_time.h"
572
573 /* Under Haskell 1.3, the list of qualified imports is always a subset
574  * of the list of unqualified imports.  For simplicity and flexibility,
575  * we do not attempt to exploit this fact - when a module is imported
576  * unqualified, it is added to both the qualified and unqualified
577  * import lists.
578  * Similarily, Haskell 1.3 does not allow a constructor to be imported
579  * or exported without exporting the type it belongs to but the export
580  * list is just a flat list of Texts (before static analysis) or
581  * Tycons, Names and Classes (after static analysis).
582  */
583 struct strModule {
584    Bool   inUse;
585    Name   nextFree;
586
587    Text   text;        /* Name of this module                              */
588
589    List   tycons;      /* Lists of top level objects ...                   */
590    List   names;       /* (local defns + imports)                          */
591    List   classes;
592    List   exports;     /* [ Entity | (Entity, NIL|DOTDOT) ]                */
593
594    List   qualImports; /* Qualified imports.                               */
595
596    Bool   fake;        /* TRUE if module exists only via GHC primop        */
597                        /* defn; usually FALSE                              */
598
599    Cell   tree;        /* Parse tree for mod or iface                      */
600    Bool   completed;   /* Fully loaded or just parsed?                     */
601    Time   lastStamp;   /* Time of last parse                               */
602
603    Bool   fromSrc;     /* is it from source ?                              */
604    Text   srcExt;      /* if yes, ".lhs", ".hs", etc"                      */
605    List   uses;        /* :: [CONID] -- names of mods imported by this one */
606
607    Text   objName;     /* Name of the primary object code file.            */
608    Int    objSize;     /* Size of the primary object code file.            */
609
610    ObjectCode* object;        /* Primary object code for this module.      */
611    ObjectCode* objectExtras;  /* And any extras it might need.             */
612    List   objectExtraNames;   /* :: [Text] -- names of extras              */
613 };
614
615 extern struct strModule* tabModule;
616 extern Int               tabModuleSz;
617
618 extern Module currentModule;           /* Module currently being processed */
619 extern List   moduleGraph;             /* :: [GRP_REC | GRP_NONREC]        */
620 extern List   prelModules;             /* :: [CONID]                       */
621 extern List   targetModules;           /* :: [CONID]                       */
622
623
624 extern Bool         isValidModule   ( Module );
625 extern Module       newModule       ( Text );
626 extern Void         nukeModule      ( Module );
627 extern Module       findModule      ( Text );
628 extern Module       findModid       ( Cell );
629 extern Void         setCurrModule   ( Module );
630
631 extern void         addOTabName     ( Module,char*,void* );
632 extern void*        lookupOTabName  ( Module,char* );
633 extern char*        nameFromOPtr    ( void* );
634
635 extern void         addSection      ( Module,void*,void*,OSectionKind );
636 extern OSectionKind lookupSection   ( void* );
637 extern void*    lookupOExtraTabName ( char* sym );
638
639 #define isPrelude(m) (m==modulePrelude)
640
641 #define N_PRELUDE_SCRIPTS (combined ? 32 : 1)
642
643 /* --------------------------------------------------------------------------
644  * Type constructor names:
645  * ------------------------------------------------------------------------*/
646
647 #define TYCON_BASE_ADDR   2000000
648 #define TYCON_MAX_SIZE    900000
649 #define TYCON_INIT_SIZE   4
650
651 #ifdef DEBUG_STORAGE
652 extern struct strTycon* generate_tycon_ref ( Cell );
653 #define tycon(tc)    (*generate_tycon_ref(tc))
654 #else
655 #define tycon(tc)    tabTycon[(tc)-TYCON_BASE_ADDR]
656 #endif
657
658 #define isTycon(c)   (TYCON_BASE_ADDR<=(c)                        \
659                       && (c)<TYCON_BASE_ADDR+tabTyconSz           \
660                       && tabTycon[(c)-TYCON_BASE_ADDR].inUse      \
661                       && tabTycon[(c)-TYCON_BASE_ADDR].tuple==-1)
662 #define isTuple(c)   (TYCON_BASE_ADDR<=(c)                        \
663                       && (c)<TYCON_BASE_ADDR+tabTyconSz           \
664                       && tabTycon[(c)-TYCON_BASE_ADDR].inUse      \
665                       && tabTycon[(c)-TYCON_BASE_ADDR].tuple>=0)
666 #define tupleOf(n)   (tycon(n).tuple)
667
668 extern Tycon mkTuple ( Int );
669
670
671 struct strTycon {
672     Bool   inUse;
673     Name   nextFree;
674     Text   text;
675     Int    line;
676     Module mod;                         /* module that defines it          */
677     Int    tuple;                      /* tuple number, or -1 if not tuple */
678     Int    arity;
679     Kind   kind;                        /* kind (includes arity) of Tycon  */
680     Cell   what;                        /* DATATYPE/SYNONYM/RESTRICTSYN... */
681     Cell   defn;
682     Name   conToTag;                    /* used in derived code            */
683     Name   tagToCon;
684     void*  itbl;                       /* For tuples, the info tbl pointer */
685     Tycon  nextTyconHash;
686 };
687
688 extern struct strTycon* tabTycon;
689 extern Int              tabTyconSz;
690
691 extern Tycon newTycon     ( Text );
692 extern Tycon findTycon    ( Text );
693 extern Tycon addTycon     ( Tycon );
694 extern Tycon findQualTycon ( Cell );
695 extern Tycon addPrimTycon ( Text,Kind,Int,Cell,Cell );
696
697 #define isSynonym(h)    (isTycon(h) && tycon(h).what==SYNONYM)
698 #define isQualType(t)   (isPair(t) && fst(t)==QUAL)
699 #define mkPolyType(n,t) pair(POLYTYPE,pair(n,t))
700 #define isPolyType(t)   (isPair(t) && fst(t)==POLYTYPE)
701 #define isPolyOrQualType(t) (isPair(t) && (fst(t)==POLYTYPE || fst(t)==QUAL))
702 #define polySigOf(t)    fst(snd(t))
703 #define monotypeOf(t)   snd(snd(t))
704 #define bang(t)         ap(BANG,t)
705
706 extern Tycon findQualTyconWithoutConsultingExportList ( QualId q );
707
708 /* --------------------------------------------------------------------------
709  * Globally defined name values:
710  * ------------------------------------------------------------------------*/
711
712 #define NAME_BASE_ADDR    1000000
713 #define NAME_MAX_SIZE     900000
714 #define NAME_INIT_SIZE    4
715
716 #ifdef DEBUG_STORAGE
717 extern struct strName* generate_name_ref ( Cell );
718 #define name(nm)    (*generate_name_ref(nm))
719 #else
720 #define name(nm)    tabName[(nm)-NAME_BASE_ADDR]
721 #endif
722
723 #define mkName(n)   (NAME_BASE_ADDR+(n))
724 #define isName(c)   (NAME_BASE_ADDR<=(c)                   \
725                      && (c)<NAME_BASE_ADDR+tabNameSz       \
726                      && tabName[(c)-NAME_BASE_ADDR].inUse)
727
728 struct strName {
729     Bool   inUse;
730     Name   nextFree;
731     Text   text;
732     Int    line;
733     Module mod;                         /* module that defines it          */
734     Syntax syntax;
735     Cell   parent; 
736     Int    arity;
737     Int    number;
738     Cell   type;
739     Cell   defn;
740     Cell   stgVar;                                      /* really StgVar   */
741     Text   callconv;                          /* for foreign import/export */
742     void*  primop;                                      /* really StgPrim* */
743     void*  itbl;                 /* For constructors, the info tbl pointer */
744     Name   nextNameHash;
745 };
746
747 extern struct strName* tabName;
748 extern Int             tabNameSz;
749
750 extern int numNames (  Void  );
751
752 /* The number field in a name is used to distinguish various kinds of name:
753  *   mfunNo(i) = code for member function, offset i
754  *               members that are sole elements of dict use mfunNo(0)
755  *               members of dicts with more than one elem use mfunNo(n), n>=1
756  *   EXECNAME  = code for executable name (bytecodes or primitive)
757  *   SELNAME   = code for selector function
758  *   DFUNNAME  = code for dictionary builder or selector
759  *   cfunNo(i) = code for data constructor
760  *               datatypes with only one constructor uses cfunNo(0)
761  *               datatypes with multiple constructors use cfunNo(n), n>=1
762  */
763
764 #define EXECNAME        0
765 #define SELNAME         1
766 #define DFUNNAME        2
767 #define CFUNNAME        3
768
769 #define isSfun(n)       (name(n).number==SELNAME)
770 #define isDfun(n)       (name(n).number==DFUNNAME)
771
772 #define isCfun(n)       (name(n).number>=CFUNNAME)
773 #define cfunOf(n)       (name(n).number-CFUNNAME)
774 #define cfunNo(i)       ((i)+CFUNNAME)
775 #define hasCfun(cs)     (nonNull(cs) && isCfun(hd(cs)))
776
777 #define isMfun(n)       (name(n).number<0)
778 #define mfunOf(n)       ((-1)-name(n).number)
779 #define mfunNo(i)       ((-1)-(i))
780
781 extern Name   newName         ( Text,Cell );
782 extern Name   findName        ( Text );
783 extern Name   addName         ( Name );
784 extern Name   findQualName    ( Cell );
785 extern Name   addPrimCfun     ( Text,Int,Int,Cell );
786 extern Name   addPrimCfunREP  ( Text,Int,Int,Int );
787 extern Int    sfunPos         ( Name,Name );
788 extern Name   nameFromStgVar  ( Cell );
789 extern Name   jrsFindQualName ( Text,Text );
790
791 extern Name findQualNameWithoutConsultingExportList ( QualId q );
792
793 /* --------------------------------------------------------------------------
794  * Type class values:
795  * ------------------------------------------------------------------------*/
796
797 #define INST_BASE_ADDR     4000000
798 #define INST_MAX_SIZE      900000
799 #define INST_INIT_SIZE     4
800
801 #ifdef DEBUG_STORAGE
802 extern struct strInst* generate_inst_ref ( Cell );
803 #define inst(in)    (*generate_inst_ref(in))
804 #else
805 #define inst(in)    tabInst[(in)-INST_BASE_ADDR]
806 #endif
807
808 #define mkInst(n)   (INST_BASE_ADDR+(n))
809 #define instOf(c)   ((Int)((c)-INST_BASE_ADDR))
810 #define isInst(c)   (INST_BASE_ADDR<=(c)                   \
811                      && (c)<INST_BASE_ADDR+tabInstSz       \
812                      && tabInst[(c)-INST_BASE_ADDR].inUse)
813
814 struct strInst {
815     Bool   inUse;
816     Name   nextFree;
817     Class  c;                           /* class C                         */
818     Int    line;
819     Module mod;                         /* module that defines it          */
820     Kinds  kinds;                       /* Kinds of variables in head      */
821     Cell   head;                        /* :: Pred                         */
822     List   specifics;                   /* :: [Pred]                       */
823     Int    numSpecifics;                /* length(specifics)               */
824     List   implements;
825     Name   builder;                     /* Dictionary constructor function */
826 };
827
828 extern struct strInst* tabInst;
829 extern Int             tabInstSz;
830
831 /* a predicate (an element :: Pred) is an application of a Class to one or
832  * more type expressions
833  */
834
835 #define CCLASS_BASE_ADDR   3000000
836 #define CCLASS_MAX_SIZE    900000
837 #define CCLASS_INIT_SIZE   4
838
839 #ifdef DEBUG_STORAGE
840 extern struct strClass* generate_cclass_ref ( Cell );
841 #define cclass(cl)   (*generate_cclass_ref(cl))
842 #else
843 #define cclass(cl)   tabClass[(cl)-CCLASS_BASE_ADDR]
844 #endif
845
846 #define mkClass(n)   (CCLASS_BASE_ADDR+(n))
847 #define isClass(c)   (CCLASS_BASE_ADDR<=(c)                   \
848                       && (c)<CCLASS_BASE_ADDR+tabClassSz      \
849                       && tabClass[(c)-CCLASS_BASE_ADDR].inUse)
850
851 struct strClass {
852     Bool   inUse;
853     Name   nextFree;
854     Text   text;                        /* Name of class                   */
855     Int    line;                        /* Line where declaration begins   */
856     Module mod;                         /* module that declares it         */
857     Int    level;                       /* Level in class hierarchy        */
858     Int    arity;                       /* Number of arguments             */
859     Kinds  kinds;                       /* Kinds of constructors in class  */
860     List   fds;                         /* Functional Dependencies         */
861     List   xfds;                        /* Xpanded Functional Dependencies */
862     Cell   head;                        /* Head of class                   */
863     Name   dcon;                        /* Dictionary constructor function */
864     List   supers;                      /* :: [Pred]                       */
865     Int    numSupers;                   /* length(supers)                  */
866     List   dsels;                       /* Superclass dictionary selectors */
867     List   members;                     /* :: [Name]                       */
868     Int    numMembers;                  /* length(members)                 */
869     List   defaults;                    /* :: [Name]                       */
870     List   instances;                   /* :: [Inst]                       */
871 };
872
873 extern struct strClass* tabClass;
874 extern Int              tabClassSz;
875
876 extern Class newClass      ( Text );
877 extern Class findClass     ( Text );
878 extern Class addClass      ( Class );
879 extern Class findQualClass ( Cell );
880 extern Inst  newInst       ( Void );
881 extern Inst  findFirstInst ( Tycon );
882 extern Inst  findNextInst  ( Tycon,Inst );
883 extern List  getAllKnownTyconsAndClasses ( void );
884 extern Class findQualClassWithoutConsultingExportList ( QualId q );
885
886 /* --------------------------------------------------------------------------
887  * Character values:
888  * ------------------------------------------------------------------------*/
889
890 /* I think this assumes that NUM_CHARS==256. */
891 #define CHARR_MIN    3000
892 #define CHARR_MAX    3255
893 #define isChar(c)    (CHARR_MIN<=(c) && (c)<=CHARR_MAX)
894 #define charOf(c)    ((Char)((c)-CHARR_MIN))
895 #define mkChar(c)    (CHARR_MIN+(((Cell)(c)) & 0xFF))
896 #define MAXCHARVAL   (NUM_CHARS-1)
897
898 /* --------------------------------------------------------------------------
899  * Small Integer values:
900  * ------------------------------------------------------------------------*/
901
902 #define SMALL_INT_MIN   100000
903 #define SMALL_INT_MAX   499999
904 #define SMALL_INT_ZERO  (1 + SMALL_INT_MIN/2 + SMALL_INT_MAX/2)
905 #define isSmall(c)      (SMALL_INT_MIN<=(c) && (c)<=SMALL_INT_MAX)
906 extern  Bool isInt      ( Cell );
907 extern  Int  intOf      ( Cell );
908 extern  Cell mkInt      ( Int );
909
910 /* --------------------------------------------------------------------------
911  * Implementation of triples:
912  * ------------------------------------------------------------------------*/
913
914 #define triple(x,y,z) pair(x,pair(y,z))
915 #define fst3(c)      fst(c)
916 #define snd3(c)      fst(snd(c))
917 #define thd3(c)      snd(snd(c))
918
919 /* --------------------------------------------------------------------------
920  * Implementation of lists:
921  * ------------------------------------------------------------------------*/
922
923 #define NIL              0
924 #define isNull(c)        ((c)==NIL)
925 #define nonNull(c)       (c)
926 #define cons(x,xs)       pair(x,xs)
927 #define singleton(x)     cons(x,NIL)
928 #define doubleton(x,y)   cons(x,cons(y,NIL))
929 #define tripleton(x,y,z) cons(x,cons(y,cons(z,NIL)))
930 #define hd(c)            fst(c)
931 #define tl(c)            snd(c)
932
933 extern  Int          length       ( List );
934 extern  List         appendOnto   ( List,List );    /* destructive     */
935 extern  List         dupOnto      ( List,List );
936 extern  List         dupList      ( List );
937 extern  List         revOnto      ( List, List );   /* destructive     */
938 #define rev(xs)      revOnto((xs),NIL)              /* destructive     */
939 #define reverse(xs)  revOnto(dupList(xs),NIL)       /* non-destructive */
940 extern  Cell         cellIsMember ( Cell,List );
941 extern  Cell         cellAssoc    ( Cell,List );
942 extern  Cell         cellRevAssoc ( Cell,List );
943 extern  Bool         eqList       ( List,List );
944 extern  Cell         varIsMember  ( Text,List );
945 extern  Name         nameIsMember ( Text,List );
946 extern  QualId       qualidIsMember ( QualId, List );
947 extern  Cell         intIsMember  ( Int,List );
948 extern  List         replicate    ( Int,Cell );
949 extern  List         diffList     ( List,List );    /* destructive     */
950 extern  List         deleteCell   ( List,Cell );    /* non-destructive */
951 extern  List         take         ( Int,List );     /* destructive     */
952 extern  List         splitAt      ( Int,List );     /* non-destructive */
953 extern  Cell         nth          ( Int,List );
954 extern  List         removeCell   ( Cell,List );    /* destructive     */
955 extern  List         dupListOnto  ( List,List );    /* non-destructive */ 
956 extern  List         nubList      ( List );         /* non-destructive */
957
958 /* The following macros provide `inline expansion' of some common ways of
959  * traversing, using and modifying lists:
960  *
961  * N.B. We use the names _f, _a, _xs, Zs, in an attempt to avoid clashes
962  *      with identifiers used elsewhere.
963  */
964
965 #define mapBasic(_init,_step)           {List Zs=(_init);\
966                                          for(;nonNull(Zs);Zs=tl(Zs))  \
967                                          _step;}
968 #define mapModify(_init,_step)          mapBasic(_init,hd(Zs)=_step)
969
970 #define mapProc(_f,_xs)                 mapBasic(_xs,_f(hd(Zs)))
971 #define map1Proc(_f,_a,_xs)             mapBasic(_xs,_f(_a,hd(Zs)))
972 #define map2Proc(_f,_a,_b,_xs)          mapBasic(_xs,_f(_a,_b,hd(Zs)))
973 #define map3Proc(_f,_a,_b,_c,_xs)       mapBasic(_xs,_f(_a,_b,_c,hd(Zs)))
974 #define map4Proc(_f,_a,_b,_c,_d,_xs)    mapBasic(_xs,_f(_a,_b,_c,_d,hd(Zs)))
975
976 #define mapOver(_f,_xs)                 mapModify(_xs,_f(hd(Zs)))
977 #define map1Over(_f,_a,_xs)             mapModify(_xs,_f(_a,hd(Zs)))
978 #define map2Over(_f,_a,_b,_xs)          mapModify(_xs,_f(_a,_b,hd(Zs)))
979 #define map3Over(_f,_a,_b,_c,_xs)       mapModify(_xs,_f(_a,_b,_c,hd(Zs)))
980 #define map4Over(_f,_a,_b,_c,_d,_xs)    mapModify(_xs,_f(_a,_b,_c,_d,hd(Zs)))
981
982 /* This is just what you want for functions with accumulating parameters */
983 #define mapAccum(_f,_acc,_xs)           mapBasic(_xs,_acc=_f(_acc,hd(Zs)))
984 #define map1Accum(_f,_acc,_a,_xs)       mapBasic(_xs,_acc=_f(_acc,_a,hd(Zs)))
985 #define map2Accum(_f,_acc,_a,_b,_xs)    mapBasic(_xs,_acc=_f(_acc,_a,_b,hd(Zs)))
986 #define map3Accum(_f,_acc,_a,_b,_c,_xs) mapBasic(_xs,_acc=_f(_acc,_a,_b,_c,hd(Zs)))
987
988
989 /* --------------------------------------------------------------------------
990  * Strongly-typed lists (z-lists) and tuples (experimental)
991  * ------------------------------------------------------------------------*/
992
993 typedef Cell ZPair;
994 typedef Cell ZTriple;
995 typedef Cell Z4Ble;
996 typedef Cell Z5Ble;
997
998 #define isZPair(c) (whatIs((c))==ZTUP2)
999
1000 extern Cell zpair    ( Cell x1, Cell x2 );
1001 extern Cell zfst     ( Cell zpair );
1002 extern Cell zsnd     ( Cell zpair );
1003
1004 extern Cell ztriple  ( Cell x1, Cell x2, Cell x3 );
1005 extern Cell zfst3    ( Cell zpair );
1006 extern Cell zsnd3    ( Cell zpair );
1007 extern Cell zthd3    ( Cell zpair );
1008
1009 extern Cell z4ble    ( Cell x1, Cell x2, Cell x3, Cell x4 );
1010 extern Cell zsel14   ( Cell zpair );
1011 extern Cell zsel24   ( Cell zpair );
1012 extern Cell zsel34   ( Cell zpair );
1013 extern Cell zsel44   ( Cell zpair );
1014
1015 extern Cell z5ble    ( Cell x1, Cell x2, Cell x3, Cell x4, Cell x5 );
1016 extern Cell zsel15   ( Cell zpair );
1017 extern Cell zsel25   ( Cell zpair );
1018 extern Cell zsel35   ( Cell zpair );
1019 extern Cell zsel45   ( Cell zpair );
1020 extern Cell zsel55   ( Cell zpair );
1021
1022 extern Cell unap     ( int tag, Cell c );
1023
1024
1025 /* --------------------------------------------------------------------------
1026  * Implementation of function application nodes:
1027  * ------------------------------------------------------------------------*/
1028
1029 #define ap(f,x)      pair(f,x)
1030 #define ap1(f,x)     ap(f,x)
1031 #define ap2(f,x,y)   ap(ap(f,x),y)
1032 #define ap3(f,x,y,z) ap(ap(ap(f,x),y),z)
1033 #define fun(c)       fst(c)
1034 #define arg(c)       snd(c)
1035 #define isAp(c)      (isPair(c) && !isTag(fst(c)))
1036
1037 extern  Cell         getHead     ( Cell );
1038 extern  List         getArgs     ( Cell );
1039 extern  Cell         nthArg      ( Int,Cell );
1040 extern  Int          numArgs     ( Cell );
1041 extern  Cell         applyToArgs ( Cell,List );
1042 extern  Int          argCount;
1043
1044 /* --------------------------------------------------------------------------
1045  * Stack implementation:
1046  *
1047  * NB: Use of macros makes order of evaluation hard to predict.
1048  *     For example, "push(1+pop());" doesn't increment TOS.
1049  * ------------------------------------------------------------------------*/
1050
1051 extern  Cell cellStack[];
1052 extern  StackPtr sp;
1053
1054 #define clearStack() sp=(-1)
1055 #define stackEmpty() (sp==(-1))
1056 #define stack(p)     cellStack[p]
1057 #define chkStack(n)  if (sp>=NUM_STACK-(n)) hugsStackOverflow()
1058 #define push(c)      do { chkStack(1); onto(c); } while (0)
1059 #define onto(c)      stack(++sp)=(c);
1060 #define pop()        stack(sp--)
1061 #define drop()       sp--
1062 #define top()        stack(sp)
1063 #define pushed(n)    stack(sp-(n))
1064 #define topfun(f)    top()=ap((f),top())
1065 #define toparg(x)    top()=ap(top(),(x))
1066
1067 extern  Void hugsStackOverflow ( Void );
1068
1069 #if SYMANTEC_C
1070 #include <Memory.h>
1071 #define STACK_HEADROOM 16384
1072 #define STACK_CHECK if (StackSpace() <= STACK_HEADROOM) \
1073                       internal("Macintosh function parameter stack overflow.");
1074 #else
1075 #define STACK_CHECK
1076 #endif
1077
1078 /* --------------------------------------------------------------------------
1079  * Misc:
1080  * ------------------------------------------------------------------------*/
1081
1082 extern  Void   setLastExpr          ( Cell );
1083 extern  Cell   getLastExpr          ( Void );
1084 extern  List   addTyconsMatching    ( String,List );
1085 extern  List   addNamesMatching     ( String,List );
1086
1087 extern  Tycon  findTyconInAnyModule ( Text t );
1088 extern  Class  findClassInAnyModule ( Text t );
1089 extern  Name   findNameInAnyModule  ( Text t );
1090
1091 extern  Void   print                ( Cell, Int );
1092 extern  void   dumpTycon            ( Int t );
1093 extern  void   dumpName             ( Int n );
1094 extern  void   dumpClass            ( Int c );
1095 extern  void   dumpInst             ( Int i );
1096 extern  void   locateSymbolByName   ( Text t );
1097
1098 /*-------------------------------------------------------------------------*/