File: code_container.hh

package info (click to toggle)
faust 2.81.10%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 431,496 kB
  • sloc: cpp: 283,941; ansic: 116,215; javascript: 18,529; sh: 14,356; vhdl: 14,052; java: 5,900; python: 5,091; objc: 3,852; makefile: 2,725; cs: 1,672; lisp: 1,146; ruby: 954; yacc: 586; xml: 471; lex: 247; awk: 111; tcl: 26
file content (804 lines) | stat: -rw-r--r-- 26,838 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
/************************************************************************
 ************************************************************************
    FAUST compiler
    Copyright (C) 2003-2018 GRAME, Centre National de Creation Musicale
    ---------------------------------------------------------------------
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2.1 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 ************************************************************************
 ************************************************************************/

#ifndef _CODE_CONTAINER_H
#define _CODE_CONTAINER_H

#include <string>

#include "Text.hh"
#include "code_loop.hh"
#include "description.hh"
#include "dsp_factory.hh"
#include "floats.hh"
#include "garbageable.hh"
#include "instructions.hh"
#include "instructions_complexity.hh"
#include "json_instructions.hh"
#include "property.hh"
#include "sigtype.hh"
#include "struct_manager.hh"
#include "tlib.hh"

#ifdef WIN32
#pragma warning(disable : 4250)
#endif

// The name of 'count' parameter
#define fFullCount std::string("count")

#define fTableName std::string("table")

class TextInstVisitor;

// Look for the name of a given subcontainer.
struct SearchSubcontainer : public DispatchVisitor {
    std::string fClassName;
    bool        fFound = false;

    SearchSubcontainer(const std::string& class_name) : fClassName(class_name) {}

    virtual void visit(NamedTyped* typed) { fFound |= (fClassName == typed->getName()); }
};

// DSP or field name, type, size, size-in-bytes, reads, writes
typedef std::vector<MemoryLayoutItem> MemoryLayoutType;

class CodeContainer : public virtual Garbageable {
   protected:
    std::list<CodeContainer*> fSubContainers;
    CodeContainer* fParentContainer;  ///< Container in which this Container is embedded, null if
                                      ///< toplevel Container

    int fNumInputs;
    int fNumOutputs;

    int fNumActives;   ///< number of active controls in the UI (sliders, buttons, etc.)
    int fNumPassives;  ///< number of passive widgets in the UI (bargraphs, etc.)

    int  fSubContainerType;
    bool fGeneratedSR;

    MemoryLayoutType fMemoryLayout;
    std::string      fKlassName;

    // Declaration part
    BlockInst* fExtGlobalDeclarationInstructions;
    BlockInst* fGlobalDeclarationInstructions;

    // For DSP Struct
    BlockInst* fDeclarationInstructions;

    // For control() function
    BlockInst* fControlDeclarationInstructions;

    // Init method
    BlockInst* fInitInstructions;
    BlockInst* fConstantFromMem;
    BlockInst* fConstantToMem;
    BlockInst* fResetUserInterfaceInstructions;
    BlockInst* fClearInstructions;
    BlockInst* fPostInitInstructions;

    // To be used in allocate method (or constructor)
    BlockInst* fAllocateInstructions;

    // To be used in delete method
    BlockInst* fDestroyInstructions;

    // Static init method
    BlockInst* fStaticInitInstructions;
    BlockInst* fPostStaticInitInstructions;

    // Static destroy method
    BlockInst* fStaticDestroyInstructions;

    // Compute method
    BlockInst* fComputeBlockInstructions;      // Before DSP loop
    BlockInst* fPostComputeBlockInstructions;  // After DSP loop

    // Additional functions generated in -fun mode
    BlockInst* fComputeFunctions;

    // User interface
    BlockInst* fUserInterfaceInstructions;

    std::set<std::string> fIncludeFileSet;
    std::set<std::string> fLibrarySet;

    // DAG of loops
    CodeLoop* fCurLoop;

    property<CodeLoop*> fLoopProperty;  ///< loops used to compute some signals

    // UI handling
    std::list<std::string> fUICode;
    std::list<std::string> fUIMacro;
    std::list<std::string> fUIMacroActives;
    std::list<std::string> fUIMacroPassives;

    void merge(std::set<std::string>& dst, std::set<std::string>& src)
    {
        for (const auto& i : src) {
            dst.insert(i);
        }
    }

