File: klass.cpp

package info (click to toggle)
faust 0.9.46-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 15,256 kB
  • ctags: 9,961
  • sloc: cpp: 47,746; sh: 2,254; ansic: 1,503; makefile: 1,211; ruby: 950; yacc: 468; objc: 459; lex: 200; xml: 177
file content (1215 lines) | stat: -rw-r--r-- 41,751 bytes parent folder | download
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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
/************************************************************************
 ************************************************************************
    FAUST compiler
	Copyright (C) 2003-2004 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 General Public License as published by
    the Free Software Foundation; either version 2 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 General Public License for more details.

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



 /**********************************************************************
			- klass.cpp : class C++ a remplir (projet FAUST) -


		Historique :
		-----------
		17-10-2001 : implementation initiale (yo)
		18-10-2001 : Ajout de getFreshID (yo)
		02-11-2001 : Ajout de sous classes (yo)
		06-11-2001 : modif impression des classes (yo)

***********************************************************************/

#include <stdio.h>
#include <iostream>
#include <sstream>
#include <string>
#include <list>
#include <map>

#include "floats.hh"
#include "smartpointer.hh"
#include "klass.hh"
#include "uitree.hh"
#include "Text.hh"
#include "signals.hh"
#include "ppsig.hh"
#include "recursivness.hh"


extern bool gVectorSwitch;
extern bool gDeepFirstSwitch;
extern bool gOpenMPSwitch;
extern bool gOpenMPLoop;
extern bool gSchedulerSwitch;
extern int  gVecSize;
extern bool gUIMacroSwitch;
extern int  gVectorLoopVariant;
extern bool	gGroupTaskSwitch;

extern map<Tree, set<Tree> > gMetaDataSet;
static int gTaskCount = 0;

void tab (int n, ostream& fout)
{
	fout << '\n';
	while (n--)	fout << '\t';
}

bool Klass::fNeedPowerDef = false;

/**
 * Store the loop used to compute a signal
 */
void Klass::setLoopProperty(Tree sig, Loop* l)
{
    fLoopProperty.set(sig,l);
}

/**
 * Returns the loop used to compute a signal
 */
bool Klass::getLoopProperty(Tree sig, Loop*& l)
{
    return  fLoopProperty.get(sig, l);
}

/**
 * Open a non-recursive loop on top of the stack of open loops.
 * @param size the number of iterations of the loop
 */
void Klass::openLoop(const string& size)
{
    fTopLoop = new Loop(fTopLoop, size);
    //cerr << "\nOPEN SHARED LOOP(" << size << ") ----> " << fTopLoop << endl;
}

/**
 * Open a recursive loop on top of the stack of open loops.
 * @param recsymbol the recursive symbol defined in this loop
 * @param size the number of iterations of the loop
 */
void Klass::openLoop(Tree recsymbol, const string& size)
{
    fTopLoop = new Loop(recsymbol, fTopLoop, size);
    //cerr << "\nOPEN REC LOOP(" << *recsymbol << ", " << size << ") ----> " << fTopLoop << endl;
}

/**
 * Close the top loop and either keep it
 * or absorb it within its enclosing loop.
 */
void Klass::closeLoop(Tree sig)
{
    assert(fTopLoop);
    Loop* l = fTopLoop;
    fTopLoop = l->fEnclosingLoop;
    assert(fTopLoop);

    //l->println(4, cerr);
    //cerr << endl;

    Tree S = symlist(sig);
    //cerr << "CLOSE LOOP :" << l << " with symbols " << *S  << endl;
    if (l->isEmpty() || fTopLoop->hasRecDependencyIn(S)) {
        //cout << " will absorb" << endl;
        // empty or dependent loop -> absorbed by enclosing one
        //cerr << "absorbed by : " << fTopLoop << endl;
        fTopLoop->absorb(l);
        //delete l; HACK !!!
    } else {
        // cout << " will NOT absorb" << endl;
        // we have an independent loop
        setLoopProperty(sig,l);     // associate the signal
        fTopLoop->fBackwardLoopDependencies.insert(l);
        // we need to indicate that all recursive symbols defined
        // in this loop are defined in this loop
        for (Tree lsym=l->fRecSymbolSet; !isNil(lsym); lsym=tl(lsym)) {
            this->setLoopProperty(hd(lsym), l);
            //cerr << "loop " << l << " defines " << *hd(lsym) << endl;
        }
    }
    //cerr << "\n" << endl;
}

/**
 * Print a list of lines.
 */
void printlines(int n, list<string>& lines, ostream& fout)
{
    list<string>::iterator s;
    for (s = lines.begin(); s != lines.end(); s++) {
        tab(n, fout); fout << *s;
    }
}

/**
 * Print a list of elements (e1, e2,...)
 */
void printdecllist(int n, const string& decl, list<string>& content, ostream& fout)
{
    if (!content.empty()) {
        list<string>::iterator s;
        fout << "\\";
        tab(n, fout); fout << decl;
        string sep = "(";
        for (s = content.begin(); s != content.end(); s++) {
            fout << sep << *s;
            sep = ", ";
        }
        fout << ')';
    }
}

/**
 * Print the required C++ libraries as comments in source code
 */
void Klass::printLibrary(ostream& fout)
{
	set<string> S;
	set<string>::iterator f;

	string sep;
	collectLibrary(S);
	fout << "/* link with ";
	for (f = S.begin(), sep =": "; f != S.end(); f++, sep = ", ") 	{
		fout << sep << *f;
	}
	fout << " */\n";
}

/**
 * Print the required include files
 */
void Klass::printIncludeFile(ostream& fout)
{
    set<string> S;
    set<string>::iterator f;

    if (gOpenMPSwitch) {
        fout << "#include <omp.h>" << "\n";
    }

    collectIncludeFile(S);
    for (f = S.begin(); f != S.end(); f++) 	{
        fout << "#include " << *f << "\n";
    }
}

