Merge branch 'master' of https://scm.cri.ensmp.fr/git/Faustine
[Faustine.git] / interpretor / faust-0.9.47mr3 / compiler / generator / sharing.cpp
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
62 int ScalarCompiler::getSharingCount(Tree sig)
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
75 void ScalarCompiler::setSharingCount(Tree sig, int count)
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
88 void ScalarCompiler::sharingAnalysis(Tree t)
89 {
90 fSharingKey = shprkey(t);
91 if (isList(t)) {
92 while (isList(t)) {
93 sharingAnnotation(kSamp, hd(t));
94 t = tl(t);
95 }
96 } else {
97 sharingAnnotation(kSamp, t);
98 }
99 }
100
101
102
103 //------------------------------------------------------------------------------
104 // Create a specific property key for the sharing count of subtrees of t
105 //------------------------------------------------------------------------------
106 void ScalarCompiler::sharingAnnotation(int vctxt, Tree sig)
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 }