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 public static Class compile(Seekable data) throws IOException, Compiler.Exn { return compile(data,null); }
14 public static Class compile(Seekable data, String extraoptions) throws IOException, Compiler.Exn { return compile(data,extraoptions,null); }
16 public static Class compile(Seekable data, String extraoptions, String sourceName) throws IOException, Compiler.Exn {
17 String className = "nestedvm.runtimecompiled";
20 bytecode = runCompiler(data,className,extraoptions,sourceName,null);
21 } catch(Compiler.Exn e) {
22 if(e.getMessage() != null || e.getMessage().indexOf("constant pool full") != -1)
23 bytecode = runCompiler(data,className,extraoptions,sourceName,"lessconstants");
27 return new SingleClassLoader().fromBytes(className,bytecode);
30 private static byte[] runCompiler(Seekable data, String name, String options, String sourceName, String moreOptions) throws IOException, Compiler.Exn {
31 ByteArrayOutputStream baos = new ByteArrayOutputStream();
34 ClassFileCompiler c = new ClassFileCompiler(data,name,baos);
35 c.parseOptions("nosupportcall,maxinsnpermethod=256");
36 c.setSource(sourceName);
37 if(options != null) c.parseOptions(options);
38 if(moreOptions != null) c.parseOptions(moreOptions);
45 return baos.toByteArray();
48 private static class SingleClassLoader extends ClassLoader {
49 public Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
50 //System.err.println(this + ": loadClass(\"" + name + "," + resolve + ");");
51 return super.loadClass(name,resolve);
53 public Class fromBytes(String name, byte[] b) { return fromBytes(name,b,0,b.length); }
54 public Class fromBytes(String name, byte[] b, int off, int len) {
55 Class c = super.defineClass(name,b,off,len);
61 public static void main(String[] args) throws Exception {
62 if(args.length == 0) {
63 System.err.println("Usage: RuntimeCompiler mipsbinary");
66 UnixRuntime r = (UnixRuntime) compile(new Seekable.File(args[0]),"unixruntime").newInstance();
67 System.err.println("Instansiated: "+ r);
68 System.exit(UnixRuntime.runAndExec(r,args));
71 private RuntimeCompiler() { }