File: compile_sched.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 (167 lines) | stat: -rw-r--r-- 6,449 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
/************************************************************************
 ************************************************************************
    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.
 ************************************************************************
 ************************************************************************/



#include "compile_sched.hh"
#include "floats.hh"
#include "ppsig.hh"

extern int gVecSize;

void SchedulerCompiler::compileMultiSignal (Tree L)
{
    //contextor recursivness(0);
    L = prepare(L);     // optimize, share and annotate expression
    
    for (int i = 0; i < fClass->inputs(); i++) {
        fClass->addZone3(subst("$1* input$0 = &input[$0][fIndex];", T(i), xfloat()));
    }
    for (int i = 0; i < fClass->outputs(); i++) {
       fClass->addZone3(subst("$1* output$0 = &output[$0][fIndex];", T(i), xfloat()));
    }
                
    fClass->addSharedDecl("fullcount"); 
    fClass->addSharedDecl("input"); 
    fClass->addSharedDecl("output"); 
    
    for (int i = 0; isList(L); L = tl(L), i++) {
        Tree sig = hd(L);
        fClass->openLoop("count");
        fClass->addExecCode(subst("output$0[i] = $2$1;", T(i), CS(sig), xcast()));
        fClass->closeLoop(sig);
    }
    
    // Build tasks list 
    fClass->buildTasksList();
    
    generateUserInterfaceTree(prepareUserInterfaceTree(fUIRoot));
 	generateMacroInterfaceTree("", prepareUserInterfaceTree(fUIRoot));
    if (fDescription) {
        fDescription->ui(prepareUserInterfaceTree(fUIRoot));
    }
}


/**
 * Generate the code for a (short) delay line
 * @param k the c++ class where the delay line will be placed.
 * @param l the loop where the code will be placed.
 * @param tname the name of the C++ type (float or int)
 * @param dlname the name of the delay line (vector) to be used.
 * @param delay the maximum delay
 * @param cexp the content of the signal as a C++ expression 
 */
void  SchedulerCompiler::vectorLoop (const string& tname, const string& vecname, const string& cexp) 
{  
    // -- declare the vector
    fClass->addSharedDecl(vecname);
    
    // -- variables moved as class fields...
    fClass->addDeclCode(subst("$0 \t$1[$2];", tname, vecname, T(gVecSize)));
    
    // -- compute the new samples
    fClass->addExecCode(subst("$0[i] = $1;", vecname, cexp));
}


/**
 * Generate the code for a (short) delay line
 * @param k the c++ class where the delay line will be placed.
 * @param l the loop where the code will be placed.
 * @param tname the name of the C++ type (float or int)
 * @param dlname the name of the delay line (vector) to be used.
 * @param delay the maximum delay
 * @param cexp the content of the signal as a C++ expression 
 */
void  SchedulerCompiler::dlineLoop (const string& tname, const string& dlname, int delay, const string& cexp) 
{
    if (delay < gMaxCopyDelay) {
        
        // Implementation of a copy based delayline
        
	    // create names for temporary and permanent storage  
	    string  buf = subst("$0_tmp", dlname); 			
        string  pmem= subst("$0_perm", dlname);
        
        // constraints delay size to be multiple of 4
        delay = (delay+3)&-4;
        
        // allocate permanent storage for delayed samples
        string  dsize   = T(delay);
        fClass->addDeclCode(subst("$0 \t$1[$2];", tname, pmem, dsize));
        
        // init permanent memory
        fClass->addInitCode(subst("for (int i=0; i<$1; i++) $0[i]=0;", pmem, dsize)); 
        
        // compute method
        
        // -- declare a buffer and a "shifted" vector
        fClass->addSharedDecl(buf);
        
        // -- variables moved as class fields...
        fClass->addDeclCode(subst("$0 \t$1[$2+$3];", tname, buf, T(gVecSize), dsize));
        
        fClass->addFirstPrivateDecl(dlname);
        fClass->addZone2(subst("$0* \t$1 = &$2[$3];", tname, dlname, buf, dsize));
        
        // -- copy the stored samples to the delay line
        fClass->addPreCode(subst("for (int i=0; i<$2; i++) $0[i]=$1[i];", buf, pmem, dsize));
        
        // -- compute the new samples
        fClass->addExecCode(subst("$0[i] = $1;", dlname, cexp));
        
        // -- copy back to stored samples
        fClass->addPostCode(subst("for (int i=0; i<$2; i++) $0[i]=$1[count+i];", pmem, buf, dsize));
        
    } else {
        
        // Implementation of a ring-buffer delayline
        
        // the size should be large enough and aligned on a power of two
        delay   = pow2limit(delay + gVecSize);
        string  dsize   = T(delay);
        string  mask    = T(delay-1);
        
        // create names for temporary and permanent storage  
        string  idx = subst("$0_idx", dlname);
        string  idx_save = subst("$0_idx_save", dlname);
        
        // allocate permanent storage for delayed samples
        fClass->addDeclCode(subst("$0 \t$1[$2];", tname, dlname, dsize));
        fClass->addDeclCode(subst("int \t$0;", idx));
        fClass->addDeclCode(subst("int \t$0;", idx_save));
        
        // init permanent memory
        fClass->addInitCode(subst("for (int i=0; i<$1; i++) $0[i]=0;", dlname, dsize)); 
        fClass->addInitCode(subst("$0 = 0;", idx));
        fClass->addInitCode(subst("$0 = 0;", idx_save));
        
        // -- update index
        fClass->addPreCode(subst("$0 = ($0+$1)&$2;", idx, idx_save, mask));
        
        // -- compute the new samples
        fClass->addExecCode(subst("$0[($2+i)&$3] = $1;", dlname, cexp, idx, mask));
        
        // -- save index
        fClass->addPostCode(subst("$0 = count;", idx_save));
    }
}