Skip to content

Commit afcda3c

Browse files
committed
Implement simple numeric binary inplace slots
1 parent e1f4c01 commit afcda3c

File tree

61 files changed

+1515
-638
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1515
-638
lines changed

graalpython/com.oracle.graal.python.annotations/src/com/oracle/graal/python/annotations/Slot.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,30 @@ enum SlotKind {
137137
nb_matrix_multiply("__matmul__, __rmatmul__"),
138138
/** foo ** bar */
139139
nb_power("__pow__, __rpow__"),
140+
/** foo += bar */
141+
nb_inplace_add("__iadd__"),
142+
/** foo -= bar */
143+
nb_inplace_subtract("__isub__"),
144+
/** foo *= bar */
145+
nb_inplace_multiply("__imul__"),
146+
/** foo %= bar */
147+
nb_inplace_remainder("__imod__"),
148+
/** foo <<= bar */
149+
nb_inplace_lshift("__ilshift__"),
150+
/** foo >>= bar */
151+
nb_inplace_rshift("__irshift__"),
152+
/** foo &= bar */
153+
nb_inplace_and("__iand__"),
154+
/** foo ^= bar */
155+
nb_inplace_xor("__ixor__"),
156+
/** foo |= bar */
157+
nb_inplace_or("__ior__"),
158+
/** foo //= bar */
159+
nb_inplace_floor_divide("__ifloordiv__"),
160+
/** foo /= bar */
161+
nb_inplace_true_divide("__itruediv__"),
162+
/** foo @= bar */
163+
nb_inplace_matrix_multiply("__imatmul__"),
140164
/** sequence length/size */
141165
sq_length("__len__"),
142166
/** sequence item: read element at index */

graalpython/com.oracle.graal.python.processor/src/com/oracle/graal/python/processor/SlotsMapping.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ static String getSlotBaseClass(Slot s) {
5656
case nb_add, nb_subtract, nb_multiply, nb_remainder, nb_divmod, nb_lshift, nb_rshift, nb_and, nb_xor, nb_or,
5757
nb_floor_divide, nb_true_divide, nb_matrix_multiply ->
5858
"TpSlotBinaryOp.TpSlotBinaryOpBuiltin";
59+
case nb_inplace_add, nb_inplace_subtract, nb_inplace_multiply, nb_inplace_remainder,
60+
nb_inplace_lshift, nb_inplace_rshift, nb_inplace_and, nb_inplace_xor, nb_inplace_or,
61+
nb_inplace_floor_divide, nb_inplace_true_divide, nb_inplace_matrix_multiply ->
62+
"TpSlotBinaryOp.TpSlotBinaryIOpBuiltin";
5963
case nb_power -> "TpSlotNbPower.TpSlotNbPowerBuiltin";
6064
case sq_concat -> "TpSlotBinaryFunc.TpSlotSqConcat";
6165
case sq_length, mp_length -> "TpSlotLen.TpSlotLenBuiltin" + getSuffix(s.isComplex());
@@ -76,7 +80,10 @@ static String getSlotNodeBaseClass(Slot s) {
7680
case nb_bool -> "com.oracle.graal.python.builtins.objects.type.slots.TpSlotInquiry.NbBoolBuiltinNode";
7781
case nb_index, nb_int, nb_float, nb_absolute, nb_positive, nb_negative, nb_invert -> "com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode";
7882
case nb_add, nb_subtract, nb_multiply, nb_remainder, nb_divmod, nb_lshift, nb_rshift, nb_and, nb_xor, nb_or,
79-
nb_floor_divide, nb_true_divide, nb_matrix_multiply ->
83+
nb_floor_divide, nb_true_divide, nb_matrix_multiply,
84+
nb_inplace_add, nb_inplace_subtract, nb_inplace_multiply, nb_inplace_remainder,
85+
nb_inplace_lshift, nb_inplace_rshift, nb_inplace_and, nb_inplace_xor, nb_inplace_or,
86+
nb_inplace_floor_divide, nb_inplace_true_divide, nb_inplace_matrix_multiply ->
8087
"com.oracle.graal.python.builtins.objects.type.slots.TpSlotBinaryOp.BinaryOpBuiltinNode";
8188
case nb_power -> "com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode";
8289
case sq_concat -> "com.oracle.graal.python.builtins.objects.type.slots.TpSlotBinaryFunc.SqConcatBuiltinNode";
@@ -146,6 +153,18 @@ public static String getExtraCtorArgs(TpSlotData slot) {
146153
case nb_floor_divide -> ", com.oracle.graal.python.nodes.SpecialMethodNames.J___FLOORDIV__";
147154
case nb_true_divide -> ", com.oracle.graal.python.nodes.SpecialMethodNames.J___TRUEDIV__";
148155
case nb_matrix_multiply -> ", com.oracle.graal.python.nodes.SpecialMethodNames.J___MATMUL__";
156+
case nb_inplace_add -> ", com.oracle.graal.python.nodes.SpecialMethodNames.J___IADD__";
157+
case nb_inplace_subtract -> ", com.oracle.graal.python.nodes.SpecialMethodNames.J___ISUB__";
158+
case nb_inplace_multiply -> ", com.oracle.graal.python.nodes.SpecialMethodNames.J___IMUL__";
159+
case nb_inplace_remainder -> ", com.oracle.graal.python.nodes.SpecialMethodNames.J___IMOD__";
160+
case nb_inplace_lshift -> ", com.oracle.graal.python.nodes.SpecialMethodNames.J___ILSHIFT__";
161+
case nb_inplace_rshift -> ", com.oracle.graal.python.nodes.SpecialMethodNames.J___IRSHIFT__";
162+
case nb_inplace_and -> ", com.oracle.graal.python.nodes.SpecialMethodNames.J___IAND__";
163+
case nb_inplace_xor -> ", com.oracle.graal.python.nodes.SpecialMethodNames.J___IXOR__";
164+
case nb_inplace_or -> ", com.oracle.graal.python.nodes.SpecialMethodNames.J___IOR__";
165+
case nb_inplace_floor_divide -> ", com.oracle.graal.python.nodes.SpecialMethodNames.J___IFLOORDIV__";
166+
case nb_inplace_true_divide -> ", com.oracle.graal.python.nodes.SpecialMethodNames.J___ITRUEDIV__";
167+
case nb_inplace_matrix_multiply -> ", com.oracle.graal.python.nodes.SpecialMethodNames.J___IMATMUL__";
149168
default -> "";
150169
};
151170
}

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_tp_slots.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -929,10 +929,9 @@ def __rmatmul__(self, other):
929929
obj = NativeNbSlotProxy(PureSlotProxy(3))
930930
obj /= 2
931931
assert obj.delegate.delegate == 1.5
932-
# TODO fix on graalpy
933-
# obj = NativeNbSlotProxy(PureSlotProxy(ObjWithMatmul()))
934-
# obj @= 1
935-
# assert obj.delegate.delegate == '@'
932+
obj = NativeNbSlotProxy(PureSlotProxy(ObjWithMatmul()))
933+
obj @= 1
934+
assert obj.delegate.delegate == '@'
936935

937936

938937
def test_sq_slot_calls():

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/PythonBuiltinClassType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@
160160
import com.oracle.graal.python.builtins.objects.property.PropertyBuiltins;
161161
import com.oracle.graal.python.builtins.objects.range.RangeBuiltins;
162162
import com.oracle.graal.python.builtins.objects.set.BaseSetBuiltins;
163+
import com.oracle.graal.python.builtins.objects.set.SetBuiltins;
163164
import com.oracle.graal.python.builtins.objects.str.StringBuiltins;
164165
import com.oracle.graal.python.builtins.objects.superobject.SuperBuiltins;
165166
import com.oracle.graal.python.builtins.objects.thread.ThreadLocalBuiltins;
@@ -263,7 +264,7 @@ public enum PythonBuiltinClassType implements TruffleObject {
263264
PReferenceType("ReferenceType", "_weakref"),
264265
PSentinelIterator("callable_iterator", Flags.PRIVATE_DERIVED_WODICT),
265266
PReverseIterator("reversed", J_BUILTINS),
266-
PSet("set", J_BUILTINS, SET_M_FLAGS, BaseSetBuiltins.SLOTS),
267+
PSet("set", J_BUILTINS, SET_M_FLAGS, TpSlots.merge(BaseSetBuiltins.SLOTS, SetBuiltins.SLOTS)),
267268
PSlice("slice", J_BUILTINS),
268269
PString("str", J_BUILTINS, STRING_M_FLAGS, StringBuiltins.SLOTS),
269270
PTraceback("traceback"),

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,7 +1619,7 @@ static Object minmaxSequenceWithKey(VirtualFrame frame, Node inliningTarget, Obj
16191619
isTrue = castToBooleanNode.execute(frame, e.getResult());
16201620
}
16211621
} else {
1622-
isTrue = castToBooleanNode.execute(frame, compare.executeObject(frame, nextKey, currentKey));
1622+
isTrue = castToBooleanNode.execute(frame, compare.execute(frame, nextKey, currentKey));
16231623
}
16241624
if (isTrue) {
16251625
currentKey = nextKey;
@@ -1663,7 +1663,7 @@ static Object minmaxBinaryWithKey(VirtualFrame frame, Node inliningTarget, Objec
16631663
isTrue = castToBooleanNode.execute(frame, e.getResult());
16641664
}
16651665
} else {
1666-
isTrue = castToBooleanNode.execute(frame, compare.executeObject(frame, nextKey, currentKey));
1666+
isTrue = castToBooleanNode.execute(frame, compare.execute(frame, nextKey, currentKey));
16671667
}
16681668
if (isTrue) {
16691669
currentKey = nextKey;
@@ -1683,7 +1683,7 @@ static Object minmaxBinaryWithKey(VirtualFrame frame, Node inliningTarget, Objec
16831683
isTrue = castToBooleanNode.execute(frame, e.getResult());
16841684
}
16851685
} else {
1686-
isTrue = castToBooleanNode.execute(frame, compare.executeObject(frame, nextKey, currentKey));
1686+
isTrue = castToBooleanNode.execute(frame, compare.execute(frame, nextKey, currentKey));
16871687
}
16881688
if (isTrue) {
16891689
currentKey = nextKey;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,7 @@ public static Object gcd(VirtualFrame frame, @SuppressWarnings("unused") Object
11581158
profile.profileCounted(args.length);
11591159
for (int i = 1; profile.inject(i < args.length); i++) {
11601160
Object b = indexNode.execute(frame, inliningTarget, args[i]);
1161-
if ((boolean) eqNode.executeObject(frame, a, 0)) {
1161+
if ((boolean) eqNode.execute(frame, a, 0)) {
11621162
continue;
11631163
}
11641164
Object g = gcdNode.execute(frame, a, b);
@@ -2488,7 +2488,7 @@ public Object doGeneric(VirtualFrame frame, Object iterable, Object startIn,
24882488
e.expectStopIteration(inliningTarget, errorProfile);
24892489
return value;
24902490
}
2491-
value = mul.executeObject(frame, value, nextValue);
2491+
value = mul.execute(frame, value, nextValue);
24922492
}
24932493
}
24942494
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3321,7 +3321,7 @@ static void doGeneric(VirtualFrame frame, Node inliningTarget, Object value, lon
33213321
@Cached(value = "createNotNormalized()", inline = false) GetItemNode getItemNode,
33223322
@Cached PyLongAsLongNode asLongNode,
33233323
@Cached PRaiseNode raiseNode) {
3324-
Object divmod = callDivmod.executeObject(frame, value, BILLION);
3324+
Object divmod = callDivmod.execute(frame, value, BILLION);
33253325
if (!PGuards.isPTuple(divmod) || lenNode.execute(inliningTarget, (PSequence) divmod) != 2) {
33263326
throw raiseNode.raise(inliningTarget, TypeError, ErrorMessages.MUST_RETURN_2TUPLE, value, divmod);
33273327
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ abstract static class PyTruffleNumber_BinOp extends CApiTernaryBuiltinNode {
332332
static Object doIntLikePrimitiveWrapper(Object left, Object right, @SuppressWarnings("unused") int op,
333333
@Cached("op") @SuppressWarnings("unused") int cachedOp,
334334
@Cached("createCallNode(op)") BinaryOpNode callNode) {
335-
return callNode.executeObject(null, left, right);
335+
return callNode.execute(null, left, right);
336336
}
337337

338338
/**
@@ -541,7 +541,7 @@ static Object repeat(Object obj, long n,
541541
@Bind("this") Node inliningTarget,
542542
@Cached PyObjectLookupAttr lookupNode,
543543
@Cached CallNode callNode,
544-
@Cached("createMul()") PyNumberMultiplyNode mulNode,
544+
@Cached PyNumberMultiplyNode mulNode,
545545
@SuppressWarnings("unused") @Exclusive @Cached PySequenceCheckNode checkNode) {
546546
Object imulCallable = lookupNode.execute(null, inliningTarget, obj, T___IMUL__);
547547
if (imulCallable != PNone.NO_VALUE) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/functools/KeyWrapperBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ BinaryComparisonNode createCmp() {
116116
boolean doCompare(VirtualFrame frame, PKeyWrapper self, PKeyWrapper other,
117117
@Cached PyObjectIsTrueNode isTrueNode) {
118118
final Object cmpResult = ensureCallNode().execute(frame, self.getCmp(), self.getObject(), other.getObject());
119-
return isTrueNode.execute(frame, ensureComparisonNode().executeObject(frame, cmpResult, 0));
119+
return isTrueNode.execute(frame, ensureComparisonNode().execute(frame, cmpResult, 0));
120120
}
121121

122122
@Fallback

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/array/ArrayBuiltins.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ static boolean cmpItems(VirtualFrame frame, Node inliningTarget, PArray left, PA
405405
Object leftValue = getLeft.execute(inliningTarget, left, i);
406406
Object rightValue = getRight.execute(inliningTarget, right, i);
407407
if (!eqNode.compare(frame, inliningTarget, leftValue, rightValue)) {
408-
return coerceToBooleanNode.execute(frame, compareNode.executeObject(frame, leftValue, rightValue));
408+
return coerceToBooleanNode.execute(frame, compareNode.execute(frame, leftValue, rightValue));
409409
}
410410
}
411411
return op.cmpResultToBool(left.getLength() - right.getLength());
@@ -422,7 +422,7 @@ static boolean cmpDoubles(VirtualFrame frame, Node inliningTarget, PArray left,
422422
double leftValue = (Double) getLeft.execute(inliningTarget, left, i);
423423
double rightValue = (Double) getRight.execute(inliningTarget, right, i);
424424
if (leftValue != rightValue) {
425-
return coerceToBooleanNode.execute(frame, compareNode.executeObject(frame, leftValue, rightValue));
425+
return coerceToBooleanNode.execute(frame, compareNode.execute(frame, leftValue, rightValue));
426426
}
427427
}
428428
return op.cmpResultToBool(left.getLength() - right.getLength());

0 commit comments

Comments
 (0)