FAUST compiler  0.9.9.6b8
sharing.cpp
Go to the documentation of this file.
00001 /************************************************************************
00002  ************************************************************************
00003     FAUST compiler
00004     Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
00005     ---------------------------------------------------------------------
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  ************************************************************************
00020  ************************************************************************/
00021 
00022 
00023 
00024 /*****************************************************************************
00025 ******************************************************************************
00026                             FAUST SIGNAL COMPILER
00027                         Y. Orlarey, (c) Grame 2002
00028 ------------------------------------------------------------------------------
00029 Compile a list of FAUST signals into a C++ class .
00030 
00031  History :
00032  ---------
00033     2002-02-08 : First version
00034 
00035 ******************************************************************************
00036 *****************************************************************************/
00037 
00038 
00039 
00040 #include <stdio.h>
00041 
00042 #include "compile_vect.hh"
00043 #include "compile_scal.hh"
00044 #include "sigtype.hh"
00045 #include "sigtyperules.hh"
00046 #include "sigprint.hh"
00047 
00048 
00049 
00050 /*****************************************************************************
00051 ******************************************************************************
00052 
00053                                 SHARING ANALYSIS
00054 
00055 ******************************************************************************
00056 *****************************************************************************/
00057 
00058 //------------------------------------------------------------------------------
00059 // Create a specific property key for the sharing count of subtrees of t
00060 //------------------------------------------------------------------------------
00061 
00062 int ScalarCompiler::getSharingCount(Tree sig)
00063 {
00064     //cerr << "getSharingCount of : " << *sig << " = ";
00065     Tree c;
00066     if (getProperty(sig, fSharingKey, c)) {
00067         //cerr << c->node().getInt() << endl;
00068         return c->node().getInt();
00069     } else {
00070         //cerr << 0 << endl;
00071         return 0;
00072     }
00073 }
00074 
00075 void ScalarCompiler::setSharingCount(Tree sig, int count)
00076 {
00077     //cerr << "setSharingCount of : " << *sig << " <- " << count << endl;
00078     setProperty(sig, fSharingKey, tree(count));
00079 }
00080 
00081 
00082 
00083 //------------------------------------------------------------------------------
00084 // Create a specific property key for the sharing count of subtrees of t
00085 //------------------------------------------------------------------------------
00086 
00087 
00088 void ScalarCompiler::sharingAnalysis(Tree t)
00089 {
00090     fSharingKey = shprkey(t);
00091     if (isList(t)) {
00092         while (isList(t)) {
00093             sharingAnnotation(kSamp, hd(t));
00094             t = tl(t);
00095         }
00096     } else {
00097         sharingAnnotation(kSamp, t);
00098     }
00099 }
00100 
00101 
00102 
00103 //------------------------------------------------------------------------------
00104 // Create a specific property key for the sharing count of subtrees of t
00105 //------------------------------------------------------------------------------
00106 void ScalarCompiler::sharingAnnotation(int vctxt, Tree sig)
00107 {
00108     Tree    c, x, y, z;
00109 
00110     //cerr << "START sharing annotation of " << *sig << endl;
00111     int count = getSharingCount(sig);
00112 
00113     if (count > 0) {
00114         // it is not our first visit
00115         setSharingCount(sig, count+1);
00116 
00117     } else {
00118         // it is our first visit,
00119         int v = getCertifiedSigType(sig)->variability();
00120 
00121         // check "time sharing" cases
00122         if (v < vctxt) {
00123             setSharingCount(sig, 2);    // time sharing occurence : slower expression in faster context
00124         } else {
00125             setSharingCount(sig, 1);    // regular occurence
00126         }
00127 
00128         if (isSigSelect3(sig,c,y,x,z)) {
00129             // make a special case for select3 implemented with real if
00130             // because the c expression will be used twice in the C++
00131             // translation
00132             sharingAnnotation(v, c);
00133             sharingAnnotation(v, c);
00134             sharingAnnotation(v, x);
00135             sharingAnnotation(v, y);
00136             sharingAnnotation(v, z);
00137         } else {
00138             // Annotate the sub signals
00139             vector<Tree> subsig;
00140             int n = getSubSignals(sig, subsig);
00141             if (n>0 && ! isSigGen(sig)) {
00142                 for (int i=0; i<n; i++) sharingAnnotation(v, subsig[i]);
00143             }
00144         }
00145     }
00146     //cerr << "END sharing annotation of " << *sig << endl;
00147 }