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
|
/************************************************************************
************************************************************************
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 "js_code_container.hh"
#include "Text.hh"
#include "exception.hh"
#include "floats.hh"
#include "global.hh"
using namespace std;
map<string, bool> JAVAScriptInstVisitor::gFunctionSymbolTable;
map<string, string> JAVAScriptInstVisitor::gMathLibTable;
dsp_factory_base* JAVAScriptCodeContainer::produceFactory()
{
return new text_dsp_factory_aux(
fKlassName, "", "",
((dynamic_cast<std::stringstream*>(fOut)) ? dynamic_cast<std::stringstream*>(fOut)->str() : ""), "");
}
CodeContainer* JAVAScriptCodeContainer::createScalarContainer(const string& name, int sub_container_type)
{
return new JAVAScriptScalarCodeContainer(name, 0, 1, fOut, sub_container_type);
}
CodeContainer* JAVAScriptCodeContainer::createContainer(const string& name, int numInputs, int numOutputs, ostream* dst)
{
CodeContainer* container;
if (gGlobal->gMemoryManager) {
throw faustexception("ERROR : -mem not supported for JavaScript\n");
}
if (gGlobal->gFloatSize == 3) {
throw faustexception("ERROR : quad format not supported for JavaScript\n");
}
if (gGlobal->gOpenCLSwitch) {
throw faustexception("ERROR : OpenCL not supported for JavaScript\n");
}
if (gGlobal->gCUDASwitch) {
throw faustexception("ERROR : CUDA not supported for JavaScript\n");
}
if (gGlobal->gOpenMPSwitch) {
throw faustexception("ERROR : OpenMP not supported for JavaScript\n");
} else if (gGlobal->gSchedulerSwitch) {
throw faustexception("ERROR : Scheduler mode not supported for JavaScript\n");
} else if (gGlobal->gVectorSwitch) {
throw faustexception("ERROR : Vector mode not supported for JavaScript\n");
} else {
container = new JAVAScriptScalarCodeContainer(name, numInputs, numOutputs, dst, kInt);
}
return container;
}
// Scalar
JAVAScriptScalarCodeContainer::JAVAScriptScalarCodeContainer(const string& name, int numInputs, int numOutputs,
std::ostream* out, int sub_container_type)
: JAVAScriptCodeContainer(name, numInputs, numOutputs, out)
{
fSubContainerType = sub_container_type;
}
JAVAScriptScalarCodeContainer::~JAVAScriptScalarCodeContainer()
{
}
void JAVAScriptCodeContainer::produceInternal()
{
int n = 0;
// Global declarations
tab(n, *fOut);
fCodeProducer.Tab(n);
generateGlobalDeclarations(&fCodeProducer);
tab(n, *fOut);
*fOut << "function " << fKlassName << "() {";
tab(n + 1, *fOut);
tab(n + 1, *fOut);
// Fields
fCodeProducer.Tab(n + 1);
generateDeclarations(&fCodeProducer);
tab(n + 1, *fOut);
// fKlassName used in method naming for subclasses
produceInfoFunctions(n + 1, fKlassName, "dsp", true, false, &fCodeProducer);
// TODO
// generateInstanceInitFun("instanceInit" + fKlassName, true, false)->accept(&fCodeProducer);
// Inits
tab(n + 1, *fOut);
*fOut << "this.instanceInit" << fKlassName << " = function(samplingFreq) {";
tab(n + 2, *fOut);
fCodeProducer.Tab(n + 2);
generateInit(&fCodeProducer);
generateResetUserInterface(&fCodeProducer);
generateClear(&fCodeProducer);
tab(n + 1, *fOut);
*fOut << "}";
// Fill
string counter = "count";
tab(n + 1, *fOut);
tab(n + 1, *fOut);
*fOut << "this.fill" << fKlassName << " = function" << subst("($0, output) {", counter);
tab(n + 2, *fOut);
fCodeProducer.Tab(n + 2);
generateComputeBlock(&fCodeProducer);
ForLoopInst* loop = fCurLoop->generateScalarLoop(counter);
loop->accept(&fCodeProducer);
tab(n + 1, *fOut);
*fOut << "}";
tab(n, *fOut);
*fOut << "}" << endl;
// Memory methods (as globals)
tab(n, *fOut);
*fOut << "new" << fKlassName << " = function() { "
<< "return new " << fKlassName << "()"
<< "; }";
tab(n, *fOut);
*fOut << "delete" << fKlassName << "= function(dsp) {}";
tab(n, *fOut);
}
void JAVAScriptCodeContainer::produceClass()
{
int n = 0;
// Libraries
printLibrary(*fOut);
// Sub containers
generateSubContainers();
// Global declarations
tab(n, *fOut);
fCodeProducer.Tab(n);
generateGlobalDeclarations(&fCodeProducer);
tab(n, *fOut);
*fOut << "function " << fKlassName << "() {";
tab(n + 1, *fOut);
// Fields
tab(n + 1, *fOut);
fCodeProducer.Tab(n + 1);
generateDeclarations(&fCodeProducer);
// Print metadata declaration
tab(n + 1, *fOut);
*fOut << "this.metadata = function(m) { ";
for (MetaDataSet::iterator i = gGlobal->gMetaDataSet.begin(); i != gGlobal->gMetaDataSet.end(); i++) {
if (i->first != tree("author")) {
tab(n + 2, *fOut);
*fOut << "m.declare(\"" << *(i->first) << "\", " << **(i->second.begin()) << ");";
} else {
for (set<Tree>::iterator j = i->second.begin(); j != i->second.end(); j++) {
if (j == i->second.begin()) {
tab(n + 2, *fOut);
*fOut << "m.declare(\"" << *(i->first) << "\", " << **j << ");";
} else {
tab(n + 2, *fOut);
*fOut << "m.declare(\""
<< "contributor"
<< "\", " << **j << ");";
}
}
}
}
tab(n + 1, *fOut);
*fOut << "}" << endl;
tab(n + 1, *fOut);
// No class name for main class
produceInfoFunctions(n + 1, "", "dsp", true, true, &fCodeProducer);
// Inits
// TODO
/*
generateStaticInitFun("classInit", false)->accept(&fCodeProducer);
generateInstanceInitFun("instanceInit", true, true)->accept(&fCodeProducer);
*/
tab(n + 1, *fOut);
*fOut << "this.classInit = function(samplingFreq) {";
tab(n + 2, *fOut);
fCodeProducer.Tab(n + 2);
generateStaticInit(&fCodeProducer);
tab(n + 1, *fOut);
*fOut << "}";
tab(n + 1, *fOut);
tab(n + 1, *fOut);
*fOut << "this.instanceConstants = function(samplingFreq) {";
tab(n + 2, *fOut);
fCodeProducer.Tab(n + 2);
generateInit(&fCodeProducer);
*fOut << "this.instanceClear();";
tab(n + 1, *fOut);
*fOut << "}";
tab(n + 1, *fOut);
tab(n + 1, *fOut);
*fOut << "instanceResetUserInterface = function() {";
tab(n + 2, *fOut);
fCodeProducer.Tab(n + 2);
generateResetUserInterface(&fCodeProducer);
tab(n + 1, *fOut);
*fOut << "}";
tab(n + 1, *fOut);
tab(n + 1, *fOut);
*fOut << "this.instanceClear = function() {";
tab(n + 2, *fOut);
fCodeProducer.Tab(n + 2);
generateClear(&fCodeProducer);
tab(n + 1, *fOut);
*fOut << "}";
tab(n + 1, *fOut);
tab(n + 1, *fOut);
*fOut << "this.init = function(samplingFreq) {";
tab(n + 2, *fOut);
*fOut << "this.classInit(samplingFreq);";
tab(n + 2, *fOut);
*fOut << "this.instanceInit(samplingFreq);";
tab(n + 1, *fOut);
*fOut << "}";
tab(n + 1, *fOut);
tab(n + 1, *fOut);
*fOut << "this.instanceInit = function(samplingFreq) {";
tab(n + 2, *fOut);
*fOut << "this.instanceConstants(samplingFreq);";
tab(n + 2, *fOut);
*fOut << "this.instanceResetUserInterface();";
tab(n + 2, *fOut);
*fOut << "this.instanceClear();";
tab(n + 1, *fOut);
*fOut << "}";
// User interface
tab(n + 1, *fOut);
tab(n + 1, *fOut);
*fOut << "this.buildUserInterface = function(ui_interface) {";
tab(n + 2, *fOut);
fCodeProducer.Tab(n + 2);
generateUserInterface(&fCodeProducer);
printlines(n + 2, fUICode, *fOut);
tab(n + 1, *fOut);
*fOut << "}";
// Compute
generateCompute(n);
// Possibly generate separated functions
fCodeProducer.Tab(n + 1);
tab(n + 1, *fOut);
generateComputeFunctions(&fCodeProducer);
tab(n, *fOut);
*fOut << "}\n" << endl;
}
void JAVAScriptScalarCodeContainer::generateCompute(int n)
{
tab(n + 1, *fOut);
tab(n + 1, *fOut);
*fOut << subst("this.compute = function($0, inputs, outputs) {", fFullCount);
tab(n + 2, *fOut);
fCodeProducer.Tab(n + 2);
// Generates local variables declaration and setup
generateComputeBlock(&fCodeProducer);
// Generates one single scalar loop
ForLoopInst* loop = fCurLoop->generateScalarLoop(fFullCount);
loop->accept(&fCodeProducer);
tab(n + 1, *fOut);
*fOut << "}";
}
|