2 /* --------------------------------------------------------------------------
3 * Defines storage datatypes: Text, Name, Module, Tycon, Cell, List, Pair,
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.
12 * $RCSfile: storage.h,v $
14 * $Date: 2000/02/24 14:05:55 $
15 * ------------------------------------------------------------------------*/
17 /* --------------------------------------------------------------------------
18 * Typedefs for main data types:
19 * Many of these type names are used to indicate the intended us of a data
20 * item, rather than for type checking purposes. Sadly (although sometimes,
21 * fortunately), the C compiler cannot distinguish between the use of two
22 * different names defined to be synonyms for the same types.
23 * ------------------------------------------------------------------------*/
25 typedef Int Text; /* text string */
26 typedef Unsigned Syntax; /* syntax (assoc,preced) */
27 typedef Int Cell; /* general cell value */
28 typedef Cell far *Heap; /* storage of heap */
29 typedef Cell Pair; /* pair cell */
30 typedef Int StackPtr; /* stack pointer */
31 typedef Cell Offset; /* offset/generic variable*/
32 typedef Int Script; /* script file number */
33 typedef Int Module; /* module */
34 typedef Cell Tycon; /* type constructor */
35 typedef Cell Type; /* type expression */
36 typedef Cell Kind; /* kind expression */
37 typedef Cell Kinds; /* list of kinds */
38 typedef Cell Constr; /* constructor expression */
39 typedef Cell Name; /* named value */
40 typedef Cell Class; /* type class */
41 typedef Cell Inst; /* instance of type class */
42 typedef Cell Triple; /* triple of cell values */
43 typedef Cell List; /* list of cells */
44 typedef Cell Bignum; /* bignum integer */
45 typedef Cell Float; /* floating pt literal */
47 typedef Cell Ext; /* extension label */
53 typedef Cell ConVarId;
55 /* --------------------------------------------------------------------------
57 * provides storage for the characters making up identifier and symbol
58 * names, string literals, character constants etc...
59 * ------------------------------------------------------------------------*/
61 extern String textToStr Args((Text));
62 extern Text findText Args((String));
63 extern Text inventText Args((Void));
64 extern Text inventDictText Args((Void));
65 extern Bool inventedText Args((Text));
66 extern Text enZcodeThenFindText Args((String));
67 extern Text unZcodeThenFindText Args((String));
69 /* Variants of textToStr and syntaxOf which work for idents, ops whether
70 * qualified or unqualified.
72 extern String identToStr Args((Cell));
73 extern Text fixLitText Args((Text));
74 extern Syntax identSyntax Args((Cell));
75 extern Syntax defaultSyntax Args((Text));
77 /* --------------------------------------------------------------------------
78 * Specification of syntax (i.e. default written form of application)
79 * ------------------------------------------------------------------------*/
81 #define MIN_PREC 0 /* weakest binding operator */
82 #define MAX_PREC 9 /* strongest binding operator */
83 #define FUN_PREC (MAX_PREC+2) /* binding of function symbols */
84 #define DEF_PREC MAX_PREC
85 #define APPLIC 0 /* written applicatively */
86 #define LEFT_ASS 1 /* left associative infix */
87 #define RIGHT_ASS 2 /* right associative infix */
88 #define NON_ASS 3 /* non associative infix */
89 #define DEF_ASS LEFT_ASS
91 #define UMINUS_PREC 6 /* Change these settings at your */
92 #define UMINUS_ASSOC LEFT_ASS /* own risk; they may not work! */
94 #define assocOf(x) ((x)&NON_ASS)
95 #define precOf(x) ((x)>>2)
96 #define mkSyntax(a,p) ((a)|((p)<<2))
97 #define DEF_OPSYNTAX mkSyntax(DEF_ASS,DEF_PREC)
98 #define NO_SYNTAX (-1)
100 extern Void addSyntax Args((Int,Text,Syntax));
101 extern Syntax syntaxOf Args((Text));
103 /* --------------------------------------------------------------------------
105 * Provides a garbage collectable heap for storage of expressions etc.
106 * ------------------------------------------------------------------------*/
108 #define heapAlloc(s) (Heap)(farCalloc(s,sizeof(Cell)))
109 #define heapBuilt() (heapFst)
111 extern Heap heapFst, heapSnd;
112 extern Heap heapTopFst;
113 extern Heap heapTopSnd;
114 extern Bool consGC; /* Set to FALSE to turn off gc from*/
115 /* C stack; use with extreme care! */
116 extern Int cellsRecovered; /* cells recovered by last gc */
118 #define fst(c) heapTopFst[c]
119 #define snd(c) heapTopSnd[c]
121 extern Pair pair Args((Cell,Cell));
122 extern Void garbageCollect Args((Void));
124 extern Void overwrite Args((Pair,Pair));
125 extern Void overwrite2 Args((Pair,Cell,Cell));
126 extern Cell markExpr Args((Cell));
127 extern Void markWithoutMove Args((Cell));
129 #define mark(v) v=markExpr(v)
131 #define isPair(c) ((c)<0)
132 #define isGenPair(c) ((c)<0 && -heapSize<=(c))
134 extern Cell whatIs Args((Cell));
136 /* --------------------------------------------------------------------------
137 * Box cell tags are used as the fst element of a pair to indicate that
138 * the snd element of the pair is to be treated in some special way, other
139 * than as a Cell. Examples include holding integer values, variable name
140 * and string text etc.
141 * ------------------------------------------------------------------------*/
143 #define TAGMIN 1 /* Box and constructor cell tag values */
144 #define BCSTAG 30 /* Box=TAGMIN..BCSTAG-1 */
145 #define isTag(c) (TAGMIN<=(c) && (c)<SPECMIN) /* Tag cell values */
146 #define isBoxTag(c) (TAGMIN<=(c) && (c)<BCSTAG) /* Box cell tag values */
147 #define isConTag(c) (BCSTAG<=(c) && (c)<SPECMIN) /* Constr cell tag values*/
149 #define FREECELL 3 /* Free list cell: snd :: Cell */
150 #define VARIDCELL 4 /* Identifier variable: snd :: Text */
151 #define VAROPCELL 5 /* Operator variable: snd :: Text */
152 #define DICTVAR 6 /* Dictionary variable: snd :: Text */
153 #define CONIDCELL 7 /* Identifier constructor: snd :: Text */
154 #define CONOPCELL 8 /* Operator constructor: snd :: Text */
155 #define STRCELL 9 /* String literal: snd :: Text */
156 #define INTCELL 10 /* Int literal: snd :: Int */
157 #define ADDPAT 11 /* (_+k) pattern discr: snd :: Int */
158 #define FLOATCELL 15 /* Floating Pt literal: snd :: Text */
159 #define BIGCELL 16 /* Integer literal: snd :: Text */
161 #define PTRCELL 17 /* C Heap Pointer snd :: Ptr */
163 #define IPCELL 19 /* Imp Param Cell: snd :: Text */
164 #define IPVAR 20 /* ?x: snd :: Text */
166 #define CPTRCELL 21 /* Native code pointer snd :: Ptr */
169 #define EXTCOPY 22 /* Copy of an Ext: snd :: Text */
172 #define qmodOf(c) (textOf(fst(snd(c)))) /* c :: QUALIDENT */
173 #define qtextOf(c) (textOf(snd(snd(c)))) /* c :: QUALIDENT */
174 #define mkVar(t) ap(VARIDCELL,t)
175 #define mkVarop(t) ap(VAROPCELL,t)
176 #define mkCon(t) ap(CONIDCELL,t)
177 #define mkConop(t) ap(CONOPCELL,t)
178 #define mkQVar(m,t) ap(QUALIDENT,pair(mkCon(m),mkVar(t)))
179 #define mkQCon(m,t) ap(QUALIDENT,pair(mkCon(m),mkCon(t)))
180 #define mkQVarOp(m,t) ap(QUALIDENT,pair(mkCon(m),mkVarop(t)))
181 #define mkQConOp(m,t) ap(QUALIDENT,pair(mkCon(m),mkConop(t)))
182 #define mkQualId(m,t) ap(QUALIDENT,pair(m,t))
183 #define intValOf(c) (snd(c))
184 #define inventVar() mkVar(inventText())
185 #define mkDictVar(t) ap(DICTVAR,t)
186 #define inventDictVar() mkDictVar(inventDictText())
187 #define mkStr(t) ap(STRCELL,t)
189 #define mkIParam(c) ap(IPCELL,snd(c))
190 #define isIP(p) (whatIs(p) == IPCELL)
191 #define ipMatch(pi, t) (isIP(fun(pi)) && textOf(fun(pi)) == t)
192 #define ipVar(pi) textOf(fun(pi))
194 #define isIP(p) FALSE
197 extern Bool isVar Args((Cell));
198 extern Bool isCon Args((Cell));
199 extern Bool isQVar Args((Cell));
200 extern Bool isQCon Args((Cell));
201 extern Bool isQualIdent Args((Cell));
202 extern Bool eqQualIdent ( QualId c1, QualId c2 );
203 extern Bool isIdent Args((Cell));
204 extern String stringNegate Args((String));
205 extern Text textOf Args((Cell));
207 #define isFloat(c) (isPair(c) && fst(c)==FLOATCELL)
208 #define stringToFloat(s) pair(FLOATCELL,findText(s))
209 #define floatToString(f) textToStr(snd(f))
210 #define floatOf(f) atof(floatToString(f))
211 #define mkFloat(f) (f) /* ToDo: is this right? */
212 #define floatNegate(f) stringToFloat(stringNegate(floatToString(f)))
214 #define stringToBignum(s) pair(BIGCELL,findText(s))
215 #define bignumToString(b) textToStr(snd(b))
218 #define isPtr(c) (isPair(c) && fst(c)==PTRCELL)
219 extern Cell mkPtr Args((Ptr));
220 extern Ptr ptrOf Args((Cell));
221 #define isCPtr(c) (isPair(c) && fst(c)==CPTRCELL)
222 extern Cell mkCPtr Args((Ptr));
223 extern Ptr cptrOf Args((Cell));
226 /* --------------------------------------------------------------------------
227 * Constructor cell tags are used as the fst element of a pair to indicate
228 * a particular syntactic construct described by the snd element of the
230 * Note that a cell c will not be treated as an application (AP/isAp) node
231 * if its first element is a constructor cell tag, whereas a cell whose fst
232 * element is a special cell will be treated as an application node.
233 * ------------------------------------------------------------------------*/
235 #define LETREC 30 /* LETREC snd :: ([Decl],Exp) */
236 #define COND 31 /* COND snd :: (Exp,Exp,Exp) */
237 #define LAMBDA 32 /* LAMBDA snd :: Alt */
238 #define FINLIST 33 /* FINLIST snd :: [Exp] */
239 #define DOCOMP 34 /* DOCOMP snd :: (Exp,[Qual]) */
240 #define BANG 35 /* BANG snd :: Type */
241 #define COMP 36 /* COMP snd :: (Exp,[Qual]) */
242 #define ASPAT 37 /* ASPAT snd :: (Var,Exp) */
243 #define ESIGN 38 /* ESIGN snd :: (Exp,Type) */
244 #define RSIGN 39 /* RSIGN snd :: (Rhs,Type) */
245 #define CASE 40 /* CASE snd :: (Exp,[Alt]) */
246 #define NUMCASE 41 /* NUMCASE snd :: (Exp,Disc,Rhs) */
247 #define FATBAR 42 /* FATBAR snd :: (Exp,Exp) */
248 #define LAZYPAT 43 /* LAZYPAT snd :: Exp */
249 #define DERIVE 45 /* DERIVE snd :: Cell */
251 #define FLOATCELL 46 /* FLOATCELL snd :: (Int,Int) */
254 #define BOOLQUAL 49 /* BOOLQUAL snd :: Exp */
255 #define QWHERE 50 /* QWHERE snd :: [Decl] */
256 #define FROMQUAL 51 /* FROMQUAL snd :: (Exp,Exp) */
257 #define DOQUAL 52 /* DOQUAL snd :: Exp */
258 #define MONADCOMP 53 /* MONADCOMP snd :: ((m,m0),(Exp,[Qual])*/
260 #define GUARDED 54 /* GUARDED snd :: [guarded exprs] */
262 #define ARRAY 55 /* Array snd :: (Bounds,[Values]) */
263 #define MUTVAR 56 /* Mutvar snd :: Cell */
265 #define HUGSOBJECT 57 /* HUGSOBJECT snd :: Cell */
269 #define WITHEXP 58 /* WITHEXP snd :: [(Var,Exp)] */
273 #define POLYTYPE 60 /* POLYTYPE snd :: (Kind,Type) */
274 #define QUAL 61 /* QUAL snd :: ([Classes],Type) */
275 #define RANK2 62 /* RANK2 snd :: (Int,Type) */
276 #define EXIST 63 /* EXIST snd :: (Int,Type) */
277 #define POLYREC 64 /* POLYREC snd :: (Int,Type) */
278 #define BIGLAM 65 /* BIGLAM snd :: (vars,patterns) */
279 #define CDICTS 66 /* CDICTS snd :: ([Pred],Type) */
281 #define LABC 67 /* LABC snd :: (con,[(Vars,Type)]) */
282 #define CONFLDS 68 /* CONFLDS snd :: (con,[Field]) */
283 #define UPDFLDS 69 /* UPDFLDS snd :: (Exp,[con],[Field]) */
285 #define RECORD 70 /* RECORD snd :: [Val] */
286 #define EXTCASE 71 /* EXTCASE snd :: (Exp,Disc,Rhs) */
287 #define RECSEL 72 /* RECSEL snd :: Ext */
289 #define IMPDEPS 73 /* IMPDEPS snd :: [Binding] */
291 #define QUALIDENT 74 /* Qualified identifier snd :: (Id,Id) */
292 #define HIDDEN 75 /* hiding import list snd :: [Entity] */
293 #define MODULEENT 76 /* module in export list snd :: con */
295 #define INFIX 77 /* INFIX snd :: (see tidyInfix) */
296 #define ONLY 78 /* ONLY snd :: Exp */
297 #define NEG 79 /* NEG snd :: Exp */
299 /* Used when parsing GHC interface files */
300 #define DICTAP 80 /* DICTAP snd :: (QClassId,[Type]) */
301 #define UNBOXEDTUP 81 /* UNBOXEDTUP snd :: [Type] */
303 #if SIZEOF_VOID_P != SIZEOF_INT
304 wuiwiuwuiwmn SIZEOF_INT cc
305 #define PTRCELL 82 /* C Heap Pointer snd :: (Int,Int) */
309 #define STGVAR 92 /* STGVAR snd :: (StgRhs,info) */
310 #define STGAPP 93 /* STGAPP snd :: (StgVar,[Arg]) */
311 #define STGPRIM 94 /* STGPRIM snd :: (PrimOp,[Arg]) */
312 #define STGCON 95 /* STGCON snd :: (StgCon,[Arg]) */
313 #define PRIMCASE 96 /* PRIMCASE snd :: (Expr,[PrimAlt]) */
314 #define DEEFALT 97 /* DEEFALT snd :: (Var,Expr) */
315 #define CASEALT 98 /* CASEALT snd :: (Con,[Var],Expr) */
316 #define PRIMALT 99 /* PRIMALT snd :: ([Var],Expr) */
320 Top-level interface entities
321 type Line = Int -- a line number
322 type ConVarId = CONIDCELL | VARIDCELL
324 type ExportListEntry = ConVarId | (ConId, <ConVarId>)
325 type Associativity = mkInt of LEFT_ASS | RIGHT_ASS | NON_ASS
326 type Constr = ((ConId, [((Type,VarId,Int))]))
327 ((constr name, [((type, field name if any, strictness))]))
328 strictness: 0 => none, 1 => !, 2 => !! (unpacked)
329 All 2/3/4/5 tuples in the interface abstract syntax are done with
333 #define I_INTERFACE 109 /* snd :: ((ConId, [I_IMPORT..I_VALUE]))
334 interface name, list of iface entities */
336 #define I_IMPORT 110 /* snd :: ((ConId, [ConVarId]))
337 module name, list of entities */
339 #define I_INSTIMPORT 111 /* snd :: NIL -- not used at present */
341 #define I_EXPORT 112 /* snd :: ((ConId, [ExportListEntry]))
342 this module name?, entities to export */
344 #define I_FIXDECL 113 /* snd :: ((NIL|Int, Associativity, ConVarId))
345 fixity, associativity, name */
347 #define I_INSTANCE 114 /* snd :: ((Line,
351 forall-y bit (eg __forall [a b] =>),
352 other bit, eg { C a1 } -> { C2 a2 } -> ... -> { Cn an },
353 name of dictionary builder,
354 (after startGHCInstance) the instance table location */
356 #define I_TYPE 115 /* snd :: ((Line, ConId, [((VarId,Kind))], Type))
357 lineno, tycon, kinded tyvars, the type expr */
359 #define I_DATA 116 /* snd :: ((Line, [((QConId,VarId))], ConId,
360 [((VarId,Kind))], [Constr])
361 lineno, context, tycon, kinded tyvars, constrs
362 An empty constr list means exported abstractly. */
364 #define I_NEWTYPE 117 /* snd :: ((Line, [((QConId,VarId))], ConId,
365 [((VarId,Kind))], ((ConId,Type)) ))
366 lineno, context, tycon, kinded tyvars, constr
367 constr==NIL means exported abstractly. */
369 #define I_CLASS 118 /* snd :: ((Line, [((QConId,VarId))], ConId,
370 [((VarId,Kind))], [((VarId,Type))]))
371 lineno, context, classname,
372 kinded tyvars, method sigs */
374 #define I_VALUE 119 /* snd :: ((Line, VarId, Type)) */
380 #define ZCONS 190 /* snd :: (Cell,Cell) */
384 #define ZTUP2 192 /* snd :: (Cell,Cell) */
385 #define ZTUP3 193 /* snd :: (Cell,(Cell,Cell)) */
386 #define ZTUP4 194 /* snd :: (Cell,(Cell,(Cell,Cell))) */
387 #define ZTUP5 195 /* snd :: (Cell,(Cell,(Cell,(Cell,Cell)))) */
389 /* Last constructor tag must be less than SPECMIN */
391 /* --------------------------------------------------------------------------
392 * Special cell values:
393 * ------------------------------------------------------------------------*/
398 #define isSpec(c) (SPECMIN<=(c) && (c)<EXTMIN)/* Special cell values */
400 #define isSpec(c) (SPECMIN<=(c) && (c)<OFFMIN)
403 #define NONE 201 /* Dummy stub */
404 #define STAR 202 /* Representing the kind of types */
406 #define ROW 203 /* Representing the kind of rows */
408 #define WILDCARD 204 /* Wildcard pattern */
409 #define SKOLEM 205 /* Skolem constant */
411 #define DOTDOT 206 /* ".." in import/export list */
413 #define NAME 210 /* whatIs code for isName */
414 #define TYCON 211 /* whatIs code for isTycon */
415 #define CLASS 212 /* whatIs code for isClass */
416 #define MODULE 213 /* whatIs code for isModule */
417 #define INSTANCE 214 /* whatIs code for isInst */
418 #define TUPLE 215 /* whatIs code for tuple constructor */
419 #define OFFSET 216 /* whatis code for offset */
420 #define AP 217 /* whatIs code for application node */
421 #define CHARCELL 218 /* whatIs code for isChar */
423 #define EXT 219 /* whatIs code for isExt */
426 #define SIGDECL 220 /* Signature declaration */
427 #define FIXDECL 221 /* Fixity declaration */
428 #define FUNBIND 222 /* Function binding */
429 #define PATBIND 223 /* Pattern binding */
431 #define DATATYPE 230 /* Datatype type constructor */
432 #define NEWTYPE 231 /* Newtype type constructor */
433 #define SYNONYM 232 /* Synonym type constructor */
434 #define RESTRICTSYN 233 /* Synonym with restricted scope */
436 #define NODEPENDS 235 /* Stop calculation of deps in type check*/
437 #define PREDEFINED 236 /* Predefined name, not yet filled */
439 /* --------------------------------------------------------------------------
440 * Tuple data/type constructors:
441 * ------------------------------------------------------------------------*/
443 extern Text ghcTupleText Args((Tycon));
444 extern Text ghcTupleText_n Args((Int));
450 #define isExt(c) (EXTMIN<=(c) && (c)<OFFMIN)
451 #define extText(e) tabExt[(e)-EXTMIN]
452 #define extField(c) arg(fun(c))
453 #define extRow(c) arg(c)
455 extern Text DECTABLE(tabExt);
456 extern Ext mkExt Args((Text));
461 extern Module findFakeModule ( Text t );
462 extern Tycon addTupleTycon ( Int n );
463 extern Name addWiredInBoxingTycon
464 ( String modNm, String typeNm, String constrNm,
465 Int rep, Kind kind );
466 Tycon addWiredInEnumTycon ( String modNm, String typeNm,
467 List /*of Text*/ constrs );
469 /* --------------------------------------------------------------------------
470 * Offsets: (generic types/stack offsets)
471 * ------------------------------------------------------------------------*/
474 #define OFFMIN (EXTMIN+NUM_EXT)
478 #define isOffset(c) (OFFMIN<=(c) && (c)<MODMIN)
479 #define offsetOf(c) ((c)-OFFMIN)
480 #define mkOffset(o) (OFFMIN+(o))
483 /* --------------------------------------------------------------------------
485 * ------------------------------------------------------------------------*/
487 #define MODMIN (OFFMIN+NUM_OFFSETS)
489 #define isModule(c) (MODMIN<=(c) && (c)<TYCMIN)
490 #define mkModule(n) (MODMIN+(n))
491 #define module(n) tabModule[(n)-MODMIN]
493 /* Import defns for the ObjectCode struct in Module. */
496 /* Under Haskell 1.3, the list of qualified imports is always a subset
497 * of the list of unqualified imports. For simplicity and flexibility,
498 * we do not attempt to exploit this fact - when a module is imported
499 * unqualified, it is added to both the qualified and unqualified
501 * Similarily, Haskell 1.3 does not allow a constructor to be imported
502 * or exported without exporting the type it belongs to but the export
503 * list is just a flat list of Texts (before static analysis) or
504 * Tycons, Names and Classes (after static analysis).
508 /* Lists of top level objects (local defns + imports) */
512 List exports; /* [ Entity | (Entity, NIL|DOTDOT) ] */
513 /* List of qualified imports. Used both during compilation and when
514 * evaluating an expression in the context of the current module.
518 /* TRUE if module exists only via GHC primop defn; usually FALSE */
521 /* The primary object file for this module. */
524 /* And any extras it might need. */
525 ObjectCode* objectExtras;
526 List objectExtraNames; /* :: [Text] -- names of extras */
530 extern Module currentModule; /* Module currently being processed */
531 extern struct Module DECTABLE(tabModule);
533 extern Bool isValidModule Args((Module));
534 extern Module newModule Args((Text));
535 extern Module findModule Args((Text));
536 extern Module findModid Args((Cell));
537 extern Void setCurrModule Args((Module));
539 extern void addOTabName Args((Module,char*,void*));
540 extern void* lookupOTabName Args((Module,char*));
541 extern char* nameFromOPtr Args((void*));
543 extern void addSection Args((Module,void*,void*,OSectionKind));
544 extern OSectionKind lookupSection Args((void*));
545 extern void* lookupOExtraTabName ( char* sym );
547 #define isPrelude(m) (m==modulePrelude)
549 #define N_PRELUDE_SCRIPTS (combined ? 30 : 1)
551 /* --------------------------------------------------------------------------
552 * Type constructor names:
553 * ------------------------------------------------------------------------*/
555 #define TYCMIN (MODMIN+NUM_MODULE)
556 #define isTycon(c) (TYCMIN<=(c) && (c)<NAMEMIN && tabTycon[(c)-TYCMIN].tuple==-1)
557 #define tycon(n) tabTycon[(n)-TYCMIN]
559 #define isTuple(c) (TYCMIN<=(c) && (c)<NAMEMIN && tabTycon[(c)-TYCMIN].tuple>=0)
560 #define tupleOf(n) (tabTycon[(n)-TYCMIN].tuple)
561 extern Tycon mkTuple ( Int );
567 Module mod; /* module that defines it */
568 Int tuple; /* tuple number, or -1 if not tuple */
570 Kind kind; /* kind (includes arity) of Tycon */
571 Cell what; /* DATATYPE/SYNONYM/RESTRICTSYN... */
573 Name conToTag; /* used in derived code */
575 void* itbl; /* For tuples, the info tbl pointer */
579 extern struct strTycon DECTABLE(tabTycon);
581 extern Tycon newTycon Args((Text));
582 extern Tycon findTycon Args((Text));
583 extern Tycon addTycon Args((Tycon));
584 extern Tycon findQualTycon Args((Cell));
585 extern Tycon addPrimTycon Args((Text,Kind,Int,Cell,Cell));
587 #define isSynonym(h) (isTycon(h) && tycon(h).what==SYNONYM)
588 #define isQualType(t) (isPair(t) && fst(t)==QUAL)
589 #define mkPolyType(n,t) pair(POLYTYPE,pair(n,t))
590 #define isPolyType(t) (isPair(t) && fst(t)==POLYTYPE)
591 #define isPolyOrQualType(t) (isPair(t) && (fst(t)==POLYTYPE || fst(t)==QUAL))
592 #define polySigOf(t) fst(snd(t))
593 #define monotypeOf(t) snd(snd(t))
595 #define bang(t) ap(BANG,t)
596 extern Tycon findQualTyconWithoutConsultingExportList ( QualId q );
598 /* --------------------------------------------------------------------------
599 * Globally defined name values:
600 * ------------------------------------------------------------------------*/
602 #define NAMEMIN (TYCMIN+NUM_TYCON)
603 #define isName(c) (NAMEMIN<=(c) && (c)<INSTMIN)
604 #define mkName(n) (NAMEMIN+(n))
605 #define name(n) tabName[(n)-NAMEMIN]
610 Module mod; /* module that defines it */
617 Cell stgVar; /* really StgVar */
618 Text callconv; /* for foreign import/export */
619 void* primop; /* really StgPrim* */
620 void* itbl; /* For constructors, the info tbl pointer */
624 extern int numNames Args(( Void ));
626 extern struct strName DECTABLE(tabName);
628 /* The number field in a name is used to distinguish various kinds of name:
629 * mfunNo(i) = code for member function, offset i
630 * members that are sole elements of dict use mfunNo(0)
631 * members of dicts with more than one elem use mfunNo(n), n>=1
632 * EXECNAME = code for executable name (bytecodes or primitive)
633 * SELNAME = code for selector function
634 * DFUNNAME = code for dictionary builder or selector
635 * cfunNo(i) = code for data constructor
636 * datatypes with only one constructor uses cfunNo(0)
637 * datatypes with multiple constructors use cfunNo(n), n>=1
645 #define isSfun(n) (name(n).number==SELNAME)
646 #define isDfun(n) (name(n).number==DFUNNAME)
648 #define isCfun(n) (name(n).number>=CFUNNAME)
649 #define cfunOf(n) (name(n).number-CFUNNAME)
650 #define cfunNo(i) ((i)+CFUNNAME)
651 #define hasCfun(cs) (nonNull(cs) && isCfun(hd(cs)))
653 #define isMfun(n) (name(n).number<0)
654 #define mfunOf(n) ((-1)-name(n).number)
655 #define mfunNo(i) ((-1)-(i))
657 extern Name newName Args((Text,Cell));
658 extern Name findName Args((Text));
659 extern Name addName Args((Name));
660 extern Name findQualName Args((Cell));
661 extern Name addPrimCfun Args((Text,Int,Int,Cell));
662 extern Name addPrimCfunREP Args((Text,Int,Int,Int));
663 extern Int sfunPos Args((Name,Name));
664 extern Name nameFromStgVar Args((Cell));
665 extern Name jrsFindQualName Args((Text,Text));
667 extern Name findQualNameWithoutConsultingExportList ( QualId q );
669 /* --------------------------------------------------------------------------
671 * ------------------------------------------------------------------------*/
673 #define INSTMIN (NAMEMIN+NUM_NAME) /* instances */
674 #define isInst(c) (INSTMIN<=(c) && (c)<CLASSMIN)
675 #define mkInst(n) (INSTMIN+(n))
676 #define instOf(c) ((Int)((c)-INSTMIN))
677 #define inst(in) tabInst[(in)-INSTMIN]
680 Class c; /* class C */
682 Module mod; /* module that defines it */
683 Kinds kinds; /* Kinds of variables in head */
684 Cell head; /* :: Pred */
685 List specifics; /* :: [Pred] */
686 Int numSpecifics; /* length(specifics) */
688 Name builder; /* Dictionary constructor function */
691 /* a predicate (an element :: Pred) is an application of a Class to one or
692 * more type expressions
695 #define CLASSMIN (INSTMIN+NUM_INSTS)
696 #define isClass(c) (CLASSMIN<=(c) && (c)<CHARMIN)
697 #define mkClass(n) (CLASSMIN+(n))
698 #define cclass(n) tabClass[(n)-CLASSMIN]
701 Text text; /* Name of class */
702 Int line; /* Line where declaration begins */
703 Module mod; /* module that declares it */
704 Int level; /* Level in class hierarchy */
705 Int arity; /* Number of arguments */
706 Kinds kinds; /* Kinds of constructors in class */
707 List fds; /* Functional Dependencies */
708 List xfds; /* Xpanded Functional Dependencies */
709 Cell head; /* Head of class */
710 Name dcon; /* Dictionary constructor function */
711 List supers; /* :: [Pred] */
712 Int numSupers; /* length(supers) */
713 List dsels; /* Superclass dictionary selectors */
714 List members; /* :: [Name] */
715 Int numMembers; /* length(members) */
716 List defaults; /* :: [Name] */
717 List instances; /* :: [Inst] */
720 extern struct strClass DECTABLE(tabClass);
721 extern struct strInst far *tabInst;
723 extern Class newClass Args((Text));
724 extern Class classMax Args((Void));
725 extern Class findClass Args((Text));
726 extern Class addClass Args((Class));
727 extern Class findQualClass Args((Cell));
728 extern Inst newInst Args((Void));
729 extern Inst findFirstInst Args((Tycon));
730 extern Inst findNextInst Args((Tycon,Inst));
731 extern List getAllKnownTyconsAndClasses ( void );
732 extern Class findQualClassWithoutConsultingExportList ( QualId q );
734 /* --------------------------------------------------------------------------
736 * ------------------------------------------------------------------------*/
738 #define CHARMIN (CLASSMIN+NUM_CLASSES)
739 #define MAXCHARVAL (NUM_CHARS-1)
740 #define isChar(c) (CHARMIN<=(c) && (c)<INTMIN)
741 #define charOf(c) ((Char)(c-CHARMIN))
742 #define mkChar(c) ((Cell)(CHARMIN+(((unsigned)(c))%NUM_CHARS)))
744 /* --------------------------------------------------------------------------
745 * Small Integer values:
746 * ------------------------------------------------------------------------*/
748 #define INTMIN (CHARMIN+NUM_CHARS)
749 #define INTMAX (MAXPOSINT)
750 #define isSmall(c) (INTMIN<=(c))
751 #define INTZERO (INTMIN/2 + INTMAX/2)
752 #define MINSMALLINT (INTMIN - INTZERO)
753 #define MAXSMALLINT (INTMAX - INTZERO)
754 #define mkDigit(c) ((Cell)((c)+INTMIN))
755 #define digitOf(c) ((Int)((c)-INTMIN))
757 extern Bool isInt Args((Cell));
758 extern Int intOf Args((Cell));
759 extern Cell mkInt Args((Int));
761 /* --------------------------------------------------------------------------
762 * Implementation of triples:
763 * ------------------------------------------------------------------------*/
765 #define triple(x,y,z) pair(x,pair(y,z))
766 #define fst3(c) fst(c)
767 #define snd3(c) fst(snd(c))
768 #define thd3(c) snd(snd(c))
770 /* --------------------------------------------------------------------------
771 * Implementation of lists:
772 * ------------------------------------------------------------------------*/
775 #define isNull(c) ((c)==NIL)
776 #define nonNull(c) (c)
777 #define cons(x,xs) pair(x,xs)
778 #define singleton(x) cons(x,NIL)
779 #define doubleton(x,y) cons(x,cons(y,NIL))
780 #define tripleton(x,y,z) cons(x,cons(y,cons(z,NIL)))
784 extern Int length Args((List));
785 extern List appendOnto Args((List,List)); /* destructive */
786 extern List dupOnto Args((List,List));
787 extern List dupList Args((List));
788 extern List revOnto Args((List, List)); /* destructive */
789 #define rev(xs) revOnto((xs),NIL) /* destructive */
790 #define reverse(xs) revOnto(dupList(xs),NIL) /* non-destructive */
791 extern Cell cellIsMember Args((Cell,List));
792 extern Cell cellAssoc Args((Cell,List));
793 extern Cell cellRevAssoc Args((Cell,List));
794 extern Bool eqList Args((List,List));
795 extern Cell varIsMember Args((Text,List));
796 extern Name nameIsMember Args((Text,List));
797 extern QualId qualidIsMember ( QualId, List );
798 extern Cell intIsMember Args((Int,List));
799 extern List replicate Args((Int,Cell));
800 extern List diffList Args((List,List)); /* destructive */
801 extern List deleteCell Args((List,Cell)); /* non-destructive */
802 extern List take Args((Int,List)); /* destructive */
803 extern List splitAt Args((Int,List)); /* non-destructive */
804 extern Cell nth Args((Int,List));
805 extern List removeCell Args((Cell,List)); /* destructive */
806 extern List dupListOnto Args((List,List)); /* non-destructive */
807 extern List nubList Args((List)); /* non-destructive */
809 /* The following macros provide `inline expansion' of some common ways of
810 * traversing, using and modifying lists:
812 * N.B. We use the names _f, _a, _xs, Zs, in an attempt to avoid clashes
813 * with identifiers used elsewhere.
816 #define mapBasic(_init,_step) {List Zs=(_init);\
817 for(;nonNull(Zs);Zs=tl(Zs)) \
819 #define mapModify(_init,_step) mapBasic(_init,hd(Zs)=_step)
821 #define mapProc(_f,_xs) mapBasic(_xs,_f(hd(Zs)))
822 #define map1Proc(_f,_a,_xs) mapBasic(_xs,_f(_a,hd(Zs)))
823 #define map2Proc(_f,_a,_b,_xs) mapBasic(_xs,_f(_a,_b,hd(Zs)))
824 #define map3Proc(_f,_a,_b,_c,_xs) mapBasic(_xs,_f(_a,_b,_c,hd(Zs)))
825 #define map4Proc(_f,_a,_b,_c,_d,_xs) mapBasic(_xs,_f(_a,_b,_c,_d,hd(Zs)))
827 #define mapOver(_f,_xs) mapModify(_xs,_f(hd(Zs)))
828 #define map1Over(_f,_a,_xs) mapModify(_xs,_f(_a,hd(Zs)))
829 #define map2Over(_f,_a,_b,_xs) mapModify(_xs,_f(_a,_b,hd(Zs)))
830 #define map3Over(_f,_a,_b,_c,_xs) mapModify(_xs,_f(_a,_b,_c,hd(Zs)))
831 #define map4Over(_f,_a,_b,_c,_d,_xs) mapModify(_xs,_f(_a,_b,_c,_d,hd(Zs)))
833 /* This is just what you want for functions with accumulating parameters */
834 #define mapAccum(_f,_acc,_xs) mapBasic(_xs,_acc=_f(_acc,hd(Zs)))
835 #define map1Accum(_f,_acc,_a,_xs) mapBasic(_xs,_acc=_f(_acc,_a,hd(Zs)))
836 #define map2Accum(_f,_acc,_a,_b,_xs) mapBasic(_xs,_acc=_f(_acc,_a,_b,hd(Zs)))
837 #define map3Accum(_f,_acc,_a,_b,_c,_xs) mapBasic(_xs,_acc=_f(_acc,_a,_b,_c,hd(Zs)))
840 /* --------------------------------------------------------------------------
841 * Strongly-typed lists (z-lists) and tuples (experimental)
842 * ------------------------------------------------------------------------*/
845 typedef Cell ZTriple;
851 extern Cell zcons ( Cell x, Cell xs );
852 extern Cell zhd ( Cell xs );
853 extern Cell ztl ( Cell xs );
854 extern Cell zsingleton ( Cell x );
855 extern Cell zdoubleton ( Cell x, Cell y );
856 extern Int zlength ( ZList xs );
857 extern ZList zreverse ( ZList xs );
860 extern Cell zpair ( Cell x1, Cell x2 );
861 extern Cell zfst ( Cell zpair );
862 extern Cell zsnd ( Cell zpair );
864 extern Cell ztriple ( Cell x1, Cell x2, Cell x3 );
865 extern Cell zfst3 ( Cell zpair );
866 extern Cell zsnd3 ( Cell zpair );
867 extern Cell zthd3 ( Cell zpair );
869 extern Cell z4ble ( Cell x1, Cell x2, Cell x3, Cell x4 );
870 extern Cell zsel14 ( Cell zpair );
871 extern Cell zsel24 ( Cell zpair );
872 extern Cell zsel34 ( Cell zpair );
873 extern Cell zsel44 ( Cell zpair );
875 extern Cell z5ble ( Cell x1, Cell x2, Cell x3, Cell x4, Cell x5 );
876 extern Cell zsel15 ( Cell zpair );
877 extern Cell zsel25 ( Cell zpair );
878 extern Cell zsel35 ( Cell zpair );
879 extern Cell zsel45 ( Cell zpair );
880 extern Cell zsel55 ( Cell zpair );
882 extern Cell unap ( int tag, Cell c );
883 #define isZPair(c) (whatIs((c))==ZTUP2)
885 /* --------------------------------------------------------------------------
886 * Implementation of function application nodes:
887 * ------------------------------------------------------------------------*/
889 #define ap(f,x) pair(f,x)
890 #define ap1(f,x) ap(f,x)
891 #define ap2(f,x,y) ap(ap(f,x),y)
892 #define ap3(f,x,y,z) ap(ap(ap(f,x),y),z)
893 #define fun(c) fst(c)
894 #define arg(c) snd(c)
895 #define isAp(c) (isPair(c) && !isTag(fst(c)))
896 extern Cell getHead Args((Cell));
897 extern List getArgs Args((Cell));
899 extern Cell nthArg Args((Int,Cell));
900 extern Int numArgs Args((Cell));
901 extern Cell applyToArgs Args((Cell,List));
903 /* --------------------------------------------------------------------------
904 * Stack implementation:
906 * NB: Use of macros makes order of evaluation hard to predict.
907 * For example, "push(1+pop());" doesn't increment TOS.
908 * ------------------------------------------------------------------------*/
910 extern Cell DECTABLE(cellStack);
913 #define clearStack() sp=(-1)
914 #define stackEmpty() (sp==(-1))
915 #define stack(p) cellStack[p]
916 #define chkStack(n) if (sp>=NUM_STACK-(n)) hugsStackOverflow()
922 #define onto(c) stack(++sp)=(c);
923 #define pop() stack(sp--)
925 #define top() stack(sp)
926 #define pushed(n) stack(sp-(n))
927 #define topfun(f) top()=ap((f),top())
928 #define toparg(x) top()=ap(top(),(x))
930 extern Void hugsStackOverflow Args((Void));
934 #define STACK_HEADROOM 16384
935 #define STACK_CHECK if (StackSpace() <= STACK_HEADROOM) \
936 internal("Macintosh function parameter stack overflow.");
941 /* --------------------------------------------------------------------------
942 * Script file control:
943 * The implementation of script file storage is hidden.
944 * ------------------------------------------------------------------------*/
946 extern Script startNewScript Args((String));
947 extern Bool moduleThisScript Args((Module));
948 extern Module moduleOfScript Args((Script));
949 extern Bool isPreludeScript Args((Void));
950 extern Module lastModule Args((Void));
951 extern Script scriptThisFile Args((Text));
952 extern Script scriptThisName Args((Name));
953 extern Script scriptThisTycon Args((Tycon));
954 extern Script scriptThisInst Args((Inst));
955 extern Script scriptThisClass Args((Class));
956 extern String fileOfModule Args((Module));
957 extern Void dropScriptsFrom Args((Script));
960 /* --------------------------------------------------------------------------
962 * ------------------------------------------------------------------------*/
965 /* This is an exact copy of the declaration found in GreenCard.h */
967 typedef int HugsStackPtr;
968 typedef int HugsStablePtr;
969 typedef Pointer HugsForeign;
973 /* evaluate next argument */
974 int (*getInt ) Args(());
975 unsigned int (*getWord ) Args(());
976 void* (*getAddr ) Args(());
977 float (*getFloat ) Args(());
978 double (*getDouble) Args(());
979 char (*getChar ) Args(());
980 HugsForeign (*getForeign) Args(());
981 HugsStablePtr (*getStablePtr) Args(());
983 /* push part of result */
984 void (*putInt ) Args((int));
985 void (*putWord ) Args((unsigned int));
986 void (*putAddr ) Args((void*));
987 void (*putFloat ) Args((double));
988 void (*putDouble) Args((double));
989 void (*putChar ) Args((char));
990 void (*putForeign) Args((HugsForeign, void (*)(HugsForeign)));
991 void (*putStablePtr) Args((HugsStablePtr));
993 /* return n values in IO monad or Id monad */
994 void (*returnIO) Args((HugsStackPtr, int));
995 void (*returnId) Args((HugsStackPtr, int));
996 int (*runIO) Args((int));
998 /* free a stable pointer */
999 void (*freeStablePtr) Args((HugsStablePtr));
1001 /* register the prim table */
1002 void (*registerPrims) Args((struct primInfo*));
1004 /* garbage collect */
1005 void (*garbageCollect) Args(());
1009 extern HugsAPI2* hugsAPI2 Args((Void));
1010 typedef Void (*InitModuleFun2) Args((HugsAPI2*));
1013 Name nameTrue, nameFalse;
1014 Name nameNil, nameCons;
1015 Name nameJust, nameNothing;
1016 Name nameLeft, nameRight;
1020 Cell (*makeInt) Args((Int));
1022 Cell (*makeChar) Args((Char));
1023 Char (*CharOf) Args((Cell));
1025 Cell (*makeFloat) Args((FloatPro));
1026 Cell (*makeTuple) Args((Int));
1027 Pair (*pair) Args((Cell,Cell));
1029 Cell (*mkMallocPtr) Args((Void *, Void (*)(Void *)));
1030 Void *(*derefMallocPtr) Args((Cell));
1032 Int (*mkStablePtr) Args((Cell));
1033 Cell (*derefStablePtr) Args((Int));
1034 Void (*freeStablePtr) Args((Int));
1036 Void (*eval) Args((Cell));
1037 Cell (*evalWithNoError) Args((Cell));
1038 Void (*evalFails) Args((StackPtr));
1044 Void (*garbageCollect) Args(());
1045 Void (*stackOverflow) Args(());
1046 Void (*internal) Args((String)) HUGS_noreturn;
1048 Void (*registerPrims) Args((struct primInfo*));
1049 Name (*addPrimCfun) Args((Text,Int,Int,Cell));
1050 Text (*inventText) Args(());
1052 Cell *(*Fst) Args((Cell));
1053 Cell *(*Snd) Args((Cell));
1059 extern HugsAPI1* hugsAPI1 Args((Void));
1060 typedef Void (*InitModuleFun1) Args((HugsAPI1*));
1061 #endif /* PLUGINS */
1064 /* --------------------------------------------------------------------------
1066 * ------------------------------------------------------------------------*/
1068 extern Void setLastExpr Args((Cell));
1069 extern Cell getLastExpr Args((Void));
1070 extern List addTyconsMatching Args((String,List));
1071 extern List addNamesMatching Args((String,List));
1073 /*-------------------------------------------------------------------------*/