diff --git a/mypyc/irbuild/builder.py b/mypyc/irbuild/builder.py index 1a465fa65e91..a39dcad7b082 100644 --- a/mypyc/irbuild/builder.py +++ b/mypyc/irbuild/builder.py @@ -259,7 +259,7 @@ def gen_import(self, id: str, line: int) -> None: self.add_bool_branch(comparison, out, needs_import) self.activate_block(needs_import) - value = self.primitive_op(import_op, [self.load_static_unicode(id)], line) + value = self.call_c(import_op, [self.load_static_unicode(id)], line) self.add(InitStatic(value, id, namespace=NAMESPACE_MODULE)) self.goto_and_activate(out) diff --git a/mypyc/irbuild/callable_class.py b/mypyc/irbuild/callable_class.py index c0f845543f2c..009b36eab004 100644 --- a/mypyc/irbuild/callable_class.py +++ b/mypyc/irbuild/callable_class.py @@ -128,7 +128,7 @@ def add_get_to_callable_class(builder: IRBuilder, fn_info: FuncInfo) -> None: builder.add(Return(vself)) builder.activate_block(instance_block) - builder.add(Return(builder.primitive_op(method_new_op, [vself, builder.read(instance)], line))) + builder.add(Return(builder.call_c(method_new_op, [vself, builder.read(instance)], line))) blocks, env, _, fn_info = builder.leave() diff --git a/mypyc/irbuild/classdef.py b/mypyc/irbuild/classdef.py index 8b5f1da98d26..ef6527e445a0 100644 --- a/mypyc/irbuild/classdef.py +++ b/mypyc/irbuild/classdef.py @@ -505,7 +505,7 @@ def dataclass_finalize( """ finish_non_ext_dict(builder, non_ext, cdef.line) dec = builder.accept(next(d for d in cdef.decorators if is_dataclass_decorator(d))) - builder.primitive_op( + builder.call_c( dataclass_sleight_of_hand, [dec, type_obj, non_ext.dict, non_ext.anns], cdef.line) diff --git a/mypyc/irbuild/expression.py b/mypyc/irbuild/expression.py index c8db606eb52a..3e20951d491f 100644 --- a/mypyc/irbuild/expression.py +++ b/mypyc/irbuild/expression.py @@ -279,7 +279,7 @@ def translate_super_method_call(builder: IRBuilder, expr: CallExpr, callee: Supe if decl.kind != FUNC_STATICMETHOD: vself = next(iter(builder.environment.indexes)) # grab first argument if decl.kind == FUNC_CLASSMETHOD: - vself = builder.primitive_op(type_op, [vself], expr.line) + vself = builder.call_c(type_op, [vself], expr.line) elif builder.fn_info.is_generator: # For generator classes, the self target is the 6th value # in the symbol table (which is an ordered dict). This is sort @@ -570,7 +570,7 @@ def get_arg(arg: Optional[Expression]) -> Value: args = [get_arg(expr.begin_index), get_arg(expr.end_index), get_arg(expr.stride)] - return builder.primitive_op(new_slice_op, args, expr.line) + return builder.call_c(new_slice_op, args, expr.line) def transform_generator_expr(builder: IRBuilder, o: GeneratorExpr) -> Value: diff --git a/mypyc/irbuild/function.py b/mypyc/irbuild/function.py index ad42a12584ee..121f28088da7 100644 --- a/mypyc/irbuild/function.py +++ b/mypyc/irbuild/function.py @@ -493,7 +493,7 @@ def handle_yield_from_and_await(builder: IRBuilder, o: Union[YieldFromExpr, Awai if isinstance(o, YieldFromExpr): iter_val = builder.primitive_op(iter_op, [builder.accept(o.expr)], o.line) else: - iter_val = builder.primitive_op(coro_op, [builder.accept(o.expr)], o.line) + iter_val = builder.call_c(coro_op, [builder.accept(o.expr)], o.line) iter_reg = builder.maybe_spill_assignable(iter_val) @@ -504,7 +504,7 @@ def handle_yield_from_and_await(builder: IRBuilder, o: Union[YieldFromExpr, Awai # Try extracting a return value from a StopIteration and return it. # If it wasn't, this reraises the exception. builder.activate_block(stop_block) - builder.assign(result, builder.primitive_op(check_stop_op, [], o.line), o.line) + builder.assign(result, builder.call_c(check_stop_op, [], o.line), o.line) builder.goto(done_block) builder.activate_block(main_block) @@ -543,7 +543,7 @@ def except_body() -> None: def else_body() -> None: # Do a next() or a .send(). It will return NULL on exception # but it won't automatically propagate. - _y = builder.primitive_op( + _y = builder.call_c( send_op, [builder.read(iter_reg), builder.read(received_reg)], o.line ) ok, stop = BasicBlock(), BasicBlock() @@ -557,7 +557,7 @@ def else_body() -> None: # Try extracting a return value from a StopIteration and return it. # If it wasn't, this rereaises the exception. builder.activate_block(stop) - builder.assign(result, builder.primitive_op(check_stop_op, [], o.line), o.line) + builder.assign(result, builder.call_c(check_stop_op, [], o.line), o.line) builder.nonlocal_control[-1].gen_break(builder, o.line) builder.push_loop_stack(loop_block, done_block) diff --git a/mypyc/irbuild/statement.py b/mypyc/irbuild/statement.py index 184458b85f31..ecbfcd18ea9d 100644 --- a/mypyc/irbuild/statement.py +++ b/mypyc/irbuild/statement.py @@ -534,7 +534,7 @@ def transform_with(builder: IRBuilder, # We could probably optimize the case where the manager is compiled by us, # but that is not our common case at all, so. mgr_v = builder.accept(expr) - typ = builder.primitive_op(type_op, [mgr_v], line) + typ = builder.call_c(type_op, [mgr_v], line) exit_ = builder.maybe_spill(builder.py_get_attr(typ, '__exit__', line)) value = builder.py_call( builder.py_get_attr(typ, '__enter__', line), [mgr_v], line diff --git a/mypyc/primitives/misc_ops.py b/mypyc/primitives/misc_ops.py index c7c2dc03e3c7..bab25ef58a07 100644 --- a/mypyc/primitives/misc_ops.py +++ b/mypyc/primitives/misc_ops.py @@ -7,7 +7,7 @@ ) from mypyc.primitives.registry import ( name_ref_op, simple_emit, unary_op, func_op, custom_op, call_emit, name_emit, - call_negative_magic_emit, c_function_op + call_negative_magic_emit, c_function_op, c_custom_op ) @@ -61,22 +61,22 @@ error_kind=ERR_NEVER) # Return the result of obj.__await()__ or obj.__iter__() (if no __await__ exists) -coro_op = custom_op(name='get_coroutine_obj', - arg_types=[object_rprimitive], - result_type=object_rprimitive, - error_kind=ERR_MAGIC, - emit=call_emit('CPy_GetCoro')) +coro_op = c_custom_op( + arg_types=[object_rprimitive], + return_type=object_rprimitive, + c_function_name='CPy_GetCoro', + error_kind=ERR_MAGIC) # Do obj.send(value), or a next(obj) if second arg is None. # (This behavior is to match the PEP 380 spec for yield from.) # Like next_raw_op, don't swallow StopIteration, # but also don't propagate an error. # Can return NULL: see next_op. -send_op = custom_op(name='send', - arg_types=[object_rprimitive, object_rprimitive], - result_type=object_rprimitive, - error_kind=ERR_NEVER, - emit=call_emit('CPyIter_Send')) +send_op = c_custom_op( + arg_types=[object_rprimitive, object_rprimitive], + return_type=object_rprimitive, + c_function_name='CPyIter_Send', + error_kind=ERR_NEVER) # This is sort of unfortunate but oh well: yield_from_except performs most of the # error handling logic in `yield from` operations. It returns a bool and a value. @@ -96,20 +96,20 @@ emit=simple_emit('{dest}.f0 = CPy_YieldFromErrorHandle({args[0]}, &{dest}.f1);')) # Create method object from a callable object and self. -method_new_op = custom_op(name='method_new', - arg_types=[object_rprimitive, object_rprimitive], - result_type=object_rprimitive, - error_kind=ERR_MAGIC, - emit=call_emit('PyMethod_New')) +method_new_op = c_custom_op( + arg_types=[object_rprimitive, object_rprimitive], + return_type=object_rprimitive, + c_function_name='PyMethod_New', + error_kind=ERR_MAGIC) # Check if the current exception is a StopIteration and return its value if so. # Treats "no exception" as StopIteration with a None value. # If it is a different exception, re-reraise it. -check_stop_op = custom_op(name='check_stop_iteration', - arg_types=[], - result_type=object_rprimitive, - error_kind=ERR_MAGIC, - emit=call_emit('CPy_FetchStopIterationValue')) +check_stop_op = c_custom_op( + arg_types=[], + return_type=object_rprimitive, + c_function_name='CPy_FetchStopIterationValue', + error_kind=ERR_MAGIC) # Negate a primitive bool unary_op(op='not', @@ -133,12 +133,11 @@ ) # Import a module -import_op = custom_op( - name='import', +import_op = c_custom_op( arg_types=[str_rprimitive], - result_type=object_rprimitive, - error_kind=ERR_MAGIC, - emit=call_emit('PyImport_Import')) + return_type=object_rprimitive, + c_function_name='PyImport_Import', + error_kind=ERR_MAGIC) # Get the sys.modules dictionary get_module_dict_op = custom_op( @@ -186,20 +185,20 @@ emit=call_negative_magic_emit('PyObject_IsTrue')) # slice(start, stop, step) -new_slice_op = func_op( - 'builtins.slice', +new_slice_op = c_function_op( + name='builtins.slice', arg_types=[object_rprimitive, object_rprimitive, object_rprimitive], - result_type=object_rprimitive, - error_kind=ERR_MAGIC, - emit=call_emit('PySlice_New')) + c_function_name='PySlice_New', + return_type=object_rprimitive, + error_kind=ERR_MAGIC) # type(obj) -type_op = func_op( - 'builtins.type', +type_op = c_function_op( + name='builtins.type', arg_types=[object_rprimitive], - result_type=object_rprimitive, - error_kind=ERR_NEVER, - emit=call_emit('PyObject_Type')) + c_function_name='PyObject_Type', + return_type=object_rprimitive, + error_kind=ERR_NEVER) # Get 'builtins.type' (base class of all classes) type_object_op = name_ref_op( @@ -221,9 +220,8 @@ # Create a dataclass from an extension class. See # CPyDataclass_SleightOfHand for more docs. -dataclass_sleight_of_hand = custom_op( +dataclass_sleight_of_hand = c_custom_op( arg_types=[object_rprimitive, object_rprimitive, dict_rprimitive, dict_rprimitive], - result_type=bool_rprimitive, - error_kind=ERR_FALSE, - format_str='{dest} = dataclass_sleight_of_hand({comma_args})', - emit=call_emit('CPyDataclass_SleightOfHand')) + return_type=bool_rprimitive, + c_function_name='CPyDataclass_SleightOfHand', + error_kind=ERR_FALSE) diff --git a/mypyc/test-data/irbuild-basic.test b/mypyc/test-data/irbuild-basic.test index 307e9c85b858..96e39f2baadd 100644 --- a/mypyc/test-data/irbuild-basic.test +++ b/mypyc/test-data/irbuild-basic.test @@ -1585,7 +1585,7 @@ L0: if r2 goto L2 else goto L1 :: bool L1: r3 = unicode_0 :: static ('builtins') - r4 = import r3 :: str + r4 = PyImport_Import(r3) builtins = r4 :: module L2: r5 = __main__.globals :: static @@ -2636,7 +2636,7 @@ L0: if r2 goto L2 else goto L1 :: bool L1: r3 = unicode_0 :: static ('builtins') - r4 = import r3 :: str + r4 = PyImport_Import(r3) builtins = r4 :: module L2: r5 = typing :: module @@ -2645,7 +2645,7 @@ L2: if r7 goto L4 else goto L3 :: bool L3: r8 = unicode_1 :: static ('typing') - r9 = import r8 :: str + r9 = PyImport_Import(r8) typing = r9 :: module L4: r10 = typing :: module @@ -2854,7 +2854,7 @@ L0: L1: return __mypyc_self__ L2: - r2 = method_new __mypyc_self__, instance + r2 = PyMethod_New(__mypyc_self__, instance) return r2 def g_a_obj.__call__(__mypyc_self__): __mypyc_self__ :: __main__.g_a_obj @@ -2913,7 +2913,7 @@ L0: L1: return __mypyc_self__ L2: - r2 = method_new __mypyc_self__, instance + r2 = PyMethod_New(__mypyc_self__, instance) return r2 def g_b_obj.__call__(__mypyc_self__): __mypyc_self__ :: __main__.g_b_obj @@ -2972,7 +2972,7 @@ L0: L1: return __mypyc_self__ L2: - r2 = method_new __mypyc_self__, instance + r2 = PyMethod_New(__mypyc_self__, instance) return r2 def __mypyc_d_decorator_helper_____mypyc_c_decorator_helper___obj.__call__(__mypyc_self__): __mypyc_self__ :: __main__.__mypyc_d_decorator_helper_____mypyc_c_decorator_helper___obj @@ -3065,7 +3065,7 @@ L0: if r2 goto L2 else goto L1 :: bool L1: r3 = unicode_0 :: static ('builtins') - r4 = import r3 :: str + r4 = PyImport_Import(r3) builtins = r4 :: module L2: r5 = typing :: module @@ -3074,7 +3074,7 @@ L2: if r7 goto L4 else goto L3 :: bool L3: r8 = unicode_1 :: static ('typing') - r9 = import r8 :: str + r9 = PyImport_Import(r8) typing = r9 :: module L4: r10 = typing :: module @@ -3122,7 +3122,7 @@ L0: L1: return __mypyc_self__ L2: - r2 = method_new __mypyc_self__, instance + r2 = PyMethod_New(__mypyc_self__, instance) return r2 def g_a_obj.__call__(__mypyc_self__): __mypyc_self__ :: __main__.g_a_obj @@ -3191,7 +3191,7 @@ L0: if r2 goto L2 else goto L1 :: bool L1: r3 = unicode_0 :: static ('builtins') - r4 = import r3 :: str + r4 = PyImport_Import(r3) builtins = r4 :: module L2: r5 = typing :: module @@ -3200,7 +3200,7 @@ L2: if r7 goto L4 else goto L3 :: bool L3: r8 = unicode_1 :: static ('typing') - r9 = import r8 :: str + r9 = PyImport_Import(r8) typing = r9 :: module L4: r10 = typing :: module diff --git a/mypyc/test-data/irbuild-classes.test b/mypyc/test-data/irbuild-classes.test index 50a079955c7a..70764a663df2 100644 --- a/mypyc/test-data/irbuild-classes.test +++ b/mypyc/test-data/irbuild-classes.test @@ -372,7 +372,7 @@ L0: if r2 goto L2 else goto L1 :: bool L1: r3 = unicode_0 :: static ('builtins') - r4 = import r3 :: str + r4 = PyImport_Import(r3) builtins = r4 :: module L2: r5 = typing :: module @@ -381,7 +381,7 @@ L2: if r7 goto L4 else goto L3 :: bool L3: r8 = unicode_1 :: static ('typing') - r9 = import r8 :: str + r9 = PyImport_Import(r8) typing = r9 :: module L4: r10 = typing :: module @@ -400,7 +400,7 @@ L4: if r22 goto L6 else goto L5 :: bool L5: r23 = unicode_4 :: static ('mypy_extensions') - r24 = import r23 :: str + r24 = PyImport_Import(r23) mypy_extensions = r24 :: module L6: r25 = mypy_extensions :: module diff --git a/mypyc/test-data/irbuild-nested.test b/mypyc/test-data/irbuild-nested.test index 8eade905606d..45e7d957feb9 100644 --- a/mypyc/test-data/irbuild-nested.test +++ b/mypyc/test-data/irbuild-nested.test @@ -45,7 +45,7 @@ L0: L1: return __mypyc_self__ L2: - r2 = method_new __mypyc_self__, instance + r2 = PyMethod_New(__mypyc_self__, instance) return r2 def inner_a_obj.__call__(__mypyc_self__): __mypyc_self__ :: __main__.inner_a_obj @@ -83,7 +83,7 @@ L0: L1: return __mypyc_self__ L2: - r2 = method_new __mypyc_self__, instance + r2 = PyMethod_New(__mypyc_self__, instance) return r2 def second_b_first_obj.__call__(__mypyc_self__): __mypyc_self__ :: __main__.second_b_first_obj @@ -109,7 +109,7 @@ L0: L1: return __mypyc_self__ L2: - r2 = method_new __mypyc_self__, instance + r2 = PyMethod_New(__mypyc_self__, instance) return r2 def first_b_obj.__call__(__mypyc_self__): __mypyc_self__ :: __main__.first_b_obj @@ -154,7 +154,7 @@ L0: L1: return __mypyc_self__ L2: - r2 = method_new __mypyc_self__, instance + r2 = PyMethod_New(__mypyc_self__, instance) return r2 def inner_c_obj.__call__(__mypyc_self__, s): __mypyc_self__ :: __main__.inner_c_obj @@ -193,7 +193,7 @@ L0: L1: return __mypyc_self__ L2: - r2 = method_new __mypyc_self__, instance + r2 = PyMethod_New(__mypyc_self__, instance) return r2 def inner_d_obj.__call__(__mypyc_self__, s): __mypyc_self__ :: __main__.inner_d_obj @@ -288,7 +288,7 @@ L0: L1: return __mypyc_self__ L2: - r2 = method_new __mypyc_self__, instance + r2 = PyMethod_New(__mypyc_self__, instance) return r2 def inner_a_obj.__call__(__mypyc_self__): __mypyc_self__ :: __main__.inner_a_obj @@ -330,7 +330,7 @@ L0: L1: return __mypyc_self__ L2: - r2 = method_new __mypyc_self__, instance + r2 = PyMethod_New(__mypyc_self__, instance) return r2 def inner_b_obj.__call__(__mypyc_self__): __mypyc_self__ :: __main__.inner_b_obj @@ -376,7 +376,7 @@ L0: L1: return __mypyc_self__ L2: - r2 = method_new __mypyc_self__, instance + r2 = PyMethod_New(__mypyc_self__, instance) return r2 def inner_c_obj.__call__(__mypyc_self__): __mypyc_self__ :: __main__.inner_c_obj @@ -400,7 +400,7 @@ L0: L1: return __mypyc_self__ L2: - r2 = method_new __mypyc_self__, instance + r2 = PyMethod_New(__mypyc_self__, instance) return r2 def inner_c_obj_0.__call__(__mypyc_self__): __mypyc_self__ :: __main__.inner_c_obj_0 @@ -462,7 +462,7 @@ L0: L1: return __mypyc_self__ L2: - r2 = method_new __mypyc_self__, instance + r2 = PyMethod_New(__mypyc_self__, instance) return r2 def c_a_b_obj.__call__(__mypyc_self__): __mypyc_self__ :: __main__.c_a_b_obj @@ -488,7 +488,7 @@ L0: L1: return __mypyc_self__ L2: - r2 = method_new __mypyc_self__, instance + r2 = PyMethod_New(__mypyc_self__, instance) return r2 def b_a_obj.__call__(__mypyc_self__): __mypyc_self__ :: __main__.b_a_obj @@ -558,7 +558,7 @@ L0: L1: return __mypyc_self__ L2: - r2 = method_new __mypyc_self__, instance + r2 = PyMethod_New(__mypyc_self__, instance) return r2 def inner_f_obj.__call__(__mypyc_self__): __mypyc_self__ :: __main__.inner_f_obj @@ -582,7 +582,7 @@ L0: L1: return __mypyc_self__ L2: - r2 = method_new __mypyc_self__, instance + r2 = PyMethod_New(__mypyc_self__, instance) return r2 def inner_f_obj_0.__call__(__mypyc_self__): __mypyc_self__ :: __main__.inner_f_obj_0 @@ -651,7 +651,7 @@ L0: L1: return __mypyc_self__ L2: - r2 = method_new __mypyc_self__, instance + r2 = PyMethod_New(__mypyc_self__, instance) return r2 def foo_f_obj.__call__(__mypyc_self__): __mypyc_self__ :: __main__.foo_f_obj @@ -677,7 +677,7 @@ L0: L1: return __mypyc_self__ L2: - r2 = method_new __mypyc_self__, instance + r2 = PyMethod_New(__mypyc_self__, instance) return r2 def bar_f_obj.__call__(__mypyc_self__): __mypyc_self__ :: __main__.bar_f_obj @@ -703,7 +703,7 @@ L0: L1: return __mypyc_self__ L2: - r2 = method_new __mypyc_self__, instance + r2 = PyMethod_New(__mypyc_self__, instance) return r2 def baz_f_obj.__call__(__mypyc_self__, n): __mypyc_self__ :: __main__.baz_f_obj @@ -796,7 +796,7 @@ L0: L1: return __mypyc_self__ L2: - r2 = method_new __mypyc_self__, instance + r2 = PyMethod_New(__mypyc_self__, instance) return r2 def __mypyc_lambda__0_f_obj.__call__(__mypyc_self__, a, b): __mypyc_self__ :: __main__.__mypyc_lambda__0_f_obj @@ -818,7 +818,7 @@ L0: L1: return __mypyc_self__ L2: - r2 = method_new __mypyc_self__, instance + r2 = PyMethod_New(__mypyc_self__, instance) return r2 def __mypyc_lambda__1_f_obj.__call__(__mypyc_self__, a, b): __mypyc_self__ :: __main__.__mypyc_lambda__1_f_obj diff --git a/mypyc/test-data/irbuild-try.test b/mypyc/test-data/irbuild-try.test index 6183ab9de1da..f8b3bfca5c12 100644 --- a/mypyc/test-data/irbuild-try.test +++ b/mypyc/test-data/irbuild-try.test @@ -356,7 +356,7 @@ def foo(x): r29 :: None L0: r0 = py_call(x) - r1 = type r0 :: object + r1 = PyObject_Type(r0) r2 = unicode_3 :: static ('__exit__') r3 = getattr r1, r2 r4 = unicode_4 :: static ('__enter__')