/**
 * Print additional functions required by the generated code
 */
void Klass::printAdditionalCode(ostream& fout)
{
    if (fNeedPowerDef) {
        // Add faustpower definition to C++ code
        fout << "#include <cmath>" << endl;
        fout << "template <int N> inline float faustpower(float x) 		{ return powf(x,N); } " << endl;
        fout << "template <int N> inline double faustpower(double x) 	{ return pow(x,N); }"  << endl;
        fout << "template <int N> inline int faustpower(int x) 			{ return faustpower<N/2>(x) * faustpower<N-N/2>(x); } " << endl;
        fout << "template <> 	 inline int faustpower<0>(int x) 		{ return 1; }" << endl;
        fout << "template <> 	 inline int faustpower<1>(int x) 		{ return x; }" << endl;
    }

}

/**
 * Print metadata declaration
 */
void Klass::printMetadata(int n, const map<Tree, set<Tree> >& S, ostream& fout)
{
    tab(n,fout); fout   << "static void metadata(Meta* m) \t{ ";

    for (map<Tree, set<Tree> >::iterator i = gMetaDataSet.begin(); i != gMetaDataSet.end(); i++) {
        if (i->first != tree("author")) {
            tab(n+1,fout); fout << "m->declare(\"" << *(i->first) << "\", " << **(i->second.begin()) << ");";
        } else {
            for (set<Tree>::iterator j = i->second.begin(); j != i->second.end(); j++) {
                if (j == i->second.begin()) {
                     tab(n+1,fout); fout << "m->declare(\"" << *(i->first) << "\", " << **j << ");" ;
                } else {
                     tab(n+1,fout); fout << "m->declare(\"" << "contributor" << "\", " << **j << ");";
                }
            }
        }
    }

    tab(n,fout); fout << "}" << endl;
}

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

/**
 * Print a loop graph deep first
 */
void Klass::printLoopDeepFirst(int n, ostream& fout, Loop* l, set<Loop*>& visited)
{
	// avoid printing already printed loops
	if (isElement(visited, l)) return;

	// remember we have printed this loop
	visited.insert(l);

	// print the dependencies loops (that need to be computed before this one)
	for (lset::const_iterator p =l->fBackwardLoopDependencies.begin(); p!=l->fBackwardLoopDependencies.end(); p++) {
        printLoopDeepFirst(n, fout, *p, visited);
    }
    // the print the loop itself
    tab(n, fout);
    tab(n, fout); fout << "// LOOP " << l << ", ORDER " << l->fOrder << endl;
    l->println(n+1, fout);
}

/**
 * Compute how many time each loop is used in a DAG
 */
static void computeUseCount(Loop* l)
{
	l->fUseCount++;
	if (l->fUseCount == 1) {
		for (lset::iterator p =l->fBackwardLoopDependencies.begin(); p!=l->fBackwardLoopDependencies.end(); p++) {
		    computeUseCount(*p);
		}
	}
}

/**
 * Group together sequences of loops
 */
static void groupSeqLoops(Loop* l)
{
	int n = l->fBackwardLoopDependencies.size();
	if (n==0) {
		return;
	} else if (n==1) {
		Loop* f = *(l->fBackwardLoopDependencies.begin());
		if (f->fUseCount ==  1) {
			l->concat(f);
			groupSeqLoops(l);
		} else {
			groupSeqLoops(f);
		}
		return;
	} else if (n > 1) {
		for (lset::iterator p =l->fBackwardLoopDependencies.begin(); p!=l->fBackwardLoopDependencies.end(); p++) {
			groupSeqLoops(*p);
		}
	}
}

#define WORK_STEALING_INDEX 0
#define LAST_TASK_INDEX 1
#define START_TASK_INDEX LAST_TASK_INDEX + 1

#define START_TASK_MAX 2

