changeset 5628:65bf69eb147c

Merge.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Thu, 14 Jun 2012 18:03:43 +0200
parents e1b29c516354 (diff) f8eb2cb76a2f (current diff)
children 26a060cc58ca
files graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/phases/SnippetIntrinsificationPhase.java graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/virtual/VirtualObjectFieldNode.java graal/com.oracle.graal.snippets/src/com/oracle/graal/snippets/Snippets.java
diffstat 6 files changed, 199 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.api/src/com/oracle/graal/api/Graal.java	Thu Jun 14 16:37:40 2012 +0200
+++ b/graal/com.oracle.graal.api/src/com/oracle/graal/api/Graal.java	Thu Jun 14 18:03:43 2012 +0200
@@ -22,7 +22,6 @@
  */
 package com.oracle.graal.api;
 
-
 public class Graal {
 
     private static GraalRuntime runtime;
@@ -49,4 +48,12 @@
             };
         }
     }
+
+    public static <T> T getRequiredCapability(Class<T> clazz) {
+        T t = getRuntime().getCapability(clazz);
+        if (t == null) {
+            throw new IllegalAccessError("Runtime does not expose required capability " + clazz.getName());
+        }
+        return t;
+    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.graal.boot.test/src/com/oracle/graal/boot/BootImageClassLoaderTest.java	Thu Jun 14 18:03:43 2012 +0200
@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.graal.boot;
+
+import java.lang.reflect.*;
+
+import org.junit.*;
+
+
+public class BootImageClassLoaderTest {
+
+    @Test
+    public void test() throws ClassNotFoundException, IllegalArgumentException, IllegalAccessException, SecurityException {
+
+        TestClassB.x = 1;
+        BootImageClassLoader l = new BootImageClassLoader();
+
+        // Assert that the class definition is really duplicated.
+        Class<?> bClass = Class.forName(TestClassB.class.getCanonicalName(), true, l);
+        Assert.assertNotSame(TestClassB.class, bClass);
+
+        // Assert that the class definition is not duplicated more than once.
+        Assert.assertSame(bClass, l.convert(TestClassB.class));
+
+        // Set field x such that it is used by the subsequent static initializer for TestClassA.
+        Field bField = bClass.getFields()[0];
+        bField.setAccessible(true);
+        bField.set(null, 2);
+
+        // Assert that the class definition is duplicated.
+        Class<?> aClass = l.convert(TestClassA.class);
+        Assert.assertNotSame(TestClassA.class, aClass);
+
+        // Assert that the original version of TestClassA was initialized correctly.
+        Assert.assertEquals(1, TestClassA.x);
+
+        // Assert that the duplicated version of TestClassA was initialized correctly.
+        Field aField = aClass.getFields()[0];
+        aField.setAccessible(true);
+        Assert.assertEquals(2, aField.getInt(null));
+
+        // Assert that system classes are not duplicated.
+        Assert.assertSame(Object.class, l.convert(Object.class));
+        Assert.assertSame(Object.class, Class.forName(Object.class.getCanonicalName(), true, l));
+    }
+
+}
+
+class TestClassA {
+    public static int x;
+
+    static {
+        x = TestClassB.x;
+    }
+}
+
+class TestClassB {
+    public static int x;
+}
--- a/graal/com.oracle.graal.boot.test/src/com/oracle/graal/boot/HelloWorldTest.java	Thu Jun 14 16:37:40 2012 +0200
+++ b/graal/com.oracle.graal.boot.test/src/com/oracle/graal/boot/HelloWorldTest.java	Thu Jun 14 18:03:43 2012 +0200
@@ -22,7 +22,13 @@
  */
 package com.oracle.graal.boot;
 
+import com.oracle.graal.boot.test.helloworld.*;
+
 
 public class HelloWorldTest {
-
+    public static void main(String[] args) {
+        BootImageGenerator generator = new BootImageGenerator();
+        generator.addEntryMethod(HelloWorldTestProgram.class, "main", String[].class);
+        generator.logState();
+    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.graal.boot/src/com/oracle/graal/boot/BootImageClassLoader.java	Thu Jun 14 18:03:43 2012 +0200
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.graal.boot;
+
+import java.io.*;
+
+public class BootImageClassLoader extends ClassLoader {
+
+    @Override
+    protected java.lang.Class< ? > loadClass(String name, boolean resolve) throws ClassNotFoundException {
+        synchronized (getClassLoadingLock(name)) {
+            Class< ? > result = findLoadedClass(name);
+            if (result == null) {
+                result = super.loadClass(name, resolve);
+                assert result.getName().equals(name);
+                return duplicate(result);
+            }
+            return result;
+        }
+    }
+
+    private Class< ? > duplicate(Class< ? > result) {
+        // This is a class in the bootclasspath => share.
+        if (result.getClassLoader() == null) {
+            return result;
+        }
+
+        // Duplicate class definition.
+        InputStream inputStream = result.getClassLoader().getResourceAsStream(result.getName().replace('.', '/').concat(".class"));
+        try {
+            byte[] byteCodes = new byte[inputStream.available()];
+            inputStream.read(byteCodes);
+            return this.defineClass(result.getName(), byteCodes, 0, byteCodes.length);
+        } catch (IOException e) {
+            throw new RuntimeException("Could not access class bytes for " + result.getName());
+        }
+    }
+
+    public Class< ? > convert(Class< ? > clazz) {
+        synchronized (getClassLoadingLock(clazz.getCanonicalName())) {
+            // This class has this class loader => no conversion necessary.
+            if (clazz.getClassLoader() == this) {
+                return clazz;
+            }
+
+            Class< ? > thisClazz = findLoadedClass(clazz.getName());
+            if (thisClazz != null) {
+                return thisClazz;
+            }
+
+            return duplicate(clazz);
+        }
+    }
+}
--- a/graal/com.oracle.graal.boot/src/com/oracle/graal/boot/BootImageGenerator.java	Thu Jun 14 16:37:40 2012 +0200
+++ b/graal/com.oracle.graal.boot/src/com/oracle/graal/boot/BootImageGenerator.java	Thu Jun 14 18:03:43 2012 +0200
@@ -22,7 +22,37 @@
  */
 package com.oracle.graal.boot;
 
+import java.lang.reflect.*;
+
+import com.oracle.graal.api.*;
+import com.oracle.graal.api.meta.*;
+import com.oracle.graal.debug.*;
+
 
 public class BootImageGenerator {
 
+    private final BootImageClassLoader classLoader = new BootImageClassLoader();
+    private final MetaAccessProvider metaAccess = Graal.getRequiredCapability(MetaAccessProvider.class);
+
+    public void addEntryMethod(Class<?> clazz, String name, Class<?> ... parameterTypes) {
+        Class<?> convertedClass = classLoader.convert(clazz);
+        Method method;
+        try {
+            method = convertedClass.getDeclaredMethod(name, parameterTypes);
+        } catch (NoSuchMethodException | SecurityException e) {
+            throw new RuntimeException("Could not find method " + name + " with parameter types " + parameterTypes + " in class " + convertedClass.getCanonicalName());
+        }
+        Debug.log("Adding method %s.%s to the boot image", method.getClass().getName(), method.getName());
+        addEntryMethod(metaAccess.getResolvedJavaMethod(method));
+    }
+
+
+    private void addEntryMethod(ResolvedJavaMethod javaMethod) {
+
+    }
+
+
+    public void logState() {
+        Debug.log("State");
+    }
 }
--- a/mx/projects	Thu Jun 14 16:37:40 2012 +0200
+++ b/mx/projects	Thu Jun 14 18:03:43 2012 +0200
@@ -147,7 +147,7 @@
 # graal.boot
 project@com.oracle.graal.boot@subDir=graal
 project@com.oracle.graal.boot@sourceDirs=src
-project@com.oracle.graal.boot@dependencies=com.oracle.graal.compiler
+project@com.oracle.graal.boot@dependencies=com.oracle.graal.compiler,com.oracle.graal.api
 project@com.oracle.graal.boot@checkstyle=com.oracle.graal.graph
 project@com.oracle.graal.boot@javaCompliance=1.7