00001 #include "Forall.h"
00002 #include "IntExpr.h"
00003
00004 namespace xi {
00005
00006 ForallConstruct::ForallConstruct(SdagConstruct* tag, IntExprConstruct* begin,
00007 IntExprConstruct* end, IntExprConstruct* step,
00008 SdagConstruct* body)
00009 : BlockConstruct(SFORALL, 0, tag, begin, end, step, body, 0) {
00010 label_str = "forall";
00011 }
00012
00013 void ForallConstruct::generateCode(XStr& decls, XStr& defs, Entry* entry) {
00014 generateClosureSignature(decls, defs, entry, false, "void", label, false, encapState);
00015
00016 unravelClosuresBegin(defs);
00017
00018 defs << " int __first = (" << con2->text << "), __last = (" << con3->text
00019 << "), __stride = (" << con4->text << ");\n";
00020 defs << " if (__first > __last) {\n";
00021 defs << " int __tmp=__first; __first=__last; __last=__tmp;\n";
00022 defs << " __stride = -__stride;\n";
00023 defs << " }\n";
00024 defs << " SDAG::CCounter *" << counter
00025 << " = new SDAG::CCounter(__first, __last, __stride);\n";
00026 defs << " for(int " << con1->text << "=__first;" << con1->text << "<=__last;"
00027 << con1->text << "+=__stride) {\n";
00028 defs << " SDAG::ForallClosure* " << con1->text << "_cl = new SDAG::ForallClosure("
00029 << con1->text << ");\n";
00030 defs << " ";
00031 generateCall(defs, encapStateChild, encapStateChild, constructs->front()->label);
00032
00033 defs << " }\n";
00034
00035 unravelClosuresEnd(defs);
00036
00037 endMethod(defs);
00038
00039 generateClosureSignature(decls, defs, entry, false, "void", label, true,
00040 encapStateChild);
00041 defs << " " << counter << "->decrement(); /* DECREMENT 1 */ \n";
00042 defs << " " << con1->text << "_cl->deref();\n";
00043 defs << " if (" << counter << "->isDone()) {\n";
00044 defs << " " << counter << "->deref();\n";
00045 defs << " ";
00046 generateCall(defs, encapState, encapState, next->label, nextBeginOrEnd ? 0 : "_end");
00047 defs << " }\n";
00048 endMethod(defs);
00049
00050 generateChildrenCode(decls, defs, entry);
00051 }
00052
00053 void ForallConstruct::propagateState(std::list<EncapState*> encap,
00054 std::list<CStateVar*>& plist,
00055 std::list<CStateVar*>& wlist, int uniqueVarNum) {
00056 encapState = encap;
00057
00058 stateVars = new std::list<CStateVar*>();
00059 stateVars->insert(stateVars->end(), plist.begin(), plist.end());
00060 stateVarsChildren = new std::list<CStateVar*>(plist);
00061
00062 CStateVar* sv = new CStateVar(0, "int", 0, con1->text->charstar(), 0, NULL, 0);
00063 stateVarsChildren->push_back(sv);
00064
00065 {
00066 std::list<CStateVar*> lst;
00067 lst.push_back(sv);
00068 EncapState* state = new EncapState(NULL, lst);
00069 state->isForall = true;
00070 state->type = new XStr("SDAG::ForallClosure");
00071 XStr* name = new XStr();
00072 *name << con1->text << "_cl";
00073 state->name = name;
00074 encap.push_back(state);
00075 }
00076
00077 {
00078 char txt[128];
00079 sprintf(txt, "_cf%d", nodeNum);
00080 counter = new XStr(txt);
00081 sv = new CStateVar(0, "SDAG::CCounter *", 0, txt, 0, NULL, 1);
00082 sv->isCounter = true;
00083 stateVarsChildren->push_back(sv);
00084
00085 std::list<CStateVar*> lst;
00086 lst.push_back(sv);
00087 EncapState* state = new EncapState(NULL, lst);
00088 state->type = new XStr("SDAG::CCounter");
00089 state->name = new XStr(txt);
00090 encap.push_back(state);
00091 }
00092
00093 encapStateChild = encap;
00094
00095 propagateStateToChildren(encap, *stateVarsChildren, wlist, uniqueVarNum);
00096 }
00097
00098 void ForallConstruct::numberNodes(void) {
00099 nodeNum = numForalls++;
00100 SdagConstruct::numberNodes();
00101 }
00102
00103 }