int_ops.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. """Arbitrary-precision integer primitive ops.
  2. These mostly operate on (usually) unboxed integers that use a tagged pointer
  3. representation (CPyTagged) and correspond to the Python 'int' type.
  4. See also the documentation for mypyc.rtypes.int_rprimitive.
  5. Use mypyc.ir.ops.IntOp for operations on fixed-width/C integers.
  6. """
  7. from __future__ import annotations
  8. from typing import NamedTuple
  9. from mypyc.ir.ops import ERR_ALWAYS, ERR_MAGIC, ERR_MAGIC_OVERLAPPING, ERR_NEVER, ComparisonOp
  10. from mypyc.ir.rtypes import (
  11. RType,
  12. bit_rprimitive,
  13. bool_rprimitive,
  14. c_pyssize_t_rprimitive,
  15. float_rprimitive,
  16. int32_rprimitive,
  17. int64_rprimitive,
  18. int_rprimitive,
  19. object_rprimitive,
  20. str_rprimitive,
  21. void_rtype,
  22. )
  23. from mypyc.primitives.registry import (
  24. CFunctionDescription,
  25. binary_op,
  26. custom_op,
  27. function_op,
  28. load_address_op,
  29. unary_op,
  30. )
  31. # Constructors for builtins.int and native int types have the same behavior. In
  32. # interpreted mode, native int types are just aliases to 'int'.
  33. for int_name in ("builtins.int", "mypy_extensions.i64", "mypy_extensions.i32"):
  34. # These int constructors produce object_rprimitives that then need to be unboxed
  35. # I guess unboxing ourselves would save a check and branch though?
  36. # Get the type object for 'builtins.int' or a native int type.
  37. # For ordinary calls to int() we use a load_address to the type.
  38. # Native ints don't have a separate type object -- we just use 'builtins.int'.
  39. load_address_op(name=int_name, type=object_rprimitive, src="PyLong_Type")
  40. # int(float). We could do a bit better directly.
  41. function_op(
  42. name=int_name,
  43. arg_types=[float_rprimitive],
  44. return_type=int_rprimitive,
  45. c_function_name="CPyTagged_FromFloat",
  46. error_kind=ERR_MAGIC,
  47. )
  48. # int(string)
  49. function_op(
  50. name=int_name,
  51. arg_types=[str_rprimitive],
  52. return_type=object_rprimitive,
  53. c_function_name="CPyLong_FromStr",
  54. error_kind=ERR_MAGIC,
  55. )
  56. # int(string, base)
  57. function_op(
  58. name=int_name,
  59. arg_types=[str_rprimitive, int_rprimitive],
  60. return_type=object_rprimitive,
  61. c_function_name="CPyLong_FromStrWithBase",
  62. error_kind=ERR_MAGIC,
  63. )
  64. # str(int)
  65. int_to_str_op = function_op(
  66. name="builtins.str",
  67. arg_types=[int_rprimitive],
  68. return_type=str_rprimitive,
  69. c_function_name="CPyTagged_Str",
  70. error_kind=ERR_MAGIC,
  71. priority=2,
  72. )
  73. # We need a specialization for str on bools also since the int one is wrong...
  74. function_op(
  75. name="builtins.str",
  76. arg_types=[bool_rprimitive],
  77. return_type=str_rprimitive,
  78. c_function_name="CPyBool_Str",
  79. error_kind=ERR_MAGIC,
  80. priority=3,
  81. )
  82. def int_binary_op(
  83. name: str,
  84. c_function_name: str,
  85. return_type: RType = int_rprimitive,
  86. error_kind: int = ERR_NEVER,
  87. ) -> None:
  88. binary_op(
  89. name=name,
  90. arg_types=[int_rprimitive, int_rprimitive],
  91. return_type=return_type,
  92. c_function_name=c_function_name,
  93. error_kind=error_kind,
  94. )
  95. # Binary, unary and augmented assignment operations that operate on CPyTagged ints
  96. # are implemented as C functions.
  97. int_binary_op("+", "CPyTagged_Add")
  98. int_binary_op("-", "CPyTagged_Subtract")
  99. int_binary_op("*", "CPyTagged_Multiply")
  100. int_binary_op("&", "CPyTagged_And")
  101. int_binary_op("|", "CPyTagged_Or")
  102. int_binary_op("^", "CPyTagged_Xor")
  103. # Divide and remainder we honestly propagate errors from because they
  104. # can raise ZeroDivisionError
  105. int_binary_op("//", "CPyTagged_FloorDivide", error_kind=ERR_MAGIC)
  106. int_binary_op("%", "CPyTagged_Remainder", error_kind=ERR_MAGIC)
  107. # Negative shift counts raise an exception
  108. int_binary_op(">>", "CPyTagged_Rshift", error_kind=ERR_MAGIC)
  109. int_binary_op("<<", "CPyTagged_Lshift", error_kind=ERR_MAGIC)
  110. int_binary_op(
  111. "/", "CPyTagged_TrueDivide", return_type=float_rprimitive, error_kind=ERR_MAGIC_OVERLAPPING
  112. )
  113. # This should work because assignment operators are parsed differently
  114. # and the code in irbuild that handles it does the assignment
  115. # regardless of whether or not the operator works in place anyway.
  116. int_binary_op("+=", "CPyTagged_Add")
  117. int_binary_op("-=", "CPyTagged_Subtract")
  118. int_binary_op("*=", "CPyTagged_Multiply")
  119. int_binary_op("&=", "CPyTagged_And")
  120. int_binary_op("|=", "CPyTagged_Or")
  121. int_binary_op("^=", "CPyTagged_Xor")
  122. int_binary_op("//=", "CPyTagged_FloorDivide", error_kind=ERR_MAGIC)
  123. int_binary_op("%=", "CPyTagged_Remainder", error_kind=ERR_MAGIC)
  124. int_binary_op(">>=", "CPyTagged_Rshift", error_kind=ERR_MAGIC)
  125. int_binary_op("<<=", "CPyTagged_Lshift", error_kind=ERR_MAGIC)
  126. def int_unary_op(name: str, c_function_name: str) -> CFunctionDescription:
  127. return unary_op(
  128. name=name,
  129. arg_type=int_rprimitive,
  130. return_type=int_rprimitive,
  131. c_function_name=c_function_name,
  132. error_kind=ERR_NEVER,
  133. )
  134. int_neg_op = int_unary_op("-", "CPyTagged_Negate")
  135. int_invert_op = int_unary_op("~", "CPyTagged_Invert")
  136. # Primitives related to integer comparison operations:
  137. # Description for building int comparison ops
  138. #
  139. # Fields:
  140. # binary_op_variant: identify which IntOp to use when operands are short integers
  141. # c_func_description: the C function to call when operands are tagged integers
  142. # c_func_negated: whether to negate the C function call's result
  143. # c_func_swap_operands: whether to swap lhs and rhs when call the function
  144. class IntComparisonOpDescription(NamedTuple):
  145. binary_op_variant: int
  146. c_func_description: CFunctionDescription
  147. c_func_negated: bool
  148. c_func_swap_operands: bool
  149. # Equals operation on two boxed tagged integers
  150. int_equal_ = custom_op(
  151. arg_types=[int_rprimitive, int_rprimitive],
  152. return_type=bit_rprimitive,
  153. c_function_name="CPyTagged_IsEq_",
  154. error_kind=ERR_NEVER,
  155. )
  156. # Less than operation on two boxed tagged integers
  157. int_less_than_ = custom_op(
  158. arg_types=[int_rprimitive, int_rprimitive],
  159. return_type=bit_rprimitive,
  160. c_function_name="CPyTagged_IsLt_",
  161. error_kind=ERR_NEVER,
  162. )
  163. # Provide mapping from textual op to short int's op variant and boxed int's description.
  164. # Note that these are not complete implementations and require extra IR.
  165. int_comparison_op_mapping: dict[str, IntComparisonOpDescription] = {
  166. "==": IntComparisonOpDescription(ComparisonOp.EQ, int_equal_, False, False),
  167. "!=": IntComparisonOpDescription(ComparisonOp.NEQ, int_equal_, True, False),
  168. "<": IntComparisonOpDescription(ComparisonOp.SLT, int_less_than_, False, False),
  169. "<=": IntComparisonOpDescription(ComparisonOp.SLE, int_less_than_, True, True),
  170. ">": IntComparisonOpDescription(ComparisonOp.SGT, int_less_than_, False, True),
  171. ">=": IntComparisonOpDescription(ComparisonOp.SGE, int_less_than_, True, False),
  172. }
  173. int64_divide_op = custom_op(
  174. arg_types=[int64_rprimitive, int64_rprimitive],
  175. return_type=int64_rprimitive,
  176. c_function_name="CPyInt64_Divide",
  177. error_kind=ERR_MAGIC_OVERLAPPING,
  178. )
  179. int64_mod_op = custom_op(
  180. arg_types=[int64_rprimitive, int64_rprimitive],
  181. return_type=int64_rprimitive,
  182. c_function_name="CPyInt64_Remainder",
  183. error_kind=ERR_MAGIC_OVERLAPPING,
  184. )
  185. int32_divide_op = custom_op(
  186. arg_types=[int32_rprimitive, int32_rprimitive],
  187. return_type=int32_rprimitive,
  188. c_function_name="CPyInt32_Divide",
  189. error_kind=ERR_MAGIC_OVERLAPPING,
  190. )
  191. int32_mod_op = custom_op(
  192. arg_types=[int32_rprimitive, int32_rprimitive],
  193. return_type=int32_rprimitive,
  194. c_function_name="CPyInt32_Remainder",
  195. error_kind=ERR_MAGIC_OVERLAPPING,
  196. )
  197. # Convert tagged int (as PyObject *) to i64
  198. int_to_int64_op = custom_op(
  199. arg_types=[object_rprimitive],
  200. return_type=int64_rprimitive,
  201. c_function_name="CPyLong_AsInt64",
  202. error_kind=ERR_MAGIC_OVERLAPPING,
  203. )
  204. ssize_t_to_int_op = custom_op(
  205. arg_types=[c_pyssize_t_rprimitive],
  206. return_type=int_rprimitive,
  207. c_function_name="CPyTagged_FromSsize_t",
  208. error_kind=ERR_MAGIC,
  209. )
  210. int64_to_int_op = custom_op(
  211. arg_types=[int64_rprimitive],
  212. return_type=int_rprimitive,
  213. c_function_name="CPyTagged_FromInt64",
  214. error_kind=ERR_MAGIC,
  215. )
  216. # Convert tagged int (as PyObject *) to i32
  217. int_to_int32_op = custom_op(
  218. arg_types=[object_rprimitive],
  219. return_type=int32_rprimitive,
  220. c_function_name="CPyLong_AsInt32",
  221. error_kind=ERR_MAGIC_OVERLAPPING,
  222. )
  223. int32_overflow = custom_op(
  224. arg_types=[],
  225. return_type=void_rtype,
  226. c_function_name="CPyInt32_Overflow",
  227. error_kind=ERR_ALWAYS,
  228. )