xref: /csrg-svn/usr.bin/struct/struct/3.then.c (revision 62294)
148123Sbostic /*-
248123Sbostic  * %sccs.include.proprietary.c%
348123Sbostic  */
448123Sbostic 
510975Srrh #ifndef lint
6*62294Sbostic static char sccsid[] = "@(#)3.then.c	8.1 (Berkeley) 06/06/93";
748123Sbostic #endif /* not lint */
810975Srrh 
910975Srrh #include <stdio.h>
1010975Srrh #include "def.h"
1110975Srrh #include "3.def.h"
1210975Srrh 
1310975Srrh #define BRANCHTYPE(t)	(t == STOPVX || t == RETVX || t == BRKVX || t == NXTVX || t == GOVX)
1410975Srrh #define MAXCHUNK	20
1510975Srrh 		/* if else clause smaller than MAXCHUNK and smaller than then clause,
1610975Srrh 			and there is no reason not to negate the if, negate the if */
1710975Srrh 
getthen(v)1810975Srrh getthen(v)		/* turn IFVX into THEN when appropriate, create else ifs where possible  */
1910975Srrh VERT v;
2010975Srrh 	{
2110975Srrh 	VERT tch, fch;
2210975Srrh 	int tn,fn;
2310975Srrh 	int recvar;
2410975Srrh 
2510975Srrh 	if (NTYPE(v) == IFVX)
2610975Srrh 		{
2710975Srrh 		tch = LCHILD(v,THEN);
2810975Srrh 		fch = LCHILD(v,ELSE);
2910975Srrh 		if (!DEFINED(fch))
3010975Srrh 			mkthen(v);
3110975Srrh 		else if (!DEFINED(tch))
3210975Srrh 			{
3310975Srrh 			negate(v);
3410975Srrh 			mkthen(v);
3510975Srrh 			}
3610975Srrh 		else if (BRANCHTYPE(NTYPE(tch)))
3710975Srrh 			mkthen(v);
3810975Srrh 		else if (BRANCHTYPE(NTYPE(fch)))
3910975Srrh 			{
4010975Srrh 			negate(v);
4110975Srrh 			mkthen(v);
4210975Srrh 			}
4310975Srrh 		else if (NTYPE(fch) != IFVX || DEFINED(RSIB(fch)))	/* not an else if */
4410975Srrh 			if ( NTYPE(tch) == IFVX && !DEFINED(RSIB(tch)))
4510975Srrh 					/* invert into else if */
4610975Srrh 				negate(v);
4710975Srrh 			else
4810975Srrh 				{
4910975Srrh 				/* asoc(v,n) returns number of statements associated with v
5010975Srrh 					if <= n, -1 otherwise */
5110975Srrh 				tn = asoc(tch,MAXCHUNK);
5210975Srrh 				fn = asoc(fch,MAXCHUNK);
5310975Srrh 				if (fn >= 0 && (tn < 0 || fn < tn))
5410975Srrh 					/* else clause smaller */
5510975Srrh 					negate(v);
5610975Srrh 				}
5710975Srrh 		}
5810975Srrh 	RECURSE(getthen,v,recvar);
5910975Srrh 	}
6010975Srrh 
mkthen(v)6110975Srrh mkthen(v)
6210975Srrh VERT v;
6310975Srrh 	{
6410975Srrh 	VERT w,tc;
6510975Srrh 	w = LCHILD(v,ELSE);
6610975Srrh 	tc = LCHILD(v,THEN);
6710975Srrh 	ASSERT(!DEFINED(w) || (DEFINED(tc) && BRANCHTYPE(NTYPE(tc)) ),mkthen);
6810975Srrh 	if (DEFINED(w))
6910975Srrh 		{
7010975Srrh 		insib(v,w);
7110975Srrh 		LCHILD(v,ELSE) = UNDEFINED;
7210975Srrh 		}
7310975Srrh 	ASSERT(IFTHEN(v),mkthen);
7410975Srrh 	}
7510975Srrh 
7610975Srrh 
negate(v)7710975Srrh negate(v)
7810975Srrh VERT v;
7910975Srrh 	{
8010975Srrh 	ASSERT(NTYPE(v) == IFVX,negate);
8110975Srrh 	exchange(&LCHILD(v,THEN), &LCHILD(v,ELSE));
8210975Srrh 	NEG(v) = !NEG(v);
8310975Srrh 	}
84