    void collectIncludeFile(std::set<std::string>& S)
    {
        for (const auto& k : fSubContainers) {
            k->collectIncludeFile(S);
        }
        merge(S, fIncludeFileSet);
    }

    void collectLibrary(std::set<std::string>& S)
    {
        for (const auto& k : fSubContainers) {
            k->collectLibrary(S);
        }
        merge(S, fLibrarySet);
    }

    void generateDAGLoopAux(CodeLoop* loop, BlockInst* loop_code, ValueInst* count, int loop_num,
                            bool omp = false);
    void generateDAGLoopInternal(CodeLoop* loop, BlockInst* block, ValueInst* count, bool omp);

    void includeFastMath()
    {
        if (gGlobal->gFastMathLib == "def") {
            addIncludeFile("\"faust/dsp/fastmath.cpp\"");
        } else if (gGlobal->gFastMathLib == "arch") {
            // Nothing
        } else {
            addIncludeFile("\"" + gGlobal->gFastMathLib + "\"");
        }
    }

    void printHeader(std::ostream& dst)
    {
        // defines the metadata we want to print as comments at the begin of in the file
        std::set<Tree> selectedKeys;
        selectedKeys.insert(tree("name"));
        selectedKeys.insert(tree("author"));
        selectedKeys.insert(tree("copyright"));
        selectedKeys.insert(tree("license"));
        selectedKeys.insert(tree("version"));

        dst << "/* ------------------------------------------------------------" << std::endl;
        for (const auto& i : gGlobal->gMetaDataSet) {
            if (selectedKeys.count(i.first)) {
                dst << *(i.first);
                const char* sep = ": ";
                for (const auto& j : i.second) {
                    dst << sep << *j;
                    sep = ", ";
                }
                dst << std::endl;
            }
        }

        dst << "Code generated with Faust " << FAUSTVERSION << " (https://faust.grame.fr)"
            << std::endl;
        dst << "Compilation options: ";
        dst << gGlobal->printCompilationOptions1();
        dst << "\n------------------------------------------------------------ */" << std::endl;
    }

    virtual void generateSR()
    {
        if (!fGeneratedSR) {
            pushDeclare(IB::genDecStructVar("fSampleRate", IB::genInt32Typed()));
        }
        pushPreInitMethod(
            IB::genStoreStructVar("fSampleRate", IB::genLoadFunArgsVar("sample_rate")));
    }

    BlockInst* inlineSubcontainersFunCalls(BlockInst* block);

    Names genMethod(const std::string& obj, bool ismethod)
    {
        Names args;
        if (!ismethod) {
            args.push_back(IB::genNamedTyped(obj, Typed::kObj_ptr));
        }
        return args;
    }

    Values genObjArg(const std::string& obj, bool ismethod)
    {
        Values args;
        if (!ismethod) {
            args.push_back(IB::genLoadFunArgsVar(obj));
        }
        return args;
    }

    void createMemoryLayout();
    void rewriteInZones();

   public:
    CodeContainer();
    void initialize(int numInputs, int numOutputs);
    virtual ~CodeContainer();

    void printMacros(std::ostream& fout, int n);

    CodeLoop* getCurLoop() { return fCurLoop; }

    void           setParentContainers(CodeContainer* parent) { fParentContainer = parent; }
    CodeContainer* getParentContainer() { return fParentContainer; }
    CodeContainer* getTopParentContainer()
    {
        return (fParentContainer) ? fParentContainer->getTopParentContainer() : this;
    }

    // Returns the name of the class
    std::string getFullClassName()
    {
        return (fParentContainer) ? (fParentContainer->getFullClassName() + "::" + getClassName())
                                  : getClassName();
    }

    void setGeneratedSR() { fGeneratedSR = true; }

    void openLoop(const std::string& index_name, int size = 0);
    void openLoop(Tree recsymbol, const std::string& index_name, int size = 0);
    void closeLoop(Tree sig);

    int inputs() { return fNumInputs; }
    int outputs() { return fNumOutputs; }

    void setInputs(int inputs) { fNumInputs = inputs; }
    void setOutputs(int outputs) { fNumOutputs = outputs; }

    void addSubContainer(CodeContainer* container) { fSubContainers.push_back(container); }

    void addIncludeFile(const std::string& str) { fIncludeFileSet.insert(str); }
    void addLibrary(const std::string& str) { fLibrarySet.insert(str); }

