File: llvm_machine_dsp_aux.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 (763 lines) | stat: -rw-r--r-- 20,983 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
/************************************************************************
 ************************************************************************
    FAUST compiler
    Copyright (C) 2003-2015 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 <stdio.h>
#include <fstream>
#include <iostream>
#include <list>
#include <sstream>
#ifndef _WIN32
#include <libgen.h>
#endif

#include "exception.hh"
#include "faust/gui/CGlue.h"
#include "llvm_machine_dsp_aux.hh"
#include "rn_base64.h"

#include <llvm/ExecutionEngine/MCJIT.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/LegacyPassNameParser.h>
#include <llvm/IR/Module.h>
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/Host.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Support/Threading.h>
#include <llvm/Transforms/IPO/PassManagerBuilder.h>
#include <system_error>
#include "llvm/ExecutionEngine/ObjectCache.h"

#define OwningPtr std::unique_ptr
#define sysfs_binary_flag sys::fs::F_None

#define STREAM_ERROR std::error_code
#define MEMORY_BUFFER MemoryBufferRef
#define MEMORY_BUFFER_GET(buffer) (buffer.getBuffer())
#define MEMORY_BUFFER_GET_REF(buffer) (buffer->get()->getMemBufferRef())
#define MEMORY_BUFFER_CREATE(stringref) (MemoryBufferRef(stringref, ""))
#define GET_CPU_NAME llvm::sys::getHostCPUName().str()
#define LLVM_MAX_OPT_LEVEL 5

using namespace llvm;

// Factories instances management
int              llvm_dsp_factory::gInstance = 0;
FactoryTableType llvm_dsp_factory::gFactoryTable;

// Global API access lock
TLockAble* gDSPFactoriesLock = 0;

static string getParam(int argc, const char* argv[], const string& param, const string& def)
{
    for (int i = 0; i < argc; i++) {
        if (string(argv[i]) == param) return argv[i + 1];
    }
    return def;
}

static bool isParam(int argc, const char* argv[], const string& param)
{
    for (int i = 0; i < argc; i++) {
        if (string(argv[i]) == param) return true;
    }
    return false;
}

static llvm_dsp_factory* checkDSPFactory(llvm_dsp_factory* factory, string& error_msg)
{
    if (factory->initJIT(error_msg)) {
        return factory;
    } else {
        delete factory;
        return NULL;
    }
}

static bool getFactory(const string& sha_key, FactoryTableIt& res)
{
    FactoryTableIt it;

    for (it = llvm_dsp_factory::gFactoryTable.begin(); it != llvm_dsp_factory::gFactoryTable.end(); it++) {
        if ((*it).first->getSHAKey() == sha_key) {
            res = it;
            return true;
        }
    }

    return false;
}

void ObjectCache::anchor()
{
}

class FaustObjectCache : public ObjectCache {
   private:
    string fMachineCode;

   public:
    FaustObjectCache(const string& machine_code = "") : fMachineCode(machine_code) {}

    virtual ~FaustObjectCache() {}

    virtual void notifyObjectCompiled(const Module* M, MemoryBufferRef Obj) { fMachineCode = Obj.getBuffer().str(); }

    virtual std::unique_ptr<MemoryBuffer> getObject(const Module* M)
    {
        return (fMachineCode == "") ? NULL : MemoryBuffer::getMemBuffer(StringRef(fMachineCode));
    }

    string getMachineCode() { return fMachineCode; }
};

void* llvm_dsp_factory::loadOptimize(const string& function)
{
    void* fun = (void*)fJIT->getFunctionAddress(function);
    if (fun) {
        return fun;
    } else {
        stringstream error;
        error << "ERROR : loadOptimize failed for '" << function << "'";
        throw faustexception(error.str());
    }
}

llvm_dsp_factory::llvm_dsp_factory(const string& sha_key, const string& machine_code, const string& target)
{
    fJIT                = 0;
    fNew                = 0;
    fDelete             = 0;
    fGetNumInputs       = 0;
    fGetNumOutputs      = 0;
    fBuildUserInterface = 0;
    fInit               = 0;
    fCompute            = 0;
    fClassName          = "mydsp";
    fExtName            = "MachineDSP";
    fTarget = (target == "") ? fTarget = (llvm::sys::getDefaultTargetTriple() + ":" + GET_CPU_NAME) : target;
    fSHAKey                            = sha_key;

    fObjectCache = new FaustObjectCache(machine_code);

    // Creates module and context
    fResult           = static_cast<LLVMResult*>(calloc(1, sizeof(LLVMResult)));
    fResult->fContext = new LLVMContext();
    fResult->fModule  = new Module(LVVM_BACKEND_NAME, *fResult->fContext);
}

llvm_dsp_aux* llvm_dsp_factory::createDSPInstance()
{
    faustassert(fResult->fModule);
    faustassert(fJIT);
    return new llvm_dsp_aux(this, fNew());
}

int llvm_dsp_factory::getOptlevel()
{
    // TODO LLVM_36
    /*
    NamedMDNode* meta_data = fResult->fModule->getOrInsertNamedMetadata("OptLevel");
    if (meta_data->getNumOperands() > 0) {
        MDNode* node = meta_data->getOperand(0);
        return (node) ? atoi(static_cast<MDString*>(node->getOperand(0))->getString().data()) : -1;
    } else {
        char opt_level[32];
        sprintf(opt_level, "%d", fOptLevel);
        Value* values[] = { MDString::get(fResult->fModule->getContext(), opt_level) };
        meta_data->addOperand(MDNode::get(fResult->fModule->getContext(), values));
        return -1;
    }
    */
    return -1;
}

