list_rprimitive, object_rprimitive, bool_rprimitive, c_int_rprimitive, c_pyssize_t_rprimitive) from mypyc.primitives.registry import (load_address_op, c_function_op, c_binary_op, c_method_op, c_custom_op) # Get the 'builtins.list' type object. load_address_op(name='builtins.list', type=object_rprimitive, src='PyList_Type') # list(obj) to_list = c_function_op( name='builtins.list', arg_types=[object_rprimitive], return_type=list_rprimitive, c_function_name='PySequence_List', error_kind=ERR_MAGIC, ) new_list_op = c_custom_op(arg_types=[c_pyssize_t_rprimitive], return_type=list_rprimitive, c_function_name='PyList_New', error_kind=ERR_MAGIC) # list[index] (for an integer index) list_get_item_op = c_method_op(name='__getitem__', arg_types=[list_rprimitive, int_rprimitive], return_type=object_rprimitive, c_function_name='CPyList_GetItem', error_kind=ERR_MAGIC)
from mypyc.ir.ops import ERR_MAGIC, EmitterInterface, EmitCallback from mypyc.ir.rtypes import (RType, object_rprimitive, str_rprimitive, bool_rprimitive, int_rprimitive, list_rprimitive) from mypyc.primitives.registry import (binary_op, simple_emit, method_op, call_emit, c_method_op, c_binary_op, c_function_op, load_address_op) # Get the 'str' type object. load_address_op(name='builtins.str', type=object_rprimitive, src='PyUnicode_Type') # str(obj) c_function_op(name='builtins.str', arg_types=[object_rprimitive], return_type=str_rprimitive, c_function_name='PyObject_Str', error_kind=ERR_MAGIC) # str1 + str2 c_binary_op(name='+', arg_types=[str_rprimitive, str_rprimitive], return_type=str_rprimitive, c_function_name='PyUnicode_Concat', error_kind=ERR_MAGIC) # str.join(obj) c_method_op(name='join', arg_types=[str_rprimitive, object_rprimitive], return_type=str_rprimitive, c_function_name='PyUnicode_Join',
error_kind=ERR_NEG_INT, priority=0) # del obj1[obj2] c_method_op( name='__delitem__', arg_types=[object_rprimitive, object_rprimitive], return_type=c_int_rprimitive, c_function_name='PyObject_DelItem', error_kind=ERR_NEG_INT, priority=0) # hash(obj) c_function_op( name='builtins.hash', arg_types=[object_rprimitive], return_type=int_rprimitive, c_function_name='CPyObject_Hash', error_kind=ERR_MAGIC) # getattr(obj, attr) py_getattr_op = c_function_op( name='builtins.getattr', arg_types=[object_rprimitive, object_rprimitive], return_type=object_rprimitive, c_function_name='CPyObject_GetAttr', error_kind=ERR_MAGIC) # getattr(obj, attr, default) c_function_op( name='builtins.getattr', arg_types=[object_rprimitive, object_rprimitive, object_rprimitive],
from mypyc.ir.ops import ERR_MAGIC, ERR_FALSE, ERR_NEVER, ERR_NEG_INT, EmitterInterface from mypyc.ir.rtypes import (object_rprimitive, bool_rprimitive, set_rprimitive, int_rprimitive, c_int_rprimitive) from typing import List # Construct an empty set. new_set_op = func_op(name='builtins.set', arg_types=[], result_type=set_rprimitive, error_kind=ERR_MAGIC, emit=simple_emit('{dest} = PySet_New(NULL);')) # set(obj) c_function_op(name='builtins.set', arg_types=[object_rprimitive], return_type=set_rprimitive, c_function_name='PySet_New', error_kind=ERR_MAGIC) # frozenset(obj) c_function_op(name='builtins.frozenset', arg_types=[object_rprimitive], return_type=object_rprimitive, c_function_name='PyFrozenSet_New', error_kind=ERR_MAGIC) def emit_len(emitter: EmitterInterface, args: List[str], dest: str) -> None: temp = emitter.temp_name() emitter.emit_declaration('Py_ssize_t %s;' % temp) emitter.emit_line('%s = PySet_GET_SIZE(%s);' % (temp, args[0]))
"""Primitive float ops.""" from mypyc.ir.ops import ERR_MAGIC from mypyc.ir.rtypes import (str_rprimitive, float_rprimitive) from mypyc.primitives.registry import (c_function_op) # float(str) c_function_op(name='builtins.float', arg_types=[str_rprimitive], return_type=float_rprimitive, c_function_name='PyFloat_FromString', error_kind=ERR_MAGIC)
c_function_name='PyDict_New', error_kind=ERR_MAGIC) # Construct a dictionary from keys and values. # Positional argument is the number of key-value pairs # Variable arguments are (key1, value1, ..., keyN, valueN). dict_build_op = c_custom_op(arg_types=[c_pyssize_t_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. c_function_op(name='builtins.dict', arg_types=[dict_rprimitive], return_type=dict_rprimitive, c_function_name='PyDict_Copy', error_kind=ERR_MAGIC, priority=2) # Generic one-argument dict constructor: dict(obj) c_function_op(name='builtins.dict', arg_types=[object_rprimitive], return_type=dict_rprimitive, c_function_name='CPyDict_FromAny', error_kind=ERR_MAGIC) # dict.keys() c_method_op(name='keys', arg_types=[dict_rprimitive], return_type=object_rprimitive, c_function_name='CPyDict_KeysView',
result_type=object_rprimitive, error_kind=ERR_NEVER, emit=name_emit('Py_Ellipsis'), is_borrowed=True) # Get the boxed NotImplemented object not_implemented_op = name_ref_op(name='builtins.NotImplemented', result_type=object_rprimitive, error_kind=ERR_NEVER, emit=name_emit('Py_NotImplemented'), is_borrowed=True) # id(obj) c_function_op(name='builtins.id', arg_types=[object_rprimitive], return_type=int_rprimitive, c_function_name='CPyTagged_Id', 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')) # 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.
# These int constructors produce object_rprimitives that then need to be unboxed # I guess unboxing ourselves would save a check and branch though? # Get the type object for 'builtins.int'. # For ordinary calls to int() we use a name_ref to the type name_ref_op('builtins.int', result_type=object_rprimitive, error_kind=ERR_NEVER, emit=name_emit('&PyLong_Type', target_type='PyObject *'), is_borrowed=True) # Convert from a float to int. We could do a bit better directly. c_function_op(name='builtins.int', arg_types=[float_rprimitive], return_type=object_rprimitive, c_function_name='CPyLong_FromFloat', error_kind=ERR_MAGIC) # int(string) c_function_op(name='builtins.int', arg_types=[str_rprimitive], return_type=object_rprimitive, c_function_name='CPyLong_FromStr', error_kind=ERR_MAGIC) # int(string, base) c_function_op(name='builtins.int', arg_types=[str_rprimitive, int_rprimitive], return_type=object_rprimitive, c_function_name='CPyLong_FromStrWithBase',
src='_Py_NoneStruct') # Get the boxed object '...' ellipsis_op = load_address_op(name='...', type=object_rprimitive, src='_Py_EllipsisObject') # Get the boxed NotImplemented object not_implemented_op = load_address_op(name='builtins.NotImplemented', type=object_rprimitive, src='_Py_NotImplementedStruct') # id(obj) c_function_op(name='builtins.id', arg_types=[object_rprimitive], return_type=int_rprimitive, c_function_name='CPyTagged_Id', error_kind=ERR_NEVER) # Return the result of obj.__await()__ or obj.__iter__() (if no __await__ exists) 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 = c_custom_op(arg_types=[object_rprimitive, object_rprimitive],
name='__getitem__', arg_types=[tuple_rprimitive, int_rprimitive], return_type=object_rprimitive, c_function_name='CPySequenceTuple_GetItem', error_kind=ERR_MAGIC) # Construct a boxed tuple from items: (item1, item2, ...) new_tuple_op = c_custom_op( arg_types=[c_pyssize_t_rprimitive], return_type=tuple_rprimitive, c_function_name='PyTuple_Pack', error_kind=ERR_MAGIC, var_arg_type=object_rprimitive) # Construct tuple from a list. list_tuple_op = c_function_op( name='builtins.tuple', arg_types=[list_rprimitive], return_type=tuple_rprimitive, c_function_name='PyList_AsTuple', error_kind=ERR_MAGIC, priority=2) # Construct tuple from an arbitrary (iterable) object. c_function_op( name='builtins.tuple', arg_types=[object_rprimitive], return_type=tuple_rprimitive, c_function_name='PySequence_Tuple', error_kind=ERR_MAGIC)
"""Primitive set (and frozenset) ops.""" from mypyc.primitives.registry import ( c_function_op, c_method_op, c_binary_op ) from mypyc.ir.ops import ERR_MAGIC, ERR_FALSE, ERR_NEG_INT from mypyc.ir.rtypes import ( object_rprimitive, bool_rprimitive, set_rprimitive, c_int_rprimitive, pointer_rprimitive ) # Construct an empty set. new_set_op = c_function_op( name='builtins.set', arg_types=[], return_type=set_rprimitive, c_function_name='PySet_New', error_kind=ERR_MAGIC, extra_int_constants=[(0, pointer_rprimitive)]) # set(obj) c_function_op( name='builtins.set', arg_types=[object_rprimitive], return_type=set_rprimitive, c_function_name='PySet_New', error_kind=ERR_MAGIC) # frozenset(obj) c_function_op( name='builtins.frozenset',