    void printLibrary(std::ostream& fout);
    void printIncludeFile(std::ostream& fout);

    void setLoopProperty(Tree sig, CodeLoop* l);   ///< Store the loop used to compute a signal
    bool getLoopProperty(Tree sig, CodeLoop*& l);  ///< Returns the loop used to compute a signal
    void listAllLoopProperties(
        Tree            sig, std::set<CodeLoop*>&,
        std::set<Tree>& visited);  ///< Returns all the loop used to compute a signal

    void printGraphDotFormat(std::ostream& fout);

    void transformDAG(DispatchVisitor* visitor);
    void computeForwardDAG(lclgraph dag, int& loop_count, std::vector<int>& ready_loop);
    void sortDeepFirstDAG(CodeLoop* l, std::set<CodeLoop*>& visited, std::list<CodeLoop*>& result);

    // Should be implemented in subclasses
    virtual void generateLocalInputs(BlockInst* loop_code, const std::string& index)
    {
        faustassert(false);
    }
    virtual void generateLocalOutputs(BlockInst* loop_code, const std::string& index)
    {
        faustassert(false);
    }

    virtual DeclareFunInst* generateAllocate(const std::string& name, const std::string& obj,
                                             bool ismethod, bool isvirtual);
    virtual DeclareFunInst* generateDestroy(const std::string& name, const std::string& obj,
                                            bool ismethod, bool isvirtual);

    DeclareFunInst* generateGetIO(const std::string& name, const std::string& obj, int io,
                                  bool ismethod, FunTyped::FunAttribute funtype);
    DeclareFunInst* generateGetInputs(const std::string& name, const std::string& obj,
                                      bool ismethod, FunTyped::FunAttribute funtype);
    DeclareFunInst* generateGetOutputs(const std::string& name, const std::string& obj,
                                       bool ismethod, FunTyped::FunAttribute funtype);

    DeclareFunInst* generateGetIORate(const std::string& name, const std::string& obj,
                                      std::vector<int>& io, bool ismethod, bool isvirtual);

    virtual DeclareFunInst* generateClassInit(const std::string& name)
    {
        faustassert(false);
        return nullptr;
    }
    virtual DeclareFunInst* generateInstanceClear(const std::string& name, const std::string& obj,
                                                  bool ismethod, bool isvirtual);

    virtual DeclareFunInst* generateInstanceConstants(const std::string& name,
                                                      const std::string& obj, bool ismethod,
                                                      bool isvirtual);

    virtual DeclareFunInst* generateInstanceResetUserInterface(const std::string& name,
                                                               const std::string& obj,
                                                               bool ismethod, bool isvirtual)
    {
        faustassert(false);
        return nullptr;
    }

    virtual DeclareFunInst* generateComputeFun(const std::string& name, const std::string& obj,
                                               bool ismethod, bool isvirtual);

    virtual BlockInst* generateComputeAux()
    {
        faustassert(false);
        return nullptr;
    }

    virtual DeclareFunInst* generateStaticInitFun(const std::string& name, bool isstatic);
    virtual DeclareFunInst* generateInstanceInitFun(const std::string& name, const std::string& obj,
                                                    bool ismethod, bool isvirtual);
    virtual DeclareFunInst* generateFillFun(const std::string& name, const std::string& obj,
                                            bool ismethod, bool isvirtual);

    DeclareFunInst* generateInit(const std::string& name, const std::string& obj, bool ismethod,
                                 bool isvirtual);
    DeclareFunInst* generateInstanceInit(const std::string& name, const std::string& obj,
                                         bool ismethod, bool isvirtual);
    DeclareFunInst* generateGetSampleRate(const std::string& name, const std::string& obj,
                                          bool ismethod, bool isvirtual);

    DeclareFunInst* generateCalloc();
    DeclareFunInst* generateFree();

    DeclareFunInst* generateNewDsp(const std::string& name, int size);
    DeclareFunInst* generateDeleteDsp(const std::string& name, const std::string& obj);

    void produceInfoFunctions(int tabs, const std::string& classname, const std::string& obj,
                              bool ismethod, FunTyped::FunAttribute funtype,
                              TextInstVisitor* producer, const std::string& in_fun = "getNumInputs",
                              const std::string& out_fun = "getNumOutputs");

    void generateDAGLoop(BlockInst* loop_code, ValueInst* count);

