Skip to content

Commit 60e484c

Browse files
authored
[mypyc] Merge generic call ops (#9316)
This PR merges three generic call ops.
1 parent d204479 commit 60e484c

10 files changed

+112
-108
lines changed

mypyc/codegen/emitfunc.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
BinaryIntOp, LoadMem, GetElementPtr, LoadAddress, ComparisonOp
1616
)
1717
from mypyc.ir.rtypes import (
18-
RType, RTuple, is_tagged, is_int32_rprimitive, is_int64_rprimitive, RStruct
18+
RType, RTuple, is_tagged, is_int32_rprimitive, is_int64_rprimitive, RStruct,
19+
is_pointer_rprimitive
1920
)
2021
from mypyc.ir.func_ir import FuncIR, FuncDecl, FUNC_STATICMETHOD, FUNC_CLASSMETHOD
2122
from mypyc.ir.class_ir import ClassIR
@@ -498,7 +499,10 @@ def label(self, label: BasicBlock) -> str:
498499

499500
def reg(self, reg: Value) -> str:
500501
if reg.name in self.const_int_regs:
501-
return str(self.const_int_regs[reg.name])
502+
val = self.const_int_regs[reg.name]
503+
if val == 0 and is_pointer_rprimitive(reg.type):
504+
return "NULL"
505+
return str(val)
502506
else:
503507
return self.emitter.reg(reg)
504508