void Klass::buildTasksList()
{
    lgraph G;

    if (gGroupTaskSwitch) {
        computeUseCount(fTopLoop);
        groupSeqLoops(fTopLoop);
    }

    sortGraph(fTopLoop, G);
    int index_task = START_TASK_INDEX;

    addDeclCode("TaskGraph fGraph;");
    addDeclCode("FAUSTFLOAT** input;");
    addDeclCode("FAUSTFLOAT** output;");
    addDeclCode("volatile bool fIsFinished;");
    addDeclCode("int fFullCount;");
    addDeclCode("int fIndex;");
    addDeclCode("DSPThreadPool* fThreadPool;");
    addDeclCode("int fStaticNumThreads;");
    addDeclCode("int fDynamicNumThreads;");

    // Compute forward dependencies
    for (int l=G.size()-1; l>=0; l--) {
        for (lset::const_iterator p =G[l].begin(); p!=G[l].end(); p++) {
            for (lset::const_iterator p1 = (*p)->fBackwardLoopDependencies.begin(); p1!=(*p)->fBackwardLoopDependencies.end(); p1++) {
                (*p1)->fForwardLoopDependencies.insert((*p));
            }
            (*p)->fIndex = index_task;
            index_task++;
        }
    }

    // Compute ready tasks list
    vector<int> task_num;
    for (int l=G.size()-1; l>=0; l--) {
        lset::const_iterator next;
        for (lset::const_iterator p =G[l].begin(); p!=G[l].end(); p++) {
            if ((*p)->fBackwardLoopDependencies.size() == 0) {
                task_num.push_back((*p)->fIndex);
            }
        }
    }

    if (task_num.size() < START_TASK_MAX) {

        // Push ready tasks thread 0, execute one task directly

        addZone3("if (cur_thread == 0) {");

        Loop* keep = NULL;
        for (int l=G.size()-1; l>=0; l--) {
            lset::const_iterator next;
            for (lset::const_iterator p =G[l].begin(); p!=G[l].end(); p++) {
                if ((*p)->fBackwardLoopDependencies.size() == 0) {
                    if (keep == NULL) {
                        keep = *p;
                    } else {
                        addZone3(subst("    taskqueue.PushHead($0);", T((*p)->fIndex)));
                    }
                }
            }
        }

        if (keep != NULL) {
            addZone3(subst("    tasknum = $0;", T(keep->fIndex)));
        }

        addZone3("} else {");
        addZone3("    tasknum = TaskQueue::GetNextTask(cur_thread, fDynamicNumThreads);");
        addZone3("}");

    } else {

        // Cut ready tasks list and have each thread (dynamically) use a subpart
        addZone3(subst("int task_list_size = $0;", T((int)task_num.size())));
        stringstream buf;
        buf << "int task_list[" << task_num.size() << "] = {";
        for(size_t i = 0; i < task_num.size(); i++) {
            buf << task_num[i];
            if (i != (task_num.size() - 1))
                buf << ",";
        }
        buf << "};";

        addZone3(buf.str());
        addZone3("taskqueue.InitTaskList(task_list_size, task_list, fDynamicNumThreads, cur_thread, tasknum);");
    }

    // Last stage connected to end task
    if (G[0].size() > 1) {
        addZone2c("// Initialize end task, if more than one input");
        addZone2c(subst("fGraph.InitTask($0,$1);", T(LAST_TASK_INDEX), T((int)G[0].size())));
    } else {
        addZone2c("// End task has only one input, so will be directly activated");
    }

    // Compute init section
    addZone2c("// Only initialize taks with more than one input");
    for (int l=G.size()-1; l>=0; l--) {
        for (lset::const_iterator p =G[l].begin(); p!=G[l].end(); p++) {
            if ((*p)->fBackwardLoopDependencies.size() > 1)  { // Only initialize taks with more than 1 input, since taks with one input are "directly" activated.
                addZone2c(subst("fGraph.InitTask($0,$1);", T(START_TASK_INDEX + gTaskCount++), T((int)(*p)->fBackwardLoopDependencies.size())));
            } else {
                gTaskCount++;
            }
        }
    }

    addInitCode("fStaticNumThreads = get_max_cpu();");
    addInitCode("fDynamicNumThreads = getenv(\"OMP_NUM_THREADS\") ? atoi(getenv(\"OMP_NUM_THREADS\")) : fStaticNumThreads;");
    addInitCode("fThreadPool = DSPThreadPool::Init();");
    addInitCode("fThreadPool->StartAll(fStaticNumThreads - 1, false);");

    gTaskCount = 0;
}

/**
 * Print the loop graph (used for vector code)
 */
void Klass::printLoopGraphVector(int n, ostream& fout)
{
    if (gGroupTaskSwitch) {
        computeUseCount(fTopLoop);
        groupSeqLoops(fTopLoop);
    }

    lgraph G;
    sortGraph(fTopLoop, G);

#if 1
    // EXPERIMENTAL
    if (gVectorSwitch && gDeepFirstSwitch) {
        set<Loop*> visited;
        printLoopDeepFirst(n, fout, fTopLoop, visited);
        return;
    }
#endif

    // normal mode
    for (int l=G.size()-1; l>=0; l--) {
        if (gVectorSwitch) { tab(n, fout); fout << "// SECTION : " << G.size() - l; }
        for (lset::const_iterator p =G[l].begin(); p!=G[l].end(); p++) {
            (*p)->println(n, fout);
        }
    }
}

/**
 * Print the loop graph as a serie of parallel loops
 */
void Klass::printLoopGraphOpenMP(int n, ostream& fout)
{
    if (gGroupTaskSwitch) {
        computeUseCount(fTopLoop);
        groupSeqLoops(fTopLoop);
    }

    lgraph G;
    sortGraph(fTopLoop, G);

    // OpenMP mode : add OpenMP directives
    for (int l=G.size()-1; l>=0; l--) {
        tab(n, fout); fout << "// SECTION : " << G.size() - l;
        printLoopLevelOpenMP(n, G.size() - l, G[l], fout);
    }
}

/**
 * Print the loop graph as a serie of parallel loops
 */
void Klass::printLoopGraphScheduler(int n, ostream& fout)
{
    if (gGroupTaskSwitch) {
        computeUseCount(fTopLoop);
        groupSeqLoops(fTopLoop);
    }

    lgraph G;
    sortGraph(fTopLoop, G);

    // OpenMP mode : add OpenMP directives
    for (int l=G.size()-1; l>0; l--) {
        tab(n, fout); fout << "// SECTION : " << G.size() - l;
        printLoopLevelScheduler(n, G.size() - l, G[l], fout);
    }

    printLastLoopLevelScheduler(n, G.size(), G[0], fout);
}


/**
 * Print the loop graph in dot format
 */
void Klass::printGraphDotFormat(ostream& fout)
{
    lgraph G;
    sortGraph(fTopLoop, G);

    fout << "strict digraph loopgraph {" << endl;
    fout << '\t' << "rankdir=LR;" << endl;
    fout << '\t' << "node[color=blue, fillcolor=lightblue, style=filled, fontsize=9];" << endl;

    int lnum = 0;       // used for loop numbers
    // for each level of the graph
    for (int l=G.size()-1; l>=0; l--) {
        // for each task in the level
        for (lset::const_iterator t =G[l].begin(); t!=G[l].end(); t++) {
            // print task label "Lxxx : 0xffffff"
            fout << '\t' << 'L'<<(*t)<<"[label=<<font face=\"verdana,bold\">L"<<lnum++<<"</font> : "<<(*t)<<">];"<<endl;
            // for each source of the task
            for (lset::const_iterator src = (*t)->fBackwardLoopDependencies.begin(); src!=(*t)->fBackwardLoopDependencies.end(); src++) {
                // print the connection Lxxx -> Lyyy;
                fout << '\t' << 'L'<<(*src)<<"->"<<'L'<<(*t)<<';'<<endl;
            }
        }
    }
    fout << "}" << endl;
}

