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