Skip to content

Commit 724fed2

Browse files
committed
Migrate lib nodes to new slots
1 parent 68305a7 commit 724fed2

19 files changed

+343
-317
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextAbstractBuiltins.java

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -95,8 +95,12 @@
9595
import com.oracle.graal.python.builtins.objects.iterator.IteratorNodes;
9696
import com.oracle.graal.python.builtins.objects.list.PList;
9797
import com.oracle.graal.python.builtins.objects.method.PBuiltinMethod;
98+
import com.oracle.graal.python.builtins.objects.slice.PSlice;
99+
import com.oracle.graal.python.builtins.objects.type.TpSlots;
100+
import com.oracle.graal.python.builtins.objects.type.TpSlots.GetObjectSlotsNode;
98101
import com.oracle.graal.python.builtins.objects.type.TypeNodes.IsSameTypeNode;
99102
import com.oracle.graal.python.builtins.objects.type.TypeNodes.IsTypeNode;
103+
import com.oracle.graal.python.builtins.objects.type.slots.TpSlotMpAssSubscript.CallSlotMpAssSubscriptNode;
100104
import com.oracle.graal.python.lib.GetNextNode;
101105
import com.oracle.graal.python.lib.PyIndexCheckNode;
102106
import com.oracle.graal.python.lib.PyIterCheckNode;
@@ -123,7 +127,6 @@
123127
import com.oracle.graal.python.nodes.attributes.WriteAttributeToPythonObjectNode;
124128
import com.oracle.graal.python.nodes.builtins.ListNodes.ConstructListNode;
125129
import com.oracle.graal.python.nodes.call.CallNode;
126-
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
127130
import com.oracle.graal.python.nodes.call.special.LookupAndCallTernaryNode;
128131
import com.oracle.graal.python.nodes.expression.BinaryArithmetic;
129132
import com.oracle.graal.python.nodes.expression.BinaryOpNode;
@@ -507,7 +510,7 @@ static Object setItem(Object obj, long key, Object value,
507510
if ((int) key != key) {
508511
throw PRaiseNode.raiseUncached(inliningTarget, OverflowError, ErrorMessages.CANNOT_FIT_P_INTO_INDEXSIZED_INT, key);
509512
}
510-
setItemNode.execute(obj, (int) key, value);
513+
setItemNode.execute(null, inliningTarget, obj, (int) key, value);
511514
return 0;
512515
}
513516
}
@@ -618,7 +621,7 @@ static Object run(Object o, long i,
618621
if ((int) i != i) {
619622
throw PRaiseNode.raiseUncached(inliningTarget, OverflowError, ErrorMessages.CANNOT_FIT_P_INTO_INDEXSIZED_INT, i);
620623
}
621-
delItemNode.execute(o, (int) i);
624+
delItemNode.execute(null, inliningTarget, o, (int) i);
622625
return 0;
623626
}
624627
}
@@ -652,10 +655,18 @@ abstract static class PySequence_SetSlice extends CApiQuaternaryBuiltinNode {
652655
@Specialization
653656
static int setSlice(Object sequence, Object iLow, Object iHigh, Object s,
654657
@Bind("this") Node inliningTarget,
655-
@Cached("create(SetItem)") LookupAndCallTernaryNode setItemNode,
656-
@Cached PySliceNew sliceNode) {
657-
setItemNode.execute(null, sequence, sliceNode.execute(inliningTarget, iLow, iHigh, PNone.NONE), s);
658-
return 0;
658+
@Cached GetObjectSlotsNode getSlotsNode,
659+
@Cached CallSlotMpAssSubscriptNode callSetItem,
660+
@Cached PySliceNew sliceNode,
661+
@Cached PRaiseNode.Lazy raiseNode) {
662+
TpSlots slots = getSlotsNode.execute(inliningTarget, sequence);
663+
if (slots.mp_ass_subscript() != null) {
664+
PSlice slice = sliceNode.execute(inliningTarget, iLow, iHigh, PNone.NONE);
665+
callSetItem.execute(null, inliningTarget, slots.mp_ass_subscript(), sequence, slice, s);
666+
return 0;
667+
} else {
668+
throw raiseNode.get(inliningTarget).raise(TypeError, ErrorMessages.P_OBJECT_DOESNT_SUPPORT_SLICE_ASSIGNMENT, sequence);
669+
}
659670
}
660671
}
661672

