File: sigRecursiveDependencies.cpp

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 (201 lines) | stat: -rw-r--r-- 6,136 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
/************************************************************************
 ************************************************************************
    FAUST compiler
    Copyright (C) 2024 INRIA, Institut national de recherche en sciences
                              et technologies du numérique
    ---------------------------------------------------------------------
    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.
 ************************************************************************
 ************************************************************************/

/************************************************************************
 * @file sigRecursiveDependencies.cpp
 * @author Yann Orlarey (yann.orlarey@inria.fr)
 * @brief Compute and check the recursive dependencies of a signal
 * @version 0.1
 * @date 2024-03-15
 *
 * @copyright Copyright (c) 2024 INRIA
 *
 ***********************************************************************/

#include "sigRecursiveDependencies.hh"

#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

#include "global.hh"
#include "ppsig.hh"
#include "signals.hh"

// Uncomment to activate type inferrence tracing
#if 0
#define TRACE(x) x
#else
#define TRACE(x) \
    {            \
        ;        \
    }
#endif

/**
 * @brief Compute the set of dependencies of a signal
 *
 * @param underVisit, stack of projections under visit (prevent infinite loops)
 * @param sig
 * @return std::set<Tree> set of dependencies of sig
 */
static std::set<Tree> sigDependencies(std::vector<Tree>& underVisit, Tree sig)
{
    int  i;
    Tree rec, id, le;
    if (gGlobal->gDependencies.count(sig)) {
        // 1) the dependencies of sig have already been computed
        return gGlobal->gDependencies[sig];
    } else if (isProj(sig, &i, rec)) {
        // 2) sig is a projection, its dependencies are itself and the dependecies of its definition
        std::set<Tree> deps;
        if (std::find(underVisit.begin(), underVisit.end(), sig) == underVisit.end()) {
            // we mark the projection under visit and compute the dependencies of its definition
            underVisit.push_back(sig);
            faustassert(isRec(rec, id, le));
            deps = sigDependencies(underVisit, nth(le, i));
            underVisit.pop_back();
        }
        deps.insert(sig);  // insert the projection itself
        gGlobal->gDependencies[sig] = deps;
        return deps;

    } else {
        // 3) sig is not a projection but is any other expression,
        // its dependencies are the dependencies of its branches
        std::set<Tree> deps;
        for (Tree b : sig->branches()) {
            std::set<Tree> depsb = sigDependencies(underVisit, b);
            deps.insert(depsb.begin(), depsb.end());
        }
        gGlobal->gDependencies[sig] = deps;
        return deps;
    }
}

/**
 * @brief compute the set of recursive dependencies of a signal
 *
 * @param sig
 * @return std::set<Tree>
 */
std::set<Tree> signalDependencies(Tree sig)
{
    std::vector<Tree> underVisit;
    return sigDependencies(underVisit, sig);
}

/**
 * @brief true if a projection is recursive (i.e. it has a definition that depends on itself), false
 * otherwise
 *
 * @param proj, a signal of type projection
 * @return true if the projection is recursive
 * @return false otherwise
 */
bool isSignalRecursive(Tree proj)
{
    std::set<Tree> deps   = signalDependencies(getProjDefinition(proj));
    bool           answer = deps.find(proj) != deps.end();
#if 0
    if (answer) {
        std::cerr << "\nSignal " << *proj << " is recursive. Its dependencies are: (";
        for (Tree d : deps) {
            std::cerr << *d << " ";
        }
        std::cerr << ")\n" << std::endl;
    } else {
        std::cerr << "\nSignal " << *proj << " is NOT recursive. Its dependencies are: (";
        for (Tree d : deps) {
            std::cerr << *d << " ";
        }
        std::cerr << ")\n" << std::endl;
    }
#endif
    return answer;
}

/**
 * @brief isDependingOn(sig, proj) returns true if sig depends on proj, false otherwise
 *
 * @param sig
 * @param proj
 * @return true
 * @return false
 */
bool isDependingOn(Tree sig, Tree proj)
{
    std::set<Tree> deps   = signalDependencies(sig);
    bool           answer = deps.find(proj) != deps.end();
    return answer;
}

/**
 * @brief Returns the definition associated with a projection
 *
 * @param a signal of type projection
 * @return the definition associated with the projection
 */
Tree getProjDefinition(Tree proj)
{
    int  i;
    Tree w, id, le;
    faustassert(isProj(proj, &i, w));
    faustassert(isRec(w, id, le));
    return nth(le, i);
}

/**
 * @brief Get the Proj Final Definition object
 *
 * @param proj
 * @return Tree
 */
Tree getProjFinalDefinition(Tree proj)
{
    int  i;
    Tree w;
    Tree def = getProjDefinition(proj);
    while (isProj(def, &i, w)) {
        def = getProjDefinition(def);
    }
    return def;
}

/**
 * @brief Print the dependecies of a signal (for debugging)
 *
 * @param msg a string prefix to the message
 * @param sig the signal we want to print the depedencies of
 */
void printDependencies(const std::string& msg, Tree sig)
{
    std::set<Tree> deps = signalDependencies(sig);
    std::cerr << msg << " dependencies of " << ppsig(sig, 20) << " are: (";
    for (Tree d : deps) {
        std::cerr << *d << " ";
    }
    std::cerr << ")\n" << std::endl;
}