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: 1999/12/16 16:34:45 $
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_INTP != SIZEOF_INT
304 #define PTRCELL 82 /* C Heap Pointer snd :: (Int,Int) */
308 #define STGVAR 92 /* STGVAR snd :: (StgRhs,info) */
309 #define STGAPP 93 /* STGAPP snd :: (StgVar,[Arg]) */
310 #define STGPRIM 94 /* STGPRIM snd :: (PrimOp,[Arg]) */
311 #define STGCON 95 /* STGCON snd :: (StgCon,[Arg]) */
312 #define PRIMCASE 96 /* PRIMCASE snd :: (Expr,[PrimAlt]) */
313 #define DEEFALT 97 /* DEEFALT snd :: (Var,Expr) */
314 #define CASEALT 98 /* CASEALT snd :: (Con,[Var],Expr) */
315 #define PRIMALT 99 /* PRIMALT snd :: ([Var],Expr) */
319 Top-level interface entities
320 type Line = Int -- a line number
321 type ConVarId = CONIDCELL | VARIDCELL
323 type ExportListEntry = ConVarId | (ConId, <ConVarId>)
324 type Associativity = mkInt of LEFT_ASS | RIGHT_ASS | NON_ASS
325 type Constr = ((ConId, [((Type,VarId,Int))]))
326 ((constr name, [((type, field name if any, strictness))]))
327 strictness: 0 => none, 1 => !, 2 => !! (unpacked)
328 All 2/3/4/5 tuples in the interface abstract syntax are done with
332 #define I_INTERFACE 109 /* snd :: ((ConId, [I_IMPORT..I_VALUE]))
333 interface name, list of iface entities */
335 #define I_IMPORT 110 /* snd :: ((ConId, [ConVarId]))
336 module name, list of entities */
338 #define I_INSTIMPORT 111 /* snd :: NIL -- not used at present */
340 #define I_EXPORT 112 /* snd :: ((ConId, [ExportListEntry]))
341 this module name?, entities to export */
343 #define I_FIXDECL 113 /* snd :: ((NIL|Int, Associativity, ConVarId))
344 fixity, associativity, name */
346 #define I_INSTANCE 114 /* snd :: ((Line, [((QConId,VarId))],
349 forall-y bit (eg __forall [a b] {M.C1 a, M.C2 b} =>),
350 other bit, eg { C a1 } -> { C2 a2 } -> ... -> { Cn an },
351 name of dictionary builder,
352 (after startGHCInstance) the instance table location */
354 #define I_TYPE 115 /* snd :: ((Line, ConId, [((VarId,Kind))], Type))
355 lineno, tycon, kinded tyvars, the type expr */
357 #define I_DATA 116 /* snd :: ((Line, [((QConId,VarId))], ConId,
358 [((VarId,Kind))], [Constr])
359 lineno, context, tycon, kinded tyvars, constrs
360 An empty constr list means exported abstractly. */
362 #define I_NEWTYPE 117 /* snd :: ((Line, [((QConId,VarId))], ConId,
363 [((VarId,Kind))], ((ConId,Type)) ))
364 lineno, context, tycon, kinded tyvars, constr
365 constr==NIL means exported abstractly. */
367 #define I_CLASS 118 /* snd :: ((Line, [((QConId,VarId))], ConId,
368 [((VarId,Kind))], [((VarId,Type))]))
369 lineno, context, classname,
370 kinded tyvars, method sigs */
372 #define I_VALUE 119 /* snd :: ((Line, VarId, Type)) */
378 #define ZCONS 190 /* snd :: (Cell,Cell) */
382 #define ZTUP2 192 /* snd :: (Cell,Cell) */
383 #define ZTUP3 193 /* snd :: (Cell,(Cell,Cell)) */
384 #define ZTUP4 194 /* snd :: (Cell,(Cell,(Cell,Cell))) */
385 #define ZTUP5 195 /* snd :: (Cell,(Cell,(Cell,(Cell,Cell)))) */
387 /* Last constructor tag must be less than SPECMIN */
389 /* --------------------------------------------------------------------------
390 * Special cell values:
391 * ------------------------------------------------------------------------*/
396 #define isSpec(c) (SPECMIN<=(c) && (c)<EXTMIN)/* Special cell values */
398 #define isSpec(c) (SPECMIN<=(c) && (c)<OFFMIN)
401 #define NONE 201 /* Dummy stub */
402 #define STAR 202 /* Representing the kind of types */
404 #define ROW 203 /* Representing the kind of rows */
406 #define WILDCARD 204 /* Wildcard pattern */
407 #define SKOLEM 205 /* Skolem constant */
409 #define DOTDOT 206 /* ".." in import/export list */
411 #define NAME 210 /* whatIs code for isName */
412 #define TYCON 211 /* whatIs code for isTycon */
413 #define CLASS 212 /* whatIs code for isClass */
414 #define MODULE 213 /* whatIs code for isModule */
415 #define INSTANCE 214 /* whatIs code for isInst */
416 #define TUPLE 215 /* whatIs code for tuple constructor */
417 #define OFFSET 216 /* whatis code for offset */
418 #define AP 217 /* whatIs code for application node */
419 #define CHARCELL 218 /* whatIs code for isChar */
421 #define EXT 219 /* whatIs code for isExt */
424 #define SIGDECL 220 /* Signature declaration */
425 #define FIXDECL 221 /* Fixity declaration */
426 #define FUNBIND 222 /* Function binding */
427 #define PATBIND 223 /* Pattern binding */
429 #define DATATYPE 230 /* Datatype type constructor */
430 #define NEWTYPE 231 /* Newtype type constructor */
431 #define SYNONYM 232 /* Synonym type constructor */
432 #define RESTRICTSYN 233 /* Synonym with restricted scope */
434 #define NODEPENDS 235 /* Stop calculation of deps in type check*/
435 #define PREDEFINED 236 /* Predefined name, not yet filled */
437 /* --------------------------------------------------------------------------
438 * Tuple data/type constructors:
439 * ------------------------------------------------------------------------*/
441 extern Text ghcTupleText Args((Tycon));
442 extern Text ghcTupleText_n Args((Int));
448 #define isExt(c) (EXTMIN<=(c) && (c)<OFFMIN)
449 #define extText(e) tabExt[(e)-EXTMIN]
450 #define extField(c) arg(fun(c))
451 #define extRow(c) arg(c)
453 extern Text DECTABLE(tabExt);
454 extern Ext mkExt Args((Text));
459 extern Module findFakeModule ( Text t );
460 extern Tycon addTupleTycon ( Int n );
461 extern Name addWiredInBoxingTycon
462 ( String modNm, String typeNm, String constrNm,
463 Int arity, Int no, Int rep );
464 Tycon addWiredInEnumTycon ( String modNm, String typeNm,
465 List /*of Text*/ constrs );
467 /* --------------------------------------------------------------------------
468 * Offsets: (generic types/stack offsets)
469 * ------------------------------------------------------------------------*/
472 #define OFFMIN (EXTMIN+NUM_EXT)
476 #define isOffset(c) (OFFMIN<=(c) && (c)<MODMIN)
477 #define offsetOf(c) ((c)-OFFMIN)
478 #define mkOffset(o) (OFFMIN+(o))
480 /* --------------------------------------------------------------------------
482 * ------------------------------------------------------------------------*/
484 /* An entry in a very crude object symbol table */
485 typedef struct { char* nm; void* ad; }
488 /* Indication of section kinds for loaded objects. Needed by
489 the GC for deciding whether or not a pointer on the stack
492 typedef enum { HUGS_DL_SECTION_CODE_OR_RODATA,
493 HUGS_DL_SECTION_RWDATA,
494 HUGS_DL_SECTION_OTHER }
497 typedef struct { void* start; void* end; DLSect sect; }
500 /* --------------------------------------------------------------------------
502 * ------------------------------------------------------------------------*/
504 #define MODMIN (OFFMIN+NUM_OFFSETS)
506 #define isModule(c) (MODMIN<=(c) && (c)<TYCMIN)
507 #define mkModule(n) (MODMIN+(n))
508 #define module(n) tabModule[(n)-MODMIN]
510 /* Under Haskell 1.3, the list of qualified imports is always a subset
511 * of the list of unqualified imports. For simplicity and flexibility,
512 * we do not attempt to exploit this fact - when a module is imported
513 * unqualified, it is added to both the qualified and unqualified
515 * Similarily, Haskell 1.3 does not allow a constructor to be imported
516 * or exported without exporting the type it belongs to but the export
517 * list is just a flat list of Texts (before static analysis) or
518 * Tycons, Names and Classes (after static analysis).
522 /* Lists of top level objects (local defns + imports) */
526 List exports; /* [ Entity | (Entity, NIL|DOTDOT) ] */
527 /* List of qualified imports. Used both during compilation and when
528 * evaluating an expression in the context of the current module.
532 /* TRUE if module exists only via GHC primop defn; usually FALSE */
535 /* ptr to malloc'd lump of memory holding the obj file */
538 /* ptr to object symbol table; lives in mallocville.
539 Dynamically expands. */
544 /* The section-kind entries for this object module. Dynamically expands. */
550 extern Module currentModule; /* Module currently being processed */
551 extern struct Module DECTABLE(tabModule);
553 extern Bool isValidModule Args((Module));
554 extern Module newModule Args((Text));
555 extern Module findModule Args((Text));
556 extern Module findModid Args((Cell));
557 extern Void setCurrModule Args((Module));
559 extern void addOTabName Args((Module,char*,void*));
560 extern void* lookupOTabName Args((Module,char*));
561 extern char* nameFromOPtr Args((void*));
563 extern void addDLSect Args((Module,void*,void*,DLSect));
564 extern DLSect lookupDLSect Args((void*));
567 #define isPrelude(m) (m==modulePrelude)
569 /* --------------------------------------------------------------------------
570 * Type constructor names:
571 * ------------------------------------------------------------------------*/
573 #define TYCMIN (MODMIN+NUM_MODULE)
574 #define isTycon(c) (TYCMIN<=(c) && (c)<NAMEMIN && tabTycon[(c)-TYCMIN].tuple==-1)
575 #define tycon(n) tabTycon[(n)-TYCMIN]
577 #define isTuple(c) (TYCMIN<=(c) && (c)<NAMEMIN && tabTycon[(c)-TYCMIN].tuple>=0)
578 #define tupleOf(n) (tabTycon[(n)-TYCMIN].tuple)
579 extern Tycon mkTuple ( Int );
585 Module mod; /* module that defines it */
586 Int tuple; /* tuple number, or -1 if not tuple */
588 Kind kind; /* kind (includes arity) of Tycon */
589 Cell what; /* DATATYPE/SYNONYM/RESTRICTSYN... */
591 Name conToTag; /* used in derived code */
593 void* itbl; /* For tuples, the info tbl pointer */
597 extern struct strTycon DECTABLE(tabTycon);
599 extern Tycon newTycon Args((Text));
600 extern Tycon findTycon Args((Text));
601 extern Tycon addTycon Args((Tycon));
602 extern Tycon findQualTycon Args((Cell));
603 extern Tycon addPrimTycon Args((Text,Kind,Int,Cell,Cell));
605 #define isSynonym(h) (isTycon(h) && tycon(h).what==SYNONYM)
606 #define isQualType(t) (isPair(t) && fst(t)==QUAL)
607 #define mkPolyType(n,t) pair(POLYTYPE,pair(n,t))
608 #define isPolyType(t) (isPair(t) && fst(t)==POLYTYPE)
609 #define isPolyOrQualType(t) (isPair(t) && (fst(t)==POLYTYPE || fst(t)==QUAL))
610 #define polySigOf(t) fst(snd(t))
611 #define monotypeOf(t) snd(snd(t))
613 #define bang(t) ap(BANG,t)
614 extern Tycon findQualTyconWithoutConsultingExportList ( QualId q );
616 /* --------------------------------------------------------------------------
617 * Globally defined name values:
618 * ------------------------------------------------------------------------*/
620 #define NAMEMIN (TYCMIN+NUM_TYCON)
621 #define isName(c) (NAMEMIN<=(c) && (c)<INSTMIN)
622 #define mkName(n) (NAMEMIN+(n))
623 #define name(n) tabName[(n)-NAMEMIN]
628 Module mod; /* module that defines it */
635 Cell stgVar; /* really StgVar */
636 Text callconv; /* for foreign import/export */
637 void* primop; /* really StgPrim* */
638 void* itbl; /* For constructors, the info tbl pointer */
642 extern int numNames Args(( Void ));
644 extern struct strName DECTABLE(tabName);
646 /* The number field in a name is used to distinguish various kinds of name:
647 * mfunNo(i) = code for member function, offset i
648 * members that are sole elements of dict use mfunNo(0)
649 * members of dicts with more than one elem use mfunNo(n), n>=1
650 * EXECNAME = code for executable name (bytecodes or primitive)
651 * SELNAME = code for selector function
652 * DFUNNAME = code for dictionary builder or selector
653 * cfunNo(i) = code for data constructor
654 * datatypes with only one constructor uses cfunNo(0)
655 * datatypes with multiple constructors use cfunNo(n), n>=1
663 #define isSfun(n) (name(n).number==SELNAME)
664 #define isDfun(n) (name(n).number==DFUNNAME)
666 #define isCfun(n) (name(n).number>=CFUNNAME)
667 #define cfunOf(n) (name(n).number-CFUNNAME)
668 #define cfunNo(i) ((i)+CFUNNAME)
669 #define hasCfun(cs) (nonNull(cs) && isCfun(hd(cs)))
671 #define isMfun(n) (name(n).number<0)
672 #define mfunOf(n) ((-1)-name(n).number)
673 #define mfunNo(i) ((-1)-(i))
675 extern Name newName Args((Text,Cell));
676 extern Name findName Args((Text));
677 extern Name addName Args((Name));
678 extern Name findQualName Args((Cell));
679 extern Name addPrimCfun Args((Text,Int,Int,Cell));
680 extern Name addPrimCfunREP Args((Text,Int,Int,Int));
681 extern Int sfunPos Args((Name,Name));
682 extern Name nameFromStgVar Args((Cell));
683 extern Name jrsFindQualName Args((Text,Text));
685 extern Name findQualNameWithoutConsultingExportList ( QualId q );
687 /* --------------------------------------------------------------------------
689 * ------------------------------------------------------------------------*/
691 #define INSTMIN (NAMEMIN+NUM_NAME) /* instances */
692 #define isInst(c) (INSTMIN<=(c) && (c)<CLASSMIN)
693 #define mkInst(n) (INSTMIN+(n))
694 #define instOf(c) ((Int)((c)-INSTMIN))
695 #define inst(in) tabInst[(in)-INSTMIN]
698 Class c; /* class C */
700 Module mod; /* module that defines it */
701 Kinds kinds; /* Kinds of variables in head */
702 Cell head; /* :: Pred */
703 List specifics; /* :: [Pred] */
704 Int numSpecifics; /* length(specifics) */
706 Name builder; /* Dictionary constructor function */
709 /* a predicate (an element :: Pred) is an application of a Class to one or
710 * more type expressions
713 #define CLASSMIN (INSTMIN+NUM_INSTS)
714 #define isClass(c) (CLASSMIN<=(c) && (c)<CHARMIN)
715 #define mkClass(n) (CLASSMIN+(n))
716 #define cclass(n) tabClass[(n)-CLASSMIN]
719 Text text; /* Name of class */
720 Int line; /* Line where declaration begins */
721 Module mod; /* module that declares it */
722 Int level; /* Level in class hierarchy */
723 Int arity; /* Number of arguments */
724 Kinds kinds; /* Kinds of constructors in class */
725 List fds; /* Functional Dependencies */
726 List xfds; /* Xpanded Functional Dependencies */
727 Cell head; /* Head of class */
728 Name dcon; /* Dictionary constructor function */
729 List supers; /* :: [Pred] */
730 Int numSupers; /* length(supers) */
731 List dsels; /* Superclass dictionary selectors */
732 List members; /* :: [Name] */
733 Int numMembers; /* length(members) */
734 List defaults; /* :: [Name] */
735 List instances; /* :: [Inst] */
738 extern struct strClass DECTABLE(tabClass);
739 extern struct strInst far *tabInst;
741 extern Class newClass Args((Text));
742 extern Class classMax Args((Void));
743 extern Class findClass Args((Text));
744 extern Class addClass Args((Class));
745 extern Class findQualClass Args((Cell));
746 extern Inst newInst Args((Void));
747 extern Inst findFirstInst Args((Tycon));
748 extern Inst findNextInst Args((Tycon,Inst));
749 extern List getAllKnownTyconsAndClasses ( void );
750 extern Class findQualClassWithoutConsultingExportList ( QualId q );
752 /* --------------------------------------------------------------------------
754 * ------------------------------------------------------------------------*/
756 #define CHARMIN (CLASSMIN+NUM_CLASSES)
757 #define MAXCHARVAL (NUM_CHARS-1)
758 #define isChar(c) (CHARMIN<=(c) && (c)<INTMIN)
759 #define charOf(c) ((Char)(c-CHARMIN))
760 #define mkChar(c) ((Cell)(CHARMIN+(((unsigned)(c))%NUM_CHARS)))
762 /* --------------------------------------------------------------------------
763 * Small Integer values:
764 * ------------------------------------------------------------------------*/
766 #define INTMIN (CHARMIN+NUM_CHARS)
767 #define INTMAX (MAXPOSINT)
768 #define isSmall(c) (INTMIN<=(c))
769 #define INTZERO (INTMIN/2 + INTMAX/2)
770 #define MINSMALLINT (INTMIN - INTZERO)
771 #define MAXSMALLINT (INTMAX - INTZERO)
772 #define mkDigit(c) ((Cell)((c)+INTMIN))
773 #define digitOf(c) ((Int)((c)-INTMIN))
775 extern Bool isInt Args((Cell));
776 extern Int intOf Args((Cell));
777 extern Cell mkInt Args((Int));
779 /* --------------------------------------------------------------------------
780 * Implementation of triples:
781 * ------------------------------------------------------------------------*/
783 #define triple(x,y,z) pair(x,pair(y,z))
784 #define fst3(c) fst(c)
785 #define snd3(c) fst(snd(c))
786 #define thd3(c) snd(snd(c))
788 /* --------------------------------------------------------------------------
789 * Implementation of lists:
790 * ------------------------------------------------------------------------*/
793 #define isNull(c) ((c)==NIL)
794 #define nonNull(c) (c)
795 #define cons(x,xs) pair(x,xs)
796 #define singleton(x) cons(x,NIL)
797 #define doubleton(x,y) cons(x,cons(y,NIL))
798 #define tripleton(x,y,z) cons(x,cons(y,cons(z,NIL)))
802 extern Int length Args((List));
803 extern List appendOnto Args((List,List)); /* destructive */
804 extern List dupOnto Args((List,List));
805 extern List dupList Args((List));
806 extern List revOnto Args((List, List)); /* destructive */
807 #define rev(xs) revOnto((xs),NIL) /* destructive */
808 #define reverse(xs) revOnto(dupList(xs),NIL) /* non-destructive */
809 extern Cell cellIsMember Args((Cell,List));
810 extern Cell cellAssoc Args((Cell,List));
811 extern Cell cellRevAssoc Args((Cell,List));
812 extern Bool eqList Args((List,List));
813 extern Cell varIsMember Args((Text,List));
814 extern Name nameIsMember Args((Text,List));
815 extern QualId qualidIsMember ( QualId, List );
816 extern Cell intIsMember Args((Int,List));
817 extern List replicate Args((Int,Cell));
818 extern List diffList Args((List,List)); /* destructive */
819 extern List deleteCell Args((List,Cell)); /* non-destructive */
820 extern List take Args((Int,List)); /* destructive */
821 extern List splitAt Args((Int,List)); /* non-destructive */
822 extern Cell nth Args((Int,List));
823 extern List removeCell Args((Cell,List)); /* destructive */
824 extern List dupListOnto Args((List,List)); /* non-destructive */
825 extern List nubList Args((List)); /* non-destructive */
827 /* The following macros provide `inline expansion' of some common ways of
828 * traversing, using and modifying lists:
830 * N.B. We use the names _f, _a, _xs, Zs, in an attempt to avoid clashes
831 * with identifiers used elsewhere.
834 #define mapBasic(_init,_step) {List Zs=(_init);\
835 for(;nonNull(Zs);Zs=tl(Zs)) \
837 #define mapModify(_init,_step) mapBasic(_init,hd(Zs)=_step)
839 #define mapProc(_f,_xs) mapBasic(_xs,_f(hd(Zs)))
840 #define map1Proc(_f,_a,_xs) mapBasic(_xs,_f(_a,hd(Zs)))
841 #define map2Proc(_f,_a,_b,_xs) mapBasic(_xs,_f(_a,_b,hd(Zs)))
842 #define map3Proc(_f,_a,_b,_c,_xs) mapBasic(_xs,_f(_a,_b,_c,hd(Zs)))
843 #define map4Proc(_f,_a,_b,_c,_d,_xs) mapBasic(_xs,_f(_a,_b,_c,_d,hd(Zs)))
845 #define mapOver(_f,_xs) mapModify(_xs,_f(hd(Zs)))
846 #define map1Over(_f,_a,_xs) mapModify(_xs,_f(_a,hd(Zs)))
847 #define map2Over(_f,_a,_b,_xs) mapModify(_xs,_f(_a,_b,hd(Zs)))
848 #define map3Over(_f,_a,_b,_c,_xs) mapModify(_xs,_f(_a,_b,_c,hd(Zs)))
849 #define map4Over(_f,_a,_b,_c,_d,_xs) mapModify(_xs,_f(_a,_b,_c,_d,hd(Zs)))
851 /* This is just what you want for functions with accumulating parameters */
852 #define mapAccum(_f,_acc,_xs) mapBasic(_xs,_acc=_f(_acc,hd(Zs)))
853 #define map1Accum(_f,_acc,_a,_xs) mapBasic(_xs,_acc=_f(_acc,_a,hd(Zs)))
854 #define map2Accum(_f,_acc,_a,_b,_xs) mapBasic(_xs,_acc=_f(_acc,_a,_b,hd(Zs)))
855 #define map3Accum(_f,_acc,_a,_b,_c,_xs) mapBasic(_xs,_acc=_f(_acc,_a,_b,_c,hd(Zs)))
858 /* --------------------------------------------------------------------------
859 * Strongly-typed lists (z-lists) and tuples (experimental)
860 * ------------------------------------------------------------------------*/
863 typedef Cell ZTriple;
869 extern Cell zcons ( Cell x, Cell xs );
870 extern Cell zhd ( Cell xs );
871 extern Cell ztl ( Cell xs );
872 extern Cell zsingleton ( Cell x );
873 extern Cell zdoubleton ( Cell x, Cell y );
874 extern Int zlength ( ZList xs );
875 extern ZList zreverse ( ZList xs );
878 extern Cell zpair ( Cell x1, Cell x2 );
879 extern Cell zfst ( Cell zpair );
880 extern Cell zsnd ( Cell zpair );
882 extern Cell ztriple ( Cell x1, Cell x2, Cell x3 );
883 extern Cell zfst3 ( Cell zpair );
884 extern Cell zsnd3 ( Cell zpair );
885 extern Cell zthd3 ( Cell zpair );
887 extern Cell z4ble ( Cell x1, Cell x2, Cell x3, Cell x4 );
888 extern Cell zsel14 ( Cell zpair );
889 extern Cell zsel24 ( Cell zpair );
890 extern Cell zsel34 ( Cell zpair );
891 extern Cell zsel44 ( Cell zpair );
893 extern Cell z5ble ( Cell x1, Cell x2, Cell x3, Cell x4, Cell x5 );
894 extern Cell zsel15 ( Cell zpair );
895 extern Cell zsel25 ( Cell zpair );
896 extern Cell zsel35 ( Cell zpair );
897 extern Cell zsel45 ( Cell zpair );
898 extern Cell zsel55 ( Cell zpair );
900 extern Cell unap ( int tag, Cell c );
901 #define isZPair(c) (whatIs((c))==ZTUP2)
903 /* --------------------------------------------------------------------------
904 * Implementation of function application nodes:
905 * ------------------------------------------------------------------------*/
907 #define ap(f,x) pair(f,x)
908 #define ap1(f,x) ap(f,x)
909 #define ap2(f,x,y) ap(ap(f,x),y)
910 #define ap3(f,x,y,z) ap(ap(ap(f,x),y),z)
911 #define fun(c) fst(c)
912 #define arg(c) snd(c)
913 #define isAp(c) (isPair(c) && !isTag(fst(c)))
914 extern Cell getHead Args((Cell));
915 extern List getArgs Args((Cell));
917 extern Cell nthArg Args((Int,Cell));
918 extern Int numArgs Args((Cell));
919 extern Cell applyToArgs Args((Cell,List));
921 /* --------------------------------------------------------------------------
922 * Stack implementation:
924 * NB: Use of macros makes order of evaluation hard to predict.
925 * For example, "push(1+pop());" doesn't increment TOS.
926 * ------------------------------------------------------------------------*/
928 extern Cell DECTABLE(cellStack);
931 #define clearStack() sp=(-1)
932 #define stackEmpty() (sp==(-1))
933 #define stack(p) cellStack[p]
934 #define chkStack(n) if (sp>=NUM_STACK-(n)) hugsStackOverflow()
940 #define onto(c) stack(++sp)=(c);
941 #define pop() stack(sp--)
943 #define top() stack(sp)
944 #define pushed(n) stack(sp-(n))
945 #define topfun(f) top()=ap((f),top())
946 #define toparg(x) top()=ap(top(),(x))
948 extern Void hugsStackOverflow Args((Void));
952 #define STACK_HEADROOM 16384
953 #define STACK_CHECK if (StackSpace() <= STACK_HEADROOM) \
954 internal("Macintosh function parameter stack overflow.");
959 /* --------------------------------------------------------------------------
960 * Script file control:
961 * The implementation of script file storage is hidden.
962 * ------------------------------------------------------------------------*/
964 extern Script startNewScript Args((String));
965 extern Bool moduleThisScript Args((Module));
966 extern Module moduleOfScript Args((Script));
967 extern Bool isPreludeScript Args((Void));
968 extern Module lastModule Args((Void));
969 extern Script scriptThisFile Args((Text));
970 extern Script scriptThisName Args((Name));
971 extern Script scriptThisTycon Args((Tycon));
972 extern Script scriptThisInst Args((Inst));
973 extern Script scriptThisClass Args((Class));
974 extern String fileOfModule Args((Module));
975 extern Void dropScriptsFrom Args((Script));
978 /* --------------------------------------------------------------------------
980 * ------------------------------------------------------------------------*/
983 /* This is an exact copy of the declaration found in GreenCard.h */
985 typedef int HugsStackPtr;
986 typedef int HugsStablePtr;
987 typedef Pointer HugsForeign;
991 /* evaluate next argument */
992 int (*getInt ) Args(());
993 unsigned int (*getWord ) Args(());
994 void* (*getAddr ) Args(());
995 float (*getFloat ) Args(());
996 double (*getDouble) Args(());
997 char (*getChar ) Args(());
998 HugsForeign (*getForeign) Args(());
999 HugsStablePtr (*getStablePtr) Args(());
1001 /* push part of result */
1002 void (*putInt ) Args((int));
1003 void (*putWord ) Args((unsigned int));
1004 void (*putAddr ) Args((void*));
1005 void (*putFloat ) Args((double));
1006 void (*putDouble) Args((double));
1007 void (*putChar ) Args((char));
1008 void (*putForeign) Args((HugsForeign, void (*)(HugsForeign)));
1009 void (*putStablePtr) Args((HugsStablePtr));
1011 /* return n values in IO monad or Id monad */
1012 void (*returnIO) Args((HugsStackPtr, int));
1013 void (*returnId) Args((HugsStackPtr, int));
1014 int (*runIO) Args((int));
1016 /* free a stable pointer */
1017 void (*freeStablePtr) Args((HugsStablePtr));
1019 /* register the prim table */
1020 void (*registerPrims) Args((struct primInfo*));
1022 /* garbage collect */
1023 void (*garbageCollect) Args(());
1027 extern HugsAPI2* hugsAPI2 Args((Void));
1028 typedef Void (*InitModuleFun2) Args((HugsAPI2*));
1031 Name nameTrue, nameFalse;
1032 Name nameNil, nameCons;
1033 Name nameJust, nameNothing;
1034 Name nameLeft, nameRight;
1038 Cell (*makeInt) Args((Int));
1040 Cell (*makeChar) Args((Char));
1041 Char (*CharOf) Args((Cell));
1043 Cell (*makeFloat) Args((FloatPro));
1044 Cell (*makeTuple) Args((Int));
1045 Pair (*pair) Args((Cell,Cell));
1047 Cell (*mkMallocPtr) Args((Void *, Void (*)(Void *)));
1048 Void *(*derefMallocPtr) Args((Cell));
1050 Int (*mkStablePtr) Args((Cell));
1051 Cell (*derefStablePtr) Args((Int));
1052 Void (*freeStablePtr) Args((Int));
1054 Void (*eval) Args((Cell));
1055 Cell (*evalWithNoError) Args((Cell));
1056 Void (*evalFails) Args((StackPtr));
1062 Void (*garbageCollect) Args(());
1063 Void (*stackOverflow) Args(());
1064 Void (*internal) Args((String)) HUGS_noreturn;
1066 Void (*registerPrims) Args((struct primInfo*));
1067 Name (*addPrimCfun) Args((Text,Int,Int,Cell));
1068 Text (*inventText) Args(());
1070 Cell *(*Fst) Args((Cell));
1071 Cell *(*Snd) Args((Cell));
1077 extern HugsAPI1* hugsAPI1 Args((Void));
1078 typedef Void (*InitModuleFun1) Args((HugsAPI1*));
1079 #endif /* PLUGINS */
1082 /* --------------------------------------------------------------------------
1084 * ------------------------------------------------------------------------*/
1086 extern Void setLastExpr Args((Cell));
1087 extern Cell getLastExpr Args((Void));
1088 extern List addTyconsMatching Args((String,List));
1089 extern List addNamesMatching Args((String,List));
1091 /*-------------------------------------------------------------------------*/