FAUST compiler  0.9.9.6b8
Functions | Variables
simplify.cpp File Reference
#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"
Include dependency graph for simplify.cpp:

Go to the source code of this file.

Functions

static Tree simplification (Tree sig)
 
static Tree sigMap (Tree key, tfun f, Tree t)
 Recursively transform a graph by applying a function f. More...
 
static Tree traced_simplification (Tree sig)
 
Tree simplify (Tree sig)
 
static Tree sigMapRename (Tree key, Tree env, tfun f, Tree t)
 Like SigMap, recursively transform a graph by applying a function f. More...
 
static Tree docTableConverter (Tree sig)
 
Tree docTableConvertion (Tree sig)
 

Variables

Tree SIMPLIFIED = tree(symbol("sigSimplifiedProp"))
 
Tree DOCTABLES = tree(symbol("DocTablesProp"))
 Converts regular tables into doc tables in order to facilitate the mathematical documentation generation. More...
 
static Tree NULLENV = tree(symbol("NullRenameEnv"))
 

Function Documentation

static Tree docTableConverter ( Tree  sig)
static

Definition at line 326 of file simplify.cpp.

References isSigGen(), isSigRDTbl(), isSigTable(), isSigWRTbl(), sigDocAccessTbl(), sigDocConstantTbl(), and sigDocWriteTbl().

Referenced by docTableConvertion().

327 {
328  Tree tbl, tbl2, id, id2, size, igen, isig, ridx, widx, wsig;
329 
330  if (isSigRDTbl(sig, tbl, ridx)) {
331  // we are in a table to convert
332  if (isSigTable(tbl, id, size, igen)) {
333  // it's a read only table
334  assert(isSigGen(igen, isig));
335  return sigDocAccessTbl(sigDocConstantTbl(size,isig),ridx);
336  } else {
337  // it's a read write table
338  assert(isSigWRTbl(tbl,id,tbl2,widx,wsig));
339  assert(isSigTable(tbl2, id2, size, igen));
340  assert(isSigGen(igen, isig));
341 
342  return sigDocAccessTbl(sigDocWriteTbl(size,isig,widx,wsig),ridx);
343  }
344 
345  } else {
346  // nothing to convert
347  return sig;
348  }
349 }
bool isSigRDTbl(Tree s, Tree &t, Tree &i)
Definition: signals.cpp:77
A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches.
Definition: tree.hh:109
bool isSigGen(Tree t, Tree &x)
Definition: signals.cpp:91
Tree sigDocAccessTbl(Tree tbl, Tree ridx)
Definition: signals.cpp:106
bool isSigTable(Tree t, Tree &id, Tree &n, Tree &sig)
Definition: signals.cpp:85
Tree sigDocConstantTbl(Tree n, Tree sig)
Definition: signals.cpp:98
bool isSigWRTbl(Tree u, Tree &id, Tree &t, Tree &i, Tree &s)
Definition: signals.cpp:81
Tree sigDocWriteTbl(Tree n, Tree sig, Tree widx, Tree wsig)
Definition: signals.cpp:102

Here is the call graph for this function:

Here is the caller graph for this function:

Tree docTableConvertion ( Tree  sig)

Definition at line 317 of file simplify.cpp.

References docTableConverter(), and sigMapRename().

Referenced by mapPrepareEqSig().

318 {
320  return r;
321 }
static Tree NULLENV
Definition: simplify.cpp:315
Tree DOCTABLES
Converts regular tables into doc tables in order to facilitate the mathematical documentation generat...
Definition: simplify.cpp:311
A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches.
Definition: tree.hh:109
static Tree sigMapRename(Tree key, Tree env, tfun f, Tree t)
Like SigMap, recursively transform a graph by applying a function f.
Definition: simplify.cpp:229
static Tree docTableConverter(Tree sig)
Definition: simplify.cpp:326

Here is the call graph for this function:

Here is the caller graph for this function:

static Tree sigMap ( Tree  key,
tfun  f,
Tree  t 
)
static

Recursively transform a graph by applying a function f.

map(f, foo[t1..tn]) = f(foo[map(f,t1)..map(f,tn)])

Definition at line 187 of file simplify.cpp.

References CTree::arity(), CTree::branch(), getProperty(), isNil(), isRec(), nil, CTree::node(), rec(), setProperty(), and tree().

Referenced by simplify().

