Skip to content

Commit

Permalink
merge some misc ops (python#9224)
Browse files Browse the repository at this point in the history
  • Loading branch information
TH3CHARLie authored Jul 28, 2020
1 parent 997b5f2 commit fba6387
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 84 deletions.
2 changes: 1 addition & 1 deletion mypyc/irbuild/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion mypyc/irbuild/callable_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion mypyc/irbuild/classdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
4 changes: 2 additions & 2 deletions mypyc/irbuild/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions mypyc/irbuild/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion mypyc/irbuild/statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
80 changes: 39 additions & 41 deletions mypyc/primitives/misc_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)


Expand Down Expand Up @@ -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.
Expand All @@ -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',
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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)
22 changes: 11 additions & 11 deletions mypyc/test-data/irbuild-basic.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions mypyc/test-data/irbuild-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Loading

0 comments on commit fba6387

Please sign in to comment.