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 _WAST_CODE_CONTAINER_H
23 #define _WAST_CODE_CONTAINER_H
24 
25 #include "code_container.hh"
26 #include "dsp_factory.hh"
27 #include "fir_to_fir.hh"
28 #include "vec_code_container.hh"
29 #include "wast_instructions.hh"
30 #include "json_instructions.hh"
31 
32 using namespace std;
33 
34 class WASTCodeContainer : public virtual CodeContainer {
35    protected:
36     std::ostream*     fOut;
37     std::stringstream fOutAux;
38     std::stringstream fHelper;
39     int               fInternalMemory;
40 
generateWASTBlock(BlockInst * block)41     void generateWASTBlock(BlockInst* block)
42     {
43         // Moves all variables declaration at the beginning of the block
44         MoveVariablesInFront3 mover;
45         BlockInst*            block_res = mover.getCode(block);
46         block_res->accept(gGlobal->gWASTVisitor);
47     }
48 
49     DeclareFunInst* generateInstanceInitFun(const string& name, const string& obj, bool ismethod, bool isvirtual);
50 
51     void generateComputeAux1(int n);
52     void generateComputeAux2(BlockInst* compute_block, int n);
53 
54     template <typename REAL>
generateJSON()55     string generateJSON()
56     {
57         // Prepare compilation options
58         stringstream compile_options;
59         gGlobal->printCompilationOptions(compile_options);
60 
61         // JSON generation
62         JSONInstVisitor<REAL> json_visitor1;
63         generateUserInterface(&json_visitor1);
64 
65         map<string, string>::iterator it;
66         std::map<std::string, int>    path_index_table;
67         map<string, MemoryDesc>&      fieldTable1 = gGlobal->gWASTVisitor->getFieldTable();
68         for (it = json_visitor1.fPathTable.begin(); it != json_visitor1.fPathTable.end(); it++) {
69             faustassert(path_index_table.find((*it).second) == path_index_table.end());
70             // Get field index
71             MemoryDesc tmp                 = fieldTable1[(*it).first];
72             path_index_table[(*it).second] = tmp.fOffset;
73         }
74 
75         // "name", "filename" found in medata
76         JSONInstVisitor<REAL> json_visitor2("", "", fNumInputs, fNumOutputs, -1, "", "", FAUSTVERSION, compile_options.str(),
77                                             gGlobal->gReader.listLibraryFiles(), gGlobal->gImportDirList,
78                                             gGlobal->gWASTVisitor->getStructSize(), path_index_table);
79         generateUserInterface(&json_visitor2);
80         generateMetaData(&json_visitor2);
81 
82         return json_visitor2.JSON(true);
83     }
84 
85    public:
86     WASTCodeContainer(const string& name, int numInputs, int numOutputs, std::ostream* out, bool internal_memory);
~WASTCodeContainer()87     virtual ~WASTCodeContainer() {}
88 
89     virtual void produceClass();
90     virtual void generateCompute(int tab) = 0;
91 
produceInternal()92     void                      produceInternal() {}
93     virtual dsp_factory_base* produceFactory();
94 
95     CodeContainer* createScalarContainer(const string& name, int sub_container_type);
96     CodeContainer* createScalarContainer(const string& name, int sub_container_type, bool internal_memory = true);
97 
98     static CodeContainer* createContainer(const string& name, int numInputs, int numOutputs, std::ostream* dst,
99                                           bool internal_memory);
100 };
101 
102 class WASTScalarCodeContainer : public WASTCodeContainer {
103    protected:
104    public:
105     WASTScalarCodeContainer(const string& name, int numInputs, int numOutputs, std::ostream* out,
106                             int sub_container_type, bool internal_memory);
~WASTScalarCodeContainer()107     virtual ~WASTScalarCodeContainer() {}
108 
109     void generateCompute(int tab);
110 };
111 
112 class WASTVectorCodeContainer : public VectorCodeContainer, public WASTCodeContainer {
113    protected:
114    public:
115     WASTVectorCodeContainer(const string& name, int numInputs, int numOutputs, std::ostream* out, bool internal_memory);
~WASTVectorCodeContainer()116     virtual ~WASTVectorCodeContainer() {}
117 
118     void generateCompute(int tab);
119 };
120 
121 #endif
122