/**
 * Print the loop graph (used for internals classes)
 */
void Klass::printLoopGraphInternal(int n, ostream& fout)
{
    lgraph G;
    sortGraph(fTopLoop, G);

    // normal mode
    for (int l=G.size()-1; l>=0; l--) {
        if (gVectorSwitch) { tab(n, fout); fout << "// SECTION : " << G.size() - l; }
        for (lset::const_iterator p =G[l].begin(); p!=G[l].end(); p++) {
            (*p)->printoneln(n, fout);
        }
    }
}

/**
 * Print the loop graph (scalar mode)
 */
void Klass::printLoopGraphScalar(int n, ostream& fout)
{
    fTopLoop->printoneln(n, fout);
}

/**
 * returns true if all the loops are non recursive
 */
static bool nonRecursiveLevel(const lset& L)
{
    for (lset::const_iterator p =L.begin(); p!=L.end(); p++) {
        if ((*p)->fIsRecursive) return false;
    }
    return true;
}

/**
 * Print the 'level' of the loop graph as a set of
 * parallel loops
 */
void Klass::printLoopLevelOpenMP(int n, int lnum, const lset& L, ostream& fout)
{
    if (nonRecursiveLevel(L) && L.size()==1) {
        for (lset::const_iterator p =L.begin(); p!=L.end(); p++) {
            if ((*p)->isEmpty() == false) {
                if (gOpenMPLoop) {
                    (*p)->printParLoopln(n, fout);
                } else {
                    tab(n, fout); fout << "#pragma omp single ";
                    tab(n, fout); fout << "{ ";
                    (*p)->println(n+1, fout);
                    tab(n, fout); fout << "} ";
                }
            }
        }

    } else if (L.size() > 1) {
        tab(n, fout); fout << "#pragma omp sections ";
        tab(n, fout); fout << "{ ";
        for (lset::const_iterator p =L.begin(); p!=L.end(); p++) {
            tab(n+1, fout); fout << "#pragma omp section ";
            tab(n+1, fout); fout << "{";
            (*p)->println(n+2, fout);
            tab(n+1, fout); fout << "} ";
        }
        tab(n, fout); fout << "} ";
    } else if (L.size() == 1 && !(*L.begin())->isEmpty()) {
        tab(n, fout); fout << "#pragma omp single ";
        tab(n, fout); fout << "{ ";
            for (lset::const_iterator p =L.begin(); p!=L.end(); p++) {
                (*p)->println(n+1, fout);
            }
        tab(n, fout); fout << "} ";
    }
}

/**
 * Print the 'level' of the loop graph as a set of
 * parallel loops
 */
void Klass::printLastLoopLevelScheduler(int n, int lnum, const lset& L, ostream& fout)
{
    if (nonRecursiveLevel(L) && L.size() == 1 && !(*L.begin())->isEmpty()) {

        lset::const_iterator p =L.begin();
        tab(n, fout); fout << "case " << gTaskCount++ << ": { ";
        (*p)->println(n+1, fout);
        tab(n+1, fout); fout << "tasknum = LAST_TASK_INDEX;";
        tab(n+1, fout); fout << "break;";
        tab(n, fout); fout << "} ";

    } else if (L.size() > 1) {

        for (lset::const_iterator p =L.begin(); p!=L.end(); p++) {
            tab(n, fout); fout << "case " << gTaskCount++ << ": { ";
            (*p)->println(n+1, fout);
            tab(n+1, fout); fout << "fGraph.ActivateOneOutputTask(taskqueue, LAST_TASK_INDEX, tasknum);";
            tab(n+1, fout); fout << "break;";
            tab(n, fout); fout << "} ";
        }

    } else if (L.size() == 1 && !(*L.begin())->isEmpty()) {

        lset::const_iterator p =L.begin();
        tab(n, fout); fout << "case " << gTaskCount++ << ": { ";
        (*p)->println(n+1, fout);
        tab(n+1, fout); fout << "tasknum = LAST_TASK_INDEX;";
        tab(n+1, fout); fout << "break;";
        tab(n, fout); fout << "} ";

    }
}

void Klass::printOneLoopScheduler(lset::const_iterator p, int n, ostream& fout)
{
    tab(n, fout); fout << "case " << gTaskCount++ << ": { ";
    (*p)->println(n+1, fout);

    // One output only
    if ((*p)->fForwardLoopDependencies.size() == 1) {

        lset::const_iterator p1 = (*p)->fForwardLoopDependencies.begin();
        if ((*p1)->fBackwardLoopDependencies.size () == 1) {
            tab(n+1, fout); fout << subst("tasknum = $0;", T((*p1)->fIndex));
        } else {
            tab(n+1, fout); fout << subst("fGraph.ActivateOneOutputTask(taskqueue, $0, tasknum);", T((*p1)->fIndex));
        }

    } else {

        Loop* keep = NULL;
        // Find one output with only one backward dependencies
        for (lset::const_iterator p1 = (*p)->fForwardLoopDependencies.begin(); p1!=(*p)->fForwardLoopDependencies.end(); p1++) {
            if ((*p1)->fBackwardLoopDependencies.size () == 1) {
                keep = *p1;
                break;
            }
        }

        if (keep == NULL) {
            tab(n+1, fout); fout << "tasknum = WORK_STEALING_INDEX;";
        }

        for (lset::const_iterator p1 = (*p)->fForwardLoopDependencies.begin(); p1!=(*p)->fForwardLoopDependencies.end(); p1++) {
            if ((*p1)->fBackwardLoopDependencies.size () == 1) {  // Task is the only input
                if (*p1 != keep) {
                    tab(n+1, fout); fout << subst("taskqueue.PushHead($0);", T((*p1)->fIndex));
                }
            } else {
                if (keep == NULL) {
                    tab(n+1, fout); fout << subst("fGraph.ActivateOutputTask(taskqueue, $0, tasknum);", T((*p1)->fIndex));
                } else {
                    tab(n+1, fout); fout << subst("fGraph.ActivateOutputTask(taskqueue, $0);", T((*p1)->fIndex));
                }
            }
        }

        if (keep != NULL) {
            tab(n+1, fout); fout << subst("tasknum = $0;", T(keep->fIndex)); // Last one
        } else {
            tab(n+1, fout); fout << "fGraph.GetReadyTask(taskqueue, tasknum);"; // Last one
        }
    }

    tab(n+1, fout); fout << "break;";
    tab(n, fout); fout << "} ";
}