mypyc/irbuild/ll_builder.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ def py_call(self,
241241
"""
242242
# If all arguments are positional, we can use py_call_op.
243243
if (arg_kinds is None) or all(kind == ARG_POS for kind in arg_kinds):
244-
return self.primitive_op(py_call_op, [function] + arg_values, line)
244+
return self.call_c(py_call_op, [function] + arg_values, line)
245245

246246
# Otherwise fallback to py_call_with_kwargs_op.
247247
assert arg_names is not None
@@ -278,7 +278,7 @@ def py_call(self,
278278

279279
kw_args_dict = self.make_dict(kw_arg_key_value_pairs, line)
280280

281-
return self.primitive_op(
281+
return self.call_c(
282282
py_call_with_kwargs_op, [function, pos_args_tuple, kw_args_dict], line)
283283

284284
def py_method_call(self,
@@ -291,7 +291,7 @@ def py_method_call(self,
291291
"""Call a Python method (non-native and slow)."""
292292
if (arg_kinds is None) or all(kind == ARG_POS for kind in arg_kinds):
293293
method_name_reg = self.load_static_unicode(method_name)
294-
return self.primitive_op(py_method_call_op, [obj, method_name_reg] + arg_values, line)
294+
return self.call_c(py_method_call_op, [obj, method_name_reg] + arg_values, line)
295295
else:
296296
method = self.py_get_attr(obj, method_name, line)
297297
return self.py_call(method, arg_values, line, arg_kinds=arg_kinds, arg_names=arg_names)

mypyc/primitives/generic_ops.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
"""
1111

1212
from mypyc.ir.ops import ERR_NEVER, ERR_MAGIC, ERR_NEG_INT
13-
from mypyc.ir.rtypes import object_rprimitive, int_rprimitive, bool_rprimitive, c_int_rprimitive
13+
from mypyc.ir.rtypes import (
14+
object_rprimitive, int_rprimitive, bool_rprimitive, c_int_rprimitive, pointer_rprimitive
15+
)
1416
from mypyc.primitives.registry import (
15-
binary_op, custom_op, call_emit, simple_emit,
16-
c_binary_op, c_unary_op, c_method_op, c_function_op, c_custom_op
17+
binary_op, simple_emit, c_binary_op, c_unary_op, c_method_op, c_function_op, c_custom_op
1718
)
1819

1920

@@ -186,32 +187,31 @@
186187

187188
# Call callable object with N positional arguments: func(arg1, ..., argN)
188189
# Arguments are (func, arg1, ..., argN).
189-
py_call_op = custom_op(
190-
arg_types=[object_rprimitive],
191-
result_type=object_rprimitive,
192-
is_var_arg=True,
190+
py_call_op = c_custom_op(
191+
arg_types=[],
192+
return_type=object_rprimitive,
193+
c_function_name='PyObject_CallFunctionObjArgs',
193194
error_kind=ERR_MAGIC,
194-
format_str='{dest} = py_call({comma_args})',
195-
emit=simple_emit('{dest} = PyObject_CallFunctionObjArgs({comma_args}, NULL);'))
195+
var_arg_type=object_rprimitive,
196+
extra_int_constant=(0, pointer_rprimitive))
196197

197198
# Call callable object with positional + keyword args: func(*args, **kwargs)
198199
# Arguments are (func, *args tuple, **kwargs dict).
199-
py_call_with_kwargs_op = custom_op(
200+
py_call_with_kwargs_op = c_custom_op(
200201
arg_types=[object_rprimitive, object_rprimitive, object_rprimitive],
201-
result_type=object_rprimitive,
202-
error_kind=ERR_MAGIC,
203-
format_str='{dest} = py_call_with_kwargs({args[0]}, {args[1]}, {args[2]})',
204-
emit=call_emit('PyObject_Call'))
202+
return_type=object_rprimitive,
203+
c_function_name='PyObject_Call',
204+
error_kind=ERR_MAGIC)
205205

206206
# Call method with positional arguments: obj.method(arg1, ...)
207207
# Arguments are (object, attribute name, arg1, ...).
208-
py_method_call_op = custom_op(
209-
arg_types=[object_rprimitive],
210-
result_type=object_rprimitive,
211-
is_var_arg=True,
208+
py_method_call_op = c_custom_op(
209+
arg_types=[],
210+
return_type=object_rprimitive,
211+
c_function_name='CPyObject_CallMethodObjArgs',
212212
error_kind=ERR_MAGIC,
213-
format_str='{dest} = py_method_call({comma_args})',
214-
emit=simple_emit('{dest} = CPyObject_CallMethodObjArgs({comma_args}, NULL);'))
213+
var_arg_type=object_rprimitive,
214+
extra_int_constant=(0, pointer_rprimitive))
215215

216216
# len(obj)
217217
generic_len_op = c_custom_op(

mypyc/test-data/exceptions.test

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ L1:
192192
r2 = CPyObject_GetAttr(r0, r1)
193193
if is_error(r2) goto L3 (error at g:3) else goto L2
194194
L2:
195-
r3 = py_call(r2)
195+
r3 = PyObject_CallFunctionObjArgs(r2, 0)
196196
dec_ref r2
197197
if is_error(r3) goto L3 (error at g:3) else goto L10
198198
L3:
@@ -203,7 +203,7 @@ L3:
203203
r8 = CPyObject_GetAttr(r6, r7)
204204
if is_error(r8) goto L6 (error at g:5) else goto L4
205205
L4:
206-
r9 = py_call(r8, r5)
206+
r9 = PyObject_CallFunctionObjArgs(r8, r5, 0)
207207
dec_ref r8
208208
if is_error(r9) goto L6 (error at g:5) else goto L11
209209
L5:
@@ -260,7 +260,7 @@ L1:
260260
r2 = CPyObject_GetAttr(r0, r1)
261261
if is_error(r2) goto L5 (error at a:3) else goto L2
262262
L2:
263-
r3 = py_call(r2)
263+
r3 = PyObject_CallFunctionObjArgs(r2, 0)
264264
dec_ref r2
265265
if is_error(r3) goto L5 (error at a:3) else goto L20
266266
L3:
@@ -283,7 +283,7 @@ L6:
283283
r14 = CPyObject_GetAttr(r12, r13)
284284
if is_error(r14) goto L13 (error at a:6) else goto L7
285285
L7:
286-
r15 = py_call(r14, r11)
286+
r15 = PyObject_CallFunctionObjArgs(r14, r11, 0)
287287
dec_ref r14
288288
if is_error(r15) goto L13 (error at a:6) else goto L21
289289
L8:
@@ -494,7 +494,7 @@ L5:
494494
L6:
495495
unreachable
496496
L7:
497-
r7 = py_call(r6, v)
497+
r7 = PyObject_CallFunctionObjArgs(r6, v, 0)
498498
dec_ref r6
499499
xdec_ref v
500500
if is_error(r7) goto L9 (error at f:7) else goto L14

0 commit comments

Comments
 (0)