00001 #include "CParsedFile.h"
00002 #include "xi-Chare.h"
00003 #include <algorithm>
00004
00005 using std::for_each;
00006 using std::list;
00007
00008 namespace xi {
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 XStr* CParsedFile::className = NULL;
00025
00026 template <typename T>
00027 struct SdagConCall {
00028 void (SdagConstruct::*fn)(T);
00029 T arg;
00030
00031 SdagConCall(void (SdagConstruct::*fn_)(T), const T& arg_) : fn(fn_), arg(arg_) {}
00032 void operator()(Entry* e) {
00033 if (e->sdagCon) {
00034 (e->sdagCon->*fn)(arg);
00035 }
00036 }
00037 };
00038
00039 template <>
00040 struct SdagConCall<void> {
00041 void (SdagConstruct::*fn)();
00042 SdagConCall(void (SdagConstruct::*fn_)()) : fn(fn_) {}
00043 void operator()(Entry* e) {
00044 if (e->sdagCon) {
00045 (e->sdagCon->*fn)();
00046 }
00047 }
00048 };
00049
00050 void CParsedFile::doProcess(XStr& classname, XStr& decls, XStr& defs) {
00051 className = &classname;
00052 decls << "#define " << classname << "_SDAG_CODE \n";
00053
00054 for_each(nodeList.begin(), nodeList.end(),
00055 SdagConCall<void>(&SdagConstruct::numberNodes));
00056 for_each(nodeList.begin(), nodeList.end(),
00057 SdagConCall<void>(&SdagConstruct::labelNodes));
00058 for_each(nodeList.begin(), nodeList.end(),
00059 SdagConCall<int>(&SdagConstruct::propagateState, 0));
00060 for_each(nodeList.begin(), nodeList.end(),
00061 SdagConCall<void>(&SdagConstruct::generateTrace));
00062 generateEntryList();
00063 mapCEntry();
00064
00065 generateCode(decls, defs);
00066 generateEntries(decls, defs);
00067 generateInitFunction(decls, defs);
00068 generatePupFunction(decls, defs);
00069 generateRegisterEp(decls, defs);
00070 generateTraceEp(decls, defs);
00071
00072 #ifdef USE_CRITICAL_PATH_HEADER_ARRAY
00073 generateDependencyMergePoints(decls);
00074 #endif
00075
00076 decls.line_append_padding('\\');
00077 decls << "\n";
00078 }
00079
00080 void CParsedFile::mapCEntry(void) {
00081 for (list<CEntry*>::iterator en = entryList.begin(); en != entryList.end(); ++en) {
00082 container->lookforCEntry(*en);
00083 }
00084 }
00085
00086 void CParsedFile::generateEntryList(void) {
00087 for (std::list<Entry*>::iterator cn = nodeList.begin(); cn != nodeList.end(); ++cn) {
00088 (*cn)->sdagCon->generateEntryList(entryList, NULL);
00089 }
00090 }
00091
00092 void CParsedFile::generateCode(XStr& decls, XStr& defs) {
00093 for (std::list<Entry*>::iterator cn = nodeList.begin(); cn != nodeList.end(); ++cn) {
00094 (*cn)->sdagCon->setNext(0, 0);
00095 (*cn)->sdagCon->generateCode(decls, defs, *cn);
00096 }
00097 }
00098
00099 void CParsedFile::generateEntries(XStr& decls, XStr& defs) {
00100 decls << "public:\n";
00101 for (list<CEntry*>::iterator en = entryList.begin(); en != entryList.end(); ++en) {
00102 (*en)->generateCode(decls, defs);
00103 }
00104 }
00105
00106 void CParsedFile::generateInitFunction(XStr& decls, XStr& defs) {
00107 decls << "public:\n";
00108 decls << " SDAG::dep_ptr __dep;\n";
00109
00110 XStr name = "_sdag_init";
00111 generateVarSignature(decls, defs, container, false, "void", &name, false, NULL);
00112 defs << " __dep.reset(new SDAG::Dependency(" << numEntries << "," << numWhens
00113 << "));\n";
00114
00115 for (list<CEntry*>::iterator en = entryList.begin(); en != entryList.end(); ++en)
00116 (*en)->generateDeps(defs);
00117 endMethod(defs);
00118
00119
00120 XStr oldname = "__sdag_init";
00121 generateVarSignature(decls, defs, container, false, "void", &oldname, false, NULL);
00122 endMethod(defs);
00123 }
00124
00131 void CParsedFile::generateDependencyMergePoints(XStr& decls) {
00132 decls << " \n";
00133
00134
00135
00136 for (int i = 0; i < numWhens; i++) {
00137 decls << " MergeablePathHistory _when_" << i
00138 << "_PathMergePoint; /* For Critical Path Detection */ \n";
00139 }
00140
00141
00142
00143 for (int i = 0; i < numOlists; i++) {
00144 decls << " MergeablePathHistory olist__co" << i
00145 << "_PathMergePoint; /* For Critical Path Detection */ \n";
00146 }
00147 }
00148
00149 void CParsedFile::generatePupFunction(XStr& decls, XStr& defs) {
00150 decls << "public:\n";
00151 XStr signature = "_sdag_pup(PUP::er &p)";
00152 decls << " void " << signature << ";\n";
00153
00154 decls << " void _" << signature << " { }\n";
00155
00156 templateGuardBegin(false, defs);
00157 defs << container->tspec() << "void " << container->baseName() << "::" << signature
00158 << " {"
00159 << " // Potentially missing " << container->baseName()
00160 << "_SDAG_CODE in your class definition?\n"
00161 << " p|__dep;\n"
00162 << "}\n";
00163 templateGuardEnd(defs);
00164 }
00165
00166 void CParsedFile::generateRegisterEp(XStr& decls, XStr& defs) {
00167 XStr name = "__sdag_register";
00168 generateVarSignature(decls, defs, container, true, "void", &name, false, NULL);
00169
00170 for (std::list<Entry*>::iterator cn = nodeList.begin(); cn != nodeList.end(); ++cn) {
00171 if ((*cn)->sdagCon != 0) {
00172 (*cn)->sdagCon->generateRegisterEp(defs);
00173 }
00174 }
00175
00176 defs << container->sdagPUPReg;
00177
00178 endMethod(defs);
00179 }
00180
00181 void CParsedFile::generateTraceEp(XStr& decls, XStr& defs) {
00182 for (std::list<Entry*>::iterator cn = nodeList.begin(); cn != nodeList.end(); ++cn) {
00183 if ((*cn)->sdagCon != 0) {
00184 (*cn)->sdagCon->generateTraceEp(decls, defs, container);
00185 }
00186 }
00187 }
00188
00189 }