|
| 1 | +"""Fallback primitive operations that operate on 'object' operands. |
| 2 | +
|
| 3 | +These just call the relevant Python C API function or a thin wrapper |
| 4 | +around an API function. Most of these also have faster, specialized |
| 5 | +ops that operate on some more specific types. |
| 6 | +""" |
| 7 | + |
| 8 | +from mypyc.ir.ops import ERR_NEVER, ERR_MAGIC, ERR_FALSE |
| 9 | +from mypyc.ir.rtypes import object_rprimitive, int_rprimitive, bool_rprimitive |
| 10 | +from mypyc.primitives.registry import ( |
| 11 | + binary_op, unary_op, func_op, method_op, custom_op, call_emit, simple_emit, |
| 12 | + call_negative_bool_emit, call_negative_magic_emit, negative_int_emit |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +for op, opid in [('==', 'Py_EQ'), |
| 17 | + ('!=', 'Py_NE'), |
| 18 | + ('<', 'Py_LT'), |
| 19 | + ('<=', 'Py_LE'), |
| 20 | + ('>', 'Py_GT'), |
| 21 | + ('>=', 'Py_GE')]: |
| 22 | + # The result type is 'object' since that's what PyObject_RichCompare returns. |
| 23 | + binary_op(op=op, |
| 24 | + arg_types=[object_rprimitive, object_rprimitive], |
| 25 | + result_type=object_rprimitive, |
| 26 | + error_kind=ERR_MAGIC, |
| 27 | + emit=simple_emit('{dest} = PyObject_RichCompare({args[0]}, {args[1]}, %s);' % opid), |
| 28 | + priority=0) |
| 29 | + |
| 30 | +for op, funcname in [('+', 'PyNumber_Add'), |
| 31 | + ('-', 'PyNumber_Subtract'), |
| 32 | + ('*', 'PyNumber_Multiply'), |
| 33 | + ('//', 'PyNumber_FloorDivide'), |
| 34 | + ('/', 'PyNumber_TrueDivide'), |
| 35 | + ('%', 'PyNumber_Remainder'), |
| 36 | + ('<<', 'PyNumber_Lshift'), |
| 37 | + ('>>', 'PyNumber_Rshift'), |
| 38 | + ('&', 'PyNumber_And'), |
| 39 | + ('^', 'PyNumber_Xor'), |
| 40 | + ('|', 'PyNumber_Or')]: |
| 41 | + binary_op(op=op, |
| 42 | + arg_types=[object_rprimitive, object_rprimitive], |
| 43 | + result_type=object_rprimitive, |
| 44 | + error_kind=ERR_MAGIC, |
| 45 | + emit=call_emit(funcname), |
| 46 | + priority=0) |
| 47 | + |
| 48 | +for op, funcname in [('+=', 'PyNumber_InPlaceAdd'), |
| 49 | + ('-=', 'PyNumber_InPlaceSubtract'), |
| 50 | + ('*=', 'PyNumber_InPlaceMultiply'), |
| 51 | + ('@=', 'PyNumber_InPlaceMatrixMultiply'), |
| 52 | + ('//=', 'PyNumber_InPlaceFloorDivide'), |
| 53 | + ('/=', 'PyNumber_InPlaceTrueDivide'), |
| 54 | + ('%=', 'PyNumber_InPlaceRemainder'), |
| 55 | + ('<<=', 'PyNumber_InPlaceLshift'), |
| 56 | + ('>>=', 'PyNumber_InPlaceRshift'), |
| 57 | + ('&=', 'PyNumber_InPlaceAnd'), |
| 58 | + ('^=', 'PyNumber_InPlaceXor'), |
| 59 | + ('|=', 'PyNumber_InPlaceOr')]: |
| 60 | + binary_op(op=op, |
| 61 | + arg_types=[object_rprimitive, object_rprimitive], |
| 62 | + result_type=object_rprimitive, |
| 63 | + error_kind=ERR_MAGIC, |
| 64 | + emit=simple_emit('{dest} = %s({args[0]}, {args[1]});' % funcname), |
| 65 | + priority=0) |
| 66 | + |
| 67 | +binary_op(op='**', |
| 68 | + arg_types=[object_rprimitive, object_rprimitive], |
| 69 | + result_type=object_rprimitive, |
| 70 | + error_kind=ERR_MAGIC, |
| 71 | + emit=simple_emit('{dest} = PyNumber_Power({args[0]}, {args[1]}, Py_None);'), |
| 72 | + priority=0) |
| 73 | + |
| 74 | +binary_op('in', |
| 75 | + arg_types=[object_rprimitive, object_rprimitive], |
| 76 | + result_type=bool_rprimitive, |
| 77 | + error_kind=ERR_MAGIC, |
| 78 | + emit=negative_int_emit('{dest} = PySequence_Contains({args[1]}, {args[0]});'), |
| 79 | + priority=0) |
| 80 | + |
| 81 | +binary_op('is', |
| 82 | + arg_types=[object_rprimitive, object_rprimitive], |
| 83 | + result_type=bool_rprimitive, |
| 84 | + error_kind=ERR_NEVER, |
| 85 | + emit=simple_emit('{dest} = {args[0]} == {args[1]};'), |
| 86 | + priority=0) |
| 87 | + |
| 88 | +binary_op('is not', |
| 89 | + arg_types=[object_rprimitive, object_rprimitive], |
| 90 | + result_type=bool_rprimitive, |
| 91 | + error_kind=ERR_NEVER, |
| 92 | + emit=simple_emit('{dest} = {args[0]} != {args[1]};'), |
| 93 | + priority=0) |
| 94 | + |
| 95 | +for op, funcname in [('-', 'PyNumber_Negative'), |
| 96 | + ('+', 'PyNumber_Positive'), |
| 97 | + ('~', 'PyNumber_Invert')]: |
| 98 | + unary_op(op=op, |
| 99 | + arg_type=object_rprimitive, |
| 100 | + result_type=object_rprimitive, |
| 101 | + error_kind=ERR_MAGIC, |
| 102 | + emit=call_emit(funcname), |
| 103 | + priority=0) |
| 104 | + |
| 105 | +unary_op(op='not', |
| 106 | + arg_type=object_rprimitive, |
| 107 | + result_type=bool_rprimitive, |
| 108 | + error_kind=ERR_MAGIC, |
| 109 | + format_str='{dest} = not {args[0]}', |
| 110 | + emit=call_negative_magic_emit('PyObject_Not'), |
| 111 | + priority=0) |
| 112 | + |
| 113 | +method_op('__getitem__', |
| 114 | + arg_types=[object_rprimitive, object_rprimitive], |
| 115 | + result_type=object_rprimitive, |
| 116 | + error_kind=ERR_MAGIC, |
| 117 | + emit=call_emit('PyObject_GetItem'), |
| 118 | + priority=0) |
| 119 | + |
| 120 | +method_op('__setitem__', |
| 121 | + arg_types=[object_rprimitive, object_rprimitive, object_rprimitive], |
| 122 | + result_type=bool_rprimitive, |
| 123 | + error_kind=ERR_FALSE, |
| 124 | + emit=call_negative_bool_emit('PyObject_SetItem'), |
| 125 | + priority=0) |
| 126 | + |
| 127 | +method_op('__delitem__', |
| 128 | + arg_types=[object_rprimitive, object_rprimitive], |
| 129 | + result_type=bool_rprimitive, |
| 130 | + error_kind=ERR_FALSE, |
| 131 | + emit=call_negative_bool_emit('PyObject_DelItem'), |
| 132 | + priority=0) |
| 133 | + |
| 134 | +func_op( |
| 135 | + name='builtins.hash', |
| 136 | + arg_types=[object_rprimitive], |
| 137 | + result_type=int_rprimitive, |
| 138 | + error_kind=ERR_MAGIC, |
| 139 | + emit=call_emit('CPyObject_Hash')) |
| 140 | + |
| 141 | +py_getattr_op = func_op( |
| 142 | + name='builtins.getattr', |
| 143 | + arg_types=[object_rprimitive, object_rprimitive], |
| 144 | + result_type=object_rprimitive, |
| 145 | + error_kind=ERR_MAGIC, |
| 146 | + emit=call_emit('PyObject_GetAttr') |
| 147 | +) |
| 148 | + |
| 149 | +func_op( |
| 150 | + name='builtins.getattr', |
| 151 | + arg_types=[object_rprimitive, object_rprimitive, object_rprimitive], |
| 152 | + result_type=object_rprimitive, |
| 153 | + error_kind=ERR_MAGIC, |
| 154 | + emit=call_emit('CPyObject_GetAttr3') |
| 155 | +) |
| 156 | + |
| 157 | +py_setattr_op = func_op( |
| 158 | + name='builtins.setattr', |
| 159 | + arg_types=[object_rprimitive, object_rprimitive, object_rprimitive], |
| 160 | + result_type=bool_rprimitive, |
| 161 | + error_kind=ERR_FALSE, |
| 162 | + emit=call_negative_bool_emit('PyObject_SetAttr') |
| 163 | +) |
| 164 | + |
| 165 | +py_hasattr_op = func_op( |
| 166 | + name='builtins.hasattr', |
| 167 | + arg_types=[object_rprimitive, object_rprimitive], |
| 168 | + result_type=bool_rprimitive, |
| 169 | + error_kind=ERR_NEVER, |
| 170 | + emit=call_emit('PyObject_HasAttr') |
| 171 | +) |
| 172 | + |
| 173 | +py_delattr_op = func_op( |
| 174 | + name='builtins.delattr', |
| 175 | + arg_types=[object_rprimitive, object_rprimitive], |
| 176 | + result_type=bool_rprimitive, |
| 177 | + error_kind=ERR_FALSE, |
| 178 | + emit=call_negative_bool_emit('PyObject_DelAttr') |
| 179 | +) |
| 180 | + |
| 181 | +py_call_op = custom_op( |
| 182 | + arg_types=[object_rprimitive], |
| 183 | + result_type=object_rprimitive, |
| 184 | + is_var_arg=True, |
| 185 | + error_kind=ERR_MAGIC, |
| 186 | + format_str='{dest} = py_call({comma_args})', |
| 187 | + emit=simple_emit('{dest} = PyObject_CallFunctionObjArgs({comma_args}, NULL);')) |
| 188 | + |
| 189 | +py_call_with_kwargs_op = custom_op( |
| 190 | + arg_types=[object_rprimitive], |
| 191 | + result_type=object_rprimitive, |
| 192 | + is_var_arg=True, |
| 193 | + error_kind=ERR_MAGIC, |
| 194 | + format_str='{dest} = py_call_with_kwargs({args[0]}, {args[1]}, {args[2]})', |
| 195 | + emit=call_emit('PyObject_Call')) |
| 196 | + |
| 197 | +py_method_call_op = custom_op( |
| 198 | + arg_types=[object_rprimitive], |
| 199 | + result_type=object_rprimitive, |
| 200 | + is_var_arg=True, |
| 201 | + error_kind=ERR_MAGIC, |
| 202 | + format_str='{dest} = py_method_call({comma_args})', |
| 203 | + emit=simple_emit('{dest} = PyObject_CallMethodObjArgs({comma_args}, NULL);')) |
| 204 | + |
| 205 | +func_op(name='builtins.len', |
| 206 | + arg_types=[object_rprimitive], |
| 207 | + result_type=int_rprimitive, |
| 208 | + error_kind=ERR_NEVER, |
| 209 | + emit=call_emit('CPyObject_Size'), |
| 210 | + priority=0) |
| 211 | + |
| 212 | +iter_op = func_op(name='builtins.iter', |
| 213 | + arg_types=[object_rprimitive], |
| 214 | + result_type=object_rprimitive, |
| 215 | + error_kind=ERR_MAGIC, |
| 216 | + emit=call_emit('PyObject_GetIter')) |
| 217 | + |
| 218 | +# Although the error_kind is set to be ERR_NEVER, this can actually |
| 219 | +# return NULL, and thus it must be checked using Branch.IS_ERROR. |
| 220 | +next_op = custom_op(name='next', |
| 221 | + arg_types=[object_rprimitive], |
| 222 | + result_type=object_rprimitive, |
| 223 | + error_kind=ERR_NEVER, |
| 224 | + emit=call_emit('PyIter_Next')) |
| 225 | + |
| 226 | +# Do a next, don't swallow StopIteration, but also don't propagate an |
| 227 | +# error. (N.B: This can still return NULL without an error to |
| 228 | +# represent an implicit StopIteration, but if StopIteration is |
| 229 | +# *explicitly* raised this will not swallow it.) |
| 230 | +# Can return NULL: see next_op. |
| 231 | +next_raw_op = custom_op(name='next', |
| 232 | + arg_types=[object_rprimitive], |
| 233 | + result_type=object_rprimitive, |
| 234 | + error_kind=ERR_NEVER, |
| 235 | + emit=call_emit('CPyIter_Next')) |
0 commit comments