use of com.oracle.svm.hosted.c.NativeLibraries in project graal by oracle.
the class NativeImageGenerator method processNativeLibraryImports.
@SuppressWarnings("try")
private NativeLibraries processNativeLibraryImports(MetaAccessProvider metaAccess, AnalysisConstantReflectionProvider aConstantReflection, SnippetReflectionProvider snippetReflection) {
CAnnotationProcessorCache.initialize();
try (StopTimer t = new Timer("(cap)").start()) {
NativeLibraries nativeLibs = new NativeLibraries(aConstantReflection, metaAccess, snippetReflection, ConfigurationValues.getTarget());
for (Method method : loader.findAnnotatedMethods(CConstant.class)) {
nativeLibs.loadJavaMethod(metaAccess.lookupJavaMethod(method));
}
for (Class<?> clazz : loader.findAnnotatedClasses(CStruct.class)) {
nativeLibs.loadJavaType(metaAccess.lookupJavaType(clazz));
}
for (Class<?> clazz : loader.findAnnotatedClasses(RawStructure.class)) {
nativeLibs.loadJavaType(metaAccess.lookupJavaType(clazz));
}
for (Class<?> clazz : loader.findAnnotatedClasses(CPointerTo.class)) {
nativeLibs.loadJavaType(metaAccess.lookupJavaType(clazz));
}
for (Class<?> clazz : loader.findAnnotatedClasses(CEnum.class)) {
nativeLibs.loadJavaType(metaAccess.lookupJavaType(clazz));
}
for (CLibrary library : loader.findAnnotations(CLibrary.class)) {
nativeLibs.addLibrary(library.value());
}
nativeLibs.finish(tempDirectory());
nativeLibs.reportErrors();
return nativeLibs;
}
}
use of com.oracle.svm.hosted.c.NativeLibraries in project graal by oracle.
the class CEntryPointCallStubMethod method buildGraph.
@Override
public StructuredGraph buildGraph(DebugContext debug, ResolvedJavaMethod method, HostedProviders providers, Purpose purpose) {
UniverseMetaAccess metaAccess = (UniverseMetaAccess) providers.getMetaAccess();
NativeLibraries nativeLibraries = CEntryPointCallStubSupport.singleton().getNativeLibraries();
HostedGraphKit kit = new HostedGraphKit(debug, providers, method);
StructuredGraph graph = kit.getGraph();
JavaType[] parameterTypes = method.toParameterTypes();
JavaType[] parameterLoadTypes = Arrays.copyOf(parameterTypes, parameterTypes.length);
EnumInfo[] parameterEnumInfos;
parameterEnumInfos = adaptParameterTypes(providers, nativeLibraries, kit, parameterTypes, parameterLoadTypes, purpose);
ValueNode[] args = kit.loadArguments(parameterLoadTypes).toArray(new ValueNode[0]);
InvokeNode prologueInvoke = generatePrologue(providers, kit, parameterLoadTypes, args);
adaptArgumentValues(providers, kit, parameterTypes, parameterEnumInfos, args);
ResolvedJavaMethod unwrappedTargetMethod = targetMethod;
while (unwrappedTargetMethod instanceof WrappedJavaMethod) {
unwrappedTargetMethod = ((WrappedJavaMethod) unwrappedTargetMethod).getWrapped();
}
ResolvedJavaMethod universeTargetMethod = lookupMethodInUniverse(metaAccess, unwrappedTargetMethod);
int invokeBci = kit.bci();
int exceptionEdgeBci = kit.bci();
// Also support non-static test methods (they are not allowed to use the receiver)
InvokeKind invokeKind = universeTargetMethod.isStatic() ? InvokeKind.Static : InvokeKind.Special;
ValueNode[] invokeArgs = args;
if (invokeKind != InvokeKind.Static) {
invokeArgs = new ValueNode[args.length + 1];
invokeArgs[0] = kit.createObject(null);
System.arraycopy(args, 0, invokeArgs, 1, args.length);
}
InvokeWithExceptionNode invoke = kit.startInvokeWithException(universeTargetMethod, invokeKind, kit.getFrameState(), invokeBci, exceptionEdgeBci, invokeArgs);
kit.exceptionPart();
ExceptionObjectNode exception = kit.exceptionObject();
generateExceptionHandler(providers, kit, exception, invoke.getStackKind());
kit.endInvokeWithException();
ValueNode returnValue = adaptReturnValue(method, providers, purpose, metaAccess, nativeLibraries, kit, invoke);
InvokeNode epilogueInvoke = generateEpilogue(providers, kit);
kit.createReturn(returnValue, returnValue.getStackKind());
inlinePrologueAndEpilogue(kit, prologueInvoke, epilogueInvoke, invoke.getStackKind());
assert graph.verify();
return graph;
}
use of com.oracle.svm.hosted.c.NativeLibraries in project graal by oracle.
the class CEntryPointCallStubMethod method adaptParameterTypes.
private EnumInfo[] adaptParameterTypes(HostedProviders providers, NativeLibraries nativeLibraries, HostedGraphKit kit, JavaType[] parameterTypes, JavaType[] parameterLoadTypes, Purpose purpose) {
EnumInfo[] parameterEnumInfos = null;
for (int i = 0; i < parameterTypes.length; i++) {
if (!parameterTypes[i].getJavaKind().isPrimitive() && !providers.getWordTypes().isWord(parameterTypes[i])) {
ElementInfo typeInfo = nativeLibraries.findElementInfo(parameterTypes[i]);
if (typeInfo instanceof EnumInfo) {
UserError.guarantee(typeInfo.getChildren().stream().anyMatch(EnumLookupInfo.class::isInstance), "Enum class " + parameterTypes[i].toJavaName() + " needs a method that is annotated with @" + CEnumLookup.class + " because it is used as a parameter of an entry point method: " + targetMethod.format("%H.%n(%p)"));
if (parameterEnumInfos == null) {
parameterEnumInfos = new EnumInfo[parameterTypes.length];
}
parameterEnumInfos[i] = (EnumInfo) typeInfo;
parameterLoadTypes[i] = providers.getMetaAccess().lookupJavaType(cEnumParameterKind.toJavaClass());
final int parameterIndex = i;
FrameState initialState = kit.getGraph().start().stateAfter();
Iterator<ValueNode> matchingNodes = initialState.values().filter(node -> ((ParameterNode) node).index() == parameterIndex).iterator();
ValueNode parameterNode = matchingNodes.next();
assert !matchingNodes.hasNext() && parameterNode.usages().filter(n -> n != initialState).isEmpty();
parameterNode.setStamp(StampFactory.forKind(cEnumParameterKind));
} else if (purpose != Purpose.ANALYSIS) {
// for analysis test cases: abort only during compilation
throw UserError.abort("Entry point method parameter types are restricted to primitive types, word types and enumerations (@" + CEnum.class.getSimpleName() + "): " + targetMethod.format("%H.%n(%p)"));
}
}
}
return parameterEnumInfos;
}
use of com.oracle.svm.hosted.c.NativeLibraries in project graal by oracle.
the class CFunctionCallStubMethod method buildGraph.
@Override
public StructuredGraph buildGraph(DebugContext debug, ResolvedJavaMethod method, HostedProviders providers, Purpose purpose) {
NativeLibraries nativeLibraries = CEntryPointCallStubSupport.singleton().getNativeLibraries();
CFunction annotation = method.getAnnotation(CFunction.class);
boolean needsTransition = annotation.transition() != Transition.NO_TRANSITION;
if (purpose == Purpose.PREPARE_RUNTIME_COMPILATION && needsTransition) {
/*
* C function calls that need a transition cannot be runtime compiled (and cannot be
* inlined during runtime compilation). Deoptimization could be required while we are
* blocked in native code, which means the deoptimization stub would need to do the
* native-to-Java transition.
*/
ImageSingletons.lookup(CFunctionFeature.class).warnRuntimeCompilationReachableCFunctionWithTransition(this);
return null;
}
boolean deoptimizationTarget = method instanceof SharedMethod && ((SharedMethod) method).isDeoptTarget();
HostedGraphKit kit = new HostedGraphKit(debug, providers, method);
FrameStateBuilder state = kit.getFrameState();
ValueNode callAddress = kit.unique(new CGlobalDataLoadAddressNode(linkage));
List<ValueNode> arguments = kit.loadArguments(method.toParameterTypes());
Signature signature = adaptSignatureAndConvertArguments(method, providers, nativeLibraries, kit, method.getSignature(), arguments);
state.clearLocals();
ValueNode returnValue = kit.createCFunctionCall(callAddress, method, arguments, signature, needsTransition, deoptimizationTarget);
returnValue = adaptReturnValue(method, providers, nativeLibraries, kit, returnValue);
kit.createReturn(returnValue, signature.getReturnKind());
assert kit.getGraph().verify();
return kit.getGraph();
}
use of com.oracle.svm.hosted.c.NativeLibraries in project graal by oracle.
the class JNIFunctionTablesFeature method beforeAnalysis.
@Override
public void beforeAnalysis(BeforeAnalysisAccess arg) {
BeforeAnalysisAccessImpl access = (BeforeAnalysisAccessImpl) arg;
AnalysisMetaAccess metaAccess = access.getMetaAccess();
JNIFunctionTables.create();
NativeLibraries nativeLibraries = access.getNativeLibraries();
AnalysisType invokeInterface = metaAccess.lookupJavaType(JNIInvokeInterface.class);
invokeInterfaceMetadata = (StructInfo) nativeLibraries.findElementInfo(invokeInterface);
AnalysisType functionTable = metaAccess.lookupJavaType(JNINativeInterface.class);
functionTableMetadata = (StructInfo) nativeLibraries.findElementInfo(functionTable);
// Manually add functions as entry points so this is only done when JNI features are enabled
AnalysisType invokes = metaAccess.lookupJavaType(JNIInvocationInterface.class);
AnalysisType exports = metaAccess.lookupJavaType(JNIInvocationInterface.Exports.class);
AnalysisType functions = metaAccess.lookupJavaType(JNIFunctions.class);
Stream<AnalysisMethod> analysisMethods = Stream.of(invokes, functions, exports).flatMap(t -> Stream.of(t.getDeclaredMethods()));
Stream<AnalysisMethod> unimplementedMethods = Stream.of((AnalysisMethod) getSingleMethod(metaAccess, UnimplementedWithJNIEnvArgument.class), (AnalysisMethod) getSingleMethod(metaAccess, UnimplementedWithJavaVMArgument.class));
Stream.concat(analysisMethods, unimplementedMethods).forEach(method -> {
CEntryPoint annotation = method.getAnnotation(CEntryPoint.class);
assert annotation != null : "only entry points allowed in class";
CEntryPointCallStubSupport.singleton().registerStubForMethod(method, () -> CEntryPointData.create(method));
});
ArrayList<ResolvedJavaMethod> generated = new ArrayList<>();
MetaAccessProvider wrappedMetaAccess = metaAccess.getWrapped();
ResolvedJavaType generatedMethodClass = wrappedMetaAccess.lookupJavaType(JNIFunctions.class);
ConstantPool constantPool = generatedMethodClass.getDeclaredMethods()[0].getConstantPool();
// Generate JNI field accessors
EnumSet<JavaKind> fldKinds = jniKinds.clone();
fldKinds.remove(JavaKind.Void);
for (JavaKind kind : fldKinds) {
boolean[] trueFalse = { true, false };
for (boolean isSetter : trueFalse) {
for (boolean isStatic : trueFalse) {
JNIFieldAccessorMethod method = new JNIFieldAccessorMethod(kind, isSetter, isStatic, generatedMethodClass, constantPool, wrappedMetaAccess);
AnalysisMethod analysisMethod = access.getUniverse().lookup(method);
access.getBigBang().addRootMethod(analysisMethod).registerAsEntryPoint(method.createEntryPointData());
generated.add(method);
}
}
}
// Generate JNI primitive array operations
EnumSet<JavaKind> primitiveArrayKinds = jniKinds.clone();
primitiveArrayKinds.remove(JavaKind.Void);
primitiveArrayKinds.remove(JavaKind.Object);
for (JavaKind kind : primitiveArrayKinds) {
for (Operation op : Operation.values()) {
JNIPrimitiveArrayOperationMethod method = new JNIPrimitiveArrayOperationMethod(kind, op, generatedMethodClass, constantPool, wrappedMetaAccess);
AnalysisMethod analysisMethod = access.getUniverse().lookup(method);
access.getBigBang().addRootMethod(analysisMethod).registerAsEntryPoint(method.createEntryPointData());
generated.add(method);
}
}
generatedMethods = generated.toArray(new ResolvedJavaMethod[0]);
}
Aggregations