start of new file
[IRC.git] / Robust / src / IR / Flat / BuildFlat.java
index 3ff20b0f0d0393d06aaa0578cde62a0b00b4a4de..a460cc98c39828d6dcb2e97a9164c142ede8a129 100644 (file)
@@ -6,10 +6,13 @@ import java.util.*;
 public class BuildFlat {
     State state;
     Hashtable temptovar;
+    MethodDescriptor currmd;
+    TypeUtil typeutil;
 
-    public BuildFlat(State st) {
+    public BuildFlat(State st, TypeUtil typeutil) {
        state=st;
        temptovar=new Hashtable();
+       this.typeutil=typeutil;
     }
 
     public Hashtable getMap() {
@@ -32,13 +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 */
@@ -57,26 +87,70 @@ 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());
+           }
        }
     }
 
+    FlatAtomicEnterNode curran=null;
+
     private void flattenClass(ClassDescriptor cn) {
        Iterator methodit=cn.getMethods();
        while(methodit.hasNext()) {
-           MethodDescriptor md=(MethodDescriptor)methodit.next();
-           BlockNode bn=state.getMethodBody(md);
-           FlatNode fn=flattenBlockNode(bn).getBegin();
-           FlatMethod fm=new FlatMethod(md, fn);
-           if (!md.isStatic())
-               fm.addParameterTemp(getTempforParam(md.getThis()));
-           for(int i=0;i<md.numParameters();i++) {
-               fm.addParameterTemp(getTempforParam(md.getParameter(i)));
+           currmd=(MethodDescriptor)methodit.next();
+           BlockNode bn=state.getMethodBody(currmd);
+
+           if (state.DSM&&currmd.getModifiers().isAtomic()) {
+               curran=new FlatAtomicEnterNode();
+           } else
+               curran=null;
+           NodePair np=flattenBlockNode(bn);
+           FlatNode fn=np.getBegin();
+           if (state.THREAD&&currmd.getModifiers().isSynchronized()) {
+               MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorEnter");
+               TempDescriptor thistd=getTempforVar(currmd.getThis());
+               FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
+               fc.addNext(fn);
+               fn=fc;
+               if (np.getEnd().kind()!=FKind.FlatReturnNode) {
+                   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()) {
+               curran.addNext(fn);
+               fn=curran;
+               if (np.getEnd().kind()!=FKind.FlatReturnNode) {
+                   FlatAtomicExitNode aen=new FlatAtomicExitNode(curran);
+                   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);
+           fm.addNext(fn);
+           if (!currmd.isStatic())
+               fm.addParameterTemp(getTempforParam(currmd.getThis()));
+           for(int i=0;i<currmd.numParameters();i++) {
+               fm.addParameterTemp(getTempforParam(currmd.getParameter(i)));
            }
-           state.addFlatCode(md,fm);
+           state.addFlatCode(currmd,fm);
        }
     }
 
@@ -124,22 +198,10 @@ 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 (con.getFlagEffects()!=null) {
-               FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.NEWOBJECT);
-               FlagEffects fes=con.getFlagEffects();
-               TempDescriptor flagtemp=out_temp;
-               for(int j=0;j<fes.numEffects();j++) {
-                   FlagEffect fe=fes.getEffect(j);
-                   ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
-               }
-               last.addNext(ffan);
-               last=ffan;
-           }
-           //Build arguments
+           // Build arguments
            for(int i=0;i<con.numArgs();i++) {
                ExpressionNode en=con.getArg(i);
                TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
@@ -153,6 +215,28 @@ public class BuildFlat {
            FlatCall fc=new FlatCall(md, null, out_temp, temps);
            last.addNext(fc);
            last=fc;
+           if (td.getClassDesc().hasFlags()) {
+               //          if (con.getFlagEffects()!=null) {
+               FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.NEWOBJECT);
+               FlagEffects fes=con.getFlagEffects();
+               TempDescriptor flagtemp=out_temp;
+               if (fes!=null) {
+                   for(int j=0;j<fes.numEffects();j++) {
+                       FlagEffect fe=fes.getEffect(j);
+                       ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
+                   } 
+                   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;
+           }
            return new NodePair(fn,last); 
        } else {
            FlatNode first=null;
@@ -173,10 +257,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
@@ -184,7 +268,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
@@ -198,9 +282,11 @@ public class BuildFlat {
 
        FlatOpNode fcomp=new FlatOpNode(tmpbool,index,temparray[i],new Operation(Operation.LT));
        FlatCondBranch fcb=new FlatCondBranch(tmpbool);
+       fcb.setTrueProb(State.TRUEPROB);
+       fcb.setLoop();
        //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));
@@ -213,7 +299,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 {
@@ -330,14 +416,23 @@ public class BuildFlat {
                //If it is a preinc we need to store the initial value
                TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
                TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
-
                FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
                last.addNext(ffn);
                last=ffn;
-               FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
-               src_tmp=tmp;
-               last.addNext(fon);
-               last=fon;
+
+               if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
+                   ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
+                   MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
+                   FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
+                   src_tmp=tmp;
+                   last.addNext(fc);
+                   last=fc;
+               } else {
+                   FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
+                   src_tmp=tmp;
+                   last.addNext(fon);
+                   last=fon;
+               }
            }
 
            FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
@@ -376,10 +471,22 @@ public class BuildFlat {
                FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp2);
                last.addNext(fen);
                last=fen;
-               FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
-               src_tmp=tmp;
-               last.addNext(fon);
-               last=fon;
+
+               if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
+                   ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
+                   MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
+                   FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
+                   src_tmp=tmp;
+                   last.addNext(fc);
+                   last=fc;
+               } else {
+
+
+                   FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
+                   src_tmp=tmp;
+                   last.addNext(fon);
+                   last=fon;
+               }
            }
 
 
@@ -416,10 +523,21 @@ public class BuildFlat {
                    FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
                    last.addNext(ffn);
                    last=ffn;
-                   FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
-                   src_tmp=tmp;
-                   last.addNext(fon);
-                   last=fon;
+
+                   
+                   if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
+                       ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
+                       MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
+                       FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
+                       src_tmp=tmp;
+                       last.addNext(fc);
+                       last=fc;
+                   } else {
+                       FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
+                       src_tmp=tmp;
+                       last.addNext(fon);
+                       last=fon;
+                   }
                }
 
 
@@ -450,10 +568,21 @@ public class BuildFlat {
                            last.addNext(ffn);
                        }
                        last=ffn;
-                       FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
-                       src_tmp=tmp;
-                       last.addNext(fon);
-                       last=fon;
+
+                       
+                       if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
+                           ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
+                           MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
+                           FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
+                           src_tmp=tmp;
+                           last.addNext(fc);
+                           last=fc;
+                       } else {
+                           FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
+                           src_tmp=tmp;
+                           last.addNext(fon);
+                           last=fon;
+                       }
                    }               
 
                    FlatSetFieldNode fsfn=new FlatSetFieldNode(getTempforVar(nn.getVar()), nn.getField(), src_tmp);
@@ -486,13 +615,26 @@ public class BuildFlat {
                            last=fon;
                        }
 
-                       FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
-                       if (first==null) 
-                           first=fon;
-                       else 
-                           last.addNext(fon);
-                       src_tmp=tmp;
-                       last=fon;
+                       
+                       if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
+                           ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
+                           MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
+                           FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
+                           if (first==null)
+                               first=fc;
+                           else
+                               last.addNext(fc);
+                           src_tmp=tmp;
+                           last=fc;
+                       } else {
+                           FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
+                           if (first==null) 
+                               first=fon;
+                           else 
+                               last.addNext(fon);
+                           src_tmp=tmp;
+                           last=fon;
+                       }
                    }
 
                    FlatOpNode fon=new FlatOpNode(getTempforVar(nn.getVar()), src_tmp, null, new Operation(Operation.ASSIGN));
@@ -519,7 +661,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);
        }
@@ -530,32 +676,7 @@ public class BuildFlat {
        TempDescriptor temp_right=null;
 
        Operation op=on.getOp();
-       /* We've moved this to assignment nodes
-
-       if (op.getOp()==Operation.POSTINC||
-           op.getOp()==Operation.POSTDEC||
-           op.getOp()==Operation.PREINC||
-           op.getOp()==Operation.PREDEC) {
-           LiteralNode ln=new LiteralNode("int",new Integer(1));
-           ln.setType(new TypeDescriptor(TypeDescriptor.INT));
-           
-           AssignmentNode an=new AssignmentNode(on.getLeft(),
-                                                new OpNode(on.getLeft(),ln, 
-                                                           new Operation((op.getOp()==Operation.POSTINC||op.getOp()==Operation.PREINC)?Operation.PLUS:Operation.MINUS))
-                                                );
-           if (op.getOp()==Operation.POSTINC||
-               op.getOp()==Operation.POSTDEC) {
-               //Can't do, this could have side effects
-               NodePair left=flattenExpressionNode(on.getLeft(),out_temp);
-               NodePair assign=flattenAssignmentNode(an,temp_left);
-               left.getEnd().addNext(assign.getBegin());
-               return new NodePair(left.getBegin(),assign.getEnd());
-           } else {
-               NodePair assign=flattenAssignmentNode(an,out_temp);
-               return assign;
-           }
-           } */
-       
+
        NodePair left=flattenExpressionNode(on.getLeft(),temp_left);
        NodePair right;
        if (on.getRight()!=null) {
@@ -592,6 +713,14 @@ public class BuildFlat {
            fcb.addFalseNext(fon1);
            fon1.addNext(fnop);
            return new NodePair(left.getBegin(), fnop);
+       } else if (op.getOp()==Operation.ADD&&on.getLeft().getType().isString()) {
+           //We have a string concatenate
+           ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
+           MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat", new TypeDescriptor[] {new TypeDescriptor(stringcd)});
+           FlatCall fc=new FlatCall(concatmd, out_temp, temp_left, new TempDescriptor[] {temp_right});
+           left.getEnd().addNext(right.getBegin());
+           right.getEnd().addNext(fc);
+           return new NodePair(left.getBegin(), fc);
        }
 
        FlatOpNode fon=new FlatOpNode(out_temp,temp_left,temp_right,op);
@@ -635,23 +764,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");
        }
     }
 
@@ -687,10 +846,14 @@ public class BuildFlat {
            NodePair body=flattenBlockNode(ln.getBody());
            FlatNode begin=initializer.getBegin();
            FlatCondBranch fcb=new FlatCondBranch(cond_temp);
+           fcb.setTrueProb(State.TRUEPROB);
+           fcb.setLoop();
            FlatNop nopend=new FlatNop();
            FlatBackEdge backedge=new FlatBackEdge();
 
-           initializer.getEnd().addNext(condition.getBegin());
+           FlatNop nop2=new FlatNop();
+           initializer.getEnd().addNext(nop2);
+           nop2.addNext(condition.getBegin());
            body.getEnd().addNext(update.getBegin());
            update.getEnd().addNext(backedge);
            backedge.addNext(condition.getBegin());
@@ -704,6 +867,8 @@ public class BuildFlat {
            NodePair body=flattenBlockNode(ln.getBody());
            FlatNode begin=condition.getBegin();
            FlatCondBranch fcb=new FlatCondBranch(cond_temp);
+           fcb.setTrueProb(State.TRUEPROB);
+           fcb.setLoop();
            FlatNop nopend=new FlatNop();
            FlatBackEdge backedge=new FlatBackEdge();
 
@@ -720,6 +885,8 @@ public class BuildFlat {
            NodePair body=flattenBlockNode(ln.getBody());
            FlatNode begin=body.getBegin();
            FlatCondBranch fcb=new FlatCondBranch(cond_temp);
+           fcb.setTrueProb(State.TRUEPROB);
+           fcb.setLoop();
            FlatNop nopend=new FlatNop();
            FlatBackEdge backedge=new FlatBackEdge();
 
@@ -741,12 +908,25 @@ public class BuildFlat {
        }
 
        FlatReturnNode rnflat=new FlatReturnNode(retval);
+       FlatNode ln=rnflat;
+       if (state.THREAD&&currmd.getModifiers().isSynchronized()) {
+           MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
+           TempDescriptor thistd=getTempforVar(currmd.getThis());
+           FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
+           fc.addNext(ln);
+           ln=fc;
+       }
+       if (state.DSM&&currmd.getModifiers().isAtomic()) {
+           FlatAtomicExitNode faen=new FlatAtomicExitNode(curran);
+           faen.addNext(ln);
+           ln=faen;
+       }
 
        if (cond!=null) {
-           cond.getEnd().addNext(rnflat);
+           cond.getEnd().addNext(ln);
            return new NodePair(cond.getBegin(),rnflat);
        } else
-           return new NodePair(rnflat,rnflat);
+           return new NodePair(ln,rnflat);
     }
 
     private NodePair flattenTaskExitNode(TaskExitNode ten) {
@@ -790,6 +970,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);
+       faen.addNext(np.getBegin());
+       np.getEnd().addNext(faexn);
+       return new NodePair(faen, faexn);
+    }
+
     private NodePair flattenBlockStatementNode(BlockStatementNode bsn) {
        switch(bsn.kind()) {
        case Kind.BlockExpressionNode:
@@ -797,6 +986,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);
@@ -812,6 +1004,9 @@ public class BuildFlat {
            
        case Kind.SubBlockNode:
            return flattenSubBlockNode((SubBlockNode)bsn);
+
+       case Kind.AtomicNode:
+           return flattenAtomicNode((AtomicNode)bsn);
            
        }
        throw new Error();