2 /* --------------------------------------------------------------------------
3 * Defines storage datatypes: Text, Name, Module, Tycon, Cell, List, Pair,
6 * Hugs 98 is Copyright (c) Mark P Jones, Alastair Reid and the Yale
7 * Haskell Group 1994-99, and is distributed as Open Source software
8 * under the Artistic License; see the file "Artistic" that is included
9 * in the distribution for details.
11 * $RCSfile: storage.h,v $
13 * $Date: 1999/07/06 15:24:45 $
14 * ------------------------------------------------------------------------*/
16 /* --------------------------------------------------------------------------
17 * Typedefs for main data types:
18 * Many of these type names are used to indicate the intended us of a data
19 * item, rather than for type checking purposes. Sadly (although sometimes,
20 * fortunately), the C compiler cannot distinguish between the use of two
21 * different names defined to be synonyms for the same types.
22 * ------------------------------------------------------------------------*/
24 typedef Int Text; /* text string */
25 typedef Unsigned Syntax; /* syntax (assoc,preced) */
26 typedef Int Cell; /* general cell value */
27 typedef Cell far *Heap; /* storage of heap */
28 typedef Cell Pair; /* pair cell */
29 typedef Int StackPtr; /* stack pointer */
30 typedef Cell Offset; /* offset/generic variable*/
31 typedef Int Script; /* script file number */
32 typedef Int Module; /* module */
33 typedef Cell Tycon; /* type constructor */
34 typedef Cell Type; /* type expression */
35 typedef Cell Kind; /* kind expression */
36 typedef Cell Kinds; /* list of kinds */
37 typedef Cell Constr; /* constructor expression */
38 typedef Cell Name; /* named value */
39 typedef Cell Class; /* type class */
40 typedef Cell Inst; /* instance of type class */
41 typedef Cell Triple; /* triple of cell values */
42 typedef Cell List; /* list of cells */
43 typedef Cell Bignum; /* bignum integer */
44 typedef Cell Float; /* floating pt literal */
46 typedef Cell Ext; /* extension label */
49 /* --------------------------------------------------------------------------
51 * provides storage for the characters making up identifier and symbol
52 * names, string literals, character constants etc...
53 * ------------------------------------------------------------------------*/
55 extern String textToStr Args((Text));
56 extern Text findText Args((String));
57 extern Text inventText Args((Void));
58 extern Text inventDictText Args((Void));
59 extern Bool inventedText Args((Text));
61 /* Variants of textToStr and syntaxOf which work for idents, ops whether
62 * qualified or unqualified.
64 extern String identToStr Args((Cell));
65 extern Syntax identSyntax Args((Cell));
66 extern Syntax defaultSyntax Args((Text));
68 /* --------------------------------------------------------------------------
69 * Specification of syntax (i.e. default written form of application)
70 * ------------------------------------------------------------------------*/
72 #define MIN_PREC 0 /* weakest binding operator */
73 #define MAX_PREC 9 /* strongest binding operator */
74 #define FUN_PREC (MAX_PREC+2) /* binding of function symbols */
75 #define DEF_PREC MAX_PREC
76 #define APPLIC 0 /* written applicatively */
77 #define LEFT_ASS 1 /* left associative infix */
78 #define RIGHT_ASS 2 /* right associative infix */
79 #define NON_ASS 3 /* non associative infix */
80 #define DEF_ASS LEFT_ASS
82 #define UMINUS_PREC 6 /* Change these settings at your */
83 #define UMINUS_ASSOC LEFT_ASS /* own risk; they may not work! */
85 #define assocOf(x) ((x)&NON_ASS)
86 #define precOf(x) ((x)>>2)
87 #define mkSyntax(a,p) ((a)|((p)<<2))
88 #define DEF_OPSYNTAX mkSyntax(DEF_ASS,DEF_PREC)
89 #define NO_SYNTAX (-1)
91 extern Void addSyntax Args((Int,Text,Syntax));
92 extern Syntax syntaxOf Args((Text));
94 /* --------------------------------------------------------------------------
96 * Provides a garbage collectable heap for storage of expressions etc.
97 * ------------------------------------------------------------------------*/
99 #define heapAlloc(s) (Heap)(farCalloc(s,sizeof(Cell)))
100 #define heapBuilt() (heapFst)
102 extern Heap heapFst, heapSnd;
103 extern Heap heapTopFst;
104 extern Heap heapTopSnd;
105 extern Bool consGC; /* Set to FALSE to turn off gc from*/
106 /* C stack; use with extreme care! */
107 extern Int cellsRecovered; /* cells recovered by last gc */
109 #define fst(c) heapTopFst[c]
110 #define snd(c) heapTopSnd[c]
112 extern Pair pair Args((Cell,Cell));
113 extern Void garbageCollect Args((Void));
115 extern Void overwrite Args((Pair,Pair));
116 extern Void overwrite2 Args((Pair,Cell,Cell));
117 extern Cell markExpr Args((Cell));
118 extern Void markWithoutMove Args((Cell));
120 #define mark(v) v=markExpr(v)
122 #define isPair(c) ((c)<0)
123 #define isGenPair(c) ((c)<0 && -heapSize<=(c))
125 extern Cell whatIs Args((Cell));
127 /* --------------------------------------------------------------------------
128 * Box cell tags are used as the fst element of a pair to indicate that
129 * the snd element of the pair is to be treated in some special way, other
130 * than as a Cell. Examples include holding integer values, variable name
131 * and string text etc.
132 * ------------------------------------------------------------------------*/
134 #define TAGMIN 1 /* Box and constructor cell tag values */
135 #define BCSTAG 20 /* Box=TAGMIN..BCSTAG-1 */
136 #define isTag(c) (TAGMIN<=(c) && (c)<SPECMIN) /* Tag cell values */
137 #define isBoxTag(c) (TAGMIN<=(c) && (c)<BCSTAG) /* Box cell tag values */
138 #define isConTag(c) (BCSTAG<=(c) && (c)<SPECMIN) /* Constr cell tag values*/
140 #define FREECELL 3 /* Free list cell: snd :: Cell */
141 #define VARIDCELL 4 /* Identifier variable: snd :: Text */
142 #define VAROPCELL 5 /* Operator variable: snd :: Text */
143 #define DICTVAR 6 /* Dictionary variable: snd :: Text */
144 #define CONIDCELL 7 /* Identifier constructor: snd :: Text */
145 #define CONOPCELL 8 /* Operator constructor: snd :: Text */
146 #define STRCELL 9 /* String literal: snd :: Text */
147 #define INTCELL 10 /* Int literal: snd :: Int */
148 #define ADDPAT 11 /* (_+k) pattern discr: snd :: Int */
149 #define FLOATCELL 15 /* Floating Pt literal: snd :: Text */
150 #define BIGCELL 16 /* Integer literal: snd :: Text */
152 #define PTRCELL 17 /* C Heap Pointer snd :: Ptr */
153 #define CPTRCELL 18 /* Native code pointer snd :: Ptr */
156 #define EXTCOPY 19 /* Copy of an Ext: snd :: Text */
159 //#define textOf(c) ((Text)(snd(c))) /* c :: (VAR|CON)(ID|OP) */
162 static Text textOf( Cell c )
165 (whatIs(c)==VARIDCELL
166 || whatIs(c)==CONIDCELL
167 || whatIs(c)==VAROPCELL
168 || whatIs(c)==CONOPCELL
169 || whatIs(c)==STRCELL
170 || whatIs(c)==DICTVAR
173 fprintf(stderr, "\ntextOf -- tag %d\n",whatIs(c) );
180 #define qmodOf(c) (textOf(fst(snd(c)))) /* c :: QUALIDENT */
181 #define qtextOf(c) (textOf(snd(snd(c)))) /* c :: QUALIDENT */
182 #define mkVar(t) ap(VARIDCELL,t)
183 #define mkVarop(t) ap(VAROPCELL,t)
184 #define mkCon(t) ap(CONIDCELL,t)
185 #define mkConop(t) ap(CONOPCELL,t)
186 #define mkQVar(m,t) ap(QUALIDENT,pair(mkCon(m),mkVar(t)))
187 #define mkQCon(m,t) ap(QUALIDENT,pair(mkCon(m),mkCon(t)))
188 #define mkQVarOp(m,t) ap(QUALIDENT,pair(mkCon(m),mkVarop(t)))
189 #define mkQConOp(m,t) ap(QUALIDENT,pair(mkCon(m),mkConop(t)))
190 #define intValOf(c) (snd(c))
191 #define inventVar() mkVar(inventText())
192 #define mkDictVar(t) ap(DICTVAR,t)
193 #define inventDictVar() mkDictVar(inventDictText())
194 #define mkStr(t) ap(STRCELL,t)
195 extern Bool isVar Args((Cell));
196 extern Bool isCon Args((Cell));
197 extern Bool isQVar Args((Cell));
198 extern Bool isQCon Args((Cell));
199 extern Bool isQualIdent Args((Cell));
200 extern Bool isIdent Args((Cell));
202 extern String stringNegate Args((String));
204 #define isFloat(c) (isPair(c) && fst(c)==FLOATCELL)
205 #define stringToFloat(s) pair(FLOATCELL,findText(s))
206 #define floatToString(f) textToStr(snd(f))
207 #define floatOf(f) atof(floatToString(f))
208 #define mkFloat(f) (f) /* ToDo: is this right? */
209 #define floatNegate(f) stringToFloat(stringNegate(floatToString(f)))
211 #define stringToBignum(s) pair(BIGCELL,findText(s))
212 #define bignumToString(b) textToStr(snd(b))
216 #define isPtr(c) (isPair(c) && fst(c)==PTRCELL)
217 extern Cell mkPtr Args((Ptr));
218 extern Ptr ptrOf Args((Cell));
219 #define isCPtr(c) (isPair(c) && fst(c)==CPTRCELL)
220 extern Cell mkCPtr Args((Ptr));
221 extern Ptr cptrOf Args((Cell));
224 /* --------------------------------------------------------------------------
225 * Constructor cell tags are used as the fst element of a pair to indicate
226 * a particular syntactic construct described by the snd element of the
228 * Note that a cell c will not be treated as an application (AP/isAp) node
229 * if its first element is a constructor cell tag, whereas a cell whose fst
230 * element is a special cell will be treated as an application node.
231 * ------------------------------------------------------------------------*/
233 #define LETREC 20 /* LETREC snd :: ([Decl],Exp) */
234 #define COND 21 /* COND snd :: (Exp,Exp,Exp) */
235 #define LAMBDA 22 /* LAMBDA snd :: Alt */
236 #define FINLIST 23 /* FINLIST snd :: [Exp] */
237 #define DOCOMP 24 /* DOCOMP snd :: (Exp,[Qual]) */
238 #define BANG 25 /* BANG snd :: Type */
239 #define COMP 26 /* COMP snd :: (Exp,[Qual]) */
240 #define ASPAT 27 /* ASPAT snd :: (Var,Exp) */
241 #define ESIGN 28 /* ESIGN snd :: (Exp,Type) */
242 #define RSIGN 29 /* RSIGN snd :: (Rhs,Type) */
243 #define CASE 30 /* CASE snd :: (Exp,[Alt]) */
244 #define NUMCASE 31 /* NUMCASE snd :: (Exp,Disc,Rhs) */
245 #define FATBAR 32 /* FATBAR snd :: (Exp,Exp) */
246 #define LAZYPAT 33 /* LAZYPAT snd :: Exp */
247 #define DERIVE 35 /* DERIVE snd :: Cell */
249 #define FLOATCELL 36 /* FLOATCELL snd :: (Int,Int) */
252 #define BOOLQUAL 39 /* BOOLQUAL snd :: Exp */
253 #define QWHERE 40 /* QWHERE snd :: [Decl] */
254 #define FROMQUAL 41 /* FROMQUAL snd :: (Exp,Exp) */
255 #define DOQUAL 42 /* DOQUAL snd :: Exp */
256 #define MONADCOMP 43 /* MONADCOMP snd :: ((m,m0),(Exp,[Qual])*/
258 #define GUARDED 44 /* GUARDED snd :: [guarded exprs] */
260 #define ARRAY 45 /* Array snd :: (Bounds,[Values]) */
261 #define MUTVAR 46 /* Mutvar snd :: Cell */
263 #define HUGSOBJECT 47 /* HUGSOBJECT snd :: Cell */
266 #define POLYTYPE 50 /* POLYTYPE snd :: (Kind,Type) */
267 #define QUAL 51 /* QUAL snd :: ([Classes],Type) */
268 #define RANK2 52 /* RANK2 snd :: (Int,Type) */
269 #define EXIST 53 /* EXIST snd :: (Int,Type) */
270 #define POLYREC 54 /* POLYREC snd :: (Int,Type) */
271 #define BIGLAM 55 /* BIGLAM snd :: (vars,patterns) */
272 #define CDICTS 56 /* CDICTS snd :: ([Pred],Type) */
274 #define LABC 60 /* LABC snd :: (con,[(Vars,Type)]) */
275 #define CONFLDS 61 /* CONFLDS snd :: (con,[Field]) */
276 #define UPDFLDS 62 /* UPDFLDS snd :: (Exp,[con],[Field]) */
278 #define RECORD 63 /* RECORD snd :: [Val] */
279 #define EXTCASE 64 /* EXTCASE snd :: (Exp,Disc,Rhs) */
280 #define RECSEL 65 /* RECSEL snd :: Ext */
282 #define IMPDEPS 68 /* IMPDEPS snd :: [Binding] */
284 #define QUALIDENT 70 /* Qualified identifier snd :: (Id,Id) */
285 #define HIDDEN 71 /* hiding import list snd :: [Entity] */
286 #define MODULEENT 72 /* module in export list snd :: con */
288 #define INFIX 80 /* INFIX snd :: (see tidyInfix) */
289 #define ONLY 81 /* ONLY snd :: Exp */
290 #define NEG 82 /* NEG snd :: Exp */
292 /* Used when parsing GHC interface files */
293 #define DICTAP 85 /* DICTTYPE snd :: (QClassId,[Type]) */
295 #if SIZEOF_INTP != SIZEOF_INT
296 #define PTRCELL 90 /* C Heap Pointer snd :: (Int,Int) */
299 #define STGVAR 92 /* STGVAR snd :: (StgRhs,info) */
300 #define STGAPP 93 /* STGAPP snd :: (StgVar,[Arg]) */
301 #define STGPRIM 94 /* STGPRIM snd :: (PrimOp,[Arg]) */
302 #define STGCON 95 /* STGCON snd :: (StgCon,[Arg]) */
303 #define PRIMCASE 96 /* PRIMCASE snd :: (Expr,[PrimAlt]) */
304 #define DEEFALT 97 /* DEEFALT snd :: (Var,Expr) */
305 #define CASEALT 98 /* CASEALT snd :: (Con,[Var],Expr) */
306 #define PRIMALT 99 /* PRIMALT snd :: ([Var],Expr) */
307 /* Last constructor tag must be less than SPECMIN */
309 /* --------------------------------------------------------------------------
310 * Special cell values:
311 * ------------------------------------------------------------------------*/
314 #define isSpec(c) (SPECMIN<=(c) && (c)<TUPMIN)/* Special cell values */
316 #define NONE 101 /* Dummy stub */
317 #define STAR 102 /* Representing the kind of types */
319 #define ROW 103 /* Representing the kind of rows */
321 #define WILDCARD 104 /* Wildcard pattern */
322 #define SKOLEM 105 /* Skolem constant */
324 #define DOTDOT 106 /* ".." in import/export list */
326 #define NAME 110 /* whatIs code for isName */
327 #define TYCON 111 /* whatIs code for isTycon */
328 #define CLASS 112 /* whatIs code for isClass */
329 #define MODULE 113 /* whatIs code for isModule */
330 #define INSTANCE 114 /* whatIs code for isInst */
331 #define TUPLE 115 /* whatIs code for tuple constructor */
332 #define OFFSET 116 /* whatis code for offset */
333 #define AP 117 /* whatIs code for application node */
334 #define CHARCELL 118 /* whatIs code for isChar */
336 #define EXT 119 /* whatIs code for isExt */
339 #define SIGDECL 120 /* Signature declaration */
340 #define FIXDECL 121 /* Fixity declaration */
341 #define FUNBIND 122 /* Function binding */
342 #define PATBIND 123 /* Pattern binding */
344 #define DATATYPE 130 /* Datatype type constructor */
345 #define NEWTYPE 131 /* Newtype type constructor */
346 #define SYNONYM 132 /* Synonym type constructor */
347 #define RESTRICTSYN 133 /* Synonym with restricted scope */
349 #define NODEPENDS 135 /* Stop calculation of deps in type check*/
350 #define PREDEFINED 136 /* Predefined name, not yet filled */
352 /* --------------------------------------------------------------------------
353 * Tuple data/type constructors:
354 * ------------------------------------------------------------------------*/
358 #define isTuple(c) (TUPMIN<=(c) && (c)<EXTMIN)
360 #define isTuple(c) (TUPMIN<=(c) && (c)<OFFMIN)
362 #define mkTuple(n) (TUPMIN+(n))
363 #define tupleOf(n) ((Int)((n)-TUPMIN))
366 #define EXTMIN (TUPMIN+NUM_TUPLES)
367 #define isExt(c) (EXTMIN<=(c) && (c)<OFFMIN)
368 #define extText(e) tabExt[(e)-EXTMIN]
369 #define extField(c) arg(fun(c))
370 #define extRow(c) arg(c)
372 extern Text DECTABLE(tabExt);
373 extern Ext mkExt Args((Text));
378 /* --------------------------------------------------------------------------
379 * Offsets: (generic types/stack offsets)
380 * ------------------------------------------------------------------------*/
383 #define OFFMIN (EXTMIN+NUM_EXT)
385 #define OFFMIN (TUPMIN+NUM_TUPLES)
387 #define isOffset(c) (OFFMIN<=(c) && (c)<MODMIN)
388 #define offsetOf(c) ((c)-OFFMIN)
389 #define mkOffset(o) (OFFMIN+(o))
391 /* --------------------------------------------------------------------------
393 * ------------------------------------------------------------------------*/
395 /* An entry in a very crude object symbol table */
396 typedef struct { char* nm; void* ad; }
399 /* Indication of section kinds for loaded objects. Needed by
400 the GC for deciding whether or not a pointer on the stack
403 typedef enum { HUGS_DL_SECTION_CODE_OR_RODATA,
404 HUGS_DL_SECTION_RWDATA,
405 HUGS_DL_SECTION_OTHER }
408 typedef struct { void* start; void* end; DLSect sect; }
411 /* --------------------------------------------------------------------------
413 * ------------------------------------------------------------------------*/
415 #define MODMIN (OFFMIN+NUM_OFFSETS)
417 #define isModule(c) (MODMIN<=(c) && (c)<TYCMIN)
418 #define mkModule(n) (MODMIN+(n))
419 #define module(n) tabModule[(n)-MODMIN]
421 /* Under Haskell 1.3, the list of qualified imports is always a subset
422 * of the list of unqualified imports. For simplicity and flexibility,
423 * we do not attempt to exploit this fact - when a module is imported
424 * unqualified, it is added to both the qualified and unqualified
426 * Similarily, Haskell 1.3 does not allow a constructor to be imported
427 * or exported without exporting the type it belongs to but the export
428 * list is just a flat list of Texts (before static analysis) or
429 * Tycons, Names and Classes (after static analysis).
433 /* Lists of top level objects (local defns + imports) */
437 List exports; /* [ Entity | (Entity, NIL|DOTDOT) ] */
438 /* List of qualified imports. Used both during compilation and when
439 * evaluating an expression in the context of the current module.
443 /* ptr to malloc'd lump of memory holding the obj file */
446 /* ptr to object symbol table; lives in mallocville.
447 Dynamically expands. */
452 /* The section-kind entries for this object module. Dynamically expands. */
458 extern Module currentModule; /* Module currently being processed */
459 extern struct Module DECTABLE(tabModule);
461 extern Bool isValidModule Args((Module));
462 extern Module newModule Args((Text));
463 extern Module findModule Args((Text));
464 extern Module findModid Args((Cell));
465 extern Void setCurrModule Args((Module));
467 extern void addOTabName Args((Module,char*,void*));
468 extern void* lookupOTabName Args((Module,char*));
469 extern char* nameFromOPtr Args((void*));
471 extern void addDLSect Args((Module,void*,void*,DLSect));
472 extern DLSect lookupDLSect Args((void*));
475 #define isPrelude(m) (m==modulePrelude)
477 /* --------------------------------------------------------------------------
478 * Type constructor names:
479 * ------------------------------------------------------------------------*/
481 #define TYCMIN (MODMIN+NUM_MODULE)
482 #define isTycon(c) (TYCMIN<=(c) && (c)<NAMEMIN)
483 #define mkTycon(n) (TCMIN+(n))
484 #define tycon(n) tabTycon[(n)-TYCMIN]
489 Module mod; /* module that defines it */
491 Kind kind; /* kind (includes arity) of Tycon */
492 Cell what; /* DATATYPE/SYNONYM/RESTRICTSYN... */
494 Name conToTag; /* used in derived code */
499 extern struct strTycon DECTABLE(tabTycon);
501 extern Tycon newTycon Args((Text));
502 extern Tycon findTycon Args((Text));
503 extern Tycon addTycon Args((Tycon));
504 extern Tycon findQualTycon Args((Cell));
505 extern Tycon addPrimTycon Args((Text,Kind,Int,Cell,Cell));
507 #define isSynonym(h) (isTycon(h) && tycon(h).what==SYNONYM)
508 #define mkPolyType(n,t) pair(POLYTYPE,pair(n,t))
509 #define isPolyType(t) (isPair(t) && fst(t)==POLYTYPE)
510 #define polySigOf(t) fst(snd(t))
511 #define monotypeOf(t) snd(snd(t))
513 /* --------------------------------------------------------------------------
514 * Globally defined name values:
515 * ------------------------------------------------------------------------*/
517 #define NAMEMIN (TYCMIN+NUM_TYCON)
518 #define isName(c) (NAMEMIN<=(c) && (c)<INSTMIN)
519 #define mkName(n) (NAMEMIN+(n))
520 #define name(n) tabName[(n)-NAMEMIN]
525 Module mod; /* module that defines it */
532 Cell stgVar; /* really StgVar */
533 Int stgSize; /* == stgSize(stgVarBody(.stgVar)) */
534 Bool inlineMe; /* self-evident */
535 Bool simplified; /* TRUE => already simplified */
536 Bool isDBuilder; /* TRUE => is a dictionary builder */
537 const void* primop; /* really StgPrim* */
541 extern int numNames Args(( Void ));
543 extern struct strName DECTABLE(tabName);
545 /* The number field in a name is used to distinguish various kinds of name:
546 * mfunNo(i) = code for member function, offset i
547 * members that are sole elements of dict use mfunNo(0)
548 * members of dicts with more than one elem use mfunNo(n), n>=1
549 * EXECNAME = code for executable name (bytecodes or primitive)
550 * SELNAME = code for selector function
551 * DFUNNAME = code for dictionary builder or selector
552 * cfunNo(i) = code for data constructor
553 * datatypes with only one constructor uses cfunNo(0)
554 * datatypes with multiple constructors use cfunNo(n), n>=1
562 #define isSfun(n) (name(n).number==SELNAME)
563 #define isDfun(n) (name(n).number==DFUNNAME)
565 #define isCfun(n) (name(n).number>=CFUNNAME)
566 #define cfunOf(n) (name(n).number-CFUNNAME)
567 #define cfunNo(i) ((i)+CFUNNAME)
568 #define hasCfun(cs) (nonNull(cs) && isCfun(hd(cs)))
570 #define isMfun(n) (name(n).number<0)
571 #define mfunOf(n) ((-1)-name(n).number)
572 #define mfunNo(i) ((-1)-(i))
574 extern Name newName Args((Text,Cell));
575 extern Name findName Args((Text));
576 extern Name addName Args((Name));
577 extern Name findQualName Args((Cell));
578 extern Name addPrimCfun Args((Text,Int,Int,Cell));
579 extern Name addPrimCfunREP Args((Text,Int,Int,Int));
580 extern Int sfunPos Args((Name,Name));
581 extern Name nameFromStgVar Args((Cell));
582 extern Name jrsFindQualName Args((Text,Text));
584 /* --------------------------------------------------------------------------
586 * ------------------------------------------------------------------------*/
588 #define INSTMIN (NAMEMIN+NUM_NAME) /* instances */
589 #define isInst(c) (INSTMIN<=(c) && (c)<CLASSMIN)
590 #define mkInst(n) (INSTMIN+(n))
591 #define instOf(c) ((Int)((c)-INSTMIN))
592 #define inst(in) tabInst[(in)-INSTMIN]
595 Class c; /* class C */
597 Module mod; /* module that defines it */
598 Kinds kinds; /* Kinds of variables in head */
599 Cell head; /* :: Pred */
600 List specifics; /* :: [Pred] */
601 Int numSpecifics; /* length(specifics) */
603 Name builder; /* Dictionary constructor function */
606 /* a predicate (an element :: Pred) is an application of a Class to one or
607 * more type expressions
610 #define CLASSMIN (INSTMIN+NUM_INSTS)
611 #define isClass(c) (CLASSMIN<=(c) && (c)<CHARMIN)
612 #define mkClass(n) (CLASSMIN+(n))
613 #define cclass(n) tabClass[(n)-CLASSMIN]
616 Text text; /* Name of class */
617 Int line; /* Line where declaration begins */
618 Module mod; /* module that declares it */
619 Int level; /* Level in class hierarchy */
620 Int arity; /* Number of arguments */
621 Kinds kinds; /* Kinds of constructors in class */
622 Cell head; /* Head of class */
623 Name dcon; /* Dictionary constructor function */
624 List supers; /* :: [Pred] */
625 Int numSupers; /* length(supers) */
626 List dsels; /* Superclass dictionary selectors */
627 List members; /* :: [Name] */
628 Int numMembers; /* length(members) */
629 Name dbuild; /* Default dictionary builder */
630 List defaults; /* :: [Name] */
631 List instances; /* :: [Inst] */
634 extern struct strClass DECTABLE(tabClass);
635 extern struct strInst far *tabInst;
637 extern Class newClass Args((Text));
638 extern Class classMax Args((Void));
639 extern Class findClass Args((Text));
640 extern Class addClass Args((Class));
641 extern Class findQualClass Args((Cell));
642 extern Inst newInst Args((Void));
643 extern Inst findFirstInst Args((Tycon));
644 extern Inst findNextInst Args((Tycon,Inst));
646 /* --------------------------------------------------------------------------
648 * ------------------------------------------------------------------------*/
650 #define CHARMIN (CLASSMIN+NUM_CLASSES)
651 #define MAXCHARVAL (NUM_CHARS-1)
652 #define isChar(c) (CHARMIN<=(c) && (c)<INTMIN)
653 #define charOf(c) ((Char)(c-CHARMIN))
654 #define mkChar(c) ((Cell)(CHARMIN+(((unsigned)(c))%NUM_CHARS)))
656 /* --------------------------------------------------------------------------
657 * Small Integer values:
658 * ------------------------------------------------------------------------*/
660 #define INTMIN (CHARMIN+NUM_CHARS)
661 #define INTMAX (MAXPOSINT)
662 #define isSmall(c) (INTMIN<=(c))
663 #define INTZERO (INTMIN/2 + INTMAX/2)
664 #define MINSMALLINT (INTMIN - INTZERO)
665 #define MAXSMALLINT (INTMAX - INTZERO)
666 #define mkDigit(c) ((Cell)((c)+INTMIN))
667 #define digitOf(c) ((Int)((c)-INTMIN))
669 extern Bool isInt Args((Cell));
670 extern Int intOf Args((Cell));
671 extern Cell mkInt Args((Int));
673 /* --------------------------------------------------------------------------
674 * Implementation of triples:
675 * ------------------------------------------------------------------------*/
677 #define triple(x,y,z) pair(x,pair(y,z))
678 #define fst3(c) fst(c)
679 #define snd3(c) fst(snd(c))
680 #define thd3(c) snd(snd(c))
682 /* --------------------------------------------------------------------------
683 * Implementation of lists:
684 * ------------------------------------------------------------------------*/
687 #define isNull(c) ((c)==NIL)
688 #define nonNull(c) (c)
689 #define cons(x,xs) pair(x,xs)
690 #define singleton(x) cons(x,NIL)
691 #define doubleton(x,y) cons(x,cons(y,NIL))
692 #define tripleton(x,y,z) cons(x,cons(y,cons(z,NIL)))
696 extern Int length Args((List));
697 extern List appendOnto Args((List,List)); /* destructive */
698 extern List dupOnto Args((List,List));
699 extern List dupList Args((List));
700 extern List revOnto Args((List, List)); /* destructive */
701 #define rev(xs) revOnto((xs),NIL) /* destructive */
702 #define reverse(xs) revOnto(dupList(xs),NIL) /* non-destructive */
703 extern Cell cellIsMember Args((Cell,List));
704 extern Cell cellAssoc Args((Cell,List));
705 extern Cell cellRevAssoc Args((Cell,List));
706 extern Bool eqList Args((List,List));
707 extern Cell varIsMember Args((Text,List));
708 extern Name nameIsMember Args((Text,List));
709 extern Cell intIsMember Args((Int,List));
710 extern List replicate Args((Int,Cell));
711 extern List diffList Args((List,List)); /* destructive */
712 extern List deleteCell Args((List,Cell)); /* non-destructive */
713 extern List take Args((Int,List)); /* destructive */
714 extern List splitAt Args((Int,List)); /* non-destructive */
715 extern Cell nth Args((Int,List));
716 extern List removeCell Args((Cell,List)); /* destructive */
717 extern List dupListOnto Args((List,List)); /* non-destructive */
718 extern List nubList Args((List)); /* non-destructive */
720 /* The following macros provide `inline expansion' of some common ways of
721 * traversing, using and modifying lists:
723 * N.B. We use the names _f, _a, _xs, Zs, in an attempt to avoid clashes
724 * with identifiers used elsewhere.
727 #define mapBasic(_init,_step) {List Zs=(_init);\
728 for(;nonNull(Zs);Zs=tl(Zs)) \
730 #define mapModify(_init,_step) mapBasic(_init,hd(Zs)=_step)
732 #define mapProc(_f,_xs) mapBasic(_xs,_f(hd(Zs)))
733 #define map1Proc(_f,_a,_xs) mapBasic(_xs,_f(_a,hd(Zs)))
734 #define map2Proc(_f,_a,_b,_xs) mapBasic(_xs,_f(_a,_b,hd(Zs)))
735 #define map3Proc(_f,_a,_b,_c,_xs) mapBasic(_xs,_f(_a,_b,_c,hd(Zs)))
736 #define map4Proc(_f,_a,_b,_c,_d,_xs) mapBasic(_xs,_f(_a,_b,_c,_d,hd(Zs)))
738 #define mapOver(_f,_xs) mapModify(_xs,_f(hd(Zs)))
739 #define map1Over(_f,_a,_xs) mapModify(_xs,_f(_a,hd(Zs)))
740 #define map2Over(_f,_a,_b,_xs) mapModify(_xs,_f(_a,_b,hd(Zs)))
741 #define map3Over(_f,_a,_b,_c,_xs) mapModify(_xs,_f(_a,_b,_c,hd(Zs)))
742 #define map4Over(_f,_a,_b,_c,_d,_xs) mapModify(_xs,_f(_a,_b,_c,_d,hd(Zs)))
744 /* This is just what you want for functions with accumulating parameters */
745 #define mapAccum(_f,_acc,_xs) mapBasic(_xs,_acc=_f(_acc,hd(Zs)))
746 #define map1Accum(_f,_acc,_a,_xs) mapBasic(_xs,_acc=_f(_acc,_a,hd(Zs)))
747 #define map2Accum(_f,_acc,_a,_b,_xs) mapBasic(_xs,_acc=_f(_acc,_a,_b,hd(Zs)))
748 #define map3Accum(_f,_acc,_a,_b,_c,_xs) mapBasic(_xs,_acc=_f(_acc,_a,_b,_c,hd(Zs)))
750 /* --------------------------------------------------------------------------
751 * Implementation of function application nodes:
752 * ------------------------------------------------------------------------*/
754 #define ap(f,x) pair(f,x)
755 #define ap1(f,x) ap(f,x)
756 #define ap2(f,x,y) ap(ap(f,x),y)
757 #define ap3(f,x,y,z) ap(ap(ap(f,x),y),z)
758 #define fun(c) fst(c)
759 #define arg(c) snd(c)
760 #define isAp(c) (isPair(c) && !isTag(fst(c)))
761 extern Cell getHead Args((Cell));
762 extern List getArgs Args((Cell));
764 extern Cell nthArg Args((Int,Cell));
765 extern Int numArgs Args((Cell));
766 extern Cell applyToArgs Args((Cell,List));
768 /* --------------------------------------------------------------------------
769 * Stack implementation:
771 * NB: Use of macros makes order of evaluation hard to predict.
772 * For example, "push(1+pop());" doesn't increment TOS.
773 * ------------------------------------------------------------------------*/
775 extern Cell DECTABLE(cellStack);
778 #define clearStack() sp=(-1)
779 #define stackEmpty() (sp==(-1))
780 #define stack(p) cellStack[p]
781 #define chkStack(n) if (sp>=NUM_STACK-(n)) hugsStackOverflow()
787 #define onto(c) stack(++sp)=(c);
788 #define pop() stack(sp--)
790 #define top() stack(sp)
791 #define pushed(n) stack(sp-(n))
792 #define topfun(f) top()=ap((f),top())
793 #define toparg(x) top()=ap(top(),(x))
795 extern Void hugsStackOverflow Args((Void));
797 /* --------------------------------------------------------------------------
798 * Script file control:
799 * The implementation of script file storage is hidden.
800 * ------------------------------------------------------------------------*/
802 extern Script startNewScript Args((String));
803 extern Bool moduleThisScript Args((Module));
804 extern Module moduleOfScript Args((Script));
805 extern Bool isPreludeScript Args((Void));
806 extern Module lastModule Args((Void));
807 extern Script scriptThisFile Args((Text));
808 extern Script scriptThisName Args((Name));
809 extern Script scriptThisTycon Args((Tycon));
810 extern Script scriptThisInst Args((Inst));
811 extern Script scriptThisClass Args((Class));
812 extern String fileOfModule Args((Module));
813 extern Void dropScriptsFrom Args((Script));
816 /* --------------------------------------------------------------------------
818 * ------------------------------------------------------------------------*/
821 /* This is an exact copy of the declaration found in GreenCard.h */
823 typedef int HugsStackPtr;
824 typedef int HugsStablePtr;
825 typedef Pointer HugsForeign;
829 /* evaluate next argument */
830 int (*getInt ) Args(());
831 unsigned int (*getWord ) Args(());
832 void* (*getAddr ) Args(());
833 float (*getFloat ) Args(());
834 double (*getDouble) Args(());
835 char (*getChar ) Args(());
836 HugsForeign (*getForeign) Args(());
837 HugsStablePtr (*getStablePtr) Args(());
839 /* push part of result */
840 void (*putInt ) Args((int));
841 void (*putWord ) Args((unsigned int));
842 void (*putAddr ) Args((void*));
843 void (*putFloat ) Args((double));
844 void (*putDouble) Args((double));
845 void (*putChar ) Args((char));
846 void (*putForeign) Args((HugsForeign, void (*)(HugsForeign)));
847 void (*putStablePtr) Args((HugsStablePtr));
849 /* return n values in IO monad or Id monad */
850 void (*returnIO) Args((HugsStackPtr, int));
851 void (*returnId) Args((HugsStackPtr, int));
852 int (*runIO) Args((int));
854 /* free a stable pointer */
855 void (*freeStablePtr) Args((HugsStablePtr));
857 /* register the prim table */
858 void (*registerPrims) Args((struct primInfo*));
860 /* garbage collect */
861 void (*garbageCollect) Args(());
865 extern HugsAPI2* hugsAPI2 Args((Void));
866 typedef Void (*InitModuleFun2) Args((HugsAPI2*));
869 Name nameTrue, nameFalse;
870 Name nameNil, nameCons;
871 Name nameJust, nameNothing;
872 Name nameLeft, nameRight;
876 Cell (*makeInt) Args((Int));
878 Cell (*makeChar) Args((Char));
879 Char (*CharOf) Args((Cell));
881 Cell (*makeFloat) Args((FloatPro));
882 Cell (*makeTuple) Args((Int));
883 Pair (*pair) Args((Cell,Cell));
885 Cell (*mkMallocPtr) Args((Void *, Void (*)(Void *)));
886 Void *(*derefMallocPtr) Args((Cell));
888 Int (*mkStablePtr) Args((Cell));
889 Cell (*derefStablePtr) Args((Int));
890 Void (*freeStablePtr) Args((Int));
892 Void (*eval) Args((Cell));
893 Cell (*evalWithNoError) Args((Cell));
894 Void (*evalFails) Args((StackPtr));
900 Void (*garbageCollect) Args(());
901 Void (*stackOverflow) Args(());
902 Void (*internal) Args((String)) HUGS_noreturn;
904 Void (*registerPrims) Args((struct primInfo*));
905 Name (*addPrimCfun) Args((Text,Int,Int,Cell));
906 Text (*inventText) Args(());
908 Cell *(*Fst) Args((Cell));
909 Cell *(*Snd) Args((Cell));
915 extern HugsAPI1* hugsAPI1 Args((Void));
916 typedef Void (*InitModuleFun1) Args((HugsAPI1*));
920 /* --------------------------------------------------------------------------
922 * ------------------------------------------------------------------------*/
924 extern Void setLastExpr Args((Cell));
925 extern Cell getLastExpr Args((Void));
926 extern List addTyconsMatching Args((String,List));
927 extern List addNamesMatching Args((String,List));
929 /*-------------------------------------------------------------------------*/