| 927 | // program |
| 928 | |
| 929 | void Program::allocateStack(TextWriter & logs, bool permanent, bool everything) { |
| 930 | // string heap |
| 931 | AllocateConstString vstr; |
| 932 | for (auto & pm : library.modules) { |
| 933 | for ( auto & var : pm->globals.each() ) { |
| 934 | if (var->used && var->init) { |
| 935 | var->init->visit(vstr); |
| 936 | } |
| 937 | } |
| 938 | for ( auto & func : pm->functions.each() ) { |
| 939 | if (func->used) { |
| 940 | func->visit(vstr); |
| 941 | } |
| 942 | } |
| 943 | } |
| 944 | globalStringHeapSize = vstr.bytesTotal; |
| 945 | // move some variables to CMRES |
| 946 | VarCMRes vcm(this, everything); |
| 947 | visit(vcm); |
| 948 | // allocate stack for the rest of them |
| 949 | AllocateStack context(this, permanent, everything, logs); |
| 950 | visit(context); |
| 951 | // adjust stack size for all the used variables |
| 952 | for (auto & pm : library.modules) { |
| 953 | for ( auto & var : pm->globals.each() ) { |
| 954 | if ( var->used ) { |
| 955 | globalInitStackSize = das::max(globalInitStackSize, var->initStackSize); |
| 956 | } |
| 957 | } |
| 958 | } |
| 959 | // allocate used variables and functions indices |
| 960 | totalVariables = 0; |
| 961 | totalFunctions = 0; |
| 962 | auto log = options.getBoolOption("log_stack"); |
| 963 | if ( log ) { |
| 964 | logs << "INIT STACK SIZE:\t" << globalInitStackSize << "\n"; |
| 965 | logs << "FUNCTION TABLE:\n"; |
| 966 | } |
| 967 | for (auto & pm : library.modules) { |
| 968 | for ( auto & func : pm->functions.each() ) { |
| 969 | if ( func->used && !func->builtIn && !func->isTemplate ) { |
| 970 | func->index = totalFunctions++; |
| 971 | if ( log ) { |
| 972 | logs << "\t" << func->index << "\t" << func->totalStackSize << "\t" << func->getMangledName(); |
| 973 | if ( func->init ) { |
| 974 | logs << " [init"; |
| 975 | if (func->lateInit ) logs << "(late)"; |
| 976 | logs << "]"; |
| 977 | } |
| 978 | if ( func->shutdown ) { |
| 979 | logs << " [finalize"; |
| 980 | if (func->lateShutdown ) logs << "(late)"; |
| 981 | logs << "]"; |
| 982 | } |
| 983 | logs << " // " << HEX << func->getMangledNameHash() << DEC; |
| 984 | logs << "\n"; |
| 985 | } |
| 986 | } |
no test coverage detected