1: /*	@(#)tree.c	2.3	SCCS id keyword	*/
   2: /* Copyright (c) 1979 Regents of the University of California */
   3: #
   4: /*
   5:  * pi - Pascal interpreter code translator
   6:  *
   7:  * Charles Haley, Bill Joy UCB
   8:  * Version 1.1 February 1978
   9:  *
  10:  *
  11:  * pxp - Pascal execution profiler
  12:  *
  13:  * Bill Joy UCB
  14:  * Version 1.1 February 1978
  15:  */
  16: 
  17: #include "whoami"
  18: #include "0.h"
  19: 
  20: /*
  21:  * TREE SPACE DECLARATIONS
  22:  */
  23: struct tr {
  24:     int *tr_low;
  25:     int *tr_high;
  26: } ttab[MAXTREE], *tract;
  27: 
  28: /*
  29:  * The variable space is the
  30:  * absolute base of the tree segments.
  31:  * (exactly the same as ttab[0].tr_low)
  32:  * Spacep is maintained to point at the
  33:  * beginning of the next tree slot to
  34:  * be allocated for use by the grammar.
  35:  * Spacep is used "extern" by the semantic
  36:  * actions in pas.y.
  37:  * The variable tract is maintained to point
  38:  * at the tree segment out of which we are
  39:  * allocating (the active segment).
  40:  */
  41: int *space, *spacep;
  42: 
  43: /*
  44:  * TREENMAX is the maximum width
  45:  * in words that any tree node
  46:  * due to the way in which the parser uses
  47:  * the pointer spacep.
  48:  */
  49: #define TREENMAX    6
  50: 
  51: int trspace[ITREE];
  52: int *space  = trspace;
  53: int *spacep = trspace;
  54: struct  tr *tract   = ttab;
  55: 
  56: /*
  57:  * Inittree allocates the first tree slot
  58:  * and sets up the first segment descriptor.
  59:  * A lot of this work is actually done statically
  60:  * above.
  61:  */
  62: inittree()
  63: {
  64: 
  65:     ttab[0].tr_low = space;
  66:     ttab[0].tr_high = &space[ITREE];
  67: }
  68: 
  69: /*
  70:  * Tree builds the nodes in the
  71:  * parse tree. It is rarely called
  72:  * directly, rather calls are made
  73:  * to tree[12345] which supplies the
  74:  * first argument to save space in
  75:  * the code. Tree also guarantees
  76:  * that spacep points to the beginning
  77:  * of the next slot it will return,
  78:  * a property required by the parser
  79:  * which was always true before we
  80:  * segmented the tree space.
  81:  */
  82: int *tree(cnt, a)
  83:     int cnt;
  84: {
  85:     register int *p, *q;
  86:     register int i;
  87: 
  88:     i = cnt;
  89:     p = spacep;
  90:     q = &a;
  91:     do
  92:         *p++ = *q++;
  93:     while (--i);
  94:     q = spacep;
  95:     spacep = p;
  96:     if (p+TREENMAX >= tract->tr_high)
  97:         /*
  98: 		 * this peek-ahead should
  99: 		 * save a great number of calls
 100: 		 * to tralloc.
 101: 		 */
 102:         tralloc(TREENMAX);
 103:     return (q);
 104: }
 105: 
 106: /*
 107:  * Tralloc preallocates enough
 108:  * space in the tree to allow
 109:  * the grammar to use the variable
 110:  * spacep, as it did before the
 111:  * tree was segmented.
 112:  */
 113: tralloc(howmuch)
 114: {
 115:     register char *cp;
 116:     register i;
 117: 
 118:     if (spacep + howmuch >= tract->tr_high) {
 119:         i = TRINC;
 120:         cp = malloc(i * sizeof ( int ));
 121:         if (cp == 0) {
 122:             yerror("Ran out of memory (tralloc)");
 123:             pexit(DIED);
 124:         }
 125:         spacep = cp;
 126:         tract++;
 127:         if (tract >= &ttab[MAXTREE]) {
 128:             yerror("Ran out of tree tables");
 129:             pexit(DIED);
 130:         }
 131:         tract->tr_low = cp;
 132:         tract->tr_high = tract->tr_low+i;
 133:     }
 134: }
 135: 
 136: extern  int yylacnt;
 137: extern  bottled;
 138: #ifdef PXP
 139: #endif
 140: /*
 141:  * Free up the tree segments
 142:  * at the end of a block.
 143:  * If there is scanner lookahead,
 144:  * i.e. if yylacnt != 0 or there is bottled output, then we
 145:  * cannot free the tree space.
 146:  * This happens only when errors
 147:  * occur and the forward move extends
 148:  * across "units".
 149:  */
 150: trfree()
 151: {
 152: 
 153:     if (yylacnt != 0 || bottled != NIL)
 154:         return;
 155: #ifdef PXP
 156:     if (needtree())
 157:         return;
 158: #endif
 159:     spacep = space;
 160:     while (tract->tr_low > spacep || tract->tr_high <= spacep) {
 161:         free(tract->tr_low);
 162:         tract->tr_low = NIL;
 163:         tract->tr_high = NIL;
 164:         tract--;
 165:         if (tract < ttab)
 166:             panic("ttab");
 167:     }
 168: #ifdef PXP
 169:     packtree();
 170: #endif
 171: }
 172: 
 173: /*
 174:  * Copystr copies a token from
 175:  * the "token" buffer into the
 176:  * tree space.
 177:  */
 178: copystr(token)
 179:     register char *token;
 180: {
 181:     register char *cp;
 182:     register int i;
 183: 
 184:     i = (strlen(token) + sizeof ( int )) & ~( ( sizeof ( int ) ) - 1 );
 185:     tralloc(i / sizeof ( int ));
 186:     strcpy(spacep, token);
 187:     cp = spacep;
 188:     spacep = cp + i;
 189:     tralloc(TREENMAX);
 190:     return (cp);
 191: }

Defined functions

copystr defined in line 178; used 4 times
inittree defined in line 62; used 1 times
tralloc defined in line 113; used 3 times
trfree defined in line 150; used 4 times

Defined variables

space defined in line 52; used 3 times
spacep defined in line 53; used 13 times
tract defined in line 54; used 14 times
trspace defined in line 51; used 2 times
ttab defined in line 26; used 5 times

Defined struct's

tr defined in line 23; used 2 times
  • in line 54(2)

Defined macros

TREENMAX defined in line 49; used 3 times
Last modified: 1981-07-10
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 2090
Valid CSS Valid XHTML 1.0 Strict