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