Skip to content

Commit

Permalink
Add ability to access language classes
Browse files Browse the repository at this point in the history
  • Loading branch information
fniephaus committed Feb 11, 2022
1 parent 6923ecb commit e1296bf
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 7 deletions.
3 changes: 3 additions & 0 deletions mx.trufflesqueak/mx_trufflesqueak.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
# Tweak Runtime
'-Xss64M', # Increase stack size (`-XX:ThreadStackSize=64M` not working)

# Make ReflectionUtils work
'--add-exports=java.base/jdk.internal.module=ALL-UNNAMED',

# Make Truffle.getRuntime() accessible for VM introspection
'--add-opens=jdk.internal.vm.compiler/org.graalvm.compiler.truffle.runtime=ALL-UNNAMED',
# Enable access to HostObject and others
Expand Down
15 changes: 9 additions & 6 deletions mx.trufflesqueak/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@
"java.management",
"jdk.unsupported",
],
"requiresConcealed" : {
"java.base" : ["jdk.internal.module"],
},
"checkstyleVersion": "8.36.1",
"jacoco": "include",
"javaCompliance": "11+",
Expand Down Expand Up @@ -182,18 +185,18 @@
"description": "TruffleSqueak engine",
"moduleInfo": {
"name": "de.hpi.swa.trufflesqueak",
"requiresConcealed": {
"org.graalvm.truffle": [
"com.oracle.truffle.api",
"com.oracle.truffle.api.instrumentation",
],
},
"exports": [
"de.hpi.swa.trufflesqueak to org.graalvm.truffle",
],
"requires": [
"jdk.unsupported" # sun.misc.Unsafe
],
"requiresConcealed": {
"org.graalvm.truffle": [
"com.oracle.truffle.api",
"com.oracle.truffle.api.instrumentation",
],
},
},
"dependencies": [
"de.hpi.swa.trufflesqueak",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3021,6 +3021,7 @@ JavaTest>>testJavaLocalDate=passing
JavaTest>>testJavaLocalTime=passing
JavaTest>>testJavaString=passing
JavaTest>>testJavaZoneId=passing
JavaTest>>testLanguageClass=passing
JPEGReadWriter2PluginTest>>testPluginPresent=passing
JPEGReadWriter2PluginTest>>testPrimJPEGReadImage=passing
# Expected form has an off-by-one pixel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
package de.hpi.swa.trufflesqueak.nodes.plugins;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.ByteOrder;
import java.util.List;
import java.util.Map;

import org.graalvm.polyglot.Engine;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
Expand Down Expand Up @@ -68,6 +72,7 @@
import de.hpi.swa.trufflesqueak.util.ArrayUtils;
import de.hpi.swa.trufflesqueak.util.LogUtils;
import de.hpi.swa.trufflesqueak.util.MiscUtils;
import de.hpi.swa.trufflesqueak.util.ReflectionUtils;
import de.hpi.swa.trufflesqueak.util.UnsafeUtils;

public final class PolyglotPlugin extends AbstractPrimitiveFactoryHolder {
Expand Down Expand Up @@ -2056,6 +2061,28 @@ protected final Object doLookupHostSymbol(@SuppressWarnings("unused") final Obje
}
}

@GenerateNodeFactory
@SqueakPrimitive(names = "primitiveLoadLanguageClass")
protected abstract static class PrimLoadLanguageClassNode extends AbstractPrimitiveNode implements BinaryPrimitiveFallback {
private static final Method LOAD_LANGUAGE_CLASS = ReflectionUtils.lookupMethod(Engine.class, "loadLanguageClass", String.class);

@TruffleBoundary
@Specialization(guards = {"value.isByteType()"})
protected final Object doLoadLanguageClass(@SuppressWarnings("unused") final Object receiver, final NativeObject value) {
final String symbolName = value.asStringUnsafe();
try {
final Class<?> languageSymbol = (Class<?>) LOAD_LANGUAGE_CLASS.invoke(null, symbolName);
if (languageSymbol != null) {
return getContext().env.asHostSymbol(languageSymbol);
} else {
return NilObject.SINGLETON;
}
} catch (final RuntimeException | IllegalAccessException | InvocationTargetException e) {
throw primitiveFailedInInterpreterCapturing(e);
}
}
}

@GenerateNodeFactory
@SqueakPrimitive(names = "primitiveIsHostFunction")
protected abstract static class PrimIsHostFunctionNode extends AbstractPrimitiveNode {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2022 Software Architecture Group, Hasso Plattner Institute
* Copyright (c) 2022 Oracle and/or its affiliates
*
* Licensed under the MIT License.
*/
package de.hpi.swa.trufflesqueak.util;

import java.lang.reflect.Method;

import jdk.internal.module.Modules;

public final class ReflectionUtils {
public static Method lookupMethod(final Class<?> declaringClass, final String methodName, final Class<?>... parameterTypes) {
try {
final Method result = declaringClass.getDeclaredMethod(methodName, parameterTypes);
openModule(declaringClass);
result.setAccessible(true);
return result;
} catch (final ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}

/**
* Ensure that this class is allowed to call setAccessible for an element of the provided
* declaring class.
*/
private static void openModule(final Class<?> declaringClass) {
openModuleByClass(declaringClass, ReflectionUtils.class);
}

private static void openModuleByClass(final Class<?> declaringClass, final Class<?> accessingClass) {
final Module declaringModule = declaringClass.getModule();
final String packageName = declaringClass.getPackageName();
Module namedAccessingModule = null;
if (accessingClass != null) {
final Module accessingModule = accessingClass.getModule();
if (accessingModule.isNamed()) {
namedAccessingModule = accessingModule;
}
}
if (namedAccessingModule != null ? declaringModule.isOpen(packageName, namedAccessingModule) : declaringModule.isOpen(packageName)) {
return;
}
if (namedAccessingModule != null) {
Modules.addOpens(declaringModule, packageName, namedAccessingModule);
} else {
Modules.addOpensToAllUnnamed(declaringModule, packageName);
}
}
}

1 comment on commit e1296bf

@TruffleSqueak-Bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performance Report (e1296bf)

Benchmarks ran on graalvm-ce-java11-22.0.0.2.

Steady (after 50 iterations)

Benchmark Name Min Geomean Median Mean Max Total (ms) Total (min)
Bounce 183 275 187.61 184 187.09 37522 0.63
DeltaBlue 261 526 377.36 377 376.48 75471 1.26
Havlak 1739 1902 1810.8 1822 1810.57 362160 6.04
Json 738 780 744.84 743 744.8 148967 2.48
List 889 987 895.55 894 895.5 179110 2.99
Mandelbrot 144 158 145.04 145 145.04 29009 0.48
NBody 583 647 590.54 586.5 590.49 118109 1.97
Permute 231 265 233.95 232 233.89 46789 0.78
Queens 256 308 263.75 258 263.46 52749 0.88
Richards 1080 1124 1085.69 1082 1085.66 217138 3.62
Sieve 214 259 217.85 215 217.61 43570 0.73
Storage 288 329 293.45 289 293.34 58690 0.98
Towers 347 538 350.37 348 350.12 70074 1.17
6953 8098 7196.79 7175.5 7194.06 1439358 23.99

e1296bf-2-steady.svg

Warmup (first 50 iterations)

e1296bf-3-warmup.svg

Please sign in to comment.