00001 #include "CaseList.h"
00002 #include "When.h"
00003 #include <list>
00004 
00005 namespace xi {
00006 
00007 CaseListConstruct::CaseListConstruct(WhenConstruct* single_construct)
00008     : SdagConstruct(SCASELIST, single_construct) {
00009   label_str = "caselist";
00010 }
00011 
00012 CaseListConstruct::CaseListConstruct(WhenConstruct* single_construct,
00013                                      CaseListConstruct* tail)
00014     : SdagConstruct(SCASELIST, single_construct, tail) {
00015   label_str = "caselist";
00016 }
00017 
00018 void CaseListConstruct::numberNodes() {
00019   nodeNum = numCaseLists++;
00020   SdagConstruct::numberNodes();
00021 }
00022 
00023 void CaseListConstruct::propagateState(std::list<EncapState*> encap,
00024                                        std::list<CStateVar*>& plist,
00025                                        std::list<CStateVar*>& wlist, int uniqueVarNum) {
00026   CStateVar* sv;
00027   std::list<CStateVar*>* whensEntryMethodStateVars = NULL;
00028 
00029   encapState = encap;
00030 
00031   stateVars = new std::list<CStateVar*>();
00032 
00033   stateVarsChildren = new std::list<CStateVar*>(plist);
00034   stateVars->insert(stateVars->end(), plist.begin(), plist.end());
00035   {
00036     char txt[128];
00037     sprintf(txt, "_cs%d", nodeNum);
00038     counter = new XStr(txt);
00039     sv = new CStateVar(0, "SDAG::CSpeculator *", 0, txt, 0, NULL, 1);
00040     sv->isSpeculator = true;
00041     stateVarsChildren->push_back(sv);
00042 
00043     for (std::list<SdagConstruct*>::iterator iter = constructs->begin();
00044          iter != constructs->end(); ++iter) {
00045       dynamic_cast<WhenConstruct*>(*iter)->speculativeState = sv;
00046     }
00047     std::list<CStateVar*> lst;
00048     lst.push_back(sv);
00049     EncapState* state = new EncapState(NULL, lst);
00050     state->name = new XStr(txt);
00051     state->type = new XStr("SDAG::CSpeculator");
00052     encap.push_back(state);
00053   }
00054 
00055   encapStateChild = encap;
00056 
00057   propagateStateToChildren(encap, *stateVarsChildren, wlist, uniqueVarNum);
00058 }
00059 
00060 void CaseListConstruct::generateCode(XStr& decls, XStr& defs, Entry* entry) {
00061   generateClosureSignature(decls, defs, entry, false, "void", label, false, encapState);
00062   defs << "  SDAG::CSpeculator* " << counter
00063        << " = new SDAG::CSpeculator(__dep->getAndIncrementSpeculationIndex());\n";
00064 
00065   defs << "  SDAG::Continuation* c = 0;\n";
00066   for (std::list<SdagConstruct*>::iterator it = constructs->begin();
00067        it != constructs->end(); ++it) {
00068     defs << "  c = ";
00069     generateCall(defs, encapStateChild, encapStateChild, (*it)->label);
00070     defs << "  if (!c) return;\n";
00071     defs << "  else c->speculationIndex = " << counter << "->speculationIndex;\n";
00072   }
00073   endMethod(defs);
00074 
00075   sprintf(nameStr, "%s%s", CParsedFile::className->charstar(), label->charstar());
00076   strcat(nameStr, "_end");
00077 
00078   generateClosureSignature(decls, defs, entry, false, "void", label, true,
00079                            encapStateChild);
00080 
00081   defs << "  " << counter << "->deref();\n";
00082   defs << "  ";
00083   generateCall(defs, encapState, encapState, next->label, nextBeginOrEnd ? 0 : "_end");
00084   endMethod(defs);
00085 
00086   generateChildrenCode(decls, defs, entry);
00087 }
00088 
00089 }