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 _INSTRUCTIONS_TYPE_H
23 #define _INSTRUCTIONS_TYPE_H
24 
25 #include <string>
26 
27 #include "garbageable.hh"
28 
29 // ============================
30 // Base class for instructions
31 // ============================
32 
33 struct Printable : public virtual Garbageable {
34     static std::ostream* fOut;
35 
PrintablePrintable36     Printable() {}
~PrintablePrintable37     virtual ~Printable() {}
38 };
39 
40 // ========================
41 //  Base classes for types
42 // ========================
43 
44 struct InstVisitor;
45 struct CloneVisitor;
46 
47 struct Typed : public Printable {
48     enum VarType {
49         kInt32,
50         kInt32_ptr,
51         kInt32_vec,
52         kInt32_vec_ptr,
53         kInt64,
54         kInt64_ptr,
55         kInt64_vec,
56         kInt64_vec_ptr,
57         kBool,
58         kBool_ptr,
59         kBool_vec,
60         kBool_vec_ptr,
61         kFloat,
62         kFloat_ptr,
63         kFloat_ptr_ptr,
64         kFloat_vec,
65         kFloat_vec_ptr,
66         kFloatMacro,
67         kFloatMacro_ptr,
68         kFloatMacro_ptr_ptr,
69         kDouble,
70         kDouble_ptr,
71         kDouble_ptr_ptr,
72         kDouble_vec,
73         kDouble_vec_ptr,
74         kQuad,
75         kQuad_ptr,
76         kQuad_ptr_ptr,
77         kQuad_vec,
78         kQuad_vec_ptr,
79         kFixedPoint,
80         kFixedPoint_ptr,
81         kFixedPoint_ptr_ptr,
82         kFixedPoint_vec,
83         kFixedPoint_vec_ptr,
84         kVoid,
85         kVoid_ptr,
86         kVoid_ptr_ptr,
87         kObj,
88         kObj_ptr,
89         kSound,
90         kSound_ptr,
91         kUint_ptr,
92         kNoType
93     };
94 
TypedTyped95     Typed() {}
~TypedTyped96     virtual ~Typed() {}
97 
98     // Returns the pointer type version of a primitive type
getPtrFromTypeTyped99     static VarType getPtrFromType(VarType type)
100     {
101         switch (type) {
102             case kFloatMacro:
103                 return kFloatMacro_ptr;
104             case kFloatMacro_ptr:
105                 return kFloatMacro_ptr_ptr;
106             case kFloat:
107                 return kFloat_ptr;
108             case kFloat_ptr:
109                 return kFloat_ptr_ptr;
110             case kFloat_vec:
111                 return kFloat_vec_ptr;
112             case kInt32:
113                 return kInt32_ptr;
114             case kInt32_vec:
115                 return kInt32_vec_ptr;
116             case kDouble:
117                 return kDouble_ptr;
118             case kDouble_ptr:
119                 return kDouble_ptr_ptr;
120             case kDouble_vec:
121                 return kDouble_vec_ptr;
122             case kQuad:
123                 return kQuad_ptr;
124             case kQuad_ptr:
125                 return kQuad_ptr_ptr;
126             case kQuad_vec:
127                 return kQuad_vec_ptr;
128             case kFixedPoint:
129                 return kFixedPoint_ptr;
130             case kFixedPoint_ptr:
131                 return kFixedPoint_ptr_ptr;
132             case kFixedPoint_vec:
133                 return kFixedPoint_vec_ptr;
134             case kBool:
135                 return kBool_ptr;
136             case kBool_vec:
137                 return kBool_vec_ptr;
138             case kVoid:
139                 return kVoid_ptr;
140             case kVoid_ptr:
141                 return kVoid_ptr_ptr;
142             case kSound:
143                 return kSound_ptr;
144             default:
145                 // Not supposed to happen
146                 std::cerr << "getPtrFromType " << type << std::endl;
147                 faustassert(false);
148                 return kNoType;
149         }
150     }
151 
152     // Returns the vector type version of a primitive type
getVecFromTypeTyped153     static VarType getVecFromType(VarType type)
154     {
155         switch (type) {
156             case kFloat:
157                 return kFloat_vec;
158             case kInt32:
159                 return kInt32_vec;
160             case kDouble:
161                 return kDouble_vec;
162             case kQuad:
163                 return kQuad_vec;
164             case kFixedPoint:
165                 return kFixedPoint_vec;
166             case kBool:
167                 return kBool_vec;
168             default:
169                 // Not supposed to happen
170                 std::cerr << "getVecFromType " << type << std::endl;
171                 faustassert(false);
172                 return kNoType;
173         }
174     }
175 
176     // Returns the type version from pointer on a primitive type
getTypeFromPtrTyped177     static VarType getTypeFromPtr(VarType type)
178     {
179         switch (type) {
180             case kFloatMacro_ptr:
181                 return kFloatMacro;
182             case kFloatMacro_ptr_ptr:
183                 return kFloatMacro_ptr;
184             case kFloat_ptr:
185                 return kFloat;
186             case kFloat_ptr_ptr:
187                 return kFloat_ptr;
188             case kFloat_vec_ptr:
189                 return kFloat_vec;
190             case kInt32_ptr:
191                 return kInt32;
192             case kInt32_vec_ptr:
193                 return kInt32_vec;
194             case kDouble_ptr:
195                 return kDouble;
196             case kDouble_ptr_ptr:
197                 return kDouble_ptr;
198             case kDouble_vec_ptr:
199                 return kDouble_vec;
200             case kQuad_ptr:
201                 return kQuad;
202             case kQuad_ptr_ptr:
203                 return kQuad_ptr;
204             case kQuad_vec_ptr:
205                 return kQuad_vec;
206             case kFixedPoint_ptr:
207                 return kFixedPoint;
208             case kFixedPoint_ptr_ptr:
209                 return kFixedPoint_ptr;
210             case kFixedPoint_vec_ptr:
211                 return kFixedPoint_vec;
212             case kBool_ptr:
213                 return kBool;
214             case kBool_vec_ptr:
215                 return kBool_vec;
216             case kVoid_ptr:
217                 return kVoid;
218             case kVoid_ptr_ptr:
219                 return kVoid_ptr;
220             case kSound_ptr:
221                 return kSound;
222             default:
223                 // Not supposed to happen
224                 std::cerr << "getTypeFromPtr " << Typed::gTypeString[type] << std::endl;
225                 faustassert(false);
226                 return kNoType;
227         }
228     }
229 
230     // Returns the type version from vector on a primitive type
getTypeFromVecTyped231     static VarType getTypeFromVec(VarType type)
232     {
233         switch (type) {
234             case kFloat_vec:
235                 return kFloat;
236             case kInt32_vec:
237                 return kInt32;
238             case kDouble_vec:
239                 return kDouble;
240             case kQuad_vec:
241                 return kQuad;
242             case kFixedPoint_vec:
243                 return kFixedPoint;
244             case kBool_vec:
245                 return kBool;
246             default:
247                 // Not supposed to happen
248                 std::cerr << "getTypeFromVec " << Typed::gTypeString[type] << std::endl;
249                 faustassert(false);
250                 return kNoType;
251         }
252     }
253 
254     static std::string gTypeString[];
255 
256     static void init();
257 
258     virtual VarType getType() const = 0;
259 
260     virtual int getSize() const = 0;
261 
262     virtual void accept(InstVisitor* visitor) = 0;
263 
264     virtual Typed* clone(CloneVisitor* cloner) = 0;
265 };
266 
267 #endif
268