cp cleanup, setsourcefile
[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     private String sourceFile; 
11     
12     private Vector interfaces = new Vector();
13     private Vector fields = new Vector();
14     private Vector methods = new Vector();
15     
16     final CPGen cp;
17     private final AttrGen attributes;
18     
19     public ClassGen(String name, String superName, int flags) {
20         this(new Type.Object(name),new Type.Object(superName),flags);
21     }
22     
23     public ClassGen(Type.Object thisType,Type.Object superType, int flags) {
24         if((flags & ~(ACC_PUBLIC|ACC_FINAL|ACC_SUPER|ACC_INTERFACE|ACC_ABSTRACT)) != 0)
25             throw new IllegalArgumentException("invalid flags");
26         this.thisType = thisType;
27         this.superType = superType;
28         this.flags = flags;
29         
30         cp = new CPGen();
31         attributes = new AttrGen(cp);
32     }
33     
34     public final MethodGen addMethod(String name, Type ret, Type[] args, int flags) {
35         MethodGen mg = new MethodGen(this,name,ret,args,flags);
36         methods.addElement(mg);
37         return mg;
38     }
39     
40     public final FieldGen addField(String name, Type type, int flags) {
41         FieldGen fg = new FieldGen(this,name,type,flags);
42         fields.addElement(fg);
43         return fg;
44     }
45     
46     public void setSourceFile(String sourceFile) { this.sourceFile = sourceFile; }
47     
48     public void dump(String s) throws IOException { dump(new File(s)); }
49     public void dump(File f) throws IOException {
50         if(f.isDirectory()) {
51             String[] a = thisType.components();
52             int i;
53             for(i=0;i<a.length-1;i++) {
54                 f = new File(f,a[i]);
55                 f.mkdir();
56             }
57             f = new File(f,a[i] + ".class");
58         }
59         dump(new FileOutputStream(f));
60     }
61     
62     public void dump(OutputStream os) throws IOException {
63         DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(os));
64         _dump(dos);
65         dos.flush();
66     }
67     
68     private void _dump(DataOutput o) throws IOException {
69         cp.add(thisType);
70         cp.add(superType);
71         for(int i=0;i<interfaces.size();i++) cp.add((Type.Object)interfaces.elementAt(i));
72         if(sourceFile != null && !attributes.contains("SourceFile")) attributes.add("SourceFile",cp.addUtf8(sourceFile));
73         
74         cp.stable();
75         
76         for(int i=0;i<methods.size();i++) ((MethodGen)methods.elementAt(i)).finish();
77         
78         cp.seal();
79         
80         o.writeInt(0xcafebabe); // magic
81         o.writeShort(3); // minor_version
82         o.writeShort(45); // major_version
83         
84         o.writeShort(cp.size()); // constant_pool_count
85         cp.dump(o); // constant_pool
86         
87         o.writeShort(flags);
88         o.writeShort(cp.getIndex(thisType)); // this_class
89         o.writeShort(cp.getIndex(superType)); // super_class
90         
91         o.writeShort(interfaces.size()); // interfaces_count
92         for(int i=0;i<interfaces.size();i++) o.writeShort(cp.getIndex(interfaces.elementAt(i))); // interfaces
93         
94         o.writeShort(fields.size()); // fields_count
95         for(int i=0;i<fields.size();i++) ((FieldGen)fields.elementAt(i)).dump(o); // fields
96
97         o.writeShort(methods.size()); // methods_count
98         for(int i=0;i<methods.size();i++) ((MethodGen)methods.elementAt(i)).dump(o); // methods
99         
100         o.writeShort(attributes.size()); // attributes_count
101         attributes.dump(o); // attributes        
102     }
103     
104     public static class Exn extends RuntimeException {
105         public Exn(String s) { super(s); }
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     static class AttrGen {
138         private final CPGen cp;
139         private final Hashtable ht = new Hashtable();
140         
141         public AttrGen(CPGen cp) {
142             this.cp = cp;
143         }
144         
145         public void add(String s, Object data) {
146             cp.addUtf8(s);
147             ht.put(s,data);
148         }
149         
150         public boolean contains(String s) { return ht.get(s) != null; }
151         
152         public int size() { return ht.size(); }
153         
154         public void dump(DataOutput o) throws IOException {
155             for(Enumeration e = ht.keys(); e.hasMoreElements();) {
156                 String name = (String) e.nextElement();
157                 Object val = ht.get(name);
158                 o.writeShort(cp.getUtf8Index(name));
159                 if(val instanceof byte[]) {
160                     byte[] buf = (byte[]) val;
161                     o.writeInt(buf.length);
162                     o.write(buf);
163                 } else if(val instanceof CPGen.Ent) {
164                     o.writeInt(2);
165                     o.writeShort(((CPGen.Ent)val).getIndex());
166                 } else {
167                     throw new Error("should never happen");
168                 }
169             }
170         }
171     }
172     
173     /*public static void main(String[] args) throws Exception {
174         Type.Object me = new Type.Object("Test");
175         ClassGen cg = new ClassGen("Test","java.lang.Object",ACC_PUBLIC|ACC_SUPER|ACC_FINAL);
176         FieldGen fg = cg.addField("foo",Type.INT,ACC_PUBLIC|ACC_STATIC);
177         
178         MethodGen mg = cg.addMethod("main",Type.VOID,new Type[]{Type.arrayType(Type.STRING)},ACC_STATIC|ACC_PUBLIC);
179         mg.setMaxLocals(1);
180         mg.addPushConst(0);
181         //mg.add(ISTORE_0);
182         mg.add(PUTSTATIC, fieldRef(me,"foo",Type.INT));
183         int top = mg.size();
184         mg.add(GETSTATIC,cg.fieldRef(new Type.Object("java.lang.System"),"out",new Type.Object("java.io.PrintStream")));
185         //mg.add(ILOAD_0);
186         mg.add(GETSTATIC,cg.fieldRef(me,"foo",Type.INT));
187         mg.add(INVOKEVIRTUAL,cg.methodRef(new Type.Object("java.io.PrintStream"),"println",Type.VOID, new Type[]{Type.INT}));
188         //mg.add(IINC,new int[]{0,1});
189         //mg.add(ILOAD_0);
190         mg.add(GETSTATIC,cg.fieldRef(me,"foo",Type.INT));
191         mg.addPushConst(1);
192         mg.add(IADD);
193         mg.add(DUP);
194         mg.add(PUTSTATIC,cg.fieldRef(me,"foo",Type.INT));       
195         mg.addPushConst(10);
196         mg.add(IF_ICMPLT,top);
197         mg.add(RETURN);
198         cg.dump("Test.class");
199     }*/
200 }