    template <typename REAL>
    void generateMetaData(JSONUIReal<REAL>* json)
    {
        // Add global metadata
        for (const auto& i : gGlobal->gMetaDataSet) {
            if (i.first != tree("author")) {
                std::stringstream str1, str2;
                str1 << *(i.first);
                str2 << **(i.second.begin());
                std::string res1 = str1.str();
                std::string res2 = unquote(str2.str());
                json->declare(res1.c_str(), res2.c_str());
            } else {
                for (std::set<Tree>::iterator j = i.second.begin(); j != i.second.end(); j++) {
                    if (j == i.second.begin()) {
                        std::stringstream str1, str2;
                        str1 << *(i.first);
                        str2 << **j;
                        std::string res1 = str1.str();
                        std::string res2 = unquote(str2.str());
                        json->declare(res1.c_str(), res2.c_str());
                    } else {
                        std::stringstream str2;
                        str2 << **j;
                        std::string res2 = unquote(str2.str());
                        json->declare("contributor", res2.c_str());
                    }
                }
            }
        }
    }

    template <typename REAL>
    void generateJSONFile()
    {
        JSONInstVisitor<REAL> visitor;
        generateJSON(&visitor);
        std::ofstream xout(subst("$0.json", gGlobal->makeDrawPath()).c_str());
        xout << visitor.JSON();
    }

    template <typename REAL>
    void generateJSON(JSONInstVisitor<REAL>* visitor)
    {
        // Prepare instructions complexity
        ForLoopInst*          loop = fCurLoop->generateScalarLoop("count");
        InstComplexityVisitor complexity;
        loop->accept(&complexity);

        // Get the DSP size
        StructInstVisitor struct_visitor;
        fDeclarationInstructions->accept(&struct_visitor);

        // "name", "filename" found in medata
        visitor->init("", "", fNumInputs, fNumOutputs, -1, "", "", FAUSTVERSION,
                      gGlobal->printCompilationOptions1(), gGlobal->gReader.listLibraryFiles(),
                      gGlobal->gImportDirList, struct_visitor.getStructSize(), PathTableType(),
                      fMemoryLayout, complexity.getInstComplexity());
        generateUserInterface(visitor);
        generateMetaData(visitor);
    }

    template <typename REAL>
    std::string generateJSON()
    {
        JSONInstVisitor<REAL> visitor;
        generateJSON(&visitor);
        return visitor.JSON(true);
    }

    std::string generateJSONAux()
    {
        if (gGlobal->gFloatSize == 1) {
            return generateJSON<float>();
        } else {
            return generateJSON<double>();
        }
    }

    /* Can be overridden by subclasses to transform the FIR before the actual code generation */
    virtual void processFIR(void);

    /* Create a single block containing all FIR */
    virtual BlockInst* flattenFIR(void);

    // Fill code for each method
    StatementInst* pushDeclare(StatementInst* inst)
    {
        faustassert(inst);
        fDeclarationInstructions->pushBackInst(inst);
        // TODO : add inter-loop vectors in current loop
        return inst;
    }

    StatementInst* pushGlobalDeclare(StatementInst* inst)
    {
        faustassert(inst);
        fGlobalDeclarationInstructions->pushBackInst(inst);
        return inst;
    }

    StatementInst* pushExtGlobalDeclare(StatementInst* inst)
    {
        faustassert(inst);
        fExtGlobalDeclarationInstructions->pushBackInst(inst);
        return inst;
    }

    StatementInst* pushControlDeclare(StatementInst* inst)
    {
        faustassert(inst);
        fControlDeclarationInstructions->pushBackInst(inst);
        return inst;
    }

    ValueInst* pushFunction(const std::string& name, Typed::VarType result,
                            std::vector<Typed::VarType>& types, const Values& args);

    void generateExtGlobalDeclarations(InstVisitor* visitor)
    {
        if (fExtGlobalDeclarationInstructions->fCode.size() > 0) {
            fExtGlobalDeclarationInstructions->accept(visitor);
        }
    }

    void generateGlobalDeclarations(InstVisitor* visitor)
    {
        if (fGlobalDeclarationInstructions->fCode.size() > 0) {
            fGlobalDeclarationInstructions->accept(visitor);
        }
    }

    void generateDeclarations(InstVisitor* visitor)
    {
        if (fDeclarationInstructions->fCode.size() > 0) {
            fDeclarationInstructions->accept(visitor);
        }
    }

