ellipsis_op = custom_op(name='...', arg_types=[], result_type=object_rprimitive, error_kind=ERR_NEVER, emit=name_emit('Py_Ellipsis'), is_borrowed=True) 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) func_op(name='builtins.id', arg_types=[object_rprimitive], result_type=int_rprimitive, error_kind=ERR_NEVER, emit=call_emit('CPyTagged_Id')) iter_op = func_op(name='builtins.iter', arg_types=[object_rprimitive], result_type=object_rprimitive, error_kind=ERR_MAGIC, emit=call_emit('PyObject_GetIter')) 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'))
emit_multiply_helper(emitter, dest, args[1], args[0]) binary_op(op='*', arg_types=[list_rprimitive, int_rprimitive], result_type=list_rprimitive, error_kind=ERR_MAGIC, format_str='{dest} = {args[0]} * {args[1]} :: list', emit=emit_multiply) binary_op(op='*', arg_types=[int_rprimitive, list_rprimitive], result_type=list_rprimitive, error_kind=ERR_MAGIC, format_str='{dest} = {args[0]} * {args[1]} :: list', emit=emit_multiply_reversed) def emit_len(emitter: EmitterInterface, args: List[str], dest: str) -> None: temp = emitter.temp_name() emitter.emit_declaration('long long %s;' % temp) emitter.emit_line('%s = PyList_GET_SIZE(%s);' % (temp, args[0])) emitter.emit_line('%s = CPyTagged_ShortFromLongLong(%s);' % (dest, temp)) list_len_op = func_op(name='builtins.len', arg_types=[list_rprimitive], result_type=int_rprimitive, error_kind=ERR_NEVER, emit=emit_len)
arg_types=[dict_rprimitive, object_rprimitive, object_rprimitive], result_type=bool_rprimitive, error_kind=ERR_FALSE, emit=simple_emit( '{dest} = PyDict_SetItem({args[0]}, {args[1]}, {args[2]}) >= 0;')) binary_op( op='in', arg_types=[object_rprimitive, dict_rprimitive], result_type=bool_rprimitive, error_kind=ERR_MAGIC, format_str='{dest} = {args[0]} in {args[1]} :: dict', emit=negative_int_emit('{dest} = PyDict_Contains({args[1]}, {args[0]});')) # NOTE: PyDict_Update is technically not equivalent to update, but the cases where it # differs (when the second argument has no keys) should never typecheck for us, so the # difference is irrelevant. dict_update_op = method_op( name='update', arg_types=[dict_rprimitive, object_rprimitive], result_type=bool_rprimitive, error_kind=ERR_FALSE, emit=simple_emit('{dest} = PyDict_Update({args[0]}, {args[1]}) != -1;')) new_dict_op = func_op(name='builtins.dict', arg_types=[], result_type=dict_rprimitive, error_kind=ERR_MAGIC, format_str='{dest} = {{}}', emit=simple_emit('{dest} = PyDict_New();'))
method_op(name='get', arg_types=[dict_rprimitive, object_rprimitive, object_rprimitive], result_type=object_rprimitive, error_kind=ERR_MAGIC, emit=call_emit('CPyDict_Get')) method_op( name='get', arg_types=[dict_rprimitive, object_rprimitive], result_type=object_rprimitive, error_kind=ERR_MAGIC, emit=simple_emit('{dest} = CPyDict_Get({args[0]}, {args[1]}, Py_None);')) new_dict_op = func_op(name='builtins.dict', arg_types=[], result_type=dict_rprimitive, error_kind=ERR_MAGIC, format_str='{dest} = {{}}', emit=call_emit('PyDict_New')) def emit_len(emitter: EmitterInterface, args: List[str], dest: str) -> None: temp = emitter.temp_name() emitter.emit_declaration('long long %s;' % temp) emitter.emit_line('%s = PyDict_Size(%s);' % (temp, args[0])) emitter.emit_line('%s = CPyTagged_ShortFromLongLong(%s);' % (dest, temp)) func_op(name='builtins.len', arg_types=[dict_rprimitive], result_type=int_rprimitive, error_kind=ERR_NEVER,
) # These int constructors produce object_rprimitives that then need to be unboxed # I guess unboxing ourselves would save a check and branch though? # 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=simple_emit('{dest} = (PyObject *)&PyLong_Type;'), is_borrowed=True) # Convert from a float. We could do a bit better directly. func_op(name='builtins.int', arg_types=[float_rprimitive], result_type=object_rprimitive, error_kind=ERR_MAGIC, emit=call_emit('CPyLong_FromFloat'), priority=1) def int_binary_op(op: str, c_func_name: str, result_type: RType = int_rprimitive) -> None: binary_op(op=op, arg_types=[int_rprimitive, int_rprimitive], result_type=result_type, error_kind=ERR_NEVER, format_str='{dest} = {args[0]} %s {args[1]} :: int' % op, emit=call_emit(c_func_name))
from typing import List, Callable from mypyc.ops import (object_rprimitive, str_rprimitive, bool_rprimitive, ERR_MAGIC, EmitterInterface) from mypyc.ops_primitive import func_op, binary_op, simple_emit func_op(name='builtins.str', arg_types=[object_rprimitive], result_type=str_rprimitive, error_kind=ERR_MAGIC, emit=simple_emit('{dest} = PyObject_Str({args[0]});')) binary_op(op='+', arg_types=[str_rprimitive, str_rprimitive], result_type=str_rprimitive, error_kind=ERR_MAGIC, emit=simple_emit('{dest} = PyUnicode_Concat({args[0]}, {args[1]});')) def emit_str_compare( comparison: str) -> Callable[[EmitterInterface, List[str], str], None]: def emit(emitter: EmitterInterface, args: List[str], dest: str) -> None: temp = emitter.temp_name() emitter.emit_declaration('int %s;' % temp) emitter.emit_lines( '%s = PyUnicode_Compare(%s, %s);' % (temp, args[0], args[1]), 'if (%s == -1 && PyErr_Occurred())' % temp, ' %s = 2;' % dest, 'else', ' %s = (%s %s);' % (dest, temp, comparison)) return emit
method_op, custom_op, simple_emit, call_emit, call_negative_bool_emit, ) name_ref_op('builtins.list', result_type=object_rprimitive, error_kind=ERR_NEVER, emit=simple_emit('{dest} = (PyObject *)&PyList_Type;'), is_borrowed=True) func_op(name='builtins.list', arg_types=[object_rprimitive], result_type=list_rprimitive, error_kind=ERR_MAGIC, emit=call_emit('PySequence_List')) def emit_new(emitter: EmitterInterface, args: List[str], dest: str) -> None: # TODO: This would be better split into multiple smaller ops. emitter.emit_line('%s = PyList_New(%d); ' % (dest, len(args))) emitter.emit_line('if (likely(%s != NULL)) {' % dest) for i, arg in enumerate(args): emitter.emit_line('PyList_SET_ITEM(%s, %s, %s);' % (dest, i, arg)) emitter.emit_line('}') new_list_op = custom_op(arg_types=[object_rprimitive], result_type=list_rprimitive,
error_kind=ERR_MAGIC, steals=False, format_str='{dest} = ({comma_args}) :: tuple', emit=simple_emit('{dest} = PyTuple_Pack({num_args}{pre_comma_args});')) 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 = PyTuple_GET_SIZE(%s);' % (temp, args[0])) emitter.emit_line('%s = CPyTagged_ShortFromSsize_t(%s);' % (dest, temp)) tuple_len_op = func_op(name='builtins.len', arg_types=[tuple_rprimitive], result_type=int_rprimitive, error_kind=ERR_NEVER, emit=emit_len) list_tuple_op = func_op(name='builtins.tuple', arg_types=[list_rprimitive], result_type=tuple_rprimitive, error_kind=ERR_MAGIC, emit=call_emit('PyList_AsTuple'), priority=2) func_op(name='builtins.tuple', arg_types=[object_rprimitive], result_type=tuple_rprimitive, error_kind=ERR_MAGIC, emit=call_emit('PySequence_Tuple'))
"""Primitive set ops.""" from mypyc.ops_primitive import func_op, custom_op, simple_emit from mypyc.ops import object_rprimitive, bool_rprimitive, ERR_MAGIC, ERR_FALSE new_set_op = func_op(name='builtins.set', arg_types=[], result_type=object_rprimitive, error_kind=ERR_MAGIC, format_str='{dest} = set()', emit=simple_emit('{dest} = PySet_New(NULL);')) # This operation is only used during set literal generation. The first argument must be a set. # If sets get added as special types, this will become a method_op on set rprimitives. set_add_op = custom_op( arg_types=[object_rprimitive, object_rprimitive], result_type=bool_rprimitive, error_kind=ERR_FALSE, format_str='{dest} = {args[0]}.add({args[1]})', emit=simple_emit('{dest} = PySet_Add({args[0]}, {args[1]}) == 0;'))
error_kind=ERR_NEVER, emit=emit_none) true_op = name_ref_op('builtins.True', result_type=bool_rprimitive, error_kind=ERR_NEVER, emit=simple_emit('{dest} = 1;')) false_op = name_ref_op('builtins.False', result_type=bool_rprimitive, error_kind=ERR_NEVER, emit=simple_emit('{dest} = 0;')) iter_op = func_op(name='builtins.iter', arg_types=[object_rprimitive], result_type=object_rprimitive, error_kind=ERR_MAGIC, emit=simple_emit('{dest} = PyObject_GetIter({args[0]});')) # Although the error_kind is set to be ERR_NEVER, this can actually return NULL, and thus it must # be checked using Branch.IS_ERROR. next_op = func_op(name='builtins.next', arg_types=[object_rprimitive], result_type=object_rprimitive, error_kind=ERR_NEVER, emit=simple_emit('{dest} = PyIter_Next({args[0]});')) # # Fallback primitive operations that operate on 'object' operands #
"""Primitive set ops.""" from mypyc.ops_primitive import func_op, method_op, binary_op, simple_emit, negative_int_emit from mypyc.ops import ( object_rprimitive, bool_rprimitive, set_rprimitive, int_rprimitive, ERR_MAGIC, ERR_FALSE, ERR_NEVER, EmitterInterface ) from typing import List 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);') ) func_op( name='builtins.set', arg_types=[object_rprimitive], result_type=set_rprimitive, error_kind=ERR_MAGIC, emit=simple_emit('{dest} = PySet_New({args[0]});') ) def emit_len(emitter: EmitterInterface, args: List[str], dest: str) -> None: temp = emitter.temp_name() emitter.emit_declaration('long long %s;' % temp) emitter.emit_line('%s = PySet_GET_SIZE(%s);' % (temp, args[0])) emitter.emit_line('%s = CPyTagged_ShortFromLongLong(%s);' % (dest, temp))
emitter.emit_line('%s = CPyDict_Build(%s, %s);' % (dest, len(args) // 2, ', '.join(args))) new_dict_op = custom_op(name='builtins.dict', arg_types=[object_rprimitive], is_var_arg=True, result_type=dict_rprimitive, format_str='{dest} = {{{colon_args}}}', error_kind=ERR_MAGIC, emit=emit_new_dict) func_op(name='builtins.dict', arg_types=[dict_rprimitive], result_type=dict_rprimitive, error_kind=ERR_MAGIC, emit=call_emit('PyDict_Copy'), priority=2) func_op(name='builtins.dict', arg_types=[object_rprimitive], result_type=dict_rprimitive, error_kind=ERR_MAGIC, emit=call_emit('CPyDict_FromAny')) 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 = PyDict_Size(%s);' % (temp, args[0])) emitter.emit_line('%s = CPyTagged_ShortFromSsize_t(%s);' % (dest, temp))
from mypyc.ops_primitive import method_op, func_op, simple_emit tuple_get_item_op = method_op( name='__getitem__', arg_types=[tuple_rprimitive, int_rprimitive], result_type=object_rprimitive, error_kind=ERR_MAGIC, emit=simple_emit( '{dest} = CPySequenceTuple_GetItem({args[0]}, {args[1]});')) def emit_len(emitter: EmitterInterface, args: List[str], dest: str) -> None: temp = emitter.temp_name() emitter.emit_declaration('long long %s;' % temp) emitter.emit_line('%s = PyTuple_GET_SIZE(%s);' % (temp, args[0])) emitter.emit_line('%s = CPyTagged_ShortFromLongLong(%s);' % (dest, temp)) tuple_len_op = func_op(name='builtins.len', arg_types=[tuple_rprimitive], result_type=int_rprimitive, error_kind=ERR_NEVER, emit=emit_len) list_tuple_op = func_op( name='builtins.tuple', arg_types=[list_rprimitive], result_type=tuple_rprimitive, error_kind=ERR_MAGIC, emit=simple_emit('{dest} = PyList_AsTuple({args[0]});'))
from mypyc.ops_primitive import name_ref_op, binary_op, unary_op, func_op, simple_emit # These int constructors produce object_rprimitives that then need to be unboxed # I guess unboxing ourselves would save a check and branch though? # 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=simple_emit('{dest} = (PyObject *)&PyLong_Type;'), is_borrowed=True) # Convert from a float. We could do a bit better directly. func_op(name='builtins.int', arg_types=[float_rprimitive], result_type=object_rprimitive, error_kind=ERR_MAGIC, emit=simple_emit('{dest} = CPyLong_FromFloat({args[0]});'), priority=1) def int_binary_op(op: str, c_func_name: str, result_type: RType = int_rprimitive) -> None: binary_op(op=op, arg_types=[int_rprimitive, int_rprimitive], result_type=result_type, error_kind=ERR_NEVER, format_str='{dest} = {args[0]} %s {args[1]} :: int' % op, emit=simple_emit('{dest} = %s({args[0]}, {args[1]});' % c_func_name))