1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
/************************************************************************
************************************************************************
FAUST compiler
Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
---------------------------------------------------------------------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
************************************************************************
************************************************************************/
#include <stdio.h>
#include <assert.h>
#include "list.hh"
#include "signals.hh"
#include "sigtype.hh"
#include "recursivness.hh"
#include "sigtyperules.hh"
#include "sigorderrules.hh"
#include "sigprint.hh"
#include "ppsig.hh"
#include "simplify.hh"
#include "num.hh"
#include "xtended.hh"
#include <map>
#include "compatibility.hh"
#include "normalize.hh"
#undef TRACE
// declarations
Tree SIMPLIFIED = tree(symbol("sigSimplifiedProp"));
//static Tree binequiv (Tree sig, int opnum, Tree a, Tree b);
static Tree simplification (Tree sig);
static Tree sigMap (Tree key, tfun f, Tree t);
static Tree traced_simplification(Tree sig)
{
assert(sig);
#ifdef TRACE
cerr << ++TABBER << "Start simplification of : " << ppsig(sig) << endl;
/*
fprintf(stderr, "\nStart simplification of : ");
printSignal(sig, stderr);
fprintf(stderr, "\n");
*/
#endif
Tree r = simplification(sig);
assert(r!=0);
#ifdef TRACE
cerr << --TABBER << "Simplification of : " << ppsig(sig) << " Returns : " << ppsig(r) << endl;
/*
fprintf(stderr, "Simplification of : ");
printSignal(sig, stderr);
fprintf(stderr, " -> ");
printSignal(r, stderr);
fprintf(stderr, "\n");
*/
#endif
return r;
}
Tree simplify (Tree sig)
{
return sigMap(SIMPLIFIED, traced_simplification, sig);
}
// Implementation
static Tree simplification (Tree sig)
{
assert(sig);
int opnum;
Tree t1, t2, t3, t4;
xtended* xt = (xtended*) getUserData(sig);
// primitive elements
if (xt)
{
//return 3;
vector<Tree> args;
for (int i=0; i<sig->arity(); i++) { args.push_back( sig->branch(i) ); }
// to avoid negative power to further normalization
if (xt != gPowPrim) {
return xt->computeSigOutput(args);
} else {
return normalizeAddTerm(xt->computeSigOutput(args));
}
} else if (isSigBinOp(sig, &opnum, t1, t2)) {
BinOp* op = gBinOpTable[opnum];
Node n1 = t1->node();
Node n2 = t2->node();
if (isNum(n1) && isNum(n2)) return tree(op->compute(n1,n2));
else if (op->isLeftNeutral(n1)) return t2;
else if (op->isRightNeutral(n2)) return t1;
else return normalizeAddTerm(sig);
} else if (isSigDelay1(sig, t1)) {
return normalizeDelay1Term (t1);
} else if (isSigFixDelay(sig, t1, t2)) {
return normalizeFixedDelayTerm (t1, t2);
} else if (isSigIntCast(sig, t1)) {
Tree tx;
int i;
double x;
Node n1 = t1->node();
if (isInt(n1, &i)) return t1;
if (isDouble(n1, &x)) return tree(int(x));
if (isSigIntCast(t1, tx)) return t1;
return sig;
} else if (isSigFloatCast(sig, t1)) {
Tree tx;
int i;
double x;
Node n1 = t1->node();
if (isInt(n1, &i)) return tree(double(i));
if (isDouble(n1, &x)) return t1;
if (isSigFloatCast(t1, tx)) return t1;
return sig;
} else if (isSigSelect2(sig, t1, t2, t3)){
Node n1 = t1->node();
if (isZero(n1)) return t2;
if (isNum(n1)) return t3;
if (t2==t3) return t2;
return sig;
} else if (isSigSelect3(sig, t1, t2, t3, t4)){
Node n1 = t1->node();
if (isZero(n1)) return t2;
if (isOne(n1)) return t3;
if (isNum(n1)) return t4;
if (t3==t4) return simplification(sigSelect2(t1,t2,t3));
return sig;
} else {
return sig;
}
}
/**
* Recursively transform a graph by applying a function f.
* map(f, foo[t1..tn]) = f(foo[map(f,t1)..map(f,tn)])
*/
static Tree sigMap (Tree key, tfun f, Tree t)
{
//printf("start sigMap\n");
Tree p,id,body;
if (getProperty(t, key, p)) {
return (isNil(p)) ? t : p; // truc pour eviter les boucles
} else if (isRec(t, id, body)) {
setProperty(t, key, nil); // avoid infinite loop
return rec(id, sigMap(key, f, body));
} else {
Tree r1=nil;
switch (t->arity()) {
case 0 :
r1 = t;
break;
case 1 :
r1 = tree(t->node(), sigMap(key,f,t->branch(0)));
break;
case 2 :
r1 = tree(t->node(), sigMap(key,f,t->branch(0)), sigMap(key,f,t->branch(1)));
break;
case 3 :
r1 = tree(t->node(), sigMap(key,f,t->branch(0)), sigMap(key,f,t->branch(1)),
sigMap(key,f,t->branch(2)));
break;
case 4 :
r1 = tree(t->node(), sigMap(key,f,t->branch(0)), sigMap(key,f,t->branch(1)),
sigMap(key,f,t->branch(2)), sigMap(key,f,t->branch(3)));
break;
}
Tree r2 = f(r1);
if (r2 == t) {
setProperty(t, key, nil);
} else {
setProperty(t, key, r2);
}
return r2;
}
}
/**
* Like SigMap, recursively transform a graph by applying a
* function f. But here recursive trees are also renamed.
* map(f, foo[t1..tn]) = f(foo[map(f,t1)..map(f,tn)])
*/
static Tree sigMapRename (Tree key, Tree env, tfun f, Tree t)
{
//printf("start sigMap\n");
Tree p,id,body;
if (getProperty(t, key, p)) {
return (isNil(p)) ? t : p; // truc pour eviter les boucles
} else if (isRec(t, id, body)) {
assert(isRef(t,id)); // controle temporaire
Tree id2;
if (searchEnv(id, id2, env)) {
// déjà en cours de visite de cette recursion
return ref(id2);
} else {
// premiere visite de cette recursion
id2 = tree(Node(unique("renamed")));
Tree body2 = sigMapRename(key, pushEnv(id, id2, env), f, body);
return rec(id2,body2);
}
} else {
Tree r1=nil;
switch (t->arity()) {
case 0 :
r1 = t;
break;
case 1 :
r1 = tree(t->node(), sigMapRename(key,env,f,t->branch(0)));
break;
case 2 :
r1 = tree(t->node(), sigMapRename(key,env,f,t->branch(0)),
sigMapRename(key,env,f,t->branch(1)));
break;
case 3 :
r1 = tree(t->node(), sigMapRename(key,env,f,t->branch(0)),
sigMapRename(key,env,f,t->branch(1)),
sigMapRename(key,env,f,t->branch(2)));
break;
case 4 :
r1 = tree(t->node(), sigMapRename(key,env,f,t->branch(0)),
sigMapRename(key,env,f,t->branch(1)),
sigMapRename(key,env,f,t->branch(2)),
sigMapRename(key,env,f,t->branch(3)));
break;
}
Tree r2 = f(r1);
if (r2 == t) {
setProperty(t, key, nil);
} else {
setProperty(t, key, r2);
}
return r2;
}
}
#if 0
static void eraseProperties (Tree key, Tree t)
{
//printf("start sigMap\n");
Tree p,id,body;
if (getProperty(t, key, p)) {
// already erased, nothing to do
} else if (isRec(t, id, body)) {
t->clearProperties();
Tree r=rec(id, body);
assert(r==t);
setProperty(t, key, nil); // avoid infinite loop
eraseProperties(key, body);
} else {
for (int i=0; i<t->arity(); i++) {
eraseProperties(key,t->branch(i));
}
}
}
void eraseAllProperties(Tree t)
{
cerr << "begin eraseAllProperties" << endl;
eraseProperties(tree(Node(unique("erase_"))), t);
cerr << "end eraseAllProperties" << endl;
}
#endif
/**
* Converts regular tables into doc tables in order to
* facilitate the mathematical documentation generation
*/
Tree DOCTABLES = tree(symbol("DocTablesProp"));
static Tree docTableConverter (Tree sig);
static Tree NULLENV = tree(symbol("NullRenameEnv"));
Tree docTableConvertion (Tree sig)
{
Tree r = sigMapRename(DOCTABLES, NULLENV, docTableConverter, sig);
return r;
}
// Implementation
static Tree docTableConverter (Tree sig)
{
Tree tbl, tbl2, id, id2, size, igen, isig, ridx, widx, wsig;
if (isSigRDTbl(sig, tbl, ridx)) {
// we are in a table to convert
if (isSigTable(tbl, id, size, igen)) {
// it's a read only table
assert(isSigGen(igen, isig));
return sigDocAccessTbl(sigDocConstantTbl(size,isig),ridx);
} else {
// it's a read write table
assert(isSigWRTbl(tbl,id,tbl2,widx,wsig));
assert(isSigTable(tbl2, id2, size, igen));
assert(isSigGen(igen, isig));
return sigDocAccessTbl(sigDocWriteTbl(size,isig,widx,wsig),ridx);
}
} else {
// nothing to convert
return sig;
}
}
|