1 /************************************************************************
2  ************************************************************************
3     FAUST compiler
4     Copyright (C) 2003-2018 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 #ifndef _KLASS_H
23 #define _KLASS_H
24 
25 /**********************************************************************
26         - klass.h : class C++ to be filled (FAUST project) -
27 
28          History :
29          -----------
30          17-10-2001 : initial implementation  (yo)
31          18-10-2001 : add getFreshID (yo)
32          02-11-2001 : add sub-classes (yo)
33          06-11-2001 : change classers impression (yo)
34 
35 ***********************************************************************/
36 
37 #include <list>
38 #include <map>
39 #include <set>
40 #include <string>
41 
42 #include "property.hh"
43 #include "sigtype.hh"
44 #include "tlib.hh"
45 #include "uitree.hh"
46 #include "graphSorting.hh"
47 #include "loop.hh"
48 
49 using namespace std;
50 
51 class Klass
52 {
53    protected:
54     // we make it global because several classes may need
55     // power def but we want the code to be generated only once
56     static bool fNeedPowerDef;
57 
58     Klass* fParentKlass;  ///< Klass in which this Klass is embedded, void if toplevel Klass
59     string fKlassName;
60     string fSuperKlassName;
61     int    fNumInputs;
62     int    fNumOutputs;
63     int    fNumActives;   ///< number of active controls in the UI (sliders, buttons, etc.)
64     int    fNumPassives;  ///< number of passive widgets in the UI (bargraphs, etc.)
65 
66     set<string> fIncludeFileSet;
67     set<string> fLibrarySet;
68 
69     list<Klass*> fSubClassList;
70 
71     list<string> fDeclCode;
72     list<string> fStaticInitCode;     ///< static init code for class constant tables
73     list<string> fStaticDestroyCode;  ///< static destroy code for class constant tables
74     list<string> fStaticFields;       ///< static fields after class
75     list<string> fInitCode;
76     list<string> fInitUICode;
77     list<string> fClearCode;
78     list<string> fUICode;
79     list<string> fUIMacro;
80 
81 #if 0
82     list<string>        fSlowDecl;
83     list<string>        fSharedDecl;            ///< declare shared variables for openMP
84     list<string>        fCommonCode;            ///< code executed by all threads
85     list<string>        fSlowCode;
86     list<string>        fEndCode;
87 #endif
88     list<string> fSharedDecl;        ///< shared declarations
89     list<string> fFirstPrivateDecl;  ///< first private declarations
90 
91     list<string> fZone1Code;   ///< shared vectors
92     list<string> fZone2Code;   ///< first private
93     list<string> fZone2bCode;  ///< single once per block
94     list<string> fZone2cCode;  ///< single once per block
95     list<string> fZone3Code;   ///< private every sub block
96     list<string> fZone4Code;   ///< code after all loops
97 
98     Loop*           fTopLoop;       ///< active loops currently open
99     property<Loop*> fLoopProperty;  ///< loops used to compute some signals
100 
101     bool fVec;
102 
103    public:
Klass(const string & name,const string & super,int numInputs,int numOutputs,bool __vec=false)104     Klass(const string& name, const string& super, int numInputs, int numOutputs, bool __vec = false)
105         : fParentKlass(0),
106           fKlassName(name),
107           fSuperKlassName(super),
108           fNumInputs(numInputs),
109           fNumOutputs(numOutputs),
110           fNumActives(0),
111           fNumPassives(0),
112           fTopLoop(new Loop(0, "count")),
113           fVec(__vec)
114     {
115     }
116 
~Klass()117     virtual ~Klass() {}
118 
setParentKlass(Klass * parent)119     void setParentKlass(Klass* parent)
120     {
121         // std::cerr << this << " setParentKlass(" << parent << ")" << std::endl;
122         fParentKlass = parent;
123     }
getParentKlass()124     Klass* getParentKlass() { return fParentKlass; }
getTopParentKlass()125     Klass* getTopParentKlass() { return (fParentKlass != 0) ? fParentKlass->getTopParentKlass() : this; }
getFullClassName()126     string getFullClassName()
127     {
128         return (fParentKlass != 0) ? fParentKlass->getFullClassName() + "::" + getClassName() : getClassName();
129     }  ///< Returns the name of the class
130 
131     void openLoop(const string& size);
132     void openLoop(Tree recsymbol, const string& size);
133     void closeLoop(Tree sig);
134 
135     void setLoopProperty(Tree sig, Loop* l);   ///< Store the loop used to compute a signal
136     bool getLoopProperty(Tree sig, Loop*& l);  ///< Returns the loop used to compute a signal
137     void listAllLoopProperties(Tree sig, set<Loop*>&, set<Tree>& visited);  ///< Returns all the loop used to compute a signal
138 
getClassName() const139     const string& getClassName() const { return fKlassName; }  ///< Returns the name of the class
140 
topLoop()141     Loop* topLoop() { return fTopLoop; }
142 
143     void buildTasksList();
144 
addIncludeFile(const string & str)145     void addIncludeFile(const string& str) { fIncludeFileSet.insert(str); }
146 
addLibrary(const string & str)147     void addLibrary(const string& str) { fLibrarySet.insert(str); }
148 
rememberNeedPowerDef()149     void rememberNeedPowerDef() { fNeedPowerDef = true; }
150 
151     void collectIncludeFile(set<string>& S);
152 
153     void collectLibrary(set<string>& S);
154 
addSubKlass(Klass * son)155     void addSubKlass(Klass* son) { fSubClassList.push_back(son); }
156 
addDeclCode(const string & str)157     void addDeclCode(const string& str) { fDeclCode.push_back(str); }
158 
addInitCode(const string & str)159     void addInitCode(const string& str) { fInitCode.push_back(str); }
addInitUICode(const string & str)160     void addInitUICode(const string& str) { fInitUICode.push_back(str); }
addClearCode(const string & str)161     void addClearCode(const string& str) { fClearCode.push_back(str); }
162 
addStaticInitCode(const string & str)163     void addStaticInitCode(const string& str) { fStaticInitCode.push_back(str); }
addStaticDestroyCode(const string & str)164     void addStaticDestroyCode(const string& str) { fStaticDestroyCode.push_back(str); }
165 
addStaticFields(const string & str)166     void addStaticFields(const string& str) { fStaticFields.push_back(str); }
167 
addUICode(const string & str)168     void addUICode(const string& str) { fUICode.push_back(str); }
169 
addUIMacro(const string & str)170     void addUIMacro(const string& str) { fUIMacro.push_back(str); }
171 
incUIActiveCount()172     void incUIActiveCount() { fNumActives++; }
incUIPassiveCount()173     void incUIPassiveCount() { fNumPassives++; }
174 
addSharedDecl(const string & str)175     void addSharedDecl(const string& str) { fSharedDecl.push_back(str); }
addFirstPrivateDecl(const string & str)176     void addFirstPrivateDecl(const string& str) { fFirstPrivateDecl.push_back(str); }
177 
addZone1(const string & str)178     void addZone1(const string& str) { fZone1Code.push_back(str); }
addZone2(const string & str)179     void addZone2(const string& str) { fZone2Code.push_back(str); }
addZone2b(const string & str)180     void addZone2b(const string& str) { fZone2bCode.push_back(str); }
addZone2c(const string & str)181     void addZone2c(const string& str) { fZone2cCode.push_back(str); }
addZone3(const string & str)182     void addZone3(const string& str) { fZone3Code.push_back(str); }
addZone4(const string & str)183     void addZone4(const string& str) { fZone4Code.push_back(str); }
184 
addPreCode(const Statement & stmt)185     void addPreCode(const Statement& stmt) { fTopLoop->addPreCode(stmt); }
addExecCode(const Statement & stmt)186     void addExecCode(const Statement& stmt) { fTopLoop->addExecCode(stmt); }
addPostCode(const Statement & stmt)187     void addPostCode(const Statement& stmt) { fTopLoop->addPostCode(stmt); }
188 
189     virtual void println(int n, ostream& fout);
190 
191     virtual void printComputeMethod(int n, ostream& fout);
192     virtual void printComputeMethodScalar(int n, ostream& fout);
193     virtual void printComputeMethodVectorFaster(int n, ostream& fout);
194     virtual void printComputeMethodVectorSimple(int n, ostream& fout);
195     virtual void printComputeMethodOpenMP(int n, ostream& fout);
196     virtual void printComputeMethodScheduler(int n, ostream& fout);
197 
198     virtual void printLoopGraphScalar(int n, ostream& fout);
199     virtual void printLoopGraphVector(int n, ostream& fout);
200     virtual void printLoopGraphOpenMP(int n, ostream& fout);
201     virtual void printLoopGraphScheduler(int n, ostream& fout);
202     virtual void printLoopGraphInternal(int n, ostream& fout);
203     virtual void printGraphDotFormat(ostream& fout);
204 
205     // experimental
206     virtual void printLoopDeepFirst(int n, ostream& fout, Loop* l, set<Loop*>& visited);
207 
208     virtual void printLastLoopLevelScheduler(int n, int lnum, const lset& L, ostream& fout);
209     virtual void printLoopLevelScheduler(int n, int lnum, const lset& L, ostream& fout);
210     virtual void printOneLoopScheduler(lset::const_iterator p, int n, ostream& fout);
211     virtual void printLoopLevelOpenMP(int n, int lnum, const lset& L, ostream& fout);
212 
213     virtual void printMetadata(int n, const MetaDataSet& S, ostream& fout);
214 
215     virtual void printIncludeFile(ostream& fout);
216 
217     virtual void printLibrary(ostream& fout);
218     virtual void printAdditionalCode(ostream& fout);
219 
inputs()220     int inputs() { return fNumInputs; }
outputs()221     int outputs() { return fNumOutputs; }
222 };
223 
224 class SigIntGenKlass : public Klass {
225    public:
SigIntGenKlass(Klass * parent,const string & name)226     SigIntGenKlass(Klass* parent, const string& name) : Klass(name, "", 0, 1, false) { fParentKlass = parent; }
227 
228     virtual void println(int n, ostream& fout);
229 };
230 
231 class SigFloatGenKlass : public Klass {
232    public:
SigFloatGenKlass(Klass * parent,const string & name)233     SigFloatGenKlass(Klass* parent, const string& name) : Klass(name, "", 0, 1, false) { fParentKlass = parent; }
234 
235     virtual void println(int n, ostream& fout);
236 };
237 
238 #endif
239