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
|
/************************************************************************
************************************************************************
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 __SIGPROMOTION__
#define __SIGPROMOTION__
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include "description.hh"
#include "ppsig.hh"
#include "sigIdentity.hh"
#include "signalVisitor.hh"
#include "sigtyperules.hh"
/*
Print the type of a signal.
To be used on a type annotated signal.
*/
class SignalTypePrinter final : public SignalVisitor {
private:
std::vector<std::string> fPrinted;
void visit(Tree sig) override;
public:
SignalTypePrinter(Tree L);
std::string print();
};
/*
Check a signal:
- for correct extended typing
- for correct SigBinOp args typing
- for proper SigIntCast and SigFloatCast use
- for correct range in sliders (min < max and default in [min...max] range)
- for correct SigWaveform values typing
- for use on control/enable (not available in -vec mode)
- for proper simplication of SigLowest/SigHighest
To be used on a type annotated signal.
*/
class SignalChecker final : public SignalVisitor {
private:
void visit(Tree sig) override;
void isRange(Tree sig, Tree init_aux, Tree min_aux, Tree max_aux);
public:
SignalChecker(Tree L)
{
// Check that the root tree is properly type annotated
getCertifiedSigType(L);
visitRoot(L);
}
};
//-------------------------SignalPromotion------------------------------
// Adds explicit int or float cast when needed. This is needed prior
// to any optimisations to avoid to scramble int and float expressions.
// To be used on a type annotated signal.
//----------------------------------------------------------------------
class SignalPromotion final : public SignalIdentity {
private:
Tree transformation(Tree sig);
// Cast a sig to t1 if t1 != t2
Tree smartCast(Type t1, Type t2, Tree sig);
Tree smartCast(int t1, int t2, Tree sig);
// Cast a sig to t
Tree cast(Type t, Tree sig);
Tree cast(int t, Tree sig);
// Adds an intCast only if needed
Tree smartIntCast(Type t, Tree sig);
// Adds a floatCast only if needed
Tree smartFloatCast(Type t, Tree sig);
public:
SignalPromotion()
{
// Go inside tables
fVisitGen = true;
}
};
//--------------------SignalBool2IntPromotion------------------
// Cast bool binary operations (comparison operations) to int.
//-------------------------------------------------------------
class SignalBool2IntPromotion final : public SignalIdentity {
private:
Tree transformation(Tree sig);
public:
SignalBool2IntPromotion()
{
// Go inside tables
fVisitGen = true;
}
};
//--------------------SignalFXPromotion------------------
// Special math function casting mode in -fx generation.
//-------------------------------------------------------------
class SignalFXPromotion final : public SignalIdentity {
private:
Tree transformation(Tree sig);
public:
SignalFXPromotion()
{
// Go inside tables
fVisitGen = true;
}
};
//-------------SignalIntCastPromotion---------------
// Float to integer conversion, checking the range.
//--------------------------------------------------
class SignalIntCastPromotion final : public SignalIdentity {
private:
Tree transformation(Tree sig);
public:
SignalIntCastPromotion()
{
// Go inside tables
fVisitGen = true;
}
};
//-------------------------SignalTablePromotion----------------------
// Generate safe access to rdtable/rwtable (wdx/rdx in [0..size-1]).
//-------------------------------------------------------------------
class SignalTablePromotion final : public SignalIdentity {
private:
Tree transformation(Tree sig);
// Safe version of rtable/rwtable access
Tree safeSigRDTbl(Tree sig, Tree tbl, Tree size, Tree ri);
Tree safeSigWRTbl(Tree sig, Tree size, Tree gen, Tree wi, Tree ws);
public:
SignalTablePromotion()
{
// Go inside tables
fVisitGen = true;
}
};
//-------------------------SignalUIPromotion--------------------
// Generate safe access to range UI items (sliders and nentry).
//--------------------------------------------------------------
class SignalUIPromotion final : public SignalIdentity {
private:
Tree transformation(Tree sig);
public:
SignalUIPromotion()
{
// Go inside tables
fVisitGen = true;
}
};
//-------------------------SignalUIFreezePromotion---------------------------
// Freeze range UI items (sliders and nentry) to their init value. Everything
// that depends of sliders and nentry will be computed at compile time.
//---------------------------------------------------------------------------
class SignalUIFreezePromotion final : public SignalIdentity {
private:
Tree transformation(Tree sig);
public:
SignalUIFreezePromotion()
{
// Go inside tables
fVisitGen = true;
}
};
//-------------SignalFTZPromotion---------------
// The wrapping code allows to flush to zero denormalized number.
// This option should be used only when it is not available on the CPU.
//--------------------------------------------------
class SignalFTZPromotion final : public SignalIdentity {
private:
Tree selfRec(Tree t);
public:
SignalFTZPromotion()
{
// Go inside tables
fVisitGen = true;
}
};
//-------------SignalAutoDifferentiate---------------
// Auto Differentiate a signal for a given variable
//--------------------------------------------------
class SignalAutoDifferentiate final : public SignalIdentity {
private:
Tree fVar;
Tree sigZero(int type) { return (type == kInt) ? sigInt(0) : sigReal(0.0); }
Tree sigOne(int type) { return (type == kInt) ? sigInt(1) : sigReal(1.0); }
Tree diff(Tree x, int ty) { return (x == fVar) ? sigOne(ty) : sigZero(ty); }
Tree transformation(Tree sig);
public:
// The variable with respect to which the differentiation is performed.
SignalAutoDifferentiate(Tree var) : fVar(var)
{
if (gGlobal->gDetailsSwitch) {
std::cout << ">>> Differentiate wrt. " << ppsig(var) << "\n";
}
// Go inside tables
fVisitGen = true;
}
};
struct DiffVarCollector : public SignalVisitor {
siglist inputs;
DiffVarCollector(Tree L)
{
while (!isNil(L)) {
self(hd(L));
L = tl(L);
}
}
void visit(Tree sig)
{
Tree label, init, min, max, step;
if (isSigButton(sig, label) || isSigCheckbox(sig, label) ||
isSigVSlider(sig, label, init, min, max, step) ||
isSigHSlider(sig, label, init, min, max, step) ||
isSigNumEntry(sig, label, init, min, max, step)) {
std::string simplifiedLabel;
std::map<std::string, std::set<std::string> > metadata;
extractMetadata(tree2str(hd(label)), simplifiedLabel, metadata);
// Look for [diff:1] or [diff:on]
for (const auto& i : metadata) {
if (i.first == "diff") {
const std::set<std::string>& values = i.second;
for (const auto& j : values) {
if (j == "1" || j == "on") {
inputs.push_back(sig);
break;
}
}
}
}
} else {
SignalVisitor::visit(sig);
}
}
};
// Public API
Tree signalPromote(Tree sig, bool trace = false);
Tree signalBool2IntPromote(Tree sig);
Tree signalFXPromote(Tree sig);
Tree signalTablePromote(Tree sig);
Tree signalIntCastPromote(Tree sig);
Tree signalUIPromote(Tree sig);
Tree signalUIFreezePromote(Tree sig);
Tree signalFTZPromote(Tree sig);
Tree signalAutoDifferentiate(Tree sig);
#endif
|