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 _FBC_OPCODE_H
23 #define _FBC_OPCODE_H
24 
25 #include <map>
26 #include <string>
27 
28 // Interpreter
29 
30 struct FBCInstruction {
31     enum Opcode {
32 
33         // Numbers
34         kRealValue,
35         kInt32Value,
36 
37         // Memory
38         kLoadReal,
39         kLoadInt,
40         kLoadSound,
41         kLoadSoundField,
42         kStoreReal,
43         kStoreInt,
44         kStoreSound,
45         kStoreRealValue,
46         kStoreIntValue,
47         kLoadIndexedReal,
48         kLoadIndexedInt,
49         kStoreIndexedReal,
50         kStoreIndexedInt,
51         kBlockStoreReal,
52         kBlockStoreInt,
53         kMoveReal,
54         kMoveInt,
55         kPairMoveReal,
56         kPairMoveInt,
57         kBlockPairMoveReal,
58         kBlockPairMoveInt,
59         kBlockShiftReal,
60         kBlockShiftInt,
61         kLoadInput,
62         kStoreOutput,
63 
64         // Cast/Bitcast
65         kCastReal,
66         kCastInt,
67         kCastRealHeap,
68         kCastIntHeap,
69         kBitcastInt,
70         kBitcastReal,
71 
72         // Standard math (stack OP stack)
73         kAddReal,
74         kAddInt,
75         kSubReal,
76         kSubInt,
77         kMultReal,
78         kMultInt,
79         kDivReal,
80         kDivInt,
81         kRemReal,
82         kRemInt,
83         kLshInt,
84         kARshInt,
85         kLRshInt,
86         kGTInt,
87         kLTInt,
88         kGEInt,
89         kLEInt,
90         kEQInt,
91         kNEInt,
92         kGTReal,
93         kLTReal,
94         kGEReal,
95         kLEReal,
96         kEQReal,
97         kNEReal,
98         kANDInt,
99         kORInt,
100         kXORInt,
101 
102         // Standard math (heap OP heap)
103         kAddRealHeap,
104         kAddIntHeap,
105         kSubRealHeap,
106         kSubIntHeap,
107         kMultRealHeap,
108         kMultIntHeap,
109         kDivRealHeap,
110         kDivIntHeap,
111         kRemRealHeap,
112         kRemIntHeap,
113         kLshIntHeap,
114         kARshIntHeap,
115         kLRshIntHeap,
116         kGTIntHeap,
117         kLTIntHeap,
118         kGEIntHeap,
119         kLEIntHeap,
120         kEQIntHeap,
121         kNEIntHeap,
122         kGTRealHeap,
123         kLTRealHeap,
124         kGERealHeap,
125         kLERealHeap,
126         kEQRealHeap,
127         kNERealHeap,
128         kANDIntHeap,
129         kORIntHeap,
130         kXORIntHeap,
131 
132         // Standard math (heap OP stack)
133         kAddRealStack,
134         kAddIntStack,
135         kSubRealStack,
136         kSubIntStack,
137         kMultRealStack,
138         kMultIntStack,
139         kDivRealStack,
140         kDivIntStack,
141         kRemRealStack,
142         kRemIntStack,
143         kLshIntStack,
144         kARshIntStack,
145         kLRshIntStack,
146         kGTIntStack,
147         kLTIntStack,
148         kGEIntStack,
149         kLEIntStack,
150         kEQIntStack,
151         kNEIntStack,
152         kGTRealStack,
153         kLTRealStack,
154         kGERealStack,
155         kLERealStack,
156         kEQRealStack,
157         kNERealStack,
158         kANDIntStack,
159         kORIntStack,
160         kXORIntStack,
161 
162         // Standard math (heap OP stack)
163         kAddRealStackValue,
164         kAddIntStackValue,
165         kSubRealStackValue,
166         kSubIntStackValue,
167         kMultRealStackValue,
168         kMultIntStackValue,
169         kDivRealStackValue,
170         kDivIntStackValue,
171         kRemRealStackValue,
172         kRemIntStackValue,
173         kLshIntStackValue,
174         kARshIntStackValue,
175         kLRshIntStackValue,
176         kGTIntStackValue,
177         kLTIntStackValue,
178         kGEIntStackValue,
179         kLEIntStackValue,
180         kEQIntStackValue,
181         kNEIntStackValue,
182         kGTRealStackValue,
183         kLTRealStackValue,
184         kGERealStackValue,
185         kLERealStackValue,
186         kEQRealStackValue,
187         kNERealStackValue,
188         kANDIntStackValue,
189         kORIntStackValue,
190         kXORIntStackValue,
191 
192         // Standard math (value OP heap)
193         kAddRealValue,
194         kAddIntValue,
195         kSubRealValue,
196         kSubIntValue,
197         kMultRealValue,
198         kMultIntValue,
199         kDivRealValue,
200         kDivIntValue,
201         kRemRealValue,
202         kRemIntValue,
203         kLshIntValue,
204         kARshIntValue,
205         kLRshIntValue,
206         kGTIntValue,
207         kLTIntValue,
208         kGEIntValue,
209         kLEIntValue,
210         kEQIntValue,
211         kNEIntValue,
212         kGTRealValue,
213         kLTRealValue,
214         kGERealValue,
215         kLERealValue,
216         kEQRealValue,
217         kNERealValue,
218         kANDIntValue,
219         kORIntValue,
220         kXORIntValue,
221 
222         // Standard math (value OP heap) : non commutative operations
223         kSubRealValueInvert,
224         kSubIntValueInvert,
225         kDivRealValueInvert,
226         kDivIntValueInvert,
227         kRemRealValueInvert,
228         kRemIntValueInvert,
229         kLshIntValueInvert,
230         kARshIntValueInvert,
231         kLRshIntValueInvert,
232         kGTIntValueInvert,
233         kLTIntValueInvert,
234         kGEIntValueInvert,
235         kLEIntValueInvert,
236         kGTRealValueInvert,
237         kLTRealValueInvert,
238         kGERealValueInvert,
239         kLERealValueInvert,
240 
241         // Extended unary math
242         kAbs,
243         kAbsf,
244         kAcosf,
245         kAcoshf,
246         kAsinf,
247         kAsinhf,
248         kAtanf,
249         kAtanhf,
250         kCeilf,
251         kCosf,
252         kCoshf,
253         kExpf,
254         kFloorf,
255         kLogf,
256         kLog10f,
257         kRintf,
258         kRoundf,
259         kSinf,
260         kSinhf,
261         kSqrtf,
262         kTanf,
263         kTanhf,
264         kIsnanf, // Not optimized for now, see isExtendedUnaryMath use in interpreter_optimizer.hh
265         kIsinff, // Not optimized for now, see isExtendedUnaryMath use in interpreter_optimizer.hh
266 
267         // Extended unary math (heap OP)
268         kAbsHeap,
269         kAbsfHeap,
270         kAcosfHeap,
271         kAcoshfHeap,
272         kAsinfHeap,
273         kAsinhfHeap,
274         kAtanfHeap,
275         kAtanhfHeap,
276         kCeilfHeap,
277         kCosfHeap,
278         kCoshfHeap,
279         kExpfHeap,
280         kFloorfHeap,
281         kLogfHeap,
282         kLog10fHeap,
283         kRintfHeap,
284         kRoundfHeap,
285         kSinfHeap,
286         kSinhfHeap,
287         kSqrtfHeap,
288         kTanfHeap,
289         kTanhfHeap,
290 
291         // Extended binary math
292         kAtan2f,
293         kFmodf,
294         kPowf,
295         kMax,
296         kMaxf,
297         kMin,
298         kMinf,
299         kCopysignf, // Not optimized for now, see isExtendedBinaryMath use in interpreter_optimizer.hh
300 
301         // Extended binary math (heap OP heap)
302         kAtan2fHeap,
303         kFmodfHeap,
304         kPowfHeap,
305         kMaxHeap,
306         kMaxfHeap,
307         kMinHeap,
308         kMinfHeap,
309 
310         // Extended binary math (heap OP stack)
311         kAtan2fStack,
312         kFmodfStack,
313         kPowfStack,
314         kMaxStack,
315         kMaxfStack,
316         kMinStack,
317         kMinfStack,
318 
319         // Extended binary math (value OP stack)
320         kAtan2fStackValue,
321         kFmodfStackValue,
322         kPowfStackValue,
323         kMaxStackValue,
324         kMaxfStackValue,
325         kMinStackValue,
326         kMinfStackValue,
327 
328         // Extended binary math (value OP heap)
329         kAtan2fValue,
330         kFmodfValue,
331         kPowfValue,
332         kMaxValue,
333         kMaxfValue,
334         kMinValue,
335         kMinfValue,
336 
337         // Extended binary math (value OP heap) : non commutative operations
338         kAtan2fValueInvert,
339         kFmodfValueInvert,
340         kPowfValueInvert,
341 
342         // Control
343         kLoop,
344         kReturn,
345 
346         // Select/if
347         kIf,
348         kSelectReal,
349         kSelectInt,
350         kCondBranch,
351 
352         // User Interface
353         kOpenVerticalBox,
354         kOpenHorizontalBox,
355         kOpenTabBox,
356         kCloseBox,
357         kAddButton,
358         kAddCheckButton,
359         kAddHorizontalSlider,
360         kAddVerticalSlider,
361         kAddNumEntry,
362         kAddSoundfile,
363         kAddHorizontalBargraph,
364         kAddVerticalBargraph,
365         kDeclare,
366 
367         kNop,
368     };
369 
~FBCInstructionFBCInstruction370     virtual ~FBCInstruction() {}
371 
sizeFBCInstruction372     virtual int size() { return 1; }
373 
isRealTypeFBCInstruction374     static bool isRealType(Opcode opt)
375     {
376         return ((opt == kRealValue)
377 
378                 || (opt == kLoadReal) || (opt == kLoadIndexedReal) || (opt == kLoadInput)
379 
380                 || (opt == kCastReal) || (opt == kBitcastReal)
381 
382                 || (opt == kSelectReal)
383 
384                 || (opt == kAddReal)
385                 || (opt == kSubReal)
386                 || (opt == kMultReal)
387                 || (opt == kDivReal)
388                 || (opt == kRemReal)
389 
390                 || (opt == kAbsf)
391                 || (opt == kAcosf) || (opt == kAcoshf)
392                 || (opt == kAsinf) || (opt == kAsinhf)
393                 || (opt == kAtanf) || (opt == kAtanhf)
394                 || (opt == kCeilf) || (opt == kCosf)
395                 || (opt == kCoshf) || (opt == kExpf)
396                 || (opt == kFloorf) || (opt == kLogf)
397                 || (opt == kLog10f) || (opt == kRintf)
398                 || (opt == kRoundf) || (opt == kSinf)
399                 || (opt == kSinhf) || (opt == kSqrtf)
400                 || (opt == kTanf) || (opt == kTanhf)
401 
402                 || (opt == kAtan2f) || (opt == kFmodf)
403                 || (opt == kPowf) || (opt == kMaxf)
404                 || (opt == kMinf) || (opt == kCopysignf));
405     }
406 
isMathFBCInstruction407     static bool isMath(Opcode opt) { return (opt >= kAddReal) && (opt <= kXORInt); }
408     // 'isnan' and 'isinf' not optimized for now, so not included in this test
isExtendedUnaryMathFBCInstruction409     static bool isExtendedUnaryMath(Opcode opt) { return (opt >= kAbs) && (opt <= kTanhf); }
410     // 'copysign' not optimized for now, so not included in this test
isExtendedBinaryMathFBCInstruction411     static bool isExtendedBinaryMath(Opcode opt) { return (opt >= kAtan2f) && (opt <= kMinf); }
isChoiceFBCInstruction412     static bool isChoice(Opcode opt) { return (opt == kIf) || (opt == kSelectReal) || (opt == kSelectInt); }
413 };
414 
415 static std::string gFBCInstructionTable[] = {
416 
417     // Numbers
418     "kRealValue", "kInt32Value",
419 
420     // Memory
421     "kLoadReal", "kLoadInt", "kLoadSound", "kLoadSoundField", "kStoreReal", "kStoreInt", "kStoreSound",
422     "kStoreRealValue", "kStoreIntValue", "kLoadIndexedReal", "kLoadIndexedInt", "kStoreIndexedReal", "kStoreIndexedInt",
423     "kBlockStoreReal", "kBlockStoreInt", "kMoveReal", "kMoveInt", "kPairMoveReal", "kPairMoveInt", "kBlockPairMoveReal",
424     "kBlockPairMoveInt", "kBlockShiftReal", "kBlockShiftInt", "kLoadInput", "kStoreOutput",
425 
426     // Cast/Bitcast
427     "kCastReal", "kCastInt", "kCastRealHeap", "kCastIntHeap", "kBitcastInt", "kBitcastReal",
428 
429     // Standard math (stack OP stack)
430     "kAddReal", "kAddInt", "kSubReal", "kSubInt", "kMultReal", "kMultInt", "kDivReal", "kDivInt", "kRemReal", "kRemInt",
431     "kLshInt", "kARshInt", "kLRshInt", "kGTInt", "kLTInt", "kGEInt", "kLEInt", "kEQInt", "kNEInt", "kGTReal", "kLTReal", "kGEReal",
432     "kLEReal", "kEQReal", "kNEReal", "kANDInt", "kORInt", "kXORInt",
433 
434     // Standard math (heap OP heap)
435     "kAddRealHeap", "kAddIntHeap", "kSubRealHeap", "kSubIntHeap", "kMultRealHeap", "kMultIntHeap", "kDivRealHeap",
436     "kDivIntHeap", "kRemRealHeap", "kRemIntHeap", "kLshIntHeap", "kARshIntHeap", "kLRshIntHeap", "kGTIntHeap", "kLTIntHeap",
437     "kGEIntHeap", "kLEIntHeap", "kEQIntHeap", "kNEIntHeap", "kGTRealHeap", "kLTRealHeap", "kGERealHeap", "kLERealHeap",
438     "kEQRealHeap", "kNERealHeap", "kANDIntHeap", "kORIntHeap", "kXORIntHeap",
439 
440     // Standard math (heap OP stack)
441     "kAddRealStack", "kAddIntStack", "kSubRealStack", "kSubIntStack", "kMultRealStack", "kMultIntStack",
442     "kDivRealStack", "kDivIntStack", "kRemRealStack", "kRemIntStack", "kLshIntStack", "kARshIntStack", "kLRshIntStack", "kGTIntStack",
443     "kLTIntStack", "kGEIntStack", "kLEIntStack", "kEQIntStack", "kNEIntStack", "kGTRealStack", "kLTRealStack",
444     "kGERealStack", "kLERealStack", "kEQRealStack", "kNERealStack", "kANDIntStack", "kORIntStack", "kXORIntStack",
445 
446     // Standard math (value OP stack)
447     "kAddRealStackValue", "kAddIntStackValue", "kSubRealStackValue", "kSubIntStackValue", "kMultRealStackValue",
448     "kMultIntStackValue", "kDivRealStackValue", "kDivIntStackValue", "kRemRealStackValue", "kRemIntStackValue",
449     "kLshIntStackValue", "kARshIntStackValue", "kLRshIntStackValue", "kGTIntStackValue", "kLTIntStackValue", "kGEIntStackValue",
450     "kLEIntStackValue", "kEQIntStackValue", "kNEIntStackValue", "kGTRealStackValue", "kLTRealStackValue",
451     "kGERealStackValue", "kLERealStackValue", "kEQRealStackValue", "kNERealStackValue", "kANDIntStackValue",
452     "kORIntStackValue", "kXORIntStackValue",
453 
454     // Standard math (value OP heap)
455     "kAddRealValue", "kAddIntValue", "kSubRealValue", "kSubIntValue", "kMultRealValue", "kMultIntValue",
456     "kDivRealValue", "kDivIntValue", "kRemRealValue", "kRemIntValue", "kLshIntValue", "kARshIntValue", "kLRshIntValue", "kGTIntValue",
457     "kLTIntValue", "kGEIntValue", "kLEIntValue", "kEQIntValue", "kNEIntValue", "kGTRealValue", "kLTRealValue",
458     "kGERealValue", "kLERealValue", "kEQRealValue", "kNERealValue", "kANDIntValue", "kORIntValue", "kXORIntValue",
459 
460     // Standard math (value OP heap) : non commutative operations
461     "kSubRealValueInvert", "kSubIntValueInvert", "kDivRealValueInvert", "kDivIntValueInvert", "kRemRealValueInvert",
462     "kRemIntValueInvert", "kLshIntValueInvert", "kARshIntValueInvert", "kLRshIntValueInvert", "kGTIntValueInvert", "kLTIntValueInvert",
463     "kLTIntValueInvert", "kLEIntValueInvert", "kGTRealValueInvert", "kLTRealValueInvert", "kGERealValueInvert",
464     "kLERealValueInvert",
465 
466     // Extended unary math
467     "kAbs", "kAbsf", "kAcosf", "kAcoshf", "kAsinf", "kAsinhf", "kAtanf", "kAtanhf",
468     "kCeilf", "kCosf", "kCoshf", "kExpf", "kFloorf", "kLogf", "kLog10f",
469     "kRintf", "kRoundf", "kSinf", "kSinhf", "kSqrtf", "kTanf", "kTanhf", "kIsnanf", "kIsinff",
470 
471     // Extended unary math (heap OP heap)
472     "kAbsHeap", "kAbsfHeap", "kAcosfHeap", "kAcoshfHeap", "kAsinfHeap", "kAsinhfHeap", "kAtanfHeap", "kAtanhfHeap", "kCeilfHeap",
473     "kCosfHeap", "kCoshfHeap", "kExpfHeap", "kFloorfHeap", "kLogfHeap", "kLog10fHeap", "kRintfHeap", "kRoundfHeap", "kSinfHeap",
474     "kSinhfHeap", "kSqrtfHeap", "kTanfHeap", "kTanhfHeap",
475 
476     // Extended binary math
477     "kAtan2f", "kFmodf", "kPowf", "kMax", "kMaxf", "kMin", "kMinf", "kCopysignf",
478 
479     // Extended binary math (heap version)
480     "kAtan2fHeap", "kFmodfHeap", "kPowfHeap", "kMaxHeap", "kMaxfHeap", "kMinHeap", "kMinfHeap",
481 
482     // Extended binary math (stack version)
483     "kAtan2fStack", "kFmodfStack", "kPowfStack", "kMaxStack", "kMaxfStack", "kMinStack", "kMinfStack",
484 
485     // Extended binary math (Stack/Value version)
486     "kAtan2fStackValue", "kFmodfStackValue", "kPowfStackValue", "kMaxStackValue",
487     "kMaxStackfValue", "kMinStackValue", "kMinfStackValue",
488 
489     // Extended binary math (Value version)
490     "kAtan2fValue", "kFmodfValue", "kPowfValue", "kMaxValue", "kMaxfValue", "kMinValue", "kMinfValue",
491 
492     // Extended binary math (Value version) : non commutative operations
493     "kAtan2fValueInvert", "kFmodfValueInvert", "kPowfValueInvert",
494 
495     // Control
496     "kLoop", "kReturn",
497 
498     // Select/if
499     "kIf", "kSelectReal", "kSelectInt", "kCondBranch",
500 
501     // User Interface
502     "kOpenVerticalBox", "kOpenHorizontalBox", "kOpenTabBox", "kCloseBox", "kAddButton", "kAddChecButton",
503     "kAddHorizontalSlider", "kAddVerticalSlider", "kAddNumEntry", "kAddSoundfile", "kAddHorizontalBargraph",
504     "kAddVerticalBargraph", "kDeclare",
505 
506     "kNop"};
507 
508 #define INTERP_FILE_VERSION 8
509 
510 #endif
511