443c217f7aaf8273ee5ea8e5dd18114b411c08bf
[nestedvm.git] / src / org / ibex / nestedvm / RuntimeCompiler.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.nestedvm;
6
7 import java.io.*;
8
9 import org.ibex.nestedvm.util.*;
10
11 // This need a lot of work to support binaries spanned across many classes
12 public class RuntimeCompiler {  
13     private static SingleClassLoader singleClassLoader;
14     private static int nextID;
15     
16     public static Class compile(Seekable data) throws IOException, Compiler.Exn { return compile(data,null); }
17     public static Class compile(Seekable data, String extraoptions) throws IOException, Compiler.Exn {
18         int id;
19         synchronized(RuntimeCompiler.class) {
20             if(nextID == 32 || singleClassLoader == null) {
21                 singleClassLoader = new SingleClassLoader();
22                 nextID = 0;
23             }
24             id = nextID++;
25         }
26         String className = "nestedvm.runtimecompiled_" + id;
27         System.err.println("RuntimeCompiler: Building " + className);
28         byte[] bytecode;
29         try {
30             bytecode = runCompiler(data,className,extraoptions,null);
31         } catch(Compiler.Exn e) {
32             if(e.getMessage() != null || e.getMessage().indexOf("constant pool full")  != -1)
33                 bytecode = runCompiler(data,className,extraoptions,"lessconstants");
34             else
35                 throw e;
36         }
37         return singleClassLoader.fromBytes(className,bytecode);
38     }
39     
40     private static byte[] runCompiler(Seekable data, String name, String options, String moreOptions) throws IOException, Compiler.Exn {
41         ByteArrayOutputStream baos = new ByteArrayOutputStream();
42         
43         try {
44             ClassFileCompiler c = new ClassFileCompiler(data,name,baos);
45             c.parseOptions("nosupportcall,maxinsnpermethod=256");
46             if(options != null) c.parseOptions(options);
47             if(moreOptions != null) c.parseOptions(moreOptions);
48             c.go();
49         } finally {
50             data.seek(0);
51         }
52         
53         baos.close();
54         return baos.toByteArray();        
55     }
56     
57     private static class SingleClassLoader extends ClassLoader {
58         public Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
59             //System.err.println(this + ": loadClass(\"" + name + "," + resolve + ");");
60             return super.loadClass(name,resolve);
61         }
62         public Class fromBytes(String name, byte[] b) { return fromBytes(name,b,0,b.length); }
63         public Class fromBytes(String name, byte[] b, int off, int len) {
64             Class c = super.defineClass(name,b,off,len);
65             resolveClass(c);
66             return c;
67         }
68     }
69     
70     public static void main(String[] args) throws Exception {
71         if(args.length == 0) {
72             System.err.println("Usage: RuntimeCompiler mipsbinary");
73             System.exit(1);
74         }
75         UnixRuntime r = (UnixRuntime) compile(new Seekable.File(args[0]),"unixruntime").newInstance();
76         System.err.println("Instansiated: "+ r);
77         System.exit(UnixRuntime.runAndExec(r,args));
78     }
79     
80     private RuntimeCompiler() { }
81 }