registry.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. """Utilities for defining primitive ops.
  2. Most of the ops can be automatically generated by matching against AST
  3. nodes and types. For example, a func_op is automatically generated when
  4. a specific function is called with the specific positional argument
  5. count and argument types.
  6. Example op definition:
  7. list_len_op = func_op(name='builtins.len',
  8. arg_types=[list_rprimitive],
  9. result_type=short_int_rprimitive,
  10. error_kind=ERR_NEVER,
  11. emit=emit_len)
  12. This op is automatically generated for calls to len() with a single
  13. list argument. The result type is short_int_rprimitive, and this
  14. never raises an exception (ERR_NEVER). The function emit_len is used
  15. to generate C for this op. The op can also be manually generated using
  16. "list_len_op". Ops that are only generated automatically don't need to
  17. be assigned to a module attribute.
  18. Ops defined with custom_op are only explicitly generated in
  19. mypyc.irbuild and won't be generated automatically. They are always
  20. assigned to a module attribute, as otherwise they won't be accessible.
  21. The actual ops are defined in other submodules of this package, grouped
  22. by category.
  23. Most operations have fallback implementations that apply to all possible
  24. arguments and types. For example, there are generic implementations of
  25. arbitrary function and method calls, and binary operators. These generic
  26. implementations are typically slower than specialized ones, but we tend
  27. to rely on them for infrequently used ops. It's impractical to have
  28. optimized implementations of all ops.
  29. """
  30. from __future__ import annotations
  31. from typing import Final, NamedTuple
  32. from mypyc.ir.ops import StealsDescription
  33. from mypyc.ir.rtypes import RType
  34. # Error kind for functions that return negative integer on exception. This
  35. # is only used for primitives. We translate it away during IR building.
  36. ERR_NEG_INT: Final = 10
  37. class CFunctionDescription(NamedTuple):
  38. name: str
  39. arg_types: list[RType]
  40. return_type: RType
  41. var_arg_type: RType | None
  42. truncated_type: RType | None
  43. c_function_name: str
  44. error_kind: int
  45. steals: StealsDescription
  46. is_borrowed: bool
  47. ordering: list[int] | None
  48. extra_int_constants: list[tuple[int, RType]]
  49. priority: int
  50. # A description for C load operations including LoadGlobal and LoadAddress
  51. class LoadAddressDescription(NamedTuple):
  52. name: str
  53. type: RType
  54. src: str # name of the target to load
  55. # CallC op for method call(such as 'str.join')
  56. method_call_ops: dict[str, list[CFunctionDescription]] = {}
  57. # CallC op for top level function call(such as 'builtins.list')
  58. function_ops: dict[str, list[CFunctionDescription]] = {}
  59. # CallC op for binary ops
  60. binary_ops: dict[str, list[CFunctionDescription]] = {}
  61. # CallC op for unary ops
  62. unary_ops: dict[str, list[CFunctionDescription]] = {}
  63. builtin_names: dict[str, tuple[RType, str]] = {}
  64. def method_op(
  65. name: str,
  66. arg_types: list[RType],
  67. return_type: RType,
  68. c_function_name: str,
  69. error_kind: int,
  70. var_arg_type: RType | None = None,
  71. truncated_type: RType | None = None,
  72. ordering: list[int] | None = None,
  73. extra_int_constants: list[tuple[int, RType]] = [],
  74. steals: StealsDescription = False,
  75. is_borrowed: bool = False,
  76. priority: int = 1,
  77. ) -> CFunctionDescription:
  78. """Define a c function call op that replaces a method call.
  79. This will be automatically generated by matching against the AST.
  80. Args:
  81. name: short name of the method (for example, 'append')
  82. arg_types: argument types; the receiver is always the first argument
  83. return_type: type of the return value. Use void_rtype to represent void.
  84. c_function_name: name of the C function to call
  85. error_kind: how errors are represented in the result (one of ERR_*)
  86. var_arg_type: type of all variable arguments
  87. truncated_type: type to truncated to(See Truncate for info)
  88. if it's defined both return_type and it should be non-referenced
  89. integer types or bool type
  90. ordering: optional ordering of the arguments, if defined,
  91. reorders the arguments accordingly.
  92. should never be used together with var_arg_type.
  93. all the other arguments(such as arg_types) are in the order
  94. accepted by the python syntax(before reordering)
  95. extra_int_constants: optional extra integer constants as the last arguments to a C call
  96. steals: description of arguments that this steals (ref count wise)
  97. is_borrowed: if True, returned value is borrowed (no need to decrease refcount)
  98. priority: if multiple ops match, the one with the highest priority is picked
  99. """
  100. ops = method_call_ops.setdefault(name, [])
  101. desc = CFunctionDescription(
  102. name,
  103. arg_types,
  104. return_type,
  105. var_arg_type,
  106. truncated_type,
  107. c_function_name,
  108. error_kind,
  109. steals,
  110. is_borrowed,
  111. ordering,
  112. extra_int_constants,
  113. priority,
  114. )
  115. ops.append(desc)
  116. return desc
  117. def function_op(
  118. name: str,
  119. arg_types: list[RType],
  120. return_type: RType,
  121. c_function_name: str,
  122. error_kind: int,
  123. var_arg_type: RType | None = None,
  124. truncated_type: RType | None = None,
  125. ordering: list[int] | None = None,
  126. extra_int_constants: list[tuple[int, RType]] = [],
  127. steals: StealsDescription = False,
  128. is_borrowed: bool = False,
  129. priority: int = 1,
  130. ) -> CFunctionDescription:
  131. """Define a c function call op that replaces a function call.
  132. This will be automatically generated by matching against the AST.
  133. Most arguments are similar to method_op().
  134. Args:
  135. name: full name of the function
  136. arg_types: positional argument types for which this applies
  137. """
  138. ops = function_ops.setdefault(name, [])
  139. desc = CFunctionDescription(
  140. name,
  141. arg_types,
  142. return_type,
  143. var_arg_type,
  144. truncated_type,
  145. c_function_name,
  146. error_kind,
  147. steals,
  148. is_borrowed,
  149. ordering,
  150. extra_int_constants,
  151. priority,
  152. )
  153. ops.append(desc)
  154. return desc
  155. def binary_op(
  156. name: str,
  157. arg_types: list[RType],
  158. return_type: RType,
  159. c_function_name: str,
  160. error_kind: int,
  161. var_arg_type: RType | None = None,
  162. truncated_type: RType | None = None,
  163. ordering: list[int] | None = None,
  164. extra_int_constants: list[tuple[int, RType]] = [],
  165. steals: StealsDescription = False,
  166. is_borrowed: bool = False,
  167. priority: int = 1,
  168. ) -> CFunctionDescription:
  169. """Define a c function call op for a binary operation.
  170. This will be automatically generated by matching against the AST.
  171. Most arguments are similar to method_op(), but exactly two argument types
  172. are expected.
  173. """
  174. ops = binary_ops.setdefault(name, [])
  175. desc = CFunctionDescription(
  176. name,
  177. arg_types,
  178. return_type,
  179. var_arg_type,
  180. truncated_type,
  181. c_function_name,
  182. error_kind,
  183. steals,
  184. is_borrowed,
  185. ordering,
  186. extra_int_constants,
  187. priority,
  188. )
  189. ops.append(desc)
  190. return desc
  191. def custom_op(
  192. arg_types: list[RType],
  193. return_type: RType,
  194. c_function_name: str,
  195. error_kind: int,
  196. var_arg_type: RType | None = None,
  197. truncated_type: RType | None = None,
  198. ordering: list[int] | None = None,
  199. extra_int_constants: list[tuple[int, RType]] = [],
  200. steals: StealsDescription = False,
  201. is_borrowed: bool = False,
  202. ) -> CFunctionDescription:
  203. """Create a one-off CallC op that can't be automatically generated from the AST.
  204. Most arguments are similar to method_op().
  205. """
  206. return CFunctionDescription(
  207. "<custom>",
  208. arg_types,
  209. return_type,
  210. var_arg_type,
  211. truncated_type,
  212. c_function_name,
  213. error_kind,
  214. steals,
  215. is_borrowed,
  216. ordering,
  217. extra_int_constants,
  218. 0,
  219. )
  220. def unary_op(
  221. name: str,
  222. arg_type: RType,
  223. return_type: RType,
  224. c_function_name: str,
  225. error_kind: int,
  226. truncated_type: RType | None = None,
  227. ordering: list[int] | None = None,
  228. extra_int_constants: list[tuple[int, RType]] = [],
  229. steals: StealsDescription = False,
  230. is_borrowed: bool = False,
  231. priority: int = 1,
  232. ) -> CFunctionDescription:
  233. """Define a c function call op for an unary operation.
  234. This will be automatically generated by matching against the AST.
  235. Most arguments are similar to method_op(), but exactly one argument type
  236. is expected.
  237. """
  238. ops = unary_ops.setdefault(name, [])
  239. desc = CFunctionDescription(
  240. name,
  241. [arg_type],
  242. return_type,
  243. None,
  244. truncated_type,
  245. c_function_name,
  246. error_kind,
  247. steals,
  248. is_borrowed,
  249. ordering,
  250. extra_int_constants,
  251. priority,
  252. )
  253. ops.append(desc)
  254. return desc
  255. def load_address_op(name: str, type: RType, src: str) -> LoadAddressDescription:
  256. assert name not in builtin_names, "already defined: %s" % name
  257. builtin_names[name] = (type, src)
  258. return LoadAddressDescription(name, type, src)
  259. import mypyc.primitives.bytes_ops
  260. import mypyc.primitives.dict_ops
  261. import mypyc.primitives.float_ops
  262. # Import various modules that set up global state.
  263. import mypyc.primitives.int_ops
  264. import mypyc.primitives.list_ops
  265. import mypyc.primitives.misc_ops
  266. import mypyc.primitives.str_ops
  267. import mypyc.primitives.tuple_ops # noqa: F401