/**
 * Print the 'level' of the loop graph as a set of
 * parallel loops
 */

void Klass::printLoopLevelScheduler(int n, int lnum, const lset& L, ostream& fout)
{
    if (nonRecursiveLevel(L) && L.size() == 1 && !(*L.begin())->isEmpty()) {
        printOneLoopScheduler(L.begin(), n, fout);
    } else if (L.size() > 1) {
        for (lset::const_iterator p = L.begin(); p != L.end(); p++) {
            printOneLoopScheduler(p, n, fout);
        }
    } else if (L.size() == 1 && !(*L.begin())->isEmpty()) {
        printOneLoopScheduler(L.begin(), n, fout);
    }
}

/**
 * Print a full C++ class corresponding to a Faust dsp
 */
void Klass::println(int n, ostream& fout)
{
	list<Klass* >::iterator k;

    tab(n,fout); fout << "#define FAUSTCLASS "<< fKlassName << endl;

    if (gSchedulerSwitch) {
        tab(n,fout); fout << "class " << fKlassName << " : public " << fSuperKlassName << ", public Runnable {";
    } else {
        tab(n,fout); fout << "class " << fKlassName << " : public " << fSuperKlassName << " {";
    }

    if (gUIMacroSwitch) {
        tab(n,fout); fout << "  public:";
    } else {
	    tab(n,fout); fout << "  private:";
    }

    for (k = fSubClassList.begin(); k != fSubClassList.end(); k++) 	(*k)->println(n+1, fout);

    printlines(n+1, fDeclCode, fout);

	tab(n,fout); fout << "  public:";

    printMetadata(n+1, gMetaDataSet, fout);

    if (gSchedulerSwitch) {
        tab(n+1,fout); fout << "virtual ~" << fKlassName << "() \t{ "
                            << "DSPThreadPool::Destroy()"
                            << "; }";
    }

    tab(n+1,fout); fout     << "virtual int getNumInputs() \t{ "
                    << "return " << fNumInputs
                    << "; }";
    tab(n+1,fout); fout 	<< "virtual int getNumOutputs() \t{ "
                    << "return " << fNumOutputs
                    << "; }";

    tab(n+1,fout); fout << "static void classInit(int samplingFreq) {";
        printlines (n+2, fStaticInitCode, fout);
    tab(n+1,fout); fout << "}";

    tab(n+1,fout); fout << "virtual void instanceInit(int samplingFreq) {";
        tab(n+2,fout); fout << "fSamplingFreq = samplingFreq;";
        printlines (n+2, fInitCode, fout);
    tab(n+1,fout); fout << "}";

    tab(n+1,fout); fout << "virtual void init(int samplingFreq) {";
        tab(n+2,fout); fout << "classInit(samplingFreq);";
         tab(n+2,fout); fout << "instanceInit(samplingFreq);";
    tab(n+1,fout); fout << "}";


    tab(n+1,fout); fout << "virtual void buildUserInterface(UI* interface) {";
        printlines (n+2, fUICode, fout);
    tab(n+1,fout); fout << "}";

    printComputeMethod(n, fout);

	tab(n,fout); fout << "};\n" << endl;

	printlines(n, fStaticFields, fout);

	// generate user interface macros if needed
	if (gUIMacroSwitch) {
		tab(n, fout); fout << "#ifdef FAUST_UIMACROS";
            tab(n+1,fout); fout << "#define FAUST_INPUTS " << fNumInputs;
            tab(n+1,fout); fout << "#define FAUST_OUTPUTS " << fNumOutputs;
            tab(n+1,fout); fout << "#define FAUST_ACTIVES " << fNumActives;
            tab(n+1,fout); fout << "#define FAUST_PASSIVES " << fNumPassives;
			printlines(n+1, fUIMacro, fout);
		tab(n, fout); fout << "#endif";
	}

	fout << endl;
}

/**
 * Print Compute() method according to the various switch
 */
void Klass::printComputeMethod(int n, ostream& fout)
{
    if (gSchedulerSwitch) {
        printComputeMethodScheduler (n, fout);
    } else if (gOpenMPSwitch) {
        printComputeMethodOpenMP (n, fout);
    } else if (gVectorSwitch) {
        switch (gVectorLoopVariant) {
            case 0 : printComputeMethodVectorFaster(n, fout); break;
            case 1 : printComputeMethodVectorSimple(n, fout); break;
            default : cerr << "unknown loop variant " << gVectorLoopVariant << endl; exit(1);
        }
   } else {
        printComputeMethodScalar(n, fout);
    }
}

void Klass::printComputeMethodScalar(int n, ostream& fout)
{
    tab(n+1,fout); fout << subst("virtual void compute (int count, $0** input, $0** output) {", xfloat());
        printlines (n+2, fZone1Code, fout);
        printlines (n+2, fZone2Code, fout);
        printlines (n+2, fZone2bCode, fout);
        printlines (n+2, fZone3Code, fout);
        printLoopGraphScalar (n+2,fout);
    tab(n+1,fout); fout << "}";
}

