File: struct_manager.hh

package info (click to toggle)
faust 2.54.9%2Bds0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 352,732 kB
  • sloc: cpp: 260,515; javascript: 33,578; ansic: 16,680; vhdl: 14,052; sh: 13,271; java: 5,900; objc: 3,875; makefile: 3,228; python: 2,176; cs: 1,642; lisp: 1,140; ruby: 951; xml: 762; yacc: 564; lex: 247; awk: 110; tcl: 26
file content (361 lines) | stat: -rw-r--r-- 14,970 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
/************************************************************************
 ************************************************************************
    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 _STRUCT_MANAGER_H
#define _STRUCT_MANAGER_H

#include <string>
#include <utility>
#include <vector>

#include "instructions.hh"

// Describe a field memory location in the DSP structure
struct MemoryDesc {
    enum memType { kLocal, kExternal };
    int fIndex;             // Index
    int fOffset;            // Offset in bytes in a mixed int/real zone
    int fIntOffset;         // Offset in bytes in a separated int zone
    int fRealOffset;        // Offset in bytes in a separated real zone
    int fRAccessCount;      // Read access counter
    int fWAccessCount;      // Write access counter
    int fSize;              // Size in frames
    int fSizeBytes;         // Size in bytes
    Typed::VarType fType;   // FIR type
    memType fMemType;       // Memory type

    MemoryDesc() : fIndex(-1), fOffset(-1),
        fIntOffset(-1), fRealOffset(-1),
        fRAccessCount(0), fWAccessCount(0),
        fSize(-1), fSizeBytes(-1),
        fType(Typed::kNoType), fMemType(kLocal) {}

    MemoryDesc(int index, int offset, int size, int size_bytes, Typed::VarType type)
    : fIndex(index), fOffset(offset),
        fIntOffset(-1), fRealOffset(-1),
        fRAccessCount(0), fWAccessCount(0),
        fSize(size), fSizeBytes(size_bytes),
        fType(type), fMemType(kLocal) {}
 
    MemoryDesc(int index, int offset,
               int int_offset, int real_offset,
               int size, int size_bytes,
               Typed::VarType type,
               memType mem_type = kLocal)
        : fIndex(index), fOffset(offset),
        fIntOffset(int_offset), fRealOffset(real_offset),
        fRAccessCount(0), fWAccessCount(0),
        fSize(size), fSizeBytes(size_bytes),
        fType(type), fMemType(mem_type) {}
    
    Typed* getTyped()
    {
        if (fSize > 1) {
            return InstBuilder::genArrayTyped(InstBuilder::genBasicTyped(fType), fSize);
        } else {
            return InstBuilder::genBasicTyped(fType);
        }
    }
};

/*
 Compute all field info, the DSP size, and separate 'int' and 'real' types
 */
struct StructInstVisitor : public DispatchVisitor {
    int        fStructIntOffset;    // Keep the int offset in bytes
    int        fStructRealOffset;   // Keep the real offset in bytes
    int        fFieldIndex;         // Keep the field index
    MemoryDesc fDefault;
    
    // Vector is used so that field names are ordered in 'getStructType'
    typedef vector<pair<string, MemoryDesc> > field_table_type;
    
    field_table_type fFieldTable;  // Table: field_name, MemoryDesc
    
    StructInstVisitor() : fStructIntOffset(0), fStructRealOffset(0), fFieldIndex(0) {}
    
    // Check if the field name exists
    bool hasField(const string& name, Typed::VarType& type)
    {
        for (const auto& field : fFieldTable) {
            if (field.first == name) {
                type = field.second.fType;
                return true;
            }
        }
        return false;
    }
    
    // Return the offset of a given field in bytes
    int getFieldOffset(const string& name)
    {
        for (const auto& field : fFieldTable) {
            if (field.first == name) return field.second.fOffset;
        }
        std::cerr << "ERROR in getFieldOffset : " << name << std::endl;
        faustassert(false);
        return -1;
    }
    
    // Return the int offset of a given field in bytes
    int getFieldIntOffset(const string& name)
    {
        for (const auto& field : fFieldTable) {
            if (field.first == name) return field.second.fIntOffset;
        }
        std::cerr << "ERROR in getFieldIntOffset : " << name << std::endl;
        faustassert(false);
        return -1;
    }
    