    void generateControlDeclarations(InstVisitor* visitor)
    {
        if (fControlDeclarationInstructions->fCode.size() > 0) {
            fControlDeclarationInstructions->accept(visitor);
        }
    }

    void generateInit(InstVisitor* visitor)
    {
        if (fInitInstructions->fCode.size() > 0) {
            fInitInstructions->accept(visitor);
        }

        if (fPostInitInstructions->fCode.size() > 0) {
            fPostInitInstructions->accept(visitor);
        }
    }

    void generateResetUserInterface(InstVisitor* visitor)
    {
        if (fResetUserInterfaceInstructions->fCode.size() > 0) {
            fResetUserInterfaceInstructions->accept(visitor);
        }
    }

    void generateClear(InstVisitor* visitor)
    {
        if (fClearInstructions->fCode.size() > 0) {
            fClearInstructions->accept(visitor);
        }
    }

    void generateStaticInit(InstVisitor* visitor)
    {
        if (fStaticInitInstructions->fCode.size() > 0) {
            fStaticInitInstructions->accept(visitor);
        }

        if (fPostStaticInitInstructions->fCode.size() > 0) {
            fPostStaticInitInstructions->accept(visitor);
        }
    }

    void generateStaticDestroy(InstVisitor* visitor)
    {
        if (fStaticDestroyInstructions->fCode.size() > 0) {
            fStaticDestroyInstructions->accept(visitor);
        }
    }

    void generateUserInterface(InstVisitor* visitor)
    {
        if (fUserInterfaceInstructions->fCode.size() > 0) {
            fUserInterfaceInstructions->accept(visitor);
        }
    }

    void generateComputeFunctions(InstVisitor* visitor)
    {
        if (fComputeFunctions->fCode.size() > 0) {
            fComputeFunctions->accept(visitor);
        }
    }

    void generateComputeBlock(InstVisitor* visitor)
    {
        if (fComputeBlockInstructions->fCode.size() > 0) {
            fComputeBlockInstructions->accept(visitor);
        }
    }

    void generatePostComputeBlock(InstVisitor* visitor)
    {
        if (fPostComputeBlockInstructions->fCode.size() > 0) {
            fPostComputeBlockInstructions->accept(visitor);
        }
    }

    void generateAllocate(InstVisitor* visitor)
    {
        if (fAllocateInstructions->fCode.size() > 0) {
            fAllocateInstructions->accept(visitor);
        }
    }

    void generateDestroy(InstVisitor* visitor)
    {
        if (fDestroyInstructions->fCode.size() > 0) {
            fDestroyInstructions->accept(visitor);
        }
    }

    StatementInst* pushInitMethod(StatementInst* inst)
    {
        faustassert(inst);
        fInitInstructions->pushBackInst(inst);
        return inst;
    }
    StatementInst* pushClearMethod(StatementInst* inst)
    {
        faustassert(inst);
        fClearInstructions->pushBackInst(inst);
        return inst;
    }
    StatementInst* pushResetUIInstructions(StatementInst* inst)
    {
        faustassert(inst);
        fResetUserInterfaceInstructions->pushBackInst(inst);
        return inst;
    }
    StatementInst* pushPostInitMethod(StatementInst* inst)
    {
        faustassert(inst);
        fPostInitInstructions->pushBackInst(inst);
        return inst;
    }
    StatementInst* pushPreInitMethod(StatementInst* inst)
    {
        faustassert(inst);
        fInitInstructions->pushFrontInst(inst);
        return inst;
    }

    StatementInst* pushUserInterfaceMethod(StatementInst* inst)
    {
        faustassert(inst);
        fUserInterfaceInstructions->pushBackInst(inst);
        return inst;
    }

    BlockInst* removeEmptyGroupsAux(BlockInst* ui, bool& remove)
    {
        bool       remove_tmp = false;
        BlockInst* res        = IB::genBlockInst();

        auto it = ui->fCode.begin();
        while (it != ui->fCode.end()) {
            // Check if current item is OpenboxInst and the next item is CloseboxInst
            if (dynamic_cast<OpenboxInst*>(*it) && dynamic_cast<CloseboxInst*>(*std::next(it))) {
                // If they are consecutive, skip both
                it         = std::next(it, 2);  // Move iterator forward by 2
                remove_tmp = true;
            } else {
                // Otherwise, add the current item to the result
                res->pushBackInst(*it);
                ++it;  // Move iterator to the next item
            }
        }
        remove = remove_tmp;
        return res;
    }