bool llvm_dsp_factory::initJIT(string& error_msg)
{
    // For host target support
    InitializeNativeTarget();

    // Restoring from machine code
    EngineBuilder  builder(unique_ptr<Module>(fResult->fModule));
    TargetMachine* tm = builder.selectTarget();
    fJIT              = builder.create(tm);
    fJIT->setObjectCache(fObjectCache);
    fJIT->finalizeObject();

    // Run static constructors.
    fJIT->runStaticConstructorsDestructors(false);
    fJIT->DisableLazyCompilation(true);

    try {
        fNew                = (newDspFun)loadOptimize("new" + fClassName);
        fDelete             = (deleteDspFun)loadOptimize("delete" + fClassName);
        fGetNumInputs       = (getNumInputsFun)loadOptimize("getNumInputs" + fClassName);
        fGetNumOutputs      = (getNumOutputsFun)loadOptimize("getNumOutputs" + fClassName);
        fBuildUserInterface = (buildUserInterfaceFun)loadOptimize("buildUserInterface" + fClassName);
        fInit               = (initFun)loadOptimize("init" + fClassName);
        fCompute            = (computeFun)loadOptimize("compute" + fClassName);
        fMetadata           = (metadataFun)loadOptimize("metadata" + fClassName);
        return true;
    } catch (
        faustexception& e) {  // Module does not contain the Faust entry points, or external symbol was not found...
        error_msg = e.Message();
        return false;
    }
}

llvm_dsp_factory::~llvm_dsp_factory()
{
    if (fJIT) {
        fJIT->runStaticConstructorsDestructors(true);
        // fResult->fModule is kept and deleted by fJIT
        delete fJIT;
    }

    if (fResult) {
        delete fResult->fContext;
        free(fResult);
    }

    delete fObjectCache;
}

void llvm_dsp_factory::metadataDSPFactory(Meta* meta)
{
    MetaGlue glue;
    buildMetaGlue(&glue, meta);
    fMetadata(&glue);
}

void llvm_dsp_factory::metadataDSPFactory(MetaGlue* glue)
{
    fMetadata(glue);
}

// Instance

llvm_dsp_aux::llvm_dsp_aux(llvm_dsp_factory* factory, llvm_dsp_imp* dsp) : fDSPFactory(factory), fDSP(dsp)
{
    faustassert(fDSPFactory);
    faustassert(fDSP);
}

