78
78
import com .oracle .graal .python .nodes .util .CastToIndexNode ;
79
79
import com .oracle .graal .python .runtime .PythonCore ;
80
80
import com .oracle .graal .python .runtime .exception .PythonErrorType ;
81
- import com .oracle .truffle .api .CompilerAsserts ;
82
81
import com .oracle .truffle .api .CompilerDirectives ;
83
82
import com .oracle .truffle .api .CompilerDirectives .CompilationFinal ;
84
83
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
92
91
import com .oracle .truffle .api .interop .InteropException ;
93
92
import com .oracle .truffle .api .interop .Message ;
94
93
import com .oracle .truffle .api .interop .TruffleObject ;
94
+ import com .oracle .truffle .api .interop .UnknownIdentifierException ;
95
95
import com .oracle .truffle .api .interop .UnsupportedMessageException ;
96
96
import com .oracle .truffle .api .interop .UnsupportedTypeException ;
97
97
import com .oracle .truffle .api .nodes .ExplodeLoop ;
98
98
import com .oracle .truffle .api .nodes .Node ;
99
+ import com .oracle .truffle .api .profiles .BranchProfile ;
99
100
100
101
public abstract class CExtNodes {
101
102
@@ -105,30 +106,40 @@ public abstract class CExtNodes {
105
106
* will call that subtype C function with two arguments, the C type object and an object
106
107
* argument to fill in from.
107
108
*/
108
- public static class SubtypeNew extends PBaseNode {
109
- private final TruffleObject subtypeFunc ;
109
+ public static class SubtypeNew extends CExtBaseNode {
110
110
@ Child private Node executeNode = Message .EXECUTE .createNode ();
111
111
@ Child private ToSulongNode toSulongNode = ToSulongNode .create ();
112
112
@ Child private ToJavaNode toJavaNode = ToJavaNode .create ();
113
113
114
+ private final String functionName ;
115
+
116
+ @ CompilationFinal private TruffleObject subtypeFunc ;
117
+
114
118
/**
115
119
* @param typenamePrefix the <code>typename</code> in <code>typename_subtype_new</code>
116
120
*/
117
121
public SubtypeNew (String typenamePrefix ) {
118
- subtypeFunc = (TruffleObject ) getContext ().getEnv ().importSymbol (typenamePrefix + "_subtype_new" );
119
- assert subtypeFunc != null ;
122
+ functionName = typenamePrefix + "_subtype_new" ;
120
123
}
121
124
122
125
public Object execute (PythonNativeClass object , Object arg ) {
123
126
try {
124
- return toJavaNode .execute (ForeignAccess .sendExecute (executeNode , subtypeFunc , toSulongNode .execute (object ), arg ));
127
+ return toJavaNode .execute (ForeignAccess .sendExecute (executeNode , getFunction () , toSulongNode .execute (object ), arg ));
125
128
} catch (UnsupportedMessageException | UnsupportedTypeException | ArityException e ) {
126
129
throw new IllegalStateException ("C subtype_new function failed" , e );
127
130
}
128
131
}
132
+
133
+ private TruffleObject getFunction () {
134
+ if (subtypeFunc == null ) {
135
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
136
+ subtypeFunc = importCAPISymbol (functionName );
137
+ }
138
+ return subtypeFunc ;
139
+ }
129
140
}
130
141
131
- public static class FromNativeSubclassNode <T > extends PBaseNode {
142
+ public static class FromNativeSubclassNode <T > extends CExtBaseNode {
132
143
private final PythonBuiltinClassType expectedType ;
133
144
private final String conversionFuncName ;
134
145
@ CompilationFinal private TruffleObject conversionFunc ;
@@ -157,7 +168,7 @@ private Node getExecNode() {
157
168
private TruffleObject getConversionFunc () {
158
169
if (conversionFunc == null ) {
159
170
CompilerDirectives .transferToInterpreterAndInvalidate ();
160
- conversionFunc = ( TruffleObject ) getContext (). getEnv (). importSymbol (conversionFuncName );
171
+ conversionFunc = importCAPISymbol (conversionFuncName );
161
172
}
162
173
return conversionFunc ;
163
174
}
@@ -193,11 +204,26 @@ public static <T> FromNativeSubclassNode<T> create(PythonBuiltinClassType expect
193
204
194
205
@ ImportStatic (PGuards .class )
195
206
abstract static class CExtBaseNode extends PBaseNode {
207
+ @ Child private Node readSymbolNode ;
196
208
197
209
protected static boolean isNativeWrapper (Object obj ) {
198
210
return obj instanceof PythonNativeWrapper ;
199
211
}
200
212
213
+ protected TruffleObject importCAPISymbol (String name ) {
214
+ TruffleObject capiLibrary = (TruffleObject ) getContext ().getCapiLibrary ();
215
+ if (readSymbolNode == null ) {
216
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
217
+ readSymbolNode = insert (Message .READ .createNode ());
218
+ }
219
+ try {
220
+ return (TruffleObject ) ForeignAccess .sendRead (readSymbolNode , capiLibrary , name );
221
+ } catch (UnknownIdentifierException | UnsupportedMessageException e ) {
222
+ CompilerDirectives .transferToInterpreter ();
223
+ throw e .raise ();
224
+ }
225
+ }
226
+
201
227
}
202
228
203
229
public abstract static class ToSulongNode extends CExtBaseNode {
@@ -407,7 +433,7 @@ Object doForeign(Object value) {
407
433
}
408
434
if (nativeToJavaFunction == null ) {
409
435
CompilerDirectives .transferToInterpreterAndInvalidate ();
410
- nativeToJavaFunction = ( TruffleObject ) getContext (). getEnv (). importSymbol (NativeCAPISymbols .FUN_NATIVE_TO_JAVA );
436
+ nativeToJavaFunction = importCAPISymbol (NativeCAPISymbols .FUN_NATIVE_TO_JAVA );
411
437
}
412
438
return toJavaNode .execute (callNativeNode .execute (nativeToJavaFunction , new Object []{value }));
413
439
}
@@ -453,15 +479,15 @@ Object doByteArray(byte[] arr,
453
479
TruffleObject getTruffleStringToCstr () {
454
480
if (truffle_string_to_cstr == null ) {
455
481
CompilerDirectives .transferToInterpreterAndInvalidate ();
456
- truffle_string_to_cstr = ( TruffleObject ) getContext (). getEnv (). importSymbol (NativeCAPISymbols .FUN_PY_TRUFFLE_STRING_TO_CSTR );
482
+ truffle_string_to_cstr = importCAPISymbol (NativeCAPISymbols .FUN_PY_TRUFFLE_STRING_TO_CSTR );
457
483
}
458
484
return truffle_string_to_cstr ;
459
485
}
460
486
461
487
TruffleObject getTruffleByteArrayToNative () {
462
488
if (truffle_byte_array_to_native == null ) {
463
489
CompilerDirectives .transferToInterpreterAndInvalidate ();
464
- truffle_byte_array_to_native = ( TruffleObject ) getContext (). getEnv (). importSymbol (NativeCAPISymbols .FUN_PY_TRUFFLE_BYTE_ARRAY_TO_NATIVE );
490
+ truffle_byte_array_to_native = importCAPISymbol (NativeCAPISymbols .FUN_PY_TRUFFLE_BYTE_ARRAY_TO_NATIVE );
465
491
}
466
492
return truffle_byte_array_to_native ;
467
493
}
@@ -483,7 +509,7 @@ public static class FromCharPointerNode extends CExtBaseNode {
483
509
TruffleObject getTruffleStringToCstr () {
484
510
if (truffle_cstr_to_string == null ) {
485
511
CompilerDirectives .transferToInterpreterAndInvalidate ();
486
- truffle_cstr_to_string = ( TruffleObject ) getContext (). getEnv (). importSymbol (NativeCAPISymbols .FUN_PY_TRUFFLE_CSTR_TO_STRING );
512
+ truffle_cstr_to_string = importCAPISymbol (NativeCAPISymbols .FUN_PY_TRUFFLE_CSTR_TO_STRING );
487
513
}
488
514
return truffle_cstr_to_string ;
489
515
}
@@ -541,7 +567,7 @@ private PCallNativeNode getCallGetObTypeNode() {
541
567
TruffleObject getObTypeFunction () {
542
568
if (func == null ) {
543
569
CompilerDirectives .transferToInterpreterAndInvalidate ();
544
- func = ( TruffleObject ) getContext (). getEnv (). importSymbol (NativeCAPISymbols .FUN_GET_OB_TYPE );
570
+ func = importCAPISymbol (NativeCAPISymbols .FUN_GET_OB_TYPE );
545
571
}
546
572
return func ;
547
573
}
@@ -559,7 +585,7 @@ public long execute() {
559
585
if (wcharSize < 0 ) {
560
586
CompilerDirectives .transferToInterpreterAndInvalidate ();
561
587
try {
562
- wcharSize = (long ) ForeignAccess .sendExecute (Message .EXECUTE .createNode (), getNativeFunction ( ));
588
+ wcharSize = (long ) ForeignAccess .sendExecute (Message .EXECUTE .createNode (), importCAPISymbol ( NativeCAPISymbols . FUN_WHCAR_SIZE ));
563
589
assert wcharSize >= 0L ;
564
590
} catch (InteropException e ) {
565
591
throw e .raise ();
@@ -568,11 +594,6 @@ public long execute() {
568
594
return wcharSize ;
569
595
}
570
596
571
- TruffleObject getNativeFunction () {
572
- CompilerAsserts .neverPartOfCompilation ();
573
- return (TruffleObject ) getContext ().getEnv ().importSymbol (NativeCAPISymbols .FUN_WHCAR_SIZE );
574
- }
575
-
576
597
public static SizeofWCharNode create () {
577
598
return new SizeofWCharNode ();
578
599
}
@@ -591,10 +612,10 @@ public boolean execute(PythonNativeObject a, PythonNativeObject b) {
591
612
}
592
613
}
593
614
594
- TruffleObject getNativeFunction () {
615
+ private TruffleObject getNativeFunction () {
595
616
if (isFunc == null ) {
596
617
CompilerDirectives .transferToInterpreterAndInvalidate ();
597
- isFunc = ( TruffleObject ) getContext (). getEnv (). importSymbol (NativeCAPISymbols .FUN_PTR_COMPARE );
618
+ isFunc = importCAPISymbol (NativeCAPISymbols .FUN_PTR_COMPARE );
598
619
}
599
620
return isFunc ;
600
621
}
@@ -914,4 +935,47 @@ public static AsLong create() {
914
935
}
915
936
}
916
937
938
+ public static class PCallBinaryCapiFunction extends CExtBaseNode {
939
+
940
+ @ Child private Node callNode ;
941
+
942
+ private final String name ;
943
+ private final BranchProfile profile = BranchProfile .create ();
944
+
945
+ @ CompilationFinal TruffleObject receiver ;
946
+
947
+ public PCallBinaryCapiFunction (String name ) {
948
+ this .name = name ;
949
+ }
950
+
951
+ public Object execute (Object arg0 , Object arg1 ) {
952
+ try {
953
+ return ForeignAccess .sendExecute (getCallNode (), getFunction (), arg0 , arg1 );
954
+ } catch (UnsupportedTypeException | ArityException | UnsupportedMessageException e ) {
955
+ profile .enter ();
956
+ throw e .raise ();
957
+ }
958
+ }
959
+
960
+ private Node getCallNode () {
961
+ if (callNode == null ) {
962
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
963
+ callNode = insert (Message .EXECUTE .createNode ());
964
+ }
965
+ return callNode ;
966
+ }
967
+
968
+ private TruffleObject getFunction () {
969
+ if (receiver == null ) {
970
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
971
+ receiver = importCAPISymbol (name );
972
+ }
973
+ return receiver ;
974
+ }
975
+
976
+ public static PCallBinaryCapiFunction create (String name ) {
977
+ return new PCallBinaryCapiFunction (name );
978
+ }
979
+ }
980
+
917
981
}
0 commit comments