From c387d7e0eca1b7016468ea0710d814219b3fb2d9 Mon Sep 17 00:00:00 2001 From: Stephane Letz Date: Sat, 25 Nov 2017 13:05:56 +0100 Subject: [PATCH] Correct wast backend -fm code generation, add use of 'tee-local' in FunctionInliner, cleanup. --- compiler/generator/fir_to_fir.hh | 11 +++- compiler/generator/instructions.cpp | 40 ------------ compiler/generator/instructions.hh | 11 +++- compiler/generator/typing_instructions.hh | 12 ++-- compiler/generator/wasm/wast_instructions.hh | 4 +- compiler/global.cpp | 61 ++++++++++++++----- compiler/global.hh | 16 ++++- .../faust4processing/src/libfaust_ext.cpp | 15 +++-- embedded/faust4processing/src/libfaust_ext.h | 15 +++-- embedded/faustcsound/faustcsound.cpp | 2 + embedded/faustjava/libfaust_ext.cpp | 15 +++-- embedded/faustjava/libfaust_ext.h | 13 ++-- 12 files changed, 126 insertions(+), 89 deletions(-) diff --git a/compiler/generator/fir_to_fir.hh b/compiler/generator/fir_to_fir.hh index af9df2dd6b..c56a976754 100644 --- a/compiler/generator/fir_to_fir.hh +++ b/compiler/generator/fir_to_fir.hh @@ -430,10 +430,17 @@ struct FunctionInliner { if (fVarTable.find(fNamed->fName) == fVarTable.end()) { // Create a stack variable with the value string tmp_in = gGlobal->getFreshID("tmp_in"); - fBlockStack.top()->pushBackInst(InstBuilder::genDecStackVar(tmp_in, fNamed->fType->clone(&cloner), fArg->clone(&cloner))); fVarTable[fNamed->fName] = tmp_in; + if (gGlobal->gHasTeeLocal) { + fBlockStack.top()->pushBackInst(InstBuilder::genDecStackVar(tmp_in, fNamed->fType->clone(&cloner))); + return InstBuilder::genTeeVar(tmp_in, fArg->clone(&cloner)); + } else { + fBlockStack.top()->pushBackInst(InstBuilder::genDecStackVar(tmp_in, fNamed->fType->clone(&cloner), fArg->clone(&cloner))); + return InstBuilder::genLoadStackVar(tmp_in); + } + } else { + return InstBuilder::genLoadStackVar(fVarTable[fNamed->fName]); } - return InstBuilder::genLoadStackVar(fVarTable[fNamed->fName]); } } else { return inst->clone(&cloner); diff --git a/compiler/generator/instructions.cpp b/compiler/generator/instructions.cpp index aa7159bfe2..ef4d2d0ad6 100644 --- a/compiler/generator/instructions.cpp +++ b/compiler/generator/instructions.cpp @@ -23,7 +23,6 @@ #include "sigtype.hh" #include "global.hh" #include "floats.hh" -#include "global.hh" std::stack BasicCloneVisitor::fBlockStack; @@ -142,45 +141,6 @@ Typed* BasicCloneVisitor::visit(BasicTyped* typed) return gGlobal->gTypeTable[typed->fType]; } -void Typed::init() -{ - gGlobal->gTypeSizeMap.clear(); - - gGlobal->gTypeSizeMap[Typed::kFloat] = gGlobal->gMachineFloatSize; - gGlobal->gTypeSizeMap[Typed::kFloat_ptr] = gGlobal->gMachinePtrSize; - gGlobal->gTypeSizeMap[Typed::kFloat_vec] = gGlobal->gMachineFloatSize * gGlobal->gVecSize; - gGlobal->gTypeSizeMap[Typed::kFloat_vec_ptr] = gGlobal->gMachinePtrSize; - - gGlobal->gTypeSizeMap[Typed::kInt32] = gGlobal->gMachineInt32Size; - gGlobal->gTypeSizeMap[Typed::kInt32_ptr] = gGlobal->gMachinePtrSize; - gGlobal->gTypeSizeMap[Typed::kInt32_vec] = gGlobal->gMachineInt32Size * gGlobal->gVecSize; - gGlobal->gTypeSizeMap[Typed::kInt32_vec_ptr] = gGlobal->gMachinePtrSize; - - gGlobal->gTypeSizeMap[Typed::kInt64] = gGlobal->gMachineInt64Size; - gGlobal->gTypeSizeMap[Typed::kInt64_ptr] = gGlobal->gMachinePtrSize; - gGlobal->gTypeSizeMap[Typed::kInt64_vec] = gGlobal->gMachineInt64Size * gGlobal->gVecSize; - gGlobal->gTypeSizeMap[Typed::kInt64_vec_ptr] = gGlobal->gMachinePtrSize; - - gGlobal->gTypeSizeMap[Typed::kDouble] = gGlobal->gMachineDoubleSize; - gGlobal->gTypeSizeMap[Typed::kDouble_ptr] = gGlobal->gMachinePtrSize; - gGlobal->gTypeSizeMap[Typed::kDouble_vec] = gGlobal->gMachineDoubleSize * gGlobal->gVecSize; - gGlobal->gTypeSizeMap[Typed::kDouble_vec_ptr] = gGlobal->gMachinePtrSize; - - gGlobal->gTypeSizeMap[Typed::kBool] = gGlobal->gMachineBoolSize; - gGlobal->gTypeSizeMap[Typed::kBool_ptr] = gGlobal->gMachinePtrSize; - gGlobal->gTypeSizeMap[Typed::kBool_vec] = gGlobal->gMachineBoolSize * gGlobal->gVecSize; - gGlobal->gTypeSizeMap[Typed::kBool_vec_ptr] = gGlobal->gMachinePtrSize; - - // Takes the type of internal real - gGlobal->gTypeSizeMap[Typed::kFloatMacro] = gGlobal->gTypeSizeMap[itfloat()]; - gGlobal->gTypeSizeMap[Typed::kFloatMacro_ptr] = gGlobal->gMachinePtrSize; - - gGlobal->gTypeSizeMap[Typed::kVoid_ptr] = gGlobal->gMachinePtrSize; - gGlobal->gTypeSizeMap[Typed::kVoid_ptr_ptr] = gGlobal->gMachinePtrSize; - - gGlobal->gTypeSizeMap[Typed::kObj_ptr] = gGlobal->gMachinePtrSize; -} - bool BlockInst::hasReturn() { list::const_iterator it = fCode.end(); it--; diff --git a/compiler/generator/instructions.hh b/compiler/generator/instructions.hh index 0a468dd4de..f353aa4449 100644 --- a/compiler/generator/instructions.hh +++ b/compiler/generator/instructions.hh @@ -335,8 +335,6 @@ struct Typed : public Printable static string gTypeString[]; - static void init(); - Typed() {} @@ -1522,7 +1520,14 @@ class BasicCloneVisitor : public CloneVisitor { virtual StatementInst* visit(DropInst* inst) { return new DropInst((inst->fResult) ? inst->fResult->clone(this) : NULL); } // Conditionnal - virtual ValueInst* visit(Select2Inst* inst) { return new Select2Inst(inst->fCond->clone(this), inst->fThen->clone(this), inst->fElse->clone(this), inst->fSize); } + virtual ValueInst* visit(Select2Inst* inst) + { + ValueInst* then_exp = inst->fThen->clone(this); + ValueInst* else_exp = inst->fElse->clone(this); + ValueInst* cond_exp = inst->fCond->clone(this); + // cond_exp has to be evaluated last for FunctionInliner to correctly work in gHasTeeLocal mode + return new Select2Inst(cond_exp, then_exp, else_exp); + } virtual StatementInst* visit(IfInst* inst) { return new IfInst(inst->fCond->clone(this), dynamic_cast(inst->fThen->clone(this)), dynamic_cast(inst->fElse->clone(this))); diff --git a/compiler/generator/typing_instructions.hh b/compiler/generator/typing_instructions.hh index d8d213879f..93d6d52dd4 100644 --- a/compiler/generator/typing_instructions.hh +++ b/compiler/generator/typing_instructions.hh @@ -45,8 +45,8 @@ struct TypingVisitor : public InstVisitor { //dump2FIR(inst); // Stack or struct variables - if (gGlobal->gVarTypeTable.find(inst->getName()) != gGlobal->gVarTypeTable.end()) { - fCurType = gGlobal->gVarTypeTable[inst->getName()]->getType(); + if (gGlobal->hasVarType(inst->getName())) { + fCurType = gGlobal->getVarType(inst->getName()); if (dynamic_cast(inst->fAddress)) { fCurType = Typed::getTypeFromPtr(fCurType); } @@ -60,8 +60,8 @@ struct TypingVisitor : public InstVisitor { virtual void visit(TeeVarInst* inst) { - if (gGlobal->gVarTypeTable.find(inst->getName()) != gGlobal->gVarTypeTable.end()) { - fCurType = gGlobal->gVarTypeTable[inst->getName()]->getType(); + if (gGlobal->hasVarType(inst->getName())) { + fCurType = gGlobal->getVarType(inst->getName()); } else { fCurType = Typed::kNoType; } @@ -150,8 +150,8 @@ struct TypingVisitor : public InstVisitor { virtual void visit(FunCallInst* inst) { - if (gGlobal->gVarTypeTable.find(inst->fName) != gGlobal->gVarTypeTable.end()) { - fCurType = gGlobal->gVarTypeTable[inst->fName]->getType(); + if (gGlobal->hasVarType(inst->fName)) { + fCurType = gGlobal->getVarType(inst->fName); } else { // Should never happen... faustassert(false); diff --git a/compiler/generator/wasm/wast_instructions.hh b/compiler/generator/wasm/wast_instructions.hh index c92a8a3c16..6d67b8c0d0 100644 --- a/compiler/generator/wasm/wast_instructions.hh +++ b/compiler/generator/wasm/wast_instructions.hh @@ -177,7 +177,7 @@ class WASTInstVisitor : public TextInstVisitor, public WASInst { if (desc.fMode == MathFunDesc::Gen::kExtMath || desc.fMode == MathFunDesc::Gen::kExtWAS) { tab(fTab, *fOut); if (desc.fMode == MathFunDesc::Gen::kExtMath || desc.fMode == MathFunDesc::Gen::kExtWAS) { - *fOut << "(import $" << inst->fName << " \"env\" \"" << gGlobal->getMathFunction(desc.fName) << "\" (param "; + *fOut << "(import $" << inst->fName << " \"env\" \"" << gGlobal->getMathFunction(inst->fName) << "\" (param "; } else { faustassert(false); } @@ -523,7 +523,7 @@ class WASTInstVisitor : public TextInstVisitor, public WASInst { *fOut << "(" << realStr << "." << desc.fName << " "; } } else { - *fOut << "(call $" << gGlobal->getMathFunction(inst->fName) << " "; + *fOut << "(call $" << inst->fName << " "; } } else { *fOut << "(call $" << inst->fName << " "; diff --git a/compiler/global.cpp b/compiler/global.cpp index 02509e909d..5ef011dbea 100644 --- a/compiler/global.cpp +++ b/compiler/global.cpp @@ -431,29 +431,29 @@ void global::init() gAllocationCount = 0; gEnableFlag = true; - TINT = makeSimpleType(kInt, kKonst, kComp, kVect, kNum, interval()); - TREAL = makeSimpleType(kReal, kKonst, kComp, kVect, kNum, interval()); + TINT = makeSimpleType(kInt, kKonst, kComp, kVect, kNum, interval()); + TREAL = makeSimpleType(kReal, kKonst, kComp, kVect, kNum, interval()); TKONST = makeSimpleType(kInt, kKonst, kComp, kVect, kNum, interval()); TBLOCK = makeSimpleType(kInt, kBlock, kComp, kVect, kNum, interval()); - TSAMP = makeSimpleType(kInt, kSamp, kComp, kVect, kNum, interval()); + TSAMP = makeSimpleType(kInt, kSamp, kComp, kVect, kNum, interval()); - TCOMP = makeSimpleType(kInt, kKonst, kComp, kVect, kNum, interval()); - TINIT = makeSimpleType(kInt, kKonst, kInit, kVect, kNum, interval()); - TEXEC = makeSimpleType(kInt, kKonst, kExec, kVect, kNum, interval()); + TCOMP = makeSimpleType(kInt, kKonst, kComp, kVect, kNum, interval()); + TINIT = makeSimpleType(kInt, kKonst, kInit, kVect, kNum, interval()); + TEXEC = makeSimpleType(kInt, kKonst, kExec, kVect, kNum, interval()); // more predefined types - TINPUT = makeSimpleType(kReal, kSamp, kExec, kVect, kNum, interval()); - TGUI = makeSimpleType(kReal, kBlock,kExec, kVect, kNum, interval()); - TGUI01 = makeSimpleType(kReal, kBlock,kExec, kVect, kNum, interval(0,1)); - INT_TGUI = makeSimpleType(kInt, kBlock,kExec, kVect, kNum, interval()); + TINPUT = makeSimpleType(kReal, kSamp, kExec, kVect, kNum, interval()); + TGUI = makeSimpleType(kReal, kBlock,kExec, kVect, kNum, interval()); + TGUI01 = makeSimpleType(kReal, kBlock,kExec, kVect, kNum, interval(0,1)); + INT_TGUI = makeSimpleType(kInt, kBlock,kExec, kVect, kNum, interval()); - TREC = makeSimpleType(kInt, kSamp, kInit, kScal, kNum, interval()); + TREC = makeSimpleType(kInt, kSamp, kInit, kScal, kNum, interval()); // predefined symbols CONS and NIL CONS = symbol("cons"); - NIL = symbol("nil"); + NIL = symbol("nil"); // predefined nil tree nil = tree(NIL); @@ -486,8 +486,41 @@ void global::init() gLatexheaderfilename = "latexheader.tex"; gDocTextsDefaultFile = "mathdoctexts-default.txt"; - - Typed::init(); + + // Init type size table + gTypeSizeMap[Typed::kFloat] = gMachineFloatSize; + gTypeSizeMap[Typed::kFloat_ptr] = gMachinePtrSize; + gTypeSizeMap[Typed::kFloat_vec] = gMachineFloatSize * gVecSize; + gTypeSizeMap[Typed::kFloat_vec_ptr] = gMachinePtrSize; + + gTypeSizeMap[Typed::kInt32] = gMachineInt32Size; + gTypeSizeMap[Typed::kInt32_ptr] = gMachinePtrSize; + gTypeSizeMap[Typed::kInt32_vec] = gMachineInt32Size * gVecSize; + gTypeSizeMap[Typed::kInt32_vec_ptr] = gMachinePtrSize; + + gTypeSizeMap[Typed::kInt64] = gMachineInt64Size; + gTypeSizeMap[Typed::kInt64_ptr] = gMachinePtrSize; + gTypeSizeMap[Typed::kInt64_vec] = gMachineInt64Size * gVecSize; + gTypeSizeMap[Typed::kInt64_vec_ptr] = gMachinePtrSize; + + gTypeSizeMap[Typed::kDouble] = gMachineDoubleSize; + gTypeSizeMap[Typed::kDouble_ptr] = gMachinePtrSize; + gTypeSizeMap[Typed::kDouble_vec] = gMachineDoubleSize * gVecSize; + gTypeSizeMap[Typed::kDouble_vec_ptr] = gMachinePtrSize; + + gTypeSizeMap[Typed::kBool] = gMachineBoolSize; + gTypeSizeMap[Typed::kBool_ptr] = gMachinePtrSize; + gTypeSizeMap[Typed::kBool_vec] = gMachineBoolSize * gVecSize; + gTypeSizeMap[Typed::kBool_vec_ptr] = gMachinePtrSize; + + // Takes the type of internal real + gTypeSizeMap[Typed::kFloatMacro] = gTypeSizeMap[itfloat()]; + gTypeSizeMap[Typed::kFloatMacro_ptr] = gMachinePtrSize; + + gTypeSizeMap[Typed::kVoid_ptr] = gMachinePtrSize; + gTypeSizeMap[Typed::kVoid_ptr_ptr] = gMachinePtrSize; + + gTypeSizeMap[Typed::kObj_ptr] = gMachinePtrSize; gCurrentLocal = setlocale(LC_ALL, NULL); if (gCurrentLocal != NULL) { diff --git a/compiler/global.hh b/compiler/global.hh index 5b8c304aad..e2cc90fd30 100644 --- a/compiler/global.hh +++ b/compiler/global.hh @@ -438,9 +438,9 @@ struct global { map gBackLink; // link to enclosing file for sub schema // FIR - map gTypeTable; - map gVarTypeTable; // Types of variables or functions - map gTypeSizeMap; // Size of types in bytes + map gTypeTable; // To share a unique BasicTyped* object for a given type + map gVarTypeTable; // Types of variables or functions + map gTypeSizeMap; // Size of types in bytes // colorize map gColorMap; @@ -518,6 +518,16 @@ struct global { return name; } } + + bool hasVarType(const string& name) + { + return gVarTypeTable.find(name) != gVarTypeTable.end(); + } + + Typed::VarType getVarType(const string& name) + { + return gVarTypeTable[name]->getType(); + } }; // Unique shared global pointer diff --git a/embedded/faust4processing/src/libfaust_ext.cpp b/embedded/faust4processing/src/libfaust_ext.cpp index a642d3f242..0cea039e1b 100644 --- a/embedded/faust4processing/src/libfaust_ext.cpp +++ b/embedded/faust4processing/src/libfaust_ext.cpp @@ -47,8 +47,10 @@ using namespace std; static char gLastError[256]; -llvm_dsp_factory* createCDSPFactoryFromFileAux(const char* filename, const char* argv, - const char* target, int opt_level) +llvm_dsp_factory* createCDSPFactoryFromFileAux(const char* filename, + const char* argv, + const char* target, + int opt_level) { int argc1 = 0; const char* argv1[64]; @@ -69,8 +71,11 @@ llvm_dsp_factory* createCDSPFactoryFromFileAux(const char* filename, const char* return factory; } -llvm_dsp_factory* createCDSPFactoryFromStringAux(const char* name_app, const char* dsp_content, - const char* argv, const char* target, int opt_level) +llvm_dsp_factory* createCDSPFactoryFromStringAux(const char* name_app, + const char* dsp_content, + const char* argv, + const char* target, + int opt_level) { int argc1 = 0; const char* argv1[64]; @@ -94,4 +99,4 @@ llvm_dsp_factory* createCDSPFactoryFromStringAux(const char* name_app, const cha const char* getCDSPLastError() { return gLastError; -} \ No newline at end of file +} diff --git a/embedded/faust4processing/src/libfaust_ext.h b/embedded/faust4processing/src/libfaust_ext.h index a5a2fd325c..80355f6dae 100644 --- a/embedded/faust4processing/src/libfaust_ext.h +++ b/embedded/faust4processing/src/libfaust_ext.h @@ -35,11 +35,16 @@ #include "faust/dsp/llvm-c-dsp.h" -llvm_dsp_factory* createCDSPFactoryFromFileAux(const char* filename, const char* argv, - const char* target, int opt_level); +llvm_dsp_factory* createCDSPFactoryFromFileAux(const char* filename, + const char* argv, + const char* target, + int opt_level); -llvm_dsp_factory* createCDSPFactoryFromStringAux(const char* name_app, const char* dsp_content, - const char* argv, const char* target, int opt_level); +llvm_dsp_factory* createCDSPFactoryFromStringAux(const char* name_app, + const char* dsp_content, + const char* argv, + const char* target, + int opt_level); const char* getCDSPLastError(); - \ No newline at end of file + diff --git a/embedded/faustcsound/faustcsound.cpp b/embedded/faustcsound/faustcsound.cpp index 31db1bd374..a525c99f0f 100644 --- a/embedded/faustcsound/faustcsound.cpp +++ b/embedded/faustcsound/faustcsound.cpp @@ -124,6 +124,8 @@ class controls : public UI { FAUSTFLOAT min, FAUSTFLOAT max){}; virtual void addVerticalBargraph(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT min, FAUSTFLOAT max) {}; + + void addSoundfile(const char* label, const char* filename, Soundfile** sf_zone) {} MYFLT *getZone(char *label){ ctl *pctl = &anchor; diff --git a/embedded/faustjava/libfaust_ext.cpp b/embedded/faustjava/libfaust_ext.cpp index a642d3f242..0cea039e1b 100644 --- a/embedded/faustjava/libfaust_ext.cpp +++ b/embedded/faustjava/libfaust_ext.cpp @@ -47,8 +47,10 @@ using namespace std; static char gLastError[256]; -llvm_dsp_factory* createCDSPFactoryFromFileAux(const char* filename, const char* argv, - const char* target, int opt_level) +llvm_dsp_factory* createCDSPFactoryFromFileAux(const char* filename, + const char* argv, + const char* target, + int opt_level) { int argc1 = 0; const char* argv1[64]; @@ -69,8 +71,11 @@ llvm_dsp_factory* createCDSPFactoryFromFileAux(const char* filename, const char* return factory; } -llvm_dsp_factory* createCDSPFactoryFromStringAux(const char* name_app, const char* dsp_content, - const char* argv, const char* target, int opt_level) +llvm_dsp_factory* createCDSPFactoryFromStringAux(const char* name_app, + const char* dsp_content, + const char* argv, + const char* target, + int opt_level) { int argc1 = 0; const char* argv1[64]; @@ -94,4 +99,4 @@ llvm_dsp_factory* createCDSPFactoryFromStringAux(const char* name_app, const cha const char* getCDSPLastError() { return gLastError; -} \ No newline at end of file +} diff --git a/embedded/faustjava/libfaust_ext.h b/embedded/faustjava/libfaust_ext.h index 4f61d2f191..e2a3e26b68 100644 --- a/embedded/faustjava/libfaust_ext.h +++ b/embedded/faustjava/libfaust_ext.h @@ -36,11 +36,16 @@ #include "faust/dsp/llvm-c-dsp.h" #include "faust/dsp/libfaust-c.h" -llvm_dsp_factory* createCDSPFactoryFromFileAux(const char* filename, const char* argv, - const char* target, int opt_level); +llvm_dsp_factory* createCDSPFactoryFromFileAux(const char* filename, + const char* argv, + const char* target, + int opt_level); -llvm_dsp_factory* createCDSPFactoryFromStringAux(const char* name_app, const char* dsp_content, - const char* argv, const char* target, int opt_level); +llvm_dsp_factory* createCDSPFactoryFromStringAux(const char* name_app, + const char* dsp_content, + const char* argv, + const char* target, + int opt_level); const char* getCDSPLastError();