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
|
/************************************************************************
************************************************************************
FAUST compiler
Copyright (C) 2017 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 "rust_code_container.hh"
#include "Text.hh"
#include "exception.hh"
#include "fir_function_builder.hh"
#include "floats.hh"
#include "global.hh"
using namespace std;
/*
Rust backend description:
- 'usize' type has to be used for all array access: cast index as 'usize' only when using it (load/store arrays)
- TODO: local stack variables (shared computation) are normally non-mutable
- inputN/outputN local buffer variables in 'compute' are not created at all: they are replaced directly in the code
with inputs[N]/outputs[N] (done in instructions_compiler.cpp)
- BoolOpcode BinOps always casted to integer
- 'delete' for SubContainers is not generated
- add 'kMutable' and 'kReference' address access type
*/
map<string, bool> RustInstVisitor::gFunctionSymbolTable;
dsp_factory_base* RustCodeContainer::produceFactory()
{
return new text_dsp_factory_aux(
fKlassName, "", "",
((dynamic_cast<ostringstream*>(fOut)) ? dynamic_cast<ostringstream*>(fOut)->str() : ""), "");
}
CodeContainer* RustCodeContainer::createScalarContainer(const string& name, int sub_container_type)
{
return new RustScalarCodeContainer(name, 0, 1, fOut, sub_container_type);
}
CodeContainer* RustCodeContainer::createContainer(const string& name, int numInputs, int numOutputs, ostream* dst)
{
gGlobal->gDSPStruct = true;
CodeContainer* container;
if (gGlobal->gMemoryManager) {
throw faustexception("ERROR : -mem not supported for Rust\n");
}
if (gGlobal->gFloatSize == 3) {
throw faustexception("ERROR : quad format not supported for Rust\n");
}
if (gGlobal->gOpenCLSwitch) {
throw faustexception("ERROR : OpenCL not supported for Rust\n");
}
if (gGlobal->gCUDASwitch) {
throw faustexception("ERROR : CUDA not supported for Rust\n");
}
if (gGlobal->gOpenMPSwitch) {
// container = new RustOpenMPCodeContainer(name, numInputs, numOutputs, dst);
throw faustexception("ERROR : OpenMP not supported for Rust\n");
} else if (gGlobal->gSchedulerSwitch) {
// container = new RustWorkStealingCodeContainer(name, numInputs, numOutputs, dst);
throw faustexception("ERROR : Scheduler not supported for Rust\n");
} else if (gGlobal->gVectorSwitch) {
// container = new RustVectorCodeContainer(name, numInputs, numOutputs, dst);
throw faustexception("ERROR : Vector not supported for Rust\n");
} else {
container = new RustScalarCodeContainer(name, numInputs, numOutputs, dst, kInt);
}
return container;
}
void RustCodeContainer::produceInternal()
{
int n = 0;
// Global declarations
tab(n, *fOut);
fCodeProducer.Tab(n);
generateGlobalDeclarations(&fCodeProducer);
tab(n, *fOut);
*fOut << "pub struct " << fKlassName << " {";
tab(n + 1, *fOut);
// Fields
fCodeProducer.Tab(n + 1);
generateDeclarations(&fCodeProducer);
back(1, *fOut);
*fOut << "}";
tab(n, *fOut);
tab(n, *fOut);
*fOut << "impl " << fKlassName << " {";
tab(n + 1, *fOut);
tab(n + 1, *fOut);
produceInfoFunctions(n + 1, fKlassName, "&self", false, false, &fCodeProducer);
// Init
// TODO
// generateInstanceInitFun("instanceInit" + fKlassName, false, false)->accept(&fCodeProducer);
tab(n + 1, *fOut);
*fOut << "fn instance_init" << fKlassName << "(&mut self, sample_rate: i32) {";
tab(n + 2, *fOut);
fCodeProducer.Tab(n + 2);
generateInit(&fCodeProducer);
generateResetUserInterface(&fCodeProducer);
generateClear(&fCodeProducer);
back(1, *fOut);
*fOut << "}";
// Fill
tab(n + 1, *fOut);
string counter = "count";
if (fSubContainerType == kInt) {
tab(n + 1, *fOut);
*fOut << "fn fill" << fKlassName << subst("(&mut self, $0: i32, table: &mut[i32]) {", counter);
} else {
tab(n + 1, *fOut);
*fOut << "fn fill" << fKlassName << subst("(&mut self, $0: i32, table: &mut[$1]) {", counter, ifloat());
}
tab(n + 2, *fOut);
fCodeProducer.Tab(n + 2);
generateComputeBlock(&fCodeProducer);
SimpleForLoopInst* loop = fCurLoop->generateSimpleScalarLoop(counter);
loop->accept(&fCodeProducer);
back(1, *fOut);
*fOut << "}" << endl;
tab(n, *fOut);
*fOut << "}" << endl;
// Memory methods
tab(n, *fOut);
tab(n, *fOut);
*fOut << "pub fn new" << fKlassName << "() -> " << fKlassName << " { ";
tab(n + 1, *fOut);
*fOut << fKlassName << " {";
RustInitFieldsVisitor initializer(fOut, n + 2);
generateDeclarations(&initializer);
tab(n + 1, *fOut);
*fOut << "}";
tab(n, *fOut);
*fOut << "}";
}
void RustCodeContainer::produceClass()
{
int n = 0;
// Sub containers
generateSubContainers();
// Functions
tab(n, *fOut);
fCodeProducer.Tab(n);
generateGlobalDeclarations(&fCodeProducer);
*fOut << "pub struct " << fKlassName << " {";
tab(n + 1, *fOut);
// Fields
fCodeProducer.Tab(n + 1);
generateDeclarations(&fCodeProducer);
back(1, *fOut);
*fOut << "}";
tab(n, *fOut);
tab(n, *fOut);
*fOut << "impl FaustDsp for " << fKlassName << " {";
// Associated type
tab(n + 1, *fOut);
*fOut << "type T = " << ifloat() << ";";
// Memory methods
tab(n + 2, *fOut);
if (fAllocateInstructions->fCode.size() > 0) {
tab(n + 2, *fOut);
*fOut << "static void allocate" << fKlassName << "(" << fKlassName << "* dsp) {";
tab(n + 2, *fOut);
fAllocateInstructions->accept(&fCodeProducer);
back(1, *fOut);
*fOut << "}";
}
tab(n + 1, *fOut);
if (fDestroyInstructions->fCode.size() > 0) {
tab(n + 1, *fOut);
*fOut << "static void destroy" << fKlassName << "(" << fKlassName << "* dsp) {";
tab(n + 2, *fOut);
fDestroyInstructions->accept(&fCodeProducer);
back(1, *fOut);
*fOut << "}";
tab(n + 1, *fOut);
}
*fOut << "fn new() -> " << fKlassName << " { ";
if (fAllocateInstructions->fCode.size() > 0) {
tab(n + 2, *fOut);
*fOut << "allocate" << fKlassName << "(dsp);";
}
tab(n + 2, *fOut);
*fOut << fKlassName << " {";
RustInitFieldsVisitor initializer(fOut, n + 3);
generateDeclarations(&initializer);
tab(n + 2, *fOut);
*fOut << "}";
tab(n + 1, *fOut);
*fOut << "}";
// Print metadata declaration
produceMetadata(n + 1);
// Get sample rate method
tab(n + 1, *fOut);
fCodeProducer.Tab(n + 1);
generateGetSampleRate("get_sample_rate", "&self", false, false)->accept(&fCodeProducer);
produceInfoFunctions(n + 1, "", "&self", false, false, &fCodeProducer);
// Inits
// TODO
//
// CInstVisitor codeproducer1(fOut, "");
// codeproducer1.Tab(n+2);
// generateStaticInitFun("classInit" + fKlassName, false)->accept(&codeproducer1);
// generateInstanceInitFun("instanceInit" + fKlassName, false, false)->accept(&codeproducer2);
tab(n + 1, *fOut);
*fOut << "fn class_init(sample_rate: i32) {";
{
tab(n + 2, *fOut);
// Local visitor here to avoid DSP object type wrong generation
RustInstVisitor codeproducer(fOut, "");
codeproducer.Tab(n + 2);
generateStaticInit(&codeproducer);
}
back(1, *fOut);
*fOut << "}";
tab(n + 1, *fOut);
*fOut << "fn instance_reset_params(&mut self) {";
{
tab(n + 2, *fOut);
// Local visitor here to avoid DSP object type wrong generation
RustInstVisitor codeproducer(fOut, "");
codeproducer.Tab(n + 2);
generateResetUserInterface(&codeproducer);
}
back(1, *fOut);
*fOut << "}";
tab(n + 1, *fOut);
*fOut << "fn instance_clear(&mut self) {";
{
tab(n + 2, *fOut);
// Local visitor here to avoid DSP object type wrong generation
RustInstVisitor codeproducer(fOut, "");
codeproducer.Tab(n + 2);
generateClear(&codeproducer);
}
back(1, *fOut);
*fOut << "}";
tab(n + 1, *fOut);
*fOut << "fn instance_constants(&mut self, sample_rate: i32) {";
{
tab(n + 2, *fOut);
// Local visitor here to avoid DSP object type wrong generation
RustInstVisitor codeproducer(fOut, "");
codeproducer.Tab(n + 2);
generateInit(&codeproducer);
}
back(1, *fOut);
*fOut << "}";
tab(n + 1, *fOut);
*fOut << "fn instance_init(&mut self, sample_rate: i32) {";
tab(n + 2, *fOut);
*fOut << "self.instance_constants(sample_rate);";
tab(n + 2, *fOut);
*fOut << "self.instance_reset_params();";
tab(n + 2, *fOut);
*fOut << "self.instance_clear();";
tab(n + 1, *fOut);
*fOut << "}";
tab(n + 1, *fOut);
*fOut << "fn init(&mut self, sample_rate: i32) {";
tab(n + 2, *fOut);
*fOut << fKlassName << "::class_init(sample_rate);";
tab(n + 2, *fOut);
*fOut << "self.instance_init(sample_rate);";
tab(n + 1, *fOut);
*fOut << "}";
// Pre-pass of user interface instructions to determine parameter lookup table (field name => index)
UserInterfaceParameterMapping parameterMappingVisitor;
fUserInterfaceInstructions->accept(¶meterMappingVisitor);
auto parameterLookup = parameterMappingVisitor.getParameterLookup();
// User interface (non-static method)
tab(n + 1, *fOut);
tab(n + 1, *fOut);
*fOut << "fn build_user_interface(&self, ui_interface: &mut dyn UI<Self::T>) {";
tab(n + 2, *fOut);
*fOut << "Self::build_user_interface_static(ui_interface);";
tab(n + 1, *fOut);
*fOut << "}";
// User interface (static method)
tab(n + 1, *fOut);
tab(n + 1, *fOut);
*fOut << "fn build_user_interface_static(ui_interface: &mut dyn UI<Self::T>) {";
tab(n + 2, *fOut);
fCodeProducer.Tab(n + 2);
RustUIInstVisitor uiCodeproducer(fOut, "", parameterLookup, n + 2);
generateUserInterface(&uiCodeproducer);
back(1, *fOut);
*fOut << "}";
// Parameter getter/setter
produceParameterGetterSetter(n + 1, parameterLookup);
// Compute
generateCompute(n + 1);
tab(n, *fOut);
*fOut << "}" << endl;
tab(n, *fOut);
}
void RustCodeContainer::produceMetadata(int n)
{
tab(n, *fOut);
*fOut << "fn metadata(&self, m: &mut dyn Meta) { ";
// We do not want to accumulate metadata from all hierachical levels, so the upper level only is kept
for (auto& i : gGlobal->gMetaDataSet) {
if (i.first != tree("author")) {
tab(n + 1, *fOut);
*fOut << "m.declare(\"" << *(i.first) << "\", " << **(i.second.begin()) << ");";
} else {
// But the "author" meta data is accumulated, the upper level becomes the main author and sub-levels become
// "contributor"
for (set<Tree>::iterator j = i.second.begin(); j != i.second.end(); j++) {
if (j == i.second.begin()) {
tab(n + 1, *fOut);
*fOut << "m.declare(\"" << *(i.first) << "\", " << **j << ");";
} else {
tab(n + 1, *fOut);
*fOut << "m.declare(\""
<< "contributor"
<< "\", " << **j << ");";
}
}
}
}
tab(n, *fOut);
*fOut << "}" << endl;
}
void RustCodeContainer::produceInfoFunctions(int tabs, const string& classname, const string& obj, bool ismethod, bool isvirtual,
TextInstVisitor* producer)
{
producer->Tab(tabs);
generateGetInputs(subst("get_num_inputs$0", classname), obj, false, false)->accept(&fCodeProducer);
generateGetOutputs(subst("get_num_outputs$0", classname), obj, false, false)->accept(&fCodeProducer);
producer->Tab(tabs);
generateGetInputRate(subst("get_input_rate$0", classname), obj, false, false)->accept(&fCodeProducer);
producer->Tab(tabs);
generateGetOutputRate(subst("get_output_rate$0", classname), obj, false, false)->accept(&fCodeProducer);
}
void RustCodeContainer::produceParameterGetterSetter(int tabs, map<string, int> parameterLookup)
{
// Add `get_param`
tab(tabs, *fOut);
tab(tabs, *fOut);
*fOut << "fn get_param(&self, param: ParamIndex) -> Option<Self::T> {";
tab(tabs + 1, *fOut);
*fOut << "match param.0 {";
for (const auto ¶mPair : parameterLookup) {
const auto fieldName = paramPair.first;
const auto index = paramPair.second;
tab(tabs + 2, *fOut);
*fOut << index << " => Some(self." << fieldName << "),";
}
tab(tabs + 2, *fOut);
*fOut << "_ => None,";
tab(tabs + 1, *fOut);
*fOut << "}";
tab(tabs, *fOut);
*fOut << "}";
// Add `set_param`
tab(tabs, *fOut);
tab(tabs, *fOut);
*fOut << "fn set_param(&mut self, param: ParamIndex, value: Self::T) {";
tab(tabs + 1, *fOut);
*fOut << "match param.0 {";
for (const auto ¶mPair : parameterLookup) {
const auto fieldName = paramPair.first;
const auto index = paramPair.second;
tab(tabs + 2, *fOut);
*fOut << index << " => { self." << fieldName << " = value }";
}
tab(tabs + 2, *fOut);
*fOut << "_ => {}";
tab(tabs + 1, *fOut);
*fOut << "}";
tab(tabs, *fOut);
*fOut << "}";
}
// Scalar
RustScalarCodeContainer::RustScalarCodeContainer(const string& name, int numInputs, int numOutputs, std::ostream* out,
int sub_container_type)
: RustCodeContainer(name, numInputs, numOutputs, out)
{
fSubContainerType = sub_container_type;
}
void RustScalarCodeContainer::generateCompute(int n)
{
// Generates declaration
tab(n, *fOut);
tab(n, *fOut);
*fOut << "fn compute("
<< subst("&mut self, $0: i32, inputs: &[&[Self::T]], outputs: &mut[&mut[Self::T]]) {", fFullCount);
tab(n + 1, *fOut);
fCodeProducer.Tab(n + 1);
// Generates local variables declaration and setup
generateComputeBlock(&fCodeProducer);
// Generates one single scalar loop
std::vector<std::string> iterators;
for (int i = 0; i < fNumInputs; ++i) {
iterators.push_back("inputs" + std::to_string(i));
}
for (int i = 0; i < fNumOutputs; ++i) {
iterators.push_back("outputs"+ std::to_string(i));
}
IteratorForLoopInst* loop = fCurLoop->generateSimpleScalarLoop(iterators);
loop->accept(&fCodeProducer);
back(1, *fOut);
*fOut << "}" << endl;
}
// Vector
RustVectorCodeContainer::RustVectorCodeContainer(const string& name, int numInputs, int numOutputs, std::ostream* out)
: VectorCodeContainer(numInputs, numOutputs), RustCodeContainer(name, numInputs, numOutputs, out)
{
}
void RustVectorCodeContainer::generateCompute(int n)
{
// Possibly generate separated functions
fCodeProducer.Tab(n);
tab(n, *fOut);
generateComputeFunctions(&fCodeProducer);
// Compute declaration
tab(n, *fOut);
*fOut << "fn compute("
<< subst("&mut self, $0: i32, inputs: &[&[Self::T]], outputs: &mut[&mut[Self::T]]) {", fFullCount);
tab(n + 1, *fOut);
fCodeProducer.Tab(n + 1);
// Generates local variables declaration and setup
generateComputeBlock(&fCodeProducer);
// Generates the DSP loop
fDAGBlock->accept(&fCodeProducer);
back(1, *fOut);
*fOut << "}" << endl;
}
// OpenMP
RustOpenMPCodeContainer::RustOpenMPCodeContainer(const string& name, int numInputs, int numOutputs, std::ostream* out)
: OpenMPCodeContainer(numInputs, numOutputs), RustCodeContainer(name, numInputs, numOutputs, out)
{
}
void RustOpenMPCodeContainer::generateCompute(int n)
{
// Possibly generate separated functions
fCodeProducer.Tab(n);
tab(n, *fOut);
generateComputeFunctions(&fCodeProducer);
// Compute declaration
tab(n, *fOut);
*fOut << "fn compute("
<< subst("&mut self, $0: i32, inputs: &[&[Self::T]], outputs: &mut[&mut[Self::T]]) {", fFullCount);
tab(n + 1, *fOut);
fCodeProducer.Tab(n + 1);
// Generates local variables declaration and setup
generateComputeBlock(&fCodeProducer);
// Generate it
fGlobalLoopBlock->accept(&fCodeProducer);
back(1, *fOut);
*fOut << "}" << endl;
}
// Works stealing scheduler
RustWorkStealingCodeContainer::RustWorkStealingCodeContainer(const string& name, int numInputs, int numOutputs,
std::ostream* out)
: WSSCodeContainer(numInputs, numOutputs, "dsp"), RustCodeContainer(name, numInputs, numOutputs, out)
{
}
void RustWorkStealingCodeContainer::generateCompute(int n)
{
// Possibly generate separated functions
fCodeProducer.Tab(n);
tab(n, *fOut);
generateComputeFunctions(&fCodeProducer);
// Generates "computeThread" code
// Note that users either have to adjust the trait in their architecture file.
// Alternatively we would have to attach this method to the impl, not the trait.
tab(n, *fOut);
*fOut << "pub fn compute_thread(" << fKlassName << "&mut self, num_thread: i32) {";
tab(n + 1, *fOut);
fCodeProducer.Tab(n + 1);
// Generate it
fThreadLoopBlock->accept(&fCodeProducer);
tab(n, *fOut);
*fOut << "}" << endl;
// Compute "compute" declaration
tab(n, *fOut);
*fOut << "fn compute("
<< subst("&mut self, $0: i32, inputs: &[&[Self::T]], outputs: &mut[&mut[Self::T]]) {", fFullCount);
tab(n + 1, *fOut);
fCodeProducer.Tab(n + 1);
// Generates local variables declaration and setup
generateComputeBlock(&fCodeProducer);
tab(n, *fOut);
*fOut << "}" << endl;
tab(n, *fOut);
*fOut << "extern \"C\" void computeThreadExternal(&mut self, num_thread: i32) {";
tab(n + 1, *fOut);
*fOut << "compute_thread((" << fKlassName << "*)dsp, num_thread);";
tab(n, *fOut);
*fOut << "}" << endl;
}
|