Skip to content

Commit f570c26

Browse files
committed
Simplify -ocpp generateBinOp to 1) remove the kDiv case which is now handled at signal stage 2) always add parenthesis.
1 parent ef8ee74 commit f570c26

File tree

5 files changed

+67
-126
lines changed

5 files changed

+67
-126
lines changed

compiler/generator/compile_scal.cpp

Lines changed: 35 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "timing.hh"
4848
#include "sharing.hh"
4949
#include "xtended.hh"
50+
#include "sigPromotion.hh"
5051

5152
using namespace std;
5253

@@ -97,6 +98,10 @@ Tree ScalarCompiler::prepare(Tree LS)
9798
} else if (gGlobal->gDumpNorm == 1) {
9899
ppsigShared(L1, cout);
99100
throw faustexception("Dump shared normal form finished...\n");
101+
} else if (gGlobal->gDumpNorm == 2) {
102+
// Print signal tree type
103+
SignalTypePrinter printer(L1);
104+
throw faustexception("Dump signal type finished...\n");
100105
}
101106
// No more table privatisation
102107
Tree L2 = L1;
@@ -326,12 +331,13 @@ bool ScalarCompiler::getCompiledExpression(Tree sig, string& cexp)
326331
*/
327332
string ScalarCompiler::setCompiledExpression(Tree sig, const string& cexp)
328333
{
329-
// cerr << "ScalarCompiler::setCompiledExpression : " << cexp << " ==> " << ppsig(sig) << endl;
330334
string old;
331335
if (fCompileProperty.get(sig, old) && (old != cexp)) {
332-
// cerr << "ERROR already a compiled expression attached : " << old << " replaced by " << cexp << endl;
333-
// exit(1);
336+
// stringstream error;
337+
// error << "ERROR already a compiled expression attached : " << old << " replaced by " << cexp << endl;
338+
// throw faustexception(error.str());
334339
}
340+
335341
fCompileProperty.set(sig, cexp);
336342
return cexp;
337343
}
@@ -347,6 +353,7 @@ string ScalarCompiler::setCompiledExpression(Tree sig, const string& cexp)
347353
* @param vecname the string representing the vector name.
348354
* @return true is already compiled
349355
*/
356+
350357
void ScalarCompiler::setVectorNameProperty(Tree sig, const string& vecname)
351358
{
352359
faustassert(vecname.size() > 0);
@@ -371,6 +378,7 @@ bool ScalarCompiler::getVectorNameProperty(Tree sig, string& vecname)
371378
* @param sig the signal expression to compile.
372379
* @return the C code translation of sig as a string
373380
*/
381+
374382
string ScalarCompiler::CS(Tree sig)
375383
{
376384
// contextor contextRecursivness;
@@ -459,9 +467,9 @@ void ScalarCompiler::compileSingleSignal(Tree sig)
459467
string ScalarCompiler::generateCode(Tree sig)
460468
{
461469
#if 0
462-
fprintf(stderr, "CALL generateCode(");
470+
fprintf(stderr, "CALL generateCode(");
463471
printSignal(sig, stderr);
464-
fprintf(stderr, ")\n");
472+
fprintf(stderr, ")\n");
465473
#endif
466474

467475
int i;
@@ -642,53 +650,13 @@ string ScalarCompiler::generateOutput(Tree sig, const string& idx, const string&
642650
return dst;
643651
}
644652

645-
static int binopPriority(Tree sig)
646-
{
647-
int opcode;
648-
Tree arg1;
649-
Tree arg2;
650-
return isSigBinOp(sig, &opcode, arg1, arg2) ? gBinOpTable[opcode]->fPriority : INT_MAX;
651-
}
652653
/*****************************************************************************
653654
BINARY OPERATION
654655
*****************************************************************************/
655656

656657
string ScalarCompiler::generateBinOp(Tree sig, int opcode, Tree arg1, Tree arg2)
657658
{
658-
// check the priorities and add parentheses when needed
659-
int p0 = gBinOpTable[opcode]->fPriority;
660-
int p1 = binopPriority(arg1);
661-
int p2 = binopPriority(arg2);
662-
663-
bool np1 = (p0 > p1) || isLogicalOpcode(opcode);
664-
bool np2 = (p0 > p2) || isLogicalOpcode(opcode);
665-
666-
string c1 = CS(arg1);
667-
string c2 = CS(arg2);
668-
669-
if (opcode == kDiv) {
670-
// special handling for division, we always want a float division
671-
Type t1 = getCertifiedSigType(arg1);
672-
Type t2 = getCertifiedSigType(arg2);
673-
674-
if (t1->nature() == kInt && t2->nature() == kInt) {
675-
return generateCacheCode(sig, subst("($3($0) $1 $3($2))", c1, gBinOpTable[opcode]->fName, c2, ifloat()));
676-
} else if (t1->nature() == kInt && t2->nature() == kReal) {
677-
if (np2) c2 = subst("($0)", c2);
678-
return generateCacheCode(sig, subst("($3($0) $1 $2)", c1, gBinOpTable[opcode]->fName, c2, ifloat()));
679-
} else if (t1->nature() == kReal && t2->nature() == kInt) {
680-
if (np1) c1 = subst("($0)", c1);
681-
return generateCacheCode(sig, subst("($0 $1 $3($2))", c1, gBinOpTable[opcode]->fName, c2, ifloat()));
682-
} else {
683-
if (np1) c1 = subst("($0)", c1);
684-
if (np2) c2 = subst("($0)", c2);
685-
return generateCacheCode(sig, subst("($0 $1 $2)", c1, gBinOpTable[opcode]->fName, c2, ifloat()));
686-
}
687-
} else {
688-
if (np1) c1 = subst("($0)", c1);
689-
if (np2) c2 = subst("($0)", c2);
690-
return generateCacheCode(sig, subst("($0 $1 $2)", c1, gBinOpTable[opcode]->fName, c2));
691-
}
659+
return generateCacheCode(sig, subst("($0 $1 $2)", CS(arg1), gBinOpTable[opcode]->fName, CS(arg2)));
692660
}
693661

694662
/*****************************************************************************
@@ -848,7 +816,7 @@ string ScalarCompiler::generateBitCast(Tree sig, Tree x)
848816
if (gGlobal->gFloatSize == 1) {
849817
return generateCacheCode(sig, subst("(*(int*)&$0)", CS(x)));
850818
} else if (gGlobal->gFloatSize == 2) {
851-
return generateCacheCode(sig, subst("((*(long int*)&$0)", CS(x)));
819+
return generateCacheCode(sig, subst("((*(int64_t*)&$0)", CS(x)));
852820
} else {
853821
faustassert(false);
854822
return "";
@@ -977,23 +945,13 @@ string ScalarCompiler::generateSoundfile(Tree sig, Tree path)
977945
{
978946
string varname = getFreshID("fSoundfile");
979947

980-
// SL
981-
// fClass->addIncludeFile("<atomic>");
982-
// fClass->addIncludeFile("\"faust/gui/soundfile.h\"");
983-
984-
// SL
985-
// fClass->addDeclCode(subst("std::atomic<Soundfile*> \t$0;", varname));
986-
fClass->addDeclCode(subst("Soundfile* \t$0;", varname));
987-
988-
// fClass->addDeclCode(subst("Soundfile* \t$0cache;", varname));
989948
addUIWidget(reverse(tl(path)), uiWidget(hd(path), tree(varname), sig));
990-
991-
// SL
949+
950+
fClass->addDeclCode(subst("Soundfile* \t$0;", varname));
951+
992952
fClass->addInitUICode(subst("if (uintptr_t($0) == 0) $0 = defaultsound;", varname));
993953
fClass->addFirstPrivateDecl(subst("$0cache", varname));
994954

995-
// SL
996-
// fClass->addZone2(subst("Soundfile* $0cache = $0.exchange(nullptr);", varname));
997955
fClass->addZone2(subst("Soundfile* $0cache = $0;", varname));
998956
fClass->addZone4(subst("$0 = $0cache;", varname));
999957
return varname;
@@ -1047,37 +1005,28 @@ string ScalarCompiler::generateTable(Tree sig, Tree tsize, Tree content)
10471005
string cexp;
10481006
string ctype, vname;
10491007

1050-
// already compiled but check if we need to add declarations
1008+
// Already compiled but check if we need to add declarations
10511009
faustassert(isSigGen(content, g));
10521010
pair<string, string> kvnames;
10531011
if (!fInstanceInitProperty.get(g, kvnames)) {
1054-
// not declared here, we add a declaration
1012+
// Not declared here, we add a declaration
10551013
bool b = fStaticInitProperty.get(g, kvnames);
10561014
faustassert(b);
10571015
fClass->addInitCode(subst("$0 $1;", kvnames.first, kvnames.second));
10581016
}
10591017

1060-
// definition of table name and type
1061-
// TO CHECK !!!!!!!!!
1062-
Type t = getCertifiedSigType(content); //, tEnv);
1063-
1064-
if (t->nature() == kInt) {
1065-
vname = getFreshID("itbl");
1066-
ctype = "int";
1067-
} else {
1068-
vname = getFreshID("ftbl");
1069-
ctype = ifloat();
1070-
}
1071-
1072-
// table declaration
1018+
// Define table name and type
1019+
getTypedNames(getCertifiedSigType(content), "tbl", ctype, vname);
1020+
1021+
// Table declaration
10731022
fClass->addDeclCode(subst("$0 \t$1[$2];", ctype, vname, T(size)));
10741023

1075-
// initialization of the content generator
1024+
// Initialization of the content generator
10761025
fClass->addInitCode(subst("$0.init(sample_rate);", generator));
1077-
// filling the table
1026+
// Filling the table
10781027
fClass->addInitCode(subst("$0.fill($1,$2);", generator, T(size), vname));
10791028

1080-
// returning the table name
1029+
// Returning the table name
10811030
return vname;
10821031
}
10831032

@@ -1107,19 +1056,11 @@ string ScalarCompiler::generateStaticTable(Tree sig, Tree tsize, Tree content)
11071056
}
11081057
}
11091058

1110-
// definition of table name and type
1111-
// TO CHECK !!!!!!!!!
1112-
Type t = getCertifiedSigType(content); //, tEnv);
1113-
1114-
if (t->nature() == kInt) {
1115-
vname = getFreshID("itbl");
1116-
ctype = "int";
1117-
} else {
1118-
vname = getFreshID("ftbl");
1119-
ctype = ifloat();
1120-
}
1059+
// Define table name and type
1060+
getTypedNames(getCertifiedSigType(content), "tbl", ctype, vname);
1061+
11211062

1122-
// table declaration
1063+
// Table declaration
11231064
if (gGlobal->gMemoryManager) {
11241065
fClass->addDeclCode(subst("static $0* \t$1;", ctype, vname));
11251066
fClass->addStaticFields(subst("$0* \t$1::$2 = 0;", ctype, fClass->getClassName(), vname));
@@ -1131,12 +1072,12 @@ string ScalarCompiler::generateStaticTable(Tree sig, Tree tsize, Tree content)
11311072
fClass->addStaticFields(subst("$0 \t$1::$2[$3];", ctype, fClass->getClassName(), vname, T(size)));
11321073
}
11331074

1134-
// initialization of the content generator
1075+
// Initialization of the content generator
11351076
fClass->addStaticInitCode(subst("$0.init(sample_rate);", cexp));
1136-
// filling the table
1077+
// Filling the table
11371078
fClass->addStaticInitCode(subst("$0.fill($1,$2);", cexp, T(size), vname));
11381079

1139-
// returning the table name
1080+
// Returning the table name
11401081
return vname;
11411082
}
11421083

@@ -1285,7 +1226,7 @@ string ScalarCompiler::generatePrefix(Tree sig, Tree x, Tree e)
12851226

12861227
string ScalarCompiler::generateSelect2(Tree sig, Tree sel, Tree s1, Tree s2)
12871228
{
1288-
return generateCacheCode(sig, subst("(($0)?$1:$2)", CS(sel), CS(s2), CS(s1)));
1229+
return generateCacheCode(sig, subst("(($0) ? $1 : $2)", CS(sel), CS(s2), CS(s1)));
12891230
}
12901231

12911232
/*****************************************************************************

compiler/generator/instructions_compiler.cpp

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,9 @@ bool InstructionsCompiler::getCompiledExpression(Tree sig, ValueType& cexp)
331331
}
332332

333333
/**
334-
* Set the string of a compiled expression is already compiled
334+
* Set the ValueType of a compiled expression is already compiled
335335
* @param sig the signal expression to compile.
336-
* @param cexp the string representing the compiled expression.
336+
* @param cexp the ValueType representing the compiled expression.
337337
* @return the cexp (for commodity)
338338
*/
339339
ValueType InstructionsCompiler::setCompiledExpression(Tree sig, const ValueType& cexp)
@@ -1383,7 +1383,7 @@ ValueInst* InstructionsCompiler::generateSigGen(Tree sig, Tree content)
13831383
CodeContainer* subcontainer = signal2Container(cname, content);
13841384
fContainer->addSubContainer(subcontainer);
13851385

1386-
// We must allocate an object of type "cname"
1386+
// We must allocate an object of type "cname"
13871387
Values args;
13881388
if (gGlobal->gMemoryManager && (gGlobal->gOneSample == -1)) {
13891389
args.push_back(InstBuilder::genLoadStaticStructVar("fManager"));
@@ -1392,9 +1392,9 @@ ValueInst* InstructionsCompiler::generateSigGen(Tree sig, Tree content)
13921392
pushInitMethod(InstBuilder::genDecStackVar(
13931393
signame, InstBuilder::genNamedTyped(cname, InstBuilder::genBasicTyped(Typed::kObj_ptr)), obj));
13941394

1395-
// HACK for Rust an Julia backends
1395+
// HACK for Rust an Julia backends
13961396
if (gGlobal->gOutputLang != "rust" && gGlobal->gOutputLang != "julia") {
1397-
// Delete object
1397+
// Delete object
13981398
Values args3;
13991399
args3.push_back(InstBuilder::genLoadStackVar(signame));
14001400
if (gGlobal->gMemoryManager && (gGlobal->gOneSample == -1)) {
@@ -1417,7 +1417,7 @@ ValueInst* InstructionsCompiler::generateStaticSigGen(Tree sig, Tree content)
14171417
CodeContainer* subcontainer = signal2Container(cname, content);
14181418
fContainer->addSubContainer(subcontainer);
14191419

1420-
// We must allocate an object of type "cname"
1420+
// We must allocate an object of type "cname"
14211421
Values args;
14221422
if (gGlobal->gMemoryManager && (gGlobal->gOneSample == -1)) {
14231423
args.push_back(InstBuilder::genLoadStaticStructVar("fManager"));
@@ -1426,9 +1426,9 @@ ValueInst* InstructionsCompiler::generateStaticSigGen(Tree sig, Tree content)
14261426
pushStaticInitMethod(InstBuilder::genDecStackVar(
14271427
signame, InstBuilder::genNamedTyped(cname, InstBuilder::genBasicTyped(Typed::kObj_ptr)), obj));
14281428

1429-
// HACK for Rust and Julia backends
1429+
// HACK for Rust and Julia backends
14301430
if (gGlobal->gOutputLang != "rust" && gGlobal->gOutputLang != "julia") {
1431-
// Delete object
1431+
// Delete object
14321432
Values args3;
14331433
args3.push_back(InstBuilder::genLoadStackVar(signame));
14341434
if (gGlobal->gMemoryManager && (gGlobal->gOneSample == -1)) {
@@ -1459,11 +1459,11 @@ ValueInst* InstructionsCompiler::generateTable(Tree sig, Tree tsize, Tree conten
14591459
Tree g;
14601460
string vname;
14611461

1462-
// already compiled but check if we need to add declarations
1462+
// Already compiled but check if we need to add declarations
14631463
faustassert(isSigGen(content, g));
14641464
pair<string, string> kvnames;
14651465
if (!fInstanceInitProperty.get(g, kvnames)) {
1466-
// not declared here, we add a declaration
1466+
// Not declared here, we add a declaration
14671467
bool b = fStaticInitProperty.get(g, kvnames);
14681468
faustassert(b);
14691469
Values args;
@@ -1785,22 +1785,6 @@ ValueInst* InstructionsCompiler::generatePrefix(Tree sig, Tree x, Tree e)
17851785
return InstBuilder::genLoadStackVar(vtemp);
17861786
}
17871787

1788-
/**
1789-
* Generate code for a unique IOTA variable increased at each sample
1790-
* and used to index ring buffers.
1791-
*/
1792-
void InstructionsCompiler::ensureIotaCode()
1793-
{
1794-
if (fCurrentIOTA == "") {
1795-
fCurrentIOTA = gGlobal->getFreshID("IOTA");
1796-
pushDeclare(InstBuilder::genDecStructVar(fCurrentIOTA, InstBuilder::genInt32Typed()));
1797-
pushClearMethod(InstBuilder::genStoreStructVar(fCurrentIOTA, InstBuilder::genInt32NumInst(0)));
1798-
1799-
FIRIndex value = FIRIndex(InstBuilder::genLoadStructVar(fCurrentIOTA)) + 1;
1800-
pushPostComputeDSPMethod(InstBuilder::genStoreStructVar(fCurrentIOTA, value));
1801-
}
1802-
}
1803-
18041788
/*****************************************************************************
18051789
SELECT
18061790
*****************************************************************************/
@@ -2145,6 +2129,22 @@ ValueInst* InstructionsCompiler::generateDelayLine(ValueInst* exp, Typed::VarTyp
21452129
return exp;
21462130
}
21472131

2132+
/**
2133+
* Generate code for a unique IOTA variable increased at each sample
2134+
* and used to index ring buffers.
2135+
*/
2136+
void InstructionsCompiler::ensureIotaCode()
2137+
{
2138+
if (fCurrentIOTA == "") {
2139+
fCurrentIOTA = gGlobal->getFreshID("IOTA");
2140+
pushDeclare(InstBuilder::genDecStructVar(fCurrentIOTA, InstBuilder::genInt32Typed()));
2141+
pushClearMethod(InstBuilder::genStoreStructVar(fCurrentIOTA, InstBuilder::genInt32NumInst(0)));
2142+
2143+
FIRIndex value = FIRIndex(InstBuilder::genLoadStructVar(fCurrentIOTA)) + 1;
2144+
pushPostComputeDSPMethod(InstBuilder::genStoreStructVar(fCurrentIOTA, value));
2145+
}
2146+
}
2147+
21482148
/*****************************************************************************
21492149
WAVEFORM
21502150
*****************************************************************************/

compiler/generator/text_instructions.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ class TextInstVisitor : public InstVisitor {
232232
* @brief test if a right expression needs parentheses.
233233
*
234234
* @param inst the top binary instruction
235-
* @param arg the left expression
235+
* @param arg the right expression
236236
* @return true if parentheses are needed, false otherwise
237237
*/
238238
virtual bool rightArgNeedsParentheses(BinopInst* inst, ValueInst* arg)

tests/compile-tests/Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ help:
131131
@echo " 'clean' : to remove the folder named using faust version"
132132
@echo "Using a specific version of Faust libraries:"
133133
@echo " 'make FAUSTLIBS=/path/to/libraries'"
134-
134+
135135
gcc1: $(cppobj)
136136

137137
gcc2: $(cobj)
@@ -142,10 +142,10 @@ clean:
142142
#########################################################################
143143
# rules for c/c++ compilation
144144
%.o: %.cpp
145-
$(eval tmp1 := $(patsubst $(version)/cpp/%, $(SAMPLESROOT)/%, $(<D)))
146-
$(eval tmp := $(patsubst $(version)/ocpp/%, $(REGRESSION)/%, $(tmp1)))
145+
$(eval tmp1 := $(patsubst $(version)/cpp/%, $(SAMPLESROOT)/%, $(<D)))
146+
$(eval tmp := $(patsubst $(version)/ocpp/%, $(REGRESSION)/%, $(tmp1)))
147147
$(CXX) -c $(CPP_OPTIONS) -I$(tmp) $< -o $@
148148

149149
%.o: %.c
150-
$(eval tmp := $(patsubst $(version)/c/%, $(SAMPLESROOT)/%, $(<D)))
150+
$(eval tmp := $(patsubst $(version)/c/%, $(SAMPLESROOT)/%, $(<D)))
151151
$(GCC) -c $(C_OPTIONS) -I$(tmp) $< -o $@

0 commit comments

Comments
 (0)