    // Return the real offset of a given field in bytes
    int getFieldRealOffset(const string& name)
    {
        for (const auto& field : fFieldTable) {
            if (field.first == name) return field.second.fRealOffset;
        }
        std::cerr << "ERROR in getFieldRealOffset : " << name << std::endl;
        faustassert(false);
        return -1;
    }
    
    // Return the index of a given field
    int getFieldIndex(const string& name)
    {
        for (const auto& field : fFieldTable) {
            if (field.first == name) return field.second.fIndex;
        }
        std::cerr << "ERROR in getFieldIndex : " << name << std::endl;
        faustassert(false);
        return -1;
    }
    
    // Return the FIR type of a given field
    Typed::VarType getFieldType(const string& name)
    {
        for (const auto& field : fFieldTable) {
            if (field.first == name) return field.second.fType;
        }
        std::cerr << "ERROR in getFieldType : " << name << std::endl;
        faustassert(false);
        return Typed::kNoType;
    }
    
    // Return the memory type of a given field
    MemoryDesc::memType getFieldMemoryType(const string& name)
    {
        for (const auto& field : fFieldTable) {
            if (field.first == name) return field.second.fMemType;
        }
        std::cerr << "ERROR in getFieldMemoryType : " << name << std::endl;
        faustassert(false);
        return MemoryDesc::kLocal;
    }
    
    // Return the memory description of a given field
    MemoryDesc& getMemoryDesc(const string& name)
    {
        for (auto& field : fFieldTable) {
            if (field.first == name) return field.second;
        }
        return fDefault;
    }
    
    // Return the struct 'int' size in bytes
    int getStructIntSize() { return fStructIntOffset; }
    
    // Return the struct 'real' size in bytes
    int getStructRealSize() { return fStructRealOffset; }
    
    // Return the struct size in bytes
    int getStructSize() { return fStructIntOffset + fStructRealOffset; }
    
    field_table_type& getFieldTable() { return fFieldTable; }
    
    int getArrayCount()
    {
        int res = 0;
        for (const auto& field : fFieldTable) {
            if (field.second.fSize > 1) res++;
        }
        return res;
    }
    
    // Return the struct type
    DeclareStructTypeInst* getStructType(const string& name)
    {
        vector<NamedTyped*> dsp_type_fields;
        for (auto& field : fFieldTable) {
            dsp_type_fields.push_back(InstBuilder::genNamedTyped(field.first, field.second.getTyped()));
        }
        return InstBuilder::genDeclareStructTypeInst(InstBuilder::genStructTyped(name, dsp_type_fields));
    }
    
    // Declarations
    void visit(DeclareVarInst* inst)
    {
        string              name   = inst->fAddress->getName();
        Address::AccessType access = inst->fAddress->getAccess();
        
        bool        is_struct   = (access & Address::kStruct) || (access & Address::kStaticStruct);
        ArrayTyped* array_typed = dynamic_cast<ArrayTyped*>(inst->fType);
        
        if (array_typed && array_typed->fSize > 1) {
            Typed::VarType type = array_typed->fType->getType();
            if (is_struct) {
                fFieldTable.push_back(make_pair(name, MemoryDesc(fFieldIndex++,
                                                                 getStructSize(),
                                                                 getStructIntSize(),
                                                                 getStructRealSize(),
                                                                 array_typed->fSize,
                                                                 array_typed->getSizeBytes(),
                                                                 type)));
                if (type == Typed::kInt32) {
                    fStructIntOffset += array_typed->getSizeBytes();
                } else {
                    fStructRealOffset += array_typed->getSizeBytes();
                }
            } else {
                // Should never happen...
                faustassert(false);
            }
        } else {
            if (is_struct) {
                fFieldTable.push_back(make_pair(name, MemoryDesc(fFieldIndex++,
                                                                 getStructSize(),
                                                                 getStructIntSize(),
                                                                 getStructRealSize(),
                                                                 1,
                                                                 inst->fType->getSizeBytes(),
                                                                 inst->fType->getType())));
                if (inst->fType->getType() == Typed::kInt32) {
                    fStructIntOffset += inst->fType->getSizeBytes();
                } else {
                    fStructRealOffset += inst->fType->getSizeBytes();
                }
            }
        }
    
        if (inst->fValue) getMemoryDesc(inst->getName()).fWAccessCount++;
        DispatchVisitor::visit(inst);
    }
       
