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 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
|
/************************************************************************
************************************************************************
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.
************************************************************************
************************************************************************/
#include <fstream>
#include <string>
#include "code_container.hh"
#include "fir_to_fir.hh"
#include "floats.hh"
#include "global.hh"
#include "instructions_complexity.hh"
#include "recursivness.hh"
#include "struct_manager.hh"
#include "text_instructions.hh"
#include "timing.hh"
#include "type_manager.hh"
using namespace std;
void CodeContainer::initialize(int numInputs, int numOutputs)
{
fNumInputs = numInputs;
fNumOutputs = numOutputs;
}
CodeContainer::CodeContainer()
: fParentContainer(nullptr),
fNumInputs(-1),
fNumOutputs(-1),
fNumActives(0),
fNumPassives(0),
fSubContainerType(kInt),
fGeneratedSR(false),
fExtGlobalDeclarationInstructions(IB::genBlockInst()),
fGlobalDeclarationInstructions(IB::genBlockInst()),
fDeclarationInstructions(IB::genBlockInst()),
fControlDeclarationInstructions(IB::genBlockInst()),
fInitInstructions(IB::genBlockInst()),
fConstantFromMem(nullptr),
fConstantToMem(nullptr),
fResetUserInterfaceInstructions(IB::genBlockInst()),
fClearInstructions(IB::genBlockInst()),
fPostInitInstructions(IB::genBlockInst()),
fAllocateInstructions(IB::genBlockInst()),
fDestroyInstructions(IB::genBlockInst()),
fStaticInitInstructions(IB::genBlockInst()),
fPostStaticInitInstructions(IB::genBlockInst()),
fStaticDestroyInstructions(IB::genBlockInst()),
fComputeBlockInstructions(IB::genBlockInst()),
fPostComputeBlockInstructions(IB::genBlockInst()),
fComputeFunctions(IB::genBlockInst()),
fUserInterfaceInstructions(IB::genBlockInst())
{
fCurLoop = new CodeLoop(0, gGlobal->getFreshID("i"));
Address::AccessType access;
if (gGlobal->gMemoryManager == 2) {
access = Address::kStruct;
}
// Special version for SYFALA where iControl/fControl and iZone/fZone have to be parameters
if (gGlobal->gMemoryManager == 3) {
access = Address::kFunArgs;
}
// iControl/fControl are created when -ec and -mem1/2/3 are used together
if (gGlobal->gExtControl && gGlobal->gMemoryManager >= 1) {
fIntControl = new ControlArray("iControl", access);
fRealControl = new ControlArray("fControl", access);
} else {
fIntControl = nullptr;
fRealControl = nullptr;
}
// Memory handling with gMemoryManager
if (gGlobal->gMemoryManager >= 1 && !gGlobal->gIntZone && !gGlobal->gRealZone) {
// Allocation done once to be shared by all containers
ZoneArray::gInternalMemorySize = gGlobal->gFPGAMemory;
gGlobal->gIntZone = new ZoneArray("iZone", access, Typed::kInt32, 4);
gGlobal->gRealZone = new ZoneArray("fZone", access, itfloat(), 4);
}
}
int ZoneArray::gInternalMemorySize = 0;
CodeContainer::~CodeContainer()
{
}
void CodeContainer::transformDAG(DispatchVisitor* visitor)
{
lclgraph G;
CodeLoop::sortGraph(fCurLoop, G);
for (int l = int(G.size() - 1); l >= 0; l--) {
for (const auto& p : G[l]) {
p->transform(visitor);
}
}
}
/**
* Store the loop used to compute a signal
*/
void CodeContainer::setLoopProperty(Tree sig, CodeLoop* l)
{
fLoopProperty.set(sig, l);
}
/**
* Returns the loop used to compute a signal
*/
bool CodeContainer::getLoopProperty(Tree sig, CodeLoop*& l)
{
faustassert(sig);
return fLoopProperty.get(sig, l);
}
void CodeContainer::listAllLoopProperties(Tree sig, set<CodeLoop*>& L, set<Tree>& visited)
{
if (visited.count(sig) == 0) {
visited.insert(sig);
CodeLoop* l;
if (getLoopProperty(sig, l)) {
L.insert(l);
} else {
// we go down the expression
tvec subsigs;
int n = getSubSignals(sig, subsigs, false);
for (int i = 0; i < n; i++) {
listAllLoopProperties(subsigs[i], L, visited);
}
}
}
}
/**
* Open a non-recursive loop on top of the stack of open loops.
* @param size the number of iterations of the loop
*/
void CodeContainer::openLoop(const string& index_name, int size)
{
fCurLoop = new CodeLoop(fCurLoop, index_name, size);
}
/**
* Open a recursive loop on top of the stack of open loops.
* @param recsymbol the recursive symbol defined in this loop
* @param size the number of iterations of the loop
*/
void CodeContainer::openLoop(Tree recsymbol, const string& index_name, int size)
{
fCurLoop = new CodeLoop(recsymbol, fCurLoop, index_name, size);
}
/**
* Close the top loop and either keep it
* or absorb it within its enclosing loop.
*/
void CodeContainer::closeLoop(Tree sig)
{
faustassert(fCurLoop);
// fix the missing dependencies
set<CodeLoop*> L;
set<Tree> V;
listAllLoopProperties(sig, L, V);
for (CodeLoop* l : L) {
fCurLoop->fBackwardLoopDependencies.insert(l);
}
CodeLoop* l = fCurLoop;
fCurLoop = l->fEnclosingLoop;
faustassert(fCurLoop);
Tree S = symlist(sig);
if (l->isEmpty() || fCurLoop->hasRecDependencyIn(S)) {
fCurLoop->absorb(l);
} else {
// cout << " will NOT absorb" << endl;
// we have an independent loop
setLoopProperty(sig, l); // associate the signal
fCurLoop->fBackwardLoopDependencies.insert(l);
// we need to indicate that all recursive symbols defined
// in this loop are defined in this loop
for (Tree lsym = l->fRecSymbolSet; !isNil(lsym); lsym = tl(lsym)) {
this->setLoopProperty(hd(lsym), l);
// cerr << "loop " << l << " defines " << *hd(lsym) << endl;
}
}
}
/**
* Print the required C++ libraries as comments in source code
*/
void CodeContainer::printLibrary(ostream& fout)
{
set<string> S;
set<string>::iterator f;
string sep;
collectLibrary(S);
if (S.size() > 0) {
fout << "/* link with ";
for (f = S.begin(), sep = ": "; f != S.end(); f++, sep = ", ") {
fout << sep << *f;
}
fout << " */\n";
}
}
/**
* Print the required include files
*/
void CodeContainer::printIncludeFile(ostream& fout)
{
set<string> S;
collectIncludeFile(S);
for (const auto& inc : S) {
// Only print non-empty include (inc is actually quoted)
if (inc.size() > 2) {
fout << "#include " << inc << "\n";
}
}
}
/**
* Print the loop graph in dot format
*/
void CodeContainer::printGraphDotFormat(ostream& fout)
{
lclgraph G;
CodeLoop::sortGraph(fCurLoop, G);
fout << "strict digraph loopgraph {" << endl;
fout << '\t' << "rankdir=LR;" << endl;
fout << '\t' << "node[color=blue, fillcolor=lightblue, style=filled, fontsize=9];" << endl;
int lnum = 0; // used for loop numbers
// for each level of the graph
for (int l = int(G.size() - 1); l >= 0; l--) {
// for each task in the level
for (const auto& t : G[l]) {
// print task label "Lxxx : 0xffffff"
fout << '\t' << 'L' << t << "[label=<<font face=\"verdana,bold\">L" << lnum++
<< "</font> : " << t << ">];" << endl;
// for each source of the task
for (const auto& src : t->fBackwardLoopDependencies) {
// print the connection Lxxx -> Lyyy;
fout << '\t' << 'L' << src << "->" << 'L' << t << ';' << endl;
}
}
}
fout << "}" << endl;
}
/**
* Adds forward dependencies in the DAG and returns loop count
*/
void CodeContainer::computeForwardDAG(lclgraph dag, int& loop_count, vector<int>& ready_loop)
{
#define START_TASK_MAX 2
int loop_index = START_TASK_MAX; // First index to be used for remaining tasks
for (int l = int(dag.size() - 1); l >= 0; l--) {
for (const auto& p : dag[l]) {
// Setup forward dependancy
for (const auto& p1 : p->fBackwardLoopDependencies) {
p1->fForwardLoopDependencies.insert(p);
}
// Setup loop index
p->fIndex = loop_index;
loop_index++;
// Keep ready loops
if (p->getBackwardLoopDependencies().size() == 0) {
ready_loop.push_back(p->getIndex());
}
}
}
loop_count = loop_index;
}
ValueInst* CodeContainer::pushFunction(const string& name, Typed::VarType result,
vector<Typed::VarType>& types, const Values& args)
{
Names named_args;
for (size_t i = 0; i < types.size(); i++) {
named_args.push_back(
IB::genNamedTyped("dummy" + to_string(i), IB::genBasicTyped(types[i])));
}
pushGlobalDeclare(
IB::genDeclareFunInst(name, IB::genFunTyped(named_args, IB::genBasicTyped(result))));
return IB::genFunCallInst(name, args);
}
void CodeContainer::sortDeepFirstDAG(CodeLoop* l, set<CodeLoop*>& visited, list<CodeLoop*>& result)
{
// Avoid printing already printed loops
if (isElement(visited, l)) {
return;
}
// Remember we have printed this loop
visited.insert(l);
// Compute the dependencies loops (that need to be computed before this one)
for (const auto& p : l->fBackwardLoopDependencies) {
sortDeepFirstDAG(p, visited, result);
}
// Keep the non-empty loops in result
if (!l->isEmpty()) {
result.push_back(l);
}
}
// Functions are coded with a "class" prefix, so to stay separated in "gGlobalTable"
void CodeContainer::produceInfoFunctions(int tabs, const string& classname, const string& obj,
bool ismethod, FunTyped::FunAttribute funtype,
TextInstVisitor* producer, const string& in_fun,
const string& out_fun)
{
// Input/Output method
producer->Tab(tabs);
generateGetInputs(in_fun + classname, obj, ismethod, funtype)->accept(producer);
generateGetOutputs(out_fun + classname, obj, ismethod, funtype)->accept(producer);
}
void CodeContainer::generateDAGLoopInternal(CodeLoop* loop, BlockInst* block, ValueInst* count,
bool omp)
{
loop->generateDAGScalarLoop(block, count, omp);
}
void CodeContainer::generateDAGLoopAux(CodeLoop* loop, BlockInst* loop_code, ValueInst* count,
int loop_num, bool omp)
{
if (gGlobal->gFunTaskSwitch) {
BlockInst* block = IB::genBlockInst();
// Generates scalar or vectorized loop
generateDAGLoopInternal(loop, block, count, omp);
Loop2FunctionBuider builder(subst("fun$0" + getClassName(), T(loop_num)), block,
gGlobal->gDSPStruct);
pushOtherComputeMethod(builder.fFunctionDef);
loop_code->pushBackInst(IB::genLabelInst(
(loop->fIsRecursive) ? subst("/* Recursive function $0 */", T(loop_num))
: subst("/* Vectorizable function $0 */", T(loop_num))));
loop_code->pushBackInst(builder.fFunctionCall);
} else {
loop_code->pushBackInst(IB::genLabelInst(
(loop->fIsRecursive) ? subst("/* Recursive loop $0 */", T(loop_num))
: subst("/* Vectorizable loop $0 */", T(loop_num))));
// Generates scalar or vectorized loop
generateDAGLoopInternal(loop, loop_code, count, omp);
}
}
void CodeContainer::generateDAGLoop(BlockInst* block, ValueInst* count)
{
int loop_num = 0;
if (gGlobal->gDeepFirstSwitch) {
set<CodeLoop*> visited;
list<CodeLoop*> result;
sortDeepFirstDAG(fCurLoop, visited, result);
for (const auto& p : result) {
generateDAGLoopAux(p, block, count, loop_num++);
}
} else {
lclgraph G;
CodeLoop::sortGraph(fCurLoop, G);
for (int l = int(G.size() - 1); l >= 0; l--) {
for (const auto& p : G[l]) {
generateDAGLoopAux(p, block, count, loop_num++);
}
}
}
}
void CodeContainer::processFIR(void)
{
// Types used in 'compute' prototype
gGlobal->setVarType("count", Typed::kInt32);
gGlobal->setVarType("inputs", Typed::kFloatMacro_ptr_ptr);
gGlobal->setVarType("outputs", Typed::kFloatMacro_ptr_ptr);
// Type used in several methods using 'sample_rate' parameter
gGlobal->setVarType("sample_rate", Typed::kInt32);
// ==========================================================================================
// FIR to FIR passes that are generic for all backends, depending of the compilation options
// ==========================================================================================
if (gGlobal->gInlineTable) {
// Rename 'sig' in 'dsp', remove 'dsp' allocation, inline subcontainers 'instanceInit' and
// 'fill' function call
fStaticInitInstructions = inlineSubcontainersFunCalls(fStaticInitInstructions);
// Rename 'sig' in 'dsp', remove 'dsp' allocation, inline subcontainers 'instanceInit' and
// 'fill' function call
fInitInstructions = inlineSubcontainersFunCalls(fInitInstructions);
}
// Additional iControl/fControl fields
if (gGlobal->gExtControl && gGlobal->gMemoryManager >= 1) {
if (fIntControl->getSize() > 0 && fIntControl->fAccess == Address::kStruct) {
pushDeclare(IB::genDecStructVar(
"iControl", IB::genArrayTyped(Typed::kInt32, fIntControl->getSize())));
}
if (fRealControl->getSize() > 0 && fRealControl->fAccess == Address::kStruct) {
pushDeclare(IB::genDecStructVar("fControl",
IB::genArrayTyped(itfloat(), fRealControl->getSize())));
}
}
// Prepare fConstantFromMem/fConstantToMem blocks
if (gGlobal->gMemoryManager >= 2) {
// Rename 'sig' in 'dsp', remove 'dsp' allocation, inline subcontainers 'instanceInit' and
// 'fill' function call
BlockInst* declare_block = inlineSubcontainersFunCalls(fInitInstructions);
gGlobal->gIntZone->declareConstant(declare_block);
gGlobal->gRealZone->declareConstant(declare_block);
fConstantFromMem = gGlobal->gIntZone->getLoadConstantCode(declare_block);
fConstantFromMem = gGlobal->gRealZone->getLoadConstantCode(fConstantFromMem);
fConstantToMem = gGlobal->gIntZone->getStoreConstantCode(declare_block);
fConstantToMem = gGlobal->gRealZone->getStoreConstantCode(fConstantToMem);
}
// Additional iZone/fZone fields
if (gGlobal->gMemoryManager >= 1) {
if (gGlobal->gIntZone->getSize() > 0 && gGlobal->gIntZone->fAccess == Address::kStruct) {
pushDeclare(IB::genDecStructVar(
"iZone", IB::genArrayTyped(Typed::kInt32, gGlobal->gIntZone->getSize())));
}
if (gGlobal->gRealZone->getSize() > 0 && gGlobal->gRealZone->fAccess == Address::kStruct) {
pushDeclare(IB::genDecStructVar(
"fZone", IB::genArrayTyped(itfloat(), gGlobal->gRealZone->getSize())));
}
}
// Possibly rewrite arrays access using iZone/fZone
rewriteInZones();
// Possibly add "fSamplingRate" field
generateSR();
// Possibly groups tasks (used by VectorCodeContainer, OpenMPCodeContainer and WSSCodeContainer)
if (gGlobal->gGroupTaskSwitch) {
CodeLoop::computeUseCount(fCurLoop);
set<CodeLoop*> visited;
CodeLoop::groupSeqLoops(fCurLoop, visited);
}
if (gGlobal->gMemoryManager >= 0) {
createMemoryLayout();
}
// Sort struct fields by size and type
// 05/16/17 : deactivated since it slows down the code...
/*
fDeclarationInstructions->fCode.sort(sortArrayDeclarations);
fDeclarationInstructions->fCode.sort(sortTypeDeclarations);
*/
// Soundfile fields moved first
fDeclarationInstructions->fCode.sort(sortSoundfiles);
// Check FIR code
if (global::isDebug("FIR_CHECKER")) {
startTiming("FIR checker");
FIRChecker fir_checker;
flattenFIR()->accept(&fir_checker);
endTiming("FIR checker");
}
if (global::isDebug("FIR_VAR_CHECKER")) {
startTiming("FIR var checker");
// Check variable scope in 'compute'
FIRVarChecker fir_var_checker(fComputeBlockInstructions,
fCurLoop->generateScalarLoop("count"));
endTiming("FIR var checker");
}
#ifdef FIR_BUILD
if (global::isDebug("FIR_PRINTER")) {
stringstream res;
FIRInstVisitor fir_visitor(&res);
flattenFIR()->accept(&fir_visitor);
std::cout << res.str();
}
#endif
}
// Possibly rewrite arrays access using iZone/fZone
void CodeContainer::rewriteInZones()
{
if (gGlobal->gMemoryManager >= 1) {
if (gGlobal->gIntZone->getSize() > 0) {
// Rewrite DSP struct access in iZone access
fGlobalDeclarationInstructions =
gGlobal->gIntZone->getCode(fGlobalDeclarationInstructions);
fStaticInitInstructions = gGlobal->gIntZone->getCode(fStaticInitInstructions);
fInitInstructions = gGlobal->gIntZone->getCode(fInitInstructions);
fControlDeclarationInstructions =
gGlobal->gIntZone->getCode(fControlDeclarationInstructions);
fComputeBlockInstructions = gGlobal->gIntZone->getCode(fComputeBlockInstructions);
fComputeFunctions = gGlobal->gIntZone->getCode(fComputeFunctions);
fClearInstructions = gGlobal->gIntZone->getCode(fClearInstructions);
}
if (gGlobal->gRealZone->getSize() > 0) {
// Rewrite DSP struct access in fZone access
fGlobalDeclarationInstructions =
gGlobal->gRealZone->getCode(fGlobalDeclarationInstructions);
fStaticInitInstructions = gGlobal->gRealZone->getCode(fStaticInitInstructions);
fInitInstructions = gGlobal->gRealZone->getCode(fInitInstructions);
fControlDeclarationInstructions =
gGlobal->gRealZone->getCode(fControlDeclarationInstructions);
fComputeBlockInstructions = gGlobal->gRealZone->getCode(fComputeBlockInstructions);
fComputeFunctions = gGlobal->gRealZone->getCode(fComputeFunctions);
fClearInstructions = gGlobal->gRealZone->getCode(fClearInstructions);
}
}
}
void CodeContainer::mergeSubContainers()
{
BlockInst* sub_ui = new BlockInst();
for (const auto& it : fSubContainers) {
// Merge the subcontainer in the main one
fExtGlobalDeclarationInstructions->merge(it->fExtGlobalDeclarationInstructions);
fGlobalDeclarationInstructions->merge(it->fGlobalDeclarationInstructions);
fDeclarationInstructions->merge(it->fDeclarationInstructions);
fControlDeclarationInstructions->merge(it->fControlDeclarationInstructions);
sub_ui->merge(it->fUserInterfaceInstructions);
// TO CHECK (used for waveform initialisation which has to be moved first...)
fStaticInitInstructions->mergeFront(it->fStaticInitInstructions);
// Then clear it
it->fGlobalDeclarationInstructions->fCode.clear();
it->fExtGlobalDeclarationInstructions->fCode.clear();
it->fDeclarationInstructions->fCode.clear();
it->fControlDeclarationInstructions->fCode.clear();
it->fUserInterfaceInstructions->fCode.clear();
it->fStaticInitInstructions->fCode.clear();
}
/*
Insert subcontainer UIs at the end of the top group, just before the last closeBox
and remove empty groups.
*/
fUserInterfaceInstructions->insert(fUserInterfaceInstructions->size() - 1,
removeEmptyGroups(sub_ui));
// Possibly rewrite access in iZone/fZone
rewriteInZones();
}
/*
Create memory layout, to be used in C++ backend and JSON generation.
The description order follows what will be done at allocation time.
Subcontainers are ignored when gGlobal->gInlineTable == true.
// Create static tables
mydsp::classInit();
// Create DSP
dsp* DSP = mydsp::create();
// Init DSP
DSP->instanceInit(44100);
*/
void CodeContainer::createMemoryLayout()
{
// Subcontainers used in classInit
if (!gGlobal->gInlineTable) {
// Compute DSP struct arrays size
StructInstVisitor struct_visitor;
// Add the global static tables
fGlobalDeclarationInstructions->accept(&struct_visitor);
// Compute R/W access for each subcontainer
ForLoopInst* loop = fCurLoop->generateScalarLoop("count");
loop->accept(&struct_visitor);
for (const auto& it : fSubContainers) {
// Check that the subcontainer name appears as a type name in fStaticInitInstructions
SearchSubcontainer search_class(it->getClassName());
fStaticInitInstructions->accept(&search_class);
if (search_class.fFound) {
// Subcontainer size
VariableSizeCounter struct_size(Address::kStruct);
it->generateDeclarations(&struct_size);
fMemoryLayout.push_back(MemoryLayoutItem{it->getClassName(), "kObj_ptr", 0,
struct_size.fSizeBytes, 0, 0});
// Get the associated table size and access
pair<string, int> field = gGlobal->gTablesSize[it->getClassName()];
// Check the table name memory description
MemoryDesc& decs = struct_visitor.getMemoryDesc(field.first);
fMemoryLayout.push_back(MemoryLayoutItem{field.first,
Typed::gTypeString[decs.fType], 0,
field.second, decs.fRAccessCount, 0});
}
}
}
{
// Compute DSP struct arrays size and R/W access
StructInstVisitor struct_visitor;
// Add the DSP fields
fDeclarationInstructions->accept(&struct_visitor);
// To generate R/W access in the DSP loop
ForLoopInst* loop = fCurLoop->generateScalarLoop("count");
loop->accept(&struct_visitor);
// DSP object
int read_access = 0;
int write_access = 0;
for (const auto& it : struct_visitor.getFieldTable()) {
// Scalar types are kept in the DSP
if (it.second.fIsScalar) {
read_access += it.second.fRAccessCount;
write_access += it.second.fWAccessCount;
}
}
{
// Array fields are transformed in pointers
ArrayToPointer array_pointer;
VariableSizeCounter struct_size(Address::kStruct);
array_pointer.getCode(fDeclarationInstructions)->accept(&struct_size);
// TODO: rework DSP site comptations with local arrays
fMemoryLayout.push_back(
MemoryLayoutItem{fKlassName, "kObj_ptr", 0,
// Raised value:
// - add virtual method pointer (8 bytes in 64 bits)
// - add 8 bytes for memory alignment
struct_size.fSizeBytes + 8 + 8, read_access, write_access});
}
// Arrays and scalars inside the DSP struct
for (const auto& it : struct_visitor.getFieldTable()) {
if (!it.second.fIsControl) {
/*
std::cout << "it.first " << it.first << std::endl;
std::cout << "Typed::gTypeString[it.second.fType] " <<
Typed::gTypeString[it.second.fType] << std::endl; std::cout << "it.second.fSize " <<
it.second.fSize << std::endl; std::cout << "it.second.fSizeBytes " <<
it.second.fSizeBytes << std::endl;
*/
fMemoryLayout.push_back(MemoryLayoutItem{
it.first,
Typed::gTypeString[(it.second.fIsScalar)
? it.second.fType
: Typed::getPtrFromType(it.second.fType)],
it.second.fSize, it.second.fSizeBytes, it.second.fRAccessCount,
it.second.fWAccessCount});
}
}
// Subcontainers used in instanceConstants
if (!gGlobal->gInlineTable) {
for (const auto& it : fSubContainers) {
// Check that the subcontainer name appears as a type name in fInitInstructions
SearchSubcontainer search_class(it->getClassName());
fInitInstructions->accept(&search_class);
if (search_class.fFound) {
VariableSizeCounter struct_size(Address::kStruct);
it->generateDeclarations(&struct_size);
fMemoryLayout.push_back(MemoryLayoutItem{it->getClassName(), "kObj_ptr", 0,
struct_size.fSizeBytes, 0, 0});
}
}
}
}
}
BlockInst* CodeContainer::flattenFIR(void)
{
BlockInst* global_block = IB::genBlockInst();
// Declaration part
global_block->pushBackInst(IB::genLabelInst("========== Declaration part =========="));
global_block->merge(fExtGlobalDeclarationInstructions);
global_block->merge(fGlobalDeclarationInstructions);
global_block->merge(fDeclarationInstructions);
// Init method
global_block->pushBackInst(IB::genLabelInst("========== Init method =========="));
global_block->merge(fInitInstructions);
global_block->merge(fResetUserInterfaceInstructions);
global_block->merge(fClearInstructions);
global_block->merge(fPostInitInstructions);
// Static init method
global_block->pushBackInst(IB::genLabelInst("========== Static init method =========="));
global_block->merge(fStaticInitInstructions);
global_block->merge(fPostStaticInitInstructions);
// Subcontainers
global_block->pushBackInst(IB::genLabelInst("========== Subcontainers =========="));
for (const auto& it : fSubContainers) {
global_block->merge(it->flattenFIR());
}
// Control method
global_block->pushBackInst(IB::genLabelInst("========== Control =========="));
global_block->merge(fControlDeclarationInstructions);
// Compute method
global_block->pushBackInst(IB::genLabelInst("========== Compute control =========="));
global_block->merge(fComputeBlockInstructions);
global_block->pushBackInst(IB::genLabelInst("========== Compute DSP =========="));
global_block->pushBackInst(fCurLoop->generateScalarLoop(fFullCount));
global_block->pushBackInst(IB::genLabelInst("========== Post compute DSP =========="));
global_block->merge(fPostComputeBlockInstructions);
return global_block;
}
BlockInst* CodeContainer::inlineSubcontainersFunCalls(BlockInst* block)
{
// Rename 'sig' in 'dsp' and remove 'dsp' allocation
block = DspRenamer().getCode(block);
// Inline subcontainers 'instanceInit' and 'fill' function call
for (const auto& it : fSubContainers) {
// Build the function to be inlined (prototype and code)
DeclareFunInst* inst_init_fun =
it->generateInstanceInitFun("instanceInit" + it->getClassName(), "dsp", true, false);
block = FunctionCallInliner(inst_init_fun).getCode(block);
// Build the function to be inlined (prototype and code)
DeclareFunInst* fill_fun =
it->generateFillFun("fill" + it->getClassName(), "dsp", true, false);
block = FunctionCallInliner(fill_fun).getCode(block);
}
// Rename all loop variables name to avoid name clash
LoopVariableRenamer loop_renamer;
block = loop_renamer.getCode(block);
return block;
}
void CodeContainer::printMacros(ostream& fout, int n)
{
// generate user interface macros if needed
if (gGlobal->gUIMacroSwitch) {
if (gGlobal->gOutputLang == "c" || gGlobal->gOutputLang == "cpp") {
tab(n, fout);
fout << "#ifdef FAUST_UIMACROS";
tab(n + 1, fout);
tab(n + 1, fout);
for (const auto& it : gGlobal->gMetaDataSet) {
if (it.first == tree("filename")) {
fout << "#define FAUST_FILE_NAME " << **(it.second.begin());
break;
}
}
tab(n + 1, fout);
fout << "#define FAUST_CLASS_NAME "
<< "\"" << fKlassName << "\"";
tab(n + 1, fout);
fout << "#define FAUST_COMPILATION_OPIONS \"" << gGlobal->printCompilationOptions1()
<< "\"";
tab(n + 1, fout);
fout << "#define FAUST_INPUTS " << fNumInputs;
tab(n + 1, fout);
fout << "#define FAUST_OUTPUTS " << fNumOutputs;
tab(n + 1, fout);
fout << "#define FAUST_ACTIVES " << fNumActives;
tab(n + 1, fout);
fout << "#define FAUST_PASSIVES " << fNumPassives;
tab(n, fout);
printlines(n + 1, fUIMacro, fout);
tab(n, fout);
tab(n, fout);
{
fout << "\t"
<< "#define FAUST_LIST_ACTIVES(p) \\";
printlines(n + 2, fUIMacroActives, fout);
tab(n, fout);
tab(n, fout);
}
{
fout << "\t"
<< "#define FAUST_LIST_PASSIVES(p) \\";
printlines(n + 2, fUIMacroPassives, fout);
tab(n, fout);
tab(n, fout);
}
fout << "#endif" << endl;
} else {
cerr << "ASSERT : incorrect backend : " << gGlobal->gOutputLang << endl;
faustassert(false);
}
}
}
// DSP API generation
DeclareFunInst* CodeContainer::generateGetIO(const string& name, const string& obj, int io,
bool ismethod, FunTyped::FunAttribute funtype)
{
Names args = genMethod(obj, ismethod);
BlockInst* block = IB::genBlockInst();
block->pushBackInst(IB::genRetInst(IB::genInt32NumInst(io)));
// Creates function
FunTyped* fun_type = IB::genFunTyped(args, IB::genInt32Typed(), funtype);
return IB::genDeclareFunInst(name, fun_type, block);
}
DeclareFunInst* CodeContainer::generateGetInputs(const string& name, const string& obj,
bool ismethod, FunTyped::FunAttribute funtype)
{
return generateGetIO(name, obj, fNumInputs, ismethod, funtype);
}
DeclareFunInst* CodeContainer::generateGetOutputs(const string& name, const string& obj,
bool ismethod, FunTyped::FunAttribute funtype)
{
return generateGetIO(name, obj, fNumOutputs, ismethod, funtype);
}
DeclareFunInst* CodeContainer::generateAllocate(const string& name, const string& obj,
bool ismethod, bool isvirtual)
{
Names args = genMethod(obj, ismethod);
BlockInst* block = IB::genBlockInst();
block->pushBackInst(fAllocateInstructions);
// Explicit return
block->pushBackInst(IB::genRetInst());
// Creates function
return IB::genVoidFunction(name, args, block, isvirtual);
}
DeclareFunInst* CodeContainer::generateDestroy(const string& name, const string& obj, bool ismethod,
bool isvirtual)
{
Names args = genMethod(obj, ismethod);
BlockInst* block = IB::genBlockInst();
block->pushBackInst(fDestroyInstructions);
// Explicit return
block->pushBackInst(IB::genRetInst());
// Creates function
return IB::genVoidFunction(name, args, block, isvirtual);
}
DeclareFunInst* CodeContainer::generateGetIORate(const string& name, const string& obj,
vector<int>& io, bool ismethod, bool isvirtual)
{
Names args = genMethod(obj, ismethod);
args.push_back(IB::genNamedTyped("channel", Typed::kInt32));
BlockInst* block = IB::genBlockInst();
ValueInst* switch_cond = IB::genLoadFunArgsVar("channel");
::SwitchInst* switch_block = IB::genSwitchInst(switch_cond);
block->pushBackInst(IB::genDecStackVar("rate", IB::genInt32Typed()));
block->pushBackInst(switch_block);
int i = 0;
for (const auto& it : io) {
// Creates "case" block
BlockInst* case_block = IB::genBlockInst();
// Compiles "case" block
case_block->pushBackInst(IB::genStoreStackVar("rate", IB::genInt32NumInst(it)));
// Add it into the switch
switch_block->addCase(i++, case_block);
}
// Default case
BlockInst* default_case_block = IB::genBlockInst();
default_case_block->pushBackInst(IB::genStoreStackVar("rate", IB::genInt32NumInst(-1)));
switch_block->addCase(-1, default_case_block);
// Return "rate" result
block->pushBackInst(IB::genRetInst(IB::genLoadStackVar("rate")));
// Creates function
FunTyped* fun_type = IB::genFunTyped(args, IB::genInt32Typed(),
(isvirtual) ? FunTyped::kVirtual : FunTyped::kDefault);
return IB::genDeclareFunInst(name, fun_type, block);
}
DeclareFunInst* CodeContainer::generateInstanceClear(const string& name, const string& obj,
bool ismethod, bool isvirtual)
{
Names args = genMethod(obj, ismethod);
BlockInst* block = IB::genBlockInst();
block->pushBackInst(fClearInstructions);
// Explicit return
block->pushBackInst(IB::genRetInst());
// Creates function
return IB::genVoidFunction(name, args, block, isvirtual);
}
DeclareFunInst* CodeContainer::generateInstanceConstants(const string& name, const string& obj,
bool ismethod, bool isvirtual)
{
Names args = genMethod(obj, ismethod);
args.push_back(IB::genNamedTyped("sample_rate", Typed::kInt32));
BlockInst* block = IB::genBlockInst();
block->pushBackInst(fInitInstructions);
// Explicit return
block->pushBackInst(IB::genRetInst());
// Creates function
return IB::genVoidFunction(name, args, block, isvirtual);
}
DeclareFunInst* CodeContainer::generateStaticInitFun(const string& name, bool isstatic)
{
Names args;
args.push_back(IB::genNamedTyped("sample_rate", Typed::kInt32));
BlockInst* block = IB::genBlockInst();
block->pushBackInst(fStaticInitInstructions);
block->pushBackInst(fPostStaticInitInstructions);
// 20/11/16 : added in generateInstanceInitFun, is this needed here ?
/*
init_block->pushBackInst(fResetUserInterfaceInstructions);
init_block->pushBackInst(fClearInstructions);
*/
// Explicit return
block->pushBackInst(IB::genRetInst());
// Creates function
FunTyped* fun_type = IB::genFunTyped(args, IB::genVoidTyped(),
(isstatic) ? FunTyped::kStatic : FunTyped::kDefault);
return IB::genDeclareFunInst(name, fun_type, block);
}
DeclareFunInst* CodeContainer::generateInstanceInitFun(const string& name, const string& obj,
bool ismethod, bool isvirtual)
{
Names args = genMethod(obj, ismethod);
args.push_back(IB::genNamedTyped("sample_rate", Typed::kInt32));
BlockInst* init_block = IB::genBlockInst();
init_block->pushBackInst(fInitInstructions);
init_block->pushBackInst(fPostInitInstructions);
init_block->pushBackInst(fResetUserInterfaceInstructions);
init_block->pushBackInst(fClearInstructions);
// Explicit return
init_block->pushBackInst(IB::genRetInst());
// Creates function
return IB::genVoidFunction(name, args, init_block, isvirtual);
}
DeclareFunInst* CodeContainer::generateFillFun(const string& name, const string& obj, bool ismethod,
bool isvirtual)
{
Names args = genMethod(obj, ismethod);
args.push_back(IB::genNamedTyped("count", Typed::kInt32));
if (fSubContainerType == kInt) {
args.push_back(IB::genNamedTyped(fTableName, Typed::kInt32_ptr));
} else {
args.push_back(IB::genNamedTyped(fTableName, itfloatptr()));
}
BlockInst* block = IB::genBlockInst();
block->pushBackInst(fComputeBlockInstructions);
// Hack for Julia
if (gGlobal->gOutputLang == "julia" || gGlobal->gOutputLang == "jax") {
block->pushBackInst(fCurLoop->generateSimpleScalarLoop("count"));
} else {
block->pushBackInst(fCurLoop->generateScalarLoop("count"));
}
// Explicit return
block->pushBackInst(IB::genRetInst());
// Creates function
return IB::genVoidFunction(name, args, block, isvirtual);
}
DeclareFunInst* CodeContainer::generateInit(const string& name, const string& obj, bool ismethod,
bool isvirtual)
{
Names args = genMethod(obj, ismethod);
args.push_back(IB::genNamedTyped("sample_rate", Typed::kInt32));
BlockInst* block = IB::genBlockInst();
{
Values args1 = genObjArg(obj, ismethod);
args1.push_back(IB::genLoadFunArgsVar("sample_rate"));
block->pushBackInst(IB::genVoidFunCallInst("classInit", args1));
}
{
Values args1 = genObjArg(obj, ismethod);
args1.push_back(IB::genLoadFunArgsVar("sample_rate"));
block->pushBackInst(IB::genVoidFunCallInst("instanceInit", args1));
}
// Creates function
return IB::genVoidFunction(name, args, block, isvirtual);
}
DeclareFunInst* CodeContainer::generateInstanceInit(const string& name, const string& obj,
bool ismethod, bool isvirtual)
{
Names args = genMethod(obj, ismethod);
args.push_back(IB::genNamedTyped("sample_rate", Typed::kInt32));
BlockInst* block = IB::genBlockInst();
{
Values args1 = genObjArg(obj, ismethod);
args1.push_back(IB::genLoadFunArgsVar("sample_rate"));
block->pushBackInst(IB::genVoidFunCallInst("instanceConstants", args1));
}
{
Values args1 = genObjArg(obj, ismethod);
block->pushBackInst(IB::genVoidFunCallInst("instanceResetUserInterface", args1));
}
{
Values args1 = genObjArg(obj, ismethod);
block->pushBackInst(IB::genVoidFunCallInst("instanceClear", args1));
}
// Creates function
return IB::genVoidFunction(name, args, block, isvirtual);
}
DeclareFunInst* CodeContainer::generateGetSampleRate(const string& name, const string& obj,
bool ismethod, bool isvirtual)
{
Names args = genMethod(obj, ismethod);
BlockInst* block = IB::genBlockInst();
block->pushBackInst(IB::genRetInst(IB::genLoadStructVar("fSampleRate")));
// Creates function
FunTyped* fun_type = IB::genFunTyped(args, IB::genInt32Typed(),
(isvirtual) ? FunTyped::kVirtual : FunTyped::kDefault);
return IB::genDeclareFunInst(name, fun_type, block);
}
DeclareFunInst* CodeContainer::generateComputeFun(const string& name, const string& obj,
bool ismethod, bool isvirtual)
{
Names args = genMethod(obj, ismethod);
args.push_back(IB::genNamedTyped("count", Typed::kInt32));
args.push_back(IB::genNamedTyped("inputs", Typed::kFloatMacro_ptr_ptr));
args.push_back(IB::genNamedTyped("outputs", Typed::kFloatMacro_ptr_ptr));
// Generates control + DSP block
BlockInst* block = generateComputeAux();
// Explicit return
block->pushBackInst(IB::genRetInst());
// Creates function
return IB::genVoidFunction(name, args, block, isvirtual);
}
// Memory methods
DeclareFunInst* CodeContainer::generateCalloc()
{
Names args;
args.push_back(IB::genNamedTyped("count", Typed::kInt64));
args.push_back(IB::genNamedTyped("size", Typed::kInt64));
// Creates function
FunTyped* fun_type =
IB::genFunTyped(args, IB::genBasicTyped(Typed::kVoid_ptr), FunTyped::kDefault);
return IB::genDeclareFunInst("calloc", fun_type);
}
DeclareFunInst* CodeContainer::generateFree()
{
Names args;
args.push_back(IB::genNamedTyped("ptr", Typed::kVoid_ptr));
// Creates function
FunTyped* fun_type = IB::genFunTyped(args, IB::genBasicTyped(Typed::kVoid), FunTyped::kDefault);
return IB::genDeclareFunInst("free", fun_type);
}
DeclareFunInst* CodeContainer::generateNewDsp(const string& name, int size)
{
Names args;
BlockInst* block = IB::genBlockInst();
{
Values args1;
args1.push_back(IB::genInt64NumInst(1));
args1.push_back(IB::genInt64NumInst(size));
block->pushBackInst(IB::genRetInst(IB::genCastInst(IB::genFunCallInst("calloc", args1),
IB::genBasicTyped(Typed::kObj_ptr))));
}
// Creates function
FunTyped* fun_type =
IB::genFunTyped(args, IB::genBasicTyped(Typed::kObj_ptr), FunTyped::kLocal);
return IB::genDeclareFunInst(name, fun_type, block);
}
DeclareFunInst* CodeContainer::generateDeleteDsp(const string& name, const string& obj)
{
Names args;
args.push_back(IB::genNamedTyped(obj, Typed::kObj_ptr));
BlockInst* block = IB::genBlockInst();
{
Values args1;
args1.push_back(
IB::genCastInst(IB::genLoadFunArgsVar(obj), IB::genBasicTyped(Typed::kVoid_ptr)));
block->pushBackInst(IB::genDropInst(IB::genFunCallInst("free", args1)));
}
// Explicit return
block->pushBackInst(IB::genRetInst());
// Creates function
FunTyped* fun_type = IB::genFunTyped(args, IB::genBasicTyped(Typed::kVoid), FunTyped::kLocal);
return IB::genDeclareFunInst(name, fun_type, block);
}
void CodeContainer::generateJSONFile()
{
// Generate JSON (which checks for non duplicated path)
if (gGlobal->gPrintJSONSwitch) {
if (gGlobal->gFloatSize == 1) {
generateJSONFile<float>();
} else {
generateJSONFile<double>();
}
} else {
// Checks for non duplicated path
JSONInstVisitor<float> path_checker;
generateUserInterface(&path_checker);
}
}
|