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.
5 package org.ibex.nestedvm;
9 import org.ibex.nestedvm.util.*;
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;
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 {
19 synchronized(RuntimeCompiler.class) {
20 if(nextID == 32 || singleClassLoader == null) {
21 singleClassLoader = new SingleClassLoader();
26 String className = "nestedvm.runtimecompiled_" + id;
27 System.err.println("RuntimeCompiler: Building " + className);
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");
37 return singleClassLoader.fromBytes(className,bytecode);
40 private static byte[] runCompiler(Seekable data, String name, String options, String moreOptions) throws IOException, Compiler.Exn {
41 ByteArrayOutputStream baos = new ByteArrayOutputStream();
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);
54 return baos.toByteArray();
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);
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);
70 public static void main(String[] args) throws Exception {
71 if(args.length == 0) {
72 System.err.println("Usage: RuntimeCompiler mipsbinary");
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));
80 private RuntimeCompiler() { }