/**
 * Uses loops of constant gVecSize boundary in order to provide the
 * C compiler with more optimisation opportunities. Improves performances
 * in general, but not always
 */
void Klass::printComputeMethodVectorFaster(int n, ostream& fout)
{
    // in vector mode we need to split loops in smaller pieces not larger
    // than gVecSize
    tab(n+1,fout); fout << subst("virtual void compute (int fullcount, $0** input, $0** output) {", xfloat());
        printlines(n+2, fZone1Code, fout);
        printlines(n+2, fZone2Code, fout);
        printlines(n+2, fZone2bCode, fout);

        tab(n+2,fout); fout << "int index;";
        tab(n+2,fout); fout << "for (index = 0; index <= fullcount - " << gVecSize << "; index += " << gVecSize << ") {";
            tab(n+3,fout); fout << "// compute by blocks of " << gVecSize << " samples";
            tab(n+3,fout); fout << "const int count = " << gVecSize << ";";
            printlines (n+3, fZone3Code, fout);
            printLoopGraphVector(n+3,fout);
        tab(n+2,fout); fout << "}";

        tab(n+2,fout); fout << "if (index < fullcount) {";
            tab(n+3,fout); fout << "// compute the remaining samples if any";
            tab(n+3,fout); fout << "int count = fullcount-index;";
            printlines (n+3, fZone3Code, fout);
             printLoopGraphVector(n+3,fout);
        tab(n+2,fout); fout << "}";
    tab(n+1,fout); fout << "}";
}

/**
 * Simple loop layout, generally less efficient than printComputeMethodVectorFaster
 */
void Klass::printComputeMethodVectorSimple(int n, ostream& fout)
{
    // in vector mode we need to split loops in smaller pieces not larger
    // than gVecSize
    tab(n+1,fout); fout << subst("virtual void compute (int fullcount, $0** input, $0** output) {", xfloat());
        printlines(n+2, fZone1Code, fout);
        printlines(n+2, fZone2Code, fout);
        printlines(n+2, fZone2bCode, fout);
        tab(n+2,fout); fout << "for (int index = 0; index < fullcount; index += " << gVecSize << ") {";
            tab(n+3,fout); fout << "int count = min("<< gVecSize << ", fullcount-index);";
            printlines (n+3, fZone3Code, fout);
            printLoopGraphVector(n+3,fout);
        tab(n+2,fout); fout << "}";
    tab(n+1,fout); fout << "}";
}

/*
void Klass::printComputeMethodVectorFix0 (int n, ostream& fout)
{
    // in vector mode we need to split loops in smaller pieces not larger
    // than gVecSize
    tab(n+1,fout); fout << "virtual void compute (int fullcount, float** input, float** output) {";
        printlines(n+2, fZone1Code, fout);
        printlines(n+2, fZone2Code, fout);
        printlines(n+2, fZone2bCode, fout);
        tab(n+2,fout); fout << "for (int index = 0; index < fullcount; index += " << gVecSize << ") {";
            tab(n+3,fout); fout << "if (fullcount >= index + " << gVecSize << ") {";
                tab(n+4,fout); fout << "// compute by blocks of " << gVecSize << " samples";
                tab(n+4,fout); fout << "const int count = " << gVecSize << ";"; // temporaire
                printlines(n+4, fZone3Code, fout);
                printLoopGraph (n+4,fout);
            tab(n+3,fout); fout << "} else if (fullcount > index) {";
                //tab(n+3,fout); fout << "int count = min ("<< gVecSize << ", fullcount-index);";
                tab(n+4,fout); fout << "// compute the remaining samples";
                tab(n+4,fout); fout << "int count = fullcount-index;" ;
                printlines(n+4, fZone3Code, fout);
                printLoopGraph (n+4,fout);
            tab(n+3,fout); fout << "}";
        tab(n+2,fout); fout << "}";
    tab(n+1,fout); fout << "}";
}

void Klass::printComputeMethodVectorFix1 (int n, ostream& fout)
{
    // in vector mode we need to split loops in smaller pieces not larger
    // than gVecSize
    tab(n+1,fout); fout << "virtual void compute (int fullcount, float** input, float** output) {";
        printlines(n+2, fZone1Code, fout);
        printlines(n+2, fZone2Code, fout);
        printlines(n+2, fZone2bCode, fout);

        tab(n+2,fout); fout << "int \tblock;";
        tab(n+2,fout); fout << "for (block = 0; block < fullcount/" << gVecSize << "; block++) {";
            tab(n+3,fout); fout << "// compute by blocks of " << gVecSize << " samples";
            tab(n+3,fout); fout << "const int index = block*" << gVecSize << ";";
            tab(n+3,fout); fout << "const int count = " << gVecSize << ";"; // temporaire
            printlines(n+3, fZone3Code, fout);
            printLoopGraph (n+3,fout);
        tab(n+2,fout); fout << "}";

        tab(n+2,fout); fout << "if (fullcount%" << gVecSize << " != 0) {";
            //tab(n+3,fout); fout << "int count = min ("<< gVecSize << ", fullcount-index);";
            tab(n+3,fout); fout << "// compute the remaining samples";
            tab(n+3,fout); fout << "const int index = block*" << gVecSize << ";";
            tab(n+3,fout); fout << "int count = fullcount%" << gVecSize << ";" ;
            printlines(n+3, fZone3Code, fout);
            printLoopGraph (n+3,fout);
        tab(n+2,fout); fout << "}";
    tab(n+1,fout); fout << "}";
}*/

