From efb78fff4583716497cfb5c26ac05925b55a49e9 Mon Sep 17 00:00:00 2001 From: bdemsky Date: Thu, 9 Mar 2006 01:02:50 +0000 Subject: [PATCH] more changes --- Robust/src/IR/Flat/BuildFlat.java | 41 ++++-- Robust/src/IR/Flat/FlatCondBranch.java | 3 + Robust/src/IR/Flat/FlatFieldNode.java | 2 +- Robust/src/IR/Flat/FlatLiteralNode.java | 11 +- Robust/src/IR/Flat/FlatMethod.java | 13 +- Robust/src/IR/Flat/FlatOpNode.java | 7 +- Robust/src/IR/Flat/FlatSetFieldNode.java | 22 +++ Robust/src/IR/MethodDescriptor.java | 2 +- Robust/src/IR/Operation.java | 13 +- Robust/src/IR/Tree/NameNode.java | 14 +- Robust/src/IR/Tree/OpNode.java | 18 +++ Robust/src/IR/Tree/SemanticCheck.java | 168 ++++++++++++++++++++--- Robust/src/IR/TypeDescriptor.java | 22 ++- Robust/src/IR/TypeUtil.java | 45 +++++- 14 files changed, 325 insertions(+), 56 deletions(-) create mode 100644 Robust/src/IR/Flat/FlatSetFieldNode.java diff --git a/Robust/src/IR/Flat/BuildFlat.java b/Robust/src/IR/Flat/BuildFlat.java index f8c214f5..d1f35b14 100644 --- a/Robust/src/IR/Flat/BuildFlat.java +++ b/Robust/src/IR/Flat/BuildFlat.java @@ -27,6 +27,7 @@ public class BuildFlat { BlockNode bn=state.getMethodBody(md); FlatNode fn=flattenBlockNode(bn).getBegin(); FlatMethod fm=new FlatMethod(md, fn); + System.out.println(fm.printMethod()); state.addFlatCode(md,fm); } } @@ -48,7 +49,10 @@ public class BuildFlat { end=np_end; } } - return new NodePair(begin,end); + if (begin==null) { + end=begin=new FlatNop(); + } + return new NodePair(begin,end); } private NodePair flattenBlockExpressionNode(BlockExpressionNode en) { @@ -157,17 +161,31 @@ public class BuildFlat { if (an.getDest().kind()==Kind.FieldAccessNode) { FieldAccessNode fan=(FieldAccessNode)an.getDest(); - - - // Need to assign field + ExpressionNode en=fan.getExpression(); + TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst"); + NodePair np_baseexp=flattenExpressionNode(en, dst_tmp); + last.addNext(np_baseexp.getBegin()); + FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp); + np_baseexp.getEnd().addNext(fsfn); + return new NodePair(np_src.getBegin(), fsfn); } else if (an.getDest().kind()==Kind.NameNode) { - - } - throw new Error(); + + //TODO: FIXME + FlatNop nop=new FlatNop(); + return new NodePair(nop,nop); + } else throw new Error(); } private NodePair flattenNameNode(NameNode nn,TempDescriptor out_temp) { - throw new Error(); + if (nn.getField()!=null) { + TempDescriptor tmp=getTempforVar(nn.getVar()); + FlatFieldNode ffn=new FlatFieldNode(nn.getField(), tmp, out_temp); + return new NodePair(ffn,ffn); + } else { + TempDescriptor tmp=getTempforVar(nn.getVar()); + FlatOpNode fon=new FlatOpNode(out_temp, tmp, null, new Operation(Operation.ASSIGN)); + return new NodePair(fon,fon); + } } private NodePair flattenOpNode(OpNode on,TempDescriptor out_temp) { @@ -213,7 +231,12 @@ public class BuildFlat { private NodePair flattenDeclarationNode(DeclarationNode dn) { VarDescriptor vd=dn.getVarDescriptor(); TempDescriptor td=getTempforVar(vd); - return flattenExpressionNode(dn.getExpression(),td); + if (dn.getExpression()!=null) + return flattenExpressionNode(dn.getExpression(),td); + else { + FlatNop fn=new FlatNop(); + return new NodePair(fn,fn); + } } private TempDescriptor getTempforVar(VarDescriptor vd) { diff --git a/Robust/src/IR/Flat/FlatCondBranch.java b/Robust/src/IR/Flat/FlatCondBranch.java index 9ebee1ee..82471ed6 100644 --- a/Robust/src/IR/Flat/FlatCondBranch.java +++ b/Robust/src/IR/Flat/FlatCondBranch.java @@ -9,11 +9,14 @@ public class FlatCondBranch extends FlatNode { } public void addTrueNext(FlatNode n) { + if (next.size()==0) + next.setSize(1); next.setElementAt(n,0); n.addPrev(this); } public void addFalseNext(FlatNode n) { + next.setSize(2); next.setElementAt(n,1); n.addPrev(this); } diff --git a/Robust/src/IR/Flat/FlatFieldNode.java b/Robust/src/IR/Flat/FlatFieldNode.java index 79d7f6a4..7dfc2e19 100644 --- a/Robust/src/IR/Flat/FlatFieldNode.java +++ b/Robust/src/IR/Flat/FlatFieldNode.java @@ -17,6 +17,6 @@ public class FlatFieldNode extends FlatNode { } public String toString() { - return dst.toString()+"="+src.toString()+"."+field.toString(); + return dst.toString()+"="+src.toString()+"."+field.getSymbol(); } } diff --git a/Robust/src/IR/Flat/FlatLiteralNode.java b/Robust/src/IR/Flat/FlatLiteralNode.java index b7aa9a91..78c2cca5 100644 --- a/Robust/src/IR/Flat/FlatLiteralNode.java +++ b/Robust/src/IR/Flat/FlatLiteralNode.java @@ -16,14 +16,11 @@ public class FlatLiteralNode extends FlatNode { return value; } - public String printNode(int indent) { - /* if (type==NULL) + public String toString() { + if (value==null) return dst+"=null"; - if (type==STRING) { - return dst+"="+'"'+escapeString(value.toString())+'"'; - }*/ - //return dst+"="+"/*"+getType()+ "*/"+value.toString(); - return ""; + else + return dst+"="+escapeString(value.toString()); } private static String escapeString(String st) { String new_st=""; diff --git a/Robust/src/IR/Flat/FlatMethod.java b/Robust/src/IR/Flat/FlatMethod.java index 7845df9f..d0c7b83e 100644 --- a/Robust/src/IR/Flat/FlatMethod.java +++ b/Robust/src/IR/Flat/FlatMethod.java @@ -16,7 +16,7 @@ public class FlatMethod extends FlatNode { } public String printMethod() { - String st=""; + String st=method+"\n"; HashSet tovisit=new HashSet(); HashSet visited=new HashSet(); int labelindex=0; @@ -48,7 +48,7 @@ public class FlatMethod extends FlatNode { tovisit=new HashSet(); visited=new HashSet(); tovisit.add(method_entry); - while(!tovisit.isEmpty()) { + while(current_node!=null||!tovisit.isEmpty()) { if (current_node==null) { current_node=(FlatNode)tovisit.iterator().next(); tovisit.remove(current_node); @@ -56,10 +56,11 @@ public class FlatMethod extends FlatNode { visited.add(current_node); if (nodetolabel.containsKey(current_node)) st+="L"+nodetolabel.get(current_node)+":\n"; - st+=current_node.toString(); if (current_node.numNext()==0) { + st+=" "+current_node.toString()+"\n"; current_node=null; } else if(current_node.numNext()==1) { + st+=" "+current_node.toString()+"\n"; FlatNode nextnode=current_node.getNext(0); if (visited.contains(nextnode)) { st+="goto L"+nodetolabel.get(nextnode)+"\n"; @@ -68,14 +69,14 @@ public class FlatMethod extends FlatNode { current_node=nextnode; } else if (current_node.numNext()==2) { /* Branch */ - st+=((FlatCondBranch)current_node).toString("L"+nodetolabel.get(current_node.getNext(1))); + st+=" "+((FlatCondBranch)current_node).toString("L"+nodetolabel.get(current_node.getNext(1)))+"\n"; + if (!visited.contains(current_node.getNext(1))) + tovisit.add(current_node.getNext(1)); if (visited.contains(current_node.getNext(0))) { st+="goto L"+nodetolabel.get(current_node.getNext(0))+"\n"; current_node=null; } else current_node=current_node.getNext(0); - if (!visited.contains(current_node.getNext(1))) - tovisit.add(current_node.getNext(1)); } else throw new Error(); } return st; diff --git a/Robust/src/IR/Flat/FlatOpNode.java b/Robust/src/IR/Flat/FlatOpNode.java index bc35587d..150b225b 100644 --- a/Robust/src/IR/Flat/FlatOpNode.java +++ b/Robust/src/IR/Flat/FlatOpNode.java @@ -32,6 +32,11 @@ public class FlatOpNode extends FlatNode { } public String toString() { - return dest.toString()+"="+left.toString()+op.toString()+right.toString(); + if (right!=null) + return dest.toString()+"="+left.toString()+op.toString()+right.toString(); + else if (op.getOp()==Operation.ASSIGN) + return dest.toString()+" = "+left.toString(); + else + return dest.toString()+" "+op.toString() +" "+left.toString(); } } diff --git a/Robust/src/IR/Flat/FlatSetFieldNode.java b/Robust/src/IR/Flat/FlatSetFieldNode.java new file mode 100644 index 00000000..874193a3 --- /dev/null +++ b/Robust/src/IR/Flat/FlatSetFieldNode.java @@ -0,0 +1,22 @@ +package IR.Flat; +import IR.FieldDescriptor; + +public class FlatSetFieldNode extends FlatNode { + TempDescriptor src; + TempDescriptor dst; + FieldDescriptor field; + + public FlatSetFieldNode(TempDescriptor dst, FieldDescriptor field, TempDescriptor src) { + this.field=field; + this.src=src; + this.dst=dst; + } + + public FieldDescriptor getField() { + return field; + } + + public String toString() { + return dst.toString()+"."+field.getSymbol()+"="+src.toString(); + } +} diff --git a/Robust/src/IR/MethodDescriptor.java b/Robust/src/IR/MethodDescriptor.java index 32061222..970dc9ea 100644 --- a/Robust/src/IR/MethodDescriptor.java +++ b/Robust/src/IR/MethodDescriptor.java @@ -104,7 +104,7 @@ public class MethodDescriptor extends Descriptor { else st=modifier.toString()+" "+identifier+"("; for(int i=0;i=BYTE)&&(type<=DOUBLE)); } diff --git a/Robust/src/IR/TypeUtil.java b/Robust/src/IR/TypeUtil.java index fa43bed9..600ec9b9 100644 --- a/Robust/src/IR/TypeUtil.java +++ b/Robust/src/IR/TypeUtil.java @@ -2,7 +2,7 @@ package IR; import java.util.*; public class TypeUtil { - public static final String StringClass="java.lang.String"; + public static final String StringClass="String"; State state; Hashtable supertable; @@ -34,10 +34,45 @@ public class TypeUtil { } public boolean isSuperorType(TypeDescriptor possiblesuper, TypeDescriptor cd2) { - if ((possiblesuper.getClassDesc()==null)|| - cd2.getClassDesc()==null) - throw new Error(); - return isSuperorType(possiblesuper.getClassDesc(), cd2.getClassDesc()); + if (possiblesuper.isClass()&& + cd2.isClass()) + return isSuperorType(possiblesuper.getClassDesc(), cd2.getClassDesc()); + else if (possiblesuper.isClass()&& + cd2.isNull()) + return true; + else if (possiblesuper.isNull()) + throw new Error(); //not sure when this case would occur + else if (possiblesuper.isPrimitive()&& + cd2.isPrimitive()) { + ///Primitive widenings from 5.1.2 + if (cd2.isByte()&&(possiblesuper.isByte()||possiblesuper.isShort()|| + possiblesuper.isInt()||possiblesuper.isLong()|| + possiblesuper.isFloat()||possiblesuper.isDouble())) + return true; + if (cd2.isShort()&&(possiblesuper.isShort()|| + possiblesuper.isInt()||possiblesuper.isLong()|| + possiblesuper.isFloat()||possiblesuper.isDouble())) + return true; + if (cd2.isChar()&&(possiblesuper.isChar()|| + possiblesuper.isInt()||possiblesuper.isLong()|| + possiblesuper.isFloat()||possiblesuper.isDouble())) + return true; + if (cd2.isInt()&&(possiblesuper.isInt()||possiblesuper.isLong()|| + possiblesuper.isFloat()||possiblesuper.isDouble())) + return true; + if (cd2.isLong()&&(possiblesuper.isLong()|| + possiblesuper.isFloat()||possiblesuper.isDouble())) + return true; + if (cd2.isFloat()&&(possiblesuper.isFloat()||possiblesuper.isDouble())) + return true; + if (cd2.isDouble()&&possiblesuper.isDouble()) + + return true; + if (cd2.isBoolean()&&possiblesuper.isBoolean()) + return true; + + return false; + } else throw new Error(); } -- 2.34.1