@@ -664,10 +675,18 @@ abstract static class PySequence_DelSlice extends CApiTernaryBuiltinNode {
664675
@Specialization
665676
static int setSlice(Object sequence, Object iLow, Object iHigh,
666677
@Bind("this") Node inliningTarget,
667-
@Cached("create(DelItem)") LookupAndCallBinaryNode delItemNode,
668-
@Cached PySliceNew sliceNode) {
669-
delItemNode.executeObject(null, sequence, sliceNode.execute(inliningTarget, iLow, iHigh, PNone.NONE));
670-
return 0;
678+
@Cached GetObjectSlotsNode getSlotsNode,
679+
@Cached CallSlotMpAssSubscriptNode callSetItem,
680+
@Cached PySliceNew sliceNode,
681+
@Cached PRaiseNode.Lazy raiseNode) {
682+
TpSlots slots = getSlotsNode.execute(inliningTarget, sequence);
683+
if (slots.mp_ass_subscript() != null) {
684+
PSlice slice = sliceNode.execute(inliningTarget, iLow, iHigh, PNone.NONE);
685+
callSetItem.execute(null, inliningTarget, slots.mp_ass_subscript(), sequence, slice, PNone.NO_VALUE);
686+
return 0;
687+
} else {
688+
throw raiseNode.get(inliningTarget).raise(TypeError, ErrorMessages.P_OBJECT_DOESNT_SUPPORT_SLICE_DELETION, sequence);
689+
}
671690
}
672691
}
673692

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextListBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ static int getSlice(PList list, Object iLow, Object iHigh, Object s,
214214
@Bind("this") Node inliningTarget,
215215
@Cached ListBuiltins.SetSubscriptNode setItemNode,
216216
@Cached PySliceNew sliceNode) {
217-
setItemNode.execute(null, list, sliceNode.execute(inliningTarget, iLow, iHigh, PNone.NONE), s);
217+
setItemNode.executeVoid(null, list, sliceNode.execute(inliningTarget, iLow, iHigh, PNone.NONE), s);
218218
return 0;
219219
}
220220

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -355,7 +355,7 @@ public void writeArrayElement(long key, Object value,
355355
getValue.execute(inliningTarget, behavior, method, this, key, value);
356356
} else {
357357
try {
358-
sequenceSetItemNode.execute(this, PInt.intValueExact(key), value);
358+
sequenceSetItemNode.execute(null, inliningTarget, this, PInt.intValueExact(key), value);
359359
} catch (OverflowException cce) {
360360
throw InvalidArrayIndexException.create(key);
361361
} catch (PException pe) {
@@ -383,7 +383,7 @@ public void removeArrayElement(long key,
383383
getValue.execute(inliningTarget, behavior, method, this, key);
384384
} else {
385385
try {
386-
sequenceDelItemNode.execute(this, PInt.intValueExact(key));
386+
sequenceDelItemNode.execute(null, inliningTarget, this, PInt.intValueExact(key));
387387
} catch (OverflowException cce) {
388388
throw InvalidArrayIndexException.create(key);
389389
} catch (PException pe) {

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

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,6 @@ private static boolean hasSequenceMethods(PythonManagedClass obj) {
118118
return (obj.getMethodsFlags() & MethodsFlags.SEQUENCE_METHODS) != 0;
119119
}
120120

121-
private static boolean hasMappingMethods(PythonManagedClass obj) {
122-
return (obj.getMethodsFlags() & MethodsFlags.MAPPING_METHODS) != 0;
123-
}
124-
125121
private static Object allocatePyAsyncMethods(PythonManagedClass obj, Object nullValue) {
126122
Object mem = CStructAccess.AllocateNode.allocUncached(CStructs.PyAsyncMethods);
127123
CStructAccess.WritePointerNode writePointerNode = CStructAccess.WritePointerNode.getUncached();
@@ -143,9 +139,7 @@ private static void writeGroupSlots(CFields groupField, TpSlots slots, WritePoin
143139
private static Object allocatePyMappingMethods(TpSlots slots, Object nullValue) {
144140
Object mem = CStructAccess.AllocateNode.allocUncached(CStructs.PyMappingMethods);
145141
CStructAccess.WritePointerNode writePointerNode = CStructAccess.WritePointerNode.getUncached();
146-
147142
writeGroupSlots(CFields.PyTypeObject__tp_as_mapping, slots, writePointerNode, mem, nullValue);
148-
149143
return mem;
150144
}
151145

@@ -176,24 +170,13 @@ private static Object allocatePyNumberMethods(PythonManagedClass obj, TpSlots sl
176170
writePointerNode.write(mem, CFields.PyNumberMethods__nb_negative, getSlot(obj, SlotMethodDef.NB_NEGATIVE));
177171
writePointerNode.write(mem, CFields.PyNumberMethods__nb_positive, getSlot(obj, SlotMethodDef.NB_POSITIVE));
178172
writePointerNode.write(mem, CFields.PyNumberMethods__nb_power, getSlot(obj, SlotMethodDef.NB_POWER));
179-
writePointerNode.write(mem, CFields.PyNumberMethods__nb_reserved, nullValue);
180173
return mem;
181174
}
182175

183-
private static Object allocatePySequenceMethods(PythonManagedClass obj, TpSlots slots, Object nullValue) {
176+
private static Object allocatePySequenceMethods(TpSlots slots, Object nullValue) {
184177
Object mem = CStructAccess.AllocateNode.allocUncached(CStructs.PyNumberMethods);
185178
CStructAccess.WritePointerNode writePointerNode = CStructAccess.WritePointerNode.getUncached();
186-
187179
writeGroupSlots(CFields.PyTypeObject__tp_as_sequence, slots, writePointerNode, mem, nullValue);
188-
189-
writePointerNode.write(mem, CFields.PySequenceMethods__was_sq_slice, nullValue);
190-
writePointerNode.write(mem, CFields.PySequenceMethods__was_sq_ass_slice, nullValue);
191-
// TODO populate sq_contains
192-
writePointerNode.write(mem, CFields.PySequenceMethods__sq_contains, nullValue);
193-
// TODO populate sq_inplace_concat
194-
writePointerNode.write(mem, CFields.PySequenceMethods__sq_inplace_concat, nullValue);
195-
// TODO populate sq_inplace_repeat
196-
writePointerNode.write(mem, CFields.PySequenceMethods__sq_inplace_repeat, nullValue);
197180
return mem;
198181
}
199182

@@ -275,7 +258,7 @@ static void initializeType(PythonClassNativeWrapper obj, Object mem, boolean hea
275258
}
276259
Object asAsync = hasAsyncMethods(clazz) ? allocatePyAsyncMethods(clazz, nullValue) : nullValue;
277260
Object asNumber = IsBuiltinClassExactProfile.profileClassSlowPath(clazz, PythonBuiltinClassType.PythonObject) ? nullValue : allocatePyNumberMethods(clazz, slots, nullValue);
278-
Object asSequence = (slots.has_as_sequence() || hasSequenceMethods(clazz)) ? allocatePySequenceMethods(clazz, slots, nullValue) : nullValue;
261+
Object asSequence = (slots.has_as_sequence() || hasSequenceMethods(clazz)) ? allocatePySequenceMethods(slots, nullValue) : nullValue;
279262
Object asMapping = slots.has_as_mapping() ? allocatePyMappingMethods(slots, nullValue) : nullValue;
280263
Object asBuffer = lookup(clazz, PyTypeObject__tp_as_buffer, HiddenAttr.AS_BUFFER);
281264
writeI64Node.write(mem, CFields.PyTypeObject__tp_weaklistoffset, weaklistoffset);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/hpy/jni/GraalHPyJNIContext.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -187,7 +187,6 @@
187187
import com.oracle.graal.python.nodes.PRaiseNode;
188188
import com.oracle.graal.python.nodes.attributes.LookupCallableSlotInMRONode;
189189
import com.oracle.graal.python.nodes.call.CallNode;
190-
import com.oracle.graal.python.nodes.call.special.CallTernaryMethodNode;
191190
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
192191
import com.oracle.graal.python.nodes.classes.IsSubtypeNodeGen;
193192
import com.oracle.graal.python.nodes.object.GetClassNode;
@@ -1926,7 +1925,8 @@ public int ctxSetItem(long hSequence, long hKey, long hValue) {
19261925
} else if (clazz == PythonBuiltinClassType.PList && PGuards.isInteger(key) && ctxListSetItem(receiver, ((Number) key).longValue(), hValue)) {
19271926
return 0;
19281927
}
1929-
return setItemGeneric(receiver, clazz, key, value);
1928+
PyObjectSetItem.executeUncached(receiver, key, value);
1929+
return 0;
19301930
} catch (PException e) {
19311931
HPyTransformExceptionToNativeNode.executeUncached(context, e);
19321932
// non-null value indicates an error
@@ -1948,7 +1948,8 @@ public int ctxSetItemi(long hSequence, long lidx, long hValue) {
19481948
return 0;
19491949
}
19501950
Object value = HPyAsPythonObjectNodeGen.getUncached().execute(hValue);
1951-
return setItemGeneric(receiver, clazz, lidx, value);
1951+
PyObjectSetItem.executeUncached(receiver, lidx, value);
1952+
return 0;
19521953
} catch (PException e) {
19531954
HPyTransformExceptionToNativeNode.executeUncached(context, e);
19541955
// non-null value indicates an error
@@ -1982,16 +1983,6 @@ private boolean ctxListSetItem(Object receiver, long lidx, long hValue) {
19821983
return false;
19831984
}
19841985

1985-
@TruffleBoundary
1986-
private static int setItemGeneric(Object receiver, Object clazz, Object key, Object value) {
1987-
Object setItemAttribute = LookupCallableSlotInMRONode.getUncached(SpecialMethodSlot.SetItem).execute(clazz);
1988-
if (setItemAttribute == PNone.NO_VALUE) {
1989-
throw PRaiseNode.raiseUncached(null, PythonBuiltinClassType.TypeError, ErrorMessages.OBJ_NOT_SUBSCRIPTABLE, receiver);
1990-
}
1991-
CallTernaryMethodNode.getUncached().execute(null, setItemAttribute, receiver, key, value);
1992-
return 0;
1993-
}
1994-
19951986
public int ctxNumberCheck(long handle) {
19961987
increment(HPyJNIUpcall.HPyNumberCheck);
19971988
if (GraalHPyBoxing.isBoxedDouble(handle) || GraalHPyBoxing.isBoxedInt(handle)) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/dict/DictBuiltins.java

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,12 @@
7878
import com.oracle.graal.python.lib.PyDictCheckNode;
7979
import com.oracle.graal.python.lib.PyDictSetDefault;
8080
import com.oracle.graal.python.lib.PyObjectGetIter;
81+
import com.oracle.graal.python.lib.PyObjectSetItem;
8182
import com.oracle.graal.python.nodes.ErrorMessages;
8283
import com.oracle.graal.python.nodes.PGuards;
8384
import com.oracle.graal.python.nodes.PRaiseNode;
8485
import com.oracle.graal.python.nodes.call.CallNode;
85-
import com.oracle.graal.python.nodes.call.special.CallTernaryMethodNode;
8686
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
87-
import com.oracle.graal.python.nodes.call.special.LookupSpecialMethodSlotNode;
8887
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
8988
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
9089
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
@@ -94,7 +93,6 @@
9493
import com.oracle.graal.python.nodes.function.builtins.PythonVarargsBuiltinNode;
9594
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
9695
import com.oracle.graal.python.nodes.object.BuiltinClassProfiles.IsBuiltinObjectProfile;
97-
import com.oracle.graal.python.nodes.object.GetClassNode;
9896
import com.oracle.graal.python.runtime.exception.PException;
9997
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
10098
import com.oracle.truffle.api.CompilerDirectives;
@@ -559,7 +557,6 @@ static Object error(Object self, Object[] args, PKeyword[] kwargs,
559557

560558
// fromkeys()
561559
@Builtin(name = "fromkeys", minNumOfPositionalArgs = 2, parameterNames = {"$cls", "iterable", "value"}, isClassmethod = true)
562-
@ImportStatic(SpecialMethodSlot.class)
563560
@GenerateNodeFactory
564561
public abstract static class FromKeysNode extends PythonTernaryBuiltinNode {
565562

@@ -578,30 +575,22 @@ static Object doKeys(VirtualFrame frame, Object cls, Object iterable, Object val
578575
@Bind("this") Node inliningTarget,
579576
@Cached PyObjectGetIter getIter,
580577
@Cached CallNode callCtor,
581-
@Cached GetClassNode getClassNode,
582-
@Cached(parameters = "SetItem") LookupSpecialMethodSlotNode lookupSetItem,
583-
@Cached CallTernaryMethodNode callSetItem,
578+
@Cached PyObjectSetItem setItem,
584579
@Cached GetNextNode nextNode,
585-
@Cached IsBuiltinObjectProfile errorProfile,
586-
@Cached PRaiseNode.Lazy raiseNode) {
580+
@Cached IsBuiltinObjectProfile errorProfile) {
587581
Object dict = callCtor.execute(frame, cls);
588582
Object val = value == PNone.NO_VALUE ? PNone.NONE : value;
589583
Object it = getIter.execute(frame, inliningTarget, iterable);
590-
Object setitemMethod = lookupSetItem.execute(frame, getClassNode.execute(inliningTarget, dict), dict);
591-
if (setitemMethod != PNone.NO_VALUE) {
592-
while (true) {
593-
try {
594-
Object key = nextNode.execute(frame, it);
595-
callSetItem.execute(frame, setitemMethod, dict, key, val);
596-
} catch (PException e) {
597-
e.expectStopIteration(inliningTarget, errorProfile);
598-
break;
599-
}
584+
while (true) {
585+
try {
586+
Object key = nextNode.execute(frame, it);
587+
setItem.execute(frame, inliningTarget, dict, key, val);
588+
} catch (PException e) {
589+
e.expectStopIteration(inliningTarget, errorProfile);
590+
break;
600591
}
601-
return dict;
602-
} else {
603-
throw raiseNode.get(inliningTarget).raise(TypeError, ErrorMessages.P_OBJ_DOES_NOT_SUPPORT_ITEM_ASSIGMENT, iterable);
604592
}
593+
return dict;
605594
}
606595

607596
protected static boolean isBuiltinDict(Node inliningTarget, Object cls, IsSameTypeNode isSameTypeNode) {

0 commit comments

Comments
 (0)