188 {
189  //printf("start sigMap\n");
190  Tree p,id,body;
191 
192  if (getProperty(t, key, p)) {
193 
194  return (isNil(p)) ? t : p; // truc pour eviter les boucles
195 
196  } else if (isRec(t, id, body)) {
197 
198  setProperty(t, key, nil); // avoid infinite loop
199  return rec(id, sigMap(key, f, body));
200 
201  } else {
202  tvec br;
203  int n = t->arity();
204  for (int i = 0; i < n; i++) {
205  br.push_back(sigMap(key,f,t->branch(i)));
206  }
207 
208  Tree r1 = tree(t->node(), br);
209 
210  Tree r2 = f(r1);
211  if (r2 == t) {
212  setProperty(t, key, nil);
213  } else {
214  setProperty(t, key, r2);
215  }
216  return r2;
217  }
218 }
Tree rec(Tree body)
create a de Bruijn recursive tree
vector< Tree > tvec
Definition: tree.hh:90
A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches.
Definition: tree.hh:109
const Node & node() const
return the content of the tree
Definition: tree.hh:143
bool isNil(Tree l)
Definition: list.hh:137
static Tree sigMap(Tree key, tfun f, Tree t)
Recursively transform a graph by applying a function f.
Definition: simplify.cpp:187
int arity() const
return the number of branches (subtrees) of a tree
Definition: tree.hh:144
bool isRec(Tree t, Tree &body)
is t a de Bruijn recursive tree
Tree tree(const Node &n)
Definition: tree.hh:186
void setProperty(Tree t, Tree key, Tree val)
Definition: list.cpp:418
Tree nil
Definition: list.cpp:116
Tree branch(int i) const
return the ith branch (subtree) of a tree
Definition: tree.hh:145
bool getProperty(Tree t, Tree key, Tree &val)
Definition: list.cpp:423

Here is the call graph for this function:

Here is the caller graph for this function:

static Tree sigMapRename ( Tree  key,
Tree  env,
tfun  f,
Tree  t 
)
static

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)])

Definition at line 229 of file simplify.cpp.

References CTree::arity(), CTree::branch(), getProperty(), isNil(), isRec(), isRef(), nil, CTree::node(), pushEnv(), rec(), ref(), searchEnv(), setProperty(), tree(), and unique().

Referenced by docTableConvertion().

230 {
231  //printf("start sigMap\n");
232  Tree p,id,body;
233 
234  if (getProperty(t, key, p)) {
235 
236  return (isNil(p)) ? t : p; // truc pour eviter les boucles
237 
238  } else if (isRec(t, id, body)) {
239 
240  assert(isRef(t,id)); // controle temporaire
241 
242  Tree id2;
243  if (searchEnv(id, id2, env)) {
244  // déjà en cours de visite de cette recursion
245  return ref(id2);
246  } else {
247  // premiere visite de cette recursion
248  id2 = tree(Node(unique("renamed")));
249  Tree body2 = sigMapRename(key, pushEnv(id, id2, env), f, body);
250  return rec(id2,body2);
251  }
252 
253  } else {
254 
255  tvec br;
256  int n = t->arity();
257  for (int i = 0; i < n; i++) {
258  br.push_back( sigMapRename(key,env,f,t->branch(i)) );
259  }
260 
261  Tree r1 = tree(t->node(), br);
262 
263 
264  Tree r2 = f(r1);
265  if (r2 == t) {
266  setProperty(t, key, nil);
267  } else {
268  setProperty(t, key, r2);
269  }
270  return r2;
271  }
272 }
Tree pushEnv(Tree key, Tree val, Tree env)
Definition: list.cpp:351
Symbol * unique(const char *str)
Returns a new unique symbol of name strxxx.
Definition: symbol.hh:97
bool searchEnv(Tree key, Tree &v, Tree env)
Definition: list.cpp:356
Class Node = (type x (int + double + Sym + void*))
Definition: node.hh:75
Tree rec(Tree body)
create a de Bruijn recursive tree
Tree ref(int level)
create a de Bruijn recursive reference
vector< Tree > tvec
Definition: tree.hh:90
A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches.
Definition: tree.hh:109
const Node & node() const
return the content of the tree
Definition: tree.hh:143
static Tree sigMapRename(Tree key, Tree env, tfun f, Tree t)
Like SigMap, recursively transform a graph by applying a function f.
Definition: simplify.cpp:229
bool isNil(Tree l)
Definition: list.hh:137
bool isRef(Tree t, int &level)
is t a de Bruijn recursive reference
int arity() const
return the number of branches (subtrees) of a tree
Definition: tree.hh:144
bool isRec(Tree t, Tree &body)
is t a de Bruijn recursive tree
Tree tree(const Node &n)
Definition: tree.hh:186
void setProperty(Tree t, Tree key, Tree val)
Definition: list.cpp:418
Tree nil
Definition: list.cpp:116
Tree branch(int i) const
return the ith branch (subtree) of a tree
Definition: tree.hh:145
bool getProperty(Tree t, Tree key, Tree &val)
Definition: list.cpp:423

