FAUST compiler  0.9.9.6b8
sharing.cpp
Go to the documentation of this file.
1 /************************************************************************
2  ************************************************************************
3  FAUST compiler
4  Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
5  ---------------------------------------------------------------------
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  ************************************************************************
20  ************************************************************************/
21 
22 
23 
24 /*****************************************************************************
25 ******************************************************************************
26  FAUST SIGNAL COMPILER
27  Y. Orlarey, (c) Grame 2002
28 ------------------------------------------------------------------------------
29 Compile a list of FAUST signals into a C++ class .
30 
31  History :
32  ---------
33  2002-02-08 : First version
34 
35 ******************************************************************************
36 *****************************************************************************/
37 
38 
39 
40 #include <stdio.h>
41 
42 #include "compile_vect.hh"
43 #include "compile_scal.hh"
44 #include "sigtype.hh"
45 #include "sigtyperules.hh"
46 #include "sigprint.hh"
47 
48 
49 
50 /*****************************************************************************
51 ******************************************************************************
52 
53  SHARING ANALYSIS
54 
55 ******************************************************************************
56 *****************************************************************************/
57 
58 //------------------------------------------------------------------------------
59 // Create a specific property key for the sharing count of subtrees of t
60 //------------------------------------------------------------------------------
61 
63 {
64  //cerr << "getSharingCount of : " << *sig << " = ";
65  Tree c;
66  if (getProperty(sig, fSharingKey, c)) {
67  //cerr << c->node().getInt() << endl;
68  return c->node().getInt();
69  } else {
70  //cerr << 0 << endl;
71  return 0;
72  }
73 }
74 
76 {
77  //cerr << "setSharingCount of : " << *sig << " <- " << count << endl;
78  setProperty(sig, fSharingKey, tree(count));
79 }
80 
81 
82 
83 //------------------------------------------------------------------------------
84 // Create a specific property key for the sharing count of subtrees of t
85 //------------------------------------------------------------------------------
86 
87 
89 {
90  fSharingKey = shprkey(t);
91  if (isList(t)) {
92  while (isList(t)) {
94  t = tl(t);
95  }
96  } else {
98  }
99 }
100 
101 
102 
103 //------------------------------------------------------------------------------
104 // Create a specific property key for the sharing count of subtrees of t
105 //------------------------------------------------------------------------------
107 {
108  Tree c, x, y, z;
109 
110  //cerr << "START sharing annotation of " << *sig << endl;
111  int count = getSharingCount(sig);
112 
113  if (count > 0) {
114  // it is not our first visit
115  setSharingCount(sig, count+1);
116 
117  } else {
118  // it is our first visit,
119  int v = getCertifiedSigType(sig)->variability();
120 
121  // check "time sharing" cases
122  if (v < vctxt) {
123  setSharingCount(sig, 2); // time sharing occurence : slower expression in faster context
124  } else {
125  setSharingCount(sig, 1); // regular occurence
126  }
127 
128  if (isSigSelect3(sig,c,y,x,z)) {
129  // make a special case for select3 implemented with real if
130  // because the c expression will be used twice in the C++
131  // translation
132  sharingAnnotation(v, c);
133  sharingAnnotation(v, c);
134  sharingAnnotation(v, x);
135  sharingAnnotation(v, y);
136  sharingAnnotation(v, z);
137  } else {
138  // Annotate the sub signals
139  vector<Tree> subsig;
140  int n = getSubSignals(sig, subsig);
141  if (n>0 && ! isSigGen(sig)) {
142  for (int i=0; i<n; i++) sharingAnnotation(v, subsig[i]);
143  }
144  }
145  }
146  //cerr << "END sharing annotation of " << *sig << endl;
147 }
Tree shprkey(Tree t)
Create a specific property key for the sharing count of subtrees of t.
Definition: shlysis.cpp:69
A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches.
Definition: tree.hh:109
Tree hd(Tree l)
Definition: list.hh:133
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
bool isSigGen(Tree t, Tree &x)
Definition: signals.cpp:91
int getSharingCount(Tree t)
Definition: sharing.cpp:62
bool isList(Tree l)
Definition: list.hh:138
void sharingAnalysis(Tree t)
Definition: sharing.cpp:88
Definition: sigtype.hh:56
Type getCertifiedSigType(Tree sig)
Retrieve the type of sig and check it exists.
Tree tree(const Node &n)
Definition: tree.hh:186
int getInt() const
Definition: node.hh:104
void setProperty(Tree t, Tree key, Tree val)
Definition: list.cpp:418
void sharingAnnotation(int vctxt, Tree t)
Definition: sharing.cpp:106
API to the typing system of signals.
Tree tl(Tree l)
Definition: list.hh:134
int getSubSignals(Tree sig, vector< Tree > &vsigs, bool visitgen=true)
Extract the sub signals of a signal expression, that is not necesseraly all the subtrees.
Definition: subsignals.cpp:12
void setSharingCount(Tree t, int count)
Definition: sharing.cpp:75
bool getProperty(Tree t, Tree key, Tree &val)
Definition: list.cpp:423