llvm_dsp_aux::~llvm_dsp_aux()
{
    if (fDSP) {
        fDSPFactory->fDelete(fDSP);
    }
}

llvm_dsp_aux* llvm_dsp_aux::copy()
{
    return fDSPFactory->createDSPInstance();
}

void llvm_dsp_aux::metadata(Meta* m)
{
    MetaGlue glue;
    buildMetaGlue(&glue, m);
    return fDSPFactory->fMetadata(&glue);
}

void llvm_dsp_aux::metadata(MetaGlue* m)
{
    return fDSPFactory->fMetadata(m);
}

int llvm_dsp_aux::getNumInputs()
{
    return fDSPFactory->fGetNumInputs(fDSP);
}
int llvm_dsp_aux::getNumOutputs()
{
    return fDSPFactory->fGetNumOutputs(fDSP);
}

void llvm_dsp_aux::init(int samplingFreq)
{
    fDSPFactory->fInit(fDSP, samplingFreq);
}

void llvm_dsp_aux::buildUserInterface(UI* ui_interface)
{
    UIGlue glue;
    buildUIGlue(&glue, ui_interface, fDSPFactory->fIsDouble);
    fDSPFactory->fBuildUserInterface(fDSP, &glue);
}

void llvm_dsp_aux::buildUserInterface(UIGlue* glue)
{
    fDSPFactory->fBuildUserInterface(fDSP, glue);
}

void llvm_dsp_aux::compute(int count, FAUSTFLOAT** input, FAUSTFLOAT** output)
{
    AVOIDDENORMALS;
    fDSPFactory->fCompute(fDSP, count, input, output);
}

// Public C++ API

EXPORT llvm_dsp_factory* createDSPFactoryFromString(const std::string& name_app, const std::string& dsp_content,
                                                    int argc, const char* argv[], const std::string& target,
                                                    std::string& error_msg, int opt_level)
{
    return NULL;
}

EXPORT bool startMTDSPFactories()
{
    try {
        if (!gDSPFactoriesLock) {
            gDSPFactoriesLock = new TLockAble();
        }
        return true;
    } catch (...) {
        return false;
    }
}

EXPORT void stopMTDSPFactories()
{
    delete gDSPFactoriesLock;
    gDSPFactoriesLock = 0;
}

EXPORT llvm_dsp_factory* getDSPFactoryFromSHAKey(const string& sha_key)
{
    TLock lock(gDSPFactoriesLock);

    FactoryTableIt it;

    if (getFactory(sha_key, it)) {
        Sllvm_dsp_factory sfactory = (*it).first;
        sfactory->addReference();
        return sfactory;
    } else {
        return NULL;
    }
}

EXPORT vector<string> getAllDSPFactories()
{
    TLock lock(gDSPFactoriesLock);

    FactoryTableIt it;
    vector<string> sha_key_list;

    for (it = llvm_dsp_factory::gFactoryTable.begin(); it != llvm_dsp_factory::gFactoryTable.end(); it++) {
        sha_key_list.push_back((*it).first->getSHAKey());
    }

    return sha_key_list;
}

EXPORT bool deleteDSPFactory(llvm_dsp_factory* factory)
{
    TLock lock(gDSPFactoriesLock);

    FactoryTableIt it;
    if ((it = llvm_dsp_factory::gFactoryTable.find(factory)) != llvm_dsp_factory::gFactoryTable.end()) {
        Sllvm_dsp_factory   sfactory = (*it).first;
        list<llvm_dsp_aux*> dsp_list = (*it).second;
        if (sfactory->refs() == 2) {  // Local stack pointer + the one in gFactoryTable...
            // Possibly delete remaining DSP
            list<llvm_dsp_aux*>::iterator it;
            for (it = dsp_list.begin(); it != dsp_list.end(); it++) {
                delete (*it);
            }
            // Last use, remove from the global table, pointer will be deleted
            llvm_dsp_factory::gFactoryTable.erase(factory);
            return true;
        } else {
            sfactory->removeReference();
        }
    }

    return false;
}

