Skip to content

Commit 7a90ee4

Browse files
committed
Implement 'GetDynamicType' MR for 'CArrayWrapper'.
1 parent 81fd559 commit 7a90ee4

File tree

1 file changed

+58
-3
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext

1 file changed

+58
-3
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/CArrayWrapperMR.java

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,17 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.cext;
4242

43+
import com.oracle.graal.python.builtins.objects.cext.CArrayWrapperMRFactory.GetTypeIDNodeGen;
4344
import com.oracle.graal.python.builtins.objects.cext.CArrayWrappers.CArrayWrapper;
4445
import com.oracle.graal.python.builtins.objects.cext.CArrayWrappers.CByteArrayWrapper;
4546
import com.oracle.graal.python.builtins.objects.cext.CArrayWrappers.CStringWrapper;
47+
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.CExtBaseNode;
48+
import com.oracle.graal.python.builtins.objects.ints.PInt;
49+
import com.oracle.graal.python.nodes.SpecialMethodNames;
4650
import com.oracle.truffle.api.CompilerDirectives;
51+
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
52+
import com.oracle.truffle.api.dsl.ImportStatic;
53+
import com.oracle.truffle.api.dsl.Specialization;
4754
import com.oracle.truffle.api.interop.ForeignAccess;
4855
import com.oracle.truffle.api.interop.Message;
4956
import com.oracle.truffle.api.interop.MessageResolution;
@@ -58,19 +65,24 @@ public class CArrayWrapperMR {
5865

5966
@Resolve(message = "READ")
6067
abstract static class ReadNode extends Node {
61-
6268
public char access(CStringWrapper object, int idx) {
6369
String s = object.getDelegate();
6470
if (idx >= 0 && idx < s.length()) {
6571
return s.charAt(idx);
6672
} else if (idx == s.length()) {
6773
return '\0';
6874
}
75+
CompilerDirectives.transferToInterpreter();
6976
throw UnknownIdentifierException.raise(Integer.toString(idx));
7077
}
7178

72-
public byte access(CByteArrayWrapper object, long idx) {
73-
return access(object, (int) idx);
79+
public char access(CStringWrapper object, long idx) {
80+
try {
81+
return access(object, PInt.intValueExact(idx));
82+
} catch (ArithmeticException e) {
83+
CompilerDirectives.transferToInterpreter();
84+
throw UnknownIdentifierException.raise(Long.toString(idx));
85+
}
7486
}
7587

7688
public byte access(CByteArrayWrapper object, int idx) {
@@ -80,8 +92,51 @@ public byte access(CByteArrayWrapper object, int idx) {
8092
} else if (idx == arr.length) {
8193
return (byte) 0;
8294
}
95+
CompilerDirectives.transferToInterpreter();
8396
throw UnknownIdentifierException.raise(Integer.toString(idx));
8497
}
98+
99+
public byte access(CByteArrayWrapper object, long idx) {
100+
try {
101+
return access(object, PInt.intValueExact(idx));
102+
} catch (ArithmeticException e) {
103+
CompilerDirectives.transferToInterpreter();
104+
throw UnknownIdentifierException.raise(Long.toString(idx));
105+
}
106+
}
107+
}
108+
109+
@SuppressWarnings("unknown-message")
110+
@Resolve(message = "com.oracle.truffle.llvm.spi.GetDynamicType")
111+
abstract static class GetDynamicTypeNode extends Node {
112+
@Child private GetTypeIDNode getTypeId = GetTypeIDNodeGen.create();
113+
114+
public Object access(CStringWrapper object) {
115+
return getTypeId.execute(object);
116+
}
117+
}
118+
119+
@ImportStatic(SpecialMethodNames.class)
120+
abstract static class GetTypeIDNode extends CExtBaseNode {
121+
122+
@Child private PCallNativeNode callUnaryNode = PCallNativeNode.create();
123+
124+
@CompilationFinal private TruffleObject funGetByteArrayTypeID;
125+
126+
public abstract Object execute(Object delegate);
127+
128+
@Specialization
129+
Object doTuple(CStringWrapper object) {
130+
return callGetByteArrayTypeID(object.getDelegate().length());
131+
}
132+
133+
private Object callGetByteArrayTypeID(long len) {
134+
if (funGetByteArrayTypeID == null) {
135+
CompilerDirectives.transferToInterpreterAndInvalidate();
136+
funGetByteArrayTypeID = importCAPISymbol(NativeCAPISymbols.FUN_GET_BYTE_ARRAY_TYPE_ID);
137+
}
138+
return callUnaryNode.execute(funGetByteArrayTypeID, new Object[]{len});
139+
}
85140
}
86141

87142
@Resolve(message = "HAS_SIZE")

0 commit comments

Comments
 (0)