changes
authorbdemsky <bdemsky>
Fri, 10 Mar 2006 19:12:50 +0000 (19:12 +0000)
committerbdemsky <bdemsky>
Fri, 10 Mar 2006 19:12:50 +0000 (19:12 +0000)
16 files changed:
Robust/src/IR/Flat/BuildFlat.java
Robust/src/IR/Flat/FlatCall.java
Robust/src/IR/Flat/FlatCastNode.java
Robust/src/IR/Flat/FlatCondBranch.java
Robust/src/IR/Flat/FlatFieldNode.java
Robust/src/IR/Flat/FlatLiteralNode.java
Robust/src/IR/Flat/FlatMethod.java
Robust/src/IR/Flat/FlatNew.java
Robust/src/IR/Flat/FlatNode.java
Robust/src/IR/Flat/FlatNop.java
Robust/src/IR/Flat/FlatOpNode.java
Robust/src/IR/Flat/FlatReturnNode.java
Robust/src/IR/Flat/FlatSetFieldNode.java
Robust/src/IR/Flat/TempDescriptor.java
Robust/src/IR/MethodDescriptor.java
Robust/src/Main/Main.java

index 32684bc6f16c3a284b414aad06f352c6642a45d2..374674a28f3450ac1395c77de0a8b45a1029cfd4 100644 (file)
@@ -12,6 +12,10 @@ public class BuildFlat {
        temptovar=new Hashtable();
     }
 