Here is the call graph for this function:

Here is the caller graph for this function:

static Tree simplification ( Tree  sig)
static

Definition at line 84 of file simplify.cpp.

References CTree::arity(), CTree::branch(), BinOp::compute(), xtended::computeSigOutput(), gBinOpTable, getUserData(), gPowPrim, isDouble(), isInt(), BinOp::isLeftNeutral(), isNum(), isOne(), BinOp::isRightNeutral(), isSigBinOp(), isSigDelay1(), isSigFixDelay(), isSigFloatCast(), isSigIntCast(), isSigSelect2(), isSigSelect3(), isZero(), CTree::node(), normalizeAddTerm(), normalizeDelay1Term(), normalizeFixedDelayTerm(), sigSelect2(), and tree().

Referenced by traced_simplification().

85 {
86  assert(sig);
87  int opnum;
88  Tree t1, t2, t3, t4;
89 
90  xtended* xt = (xtended*) getUserData(sig);
91  // primitive elements
92  if (xt)
93  {
94  //return 3;
95  vector<Tree> args;
96  for (int i=0; i<sig->arity(); i++) { args.push_back( sig->branch(i) ); }
97 
98  // to avoid negative power to further normalization
99  if (xt != gPowPrim) {
100  return xt->computeSigOutput(args);
101  } else {
102  return normalizeAddTerm(xt->computeSigOutput(args));
103  }
104 
105  } else if (isSigBinOp(sig, &opnum, t1, t2)) {
106 
107  BinOp* op = gBinOpTable[opnum];
108 
109  Node n1 = t1->node();
110  Node n2 = t2->node();
111 
112  if (isNum(n1) && isNum(n2)) return tree(op->compute(n1,n2));
113 
114  else if (op->isLeftNeutral(n1)) return t2;
115 
116  else if (op->isRightNeutral(n2)) return t1;
117 
118  else return normalizeAddTerm(sig);
119 
120  } else if (isSigDelay1(sig, t1)) {
121 
122  return normalizeDelay1Term (t1);
123 
124  } else if (isSigFixDelay(sig, t1, t2)) {
125 
126  return normalizeFixedDelayTerm (t1, t2);
127 
128  } else if (isSigIntCast(sig, t1)) {
129 
130  Tree tx;
131  int i;
132  double x;
133  Node n1 = t1->node();
134 
135  if (isInt(n1, &i)) return t1;
136  if (isDouble(n1, &x)) return tree(int(x));
137  if (isSigIntCast(t1, tx)) return t1;
138 
139  return sig;
140 
141  } else if (isSigFloatCast(sig, t1)) {
142 
143  Tree tx;
144  int i;
145  double x;
146  Node n1 = t1->node();
147 
148  if (isInt(n1, &i)) return tree(double(i));
149  if (isDouble(n1, &x)) return t1;
150  if (isSigFloatCast(t1, tx)) return t1;
151 
152  return sig;
153 
154  } else if (isSigSelect2(sig, t1, t2, t3)){
155 
156  Node n1 = t1->node();
157 
158  if (isZero(n1)) return t2;
159  if (isNum(n1)) return t3;
160 
161  if (t2==t3) return t2;
162 
163  return sig;
164 
165  } else if (isSigSelect3(sig, t1, t2, t3, t4)){
166 
167  Node n1 = t1->node();
168 
169  if (isZero(n1)) return t2;
170  if (isOne(n1)) return t3;
171  if (isNum(n1)) return t4;
172 
173  if (t3==t4) return simplification(sigSelect2(t1,t2,t3));
174 
175  return sig;
176 
177  } else {
178 
179  return sig;
180  }
181 }
bool isZero(Tree a)
Definition: signals.hh:188
Class Node = (type x (int + double + Sym + void*))
Definition: node.hh:75
bool isSigSelect2(Tree t, Tree &selector, Tree &s1, Tree &s2)
Definition: signals.cpp:116
bool isNum(Tree a)
Definition: signals.hh:187
bool isOne(Tree a)
Definition: signals.hh:191
Tree sigSelect2(Tree selector, Tree s1, Tree s2)
Definition: signals.cpp:115
A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches.
Definition: tree.hh:109
bool isSigFixDelay(Tree t, Tree &t0, Tree &t1)
Definition: signals.cpp:62
const Node & node() const
return the content of the tree
Definition: tree.hh:143
bool isSigSelect3(Tree t, Tree &selector, Tree &s1, Tree &s2, Tree &s3)
Definition: signals.cpp:119
Tree normalizeFixedDelayTerm(Tree s, Tree d)
Compute the normal form of a fixed delay term (s).
Definition: normalize.cpp:81
Tree normalizeDelay1Term(Tree s)
Compute the normal form of a 1-sample delay term s'.
Definition: normalize.cpp:59
bool isSigBinOp(Tree s, int *op, Tree &x, Tree &y)
Definition: signals.cpp:126
bool isDouble(const Node &n)
Definition: node.hh:143
bool isRightNeutral(const Node &a)
Definition: binop.hh:47
static Tree simplification(Tree sig)
Definition: simplify.cpp:84
Tree normalizeAddTerm(Tree t)
Compute the Add-Normal form of a term t.
Definition: normalize.cpp:32
int arity() const
return the number of branches (subtrees) of a tree
Definition: tree.hh:144
virtual Tree computeSigOutput(const vector< Tree > &args)=0
Tree tree(const Node &n)
Definition: tree.hh:186
bool isSigDelay1(Tree t, Tree &t0)
Definition: signals.cpp:58
bool isSigFloatCast(Tree t)
Definition: signals.cpp:187
void * getUserData(Symbol *sym)
Returns user data.
Definition: symbol.hh:100
bool isSigIntCast(Tree t)
Definition: signals.cpp:184
BinOp * gBinOpTable[]
Definition: binop.cpp:28
xtended * gPowPrim
Definition: powprim.cpp:77
bool isInt(const Node &n)
Definition: node.hh:126
Node compute(const Node &a, const Node &b)
Definition: binop.hh:45
Definition: binop.hh:32
bool isLeftNeutral(const Node &a)
Definition: binop.hh:48
Tree branch(int i) const
return the ith branch (subtree) of a tree
Definition: tree.hh:145

