1: #ifndef lint 2: static char sccsid[] = "@(#)1.recog.c 4.1 (Berkeley) 2/11/83"; 3: #endif not lint 4: 5: #include <stdio.h> 6: #include "1.incl.h" 7: #include "def.h" 8: 9: 10: recognize(type, ifflag) /* if ifflag = 1, statement is if()type; otherwise is type */ 11: int type, ifflag; /* do whatever is needed for this statement */ 12: { 13: int *arctype, i, sp; 14: VERT num, num1, nest, loophead; 15: extern long label(); 16: long *arclab; 17: if (nlabs > 3) sp = nlabs; else sp = 3; 18: arctype = (int *)challoc(sizeof(*arctype) * sp); 19: arclab = (long *)challoc(sizeof(*arclab) * sp); 20: for( i=0; i < endbuf; i++) {if (buffer[i] == '~') buffer[i] = ' ';} 21: loophead = nest = innerdo(label(0)); 22: if (DEFINED(nest)) 23: { 24: /* this statement is last line of do loop */ 25: nest = ARC(nest,0); /* nest is ITERVX of the innermost do ending here */ 26: } 27: 28: 29: if (ifflag) 30: { 31: if (type == ungo) 32: { 33: arctype[0] = -2; 34: arclab[0] = label(1); 35: } 36: else 37: arctype[0] = 0; 38: 39: arctype[1] = (nest >= 0) ? nest : -2; 40: arclab[1] = implicit; 41: num1 = makenode(IFVX,TRUE,TRUE,label(0),2,arctype,arclab); 42: PRED(num1) = (int)pred; 43: } 44: 45: arctype[0] = (nest >= 0) ? nest : -2; 46: arclab[0] = implicit; 47: 48: switch(type) 49: { 50: case ungo: 51: if (!ifflag) 52: { 53: connect(label(1),implicit); 54: if (label(0) != implicit) connect(label(1),label(0)); 55: } 56: break; 57: case RETVX: 58: case STOPVX: 59: if (type == RETVX) 60: { 61: if (retvert == UNDEFINED) 62: retvert = makenode(type,FALSE,FALSE,implicit,0,arctype,arclab); 63: num = retvert; 64: } 65: else 66: { 67: if (stopvert == UNDEFINED) 68: stopvert = makenode(type,FALSE,FALSE,implicit,0,arctype,arclab); 69: num = stopvert; 70: } 71: if (!ifflag) 72: { 73: fixvalue(implicit,num); 74: clear(implicit); 75: if (label(0) != implicit) fixvalue(label(0),num); 76: } 77: break; 78: 79: 80: case contst: 81: contin(label(0),loophead); 82: break; 83: 84: case FMTVX: 85: num = makenode(FMTVX,FALSE,TRUE,implicit,0,arctype,arclab); 86: BEGCODE(num) = comchar + 1 - rtnbeg; 87: ONDISK(num) = endline - endcom; 88: if (label(0) != implicit) 89: fixvalue(label(0),num); 90: FMTLST = append(num,FMTLST); 91: break; 92: case STLNVX: 93: if (DEFINED(stflag) && !ifflag && (label(0) == implicit)) 94: { 95: ++CODELINES(stflag); 96: ONDISK(stflag) += endline - begline + 1; 97: } 98: else 99: { 100: num = makenode(STLNVX,!ifflag,!ifflag,label(0),1,arctype,arclab); 101: if (!ifflag) 102: { 103: stflag = num; 104: BEGCODE(num) = comchar + 1 - rtnbeg; 105: ONDISK(num) = endline - endcom; 106: CODELINES(num) = 1; 107: } 108: else 109: { 110: BEGCODE(num) = (int)stcode; 111: ONDISK(num) = FALSE; 112: CODELINES(num) = 1; 113: } 114: } 115: break; 116: 117: case DOVX: 118: if (arctype[0] != -2) 119: { 120: error("illegal do range, ","",""); 121: fprintf(stderr," between lines %d and %d\n",begline, endline); 122: exit(1); 123: } 124: arctype[1] = UNDEFINED; 125: num1 = makenode(DOVX,TRUE,TRUE,label(0),2,arctype,arclab); 126: if (++doptr >= maxdo) 127: { 128: faterr("in parsing:\n","do loops nested deeper than allowed",""); 129: } 130: dostack[doptr] = label(1); 131: doloc[doptr] = num1; /* stack link to node after loop */ 132: INC(num1) = (int)inc; 133: num = makenode(ITERVX,TRUE,FALSE,implicit,1,arctype,arclab); 134: ARC(num1,0) = num; 135: FATH(num) = UNDEFINED; /* number of DOVX can change so leave UNDEFINED until later */ 136: break; 137: case arithif: 138: if (label(1) == label(2) || label(1) == 0L) 139: makeif(1,label(0),concat(pred," > 0"),label(3),label(2)); 140: else if (label(1) == label(3) || label(3) == 0L) 141: makeif(1,label(0),concat(pred," == 0"),label(2),label(1)); 142: else if (label(2) == label(3) || label(2) == 0L) 143: makeif(1,label(0),concat(pred," < 0"),label(1),label(3)); 144: else 145: { 146: makeif(1,label(0),concat(pred," < 0"),label(1),implicit); 147: makeif(1,implicit,concat(pred," == 0"),label(2),label(3)); 148: } 149: break; 150: 151: case IOVX: 152: if (endlab) 153: { 154: arctype[1] = -2; 155: arclab[1] = endlab->labelt; 156: } 157: else 158: arctype[1] = UNDEFINED; 159: if (errlab) 160: { 161: arctype[2] = -2; 162: arclab[2] = errlab->labelt; 163: } 164: else 165: arctype[2] = UNDEFINED; 166: num = makenode(IOVX,!ifflag,!ifflag,label(0),3,arctype,arclab); 167: PRERW(num) = (int)prerw; 168: POSTRW(num) = (int)postrw; 169: if (reflab) 170: addref(reflab->labelt, &FMTREF(num)); 171: else 172: FMTREF(num) = UNDEFINED; 173: break; 174: 175: case COMPVX: 176: if (intcase) 177: { 178: num = compcase(ifflag); 179: break; 180: } 181: case ASGOVX: 182: for (i = 0; i < nlabs - 1; i++) 183: { 184: arctype[i] = -2; 185: arclab[i] = label(nlabs-i-1); 186: } 187: num = makenode(type,!ifflag,!ifflag,label(0),nlabs - 1, arctype, arclab); 188: EXP(num) = (int)exp; 189: break; 190: case ASVX: 191: num = makenode(ASVX,!ifflag,!ifflag,label(0),1,arctype,arclab); 192: EXP(num) = (int)exp; 193: addref(label(1),&LABREF(num)); 194: break; 195: case entry: 196: num = makenode(STLNVX,FALSE,TRUE,label(0),1,arctype,arclab); 197: BEGCODE(num) = comchar + 1 - rtnbeg; 198: ONDISK(num) = endline - endcom; 199: CODELINES(num) = 1; 200: ENTLST = append(num,ENTLST); 201: break; 202: } 203: if (ifflag && type != ungo) 204: { 205: ARC(num1,0) = num; 206: } 207: if (DEFINED(loophead)) nesteddo(label(0), loophead); 208: if (ifflag || DEFINED(loophead) || type != STLNVX) stflag = UNDEFINED; 209: 210: 211: chfree(arctype,sizeof(*arctype) * sp); chfree(arclab,sizeof(*arclab) * sp); 212: if (debug) 213: { 214: fprintf(debfd,"line %d: ", begline); 215: if (ifflag) fprintf(debfd,"if() "); 216: switch(type) 217: {case RETVX: fprintf(debfd,"return"); break; 218: case STOPVX: fprintf(debfd,"stop"); break; 219: case contst: fprintf(debfd,"continue"); break; 220: case ungo: fprintf(debfd,"uncond. goto"); break; 221: case COMPVX: fprintf(debfd,"comp. goto"); break; 222: case ASGOVX: fprintf(debfd,"ass. goto, labs"); break; 223: case ASVX: fprintf(debfd,"label assignment"); break; 224: case STLNVX: fprintf(debfd,"simple statement"); break; 225: case arithif: fprintf(debfd,"arith if"); break; 226: case DOVX: fprintf(debfd,"do "); break; 227: case FMTVX: fprintf(debfd,"format st"); break; 228: case IOVX: fprintf(debfd,"IOVX statement "); break; 229: case entry: fprintf(debfd,"entry statement "); break; 230: } 231: fprintf(debfd,"\n%s\n", buffer); 232: } 233: } 234: 235: 236: 237: makeif(first,labe,test,arc1,arc2) /* construct IFVX with arcs to labels arc1,arc2 */ 238: int first; 239: long labe, arc1,arc2; 240: char *test; 241: { 242: int num, arctype[2]; 243: long arclab[2]; 244: arctype[0] = arctype[1] = -2; 245: arclab[0] = arc1; 246: arclab[1] = arc2; 247: num = makenode(IFVX,first,first,labe,2,arctype,arclab); 248: PRED(num) = (int)test; 249: return(num); 250: } 251: 252: 253: innerdo(labe) /* return number of DOVX associated with labe, or UNDEFINED */ 254: long labe; 255: { 256: if (DEFINED(doptr)) 257: {if (dostack[doptr] == labe) 258: return(doloc[doptr--]); 259: } 260: return(UNDEFINED); 261: } 262: 263: 264: 265: 266: contin(labe,nest) /* handle continue statements */ 267: long labe; 268: int nest; 269: { 270: VERT y; 271: 272: if (!DEFINED(nest)) 273: { /* not nested */ 274: if (labe != implicit) connect(implicit,labe); /* labe pts to next node */ 275: } 276: else 277: { /* nested */ 278: y = ARC(nest,0); 279: fixvalue(labe,y); /* labe pts to ITERVX */ 280: fixvalue(implicit, y); /* implicit links pt to ITERVX */ 281: clear(implicit); 282: } 283: } 284: 285: 286: 287: 288: nesteddo(labe,v) 289: /* if multiple do's end on same label, add arc from inner DOVX 290: to enclosing DOVX; 291: add implicit link out of outermost DOVX with this label */ 292: long labe; 293: int v; 294: { 295: 296: while (DEFINED(doptr) && dostack[doptr] == labe) 297: { 298: ARC(v,1) = ARC(doloc[doptr],0); /*set inner DOVX to point to outer ITERVX */ 299: v = doloc[doptr--]; 300: } 301: addref(implicit, &ARC(v,1)); 302: } 303: 304: 305: 306: compcase(ifflag) /* turn computed goto into case statement */ 307: LOGICAL ifflag; 308: { 309: int *arctype, i, num, d, arct; 310: extern long label(); 311: long *arclab; 312: char *str; 313: arctype = (int *)challoc(sizeof(*arctype) * nlabs); 314: arclab = (long *)challoc (sizeof(*arclab) * nlabs); 315: 316: d = distinct(linelabs->nxtlab,arctype,arclab,nlabs-1); 317: /* puts distinct labels in arclab, count of each in arctype */ 318: arct = -2; 319: for (i = 0; i < d; ++i) 320: arctype[i] = makenode(ICASVX,FALSE,FALSE,implicit,1,&arct,&arclab[i]); 321: num = makenode(SWCHVX,!ifflag,!ifflag,label(0),d,arctype,arclab); 322: EXP(num) = (int)exp; 323: 324: str = (char *)challoc(6*(nlabs-1)); /* 5 digits + , or \0 per label */ 325: for (i = 0; i < d; ++i) /* construct list of values for each label */ 326: EXP(arctype[i]) = stralloc(str,accum(str,linelabs->nxtlab,arclab[i])); 327: chfree(str,6*(nlabs-1)); 328: chfree(arctype,sizeof(*arctype) * nlabs); chfree(arclab,sizeof(*arclab) * nlabs); 329: return(num); 330: } 331: 332: 333: accum(str,vlist,f) /* build string of indices in compnode corr. to label f */ 334: char *str; long f; struct lablist *vlist; 335: { 336: int s,j; struct lablist *p; 337: 338: s = 0; 339: j = 1; 340: for (p = vlist; p ; p = p->nxtlab) /* search for occurrences of f */ 341: { 342: if (p->labelt ==f) 343: { 344: if (s) 345: { 346: str[s] = ','; 347: ++s; 348: } 349: sprintf(&str[s],"%d",j); 350: while (str[s] != '\0') ++s; 351: } 352: ++j; 353: } 354: return(s+1); 355: } 356: 357: 358: distinct(vlist,count,dlist,size) /* make dlist into list of distinct labels in vlist */ 359: struct lablist *vlist; long dlist[]; /*count[] gets count of each label; d distinct labels */ 360: int count[],size; 361: {int d,i; 362: d = 0; 363: for(i = 0; i < size; i++) count[i] = 0; 364: 365: for (;vlist && vlist->labelt != 0L; vlist = vlist ->nxtlab) 366: { 367: for (i = 0; ;i++) 368: { 369: if (i == d) dlist[d++] = vlist->labelt; 370: if (dlist[i] == vlist->labelt) 371: { 372: ++count[i]; break; 373: } 374: } 375: } 376: return(d); 377: }