    void visit(LoadVarInst* inst)
    {
        getMemoryDesc(inst->getName()).fRAccessCount++;
        DispatchVisitor::visit(inst);
    }
     
    void visit(StoreVarInst* inst)
    {
        getMemoryDesc(inst->getName()).fWAccessCount++;
        DispatchVisitor::visit(inst);
    }
    
};

/*
 A version that separates some of the fields for the iZone/fZone model
 and keep the others in the DSP struct:
    - small arrays (size < fDLThreshold) are allocated in the DSP struct
    - first generated big arrays are moved in iZone/fZone, until fExternalMemory reaches 0
    - then other big arrays are kept in the DSP struct
*/
struct StructInstVisitor1 : public StructInstVisitor {

    int fExternalMemory;
    int fDLThreshold;
    
    // To be computed with DSP struct size and FAUST_MAX_SIZE
    StructInstVisitor1(int external_memory, int dl_threshold = 4)
    : StructInstVisitor(), fExternalMemory(external_memory), fDLThreshold(dl_threshold)
    {}
    
    // Declarations
    void visit(DeclareVarInst* inst)
    {
        string              name   = inst->fAddress->getName();
        Address::AccessType access = inst->fAddress->getAccess();
        
        bool        is_struct   = (access & Address::kStruct) || (access & Address::kStaticStruct);
        ArrayTyped* array_typed = dynamic_cast<ArrayTyped*>(inst->fType);
        
        if (array_typed && array_typed->fSize > 1) {
            Typed::VarType type = array_typed->fType->getType();
            if (is_struct) {
                // Arrays are allocated in iZone/fZone until fExternalMemory reaches 0
                // kStaticStruct are always allocated in kExternal
                // RW tables ("itblXX" and "ftblXX") are always allocated in kExternal
                if ((access & Address::kStaticStruct)
                    || startWith(name, "itbl")
                    || startWith(name, "ftbl")
                    || (fExternalMemory > 0 && array_typed->fSize > fDLThreshold)) {
                    fFieldTable.push_back(make_pair(name, MemoryDesc(fFieldIndex++,
                                                                     getStructSize(),
                                                                     getStructIntSize(),
                                                                     getStructRealSize(),
                                                                     array_typed->fSize,
                                                                     array_typed->getSizeBytes(),
                                                                     type,
                                                                     MemoryDesc::kExternal)));
                    
                    if (type == Typed::kInt32) {
                        fStructIntOffset += array_typed->getSizeBytes();
                    } else {
                        fStructRealOffset += array_typed->getSizeBytes();
                    }
                    fExternalMemory -= array_typed->getSizeBytes();
                } else {
                    // Keep arrays in local struct memory
                    fFieldTable.push_back(make_pair(name, MemoryDesc(fFieldIndex++,
                                                                     getStructSize(),
                                                                     getStructIntSize(),
                                                                     getStructRealSize(),
                                                                     array_typed->fSize,
                                                                     array_typed->getSizeBytes(),
                                                                     type,
                                                                     MemoryDesc::kLocal)));
                }
            } else {
                // Should never happen...
                faustassert(false);
            }
        } else {
            if (is_struct) {
                // Scalar variable always stay in local struct memory
                fFieldTable.push_back(make_pair(name, MemoryDesc(fFieldIndex++,
                                                                 getStructSize(),
                                                                 getStructIntSize(),
                                                                 getStructRealSize(),
                                                                 1,
                                                                 inst->fType->getSizeBytes(),
                                                                 inst->fType->getType(),
                                                                 MemoryDesc::kLocal)));
            }
        }
    
        if (inst->fValue) getMemoryDesc(inst->getName()).fWAccessCount++;
        DispatchVisitor::visit(inst);
    }
    
};

#endif