148123Sbostic /*-
248123Sbostic * %sccs.include.proprietary.c%
348123Sbostic */
448123Sbostic
510967Srrh #ifndef lint
6*62290Sbostic static char sccsid[] = "@(#)2.tree.c 8.1 (Berkeley) 06/06/93";
748123Sbostic #endif /* not lint */
810967Srrh
910967Srrh #include <stdio.h>
1010967Srrh #
1110967Srrh /* use inarc, dom, and head to build tree representing structure of program.
1210967Srrh Each node v has CHILDNUM(v) children denoted by
1310967Srrh LCHILD(v,0), LCHILD(v,1),...
1410967Srrh RSIB((v) is right sibling of v or UNDEFINED;
1510967Srrh RSIB(v) represents code following v at the same level of nesting,
1610967Srrh while LCHILD(v,i) represents code nested within v
1710967Srrh */
1810967Srrh #include "def.h"
1910967Srrh #include "2.def.h"
2010967Srrh
2110967Srrh gettree(inarc,dom,head) /* build tree */
2210967Srrh struct list **inarc;
2310967Srrh VERT *dom, *head;
2410967Srrh {
2510967Srrh VERT v,u,from;
2610967Srrh int i;
2710967Srrh for ( v = 0; v < nodenum; ++v)
2810967Srrh {
2910967Srrh RSIB(v) = UNDEFINED;
3010967Srrh for (i = 0; i < CHILDNUM(v); ++i)
3110967Srrh LCHILD(v,i) = UNDEFINED;
3210967Srrh }
3310967Srrh for (i = accessnum-1; i > 0; --i)
3410967Srrh {
3510967Srrh v = after[i];
3610967Srrh from = oneelt(inarc[v]); /* the unique elt of inarc[v] or UNDEFINED */
3710967Srrh if (DEFINED(from))
3810967Srrh if (NTYPE(from) == IFVX && (head[v] == head[from] || asoc(v,exitsize) != -1) )
3910967Srrh /* place in clause of IFVX if in smallest loop containing it
4010967Srrh or if size of code for v is <= exitsize */
4110967Srrh if (ARC(from,THEN) == v)
4210967Srrh {
4310967Srrh LCHILD(from,THEN) = v;
4410967Srrh continue;
4510967Srrh }
4610967Srrh else
4710967Srrh {
4810967Srrh ASSERT(ARC(from,ELSE) == v,gettree);
4910967Srrh LCHILD(from,ELSE) = v;
5010967Srrh continue;
5110967Srrh }
5210967Srrh else if (NTYPE(v) == ITERVX || NTYPE(from) == ITERVX )
5310967Srrh /* LOOPVX -> ITERVX ->vert always in same loop*/
5410967Srrh {
5510967Srrh LCHILD(from,0) = v;
5610967Srrh continue;
5710967Srrh }
5810967Srrh else if (NTYPE(from) == SWCHVX)
5910967Srrh {
6010967Srrh ASSERT(0 < ARCNUM(v),gettree);
6110967Srrh if (ARC(from,0) == v)
6210967Srrh LCHILD(from,0) = v;
6310967Srrh else
6410967Srrh {
6510967Srrh int j;
6610967Srrh for (j = 1; j < ARCNUM(from); ++j)
6710967Srrh if (ARC(from,j) == v)
6810967Srrh {insib(ARC(from,j-1),v);
6910967Srrh break;
7010967Srrh }
7110967Srrh }
7210967Srrh continue;
7310967Srrh }
7410967Srrh else if (NTYPE(from) == ICASVX && (head[v] == head[from] || asoc(v,exitsize) != -1))
7510967Srrh {
7610967Srrh LCHILD(from,0) = v;
7710967Srrh continue;
7810967Srrh }
7910967Srrh else if (NTYPE(from) == DUMVX && ARC(from,0) == v)
8010967Srrh {
8110967Srrh LCHILD(from,0) = v;
8210967Srrh continue;
8310967Srrh }
8410967Srrh if (loomem(v,head[dom[v]],head))
8510967Srrh /* v is in smallest loop containing dom[v] */
8610967Srrh insib(dom[v],v);
8710967Srrh else
8810967Srrh {
8910967Srrh /* make v follow LOOPVX heading largest loop
9010967Srrh containing DOM[v] but not v */
9110967Srrh ASSERT(DEFINED(head[dom[v]]),gettree);
9210967Srrh for (u = head[dom[v]]; head[u] != head[v]; u = head[u])
9310967Srrh ASSERT(DEFINED(head[u]),gettree);
9410967Srrh ASSERT(NTYPE(u) == ITERVX,gettree);
9510967Srrh insib(FATH(u),v);
9610967Srrh }
9710967Srrh }
9810967Srrh }
9910967Srrh
10010967Srrh
10110967Srrh
10210967Srrh
insib(w,v)10310967Srrh insib(w,v) /* make RSIB(w) = v, and make RSIB(rightmost sib of v) = old RSIB(w) */
10410967Srrh VERT w,v;
10510967Srrh {
10610967Srrh VERT u, temp;
10710967Srrh temp = RSIB(w);
10810967Srrh RSIB(w) = v;
10910967Srrh for (u = v; DEFINED(RSIB(u)); u = RSIB(u))
11010967Srrh ;
11110967Srrh RSIB(u) = temp;
11210967Srrh }
11310967Srrh
11410967Srrh
asoc(v,n)11510967Srrh asoc(v,n) /* return # of nodes associated with v if <= n, -1 otherwise */
11610967Srrh VERT v;
11710967Srrh int n;
11810967Srrh {
11910967Srrh int count,i,temp;
12010967Srrh VERT w;
12110967Srrh count = (NTYPE(v) == STLNVX) ? CODELINES(v) : 1;
12210967Srrh for (i = 0; i < CHILDNUM(v); ++i)
12310967Srrh {
12410967Srrh w = LCHILD(v,i);
12510967Srrh if (!DEFINED(w)) continue;
12610967Srrh temp = asoc(w,n-count);
12710967Srrh if (temp == -1) return(-1);
12810967Srrh count += temp;
12910967Srrh if (count > n) return(-1);
13010967Srrh }
13110967Srrh if (DEFINED(RSIB(v)))
13210967Srrh {
13310967Srrh temp = asoc(RSIB(v),n-count);
13410967Srrh if (temp == -1) return(-1);
13510967Srrh count += temp;
13610967Srrh }
13710967Srrh if (count > n) return(-1);
13810967Srrh else return(count);
13910967Srrh }
140