EXPORT std::string llvm_dsp_factory::getName()
{
    struct MyMeta : public Meta {
        string name;

        virtual void declare(const char* key, const char* value)
        {
            if (strcmp(key, "name") == 0) {
                name = value;
            }
        }
    };

    MyMeta metadata;
    metadataDSPFactory(&metadata);
    return (fExtName != "") ? fExtName : metadata.name;
}

EXPORT std::string llvm_dsp_factory::getSHAKey()
{
    return fSHAKey;
}

EXPORT std::string llvm_dsp_factory::getDSPCode()
{
    return fExpandedDSP;
}

EXPORT std::string llvm_dsp_factory::getTarget()
{
    return fTarget;
}

EXPORT std::string getDSPMachineTarget()
{
    return (llvm::sys::getDefaultTargetTriple() + ":" + GET_CPU_NAME);
}

EXPORT std::vector<std::string> getLibraryList(llvm_dsp_factory* factory)
{
    TLock lock(gDSPFactoriesLock);

    return factory->getLibraryList();
}

EXPORT void deleteAllDSPFactories()
{
    TLock lock(gDSPFactoriesLock);

    FactoryTableIt it;
    for (it = llvm_dsp_factory::gFactoryTable.begin(); it != llvm_dsp_factory::gFactoryTable.end(); it++) {
        // Decrement counter up to one...
        while (((*it).first)->refs() > 1) {
            ((*it).first)->removeReference();
        }
    }
    // Then clear the table thus finally deleting all ref = 1 smart pointers
    llvm_dsp_factory::gFactoryTable.clear();
}

static llvm_dsp_factory* readDSPFactoryFromMachineAux(MEMORY_BUFFER buffer, const std::string& target)
{
    string         sha_key = generateSHA1(MEMORY_BUFFER_GET(buffer).str());
    FactoryTableIt it;

    if (getFactory(sha_key, it)) {
        Sllvm_dsp_factory sfactory = (*it).first;
        sfactory->addReference();
        return sfactory;
    } else {
        string error_msg;
        try {
            llvm_dsp_factory* factory =
                checkDSPFactory(new llvm_dsp_factory(sha_key, MEMORY_BUFFER_GET(buffer).str(), target), error_msg);
            llvm_dsp_factory::gFactoryTable[factory] = list<llvm_dsp_aux*>();
            return factory;
        } catch (faustexception& e) {
            e.PrintMessage();
            return NULL;
        }
    }
}

// machine <==> string
EXPORT llvm_dsp_factory* readDSPFactoryFromMachine(const std::string& machine_code, const std::string& target)
{
    TLock lock(gDSPFactoriesLock);

    return readDSPFactoryFromMachineAux(MEMORY_BUFFER_CREATE(StringRef(base64_decode(machine_code))), target);
}

// machine <==> file
EXPORT llvm_dsp_factory* readDSPFactoryFromMachineFile(const std::string& machine_code_path, const std::string& target)
{
    TLock lock(gDSPFactoriesLock);

    ErrorOr<OwningPtr<MemoryBuffer>> buffer = MemoryBuffer::getFileOrSTDIN(machine_code_path);
    if (std::error_code ec = buffer.getError()) {
        printf("readDSPFactoryFromMachineFile failed : %s\n", ec.message().c_str());
        return NULL;
    } else {
        return readDSPFactoryFromMachineAux(MEMORY_BUFFER_GET_REF(buffer), target);
    }
}

EXPORT void metadataDSPFactory(llvm_dsp_factory* factory, Meta* m)
{
    TLock lock(gDSPFactoriesLock);

    if (factory && m) {
        factory->metadataDSPFactory(m);
    }
}

// Instance

EXPORT llvm_dsp* createDSPInstance(llvm_dsp_factory* factory)
{
    TLock lock(gDSPFactoriesLock);

    FactoryTableIt it;
    if ((it = llvm_dsp_factory::gFactoryTable.find(factory)) != llvm_dsp_factory::gFactoryTable.end()) {
        llvm_dsp_aux* instance = factory->createDSPInstance();
        (*it).second.push_back(instance);
        return reinterpret_cast<llvm_dsp*>(instance);
    } else {
        return NULL;
    }
}

