runtimecompiler
authorbrian <brian@brianweb.net>
Mon, 3 May 2004 06:46:29 +0000 (23:46 -0700)
committerbrian <brian@brianweb.net>
Mon, 3 May 2004 06:46:29 +0000 (23:46 -0700)
darcs-hash:20040503064629-24bed-227423d4eee428a0eaee0d6a36e15be9e5040df3.gz

src/org/ibex/nestedvm/RuntimeCompiler.java [new file with mode: 0644]

diff --git a/src/org/ibex/nestedvm/RuntimeCompiler.java b/src/org/ibex/nestedvm/RuntimeCompiler.java
new file mode 100644 (file)
index 0000000..2aa4305
--- /dev/null
@@ -0,0 +1,51 @@
+package org.ibex.nestedvm;
+
+import java.io.*;
+
+import org.ibex.nestedvm.util.*;
+
+// FEATURE: This need a lot of work to support binaries spanned across many classes
+public class RuntimeCompiler {          
+    private static final SingleClassLoader singleClassLoader = new SingleClassLoader();
+    // FEATURE: Is it ok if this overflows?
+    private static long nextID = 1;
+    private static synchronized String uniqueID() { return Long.toString(nextID++); }
+    
+    public static Class compile(Seekable data) throws IOException, Compiler.Exn {
+        String className = "nextedvm.runtimecompiled_" + uniqueID();
+        System.err.println("RuntimeCompiler: Building " + className);
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ClassFileCompiler c = new ClassFileCompiler(data,className,baos);
+        // FEATURE: make this Optional, pass options on compile arguments
+        c.parseOptions("unixruntime");
+        c.go();
+        baos.close();
+        byte[] bytecode = baos.toByteArray();
+        return singleClassLoader.fromBytes(className,bytecode);
+    }
+    
+    private static class SingleClassLoader extends ClassLoader {
+        public Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
+                   System.err.println(this + ": loadClass(\"" + name + "," + resolve + ");");
+            return super.loadClass(name,resolve);
+        }
+        public Class fromBytes(String name, byte[] b) { return fromBytes(name,b,0,b.length); }
+        public Class fromBytes(String name, byte[] b, int off, int len) {
+            Class c = super.defineClass(name,b,off,len);
+            resolveClass(c);
+            return c;
+        }
+    }
+    
+    public static void main(String[] args) throws Exception {
+           if(args.length == 0) {
+                   System.err.println("Usage: RuntimeCompiler mipsbinary");
+            System.exit(1);
+        }
+        UnixRuntime r = (UnixRuntime) compile(new Seekable.File(args[0])).newInstance();
+        System.err.println("Instansiated: "+ r);
+        System.exit(r.run(args));
+    }
+    
+    private RuntimeCompiler() { }
+}