Add beginning of support for dsm
[IRC.git] / Robust / src / IR / Flat / BuildFlat.java
index a20080a304db1f7b702d9ab8a9abce4d445980ad..be735ffa9bed702a094d7838daab5596e928fdcb 100644 (file)
@@ -35,14 +35,40 @@ public class BuildFlat {
     
     private void flattenTask(TaskDescriptor td) {
        BlockNode bn=state.getMethodBody(td);
-       FlatNode fn=flattenBlockNode(bn).getBegin();
+       NodePair np=flattenBlockNode(bn);
+       FlatNode fn=np.getBegin();
+       if (np.getEnd().kind()!=FKind.FlatReturnNode) {
+           FlatReturnNode rnflat=new FlatReturnNode(null);
+           np.getEnd().addNext(rnflat);
+       }
+
        FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.PRE);
        ffan.addNext(fn);
 
-       FlatMethod fm=new FlatMethod(td, ffan);
+       FlatMethod fm=new FlatMethod(td);
+       fm.addNext(ffan);
+
+       Hashtable visitedset=new Hashtable();
 
        for(int i=0;i<td.numParameters();i++) {
-           fm.addParameterTemp(getTempforVar(td.getParameter(i)));
+           VarDescriptor paramvd=td.getParameter(i);
+           fm.addParameterTemp(getTempforVar(paramvd));
+           TagExpressionList tel=td.getTag(paramvd);
+           //BUG added next line to fix...to test feed in any task program
+           if (tel!=null)
+               for(int j=0;j<tel.numTags();j++) {
+                   TagVarDescriptor tvd=(TagVarDescriptor) td.getParameterTable().getFromSameScope(tel.getName(j));
+                   TempDescriptor tagtmp=getTempforVar(tvd);
+                   if (!visitedset.containsKey(tvd.getName())) {
+                       visitedset.put(tvd.getName(),tvd.getTag());
+                       fm.addTagTemp(tagtmp);
+                   } else {
+                       TagDescriptor tmptd=(TagDescriptor) visitedset.get(tvd.getName());
+                       if (!tmptd.equals(tvd.getTag()))
+                           throw new Error("Two different tag types with same name as parameters to:"+td);
+                   }
+                   tel.setTemp(j, tagtmp);
+               }
        }
 
        /* Flatten Vector of Flag Effects */
@@ -61,10 +87,18 @@ public class BuildFlat {
        for(int i=0;i<flags.size();i++) {
            FlagEffects fes=(FlagEffects)flags.get(i);
            TempDescriptor flagtemp=getTempforVar(fes.getVar());
+           // Process the flags
            for(int j=0;j<fes.numEffects();j++) {
                FlagEffect fe=fes.getEffect(j);
                ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
            }
+           // Process the tags
+           for(int j=0;j<fes.numTagEffects();j++) {
+               TagEffect te=fes.getTagEffect(j);
+               TempDescriptor tagtemp=getTempforVar(te.getTag());
+               
+               ffan.addTagAction(flagtemp, te.getTag().getTag(), tagtemp, te.getStatus());
+           }
        }
     }
 
@@ -85,10 +119,26 @@ public class BuildFlat {
                    MethodDescriptor memdex=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
                    FlatCall fcunlock=new FlatCall(memdex, null, thistd, new TempDescriptor[0]);
                    np.getEnd().addNext(fcunlock);
+                   FlatReturnNode rnflat=new FlatReturnNode(null);
+                   fcunlock.addNext(rnflat);
+               }
+           } else if (state.DSM&&currmd.getModifiers().isAtomic()) {
+               FlatAtomicEnterNode an=new FlatAtomicEnterNode();
+               an.addNext(fn);
+               fn=an;
+               if (np.getEnd().kind()!=FKind.FlatReturnNode) {
+                   FlatAtomicExitNode aen=new FlatAtomicExitNode();
+                   np.getEnd().addNext(aen);
+                   FlatReturnNode rnflat=new FlatReturnNode(null);
+                   aen.addNext(rnflat);
                }
+           } else if (np.getEnd().kind()!=FKind.FlatReturnNode) {
+               FlatReturnNode rnflat=new FlatReturnNode(null);
+               np.getEnd().addNext(rnflat);
            }
 
-           FlatMethod fm=new FlatMethod(currmd, fn);
+           FlatMethod fm=new FlatMethod(currmd);
+           fm.addNext(fn);
            if (!currmd.isStatic())
                fm.addParameterTemp(getTempforParam(currmd.getThis()));
            for(int i=0;i<currmd.numParameters();i++) {
@@ -142,7 +192,7 @@ public class BuildFlat {
     private NodePair flattenCreateObjectNode(CreateObjectNode con,TempDescriptor out_temp) {
        TypeDescriptor td=con.getType();
        if (!td.isArray()) {
-           FlatNew fn=new FlatNew(td, out_temp);
+           FlatNew fn=new FlatNew(td, out_temp, con.isGlobal());
            TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
            FlatNode last=fn;
            if (td.getClassDesc().hasFlags()) {
@@ -150,14 +200,20 @@ public class BuildFlat {
                FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.NEWOBJECT);
                FlagEffects fes=con.getFlagEffects();
                TempDescriptor flagtemp=out_temp;
-               if (fes!=null)
+               if (fes!=null) {
                    for(int j=0;j<fes.numEffects();j++) {
                        FlagEffect fe=fes.getEffect(j);
                        ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
-                   } else {
-                       ffan.addFlagAction(flagtemp, null, false);
-                   }
+                   } 
+                   for(int j=0;j<fes.numTagEffects();j++) {
+                       TagEffect te=fes.getTagEffect(j);
+                       TempDescriptor tagtemp=getTempforVar(te.getTag());
                
+                       ffan.addTagAction(flagtemp, te.getTag().getTag(), tagtemp, te.getStatus());
+                   }
+               } else {
+                   ffan.addFlagAction(flagtemp, null, false);
+               }
                last.addNext(ffan);
                last=ffan;
            }
@@ -195,10 +251,10 @@ public class BuildFlat {
                    out_temp:
                TempDescriptor.tempFactory("arg",en.getType());
            }
-           FlatNew fn=new FlatNew(td, out_temp, temps[0]);
+           FlatNew fn=new FlatNew(td, out_temp, temps[0], con.isGlobal());
            last.addNext(fn);
            if (temps.length>1) {
-               NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0);
+               NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0, con.isGlobal());
                fn.addNext(np.getBegin());
                return new NodePair(first,np.getEnd()); 
            } else
@@ -206,7 +262,7 @@ public class BuildFlat {
        }
     }
 
-    private NodePair generateNewArrayLoop(TempDescriptor[] temparray, TypeDescriptor td, TempDescriptor tmp, int i) {
+    private NodePair generateNewArrayLoop(TempDescriptor[] temparray, TypeDescriptor td, TempDescriptor tmp, int i, boolean isglobal) {
        TempDescriptor index=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
        TempDescriptor tmpone=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
        FlatNop fnop=new FlatNop();//last node
@@ -222,7 +278,7 @@ public class BuildFlat {
        FlatCondBranch fcb=new FlatCondBranch(tmpbool);
        //is index<temp[i]
        TempDescriptor new_tmp=TempDescriptor.tempFactory("tmp",td);
-       FlatNew fn=new FlatNew(td, new_tmp, temparray[i+1]);
+       FlatNew fn=new FlatNew(td, new_tmp, temparray[i+1], isglobal);
        FlatSetElementNode fsen=new FlatSetElementNode(tmp,index,new_tmp);
        // index=index+1
        FlatOpNode fon=new FlatOpNode(index,index,tmpone,new Operation(Operation.ADD));
@@ -235,7 +291,7 @@ public class BuildFlat {
        fn.addNext(fsen);
        //Recursive call here
        if ((i+2)<temparray.length) {
-           NodePair np2=generateNewArrayLoop(temparray, td.dereference(), new_tmp, i+1);
+           NodePair np2=generateNewArrayLoop(temparray, td.dereference(), new_tmp, i+1, isglobal);
            fsen.addNext(np2.getBegin());
            np2.getEnd().addNext(fon);
        } else {
@@ -541,7 +597,11 @@ public class BuildFlat {
            FlatFieldNode ffn=new FlatFieldNode(nn.getField(), tmp, out_temp); 
            return new NodePair(ffn,ffn);
        } else {
-           TempDescriptor tmp=getTempforVar(nn.getVar());
+           TempDescriptor tmp=getTempforVar(nn.isTag()?nn.getTagVar():nn.getVar());
+           if (nn.isTag()) {
+               //propagate tag
+               out_temp.setTag(tmp.getTag());
+           }
            FlatOpNode fon=new FlatOpNode(out_temp, tmp, null, new Operation(Operation.ASSIGN));
            return new NodePair(fon,fon);
        }
@@ -657,23 +717,53 @@ public class BuildFlat {
        }
     }
 
-    private TempDescriptor getTempforParam(VarDescriptor vd) {
-       if (temptovar.containsKey(vd))
-           return (TempDescriptor)temptovar.get(vd);
+    private NodePair flattenTagDeclarationNode(TagDeclarationNode dn) {
+       TagVarDescriptor tvd=dn.getTagVarDescriptor();
+       TagDescriptor tag=tvd.getTag();
+       TempDescriptor tmp=getTempforVar(tvd);
+       FlatTagDeclaration ftd=new FlatTagDeclaration(tag, tmp);
+       return new NodePair(ftd,ftd);
+    }
+
+    private TempDescriptor getTempforParam(Descriptor d) {
+       if (temptovar.containsKey(d))
+           return (TempDescriptor)temptovar.get(d);
        else {
-           TempDescriptor td=TempDescriptor.paramtempFactory(vd.getName(),vd.getType());
-           temptovar.put(vd,td);
-           return td;
+           if (d instanceof VarDescriptor) {
+               VarDescriptor vd=(VarDescriptor)d;
+               TempDescriptor td=TempDescriptor.paramtempFactory(vd.getName(),vd.getType());
+               temptovar.put(vd,td);
+               return td;
+           } else if (d instanceof TagVarDescriptor) {
+               TagVarDescriptor tvd=(TagVarDescriptor)d;
+               TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
+               TempDescriptor td=TempDescriptor.paramtempFactory(tvd.getName(), tagtype, tvd.getTag());
+               temptovar.put(tvd,td);
+               return td;
+           } else throw new Error("Unreconized Descriptor");
        }
     }
-
-    private TempDescriptor getTempforVar(VarDescriptor vd) {
-       if (temptovar.containsKey(vd))
-           return (TempDescriptor)temptovar.get(vd);
+    
+    private TempDescriptor getTempforVar(Descriptor d) {
+       if (temptovar.containsKey(d))
+           return (TempDescriptor)temptovar.get(d);
        else {
-           TempDescriptor td=TempDescriptor.tempFactory(vd.getName(),vd.getType());
-           temptovar.put(vd,td);
-           return td;
+           if (d instanceof VarDescriptor) {
+               VarDescriptor vd=(VarDescriptor)d;
+               TempDescriptor td=TempDescriptor.tempFactory(vd.getName(), vd.getType());
+               temptovar.put(vd,td);
+               return td;
+           } else if (d instanceof TagVarDescriptor) {
+               TagVarDescriptor tvd=(TagVarDescriptor)d;
+               //BUGFIX TAGTYPE - add next line, modify following
+               //line to tag this new type descriptor, modify
+               //TempDescriptor constructor & factory to set type
+               //using this Type To test, use any program with tags
+               TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
+               TempDescriptor td=TempDescriptor.tempFactory(tvd.getName(),tagtype, tvd.getTag());
+               temptovar.put(tvd,td);
+               return td;
+           } else throw new Error("Unrecognized Descriptor");
        }
     }
 
@@ -820,6 +910,15 @@ public class BuildFlat {
        return flattenBlockNode(sbn.getBlockNode());
     }
 
+    private NodePair flattenAtomicNode(AtomicNode sbn) {
+       NodePair np=flattenBlockNode(sbn.getBlockNode());
+       FlatAtomicEnterNode faen=new FlatAtomicEnterNode();
+       FlatAtomicExitNode faexn=new FlatAtomicExitNode();
+       faen.addNext(np.getBegin());
+       np.getEnd().addNext(faexn);
+       return new NodePair(faen, faexn);
+    }
+
     private NodePair flattenBlockStatementNode(BlockStatementNode bsn) {
        switch(bsn.kind()) {
        case Kind.BlockExpressionNode:
@@ -827,6 +926,9 @@ public class BuildFlat {
            
        case Kind.DeclarationNode:
            return flattenDeclarationNode((DeclarationNode)bsn);
+
+       case Kind.TagDeclarationNode:
+           return flattenTagDeclarationNode((TagDeclarationNode)bsn);
            
        case Kind.IfStatementNode:
            return flattenIfStatementNode((IfStatementNode)bsn);
@@ -842,6 +944,9 @@ public class BuildFlat {
            
        case Kind.SubBlockNode:
            return flattenSubBlockNode((SubBlockNode)bsn);
+
+       case Kind.AtomicNode:
+           return flattenAtomicNode((AtomicNode)bsn);
            
        }
        throw new Error();