1 package org.ibex.nestedvm;
5 import org.ibex.nestedvm.util.*;
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;
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 {
15 synchronized(RuntimeCompiler.class) {
16 if(nextID == 32 || singleClassLoader == null) {
17 singleClassLoader = new SingleClassLoader();
22 String className = "nestedvm.runtimecompiled_" + id;
23 System.err.println("RuntimeCompiler: Building " + className);
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");
33 return singleClassLoader.fromBytes(className,bytecode);
36 private static byte[] runCompiler(Seekable data, String name, String options, String moreOptions) throws IOException, Compiler.Exn {
37 ByteArrayOutputStream baos = new ByteArrayOutputStream();
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);
50 return baos.toByteArray();
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);
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);
66 public static void main(String[] args) throws Exception {
67 if(args.length == 0) {
68 System.err.println("Usage: RuntimeCompiler mipsbinary");
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));
76 private RuntimeCompiler() { }