Skip to content

Commit 1aad6d1

Browse files
committed
Add 'BoolNativeWrapper'.
1 parent 6860a53 commit 1aad6d1

File tree

5 files changed

+135
-51
lines changed

5 files changed

+135
-51
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/TruffleCextBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1257,7 +1257,7 @@ int doPythonObject(PythonAbstractObject nativeWrapper, TruffleObject ptr) {
12571257
}
12581258

12591259
@Specialization
1260-
int doNativeWrapper(PythonObjectNativeWrapper nativeWrapper, TruffleObject ptr) {
1260+
int doNativeWrapper(PythonNativeWrapper nativeWrapper, TruffleObject ptr) {
12611261
if (nativeWrapper.isNative()) {
12621262
PythonContext.getSingleNativeContextAssumption().invalidate();
12631263
} else {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/PythonAbstractObject.java

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

43-
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonObjectNativeWrapper;
43+
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.DynamicObjectNativeWrapper;
4444
import com.oracle.graal.python.runtime.interop.PythonMessageResolutionForeign;
4545
import com.oracle.truffle.api.interop.ForeignAccess;
4646
import com.oracle.truffle.api.interop.TruffleObject;
4747

4848
public abstract class PythonAbstractObject implements TruffleObject, Comparable<Object> {
49-
private PythonObjectNativeWrapper nativeWrapper;
49+
private DynamicObjectNativeWrapper nativeWrapper;
5050

5151
public final ForeignAccess getForeignAccess() {
5252
return PythonMessageResolutionForeign.ACCESS;
5353
}
5454

55-
public PythonObjectNativeWrapper getNativeWrapper() {
55+
public DynamicObjectNativeWrapper getNativeWrapper() {
5656
return nativeWrapper;
5757
}
5858

59-
public void setNativeWrapper(PythonObjectNativeWrapper nativeWrapper) {
59+
public void setNativeWrapper(DynamicObjectNativeWrapper nativeWrapper) {
6060
assert this.nativeWrapper == null;
6161
this.nativeWrapper = nativeWrapper;
6262
}

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

Lines changed: 70 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.ObjectUpcallNodeGen;
5757
import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.ToJavaNodeGen;
5858
import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.ToSulongNodeGen;
59+
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.BoolNativeWrapper;
5960
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.ByteNativeWrapper;
6061
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.DoubleNativeWrapper;
6162
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.IntNativeWrapper;
@@ -222,6 +223,10 @@ protected static boolean isNativeNull(Object object) {
222223
return object instanceof PythonNativeNull;
223224
}
224225

226+
protected static boolean isMaterialized(PrimitiveNativeWrapper wrapper) {
227+
return wrapper.getMaterializedObject() != null;
228+
}
229+
225230
protected TruffleObject importCAPISymbol(String name) {
226231
TruffleObject capiLibrary = (TruffleObject) getContext().getCapiLibrary();
227232
if (readSymbolNode == null) {
@@ -254,9 +259,8 @@ Object doString(String str,
254259
}
255260

256261
@Specialization
257-
Object doBoolean(boolean b,
258-
@Cached("createBinaryProfile()") ConditionProfile noWrapperProfile) {
259-
return PythonObjectNativeWrapper.wrap(factory().createInt(b), noWrapperProfile);
262+
Object doBoolean(boolean b) {
263+
return BoolNativeWrapper.create(b);
260264
}
261265

262266
@Specialization
@@ -270,9 +274,8 @@ Object doLong(long l) {
270274
}
271275

272276
@Specialization
273-
Object doDouble(double d,
274-
@Cached("createBinaryProfile()") ConditionProfile noWrapperProfile) {
275-
return PythonObjectNativeWrapper.wrap(factory().createFloat(d), noWrapperProfile);
277+
Object doDouble(double d) {
278+
return DoubleNativeWrapper.create(d);
276279
}
277280

278281
@Specialization
@@ -347,6 +350,11 @@ public abstract static class AsPythonObjectNode extends CExtBaseNode {
347350

348351
@Child GetClassNode getClassNode;
349352

353+
@Specialization(guards = "!isMaterialized(object)")
354+
boolean doBoolNativeWrapper(BoolNativeWrapper object) {
355+
return object.getValue();
356+
}
357+
350358
@Specialization(guards = "!isMaterialized(object)")
351359
byte doByteNativeWrapper(ByteNativeWrapper object) {
352360
return object.getValue();
@@ -436,10 +444,6 @@ protected boolean isForeignObject(TruffleObject obj) {
436444
return getClassNode.execute(obj) == getCore().lookupType(PythonBuiltinClassType.TruffleObject);
437445
}
438446

439-
protected static boolean isMaterialized(PrimitiveNativeWrapper wrapper) {
440-
return wrapper.getMaterializedObject() != null;
441-
}
442-
443447
@TruffleBoundary
444448
public static Object doSlowPath(PythonCore core, Object object) {
445449
if (object instanceof PythonNativeWrapper) {
@@ -464,28 +468,39 @@ public abstract static class MaterializeDelegateNode extends CExtBaseNode {
464468

465469
@Child GetClassNode getClassNode;
466470

467-
@Specialization
471+
@Specialization(guards = "!isMaterialized(object)")
472+
PInt doBoolNativeWrapper(BoolNativeWrapper object) {
473+
PInt materializedInt = factory().createInt(object.getValue());
474+
object.setMaterializedObject(materializedInt);
475+
materializedInt.setNativeWrapper(object);
476+
return materializedInt;
477+
}
478+
479+
@Specialization(guards = "!isMaterialized(object)")
468480
PInt doByteNativeWrapper(ByteNativeWrapper object) {
469481
PInt materializedInt = factory().createInt(object.getValue());
470482
object.setMaterializedObject(materializedInt);
483+
materializedInt.setNativeWrapper(object);
471484
return materializedInt;
472485
}
473486

474-
@Specialization
487+
@Specialization(guards = "!isMaterialized(object)")
475488
PInt doIntNativeWrapper(IntNativeWrapper object) {
476489
PInt materializedInt = factory().createInt(object.getValue());
477490
object.setMaterializedObject(materializedInt);
491+
materializedInt.setNativeWrapper(object);
478492
return materializedInt;
479493
}
480494

481-
@Specialization
495+
@Specialization(guards = "!isMaterialized(object)")
482496
PInt doLongNativeWrapper(LongNativeWrapper object) {
483497
PInt materializedInt = factory().createInt(object.getValue());
484498
object.setMaterializedObject(materializedInt);
499+
materializedInt.setNativeWrapper(object);
485500
return materializedInt;
486501
}
487502

488-
@Specialization
503+
@Specialization(guards = "!isMaterialized(object)")
489504
PFloat doDoubleNativeWrapper(DoubleNativeWrapper object) {
490505
PFloat materializedInt = factory().createFloat(object.getValue());
491506
object.setMaterializedObject(materializedInt);
@@ -512,6 +527,12 @@ public static MaterializeDelegateNode create() {
512527
}
513528
}
514529

530+
public abstract static class GetNativeWrapper extends CExtBaseNode {
531+
532+
public abstract PythonNativeWrapper execute(Object obj);
533+
534+
}
535+
515536
/**
516537
* Does the same conversion as the native function {@code to_java}. The node tries to avoid
517538
* calling the native function for resolving native handles.
@@ -973,6 +994,31 @@ public static AsDouble create() {
973994
return value.getValue();
974995
}
975996

997+
@Specialization
998+
double doBoolNativeWrapper(BoolNativeWrapper object) {
999+
return PInt.intValue(object.getValue());
1000+
}
1001+
1002+
@Specialization
1003+
double doByteNativeWrapper(ByteNativeWrapper object) {
1004+
return object.getValue();
1005+
}
1006+
1007+
@Specialization
1008+
double doIntNativeWrapper(IntNativeWrapper object) {
1009+
return object.getValue();
1010+
}
1011+
1012+
@Specialization
1013+
double doLongNativeWrapper(LongNativeWrapper object) {
1014+
return object.getValue();
1015+
}
1016+
1017+
@Specialization
1018+
double doDoubleNativeWrapper(DoubleNativeWrapper object) {
1019+
return object.getValue();
1020+
}
1021+
9761022
// TODO: this should just use the builtin constructor node so we don't duplicate the corner
9771023
// cases
9781024
@Fallback
@@ -1036,6 +1082,16 @@ long run(PFloat value) {
10361082
return (long) value.getValue();
10371083
}
10381084

1085+
@Specialization
1086+
long doBoolNativeWrapper(BoolNativeWrapper object) {
1087+
return PInt.intValue(object.getValue());
1088+
}
1089+
1090+
@Specialization
1091+
long doByteNativeWrapper(ByteNativeWrapper object) {
1092+
return object.getValue();
1093+
}
1094+
10391095
@Specialization
10401096
long doIntNativeWrapper(IntNativeWrapper object) {
10411097
return object.getValue();

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

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public ForeignAccess getForeignAccess() {
8686
}
8787
}
8888

89-
abstract static class DynamicObjectNativeWrapper extends PythonNativeWrapper {
89+
public abstract static class DynamicObjectNativeWrapper extends PythonNativeWrapper {
9090
private static final Layout OBJECT_LAYOUT = Layout.newLayout().build();
9191
private static final Shape SHAPE = OBJECT_LAYOUT.createShape(new ObjectType());
9292

@@ -120,9 +120,9 @@ public PythonAbstractObject getPythonObject() {
120120
return pythonObject;
121121
}
122122

123-
public static PythonObjectNativeWrapper wrap(PythonAbstractObject obj, ConditionProfile noWrapperProfile) {
123+
public static DynamicObjectNativeWrapper wrap(PythonAbstractObject obj, ConditionProfile noWrapperProfile) {
124124
// important: native wrappers are cached
125-
PythonObjectNativeWrapper nativeWrapper = obj.getNativeWrapper();
125+
DynamicObjectNativeWrapper nativeWrapper = obj.getNativeWrapper();
126126
if (noWrapperProfile.profile(nativeWrapper == null)) {
127127
nativeWrapper = new PythonObjectNativeWrapper(obj);
128128
obj.setNativeWrapper(nativeWrapper);
@@ -146,7 +146,7 @@ public abstract static class PrimitiveNativeWrapper extends DynamicObjectNativeW
146146

147147
private PythonObject materializedObject;
148148

149-
protected abstract Number getBoxedValue();
149+
protected abstract Object getBoxedValue();
150150

151151
@Override
152152
public Object getDelegate() {
@@ -165,84 +165,111 @@ PythonObject getMaterializedObject() {
165165
}
166166
}
167167

168-
public static class IntNativeWrapper extends PrimitiveNativeWrapper {
169-
private final int value;
168+
public static class BoolNativeWrapper extends PrimitiveNativeWrapper {
169+
private final boolean value;
170170

171-
private IntNativeWrapper(int value) {
171+
private BoolNativeWrapper(boolean value) {
172172
this.value = value;
173173
}
174174

175-
public int getValue() {
175+
public boolean getValue() {
176176
return value;
177177
}
178178

179179
@Override
180-
public Integer getBoxedValue() {
180+
public Boolean getBoxedValue() {
181181
return value;
182182
}
183183

184184
@Override
185185
public String toString() {
186186
CompilerAsserts.neverPartOfCompilation();
187-
return String.format("IntNativeWrapper(%s, isNative=%s)", value, isNative());
187+
return String.format("BoolNativeWrapper(%s, isNative=%s)", value, isNative());
188188
}
189189

190-
public static IntNativeWrapper create(int value) {
191-
return new IntNativeWrapper(value);
190+
public static BoolNativeWrapper create(boolean value) {
191+
return new BoolNativeWrapper(value);
192192
}
193193
}
194194

195-
public static class LongNativeWrapper extends PrimitiveNativeWrapper {
196-
private final long value;
195+
public static class ByteNativeWrapper extends PrimitiveNativeWrapper {
196+
private final byte value;
197197

198-
private LongNativeWrapper(long value) {
198+
private ByteNativeWrapper(byte value) {
199199
this.value = value;
200200
}
201201

202-
public long getValue() {
202+
public byte getValue() {
203203
return value;
204204
}
205205

206206
@Override
207-
public Long getBoxedValue() {
207+
public Byte getBoxedValue() {
208208
return value;
209209
}
210210

211211
@Override
212212
public String toString() {
213213
CompilerAsserts.neverPartOfCompilation();
214-
return String.format("LongNativeWrapper(%s, isNative=%s)", value, isNative());
214+
return String.format("ByteNativeWrapper(%s, isNative=%s)", value, isNative());
215215
}
216216

217-
public static LongNativeWrapper create(long value) {
218-
return new LongNativeWrapper(value);
217+
public static ByteNativeWrapper create(byte value) {
218+
return new ByteNativeWrapper(value);
219219
}
220220
}
221221

222-
public static class ByteNativeWrapper extends PrimitiveNativeWrapper {
223-
private final byte value;
222+
public static class IntNativeWrapper extends PrimitiveNativeWrapper {
223+
private final int value;
224224

225-
private ByteNativeWrapper(byte value) {
225+
private IntNativeWrapper(int value) {
226226
this.value = value;
227227
}
228228

229-
public byte getValue() {
229+
public int getValue() {
230230
return value;
231231
}
232232

233233
@Override
234-
public Byte getBoxedValue() {
234+
public Integer getBoxedValue() {
235235
return value;
236236
}
237237

238238
@Override
239239
public String toString() {
240240
CompilerAsserts.neverPartOfCompilation();
241-
return String.format("ByteNativeWrapper(%s, isNative=%s)", value, isNative());
241+
return String.format("IntNativeWrapper(%s, isNative=%s)", value, isNative());
242242
}
243243

244-
public static ByteNativeWrapper create(byte value) {
245-
return new ByteNativeWrapper(value);
244+
public static IntNativeWrapper create(int value) {
245+
return new IntNativeWrapper(value);
246+
}
247+
}
248+
249+
public static class LongNativeWrapper extends PrimitiveNativeWrapper {
250+
private final long value;
251+
252+
private LongNativeWrapper(long value) {
253+
this.value = value;
254+
}
255+
256+
public long getValue() {
257+
return value;
258+
}
259+
260+
@Override
261+
public Long getBoxedValue() {
262+
return value;
263+
}
264+
265+
@Override
266+
public String toString() {
267+
CompilerAsserts.neverPartOfCompilation();
268+
return String.format("LongNativeWrapper(%s, isNative=%s)", value, isNative());
269+
}
270+
271+
public static LongNativeWrapper create(long value) {
272+
return new LongNativeWrapper(value);
246273
}
247274
}
248275

0 commit comments

Comments
 (0)