numba-cuda 0.18.1__py3-none-any.whl → 0.19.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of numba-cuda might be problematic. Click here for more details.

Files changed (88) hide show
  1. numba_cuda/VERSION +1 -1
  2. numba_cuda/numba/cuda/__init__.py +1 -1
  3. numba_cuda/numba/cuda/_internal/cuda_bf16.py +2 -2
  4. numba_cuda/numba/cuda/_internal/cuda_fp16.py +1 -1
  5. numba_cuda/numba/cuda/api.py +2 -7
  6. numba_cuda/numba/cuda/compiler.py +7 -4
  7. numba_cuda/numba/cuda/core/interpreter.py +3592 -0
  8. numba_cuda/numba/cuda/core/ir_utils.py +2645 -0
  9. numba_cuda/numba/cuda/core/sigutils.py +55 -0
  10. numba_cuda/numba/cuda/cuda_paths.py +9 -17
  11. numba_cuda/numba/cuda/cudadecl.py +1 -1
  12. numba_cuda/numba/cuda/cudadrv/driver.py +4 -19
  13. numba_cuda/numba/cuda/cudadrv/libs.py +1 -2
  14. numba_cuda/numba/cuda/cudadrv/nvrtc.py +44 -44
  15. numba_cuda/numba/cuda/cudadrv/nvvm.py +3 -18
  16. numba_cuda/numba/cuda/cudadrv/runtime.py +12 -1
  17. numba_cuda/numba/cuda/cudamath.py +1 -1
  18. numba_cuda/numba/cuda/decorators.py +4 -3
  19. numba_cuda/numba/cuda/deviceufunc.py +2 -1
  20. numba_cuda/numba/cuda/dispatcher.py +3 -2
  21. numba_cuda/numba/cuda/extending.py +1 -1
  22. numba_cuda/numba/cuda/itanium_mangler.py +211 -0
  23. numba_cuda/numba/cuda/libdevicedecl.py +1 -1
  24. numba_cuda/numba/cuda/libdevicefuncs.py +1 -1
  25. numba_cuda/numba/cuda/lowering.py +1 -1
  26. numba_cuda/numba/cuda/simulator/api.py +1 -1
  27. numba_cuda/numba/cuda/simulator/cudadrv/driver.py +0 -7
  28. numba_cuda/numba/cuda/target.py +1 -2
  29. numba_cuda/numba/cuda/testing.py +4 -6
  30. numba_cuda/numba/cuda/tests/core/test_itanium_mangler.py +80 -0
  31. numba_cuda/numba/cuda/tests/cudadrv/test_cuda_ndarray.py +1 -1
  32. numba_cuda/numba/cuda/tests/cudadrv/test_deallocations.py +1 -1
  33. numba_cuda/numba/cuda/tests/cudadrv/test_detect.py +1 -1
  34. numba_cuda/numba/cuda/tests/cudadrv/test_emm_plugins.py +1 -1
  35. numba_cuda/numba/cuda/tests/cudadrv/test_linker.py +1 -1
  36. numba_cuda/numba/cuda/tests/cudadrv/test_managed_alloc.py +1 -1
  37. numba_cuda/numba/cuda/tests/cudadrv/test_mvc.py +1 -1
  38. numba_cuda/numba/cuda/tests/cudadrv/test_nvrtc.py +4 -6
  39. numba_cuda/numba/cuda/tests/cudadrv/test_nvvm_driver.py +0 -4
  40. numba_cuda/numba/cuda/tests/cudadrv/test_ptds.py +1 -1
  41. numba_cuda/numba/cuda/tests/cudapy/test_bfloat16.py +1 -3
  42. numba_cuda/numba/cuda/tests/cudapy/test_bfloat16_bindings.py +1 -3
  43. numba_cuda/numba/cuda/tests/cudapy/test_caching.py +146 -3
  44. numba_cuda/numba/cuda/tests/cudapy/test_cffi.py +1 -1
  45. numba_cuda/numba/cuda/tests/cudapy/test_compiler.py +0 -4
  46. numba_cuda/numba/cuda/tests/cudapy/test_cuda_array_interface.py +1 -1
  47. numba_cuda/numba/cuda/tests/cudapy/test_cuda_jit_no_types.py +1 -1
  48. numba_cuda/numba/cuda/tests/cudapy/test_debug.py +1 -1
  49. numba_cuda/numba/cuda/tests/cudapy/test_debuginfo.py +1 -284
  50. numba_cuda/numba/cuda/tests/cudapy/test_debuginfo_types.py +473 -0
  51. numba_cuda/numba/cuda/tests/cudapy/test_device_func.py +1 -1
  52. numba_cuda/numba/cuda/tests/cudapy/test_errors.py +1 -1
  53. numba_cuda/numba/cuda/tests/cudapy/test_extending.py +1 -6
  54. numba_cuda/numba/cuda/tests/cudapy/test_gufunc.py +1 -1
  55. numba_cuda/numba/cuda/tests/cudapy/test_ipc.py +1 -1
  56. numba_cuda/numba/cuda/tests/cudapy/test_ir_utils.py +295 -0
  57. numba_cuda/numba/cuda/tests/cudapy/test_lineinfo.py +1 -1
  58. numba_cuda/numba/cuda/tests/cudapy/test_operator.py +1 -1
  59. numba_cuda/numba/cuda/tests/cudapy/test_ufuncs.py +1 -1
  60. numba_cuda/numba/cuda/tests/cudapy/test_warning.py +5 -1
  61. numba_cuda/numba/cuda/tests/doc_examples/test_cpointer.py +1 -1
  62. numba_cuda/numba/cuda/tests/doc_examples/test_cpu_gpu_compat.py +1 -1
  63. numba_cuda/numba/cuda/tests/doc_examples/test_ffi.py +1 -1
  64. numba_cuda/numba/cuda/tests/doc_examples/test_laplace.py +1 -1
  65. numba_cuda/numba/cuda/tests/doc_examples/test_matmul.py +1 -1
  66. numba_cuda/numba/cuda/tests/doc_examples/test_montecarlo.py +1 -1
  67. numba_cuda/numba/cuda/tests/doc_examples/test_reduction.py +1 -1
  68. numba_cuda/numba/cuda/tests/doc_examples/test_sessionize.py +1 -1
  69. numba_cuda/numba/cuda/tests/doc_examples/test_ufunc.py +1 -1
  70. numba_cuda/numba/cuda/tests/doc_examples/test_vecadd.py +1 -1
  71. numba_cuda/numba/cuda/tests/nocuda/test_import.py +1 -1
  72. numba_cuda/numba/cuda/tests/nrt/test_nrt.py +2 -2
  73. numba_cuda/numba/cuda/tests/nrt/test_nrt_refct.py +1 -1
  74. numba_cuda/numba/cuda/tests/support.py +752 -0
  75. numba_cuda/numba/cuda/tests/test_binary_generation/Makefile +3 -3
  76. numba_cuda/numba/cuda/tests/test_binary_generation/generate_raw_ltoir.py +4 -1
  77. numba_cuda/numba/cuda/typing/__init__.py +8 -0
  78. numba_cuda/numba/cuda/typing/templates.py +1453 -0
  79. numba_cuda/numba/cuda/vector_types.py +3 -3
  80. {numba_cuda-0.18.1.dist-info → numba_cuda-0.19.0.dist-info}/METADATA +21 -28
  81. {numba_cuda-0.18.1.dist-info → numba_cuda-0.19.0.dist-info}/RECORD +84 -79
  82. numba_cuda/numba/cuda/include/11/cuda_bf16.h +0 -3749
  83. numba_cuda/numba/cuda/include/11/cuda_bf16.hpp +0 -2683
  84. numba_cuda/numba/cuda/include/11/cuda_fp16.h +0 -3794
  85. numba_cuda/numba/cuda/include/11/cuda_fp16.hpp +0 -2614
  86. {numba_cuda-0.18.1.dist-info → numba_cuda-0.19.0.dist-info}/WHEEL +0 -0
  87. {numba_cuda-0.18.1.dist-info → numba_cuda-0.19.0.dist-info}/licenses/LICENSE +0 -0
  88. {numba_cuda-0.18.1.dist-info → numba_cuda-0.19.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,3592 @@
1
+ import builtins
2
+ import collections
3
+ import dis
4
+ import operator
5
+ import logging
6
+ import textwrap
7
+
8
+ from numba.core import errors, ir, config
9
+ from numba.core.errors import (
10
+ NotDefinedError,
11
+ UnsupportedBytecodeError,
12
+ error_extras,
13
+ )
14
+ from numba.cuda.core import ir_utils
15
+ from numba.core.utils import (
16
+ PYVERSION,
17
+ BINOPS_TO_OPERATORS,
18
+ INPLACE_BINOPS_TO_OPERATORS,
19
+ _lazy_pformat,
20
+ )
21
+ from numba.core.byteflow import Flow, AdaptDFA, AdaptCFA, BlockKind
22
+ from numba.core.unsafe import eh
23
+ from numba.cpython.unsafe.tuple import unpack_single_tuple
24
+
25
+
26
+ if PYVERSION in ((3, 12), (3, 13)):
27
+ # Operands for CALL_INTRINSIC_1
28
+ from numba.core.byteflow import CALL_INTRINSIC_1_Operand as ci1op
29
+ elif PYVERSION in ((3, 10), (3, 11)):
30
+ pass
31
+ else:
32
+ raise NotImplementedError(PYVERSION)
33
+
34
+
35
+ class _UNKNOWN_VALUE(object):
36
+ """Represents an unknown value, this is for ease of debugging purposes only."""
37
+
38
+ def __init__(self, varname):
39
+ self._varname = varname
40
+
41
+ def __repr__(self):
42
+ return "_UNKNOWN_VALUE({})".format(self._varname)
43
+
44
+
45
+ _logger = logging.getLogger(__name__)
46
+
47
+
48
+ class Assigner(object):
49
+ """
50
+ This object keeps track of potential assignment simplifications
51
+ inside a code block.
52
+ For example `$O.1 = x` followed by `y = $0.1` can be simplified
53
+ into `y = x`, but it's not possible anymore if we have `x = z`
54
+ in-between those two instructions.
55
+
56
+ NOTE: this is not only an optimization, but is actually necessary
57
+ due to certain limitations of Numba - such as only accepting the
58
+ returning of an array passed as function argument.
59
+ """
60
+
61
+ def __init__(self):
62
+ # { destination variable name -> source Var object }
63
+ self.dest_to_src = {}
64
+ # Basically a reverse mapping of dest_to_src:
65
+ # { source variable name -> all destination names in dest_to_src }
66
+ self.src_invalidate = collections.defaultdict(list)
67
+ self.unused_dests = set()
68
+
69
+ def assign(self, srcvar, destvar):
70
+ """
71
+ Assign *srcvar* to *destvar*. Return either *srcvar* or a possible
72
+ simplified assignment source (earlier assigned to *srcvar*).
73
+ """
74
+ srcname = srcvar.name
75
+ destname = destvar.name
76
+ if destname in self.src_invalidate:
77
+ # destvar will change, invalidate all previously known
78
+ # simplifications
79
+ for d in self.src_invalidate.pop(destname):
80
+ self.dest_to_src.pop(d)
81
+ if srcname in self.dest_to_src:
82
+ srcvar = self.dest_to_src[srcname]
83
+ if destvar.is_temp:
84
+ self.dest_to_src[destname] = srcvar
85
+ self.src_invalidate[srcname].append(destname)
86
+ self.unused_dests.add(destname)
87
+ return srcvar
88
+
89
+ def get_assignment_source(self, destname):
90
+ """
91
+ Get a possible assignment source (a ir.Var instance) to replace
92
+ *destname*, otherwise None.
93
+ """
94
+ if destname in self.dest_to_src:
95
+ return self.dest_to_src[destname]
96
+ self.unused_dests.discard(destname)
97
+ return None
98
+
99
+
100
+ def _remove_assignment_definition(old_body, idx, func_ir, already_deleted_defs):
101
+ """
102
+ Deletes the definition defined for old_body at index idx
103
+ from func_ir. We assume this stmt will be deleted from
104
+ new_body.
105
+
106
+ In some optimizations we may update the same variable multiple times.
107
+ In this situation, we only need to delete a particular definition once,
108
+ this is tracked in already_deleted_def, which is a map from
109
+ assignment name to the set of values that have already been
110
+ deleted.
111
+ """
112
+ lhs = old_body[idx].target.name
113
+ rhs = old_body[idx].value
114
+ if rhs in func_ir._definitions[lhs]:
115
+ func_ir._definitions[lhs].remove(rhs)
116
+ already_deleted_defs[lhs].add(rhs)
117
+ elif rhs not in already_deleted_defs[lhs]:
118
+ raise UnsupportedBytecodeError(
119
+ "Inconsistency found in the definitions while executing"
120
+ " a peephole optimization. This suggests an internal"
121
+ " error or inconsistency elsewhere in the compiler."
122
+ )
123
+
124
+
125
+ def _call_function_ex_replace_kws_small(
126
+ old_body,
127
+ keyword_expr,
128
+ new_body,
129
+ buildmap_idx,
130
+ func_ir,
131
+ already_deleted_defs,
132
+ ):
133
+ """
134
+ Extracts the kws args passed as varkwarg
135
+ for CALL_FUNCTION_EX. This pass is taken when
136
+ n_kws <= 15 and the bytecode looks like:
137
+
138
+ # Start for each argument
139
+ LOAD_FAST # Load each argument.
140
+ # End for each argument
141
+ ...
142
+ BUILD_CONST_KEY_MAP # Build a map
143
+
144
+ In the generated IR, the varkwarg refers
145
+ to a single build_map that contains all of the
146
+ kws. In addition to returning the kws, this
147
+ function updates new_body to remove all usage
148
+ of the map.
149
+ """
150
+ kws = keyword_expr.items.copy()
151
+ # kws are required to have constant keys.
152
+ # We update these with the value_indexes
153
+ value_indexes = keyword_expr.value_indexes
154
+ for key, index in value_indexes.items():
155
+ kws[index] = (key, kws[index][1])
156
+ # Remove the build_map by setting the list
157
+ # index to None. Nones will be removed later.
158
+ new_body[buildmap_idx] = None
159
+ # Remove the definition.
160
+ _remove_assignment_definition(
161
+ old_body, buildmap_idx, func_ir, already_deleted_defs
162
+ )
163
+ return kws
164
+
165
+
166
+ def _call_function_ex_replace_kws_large(
167
+ old_body,
168
+ buildmap_name,
169
+ buildmap_idx,
170
+ search_end,
171
+ new_body,
172
+ func_ir,
173
+ errmsg,
174
+ already_deleted_defs,
175
+ ):
176
+ """
177
+ Extracts the kws args passed as varkwarg
178
+ for CALL_FUNCTION_EX. This pass is taken when
179
+ n_kws > 15 and the bytecode looks like:
180
+
181
+ BUILD_MAP # Construct the map
182
+ # Start for each argument
183
+ LOAD_CONST # Load a constant for the name of the argument
184
+ LOAD_FAST # Load each argument.
185
+ MAP_ADD # Append the (key, value) pair to the map
186
+ # End for each argument
187
+
188
+ In the IR generated, the initial build map is empty and a series
189
+ of setitems are applied afterwards. THE IR looks like:
190
+
191
+ $build_map_var = build_map(items=[])
192
+ $constvar = const(str, ...) # create the const key
193
+ # CREATE THE ARGUMENT, This may take multiple lines.
194
+ $created_arg = ...
195
+ $var = getattr(
196
+ value=$build_map_var,
197
+ attr=__setitem__,
198
+ )
199
+ $unused_var = call $var($constvar, $created_arg)
200
+
201
+ We iterate through the IR, deleting all usages of the buildmap
202
+ from the new_body, and adds the kws to a new kws list.
203
+ """
204
+ # Remove the build_map from the body.
205
+ new_body[buildmap_idx] = None
206
+ # Remove the definition.
207
+ _remove_assignment_definition(
208
+ old_body, buildmap_idx, func_ir, already_deleted_defs
209
+ )
210
+ kws = []
211
+ search_start = buildmap_idx + 1
212
+ while search_start <= search_end:
213
+ # The first value must be a constant.
214
+ const_stmt = old_body[search_start]
215
+ if not (
216
+ isinstance(const_stmt, ir.Assign)
217
+ and isinstance(const_stmt.value, ir.Const)
218
+ ):
219
+ # We cannot handle this format so raise the
220
+ # original error message.
221
+ raise UnsupportedBytecodeError(errmsg)
222
+ key_var_name = const_stmt.target.name
223
+ key_val = const_stmt.value.value
224
+ search_start += 1
225
+ # Now we need to search for a getattr with setitem
226
+ found_getattr = False
227
+ while search_start <= search_end and not found_getattr:
228
+ getattr_stmt = old_body[search_start]
229
+ if (
230
+ isinstance(getattr_stmt, ir.Assign)
231
+ and isinstance(getattr_stmt.value, ir.Expr)
232
+ and getattr_stmt.value.op == "getattr"
233
+ and (getattr_stmt.value.value.name == buildmap_name)
234
+ and getattr_stmt.value.attr == "__setitem__"
235
+ ):
236
+ found_getattr = True
237
+ else:
238
+ # If the argument is "created" in JIT, then there
239
+ # will be intermediate operations in between setitems.
240
+ # For example we have arg5=pow(arg5, 2),
241
+ # then the IR would look like:
242
+ #
243
+ # # Creation of the constant key.
244
+ # $const44.26 = const(str, arg5)
245
+ #
246
+ # # Argument creation. This is the section we are skipping
247
+ # $46load_global.27 = global(pow: <built-in function pow>)
248
+ # $const50.29 = const(int, 2)
249
+ # $call.30 = call $46load_global.27(arg5, $const50.29)
250
+ #
251
+ # # Setitem with arg5
252
+ # $54map_add.31 = getattr(value=$map.2, attr=__setitem__)
253
+ # $54map_add.32 = call $54map_add.31($const44.26, $call.30)
254
+ search_start += 1
255
+ if not found_getattr or search_start == search_end:
256
+ # We cannot handle this format so raise the
257
+ # original error message.
258
+ raise UnsupportedBytecodeError(errmsg)
259
+ setitem_stmt = old_body[search_start + 1]
260
+ if not (
261
+ isinstance(setitem_stmt, ir.Assign)
262
+ and isinstance(setitem_stmt.value, ir.Expr)
263
+ and setitem_stmt.value.op == "call"
264
+ and (setitem_stmt.value.func.name == getattr_stmt.target.name)
265
+ and len(setitem_stmt.value.args) == 2
266
+ and (setitem_stmt.value.args[0].name == key_var_name)
267
+ ):
268
+ # A call statement should always immediately follow the
269
+ # getattr. If for some reason this doesn't match the code
270
+ # format, we raise the original error message. This check
271
+ # is meant as a precaution.
272
+ raise UnsupportedBytecodeError(errmsg)
273
+ arg_var = setitem_stmt.value.args[1]
274
+ # Append the (key, value) pair.
275
+ kws.append((key_val, arg_var))
276
+ # Remove the __setitem__ getattr and call
277
+ new_body[search_start] = None
278
+ new_body[search_start + 1] = None
279
+ # Remove the definitions.
280
+ _remove_assignment_definition(
281
+ old_body, search_start, func_ir, already_deleted_defs
282
+ )
283
+ _remove_assignment_definition(
284
+ old_body, search_start + 1, func_ir, already_deleted_defs
285
+ )
286
+ search_start += 2
287
+ return kws
288
+
289
+
290
+ def _call_function_ex_replace_args_small(
291
+ old_body,
292
+ tuple_expr,
293
+ new_body,
294
+ buildtuple_idx,
295
+ func_ir,
296
+ already_deleted_defs,
297
+ ):
298
+ """
299
+ Extracts the args passed as vararg
300
+ for CALL_FUNCTION_EX. This pass is taken when
301
+ n_args <= 30 and the bytecode looks like:
302
+
303
+ # Start for each argument
304
+ LOAD_FAST # Load each argument.
305
+ # End for each argument
306
+ ...
307
+ BUILD_TUPLE # Create a tuple of the arguments
308
+
309
+ In the IR generated, the vararg refer
310
+ to a single build_tuple that contains all of the
311
+ args. In addition to returning the args, this
312
+ function updates new_body to remove all usage
313
+ of the tuple.
314
+ """
315
+ # Delete the build tuple
316
+ new_body[buildtuple_idx] = None
317
+ # Remove the definition.
318
+ _remove_assignment_definition(
319
+ old_body, buildtuple_idx, func_ir, already_deleted_defs
320
+ )
321
+ # Return the args.
322
+ return tuple_expr.items
323
+
324
+
325
+ def _call_function_ex_replace_args_large(
326
+ old_body,
327
+ vararg_stmt,
328
+ new_body,
329
+ search_end,
330
+ func_ir,
331
+ errmsg,
332
+ already_deleted_defs,
333
+ ):
334
+ """
335
+ Extracts the args passed as vararg
336
+ for CALL_FUNCTION_EX. This pass is taken when
337
+ n_args > 30 and the bytecode looks like:
338
+
339
+ BUILD_TUPLE # Create a list to append to
340
+ # Start for each argument
341
+ LOAD_FAST # Load each argument.
342
+ LIST_APPEND # Add the argument to the list
343
+ # End for each argument
344
+ ...
345
+ LIST_TO_TUPLE # Convert the args to a tuple.
346
+
347
+ In the IR generated, the tuple is created by concatenating
348
+ together several 1 element tuples to an initial empty tuple.
349
+ We traverse backwards in the IR, collecting args, until we
350
+ find the original empty tuple. For example, the IR might
351
+ look like:
352
+
353
+ $orig_tuple = build_tuple(items=[])
354
+ $first_var = build_tuple(items=[Var(arg0, test.py:6)])
355
+ $next_tuple = $orig_tuple + $first_var
356
+ ...
357
+ $final_var = build_tuple(items=[Var(argn, test.py:6)])
358
+ $final_tuple = $prev_tuple + $final_var
359
+ $varargs_var = $final_tuple
360
+ """
361
+ # We traverse to the front of the block to look for the original
362
+ # tuple.
363
+ search_start = 0
364
+ total_args = []
365
+ if isinstance(vararg_stmt, ir.Assign) and isinstance(
366
+ vararg_stmt.value, ir.Var
367
+ ):
368
+ target_name = vararg_stmt.value.name
369
+ # If there is an initial assignment, delete it
370
+ new_body[search_end] = None
371
+ # Remove the definition.
372
+ _remove_assignment_definition(
373
+ old_body, search_end, func_ir, already_deleted_defs
374
+ )
375
+ search_end -= 1
376
+ else:
377
+ # There must always be an initial assignment
378
+ # https://github.com/numba/numba/blob/59fa2e335be68148b3bd72a29de3ff011430038d/numba/core/interpreter.py#L259-L260
379
+ # If this changes we may need to support this branch.
380
+ raise AssertionError("unreachable")
381
+ # Traverse backwards to find all concatenations
382
+ # until eventually reaching the original empty tuple.
383
+ while search_end >= search_start:
384
+ concat_stmt = old_body[search_end]
385
+ if (
386
+ isinstance(concat_stmt, ir.Assign)
387
+ and concat_stmt.target.name == target_name
388
+ and isinstance(concat_stmt.value, ir.Expr)
389
+ and concat_stmt.value.op == "build_tuple"
390
+ and not concat_stmt.value.items
391
+ ):
392
+ new_body[search_end] = None
393
+ # Remove the definition.
394
+ _remove_assignment_definition(
395
+ old_body, search_end, func_ir, already_deleted_defs
396
+ )
397
+ # If we have reached the build_tuple we exit.
398
+ break
399
+ else:
400
+ # We expect to find another arg to append.
401
+ # The first stmt must be a binop "add"
402
+ if (search_end == search_start) or not (
403
+ isinstance(concat_stmt, ir.Assign)
404
+ and (concat_stmt.target.name == target_name)
405
+ and isinstance(concat_stmt.value, ir.Expr)
406
+ and concat_stmt.value.op == "binop"
407
+ and concat_stmt.value.fn == operator.add
408
+ ):
409
+ # We cannot handle this format.
410
+ raise UnsupportedBytecodeError(errmsg)
411
+ lhs_name = concat_stmt.value.lhs.name
412
+ rhs_name = concat_stmt.value.rhs.name
413
+ # The previous statement should be a
414
+ # build_tuple containing the arg.
415
+ arg_tuple_stmt = old_body[search_end - 1]
416
+ if not (
417
+ isinstance(arg_tuple_stmt, ir.Assign)
418
+ and isinstance(arg_tuple_stmt.value, ir.Expr)
419
+ and (arg_tuple_stmt.value.op == "build_tuple")
420
+ and len(arg_tuple_stmt.value.items) == 1
421
+ ):
422
+ # We cannot handle this format.
423
+ raise UnsupportedBytecodeError(errmsg)
424
+ if arg_tuple_stmt.target.name == lhs_name:
425
+ # The tuple should always be generated on the RHS.
426
+ raise AssertionError("unreachable")
427
+ elif arg_tuple_stmt.target.name == rhs_name:
428
+ target_name = lhs_name
429
+ else:
430
+ # We cannot handle this format.
431
+ raise UnsupportedBytecodeError(errmsg)
432
+ total_args.append(arg_tuple_stmt.value.items[0])
433
+ new_body[search_end] = None
434
+ new_body[search_end - 1] = None
435
+ # Remove the definitions.
436
+ _remove_assignment_definition(
437
+ old_body, search_end, func_ir, already_deleted_defs
438
+ )
439
+ _remove_assignment_definition(
440
+ old_body, search_end - 1, func_ir, already_deleted_defs
441
+ )
442
+ search_end -= 2
443
+ # Avoid any space between appends
444
+ keep_looking = True
445
+ while search_end >= search_start and keep_looking:
446
+ next_stmt = old_body[search_end]
447
+ if isinstance(next_stmt, ir.Assign) and (
448
+ next_stmt.target.name == target_name
449
+ ):
450
+ keep_looking = False
451
+ else:
452
+ # If the argument is "created" in JIT, then there
453
+ # will be intermediate operations in between appends.
454
+ # For example if the next arg after arg4 is pow(arg5, 2),
455
+ # then the IR would look like:
456
+ #
457
+ # # Appending arg4
458
+ # $arg4_tup = build_tuple(items=[arg4])
459
+ # $append_var.5 = $append_var.4 + $arg4_tup
460
+ #
461
+ # # Creation of arg5.
462
+ # # This is the section that we are skipping.
463
+ # $32load_global.20 = global(pow: <built-in function pow>)
464
+ # $const36.22 = const(int, 2)
465
+ # $call.23 = call $32load_global.20(arg5, $const36.22)
466
+ #
467
+ # # Appending arg5
468
+ # $arg5_tup = build_tuple(items=[$call.23])
469
+ # $append_var.6 = $append_var.5 + $arg5_tup
470
+ search_end -= 1
471
+ if search_end == search_start:
472
+ # If we reached the start we never found the build_tuple.
473
+ # We cannot handle this format so raise the
474
+ # original error message.
475
+ raise UnsupportedBytecodeError(errmsg)
476
+ # Reverse the arguments so we get the correct order.
477
+ return total_args[::-1]
478
+
479
+
480
+ def peep_hole_call_function_ex_to_call_function_kw(func_ir):
481
+ """
482
+ This peephole rewrites a bytecode sequence unique to Python 3.10
483
+ where CALL_FUNCTION_EX is used instead of CALL_FUNCTION_KW because of
484
+ stack limitations set by CPython. This limitation is imposed whenever
485
+ a function call has too many arguments or keyword arguments.
486
+
487
+ https://github.com/python/cpython/blob/a58ebcc701dd6c43630df941481475ff0f615a81/Python/compile.c#L55
488
+ https://github.com/python/cpython/blob/a58ebcc701dd6c43630df941481475ff0f615a81/Python/compile.c#L4442
489
+
490
+ In particular, this change is imposed whenever (n_args / 2) + n_kws > 15.
491
+
492
+ Different bytecode is generated for args depending on if n_args > 30
493
+ or n_args <= 30 and similarly if n_kws > 15 or n_kws <= 15.
494
+
495
+ This function unwraps the *args and **kwargs in the function call
496
+ and places these values directly into the args and kwargs of the call.
497
+ """
498
+ # All changes are local to the a single block
499
+ # so it can be traversed in any order.
500
+ errmsg = textwrap.dedent("""
501
+ CALL_FUNCTION_EX with **kwargs not supported.
502
+ If you are not using **kwargs this may indicate that
503
+ you have a large number of kwargs and are using inlined control
504
+ flow. You can resolve this issue by moving the control flow out of
505
+ the function call. For example, if you have
506
+
507
+ f(a=1 if flag else 0, ...)
508
+
509
+ Replace that with:
510
+
511
+ a_val = 1 if flag else 0
512
+ f(a=a_val, ...)""")
513
+
514
+ # Track which definitions have already been deleted
515
+ already_deleted_defs = collections.defaultdict(set)
516
+ for blk in func_ir.blocks.values():
517
+ blk_changed = False
518
+ new_body = []
519
+ for i, stmt in enumerate(blk.body):
520
+ if (
521
+ isinstance(stmt, ir.Assign)
522
+ and isinstance(stmt.value, ir.Expr)
523
+ and stmt.value.op == "call"
524
+ and stmt.value.varkwarg is not None
525
+ ):
526
+ blk_changed = True
527
+ call = stmt.value
528
+ args = call.args
529
+ kws = call.kws
530
+ # We need to check the call expression contents if
531
+ # it contains either vararg or varkwarg. If it contains
532
+ # varkwarg we need to update the IR. If it just contains
533
+ # vararg we don't need to update the IR, but we need to
534
+ # check if peep_hole_list_to_tuple failed to replace the
535
+ # vararg list with a tuple. If so, we output an error
536
+ # message with suggested code changes.
537
+ vararg = call.vararg
538
+ varkwarg = call.varkwarg
539
+ start_search = i - 1
540
+ # varkwarg should be defined second so we start there.
541
+ varkwarg_loc = start_search
542
+ keyword_def = None
543
+ found = False
544
+ while varkwarg_loc >= 0 and not found:
545
+ keyword_def = blk.body[varkwarg_loc]
546
+ if (
547
+ isinstance(keyword_def, ir.Assign)
548
+ and keyword_def.target.name == varkwarg.name
549
+ ):
550
+ found = True
551
+ else:
552
+ varkwarg_loc -= 1
553
+ if (
554
+ kws
555
+ or not found
556
+ or not (
557
+ isinstance(keyword_def.value, ir.Expr)
558
+ and keyword_def.value.op == "build_map"
559
+ )
560
+ ):
561
+ # If we couldn't find where the kwargs are created
562
+ # then it should be a normal **kwargs call
563
+ # so we produce an unsupported message.
564
+ raise UnsupportedBytecodeError(errmsg)
565
+ # Determine the kws
566
+ if keyword_def.value.items:
567
+ # n_kws <= 15 case.
568
+ # Here the IR looks like a series of
569
+ # constants, then the arguments and finally
570
+ # a build_map that contains all of the pairs.
571
+ # For Example:
572
+ #
573
+ # $const_n = const("arg_name")
574
+ # $arg_n = ...
575
+ # $kwargs_var = build_map(items=[
576
+ # ($const_0, $arg_0),
577
+ # ...,
578
+ # ($const_n, $arg_n),])
579
+ kws = _call_function_ex_replace_kws_small(
580
+ blk.body,
581
+ keyword_def.value,
582
+ new_body,
583
+ varkwarg_loc,
584
+ func_ir,
585
+ already_deleted_defs,
586
+ )
587
+ else:
588
+ # n_kws > 15 case.
589
+ # Here the IR is an initial empty build_map
590
+ # followed by a series of setitems with a constant
591
+ # key and then the argument.
592
+ # For example:
593
+ #
594
+ # $kwargs_var = build_map(items=[])
595
+ # $const_0 = const("arg_name")
596
+ # $arg_0 = ...
597
+ # $my_attr = getattr(const_0, attr=__setitem__)
598
+ # $unused_var = call $my_attr($const_0, $arg_0)
599
+ # ...
600
+ kws = _call_function_ex_replace_kws_large(
601
+ blk.body,
602
+ varkwarg.name,
603
+ varkwarg_loc,
604
+ i - 1,
605
+ new_body,
606
+ func_ir,
607
+ errmsg,
608
+ already_deleted_defs,
609
+ )
610
+ start_search = varkwarg_loc
611
+ # Vararg isn't required to be provided.
612
+ if vararg is not None:
613
+ if args:
614
+ # If we have vararg then args is expected to
615
+ # be an empty list.
616
+ raise UnsupportedBytecodeError(errmsg)
617
+ vararg_loc = start_search
618
+ args_def = None
619
+ found = False
620
+ while vararg_loc >= 0 and not found:
621
+ args_def = blk.body[vararg_loc]
622
+ if (
623
+ isinstance(args_def, ir.Assign)
624
+ and args_def.target.name == vararg.name
625
+ ):
626
+ found = True
627
+ else:
628
+ vararg_loc -= 1
629
+ if not found:
630
+ # If we couldn't find where the args are created
631
+ # then we can't handle this format.
632
+ raise UnsupportedBytecodeError(errmsg)
633
+ if (
634
+ isinstance(args_def.value, ir.Expr)
635
+ and args_def.value.op == "build_tuple"
636
+ ):
637
+ # n_args <= 30 case.
638
+ # Here the IR is a simple build_tuple containing
639
+ # all of the args.
640
+ # For example:
641
+ #
642
+ # $arg_n = ...
643
+ # $varargs = build_tuple(
644
+ # items=[$arg_0, ..., $arg_n]
645
+ # )
646
+ args = _call_function_ex_replace_args_small(
647
+ blk.body,
648
+ args_def.value,
649
+ new_body,
650
+ vararg_loc,
651
+ func_ir,
652
+ already_deleted_defs,
653
+ )
654
+ elif (
655
+ isinstance(args_def.value, ir.Expr)
656
+ and args_def.value.op == "list_to_tuple"
657
+ ):
658
+ # If there is a call with vararg we need to check
659
+ # if the list -> tuple conversion failed and if so
660
+ # throw an error.
661
+ raise UnsupportedBytecodeError(errmsg)
662
+ else:
663
+ # Here the IR is an initial empty build_tuple.
664
+ # Then for each arg, a new tuple with a single
665
+ # element is created and one by one these are
666
+ # added to a growing tuple.
667
+ # For example:
668
+ #
669
+ # $combo_tup_0 = build_tuple(items=[])
670
+ # $arg0 = ...
671
+ # $arg0_tup = build_tuple(items=[$arg0])
672
+ # $combo_tup_1 = $combo_tup_0 + $arg0_tup
673
+ # $arg1 = ...
674
+ # $arg1_tup = build_tuple(items=[$arg1])
675
+ # $combo_tup_2 = $combo_tup_1 + $arg1_tup
676
+ # ...
677
+ # $combo_tup_n = $combo_tup_{n-1} + $argn_tup
678
+ #
679
+ # In addition, the IR contains a final
680
+ # assignment for the varargs that looks like:
681
+ #
682
+ # $varargs_var = $combo_tup_n
683
+ #
684
+ # Here args_def is expected to be a simple assignment.
685
+ args = _call_function_ex_replace_args_large(
686
+ blk.body,
687
+ args_def,
688
+ new_body,
689
+ vararg_loc,
690
+ func_ir,
691
+ errmsg,
692
+ already_deleted_defs,
693
+ )
694
+ # Create a new call updating the args and kws
695
+ new_call = ir.Expr.call(
696
+ call.func, args, kws, call.loc, target=call.target
697
+ )
698
+ # Drop the existing definition for this stmt.
699
+ _remove_assignment_definition(
700
+ blk.body, i, func_ir, already_deleted_defs
701
+ )
702
+ # Update the statement
703
+ stmt = ir.Assign(new_call, stmt.target, stmt.loc)
704
+ # Update the definition
705
+ func_ir._definitions[stmt.target.name].append(new_call)
706
+ elif (
707
+ isinstance(stmt, ir.Assign)
708
+ and isinstance(stmt.value, ir.Expr)
709
+ and stmt.value.op == "call"
710
+ and stmt.value.vararg is not None
711
+ ):
712
+ # If there is a call with vararg we need to check
713
+ # if the list -> tuple conversion failed and if so
714
+ # throw an error.
715
+ call = stmt.value
716
+ vararg_name = call.vararg.name
717
+ if (
718
+ vararg_name in func_ir._definitions
719
+ and len(func_ir._definitions[vararg_name]) == 1
720
+ ):
721
+ # If this value is still a list to tuple raise the
722
+ # exception.
723
+ expr = func_ir._definitions[vararg_name][0]
724
+ if isinstance(expr, ir.Expr) and expr.op == "list_to_tuple":
725
+ raise UnsupportedBytecodeError(errmsg)
726
+
727
+ new_body.append(stmt)
728
+ # Replace the block body if we changed the IR
729
+ if blk_changed:
730
+ blk.body.clear()
731
+ blk.body.extend([x for x in new_body if x is not None])
732
+ return func_ir
733
+
734
+
735
+ def peep_hole_list_to_tuple(func_ir):
736
+ """
737
+ This peephole rewrites a bytecode sequence new to Python 3.9 that looks
738
+ like e.g.:
739
+
740
+ def foo(a):
741
+ return (*a,)
742
+
743
+ 41 0 BUILD_LIST 0
744
+ 2 LOAD_FAST 0 (a)
745
+ 4 LIST_EXTEND 1
746
+ 6 LIST_TO_TUPLE
747
+ 8 RETURN_VAL
748
+
749
+ essentially, the unpacking of tuples is written as a list which is appended
750
+ to/extended and then "magicked" into a tuple by the new LIST_TO_TUPLE
751
+ opcode.
752
+
753
+ This peephole repeatedly analyses the bytecode in a block looking for a
754
+ window between a `LIST_TO_TUPLE` and `BUILD_LIST` and...
755
+
756
+ 1. Turns the BUILD_LIST into a BUILD_TUPLE
757
+ 2. Sets an accumulator's initial value as the target of the BUILD_TUPLE
758
+ 3. Searches for 'extend' on the original list and turns these into binary
759
+ additions on the accumulator.
760
+ 4. Searches for 'append' on the original list and turns these into a
761
+ `BUILD_TUPLE` which is then appended via binary addition to the
762
+ accumulator.
763
+ 5. Assigns the accumulator to the variable that exits the peephole and the
764
+ rest of the block/code refers to as the result of the unpack operation.
765
+ 6. Patches up
766
+ """
767
+ _DEBUG = False
768
+
769
+ # For all blocks
770
+ for offset, blk in func_ir.blocks.items():
771
+ # keep doing the peephole rewrite until nothing is left that matches
772
+ while True:
773
+ # first try and find a matching region
774
+ # i.e. BUILD_LIST...<stuff>...LIST_TO_TUPLE
775
+ def find_postive_region():
776
+ found = False
777
+ for idx in reversed(range(len(blk.body))):
778
+ stmt = blk.body[idx]
779
+ if isinstance(stmt, ir.Assign):
780
+ value = stmt.value
781
+ if (
782
+ isinstance(value, ir.Expr)
783
+ and value.op == "list_to_tuple"
784
+ ):
785
+ target_list = value.info[0]
786
+ found = True
787
+ bt = (idx, stmt)
788
+ if found:
789
+ if isinstance(stmt, ir.Assign):
790
+ if stmt.target.name == target_list:
791
+ region = (bt, (idx, stmt))
792
+ return region
793
+
794
+ region = find_postive_region()
795
+ # if there's a peep hole region then do something with it
796
+ if region is not None:
797
+ peep_hole = blk.body[region[1][0] : region[0][0]]
798
+ if _DEBUG:
799
+ print("\nWINDOW:")
800
+ for x in peep_hole:
801
+ print(x)
802
+ print("")
803
+
804
+ appends = []
805
+ extends = []
806
+ init = region[1][1]
807
+ const_list = init.target.name
808
+ # Walk through the peep_hole and find things that are being
809
+ # "extend"ed and "append"ed to the BUILD_LIST
810
+ for x in peep_hole:
811
+ if isinstance(x, ir.Assign):
812
+ if isinstance(x.value, ir.Expr):
813
+ expr = x.value
814
+ if (
815
+ expr.op == "getattr"
816
+ and expr.value.name == const_list
817
+ ):
818
+ # it's not strictly necessary to split out
819
+ # extends and appends, but it helps with
820
+ # debugging to do so!
821
+ if expr.attr == "extend":
822
+ extends.append(x.target.name)
823
+ elif expr.attr == "append":
824
+ appends.append(x.target.name)
825
+ else:
826
+ assert 0
827
+ # go back through the peep hole build new IR based on it.
828
+ new_hole = []
829
+
830
+ def append_and_fix(x):
831
+ """Adds to the new_hole and fixes up definitions"""
832
+ new_hole.append(x)
833
+ if x.target.name in func_ir._definitions:
834
+ # if there's already a definition, drop it, should only
835
+ # be 1 as the way cpython emits the sequence for
836
+ # `list_to_tuple` should ensure this.
837
+ assert len(func_ir._definitions[x.target.name]) == 1
838
+ func_ir._definitions[x.target.name].clear()
839
+ func_ir._definitions[x.target.name].append(x.value)
840
+
841
+ the_build_list = init.target
842
+
843
+ # Do the transform on the peep hole
844
+ if _DEBUG:
845
+ print("\nBLOCK:")
846
+ blk.dump()
847
+
848
+ # This section basically accumulates list appends and extends
849
+ # as binop(+) on tuples, it drops all the getattr() for extend
850
+ # and append as they are now dead and replaced with binop(+).
851
+ # It also switches out the build_list for a build_tuple and then
852
+ # ensures everything is wired up and defined ok.
853
+ t2l_agn = region[0][1]
854
+ acc = the_build_list
855
+ for x in peep_hole:
856
+ if isinstance(x, ir.Assign):
857
+ if isinstance(x.value, ir.Expr):
858
+ expr = x.value
859
+ if expr.op == "getattr":
860
+ if (
861
+ x.target.name in extends
862
+ or x.target.name in appends
863
+ ):
864
+ # drop definition, it's being wholesale
865
+ # replaced.
866
+ func_ir._definitions.pop(x.target.name)
867
+ continue
868
+ else:
869
+ # a getattr on something we're not
870
+ # interested in
871
+ new_hole.append(x)
872
+ elif expr.op == "call":
873
+ fname = expr.func.name
874
+ if fname in extends or fname in appends:
875
+ arg = expr.args[0]
876
+ if isinstance(arg, ir.Var):
877
+ tmp_name = "%s_var_%s" % (
878
+ fname,
879
+ arg.name,
880
+ )
881
+ if fname in appends:
882
+ bt = ir.Expr.build_tuple(
883
+ [
884
+ arg,
885
+ ],
886
+ expr.loc,
887
+ )
888
+ else:
889
+ # Extend as tuple
890
+ gv_tuple = ir.Global(
891
+ name="tuple",
892
+ value=tuple,
893
+ loc=expr.loc,
894
+ )
895
+ tuple_var = arg.scope.redefine(
896
+ "$_list_extend_gv_tuple",
897
+ loc=expr.loc,
898
+ )
899
+ new_hole.append(
900
+ ir.Assign(
901
+ target=tuple_var,
902
+ value=gv_tuple,
903
+ loc=expr.loc,
904
+ ),
905
+ )
906
+ bt = ir.Expr.call(
907
+ tuple_var,
908
+ (arg,),
909
+ (),
910
+ loc=expr.loc,
911
+ )
912
+ var = ir.Var(
913
+ arg.scope, tmp_name, expr.loc
914
+ )
915
+ asgn = ir.Assign(bt, var, expr.loc)
916
+ append_and_fix(asgn)
917
+ arg = var
918
+
919
+ # this needs to be a binary add
920
+ new = ir.Expr.binop(
921
+ fn=operator.add,
922
+ lhs=acc,
923
+ rhs=arg,
924
+ loc=x.loc,
925
+ )
926
+ asgn = ir.Assign(new, x.target, expr.loc)
927
+ append_and_fix(asgn)
928
+ acc = asgn.target
929
+ else:
930
+ # there could be a call in the unpack, like
931
+ # *(a, x.append(y))
932
+ new_hole.append(x)
933
+ elif (
934
+ expr.op == "build_list"
935
+ and x.target.name == const_list
936
+ ):
937
+ new = ir.Expr.build_tuple(expr.items, expr.loc)
938
+ asgn = ir.Assign(new, x.target, expr.loc)
939
+ # Not a temporary any more
940
+ append_and_fix(asgn)
941
+ else:
942
+ new_hole.append(x)
943
+ else:
944
+ new_hole.append(x)
945
+
946
+ else:
947
+ # stick everything else in as-is
948
+ new_hole.append(x)
949
+ # Finally write the result back into the original build list as
950
+ # everything refers to it.
951
+ append_and_fix(
952
+ ir.Assign(acc, t2l_agn.target, the_build_list.loc)
953
+ )
954
+ if _DEBUG:
955
+ print("\nNEW HOLE:")
956
+ for x in new_hole:
957
+ print(x)
958
+
959
+ # and then update the block body with the modified region
960
+ cpy = blk.body[:]
961
+ head = cpy[: region[1][0]]
962
+ tail = blk.body[region[0][0] + 1 :]
963
+ tmp = head + new_hole + tail
964
+ blk.body.clear()
965
+ blk.body.extend(tmp)
966
+
967
+ if _DEBUG:
968
+ print("\nDUMP post hole:")
969
+ blk.dump()
970
+
971
+ else:
972
+ # else escape
973
+ break
974
+
975
+ return func_ir
976
+
977
+
978
+ def peep_hole_delete_with_exit(func_ir):
979
+ """
980
+ This rewrite removes variables used to store the `__exit__` function
981
+ loaded by SETUP_WITH.
982
+ """
983
+ dead_vars = set()
984
+
985
+ for blk in func_ir.blocks.values():
986
+ for stmt in blk.body:
987
+ # Any statement that uses a variable with the '$setup_with_exitfn'
988
+ # prefix is considered dead.
989
+ used = set(stmt.list_vars())
990
+ for v in used:
991
+ if v.name.startswith("$setup_with_exitfn"):
992
+ dead_vars.add(v)
993
+ # Any assignment that uses any of the dead variable is considered
994
+ # dead.
995
+ if used & dead_vars:
996
+ if isinstance(stmt, ir.Assign):
997
+ dead_vars.add(stmt.target)
998
+
999
+ new_body = []
1000
+ for stmt in blk.body:
1001
+ # Skip any statements that uses anyone of the dead variable.
1002
+ if not (set(stmt.list_vars()) & dead_vars):
1003
+ new_body.append(stmt)
1004
+ blk.body.clear()
1005
+ blk.body.extend(new_body)
1006
+
1007
+ return func_ir
1008
+
1009
+
1010
+ def peep_hole_fuse_dict_add_updates(func_ir):
1011
+ """
1012
+ This rewrite removes d1._update_from_bytecode(d2)
1013
+ calls that are between two dictionaries, d1 and d2,
1014
+ in the same basic block. This pattern can appear as a
1015
+ result of Python 3.10 bytecode emission changes, which
1016
+ prevent large constant literal dictionaries
1017
+ (> 15 elements) from being constant. If both dictionaries
1018
+ are constant dictionaries defined in the same block and
1019
+ neither is used between the update call, then we replace d1
1020
+ with a new definition that combines the two dictionaries. At
1021
+ the bytecode translation stage we convert DICT_UPDATE into
1022
+ _update_from_bytecode, so we know that _update_from_bytecode
1023
+ always comes from the bytecode change and not user code.
1024
+
1025
+ Python 3.10 may also rewrite the individual dictionaries
1026
+ as an empty build_map + many map_add. Here we again look
1027
+ for an _update_from_bytecode, and if so we replace these
1028
+ with a single constant dictionary.
1029
+
1030
+ When running this algorithm we can always safely remove d2.
1031
+
1032
+ This is the relevant section of the CPython 3.10 that causes
1033
+ this bytecode change:
1034
+ https://github.com/python/cpython/blob/3.10/Python/compile.c#L4048
1035
+ """
1036
+
1037
+ # This algorithm fuses build_map expressions into the largest
1038
+ # possible build map before use. For example, if we have an
1039
+ # IR that looks like this:
1040
+ #
1041
+ # $d1 = build_map([])
1042
+ # $key = const("a")
1043
+ # $value = const(2)
1044
+ # $setitem_func = getattr($d1, "__setitem__")
1045
+ # $unused1 = call (setitem_func, ($key, $value))
1046
+ # $key2 = const("b")
1047
+ # $value2 = const(3)
1048
+ # $d2 = build_map([($key2, $value2)])
1049
+ # $update_func = getattr($d1, "_update_from_bytecode")
1050
+ # $unused2 = call ($update_func, ($d2,))
1051
+ # $othervar = None
1052
+ # $retvar = cast($othervar)
1053
+ # return $retvar
1054
+ #
1055
+ # Then the IR is rewritten such that any __setitem__ and
1056
+ # _update_from_bytecode operations are fused into the original buildmap.
1057
+ # The new buildmap is then added to the
1058
+ # last location where it had previously had encountered a __setitem__,
1059
+ # _update_from_bytecode, or build_map before any other uses.
1060
+ # The new IR would look like:
1061
+ #
1062
+ # $key = const("a")
1063
+ # $value = const(2)
1064
+ # $key2 = const("b")
1065
+ # $value2 = const(3)
1066
+ # $d1 = build_map([($key, $value), ($key2, $value2)])
1067
+ # $othervar = None
1068
+ # $retvar = cast($othervar)
1069
+ # return $retvar
1070
+ #
1071
+ # Note that we don't push $d1 to the bottom of the block. This is because
1072
+ # some values may be found below this block (e.g pop_block) that are pattern
1073
+ # matched in other locations, such as objmode handling. It should be safe to
1074
+ # move a map to the last location at which there was _update_from_bytecode.
1075
+
1076
+ errmsg = textwrap.dedent("""
1077
+ A DICT_UPDATE op-code was encountered that could not be replaced.
1078
+ If you have created a large constant dictionary, this may
1079
+ be an an indication that you are using inlined control
1080
+ flow. You can resolve this issue by moving the control flow out of
1081
+ the dicitonary constructor. For example, if you have
1082
+
1083
+ d = {a: 1 if flag else 0, ...)
1084
+
1085
+ Replace that with:
1086
+
1087
+ a_val = 1 if flag else 0
1088
+ d = {a: a_val, ...)""")
1089
+
1090
+ already_deleted_defs = collections.defaultdict(set)
1091
+ for blk in func_ir.blocks.values():
1092
+ new_body = []
1093
+ # literal map var name -> block idx of the original build_map
1094
+ lit_map_def_idx = {}
1095
+ # literal map var name -> list(map_uses)
1096
+ # This is the index of every build_map or __setitem__
1097
+ # in the IR that will need to be removed if the map
1098
+ # is updated.
1099
+ lit_map_use_idx = collections.defaultdict(list)
1100
+ # literal map var name -> list of key/value items for build map
1101
+ map_updates = {}
1102
+ blk_changed = False
1103
+
1104
+ for i, stmt in enumerate(blk.body):
1105
+ # What instruction should we append
1106
+ new_inst = stmt
1107
+ # Name that should be skipped when tracking used
1108
+ # vars in statement. This is always the lhs with
1109
+ # a build_map.
1110
+ stmt_build_map_out = None
1111
+ if isinstance(stmt, ir.Assign) and isinstance(stmt.value, ir.Expr):
1112
+ if stmt.value.op == "build_map":
1113
+ # Skip the output build_map when looking for used vars.
1114
+ stmt_build_map_out = stmt.target.name
1115
+ # If we encounter a build map add it to the
1116
+ # tracked maps.
1117
+ lit_map_def_idx[stmt.target.name] = i
1118
+ lit_map_use_idx[stmt.target.name].append(i)
1119
+ map_updates[stmt.target.name] = stmt.value.items.copy()
1120
+ elif stmt.value.op == "call" and i > 0:
1121
+ # If we encounter a call we may need to replace
1122
+ # the body
1123
+ func_name = stmt.value.func.name
1124
+ # If we have an update or a setitem
1125
+ # it will be the previous expression.
1126
+ getattr_stmt = blk.body[i - 1]
1127
+ args = stmt.value.args
1128
+ if (
1129
+ isinstance(getattr_stmt, ir.Assign)
1130
+ and getattr_stmt.target.name == func_name
1131
+ and isinstance(getattr_stmt.value, ir.Expr)
1132
+ and getattr_stmt.value.op == "getattr"
1133
+ and getattr_stmt.value.attr
1134
+ in ("__setitem__", "_update_from_bytecode")
1135
+ ):
1136
+ update_map_name = getattr_stmt.value.value.name
1137
+ attr = getattr_stmt.value.attr
1138
+ if (
1139
+ attr == "__setitem__"
1140
+ and update_map_name in lit_map_use_idx
1141
+ ):
1142
+ # If we have a setitem, update the lists
1143
+ map_updates[update_map_name].append(args)
1144
+ # Update the list of instructions that would
1145
+ # need to be removed to include the setitem
1146
+ # and the the getattr
1147
+ lit_map_use_idx[update_map_name].extend([i - 1, i])
1148
+ elif attr == "_update_from_bytecode":
1149
+ d2_map_name = args[0].name
1150
+ if (
1151
+ update_map_name in lit_map_use_idx
1152
+ and d2_map_name in lit_map_use_idx
1153
+ ):
1154
+ # If we have an update and the arg is also
1155
+ # a literal dictionary, fuse the lists.
1156
+ map_updates[update_map_name].extend(
1157
+ map_updates[d2_map_name]
1158
+ )
1159
+ # Delete the old IR for d1 and d2
1160
+ lit_map_use_idx[update_map_name].extend(
1161
+ lit_map_use_idx[d2_map_name]
1162
+ )
1163
+ lit_map_use_idx[update_map_name].append(i - 1)
1164
+ for linenum in lit_map_use_idx[update_map_name]:
1165
+ # Drop the existing definition.
1166
+ _remove_assignment_definition(
1167
+ blk.body,
1168
+ linenum,
1169
+ func_ir,
1170
+ already_deleted_defs,
1171
+ )
1172
+ # Delete it from the new block
1173
+ new_body[linenum] = None
1174
+ # Delete the maps from dicts
1175
+ del lit_map_def_idx[d2_map_name]
1176
+ del lit_map_use_idx[d2_map_name]
1177
+ del map_updates[d2_map_name]
1178
+ # Add d1 as the new instruction, removing the
1179
+ # old definition.
1180
+ _remove_assignment_definition(
1181
+ blk.body, i, func_ir, already_deleted_defs
1182
+ )
1183
+ new_inst = _build_new_build_map(
1184
+ func_ir,
1185
+ update_map_name,
1186
+ blk.body,
1187
+ lit_map_def_idx[update_map_name],
1188
+ map_updates[update_map_name],
1189
+ )
1190
+ # Update d1 in lit_map_use_idx to just the new
1191
+ # definition and clear the previous list.
1192
+ lit_map_use_idx[update_map_name].clear()
1193
+ lit_map_use_idx[update_map_name].append(i)
1194
+ # Mark that this block has been modified
1195
+ blk_changed = True
1196
+ else:
1197
+ # If we cannot remove _update_from_bytecode
1198
+ # Then raise an error for the user.
1199
+ raise UnsupportedBytecodeError(errmsg)
1200
+
1201
+ # Check if we need to drop any maps from being tracked.
1202
+ # Skip the setitem/_update_from_bytecode getattr that
1203
+ # will be removed when handling their call in the next
1204
+ # iteration.
1205
+ if not (
1206
+ isinstance(stmt, ir.Assign)
1207
+ and isinstance(stmt.value, ir.Expr)
1208
+ and stmt.value.op == "getattr"
1209
+ and stmt.value.value.name in lit_map_use_idx
1210
+ and stmt.value.attr in ("__setitem__", "_update_from_bytecode")
1211
+ ):
1212
+ for var in stmt.list_vars():
1213
+ # If a map is used it cannot be fused later in
1214
+ # the block. As a result we delete it from
1215
+ # the dicitonaries
1216
+ if (
1217
+ var.name in lit_map_use_idx
1218
+ and var.name != stmt_build_map_out
1219
+ ):
1220
+ del lit_map_def_idx[var.name]
1221
+ del lit_map_use_idx[var.name]
1222
+ del map_updates[var.name]
1223
+
1224
+ # Append the instruction to the new block
1225
+ new_body.append(new_inst)
1226
+
1227
+ if blk_changed:
1228
+ # If the block is changed replace the block body.
1229
+ blk.body.clear()
1230
+ blk.body.extend([x for x in new_body if x is not None])
1231
+
1232
+ return func_ir
1233
+
1234
+
1235
+ def peep_hole_split_at_pop_block(func_ir):
1236
+ """
1237
+ Split blocks that contain ir.PopBlock.
1238
+
1239
+ This rewrite restores the IR structure to pre 3.11 so that withlifting
1240
+ can work correctly.
1241
+ """
1242
+ new_block_map = {}
1243
+ sorted_blocks = sorted(func_ir.blocks.items())
1244
+ for blk_idx, (label, blk) in enumerate(sorted_blocks):
1245
+ # Gather locations of PopBlock
1246
+ pop_block_locs = []
1247
+ for i, inst in enumerate(blk.body):
1248
+ if isinstance(inst, ir.PopBlock):
1249
+ pop_block_locs.append(i)
1250
+ # Rewrite block with PopBlock
1251
+ if pop_block_locs:
1252
+ new_blocks = []
1253
+ for i in pop_block_locs:
1254
+ before_blk = ir.Block(blk.scope, loc=blk.loc)
1255
+ before_blk.body.extend(blk.body[:i])
1256
+ new_blocks.append(before_blk)
1257
+
1258
+ popblk_blk = ir.Block(blk.scope, loc=blk.loc)
1259
+ popblk_blk.body.append(blk.body[i])
1260
+ new_blocks.append(popblk_blk)
1261
+ # Add jump instructions
1262
+ prev_label = label
1263
+ for newblk in new_blocks:
1264
+ new_block_map[prev_label] = newblk
1265
+ next_label = prev_label + 1
1266
+ newblk.body.append(ir.Jump(next_label, loc=blk.loc))
1267
+ prev_label = next_label
1268
+ # Check prev_label does not exceed current new block label
1269
+ if blk_idx + 1 < len(sorted_blocks):
1270
+ if prev_label >= sorted_blocks[blk_idx + 1][0]:
1271
+ # Panic! Due to heuristic in with-lifting, block labels
1272
+ # must be monotonically increasing. We cannot continue if we
1273
+ # run out of usable label between the two blocks.
1274
+ raise errors.InternalError("POP_BLOCK peephole failed")
1275
+ # Add tail block, which will get the original terminator
1276
+ tail_blk = ir.Block(blk.scope, loc=blk.loc)
1277
+ tail_blk.body.extend(blk.body[pop_block_locs[-1] + 1 :])
1278
+ new_block_map[prev_label] = tail_blk
1279
+
1280
+ func_ir.blocks.update(new_block_map)
1281
+ return func_ir
1282
+
1283
+
1284
+ def _build_new_build_map(func_ir, name, old_body, old_lineno, new_items):
1285
+ """
1286
+ Create a new build_map with a new set of key/value items
1287
+ but all the other info the same.
1288
+ """
1289
+ old_assign = old_body[old_lineno]
1290
+ old_target = old_assign.target
1291
+ old_bm = old_assign.value
1292
+ # Build the literals
1293
+ literal_keys = []
1294
+ # Track the constant key/values to set the literal_value
1295
+ # field of build_map properly
1296
+ values = []
1297
+ for pair in new_items:
1298
+ k, v = pair
1299
+ key_def = ir_utils.guard(ir_utils.get_definition, func_ir, k)
1300
+ if isinstance(key_def, (ir.Const, ir.Global, ir.FreeVar)):
1301
+ literal_keys.append(key_def.value)
1302
+ value_def = ir_utils.guard(ir_utils.get_definition, func_ir, v)
1303
+ if isinstance(value_def, (ir.Const, ir.Global, ir.FreeVar)):
1304
+ values.append(value_def.value)
1305
+ else:
1306
+ # Append unknown value if not a literal.
1307
+ values.append(_UNKNOWN_VALUE(v.name))
1308
+
1309
+ value_indexes = {}
1310
+ if len(literal_keys) == len(new_items):
1311
+ # All keys must be literals to have any literal values.
1312
+ literal_value = {x: y for x, y in zip(literal_keys, values)}
1313
+ for i, k in enumerate(literal_keys):
1314
+ value_indexes[k] = i
1315
+ else:
1316
+ literal_value = None
1317
+
1318
+ # Construct a new build map.
1319
+ new_bm = ir.Expr.build_map(
1320
+ items=new_items,
1321
+ size=len(new_items),
1322
+ literal_value=literal_value,
1323
+ value_indexes=value_indexes,
1324
+ loc=old_bm.loc,
1325
+ )
1326
+
1327
+ # The previous definition has already been removed
1328
+ # when updating the IR in peep_hole_fuse_dict_add_updates
1329
+ func_ir._definitions[name].append(new_bm)
1330
+
1331
+ # Return a new assign.
1332
+ return ir.Assign(
1333
+ new_bm, ir.Var(old_target.scope, name, old_target.loc), new_bm.loc
1334
+ )
1335
+
1336
+
1337
+ class Interpreter(object):
1338
+ """A bytecode interpreter that builds up the IR."""
1339
+
1340
+ _DEBUG_PRINT = False
1341
+
1342
+ def __init__(self, func_id):
1343
+ self.func_id = func_id
1344
+ if self._DEBUG_PRINT:
1345
+ print(func_id.func)
1346
+ self.arg_count = func_id.arg_count
1347
+ self.arg_names = func_id.arg_names
1348
+ self.loc = self.first_loc = ir.Loc.from_function_id(func_id)
1349
+ self.is_generator = func_id.is_generator
1350
+
1351
+ # { inst offset : ir.Block }
1352
+ self.blocks = {}
1353
+ # { name: [definitions] } of local variables
1354
+ self.definitions = collections.defaultdict(list)
1355
+ # A set to keep track of all exception variables.
1356
+ # To be used in _legalize_exception_vars()
1357
+ self._exception_vars = set()
1358
+
1359
+ def interpret(self, bytecode):
1360
+ """
1361
+ Generate IR for this bytecode.
1362
+ """
1363
+ self.bytecode = bytecode
1364
+
1365
+ self.scopes = []
1366
+ global_scope = ir.Scope(parent=None, loc=self.loc)
1367
+ self.scopes.append(global_scope)
1368
+
1369
+ flow = Flow(bytecode)
1370
+ flow.run()
1371
+ self.dfa = AdaptDFA(flow)
1372
+ self.cfa = AdaptCFA(flow)
1373
+ if config.DUMP_CFG:
1374
+ self.cfa.dump()
1375
+
1376
+ # Temp states during interpretation
1377
+ self.current_block = None
1378
+ self.current_block_offset = None
1379
+ last_active_offset = 0
1380
+ for _, inst_blocks in self.cfa.blocks.items():
1381
+ if inst_blocks.body:
1382
+ last_active_offset = max(
1383
+ last_active_offset, max(inst_blocks.body)
1384
+ )
1385
+ self.last_active_offset = last_active_offset
1386
+
1387
+ if PYVERSION in ((3, 12), (3, 13)):
1388
+ self.active_exception_entries = tuple(
1389
+ [
1390
+ entry
1391
+ for entry in self.bytecode.exception_entries
1392
+ if entry.start < self.last_active_offset
1393
+ ]
1394
+ )
1395
+ elif PYVERSION in ((3, 10), (3, 11)):
1396
+ pass
1397
+ else:
1398
+ raise NotImplementedError(PYVERSION)
1399
+ self.syntax_blocks = []
1400
+ self.dfainfo = None
1401
+
1402
+ self.scopes.append(ir.Scope(parent=self.current_scope, loc=self.loc))
1403
+
1404
+ # Interpret loop
1405
+ for inst, kws in self._iter_inst():
1406
+ self._dispatch(inst, kws)
1407
+ if PYVERSION in ((3, 11), (3, 12), (3, 13)):
1408
+ # Insert end of try markers
1409
+ self._end_try_blocks()
1410
+ elif PYVERSION in ((3, 10),):
1411
+ pass
1412
+ else:
1413
+ raise NotImplementedError(PYVERSION)
1414
+ self._legalize_exception_vars()
1415
+ # Prepare FunctionIR
1416
+ func_ir = ir.FunctionIR(
1417
+ self.blocks,
1418
+ self.is_generator,
1419
+ self.func_id,
1420
+ self.first_loc,
1421
+ self.definitions,
1422
+ self.arg_count,
1423
+ self.arg_names,
1424
+ )
1425
+ _logger.debug(
1426
+ _lazy_pformat(func_ir, lazy_func=lambda x: x.dump_to_string())
1427
+ )
1428
+
1429
+ # post process the IR to rewrite opcodes/byte sequences that are too
1430
+ # involved to risk handling as part of direct interpretation
1431
+ peepholes = []
1432
+ if PYVERSION in ((3, 11), (3, 12), (3, 13)):
1433
+ peepholes.append(peep_hole_split_at_pop_block)
1434
+ if PYVERSION in ((3, 10), (3, 11), (3, 12), (3, 13)):
1435
+ peepholes.append(peep_hole_list_to_tuple)
1436
+ peepholes.append(peep_hole_delete_with_exit)
1437
+ if PYVERSION in ((3, 10), (3, 11), (3, 12), (3, 13)):
1438
+ # peep_hole_call_function_ex_to_call_function_kw
1439
+ # depends on peep_hole_list_to_tuple converting
1440
+ # any large number of arguments from a list to a
1441
+ # tuple.
1442
+ peepholes.append(peep_hole_call_function_ex_to_call_function_kw)
1443
+ peepholes.append(peep_hole_fuse_dict_add_updates)
1444
+
1445
+ post_processed_ir = self.post_process(peepholes, func_ir)
1446
+
1447
+ return post_processed_ir
1448
+
1449
+ def post_process(self, peepholes, func_ir):
1450
+ for peep in peepholes:
1451
+ func_ir = peep(func_ir)
1452
+ return func_ir
1453
+
1454
+ def _end_try_blocks(self):
1455
+ """Closes all try blocks by inserting the required marker at the
1456
+ exception handler
1457
+
1458
+ This is only needed for py3.11 because of the changes in exception
1459
+ handling. This merely maps the new py3.11 semantics back to the old way.
1460
+
1461
+ What the code does:
1462
+
1463
+ - For each block, compute the difference of blockstack to its incoming
1464
+ blocks' blockstack.
1465
+ - If the incoming blockstack has an extra TRY, the current block must
1466
+ be the EXCEPT block and we need to insert a marker.
1467
+
1468
+ See also: _insert_try_block_end
1469
+ """
1470
+ assert PYVERSION in ((3, 11), (3, 12), (3, 13))
1471
+ graph = self.cfa.graph
1472
+ for offset, block in self.blocks.items():
1473
+ # Get current blockstack
1474
+ cur_bs = self.dfa.infos[offset].blockstack
1475
+ # Check blockstack of the incoming blocks
1476
+ for inc, _ in graph.predecessors(offset):
1477
+ inc_bs = self.dfa.infos[inc].blockstack
1478
+
1479
+ # find first diff in the blockstack
1480
+ for i, (x, y) in enumerate(zip(cur_bs, inc_bs)):
1481
+ if x != y:
1482
+ break
1483
+ else:
1484
+ i = min(len(cur_bs), len(inc_bs))
1485
+
1486
+ def do_change(remain):
1487
+ while remain:
1488
+ ent = remain.pop()
1489
+ if ent["kind"] == BlockKind("TRY"):
1490
+ # Extend block with marker for end of try
1491
+ self.current_block = block
1492
+ oldbody = list(block.body)
1493
+ block.body.clear()
1494
+ self._insert_try_block_end()
1495
+ block.body.extend(oldbody)
1496
+ return True
1497
+
1498
+ if do_change(list(inc_bs[i:])):
1499
+ break
1500
+
1501
+ def _legalize_exception_vars(self):
1502
+ """Search for unsupported use of exception variables.
1503
+ Note, they cannot be stored into user variable.
1504
+ """
1505
+ # Build a set of exception variables
1506
+ excvars = self._exception_vars.copy()
1507
+ # Propagate the exception variables to LHS of assignment
1508
+ for varname, defnvars in self.definitions.items():
1509
+ for v in defnvars:
1510
+ if isinstance(v, ir.Var):
1511
+ k = v.name
1512
+ if k in excvars:
1513
+ excvars.add(varname)
1514
+ # Filter out the user variables.
1515
+ uservar = list(filter(lambda x: not x.startswith("$"), excvars))
1516
+ if uservar:
1517
+ # Complain about the first user-variable storing an exception
1518
+ first = uservar[0]
1519
+ loc = self.current_scope.get(first).loc
1520
+ msg = "Exception object cannot be stored into variable ({})."
1521
+ raise errors.UnsupportedBytecodeError(msg.format(first), loc=loc)
1522
+
1523
+ def init_first_block(self):
1524
+ # Define variables receiving the function arguments
1525
+ for index, name in enumerate(self.arg_names):
1526
+ val = ir.Arg(index=index, name=name, loc=self.loc)
1527
+ self.store(val, name)
1528
+
1529
+ def _iter_inst(self):
1530
+ for blkct, block in enumerate(self.cfa.iterliveblocks()):
1531
+ firstinst = self.bytecode[block.offset]
1532
+ # If its an END_FOR instruction, the start location of block
1533
+ # is set to start of the FOR loop, so take the location of
1534
+ # next instruction. This only affects the source location
1535
+ # marking and has no impact to semantic.
1536
+ if firstinst.opname == "END_FOR":
1537
+ firstinst = self.bytecode[firstinst.next]
1538
+ self.loc = self.loc.with_lineno(firstinst.lineno)
1539
+ self._start_new_block(block.offset)
1540
+ if blkct == 0:
1541
+ # Is first block
1542
+ self.init_first_block()
1543
+ for offset, kws in self.dfainfo.insts:
1544
+ inst = self.bytecode[offset]
1545
+ self.loc = self.loc.with_lineno(inst.lineno)
1546
+ yield inst, kws
1547
+ self._end_current_block()
1548
+
1549
+ def _start_new_block(self, offset):
1550
+ oldblock = self.current_block
1551
+ self.insert_block(offset)
1552
+
1553
+ tryblk = self.dfainfo.active_try_block if self.dfainfo else None
1554
+ # Ensure the last block is terminated
1555
+ if oldblock is not None and not oldblock.is_terminated:
1556
+ # Handle ending try block.
1557
+ # If there's an active try-block and the handler block is live.
1558
+ if tryblk is not None and tryblk["end"] in self.cfa.graph.nodes():
1559
+ # We are in a try-block, insert a branch to except-block.
1560
+ # This logic cannot be in self._end_current_block()
1561
+ # because we don't know the non-raising next block-offset.
1562
+ branch = ir.Branch(
1563
+ cond=self.get("$exception_check"),
1564
+ truebr=tryblk["end"],
1565
+ falsebr=offset,
1566
+ loc=self.loc,
1567
+ )
1568
+ oldblock.append(branch)
1569
+ # Handle normal case
1570
+ else:
1571
+ jmp = ir.Jump(offset, loc=self.loc)
1572
+ oldblock.append(jmp)
1573
+
1574
+ # Get DFA block info
1575
+ self.dfainfo = self.dfa.infos[self.current_block_offset]
1576
+ self.assigner = Assigner()
1577
+ # Check out-of-scope syntactic-block
1578
+ if PYVERSION in ((3, 11), (3, 12), (3, 13)):
1579
+ # This is recreating pre-3.11 code structure
1580
+ while self.syntax_blocks:
1581
+ if offset >= self.syntax_blocks[-1].exit:
1582
+ synblk = self.syntax_blocks.pop()
1583
+ if isinstance(synblk, ir.With):
1584
+ self.current_block.append(ir.PopBlock(self.loc))
1585
+ else:
1586
+ break
1587
+ # inject try block:
1588
+ newtryblk = self.dfainfo.active_try_block
1589
+ if newtryblk is not None:
1590
+ if newtryblk is not tryblk:
1591
+ self._insert_try_block_begin()
1592
+ elif PYVERSION in ((3, 10),):
1593
+ while self.syntax_blocks:
1594
+ if offset >= self.syntax_blocks[-1].exit:
1595
+ self.syntax_blocks.pop()
1596
+ else:
1597
+ break
1598
+ else:
1599
+ raise NotImplementedError(PYVERSION)
1600
+
1601
+ def _end_current_block(self):
1602
+ # Handle try block
1603
+ if not self.current_block.is_terminated:
1604
+ tryblk = self.dfainfo.active_try_block
1605
+ if tryblk is not None:
1606
+ self._insert_exception_check()
1607
+ # Handle normal block cleanup
1608
+ self._remove_unused_temporaries()
1609
+ self._insert_outgoing_phis()
1610
+
1611
+ def _inject_call(self, func, gv_name, res_name=None):
1612
+ """A helper function to inject a call to *func* which is a python
1613
+ function.
1614
+ Parameters
1615
+ ----------
1616
+ func : callable
1617
+ The function object to be called.
1618
+ gv_name : str
1619
+ The variable name to be used to store the function object.
1620
+ res_name : str; optional
1621
+ The variable name to be used to store the call result.
1622
+ If ``None``, a name is created automatically.
1623
+ """
1624
+ gv_fn = ir.Global(gv_name, func, loc=self.loc)
1625
+ self.store(value=gv_fn, name=gv_name, redefine=True)
1626
+ callres = ir.Expr.call(self.get(gv_name), (), (), loc=self.loc)
1627
+ res_name = res_name or "$callres_{}".format(gv_name)
1628
+ self.store(value=callres, name=res_name, redefine=True)
1629
+
1630
+ def _insert_try_block_begin(self):
1631
+ """Insert IR-nodes to mark the start of a `try` block."""
1632
+ self._inject_call(eh.mark_try_block, "mark_try_block")
1633
+
1634
+ def _insert_try_block_end(self):
1635
+ """Insert IR-nodes to mark the end of a `try` block."""
1636
+ self._inject_call(eh.end_try_block, "end_try_block")
1637
+
1638
+ def _insert_exception_variables(self):
1639
+ """Insert IR-nodes to initialize the exception variables."""
1640
+ tryblk = self.dfainfo.active_try_block
1641
+ # Get exception variables
1642
+ endblk = tryblk["end"]
1643
+ edgepushed = self.dfainfo.outgoing_edgepushed.get(endblk)
1644
+ # Note: the last value on the stack is the exception value
1645
+ # Note: due to the current limitation, all exception variables are None
1646
+ if edgepushed:
1647
+ const_none = ir.Const(value=None, loc=self.loc)
1648
+ # For each variable going to the handler block.
1649
+ for var in edgepushed:
1650
+ if var in self.definitions:
1651
+ raise AssertionError(
1652
+ "exception variable CANNOT be defined by other code",
1653
+ )
1654
+ self.store(value=const_none, name=var)
1655
+ self._exception_vars.add(var)
1656
+
1657
+ def _insert_exception_check(self):
1658
+ """Called before the end of a block to inject checks if raised."""
1659
+ self._insert_exception_variables()
1660
+ # Do exception check
1661
+ self._inject_call(
1662
+ eh.exception_check, "exception_check", "$exception_check"
1663
+ )
1664
+
1665
+ def _remove_unused_temporaries(self):
1666
+ """
1667
+ Remove assignments to unused temporary variables from the
1668
+ current block.
1669
+ """
1670
+ new_body = []
1671
+ replaced_var = {}
1672
+ for inst in self.current_block.body:
1673
+ # the same temporary is assigned to multiple variables in cases
1674
+ # like a = b[i] = 1, so need to handle replaced temporaries in
1675
+ # later setitem/setattr nodes
1676
+ if (
1677
+ isinstance(inst, (ir.SetItem, ir.SetAttr))
1678
+ and inst.value.name in replaced_var
1679
+ ):
1680
+ inst.value = replaced_var[inst.value.name]
1681
+ elif isinstance(inst, ir.Assign):
1682
+ if (
1683
+ inst.target.is_temp
1684
+ and inst.target.name in self.assigner.unused_dests
1685
+ ):
1686
+ continue
1687
+ # the same temporary is assigned to multiple variables in cases
1688
+ # like a = b = 1, so need to handle replaced temporaries in
1689
+ # later assignments
1690
+ if (
1691
+ isinstance(inst.value, ir.Var)
1692
+ and inst.value.name in replaced_var
1693
+ ):
1694
+ inst.value = replaced_var[inst.value.name]
1695
+ new_body.append(inst)
1696
+ continue
1697
+ # chained unpack cases may reuse temporary
1698
+ # e.g. a = (b, c) = (x, y)
1699
+ if (
1700
+ isinstance(inst.value, ir.Expr)
1701
+ and inst.value.op == "exhaust_iter"
1702
+ and inst.value.value.name in replaced_var
1703
+ ):
1704
+ inst.value.value = replaced_var[inst.value.value.name]
1705
+ new_body.append(inst)
1706
+ continue
1707
+ # eliminate temporary variables that are assigned to user
1708
+ # variables right after creation. E.g.:
1709
+ # $1 = f(); a = $1 -> a = f()
1710
+ # the temporary variable is not reused elsewhere since CPython
1711
+ # bytecode is stack-based and this pattern corresponds to a pop
1712
+ if (
1713
+ isinstance(inst.value, ir.Var)
1714
+ and inst.value.is_temp
1715
+ and new_body
1716
+ and isinstance(new_body[-1], ir.Assign)
1717
+ ):
1718
+ prev_assign = new_body[-1]
1719
+ # _var_used_in_binop check makes sure we don't create a new
1720
+ # inplace binop operation which can fail
1721
+ # (see TestFunctionType.test_in_iter_func_call)
1722
+ if (
1723
+ prev_assign.target.name == inst.value.name
1724
+ and not self._var_used_in_binop(
1725
+ inst.target.name, prev_assign.value
1726
+ )
1727
+ ):
1728
+ replaced_var[inst.value.name] = inst.target
1729
+ prev_assign.target = inst.target
1730
+ # replace temp var definition in target with proper defs
1731
+ self.definitions[inst.target.name].remove(inst.value)
1732
+ self.definitions[inst.target.name].extend(
1733
+ self.definitions.pop(inst.value.name)
1734
+ )
1735
+ continue
1736
+
1737
+ new_body.append(inst)
1738
+
1739
+ self.current_block.body = new_body
1740
+
1741
+ def _var_used_in_binop(self, varname, expr):
1742
+ """return True if 'expr' is a binary expression and 'varname' is used
1743
+ in it as an argument
1744
+ """
1745
+ return (
1746
+ isinstance(expr, ir.Expr)
1747
+ and expr.op in ("binop", "inplace_binop")
1748
+ and (varname == expr.lhs.name or varname == expr.rhs.name)
1749
+ )
1750
+
1751
+ def _insert_outgoing_phis(self):
1752
+ """
1753
+ Add assignments to forward requested outgoing values
1754
+ to subsequent blocks.
1755
+ """
1756
+ for phiname, varname in self.dfainfo.outgoing_phis.items():
1757
+ target = self.current_scope.get_or_define(phiname, loc=self.loc)
1758
+ try:
1759
+ val = self.get(varname)
1760
+ except ir.NotDefinedError:
1761
+ # Hack to make sure exception variables are defined
1762
+ assert PYVERSION in ((3, 11), (3, 12), (3, 13)), (
1763
+ "unexpected missing definition"
1764
+ )
1765
+ val = ir.Const(value=None, loc=self.loc)
1766
+ stmt = ir.Assign(value=val, target=target, loc=self.loc)
1767
+ self.definitions[target.name].append(stmt.value)
1768
+ if not self.current_block.is_terminated:
1769
+ self.current_block.append(stmt)
1770
+ else:
1771
+ self.current_block.insert_before_terminator(stmt)
1772
+
1773
+ def get_global_value(self, name):
1774
+ """
1775
+ Get a global value from the func_global (first) or
1776
+ as a builtins (second). If both failed, return a ir.UNDEFINED.
1777
+ """
1778
+ try:
1779
+ return self.func_id.func.__globals__[name]
1780
+ except KeyError:
1781
+ return getattr(builtins, name, ir.UNDEFINED)
1782
+
1783
+ def get_closure_value(self, index):
1784
+ """
1785
+ Get a value from the cell contained in this function's closure.
1786
+ If not set, return a ir.UNDEFINED.
1787
+ """
1788
+ cell = self.func_id.func.__closure__[index]
1789
+ try:
1790
+ return cell.cell_contents
1791
+ except ValueError:
1792
+ return ir.UNDEFINED
1793
+
1794
+ @property
1795
+ def current_scope(self):
1796
+ return self.scopes[-1]
1797
+
1798
+ @property
1799
+ def code_consts(self):
1800
+ return self.bytecode.co_consts
1801
+
1802
+ @property
1803
+ def code_locals(self):
1804
+ return self.bytecode.co_varnames
1805
+
1806
+ @property
1807
+ def code_names(self):
1808
+ return self.bytecode.co_names
1809
+
1810
+ @property
1811
+ def code_cellvars(self):
1812
+ return self.bytecode.co_cellvars
1813
+
1814
+ @property
1815
+ def code_freevars(self):
1816
+ return self.bytecode.co_freevars
1817
+
1818
+ def _dispatch(self, inst, kws):
1819
+ if self._DEBUG_PRINT:
1820
+ print(inst)
1821
+ assert self.current_block is not None
1822
+ if PYVERSION in ((3, 11), (3, 12), (3, 13)):
1823
+ if self.syntax_blocks:
1824
+ top = self.syntax_blocks[-1]
1825
+ if isinstance(top, ir.With):
1826
+ if inst.offset >= top.exit:
1827
+ self.current_block.append(ir.PopBlock(loc=self.loc))
1828
+ self.syntax_blocks.pop()
1829
+ elif PYVERSION in ((3, 10),):
1830
+ pass
1831
+ else:
1832
+ raise NotImplementedError(PYVERSION)
1833
+
1834
+ fname = "op_%s" % inst.opname.replace("+", "_")
1835
+ try:
1836
+ fn = getattr(self, fname)
1837
+ except AttributeError:
1838
+ raise NotImplementedError(inst)
1839
+ else:
1840
+ try:
1841
+ return fn(inst, **kws)
1842
+ except errors.NotDefinedError as e:
1843
+ if e.loc is None:
1844
+ loc = self.loc
1845
+ else:
1846
+ loc = e.loc
1847
+
1848
+ err = errors.NotDefinedError(e.name, loc=loc)
1849
+ if not config.FULL_TRACEBACKS:
1850
+ raise err from None
1851
+ else:
1852
+ m = f"handling op: {inst} | offset: {inst.offset}"
1853
+ err.add_context(m)
1854
+ err.add_context(self.bytecode.dump())
1855
+ raise err
1856
+
1857
+ # --- Scope operations ---
1858
+
1859
+ def store(self, value, name, redefine=False):
1860
+ """
1861
+ Store *value* (a Expr or Var instance) into the variable named *name*
1862
+ (a str object). Returns the target variable.
1863
+ """
1864
+ if redefine or self.current_block_offset in self.cfa.backbone:
1865
+ rename = name not in self.code_cellvars
1866
+ target = self.current_scope.redefine(
1867
+ name, loc=self.loc, rename=rename
1868
+ )
1869
+ else:
1870
+ target = self.current_scope.get_or_define(name, loc=self.loc)
1871
+ if isinstance(value, ir.Var):
1872
+ value = self.assigner.assign(value, target)
1873
+ stmt = ir.Assign(value=value, target=target, loc=self.loc)
1874
+ self.current_block.append(stmt)
1875
+ self.definitions[target.name].append(value)
1876
+ return target
1877
+
1878
+ def get(self, name):
1879
+ """
1880
+ Get the variable (a Var instance) with the given *name*.
1881
+ """
1882
+ # Implicit argument for comprehension starts with '.'
1883
+ # See Parameter class in inspect.py (from Python source)
1884
+ if name[0] == "." and name[1:].isdigit():
1885
+ name = "implicit{}".format(name[1:])
1886
+
1887
+ # Try to simplify the variable lookup by returning an earlier
1888
+ # variable assigned to *name*.
1889
+ var = self.assigner.get_assignment_source(name)
1890
+ if var is None:
1891
+ var = self.current_scope.get(name)
1892
+ return var
1893
+
1894
+ # --- Block operations ---
1895
+
1896
+ def insert_block(self, offset, scope=None, loc=None):
1897
+ scope = scope or self.current_scope
1898
+ loc = loc or self.loc
1899
+ blk = ir.Block(scope=scope, loc=loc)
1900
+ self.blocks[offset] = blk
1901
+ self.current_block = blk
1902
+ self.current_block_offset = offset
1903
+ return blk
1904
+
1905
+ # --- Bytecode handlers ---
1906
+
1907
+ def op_NOP(self, inst):
1908
+ pass
1909
+
1910
+ def op_RESUME(self, inst):
1911
+ pass
1912
+
1913
+ def op_CACHE(self, inst):
1914
+ pass
1915
+
1916
+ def op_PRECALL(self, inst):
1917
+ pass
1918
+
1919
+ def op_PUSH_NULL(self, inst):
1920
+ pass
1921
+
1922
+ def op_RETURN_GENERATOR(self, inst):
1923
+ pass
1924
+
1925
+ def op_PRINT_ITEM(self, inst, item, printvar, res):
1926
+ item = self.get(item)
1927
+ printgv = ir.Global("print", print, loc=self.loc)
1928
+ self.store(value=printgv, name=printvar)
1929
+ call = ir.Expr.call(self.get(printvar), (item,), (), loc=self.loc)
1930
+ self.store(value=call, name=res)
1931
+
1932
+ def op_PRINT_NEWLINE(self, inst, printvar, res):
1933
+ printgv = ir.Global("print", print, loc=self.loc)
1934
+ self.store(value=printgv, name=printvar)
1935
+ call = ir.Expr.call(self.get(printvar), (), (), loc=self.loc)
1936
+ self.store(value=call, name=res)
1937
+
1938
+ def op_UNPACK_SEQUENCE(self, inst, iterable, stores, tupleobj):
1939
+ count = len(stores)
1940
+ # Exhaust the iterable into a tuple-like object
1941
+ tup = ir.Expr.exhaust_iter(
1942
+ value=self.get(iterable), loc=self.loc, count=count
1943
+ )
1944
+ self.store(name=tupleobj, value=tup)
1945
+
1946
+ # then index the tuple-like object to extract the values
1947
+ for i, st in enumerate(stores):
1948
+ expr = ir.Expr.static_getitem(
1949
+ self.get(tupleobj), index=i, index_var=None, loc=self.loc
1950
+ )
1951
+ self.store(expr, st)
1952
+
1953
+ def op_FORMAT_SIMPLE(self, inst, value, res, strvar):
1954
+ # Same as FORMAT_VALUE
1955
+ return self.op_FORMAT_VALUE(inst, value, res, strvar)
1956
+
1957
+ def op_FORMAT_VALUE(self, inst, value, res, strvar):
1958
+ """
1959
+ FORMAT_VALUE(flags): flags argument specifies format spec which is not
1960
+ supported yet. Currently, str() is simply called on the value.
1961
+ https://docs.python.org/3/library/dis.html#opcode-FORMAT_VALUE
1962
+ """
1963
+ value = self.get(value)
1964
+ strgv = ir.Global("str", str, loc=self.loc)
1965
+ self.store(value=strgv, name=strvar)
1966
+ call = ir.Expr.call(self.get(strvar), (value,), (), loc=self.loc)
1967
+ self.store(value=call, name=res)
1968
+
1969
+ def op_BUILD_STRING(self, inst, strings, tmps):
1970
+ """
1971
+ BUILD_STRING(count): Concatenates count strings.
1972
+ Required for supporting f-strings.
1973
+ https://docs.python.org/3/library/dis.html#opcode-BUILD_STRING
1974
+ """
1975
+ count = inst.arg
1976
+ # corner case: f""
1977
+ if count == 0:
1978
+ const = ir.Const("", loc=self.loc)
1979
+ self.store(const, tmps[-1])
1980
+ return
1981
+
1982
+ prev = self.get(strings[0])
1983
+ for other, tmp in zip(strings[1:], tmps):
1984
+ other = self.get(other)
1985
+ expr = ir.Expr.binop(
1986
+ operator.add, lhs=prev, rhs=other, loc=self.loc
1987
+ )
1988
+ self.store(expr, tmp)
1989
+ prev = self.get(tmp)
1990
+
1991
+ def op_BUILD_SLICE(self, inst, start, stop, step, res, slicevar):
1992
+ start = self.get(start)
1993
+ stop = self.get(stop)
1994
+
1995
+ slicegv = ir.Global("slice", slice, loc=self.loc)
1996
+ self.store(value=slicegv, name=slicevar)
1997
+
1998
+ if step is None:
1999
+ sliceinst = ir.Expr.call(
2000
+ self.get(slicevar), (start, stop), (), loc=self.loc
2001
+ )
2002
+ else:
2003
+ step = self.get(step)
2004
+ sliceinst = ir.Expr.call(
2005
+ self.get(slicevar), (start, stop, step), (), loc=self.loc
2006
+ )
2007
+ self.store(value=sliceinst, name=res)
2008
+
2009
+ if PYVERSION in ((3, 12), (3, 13)):
2010
+
2011
+ def op_BINARY_SLICE(
2012
+ self, inst, start, end, container, res, slicevar, temp_res
2013
+ ):
2014
+ start = self.get(start)
2015
+ end = self.get(end)
2016
+ slicegv = ir.Global("slice", slice, loc=self.loc)
2017
+ self.store(value=slicegv, name=slicevar)
2018
+ sliceinst = ir.Expr.call(
2019
+ self.get(slicevar), (start, end), (), loc=self.loc
2020
+ )
2021
+ self.store(value=sliceinst, name=temp_res)
2022
+ index = self.get(temp_res)
2023
+ target = self.get(container)
2024
+ expr = ir.Expr.getitem(target, index=index, loc=self.loc)
2025
+ self.store(expr, res)
2026
+ elif PYVERSION in ((3, 10), (3, 11)):
2027
+ pass
2028
+ else:
2029
+ raise NotImplementedError(PYVERSION)
2030
+
2031
+ if PYVERSION in ((3, 12), (3, 13)):
2032
+
2033
+ def op_STORE_SLICE(
2034
+ self, inst, start, end, container, value, res, slicevar
2035
+ ):
2036
+ start = self.get(start)
2037
+ end = self.get(end)
2038
+ slicegv = ir.Global("slice", slice, loc=self.loc)
2039
+ self.store(value=slicegv, name=slicevar)
2040
+ sliceinst = ir.Expr.call(
2041
+ self.get(slicevar), (start, end), (), loc=self.loc
2042
+ )
2043
+ self.store(value=sliceinst, name=res)
2044
+ index = self.get(res)
2045
+ target = self.get(container)
2046
+ value = self.get(value)
2047
+
2048
+ stmt = ir.SetItem(
2049
+ target=target, index=index, value=value, loc=self.loc
2050
+ )
2051
+ self.current_block.append(stmt)
2052
+ elif PYVERSION in ((3, 10), (3, 11)):
2053
+ pass
2054
+ else:
2055
+ raise NotImplementedError(PYVERSION)
2056
+
2057
+ def op_SLICE_0(self, inst, base, res, slicevar, indexvar, nonevar):
2058
+ base = self.get(base)
2059
+
2060
+ slicegv = ir.Global("slice", slice, loc=self.loc)
2061
+ self.store(value=slicegv, name=slicevar)
2062
+
2063
+ nonegv = ir.Const(None, loc=self.loc)
2064
+ self.store(value=nonegv, name=nonevar)
2065
+ none = self.get(nonevar)
2066
+
2067
+ index = ir.Expr.call(self.get(slicevar), (none, none), (), loc=self.loc)
2068
+ self.store(value=index, name=indexvar)
2069
+
2070
+ expr = ir.Expr.getitem(base, self.get(indexvar), loc=self.loc)
2071
+ self.store(value=expr, name=res)
2072
+
2073
+ def op_SLICE_1(self, inst, base, start, nonevar, res, slicevar, indexvar):
2074
+ base = self.get(base)
2075
+ start = self.get(start)
2076
+
2077
+ nonegv = ir.Const(None, loc=self.loc)
2078
+ self.store(value=nonegv, name=nonevar)
2079
+ none = self.get(nonevar)
2080
+
2081
+ slicegv = ir.Global("slice", slice, loc=self.loc)
2082
+ self.store(value=slicegv, name=slicevar)
2083
+
2084
+ index = ir.Expr.call(
2085
+ self.get(slicevar), (start, none), (), loc=self.loc
2086
+ )
2087
+ self.store(value=index, name=indexvar)
2088
+
2089
+ expr = ir.Expr.getitem(base, self.get(indexvar), loc=self.loc)
2090
+ self.store(value=expr, name=res)
2091
+
2092
+ def op_SLICE_2(self, inst, base, nonevar, stop, res, slicevar, indexvar):
2093
+ base = self.get(base)
2094
+ stop = self.get(stop)
2095
+
2096
+ nonegv = ir.Const(None, loc=self.loc)
2097
+ self.store(value=nonegv, name=nonevar)
2098
+ none = self.get(nonevar)
2099
+
2100
+ slicegv = ir.Global("slice", slice, loc=self.loc)
2101
+ self.store(value=slicegv, name=slicevar)
2102
+
2103
+ index = ir.Expr.call(
2104
+ self.get(slicevar),
2105
+ (
2106
+ none,
2107
+ stop,
2108
+ ),
2109
+ (),
2110
+ loc=self.loc,
2111
+ )
2112
+ self.store(value=index, name=indexvar)
2113
+
2114
+ expr = ir.Expr.getitem(base, self.get(indexvar), loc=self.loc)
2115
+ self.store(value=expr, name=res)
2116
+
2117
+ def op_SLICE_3(self, inst, base, start, stop, res, slicevar, indexvar):
2118
+ base = self.get(base)
2119
+ start = self.get(start)
2120
+ stop = self.get(stop)
2121
+
2122
+ slicegv = ir.Global("slice", slice, loc=self.loc)
2123
+ self.store(value=slicegv, name=slicevar)
2124
+
2125
+ index = ir.Expr.call(
2126
+ self.get(slicevar), (start, stop), (), loc=self.loc
2127
+ )
2128
+ self.store(value=index, name=indexvar)
2129
+
2130
+ expr = ir.Expr.getitem(base, self.get(indexvar), loc=self.loc)
2131
+ self.store(value=expr, name=res)
2132
+
2133
+ def op_STORE_SLICE_0(self, inst, base, value, slicevar, indexvar, nonevar):
2134
+ base = self.get(base)
2135
+
2136
+ slicegv = ir.Global("slice", slice, loc=self.loc)
2137
+ self.store(value=slicegv, name=slicevar)
2138
+
2139
+ nonegv = ir.Const(None, loc=self.loc)
2140
+ self.store(value=nonegv, name=nonevar)
2141
+ none = self.get(nonevar)
2142
+
2143
+ index = ir.Expr.call(self.get(slicevar), (none, none), (), loc=self.loc)
2144
+ self.store(value=index, name=indexvar)
2145
+
2146
+ stmt = ir.SetItem(
2147
+ base, self.get(indexvar), self.get(value), loc=self.loc
2148
+ )
2149
+ self.current_block.append(stmt)
2150
+
2151
+ def op_STORE_SLICE_1(
2152
+ self, inst, base, start, nonevar, value, slicevar, indexvar
2153
+ ):
2154
+ base = self.get(base)
2155
+ start = self.get(start)
2156
+
2157
+ nonegv = ir.Const(None, loc=self.loc)
2158
+ self.store(value=nonegv, name=nonevar)
2159
+ none = self.get(nonevar)
2160
+
2161
+ slicegv = ir.Global("slice", slice, loc=self.loc)
2162
+ self.store(value=slicegv, name=slicevar)
2163
+
2164
+ index = ir.Expr.call(
2165
+ self.get(slicevar), (start, none), (), loc=self.loc
2166
+ )
2167
+ self.store(value=index, name=indexvar)
2168
+
2169
+ stmt = ir.SetItem(
2170
+ base, self.get(indexvar), self.get(value), loc=self.loc
2171
+ )
2172
+ self.current_block.append(stmt)
2173
+
2174
+ def op_STORE_SLICE_2(
2175
+ self, inst, base, nonevar, stop, value, slicevar, indexvar
2176
+ ):
2177
+ base = self.get(base)
2178
+ stop = self.get(stop)
2179
+
2180
+ nonegv = ir.Const(None, loc=self.loc)
2181
+ self.store(value=nonegv, name=nonevar)
2182
+ none = self.get(nonevar)
2183
+
2184
+ slicegv = ir.Global("slice", slice, loc=self.loc)
2185
+ self.store(value=slicegv, name=slicevar)
2186
+
2187
+ index = ir.Expr.call(
2188
+ self.get(slicevar),
2189
+ (
2190
+ none,
2191
+ stop,
2192
+ ),
2193
+ (),
2194
+ loc=self.loc,
2195
+ )
2196
+ self.store(value=index, name=indexvar)
2197
+
2198
+ stmt = ir.SetItem(
2199
+ base, self.get(indexvar), self.get(value), loc=self.loc
2200
+ )
2201
+ self.current_block.append(stmt)
2202
+
2203
+ def op_STORE_SLICE_3(
2204
+ self, inst, base, start, stop, value, slicevar, indexvar
2205
+ ):
2206
+ base = self.get(base)
2207
+ start = self.get(start)
2208
+ stop = self.get(stop)
2209
+
2210
+ slicegv = ir.Global("slice", slice, loc=self.loc)
2211
+ self.store(value=slicegv, name=slicevar)
2212
+
2213
+ index = ir.Expr.call(
2214
+ self.get(slicevar), (start, stop), (), loc=self.loc
2215
+ )
2216
+ self.store(value=index, name=indexvar)
2217
+ stmt = ir.SetItem(
2218
+ base, self.get(indexvar), self.get(value), loc=self.loc
2219
+ )
2220
+ self.current_block.append(stmt)
2221
+
2222
+ def op_DELETE_SLICE_0(self, inst, base, slicevar, indexvar, nonevar):
2223
+ base = self.get(base)
2224
+
2225
+ slicegv = ir.Global("slice", slice, loc=self.loc)
2226
+ self.store(value=slicegv, name=slicevar)
2227
+
2228
+ nonegv = ir.Const(None, loc=self.loc)
2229
+ self.store(value=nonegv, name=nonevar)
2230
+ none = self.get(nonevar)
2231
+
2232
+ index = ir.Expr.call(self.get(slicevar), (none, none), (), loc=self.loc)
2233
+ self.store(value=index, name=indexvar)
2234
+
2235
+ stmt = ir.DelItem(base, self.get(indexvar), loc=self.loc)
2236
+ self.current_block.append(stmt)
2237
+
2238
+ def op_DELETE_SLICE_1(self, inst, base, start, nonevar, slicevar, indexvar):
2239
+ base = self.get(base)
2240
+ start = self.get(start)
2241
+
2242
+ nonegv = ir.Const(None, loc=self.loc)
2243
+ self.store(value=nonegv, name=nonevar)
2244
+ none = self.get(nonevar)
2245
+
2246
+ slicegv = ir.Global("slice", slice, loc=self.loc)
2247
+ self.store(value=slicegv, name=slicevar)
2248
+
2249
+ index = ir.Expr.call(
2250
+ self.get(slicevar), (start, none), (), loc=self.loc
2251
+ )
2252
+ self.store(value=index, name=indexvar)
2253
+
2254
+ stmt = ir.DelItem(base, self.get(indexvar), loc=self.loc)
2255
+ self.current_block.append(stmt)
2256
+
2257
+ def op_DELETE_SLICE_2(self, inst, base, nonevar, stop, slicevar, indexvar):
2258
+ base = self.get(base)
2259
+ stop = self.get(stop)
2260
+
2261
+ nonegv = ir.Const(None, loc=self.loc)
2262
+ self.store(value=nonegv, name=nonevar)
2263
+ none = self.get(nonevar)
2264
+
2265
+ slicegv = ir.Global("slice", slice, loc=self.loc)
2266
+ self.store(value=slicegv, name=slicevar)
2267
+
2268
+ index = ir.Expr.call(
2269
+ self.get(slicevar),
2270
+ (
2271
+ none,
2272
+ stop,
2273
+ ),
2274
+ (),
2275
+ loc=self.loc,
2276
+ )
2277
+ self.store(value=index, name=indexvar)
2278
+
2279
+ stmt = ir.DelItem(base, self.get(indexvar), loc=self.loc)
2280
+ self.current_block.append(stmt)
2281
+
2282
+ def op_DELETE_SLICE_3(self, inst, base, start, stop, slicevar, indexvar):
2283
+ base = self.get(base)
2284
+ start = self.get(start)
2285
+ stop = self.get(stop)
2286
+
2287
+ slicegv = ir.Global("slice", slice, loc=self.loc)
2288
+ self.store(value=slicegv, name=slicevar)
2289
+
2290
+ index = ir.Expr.call(
2291
+ self.get(slicevar), (start, stop), (), loc=self.loc
2292
+ )
2293
+ self.store(value=index, name=indexvar)
2294
+ stmt = ir.DelItem(base, self.get(indexvar), loc=self.loc)
2295
+ self.current_block.append(stmt)
2296
+
2297
+ def _op_LOAD_FAST(self, inst, res):
2298
+ srcname = self.code_locals[inst.arg]
2299
+ self.store(value=self.get(srcname), name=res)
2300
+
2301
+ if PYVERSION in ((3, 13),):
2302
+
2303
+ def op_LOAD_FAST(self, inst, res, as_load_deref=False):
2304
+ if as_load_deref:
2305
+ self.op_LOAD_DEREF(inst, res)
2306
+ else:
2307
+ self._op_LOAD_FAST(inst, res)
2308
+
2309
+ else:
2310
+ op_LOAD_FAST = _op_LOAD_FAST
2311
+
2312
+ if PYVERSION in ((3, 13),):
2313
+
2314
+ def op_LOAD_FAST_LOAD_FAST(self, inst, res1, res2):
2315
+ oparg = inst.arg
2316
+ oparg1 = oparg >> 4
2317
+ oparg2 = oparg & 15
2318
+ src1 = self.get(self.code_locals[oparg1])
2319
+ src2 = self.get(self.code_locals[oparg2])
2320
+ self.store(value=src1, name=res1)
2321
+ self.store(value=src2, name=res2)
2322
+
2323
+ def op_STORE_FAST_LOAD_FAST(self, inst, store_value, load_res):
2324
+ oparg = inst.arg
2325
+ oparg1 = oparg >> 4
2326
+ oparg2 = oparg & 15
2327
+
2328
+ dstname = self.code_locals[oparg1]
2329
+ dst_value = self.get(store_value)
2330
+ self.store(value=dst_value, name=dstname)
2331
+
2332
+ src_value = self.get(self.code_locals[oparg2])
2333
+ self.store(value=src_value, name=load_res)
2334
+
2335
+ def op_STORE_FAST_STORE_FAST(self, inst, value1, value2):
2336
+ oparg = inst.arg
2337
+ oparg1 = oparg >> 4
2338
+ oparg2 = oparg & 15
2339
+
2340
+ dstname = self.code_locals[oparg1]
2341
+ self.store(value=self.get(value1), name=dstname)
2342
+ dstname = self.code_locals[oparg2]
2343
+ self.store(value=self.get(value2), name=dstname)
2344
+
2345
+ elif PYVERSION in ((3, 10), (3, 11), (3, 12)):
2346
+ pass
2347
+ else:
2348
+ raise NotImplementedError(PYVERSION)
2349
+
2350
+ if PYVERSION in ((3, 12), (3, 13)):
2351
+ op_LOAD_FAST_CHECK = op_LOAD_FAST
2352
+
2353
+ def op_LOAD_FAST_AND_CLEAR(self, inst, res):
2354
+ try:
2355
+ # try the regular LOAD_FAST logic
2356
+ srcname = self.code_locals[inst.arg]
2357
+ self.store(value=self.get(srcname), name=res)
2358
+ except NotDefinedError:
2359
+ # If the variable is not in the scope, set it to `undef`
2360
+ undef = ir.Expr.undef(loc=self.loc)
2361
+ self.store(undef, name=res)
2362
+
2363
+ elif PYVERSION in ((3, 10), (3, 11)):
2364
+ pass
2365
+ else:
2366
+ raise NotImplementedError(PYVERSION)
2367
+
2368
+ def op_STORE_FAST(self, inst, value):
2369
+ dstname = self.code_locals[inst.arg]
2370
+ value = self.get(value)
2371
+ self.store(value=value, name=dstname)
2372
+
2373
+ def op_DELETE_FAST(self, inst):
2374
+ dstname = self.code_locals[inst.arg]
2375
+ self.current_block.append(ir.Del(dstname, loc=self.loc))
2376
+
2377
+ def op_DUP_TOPX(self, inst, orig, duped):
2378
+ for src, dst in zip(orig, duped):
2379
+ self.store(value=self.get(src), name=dst)
2380
+
2381
+ op_DUP_TOP = op_DUP_TOPX
2382
+ op_DUP_TOP_TWO = op_DUP_TOPX
2383
+
2384
+ def op_STORE_ATTR(self, inst, target, value):
2385
+ attr = self.code_names[inst.arg]
2386
+ sa = ir.SetAttr(
2387
+ target=self.get(target),
2388
+ value=self.get(value),
2389
+ attr=attr,
2390
+ loc=self.loc,
2391
+ )
2392
+ self.current_block.append(sa)
2393
+
2394
+ def op_DELETE_ATTR(self, inst, target):
2395
+ attr = self.code_names[inst.arg]
2396
+ sa = ir.DelAttr(target=self.get(target), attr=attr, loc=self.loc)
2397
+ self.current_block.append(sa)
2398
+
2399
+ def op_LOAD_ATTR(self, inst, item, res):
2400
+ item = self.get(item)
2401
+ if PYVERSION in ((3, 12), (3, 13)):
2402
+ attr = self.code_names[inst.arg >> 1]
2403
+ elif PYVERSION in ((3, 10), (3, 11)):
2404
+ attr = self.code_names[inst.arg]
2405
+ else:
2406
+ raise NotImplementedError(PYVERSION)
2407
+ getattr = ir.Expr.getattr(item, attr, loc=self.loc)
2408
+ self.store(getattr, res)
2409
+
2410
+ def op_LOAD_CONST(self, inst, res):
2411
+ value = self.code_consts[inst.arg]
2412
+ if isinstance(value, tuple):
2413
+ st = []
2414
+ for x in value:
2415
+ nm = "$const_%s" % str(x)
2416
+ val_const = ir.Const(x, loc=self.loc)
2417
+ target = self.store(val_const, name=nm, redefine=True)
2418
+ st.append(target)
2419
+ const = ir.Expr.build_tuple(st, loc=self.loc)
2420
+ elif isinstance(value, frozenset):
2421
+ st = []
2422
+ for x in value:
2423
+ nm = "$const_%s" % str(x)
2424
+ val_const = ir.Const(x, loc=self.loc)
2425
+ target = self.store(val_const, name=nm, redefine=True)
2426
+ st.append(target)
2427
+ const = ir.Expr.build_set(st, loc=self.loc)
2428
+ else:
2429
+ const = ir.Const(value, loc=self.loc)
2430
+ self.store(const, res)
2431
+
2432
+ if PYVERSION in ((3, 11), (3, 12), (3, 13)):
2433
+
2434
+ def op_LOAD_GLOBAL(self, inst, idx, res):
2435
+ name = self.code_names[idx]
2436
+ value = self.get_global_value(name)
2437
+ gl = ir.Global(name, value, loc=self.loc)
2438
+ self.store(gl, res)
2439
+ elif PYVERSION in ((3, 10),):
2440
+
2441
+ def op_LOAD_GLOBAL(self, inst, res):
2442
+ name = self.code_names[inst.arg]
2443
+ value = self.get_global_value(name)
2444
+ gl = ir.Global(name, value, loc=self.loc)
2445
+ self.store(gl, res)
2446
+ else:
2447
+ raise NotImplementedError(PYVERSION)
2448
+
2449
+ def op_COPY_FREE_VARS(self, inst):
2450
+ pass
2451
+
2452
+ if PYVERSION in ((3, 11), (3, 12), (3, 13)):
2453
+
2454
+ def op_LOAD_DEREF(self, inst, res):
2455
+ name = self.func_id.func.__code__._varname_from_oparg(inst.arg)
2456
+ if name in self.code_cellvars:
2457
+ try:
2458
+ gl = self.get(name)
2459
+ except NotDefinedError:
2460
+ msg = "Unsupported use of cell variable encountered"
2461
+ raise NotImplementedError(msg)
2462
+ elif name in self.code_freevars:
2463
+ idx = self.code_freevars.index(name)
2464
+ value = self.get_closure_value(idx)
2465
+ gl = ir.FreeVar(idx, name, value, loc=self.loc)
2466
+ self.store(gl, res)
2467
+ elif PYVERSION in ((3, 10),):
2468
+
2469
+ def op_LOAD_DEREF(self, inst, res):
2470
+ n_cellvars = len(self.code_cellvars)
2471
+ if inst.arg < n_cellvars:
2472
+ name = self.code_cellvars[inst.arg]
2473
+ gl = self.get(name)
2474
+ else:
2475
+ idx = inst.arg - n_cellvars
2476
+ name = self.code_freevars[idx]
2477
+ value = self.get_closure_value(idx)
2478
+ gl = ir.FreeVar(idx, name, value, loc=self.loc)
2479
+ self.store(gl, res)
2480
+ else:
2481
+ raise NotImplementedError(PYVERSION)
2482
+
2483
+ if PYVERSION in ((3, 11), (3, 12), (3, 13)):
2484
+
2485
+ def op_MAKE_CELL(self, inst):
2486
+ pass # ignored bytecode
2487
+
2488
+ if PYVERSION in ((3, 11), (3, 12), (3, 13)):
2489
+
2490
+ def op_STORE_DEREF(self, inst, value):
2491
+ name = self.func_id.func.__code__._varname_from_oparg(inst.arg)
2492
+ value = self.get(value)
2493
+ self.store(value=value, name=name)
2494
+ elif PYVERSION in ((3, 10),):
2495
+
2496
+ def op_STORE_DEREF(self, inst, value):
2497
+ n_cellvars = len(self.code_cellvars)
2498
+ if inst.arg < n_cellvars:
2499
+ dstname = self.code_cellvars[inst.arg]
2500
+ else:
2501
+ dstname = self.code_freevars[inst.arg - n_cellvars]
2502
+ value = self.get(value)
2503
+ self.store(value=value, name=dstname)
2504
+ else:
2505
+ raise NotImplementedError(PYVERSION)
2506
+
2507
+ def op_SETUP_LOOP(self, inst):
2508
+ assert self.blocks[inst.offset] is self.current_block
2509
+ loop = ir.Loop(inst.offset, exit=(inst.next + inst.arg))
2510
+ self.syntax_blocks.append(loop)
2511
+
2512
+ def op_SETUP_WITH(self, inst, contextmanager, exitfn=None):
2513
+ assert self.blocks[inst.offset] is self.current_block
2514
+ # Handle with
2515
+ exitpt = inst.next + inst.arg
2516
+
2517
+ wth = ir.With(inst.offset, exit=exitpt)
2518
+ self.syntax_blocks.append(wth)
2519
+ ctxmgr = self.get(contextmanager)
2520
+ self.current_block.append(
2521
+ ir.EnterWith(
2522
+ contextmanager=ctxmgr,
2523
+ begin=inst.offset,
2524
+ end=exitpt,
2525
+ loc=self.loc,
2526
+ )
2527
+ )
2528
+
2529
+ # Store exit fn
2530
+ exit_fn_obj = ir.Const(None, loc=self.loc)
2531
+ self.store(value=exit_fn_obj, name=exitfn)
2532
+
2533
+ def op_BEFORE_WITH(self, inst, contextmanager, exitfn, end):
2534
+ assert self.blocks[inst.offset] is self.current_block
2535
+ if PYVERSION in ((3, 12), (3, 13)):
2536
+ # Python 3.12 hack for handling nested with blocks
2537
+ if end > self.last_active_offset:
2538
+ # Use exception entries to figure out end of syntax block
2539
+ end = max(
2540
+ [
2541
+ ex.end
2542
+ for ex in self.active_exception_entries
2543
+ if ex.target == end
2544
+ ]
2545
+ )
2546
+ elif PYVERSION in ((3, 10), (3, 11)):
2547
+ pass
2548
+ else:
2549
+ raise NotImplementedError(PYVERSION)
2550
+ # Handle with
2551
+ wth = ir.With(inst.offset, exit=end)
2552
+ self.syntax_blocks.append(wth)
2553
+ ctxmgr = self.get(contextmanager)
2554
+ self.current_block.append(
2555
+ ir.EnterWith(
2556
+ contextmanager=ctxmgr,
2557
+ begin=inst.offset,
2558
+ end=end,
2559
+ loc=self.loc,
2560
+ )
2561
+ )
2562
+
2563
+ # Store exit function
2564
+ exit_fn_obj = ir.Const(None, loc=self.loc)
2565
+ self.store(value=exit_fn_obj, name=exitfn)
2566
+
2567
+ def op_SETUP_FINALLY(self, inst):
2568
+ # Removed since python3.11
2569
+ self._insert_try_block_begin()
2570
+
2571
+ def op_WITH_CLEANUP(self, inst):
2572
+ "no-op"
2573
+
2574
+ def op_WITH_CLEANUP_START(self, inst):
2575
+ "no-op"
2576
+
2577
+ def op_WITH_CLEANUP_FINISH(self, inst):
2578
+ "no-op"
2579
+
2580
+ def op_END_FINALLY(self, inst):
2581
+ "no-op"
2582
+
2583
+ def op_BEGIN_FINALLY(self, inst, temps):
2584
+ # The *temps* are the exception variables
2585
+ const_none = ir.Const(None, loc=self.loc)
2586
+ for tmp in temps:
2587
+ # Set to None for now
2588
+ self.store(const_none, name=tmp)
2589
+ self._exception_vars.add(tmp)
2590
+
2591
+ def op_CALL(self, inst, func, args, kw_names, res):
2592
+ func = self.get(func)
2593
+ args = [self.get(x) for x in args]
2594
+ if kw_names is not None:
2595
+ assert PYVERSION < (3, 13)
2596
+ names = self.code_consts[kw_names]
2597
+ kwargs = list(zip(names, args[-len(names) :]))
2598
+ args = args[: -len(names)]
2599
+ else:
2600
+ kwargs = ()
2601
+ expr = ir.Expr.call(func, args, kwargs, loc=self.loc)
2602
+ self.store(expr, res)
2603
+
2604
+ if PYVERSION in ((3, 13),):
2605
+
2606
+ def op_CALL_KW(self, inst, func, args, kw_names, res):
2607
+ func = self.get(func)
2608
+ args = [self.get(x) for x in args]
2609
+ consti = int(kw_names.rsplit(".", 2)[-1])
2610
+ names = self.code_consts[consti]
2611
+ kwargs = list(zip(names, args[-len(names) :]))
2612
+ args = args[: -len(names)]
2613
+ expr = ir.Expr.call(func, args, kwargs, loc=self.loc)
2614
+ self.store(expr, res)
2615
+ else:
2616
+ assert PYVERSION < (3, 13)
2617
+
2618
+ def op_CALL_FUNCTION(self, inst, func, args, res):
2619
+ func = self.get(func)
2620
+ args = [self.get(x) for x in args]
2621
+ expr = ir.Expr.call(func, args, (), loc=self.loc)
2622
+ self.store(expr, res)
2623
+
2624
+ def op_CALL_FUNCTION_KW(self, inst, func, args, names, res):
2625
+ func = self.get(func)
2626
+ args = [self.get(x) for x in args]
2627
+ # Find names const
2628
+ names = self.get(names)
2629
+ for inst in self.current_block.body:
2630
+ if isinstance(inst, ir.Assign) and inst.target is names:
2631
+ self.current_block.remove(inst)
2632
+ # scan up the block looking for the values, remove them
2633
+ # and find their name strings
2634
+ named_items = []
2635
+ for x in inst.value.items:
2636
+ for y in self.current_block.body[::-1]:
2637
+ if x == y.target:
2638
+ self.current_block.remove(y)
2639
+ named_items.append(y.value.value)
2640
+ break
2641
+ keys = named_items
2642
+ break
2643
+
2644
+ nkeys = len(keys)
2645
+ posvals = args[:-nkeys]
2646
+ kwvals = args[-nkeys:]
2647
+ keyvalues = list(zip(keys, kwvals))
2648
+
2649
+ expr = ir.Expr.call(func, posvals, keyvalues, loc=self.loc)
2650
+ self.store(expr, res)
2651
+
2652
+ def op_CALL_FUNCTION_EX(self, inst, func, vararg, varkwarg, res):
2653
+ func = self.get(func)
2654
+ vararg = self.get(vararg)
2655
+ if varkwarg is not None:
2656
+ varkwarg = self.get(varkwarg)
2657
+ expr = ir.Expr.call(
2658
+ func, [], [], loc=self.loc, vararg=vararg, varkwarg=varkwarg
2659
+ )
2660
+ self.store(expr, res)
2661
+
2662
+ def _build_tuple_unpack(self, inst, tuples, temps, is_assign):
2663
+ first = self.get(tuples[0])
2664
+ if is_assign:
2665
+ # it's assign-like, defer handling to an intrinsic that will have
2666
+ # type information.
2667
+ # Can deal with tuples only, i.e. y = (*x,). where x = <tuple>
2668
+ gv_name = "unpack_single_tuple"
2669
+ gv_fn = ir.Global(
2670
+ gv_name,
2671
+ unpack_single_tuple,
2672
+ loc=self.loc,
2673
+ )
2674
+ self.store(value=gv_fn, name=gv_name, redefine=True)
2675
+ exc = ir.Expr.call(
2676
+ self.get(gv_name),
2677
+ args=(first,),
2678
+ kws=(),
2679
+ loc=self.loc,
2680
+ )
2681
+ self.store(exc, temps[0])
2682
+ else:
2683
+ loc = self.loc
2684
+ for other, tmp in zip(map(self.get, tuples[1:]), temps):
2685
+ # Emit as `first + tuple(other)`
2686
+ gv_tuple = ir.Global(
2687
+ name="tuple",
2688
+ value=tuple,
2689
+ loc=loc,
2690
+ )
2691
+ tuple_var = self.store(
2692
+ gv_tuple,
2693
+ "$_list_extend_gv_tuple",
2694
+ redefine=True,
2695
+ )
2696
+ tuplify_val = ir.Expr.call(
2697
+ tuple_var,
2698
+ (other,),
2699
+ (),
2700
+ loc=loc,
2701
+ )
2702
+ tuplify_var = self.store(
2703
+ tuplify_val, "$_tuplify", redefine=True
2704
+ )
2705
+ out = ir.Expr.binop(
2706
+ fn=operator.add,
2707
+ lhs=first,
2708
+ rhs=self.get(tuplify_var.name),
2709
+ loc=self.loc,
2710
+ )
2711
+ self.store(out, tmp)
2712
+ first = self.get(tmp)
2713
+
2714
+ def op_BUILD_TUPLE_UNPACK_WITH_CALL(self, inst, tuples, temps, is_assign):
2715
+ # just unpack the input tuple, call inst will be handled afterwards
2716
+ self._build_tuple_unpack(inst, tuples, temps, is_assign)
2717
+
2718
+ def op_BUILD_TUPLE_UNPACK(self, inst, tuples, temps, is_assign):
2719
+ self._build_tuple_unpack(inst, tuples, temps, is_assign)
2720
+
2721
+ def op_LIST_TO_TUPLE(self, inst, const_list, res):
2722
+ expr = ir.Expr.dummy("list_to_tuple", (const_list,), loc=self.loc)
2723
+ self.store(expr, res)
2724
+
2725
+ def op_BUILD_CONST_KEY_MAP(self, inst, keys, keytmps, values, res):
2726
+ # Unpack the constant key-tuple and reused build_map which takes
2727
+ # a sequence of (key, value) pair.
2728
+ keyvar = self.get(keys)
2729
+ # TODO: refactor this pattern. occurred several times.
2730
+ for inst in self.current_block.body:
2731
+ if isinstance(inst, ir.Assign) and inst.target is keyvar:
2732
+ self.current_block.remove(inst)
2733
+ # scan up the block looking for the values, remove them
2734
+ # and find their name strings
2735
+ named_items = []
2736
+ for x in inst.value.items:
2737
+ for y in self.current_block.body[::-1]:
2738
+ if x == y.target:
2739
+ self.current_block.remove(y)
2740
+ named_items.append(y.value.value)
2741
+ break
2742
+ keytup = named_items
2743
+ break
2744
+ assert len(keytup) == len(values)
2745
+ keyconsts = [ir.Const(value=x, loc=self.loc) for x in keytup]
2746
+ for kval, tmp in zip(keyconsts, keytmps):
2747
+ self.store(kval, tmp)
2748
+ items = list(zip(map(self.get, keytmps), map(self.get, values)))
2749
+
2750
+ # sort out literal values
2751
+ literal_items = []
2752
+ for v in values:
2753
+ defns = self.definitions[v]
2754
+ if len(defns) != 1:
2755
+ break
2756
+ defn = defns[0]
2757
+ if not isinstance(defn, ir.Const):
2758
+ break
2759
+ literal_items.append(defn.value)
2760
+
2761
+ def resolve_const(v):
2762
+ defns = self.definitions[v]
2763
+ if len(defns) != 1:
2764
+ return _UNKNOWN_VALUE(self.get(v).name)
2765
+ defn = defns[0]
2766
+ if not isinstance(defn, ir.Const):
2767
+ return _UNKNOWN_VALUE(self.get(v).name)
2768
+ return defn.value
2769
+
2770
+ if len(literal_items) != len(values):
2771
+ literal_dict = {x: resolve_const(y) for x, y in zip(keytup, values)}
2772
+ else:
2773
+ literal_dict = {x: y for x, y in zip(keytup, literal_items)}
2774
+
2775
+ # to deal with things like {'a': 1, 'a': 'cat', 'b': 2, 'a': 2j}
2776
+ # store the index of the actual used value for a given key, this is
2777
+ # used when lowering to pull the right value out into the tuple repr
2778
+ # of a mixed value type dictionary.
2779
+ value_indexes = {}
2780
+ for i, k in enumerate(keytup):
2781
+ value_indexes[k] = i
2782
+
2783
+ expr = ir.Expr.build_map(
2784
+ items=items,
2785
+ size=2,
2786
+ literal_value=literal_dict,
2787
+ value_indexes=value_indexes,
2788
+ loc=self.loc,
2789
+ )
2790
+
2791
+ self.store(expr, res)
2792
+
2793
+ def op_GET_ITER(self, inst, value, res):
2794
+ expr = ir.Expr.getiter(value=self.get(value), loc=self.loc)
2795
+ self.store(expr, res)
2796
+
2797
+ def op_FOR_ITER(self, inst, iterator, pair, indval, pred):
2798
+ """
2799
+ Assign new block other this instruction.
2800
+ """
2801
+ assert inst.offset in self.blocks, "FOR_ITER must be block head"
2802
+
2803
+ # Emit code
2804
+ val = self.get(iterator)
2805
+
2806
+ pairval = ir.Expr.iternext(value=val, loc=self.loc)
2807
+ self.store(pairval, pair)
2808
+
2809
+ iternext = ir.Expr.pair_first(value=self.get(pair), loc=self.loc)
2810
+ self.store(iternext, indval)
2811
+
2812
+ isvalid = ir.Expr.pair_second(value=self.get(pair), loc=self.loc)
2813
+ self.store(isvalid, pred)
2814
+
2815
+ # Conditional jump
2816
+ br = ir.Branch(
2817
+ cond=self.get(pred),
2818
+ truebr=inst.next,
2819
+ falsebr=inst.get_jump_target(),
2820
+ loc=self.loc,
2821
+ )
2822
+ self.current_block.append(br)
2823
+
2824
+ def op_BINARY_SUBSCR(self, inst, target, index, res):
2825
+ index = self.get(index)
2826
+ target = self.get(target)
2827
+ expr = ir.Expr.getitem(target, index=index, loc=self.loc)
2828
+ self.store(expr, res)
2829
+
2830
+ def op_STORE_SUBSCR(self, inst, target, index, value):
2831
+ index = self.get(index)
2832
+ target = self.get(target)
2833
+ value = self.get(value)
2834
+ stmt = ir.SetItem(target=target, index=index, value=value, loc=self.loc)
2835
+ self.current_block.append(stmt)
2836
+
2837
+ def op_DELETE_SUBSCR(self, inst, target, index):
2838
+ index = self.get(index)
2839
+ target = self.get(target)
2840
+ stmt = ir.DelItem(target=target, index=index, loc=self.loc)
2841
+ self.current_block.append(stmt)
2842
+
2843
+ def op_BUILD_TUPLE(self, inst, items, res):
2844
+ expr = ir.Expr.build_tuple(
2845
+ items=[self.get(x) for x in items], loc=self.loc
2846
+ )
2847
+ self.store(expr, res)
2848
+
2849
+ def op_BUILD_LIST(self, inst, items, res):
2850
+ expr = ir.Expr.build_list(
2851
+ items=[self.get(x) for x in items], loc=self.loc
2852
+ )
2853
+ self.store(expr, res)
2854
+
2855
+ def op_BUILD_SET(self, inst, items, res):
2856
+ expr = ir.Expr.build_set(
2857
+ items=[self.get(x) for x in items], loc=self.loc
2858
+ )
2859
+ self.store(expr, res)
2860
+
2861
+ def op_SET_UPDATE(self, inst, target, value, updatevar, res):
2862
+ target = self.get(target)
2863
+ value = self.get(value)
2864
+ updateattr = ir.Expr.getattr(target, "update", loc=self.loc)
2865
+ self.store(value=updateattr, name=updatevar)
2866
+ updateinst = ir.Expr.call(
2867
+ self.get(updatevar), (value,), (), loc=self.loc
2868
+ )
2869
+ self.store(value=updateinst, name=res)
2870
+
2871
+ def op_DICT_UPDATE(self, inst, target, value, updatevar, res):
2872
+ target = self.get(target)
2873
+ value = self.get(value)
2874
+ # We generate _update_from_bytecode instead of update so we can
2875
+ # differentiate between user .update() calls and those from the
2876
+ # bytecode. This is then used to recombine dictionaries in peephole
2877
+ # optimizations. See the dicussion in this PR about why:
2878
+ # https://github.com/numba/numba/pull/7964/files#r868229306
2879
+ updateattr = ir.Expr.getattr(
2880
+ target, "_update_from_bytecode", loc=self.loc
2881
+ )
2882
+ self.store(value=updateattr, name=updatevar)
2883
+ updateinst = ir.Expr.call(
2884
+ self.get(updatevar), (value,), (), loc=self.loc
2885
+ )
2886
+ self.store(value=updateinst, name=res)
2887
+
2888
+ def op_BUILD_MAP(self, inst, items, size, res):
2889
+ got_items = [(self.get(k), self.get(v)) for k, v in items]
2890
+
2891
+ # sort out literal values, this is a bit contrived but is to handle
2892
+ # situations like `{1: 10, 1: 10}` where the size of the literal dict
2893
+ # is smaller than the definition
2894
+ def get_literals(target):
2895
+ literal_items = []
2896
+ values = [self.get(v.name) for v in target]
2897
+ for v in values:
2898
+ defns = self.definitions[v.name]
2899
+ if len(defns) != 1:
2900
+ break
2901
+ defn = defns[0]
2902
+ if not isinstance(defn, ir.Const):
2903
+ break
2904
+ literal_items.append(defn.value)
2905
+ return literal_items
2906
+
2907
+ literal_keys = get_literals(x[0] for x in got_items)
2908
+ literal_values = get_literals(x[1] for x in got_items)
2909
+
2910
+ has_literal_keys = len(literal_keys) == len(got_items)
2911
+ has_literal_values = len(literal_values) == len(got_items)
2912
+
2913
+ value_indexes = {}
2914
+ if not has_literal_keys and not has_literal_values:
2915
+ literal_dict = None
2916
+ elif has_literal_keys and not has_literal_values:
2917
+ literal_dict = {
2918
+ x: _UNKNOWN_VALUE(y[1]) for x, y in zip(literal_keys, got_items)
2919
+ }
2920
+ for i, k in enumerate(literal_keys):
2921
+ value_indexes[k] = i
2922
+ else:
2923
+ literal_dict = {x: y for x, y in zip(literal_keys, literal_values)}
2924
+ for i, k in enumerate(literal_keys):
2925
+ value_indexes[k] = i
2926
+
2927
+ expr = ir.Expr.build_map(
2928
+ items=got_items,
2929
+ size=size,
2930
+ literal_value=literal_dict,
2931
+ value_indexes=value_indexes,
2932
+ loc=self.loc,
2933
+ )
2934
+ self.store(expr, res)
2935
+
2936
+ def op_STORE_MAP(self, inst, dct, key, value):
2937
+ stmt = ir.StoreMap(
2938
+ dct=self.get(dct),
2939
+ key=self.get(key),
2940
+ value=self.get(value),
2941
+ loc=self.loc,
2942
+ )
2943
+ self.current_block.append(stmt)
2944
+
2945
+ def op_UNARY_NEGATIVE(self, inst, value, res):
2946
+ value = self.get(value)
2947
+ expr = ir.Expr.unary("-", value=value, loc=self.loc)
2948
+ return self.store(expr, res)
2949
+
2950
+ def op_UNARY_POSITIVE(self, inst, value, res):
2951
+ value = self.get(value)
2952
+ expr = ir.Expr.unary("+", value=value, loc=self.loc)
2953
+ return self.store(expr, res)
2954
+
2955
+ def op_UNARY_INVERT(self, inst, value, res):
2956
+ value = self.get(value)
2957
+ expr = ir.Expr.unary("~", value=value, loc=self.loc)
2958
+ return self.store(expr, res)
2959
+
2960
+ def op_UNARY_NOT(self, inst, value, res):
2961
+ value = self.get(value)
2962
+ expr = ir.Expr.unary("not", value=value, loc=self.loc)
2963
+ return self.store(expr, res)
2964
+
2965
+ def _binop(self, op, lhs, rhs, res):
2966
+ op = BINOPS_TO_OPERATORS[op]
2967
+ lhs = self.get(lhs)
2968
+ rhs = self.get(rhs)
2969
+ expr = ir.Expr.binop(op, lhs=lhs, rhs=rhs, loc=self.loc)
2970
+ self.store(expr, res)
2971
+
2972
+ def _inplace_binop(self, op, lhs, rhs, res):
2973
+ immuop = BINOPS_TO_OPERATORS[op]
2974
+ op = INPLACE_BINOPS_TO_OPERATORS[op + "="]
2975
+ lhs = self.get(lhs)
2976
+ rhs = self.get(rhs)
2977
+ expr = ir.Expr.inplace_binop(op, immuop, lhs=lhs, rhs=rhs, loc=self.loc)
2978
+ self.store(expr, res)
2979
+
2980
+ def op_BINARY_OP(self, inst, op, lhs, rhs, res):
2981
+ if "=" in op:
2982
+ self._inplace_binop(op[:-1], lhs, rhs, res)
2983
+ else:
2984
+ self._binop(op, lhs, rhs, res)
2985
+
2986
+ def op_BINARY_ADD(self, inst, lhs, rhs, res):
2987
+ self._binop("+", lhs, rhs, res)
2988
+
2989
+ def op_BINARY_SUBTRACT(self, inst, lhs, rhs, res):
2990
+ self._binop("-", lhs, rhs, res)
2991
+
2992
+ def op_BINARY_MULTIPLY(self, inst, lhs, rhs, res):
2993
+ self._binop("*", lhs, rhs, res)
2994
+
2995
+ def op_BINARY_DIVIDE(self, inst, lhs, rhs, res):
2996
+ self._binop("/?", lhs, rhs, res)
2997
+
2998
+ def op_BINARY_TRUE_DIVIDE(self, inst, lhs, rhs, res):
2999
+ self._binop("/", lhs, rhs, res)
3000
+
3001
+ def op_BINARY_FLOOR_DIVIDE(self, inst, lhs, rhs, res):
3002
+ self._binop("//", lhs, rhs, res)
3003
+
3004
+ def op_BINARY_MODULO(self, inst, lhs, rhs, res):
3005
+ self._binop("%", lhs, rhs, res)
3006
+
3007
+ def op_BINARY_POWER(self, inst, lhs, rhs, res):
3008
+ self._binop("**", lhs, rhs, res)
3009
+
3010
+ def op_BINARY_MATRIX_MULTIPLY(self, inst, lhs, rhs, res):
3011
+ self._binop("@", lhs, rhs, res)
3012
+
3013
+ def op_BINARY_LSHIFT(self, inst, lhs, rhs, res):
3014
+ self._binop("<<", lhs, rhs, res)
3015
+
3016
+ def op_BINARY_RSHIFT(self, inst, lhs, rhs, res):
3017
+ self._binop(">>", lhs, rhs, res)
3018
+
3019
+ def op_BINARY_AND(self, inst, lhs, rhs, res):
3020
+ self._binop("&", lhs, rhs, res)
3021
+
3022
+ def op_BINARY_OR(self, inst, lhs, rhs, res):
3023
+ self._binop("|", lhs, rhs, res)
3024
+
3025
+ def op_BINARY_XOR(self, inst, lhs, rhs, res):
3026
+ self._binop("^", lhs, rhs, res)
3027
+
3028
+ def op_INPLACE_ADD(self, inst, lhs, rhs, res):
3029
+ self._inplace_binop("+", lhs, rhs, res)
3030
+
3031
+ def op_INPLACE_SUBTRACT(self, inst, lhs, rhs, res):
3032
+ self._inplace_binop("-", lhs, rhs, res)
3033
+
3034
+ def op_INPLACE_MULTIPLY(self, inst, lhs, rhs, res):
3035
+ self._inplace_binop("*", lhs, rhs, res)
3036
+
3037
+ def op_INPLACE_DIVIDE(self, inst, lhs, rhs, res):
3038
+ self._inplace_binop("/?", lhs, rhs, res)
3039
+
3040
+ def op_INPLACE_TRUE_DIVIDE(self, inst, lhs, rhs, res):
3041
+ self._inplace_binop("/", lhs, rhs, res)
3042
+
3043
+ def op_INPLACE_FLOOR_DIVIDE(self, inst, lhs, rhs, res):
3044
+ self._inplace_binop("//", lhs, rhs, res)
3045
+
3046
+ def op_INPLACE_MODULO(self, inst, lhs, rhs, res):
3047
+ self._inplace_binop("%", lhs, rhs, res)
3048
+
3049
+ def op_INPLACE_POWER(self, inst, lhs, rhs, res):
3050
+ self._inplace_binop("**", lhs, rhs, res)
3051
+
3052
+ def op_INPLACE_MATRIX_MULTIPLY(self, inst, lhs, rhs, res):
3053
+ self._inplace_binop("@", lhs, rhs, res)
3054
+
3055
+ def op_INPLACE_LSHIFT(self, inst, lhs, rhs, res):
3056
+ self._inplace_binop("<<", lhs, rhs, res)
3057
+
3058
+ def op_INPLACE_RSHIFT(self, inst, lhs, rhs, res):
3059
+ self._inplace_binop(">>", lhs, rhs, res)
3060
+
3061
+ def op_INPLACE_AND(self, inst, lhs, rhs, res):
3062
+ self._inplace_binop("&", lhs, rhs, res)
3063
+
3064
+ def op_INPLACE_OR(self, inst, lhs, rhs, res):
3065
+ self._inplace_binop("|", lhs, rhs, res)
3066
+
3067
+ def op_INPLACE_XOR(self, inst, lhs, rhs, res):
3068
+ self._inplace_binop("^", lhs, rhs, res)
3069
+
3070
+ def op_JUMP_ABSOLUTE(self, inst):
3071
+ jmp = ir.Jump(inst.get_jump_target(), loc=self.loc)
3072
+ self.current_block.append(jmp)
3073
+
3074
+ def op_JUMP_FORWARD(self, inst):
3075
+ jmp = ir.Jump(inst.get_jump_target(), loc=self.loc)
3076
+ self.current_block.append(jmp)
3077
+
3078
+ def op_JUMP_BACKWARD(self, inst):
3079
+ jmp = ir.Jump(inst.get_jump_target(), loc=self.loc)
3080
+ self.current_block.append(jmp)
3081
+
3082
+ op_JUMP_BACKWARD_NO_INTERRUPT = op_JUMP_BACKWARD
3083
+
3084
+ def op_POP_BLOCK(self, inst, kind=None):
3085
+ if kind is None:
3086
+ self.syntax_blocks.pop()
3087
+ elif kind == "with":
3088
+ d = ir.PopBlock(loc=self.loc)
3089
+ self.current_block.append(d)
3090
+ elif kind == "try":
3091
+ self._insert_try_block_end()
3092
+
3093
+ def op_RETURN_VALUE(self, inst, retval, castval):
3094
+ self.store(ir.Expr.cast(self.get(retval), loc=self.loc), castval)
3095
+ ret = ir.Return(self.get(castval), loc=self.loc)
3096
+ self.current_block.append(ret)
3097
+
3098
+ if PYVERSION in ((3, 12), (3, 13)):
3099
+
3100
+ def op_RETURN_CONST(self, inst, retval, castval):
3101
+ value = self.code_consts[inst.arg]
3102
+ const = ir.Const(value, loc=self.loc)
3103
+ self.store(const, retval)
3104
+ self.store(ir.Expr.cast(self.get(retval), loc=self.loc), castval)
3105
+ ret = ir.Return(self.get(castval), loc=self.loc)
3106
+ self.current_block.append(ret)
3107
+ elif PYVERSION in ((3, 10), (3, 11)):
3108
+ pass
3109
+ else:
3110
+ raise NotImplementedError(PYVERSION)
3111
+
3112
+ if PYVERSION in ((3, 13),):
3113
+
3114
+ def op_TO_BOOL(self, inst, val, res):
3115
+ self.store(self.get(val), res) # TODO: just a lazy hack
3116
+
3117
+ elif PYVERSION in ((3, 10), (3, 11), (3, 12)):
3118
+ pass
3119
+ else:
3120
+ raise NotImplementedError(PYVERSION)
3121
+
3122
+ def op_COMPARE_OP(self, inst, lhs, rhs, res):
3123
+ if PYVERSION in ((3, 13),):
3124
+ op = dis.cmp_op[inst.arg >> 5]
3125
+ # TODO: fifth lowest bit now indicates a forced version to bool.
3126
+ elif PYVERSION in ((3, 12),):
3127
+ op = dis.cmp_op[inst.arg >> 4]
3128
+ elif PYVERSION in ((3, 10), (3, 11)):
3129
+ op = dis.cmp_op[inst.arg]
3130
+ else:
3131
+ raise NotImplementedError(PYVERSION)
3132
+ if op == "in" or op == "not in":
3133
+ lhs, rhs = rhs, lhs
3134
+
3135
+ if op == "not in":
3136
+ self._binop("in", lhs, rhs, res)
3137
+ tmp = self.get(res)
3138
+ out = ir.Expr.unary("not", value=tmp, loc=self.loc)
3139
+ self.store(out, res)
3140
+ elif op == "exception match":
3141
+ gv_fn = ir.Global(
3142
+ "exception_match",
3143
+ eh.exception_match,
3144
+ loc=self.loc,
3145
+ )
3146
+ exc_match_name = "$exc_match"
3147
+ self.store(value=gv_fn, name=exc_match_name, redefine=True)
3148
+ lhs = self.get(lhs)
3149
+ rhs = self.get(rhs)
3150
+ exc = ir.Expr.call(
3151
+ self.get(exc_match_name),
3152
+ args=(lhs, rhs),
3153
+ kws=(),
3154
+ loc=self.loc,
3155
+ )
3156
+ self.store(exc, res)
3157
+ else:
3158
+ self._binop(op, lhs, rhs, res)
3159
+
3160
+ def op_IS_OP(self, inst, lhs, rhs, res):
3161
+ # invert if op case is 1
3162
+ op = "is not" if inst.arg == 1 else "is"
3163
+ self._binop(op, lhs, rhs, res)
3164
+
3165
+ def op_CONTAINS_OP(self, inst, lhs, rhs, res):
3166
+ lhs, rhs = rhs, lhs
3167
+ self._binop("in", lhs, rhs, res)
3168
+ # invert if op case is 1
3169
+ if inst.arg == 1:
3170
+ tmp = self.get(res)
3171
+ out = ir.Expr.unary("not", value=tmp, loc=self.loc)
3172
+ self.store(out, res)
3173
+
3174
+ def op_BREAK_LOOP(self, inst, end=None):
3175
+ if end is None:
3176
+ loop = self.syntax_blocks[-1]
3177
+ assert isinstance(loop, ir.Loop)
3178
+ end = loop.exit
3179
+ jmp = ir.Jump(target=end, loc=self.loc)
3180
+ self.current_block.append(jmp)
3181
+
3182
+ def _op_JUMP_IF(self, inst, pred, iftrue):
3183
+ brs = {
3184
+ True: inst.get_jump_target(),
3185
+ False: inst.next,
3186
+ }
3187
+ truebr = brs[iftrue]
3188
+ falsebr = brs[not iftrue]
3189
+
3190
+ name = "$bool%s" % (inst.offset)
3191
+ gv_fn = ir.Global("bool", bool, loc=self.loc)
3192
+ self.store(value=gv_fn, name=name)
3193
+
3194
+ callres = ir.Expr.call(
3195
+ self.get(name), (self.get(pred),), (), loc=self.loc
3196
+ )
3197
+
3198
+ pname = "$%spred" % (inst.offset)
3199
+ predicate = self.store(value=callres, name=pname)
3200
+ bra = ir.Branch(
3201
+ cond=predicate, truebr=truebr, falsebr=falsebr, loc=self.loc
3202
+ )
3203
+ self.current_block.append(bra)
3204
+
3205
+ def op_JUMP_IF_FALSE(self, inst, pred):
3206
+ self._op_JUMP_IF(inst, pred=pred, iftrue=False)
3207
+
3208
+ def op_JUMP_IF_TRUE(self, inst, pred):
3209
+ self._op_JUMP_IF(inst, pred=pred, iftrue=True)
3210
+
3211
+ def _jump_if_none(self, inst, pred, iftrue):
3212
+ # branch pruning assumes true falls through and false is jump
3213
+ truebr = inst.next
3214
+ falsebr = inst.get_jump_target()
3215
+
3216
+ # this seems strange
3217
+ if not iftrue:
3218
+ op = BINOPS_TO_OPERATORS["is"]
3219
+ else:
3220
+ op = BINOPS_TO_OPERATORS["is not"]
3221
+
3222
+ rhs = self.store(
3223
+ value=ir.Const(None, loc=self.loc), name=f"$constNone{inst.offset}"
3224
+ )
3225
+ lhs = self.get(pred)
3226
+ isnone = ir.Expr.binop(op, lhs=lhs, rhs=rhs, loc=self.loc)
3227
+
3228
+ maybeNone = f"$maybeNone{inst.offset}"
3229
+ self.store(value=isnone, name=maybeNone)
3230
+
3231
+ name = f"$bool{inst.offset}"
3232
+ gv_fn = ir.Global("bool", bool, loc=self.loc)
3233
+ self.store(value=gv_fn, name=name)
3234
+
3235
+ callres = ir.Expr.call(
3236
+ self.get(name), (self.get(maybeNone),), (), loc=self.loc
3237
+ )
3238
+
3239
+ pname = f"$pred{inst.offset}"
3240
+ predicate = self.store(value=callres, name=pname)
3241
+ branch = ir.Branch(
3242
+ cond=predicate, truebr=truebr, falsebr=falsebr, loc=self.loc
3243
+ )
3244
+ self.current_block.append(branch)
3245
+
3246
+ def op_POP_JUMP_FORWARD_IF_NONE(self, inst, pred):
3247
+ self._jump_if_none(inst, pred, True)
3248
+
3249
+ def op_POP_JUMP_FORWARD_IF_NOT_NONE(self, inst, pred):
3250
+ self._jump_if_none(inst, pred, False)
3251
+
3252
+ if PYVERSION in ((3, 12), (3, 13)):
3253
+
3254
+ def op_POP_JUMP_IF_NONE(self, inst, pred):
3255
+ self._jump_if_none(inst, pred, True)
3256
+
3257
+ def op_POP_JUMP_IF_NOT_NONE(self, inst, pred):
3258
+ self._jump_if_none(inst, pred, False)
3259
+ elif PYVERSION in ((3, 10), (3, 11)):
3260
+ pass
3261
+ else:
3262
+ raise NotImplementedError(PYVERSION)
3263
+
3264
+ def op_POP_JUMP_BACKWARD_IF_NONE(self, inst, pred):
3265
+ self._jump_if_none(inst, pred, True)
3266
+
3267
+ def op_POP_JUMP_BACKWARD_IF_NOT_NONE(self, inst, pred):
3268
+ self._jump_if_none(inst, pred, False)
3269
+
3270
+ def op_POP_JUMP_FORWARD_IF_FALSE(self, inst, pred):
3271
+ self._op_JUMP_IF(inst, pred=pred, iftrue=False)
3272
+
3273
+ def op_POP_JUMP_FORWARD_IF_TRUE(self, inst, pred):
3274
+ self._op_JUMP_IF(inst, pred=pred, iftrue=True)
3275
+
3276
+ def op_POP_JUMP_BACKWARD_IF_FALSE(self, inst, pred):
3277
+ self._op_JUMP_IF(inst, pred=pred, iftrue=False)
3278
+
3279
+ def op_POP_JUMP_BACKWARD_IF_TRUE(self, inst, pred):
3280
+ self._op_JUMP_IF(inst, pred=pred, iftrue=True)
3281
+
3282
+ def op_POP_JUMP_IF_FALSE(self, inst, pred):
3283
+ self._op_JUMP_IF(inst, pred=pred, iftrue=False)
3284
+
3285
+ def op_POP_JUMP_IF_TRUE(self, inst, pred):
3286
+ self._op_JUMP_IF(inst, pred=pred, iftrue=True)
3287
+
3288
+ def op_JUMP_IF_FALSE_OR_POP(self, inst, pred):
3289
+ self._op_JUMP_IF(inst, pred=pred, iftrue=False)
3290
+
3291
+ def op_JUMP_IF_TRUE_OR_POP(self, inst, pred):
3292
+ self._op_JUMP_IF(inst, pred=pred, iftrue=True)
3293
+
3294
+ def op_CHECK_EXC_MATCH(self, inst, pred, tos, tos1):
3295
+ gv_fn = ir.Global(
3296
+ "exception_match",
3297
+ eh.exception_match,
3298
+ loc=self.loc,
3299
+ )
3300
+ exc_match_name = "$exc_match"
3301
+ self.store(value=gv_fn, name=exc_match_name, redefine=True)
3302
+ lhs = self.get(tos1)
3303
+ rhs = self.get(tos)
3304
+ exc = ir.Expr.call(
3305
+ self.get(exc_match_name),
3306
+ args=(lhs, rhs),
3307
+ kws=(),
3308
+ loc=self.loc,
3309
+ )
3310
+ self.store(exc, pred)
3311
+
3312
+ def op_JUMP_IF_NOT_EXC_MATCH(self, inst, pred, tos, tos1):
3313
+ truebr = inst.next
3314
+ falsebr = inst.get_jump_target()
3315
+ gv_fn = ir.Global(
3316
+ "exception_match",
3317
+ eh.exception_match,
3318
+ loc=self.loc,
3319
+ )
3320
+ exc_match_name = "$exc_match"
3321
+ self.store(value=gv_fn, name=exc_match_name, redefine=True)
3322
+ lhs = self.get(tos1)
3323
+ rhs = self.get(tos)
3324
+ exc = ir.Expr.call(
3325
+ self.get(exc_match_name),
3326
+ args=(lhs, rhs),
3327
+ kws=(),
3328
+ loc=self.loc,
3329
+ )
3330
+ predicate = self.store(exc, pred)
3331
+ bra = ir.Branch(
3332
+ cond=predicate, truebr=truebr, falsebr=falsebr, loc=self.loc
3333
+ )
3334
+ self.current_block.append(bra)
3335
+
3336
+ def op_RERAISE(self, inst, exc):
3337
+ tryblk = self.dfainfo.active_try_block
3338
+ if tryblk is not None:
3339
+ stmt = ir.TryRaise(exception=None, loc=self.loc)
3340
+ self.current_block.append(stmt)
3341
+ self._insert_try_block_end()
3342
+ self.current_block.append(ir.Jump(tryblk["end"], loc=self.loc))
3343
+ else:
3344
+ # Numba can't handle this case and it's caught else where, this is a
3345
+ # runtime guard in case this is reached by unknown means.
3346
+ msg = (
3347
+ f"Unreachable condition reached (op code RERAISE executed)"
3348
+ f"{error_extras['reportable']}"
3349
+ )
3350
+ stmt = ir.StaticRaise(AssertionError, (msg,), self.loc)
3351
+ self.current_block.append(stmt)
3352
+
3353
+ def op_RAISE_VARARGS(self, inst, exc):
3354
+ if exc is not None:
3355
+ exc = self.get(exc)
3356
+ tryblk = self.dfainfo.active_try_block
3357
+ if tryblk is not None:
3358
+ # In a try block
3359
+ stmt = ir.TryRaise(exception=exc, loc=self.loc)
3360
+ self.current_block.append(stmt)
3361
+ self._insert_try_block_end()
3362
+ self.current_block.append(ir.Jump(tryblk["end"], loc=self.loc))
3363
+ else:
3364
+ # Not in a try block
3365
+ stmt = ir.Raise(exception=exc, loc=self.loc)
3366
+ self.current_block.append(stmt)
3367
+
3368
+ def op_YIELD_VALUE(self, inst, value, res):
3369
+ # initialize index to None. it's being set later in post-processing
3370
+ index = None
3371
+ inst = ir.Yield(value=self.get(value), index=index, loc=self.loc)
3372
+ return self.store(inst, res)
3373
+
3374
+ def op_MAKE_FUNCTION(
3375
+ self, inst, name, code, closure, annotations, kwdefaults, defaults, res
3376
+ ):
3377
+ # annotations are ignored by numba but useful for static analysis
3378
+ # re. https://github.com/numba/numba/issues/7269
3379
+ if kwdefaults is not None:
3380
+ msg = "op_MAKE_FUNCTION with kwdefaults is not implemented"
3381
+ raise NotImplementedError(msg)
3382
+ if defaults:
3383
+ if isinstance(defaults, tuple):
3384
+ defaults = tuple([self.get(name) for name in defaults])
3385
+ else:
3386
+ defaults = self.get(defaults)
3387
+
3388
+ assume_code_const = self.definitions[code][0]
3389
+ if not isinstance(assume_code_const, ir.Const):
3390
+ msg = (
3391
+ "Unsupported use of closure. "
3392
+ "Probably caused by complex control-flow constructs; "
3393
+ "e.g. try-except"
3394
+ )
3395
+ raise errors.UnsupportedBytecodeError(msg, loc=self.loc)
3396
+ fcode = assume_code_const.value
3397
+ if name:
3398
+ name = self.get(name)
3399
+ if closure:
3400
+ closure = self.get(closure)
3401
+ expr = ir.Expr.make_function(name, fcode, closure, defaults, self.loc)
3402
+ self.store(expr, res)
3403
+
3404
+ def op_MAKE_CLOSURE(
3405
+ self, inst, name, code, closure, annotations, kwdefaults, defaults, res
3406
+ ):
3407
+ self.op_MAKE_FUNCTION(
3408
+ inst, name, code, closure, annotations, kwdefaults, defaults, res
3409
+ )
3410
+
3411
+ if PYVERSION in ((3, 11), (3, 12), (3, 13)):
3412
+
3413
+ def op_LOAD_CLOSURE(self, inst, res):
3414
+ name = self.func_id.func.__code__._varname_from_oparg(inst.arg)
3415
+ if name in self.code_cellvars:
3416
+ try:
3417
+ gl = self.get(name)
3418
+ except NotDefinedError:
3419
+ msg = "Unsupported use of cell variable encountered"
3420
+ raise NotImplementedError(msg)
3421
+ elif name in self.code_freevars:
3422
+ idx = self.code_freevars.index(name)
3423
+ value = self.get_closure_value(idx)
3424
+ gl = ir.FreeVar(idx, name, value, loc=self.loc)
3425
+ else:
3426
+ assert 0, "unreachable"
3427
+ self.store(gl, res)
3428
+
3429
+ elif PYVERSION in ((3, 10),):
3430
+
3431
+ def op_LOAD_CLOSURE(self, inst, res):
3432
+ n_cellvars = len(self.code_cellvars)
3433
+ if inst.arg < n_cellvars:
3434
+ name = self.code_cellvars[inst.arg]
3435
+ try:
3436
+ gl = self.get(name)
3437
+ except NotDefinedError:
3438
+ msg = "Unsupported use of cell variable encountered"
3439
+ raise NotImplementedError(msg)
3440
+ else:
3441
+ idx = inst.arg - n_cellvars
3442
+ name = self.code_freevars[idx]
3443
+ value = self.get_closure_value(idx)
3444
+ gl = ir.FreeVar(idx, name, value, loc=self.loc)
3445
+ self.store(gl, res)
3446
+ else:
3447
+ raise NotImplementedError(PYVERSION)
3448
+
3449
+ def op_LIST_APPEND(self, inst, target, value, appendvar, res):
3450
+ target = self.get(target)
3451
+ value = self.get(value)
3452
+ appendattr = ir.Expr.getattr(target, "append", loc=self.loc)
3453
+ self.store(value=appendattr, name=appendvar)
3454
+ appendinst = ir.Expr.call(
3455
+ self.get(appendvar), (value,), (), loc=self.loc
3456
+ )
3457
+ self.store(value=appendinst, name=res)
3458
+
3459
+ def op_LIST_EXTEND(self, inst, target, value, extendvar, res):
3460
+ target = self.get(target)
3461
+ value = self.get(value)
3462
+ # If the statements between the current instruction and the target
3463
+ # are N * consts followed by build_tuple AND the target has no items,
3464
+ # it's a situation where a list is being statically initialised, rewrite
3465
+ # the build_tuple as a build_list, drop the extend, and wire up the
3466
+ # target as the result from the build_tuple that's been rewritten.
3467
+
3468
+ # See if this is the first statement in a block, if so its probably from
3469
+ # control flow in a tuple unpack like:
3470
+ # `(*(1, (2,) if predicate else (3,)))`
3471
+ # this cannot be handled as present so raise
3472
+ msg = (
3473
+ "An unsupported bytecode sequence has been encountered: "
3474
+ "op_LIST_EXTEND at the start of a block.\n\nThis could be "
3475
+ "due to the use of a branch in a tuple unpacking statement."
3476
+ )
3477
+ if not self.current_block.body:
3478
+ raise errors.UnsupportedBytecodeError(msg)
3479
+
3480
+ # is last emitted statement a build_tuple?
3481
+ stmt = self.current_block.body[-1]
3482
+ ok = isinstance(stmt.value, ir.Expr) and stmt.value.op == "build_tuple"
3483
+ # check statements from self.current_block.body[-1] through to target,
3484
+ # make sure they are consts
3485
+ build_empty_list = None
3486
+ if ok:
3487
+ for stmt in reversed(self.current_block.body[:-1]):
3488
+ if not isinstance(stmt, ir.Assign):
3489
+ ok = False
3490
+ break
3491
+ # if its not a const, it needs to be the `build_list` for the
3492
+ # target, else it's something else we don't know about so just
3493
+ # bail
3494
+ if isinstance(stmt.value, ir.Const):
3495
+ continue
3496
+
3497
+ # it's not a const, check for target
3498
+ elif isinstance(stmt.value, ir.Expr) and stmt.target == target:
3499
+ build_empty_list = stmt
3500
+ # it's only ok to do this if the target has no initializer
3501
+ # already
3502
+ ok = not stmt.value.items
3503
+ break
3504
+ else:
3505
+ ok = False
3506
+ break
3507
+ if ok and build_empty_list is None:
3508
+ raise errors.UnsupportedBytecodeError(msg)
3509
+ if ok:
3510
+ stmts = self.current_block.body
3511
+ build_tuple_asgn = self.current_block.body[-1]
3512
+ # move build list to last issued statement
3513
+ stmts.append(stmts.pop(stmts.index(build_empty_list)))
3514
+ # fix the build list
3515
+ build_tuple = build_tuple_asgn.value
3516
+ build_list = build_empty_list.value
3517
+ build_list.items = build_tuple.items
3518
+ else:
3519
+ # it's just a list extend with no static init, let it be
3520
+ extendattr = ir.Expr.getattr(target, "extend", loc=self.loc)
3521
+ self.store(value=extendattr, name=extendvar)
3522
+ extendinst = ir.Expr.call(
3523
+ self.get(extendvar), (value,), (), loc=self.loc
3524
+ )
3525
+ self.store(value=extendinst, name=res)
3526
+
3527
+ def op_MAP_ADD(self, inst, target, key, value, setitemvar, res):
3528
+ target = self.get(target)
3529
+ key = self.get(key)
3530
+ value = self.get(value)
3531
+ setitemattr = ir.Expr.getattr(target, "__setitem__", loc=self.loc)
3532
+ self.store(value=setitemattr, name=setitemvar)
3533
+ appendinst = ir.Expr.call(
3534
+ self.get(setitemvar),
3535
+ (
3536
+ key,
3537
+ value,
3538
+ ),
3539
+ (),
3540
+ loc=self.loc,
3541
+ )
3542
+ self.store(value=appendinst, name=res)
3543
+
3544
+ def op_LOAD_ASSERTION_ERROR(self, inst, res):
3545
+ gv_fn = ir.Global("AssertionError", AssertionError, loc=self.loc)
3546
+ self.store(value=gv_fn, name=res)
3547
+
3548
+ # NOTE: The LOAD_METHOD opcode is implemented as a LOAD_ATTR for ease,
3549
+ # however this means a new object (the bound-method instance) could be
3550
+ # created. Conversely, using a pure LOAD_METHOD no intermediary is present
3551
+ # and it is essentially like a pointer grab and forward to CALL_METHOD. The
3552
+ # net outcome is that the implementation in Numba produces the same result,
3553
+ # but in object mode it may be that it runs more slowly than it would if
3554
+ # run in CPython.
3555
+
3556
+ def op_LOAD_METHOD(self, *args, **kws):
3557
+ self.op_LOAD_ATTR(*args, **kws)
3558
+
3559
+ def op_CALL_METHOD(self, *args, **kws):
3560
+ self.op_CALL_FUNCTION(*args, **kws)
3561
+
3562
+ if PYVERSION in ((3, 12), (3, 13)):
3563
+
3564
+ def op_CALL_INTRINSIC_1(self, inst, operand, **kwargs):
3565
+ if operand == ci1op.INTRINSIC_STOPITERATION_ERROR:
3566
+ stmt = ir.StaticRaise(
3567
+ INTRINSIC_STOPITERATION_ERROR, (), self.loc
3568
+ )
3569
+ self.current_block.append(stmt)
3570
+ return
3571
+ elif operand == ci1op.UNARY_POSITIVE:
3572
+ self.op_UNARY_POSITIVE(inst, **kwargs)
3573
+ return
3574
+ elif operand == ci1op.INTRINSIC_LIST_TO_TUPLE:
3575
+ self.op_LIST_TO_TUPLE(inst, **kwargs)
3576
+ return
3577
+ else:
3578
+ raise NotImplementedError(operand)
3579
+ elif PYVERSION in ((3, 10), (3, 11)):
3580
+ pass
3581
+ else:
3582
+ raise NotImplementedError(PYVERSION)
3583
+
3584
+
3585
+ if PYVERSION in ((3, 12), (3, 13)):
3586
+
3587
+ class INTRINSIC_STOPITERATION_ERROR(AssertionError):
3588
+ pass
3589
+ elif PYVERSION in ((3, 10), (3, 11)):
3590
+ pass
3591
+ else:
3592
+ raise NotImplementedError(PYVERSION)