| 1 | #include <stdio.h> |
| 2 | #include "def.h" |
| 3 | #include "3.def.h" |
| 4 | |
| 5 | #define BRANCHTYPE(t) (t == STOPVX || t == RETVX || t == BRKVX || t == NXTVX || t == GOVX) |
| 6 | #define MAXCHUNK 20 |
| 7 | /* if else clause smaller than MAXCHUNK and smaller than then clause, |
| 8 | and there is no reason not to negate the if, negate the if */ |
| 9 | |
| 10 | getthen(v) /* turn IFVX into THEN when appropriate, create else ifs where possible */ |
| 11 | VERT v; |
| 12 | { |
| 13 | VERT tch, fch; |
| 14 | int tn,fn; |
| 15 | int recvar; |
| 16 | |
| 17 | if (NTYPE(v) == IFVX) |
| 18 | { |
| 19 | tch = LCHILD(v,THEN); |
| 20 | fch = LCHILD(v,ELSE); |
| 21 | if (!DEFINED(fch)) |
| 22 | mkthen(v); |
| 23 | else if (!DEFINED(tch)) |
| 24 | { |
| 25 | negate(v); |
| 26 | mkthen(v); |
| 27 | } |
| 28 | else if (BRANCHTYPE(NTYPE(tch))) |
| 29 | mkthen(v); |
| 30 | else if (BRANCHTYPE(NTYPE(fch))) |
| 31 | { |
| 32 | negate(v); |
| 33 | mkthen(v); |
| 34 | } |
| 35 | else if (NTYPE(fch) != IFVX || DEFINED(RSIB(fch))) /* not an else if */ |
| 36 | if ( NTYPE(tch) == IFVX && !DEFINED(RSIB(tch))) |
| 37 | /* invert into else if */ |
| 38 | negate(v); |
| 39 | else |
| 40 | { |
| 41 | /* asoc(v,n) returns number of statements associated with v |
| 42 | if <= n, -1 otherwise */ |
| 43 | tn = asoc(tch,MAXCHUNK); |
| 44 | fn = asoc(fch,MAXCHUNK); |
| 45 | if (fn >= 0 && (tn < 0 || fn < tn)) |
| 46 | /* else clause smaller */ |
| 47 | negate(v); |
| 48 | } |
| 49 | } |
| 50 | RECURSE(getthen,v,recvar); |
| 51 | } |
| 52 | |
| 53 | mkthen(v) |
| 54 | VERT v; |
| 55 | { |
| 56 | VERT w,tc; |
| 57 | w = LCHILD(v,ELSE); |
| 58 | tc = LCHILD(v,THEN); |
| 59 | ASSERT(!DEFINED(w) || (DEFINED(tc) && BRANCHTYPE(NTYPE(tc)) ),mkthen); |
| 60 | if (DEFINED(w)) |
| 61 | { |
| 62 | insib(v,w); |
| 63 | LCHILD(v,ELSE) = UNDEFINED; |
| 64 | } |
| 65 | ASSERT(IFTHEN(v),mkthen); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | negate(v) |
| 70 | VERT v; |
| 71 | { |
| 72 | ASSERT(NTYPE(v) == IFVX,negate); |
| 73 | exchange(&LCHILD(v,THEN), &LCHILD(v,ELSE)); |
| 74 | NEG(v) = !NEG(v); |
| 75 | } |