00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <stdio.h>
00025 #include <assert.h>
00026 #include "sigtype.hh"
00027 #include "sigtyperules.hh"
00028 #include "sigorderrules.hh"
00029 #include "sigprint.hh"
00030 #include "simplify.hh"
00031 #include "num.hh"
00032 #include "xtended.hh"
00033 #include <map>
00034
00035 #include "normalize.hh"
00036
00037 #undef TRACE
00038
00039
00040 Tree SIMPLIFIED = tree(symbol("sigSimplifiedProp"));
00041
00042 static Tree simplification (Tree sig);
00043 static Tree sigMap (Tree key, tfun f, Tree t);
00044
00045 static Tree traced_simplification(Tree sig)
00046 {
00047 assert(sig);
00048 #ifdef TRACE
00049 cerr << ++TABBER << "Start simplification of : " << *sig << endl;
00050
00051
00052
00053
00054
00055 #endif
00056 Tree r = simplification(sig);
00057 #ifdef TRACE
00058 cerr << --TABBER << "Simplification of : " << *sig << " Returns : " << *r << endl;
00059
00060
00061
00062
00063
00064
00065
00066 #endif
00067 return r;
00068 }
00069
00070 Tree simplify (Tree sig)
00071 {
00072 return sigMap(SIMPLIFIED, traced_simplification, sig);
00073 }
00074
00075
00076
00077
00078 static Tree simplification (Tree sig)
00079 {
00080 assert(sig);
00081 int opnum;
00082 Tree t1, t2;
00083
00084 xtended* xt = (xtended*) getUserData(sig);
00085
00086 if (xt)
00087 {
00088
00089 vector<Tree> args;
00090 for (int i=0; i<sig->arity(); i++) { args.push_back( sig->branch(i) ); }
00091 return xt->computeSigOutput(args);
00092
00093 } else if (isSigBinOp(sig, &opnum, t1, t2)) {
00094
00095 BinOp* op = gBinOpTable[opnum];
00096
00097 Node n1 = t1->node();
00098 Node n2 = t2->node();
00099
00100 if (isNum(n1) && isNum(n2)) return tree(op->compute(n1,n2));
00101
00102 else if (op->isLeftNeutral(n1)) return t2;
00103
00104 else if (op->isRightNeutral(n2)) return t1;
00105
00106 else return normalizeAddTerm(sig);
00107
00108 } else if (isSigDelay1(sig, t1)) {
00109
00110 return normalizeDelay1Term (t1);
00111
00112 } else if (isSigFixDelay(sig, t1, t2)) {
00113
00114 return normalizeFixedDelayTerm (t1, t2);
00115
00116 } else if (isSigIntCast(sig, t1)) {
00117
00118 Tree tx;
00119 int i;
00120 float x;
00121 Node n1 = t1->node();
00122
00123 if (isInt(n1, &i)) return t1;
00124 if (isFloat(n1, &x)) return tree(int(x));
00125 if (isSigIntCast(t1, tx)) return t1;
00126
00127 return sig;
00128
00129 } else if (isSigFloatCast(sig, t1)) {
00130
00131 Tree tx;
00132 int i;
00133 float x;
00134 Node n1 = t1->node();
00135
00136 if (isInt(n1, &i)) return tree(float(i));
00137 if (isFloat(n1, &x)) return t1;
00138 if (isSigFloatCast(t1, tx)) return t1;
00139
00140 return sig;
00141
00142 } else {
00143
00144 return sig;
00145 }
00146 }
00147
00148
00149
00150
00151
00152 static Tree sigMap (Tree key, tfun f, Tree t)
00153 {
00154
00155 Tree p,id,body;
00156
00157 if (getProperty(t, key, p)) {
00158
00159 return (isNil(p)) ? t : p;
00160
00161 } else if (isRec(t, id, body)) {
00162
00163 setProperty(t, key, nil);
00164 return rec(id, sigMap(key, f, body));
00165
00166 } else {
00167
00168 Tree r1=nil;
00169 switch (t->arity()) {
00170
00171 case 0 :
00172 r1 = t;
00173 break;
00174 case 1 :
00175 r1 = tree(t->node(), sigMap(key,f,t->branch(0)));
00176 break;
00177 case 2 :
00178 r1 = tree(t->node(), sigMap(key,f,t->branch(0)), sigMap(key,f,t->branch(1)));
00179 break;
00180 case 3 :
00181 r1 = tree(t->node(), sigMap(key,f,t->branch(0)), sigMap(key,f,t->branch(1)),
00182 sigMap(key,f,t->branch(2)));
00183 break;
00184 case 4 :
00185 r1 = tree(t->node(), sigMap(key,f,t->branch(0)), sigMap(key,f,t->branch(1)),
00186 sigMap(key,f,t->branch(2)), sigMap(key,f,t->branch(3)));
00187 break;
00188 }
00189 Tree r2 = f(r1);
00190 if (r2 == t) {
00191 setProperty(t, key, nil);
00192 } else {
00193 setProperty(t, key, r2);
00194 }
00195 return r2;
00196 }
00197 }
00198
00199