1 package org.ibex.nestedvm;
5 import org.ibex.nestedvm.util.*;
7 // FEATURE: This need a lot of work to support binaries spanned across many classes
8 public class RuntimeCompiler {
9 // FEATURE: Do we need to periodicly create a new classloader to allow old clases to be GCed?
10 private static SingleClassLoader singleClassLoader = new SingleClassLoader();
11 // FEATURE: Is it ok if this overflows?
12 private static long nextID = 1;
13 private static synchronized String uniqueID() { return Long.toString(nextID++); }
15 public static Class compile(Seekable data) throws IOException, Compiler.Exn {
16 String className = "nextedvm.runtimecompiled_" + uniqueID();
17 System.err.println("RuntimeCompiler: Building " + className);
18 ByteArrayOutputStream baos = new ByteArrayOutputStream();
19 ClassFileCompiler c = new ClassFileCompiler(data,className,baos);
20 // FEATURE: make this Optional, pass options on compile arguments
21 c.parseOptions("unixruntime");
24 byte[] bytecode = baos.toByteArray();
25 return singleClassLoader.fromBytes(className,bytecode);
28 private static class SingleClassLoader extends ClassLoader {
29 public Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
30 System.err.println(this + ": loadClass(\"" + name + "," + resolve + ");");
31 return super.loadClass(name,resolve);
33 public Class fromBytes(String name, byte[] b) { return fromBytes(name,b,0,b.length); }
34 public Class fromBytes(String name, byte[] b, int off, int len) {
35 Class c = super.defineClass(name,b,off,len);
41 public static void main(String[] args) throws Exception {
42 if(args.length == 0) {
43 System.err.println("Usage: RuntimeCompiler mipsbinary");
46 UnixRuntime r = (UnixRuntime) compile(new Seekable.File(args[0])).newInstance();
47 System.err.println("Instansiated: "+ r);
48 System.exit(r.run(args));
51 private RuntimeCompiler() { }