formatting only: added spaces after commas
[org.ibex.classgen.git] / src / org / ibex / classgen / CPGen.java
index 6527816..3a453d1 100644 (file)
@@ -24,6 +24,7 @@ class CPGen {
         CPGen owner; // don't need this yet, but we will to implement ref() and unref()
         int n; // this is the refcount if state == OPEN, index if >= STABLE
         int tag;
+        Ent e0;
         
         Ent(CPGen owner, int tag) { this.owner = owner; this.tag = tag; }
         
@@ -35,7 +36,7 @@ class CPGen {
     // INVARIANTS: tag == 3 || tag == 4
     static class IntEnt extends Ent {
         int i;
-        IntEnt(CPGen owner, int tag) { super(owner,tag); }
+        IntEnt(CPGen owner, int tag) { super(owner, tag); }
         void dump(DataOutput o) throws IOException { super.dump(o); o.writeInt(i);  }
         Object key() {
             switch(tag) {
@@ -49,7 +50,7 @@ class CPGen {
     // INVARIANTS: tag == 5 || tag == 6
     static class LongEnt extends Ent {
         long l;
-        LongEnt(CPGen owner, int tag) { super(owner,tag); }
+        LongEnt(CPGen owner, int tag) { super(owner, tag); }
         void dump(DataOutput o) throws IOException { super.dump(o); o.writeLong(l); }
         Object key() {
             switch(tag) {
@@ -74,7 +75,7 @@ class CPGen {
     static class CPRefEnt extends Ent {
         Ent e1;
         Ent e2;
-        CPRefEnt(CPGen owner, int tag) { super(owner,tag); }
+        CPRefEnt(CPGen owner, int tag) { super(owner, tag); }
         
         String debugToString() { return "[" + e1.n + ":" + e1.debugToString() + (e2 == null ? "" : " + " + e2.n + ":" + e2.debugToString()) + "]"; }
         
@@ -84,27 +85,32 @@ class CPGen {
             if(e2 != null) o.writeShort(e2.n);
         }
         
+        private String fixme() { throw new Error("fixme"); }
         Object key() throws ClassGen.ClassReadExn {
             switch(tag) {
-                case 7: return new Type.Object(((Utf8Ent)e0).s);
+                case 7: return Type.fromDescriptor(((Utf8Ent)e0).s);
                 case 8: return (((Utf8Ent)e1).s);
-                case 9:
+                case 9: {
                     NameAndTypeKey nt = (NameAndTypeKey) e2.key();
                     Type t = Type.fromDescriptor(nt.type);
                     if(t == null) throw new ClassGen.ClassReadExn("invalid type descriptor");
-                    return FieldRef((Type.Object)e1.key(), nt.name, t);
-                case 10:
+                    return new FieldRef((Type.Object)e1.key(), nt.name, t);
+                }
+                case 10: {
                     NameAndTypeKey nt = (NameAndTypeKey) e2.key();
-                    return MethodRef((Type.Object)e1.key(),throw new Error("fixme"));
+                    return new MethodRef((Type.Object)e1.key(), fixme(), null, null);
+                }
             }
+            throw new Error("FIXME");
         }
     }
         
     static class Utf8Ent extends Ent {
         String s;
-        Utf8Ent(CPGen owner) { super(owner,1); }
+        Utf8Ent(CPGen owner) { super(owner, 1); }
         String debugToString() { return s; }
         void dump(DataOutput o) throws IOException { super.dump(o); o.writeUTF(s); }
+        Object key() { throw new Error("Brian is lame"); }
     }
     
     /*
@@ -150,6 +156,8 @@ class CPGen {
         return ent.n;
     }
 
+    public final Type.Object getType(int index) { return Type.fromDescriptor(((Utf8Ent)getByIndex(index)).s).asObject(); }
+
     public final Ent getByIndex(int index) {
         if(state < STABLE) throw new IllegalStateException("constant pool is not stable");
         Ent e;
@@ -158,7 +166,7 @@ class CPGen {
         return e;
     }
     
-    public final Ent addNameAndType(String name, String descriptor) { return add(new NameAndTypeKey(name,descriptor)); }
+    public final Ent addNameAndType(String name, String descriptor) { return add(new NameAndTypeKey(name, descriptor)); }
     public final Ent addUtf8(String s) { return add(new Utf8Key(s)); }
     
     public final Ent add(Object o) {
@@ -171,27 +179,27 @@ class CPGen {
         }
         
         if(o instanceof Type.Object) {
-            CPRefEnt ce = new CPRefEnt(this,7);
+            CPRefEnt ce = new CPRefEnt(this, 7);
             ce.e1 = addUtf8(((Type.Object)o).internalForm());
             ent = ce;
         } else if(o instanceof String) {
-            CPRefEnt ce = new CPRefEnt(this,8);
+            CPRefEnt ce = new CPRefEnt(this, 8);
             ce.e1 = addUtf8((String)o);
             ent = ce;
         } else if(o instanceof Integer) {
-            IntEnt ue = new IntEnt(this,3);
+            IntEnt ue = new IntEnt(this, 3);
             ue.i = ((Integer)o).intValue();
             ent = ue;
         } else if(o instanceof Float) {
-            IntEnt ue = new IntEnt(this,4);
+            IntEnt ue = new IntEnt(this, 4);
             ue.i = Float.floatToIntBits(((Float)o).floatValue());
             ent = ue;
         } else if(o instanceof Long) {
-            LongEnt le = new LongEnt(this,5);
+            LongEnt le = new LongEnt(this, 5);
             le.l = ((Long)o).longValue();
             ent = le;
         } else if(o instanceof Double) {
-            LongEnt le = new LongEnt(this,6);
+            LongEnt le = new LongEnt(this, 6);
             le.l = Double.doubleToLongBits(((Double)o).doubleValue());
             ent = le;
         } else if(o instanceof Utf8Key) {
@@ -199,7 +207,7 @@ class CPGen {
             ue.s = ((Utf8Key)o).s;
             ent = ue;
         } else if(o instanceof NameAndTypeKey) {
-            CPRefEnt ce = new CPRefEnt(this,12);
+            CPRefEnt ce = new CPRefEnt(this, 12);
             NameAndTypeKey key = (NameAndTypeKey) o;
             ce.e1 = addUtf8(key.name);
             ce.e2 = addUtf8(key.type);
@@ -208,9 +216,9 @@ class CPGen {
             ClassGen.FieldOrMethodRef key = (ClassGen.FieldOrMethodRef) o;
             int tag = o instanceof FieldRef ? 9 : o instanceof MethodRef ? 10 : o instanceof MethodRef.I ? 11 : 0;
             if(tag == 0) throw new Error("should never happen");
-            CPRefEnt ce = new CPRefEnt(this,tag);
+            CPRefEnt ce = new CPRefEnt(this, tag);
             ce.e1 = add(key.klass);
-            ce.e2 = addNameAndType(key.name,key.descriptor);
+            ce.e2 = addNameAndType(key.name, key.descriptor);
             ent = ce;
         } else {
             throw new IllegalArgumentException("Unknown type passed to add");
@@ -223,7 +231,7 @@ class CPGen {
 
         usedSlots += spaces;        
 
-        entries.put(o,ent);
+        entries.put(o, ent);
         return ent;
     }
     
@@ -273,7 +281,7 @@ class CPGen {
     public void optimize() {
         if(state != OPEN) throw new IllegalStateException("can't optimize a stable constant pool");
         Ent[] ents = asArray();
-        Sort.sort(ents,reverseCompareFunc);
+        Sort.sort(ents, reverseCompareFunc);
         state = STABLE;
         assignIndex(ents);
     }
@@ -291,7 +299,7 @@ class CPGen {
     
     public void dump(DataOutput o) throws IOException {
         Ent[] ents = asArray();
-        Sort.sort(ents,compareFunc);
+        Sort.sort(ents, compareFunc);
         o.writeShort(usedSlots);
         for(int i=0;i<ents.length;i++) {
             //System.err.println("" + ents[i].n + ": " + ents[i].debugToString());
@@ -299,18 +307,19 @@ class CPGen {
         }
     }
     
-    CPGen(DataInput in) throws ClassGen.ClassReadExn {
-        usedSlots = i.readUnsignedShort();
+    CPGen(DataInput in) throws ClassGen.ClassReadExn, IOException {
+        usedSlots = in.readUnsignedShort();
         if(usedSlots==0) throw new ClassGen.ClassReadExn("invalid used slots");
         
         // these are to remember the CPRefEnt e0 and e1s we have to fix up
+        int[] e0s = new int[usedSlots];
         int[] e1s = new int[usedSlots];
         int[] e2s = new int[usedSlots];
         
         entriesByIndex = new Ent[usedSlots];
         
         for(int index=1;index<usedSlots;index++) {
-            byte tag = i.readByte();
+            byte tag = in.readByte();
             Ent e;
             switch(tag) {
                 case 7: // Object Type
@@ -320,25 +329,25 @@ class CPGen {
                 case 11: // Instance Method Ref
                 case 12: // NameAndType
                 {
-                    e = new CPRefEnt(this,tag);
-                    e1s[index] = i.readUnsignedShort();;
-                    if(tag != 7 && tag != 8) e2s[index] = i.readUnsignedShort();
+                    e = new CPRefEnt(this, tag);
+                    e1s[index] = in.readUnsignedShort();;
+                    if(tag != 7 && tag != 8) e2s[index] = in.readUnsignedShort();
                     break;
                 }
                 case 3: // Integer
                 case 4: // Float
                 {
                     IntEnt ie;
-                    e = ie = new IntEnt(this,tag);
-                    ie.i = i.readInt();
+                    e = ie = new IntEnt(this, tag);
+                    ie.i = in.readInt();
                     break;
                 }
                 case 5: // Long
                 case 6: // Double
                 {
                     LongEnt le;
-                    e = le = new LongEnt(this,tag);
-                    le.l = i.readLong();
+                    e = le = new LongEnt(this, tag);
+                    le.l = in.readLong();
                     index++;
                     break;
                 }
@@ -346,7 +355,7 @@ class CPGen {
                 {
                     Utf8Ent ue;
                     e = ue = new Utf8Ent(this);
-                    ue.s = i.readUTF();
+                    ue.s = in.readUTF();
                     break;
                 }
                 default:
@@ -356,6 +365,7 @@ class CPGen {
         }
         
         for(int index=1;index<usedSlots;index++) {
+            int i = index;
             Ent e = entriesByIndex[index];
             if(e == null) throw new Error("should never happen");
             if(e instanceof CPRefEnt) {
@@ -388,7 +398,7 @@ class CPGen {
             } else if(e instanceof LongEnt) {
                 index++;
             }
-            entries.put(e.key(),e);
+            entries.put(e.key(), e);
         }
         state = STABLE;
     }