From: jzhou Date: Thu, 28 Oct 2010 18:58:57 +0000 (+0000) Subject: Don't allocate type number for interfaces so that we do not need allocate lines in... X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=586b9416b551468db00febb1b0ec923d7bc0e369;p=IRC.git Don't allocate type number for interfaces so that we do not need allocate lines in the virtual method table for interfaces and the virtual method table can be tight. --- diff --git a/Robust/src/IR/ClassDescriptor.java b/Robust/src/IR/ClassDescriptor.java index 6c3735b2..d16940e4 100644 --- a/Robust/src/IR/ClassDescriptor.java +++ b/Robust/src/IR/ClassDescriptor.java @@ -21,22 +21,25 @@ public class ClassDescriptor extends Descriptor { int numstaticfields = 0; // for interfaces - boolean isInterface=false; Vector superinterfaces; SymbolTable superIFdesc; - public ClassDescriptor(String classname) { - this("", classname); + public ClassDescriptor(String classname, boolean isInterface) { + this("", classname, isInterface); } - public ClassDescriptor(String packagename, String classname) { + public ClassDescriptor(String packagename, String classname, boolean isInterface) { super(classname); superclass=null; flags=new SymbolTable(); fields=new SymbolTable(); fieldvec=new Vector(); methods=new SymbolTable(); - classid=UIDCount++; + if(isInterface) { + classid = -2; + } else { + classid=UIDCount++; + } this.packagename=packagename; superinterfaces = new Vector(); superIFdesc = new SymbolTable(); @@ -212,10 +215,6 @@ public class ClassDescriptor extends Descriptor { } public boolean isInterface() { - return this.isInterface; - } - - public void setAsInterface() { - this.isInterface = true; + return this.classid == -2; } } diff --git a/Robust/src/IR/Flat/BuildCode.java b/Robust/src/IR/Flat/BuildCode.java index 8e80c4f6..19e884d6 100644 --- a/Robust/src/IR/Flat/BuildCode.java +++ b/Robust/src/IR/Flat/BuildCode.java @@ -1150,6 +1150,9 @@ public class BuildCode { classit=state.getClassSymbolTable().getDescriptorsIterator(); while(classit.hasNext()) { ClassDescriptor cd=(ClassDescriptor)classit.next(); + if(cd.isInterface()) { + continue; + } if (state.DSM||state.SINGLETM) fillinRow(cd, lbvirtualtable, cd.getId()); else @@ -1272,7 +1275,9 @@ public class BuildCode { cdarray[0] = null; while(it.hasNext()) { ClassDescriptor cd=(ClassDescriptor)it.next(); - cdarray[cd.getId()]=cd; + if(!cd.isInterface()) { + cdarray[cd.getId()]=cd; + } } arraytable=new TypeDescriptor[state.numArrays()]; diff --git a/Robust/src/IR/State.java b/Robust/src/IR/State.java index cf367770..459021b1 100644 --- a/Robust/src/IR/State.java +++ b/Robust/src/IR/State.java @@ -229,7 +229,9 @@ public class State { if (classes.contains(tdn.getSymbol())) throw new Error("Class "+tdn.getSymbol()+" defined twice"); classes.add(tdn); - numclasses++; + if(!tdn.isInterface()) { + numclasses++; + } if((tdn.numstaticfields != 0) || (tdn.numstaticblocks != 0)) { sclasses.add(tdn); } diff --git a/Robust/src/IR/Tree/BuildIR.java b/Robust/src/IR/Tree/BuildIR.java index ddae6ac0..f1372753 100644 --- a/Robust/src/IR/Tree/BuildIR.java +++ b/Robust/src/IR/Tree/BuildIR.java @@ -74,8 +74,8 @@ public class BuildIR { } public ClassDescriptor parseInterfaceDecl(ParseNode pn) { - ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal()); - cn.setAsInterface(); + ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal(), true); + //cn.setAsInterface(); if (!isEmpty(pn.getChild("superIF").getTerminal())) { /* parse inherited interface name */ ParseNode snlist=pn.getChild("superIF").getChild("extend_interface_list"); @@ -307,7 +307,7 @@ public class BuildIR { } public ClassDescriptor parseTypeDecl(ParseNode pn) { - ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal()); + ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal(), false); if (!isEmpty(pn.getChild("super").getTerminal())) { /* parse superclass name */ ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name"); diff --git a/Robust/src/IR/Virtual.java b/Robust/src/IR/Virtual.java index 907ce865..87abaaec 100644 --- a/Robust/src/IR/Virtual.java +++ b/Robust/src/IR/Virtual.java @@ -109,43 +109,43 @@ public class Virtual { for(Iterator it=cd.getMethods(); it.hasNext();) { MethodDescriptor md=(MethodDescriptor)it.next(); if (md.isStatic()||md.getReturnType()==null) - continue; + continue; if (superdesc!=null) { - Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol()); - boolean foundmatch=false; - for(Iterator matchit=possiblematches.iterator(); matchit.hasNext();) { - MethodDescriptor matchmd=(MethodDescriptor)matchit.next(); - if (md.matches(matchmd)) { - int num=((Integer)methodnumber.get(matchmd)).intValue(); - methodnumber.put(md, new Integer(num)); - foundmatch=true; - break; - } - } - if(state.MGC) { - // TODO add version for normal Java later - if(!foundmatch) { - // check if there is a matched method in inherited interfaces - Iterator it_sifs = cd.getSuperInterfaces(); - while(it_sifs.hasNext() && !foundmatch) { - ClassDescriptor superif = (ClassDescriptor)it_sifs.next(); - Set possiblematches_if=superif.getMethodTable().getSet(md.getSymbol()); - for(Iterator matchit=possiblematches_if.iterator(); matchit.hasNext();) { - MethodDescriptor matchmd=(MethodDescriptor)matchit.next(); - if (md.matches(matchmd)) { - int num=((Integer)methodnumber.get(matchmd)).intValue(); - methodnumber.put(md, new Integer(num)); - foundmatch=true; - break; + Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol()); + boolean foundmatch=false; + for(Iterator matchit=possiblematches.iterator(); matchit.hasNext();) { + MethodDescriptor matchmd=(MethodDescriptor)matchit.next(); + if (md.matches(matchmd)) { + int num=((Integer)methodnumber.get(matchmd)).intValue(); + methodnumber.put(md, new Integer(num)); + foundmatch=true; + break; + } + } + if(state.MGC) { + // TODO add version for normal Java later + if(!foundmatch) { + // check if there is a matched method in inherited interfaces + Iterator it_sifs = cd.getSuperInterfaces(); + while(it_sifs.hasNext() && !foundmatch) { + ClassDescriptor superif = (ClassDescriptor)it_sifs.next(); + Set possiblematches_if=superif.getMethodTable().getSet(md.getSymbol()); + for(Iterator matchit=possiblematches_if.iterator(); matchit.hasNext();) { + MethodDescriptor matchmd=(MethodDescriptor)matchit.next(); + if (md.matches(matchmd)) { + int num=((Integer)methodnumber.get(matchmd)).intValue(); + methodnumber.put(md, new Integer(num)); + foundmatch=true; + break; + } + } } } } - } - } - if (!foundmatch) - methodnumber.put(md, new Integer(start++)); + if (!foundmatch) + methodnumber.put(md, new Integer(start++)); } else { - methodnumber.put(md, new Integer(start++)); + methodnumber.put(md, new Integer(start++)); } } classmethodcount.put(cd, new Integer(start)); diff --git a/Robust/src/Parse/java14.cup b/Robust/src/Parse/java14.cup index c1b5470a..866831ee 100644 --- a/Robust/src/Parse/java14.cup +++ b/Robust/src/Parse/java14.cup @@ -820,7 +820,16 @@ class_member_declaration ::= RESULT=(new ParseNode("method")).addChild(method).getRoot(); :} /* repeat the prod for 'class_declaration' here: */ -// | modifiers_opt CLASS IDENTIFIER super_opt class_body +// | modifiers_opt:mo CLASS IDENTIFIER:id super_opt:so interfaces_opt:ifo class_body:body +// {: +// ParseNode pn=new ParseNode("inner_class_declaration"); +// pn.addChild("modifiers").addChild(mo); +// pn.addChild("name").addChild(id); +// pn.addChild("super").addChild(so); +// pn.addChild("superIF").addChild(ifo); +// pn.addChild("classbody").addChild(body); +// RESULT=pn; +// :} | interface_declaration:interfaced {: RESULT=(new ParseNode("interface")).addChild(interfaced).getRoot(); :} diff --git a/Robust/src/Tests/InterfaceTest.java b/Robust/src/Tests/InterfaceTest.java index 80eec8f8..d1a93f55 100644 --- a/Robust/src/Tests/InterfaceTest.java +++ b/Robust/src/Tests/InterfaceTest.java @@ -6,7 +6,12 @@ public interface Instrument { void adjust(); } -class Wind implements Instrument { +public interface Instrument2 { + // Cannot have method definitions: + void play(int n); // Automatically public +} + +class Wind implements Instrument,Instrument2 { public Wind(){} public void play(int n) { System.out.println("Wind.play() " + n); @@ -15,7 +20,7 @@ class Wind implements Instrument { public void adjust() { System.out.println("Wind.adjust()"); } } -class Percussion implements Instrument { +class Percussion implements Instrument,Instrument2 { public Percussion(){} public void play(int n) { System.out.println("Percussion.play() " + n); @@ -24,7 +29,7 @@ class Percussion implements Instrument { public void adjust() { System.out.println("Percussion.adjust()"); } } -class Stringed implements Instrument { +class Stringed implements Instrument,Instrument2 { public Stringed(){} public void play(int n) { System.out.println("Stringed.play() " + n); @@ -58,6 +63,16 @@ public class InterfaceTest { tune(i); } } + static void tune2(Instrument2 i) { + // ... + i.play(9); + } + static void tuneAll2(Instrument2[] e) { + for(int k = 0; k < e.length; k++) { + Instrument2 i = e[k]; + tune2(i); + } + } public static void main(String[] args) { // Upcasting during addition to the array: Instrument.VALUE=5; @@ -68,6 +83,13 @@ public class InterfaceTest { orchestra[3] = new Brass(); orchestra[4] = new Woodwind(); tuneAll(orchestra); + Instrument2[] orchestra2 = new Instrument2[5]; + orchestra2[0] = new Wind(); + orchestra2[1] = new Percussion(); + orchestra2[2] = new Stringed(); + orchestra2[3] = new Brass(); + orchestra2[4] = new Woodwind(); + tuneAll2(orchestra2); } } /* Output: Wind.play() MIDDLE_C