Array support
authorbdemsky <bdemsky>
Sun, 9 Apr 2006 08:23:32 +0000 (08:23 +0000)
committerbdemsky <bdemsky>
Sun, 9 Apr 2006 08:23:32 +0000 (08:23 +0000)
12 files changed:
Robust/src/IR/ClassDescriptor.java
Robust/src/IR/Flat/BuildCode.java
Robust/src/IR/Flat/BuildFlat.java
Robust/src/IR/State.java
Robust/src/IR/Tree/BuildIR.java
Robust/src/IR/Tree/CreateObjectNode.java
Robust/src/IR/Tree/SemanticCheck.java
Robust/src/IR/TypeDescriptor.java
Robust/src/IR/TypeUtil.java
Robust/src/Parse/java14.cup
Robust/src/Runtime/runtime.c
Robust/src/Runtime/runtime.h

index 73b66d7aad7394fe8c3293da18bebff54e857572..aae2e558d54fcbeed71d68d1fa709a05df9d19bc 100644 (file)
@@ -15,17 +15,7 @@ public class ClassDescriptor extends Descriptor {
        methods=new SymbolTable();
        classid=UIDCount++;
     }
-    private static int UIDCount=10; /* 0 is for Arrays */
-    /*    For element types we use as defined in TypeDescriptor
-         public static final int BYTE=1;
-         public static final int SHORT=2;
-         public static final int INT=3;
-         public static final int LONG=4;
-         public static final int CHAR=5;
-         public static final int BOOLEAN=6;
-         public static final int FLOAT=7;
-         public static final int DOUBLE=8;*/
-
+    private static int UIDCount=0; 
     private final int classid;
     String classname;
     String superclass;
