Skip to content

Commit d83e83a

Browse files
committed
add inplace true div
1 parent 2c2f86b commit d83e83a

File tree

6 files changed

+83
-1
lines changed

6 files changed

+83
-1
lines changed

graalpython/com.oracle.graal.python.cext/src/abstract.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
#include "capi.h"
4242

4343
typedef enum e_binop {
44-
ADD=0, SUB, MUL, TRUEDIV, LSHIFT, RSHIFT, OR, AND, XOR, FLOORDIV, MOD
44+
ADD=0, SUB, MUL, TRUEDIV, LSHIFT, RSHIFT, OR, AND, XOR, FLOORDIV, MOD,
45+
INPLACE_OFFSET,
4546
} BinOp;
4647

4748
typedef enum e_unaryop {
@@ -71,6 +72,10 @@ static PyObject * do_binop(PyObject *v, PyObject *w, BinOp binop, char *binop_na
7172
return UPCALL_CEXT_O("PyNumber_BinOp", native_to_java(v), native_to_java(w), binop, polyglot_from_string(binop_name, SRC_CS));
7273
}
7374

75+
static PyObject * do_inplace_binop(PyObject *v, PyObject *w, BinOp binop, char *binop_name) {
76+
return UPCALL_CEXT_O("PyNumber_InPlaceBinOp", native_to_java(v), native_to_java(w), binop, polyglot_from_string(binop_name, SRC_CS));
77+
}
78+
7479
PyObject * PyNumber_Add(PyObject *o1, PyObject *o2) {
7580
return do_binop(o1, o2, ADD, "+");
7681
}
@@ -134,6 +139,10 @@ PyObject * PyNumber_Index(PyObject *o) {
134139
return UPCALL_CEXT_O("PyNumber_Index", native_to_java(o));
135140
}
136141

142+
PyObject * PyNumber_InPlaceTrueDivide(PyObject *o1, PyObject *o2) {
143+
return do_inplace_binop(o1, o2, TRUEDIV, "/");
144+
}
145+
137146
Py_ssize_t PyNumber_AsSsize_t(PyObject *item, PyObject *err) {
138147
Py_ssize_t result;
139148
PyObject *runerr;

graalpython/com.oracle.graal.python.cext/src/capi.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,3 +597,7 @@ void* wrap_fastcall(_PyCFunctionFast fun, PyObject *self, PyObject **args, PyObj
597597
void* wrap_unsupported(void *fun, ...) {
598598
return NULL;
599599
}
600+
601+
int truffle_ptr_compare(void* x, void* y) {
602+
return x == y;
603+
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,4 +544,27 @@ public static SizeofWCharNode create() {
544544
return new SizeofWCharNode();
545545
}
546546
}
547+
548+
public static class IsNode extends CExtBaseNode {
549+
@CompilationFinal private TruffleObject isFunc = null;
550+
@Child Node executeNode = Message.createExecute(2).createNode();
551+
552+
public boolean execute(PythonNativeObject a, PythonNativeObject b) {
553+
try {
554+
return (int) ForeignAccess.sendExecute(executeNode, getNativeFunction(), a.object, b.object) != 0;
555+
} catch (UnsupportedTypeException | ArityException | UnsupportedMessageException e) {
556+
CompilerDirectives.transferToInterpreter();
557+
throw new IllegalStateException(NativeCAPISymbols.FUN_PTR_COMPARE + " didn't work!");
558+
}
559+
}
560+
561+
TruffleObject getNativeFunction() {
562+
if (isFunc == null) {
563+
CompilerDirectives.transferToInterpreterAndInvalidate();
564+
isFunc = (TruffleObject) getContext().getEnv().importSymbol(NativeCAPISymbols.FUN_PTR_COMPARE);
565+
}
566+
return isFunc;
567+
}
568+
569+
}
547570
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,5 @@ public abstract class NativeCAPISymbols {
5656
public static final String FUN_DEREF_HANDLE = "truffle_deref_handle_for_managed";
5757
public static final String FUN_GET_BYTE_ARRAY_TYPE_ID = "get_byte_array_typeid";
5858
public static final String FUN_GET_PTR_ARRAY_TYPE_ID = "get_ptr_array_typeid";
59+
public static final String FUN_PTR_COMPARE = "truffle_ptr_compare";
5960
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/expression/IsNode.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@
4040
*/
4141
package com.oracle.graal.python.nodes.expression;
4242

43+
import com.oracle.graal.python.builtins.objects.cext.CExtNodes;
44+
import com.oracle.graal.python.builtins.objects.cext.PythonNativeObject;
4345
import com.oracle.graal.python.builtins.objects.ints.PInt;
4446
import com.oracle.graal.python.nodes.PNode;
47+
import com.oracle.truffle.api.dsl.Cached;
4548
import com.oracle.truffle.api.dsl.Fallback;
4649
import com.oracle.truffle.api.dsl.Specialization;
4750

@@ -158,6 +161,16 @@ boolean doDD(double left, double right) {
158161
return left == right;
159162
}
160163

164+
protected static CExtNodes.IsNode getNativeIsNode() {
165+
return new CExtNodes.IsNode();
166+
}
167+
168+
@Specialization
169+
boolean doNative(PythonNativeObject left, PythonNativeObject right,
170+
@Cached("getNativeIsNode()") CExtNodes.IsNode isNode) {
171+
return isNode.execute(left, right);
172+
}
173+
161174
@Fallback
162175
boolean doGeneric(Object left, Object right) {
163176
return left == right;

graalpython/lib-graalpython/python_cext.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,38 @@ def PyNumber_BinOp(v, w, binop, name):
414414
raise SystemError("unknown binary operator %s" % name)
415415

416416

417+
@may_raise
418+
def PyNumber_InPlaceBinOp(v, w, binop, name):
419+
control = v
420+
if binop == 0:
421+
v += w
422+
elif binop == 1:
423+
v -= w
424+
elif binop == 2:
425+
v *= w
426+
elif binop == 3:
427+
v /= w
428+
elif binop == 4:
429+
v <<= w
430+
elif binop == 5:
431+
v >>= w
432+
elif binop == 6:
433+
v |= w
434+
elif binop == 7:
435+
v &= w
436+
elif binop == 8:
437+
v ^= w
438+
elif binop == 9:
439+
v //= w
440+
elif binop == 10:
441+
v %= w
442+
else:
443+
raise SystemError("unknown binary operator %s" % name)
444+
if control is not v:
445+
raise TypeError("unsupported operand type(s) for %s=: '%s' and '%s'" % (name, type(v), type(w)))
446+
return control
447+
448+
417449
@may_raise
418450
def PyNumber_UnaryOp(v, unaryop, name):
419451
if unaryop == 0:

0 commit comments

Comments
 (0)