massive refactoring of method-body-printing; see Type.Class.Method.Body
authoradam <adam@megacz.com>
Fri, 1 Jul 2005 06:47:10 +0000 (06:47 +0000)
committeradam <adam@megacz.com>
Fri, 1 Jul 2005 06:47:10 +0000 (06:47 +0000)
darcs-hash:20050701064710-5007d-41ba0a61d4231d7b7fd5bdc766fd56de31ea9d0d.gz

src/org/ibex/classgen/ClassFile.java
src/org/ibex/classgen/HasFlags.java [new file with mode: 0644]
src/org/ibex/classgen/MethodGen.java
src/org/ibex/classgen/Type.java

index a5c22f2..2a755f0 100644 (file)
@@ -199,7 +199,8 @@ public class ClassFile implements CGConst {
         }
     }
 
-    ClassFile(DataInput i) throws IOException {
+    ClassFile(DataInput i) throws IOException { this(i, false); }
+    ClassFile(DataInput i, boolean ssa) throws IOException {
         int magic = i.readInt();
         if (magic != 0xcafebabe) throw new ClassReadExn("invalid magic: " + Long.toString(0xffffffffL & magic, 16));
         minor = i.readShort();
@@ -215,7 +216,9 @@ public class ClassFile implements CGConst {
         int numFields = i.readShort();
         for(int j=0; j<numFields; j++) fields.addElement(new FieldGen(i, cp));
         int numMethods = i.readShort();
-        for(int j=0; j<numMethods; j++) methods.addElement(new MethodGen(this.getType(), i, cp));
+        for(int j=0; j<numMethods; j++) methods.addElement(ssa 
+                                                           ? new JSSA(this.getType(), i, cp) 
+                                                           : new MethodGen(this.getType(), i, cp));
         attributes = new AttrGen(i, cp);
         
         // FEATURE: Support these
diff --git a/src/org/ibex/classgen/HasFlags.java b/src/org/ibex/classgen/HasFlags.java
new file mode 100644 (file)
index 0000000..4bbd8f4
--- /dev/null
@@ -0,0 +1,5 @@
+package org.ibex.classgen;
+
+public interface HasFlags {
+    public int getFlags();
+}
index 7c740d1..854b76a 100644 (file)
@@ -5,7 +5,7 @@ import java.util.*;
 
 /** A class representing a method in a generated classfile
     @see ClassFile#addMethod */
-public class MethodGen implements CGConst {
+public class MethodGen extends Type.Class.Method.Body implements CGConst {
     private final static boolean EMIT_NOPS = false;
     
     private static final int NO_CODE = -1;
@@ -30,6 +30,7 @@ public class MethodGen implements CGConst {
     // Constructors //////////////////////////////////////////////////////////////////////////////
 
     MethodGen(Type.Class.Method method, int flags) {
+        method.super();
         if ((flags & ~VALID_METHOD_FLAGS) != 0) throw new IllegalArgumentException("invalid flags");
         this.method = method;
         this.flags = flags;
@@ -43,10 +44,16 @@ public class MethodGen implements CGConst {
     }
 
     MethodGen(Type.Class c, DataInput in, ConstantPool cp) throws IOException {
-        this.flags = in.readShort();
+        this(in.readShort(), cp.getUtf8KeyByIndex(in.readShort()), c, in, cp); }
+
+    private MethodGen(short flags, String name, Type.Class c, DataInput in, ConstantPool cp) throws IOException {
+        this(flags, name, c.method(name+cp.getUtf8KeyByIndex(in.readShort())), c, in, cp); }
+    private MethodGen(short flags, String name, Type.Class.Method m,
+                      Type.Class c, DataInput in, ConstantPool cp) throws IOException {
+        m.super();
+        this.flags = flags;
         if ((flags & ~VALID_METHOD_FLAGS) != 0) throw new ClassFile.ClassReadExn("invalid flags");
-        String name = cp.getUtf8KeyByIndex(in.readShort());
-        this.method = c.method(name+cp.getUtf8KeyByIndex(in.readShort()));
+        this.method = m;
         this.attrs = new ClassFile.AttrGen(in,cp);
         
         if ((flags & (NATIVE|ABSTRACT))==0)  {
@@ -317,6 +324,9 @@ public class MethodGen implements CGConst {
 
     // Accessors //////////////////////////////////////////////////////////////////////////////
     
+    public int getFlags() { return flags; }
+    public Hashtable getThrownExceptions() { return thrownExceptions; }
+
     /** Returns the size (in instructions) of this method 
         @return The size of the method (in instructions)
     */
@@ -944,7 +954,7 @@ public class MethodGen implements CGConst {
 
     // Debugging //////////////////////////////////////////////////////////////////////////////
 
-    public void debugToString(StringBuffer sb, String constructorName) {
+    public void debugBodyToString(StringBuffer sb) {
         // This is intentionally a local variable so it can be removed by gcclass
         final String[] OP_NAMES = new String[]{
             "nop", "aconst_null", "iconst_m1", "iconst_0", "iconst_1", "iconst_2", 
@@ -991,34 +1001,18 @@ public class MethodGen implements CGConst {
             "", "", "", "", "", "", 
             "", "", "", ""
         };
-        
-        sb.append("  ").append(ClassFile.flagsToString(flags,false));
-        sb.append(method.debugToString());
-        if (thrownExceptions.size() > 0) {
-            sb.append("throws");
-            for(Enumeration e = thrownExceptions.keys();e.hasMoreElements();)
-                sb.append(" ").append(((Type.Class)e.nextElement()).debugToString()).append(",");
-            sb.setLength(sb.length()-1);
-            sb.append(" ");
-        }
-        if ((flags & (NATIVE|ABSTRACT))==0) {
-            sb.append("{\n");
-            for(int i=0;i<size();i++) {
-                sb.append("    ");
-                for(int j=i==0?1:i;j<10000;j*=10) sb.append(" ");
-                sb.append(i).append(": ");
-                sb.append(OP_NAMES[op[i]&0xff]);
-                String s = null;
-                if (arg[i] instanceof Type) s = ((Type)arg[i]).debugToString();
-                else if (arg[i] instanceof Type.Class.Member) s = ((Type.Class.Member)arg[i]).toString();
-                else if (arg[i] instanceof String) s = "\"" + s + "\"";
-                else if (arg[i] != null) s = arg[i].toString();
-                if (s != null) sb.append(" ").append(s);
-                sb.append("\n");
-            }
-            sb.append("  }\n");
-        } else {
-            sb.append(";");
+        for(int i=0;i<size();i++) {
+            sb.append("    ");
+            for(int j=i==0?1:i;j<10000;j*=10) sb.append(" ");
+            sb.append(i).append(": ");
+            sb.append(OP_NAMES[op[i]&0xff]);
+            String s = null;
+            if (arg[i] instanceof Type) s = ((Type)arg[i]).debugToString();
+            else if (arg[i] instanceof Type.Class.Member) s = ((Type.Class.Member)arg[i]).toString();
+            else if (arg[i] instanceof String) s = "\"" + s + "\"";
+            else if (arg[i] != null) s = arg[i].toString();
+            if (s != null) sb.append(" ").append(s);
+            sb.append("\n");
         }
     }
 
index 795a73e..492986d 100644 (file)
@@ -3,7 +3,7 @@ package org.ibex.classgen;
 import java.util.StringTokenizer;
 import java.util.Hashtable;
 
-public class Type {
+public class Type implements CGConst {
 
     private static Hashtable instances = new Hashtable();  // this has to appear at the top of the file
 
@@ -187,6 +187,30 @@ public class Type {
                 sb.append(returnType.getDescriptor());
                 return sb.toString();
             }
+            public abstract class Body implements HasFlags {
+                public abstract java.util.Hashtable getThrownExceptions();
+                public abstract void debugBodyToString(StringBuffer sb);
+                public void debugToString(StringBuffer sb, String constructorName) {
+                    int flags = getFlags();
+                    sb.append("  ").append(ClassFile.flagsToString(flags,false));
+                    sb.append(Method.this.debugToString());
+                    java.util.Hashtable thrownExceptions = getThrownExceptions();
+                    if (thrownExceptions.size() > 0) {
+                        sb.append("throws");
+                        for(java.util.Enumeration e = thrownExceptions.keys();e.hasMoreElements();)
+                            sb.append(" ").append(((Type.Class)e.nextElement()).debugToString()).append(",");
+                        sb.setLength(sb.length()-1);
+                        sb.append(" ");
+                    }
+                    if ((flags & (NATIVE|ABSTRACT))==0) {
+                        sb.append("{\n");
+                        debugBodyToString(sb);
+                        sb.append("  }\n");
+                    } else {
+                        sb.append(";");
+                    }
+                }
+            }
         }
     }