index 8dfb38d5b64e8e2eefde7ae25c093a30a82aaa99..84d8eb4eff95566809e4d24d19b854a29466b9c0 100644 (file)
@@ -14,7 +14,7 @@ public class BuildCode {
     String paramsprefix="___params___";
     public static boolean GENERATEPRECISEGC=false;
     public static String PREFIX="";
-    public static String arraytype="___array___";
+    public static String arraytype="ArrayObject";
     Virtual virtualcalls;
     TypeUtil typeutil;
 
@@ -88,6 +88,7 @@ public class BuildCode {
        outmethod.println("#include <runtime.h>");
 
        outclassdefs.println("extern int classsize[];");
+       //Store the sizes of classes & array elements
        generateSizeArray(outmethod);
 
        Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();
@@ -139,10 +140,10 @@ public class BuildCode {
        }
 
        ClassDescriptor objectcd=typeutil.getClass(TypeUtil.ObjectClass);
-       Iteratory arrait=state.getArrayIterator();
+       Iterator arrayit=state.getArrayIterator();
        while(arrayit.hasNext()) {
            TypeDescriptor td=(TypeDescriptor)arrayit.next();
-           int id=getArrayNumber(td);
+           int id=state.getArrayNumber(td);
            fillinRow(objectcd, virtualtable, id+state.numClasses());
        }
        
@@ -195,6 +196,27 @@ public class BuildCode {
            outclassdefs.print("sizeof(struct "+cdarray[i].getSafeSymbol()+")");            
            needcomma=true;
        }
+
+       TypeDescriptor[] sizetable=new TypeDescriptor[state.numArrays()];
+
+       Iterator arrayit=state.getArrayIterator();
+       while(arrayit.hasNext()) {
+           TypeDescriptor td=(TypeDescriptor)arrayit.next();
+           int id=state.getArrayNumber(td);
+           sizetable[id]=td;
+       }
+       
+       for(int i=0;i<state.numArrays();i++) {
+           if (needcomma)
+               outclassdefs.print(", ");
+           TypeDescriptor tdelement=sizetable[i].dereference();
+           if (tdelement.isArray()||tdelement.isClass())
+               outclassdefs.print("sizeof(void *)");
+           else
+               outclassdefs.print("sizeof("+tdelement.getSafeSymbol()+")");
+           needcomma=true;
+       }
+
        outclassdefs.println("};");
     }
 
@@ -247,7 +269,7 @@ public class BuildCode {
 
        for(int i=0;i<fields.size();i++) {
            FieldDescriptor fd=(FieldDescriptor)fields.get(i);
-           if (fd.getType().isClass())
+           if (fd.getType().isClass()||fd.getType().isArray())
                classdefout.println("  struct "+fd.getType().getSafeSymbol()+" * "+fd.getSafeSymbol()+";");
            else 
                classdefout.println("  "+fd.getType().getSafeSymbol()+" "+fd.getSafeSymbol()+";");
@@ -301,7 +323,7 @@ public class BuildCode {
            
            /* Output method declaration */
            if (md.getReturnType()!=null) {
-               if (md.getReturnType().isClass())
+               if (md.getReturnType().isClass()||md.getReturnType().isArray())
                    headersout.print("struct " + md.getReturnType().getSafeSymbol()+" * ");
                else
                    headersout.print(md.getReturnType().getSafeSymbol()+" ");
@@ -321,7 +343,7 @@ public class BuildCode {
                if (printcomma)
                    headersout.print(", ");
                printcomma=true;
-               if (temp.getType().isClass())
+               if (temp.getType().isClass()||temp.getType().isArray())
                    headersout.print("struct " + temp.getType().getSafeSymbol()+" * "+temp.getSafeSymbol());
                else
                    headersout.print(temp.getType().getSafeSymbol()+" "+temp.getSafeSymbol());
@@ -348,7 +370,7 @@ public class BuildCode {
            TypeDescriptor type=td.getType();
            if (type.isNull())
                output.println("   void * "+td.getSafeSymbol()+";");
-           else if (type.isClass())
+           else if (type.isClass()||type.isArray())
                output.println("   struct "+type.getSafeSymbol()+" * "+td.getSafeSymbol()+";");
            else
                output.println("   "+type.getSafeSymbol()+" "+td.getSafeSymbol()+";");
@@ -453,6 +475,12 @@ public class BuildCode {
        case FKind.FlatFieldNode:
            generateFlatFieldNode(fm, (FlatFieldNode) fn,output);
            return;
+       case FKind.FlatElementNode:
+           generateFlatElementNode(fm, (FlatElementNode) fn,output);
+           return;
+       case FKind.FlatSetElementNode:
+           generateFlatSetElementNode(fm, (FlatSetElementNode) fn,output);
+           return;
        case FKind.FlatSetFieldNode:
            generateFlatSetFieldNode(fm, (FlatSetFieldNode) fn,output);
            return;
@@ -514,7 +542,7 @@ public class BuildCode {
        } else {
            
            output.print("((");
-           if (md.getReturnType().isClass())
+           if (md.getReturnType().isClass()||md.getReturnType().isArray())
                output.print("struct " + md.getReturnType().getSafeSymbol()+" * ");
            else
                output.print(md.getReturnType().getSafeSymbol()+" ");
@@ -532,7 +560,7 @@ public class BuildCode {
                if (printcomma)
                    output.print(", ");
                printcomma=true;
-               if (temp.getType().isClass())
+               if (temp.getType().isClass()||temp.getType().isArray())
                    output.print("struct " + temp.getType().getSafeSymbol()+" * ");
                else
                    output.print(temp.getType().getSafeSymbol());
@@ -591,9 +619,34 @@ public class BuildCode {
        output.println(generateTemp(fm, fsfn.getDst())+"->"+ fsfn.getField().getSafeSymbol()+"="+ generateTemp(fm,fsfn.getSrc())+";");
     }
 
+    private void generateFlatElementNode(FlatMethod fm, FlatElementNode fen, PrintWriter output) {
+       TypeDescriptor elementtype=fen.getSrc().getType().dereference();
+       String type="";
+
+       if (elementtype.isArray()||elementtype.isClass())
+           type="void *";
+       else 
+           type=elementtype.getSafeSymbol()+" ";
+       output.println(generateTemp(fm, fen.getDst())+"=(("+ type+"*)(((char *) &("+ generateTemp(fm,fen.getSrc())+"->length))+sizeof(int)))["+generateTemp(fm, fen.getIndex())+"];");
+    }
+
+    private void generateFlatSetElementNode(FlatMethod fm, FlatSetElementNode fsen, PrintWriter output) {
+       //TODO need dynamic check to make sure this assignment is actually legal
+       //Because Object[] could actually be something more specific...ie. Integer[]
+       TypeDescriptor elementtype=fsen.getDst().getType().dereference();
+       String type="";
+
+       if (elementtype.isArray()||elementtype.isClass())
+           type="void *";
+       else 
+           type=elementtype.getSafeSymbol()+" ";
+
+       output.println("(("+type +"*)(((char *) &("+ generateTemp(fm,fsen.getDst())+"->length))+sizeof(int)))["+generateTemp(fm, fsen.getIndex())+"]="+generateTemp(fm,fsen.getSrc())+";");
+    }
+
     private void generateFlatNew(FlatMethod fm, FlatNew fn, PrintWriter output) {
-       if (fm.getType().isArray()) {
-           int arrayid=state.getArrayNumber(fm.getType())+state.numClasses();
+       if (fn.getType().isArray()) {
+           int arrayid=state.getArrayNumber(fn.getType())+state.numClasses();
            output.println(generateTemp(fm,fn.getDst())+"=allocate_newarray("+arrayid+", "+generateTemp(fm, fn.getSize())+");");
        } else
            output.println(generateTemp(fm,fn.getDst())+"=allocate_new("+fn.getType().getClassDesc().getId()+");");
@@ -657,7 +710,7 @@ public class BuildCode {
        ClassDescriptor cn=md.getClassDesc();
        
        if (md.getReturnType()!=null) {
-           if (md.getReturnType().isClass())
+           if (md.getReturnType().isClass()||md.getReturnType().isArray())
                output.print("struct " + md.getReturnType().getSafeSymbol()+" * ");
            else
                output.print(md.getReturnType().getSafeSymbol()+" ");
@@ -678,7 +731,7 @@ public class BuildCode {
            if (printcomma)
                output.print(", ");
            printcomma=true;
-           if (temp.getType().isClass())
+           if (temp.getType().isClass()||temp.getType().isArray())
                output.print("struct "+temp.getType().getSafeSymbol()+" * "+temp.getSafeSymbol());
            else
                output.print(temp.getType().getSafeSymbol()+" "+temp.getSafeSymbol());
index 5b93dc64892209e6965ae593a06ba735d0968345..9eaebf8d49acd567d6df16115aa139882abd3f5c 100644 (file)
@@ -123,9 +123,12 @@ public class BuildFlat {
            }
            FlatNew fn=new FlatNew(td, out_temp, temps[0]);
            last.addNext(fn);
-           NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0);
-           fn.addNext(np.getBegin());
-           return new NodePair(first,np.getEnd()); 
+           if (temps.length>1) {
+               NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0);
+               fn.addNext(np.getBegin());
+               return new NodePair(first,np.getEnd()); 
+           } else
+               return new NodePair(first, fn);
        }
     }
 
@@ -157,7 +160,7 @@ public class BuildFlat {
        fcb.addFalseNext(fnop);
        fn.addNext(fsen);
        //Recursive call here
-       if ((i+1)<temparray.length) {
+       if ((i+2)<temparray.length) {
            NodePair np2=generateNewArrayLoop(temparray, td.dereference(), new_tmp, i+1);
            fsen.addNext(np2.getBegin());
            np2.getEnd().addNext(fon);
index b8b43463c92a8b50a1a78d6ca76451ed5060169d..f70466cb946d191a84fe9ce6a079069575353a7f 100644 (file)
@@ -30,8 +30,10 @@ public class State {
     private int arraycount=0;
 
     public void addArrayType(TypeDescriptor td) {
-       arraytypes.add(td);
-       arraytonumber.put(td,new Integer(arraycount++));
+       if (!arraytypes.contains(td)) {
+           arraytypes.add(td);
+           arraytonumber.put(td,new Integer(arraycount++));
+       }
     }
 
     public Iterator getArrayIterator() {
index 79026676ca5adc2ccf239c16d7877fdfab7d5126..6a22ef7a1657b128a24bf28fb2532619c8f8572c 100644 (file)
@@ -193,12 +193,13 @@ public class BuildIR {
            }
            return con;
        } else if (isNode(pn,"createarray")) {
+           System.out.println(pn.PPrint(3,true));
            TypeDescriptor td=parseTypeDescriptor(pn);
            Vector args=parseDimExprs(pn);
            int num=0;
            if (pn.getChild("dims_opt").getLiteral()!=null)
                num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
-           for(int i=0;i<args.size()+num;i++)
+           for(int i=0;i<(args.size()+num);i++)
                td=td.makeArray(state);
            CreateObjectNode con=new CreateObjectNode(td);
            for(int i=0;i<args.size();i++) {
@@ -233,7 +234,7 @@ public class BuildIR {
            return new FieldAccessNode(en,fieldname);
        } else if (isNode(pn,"arrayaccess")) { 
            ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
-           ExpressionNode index=parseExpression(pn.getChild("index"));
+           ExpressionNode index=parseExpression(pn.getChild("index").getFirstChild());
            return new ArrayAccessNode(en,index);
        } else if (isNode(pn,"cast1")) { 
            return new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
index d6dd24441c21f750dcaced94d7267394fe3502f5..afeae5fbb1d479aba5110fad14b404f59c9a5c96 100644 (file)
@@ -38,14 +38,26 @@ public class CreateObjectNode extends ExpressionNode {
     }
 
     public String printNode(int indent) {
-       String st="new "+td.toString()+"(";
+       String st;
+       boolean isarray=td.isArray();
+       if (isarray)
+           st="new "+td.toString()+"[";
+       else
+           st="new "+td.toString()+"(";
        for(int i=0;i<argumentlist.size();i++) {
            ExpressionNode en=(ExpressionNode)argumentlist.get(i);
            st+=en.printNode(indent);
-           if ((i+1)!=argumentlist.size())
-               st+=", ";
+           if ((i+1)!=argumentlist.size()) {
+               if (isarray) 
+                   st+="][";
+               else
+                   st+=", ";
+           }
        }
-       return st+")";
+       if (isarray)
+           return st+"]";
+       else
+           return st+")";
     }
 
     public int kind() {
index 680603c034000ea1008cc9f3fe10a3858bd9aaff..da31b9d34233e4fe4e826f00f07bbec8ad8d2c70 100644 (file)
@@ -181,6 +181,9 @@ public class SemanticCheck {
         case Kind.FieldAccessNode:
            checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
            return;
+        case Kind.ArrayAccessNode:
+           checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
+           return;
         case Kind.LiteralNode:
            checkLiteralNode(md,nametable,(LiteralNode)en,td);
            return;
@@ -244,6 +247,18 @@ public class SemanticCheck {
                throw new Error("Field node returns "+fan.getType()+", but need "+td);
     }
 
+    void checkArrayAccessNode(MethodDescriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
+       ExpressionNode left=aan.getExpression();
+       checkExpressionNode(md,nametable,left,null);
+
+       checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
+       TypeDescriptor ltd=left.getType();
+
+       if (td!=null)
+           if (!typeutil.isSuperorType(td,aan.getType()))
+               throw new Error("Field node returns "+aan.getType()+", but need "+td);
+    }
+
     void checkLiteralNode(MethodDescriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
        /* Resolve the type */
        Object o=ln.getValue();
@@ -300,6 +315,7 @@ public class SemanticCheck {
        checkExpressionNode(md, nametable, an.getSrc() ,td);
        //TODO: Need check on validity of operation here
        if (!((an.getDest() instanceof FieldAccessNode)||
+             (an.getDest() instanceof ArrayAccessNode)||
              (an.getDest() instanceof NameNode)))
            throw new Error("Bad lside in "+an.printNode(0));
        checkExpressionNode(md, nametable, an.getDest(), null);
@@ -339,43 +355,44 @@ public class SemanticCheck {
 
        TypeDescriptor typetolookin=con.getType();
        checkTypeDescriptor(typetolookin);
-       if (!typetolookin.isClass()) 
-           throw new Error();
-
-       ClassDescriptor classtolookin=typetolookin.getClassDesc();
-       System.out.println("Looking for "+typetolookin.getSymbol());
-       System.out.println(classtolookin.getMethodTable());
-
-       Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
-       MethodDescriptor bestmd=null;
+       if ((!typetolookin.isClass())&&(!typetolookin.isArray())) 
+           throw new Error("Can't allocate primitive type:"+con.printNode(0));
+
+       if (!typetolookin.isArray()) {
+           //Array's don't need constructor calls
+           ClassDescriptor classtolookin=typetolookin.getClassDesc();
+           System.out.println("Looking for "+typetolookin.getSymbol());
+           System.out.println(classtolookin.getMethodTable());
+           
+           Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
+           MethodDescriptor bestmd=null;
        NextMethod:
-       for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
-           MethodDescriptor currmd=(MethodDescriptor)methodit.next();
-           /* Need correct number of parameters */
-           System.out.println("Examining: "+currmd);
-           if (con.numArgs()!=currmd.numParameters())
-               continue;
-           for(int i=0;i<con.numArgs();i++) {
-               if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
-                   continue NextMethod;
-           }
-           /* Method okay so far */
-           if (bestmd==null)
-               bestmd=currmd;
-           else {
-               if (isMoreSpecific(currmd,bestmd)) {
+           for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
+               MethodDescriptor currmd=(MethodDescriptor)methodit.next();
+               /* Need correct number of parameters */
+               System.out.println("Examining: "+currmd);
+               if (con.numArgs()!=currmd.numParameters())
+                   continue;
+               for(int i=0;i<con.numArgs();i++) {
+                   if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
+                       continue NextMethod;
+               }
+               /* Method okay so far */
+               if (bestmd==null)
                    bestmd=currmd;
-               } else if (!isMoreSpecific(bestmd, currmd))
-                   throw new Error("No method is most specific");
-               
-               /* Is this more specific than bestmd */
+               else {
+                   if (isMoreSpecific(currmd,bestmd)) {
+                       bestmd=currmd;
+                   } else if (!isMoreSpecific(bestmd, currmd))
+                       throw new Error("No method is most specific");
+                   
+                   /* Is this more specific than bestmd */
+               }
            }
+           if (bestmd==null)
+               throw new Error("No method found for "+con.printNode(0));
+           con.setConstructor(bestmd);
        }
-       if (bestmd==null)
-           throw new Error("No method found for "+con.printNode(0));
-       con.setConstructor(bestmd);
-
-       
     }
 
 
index 8327609188d02a799377e9f41b702d608a1e3749..78270a0b2af4be737a1b405f6495a3ae2e80c33e 100644 (file)
@@ -55,7 +55,11 @@ public class TypeDescriptor extends Descriptor {
     }
 
     public boolean isArray() {
-       return arraycount>0;
+       return (arraycount>0);
+    }
+
+    public int getArrayCount() {
+       return arraycount;
     }
 
     public TypeDescriptor dereference() {
@@ -69,7 +73,9 @@ public class TypeDescriptor extends Descriptor {
     }
 
     public String getSafeSymbol() {
-       if (isClass())
+       if (isArray()) 
+           return IR.Flat.BuildCode.arraytype;
+       else if (isClass())
            return class_desc.getSafeSymbol();
        else if (isByte())
            return "char";
index fcf7a63ea7eab27a5514d415bd41af7f8e32e9fd..5e12999aafbd4ee7b4d453361c25a4aabcc9a946 100644 (file)
@@ -59,6 +59,30 @@ public class TypeUtil {
     }
 
     public boolean isSuperorType(TypeDescriptor possiblesuper, TypeDescriptor cd2) {
+       //Matching type are always okay
+       if (possiblesuper.equals(cd2))
+           return true;
+
+       //Handle arrays
+       if (cd2.isArray()||possiblesuper.isArray()) {
+           // Object is super class of all arrays
+           if (possiblesuper.getSymbol().equals(ObjectClass)&&!possiblesuper.isArray())
+               return true;
+
+           // If we have the same dimensionality of arrays & both are classes, we can default to the normal test
+           if (cd2.isClass()&&possiblesuper.isClass()
+               &&(possiblesuper.getArrayCount()==cd2.getArrayCount())&&
+               isSuperorType(possiblesuper.getClassDesc(), cd2.getClassDesc()))
+               return true;
+
+           // Object is superclass of all array classes
+           if (possiblesuper.getSymbol().equals(ObjectClass)&&cd2.isClass()
+               &&(possiblesuper.getArrayCount()<cd2.getArrayCount()))
+               return true;
+
+           return false;
+       }
+
        if (possiblesuper.isClass()&&
             cd2.isClass())
            return isSuperorType(possiblesuper.getClassDesc(), cd2.getClassDesc());
@@ -101,14 +125,14 @@ public class TypeUtil {
     }
 
 
-    public boolean isSuperorType(ClassDescriptor possiblesuper, ClassDescriptor cd2) {
+    private boolean isSuperorType(ClassDescriptor possiblesuper, ClassDescriptor cd2) {
        if (possiblesuper==cd2)
            return true;
        else
            return isSuper(possiblesuper, cd2);
     }
 
-    public boolean isSuper(ClassDescriptor possiblesuper, ClassDescriptor cd2) {
+    private boolean isSuper(ClassDescriptor possiblesuper, ClassDescriptor cd2) {
        while(cd2!=null) {
            cd2=getSuper(cd2);
            if (cd2==possiblesuper)
index 13000dc058b184dc882a132a1b277f53af55a58b..ec6c5b80a6facc1ef1ffb283e104a54098280418 100644 (file)
@@ -1068,8 +1068,8 @@ array_creation_uninit ::=
 //     ;
 dim_exprs ::=  dim_expr:exp {: 
                ParseNode pn=new ParseNode("dim_exprs");
-               pn.addChild("expr").addChild(exp);
-               RESULT=exp; :}
+               pn.addChild(exp);
+               RESULT=pn; :}
        |       dim_exprs:base dim_expr:exp {: 
                base.addChild(exp);
                RESULT=base;
index 05d947899c5da42341fbd0d5a739034e56839784..4a408c02f758d5515abdebffb5bc23b46f99ce7f 100644 (file)
@@ -15,3 +15,10 @@ void * allocate_new(int type) {
   *((int *)v)=type;
   return v;
 }
+
+void * allocate_newarray(int type, int length) {
+  void * v=calloc(1,sizeof(struct ArrayObject)+length*classsize[type]);
+  ((int *)v)[0]=type;
+  ((int *)v)[1]=length;
+  return v;
+}
index 3cd48e8fd6831f58918f08d373430805393d9cb6..8278de1db574123151d6521efcd7261421cc8547 100644 (file)
@@ -5,5 +5,6 @@
 
 
 void * allocate_new(int type);
+void * allocate_newarray(int type, int length);
 
 #endif