EXPORT void deleteDSPInstance(llvm_dsp* dsp)
{
    TLock lock(gDSPFactoriesLock);

    if (dsp) {
        FactoryTableIt    it;
        llvm_dsp_aux*     dsp_aux = reinterpret_cast<llvm_dsp_aux*>(dsp);
        llvm_dsp_factory* factory = dsp_aux->getFactory();

        it = llvm_dsp_factory::gFactoryTable.find(factory);
        faustassert(it != llvm_dsp_factory::gFactoryTable.end());
        (*it).second.remove(dsp_aux);

        delete dsp_aux;
    }
}

EXPORT void llvm_dsp::metadata(Meta* m)
{
    reinterpret_cast<llvm_dsp_aux*>(this)->metadata(m);
}

EXPORT int llvm_dsp::getNumInputs()
{
    return reinterpret_cast<llvm_dsp_aux*>(this)->getNumInputs();
}

int EXPORT llvm_dsp::getNumOutputs()
{
    return reinterpret_cast<llvm_dsp_aux*>(this)->getNumOutputs();
}

EXPORT void llvm_dsp::init(int samplingFreq)
{
    reinterpret_cast<llvm_dsp_aux*>(this)->init(samplingFreq);
}

EXPORT void llvm_dsp::buildUserInterface(UI* ui_interface)
{
    reinterpret_cast<llvm_dsp_aux*>(this)->buildUserInterface(ui_interface);
}

EXPORT void llvm_dsp::compute(int count, FAUSTFLOAT** input, FAUSTFLOAT** output)
{
    reinterpret_cast<llvm_dsp_aux*>(this)->compute(count, input, output);
}

EXPORT llvm_dsp* llvm_dsp::copy()
{
    return reinterpret_cast<llvm_dsp*>(reinterpret_cast<llvm_dsp_aux*>(this)->copy());
}

// Public C interface : lock management is done by called C++ API

EXPORT llvm_dsp_factory* getCDSPFactoryFromSHAKey(const char* sha_key)
{
    return getDSPFactoryFromSHAKey(sha_key);
}

EXPORT llvm_dsp_factory* createCDSPFactoryFromString(const char* name_app, const char* dsp_content, int argc,
                                                     const char* argv[], const char* target, char* error_msg,
                                                     int opt_level)
{
    return NULL;
}

EXPORT const char** getAllCDSPFactories()
{
    vector<string> sha_key_list1 = getAllDSPFactories();
    const char**   sha_key_list2 = (const char**)malloc(sizeof(char*) * (sha_key_list1.size() + 1));

    size_t i;
    for (i = 0; i < sha_key_list1.size(); i++) {
        sha_key_list2[i] = strdup(sha_key_list1[i].c_str());
    }

    // Last element is NULL
    sha_key_list2[i] = NULL;
    return sha_key_list2;
}

EXPORT bool startMTCDSPFactories()
{
    return startMTDSPFactories();
}

EXPORT void stopMTCDSPFactories()
{
    stopMTDSPFactories();
}

EXPORT bool deleteCDSPFactory(llvm_dsp_factory* factory)
{
    return deleteDSPFactory(factory);
}

EXPORT char* getCName(llvm_dsp_factory* factory)
{
    return (factory) ? strdup(factory->getName().c_str()) : NULL;
}

EXPORT char* getCSHAKey(llvm_dsp_factory* factory)
{
    return (factory) ? strdup(factory->getSHAKey().c_str()) : NULL;
}

EXPORT char* getCDSPCode(llvm_dsp_factory* factory)
{
    return (factory) ? strdup(factory->getDSPCode().c_str()) : NULL;
}

EXPORT char* getCTarget(llvm_dsp_factory* factory)
{
    return (factory) ? strdup(factory->getTarget().c_str()) : NULL;
}

