Skip to content

Commit c96bf92

Browse files
committed
Make native binop operations at most ternary upcalls.
1 parent a3a9089 commit c96bf92

File tree

2 files changed

+53
-28
lines changed

2 files changed

+53
-28
lines changed

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -66,74 +66,74 @@ int PyNumber_Check(PyObject *o) {
6666
}
6767

6868
UPCALL_ID(PyNumber_UnaryOp);
69-
static PyObject * do_unaryop(PyObject *v, UnaryOp unaryop, char *unaryop_name) {
70-
return UPCALL_CEXT_O(_jls_PyNumber_UnaryOp, native_to_java(v), unaryop, polyglot_from_string(unaryop_name, SRC_CS));
69+
static PyObject * do_unaryop(PyObject *v, UnaryOp unaryop) {
70+
return UPCALL_CEXT_O(_jls_PyNumber_UnaryOp, native_to_java(v), unaryop);
7171
}
7272

7373
UPCALL_ID(PyNumber_BinOp);
74-
static PyObject * do_binop(PyObject *v, PyObject *w, BinOp binop, char *binop_name) {
75-
return UPCALL_CEXT_O(_jls_PyNumber_BinOp, native_to_java(v), native_to_java(w), binop, polyglot_from_string(binop_name, SRC_CS));
74+
static PyObject * do_binop(PyObject *v, PyObject *w, BinOp binop) {
75+
return UPCALL_CEXT_O(_jls_PyNumber_BinOp, native_to_java(v), native_to_java(w), binop);
7676
}
7777

7878
UPCALL_ID(PyNumber_InPlaceBinOp);
79-
static PyObject * do_inplace_binop(PyObject *v, PyObject *w, BinOp binop, char *binop_name) {
80-
return UPCALL_CEXT_O(_jls_PyNumber_InPlaceBinOp, native_to_java(v), native_to_java(w), binop, polyglot_from_string(binop_name, SRC_CS));
79+
static PyObject * do_inplace_binop(PyObject *v, PyObject *w, BinOp binop) {
80+
return UPCALL_CEXT_O(_jls_PyNumber_InPlaceBinOp, native_to_java(v), native_to_java(w), binop);
8181
}
8282

8383
PyObject * PyNumber_Add(PyObject *o1, PyObject *o2) {
84-
return do_binop(o1, o2, ADD, "+");
84+
return do_binop(o1, o2, ADD);
8585
}
8686

8787
PyObject * PyNumber_Subtract(PyObject *o1, PyObject *o2) {
88-
return do_binop(o1, o2, SUB, "-");
88+
return do_binop(o1, o2, SUB);
8989
}
9090

9191
PyObject * PyNumber_Multiply(PyObject *o1, PyObject *o2) {
92-
return do_binop(o1, o2, MUL, "*");
92+
return do_binop(o1, o2, MUL);
9393
}
9494

9595
PyObject * PyNumber_TrueDivide(PyObject *o1, PyObject *o2) {
96-
return do_binop(o1, o2, TRUEDIV, "/");
96+
return do_binop(o1, o2, TRUEDIV);
9797
}
9898

9999
PyObject * PyNumber_FloorDivide(PyObject *o1, PyObject *o2) {
100-
return do_binop(o1, o2, FLOORDIV, "//");
100+
return do_binop(o1, o2, FLOORDIV);
101101
}
102102

103103
PyObject * PyNumber_Remainder(PyObject *o1, PyObject *o2) {
104-
return do_binop(o1, o2, MOD, "%");
104+
return do_binop(o1, o2, MOD);
105105
}
106106

107107
PyObject * PyNumber_Lshift(PyObject *o1, PyObject *o2) {
108-
return do_binop(o1, o2, LSHIFT, "<<");
108+
return do_binop(o1, o2, LSHIFT);
109109
}
110110

111111
PyObject * PyNumber_Rshift(PyObject *o1, PyObject *o2) {
112-
return do_binop(o1, o2, RSHIFT, ">>");
112+
return do_binop(o1, o2, RSHIFT);
113113
}
114114

115115
PyObject * PyNumber_Or(PyObject *o1, PyObject *o2) {
116-
return do_binop(o1, o2, OR, "|");
116+
return do_binop(o1, o2, OR);
117117
}
118118

119119
PyObject * PyNumber_And(PyObject *o1, PyObject *o2) {
120-
return do_binop(o1, o2, AND, "&");
120+
return do_binop(o1, o2, AND);
121121
}
122122

123123
PyObject * PyNumber_Xor(PyObject *o1, PyObject *o2) {
124-
return do_binop(o1, o2, XOR, "^");
124+
return do_binop(o1, o2, XOR);
125125
}
126126

127127
PyObject * PyNumber_Positive(PyObject *o) {
128-
return do_unaryop(o, POS, "+");
128+
return do_unaryop(o, POS);
129129
}
130130

131131
PyObject * PyNumber_Negative(PyObject *o) {
132-
return do_unaryop(o, NEG, "-");
132+
return do_unaryop(o, NEG);
133133
}
134134

135135
PyObject * PyNumber_Invert(PyObject *o) {
136-
return do_unaryop(o, INVERT, "~");
136+
return do_unaryop(o, INVERT);
137137
}
138138

139139
UPCALL_ID(PyNumber_Index);
@@ -145,7 +145,7 @@ PyObject * PyNumber_Index(PyObject *o) {
145145
}
146146

147147
PyObject * PyNumber_InPlaceTrueDivide(PyObject *o1, PyObject *o2) {
148-
return do_inplace_binop(o1, o2, TRUEDIV, "/");
148+
return do_inplace_binop(o1, o2, TRUEDIV);
149149
}
150150

151151
Py_ssize_t PyNumber_AsSsize_t(PyObject *item, PyObject *err) {

graalpython/lib-graalpython/python_cext.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ def PyNumber_Check(v):
374374

375375

376376
@may_raise
377-
def PyNumber_BinOp(v, w, binop, name):
377+
def PyNumber_BinOp(v, w, binop):
378378
if binop == 0:
379379
return v + w
380380
elif binop == 1:
@@ -398,11 +398,36 @@ def PyNumber_BinOp(v, w, binop, name):
398398
elif binop == 10:
399399
return v % w
400400
else:
401-
raise SystemError("unknown binary operator %s" % name)
401+
raise SystemError("unknown binary operator (code=%s)" % binop)
402+
403+
404+
def _binop_name(binop):
405+
if binop == 0:
406+
return "+"
407+
elif binop == 1:
408+
return "-"
409+
elif binop == 2:
410+
return "*"
411+
elif binop == 3:
412+
return "/"
413+
elif binop == 4:
414+
return "<<"
415+
elif binop == 5:
416+
return ">>"
417+
elif binop == 6:
418+
return "|"
419+
elif binop == 7:
420+
return "&"
421+
elif binop == 8:
422+
return "^"
423+
elif binop == 9:
424+
return "//"
425+
elif binop == 10:
426+
return "%"
402427

403428

404429
@may_raise
405-
def PyNumber_InPlaceBinOp(v, w, binop, name):
430+
def PyNumber_InPlaceBinOp(v, w, binop):
406431
control = v
407432
if binop == 0:
408433
v += w
@@ -427,22 +452,22 @@ def PyNumber_InPlaceBinOp(v, w, binop, name):
427452
elif binop == 10:
428453
v %= w
429454
else:
430-
raise SystemError("unknown binary operator %s" % name)
455+
raise SystemError("unknown in-place binary operator (code=%s)" % binop)
431456
if control is not v:
432-
raise TypeError("unsupported operand type(s) for %s=: '%s' and '%s'" % (name, type(v), type(w)))
457+
raise TypeError("unsupported operand type(s) for %s=: '%s' and '%s'" % (_binop_name(binop), type(v), type(w)))
433458
return control
434459

435460

436461
@may_raise
437-
def PyNumber_UnaryOp(v, unaryop, name):
462+
def PyNumber_UnaryOp(v, unaryop):
438463
if unaryop == 0:
439464
return +v
440465
elif unaryop == 1:
441466
return -v
442467
elif unaryop == 2:
443468
return ~v
444469
else:
445-
raise SystemError("unknown unary operator %s" % name)
470+
raise SystemError("unknown unary operator (code=%s)" % unaryop)
446471

447472

448473
@may_raise

0 commit comments

Comments
 (0)