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 "signals.hh"
00027 #include "sigtype.hh"
00028 #include "sigtyperules.hh"
00029 #include "sigorderrules.hh"
00030 #include "sigprint.hh"
00031 #include "simplify.hh"
00032 #include "num.hh"
00033 #include "xtended.hh"
00034 #include <map>
00035
00036 #include "normalize.hh"
00037
00038 #undef TRACE
00039
00040
00041 Tree SIMPLIFIED = tree(symbol("sigSimplifiedProp"));
00042
00043 static Tree simplification (Tree sig);
00044 static Tree sigMap (Tree key, tfun f, Tree t);
00045
00046 static Tree traced_simplification(Tree sig)
00047 {
00048 assert(sig);
00049 #ifdef TRACE
00050 cerr << ++TABBER << "Start simplification of : " << *sig << endl;
00051
00052
00053
00054
00055
00056 #endif
00057 Tree r = simplification(sig);
00058 assert(r!=0);
00059 #ifdef TRACE
00060 cerr << --TABBER << "Simplification of : " << *sig << " Returns : " << *r << endl;
00061
00062
00063
00064
00065
00066
00067
00068 #endif
00069 return r;
00070 }
00071
00072 Tree simplify (Tree sig)
00073 {
00074 return sigMap(SIMPLIFIED, traced_simplification, sig);
00075 }
00076
00077
00078
00079
00080 static Tree simplification (Tree sig)
00081 {
00082 assert(sig);
00083 int opnum;
00084 Tree t1, t2;
00085
00086 xtended* xt = (xtended*) getUserData(sig);
00087
00088 if (xt)
00089 {
00090
00091 vector<Tree> args;
00092 for (int i=0; i<sig->arity(); i++) { args.push_back( sig->branch(i) ); }
00093 return xt->computeSigOutput(args);
00094
00095 } else if (isSigBinOp(sig, &opnum, t1, t2)) {
00096
00097 BinOp* op = gBinOpTable[opnum];
00098
00099 Node n1 = t1->node();
00100 Node n2 = t2->node();
00101
00102 if (isNum(n1) && isNum(n2)) return tree(op->compute(n1,n2));
00103
00104 else if (op->isLeftNeutral(n1)) return t2;
00105
00106 else if (op->isRightNeutral(n2)) return t1;
00107
00108 else return normalizeAddTerm(sig);
00109
00110 } else if (isSigDelay1(sig, t1)) {
00111
00112 return normalizeDelay1Term (t1);
00113
00114 } else if (isSigFixDelay(sig, t1, t2)) {
00115
00116 return normalizeFixedDelayTerm (t1, t2);
00117
00118 } else if (isSigIntCast(sig, t1)) {
00119
00120 Tree tx;
00121 int i;
00122 double x;
00123 Node n1 = t1->node();
00124
00125 if (isInt(n1, &i)) return t1;
00126 if (isDouble(n1, &x)) return tree(int(x));
00127 if (isSigIntCast(t1, tx)) return t1;
00128
00129 return sig;
00130
00131 } else if (isSigFloatCast(sig, t1)) {
00132
00133 Tree tx;
00134 int i;
00135 double x;
00136 Node n1 = t1->node();
00137
00138 if (isInt(n1, &i)) return tree(double(i));
00139 if (isDouble(n1, &x)) return t1;
00140 if (isSigFloatCast(t1, tx)) return t1;
00141
00142 return sig;
00143
00144 } else {
00145
00146 return sig;
00147 }
00148 }
00149
00150
00151
00152
00153
00154 static Tree sigMap (Tree key, tfun f, Tree t)
00155 {
00156
00157 Tree p,id,body;
00158
00159 if (getProperty(t, key, p)) {
00160
00161 return (isNil(p)) ? t : p;
00162
00163 } else if (isRec(t, id, body)) {
00164
00165 setProperty(t, key, nil);
00166 return rec(id, sigMap(key, f, body));
00167
00168 } else {
00169
00170 Tree r1=nil;
00171 switch (t->arity()) {
00172
00173 case 0 :
00174 r1 = t;
00175 break;
00176 case 1 :
00177 r1 = tree(t->node(), sigMap(key,f,t->branch(0)));
00178 break;
00179 case 2 :
00180 r1 = tree(t->node(), sigMap(key,f,t->branch(0)), sigMap(key,f,t->branch(1)));
00181 break;
00182 case 3 :
00183 r1 = tree(t->node(), sigMap(key,f,t->branch(0)), sigMap(key,f,t->branch(1)),
00184 sigMap(key,f,t->branch(2)));
00185 break;
00186 case 4 :
00187 r1 = tree(t->node(), sigMap(key,f,t->branch(0)), sigMap(key,f,t->branch(1)),
00188 sigMap(key,f,t->branch(2)), sigMap(key,f,t->branch(3)));
00189 break;
00190 }
00191 Tree r2 = f(r1);
00192 if (r2 == t) {
00193 setProperty(t, key, nil);
00194 } else {
00195 setProperty(t, key, r2);
00196 }
00197 return r2;
00198 }
00199 }
00200
00206 Tree DOCTABLES = tree(symbol("DocTablesProp"));
00207
00208 static Tree docTableConverter (Tree sig);
00209
00210 Tree docTableConvertion (Tree sig)
00211 {
00212 return sigMap(DOCTABLES, docTableConverter, sig);
00213 }
00214
00215
00216
00217
00218 static Tree docTableConverter (Tree sig)
00219 {
00220 Tree tbl, tbl2, id, id2, size, igen, isig, ridx, widx, wsig;
00221
00222 if (isSigRDTbl(sig, tbl, ridx)) {
00223
00224 if (isSigTable(tbl, id, size, igen)) {
00225
00226 assert(isSigGen(igen, isig));
00227 return sigDocAccessTbl(sigDocConstantTbl(size,isig),ridx);
00228 } else {
00229
00230 assert(isSigWRTbl(tbl,id,tbl2,widx,wsig));
00231 assert(isSigTable(tbl2, id2, size, igen));
00232 assert(isSigGen(igen, isig));
00233
00234 return sigDocAccessTbl(sigDocWriteTbl(size,isig,widx,wsig),ridx);
00235 }
00236
00237 } else {
00238
00239 return sig;
00240 }
00241 }