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
|
/************************************************************************
************************************************************************
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 "sigtype.hh"
#include "sigtyperules.hh"
#include "sigorderrules.hh"
#include "sigprint.hh"
#include "simplify.hh"
#include "num.hh"
#include "xtended.hh"
#include <map>
#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 : " << *sig << endl;
/*
fprintf(stderr, "\nStart simplification of : ");
printSignal(sig, stderr);
fprintf(stderr, "\n");
*/
#endif
Tree r = simplification(sig);
#ifdef TRACE
cerr << --TABBER << "Simplification of : " << *sig << " Returns : " << *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;
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) ); }
return 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;
float x;
Node n1 = t1->node();
if (isInt(n1, &i)) return t1;
if (isFloat(n1, &x)) return tree(int(x));
if (isSigIntCast(t1, tx)) return t1;
return sig;
} else if (isSigFloatCast(sig, t1)) {
Tree tx;
int i;
float x;
Node n1 = t1->node();
if (isInt(n1, &i)) return tree(float(i));
if (isFloat(n1, &x)) return t1;
if (isSigFloatCast(t1, tx)) return t1;
return sig;
} else {
return sig;
}
}
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;
}
}
|