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