void Klass::printComputeMethodOpenMP(int n, ostream& fout)
{
    // in openMP mode we need to split loops in smaller pieces not larger
    // than gVecSize and add OpenMP pragmas
    tab(n+1,fout); fout << subst("virtual void compute (int fullcount, $0** input, $0** output) {", xfloat());
        printlines(n+2, fZone1Code, fout);
        printlines(n+2, fZone2Code, fout);
        tab(n+2,fout); fout << "#pragma omp parallel";
        printdecllist(n+3, "firstprivate", fFirstPrivateDecl, fout);

        tab(n+2,fout); fout << "{";
            if (!fZone2bCode.empty()) {
                tab(n+3,fout); fout << "#pragma omp single";
                tab(n+3,fout); fout << "{";
                    printlines(n+4, fZone2bCode, fout);
                tab(n+3,fout); fout << "}";
            }

            tab(n+3,fout); fout << "for (int index = 0; index < fullcount; index += " << gVecSize << ") {";
            tab(n+4,fout); fout << "int count = min ("<< gVecSize << ", fullcount-index);";

            printlines (n+4, fZone3Code, fout);
            printLoopGraphOpenMP (n+4,fout);

            tab(n+3,fout); fout << "}";

        tab(n+2,fout); fout << "}";
    tab(n+1,fout); fout << "}";
}

/*
void Klass::printComputeMethodScheduler (int n, ostream& fout)
{
    tab(n+1,fout); fout << subst("virtual void compute (int fullcount, $0** input, $0** output) {", xfloat());
        printlines (n+2, fZone1Code, fout);
        printlines (n+2, fZone2Code, fout);

        // Init input and output
        tab(n+2,fout); fout << "// Init input and output";
        printlines (n+2, fZone3aCode, fout);
        printlines (n+2, fZone3bCode, fout);

        tab(n+2,fout); fout << "// Init graph state";
        tab(n+2,fout); fout << "initState(fTasksList);";
        tab(n+2,fout); fout << "bool is_finished = false;";
        tab(n+2,fout); fout << "unsigned int index_in = 0;";
        tab(n+2,fout); fout << "unsigned int index_out = 0;";
        tab(n+2,fout); fout << "int count = min ("<< gVecSize << ", fullcount);";

        tab(n+2,fout); fout << "InitSchedulingMap();";
        tab(n+2,fout); fout << "#pragma omp parallel";
        printdecllist(n+3, "firstprivate", fFirstPrivateDecl, fout);

        tab(n+2,fout); fout << "{";
            tab(n+3,fout); fout << "while (!is_finished) {";
                tab(n+4,fout); fout << "Task* task = searchTaskToAcquire(fTasksList);";
                tab(n+4,fout); fout << "if (task != NULL) {";
                    tab(n+5,fout); fout << "bool last_cycle_for_thread = false;";
                    tab(n+5,fout); fout << "do {";
                        tab(n+6,fout); fout << "AddTaskToScheduling(task);";
                        tab(n+6,fout); fout << "switch (task->fNum) {";

                            // DSP tasks
                            printLoopGraph (n+7,fout);

                            // Input task
                            tab(n+7, fout); fout << "case " << gTaskCount++ << ": { ";
                            printlines (n+8, fZone6Code, fout);
                            tab(n+8, fout); fout << "index_in += count;";
                            tab(n+8, fout); fout << "last_cycle_for_thread = (index_in > fullcount);";
                            tab(n+8, fout); fout << "break;";
                            tab(n+7, fout); fout << "} ";

                            // Output task
                            tab(n+7, fout); fout << "case " << gTaskCount++ << ": { ";
                            printlines (n+8, fZone7Code, fout);
                            tab(n+8, fout); fout << "index_out += count;";
                            tab(n+8, fout); fout << "last_cycle_for_thread = (index_out > fullcount);";
                            tab(n+8, fout); fout << "break;";
                            tab(n+7, fout); fout << "} ";

                            // End task
                            tab(n+7, fout); fout << "case " << gTaskCount++ << ": { ";
                            tab(n+8, fout); fout << "is_finished = ((index_in >= fullcount) && (index_out >= fullcount));";
                            tab(n+8, fout); fout << "break;";
                            tab(n+7, fout); fout << "} ";

                        tab(n+6,fout); fout << "}";
                        tab(n+6,fout); fout << "if (last_cycle_for_thread) break;";

                    tab(n+5,fout); fout << "} while ((task = task->concludeAndTryToAcquireNext()) != NULL);";
                tab(n+4,fout); fout << "}";
            tab(n+3,fout); fout << "}";
        tab(n+2,fout); fout << "}";
        tab(n+2,fout); fout << "PrintSchedulingMap();";
    tab(n+1,fout); fout << "}";
}
*/

