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