Checking in more flattening code.
authorbdemsky <bdemsky>
Thu, 16 Feb 2006 22:19:31 +0000 (22:19 +0000)
committerbdemsky <bdemsky>
Thu, 16 Feb 2006 22:19:31 +0000 (22:19 +0000)
Robust/src/IR/Flat/BuildFlat.java
Robust/src/IR/Tree/BlockExpressionNode.java
Robust/src/IR/Tree/IfStatementNode.java
Robust/src/IR/Tree/ReturnNode.java
Robust/src/Main/Main.java

index 257be353e2901b8a8d06f7ef4caf10e5746690f8..e00b609270b6a2e69eedbc6eafc19530369dde4f 100644 (file)
@@ -9,7 +9,7 @@ public class BuildFlat {
        state=st;
     }
 
-    public void buildflat() {
+    public void buildFlat() {
        Iterator it=state.classset.iterator();
        while(it.hasNext()) {
            ClassDescriptor cn=(ClassDescriptor)it.next();
@@ -49,10 +49,11 @@ public class BuildFlat {
     }
 
     private NodePair flattenBlockExpressionNode(BlockExpressionNode en) {
-       throw new Error();
+       TempDescriptor tmp=TempDescriptor.tempFactory("neverused");
+       return flattenExpressionNode(en.getExpression(),tmp);
     }
 
-    private NodePair flattenExpressionNode(ExpressionNode en) {
+    private NodePair flattenExpressionNode(ExpressionNode en, TempDescriptor out_temp) {
        throw new Error();
     }
 
@@ -61,14 +62,33 @@ public class BuildFlat {
     }
         
     private NodePair flattenIfStatementNode(IfStatementNode isn) {
-       throw new Error();
+       TempDescriptor cond_temp=TempDescriptor.tempFactory("condition");
+       NodePair cond=flattenExpressionNode(isn.getCondition(),cond_temp);
+       FlatCondBranch fcb=new FlatCondBranch(cond_temp);
+       NodePair true_np=flattenBlockNode(isn.getTrueBlock());
+       NodePair false_np;
+       FlatNop nopend=new FlatNop();
+
+       if (isn.getFalseBlock()!=null)
+           false_np=flattenBlockNode(isn.getFalseBlock());
+       else {
+           FlatNop nop=new FlatNop();
+           false_np=new NodePair(nop,nop);
+       }
+
+       cond.getEnd().addNext(fcb);
+       fcb.addTrueNext(true_np.getBegin());
+       fcb.addFalseNext(false_np.getBegin());
+       true_np.getEnd().addNext(nopend);
+       false_np.getEnd().addNext(nopend);
+       return new NodePair(cond.getBegin(), nopend);
     }
            
     private NodePair flattenLoopNode(LoopNode ln) {
        if (ln.getType()==LoopNode.FORLOOP) {
            NodePair initializer=flattenBlockNode(ln.getInitializer());
            TempDescriptor cond_temp=TempDescriptor.tempFactory("condition");
-           NodePair condition=flattenExpressionNode(ln.getCondition());
+           NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
            NodePair update=flattenBlockNode(ln.getUpdate());
            NodePair body=flattenBlockNode(ln.getBody());
            FlatNode begin=initializer.getBegin();
@@ -84,7 +104,7 @@ public class BuildFlat {
            return new NodePair(begin,nopend);
        } else if (ln.getType()==LoopNode.WHILELOOP) {
            TempDescriptor cond_temp=TempDescriptor.tempFactory("condition");
-           NodePair condition=flattenExpressionNode(ln.getCondition());
+           NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
            NodePair body=flattenBlockNode(ln.getBody());
            FlatNode begin=condition.getBegin();
            FlatCondBranch fcb=new FlatCondBranch(cond_temp);
@@ -97,7 +117,7 @@ public class BuildFlat {
            return new NodePair(begin,nopend);
        } else if (ln.getType()==LoopNode.DOWHILELOOP) {
            TempDescriptor cond_temp=TempDescriptor.tempFactory("condition");
-           NodePair condition=flattenExpressionNode(ln.getCondition());
+           NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
            NodePair body=flattenBlockNode(ln.getBody());
            FlatNode begin=body.getBegin();
            FlatCondBranch fcb=new FlatCondBranch(cond_temp);
@@ -112,7 +132,11 @@ public class BuildFlat {
     }
            
     private NodePair flattenReturnNode(IR.Tree.ReturnNode rntree) {
-       throw new Error();
+       TempDescriptor retval=TempDescriptor.tempFactory("ret_value");
+       NodePair cond=flattenExpressionNode(rntree.getReturnExpression(),retval);
+       ReturnNode rnflat=new IR.Flat.ReturnNode(retval);
+       cond.getEnd().addNext(rnflat);
+       return new NodePair(cond.getBegin(),rnflat);
     }
            
     private NodePair flattenSubBlockNode(SubBlockNode sbn) {
index 751e134d3e6525ca538c02786453ff6b5a528483..22a9ee0096ff7e2089f60b15933c6ba932f1a185 100644 (file)
@@ -10,6 +10,10 @@ public class BlockExpressionNode extends BlockStatementNode {
        return en.printNode(indent);
     }
 
+    public ExpressionNode getExpression() {
+       return en;
+    }
+
     public int kind() {
        return Kind.BlockExpressionNode;
     }
index 419f0de34f18bedf3d558d10238e9e3ad62c66f0..c24a703996f01d0154f805ad9fdb7e0dfcb5f9e5 100644 (file)
@@ -10,6 +10,18 @@ public class IfStatementNode extends BlockStatementNode {
        this.true_st=true_st;
        this.else_st=else_st;
     }
+
+    public ExpressionNode getCondition() {
+       return cond;
+    }
+
+    public BlockNode getTrueBlock() {
+       return true_st;
+    }
+
+    public BlockNode getFalseBlock() {
+       return else_st;
+    }
     
     public String printNode(int indent) {
        if (else_st==null)
index 27dfd32f4c7eafdb322c6b1ab5a746a06fe4d2b3..780a4cbda58c82119b519306ae42c8a5c58ff947 100644 (file)
@@ -11,6 +11,10 @@ public class ReturnNode extends BlockStatementNode {
        this.en=en;
     }
 
+    public ExpressionNode getReturnExpression() {
+       return en;
+    }
+
     public String printNode(int indent) {
        if (en==null)
            return "return";
index 027781da26a9eb895ab561e7ba430811c1be72cd..8eebe0a18e5b84d3030f41f42f200ab679af678c 100644 (file)
@@ -25,7 +25,7 @@ public class Main {
     bir.buildtree();
     
     BuildFlat bf=new BuildFlat(state);
-    bf.buildflat();
+    bf.buildFlat();
 
     System.exit(l.numErrors());
   }