void Klass::printComputeMethodScheduler (int n, ostream& fout)
{
    tab(n+1,fout); fout << "void display() {";
        tab(n+2,fout); fout << "fGraph.Display();";
    tab(n+1,fout); fout << "}";

    tab(n+1,fout); fout << subst("virtual void compute (int fullcount, $0** input, $0** output) {", xfloat());

        tab(n+2,fout); fout << "GetRealTime();";

        tab(n+2,fout); fout << "this->input = input;";
        tab(n+2,fout); fout << "this->output = output;";

        tab(n+2,fout); fout << "StartMeasure();";

        tab(n+2,fout); fout << "for (fIndex = 0; fIndex < fullcount; fIndex += " << gVecSize << ") {";

        tab(n+3,fout); fout << "fFullCount = min ("<< gVecSize << ", fullcount-fIndex);";
        tab(n+3,fout); fout << "TaskQueue::Init();";
        printlines (n+3, fZone2cCode, fout);

        tab(n+3,fout); fout << "fIsFinished = false;";
        tab(n+3,fout); fout << "fThreadPool->SignalAll(fDynamicNumThreads - 1, this);";
        tab(n+3,fout); fout << "computeThread(0);";
        tab(n+3,fout); fout << "while (!fThreadPool->IsFinished()) {}";

        tab(n+2,fout); fout << "}";

        tab(n+2,fout); fout << "StopMeasure(fStaticNumThreads, fDynamicNumThreads);";

    tab(n+1,fout); fout << "}";

    tab(n+1,fout); fout << "void computeThread(int cur_thread) {";
        printlines (n+2, fZone1Code, fout);
        printlines (n+2, fZone2Code, fout);

        tab(n+2,fout); fout << "// Init graph state";

        tab(n+2,fout); fout << "{";
            tab(n+3,fout); fout << "TaskQueue taskqueue(cur_thread);";
            tab(n+3,fout); fout << "int tasknum = -1;";
            tab(n+3,fout); fout << "int count = fFullCount;";

            // Init input and output
            tab(n+3,fout); fout << "// Init input and output";
            printlines (n+3, fZone3Code, fout);

            tab(n+3,fout); fout << "while (!fIsFinished) {";
                 tab(n+4,fout); fout << "switch (tasknum) {";

                    // Work stealing task
                    tab(n+5, fout); fout << "case WORK_STEALING_INDEX: { ";
                        tab(n+6, fout); fout << "tasknum = TaskQueue::GetNextTask(cur_thread, fDynamicNumThreads);";
                        tab(n+6, fout); fout << "break;";
                    tab(n+5, fout); fout << "} ";

                    // End task
                    tab(n+5, fout); fout << "case LAST_TASK_INDEX: { ";
                        tab(n+6, fout); fout << "fIsFinished = true;";
                        tab(n+6, fout); fout << "break;";
                    tab(n+5, fout); fout << "} ";

                    gTaskCount = START_TASK_INDEX;

                    // DSP tasks
                    printLoopGraphScheduler (n+5,fout);

                 tab(n+4,fout); fout << "}";
            tab(n+3,fout); fout << "}";
        tab(n+2,fout); fout << "}";
    tab(n+1,fout); fout << "}";
}

/**
 * Print an auxillary C++ class corresponding to an integer init signal
 */
void SigIntGenKlass::println(int n, ostream& fout)
{
	list<Klass* >::iterator k;

	tab(n,fout); fout << "class " << fKlassName << " {";

	tab(n,fout); fout << "  private:";
		tab(n+1,fout); fout << "int \tfSamplingFreq;";

		for (k = fSubClassList.begin(); k != fSubClassList.end(); k++) 	(*k)->println(n+1, fout);

		printlines(n+1, fDeclCode, fout);

	tab(n,fout); fout << "  public:";

		tab(n+1,fout); fout 	<< "int getNumInputs() \t{ "
						<< "return " << fNumInputs << "; }";
		tab(n+1,fout); fout 	<< "int getNumOutputs() \t{ "
						<< "return " << fNumOutputs << "; }";

		tab(n+1,fout); fout << "void init(int samplingFreq) {";
			tab(n+2,fout); fout << "fSamplingFreq = samplingFreq;";
		tab(n+1,fout); fout << "}";

		tab(n+1,fout); fout << "void fill (int count, int output[]) {";
            printlines (n+2, fZone1Code, fout);
            printlines (n+2, fZone2Code, fout);
            printlines (n+2, fZone2bCode, fout);
            printlines (n+2, fZone3Code, fout);
            printLoopGraphInternal (n+2,fout);
		tab(n+1,fout); fout << "}";

	tab(n,fout); fout << "};\n" << endl;
}

/**
 * Print an auxillary C++ class corresponding to an float init signal
 */
void SigFloatGenKlass::println(int n, ostream& fout)
{
	list<Klass* >::iterator k;

	tab(n,fout); fout << "class " << fKlassName << " {";

	tab(n,fout); fout << "  private:";
		tab(n+1,fout); fout << "int \tfSamplingFreq;";

		for (k = fSubClassList.begin(); k != fSubClassList.end(); k++) 	(*k)->println(n+1, fout);

		printlines(n+1, fDeclCode, fout);

	tab(n,fout); fout << "  public:";

		tab(n+1,fout); fout 	<< "int getNumInputs() \t{ "
						<< "return " << fNumInputs << "; }";
		tab(n+1,fout); fout 	<< "int getNumOutputs() \t{ "
						<< "return " << fNumOutputs << "; }";

		tab(n+1,fout); fout << "void init(int samplingFreq) {";
			tab(n+2,fout); fout << "fSamplingFreq = samplingFreq;";
			printlines(n+2, fInitCode, fout);
		tab(n+1,fout); fout << "}";

		tab(n+1,fout); fout << subst("void fill (int count, $0 output[]) {", ifloat());
            printlines (n+2, fZone1Code, fout);
            printlines (n+2, fZone2Code, fout);
            printlines (n+2, fZone2bCode, fout);
            printlines (n+2, fZone3Code, fout);
            printLoopGraphInternal(n+2,fout);
 		tab(n+1,fout); fout << "}";

	tab(n,fout); fout << "};\n" << endl;
}

static void merge (set<string>& dst, set<string>& src)
{
	set<string>::iterator i;
	for (i = src.begin(); i != src.end(); i++)  dst.insert(*i);
}

void Klass::collectIncludeFile(set<string>& S)
{
	list<Klass* >::iterator 	k;

	for (k = fSubClassList.begin(); k != fSubClassList.end(); k++) 	(*k)->collectIncludeFile(S);
	merge(S, fIncludeFileSet);
}

void Klass::collectLibrary(set<string>& S)
{
	list<Klass* >::iterator 	k;

	for (k = fSubClassList.begin(); k != fSubClassList.end(); k++) 	(*k)->collectLibrary(S);
	merge(S, fLibrarySet);
}