bug fix: the loop entrance node of DOWHILELOOP is the begin node, not the condition...
[IRC.git] / Robust / src / IR / Flat / BuildFlat.java
index fb9ecf313aec9dbfe174283632ba3bf8d09a3217..fb3b75d1d7cf8126054e4d920189ba538ec096ef 100644 (file)
@@ -16,6 +16,10 @@ public class BuildFlat {
 
   // for synchronized blocks
   Stack<TempDescriptor> lockStack;
+  
+  // maps tree node to the set of flat node that is translated from the tree node 
+  // WARNING: ONLY DID FOR NAMENODE NOW!
+  Hashtable<TreeNode,Set<FlatNode>> mapNode2FlatNodeSet;
 
   public BuildFlat(State st, TypeUtil typeutil) {
     state=st;
@@ -24,6 +28,7 @@ public class BuildFlat {
     this.breakset=new HashSet();
     this.continueset=new HashSet();
     this.lockStack = new Stack<TempDescriptor>();
+    this.mapNode2FlatNodeSet=new Hashtable<TreeNode,Set<FlatNode>>();
   }
 
   public Hashtable getMap() {
@@ -304,10 +309,6 @@ public class BuildFlat {
       NodePair np=flattenBlockStatementNode(bn.get(i));
       FlatNode np_begin=np.getBegin();
       FlatNode np_end=np.getEnd();
-      if(bn.getLabel()!=null) {
-        // interim implementation to have the labeled statement
-        state.fn2labelMap.put(np_begin, bn.getLabel());
-      }
       if (begin==null) {
         begin=np_begin;
       }
@@ -663,11 +664,18 @@ public class BuildFlat {
 
         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});
+          ClassDescriptor objectcd=typeutil.getClass(TypeUtil.ObjectClass);
+         TempDescriptor src_tmp3=TempDescriptor.tempFactory("src", new TypeDescriptor(stringcd));
+          MethodDescriptor valueOfmd=typeutil.getMethod(stringcd, "valueOf", new TypeDescriptor[] {new TypeDescriptor(objectcd)});
+          FlatCall fc1=new FlatCall(valueOfmd, src_tmp3, null, new TempDescriptor[] {src_tmp2});
+          fc1.setNumLine(an.getNumLine());
+
+          MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat", new TypeDescriptor[] {new TypeDescriptor(stringcd)});
+          FlatCall fc=new FlatCall(concatmd, tmp, src_tmp3, new TempDescriptor[] {src_tmp});
           fc.setNumLine(an.getNumLine());
           src_tmp=tmp;
-          last.addNext(fc);
+          last.addNext(fc1);
+         fc1.addNext(fc);
           last=fc;
         } else {
           FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
@@ -730,11 +738,19 @@ public class BuildFlat {
         }
         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});
+          ClassDescriptor objectcd=typeutil.getClass(TypeUtil.ObjectClass);
+         TempDescriptor src_tmp3=TempDescriptor.tempFactory("src", new TypeDescriptor(stringcd));
+          MethodDescriptor valueOfmd=typeutil.getMethod(stringcd, "valueOf", new TypeDescriptor[] {new TypeDescriptor(objectcd)});
+          FlatCall fc1=new FlatCall(valueOfmd, src_tmp3, null, new TempDescriptor[] {src_tmp2});
+          fc1.setNumLine(an.getNumLine());
+
+          MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat", new TypeDescriptor[] {new TypeDescriptor(stringcd)});
+          FlatCall fc=new FlatCall(concatmd, tmp, src_tmp3, new TempDescriptor[] {src_tmp});
           fc.setNumLine(an.getNumLine());
+
           src_tmp=tmp;
-          last.addNext(fc);
+          last.addNext(fc1);
+         fc1.addNext(fc);
           last=fc;
         } else {
           FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
@@ -807,13 +823,20 @@ public class BuildFlat {
 
 
           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});
-            fc.setNumLine(an.getNumLine());
-            src_tmp=tmp;
-            last.addNext(fc);
-            last=fc;
+           ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
+           ClassDescriptor objectcd=typeutil.getClass(TypeUtil.ObjectClass);
+           TempDescriptor src_tmp3=TempDescriptor.tempFactory("src", new TypeDescriptor(stringcd));
+           MethodDescriptor valueOfmd=typeutil.getMethod(stringcd, "valueOf", new TypeDescriptor[] {new TypeDescriptor(objectcd)});
+           FlatCall fc1=new FlatCall(valueOfmd, src_tmp3, null, new TempDescriptor[] {src_tmp2});
+           fc1.setNumLine(an.getNumLine());
+           
+           MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat", new TypeDescriptor[] {new TypeDescriptor(stringcd)});
+           FlatCall fc=new FlatCall(concatmd, tmp, src_tmp3, new TempDescriptor[] {src_tmp});
+           fc.setNumLine(an.getNumLine());
+           
+           src_tmp=tmp;
+           last.addNext(fc1);
+           last=fc;
           } else {
             FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
             fon.setNumLine(an.getNumLine());
@@ -866,12 +889,19 @@ public class BuildFlat {
 
 
             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});
-              fc.setNumLine(an.getNumLine());
-              src_tmp=tmp;
-              last.addNext(fc);
+             ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
+             ClassDescriptor objectcd=typeutil.getClass(TypeUtil.ObjectClass);
+             TempDescriptor src_tmp3=TempDescriptor.tempFactory("src", new TypeDescriptor(stringcd));
+             MethodDescriptor valueOfmd=typeutil.getMethod(stringcd, "valueOf", new TypeDescriptor[] {new TypeDescriptor(objectcd)});
+             FlatCall fc1=new FlatCall(valueOfmd, src_tmp3, null, new TempDescriptor[] {src_tmp2});
+             fc1.setNumLine(an.getNumLine());
+             
+             MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat", new TypeDescriptor[] {new TypeDescriptor(stringcd)});
+             FlatCall fc=new FlatCall(concatmd, tmp, src_tmp3, new TempDescriptor[] {src_tmp});
+             fc.setNumLine(an.getNumLine());
+             src_tmp=tmp;
+             last.addNext(fc1);
+             fc1.addNext(fc);
               last=fc;
             } else {
               FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
@@ -924,14 +954,23 @@ public class BuildFlat {
 
 
             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});
-              fc.setNumLine(an.getNumLine());
+             ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
+             ClassDescriptor objectcd=typeutil.getClass(TypeUtil.ObjectClass);
+             TempDescriptor src_tmp3=TempDescriptor.tempFactory("src", new TypeDescriptor(stringcd));
+             MethodDescriptor valueOfmd=typeutil.getMethod(stringcd, "valueOf", new TypeDescriptor[] {new TypeDescriptor(objectcd)});
+             FlatCall fc1=new FlatCall(valueOfmd, src_tmp3, null, new TempDescriptor[] {src_tmp2});
+             fc1.setNumLine(an.getNumLine());
+             
+             MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat", new TypeDescriptor[] {new TypeDescriptor(stringcd)});
+             FlatCall fc=new FlatCall(concatmd, tmp, src_tmp3, new TempDescriptor[] {src_tmp});
+             fc.setNumLine(an.getNumLine());
+             src_tmp=tmp;
+             fc1.addNext(fc);
+
               if (first==null)
-                first=fc;
+                first=fc1;
               else
-                last.addNext(fc);
+                last.addNext(fc1);
               src_tmp=tmp;
               last=fc;
             } else {
@@ -980,6 +1019,7 @@ public class BuildFlat {
       }
       FlatFieldNode ffn=new FlatFieldNode(nn.getField(), tmp, out_temp);
       ffn.setNumLine(nn.getNumLine());
+      addMapNode2FlatNodeSet(nn,ffn);
       return new NodePair(ffn,ffn);
     } else {
       TempDescriptor tmp=getTempforVar(nn.isTag()?nn.getTagVar():nn.getVar());
@@ -989,6 +1029,7 @@ public class BuildFlat {
       }
       FlatOpNode fon=new FlatOpNode(out_temp, tmp, null, new Operation(Operation.ASSIGN));
       fon.setNumLine(nn.getNumLine());
+      addMapNode2FlatNodeSet(nn,fon);
       return new NodePair(fon,fon);
     }
   }
@@ -1044,11 +1085,18 @@ public class BuildFlat {
     } else if (op.getOp()==Operation.ADD&&on.getLeft().getType().isString()) {
       //We have a string concatenate
       ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
+      ClassDescriptor objectcd=typeutil.getClass(TypeUtil.ObjectClass);
+      TempDescriptor src_tmp3=TempDescriptor.tempFactory("src", new TypeDescriptor(stringcd));
+      MethodDescriptor valueOfmd=typeutil.getMethod(stringcd, "valueOf", new TypeDescriptor[] {new TypeDescriptor(objectcd)});
+      FlatCall fc1=new FlatCall(valueOfmd, src_tmp3, null, new TempDescriptor[] {temp_left});
+      fc1.setNumLine(on.getNumLine());
+      
       MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat", new TypeDescriptor[] {new TypeDescriptor(stringcd)});
-      FlatCall fc=new FlatCall(concatmd, out_temp, temp_left, new TempDescriptor[] {temp_right});
+      FlatCall fc=new FlatCall(concatmd, out_temp, src_tmp3, new TempDescriptor[] {temp_right});
       fc.setNumLine(on.getNumLine());
       left.getEnd().addNext(right.getBegin());
-      right.getEnd().addNext(fc);
+      right.getEnd().addNext(fc1);
+      fc1.addNext(fc);
       return new NodePair(left.getBegin(), fc);
     }
 
@@ -1290,6 +1338,7 @@ public class BuildFlat {
   }
 
   private NodePair flattenLoopNode(LoopNode ln) {
+    
     HashSet oldbs=breakset;
     HashSet oldcs=continueset;
     breakset=new HashSet();
@@ -1331,6 +1380,9 @@ public class BuildFlat {
       }
       breakset=oldbs;
       continueset=oldcs;
+      if(ln.getLabel()!=null){
+        state.fn2labelMap.put(condition.getBegin(), ln.getLabel());
+      }
       return new NodePair(begin,nopend);
     } else if (ln.getType()==LoopNode.WHILELOOP) {
       TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
@@ -1364,6 +1416,9 @@ public class BuildFlat {
       }
       breakset=oldbs;
       continueset=oldcs;
+      if(ln.getLabel()!=null){
+        state.fn2labelMap.put(begin, ln.getLabel());
+      }
       return new NodePair(begin,nopend);
     } else if (ln.getType()==LoopNode.DOWHILELOOP) {
       TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
@@ -1396,6 +1451,9 @@ public class BuildFlat {
       }
       breakset=oldbs;
       continueset=oldcs;
+      if(ln.getLabel()!=null){
+        state.fn2labelMap.put(begin, ln.getLabel());
+      }
       return new NodePair(begin,nopend);
     } else throw new Error();
   }
@@ -1705,4 +1763,20 @@ public class BuildFlat {
     }
     throw new Error();
   }
+  
+  private void addMapNode2FlatNodeSet(TreeNode tn, FlatNode fn){
+    Set<FlatNode> fnSet=mapNode2FlatNodeSet.get(tn);
+    if(fnSet==null){
+      fnSet=new HashSet<FlatNode>();
+      mapNode2FlatNodeSet.put(tn, fnSet);
+    }
+    fnSet.add(fn);
+  }
+  
+  public Set<FlatNode> getFlatNodeSet(TreeNode tn){
+    // WARNING: ONLY DID FOR NAMENODE NOW!
+    assert(tn instanceof NameNode);
+    return mapNode2FlatNodeSet.get(tn);
+  }
+  
 }