Skip to content

[mypyc] Support var arg in CallC, replace new_dict_op #8948

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 9, 2020
Prev Previous commit
Next Next commit
support var_arg in CallC, add dict_new_op and dict_build_op
  • Loading branch information
TH3CHARLie committed Jun 4, 2020
commit b6264ee5c6ce2c10cd652ed1b073921940744f10
4 changes: 3 additions & 1 deletion mypyc/ir/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1151,13 +1151,15 @@ def __init__(self,
ret_type: RType,
steals: StealsDescription,
error_kind: int,
line: int) -> None:
line: int,
var_arg_idx: int = -1) -> None:
self.error_kind = error_kind
super().__init__(line)
self.function_name = function_name
self.args = args
self.type = ret_type
self.steals = steals
self.var_arg_idx = var_arg_idx

def to_str(self, env: Environment) -> str:
args_str = ', '.join(env.format('%r', arg) for arg in self.args)
Expand Down
10 changes: 5 additions & 5 deletions mypyc/irbuild/classdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
dataclass_sleight_of_hand, pytype_from_template_op, py_calc_meta_op, type_object_op,
not_implemented_op, true_op
)
from mypyc.primitives.dict_ops import dict_set_item_op, new_dict_op
from mypyc.primitives.dict_ops import dict_set_item_op, dict_new_op
from mypyc.primitives.tuple_ops import new_tuple_op
from mypyc.common import SELF_NAME
from mypyc.irbuild.util import (
Expand Down Expand Up @@ -73,7 +73,7 @@ def transform_class_def(builder: IRBuilder, cdef: ClassDef) -> None:
# We populate __annotations__ for non-extension classes
# because dataclasses uses it to determine which attributes to compute on.
# TODO: Maybe generate more precise types for annotations
non_ext_anns = builder.primitive_op(new_dict_op, [], cdef.line)
non_ext_anns = builder.call_c(dict_new_op, [], cdef.line)
non_ext = NonExtClassInfo(non_ext_dict, non_ext_bases, non_ext_anns, non_ext_metaclass)
dataclass_non_ext = None
type_obj = None
Expand Down Expand Up @@ -258,7 +258,7 @@ def setup_non_ext_dict(builder: IRBuilder,
builder.goto(exit_block)

builder.activate_block(false_block)
builder.assign(non_ext_dict, builder.primitive_op(new_dict_op, [], cdef.line), cdef.line)
builder.assign(non_ext_dict, builder.call_c(dict_new_op, [], cdef.line), cdef.line)
builder.goto(exit_block)
builder.activate_block(exit_block)

Expand Down Expand Up @@ -518,9 +518,9 @@ def dataclass_non_ext_info(builder: IRBuilder, cdef: ClassDef) -> Optional[NonEx
"""
if is_dataclass(cdef):
return NonExtClassInfo(
builder.primitive_op(new_dict_op, [], cdef.line),
builder.call_c(dict_new_op, [], cdef.line),
builder.add(TupleSet([], cdef.line)),
builder.primitive_op(new_dict_op, [], cdef.line),
builder.call_c(dict_new_op, [], cdef.line),
builder.primitive_op(type_object_op, [], cdef.line),
)
else:
Expand Down
4 changes: 2 additions & 2 deletions mypyc/irbuild/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from mypyc.primitives.misc_ops import new_slice_op, ellipsis_op, type_op
from mypyc.primitives.list_ops import new_list_op, list_append_op, list_extend_op
from mypyc.primitives.tuple_ops import list_tuple_op
from mypyc.primitives.dict_ops import new_dict_op, dict_set_item_op
from mypyc.primitives.dict_ops import dict_new_op, dict_set_item_op
from mypyc.primitives.set_ops import new_set_op, set_add_op, set_update_op
from mypyc.irbuild.specialize import specializers
from mypyc.irbuild.builder import IRBuilder
Expand Down Expand Up @@ -541,7 +541,7 @@ def gen_inner_stmts() -> None:


def transform_dictionary_comprehension(builder: IRBuilder, o: DictionaryComprehension) -> Value:
d = builder.primitive_op(new_dict_op, [], o.line)
d = builder.call_c(dict_new_op, [], o.line)
loop_params = list(zip(o.indices, o.sequences, o.condlists))

def gen_inner_stmts() -> None:
Expand Down
14 changes: 12 additions & 2 deletions mypyc/irbuild/ll_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,12 +681,22 @@ def call_c(self,
result_type: Optional[RType] = None) -> Value:
# handle void function via singleton RVoid instance
coerced = []
for i, arg in enumerate(args):
# coerce fixed number arguments
for i in range(min(len(args), len(desc.arg_types))):
formal_type = desc.arg_types[i]
arg = args[i]
arg = self.coerce(arg, formal_type, line)
coerced.append(arg)
# coerce any var_arg
var_arg_idx = -1
if desc.var_arg_type is not None:
var_arg_idx = len(desc.arg_types)
for i in range(len(desc.arg_types), len(args)):
arg = args[i]
arg = self.coerce(arg, desc.var_arg_type, line)
coerced.append(arg)
target = self.add(CallC(desc.c_function_name, coerced, desc.return_type, desc.steals,
desc.error_kind, line))
desc.error_kind, line, var_arg_idx))
if result_type and not is_runtime_subtype(target.type, result_type):
if is_none_rprimitive(result_type):
# Special case None return. The actual result may actually be a bool
Expand Down
15 changes: 14 additions & 1 deletion mypyc/primitives/dict_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from mypyc.primitives.registry import (
name_ref_op, method_op, binary_op, func_op, custom_op,
simple_emit, negative_int_emit, call_emit, call_negative_bool_emit,
name_emit,
name_emit, c_custom_op
)


Expand Down Expand Up @@ -108,6 +108,19 @@ def emit_new_dict(emitter: EmitterInterface, args: List[str], dest: str) -> None
error_kind=ERR_MAGIC,
emit=emit_new_dict)

dict_new_op = c_custom_op(
arg_types=[],
return_type=dict_rprimitive,
c_function_name='PyDict_New',
error_kind=ERR_MAGIC)

dict_build_op = c_custom_op(
arg_types=[int_rprimitive],
return_type=dict_rprimitive,
c_function_name='CPyDict_Build',
error_kind=ERR_MAGIC,
var_arg_type=object_rprimitive,)

# Construct a dictionary from another dictionary.
func_op(
name='builtins.dict',
Expand Down
13 changes: 9 additions & 4 deletions mypyc/primitives/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
'CFunctionDescription', [('name', str),
('arg_types', List[RType]),
('return_type', RType),
('var_arg_type', Optional[RType]),
('c_function_name', str),
('error_kind', int),
('steals', StealsDescription),
Expand Down Expand Up @@ -334,10 +335,11 @@ def c_method_op(name: str,
return_type: RType,
c_function_name: str,
error_kind: int,
var_arg_type: Optional[RType] = None,
steals: StealsDescription = False,
priority: int = 1) -> CFunctionDescription:
ops = c_method_call_ops.setdefault(name, [])
desc = CFunctionDescription(name, arg_types, return_type,
desc = CFunctionDescription(name, arg_types, return_type, var_arg_type,
c_function_name, error_kind, steals, priority)
ops.append(desc)
return desc
Expand All @@ -348,10 +350,11 @@ def c_function_op(name: str,
return_type: RType,
c_function_name: str,
error_kind: int,
var_arg_type: Optional[RType] = None,
steals: StealsDescription = False,
priority: int = 1) -> CFunctionDescription:
ops = c_function_ops.setdefault(name, [])
desc = CFunctionDescription(name, arg_types, return_type,
desc = CFunctionDescription(name, arg_types, return_type, var_arg_type,
c_function_name, error_kind, steals, priority)
ops.append(desc)
return desc
Expand All @@ -362,10 +365,11 @@ def c_binary_op(name: str,
return_type: RType,
c_function_name: str,
error_kind: int,
var_arg_type: Optional[RType] = None,
steals: StealsDescription = False,
priority: int = 1) -> CFunctionDescription:
ops = c_binary_ops.setdefault(name, [])
desc = CFunctionDescription(name, arg_types, return_type,
desc = CFunctionDescription(name, arg_types, return_type, var_arg_type,
c_function_name, error_kind, steals, priority)
ops.append(desc)
return desc
Expand All @@ -375,8 +379,9 @@ def c_custom_op(arg_types: List[RType],
return_type: RType,
c_function_name: str,
error_kind: int,
var_arg_type: Optional[RType] = None,
steals: StealsDescription = False) -> CFunctionDescription:
return CFunctionDescription('<custom>', arg_types, return_type,
return CFunctionDescription('<custom>', arg_types, return_type, var_arg_type,
c_function_name, error_kind, steals, 0)


Expand Down
1 change: 1 addition & 0 deletions mypyc/primitives/str_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
return_type=str_rprimitive,
c_function_name='CPyStr_Append',
error_kind=ERR_MAGIC,
var_arg_type=None,
steals=[True, False])


Expand Down
7 changes: 4 additions & 3 deletions mypyc/test/test_emitfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from mypyc.ir.ops import (
Environment, BasicBlock, Goto, Return, LoadInt, Assign, IncRef, DecRef, Branch,
Call, Unbox, Box, TupleGet, GetAttr, PrimitiveOp, RegisterOp,
SetAttr, Op, Value
SetAttr, Op, Value, CallC
)
from mypyc.ir.rtypes import (
RTuple, RInstance, int_rprimitive, bool_rprimitive, list_rprimitive,
Expand All @@ -25,7 +25,7 @@
list_len_op, list_get_item_op, list_set_item_op, new_list_op, list_append_op
)
from mypyc.primitives.dict_ops import (
new_dict_op, dict_update_op, dict_get_item_op, dict_set_item_op
dict_new_op, dict_update_op, dict_get_item_op, dict_set_item_op
)
from mypyc.primitives.int_ops import int_neg_op
from mypyc.subtype import is_subtype
Expand Down Expand Up @@ -221,7 +221,8 @@ def test_dict_update(self) -> None:
"""cpy_r_r0 = CPyDict_Update(cpy_r_d, cpy_r_o) >= 0;""")

def test_new_dict(self) -> None:
self.assert_emit(PrimitiveOp([], new_dict_op, 1),
self.assert_emit(CallC(dict_new_op.c_function_name, [], dict_new_op.return_type,
dict_new_op.steals, dict_new_op.error_kind, 1),
"""cpy_r_r0 = PyDict_New();""")

def test_dict_contains(self) -> None:
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy