File: wast_code_container.cpp

package info (click to toggle)
faust 2.14.4~repack2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 276,136 kB
  • sloc: cpp: 231,578; ansic: 15,403; sh: 10,871; java: 6,917; objc: 4,085; makefile: 3,002; cs: 1,077; ruby: 951; python: 885; xml: 550; yacc: 516; lex: 233; lisp: 201
file content (500 lines) | stat: -rw-r--r-- 19,705 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
/************************************************************************
 ************************************************************************
    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 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 "wast_code_container.hh"
#include "Text.hh"
#include "exception.hh"
#include "floats.hh"
#include "global.hh"
#include "json_instructions.hh"

using namespace std;

/*
 WAST backend and module description:

 - mathematical functions are either part of WebAssembly (like f32.sqrt, f32.main, f32.max), are imported from JS
 "global.Math", or are externally implemented (fmod and remainder in JS)
 - local variables have to be declared first on the block, before being actually initialized or set: this is done using
 MoveVariablesInFront3
 - 'faustpower' function fallbacks to regular 'pow' (see powprim.h)
 - subcontainers are inlined in 'classInit' and 'instanceConstants' functions
 - waveform generation is 'inlined' using MoveVariablesInFront3, done in a special version of generateInstanceInitFun
 - integer 'min/max' is done in the module in 'min_i/max_i' (using lt/select)
 - memory can be allocated internally in the module and exported, or externally in JS and imported
 - the JSON string is written at offset 0 in a data segment. This string *has* to be converted in a JS string *before*
 using the DSP instance
 - memory module size cannot be written while generating the output stream, since DSP size is computed when inlining
 subcontainers and waveform. The final memory size is finally written after module code generation.
 - in Load/Store, check if address is constant, so that to be used as an 'offset'
 - move loop 'i' variable by bytes instead of frames to save index code generation of input/output accesses
 (gLoopVarInBytes)
 - offset of inputs/outputs are constant, so can be directly generated

*/

dsp_factory_base* WASTCodeContainer::produceFactory()
{
    return new text_dsp_factory_aux(
        fKlassName, "", "",
        ((dynamic_cast<std::stringstream*>(fOut)) ? dynamic_cast<std::stringstream*>(fOut)->str() : ""), fHelper.str());
}

WASTCodeContainer::WASTCodeContainer(const string& name, int numInputs, int numOutputs, std::ostream* out,
                                     bool internal_memory)
    : fOut(out)
{
    initialize(numInputs, numOutputs);
    fKlassName      = name;
    fInternalMemory = internal_memory;

    // Allocate one static visitor to be shared by main and sub containers
    if (!gGlobal->gWASTVisitor) {
        gGlobal->gWASTVisitor = new WASTInstVisitor(&fOutAux, fInternalMemory);
    }
}

CodeContainer* WASTCodeContainer::createScalarContainer(const string& name, int sub_container_type)
{
    return new WASTScalarCodeContainer(name, 0, 1, &fOutAux, sub_container_type, true);
}

CodeContainer* WASTCodeContainer::createScalarContainer(const string& name, int sub_container_type,
                                                        bool internal_memory)
{
    return new WASTScalarCodeContainer(name, 0, 1, &fOutAux, sub_container_type, internal_memory);
}

CodeContainer* WASTCodeContainer::createContainer(const string& name, int numInputs, int numOutputs, ostream* dst,
                                                  bool internal_memory)
{
    CodeContainer* container;

    if (gGlobal->gMemoryManager) {
        throw faustexception("ERROR : -mem not supported for WebAssembly\n");
    }
    if (gGlobal->gFloatSize == 3) {
        throw faustexception("ERROR : quad format not supported for WebAssembly\n");
    }
    if (gGlobal->gOpenCLSwitch) {
        throw faustexception("ERROR : OpenCL not supported for WebAssembly\n");
    }
    if (gGlobal->gCUDASwitch) {
        throw faustexception("ERROR : CUDA not supported for WebAssembly\n");
    }

    if (gGlobal->gOpenMPSwitch) {
        throw faustexception("ERROR : OpenMP not supported for WebAssembly\n");
    } else if (gGlobal->gSchedulerSwitch) {
        throw faustexception("ERROR : Scheduler mode not supported for WebAssembly\n");
    } else if (gGlobal->gVectorSwitch) {
        //throw faustexception("ERROR : Vector mode not supported for WebAssembly\n");
        container = new WASTVectorCodeContainer(name, numInputs, numOutputs, dst, internal_memory);
    } else {
        container = new WASTScalarCodeContainer(name, numInputs, numOutputs, dst, kInt, internal_memory);
    }

    return container;
}

// Scalar
WASTScalarCodeContainer::WASTScalarCodeContainer(const string& name, int numInputs, int numOutputs, std::ostream* out,
                                                 int sub_container_type, bool internal_memory)
    : WASTCodeContainer(name, numInputs, numOutputs, out, internal_memory)
{
    fSubContainerType = sub_container_type;
}

// Special version that uses MoveVariablesInFront3 to inline waveforms...
DeclareFunInst* WASTCodeContainer::generateInstanceInitFun(const string& name, const string& obj, bool ismethod,
                                                           bool isvirtual, bool addreturn)
{
    list<NamedTyped*> args;
    if (!ismethod) {
        args.push_back(InstBuilder::genNamedTyped(obj, Typed::kObj_ptr));
    }
    args.push_back(InstBuilder::genNamedTyped("samplingFreq", Typed::kInt32));
    BlockInst* init_block = InstBuilder::genBlockInst();

    init_block->pushBackInst(MoveVariablesInFront3().getCode(fStaticInitInstructions));

    init_block->pushBackInst(MoveVariablesInFront3().getCode(fInitInstructions));

    init_block->pushBackInst(MoveVariablesInFront3().getCode(fPostInitInstructions));

    init_block->pushBackInst(MoveVariablesInFront3().getCode(fResetUserInterfaceInstructions));

    init_block->pushBackInst(MoveVariablesInFront3().getCode(fClearInstructions));

    if (addreturn) {
        init_block->pushBackInst(InstBuilder::genRetInst());
    }

    // Creates function
    FunTyped* fun_type = InstBuilder::genFunTyped(args, InstBuilder::genBasicTyped(Typed::kVoid),
                                                  (isvirtual) ? FunTyped::kVirtual : FunTyped::kDefault);
    return InstBuilder::genDeclareFunInst(name, fun_type, init_block);
}

void WASTCodeContainer::produceInternal()
{
    // Fields generation
    generateGlobalDeclarations(gGlobal->gWASTVisitor);
    generateDeclarations(gGlobal->gWASTVisitor);
}

void WASTCodeContainer::produceClass()
{
    int n = 0;
    gGlobal->gWASTVisitor->Tab(n);

    tab(n, fOutAux);
    fOutAux << "(module";

    // Global declarations (mathematical functions, global variables...)
    gGlobal->gWASTVisitor->Tab(n + 1);

    // Sub containers : before functions generation
    mergeSubContainers();

    // All mathematical functions (got from math library as variables) have to be first
    generateGlobalDeclarations(gGlobal->gWASTVisitor);

    // Exported functions
    tab(n + 1, fOutAux);
    fOutAux << "(export \"getNumInputs\" (func $getNumInputs))";
    tab(n + 1, fOutAux);
    fOutAux << "(export \"getNumOutputs\" (func $getNumOutputs))";
    tab(n + 1, fOutAux);
    fOutAux << "(export \"getSampleRate\" (func $getSampleRate))";
    tab(n + 1, fOutAux);
    fOutAux << "(export \"init\" (func $init))";
    tab(n + 1, fOutAux);
    fOutAux << "(export \"instanceInit\" (func $instanceInit))";
    tab(n + 1, fOutAux);
    fOutAux << "(export \"instanceConstants\" (func $instanceConstants))";
    tab(n + 1, fOutAux);
    fOutAux << "(export \"instanceResetUserInterface\" (func $instanceResetUserInterface))";
    tab(n + 1, fOutAux);
    fOutAux << "(export \"instanceClear\" (func $instanceClear))";
    tab(n + 1, fOutAux);
    fOutAux << "(export \"setParamValue\" (func $setParamValue))";
    tab(n + 1, fOutAux);
    fOutAux << "(export \"getParamValue\" (func $getParamValue))";
    tab(n + 1, fOutAux);
    fOutAux << "(export \"compute\" (func $compute))";

    // General imports
    tab(n + 1, fOutAux);
    fOutAux << "(import \"env\" \"memoryBase\" (global $memoryBase i32))";
    tab(n + 1, fOutAux);
    fOutAux << "(import \"env\" \"tableBase\" (global $tableBase i32))";

    // Fields : compute the structure size to use in 'new'
    gGlobal->gWASTVisitor->Tab(n + 1);
    generateDeclarations(gGlobal->gWASTVisitor);

    // After field declaration
    generateSubContainers();

    // Keep location of memory generation
    streampos begin_memory = fOutAux.tellp();

    // Always generated mathematical functions
    tab(n + 1, fOutAux);
    WASInst::generateIntMin()->accept(gGlobal->gWASTVisitor);
    WASInst::generateIntMax()->accept(gGlobal->gWASTVisitor);

    // getNumInputs/getNumOutputs
    generateGetInputs("getNumInputs", "dsp", false, false)->accept(gGlobal->gWASTVisitor);
    generateGetOutputs("getNumOutputs", "dsp", false, false)->accept(gGlobal->gWASTVisitor);

    // Inits
    tab(n + 1, fOutAux);
    fOutAux << "(func $classInit (param $dsp i32) (param $samplingFreq i32)";
    tab(n + 2, fOutAux);
    gGlobal->gWASTVisitor->Tab(n + 2);
    {
        BlockInst* inlined = inlineSubcontainersFunCalls(fStaticInitInstructions);
        generateWASTBlock(inlined);
    }
    tab(n + 1, fOutAux);
    fOutAux << ")";

    tab(n + 1, fOutAux);
    fOutAux << "(func $instanceConstants (param $dsp i32) (param $samplingFreq i32)";
    tab(n + 2, fOutAux);
    gGlobal->gWASTVisitor->Tab(n + 2);
    {
        BlockInst* inlined = inlineSubcontainersFunCalls(fInitInstructions);
        generateWASTBlock(inlined);
    }
    tab(n + 1, fOutAux);
    fOutAux << ")";

    tab(n + 1, fOutAux);
    fOutAux << "(func $instanceResetUserInterface (param $dsp i32)";
    tab(n + 2, fOutAux);
    gGlobal->gWASTVisitor->Tab(n + 2);
    {
        // Rename 'sig' in 'dsp' and remove 'dsp' allocation
        generateWASTBlock(DspRenamer().getCode(fResetUserInterfaceInstructions));
    }
    tab(n + 1, fOutAux);
    fOutAux << ")";

    tab(n + 1, fOutAux);
    fOutAux << "(func $instanceClear (param $dsp i32)";
    tab(n + 2, fOutAux);
    gGlobal->gWASTVisitor->Tab(n + 2);
    {
        // Rename 'sig' in 'dsp' and remove 'dsp' allocation
        generateWASTBlock(DspRenamer().getCode(fClearInstructions));
    }
    tab(n + 1, fOutAux);
    fOutAux << ")";

    gGlobal->gWASTVisitor->Tab(n + 1);

    // init
    generateInit("dsp", false, false)->accept(gGlobal->gWASTVisitor);

    // instanceInit
    generateInstanceInit("dsp", false, false)->accept(gGlobal->gWASTVisitor);

    // getSampleRate
    generateGetSampleRate("dsp", false, false)->accept(gGlobal->gWASTVisitor);

    // setParamValue
    tab(n + 1, fOutAux);
    fOutAux << "(func $setParamValue (param $dsp i32) (param $index i32) (param $value " << realStr << ")";
    tab(n + 2, fOutAux);
    fOutAux << "(" << realStr << ".store ";
    tab(n + 3, fOutAux);
    fOutAux << "(i32.add (local.get $dsp) (local.get $index))";
    tab(n + 3, fOutAux);
    fOutAux << "(local.get $value)";
    tab(n + 2, fOutAux);
    fOutAux << ")";
    tab(n + 1, fOutAux);
    fOutAux << ")";

    // getParamValue
    tab(n + 1, fOutAux);
    fOutAux << "(func $getParamValue (param $dsp i32) (param $index i32) (result " << realStr << ")";
    tab(n + 2, fOutAux);
    fOutAux << "(return (" << realStr << ".load (i32.add (local.get $dsp) (local.get $index))))";
    tab(n + 1, fOutAux);
    fOutAux << ")";

    // compute
    generateCompute(n);

    // Possibly generate separated functions
    gGlobal->gWASTVisitor->Tab(n + 1);
    tab(n + 1, fOutAux);
    generateComputeFunctions(gGlobal->gWASTVisitor);

    tab(n, fOutAux);
    fOutAux << ")";
    tab(n, fOutAux);

    // JSON generation

    // Prepare compilation options
    stringstream compile_options;
    gGlobal->printCompilationOptions(compile_options, false);

    stringstream size;
    size << gGlobal->gWASTVisitor->getStructSize();

    JSONInstVisitor json_visitor1;
    generateUserInterface(&json_visitor1);

    map<string, string>::iterator it;
    std::map<std::string, int>    path_index_table;
    map<string, MemoryDesc>&      fieldTable1 = gGlobal->gWASTVisitor->getFieldTable();
    for (it = json_visitor1.fPathTable.begin(); it != json_visitor1.fPathTable.end(); it++) {
        // Get field index
        MemoryDesc tmp                 = fieldTable1[(*it).first];
        path_index_table[(*it).second] = tmp.fOffset;
    }

    // "name", "filename" found in medata
    JSONInstVisitor json_visitor2("", "", fNumInputs, fNumOutputs, "", "", FAUSTVERSION, compile_options.str(),
                                  gGlobal->gReader.listLibraryFiles(), gGlobal->gImportDirList, size.str(),
                                  path_index_table);
    generateUserInterface(&json_visitor2);
    generateMetaData(&json_visitor2);

    string json = json_visitor2.JSON(true);

    // Now that DSP structure size is known, concatenate stream parts to produce the final stream
    string tmp_aux = fOutAux.str();
    string begin   = tmp_aux.substr(0, begin_memory);
    string end     = tmp_aux.substr(begin_memory);

    // Write begining of code stream on *fOut
    *fOut << begin;

    // Insert memory generation
    tab(n + 1, *fOut);
    if (fInternalMemory) {
        *fOut << "(memory (export \"memory\") ";
        // Since JSON is written in data segment at offset 0, the memory size must be computed taking account JSON size
        // and DSP + audio buffer size
        *fOut << genMemSize(gGlobal->gWASTVisitor->getStructSize(), fNumInputs + fNumOutputs, (int)json.size())
              << ")";  // memory initial pages
    } else {
        // Memory size set by JS code, so use a minimum value that contains the data segment size (shoud be OK for any
        // JSON)
        *fOut << "(import \"env\" \"memory\" (memory $0 1))";
    }

    // Generate one data segment containing the JSON string starting at offset 0
    tab(n + 1, *fOut);
    *fOut << "(data (i32.const 0) \"" << json << "\")";

    // And write end of code stream on *fOut
    *fOut << end;

    // Helper code

    // Generate JSON and getSize
    tab(n, fHelper);
    fHelper << "/*\n"
            << "Code generated with Faust version " << FAUSTVERSION << endl;
    fHelper << "Compilation options: ";
    gGlobal->printCompilationOptions(fHelper);
    fHelper << "\n*/\n";

    // Generate JSON
    tab(n, fHelper);
    fHelper << "function getJSON" << fKlassName << "() {";
    tab(n + 1, fHelper);
    fHelper << "return \"";
    fHelper << json;
    fHelper << "\";";
    printlines(n + 1, fUICode, fHelper);
    tab(n, fHelper);
    fHelper << "}\n";
}

DeclareFunInst* WASInst::generateIntMin()
{
    string v1 = gGlobal->getFreshID("v1");
    string v2 = gGlobal->getFreshID("v2");
    
    list<NamedTyped*> args;
    args.push_back(InstBuilder::genNamedTyped(v1, Typed::kInt32));
    args.push_back(InstBuilder::genNamedTyped(v2, Typed::kInt32));
    
    BlockInst* block = InstBuilder::genBlockInst();
    block->pushBackInst(InstBuilder::genRetInst(InstBuilder::genSelect2Inst(InstBuilder::genLessThan(InstBuilder::genLoadFunArgsVar(v1),
                                                                                                     InstBuilder::genLoadFunArgsVar(v2)),
                                                                            InstBuilder::genLoadFunArgsVar(v1),
                                                                            InstBuilder::genLoadFunArgsVar(v2))));
    // Creates function
    FunTyped* fun_type = InstBuilder::genFunTyped(args, InstBuilder::genBasicTyped(Typed::kInt32), FunTyped::kDefault);
    return InstBuilder::genDeclareFunInst("min_i", fun_type, block);
}

DeclareFunInst* WASInst::generateIntMax()
{
    string v1 = gGlobal->getFreshID("v1");
    string v2 = gGlobal->getFreshID("v2");
    
    list<NamedTyped*> args;
    args.push_back(InstBuilder::genNamedTyped(v1, Typed::kInt32));
    args.push_back(InstBuilder::genNamedTyped(v2, Typed::kInt32));
    
    BlockInst* block = InstBuilder::genBlockInst();
    block->pushBackInst(InstBuilder::genRetInst(InstBuilder::genSelect2Inst(InstBuilder::genLessThan(InstBuilder::genLoadFunArgsVar(v1),
                                                                                                     InstBuilder::genLoadFunArgsVar(v2)),
                                                                            InstBuilder::genLoadFunArgsVar(v2),
                                                                            InstBuilder::genLoadFunArgsVar(v1))));
    // Creates function
    FunTyped* fun_type = InstBuilder::genFunTyped(args, InstBuilder::genBasicTyped(Typed::kInt32), FunTyped::kDefault);
    return InstBuilder::genDeclareFunInst("max_i", fun_type, block);
}

// Auxiliary functions for shared code in generateCompute
void WASTCodeContainer::generateComputeAux1(int n)
{
    tab(n + 1, fOutAux);
    fOutAux << "(func $compute (param $dsp i32) (param $count i32) (param $inputs i32) (param $outputs i32)";
    tab(n + 2, fOutAux);
    gGlobal->gWASTVisitor->Tab(n + 2);
}

void WASTCodeContainer::generateComputeAux2(BlockInst* compute_block, int n)
{
    DeclareFunInst* int_max_fun = WASInst::generateIntMax();
    DeclareFunInst* int_min_fun = WASInst::generateIntMin();
    
    // Remove unecessary cast
    compute_block = CastRemover().getCode(compute_block);
    
    // Inline "max_i" call
    compute_block = FunctionCallInliner(int_max_fun).getCode(compute_block);
    
    // Inline "min_i" call
    compute_block = FunctionCallInliner(int_min_fun).getCode(compute_block);
    
    // Push the loop in compute block
    fComputeBlockInstructions->pushBackInst(compute_block);
    
    // Put local variables at the begining
    BlockInst* block = MoveVariablesInFront2().getCode(fComputeBlockInstructions, true);
    
    block->accept(gGlobal->gWASTVisitor);
    tab(n + 1, fOutAux);
    fOutAux << ")";
}

void WASTScalarCodeContainer::generateCompute(int n)
{
    generateComputeAux1(n);

    // Loop 'i' variable is moved by bytes
    BlockInst* compute_block = InstBuilder::genBlockInst();
    compute_block->pushBackInst(fCurLoop->generateScalarLoop(fFullCount, gGlobal->gLoopVarInBytes));

    generateComputeAux2(compute_block, n);
}

// Vector
WASTVectorCodeContainer::WASTVectorCodeContainer(const string& name, int numInputs, int numOutputs, std::ostream* out,
                                                 bool internal_memory)
    :VectorCodeContainer(numInputs, numOutputs), WASTCodeContainer(name, numInputs, numOutputs, out, internal_memory)
{
    // No array on stack, move all of them in struct
    gGlobal->gMachineMaxStackSize = -1;
}

void WASTVectorCodeContainer::generateCompute(int n)
{
    generateComputeAux1(n);
    
    // Rename all loop variables name to avoid name clash
    LoopVariableRenamer loop_renamer;
    generateComputeAux2(loop_renamer.getCode(fDAGBlock), n);
}