last few fixmes
[org.ibex.classgen.git] / src / org / ibex / classgen / ClassGen.java
1 package org.ibex.classgen;
2
3 import java.util.*;
4 import java.io.*;
5
6 public class ClassGen implements CGConst {
7     private Type.Object thisType;
8     private Type.Object superType;
9     int flags;
10     
11     private Vector interfaces = new Vector();
12     private Vector fields = new Vector();
13     private Vector methods = new Vector();
14     
15     final CPGen cp;
16     private final AttrGen attributes;
17     
18     public ClassGen(String name, String superName, int flags) {
19         this(new Type.Object(name),new Type.Object(superName),flags);
20     }
21     
22     public ClassGen(Type.Object thisType,Type.Object superType, int flags) {
23         if((flags & ~(ACC_PUBLIC|ACC_FINAL|ACC_SUPER|ACC_INTERFACE|ACC_ABSTRACT)) != 0)
24             throw new IllegalArgumentException("invalid flags");
25         this.thisType = thisType;
26         this.superType = superType;
27         this.flags = flags;
28         
29         cp = new CPGen();
30         attributes = new AttrGen(cp);
31     }
32     
33     public final MethodGen addMethod(String name, Type ret, Type[] args, int flags) {
34         MethodGen mg = new MethodGen(this,name,ret,args,flags);
35         methods.addElement(mg);
36         return mg;
37     }
38     
39     public final FieldGen addField(String name, Type type, int flags) {
40         FieldGen fg = new FieldGen(this,name,type,flags);
41         fields.addElement(fg);
42         return fg;
43     }
44     
45     public void dump(String s) throws IOException { dump(new File(s)); }
46     public void dump(File f) throws IOException {
47         if(f.isDirectory()) {
48             String[] a = thisType.components();
49             int i;
50             for(i=0;i<a.length-1;i++) {
51                 f = new File(f,a[i]);
52                 f.mkdir();
53             }
54             f = new File(f,a[i] + ".class");
55         }
56         dump(new FileOutputStream(f));
57     }
58     public void dump(OutputStream os) throws IOException {
59         DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(os));
60         _dump(dos);
61         dos.flush();
62     }
63     
64     private void _dump(DataOutput o) throws IOException {
65         cp.add(thisType);
66         cp.add(superType);
67         for(int i=0;i<interfaces.size();i++) cp.add((Type.Object)interfaces.elementAt(i));
68         for(int i=0;i<methods.size();i++) ((MethodGen)methods.elementAt(i)).finish();
69         cp.seal();
70         
71         o.writeInt(0xcafebabe); // magic
72         o.writeShort(3); // minor_version
73         o.writeShort(45); // major_version
74         
75         o.writeShort(cp.size()); // constant_pool_count
76         cp.dump(o); // constant_pool
77         
78         o.writeShort(flags);
79         o.writeShort(cp.getIndex(thisType)); // this_class
80         o.writeShort(cp.getIndex(superType)); // super_class
81         
82         o.writeShort(interfaces.size()); // interfaces_count
83         for(int i=0;i<interfaces.size();i++) o.writeShort(cp.getIndex(interfaces.elementAt(i))); // interfaces
84         
85         o.writeShort(fields.size()); // fields_count
86         for(int i=0;i<fields.size();i++) ((FieldGen)fields.elementAt(i)).dump(o); // fields
87
88         o.writeShort(methods.size()); // methods_count
89         for(int i=0;i<methods.size();i++) ((MethodGen)methods.elementAt(i)).dump(o); // methods
90         
91         o.writeShort(attributes.size()); // attributes_count
92         attributes.dump(o); // attributes        
93     }
94     
95     // FEATURE: Make some of these checked exceptions?
96     public static class Exn extends RuntimeException {
97         public Exn(String s) { super(s); }
98     }
99
100     // FEATURE: Remove these - they are just here to be compatible with the old api
101     public final static MethodRef methodRef(Type.Object c, String name, Type ret, Type[] args) {        
102         return new MethodRef(c,name,ret,args);
103     }
104     public final static FieldRef fieldRef(Type.Object c, String name, Type type) {
105         return new FieldRef(c,name,type);
106     }
107     
108     public static class NameAndType {
109         String name;
110         String type;
111         NameAndType(String name, String type) { this.name = name; this.type = type; }
112         public boolean equals(Object o_) {
113             if(!(o_ instanceof NameAndType)) return false;
114             NameAndType o = (NameAndType) o_;
115             return o.name.equals(name) && o.type.equals(type);
116         }
117         public int hashCode() { return name.hashCode() ^ type.hashCode(); }
118     }
119     
120     public static abstract class FieldMethodRef {
121         Type.Object klass;
122         NameAndType nameAndType;
123         FieldMethodRef(Type.Object klass, NameAndType nameAndType) { this.klass = klass; this.nameAndType = nameAndType; }
124         public boolean equals(Object o_) {
125             if(!(o_ instanceof FieldMethodRef)) return false;
126             FieldMethodRef o = (FieldMethodRef) o_;
127             return o.klass.equals(klass) && o.nameAndType.equals(nameAndType);
128         }
129         public int hashCode() { return klass.hashCode() ^ nameAndType.hashCode(); }
130     }
131     
132     public static class InterfaceMethodRef extends FieldMethodRef {
133         public InterfaceMethodRef(MethodRef r) { super(r.klass,r.nameAndType); }
134         public InterfaceMethodRef(Type.Object c, NameAndType t) { super(c,t); }
135     }
136     
137     public static void main(String[] args) throws Exception {
138         Type.Object me = new Type.Object("Test");
139         ClassGen cg = new ClassGen("Test","java.lang.Object",ACC_PUBLIC|ACC_SUPER|ACC_FINAL);
140         FieldGen fg = cg.addField("foo",Type.INT,ACC_PUBLIC|ACC_STATIC);
141         
142         MethodGen mg = cg.addMethod("main",Type.VOID,new Type[]{Type.arrayType(Type.STRING)},ACC_STATIC|ACC_PUBLIC);
143         mg.setMaxLocals(1);
144         mg.addPushConst(0);
145         //mg.add(ISTORE_0);
146         mg.add(PUTSTATIC,cg.fieldRef(me,"foo",Type.INT));
147         int top = mg.size();
148         mg.add(GETSTATIC,cg.fieldRef(new Type.Object("java.lang.System"),"out",new Type.Object("java.io.PrintStream")));
149         //mg.add(ILOAD_0);
150         mg.add(GETSTATIC,cg.fieldRef(me,"foo",Type.INT));
151         mg.add(INVOKEVIRTUAL,cg.methodRef(new Type.Object("java.io.PrintStream"),"println",Type.VOID, new Type[]{Type.INT}));
152         //mg.add(IINC,new int[]{0,1});
153         //mg.add(ILOAD_0);
154         mg.add(GETSTATIC,cg.fieldRef(me,"foo",Type.INT));
155         mg.addPushConst(1);
156         mg.add(IADD);
157         mg.add(DUP);
158         mg.add(PUTSTATIC,cg.fieldRef(me,"foo",Type.INT));       
159         mg.addPushConst(10);
160         mg.add(IF_ICMPLT,top);
161         mg.add(RETURN);
162         cg.dump("Test.class");
163     }
164 }