Here is the call graph for this function:

Here is the caller graph for this function:

Tree simplify ( Tree  sig)

Definition at line 76 of file simplify.cpp.

References sigMap(), and traced_simplification().

Referenced by eval2double(), eval2int(), isBoxNumeric(), mapPrepareEqSig(), normalizeFixedDelayTerm(), numericBoxSimplification(), and ScalarCompiler::prepare().

77 {
79 }
static Tree traced_simplification(Tree sig)
Definition: simplify.cpp:50
Tree SIMPLIFIED
Definition: simplify.cpp:45
static Tree sigMap(Tree key, tfun f, Tree t)
Recursively transform a graph by applying a function f.
Definition: simplify.cpp:187

Here is the call graph for this function:

Here is the caller graph for this function:

static Tree traced_simplification ( Tree  sig)
static

Definition at line 50 of file simplify.cpp.

References simplification(), and TABBER.

Referenced by simplify().

51 {
52  assert(sig);
53 #ifdef TRACE
54  cerr << ++TABBER << "Start simplification of : " << ppsig(sig) << endl;
55  /*
56  fprintf(stderr, "\nStart simplification of : ");
57  printSignal(sig, stderr);
58  fprintf(stderr, "\n");
59  */
60 #endif
61  Tree r = simplification(sig);
62  assert(r!=0);
63 #ifdef TRACE
64  cerr << --TABBER << "Simplification of : " << ppsig(sig) << " Returns : " << ppsig(r) << endl;
65  /*
66  fprintf(stderr, "Simplification of : ");
67  printSignal(sig, stderr);
68  fprintf(stderr, " -> ");
69  printSignal(r, stderr);
70  fprintf(stderr, "\n");
71  */
72 #endif
73  return r;
74 }
A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches.
Definition: tree.hh:109
Definition: ppsig.hh:48
static Tree simplification(Tree sig)
Definition: simplify.cpp:84
Tabber TABBER(1)
Definition: tree.cpp:87

Here is the call graph for this function:

Here is the caller graph for this function:

Variable Documentation

Tree DOCTABLES = tree(symbol("DocTablesProp"))

Converts regular tables into doc tables in order to facilitate the mathematical documentation generation.

Definition at line 311 of file simplify.cpp.

Tree NULLENV = tree(symbol("NullRenameEnv"))
static

Definition at line 315 of file simplify.cpp.

Tree SIMPLIFIED = tree(symbol("sigSimplifiedProp"))

Definition at line 45 of file simplify.cpp.