    BlockInst* removeEmptyGroups(BlockInst* ui)
    {
        bool       removed = false;
        BlockInst* ui_tmp  = ui;
        do {
            ui_tmp = removeEmptyGroupsAux(ui_tmp, removed);
        } while (removed);
        return ui_tmp;
    }

    StatementInst* pushPreUserInterfaceMethod(StatementInst* inst)
    {
        faustassert(inst);
        fUserInterfaceInstructions->pushFrontInst(inst);
        return inst;
    }

    StatementInst* pushAllocateMethod(StatementInst* inst)
    {
        faustassert(inst);
        fAllocateInstructions->pushBackInst(inst);
        return inst;
    }
    StatementInst* pushDestroyMethod(StatementInst* inst)
    {
        faustassert(inst);
        fDestroyInstructions->pushBackInst(inst);
        return inst;
    }

    StatementInst* pushStaticInitMethod(StatementInst* inst)
    {
        faustassert(inst);
        fStaticInitInstructions->pushBackInst(inst);
        return inst;
    }
    StatementInst* pushStaticDestroyMethod(StatementInst* inst)
    {
        faustassert(inst);
        fStaticDestroyInstructions->pushBackInst(inst);
        return inst;
    }
    StatementInst* pushPostStaticInitMethod(StatementInst* inst)
    {
        faustassert(inst);
        fPostStaticInitInstructions->pushBackInst(inst);
        return inst;
    }

    StatementInst* pushComputeBlockMethod(StatementInst* inst)
    {
        faustassert(inst);
        fComputeBlockInstructions->pushBackInst(inst);
        return inst;
    }
    StatementInst* pushPostComputeBlockMethod(StatementInst* inst)
    {
        faustassert(inst);
        fPostComputeBlockInstructions->pushBackInst(inst);
        return inst;
    }

    StatementInst* pushOtherComputeMethod(StatementInst* inst)
    {
        faustassert(inst);
        fComputeFunctions->pushBackInst(inst);
        return inst;
    }

    StatementInst* pushPreComputeDSPMethod(StatementInst* inst)
    {
        faustassert(inst);
        return fCurLoop->pushPreComputeDSPMethod(inst);
    }
    StatementInst* pushComputeDSPMethod(StatementInst* inst)
    {
        faustassert(inst);
        return fCurLoop->pushComputeDSPMethod(inst);
    }
    StatementInst* pushPostComputeDSPMethod(StatementInst* inst)
    {
        faustassert(inst);
        return fCurLoop->pushPostComputeDSPMethod(inst);
    }

    void generateSubContainers()
    {
        for (const auto& it : fSubContainers) {
            it->produceInternal();
        }
    }

    // merge declaration part
    void mergeSubContainers();

    size_t getSubContainers() { return fSubContainers.size(); }

    const std::string getTableName() { return fTableName; }
    const std::string getClassName() { return fKlassName; }
    const std::string getFaustPowerName() { return fKlassName + "_faustpower"; }

    // UI construction
    void addUIMacro(const std::string& str) { fUIMacro.push_back(str); }
    void addUIMacroActives(const std::string& str) { fUIMacroActives.push_back(str); }
    void addUIMacroPassives(const std::string& str) { fUIMacroPassives.push_back(str); }
    void addUICode(const std::string& str) { fUICode.push_back(str); }

    void incUIActiveCount() { fNumActives++; }
    void incUIPassiveCount() { fNumPassives++; }

    virtual CodeContainer* createScalarContainer(const std::string& name,
                                                 int                sub_container_type) = 0;

    virtual void produceInternal() = 0;

    virtual void printHeader() {}
    virtual void printFloatDef() {}
    virtual void printFooter() {}
    virtual void produceClass() {}

    virtual void dump(std::ostream* dst) {}

    virtual dsp_factory_base* produceFactory()
    {
        faustassert(false);
        return nullptr;
    }

    void generateJSONFile();

    // Used in -ec mode
    ControlArray* fIntControl;   // array of 'int32' intermediate control values
    ControlArray* fRealControl;  // array of 'real' intermediate control values
};

inline bool isElement(const std::set<CodeLoop*>& S, CodeLoop* l)
{
    return S.find(l) != S.end();
}

#endif