cp cleanup, setsourcefile
authorbrian <brian@brianweb.net>
Thu, 27 May 2004 07:03:46 +0000 (07:03 +0000)
committerbrian <brian@brianweb.net>
Thu, 27 May 2004 07:03:46 +0000 (07:03 +0000)
darcs-hash:20040527070346-24bed-d9812c9e818a589db4cd6467655dad5ba36585a1.gz

src/org/ibex/classgen/AttrGen.java [deleted file]
src/org/ibex/classgen/CPGen.java
src/org/ibex/classgen/ClassGen.java
src/org/ibex/classgen/FieldGen.java
src/org/ibex/classgen/MethodGen.java

diff --git a/src/org/ibex/classgen/AttrGen.java b/src/org/ibex/classgen/AttrGen.java
deleted file mode 100644 (file)
index 758d9f0..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-package org.ibex.classgen;
-
-import java.io.*;
-import java.util.*;
-
-public class AttrGen {
-    private final CPGen cp;
-    private final Hashtable ht = new Hashtable();
-    
-    public AttrGen(CPGen cp) {
-        this.cp = cp;
-    }
-    
-    public void add(String s, byte[] data) {
-        cp.addUtf8(s);
-        ht.put(s,data);
-    }
-    
-    public boolean contains(String s) { return ht.get(s) != null; }
-    
-    public int size() { return ht.size(); }
-    
-    public void dump(DataOutput o) throws IOException {
-        for(Enumeration e = ht.keys(); e.hasMoreElements();) {
-            String name = (String) e.nextElement();
-            byte[] val = (byte[]) ht.get(name);
-            o.writeShort(cp.getUtf8Index(name));
-            o.writeInt(val.length);
-            o.write(val);
-        }
-    }
-}
index dece6c2..56d5149 100644 (file)
@@ -11,7 +11,10 @@ class CPGen {
     private Hashtable entries = new Hashtable();
     private int nextIndex = 1; // 0 is reserved
     private int count;
-    private boolean sealed;
+    private int state;
+    private static final int OPEN = 0;
+    private static final int STABLE = 1; // existing entries won't change
+    private static final int SEALED = 2; // no new entries
     
     CPGen() { }
     
@@ -71,7 +74,8 @@ class CPGen {
     /*
      * Methods
      */
-    public void seal() { sealed = true; }
+    public void seal() { if(state >= SEALED) throw new IllegalStateException(); state = SEALED; }
+    public void stable() { if(state >= STABLE) throw new IllegalStateException(); state = STABLE; }
     
     public final Ent get(Object o) { return (Ent) entries.get(o); }
     public final Ent getUtf8(String s) { return get(new Utf8Key(s)); }
@@ -89,9 +93,8 @@ class CPGen {
     public final Ent addNameAndType(String name, String descriptor) { return add(new ClassGen.NameAndType(name,descriptor)); }
     public final Ent addUtf8(String s) { return add(new Utf8Key(s)); }
     
-    // FEATURE: Don't resolve indexes until dump (for optimize) 
     public final Ent add(Object o) {
-        if(sealed) throw new IllegalStateException("constant pool is sealed");
+        if(state == SEALED) throw new IllegalStateException("constant pool is sealed");
             
         Ent ent = get(o);
         if(ent != null) return ent;
index fd732ec..de32d3c 100644 (file)
@@ -7,6 +7,7 @@ public class ClassGen implements CGConst {
     private Type.Object thisType;
     private Type.Object superType;
     int flags;
+    private String sourceFile; 
     
     private Vector interfaces = new Vector();
     private Vector fields = new Vector();
@@ -42,6 +43,8 @@ public class ClassGen implements CGConst {
         return fg;
     }
     
+    public void setSourceFile(String sourceFile) { this.sourceFile = sourceFile; }
+    
     public void dump(String s) throws IOException { dump(new File(s)); }
     public void dump(File f) throws IOException {
         if(f.isDirectory()) {
@@ -55,6 +58,7 @@ public class ClassGen implements CGConst {
         }
         dump(new FileOutputStream(f));
     }
+    
     public void dump(OutputStream os) throws IOException {
         DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(os));
         _dump(dos);
@@ -65,7 +69,12 @@ public class ClassGen implements CGConst {
         cp.add(thisType);
         cp.add(superType);
         for(int i=0;i<interfaces.size();i++) cp.add((Type.Object)interfaces.elementAt(i));
+        if(sourceFile != null && !attributes.contains("SourceFile")) attributes.add("SourceFile",cp.addUtf8(sourceFile));
+        
+        cp.stable();
+        
         for(int i=0;i<methods.size();i++) ((MethodGen)methods.elementAt(i)).finish();
+        
         cp.seal();
         
         o.writeInt(0xcafebabe); // magic
@@ -92,18 +101,9 @@ public class ClassGen implements CGConst {
         attributes.dump(o); // attributes        
     }
     
-    // FEATURE: Make some of these checked exceptions?
     public static class Exn extends RuntimeException {
         public Exn(String s) { super(s); }
     }
-
-    // FEATURE: Remove these - they are just here to be compatible with the old api
-    public final static MethodRef methodRef(Type.Object c, String name, Type ret, Type[] args) {        
-        return new MethodRef(c,name,ret,args);
-    }
-    public final static FieldRef fieldRef(Type.Object c, String name, Type type) {
-        return new FieldRef(c,name,type);
-    }
     
     public static class NameAndType {
         String name;
@@ -134,7 +134,43 @@ public class ClassGen implements CGConst {
         public InterfaceMethodRef(Type.Object c, NameAndType t) { super(c,t); }
     }
     
-    public static void main(String[] args) throws Exception {
+    static class AttrGen {
+        private final CPGen cp;
+        private final Hashtable ht = new Hashtable();
+        
+        public AttrGen(CPGen cp) {
+            this.cp = cp;
+        }
+        
+        public void add(String s, Object data) {
+            cp.addUtf8(s);
+            ht.put(s,data);
+        }
+        
+        public boolean contains(String s) { return ht.get(s) != null; }
+        
+        public int size() { return ht.size(); }
+        
+        public void dump(DataOutput o) throws IOException {
+            for(Enumeration e = ht.keys(); e.hasMoreElements();) {
+                String name = (String) e.nextElement();
+                Object val = ht.get(name);
+                o.writeShort(cp.getUtf8Index(name));
+                if(val instanceof byte[]) {
+                    byte[] buf = (byte[]) val;
+                    o.writeInt(buf.length);
+                    o.write(buf);
+                } else if(val instanceof CPGen.Ent) {
+                    o.writeInt(2);
+                    o.writeShort(((CPGen.Ent)val).getIndex());
+                } else {
+                    throw new Error("should never happen");
+                }
+            }
+        }
+    }
+    
+    /*public static void main(String[] args) throws Exception {
         Type.Object me = new Type.Object("Test");
         ClassGen cg = new ClassGen("Test","java.lang.Object",ACC_PUBLIC|ACC_SUPER|ACC_FINAL);
         FieldGen fg = cg.addField("foo",Type.INT,ACC_PUBLIC|ACC_STATIC);
@@ -143,7 +179,7 @@ public class ClassGen implements CGConst {
         mg.setMaxLocals(1);
         mg.addPushConst(0);
         //mg.add(ISTORE_0);
-        mg.add(PUTSTATIC,cg.fieldRef(me,"foo",Type.INT));
+        mg.add(PUTSTATIC, fieldRef(me,"foo",Type.INT));
         int top = mg.size();
         mg.add(GETSTATIC,cg.fieldRef(new Type.Object("java.lang.System"),"out",new Type.Object("java.io.PrintStream")));
         //mg.add(ILOAD_0);
@@ -160,5 +196,5 @@ public class ClassGen implements CGConst {
         mg.add(IF_ICMPLT,top);
         mg.add(RETURN);
         cg.dump("Test.class");
-    }
+    }*/
 }
index 39e91c0..e2371d0 100644 (file)
@@ -7,7 +7,7 @@ public class FieldGen implements CGConst {
     private final String name;
     private final Type type;
     private final int flags;
-    private final AttrGen attrs;
+    private final ClassGen.AttrGen attrs;
     
     FieldGen(ClassGen owner, String name,Type type, int flags) {
         if((flags & ~(ACC_PUBLIC|ACC_PRIVATE|ACC_PROTECTED|ACC_VOLATILE|ACC_TRANSIENT|ACC_STATIC|ACC_FINAL)) != 0)
@@ -16,7 +16,7 @@ public class FieldGen implements CGConst {
         this.name = name;
         this.type = type;
         this.flags = flags;
-        this.attrs = new AttrGen(cp);
+        this.attrs = new ClassGen.AttrGen(cp);
         
         cp.addUtf8(name);
         cp.addUtf8(type.getDescriptor());
index 04b6d55..b82567e 100644 (file)
@@ -11,8 +11,8 @@ public class MethodGen implements CGConst {
     private final Type ret;
     private final Type[] args;
     private final int flags;
-    private final AttrGen attrs;
-    private final AttrGen codeAttrs;
+    private final ClassGen.AttrGen attrs;
+    private final ClassGen.AttrGen codeAttrs;
     private final Hashtable exnTable = new Hashtable();
     private final Hashtable thrownExceptions = new Hashtable();
     
@@ -33,8 +33,8 @@ public class MethodGen implements CGConst {
         this.args = args;
         this.flags = flags;
         
-        attrs = new AttrGen(cp);
-        codeAttrs = new AttrGen(cp);
+        attrs = new ClassGen.AttrGen(cp);
+        codeAttrs = new ClassGen.AttrGen(cp);
         
         cp.addUtf8(name);
         cp.addUtf8(getDescriptor());
@@ -91,9 +91,6 @@ public class MethodGen implements CGConst {
     }
     public final int size() { return size; }
     
-    // FEATURE: Deprecate this
-    public final int addPushConst(int n) { return add(LDC,n); }
-
     // These two are optimized for speed, they don't call set() below
     public final int add(byte op) {
         int s = size;