EXPORT char* getCDSPMachineTarget()
{
    return strdup(getDSPMachineTarget().c_str());
}

EXPORT void deleteAllCDSPFactories()
{
    deleteAllDSPFactories();
}

EXPORT llvm_dsp_factory* readCDSPFactoryFromMachine(const char* machine_code, const char* target)
{
    return readDSPFactoryFromMachine(machine_code, target);
}

EXPORT llvm_dsp_factory* readCDSPFactoryFromMachineFile(const char* machine_code_path, const char* target)
{
    return readDSPFactoryFromMachineFile(machine_code_path, target);
}

EXPORT char* writeCDSPFactoryToMachine(llvm_dsp_factory* factory, const char* target)
{
    return NULL;
}

EXPORT void writeCDSPFactoryToMachineFile(llvm_dsp_factory* factory, const char* machine_code_path, const char* target)
{
}

EXPORT void metadataCDSPFactory(llvm_dsp_factory* factory, MetaGlue* glue)
{
    if (factory) {
        factory->metadataDSPFactory(glue);
    }
}

EXPORT void metadataCDSPInstance(llvm_dsp* dsp, MetaGlue* glue)
{
    if (dsp) {
        reinterpret_cast<llvm_dsp_aux*>(dsp)->metadata(glue);
    }
}

EXPORT int getNumInputsCDSPInstance(llvm_dsp* dsp)
{
    return (dsp) ? reinterpret_cast<llvm_dsp_aux*>(dsp)->getNumInputs() : 0;
}

EXPORT int getNumOutputsCDSPInstance(llvm_dsp* dsp)
{
    return (dsp) ? reinterpret_cast<llvm_dsp_aux*>(dsp)->getNumOutputs() : 0;
}

EXPORT void initCDSPInstance(llvm_dsp* dsp, int samplingFreq)
{
    if (dsp) {
        reinterpret_cast<llvm_dsp_aux*>(dsp)->init(samplingFreq);
    }
}

EXPORT void buildUserInterfaceCDSPInstance(llvm_dsp* dsp, UIGlue* glue)
{
    if (dsp) {
        reinterpret_cast<llvm_dsp_aux*>(dsp)->buildUserInterface(glue);
    }
}

EXPORT void computeCDSPInstance(llvm_dsp* dsp, int count, FAUSTFLOAT** input, FAUSTFLOAT** output)
{
    if (dsp) {
        reinterpret_cast<llvm_dsp_aux*>(dsp)->compute(count, input, output);
    }
}

EXPORT llvm_dsp* copyCDSPInstance(llvm_dsp* dsp)
{
    return (dsp) ? reinterpret_cast<llvm_dsp*>(reinterpret_cast<llvm_dsp_aux*>(dsp)->copy()) : 0;
}

EXPORT llvm_dsp* createCDSPInstance(llvm_dsp_factory* factory)
{
    return (factory) ? reinterpret_cast<llvm_dsp*>(factory->createDSPInstance()) : 0;
}

EXPORT void deleteCDSPInstance(llvm_dsp* dsp)
{
    if (dsp) {
        delete reinterpret_cast<llvm_dsp_aux*>(dsp);
    }
}

EXPORT string generateSHA1(const string& dsp_content)
{
    // compute SHA1 key
    unsigned char obuf[20];
    SHA1((const unsigned char*)dsp_content.c_str(), dsp_content.size(), obuf);

    // convert SHA1 key into hexadecimal string
    string sha1key;
    for (int i = 0; i < 20; i++) {
        const char* H  = "0123456789ABCDEF";
        char        c1 = H[(obuf[i] >> 4)];
        char        c2 = H[(obuf[i] & 15)];
        sha1key += c1;
        sha1key += c2;
    }

    return sha1key;
}

EXPORT void generateCSHA1(const char* data, char* sha_key)
{
    string res = generateSHA1(data);
    strncpy(sha_key, res.c_str(), 64);
}

EXPORT void freeCMemory(void* ptr)
{
    free(ptr);
}