2 /* --------------------------------------------------------------------------
3 * Primitives for manipulating global data structures
5 * The Hugs 98 system is Copyright (c) Mark P Jones, Alastair Reid, the
6 * Yale Haskell Group, and the Oregon Graduate Institute of Science and
7 * Technology, 1994-1999, All rights reserved. It is distributed as
8 * free software under the license in the file "License", which is
9 * included in the distribution.
11 * $RCSfile: storage.c,v $
13 * $Date: 2000/04/06 14:23:55 $
14 * ------------------------------------------------------------------------*/
16 #include "hugsbasictypes.h"
23 /*#define DEBUG_SHOWUSE*/
25 /* --------------------------------------------------------------------------
26 * local function prototypes:
27 * ------------------------------------------------------------------------*/
29 static Int local hash ( String );
30 static Int local saveText ( Text );
31 static Module local findQualifier ( Text );
32 static Void local hashTycon ( Tycon );
33 static List local insertTycon ( Tycon,List );
34 static Void local hashName ( Name );
35 static List local insertName ( Name,List );
36 static Void local patternError ( String );
37 static Bool local stringMatch ( String,String );
38 static Bool local typeInvolves ( Type,Type );
39 static Cell local markCell ( Cell );
40 static Void local markSnd ( Cell );
41 static Cell local lowLevelLastIn ( Cell );
42 static Cell local lowLevelLastOut ( Cell );
45 /* --------------------------------------------------------------------------
48 * provides storage for the characters making up identifier and symbol
49 * names, string literals, character constants etc...
51 * All character strings are stored in a large character array, with textHw
52 * pointing to the next free position. Lookup in the array is improved using
53 * a hash table. Internally, text strings are represented by integer offsets
54 * from the beginning of the array to the string in question.
56 * Where memory permits, the use of multiple hashtables gives a significant
57 * increase in performance, particularly when large source files are used.
59 * Each string in the array is terminated by a zero byte. No string is
60 * stored more than once, so that it is safe to test equality of strings by
61 * comparing the corresponding offsets.
63 * Special text values (beyond the range of the text array table) are used
64 * to generate unique `new variable names' as required.
66 * The same text storage is also used to hold text values stored in a saved
67 * expression. This grows downwards from the top of the text table (and is
68 * not included in the hash table).
69 * ------------------------------------------------------------------------*/
71 #define TEXTHSZ 512 /* Size of Text hash table */
72 #define NOTEXT ((Text)(~0)) /* Empty bucket in Text hash table */
73 static Text textHw; /* Next unused position */
74 static Text savedText = TEXT_SIZE; /* Start of saved portion of text */
75 static Text nextNewText; /* Next new text value */
76 static Text nextNewDText; /* Next new dict text value */
77 static char text[TEXT_SIZE]; /* Storage of character strings */
78 static Text textHash[TEXTHSZ][NUM_TEXTH]; /* Hash table storage */
80 String textToStr(t) /* find string corresp to given Text*/
82 static char newVar[16];
84 if (isText(t)) /* standard char string */
85 return text + t - TEXT_BASE_ADDR;
86 if (isInventedDictVar(t)) {
88 t-INDVAR_BASE_ADDR); /* dictionary variable */
91 if (isInventedVar(t)) {
93 t-INVAR_BASE_ADDR); /* normal variable */
96 internal("textToStr");
99 String identToStr(v) /*find string corresp to given ident or qualified name*/
102 internal("identToStr");
108 case CONOPCELL : return textToStr(textOf(v));
110 case QUALIDENT : { String qmod = textToStr(qmodOf(v));
111 String qtext = textToStr(qtextOf(v));
114 while (pos+1 < savedText && *qmod!=0) {
115 text[pos++] = *qmod++;
117 if (pos+1 < savedText) {
120 while (pos+1 < savedText && *qtext!=0) {
121 text[pos++] = *qtext++;
127 internal("identToStr2");
128 return 0; /* NOTREACHED */
131 Text inventText() { /* return new unused variable name */
132 if (nextNewText >= INVAR_BASE_ADDR+INVAR_MAX_AVAIL)
133 internal("inventText: too many invented variables");
134 return nextNewText++;
137 Text inventDictText() { /* return new unused dictvar name */
138 if (nextNewDText >= INDVAR_BASE_ADDR+INDVAR_MAX_AVAIL)
139 internal("inventDictText: too many invented variables");
140 return nextNewDText++;
143 Bool inventedText(t) /* Signal TRUE if text has been */
144 Text t; { /* generated internally */
145 return isInventedVar(t) || isInventedDictVar(t);
148 #define MAX_FIXLIT 100
149 Text fixLitText(t) /* fix literal text that might include \ */
151 String s = textToStr(t);
154 for(i = 0;i < MAX_FIXLIT-2 && *s;s++) {
160 if (i < MAX_FIXLIT-2) {
163 ERRMSG(0) "storage space exhausted for internal literal string"
166 return (findText(p));
170 static Int local hash(s) /* Simple hash function on strings */
174 for (v=((int)(*s))*8; *s; s++)
175 v += ((int)(*s))*(j++);
181 Text findText(s) /* Locate string in Text array */
185 Text textPos = textHash[h][hashno];
187 # define TryMatch { Text originalTextPos = textPos; \
189 for (t=s; *t==text[textPos]; textPos++,t++) \
191 return originalTextPos+TEXT_BASE_ADDR; \
193 # define Skip while (text[textPos++]) ;
195 while (textPos!=NOTEXT) {
197 if (++hashno<NUM_TEXTH) /* look in next hashtable entry */
198 textPos = textHash[h][hashno];
201 while (textPos < textHw) {
212 textPos = textHw; /* if not found, save in array */
213 if (textHw + (Int)strlen(s) + 1 > savedText) {
214 ERRMSG(0) "Character string storage space exhausted"
217 while ((text[textHw++] = *s++) != 0) {
219 if (hashno<NUM_TEXTH) { /* updating hash table as necessary */
220 textHash[h][hashno] = textPos;
221 if (hashno<NUM_TEXTH-1)
222 textHash[h][hashno+1] = NOTEXT;
225 return textPos+TEXT_BASE_ADDR;
228 static Int local saveText(t) /* Save text value in buffer */
229 Text t; { /* at top of text table */
230 String s = textToStr(t);
232 if (textHw + l + 1 > savedText) {
233 ERRMSG(0) "Character string storage space exhausted"
237 strcpy(text+savedText,s);
242 static int fromHexDigit ( char c )
245 case '0': case '1': case '2': case '3': case '4':
246 case '5': case '6': case '7': case '8': case '9':
248 case 'a': case 'A': return 10;
249 case 'b': case 'B': return 11;
250 case 'c': case 'C': return 12;
251 case 'd': case 'D': return 13;
252 case 'e': case 'E': return 14;
253 case 'f': case 'F': return 15;
259 /* returns findText (unZencode s) */
260 Text unZcodeThenFindText ( String s )
267 nn = 100 + 10 * strlen(s);
269 if (!p) internal ("unZcodeThenFindText: malloc failed");
274 if (n > nn-90) internal ("unZcodeThenFindText: result is too big");
275 if (*s != 'z' && *s != 'Z') {
280 if (!(*s)) goto parse_error;
282 case 'Z': p[n++] = 'Z'; break;
283 case 'C': p[n++] = ':'; break;
284 case 'L': p[n++] = '('; break;
285 case 'R': p[n++] = ')'; break;
286 case 'M': p[n++] = '['; break;
287 case 'N': p[n++] = ']'; break;
288 case 'z': p[n++] = 'z'; break;
289 case 'a': p[n++] = '&'; break;
290 case 'b': p[n++] = '|'; break;
291 case 'd': p[n++] = '$'; break;
292 case 'e': p[n++] = '='; break;
293 case 'g': p[n++] = '>'; break;
294 case 'h': p[n++] = '#'; break;
295 case 'i': p[n++] = '.'; break;
296 case 'l': p[n++] = '<'; break;
297 case 'm': p[n++] = '-'; break;
298 case 'n': p[n++] = '!'; break;
299 case 'p': p[n++] = '+'; break;
300 case 'q': p[n++] = '\\'; break;
301 case 'r': p[n++] = '\''; break;
302 case 's': p[n++] = '/'; break;
303 case 't': p[n++] = '*'; break;
304 case 'u': p[n++] = '^'; break;
305 case 'v': p[n++] = '%'; break;
307 if (!s[0] || !s[1]) goto parse_error;
308 if (fromHexDigit(s[0]) < 0 || fromHexDigit(s[1]) < 0) goto parse_error;
309 p[n++] = 16 * fromHexDigit(s[0]) + fromHexDigit(s[1]);
312 case '0': case '1': case '2': case '3': case '4':
313 case '5': case '6': case '7': case '8': case '9':
316 while (*s && isdigit((int)(*s))) {
317 i = 10 * i + (*s - '0');
320 if (*s != 'T') goto parse_error;
323 while (i > 0) { p[n++] = ','; i--; };
337 fprintf ( stderr, "\nstring = `%s'\n", s );
338 internal ( "unZcodeThenFindText: parse error on above string");
339 return NIL; /*notreached*/
343 Text enZcodeThenFindText ( String s )
348 char toHex[16] = "0123456789ABCDEF";
351 nn = 100 + 10 * strlen(s);
353 if (!p) internal ("enZcodeThenFindText: malloc failed");
357 if (n > nn-90) internal ("enZcodeThenFindText: result is too big");
360 && (isalnum((int)(*s)) || *s == '_')) {
368 while (*s && *s==',') { s++; tup++; };
369 if (*s != ')') internal("enZcodeThenFindText: invalid tuple type");
372 sprintf(num,"%d",tup);
373 p[n] = 0; strcat ( &(p[n]), num ); n += strlen(num);
378 case '(': p[n++] = 'Z'; p[n++] = 'L'; break;
379 case ')': p[n++] = 'Z'; p[n++] = 'R'; break;
380 case '[': p[n++] = 'Z'; p[n++] = 'M'; break;
381 case ']': p[n++] = 'Z'; p[n++] = 'N'; break;
382 case ':': p[n++] = 'Z'; p[n++] = 'C'; break;
383 case 'Z': p[n++] = 'Z'; p[n++] = 'Z'; break;
384 case 'z': p[n++] = 'z'; p[n++] = 'z'; break;
385 case '&': p[n++] = 'z'; p[n++] = 'a'; break;
386 case '|': p[n++] = 'z'; p[n++] = 'b'; break;
387 case '$': p[n++] = 'z'; p[n++] = 'd'; break;
388 case '=': p[n++] = 'z'; p[n++] = 'e'; break;
389 case '>': p[n++] = 'z'; p[n++] = 'g'; break;
390 case '#': p[n++] = 'z'; p[n++] = 'h'; break;
391 case '.': p[n++] = 'z'; p[n++] = 'i'; break;
392 case '<': p[n++] = 'z'; p[n++] = 'l'; break;
393 case '-': p[n++] = 'z'; p[n++] = 'm'; break;
394 case '!': p[n++] = 'z'; p[n++] = 'n'; break;
395 case '+': p[n++] = 'z'; p[n++] = 'p'; break;
396 case '\'': p[n++] = 'z'; p[n++] = 'q'; break;
397 case '\\': p[n++] = 'z'; p[n++] = 'r'; break;
398 case '/': p[n++] = 'z'; p[n++] = 's'; break;
399 case '*': p[n++] = 'z'; p[n++] = 't'; break;
400 case '^': p[n++] = 'z'; p[n++] = 'u'; break;
401 case '%': p[n++] = 'z'; p[n++] = 'v'; break;
402 default: s--; p[n++] = 'z'; p[n++] = 'x';
403 p[n++] = toHex[(int)(*s)/16];
404 p[n++] = toHex[(int)(*s)%16];
415 Text textOf ( Cell c )
429 fprintf(stderr, "\ntextOf: bad tag %d\n",wot );
430 internal("textOf: bad tag");
435 /* --------------------------------------------------------------------------
438 * Currently, the only attributes that we store for each Ext value is the
439 * corresponding Text label. At some later stage, we may decide to cache
440 * types, predicates, etc. here as a space saving gesture. Given that Text
441 * comparison is cheap, and that this is an experimental implementation, we
442 * will use a straightforward linear search to locate Ext values from their
443 * corresponding Text labels; a hashing scheme can be introduced later if
444 * this turns out to be a problem.
445 * ------------------------------------------------------------------------*/
448 Text DEFTABLE(tabExt,NUM_EXT); /* Storage for Ext names */
451 Ext mkExt(t) /* Allocate or find an Ext value */
457 if (extHw-EXTMIN >= NUM_EXT) {
458 ERRMSG(0) "Ext storage space exhausted"
467 /* --------------------------------------------------------------------------
468 * Expandable symbol tables. A template, which is instantiated for the name,
469 * tycon, class, instance and module tables. Also, potentially, TREX Exts.
470 * ------------------------------------------------------------------------*/
472 #ifdef DEBUG_STORAGE_EXTRA
473 static Bool debugStorageExtra = TRUE;
475 static Bool debugStorageExtra = FALSE;
479 #define EXPANDABLE_SYMBOL_TABLE(type_name,struct_name, \
480 proc_name,free_proc_name, \
481 free_list,tab_name,tab_size,err_msg, \
482 TAB_INIT_SIZE,TAB_MAX_SIZE, \
485 struct struct_name* tab_name = NULL; \
487 static type_name free_list = TAB_BASE_ADDR-1; \
489 void free_proc_name ( type_name n ) \
491 assert(TAB_BASE_ADDR <= n); \
492 assert(n < TAB_BASE_ADDR+tab_size); \
493 assert(tab_name[n-TAB_BASE_ADDR].inUse); \
494 tab_name[n-TAB_BASE_ADDR].inUse = FALSE; \
495 if (!debugStorageExtra) { \
496 tab_name[n-TAB_BASE_ADDR].nextFree = free_list; \
501 type_name proc_name ( void ) \
505 struct struct_name* newTab; \
506 struct struct_name* temp; \
508 if (free_list != TAB_BASE_ADDR-1) { \
509 type_name t = free_list; \
510 free_list = tab_name[free_list-TAB_BASE_ADDR].nextFree; \
511 assert (!(tab_name[t-TAB_BASE_ADDR].inUse)); \
512 tab_name[t-TAB_BASE_ADDR].inUse = TRUE; \
516 newSz = (tab_size == 0 ? TAB_INIT_SIZE : 2 * tab_size); \
517 if (newSz > TAB_MAX_SIZE) goto cant_allocate; \
518 newTab = malloc(newSz * sizeof(struct struct_name)); \
519 if (!newTab) goto cant_allocate; \
520 for (i = 0; i < tab_size; i++) \
521 newTab[i] = tab_name[i]; \
522 for (i = tab_size; i < newSz; i++) { \
523 newTab[i].inUse = FALSE; \
524 newTab[i].nextFree = i-1+TAB_BASE_ADDR; \
526 if (0 && debugStorageExtra) \
527 fprintf(stderr, "Expanding " #type_name \
528 "table to size %d\n", newSz ); \
529 newTab[tab_size].nextFree = TAB_BASE_ADDR-1; \
530 free_list = newSz-1+TAB_BASE_ADDR; \
534 if (temp) free(temp); \
544 EXPANDABLE_SYMBOL_TABLE(Name,strName,allocNewName,freeName,
545 nameFL,tabName,tabNameSz,
546 "Name storage space exhausted",
547 NAME_INIT_SIZE,NAME_MAX_SIZE,NAME_BASE_ADDR)
550 EXPANDABLE_SYMBOL_TABLE(Tycon,strTycon,allocNewTycon,freeTycon,
551 tyconFL,tabTycon,tabTyconSz,
552 "Type constructor storage space exhausted",
553 TYCON_INIT_SIZE,TYCON_MAX_SIZE,TYCON_BASE_ADDR)
556 EXPANDABLE_SYMBOL_TABLE(Class,strClass,allocNewClass,freeClass,
557 classFL,tabClass,tabClassSz,
558 "Class storage space exhausted",
559 CCLASS_INIT_SIZE,CCLASS_MAX_SIZE,CCLASS_BASE_ADDR)
562 EXPANDABLE_SYMBOL_TABLE(Inst,strInst,allocNewInst,freeInst,
563 instFL,tabInst,tabInstSz,
564 "Instance storage space exhausted",
565 INST_INIT_SIZE,INST_MAX_SIZE,INST_BASE_ADDR)
568 EXPANDABLE_SYMBOL_TABLE(Module,strModule,allocNewModule,freeModule,
569 moduleFL,tabModule,tabModuleSz,
570 "Module storage space exhausted",
571 MODULE_INIT_SIZE,MODULE_MAX_SIZE,MODULE_BASE_ADDR)
574 struct strName* generate_name_ref ( Cell nm )
577 nm -= NAME_BASE_ADDR;
578 assert(tabName[nm].inUse);
579 assert(isModule(tabName[nm].mod));
580 return & tabName[nm];
582 struct strTycon* generate_tycon_ref ( Cell tc )
584 assert(isTycon(tc) || isTuple(tc));
585 tc -= TYCON_BASE_ADDR;
586 assert(tabTycon[tc].inUse);
587 assert(isModule(tabTycon[tc].mod));
588 return & tabTycon[tc];
590 struct strClass* generate_cclass_ref ( Cell cl )
593 cl -= CCLASS_BASE_ADDR;
594 assert(tabClass[cl].inUse);
595 assert(isModule(tabClass[cl].mod));
596 return & tabClass[cl];
598 struct strInst* generate_inst_ref ( Cell in )
601 in -= INST_BASE_ADDR;
602 assert(tabInst[in].inUse);
603 assert(isModule(tabInst[in].mod));
604 return & tabInst[in];
606 struct strModule* generate_module_ref ( Cell mo )
608 assert(isModule(mo));
609 mo -= MODULE_BASE_ADDR;
610 assert(tabModule[mo].inUse);
611 return & tabModule[mo];
616 /* --------------------------------------------------------------------------
619 * A Tycon represents a user defined type constructor. Tycons are indexed
620 * by Text values ... a very simple hash function is used to improve lookup
621 * times. Tycon entries with the same hash code are chained together, with
622 * the most recent entry at the front of the list.
623 * ------------------------------------------------------------------------*/
625 #define TYCONHSZ 256 /* Size of Tycon hash table*/
626 static Tycon tyconHash[TYCONHSZ]; /* Hash table storage */
628 static int tHash(Text x)
631 assert(isText(x) || inventedText(x));
640 static int RC_T ( int x )
642 assert (x >= 0 && x < TYCONHSZ);
646 Tycon newTycon ( Text t ) /* add new tycon to tycon table */
649 Tycon tc = allocNewTycon();
651 [tc-TYCON_BASE_ADDR].tuple = -1;
653 [tc-TYCON_BASE_ADDR].mod = currentModule;
654 tycon(tc).text = t; /* clear new tycon record */
655 tycon(tc).kind = NIL;
656 tycon(tc).defn = NIL;
657 tycon(tc).what = NIL;
658 tycon(tc).conToTag = NIL;
659 tycon(tc).tagToCon = NIL;
660 tycon(tc).itbl = NULL;
662 module(currentModule).tycons = cons(tc,module(currentModule).tycons);
663 tycon(tc).nextTyconHash = tyconHash[RC_T(h)];
664 tyconHash[RC_T(h)] = tc;
668 Tycon findTycon(t) /* locate Tycon in tycon table */
670 Tycon tc = tyconHash[RC_T(tHash(t))];
671 assert(isTycon(tc) || isTuple(tc) || isNull(tc));
672 while (nonNull(tc) && tycon(tc).text!=t)
673 tc = tycon(tc).nextTyconHash;
677 Tycon addTycon(tc) /* Insert Tycon in tycon table - if no clash is caused */
680 assert(isTycon(tc) || isTuple(tc));
681 oldtc = findTycon(tycon(tc).text);
684 module(currentModule).tycons=cons(tc,module(currentModule).tycons);
690 static Void local hashTycon(tc) /* Insert Tycon into hash table */
694 assert(isTycon(tc) || isTuple(tc));
695 {int i; for (i = 0; i < TYCONHSZ; i++)
696 assert (tyconHash[i] == 0
697 || isTycon(tyconHash[i])
698 || isTuple(tyconHash[i]));
702 tycon(tc).nextTyconHash = tyconHash[RC_T(h)];
703 tyconHash[RC_T(h)] = tc;
706 Tycon findQualTycon(id) /*locate (possibly qualified) Tycon in tycon table */
708 if (!isPair(id)) internal("findQualTycon");
712 return findTycon(textOf(id));
714 Text t = qtextOf(id);
715 Module m = findQualifier(qmodOf(id));
717 if (isNull(m)) return NIL;
718 for(es=module(m).exports; nonNull(es); es=tl(es)) {
720 if (isPair(e) && isTycon(fst(e)) && tycon(fst(e)).text==t)
725 default : internal("findQualTycon2");
727 return NIL; /* NOTREACHED */
730 Tycon addPrimTycon(t,kind,ar,what,defn) /* add new primitive type constr */
736 Tycon tc = newTycon(t);
738 tycon(tc).kind = kind;
739 tycon(tc).what = what;
740 tycon(tc).defn = defn;
741 tycon(tc).arity = ar;
745 static List local insertTycon(tc,ts) /* insert tycon tc into sorted list*/
750 String s = textToStr(tycon(tc).text);
752 while (nonNull(curr) && strCompare(s,textToStr(tycon(hd(curr)).text))>=0) {
753 if (hd(curr)==tc) /* just in case we get duplicates! */
759 tl(prev) = cons(tc,curr);
763 return cons(tc,curr);
766 List addTyconsMatching(pat,ts) /* Add tycons matching pattern pat */
767 String pat; /* to list of Tycons ts */
768 List ts; { /* Null pattern matches every tycon*/
769 Tycon tc; /* (Tycons with NIL kind excluded) */
770 for (tc = TYCON_BASE_ADDR;
771 tc < TYCON_BASE_ADDR+tabTyconSz; ++tc)
772 if (tabTycon[tc-TYCON_BASE_ADDR].inUse)
773 if (!pat || stringMatch(pat,textToStr(tycon(tc).text)))
774 if (nonNull(tycon(tc).kind))
775 ts = insertTycon(tc,ts);
779 Text ghcTupleText_n ( Int n )
784 if (n < 0 || n >= 100) internal("ghcTupleText_n");
785 if (n == 1) internal("ghcTupleText_n==1");
787 for (i = 1; i <= n-1; i++) buf[x++] = ',';
790 return findText(buf);
793 Text ghcTupleText(tup)
796 assert(isTuple(tup));
798 return ghcTupleText_n ( tupleOf(tup) );
802 Tycon mkTuple ( Int n )
806 internal("mkTuple: request for tuple of unsupported size");
807 for (i = TYCON_BASE_ADDR;
808 i < TYCON_BASE_ADDR+tabTyconSz; i++)
809 if (tabTycon[i-TYCON_BASE_ADDR].inUse)
810 if (tycon(i).tuple == n) return i;
811 internal("mkTuple: request for non-existent tuple");
815 /* --------------------------------------------------------------------------
818 * A Name represents a top level binding of a value to an identifier.
819 * Such values may be a constructor function, a member function in a
820 * class, a user-defined or primitive value/function.
822 * Names are indexed by Text values ... a very simple hash functions speeds
823 * access to the table of Names and Name entries with the same hash value
824 * are chained together, with the most recent entry at the front of the
826 * ------------------------------------------------------------------------*/
828 #define NAMEHSZ 256 /* Size of Name hash table */
829 static Name nameHash[NAMEHSZ]; /* Hash table storage */
831 static int nHash(Text x)
833 assert(isText(x) || inventedText(x));
841 assert (x >= 0 && x < NAMEHSZ);
845 void hashSanity ( void )
848 for (i = 0; i < TYCONHSZ; i++) {
851 assert(isTycon(j) || isTuple(j));
852 j = tycon(j).nextTyconHash;
855 for (i = 0; i < NAMEHSZ; i++) {
859 j = name(j).nextNameHash;
864 Name newName ( Text t, Cell parent ) /* Add new name to name table */
867 Name nm = allocNewName();
869 [nm-NAME_BASE_ADDR].mod = currentModule;
870 name(nm).text = t; /* clear new name record */
872 name(nm).syntax = NO_SYNTAX;
873 name(nm).parent = parent;
875 name(nm).number = EXECNAME;
877 name(nm).hasStrict = FALSE;
878 name(nm).stgVar = NIL;
879 name(nm).callconv = NIL;
881 name(nm).primop = NULL;
882 name(nm).itbl = NULL;
883 module(currentModule).names = cons(nm,module(currentModule).names);
884 name(nm).nextNameHash = nameHash[RC_N(h)];
885 nameHash[RC_N(h)] = nm;
889 Name findName(t) /* Locate name in name table */
891 Name n = nameHash[RC_N(nHash(t))];
892 assert(isText(t) || isInventedVar(t) || isInventedDictVar(t));
893 assert(isName(n) || isNull(n));
894 while (nonNull(n) && name(n).text!=t)
895 n = name(n).nextNameHash;
899 Name addName(nm) /* Insert Name in name table - if */
900 Name nm; { /* no clash is caused */
903 oldnm = findName(name(nm).text);
906 module(currentModule).names=cons(nm,module(currentModule).names);
912 static Void local hashName(nm) /* Insert Name into hash table */
919 name(nm).nextNameHash = nameHash[RC_N(h)];
920 nameHash[RC_N(h)] = nm;
923 Name findQualName(id) /* Locate (possibly qualified) name*/
924 Cell id; { /* in name table */
926 internal("findQualName");
932 return findName(textOf(id));
934 Text t = qtextOf(id);
935 Module m = findQualifier(qmodOf(id));
937 if (isNull(m)) return NIL;
938 for(es=module(m).exports; nonNull(es); es=tl(es)) {
940 if (isName(e) && name(e).text==t)
942 else if (isPair(e) && DOTDOT==snd(e)) {
943 List subentities = NIL;
946 && (tycon(c).what==DATATYPE || tycon(c).what==NEWTYPE))
947 subentities = tycon(c).defn;
949 subentities = cclass(c).members;
950 for(; nonNull(subentities); subentities=tl(subentities)) {
951 if (!isName(hd(subentities)))
952 internal("findQualName3");
953 if (name(hd(subentities)).text == t)
954 return hd(subentities);
960 default : internal("findQualName2");
962 return 0; /* NOTREACHED */
966 Name nameFromStgVar ( StgVar v )
969 for (n = NAME_BASE_ADDR;
970 n < NAME_BASE_ADDR+tabNameSz; n++)
971 if (tabName[n-NAME_BASE_ADDR].inUse)
972 if (name(n).stgVar == v) return n;
976 void* getHugs_AsmObject_for ( char* s )
979 Text t = findText(s);
981 for (n = NAME_BASE_ADDR;
982 n < NAME_BASE_ADDR+tabNameSz; n++)
983 if (tabName[n-NAME_BASE_ADDR].inUse)
984 if (name(n).text == t) break;
985 if (n == NAME_BASE_ADDR+tabNameSz) {
986 fprintf ( stderr, "can't find `%s' in ...\n", s );
987 internal("getHugs_AsmObject_for(1)");
990 if (!isStgVar(v) || !isPtr(stgVarInfo(v)))
991 internal("getHugs_AsmObject_for(2)");
992 return ptrOf(stgVarInfo(v));
995 /* --------------------------------------------------------------------------
996 * Primitive functions:
997 * ------------------------------------------------------------------------*/
999 Module findFakeModule ( Text t )
1001 Module m = findModule(t);
1003 if (!module(m).fake) internal("findFakeModule");
1006 module(m).fake = TRUE;
1012 Name addWiredInBoxingTycon
1013 ( String modNm, String typeNm, String constrNm,
1014 Int rep, Kind kind )
1018 Text modT = findText(modNm);
1019 Text typeT = findText(typeNm);
1020 Text conT = findText(constrNm);
1021 Module m = findFakeModule(modT);
1024 n = newName(conT,NIL);
1026 name(n).number = cfunNo(0);
1028 name(n).primop = (void*)rep;
1030 t = newTycon(typeT);
1031 tycon(t).what = DATATYPE;
1032 tycon(t).kind = kind;
1037 Tycon addTupleTycon ( Int n )
1045 for (i = TYCON_BASE_ADDR;
1046 i < TYCON_BASE_ADDR+tabTyconSz; i++)
1047 if (tabTycon[i-TYCON_BASE_ADDR].inUse)
1048 if (tycon(i).tuple == n) return i;
1051 m = findFakeModule(findText(n==0 ? "PrelBase" : "PrelTup")); else
1052 m = findModule(findText("PrelPrim"));
1056 for (i = 0; i < n; i++) k = ap(STAR,k);
1057 t = newTycon(ghcTupleText_n(n));
1060 tycon(t).what = DATATYPE;
1063 /* maybe we want to do this for all n ? */
1064 nm = newName(ghcTupleText_n(n), t);
1065 name(nm).type = t; /* ummm ... for n > 0 */
1072 Tycon addWiredInEnumTycon ( String modNm, String typeNm,
1073 List /*of Text*/ constrs )
1077 Text modT = findText(modNm);
1078 Text typeT = findText(typeNm);
1079 Module m = findFakeModule(modT);
1082 t = newTycon(typeT);
1083 tycon(t).kind = STAR;
1084 tycon(t).what = DATATYPE;
1086 constrs = reverse(constrs);
1087 i = length(constrs);
1088 for (; nonNull(constrs); constrs=tl(constrs),i--) {
1089 Text conT = hd(constrs);
1090 Name con = newName(conT,t);
1091 name(con).number = cfunNo(i);
1093 name(con).parent = t;
1094 tycon(t).defn = cons(con, tycon(t).defn);
1100 Name addPrimCfunREP(t,arity,no,rep) /* add primitive constructor func */
1101 Text t; /* sets rep, not type */
1104 Int rep; { /* Really AsmRep */
1105 Name n = newName(t,NIL);
1106 name(n).arity = arity;
1107 name(n).number = cfunNo(no);
1109 name(n).primop = (void*)rep;
1114 Name addPrimCfun(t,arity,no,type) /* add primitive constructor func */
1119 Name n = newName(t,NIL);
1120 name(n).arity = arity;
1121 name(n).number = cfunNo(no);
1122 name(n).type = type;
1127 Int sfunPos(s,c) /* Find position of field with */
1128 Name s; /* selector s in constructor c. */
1132 for (; nonNull(cns); cns=tl(cns))
1133 if (fst(hd(cns))==c)
1134 return intOf(snd(hd(cns)));
1135 internal("sfunPos");
1136 return 0;/* NOTREACHED */
1139 static List local insertName(nm,ns) /* insert name nm into sorted list */
1144 String s = textToStr(name(nm).text);
1146 while (nonNull(curr) && strCompare(s,textToStr(name(hd(curr)).text))>=0) {
1147 if (hd(curr)==nm) /* just in case we get duplicates! */
1152 if (nonNull(prev)) {
1153 tl(prev) = cons(nm,curr);
1157 return cons(nm,curr);
1160 List addNamesMatching(pat,ns) /* Add names matching pattern pat */
1161 String pat; /* to list of names ns */
1162 List ns; { /* Null pattern matches every name */
1163 Name nm; /* (Names with NIL type, or hidden */
1164 /* or invented names are excluded) */
1166 for (nm = NAME_BASE_ADDR;
1167 nm < NAME_BASE_ADDR+tabNameSz; ++nm)
1168 if (tabName[nm-NAME_BASE_ADDR].inUse) {
1169 if (!inventedText(name(nm).text) && nonNull(name(nm).type)) {
1170 String str = textToStr(name(nm).text);
1171 if (str[0]!='_' && (!pat || stringMatch(pat,str)))
1172 ns = insertName(nm,ns);
1177 List mns = module(currentModule).names;
1178 for(; nonNull(mns); mns=tl(mns)) {
1180 if (!inventedText(name(nm).text)) {
1181 String str = textToStr(name(nm).text);
1182 if (str[0]!='_' && (!pat || stringMatch(pat,str)))
1183 ns = insertName(nm,ns);
1190 /* --------------------------------------------------------------------------
1191 * A simple string matching routine
1192 * `*' matches any sequence of zero or more characters
1193 * `?' matches any single character exactly
1194 * `@str' matches the string str exactly (ignoring any special chars)
1195 * `\c' matches the character c only (ignoring special chars)
1196 * c matches the character c only
1197 * ------------------------------------------------------------------------*/
1199 static Void local patternError(s) /* report error in pattern */
1201 ERRMSG(0) "%s in pattern", s
1205 static Bool local stringMatch(pat,str) /* match string against pattern */
1211 case '\0' : return (*str=='\0');
1214 if (stringMatch(pat+1,str))
1219 case '?' : if (*str++=='\0')
1224 case '[' : { Bool found = FALSE;
1225 while (*++pat!='\0' && *pat!=']')
1226 if (!found && ( pat[0] == *str ||
1235 patternError("missing `]'");
1243 case '\\' : if (*++pat == '\0')
1244 patternError("extra trailing `\\'");
1246 default : if (*pat++ != *str++)
1252 /* --------------------------------------------------------------------------
1253 * Storage of type classes, instances etc...:
1254 * ------------------------------------------------------------------------*/
1256 static List classes; /* list of classes in current scope */
1258 Class newClass ( Text t ) /* add new class to class table */
1260 Class cl = allocNewClass();
1262 [cl-CCLASS_BASE_ADDR].mod = currentModule;
1263 cclass(cl).text = t;
1264 cclass(cl).arity = 0;
1265 cclass(cl).kinds = NIL;
1266 cclass(cl).head = NIL;
1267 cclass(cl).fds = NIL;
1268 cclass(cl).xfds = NIL;
1269 cclass(cl).dcon = NIL;
1270 cclass(cl).supers = NIL;
1271 cclass(cl).dsels = NIL;
1272 cclass(cl).members = NIL;
1273 cclass(cl).defaults = NIL;
1274 cclass(cl).instances = NIL;
1275 classes = cons(cl,classes);
1276 module(currentModule).classes
1277 = cons(cl,module(currentModule).classes);
1281 Class findClass(t) /* look for named class in table */
1285 for (cs=classes; nonNull(cs); cs=tl(cs)) {
1287 if (cclass(cl).text==t)
1293 Class addClass(c) /* Insert Class in class list */
1294 Class c; { /* - if no clash caused */
1296 assert(whatIs(c)==CLASS);
1297 oldc = findClass(cclass(c).text);
1299 classes=cons(c,classes);
1300 module(currentModule).classes=cons(c,module(currentModule).classes);
1307 Class findQualClass(c) /* Look for (possibly qualified) */
1308 Cell c; { /* class in class list */
1309 if (!isQualIdent(c)) {
1310 return findClass(textOf(c));
1312 Text t = qtextOf(c);
1313 Module m = findQualifier(qmodOf(c));
1317 for (es=module(m).exports; nonNull(es); es=tl(es)) {
1319 if (isPair(e) && isClass(fst(e)) && cclass(fst(e)).text==t)
1326 Inst newInst() { /* Add new instance to table */
1327 Inst in = allocNewInst();
1329 [in-INST_BASE_ADDR].mod = currentModule;
1330 inst(in).kinds = NIL;
1331 inst(in).head = NIL;
1332 inst(in).specifics = NIL;
1333 inst(in).implements = NIL;
1334 inst(in).builder = NIL;
1339 extern Void printInst ( Inst));
1343 Class cl = inst(in).c;
1344 Printf("%s-", textToStr(cclass(cl).text));
1345 printType(stdout,inst(in).t);
1347 #endif /* DEBUG_DICTS */
1349 Inst findFirstInst(tc) /* look for 1st instance involving */
1350 Tycon tc; { /* the type constructor tc */
1351 return findNextInst(tc,INST_BASE_ADDR-1);
1354 Inst findNextInst(tc,in) /* look for next instance involving*/
1355 Tycon tc; /* the type constructor tc */
1356 Inst in; { /* starting after instance in */
1358 while (++in < INST_BASE_ADDR+tabInstSz) {
1359 if (!tabInst[in-INST_BASE_ADDR].inUse) continue;
1360 assert(isModule(inst(in).mod));
1362 for (; isAp(pi); pi=fun(pi))
1363 if (typeInvolves(arg(pi),tc))
1369 static Bool local typeInvolves(ty,tc) /* Test to see if type ty involves */
1370 Type ty; /* type constructor/tuple tc. */
1373 || (isAp(ty) && (typeInvolves(fun(ty),tc)
1374 || typeInvolves(arg(ty),tc)));
1378 /* Needed by finishGHCInstance to find classes, before the
1379 export list has been built -- so we can't use
1382 Class findQualClassWithoutConsultingExportList ( QualId q )
1392 t_class = textOf(q);
1395 t_class = qtextOf(q);
1398 for (cl = CCLASS_BASE_ADDR;
1399 cl < CCLASS_BASE_ADDR+tabClassSz; cl++) {
1400 if (tabClass[cl-CCLASS_BASE_ADDR].inUse)
1401 if (cclass(cl).text == t_class) {
1402 /* Class name is ok, but is this the right module? */
1403 if (isNull(t_mod) /* no module name specified */
1405 && t_mod == module(cclass(cl).mod).text)
1413 /* Same deal, except for Tycons. */
1414 Tycon findQualTyconWithoutConsultingExportList ( QualId q )
1424 t_tycon = textOf(q);
1427 t_tycon = qtextOf(q);
1430 for (tc = TYCON_BASE_ADDR;
1431 tc < TYCON_BASE_ADDR+tabTyconSz; tc++) {
1432 if (tabTycon[tc-TYCON_BASE_ADDR].inUse)
1433 if (tycon(tc).text == t_tycon) {
1434 /* Tycon name is ok, but is this the right module? */
1435 if (isNull(t_mod) /* no module name specified */
1437 && t_mod == module(tycon(tc).mod).text)
1445 /* Same deal, except for Names. */
1446 Name findQualNameWithoutConsultingExportList ( QualId q )
1452 assert(isQVar(q) || isQCon(q));
1454 if (isCon(q) || isVar(q)) {
1459 t_name = qtextOf(q);
1462 for (nm = NAME_BASE_ADDR;
1463 nm < NAME_BASE_ADDR+tabNameSz; nm++) {
1464 if (tabName[nm-NAME_BASE_ADDR].inUse)
1465 if (name(nm).text == t_name) {
1466 /* Name is ok, but is this the right module? */
1467 if (isNull(t_mod) /* no module name specified */
1469 && t_mod == module(name(nm).mod).text)
1478 Tycon findTyconInAnyModule ( Text t )
1481 for (tc = TYCON_BASE_ADDR;
1482 tc < TYCON_BASE_ADDR+tabTyconSz; tc++)
1483 if (tabTycon[tc-TYCON_BASE_ADDR].inUse)
1484 if (tycon(tc).text == t) return tc;
1488 Class findClassInAnyModule ( Text t )
1491 for (cc = CCLASS_BASE_ADDR;
1492 cc < CCLASS_BASE_ADDR+tabClassSz; cc++)
1493 if (tabClass[cc-CCLASS_BASE_ADDR].inUse)
1494 if (cclass(cc).text == t) return cc;
1498 Name findNameInAnyModule ( Text t )
1501 for (nm = NAME_BASE_ADDR;
1502 nm < NAME_BASE_ADDR+tabNameSz; nm++)
1503 if (tabName[nm-NAME_BASE_ADDR].inUse)
1504 if (name(nm).text == t) return nm;
1509 /* returns List of QualId */
1510 List getAllKnownTyconsAndClasses ( void )
1515 for (tc = TYCON_BASE_ADDR;
1516 tc < TYCON_BASE_ADDR+tabTyconSz; tc++) {
1517 if (tabTycon[tc-TYCON_BASE_ADDR].inUse) {
1518 /* almost certainly undue paranoia about duplicate avoidance */
1519 QualId q = mkQCon( module(tycon(tc).mod).text, tycon(tc).text );
1520 if (!qualidIsMember(q,xs))
1521 xs = cons ( q, xs );
1524 for (nw = CCLASS_BASE_ADDR;
1525 nw < CCLASS_BASE_ADDR+tabClassSz; nw++) {
1526 if (tabClass[nw-CCLASS_BASE_ADDR].inUse) {
1527 QualId q = mkQCon( module(cclass(nw).mod).text, cclass(nw).text );
1528 if (!qualidIsMember(q,xs))
1529 xs = cons ( q, xs );
1535 Int numQualifiers ( Type t )
1537 if (isPolyType(t)) t = monotypeOf(t);
1539 return length ( fst(snd(t)) ); else
1544 /* Purely for debugging. */
1545 void locateSymbolByName ( Text t )
1548 for (i = NAME_BASE_ADDR;
1549 i < NAME_BASE_ADDR+tabNameSz; i++)
1550 if (tabName[i-NAME_BASE_ADDR].inUse && name(i).text == t)
1551 fprintf ( stderr, "name(%d)\n", i-NAME_BASE_ADDR);
1552 for (i = TYCON_BASE_ADDR;
1553 i < TYCON_BASE_ADDR+tabTyconSz; i++)
1554 if (tabTycon[i-TYCON_BASE_ADDR].inUse && tycon(i).text == t)
1555 fprintf ( stderr, "tycon(%d)\n", i-TYCON_BASE_ADDR);
1556 for (i = CCLASS_BASE_ADDR;
1557 i < CCLASS_BASE_ADDR+tabClassSz; i++)
1558 if (tabClass[i-CCLASS_BASE_ADDR].inUse && cclass(i).text == t)
1559 fprintf ( stderr, "class(%d)\n", i-CCLASS_BASE_ADDR);
1562 /* --------------------------------------------------------------------------
1565 * Various parts of the system use a stack of cells. Most of the stack
1566 * operations are defined as macros, expanded inline.
1567 * ------------------------------------------------------------------------*/
1569 Cell cellStack[NUM_STACK]; /* Storage for cells on stack */
1570 StackPtr sp; /* stack pointer */
1572 Void hugsStackOverflow() { /* Report stack overflow */
1573 ERRMSG(0) "Control stack overflow"
1578 /* --------------------------------------------------------------------------
1581 * A Module represents a user defined module.
1583 * Note: there are now two lookup mechanisms in the system:
1585 * 1) The exports from a module are stored in a big list.
1586 * We resolve qualified names, and import lists by linearly scanning
1587 * through this list.
1589 * 2) Unqualified imports and local definitions for the current module
1590 * are stored in hash tables (tyconHash and nameHash) or linear lists
1593 * ------------------------------------------------------------------------*/
1595 Module currentModule; /* Module currently being processed*/
1597 Bool isValidModule(m) /* is m a legitimate module id? */
1602 Module newModule ( Text t ) /* add new module to module table */
1604 Module mod = allocNewModule();
1605 module(mod).text = t; /* clear new module record */
1607 module(mod).tycons = NIL;
1608 module(mod).names = NIL;
1609 module(mod).classes = NIL;
1610 module(mod).exports = NIL;
1611 module(mod).qualImports = NIL;
1612 module(mod).fake = FALSE;
1614 module(mod).tree = NIL;
1615 module(mod).completed = FALSE;
1616 module(mod).lastStamp = 0; /* ???? */
1618 module(mod).mode = NIL;
1619 module(mod).srcExt = findText("");
1620 module(mod).uses = NIL;
1622 module(mod).objName = findText("");
1623 module(mod).objSize = 0;
1625 module(mod).object = NULL;
1626 module(mod).objectExtras = NULL;
1627 module(mod).objectExtraNames = NIL;
1631 void nukeModule ( Module m )
1636 assert(isModule(m));
1637 /*fprintf(stderr, "NUKEMODULE `%s'\n", textToStr(module(m).text)); */
1638 oc = module(m).object;
1644 oc = module(m).objectExtras;
1651 for (i = NAME_BASE_ADDR; i < NAME_BASE_ADDR+tabNameSz; i++)
1652 if (tabName[i-NAME_BASE_ADDR].inUse && name(i).mod == m) {
1653 if (name(i).itbl) free(name(i).itbl);
1654 name(i).itbl = NULL;
1658 for (i = TYCON_BASE_ADDR; i < TYCON_BASE_ADDR+tabTyconSz; i++)
1659 if (tabTycon[i-TYCON_BASE_ADDR].inUse && tycon(i).mod == m) {
1660 if (tycon(i).itbl) free(tycon(i).itbl);
1661 tycon(i).itbl = NULL;
1665 for (i = CCLASS_BASE_ADDR; i < CCLASS_BASE_ADDR+tabClassSz; i++)
1666 if (tabClass[i-CCLASS_BASE_ADDR].inUse) {
1667 if (cclass(i).mod == m) {
1670 List /* Inst */ ins;
1671 List /* Inst */ ins2 = NIL;
1672 for (ins = cclass(i).instances; nonNull(ins); ins=tl(ins))
1673 if (inst(hd(ins)).mod != m)
1674 ins2 = cons(hd(ins),ins2);
1675 cclass(i).instances = ins2;
1680 for (i = INST_BASE_ADDR; i < INST_BASE_ADDR+tabInstSz; i++)
1681 if (tabInst[i-INST_BASE_ADDR].inUse && inst(i).mod == m)
1685 //for (i = 0; i < TYCONHSZ; i++) tyconHash[i] = 0;
1686 //for (i = 0; i < NAMEHSZ; i++) nameHash[i] = 0;
1691 void ppModules ( void )
1694 fflush(stderr); fflush(stdout);
1695 printf ( "begin MODULES\n" );
1696 for (i = MODULE_BASE_ADDR+tabModuleSz-1;
1697 i >= MODULE_BASE_ADDR; i--)
1698 if (tabModule[i-MODULE_BASE_ADDR].inUse)
1699 printf ( " %2d: %16s\n",
1700 i-MODULE_BASE_ADDR, textToStr(module(i).text)
1702 printf ( "end MODULES\n" );
1703 fflush(stderr); fflush(stdout);
1707 Module findModule(t) /* locate Module in module table */
1710 for(m = MODULE_BASE_ADDR;
1711 m < MODULE_BASE_ADDR+tabModuleSz; ++m) {
1712 if (tabModule[m-MODULE_BASE_ADDR].inUse)
1713 if (module(m).text==t)
1719 Module findModid(c) /* Find module by name or filename */
1721 switch (whatIs(c)) {
1722 case STRCELL : internal("findModid-STRCELL unimp");
1723 case CONIDCELL : return findModule(textOf(c));
1724 default : internal("findModid");
1726 return NIL;/*NOTUSED*/
1729 static local Module findQualifier(t) /* locate Module in import list */
1732 for (ms=module(currentModule).qualImports; nonNull(ms); ms=tl(ms)) {
1733 if (textOf(fst(hd(ms)))==t)
1736 if (module(currentModule).text==t)
1737 return currentModule;
1741 Void setCurrModule(m) /* set lookup tables for current module */
1744 assert(isModule(m));
1745 /* fprintf(stderr, "SET CURR MODULE %s %d\n", textToStr(module(m).text),m); */
1747 for (t = module(m).names; nonNull(t); t=tl(t))
1748 assert(isName(hd(t)));
1749 for (t = module(m).tycons; nonNull(t); t=tl(t))
1750 assert(isTycon(hd(t)) || isTuple(hd(t)));
1751 for (t = module(m).classes; nonNull(t); t=tl(t))
1752 assert(isClass(hd(t)));
1755 currentModule = m; /* This is the only assignment to currentModule */
1756 for (i=0; i<TYCONHSZ; ++i)
1757 tyconHash[RC_T(i)] = NIL;
1758 mapProc(hashTycon,module(m).tycons);
1759 for (i=0; i<NAMEHSZ; ++i)
1760 nameHash[RC_N(i)] = NIL;
1761 mapProc(hashName,module(m).names);
1762 classes = module(m).classes;
1766 Name jrsFindQualName ( Text mn, Text sn )
1771 for (m = MODULE_BASE_ADDR;
1772 m < MODULE_BASE_ADDR+tabModuleSz; m++)
1773 if (tabModule[m-MODULE_BASE_ADDR].inUse
1774 && module(m).text == mn) break;
1776 if (m == MODULE_BASE_ADDR+tabModuleSz) return NIL;
1778 for (ns = module(m).names; nonNull(ns); ns=tl(ns))
1779 if (name(hd(ns)).text == sn) return hd(ns);
1785 char* nameFromOPtr ( void* p )
1789 for (m = MODULE_BASE_ADDR;
1790 m < MODULE_BASE_ADDR+tabModuleSz; m++) {
1791 if (tabModule[m-MODULE_BASE_ADDR].inUse && module(m).object) {
1792 char* nm = ocLookupAddr ( module(m).object, p );
1797 /* A kludge to assist Win32 debugging; not actually necessary. */
1798 { char* nm = nameFromStaticOPtr(p);
1806 void* lookupOTabName ( Module m, char* sym )
1808 assert(isModule(m));
1809 if (module(m).object)
1810 return ocLookupSym ( module(m).object, sym );
1815 void* lookupOExtraTabName ( char* sym )
1819 for (m = MODULE_BASE_ADDR;
1820 m < MODULE_BASE_ADDR+tabModuleSz; m++) {
1821 if (tabModule[m-MODULE_BASE_ADDR].inUse)
1822 for (oc = module(m).objectExtras; oc; oc=oc->next) {
1823 void* ad = ocLookupSym ( oc, sym );
1831 OSectionKind lookupSection ( void* ad )
1838 for (m = MODULE_BASE_ADDR;
1839 m < MODULE_BASE_ADDR+tabModuleSz; m++) {
1840 if (tabModule[m-MODULE_BASE_ADDR].inUse) {
1841 if (module(m).object) {
1842 sect = ocLookupSection ( module(m).object, ad );
1843 if (sect != HUGS_SECTIONKIND_NOINFOAVAIL)
1846 for (oc = module(m).objectExtras; oc; oc=oc->next) {
1847 sect = ocLookupSection ( oc, ad );
1848 if (sect != HUGS_SECTIONKIND_NOINFOAVAIL)
1853 return HUGS_SECTIONKIND_OTHER;
1857 /* --------------------------------------------------------------------------
1860 * Provides a garbage collectable heap for storage of expressions etc.
1862 * Now incorporates a flat resource: A two-space collected extension of
1863 * the heap that provides storage for contiguous arrays of Cell storage,
1864 * cooperating with the garbage collection mechanisms for the main heap.
1865 * ------------------------------------------------------------------------*/
1867 Int heapSize = DEFAULTHEAP; /* number of cells in heap */
1868 Heap heapFst; /* array of fst component of pairs */
1869 Heap heapSnd; /* array of snd component of pairs */
1872 Bool consGC = TRUE; /* Set to FALSE to turn off gc from*/
1873 /* C stack; use with extreme care! */
1875 Int numGcs; /* number of garbage collections */
1876 Int cellsRecovered; /* number of cells recovered */
1878 static Cell freeList; /* free list of unused cells */
1879 static Cell lsave, rsave; /* save components of pair */
1883 static Int markCount, stackRoots;
1885 #define initStackRoots() stackRoots = 0
1886 #define recordStackRoot() stackRoots++
1899 #define start() markCount = 0
1900 #define end(thing,rs) \
1902 Printf("GC: %-18s: %4d cells, %4d roots.\n", thing, markCount, rs); \
1905 #define recordMark() markCount++
1907 #else /* !GC_STATISTICS */
1912 #define initStackRoots()
1913 #define recordStackRoot()
1916 #define end(thing,root)
1917 #define recordMark()
1919 #endif /* !GC_STATISTICS */
1921 Cell pair(l,r) /* Allocate pair (l, r) from */
1922 Cell l, r; { /* heap, garbage collecting first */
1923 Cell c = freeList; /* if necessary ... */
1934 freeList = snd(freeList);
1942 static Int marksSize;
1944 void mark ( Cell root )
1947 Cell mstack[NUM_MSTACK];
1951 mstack[++msp] = root;
1954 if (msp > msp_max) msp_max = msp;
1956 if (!isGenPair(c)) continue;
1957 if (fst(c)==FREECELL) continue;
1959 register int place = placeInSet(c);
1960 register int mask = maskInSet(c);
1961 if (!(marks[place]&mask)) {
1962 marks[place] |= mask;
1963 if (msp >= NUM_MSTACK-5) {
1965 "hugs: fatal stack overflow during GC. "
1966 "Increase NUM_MSTACK.\n" );
1969 mstack[++msp] = fst(c);
1970 mstack[++msp] = snd(c);
1974 // fprintf(stderr, "%d ",msp_max);
1978 Void garbageCollect() { /* Run garbage collector ... */
1979 /* disable break checking */
1984 jmp_buf regs; /* save registers on stack */
1985 HugsBreakAction oldBrk
1986 = setBreakAction ( HugsIgnoreBreak );
1992 for (i=0; i<marksSize; ++i) /* initialise mark set to empty */
1995 everybody(MARK); /* Mark all components of system */
1997 gcScanning(); /* scan mark set */
2004 for (i=1; i<=heapSize; i++) {
2005 if ((marks[place] & mask) == 0) {
2012 if (++j == bitsPerWord) {
2019 gcRecovered(recovered);
2020 setBreakAction ( oldBrk );
2024 #if defined(DEBUG_STORAGE) || defined(DEBUG_STORAGE_EXTRA)
2025 /* fprintf(stderr, "\n--- GC recovered %d\n",recovered ); */
2028 /* can only return if freeList is nonempty on return. */
2029 if (recovered<minRecovery || isNull(freeList)) {
2030 ERRMSG(0) "Garbage collection fails to reclaim sufficient space"
2033 cellsRecovered = recovered;
2036 /* --------------------------------------------------------------------------
2037 * Code for saving last expression entered:
2039 * This is a little tricky because some text values (e.g. strings or variable
2040 * names) may not be defined or have the same value when the expression is
2041 * recalled. These text values are therefore saved in the top portion of
2043 * ------------------------------------------------------------------------*/
2045 static Cell lastExprSaved; /* last expression to be saved */
2047 Void setLastExpr(e) /* save expression for later recall*/
2049 lastExprSaved = NIL; /* in case attempt to save fails */
2050 savedText = TEXT_SIZE;
2051 lastExprSaved = lowLevelLastIn(e);
2054 static Cell local lowLevelLastIn(c) /* Duplicate expression tree (i.e. */
2055 Cell c; { /* acyclic graph) for later recall */
2056 if (isPair(c)) { /* Duplicating any text strings */
2057 if (isTagNonPtr(fst(c))) /* in case these are lost at some */
2058 switch (fst(c)) { /* point before the expr is reused */
2064 case STRCELL : return pair(fst(c),saveText(textOf(c)));
2065 default : return pair(fst(c),snd(c));
2068 return pair(lowLevelLastIn(fst(c)),lowLevelLastIn(snd(c)));
2072 return pair(EXTCOPY,saveText(extText(c)));
2078 Cell getLastExpr() { /* recover previously saved expr */
2079 return lowLevelLastOut(lastExprSaved);
2082 static Cell local lowLevelLastOut(c) /* As with lowLevelLastIn() above */
2083 Cell c; { /* except that Cells refering to */
2084 if (isPair(c)) { /* Text values are restored to */
2085 if (isTagNonPtr(fst(c))) /* appropriate values */
2092 case STRCELL : return pair(fst(c),
2093 findText(text+intValOf(c)));
2095 case EXTCOPY : return mkExt(findText(text+intValOf(c)));
2097 default : return pair(fst(c),snd(c));
2100 return pair(lowLevelLastOut(fst(c)),lowLevelLastOut(snd(c)));
2106 /* --------------------------------------------------------------------------
2107 * Miscellaneous operations on heap cells:
2108 * ------------------------------------------------------------------------*/
2110 Cell whatIs ( register Cell c )
2113 register Cell fstc = fst(c);
2114 return isTag(fstc) ? fstc : AP;
2116 if (isOffset(c)) return OFFSET;
2117 if (isChar(c)) return CHARCELL;
2118 if (isInt(c)) return INTCELL;
2119 if (isName(c)) return NAME;
2120 if (isTycon(c)) return TYCON;
2121 if (isTuple(c)) return TUPLE;
2122 if (isClass(c)) return CLASS;
2123 if (isInst(c)) return INSTANCE;
2124 if (isModule(c)) return MODULE;
2125 if (isText(c)) return TEXTCELL;
2126 if (isInventedVar(c)) return INVAR;
2127 if (isInventedDictVar(c)) return INDVAR;
2128 if (isSpec(c)) return c;
2129 if (isNull(c)) return c;
2130 fprintf ( stderr, "whatIs: unknown %d\n", c );
2136 Cell whatIs(c) /* identify type of cell */
2139 register Cell fstc = fst(c);
2140 return isTag(fstc) ? fstc : AP;
2142 if (c<OFFMIN) return c;
2144 if (isExt(c)) return EXT;
2146 if (c>=INTMIN) return INTCELL;
2148 if (c>=NAMEMIN){if (c>=CLASSMIN) {if (c>=CHARMIN) return CHARCELL;
2150 else if (c>=INSTMIN) return INSTANCE;
2152 else if (c>=MODMIN) {if (c>=TYCMIN) return isTuple(c) ? TUPLE : TYCON;
2153 else return MODULE;}
2154 else if (c>=OFFMIN) return OFFSET;
2156 else return (c>=EXTMIN) ?
2164 register Cell fstc = fst(c);
2165 return isTag(fstc) ? fstc : AP;
2167 if (c>=INTMIN) return INTCELL;
2168 if (c>=CHARMIN) return CHARCELL;
2169 if (c>=CLASSMIN) return CLASS;
2170 if (c>=INSTMIN) return INSTANCE;
2171 if (c>=NAMEMIN) return NAME;
2172 if (c>=TYCMIN) return TYCON;
2173 if (c>=MODMIN) return MODULE;
2174 if (c>=OFFMIN) return OFFSET;
2176 if (c>=EXTMIN) return EXT;
2178 if (c>=TUPMIN) return TUPLE;
2184 /* A very, very simple printer.
2185 * Output is uglier than from printExp - but the printer is more
2186 * robust and can be used on any data structure irrespective of
2189 Void print ( Cell c, Int depth )
2194 else if (isNull(c)) {
2197 else if (isTagPtr(c)) {
2198 Printf("TagP(%d)", c);
2200 else if (isTagNonPtr(c)) {
2201 Printf("TagNP(%d)", c);
2203 else if (isSpec(c)) {
2204 Printf("TagS(%d)", c);
2206 else if (isText(c)) {
2207 Printf("text(%d)=\"%s\"",c-TEXT_BASE_ADDR,textToStr(c));
2209 else if (isInventedVar(c)) {
2210 Printf("invented(%d)", c-INVAR_BASE_ADDR);
2212 else if (isInventedDictVar(c)) {
2213 Printf("inventedDict(%d)",c-INDVAR_BASE_ADDR);
2216 Int tag = whatIs(c);
2220 print(fst(c), depth-1);
2222 print(snd(c), depth-1);
2226 Printf("free(%d)", c);
2229 Printf("int(%d)", intOf(c));
2232 Printf("bignum(%s)", bignumToString(c));
2235 Printf("char('%c')", charOf(c));
2238 Printf("ptr(%p)",ptrOf(c));
2241 Printf("class(%d)", c-CCLASS_BASE_ADDR);
2242 Printf("=\"%s\"", textToStr(cclass(c).text));
2245 Printf("instance(%d)", c - INST_BASE_ADDR);
2248 Printf("name(%d)", c-NAME_BASE_ADDR);
2249 Printf("=\"%s\"", textToStr(name(c).text));
2252 Printf("tycon(%d)", c-TYCON_BASE_ADDR);
2253 Printf("=\"%s\"", textToStr(tycon(c).text));
2256 Printf("module(%d)", c - MODULE_BASE_ADDR);
2257 Printf("=\"%s\"", textToStr(module(c).text));
2260 Printf("Offset %d", offsetOf(c));
2263 Printf("%s", textToStr(ghcTupleText(c)));
2267 print(snd(c),depth-1);
2271 print(snd(c),depth-1);
2275 if (isPair(snd(c)) && isInt(fst(snd(c)))) {
2276 Printf("%d ", intOf(fst(snd(c))));
2277 print(snd(snd(c)),depth-1);
2279 print(snd(c),depth-1);
2293 Printf("{dict %d}",textOf(c));
2299 Printf("{id %s}",textToStr(textOf(c)));
2303 Printf("{ip %s}",textToStr(textOf(c)));
2306 Printf("?%s",textToStr(textOf(c)));
2310 Printf("{qid %s.%s}",textToStr(qmodOf(c)),textToStr(qtextOf(c)));
2314 print(fst(snd(c)),depth-1);
2316 print(snd(snd(c)),depth-1);
2321 print(snd(c),depth-1);
2326 print(snd(c),depth-1);
2331 print(fst(snd(c)),depth-1);
2333 print(snd(snd(c)),depth-1);
2338 print(fst(snd(c)),depth-1);
2340 print(snd(snd(c)),depth-1);
2344 Printf("FromQual(");
2345 print(fst(snd(c)),depth-1);
2347 print(snd(snd(c)),depth-1);
2351 Printf("StgVar%d=",-c);
2352 print(snd(c), depth-1);
2356 print(fst(snd(c)),depth-1);
2358 print(snd(snd(c)),depth-1);
2363 print(fst(snd(c)),depth-1);
2365 print(snd(snd(c)),depth-1);
2370 print(fst(snd(c)),depth-1);
2372 print(snd(snd(c)),depth-1);
2376 Printf("PrimCase(");
2377 print(fst(snd(c)),depth-1);
2379 print(snd(snd(c)),depth-1);
2384 print(snd(c),depth-1);
2388 Printf("(UNBOXEDTUP,");
2389 print(snd(c),depth-1);
2394 print(zfst(c),depth-1);
2396 print(zsnd(c),depth-1);
2400 Printf("<ZTriple ");
2401 print(zfst3(c),depth-1);
2403 print(zsnd3(c),depth-1);
2405 print(zthd3(c),depth-1);
2410 print(snd(c),depth-1);
2414 if (isTagNonPtr(tag)) {
2415 Printf("(TagNP=%d,%d)", c, tag);
2416 } else if (isTagPtr(tag)) {
2417 Printf("(TagP=%d,",tag);
2418 print(snd(c), depth-1);
2421 } else if (c == tag) {
2422 Printf("Tag(%d)", c);
2424 Printf("Tag(%d)=%d", c, tag);
2433 Bool isVar(c) /* is cell a VARIDCELL/VAROPCELL ? */
2434 Cell c; { /* also recognises DICTVAR cells */
2436 (fst(c)==VARIDCELL || fst(c)==VAROPCELL || fst(c)==DICTVAR);
2439 Bool isCon(c) /* is cell a CONIDCELL/CONOPCELL ? */
2441 return isPair(c) && (fst(c)==CONIDCELL || fst(c)==CONOPCELL);
2444 Bool isQVar(c) /* is cell a [un]qualified varop/id? */
2446 if (!isPair(c)) return FALSE;
2449 case VAROPCELL : return TRUE;
2451 case QUALIDENT : return isVar(snd(snd(c)));
2453 default : return FALSE;
2457 Bool isQCon(c) /*is cell a [un]qualified conop/id? */
2459 if (!isPair(c)) return FALSE;
2462 case CONOPCELL : return TRUE;
2464 case QUALIDENT : return isCon(snd(snd(c)));
2466 default : return FALSE;
2470 Bool isQualIdent(c) /* is cell a qualified identifier? */
2472 return isPair(c) && (fst(c)==QUALIDENT);
2475 Bool eqQualIdent ( QualId c1, QualId c2 )
2477 assert(isQualIdent(c1));
2478 if (!isQualIdent(c2)) {
2479 assert(isQualIdent(c2));
2481 return qmodOf(c1)==qmodOf(c2) &&
2482 qtextOf(c1)==qtextOf(c2);
2485 Bool isIdent(c) /* is cell an identifier? */
2487 if (!isPair(c)) return FALSE;
2492 case CONOPCELL : return TRUE;
2494 case QUALIDENT : return TRUE;
2496 default : return FALSE;
2500 Bool isInt(c) /* cell holds integer value? */
2502 return isSmall(c) || (isPair(c) && fst(c)==INTCELL);
2505 Int intOf(c) /* find integer value of cell? */
2508 return isPair(c) ? (Int)(snd(c)) : (Int)(c-SMALL_INT_ZERO);
2511 Cell mkInt(n) /* make cell representing integer */
2513 return (SMALL_INT_MIN <= SMALL_INT_ZERO+n &&
2514 SMALL_INT_ZERO+n <= SMALL_INT_MAX)
2519 #if SIZEOF_VOID_P == SIZEOF_INT
2521 typedef union {Int i; Ptr p;} IntOrPtr;
2528 return pair(PTRCELL,x.i);
2535 assert(fst(c) == PTRCELL);
2545 return pair(CPTRCELL,x.i);
2552 assert(fst(c) == CPTRCELL);
2557 #elif SIZEOF_VOID_P == 2*SIZEOF_INT
2559 typedef union {struct {Int i1; Int i2;} i; Ptr p;} IntOrPtr;
2566 return pair(PTRCELL,pair(mkInt(x.i.i1),mkInt(x.i.i2)));
2573 assert(fst(c) == PTRCELL);
2574 x.i.i1 = intOf(fst(snd(c)));
2575 x.i.i2 = intOf(snd(snd(c)));
2584 return pair(CPTRCELL,pair(mkInt(x.i.i1),mkInt(x.i.i2)));
2591 assert(fst(c) == CPTRCELL);
2592 x.i.i1 = intOf(fst(snd(c)));
2593 x.i.i2 = intOf(snd(snd(c)));
2599 #error "Can't implement mkPtr/ptrOf on this architecture."
2604 String stringNegate( s )
2612 strcpy(&t[1],s); /* ToDo: use strncpy instead */
2617 /* --------------------------------------------------------------------------
2619 * ------------------------------------------------------------------------*/
2621 Int length(xs) /* calculate length of list xs */
2624 for (; nonNull(xs); ++n)
2629 List appendOnto(xs,ys) /* Destructively prepend xs onto */
2630 List xs, ys; { /* ys by modifying xs ... */
2635 while (nonNull(tl(zs)))
2642 List dupOnto(xs,ys) /* non-destructively prepend xs backwards onto ys */
2645 for (; nonNull(xs); xs=tl(xs))
2646 ys = cons(hd(xs),ys);
2650 List dupListOnto(xs,ys) /* Duplicate spine of list xs onto ys */
2653 return revOnto(dupOnto(xs,NIL),ys);
2656 List dupList(xs) /* Duplicate spine of list xs */
2659 for (; nonNull(xs); xs=tl(xs))
2660 ys = cons(hd(xs),ys);
2664 List revOnto(xs,ys) /* Destructively reverse elements of*/
2665 List xs, ys; { /* list xs onto list ys... */
2668 while (nonNull(xs)) {
2677 QualId qualidIsMember ( QualId q, List xs )
2679 for (; nonNull(xs); xs=tl(xs)) {
2680 if (eqQualIdent(q, hd(xs)))
2686 Cell varIsMember(t,xs) /* Test if variable is a member of */
2687 Text t; /* given list of variables */
2689 for (; nonNull(xs); xs=tl(xs))
2690 if (t==textOf(hd(xs)))
2695 Name nameIsMember(t,ns) /* Test if name with text t is a */
2696 Text t; /* member of list of names xs */
2698 for (; nonNull(ns); ns=tl(ns))
2699 if (t==name(hd(ns)).text)
2704 Cell intIsMember(n,xs) /* Test if integer n is member of */
2705 Int n; /* given list of integers */
2707 for (; nonNull(xs); xs=tl(xs))
2708 if (n==intOf(hd(xs)))
2713 Cell cellIsMember(x,xs) /* Test for membership of specific */
2714 Cell x; /* cell x in list xs */
2716 for (; nonNull(xs); xs=tl(xs))
2722 Cell cellAssoc(c,xs) /* Lookup cell in association list */
2725 for (; nonNull(xs); xs=tl(xs))
2731 Cell cellRevAssoc(c,xs) /* Lookup cell in range of */
2732 Cell c; /* association lists */
2734 for (; nonNull(xs); xs=tl(xs))
2740 List replicate(n,x) /* create list of n copies of x */
2749 List diffList(from,take) /* list difference: from\take */
2750 List from, take; { /* result contains all elements of */
2751 List result = NIL; /* `from' not appearing in `take' */
2753 while (nonNull(from)) {
2754 List next = tl(from);
2755 if (!cellIsMember(hd(from),take)) {
2764 List deleteCell(xs, y) /* copy xs deleting pointers to y */
2768 for(;nonNull(xs);xs=tl(xs)) {
2771 result=cons(x,result);
2777 List take(n,xs) /* destructively truncate list to */
2778 Int n; /* specified length */
2784 while (1<n-- && nonNull(xs))
2791 List splitAt(n,xs) /* drop n things from front of list*/
2800 Cell nth(n,xs) /* extract n'th element of list */
2803 for(; n>0 && nonNull(xs); --n, xs=tl(xs)) {
2810 List removeCell(x,xs) /* destructively remove cell from */
2815 return tl(xs); /* element at front of list */
2819 for (; nonNull(curr); prev=curr, curr=tl(prev))
2821 tl(prev) = tl(curr);
2822 return xs; /* element in middle of list */
2826 return xs; /* here if element not found */
2829 List nubList(xs) /* nuke dups in list */
2830 List xs; { /* non destructive */
2832 for (; nonNull(xs); xs=tl(xs))
2833 if (isNull(cellIsMember(hd(xs),outs)))
2834 outs = cons(hd(xs),outs);
2840 /* --------------------------------------------------------------------------
2841 * Tagged tuples (experimental)
2842 * ------------------------------------------------------------------------*/
2844 static void z_tag_check ( Cell x, int tag, char* caller )
2848 sprintf(buf,"z_tag_check(%s): null\n", caller);
2851 if (whatIs(x) != tag) {
2853 "z_tag_check(%s): tag was %d, expected %d\n",
2854 caller, whatIs(x), tag );
2859 Cell zpair ( Cell x1, Cell x2 )
2860 { return ap(ZTUP2,ap(x1,x2)); }
2861 Cell zfst ( Cell zpair )
2862 { z_tag_check(zpair,ZTUP2,"zfst"); return fst( snd(zpair) ); }
2863 Cell zsnd ( Cell zpair )
2864 { z_tag_check(zpair,ZTUP2,"zsnd"); return snd( snd(zpair) ); }
2866 Cell ztriple ( Cell x1, Cell x2, Cell x3 )
2867 { return ap(ZTUP3,ap(x1,ap(x2,x3))); }
2868 Cell zfst3 ( Cell zpair )
2869 { z_tag_check(zpair,ZTUP3,"zfst3"); return fst( snd(zpair) ); }
2870 Cell zsnd3 ( Cell zpair )
2871 { z_tag_check(zpair,ZTUP3,"zsnd3"); return fst(snd( snd(zpair) )); }
2872 Cell zthd3 ( Cell zpair )
2873 { z_tag_check(zpair,ZTUP3,"zthd3"); return snd(snd( snd(zpair) )); }
2875 Cell z4ble ( Cell x1, Cell x2, Cell x3, Cell x4 )
2876 { return ap(ZTUP4,ap(x1,ap(x2,ap(x3,x4)))); }
2877 Cell zsel14 ( Cell zpair )
2878 { z_tag_check(zpair,ZTUP4,"zsel14"); return fst( snd(zpair) ); }
2879 Cell zsel24 ( Cell zpair )
2880 { z_tag_check(zpair,ZTUP4,"zsel24"); return fst(snd( snd(zpair) )); }
2881 Cell zsel34 ( Cell zpair )
2882 { z_tag_check(zpair,ZTUP4,"zsel34"); return fst(snd(snd( snd(zpair) ))); }
2883 Cell zsel44 ( Cell zpair )
2884 { z_tag_check(zpair,ZTUP4,"zsel44"); return snd(snd(snd( snd(zpair) ))); }
2886 Cell z5ble ( Cell x1, Cell x2, Cell x3, Cell x4, Cell x5 )
2887 { return ap(ZTUP5,ap(x1,ap(x2,ap(x3,ap(x4,x5))))); }
2888 Cell zsel15 ( Cell zpair )
2889 { z_tag_check(zpair,ZTUP5,"zsel15"); return fst( snd(zpair) ); }
2890 Cell zsel25 ( Cell zpair )
2891 { z_tag_check(zpair,ZTUP5,"zsel25"); return fst(snd( snd(zpair) )); }
2892 Cell zsel35 ( Cell zpair )
2893 { z_tag_check(zpair,ZTUP5,"zsel35"); return fst(snd(snd( snd(zpair) ))); }
2894 Cell zsel45 ( Cell zpair )
2895 { z_tag_check(zpair,ZTUP5,"zsel45"); return fst(snd(snd(snd( snd(zpair) )))); }
2896 Cell zsel55 ( Cell zpair )
2897 { z_tag_check(zpair,ZTUP5,"zsel55"); return snd(snd(snd(snd( snd(zpair) )))); }
2900 Cell unap ( int tag, Cell c )
2903 if (whatIs(c) != tag) {
2904 sprintf(buf, "unap: specified %d, actual %d\n",
2911 /* --------------------------------------------------------------------------
2912 * Operations on applications:
2913 * ------------------------------------------------------------------------*/
2915 Int argCount; /* number of args in application */
2917 Cell getHead(e) /* get head cell of application */
2918 Cell e; { /* set number of args in argCount */
2919 for (argCount=0; isAp(e); e=fun(e))
2924 List getArgs(e) /* get list of arguments in function*/
2925 Cell e; { /* application: */
2926 List as; /* getArgs(f e1 .. en) = [e1,..,en] */
2928 for (as=NIL; isAp(e); e=fun(e))
2929 as = cons(arg(e),as);
2933 Cell nthArg(n,e) /* return nth arg in application */
2934 Int n; /* of function to m args (m>=n) */
2935 Cell e; { /* nthArg n (f x0 x1 ... xm) = xn */
2936 for (n=numArgs(e)-n-1; n>0; n--)
2941 Int numArgs(e) /* find number of arguments to expr */
2944 for (n=0; isAp(e); e=fun(e))
2949 Cell applyToArgs(f,args) /* destructively apply list of args */
2950 Cell f; /* to function f */
2952 while (nonNull(args)) {
2953 Cell temp = tl(args);
2954 tl(args) = hd(args);
2962 /* --------------------------------------------------------------------------
2964 * ------------------------------------------------------------------------*/
2966 static String maybeModuleStr ( Module m )
2968 if (isModule(m)) return textToStr(module(m).text); else return "??";
2971 static String maybeNameStr ( Name n )
2973 if (isName(n)) return textToStr(name(n).text); else return "??";
2976 static String maybeTyconStr ( Tycon t )
2978 if (isTycon(t)) return textToStr(tycon(t).text); else return "??";
2981 static String maybeClassStr ( Class c )
2983 if (isClass(c)) return textToStr(cclass(c).text); else return "??";
2986 static String maybeText ( Text t )
2988 if (isNull(t)) return "(nil)";
2989 return textToStr(t);
2992 static void print100 ( Int x )
2994 print ( x, 100); printf("\n");
2997 void dumpTycon ( Int t )
2999 if (isTycon(TYCON_BASE_ADDR+t) && !isTycon(t)) t += TYCON_BASE_ADDR;
3001 printf ( "dumpTycon %d: not a tycon\n", t);
3005 printf ( " text: %s\n", textToStr(tycon(t).text) );
3006 printf ( " line: %d\n", tycon(t).line );
3007 printf ( " mod: %s\n", maybeModuleStr(tycon(t).mod));
3008 printf ( " tuple: %d\n", tycon(t).tuple);
3009 printf ( " arity: %d\n", tycon(t).arity);
3010 printf ( " kind: "); print100(tycon(t).kind);
3011 printf ( " what: %d\n", tycon(t).what);
3012 printf ( " defn: "); print100(tycon(t).defn);
3013 printf ( " cToT: %d %s\n", tycon(t).conToTag,
3014 maybeNameStr(tycon(t).conToTag));
3015 printf ( " tToC: %d %s\n", tycon(t).tagToCon,
3016 maybeNameStr(tycon(t).tagToCon));
3017 printf ( " itbl: %p\n", tycon(t).itbl);
3018 printf ( " nextTH: %d %s\n", tycon(t).nextTyconHash,
3019 maybeTyconStr(tycon(t).nextTyconHash));
3023 void dumpName ( Int n )
3025 if (isName(NAME_BASE_ADDR+n) && !isName(n)) n += NAME_BASE_ADDR;
3027 printf ( "dumpName %d: not a name\n", n);
3031 printf ( " text: %s\n", textToStr(name(n).text) );
3032 printf ( " line: %d\n", name(n).line );
3033 printf ( " mod: %s\n", maybeModuleStr(name(n).mod));
3034 printf ( " syntax: %d\n", name(n).syntax );
3035 printf ( " parent: %d\n", name(n).parent );
3036 printf ( " arity: %d\n", name(n).arity );
3037 printf ( " number: %d\n", name(n).number );
3038 printf ( " type: "); print100(name(n).type);
3039 printf ( " defn: %d\n", name(n).defn );
3040 printf ( " stgVar: "); print100(name(n).stgVar);
3041 printf ( " cconv: %d\n", name(n).callconv );
3042 printf ( " primop: %p\n", name(n).primop );
3043 printf ( " itbl: %p\n", name(n).itbl );
3044 printf ( " nextNH: %d\n", name(n).nextNameHash );
3049 void dumpClass ( Int c )
3051 if (isClass(CCLASS_BASE_ADDR+c) && !isClass(c)) c += CCLASS_BASE_ADDR;
3053 printf ( "dumpClass %d: not a class\n", c);
3057 printf ( " text: %s\n", textToStr(cclass(c).text) );
3058 printf ( " line: %d\n", cclass(c).line );
3059 printf ( " mod: %s\n", maybeModuleStr(cclass(c).mod));
3060 printf ( " arity: %d\n", cclass(c).arity );
3061 printf ( " level: %d\n", cclass(c).level );
3062 printf ( " kinds: "); print100( cclass(c).kinds );
3063 printf ( " fds: %d\n", cclass(c).fds );
3064 printf ( " xfds: %d\n", cclass(c).xfds );
3065 printf ( " head: "); print100( cclass(c).head );
3066 printf ( " dcon: "); print100( cclass(c).dcon );
3067 printf ( " supers: "); print100( cclass(c).supers );
3068 printf ( " #supers: %d\n", cclass(c).numSupers );
3069 printf ( " dsels: "); print100( cclass(c).dsels );
3070 printf ( " members: "); print100( cclass(c).members );
3071 printf ( "#members: %d\n", cclass(c).numMembers );
3072 printf ( "defaults: "); print100( cclass(c).defaults );
3073 printf ( " insts: "); print100( cclass(c).instances );
3078 void dumpInst ( Int i )
3080 if (isInst(INST_BASE_ADDR+i) && !isInst(i)) i += INST_BASE_ADDR;
3082 printf ( "dumpInst %d: not an instance\n", i);
3086 printf ( " class: %s\n", maybeClassStr(inst(i).c) );
3087 printf ( " line: %d\n", inst(i).line );
3088 printf ( " mod: %s\n", maybeModuleStr(inst(i).mod));
3089 printf ( " kinds: "); print100( inst(i).kinds );
3090 printf ( " head: "); print100( inst(i).head );
3091 printf ( " specs: "); print100( inst(i).specifics );
3092 printf ( " #specs: %d\n", inst(i).numSpecifics );
3093 printf ( " impls: "); print100( inst(i).implements );
3094 printf ( " builder: %s\n", maybeNameStr( inst(i).builder ) );
3099 /* --------------------------------------------------------------------------
3101 * ------------------------------------------------------------------------*/
3108 case POSTPREL: break;
3110 case RESET : clearStack();
3112 /* the next 2 statements are particularly important
3113 * if you are using GLOBALfst or GLOBALsnd since the
3114 * corresponding registers may be reset to their
3115 * uninitialised initial values by a longjump.
3117 heapTopFst = heapFst + heapSize;
3118 heapTopSnd = heapSnd + heapSize;
3122 if (isNull(lastExprSaved))
3123 savedText = TEXT_SIZE;
3128 for (i = NAME_BASE_ADDR;
3129 i < NAME_BASE_ADDR+tabNameSz; ++i) {
3130 if (tabName[i-NAME_BASE_ADDR].inUse) {
3131 mark(name(i).parent);
3134 mark(name(i).stgVar);
3137 end("Names", nameHw-NAMEMIN);
3140 for (i = MODULE_BASE_ADDR;
3141 i < MODULE_BASE_ADDR+tabModuleSz; ++i) {
3142 if (tabModule[i-MODULE_BASE_ADDR].inUse) {
3143 mark(module(i).tycons);
3144 mark(module(i).names);
3145 mark(module(i).classes);
3146 mark(module(i).exports);
3147 mark(module(i).qualImports);
3148 mark(module(i).tree);
3149 mark(module(i).uses);
3150 mark(module(i).objectExtraNames);
3155 mark(targetModules);
3156 end("Modules", moduleHw-MODMIN);
3159 for (i = TYCON_BASE_ADDR;
3160 i < TYCON_BASE_ADDR+tabTyconSz; ++i) {
3161 if (tabTycon[i-TYCON_BASE_ADDR].inUse) {
3162 mark(tycon(i).kind);
3163 mark(tycon(i).what);
3164 mark(tycon(i).defn);
3167 end("Type constructors", tyconHw-TYCMIN);
3170 for (i = CCLASS_BASE_ADDR;
3171 i < CCLASS_BASE_ADDR+tabClassSz; ++i) {
3172 if (tabClass[i-CCLASS_BASE_ADDR].inUse) {
3173 mark(cclass(i).kinds);
3174 mark(cclass(i).fds);
3175 mark(cclass(i).xfds);
3176 mark(cclass(i).head);
3177 mark(cclass(i).supers);
3178 mark(cclass(i).dsels);
3179 mark(cclass(i).members);
3180 mark(cclass(i).defaults);
3181 mark(cclass(i).instances);
3185 end("Classes", classHw-CLASSMIN);
3188 for (i = INST_BASE_ADDR;
3189 i < INST_BASE_ADDR+tabInstSz; ++i) {
3190 if (tabInst[i-INST_BASE_ADDR].inUse) {
3191 mark(inst(i).kinds);
3193 mark(inst(i).specifics);
3194 mark(inst(i).implements);
3197 end("Instances", instHw-INSTMIN);
3200 for (i=0; i<=sp; ++i)
3205 mark(lastExprSaved);
3208 end("Last expression", 3);
3213 end("C stack", stackRoots);
3218 case PREPREL : heapFst = heapAlloc(heapSize);
3219 heapSnd = heapAlloc(heapSize);
3221 if (heapFst==(Heap)0 || heapSnd==(Heap)0) {
3222 ERRMSG(0) "Cannot allocate heap storage (%d cells)",
3227 heapTopFst = heapFst + heapSize;
3228 heapTopSnd = heapSnd + heapSize;
3229 for (i=1; i<heapSize; ++i) {
3233 snd(-heapSize) = NIL;
3240 marksSize = bitArraySize(heapSize);
3241 if ((marks=(Int *)calloc(marksSize, sizeof(Int)))==0) {
3242 ERRMSG(0) "Unable to allocate gc markspace"
3249 nextNewText = INVAR_BASE_ADDR;
3250 nextNewDText = INDVAR_BASE_ADDR;
3251 lastExprSaved = NIL;
3252 savedText = TEXT_SIZE;
3254 for (i=0; i<TEXTHSZ; ++i) textHash[i][0] = NOTEXT;
3255 for (i=0; i<TYCONHSZ; ++i) tyconHash[RC_T(i)] = NIL;
3256 for (i=0; i<NAMEHSZ; ++i) nameHash[RC_N(i)] = NIL;
3262 /*-------------------------------------------------------------------------*/