+    public Hashtable getMap() {
+       return temptovar;
+    }
+
     public void buildFlat() {
        Iterator it=state.getClassSymbolTable().getDescriptorsIterator();
        while(it.hasNext()) {
@@ -27,6 +31,11 @@ public class BuildFlat {
            BlockNode bn=state.getMethodBody(md);
            FlatNode fn=flattenBlockNode(bn).getBegin();
            FlatMethod fm=new FlatMethod(md, fn);
+           if (!md.isStatic())
+               fm.addParameterTemp(getTempforVar(md.getThis()));
+           for(int i=0;i<md.numParameters();i++) {
+               fm.addParameterTemp(getTempforVar(md.getParameter(i)));
+           }
            System.out.println(fm.printMethod());
            state.addFlatCode(md,fm);
        }
@@ -56,12 +65,12 @@ public class BuildFlat {
     }
 
     private NodePair flattenBlockExpressionNode(BlockExpressionNode en) {
-       TempDescriptor tmp=TempDescriptor.tempFactory("neverused");
+       TempDescriptor tmp=TempDescriptor.tempFactory("neverused",en.getExpression().getType());
        return flattenExpressionNode(en.getExpression(),tmp);
     }
 
     private NodePair flattenCastNode(CastNode cn,TempDescriptor out_temp) {
-       TempDescriptor tmp=TempDescriptor.tempFactory("tocast");
+       TempDescriptor tmp=TempDescriptor.tempFactory("tocast",cn.getExpression().getType());
        NodePair np=flattenExpressionNode(cn.getExpression(), tmp);
        FlatCastNode fcn=new FlatCastNode(cn.getType(), tmp, out_temp);
        np.getEnd().addNext(fcn);
@@ -81,7 +90,7 @@ public class BuildFlat {
        //Build arguments
        for(int i=0;i<con.numArgs();i++) {
            ExpressionNode en=con.getArg(i);
-           TempDescriptor tmp=TempDescriptor.tempFactory("arg");
+           TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
            temps[i]=tmp;
            NodePair np=flattenExpressionNode(en, tmp);
            last.addNext(np.getBegin());
@@ -101,7 +110,7 @@ public class BuildFlat {
        TempDescriptor thisarg=null;
 
        if (min.getExpression()!=null) {
-           thisarg=TempDescriptor.tempFactory("thisarg");
+           thisarg=TempDescriptor.tempFactory("thisarg",min.getExpression().getType());
            NodePair np=flattenExpressionNode(min.getExpression(),thisarg);
            first=np.getBegin();
            last=np.getEnd();
@@ -110,7 +119,7 @@ public class BuildFlat {
        //Build arguments
        for(int i=0;i<min.numArgs();i++) {
            ExpressionNode en=min.getArg(i);
-           TempDescriptor td=TempDescriptor.tempFactory("arg");
+           TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
            temps[i]=td;
            NodePair np=flattenExpressionNode(en, td);
            if (first==null)
@@ -132,7 +141,7 @@ public class BuildFlat {
     }
 
     private NodePair flattenFieldAccessNode(FieldAccessNode fan,TempDescriptor out_temp) {
-       TempDescriptor tmp=TempDescriptor.tempFactory("temp");
+       TempDescriptor tmp=TempDescriptor.tempFactory("temp",fan.getExpression().getType());
        NodePair npe=flattenExpressionNode(fan.getExpression(),tmp);
        FlatFieldNode fn=new FlatFieldNode(fan.getField(),tmp,out_temp);
        npe.getEnd().addNext(fn);
@@ -145,14 +154,14 @@ public class BuildFlat {
        // left side is field
        
        Operation base=an.getOperation().getBaseOp();
-       TempDescriptor src_tmp=TempDescriptor.tempFactory("src");
+       TempDescriptor src_tmp=TempDescriptor.tempFactory("src",an.getSrc().getType());
        NodePair np_src=flattenExpressionNode(an.getSrc(),src_tmp);
        FlatNode last=np_src.getEnd();
        if (base!=null) {
-           TempDescriptor src_tmp2=TempDescriptor.tempFactory("tmp");
+           TempDescriptor src_tmp2=TempDescriptor.tempFactory("tmp", an.getDest().getType());
            NodePair np_dst_init=flattenExpressionNode(an.getDest(),src_tmp2);
            last.addNext(np_dst_init.getBegin());
-           TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst_tmp");
+           TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst_tmp",an.getDest().getType());
            FlatOpNode fon=new FlatOpNode(dst_tmp, src_tmp,src_tmp2, base);
            np_dst_init.getEnd().addNext(fon);
            last=fon;
@@ -162,7 +171,7 @@ public class BuildFlat {
        if (an.getDest().kind()==Kind.FieldAccessNode) {
            FieldAccessNode fan=(FieldAccessNode)an.getDest();
            ExpressionNode en=fan.getExpression();
-           TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst");
+           TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
            NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
            last.addNext(np_baseexp.getBegin());
            FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
@@ -173,7 +182,7 @@ public class BuildFlat {
            if (nn.getExpression()!=null) {
                FieldAccessNode fan=(FieldAccessNode)nn.getExpression();
                ExpressionNode en=fan.getExpression();
-               TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst");
+               TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
                NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
                last.addNext(np_baseexp.getBegin());
                FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
@@ -210,13 +219,15 @@ public class BuildFlat {
     }
 
     private NodePair flattenOpNode(OpNode on,TempDescriptor out_temp) {
-       TempDescriptor temp_left=TempDescriptor.tempFactory("leftop");
-       TempDescriptor temp_right=TempDescriptor.tempFactory("rightop");
+       TempDescriptor temp_left=TempDescriptor.tempFactory("leftop",on.getLeft().getType());
+       TempDescriptor temp_right=null;
+
        NodePair left=flattenExpressionNode(on.getLeft(),temp_left);
        NodePair right;
-       if (on.getRight()!=null)
+       if (on.getRight()!=null) {
+           temp_right=TempDescriptor.tempFactory("rightop",on.getRight().getType());
            right=flattenExpressionNode(on.getRight(),temp_right);
-       else {
+       else {
            FlatNop nop=new FlatNop();
            right=new NodePair(nop,nop);
        }
@@ -264,14 +275,14 @@ public class BuildFlat {
        if (temptovar.containsKey(vd))
            return (TempDescriptor)temptovar.get(vd);
        else {
-           TempDescriptor td=TempDescriptor.tempFactory(vd.getName());
+           TempDescriptor td=TempDescriptor.tempFactory(vd.getName(),vd.getType());
            temptovar.put(vd,td);
            return td;
        }
     }
 
     private NodePair flattenIfStatementNode(IfStatementNode isn) {
-       TempDescriptor cond_temp=TempDescriptor.tempFactory("condition");
+       TempDescriptor cond_temp=TempDescriptor.tempFactory("condition",new TypeDescriptor(TypeDescriptor.BOOLEAN));
        NodePair cond=flattenExpressionNode(isn.getCondition(),cond_temp);
        FlatCondBranch fcb=new FlatCondBranch(cond_temp);
        NodePair true_np=flattenBlockNode(isn.getTrueBlock());
@@ -296,7 +307,7 @@ public class BuildFlat {
     private NodePair flattenLoopNode(LoopNode ln) {
        if (ln.getType()==LoopNode.FORLOOP) {
            NodePair initializer=flattenBlockNode(ln.getInitializer());
-           TempDescriptor cond_temp=TempDescriptor.tempFactory("condition");
+           TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
            NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
            NodePair update=flattenBlockNode(ln.getUpdate());
            NodePair body=flattenBlockNode(ln.getBody());
@@ -312,7 +323,7 @@ public class BuildFlat {
            fcb.addTrueNext(body.getBegin());
            return new NodePair(begin,nopend);
        } else if (ln.getType()==LoopNode.WHILELOOP) {
-           TempDescriptor cond_temp=TempDescriptor.tempFactory("condition");
+           TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
            NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
            NodePair body=flattenBlockNode(ln.getBody());
            FlatNode begin=condition.getBegin();
@@ -325,7 +336,7 @@ public class BuildFlat {
            fcb.addTrueNext(body.getBegin());
            return new NodePair(begin,nopend);
        } else if (ln.getType()==LoopNode.DOWHILELOOP) {
-           TempDescriptor cond_temp=TempDescriptor.tempFactory("condition");
+           TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
            NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
            NodePair body=flattenBlockNode(ln.getBody());
            FlatNode begin=body.getBegin();
@@ -341,7 +352,7 @@ public class BuildFlat {
     }
            
     private NodePair flattenReturnNode(ReturnNode rntree) {
-       TempDescriptor retval=TempDescriptor.tempFactory("ret_value");
+       TempDescriptor retval=TempDescriptor.tempFactory("ret_value", rntree.getReturnExpression().getType());
        NodePair cond=flattenExpressionNode(rntree.getReturnExpression(),retval);
        FlatReturnNode rnflat=new FlatReturnNode(retval);
        cond.getEnd().addNext(rnflat);
index df88faced694681306fca51d9cd7c840bb83cd1d..d11d79d8f6ef3582efe67400bc76dba66b01fa29 100644 (file)
@@ -33,4 +33,28 @@ public class FlatCall extends FlatNode {
        }
        return st+")";
     }
+
+    public int kind() {
+       return FKind.FlatCall;
+    }
+
+    public TempDescriptor [] readsTemps() {
+       int size=args.length;
+       if (this_temp!=null)
+           size++;
+       TempDescriptor [] t=new TempDescriptor[size];
+       int offset=0;
+       if (this_temp!=null)
+           t[offset++]=this_temp;
+       for(int i=0;i<args.length;i++)
+           t[offset++]=args[i];
+       return t;
+    }
+
+    public TempDescriptor [] writesTemps() {
+       if (dst!=null)
+           return new TempDescriptor[] {dst};
+       else
+           return new TempDescriptor[0];
+    }
 }
index 2b3e45a7761b73dc00bcf5423986ec49d70ee23e..d8d80b5fe272f825452ec012648cfda491be52da 100644 (file)
@@ -15,4 +15,17 @@ public class FlatCastNode extends FlatNode {
     public String toString() {
        return dst.toString()+"=("+type.toString()+")"+src.toString();
     }
+
+    public int kind() {
+       return FKind.FlatCastNode;
+    }
+
+    public TempDescriptor [] writesTemps() {
+       return new TempDescriptor[] {dst};
+    }
+
+    public TempDescriptor [] readsTemps() {
+       return new TempDescriptor[] {src};
+    }
+
 }
index 82471ed6738c053b1cab7ce8552b9d45665f29f5..f6a885bf1b3995f4554e1b92e222402c2403b8ed 100644 (file)
@@ -32,4 +32,12 @@ public class FlatCondBranch extends FlatNode {
     public void addNext(FlatNode n) {
        throw new Error();
     }
+
+    public int kind() {
+       return FKind.FlatCondBranch;
+    }
+
+    public TempDescriptor [] readsTemps() {
+       return new TempDescriptor[] {test_cond};
+    }
 }
index 7dfc2e19b35e05d517db16306c8cae627aa209a8..48054b37ad44defd15ae4e21e920323016123cb7 100644 (file)
@@ -19,4 +19,16 @@ public class FlatFieldNode extends FlatNode {
     public String toString() {
        return dst.toString()+"="+src.toString()+"."+field.getSymbol();
     }
+
+    public int kind() {
+       return FKind.FlatFieldNode;
+    }
+
+    public TempDescriptor [] writesTemps() {
+       return new TempDescriptor[] {dst};
+    }
+
+    public TempDescriptor [] readsTemps() {
+       return new TempDescriptor[] {src};
+    }
 }
index 78c2cca52a3eb20b51917b806826a84cc685011b..c27836bcb8d0a75fbfb927ed2d9bc5624a2909cb 100644 (file)
@@ -34,4 +34,12 @@ public class FlatLiteralNode extends FlatNode {
        }
        return new_st;
     }
+
+    public int kind() {
+       return FKind.FlatLiteralNode;
+    }
+
+    public TempDescriptor [] writesTemps() {
+       return new TempDescriptor[] {dst};
+    }
 }
index d0c7b83e09dd1bc95aaa234a345dde110c2c7396..fc444128d53b5e8db459fddf43700031e9f93117 100644 (file)
@@ -5,18 +5,24 @@ import java.util.*;
 public class FlatMethod extends FlatNode {
     FlatNode method_entry;
     MethodDescriptor method;
+    Vector parameterTemps;
 
     FlatMethod(MethodDescriptor md, FlatNode entry) {
        method=md;
        method_entry=entry;
+       parameterTemps=new Vector();
     }
-    
+
     public String toString() {
        return method.toString();
     }
     
+    public void addParameterTemp(TempDescriptor t) {
+       parameterTemps.add(t);
+    }
+
     public String printMethod() {
-       String st=method+"\n";
+       String st=method+" {\n";
        HashSet tovisit=new HashSet();
        HashSet visited=new HashSet();
        int labelindex=0;
@@ -79,7 +85,10 @@ public class FlatMethod extends FlatNode {
                    current_node=current_node.getNext(0);
            } else throw new Error();
        }
-       return st;
+       return st+"}\n";
+    }
+    
+    public TempDescriptor [] writesTemps() {
+       return (TempDescriptor[]) parameterTemps.toArray(new TempDescriptor[ parameterTemps.size()]);
     }
-
 }
index e8aeaa633ab9c17ffed0d6454b665d622f9a7770..c8769bdb338d446828e69bf6a298c125dfca3300 100644 (file)
@@ -13,4 +13,12 @@ public class FlatNew extends FlatNode {
     public String toString() {
        return dst.toString()+"= NEW "+type.toString();
     }
+
+    public int kind() {
+       return FKind.FlatNew;
+    }
+
+    public TempDescriptor [] writesTemps() {
+       return new TempDescriptor[] {dst};
+    }
 }
index ba5641bf2d05489f1fa298058fdc5d120771a1d4..b624516da2f3925be2a897aa8199ae61c8275096 100644 (file)
@@ -34,4 +34,15 @@ public class FlatNode {
     protected void addPrev(FlatNode p) {
        prev.add(p);
     }
+    public int kind() {
+       throw new Error();
+    }
+
+    public TempDescriptor [] readsTemps() {
+       return new TempDescriptor[0];
+    }
+
+    public TempDescriptor [] writesTemps() {
+       return new TempDescriptor[0];
+    }
 }
index 62fad29109b8e55e534c352c0d78452444839f4e..36079fbfaf7d632371aa7221f3e568a63b59e7e8 100644 (file)
@@ -8,4 +8,8 @@ public class FlatNop extends FlatNode {
     public String toString() {
        return "nop";
     }
+
+    public int kind() {
+       return FKind.FlatNop;
+    }
 }
index 150b225b39f002316cd2316f8d86057049d440b3..096929060f60a9690b8ab83fe5c23ae4c81ef661 100644 (file)
@@ -39,4 +39,19 @@ public class FlatOpNode extends FlatNode {
        else
            return dest.toString()+" "+op.toString() +" "+left.toString();
     }
+
+    public int kind() {
+       return FKind.FlatOpNode;
+    }
+
+    public TempDescriptor [] readsTemps() {
+       if (right!=null)
+           return new TempDescriptor [] {left,right};
+       else
+           return new TempDescriptor [] {left};
+    }
+
+    public TempDescriptor [] writesTemps() {
+       return new TempDescriptor [] {dest};
+    }
 }
index d407a38dec50529c67a0d1a8a033d8c2e01809ca..313bf84ca2db4e952753b7867504fe4a244d7cd3 100644 (file)
@@ -10,5 +10,13 @@ public class FlatReturnNode extends FlatNode {
     public String toString() {
        return "return "+tempdesc;
     }
-
+    public int kind() {
+       return FKind.FlatReturnNode;
+    }
+    public TempDescriptor [] readsTemps() {
+       if (tempdesc==null)
+           return new TempDescriptor [0];
+       else
+           return new TempDescriptor [] {tempdesc};
+    }
 }
index 874193a3af6a09b3c4dabfc59a28b0451fad17f0..396627ac536b087e82d06969996433a8c32e076f 100644 (file)
@@ -19,4 +19,12 @@ public class FlatSetFieldNode extends FlatNode {
     public String toString() {
        return dst.toString()+"."+field.getSymbol()+"="+src.toString();
     }
+
+    public int kind() {
+       return FKind.FlatSetFieldNode;
+    }
+    
+    public TempDescriptor [] readsTemps() {
+       return new TempDescriptor [] {src,dst};
+    }
 }
index 8cff4119c883f66e6ca74cdd89770b2addbda080..22daa68850c434f767e323277ee27f8ad71f7b89 100644 (file)
@@ -5,11 +5,17 @@ public class TempDescriptor {
     static int currentid=0;
     int id;
     String safename;
+    TypeDescriptor type;
 
     public TempDescriptor(String name) {
        safename="__"+name+"__";
        id=currentid++;
     }
+
+    public TempDescriptor(String name, TypeDescriptor td) {
+       this(name);
+       type=td;
+    }
     
     public static TempDescriptor tempFactory() {
        return new TempDescriptor("temp_"+currentid);
@@ -19,7 +25,20 @@ public class TempDescriptor {
        return new TempDescriptor(name+currentid);
     }
 
+    public static TempDescriptor tempFactory(String name, TypeDescriptor td) {
+       return new TempDescriptor(name+currentid,td);
+    }
+
+
     public String toString() {
        return safename;
     }
+
+    public void setType(TypeDescriptor td) {
+       type=td;
+    }
+
+    public TypeDescriptor getType() {
+       return type;
+    }
 }
index 970dc9eacd18730c2ec981893f1c4b558b5598c3..acf3c5d8419fd8b796bb632e9433ae6a31943f45 100644 (file)
@@ -49,6 +49,10 @@ public class MethodDescriptor extends Descriptor {
        paramtable.add(vd);
     }
 
+    public VarDescriptor getThis() {
+       return thisvd;
+    }
+
     public boolean isStatic() {
        return modifier.isStatic();
     }
@@ -89,6 +93,10 @@ public class MethodDescriptor extends Descriptor {
        return params.size();
     }
 
+    public VarDescriptor getParameter(int i) {
+       return (VarDescriptor)params.get(i);
+    }
+
     public String getParamName(int i) {
        return ((VarDescriptor)params.get(i)).getName();
     }
index f8a83bb00321919487920bc9ed71e57157afcb2d..e49e21122c2d3ce0d0ca5270de210136969e046e 100644 (file)
@@ -7,6 +7,7 @@ import IR.Tree.ParseNode;
 import IR.Tree.BuildIR;
 import IR.Tree.SemanticCheck;
 import IR.Flat.BuildFlat;
+import IR.Flat.BuildCode;
 import IR.State;
 import IR.TypeUtil;
 
@@ -21,7 +22,6 @@ public class Main {
     java_cup.runtime.lr_parser g;
     g = new Parse.Parser(l);
     ParseNode p=(ParseNode) g./*debug_*/parse().value;
-    //    System.out.println(p.PPrint(2,true));
     State state=new State(p);
 
     BuildIR bir=new BuildIR(state);
@@ -35,6 +35,9 @@ public class Main {
     BuildFlat bf=new BuildFlat(state);
     bf.buildFlat();
 
+    BuildCode bc=new BuildCode(state, bf.getMap());
+    
+
     System.exit(l.numErrors());
   }
 }