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/15 13:16:20 $
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,
350 forall-y bit (eg __forall [a b] =>),
351 other bit, eg { C a1 } -> { C2 a2 } -> ... -> { Cn an },
352 name of dictionary builder,
353 (after startGHCInstance) the instance table location */
355 #define I_TYPE 115 /* snd :: ((Line, ConId, [((VarId,Kind))], Type))
356 lineno, tycon, kinded tyvars, the type expr */
358 #define I_DATA 116 /* snd :: ((Line, [((QConId,VarId))], ConId,
359 [((VarId,Kind))], [Constr])
360 lineno, context, tycon, kinded tyvars, constrs
361 An empty constr list means exported abstractly. */
363 #define I_NEWTYPE 117 /* snd :: ((Line, [((QConId,VarId))], ConId,
364 [((VarId,Kind))], ((ConId,Type)) ))
365 lineno, context, tycon, kinded tyvars, constr
366 constr==NIL means exported abstractly. */
368 #define I_CLASS 118 /* snd :: ((Line, [((QConId,VarId))], ConId,
369 [((VarId,Kind))], [((VarId,Type))]))
370 lineno, context, classname,
371 kinded tyvars, method sigs */
373 #define I_VALUE 119 /* snd :: ((Line, VarId, Type)) */
379 #define ZCONS 190 /* snd :: (Cell,Cell) */
383 #define ZTUP2 192 /* snd :: (Cell,Cell) */
384 #define ZTUP3 193 /* snd :: (Cell,(Cell,Cell)) */
385 #define ZTUP4 194 /* snd :: (Cell,(Cell,(Cell,Cell))) */
386 #define ZTUP5 195 /* snd :: (Cell,(Cell,(Cell,(Cell,Cell)))) */
388 /* Last constructor tag must be less than SPECMIN */
390 /* --------------------------------------------------------------------------
391 * Special cell values:
392 * ------------------------------------------------------------------------*/
397 #define isSpec(c) (SPECMIN<=(c) && (c)<EXTMIN)/* Special cell values */
399 #define isSpec(c) (SPECMIN<=(c) && (c)<OFFMIN)
402 #define NONE 201 /* Dummy stub */
403 #define STAR 202 /* Representing the kind of types */
405 #define ROW 203 /* Representing the kind of rows */
407 #define WILDCARD 204 /* Wildcard pattern */
408 #define SKOLEM 205 /* Skolem constant */
410 #define DOTDOT 206 /* ".." in import/export list */
412 #define NAME 210 /* whatIs code for isName */
413 #define TYCON 211 /* whatIs code for isTycon */
414 #define CLASS 212 /* whatIs code for isClass */
415 #define MODULE 213 /* whatIs code for isModule */
416 #define INSTANCE 214 /* whatIs code for isInst */
417 #define TUPLE 215 /* whatIs code for tuple constructor */
418 #define OFFSET 216 /* whatis code for offset */
419 #define AP 217 /* whatIs code for application node */
420 #define CHARCELL 218 /* whatIs code for isChar */
422 #define EXT 219 /* whatIs code for isExt */
425 #define SIGDECL 220 /* Signature declaration */
426 #define FIXDECL 221 /* Fixity declaration */
427 #define FUNBIND 222 /* Function binding */
428 #define PATBIND 223 /* Pattern binding */
430 #define DATATYPE 230 /* Datatype type constructor */
431 #define NEWTYPE 231 /* Newtype type constructor */
432 #define SYNONYM 232 /* Synonym type constructor */
433 #define RESTRICTSYN 233 /* Synonym with restricted scope */
435 #define NODEPENDS 235 /* Stop calculation of deps in type check*/
436 #define PREDEFINED 236 /* Predefined name, not yet filled */
438 /* --------------------------------------------------------------------------
439 * Tuple data/type constructors:
440 * ------------------------------------------------------------------------*/
442 extern Text ghcTupleText Args((Tycon));
443 extern Text ghcTupleText_n Args((Int));
449 #define isExt(c) (EXTMIN<=(c) && (c)<OFFMIN)
450 #define extText(e) tabExt[(e)-EXTMIN]
451 #define extField(c) arg(fun(c))
452 #define extRow(c) arg(c)
454 extern Text DECTABLE(tabExt);
455 extern Ext mkExt Args((Text));
460 extern Module findFakeModule ( Text t );
461 extern Tycon addTupleTycon ( Int n );
462 extern Name addWiredInBoxingTycon
463 ( String modNm, String typeNm, String constrNm,
464 Int rep, Kind kind );
465 Tycon addWiredInEnumTycon ( String modNm, String typeNm,
466 List /*of Text*/ constrs );
468 /* --------------------------------------------------------------------------
469 * Offsets: (generic types/stack offsets)
470 * ------------------------------------------------------------------------*/
473 #define OFFMIN (EXTMIN+NUM_EXT)
477 #define isOffset(c) (OFFMIN<=(c) && (c)<MODMIN)
478 #define offsetOf(c) ((c)-OFFMIN)
479 #define mkOffset(o) (OFFMIN+(o))
482 /* --------------------------------------------------------------------------
484 * ------------------------------------------------------------------------*/
486 #define MODMIN (OFFMIN+NUM_OFFSETS)
488 #define isModule(c) (MODMIN<=(c) && (c)<TYCMIN)
489 #define mkModule(n) (MODMIN+(n))
490 #define module(n) tabModule[(n)-MODMIN]
492 /* Import defns for the ObjectCode struct in Module. */
495 /* Under Haskell 1.3, the list of qualified imports is always a subset
496 * of the list of unqualified imports. For simplicity and flexibility,
497 * we do not attempt to exploit this fact - when a module is imported
498 * unqualified, it is added to both the qualified and unqualified
500 * Similarily, Haskell 1.3 does not allow a constructor to be imported
501 * or exported without exporting the type it belongs to but the export
502 * list is just a flat list of Texts (before static analysis) or
503 * Tycons, Names and Classes (after static analysis).
507 /* Lists of top level objects (local defns + imports) */
511 List exports; /* [ Entity | (Entity, NIL|DOTDOT) ] */
512 /* List of qualified imports. Used both during compilation and when
513 * evaluating an expression in the context of the current module.
517 /* TRUE if module exists only via GHC primop defn; usually FALSE */
520 /* The primary object file for this module. */
523 /* And any extras it might need. */
524 ObjectCode* objectExtras;
525 List objectExtraNames; /* :: [Text] -- names of extras */
529 extern Module currentModule; /* Module currently being processed */
530 extern struct Module DECTABLE(tabModule);
532 extern Bool isValidModule Args((Module));
533 extern Module newModule Args((Text));
534 extern Module findModule Args((Text));
535 extern Module findModid Args((Cell));
536 extern Void setCurrModule Args((Module));
538 extern void addOTabName Args((Module,char*,void*));
539 extern void* lookupOTabName Args((Module,char*));
540 extern char* nameFromOPtr Args((void*));
542 extern void addSection Args((Module,void*,void*,OSectionKind));
543 extern OSectionKind lookupSection Args((void*));
544 extern void* lookupOExtraTabName ( char* sym );
546 #define isPrelude(m) (m==modulePrelude)
548 #define N_PRELUDE_SCRIPTS (combined ? 30 : 1)
550 /* --------------------------------------------------------------------------
551 * Type constructor names:
552 * ------------------------------------------------------------------------*/
554 #define TYCMIN (MODMIN+NUM_MODULE)
555 #define isTycon(c) (TYCMIN<=(c) && (c)<NAMEMIN && tabTycon[(c)-TYCMIN].tuple==-1)
556 #define tycon(n) tabTycon[(n)-TYCMIN]
558 #define isTuple(c) (TYCMIN<=(c) && (c)<NAMEMIN && tabTycon[(c)-TYCMIN].tuple>=0)
559 #define tupleOf(n) (tabTycon[(n)-TYCMIN].tuple)
560 extern Tycon mkTuple ( Int );
566 Module mod; /* module that defines it */
567 Int tuple; /* tuple number, or -1 if not tuple */
569 Kind kind; /* kind (includes arity) of Tycon */
570 Cell what; /* DATATYPE/SYNONYM/RESTRICTSYN... */
572 Name conToTag; /* used in derived code */
574 void* itbl; /* For tuples, the info tbl pointer */
578 extern struct strTycon DECTABLE(tabTycon);
580 extern Tycon newTycon Args((Text));
581 extern Tycon findTycon Args((Text));
582 extern Tycon addTycon Args((Tycon));
583 extern Tycon findQualTycon Args((Cell));
584 extern Tycon addPrimTycon Args((Text,Kind,Int,Cell,Cell));
586 #define isSynonym(h) (isTycon(h) && tycon(h).what==SYNONYM)
587 #define isQualType(t) (isPair(t) && fst(t)==QUAL)
588 #define mkPolyType(n,t) pair(POLYTYPE,pair(n,t))
589 #define isPolyType(t) (isPair(t) && fst(t)==POLYTYPE)
590 #define isPolyOrQualType(t) (isPair(t) && (fst(t)==POLYTYPE || fst(t)==QUAL))
591 #define polySigOf(t) fst(snd(t))
592 #define monotypeOf(t) snd(snd(t))
594 #define bang(t) ap(BANG,t)
595 extern Tycon findQualTyconWithoutConsultingExportList ( QualId q );
597 /* --------------------------------------------------------------------------
598 * Globally defined name values:
599 * ------------------------------------------------------------------------*/
601 #define NAMEMIN (TYCMIN+NUM_TYCON)
602 #define isName(c) (NAMEMIN<=(c) && (c)<INSTMIN)
603 #define mkName(n) (NAMEMIN+(n))
604 #define name(n) tabName[(n)-NAMEMIN]
609 Module mod; /* module that defines it */
616 Cell stgVar; /* really StgVar */
617 Text callconv; /* for foreign import/export */
618 void* primop; /* really StgPrim* */
619 void* itbl; /* For constructors, the info tbl pointer */
623 extern int numNames Args(( Void ));
625 extern struct strName DECTABLE(tabName);
627 /* The number field in a name is used to distinguish various kinds of name:
628 * mfunNo(i) = code for member function, offset i
629 * members that are sole elements of dict use mfunNo(0)
630 * members of dicts with more than one elem use mfunNo(n), n>=1
631 * EXECNAME = code for executable name (bytecodes or primitive)
632 * SELNAME = code for selector function
633 * DFUNNAME = code for dictionary builder or selector
634 * cfunNo(i) = code for data constructor
635 * datatypes with only one constructor uses cfunNo(0)
636 * datatypes with multiple constructors use cfunNo(n), n>=1
644 #define isSfun(n) (name(n).number==SELNAME)
645 #define isDfun(n) (name(n).number==DFUNNAME)
647 #define isCfun(n) (name(n).number>=CFUNNAME)
648 #define cfunOf(n) (name(n).number-CFUNNAME)
649 #define cfunNo(i) ((i)+CFUNNAME)
650 #define hasCfun(cs) (nonNull(cs) && isCfun(hd(cs)))
652 #define isMfun(n) (name(n).number<0)
653 #define mfunOf(n) ((-1)-name(n).number)
654 #define mfunNo(i) ((-1)-(i))
656 extern Name newName Args((Text,Cell));
657 extern Name findName Args((Text));
658 extern Name addName Args((Name));
659 extern Name findQualName Args((Cell));
660 extern Name addPrimCfun Args((Text,Int,Int,Cell));
661 extern Name addPrimCfunREP Args((Text,Int,Int,Int));
662 extern Int sfunPos Args((Name,Name));
663 extern Name nameFromStgVar Args((Cell));
664 extern Name jrsFindQualName Args((Text,Text));
666 extern Name findQualNameWithoutConsultingExportList ( QualId q );
668 /* --------------------------------------------------------------------------
670 * ------------------------------------------------------------------------*/
672 #define INSTMIN (NAMEMIN+NUM_NAME) /* instances */
673 #define isInst(c) (INSTMIN<=(c) && (c)<CLASSMIN)
674 #define mkInst(n) (INSTMIN+(n))
675 #define instOf(c) ((Int)((c)-INSTMIN))
676 #define inst(in) tabInst[(in)-INSTMIN]
679 Class c; /* class C */
681 Module mod; /* module that defines it */
682 Kinds kinds; /* Kinds of variables in head */
683 Cell head; /* :: Pred */
684 List specifics; /* :: [Pred] */
685 Int numSpecifics; /* length(specifics) */
687 Name builder; /* Dictionary constructor function */
690 /* a predicate (an element :: Pred) is an application of a Class to one or
691 * more type expressions
694 #define CLASSMIN (INSTMIN+NUM_INSTS)
695 #define isClass(c) (CLASSMIN<=(c) && (c)<CHARMIN)
696 #define mkClass(n) (CLASSMIN+(n))
697 #define cclass(n) tabClass[(n)-CLASSMIN]
700 Text text; /* Name of class */
701 Int line; /* Line where declaration begins */
702 Module mod; /* module that declares it */
703 Int level; /* Level in class hierarchy */
704 Int arity; /* Number of arguments */
705 Kinds kinds; /* Kinds of constructors in class */
706 List fds; /* Functional Dependencies */
707 List xfds; /* Xpanded Functional Dependencies */
708 Cell head; /* Head of class */
709 Name dcon; /* Dictionary constructor function */
710 List supers; /* :: [Pred] */
711 Int numSupers; /* length(supers) */
712 List dsels; /* Superclass dictionary selectors */
713 List members; /* :: [Name] */
714 Int numMembers; /* length(members) */
715 List defaults; /* :: [Name] */
716 List instances; /* :: [Inst] */
719 extern struct strClass DECTABLE(tabClass);
720 extern struct strInst far *tabInst;
722 extern Class newClass Args((Text));
723 extern Class classMax Args((Void));
724 extern Class findClass Args((Text));
725 extern Class addClass Args((Class));
726 extern Class findQualClass Args((Cell));
727 extern Inst newInst Args((Void));
728 extern Inst findFirstInst Args((Tycon));
729 extern Inst findNextInst Args((Tycon,Inst));
730 extern List getAllKnownTyconsAndClasses ( void );
731 extern Class findQualClassWithoutConsultingExportList ( QualId q );
733 /* --------------------------------------------------------------------------
735 * ------------------------------------------------------------------------*/
737 #define CHARMIN (CLASSMIN+NUM_CLASSES)
738 #define MAXCHARVAL (NUM_CHARS-1)
739 #define isChar(c) (CHARMIN<=(c) && (c)<INTMIN)
740 #define charOf(c) ((Char)(c-CHARMIN))
741 #define mkChar(c) ((Cell)(CHARMIN+(((unsigned)(c))%NUM_CHARS)))
743 /* --------------------------------------------------------------------------
744 * Small Integer values:
745 * ------------------------------------------------------------------------*/
747 #define INTMIN (CHARMIN+NUM_CHARS)
748 #define INTMAX (MAXPOSINT)
749 #define isSmall(c) (INTMIN<=(c))
750 #define INTZERO (INTMIN/2 + INTMAX/2)
751 #define MINSMALLINT (INTMIN - INTZERO)
752 #define MAXSMALLINT (INTMAX - INTZERO)
753 #define mkDigit(c) ((Cell)((c)+INTMIN))
754 #define digitOf(c) ((Int)((c)-INTMIN))
756 extern Bool isInt Args((Cell));
757 extern Int intOf Args((Cell));
758 extern Cell mkInt Args((Int));
760 /* --------------------------------------------------------------------------
761 * Implementation of triples:
762 * ------------------------------------------------------------------------*/
764 #define triple(x,y,z) pair(x,pair(y,z))
765 #define fst3(c) fst(c)
766 #define snd3(c) fst(snd(c))
767 #define thd3(c) snd(snd(c))
769 /* --------------------------------------------------------------------------
770 * Implementation of lists:
771 * ------------------------------------------------------------------------*/
774 #define isNull(c) ((c)==NIL)
775 #define nonNull(c) (c)
776 #define cons(x,xs) pair(x,xs)
777 #define singleton(x) cons(x,NIL)
778 #define doubleton(x,y) cons(x,cons(y,NIL))
779 #define tripleton(x,y,z) cons(x,cons(y,cons(z,NIL)))
783 extern Int length Args((List));
784 extern List appendOnto Args((List,List)); /* destructive */
785 extern List dupOnto Args((List,List));
786 extern List dupList Args((List));
787 extern List revOnto Args((List, List)); /* destructive */
788 #define rev(xs) revOnto((xs),NIL) /* destructive */
789 #define reverse(xs) revOnto(dupList(xs),NIL) /* non-destructive */
790 extern Cell cellIsMember Args((Cell,List));
791 extern Cell cellAssoc Args((Cell,List));
792 extern Cell cellRevAssoc Args((Cell,List));
793 extern Bool eqList Args((List,List));
794 extern Cell varIsMember Args((Text,List));
795 extern Name nameIsMember Args((Text,List));
796 extern QualId qualidIsMember ( QualId, List );
797 extern Cell intIsMember Args((Int,List));
798 extern List replicate Args((Int,Cell));
799 extern List diffList Args((List,List)); /* destructive */
800 extern List deleteCell Args((List,Cell)); /* non-destructive */
801 extern List take Args((Int,List)); /* destructive */
802 extern List splitAt Args((Int,List)); /* non-destructive */
803 extern Cell nth Args((Int,List));
804 extern List removeCell Args((Cell,List)); /* destructive */
805 extern List dupListOnto Args((List,List)); /* non-destructive */
806 extern List nubList Args((List)); /* non-destructive */
808 /* The following macros provide `inline expansion' of some common ways of
809 * traversing, using and modifying lists:
811 * N.B. We use the names _f, _a, _xs, Zs, in an attempt to avoid clashes
812 * with identifiers used elsewhere.
815 #define mapBasic(_init,_step) {List Zs=(_init);\
816 for(;nonNull(Zs);Zs=tl(Zs)) \
818 #define mapModify(_init,_step) mapBasic(_init,hd(Zs)=_step)
820 #define mapProc(_f,_xs) mapBasic(_xs,_f(hd(Zs)))
821 #define map1Proc(_f,_a,_xs) mapBasic(_xs,_f(_a,hd(Zs)))
822 #define map2Proc(_f,_a,_b,_xs) mapBasic(_xs,_f(_a,_b,hd(Zs)))
823 #define map3Proc(_f,_a,_b,_c,_xs) mapBasic(_xs,_f(_a,_b,_c,hd(Zs)))
824 #define map4Proc(_f,_a,_b,_c,_d,_xs) mapBasic(_xs,_f(_a,_b,_c,_d,hd(Zs)))
826 #define mapOver(_f,_xs) mapModify(_xs,_f(hd(Zs)))
827 #define map1Over(_f,_a,_xs) mapModify(_xs,_f(_a,hd(Zs)))
828 #define map2Over(_f,_a,_b,_xs) mapModify(_xs,_f(_a,_b,hd(Zs)))
829 #define map3Over(_f,_a,_b,_c,_xs) mapModify(_xs,_f(_a,_b,_c,hd(Zs)))
830 #define map4Over(_f,_a,_b,_c,_d,_xs) mapModify(_xs,_f(_a,_b,_c,_d,hd(Zs)))
832 /* This is just what you want for functions with accumulating parameters */
833 #define mapAccum(_f,_acc,_xs) mapBasic(_xs,_acc=_f(_acc,hd(Zs)))
834 #define map1Accum(_f,_acc,_a,_xs) mapBasic(_xs,_acc=_f(_acc,_a,hd(Zs)))
835 #define map2Accum(_f,_acc,_a,_b,_xs) mapBasic(_xs,_acc=_f(_acc,_a,_b,hd(Zs)))
836 #define map3Accum(_f,_acc,_a,_b,_c,_xs) mapBasic(_xs,_acc=_f(_acc,_a,_b,_c,hd(Zs)))
839 /* --------------------------------------------------------------------------
840 * Strongly-typed lists (z-lists) and tuples (experimental)
841 * ------------------------------------------------------------------------*/
844 typedef Cell ZTriple;
850 extern Cell zcons ( Cell x, Cell xs );
851 extern Cell zhd ( Cell xs );
852 extern Cell ztl ( Cell xs );
853 extern Cell zsingleton ( Cell x );
854 extern Cell zdoubleton ( Cell x, Cell y );
855 extern Int zlength ( ZList xs );
856 extern ZList zreverse ( ZList xs );
859 extern Cell zpair ( Cell x1, Cell x2 );
860 extern Cell zfst ( Cell zpair );
861 extern Cell zsnd ( Cell zpair );
863 extern Cell ztriple ( Cell x1, Cell x2, Cell x3 );
864 extern Cell zfst3 ( Cell zpair );
865 extern Cell zsnd3 ( Cell zpair );
866 extern Cell zthd3 ( Cell zpair );
868 extern Cell z4ble ( Cell x1, Cell x2, Cell x3, Cell x4 );
869 extern Cell zsel14 ( Cell zpair );
870 extern Cell zsel24 ( Cell zpair );
871 extern Cell zsel34 ( Cell zpair );
872 extern Cell zsel44 ( Cell zpair );
874 extern Cell z5ble ( Cell x1, Cell x2, Cell x3, Cell x4, Cell x5 );
875 extern Cell zsel15 ( Cell zpair );
876 extern Cell zsel25 ( Cell zpair );
877 extern Cell zsel35 ( Cell zpair );
878 extern Cell zsel45 ( Cell zpair );
879 extern Cell zsel55 ( Cell zpair );
881 extern Cell unap ( int tag, Cell c );
882 #define isZPair(c) (whatIs((c))==ZTUP2)
884 /* --------------------------------------------------------------------------
885 * Implementation of function application nodes:
886 * ------------------------------------------------------------------------*/
888 #define ap(f,x) pair(f,x)
889 #define ap1(f,x) ap(f,x)
890 #define ap2(f,x,y) ap(ap(f,x),y)
891 #define ap3(f,x,y,z) ap(ap(ap(f,x),y),z)
892 #define fun(c) fst(c)
893 #define arg(c) snd(c)
894 #define isAp(c) (isPair(c) && !isTag(fst(c)))
895 extern Cell getHead Args((Cell));
896 extern List getArgs Args((Cell));
898 extern Cell nthArg Args((Int,Cell));
899 extern Int numArgs Args((Cell));
900 extern Cell applyToArgs Args((Cell,List));
902 /* --------------------------------------------------------------------------
903 * Stack implementation:
905 * NB: Use of macros makes order of evaluation hard to predict.
906 * For example, "push(1+pop());" doesn't increment TOS.
907 * ------------------------------------------------------------------------*/
909 extern Cell DECTABLE(cellStack);
912 #define clearStack() sp=(-1)
913 #define stackEmpty() (sp==(-1))
914 #define stack(p) cellStack[p]
915 #define chkStack(n) if (sp>=NUM_STACK-(n)) hugsStackOverflow()
921 #define onto(c) stack(++sp)=(c);
922 #define pop() stack(sp--)
924 #define top() stack(sp)
925 #define pushed(n) stack(sp-(n))
926 #define topfun(f) top()=ap((f),top())
927 #define toparg(x) top()=ap(top(),(x))
929 extern Void hugsStackOverflow Args((Void));
933 #define STACK_HEADROOM 16384
934 #define STACK_CHECK if (StackSpace() <= STACK_HEADROOM) \
935 internal("Macintosh function parameter stack overflow.");
940 /* --------------------------------------------------------------------------
941 * Script file control:
942 * The implementation of script file storage is hidden.
943 * ------------------------------------------------------------------------*/
945 extern Script startNewScript Args((String));
946 extern Bool moduleThisScript Args((Module));
947 extern Module moduleOfScript Args((Script));
948 extern Bool isPreludeScript Args((Void));
949 extern Module lastModule Args((Void));
950 extern Script scriptThisFile Args((Text));
951 extern Script scriptThisName Args((Name));
952 extern Script scriptThisTycon Args((Tycon));
953 extern Script scriptThisInst Args((Inst));
954 extern Script scriptThisClass Args((Class));
955 extern String fileOfModule Args((Module));
956 extern Void dropScriptsFrom Args((Script));
959 /* --------------------------------------------------------------------------
961 * ------------------------------------------------------------------------*/
964 /* This is an exact copy of the declaration found in GreenCard.h */
966 typedef int HugsStackPtr;
967 typedef int HugsStablePtr;
968 typedef Pointer HugsForeign;
972 /* evaluate next argument */
973 int (*getInt ) Args(());
974 unsigned int (*getWord ) Args(());
975 void* (*getAddr ) Args(());
976 float (*getFloat ) Args(());
977 double (*getDouble) Args(());
978 char (*getChar ) Args(());
979 HugsForeign (*getForeign) Args(());
980 HugsStablePtr (*getStablePtr) Args(());
982 /* push part of result */
983 void (*putInt ) Args((int));
984 void (*putWord ) Args((unsigned int));
985 void (*putAddr ) Args((void*));
986 void (*putFloat ) Args((double));
987 void (*putDouble) Args((double));
988 void (*putChar ) Args((char));
989 void (*putForeign) Args((HugsForeign, void (*)(HugsForeign)));
990 void (*putStablePtr) Args((HugsStablePtr));
992 /* return n values in IO monad or Id monad */
993 void (*returnIO) Args((HugsStackPtr, int));
994 void (*returnId) Args((HugsStackPtr, int));
995 int (*runIO) Args((int));
997 /* free a stable pointer */
998 void (*freeStablePtr) Args((HugsStablePtr));
1000 /* register the prim table */
1001 void (*registerPrims) Args((struct primInfo*));
1003 /* garbage collect */
1004 void (*garbageCollect) Args(());
1008 extern HugsAPI2* hugsAPI2 Args((Void));
1009 typedef Void (*InitModuleFun2) Args((HugsAPI2*));
1012 Name nameTrue, nameFalse;
1013 Name nameNil, nameCons;
1014 Name nameJust, nameNothing;
1015 Name nameLeft, nameRight;
1019 Cell (*makeInt) Args((Int));
1021 Cell (*makeChar) Args((Char));
1022 Char (*CharOf) Args((Cell));
1024 Cell (*makeFloat) Args((FloatPro));
1025 Cell (*makeTuple) Args((Int));
1026 Pair (*pair) Args((Cell,Cell));
1028 Cell (*mkMallocPtr) Args((Void *, Void (*)(Void *)));
1029 Void *(*derefMallocPtr) Args((Cell));
1031 Int (*mkStablePtr) Args((Cell));
1032 Cell (*derefStablePtr) Args((Int));
1033 Void (*freeStablePtr) Args((Int));
1035 Void (*eval) Args((Cell));
1036 Cell (*evalWithNoError) Args((Cell));
1037 Void (*evalFails) Args((StackPtr));
1043 Void (*garbageCollect) Args(());
1044 Void (*stackOverflow) Args(());
1045 Void (*internal) Args((String)) HUGS_noreturn;
1047 Void (*registerPrims) Args((struct primInfo*));
1048 Name (*addPrimCfun) Args((Text,Int,Int,Cell));
1049 Text (*inventText) Args(());
1051 Cell *(*Fst) Args((Cell));
1052 Cell *(*Snd) Args((Cell));
1058 extern HugsAPI1* hugsAPI1 Args((Void));
1059 typedef Void (*InitModuleFun1) Args((HugsAPI1*));
1060 #endif /* PLUGINS */
1063 /* --------------------------------------------------------------------------
1065 * ------------------------------------------------------------------------*/
1067 extern Void setLastExpr Args((Cell));
1068 extern Cell getLastExpr Args((Void));
1069 extern List addTyconsMatching Args((String,List));
1070 extern List addNamesMatching Args((String,List));
1072 /*-------------------------------------------------------------------------*/