1ae97412ec6da7b154aa20a8190deac0fbfe8e2d
[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         for(int i=0;i<fields.size();i++) ((FieldGen)fields.elementAt(i)).finish();
78         
79         cp.seal();
80         
81         o.writeInt(0xcafebabe); // magic
82         o.writeShort(3); // minor_version
83         o.writeShort(45); // major_version
84         
85         o.writeShort(cp.size()); // constant_pool_count
86         cp.dump(o); // constant_pool
87         
88         o.writeShort(flags);
89         o.writeShort(cp.getIndex(thisType)); // this_class
90         o.writeShort(cp.getIndex(superType)); // super_class
91         
92         o.writeShort(interfaces.size()); // interfaces_count
93         for(int i=0;i<interfaces.size();i++) o.writeShort(cp.getIndex(interfaces.elementAt(i))); // interfaces
94         
95         o.writeShort(fields.size()); // fields_count
96         for(int i=0;i<fields.size();i++) ((FieldGen)fields.elementAt(i)).dump(o); // fields
97
98         o.writeShort(methods.size()); // methods_count
99         for(int i=0;i<methods.size();i++) ((MethodGen)methods.elementAt(i)).dump(o); // methods
100         
101         o.writeShort(attributes.size()); // attributes_count
102         attributes.dump(o); // attributes        
103     }
104     
105     public static class Exn extends RuntimeException {
106         public Exn(String s) { super(s); }
107     }
108     
109     public static abstract class FieldOrMethodRef {
110         Type.Object klass;
111         String name;
112         String descriptor;
113         
114         FieldOrMethodRef(Type.Object klass, String name, String descriptor) { this.klass = klass; this.name = name; this.descriptor = descriptor; }
115         FieldOrMethodRef(FieldOrMethodRef o) { this.klass = o.klass; this.name = o.name; this.descriptor = o.descriptor; }
116         public boolean equals(Object o_) {
117             if(!(o_ instanceof FieldOrMethodRef)) return false;
118             FieldOrMethodRef o = (FieldOrMethodRef) o_;
119             return o.klass.equals(klass) && o.name.equals(name) && o.descriptor.equals(descriptor);
120         }
121         public int hashCode() { return klass.hashCode() ^ name.hashCode() ^ descriptor.hashCode(); }
122     }
123     
124     static class AttrGen {
125         private final CPGen cp;
126         private final Hashtable ht = new Hashtable();
127         
128         public AttrGen(CPGen cp) {
129             this.cp = cp;
130         }
131         
132         public void add(String s, Object data) {
133             cp.addUtf8(s);
134             ht.put(s,data);
135         }
136         
137         public boolean contains(String s) { return ht.get(s) != null; }
138         
139         public int size() { return ht.size(); }
140         
141         public void dump(DataOutput o) throws IOException {
142             for(Enumeration e = ht.keys(); e.hasMoreElements();) {
143                 String name = (String) e.nextElement();
144                 Object val = ht.get(name);
145                 o.writeShort(cp.getUtf8Index(name));
146                 if(val instanceof byte[]) {
147                     byte[] buf = (byte[]) val;
148                     o.writeInt(buf.length);
149                     o.write(buf);
150                 } else if(val instanceof CPGen.Ent) {
151                     o.writeInt(2);
152                     o.writeShort(((CPGen.Ent)val).getIndex());
153                 } else {
154                     throw new Error("should never happen");
155                 }
156             }
157         }
158     }
159     
160     /*public static void main(String[] args) throws Exception {
161         Type.Object me = new Type.Object("Test");
162         ClassGen cg = new ClassGen("Test","java.lang.Object",ACC_PUBLIC|ACC_SUPER|ACC_FINAL);
163         FieldGen fg = cg.addField("foo",Type.INT,ACC_PUBLIC|ACC_STATIC);
164         
165         MethodGen mg = cg.addMethod("main",Type.VOID,new Type[]{Type.arrayType(Type.STRING)},ACC_STATIC|ACC_PUBLIC);
166         mg.setMaxLocals(1);
167         mg.addPushConst(0);
168         //mg.add(ISTORE_0);
169         mg.add(PUTSTATIC, fieldRef(me,"foo",Type.INT));
170         int top = mg.size();
171         mg.add(GETSTATIC,cg.fieldRef(new Type.Object("java.lang.System"),"out",new Type.Object("java.io.PrintStream")));
172         //mg.add(ILOAD_0);
173         mg.add(GETSTATIC,cg.fieldRef(me,"foo",Type.INT));
174         mg.add(INVOKEVIRTUAL,cg.methodRef(new Type.Object("java.io.PrintStream"),"println",Type.VOID, new Type[]{Type.INT}));
175         //mg.add(IINC,new int[]{0,1});
176         //mg.add(ILOAD_0);
177         mg.add(GETSTATIC,cg.fieldRef(me,"foo",Type.INT));
178         mg.addPushConst(1);
179         mg.add(IADD);
180         mg.add(DUP);
181         mg.add(PUTSTATIC,cg.fieldRef(me,"foo",Type.INT));       
182         mg.addPushConst(10);
183         mg.add(IF_ICMPLT,top);
184         mg.add(RETURN);
185         cg.dump("Test.class");
186     }*/
187 }