4040 */
4141package com .oracle .graal .python .processor ;
4242
43+ import static com .oracle .graal .python .processor .NativeDowncallMethodHandleGenerator .argName ;
44+
4345import java .io .IOException ;
4446import java .nio .file .Files ;
4547import java .nio .file .Path ;
8688import com .oracle .graal .python .annotations .CApiFields ;
8789import com .oracle .graal .python .annotations .CApiStructs ;
8890import com .oracle .graal .python .annotations .CApiUpcallTarget ;
91+ import com .oracle .graal .python .annotations .NativeSimpleType ;
8992import com .sun .source .tree .VariableTree ;
9093import com .sun .source .util .TreePathScanner ;
9194import com .sun .source .util .Trees ;
@@ -291,10 +294,6 @@ private String capiTypeToForeignPrimitiveType(VariableElement element) {
291294 return type .equals ("void" ) ? "void" : ("j" + type );
292295 }
293296
294- private static String argName (int i ) {
295- return "" + (char ) ('a' + i );
296- }
297-
298297 private static final String CAPI_BUILTIN = "com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiBuiltin" ;
299298 private static final String CAPI_BUILTINS = "com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiBuiltins" ;
300299 private static final String CAPI_WRAPPER_DESCRIPTOR = "com.oracle.graal.python.builtins.objects.cext.capi.ExternalFunctionNodes.CApiWrapperDescriptor" ;
@@ -1261,21 +1260,6 @@ private TypeMirror toJavaNfiType(TypeMirror typeMirror) {
12611260 return processingEnv .getTypeUtils ().getPrimitiveType (TypeKind .LONG );
12621261 }
12631262
1264- private static String getNativeMethodHandleVarName (String signatureName ) {
1265- return "NATIVE_METHOD_HANDLE_" + signatureName ;
1266- }
1267-
1268- private static String toClassLiteral (String javaType ) {
1269- return switch (javaType ) {
1270- case "void" -> "void.class" ;
1271- case "int" -> "int.class" ;
1272- case "long" -> "long.class" ;
1273- case "float" -> "float.class" ;
1274- case "double" -> "double.class" ;
1275- default -> throw new IllegalArgumentException ("Unexpected Java type: " + javaType );
1276- };
1277- }
1278-
12791263 private boolean isCannotRaise (VariableElement signature ) {
12801264 String externalFunctionSignatureInitializer = getExternalFunctionSignatureInitializer (signature );
12811265 int start = externalFunctionSignatureInitializer .indexOf ('(' );
@@ -1348,7 +1332,6 @@ private void generateExternalFunctionInvoker(List<CApiExternalFunctionSignatureD
13481332 assert sig .returnType != null ;
13491333 assert sig .argumentTypes != null ;
13501334 String returnType = toJavaNfiType (sig .returnType );
1351- String returnTypeLiteral = toClassLiteral (returnType );
13521335 List <String > argTypes = Arrays .stream (sig .argumentTypes ).map (this ::toJavaNfiType ).toList ();
13531336
13541337 boolean isVoidReturn = "void" .equals (returnType );
@@ -1381,14 +1364,7 @@ private void generateExternalFunctionInvoker(List<CApiExternalFunctionSignatureD
13811364 for (String argType : argTypes ) {
13821365 typedArgs .add (argType + " " + argName (i ++));
13831366 }
1384-
1385- List <String > methodTypeArgs = new ArrayList <>();
1386- methodTypeArgs .add ("long.class" );
1387- for (String argType : argTypes ) {
1388- methodTypeArgs .add (toClassLiteral (argType ));
1389- }
1390- lines .add (" private static final MethodHandle " + getNativeMethodHandleVarName (sig .name ) + " = NativeAccessSupport.createDowncallHandle(" +
1391- "MethodType.methodType(" + returnTypeLiteral + ", " + String .join (", " , methodTypeArgs ) + "), false);" );
1367+ NativeDowncallMethodHandleGenerator .emitMethodHandleField (lines , NativeDowncallMethodHandleGenerator .methodHandleVarName (sig .name ), returnType , argTypes );
13921368
13931369 lines .add ("" );
13941370 if (sig .cannotRaise ) {
@@ -1464,9 +1440,9 @@ private void generateExternalFunctionInvoker(List<CApiExternalFunctionSignatureD
14641440 lines .add (" public static " + returnType + " invoke" + sig .name + "(" + String .join (", " , rawInvokeArgs ) + ") throws Throwable {" );
14651441 String directArgExpr = cArgs .isEmpty () ? "function" : "function, " + String .join (", " , cArgs );
14661442 if (isVoidReturn ) {
1467- lines .add (" " + getNativeMethodHandleVarName (sig .name ) + ".invokeExact(" + directArgExpr + ");" );
1443+ lines .add (" " + NativeDowncallMethodHandleGenerator . methodHandleVarName (sig .name ) + ".invokeExact(" + directArgExpr + ");" );
14681444 } else {
1469- lines .add (" return (" + returnType + ") " + getNativeMethodHandleVarName (sig .name ) + ".invokeExact(" + directArgExpr + ");" );
1445+ lines .add (" return (" + returnType + ") " + NativeDowncallMethodHandleGenerator . methodHandleVarName (sig .name ) + ".invokeExact(" + directArgExpr + ");" );
14701446 }
14711447 lines .add (" }" );
14721448 }
@@ -1504,6 +1480,7 @@ private void generateNativeAccessSupport(Element[] origins) throws IOException {
15041480 lines .add ("import java.util.OptionalLong;" );
15051481 lines .add ("" );
15061482 lines .add ("import static com.oracle.truffle.api.CompilerDirectives.shouldNotReachHere;" );
1483+ lines .add ("import " + NativeSimpleType .class .getCanonicalName () + ";" );
15071484 lines .add ("" );
15081485 lines .add ("public final class " + NATIVE_ACCESS_SUPPORT_IMPL_CLASS_NAME + " extends " + NATIVE_ACCESS_SUPPORT_CLASS_NAME + " {" );
15091486 lines .add (" private static final MethodHandle OF_ADDRESS;" );
@@ -1604,7 +1581,7 @@ private void generateNativeAccessSupport(Element[] origins) throws IOException {
16041581 lines .add (" case SINT64 -> ValueLayout.JAVA_LONG;" );
16051582 lines .add (" case FLOAT -> ValueLayout.JAVA_FLOAT;" );
16061583 lines .add (" case DOUBLE -> ValueLayout.JAVA_DOUBLE;" );
1607- lines .add (" case RAW_POINTER -> ValueLayout.JAVA_LONG;" );
1584+ lines .add (" case POINTER -> ValueLayout.JAVA_LONG;" );
16081585 lines .add (" };" );
16091586 lines .add (" }" );
16101587 lines .add ("}" );
@@ -1626,6 +1603,8 @@ private void generateDummyNativeAccessSupport(Element[] origins) throws IOExcept
16261603 lines .add ("import java.lang.invoke.MethodHandle;" );
16271604 lines .add ("import java.lang.invoke.MethodType;" );
16281605 lines .add ("" );
1606+ lines .add ("import " + NativeSimpleType .class .getCanonicalName () + ";" );
1607+ lines .add ("" );
16291608 lines .add ("public final class " + NATIVE_ACCESS_SUPPORT_IMPL_CLASS_NAME + " extends " + NATIVE_ACCESS_SUPPORT_CLASS_NAME + " {" );
16301609 lines .add (" @Override" );
16311610 lines .add (" protected Object createArenaImpl() {" );
0 commit comments